comparison tools/effectiveT3/effectiveT3.py @ 32:1129cafdf0ae draft

planemo upload for repository https://github.com/peterjc/pico_galaxy/tree/master/tools/effectiveT3 commit b557cc18c85dcc5147b80c71f99c16a7015e12b2-dirty
author peterjc
date Wed, 07 Jun 2017 10:51:31 -0400
parents 3ef6c246ccac
children 011cfce866f1
comparison
equal deleted inserted replaced
31:3ef6c246ccac 32:1129cafdf0ae
10 It then calls the standalone Effective T3 v1.0.1 program (not the 10 It then calls the standalone Effective T3 v1.0.1 program (not the
11 webservice), and reformats the semi-colon separated output into 11 webservice), and reformats the semi-colon separated output into
12 tab separated output for use in Galaxy. 12 tab separated output for use in Galaxy.
13 """ 13 """
14 import os 14 import os
15 # We want to be able to use shutil.which, but need Python 3.3+
16 # import shutil
15 import subprocess 17 import subprocess
16 import sys 18 import sys
17 19
18 # The Galaxy auto-install via tool_dependencies.xml will set this environment variable 20 # The Galaxy auto-install via tool_dependencies.xml will set the
19 effective_t3_dir = os.environ.get("EFFECTIVET3", "/opt/EffectiveT3/") 21 # environment variable $EFFECTIVET3 pointing at the folder with
20 effective_t3_jar = os.path.join(effective_t3_dir, "TTSS_GUI-1.0.1.jar") 22 # the JAR file.
23 #
24 # The BioConda recipe will put a wrapper script on the $PATH,
25 # which we can use to find the JAR file.
26 #
27 # We fall back on /opt/EffectiveT3/
28 #
29 effective_t3_jarname = "TTSS_GUI-1.0.1.jar"
21 30
22 if "-v" in sys.argv or "--version" in sys.argv: 31 if "-v" in sys.argv or "--version" in sys.argv:
23 # TODO - Get version of the JAR file dynamically? 32 # TODO - Get version of the JAR file dynamically?
24 print("Wrapper v0.0.19, TTSS_GUI-1.0.1.jar") 33 print("Wrapper v0.0.20, for %s" % effective_t3_jarname)
25 sys.exit(0) 34 sys.exit(0)
26 35
27 if len(sys.argv) != 5: 36 if len(sys.argv) != 5:
28 sys.exit("Require four arguments: model, threshold, input protein FASTA file & output tabular file") 37 sys.exit("Require four arguments: model, threshold, input protein FASTA file & output tabular file")
29 38
86 sys.exit("Return code %i from command:\n%s\n\n%s\n\n%s" % (return_code, cmd_str, stdout, stderr)) 95 sys.exit("Return code %i from command:\n%s\n\n%s\n\n%s" % (return_code, cmd_str, stdout, stderr))
87 else: 96 else:
88 sys.exit("Return code %i from command:\n%s\n%s" % (return_code, cmd_str, stderr)) 97 sys.exit("Return code %i from command:\n%s\n%s" % (return_code, cmd_str, stderr))
89 98
90 99
91 if not os.path.isdir(effective_t3_dir): 100 try:
92 sys.exit("Effective T3 folder not found: %r" % effective_t3_dir) 101 from shutil import which
93 102 except ImportError:
94 if not os.path.isfile(effective_t3_jar): 103 # Likely running on Python 2, use backport:
95 sys.exit("Effective T3 JAR file not found: %r" % effective_t3_jar) 104 def which(cmd, mode=os.F_OK | os.X_OK, path=None):
105 """Given a command, mode, and a PATH string, return the path which
106 conforms to the given mode on the PATH, or None if there is no such
107 file.
108 `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result
109 of os.environ.get("PATH"), or can be overridden with a custom search
110 path.
111 """
112
113 # Check that a given file can be accessed with the correct mode.
114 # Additionally check that `file` is not a directory, as on Windows
115 # directories pass the os.access check.
116 def _access_check(fn, mode):
117 return (os.path.exists(fn) and os.access(fn, mode) and
118 not os.path.isdir(fn))
119
120 # Short circuit. If we're given a full path which matches the mode
121 # and it exists, we're done here.
122 if _access_check(cmd, mode):
123 return cmd
124
125 path = (path or os.environ.get("PATH", os.defpath)).split(os.pathsep)
126
127 if sys.platform == "win32":
128 # The current directory takes precedence on Windows.
129 if os.curdir not in path:
130 path.insert(0, os.curdir)
131
132 # PATHEXT is necessary to check on Windows.
133 pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
134 # See if the given file matches any of the expected path extensions.
135 # This will allow us to short circuit when given "python.exe".
136 matches = [cmd for ext in pathext if cmd.lower().endswith(ext.lower())]
137 # If it does match, only test that one, otherwise we have to try
138 # others.
139 files = [cmd] if matches else [cmd + ext.lower() for ext in pathext]
140 else:
141 # On other platforms you don't have things like PATHEXT to tell you
142 # what file suffixes are executable, so just pass on cmd as-is.
143 files = [cmd]
144
145 seen = set()
146 for dir in path:
147 dir = os.path.normcase(dir)
148 if dir not in seen:
149 seen.add(dir)
150 for thefile in files:
151 name = os.path.join(dir, thefile)
152 if _access_check(name, mode):
153 return name
154 return None
155
156
157 # Try in order the following to find the JAR file:
158 # - Location of any wrapper script, e.g. from BioConda installation
159 # - The $EFFECTIVET3 env var, e.g. old-style Galaxy tool installation
160 # - The /opt/EffectiveT3/ folder.
161 effective_t3_jar = None
162 effective_t3_dir = None
163 dirs = ["/opt/EffectiveT3/"]
164 if "EFFECTIVET3" in os.environ:
165 dirs.insert(0, os.environ.get("EFFECTIVET3"))
166 if which("effectivet3"):
167 # Assuming this is a BioConda installed wrapper for effective T3,
168 # this will get the directory of the wrapper script which is where
169 # the JAR file will be:
170 dirs.insert(0, os.path.split(os.path.realpath(which("effectivet3")))[0])
171 for effective_t3_dir in dirs:
172 effective_t3_jar = os.path.join(effective_t3_dir, effective_t3_jarname)
173 if os.path.isfile(effective_t3_jar):
174 # Good
175 break
176 effective_t3_jar = None
177 if not effective_t3_dir or not effective_t3_jar:
178 sys.exit("Effective T3 JAR file %r not found in %r" % (effective_t3_jarname, dirs))
96 179
97 if not os.path.isdir(os.path.join(effective_t3_dir, "module")): 180 if not os.path.isdir(os.path.join(effective_t3_dir, "module")):
98 sys.exit("Effective T3 module folder not found: %r" % os.path.join(effective_t3_dir, "module")) 181 sys.exit("Effective T3 module folder not found: %r" % os.path.join(effective_t3_dir, "module"))
99 182
100 effective_t3_model = os.path.join(effective_t3_dir, "module", model) 183 effective_t3_model = os.path.join(effective_t3_dir, "module", model)
104 ", ".join(repr(p) for p in os.listdir(os.path.join(effective_t3_dir, "module"))))) 187 ", ".join(repr(p) for p in os.listdir(os.path.join(effective_t3_dir, "module")))))
105 sys.stderr.write("Main JAR was found: %r\n" % effective_t3_jar) 188 sys.stderr.write("Main JAR was found: %r\n" % effective_t3_jar)
106 sys.exit("Effective T3 model JAR file not found: %r" % effective_t3_model) 189 sys.exit("Effective T3 model JAR file not found: %r" % effective_t3_model)
107 190
108 # We will have write access wherever the output should be, 191 # We will have write access wherever the output should be,
109 temp_file = os.path.abspath(tabular_file + ".tmp") 192 if tabular_file == "/dev/stdout":
193 temp_file = os.path.abspath("effectivet3_tabular_output.tmp")
194 else:
195 temp_file = os.path.abspath(tabular_file + ".tmp")
110 196
111 # Use absolute paths since will change current directory... 197 # Use absolute paths since will change current directory...
112 tabular_file = os.path.abspath(tabular_file) 198 tabular_file = os.path.abspath(tabular_file)
113 fasta_file = os.path.abspath(fasta_file) 199 fasta_file = os.path.abspath(fasta_file)
114 200