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