-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile
519 lines (448 loc) · 19 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
"""
2022 - Analysis of Orthologous Collections (AOC).
@Author: Alexander G. Lucaci
"""
#----------------------------------------------------------------------------
# Imports
#----------------------------------------------------------------------------
import itertools
import os
import sys
import csv
import json
from pathlib import Path
from snakemake.utils import min_version
min_version("6.3")
from Bio import Entrez
from ete3 import NCBITaxa
import pandas as pd
from ete3 import Tree
import glob
#----------------------------------------------------------------------------
# Configuration
#----------------------------------------------------------------------------
configfile: 'config.yml'
print("# Loaded config yaml file")
with open("cluster.json", "r") as fh_cluster:
cluster = json.load(fh_cluster)
fh_cluster.close()
#end with
print("# Loaded cluster json file")
Nucleotide_file = config["Nucleotide"]
Protein_file = config["Protein"]
Label = config["Label"]
print("# Using nucleotide data from:", Nucleotide_file)
print("# Using protein data from:", Protein_file)
print("# Using the analysis label:", Label)
HumanRef = config["HumanRef"]
# Set output directory
BASEDIR = os.getcwd()
print("# We are operating out of base directory:", BASEDIR)
OUTDIR = os.path.join(BASEDIR, "results", Label)
OUTDIR_RESULTS = os.path.join(BASEDIR, "results")
print("# We will create and store results in:", OUTDIR)
# Create output dir.
os.makedirs(OUTDIR_RESULTS, exist_ok = True)
print("# Directory '% s' created" % OUTDIR_RESULTS)
os.makedirs(OUTDIR, exist_ok = True)
print("# Directory '% s' created" % OUTDIR)
#Path(OUTDIR_RESULTS).mkdir(parents=True, exist_ok=True)
#Path(OUTDIR).mkdir(parents=True, exist_ok=True)
PPN = cluster["__default__"]["ppn"]
#PPN_MSA = int(PPN) - 7
# Batch files
PREMSA = os.path.join(BASEDIR, config["PREMSA"])
POSTMSA = os.path.join(BASEDIR, config["POSTMSA"])
FILTER_OUTLIERS_BF = os.path.join(BASEDIR, "hyphy-analyses", "find-outliers", "find-outliers-slac.bf")
FITMG94 = os.path.join(BASEDIR, "hyphy-analyses", "FitMG94", "FitMG94.bf")
# Hard-coded HyPhy settings
HYPHY = config["HYPHY"]
HYPHYMPI = config["HYPHYMPI"]
#RES = os.path.join(BASEDIR, config["RES"])
CODON_OUTPUT = os.path.join(OUTDIR, Label)
# Clustering
TN93_T = config["TN93_Threshold"]
CSV = os.path.join(BASEDIR, config["CSV"])
#----------------------------------------------------------------------------
# Rule all
#----------------------------------------------------------------------------
rule all:
input:
CODON_OUTPUT,
os.path.join(OUTDIR, Label + "_protein.fas"),
os.path.join(OUTDIR, Label + "_nuc.fas"),
os.path.join(OUTDIR, Label + "_protein.aln"),
os.path.join(OUTDIR, Label + "_codons.fasta"),
os.path.join(OUTDIR, Label + "_codons_duplicates.json"),
#os.path.join(OUTDIR, Label + "_codons.SA.fasta.dst"),
os.path.join(OUTDIR, Label + "_codons.SA.fasta"),
os.path.join(OUTDIR, Label + "_codons.SA.fasta.treefile"),
os.path.join(OUTDIR, Label + "_codons.SA.fasta.SLAC.json"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.json"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.dst"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.treefile"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.GARD.json"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.BUSTEDS.json"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.BUSTED.json"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.BUSTEDS+MH.json"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.BUSTED+MH.json"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.FITMG94.json"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.BGM.json"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.FEL.json"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.FUBAR.json"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.FMM.json"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.MEME.json"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.SLAC.json"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.aBSRELS.json"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.aBSRELS+MH.json"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.PRIME.json"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.treefile.labelled"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.RELAX.json"),
os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.CFEL.json")
#end rule all
print("# Moving on to processing rules")
#----------------------------------------------------------------------------
# Rules
#----------------------------------------------------------------------------
rule get_codons:
output:
codons = CODON_OUTPUT,
params:
Nuc = Nucleotide_file,
Prot = Protein_file,
Out = CODON_OUTPUT,
Logfile = CODON_OUTPUT + ".log"
conda:
"environment.yml"
script:
"scripts/codons.py"
#end rule
#----------------------------------------------------------------------------
# Alignment
#----------------------------------------------------------------------------
rule pre_msa:
input:
codons = rules.get_codons.output.codons
output:
protein_fas = os.path.join(OUTDIR, Label + "_protein.fas"),
nucleotide_fas = os.path.join(OUTDIR, Label + "_nuc.fas")
conda:
"environment.yml"
shell:
"mpirun -np {PPN} {HYPHYMPI} {PREMSA} --input {input.codons} --reference {HumanRef}"
#"mpirun -np {PPN} {HYPHYMPI} {PREMSA} --input {input.codons}"
#end rule
rule mafft:
input:
protein = rules.pre_msa.output.protein_fas
output:
protein_aln = os.path.join(OUTDIR, Label + "_protein.aln")
conda:
"environment.yml"
shell:
"mafft --auto {input.protein} > {output.protein_aln}"
#end rule
rule post_msa:
input:
protein_aln = rules.mafft.output.protein_aln,
nucleotide_seqs = rules.pre_msa.output.nucleotide_fas
output:
codons_fas = os.path.join(OUTDIR, Label + "_codons.fasta"),
duplicates_json = os.path.join(OUTDIR, Label + "_codons_duplicates.json")
conda:
"environment.yml"
shell:
"mpirun -np {PPN} {HYPHYMPI} {POSTMSA} --protein-msa {input.protein_aln} --nucleotide-sequences {input.nucleotide_seqs} --output {output.codons_fas} --duplicates {output.duplicates_json}"
#end rule
#----------------------------------------------------------------------------
# Remove ambiguous codons, a source of noise.
#----------------------------------------------------------------------------
rule strike_ambigs:
input:
in_msa = rules.post_msa.output.codons_fas
output:
out_strike_ambigs = os.path.join(OUTDIR, Label + "_codons.SA.fasta")
conda:
"environment.yml"
shell:
"{HYPHY} scripts/strike-ambigs.bf --alignment {input.in_msa} --output {output.out_strike_ambigs}"
#end rule
#----------------------------------------------------------------------------
# IQ-TREE for ML tree inference
#----------------------------------------------------------------------------
rule iqtree: # Unrooted
input:
codons_fas = rules.strike_ambigs.output.out_strike_ambigs
output:
tree = os.path.join(OUTDIR, Label + "_codons.SA.fasta.treefile")
conda:
"environment.yml"
shell:
"iqtree -s {input.codons_fas} -T AUTO -B 100"
#end shell
#end rule iqtree
#----------------------------------------------------------------------------
# Run SLAC
#----------------------------------------------------------------------------
rule SLAC:
input:
codon_aln = rules.strike_ambigs.output.out_strike_ambigs,
tree = rules.iqtree.output.tree
output:
results = os.path.join(OUTDIR, Label + "_codons.SA.fasta.SLAC.json")
conda:
"environment.yml"
shell:
"mpirun -np {PPN} {HYPHYMPI} SLAC --alignment {input.codon_aln} --tree {input.tree} --output {output.results}"
#end rule
#----------------------------------------------------------------------------
# Filter outliers
#----------------------------------------------------------------------------
rule filter_outliers:
input:
slac_json = rules.SLAC.output.results
output:
fasta = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta"),
json = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.json")
conda:
"environment.yml"
shell:
"{HYPHY} {FILTER_OUTLIERS_BF} --slac {input.slac_json} --output {output.fasta} --outlier-coord-output {output.json}"
#end rule
#----------------------------------------------------------------------------
# IQ-TREE for ML tree inference -- After filtering outliers
#----------------------------------------------------------------------------
rule iqtree_fo: # Unrooted
input:
codons_fas = rules.filter_outliers.output.fasta
output:
tree = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.treefile"),
conda:
"environment.yml"
shell:
"iqtree -s {input.codons_fas} -T AUTO -B 100"
#end shell
#end rule iqtree
#----------------------------------------------------------------------------
# Recombination detection
#----------------------------------------------------------------------------
rule recombination_filter_outliers:
input:
input = rules.filter_outliers.output.fasta
output:
output = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.GARD.json"),
shell:
"mpirun -np {PPN} {HYPHYMPI} GARD --alignment {input.input} --rv GDD --output {output.output} ENV='TOLERATE_NUMERICAL_ERRORS=1;'"
#end rule
#----------------------------------------------------------------------------
# TN93, genetic distance calculation
#----------------------------------------------------------------------------
rule tn93:
input:
input = rules.filter_outliers.output.fasta
output:
output = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.dst")
shell:
"tn93 -t 1 -o {output.output} {input.input}"
#end shell
#end rule
#----------------------------------------------------------------------------
# Selection analyses
#----------------------------------------------------------------------------
rule BUSTEDS:
input:
codon_aln = rules.filter_outliers.output.fasta,
tree = rules.iqtree_fo.output.tree
output:
results = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.BUSTEDS.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} BUSTED --alignment {input.codon_aln} --tree {input.tree} --output {output.results} --srv Yes --starting-points 10"
#end rule
rule BUSTED:
input:
codon_aln = rules.filter_outliers.output.fasta,
tree = rules.iqtree_fo.output.tree
output:
results = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.BUSTED.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} BUSTED --alignment {input.codon_aln} --tree {input.tree} --output {output.results} --srv No --starting-points 10"
#end rule
rule BUSTEDSMH:
input:
codon_aln = rules.filter_outliers.output.fasta,
tree = rules.iqtree_fo.output.tree
output:
results = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.BUSTEDS+MH.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} BUSTED --alignment {input.codon_aln} --tree {input.tree} --output {output.results} --srv Yes --starting-points 10 --multiple-hits Double+Triple"
#end rule
rule BUSTEDMH:
input:
codon_aln = rules.filter_outliers.output.fasta,
tree = rules.iqtree_fo.output.tree
output:
results = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.BUSTED+MH.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} BUSTED --alignment {input.codon_aln} --tree {input.tree} --output {output.results} --srv No --starting-points 10 --multiple-hits Double+Triple"
#end rule
rule BGM:
input:
codon_aln = rules.filter_outliers.output.fasta,
tree = rules.iqtree_fo.output.tree
output:
results = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.BGM.json")
shell:
#"mpirun -np {PPN} {HYPHYMPI} BGM --alignment {input.codon_aln} --tree {input.tree} --output {output.results}"
"{HYPHY} BGM --alignment {input.codon_aln} --tree {input.tree} --output {output.results}"
#end rule
rule SLAC_FO:
input:
codon_aln = rules.filter_outliers.output.fasta,
tree = rules.iqtree_fo.output.tree
output:
results = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.SLAC.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} SLAC --alignment {input.codon_aln} --tree {input.tree} --output {output.results} --samples 100"
#end rule
rule ABSRELS:
input:
codon_aln = rules.filter_outliers.output.fasta,
tree = rules.iqtree_fo.output.tree
output:
results = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.aBSRELS.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} ABSREL --alignment {input.codon_aln} --tree {input.tree} --output {output.results} --srv Yes"
#end rule
rule ABSRELSMH:
input:
codon_aln = rules.filter_outliers.output.fasta,
tree = rules.iqtree_fo.output.tree
output:
results = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.aBSRELS+MH.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} ABSREL --alignment {input.codon_aln} --tree {input.tree} --output {output.results} --srv Yes --multiple-hits Double+Triple"
#end rule
rule FITMG94:
input:
codon_aln = rules.filter_outliers.output.fasta,
tree = rules.iqtree_fo.output.tree
output:
results = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.FITMG94.json")
shell:
"{HYPHY} {FITMG94} --alignment {input.codon_aln} --tree {input.tree} --output {output.results} --rooted No --lrt Yes --type global --frequencies CF3x4"
#end rule
rule FMM:
input:
codon_aln = rules.filter_outliers.output.fasta,
tree = rules.iqtree_fo.output.tree
output:
results = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.FMM.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} FMM --alignment {input.codon_aln} --tree {input.tree} --output {output.results} --triple-islands Yes"
#end rule
rule MEME:
input:
codon_aln = rules.filter_outliers.output.fasta,
tree = rules.iqtree_fo.output.tree
output:
results = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.MEME.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} MEME --alignment {input.codon_aln} --tree {input.tree} --output {output.results}"
#end rule
rule FEL:
input:
codon_aln = rules.filter_outliers.output.fasta,
tree = rules.iqtree_fo.output.tree
output:
results = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.FEL.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} FEL --alignment {input.codon_aln} --tree {input.tree} --output {output.results} --ci Yes"
#end rule
rule FUBAR:
input:
codon_aln = rules.filter_outliers.output.fasta,
tree = rules.iqtree_fo.output.tree
output:
results = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.FUBAR.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} FUBAR --alignment {input.codon_aln} --tree {input.tree} --output {output.results}"
#end rule
rule PRIME:
input:
codon_aln = rules.filter_outliers.output.fasta,
tree = rules.iqtree_fo.output.tree
output:
results = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.PRIME.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} PRIME --alignment {input.codon_aln} --tree {input.tree} --output {output.results}"
#end rule
#----------------------------------------------------------------------------
# Lineages
#----------------------------------------------------------------------------
rule GatherLineages:
input:
out_d = OUTDIR,
csv_f = CSV,
tree_f = rules.iqtree_fo.output.tree
output:
output = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.treefile.log")
shell:
"python scripts/LineageAnnotation_Pipeline.py {input.out_d} {input.csv_f} {input.tree_f}"
#end rule
CLADE_FILES = [x for x in glob.glob(os.path.join(OUTDIR, "*.clade"))]
print("# We have", len(CLADE_FILES), "clade files")
print(CLADE_FILES)
rule AssignLineages:
input:
tree = rules.iqtree.output.tree,
log = rules.GatherLineages.output.output
output:
output = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.treefile.labelled")
run:
first_time = True
for clade_file in CLADE_FILES:
print(clade_file, input[0])
label = os.path.basename(clade_file).split(".")[0]
if first_time == True:
cmd = " ".join([HYPHY,
os.path.join(BASEDIR, "scripts", "label-tree.bf"),
"--tree", input[0],
"--list", clade_file,
"--output", output[0],
"--label", label])
first_time = False
else:
cmd = " ".join([HYPHY,
os.path.join(BASEDIR, "scripts", "label-tree.bf"),
"--tree", output[0],
"--list", clade_file,
"--output", output[0],
"--label", label])
#end if
print(cmd)
os.system(cmd)
#end for
#end run
#end rule
rule RELAX:
input:
treefile = rules.AssignLineages.output.output,
fasta = rules.filter_outliers.output.fasta
output:
output = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.RELAX.json")
shell:
"{HYPHY} RELAX --alignment {input.fasta} --tree {input.treefile} --output {output.output} --reference-group Primates --models All --mode 'Group mode' --starting-points 10 --srv Yes"
#end rule
rule CFEL:
input:
treefile = rules.AssignLineages.output.output,
fasta = rules.filter_outliers.output.fasta
output:
output = os.path.join(OUTDIR, Label + "_codons.SA.FilterOutliers.fasta.CFEL.json")
shell:
"{HYPHY} contrast-fel --alignment {input.fasta} --tree {input.treefile} --output {output.output} --branch-set Primates"
#end file
#----------------------------------------------------------------------------
# End of file
#----------------------------------------------------------------------------