forked from nextstrain/ncov
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
370 lines (350 loc) · 11.9 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
def get_todays_date():
from datetime import datetime
date = datetime.today().strftime('%Y-%m-%d')
return date
rule all:
input:
auspice_json = "auspice/ncov.json",
dated_auspice_json = expand("auspice/ncov_{date}.json", date=get_todays_date()),
auspice_json_gisaid = "auspice/ncov_gisaid.json",
auspice_json_zh = "auspice/ncov_zh.json"
rule files:
params:
input_fasta = "data/ncov.fasta",
include = "config/include.txt",
exclude = "config/exclude.txt",
reference = "config/reference.gb",
outgroup = "config/outgroup.fasta",
auspice_config = "config/auspice_config.json",
auspice_config_gisaid = "config/auspice_config_gisaid.json",
auspice_config_zh = "config/auspice_config_zh.json",
colors = "config/colors.tsv",
lat_longs = "config/lat_longs.tsv",
description = "config/description.md",
description_zh = "config/description_zh.md"
files = rules.files.params
rule download:
message: "Downloading sequences from fauna"
output:
sequences = "data/ncov.fasta"
params:
fasta_fields = "strain virus accession collection_date region country location locus host originating_lab submitting_lab authors url title journal puburls"
shell:
"""
python3 ../fauna/vdb/download.py \
--database vdb \
--virus ncov \
--fasta_fields {params.fasta_fields} \
--resolve_method choose_genbank \
--path $(dirname {output.sequences}) \
--fstem $(basename {output.sequences} .fasta)
sed -i -e 's/BetaCoV\///g' data/ncov.fasta
sed -i -e 's/2019-nCoV_//g' data/ncov.fasta
sed -i -e 's/2019-nCoV\///g' data/ncov.fasta
"""
rule parse:
message: "Parsing fasta into sequences and metadata"
input:
sequences = rules.download.output.sequences
output:
sequences = "data/sequences.fasta",
metadata = "data/metadata.tsv"
params:
fasta_fields = "strain virus accession date region country location segment host originating_lab submitting_lab authors url title",
prettify_fields = "region country location"
shell:
"""
augur parse \
--sequences {input.sequences} \
--output-sequences {output.sequences} \
--output-metadata {output.metadata} \
--fields {params.fasta_fields} \
--prettify-fields {params.prettify_fields}
"""
rule filter:
message:
"""
Filtering to
- {params.sequences_per_group} sequence(s) per {params.group_by!s}
- excluding strains in {input.exclude}
- minimum genome length of {params.min_length}
"""
input:
sequences = rules.parse.output.sequences,
metadata = rules.parse.output.metadata,
include = files.include,
exclude = files.exclude
output:
sequences = "results/filtered.fasta"
params:
group_by = "country",
sequences_per_group = 100,
min_length = 5000,
shell:
"""
augur filter \
--sequences {input.sequences} \
--metadata {input.metadata} \
--exclude {input.exclude} \
--include {input.include} \
--output {output.sequences} \
--group-by {params.group_by} \
--sequences-per-group {params.sequences_per_group} \
--min-length {params.min_length}
"""
rule align:
message:
"""
Aligning sequences to {input.reference}
- filling gaps with N
"""
input:
sequences = rules.filter.output.sequences,
reference = files.reference
output:
alignment = "results/aligned.fasta"
shell:
"""
augur align \
--sequences {input.sequences} \
--reference-sequence {input.reference} \
--output {output.alignment} \
--fill-gaps \
--remove-reference
"""
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.align.output.alignment
output:
alignment = "results/masked.fasta"
params:
mask_from_beginning = 130,
mask_from_end = 15,
mask_sites = 18529
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"
shell:
"""
augur tree \
--alignment {input.alignment} \
--output {output.tree}
"""
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.parse.output.metadata
output:
tree = "results/tree.nwk",
node_data = "results/branch_lengths.json"
params:
root = "Wuhan/WIV04/2019 Wuhan/WIV06/2019",
clock_rate = 0.00035,
clock_std_dev = 0.00015,
coalescent = "skyline",
date_inference = "marginal",
divergence_unit = "mutations"
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
"""
rule ancestral:
message: "Reconstructing ancestral sequences and mutations"
input:
tree = rules.refine.output.tree,
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}
"""
rule translate:
message: "Translating amino acid sequences"
input:
tree = rules.refine.output.tree,
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 export:
message: "Exporting data files for for auspice"
input:
tree = rules.refine.output.tree,
metadata = rules.parse.output.metadata,
branch_lengths = rules.refine.output.node_data,
nt_muts = rules.ancestral.output.node_data,
aa_muts = rules.translate.output.node_data,
auspice_config = files.auspice_config,
colors = files.colors,
lat_longs = files.lat_longs,
description = files.description
output:
auspice_json = "auspice/ncov.json"
shell:
"""
augur export v2 \
--tree {input.tree} \
--metadata {input.metadata} \
--node-data {input.branch_lengths} {input.nt_muts} {input.aa_muts} \
--auspice-config {input.auspice_config} \
--colors {input.colors} \
--lat-longs {input.lat_longs} \
--description {input.description} \
--output {output.auspice_json}
"""
rule gisaid_export:
message: "Exporting data files for for auspice"
input:
tree = rules.refine.output.tree,
metadata = rules.parse.output.metadata,
branch_lengths = rules.refine.output.node_data,
nt_muts = rules.ancestral.output.node_data,
aa_muts = rules.translate.output.node_data,
auspice_config = files.auspice_config_gisaid,
colors = files.colors,
lat_longs = files.lat_longs,
description = files.description
output:
auspice_json = "auspice/ncov_gisaid.json"
shell:
"""
augur export v2 \
--tree {input.tree} \
--metadata {input.metadata} \
--node-data {input.branch_lengths} {input.nt_muts} {input.aa_muts} \
--auspice-config {input.auspice_config} \
--colors {input.colors} \
--lat-longs {input.lat_longs} \
--description {input.description} \
--output {output.auspice_json}
"""
rule chinese_language_export:
message: "Exporting data files for for auspice"
input:
tree = rules.refine.output.tree,
metadata = rules.parse.output.metadata,
branch_lengths = rules.refine.output.node_data,
nt_muts = rules.ancestral.output.node_data,
aa_muts = rules.translate.output.node_data,
auspice_config = files.auspice_config_zh,
colors = files.colors,
lat_longs = files.lat_longs,
description = files.description_zh
output:
auspice_json = "auspice/ncov_zh.json"
shell:
"""
augur export v2 \
--tree {input.tree} \
--metadata {input.metadata} \
--node-data {input.branch_lengths} {input.nt_muts} {input.aa_muts} \
--auspice-config {input.auspice_config} \
--colors {input.colors} \
--lat-longs {input.lat_longs} \
--description {input.description} \
--output {output.auspice_json}
"""
rule dated_json:
message: "Copying dated Auspice JSON"
input:
auspice_json = rules.export.output.auspice_json
output:
dated_auspice_json = rules.all.input.dated_auspice_json
shell:
"""
cp {input.auspice_json} {output.dated_auspice_json}
"""
rule poisson_tmrca:
input:
tree = rules.refine.output.tree,
metadata = rules.parse.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 = [600, 3000, 15000],
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]}
"""
rule clean:
message: "Removing directories: {params}"
params:
"results ",
"auspice"
shell:
"rm -rfv {params}"