annotate beast.py @ 4:2ca3df65222b draft default tip

Uploaded to the correct location
author malex
date Thu, 26 Apr 2012 11:12:24 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
1 #!/usr/bin/env python
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
2
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
3 """
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
4 Copyright 2012 Oleksandr Moskalenko <om@hpc.ufl.edu>
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
5
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
6 Runs BEAST on an input XML file.
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
7 For use with BEAST version 1.7.1
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
8
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
9 usage: beast.py inputxml
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
10
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
11 Produces:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
12 output log file
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
13 mcmc.operators file
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
14 A variable number of '.tree' files depending on the XML input
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
15 """
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
16 import os, shutil, subprocess, sys, optparse, glob, string
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
17 from xml.dom.minidom import parse, Node
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
18
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
19 def stop_err(msg):
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
20 sys.stderr.write("%s\n" % msg)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
21 sys.exit()
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
22
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
23 def parseFnames(nodelist):
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
24 filenames = []
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
25 for node in nodelist:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
26 if node.hasAttributes():
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
27 fname = node.getAttribute('fileName')
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
28 if fname != "":
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
29 filenames.append(fname)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
30 else:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
31 pass
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
32 return filenames
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
33
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
34 def __main__():
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
35 usage = "usage: %prog inputXML"
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
36 parser = optparse.OptionParser(usage = usage)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
37 parser.add_option("-T", "--threads", action="store", type="string", dest="threads", help="Number of threads")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
38 parser.add_option("-s", "--seed", action="store", type="string",
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
39 dest="seed", help="Random seed")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
40 parser.add_option("-r", "--strict", action="store_true", dest="strict", help="Strict XML parsing")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
41 parser.add_option("-e", "--errors", action="store", type="string",
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
42 dest="errors", help="Maximum number of errors allowed")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
43 parser.add_option("-i", "--inputxml", action="store", type="string", dest="inputxml", help="Input XML")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
44 parser.add_option("-o", "--operators", action="store", type="string", dest="operators", help="Operators")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
45 parser.add_option("-l", "--logs", action="store", type="string", dest="logs", help="Logs")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
46 parser.add_option("-t", "--trees", action="store", type="string", dest="trees", help="Trees")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
47 parser.add_option("-d", "--id", action="store", type="string", dest="treeid", help="Tree ID")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
48 parser.add_option("-p", "--path", action="store", type="string", dest="path", help="New file path")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
49 (options, args) = parser.parse_args()
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
50 if options.threads == None:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
51 threads = 1
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
52 else:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
53 threads = int(options.threads)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
54 if options.seed != None:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
55 seed = int(options.seed)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
56 else:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
57 seed = 12345
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
58 if options.strict == "-strict":
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
59 print "Strict XML check was chosen\n"
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
60 strict = True
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
61 print "No strict XML check was chosen\n"
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
62 else:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
63 strict = False
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
64 inputxml = options.inputxml
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
65 operators = options.operators
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
66 logs = options.logs
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
67 trees = options.trees
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
68 errors = options.errors
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
69 treefile_id = options.treeid
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
70 newfilepath = options.path
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
71 sys.stdout.write("The following parameters have been provided:\n")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
72 sys.stdout.write("Input XML: %s\n" % inputxml)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
73 sys.stdout.write("Operators: %s\n" % operators)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
74 sys.stdout.write("Logs: %s\n" % logs)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
75 sys.stdout.write("Trees: %s\n" % trees)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
76 sys.stdout.write("Strict: %s\n" % strict)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
77 sys.stdout.write("New file path: %s\n" % newfilepath)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
78 sys.stdout.write("Tree file ID: %s\n" % treefile_id)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
79 if errors != None:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
80 sys.stdout.write("Errors: %s\n" % errors)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
81 cmd = []
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
82 cmd.append('beast')
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
83 if strict == True:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
84 cmd.append('-strict')
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
85 thread_opt = "-threads %d" % threads
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
86 sys.stdout.write("Threads: %s\n" % thread_opt)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
87 cmd.append(thread_opt)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
88 cmd.append('-seed %d' % int(seed))
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
89 if errors != None:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
90 cmd.append('-errors %d' % int(errors))
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
91 cmd.append(inputxml)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
92 try:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
93 proc = subprocess.Popen(args=cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
94 except Exception, err:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
95 sys.stderr.write("Error invoking command: \n%s\n\n%s\n" % (cmd, err))
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
96 sys.exit(1)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
97 stdout, stderr = proc.communicate()
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
98 return_code = proc.returncode
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
99 if return_code:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
100 sys.stdout.write(stdout)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
101 sys.stderr.write(stderr)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
102 sys.stderr.write("Return error code %i from command:\n" % return_code)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
103 sys.stderr.write("%s\n" % cmd)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
104 sys.exit(1)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
105 else:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
106 sys.stdout.write(stdout)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
107 sys.stdout.write(stderr)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
108 #2012-04-24 - 2nd approach, parse the .xml file:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
109 xml_file = os.path.abspath(inputxml)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
110 if not os.path.exists(inputxml):
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
111 sys.stderr.write("Cannot find the input XML file for parsing.\n")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
112 dom = parse(inputxml)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
113 xml_logs = dom.getElementsByTagName('log')
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
114 xml_trees = dom.getElementsByTagName('logTree')
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
115 logfiles_orig = parseFnames(xml_logs)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
116 treefiles_orig = parseFnames(xml_trees)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
117 try:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
118 if len(logfiles_orig) == 0:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
119 logfiles_orig = glob.glob("*.log*")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
120 if len(logfiles_orig) == 0:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
121 logfiles_orig.append('Error_no_log')
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
122 dummy_file = open('Error_no_log','w')
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
123 dummy_file.write("BEAST run has not produced a log or it's named in such a way that I can't locate it. Configure BEAST to produce .log files without spaces in their names and rerun the analysis.\n")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
124 dummy_file.close()
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
125 logfiles = []
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
126 if os.path.isdir(newfilepath):
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
127 for filename in logfiles_orig:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
128 if os.path.isfile(filename):
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
129 name = string.replace(os.path.splitext(filename)[0], "_", "-")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
130 filestring = "primary_%s_%s_visible_nexus" % (treefile_id, name)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
131 newpath = os.path.join(newfilepath,filestring)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
132 logfiles.append(newpath)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
133 # else:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
134 # sys.stderr.write("Can't find the log file to rename.\n")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
135 logfiles[0] = logs
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
136 for i in range(len(logfiles_orig)):
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
137 src = logfiles_orig[i]
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
138 dst = logfiles[i]
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
139 if os.path.exists(src):
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
140 shutil.copy(src, dst)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
141 # else:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
142 # print "File '%s' can't be found.\n" % src
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
143 except Exception, err:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
144 sys.stderr.write("Error copying log file: \n%s\n" % err)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
145 try:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
146 if not os.path.exists('mcmc.operators'):
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
147 bat = open('mcmc.operators','w')
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
148 bat.write('The mcmc.operators file did not have any output.\n')
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
149 bat.close()
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
150 except Exception, err:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
151 sys.stderr.write("Error copying mcmc.operators file: \n%s\n" % err)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
152 try:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
153 if len(treefiles_orig) == 0:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
154 print "No tree files found by the xml file parser.\n"
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
155 treefiles_orig = glob.glob("*.trees*")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
156 # print "Original tree files from the directory:\n\t%s" % " ".join(treefiles_orig)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
157 if len(treefiles_orig) == 0:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
158 treefiles_orig.append('Error_no_tree')
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
159 dummy_file = open('Error_no_tree','w')
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
160 dummy_file.write("BEAST run has not produced an output tree or it's named in such a way that I can't locate it. Configure BEAST to produce .tree files without spaces in their names and rerun the analysis.\n")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
161 dummy_file.close()
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
162 treefiles = []
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
163 if os.path.isdir(newfilepath):
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
164 for filename in treefiles_orig:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
165 if os.path.isfile(filename):
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
166 name = string.replace(os.path.splitext(filename)[0], "_", "-")
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
167 filestring = "primary_%s_%s_visible_nexus" % (treefile_id, name)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
168 newpath = os.path.join(newfilepath,filestring)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
169 treefiles.append(newpath)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
170 treefiles[0] = trees
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
171 for i in range(len(treefiles_orig)):
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
172 src = treefiles_orig[i]
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
173 dst = treefiles[i]
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
174 if os.path.exists(src):
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
175 shutil.copy(src, dst)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
176 except Exception, err:
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
177 sys.stderr.write("Error copying trees file(s): \n%s\n" % err)
2ca3df65222b Uploaded to the correct location
malex
parents:
diff changeset
178 if __name__=="__main__": __main__()