-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile_Recombinants
362 lines (309 loc) · 13.2 KB
/
Snakefile_Recombinants
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
"""
Snakemakefile that runs the AOC application - Selection analysis.
Written by Alexander G Lucaci (2024)
"""
# =============================================================================
# Imports
# =============================================================================
import itertools
import os
import sys
import csv
import json
from pathlib import Path
from snakemake.utils import min_version
import glob
# =============================================================================
# Configuration
# =============================================================================
configfile: 'config.yml'
with open("cluster.json", "r") as fh:
cluster = json.load(fh)
#end with
Label = config["Label"]
# Set output directory
BASEDIR = os.getcwd()
# Set output directory
BASEDIR = os.getcwd()
print("# Selection Analysis! We are operating out of base directory:", BASEDIR)
OUTDIR = os.path.join(BASEDIR, "results", Label)
print("# Output directory:", OUTDIR)
Recombinants = sorted([os.path.basename(x) for x in glob.glob(os.path.join(OUTDIR,
'*.codon.fas'))])
Recombinants_Trees = sorted([os.path.basename(x) for x in glob.glob(os.path.join(OUTDIR,
'*.tree.nwk'))])
DATADIR = os.path.join(BASEDIR,
"data",
Label)
CladeLabels = sorted([x for x in glob.glob(os.path.join(OUTDIR, '*.clade'))])
ReferenceClade = os.path.basename(CladeLabels[0]).split(".")[0]
#CladeLabels[0].split(".")[0]
print("# We will process selection analyses in", len(Recombinants), "files")
print("# We will use the following clade labels:", CladeLabels)
# Set PPN
PPN = cluster["__default__"]["ppn"]
# HyPhy settings
HYPHY = "hyphy"
HYPHYMPI = "HYPHYMPI"
FITMG94 = os.path.join(BASEDIR, "hyphy-analyses", "FitMG94", "FitMG94.bf")
# =============================================================================
# Rule all
# =============================================================================
rule all:
input:
# Calculate genetic distances
expand(os.path.join(OUTDIR, "{sample}.dst"), sample = Recombinants, tree = Recombinants_Trees),
expand(os.path.join(OUTDIR, "{sample}.FastTree.treefile"), sample=Recombinants),
# Selection analysis repertoire
#expand(os.path.join(OUTDIR, "{sample}.MG94.json"), sample=Recombinants),
expand(os.path.join(OUTDIR, "{sample}.FEL.json"), sample=Recombinants),
expand(os.path.join(OUTDIR, "{sample}.FUBAR.json"), sample=Recombinants),
expand(os.path.join(OUTDIR, "{sample}.BUSTEDS.json"), sample=Recombinants),
expand(os.path.join(OUTDIR, "{sample}.MEME.json"), sample=Recombinants),
#expand(os.path.join(OUTDIR, "{sample}.MEME-iS.json"), sample=Recombinants),
expand(os.path.join(OUTDIR, "{sample}.ABSREL.json"), sample=Recombinants),
expand(os.path.join(OUTDIR, "{sample}.SLAC.json"), sample=Recombinants),
expand(os.path.join(OUTDIR, "{sample}.BGM.json"), sample=Recombinants),
#expand(os.path.join(OUTDIR, "{sample}.PRIME.json"), sample=Recombinants),
#expand(os.path.join(OUTDIR, "{sample}.ABSREL-MH.json"), sample=Recombinants),
expand(os.path.join(OUTDIR, "{sample}.BUSTEDS-MH.json"), sample=Recombinants),
expand(os.path.join(OUTDIR, "{sample}.FMM.json"), sample=Recombinants),
# Labelling taxonomy
expand(os.path.join(OUTDIR, "{tree}.labelled"), tree = Recombinants_Trees),
expand(os.path.join(OUTDIR, "{sample}.RELAX.json"), sample=Recombinants),
expand(os.path.join(OUTDIR, "{sample}.CFEL.json"), sample=Recombinants),
# Executive Summary
#expand(os.path.join(OUTDIR, "{sample}.FEL.png"), sample=Recombinants), # Start summary of results
#expand(os.path.join(OUTDIR, "{sample}.FEL.csv"), sample=Recombinants),
#expand(os.path.join(OUTDIR, "{sample}.FEL.FigureLegend"), sample=Recombinants),
#expand(os.path.join(OUTDIR, "{sample}.cumulativeResults.csv"), sample=Recombinants)
# Executive Summary
#expand(os.path.join(OUTDIR, "{sample}.executiveSummary.csv"), sample=Recombinants)
#end input
#end rule all
# =============================================================================
# Rules
# =============================================================================
rule tn93:
input:
input = os.path.join(OUTDIR, "{sample}")
output:
output = os.path.join(OUTDIR, "{sample}.dst")
shell:
"tn93 -t 1 -o {output.output} {input.input}"
#end rule tn93
# =============================================================================
# FastTree
# =============================================================================
rule fasttree:
input:
input = os.path.join(OUTDIR, "{sample}")
output:
tree = os.path.join(OUTDIR, "{sample}.FastTree.treefile")
shell:
"FastTree -nt -gtr {input.input} > {output.tree}"
#end rule
# =============================================================================
# Selection Analyses
# =============================================================================
rule FITMG94:
input:
codon_aln = os.path.join(OUTDIR, "{sample}"),
tree = rules.fasttree.output.tree
output:
results = os.path.join(OUTDIR, "{sample}.MG94.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 FEL:
input:
codon_aln = os.path.join(OUTDIR, "{sample}"),
tree = rules.fasttree.output.tree
output:
results = os.path.join(OUTDIR, "{sample}.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 = os.path.join(OUTDIR, "{sample}"),
tree = rules.fasttree.output.tree
output:
results = os.path.join(OUTDIR, "{sample}.FUBAR.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} FUBAR --alignment {input.codon_aln} --tree {input.tree} --output {output.results}"
#end rule
rule BUSTEDS:
input:
codon_aln = os.path.join(OUTDIR, "{sample}"),
tree = rules.fasttree.output.tree
output:
results = os.path.join(OUTDIR, "{sample}.BUSTEDS.json")
shell:
"{HYPHYMPI} BUSTED --alignment {input.codon_aln} --tree {input.tree} --output {output.results} --srv Yes --starting-points 25"
#end rule
rule MEME:
input:
codon_aln = os.path.join(OUTDIR, "{sample}"),
tree = rules.fasttree.output.tree
output:
results = os.path.join(OUTDIR, "{sample}.MEME.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} MEME --alignment {input.codon_aln} --tree {input.tree} --output {output.results}"
#end rule
rule MEME_iS:
input:
codon_aln = os.path.join(OUTDIR, "{sample}"),
tree = rules.fasttree.output.tree
output:
results = os.path.join(OUTDIR, "{sample}.MEME-iS.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} MEME --alignment {input.codon_aln} --tree {input.tree} --output {output.results} --impute-states Yes"
#end rule
rule ABSREL:
input:
codon_aln = os.path.join(OUTDIR, "{sample}"),
tree = rules.fasttree.output.tree
output:
results = os.path.join(OUTDIR, "{sample}.ABSREL.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} ABSREL --alignment {input.codon_aln} --tree {input.tree} --output {output.results}"
#end rule
rule SLAC:
input:
codon_aln = os.path.join(OUTDIR, "{sample}"),
tree = rules.fasttree.output.tree
output:
results = os.path.join(OUTDIR, "{sample}.SLAC.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} SLAC --alignment {input.codon_aln} --tree {input.tree} --output {output.results}"
#end rule
rule BGM:
input:
codon_aln = os.path.join(OUTDIR, "{sample}"),
tree = rules.fasttree.output.tree
output:
results = os.path.join(OUTDIR, "{sample}.BGM.json")
shell:
"{HYPHY} BGM --alignment {input.codon_aln} --tree {input.tree} --output {output.results}"
#end rule
rule PRIME:
input:
codon_aln = os.path.join(OUTDIR, "{sample}"),
tree = os.path.join(OUTDIR, "{sample}.tree.nwk")
output:
results = os.path.join(OUTDIR, "{sample}.PRIME.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} PRIME --alignment {input.codon_aln} --tree {input.tree} --output {output.results} --impute-states Yes"
#end rule
"""
rule ABSRELMH:
input:
codon_aln = os.path.join(OUTDIR, "{sample}"),
tree = os.path.join(OUTDIR, "{sample}.tree.nwk")
output:
results = os.path.join(OUTDIR, "{sample}.ABSREL-MH.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} LIBPATH={RES} ABSREL --alignment {input.codon_aln} --tree {input.tree} --output {output.results} --multiple-hits Double+Triple"
#end rule ABSRELMH
"""
rule BUSTEDSMH:
input:
codon_aln = os.path.join(OUTDIR, "{sample}"),
tree = rules.fasttree.output.tree
output:
results = os.path.join(OUTDIR, "{sample}.BUSTEDS-MH.json")
shell:
"{HYPHYMPI} BUSTED --alignment {input.codon_aln} --tree {input.tree} --output {output.results} --srv Yes --multiple-hits Double+Triple --starting-points 25"
#end rule
rule FMM:
input:
codon_aln = os.path.join(OUTDIR, "{sample}"),
tree = rules.fasttree.output.tree
output:
results = os.path.join(OUTDIR, "{sample}.FMM.json")
shell:
"mpirun -np {PPN} {HYPHYMPI} FMM --alignment {input.codon_aln} --tree {input.tree} --output {output.results} --triple-islands Yes"
#end rule
# =============================================================================
# Lineages
# =============================================================================
rule AssignLineages:
input:
tree = rules.fasttree.output.tree
output:
output = os.path.join(OUTDIR, "{sample}.FastTree.treefile.labelled")
run:
first_time = True
for clade_file in CladeLabels:
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
# =============================================================================
# Group level Selection analyses
# =============================================================================
rule RELAX:
input:
codon_aln = os.path.join(OUTDIR, "{sample}"),
tree = rules.AssignLineages.output.output
output:
output = os.path.join(OUTDIR, "{sample}.RELAX.json")
params:
RefClade = ReferenceClade
shell:
"{HYPHY} RELAX --alignment {input.codon_aln} --tree {input.tree} --output {output.output} --reference-group {params.RefClade} --models All --mode 'Group mode' --starting-points 10 --srv Yes"
#end rule
rule CFEL:
input:
codon_aln = os.path.join(OUTDIR, "{sample}"),
tree = rules.AssignLineages.output.output
output:
output = os.path.join(OUTDIR, "{sample}.CFEL.json")
params:
RefClade = ReferenceClade
shell:
"{HYPHY} contrast-fel --alignment {input.codon_aln} --tree {input.tree} --output {output.output} --branch-set {params.RefClade}"
#end rule
# =============================================================================
# Summarize results
# =============================================================================
rule executiveSummary:
input:
inputFEL = rules.FEL.output.results,
inputFUBAR = rules.FUBAR.output.results,
inputMEME = rules.MEME.output.results,
#inputBUSTEDS = rules.BUSTEDS.output.results,
#inputBUSTEDSMH = rules.BUSTEDSMH.output.results,
inputBGM = rules.BGM.output.results,
inputABSREL = rules.ABSREL.output.results,
inputSLAC = rules.SLAC.output.results,
inputRELAX = rules.RELAX.output.output,
inputCFEL = rules.CFEL.output.output
output:
output = os.path.join(OUTDIR, "{sample}.executiveSummary.csv")
script:
"scripts/executiveSummary.py"
#end rule
# =============================================================================
# End of file
# =============================================================================