Mercurial > repos > peterjc > effectivet3
comparison tools/effectiveT3/effectiveT3.py @ 3:b0b927299aee draft
Uploaded v0.0.11 with automatic dependency installation.
The Python wrapper also gives specific errors for partial installation issues.
author | peterjc |
---|---|
date | Thu, 16 May 2013 04:45:41 -0400 |
parents | |
children | 1ea715da1879 |
comparison
equal
deleted
inserted
replaced
2:5a8e09f115f8 | 3:b0b927299aee |
---|---|
1 #!/usr/bin/env python | |
2 """Wrapper for EffectiveT3 v1.0.1 for use in Galaxy. | |
3 | |
4 This script takes exactly five command line arguments: | |
5 * model name (e.g. TTSS_STD-1.0.1.jar) | |
6 * threshold (selective or sensitive) | |
7 * an input protein FASTA filename | |
8 * output tabular filename | |
9 | |
10 It then calls the standalone Effective T3 v1.0.1 program (not the | |
11 webservice), and reformats the semi-colon separated output into | |
12 tab separated output for use in Galaxy. | |
13 """ | |
14 import sys | |
15 import os | |
16 import subprocess | |
17 | |
18 #The Galaxy auto-install via tool_dependencies.xml will set this environment variable | |
19 effectiveT3_dir = os.environ.get("EFFECTIVET3", "/opt/EffectiveT3/") | |
20 effectiveT3_jar = os.path.join(effectiveT3_dir, "TTSS_GUI-1.0.1.jar") | |
21 | |
22 if "-v" in sys.argv or "--version" in sys.argv: | |
23 #TODO - Get version of the JAR file dynamically? | |
24 print "Wrapper v0.0.11, TTSS_GUI-1.0.1.jar" | |
25 sys.exit(0) | |
26 | |
27 def stop_err(msg, error_level=1): | |
28 """Print error message to stdout and quit with given error level.""" | |
29 sys.stderr.write("%s\n" % msg) | |
30 sys.exit(error_level) | |
31 | |
32 if len(sys.argv) != 5: | |
33 stop_err("Require four arguments: model, threshold, input protein FASTA file & output tabular file") | |
34 | |
35 model, threshold, fasta_file, tabular_file = sys.argv[1:] | |
36 | |
37 if not os.path.isfile(fasta_file): | |
38 stop_err("Input FASTA file not found: %s" % fasta_file) | |
39 | |
40 if threshold not in ["selective", "sensitive"] \ | |
41 and not threshold.startswith("cutoff="): | |
42 stop_err("Threshold should be selective, sensitive, or cutoff=..., not %r" % threshold) | |
43 | |
44 def clean_tabular(raw_handle, out_handle): | |
45 """Clean up Effective T3 output to make it tabular.""" | |
46 count = 0 | |
47 positive = 0 | |
48 errors = 0 | |
49 for line in raw_handle: | |
50 if not line or line.startswith("#") \ | |
51 or line.startswith("Id; Description; Score;"): | |
52 continue | |
53 assert line.count(";") >= 3, repr(line) | |
54 #Normally there will just be three semi-colons, however the | |
55 #original FASTA file's ID or description might have had | |
56 #semi-colons in it as well, hence the following hackery: | |
57 try: | |
58 id_descr, score, effective = line.rstrip("\r\n").rsplit(";",2) | |
59 #Cope when there was no FASTA description | |
60 if "; " not in id_descr and id_descr.endswith(";"): | |
61 id = id_descr[:-1] | |
62 descr = "" | |
63 else: | |
64 id, descr = id_descr.split("; ",1) | |
65 except ValueError: | |
66 stop_err("Problem parsing line:\n%s\n" % line) | |
67 parts = [s.strip() for s in [id, descr, score, effective]] | |
68 out_handle.write("\t".join(parts) + "\n") | |
69 count += 1 | |
70 if float(score) < 0: | |
71 errors += 1 | |
72 if effective.lower() == "true": | |
73 positive += 1 | |
74 return count, positive, errors | |
75 | |
76 def run(cmd): | |
77 #Avoid using shell=True when we call subprocess to ensure if the Python | |
78 #script is killed, so too is the child process. | |
79 try: | |
80 child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
81 except Exception, err: | |
82 stop_err("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err)) | |
83 #Use .communicate as can get deadlocks with .wait(), | |
84 stdout, stderr = child.communicate() | |
85 return_code = child.returncode | |
86 if return_code: | |
87 if stderr and stdout: | |
88 stop_err("Return code %i from command:\n%s\n\n%s\n\n%s" % (return_code, err, stdout, stderr)) | |
89 else: | |
90 stop_err("Return code %i from command:\n%s\n%s" % (return_code, err, stderr)) | |
91 | |
92 if not os.path.isdir(effectiveT3_dir): | |
93 stop_err("Effective T3 folder not found: %r" % effectiveT3_dir) | |
94 | |
95 if not os.path.isfile(effectiveT3_jar): | |
96 stop_err("Effective T3 JAR file not found: %r" % effectiveT3_jar) | |
97 | |
98 if not os.path.isdir(os.path.join(effectiveT3_dir, "module")): | |
99 stop_err("Effective T3 module folder not found: %r" % os.path.join(effectiveT3_dir, "module")) | |
100 | |
101 effectiveT3_model = os.path.join(effectiveT3_dir, "module", model) | |
102 if not os.path.isfile(effectiveT3_model): | |
103 sys.stderr.write("Contents of %r is %s\n" | |
104 % (os.path.join(effectiveT3_dir, "module"), | |
105 ", ".join(repr(p) for p in os.listdir(os.path.join(effectiveT3_dir, "module"))))) | |
106 sys.stderr.write("Main JAR was found: %r\n" % effectiveT3_jar) | |
107 stop_err("Effective T3 model JAR file not found: %r" % effectiveT3_model) | |
108 | |
109 #We will have write access whereever the output should be, | |
110 temp_file = os.path.abspath(tabular_file + ".tmp") | |
111 | |
112 #Use absolute paths since will change current directory... | |
113 tabular_file = os.path.abspath(tabular_file) | |
114 fasta_file = os.path.abspath(fasta_file) | |
115 | |
116 cmd = ["java", "-jar", effectiveT3_jar, | |
117 "-f", fasta_file, | |
118 "-m", model, | |
119 "-t", threshold, | |
120 "-o", temp_file, | |
121 "-q"] | |
122 | |
123 try: | |
124 #Must run from directory above the module subfolder: | |
125 os.chdir(effectiveT3_dir) | |
126 except: | |
127 stop_err("Could not change to Effective T3 folder: %s" % effectiveT3_dir) | |
128 | |
129 run(cmd) | |
130 | |
131 if not os.path.isfile(temp_file): | |
132 stop_err("ERROR - No output file from Effective T3") | |
133 | |
134 out_handle = open(tabular_file, "w") | |
135 out_handle.write("#ID\tDescription\tScore\tEffective\n") | |
136 data_handle = open(temp_file) | |
137 count, positive, errors = clean_tabular(data_handle, out_handle) | |
138 data_handle.close() | |
139 out_handle.close() | |
140 | |
141 os.remove(temp_file) | |
142 | |
143 if errors: | |
144 print "%i sequences, %i positive, %i errors" \ | |
145 % (count, positive, errors) | |
146 else: | |
147 print "%i/%i sequences positive" % (positive, count) | |
148 | |
149 if count and count==errors: | |
150 #Galaxy will still allow them to see the output file | |
151 stop_err("All your sequences gave an error code") |