-
Notifications
You must be signed in to change notification settings - Fork 123
/
rocplot.Rscript
executable file
·198 lines (168 loc) · 6.63 KB
/
rocplot.Rscript
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
#!/usr/bin/env Rscript
#
# Copyright (c) 2010-2015 Illumina, Inc.
# All rights reserved.
#
# This file is distributed under the simplified BSD license.
# The full text can be found here (and in LICENSE.txt in the root folder of
# this distribution):
#
# https://github.com/Illumina/licenses/blob/master/Simplified-BSD-License.txt
#
# Plot hap.py ROCs and PASS points
#
# This script requires the ggplot2 package.
# To install it, run this command in R:
#
# install.packages(c("ggplot2"))
#
# Usage:
#
# This script runs on a set of hap.py results. Result n will have been run
# with hap.py -o prefix_n. The names for each result are optional, they can
# be used to specify a custom label for the ROCs in the plot.
#
# run Rscript rocplot.Rscript [-pr] output_name prefix_1:name_1 ... prefix_n:name_n
#
# Use the -pr switch to produce a precision-recall curve rather than a TPR/FPR curve
#
# Author:
#
# Peter Krusche <[email protected]>
#
library(ggplot2)
library(tools)
args = commandArgs(trailingOnly=TRUE)
if(length(args) < 2) {
stop("Usage: rocplot.Rscript output_name prefix_1:name_1 ... ")
}
if("-pr" %in% args) {
args = args[args != "-pr"]
pr = TRUE
} else {
pr = FALSE
}
# Read single hap.py / xcmp dataset
read_single = function(x) {
nx = strsplit(x, "\\:")[[1]]
if(length(nx) == 1) {
name = basename(file_path_sans_ext(x))
} else {
x = nx[1]
name = nx[2]
}
cat(sprintf("Reading %s as %s\n", x, name))
all_results = list()
all_results$roc_data_snp_all = read.csv(paste(x, "roc.Locations.SNP", "csv", "gz", sep="."))
all_results$roc_data_indel_all = read.csv(paste(x, "roc.Locations.INDEL", "csv", "gz", sep="."))
all_results$roc_data_snp_pass = read.csv(paste(x, "roc.Locations.SNP.PASS", "csv", "gz", sep="."))
all_results$roc_data_indel_pass = read.csv(paste(x, "roc.Locations.INDEL.PASS", "csv", "gz", sep="."))
sel_snp_file = paste(x, "roc.Locations.SNP.SEL", "csv", "gz", sep=".")
# if we have a selectively-filtered ROC, don't show "PASS" ROC
if(file.exists(sel_snp_file)) {
all_results$roc_data_snp_sel = read.csv(sel_snp_file)
} else {
all_results$roc_data_snp_sel = all_results$roc_data_snp_pass
}
all_results$roc_data_snp_all = head(subset(all_results$roc_data_snp_all,
QQ == min(all_results$roc_data_snp_all["QQ"])),
n=1)
all_results$roc_data_snp_pass = head(subset(all_results$roc_data_snp_pass,
QQ == min(all_results$roc_data_snp_pass["QQ"])),
n=1)
sel_min = head(subset(all_results$roc_data_snp_sel,
QQ == min(all_results$roc_data_snp_sel["QQ"])),
n=1)
all_results$roc_connector_snp =
rbind(all_results$roc_data_snp_all, sel_min)
all_results$roc_connector_snp$Filter = "CONN"
all_results$roc_data_snp_sel$Filter = "ROC"
sel_indel_file = paste(x, "roc.Locations.INDEL.SEL", "csv", "gz", sep=".")
if(file.exists(sel_indel_file)) {
all_results$roc_data_indel_sel = read.csv(sel_indel_file)
} else {
# use PASS ROC if no SEL ROC present
all_results$roc_data_indel_sel = all_results$roc_data_indel_pass
}
# just keep single ALL and PASS point
all_results$roc_data_indel_all = head(subset(all_results$roc_data_indel_all,
QQ == min(all_results$roc_data_indel_all["QQ"])),
n=1)
all_results$roc_data_indel_pass = head(subset(all_results$roc_data_indel_pass,
QQ == min(all_results$roc_data_indel_pass["QQ"])),
n=1)
sel_min = head(subset(all_results$roc_data_indel_sel,
QQ == min(all_results$roc_data_indel_sel["QQ"])),
n=1)
all_results$roc_connector_indel =
rbind(all_results$roc_data_indel_all, sel_min)
all_results$roc_connector_indel$Filter = "CONN"
all_results$roc_data_indel_sel$Filter = "ROC"
result = do.call(rbind, all_results)
row.names(result) = NULL
result$filename = x
result$name = name
result$igroup = paste(result$name,
result$Filter,
result$Type)
return(result)
}
# Plot P/R curves
plot_data = function(pdata, is.PR=FALSE) {
# precision / recall curve
if(is.PR) {
xaxis = "METRIC.Recall"
yaxis = "METRIC.Precision"
} else {
# approximate ROC-style curve (FPR is not correct)
xaxis = "FPR"
yaxis = "TPR"
pdata$FPR = pdata$QUERY.FP / (pdata$QUERY.TOTAL - pdata$QUERY.UNK)
pdata$TPR = pdata$TRUTH.TP / (pdata$TRUTH.TP + pdata$TRUTH.FN)
cc = complete.cases(pdata[, c(xaxis, yaxis)])
pdata = pdata[cc, ]
}
plt = ggplot(pdata, aes_string(x=xaxis, y=yaxis, color="name"))
facet_wrap(~Type)
# ROC lines
plt = plt +
geom_line(data = subset(pdata, Filter == "ROC"),
mapping=aes(group=igroup),
size=1.5,
linetype=3)
# Connector between ALL and start of ROC
plt = plt +
geom_line(data = subset(pdata, Filter == "CONN"),
mapping=aes(group=igroup),
size=1.5,
linetype=4)
plt = plt +
geom_point(data = subset(pdata, Filter %in% c("CONN")),
mapping=aes(group=igroup),
size=8)
plt = plt +
geom_point(data = subset(pdata, Filter %in% c("PASS", "ALL")),
mapping=aes(shape=Filter, group=igroup),
size=8)
xl_min = max(0,
min(subset(pdata, Filter %in% c("PASS", "ALL"))[[xaxis]]) - 0.02)
xl_max = min(1.0,
max(subset(pdata, Filter %in% c("PASS", "ALL"))[[xaxis]]) + 0.02)
yl_min = max(0,
min(subset(pdata, Filter %in% c("PASS", "ALL"))[[yaxis]]) - 0.01)
yl_max = min(1.0,
max(subset(pdata, Filter %in% c("PASS", "ALL"))[[yaxis]]) + 0.01)
plt = plt +
scale_color_brewer("", palette="Set1") +
xlim(c(xl_min, xl_max)) +
ylim(c(yl_min, yl_max)) +
theme_bw(base_size=18)
return(plt)
}
data = do.call(rbind, lapply(args[2:length(args)], read_single))
pdata = subset(data, Type=="SNP")
ggsave(paste(args[1], "SNP", "png", sep="."),
plot_data(pdata, is.PR=pr), width=8, height=)
pdata = subset(data, Type=="INDEL")
ggsave(paste(args[1], "INDEL", "png", sep="."),
plot_data(pdata, is.PR=pr), width=8, height=)