Mercurial > repos > peterjc > mira4_assembler
annotate tools/mira4_0/mira4_bait.py @ 35:259891fce7fd draft
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
| author | peterjc | 
|---|---|
| date | Wed, 09 Aug 2017 13:32:11 -0400 | 
| parents | 0785a6537f3e | 
| children | cee8f9005e43 | 
| rev | line source | 
|---|---|
| 25 | 1 #!/usr/bin/env python | 
| 2 """A simple wrapper script to call MIRA4's mirabait and collect its output. | |
| 3 """ | |
| 32 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 4 | 
| 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 5 from __future__ import print_function | 
| 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 6 | 
| 25 | 7 import os | 
| 32 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 8 import shutil | 
| 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 9 import subprocess | 
| 25 | 10 import sys | 
| 11 import time | |
| 12 | |
| 35 
259891fce7fd
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
 peterjc parents: 
34diff
changeset | 13 WRAPPER_VER = "0.0.11" # Keep in sync with the XML file | 
| 25 | 14 | 
| 15 | |
| 16 def get_version(mira_binary): | |
| 17 """Run MIRA to find its version number""" | |
| 18 # At the commend line I would use: mira -v | head -n 1 | |
| 19 # however there is some pipe error when doing that here. | |
| 20 cmd = [mira_binary, "-v"] | |
| 21 try: | |
| 34 
0785a6537f3e
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 6405ba93fcec7ea93452bf54d559c7507ee7a57c
 peterjc parents: 
32diff
changeset | 22 child = subprocess.Popen(cmd, universal_newlines=True, | 
| 25 | 23 stdout=subprocess.PIPE, | 
| 24 stderr=subprocess.STDOUT) | |
| 32 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 25 except Exception as err: | 
| 25 | 26 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err)) | 
| 27 sys.exit(1) | |
| 28 ver, tmp = child.communicate() | |
| 29 del child | |
| 32 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 30 # Workaround for -v not working in mirabait 4.0RC4 | 
| 25 | 31 if "invalid option" in ver.split("\n", 1)[0]: | 
| 32 for line in ver.split("\n", 1): | |
| 33 if " version " in line: | |
| 34 line = line.split() | |
| 32 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 35 return line[line.index("version") + 1].rstrip(")") | 
| 31 
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
 peterjc parents: 
25diff
changeset | 36 sys.exit("Could not determine MIRA version:\n%s" % ver) | 
| 25 | 37 return ver.split("\n", 1)[0] | 
| 38 | |
| 32 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 39 | 
| 35 
259891fce7fd
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
 peterjc parents: 
34diff
changeset | 40 if "MIRA4" in os.environ: | 
| 25 | 41 mira_path = os.environ["MIRA4"] | 
| 35 
259891fce7fd
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
 peterjc parents: 
34diff
changeset | 42 mira_binary = os.path.join(mira_path, "mirabait") | 
| 
259891fce7fd
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
 peterjc parents: 
34diff
changeset | 43 if not os.path.isfile(mira_binary): | 
| 
259891fce7fd
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
 peterjc parents: 
34diff
changeset | 44 sys.exit("Missing mirabait under $MIRA4, %r\nFolder contained: %s" | 
| 
259891fce7fd
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
 peterjc parents: 
34diff
changeset | 45 % (mira_binary, ", ".join(os.listdir(mira_path)))) | 
| 
259891fce7fd
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
 peterjc parents: 
34diff
changeset | 46 else: | 
| 
259891fce7fd
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
 peterjc parents: 
34diff
changeset | 47 sys.stderr.write("DEBUG: Since $MIRA4 is not set, assuming mira binaries are on $PATH.\n") | 
| 
259891fce7fd
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
 peterjc parents: 
34diff
changeset | 48 mira_path = None | 
| 
259891fce7fd
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
 peterjc parents: 
34diff
changeset | 49 mira_binary = "mirabait" | 
| 
259891fce7fd
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit e4c56df75150c82d3e9c4ac487c4209108e52412
 peterjc parents: 
34diff
changeset | 50 | 
| 25 | 51 mira_ver = get_version(mira_binary) | 
| 52 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: 
25diff
changeset | 53 sys.exit("This wrapper is for MIRA V4.0, not:\n%s" % mira_ver) | 
| 25 | 54 if "-v" in sys.argv or "--version" in sys.argv: | 
| 32 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 55 print("%s, MIRA wrapper version %s" % (mira_ver, WRAPPER_VER)) | 
| 25 | 56 sys.exit(0) | 
| 57 | |
| 58 | |
| 59 format, output_choice, strand_choice, kmer_length, min_occurance, bait_file, in_file, out_file = sys.argv[1:] | |
| 60 | |
| 61 if format.startswith("fastq"): | |
| 62 format = "fastq" | |
| 63 elif format == "mira": | |
| 64 format = "maf" | |
| 65 elif format != "fasta": | |
| 31 
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
 peterjc parents: 
25diff
changeset | 66 sys.exit("Was not expected format %r" % format) | 
| 25 | 67 | 
| 68 assert out_file.endswith(".dat") | |
| 69 out_file_stem = out_file[:-4] | |
| 70 | |
| 71 cmd_list = [mira_binary, "-f", format, "-t", format, | |
| 72 "-k", kmer_length, "-n", min_occurance, | |
| 73 bait_file, in_file, out_file_stem] | |
| 74 if output_choice == "pos": | |
| 75 pass | |
| 76 elif output_choice == "neg": | |
| 32 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 77 # Invert the selection... | 
| 25 | 78 cmd_list.insert(1, "-i") | 
| 79 else: | |
| 31 
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
 peterjc parents: 
25diff
changeset | 80 sys.exit("Output choice should be 'pos' or 'neg', not %r" % output_choice) | 
| 25 | 81 if strand_choice == "both": | 
| 82 pass | |
| 83 elif strand_choice == "fwd": | |
| 32 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 84 # Ingore reverse strand... | 
| 25 | 85 cmd_list.insert(1, "-r") | 
| 86 else: | |
| 31 
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
 peterjc parents: 
25diff
changeset | 87 sys.exit("Strand choice should be 'both' or 'fwd', not %r" % strand_choice) | 
| 25 | 88 | 
| 89 cmd = " ".join(cmd_list) | |
| 32 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 90 # print cmd | 
| 25 | 91 start_time = time.time() | 
| 92 try: | |
| 32 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 93 # Run MIRA | 
| 34 
0785a6537f3e
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 6405ba93fcec7ea93452bf54d559c7507ee7a57c
 peterjc parents: 
32diff
changeset | 94 child = subprocess.Popen(cmd_list, universal_newlines=True, | 
| 25 | 95 stdout=subprocess.PIPE, | 
| 96 stderr=subprocess.STDOUT) | |
| 32 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 97 except Exception as err: | 
| 25 | 98 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err)) | 
| 99 sys.exit(1) | |
| 32 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 100 # Use .communicate as can get deadlocks with .wait(), | 
| 25 | 101 stdout, stderr = child.communicate() | 
| 32 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 102 assert stderr is None # Due to way we ran with subprocess | 
| 25 | 103 run_time = time.time() - start_time | 
| 104 return_code = child.returncode | |
| 32 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 105 print("mirabait took %0.2f minutes" % (run_time / 60.0)) | 
| 25 | 106 | 
| 107 if return_code: | |
| 108 sys.stderr.write(stdout) | |
| 31 
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
 peterjc parents: 
25diff
changeset | 109 sys.exit("Return error code %i from command:\n%s" % (return_code, cmd), | 
| 25 | 110 return_code) | 
| 111 | |
| 32 
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
 peterjc parents: 
31diff
changeset | 112 # Capture output | 
| 25 | 113 out_tmp = out_file_stem + "." + format | 
| 114 if not os.path.isfile(out_tmp): | |
| 115 sys.stderr.write(stdout) | |
| 31 
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
 peterjc parents: 
25diff
changeset | 116 sys.exit("Missing output file from mirabait: %s" % out_tmp) | 
| 25 | 117 shutil.move(out_tmp, out_file) | 
