comparison tools/sr_assembly/mira.py @ 3:298f5c1d9521

Uploaded v0.0.2 of the wrapper, which improves capture of the stdout/stderr log file from MIRA.
author peterjc
date Tue, 21 Jun 2011 09:50:32 -0400
parents 03b240624b5a
children 117cce3296af
comparison
equal deleted inserted replaced
2:73263d5c2c9f 3:298f5c1d9521
29 out_handle.close() 29 out_handle.close()
30 in_handle.close() 30 in_handle.close()
31 31
32 def collect_output(temp, name): 32 def collect_output(temp, name):
33 n3 = (temp, name, name, name) 33 n3 = (temp, name, name, name)
34 tcs_to_tabular("%s/%s_assembly/%s_d_results/%s_out.tcs" % n3, out_tcs) 34 f = "%s/%s_assembly/%s_d_results" % (temp, name, name)
35 for old, new in [("%s/%s_assembly/%s_d_results/%s_out.unpadded.fasta" % n3, out_fasta), 35 if not os.path.isdir(f):
36 ("%s/%s_assembly/%s_d_results/%s_out.unpadded.fasta.qual" % n3, out_qual), 36 stop_err("Missing output folder")
37 ("%s/%s_assembly/%s_d_results/%s_out.wig" % n3, out_wig), 37 if not os.listdir(f):
38 ("%s/%s_assembly/%s_d_results/%s_out.caf" % n3, out_caf), 38 stop_err("Empty output folder")
39 ("%s/%s_assembly/%s_d_results/%s_out.ace" % n3, out_ace)]: 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)]:
40 if not os.path.isfile(old): 44 if not os.path.isfile(old):
41 stop_err("Missing %s output file" % os.path.splitext(old)[-1]) 45 stop_err("Missing %s output file" % os.path.splitext(old)[-1])
42 else: 46 else:
43 shutil.move(old, new) 47 shutil.move(old, new)
48 tcs_to_tabular("%s/%s_assembly/%s_d_results/%s_out.tcs" % n3, out_tcs)
44 49
45 def clean_up(temp, name): 50 def clean_up(temp, name):
46 folder = "%s/%s_assembly" % (temp, name) 51 folder = "%s/%s_assembly" % (temp, name)
47 if os.path.isdir(folder): 52 if os.path.isdir(folder):
48 shutil.rmtree(folder) 53 shutil.rmtree(folder)
49 54
50 #TODO - Run MIRA in /tmp or a configurable directory? 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/
51 temp = "." 58 temp = "."
52 name, out_fasta, out_qual, out_tcs, out_ace, out_caf, out_wig, out_log = sys.argv[1:9] 59 name, out_fasta, out_qual, out_tcs, out_ace, out_caf, out_wig, out_log = sys.argv[1:9]
60
53 start_time = time.time() 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)
54 try: 67 try:
55 cmd = " ".join(sys.argv[9:]) 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
56 child = subprocess.Popen(sys.argv[9:], 80 child = subprocess.Popen(sys.argv[9:],
57 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 81 stdout=handle,
82 stderr=subprocess.STDOUT)
58 except Exception, err: 83 except Exception, err:
59 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (cmd, 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()
60 sys.exit(1) 88 sys.exit(1)
61 #Use .communicate as can get deadlocks with .wait(), 89 #Use .communicate as can get deadlocks with .wait(),
62 stdout, stderr = child.communicate() 90 stdout, stderr = child.communicate()
91 assert not stdout and not stderr #Should be empty as sent to handle
63 run_time = time.time() - start_time 92 run_time = time.time() - start_time
64 return_code = child.returncode 93 return_code = child.returncode
65 handle = open(out_log, "w")
66 handle.write(stdout)
67 handle.write("\n\nMIRA took %0.2f minutes\n" % (run_time / 60.0)) 94 handle.write("\n\nMIRA took %0.2f minutes\n" % (run_time / 60.0))
68 print "MIRA took %0.2f minutes" % (run_time / 60.0) 95 print "MIRA took %0.2f minutes" % (run_time / 60.0)
69 if return_code: 96 if return_code:
70 handle.write("Return error code %i from command:\n" % return_code) 97 handle.write("Return error code %i from command:\n" % return_code)
71 handle.write(cmd + "\n") 98 handle.write(cmd + "\n")