-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprep_parsimony_trees.py
196 lines (177 loc) · 5.87 KB
/
prep_parsimony_trees.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
#!/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 02 March 2014 15:20 PST (-0800)
"""
import os
import shlex
import random
import argparse
import subprocess
from jinja2 import Environment, FileSystemLoader
from phyluce.helpers import FullPaths, CreateDir, is_dir, is_file
from phyluce.log import setup_logging
import pdb
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(
"--templates",
required=True,
action=FullPaths,
type=is_dir,
help="""The path to the queue submission templates."""
)
parser.add_argument(
"--trees",
required=True,
type=int,
default=20,
help="""The number of trees to search."""
)
parser.add_argument(
"--model",
choices = ["GAMMA", "PSR"],
default="GAMMA",
help="""The substitution model to use."""
)
parser.add_argument(
"--threads",
type=int,
default=1,
help="""The number of compute threads to use."""
)
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."""
)
return parser.parse_args()
def compute_starting_parsimony_tree(log, args, env, i):
log.info("[Parsimony Tree %s] Creating parsimony starting tree submit script" % i)
# make sure we're in output/working dir
starting_tree = "%s.P%s.newick" % (
os.path.basename(args.phylip),
i
)
submit_script = "raxml.parsimony.%s.P%s.sh" % (
os.path.basename(args.phylip),
i
)
submit_script_pth = os.path.join(args.output, submit_script)
#starting_tree_pth = os.path.join(
# args.output,
# "RAxML_parsimonyTree.{}".format(starting_tree)
#)
seed = random.randrange(0, 1000000)
log.info("[Parsimony Tree %s] Seed is %s" % (i, seed))
# raxmlHPC-SSE3 -y -m GTRCAT -s dna.phy -p 12345 -n startingTree
# write the submit script
template = env.get_template('prep-parsimony.sh')
if args.model == "PSR":
model = "GTRCAT"
else:
model = "GTRGAMMA"
formatted_result = template.render(
working_dir=args.output,
threads=args.threads,
model=model,
phylip=args.phylip,
seed=seed,
starting_tree=starting_tree
)
log.info("[Parsimony Tree %s] Writing submit script %s" % (i, submit_script))
outf = open(submit_script_pth, 'w')
outf.write(formatted_result)
outf.close()
return submit_script_pth
def submit_parsimony_job(log, args, env, i, submit_script_pth):
log.info("[Parsimony Tree %s] Setting up queueing command" % i)
template = env.get_template('prep-parsimony.submit')
command_string = template.render(
threads=args.threads,
submit_script=submit_script_pth
)
qsub = shlex.split(command_string)
log.info("[Parsimony Tree %s] Submitting job" % i)
stderr, stdout = subprocess.Popen(
qsub,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE
).communicate()
log.info("[Parsimony Tree %s] Queing info %s | %s" % (i, stdout.strip(), stderr.strip()))
def prep_parser_script(log, args, env):
log.info("[Binary phylip] Creating parser submit script")
submit_script = "%s.examl-parser.sh" % (
os.path.basename(args.phylip)
)
submit_script_pth = os.path.join(args.output, submit_script)
template = env.get_template('prep-binary.sh')
# the parser will add an extesion to the file after prepping the binary
formatted_result = template.render(
working_dir=args.output,
phylip=args.phylip,
binary_phylip=os.path.basename(args.phylip)
)
log.info("[Binary phylip] Writing submit script %s" % (submit_script))
outf = open(submit_script_pth, 'w')
outf.write(formatted_result)
outf.close()
return submit_script_pth
def submit_parser_job(log, args, env, submit_script_pth):
log.info("[Binary phylip] Setting up queueing command")
template = env.get_template('prep-binary.submit')
command_string = template.render(
submit_script=submit_script_pth
)
qsub = shlex.split(command_string)
log.info("[Binary phylip] Submitting job")
stderr, stdout = subprocess.Popen(
qsub,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE
).communicate()
log.info("[Binary phylip] Queing info %s | %s" % (stdout.strip(), stderr.strip()))
def main():
args = get_args()
# setup logging
log, my_name = setup_logging(args)
env = Environment(loader=FileSystemLoader(args.templates))
for i in xrange(args.trees):
submit_script_pth = compute_starting_parsimony_tree(log, args, env, i)
submit_parsimony_job(log, args, env, i, submit_script_pth)
# convert the phylip file to binary format
submit_script_pth = prep_parser_script(log, args, env)
submit_parser_job(log, args, env, submit_script_pth)
text = " Completed {} ".format(my_name)
log.info(text.center(65, "="))
if __name__ == '__main__':
main()