generated from snakemake-workflows/snakemake-workflow-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.smk
385 lines (343 loc) · 12.5 KB
/
eval.smk
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
rule get_reference_dict:
input:
reference="resources/reference/genome.fasta",
output:
"resources/reference/genome.fasta.dict",
log:
"logs/get-reference-dict.log",
conda:
"../envs/picard.yaml"
shell:
"picard CreateSequenceDictionary -R {input.reference} -O {input.reference}.dict &> {log}"
rule liftover_callset:
input:
callset=get_callset_correct_contigs,
liftover_chain="resources/liftover/GRCh37_to_GRCh38.chain.gz",
reference="resources/reference/genome.fasta",
reference_dict="resources/reference/genome.fasta.dict",
output:
"results/normalized-variants/{callset}.lifted.vcf.gz",
log:
"logs/liftover_callset/{callset}.log",
conda:
"../envs/picard.yaml"
resources:
mem_mb=64000,
shell:
"picard LiftoverVcf -Xmx64g --MAX_RECORDS_IN_RAM 100000 -I {input.callset} -O {output} --CHAIN {input.liftover_chain} --REJECT {output}_rejected_variants.vcf -R {input.reference} &> {log}"
rule rename_contigs:
input:
calls=get_raw_callset,
repl_file=get_rename_contig_file,
output:
"results/normalized-variants/{callset}.replaced-contigs.vcf.gz",
log:
"logs/rename-contigs/{callset}.log",
conda:
"../envs/tools.yaml"
shell:
"bcftools annotate {input.calls} --rename-chrs {input.repl_file} "
"-Oz -o {output} 2> {log}"
rule add_genotype_field:
input:
get_callset_correct_contigs_liftover,
output:
"results/normalized-variants/{callset}.gt-added.vcf.gz",
log:
"logs/add_genotype_field/{callset}.log",
params:
get_somatic_sample_name,
conda:
"../envs/vatools.yaml"
shell:
# part after || gets executed if vcf-genotype-annotater fails because GT field is already present
# bcftools convert makes sure that input for vcf-genotype-annotator is in vcf format
"vcf-genotype-annotator <(bcftools convert -Ov {input}) {params} 0/1 -o {output} &> {log} || bcftools view {input} -Oz > {output}"
rule add_format_field:
input:
"resources/variants/{genome}/all.truth.norm.bcf",
output:
"resources/variants/{genome}/all.truth.format-added.vcf.gz",
log:
"logs/add_format_field/{genome}.log",
conda:
"../envs/vatools.yaml"
shell:
# TODO: Optional - Check first if FORMAT field is present for example with
# TODO: bcftools view -h out.vcf.gz | grep FORMAT oder bcftools query -l all.bcf
# bcftools convert makes sure that input for vcf-genotype-annotator is in vcf format
# adds FORMAT field with GT field and sample name 'truth'
"vcf-genotype-annotator <(bcftools convert -Ov {input}) truth 0/1 -o {output} &> {log}"
rule remove_non_pass:
input:
get_callset,
output:
"results/filtered-variants/{callset}.bcf",
log:
"logs/filter/{callset}.log",
params:
extra="-f 'PASS,.'",
wrapper:
"v3.3.6/bio/bcftools/view"
rule intersect_calls_with_target_regions:
input:
bcf="results/filtered-variants/{callset}.bcf",
regions=get_target_regions,
output:
pipe("results/normalized-variants/{callset}_intersected.vcf"),
log:
"logs/intersect-calls/{callset}.log",
conda:
"../envs/tools.yaml"
shell:
"(bedtools intersect -b {input.regions} -a "
"<(bcftools view {input.bcf}) -wa -f 1.0 -header > {output}) 2> {log}"
rule restrict_to_reference_contigs:
input:
calls="results/filtered-variants/{callset}.bcf",
calls_index="results/filtered-variants/{callset}.bcf.csi",
ref_index="resources/reference/genome.fasta.fai",
output:
"results/filtered-variants/{callset}_restricted.bcf",
log:
"logs/restrict-to-reference-contigs/{callset}.log",
conda:
"../envs/tools.yaml"
shell:
"(bcftools view --regions $(cut -f1 {input.ref_index} | tr '\\n' ',') {input.calls} |"
" bcftools reheader -f {input.ref_index} > {output}) 2> {log}"
rule normalize_calls:
input:
calls=branch(
intersect_calls,
then="results/normalized-variants/{callset}_intersected.vcf",
otherwise="results/filtered-variants/{callset}_restricted.bcf",
),
ref="resources/reference/genome.fasta",
ref_index="resources/reference/genome.fasta.fai",
output:
"results/normalized-variants/{callset}.vcf.gz",
params:
extra=get_norm_params,
log:
"logs/normalize-calls/{callset}.log",
conda:
"../envs/tools.yaml"
shell:
"(bcftools norm {params.extra} --fasta-ref {input.ref} {input.calls} | "
"bcftools view -Oz > {output}) 2> {log}"
rule stratify_truth:
input:
variants=get_benchmark_truth,
regions="resources/regions/{benchmark}/test-regions.cov-{cov}.bed",
output:
"results/variants/{benchmark}.truth.cov-{cov}.vcf.gz",
log:
"logs/stratify-truth/{benchmark}.{cov}.log",
conda:
"../envs/tools.yaml"
shell:
"(bedtools intersect -b {input.regions} -a "
"<(bcftools view {input.variants} | bcftools reheader -s <(echo 'truth')) -wa -f 1.0 -header | "
"bcftools view -Oz > {output}) 2> {log}"
rule stratify_results:
input:
variants="results/normalized-variants/{callset}.vcf.gz",
regions=get_test_regions,
output:
"results/stratified-variants/{callset}/{cov}.vcf.gz",
log:
"logs/stratify-results/{callset}/{cov}.log",
conda:
"../envs/tools.yaml"
shell:
"(bedtools intersect -b {input.regions} -a "
"<(bcftools view {input.variants}) -wa -f 1.0 -header | "
"bcftools view -Oz > {output}) 2> {log}"
rule index_stratified_truth:
input:
"results/variants/{benchmark}.truth.cov-{cov}.vcf.gz",
output:
"results/variants/{benchmark}.truth.cov-{cov}.vcf.gz.csi",
log:
"logs/bcftools-index/{benchmark}.truth.{cov}.log",
wrapper:
"v1.7.2/bio/bcftools/index"
checkpoint stat_truth:
input:
"results/variants/{benchmark}.truth.cov-{cov}.vcf.gz",
output:
"results/variants/{benchmark}.truth.cov-{cov}.stats.json",
log:
"logs/stat-truth/{benchmark}.{cov}.log",
conda:
"../envs/pysam.yaml"
script:
"../scripts/stat-truth.py"
rule generate_sdf:
input:
genome="resources/reference/genome.fasta",
genome_index="resources/reference/genome.fasta.fai",
output:
directory("resources/reference/genome-sdf"),
log:
"logs/rtg-tools/sdf.log",
conda:
"../envs/rtg-tools.yaml"
shell:
"rtg format --output {output} {input.genome} &> {log}"
rule benchmark_variants:
input:
truth=get_stratified_truth(),
truth_idx=get_stratified_truth(".tbi"),
query="results/stratified-variants/{callset}/{cov}.vcf.gz",
query_index="results/stratified-variants/{callset}/{cov}.vcf.gz.tbi",
genome="resources/reference/genome-sdf",
output:
"results/vcfeval/{callset}/{cov}/output.vcf.gz",
log:
"logs/vcfeval/{callset}/{cov}.log",
params:
output=lambda w, output: os.path.dirname(output[0]),
somatic=get_somatic_flag,
conda:
"../envs/rtg-tools.yaml"
threads: 32
shell:
"rm -r {params.output}; rtg vcfeval --threads {threads} --ref-overlap --all-records "
"--output-mode ga4gh --baseline {input.truth} --calls {input.query} "
"--output {params.output} --template {input.genome} {params.somatic} &> {log}"
rule calc_precision_recall:
input:
calls="results/vcfeval/{callset}/{cov}/output.vcf.gz",
idx="results/vcfeval/{callset}/{cov}/output.vcf.gz.tbi",
common_src=common_src,
truth=get_stratified_truth(),
truth_idx=get_stratified_truth(".tbi"),
query="results/stratified-variants/{callset}/{cov}.vcf.gz",
query_index="results/stratified-variants/{callset}/{cov}.vcf.gz.tbi",
output:
snvs="results/precision-recall/callsets/{callset}/{cov}.{vartype}.tsv",
log:
"logs/calc-precision-recall/{callset}/{cov}/{vartype}.log",
params:
vaf_fields=get_vaf_fields,
conda:
"../envs/pysam.yaml"
script:
"../scripts/calc-precision-recall.py"
rule collect_stratifications:
input:
get_collect_stratifications_input,
output:
"results/precision-recall/callsets/{callset}.{vartype}.tsv",
params:
coverages=get_nonempty_coverages,
coverage_lower_bounds=get_coverages,
log:
"logs/collect-stratifications/{callset}/{vartype}.log",
conda:
"../envs/stats.yaml"
# We want this to be determined before FP/FN collection in order to avoid memory
# issues with callsets that do not match the truth at all.
priority: 2
script:
"../scripts/collect-stratifications.py"
rule collect_precision_recall:
input:
tables=get_collect_precision_recall_input,
output:
"results/precision-recall/benchmarks/{benchmark}.{vartype}.tsv",
params:
callsets=lambda w: get_benchmark_callsets(w.benchmark),
labels=get_collect_precision_recall_labels,
vaf=get_vaf_status,
log:
"logs/collect-precision-recall/{benchmark}/{vartype}.log",
conda:
"../envs/stats.yaml"
script:
"../scripts/collect-precision-recall.py"
rule report_precision_recall:
input:
config=workflow.source_path(
"../resources/datavzrd/precision-recall-config.yte.yaml"
),
table="results/precision-recall/benchmarks/{benchmark}.{vartype}.tsv",
output:
report(
directory("results/report/precision-recall/{benchmark}/{vartype}"),
htmlindex="index.html",
category="precision/recall",
labels={
"benchmark": "{benchmark}",
"vartype": "{vartype}",
},
),
log:
"logs/datavzrd/precision-recall/{benchmark}/{vartype}.log",
params:
somatic=get_somatic_status,
vaf=get_vaf_status,
high_coverage=get_high_coverage_status,
genome=get_genome_name,
version=get_genome_version,
wrapper:
"v3.13.7/utils/datavzrd"
rule extract_fp_fn:
input:
calls="results/vcfeval/{callset}/{cov}/output.vcf.gz",
common_src=common_src,
output:
"results/fp-fn/callsets/{cov}/{callset}/{classification}.tsv",
log:
"logs/extract-fp-fn/{cov}/{callset}/{classification}.log",
conda:
"../envs/vembrane.yaml"
script:
"../scripts/extract-fp-fn.py"
# TODO: if one of the input callsets has all sites as FN, the resulting merged table
# becomes empty. This needs to be fixed, but is not super urgent because it is not realistic to
# happen in reality.
rule collect_fp_fn:
input:
tables=get_collect_fp_fn_input,
output:
main="results/fp-fn/genomes/{genome}/{cov}/{classification}/main.tsv",
dependency_sorting=directory(
"results/fp-fn/genomes/{genome}/{cov}/{classification}/dependency-sorting"
),
params:
callsets=get_collect_fp_fn_callsets,
labels=get_collect_fp_fn_labels,
label_names=lambda w: get_callsets_labels(get_genome_callsets(w.genome)),
max_entries=config.get("max-fp-fn-entries", 300),
log:
"logs/collect-fp-fn/{genome}/{cov}/{classification}.log",
conda:
"../envs/stats.yaml"
# This has to happen after precision/recall has been computed, otherwise we risk
# extremely high memory usage if a callset does not match the truth at all.
priority: 1
script:
"../scripts/collect-fp-fn.py"
rule report_fp_fn:
input:
main_dataset="results/fp-fn/genomes/{genome}/{cov}/{classification}/main.tsv",
dependency_sorting_datasets="results/fp-fn/genomes/{genome}/{cov}/{classification}/dependency-sorting",
config=workflow.source_path("../resources/datavzrd/fp-fn-config.yte.yaml"),
output:
report(
directory("results/report/fp-fn/{genome}/{cov}/{classification}"),
htmlindex="index.html",
category="{classification} variants",
subcategory=lambda w: w.genome,
labels=lambda w: {"coverage": w.cov},
),
log:
"logs/datavzrd/fp-fn/{genome}/{cov}/{classification}.log",
params:
labels=lambda w: get_callsets_labels(get_genome_callsets(w.genome)),
version=get_genome_version,
wrapper:
"v3.13.7/utils/datavzrd"