annotate tools/mira4_0/mira4.py @ 31:fd95aaef8818 draft

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