annotate toolfactory/rgToolFactory2.py @ 31:69eed330c91f draft

Uploaded
author fubar
date Fri, 07 Aug 2020 07:55:35 -0400
parents 6f48315c32c1
children 4d578c8c1613
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
1 # rgToolFactory.py
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
2 # see https://github.com/fubar2/toolfactory
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
3 #
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
4 # copyright ross lazarus (ross stop lazarus at gmail stop com) May 2012
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
5 #
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
6 # all rights reserved
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
7 # Licensed under the LGPL
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
8 # suggestions for improvement and bug fixes welcome at https://github.com/fubar2/toolfactory
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
9 #
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
10 # July 2020: BCC was fun and I feel like rip van winkle after 5 years.
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
11 # Decided to
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
12 # 1. Fix the toolfactory so it works - done for simplest case
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
13 # 2. Fix planemo so the toolfactory function works
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
14 # 3. Rewrite bits using galaxyxml functions where that makes sense - done
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
15 #
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
16 # removed all the old complications including making the new tool use this same script
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
17 # galaxyxml now generates the tool xml https://github.com/hexylena/galaxyxml
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
18 # No support for automatic HTML file creation from arbitrary outputs
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
19 # TODO: add option to run that code as a post execution hook
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
20 # TODO: add additional history input parameters - currently only one
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
21
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
22 import sys
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
23 import subprocess
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
24 import shutil
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
25 import os
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
26 import time
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
27 import tempfile
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
28 import argparse
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
29 import tarfile
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
30 import re
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
31 import galaxyxml.tool as gxt
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
32 import galaxyxml.tool.parameters as gxtp
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
33 import logging
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
34
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
35
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
36 progname = os.path.split(sys.argv[0])[1]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
37 myversion = 'V2.1 July 2020'
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
38 verbose = True
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
39 debug = True
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
40 toolFactoryURL = 'https://github.com/fubar2/toolfactory'
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
41 ourdelim = '~~~'
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
42
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
43 # --input_files="$input_files~~~$CL~~~$input_formats~~~$input_label~~~$input_help"
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
44 IPATHPOS = 0
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
45 ICLPOS = 1
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
46 IFMTPOS = 2
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
47 ILABPOS = 3
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
48 IHELPOS = 4
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
49 IOCLPOS = 5
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
50 # --output_files "$otab.history_name~~~$otab.history_format~~~$otab.CL
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
51 ONAMEPOS = 0
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
52 OFMTPOS = 1
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
53 OCLPOS = 2
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
54 OOCLPOS = 3
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
55
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
56 #--additional_parameters="$i.param_name~~~$i.param_value~~~$i.param_label~~~$i.param_help~~~$i.param_type~~~$i.CL"
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
57 ANAMEPOS = 0
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
58 AVALPOS = 1
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
59 ALABPOS = 2
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
60 AHELPPOS = 3
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
61 ATYPEPOS = 4
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
62 ACLPOS = 5
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
63 AOCLPOS = 6
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
64
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
65 def timenow():
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
66 """return current time as a string
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
67 """
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
68 return time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(time.time()))
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
69
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
70
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
71 def quote_non_numeric(s):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
72 """return a prequoted string for non-numerics
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
73 useful for perl and Rscript parameter passing?
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
74 """
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
75 try:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
76 _ = float(s)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
77 return s
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
78 except ValueError:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
79 return '"%s"' % s
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
80
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
81
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
82 html_escape_table = {
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
83 "&": "&",
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
84 ">": ">",
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
85 "<": "&lt;",
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
86 "$": r"\$"
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
87 }
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
88
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
89
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
90 def html_escape(text):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
91 """Produce entities within text."""
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
92 return "".join(html_escape_table.get(c, c) for c in text)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
93
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
94
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
95 def html_unescape(text):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
96 """Revert entities within text. Multiple character targets so use replace"""
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
97 t = text.replace('&amp;', '&')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
98 t = t.replace('&gt;', '>')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
99 t = t.replace('&lt;', '<')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
100 t = t.replace('\\$', '$')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
101 return t
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
102
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
103
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
104 def parse_citations(citations_text):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
105 """
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
106 """
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
107 citations = [c for c in citations_text.split("**ENTRY**") if c.strip()]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
108 citation_tuples = []
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
109 for citation in citations:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
110 if citation.startswith("doi"):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
111 citation_tuples.append(("doi", citation[len("doi"):].strip()))
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
112 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
113 citation_tuples.append(
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
114 ("bibtex", citation[len("bibtex"):].strip()))
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
115 return citation_tuples
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
116
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
117
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
118 class ScriptRunner:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
119 """Wrapper for an arbitrary script
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
120 uses galaxyxml
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
121
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
122 """
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
123
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
124
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
125 def __init__(self, args=None):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
126 """
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
127 prepare command line cl for running the tool here
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
128 and prepare elements needed for galaxyxml tool generation
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
129 """
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
130
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
131 self.infiles = [x.split(ourdelim) for x in args.input_files]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
132 self.outfiles = [x.split(ourdelim) for x in args.output_files]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
133 self.addpar = [x.split(ourdelim) for x in args.additional_parameters]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
134 self.args = args
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
135 self.cleanuppar()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
136 self.lastclredirect = None
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
137 self.lastxclredirect = None
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
138 self.cl = []
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
139 self.xmlcl = []
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
140 aCL = self.cl.append
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
141 assert args.parampass in ['0','argparse','positional'],'Parameter passing in args.parampass must be "0","positional" or "argparse"'
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
142 self.tool_name = re.sub('[^a-zA-Z0-9_]+', '', args.tool_name)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
143 self.tool_id = self.tool_name
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
144 self.xmlfile = '%s.xml' % self.tool_name
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
145 if self.args.runmode == "Executable" or self.args.runmode == "system": # binary - no need
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
146 aCL(self.args.exe_package) # this little CL will just run
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
147 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
148 rx = open(self.args.script_path, 'r').readlines()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
149 rx = [x.rstrip() for x in rx ]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
150 rxcheck = [x.strip() for x in rx if x.strip() > '']
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
151 assert len(rxcheck) > 0,"Supplied script is empty. Cannot run"
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
152 self.script = '\n'.join(rx)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
153 fhandle, self.sfile = tempfile.mkstemp(
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
154 prefix=self.tool_name, suffix=".%s" % (args.interpreter_name))
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
155 tscript = open(self.sfile, 'w')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
156 tscript.write(self.script)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
157 tscript.close()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
158 self.indentedScript = " %s" % '\n'.join(
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
159 [' %s' % html_escape(x) for x in rx])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
160 self.escapedScript = "%s" % '\n'.join(
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
161 [' %s' % html_escape(x) for x in rx])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
162 art = '%s.%s' % (self.tool_name, args.interpreter_name)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
163 artifact = open(art, 'wb')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
164 artifact.write(bytes(self.script, "utf8"))
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
165 artifact.close()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
166 aCL(self.args.interpreter_name)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
167 aCL(self.sfile)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
168 self.elog = "%s_error_log.txt" % self.tool_name
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
169 self.tlog = "%s_runner_log.txt" % self.tool_name
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
170
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
171 if self.args.parampass == '0':
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
172 self.clsimple()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
173 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
174 clsuffix = []
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
175 xclsuffix = []
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
176 for i, p in enumerate(self.infiles):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
177 appendme = [p[IOCLPOS], p[ICLPOS], p[IPATHPOS]]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
178 clsuffix.append(appendme)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
179 xclsuffix.append([p[IOCLPOS],p[ICLPOS],'$%s' % p[ICLPOS]])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
180 #print('##infile i=%d, appendme=%s' % (i,appendme))
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
181 for i, p in enumerate(self.outfiles):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
182 if p[OOCLPOS] == "STDOUT":
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
183 self.lastclredirect = ['>',p[ONAMEPOS]]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
184 self.lastxclredirect = ['>','$%s' % p[OCLPOS]]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
185 #print('##outfiles i=%d lastclredirect = %s' % (i,self.lastclredirect))
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
186 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
187 appendme = [p[OOCLPOS], p[OCLPOS],p[ONAMEPOS]]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
188 clsuffix.append(appendme)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
189 xclsuffix.append([p[OOCLPOS], p[OCLPOS],'$%s' % p[ONAMEPOS]])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
190 #print('##outfiles i=%d' % i,'appendme',appendme)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
191 for p in self.addpar:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
192 appendme = [p[AOCLPOS], p[ACLPOS], p[AVALPOS]]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
193 clsuffix.append(appendme)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
194 xclsuffix.append([p[AOCLPOS], p[ACLPOS], '"$%s"' % p[ANAMEPOS]])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
195 #print('##adpar %d' % i,'appendme=',appendme)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
196 clsuffix.sort()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
197 xclsuffix.sort()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
198 self.xclsuffix = xclsuffix
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
199 self.clsuffix = clsuffix
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
200 if self.args.parampass == 'positional':
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
201 self.clpositional()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
202 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
203 self.clargparse()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
204
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
205 def cleanuppar(self):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
206 """ positional parameters are complicated by their numeric ordinal"""
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
207 for i,p in enumerate(self.infiles):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
208 if self.args.parampass == 'positional':
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
209 assert p[ICLPOS].isdigit(), "Positional parameters must be ordinal integers - got %s for %s" % (p[ICLPOS],p[ILABPOS])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
210 p.append(p[ICLPOS])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
211 if p[ICLPOS].isdigit() or self.args.parampass == "0":
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
212 scl = 'input%d' % (i+1)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
213 p[ICLPOS] = scl
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
214 self.infiles[i] = p
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
215 for i,p in enumerate(self.outfiles): # trying to automagically gather using extensions
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
216 if self.args.parampass == 'positional' and p[OCLPOS] != "STDOUT":
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
217 assert p[OCLPOS].isdigit(), "Positional parameters must be ordinal integers - got %s for %s" % (p[OCLPOS],p[ONAMEPOS])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
218 p.append(p[OCLPOS])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
219 if p[OCLPOS].isdigit() or p[OCLPOS] == "STDOUT":
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
220 scl = p[ONAMEPOS]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
221 p[OCLPOS] = scl
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
222 self.outfiles[i] = p
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
223 for i,p in enumerate(self.addpar):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
224 if self.args.parampass == 'positional':
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
225 assert p[ACLPOS].isdigit(), "Positional parameters must be ordinal integers - got %s for %s" % (p[ACLPOS],p[ANAMEPOS])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
226 p.append(p[ACLPOS])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
227 if p[ACLPOS].isdigit():
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
228 scl = 'input%s' % p[ACLPOS]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
229 p[ACLPOS] = scl
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
230 self.addpar[i] = p
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
231
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
232
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
233
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
234 def clsimple(self):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
235 """ no parameters - uses < and > for i/o
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
236 """
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
237 aCL = self.cl.append
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
238 aCL('<')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
239 aCL(self.infiles[0][IPATHPOS])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
240 aCL('>')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
241 aCL(self.outfiles[0][OCLPOS])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
242 aXCL = self.xmlcl.append
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
243 aXCL('<')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
244 aXCL('$%s' % self.infiles[0][ICLPOS])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
245 aXCL('>')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
246 aXCL('$%s' % self.outfiles[0][ONAMEPOS])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
247
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
248
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
249 def clpositional(self):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
250 # inputs in order then params
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
251 aCL = self.cl.append
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
252 for (o_v,k, v) in self.clsuffix:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
253 if " " in v:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
254 aCL("%s" % v)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
255 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
256 aCL(v)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
257 aXCL = self.xmlcl.append
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
258 for (o_v,k, v) in self.xclsuffix:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
259 aXCL(v)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
260 if self.lastxclredirect:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
261 aXCL(self.lastxclredirect[0])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
262 aXCL(self.lastxclredirect[1])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
263
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
264
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
265
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
266 def clargparse(self):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
267 """ argparse style
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
268 """
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
269 aCL = self.cl.append
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
270 aXCL = self.xmlcl.append
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
271 # inputs then params in argparse named form
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
272 for (o_v,k, v) in self.xclsuffix:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
273 aXCL(k)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
274 aXCL(v)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
275 for (o_v,k, v) in self.clsuffix:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
276 if len(k.strip()) == 1:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
277 k = '-%s' % k
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
278 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
279 k = '--%s' % k
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
280 aCL(k)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
281 aCL(v)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
282
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
283
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
284
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
285
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
286 def makeXML(self):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
287 """
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
288 Create a Galaxy xml tool wrapper for the new script
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
289 Uses galaxyhtml
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
290 Hmmm. How to get the command line into correct order...
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
291 """
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
292
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
293 if self.args.interpreter_name:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
294 exe = "$runMe"
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
295 interp = self.args.interpreter_name
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
296 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
297 interp = None
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
298 exe = self.args.exe_package
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
299 assert exe is not None, 'No interpeter or executable passed in to makeXML'
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
300 tool = gxt.Tool(self.args.tool_name, self.tool_id,
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
301 self.args.tool_version, self.args.tool_desc, exe)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
302 tool.command_line_override = self.xmlcl
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
303 print('#### tool cl override=',self.xmlcl)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
304 if interp:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
305 tool.interpreter = interp
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
306 if self.args.help_text:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
307 helptext = open(self.args.help_text, 'r').readlines()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
308 helptext = [html_escape(x) for x in helptext]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
309 tool.help = ''.join([x for x in helptext])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
310 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
311 tool.help = 'Please ask the tool author (%s) for help \
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
312 as none was supplied at tool generation\n' % (self.args.user_email)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
313 tool.version_command = None # do not want
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
314 tinputs = gxtp.Inputs()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
315 toutputs = gxtp.Outputs()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
316 requirements = gxtp.Requirements()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
317 testparam = []
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
318 is_positional = (self.args.parampass == 'positional')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
319 if self.args.interpreter_name:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
320 if self.args.interpreter_name == 'python':
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
321 requirements.append(gxtp.Requirement(
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
322 'package', 'python', self.args.interpreter_version))
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
323 elif self.args.interpreter_name not in ['bash', 'sh']:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
324 requirements.append(gxtp.Requirement(
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
325 'package', self.args.interpreter_name, self.args.interpreter_version))
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
326 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
327 if self.args.exe_package and self.args.parampass != "system":
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
328 requirements.append(gxtp.Requirement(
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
329 'package', self.args.exe_package, self.args.exe_package_version))
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
330 tool.requirements = requirements
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
331 if self.args.parampass == '0':
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
332 alab = self.infiles[0][ILABPOS]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
333 if len(alab) == 0:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
334 alab = self.infiles[0][ICLPOS]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
335 max1s = 'Maximum one input if parampass is 0 - more than one input files supplied - %s' % str(self.infiles)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
336 assert len(self.infiles) == 1,max1s
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
337 newname = self.infiles[0][ICLPOS]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
338 aninput = gxtp.DataParam(newname, optional=False, label=alab, help=self.infiles[0][IHELPOS],
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
339 format=self.infiles[0][IFMTPOS], multiple=False, num_dashes=0)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
340 aninput.command_line_override = '< $%s' % newname
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
341 aninput.positional = is_positional
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
342 tinputs.append(aninput)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
343 tp = gxtp.TestParam(name=newname, value='%s_sample' % newname)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
344 testparam.append(tp)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
345 newname = self.outfiles[0][OCLPOS]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
346 newfmt = self.outfiles[0][OFMTPOS]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
347 anout = gxtp.OutputData(newname, format=newfmt, num_dashes=0)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
348 anout.command_line_override = '> $%s' % newname
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
349 anout.positional = is_positional
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
350 toutputs.append(anout)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
351 tp = gxtp.TestOutput(name=newname, value='%s_sample' % newname,format=newfmt)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
352 testparam.append(tp)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
353 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
354 for p in self.outfiles:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
355 newname,newfmt,newcl,oldcl = p
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
356 if is_positional:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
357 ndash = 0
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
358 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
359 ndash = 2
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
360 if len(newcl) < 2:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
361 ndash = 1
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
362 aparm = gxtp.OutputData(newcl, format=newfmt, num_dashes=ndash)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
363 aparm.positional = is_positional
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
364 if is_positional:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
365 if oldcl == "STDOUT":
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
366 aparm.positional = 9999999
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
367 aparm.command_line_override = "> $%s" % newcl
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
368 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
369 aparm.positional = int(oldcl)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
370 aparm.command_line_override = '$%s' % newcl
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
371 toutputs.append(aparm)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
372 tp = gxtp.TestOutput(name=newcl, value='%s_sample' % newcl ,format=newfmt)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
373 testparam.append(tp)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
374 for p in self.infiles:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
375 newname = p[ICLPOS]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
376 newfmt = p[IFMTPOS]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
377 if is_positional:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
378 ndash = 0
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
379 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
380 if len(newname) > 1:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
381 ndash = 2
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
382 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
383 ndash = 1
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
384 if not len(p[ILABPOS]) > 0:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
385 alab = p[ICLPOS]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
386 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
387 alab = p[ILABPOS]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
388 aninput = gxtp.DataParam(newname, optional=False, label=alab, help=p[IHELPOS],
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
389 format=newfmt, multiple=False, num_dashes=ndash)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
390 aninput.positional = is_positional
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
391 if is_positional:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
392 aninput.positional = is_positional
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
393 tinputs.append(aninput)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
394 tparm = gxtp.TestParam(name=newname, value='%s_sample' % newname )
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
395 testparam.append(tparm)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
396 for p in self.addpar:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
397 newname, newval, newlabel, newhelp, newtype, newcl, oldcl = p
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
398 if not len(newlabel) > 0:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
399 newlabel = newname
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
400 if is_positional:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
401 ndash = 0
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
402 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
403 if len(newname) > 1:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
404 ndash = 2
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
405 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
406 ndash = 1
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
407 if newtype == "text":
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
408 aparm = gxtp.TextParam(
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
409 newname, label=newlabel, help=newhelp, value=newval, num_dashes=ndash)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
410 elif newtype == "integer":
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
411 aparm = gxtp.IntegerParam(
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
412 newname, label=newname, help=newhelp, value=newval, num_dashes=ndash)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
413 elif newtype == "float":
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
414 aparm = gxtp.FloatParam(
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
415 newname, label=newname, help=newhelp, value=newval, num_dashes=ndash)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
416 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
417 raise ValueError('Unrecognised parameter type "%s" for\
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
418 additional parameter %s in makeXML' % (newtype, newname))
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
419 aparm.positional = is_positional
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
420 if is_positional:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
421 aninput.positional = int(oldcl)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
422 tinputs.append(aparm)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
423 tparm = gxtp.TestParam(newname, value=newval)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
424 testparam.append(tparm)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
425 tool.outputs = toutputs
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
426 tool.inputs = tinputs
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
427 if not self.args.runmode in ['Executable','system']:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
428 configfiles = gxtp.Configfiles()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
429 configfiles.append(gxtp.Configfile(name="runMe", text=self.script))
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
430 tool.configfiles = configfiles
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
431 tests = gxtp.Tests()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
432 test_a = gxtp.Test()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
433 for tp in testparam:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
434 test_a.append(tp)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
435 tests.append(test_a)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
436 tool.tests = tests
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
437 tool.add_comment('Created by %s at %s using the Galaxy Tool Factory.' % (
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
438 self.args.user_email, timenow()))
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
439 tool.add_comment('Source in git at: %s' % (toolFactoryURL))
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
440 tool.add_comment(
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
441 'Cite: Creating re-usable tools from scripts doi: 10.1093/bioinformatics/bts573')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
442 exml = tool.export()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
443 xf = open(self.xmlfile, 'w')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
444 xf.write(exml)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
445 xf.write('\n')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
446 xf.close()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
447 # ready for the tarball
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
448
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
449 def makeTooltar(self):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
450 """
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
451 a tool is a gz tarball with eg
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
452 /toolname/tool.xml /toolname/tool.py /toolname/test-data/test1_in.foo ...
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
453 NOTE names for test inputs and outputs are munged here so must
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
454 correspond to actual input and output names used on the generated cl
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
455 """
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
456 retval = self.run()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
457 if retval:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
458 sys.stderr.write(
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
459 '## Run failed. Cannot build yet. Please fix and retry')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
460 sys.exit(1)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
461 tdir = 'tfout'
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
462 if not os.path.exists(tdir):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
463 os.mkdir(tdir)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
464 self.makeXML()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
465 testdir = os.path.join(tdir,'test-data')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
466 if not os.path.exists(testdir):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
467 os.mkdir(testdir) # make tests directory
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
468 for p in self.infiles:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
469 pth = p[IPATHPOS]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
470 dest = os.path.join(testdir, '%s_sample' %
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
471 p[ICLPOS])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
472 shutil.copyfile(pth, dest)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
473 for p in self.outfiles:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
474 pth = p[OCLPOS]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
475 if p[OOCLPOS] == 'STDOUT' or self.args.parampass == "0":
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
476 pth = p[ONAMEPOS]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
477 dest = os.path.join(testdir,'%s_sample' % p[ONAMEPOS])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
478 shutil.copyfile(pth, dest)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
479 dest = os.path.join(tdir, p[ONAMEPOS])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
480 shutil.copyfile(pth, dest)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
481 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
482 pth = p[OCLPOS]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
483 dest = os.path.join(testdir,'%s_sample' % p[OCLPOS])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
484 shutil.copyfile(pth, dest)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
485 dest = os.path.join(tdir, p[OCLPOS])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
486 shutil.copyfile(pth, dest)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
487
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
488 if os.path.exists(self.tlog) and os.stat(self.tlog).st_size > 0:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
489 shutil.copyfile(self.tlog, os.path.join(
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
490 testdir, 'test1_log.txt'))
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
491 if not self.args.runmode in ['Executable','system']:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
492 stname = os.path.join(tdir, '%s' % (self.sfile))
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
493 if not os.path.exists(stname):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
494 shutil.copyfile(self.sfile, stname)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
495 xtname = os.path.join(tdir,self.xmlfile)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
496 if not os.path.exists(xtname):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
497 shutil.copyfile(self.xmlfile, xtname)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
498 tarpath = 'toolfactory_%s.tgz' % self.tool_name
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
499 tf = tarfile.open(tarpath,"w:gz")
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
500 tf.add(name=tdir,arcname=self.tool_name)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
501 tf.close()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
502 shutil.copyfile(tarpath, self.args.new_tool)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
503 return retval
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
504
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
505 def run(self):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
506 """
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
507 Some devteam tools have this defensive stderr read so I'm keeping with the faith
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
508 Feel free to update.
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
509 """
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
510 s = 'run cl=%s' % str(self.cl)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
511
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
512 logging.debug(s)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
513 scl = ' '.join(self.cl)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
514 err = None
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
515 if self.args.parampass != '0':
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
516 ste = open(self.elog, 'wb')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
517 if self.lastclredirect:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
518 sto = open(self.lastclredirect[1],'wb') # is name of an output file
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
519 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
520 sto = open(self.tlog, 'wb')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
521 sto.write(
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
522 bytes('## Executing Toolfactory generated command line = %s\n' % scl, "utf8"))
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
523 sto.flush()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
524 p = subprocess.run(self.cl, shell=False, stdout=sto,
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
525 stderr=ste)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
526 sto.close()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
527 ste.close()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
528 tmp_stderr = open(self.elog, 'rb')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
529 err = ''
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
530 buffsize = 1048576
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
531 try:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
532 while True:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
533 err += str(tmp_stderr.read(buffsize))
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
534 if not err or len(err) % buffsize != 0:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
535 break
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
536 except OverflowError:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
537 pass
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
538 tmp_stderr.close()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
539 retval = p.returncode
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
540 else: # work around special case of simple scripts that take stdin and write to stdout
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
541 sti = open(self.infiles[0][IPATHPOS], 'rb')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
542 sto = open(self.outfiles[0][ONAMEPOS], 'wb')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
543 # must use shell to redirect
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
544 p = subprocess.run(self.cl, shell=False, stdout=sto, stdin=sti)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
545 retval = p.returncode
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
546 sto.close()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
547 sti.close()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
548 if os.path.isfile(self.tlog) and os.stat(self.tlog).st_size == 0:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
549 os.unlink(self.tlog)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
550 if os.path.isfile(self.elog) and os.stat(self.elog).st_size == 0:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
551 os.unlink(self.elog)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
552 if p.returncode != 0 and err: # problem
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
553 sys.stderr.write(err)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
554 logging.debug('run done')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
555 return retval
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
556
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
557
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
558 def main():
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
559 """
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
560 This is a Galaxy wrapper. It expects to be called by a special purpose tool.xml as:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
561 <command interpreter="python">rgBaseScriptWrapper.py --script_path "$scriptPath" --tool_name "foo" --interpreter "Rscript"
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
562 </command>
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
563 """
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
564 parser = argparse.ArgumentParser()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
565 a = parser.add_argument
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
566 a('--script_path', default='')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
567 a('--tool_name', default=None)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
568 a('--interpreter_name', default=None)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
569 a('--interpreter_version', default=None)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
570 a('--exe_package', default=None)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
571 a('--exe_package_version', default=None)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
572 a('--input_files', default=[], action="append")
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
573 a('--output_files', default=[], action="append")
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
574 a('--user_email', default='Unknown')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
575 a('--bad_user', default=None)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
576 a('--make_Tool', default=None)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
577 a('--help_text', default=None)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
578 a('--tool_desc', default=None)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
579 a('--tool_version', default=None)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
580 a('--citations', default=None)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
581 a('--additional_parameters', action='append', default=[])
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
582 a('--edit_additional_parameters', action="store_true", default=False)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
583 a('--parampass', default="positional")
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
584 a('--tfout', default="./tfout")
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
585 a('--new_tool',default="new_tool")
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
586 a('--runmode',default=None)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
587 args = parser.parse_args()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
588 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' % (
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
589 args.bad_user, args.bad_user)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
590 assert args.tool_name, '## Tool Factory expects a tool name - eg --tool_name=DESeq'
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
591 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'
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
592 assert args.exe_package or (len(args.script_path) > 0 and os.path.isfile(
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
593 args.script_path)), '## Tool Factory wrapper expects a script path - eg --script_path=foo.R if no executable'
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
594 args.input_files = [x.replace('"', '').replace("'", '')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
595 for x in args.input_files]
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
596 # remove quotes we need to deal with spaces in CL params
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
597 for i, x in enumerate(args.additional_parameters):
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
598 args.additional_parameters[i] = args.additional_parameters[i].replace(
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
599 '"', '')
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
600 r = ScriptRunner(args)
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
601 if args.make_Tool:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
602 retcode = r.makeTooltar()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
603 else:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
604 retcode = r.run()
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
605 if retcode:
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
606 sys.exit(retcode) # indicate failure to job runner
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
607
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
608
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
609 if __name__ == "__main__":
6f48315c32c1 Uploaded
fubar
parents:
diff changeset
610 main()