Mercurial > repos > tiagoantao > raxml
comparison raxml.py @ 0:66bcdd1ac9ca draft
planemo upload commit 2c71c5e5604b9c05648b883fddbb163a8e03c807
author | tiagoantao |
---|---|
date | Tue, 03 Nov 2015 16:27:41 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:66bcdd1ac9ca |
---|---|
1 #!/usr/bin/env python | |
2 """ | |
3 Runs RAxML on a sequence file. | |
4 For use with RAxML version 8.2.4 | |
5 """ | |
6 import os, shutil, subprocess, sys, optparse, fnmatch, glob | |
7 | |
8 def stop_err(msg): | |
9 sys.stderr.write("%s\n" % msg) | |
10 sys.exit() | |
11 | |
12 def getint(name): | |
13 basename = name.partition('RUN.') | |
14 if basename[2] != '': | |
15 num = basename[2] | |
16 return int(num) | |
17 | |
18 def __main__(): | |
19 usage = "usage: %prog -T <threads> -s <input> -n <output> -m <model> [optional arguments]" | |
20 | |
21 # Parse the primary wrapper's command line options | |
22 parser = optparse.OptionParser(usage = usage) | |
23 # raxml binary name, hardcoded in the xml file | |
24 parser.add_option("--binary", action="store", type="string", dest="binary", help="Command to run") | |
25 # (-a) | |
26 parser.add_option("--weightfile", action="store", type="string", dest="weightfile", help="Column weight file") | |
27 # (-A) | |
28 parser.add_option("--secondary_structure_model", action="store", type="string", dest="secondary_structure_model", help="Secondary structure model") | |
29 # (-b) | |
30 parser.add_option("--bootseed", action="store", type="int", dest="bootseed", help="Bootstrap random number seed") | |
31 # (-c) | |
32 parser.add_option("--numofcats", action="store", type="int", dest="numofcats", help="Number of distinct rate categories") | |
33 # (-d) | |
34 parser.add_option("--search_complete_random_tree", action="store_true", dest="search_complete_random_tree", help="Search with a complete random starting tree") | |
35 # (-D) | |
36 parser.add_option("--ml_search_convergence", action="store_true", dest="ml_search_convergence", help="ML search onvergence criterion") | |
37 # (-e) | |
38 parser.add_option("--model_opt_precision", action="store", type="float", dest="model_opt_precision", help="Model Optimization Precision (-e)") | |
39 # (-E) | |
40 parser.add_option("--excludefile", action="store", type="string", dest="excludefile", help="Exclude File Name") | |
41 # (-f) | |
42 parser.add_option("--search_algorithm", action="store", type="string", dest="search_algorithm", help="Search Algorithm") | |
43 # (-F) | |
44 parser.add_option("--save_memory_cat_model", action="store_true", dest="save_memory_cat_model", help="Save memory under CAT and GTRGAMMA models") | |
45 # (-g) | |
46 parser.add_option("--groupingfile", action="store", type="string", dest="groupingfile", help="Grouping File Name") | |
47 # (-G) | |
48 parser.add_option("--enable_evol_heuristics", action="store_true", dest="enable_evol_heuristics", help="Enable evol algo heuristics") | |
49 # (-i) | |
50 parser.add_option("--initial_rearrangement_setting", action="store", type="int", dest="initial_rearrangement_setting", help="Initial Rearrangement Setting") | |
51 # (-I) | |
52 parser.add_option("--posterior_bootstopping_analysis", action="store", type="string", dest="posterior_bootstopping_analysis", help="Posterior bootstopping analysis") | |
53 # (-J) | |
54 parser.add_option("--majority_rule_consensus", action="store", type="string", dest="majority_rule_consensus", help="Majority rule consensus") | |
55 # (-k) | |
56 parser.add_option("--print_branch_lengths", action="store_true", dest="print_branch_lengths", help="Print branch lengths") | |
57 # (-K) | |
58 parser.add_option("--multistate_sub_model", action="store", type="string", dest="multistate_sub_model", help="Multistate substitution model") | |
59 # (-m) | |
60 parser.add_option("--model_type", action="store", type="string", dest="model_type", help="Model Type") | |
61 parser.add_option("--base_model", action="store", type="string", dest="base_model", help="Base Model") | |
62 parser.add_option("--aa_empirical_freq", action="store_true", dest="aa_empirical_freq", help="Use AA Empirical base frequences") | |
63 parser.add_option("--aa_search_matrix", action="store", type="string", dest="aa_search_matrix", help="AA Search Matrix") | |
64 # (-n) | |
65 parser.add_option("--name", action="store", type="string", dest="name", help="Run Name") | |
66 # (-N/#) | |
67 parser.add_option("--number_of_runs", action="store", type="int", dest="number_of_runs", help="Number of alternative runs") | |
68 parser.add_option("--number_of_runs_bootstop", action="store", type="string", dest="number_of_runs_bootstop", help="Number of alternative runs based on the bootstop criteria") | |
69 # (-M) | |
70 parser.add_option("--estimate_individual_branch_lengths", action="store_true", dest="estimate_individual_branch_lengths", help="Estimate individual branch lengths") | |
71 # (-o) | |
72 parser.add_option("--outgroup_name", action="store", type="string", dest="outgroup_name", help="Outgroup Name") | |
73 # (-O) | |
74 parser.add_option("--disable_undetermined_seq_check", action="store_true", dest="disable_undetermined_seq_check", help="Disable undetermined sequence check") | |
75 # (-p) | |
76 parser.add_option("--random_seed", action="store", type="int", dest="random_seed", help="Random Number Seed") | |
77 # (-P) | |
78 parser.add_option("--external_protein_model", action="store", type="string", dest="external_protein_model", help="External Protein Model") | |
79 # (-q) | |
80 parser.add_option("--multiple_model", action="store", type="string", dest="multiple_model", help="Multiple Model File") | |
81 # (-r) | |
82 parser.add_option("--constraint_file", action="store", type="string", dest="constraint_file", help="Constraint File") | |
83 # (-R) | |
84 parser.add_option("--bin_model_parameter_file", action="store", type="string", dest="bin_model_parameter_file", help="Constraint File") | |
85 # (-s) | |
86 parser.add_option("--source", action="store", type="string", dest="source", help="Input file") | |
87 # (-S) | |
88 parser.add_option("--secondary_structure_file", action="store", type="string", dest="secondary_structure_file", help="Secondary structure file") | |
89 # (-t) | |
90 parser.add_option("--starting_tree", action="store", type="string", dest="starting_tree", help="Starting Tree") | |
91 # (-T) | |
92 parser.add_option("--threads", action="store", type="int", dest="threads", help="Number of threads to use") | |
93 # (-u) | |
94 parser.add_option("--use_median_approximation", action="store_true", dest="use_median_approximation", help="Use median approximation") | |
95 # (-U) | |
96 parser.add_option("--save_memory_gappy_alignments", action="store_true", dest="save_memory_gappy_alignments", help="Save memory in large gapped alignments") | |
97 # (-V) | |
98 parser.add_option("--disable_rate_heterogeneity", action="store_true", dest="disable_rate_heterogeneity", help="Disable rate heterogeneity") | |
99 # (-W) | |
100 parser.add_option("--sliding_window_size", action="store", type="string", dest="sliding_window_size", help="Sliding window size") | |
101 # (-x) | |
102 parser.add_option("--rapid_bootstrap_random_seed", action="store", type="int", dest="rapid_bootstrap_random_seed", help="Rapid Boostrap Random Seed") | |
103 # (-y) | |
104 parser.add_option("--parsimony_starting_tree_only", action="store_true", dest="parsimony_starting_tree_only", help="Generate a parsimony starting tree only") | |
105 # (-z) | |
106 parser.add_option("--file_multiple_trees", action="store", type="string", dest="file_multiple_trees", help="Multiple Trees File") | |
107 | |
108 (options, args) = parser.parse_args() | |
109 cmd = [] | |
110 | |
111 # Required parameters | |
112 binary = options.binary | |
113 cmd.append(binary) | |
114 # Threads | |
115 threads = "-T %d" % options.threads | |
116 cmd.append(threads) | |
117 # Source | |
118 source = "-s %s" % options.source | |
119 cmd.append(source) | |
120 #Hardcode to "galaxy" first to simplify the output part of the wrapper | |
121 #name = "-n %s" % options.name | |
122 name = "-n galaxy" | |
123 cmd.append(name) | |
124 ## Model | |
125 model_type = options.model_type | |
126 base_model = options.base_model | |
127 aa_search_matrix = options.aa_search_matrix | |
128 aa_empirical_freq = options.aa_empirical_freq | |
129 if model_type == 'aminoacid': | |
130 model = "-m %s%s" % (base_model, aa_search_matrix) | |
131 if aa_empirical_freq: | |
132 model = "-m %s%s%s" % (base_model, aa_search_matrix, 'F') | |
133 # (-P) | |
134 if options.external_protein_model: | |
135 external_protein_model = "-P %s" % options.external_protein_model | |
136 cmd.append(external_protein_model) | |
137 else: | |
138 model = "-m %s" % base_model | |
139 cmd.append(model) | |
140 if model == "GTRCAT": | |
141 # (-c) | |
142 if options.numofcats: | |
143 numofcats = "-c %d" % options.numofcats | |
144 cmd.append(numofcats) | |
145 # Optional parameters | |
146 if options.number_of_runs_bootstop: | |
147 number_of_runs_bootstop = "-N %s" % options.number_of_runs_bootstop | |
148 cmd.append(number_of_runs_bootstop) | |
149 else: | |
150 number_of_runs_bootstop = '' | |
151 if options.number_of_runs: | |
152 number_of_runs_opt = "-N %d" % options.number_of_runs | |
153 cmd.append(number_of_runs_opt) | |
154 else: | |
155 number_of_runs_opt = 0 | |
156 # (-a) | |
157 if options.weightfile: | |
158 weightfile = "-a %s" % options.weightfile | |
159 cmd.append(weightfile) | |
160 # (-A) | |
161 if options.secondary_structure_model: | |
162 secondary_structure_model = "-A %s" % options.secondary_structure_model | |
163 cmd.append(secondary_structure_model ) | |
164 # (-b) | |
165 if options.bootseed: | |
166 bootseed = "-b %d" % options.bootseed | |
167 cmd.append(bootseed) | |
168 else: | |
169 bootseed = 0 | |
170 # -C - doesn't work in pthreads version, skipped | |
171 if options.search_complete_random_tree: | |
172 cmd.append("-d") | |
173 if options.ml_search_convergence: | |
174 cmd.append("-D" ) | |
175 if options.model_opt_precision: | |
176 model_opt_precision = "-e %f" % options.model_opt_precision | |
177 cmd.append(model_opt_precision) | |
178 if options.excludefile: | |
179 excludefile = "-E %s" % options.excludefile | |
180 cmd.append(excludefile) | |
181 if options.search_algorithm: | |
182 search_algorithm = "-f %s" % options.search_algorithm | |
183 cmd.append(search_algorithm) | |
184 if options.save_memory_cat_model: | |
185 cmd.append("-F") | |
186 if options.groupingfile: | |
187 groupingfile = "-g %s" % options.groupingfile | |
188 cmd.append(groupingfile) | |
189 if options.enable_evol_heuristics: | |
190 enable_evol_heuristics = "-G %f" % options.enable_evol_heuristics | |
191 cmd.append(enable_evol_heuristics ) | |
192 if options.initial_rearrangement_setting: | |
193 initial_rearrangement_setting = "-i %s" % options.initial_rearrangement_setting | |
194 cmd.append(initial_rearrangement_setting) | |
195 if options.posterior_bootstopping_analysis: | |
196 posterior_bootstopping_analysis = "-I %s" % options.posterior_bootstopping_analysis | |
197 cmd.append(posterior_bootstopping_analysis) | |
198 if options.majority_rule_consensus: | |
199 majority_rule_consensus = "-J %s" % options.majority_rule_consensus | |
200 cmd.append(majority_rule_consensus) | |
201 if options.print_branch_lengths: | |
202 cmd.append("-k") | |
203 if options.multistate_sub_model: | |
204 multistate_sub_model = "-K %s" % options.multistate_sub_model | |
205 cmd.append(multistate_sub_model) | |
206 if options.estimate_individual_branch_lengths: | |
207 cmd.append("-M") | |
208 if options.outgroup_name: | |
209 outgroup_name = "-o %s" % options.outgroup_name | |
210 cmd.append(outgroup_name) | |
211 if options.disable_undetermined_seq_check: | |
212 cmd.append("-O") | |
213 if options.random_seed: | |
214 random_seed = "-p %d" % options.random_seed | |
215 cmd.append(random_seed) | |
216 multiple_model = None | |
217 if options.multiple_model: | |
218 multiple_model = "-q %s" % options.multiple_model | |
219 cmd.append(multiple_model) | |
220 if options.constraint_file: | |
221 constraint_file = "-r %s" % options.constraint_file | |
222 cmd.append(constraint_file) | |
223 if options.bin_model_parameter_file: | |
224 bin_model_parameter_file_name = "RAxML_binaryModelParameters.galaxy" | |
225 os.symlink(options.bin_model_parameter_file, bin_model_parameter_file_name ) | |
226 bin_model_parameter_file = "-R %s" % options.bin_model_parameter_file | |
227 #Needs testing. Is the hardcoded name or the real path needed? | |
228 cmd.append(bin_model_parameter_file) | |
229 if options.secondary_structure_file: | |
230 secondary_structure_file = "-S %s" % options.secondary_structure_file | |
231 cmd.append(secondary_structure_file) | |
232 if options.starting_tree: | |
233 starting_tree = "-t %s" % options.starting_tree | |
234 cmd.append(starting_tree) | |
235 if options.use_median_approximation: | |
236 cmd.append("-u") | |
237 if options.save_memory_gappy_alignments: | |
238 cmd.append("-U") | |
239 if options.disable_rate_heterogeneity: | |
240 cmd.append("-V") | |
241 if options.sliding_window_size: | |
242 sliding_window_size = "-W %d" % options.sliding_window_size | |
243 cmd.append(sliding_window_size) | |
244 if options.rapid_bootstrap_random_seed: | |
245 rapid_bootstrap_random_seed = "-x %d" % options.rapid_bootstrap_random_seed | |
246 cmd.append(rapid_bootstrap_random_seed) | |
247 else: | |
248 rapid_bootstrap_random_seed = 0 | |
249 if options.parsimony_starting_tree_only: | |
250 cmd.append("-y") | |
251 if options.file_multiple_trees: | |
252 file_multiple_trees = "-z %s" % options.file_multiple_trees | |
253 cmd.append(file_multiple_trees) | |
254 | |
255 print "cmd list: ", cmd, "\n" | |
256 | |
257 full_cmd = " ".join(cmd) | |
258 print "Command string: %s" % full_cmd | |
259 | |
260 try: | |
261 proc = subprocess.Popen(args=full_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
262 except Exception, err: | |
263 sys.stderr.write("Error invoking command: \n%s\n\n%s\n" % (cmd, err)) | |
264 sys.exit(1) | |
265 stdout, stderr = proc.communicate() | |
266 return_code = proc.returncode | |
267 if return_code: | |
268 sys.stdout.write(stdout) | |
269 sys.stderr.write(stderr) | |
270 sys.stderr.write("Return error code %i from command:\n" % return_code) | |
271 sys.stderr.write("%s\n" % cmd) | |
272 else: | |
273 sys.stdout.write(stdout) | |
274 sys.stdout.write(stderr) | |
275 | |
276 #Multiple runs - concatenate | |
277 if number_of_runs_opt > 0: | |
278 if (bootseed == 0) and (rapid_bootstrap_random_seed == 0 ): | |
279 runfiles = glob.glob('RAxML*RUN*') | |
280 runfiles.sort(key=getint) | |
281 # Logs | |
282 outfile = open('RAxML_log.galaxy','w') | |
283 for filename in runfiles: | |
284 if fnmatch.fnmatch(filename, 'RAxML_log.galaxy.RUN.*'): | |
285 infile = open(filename, 'r') | |
286 filename_line = "%s\n" % filename | |
287 outfile.write(filename_line) | |
288 for line in infile: | |
289 outfile.write(line) | |
290 infile.close() | |
291 outfile.close() | |
292 # Parsimony Trees | |
293 outfile = open('RAxML_parsimonyTree.galaxy','w') | |
294 for filename in runfiles: | |
295 if fnmatch.fnmatch(filename, 'RAxML_parsimonyTree.galaxy.RUN.*'): | |
296 infile = open(filename, 'r') | |
297 filename_line = "%s\n" % filename | |
298 outfile.write(filename_line) | |
299 for line in infile: | |
300 outfile.write(line) | |
301 infile.close() | |
302 outfile.close() | |
303 # Results | |
304 outfile = open('RAxML_result.galaxy','w') | |
305 for filename in runfiles: | |
306 if fnmatch.fnmatch(filename, 'RAxML_result.galaxy.RUN.*'): | |
307 infile = open(filename, 'r') | |
308 filename_line = "%s\n" % filename | |
309 outfile.write(filename_line) | |
310 for line in infile: | |
311 outfile.write(line) | |
312 infile.close() | |
313 outfile.close() | |
314 # Multiple Model Partition Files | |
315 if multiple_model: | |
316 files = glob.glob('RAxML_bestTree.galaxy.PARTITION.*') | |
317 if len(files) > 0: | |
318 files.sort(key=getint) | |
319 outfile = open('RAxML_bestTreePartitions.galaxy','w') | |
320 # Best Tree Partitions | |
321 for filename in files: | |
322 if fnmatch.fnmatch(filename, 'RAxML_bestTree.galaxy.PARTITION.*'): | |
323 infile = open(filename, 'r') | |
324 filename_line = "%s\n" % filename | |
325 outfile.write(filename_line) | |
326 for line in infile: | |
327 outfile.write(line) | |
328 infile.close() | |
329 outfile.close() | |
330 else: | |
331 outfile = open('RAxML_bestTreePartitions.galaxy','w') | |
332 outfile.write("No partition files were produced.\n") | |
333 outfile.close() | |
334 | |
335 # Result Partitions | |
336 files = glob.glob('RAxML_result.galaxy.PARTITION.*') | |
337 if len(files) > 0: | |
338 files.sort(key=getint) | |
339 outfile = open('RAxML_resultPartitions.galaxy','w') | |
340 for filename in files: | |
341 if fnmatch.fnmatch(filename, 'RAxML_result.galaxy.PARTITION.*'): | |
342 infile = open(filename, 'r') | |
343 filename_line = "%s\n" % filename | |
344 outfile.write(filename_line) | |
345 for line in infile: | |
346 outfile.write(line) | |
347 infile.close() | |
348 outfile.close() | |
349 else: | |
350 outfile = open('RAxML_resultPartitions.galaxy','w') | |
351 outfile.write("No partition files were produced.\n") | |
352 outfile.close() | |
353 | |
354 # DEBUG options | |
355 infof = open('RAxML_info.galaxy','a') | |
356 infof.write('\nOM: CLI options DEBUG START:\n') | |
357 infof.write(options.__repr__()) | |
358 infof.write('\nOM: CLI options DEBUG END\n') | |
359 | |
360 if __name__=="__main__": __main__() |