annotate rgToolFactory.py @ 39:64f7bd086146 draft

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