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