-
Notifications
You must be signed in to change notification settings - Fork 2
/
FusionMetaCaller.R
executable file
·166 lines (148 loc) · 6.27 KB
/
FusionMetaCaller.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
#!/usr/bin/env Rscript
library(data.table)
## library(FusionMetaCaller)
### ./FusionMetaCaller.R merged.txt merged.annotated.txt
###
### This script applies FusionMetaCaller ranking to fusions,
### after ignoring STAR results and
### grouping both by breakpoint and by gene pair.
### It adds columns and writes an annotated file.
FusionMetaCaller <- function (countMatrix, vote = 2, plot = F, trueFusion = NA){
filterInd <- apply(countMatrix > 0, 1, sum) >= vote
if(sum(filterInd) == 1){
filterMatrix <- t(as.matrix(countMatrix[filterInd, ]))
rownames(filterMatrix) <- rownames(countMatrix)[filterInd]
} else {
filterMatrix <- countMatrix[filterInd, ]
}
orderMatrix <- matrix(0, nrow(filterMatrix), ncol(filterMatrix))
for (j in 1:ncol(filterMatrix)) {
ind <- filterMatrix[, j] != 0
orderMatrix[ind, j] <- order(filterMatrix[ind, j])
}
rankSum <- apply(orderMatrix, 1, sum)
orderOfRankSum <- order(-rankSum)
if(sum(filterInd) == 1){
sortMatrix <- t(as.matrix(filterMatrix[orderOfRankSum, ]))
rownames(sortMatrix) <- rownames(filterMatrix)[orderOfRankSum]
} else {
sortMatrix <- filterMatrix[orderOfRankSum, ]
}
if (plot == T) {
co <- 2
colLabel <- c("red", "blue", "green", "yellow", "purple",
"chocolate1", "hotpink", "orange", "darkblue", "darkgreen")
ltyLabel <- c(rep(c(1, 5), each = 10))
mark <- rep(0, times = nrow(countMatrix))
mark[rownames(countMatrix) %in% trueFusion] <- 1
pdf("precision_recall_plot.pdf")
par(mar = c(5.1, 4.1, 4.1, 8.1), xpd = TRUE)
plot(x = NULL, y = NULL, xlim = c(0, 1), ylim = c(0,
1), xlab = "Recall", ylab = "Precision", main = "Precison-Recall Plot",
cex.lab = 1.5, cex.axis = 1.3)
for (j in 1:ncol(countMatrix)) {
sortCount <- sort(countMatrix[, j], decreasing = T)
numFusion <- sum(sortCount > 0)
sortMark <- mark[order(countMatrix[, j], decreasing = T)]
sortMark <- sortMark[1:numFusion]
CUTOFF <- seq(from = 1, to = length(sortMark), by = co)
if (CUTOFF[length(CUTOFF)] != length(sortMark)) {
CUTOFF <- c(CUTOFF, length(sortMark))
}
precision <- rep(0, times = length(CUTOFF))
recall <- rep(0, times = length(CUTOFF))
for (i in 1:length(CUTOFF)) {
cutoff <- CUTOFF[i]
TP <- sum(sortMark[1:cutoff])
precision[i] <- TP/cutoff
recall[i] <- TP/length(trueFusion)
}
collabel <- colLabel[j%%10]
ltylabel <- ltyLabel[j%%20]
lines(recall, precision, col = collabel, lwd = 2,
lty = ltylabel)
}
sortMark <- rownames(sortMatrix) %in% trueFusion
CUTOFF <- seq(from = 1, to = length(sortMark), by = co)
if (CUTOFF[length(CUTOFF)] != length(sortMark)) {
CUTOFF <- c(CUTOFF, length(sortMark))
}
precision <- rep(0, times = length(CUTOFF))
recall <- rep(0, times = length(CUTOFF))
for (i in 1:length(CUTOFF)) {
cutoff <- CUTOFF[i]
TP <- sum(sortMark[1:cutoff])
precision[i] <- TP/cutoff
recall[i] <- TP/length(trueFusion)
}
collabel <- "black"
ltylabel <- 2
lines(recall, precision, col = collabel, lwd = 2, lty = ltylabel)
legend("topright", inset = c(-0.35, 0), legend = c(colnames(countMatrix),
"meta-caller"), lty = c(rep(ltyLabel, length = ncol(countMatrix)),
2), lwd = 3, col = c(rep(colLabel, length = ncol(countMatrix)),
"black"), bty = "n")
dev.off()
}
return(list(sortMatrix = sortMatrix))
}
annotate_merged_file <- function(merged,
TAG_name = "TAG_breakpoints",
FusionMetaCaller_rank_name = "FusionMetaCaller_rank_breakpoints"){
### uses FusionMetaCaller to rank fusion calls
### adds a column to a data.table (merged) with name FusionMetaCaller_rank_name
### requires columns with names "TOTAL_COVERAGE" and TAG_name
merged_dc <- dcast.data.table(merged[!is.na(get(TAG_name))],
paste(TAG_name, "~", "CALLER_ID"),
value.var = "TOTAL_COVERAGE",
fill = 0)
merged_m <- as.matrix(merged_dc[, -1, with = F]);
rownames(merged_m) <- merged_dc[[1]]
rm(merged_dc)
fusions <- rownames(FusionMetaCaller(merged_m)[["sortMatrix"]])
merged[, FusionMetaCaller_rank_name := match(get(TAG_name), fusions), with = F]
merged
}
write.maf <- function (...){
write.table(..., quote = F, col.names = T, row.names = F,
sep = "\t")
}
if(!interactive()){
args <- commandArgs(TRUE)
filename <- args[[1]]; args <- args[-1]
### /ifs/e63data/schultzlab/wangq/rna-seq-normal/fusion/bladder/fusion/Proj_GTEX_Bladder_merged_fusions_s_SRR1071717.txt
output_filename <- args[[1]]; args <- args[-1]
### load file
merged <- suppressWarnings(fread(filename))
merged <- merged[CALLER_ID != "STAR"]
merged <- merged[order(-TOTAL_COVERAGE)]
merged[!duplicated(merged,
by = c("CALLER_ID",
"GENE1",
"GENE2")),
TAG_genes := paste(GENE1,
GENE2,
sep = ":")]
merged <- annotate_merged_file(merged,
"TAG_genes",
"FusionMetaCaller_rank_genes")
merged[!duplicated(merged,
by = c("CALLER_ID",
"GENE1",
"GENE2",
"CHR1",
"BREAK_POINT1",
"CHR2",
"BREAK_POINT2")),
TAG_breakpoints := paste(GENE1,
GENE2,
CHR1,
BREAK_POINT1,
CHR2,
BREAK_POINT2,
sep = ":")]
merged <- annotate_merged_file(merged,
"TAG_breakpoints",
"FusionMetaCaller_rank_breakpoints")
write.maf(merged, output_filename)
}