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