Mercurial > repos > peterjc > effectivet3
annotate tools/effectiveT3/effectiveT3.py @ 8:4a0aa59062f7 draft
Uploaded v0.0.11, take 9, debug output if can't find model
author | peterjc |
---|---|
date | Fri, 10 May 2013 11:32:29 -0400 |
parents | 66e9d4c44ca2 |
children | b863c4bec840 |
rev | line source |
---|---|
2 | 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): | |
8
4a0aa59062f7
Uploaded v0.0.11, take 9, debug output if can't find model
peterjc
parents:
2
diff
changeset
|
93 stop_err("Effective T3 folder not found: %r" % effectiveT3_dir) |
2 | 94 |
95 if not os.path.isfile(effectiveT3_jar): | |
8
4a0aa59062f7
Uploaded v0.0.11, take 9, debug output if can't find model
peterjc
parents:
2
diff
changeset
|
96 stop_err("Effective T3 JAR file not found: %r" % effectiveT3_jar) |
2 | 97 |
98 effectiveT3_model = os.path.join(effectiveT3_dir, "module", model) | |
99 if not os.path.isfile(effectiveT3_model): | |
8
4a0aa59062f7
Uploaded v0.0.11, take 9, debug output if can't find model
peterjc
parents:
2
diff
changeset
|
100 sys.stderr.write("Contents of %r is %s\n" |
4a0aa59062f7
Uploaded v0.0.11, take 9, debug output if can't find model
peterjc
parents:
2
diff
changeset
|
101 % (os.path.join(effectiveT3_dir, "module"), |
4a0aa59062f7
Uploaded v0.0.11, take 9, debug output if can't find model
peterjc
parents:
2
diff
changeset
|
102 ", ".join(repr(p) for p in os.listdir(os.path.join(effectiveT3_dir, "module"))))) |
4a0aa59062f7
Uploaded v0.0.11, take 9, debug output if can't find model
peterjc
parents:
2
diff
changeset
|
103 sys.stderr.write("Main JAR was found: %r\n" % effectiveT3_jar) |
4a0aa59062f7
Uploaded v0.0.11, take 9, debug output if can't find model
peterjc
parents:
2
diff
changeset
|
104 stop_err("Effective T3 model JAR file not found: %r" % effectiveT3_model) |
2 | 105 |
106 #We will have write access whereever the output should be, | |
107 temp_file = os.path.abspath(tabular_file + ".tmp") | |
108 | |
109 #Use absolute paths since will change current directory... | |
110 tabular_file = os.path.abspath(tabular_file) | |
111 fasta_file = os.path.abspath(fasta_file) | |
112 | |
113 cmd = ["java", "-jar", effectiveT3_jar, | |
114 "-f", fasta_file, | |
115 "-m", model, | |
116 "-t", threshold, | |
117 "-o", temp_file, | |
118 "-q"] | |
119 | |
120 try: | |
121 #Must run from directory above the module subfolder: | |
122 os.chdir(effectiveT3_dir) | |
123 except: | |
124 stop_err("Could not change to Effective T3 folder: %s" % effectiveT3_dir) | |
125 | |
126 run(cmd) | |
127 | |
128 if not os.path.isfile(temp_file): | |
129 stop_err("ERROR - No output file from Effective T3") | |
130 | |
131 out_handle = open(tabular_file, "w") | |
132 out_handle.write("#ID\tDescription\tScore\tEffective\n") | |
133 data_handle = open(temp_file) | |
134 count, positive, errors = clean_tabular(data_handle, out_handle) | |
135 data_handle.close() | |
136 out_handle.close() | |
137 | |
138 os.remove(temp_file) | |
139 | |
140 if errors: | |
141 print "%i sequences, %i positive, %i errors" \ | |
142 % (count, positive, errors) | |
143 else: | |
144 print "%i/%i sequences positive" % (positive, count) | |
145 | |
146 if count and count==errors: | |
147 #Galaxy will still allow them to see the output file | |
148 stop_err("All your sequences gave an error code") |