forked from aglucaci/Endothermy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Snakefile_env
executable file
·362 lines (342 loc) · 16.1 KB
/
Snakefile_env
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
"""
@Description: Fish endothermy examination
@Authors: Alexander G Lucaci, Avery Selberg
@Version: 2023.4
"""
# =============================================================================
# Imports
# =============================================================================
import os
import sys
import json
import csv
from pathlib import Path
import glob
from Bio import SeqIO
# =============================================================================
# Declares
# =============================================================================
with open("config.MACSE.json", "r") as input_sc:
config = json.load(input_sc)
#end with
with open("cluster.json", "r") as input_c:
cluster = json.load(input_c)
#end with
# =============================================================================
# User Settings
# =============================================================================
BASEDIR = os.getcwd()
print("# We are operating out of base directory:", BASEDIR)
# Which project are we analyzing?
LABEL = config["Label"]
DATA_DIRECTORY = os.path.join(BASEDIR, "data", config["DataDirectory"])
print("# Examining data from:", DATA_DIRECTORY)
EXTENSION = "fasta"
PARTITION_LIST = config["Partition"].split(",")
print("# Testing Partition(s):", PARTITION_LIST)
CANDIDATE_GENES = [os.path.basename(x) for x in glob.glob(os.path.join(DATA_DIRECTORY, "*." + EXTENSION))]
## modified candidate genes, takes in tree files from outdir#
#print("# We will process", len(CANDIDATE_GENES), "candidate gene files")
# Check for Multiples of 3 in Codon alignments
def CheckMSA(msa):
with open(msa, "r") as fh:
for n, _record in enumerate(SeqIO.parse(fh, "fasta")):
_id = _record.id
_desc = _record.description
_seq = _record.seq
break
#end for
#end with
if len(str(_seq)) % 3 == 0:
return True
else:
return False
#end if
#end method
# =============================================================================
# Software Settings
# =============================================================================
HYPHY = "/home/agselberg/hyphy-develop/hyphy LIBPATH=/home/agselberg/hyphy-develop/res"
HYPHYMPI = "HYPHYMPI"
HYPHY_ANALYSES = os.path.join(BASEDIR, "hyphy-analyses")
STRIKE_AMBIGS_BF = os.path.join(BASEDIR, "scripts", "strike-ambigs.bf")
OUTDIR = os.path.join(BASEDIR, "results", LABEL + "_MACSE")
OUTDIR_BUSTED= os.path.join(os.path.join(BASEDIR, "results", LABEL + "_MACSE_ENV"))
# Hyphy-analyses
REMOVE_DUPS_BF = os.path.join(HYPHY_ANALYSES, "remove-duplicates", "remove-duplicates.bf")
BUSTED_PH_BF = os.path.join(HYPHY_ANALYSES, "BUSTED-PH", "BUSTED-PH.bf")
LABEL_TREE_BF = os.path.join(HYPHY_ANALYSES, "LabelTrees", "label-tree.bf")
FILTER_OUTLIERS_BF = os.path.join(HYPHY_ANALYSES, "find-outliers", "find-outliers-slac.bf")
# Other scripts
LABEL_BG = os.path.join(BASEDIR, "scripts", "tree-remaining-bg.py")
# Create output directories
Path(os.path.join(BASEDIR, "results")).mkdir(parents=True, exist_ok=True)
Path(OUTDIR).mkdir(parents=True, exist_ok=True)
# Settings, these can be passed in or set in a config.json type file
PPN = cluster["__default__"]["ppn"]
# =============================================================================
# rule all
# =============================================================================
rule all:
input:
expand(os.path.join(OUTDIR, "{GENE}.fa"), GENE=CANDIDATE_GENES),
expand(os.path.join(OUTDIR, "{GENE}.codons.trim.fa"), GENE=CANDIDATE_GENES),
expand(os.path.join(OUTDIR, "{GENE}.codons.fa"), GENE=CANDIDATE_GENES),
expand(os.path.join(OUTDIR, "{GENE}.codons.cln.fa"), GENE=CANDIDATE_GENES),
expand(os.path.join(OUTDIR, "{GENE}.SA.codons.cln.fa"), GENE=CANDIDATE_GENES),
expand(os.path.join(OUTDIR, "{GENE}.RD.SA.codons.cln.fa"), GENE=CANDIDATE_GENES),
expand(os.path.join(OUTDIR, "{GENE}.RD.SA.codons.cln.trim67.fa"), GENE=CANDIDATE_GENES),
expand(os.path.join(OUTDIR, "{GENE}.RD.SA.codons.cln.trim67.fa.raxml.bestTree"), GENE=CANDIDATE_GENES),
expand(os.path.join(OUTDIR, "{GENE}.RD.SA.codons.cln.trim67.fa.raxml.bestTree.labeled_fgOnly_{P}.nwk"), GENE=CANDIDATE_GENES, P=PARTITION_LIST),
expand(os.path.join(OUTDIR, "{GENE}.RD.SA.codons.cln.trim67.fa.raxml.bestTree.labeled_fg-nuOnly_{P}.nwk"), GENE=CANDIDATE_GENES, P=PARTITION_LIST),
expand(os.path.join(OUTDIR, "{GENE}.RD.SA.codons.cln.trim67.fa.raxml.bestTree.labeled_{P}.nwk"), GENE=CANDIDATE_GENES, P=PARTITION_LIST),
expand(os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.BUSTED.json"), GENE=CANDIDATE_GENES),
expand(os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.BUSTED-E.json"), GENE=CANDIDATE_GENES),
expand(os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.FILTER.json"), GENE=CANDIDATE_GENES),
expand(os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.FILTER.fasta"), GENE=CANDIDATE_GENES),
expand(os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.FILTERED.labeled_fgOnly_{P}.nwk"), GENE=CANDIDATE_GENES, P=PARTITION_LIST),
expand(os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.FILTERED.labeled_fg-nuOnly_{P}.nwk"), GENE=CANDIDATE_GENES, P=PARTITION_LIST),
expand(os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.FILTERED.labeled_{P}.nwk"), GENE=CANDIDATE_GENES, P=PARTITION_LIST),
expand(os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.{P}.filtered.BUSTED-PH.json"), GENE=CANDIDATE_GENES, P=PARTITION_LIST),
expand(os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.{P}.BUSTED-PH.json"), GENE=CANDIDATE_GENES, P=PARTITION_LIST),
# expand(), GENE=CANDIDATE_GENES, P=PARTITION_LIST),
# expand(), GENE=CANDIDATE_GENES),
# =============================================================================
# Individual rules: preprocessing steps
# =============================================================================
# remove unpermitted characters from sequence names
rule clean:
input:
input = os.path.join(DATA_DIRECTORY, "{GENE}")
output:
output = os.path.join(OUTDIR, "{GENE}.fa")
shell:
"python scripts/clean-fasta.py {input.input} {output.output}"
#end rule
"""
for sequences in E*unaligned.fasta;
do
if [ ! -e ${sequences%.*.*}.macse.txt ];
then
echo macse alignment started > ${sequences%.*.*}.macse.txt;
java -jar $macsepath -prog trimNonHomologousFragments -seq $sequences -min_homology_to_keep_seq 0.4 -out_NT ${sequences%.*}.NT_trimNonHomologousFragments.fasta -out_AA ${sequences%.*}.AA_trimNonHomologousFragments.fasta;
java -jar $macsepath -prog alignSequences -seq ${sequences%.*}.NT_trimNonHomologousFragments.fasta -out_NT ${sequences%.*.*}.NT_aligned.fasta -out_AA ${sequences%.*.*}.AA_aligned.fasta;
# replace '!' insertion character with 'N', since it seems to interfere with other software
sed -i 's/!/N/g' ${sequences%.*.*}.NT_aligned.fasta;
fi;
done
"""
# trim non-homologous fragments with MACSE
rule macse_trim:
input:
input = rules.clean.output.output
output:
codons = os.path.join(OUTDIR, "{GENE}.codons.trim.fa"),
aa = os.path.join(OUTDIR, "{GENE}.aa.trim.fa")
shell:
"macse -prog trimNonHomologousFragments -seq {input.input} -min_homology_to_keep_seq 0.4 -out_NT {output.codons} -out_AA {output.aa}"
#end rule
# align sequences with MACSE
rule macse:
input:
input = rules.macse_trim.output.codons
output:
codons = os.path.join(OUTDIR, "{GENE}.codons.fa"),
aa = os.path.join(OUTDIR, "{GENE}.aa.fa")
shell:
"""
macse -prog alignSequences -seq {input.input} -out_NT {output.codons} -out_AA {output.aa} -max_refine_iter 3 -local_realign_init 0.3 -local_realign_dec 0.2
sed -i 's/!/N/g' {output.codons}
"""
#end rule
# Get rid of internal stop codons
rule cln:
input:
input = rules.macse.output.codons
output:
output = os.path.join(OUTDIR, "{GENE}.codons.cln.fa")
shell:
"{HYPHY} CLN Universal {input.input} 'No/No' {output.output}"
#end rule
# replace ambiguous sequences with ---
rule strike_ambigs_msa:
input:
input_msa = rules.cln.output.output
output:
output = os.path.join(OUTDIR, "{GENE}.SA.codons.cln.fa")
shell:
"{HYPHY} {STRIKE_AMBIGS_BF} --alignment {input.input_msa} --output {output.output}"
#end rule
# remove duplicate sequences
rule remove_duplicates_msa:
input:
input_msa = rules.strike_ambigs_msa.output.output
output:
output = os.path.join(OUTDIR, "{GENE}.RD.SA.codons.cln.fa")
shell:
"{HYPHY} {REMOVE_DUPS_BF} --msa {input.input_msa} --output {output.output} ENV='DATA_FILE_PRINT_FORMAT=9'"
#end rule
# trimal for automated alignment trimming
rule trimal:
input:
input = rules.remove_duplicates_msa.output.output
output:
output = os.path.join(OUTDIR, "{GENE}.RD.SA.codons.cln.trim67.fa")
shell:
"/home/agselberg/trimal/source/trimal -in {input.input} -out {output.output} -gt 0.67"
#end rule
# raxml for gene trees
rule raxml_ng_msa:
params:
THREADS = 1 # note: this will not run if too many threads
input:
input = rules.trimal.output.output
output:
output = os.path.join(OUTDIR, "{GENE}.RD.SA.codons.cln.trim67.fa.raxml.bestTree")
shell:
"raxml-ng --model GTR+G --msa {input.input} --threads {params.THREADS} --seed 2 --redo"
#end rule
# label gene trees with FOREGROUND species/nodes
rule label_tree_fg:
input:
tree = rules.raxml_ng_msa.output.output,
partition_file = os.path.join(BASEDIR, "data", "Partitions", "{P}_FG.txt"),
output:
output = os.path.join(OUTDIR, "{GENE}.RD.SA.codons.cln.trim67.fa.raxml.bestTree.labeled_fgOnly_{P}.nwk")
shell:
"{HYPHY} {LABEL_TREE_BF} --tree {input.tree} --list {input.partition_file} --output {output.output} --internal-nodes 'Parsimony' --label FOREGROUND"
#end rule
# label gene trees with NUISANCE species/nodes
rule label_tree_nu:
input:
tree = rules.label_tree_fg.output.output,
partition_file = os.path.join(BASEDIR, "data", "Partitions", "{P}_Nuisance.txt")
output:
output = os.path.join(OUTDIR, "{GENE}.RD.SA.codons.cln.trim67.fa.raxml.bestTree.labeled_fg-nuOnly_{P}.nwk")
shell:
"{HYPHY} {LABEL_TREE_BF} --tree {input.tree} --list {input.partition_file} --output {output.output} --internal-nodes 'Parsimony' --label NUISANCE"
#end rule
# label all unlabeled branches on gene trees as BACKGROUND
rule label_tree_bg:
input:
tree = rules.label_tree_nu.output.output,
output:
output = os.path.join(OUTDIR, "{GENE}.RD.SA.codons.cln.trim67.fa.raxml.bestTree.labeled_{P}.nwk")
shell:
"python {LABEL_BG} {input.tree} {output.output}"
#end rule
#----------------------------------------------------------------------------
# Run BUSTED WITHOUT ERROR SINK COMPONENT
#----------------------------------------------------------------------------
## RUN BUSTED ON ALL BRANCHES##
rule busted:
input:
seq = rules.trimal.output.output,
tree = rules.raxml_ng_msa.output.output
output:
json = os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.BUSTED.json"),
fits = os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.BUSTED-fit.lf"),
int_fits = os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.BUSTED-intfit.json")
shell:
"""
{HYPHY} BUSTED --alignment {input.seq} --tree {input.tree} --srv Yes --starting-points 10 --eror-sink No --save-fit {output.fits} --intermediate-fits {output.int_fits} --output {output.json} ENV='TOLERATE_NUMERICAL_ERRORS=1'
"""
###end rule
#----------------------------------------------------------------------------
# Run BUSTED WITH ERROR SINK COMPONENT
#----------------------------------------------------------------------------
## RUN BUSTED-E ON ALL BRANCHES##
rule busted_e:
input:
seq = rules.trimal.output.output,
tree = rules.raxml_ng_msa.output.output,
int_fits = rules.busted.output.int_fits
output:
json = os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.BUSTED-E.json"),
fits = os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.BUSTED-E-fit.lf")
shell:
"""
{HYPHY} BUSTED --alignment {input.seq} --tree {input.tree} --srv Yes --starting-points 10 --error-sink Yes --save-fit {output.fits} --intermediate-fits {input.int_fits} --output {output.json} ENV='TOLERATE_NUMERICAL_ERRORS=1;'
"""
###end rule
# filter script to convert BUSTED-E to filtered fas file
rule filter:
input:
e_json = rules.busted_e.output.json
output:
json = os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.FILTER.json"),
seq = os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.FILTER.fas")
shell:
"""
{HYPHY} error-filter {input.e_json} --output {output.seq} --output-json {output.json};
"""
###end rule
# obtain tree from filtered fas file
rule get_filtered_tree:
input:
seq = rules.filter.output.seq
output:
seq = os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.FILTER.fasta"),
tree = os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.FILTER.nwk"),
shell:
"""
head -n -1 {input.seq} > {output.seq};
tail -n 1 {input.seq} > {output.tree}
"""
# label FOREGROUND branches/nodes in tree after alignment filtering
rule label_filtered_fg:
input:
tree = rules.get_filtered_tree.output.tree,
partition_file = os.path.join(BASEDIR, "data", "Partitions", "{P}_FG.txt"),
output:
output = os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.FILTERED.labeled_fgOnly_{P}.nwk")
shell:
"{HYPHY} {LABEL_TREE_BF} --tree {input.tree} --list {input.partition_file} --output {output.output} --internal-nodes 'Parsimony' --label FOREGROUND"
#end rule
# label NUISANCE branches/nodes in tree after alignment filtering
rule label_filtered_nu:
input:
tree = rules.label_filtered_fg.output.output,
partition_file = os.path.join(BASEDIR, "data", "Partitions", "{P}_Nuisance.txt")
output:
output = os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.FILTERED.labeled_fg-nuOnly_{P}.nwk")
shell:
"{HYPHY} {LABEL_TREE_BF} --tree {input.tree} --list {input.partition_file} --output {output.output} --internal-nodes 'Parsimony' --label NUISANCE"
#end rule
# label all remaining branches/nodes in tree (after alignment filtering) as BACKGROUND
rule label_filtered_bg:
input:
tree = rules.label_filtered_nu.output.output,
output:
output = os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.FILTERED.labeled_{P}.nwk")
shell:
"python {LABEL_BG} {input.tree} {output.output}"
#end rule
###end rule
#----------------------------------------------------------------------------
# Run BUSTED-PH
#----------------------------------------------------------------------------
## RUN BUSTED-PH WITH FILTERED FASTA##
rule busted_ph_filtered:
input:
seq = rules.get_filtered_tree.output.seq,
tree = rules.label_filtered_bg.output.output
output:
json = os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.{P}.filtered.BUSTED-PH.json"),
shell:
"""
{HYPHY} {BUSTED_PH_BF} --alignment {input.seq} --tree {input.tree} --srv Yes --starting-points 10 --branches FOREGROUND --output {output.json} --comparison BACKGROUND ENV='TOLERATE_NUMERICAL_ERRORS=1'
"""
#end rule
## RUN BUSTED-PH WITH UNFILTERED FASTA##
rule busted_ph_unfiltered:
input:
seq = rules.trimal.output.output,
tree = rules.label_tree_bg.output.output
output:
json = os.path.join(OUTDIR_BUSTED, "{GENE}.RD.SA.codons.cln.trim67.fa.{P}.BUSTED-PH.json"),
shell:
"""
{HYPHY} {BUSTED_PH_BF} --alignment {input.seq} --tree {input.tree} --srv Yes --starting-points 10 --branches FOREGROUND --comparison BACKGROUND --output {output.json} ENV='TOLERATE_NUMERICAL_ERRORS=1'
"""
#end rule