-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
310 lines (293 loc) · 9.22 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
rule all:
input:
auspice_json = "auspice/ncov.json",
tip_frequencies_json = "auspice/ncov_tip-frequencies.json"
rule files:
params:
auspice_config = "config/auspice_config.json",
clades = "config/clades.tsv",
color_schemes = "config/color_schemes.tsv",
description = "config/description.md",
lat_longs = "config/lat_longs.tsv",
reference = "config/reference.gb",
sequences = "data/sequences.fasta",
metadata = "data/metadata.tsv"
files = rules.files.params
rule clean_metadata:
message:
"""
Cleaning {input.metadata}:
- remove/fix non-AZ counties
"""
input:
metadata = files.metadata
output:
metadata = "results/metadata.tsv"
shell:
"""
python3 scripts/clean_metadata.py {input.metadata} {output.metadata}
"""
rule fix_colors:
message:
"""
Generate color file based on metadata of interest
- Color AZ County metadata
- Color Pangolin lineages
"""
input:
metadata = rules.clean_metadata.output.metadata,
color_schemes = files.color_schemes
output:
colors = "results/colors.tsv"
shell:
"""
python3 scripts/fix_colors.py {input.metadata} {input.color_schemes} {output.colors}
"""
checkpoint partition_sequences:
input:
sequences = files.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: 40
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: 40
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.clean_metadata.output.metadata
output:
tree = "results/tree.nwk",
node_data = "results/branch_lengths.json"
params:
root = "best",
clock_rate = 0.0008,
clock_std_dev = 0.0004,
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
- 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 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 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 recency:
message: "Use metadata on submission date to construct submission recency field"
input:
metadata = rules.clean_metadata.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.clean_metadata.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 auspice"
input:
tree = rules.refine.output.tree,
metadata = rules.clean_metadata.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 = rules.fix_colors.output.colors,
lat_longs = files.lat_longs,
description = files.description,
clades = rules.clades.output.clade_data,
recency = rules.recency.output
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} {input.clades} {input.recency} \
--auspice-config {input.auspice_config} \
--colors {input.colors} \
--lat-longs {input.lat_longs} \
--description {input.description} \
--minify-json \
--output {output.auspice_json}
"""