-
Notifications
You must be signed in to change notification settings - Fork 0
/
biotype-comparison.qmd
272 lines (228 loc) · 6.58 KB
/
biotype-comparison.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
---
title: "smallRNA Biotype Comparison"
author: "Lance Parsons"
date: last-modified
format:
html:
toc: true
code-fold: true
df-print: paged
embed-resources: true
fig-format: svg
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("stringr")
library("ggplot2")
library("downloadthis")
```
# Read data
{{< include _sample-metadata.qmd >}}
# Biotype comparison
* count only fragments that were properly aligned
* annotate with GENCODE gene model
* primary alignments were counted, even if the fragments aligned multiple times
* fragments aligning to multiple features were assigned to the feature that mostly closely overlapped with the fragment
```{r}
#| label: import-biotype-counts
human_counts_dir <- "results/smrna_count/"
biotype_counts_files <- paste0(
human_counts_dir,
sample_units$sample_unit,
"_first_proper_pair_biotype_count.txt"
)
biotype_counts <- readr::read_tsv(
biotype_counts_files[1],
comment = "#",
col_names = c("biotype", biotype_counts_files[1]),
col_types = "ci"
)
for (i in 2:length(biotype_counts_files)) {
biotype_sample <-
readr::read_tsv(
biotype_counts_files[i],
comment = "#",
col_names = c("biotype", biotype_counts_files[i]),
col_types = "ci"
)
biotype_counts <- biotype_counts %>%
dplyr::full_join(biotype_sample, by = "biotype")
}
biotype_counts <- biotype_counts %>%
rename_all(~ stringr::str_replace_all(
., human_counts_dir, ""
)) %>%
rename_all(~ str_replace_all(
.,
"_first_proper_pair_biotype_count.txt",
""
)) %>%
tidyr::pivot_longer(!biotype, names_to = "sample", values_to = "count") %>%
# Add fraction column
mutate(fraction_in_sample = count / sum(count, na.rm = TRUE)) %>% arrange(sample)
biotype_counts
biotype_counts %>% as.data.frame() %>%
download_this(
output_name = "biotype_counts",
output_extension = ".csv",
button_label = "Download data as csv",
button_type = "default",
csv2 = FALSE,
self_contained = TRUE,
has_icon = TRUE,
icon = "fa fa-save"
)
```
```{r fig.height=7, fig.width=16}
#| label: biotype-barplot
#| fig-height: 7
#| fig-width: 16
#| fig-format: pdf
p <- ggplot(
data = subset(biotype_counts, !is.na(count)),
aes(x = sample, y = count, fill = biotype)
) +
geom_bar(stat = "identity", position = "fill") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
p
```
## Biotypes from featureCounts
```{r}
featurecounts_files <- paste0(
"results/smrna_featurecounts/",
sample_units$sample_unit,
"_first_proper_pair.featureCounts"
)
featurecounts <- readr::read_tsv(
featurecounts_files[1],
comment = "#",
col_types = "cccccici")
for (i in 2:length(featurecounts_files)) {
featurecounts_sample <-
readr::read_tsv(
featurecounts_files[i],
comment = "#",
col_types = "c------i")
featurecounts <- featurecounts %>%
dplyr::full_join(featurecounts_sample, by = "Geneid")
}
featurecounts <- featurecounts %>%
rename_all(~ stringr::str_replace_all(
., "results/alignments/Homo_sapiens.GRCh38.dna.primary_assembly/filtered/", ""
)) %>%
rename_all(~ str_replace_all(
.,
"_first_proper_pair.bam",
""
)) %>%
# Pivot
select(-Chr, -Start, -End, -Strand, -Length) %>%
tidyr::pivot_longer(!Geneid & !gene_type, names_to = "sample", values_to = "count") %>%
# Summarize (count by type)
group_by(sample, gene_type) %>% summarize(count = sum(count), .groups = "keep") %>%
# Add fraction column
group_by(sample) %>%
mutate(fraction_in_sample = count / sum(count, na.rm = TRUE)) %>% arrange(sample)
featurecounts
featurecounts %>% as.data.frame() %>%
download_this(
output_name = "featurecounts",
output_extension = ".csv",
button_label = "Download data as csv",
button_type = "default",
csv2 = FALSE,
self_contained = TRUE,
has_icon = TRUE,
icon = "fa fa-save"
)
```
```{r fig.height=7, fig.width=16}
#| label: biotype-featurecounts-barplot
#| fig-height: 7
#| fig-width: 16
#| fig-format: pdf
p <- ggplot(
data = subset(featurecounts, !is.na(count)),
aes(x = sample, y = count, fill = gene_type)
) +
geom_bar(stat = "identity", position = "fill") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
p
```
## Biotypes from featureCounts (allowMultiOverlap)
```{r}
featurecounts_multioverlap_files <- paste0(
"results/smrna_featurecounts_multioverlap/",
sample_units$sample_unit,
"_first_proper_pair.featureCounts"
)
featurecounts_multioverlap <- readr::read_tsv(
featurecounts_multioverlap_files[1],
comment = "#",
col_types = "cccccici")
for (i in 2:length(featurecounts_multioverlap_files)) {
featurecounts_sample <-
readr::read_tsv(
featurecounts_multioverlap_files[i],
comment = "#",
col_types = "c------i")
featurecounts_multioverlap <- featurecounts_multioverlap %>%
dplyr::full_join(featurecounts_sample, by = "Geneid")
}
featurecounts_multioverlap <- featurecounts_multioverlap %>%
rename_all(~ stringr::str_replace_all(
., "results/alignments/Homo_sapiens.GRCh38.dna.primary_assembly/filtered/", ""
)) %>%
rename_all(~ str_replace_all(
.,
"_first_proper_pair.bam",
""
)) %>%
# Pivot
select(-Chr, -Start, -End, -Strand, -Length) %>%
tidyr::pivot_longer(!Geneid & !gene_type, names_to = "sample", values_to = "count") %>%
# Summarize (count by type)
group_by(sample, gene_type) %>% summarize(count = sum(count), .groups = "keep") %>%
# Add fraction column
group_by(sample) %>%
mutate(fraction_in_sample = count / sum(count, na.rm = TRUE)) %>% arrange(sample)
featurecounts_multioverlap
featurecounts_multioverlap %>% as.data.frame() %>%
download_this(
output_name = "featurecounts_multioverlap",
output_extension = ".csv",
button_label = "Download data as csv",
button_type = "default",
csv2 = FALSE,
self_contained = TRUE,
has_icon = TRUE,
icon = "fa fa-save"
)
```
```{r fig.height=7, fig.width=16}
#| label: biotype-featurecounts-multioverlap-barplot
#| fig-height: 7
#| fig-width: 16
#| fig-format: pdf
p <- ggplot(
data = subset(featurecounts_multioverlap, !is.na(count)),
aes(x = sample, y = count, fill = gene_type)
) +
geom_bar(stat = "identity", position = "fill") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
p
```