annotate tools/mira_3_4/mira.py @ 3:c1c7d6f726b0 draft

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