annotate tools/mira4/mira4_bait.py @ 0:6a88b42ce6b9 draft

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