comparison diffbind.R @ 30:6b235ac52faf draft default tip

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/diffbind commit fd148a124034b44d0d61db3eec32ff991d8c152c
author iuc
date Mon, 08 Jul 2024 18:31:37 +0000
parents f567dc4145ac
children
comparison
equal deleted inserted replaced
29:3aa2c26cc990 30:6b235ac52faf
1 ## Setup R error handling to go to stderr 1 ## Setup R error handling to go to stderr
2 options(show.error.messages = F, error = function() { 2 options(show.error.messages = F, error = function() {
3 cat(geterrmessage(), file = stderr()); q("no", 1, F) 3 cat(geterrmessage(), file = stderr())
4 q("no", 1, F)
4 }) 5 })
5 # we need that to not crash galaxy with an UTF8 error on German LC settings. 6 # we need that to not crash galaxy with an UTF8 error on German LC settings.
6 Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") 7 Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
7 8
8 suppressPackageStartupMessages({ 9 suppressPackageStartupMessages({
12 }) 13 })
13 14
14 options(stringAsfactors = FALSE, useFancyQuotes = FALSE) 15 options(stringAsfactors = FALSE, useFancyQuotes = FALSE)
15 args <- commandArgs(trailingOnly = TRUE) 16 args <- commandArgs(trailingOnly = TRUE)
16 17
17 #get options, using the spec as defined by the enclosed list. 18 # get options, using the spec as defined by the enclosed list.
18 #we read the options from the default: commandArgs(TRUE). 19 # we read the options from the default: commandArgs(TRUE).
19 spec <- matrix(c( 20 spec <- matrix(c(
20 "infile", "i", 1, "character", 21 "infile", "i", 1, "character",
21 "outfile", "o", 1, "character", 22 "outfile", "o", 1, "character",
23 "method", "m", 1, "character",
22 "scorecol", "n", 1, "integer", 24 "scorecol", "n", 1, "integer",
23 "lowerbetter", "l", 1, "logical", 25 "lowerbetter", "l", 1, "logical",
24 "summits", "s", 1, "integer", 26 "summits", "s", 1, "integer",
25 "th", "t", 1, "double", 27 "th", "t", 1, "double",
28 "minoverlap", "O", 1, "integer",
29 "use_blacklist", "B", 0, "logical",
26 "format", "f", 1, "character", 30 "format", "f", 1, "character",
27 "plots", "p", 2, "character", 31 "plots", "p", 2, "character",
28 "bmatrix", "b", 0, "logical", 32 "bmatrix", "b", 0, "logical",
29 "rdaOpt", "r", 0, "logical", 33 "rdaOpt", "r", 0, "logical",
30 "infoOpt", "a", 0, "logical", 34 "infoOpt", "a", 0, "logical",
31 "verbose", "v", 2, "integer", 35 "verbose", "v", 2, "integer",
32 "help", "h", 0, "logical" 36 "help", "h", 0, "logical"
33 ), byrow = TRUE, ncol = 4); 37 ), byrow = TRUE, ncol = 4)
34 38
35 opt <- getopt(spec); 39 opt <- getopt(spec)
36
37 # if help was asked for print a friendly message 40 # if help was asked for print a friendly message
38 # and exit with a non-zero error code 41 # and exit with a non-zero error code
39 if (!is.null(opt$help)) { 42 if (!is.null(opt$help)) {
40 cat(getopt(spec, usage = TRUE)); 43 cat(getopt(spec, usage = TRUE))
41 q(status = 1); 44 q(status = 1)
42 } 45 }
43 46
44 parser <- newJSONParser() 47 parser <- newJSONParser()
45 parser$addData(opt$infile) 48 parser$addData(opt$infile)
46 factor_list <- parser$getObject() 49 factor_list <- parser$getObject()
52 # get the group and sample id from the peaks filenames 55 # get the group and sample id from the peaks filenames
53 groups <- sapply(strsplit(peaks, "-"), `[`, 1) 56 groups <- sapply(strsplit(peaks, "-"), `[`, 1)
54 samples <- sapply(strsplit(peaks, "-"), `[`, 2) 57 samples <- sapply(strsplit(peaks, "-"), `[`, 2)
55 58
56 if (length(ctrls) != 0) { 59 if (length(ctrls) != 0) {
57 sample_table <- data.frame(SampleID = samples, 60 sample_table <- data.frame(
58 Condition = groups, 61 SampleID = samples,
59 bamReads = bams, 62 Condition = groups,
60 bamControl = ctrls, 63 bamReads = bams,
61 Peaks = peaks, 64 bamControl = ctrls,
62 Tissue = samples) # using "Tissue" column to display ids as labels in PCA plot 65 Peaks = peaks,
66 Tissue = samples
67 ) # using "Tissue" column to display ids as labels in PCA plot
63 } else { 68 } else {
64 69 sample_table <- data.frame(
65 sample_table <- data.frame(SampleID = samples, 70 SampleID = samples,
66 Replicate = samples, 71 Replicate = samples,
67 Condition = groups, 72 Condition = groups,
68 bamReads = bams, 73 bamReads = bams,
69 Peaks = peaks, 74 Peaks = peaks,
70 Tissue = samples) 75 Tissue = samples
76 )
71 } 77 }
72 78
73 sample <- dba(sampleSheet = sample_table, peakFormat = "bed", scoreCol = opt$scorecol, bLowerScoreBetter = opt$lowerbetter) 79 sample <- dba(sampleSheet = sample_table, peakFormat = "bed", scoreCol = opt$scorecol, bLowerScoreBetter = opt$lowerbetter, minOverlap = opt$minoverlap)
80
81 if (!is.null(opt$use_blacklist)) {
82 sample <- dba.blacklist(sample, blacklist = TRUE)
83 }
74 84
75 if (!is.null(opt$summits)) { 85 if (!is.null(opt$summits)) {
76 sample_count <- dba.count(sample, summits = opt$summits) 86 sample_count <- dba.count(sample, summits = opt$summits)
77 } else { 87 } else {
78 sample_count <- dba.count(sample) 88 sample_count <- dba.count(sample)
79 } 89 }
80 90
81 sample_contrast <- dba.contrast(sample_count, categories = DBA_CONDITION, minMembers = 2) 91 sample_contrast <- dba.contrast(sample_count, categories = DBA_CONDITION, minMembers = 2)
82 sample_analyze <- dba.analyze(sample_contrast) 92
83 diff_bind <- dba.report(sample_analyze, th = opt$th) 93 if (opt$method == "DBA_DESEQ2") {
94 method <- DBA_DESEQ2
95 } else if (opt$method == "DBA_EDGER") {
96 method <- DBA_EDGER
97 }
98
99 sample_analyze <- dba.analyze(sample_contrast, method = method, bBlacklist = FALSE, bGreylist = FALSE)
100
101 diff_bind <- dba.report(sample_analyze, th = opt$th, method = method)
84 102
85 # Generate plots 103 # Generate plots
86 if (!is.null(opt$plots)) { 104 if (!is.null(opt$plots)) {
87 pdf(opt$plots) 105 pdf(opt$plots)
88 orvals <- dba.plotHeatmap(sample_analyze, contrast = 1, correlations = FALSE, cexCol = 0.8, th = opt$th) 106 orvals <- dba.plotHeatmap(sample_analyze, contrast = 1, correlations = FALSE, cexCol = 0.8, th = opt$th, method = method)
89 dba.plotPCA(sample_analyze, contrast = 1, th = opt$th, label = DBA_TISSUE, labelSize = 0.3) 107 dba.plotPCA(sample_analyze, contrast = 1, th = opt$th, label = DBA_TISSUE, labelSize = 0.3, method = method)
90 dba.plotMA(sample_analyze, th = opt$th) 108 dba.plotMA(sample_analyze, th = opt$th, method = method)
91 dba.plotVolcano(sample_analyze, th = opt$th) 109 dba.plotVolcano(sample_analyze, th = opt$th, method = method)
92 dba.plotBox(sample_analyze, th = opt$th) 110 dba.plotBox(sample_analyze, th = opt$th, method = method)
93 dev.off() 111 dev.off()
94 } 112 }
95 113
96 # Output differential binding sites 114 # Output differential binding sites
97 res_sorted <- diff_bind[order(diff_bind$FDR), ] 115 res_sorted <- diff_bind[order(diff_bind$FDR), ]
98 # Convert from GRanges (1-based) to 0-based format (adapted from https://www.biostars.org/p/89341/) 116 # Convert from GRanges (1-based) to 0-based format (adapted from https://www.biostars.org/p/89341/)
99 if (opt$format == "bed") { 117 if (opt$format == "bed") {
100 res_sorted <- data.frame(Chrom = seqnames(res_sorted), 118 res_sorted <- data.frame(
119 Chrom = seqnames(res_sorted),
101 Start = start(res_sorted) - 1, 120 Start = start(res_sorted) - 1,
102 End = end(res_sorted), 121 End = end(res_sorted),
103 Name = rep("DiffBind", length(res_sorted)), 122 Name = rep("DiffBind", length(res_sorted)),
104 Score = rep("0", length(res_sorted)), 123 Score = rep("0", length(res_sorted)),
105 Strand = gsub("\\*", ".", strand(res_sorted))) 124 Strand = gsub("\\*", ".", strand(res_sorted))
125 )
106 } else if (opt$format == "interval") { 126 } else if (opt$format == "interval") {
107 # Output as interval 127 # Output as interval
108 df <- as.data.frame(res_sorted) 128 df <- as.data.frame(res_sorted)
109 extrainfo <- NULL 129 extrainfo <- NULL
110 for (i in seq_len(nrow(df))) { 130 for (i in seq_len(nrow(df))) {
111 extrainfo[i] <- paste0(c(df$width[i], df[i, 6:ncol(df)]), collapse = "|") 131 extrainfo[i] <- paste0(c(df$width[i], df[i, 6:ncol(df)]), collapse = "|")
112 } 132 }
113 res_sorted <- data.frame(Chrom = seqnames(res_sorted), 133 res_sorted <- data.frame(
134 Chrom = seqnames(res_sorted),
114 Start = start(res_sorted) - 1, 135 Start = start(res_sorted) - 1,
115 End = end(res_sorted), 136 End = end(res_sorted),
116 Name = rep("DiffBind", length(res_sorted)), 137 Name = rep("DiffBind", length(res_sorted)),
117 Score = rep("0", length(res_sorted)), 138 Score = rep("0", length(res_sorted)),
118 Strand = gsub("\\*", ".", strand(res_sorted)), 139 Strand = gsub("\\*", ".", strand(res_sorted)),
119 Comment = extrainfo) 140 Comment = extrainfo
141 )
120 } else { 142 } else {
121 # Output as 0-based tabular 143 # Output as 0-based tabular
122 res_sorted <- data.frame(Chrom = seqnames(res_sorted), 144 res_sorted <- data.frame(
145 Chrom = seqnames(res_sorted),
123 Start = start(res_sorted) - 1, 146 Start = start(res_sorted) - 1,
124 End = end(res_sorted), 147 End = end(res_sorted),
125 Name = rep("DiffBind", length(res_sorted)), 148 Name = rep("DiffBind", length(res_sorted)),
126 Score = rep("0", length(res_sorted)), 149 Score = rep("0", length(res_sorted)),
127 Strand = gsub("\\*", ".", strand(res_sorted)), 150 Strand = gsub("\\*", ".", strand(res_sorted)),
128 mcols(res_sorted)) 151 mcols(res_sorted)
152 )
129 } 153 }
130 write.table(res_sorted, file = opt$outfile, sep = "\t", quote = FALSE, row.names = FALSE) 154 write.table(res_sorted, file = opt$outfile, sep = "\t", quote = FALSE, row.names = FALSE)
131 155
132 # Output binding affinity scores 156 # Output binding affinity scores
133 if (!is.null(opt$bmatrix)) { 157 if (!is.null(opt$bmatrix)) {
134 bmat <- dba.peakset(sample_count, bRetrieve = TRUE, DataType = DBA_DATA_FRAME) 158 bmat <- dba.peakset(sample_count, bRetrieve = TRUE, DataType = DBA_DATA_FRAME, minOverlap = opt$minoverlap)
135 # Output as 0-based tabular 159 # Output as 0-based tabular
136 bmat <- data.frame(Chrom = bmat[, 1], 160 bmat <- data.frame(
161 Chrom = bmat[, 1],
137 Start = bmat[, 2] - 1, 162 Start = bmat[, 2] - 1,
138 End = bmat[, 3], 163 End = bmat[, 3],
139 bmat[, 4:ncol(bmat)]) 164 bmat[, 4:ncol(bmat)]
165 )
140 write.table(bmat, file = "bmatrix.tab", sep = "\t", quote = FALSE, row.names = FALSE) 166 write.table(bmat, file = "bmatrix.tab", sep = "\t", quote = FALSE, row.names = FALSE)
141 } 167 }
142 168
143 # Output RData file 169 # Output RData file
144 if (!is.null(opt$rdaOpt)) { 170 if (!is.null(opt$rdaOpt)) {