-
Notifications
You must be signed in to change notification settings - Fork 0
/
junction_assignment_and_psi_calculation.R
348 lines (233 loc) · 9.99 KB
/
junction_assignment_and_psi_calculation.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
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
library(data.table)
new_genes <- fread("data/genes_new.csv", data.table = F)
library(tidyr)
library(dplyr)
# Get searchable coordinates
gene_coord <- new_genes %>%
dplyr::select(starts_with("chr"), strand) %>%
dplyr::select(-chr_size) %>%
dplyr::rename("seqnames" = "chr_name", "start" = "chr_start", "end" = "chr_end") %>%
GenomicRanges::GRanges()
# The coordinates are in the hg19 version on the genome. Normally we use the version hg38
# We will proceed to 1) convert the coordinates to hg38
library(rtracklayer)
path <- "data/hg19ToHg38.over.chain"
ch <- import.chain(path)
ch
new_exons <- liftOver(gene_coord, ch) %>%
unlist()
# Now we need to get the upstream and downstream junctions.
# For this we need to overlap the exon with the introns
library(TxDb.Hsapiens.UCSC.hg38.knownGene)
txdb <- TxDb.Hsapiens.UCSC.hg38.knownGene
hg38_exons <- exons(txdb, columns = c("TXID", "TXNAME", "EXONID"))
hg38_introns <- intronsByTranscript(txdb, use.names = T)
ovp <- findOverlapPairs(new_exons, hg38_exons, type = "any")
# The start of the exons is off by 1 nt
proper_ovp <- ovp[(width(first(ovp)) - width(second(ovp))) == 1]
library(IRanges)
# Getting upstream and downstream exons
paired_introns <- lapply(seq_along(proper_ovp), function(exon_idx) {
introns_un <- unlist(hg38_introns)
upstream_int <- precede(second(proper_ovp[exon_idx]), introns_un, select = "all")
downstream_int <- follow(second(proper_ovp[exon_idx]), introns_un, select = "all")
tx_names_up <- names(introns_un[subjectHits(upstream_int)])
tx_names_down <- names(introns_un[subjectHits(downstream_int)])
match_tx <- intersect(tx_names_up, tx_names_down)
tx_names_exon <- second(proper_ovp)[exon_idx]$TXNAME
tx_matched <- intersect(unlist(tx_names_exon), match_tx)
matched_up_introns <- introns_un[subjectHits(upstream_int)[tx_names_up %in% tx_matched]]
matched_down_introns <- introns_un[subjectHits(downstream_int)[tx_names_down %in% tx_matched]]
return(list(
"upstream" = unique(matched_up_introns),
"downstream" = unique(matched_down_introns)
))
})
# Generating the junctions table
# If "+" -> upstream_ss = start(upstream) and downstream_ss = end(downstream)
# If "-" -> upstream_ss = end(downstream) and downstream_ss = start(upstream)
library(snapcount)
# Calculation of PSI based on the SNAPTRON manual and using the recently updated data from TCGA (V2) or GTEx (v2)
psi_query_calc <- function(ljx, rjx, skipjx, ex_str, dtb) {
print(list(ljx, rjx, skipjx, ex_str))
lq <- QueryBuilder(compilation = dtb, regions = ljx)
lq <- set_row_filters(lq, strand == {{ ex_str }})
lq <- set_coordinate_modifier(lq, Coordinates$Exact)
# right inclusion query
rq <- QueryBuilder(compilation = dtb, regions = rjx)
rq <- set_row_filters(rq, strand == {{ ex_str }})
rq <- set_coordinate_modifier(rq, Coordinates$Exact)
# exclusion query
ex <- QueryBuilder(compilation = dtb, regions = skipjx)
ex <- set_row_filters(ex, strand == {{ ex_str }})
ex <- set_coordinate_modifier(ex, Coordinates$Exact)
psi <- tryCatch(
{
percent_spliced_in(list(lq), list(rq), list(ex), min_count = 10)
},
error = function(e) {}
)
return(psi)
}
library(GenomicRanges)
# Function to deal with undefined junctions (more than 1 possibility downstream/upstream)
calculate_psi <- function(jxlist, dtbs) {
exon_str <- as.character(unique(strand(jxlist$upstream)))
upstream_jx <- jxlist[[ifelse(exon_str == "+", 2, 1)]]
downstream_jx <- jxlist[[ifelse(exon_str == "+", 1, 2)]]
upstream_jx <- gsub(":\\+|:\\-", "", as.character(upstream_jx))
downstream_jx <- gsub(":\\+|:\\-", "", as.character(downstream_jx))
if (length(upstream_jx) > 1) {
psi <- lapply(upstream_jx, function(x) {
sk_jx <- paste0(
seqnames(GRanges(downstream_jx)), ":",
start(GRanges(x)), "-",
end(GRanges(downstream_jx))
)
psi_query_calc(x, downstream_jx, sk_jx, exon_str, dtbs)
})
psi <- rbindlist(psi, fill = T, idcol = "TXNAME") # Collapse results of possible isoforms
}
else if (length(downstream_jx) > 1) {
psi <- lapply(downstream_jx, function(x) {
sk_jx <- paste0(
seqnames(GRanges(upstream_jx)), ":",
start(GRanges(upstream_jx)), "-",
end(GRanges(x))
)
psi_query_calc(upstream_jx, x, sk_jx, exon_str, dtbs)
})
psi <- rbindlist(psi, fill = T, idcol = "TXNAME")
}
else { # If there is only one option in both sides, this line is run
sk_jx <- paste0(
seqnames(GRanges(upstream_jx)), ":",
start(GRanges(upstream_jx)), "-",
end(GRanges(downstream_jx))
)
psi <- psi_query_calc(upstream_jx, downstream_jx, sk_jx, exon_str, dtbs)
}
return(psi)
}
# TCGA DATA ------------------------------------------------------------------
# Apply the function to all the exons
psi_values <- lapply(paired_introns, function(intronpair) {
calculate_psi(intronpair, "tcgav2")
})
# Adding the names as GeneSymbol_coordinate
names(psi_values) <- paste0(new_genes$gene_symbol, "_", as.character(gene_coord))
# Generating a single table
psi_values <- rbindlist(psi_values, use.names = T, idcol = "SYMBOL_COORDINATE", fill = T)
# Filtering
psi_values_w <- psi_values %>%
filter(psi > 0) %>%
mutate(SYMBOL_COORDINATE_TX = paste0(ifelse(is.na(TXNAME), "", paste0(TXNAME, "-")), SYMBOL_COORDINATE))
# Intron coordinates ---------------------------------------------------------
names(paired_introns) <- paste0(new_genes$gene_symbol, "_", as.character(gene_coord))
introns_table<-lapply(paired_introns, function(x){
event_df <- lapply(x, as.data.frame)
event_df <- rbindlist(event_df, use.names = T, idcol = "tx")
event_df$tx_name <- unlist(sapply(x, names))
return(event_df)
}) %>%
rbindlist(., idcol = "exon_id")
tx_one<-psi_values_w %>%
filter(is.na(psi_values_w$TXNAME)) %>%
pull(SYMBOL_COORDINATE) %>%
unique()
hg38_convert<- data.frame("exon_hg38_coordinate" = as.character(second(proper_ovp)),
"exon_id" = names(paired_introns))
introns_table %>%
filter(tx_name %in% unique(psi_values_w$TXNAME) | exon_id %in% tx_one) %>%
left_join(., hg38_convert, by = "exon_id") %>%
fwrite("output/intron_information.tab", sep = "\t", quote = F, row.names = F)
####
# Adding TCGA IDs in the shape of: TCGA-XX-XX-XX
metadata <- fread("http://snaptron.cs.jhu.edu/data/tcgav2/samples.tsv", header = T)
ids <- metadata[, c("rail_id", "cgc_sample_id")]
ids <- ids %>%
mutate(sample_id_red = gsub("[A-Z]$", "", cgc_sample_id)) %>%
filter(sample_id_red != "")
# Merge and transform to matrix format
psi_values_w <- psi_values_w %>%
inner_join(., ids, by = c("sample_id" = "rail_id")) %>%
group_by(sample_id_red, SYMBOL_COORDINATE_TX) %>%
arrange(desc(psi)) %>% # Some ids have more than one sample, so we select the one with a biggest psi
slice_head() %>%
ungroup()
# Generate a new file for the individual read counts
junctions_reads<-psi_values_w %>%
dplyr::select(SYMBOL_COORDINATE_TX,
sample_id_red,
inclusion_group1_coverage,
inclusion_group2_coverage,
exclusion_group_coverage) %>%
pivot_longer(cols = c(inclusion_group1_coverage,
inclusion_group2_coverage,
exclusion_group_coverage),
values_to = "junction_reads") %>%
pivot_wider(names_from = sample_id_red,
values_from = junction_reads,
values_fill = 0)
# PSI counts
psi_values_w <- psi_values_w %>%
dplyr::select(SYMBOL_COORDINATE_TX, sample_id_red, psi) %>%
pivot_wider(
names_from = sample_id_red,
values_from = psi,
values_fill = 0
)
# GTExs DATA ------------------------------------------------------------------
# Apply the function to all the exons
psi_values <- lapply(paired_introns, function(intronpair) {
calculate_psi(intronpair, "gtexv2")
})
# Adding the names as GeneSymbol_coordinate
names(psi_values) <- paste0(new_genes$gene_symbol, "_", as.character(gene_coord))
# Generating a single table
psi_values <- rbindlist(psi_values, use.names = T, idcol = "SYMBOL_COORDINATE", fill = T)
# Filtering
psi_values_w_gtex <- psi_values %>%
filter(psi > 0) %>%
mutate(SYMBOL_COORDINATE_TX = paste0(ifelse(is.na(TXNAME), "", paste0(TXNAME, "-")), SYMBOL_COORDINATE))
# Adding GTEx IDs in the shape of: TCGA-XX-XX-XX
metadata <- fread("http://snaptron.cs.jhu.edu/data/gtexv2/samples.tsv", header = T)
ids <- metadata[, c("rail_id", "SAMPID")]
# Merge and transform to matrix format
psi_values_w_gtex <- psi_values_w_gtex %>%
inner_join(., ids, by = c("sample_id" = "rail_id")) %>%
group_by(SAMPID, SYMBOL_COORDINATE_TX) %>%
arrange(desc(psi)) %>% # Some ids have more than one sample, so we select the one with a biggest psi
slice_head() %>%
ungroup()
junctions_reads_gtex<-psi_values_w_gtex %>%
dplyr::select(SYMBOL_COORDINATE_TX,
SAMPID,
inclusion_group1_coverage,
inclusion_group2_coverage,
exclusion_group_coverage) %>%
pivot_longer(cols = c(inclusion_group1_coverage,
inclusion_group2_coverage,
exclusion_group_coverage),
values_to = "junction_reads") %>%
pivot_wider(names_from = SAMPID,
values_from = junction_reads,
values_fill = 0)
psi_values_w_gtex <- psi_values_w_gtex %>%
dplyr::select(SYMBOL_COORDINATE_TX, SAMPID, psi) %>%
pivot_wider(
names_from = SAMPID,
values_from = psi,
values_fill = 0
)
# Merge TCGA and GTEx --------------------------------------------
psi_mtx <- left_join(psi_values_w, psi_values_w_gtex, by = "SYMBOL_COORDINATE_TX")
# Saving the results
#fwrite(psi_mtx, "output/20210104_PSI_table_exons.csv.gz")
# Junction reads
jxreads_mtx <- left_join(junctions_reads, junctions_reads_gtex, by = c("SYMBOL_COORDINATE_TX", "name"))
jxlist<-split.data.frame(jxreads_mtx[, -1],
jxreads_mtx$SYMBOL_COORDINATE_TX)
lapply(names(jxlist), function(jx_file_name){
fwrite(jxlist[[jx_file_name]], paste0("output/jx_reads/", jx_file_name, ".csv"))
})