annotate rgToolFactory.py @ 42:4b807bc775ad draft

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