0
|
1 # rgToolFactory.py
|
|
2 # see https://bitbucket.org/fubar/galaxytoolfactory/wiki/Home
|
|
3 #
|
|
4 # copyright ross lazarus (ross stop lazarus at gmail stop com) May 2012
|
|
5 #
|
|
6 # all rights reserved
|
|
7 # Licensed under the LGPL
|
|
8 # suggestions for improvement and bug fixes welcome at https://bitbucket.org/fubar/galaxytoolfactory/wiki/Home
|
|
9 #
|
|
10 # july 2013
|
|
11 # added ability to combine images and individual log files into html output
|
|
12 # just make sure there's a log file foo.log and it will be output
|
|
13 # together with all images named like "foo_*.pdf
|
|
14 # otherwise old format for html
|
|
15 #
|
|
16 # January 2013
|
|
17 # problem pointed out by Carlos Borroto
|
|
18 # added escaping for <>$ - thought I did that ages ago...
|
|
19 #
|
|
20 # August 11 2012
|
|
21 # changed to use shell=False and cl as a sequence
|
|
22
|
|
23 # This is a Galaxy tool factory for simple scripts in python, R or whatever ails ye.
|
|
24 # It also serves as the wrapper for the new tool.
|
|
25 #
|
|
26 # you paste and run your script
|
|
27 # Only works for simple scripts that read one input from the history.
|
|
28 # Optionally can write one new history dataset,
|
|
29 # and optionally collect any number of outputs into links on an autogenerated HTML page.
|
|
30
|
|
31 # DO NOT install on a public or important site - please.
|
|
32
|
|
33 # installed generated tools are fine if the script is safe.
|
|
34 # They just run normally and their user cannot do anything unusually insecure
|
|
35 # but please, practice safe toolshed.
|
|
36 # Read the fucking code before you install any tool
|
|
37 # especially this one
|
|
38
|
|
39 # After you get the script working on some test data, you can
|
|
40 # optionally generate a toolshed compatible gzip file
|
|
41 # containing your script safely wrapped as an ordinary Galaxy script in your local toolshed for
|
|
42 # safe and largely automated installation in a production Galaxy.
|
|
43
|
|
44 # If you opt for an HTML output, you get all the script outputs arranged
|
|
45 # as a single Html history item - all output files are linked, thumbnails for all the pdfs.
|
|
46 # Ugly but really inexpensive.
|
|
47 #
|
|
48 # Patches appreciated please.
|
|
49 #
|
|
50 #
|
|
51 # long route to June 2012 product
|
|
52 # Behold the awesome power of Galaxy and the toolshed with the tool factory to bind them
|
|
53 # derived from an integrated script model
|
|
54 # called rgBaseScriptWrapper.py
|
|
55 # Note to the unwary:
|
|
56 # This tool allows arbitrary scripting on your Galaxy as the Galaxy user
|
|
57 # There is nothing stopping a malicious user doing whatever they choose
|
|
58 # Extremely dangerous!!
|
|
59 # Totally insecure. So, trusted users only
|
|
60 #
|
|
61 # preferred model is a developer using their throw away workstation instance - ie a private site.
|
|
62 # no real risk. The universe_wsgi.ini admin_users string is checked - only admin users are permitted to run this tool.
|
|
63 #
|
|
64
|
|
65 import sys
|
|
66 import shutil
|
|
67 import subprocess
|
|
68 import os
|
|
69 import time
|
|
70 import tempfile
|
|
71 import optparse
|
|
72 import tarfile
|
|
73 import re
|
|
74 import shutil
|
|
75 import math
|
|
76
|
|
77 progname = os.path.split(sys.argv[0])[1]
|
|
78 myversion = 'V000.2 June 2012'
|
|
79 verbose = False
|
|
80 debug = False
|
|
81 toolFactoryURL = 'https://bitbucket.org/fubar/galaxytoolfactory'
|
|
82
|
|
83 def timenow():
|
|
84 """return current time as a string
|
|
85 """
|
|
86 return time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(time.time()))
|
|
87
|
|
88 html_escape_table = {
|
|
89 "&": "&",
|
|
90 ">": ">",
|
|
91 "<": "<",
|
|
92 "$": "\$"
|
|
93 }
|
|
94
|
|
95 def html_escape(text):
|
|
96 """Produce entities within text."""
|
|
97 return "".join(html_escape_table.get(c,c) for c in text)
|
|
98
|
|
99 def cmd_exists(cmd):
|
|
100 return subprocess.call("type " + cmd, shell=True,
|
|
101 stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0
|
|
102
|
|
103
|
|
104 class ScriptRunner:
|
|
105 """class is a wrapper for an arbitrary script
|
|
106 """
|
|
107
|
|
108 def __init__(self,opts=None,treatbashSpecial=True):
|
|
109 """
|
|
110 cleanup inputs, setup some outputs
|
|
111
|
|
112 """
|
|
113 self.useGM = cmd_exists('gm')
|
|
114 self.useIM = cmd_exists('convert')
|
|
115 self.useGS = cmd_exists('gs')
|
|
116 self.treatbashSpecial = treatbashSpecial
|
|
117 if opts.output_dir: # simplify for the tool tarball
|
|
118 os.chdir(opts.output_dir)
|
6
|
119 self.thumbformat = 'png'
|
0
|
120 self.opts = opts
|
|
121 self.toolname = re.sub('[^a-zA-Z0-9_]+', '', opts.tool_name) # a sanitizer now does this but..
|
|
122 self.toolid = self.toolname
|
|
123 self.myname = sys.argv[0] # get our name because we write ourselves out as a tool later
|
|
124 self.pyfile = self.myname # crude but efficient - the cruft won't hurt much
|
|
125 self.xmlfile = '%s.xml' % self.toolname
|
|
126 s = open(self.opts.script_path,'r').readlines()
|
|
127 s = [x.rstrip() for x in s] # remove pesky dos line endings if needed
|
|
128 self.script = '\n'.join(s)
|
|
129 fhandle,self.sfile = tempfile.mkstemp(prefix=self.toolname,suffix=".%s" % (opts.interpreter))
|
|
130 tscript = open(self.sfile,'w') # use self.sfile as script source for Popen
|
|
131 tscript.write(self.script)
|
|
132 tscript.close()
|
|
133 self.indentedScript = '\n'.join([' %s' % x for x in s]) # for restructured text in help
|
|
134 self.escapedScript = '\n'.join([html_escape(x) for x in s])
|
19
|
135 self.elog = os.path.join(self.opts.output_dir,"%s_error.log" % self.toolname)
|
|
136 if opts.output_dir: # may not want these complexities
|
0
|
137 self.tlog = os.path.join(self.opts.output_dir,"%s_runner.log" % self.toolname)
|
|
138 art = '%s.%s' % (self.toolname,opts.interpreter)
|
|
139 artpath = os.path.join(self.opts.output_dir,art) # need full path
|
|
140 artifact = open(artpath,'w') # use self.sfile as script source for Popen
|
|
141 artifact.write(self.script)
|
|
142 artifact.close()
|
|
143 self.cl = []
|
|
144 self.html = []
|
|
145 a = self.cl.append
|
|
146 a(opts.interpreter)
|
|
147 if self.treatbashSpecial and opts.interpreter in ['bash','sh']:
|
|
148 a(self.sfile)
|
|
149 else:
|
|
150 a('-') # stdin
|
|
151 a(opts.input_tab)
|
|
152 a(opts.output_tab)
|
|
153 self.outFormats = 'tabular' # TODO make this an option at tool generation time
|
|
154 self.inputFormats = 'tabular' # TODO make this an option at tool generation time
|
|
155 self.test1Input = '%s_test1_input.xls' % self.toolname
|
|
156 self.test1Output = '%s_test1_output.xls' % self.toolname
|
|
157 self.test1HTML = '%s_test1_output.html' % self.toolname
|
|
158
|
|
159 def makeXML(self):
|
|
160 """
|
|
161 Create a Galaxy xml tool wrapper for the new script as a string to write out
|
|
162 fixme - use templating or something less fugly than this example of what we produce
|
|
163
|
|
164 <tool id="reverse" name="reverse" version="0.01">
|
|
165 <description>a tabular file</description>
|
|
166 <command interpreter="python">
|
|
167 reverse.py --script_path "$runMe" --interpreter "python"
|
|
168 --tool_name "reverse" --input_tab "$input1" --output_tab "$tab_file"
|
|
169 </command>
|
|
170 <inputs>
|
|
171 <param name="input1" type="data" format="tabular" label="Select a suitable input file from your history"/><param name="job_name" type="text" label="Supply a name for the outputs to remind you what they contain" value="reverse"/>
|
|
172
|
|
173 </inputs>
|
|
174 <outputs>
|
|
175 <data format="tabular" name="tab_file" label="${job_name}"/>
|
|
176
|
|
177 </outputs>
|
|
178 <help>
|
|
179
|
|
180 **What it Does**
|
|
181
|
|
182 Reverse the columns in a tabular file
|
|
183
|
|
184 </help>
|
|
185 <configfiles>
|
|
186 <configfile name="runMe">
|
|
187
|
|
188 # reverse order of columns in a tabular file
|
|
189 import sys
|
|
190 inp = sys.argv[1]
|
|
191 outp = sys.argv[2]
|
|
192 i = open(inp,'r')
|
|
193 o = open(outp,'w')
|
|
194 for row in i:
|
|
195 rs = row.rstrip().split('\t')
|
|
196 rs.reverse()
|
|
197 o.write('\t'.join(rs))
|
|
198 o.write('\n')
|
|
199 i.close()
|
|
200 o.close()
|
|
201
|
|
202
|
|
203 </configfile>
|
|
204 </configfiles>
|
|
205 </tool>
|
|
206
|
|
207 """
|
|
208 newXML="""<tool id="%(toolid)s" name="%(toolname)s" version="%(tool_version)s">
|
|
209 %(tooldesc)s
|
|
210 %(command)s
|
|
211 <inputs>
|
|
212 %(inputs)s
|
|
213 </inputs>
|
|
214 <outputs>
|
|
215 %(outputs)s
|
|
216 </outputs>
|
|
217 <configfiles>
|
|
218 <configfile name="runMe">
|
|
219 %(script)s
|
|
220 </configfile>
|
|
221 </configfiles>
|
|
222 %(tooltests)s
|
|
223 <help>
|
|
224 %(help)s
|
|
225 </help>
|
|
226 </tool>""" # needs a dict with toolname, toolid, interpreter, scriptname, command, inputs as a multi line string ready to write, outputs ditto, help ditto
|
|
227
|
|
228 newCommand="""<command interpreter="python">
|
|
229 %(toolname)s.py --script_path "$runMe" --interpreter "%(interpreter)s"
|
|
230 --tool_name "%(toolname)s" %(command_inputs)s %(command_outputs)s
|
|
231 </command>""" # may NOT be an input or htmlout
|
|
232 tooltestsTabOnly = """<tests><test>
|
|
233 <param name="input1" value="%(test1Input)s" ftype="tabular"/>
|
|
234 <param name="job_name" value="test1"/>
|
|
235 <param name="runMe" value="$runMe"/>
|
|
236 <output name="tab_file" file="%(test1Output)s" ftype="tabular"/>
|
|
237 </test></tests>"""
|
|
238 tooltestsHTMLOnly = """<tests><test>
|
|
239 <param name="input1" value="%(test1Input)s" ftype="tabular"/>
|
|
240 <param name="job_name" value="test1"/>
|
|
241 <param name="runMe" value="$runMe"/>
|
|
242 <output name="html_file" file="%(test1HTML)s" ftype="html" lines_diff="5"/>
|
|
243 </test></tests>"""
|
|
244 tooltestsBoth = """<tests><test>
|
|
245 <param name="input1" value="%(test1Input)s" ftype="tabular"/>
|
|
246 <param name="job_name" value="test1"/>
|
|
247 <param name="runMe" value="$runMe"/>
|
|
248 <output name="tab_file" file="%(test1Output)s" ftype="tabular" />
|
|
249 <output name="html_file" file="%(test1HTML)s" ftype="html" lines_diff="10"/>
|
|
250 </test></tests>"""
|
|
251 xdict = {}
|
|
252 xdict['tool_version'] = self.opts.tool_version
|
|
253 xdict['test1Input'] = self.test1Input
|
|
254 xdict['test1HTML'] = self.test1HTML
|
|
255 xdict['test1Output'] = self.test1Output
|
|
256 if self.opts.make_HTML and self.opts.output_tab <> 'None':
|
|
257 xdict['tooltests'] = tooltestsBoth % xdict
|
|
258 elif self.opts.make_HTML:
|
|
259 xdict['tooltests'] = tooltestsHTMLOnly % xdict
|
|
260 else:
|
|
261 xdict['tooltests'] = tooltestsTabOnly % xdict
|
|
262 xdict['script'] = self.escapedScript
|
|
263 # configfile is least painful way to embed script to avoid external dependencies
|
|
264 # but requires escaping of <, > and $ to avoid Mako parsing
|
|
265 if self.opts.help_text:
|
|
266 xdict['help'] = open(self.opts.help_text,'r').read()
|
|
267 else:
|
|
268 xdict['help'] = 'Please ask the tool author for help as none was supplied at tool generation'
|
|
269 coda = ['**Script**','Pressing execute will run the following code over your input file and generate some outputs in your history::']
|
|
270 coda.append(self.indentedScript)
|
|
271 coda.append('**Attribution** This Galaxy tool was created by %s at %s\nusing the Galaxy Tool Factory.' % (self.opts.user_email,timenow()))
|
|
272 coda.append('See %s for details of that project' % (toolFactoryURL))
|
|
273 coda.append('Please cite: Creating re-usable tools from scripts: The Galaxy Tool Factory. Ross Lazarus; Antony Kaspi; Mark Ziemann; The Galaxy Team. ')
|
|
274 coda.append('Bioinformatics 2012; doi: 10.1093/bioinformatics/bts573')
|
|
275 xdict['help'] = '%s\n%s' % (xdict['help'],'\n'.join(coda))
|
|
276 if self.opts.tool_desc:
|
|
277 xdict['tooldesc'] = '<description>%s</description>' % self.opts.tool_desc
|
|
278 else:
|
|
279 xdict['tooldesc'] = ''
|
|
280 xdict['command_outputs'] = ''
|
|
281 xdict['outputs'] = ''
|
|
282 if self.opts.input_tab <> 'None':
|
|
283 xdict['command_inputs'] = '--input_tab "$input1" ' # the space may matter a lot if we append something
|
|
284 xdict['inputs'] = '<param name="input1" type="data" format="%s" label="Select a suitable input file from your history"/> \n' % self.inputFormats
|
|
285 else:
|
|
286 xdict['command_inputs'] = '' # assume no input - eg a random data generator
|
|
287 xdict['inputs'] = ''
|
|
288 xdict['inputs'] += '<param name="job_name" type="text" label="Supply a name for the outputs to remind you what they contain" value="%s"/> \n' % self.toolname
|
|
289 xdict['toolname'] = self.toolname
|
|
290 xdict['toolid'] = self.toolid
|
|
291 xdict['interpreter'] = self.opts.interpreter
|
|
292 xdict['scriptname'] = self.sfile
|
|
293 if self.opts.make_HTML:
|
|
294 xdict['command_outputs'] += ' --output_dir "$html_file.files_path" --output_html "$html_file" --make_HTML "yes" '
|
|
295 xdict['outputs'] += ' <data format="html" name="html_file" label="${job_name}.html"/>\n'
|
|
296 if self.opts.output_tab <> 'None':
|
|
297 xdict['command_outputs'] += ' --output_tab "$tab_file"'
|
|
298 xdict['outputs'] += ' <data format="%s" name="tab_file" label="${job_name}"/>\n' % self.outFormats
|
|
299 xdict['command'] = newCommand % xdict
|
|
300 xmls = newXML % xdict
|
|
301 xf = open(self.xmlfile,'w')
|
|
302 xf.write(xmls)
|
|
303 xf.write('\n')
|
|
304 xf.close()
|
|
305 # ready for the tarball
|
|
306
|
|
307
|
|
308 def makeTooltar(self):
|
|
309 """
|
|
310 a tool is a gz tarball with eg
|
|
311 /toolname/tool.xml /toolname/tool.py /toolname/test-data/test1_in.foo ...
|
|
312 """
|
|
313 retval = self.run()
|
|
314 if retval:
|
|
315 print >> sys.stderr,'## Run failed. Cannot build yet. Please fix and retry'
|
|
316 sys.exit(1)
|
|
317 self.makeXML()
|
|
318 tdir = self.toolname
|
|
319 os.mkdir(tdir)
|
|
320 if self.opts.input_tab <> 'None': # no reproducible test otherwise? TODO: maybe..
|
|
321 testdir = os.path.join(tdir,'test-data')
|
|
322 os.mkdir(testdir) # make tests directory
|
|
323 shutil.copyfile(self.opts.input_tab,os.path.join(testdir,self.test1Input))
|
|
324 if self.opts.output_tab <> 'None':
|
|
325 shutil.copyfile(self.opts.output_tab,os.path.join(testdir,self.test1Output))
|
|
326 if self.opts.make_HTML:
|
|
327 shutil.copyfile(self.opts.output_html,os.path.join(testdir,self.test1HTML))
|
|
328 if self.opts.output_dir:
|
|
329 shutil.copyfile(self.tlog,os.path.join(testdir,'test1_out.log'))
|
|
330 op = '%s.py' % self.toolname # new name
|
|
331 outpiname = os.path.join(tdir,op) # path for the tool tarball
|
|
332 pyin = os.path.basename(self.pyfile) # our name - we rewrite ourselves (TM)
|
|
333 notes = ['# %s - a self annotated version of %s generated by running %s\n' % (op,pyin,pyin),]
|
|
334 notes.append('# to make a new Galaxy tool called %s\n' % self.toolname)
|
|
335 notes.append('# User %s at %s\n' % (self.opts.user_email,timenow()))
|
|
336 pi = open(self.pyfile,'r').readlines() # our code becomes new tool wrapper (!) - first Galaxy worm
|
|
337 notes += pi
|
|
338 outpi = open(outpiname,'w')
|
|
339 outpi.write(''.join(notes))
|
|
340 outpi.write('\n')
|
|
341 outpi.close()
|
|
342 stname = os.path.join(tdir,self.sfile)
|
|
343 if not os.path.exists(stname):
|
|
344 shutil.copyfile(self.sfile, stname)
|
|
345 xtname = os.path.join(tdir,self.xmlfile)
|
|
346 if not os.path.exists(xtname):
|
|
347 shutil.copyfile(self.xmlfile,xtname)
|
|
348 tarpath = "%s.gz" % self.toolname
|
|
349 tar = tarfile.open(tarpath, "w:gz")
|
|
350 tar.add(tdir,arcname=self.toolname)
|
|
351 tar.close()
|
|
352 shutil.copyfile(tarpath,self.opts.new_tool)
|
|
353 shutil.rmtree(tdir)
|
|
354 ## TODO: replace with optional direct upload to local toolshed?
|
|
355 return retval
|
|
356
|
|
357
|
|
358 def compressPDF(self,inpdf=None,thumbformat='png'):
|
|
359 """need absolute path to pdf
|
|
360 """
|
|
361 assert os.path.isfile(inpdf), "## Input %s supplied to %s compressPDF not found" % (inpdf,self.myName)
|
|
362 hf,hlog = tempfile.mkstemp(suffix="%s.log" % self.toolname)
|
|
363 sto = open(hlog,'w')
|
|
364 outpdf = '%s_compressed' % inpdf
|
|
365 cl = ["gs", "-sDEVICE=pdfwrite", "-dNOPAUSE", "-dBATCH","-dPDFSETTINGS=/printer", "-sOutputFile=%s" % outpdf,inpdf]
|
|
366 x = subprocess.Popen(cl,stdout=sto,stderr=sto,cwd=self.opts.output_dir)
|
|
367 retval1 = x.wait()
|
|
368 if retval1 == 0:
|
|
369 os.unlink(inpdf)
|
|
370 shutil.move(outpdf,inpdf)
|
|
371 outpng = '%s.%s' % (os.path.splitext(inpdf)[0],thumbformat)
|
|
372 if self.useGM:
|
|
373 cl2 = ['gm convert', inpdf, outpng]
|
|
374 else: # assume imagemagick
|
|
375 cl2 = ['convert', inpdf, outpng]
|
|
376 x = subprocess.Popen(cl2,stdout=sto,stderr=sto,cwd=self.opts.output_dir)
|
|
377 retval2 = x.wait()
|
|
378 sto.close()
|
|
379 retval = retval1 or retval2
|
|
380 return retval
|
|
381
|
|
382
|
|
383 def getfSize(self,fpath,outpath):
|
|
384 """
|
|
385 format a nice file size string
|
|
386 """
|
|
387 size = ''
|
|
388 fp = os.path.join(outpath,fpath)
|
|
389 if os.path.isfile(fp):
|
|
390 size = '0 B'
|
|
391 n = float(os.path.getsize(fp))
|
|
392 if n > 2**20:
|
|
393 size = '%1.1f MB' % (n/2**20)
|
|
394 elif n > 2**10:
|
|
395 size = '%1.1f KB' % (n/2**10)
|
|
396 elif n > 0:
|
|
397 size = '%d B' % (int(n))
|
|
398 return size
|
|
399
|
|
400 def makeHtml(self):
|
|
401 """ Create an HTML file content to list all the artifacts found in the output_dir
|
|
402 """
|
|
403
|
|
404 galhtmlprefix = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
405 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
406 <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
407 <meta name="generator" content="Galaxy %s tool output - see http://g2.trac.bx.psu.edu/" />
|
|
408 <title></title>
|
|
409 <link rel="stylesheet" href="/static/style/base.css" type="text/css" />
|
|
410 </head>
|
|
411 <body>
|
|
412 <div class="toolFormBody">
|
|
413 """
|
|
414 galhtmlattr = """<hr/><div class="infomessage">This tool (%s) was generated by the <a href="https://bitbucket.org/fubar/galaxytoolfactory/overview">Galaxy Tool Factory</a></div><br/>"""
|
|
415 galhtmlpostfix = """</div></body></html>\n"""
|
|
416
|
|
417 flist = os.listdir(self.opts.output_dir)
|
|
418 flist = [x for x in flist if x <> 'Rplots.pdf']
|
|
419 flist.sort()
|
|
420 html = []
|
|
421 html.append(galhtmlprefix % progname)
|
|
422 html.append('<div class="infomessage">Galaxy Tool "%s" run at %s</div><br/>' % (self.toolname,timenow()))
|
|
423 fhtml = []
|
|
424 if len(flist) > 0:
|
|
425 logfiles = [x for x in flist if x.lower().endswith('.log')] # log file names determine sections
|
|
426 logfiles.sort()
|
|
427 logfiles = [x for x in logfiles if os.path.abspath(x) <> os.path.abspath(self.tlog)]
|
|
428 logfiles.append(os.path.abspath(self.tlog)) # make it the last one
|
|
429 pdflist = []
|
|
430 npdf = len([x for x in flist if os.path.splitext(x)[-1].lower() == '.pdf'])
|
|
431 for rownum,fname in enumerate(flist):
|
|
432 dname,e = os.path.splitext(fname)
|
|
433 sfsize = self.getfSize(fname,self.opts.output_dir)
|
|
434 if e.lower() == '.pdf' : # compress and make a thumbnail
|
|
435 thumb = '%s.%s' % (dname,self.thumbformat)
|
|
436 pdff = os.path.join(self.opts.output_dir,fname)
|
|
437 retval = self.compressPDF(inpdf=pdff,thumbformat=self.thumbformat)
|
|
438 if retval == 0:
|
|
439 pdflist.append((fname,thumb))
|
|
440 if (rownum+1) % 2 == 0:
|
|
441 fhtml.append('<tr class="odd_row"><td><a href="%s">%s</a></td><td>%s</td></tr>' % (fname,fname,sfsize))
|
|
442 else:
|
|
443 fhtml.append('<tr><td><a href="%s">%s</a></td><td>%s</td></tr>' % (fname,fname,sfsize))
|
|
444 for logfname in logfiles: # expect at least tlog - if more
|
|
445 if os.path.abspath(logfname) == os.path.abspath(self.tlog): # handled later
|
|
446 sectionname = 'All tool run'
|
|
447 if (len(logfiles) > 1):
|
|
448 sectionname = 'Other'
|
|
449 ourpdfs = pdflist
|
|
450 else:
|
|
451 realname = os.path.basename(logfname)
|
|
452 sectionname = os.path.splitext(realname)[0].split('_')[0] # break in case _ added to log
|
|
453 ourpdfs = [x for x in pdflist if os.path.basename(x[0]).split('_')[0] == sectionname]
|
|
454 pdflist = [x for x in pdflist if os.path.basename(x[0]).split('_')[0] <> sectionname] # remove
|
|
455 nacross = 1
|
|
456 npdf = len(ourpdfs)
|
|
457
|
|
458 if npdf > 0:
|
|
459 nacross = math.sqrt(npdf) ## int(round(math.log(npdf,2)))
|
|
460 if int(nacross)**2 != npdf:
|
|
461 nacross += 1
|
|
462 nacross = int(nacross)
|
|
463 width = min(400,int(1200/nacross))
|
|
464 html.append('<div class="toolFormTitle">%s images and outputs</div>' % sectionname)
|
|
465 html.append('(Click on a thumbnail image to download the corresponding original PDF image)<br/>')
|
|
466 ntogo = nacross # counter for table row padding with empty cells
|
|
467 html.append('<div><table class="simple" cellpadding="2" cellspacing="2">\n<tr>')
|
|
468 for i,paths in enumerate(ourpdfs):
|
|
469 fname,thumb = paths
|
|
470 s= """<td><a href="%s"><img src="%s" title="Click to download a PDF of %s" hspace="5" width="%d"
|
|
471 alt="Image called %s"/></a></td>\n""" % (fname,thumb,fname,width,fname)
|
|
472 if ((i+1) % nacross == 0):
|
|
473 s += '</tr>\n'
|
|
474 ntogo = 0
|
|
475 if i < (npdf - 1): # more to come
|
|
476 s += '<tr>'
|
|
477 ntogo = nacross
|
|
478 else:
|
|
479 ntogo -= 1
|
|
480 html.append(s)
|
|
481 if html[-1].strip().endswith('</tr>'):
|
|
482 html.append('</table></div>\n')
|
|
483 else:
|
|
484 if ntogo > 0: # pad
|
|
485 html.append('<td> </td>'*ntogo)
|
|
486 html.append('</tr></table></div>\n')
|
|
487 logt = open(logfname,'r').readlines()
|
|
488 logtext = [x for x in logt if x.strip() > '']
|
|
489 html.append('<div class="toolFormTitle">%s log output</div>' % sectionname)
|
|
490 if len(logtext) > 1:
|
|
491 html.append('\n<pre>\n')
|
|
492 html += logtext
|
|
493 html.append('\n</pre>\n')
|
|
494 else:
|
|
495 html.append('%s is empty<br/>' % logfname)
|
|
496 if len(fhtml) > 0:
|
|
497 fhtml.insert(0,'<div><table class="colored" cellpadding="3" cellspacing="3"><tr><th>Output File Name (click to view)</th><th>Size</th></tr>\n')
|
|
498 fhtml.append('</table></div><br/>')
|
|
499 html.append('<div class="toolFormTitle">All output files available for downloading</div>\n')
|
|
500 html += fhtml # add all non-pdf files to the end of the display
|
|
501 else:
|
|
502 html.append('<div class="warningmessagelarge">### Error - %s returned no files - please confirm that parameters are sane</div>' % self.opts.interpreter)
|
|
503 html.append(galhtmlpostfix)
|
|
504 htmlf = file(self.opts.output_html,'w')
|
|
505 htmlf.write('\n'.join(html))
|
|
506 htmlf.write('\n')
|
|
507 htmlf.close()
|
|
508 self.html = html
|
|
509
|
|
510
|
|
511 def run(self):
|
|
512 """
|
|
513 scripts must be small enough not to fill the pipe!
|
|
514 """
|
|
515 if self.treatbashSpecial and self.opts.interpreter in ['bash','sh']:
|
|
516 retval = self.runBash()
|
|
517 else:
|
|
518 if self.opts.output_dir:
|
19
|
519 ste = open(self.elog,'w')
|
0
|
520 sto = open(self.tlog,'w')
|
|
521 sto.write('## Toolfactory generated command line = %s\n' % ' '.join(self.cl))
|
|
522 sto.flush()
|
19
|
523 p = subprocess.Popen(self.cl,shell=False,stdout=sto,stderr=elog,stdin=subprocess.PIPE,cwd=self.opts.output_dir)
|
0
|
524 else:
|
|
525 p = subprocess.Popen(self.cl,shell=False,stdin=subprocess.PIPE)
|
|
526 p.stdin.write(self.script)
|
|
527 p.stdin.close()
|
|
528 retval = p.wait()
|
|
529 if self.opts.output_dir:
|
|
530 sto.close()
|
19
|
531 ste.close()
|
|
532 err = open(self.elog,'r').readlines()
|
|
533 if retval <> 0 and err: # problem
|
|
534 print >> sys.stderr,err
|
0
|
535 if self.opts.make_HTML:
|
|
536 self.makeHtml()
|
|
537 return retval
|
|
538
|
|
539 def runBash(self):
|
|
540 """
|
|
541 cannot use - for bash so use self.sfile
|
|
542 """
|
|
543 if self.opts.output_dir:
|
|
544 s = '## Toolfactory generated command line = %s\n' % ' '.join(self.cl)
|
|
545 sto = open(self.tlog,'w')
|
|
546 sto.write(s)
|
|
547 sto.flush()
|
|
548 p = subprocess.Popen(self.cl,shell=False,stdout=sto,stderr=sto,cwd=self.opts.output_dir)
|
|
549 else:
|
|
550 p = subprocess.Popen(self.cl,shell=False)
|
|
551 retval = p.wait()
|
|
552 if self.opts.output_dir:
|
|
553 sto.close()
|
|
554 if self.opts.make_HTML:
|
|
555 self.makeHtml()
|
|
556 return retval
|
|
557
|
|
558
|
|
559 def main():
|
|
560 u = """
|
|
561 This is a Galaxy wrapper. It expects to be called by a special purpose tool.xml as:
|
|
562 <command interpreter="python">rgBaseScriptWrapper.py --script_path "$scriptPath" --tool_name "foo" --interpreter "Rscript"
|
|
563 </command>
|
|
564 """
|
|
565 op = optparse.OptionParser()
|
|
566 a = op.add_option
|
|
567 a('--script_path',default=None)
|
|
568 a('--tool_name',default=None)
|
|
569 a('--interpreter',default=None)
|
|
570 a('--output_dir',default=None)
|
|
571 a('--output_html',default=None)
|
|
572 a('--input_tab',default="None")
|
|
573 a('--output_tab',default="None")
|
|
574 a('--user_email',default='Unknown')
|
|
575 a('--bad_user',default=None)
|
|
576 a('--make_Tool',default=None)
|
|
577 a('--make_HTML',default=None)
|
|
578 a('--help_text',default=None)
|
|
579 a('--tool_desc',default=None)
|
|
580 a('--new_tool',default=None)
|
|
581 a('--tool_version',default=None)
|
|
582 opts, args = op.parse_args()
|
|
583 assert not opts.bad_user,'UNAUTHORISED: %s is NOT authorized to use this tool until Galaxy admin adds %s to admin_users in universe_wsgi.ini' % (opts.bad_user,opts.bad_user)
|
|
584 assert opts.tool_name,'## Tool Factory expects a tool name - eg --tool_name=DESeq'
|
|
585 assert opts.interpreter,'## Tool Factory wrapper expects an interpreter - eg --interpreter=Rscript'
|
|
586 assert os.path.isfile(opts.script_path),'## Tool Factory wrapper expects a script path - eg --script_path=foo.R'
|
|
587 if opts.output_dir:
|
|
588 try:
|
|
589 os.makedirs(opts.output_dir)
|
|
590 except:
|
|
591 pass
|
|
592 r = ScriptRunner(opts)
|
|
593 if opts.make_Tool:
|
|
594 retcode = r.makeTooltar()
|
|
595 else:
|
|
596 retcode = r.run()
|
|
597 os.unlink(r.sfile)
|
|
598 if retcode:
|
|
599 sys.exit(retcode) # indicate failure to job runner
|
|
600
|
|
601
|
|
602 if __name__ == "__main__":
|
|
603 main()
|
|
604
|
|
605
|