Mercurial > repos > peterjc > mira_assembler
annotate tools/sr_assembly/mira.py @ 5:216bf640baaf draft
Uploaded v0.0.4 which fixes a problem in v0.0.3 with the backbone arguments.
author | peterjc |
---|---|
date | Wed, 09 May 2012 12:40:06 -0400 |
parents | 117cce3296af |
children | 3e7eca1f5d04 |
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 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
|
15 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
|
16 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
|
17 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
|
18 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
|
19 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
|
20 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
|
21 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
|
22 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
|
23 ("%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
|
24 ("%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
|
25 ("%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
|
26 ("%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
|
27 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
|
28 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
|
29 else: |
03b240624b5a
Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
30 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
|
31 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
|
32 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
|
33 |
03b240624b5a
Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
34 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
|
35 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
|
36 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
|
37 shutil.rmtree(folder) |
03b240624b5a
Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
38 |
03b240624b5a
Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
39 #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
|
40 #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
|
41 #/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
|
42 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
|
43 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
|
44 |
0
03b240624b5a
Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
45 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
|
46 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
|
47 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
|
48 |
298f5c1d9521
Uploaded v0.0.2 of the wrapper, which improves capture of the stdout/stderr log file from MIRA.
peterjc
parents:
0
diff
changeset
|
49 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
|
50 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
|
51 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
|
52 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
|
53 #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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 |
298f5c1d9521
Uploaded v0.0.2 of the wrapper, which improves capture of the stdout/stderr log file from MIRA.
peterjc
parents:
0
diff
changeset
|
59 #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
|
60 #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
|
61 |
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 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
|
63 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
|
64 #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
|
65 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
|
66 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
|
67 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
|
68 except Exception, err: |
03b240624b5a
Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
69 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
|
70 #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
|
71 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
|
72 handle.close() |
0
03b240624b5a
Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
73 sys.exit(1) |
03b240624b5a
Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
74 #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
|
75 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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 if return_code: |
03b240624b5a
Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
82 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
|
83 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
|
84 handle.close() |
03b240624b5a
Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
85 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
|
86 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
|
87 return_code) |
03b240624b5a
Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
88 handle.close() |
03b240624b5a
Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
89 |
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
|
90 #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
|
91 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
|
92 |
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 #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
|
94 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
|
95 |
0
03b240624b5a
Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
96 print "Done" |