-
Notifications
You must be signed in to change notification settings - Fork 17
/
OKseq_switches.R
153 lines (114 loc) · 4.34 KB
/
OKseq_switches.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
#args = commandArgs(trailingOnly=TRUE)
if (!require(argparse)) {
PrintErrorMessage()
stop("Please install package names \"rtracklayer\"")
}
library(argparse)
parser <- ArgumentParser()
parser$add_argument("-i", "--input", type="character", default="test.bw",
help="Input OK-seq bigWig file.")
parser$add_argument("-b", "--binsize", type="integer", default=50000,
help="Bin size [default %(default)s]")
parser$add_argument("-l", "--leftmin", type="double", default=-0.15,
help="Mean value of bin on left side [default %(default)s]")
parser$add_argument("-r", "--rightmin", type="double", default=0.15,
help="Mean value of bin on right side [default %(default)s]")
parser$add_argument("-d", "--diff", type="double", default=0.4,
help="Minimum difference between left and right side means [default %(default)s]")
parser$add_argument("-s", "--slidesize", type="double", default=50000,
help="Minimum difference between left and right side means [default %(default)s]")
args <- parser$parse_args()
min_left_value = args$leftmin
min_right_value = args$rightmin
min_diff = args$diff
windowsize = args$binsize
infile = args$input
sliding_window = args$slidesize
if(length(args) < 1){
parser$print_help()
stop("Not enough arguments. Replication timing bigwig file has to be specified (created with BAMscale).")
}
if(!file.exists(infile)) {
parser$print_help()
stop(paste0("Input file specified does not exist: \"", infile, "\""))
}
if (!require(rtracklayer)) {
parser$print_help()
stop("Please install package names \"rtracklayer\"")
}
library(rtracklayer)
GetMergedElements = function(fbw, typeseg) {
fbw = fbw[fbw$segments == typeseg,]
fbw = reduce(fbw)
fbw$length = width(fbw)
fbw = fbw[fbw$length > min_segment_length]
fbw$segments = typeseg
return(fbw)
}
outfile = gsub(pattern ="\\.bw|\\.bigWig|\\.bigwig", replacement = "", x = infile)
outfile = paste0(outfile, ".OKseq_switches.bed")
include_zeroes_segment_thresholds = 0
bw = import(infile)
dat_bw = as.data.frame(bw)
chrnames = as.character(unique(dat_bw$seqnames))
chrnames = chrnames[grepl(pattern = "_", chrnames) == F]
df <- data.frame(chr=character(),
start=numeric(),
end=numeric(),
stringsAsFactors=FALSE)
binsize = dat_bw[1, "end"] - dat_bw[1, 'start'] + 1
windowsize = windowsize / binsize
rsize = args$binsize
for(j in 1:length(chrnames)) {
print(chrnames[j])
dat = dat_bw[dat_bw$seqnames == chrnames[j],]
endcoord = dat[nrow(dat), "end"]
nbins = (endcoord - rsize) / sliding_window
lastpos = -1
reslist = list()
for(i in 1:nbins) {
startcoord = as.integer(((i-1)*sliding_window)) - 1
endcoord = as.integer(startcoord + rsize*3) + 2
tdat = dat[dat$start >= (startcoord-1) & dat$end <= (endcoord + 2),]
tscore = rep(tdat$score, tdat$width)
if(length(tscore) >= rsize*2) {
lscore = mean(tscore[0:rsize])
rscore = mean(tscore[(length(tscore)-rsize):length(tscore)])
if(rscore > lscore) {
if(rscore - lscore >= min_diff & lscore <= min_left_value & rscore >= min_right_value) {
reslist = append(reslist, list(chrnames[j], startcoord, endcoord))
lastpos = endcoord
}
}
}
}
if(length(reslist) > 0) {
leftpos = data.frame(matrix(unlist(reslist), nrow=length(reslist) / 3, byrow=T),stringsAsFactors=FALSE)
colnames(leftpos) = c("chr", "start", "end")
leftpos$start = as.numeric(leftpos$start)
leftpos$end = as.numeric(leftpos$end)
if(nrow(df) == 0) {
df = leftpos
} else {
df = merge(df, leftpos, all = T, no.dup = F)
}
}
}
leftpos = df
leftpos$tval = "."
leftpos$nval = "."
leftpos$color = "77,0,0"
rightpos = leftpos
rightpos$start = rightpos$start + 2*rsize
rightpos$end = rightpos$start + rsize
rightpos$color = "19,108,0"
leftpos$end = leftpos$start + rsize
merged = rbind(leftpos, rightpos)
interpos = leftpos
interpos$start = interpos$start + rsize
interpos$end = interpos$start + rsize
interpos$color = "240,230,140"
merged = rbind(merged, interpos)
merged = merged[order(merged$chr, merged$start),]
merged = merged[,c("chr", "start", "end", "tval", "tval", "nval", "start", "end", "color")]
write.table(merged, outfile, quote = F, sep = "\t", col.names = F, row.names = F)