annotate transposetest/transposetest.py @ 0:bf814ef1c04d draft default tip

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