-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfusion_detection_functions.R
More file actions
164 lines (159 loc) · 7.61 KB
/
fusion_detection_functions.R
File metadata and controls
164 lines (159 loc) · 7.61 KB
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
prepareAnnotationsFromGTF <- function(file) {
if (missing(file)) {
stop("A GTF file is required.")
} else {
data <- utils::read.delim(file, header = FALSE, comment.char = "#")
colnames(data) <- c("seqname", "source", "type", "start", "end",
"score", "strand", "frame", "attribute")
data <- data[data$type == "exon", ]
data$strand[data$strand == "."] <- "*"
data$GENEID <- gsub("gene_id (.*?);.*", "\\1", data$attribute)
data$TXNAME <- gsub(".*transcript_id (.*?);.*", "\\1", data$attribute)
data$hgnc_symbol <- gsub(".*gene_name (.*?);.*","\\1",data$attribute)
multiTxCheck <- as_tibble(data) %>% select(seqname, GENEID) %>% distinct() %>% group_by(GENEID) %>%
mutate(n=n(), id=paste0('-',row_number()))
if(any(multiTxCheck$n>1)) { # identical TXNAMES
warning('Annotations contain duplicated transcript names
Transcript names will be made unique')
uniqueNamesTbl <- as_tibble(data) %>%
select(seqname, TXNAME, GENEID) %>%
mutate(order=row_number()) %>% left_join(multiTxCheck) %>%
mutate(gene_unique = paste0(GENEID, ifelse(n==1,'', id)),
tx_unique = paste0(TXNAME, ifelse(n==1,'', id))) %>%
arrange(order)
data$TXNAME_Original <- data$TXNAME
data$GENEID_Original <- data$GENEID
data$TXNAME <- uniqueNamesTbl$tx_unique
data$GENEID <- uniqueNamesTbl$gene_unique
}
geneData <- unique(data[, c("TXNAME", "GENEID","hgnc_symbol")])
grlist <- makeGRangesListFromDataFrame(
data[, c("seqname", "start", "end", "strand", "TXNAME")],
split.field = "TXNAME", keep.extra.columns = TRUE)
grlist <- grlist[IRanges::order(start(grlist))]
unlistedExons <- unlist(grlist, use.names = FALSE)
partitioning <- PartitioningByEnd(cumsum(elementNROWS(grlist)),
names = NULL)
txIdForReorder <- togroup(PartitioningByWidth(grlist))
exon_rank <- lapply(elementNROWS(grlist), seq, from = 1)
exon_rank[which(unlist(unique(strand(grlist))) == "-")] <- lapply(
exon_rank[which(unlist(unique(strand(grlist))) == "-")], rev
) # * assumes positive for exon ranking
names(exon_rank) <- NULL
unlistedExons$exon_rank <- unlist(exon_rank)
unlistedExons <- unlistedExons[order(txIdForReorder,
unlistedExons$exon_rank)]
# exonsByTx is always sorted by exon rank, not by strand,
# make sure that this is the case here
unlistedExons$exon_endRank <- unlist(lapply(elementNROWS(grlist),
seq, to = 1), use.names = FALSE)
unlistedExons <- unlistedExons[order(txIdForReorder,
start(unlistedExons))]
mcols(unlistedExons) <- mcols(unlistedExons)[, c("exon_rank",
"exon_endRank")]
grlist <- relist(unlistedExons, partitioning)
# sort the grlist by start position, ranked by exon number
mcols(grlist) <- DataFrame(geneData[(match(names(grlist),
geneData$TXNAME)), ])
mcols(grlist)$txid <- seq_along(grlist)
minEqClasses <- getMinimumEqClassByTx(grlist)
if(!identical(names(grlist),minEqClasses$queryTxId)) warning('eq classes might be incorrect')
mcols(grlist)$eqClassById <- minEqClasses$eqClassById
}
return(grlist)
}
getFusionAnnotations <- function(fusionGeneNames, anno.file,ensemblAnnotations.genes,ensemblAnnotations.txs,fusionTx){
annotationRanges <- anno.file#prepareAnnotations(anno.file)
fusionAnnotation <- lapply(fusionGeneNames, function(s){
print(s)
genevec <- unlist(strsplit(s, ":"))
valid <- do.call("c",lapply(genevec, function(g){
geneid <- ensemblAnnotations.genes[hgnc_symbol==g]$ensembl_gene_id
if(length(geneid)>1){ return(FALSE)}
else{return(TRUE)}
}))
if(!all(valid)){
print("multiple gene ids for hgnc symbol, skipping")
print(s)
return(NULL)
}
tmp_range <- do.call("c",lapply(genevec, function(g){
geneid <- ensemblAnnotations.genes[hgnc_symbol==g]$ensembl_gene_id
txid <- ensemblAnnotations.txs[ensembl_gene_id==geneid]$ensembl_transcript_id
print(length(txid))
tmp <- do.call("c",lapply(txid, function(t){
print(t)
tmp <- annotationRanges[t]
fusionTmp <- fusionTx[[s]][[t]]
seqlevels(tmp, pruning.mode = "tidy") <- as.character(unique(seqnames(tmp)))
seqlevels(tmp) <- as.character(unique(seqnames(fusionTmp)))
ranges(tmp[[1]]) <- ranges(fusionTmp)
# if negative strand
if(unique(strand(tmp[[1]]))=="-"){
tmp[[1]]$exon_rank <- max(tmp[[1]]$exon_rank)-tmp[[1]]$exon_rank+1
tmp[[1]]$exon_endRank <- max(tmp[[1]]$exon_endRank)-tmp[[1]]$exon_endRank+1
}
strand(tmp[[1]]) <- strand(fusionTmp)
return(tmp)
}))
return(tmp)
}))
return(tmp_range)
})
keep.id <- which(!sapply(fusionAnnotation, is.null))
fusionAnnotation <- fusionAnnotation[keep.id]
fusionAnnotation <- do.call("c", fusionAnnotation)
return(fusionAnnotation)
}
getFusionTxRange <- function(fusionGeneNames, ensemblAnnotations.genes, exonsByGene, ensemblAnnotations.txs, exonsByTx, jaffa_results_table){
fusionTx <- lapply(seq_along(fusionGeneNames), function(s){
print(s)
tmp <- fusionGeneNames[s]
genevec <- unlist(strsplit(tmp, ":"))
if(any(!(genevec %in% ensemblAnnotations.genes$hgnc_symbol))){ return(NULL) }
geneid <- ensemblAnnotations.genes[hgnc_symbol==genevec[1]]$ensembl_gene_id
tmp_range <- exonsByGene[geneid][[1]]
min_start <- min(start(tmp_range))
length_g1 <- max(end(tmp_range)) - min_start + 1
tmp_range <- lapply(seq_along(genevec), function(g){
geneid <- ensemblAnnotations.genes[hgnc_symbol==genevec[g]]$ensembl_gene_id
tmp_range <- exonsByGene[geneid][[1]]
min_start <- min(start(tmp_range))
length_g_tmp <- max(end(tmp_range))
strand.info <- as.character(unique(strand(tmp_range)))
txid <- ensemblAnnotations.txs[(ensembl_gene_id == geneid)]$ensembl_transcript_id
new_range <- lapply(txid, function(t){
tmp_range <- exonsByTx[t][[1]]
if(g==1){
scoreNum = ifelse(strand.info=="-", length_g_tmp-jaffa_results_table[fusion.genes == tmp]$base1+1,
jaffa_results_table[fusion.genes == tmp]$base1-min_start+1)
}else{
scoreNum = ifelse(strand.info=="-",length_g_tmp-jaffa_results_table[fusion.genes == tmp]$base2+1+length_g1,
jaffa_results_table[fusion.genes == tmp]$base2-min_start+1+length_g1)
}
if(strand.info=="-"){
new_range <- GRanges(seqnames=Rle(tmp,length(tmp_range)),
ranges = IRanges(start = length_g_tmp - end(tmp_range) + 1,
end = length_g_tmp - start(tmp_range) + 1 ),
strand = Rle("+",length(tmp_range)),
score = scoreNum)
}else{
new_range <- GRanges(seqnames=Rle(tmp,length(tmp_range)),
ranges = IRanges(start = start(tmp_range) - min_start+1,
end = end(tmp_range) - min_start+1),
strand = Rle(strand.info,length(tmp_range)),
score = scoreNum)
}
if(g == 2){
new_range <- GenomicRanges::shift(new_range, shift = length_g1)
}
return(new_range)
})
names(new_range) <- txid
return(new_range)
})
return(do.call("c",tmp_range))
})
names(fusionTx) <- fusionGeneNames
return(fusionTx)
}