forked from nextstrain/seasonal-flu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile_countries
176 lines (151 loc) · 6.72 KB
/
Snakefile_countries
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
configfile: "config/config.json"
localrules: all_live, download_all, download_titers, download_sequences, rename_output
ruleorder: select_strains_countries > select_strains
ruleorder: titers_rename>download_titers
segments = ['ha', 'na']
lineages = ['h3n2', 'h1n1pdm', 'vic', 'yam']
resolutions = ['2y']
passages = ['cell']
centers = ['cdc']
assays = ['hi']
include: "Snakefile_base"
countries = {"netherlands":["Netherlands"], "NorthAmerica":["USA", "Canada"],
"BI":["Ireland", "United Kingdom"], "scandinavia":["Norway", "Sweden", "Denmark"],
"east-asia":["China", "Japan", "Korea", "Hong Kong"],
"aus-nz":["Australia", "New Zealand"]}
def focal_vpm(v):
focal_vpm = {'6m':300, '2y':70, '3y':50, '6y':20, '12y':10, '60y':4}
return focal_vpm[v.resolution] if v.resolution in focal_vpm else 5
def other_vpm(v):
return vpm(v) - focal_vpm(v)
lineage_label = {"h3n2":"A/H3N2", "h1n1pdm":"A/H1N1pdm", "vic":"B/Vic", "yam":"B/Yam"}
def get_title(w):
if w.center=='netherlands':
return f"Influenza {lineage_label[w.lineage]} in the Netherlands -- using data from EMC & RIVM & GISAID"
elif w.center=='NorthAmerica':
return f"Influenza {lineage_label[w.lineage]} in NorthAmerica -- using data from US CDC & GISAID"
else:
return f"Real-time tracking of influenza {lineage_label[w.lineage]} evolution using data from GISAID"
def get_maintainers(w):
if w.center=='netherlands':
return "Adam Meijer and Richard Neher <http://rivm.nl/>"
else:
return "not specified"
def get_geo_resolution(w):
if w.center in ["NorthAmerica", "BI", "netherlands", "scandinavia", "east-asia", "aus-nz"]:
return "region country division"
else:
return "region country"
rule all_country:
input:
auspice_tree = expand("auspice_FBC/FluByCountry_{center}_{lineage}_{segment}_{resolution}.json",
center=countries, lineage=lineages, segment=segments, resolution=resolutions),
auspice_tip_frequencies = expand("auspice_FBC/FluByCountry_{center}_{lineage}_{segment}_{resolution}_tip-frequencies.json",
center=countries, lineage=lineages, segment=segments, resolution=resolutions)
rule titers_rename:
input:
titer_file = "data/who_{lineage}_{passage}_{assay}_titers.tsv"
output:
titer_file = "data/{center}_{lineage}_{passage}_{assay}_titers.tsv"
shell:
'''
cp {input.titer_file} {output.titer_file}
'''
# separate rule for interaction with fauna
rule download_all:
input:
titers = expand("data/{center}_{lineage}_{passage}_{assay}_titers.tsv",
center=["who"], lineage=lineages, passage=passages, assay=assays),
sequences = expand("data/{lineage}_{segment}.fasta", lineage=lineages, segment=segments)
def _get_focus_countries(wildcards):
return countries[wildcards.center]
def _get_node_data_for_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.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
]
# Only request a distance file for builds that have distance map
# 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:
input:
tree = rules.refine.output.tree,
metadata = rules.parse.output.metadata,
auspice_config = files.auspice_config,
node_data = _get_node_data_for_export,
lat_longs = "config/lat_longs.tsv"
output:
auspice_main = "auspice/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_tree.json"
#auspice_rs = "auspice/flu_{center}_{lineage}_{segment}_{resolution}_{passage}_{assay}_tree_root-sequence.json",
params:
title = get_title,
maintainers = get_maintainers,
geo = get_geo_resolution
shell:
"""
augur export v2 \
--tree {input.tree} \
--metadata {input.metadata} \
--title {params.title:q} \
--maintainers {params.maintainers:q} \
--geo-resolutions {params.geo} \
--lat-longs {input.lat_longs} \
--node-data {input.node_data} \
--auspice-config {input.auspice_config} \
--output {output.auspice_main} \
--minify-json
"""
rule rename_output:
input:
frequencies = "auspice/flu_{center}_{lineage}_{segment}_{resolution}_cell_hi_tip-frequencies.json",
tree = "auspice/flu_{center}_{lineage}_{segment}_{resolution}_cell_hi_tree.json"
#root_seq = "auspice/flu_{center}_{lineage}_{segment}_{resolution}_cell_hi_tree_root-sequence.json"
output:
tree = "auspice_FBC/FluByCountry_{center}_{lineage}_{segment}_{resolution}.json",
#root_seq = "auspice_FBC/FluByCountry_{center}_{lineage}_{segment}_{resolution}_root-sequence.json",
frequencies = "auspice_FBC/FluByCountry_{center}_{lineage}_{segment}_{resolution}_tip-frequencies.json"
shell:
'''
mv {input.tree} {output.tree} &
mv {input.frequencies} {output.frequencies}
'''
rule select_strains_countries:
input:
sequences = expand("results/filtered_{{lineage}}_{segment}_{{passage}}.fasta", segment=segments),
metadata = expand("results/metadata_{{lineage}}_{segment}.tsv", segment=segments),
titers = "data/who_{lineage}_{passage}_{assay}_titers.tsv",
include = files.references
output:
strains = "results/strains_{center}_{lineage}_{resolution}_{passage}_{assay}.txt",
params:
viruses_per_month = other_vpm,
extra_viruses_per_month = focal_vpm,
focus_countries = _get_focus_countries
shell:
"""
python3 scripts/select_strains.py \
--sequences {input.sequences} \
--metadata {input.metadata} \
--segments {segments} \
--focus-countries {params.focus_countries:q} \
--extra-viruses-per-month {params.extra_viruses_per_month} \
--include {input.include} \
--lineage {wildcards.lineage} \
--resolution {wildcards.resolution} \
--viruses-per-month {params.viruses_per_month} \
--titers {input.titers} \
--output {output.strains}
"""