-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_exogenous-alignments.qmd
153 lines (140 loc) · 4.23 KB
/
_exogenous-alignments.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
## BAM reading functions
```{r}
#| label: bam-functions
rna_species_plot_range <- function(sequence_name) {
plot_range <- rna_mixes %>%
dplyr::filter(.data$rna_species == sequence_name) %>%
dplyr::select(start, end) %>%
unique()
return(plot_range)
}
# GRanges from BAM
granges_from_bam <- function(sample_unit,
sequence_name,
is_proper_pair = TRUE,
bam_dir =
"results/alignments/exogenous_rna/sorted") {
plot_range <- rna_species_plot_range(sequence_name)
which <- GRanges(
sprintf("%s:%i-%i", sequence_name, plot_range$start, plot_range$end)
)
param <- ScanBamParam(
flag = scanBamFlag(isProperPair = is_proper_pair),
mapqFilter = 1,
which = which
)
aligned_fragments_list <- list()
if (is_proper_pair) {
aligned_fragments <- granges(
readGAlignmentPairs(
sprintf("%s/%s.bam", bam_dir, sample_unit),
param = param
),
on.discordant.seqnames = "drop"
)
rna_info <- rna_mixes %>%
dplyr::filter(rna_species == {{ sequence_name }}) %>%
dplyr::select(-"exogenous_rna") %>%
unique()
# Active in cis
aligned_fragments_list$active_cis <- aligned_fragments %>%
plyranges::filter(
start <= rna_info$active_cis_start_max,
end >= rna_info$active_cis_end_min
)
# Active in trans
aligned_fragments_list$active_trans <- aligned_fragments %>%
plyranges::filter(
start > rna_info$active_cis_start_max,
start <= rna_info$active_trans_start_max,
end >= rna_info$active_trans_end_min
)
# Inactive Cryptic Termination
aligned_fragments_list$inactive_cryptic_termination <-
aligned_fragments %>%
plyranges::filter(end < rna_info$inactive_ct_end_max)
# Inactive Other
aligned_fragments_list$inactive_other <- plyranges::bind_ranges(
aligned_fragments %>%
plyranges::filter(
start <= rna_info$active_cis_start_max,
end < rna_info$active_cis_end_min,
end > rna_info$inactive_ct_end_max
),
aligned_fragments %>%
plyranges::filter(
start > rna_info$active_cis_start_max,
(
start > rna_info$active_trans_start_max |
end < rna_info$active_trans_end_min
)
),
)
} else {
aligned_fragments <- granges(
readGAlignments(
sprintf("%s/%s.bam", bam_dir, sample_unit),
param = param
)
)
aligned_fragments_list$discordant <- aligned_fragments
}
return(aligned_fragments_list)
}
```
## Read BAM coverage
```{r}
#| label: load-bam-coverage
#| cache: true
rna_species_plot_data <- list()
for (rna_species_to_plot in rna_mixes$rna_species) {
rna_info <- rna_mixes %>%
dplyr::filter(rna_species == rna_species_to_plot)
sample_units_to_plot <- sample_units %>%
dplyr::filter(exogenous_rna %in% rna_info$exogenous_rna)
granges_to_plot <- list()
for (sample_unit in sample_units_to_plot$sample_unit) {
granges_to_plot[[sample_unit]] <- granges_from_bam(
sample_unit,
rna_species_to_plot,
TRUE
)
}
rna_species_plot_data[[rna_species_to_plot]] <- granges_to_plot
}
```
## Organize coverage data
```{r}
#| label: organize-coverage-data
exogenous_rna_count_data <- tibble(
sample_unit = character(),
sequence_name = character(),
category = character(),
count = integer()
)
for (rna_species in names(rna_species_plot_data)) {
for (sample_unit in names(rna_species_plot_data[[rna_species]])) {
for (category in
names(rna_species_plot_data[[rna_species]][[sample_unit]])) {
exogenous_rna_count_data <- exogenous_rna_count_data %>%
add_row(
sample_unit = sample_unit,
sequence_name = rna_species,
category = category,
count = length(
rna_species_plot_data[[rna_species]][[sample_unit]][[category]]
)
)
}
}
}
exogenous_rna_count_data
```
## Summarize coverage data
```{r}
#| label: summarize-coverage-data
exogenous_rna_mapped_totals <- exogenous_rna_count_data %>%
group_by(sample_unit, sequence_name) %>%
summarize(mapped_fragments = sum(count), .groups = "keep")
exogenous_rna_mapped_totals
```