-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile
400 lines (370 loc) · 13.1 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
configfile: "config.yaml"
wildcard_constraints:
gene = '\w+'
rule final:
input: expand("RAxML_labelledTree.{gene} \
results/new.{gene}.db.fasta \
results/{gene}.taxonomy.final.txt".split(), gene=config["gene"], cutoff_otu=config["cutoff_otu"]) if config['update']\
else expand("results/{gene}.treefile \
results/{gene}.aligned.good.filter.unique.pick.good.filter.redundant.fasta \
results/{gene}.taxonomy.final.txt".split(), gene=config["gene"], cutoff_otu=config["cutoff_otu"])
rule download_ncbi:
output:
"interm/{gene}.fasta"
conda:
"envs/entrez.yaml"
params:
gene="{gene}",
full_name=config["full_name"],
mindate=config["mindate"],
maxdate=config["maxdate"]
message: "Retrieving gene sequences from NCBI. Note, this can take a (long) while"
shell:"""
if [[ {config[update]} == True ]]; then
esearch -db nucleotide -query "{params.gene}[gene]" -mindate {params.mindate} -maxdate {params.maxdate} | \
efetch -format gpc | \
xtract -pattern INSDFeature -if INSDFeature_key -equals CDS -and INSDQualifier_value -equals {params.gene} -or INSDQualifier_value -contains '{params.full_name}' -element INSDInterval_accession -element INSDInterval_from -element INSDInterval_to | \
sort -u -k1,1 | \
uniq | \
awk 'NF<4' | \
xargs -n 3 sh -c 'efetch -db nuccore -id "$0" -seq_start "$1" -seq_stop "$2" -format fasta' > {output}
else
esearch -db nucleotide -query "{params.gene}[gene]" | \
efetch -format gpc | \
xtract -pattern INSDFeature -if INSDFeature_key -equals CDS -and INSDQualifier_value -equals {params.gene} -or INSDQualifier_value -contains '{params.full_name}' -element INSDInterval_accession -element INSDInterval_from -element INSDInterval_to | \
sort -u -k1,1 | \
uniq | \
awk 'NF<4' | \
xargs -n 3 sh -c 'efetch -db nuccore -id "$0" -seq_start "$1" -seq_stop "$2" -format fasta' > {output}
fi
"""
rule download_taxonomy:
output:
"interm/tax_{gene}.txt"
conda:
"envs/entrez.yaml"
params:
gene="{gene}",
full_name=config["full_name"],
mindate=config["mindate"],
maxdate=config["maxdate"]
message: "Retrieving taxonomy from NCBI. Note, this can take a (long) while"
shell:"""
if [[ {config[update]} == True ]]; then
esearch -db nucleotide -query "{params.gene}[gene]" -mindate {params.mindate} -maxdate {params.maxdate} | \
efetch -format gpc | \
xtract -pattern INSDSeq -if INSDFeature_key -equals CDS -and INSDQualifier_value -equals {params.gene} -or INSDQualifier_value -contains '{params.full_name}' -element INSDSeq_accession-version -element INSDSeq_taxonomy |\
sort -u -k1,1 |\
uniq > {output}
else
esearch -db nucleotide -query "{params.gene}[gene]" | \
efetch -format gpc | \
xtract -pattern INSDSeq -if INSDFeature_key -equals CDS -and INSDQualifier_value -equals {params.gene} -or INSDQualifier_value -contains '{params.full_name}' -element INSDSeq_accession-version -element INSDSeq_taxonomy |\
sort -u -k1,1 |\
uniq > {output}
fi
"""
rule rename_seqs:
input:
"interm/{gene}.fasta"
output:
"interm/{gene}.renamed.fasta"
shell:
"""
sed -e 's/[.]/-/' -e 's/ /-/g' -e 's/_//g' {input} | \
stdbuf -o0 cut -d "-" -f 1,4,5| \
sed -e 's/-/_/g' -e 's/[.]//g' -e 's/[,]//g' > {output}
"""
rule get_unverified:
input:
"interm/{gene}.renamed.fasta"
output:
"interm/{gene}.unverified.accnos"
shell:
"""
set +o pipefail; grep UNVERIFIED {input} | \
stdbuf -o0 cut -c 2- > {output}
"""
rule remove_unverified:
input:
accnos="interm/{gene}.unverified.accnos",
fasta="interm/{gene}.renamed.fasta"
output:
"interm/{gene}.renamed.pick.fasta"
conda:
"envs/mothur.yaml"
shell:
'''
mothur "#remove.seqs(accnos={input.accnos}, fasta={input.fasta})"
'''
rule mothur_trim:
input:
"interm/{gene}.renamed.pick.fasta"
output:
"interm/{gene}.renamed.pick.trim.fasta"
conda:
"envs/mothur.yaml"
params:
minlength=config['minlength']
threads:10
shell:
'''
mothur "#trim.seqs(fasta={input}, minlength={params.minlength}, maxambig=0, processors={threads})"
'''
if config['framebot_db'] == True:
rule framebot:
input:
fasta="interm/{gene}.renamed.pick.trim.fasta",
db_framebot="dbs/{gene}.fungene.clean.fasta"
output:
"interm/{gene}.framebot_corr_nucl.fasta"
params:
"interm/{gene}.framebot"
conda:
"envs/rdptools.yaml"
shell:
"FrameBot framebot -o {params} -N {input.db_framebot} {input.fasta}"
rule align:
input:
"interm/{gene}.framebot_corr_nucl.fasta" if config['framebot_db'] == True else\
"interm/{gene}.renamed.pick.trim.fasta"
output:
"interm/{gene}.aligned.fasta"
conda:
"envs/mafft.yaml"
threads:10
shell:
"mafft --thread {threads} --auto {input} >{output}"
rule screen_alignment:
input:
"interm/{gene}.aligned.fasta"
output:
"interm/{gene}.aligned.good.fasta"
conda:
"envs/mothur.yaml"
threads:10
shell:
'''
mothur "#screen.seqs(fasta={input}, optimize=start-end, criteria=96, processors={threads})"
'''
rule filter_alignment:
input:
"interm/{gene}.aligned.good.fasta"
output:
"interm/{gene}.aligned.good.filter.fasta"
conda:
"envs/mothur.yaml"
threads:10
shell:
'''
mothur "#filter.seqs(fasta={input}, vertical=T, trump=., processors={threads})"
'''
rule unique1:
input:
"interm/{gene}.aligned.good.filter.fasta"
output:
"interm/{gene}.aligned.good.filter.unique.fasta",
"interm/{gene}.aligned.good.filter.names"
conda:
"envs/mothur.yaml"
shell:
'''
mothur "#unique.seqs(fasta={input})"
'''
rule chimera_vsearch:
input:
fasta="interm/{gene}.aligned.good.filter.unique.fasta",
name="interm/{gene}.aligned.good.filter.names"
output:
"interm/{gene}.aligned.good.filter.unique.denovo.vsearch.accnos"
conda:
"envs/mothur.yaml"
shell:
'''
mothur "#chimera.vsearch(fasta={input.fasta}, name={input.name})"
'''
rule chimera_removal:
input:
accnos="interm/{gene}.aligned.good.filter.unique.denovo.vsearch.accnos",
fasta="interm/{gene}.aligned.good.filter.unique.fasta",
name="interm/{gene}.aligned.good.filter.names"
output:
"interm/{gene}.aligned.good.filter.pick.names",
"interm/{gene}.aligned.good.filter.unique.pick.fasta"
conda:
"envs/mothur.yaml"
shell:
'''
mothur "#remove.seqs(accnos={input.accnos}, fasta={input.fasta}, name={input.name})"
'''
rule screen_alignment2:
input:
fasta="interm/{gene}.aligned.good.filter.unique.pick.fasta",
name="interm/{gene}.aligned.good.filter.pick.names"
output:
"interm/{gene}.aligned.good.filter.unique.pick.good.fasta"
conda:
"envs/mothur.yaml"
threads:10
shell:'''
mothur "#screen.seqs(fasta={input.fasta}, name={input.name}, optimize=start-end, criteria=96, processors={threads})"
'''
rule filter_alignment2:
input:
"interm/{gene}.aligned.good.filter.unique.pick.good.fasta"
output:
"interm/{gene}.aligned.good.filter.unique.pick.good.filter.fasta"
conda:
"envs/mothur.yaml"
threads:10
shell:
'''
mothur "#filter.seqs(fasta={input}, vertical=T, trump=., processors={threads})"
'''
rule distance_matrix:
input:
"interm/{gene}.aligned.good.filter.unique.pick.good.filter.fasta"
output:
"interm/{gene}.aligned.good.filter.unique.pick.good.filter.dist"
conda:
"envs/mothur.yaml"
threads:10
shell:
'''
mothur "#dist.seqs(fasta={input}, cutoff={config[cutoff_dm]}, processors={threads})"
'''
import os.path
from os import path
def get_namesfile(gene=config["gene"]):
if os.path.exists("interm/{gene}.aligned.good.filter.pick.good.names"):
return "interm/{gene}.aligned.good.filter.pick.good.names"
else:
return "interm/{gene}.aligned.good.filter.pick.names"
rule clustering:
input:
column="interm/{gene}.aligned.good.filter.unique.pick.good.filter.dist",
name=get_namesfile
output:
"interm/{gene}.aligned.good.filter.unique.pick.good.filter.an.list"
conda:
"envs/mothur.yaml"
shell:
'''
mothur "#cluster(column={input.column}, name={input.name}, method=average, cutoff={config[cutoff_dm]})"
'''
rule otu_reps:
input:
column="interm/{gene}.aligned.good.filter.unique.pick.good.filter.dist",
fasta="interm/{gene}.aligned.good.filter.unique.pick.good.filter.fasta",
name=get_namesfile,
list="interm/{gene}.aligned.good.filter.unique.pick.good.filter.an.list"
output:
"interm/{gene}.aligned.good.filter.unique.pick.good.filter.an.{cutoff_otu}.rep.fasta",
"interm/{gene}.aligned.good.filter.unique.pick.good.filter.an.{cutoff_otu}.rep.names"
conda:
"envs/mothur.yaml"
shell:
'''
mothur "#get.oturep(column={input.column}, fasta={input.fasta}, name={input.name}, list={input.list}, cutoff={config[cutoff_otu]})"
'''
rule final_database:
input:
fasta="interm/{gene}.aligned.good.filter.unique.pick.good.filter.fasta",
name=get_namesfile
output:
"results/{gene}.aligned.good.filter.unique.pick.good.filter.redundant.fasta"
conda:
"envs/mothur.yaml"
shell:
'''
mothur "#deunique.seqs(fasta={input.fasta}, name={input.name}, outputdir=./results)"
'''
rule tax_format:
input:
"interm/tax_{gene}.txt"
output:
"interm/newtax_{gene}.txt"
shell:
"""
sed -e 's/_//g' -e 's/ //g' -e 's/$/;/' {input} > {output}
"""
rule get_fasta_names:
input:
"results/{gene}.aligned.good.filter.unique.pick.good.filter.redundant.fasta"
output:
"interm/{gene}_fasta_final.names.txt"
shell:
"""
grep ">" {input} | stdbuf -o0 cut -c 2- > {output}
"""
rule tax_format_final:
input:
fasta="interm/{gene}_fasta_final.names.txt",
tax="interm/newtax_{gene}.txt"
output:
final_tax="results/{gene}.taxonomy.final.txt"
conda:
"envs/tidyr.yaml"
script:
"renaming.R"
if not config['update']:
rule iqtree:
input:
expand("interm/{gene}.aligned.good.filter.unique.pick.good.filter.an.{cutoff_otu}.rep.fasta", gene=config["gene"], cutoff_otu=config["cutoff_otu"])
output:
"results/{gene}.treefile"
params:
"results/{gene}"
conda:
"envs/iqtree.yaml"
threads:10
shell:
"iqtree -s {input} -m MFP -alrt 1000 -bb 1000 -nt {threads} -pre {params}"
if config['update']:
rule cat_EPA:
input:
fasta_new=expand("interm/{gene}.aligned.good.filter.unique.pick.good.filter.an.{cutoff_otu}.rep.fasta", gene=config["gene"], cutoff_otu=config["cutoff_otu"]),
fasta=config['path_to_seqs']
output:
"interm/new.{gene}.fasta"
shell:
"""
cat {input.fasta_new} {input.fasta} > {output}
"""
rule align_EPA:
input:
"interm/new.{gene}.fasta"
output:
"interm/new.{gene}.aligned.fasta"
conda:
"envs/mafft.yaml"
threads:10
shell:
"""
mafft --thread {threads} --auto {input} >{output}
"""
rule EPA:
input:
fasta="interm/new.{gene}.aligned.fasta",
tree=config['path_to_tree']
output:
"RAxML_labelledTree.{gene}"
params:
"{gene}"
conda:
"envs/raxml.yaml"
threads:10
shell:
"""
raxmlHPC -f v -s {input.fasta} -t {input.tree} -m GTRCAT -H -n {params}
"""
rule cat_db:
input:
fasta_new="results/{gene}.aligned.good.filter.unique.pick.good.filter.redundant.fasta",
fasta_db=config['path_to_db']
output:
"results/new.{gene}.db.fasta"
shell:
"""
cat {input.fasta_new} {input.fasta_db} > {output}
"""