annotate rgToolFactory.py @ 22:4e3aa95ed3ac draft

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