-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexogenous-rna-profiles.qmd
389 lines (326 loc) · 10 KB
/
exogenous-rna-profiles.qmd
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
---
title: "Adamson smallRNA - pegRNA"
author: "Lance Parsons"
format:
html:
toc: true
code-fold: true
df-print: paged
embed-resources: true
editor: source
---
# Load libraries
This project uses [`renv`](https://rstudio.github.io/renv/articles/renv.html)
to keep track of installed packages. Install `renv` if not installed and load
dependencies with `renv::restore()`.
```r
install.packages("renv")
renv::restore()
```
```{r}
#| label: load-packages
#| include: false
#| message: false
library(readr)
library(dplyr)
library(tidyr)
library(reshape2)
library(GenomicRanges)
library(pheatmap)
library(tibble)
library(ggplot2)
library(stringr)
library(cowplot)
library(markdown)
library(RColorBrewer)
library(GenomicAlignments)
library(reshape2)
```
# Read data
1. Get list of samples
```{r}
samples <- read_tsv("config/samples.tsv", show_col_types = FALSE)
units <- read_tsv("config/units.tsv", show_col_types = FALSE)
sample_units <- dplyr::left_join(samples, units, by = "sample_name") %>%
unite(sample_unit, sample_name, unit_name, remove = FALSE)
sample_units
```
2. Read Samtools `idxstats` to get human coverage for normalization
Notes:
* The counts include the total number of reads aligned, they
are not limited to uniquely aligned reads.
* The counts are reads, not pairs or fragments
```{r}
idxstats_exogenousrna_dir <-
"results/samtools_idxstats/exogenous_rna/"
idxstats_human_dir <-
"results/samtools_idxstats/Homo_sapiens.GRCh38.dna.primary_assembly/"
bowtie2_human_logs <-
"results/logs/bowtie2/Homo_sapiens.GRCh38.dna.primary_assembly/"
idxstats <- tibble()
for (row in seq_len(nrow(sample_units))) {
sample <- sample_units[row, ]$sample_unit
# Read `idsxstats` for exogenous mapped reads
exogenous_rna_stats <- read_tsv(
file.path(idxstats_exogenousrna_dir, sprintf("%s.bam.idxstats", sample)),
col_names = c(
"sequence_name", "sequence_length",
"mapped_reads", "unmapped_reads"
),
col_types = "ciii"
)
exogenous_rna_mapped_reads <- exogenous_rna_stats %>%
filter(!sequence_name %in% c("*")) %>%
select(sequence_name, mapped_reads) %>%
mutate(sample = sample)
# Read `idxstats` for human mapped reads
human_stats <- read_tsv(
file.path(idxstats_human_dir, sprintf("%s.bam.idxstats", sample)),
col_names = c(
"sequence_name", "sequence_length",
"mapped_reads", "unmapped_reads"
),
col_types = "ciii"
)
grch38_mapped_reads <- human_stats %>%
filter(!sequence_name %in% c("*")) %>%
select(mapped_reads) %>%
sum()
grch38_mapped_reads <- tibble(
sequence_name = "grch38_mapped_reads",
mapped_reads = grch38_mapped_reads,
sample = sample
)
# Read bowtie2 logs for unmapped reads
bowtie2_log <- readLines(
file.path(bowtie2_human_logs, sprintf("%s.log", sample))
)
total_pairs <- strtoi(str_split(bowtie2_log[1], " ")[[1]][1])
total_reads <- total_pairs * 2
unmapped_reads <- tibble(
sequence_name = "unmapped",
mapped_reads = total_reads - grch38_mapped_reads$mapped_reads,
sample = sample
)
# Consolidate counts for rows
idxstats <- rbind(
idxstats,
exogenous_rna_mapped_reads,
grch38_mapped_reads,
unmapped_reads
)
}
idxstats
```
# Coverage
## Concordant vs Discordant paired reads
Concordant pairs are pairs of reads that:
* Align on the same pegRNA
* Align within 500 bp of each other
* Align in the expected forward-reverse orientation (`--> .. <--`)
Discordant reads aligned but whose mate:
* Did not align (on the pegRNA)
* Aligned more than 500 bp away
* Aligned in an unexpected orientation
```{r fig.width=10, fig.height=10}
## Config and function definition
bam_dir <- "results/alignments/exogenous_rna/sorted"
last_day <- 0
cols <- brewer.pal(n = 5, name = "RdBu")
concordant_cell_line_colors <- list(
"Parental" = "#CA0020",
"P1E10" = "#0571B0"
)
discordant_cell_line_colors <- list(
"Parental" = "#F4A582",
"P1E10" = "#92C5DE"
)
# Exogenous RNA mixtures
rna_mixes <- tibble()
for (mix in c("mastermix1", "mastermix2", "PJY103", "PJY300")) {
t <- readDNAStringSet(sprintf("data/references/%s.fa", mix))
rna_mixes <- rbind(rna_mixes, tibble(
exogenous_rna = mix,
rna_species = word(t@ranges@NAMES, 1),
start = 1,
end = t@ranges@width
))
}
rna_mixes <- rna_mixes %>%
rows_update(tibble(exogenous_rna = "PJY103", start = 331, end = 460),
by = "exogenous_rna"
) %>%
rows_update(tibble(exogenous_rna = "PJY300", start = 331, end = 497),
by = "exogenous_rna"
)
rna_mixes
source("pegrna_plots.R")
```
## PJY103 / PJY300 (Batch 2)
```{r fig.width=20, fig.height=10}
#| warning: false
sample_list <- rna_mixes %>% filter(grepl("PJY103|PJY300", rna_species))
for (start_offset in c(NA, 5)) {
for (norm_factor in c("exogenous_rna_mapped_reads", "grch38_mapped_reads")) {
for (i in seq_len(nrow(sample_list))) {
rna_species <- sample_list[[i, "rna_species"]]
mix <- sample_list[[i, "exogenous_rna"]]
pegrna_plots(
sequence_name = rna_species,
normalization_factor = norm_factor,
mix = mix,
ylab = sprintf("%s coverage (normalized to %s)", mix, norm_factor),
vlines = seq(sample_list[i, ]$start, sample_list[i, ]$end, 2),
start_offset = start_offset
)
}
}
}
```
## VEGFA
```{r fig.width=20, fig.height=10}
sample_list <- rna_mixes %>% filter(grepl("VEGFA", rna_species))
for (start_offset in c(NA, 5)) {
for (norm_factor in c("exogenous_rna_mapped_reads", "grch38_mapped_reads")) {
for (i in seq_len(nrow(sample_list))) {
rna_species <- sample_list[[i, "rna_species"]]
mix <- sample_list[[i, "exogenous_rna"]]
pegrna_plots(
sequence_name = rna_species,
normalization_factor = norm_factor,
mix = mix,
ylab = sprintf("%s coverage (normalized to %s)", mix, norm_factor),
vlines = seq(sample_list[i, ]$start, sample_list[i, ]$end, 2),
start_offset = start_offset
)
}
}
}
```
## FANCF
```{r fig.width=20, fig.height=10}
sample_list <- rna_mixes %>% filter(grepl("FANCF", rna_species))
for (start_offset in c(NA, 5)) {
for (norm_factor in c("exogenous_rna_mapped_reads", "grch38_mapped_reads")) {
for (i in seq_len(nrow(sample_list))) {
rna_species <- sample_list[i, ]$rna_species
pegrna_plots(
sequence_name = rna_species,
normalization_factor = norm_factor,
ylab = sprintf("Coverage (normalized to %s)", norm_factor),
vlines = seq(sample_list[i, ]$start, sample_list[i, ]$end, 2),
start_offset = start_offset
)
}
}
}
```
## HEK3
```{r fig.width=20, fig.height=10}
sample_list <- rna_mixes %>% filter(grepl("HEK3", rna_species))
for (start_offset in c(NA, 5)) {
for (norm_factor in c("exogenous_rna_mapped_reads", "grch38_mapped_reads")) {
for (i in seq_len(nrow(sample_list))) {
rna_species <- sample_list[i, ]$rna_species
pegrna_plots(
sequence_name = rna_species,
normalization_factor = norm_factor,
ylab = sprintf("Coverage (normalized to %s)", norm_factor),
vlines = seq(sample_list[i, ]$start, sample_list[i, ]$end, 2),
start_offset = start_offset
)
}
}
}
```
## DNMT1
```{r fig.width=20, fig.height=10}
sample_list <- rna_mixes %>% filter(grepl("DNMT1", rna_species))
for (start_offset in c(NA, 5)) {
for (norm_factor in c("exogenous_rna_mapped_reads", "grch38_mapped_reads")) {
for (i in seq_len(nrow(sample_list))) {
rna_species <- sample_list[i, ]$rna_species
pegrna_plots(
sequence_name = rna_species,
normalization_factor = norm_factor,
ylab = sprintf("Coverage (normalized to %s)", norm_factor),
vlines = seq(sample_list[i, ]$start, sample_list[i, ]$end, 2),
start_offset = start_offset
)
}
}
}
```
## RUNX1
```{r fig.width=20, fig.height=10}
sample_list <- rna_mixes %>% filter(grepl("RUNX1", rna_species))
for (start_offset in c(NA, 5)) {
for (norm_factor in c("exogenous_rna_mapped_reads", "grch38_mapped_reads")) {
for (i in seq_len(nrow(sample_list))) {
rna_species <- sample_list[i, ]$rna_species
pegrna_plots(
sequence_name = rna_species,
normalization_factor = norm_factor,
ylab = sprintf("Coverage (normalized to %s)", norm_factor),
vlines = seq(sample_list[i, ]$start, sample_list[i, ]$end, 2),
start_offset = start_offset
)
}
}
}
```
## EMX1
```{r fig.width=20, fig.height=10}
sample_list <- rna_mixes %>% filter(grepl("EMX1", rna_species))
for (start_offset in c(NA, 5)) {
for (norm_factor in c("exogenous_rna_mapped_reads", "grch38_mapped_reads")) {
for (i in seq_len(nrow(sample_list))) {
rna_species <- sample_list[i, ]$rna_species
pegrna_plots(
sequence_name = rna_species,
normalization_factor = norm_factor,
ylab = sprintf("Coverage (normalized to %s)", norm_factor),
vlines = seq(sample_list[i, ]$start, sample_list[i, ]$end, 2),
start_offset = start_offset
)
}
}
}
```
## RNF2
```{r fig.width=20, fig.height=10}
sample_list <- rna_mixes %>% filter(grepl("RNF2", rna_species))
for (start_offset in c(NA, 5)) {
for (norm_factor in c("exogenous_rna_mapped_reads", "grch38_mapped_reads")) {
for (i in seq_len(nrow(sample_list))) {
rna_species <- sample_list[i, ]$rna_species
pegrna_plots(
sequence_name = rna_species,
normalization_factor = norm_factor,
ylab = sprintf("Coverage (normalized to %s)", norm_factor),
vlines = seq(sample_list[i, ]$start, sample_list[i, ]$end, 2),
start_offset = start_offset
)
}
}
}
```
# Strandedness
```{r}
#| warning: false
source("pegrna_alignment_strandedness.R")
for (rna_species in rna_mixes %>%
select(rna_species) %>%
unique() %>%
pull()) {
stranded_counts <- pegrna_alignment_strandedness(sequence_name = rna_species)
p <- ggplot(
data = stranded_counts,
aes(x = sample_unit, y = count, fill = strand)
) +
geom_bar(stat = "identity") +
labs(title = rna_species) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
print(p)
}
```