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 | from itertools import product
from functools import partial
import matplotlib
matplotlib.use("agg")
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import common
import numpy as np
import math
from matplotlib.lines import Line2D
vartype = snakemake.wildcards.vartype
colors = common.get_colors(snakemake.config)
def props(callers):
return product(callers, snakemake.params.len_ranges)
def plot_len_range(minlen, maxlen, min_precision=0.0):
truth = common.load_variants(
snakemake.input.truth, minlen, maxlen, vartype=vartype)
def plot(calls,
label,
color,
line=True,
style="-",
invert=False,
markersize=4,
endmarker=False):
calls = pd.read_table(calls, index_col=0)
if len(calls) < 10:
return
if line:
thresholds = calls.score.quantile(np.linspace(0.0, 1.0, 50))
precision = []
recall = []
for t in thresholds:
if invert:
c = calls[calls.score >= t]
else:
c = calls[calls.score <= t]
p = common.precision(c)
r = common.recall(c, truth)
print(label, t, c.shape[0], p, r)
if len(c) < 10:
print("skipping threshold: too few calls", c)
continue
precision.append(p)
recall.append(r)
if len(precision) <= 2:
print("skipping curve because we have too few values")
return
else:
precision = [common.precision(calls)]
recall = [common.recall(calls, truth)]
style = "."
print(label, calls.shape[0], precision, recall)
plt.plot(
recall,
precision,
style,
color=color,
label=label,
markersize=markersize
)
if endmarker:
plt.plot(recall[-1], precision[-1], "s", color=color, markersize=markersize)
handles = []
for calls, (caller,
len_range) in zip(snakemake.input.varlociraptor_calls,
props(snakemake.params.varlociraptor_callers)):
if len_range[0] != minlen and len_range[1] != maxlen:
continue
label = "varlociraptor+{}".format(caller)
plot(calls, label, colors[caller], endmarker=True)
handles.append(Line2D([0], [0], color=colors[caller], label=label))
for calls, (caller,
len_range) in zip(snakemake.input.default_calls,
props(snakemake.params.default_callers)):
if len_range[0] != minlen and len_range[1] != maxlen:
continue
color = colors[caller]
plot(
calls,
caller,
color,
style=":",
invert=snakemake.config["caller"][caller].get("invert", False))
if caller in snakemake.params.adhoc_callers:
handles.append(Line2D([0], [0], markersize=10, markerfacecolor=color, markeredgecolor=color, color=color, label=caller, marker=".", linestyle=":"))
else:
handles.append(Line2D([0], [0], color=color, label=caller, linestyle=":"))
for calls, (caller, len_range) in zip(snakemake.input.adhoc_calls,
props(snakemake.params.adhoc_callers)):
if len_range[0] != minlen and len_range[1] != maxlen:
continue
color = colors[caller]
plot(calls, caller, color, markersize=10, line=False)
if caller not in snakemake.params.default_callers:
handles.append(Line2D([0], [0], markersize=10, markerfacecolor=color, markeredgecolor=color, label=caller, marker=".", lw=0))
sns.despine()
ax = plt.gca()
plt.ylim((min_precision, 1.01 if min_precision == 0.0 else 1.001))
return ax, handles
plot = plot_len_range
fig_height = None
legend_outside = snakemake.params.legend_outside
if snakemake.wildcards.zoom == "zoom":
plot = partial(plot_len_range, min_precision=0.99 if vartype == "INS" else 0.95)
fig_height = 3
legend_outside = True
common.plot_ranges(
snakemake.params.len_ranges,
plot,
xlabel="recall",
ylabel="precision",
fig_height=fig_height,
legend_outside=legend_outside,
)
plt.savefig(snakemake.output[0], bbox_inches="tight")
|