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