annotate tools/sr_assembly/mira.py @ 7:4d3f94dfb857 draft

Uploaded v0.0.6, tell MIRA to use /tmp for temporary files, and ignore long read names (to prevent it aborting).
author peterjc
date Thu, 14 Feb 2013 06:26:32 -0500
parents 3e7eca1f5d04
children
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
6
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
10 WRAPPER_VER = "0.0.5" #Keep in sync with the XML file
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
11
0
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
12 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
13 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
14 sys.exit(err)
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
15
6
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
16
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
17 def get_version():
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
18 """Run MIRA to find its version number"""
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
19 # At the commend line I would use: mira -v | head -n 1
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
20 # however there is some pipe error when doing that here.
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
21 try:
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
22 child = subprocess.Popen(["mira", "-v"],
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
23 stdout=subprocess.PIPE,
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
24 stderr=subprocess.STDOUT)
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
25 except Exception, err:
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
26 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err))
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
27 sys.exit(1)
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
28 ver, tmp = child.communicate()
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
29 del child
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
30 return ver.split("\n", 1)[0]
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
31
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
32
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
33 mira_ver = get_version()
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
34 if "V3.4." not in mira_ver:
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
35 stop_err("This wrapper is for MIRA V3.4, not %s" % mira_ver)
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
36 if "-v" in sys.argv:
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
37 print "MIRA wrapper version %s," % WRAPPER_VER
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
38 print mira_ver
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
39 sys.exit(0)
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
40
3e7eca1f5d04 MIRA wrapper v0.0.5 with <version_command> support and check for MIRA V3.4
peterjc
parents: 4
diff changeset
41
0
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
42 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
43 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
44 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
45 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
46 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
47 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
48 stop_err("Empty output folder")
4
117cce3296af Uploaded wrapper v0.0.3, which is for MIRA v3.4.x which includes support for Ion Torrent.
peterjc
parents: 3
diff changeset
49 missing = []
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
50 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
51 ("%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
52 ("%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
53 ("%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
54 ("%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
55 if not os.path.isfile(old):
4
117cce3296af Uploaded wrapper v0.0.3, which is for MIRA v3.4.x which includes support for Ion Torrent.
peterjc
parents: 3
diff changeset
56 missing.append(os.path.splitext(old)[-1])
0
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
57 else:
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
58 shutil.move(old, new)
4
117cce3296af Uploaded wrapper v0.0.3, which is for MIRA v3.4.x which includes support for Ion Torrent.
peterjc
parents: 3
diff changeset
59 if missing:
117cce3296af Uploaded wrapper v0.0.3, which is for MIRA v3.4.x which includes support for Ion Torrent.
peterjc
parents: 3
diff changeset
60 stop_err("Missing output files: %s" % ", ".join(missing))
0
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
61
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
62 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
63 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
64 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
65 shutil.rmtree(folder)
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
66
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
67 #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
68 #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
69 #/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
70 temp = "."
4
117cce3296af Uploaded wrapper v0.0.3, which is for MIRA v3.4.x which includes support for Ion Torrent.
peterjc
parents: 3
diff changeset
71 name, out_fasta, out_qual, out_ace, out_caf, out_wig, out_log = sys.argv[1:8]
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
72
0
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
73 start_time = time.time()
4
117cce3296af Uploaded wrapper v0.0.3, which is for MIRA v3.4.x which includes support for Ion Torrent.
peterjc
parents: 3
diff changeset
74 cmd_list =sys.argv[8:]
117cce3296af Uploaded wrapper v0.0.3, which is for MIRA v3.4.x which includes support for Ion Torrent.
peterjc
parents: 3
diff changeset
75 cmd = " ".join(cmd_list)
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
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 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
78 d = "%s_assembly" % name
4
117cce3296af Uploaded wrapper v0.0.3, which is for MIRA v3.4.x which includes support for Ion Torrent.
peterjc
parents: 3
diff changeset
79 assert not os.path.isdir(d), "Path %s already exists" % d
0
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
80 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
81 #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
82 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
83 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
84 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
85 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
86
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 #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
88 #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
89
298f5c1d9521 Uploaded v0.0.2 of the wrapper, which improves capture of the stdout/stderr log file from MIRA.
peterjc
parents: 0
diff changeset
90 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
91 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
92 #Run MIRA
4
117cce3296af Uploaded wrapper v0.0.3, which is for MIRA v3.4.x which includes support for Ion Torrent.
peterjc
parents: 3
diff changeset
93 child = subprocess.Popen(cmd_list,
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
94 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
95 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
96 except Exception, err:
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
97 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
98 #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
99 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
100 handle.close()
0
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
101 sys.exit(1)
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
102 #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
103 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
104 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
105 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
106 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
107 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
108 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
109 if return_code:
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
110 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
111 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
112 handle.close()
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
113 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
114 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
115 return_code)
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
116 handle.close()
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
117
4
117cce3296af Uploaded wrapper v0.0.3, which is for MIRA v3.4.x which includes support for Ion Torrent.
peterjc
parents: 3
diff changeset
118 #print "Collecting output..."
0
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
119 collect_output(temp, name)
4
117cce3296af Uploaded wrapper v0.0.3, which is for MIRA v3.4.x which includes support for Ion Torrent.
peterjc
parents: 3
diff changeset
120
117cce3296af Uploaded wrapper v0.0.3, which is for MIRA v3.4.x which includes support for Ion Torrent.
peterjc
parents: 3
diff changeset
121 #print "Cleaning up..."
0
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
122 clean_up(temp, name)
4
117cce3296af Uploaded wrapper v0.0.3, which is for MIRA v3.4.x which includes support for Ion Torrent.
peterjc
parents: 3
diff changeset
123
0
03b240624b5a Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff changeset
124 print "Done"