forked from nextstrain/ncov
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
630 lines (592 loc) · 20.7 KB
/
Snakefile
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
from os import environ
from socket import getfqdn
from getpass import getuser
def get_todays_date():
from datetime import datetime
date = datetime.today().strftime('%Y-%m-%d')
return date
configfile: "config/Snakefile.yaml"
rule all:
input:
auspice_json = "auspice/ncov.json",
tip_frequencies_json = "auspice/ncov_tip-frequencies.json",
dated_auspice_json = expand("auspice/ncov_{date}.json", date=get_todays_date()),
dated_tip_frequencies_json = expand("auspice/ncov_{date}_tip-frequencies.json", date=get_todays_date()),
auspice_json_gisaid = "auspice/ncov_gisaid.json",
auspice_json_zh = "auspice/ncov_zh.json"
rule files:
params:
include = "config/include.txt",
exclude = "config/exclude.txt",
reference = "config/reference.gb",
outgroup = "config/outgroup.fasta",
weights = "config/weights.tsv",
ordering = "config/ordering.tsv",
color_schemes = "config/color_schemes.tsv",
auspice_config = "config/auspice_config.json",
auspice_config_gisaid = "config/auspice_config_gisaid.json",
auspice_config_zh = "config/auspice_config_zh.json",
lat_longs = "config/lat_longs.tsv",
description = "config/description.md",
description_zh = "config/description_zh.md",
clades = "config/clades.tsv"
files = rules.files.params
rule download:
message: "Downloading metadata and fasta files from S3"
output:
sequences = config["sequences"],
metadata = config["metadata"]
shell:
"""
aws s3 cp s3://nextstrain-ncov-private/sequences.fasta {output.sequences:q}
aws s3 cp s3://nextstrain-ncov-private/metadata.tsv {output.metadata:q}
"""
rule filter:
message:
"""
Filtering to
- excluding strains in {input.exclude}
- minimum genome length of {params.min_length}
"""
input:
sequences = rules.download.output.sequences,
metadata = rules.download.output.metadata,
include = files.include,
exclude = files.exclude
output:
sequences = "results/filtered.fasta"
params:
min_length = 25000,
group_by = "country",
sequences_per_group = 500,
exclude_where = "date='2020' date='2020-01-XX' date='2020-02-XX' date='2020-03-XX' date='2020-01' date='2020-02' date='2020-03'"
shell:
"""
augur filter \
--sequences {input.sequences} \
--metadata {input.metadata} \
--include {input.include} \
--exclude {input.exclude} \
--exclude-where {params.exclude_where}\
--min-length {params.min_length} \
--group-by {params.group_by} \
--sequences-per-group {params.sequences_per_group} \
--output {output.sequences}
"""
checkpoint partition_sequences:
input:
sequences = rules.filter.output.sequences
output:
split_sequences = directory("results/split_sequences")
params:
sequences_per_group = 150
shell:
"""
python3 scripts/partition-sequences.py \
--sequences {input.sequences} \
--sequences-per-group {params.sequences_per_group} \
--output-dir {output.split_sequences}
"""
rule align:
message:
"""
Aligning sequences to {input.reference}
- gaps relative to reference are considered real
"""
input:
sequences = "results/split_sequences/{i}.fasta",
reference = files.reference
output:
alignment = "results/split_alignments/{i}.fasta"
threads: 2
shell:
"""
augur align \
--sequences {input.sequences} \
--reference-sequence {input.reference} \
--output {output.alignment} \
--nthreads {threads} \
--remove-reference \
--fill-gaps
"""
def _get_alignments(wildcards):
checkpoint_output = checkpoints.partition_sequences.get(**wildcards).output[0]
return expand("results/split_alignments/{i}.fasta",
i=glob_wildcards(os.path.join(checkpoint_output, "{i}.fasta")).i)
rule aggregate_alignments:
message: "Collecting alignments"
input:
alignments = _get_alignments
output:
alignment = "results/aligned.fasta"
shell:
"""
cat {input.alignments} > {output.alignment}
"""
rule mask:
message:
"""
Mask bases in alignment
- masking {params.mask_from_beginning} from beginning
- masking {params.mask_from_end} from end
- masking other sites: {params.mask_sites}
"""
input:
alignment = rules.aggregate_alignments.output.alignment
output:
alignment = "results/masked.fasta"
params:
mask_from_beginning = 130,
mask_from_end = 50,
mask_sites = "18529 29849 29851 29853"
shell:
"""
python3 scripts/mask-alignment.py \
--alignment {input.alignment} \
--mask-from-beginning {params.mask_from_beginning} \
--mask-from-end {params.mask_from_end} \
--mask-sites {params.mask_sites} \
--output {output.alignment}
"""
rule tree:
message: "Building tree"
input:
alignment = rules.mask.output.alignment
output:
tree = "results/tree_raw.nwk"
threads: 4
shell:
"""
augur tree \
--alignment {input.alignment} \
--output {output.tree} \
--nthreads {threads}
"""
rule refine:
message:
"""
Refining tree
- estimate timetree
- use {params.coalescent} coalescent timescale
- estimate {params.date_inference} node dates
"""
input:
tree = rules.tree.output.tree,
alignment = rules.mask.output,
metadata = rules.download.output.metadata
output:
tree = "results/tree.nwk",
node_data = "results/branch_lengths.json"
params:
root = "Wuhan-Hu-1/2019 Wuhan/WH01/2019",
clock_rate = 0.0008,
clock_std_dev = 0.0004,
coalescent = "skyline",
date_inference = "marginal",
divergence_unit = "mutations",
clock_filter_iqd = 4
shell:
"""
augur refine \
--tree {input.tree} \
--alignment {input.alignment} \
--metadata {input.metadata} \
--output-tree {output.tree} \
--output-node-data {output.node_data} \
--root {params.root} \
--timetree \
--clock-rate {params.clock_rate} \
--clock-std-dev {params.clock_std_dev} \
--coalescent {params.coalescent} \
--date-inference {params.date_inference} \
--divergence-unit {params.divergence_unit} \
--date-confidence \
--no-covariance \
--clock-filter-iqd {params.clock_filter_iqd}
"""
rule ancestral:
message:
"""
Reconstructing ancestral sequences and mutations
- not inferring ambiguous mutations
"""
input:
tree = "results/tree.nwk",
alignment = rules.mask.output
output:
node_data = "results/nt_muts.json"
params:
inference = "joint"
shell:
"""
augur ancestral \
--tree {input.tree} \
--alignment {input.alignment} \
--output-node-data {output.node_data} \
--inference {params.inference} \
--infer-ambiguous
"""
rule haplotype_status:
message: "Annotating haplotype status relative to {params.reference_node_name}"
input:
nt_muts = rules.ancestral.output.node_data
output:
node_data = "results/haplotype_status.json"
params:
reference_node_name = "USA/WA1/2020"
shell:
"""
python3 scripts/annotate-haplotype-status.py \
--ancestral-sequences {input.nt_muts} \
--reference-node-name {params.reference_node_name:q} \
--output {output.node_data}
"""
rule translate:
message: "Translating amino acid sequences"
input:
tree = "results/tree.nwk",
node_data = rules.ancestral.output.node_data,
reference = files.reference
output:
node_data = "results/aa_muts.json"
shell:
"""
augur translate \
--tree {input.tree} \
--ancestral-sequences {input.node_data} \
--reference-sequence {input.reference} \
--output-node-data {output.node_data} \
"""
rule traits:
message:
"""
Inferring ancestral traits for {params.columns!s}
- increase uncertainty of reconstruction by {params.sampling_bias_correction} to partially account for sampling bias
"""
input:
tree = "results/tree.nwk",
metadata = rules.download.output.metadata,
weights = files.weights
output:
node_data = "results/traits.json",
params:
columns = "division_exposure",
sampling_bias_correction = 2.5
shell:
"""
augur traits \
--tree {input.tree} \
--metadata {input.metadata} \
--weights {input.weights} \
--output {output.node_data} \
--columns {params.columns} \
--confidence \
--sampling-bias-correction {params.sampling_bias_correction} \
"""
rule clades:
message: "Adding internal clade labels"
input:
tree = "results/tree.nwk",
aa_muts = rules.translate.output.node_data,
nuc_muts = rules.ancestral.output.node_data,
clades = files.clades
output:
clade_data = "results/clades.json"
shell:
"""
augur clades --tree {input.tree} \
--mutations {input.nuc_muts} {input.aa_muts} \
--clades {input.clades} \
--output-node-data {output.clade_data}
"""
rule colors:
message: "Constructing colors file"
input:
ordering = files.ordering,
color_schemes = files.color_schemes
output:
colors = "config/colors.tsv"
shell:
"""
python3 scripts/assign-colors.py \
--ordering {input.ordering} \
--color-schemes {input.color_schemes} \
--output {output.colors}
"""
rule recency:
message: "Use metadata on submission date to construct submission recency field"
input:
metadata = rules.download.output.metadata
output:
"results/recency.json"
shell:
"""
python3 scripts/construct-recency-from-submission-date.py \
--metadata {input.metadata} \
--output {output}
"""
rule tip_frequencies:
message: "Estimating censored KDE frequencies for tips"
input:
tree = rules.refine.output.tree,
metadata = rules.download.output.metadata
output:
tip_frequencies_json = "auspice/ncov_tip-frequencies.json"
params:
min_date = 2020.0,
pivot_interval = 1,
narrow_bandwidth = 0.05,
proportion_wide = 0.0
shell:
"""
augur frequencies \
--method kde \
--metadata {input.metadata} \
--tree {input.tree} \
--min-date {params.min_date} \
--pivot-interval {params.pivot_interval} \
--narrow-bandwidth {params.narrow_bandwidth} \
--proportion-wide {params.proportion_wide} \
--output {output.tip_frequencies_json}
"""
rule export:
message: "Exporting data files for for auspice"
input:
tree = rules.refine.output.tree,
metadata = rules.download.output.metadata,
branch_lengths = rules.refine.output.node_data,
nt_muts = rules.ancestral.output.node_data,
aa_muts = rules.translate.output.node_data,
traits = rules.traits.output.node_data,
auspice_config = files.auspice_config,
colors = rules.colors.output.colors,
lat_longs = files.lat_longs,
description = files.description,
clades = "results/clades.json",
recency = rules.recency.output
output:
auspice_json = "results/ncov_with_accessions.json"
shell:
"""
augur export v2 \
--tree {input.tree} \
--metadata {input.metadata} \
--node-data {input.branch_lengths} {input.nt_muts} {input.aa_muts} {input.traits} {input.clades} {input.recency} \
--auspice-config {input.auspice_config} \
--colors {input.colors} \
--lat-longs {input.lat_longs} \
--description {input.description} \
--output {output.auspice_json}
"""
rule export_gisaid:
message: "Exporting data files for for auspice"
input:
tree = rules.refine.output.tree,
metadata = rules.download.output.metadata,
branch_lengths = rules.refine.output.node_data,
nt_muts = rules.ancestral.output.node_data,
aa_muts = rules.translate.output.node_data,
traits = rules.traits.output.node_data,
auspice_config = files.auspice_config_gisaid,
colors = rules.colors.output.colors,
lat_longs = files.lat_longs,
description = files.description,
clades = rules.clades.output.clade_data,
recency = rules.recency.output
output:
auspice_json = "results/ncov_gisaid_with_accessions.json"
shell:
"""
augur export v2 \
--tree {input.tree} \
--metadata {input.metadata} \
--node-data {input.branch_lengths} {input.nt_muts} {input.aa_muts} {input.traits} {input.clades} {input.recency} \
--auspice-config {input.auspice_config} \
--colors {input.colors} \
--lat-longs {input.lat_longs} \
--description {input.description} \
--output {output.auspice_json}
"""
rule export_zh:
message: "Exporting data files for for auspice"
input:
tree = rules.refine.output.tree,
metadata = rules.download.output.metadata,
branch_lengths = rules.refine.output.node_data,
nt_muts = rules.ancestral.output.node_data,
aa_muts = rules.translate.output.node_data,
traits = rules.traits.output.node_data,
auspice_config = files.auspice_config_zh,
colors = rules.colors.output.colors,
lat_longs = files.lat_longs,
description = files.description_zh,
clades = rules.clades.output.clade_data,
recency = rules.recency.output
output:
auspice_json = "results/ncov_zh_with_accessions.json"
shell:
"""
augur export v2 \
--tree {input.tree} \
--metadata {input.metadata} \
--node-data {input.branch_lengths} {input.nt_muts} {input.aa_muts} {input.traits} {input.clades} {input.recency} \
--auspice-config {input.auspice_config} \
--colors {input.colors} \
--lat-longs {input.lat_longs} \
--description {input.description} \
--output {output.auspice_json}
"""
rule incorporate_travel_history:
message: "Adjusting main auspice JSON to take into account travel history"
input:
lat_longs = files.lat_longs,
colors = rules.colors.output.colors,
auspice_json = rules.export.output.auspice_json
output:
auspice_json = "results/ncov_with_accessions_and_travel_branches.json"
shell:
"""
python3 ./scripts/modify_tree_according_to_division_exposure.py \
{input.auspice_json} {input.colors} {input.lat_longs} {output.auspice_json}
"""
rule incorporate_travel_history_gisaid:
message: "Adjusting GISAID auspice JSON to take into account travel history"
input:
lat_longs = files.lat_longs,
colors = rules.colors.output.colors,
auspice_json = rules.export_gisaid.output.auspice_json
output:
auspice_json = "results/ncov_gisaid_with_accessions_and_travel_branches.json"
shell:
"""
python3 ./scripts/modify_tree_according_to_division_exposure.py \
{input.auspice_json} {input.colors} {input.lat_longs} {output.auspice_json}
"""
rule incorporate_travel_history_zh:
message: "Adjusting ZH auspice JSON to take into account travel history"
input:
lat_longs = files.lat_longs,
colors = rules.colors.output.colors,
auspice_json = rules.export_zh.output.auspice_json
output:
auspice_json = "results/ncov_zh_with_accessions_and_travel_branches.json"
shell:
"""
python3 ./scripts/modify_tree_according_to_division_exposure.py \
{input.auspice_json} {input.colors} {input.lat_longs} {output.auspice_json}
"""
rule fix_colorings:
message: "Remove extraneous colorings for main build"
input:
auspice_json = rules.incorporate_travel_history.output.auspice_json
output:
auspice_json = "auspice/ncov.json"
shell:
"""
python scripts/fix-colorings.py \
--input {input.auspice_json} \
--output {output.auspice_json}
"""
rule fix_colorings_gisaid:
message: "Remove extraneous colorings for the GISAID build"
input:
auspice_json = rules.incorporate_travel_history_gisaid.output.auspice_json
output:
auspice_json = "auspice/ncov_gisaid.json"
shell:
"""
python scripts/fix-colorings.py \
--input {input.auspice_json} \
--output {output.auspice_json}
"""
rule fix_colorings_zh:
message: "Remove extraneous colorings for the Chinese language build"
input:
auspice_json = rules.incorporate_travel_history_zh.output.auspice_json
output:
auspice_json = "auspice/ncov_zh.json"
shell:
"""
python scripts/fix-colorings.py \
--input {input.auspice_json} \
--output {output.auspice_json}
"""
rule dated_json:
message: "Copying dated Auspice JSON"
input:
auspice_json = rules.fix_colorings.output.auspice_json,
tip_frequencies_json = rules.tip_frequencies.output.tip_frequencies_json
output:
dated_auspice_json = rules.all.input.dated_auspice_json,
dated_tip_frequencies_json = rules.all.input.dated_tip_frequencies_json
shell:
"""
cp {input.auspice_json} {output.dated_auspice_json}
cp {input.tip_frequencies_json} {output.dated_tip_frequencies_json}
"""
rule poisson_tmrca:
input:
tree = rules.refine.output.tree,
metadata = rules.download.output.metadata,
nt_muts = rules.ancestral.output.node_data
output:
"figures/ncov_poisson-tmrca.png"
shell:
"""
python scripts/tmrca_estimate.py --tree {input.tree} --metadata {input.metadata} --node-data {input.nt_muts} --output {output}
"""
rule branching_process_R0:
params:
infectious_period = 10, # days
population = [6000, 30000, 150000],
start_recent = "2019-12-01",
start_early = "2019-11-01"
output:
"figures/ncov_branching-R0-recent.png",
"figures/ncov_branching-R0-early.png"
shell:
"""
python scripts/branching_process.py --infectious-period {params.infectious_period}\
--start {params.start_recent} \
--population {params.population} \
--output {output[0]} &&\
python scripts/branching_process.py --infectious-period {params.infectious_period}\
--start {params.start_early} \
--population {params.population} \
--output {output[1]}
"""
try:
deploy_origin = (
f"from AWS Batch job `{environ['AWS_BATCH_JOB_ID']}`"
if environ.get("AWS_BATCH_JOB_ID") else
f"by the hands of {getuser()}@{getfqdn()}"
)
except:
# getuser() and getfqdn() may not always succeed, and this catch-all except
# means that the Snakefile won't crash.
deploy_origin = "by an unknown identity"
rule deploy_to_staging:
input:
*rules.all.input
params:
slack_message = json.dumps({"text":f"Deployed <https://nextstrain.org/staging/ncov|nextstrain.org/staging/ncov> {deploy_origin}"}),
slack_webhook = config["slack_webhook"] or "",
s3_staging_url = config["s3_staging_url"]
shell:
"""
nextstrain deploy {params.s3_staging_url:q} {input:q}
if [[ -n "{params.slack_webhook}" ]]; then
curl {params.slack_webhook:q} \
--header 'Content-type: application/json' \
--data-raw {params.slack_message:q} \
--fail --silent --show-error \
--include
fi
"""
rule clean:
message: "Removing directories: {params}"
params:
"results ",
"auspice"
shell:
"rm -rfv {params}"