annotate tools/mira3/mira.py @ 12:5b0af75dfe5a draft

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