annotate toolfactory/rgToolFactory2.py @ 28:ad564ab3cf7b draft

Uploaded
author fubar
date Fri, 31 Jul 2020 23:00:31 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
1 # rgToolFactory.py
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
2 # see https://github.com/fubar2/toolfactory
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
3 #
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
4 # copyright ross lazarus (ross stop lazarus at gmail stop com) May 2012
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
5 #
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
6 # all rights reserved
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
7 # Licensed under the LGPL
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
8 # suggestions for improvement and bug fixes welcome at https://github.com/fubar2/toolfactory
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
9 #
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
10 # July 2020: BCC was fun and I feel like rip van winkle after 5 years.
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
11 # Decided to
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
12 # 1. Fix the toolfactory so it works - done for simplest case
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
13 # 2. Fix planemo so the toolfactory function works
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
14 # 3. Rewrite bits using galaxyxml functions where that makes sense - done
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
15 #
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
16 # removed all the old complications including making the new tool use this same script
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
17 # galaxyxml now generates the tool xml https://github.com/hexylena/galaxyxml
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
18 # No support for automatic HTML file creation from arbitrary outputs
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
19 # TODO: add option to run that code as a post execution hook
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
20 # TODO: add additional history input parameters - currently only one
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
21
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
22 import sys
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
23 import subprocess
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
24 import shutil
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
25 import os
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
26 import time
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
27 import tempfile
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
28 import argparse
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
29 import tarfile
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
30 import re
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
31 import galaxyxml.tool as gxt
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
32 import galaxyxml.tool.parameters as gxtp
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
33 import logging
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
34
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
35
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
36 progname = os.path.split(sys.argv[0])[1]
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
37 myversion = 'V2.1 July 2020'
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
38 verbose = True
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
39 debug = True
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
40 toolFactoryURL = 'https://github.com/fubar2/toolfactory'
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
41 ourdelim = '~~~'
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
42
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
43
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
44 def timenow():
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
45 """return current time as a string
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
46 """
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
47 return time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(time.time()))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
48
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
49
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
50 def quote_non_numeric(s):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
51 """return a prequoted string for non-numerics
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
52 useful for perl and Rscript parameter passing?
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
53 """
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
54 try:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
55 _ = float(s)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
56 return s
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
57 except ValueError:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
58 return '"%s"' % s
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
59
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
60
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
61 html_escape_table = {
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
62 "&": "&",
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
63 ">": ">",
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
64 "<": "&lt;",
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
65 "$": r"\$"
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
66 }
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
67
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
68
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
69 def html_escape(text):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
70 """Produce entities within text."""
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
71 return "".join(html_escape_table.get(c, c) for c in text)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
72
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
73
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
74 def html_unescape(text):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
75 """Revert entities within text. Multiple character targets so use replace"""
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
76 t = text.replace('&amp;', '&')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
77 t = t.replace('&gt;', '>')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
78 t = t.replace('&lt;', '<')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
79 t = t.replace('\\$', '$')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
80 return t
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
81
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
82
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
83 def parse_citations(citations_text):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
84 """
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
85 """
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
86 citations = [c for c in citations_text.split("**ENTRY**") if c.strip()]
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
87 citation_tuples = []
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
88 for citation in citations:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
89 if citation.startswith("doi"):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
90 citation_tuples.append(("doi", citation[len("doi"):].strip()))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
91 else:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
92 citation_tuples.append(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
93 ("bibtex", citation[len("bibtex"):].strip()))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
94 return citation_tuples
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
95
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
96
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
97 class ScriptRunner:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
98 """Wrapper for an arbitrary script
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
99 uses galaxyxml
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
100
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
101 """
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
102
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
103 def __init__(self, args=None):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
104 """
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
105 prepare command line cl for running the tool here
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
106 and prepare elements needed for galaxyxml tool generation
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
107 """
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
108 lastclredirect = None
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
109 self.cl = []
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
110 aCL = self.cl.append
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
111 if args.output_dir: # simplify for the tool tarball
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
112 os.chdir(args.output_dir)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
113 self.args = args
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
114 # a sanitizer now does this but..
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
115 self.tool_name = re.sub('[^a-zA-Z0-9_]+', '', args.tool_name)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
116 self.tool_id = self.tool_name
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
117 self.xmlfile = '%s.xml' % self.tool_name
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
118 if self.args.interpreter_name == "Executable": # binary - no need
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
119 aCL(self.args.exe_package) # this little CL will just run
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
120 else: # a script has been provided
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
121 rx = open(self.args.script_path, 'r').readlines()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
122 # remove pesky dos line endings if needed
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
123 rx = [x.rstrip() for x in rx]
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
124 self.script = '\n'.join(rx)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
125 fhandle, self.sfile = tempfile.mkstemp(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
126 prefix=self.tool_name, suffix=".%s" % (args.interpreter_name))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
127 # use self.sfile as script source for Popen
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
128 tscript = open(self.sfile, 'w')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
129 tscript.write(self.script)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
130 tscript.close()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
131 self.indentedScript = " %s" % '\n'.join(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
132 [' %s' % html_escape(x) for x in rx]) # for restructured text in help
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
133 self.escapedScript = "%s" % '\n'.join(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
134 [' %s' % html_escape(x) for x in rx])
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
135 aCL(self.args.interpreter_name)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
136 aCL(self.sfile)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
137 self.elog = os.path.join(self.args.output_dir,
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
138 "%s_error.log" % self.tool_name)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
139 if args.output_dir: # may not want these complexities
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
140 self.tlog = os.path.join(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
141 self.args.output_dir, "%s_runner.log" % self.tool_name)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
142 art = '%s.%s' % (self.tool_name, args.interpreter_name)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
143 artpath = os.path.join(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
144 self.args.output_dir,
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
145 art) # need full path
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
146 # use self.sfile as script source for Popen
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
147 artifact = open(artpath, 'w')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
148 artifact.write(self.script)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
149 artifact.close()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
150 self.infile_paths = []
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
151 self.infile_format = []
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
152 self.infile_cl = []
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
153 self.infile_label = []
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
154 self.infile_help = []
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
155 if self.args.input_files:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
156 aif = [x.split(ourdelim) for x in self.args.input_files]
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
157 # transpose the input_files array passed as
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
158 # --input_files="$input_files~~~$CL~~~$input_formats~~~$input_label~~~$input_help"
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
159 laif = list(map(list, zip(*aif)))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
160 self.infile_paths, self.infile_cl, self.infile_format, self.infile_label, self.infile_help = laif
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
161 self.infile_name = []
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
162 # positionals have integers indicating order - need valid internal
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
163 # names
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
164 for i, scl in enumerate(self.infile_cl):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
165 if scl.isdigit():
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
166 scl = 'input%s' % scl
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
167 if scl.upper() in ['STDOUT', 'STDIN']:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
168 scl = 'input%d' % (i + 1)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
169 # make a list of internal names for each input file
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
170 self.infile_name.append(scl)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
171 # list all (cl param) pairs - positional needs sorting by cl index so decorate
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
172 clsuffix = []
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
173 clsuffix.append([self.args.output_cl, self.args.output_tab])
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
174 if self.args.parampass == '0': # only need two
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
175 aCL('<')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
176 aCL('%s' % self.infile_paths[0])
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
177 aCL('>')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
178 aCL('%s' % self.args.output_tab)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
179 else:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
180 for i, p in enumerate(self.infile_paths):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
181 # decorator is cl - sort for positional
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
182 clsuffix.append([self.infile_cl[i], p])
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
183 for p in self.args.additional_parameters:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
184 psplit = p.split(ourdelim)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
185 pform = psplit[5]
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
186 if pform == 'STDOUT':
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
187 lastclredirect = ['>', psplit[1]]
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
188 else:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
189 clsuffix.append([pform, psplit[1]]) # cl,value
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
190 clsuffix.sort()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
191 if self.args.parampass == "positional":
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
192 # inputs in order then params in order TODO fix ordering using
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
193 # self.infile_cl
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
194 for (k, v) in clsuffix:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
195 if ' ' in v:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
196 aCL("v")
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
197 else:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
198 aCL(v)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
199 elif self.args.parampass == "argparse":
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
200 # inputs then params in argparse named form
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
201 for (k, v) in clsuffix:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
202 if ' ' in v:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
203 aCL('--%s' % k)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
204 aCL('"%s"' % v)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
205 else:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
206 aCL('--%s' % k)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
207 aCL('%s' % v)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
208 if lastclredirect:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
209 for v in lastclredirect:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
210 aCL(v) # add the stdout parameter last
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
211 self.test1Output = '%s_test1_output.xls' % self.tool_name
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
212 self.test1HTML = '%s_test1_output.html' % self.tool_name
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
213
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
214 def makeXML(self):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
215 """
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
216 Create a Galaxy xml tool wrapper for the new script
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
217 Uses galaxyhtml
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
218 """
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
219 # need interp and executable (?script) or else executable only
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
220 if self.args.interpreter_name:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
221 exe = "$runMe" # our dynamic script from the tool builder
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
222 interp = self.args.interpreter_name
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
223 else:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
224 interp = None
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
225 exe = self.args.exe_package
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
226 assert exe is not None, 'No interpeter or executable passed in to makeXML'
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
227 tool = gxt.Tool(self.args.tool_name, self.tool_id,
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
228 self.args.tool_version, self.args.tool_desc, exe)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
229 if interp:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
230 tool.interpreter = interp
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
231 if self.args.help_text:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
232 helptext = open(self.args.help_text, 'r').readlines()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
233 # must html escape here too - thanks to Marius van den Beek
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
234 helptext = [html_escape(x) for x in helptext]
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
235 tool.help = ''.join([x for x in helptext])
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
236 else:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
237 tool.help = 'Please ask the tool author (%s) for help \
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
238 as none was supplied at tool generation\n' % (self.args.user_email)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
239 tool.version_command = None # do not want
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
240 inputs = gxtp.Inputs()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
241 outputs = gxtp.Outputs()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
242 requirements = gxtp.Requirements()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
243 testparam = []
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
244 is_positional = (self.args.parampass == 'positional')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
245 if self.args.include_dependencies == "yes":
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
246 requirements.append(gxtp.Requirement('package', 'ghostscript'))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
247 requirements.append(gxtp.Requirement('package', 'graphicsmagick'))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
248 if self.args.interpreter_name:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
249 if self.args.interpreter_name == 'python': # always needed for this runner script
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
250 requirements.append(gxtp.Requirement(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
251 'package', 'python', self.args.interpreter_version))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
252 elif self.args.interpreter_name not in ['bash', 'sh']:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
253 requirements.append(gxtp.Requirement(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
254 'package', self.args.interpreter_name, self.args.interpreter_version))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
255 else:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
256 if self.args.exe_package: # uses exe not interpreter
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
257 requirements.append(gxtp.Requirement(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
258 'package', self.args.exe_package, self.args.exe_package_version))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
259 tool.requirements = requirements
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
260 for i, infpath in enumerate(self.infile_paths):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
261 if self.args.parampass == 0:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
262 assert len(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
263 self.infile_name) == 1, 'Maximum one "<" if parampass is 0 - more than one input files supplied'
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
264 newname = self.infile_name[i]
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
265 if len(newname) > 1:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
266 ndash = 2
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
267 else:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
268 ndash = 1
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
269 if not len(self.infile_label[i]) > 0:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
270 alab = self.infile_name[i]
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
271 else:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
272 alab = self.infile_label[i]
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
273 aninput = gxtp.DataParam(self.infile_name[i], optional=False, label=alab, help=self.infile_help[i],
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
274 format=self.infile_format[i], multiple=False, num_dashes=ndash)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
275 if self.args.parampass == '0':
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
276 aninput.command_line_override = '< $%s' % self.infile_name[i]
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
277 aninput.positional = is_positional
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
278 inputs.append(aninput)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
279 for parm in self.args.additional_parameters:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
280 newname, newval, newlabel, newhelp, newtype, newcl = parm.split(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
281 ourdelim)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
282 if not len(newlabel) > 0:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
283 newlabel = newname
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
284 if len(newname) > 1:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
285 ndash = 2
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
286 else:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
287 ndash = 1
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
288 if newtype == "text":
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
289 aparm = gxtp.TextParam(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
290 newname, label=newlabel, help=newhelp, value=newval, num_dashes=ndash)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
291 elif newtype == "integer":
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
292 aparm = gxtp.IntegerParam(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
293 newname, label=newname, help=newhelp, value=newval, num_dashes=ndash)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
294 elif newtype == "float":
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
295 aparm = gxtp.FloatParam(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
296 newname, label=newname, help=newhelp, value=newval, num_dashes=ndash)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
297 else:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
298 raise ValueError('Unrecognised parameter type "%s" for\
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
299 additional parameter %s in makeXML' % (newtype, newname))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
300 aparm.positional = is_positional
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
301 inputs.append(aparm)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
302 tparm = gxtp.TestParam(newname, value=newval)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
303 testparam.append(tparm)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
304 tool.inputs = inputs
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
305 configfiles = gxtp.Configfiles()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
306 configfiles.append(gxtp.Configfile(name="runMe", text=self.script))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
307 tool.configfiles = configfiles
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
308 if self.args.output_tab:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
309 ext = self.args.output_format
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
310 aparm = gxtp.OutputData(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
311 self.args.output_cl, format=ext, num_dashes=ndash)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
312 if is_positional:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
313 aparm.command_line_override = '> $output1'
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
314 aparm.positional = is_positional
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
315 outputs.append(aparm)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
316 tool.outputs = outputs
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
317 tests = gxtp.Tests()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
318 test_a = gxtp.Test()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
319 ext = self.infile_format[0].split(',')[0]
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
320 if is_positional:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
321 param = gxtp.TestParam(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
322 'input1', value='input1.%s' % ext, ftype=ext)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
323 else:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
324 param = gxtp.TestParam(self.infile_name[0], value='%s.%s' % (
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
325 self.infile_name[0], ext), ftype=ext)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
326 test_a.append(param)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
327 param = gxtp.TestParam('job_name', value='test_a')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
328 test_a.append(param)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
329 param = gxtp.TestParam('runMe', value="$runMe")
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
330 test_a.append(param)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
331 for aparam in testparam:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
332 test_a.append(aparam)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
333 test_out = gxtp.TestOutput(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
334 name=self.args.output_cl, value=self.test1Output)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
335 test_a.append(test_out)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
336 tests.append(test_a)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
337 tool.tests = tests
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
338 tool.add_comment('Created by %s at %s using the Galaxy Tool Factory.' % (
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
339 self.args.user_email, timenow()))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
340 tool.add_comment('Source in git at: %s' % (toolFactoryURL))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
341 tool.add_comment(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
342 'Cite: Creating re-usable tools from scripts doi: 10.1093/bioinformatics/bts573')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
343 exml = tool.export()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
344 xf = open(self.xmlfile, 'w')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
345 xf.write(exml)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
346 xf.write('\n')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
347 xf.close()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
348 # ready for the tarball
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
349
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
350 def makeTooltar(self):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
351 """
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
352 a tool is a gz tarball with eg
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
353 /toolname/tool.xml /toolname/tool.py /toolname/test-data/test1_in.foo ...
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
354 """
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
355 retval = self.run()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
356 if retval:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
357 sys.stderr.write(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
358 '## Run failed. Cannot build yet. Please fix and retry')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
359 sys.exit(1)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
360 tdir = 'tdir_%s' % self.tool_name
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
361 if not os.path.exists(tdir):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
362 os.mkdir(tdir)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
363 self.makeXML()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
364 testdir = os.path.join(tdir, 'test-data')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
365 if not os.path.exists(testdir):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
366 os.mkdir(testdir) # make tests directory
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
367 for i, infile in enumerate(self.infile_paths):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
368 dest = os.path.join(testdir, '%s.%s' %
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
369 (self.infile_name[i], self.infile_format[i]))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
370 if infile != dest:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
371 shutil.copyfile(infile, dest)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
372 if self.args.output_tab and os.path.exists(self.args.output_tab):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
373 shutil.copyfile(self.args.output_tab,
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
374 os.path.join(testdir, self.test1Output))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
375 else:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
376 print('#### no output_tab %s exists' % self.args.output_tab)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
377 if self.args.output_dir:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
378 if os.path.exists(self.tlog):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
379 shutil.copyfile(self.tlog, os.path.join(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
380 testdir, 'test1_out.log'))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
381 stname = os.path.join(tdir, self.sfile)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
382 if not os.path.exists(stname):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
383 shutil.copyfile(self.sfile, stname)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
384 xtname = os.path.join(tdir, self.xmlfile)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
385 if not os.path.exists(xtname):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
386 shutil.copyfile(self.xmlfile, xtname)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
387 tarpath = "%s.tar.gz" % self.tool_name
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
388 tar = tarfile.open(tarpath, "w:gz")
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
389 tar.add(tdir, recursive=True, arcname='%s' % self.tool_name)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
390 tar.close()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
391 shutil.copyfile(tarpath, self.args.new_tool)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
392 shutil.rmtree(tdir)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
393 # TODO: replace with optional direct upload to local toolshed?
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
394 return retval
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
395
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
396 def run(self):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
397 """
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
398 Some devteam tools have this defensive stderr read so I'm keeping with the faith
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
399 Feel free to update.
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
400 """
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
401 logging.debug('run cl=%s' % str(self.cl))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
402 scl = ' '.join(self.cl)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
403 err = None
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
404 if self.args.parampass != '0':
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
405 ste = open(self.elog, 'wb')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
406 sto = open(self.tlog, 'wb')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
407 sto.write(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
408 bytes('## Executing Toolfactory generated command line = %s\n' % scl, "utf8"))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
409 sto.flush()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
410 p = subprocess.run(self.cl, shell=False, stdout=sto,
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
411 stderr=ste, cwd=self.args.output_dir)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
412 sto.close()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
413 ste.close()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
414 tmp_stderr = open(self.elog, 'rb')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
415 err = ''
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
416 buffsize = 1048576
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
417 try:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
418 while True:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
419 err += str(tmp_stderr.read(buffsize))
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
420 if not err or len(err) % buffsize != 0:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
421 break
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
422 except OverflowError:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
423 pass
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
424 tmp_stderr.close()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
425 retval = p.returncode
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
426 else: # work around special case of simple scripts that take stdin and write to stdout
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
427 sti = open(self.infile_paths[0], 'rb')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
428 sto = open(self.args.output_tab, 'wb')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
429 # must use shell to redirect
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
430 p = subprocess.run(self.cl, shell=False, stdout=sto, stdin=sti)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
431 retval = p.returncode
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
432 sto.close()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
433 sti.close()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
434 if self.args.output_dir:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
435 if p.returncode != 0 and err: # problem
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
436 sys.stderr.write(err)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
437 logging.debug('run done')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
438 return retval
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
439
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
440
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
441 def main():
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
442 """
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
443 This is a Galaxy wrapper. It expects to be called by a special purpose tool.xml as:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
444 <command interpreter="python">rgBaseScriptWrapper.py --script_path "$scriptPath" --tool_name "foo" --interpreter "Rscript"
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
445 </command>
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
446 """
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
447 parser = argparse.ArgumentParser()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
448 a = parser.add_argument
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
449 a('--script_path', default='')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
450 a('--tool_name', default=None)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
451 a('--interpreter_name', default=None)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
452 a('--interpreter_version', default=None)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
453 a('--exe_package', default=None)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
454 a('--exe_package_version', default=None)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
455 a('--output_dir', default='./')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
456 a('--input_files', default=[], action="append")
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
457 a("--input_formats", default="tabular")
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
458 a('--output_tab', default=None)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
459 a('--output_format', default='tabular')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
460 a('--output_cl', default=None)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
461 a('--user_email', default='Unknown')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
462 a('--bad_user', default=None)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
463 a('--make_Tool', default=None)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
464 a('--help_text', default=None)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
465 a('--tool_desc', default=None)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
466 a('--new_tool', default=None)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
467 a('--tool_version', default=None)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
468 a('--include_dependencies', default=None)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
469 a('--citations', default=None)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
470 a('--additional_parameters', dest='additional_parameters',
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
471 action='append', default=[])
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
472 a('--edit_additional_parameters', action="store_true", default=False)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
473 a('--parampass', default="positional")
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
474 args = parser.parse_args()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
475 assert not args.bad_user, 'UNAUTHORISED: %s is NOT authorized to use this tool until Galaxy admin adds %s to "admin_users" in the Galaxy configuration file' % (
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
476 args.bad_user, args.bad_user)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
477 assert args.tool_name, '## Tool Factory expects a tool name - eg --tool_name=DESeq'
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
478 assert (args.interpreter_name or args.exe_package), '## Tool Factory wrapper expects an interpreter - eg --interpreter_name=Rscript or an executable package findable by the dependency management package'
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
479 assert args.exe_package or (len(args.script_path) > 0 and os.path.isfile(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
480 args.script_path)), '## Tool Factory wrapper expects a script path - eg --script_path=foo.R if no executable'
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
481 if args.output_dir:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
482 try:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
483 os.makedirs(args.output_dir)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
484 except BaseException:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
485 pass
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
486 args.input_files = [x.replace('"', '').replace("'", '')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
487 for x in args.input_files]
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
488 # remove quotes we need to deal with spaces in CL params
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
489 for i, x in enumerate(args.additional_parameters):
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
490 args.additional_parameters[i] = args.additional_parameters[i].replace(
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
491 '"', '')
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
492 r = ScriptRunner(args)
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
493 if args.make_Tool:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
494 retcode = r.makeTooltar()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
495 else:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
496 retcode = r.run()
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
497 if retcode:
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
498 sys.exit(retcode) # indicate failure to job runner
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
499
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
500
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
501 if __name__ == "__main__":
ad564ab3cf7b Uploaded
fubar
parents:
diff changeset
502 main()