Mercurial > repos > peterjc > mira4_assembler
annotate tools/mira4_0/mira4_bait.py @ 33:1291ed21789f draft
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit 1d7d466d01b23d03d214e93f1f8efa19cfa18268
author | peterjc |
---|---|
date | Fri, 02 Jun 2017 11:22:01 -0400 |
parents | 56b421d59805 |
children | 0785a6537f3e |
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:
31
diff
changeset
|
4 |
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
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:
31
diff
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:
31
diff
changeset
|
8 import shutil |
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
9 import subprocess |
25 | 10 import sys |
11 import time | |
12 | |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
13 WRAPPER_VER = "0.0.10" # 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: | |
22 child = subprocess.Popen(cmd, | |
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:
31
diff
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:
31
diff
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:
31
diff
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:
25
diff
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:
31
diff
changeset
|
39 |
25 | 40 try: |
41 mira_path = os.environ["MIRA4"] | |
42 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
|
43 sys.exit("Environment variable $MIRA4 not set") |
25 | 44 mira_binary = os.path.join(mira_path, "mirabait") |
45 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
|
46 sys.exit("Missing mirabait under $MIRA4, %r\nFolder contained: %s" |
25 | 47 % (mira_binary, ", ".join(os.listdir(mira_path)))) |
48 mira_ver = get_version(mira_binary) | |
49 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
|
50 sys.exit("This wrapper is for MIRA V4.0, not:\n%s" % mira_ver) |
25 | 51 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:
31
diff
changeset
|
52 print("%s, MIRA wrapper version %s" % (mira_ver, WRAPPER_VER)) |
25 | 53 sys.exit(0) |
54 | |
55 | |
56 format, output_choice, strand_choice, kmer_length, min_occurance, bait_file, in_file, out_file = sys.argv[1:] | |
57 | |
58 if format.startswith("fastq"): | |
59 format = "fastq" | |
60 elif format == "mira": | |
61 format = "maf" | |
62 elif format != "fasta": | |
31
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
peterjc
parents:
25
diff
changeset
|
63 sys.exit("Was not expected format %r" % format) |
25 | 64 |
65 assert out_file.endswith(".dat") | |
66 out_file_stem = out_file[:-4] | |
67 | |
68 cmd_list = [mira_binary, "-f", format, "-t", format, | |
69 "-k", kmer_length, "-n", min_occurance, | |
70 bait_file, in_file, out_file_stem] | |
71 if output_choice == "pos": | |
72 pass | |
73 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:
31
diff
changeset
|
74 # Invert the selection... |
25 | 75 cmd_list.insert(1, "-i") |
76 else: | |
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("Output choice should be 'pos' or 'neg', not %r" % output_choice) |
25 | 78 if strand_choice == "both": |
79 pass | |
80 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:
31
diff
changeset
|
81 # Ingore reverse strand... |
25 | 82 cmd_list.insert(1, "-r") |
83 else: | |
31
fd95aaef8818
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit bc3d484c5cd68ddcf456db2fff489d584aa2034c
peterjc
parents:
25
diff
changeset
|
84 sys.exit("Strand choice should be 'both' or 'fwd', not %r" % strand_choice) |
25 | 85 |
86 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:
31
diff
changeset
|
87 # print cmd |
25 | 88 start_time = time.time() |
89 try: | |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
90 # Run MIRA |
25 | 91 child = subprocess.Popen(cmd_list, |
92 stdout=subprocess.PIPE, | |
93 stderr=subprocess.STDOUT) | |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
94 except Exception as err: |
25 | 95 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err)) |
96 sys.exit(1) | |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
97 # Use .communicate as can get deadlocks with .wait(), |
25 | 98 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:
31
diff
changeset
|
99 assert stderr is None # Due to way we ran with subprocess |
25 | 100 run_time = time.time() - start_time |
101 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:
31
diff
changeset
|
102 print("mirabait took %0.2f minutes" % (run_time / 60.0)) |
25 | 103 |
104 if return_code: | |
105 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:
25
diff
changeset
|
106 sys.exit("Return error code %i from command:\n%s" % (return_code, cmd), |
25 | 107 return_code) |
108 | |
32
56b421d59805
planemo upload for repository https://github.com/peterjc/galaxy_mira/tree/master/tools/mira4_0 commit fd979d17340cde155de176604744831d9597c6b6
peterjc
parents:
31
diff
changeset
|
109 # Capture output |
25 | 110 out_tmp = out_file_stem + "." + format |
111 if not os.path.isfile(out_tmp): | |
112 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:
25
diff
changeset
|
113 sys.exit("Missing output file from mirabait: %s" % out_tmp) |
25 | 114 shutil.move(out_tmp, out_file) |