-
Notifications
You must be signed in to change notification settings - Fork 403
/
main_workflow.smk
1003 lines (936 loc) · 36.6 KB
/
main_workflow.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
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
rule download:
message: "Downloading metadata and fasta files from S3"
output:
sequences = config["sequences"],
metadata = config["metadata"]
conda: config["conda_environment"]
shell:
"""
aws s3 cp s3://nextstrain-ncov-private/metadata.tsv.gz - | gunzip -cq >{output.metadata:q}
aws s3 cp s3://nextstrain-ncov-private/sequences.fasta.gz - | gunzip -cq > {output.sequences:q}
"""
rule filter:
message:
"""
Filtering to
- excluding strains in {input.exclude}
"""
input:
sequences = rules.download.output.sequences,
metadata = rules.download.output.metadata,
include = config["files"]["include"],
exclude = config["files"]["exclude"]
output:
sequences = "results/filtered.fasta"
log:
"logs/filtered.txt"
params:
min_length = config["filter"]["min_length"],
exclude_where = config["filter"]["exclude_where"],
min_date = config["filter"]["min_date"],
date = date.today().strftime("%Y-%m-%d")
conda: config["conda_environment"]
shell:
"""
augur filter \
--sequences {input.sequences} \
--metadata {input.metadata} \
--include {input.include} \
--max-date {params.date} \
--min-date {params.min_date} \
--exclude {input.exclude} \
--exclude-where {params.exclude_where}\
--min-length {params.min_length} \
--output {output.sequences} 2>&1 | tee {log}
"""
rule excluded_sequences:
message:
"""
Generating fasta file of excluded sequences
"""
input:
sequences = rules.download.output.sequences,
metadata = rules.download.output.metadata,
include = config["files"]["exclude"]
output:
sequences = "results/excluded.fasta"
log:
"logs/excluded.txt"
conda: config["conda_environment"]
shell:
"""
augur filter \
--sequences {input.sequences} \
--metadata {input.metadata} \
--min-length 50000 \
--include {input.include} \
--output {output.sequences} 2>&1 | tee {log}
"""
rule align_excluded:
message:
"""
Aligning excluded sequences to {input.reference}
- gaps relative to reference are considered real
"""
input:
sequences = rules.excluded_sequences.output.sequences,
reference = config["files"]["reference"]
output:
alignment = "results/excluded_alignment.fasta"
log:
"logs/align_excluded.txt"
threads: 2
conda: config["conda_environment"]
shell:
"""
augur align \
--sequences {input.sequences} \
--reference-sequence {input.reference} \
--output {output.alignment} \
--nthreads {threads} \
--remove-reference 2>&1 | tee {log}
"""
rule diagnose_excluded:
message: "Scanning excluded sequences {input.alignment} for problematic sequences"
input:
alignment = rules.align_excluded.output.alignment,
metadata = rules.download.output.metadata,
reference = config["files"]["reference"]
output:
diagnostics = "results/excluded-sequence-diagnostics.tsv",
flagged = "results/excluded-flagged-sequences.tsv",
to_exclude = "results/check_exclusion.txt"
log:
"logs/diagnose-excluded.txt"
params:
mask_from_beginning = config["mask"]["mask_from_beginning"],
mask_from_end = config["mask"]["mask_from_end"]
conda: config["conda_environment"]
shell:
"""
python3 scripts/diagnostic.py \
--alignment {input.alignment} \
--metadata {input.metadata} \
--reference {input.reference} \
--mask-from-beginning {params.mask_from_beginning} \
--mask-from-end {params.mask_from_end} \
--output-flagged {output.flagged} \
--output-diagnostics {output.diagnostics} \
--output-exclusion-list {output.to_exclude} 2>&1 | tee {log}
"""
checkpoint partition_sequences:
input:
sequences = rules.filter.output.sequences
output:
split_sequences = directory("results/split_sequences/")
log:
"logs/partition_sequences.txt"
params:
sequences_per_group = config["partition_sequences"]["sequences_per_group"]
conda: config["conda_environment"]
shell:
"""
python3 scripts/partition-sequences.py \
--sequences {input.sequences} \
--sequences-per-group {params.sequences_per_group} \
--output-dir {output.split_sequences} 2>&1 | tee {log}
"""
rule align:
message:
"""
Aligning sequences to {input.reference}
- gaps relative to reference are considered real
Cluster: {wildcards.cluster}
"""
input:
sequences = "results/split_sequences/{cluster}.fasta",
reference = config["files"]["reference"]
output:
alignment = "results/split_alignments/{cluster}.fasta"
log:
"logs/align_{cluster}.txt"
benchmark:
"benchmarks/align_{cluster}.txt"
threads: 2
conda: config["conda_environment"]
shell:
"""
augur align \
--sequences {input.sequences} \
--reference-sequence {input.reference} \
--output {output.alignment} \
--nthreads {threads} \
--remove-reference 2>&1 | tee {log}
"""
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"
log:
"logs/aggregate_alignments.txt"
conda: config["conda_environment"]
shell:
"""
cat {input.alignments} > {output.alignment} 2> {log}
"""
rule diagnostic:
message: "Scanning aligned sequences {input.alignment} for problematic sequences"
input:
alignment = rules.aggregate_alignments.output.alignment,
metadata = rules.download.output.metadata,
reference = config["files"]["reference"]
output:
diagnostics = "results/sequence-diagnostics.tsv",
flagged = "results/flagged-sequences.tsv",
to_exclude = "results/to-exclude.txt"
log:
"logs/diagnostics.txt"
params:
mask_from_beginning = config["mask"]["mask_from_beginning"],
mask_from_end = config["mask"]["mask_from_end"]
conda: config["conda_environment"]
shell:
"""
python3 scripts/diagnostic.py \
--alignment {input.alignment} \
--metadata {input.metadata} \
--reference {input.reference} \
--mask-from-beginning {params.mask_from_beginning} \
--mask-from-end {params.mask_from_end} \
--output-flagged {output.flagged} \
--output-diagnostics {output.diagnostics} \
--output-exclusion-list {output.to_exclude} 2>&1 | tee {log}
"""
rule refilter:
message:
"""
excluding sequences flagged in the diagnostic step in file {input.exclude}
"""
input:
sequences = rules.aggregate_alignments.output.alignment,
metadata = rules.download.output.metadata,
exclude = rules.diagnostic.output.to_exclude
output:
sequences = "results/aligned-filtered.fasta"
log:
"logs/refiltered.txt"
conda: config["conda_environment"]
shell:
"""
augur filter \
--sequences {input.sequences} \
--metadata {input.metadata} \
--exclude {input.exclude} \
--output {output.sequences} 2>&1 | tee {log}
"""
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.refilter.output.sequences
output:
alignment = "results/masked.fasta"
log:
"logs/mask.txt"
params:
mask_from_beginning = config["mask"]["mask_from_beginning"],
mask_from_end = config["mask"]["mask_from_end"],
mask_sites = config["mask"]["mask_sites"]
conda: config["conda_environment"]
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} \
--mask-terminal-gaps \
--output {output.alignment} 2>&1 | tee {log}
"""
def _get_subsampling_settings(wildcards):
# Allow users to override default subsampling with their own settings keyed
# by location type and name. For example, "region_europe" or
# "country_iceland". Otherwise, default to settings for the location type.
subsampling_scheme = _get_subsampling_scheme_by_build_name(wildcards.build_name)
subsampling_settings = config["subsampling"][subsampling_scheme]
if hasattr(wildcards, "subsample"):
subsampling_settings = subsampling_settings[wildcards.subsample]
# If users have supplied both `max_sequences` and `seq_per_group`, we
# throw an error instead of assuming the user prefers one setting over
# another by default.
if subsampling_settings.get("max_sequences") and subsampling_settings.get("seq_per_group"):
raise Exception(f"The subsampling scheme '{subsampling_scheme}' for build '{wildcards.build_name}' defines both `max_sequences` and `seq_per_group`, but these arguments are mutually exclusive. If you didn't define both of these settings, this conflict could be caused by using the same subsampling scheme name as a default scheme. In this case, rename your subsampling scheme, '{subsampling_scheme}', to a unique name (e.g., 'custom_{subsampling_scheme}') and run the workflow again.")
# If users have supplied neither `max_sequences` nor `seq_per_group`, we
# throw an error because the subsampling rule will still group by one or
# more fields and the lack of limits on this grouping could produce
# unexpected behavior.
if not subsampling_settings.get("max_sequences") and not subsampling_settings.get("seq_per_group"):
raise Exception(f"The subsampling scheme '{subsampling_scheme}' for build '{wildcards.build_name}' must define `max_sequences` or `seq_per_group`.")
return subsampling_settings
def get_priorities(wildcards):
subsampling_settings = _get_subsampling_settings(wildcards)
if "priorities" in subsampling_settings and subsampling_settings["priorities"]["type"] == "proximity":
return f"results/{wildcards.build_name}/proximity_{subsampling_settings['priorities']['focus']}.tsv"
else:
# TODO: find a way to make the list of input files depend on config
return config["files"]["include"]
def get_priority_argument(wildcards):
subsampling_settings = _get_subsampling_settings(wildcards)
if "priorities" in subsampling_settings and subsampling_settings["priorities"]["type"] == "proximity":
return "--priority " + get_priorities(wildcards)
else:
return ""
def _get_specific_subsampling_setting(setting, optional=False):
def _get_setting(wildcards):
if optional:
value = _get_subsampling_settings(wildcards).get(setting, "")
else:
value = _get_subsampling_settings(wildcards)[setting]
if isinstance(value, str):
# Load build attributes including geographic details about the
# build's region, country, division, etc. as needed for subsampling.
build = config["builds"][wildcards.build_name]
value = value.format(**build)
elif value is not None:
# If is 'seq_per_group' or 'max_sequences' build subsampling setting,
# need to return the 'argument' for augur
if setting == 'seq_per_group':
value = f"--sequences-per-group {value}"
elif setting == 'max_sequences':
value = f"--subsample-max-sequences {value}"
return value
else:
value = ""
# Check format strings that haven't been resolved.
if re.search(r'\{.+\}', value):
raise Exception(f"The parameters for the subsampling scheme '{wildcards.subsample}' of build '{wildcards.build_name}' reference build attributes that are not defined in the configuration file: '{value}'. Add these build attributes to the appropriate configuration file and try again.")
return value
return _get_setting
rule subsample:
message:
"""
Subsample all sequences by '{wildcards.subsample}' scheme for build '{wildcards.build_name}' with the following parameters:
- group by: {params.group_by}
- sequences per group: {params.sequences_per_group}
- subsample max sequences: {params.subsample_max_sequences}
- min-date: {params.min_date}
- max-date: {params.max_date}
- exclude: {params.exclude_argument}
- include: {params.include_argument}
- query: {params.query_argument}
- priority: {params.priority_argument}
"""
input:
sequences = rules.mask.output.alignment,
metadata = rules.download.output.metadata,
include = config["files"]["include"],
priorities = get_priorities
output:
sequences = "results/{build_name}/sample-{subsample}.fasta"
log:
"logs/subsample_{build_name}_{subsample}.txt"
params:
group_by = _get_specific_subsampling_setting("group_by"),
sequences_per_group = _get_specific_subsampling_setting("seq_per_group", optional=True),
subsample_max_sequences = _get_specific_subsampling_setting("max_sequences", optional=True),
exclude_argument = _get_specific_subsampling_setting("exclude", optional=True),
include_argument = _get_specific_subsampling_setting("include", optional=True),
query_argument = _get_specific_subsampling_setting("query", optional=True),
min_date = _get_specific_subsampling_setting("min_date", optional=True),
max_date = _get_specific_subsampling_setting("max_date", optional=True),
priority_argument = get_priority_argument
conda: config["conda_environment"]
shell:
"""
augur filter \
--sequences {input.sequences} \
--metadata {input.metadata} \
--include {input.include} \
{params.min_date} \
{params.max_date} \
{params.exclude_argument} \
{params.include_argument} \
{params.query_argument} \
{params.priority_argument} \
--group-by {params.group_by} \
{params.sequences_per_group} \
{params.subsample_max_sequences} \
--output {output.sequences} 2>&1 | tee {log}
"""
rule proximity_score:
message:
"""
determine priority for inclusion in as phylogenetic context by
genetic similiarity to sequences in focal set for build '{wildcards.build_name}'.
"""
input:
alignment = rules.mask.output.alignment,
metadata = rules.download.output.metadata,
reference = config["files"]["reference"],
focal_alignment = "results/{build_name}/sample-{focus}.fasta"
output:
priorities = "results/{build_name}/proximity_{focus}.tsv"
log:
"logs/subsampling_priorities_{build_name}_{focus}.txt"
resources:
mem_mb = 4000
conda: config["conda_environment"]
shell:
"""
python3 scripts/priorities.py --alignment {input.alignment} \
--metadata {input.metadata} \
--reference {input.reference} \
--focal-alignment {input.focal_alignment} \
--output {output.priorities} 2>&1 | tee {log}
"""
def _get_subsampled_files(wildcards):
subsampling_settings = _get_subsampling_settings(wildcards)
return [
f"results/{wildcards.build_name}/sample-{subsample}.fasta"
for subsample in subsampling_settings
]
rule combine_samples:
message:
"""
Combine and deduplicate FASTAs
"""
input:
_get_subsampled_files
output:
alignment = "results/{build_name}/subsampled_alignment.fasta"
log:
"logs/subsample_regions_{build_name}.txt"
conda: config["conda_environment"]
shell:
"""
python3 scripts/combine-and-dedup-fastas.py \
--input {input} \
--output {output} 2>&1 | tee {log}
"""
# TODO: This will probably not work for build names like "country_usa" where we need to know the country is "USA".
rule adjust_metadata_regions:
message:
"""
Adjusting metadata for build '{wildcards.build_name}'
"""
input:
metadata = rules.download.output.metadata
output:
metadata = "results/{build_name}/metadata_adjusted.tsv"
params:
region = lambda wildcards: config["builds"][wildcards.build_name]["region"]
log:
"logs/adjust_metadata_regions_{build_name}.txt"
conda: config["conda_environment"]
shell:
"""
python3 scripts/adjust_regional_meta.py \
--region {params.region:q} \
--metadata {input.metadata} \
--output {output.metadata} 2>&1 | tee {log}
"""
rule tree:
message: "Building tree"
input:
alignment = rules.combine_samples.output.alignment
output:
tree = "results/{build_name}/tree_raw.nwk"
params:
args = lambda w: config["tree"].get("tree-builder-args","") if "tree" in config else ""
log:
"logs/tree_{build_name}.txt"
benchmark:
"benchmarks/tree_{build_name}.txt"
threads: 8
resources:
# Multiple sequence alignments can use up to 40 times their disk size in
# memory, especially for larger alignments.
# Note that Snakemake >5.10.0 supports input.size_mb to avoid converting from bytes to MB.
mem_mb=lambda wildcards, input: 40 * int(input.size / 1024 / 1024)
conda: config["conda_environment"]
shell:
"""
augur tree \
--alignment {input.alignment} \
--tree-builder-args {params.args} \
--output {output.tree} \
--nthreads {threads} 2>&1 | tee {log}
"""
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.combine_samples.output.alignment,
metadata = _get_metadata_by_wildcards
output:
tree = "results/{build_name}/tree.nwk",
node_data = "results/{build_name}/branch_lengths.json"
log:
"logs/refine_{build_name}.txt"
benchmark:
"benchmarks/refine_{build_name}.txt"
threads: 1
resources:
# Multiple sequence alignments can use up to 15 times their disk size in
# memory.
# Note that Snakemake >5.10.0 supports input.size_mb to avoid converting from bytes to MB.
mem_mb=lambda wildcards, input: 15 * int(input.size / 1024 / 1024)
params:
root = config["refine"]["root"],
clock_rate = config["refine"]["clock_rate"],
clock_std_dev = config["refine"]["clock_std_dev"],
coalescent = config["refine"]["coalescent"],
date_inference = config["refine"]["date_inference"],
divergence_unit = config["refine"]["divergence_unit"],
clock_filter_iqd = config["refine"]["clock_filter_iqd"],
keep_polytomies = "--keep-polytomies" if config["refine"].get("keep_polytomies", False) else "",
timetree = "" if config["refine"].get("no_timetree", False) else "--timetree"
conda: config["conda_environment"]
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} \
{params.timetree} \
{params.keep_polytomies} \
--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} 2>&1 | tee {log}
"""
rule ancestral:
message:
"""
Reconstructing ancestral sequences and mutations
- inferring ambiguous mutations
"""
input:
tree = rules.refine.output.tree,
alignment = rules.combine_samples.output.alignment
output:
node_data = "results/{build_name}/nt_muts.json"
log:
"logs/ancestral_{build_name}.txt"
params:
inference = config["ancestral"]["inference"]
conda: config["conda_environment"]
shell:
"""
augur ancestral \
--tree {input.tree} \
--alignment {input.alignment} \
--output-node-data {output.node_data} \
--inference {params.inference} \
--infer-ambiguous 2>&1 | tee {log}
"""
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/{build_name}/haplotype_status.json"
log:
"logs/haplotype_status_{build_name}.txt"
params:
reference_node_name = config["reference_node_name"]
conda: config["conda_environment"]
shell:
"""
python3 scripts/annotate-haplotype-status.py \
--ancestral-sequences {input.nt_muts} \
--reference-node-name {params.reference_node_name:q} \
--output {output.node_data} 2>&1 | tee {log}
"""
rule translate:
message: "Translating amino acid sequences"
input:
tree = rules.refine.output.tree,
node_data = rules.ancestral.output.node_data,
reference = config["files"]["reference"]
output:
node_data = "results/{build_name}/aa_muts.json"
log:
"logs/translate_{build_name}.txt"
conda: config["conda_environment"]
shell:
"""
augur translate \
--tree {input.tree} \
--ancestral-sequences {input.node_data} \
--reference-sequence {input.reference} \
--output-node-data {output.node_data} 2>&1 | tee {log}
"""
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 = rules.refine.output.tree,
metadata = _get_metadata_by_wildcards
output:
node_data = "results/{build_name}/traits.json"
log:
"logs/traits_{build_name}.txt"
params:
columns = _get_trait_columns_by_wildcards,
sampling_bias_correction = _get_sampling_bias_correction_for_wildcards
conda: config["conda_environment"]
shell:
"""
augur traits \
--tree {input.tree} \
--metadata {input.metadata} \
--output {output.node_data} \
--columns {params.columns} \
--confidence \
--sampling-bias-correction {params.sampling_bias_correction} 2>&1 | tee {log}
"""
def _get_clade_files(wildcards):
if "subclades" in config["builds"][wildcards.build_name]:
return [config["files"]["clades"], config["builds"][wildcards.build_name]["subclades"]]
else:
return config["files"]["clades"]
rule clade_files:
input:
clade_files = _get_clade_files
output:
"results/{build_name}/clades.tsv"
shell:
'''
cat {input.clade_files} > {output}
'''
rule clades:
message: "Adding internal clade labels"
input:
tree = rules.refine.output.tree,
aa_muts = rules.translate.output.node_data,
nuc_muts = rules.ancestral.output.node_data,
clades = rules.clade_files.output
output:
clade_data = "results/{build_name}/clades.json"
log:
"logs/clades_{build_name}.txt"
conda: config["conda_environment"]
shell:
"""
augur clades --tree {input.tree} \
--mutations {input.nuc_muts} {input.aa_muts} \
--clades {input.clades} \
--output-node-data {output.clade_data} 2>&1 | tee {log}
"""
rule pangolin:
message: "Adding internal clade labels"
input:
tree = rules.refine.output.tree,
output:
clade_data = "results/{build_name}/pangolin.json"
log:
"logs/pangolin_{build_name}.txt"
conda: config["conda_environment"]
shell:
"""
python3 scripts/add_pangolin_lineages.py \
--tree {input.tree} \
--output {output.clade_data}
"""
rule legacy_clades:
message: "Adding internal clade labels"
input:
tree = rules.refine.output.tree,
aa_muts = rules.translate.output.node_data,
nuc_muts = rules.ancestral.output.node_data,
clades = config["files"]["legacy_clades"]
output:
clade_data = "results/{build_name}/temp_legacy_clades.json"
log:
"logs/legacy_clades_{build_name}.txt"
conda: config["conda_environment"]
shell:
"""
augur clades --tree {input.tree} \
--mutations {input.nuc_muts} {input.aa_muts} \
--clades {input.clades} \
--output-node-data {output.clade_data} 2>&1 | tee {log}
"""
rule rename_legacy_clades:
input:
node_data = rules.legacy_clades.output.clade_data
output:
clade_data = "results/{build_name}/legacy_clades.json"
run:
import json
with open(input.node_data, 'r', encoding='utf-8') as fh:
d = json.load(fh)
new_data = {}
for k,v in d['nodes'].items():
if "clade_membership" in v:
new_data[k] = {"legacy_clade_membership": v["clade_membership"]}
with open(output.clade_data, "w") as fh:
json.dump({"nodes":new_data}, fh)
rule subclades:
message: "Adding internal clade labels"
input:
tree = rules.refine.output.tree,
aa_muts = rules.translate.output.node_data,
nuc_muts = rules.ancestral.output.node_data,
subclades = config["files"]["subclades"],
clades = config["files"]["clades"]
output:
clade_data = "results/{build_name}/temp_subclades.json"
params:
clade_file = "results/{build_name}/temp_subclades.tsv"
log:
"logs/subclades_{build_name}.txt"
conda: config["conda_environment"]
shell:
"""
cat {input.clades} {input.subclades} > {params.clade_file} && \
augur clades --tree {input.tree} \
--mutations {input.nuc_muts} {input.aa_muts} \
--clades {params.clade_file} \
--output-node-data {output.clade_data} 2>&1 | tee {log}
"""
rule rename_subclades:
input:
node_data = rules.subclades.output.clade_data
output:
clade_data = "results/{build_name}/subclades.json"
run:
import json
with open(input.node_data, 'r', encoding='utf-8') as fh:
d = json.load(fh)
new_data = {}
for k,v in d['nodes'].items():
if "clade_membership" in v:
new_data[k] = {"subclade_membership": v["clade_membership"]}
with open(output.clade_data, "w") as fh:
json.dump({"nodes":new_data}, fh)
rule colors:
message: "Constructing colors file"
input:
ordering = config["files"]["ordering"],
color_schemes = config["files"]["color_schemes"],
metadata = _get_metadata_by_wildcards
output:
colors = "results/{build_name}/colors.tsv"
log:
"logs/colors_{build_name}.txt"
conda: config["conda_environment"]
shell:
"""
python3 scripts/assign-colors.py \
--ordering {input.ordering} \
--color-schemes {input.color_schemes} \
--output {output.colors} \
--metadata {input.metadata} 2>&1 | tee {log}
"""
rule recency:
message: "Use metadata on submission date to construct submission recency field"
input:
metadata = _get_metadata_by_wildcards
output:
node_data = "results/{build_name}/recency.json"
log:
"logs/recency_{build_name}.txt"
conda: config["conda_environment"]
shell:
"""
python3 scripts/construct-recency-from-submission-date.py \
--metadata {input.metadata} \
--output {output} 2>&1 | tee {log}
"""
rule tip_frequencies:
message: "Estimating censored KDE frequencies for tips"
input:
tree = rules.refine.output.tree,
metadata = _get_metadata_by_wildcards
output:
tip_frequencies_json = "results/{build_name}/tip-frequencies.json"
log:
"logs/tip_frequencies_{build_name}.txt"
params:
min_date = config["frequencies"]["min_date"],
pivot_interval = config["frequencies"]["pivot_interval"],
narrow_bandwidth = config["frequencies"]["narrow_bandwidth"],
proportion_wide = config["frequencies"]["proportion_wide"]
conda: config["conda_environment"]
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} 2>&1 | tee {log}
"""
rule nucleotide_mutation_frequencies:
message: "Estimate nucleotide mutation frequencies"
input:
alignment = rules.combine_samples.output.alignment,
metadata = _get_metadata_by_wildcards
output:
frequencies = "results/{build_name}/nucleotide_mutation_frequencies.json"
log:
"logs/nucleotide_mutation_frequencies_{build_name}.txt"
params:
min_date = config["frequencies"]["min_date"],
minimal_frequency = config["frequencies"]["minimal_frequency"],
pivot_interval = config["frequencies"]["pivot_interval"],
stiffness = config["frequencies"]["stiffness"],
inertia = config["frequencies"]["inertia"]
conda: config["conda_environment"]
shell:
"""
augur frequencies \
--method diffusion \
--alignments {input.alignment} \
--gene-names nuc \
--metadata {input.metadata} \
--min-date {params.min_date} \
--minimal-frequency {params.minimal_frequency} \
--pivot-interval {params.pivot_interval} \
--stiffness {params.stiffness} \
--inertia {params.inertia} \
--output {output.frequencies} 2>&1 | tee {log}
"""
def export_title(wildcards):
# TODO: maybe we could replace this with a config entry for full/human-readable build name?
location_name = wildcards.build_name
# If specified in config file generally, or in a config file build
if "title" in config["builds"][location_name]:
return config["builds"][location_name]["title"]
elif "title" in config:
return config["title"]
# Else return an auto-generated title
if not location_name:
return "Genomic epidemiology of novel coronavirus"
elif location_name == "global":
return "Genomic epidemiology of novel coronavirus - Global subsampling"
else:
location_title = location_name.replace("-", " ").title()
return f"Genomic epidemiology of novel coronavirus - {location_title}-focused subsampling"
def _get_node_data_by_wildcards(wildcards):
"""Return a list of node data files to include for a given build's wildcards.
"""
# Define inputs shared by all builds.
wildcards_dict = dict(wildcards)
inputs = [
rules.refine.output.node_data,
rules.ancestral.output.node_data,
rules.translate.output.node_data,
rules.rename_legacy_clades.output.clade_data,
rules.rename_subclades.output.clade_data,
rules.clades.output.clade_data,
rules.recency.output.node_data,
rules.traits.output.node_data
]
# Convert input files from wildcard strings to real file names.
inputs = [input_file.format(**wildcards_dict) for input_file in inputs]
return inputs
rule export:
message: "Exporting data files for for auspice"
input:
tree = rules.refine.output.tree,
metadata = _get_metadata_by_wildcards,
node_data = _get_node_data_by_wildcards,
auspice_config = lambda w: config["builds"][w.build_name]["auspice_config"] if "auspice_config" in config["builds"][w.build_name] else config["files"]["auspice_config"],
colors = lambda w: config["builds"][w.build_name]["colors"] if "colors" in config["builds"][w.build_name] else ( config["files"]["colors"] if "colors" in config["files"] else rules.colors.output.colors.format(**w) ),
lat_longs = config["files"]["lat_longs"],
description = lambda w: config["builds"][w.build_name]["description"] if "description" in config["builds"][w.build_name] else config["files"]["description"]
output:
auspice_json = "results/{build_name}/ncov_with_accessions.json",
root_sequence_json = "results/{build_name}/ncov_with_accessions_root-sequence.json"
log:
"logs/export_{build_name}.txt"
params:
title = export_title
conda: config["conda_environment"]
shell:
"""
augur export v2 \
--tree {input.tree} \
--metadata {input.metadata} \
--node-data {input.node_data} \
--auspice-config {input.auspice_config} \
--include-root-sequence \
--colors {input.colors} \
--lat-longs {input.lat_longs} \
--title {params.title:q} \
--description {input.description} \
--output {output.auspice_json} 2>&1 | tee {log}
"""
rule incorporate_travel_history:
message: "Adjusting main auspice JSON to take into account travel history"
input:
auspice_json = rules.export.output.auspice_json,
colors = lambda w: config["builds"][w.build_name]["colors"] if "colors" in config["builds"][w.build_name] else ( config["files"]["colors"] if "colors" in config["files"] else rules.colors.output.colors.format(**w) ),
lat_longs = config["files"]["lat_longs"]
params:
sampling = _get_sampling_trait_for_wildcards,
exposure = _get_exposure_trait_for_wildcards
output:
auspice_json = "results/{build_name}/ncov_with_accessions_and_travel_branches.json"
log:
"logs/incorporate_travel_history_{build_name}.txt"
conda: config["conda_environment"]
shell:
"""
python3 ./scripts/modify-tree-according-to-exposure.py \
--input {input.auspice_json} \
--colors {input.colors} \
--lat-longs {input.lat_longs} \
--sampling {params.sampling} \
--exposure {params.exposure} \
--output {output.auspice_json} 2>&1 | tee {log}
"""
rule finalize:
message: "Remove extraneous colorings for main build and move frequencies"
input:
auspice_json = rules.incorporate_travel_history.output.auspice_json,
frequencies = rules.tip_frequencies.output.tip_frequencies_json,
root_sequence_json = rules.export.output.root_sequence_json
output:
auspice_json = "auspice/ncov_{build_name}.json",
tip_frequency_json = "auspice/ncov_{build_name}_tip-frequencies.json",
root_sequence_json = "auspice/ncov_{build_name}_root-sequence.json"
log:
"logs/fix_colorings_{build_name}.txt"
conda: config["conda_environment"]
shell:
"""
python3 scripts/fix-colorings.py \
--input {input.auspice_json} \
--output {output.auspice_json} 2>&1 | tee {log} &&