annotate teste_out/teste_out.py @ 0:a3d3dc004a88 draft default tip

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