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