annotate rgToolFactory2.py @ 20:d98f5a09137f draft

Uploaded
author fubar
date Mon, 02 Feb 2015 20:57:40 -0500
parents
children 71b85c322600
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
d98f5a09137f Uploaded
fubar
parents:
diff changeset
1 # rgToolFactoryMultIn.py
d98f5a09137f Uploaded
fubar
parents:
diff changeset
2 # see https://bitbucket.org/fubar/galaxytoolfactory/wiki/Home
d98f5a09137f Uploaded
fubar
parents:
diff changeset
3 #
d98f5a09137f Uploaded
fubar
parents:
diff changeset
4 # copyright ross lazarus (ross stop lazarus at gmail stop com) May 2012
d98f5a09137f Uploaded
fubar
parents:
diff changeset
5 #
d98f5a09137f Uploaded
fubar
parents:
diff changeset
6 # all rights reserved
d98f5a09137f Uploaded
fubar
parents:
diff changeset
7 # Licensed under the LGPL
d98f5a09137f Uploaded
fubar
parents:
diff changeset
8 # suggestions for improvement and bug fixes welcome at https://bitbucket.org/fubar/galaxytoolfactory/wiki/Home
d98f5a09137f Uploaded
fubar
parents:
diff changeset
9 #
d98f5a09137f Uploaded
fubar
parents:
diff changeset
10 # January 2015
d98f5a09137f Uploaded
fubar
parents:
diff changeset
11 # unified all setups by passing the script on the cl rather than via a PIPE - no need for treat_bash_special so removed
d98f5a09137f Uploaded
fubar
parents:
diff changeset
12 #
d98f5a09137f Uploaded
fubar
parents:
diff changeset
13 # in the process of building a complex tool
d98f5a09137f Uploaded
fubar
parents:
diff changeset
14 # added ability to choose one of the current toolshed package_r or package_perl or package_python dependencies and source that package
d98f5a09137f Uploaded
fubar
parents:
diff changeset
15 # add that package to tool_dependencies
d98f5a09137f Uploaded
fubar
parents:
diff changeset
16 # Note that once the generated tool is loaded, it will have that package's env.sh loaded automagically so there is no
d98f5a09137f Uploaded
fubar
parents:
diff changeset
17 # --envshpath in the parameters for the generated tool and it uses the system one which will be first on the adjusted path.
d98f5a09137f Uploaded
fubar
parents:
diff changeset
18 #
d98f5a09137f Uploaded
fubar
parents:
diff changeset
19 # sept 2014 added additional params from
d98f5a09137f Uploaded
fubar
parents:
diff changeset
20 # https://bitbucket.org/mvdbeek/dockertoolfactory/src/d4863bcf7b521532c7e8c61b6333840ba5393f73/DockerToolFactory.py?at=default
d98f5a09137f Uploaded
fubar
parents:
diff changeset
21 # passing them is complex
d98f5a09137f Uploaded
fubar
parents:
diff changeset
22 # and they are restricted to NOT contain commas or double quotes to ensure that they can be safely passed together on
d98f5a09137f Uploaded
fubar
parents:
diff changeset
23 # the toolfactory command line as a comma delimited double quoted string for parsing and passing to the script
d98f5a09137f Uploaded
fubar
parents:
diff changeset
24 # see examples on this tool form
d98f5a09137f Uploaded
fubar
parents:
diff changeset
25
d98f5a09137f Uploaded
fubar
parents:
diff changeset
26 # august 2014
d98f5a09137f Uploaded
fubar
parents:
diff changeset
27
d98f5a09137f Uploaded
fubar
parents:
diff changeset
28 # Allows arbitrary number of input files
d98f5a09137f Uploaded
fubar
parents:
diff changeset
29 # NOTE positional parameters are now passed to script
d98f5a09137f Uploaded
fubar
parents:
diff changeset
30 # and output (may be "None") is *before* arbitrary number of inputs
d98f5a09137f Uploaded
fubar
parents:
diff changeset
31 #
d98f5a09137f Uploaded
fubar
parents:
diff changeset
32 # march 2014
d98f5a09137f Uploaded
fubar
parents:
diff changeset
33 # had to remove dependencies because cross toolshed dependencies are not possible - can't pre-specify a toolshed url for graphicsmagick and ghostscript
d98f5a09137f Uploaded
fubar
parents:
diff changeset
34 # grrrrr - night before a demo
d98f5a09137f Uploaded
fubar
parents:
diff changeset
35 # added dependencies to a tool_dependencies.xml if html page generated so generated tool is properly portable
d98f5a09137f Uploaded
fubar
parents:
diff changeset
36 #
d98f5a09137f Uploaded
fubar
parents:
diff changeset
37 # added ghostscript and graphicsmagick as dependencies
d98f5a09137f Uploaded
fubar
parents:
diff changeset
38 # fixed a wierd problem where gs was trying to use the new_files_path from universe (database/tmp) as ./database/tmp
d98f5a09137f Uploaded
fubar
parents:
diff changeset
39 # errors ensued
d98f5a09137f Uploaded
fubar
parents:
diff changeset
40 #
d98f5a09137f Uploaded
fubar
parents:
diff changeset
41 # august 2013
d98f5a09137f Uploaded
fubar
parents:
diff changeset
42 # found a problem with GS if $TMP or $TEMP missing - now inject /tmp and warn
d98f5a09137f Uploaded
fubar
parents:
diff changeset
43 #
d98f5a09137f Uploaded
fubar
parents:
diff changeset
44 # july 2013
d98f5a09137f Uploaded
fubar
parents:
diff changeset
45 # added ability to combine images and individual log files into html output
d98f5a09137f Uploaded
fubar
parents:
diff changeset
46 # just make sure there's a log file foo.log and it will be output
d98f5a09137f Uploaded
fubar
parents:
diff changeset
47 # together with all images named like "foo_*.pdf
d98f5a09137f Uploaded
fubar
parents:
diff changeset
48 # otherwise old format for html
d98f5a09137f Uploaded
fubar
parents:
diff changeset
49 #
d98f5a09137f Uploaded
fubar
parents:
diff changeset
50 # January 2013
d98f5a09137f Uploaded
fubar
parents:
diff changeset
51 # problem pointed out by Carlos Borroto
d98f5a09137f Uploaded
fubar
parents:
diff changeset
52 # added escaping for <>$ - thought I did that ages ago...
d98f5a09137f Uploaded
fubar
parents:
diff changeset
53 #
d98f5a09137f Uploaded
fubar
parents:
diff changeset
54 # August 11 2012
d98f5a09137f Uploaded
fubar
parents:
diff changeset
55 # changed to use shell=False and cl as a sequence
d98f5a09137f Uploaded
fubar
parents:
diff changeset
56
d98f5a09137f Uploaded
fubar
parents:
diff changeset
57 # This is a Galaxy tool factory for simple scripts in python, R or whatever ails ye.
d98f5a09137f Uploaded
fubar
parents:
diff changeset
58 # It also serves as the wrapper for the new tool.
d98f5a09137f Uploaded
fubar
parents:
diff changeset
59 #
d98f5a09137f Uploaded
fubar
parents:
diff changeset
60 # you paste and run your script
d98f5a09137f Uploaded
fubar
parents:
diff changeset
61 # Only works for simple scripts that read one input from the history.
d98f5a09137f Uploaded
fubar
parents:
diff changeset
62 # Optionally can write one new history dataset,
d98f5a09137f Uploaded
fubar
parents:
diff changeset
63 # and optionally collect any number of outputs into links on an autogenerated HTML page.
d98f5a09137f Uploaded
fubar
parents:
diff changeset
64
d98f5a09137f Uploaded
fubar
parents:
diff changeset
65 # DO NOT install on a public or important site - please.
d98f5a09137f Uploaded
fubar
parents:
diff changeset
66
d98f5a09137f Uploaded
fubar
parents:
diff changeset
67 # installed generated tools are fine if the script is safe.
d98f5a09137f Uploaded
fubar
parents:
diff changeset
68 # They just run normally and their user cannot do anything unusually insecure
d98f5a09137f Uploaded
fubar
parents:
diff changeset
69 # but please, practice safe toolshed.
d98f5a09137f Uploaded
fubar
parents:
diff changeset
70 # Read the fucking code before you install any tool
d98f5a09137f Uploaded
fubar
parents:
diff changeset
71 # especially this one
d98f5a09137f Uploaded
fubar
parents:
diff changeset
72
d98f5a09137f Uploaded
fubar
parents:
diff changeset
73 # After you get the script working on some test data, you can
d98f5a09137f Uploaded
fubar
parents:
diff changeset
74 # optionally generate a toolshed compatible gzip file
d98f5a09137f Uploaded
fubar
parents:
diff changeset
75 # containing your script safely wrapped as an ordinary Galaxy script in your local toolshed for
d98f5a09137f Uploaded
fubar
parents:
diff changeset
76 # safe and largely automated installation in a production Galaxy.
d98f5a09137f Uploaded
fubar
parents:
diff changeset
77
d98f5a09137f Uploaded
fubar
parents:
diff changeset
78 # If you opt for an HTML output, you get all the script outputs arranged
d98f5a09137f Uploaded
fubar
parents:
diff changeset
79 # as a single Html history item - all output files are linked, thumbnails for all the pdfs.
d98f5a09137f Uploaded
fubar
parents:
diff changeset
80 # Ugly but really inexpensive.
d98f5a09137f Uploaded
fubar
parents:
diff changeset
81 #
d98f5a09137f Uploaded
fubar
parents:
diff changeset
82 # Patches appreciated please.
d98f5a09137f Uploaded
fubar
parents:
diff changeset
83 #
d98f5a09137f Uploaded
fubar
parents:
diff changeset
84 #
d98f5a09137f Uploaded
fubar
parents:
diff changeset
85 # long route to June 2012 product
d98f5a09137f Uploaded
fubar
parents:
diff changeset
86 # Behold the awesome power of Galaxy and the toolshed with the tool factory to bind them
d98f5a09137f Uploaded
fubar
parents:
diff changeset
87 # derived from an integrated script model
d98f5a09137f Uploaded
fubar
parents:
diff changeset
88 # called rgBaseScriptWrapper.py
d98f5a09137f Uploaded
fubar
parents:
diff changeset
89 # Note to the unwary:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
90 # This tool allows arbitrary scripting on your Galaxy as the Galaxy user
d98f5a09137f Uploaded
fubar
parents:
diff changeset
91 # There is nothing stopping a malicious user doing whatever they choose
d98f5a09137f Uploaded
fubar
parents:
diff changeset
92 # Extremely dangerous!!
d98f5a09137f Uploaded
fubar
parents:
diff changeset
93 # Totally insecure. So, trusted users only
d98f5a09137f Uploaded
fubar
parents:
diff changeset
94 #
d98f5a09137f Uploaded
fubar
parents:
diff changeset
95 # preferred model is a developer using their throw away workstation instance - ie a private site.
d98f5a09137f Uploaded
fubar
parents:
diff changeset
96 # no real risk. The universe_wsgi.ini admin_users string is checked - only admin users are permitted to run this tool.
d98f5a09137f Uploaded
fubar
parents:
diff changeset
97 #
d98f5a09137f Uploaded
fubar
parents:
diff changeset
98
d98f5a09137f Uploaded
fubar
parents:
diff changeset
99 import sys
d98f5a09137f Uploaded
fubar
parents:
diff changeset
100 import shutil
d98f5a09137f Uploaded
fubar
parents:
diff changeset
101 import subprocess
d98f5a09137f Uploaded
fubar
parents:
diff changeset
102 import os
d98f5a09137f Uploaded
fubar
parents:
diff changeset
103 import time
d98f5a09137f Uploaded
fubar
parents:
diff changeset
104 import tempfile
d98f5a09137f Uploaded
fubar
parents:
diff changeset
105 import optparse
d98f5a09137f Uploaded
fubar
parents:
diff changeset
106 import tarfile
d98f5a09137f Uploaded
fubar
parents:
diff changeset
107 import re
d98f5a09137f Uploaded
fubar
parents:
diff changeset
108 import shutil
d98f5a09137f Uploaded
fubar
parents:
diff changeset
109 import math
d98f5a09137f Uploaded
fubar
parents:
diff changeset
110
d98f5a09137f Uploaded
fubar
parents:
diff changeset
111 progname = os.path.split(sys.argv[0])[1]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
112 myversion = 'V001.1 March 2014'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
113 verbose = False
d98f5a09137f Uploaded
fubar
parents:
diff changeset
114 debug = False
d98f5a09137f Uploaded
fubar
parents:
diff changeset
115 toolFactoryURL = 'https://bitbucket.org/fubar/galaxytoolfactory'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
116
d98f5a09137f Uploaded
fubar
parents:
diff changeset
117 # if we do html we need these dependencies specified in a tool_dependencies.xml file and referred to in the generated
d98f5a09137f Uploaded
fubar
parents:
diff changeset
118 # tool xml
d98f5a09137f Uploaded
fubar
parents:
diff changeset
119
d98f5a09137f Uploaded
fubar
parents:
diff changeset
120 def timenow():
d98f5a09137f Uploaded
fubar
parents:
diff changeset
121 """return current time as a string
d98f5a09137f Uploaded
fubar
parents:
diff changeset
122 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
123 return time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(time.time()))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
124
d98f5a09137f Uploaded
fubar
parents:
diff changeset
125 def quote_non_numeric(s):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
126 """return a prequoted string for non-numerics
d98f5a09137f Uploaded
fubar
parents:
diff changeset
127 useful for perl and Rscript parameter passing?
d98f5a09137f Uploaded
fubar
parents:
diff changeset
128 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
129 try:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
130 res = float(s)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
131 return s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
132 except ValueError:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
133 return '"%s"' % s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
134
d98f5a09137f Uploaded
fubar
parents:
diff changeset
135 html_escape_table = {
d98f5a09137f Uploaded
fubar
parents:
diff changeset
136 "&": "&amp;",
d98f5a09137f Uploaded
fubar
parents:
diff changeset
137 ">": "&gt;",
d98f5a09137f Uploaded
fubar
parents:
diff changeset
138 "<": "&lt;",
d98f5a09137f Uploaded
fubar
parents:
diff changeset
139 "$": "\$"
d98f5a09137f Uploaded
fubar
parents:
diff changeset
140 }
d98f5a09137f Uploaded
fubar
parents:
diff changeset
141
d98f5a09137f Uploaded
fubar
parents:
diff changeset
142 def html_escape(text):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
143 """Produce entities within text."""
d98f5a09137f Uploaded
fubar
parents:
diff changeset
144 return "".join(html_escape_table.get(c,c) for c in text)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
145
d98f5a09137f Uploaded
fubar
parents:
diff changeset
146
d98f5a09137f Uploaded
fubar
parents:
diff changeset
147 def html_unescape(text):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
148 """Revert entities within text."""
d98f5a09137f Uploaded
fubar
parents:
diff changeset
149 t = text.replace('&amp;','&').replace('&gt;','>').replace('&lt;','<').replace('\$','$')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
150 return t
d98f5a09137f Uploaded
fubar
parents:
diff changeset
151
d98f5a09137f Uploaded
fubar
parents:
diff changeset
152 def cmd_exists(cmd):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
153 return subprocess.call("type " + cmd, shell=True,
d98f5a09137f Uploaded
fubar
parents:
diff changeset
154 stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0
d98f5a09137f Uploaded
fubar
parents:
diff changeset
155
d98f5a09137f Uploaded
fubar
parents:
diff changeset
156 def parse_citations(citations_text):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
157 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
158 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
159 citations = [c for c in citations_text.split("**ENTRY**") if c.strip()]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
160 citation_tuples = []
d98f5a09137f Uploaded
fubar
parents:
diff changeset
161 for citation in citations:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
162 if citation.startswith("doi"):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
163 citation_tuples.append( ("doi", citation[len("doi"):].strip() ) )
d98f5a09137f Uploaded
fubar
parents:
diff changeset
164 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
165 citation_tuples.append( ("bibtex", citation[len("bibtex"):].strip() ) )
d98f5a09137f Uploaded
fubar
parents:
diff changeset
166 return citation_tuples
d98f5a09137f Uploaded
fubar
parents:
diff changeset
167
d98f5a09137f Uploaded
fubar
parents:
diff changeset
168 def shell_source(script):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
169 """need a way to source a Galaxy tool interpreter env.sh to point at the right dependency package
d98f5a09137f Uploaded
fubar
parents:
diff changeset
170 This based on the idea in http://pythonwise.blogspot.fr/2010/04/sourcing-shell-script.html
d98f5a09137f Uploaded
fubar
parents:
diff changeset
171 Note that we have to finesse any wierdly quoted newlines in automagic exports using nulls (env -0) as newlines"""
d98f5a09137f Uploaded
fubar
parents:
diff changeset
172 pipe = subprocess.Popen("env -i ; . %s ; env -0" % script, stdout=subprocess.PIPE, shell=True)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
173 output = pipe.communicate()[0]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
174 outl = output.split('\0')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
175 outl = [x for x in outl if len(x.split("=")) == 2]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
176 newenv = dict((line.split("=", 1) for line in outl))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
177 os.environ.update(newenv)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
178
d98f5a09137f Uploaded
fubar
parents:
diff changeset
179 class ScriptRunner:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
180 """class is a wrapper for an arbitrary script
d98f5a09137f Uploaded
fubar
parents:
diff changeset
181 note funky templating. this should all be done proper.
d98f5a09137f Uploaded
fubar
parents:
diff changeset
182 Problem is, this kludge developed quite naturally and seems to work ok with
d98f5a09137f Uploaded
fubar
parents:
diff changeset
183 little overhead...
d98f5a09137f Uploaded
fubar
parents:
diff changeset
184
d98f5a09137f Uploaded
fubar
parents:
diff changeset
185 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
186
d98f5a09137f Uploaded
fubar
parents:
diff changeset
187
d98f5a09137f Uploaded
fubar
parents:
diff changeset
188 def __init__(self,opts=None):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
189 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
190 cleanup inputs, setup some outputs
d98f5a09137f Uploaded
fubar
parents:
diff changeset
191
d98f5a09137f Uploaded
fubar
parents:
diff changeset
192 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
193
d98f5a09137f Uploaded
fubar
parents:
diff changeset
194 self.toolhtmldepinterpskel = """<?xml version="1.0"?>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
195 <tool_dependency>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
196 <package name="ghostscript" version="9.10">
d98f5a09137f Uploaded
fubar
parents:
diff changeset
197 <repository name="package_ghostscript_9_10" owner="devteam" prior_installation_required="True" />
d98f5a09137f Uploaded
fubar
parents:
diff changeset
198 </package>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
199 <package name="graphicsmagick" version="1.3.18">
d98f5a09137f Uploaded
fubar
parents:
diff changeset
200 <repository name="package_graphicsmagick_1_3" owner="iuc" prior_installation_required="True" />
d98f5a09137f Uploaded
fubar
parents:
diff changeset
201 </package>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
202 <package name="%(interpreter_name)s" version="%(interpreter_version)s">
d98f5a09137f Uploaded
fubar
parents:
diff changeset
203 <repository name="%(interpreter_pack)s" owner="%(interpreter_owner)s" prior_installation_required="True" />
d98f5a09137f Uploaded
fubar
parents:
diff changeset
204 </package>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
205
d98f5a09137f Uploaded
fubar
parents:
diff changeset
206 <readme>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
207 %(readme)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
208 This file was autogenerated by the Galaxy Tool Factory 2
d98f5a09137f Uploaded
fubar
parents:
diff changeset
209 </readme>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
210 </tool_dependency>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
211 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
212
d98f5a09137f Uploaded
fubar
parents:
diff changeset
213 self.toolhtmldepskel = """<?xml version="1.0"?>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
214 <tool_dependency>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
215 <package name="ghostscript" version="9.10">
d98f5a09137f Uploaded
fubar
parents:
diff changeset
216 <repository name="package_ghostscript_9_10" owner="devteam" prior_installation_required="True" />
d98f5a09137f Uploaded
fubar
parents:
diff changeset
217 </package>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
218 <package name="graphicsmagick" version="1.3.18">
d98f5a09137f Uploaded
fubar
parents:
diff changeset
219 <repository name="package_graphicsmagick_1_3" owner="iuc" prior_installation_required="True" />
d98f5a09137f Uploaded
fubar
parents:
diff changeset
220 </package>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
221 <readme>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
222 %(readme)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
223 This file was autogenerated by the Galaxy Tool Factory 2
d98f5a09137f Uploaded
fubar
parents:
diff changeset
224 </readme>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
225 </tool_dependency>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
226 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
227
d98f5a09137f Uploaded
fubar
parents:
diff changeset
228 self.emptytoolhtmldepskel = """<?xml version="1.0"?>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
229 <tool_dependency>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
230 <readme>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
231 %(readme)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
232 This file was autogenerated by the Galaxy Tool Factory 2
d98f5a09137f Uploaded
fubar
parents:
diff changeset
233 </readme>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
234 </tool_dependency>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
235 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
236
d98f5a09137f Uploaded
fubar
parents:
diff changeset
237 self.protorequirements = """<requirements>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
238 <requirement type="package" version="9.10">ghostscript</requirement>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
239 <requirement type="package" version="1.3.18">graphicsmagick</requirement>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
240 </requirements>"""
d98f5a09137f Uploaded
fubar
parents:
diff changeset
241
d98f5a09137f Uploaded
fubar
parents:
diff changeset
242 self.protorequirements_interpreter = """<requirements>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
243 <requirement type="package" version="9.10">ghostscript</requirement>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
244 <requirement type="package" version="1.3.18">graphicsmagick</requirement>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
245 <requirement type="package" version="%(interpreter_version)s">%(interpreter_name)s</requirement>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
246 </requirements>"""
d98f5a09137f Uploaded
fubar
parents:
diff changeset
247
d98f5a09137f Uploaded
fubar
parents:
diff changeset
248
d98f5a09137f Uploaded
fubar
parents:
diff changeset
249 self.newCommand="""
d98f5a09137f Uploaded
fubar
parents:
diff changeset
250 %(toolname)s.py --script_path "$runMe" --interpreter "%(interpreter)s"
d98f5a09137f Uploaded
fubar
parents:
diff changeset
251 --tool_name "%(toolname)s"
d98f5a09137f Uploaded
fubar
parents:
diff changeset
252 %(command_inputs)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
253 %(command_outputs)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
254 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
255
d98f5a09137f Uploaded
fubar
parents:
diff changeset
256 self.tooltestsTabOnly = """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
257 <test>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
258 %(test1Inputs)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
259 <param name="job_name" value="test1"/>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
260 <param name="runMe" value="$runMe"/>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
261 <output name="output1="%(test1Output)s" ftype="tabular"/>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
262 %(additionalParams)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
263 </test>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
264 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
265
d98f5a09137f Uploaded
fubar
parents:
diff changeset
266 self.tooltestsHTMLOnly = """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
267 <test>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
268 %(test1Inputs)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
269 <param name="job_name" value="test1"/>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
270 <param name="runMe" value="$runMe"/>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
271 %(additionalParams)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
272 <output name="html_file" file="%(test1HTML)s" ftype="html" lines_diff="5"/>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
273 </test>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
274 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
275
d98f5a09137f Uploaded
fubar
parents:
diff changeset
276 self.tooltestsBoth = """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
277 <test>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
278 %(test1Inputs)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
279 <param name="job_name" value="test1"/>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
280 <param name="runMe" value="$runMe"/>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
281 %(additionalParams)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
282 <output name="output1" file="%(test1Output)s" ftype="tabular" />
d98f5a09137f Uploaded
fubar
parents:
diff changeset
283 <output name="html_file" file="%(test1HTML)s" ftype="html" lines_diff="10"/>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
284 </test>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
285 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
286
d98f5a09137f Uploaded
fubar
parents:
diff changeset
287 self.newXML="""<tool id="%(toolid)s" name="%(toolname)s" version="%(tool_version)s">
d98f5a09137f Uploaded
fubar
parents:
diff changeset
288 %(tooldesc)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
289 %(requirements)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
290 <command interpreter="python">
d98f5a09137f Uploaded
fubar
parents:
diff changeset
291 %(command)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
292 </command>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
293 <inputs>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
294 %(inputs)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
295 %(additionalInputs)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
296 </inputs>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
297 <outputs>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
298 %(outputs)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
299 </outputs>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
300 <configfiles>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
301 <configfile name="runMe">
d98f5a09137f Uploaded
fubar
parents:
diff changeset
302 %(script)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
303 </configfile>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
304 </configfiles>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
305 <tests>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
306 %(tooltests)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
307 </tests>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
308 <help>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
309
d98f5a09137f Uploaded
fubar
parents:
diff changeset
310 %(help)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
311
d98f5a09137f Uploaded
fubar
parents:
diff changeset
312 This tool was autogenerated from a user provided script using the Galaxy Tool Factory 2
d98f5a09137f Uploaded
fubar
parents:
diff changeset
313 https://toolshed.g2.bx.psu.edu/view/fubar/tool_factory_2
d98f5a09137f Uploaded
fubar
parents:
diff changeset
314 </help>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
315 <citations>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
316 %(citations)s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
317 <citation type="doi">10.1093/bioinformatics/bts573</citation>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
318 </citations>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
319 </tool>"""
d98f5a09137f Uploaded
fubar
parents:
diff changeset
320
d98f5a09137f Uploaded
fubar
parents:
diff changeset
321 self.useGM = cmd_exists('gm')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
322 self.useIM = cmd_exists('convert')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
323 self.useGS = cmd_exists('gs')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
324 self.temp_warned = False # we want only one warning if $TMP not set
d98f5a09137f Uploaded
fubar
parents:
diff changeset
325 if opts.output_dir: # simplify for the tool tarball
d98f5a09137f Uploaded
fubar
parents:
diff changeset
326 os.chdir(opts.output_dir)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
327 self.thumbformat = 'png'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
328 self.opts = opts
d98f5a09137f Uploaded
fubar
parents:
diff changeset
329 self.toolname = re.sub('[^a-zA-Z0-9_]+', '', opts.tool_name) # a sanitizer now does this but..
d98f5a09137f Uploaded
fubar
parents:
diff changeset
330 self.toolid = self.toolname
d98f5a09137f Uploaded
fubar
parents:
diff changeset
331 self.myname = sys.argv[0] # get our name because we write ourselves out as a tool later
d98f5a09137f Uploaded
fubar
parents:
diff changeset
332 self.pyfile = self.myname # crude but efficient - the cruft won't hurt much
d98f5a09137f Uploaded
fubar
parents:
diff changeset
333 self.xmlfile = '%s.xml' % self.toolname
d98f5a09137f Uploaded
fubar
parents:
diff changeset
334 rx = open(self.opts.script_path,'r').readlines()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
335 rx = [x.rstrip() for x in rx] # remove pesky dos line endings if needed
d98f5a09137f Uploaded
fubar
parents:
diff changeset
336 self.script = '\n'.join(rx)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
337 fhandle,self.sfile = tempfile.mkstemp(prefix=self.toolname,suffix=".%s" % (opts.interpreter))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
338 tscript = open(self.sfile,'w') # use self.sfile as script source for Popen
d98f5a09137f Uploaded
fubar
parents:
diff changeset
339 tscript.write(self.script)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
340 tscript.close()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
341 self.indentedScript = " %s" % '\n'.join([' %s' % html_escape(x) for x in rx]) # for restructured text in help
d98f5a09137f Uploaded
fubar
parents:
diff changeset
342 self.escapedScript = "%s" % '\n'.join([' %s' % html_escape(x) for x in rx])
d98f5a09137f Uploaded
fubar
parents:
diff changeset
343 self.elog = os.path.join(self.opts.output_dir,"%s_error.log" % self.toolname)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
344 if opts.output_dir: # may not want these complexities
d98f5a09137f Uploaded
fubar
parents:
diff changeset
345 self.tlog = os.path.join(self.opts.output_dir,"%s_runner.log" % self.toolname)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
346 art = '%s.%s' % (self.toolname,opts.interpreter)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
347 artpath = os.path.join(self.opts.output_dir,art) # need full path
d98f5a09137f Uploaded
fubar
parents:
diff changeset
348 artifact = open(artpath,'w') # use self.sfile as script source for Popen
d98f5a09137f Uploaded
fubar
parents:
diff changeset
349 artifact.write(self.script)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
350 artifact.close()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
351 self.cl = []
d98f5a09137f Uploaded
fubar
parents:
diff changeset
352 self.html = []
d98f5a09137f Uploaded
fubar
parents:
diff changeset
353 self.test1Inputs = [] # now a list
d98f5a09137f Uploaded
fubar
parents:
diff changeset
354 a = self.cl.append
d98f5a09137f Uploaded
fubar
parents:
diff changeset
355 a(opts.interpreter)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
356 a(self.sfile)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
357 # if multiple inputs - positional or need to distinguish them with cl params
d98f5a09137f Uploaded
fubar
parents:
diff changeset
358 if opts.input_tab:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
359 tests = []
d98f5a09137f Uploaded
fubar
parents:
diff changeset
360 for i,intab in enumerate(opts.input_tab): # if multiple, make tests
d98f5a09137f Uploaded
fubar
parents:
diff changeset
361 if intab.find(',') <> -1:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
362 (gpath,uname) = intab.split(',')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
363 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
364 gpath = uname = intab
d98f5a09137f Uploaded
fubar
parents:
diff changeset
365 tests.append(os.path.basename(gpath))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
366 self.test1Inputs = '<param name="input_tab" value="%s" />' % (','.join(tests))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
367 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
368 self.test1Inputs = ''
d98f5a09137f Uploaded
fubar
parents:
diff changeset
369 # we always pass path,name pairs in using python optparse append
d98f5a09137f Uploaded
fubar
parents:
diff changeset
370 # but the command line has to be different
d98f5a09137f Uploaded
fubar
parents:
diff changeset
371 self.infile_paths = ''
d98f5a09137f Uploaded
fubar
parents:
diff changeset
372 self.infile_names = ''
d98f5a09137f Uploaded
fubar
parents:
diff changeset
373 if self.opts.input_tab:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
374 self.infile_paths = ','.join([x.split(',')[0].strip() for x in self.opts.input_tab])
d98f5a09137f Uploaded
fubar
parents:
diff changeset
375 self.infile_names = ','.join([x.split(',')[1].strip() for x in self.opts.input_tab])
d98f5a09137f Uploaded
fubar
parents:
diff changeset
376 if self.opts.interpreter == 'python':
d98f5a09137f Uploaded
fubar
parents:
diff changeset
377 # yes, this is how additional parameters are always passed in python - to the TF itself and to
d98f5a09137f Uploaded
fubar
parents:
diff changeset
378 # scripts to avoid having unknown parameter names (yes, they can be parsed but...) on the command line
d98f5a09137f Uploaded
fubar
parents:
diff changeset
379 if self.opts.input_tab:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
380 a('--INPATHS "%s"' % (self.infile_paths))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
381 a('--INNAMES "%s"' % (self.infile_names))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
382 if self.opts.output_tab:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
383 a('--OUTPATH "%s"' % self.opts.output_tab)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
384 for p in opts.additional_parameters:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
385 p = p.replace('"','')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
386 psplit = p.split(',')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
387 param = html_unescape(psplit[0])
d98f5a09137f Uploaded
fubar
parents:
diff changeset
388 value = html_unescape(psplit[1])
d98f5a09137f Uploaded
fubar
parents:
diff changeset
389 a('%s="%s"' % (param,value))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
390 if (self.opts.interpreter == 'Rscript'):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
391 # pass params on command line as expressions which the script evaluates - see sample
d98f5a09137f Uploaded
fubar
parents:
diff changeset
392 if self.opts.input_tab:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
393 a('INPATHS="%s"' % self.infile_paths)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
394 a('INNAMES="%s"' % self.infile_names)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
395 if self.opts.output_tab:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
396 a('OUTPATH="%s"' % self.opts.output_tab)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
397 for p in opts.additional_parameters:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
398 p = p.replace('"','')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
399 psplit = p.split(',')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
400 param = html_unescape(psplit[0])
d98f5a09137f Uploaded
fubar
parents:
diff changeset
401 value = html_unescape(psplit[1])
d98f5a09137f Uploaded
fubar
parents:
diff changeset
402 a('%s=%s' % (param,quote_non_numeric(value)))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
403 if (self.opts.interpreter == 'perl'):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
404 # pass positional params on command line - perl script needs to discombobulate the path/name lists
d98f5a09137f Uploaded
fubar
parents:
diff changeset
405 if self.opts.input_tab:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
406 a('%s' % self.infile_paths)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
407 a('%s' % self.infile_names)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
408 if self.opts.output_tab:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
409 a('%s' % self.opts.output_tab)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
410 for p in opts.additional_parameters:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
411 # followed by any additional name=value parameter pairs
d98f5a09137f Uploaded
fubar
parents:
diff changeset
412 p = p.replace('"','')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
413 psplit = p.split(',')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
414 param = html_unescape(psplit[0])
d98f5a09137f Uploaded
fubar
parents:
diff changeset
415 value = html_unescape(psplit[1])
d98f5a09137f Uploaded
fubar
parents:
diff changeset
416 a('%s=%s' % (param,quote_non_numeric(value)))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
417 if self.opts.interpreter == 'sh' or self.opts.interpreter == 'bash':
d98f5a09137f Uploaded
fubar
parents:
diff changeset
418 # more is better - now move all params into environment AND drop on to command line.
d98f5a09137f Uploaded
fubar
parents:
diff changeset
419 self.cl.insert(0,'env')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
420 if self.opts.input_tab:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
421 self.cl.insert(1,'INPATHS=%s' % (self.infile_paths))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
422 self.cl.insert(2,'INNAMES=%s' % (self.infile_names))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
423 if self.opts.output_tab:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
424 self.cl.insert(3,'OUTPATH=%s' % (self.opts.output_tab))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
425 a('OUTPATH=%s' % (self.opts.output_tab))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
426 # sets those environment variables for the script
d98f5a09137f Uploaded
fubar
parents:
diff changeset
427 # additional params appear in CL - yes, it's confusing
d98f5a09137f Uploaded
fubar
parents:
diff changeset
428 for i,p in enumerate(opts.additional_parameters):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
429 psplit = p.split(',')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
430 param = html_unescape(psplit[0])
d98f5a09137f Uploaded
fubar
parents:
diff changeset
431 value = html_unescape(psplit[1])
d98f5a09137f Uploaded
fubar
parents:
diff changeset
432 a('%s=%s' % (param,quote_non_numeric(value)))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
433 self.cl.insert(4+i,'%s=%s' % (param,quote_non_numeric(value)))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
434 self.interpreter_owner = 'SYSTEM'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
435 self.interpreter_pack = 'SYSTEM'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
436 self.interpreter_name = 'SYSTEM'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
437 self.interpreter_version = 'SYSTEM'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
438 self.interpreter_revision = 'SYSTEM'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
439 if opts.envshpath <> 'system': # need to parse out details for our tool_dependency
d98f5a09137f Uploaded
fubar
parents:
diff changeset
440 try: # fragile - depends on common naming convention as at jan 2015 = package_[interp]_v0_v1_v2... = version v0.v1.v2.. is in play
d98f5a09137f Uploaded
fubar
parents:
diff changeset
441 # this ONLY happens at tool generation by an admin - the generated tool always uses the default of system so path is from local env.sh
d98f5a09137f Uploaded
fubar
parents:
diff changeset
442 packdetails = opts.envshpath.split(os.path.sep)[-4:-1] # eg ['fubar', 'package_r_3_1_1', '63cdb9b2234c']
d98f5a09137f Uploaded
fubar
parents:
diff changeset
443 self.interpreter_owner = packdetails[0]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
444 self.interpreter_pack = packdetails[1]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
445 self.interpreter_name = packdetails[1].split('_')[1].upper()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
446 self.interpreter_revision = packdetails[2]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
447 self.interpreter_version = '.'.join(packdetails[1].split('_')[2:])
d98f5a09137f Uploaded
fubar
parents:
diff changeset
448 except:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
449 pass
d98f5a09137f Uploaded
fubar
parents:
diff changeset
450 self.outFormats = opts.output_format
d98f5a09137f Uploaded
fubar
parents:
diff changeset
451 self.inputFormats = opts.input_formats
d98f5a09137f Uploaded
fubar
parents:
diff changeset
452 self.test1Output = '%s_test1_output.xls' % self.toolname
d98f5a09137f Uploaded
fubar
parents:
diff changeset
453 self.test1HTML = '%s_test1_output.html' % self.toolname
d98f5a09137f Uploaded
fubar
parents:
diff changeset
454
d98f5a09137f Uploaded
fubar
parents:
diff changeset
455 def makeXML(self):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
456 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
457 Create a Galaxy xml tool wrapper for the new script as a string to write out
d98f5a09137f Uploaded
fubar
parents:
diff changeset
458 fixme - use templating or something less fugly than this example of what we produce
d98f5a09137f Uploaded
fubar
parents:
diff changeset
459
d98f5a09137f Uploaded
fubar
parents:
diff changeset
460 <tool id="reverse" name="reverse" version="0.01">
d98f5a09137f Uploaded
fubar
parents:
diff changeset
461 <description>a tabular file</description>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
462 <command interpreter="python">
d98f5a09137f Uploaded
fubar
parents:
diff changeset
463 reverse.py --script_path "$runMe" --interpreter "python"
d98f5a09137f Uploaded
fubar
parents:
diff changeset
464 --tool_name "reverse" --input_tab "$input1" --output_tab "$output1"
d98f5a09137f Uploaded
fubar
parents:
diff changeset
465 </command>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
466 <inputs>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
467 <param name="input1" type="data" format="tabular" label="Select one or more input files from your history"/>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
468 <param name="job_name" type="text" label="Supply a name for the outputs to remind you what they contain" value="reverse"/>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
469 </inputs>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
470 <outputs>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
471 <data format="tabular" name="output1q" label="${job_name}"/>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
472
d98f5a09137f Uploaded
fubar
parents:
diff changeset
473 </outputs>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
474 <help>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
475
d98f5a09137f Uploaded
fubar
parents:
diff changeset
476 **What it Does**
d98f5a09137f Uploaded
fubar
parents:
diff changeset
477
d98f5a09137f Uploaded
fubar
parents:
diff changeset
478 Reverse the columns in a tabular file
d98f5a09137f Uploaded
fubar
parents:
diff changeset
479
d98f5a09137f Uploaded
fubar
parents:
diff changeset
480 </help>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
481 <configfiles>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
482 <configfile name="runMe">
d98f5a09137f Uploaded
fubar
parents:
diff changeset
483
d98f5a09137f Uploaded
fubar
parents:
diff changeset
484 # reverse order of columns in a tabular file
d98f5a09137f Uploaded
fubar
parents:
diff changeset
485 import sys
d98f5a09137f Uploaded
fubar
parents:
diff changeset
486 inp = sys.argv[1]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
487 outp = sys.argv[2]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
488 i = open(inp,'r')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
489 o = open(outp,'w')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
490 for row in i:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
491 rs = row.rstrip().split('\t')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
492 rs.reverse()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
493 o.write('\t'.join(rs))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
494 o.write('\n')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
495 i.close()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
496 o.close()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
497
d98f5a09137f Uploaded
fubar
parents:
diff changeset
498
d98f5a09137f Uploaded
fubar
parents:
diff changeset
499 </configfile>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
500 </configfiles>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
501 </tool>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
502
d98f5a09137f Uploaded
fubar
parents:
diff changeset
503 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
504
d98f5a09137f Uploaded
fubar
parents:
diff changeset
505 # these templates need a dict with the right keys to match the parameters - outputs, help, code...
d98f5a09137f Uploaded
fubar
parents:
diff changeset
506
d98f5a09137f Uploaded
fubar
parents:
diff changeset
507 xdict = {}
d98f5a09137f Uploaded
fubar
parents:
diff changeset
508 xdict['additionalParams'] = ''
d98f5a09137f Uploaded
fubar
parents:
diff changeset
509 xdict['additionalInputs'] = ''
d98f5a09137f Uploaded
fubar
parents:
diff changeset
510 if self.opts.additional_parameters:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
511 if self.opts.edit_additional_parameters: # add to new tool form with default value set to original value
d98f5a09137f Uploaded
fubar
parents:
diff changeset
512 xdict['additionalInputs'] = '\n'.join(['<param name="%s" value="%s" label="%s" help="%s" type="%s"/>' % \
d98f5a09137f Uploaded
fubar
parents:
diff changeset
513 (x.split(',')[0],html_escape(x.split(',')[1]),html_escape(x.split(',')[2]),html_escape(x.split(',')[3]), x.split(',')[4]) for x in self.opts.additional_parameters])
d98f5a09137f Uploaded
fubar
parents:
diff changeset
514 xdict['additionalParams'] = '\n'.join(['<param name="%s" value="%s" />' % (x.split(',')[0],html_escape(x.split(',')[1])) for x in self.opts.additional_parameters])
d98f5a09137f Uploaded
fubar
parents:
diff changeset
515 xdict['interpreter_owner'] = self.interpreter_owner
d98f5a09137f Uploaded
fubar
parents:
diff changeset
516 xdict['interpreter_version'] = self.interpreter_version
d98f5a09137f Uploaded
fubar
parents:
diff changeset
517 xdict['interpreter_pack'] = self.interpreter_pack
d98f5a09137f Uploaded
fubar
parents:
diff changeset
518 xdict['interpreter_name'] = self.interpreter_name
d98f5a09137f Uploaded
fubar
parents:
diff changeset
519 xdict['requirements'] = ''
d98f5a09137f Uploaded
fubar
parents:
diff changeset
520 if self.opts.include_dependencies == "yes":
d98f5a09137f Uploaded
fubar
parents:
diff changeset
521 if self.opts.envshpath <> 'system':
d98f5a09137f Uploaded
fubar
parents:
diff changeset
522 xdict['requirements'] = self.protorequirements_interpreter % xdict
d98f5a09137f Uploaded
fubar
parents:
diff changeset
523 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
524 xdict['requirements'] = self.protorequirements
d98f5a09137f Uploaded
fubar
parents:
diff changeset
525 xdict['tool_version'] = self.opts.tool_version
d98f5a09137f Uploaded
fubar
parents:
diff changeset
526 xdict['test1HTML'] = self.test1HTML
d98f5a09137f Uploaded
fubar
parents:
diff changeset
527 xdict['test1Output'] = self.test1Output
d98f5a09137f Uploaded
fubar
parents:
diff changeset
528 xdict['test1Inputs'] = self.test1Inputs
d98f5a09137f Uploaded
fubar
parents:
diff changeset
529 if self.opts.make_HTML and self.opts.output_tab:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
530 xdict['tooltests'] = self.tooltestsBoth % xdict
d98f5a09137f Uploaded
fubar
parents:
diff changeset
531 elif self.opts.make_HTML:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
532 xdict['tooltests'] = self.tooltestsHTMLOnly % xdict
d98f5a09137f Uploaded
fubar
parents:
diff changeset
533 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
534 xdict['tooltests'] = self.tooltestsTabOnly % xdict
d98f5a09137f Uploaded
fubar
parents:
diff changeset
535 xdict['script'] = self.escapedScript
d98f5a09137f Uploaded
fubar
parents:
diff changeset
536 # configfile is least painful way to embed script to avoid external dependencies
d98f5a09137f Uploaded
fubar
parents:
diff changeset
537 # but requires escaping of <, > and $ to avoid Mako parsing
d98f5a09137f Uploaded
fubar
parents:
diff changeset
538 if self.opts.help_text:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
539 helptext = open(self.opts.help_text,'r').readlines()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
540 helptext = [html_escape(x) for x in helptext] # must html escape here too - thanks to Marius van den Beek
d98f5a09137f Uploaded
fubar
parents:
diff changeset
541 xdict['help'] = ''.join([x for x in helptext])
d98f5a09137f Uploaded
fubar
parents:
diff changeset
542 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
543 xdict['help'] = 'Please ask the tool author (%s) for help as none was supplied at tool generation\n' % (self.opts.user_email)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
544 coda = ['**Script**','Pressing execute will run the following code over your input file and generate some outputs in your history::']
d98f5a09137f Uploaded
fubar
parents:
diff changeset
545 coda.append('\n')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
546 coda.append(self.indentedScript)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
547 coda.append('\n**Attribution**\nThis Galaxy tool was created by %s at %s\nusing the Galaxy Tool Factory.\n' % (self.opts.user_email,timenow()))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
548 coda.append('See %s for details of that project' % (toolFactoryURL))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
549 coda.append('Please cite: Creating re-usable tools from scripts: The Galaxy Tool Factory. Ross Lazarus; Antony Kaspi; Mark Ziemann; The Galaxy Team. ')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
550 coda.append('Bioinformatics 2012; doi: 10.1093/bioinformatics/bts573\n')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
551 xdict['help'] = '%s\n%s' % (xdict['help'],'\n'.join(coda))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
552 if self.opts.tool_desc:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
553 xdict['tooldesc'] = '<description>%s</description>' % self.opts.tool_desc
d98f5a09137f Uploaded
fubar
parents:
diff changeset
554 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
555 xdict['tooldesc'] = ''
d98f5a09137f Uploaded
fubar
parents:
diff changeset
556 xdict['command_outputs'] = ''
d98f5a09137f Uploaded
fubar
parents:
diff changeset
557 xdict['outputs'] = ''
d98f5a09137f Uploaded
fubar
parents:
diff changeset
558 if self.opts.input_tab:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
559 cins = ['\n',]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
560 cins.append('--input_formats %s' % self.opts.input_formats)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
561 cins.append('#for intab in $input1:')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
562 cins.append('--input_tab "${intab},${intab.name}"')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
563 cins.append('#end for\n')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
564 xdict['command_inputs'] = '\n'.join(cins)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
565 xdict['inputs'] = '''<param name="input_tab" multiple="true" type="data" format="%s" label="Select one or more %s input files from your history"
d98f5a09137f Uploaded
fubar
parents:
diff changeset
566 help="Multiple inputs may be selected assuming the script can deal with them..."/> \n''' % (self.inputFormats,self.inputFormats)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
567 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
568 xdict['command_inputs'] = '' # assume no input - eg a random data generator
d98f5a09137f Uploaded
fubar
parents:
diff changeset
569 xdict['inputs'] = ''
d98f5a09137f Uploaded
fubar
parents:
diff changeset
570 if (len(self.opts.additional_parameters) > 0):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
571 cins = ['\n',]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
572 for params in self.opts.additional_parameters:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
573 psplit = params.split(',') # name,value...
d98f5a09137f Uploaded
fubar
parents:
diff changeset
574 psplit[3] = html_escape(psplit[3])
d98f5a09137f Uploaded
fubar
parents:
diff changeset
575 if self.opts.edit_additional_parameters:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
576 psplit[1] = '$%s' % psplit[0] # replace with form value
d98f5a09137f Uploaded
fubar
parents:
diff changeset
577 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
578 psplit[1] = html_escape(psplit[1]) # leave prespecified value
d98f5a09137f Uploaded
fubar
parents:
diff changeset
579 cins.append('--additional_parameters """%s"""' % ','.join(psplit))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
580 xdict['command_inputs'] = '%s\n%s' % (xdict['command_inputs'],'\n'.join(cins))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
581 xdict['inputs'] += '<param name="job_name" type="text" size="60" label="Supply a name for the outputs to remind you what they contain" value="%s"/> \n' % self.toolname
d98f5a09137f Uploaded
fubar
parents:
diff changeset
582 xdict['toolname'] = self.toolname
d98f5a09137f Uploaded
fubar
parents:
diff changeset
583 xdict['toolid'] = self.toolid
d98f5a09137f Uploaded
fubar
parents:
diff changeset
584 xdict['interpreter'] = self.opts.interpreter
d98f5a09137f Uploaded
fubar
parents:
diff changeset
585 xdict['scriptname'] = self.sfile
d98f5a09137f Uploaded
fubar
parents:
diff changeset
586 if self.opts.make_HTML:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
587 xdict['command_outputs'] += ' --output_dir "$html_file.files_path" --output_html "$html_file" --make_HTML "yes"'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
588 xdict['outputs'] += ' <data format="html" name="html_file" label="${job_name}.html"/>\n'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
589 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
590 xdict['command_outputs'] += ' --output_dir "./"'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
591 if self.opts.output_tab:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
592 xdict['command_outputs'] += ' --output_tab "$output1"'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
593 xdict['outputs'] += ' <data format="%s" name="output1" label="${job_name}"/>\n' % self.outFormats
d98f5a09137f Uploaded
fubar
parents:
diff changeset
594 xdict['command'] = self.newCommand % xdict
d98f5a09137f Uploaded
fubar
parents:
diff changeset
595 if self.opts.citations:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
596 citationstext = open(self.opts.citations,'r').read()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
597 citation_tuples = parse_citations(citationstext)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
598 citations_xml = ""
d98f5a09137f Uploaded
fubar
parents:
diff changeset
599 for citation_type, citation_content in citation_tuples:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
600 citation_xml = """<citation type="%s">%s</citation>""" % (citation_type, html_escape(citation_content))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
601 citations_xml += citation_xml
d98f5a09137f Uploaded
fubar
parents:
diff changeset
602 xdict['citations'] = citations_xml
d98f5a09137f Uploaded
fubar
parents:
diff changeset
603 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
604 xdict['citations'] = ""
d98f5a09137f Uploaded
fubar
parents:
diff changeset
605 xmls = self.newXML % xdict
d98f5a09137f Uploaded
fubar
parents:
diff changeset
606 xf = open(self.xmlfile,'w')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
607 xf.write(xmls)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
608 xf.write('\n')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
609 xf.close()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
610 # ready for the tarball
d98f5a09137f Uploaded
fubar
parents:
diff changeset
611
d98f5a09137f Uploaded
fubar
parents:
diff changeset
612
d98f5a09137f Uploaded
fubar
parents:
diff changeset
613 def makeTooltar(self):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
614 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
615 a tool is a gz tarball with eg
d98f5a09137f Uploaded
fubar
parents:
diff changeset
616 /toolname/tool.xml /toolname/tool.py /toolname/test-data/test1_in.foo ...
d98f5a09137f Uploaded
fubar
parents:
diff changeset
617 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
618 retval = self.run()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
619 if retval:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
620 print >> sys.stderr,'## Run failed. Cannot build yet. Please fix and retry'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
621 sys.exit(1)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
622 tdir = self.toolname
d98f5a09137f Uploaded
fubar
parents:
diff changeset
623 os.mkdir(tdir)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
624 self.makeXML()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
625 if self.opts.help_text:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
626 hlp = open(self.opts.help_text,'r').read()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
627 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
628 hlp = 'Please ask the tool author for help as none was supplied at tool generation\n'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
629 readme_dict = {'readme':hlp,'interpreter':self.opts.interpreter,'interpreter_version':self.interpreter_version,'interpreter_name':self.interpreter_name,
d98f5a09137f Uploaded
fubar
parents:
diff changeset
630 'interpreter_owner':self.interpreter_owner,'interpreter_pack':self.interpreter_pack}
d98f5a09137f Uploaded
fubar
parents:
diff changeset
631 if self.opts.include_dependencies == "yes":
d98f5a09137f Uploaded
fubar
parents:
diff changeset
632 if self.opts.envshpath == 'system':
d98f5a09137f Uploaded
fubar
parents:
diff changeset
633 tooldepcontent = self.toolhtmldepskel % readme_dict
d98f5a09137f Uploaded
fubar
parents:
diff changeset
634 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
635 tooldepcontent = self.toolhtmldepinterpskel % readme_dict
d98f5a09137f Uploaded
fubar
parents:
diff changeset
636 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
637 tooldepcontent = self.emptytoolhtmldepskel % readme_dict
d98f5a09137f Uploaded
fubar
parents:
diff changeset
638 depf = open(os.path.join(tdir,'tool_dependencies.xml'),'w')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
639 depf.write(tooldepcontent)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
640 depf.write('\n')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
641 depf.close()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
642 testdir = os.path.join(tdir,'test-data')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
643 os.mkdir(testdir) # make tests directory
d98f5a09137f Uploaded
fubar
parents:
diff changeset
644 for i,intab in enumerate(self.opts.input_tab):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
645 si = self.opts.input_tab[i]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
646 if si.find(',') <> -1:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
647 s = si.split(',')[0]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
648 si = s
d98f5a09137f Uploaded
fubar
parents:
diff changeset
649 dest = os.path.join(testdir,os.path.basename(si))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
650 if si <> dest:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
651 shutil.copyfile(si,dest)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
652 if self.opts.output_tab:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
653 shutil.copyfile(self.opts.output_tab,os.path.join(testdir,self.test1Output))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
654 if self.opts.make_HTML:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
655 shutil.copyfile(self.opts.output_html,os.path.join(testdir,self.test1HTML))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
656 if self.opts.output_dir:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
657 shutil.copyfile(self.tlog,os.path.join(testdir,'test1_out.log'))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
658 outpif = '%s.py' % self.toolname # new name
d98f5a09137f Uploaded
fubar
parents:
diff changeset
659 outpiname = os.path.join(tdir,outpif) # path for the tool tarball
d98f5a09137f Uploaded
fubar
parents:
diff changeset
660 pyin = os.path.basename(self.pyfile) # our name - we rewrite ourselves (TM)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
661 notes = ['# %s - a self annotated version of %s generated by running %s\n' % (outpiname,pyin,pyin),]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
662 notes.append('# to make a new Galaxy tool called %s\n' % self.toolname)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
663 notes.append('# User %s at %s\n' % (self.opts.user_email,timenow()))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
664 pi = open(self.pyfile,'r').readlines() # our code becomes new tool wrapper (!) - first Galaxy worm
d98f5a09137f Uploaded
fubar
parents:
diff changeset
665 notes += pi
d98f5a09137f Uploaded
fubar
parents:
diff changeset
666 outpi = open(outpiname,'w')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
667 outpi.write(''.join(notes))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
668 outpi.write('\n')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
669 outpi.close()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
670 stname = os.path.join(tdir,self.sfile)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
671 if not os.path.exists(stname):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
672 shutil.copyfile(self.sfile, stname)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
673 xtname = os.path.join(tdir,self.xmlfile)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
674 if not os.path.exists(xtname):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
675 shutil.copyfile(self.xmlfile,xtname)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
676 tarpath = "%s.tar.gz" % self.toolname
d98f5a09137f Uploaded
fubar
parents:
diff changeset
677 tar = tarfile.open(tarpath, "w:gz")
d98f5a09137f Uploaded
fubar
parents:
diff changeset
678 tar.add(tdir,arcname='%s' % self.toolname)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
679 tar.close()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
680 shutil.copyfile(tarpath,self.opts.new_tool)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
681 shutil.rmtree(tdir)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
682 ## TODO: replace with optional direct upload to local toolshed?
d98f5a09137f Uploaded
fubar
parents:
diff changeset
683 return retval
d98f5a09137f Uploaded
fubar
parents:
diff changeset
684
d98f5a09137f Uploaded
fubar
parents:
diff changeset
685
d98f5a09137f Uploaded
fubar
parents:
diff changeset
686 def compressPDF(self,inpdf=None,thumbformat='png'):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
687 """need absolute path to pdf
d98f5a09137f Uploaded
fubar
parents:
diff changeset
688 note that GS gets confoozled if no $TMP or $TEMP
d98f5a09137f Uploaded
fubar
parents:
diff changeset
689 so we set it
d98f5a09137f Uploaded
fubar
parents:
diff changeset
690 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
691 assert os.path.isfile(inpdf), "## Input %s supplied to %s compressPDF not found" % (inpdf,self.myName)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
692 hlog = os.path.join(self.opts.output_dir,"compress_%s.txt" % os.path.basename(inpdf))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
693 sto = open(hlog,'a')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
694 our_env = os.environ.copy()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
695 our_tmp = our_env.get('TMP',None)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
696 if not our_tmp:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
697 our_tmp = our_env.get('TEMP',None)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
698 if not (our_tmp and os.path.exists(our_tmp)):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
699 newtmp = os.path.join(self.opts.output_dir,'tmp')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
700 try:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
701 os.mkdir(newtmp)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
702 except:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
703 sto.write('## WARNING - cannot make %s - it may exist or permissions need fixing\n' % newtmp)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
704 our_env['TEMP'] = newtmp
d98f5a09137f Uploaded
fubar
parents:
diff changeset
705 if not self.temp_warned:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
706 sto.write('## WARNING - no $TMP or $TEMP!!! Please fix - using %s temporarily\n' % newtmp)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
707 self.temp_warned = True
d98f5a09137f Uploaded
fubar
parents:
diff changeset
708 outpdf = '%s_compressed' % inpdf
d98f5a09137f Uploaded
fubar
parents:
diff changeset
709 cl = ["gs", "-sDEVICE=pdfwrite", "-dNOPAUSE", "-dUseCIEColor", "-dBATCH","-dPDFSETTINGS=/printer", "-sOutputFile=%s" % outpdf,inpdf]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
710 x = subprocess.Popen(cl,stdout=sto,stderr=sto,cwd=self.opts.output_dir,env=our_env)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
711 retval1 = x.wait()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
712 sto.close()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
713 if retval1 == 0:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
714 os.unlink(inpdf)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
715 shutil.move(outpdf,inpdf)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
716 os.unlink(hlog)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
717 hlog = os.path.join(self.opts.output_dir,"thumbnail_%s.txt" % os.path.basename(inpdf))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
718 sto = open(hlog,'w')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
719 outpng = '%s.%s' % (os.path.splitext(inpdf)[0],thumbformat)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
720 if self.useGM:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
721 cl2 = ['gm', 'convert', inpdf, outpng]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
722 else: # assume imagemagick
d98f5a09137f Uploaded
fubar
parents:
diff changeset
723 cl2 = ['convert', inpdf, outpng]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
724 x = subprocess.Popen(cl2,stdout=sto,stderr=sto,cwd=self.opts.output_dir,env=our_env)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
725 retval2 = x.wait()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
726 sto.close()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
727 if retval2 == 0:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
728 os.unlink(hlog)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
729 retval = retval1 or retval2
d98f5a09137f Uploaded
fubar
parents:
diff changeset
730 return retval
d98f5a09137f Uploaded
fubar
parents:
diff changeset
731
d98f5a09137f Uploaded
fubar
parents:
diff changeset
732
d98f5a09137f Uploaded
fubar
parents:
diff changeset
733 def getfSize(self,fpath,outpath):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
734 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
735 format a nice file size string
d98f5a09137f Uploaded
fubar
parents:
diff changeset
736 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
737 size = ''
d98f5a09137f Uploaded
fubar
parents:
diff changeset
738 fp = os.path.join(outpath,fpath)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
739 if os.path.isfile(fp):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
740 size = '0 B'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
741 n = float(os.path.getsize(fp))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
742 if n > 2**20:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
743 size = '%1.1f MB' % (n/2**20)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
744 elif n > 2**10:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
745 size = '%1.1f KB' % (n/2**10)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
746 elif n > 0:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
747 size = '%d B' % (int(n))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
748 return size
d98f5a09137f Uploaded
fubar
parents:
diff changeset
749
d98f5a09137f Uploaded
fubar
parents:
diff changeset
750 def makeHtml(self):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
751 """ Create an HTML file content to list all the artifacts found in the output_dir
d98f5a09137f Uploaded
fubar
parents:
diff changeset
752 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
753
d98f5a09137f Uploaded
fubar
parents:
diff changeset
754 galhtmlprefix = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
d98f5a09137f Uploaded
fubar
parents:
diff changeset
755 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
d98f5a09137f Uploaded
fubar
parents:
diff changeset
756 <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
d98f5a09137f Uploaded
fubar
parents:
diff changeset
757 <meta name="generator" content="Galaxy %s tool output - see http://g2.trac.bx.psu.edu/" />
d98f5a09137f Uploaded
fubar
parents:
diff changeset
758 <title></title>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
759 <link rel="stylesheet" href="/static/style/base.css" type="text/css" />
d98f5a09137f Uploaded
fubar
parents:
diff changeset
760 </head>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
761 <body>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
762 <div class="toolFormBody">
d98f5a09137f Uploaded
fubar
parents:
diff changeset
763 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
764 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/>"""
d98f5a09137f Uploaded
fubar
parents:
diff changeset
765 galhtmlpostfix = """</div></body></html>\n"""
d98f5a09137f Uploaded
fubar
parents:
diff changeset
766
d98f5a09137f Uploaded
fubar
parents:
diff changeset
767 flist = os.listdir(self.opts.output_dir)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
768 flist = [x for x in flist if x <> 'Rplots.pdf']
d98f5a09137f Uploaded
fubar
parents:
diff changeset
769 flist.sort()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
770 html = []
d98f5a09137f Uploaded
fubar
parents:
diff changeset
771 html.append(galhtmlprefix % progname)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
772 html.append('<div class="infomessage">Galaxy Tool "%s" run at %s</div><br/>' % (self.toolname,timenow()))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
773 fhtml = []
d98f5a09137f Uploaded
fubar
parents:
diff changeset
774 if len(flist) > 0:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
775 logfiles = [x for x in flist if x.lower().endswith('.log')] # log file names determine sections
d98f5a09137f Uploaded
fubar
parents:
diff changeset
776 logfiles.sort()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
777 logfiles = [x for x in logfiles if os.path.abspath(x) <> os.path.abspath(self.tlog)]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
778 logfiles.append(os.path.abspath(self.tlog)) # make it the last one
d98f5a09137f Uploaded
fubar
parents:
diff changeset
779 pdflist = []
d98f5a09137f Uploaded
fubar
parents:
diff changeset
780 npdf = len([x for x in flist if os.path.splitext(x)[-1].lower() == '.pdf'])
d98f5a09137f Uploaded
fubar
parents:
diff changeset
781 for rownum,fname in enumerate(flist):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
782 dname,e = os.path.splitext(fname)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
783 sfsize = self.getfSize(fname,self.opts.output_dir)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
784 if e.lower() == '.pdf' : # compress and make a thumbnail
d98f5a09137f Uploaded
fubar
parents:
diff changeset
785 thumb = '%s.%s' % (dname,self.thumbformat)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
786 pdff = os.path.join(self.opts.output_dir,fname)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
787 retval = self.compressPDF(inpdf=pdff,thumbformat=self.thumbformat)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
788 if retval == 0:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
789 pdflist.append((fname,thumb))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
790 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
791 pdflist.append((fname,fname))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
792 if (rownum+1) % 2 == 0:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
793 fhtml.append('<tr class="odd_row"><td><a href="%s">%s</a></td><td>%s</td></tr>' % (fname,fname,sfsize))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
794 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
795 fhtml.append('<tr><td><a href="%s">%s</a></td><td>%s</td></tr>' % (fname,fname,sfsize))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
796 for logfname in logfiles: # expect at least tlog - if more
d98f5a09137f Uploaded
fubar
parents:
diff changeset
797 if os.path.abspath(logfname) == os.path.abspath(self.tlog): # handled later
d98f5a09137f Uploaded
fubar
parents:
diff changeset
798 sectionname = 'All tool run'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
799 if (len(logfiles) > 1):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
800 sectionname = 'Other'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
801 ourpdfs = pdflist
d98f5a09137f Uploaded
fubar
parents:
diff changeset
802 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
803 realname = os.path.basename(logfname)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
804 sectionname = os.path.splitext(realname)[0].split('_')[0] # break in case _ added to log
d98f5a09137f Uploaded
fubar
parents:
diff changeset
805 ourpdfs = [x for x in pdflist if os.path.basename(x[0]).split('_')[0] == sectionname]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
806 pdflist = [x for x in pdflist if os.path.basename(x[0]).split('_')[0] <> sectionname] # remove
d98f5a09137f Uploaded
fubar
parents:
diff changeset
807 nacross = 1
d98f5a09137f Uploaded
fubar
parents:
diff changeset
808 npdf = len(ourpdfs)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
809
d98f5a09137f Uploaded
fubar
parents:
diff changeset
810 if npdf > 0:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
811 nacross = math.sqrt(npdf) ## int(round(math.log(npdf,2)))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
812 if int(nacross)**2 != npdf:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
813 nacross += 1
d98f5a09137f Uploaded
fubar
parents:
diff changeset
814 nacross = int(nacross)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
815 width = min(400,int(1200/nacross))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
816 html.append('<div class="toolFormTitle">%s images and outputs</div>' % sectionname)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
817 html.append('(Click on a thumbnail image to download the corresponding original PDF image)<br/>')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
818 ntogo = nacross # counter for table row padding with empty cells
d98f5a09137f Uploaded
fubar
parents:
diff changeset
819 html.append('<div><table class="simple" cellpadding="2" cellspacing="2">\n<tr>')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
820 for i,paths in enumerate(ourpdfs):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
821 fname,thumb = paths
d98f5a09137f Uploaded
fubar
parents:
diff changeset
822 s= """<td><a href="%s"><img src="%s" title="Click to download a PDF of %s" hspace="5" width="%d"
d98f5a09137f Uploaded
fubar
parents:
diff changeset
823 alt="Image called %s"/></a></td>\n""" % (fname,thumb,fname,width,fname)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
824 if ((i+1) % nacross == 0):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
825 s += '</tr>\n'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
826 ntogo = 0
d98f5a09137f Uploaded
fubar
parents:
diff changeset
827 if i < (npdf - 1): # more to come
d98f5a09137f Uploaded
fubar
parents:
diff changeset
828 s += '<tr>'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
829 ntogo = nacross
d98f5a09137f Uploaded
fubar
parents:
diff changeset
830 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
831 ntogo -= 1
d98f5a09137f Uploaded
fubar
parents:
diff changeset
832 html.append(s)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
833 if html[-1].strip().endswith('</tr>'):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
834 html.append('</table></div>\n')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
835 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
836 if ntogo > 0: # pad
d98f5a09137f Uploaded
fubar
parents:
diff changeset
837 html.append('<td>&nbsp;</td>'*ntogo)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
838 html.append('</tr></table></div>\n')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
839 logt = open(logfname,'r').readlines()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
840 logtext = [x for x in logt if x.strip() > '']
d98f5a09137f Uploaded
fubar
parents:
diff changeset
841 html.append('<div class="toolFormTitle">%s log output</div>' % sectionname)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
842 if len(logtext) > 1:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
843 html.append('\n<pre>\n')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
844 html += logtext
d98f5a09137f Uploaded
fubar
parents:
diff changeset
845 html.append('\n</pre>\n')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
846 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
847 html.append('%s is empty<br/>' % logfname)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
848 if len(fhtml) > 0:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
849 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')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
850 fhtml.append('</table></div><br/>')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
851 html.append('<div class="toolFormTitle">All output files available for downloading</div>\n')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
852 html += fhtml # add all non-pdf files to the end of the display
d98f5a09137f Uploaded
fubar
parents:
diff changeset
853 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
854 html.append('<div class="warningmessagelarge">### Error - %s returned no files - please confirm that parameters are sane</div>' % self.opts.interpreter)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
855 html.append(galhtmlpostfix)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
856 htmlf = file(self.opts.output_html,'w')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
857 htmlf.write('\n'.join(html))
d98f5a09137f Uploaded
fubar
parents:
diff changeset
858 htmlf.write('\n')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
859 htmlf.close()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
860 self.html = html
d98f5a09137f Uploaded
fubar
parents:
diff changeset
861
d98f5a09137f Uploaded
fubar
parents:
diff changeset
862
d98f5a09137f Uploaded
fubar
parents:
diff changeset
863
d98f5a09137f Uploaded
fubar
parents:
diff changeset
864 def run(self):
d98f5a09137f Uploaded
fubar
parents:
diff changeset
865 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
866 Some devteam tools have this defensive stderr read so I'm keeping with the faith
d98f5a09137f Uploaded
fubar
parents:
diff changeset
867 Feel free to update.
d98f5a09137f Uploaded
fubar
parents:
diff changeset
868 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
869 if self.opts.envshpath <> 'system':
d98f5a09137f Uploaded
fubar
parents:
diff changeset
870 shell_source(self.opts.envshpath)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
871 # this only happens at tool generation - the generated tool relies on the dependencies all being set up
d98f5a09137f Uploaded
fubar
parents:
diff changeset
872 # at toolshed installation by sourcing local env.sh
d98f5a09137f Uploaded
fubar
parents:
diff changeset
873 if self.opts.output_dir:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
874 ste = open(self.elog,'wb')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
875 sto = open(self.tlog,'wb')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
876 s = ' '.join(self.cl)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
877 sto.write('## Executing Toolfactory generated command line = %s\n' % s)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
878 sto.flush()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
879 p = subprocess.Popen(self.cl,shell=False,stdout=sto,stderr=ste,cwd=self.opts.output_dir)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
880 retval = p.wait()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
881 sto.close()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
882 ste.close()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
883 tmp_stderr = open( self.elog, 'rb' )
d98f5a09137f Uploaded
fubar
parents:
diff changeset
884 err = ''
d98f5a09137f Uploaded
fubar
parents:
diff changeset
885 buffsize = 1048576
d98f5a09137f Uploaded
fubar
parents:
diff changeset
886 try:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
887 while True:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
888 err += tmp_stderr.read( buffsize )
d98f5a09137f Uploaded
fubar
parents:
diff changeset
889 if not err or len( stderr ) % buffsize != 0:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
890 break
d98f5a09137f Uploaded
fubar
parents:
diff changeset
891 except OverflowError:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
892 pass
d98f5a09137f Uploaded
fubar
parents:
diff changeset
893 tmp_stderr.close()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
894 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
895 p = subprocess.Popen(self.cl,shell=False)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
896 retval = p.wait()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
897 if self.opts.output_dir:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
898 if retval <> 0 and err: # problem
d98f5a09137f Uploaded
fubar
parents:
diff changeset
899 print >> sys.stderr,err
d98f5a09137f Uploaded
fubar
parents:
diff changeset
900 if self.opts.make_HTML:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
901 self.makeHtml()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
902 return retval
d98f5a09137f Uploaded
fubar
parents:
diff changeset
903
d98f5a09137f Uploaded
fubar
parents:
diff changeset
904
d98f5a09137f Uploaded
fubar
parents:
diff changeset
905
d98f5a09137f Uploaded
fubar
parents:
diff changeset
906 def main():
d98f5a09137f Uploaded
fubar
parents:
diff changeset
907 u = """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
908 This is a Galaxy wrapper. It expects to be called by a special purpose tool.xml as:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
909 <command interpreter="python">rgBaseScriptWrapper.py --script_path "$scriptPath" --tool_name "foo" --interpreter "Rscript"
d98f5a09137f Uploaded
fubar
parents:
diff changeset
910 </command>
d98f5a09137f Uploaded
fubar
parents:
diff changeset
911 """
d98f5a09137f Uploaded
fubar
parents:
diff changeset
912 op = optparse.OptionParser()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
913 a = op.add_option
d98f5a09137f Uploaded
fubar
parents:
diff changeset
914 a('--script_path',default=None)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
915 a('--tool_name',default=None)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
916 a('--interpreter',default=None)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
917 a('--output_dir',default='./')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
918 a('--output_html',default=None)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
919 a('--input_tab',default=[], action="append") # these are "galaxypath,metadataname" pairs
d98f5a09137f Uploaded
fubar
parents:
diff changeset
920 a("--input_formats",default="tabular")
d98f5a09137f Uploaded
fubar
parents:
diff changeset
921 a('--output_tab',default=None)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
922 a('--output_format',default='tabular')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
923 a('--user_email',default='Unknown')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
924 a('--bad_user',default=None)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
925 a('--make_Tool',default=None)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
926 a('--make_HTML',default=None)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
927 a('--help_text',default=None)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
928 a('--tool_desc',default=None)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
929 a('--new_tool',default=None)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
930 a('--tool_version',default=None)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
931 a('--include_dependencies',default="yes")
d98f5a09137f Uploaded
fubar
parents:
diff changeset
932 a('--citations',default=None)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
933 a('--additional_parameters', dest='additional_parameters', action='append', default=[])
d98f5a09137f Uploaded
fubar
parents:
diff changeset
934 a('--edit_additional_parameters', action="store_true", default=False)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
935 a('--envshpath',default="system")
d98f5a09137f Uploaded
fubar
parents:
diff changeset
936 opts, args = op.parse_args()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
937 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)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
938 assert opts.tool_name,'## Tool Factory expects a tool name - eg --tool_name=DESeq'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
939 assert opts.interpreter,'## Tool Factory wrapper expects an interpreter - eg --interpreter=Rscript'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
940 assert os.path.isfile(opts.script_path),'## Tool Factory wrapper expects a script path - eg --script_path=foo.R'
d98f5a09137f Uploaded
fubar
parents:
diff changeset
941 if opts.output_dir:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
942 try:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
943 os.makedirs(opts.output_dir)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
944 except:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
945 pass
d98f5a09137f Uploaded
fubar
parents:
diff changeset
946 opts.input_tab = [x.replace('"','').replace("'",'') for x in opts.input_tab]
d98f5a09137f Uploaded
fubar
parents:
diff changeset
947 for i,x in enumerate(opts.additional_parameters): # remove quotes we need to deal with spaces in CL params
d98f5a09137f Uploaded
fubar
parents:
diff changeset
948 opts.additional_parameters[i] = opts.additional_parameters[i].replace('"','')
d98f5a09137f Uploaded
fubar
parents:
diff changeset
949 r = ScriptRunner(opts)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
950 if opts.make_Tool:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
951 retcode = r.makeTooltar()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
952 else:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
953 retcode = r.run()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
954 os.unlink(r.sfile)
d98f5a09137f Uploaded
fubar
parents:
diff changeset
955 if retcode:
d98f5a09137f Uploaded
fubar
parents:
diff changeset
956 sys.exit(retcode) # indicate failure to job runner
d98f5a09137f Uploaded
fubar
parents:
diff changeset
957
d98f5a09137f Uploaded
fubar
parents:
diff changeset
958
d98f5a09137f Uploaded
fubar
parents:
diff changeset
959 if __name__ == "__main__":
d98f5a09137f Uploaded
fubar
parents:
diff changeset
960 main()
d98f5a09137f Uploaded
fubar
parents:
diff changeset
961
d98f5a09137f Uploaded
fubar
parents:
diff changeset
962