-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexaml-helper.py
220 lines (203 loc) · 5.77 KB
/
examl-helper.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
(c) 2014 Brant Faircloth || http://faircloth-lab.org/
All rights reserved.
This code is distributed under a 3-clause BSD license. Please see
LICENSE.txt for more information.
Created on 28 February 2014 09:37 PST (-0800)
"""
import os
import re
import random
import argparse
import subprocess
from phyluce.helpers import FullPaths, CreateDir, is_dir, is_file
from phyluce.log import setup_logging
import pdb
RAXML = "/Users/bcf/git/standard-RAxML/raxmlHPC-SSE3"
EXAML = "/Users/bcf/git/examl/examl/examl"
PARSER = "/Users/bcf/git/examl/parser/parser"
def get_args():
"""Get arguments from CLI"""
parser = argparse.ArgumentParser(
description="""Run standard tree inference using raxml-light/examl"""
)
parser.add_argument(
"--phylip",
required=True,
type=is_file,
action=FullPaths,
help="""The phylip file you want to run raxml against."""
)
parser.add_argument(
"--output",
required=True,
action=CreateDir,
help="""The output directory in which to store results."""
)
parser.add_argument(
"--model",
choices = ["GTRGAMMA", "GTRCAT"],
default="GTRGAMMA",
help="""The substitution model to use."""
)
parser.add_argument(
"--cores",
type=int,
default=2,
help="""The number of compute cores to use."""
)
parser.add_argument(
"--trees",
type=int,
default=20,
help="""The number of trees to search."""
)
parser.add_argument(
"--verbosity",
type=str,
choices=["INFO", "WARN", "CRITICAL"],
default="INFO",
help="""The logging level to use."""
)
parser.add_argument(
"--log-path",
action=FullPaths,
type=is_dir,
default=None,
help="""The path to a directory to hold logs."""
)
parser.add_argument(
"--flag",
action="store_true",
default=False,
help="""Help text""",
)
return parser.parse_args()
def convert_phylip_to_examl_binary(log, args):
log.info("Converting PHYLIP to binary format")
# make sure we're in output/working dir
os.chdir(args.output)
phylip_file_name = os.path.basename(args.phylip)
binary_file_name = "{}.unpartitioned".format(phylip_file_name)
binary_file_pth = os.path.join(args.output, binary_file_name)
cmd = [
PARSER,
"-s",
args.phylip,
"-m",
"DNA",
"-n",
binary_file_name
]
proc = subprocess.Popen(
cmd,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE
)
try:
stderr, stdout = proc.communicate()
except subprocess.CalledProcessError as e:
print(e.returncode)
print stderr
return "{}.binary".format(binary_file_pth)
def compute_starting_parsimony_tree(log, args, i, binary_file):
log.info("[Tree {}] Computing parsimony starting tree".format(i))
# make sure we're in output/working dir
os.chdir(args.output)
starting_tree = "{}.P{}.newick".format(
os.path.basename(binary_file),
i
)
starting_tree_pth = os.path.join(
args.output,
"RAxML_parsimonyTree.{}".format(starting_tree)
)
seed = random.randrange(0, 1000000)
log.info("[Tree {}] Parsimony seed is {}".format(i, seed))
# raxmlHPC-SSE3 -y -m GTRCAT -s dna.phy -p 12345 -n startingTree
cmd = [
RAXML,
"-y",
"-m",
args.model,
"-s",
args.phylip,
"-p",
str(seed),
"-n",
starting_tree
]
proc = subprocess.Popen(
cmd,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE
)
try:
stderr, stdout = proc.communicate()
except subprocess.CalledProcessError as e:
print(e.returncode)
print stderr
return seed, starting_tree_pth
def run_examl_against_binary_data(log, args, i, binary_file_pth, starting_tree_pth):
log.info("[Tree {}] Running examl using binary file and starting tree {}".format(
i,
os.path.basename(starting_tree_pth),
))
# make sure we're in output/working dir
os.chdir(args.output)
ml_tree_name = "{}.T{}".format(
os.path.basename(args.phylip),
i
)
# determine correct model
if args.model == "GTRCAT":
model = "PSR"
else:
model = "GAMMA"
# mpirun -np 8 examl -s 49.unpartitioned.binary -t 49.tree -m GAMMA -n T1
cmd = [
"mpirun",
"-np",
str(args.cores),
EXAML,
"-s",
binary_file_pth,
"-t",
starting_tree_pth,
"-m",
model,
"-n",
ml_tree_name
]
proc = subprocess.Popen(
cmd,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE
)
try:
stderr, stdout = proc.communicate()
ll_result = re.search("Likelihood\s+:\s(-\d+.\d+)", stderr)
ll = ll_result.groups()[0]
log.info("[Tree {}] LogLik {}".format(i, ll))
except subprocess.CalledProcessError as e:
print(e.returncode)
print stderr
def main():
args = get_args()
# setup logging
log, my_name = setup_logging(args)
# change to working dir
starting_dir = os.getcwd()
# convert data to binary
binary_file_pth = convert_phylip_to_examl_binary(log, args)
for iter in xrange(args.trees):
# compute starting tree on data
seed, starting_tree_pth = compute_starting_parsimony_tree(log, args, iter, binary_file_pth)
# run examl against binary data with starting tree
run_examl_against_binary_data(log, args, iter, binary_file_pth, starting_tree_pth)
# return to starting dir
os.chdir(starting_dir)
if __name__ == '__main__':
main()