comparison tools/mira3/mira.py @ 10:a2fb1e67bd11 draft

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