-
Notifications
You must be signed in to change notification settings - Fork 13
/
xcorr.r
214 lines (183 loc) · 6.23 KB
/
xcorr.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
########################################################################
# JAMMv1.0.8 is a peak finder for joint analysis of NGS replicates.
# Copyright (C) 2014-2019 Mahmoud Ibrahim
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Contact: [email protected]
########################################################################
# =======================
# User-defined variables
# =======================
presched = TRUE #Set preschedule in mclapply to TRUE or FALSE
options(warn = -1, scipen = 1000) #R will not report any warnings (warn = -1), R will not use scientific notation (scipen = 1000).
#=======================> DONE!
# =========================
# Required Libraries check
# =========================
if ((is.element('parallel', installed.packages()[,1]) == FALSE)) {
stop("R package'parallel' is required. Please install it!")
}
suppressPackageStartupMessages(library("parallel"))
#=======================> DONE!
# ==============================================
# Parsing Arguments (source: phantom SPP script)
# ==============================================
args = commandArgs(trailingOnly = TRUE) # Read Arguments from command line
#Set arguments to default values
ibed = NA # input bed file
sFile = NA # chromosome size
storeFile = NA # file to store result
for (each.arg in args) {
if (grepl('^-s=',each.arg)) {
arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
if (! is.na(arg.split[2]) ) {
sFile <- arg.split[2]
} else {
stop('No chromosome size file')
}
###
chromosomes = read.table(sFile, header=FALSE)
chromName = chromosomes$V1 #which chromosome
chromSize = as.numeric(chromosomes$V2) #chromosome size
rm(chromosomes)
###
}
#input bed file
if (grepl('^-ibed=',each.arg)) {
arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
if (! is.na(arg.split[2]) ) {
ibed <- arg.split[2]
} else {
message(paste0(chromName, ": No reads found in one or more replicates!"))
quit()
}
}
#bakcground files directory
if (grepl('-bkgd=',each.arg)) {
arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
if (! is.na(arg.split[2]) ) {
bkgd <- arg.split[2]
} else {
stop('No background file')
}
}
if (grepl('^-rl=',each.arg)) {
arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
if (! is.na(arg.split[2]) ) {
rl <- arg.split[2]
} else {
stop('Read length missing')
}
}
if (grepl('^-d=',each.arg)) {
arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
if (! is.na(arg.split[2]) ) {
storeFile <- arg.split[2]
} else {
stop('No file to store result')
}
}
if (grepl('^-p=',each.arg)) {
arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
if (! is.na(arg.split[2]) ) {
cornum <- as.numeric(arg.split[2])
} else {
stop('No number of cores given')
}
}
#number of replicates
if (grepl('-nreps=',each.arg)) {
arg.split <- strsplit(each.arg,'=',fixed=TRUE)[[1]]
if (! is.na(arg.split[2]) ) {
nreps <- as.numeric(arg.split[2])
}
}
}
#Read in variables
ibed = strsplit(ibed, ",", fixed = TRUE)[[1]]
numdup = length(ibed) #number of replicates
if (bkgd != "None") {
nreps = nreps + 1
}
if (numdup != nreps) {
message(paste0(chromName, ": No reads found in one or more replicates!"))
quit()
}
rl = as.numeric(strsplit(rl, ",", fixed = TRUE)[[1]])
#=======================> DONE!
counting = function(readst, br){
if (max(readst) > max(br)) {
return(rep(0, length = length(br)))
} else {
return(hist(readst, breaks = br, plot = FALSE)$counts)
}
}
countingreads = function(bedfile, readlen, filelist, gv) {
reads = read.table(bedfile, header = FALSE)
reads = cbind(as.character(reads[[1]]), as.character(reads[[2]]))
o = which(filelist == bedfile)
tri = list(as.numeric(reads[reads[,2] == "+",,drop = FALSE][,1]), (as.numeric(reads[reads[,2] == "-",,drop = FALSE][,1])) + readlen[o] - 1)
readcounts = mclapply(tri, counting, br = gv, mc.cores = cornum, mc.preschedule = presched)
return(readcounts)
}
xc = function(countlist) {
if (sum(unlist(countlist)) != 0) {
crossCorrelation = ccf(countlist[[2]], countlist[[1]], plot = FALSE); #xcorr
crossCorrelation$lag = crossCorrelation$lag * 20; #correct lag for counts window
maxCorr = which.max(crossCorrelation$acf);
maxCorr = abs(crossCorrelation$lag[maxCorr]);
return(maxCorr)
} else {
return(0)
}
}
# ==================
# Read Start Count
# ==================
#make the window vector (a vector with start positions of chromosome windows)
genomevec = seq(0, chromSize, by = 20);
if (max(genomevec) < chromSize) {
genomevec = c(genomevec, chromSize);
}
datain = mclapply(ibed, countingreads, readlen = rl, filelist = ibed, gv = genomevec, mc.cores = cornum, mc.preschedule = presched)
if (sum(unlist(datain)) == 0) {
message(paste0(chromName, ", Warning: Read alignments do not match chromosome length, Skipped!"))
quit()
}
#=======================> DONE!
# ==================
# Cross Correlation
# ==================
xcorr = unlist(mclapply(datain, xc, mc.cores = cornum, mc.preschedule = presched))
#=======================> DONE!
# =====================
# Write result to File
# =====================
for (i in 1:length(xcorr)) {
filename = strsplit(ibed[i], "/", fixed = TRUE)[[1]]
filename = filename[length(filename)]
filename = strsplit(filename, ".", fixed = TRUE)[[1]]
filename = filename[3]
if ((xcorr[i] <= 500) && (xcorr[i] >= 50)) { #only write this to file if xcorr value was plausible
message(paste0(chromName, ", ", filename, ": Ok!"))
write(paste(chromName, xcorr[i], sep = "\t"), file = paste0(storeFile, "/xc.", filename, ".tab"), append = TRUE)
} else {
if (xcorr[i]) {
} else {
message(paste0(chromName, ", ", filename, ": Value Not Used!"))
}
}
}
#=======================> DONE!