annotate fubar-galaxytoolfactory-008551b45fdb/rgToolFactory.py @ 6:3cf6c3cf837b draft

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