forked from nextstrain/seasonal-flu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile_WHO
268 lines (237 loc) · 10.5 KB
/
Snakefile_WHO
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
configfile: "config/config.json"
passages = ['cell', 'egg']
assays = ['hi', 'fra']
centers = ['who', 'cdc', 'crick', 'niid', 'vidrl']
resolutions = ['2y', '6y']
lineages = ['h3n2' , 'h1n1pdm', 'vic', 'yam']
segments = ['ha', 'na']
include: "Snakefile_base"
localrules: all_builds, download_all, download_titers, download_sequences
def all_builds(w):
builds = []
for lineage in lineages:
assays = ['hi', 'fra'] if lineage=='h3n2' else ['hi']
for file_suffix in ['tree', 'meta', 'frequencies', 'titers', 'titer-tree-model', 'titer-sub-model', 'entropy', 'sequences']:
builds.extend(
expand("auspice-who/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_{f}.json",
center=centers, lineage=lineages, segment=segments,
resolution=resolutions, assay=assays, passage=passages, f=[file_suffix]))
return builds
def region_translations(w):
genes = gene_names(w)
return ["results/full-aaseq-%s_%s_%s_%s_%s.fasta"%(g, w.region, w.lineage, w.segment, w.resolution)
for g in genes]
rule all_who:
input:
all_builds
# separate rule for interaction with fauna
rule download_all:
input:
titers = expand("data/{center}_{lineage}_{passage}_{assay}_titers.tsv",
lineage=lineages, center=centers, assay=assays, passage=passages),
sequences = expand("data/{lineage}_{segment}.fasta", lineage=lineages, segment=segments)
for seg, genes in genes_to_translate.items():
rule:
input:
metadata = rules.parse.output.metadata,
sequences = rules.parse.output.sequences,
exclude = files.outliers,
reference = files.reference
params:
genes=genes,
region="{region}"
output:
alignments = expand("results/full-aaseq-{gene}_{{region}}_{{lineage}}_{{segment}}_{{resolution}}.fasta",
gene=genes)
shell:
"""
python3 scripts/full_region_alignments.py --sequences {input.sequences}\
--metadata {input.metadata} \
--exclude {input.exclude} \
--genes {params.genes} \
--region {params.region:q} \
--resolution {wildcards.resolution} \
--reference {input.reference} \
--output {output.alignments:q}
"""
rule complete_mutation_frequencies_by_region:
input:
metadata = rules.parse.output.metadata,
alignment = region_translations
params:
genes = gene_names,
min_date = min_date,
max_date = max_date,
min_freq = 0.003,
pivot_interval = pivot_interval,
stiffness = 20,
inertia = 0.2
output:
mut_freq = "results/mutation_frequencies_{region}_{lineage}_{segment}_{resolution}.json"
shell:
"""
augur frequencies --method diffusion \
--alignments {input.alignment:q} \
--metadata {input.metadata} \
--gene-names {params.genes} \
--pivot-interval {params.pivot_interval} \
--stiffness {params.stiffness} \
--inertia {params.inertia} \
--ignore-char X \
--min-date {params.min_date} \
--max-date {params.max_date} \
--minimal-frequency {params.min_freq} \
--output {output.mut_freq:q}
"""
rule global_mutation_frequencies:
input:
frequencies = expand("results/mutation_frequencies_{region}_{{lineage}}_{{segment}}_{{resolution}}.json",
region = frequency_regions),
tree_freq = rules.tree_frequencies.output
params:
regions = frequency_regions
output:
auspice="auspice-who/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_frequencies.json",
augur="results/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_frequencies.json"
shell:
'''
python3 scripts/global_frequencies.py --region-frequencies {input.frequencies:q} \
--tree-frequencies {input.tree_freq} \
--regions {params.regions:q} \
--output-auspice {output.auspice} \
--output-augur {output.augur}
'''
rule scores:
input:
metadata = rules.parse.output.metadata,
tree = rules.refine.output.tree
output:
scores = "results/scores_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}.json"
shell:
"""
python3 scripts/scores.py --metadata {input.metadata} \
--tree {input.tree} \
--output {output}
"""
rule export_titers:
input:
sub = rules.titers_sub.output.titers_model,
tree = rules.titers_tree.output.titers_model
output:
raw = "auspice-who/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_titers.json",
tree = "auspice-who/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_titer-tree-model.json",
sub = "auspice-who/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_titer-sub-model.json",
run:
import json
with open(input.sub) as fh:
sub = json.load(fh)
with open(output.sub, 'wt') as sub_file:
json.dump({'avidity': sub['avidity'],
'potency': sub['potency'],
'substitution': sub['substitution']},
sub_file, indent=1)
with open(output.raw, 'wt') as raw_file:
json.dump(sub['titers'], raw_file, indent=1)
with open(input.tree) as fh:
tree = json.load(fh)
with open(output.tree, 'wt') as tree_file:
json.dump({'avidity': tree['avidity'],
'potency': tree['potency'],
'dTiter': {k:v['dTiter'] for k,v in tree['nodes'].items()}},
tree_file, indent=1)
rule export_entropy:
input:
aln = rules.align.output.alignment,
reference = files.reference,
params:
genes = gene_names
output:
"auspice-who/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_entropy.json",
shell:
'''
python3 scripts/entropy.py --alignment {input.aln} \
--genes {params.genes} \
--reference-sequence {input.reference} \
--output {output}
'''
rule export_sequence_json:
input:
aln = rules.ancestral.output.node_data,
tree = rules.refine.output.tree,
aa_seqs = translations
params:
genes = gene_names
output:
"auspice-who/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_sequences.json",
shell:
'''
python3 scripts/sequence_export.py --alignment {input.aln} \
--genes {params.genes} \
--tree {input.tree} \
--translations {input.aa_seqs} \
--output {output}
'''
def _get_node_data_for_report_export(wildcards):
"""Return a list of node data files to include for a given build's wildcards.
"""
# Define inputs shared by all builds.
inputs = [
rules.annotate_recency_of_submissions.output.node_data,
rules.refine.output.node_data,
rules.ancestral.output.node_data,
rules.translate.output.node_data,
rules.titers_tree.output.titers_model,
rules.titers_sub.output.titers_model,
rules.clades.output.clades,
rules.traits.output.node_data,
rules.lbi.output.lbi,
rules.scores.output.scores,
]
# glycosilation only makes sense for surface proteins
if wildcards.segment in ['ha', 'na']:
inputs.append(rules.glyc.output.glyc)
# Only request a distance file for builds that have mask configurations
# defined.
if _get_build_distance_map_config(wildcards) is not None:
inputs.append(rules.distances.output.distances)
# Convert input files from wildcard strings to real file names.
inputs = [input_file.format(**wildcards) for input_file in inputs]
return inputs
rule export_who:
input:
tree = rules.refine.output.tree,
metadata = rules.parse.output.metadata,
auspice_config = "config/auspice_config_who_{lineage}.json",
node_data = _get_node_data_for_report_export,
colors = files.colors,
output:
tree = "auspice-who/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_tree.json",
meta = "auspice-who/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_meta.json",
shell:
"""
augur export v1 \
--tree {input.tree} \
--metadata {input.metadata} \
--node-data {input.node_data} \
--auspice-config {input.auspice_config} \
--colors {input.colors} \
--output-tree {output.tree} \
--output-meta {output.meta} \
--minify-json
"""
rule targets:
input:
tree = "auspice-who/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_tree.json",
meta = "auspice-who/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_meta.json",
entropy = "auspice-who/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_entropy.json",
frequencies = "auspice-who/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_frequencies.json",
sequences = "auspice-who/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_sequences.json",
titer_sub_model = "auspice-who/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_titer-sub-model.json",
titer_tree_model = "auspice-who/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_titer-tree-model.json",
titers = "auspice-who/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_titers.json"
output:
target = "targets/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}"
shell:
'''
touch {output.target}
'''