1
|
1 ########################################################################
|
|
2 # JAMMv1.0.7rev1 is a peak finder for joint analysis of NGS replicates.
|
|
3 # Copyright (C) 2014-2015 Mahmoud Ibrahim
|
|
4 #
|
|
5 # This program is free software: you can redistribute it and/or modify
|
|
6 # it under the terms of the GNU General Public License as published by
|
|
7 # the Free Software Foundation, either version 3 of the License, or
|
|
8 # (at your option) any later version.
|
|
9 #
|
|
10 # This program is distributed in the hope that it will be useful,
|
|
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 # GNU General Public License for more details.
|
|
14 #
|
|
15 # You should have received a copy of the GNU General Public License
|
|
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17 #
|
|
18 # Contact: mahmoud.ibrahim@mdc-berlin.de
|
|
19 ########################################################################
|
0
|
20
|
|
21
|
|
22 # =======================
|
|
23 # User-defined variables
|
|
24 # =======================
|
|
25 options(warn = -1, scipen = 1000) #R will not report any warnings (warn = -1), R will not use scientific notation (scipen = 1000)
|
|
26 #=======================> DONE!
|
|
27
|
|
28
|
|
29
|
|
30
|
|
31 # ================================
|
|
32 # Required Libraries check & load
|
|
33 # ================================
|
|
34 if ((is.element('signal', installed.packages()[,1]) == FALSE) || (is.element('parallel', installed.packages()[,1]) == FALSE)) {
|
|
35 stop("R packages 'signal'and 'parallel' are required. Please install them!")
|
|
36 }
|
|
37 suppressPackageStartupMessages(library("signal"))
|
|
38 suppressPackageStartupMessages(library("parallel"))
|
|
39 #=======================> DONE!
|
|
40
|
|
41
|
|
42
|
|
43
|
|
44
|
|
45 # =================
|
|
46 # Custom Functions
|
|
47 # =================
|
|
48 #Get per-row Geometric mean (takes list, returns vectors, not lists!)
|
|
49 geomeanL <- function(mat){
|
|
50 n = length(mat)
|
|
51 if (n > 1) {
|
|
52 mult = (mat[[1]])*(mat[[2]])
|
|
53 if (n > 2) {
|
|
54 for (i in 3:n) {
|
|
55 mult = mult*(mat[[i]])
|
|
56 }
|
|
57 }
|
|
58 mult = mult^(1/n)
|
|
59 mat = mult
|
|
60 return(mat)
|
|
61 } else {
|
|
62 return(mat[[1]])
|
|
63 }
|
|
64 }
|
|
65
|
|
66
|
|
67
|
|
68 #Get per-row Geometric mean (takes matrix, returns vectors, not lists!)
|
|
69 geomean <- function(mat){
|
|
70 n = NCOL(mat)
|
|
71 if (n > 1) {
|
|
72 mult = (mat[,1])*(mat[,2])
|
|
73 if (n > 2) {
|
|
74 for (i in 3:n) {
|
|
75 mult = mult*(mat[,i])
|
|
76 }
|
|
77 }
|
|
78 mult = mult^(1/n)
|
|
79 mat = mult
|
|
80 }
|
|
81 return(mat)
|
|
82 }
|
|
83
|
|
84
|
|
85
|
|
86 #Read in bed(st2) file
|
|
87 parsein = function(bedfile) {
|
|
88 reads = read.table(bedfile, header = FALSE)[[1]]
|
|
89 return(reads)
|
|
90 }
|
|
91
|
1
|
92 #Read in bedpe(st2)
|
|
93 parseinpe = function(bedfile) {
|
|
94 l = read.table(bedfile, header = FALSE)
|
|
95 l = cbind((l[[1]] + 1), l[[2]])
|
|
96 return(l)
|
|
97 }
|
0
|
98
|
|
99 #Produces normalized extended read counts (takes output of parsein(), return a vector of floats)
|
1
|
100 countreads = function(bedfile, reads, frag, chromsize, filelist, chrcount) {
|
0
|
101
|
|
102 o = which(filelist == bedfile)
|
|
103
|
|
104 counts = vector(mode = "numeric", length = chromsize)
|
|
105
|
|
106 for (j in 1:length(reads[[o]])) {
|
|
107 if ((reads[[o]][j]+frag[o]-1) <= chromsize) {
|
|
108 counts[(reads[[o]][j]):(reads[[o]][j]+frag[o]-1)] = counts[(reads[[o]][j]):(reads[[o]][j]+frag[o]-1)] + 1
|
|
109 }
|
|
110 }
|
|
111
|
|
112 mCount = mean(counts)
|
|
113
|
|
114 if (chrcount == 1) {
|
|
115 counts = counts/mCount
|
|
116 write(paste(mCount), file = paste0(out, "/norma.", o, ".info"))
|
|
117 } else {
|
|
118 meanCounts = mean(as.numeric(read.table(paste0(out, "/norma.", o, ".info"))[[1]]))
|
|
119 if ((mCount > (5*meanCounts)) || (mCount < (0.2*meanCounts))) {
|
|
120 mCount = meanCounts
|
|
121 } else {
|
|
122 write(paste(mCount), file = paste0(out, "/norma.", o, ".info"), append = TRUE)
|
|
123 }
|
|
124 counts = counts/mCount
|
|
125 }
|
|
126
|
|
127 return(counts)
|
|
128 }
|
|
129
|
|
130
|
|
131
|
|
132
|
1
|
133 #Produces normalized extended read counts (takes output of parsein(), return a vector of floats / PAIRED END)
|
|
134 countreadspe = function(bedfile, reads, chromsize, filelist, chrcount) {
|
|
135
|
|
136 o = which(filelist == bedfile)
|
|
137
|
|
138 counts = vector(mode = "numeric", length = chromsize)
|
|
139
|
|
140 for (j in 1:length(reads[[o]][,1])) {
|
|
141 counts[(reads[[o]][j,1]):(reads[[o]][j,2])] = counts[(reads[[o]][j,1]):(reads[[o]][j,2])] + 1
|
|
142 }
|
|
143
|
|
144 mCount = mean(counts)
|
|
145
|
|
146 if (chrcount == 1) {
|
|
147 counts = counts/mCount
|
|
148 write(paste(mCount), file = paste0(out, "/norma.", o, ".info"))
|
|
149 } else {
|
|
150 meanCounts = mean(as.numeric(read.table(paste0(out, "/norma.", o, ".info"))[[1]]))
|
|
151 if ((mCount > (5*meanCounts)) || (mCount < (0.2*meanCounts))) {
|
|
152 mCount = meanCounts
|
|
153 } else {
|
|
154 write(paste(mCount), file = paste0(out, "/norma.", o, ".info"), append = TRUE)
|
|
155 }
|
|
156 counts = counts/mCount
|
|
157 }
|
|
158
|
|
159 return(counts)
|
|
160 }
|
|
161
|
|
162
|
0
|
163 #generate signal
|
|
164 getsig = function(winStart, winEnds, startList, stepSize, chromName) {
|
|
165
|
|
166
|
|
167 plz = which(startList == winStart)
|
|
168 winEnd = winEnds[plz]
|
|
169 rWinSizeTemp = winEnd - winStart + 1
|
|
170
|
|
171 #extract subset of the IP
|
|
172 Rs = matrix(nrow = rWinSizeTemp, ncol = numdup)
|
|
173 for (j in 1:numdup) {
|
|
174 Rs[,j] = counts[[j]][winStart:winEnd]
|
|
175 Rs[,j] = filtfilt(rep(1,80)/80,1,Rs[,j])
|
|
176 }
|
|
177
|
|
178 #extract subset of the background and initial signal
|
|
179 if (bkgd != "None") {
|
|
180 Cs = counts[[numdup+1]][winStart:winEnd]
|
|
181 Cs = filtfilt(rep(1,80)/80,1,Cs)
|
|
182 signal = (geomean(Rs)) - (Cs)
|
|
183 } else {
|
|
184 signal = geomean(Rs)
|
|
185 }
|
|
186
|
|
187
|
|
188 #get binned signal
|
|
189 signal = round(colMeans(matrix(c(signal, rep(0, stepSize - length(signal) %% stepSize)), stepSize)), digits = 2)
|
|
190 steps = seq(winStart, winEnd, by = stepSize)
|
|
191 starts = steps - 1
|
|
192 ends = steps + stepSize - 1
|
|
193 if (ends[length(ends)] > winEnd) {
|
|
194 ends[length(ends)] = winEnd
|
|
195 }
|
|
196
|
|
197 #write to file
|
|
198 write(paste(chromName, starts, ends, signal, sep = " "), file = paste0(out, "/", chromName, plz, ".bedSignal"), ncolumns = 1)
|
|
199
|
|
200
|
|
201 return(NULL)
|
|
202 }
|
|
203 #=======================> DONE!
|
|
204
|
|
205
|
|
206
|
|
207
|
|
208
|
|
209 # ==========================
|
|
210 # Parse-in System Variables
|
|
211 # ==========================
|
|
212 args = commandArgs(trailingOnly = TRUE) # Read Arguments from command line
|
|
213
|
|
214 #Parsing arguments and storing values
|
|
215 for (each.arg in args) {
|
|
216 #bed file names
|
|
217 if (grepl('^-bednames=',each.arg)) {
|
|
218 arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
|
|
219 if (! is.na(arg.split[2]) ) {
|
|
220 bednames <- arg.split[2]
|
|
221 } else {
|
|
222 stop('No bed file names')
|
|
223 }
|
|
224 }
|
|
225 #bed file names
|
|
226 if (grepl('^-chromo=',each.arg)) {
|
|
227 arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
|
|
228 if (! is.na(arg.split[2]) ) {
|
|
229 chromName <- arg.split[2]
|
|
230 } else {
|
|
231 stop('No bed file names')
|
|
232 }
|
|
233 }
|
|
234 #bed files directory
|
|
235 if (grepl('^-frag=',each.arg)) {
|
|
236 arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
|
|
237 if (! is.na(arg.split[2]) ) {
|
1
|
238 frag <- arg.split[2]
|
|
239 }
|
0
|
240 }
|
|
241 #bakcground files directory
|
|
242 if (grepl('^-bkgd=',each.arg)) {
|
|
243 arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
|
|
244 if (! is.na(arg.split[2]) ) {
|
|
245 bkgd <- arg.split[2]
|
|
246 } else {
|
|
247 stop('No background file')
|
|
248 }
|
|
249 }
|
|
250 #bakcground files directory
|
|
251 if (grepl('^-out=',each.arg)) {
|
|
252 arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
|
|
253 if (! is.na(arg.split[2]) ) {
|
|
254 out <- arg.split[2]
|
|
255 } else {
|
|
256 stop('No output directory given')
|
|
257 }
|
|
258 }
|
|
259 #Cluster number
|
|
260 if (grepl('^-clustnummer=',each.arg)) {
|
|
261 arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
|
|
262 if (! is.na(arg.split[2]) ) {
|
|
263 clustnummer <- as.numeric(arg.split[2])
|
|
264 }
|
|
265 }
|
|
266 #regions
|
|
267 if (grepl('^-regions=',each.arg)) {
|
|
268 arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
|
|
269 if (! is.na(arg.split[2]) ) {
|
|
270 regions <- arg.split[2]
|
|
271 }
|
|
272 }
|
|
273 #processor cores
|
|
274 if (grepl('^-p=',each.arg)) {
|
|
275 arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
|
|
276 if (! is.na(arg.split[2]) ) {
|
|
277 cornum <- as.numeric(arg.split[2])
|
|
278 }
|
|
279 }
|
|
280 #chromSize
|
|
281 if (grepl('^-chromoS=',each.arg)) {
|
|
282 arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
|
|
283 if (! is.na(arg.split[2]) ) {
|
|
284 chromSize <- as.numeric(arg.split[2])
|
|
285 }
|
|
286 }
|
|
287 #stepsize
|
|
288 if (grepl('^-sig=',each.arg)) {
|
|
289 arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
|
|
290 if (! is.na(arg.split[2]) ) {
|
|
291 sigstep <- as.numeric(arg.split[2])
|
|
292 }
|
|
293 }
|
|
294 #chrcount
|
|
295 if (grepl('^-chrcount=',each.arg)) {
|
|
296 arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
|
|
297 if (! is.na(arg.split[2]) ) {
|
|
298 chrcount <- as.numeric(arg.split[2])
|
|
299 }
|
|
300 }
|
1
|
301 #alignment type
|
|
302 if (grepl('^-t=',each.arg)) {
|
|
303 arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
|
|
304 if (! is.na(arg.split[2]) ) {
|
|
305 type <- arg.split[2]
|
|
306 }
|
|
307 }
|
0
|
308 }
|
|
309
|
|
310 ##Parse in variables
|
|
311 readsFiles = as.list(strsplit(bednames, ",", fixed = TRUE)[[1]])
|
|
312 numdup = length(readsFiles) #number of replicates
|
|
313 if (bkgd != "None") {
|
|
314 readsFiles[[numdup+1]] = bkgd
|
|
315 }
|
1
|
316 if (type == "single") {
|
|
317 frags = as.numeric(strsplit(frag, ",", fixed = TRUE)[[1]])
|
|
318 }
|
0
|
319 rm(bednames)
|
|
320
|
|
321 options(stringsAsFactors = FALSE)
|
|
322 #=======================> DONE!
|
|
323
|
|
324
|
|
325
|
|
326
|
|
327 # =======================
|
|
328 # Some preliminary stuff
|
|
329 # =======================
|
|
330 #import regions
|
|
331 regions = read.table(regions, header = FALSE)
|
|
332 regions = regions[regions[[1]] == chromName, , drop = FALSE]
|
|
333 regions = cbind(regions[[2]] + 1, regions[[3]])
|
|
334
|
|
335 #import read data
|
1
|
336 if (type == "single") {
|
|
337 if (cornum > 1) {
|
|
338 datain = mclapply(readsFiles, parsein, mc.cores = cornum, mc.preschedule = TRUE) #read in all bed files (samples and control)
|
|
339 } else {
|
|
340 datain = lapply(readsFiles, parsein) #read in all bed files (samples and control)
|
|
341 }
|
|
342 }
|
|
343 #import read data
|
|
344 if (type == "paired") {
|
|
345 if (cornum > 1) {
|
|
346 datain = mclapply(readsFiles, parseinpe, mc.cores = cornum, mc.preschedule = TRUE) #read in all bed files (samples and control)
|
|
347 } else {
|
|
348 datain = lapply(readsFiles, parseinpe) #read in all bed files (samples and control)
|
|
349 }
|
0
|
350 }
|
|
351 #=======================> DONE!
|
|
352
|
|
353
|
|
354
|
|
355 # ===============
|
|
356 # Counting Reads
|
|
357 # ===============
|
1
|
358 if (type == "single") {
|
|
359 if (cornum > 1) {
|
|
360 counts = mclapply(readsFiles, countreads, reads = datain, frag = frags, chromsize = chromSize, filelist = readsFiles, chrcount = chrcount, mc.cores = cornum, mc.preschedule = TRUE)
|
|
361 } else {
|
|
362 counts = lapply(readsFiles, countreads, reads = datain, frag = frags, chromsize = chromSize, filelist = readsFiles, chrcount = chrcount)
|
|
363 }
|
|
364 }
|
|
365
|
|
366 if (type == "paired") {
|
|
367 if (cornum > 1) {
|
|
368 counts = mclapply(readsFiles, countreadspe, reads = datain, chromsize = chromSize, filelist = readsFiles, chrcount = chrcount, mc.cores = cornum, mc.preschedule = TRUE)
|
|
369 } else {
|
|
370 counts = lapply(readsFiles, countreadspe, reads = datain, chromsize = chromSize, filelist = readsFiles, chrcount = chrcount)
|
|
371 }
|
0
|
372 }
|
|
373 rm(datain)
|
|
374 #=======================> DONE!
|
|
375
|
|
376
|
|
377
|
|
378 # ==============
|
|
379 # Getting signal
|
|
380 # ==============
|
|
381 if (cornum > 1) {
|
|
382 sig = mclapply(regions[,1], getsig, winEnds = regions[,2], startList = regions[,1], stepSize = sigstep, chromName, mc.cores = cornum, mc.preschedule = TRUE)
|
|
383 } else {
|
|
384 sig = lapply(regions[,1], getsig, winEnds = regions[,2], startList = regions[,1], stepSize = sigstep, chromName)
|
|
385 }
|
|
386 message("Done!")
|
|
387 #=======================> DONE!
|