Mercurial > repos > peterjc > mira4_assembler
comparison tools/mira4_0/mira4.py @ 39:bbf14bb9607b draft default tip
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 89578746a1c5b29c84a173d8b2709f086f69a7b6
author | peterjc |
---|---|
date | Mon, 03 Jun 2019 13:29:00 -0400 |
parents | cee8f9005e43 |
children |
comparison
equal
deleted
inserted
replaced
38:cee8f9005e43 | 39:bbf14bb9607b |
---|---|
22 """Run MIRA to find its version number.""" | 22 """Run MIRA to find its version number.""" |
23 # At the commend line I would use: mira -v | head -n 1 | 23 # At the commend line I would use: mira -v | head -n 1 |
24 # however there is some pipe error when doing that here. | 24 # however there is some pipe error when doing that here. |
25 cmd = [mira_binary, "-v"] | 25 cmd = [mira_binary, "-v"] |
26 try: | 26 try: |
27 child = subprocess.Popen(cmd, universal_newlines=True, | 27 child = subprocess.Popen( |
28 stdout=subprocess.PIPE, | 28 cmd, |
29 stderr=subprocess.STDOUT) | 29 universal_newlines=True, |
30 stdout=subprocess.PIPE, | |
31 stderr=subprocess.STDOUT, | |
32 ) | |
30 except Exception as err: | 33 except Exception as err: |
31 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err)) | 34 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err)) |
32 sys.exit(1) | 35 sys.exit(1) |
33 ver, tmp = child.communicate() | 36 ver, tmp = child.communicate() |
34 del child | 37 del child |
35 return ver.split("\n", 1)[0].strip() | 38 return ver.split("\n", 1)[0].strip() |
36 | 39 |
37 | 40 |
38 # Parse Command Line | 41 # Parse Command Line |
39 usage = """Galaxy MIRA4 wrapper script v%s - use as follows: | 42 usage = ( |
43 """Galaxy MIRA4 wrapper script v%s - use as follows: | |
40 | 44 |
41 $ python mira4.py ... | 45 $ python mira4.py ... |
42 | 46 |
43 This will run the MIRA binary and collect its output files as directed. | 47 This will run the MIRA binary and collect its output files as directed. |
44 """ % WRAPPER_VER | 48 """ |
49 % WRAPPER_VER | |
50 ) | |
45 parser = OptionParser(usage=usage) | 51 parser = OptionParser(usage=usage) |
46 parser.add_option("-m", "--manifest", dest="manifest", | 52 parser.add_option( |
47 default=None, metavar="FILE", | 53 "-m", |
48 help="MIRA manifest filename") | 54 "--manifest", |
49 parser.add_option("--maf", dest="maf", | 55 dest="manifest", |
50 default="-", metavar="FILE", | 56 default=None, |
51 help="MIRA MAF output filename") | 57 metavar="FILE", |
52 parser.add_option("--bam", dest="bam", | 58 help="MIRA manifest filename", |
53 default="-", metavar="FILE", | 59 ) |
54 help="Unpadded BAM output filename") | 60 parser.add_option( |
55 parser.add_option("--fasta", dest="fasta", | 61 "--maf", dest="maf", default="-", metavar="FILE", help="MIRA MAF output filename" |
56 default="-", metavar="FILE", | 62 ) |
57 help="Unpadded FASTA output filename") | 63 parser.add_option( |
58 parser.add_option("--log", dest="log", | 64 "--bam", |
59 default="-", metavar="FILE", | 65 dest="bam", |
60 help="MIRA logging output filename") | 66 default="-", |
61 parser.add_option("-v", "--version", dest="version", | 67 metavar="FILE", |
62 default=False, action="store_true", | 68 help="Unpadded BAM output filename", |
63 help="Show version and quit") | 69 ) |
70 parser.add_option( | |
71 "--fasta", | |
72 dest="fasta", | |
73 default="-", | |
74 metavar="FILE", | |
75 help="Unpadded FASTA output filename", | |
76 ) | |
77 parser.add_option( | |
78 "--log", | |
79 dest="log", | |
80 default="-", | |
81 metavar="FILE", | |
82 help="MIRA logging output filename", | |
83 ) | |
84 parser.add_option( | |
85 "-v", | |
86 "--version", | |
87 dest="version", | |
88 default=False, | |
89 action="store_true", | |
90 help="Show version and quit", | |
91 ) | |
64 options, args = parser.parse_args() | 92 options, args = parser.parse_args() |
65 manifest = options.manifest | 93 manifest = options.manifest |
66 out_maf = options.maf | 94 out_maf = options.maf |
67 out_bam = options.bam | 95 out_bam = options.bam |
68 out_fasta = options.fasta | 96 out_fasta = options.fasta |
70 | 98 |
71 if "MIRA4" in os.environ: | 99 if "MIRA4" in os.environ: |
72 mira_path = os.environ["MIRA4"] | 100 mira_path = os.environ["MIRA4"] |
73 mira_binary = os.path.join(mira_path, "mira") | 101 mira_binary = os.path.join(mira_path, "mira") |
74 if not os.path.isfile(mira_binary): | 102 if not os.path.isfile(mira_binary): |
75 sys.exit("Missing mira under $MIRA4, %r\nFolder contained: %s" | 103 sys.exit( |
76 % (mira_binary, ", ".join(os.listdir(mira_path)))) | 104 "Missing mira under $MIRA4, %r\nFolder contained: %s" |
105 % (mira_binary, ", ".join(os.listdir(mira_path))) | |
106 ) | |
77 mira_convert = os.path.join(mira_path, "miraconvert") | 107 mira_convert = os.path.join(mira_path, "miraconvert") |
78 if not os.path.isfile(mira_convert): | 108 if not os.path.isfile(mira_convert): |
79 sys.exit("Missing miraconvert under $MIRA4, %r\nFolder contained: %s" | 109 sys.exit( |
80 % (mira_convert, ", ".join(os.listdir(mira_path)))) | 110 "Missing miraconvert under $MIRA4, %r\nFolder contained: %s" |
111 % (mira_convert, ", ".join(os.listdir(mira_path))) | |
112 ) | |
81 else: | 113 else: |
82 sys.stderr.write("DEBUG: Since $MIRA4 is not set, assuming mira binaries are on $PATH.\n") | 114 sys.stderr.write( |
115 "DEBUG: Since $MIRA4 is not set, assuming mira binaries are on $PATH.\n" | |
116 ) | |
83 mira_path = None | 117 mira_path = None |
84 mira_binary = "mira" | 118 mira_binary = "mira" |
85 mira_convert = "miraconvert" | 119 mira_convert = "miraconvert" |
86 | 120 |
87 mira_ver = get_version(mira_binary) | 121 mira_ver = get_version(mira_binary) |
176 if not os.path.isfile(old_fasta): | 210 if not os.path.isfile(old_fasta): |
177 old_fasta = "%s/%s_out_ReferenceStrain.unpadded.fasta" % (f, name) | 211 old_fasta = "%s/%s_out_ReferenceStrain.unpadded.fasta" % (f, name) |
178 ref_fasta = "%s/%s_out_ReferenceStrain.padded.fasta" % (f, name) | 212 ref_fasta = "%s/%s_out_ReferenceStrain.padded.fasta" % (f, name) |
179 | 213 |
180 missing = False | 214 missing = False |
181 for old, new in [(old_maf, out_maf), | 215 for old, new in [(old_maf, out_maf), (old_fasta, out_fasta)]: |
182 (old_fasta, out_fasta)]: | |
183 if not os.path.isfile(old): | 216 if not os.path.isfile(old): |
184 missing = True | 217 missing = True |
185 elif not new or new == "-": | 218 elif not new or new == "-": |
186 handle.write("Ignoring %s\n" % old) | 219 handle.write("Ignoring %s\n" % old) |
187 else: | 220 else: |
243 | 276 |
244 if out_log and out_log != "-": | 277 if out_log and out_log != "-": |
245 handle = open(out_log, "w") | 278 handle = open(out_log, "w") |
246 else: | 279 else: |
247 handle = open(os.devnull, "w") | 280 handle = open(os.devnull, "w") |
248 handle.write("======================== MIRA manifest (instructions) ========================\n") | 281 handle.write( |
282 "======================== MIRA manifest (instructions) ========================\n" | |
283 ) | |
249 m = open(manifest, "rU") | 284 m = open(manifest, "rU") |
250 for line in m: | 285 for line in m: |
251 handle.write(line) | 286 handle.write(line) |
252 m.close() | 287 m.close() |
253 del m | 288 del m |
254 handle.write("\n") | 289 handle.write("\n") |
255 handle.write("============================ Starting MIRA now ===============================\n") | 290 handle.write( |
291 "============================ Starting MIRA now ===============================\n" | |
292 ) | |
256 handle.flush() | 293 handle.flush() |
257 try: | 294 try: |
258 # Run MIRA | 295 # Run MIRA |
259 child = subprocess.Popen(cmd_list, | 296 child = subprocess.Popen(cmd_list, stdout=handle, stderr=subprocess.STDOUT) |
260 stdout=handle, | |
261 stderr=subprocess.STDOUT) | |
262 except Exception as err: | 297 except Exception as err: |
263 log_manifest(manifest) | 298 log_manifest(manifest) |
264 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err)) | 299 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err)) |
265 # TODO - call clean up? | 300 # TODO - call clean up? |
266 handle.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err)) | 301 handle.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err)) |
270 stdout, stderr = child.communicate() | 305 stdout, stderr = child.communicate() |
271 assert not stdout and not stderr # Should be empty as sent to handle | 306 assert not stdout and not stderr # Should be empty as sent to handle |
272 run_time = time.time() - start_time | 307 run_time = time.time() - start_time |
273 return_code = child.returncode | 308 return_code = child.returncode |
274 handle.write("\n") | 309 handle.write("\n") |
275 handle.write("============================ MIRA has finished ===============================\n") | 310 handle.write( |
311 "============================ MIRA has finished ===============================\n" | |
312 ) | |
276 handle.write("MIRA took %0.2f hours\n" % (run_time / 3600.0)) | 313 handle.write("MIRA took %0.2f hours\n" % (run_time / 3600.0)) |
277 if return_code: | 314 if return_code: |
278 print("MIRA took %0.2f hours" % (run_time / 3600.0)) | 315 print("MIRA took %0.2f hours" % (run_time / 3600.0)) |
279 handle.write("Return error code %i from command:\n" % return_code) | 316 handle.write("Return error code %i from command:\n" % return_code) |
280 handle.write(cmd + "\n") | 317 handle.write(cmd + "\n") |
286 sys.exit(return_code) | 323 sys.exit(return_code) |
287 handle.flush() | 324 handle.flush() |
288 | 325 |
289 if os.path.isfile("MIRA_assembly/MIRA_d_results/ec.log"): | 326 if os.path.isfile("MIRA_assembly/MIRA_d_results/ec.log"): |
290 handle.write("\n") | 327 handle.write("\n") |
291 handle.write("====================== Extract Large Contigs failed ==========================\n") | 328 handle.write( |
329 "====================== Extract Large Contigs failed ==========================\n" # noqa: E501 | |
330 ) | |
292 e = open("MIRA_assembly/MIRA_d_results/ec.log", "rU") | 331 e = open("MIRA_assembly/MIRA_d_results/ec.log", "rU") |
293 for line in e: | 332 for line in e: |
294 handle.write(line) | 333 handle.write(line) |
295 e.close() | 334 e.close() |
296 handle.write("============================ (end of ec.log) =================================\n") | 335 handle.write( |
336 "============================ (end of ec.log) =================================\n" # noqa: E501 | |
337 ) | |
297 handle.flush() | 338 handle.flush() |
298 | 339 |
299 # print("Collecting output...") | 340 # print("Collecting output...") |
300 start_time = time.time() | 341 start_time = time.time() |
301 collect_output(temp, name, handle) | 342 collect_output(temp, name, handle) |
302 collect_time = time.time() - start_time | 343 collect_time = time.time() - start_time |
303 handle.write("MIRA took %0.2f hours; collecting output %0.2f minutes\n" | 344 handle.write( |
304 % (run_time / 3600.0, collect_time / 60.0)) | 345 "MIRA took %0.2f hours; collecting output %0.2f minutes\n" |
305 print("MIRA took %0.2f hours; collecting output %0.2f minutes\n" | 346 % (run_time / 3600.0, collect_time / 60.0) |
306 % (run_time / 3600.0, collect_time / 60.0)) | 347 ) |
348 print( | |
349 "MIRA took %0.2f hours; collecting output %0.2f minutes\n" | |
350 % (run_time / 3600.0, collect_time / 60.0) | |
351 ) | |
307 | 352 |
308 if os.path.isfile("MIRA_assembly/MIRA_d_results/ec.log"): | 353 if os.path.isfile("MIRA_assembly/MIRA_d_results/ec.log"): |
309 # Treat as an error, but doing this AFTER collect_output | 354 # Treat as an error, but doing this AFTER collect_output |
310 sys.stderr.write("Extract Large Contigs failed\n") | 355 sys.stderr.write("Extract Large Contigs failed\n") |
311 handle.write("Extract Large Contigs failed\n") | 356 handle.write("Extract Large Contigs failed\n") |