Mercurial > repos > peterjc > effectivet3
annotate tools/effectiveT3/effectiveT3.py @ 10:a46d7861c32c draft
"Update all the pico_galaxy tools on main Tool Shed"
| author | peterjc |
|---|---|
| date | Fri, 16 Apr 2021 22:34:56 +0000 |
| parents | 512530020360 |
| children | ed8c1babc166 |
| 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 """ | |
| 131 # Check that a given file can be accessed with the correct mode. | |
| 132 # Additionally check that `file` is not a directory, as on Windows | |
| 133 # directories pass the os.access check. | |
| 134 def _access_check(fn, mode): | |
| 135 return os.path.exists(fn) and os.access(fn, mode) and not os.path.isdir(fn) | |
| 136 | |
| 137 # Short circuit. If we're given a full path which matches the mode | |
| 138 # and it exists, we're done here. | |
| 139 if _access_check(cmd, mode): | |
| 140 return cmd | |
| 141 | |
| 142 path = (path or os.environ.get("PATH", os.defpath)).split(os.pathsep) | |
| 143 | |
| 144 if sys.platform == "win32": | |
| 145 # The current directory takes precedence on Windows. | |
| 146 if os.curdir not in path: | |
| 147 path.insert(0, os.curdir) | |
|
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
148 |
| 10 | 149 # PATHEXT is necessary to check on Windows. |
| 150 pathext = os.environ.get("PATHEXT", "").split(os.pathsep) | |
| 151 # See if the given file matches any of the expected path extensions. | |
| 152 # This will allow us to short circuit when given "python.exe". | |
| 153 matches = [cmd for ext in pathext if cmd.lower().endswith(ext.lower())] | |
| 154 # If it does match, only test that one, otherwise we have to try | |
| 155 # others. | |
| 156 files = [cmd] if matches else [cmd + ext.lower() for ext in pathext] | |
| 157 else: | |
| 158 # On other platforms you don't have things like PATHEXT to tell you | |
| 159 # what file suffixes are executable, so just pass on cmd as-is. | |
| 160 files = [cmd] | |
| 161 | |
| 162 seen = set() | |
| 163 for dir in path: | |
| 164 dir = os.path.normcase(dir) | |
| 165 if dir not in seen: | |
| 166 seen.add(dir) | |
| 167 for thefile in files: | |
| 168 name = os.path.join(dir, thefile) | |
| 169 if _access_check(name, mode): | |
| 170 return name | |
| 171 return None | |
| 172 | |
| 173 | |
| 174 # Try in order the following to find the JAR file: | |
| 175 # - Location of any wrapper script, e.g. from BioConda installation | |
| 176 # - The $EFFECTIVET3 env var, e.g. old-style Galaxy tool installation | |
| 177 # - The /opt/EffectiveT3/ folder. | |
| 178 effective_t3_jar = None | |
| 179 effective_t3_dir = None | |
| 180 dirs = ["/opt/EffectiveT3/"] | |
| 181 if "EFFECTIVET3" in os.environ: | |
| 182 dirs.insert(0, os.environ.get("EFFECTIVET3")) | |
| 183 if which("effectivet3"): | |
| 184 # Assuming this is a BioConda installed wrapper for effective T3, | |
| 185 # this will get the directory of the wrapper script which is where | |
| 186 # the JAR file will be: | |
| 187 dirs.insert(0, os.path.split(os.path.realpath(which("effectivet3")))[0]) | |
| 188 for effective_t3_dir in dirs: | |
| 189 effective_t3_jar = os.path.join(effective_t3_dir, effective_t3_jarname) | |
| 190 if os.path.isfile(effective_t3_jar): | |
| 191 # Good | |
| 192 break | |
| 193 effective_t3_jar = None | |
| 194 if not effective_t3_dir or not effective_t3_jar: | |
| 195 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
|
196 |
| 8 | 197 if not os.path.isdir(os.path.join(effective_t3_dir, "module")): |
| 10 | 198 sys.exit( |
| 199 "Effective T3 module folder not found: %r" | |
| 200 % os.path.join(effective_t3_dir, "module") | |
| 201 ) | |
|
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
202 |
| 8 | 203 effective_t3_model = os.path.join(effective_t3_dir, "module", model) |
| 204 if not os.path.isfile(effective_t3_model): | |
| 10 | 205 sys.stderr.write( |
| 206 "Contents of %r is %s\n" | |
| 207 % ( | |
| 208 os.path.join(effective_t3_dir, "module"), | |
| 209 ", ".join( | |
| 210 repr(p) for p in os.listdir(os.path.join(effective_t3_dir, "module")) | |
| 211 ), | |
| 212 ) | |
| 213 ) | |
| 8 | 214 sys.stderr.write("Main JAR was found: %r\n" % effective_t3_jar) |
| 215 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
|
216 |
| 10 | 217 # We will have write access wherever the output should be, |
| 218 if tabular_file == "/dev/stdout": | |
| 219 temp_file = os.path.abspath("effectivet3_tabular_output.tmp") | |
| 220 else: | |
| 221 temp_file = os.path.abspath(tabular_file + ".tmp") | |
|
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
222 |
|
5
1ea715da1879
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
3
diff
changeset
|
223 # Use absolute paths since will change current directory... |
|
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
224 tabular_file = os.path.abspath(tabular_file) |
|
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
225 fasta_file = os.path.abspath(fasta_file) |
|
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
226 |
| 10 | 227 cmd = [ |
| 228 "java", | |
| 229 "-jar", | |
| 230 effective_t3_jar, | |
| 231 "-f", | |
| 232 fasta_file, | |
| 233 "-m", | |
| 234 model, | |
| 235 "-t", | |
| 236 threshold, | |
| 237 "-o", | |
| 238 temp_file, | |
| 239 "-q", | |
| 240 ] | |
|
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
241 |
|
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
242 try: |
|
5
1ea715da1879
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
3
diff
changeset
|
243 # Must run from directory above the module subfolder: |
| 8 | 244 os.chdir(effective_t3_dir) |
| 245 except Exception: | |
| 246 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
|
247 |
|
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
248 run(cmd) |
|
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
249 |
|
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
250 if not os.path.isfile(temp_file): |
| 8 | 251 sys.exit("ERROR - No output file from Effective T3") |
|
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
252 |
|
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
253 out_handle = open(tabular_file, "w") |
|
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
254 out_handle.write("#ID\tDescription\tScore\tEffective\n") |
|
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
255 data_handle = open(temp_file) |
|
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
256 count, positive, errors = clean_tabular(data_handle, out_handle) |
|
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
257 data_handle.close() |
|
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
258 out_handle.close() |
|
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
259 |
|
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
260 os.remove(temp_file) |
|
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
261 |
|
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
262 if errors: |
| 10 | 263 print("%i sequences, %i positive, %i errors" % (count, positive, errors)) |
|
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
264 else: |
| 8 | 265 print("%i/%i sequences positive" % (positive, count)) |
|
3
b0b927299aee
Uploaded v0.0.11 with automatic dependency installation.
peterjc
parents:
diff
changeset
|
266 |
| 8 | 267 if count and count == errors: |
| 268 # Galaxy will still allow them to see the output file | |
| 269 sys.exit("All your sequences gave an error code") |
