-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcount_reads_per_base.R
253 lines (189 loc) · 6.36 KB
/
count_reads_per_base.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
args <- commandArgs(trailingOnly = TRUE)
reads<-args[1]
insertion<- args[2]
deletion<- args[3]
pileup<- args[4]
# filename.reads<- args[5]
filename.counts<-args[5]
print(args)
#load dataset
library(GenomicAlignments)
library(rtracklayer)
library(Rsamtools)
library(biovizBase)
library(BSgenome.Hsapiens.UCSC.hg19)
library(lattice)
library(plyr)
library(dplyr)
library(data.table)
library(reshape)
library(rmutil)
library(lattice)
library(stringr)
library(pbapply)
library(readr)
print("initialising counts...................")
# colnames for the pileup files
set.cols<- c("seqnames", "start", "pileup")
# read insertion containing reads
print("reading insertion pileup")
if(file.info(insertion)$size >0){
# read insertion pileup file
print("count insertions per base")
ins<- as.data.frame(fread(insertion, sep="\t"))
colnames(ins)<- set.cols
ins$pileup<- as.character(ins$pileup)
ins$INS<- str_count(ins$pileup,"\\+")
# add end co-ordinates of the insertion
ins$end<- ins$start+1
# add insertion sizes
print("get insertion size")
ins.size<- function(i){
p<- as.character(ins$pileup[i])
return(str_match_all(string = p, "\\+[0-9]+") %>%
unlist %>% unique %>% as.numeric %>%
abs %>% mean %>% floor)
}
ins$size<- unlist(lapply(1:nrow(ins), ins.size))
# add identifier
ins$bpos<- paste(ins$seqnames, ins$start, sep=":")
}else{
ins<- data.frame(NULL)
}
# read deletion pileups
print ("reading deletion pileup")
if(file.info(deletion)$size >0){
# read deletions
print("count deletion per base")
del<- as.data.frame(fread(deletion, sep="\t"))
colnames(del)<- set.cols
del$pileup<- as.character(del$pileup)
del$DEL<- str_count(del$pileup,"\\-")
# get end position for deletion
print ("get deletion sizes")
del.size<- function(i){
p<- as.character(del$pileup[i])
return(str_match_all(string = p, "\\-[0-9]+") %>%
unlist %>% unique %>% as.numeric %>%
abs %>% mean %>% floor)
}
del$size<- unlist(lapply(1:nrow(del), del.size))
# add identifier:: DEL sites have 2 identifiers i.e. the start and end position
del$end= del$start + del$size
del$start.bpos= paste(del$seqnames, del$start, sep=":")
del$end.bpos= paste(del$seqnames, del$end, sep=":")
}else{
del<- data.frame(NULL)
}
# get base positions within peaks
print("getting all pileup positions")
pileup <- as.data.frame(fread(pileup, sep="\t")[,c(1,2)])
colnames(pileup)<- c("seqnames", "start")
pileup$end<- pileup$start
pileup$bpos<- paste(pileup$seqnames, pileup$start, sep=":")
# remove unnecessary chr
pileup<- pileup[!pileup$seqnames %in% c("chrM", "chrY", "chrMT", "M", "MT", "Y"),]
rownames(pileup)<- NULL
# make subject positions
subj<- makeGRangesFromDataFrame(pileup, keep.extra.columns = T)
# reads
# make Granges
print("importing reads and converting to Granges")
f= makeGRangesFromDataFrame(as.data.frame(fread(reads, sep="\t",fill=TRUE)), keep.extra.columns = T)
# # get reads which contain soft-clipped bases
# soft.clipped<- f[grep("S", f$cigar)]
# # process soft-clipped
# process_clipped<- function(query, subject){
# # for each chunk assign SC reads to bases
# print("overlap soft clipped and base positions")
# hits<- findOverlaps(query, subject)
# pos<- query[queryHits(hits)]
# pos$subj.pos= start(subject[subjectHits(hits)])
# # parse positions and select correct reads
# df<- as.data.frame(pos)
# df<- df[!duplicated(df),]
# #calculate distance from start and end of the reads
# df$dist2start= as.numeric(df$subj.pos) - as.numeric(df$start)
# df$dist2end=as.numeric(df$subj.pos) - as.numeric(df$end)
# df$bpos<- paste(df$seqnames, df$subj.pos, sep=":")
# # remove positions where distance to start or end in non-zero
# sc= subset(df,dist2start==0|dist2end==0)
# # true positions for each type of reads
# sm= subset(sc,dist2start==0 & com=="S;M")
# ms= subset(sc,dist2end==0 & com=="M;S")
# ss= subset(sc,com=="S;S")
# # make new true count dataframe
# n.sc<- rbind(sm, ms, ss)
# # make sure the important fields are characters
# n.sc$bpos= as.character(n.sc$bpos)
# n.sc$com=as.character(n.sc$com)
# # assign S.S type reads correctly
# print("output soft clipped reads")
# n.sc$n.com[(n.sc$dist2start==0)]<- "left"
# n.sc$n.com[(n.sc$dist2end==0)]<- "right"
# return(n.sc)
# }
# # run soft-clipped processing function
# if(length(soft.clipped) > 0){
# n.sc<- process_clipped(query = soft.clipped, subject = subj)
# write_tsv(n.sc, filename.reads)
# }else{
# n.sc<- data.frame(NULL)
# }
# # function count reads per soft-clipped base
# count_clipped<- function(data){
# counts= data.frame(with(data, table(bpos,n.com)))
# SC = dcast(counts , bpos~n.com,value.var="Freq")
# return(SC)
# }
# # running count reads for soft-clipped reads function
# if(nrow(n.sc)> 0){
# SC<- count_clipped(n.sc)
# cols<- c("bpos", "left", "right")
# missing.cols<- cols[!cols %in% colnames(SC)]
# if(length(missing.cols)> 0){
# SC[[missing.cols]]<- 0
# }else{
# SC<- SC
# }
# }else{
# SC<- data.frame(NULL)
# }
# add total counts
print("count total reads per base")
subj$N<- countOverlaps(subj,f)
# make count matrix
mat<- as.data.frame(mcols(subj)[, c("bpos", "N")])
# match and add ins
if(nrow(ins)> 0){
mat$INS<- ins$INS[match(mat$bpos, ins$bpos)]
}else{
mat$INS<- 0
}
# match and add del
if(nrow(del)> 0){
dl<- data.frame(bpos= c(del$start.bpos, del$end.bpos),
DEL= rep(del$DEL,2))
mat$DEL<- dl$DEL[match(mat$bpos, dl$bpos)]
}else{
mat$DEL<- 0
}
mat[is.na(mat)]<- 0
# # match and add SC
# if (nrow(SC)>0){
# m= match(mat$bpos, SC$bpos)
# mat$left<- SC$left[m]
# mat$right<- SC$right[m]
# mat[is.na(mat)]<- 0
# mat$SC<- rowSums(mat[,c("left", "right")])
# }else{
# mat[is.na(mat)]<- 0
# mat$left<-0
# mat$right<- 0
# mat$SC<- 0
# }
# write count data
print("output counts")
# mat<- mat[, c("bpos", "N", "INS", "DEL", "left", "right", "SC")]
mat<- mat[, c("bpos", "N", "INS", "DEL")]
write_tsv(mat, filename.counts)