diff diffbind.R @ 13:1de83981d43c draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/diffbind commit 13485bed6a57ec4a34cab4ec6bb8b36d219e3610
author iuc
date Wed, 30 May 2018 12:25:42 -0400
parents 4c7ab9995f9e
children 194e3f2c1d86
line wrap: on
line diff
--- a/diffbind.R	Thu Apr 19 17:15:53 2018 -0400
+++ b/diffbind.R	Wed May 30 12:25:42 2018 -0400
@@ -94,12 +94,49 @@
 
 # Output differential binding sites
 resSorted <- diff_bind[order(diff_bind$FDR),]
-write.table(as.data.frame(resSorted), file = opt$outfile, sep="\t", quote = FALSE, append=TRUE, row.names = FALSE)
+# Convert from GRanges (1-based) to 0-based format (adapted from https://www.biostars.org/p/89341/)
+if (opt$format == "bed") {
+    resSorted  <- data.frame(Chrom=seqnames(resSorted),
+        Start=start(resSorted) - 1,
+        End=end(resSorted),
+        Name=rep("DiffBind", length(resSorted)),
+        Score=rep("0", length(resSorted)),
+        Strand=gsub("\\*", ".", strand(resSorted)))
+} else if (opt$format == "interval") {
+     # Output as interval
+    df <- as.data.frame(resSorted)
+    extrainfo <- NULL
+    for (i in 1:nrow(df)) {
+        extrainfo[i] <- paste0(c(df$width[i], df[i, 6:ncol(df)]), collapse="|")
+    }
+    resSorted  <- data.frame(Chrom=seqnames(resSorted),
+        Start=start(resSorted) - 1,
+        End=end(resSorted),
+        Name=rep("DiffBind", length(resSorted)),
+        Score=rep("0", length(resSorted)),
+        Strand=gsub("\\*", ".", strand(resSorted)),
+        Comment=extrainfo)
+} else {
+    # Output as 0-based tabular
+    resSorted <- data.frame(Chrom=seqnames(resSorted),
+        Start=start(resSorted) - 1,
+        End=end(resSorted),
+        Name=rep("DiffBind", length(resSorted)),
+        Score=rep("0", length(resSorted)),
+        Strand=gsub("\\*", ".", strand(resSorted)),
+        mcols(resSorted))
+}
+write.table(resSorted, file = opt$outfile, sep="\t", quote = FALSE, append=TRUE, row.names = FALSE)
 
 # Output binding affinity scores
 if (!is.null(opt$bmatrix)) {
     bmat <- dba.peakset(sample_count, bRetrieve=TRUE, DataType=DBA_DATA_FRAME)
-    write.table(as.data.frame(bmat), file="bmatrix.tab", sep="\t", quote=FALSE, row.names=FALSE)
+    # Output as 0-based tabular
+    bmat <- data.frame(Chrom=bmat[, 1],
+        Start=bmat[, 2] - 1,
+        End=bmat[, 3],
+        bmat[, 4:ncol(bmat)])
+    write.table(bmat, file="bmatrix.tab", sep="\t", quote=FALSE, row.names=FALSE)
 }
 
 # Output RData file