annotate rgToolFactory.py @ 26:63082502722a draft

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