-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing_cigar.Rmd
313 lines (254 loc) · 8.46 KB
/
parsing_cigar.Rmd
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
---
title: "Nanograd"
author: "Bhavika Kumar"
date: "2023-03-20"
output: html_document
editor_options:
chunk_output_type: console
---
This script is taking in the bam file converting into data frame and parsing cigar to get map, del, insert and sum of soft clip len and also max del len, max insert len and max soft clip len.
The output of the script is the dataframe that has all the lens calculated, including the read len and the number of reads a transcript has.
The script contains paths to all three bam files (undegraded, heavily degraded and mildly degraded)
Loading Libraries
```{r warning=FALSE, message=FALSE}
suppressPackageStartupMessages({
library(tidyverse)
library(GenomicFeatures)
library(rtracklayer)
library(dplyr)
library(Rsamtools)
library(glmnet)
library(ggpubr)
library(visreg)
library(DESeq2)
library(apeglm)
})
```
Importing a Bam file
1. Heavily Degraded sample- rep 1
```{r warning=FALSE, message=FALSE}
bam_file <- scanBam("/home/bhavika/Desktop/Nanograd/Data/bam_files/all.5mM_MgCl_degrdation_pass1.fastq.gz.sorted.bam")
```
Subset bam file- degraded
```{r warning=FALSE, message=FALSE}
bam_file <- scanBam("/home/bhavika/Desktop/Nanograd/Data/bam_files/subset_heavy.bam")
```
Rep 2
```{r warning=FALSE, message=FALSE}
bam_file <- scanBam("/home/bhavika/Desktop/Nanograd/Data/bam_files/all.5mM_MgCl_degrdation_pass2.fastq.gz.sorted.bam")
```
2. Undegraded sample- rep 1
```{r warning=FALSE, message=FALSE}
bam_file <- scanBam("/home/bhavika/Desktop/Nanograd/Data/bam_files/all.undegraded_hek293_pass1.fastq.gz.sorted.bam")
```
Subset bam file- undegraded
```{r warning=FALSE, message=FALSE}
bam_file <- scanBam("/home/bhavika/Desktop/Nanograd/Data/bam_files/subset_un.bam")
```
Rep 2
```{r warning=FALSE, message=FALSE}
bam_file <- scanBam("/home/bhavika/Desktop/Nanograd/Data/bam_files/all.undegraded_hek293_pass2.fastq.gz.sorted.bam")
```
3. Mildly degraded sample
```{r warning=FALSE, message=FALSE}
bam_file <- scanBam("/home/bhavika/Desktop/Nanograd/Data/bam_files/primary_mild_degradataion_rep1.fastq.bam_sorted_primary.bam")
```
Subset bam file - mildly degraded
```{r warning=FALSE, message=FALSE}
bam_file <- scanBam("/home/bhavika/Desktop/Nanograd/Data/bam_files/subset_md.bam")
```
Rep 2
```{r warning=FALSE, message=FALSE}
bam_file <- scanBam("/home/bhavika/Desktop/Nanograd/Data/bam_files/primary_mild_degradataion_rep2.fastq.bam_sorted_primary.bam")
```
Function for collapsing the list of lists into a single list as per the Rsamtools vignette
```{r warning=FALSE, message=FALSE}
.unlist <- function (x){
## do.call(c, ...) coerces factor to integer, which is undesired
x1 <- x[[1L]]
if (is.factor(x1)){
structure(unlist(x), class = "factor", levels = levels(x1))
} else {
do.call(c, x)
}
}
```
Store names of BAM fields
```{r warning=FALSE, message=FALSE}
bam_field <- names(bam_file[[1]])
```
go through each BAM field and unlist
```{r warning=FALSE, message=FALSE}
list <- lapply(bam_field, function(y) .unlist(lapply(bam_file, "[[", y)))
```
Storing the data as data frame
```{r warning=FALSE, message=FALSE}
bam_df <- do.call("DataFrame", list)
names(bam_df) <- bam_field
```
converting bam_df into data frame without the package Rsamtools
```{r warning=FALSE, message=FALSE}
df <- as.data.frame(bam_df)
```
selecting columns required for analysis
```{r warning=FALSE, message=FALSE}
input <- df %>%
dplyr::select(1,3,5,8,12) %>%
dplyr::rename(Read = 1, Transcript = 2, StartCoord = 3, Cigar = 4, Sequence = 5)
```
## Parsing CIGAR- This matcher and doone function parses all the M, I, D and S in the full cigar string
Matcher function
```{r warning=FALSE, message=FALSE}
matcher <- function(pattern, x) {
ind = gregexpr(pattern, x)[[1]]
start = as.numeric(ind)
end = start + attr(ind, "match.length")- 2
apply(cbind(start,end), 1, function(y) substr(x, start=y[1], stop=y[2]));
}
```
Doone function- This takes the sum of all the M, I, D and S
```{r warning=FALSE, message=FALSE}
doone <- function(c, Cigar) {
pat <- paste("\\d+", c , sep="")
sum(as.numeric(matcher(pat, Cigar)), na.rm = T)
}
```
Finding Matches/Deletions/Insertions/Soft Clip
1. Match
```{r warning=FALSE, message=FALSE}
cigarsums <- function(Cigar, chars=c("M")) {
sapply (chars, doone, Cigar)
}
```
calculates matches - applying cigarsums function to each row of the input to calculate the total mapped length
```{r warning=FALSE, message=FALSE}
addcig_1 <- input %>%
rowwise %>% mutate(MapLen = cigarsums(Cigar))
```
2. Deletion
```{r warning=FALSE, message=FALSE}
cigarsums <- function(Cigar, chars=c("D")) {
sapply (chars, doone, Cigar)
}
```
calculates deletions- we apply the cigarsums function to each row of the input to calculate the total length that is deleted
```{r warning=FALSE, message=FALSE}
addcig_2 <- input %>%
rowwise %>% mutate(DelLen = cigarsums(Cigar))
```
3. Insertion
```{r warning=FALSE, message=FALSE}
cigarsums <- function(Cigar, chars=c("I")) {
sapply (chars, doone, Cigar)
}
```
calculates insertions- applying cigarsums function to each row of the input to calculate the length that is inserted
```{r warning=FALSE, message=FALSE}
addcig_3 <- input %>%
rowwise %>% mutate(InsertLen = cigarsums(Cigar))
```
4. Soft clipping (S)
```{r warning=FALSE, message=FALSE}
cigarsums <- function(Cigar, chars=c("S")) {
sapply (chars, doone, Cigar)
}
```
calculates Soft clip length- total sum on both the ends
```{r warning=FALSE, message=FALSE}
addcig_4 <- input %>%
rowwise %>% mutate(SoftClipLenSum = cigarsums(Cigar))
```
Calculating the max lens- max del len, max insert len and max soft clip len
Doone_1 function is calculating the max len in the string
```{r warning=FALSE, message=FALSE}
doone_1 <- function(c, Cigar) {
pat <- paste("\\d+", c , sep="")
max(as.numeric(matcher(pat, Cigar)), na.rm = T)
}
```
Max del len
```{r warning=FALSE, message=FALSE}
cigarsums <- function(Cigar, chars=c("D")) {
sapply (chars, doone_1, Cigar)
}
```
```{r warning=FALSE, message=FALSE}
addcig_5 <- input %>%
rowwise %>% mutate(MaxDelLen = cigarsums(Cigar))
```
Max insert len
```{r warning=FALSE, message=FALSE}
cigarsums <- function(Cigar, chars=c("I")) {
sapply (chars, doone_1, Cigar)
}
```
```{r warning=FALSE, message=FALSE}
addcig_6 <- input %>%
rowwise %>% mutate(MaxInsertLen = cigarsums(Cigar))
```
Max soft clip len
```{r warning=FALSE, message=FALSE}
cigarsums <- function(Cigar, chars=c("S")) {
sapply (chars, doone_1, Cigar)
}
```
```{r warning=FALSE, message=FALSE}
addcig_7 <- input %>%
rowwise %>% mutate(MaxSoftClipLen = cigarsums(Cigar))
```
Merging all the addcig data frames into one data frame
```{r warning=FALSE, message=FALSE}
merged_1 <- merge(x=addcig_1, y= addcig_2, all=T)
merged_2 <- merge(x=addcig_3, y=addcig_4, all=T)
merge_cigars_sum <- merge(x=merged_1, y=merged_2, all=T)
merged_3 <- merge(x=addcig_5, y=addcig_6, all=T)
merged_4 <- merge(x=addcig_7, y=addcig_8, all=T)
merge_cigar_max <- merge(x=merged_3, y=merged_4, all=T)
merge_cigar <- merge(x=merge_cigars_sum, y=merge_cigar_max, all=T)
```
calculating number of reads of each transcript
```{r warning=FALSE, message=FALSE}
parse_cigar_n <- merge_cigar %>%
group_by(Transcript) %>%
mutate(NReads=n()) %>%
ungroup()
```
Function to calculate the 3' end coordinate
```{r warning=FALSE, message=FALSE}
subset_1 <- parse_cigar_n %>% as_tibble() %>%
rowwise() %>%
mutate(L = sum(c_across(c(6:7)))) # L is taking the sum of MapLen and DelLen
end_coord <- function(StartCoord, L) {
end <- StartCoord + L - 1
return(end)
}
```
Calculating the end coordinate without soft clip (sc) row wise and making a new column in the data frame as end_without_sc
```{r warning=FALSE, message=FALSE}
subset_2 <- subset_1 %>%
rowwise() %>%
mutate(end_without_sc = end_coord(StartCoord, L))
```
Getting read length from seq column by taking the sum of map len, del len and insert len. include soft clip or not has to be decided???
```{r warning=FALSE, message=FALSE}
subset_final <- subset_2 %>%
mutate(ReadLen=sum(c_across(c(6:8))))
```
subset_final dataset has the following columns-
1. Read,
2. Transcript,
3. StartCoord(5' end),
4. Cigar,
5. Sequence,
6. MapLen,
7. DelLen,
8. InsertLen,
9. Sum of soft clips at both the ends,
10. NReads(number of reads of each transcript),
11. L (sum of map and del len),
12. end without sc,
13. Read len (calculated by taking the sum of map len, del len and insert len)
14. MaxDelLen
15. MaxInsertLen
16. MaxSoftClipLen
If separate 3' and 5' end soft clips required, its in 'separating_soft_clips_start_and_end' Rmd.