Mercurial > repos > peterjc > mira4_assembler
annotate tools/mira4/mira4.py @ 20:aeb3e35f8236 draft
Uploaded v0.0.4 preview, made MAF and BAM output optional
author | peterjc |
---|---|
date | Tue, 10 Jun 2014 10:11:58 -0400 |
parents | 8487d70e82aa |
children | 4abe8d59a438 |
rev | line source |
---|---|
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
1 #!/usr/bin/env python |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
2 """A simple wrapper script to call MIRA and collect its output. |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
3 """ |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
4 import os |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
5 import sys |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
6 import subprocess |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
7 import shutil |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
8 import time |
18
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
9 import tempfile |
20
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
10 from optparse import OptionParser |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
11 |
9 | 12 #Do we need any PYTHONPATH magic? |
13 from mira4_make_bam import make_bam | |
14 | |
20
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
15 WRAPPER_VER = "0.0.4" #Keep in sync with the XML file |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
16 |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
17 def stop_err(msg, err=1): |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
18 sys.stderr.write(msg+"\n") |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
19 sys.exit(err) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
20 |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
21 |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
22 def get_version(mira_binary): |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
23 """Run MIRA to find its version number""" |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
24 # At the commend line I would use: mira -v | head -n 1 |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
25 # however there is some pipe error when doing that here. |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
26 cmd = [mira_binary, "-v"] |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
27 try: |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
28 child = subprocess.Popen(cmd, |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
29 stdout=subprocess.PIPE, |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
30 stderr=subprocess.STDOUT) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
31 except Exception, err: |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
32 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err)) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
33 sys.exit(1) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
34 ver, tmp = child.communicate() |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
35 del child |
9 | 36 return ver.split("\n", 1)[0].strip() |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
37 |
20
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
38 #Parse Command Line |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
39 usage = """Galaxy MIRA4 wrapper script v%s - use as follows: |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
40 |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
41 $ python mira4.py ... |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
42 |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
43 This will run the MIRA binary and collect its output files as directed. |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
44 """ % WRAPPER_VER |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
45 parser = OptionParser(usage=usage) |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
46 parser.add_option("-m", "--manifest", dest="manifest", |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
47 default=None, metavar="FILE", |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
48 help="MIRA manifest filename") |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
49 parser.add_option("--maf", dest="maf", |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
50 default="-", metavar="FILE", |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
51 help="MIRA MAF output filename") |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
52 parser.add_option("--bam", dest="bam", |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
53 default="-", metavar="FILE", |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
54 help="Unpadded BAM output filename") |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
55 parser.add_option("--fasta", dest="fasta", |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
56 default="-", metavar="FILE", |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
57 help="Unpadded FASTA output filename") |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
58 parser.add_option("--log", dest="log", |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
59 default="-", metavar="FILE", |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
60 help="MIRA logging output filename") |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
61 options, args = parser.parse_args() |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
62 manifest = options.manifest |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
63 out_maf = options.maf |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
64 out_bam = options.bam |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
65 out_fasta = options.fasta |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
66 out_log = options.log |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
67 |
7 | 68 try: |
69 mira_path = os.environ["MIRA4"] | |
12
a6a56440567c
Uploaded v0.0.2 preview 4, now using MIRA 4.0 (not RC), and sets MIRA4 environment variable during install.
peterjc
parents:
11
diff
changeset
|
70 except KeyError: |
7 | 71 stop_err("Environment variable $MIRA4 not set") |
72 mira_binary = os.path.join(mira_path, "mira") | |
73 if not os.path.isfile(mira_binary): | |
13
7fcabeeca5df
Uploaded v0.0.2 preview 5, fixes for MIRA 4.0 (final), more verbose error if $MIRA4 path wrong
peterjc
parents:
12
diff
changeset
|
74 stop_err("Missing mira under $MIRA4, %r\nFolder contained: %s" |
7fcabeeca5df
Uploaded v0.0.2 preview 5, fixes for MIRA 4.0 (final), more verbose error if $MIRA4 path wrong
peterjc
parents:
12
diff
changeset
|
75 % (mira_binary, ", ".join(os.listdir(mira_path)))) |
9 | 76 mira_convert = os.path.join(mira_path, "miraconvert") |
77 if not os.path.isfile(mira_convert): | |
13
7fcabeeca5df
Uploaded v0.0.2 preview 5, fixes for MIRA 4.0 (final), more verbose error if $MIRA4 path wrong
peterjc
parents:
12
diff
changeset
|
78 stop_err("Missing miraconvert under $MIRA4, %r\nFolder contained: %s" |
7fcabeeca5df
Uploaded v0.0.2 preview 5, fixes for MIRA 4.0 (final), more verbose error if $MIRA4 path wrong
peterjc
parents:
12
diff
changeset
|
79 % (mira_convert, ", ".join(os.listdir(mira_path)))) |
7 | 80 |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
81 mira_ver = get_version(mira_binary) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
82 if not mira_ver.strip().startswith("4.0"): |
9 | 83 stop_err("This wrapper is for MIRA V4.0, not:\n%s\n%s" % (mira_ver, mira_binary)) |
84 mira_convert_ver = get_version(mira_convert) | |
85 if not mira_convert_ver.strip().startswith("4.0"): | |
86 stop_err("This wrapper is for MIRA V4.0, not:\n%s\n%s" % (mira_ver, mira_convert)) | |
5
ffefb87bd414
Uploaded v0.0.1 preview 5, using MIRA 4.0 RC4, supports segment_placement (pairing type)
peterjc
parents:
4
diff
changeset
|
87 if "-v" in sys.argv or "--version" in sys.argv: |
ffefb87bd414
Uploaded v0.0.1 preview 5, using MIRA 4.0 RC4, supports segment_placement (pairing type)
peterjc
parents:
4
diff
changeset
|
88 print "%s, MIRA wrapper version %s" % (mira_ver, WRAPPER_VER) |
9 | 89 if mira_ver != mira_convert_ver: |
90 print "WARNING: miraconvert %s" % mira_convert_ver | |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
91 sys.exit(0) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
92 |
20
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
93 if not manifest: |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
94 stop_err("Manifest is required") |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
95 elif not os.path.isfile(manifest): |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
96 stop_err("Missing input MIRA manifest file: %r" % manifest) |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
97 |
7 | 98 |
11
02350bef2e99
Uploaded v0.0.2 preview 3, correction regression in URL
peterjc
parents:
9
diff
changeset
|
99 try: |
02350bef2e99
Uploaded v0.0.2 preview 3, correction regression in URL
peterjc
parents:
9
diff
changeset
|
100 threads = int(os.environ.get("GALAXY_SLOTS", "1")) |
02350bef2e99
Uploaded v0.0.2 preview 3, correction regression in URL
peterjc
parents:
9
diff
changeset
|
101 except ValueError: |
02350bef2e99
Uploaded v0.0.2 preview 3, correction regression in URL
peterjc
parents:
9
diff
changeset
|
102 threads = 1 |
02350bef2e99
Uploaded v0.0.2 preview 3, correction regression in URL
peterjc
parents:
9
diff
changeset
|
103 assert 1 <= threads, threads |
7 | 104 |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
105 |
18
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
106 def override_temp(manifest): |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
107 """Override ``-DI:trt=/tmp`` in manifest with environment variable. |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
108 |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
109 Currently MIRA 4 does not allow envronment variables like ``$TMP`` |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
110 inside the manifest, which is a problem if you need to override |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
111 the default at run time. |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
112 |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
113 The tool XML will ``/tmp`` and we replace that here with |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
114 ``tempfile.gettempdir()`` which will respect $TMPDIR, $TEMP, $TMP |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
115 as explained in the Python standard library documentation: |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
116 http://docs.python.org/2/library/tempfile.html#tempfile.tempdir |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
117 |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
118 By default MIRA 4 would write its temporary files within the output |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
119 folder, which is a problem if that is a network drive. |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
120 """ |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
121 handle = open(manifest, "r") |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
122 text = handle.read() |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
123 handle.close() |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
124 |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
125 #At time of writing, this is at the end of a file, |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
126 #but could be followed by a space in future... |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
127 text = text.replace("-DI:trt=/tmp", "-DI:trt=" + tempfile.gettempdir()) |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
128 |
19 | 129 #Want to try to ensure this gets written to disk before MIRA attempts |
130 #to open it - any networked file system may impose a delay... | |
18
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
131 handle = open(manifest, "w") |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
132 handle.write(text) |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
133 handle.flush() |
19 | 134 os.fsync(handle.fileno()) |
18
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
135 handle.close() |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
136 |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
137 |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
138 def log_manifest(manifest): |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
139 """Write the manifest file to stderr.""" |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
140 sys.stderr.write("\n%s\nManifest file\n%s\n" % ("="*60, "="*60)) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
141 with open(manifest) as h: |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
142 for line in h: |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
143 sys.stderr.write(line) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
144 sys.stderr.write("\n%s\nEnd of manifest\n%s\n" % ("="*60, "="*60)) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
145 |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
146 |
4 | 147 def collect_output(temp, name, handle): |
9 | 148 """Moves files to the output filenames (global variables).""" |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
149 n3 = (temp, name, name, name) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
150 f = "%s/%s_assembly/%s_d_results" % (temp, name, name) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
151 if not os.path.isdir(f): |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
152 log_manifest(manifest) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
153 stop_err("Missing output folder") |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
154 if not os.listdir(f): |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
155 log_manifest(manifest) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
156 stop_err("Empty output folder") |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
157 missing = [] |
4 | 158 |
159 old_maf = "%s/%s_out.maf" % (f, name) | |
160 if not os.path.isfile(old_maf): | |
161 #Triggered extractLargeContigs.sh? | |
162 old_maf = "%s/%s_LargeContigs_out.maf" % (f, name) | |
163 | |
164 #De novo or single strain mapping, | |
165 old_fasta = "%s/%s_out.unpadded.fasta" % (f, name) | |
9 | 166 ref_fasta = "%s/%s_out.padded.fasta" % (f, name) |
4 | 167 if not os.path.isfile(old_fasta): |
9 | 168 #Mapping (StrainX versus reference) or de novo |
4 | 169 old_fasta = "%s/%s_out_StrainX.unpadded.fasta" % (f, name) |
9 | 170 ref_fasta = "%s/%s_out_StrainX.padded.fasta" % (f, name) |
4 | 171 if not os.path.isfile(old_fasta): |
9 | 172 old_fasta = "%s/%s_out_ReferenceStrain.unpadded.fasta" % (f, name) |
173 ref_fasta = "%s/%s_out_ReferenceStrain.padded.fasta" % (f, name) | |
174 | |
4 | 175 |
176 missing = False | |
177 for old, new in [(old_maf, out_maf), | |
178 (old_fasta, out_fasta)]: | |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
179 if not os.path.isfile(old): |
4 | 180 missing = True |
20
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
181 elif not new or new == "-": |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
182 handle.write("Ignoring %s\n" % old) |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
183 else: |
4 | 184 handle.write("Capturing %s\n" % old) |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
185 shutil.move(old, new) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
186 if missing: |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
187 log_manifest(manifest) |
4 | 188 sys.stderr.write("Contents of %r:\n" % f) |
189 for filename in sorted(os.listdir(f)): | |
190 sys.stderr.write("%s\n" % filename) | |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
191 |
9 | 192 #For mapping mode, probably most people would expect a BAM file |
193 #using the reference FASTA file... | |
20
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
194 if out_bam and out_bam != "-": |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
195 msg = make_bam(mira_convert, out_maf, ref_fasta, out_bam, handle) |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
196 if msg: |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
197 stop_err(msg) |
9 | 198 |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
199 def clean_up(temp, name): |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
200 folder = "%s/%s_assembly" % (temp, name) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
201 if os.path.isdir(folder): |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
202 shutil.rmtree(folder) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
203 |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
204 #TODO - Run MIRA in /tmp or a configurable directory? |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
205 #Currently Galaxy puts us somewhere safe like: |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
206 #/opt/galaxy-dist/database/job_working_directory/846/ |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
207 temp = "." |
20
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
208 |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
209 name = "MIRA" |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
210 |
18
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
211 override_temp(manifest) |
381aa262c8cb
Uploaded v0.0.2 preview 10, override /tmp via environment variable
peterjc
parents:
13
diff
changeset
|
212 |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
213 start_time = time.time() |
11
02350bef2e99
Uploaded v0.0.2 preview 3, correction regression in URL
peterjc
parents:
9
diff
changeset
|
214 cmd_list = [mira_binary, "-t", str(threads), manifest] |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
215 cmd = " ".join(cmd_list) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
216 |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
217 assert os.path.isdir(temp) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
218 d = "%s_assembly" % name |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
219 assert not os.path.isdir(d), "Path %s already exists" % d |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
220 try: |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
221 #Check path access |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
222 os.mkdir(d) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
223 except Exception, err: |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
224 log_manifest(manifest) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
225 sys.stderr.write("Error making directory %s\n%s" % (d, err)) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
226 sys.exit(1) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
227 |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
228 #print os.path.abspath(".") |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
229 #print cmd |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
230 |
20
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
231 if out_log and out_log != "-": |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
232 handle = open(out_log, "w") |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
233 else: |
aeb3e35f8236
Uploaded v0.0.4 preview, made MAF and BAM output optional
peterjc
parents:
19
diff
changeset
|
234 handle = open(os.devnull, "w") |
4 | 235 handle.write("======================== MIRA manifest (instructions) ========================\n") |
236 m = open(manifest, "rU") | |
237 for line in m: | |
238 handle.write(line) | |
239 m.close() | |
240 del m | |
241 handle.write("\n") | |
242 handle.write("============================ Starting MIRA now ===============================\n") | |
243 handle.flush() | |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
244 try: |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
245 #Run MIRA |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
246 child = subprocess.Popen(cmd_list, |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
247 stdout=handle, |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
248 stderr=subprocess.STDOUT) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
249 except Exception, err: |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
250 log_manifest(manifest) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
251 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err)) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
252 #TODO - call clean up? |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
253 handle.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err)) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
254 handle.close() |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
255 sys.exit(1) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
256 #Use .communicate as can get deadlocks with .wait(), |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
257 stdout, stderr = child.communicate() |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
258 assert not stdout and not stderr #Should be empty as sent to handle |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
259 run_time = time.time() - start_time |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
260 return_code = child.returncode |
4 | 261 handle.write("\n") |
262 handle.write("============================ MIRA has finished ===============================\n") | |
263 handle.write("MIRA took %0.2f hours\n" % (run_time / 3600.0)) | |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
264 if return_code: |
9 | 265 print "MIRA took %0.2f hours" % (run_time / 3600.0) |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
266 handle.write("Return error code %i from command:\n" % return_code) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
267 handle.write(cmd + "\n") |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
268 handle.close() |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
269 clean_up(temp, name) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
270 log_manifest(manifest) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
271 stop_err("Return error code %i from command:\n%s" % (return_code, cmd), |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
272 return_code) |
4 | 273 handle.flush() |
274 | |
275 if os.path.isfile("MIRA_assembly/MIRA_d_results/ec.log"): | |
276 handle.write("\n") | |
277 handle.write("====================== Extract Large Contigs failed ==========================\n") | |
278 e = open("MIRA_assembly/MIRA_d_results/ec.log", "rU") | |
279 for line in e: | |
280 handle.write(line) | |
281 e.close() | |
282 handle.write("============================ (end of ec.log) =================================\n") | |
283 handle.flush() | |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
284 |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
285 #print "Collecting output..." |
9 | 286 start_time = time.time() |
4 | 287 collect_output(temp, name, handle) |
9 | 288 collect_time = time.time() - start_time |
289 handle.write("MIRA took %0.2f hours; collecting output %0.2f minutes\n" % (run_time / 3600.0, collect_time / 60.0)) | |
290 print("MIRA took %0.2f hours; collecting output %0.2f minutes\n" % (run_time / 3600.0, collect_time / 60.0)) | |
4 | 291 |
292 if os.path.isfile("MIRA_assembly/MIRA_d_results/ec.log"): | |
293 #Treat as an error, but doing this AFTER collect_output | |
294 sys.stderr.write("Extract Large Contigs failed\n") | |
295 handle.write("Extract Large Contigs failed\n") | |
296 handle.close() | |
297 sys.exit(1) | |
0
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
298 |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
299 #print "Cleaning up..." |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
300 clean_up(temp, name) |
32f693f6e741
Uploaded v0.0.1 preview0, very much a work in progress, primarily checking mira_datatypes dependency
peterjc
parents:
diff
changeset
|
301 |
9 | 302 handle.write("\nDone\n") |
4 | 303 handle.close() |
304 print("Done") |