annotate tools/mira_3_4/mira.py @ 9:5573d802e431 draft

Uploaded v0.0.8, MIT licence, RST for README, citation information, development moved to GitHub.
author peterjc
date Wed, 18 Sep 2013 06:22:19 -0400
parents 4266cccbb45a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
1 #!/usr/bin/env python
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
2 """A simple wrapper script to call MIRA and collect its output.
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
3 """
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
4 import os
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
5 import sys
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
6 import subprocess
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
7 import shutil
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
8 import time
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
9
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
10 WRAPPER_VER = "0.0.5" #Keep in sync with the XML file
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
11
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
12 def stop_err(msg, err=1):
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
13 sys.stderr.write(msg+"\n")
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
14 sys.exit(err)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
15
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
16
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
17 def get_version():
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
18 """Run MIRA to find its version number"""
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
19 # At the commend line I would use: mira -v | head -n 1
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
20 # however there is some pipe error when doing that here.
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
21 try:
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
22 child = subprocess.Popen(["mira", "-v"],
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
23 stdout=subprocess.PIPE,
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
24 stderr=subprocess.STDOUT)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
25 except Exception, err:
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
26 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err))
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
27 sys.exit(1)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
28 ver, tmp = child.communicate()
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
29 del child
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
30 return ver.split("\n", 1)[0]
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
31
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
32
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
33 mira_ver = get_version()
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
34 if "V3.4." not in mira_ver:
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
35 stop_err("This wrapper is for MIRA V3.4, not %s" % mira_ver)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
36 if "-v" in sys.argv:
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
37 print "MIRA wrapper version %s," % WRAPPER_VER
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
38 print mira_ver
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
39 sys.exit(0)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
40
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
41
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
42 def collect_output(temp, name):
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
43 n3 = (temp, name, name, name)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
44 f = "%s/%s_assembly/%s_d_results" % (temp, name, name)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
45 if not os.path.isdir(f):
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
46 stop_err("Missing output folder")
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
47 if not os.listdir(f):
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
48 stop_err("Empty output folder")
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
49 missing = []
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
50 for old, new in [("%s/%s_out.unpadded.fasta" % (f, name), out_fasta),
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
51 ("%s/%s_out.unpadded.fasta.qual" % (f, name), out_qual),
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
52 ("%s/%s_out.wig" % (f, name), out_wig),
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
53 ("%s/%s_out.caf" % (f, name), out_caf),
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
54 ("%s/%s_out.ace" % (f, name), out_ace)]:
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
55 if not os.path.isfile(old):
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
56 missing.append(os.path.splitext(old)[-1])
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
57 else:
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
58 shutil.move(old, new)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
59 if missing:
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
60 stop_err("Missing output files: %s" % ", ".join(missing))
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
61
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
62 def clean_up(temp, name):
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
63 folder = "%s/%s_assembly" % (temp, name)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
64 if os.path.isdir(folder):
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
65 shutil.rmtree(folder)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
66
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
67 #TODO - Run MIRA in /tmp or a configurable directory?
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
68 #Currently Galaxy puts us somewhere safe like:
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
69 #/opt/galaxy-dist/database/job_working_directory/846/
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
70 temp = "."
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
71 name, out_fasta, out_qual, out_ace, out_caf, out_wig, out_log = sys.argv[1:8]
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
72
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
73 start_time = time.time()
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
74 cmd_list =sys.argv[8:]
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
75 cmd = " ".join(cmd_list)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
76
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
77 assert os.path.isdir(temp)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
78 d = "%s_assembly" % name
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
79 assert not os.path.isdir(d), "Path %s already exists" % d
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
80 try:
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
81 #Check path access
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
82 os.mkdir(d)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
83 except Exception, err:
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
84 sys.stderr.write("Error making directory %s\n%s" % (d, err))
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
85 sys.exit(1)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
86
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
87 #print os.path.abspath(".")
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
88 #print cmd
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
89
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
90 handle = open(out_log, "w")
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
91 try:
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
92 #Run MIRA
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
93 child = subprocess.Popen(cmd_list,
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
94 stdout=handle,
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
95 stderr=subprocess.STDOUT)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
96 except Exception, err:
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
97 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err))
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
98 #TODO - call clean up?
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
99 handle.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err))
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
100 handle.close()
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
101 sys.exit(1)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
102 #Use .communicate as can get deadlocks with .wait(),
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
103 stdout, stderr = child.communicate()
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
104 assert not stdout and not stderr #Should be empty as sent to handle
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
105 run_time = time.time() - start_time
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
106 return_code = child.returncode
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
107 handle.write("\n\nMIRA took %0.2f minutes\n" % (run_time / 60.0))
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
108 print "MIRA took %0.2f minutes" % (run_time / 60.0)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
109 if return_code:
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
110 handle.write("Return error code %i from command:\n" % return_code)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
111 handle.write(cmd + "\n")
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
112 handle.close()
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
113 clean_up(temp, name)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
114 stop_err("Return error code %i from command:\n%s" % (return_code, cmd),
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
115 return_code)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
116 handle.close()
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
117
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
118 #print "Collecting output..."
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
119 collect_output(temp, name)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
120
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
121 #print "Cleaning up..."
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
122 clean_up(temp, name)
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
123
4266cccbb45a Uploaded v0.0.7 take 2, fixed path in installation
peterjc
parents:
diff changeset
124 print "Done"