Mercurial > repos > peterjc > mira_assembler
annotate mira_wrapper_v0.0.2.tar.gz/tools/sr_assembly/mira.py @ 1:e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
author | peterjc |
---|---|
date | Thu, 16 Jun 2011 04:44:00 -0400 |
parents | |
children |
rev | line source |
---|---|
1
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
1 #!/usr/bin/env python |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
2 """A simple wrapper script to call MIRA and collect its output. |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
3 """ |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
4 import os |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
5 import sys |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
6 import subprocess |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
7 import shutil |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
8 import time |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
9 |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
10 def stop_err(msg, err=1): |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
11 sys.stderr.write(msg+"\n") |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
12 sys.exit(err) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
13 |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
14 def tcs_to_tabular(old, new): |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
15 in_handle = open(old, "rU") |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
16 out_handle = open(new, "w") |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
17 assert in_handle.readline() == "#TCS V1.0\n" |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
18 assert in_handle.readline() == "#\n" |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
19 assert in_handle.readline() == "# contig name padPos upadPos | B Q | tcov covA covC covG covT cov* | qA qC qG qT q* | S | Tags\n" |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
20 assert in_handle.readline() == "#\n" |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
21 out_handle.write("#%s\n" % "\t".join(["contig", "pasPos", "upadPos", "B", "Q", |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
22 "tcov", "covA", "covC", "covG", "covT", "cov*", |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
23 "qA", "qC", "qG", "qT", "q*", "S", "Tags"])) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
24 for line in in_handle: |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
25 parts = line.rstrip("\n").split(None,22) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
26 assert parts[3] == parts[6] == parts[13] == parts[19] == parts[21] == "|" |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
27 wanted = parts[:3] + parts[4:6]+parts[7:13]+parts[14:19]+parts[20:21]+parts[22:] |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
28 out_handle.write("%s\n" % "\t".join(wanted)) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
29 out_handle.close() |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
30 in_handle.close() |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
31 |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
32 def collect_output(temp, name): |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
33 n3 = (temp, name, name, name) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
34 f = "%s/%s_assembly/%s_d_results" % (temp, name, name) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
35 if not os.path.isdir(f): |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
36 stop_err("Missing output folder") |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
37 if not os.listdir(f): |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
38 stop_err("Empty output folder") |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
39 for old, new in [("%s/%s_out.unpadded.fasta" % (f, name), out_fasta), |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
40 ("%s/%s_out.unpadded.fasta.qual" % (f, name), out_qual), |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
41 ("%s/%s_out.wig" % (f, name), out_wig), |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
42 ("%s/%s_out.caf" % (f, name), out_caf), |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
43 ("%s/%s_out.ace" % (f, name), out_ace)]: |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
44 if not os.path.isfile(old): |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
45 stop_err("Missing %s output file" % os.path.splitext(old)[-1]) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
46 else: |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
47 shutil.move(old, new) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
48 tcs_to_tabular("%s/%s_assembly/%s_d_results/%s_out.tcs" % n3, out_tcs) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
49 |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
50 def clean_up(temp, name): |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
51 folder = "%s/%s_assembly" % (temp, name) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
52 if os.path.isdir(folder): |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
53 shutil.rmtree(folder) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
54 |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
55 #TODO - Run MIRA in /tmp or a configurable directory? |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
56 #Currently Galaxy puts us somewhere safe like: |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
57 #/opt/galaxy-dist/database/job_working_directory/846/ |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
58 temp = "." |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
59 name, out_fasta, out_qual, out_tcs, out_ace, out_caf, out_wig, out_log = sys.argv[1:9] |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
60 |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
61 start_time = time.time() |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
62 cmd = " ".join(sys.argv[9:]) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
63 |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
64 assert os.path.isdir(temp) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
65 d = "%s_assembly" % name |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
66 assert not os.path.isdir(d) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
67 try: |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
68 #Check path access |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
69 os.mkdir(d) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
70 except Exception, err: |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
71 sys.stderr.write("Error making directory %s\n%s" % (d, err)) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
72 sys.exit(1) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
73 |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
74 #print os.path.abspath(".") |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
75 #print cmd |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
76 |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
77 handle = open(out_log, "w") |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
78 try: |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
79 #Run MIRA |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
80 child = subprocess.Popen(sys.argv[9:], |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
81 stdout=handle, |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
82 stderr=subprocess.STDOUT) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
83 except Exception, err: |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
84 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err)) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
85 #TODO - call clean up? |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
86 handle.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err)) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
87 handle.close() |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
88 sys.exit(1) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
89 #Use .communicate as can get deadlocks with .wait(), |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
90 stdout, stderr = child.communicate() |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
91 assert not stdout and not stderr #Should be empty as sent to handle |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
92 run_time = time.time() - start_time |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
93 return_code = child.returncode |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
94 handle.write("\n\nMIRA took %0.2f minutes\n" % (run_time / 60.0)) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
95 print "MIRA took %0.2f minutes" % (run_time / 60.0) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
96 if return_code: |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
97 handle.write("Return error code %i from command:\n" % return_code) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
98 handle.write(cmd + "\n") |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
99 handle.close() |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
100 clean_up(temp, name) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
101 stop_err("Return error code %i from command:\n%s" % (return_code, cmd), |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
102 return_code) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
103 handle.close() |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
104 |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
105 collect_output(temp, name) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
106 clean_up(temp, name) |
e53a79816f5f
v0.0.2 - Improve capture of stdout/stderr (should see it as it runs)
peterjc
parents:
diff
changeset
|
107 print "Done" |