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