Mercurial > repos > peterjc > effectivet3
annotate tools/effectiveT3/effectiveT3.py @ 11:ed8c1babc166 draft default tip
v0.0.21 - Added bio.tools xref
author | peterjc |
---|---|
date | Tue, 12 Mar 2024 16:04:13 +0000 |
parents | a46d7861c32c |
children |
rev | line source |
---|---|
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
1 #!/usr/bin/env python |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
2 """Wrapper for EffectiveT3 v1.0.1 for use in Galaxy. |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
3 |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
4 This script takes exactly five command line arguments: |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
5 * model name (e.g. TTSS_STD-1.0.1.jar) |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
6 * threshold (selective or sensitive) |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
7 * an input protein FASTA filename |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
8 * output tabular filename |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
9 |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
10 It then calls the standalone Effective T3 v1.0.1 program (not the |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
11 webservice), and reformats the semi-colon separated output into |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
12 tab separated output for use in Galaxy. |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
13 """ |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
14 import os |
10 | 15 |
16 # We want to be able to use shutil.which, but need Python 3.3+ | |
17 # import shutil | |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
18 import subprocess |
9 | 19 import sys |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
20 |
10 | 21 # The Galaxy auto-install via tool_dependencies.xml will set the |
22 # environment variable $EFFECTIVET3 pointing at the folder with | |
23 # the JAR file. | |
24 # | |
25 # The BioConda recipe will put a wrapper script on the $PATH, | |
26 # which we can use to find the JAR file. | |
27 # | |
28 # We fall back on /opt/EffectiveT3/ | |
29 # | |
30 effective_t3_jarname = "TTSS_GUI-1.0.1.jar" | |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
31 |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
32 if "-v" in sys.argv or "--version" in sys.argv: |
5
1ea715da1879
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
3
diff
changeset
|
33 # TODO - Get version of the JAR file dynamically? |
10 | 34 print("Wrapper v0.0.20, for %s" % effective_t3_jarname) |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
35 sys.exit(0) |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
36 |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
37 if len(sys.argv) != 5: |
10 | 38 sys.exit( |
39 "Require four arguments: model, threshold, input protein " | |
40 "FASTA file & output tabular file" | |
41 ) | |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
42 |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
43 model, threshold, fasta_file, tabular_file = sys.argv[1:] |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
44 |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
45 if not os.path.isfile(fasta_file): |
8 | 46 sys.exit("Input FASTA file not found: %s" % fasta_file) |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
47 |
9 | 48 if threshold not in ["selective", "sensitive"] and not threshold.startswith("cutoff="): |
10 | 49 sys.exit( |
50 "Threshold should be selective, sensitive, or cutoff=..., not %r" % threshold | |
51 ) | |
8 | 52 |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
53 |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
54 def clean_tabular(raw_handle, out_handle): |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
55 """Clean up Effective T3 output to make it tabular.""" |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
56 count = 0 |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
57 positive = 0 |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
58 errors = 0 |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
59 for line in raw_handle: |
10 | 60 if ( |
61 not line | |
62 or line.startswith("#") | |
63 or line.startswith("Id; Description; Score;") | |
64 ): | |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
65 continue |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
66 assert line.count(";") >= 3, repr(line) |
5
1ea715da1879
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
3
diff
changeset
|
67 # Normally there will just be three semi-colons, however the |
1ea715da1879
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
3
diff
changeset
|
68 # original FASTA file's ID or description might have had |
1ea715da1879
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
3
diff
changeset
|
69 # semi-colons in it as well, hence the following hackery: |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
70 try: |
8 | 71 id_descr, score, effective = line.rstrip("\r\n").rsplit(";", 2) |
5
1ea715da1879
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
3
diff
changeset
|
72 # Cope when there was no FASTA description |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
73 if "; " not in id_descr and id_descr.endswith(";"): |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
74 id = id_descr[:-1] |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
75 descr = "" |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
76 else: |
8 | 77 id, descr = id_descr.split("; ", 1) |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
78 except ValueError: |
8 | 79 sys.exit("Problem parsing line:\n%s\n" % line) |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
80 parts = [s.strip() for s in [id, descr, score, effective]] |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
81 out_handle.write("\t".join(parts) + "\n") |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
82 count += 1 |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
83 if float(score) < 0: |
8 | 84 errors += 1 |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
85 if effective.lower() == "true": |
8 | 86 positive += 1 |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
87 return count, positive, errors |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
88 |
8 | 89 |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
90 def run(cmd): |
9 | 91 """Run the command line string via subprocess.""" |
5
1ea715da1879
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
3
diff
changeset
|
92 # Avoid using shell=True when we call subprocess to ensure if the Python |
1ea715da1879
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
3
diff
changeset
|
93 # script is killed, so too is the child process. |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
94 try: |
10 | 95 child = subprocess.Popen( |
96 cmd, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE | |
97 ) | |
98 except Exception as err: | |
8 | 99 sys.exit("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err)) |
5
1ea715da1879
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
3
diff
changeset
|
100 # Use .communicate as can get deadlocks with .wait(), |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
101 stdout, stderr = child.communicate() |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
102 return_code = child.returncode |
7 | 103 if return_code or stderr.startswith("Exception in thread"): |
8 | 104 cmd_str = " ".join(cmd) # doesn't quote spaces etc |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
105 if stderr and stdout: |
10 | 106 sys.exit( |
107 "Return code %i from command:\n%s\n\n%s\n\n%s" | |
108 % (return_code, cmd_str, stdout, stderr) | |
109 ) | |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
110 else: |
10 | 111 sys.exit( |
112 "Return code %i from command:\n%s\n%s" % (return_code, cmd_str, stderr) | |
113 ) | |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
114 |
7 | 115 |
10 | 116 try: |
117 from shutil import which | |
118 except ImportError: | |
119 # Likely running on Python 2, use backport: | |
120 def which(cmd, mode=os.F_OK | os.X_OK, path=None): | |
121 """Python implementation of command line tool which. | |
122 | |
123 Given a command, mode, and a PATH string, return the path which | |
124 conforms to the given mode on the PATH, or None if there is no such | |
125 file. | |
126 | |
127 `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result | |
128 of os.environ.get("PATH"), or can be overridden with a custom search | |
129 path. | |
130 """ | |
11 | 131 |
10 | 132 # Check that a given file can be accessed with the correct mode. |
133 # Additionally check that `file` is not a directory, as on Windows | |
134 # directories pass the os.access check. | |
135 def _access_check(fn, mode): | |
136 return os.path.exists(fn) and os.access(fn, mode) and not os.path.isdir(fn) | |
137 | |
138 # Short circuit. If we're given a full path which matches the mode | |
139 # and it exists, we're done here. | |
140 if _access_check(cmd, mode): | |
141 return cmd | |
142 | |
143 path = (path or os.environ.get("PATH", os.defpath)).split(os.pathsep) | |
144 | |
145 if sys.platform == "win32": | |
146 # The current directory takes precedence on Windows. | |
147 if os.curdir not in path: | |
148 path.insert(0, os.curdir) | |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
149 |
10 | 150 # PATHEXT is necessary to check on Windows. |
151 pathext = os.environ.get("PATHEXT", "").split(os.pathsep) | |
152 # See if the given file matches any of the expected path extensions. | |
153 # This will allow us to short circuit when given "python.exe". | |
154 matches = [cmd for ext in pathext if cmd.lower().endswith(ext.lower())] | |
155 # If it does match, only test that one, otherwise we have to try | |
156 # others. | |
157 files = [cmd] if matches else [cmd + ext.lower() for ext in pathext] | |
158 else: | |
159 # On other platforms you don't have things like PATHEXT to tell you | |
160 # what file suffixes are executable, so just pass on cmd as-is. | |
161 files = [cmd] | |
162 | |
163 seen = set() | |
164 for dir in path: | |
165 dir = os.path.normcase(dir) | |
166 if dir not in seen: | |
167 seen.add(dir) | |
168 for thefile in files: | |
169 name = os.path.join(dir, thefile) | |
170 if _access_check(name, mode): | |
171 return name | |
172 return None | |
173 | |
174 | |
175 # Try in order the following to find the JAR file: | |
176 # - Location of any wrapper script, e.g. from BioConda installation | |
177 # - The $EFFECTIVET3 env var, e.g. old-style Galaxy tool installation | |
178 # - The /opt/EffectiveT3/ folder. | |
179 effective_t3_jar = None | |
180 effective_t3_dir = None | |
181 dirs = ["/opt/EffectiveT3/"] | |
182 if "EFFECTIVET3" in os.environ: | |
183 dirs.insert(0, os.environ.get("EFFECTIVET3")) | |
184 if which("effectivet3"): | |
185 # Assuming this is a BioConda installed wrapper for effective T3, | |
186 # this will get the directory of the wrapper script which is where | |
187 # the JAR file will be: | |
188 dirs.insert(0, os.path.split(os.path.realpath(which("effectivet3")))[0]) | |
189 for effective_t3_dir in dirs: | |
190 effective_t3_jar = os.path.join(effective_t3_dir, effective_t3_jarname) | |
191 if os.path.isfile(effective_t3_jar): | |
192 # Good | |
193 break | |
194 effective_t3_jar = None | |
195 if not effective_t3_dir or not effective_t3_jar: | |
196 sys.exit("Effective T3 JAR file %r not found in %r" % (effective_t3_jarname, dirs)) | |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
197 |
8 | 198 if not os.path.isdir(os.path.join(effective_t3_dir, "module")): |
10 | 199 sys.exit( |
200 "Effective T3 module folder not found: %r" | |
201 % os.path.join(effective_t3_dir, "module") | |
202 ) | |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
203 |
8 | 204 effective_t3_model = os.path.join(effective_t3_dir, "module", model) |
205 if not os.path.isfile(effective_t3_model): | |
10 | 206 sys.stderr.write( |
207 "Contents of %r is %s\n" | |
208 % ( | |
209 os.path.join(effective_t3_dir, "module"), | |
210 ", ".join( | |
211 repr(p) for p in os.listdir(os.path.join(effective_t3_dir, "module")) | |
212 ), | |
213 ) | |
214 ) | |
8 | 215 sys.stderr.write("Main JAR was found: %r\n" % effective_t3_jar) |
216 sys.exit("Effective T3 model JAR file not found: %r" % effective_t3_model) | |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
217 |
10 | 218 # We will have write access wherever the output should be, |
219 if tabular_file == "/dev/stdout": | |
220 temp_file = os.path.abspath("effectivet3_tabular_output.tmp") | |
221 else: | |
222 temp_file = os.path.abspath(tabular_file + ".tmp") | |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
223 |
5
1ea715da1879
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
3
diff
changeset
|
224 # Use absolute paths since will change current directory... |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
225 tabular_file = os.path.abspath(tabular_file) |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
226 fasta_file = os.path.abspath(fasta_file) |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
227 |
10 | 228 cmd = [ |
229 "java", | |
230 "-jar", | |
231 effective_t3_jar, | |
232 "-f", | |
233 fasta_file, | |
234 "-m", | |
235 model, | |
236 "-t", | |
237 threshold, | |
238 "-o", | |
239 temp_file, | |
240 "-q", | |
241 ] | |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
242 |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
243 try: |
5
1ea715da1879
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
3
diff
changeset
|
244 # Must run from directory above the module subfolder: |
8 | 245 os.chdir(effective_t3_dir) |
246 except Exception: | |
247 sys.exit("Could not change to Effective T3 folder: %s" % effective_t3_dir) | |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
248 |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
249 run(cmd) |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
250 |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
251 if not os.path.isfile(temp_file): |
8 | 252 sys.exit("ERROR - No output file from Effective T3") |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
253 |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
254 out_handle = open(tabular_file, "w") |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
255 out_handle.write("#ID\tDescription\tScore\tEffective\n") |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
256 data_handle = open(temp_file) |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
257 count, positive, errors = clean_tabular(data_handle, out_handle) |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
258 data_handle.close() |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
259 out_handle.close() |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
260 |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
261 os.remove(temp_file) |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
262 |
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
263 if errors: |
10 | 264 print("%i sequences, %i positive, %i errors" % (count, positive, errors)) |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
265 else: |
8 | 266 print("%i/%i sequences positive" % (positive, count)) |
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
267 |
8 | 268 if count and count == errors: |
269 # Galaxy will still allow them to see the output file | |
270 sys.exit("All your sequences gave an error code") |