84
|
1 # replace with shebang for biocontainer
|
48
|
2 # see https://github.com/fubar2/toolfactory
|
|
3 #
|
|
4 # copyright ross lazarus (ross stop lazarus at gmail stop com) May 2012
|
|
5 #
|
|
6 # all rights reserved
|
|
7 # Licensed under the LGPL
|
49
|
8 # suggestions for improvement and bug fixes welcome at
|
|
9 # https://github.com/fubar2/toolfactory
|
48
|
10 #
|
|
11 # July 2020: BCC was fun and I feel like rip van winkle after 5 years.
|
|
12 # Decided to
|
|
13 # 1. Fix the toolfactory so it works - done for simplest case
|
|
14 # 2. Fix planemo so the toolfactory function works
|
|
15 # 3. Rewrite bits using galaxyxml functions where that makes sense - done
|
|
16 #
|
|
17 # removed all the old complications including making the new tool use this same script
|
|
18 # galaxyxml now generates the tool xml https://github.com/hexylena/galaxyxml
|
|
19 # No support for automatic HTML file creation from arbitrary outputs
|
|
20 # essential problem is to create two command lines - one for the tool xml and a different
|
|
21 # one to run the executable with the supplied test data and settings
|
|
22 # Be simpler to write the tool, then run it with planemo and soak up the test outputs.
|
95
|
23 # well well. sh run_tests.sh --id rgtf2 --report_file tool_tests_tool_conf.html functional.test_toolbox
|
|
24 # does the needful. Use GALAXY_TEST_SAVE /foo to save outputs - only the tar.gz - not the rest sadly
|
|
25 # GALAXY_TEST_NO_CLEANUP GALAXY_TEST_TMP_DIR=wherever
|
99
|
26 # planemo test --engine docker_galaxy --test_data ./test-data/ --docker_extra_volume ./test-data rgToolFactory2.xml
|
48
|
27
|
|
28 import argparse
|
76
|
29 import datetime
|
|
30 import json
|
48
|
31 import logging
|
|
32 import os
|
|
33 import re
|
|
34 import shutil
|
|
35 import subprocess
|
|
36 import sys
|
|
37 import tarfile
|
|
38 import tempfile
|
|
39 import time
|
|
40
|
75
|
41
|
100
|
42 from bioblend import ConnectionError
|
63
|
43 from bioblend import toolshed
|
|
44
|
98
|
45 # import docker
|
|
46
|
48
|
47 import galaxyxml.tool as gxt
|
|
48 import galaxyxml.tool.parameters as gxtp
|
|
49
|
|
50 import lxml
|
|
51
|
|
52 import yaml
|
|
53
|
|
54 myversion = "V2.1 July 2020"
|
|
55 verbose = True
|
|
56 debug = True
|
|
57 toolFactoryURL = "https://github.com/fubar2/toolfactory"
|
|
58 ourdelim = "~~~"
|
50
|
59 ALOT = 10000000 # srsly. command or test overrides use read() so just in case
|
49
|
60 STDIOXML = """<stdio>
|
|
61 <exit_code range="100:" level="debug" description="shite happens" />
|
|
62 </stdio>"""
|
48
|
63
|
|
64 # --input_files="$input_files~~~$CL~~~$input_formats~~~$input_label
|
|
65 # ~~~$input_help"
|
|
66 IPATHPOS = 0
|
|
67 ICLPOS = 1
|
|
68 IFMTPOS = 2
|
|
69 ILABPOS = 3
|
|
70 IHELPOS = 4
|
|
71 IOCLPOS = 5
|
|
72
|
49
|
73 # --output_files "$otab.history_name~~~$otab.history_format~~~$otab.CL~~~otab.history_test
|
48
|
74 ONAMEPOS = 0
|
|
75 OFMTPOS = 1
|
|
76 OCLPOS = 2
|
49
|
77 OTESTPOS = 3
|
|
78 OOCLPOS = 4
|
|
79
|
48
|
80
|
|
81 # --additional_parameters="$i.param_name~~~$i.param_value~~~
|
|
82 # $i.param_label~~~$i.param_help~~~$i.param_type~~~$i.CL~~~i$.param_CLoverride"
|
|
83 ANAMEPOS = 0
|
|
84 AVALPOS = 1
|
|
85 ALABPOS = 2
|
|
86 AHELPPOS = 3
|
|
87 ATYPEPOS = 4
|
|
88 ACLPOS = 5
|
|
89 AOVERPOS = 6
|
|
90 AOCLPOS = 7
|
|
91
|
|
92
|
|
93 foo = len(lxml.__version__)
|
|
94 # fug you, flake8. Say my name!
|
49
|
95 FAKEEXE = "~~~REMOVE~~~ME~~~"
|
|
96 # need this until a PR/version bump to fix galaxyxml prepending the exe even
|
|
97 # with override.
|
|
98
|
48
|
99
|
|
100 def timenow():
|
75
|
101 """return current time as a string"""
|
48
|
102 return time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(time.time()))
|
|
103
|
|
104
|
|
105 def quote_non_numeric(s):
|
|
106 """return a prequoted string for non-numerics
|
|
107 useful for perl and Rscript parameter passing?
|
|
108 """
|
|
109 try:
|
|
110 _ = float(s)
|
|
111 return s
|
|
112 except ValueError:
|
|
113 return '"%s"' % s
|
|
114
|
|
115
|
|
116 html_escape_table = {"&": "&", ">": ">", "<": "<", "$": r"\$"}
|
|
117
|
|
118
|
|
119 def html_escape(text):
|
|
120 """Produce entities within text."""
|
|
121 return "".join(html_escape_table.get(c, c) for c in text)
|
|
122
|
|
123
|
|
124 def html_unescape(text):
|
|
125 """Revert entities within text. Multiple character targets so use replace"""
|
|
126 t = text.replace("&", "&")
|
|
127 t = t.replace(">", ">")
|
|
128 t = t.replace("<", "<")
|
|
129 t = t.replace("\\$", "$")
|
|
130 return t
|
|
131
|
|
132
|
|
133 def parse_citations(citations_text):
|
75
|
134 """"""
|
48
|
135 citations = [c for c in citations_text.split("**ENTRY**") if c.strip()]
|
|
136 citation_tuples = []
|
|
137 for citation in citations:
|
|
138 if citation.startswith("doi"):
|
|
139 citation_tuples.append(("doi", citation[len("doi") :].strip()))
|
|
140 else:
|
49
|
141 citation_tuples.append(("bibtex", citation[len("bibtex") :].strip()))
|
48
|
142 return citation_tuples
|
|
143
|
|
144
|
|
145 class ScriptRunner:
|
|
146 """Wrapper for an arbitrary script
|
|
147 uses galaxyxml
|
|
148
|
|
149 """
|
|
150
|
|
151 def __init__(self, args=None):
|
|
152 """
|
|
153 prepare command line cl for running the tool here
|
|
154 and prepare elements needed for galaxyxml tool generation
|
|
155 """
|
|
156 self.infiles = [x.split(ourdelim) for x in args.input_files]
|
|
157 self.outfiles = [x.split(ourdelim) for x in args.output_files]
|
|
158 self.addpar = [x.split(ourdelim) for x in args.additional_parameters]
|
|
159 self.args = args
|
|
160 self.cleanuppar()
|
|
161 self.lastclredirect = None
|
|
162 self.lastxclredirect = None
|
|
163 self.cl = []
|
|
164 self.xmlcl = []
|
|
165 self.is_positional = self.args.parampass == "positional"
|
63
|
166 if self.args.sysexe:
|
49
|
167 self.executeme = self.args.sysexe
|
63
|
168 else:
|
|
169 if self.args.packages:
|
|
170 self.executeme = self.args.packages.split(",")[0].split(":")[0]
|
|
171 else:
|
|
172 self.executeme = None
|
48
|
173 aCL = self.cl.append
|
49
|
174 aXCL = self.xmlcl.append
|
48
|
175 assert args.parampass in [
|
|
176 "0",
|
|
177 "argparse",
|
|
178 "positional",
|
49
|
179 ], 'args.parampass must be "0","positional" or "argparse"'
|
48
|
180 self.tool_name = re.sub("[^a-zA-Z0-9_]+", "", args.tool_name)
|
|
181 self.tool_id = self.tool_name
|
50
|
182 self.newtool = gxt.Tool(
|
48
|
183 self.args.tool_name,
|
|
184 self.tool_id,
|
|
185 self.args.tool_version,
|
|
186 self.args.tool_desc,
|
50
|
187 FAKEEXE,
|
48
|
188 )
|
76
|
189 self.newtarpath = "toolfactory_%s.tgz" % self.tool_name
|
98
|
190 self.tooloutdir = "./tfout"
|
|
191 self.repdir = "./TF_run_report_tempdir"
|
48
|
192 self.testdir = os.path.join(self.tooloutdir, "test-data")
|
|
193 if not os.path.exists(self.tooloutdir):
|
|
194 os.mkdir(self.tooloutdir)
|
|
195 if not os.path.exists(self.testdir):
|
|
196 os.mkdir(self.testdir) # make tests directory
|
|
197 if not os.path.exists(self.repdir):
|
|
198 os.mkdir(self.repdir)
|
|
199 self.tinputs = gxtp.Inputs()
|
|
200 self.toutputs = gxtp.Outputs()
|
|
201 self.testparam = []
|
49
|
202 if self.args.script_path:
|
|
203 self.prepScript()
|
|
204 if self.args.command_override:
|
|
205 scos = open(self.args.command_override, "r").readlines()
|
|
206 self.command_override = [x.rstrip() for x in scos]
|
|
207 else:
|
|
208 self.command_override = None
|
|
209 if self.args.test_override:
|
|
210 stos = open(self.args.test_override, "r").readlines()
|
|
211 self.test_override = [x.rstrip() for x in stos]
|
|
212 else:
|
|
213 self.test_override = None
|
50
|
214 if self.args.cl_prefix: # DIY CL start
|
49
|
215 clp = self.args.cl_prefix.split(" ")
|
|
216 for c in clp:
|
|
217 aCL(c)
|
|
218 aXCL(c)
|
|
219 else:
|
56
|
220 if self.args.script_path:
|
|
221 aCL(self.executeme)
|
|
222 aCL(self.sfile)
|
|
223 aXCL(self.executeme)
|
|
224 aXCL("$runme")
|
48
|
225 else:
|
56
|
226 aCL(self.executeme) # this little CL will just run
|
|
227 aXCL(self.executeme)
|
50
|
228 self.elog = os.path.join(self.repdir, "%s_error_log.txt" % self.tool_name)
|
|
229 self.tlog = os.path.join(self.repdir, "%s_runner_log.txt" % self.tool_name)
|
48
|
230
|
|
231 if self.args.parampass == "0":
|
|
232 self.clsimple()
|
|
233 else:
|
|
234 clsuffix = []
|
|
235 xclsuffix = []
|
|
236 for i, p in enumerate(self.infiles):
|
|
237 if p[IOCLPOS] == "STDIN":
|
|
238 appendme = [
|
|
239 p[IOCLPOS],
|
|
240 p[ICLPOS],
|
|
241 p[IPATHPOS],
|
|
242 "< %s" % p[IPATHPOS],
|
|
243 ]
|
|
244 xappendme = [
|
|
245 p[IOCLPOS],
|
|
246 p[ICLPOS],
|
|
247 p[IPATHPOS],
|
|
248 "< $%s" % p[ICLPOS],
|
|
249 ]
|
|
250 else:
|
|
251 appendme = [p[IOCLPOS], p[ICLPOS], p[IPATHPOS], ""]
|
|
252 xappendme = [p[IOCLPOS], p[ICLPOS], "$%s" % p[ICLPOS], ""]
|
|
253 clsuffix.append(appendme)
|
|
254 xclsuffix.append(xappendme)
|
|
255 for i, p in enumerate(self.outfiles):
|
|
256 if p[OOCLPOS] == "STDOUT":
|
|
257 self.lastclredirect = [">", p[ONAMEPOS]]
|
|
258 self.lastxclredirect = [">", "$%s" % p[OCLPOS]]
|
|
259 else:
|
|
260 clsuffix.append([p[OOCLPOS], p[OCLPOS], p[ONAMEPOS], ""])
|
49
|
261 xclsuffix.append([p[OOCLPOS], p[OCLPOS], "$%s" % p[ONAMEPOS], ""])
|
48
|
262 for p in self.addpar:
|
49
|
263 clsuffix.append([p[AOCLPOS], p[ACLPOS], p[AVALPOS], p[AOVERPOS]])
|
48
|
264 xclsuffix.append(
|
|
265 [p[AOCLPOS], p[ACLPOS], '"$%s"' % p[ANAMEPOS], p[AOVERPOS]]
|
|
266 )
|
|
267 clsuffix.sort()
|
|
268 xclsuffix.sort()
|
|
269 self.xclsuffix = xclsuffix
|
|
270 self.clsuffix = clsuffix
|
|
271 if self.args.parampass == "positional":
|
|
272 self.clpositional()
|
|
273 else:
|
|
274 self.clargparse()
|
|
275
|
|
276 def prepScript(self):
|
|
277 rx = open(self.args.script_path, "r").readlines()
|
|
278 rx = [x.rstrip() for x in rx]
|
|
279 rxcheck = [x.strip() for x in rx if x.strip() > ""]
|
|
280 assert len(rxcheck) > 0, "Supplied script is empty. Cannot run"
|
|
281 self.script = "\n".join(rx)
|
|
282 fhandle, self.sfile = tempfile.mkstemp(
|
49
|
283 prefix=self.tool_name, suffix="_%s" % (self.executeme)
|
48
|
284 )
|
|
285 tscript = open(self.sfile, "w")
|
|
286 tscript.write(self.script)
|
|
287 tscript.close()
|
49
|
288 self.indentedScript = " %s" % "\n".join([" %s" % html_escape(x) for x in rx])
|
|
289 self.escapedScript = "%s" % "\n".join([" %s" % html_escape(x) for x in rx])
|
|
290 art = "%s.%s" % (self.tool_name, self.executeme)
|
48
|
291 artifact = open(art, "wb")
|
|
292 artifact.write(bytes(self.script, "utf8"))
|
|
293 artifact.close()
|
|
294
|
|
295 def cleanuppar(self):
|
|
296 """ positional parameters are complicated by their numeric ordinal"""
|
|
297 for i, p in enumerate(self.infiles):
|
|
298 if self.args.parampass == "positional":
|
75
|
299 assert p[
|
|
300 ICLPOS
|
|
301 ].isdigit(), "Positional parameters must be ordinal integers - got %s for %s" % (
|
|
302 p[ICLPOS],
|
|
303 p[ILABPOS],
|
48
|
304 )
|
|
305 p.append(p[ICLPOS])
|
|
306 if p[ICLPOS].isdigit() or self.args.parampass == "0":
|
|
307 scl = "input%d" % (i + 1)
|
|
308 p[ICLPOS] = scl
|
|
309 self.infiles[i] = p
|
|
310 for i, p in enumerate(
|
|
311 self.outfiles
|
|
312 ): # trying to automagically gather using extensions
|
|
313 if self.args.parampass == "positional" and p[OCLPOS] != "STDOUT":
|
75
|
314 assert p[
|
|
315 OCLPOS
|
|
316 ].isdigit(), "Positional parameters must be ordinal integers - got %s for %s" % (
|
|
317 p[OCLPOS],
|
|
318 p[ONAMEPOS],
|
48
|
319 )
|
|
320 p.append(p[OCLPOS])
|
|
321 if p[OCLPOS].isdigit() or p[OCLPOS] == "STDOUT":
|
|
322 scl = p[ONAMEPOS]
|
|
323 p[OCLPOS] = scl
|
|
324 self.outfiles[i] = p
|
|
325 for i, p in enumerate(self.addpar):
|
|
326 if self.args.parampass == "positional":
|
75
|
327 assert p[
|
|
328 ACLPOS
|
|
329 ].isdigit(), "Positional parameters must be ordinal integers - got %s for %s" % (
|
|
330 p[ACLPOS],
|
|
331 p[ANAMEPOS],
|
48
|
332 )
|
|
333 p.append(p[ACLPOS])
|
|
334 if p[ACLPOS].isdigit():
|
|
335 scl = "input%s" % p[ACLPOS]
|
|
336 p[ACLPOS] = scl
|
|
337 self.addpar[i] = p
|
|
338
|
|
339 def clsimple(self):
|
75
|
340 """no parameters - uses < and > for i/o"""
|
48
|
341 aCL = self.cl.append
|
|
342 aXCL = self.xmlcl.append
|
62
|
343
|
|
344 if len(self.infiles) > 0:
|
|
345 aCL("<")
|
|
346 aCL(self.infiles[0][IPATHPOS])
|
|
347 aXCL("<")
|
|
348 aXCL("$%s" % self.infiles[0][ICLPOS])
|
|
349 if len(self.outfiles) > 0:
|
|
350 aCL(">")
|
|
351 aCL(self.outfiles[0][OCLPOS])
|
|
352 aXCL(">")
|
|
353 aXCL("$%s" % self.outfiles[0][ONAMEPOS])
|
48
|
354
|
|
355 def clpositional(self):
|
|
356 # inputs in order then params
|
|
357 aCL = self.cl.append
|
|
358 for (o_v, k, v, koverride) in self.clsuffix:
|
|
359 if " " in v:
|
|
360 aCL("%s" % v)
|
|
361 else:
|
|
362 aCL(v)
|
|
363 aXCL = self.xmlcl.append
|
|
364 for (o_v, k, v, koverride) in self.xclsuffix:
|
|
365 aXCL(v)
|
|
366 if self.lastxclredirect:
|
|
367 aXCL(self.lastxclredirect[0])
|
|
368 aXCL(self.lastxclredirect[1])
|
|
369
|
|
370 def clargparse(self):
|
75
|
371 """argparse style"""
|
48
|
372 aCL = self.cl.append
|
|
373 aXCL = self.xmlcl.append
|
|
374 # inputs then params in argparse named form
|
|
375 for (o_v, k, v, koverride) in self.xclsuffix:
|
|
376 if koverride > "":
|
|
377 k = koverride
|
|
378 elif len(k.strip()) == 1:
|
|
379 k = "-%s" % k
|
|
380 else:
|
|
381 k = "--%s" % k
|
|
382 aXCL(k)
|
|
383 aXCL(v)
|
|
384 for (o_v, k, v, koverride) in self.clsuffix:
|
|
385 if koverride > "":
|
|
386 k = koverride
|
|
387 elif len(k.strip()) == 1:
|
|
388 k = "-%s" % k
|
|
389 else:
|
|
390 k = "--%s" % k
|
|
391 aCL(k)
|
|
392 aCL(v)
|
|
393
|
|
394 def getNdash(self, newname):
|
|
395 if self.is_positional:
|
|
396 ndash = 0
|
|
397 else:
|
|
398 ndash = 2
|
|
399 if len(newname) < 2:
|
|
400 ndash = 1
|
|
401 return ndash
|
|
402
|
|
403 def doXMLparam(self):
|
|
404 """flake8 made me do this..."""
|
|
405 for p in self.outfiles:
|
49
|
406 newname, newfmt, newcl, test, oldcl = p
|
48
|
407 ndash = self.getNdash(newcl)
|
|
408 aparm = gxtp.OutputData(newcl, format=newfmt, num_dashes=ndash)
|
|
409 aparm.positional = self.is_positional
|
|
410 if self.is_positional:
|
|
411 if oldcl == "STDOUT":
|
|
412 aparm.positional = 9999999
|
|
413 aparm.command_line_override = "> $%s" % newcl
|
|
414 else:
|
|
415 aparm.positional = int(oldcl)
|
|
416 aparm.command_line_override = "$%s" % newcl
|
|
417 self.toutputs.append(aparm)
|
49
|
418 usetest = None
|
|
419 ld = None
|
50
|
420 if test > "":
|
|
421 if test.startswith("diff"):
|
|
422 usetest = "diff"
|
|
423 if test.split(":")[1].isdigit:
|
|
424 ld = int(test.split(":")[1])
|
49
|
425 else:
|
|
426 usetest = test
|
50
|
427 tp = gxtp.TestOutput(
|
|
428 name=newcl,
|
|
429 value="%s_sample" % newcl,
|
|
430 format=newfmt,
|
|
431 compare=usetest,
|
|
432 lines_diff=ld,
|
|
433 delta=None,
|
|
434 )
|
48
|
435 self.testparam.append(tp)
|
|
436 for p in self.infiles:
|
|
437 newname = p[ICLPOS]
|
|
438 newfmt = p[IFMTPOS]
|
|
439 ndash = self.getNdash(newname)
|
|
440 if not len(p[ILABPOS]) > 0:
|
|
441 alab = p[ICLPOS]
|
|
442 else:
|
|
443 alab = p[ILABPOS]
|
|
444 aninput = gxtp.DataParam(
|
|
445 newname,
|
|
446 optional=False,
|
|
447 label=alab,
|
|
448 help=p[IHELPOS],
|
|
449 format=newfmt,
|
|
450 multiple=False,
|
|
451 num_dashes=ndash,
|
|
452 )
|
|
453 aninput.positional = self.is_positional
|
|
454 self.tinputs.append(aninput)
|
|
455 tparm = gxtp.TestParam(name=newname, value="%s_sample" % newname)
|
|
456 self.testparam.append(tparm)
|
|
457 for p in self.addpar:
|
|
458 newname, newval, newlabel, newhelp, newtype, newcl, override, oldcl = p
|
|
459 if not len(newlabel) > 0:
|
|
460 newlabel = newname
|
|
461 ndash = self.getNdash(newname)
|
|
462 if newtype == "text":
|
|
463 aparm = gxtp.TextParam(
|
|
464 newname,
|
|
465 label=newlabel,
|
|
466 help=newhelp,
|
|
467 value=newval,
|
|
468 num_dashes=ndash,
|
|
469 )
|
|
470 elif newtype == "integer":
|
|
471 aparm = gxtp.IntegerParam(
|
|
472 newname,
|
|
473 label=newname,
|
|
474 help=newhelp,
|
|
475 value=newval,
|
|
476 num_dashes=ndash,
|
|
477 )
|
|
478 elif newtype == "float":
|
|
479 aparm = gxtp.FloatParam(
|
|
480 newname,
|
|
481 label=newname,
|
|
482 help=newhelp,
|
|
483 value=newval,
|
|
484 num_dashes=ndash,
|
|
485 )
|
|
486 else:
|
|
487 raise ValueError(
|
|
488 'Unrecognised parameter type "%s" for\
|
|
489 additional parameter %s in makeXML'
|
|
490 % (newtype, newname)
|
|
491 )
|
|
492 aparm.positional = self.is_positional
|
|
493 if self.is_positional:
|
63
|
494 aparm.positional = int(oldcl)
|
48
|
495 self.tinputs.append(aparm)
|
63
|
496 tparm = gxtp.TestParam(newname, value=newval)
|
48
|
497 self.testparam.append(tparm)
|
|
498
|
|
499 def doNoXMLparam(self):
|
49
|
500 """filter style package - stdin to stdout"""
|
62
|
501 if len(self.infiles) > 0:
|
|
502 alab = self.infiles[0][ILABPOS]
|
|
503 if len(alab) == 0:
|
|
504 alab = self.infiles[0][ICLPOS]
|
|
505 max1s = (
|
|
506 "Maximum one input if parampass is 0 but multiple input files supplied - %s"
|
|
507 % str(self.infiles)
|
|
508 )
|
|
509 assert len(self.infiles) == 1, max1s
|
|
510 newname = self.infiles[0][ICLPOS]
|
|
511 aninput = gxtp.DataParam(
|
|
512 newname,
|
|
513 optional=False,
|
|
514 label=alab,
|
|
515 help=self.infiles[0][IHELPOS],
|
|
516 format=self.infiles[0][IFMTPOS],
|
|
517 multiple=False,
|
|
518 num_dashes=0,
|
|
519 )
|
|
520 aninput.command_line_override = "< $%s" % newname
|
|
521 aninput.positional = self.is_positional
|
|
522 self.tinputs.append(aninput)
|
|
523 tp = gxtp.TestParam(name=newname, value="%s_sample" % newname)
|
|
524 self.testparam.append(tp)
|
63
|
525 if len(self.outfiles) > 0:
|
62
|
526 newname = self.outfiles[0][OCLPOS]
|
|
527 newfmt = self.outfiles[0][OFMTPOS]
|
|
528 anout = gxtp.OutputData(newname, format=newfmt, num_dashes=0)
|
|
529 anout.command_line_override = "> $%s" % newname
|
|
530 anout.positional = self.is_positional
|
|
531 self.toutputs.append(anout)
|
75
|
532 tp = gxtp.TestOutput(
|
|
533 name=newname, value="%s_sample" % newname, format=newfmt
|
|
534 )
|
62
|
535 self.testparam.append(tp)
|
48
|
536
|
|
537 def makeXML(self):
|
|
538 """
|
|
539 Create a Galaxy xml tool wrapper for the new script
|
|
540 Uses galaxyhtml
|
|
541 Hmmm. How to get the command line into correct order...
|
|
542 """
|
49
|
543 if self.command_override:
|
56
|
544 self.newtool.command_override = self.command_override # config file
|
48
|
545 else:
|
56
|
546 self.newtool.command_override = self.xmlcl
|
48
|
547 if self.args.help_text:
|
|
548 helptext = open(self.args.help_text, "r").readlines()
|
50
|
549 safertext = [html_escape(x) for x in helptext]
|
63
|
550 if False and self.args.script_path:
|
75
|
551 scrp = self.script.split("\n")
|
|
552 scrpt = [" %s" % x for x in scrp] # try to stop templating
|
|
553 scrpt.insert(0, "```\n")
|
50
|
554 if len(scrpt) > 300:
|
75
|
555 safertext = (
|
|
556 safertext + scrpt[:100] + \
|
|
557 [">500 lines - stuff deleted", "......"] + scrpt[-100:]
|
|
558 )
|
50
|
559 else:
|
|
560 safertext = safertext + scrpt
|
|
561 safertext.append("\n```")
|
62
|
562 self.newtool.help = "\n".join([x for x in safertext])
|
48
|
563 else:
|
50
|
564 self.newtool.help = (
|
48
|
565 "Please ask the tool author (%s) for help \
|
|
566 as none was supplied at tool generation\n"
|
|
567 % (self.args.user_email)
|
|
568 )
|
50
|
569 self.newtool.version_command = None # do not want
|
48
|
570 requirements = gxtp.Requirements()
|
49
|
571 if self.args.packages:
|
|
572 for d in self.args.packages.split(","):
|
|
573 if ":" in d:
|
|
574 packg, ver = d.split(":")
|
|
575 else:
|
|
576 packg = d
|
|
577 ver = ""
|
50
|
578 requirements.append(
|
|
579 gxtp.Requirement("package", packg.strip(), ver.strip())
|
|
580 )
|
|
581 self.newtool.requirements = requirements
|
48
|
582 if self.args.parampass == "0":
|
|
583 self.doNoXMLparam()
|
|
584 else:
|
|
585 self.doXMLparam()
|
50
|
586 self.newtool.outputs = self.toutputs
|
|
587 self.newtool.inputs = self.tinputs
|
|
588 if self.args.script_path:
|
48
|
589 configfiles = gxtp.Configfiles()
|
49
|
590 configfiles.append(gxtp.Configfile(name="runme", text=self.script))
|
50
|
591 self.newtool.configfiles = configfiles
|
48
|
592 tests = gxtp.Tests()
|
|
593 test_a = gxtp.Test()
|
|
594 for tp in self.testparam:
|
|
595 test_a.append(tp)
|
|
596 tests.append(test_a)
|
50
|
597 self.newtool.tests = tests
|
|
598 self.newtool.add_comment(
|
48
|
599 "Created by %s at %s using the Galaxy Tool Factory."
|
|
600 % (self.args.user_email, timenow())
|
|
601 )
|
50
|
602 self.newtool.add_comment("Source in git at: %s" % (toolFactoryURL))
|
|
603 self.newtool.add_comment(
|
48
|
604 "Cite: Creating re-usable tools from scripts doi: \
|
|
605 10.1093/bioinformatics/bts573"
|
|
606 )
|
50
|
607 exml0 = self.newtool.export()
|
49
|
608 exml = exml0.replace(FAKEEXE, "") # temporary work around until PR accepted
|
50
|
609 if (
|
|
610 self.test_override
|
|
611 ): # cannot do this inside galaxyxml as it expects lxml objects for tests
|
|
612 part1 = exml.split("<tests>")[0]
|
|
613 part2 = exml.split("</tests>")[1]
|
|
614 fixed = "%s\n%s\n%s" % (part1, self.test_override, part2)
|
49
|
615 exml = fixed
|
63
|
616 exml = exml.replace('range="1:"', 'range="1000:"')
|
49
|
617 xf = open("%s.xml" % self.tool_name, "w")
|
48
|
618 xf.write(exml)
|
|
619 xf.write("\n")
|
|
620 xf.close()
|
|
621 # ready for the tarball
|
|
622
|
|
623 def run(self):
|
|
624 """
|
50
|
625 generate test outputs by running a command line
|
56
|
626 won't work if command or test override in play - planemo is the
|
50
|
627 easiest way to generate test outputs for that case so is
|
|
628 automagically selected
|
48
|
629 """
|
|
630 scl = " ".join(self.cl)
|
|
631 err = None
|
|
632 if self.args.parampass != "0":
|
56
|
633 if os.path.exists(self.elog):
|
|
634 ste = open(self.elog, "a")
|
|
635 else:
|
|
636 ste = open(self.elog, "w")
|
48
|
637 if self.lastclredirect:
|
49
|
638 sto = open(self.lastclredirect[1], "wb") # is name of an output file
|
48
|
639 else:
|
56
|
640 if os.path.exists(self.tlog):
|
|
641 sto = open(self.tlog, "a")
|
|
642 else:
|
|
643 sto = open(self.tlog, "w")
|
48
|
644 sto.write(
|
75
|
645 "## Executing Toolfactory generated command line = %s\n" % scl
|
48
|
646 )
|
|
647 sto.flush()
|
98
|
648 subp = subprocess.run(self.cl, shell=False, stdout=sto, stderr=ste)
|
48
|
649 sto.close()
|
|
650 ste.close()
|
98
|
651 retval = subp.returncode
|
49
|
652 else: # work around special case - stdin and write to stdout
|
62
|
653 if len(self.infiles) > 0:
|
|
654 sti = open(self.infiles[0][IPATHPOS], "rb")
|
|
655 else:
|
63
|
656 sti = sys.stdin
|
62
|
657 if len(self.outfiles) > 0:
|
|
658 sto = open(self.outfiles[0][ONAMEPOS], "wb")
|
|
659 else:
|
|
660 sto = sys.stdout
|
98
|
661 subp = subprocess.run(self.cl, shell=False, stdout=sto, stdin=sti)
|
75
|
662 sto.write("## Executing Toolfactory generated command line = %s\n" % scl)
|
98
|
663 retval = subp.returncode
|
48
|
664 sto.close()
|
|
665 sti.close()
|
|
666 if os.path.isfile(self.tlog) and os.stat(self.tlog).st_size == 0:
|
|
667 os.unlink(self.tlog)
|
|
668 if os.path.isfile(self.elog) and os.stat(self.elog).st_size == 0:
|
|
669 os.unlink(self.elog)
|
|
670 if retval != 0 and err: # problem
|
|
671 sys.stderr.write(err)
|
|
672 logging.debug("run done")
|
|
673 return retval
|
|
674
|
99
|
675
|
|
676 def gal_tool_test(self):
|
|
677 """
|
|
678 This handy script writes test outputs even if they don't exist
|
|
679 galaxy-tool-test [-h] [-u GALAXY_URL] [-k KEY] [-a ADMIN_KEY] [--force_path_paste] [-t TOOL_ID] [--tool-version TOOL_VERSION]
|
|
680 [-i TEST_INDEX] [-o OUTPUT] [--append] [-j OUTPUT_JSON] [--verbose] [-c CLIENT_TEST_CONFIG]
|
|
681 galaxy-tool-test -u http://localhost:8080 -a 3c9afe09f1b7892449d266109639c104 -o /tmp/foo -t hello -j /tmp/foo/hello.json --verbose
|
|
682 handy - just leaves outputs in -o
|
|
683 """
|
|
684 if os.path.exists(self.tlog):
|
|
685 tout = open(self.tlog, "a")
|
|
686 else:
|
|
687 tout = open(self.tlog, "w")
|
|
688 testouts = tempfile.mkdtemp(suffix=None, prefix="tftemp")
|
|
689 dummy, tfile = tempfile.mkstemp()
|
|
690 cll = [
|
|
691 os.path.join(self.args.tool_dir,"galaxy-tool-test"),
|
|
692 "-u",
|
|
693 self.args.galaxy_url,
|
|
694 "-k",
|
|
695 self.args.galaxy_api_key,
|
|
696 "-t",
|
|
697 self.args.tool_name,
|
|
698 "-o",
|
|
699 testouts,
|
|
700 ]
|
|
701 subp = subprocess.run(
|
|
702 cll, shell=False, stderr=dummy, stdout=dummy
|
|
703 )
|
|
704 outfiles = []
|
|
705 for p in self.outfiles:
|
|
706 oname = p[ONAMEPOS]
|
|
707 outfiles.append(oname)
|
|
708 with os.scandir(testouts) as outs:
|
|
709 for entry in outs:
|
|
710 if not entry.is_file():
|
|
711 continue
|
|
712 dest = os.path.join(self.tooloutdir, entry.name)
|
|
713 src = os.path.join(testouts, entry.name)
|
|
714 shutil.copyfile(src, dest)
|
|
715 dest = os.path.join(self.testdir, entry.name)
|
|
716 src = os.path.join(testouts, entry.name)
|
|
717 shutil.copyfile(src, dest)
|
|
718 dest = os.path.join(self.repdir,f"{entry.name}_sample")
|
|
719 tout.write(f"## found and moved output {entry.name} to {dest}\n")
|
|
720 tout.close()
|
|
721 shutil.rmtree(testouts)
|
|
722 return subp.returncode
|
|
723
|
|
724 def gal_test(self):
|
|
725 """
|
|
726 Uses the built in galaxy tool tester run_test.sh
|
|
727
|
|
728 export GALAXY_TEST_SAVE="./foo" && export GALAXY_TEST_NO_CLEANUP="1" \
|
|
729 && export GALAXY_TEST_TMP_DIR=./foo && sh run_tests.sh --id rgtf2 --report_file tool_tests_tool_conf.html functional.test_toolbox
|
|
730
|
|
731 """
|
|
732 testdir = tempfile.mkdtemp(suffix=None, prefix="tftemp")
|
|
733 tool_test_rep = f"{self.tool_name}_galaxy_test_report_html.html"
|
|
734 if os.path.exists(self.tlog):
|
|
735 tout = open(self.tlog, "a")
|
|
736 else:
|
|
737 tout = open(self.tlog, "w")
|
|
738
|
|
739 ourenv = os.environ
|
|
740 ourenv["GALAXY_TEST_SAVE"] = testdir
|
|
741 ourenv["GALAXY_TEST_NO_CLEANUP"] = "1"
|
|
742 ourenv["GALAXY_TEST_TMP_DIR"] = testdir
|
|
743
|
|
744 cll = [
|
|
745 "sh", f"{self.args.galaxy_root}/run_tests.sh", "--id", self.args.tool_name,
|
|
746 "--report_file", os.path.join(testdir,tool_test_rep), "functional.test_toolbox",
|
|
747 ]
|
|
748 subp = subprocess.run(
|
|
749 cll, env = ourenv,
|
|
750 shell=False, cwd=self.args.galaxy_root, stderr=tout, stdout=tout
|
|
751 )
|
|
752 src = os.path.join(testdir, tool_test_rep)
|
|
753 if os.path.isfile(src):
|
|
754 dest = os.path.join(self.repdir, tool_test_rep)
|
|
755 shutil.copyfile(src, dest)
|
|
756 else:
|
|
757 tout.write(f"### {src} not found\n")
|
|
758 tout.close()
|
|
759 return subp.returncode
|
|
760
|
|
761
|
63
|
762 def shedLoad(self):
|
48
|
763 """
|
63
|
764 {'deleted': False,
|
|
765 'description': 'Tools for manipulating data',
|
|
766 'id': '175812cd7caaf439',
|
|
767 'model_class': 'Category',
|
|
768 'name': 'Text Manipulation',
|
|
769 'url': '/api/categories/175812cd7caaf439'}]
|
|
770
|
|
771
|
48
|
772 """
|
49
|
773 if os.path.exists(self.tlog):
|
63
|
774 sto = open(self.tlog, "a")
|
48
|
775 else:
|
63
|
776 sto = open(self.tlog, "w")
|
48
|
777
|
75
|
778 ts = toolshed.ToolShedInstance(
|
|
779 url=self.args.toolshed_url, key=self.args.toolshed_api_key, verify=False
|
|
780 )
|
63
|
781 repos = ts.repositories.get_repositories()
|
75
|
782 rnames = [x.get("name", "?") for x in repos]
|
|
783 rids = [x.get("id", "?") for x in repos]
|
80
|
784 sto.write(f"############names={rnames} rids={rids}\n")
|
100
|
785 sto.write(f"############names={repos}\n")
|
98
|
786 tfcat = "ToolFactory generated tools"
|
63
|
787 if self.args.tool_name not in rnames:
|
|
788 tscat = ts.categories.get_categories()
|
98
|
789 cnames = [x.get("name", "?").strip() for x in tscat]
|
75
|
790 cids = [x.get("id", "?") for x in tscat]
|
63
|
791 catID = None
|
98
|
792 if tfcat.strip() in cnames:
|
|
793 ci = cnames.index(tfcat)
|
63
|
794 catID = cids[ci]
|
75
|
795 res = ts.repositories.create_repository(
|
|
796 name=self.args.tool_name,
|
|
797 synopsis="Synopsis:%s" % self.args.tool_desc,
|
|
798 description=self.args.tool_desc,
|
|
799 type="unrestricted",
|
|
800 remote_repository_url=self.args.toolshed_url,
|
|
801 homepage_url=None,
|
|
802 category_ids=catID,
|
|
803 )
|
|
804 tid = res.get("id", None)
|
80
|
805 sto.write(f"##########create res={res}\n")
|
49
|
806 else:
|
75
|
807 i = rnames.index(self.args.tool_name)
|
|
808 tid = rids[i]
|
100
|
809 try:
|
|
810 res = ts.repositories.update_repository(
|
|
811 id=tid, tar_ball_path=self.newtarpath, commit_message=None)
|
|
812 sto.write(f"#####update res={res}\n")
|
|
813 except ConnectionError:
|
|
814 sto.write("Probably no change to repository - bioblend shed upload failed\n")
|
63
|
815 sto.close()
|
|
816
|
48
|
817 def eph_galaxy_load(self):
|
75
|
818 """load the new tool from the local toolshed after planemo uploads it"""
|
49
|
819 if os.path.exists(self.tlog):
|
50
|
820 tout = open(self.tlog, "a")
|
49
|
821 else:
|
50
|
822 tout = open(self.tlog, "w")
|
49
|
823 cll = [
|
|
824 "shed-tools",
|
|
825 "install",
|
|
826 "-g",
|
|
827 self.args.galaxy_url,
|
|
828 "--latest",
|
|
829 "-a",
|
|
830 self.args.galaxy_api_key,
|
|
831 "--name",
|
|
832 self.args.tool_name,
|
|
833 "--owner",
|
|
834 "fubar",
|
|
835 "--toolshed",
|
|
836 self.args.toolshed_url,
|
75
|
837 "--section_label",
|
63
|
838 "ToolFactory",
|
49
|
839 ]
|
63
|
840 tout.write("running\n%s\n" % " ".join(cll))
|
98
|
841 subp = subprocess.run(cll, shell=False, stderr=tout, stdout=tout)
|
75
|
842 tout.write(
|
98
|
843 "installed %s - got retcode %d\n" % (self.args.tool_name, subp.returncode)
|
75
|
844 )
|
63
|
845 tout.close()
|
98
|
846 return subp.returncode
|
63
|
847
|
99
|
848 def planemo_shedLoad(self):
|
63
|
849 """
|
|
850 planemo shed_create --shed_target testtoolshed
|
|
851 planemo shed_init --name=<name>
|
|
852 --owner=<shed_username>
|
|
853 --description=<short description>
|
|
854 [--remote_repository_url=<URL to .shed.yml on github>]
|
|
855 [--homepage_url=<Homepage for tool.>]
|
|
856 [--long_description=<long description>]
|
|
857 [--category=<category name>]*
|
66
|
858
|
|
859
|
63
|
860 planemo shed_update --check_diff --shed_target testtoolshed
|
|
861 """
|
|
862 if os.path.exists(self.tlog):
|
|
863 tout = open(self.tlog, "a")
|
48
|
864 else:
|
63
|
865 tout = open(self.tlog, "w")
|
75
|
866 ts = toolshed.ToolShedInstance(
|
|
867 url=self.args.toolshed_url, key=self.args.toolshed_api_key, verify=False
|
|
868 )
|
63
|
869 repos = ts.repositories.get_repositories()
|
75
|
870 rnames = [x.get("name", "?") for x in repos]
|
|
871 rids = [x.get("id", "?") for x in repos]
|
|
872 #cat = "ToolFactory generated tools"
|
63
|
873 if self.args.tool_name not in rnames:
|
75
|
874 cll = [
|
|
875 "planemo",
|
|
876 "shed_create",
|
|
877 "--shed_target",
|
|
878 "local",
|
|
879 "--owner",
|
|
880 "fubar",
|
|
881 "--name",
|
|
882 self.args.tool_name,
|
|
883 "--shed_key",
|
|
884 self.args.toolshed_api_key,
|
|
885 ]
|
63
|
886 try:
|
98
|
887 subp = subprocess.run(
|
63
|
888 cll, shell=False, cwd=self.tooloutdir, stdout=tout, stderr=tout
|
|
889 )
|
|
890 except:
|
|
891 pass
|
98
|
892 if subp.returncode != 0:
|
80
|
893 tout.write("Repository %s exists\n" % self.args.tool_name)
|
63
|
894 else:
|
80
|
895 tout.write("initiated %s\n" % self.args.tool_name)
|
63
|
896 cll = [
|
|
897 "planemo",
|
|
898 "shed_upload",
|
|
899 "--shed_target",
|
|
900 "local",
|
|
901 "--owner",
|
|
902 "fubar",
|
|
903 "--name",
|
|
904 self.args.tool_name,
|
|
905 "--shed_key",
|
|
906 self.args.toolshed_api_key,
|
|
907 "--tar",
|
|
908 self.newtarpath,
|
|
909 ]
|
98
|
910 subp = subprocess.run(cll, shell=False, stdout=tout, stderr=tout)
|
|
911 tout.write("Ran %s got %d\n" % (" ".join(cll),subp.returncode))
|
49
|
912 tout.close()
|
98
|
913 return subp.returncode
|
48
|
914
|
76
|
915 def eph_test(self, genoutputs=True):
|
|
916 """problem getting jobid - ephemeris upload is the job before the one we want - but depends on how many inputs
|
|
917 """
|
75
|
918 if os.path.exists(self.tlog):
|
|
919 tout = open(self.tlog, "a")
|
|
920 else:
|
|
921 tout = open(self.tlog, "w")
|
|
922 cll = [
|
|
923 "shed-tools",
|
|
924 "test",
|
|
925 "-g",
|
|
926 self.args.galaxy_url,
|
|
927 "-a",
|
|
928 self.args.galaxy_api_key,
|
|
929 "--name",
|
|
930 self.args.tool_name,
|
|
931 "--owner",
|
|
932 "fubar",
|
|
933 ]
|
76
|
934 if genoutputs:
|
|
935 dummy, tfile = tempfile.mkstemp()
|
98
|
936 subp = subprocess.run(
|
76
|
937 cll, shell=False, stderr=dummy, stdout=dummy
|
|
938 )
|
|
939
|
|
940 with open('tool_test_output.json','rb') as f:
|
|
941 s = json.loads(f.read())
|
|
942 print('read %s' % s)
|
|
943 cl = s['tests'][0]['data']['job']['command_line'].split()
|
|
944 n = cl.index('--script_path')
|
|
945 jobdir = cl[n+1]
|
|
946 jobdir = jobdir.replace('"','')
|
|
947 jobdir = jobdir.split('/configs')[0]
|
|
948 print('jobdir=%s' % jobdir)
|
|
949
|
|
950 #"/home/ross/galthrow/database/jobs_directory/000/649/configs/tmptfxu51gs\"
|
|
951 src = os.path.join(jobdir,'working',self.newtarpath)
|
|
952 if os.path.exists(src):
|
|
953 dest = os.path.join(self.testdir, self.newtarpath)
|
|
954 shutil.copyfile(src, dest)
|
|
955 else:
|
|
956 tout.write('No toolshed archive found after first ephemeris test - not a good sign')
|
|
957 ephouts = os.path.join(jobdir,'working','tfout','test-data')
|
|
958 with os.scandir(ephouts) as outs:
|
|
959 for entry in outs:
|
|
960 if not entry.is_file():
|
|
961 continue
|
|
962 dest = os.path.join(self.tooloutdir, entry.name)
|
|
963 src = os.path.join(ephouts, entry.name)
|
|
964 shutil.copyfile(src, dest)
|
|
965 else:
|
98
|
966 subp = subprocess.run(
|
76
|
967 cll, shell=False, stderr=tout, stdout=tout)
|
98
|
968 tout.write("eph_test Ran %s got %d" % (" ".join(cll), subp.returncode))
|
75
|
969 tout.close()
|
98
|
970 return subp.returncode
|
63
|
971
|
83
|
972 def planemo_test_biocontainer(self, genoutputs=True):
|
|
973 """planemo is a requirement so is available for testing but testing in a biocontainer
|
|
974 requires some fiddling to use the hacked galaxy-central .venv
|
|
975
|
|
976 Planemo runs:
|
|
977 python ./scripts/functional_tests.py -v --with-nosehtml --html-report-file
|
|
978 /export/galaxy-central/database/job_working_directory/000/17/working/TF_run_report_tempdir/tacrev_planemo_test_report.html
|
|
979 --with-xunit --xunit-file /tmp/tmpt90p7f9h/xunit.xml --with-structureddata
|
|
980 --structured-data-file
|
|
981 /export/galaxy-central/database/job_working_directory/000/17/working/tfout/tool_test_output.json functional.test_toolbox
|
|
982
|
|
983
|
|
984 for the planemo-biocontainer,
|
|
985 planemo test --conda_dependency_resolution --skip_venv --galaxy_root /galthrow/ rgToolFactory2.xml
|
|
986
|
|
987 """
|
|
988 xreal = "%s.xml" % self.tool_name
|
|
989 tool_test_path = os.path.join(self.repdir,f"{self.tool_name}_planemo_test_report.html")
|
|
990 if os.path.exists(self.tlog):
|
|
991 tout = open(self.tlog, "a")
|
|
992 else:
|
|
993 tout = open(self.tlog, "w")
|
|
994 if genoutputs:
|
|
995 dummy, tfile = tempfile.mkstemp()
|
|
996 cll = [
|
95
|
997 ".", os.path.join(self.args.galaxy_root,'.venv','bin','activate'),"&&",
|
83
|
998 "planemo",
|
|
999 "test",
|
98
|
1000 "--test_data", self.testdir,
|
|
1001 "--test_output", tool_test_path,
|
83
|
1002 "--skip_venv",
|
|
1003 "--galaxy_root",
|
91
|
1004 self.args.galaxy_root,
|
83
|
1005 "--update_test_data",
|
98
|
1006 xreal,
|
83
|
1007 ]
|
98
|
1008 subp = subprocess.run(
|
83
|
1009 cll,
|
|
1010 shell=False,
|
|
1011 cwd=self.tooloutdir,
|
|
1012 stderr=dummy,
|
|
1013 stdout=dummy,
|
|
1014 )
|
|
1015
|
|
1016 else:
|
|
1017 cll = [
|
95
|
1018 ".", os.path.join(self.args.galaxy_root,'.venv','bin','activate'),"&&",
|
83
|
1019 "planemo",
|
|
1020 "test",
|
98
|
1021 "--test_data", os.path.self.testdir,
|
|
1022 "--test_output", os.path.tool_test_path,
|
83
|
1023 "--skip_venv",
|
|
1024 "--galaxy_root",
|
91
|
1025 self.args.galaxy_root,
|
98
|
1026 xreal,
|
83
|
1027 ]
|
98
|
1028 subp = subprocess.run(
|
83
|
1029 cll, shell=False, cwd=self.tooloutdir, stderr=tout, stdout=tout
|
|
1030 )
|
|
1031 tout.close()
|
98
|
1032 return subp.returncode
|
83
|
1033
|
63
|
1034 def planemo_test(self, genoutputs=True):
|
83
|
1035 """planemo is a requirement so is available for testing but needs a different call if
|
|
1036 in the biocontainer - see above
|
63
|
1037 and for generating test outputs if command or test overrides are supplied
|
|
1038 test outputs are sent to repdir for display
|
81
|
1039 planemo test --engine docker_galaxy --galaxy_root /galaxy-central pyrevpos/pyrevpos.xml
|
|
1040
|
|
1041 Planemo runs:
|
|
1042 python ./scripts/functional_tests.py -v --with-nosehtml --html-report-file
|
|
1043 /export/galaxy-central/database/job_working_directory/000/17/working/TF_run_report_tempdir/tacrev_planemo_test_report.html
|
|
1044 --with-xunit --xunit-file /tmp/tmpt90p7f9h/xunit.xml --with-structureddata
|
|
1045 --structured-data-file
|
|
1046 /export/galaxy-central/database/job_working_directory/000/17/working/tfout/tool_test_output.json functional.test_toolbox
|
|
1047
|
|
1048
|
|
1049 for the planemo-biocontainer,
|
|
1050 planemo test --conda_dependency_resolution --skip_venv --galaxy_root /galthrow/ rgToolFactory2.xml
|
|
1051
|
63
|
1052 """
|
|
1053 xreal = "%s.xml" % self.tool_name
|
78
|
1054 tool_test_path = os.path.join(self.repdir,f"{self.tool_name}_planemo_test_report.html")
|
63
|
1055 if os.path.exists(self.tlog):
|
|
1056 tout = open(self.tlog, "a")
|
|
1057 else:
|
|
1058 tout = open(self.tlog, "w")
|
|
1059 if genoutputs:
|
75
|
1060 dummy, tfile = tempfile.mkstemp()
|
63
|
1061 cll = [
|
|
1062 "planemo",
|
|
1063 "test",
|
|
1064 "--galaxy_root",
|
|
1065 self.args.galaxy_root,
|
74
|
1066 "--update_test_data",
|
98
|
1067 xreal,
|
63
|
1068 ]
|
98
|
1069 subp = subprocess.run(
|
75
|
1070 cll,
|
|
1071 shell=False,
|
96
|
1072 cwd=self.testdir,
|
80
|
1073 stderr=dummy,
|
|
1074 stdout=dummy,
|
75
|
1075 )
|
80
|
1076
|
63
|
1077 else:
|
75
|
1078 cll = [
|
|
1079 "planemo",
|
|
1080 "test",
|
98
|
1081 "--test_data", self.testdir,
|
|
1082 "--test_output",tool_test_path,
|
75
|
1083 "--galaxy_root",
|
74
|
1084 self.args.galaxy_root,
|
98
|
1085 xreal,
|
75
|
1086 ]
|
98
|
1087 subp = subprocess.run(
|
96
|
1088 cll, shell=False, cwd=self.testdir, stderr=tout, stdout=tout
|
75
|
1089 )
|
63
|
1090 tout.close()
|
98
|
1091 return subp.returncode
|
63
|
1092
|
83
|
1093
|
48
|
1094 def writeShedyml(self):
|
75
|
1095 """for planemo"""
|
49
|
1096 yuser = self.args.user_email.split("@")[0]
|
|
1097 yfname = os.path.join(self.tooloutdir, ".shed.yml")
|
|
1098 yamlf = open(yfname, "w")
|
|
1099 odict = {
|
|
1100 "name": self.tool_name,
|
|
1101 "owner": yuser,
|
|
1102 "type": "unrestricted",
|
|
1103 "description": self.args.tool_desc,
|
50
|
1104 "synopsis": self.args.tool_desc,
|
|
1105 "category": "TF Generated Tools",
|
49
|
1106 }
|
48
|
1107 yaml.dump(odict, yamlf, allow_unicode=True)
|
|
1108 yamlf.close()
|
|
1109
|
50
|
1110 def makeTool(self):
|
75
|
1111 """write xmls and input samples into place"""
|
50
|
1112 self.makeXML()
|
|
1113 if self.args.script_path:
|
|
1114 stname = os.path.join(self.tooloutdir, "%s" % (self.sfile))
|
|
1115 if not os.path.exists(stname):
|
|
1116 shutil.copyfile(self.sfile, stname)
|
|
1117 xreal = "%s.xml" % self.tool_name
|
|
1118 xout = os.path.join(self.tooloutdir, xreal)
|
|
1119 shutil.copyfile(xreal, xout)
|
|
1120 for p in self.infiles:
|
|
1121 pth = p[IPATHPOS]
|
|
1122 dest = os.path.join(self.testdir, "%s_sample" % p[ICLPOS])
|
|
1123 shutil.copyfile(pth, dest)
|
49
|
1124
|
50
|
1125 def makeToolTar(self):
|
75
|
1126 """move outputs into test-data and prepare the tarball"""
|
66
|
1127 excludeme = "tool_test_output"
|
75
|
1128
|
66
|
1129 def exclude_function(tarinfo):
|
75
|
1130 filename = tarinfo.name
|
|
1131 return (
|
|
1132 None
|
80
|
1133 if filename.startswith(excludeme)
|
75
|
1134 else tarinfo
|
|
1135 )
|
66
|
1136
|
50
|
1137 for p in self.outfiles:
|
96
|
1138 oname = p[ONAMEPOS]
|
99
|
1139 tdest = os.path.join(self.testdir, "%s_sample" % oname)
|
|
1140 if not os.path.isfile(tdest):
|
|
1141 src = os.path.join(self.testdir,oname)
|
|
1142 if os.path.isfile(src):
|
|
1143 shutil.copyfile(src, tdest)
|
|
1144 dest = os.path.join(self.repdir, "%s.sample" % (oname))
|
|
1145 shutil.copyfile(src, dest)
|
|
1146 else:
|
|
1147 print(
|
|
1148 "### problem - output file %s not found in testdir %s"
|
|
1149 % (tdest, self.testdir)
|
|
1150 )
|
50
|
1151 tf = tarfile.open(self.newtarpath, "w:gz")
|
66
|
1152 tf.add(name=self.tooloutdir, arcname=self.tool_name, filter=exclude_function)
|
50
|
1153 tf.close()
|
|
1154 shutil.copyfile(self.newtarpath, self.args.new_tool)
|
|
1155
|
|
1156 def moveRunOutputs(self):
|
75
|
1157 """need to move planemo or run outputs into toolfactory collection"""
|
50
|
1158 with os.scandir(self.tooloutdir) as outs:
|
|
1159 for entry in outs:
|
80
|
1160 if not entry.is_file():
|
|
1161 continue
|
|
1162 if "." in entry.name:
|
|
1163 nayme, ext = os.path.splitext(entry.name)
|
|
1164 if ext in ['.yml','.xml','.json','.yaml']:
|
|
1165 ext = f'{ext}.txt'
|
|
1166 else:
|
|
1167 ext = ".txt"
|
|
1168 ofn = "%s%s" % (entry.name.replace(".", "_"), ext)
|
|
1169 dest = os.path.join(self.repdir, ofn)
|
|
1170 src = os.path.join(self.tooloutdir, entry.name)
|
|
1171 shutil.copyfile(src, dest)
|
|
1172 with os.scandir(self.testdir) as outs:
|
|
1173 for entry in outs:
|
|
1174 if not entry.is_file():
|
50
|
1175 continue
|
|
1176 if "." in entry.name:
|
|
1177 nayme, ext = os.path.splitext(entry.name)
|
|
1178 else:
|
|
1179 ext = ".txt"
|
80
|
1180 newname = f"{entry.name}{ext}"
|
|
1181 dest = os.path.join(self.repdir, newname)
|
|
1182 src = os.path.join(self.testdir, entry.name)
|
50
|
1183 shutil.copyfile(src, dest)
|
|
1184
|
49
|
1185
|
76
|
1186
|
48
|
1187 def main():
|
|
1188 """
|
|
1189 This is a Galaxy wrapper. It expects to be called by a special purpose tool.xml as:
|
49
|
1190 <command interpreter="python">rgBaseScriptWrapper.py --script_path "$scriptPath"
|
|
1191 --tool_name "foo" --interpreter "Rscript"
|
48
|
1192 </command>
|
|
1193 """
|
|
1194 parser = argparse.ArgumentParser()
|
|
1195 a = parser.add_argument
|
49
|
1196 a("--script_path", default=None)
|
|
1197 a("--history_test", default=None)
|
|
1198 a("--cl_prefix", default=None)
|
|
1199 a("--sysexe", default=None)
|
|
1200 a("--packages", default=None)
|
76
|
1201 a("--tool_name", default="newtool")
|
72
|
1202 a("--tool_dir", default=None)
|
48
|
1203 a("--input_files", default=[], action="append")
|
|
1204 a("--output_files", default=[], action="append")
|
|
1205 a("--user_email", default="Unknown")
|
|
1206 a("--bad_user", default=None)
|
49
|
1207 a("--make_Tool", default="runonly")
|
48
|
1208 a("--help_text", default=None)
|
|
1209 a("--tool_desc", default=None)
|
|
1210 a("--tool_version", default=None)
|
|
1211 a("--citations", default=None)
|
49
|
1212 a("--command_override", default=None)
|
|
1213 a("--test_override", default=None)
|
48
|
1214 a("--additional_parameters", action="append", default=[])
|
|
1215 a("--edit_additional_parameters", action="store_true", default=False)
|
|
1216 a("--parampass", default="positional")
|
|
1217 a("--tfout", default="./tfout")
|
|
1218 a("--new_tool", default="new_tool")
|
49
|
1219 a("--galaxy_url", default="http://localhost:8080")
|
75
|
1220 a(
|
76
|
1221 "--toolshed_url", default="http://localhost:9009")
|
|
1222 # make sure this is identical to tool_sheds_conf.xml localhost != 127.0.0.1 so validation fails
|
63
|
1223 a("--toolshed_api_key", default="fakekey")
|
50
|
1224 a("--galaxy_api_key", default="fakekey")
|
|
1225 a("--galaxy_root", default="/galaxy-central")
|
48
|
1226 args = parser.parse_args()
|
|
1227 assert not args.bad_user, (
|
|
1228 'UNAUTHORISED: %s is NOT authorized to use this tool until Galaxy admin adds %s to "admin_users" in the Galaxy configuration file'
|
|
1229 % (args.bad_user, args.bad_user)
|
|
1230 )
|
49
|
1231 assert args.tool_name, "## Tool Factory expects a tool name - eg --tool_name=DESeq"
|
48
|
1232 assert (
|
49
|
1233 args.sysexe or args.packages
|
48
|
1234 ), "## Tool Factory wrapper expects an interpreter or an executable package"
|
49
|
1235 args.input_files = [x.replace('"', "").replace("'", "") for x in args.input_files]
|
48
|
1236 # remove quotes we need to deal with spaces in CL params
|
|
1237 for i, x in enumerate(args.additional_parameters):
|
49
|
1238 args.additional_parameters[i] = args.additional_parameters[i].replace('"', "")
|
48
|
1239 r = ScriptRunner(args)
|
49
|
1240 r.writeShedyml()
|
|
1241 r.makeTool()
|
66
|
1242 if args.make_Tool == "generate":
|
|
1243 retcode = r.run()
|
|
1244 r.moveRunOutputs()
|
|
1245 r.makeToolTar()
|
|
1246 else:
|
99
|
1247 r.makeToolTar()
|
100
|
1248 r.planemo_shedLoad()
|
97
|
1249 r.shedLoad()
|
|
1250 r.eph_galaxy_load()
|
99
|
1251 retcode = r.gal_tool_test() # writes outputs
|
97
|
1252 r.makeToolTar()
|
100
|
1253 r.planemo_shedLoad()
|
99
|
1254 r.shedLoad()
|
|
1255 r.eph_galaxy_load()
|
|
1256 retcode = r.gal_test()
|
66
|
1257 r.moveRunOutputs()
|
|
1258 r.makeToolTar()
|
97
|
1259 print(f"second galaxy_test returned {retcode}")
|
63
|
1260
|
48
|
1261
|
|
1262 if __name__ == "__main__":
|
|
1263 main()
|