annotate fubar-galaxytoolfactory-cfcf6c9df5b7/rgToolFactory.py @ 1:87613ace5113 draft

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