annotate fubar-galaxytoolfactory-f726a966e35d/rgToolFactory.py @ 20:b9892d7f0983 draft default tip

USE MAIN TOOLSHED PLEASE I keep forgetting to update this one
author fubar
date Fri, 08 Mar 2013 01:50:14 -0500
parents f1dae673ec8b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
1 # rgToolFactory.py
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
2 # see https://bitbucket.org/fubar/galaxytoolfactory/wiki/Home
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
3 #
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
4 # copyright ross lazarus (ross stop lazarus at gmail stop com) May 2012
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
5 #
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
6 # all rights reserved
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
7 # Licensed under the LGPL
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
8 # suggestions for improvement and bug fixes welcome at https://bitbucket.org/fubar/galaxytoolfactory/wiki/Home
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
9
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
10 # August 11 2012
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
11 # changed to use shell=False and cl as a sequence
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
12
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
13 # This is a Galaxy tool factory for simple scripts in python, R or whatever ails ye.
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
14 # It also serves as the wrapper for the new tool.
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
15 #
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
16 # you paste and run your script
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
17 # Only works for simple scripts that read one input from the history.
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
18 # Optionally can write one new history dataset,
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
19 # and optionally collect any number of outputs into links on an autogenerated HTML page.
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
20
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
21 # DO NOT install on a public or important site - please.
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
22
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
23 # installed generated tools are fine if the script is safe.
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
24 # They just run normally and their user cannot do anything unusually insecure
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
25 # but please, practice safe toolshed.
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
26 # Read the fucking code before you install any tool
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
27 # especially this one
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
28
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
29 # After you get the script working on some test data, you can
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
30 # optionally generate a toolshed compatible gzip file
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
31 # containing your script safely wrapped as an ordinary Galaxy script in your local toolshed for
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
32 # safe and largely automated installation in a production Galaxy.
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
33
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
34 # If you opt for an HTML output, you get all the script outputs arranged
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
35 # as a single Html history item - all output files are linked, thumbnails for all the pdfs.
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
36 # Ugly but really inexpensive.
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
37 #
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
38 # Patches appreciated please.
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
39 #
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
40 #
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
41 # long route to June 2012 product
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
42 # Behold the awesome power of Galaxy and the toolshed with the tool factory binds to bind them
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
43 # derived from an integrated script model
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
44 # called rgBaseScriptWrapper.py
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
45 # Note to the unwary:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
46 # This tool allows arbitrary scripting on your Galaxy as the Galaxy user
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
47 # There is nothing stopping a malicious user doing whatever they choose
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
48 # Extremely dangerous!!
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
49 # Totally insecure. So, trusted users only
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
50 #
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
51 # preferred model is a developer using their throw away workstation instance - ie a private site.
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
52 # no real risk. The universe_wsgi.ini admin_users string is checked - only admin users are permitted to run this tool.
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
53 #
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
54
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
55 import sys
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
56 import shutil
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
57 import subprocess
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
58 import os
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
59 import time
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
60 import tempfile
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
61 import optparse
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
62 import tarfile
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
63 import re
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
64 import shutil
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
65 import math
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
66
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
67 progname = os.path.split(sys.argv[0])[1]
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
68 myversion = 'V000.2 June 2012'
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
69 verbose = False
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
70 debug = False
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
71 toolFactoryURL = 'https://bitbucket.org/fubar/galaxytoolfactory'
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
72
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
73 def timenow():
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
74 """return current time as a string
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
75 """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
76 return time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(time.time()))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
77
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
78
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
79 class ScriptRunner:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
80 """class is a wrapper for an arbitrary script
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
81 """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
82
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
83 def __init__(self,opts=None,treatbashSpecial=True):
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
84 """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
85 cleanup inputs, setup some outputs
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
86
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
87 """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
88 self.treatbashSpecial = treatbashSpecial
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
89 if opts.output_dir: # simplify for the tool tarball
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
90 os.chdir(opts.output_dir)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
91 self.thumbformat = 'jpg'
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
92 self.opts = opts
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
93 self.toolname = re.sub('[^a-zA-Z0-9_]+', '', opts.tool_name) # a sanitizer now does this but..
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
94 self.toolid = self.toolname
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
95 self.myname = sys.argv[0] # get our name because we write ourselves out as a tool later
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
96 self.pyfile = self.myname # crude but efficient - the cruft won't hurt much
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
97 self.xmlfile = '%s.xml' % self.toolname
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
98 s = open(self.opts.script_path,'r').readlines()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
99 s = [x.rstrip() for x in s] # remove pesky dos line endings if needed
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
100 self.script = '\n'.join(s)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
101 fhandle,self.sfile = tempfile.mkstemp(prefix=self.toolname,suffix=".%s" % (opts.interpreter))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
102 tscript = open(self.sfile,'w') # use self.sfile as script source for Popen
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
103 tscript.write(self.script)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
104 tscript.close()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
105 self.indentedScript = ''.join([' %s' % x for x in s]) # for restructured text in help
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
106 if opts.output_dir: # may not want these complexities
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
107 self.tlog = os.path.join(opts.output_dir,"%s_runner.log" % self.toolname)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
108 art = '%s.%s' % (self.toolname,opts.interpreter)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
109 artpath = os.path.join(self.opts.output_dir,art) # need full path
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
110 artifact = open(artpath,'w') # use self.sfile as script source for Popen
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
111 artifact.write(self.script)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
112 artifact.close()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
113 self.cl = []
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
114 self.html = []
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
115 a = self.cl.append
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
116 a(opts.interpreter)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
117 if self.treatbashSpecial and opts.interpreter in ['bash','sh']:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
118 a(self.sfile)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
119 else:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
120 a('-') # stdin
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
121 a(opts.input_tab)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
122 a(opts.output_tab)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
123 self.outFormats = 'tabular' # TODO make this an option at tool generation time
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
124 self.inputFormats = 'tabular' # TODO make this an option at tool generation time
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
125 self.test1Input = '%s_test1_input.xls' % self.toolname
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
126 self.test1Output = '%s_test1_output.xls' % self.toolname
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
127 self.test1HTML = '%s_test1_output.html' % self.toolname
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
128
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
129 def makeXML(self):
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
130 """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
131 Create a Galaxy xml tool wrapper for the new script as a string to write out
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
132 fixme - use templating or something less fugly than this example of what we produce
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
133
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
134 <tool id="reverse" name="reverse" version="0.01">
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
135 <description>a tabular file</description>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
136 <command interpreter="python">
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
137 reverse.py --script_path "$runMe" --interpreter "python"
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
138 --tool_name "reverse" --input_tab "$input1" --output_tab "$tab_file"
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
139 </command>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
140 <inputs>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
141 <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"/>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
142
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
143 </inputs>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
144 <outputs>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
145 <data format="tabular" name="tab_file" label="${job_name}"/>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
146
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
147 </outputs>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
148 <help>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
149
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
150 **What it Does**
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
151
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
152 Reverse the columns in a tabular file
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
153
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
154 </help>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
155 <configfiles>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
156 <configfile name="runMe">
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
157
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
158 # reverse order of columns in a tabular file
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
159 import sys
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
160 inp = sys.argv[1]
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
161 outp = sys.argv[2]
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
162 i = open(inp,'r')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
163 o = open(outp,'w')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
164 for row in i:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
165 rs = row.rstrip().split('\t')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
166 rs.reverse()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
167 o.write('\t'.join(rs))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
168 o.write('\n')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
169 i.close()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
170 o.close()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
171
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
172
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
173 </configfile>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
174 </configfiles>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
175 </tool>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
176
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
177 """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
178 newXML="""<tool id="%(toolid)s" name="%(toolname)s" version="%(tool_version)s">
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
179 %(tooldesc)s
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
180 %(command)s
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
181 <inputs>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
182 %(inputs)s
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
183 </inputs>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
184 <outputs>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
185 %(outputs)s
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
186 </outputs>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
187 <configfiles>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
188 <configfile name="runMe">
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
189 %(script)s
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
190 </configfile>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
191 </configfiles>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
192 %(tooltests)s
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
193 <help>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
194 %(help)s
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
195 </help>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
196 </tool>""" # needs a dict with toolname, toolid, interpreter, scriptname, command, inputs as a multi line string ready to write, outputs ditto, help ditto
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
197
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
198 newCommand="""<command interpreter="python">
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
199 %(toolname)s.py --script_path "$runMe" --interpreter "%(interpreter)s"
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
200 --tool_name "%(toolname)s" %(command_inputs)s %(command_outputs)s
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
201 </command>""" # may NOT be an input or htmlout
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
202 tooltestsTabOnly = """<tests><test>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
203 <param name="input1" value="%(test1Input)s" ftype="tabular"/>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
204 <param name="job_name" value="test1"/>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
205 <param name="runMe" value="$runMe"/>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
206 <output name="tab_file" file="%(test1Output)s" ftype="tabular"/>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
207 </test></tests>"""
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
208 tooltestsHTMLOnly = """<tests><test>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
209 <param name="input1" value="%(test1Input)s" ftype="tabular"/>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
210 <param name="job_name" value="test1"/>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
211 <param name="runMe" value="$runMe"/>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
212 <output name="html_file" file="%(test1HTML)s" ftype="html" lines_diff="5"/>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
213 </test></tests>"""
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
214 tooltestsBoth = """<tests><test>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
215 <param name="input1" value="%(test1Input)s" ftype="tabular"/>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
216 <param name="job_name" value="test1"/>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
217 <param name="runMe" value="$runMe"/>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
218 <output name="tab_file" file="%(test1Output)s" ftype="tabular" />
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
219 <output name="html_file" file="%(test1HTML)s" ftype="html" lines_diff="10"/>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
220 </test></tests>"""
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
221 xdict = {}
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
222 xdict['tool_version'] = self.opts.tool_version
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
223 xdict['test1Input'] = self.test1Input
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
224 xdict['test1HTML'] = self.test1HTML
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
225 xdict['test1Output'] = self.test1Output
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
226 if self.opts.make_HTML and self.opts.output_tab <> 'None':
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
227 xdict['tooltests'] = tooltestsBoth % xdict
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
228 elif self.opts.make_HTML:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
229 xdict['tooltests'] = tooltestsHTMLOnly % xdict
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
230 else:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
231 xdict['tooltests'] = tooltestsTabOnly % xdict
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
232 xdict['script'] = self.script # configfile is least painful way to embed script to avoid external dependencies
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
233 if self.opts.help_text:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
234 xdict['help'] = open(self.opts.help_text,'r').read()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
235 else:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
236 xdict['help'] = 'Please ask the tool author for help as none was supplied at tool generation'
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
237 coda = ['**Script**','Pressing execute will run the following code over your input file and generate some outputs in your history::']
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
238 coda.append(self.indentedScript)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
239 coda.append('**Attribution** This Galaxy tool was created by %s at %s\nusing the Galaxy Tool Factory.' % (self.opts.user_email,timenow()))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
240 coda.append('See %s for details of that project' % (toolFactoryURL))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
241 xdict['help'] = '%s\n%s' % (xdict['help'],'\n'.join(coda))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
242 if self.opts.tool_desc:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
243 xdict['tooldesc'] = '<description>%s</description>' % self.opts.tool_desc
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
244 else:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
245 xdict['tooldesc'] = ''
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
246 xdict['command_outputs'] = ''
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
247 xdict['outputs'] = ''
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
248 if self.opts.input_tab <> 'None':
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
249 xdict['command_inputs'] = '--input_tab "$input1" ' # the space may matter a lot if we append something
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
250 xdict['inputs'] = '<param name="input1" type="data" format="%s" label="Select a suitable input file from your history"/> \n' % self.inputFormats
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
251 else:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
252 xdict['command_inputs'] = '' # assume no input - eg a random data generator
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
253 xdict['inputs'] = ''
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
254 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
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
255 xdict['toolname'] = self.toolname
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
256 xdict['toolid'] = self.toolid
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
257 xdict['interpreter'] = self.opts.interpreter
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
258 xdict['scriptname'] = self.sfile
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
259 if self.opts.make_HTML:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
260 xdict['command_outputs'] += ' --output_dir "$html_file.files_path" --output_html "$html_file" --make_HTML "yes" '
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
261 xdict['outputs'] += ' <data format="html" name="html_file" label="${job_name}.html"/>\n'
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
262 if self.opts.output_tab <> 'None':
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
263 xdict['command_outputs'] += ' --output_tab "$tab_file"'
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
264 xdict['outputs'] += ' <data format="%s" name="tab_file" label="${job_name}"/>\n' % self.outFormats
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
265 xdict['command'] = newCommand % xdict
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
266 xmls = newXML % xdict
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
267 xf = open(self.xmlfile,'w')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
268 xf.write(xmls)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
269 xf.write('\n')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
270 xf.close()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
271 # ready for the tarball
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
272
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
273
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
274 def makeTooltar(self):
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
275 """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
276 a tool is a gz tarball with eg
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
277 /toolname/tool.xml /toolname/tool.py /toolname/test-data/test1_in.foo ...
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
278 """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
279 retval = self.run()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
280 if retval:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
281 print >> sys.stderr,'## Run failed. Cannot build yet. Please fix and retry'
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
282 sys.exit(1)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
283 self.makeXML()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
284 tdir = self.toolname
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
285 os.mkdir(tdir)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
286 if self.opts.input_tab <> 'None': # no reproducible test otherwise? TODO: maybe..
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
287 testdir = os.path.join(tdir,'test-data')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
288 os.mkdir(testdir) # make tests directory
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
289 shutil.copyfile(self.opts.input_tab,os.path.join(testdir,self.test1Input))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
290 if self.opts.output_tab <> 'None':
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
291 shutil.copyfile(self.opts.output_tab,os.path.join(testdir,self.test1Output))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
292 if self.opts.make_HTML:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
293 shutil.copyfile(self.opts.output_html,os.path.join(testdir,self.test1HTML))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
294 if self.opts.output_dir:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
295 shutil.copyfile(self.tlog,os.path.join(testdir,'test1_out.log'))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
296 op = '%s.py' % self.toolname # new name
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
297 outpiname = os.path.join(tdir,op) # path for the tool tarball
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
298 pyin = os.path.basename(self.pyfile) # our name - we rewrite ourselves (TM)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
299 notes = ['# %s - a self annotated version of %s generated by running %s\n' % (op,pyin,pyin),]
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
300 notes.append('# to make a new Galaxy tool called %s\n' % self.toolname)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
301 notes.append('# User %s at %s\n' % (self.opts.user_email,timenow()))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
302 pi = open(self.pyfile,'r').readlines() # our code becomes new tool wrapper (!) - first Galaxy worm
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
303 notes += pi
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
304 outpi = open(outpiname,'w')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
305 outpi.write(''.join(notes))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
306 outpi.write('\n')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
307 outpi.close()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
308 stname = os.path.join(tdir,self.sfile)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
309 if not os.path.exists(stname):
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
310 shutil.copyfile(self.sfile, stname)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
311 xtname = os.path.join(tdir,self.xmlfile)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
312 if not os.path.exists(xtname):
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
313 shutil.copyfile(self.xmlfile,xtname)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
314 tarpath = "%s.gz" % self.toolname
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
315 tar = tarfile.open(tarpath, "w:gz")
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
316 tar.add(tdir,arcname=self.toolname)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
317 tar.close()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
318 shutil.copyfile(tarpath,self.opts.new_tool)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
319 shutil.rmtree(tdir)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
320 ## TODO: replace with optional direct upload to local toolshed?
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
321 return retval
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
322
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
323 def compressPDF(self,inpdf=None,thumbformat='png'):
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
324 """need absolute path to pdf
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
325 """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
326 assert os.path.isfile(inpdf), "## Input %s supplied to %s compressPDF not found" % (inpdf,self.myName)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
327 hf,hlog = tempfile.mkstemp(suffix="%s.log" % self.toolname)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
328 sto = open(hlog,'w')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
329 outpdf = '%s_compressed' % inpdf
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
330 cl = ["gs", "-sDEVICE=pdfwrite", "-dNOPAUSE", "-dBATCH", "-sOutputFile=%s" % outpdf,inpdf]
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
331 x = subprocess.Popen(cl,stdout=sto,stderr=sto,cwd=self.opts.output_dir)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
332 retval1 = x.wait()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
333 if retval1 == 0:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
334 os.unlink(inpdf)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
335 shutil.move(outpdf,inpdf)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
336 outpng = '%s.%s' % (os.path.splitext(inpdf)[0],thumbformat)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
337 cl2 = ['convert', inpdf, outpng]
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
338 x = subprocess.Popen(cl2,stdout=sto,stderr=sto,cwd=self.opts.output_dir)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
339 retval2 = x.wait()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
340 sto.close()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
341 retval = retval1 or retval2
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
342 return retval
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
343
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
344
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
345 def getfSize(self,fpath,outpath):
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
346 """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
347 format a nice file size string
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
348 """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
349 size = ''
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
350 fp = os.path.join(outpath,fpath)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
351 if os.path.isfile(fp):
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
352 size = '0 B'
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
353 n = float(os.path.getsize(fp))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
354 if n > 2**20:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
355 size = '%1.1f MB' % (n/2**20)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
356 elif n > 2**10:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
357 size = '%1.1f KB)' % (n/2**10)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
358 elif n > 0:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
359 size = '%d B' % (int(n))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
360 return size
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
361
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
362 def makeHtml(self):
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
363 """ Create an HTML file content to list all the artifacts found in the output_dir
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
364 """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
365
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
366 galhtmlprefix = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
367 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
368 <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
369 <meta name="generator" content="Galaxy %s tool output - see http://g2.trac.bx.psu.edu/" />
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
370 <title></title>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
371 <link rel="stylesheet" href="/static/style/base.css" type="text/css" />
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
372 </head>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
373 <body>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
374 <div class="toolFormBody">
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
375 """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
376 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/>"""
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
377 galhtmlpostfix = """</div></body></html>\n"""
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
378
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
379 flist = os.listdir(self.opts.output_dir)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
380 flist = [x for x in flist if x <> 'Rplots.pdf']
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
381 flist.sort()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
382 html = []
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
383 html.append(galhtmlprefix % progname)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
384 html.append('<div class="infomessage">Galaxy Tool "%s" run at %s</div><br/>' % (self.toolname,timenow()))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
385 fhtml = []
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
386 if len(flist) > 0:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
387 pdflist = []
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
388 npdf = len([x for x in flist if os.path.splitext(x)[-1].lower() == '.pdf'])
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
389 nacross = 1
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
390 if npdf > 0:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
391 nacross = int(round(math.log(npdf,2)))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
392 nacross = max(1,nacross)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
393 width = min(400,int(1200/nacross))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
394 for rownum,fname in enumerate(flist):
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
395 dname,e = os.path.splitext(fname)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
396 sfsize = self.getfSize(fname,self.opts.output_dir)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
397 if e.lower() == '.pdf' : # compress and make a thumbnail
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
398 thumb = '%s.%s' % (dname,self.thumbformat)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
399 pdff = os.path.join(self.opts.output_dir,fname)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
400 retval = self.compressPDF(inpdf=pdff,thumbformat=self.thumbformat)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
401 if retval == 0:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
402 pdflist.append((fname,thumb))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
403 if (rownum+1) % 2 == 0:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
404 fhtml.append('<tr class="odd_row"><td><a href="%s">%s</a></td><td>%s</td></tr>' % (fname,fname,sfsize))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
405 else:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
406 fhtml.append('<tr><td><a href="%s">%s</a></td><td>%s</td></tr>' % (fname,fname,sfsize))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
407 ntogo = nacross # counter for table row padding with empty cells
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
408 if len(pdflist) > 0:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
409 html.append('<div><table class="simple" cellpadding="2" cellspacing="2">\n<tr>')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
410 for i,paths in enumerate(pdflist):
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
411 fname,thumb = paths
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
412 s= """<td><a href="%s"><img src="%s" title="Click to download a PDF of %s" hspace="5" width="%d"
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
413 alt="Image called %s"/></a></td>\n""" % (fname,thumb,fname,width,fname)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
414 if ((i+1) % nacross == 0):
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
415 s += '</tr>\n'
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
416 ntogo = 0
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
417 if i < (npdf - 1): # more to come
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
418 s += '<tr>'
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
419 ntogo = nacross
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
420 else:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
421 ntogo -= 1
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
422 html.append(s)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
423 if html[-1].strip().endswith('</tr>'):
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
424 html.append('</table></div>\n')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
425 else:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
426 if ntogo > 0: # pad
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
427 html.append('<td>&nbsp;</td>'*ntogo)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
428 html.append('</tr></table></div>\n')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
429 if len(fhtml) > 0:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
430 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')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
431 fhtml.append('</table></div><br/>')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
432 html += fhtml # add all non-pdf files to the end of the display
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
433 else:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
434 html.append('<div class="warningmessagelarge">### Error - %s returned no files - please confirm that parameters are sane</div>' % self.opts.interpreter)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
435 rlog = open(self.tlog,'r').readlines()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
436 rlog = [x for x in rlog if x.strip() > '']
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
437 if len(rlog) > 1:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
438 html.append('<div class="toolFormTitle">%s log</div><pre>\n' % self.opts.interpreter)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
439 html += rlog
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
440 html.append('</pre>\n')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
441 html.append(galhtmlattr % (self.toolname))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
442 html.append(galhtmlpostfix)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
443 htmlf = file(self.opts.output_html,'w')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
444 htmlf.write('\n'.join(html))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
445 htmlf.write('\n')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
446 htmlf.close()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
447 self.html = html
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
448
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
449
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
450 def run(self):
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
451 """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
452 scripts must be small enough not to fill the pipe!
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
453 """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
454 if self.treatbashSpecial and self.opts.interpreter in ['bash','sh']:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
455 retval = self.runBash()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
456 else:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
457 if self.opts.output_dir:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
458 sto = open(self.tlog,'w')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
459 sto.write('## Toolfactory generated command line = %s\n' % ' '.join(self.cl))
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
460 sto.flush()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
461 p = subprocess.Popen(self.cl,shell=False,stdout=sto,stderr=sto,stdin=subprocess.PIPE,cwd=self.opts.output_dir)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
462 else:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
463 p = subprocess.Popen(self.cl,shell=False,stdin=subprocess.PIPE)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
464 p.stdin.write(self.script)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
465 p.stdin.close()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
466 retval = p.wait()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
467 if self.opts.output_dir:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
468 sto.close()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
469 if self.opts.make_HTML:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
470 self.makeHtml()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
471 return retval
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
472
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
473 def runBash(self):
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
474 """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
475 cannot use - for bash so use self.sfile
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
476 """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
477 if self.opts.output_dir:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
478 s = '## Toolfactory generated command line = %s\n' % ' '.join(self.cl)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
479 sto = open(self.tlog,'w')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
480 sto.write(s)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
481 sto.flush()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
482 p = subprocess.Popen(self.cl,shell=False,stdout=sto,stderr=sto,cwd=self.opts.output_dir)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
483 else:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
484 p = subprocess.Popen(self.cl,shell=False)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
485 retval = p.wait()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
486 if self.opts.output_dir:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
487 sto.close()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
488 if self.opts.make_HTML:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
489 self.makeHtml()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
490 return retval
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
491
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
492
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
493 def main():
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
494 u = """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
495 This is a Galaxy wrapper. It expects to be called by a special purpose tool.xml as:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
496 <command interpreter="python">rgBaseScriptWrapper.py --script_path "$scriptPath" --tool_name "foo" --interpreter "Rscript"
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
497 </command>
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
498 """
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
499 op = optparse.OptionParser()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
500 a = op.add_option
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
501 a('--script_path',default=None)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
502 a('--tool_name',default=None)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
503 a('--interpreter',default=None)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
504 a('--output_dir',default=None)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
505 a('--output_html',default=None)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
506 a('--input_tab',default="None")
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
507 a('--output_tab',default="None")
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
508 a('--user_email',default='Unknown')
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
509 a('--bad_user',default=None)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
510 a('--make_Tool',default=None)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
511 a('--make_HTML',default=None)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
512 a('--help_text',default=None)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
513 a('--tool_desc',default=None)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
514 a('--new_tool',default=None)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
515 a('--tool_version',default=None)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
516 opts, args = op.parse_args()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
517 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)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
518 assert opts.tool_name,'## Tool Factory expects a tool name - eg --tool_name=DESeq'
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
519 assert opts.interpreter,'## Tool Factory wrapper expects an interpreter - eg --interpreter=Rscript'
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
520 assert os.path.isfile(opts.script_path),'## Tool Factory wrapper expects a script path - eg --script_path=foo.R'
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
521 if opts.output_dir:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
522 try:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
523 os.makedirs(opts.output_dir)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
524 except:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
525 pass
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
526 r = ScriptRunner(opts)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
527 if opts.make_Tool:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
528 retcode = r.makeTooltar()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
529 else:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
530 retcode = r.run()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
531 os.unlink(r.sfile)
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
532 if retcode:
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
533 sys.exit(retcode) # indicate failure to job runner
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
534
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
535
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
536 if __name__ == "__main__":
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
537 main()
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
538
f1dae673ec8b Uploaded corrected version as 0.9 to trigger updates for those with the older version
fubar
parents:
diff changeset
539