annotate rgToolFactory2.py @ 13:00777b83aaca draft

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