-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile_phylogeny
234 lines (215 loc) · 7.76 KB
/
Snakefile_phylogeny
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
import os
# To run locally:
# snakemake --snakefile Snakefile_phylogeny --keep-going --cores 4 --use-singularity --singularity-prefix ~/.singularity --singularity-args "--home ~"
# To visualise the pipeline
# snakemake --snakefile Snakefile_phylogeny --dag | dot -Tsvg > pipeline_phylogeny.svg
localrules: all
folder = os.path.abspath(config.get("folder", '..'))
data_dir = os.path.join(folder, 'data')
tree_types = ['raxmlng', 'iq']
os.makedirs('logs', exist_ok=True)
rule all:
input:
os.path.join(data_dir, 'trees', 'best_tree.pos.rooted_African.collapsed.nwk'),
rule get_ids_of_type:
'''
Extract sequence ids of interest.
'''
input:
tab = os.path.join(data_dir, 'metadata.combined.tab'),
output:
tab = os.path.join(data_dir, 'ids_{type,(African|Asian)}.txt')
params:
mem = 500,
name = 'ids_{type}',
col_value = '{type}',
col_name = 'type',
qos = 'fast'
threads: 1
singularity: "docker://evolbioinfo/python-evol:v3.6"
shell:
"""
python3 py/get_seq_ids.py --input_data {input.tab} --output_data {output.tab} \
--col_name "{params.col_name}" --col_value {params.col_value}
"""
rule partitioning_nt:
'''
Prepare nucleotide partitioning files.
'''
input:
ref = os.path.join(data_dir, 'ref', 'ZIKV.xml'),
output:
part = os.path.join(data_dir, 'partition_{tool}.{type}.txt'),
threads: 1
singularity: "docker://evolbioinfo/python-evol:v3.6"
params:
mem = 10000,
name = 'part',
tool = '{tool}',
type = '{type}'
shell:
"""
python3 py/dna_partitioner.py --input_xml {input.ref} --output_partitioning {output.part} \
--tool {params.tool} --level {params.type}
"""
rule raxml_nt:
'''
Reconstructs a tree with RAxML.
'''
input:
aln = os.path.join(data_dir, 'aln.fa'),
part = os.path.join(data_dir, 'partition_raxml.{type}.txt'),
output:
tree = os.path.join(data_dir, 'trees', 'raxmlng_tree.{type,(gene|type|pos)}.nwk'),
bs = os.path.join(data_dir, 'trees', 'raxmlng_tree.{type,(gene|type|pos)}.bootstrap_trees.nwk'),
log = os.path.join(data_dir, 'trees', 'raxmlng_tree.{type,(gene|type|pos)}.log'),
model = os.path.join(data_dir, 'trees', 'raxmlng_tree.{type,(gene|type|pos)}.model'),
threads: 24
singularity: "docker://evolbioinfo/raxml-ng:v0.9.0"
params:
mem = 100000,
name = 'raxml.{type}',
part = '{type}',
shell:
"""
cp {input.aln} {input.aln}.{params.part}
raxml-ng --all --model {input.part} --msa {input.aln}.{params.part} --msa-format FASTA --data-type DNA --tree pars{{5}} \
--seed 239 --threads {threads} --force --bs-trees 100 --bs-metric fbp
# mv {input.aln}.{params.part}.raxml.bestTree {output.tree}
mv {input.aln}.{params.part}.raxml.log {output.log}
mv {input.aln}.{params.part}.raxml.bestModel {output.model}
mv {input.aln}.{params.part}.raxml.support {output.tree}
mv {input.aln}.{params.part}.raxml.bootstraps {output.bs}
# rm -rf {input.aln}.{params.part}*
"""
rule iqtree_nt:
'''
Reconstructs a tree with IQTREE.
'''
input:
aln = os.path.join(data_dir, 'aln.fa'),
part = os.path.join(data_dir, 'partition_iq.{type}.txt'),
output:
tree = os.path.join(data_dir, 'trees', 'iq_tree.{type,(gene|type|pos)}.nwk'),
bs = os.path.join(data_dir, 'trees', 'iq_tree.{type,(gene|type|pos)}.bootstrap_trees.nwk'),
log = os.path.join(data_dir, 'trees', 'iq_tree.{type,(gene|type|pos)}.log'),
model = os.path.join(data_dir, 'trees', 'iq_tree.{type,(gene|type|pos)}.model'),
threads: 36
singularity: "docker://evolbioinfo/iqtree:v2.0-rc1"
params:
mem = 100000,
name = 'iq.{type}',
shell:
"""
iqtree -s {input.aln} -st DNA -nt {threads} -m GTR+FO+I+G6 -spp {input.part} --runs 1 -b 100
mv {input.part}.treefile {output.tree}
sed -i -e 's/\\///g' {output.tree}
mv {input.part}.iqtree {output.log}
mv {input.part}.boottrees {output.bs}
mv {input.part}.best_model.nex {output.model}
# rm -rf {input.part}.*
"""
rule log_lh:
'''
Compares tree likelihoods.
'''
input:
aln = os.path.join(data_dir, 'aln.fa'),
trees = expand(os.path.join(data_dir, 'trees', '{type}_tree.{{part}}.nwk'), type=tree_types),
part = os.path.join(data_dir, 'partition_iq.{part}.txt'),
output:
log = os.path.join(data_dir, 'trees', 'loglikelihoods.{part}.log'),
threads: 12
singularity: "docker://evolbioinfo/iqtree:v2.0-rc1"
params:
mem = 60000,
name = 'lh_iqtree',
shell:
"""
for t in `(ls {input.trees})`; do
name=`(basename $t .nwk)`
cp {input.part} {input.part}.iq.$name
iqtree -s {input.aln} -st DNA -nt {threads} -m GTR+FO+I+G6 -spp {input.part}.iq.$name --show-lh -te $t
l=`(grep -e 'Log-likelihood of the tree: .* (s.e.' {input.part}.iq.${{name}}.iqtree | sed -e "s/Log-likelihood of the tree: //g" | sed -e "s/ (s.e. .*)//g")`
echo "$name $l">> {output.log}
rm -rf {input.part}.iq.${{name}}*
done
"""
rule best_tree:
'''
Gets the best tree in terms of likelihood.
'''
input:
log = os.path.join(data_dir, 'trees', 'loglikelihoods.{part}.log'),
trees = expand(os.path.join(data_dir, 'trees', '{type}_tree.{{part}}.nwk'), type=tree_types),
output:
tree = os.path.join(data_dir, 'trees', 'best_tree.{part,(gene|type|pos|aa)}.nwk'),
threads: 1
singularity: "docker://evolbioinfo/python-evol:v3.6"
params:
mem = 10000,
name = 'best_tree.{part}',
shell:
"""
python3 py/best_tree.py --log {input.log} --trees {input.trees} --best_tree {output.tree}
"""
rule root_tree:
'''
Roots a tree with an outgroup.
'''
input:
tree = os.path.join(data_dir, 'trees', '{tree}.nwk'),
outgroup = os.path.join(data_dir, 'ids_{outgroup}.txt'),
output:
tree = os.path.join(data_dir, 'trees', '{tree}.rooted_{outgroup}.nwk'),
singularity:
"docker://evolbioinfo/gotree:v0.4.1a"
params:
mem = 2000,
name = 'root',
threads: 1
shell:
"""
gotree reroot outgroup -i {input.tree} -l "{input.outgroup}" -o "{output.tree}" -r
"""
rule aln_length:
'''
Calculates alignment length.
'''
input:
aln = os.path.join(data_dir, '{aln}.fa'),
output:
length = os.path.join(data_dir, '{aln}.length'),
singularity:
"docker://evolbioinfo/goalign:v0.3.1"
params:
mem = 2000,
name = 'aln_len',
qos = 'fast'
threads: 1
shell:
"""
goalign stats length -i {input.aln} > {output.length}
"""
rule collapse_non_informative_branches:
'''
Collapse internal branches of length <= 1/2 mut/site, set external branches of length <= 1/2 mut/site to zero.
If the tree is rooted and a root child branch is to be collapsed, set it to zero instead (to keep the tree rooted).
'''
input:
tree = os.path.join(data_dir, 'trees', '{tree}.nwk'),
length = os.path.join(data_dir, 'aln.length'),
output:
tree = os.path.join(data_dir, 'trees', '{tree}.collapsed.nwk'),
params:
mem = 2000,
name = 'collapse',
qos = 'fast',
threads: 1
singularity: "docker://evolbioinfo/python-evol:v3.6richer"
shell:
"""
n=`head {input.length}`
l=`awk -v n="$n" 'BEGIN {{ m=1/n/2; print m}}'`
python3 py/collapse.py --input_tree {input.tree} --output_tree {output.tree} --threshold $l --feature dist
"""