annotate fubar-galaxytoolfactory-8cc3f3c0e539/rgToolFactory.py @ 3:5dbaf051b2e7 draft

Uploaded
author fubar
date Wed, 06 Jun 2012 23:06:32 -0400
parents f82dd90a8c9e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
1 # rgToolFactory.py
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
2 # https://bitbucket.org/fubar/galaxytoolfactory/wiki/Home
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
3
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
4 # this is a tool factory for simple scripts in python, R or whatever ails ye.
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
5 # you paste and run your script
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
6 # Only works for simple scripts that read one input from the history.
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
7 # Optionally can write one new history dataset,
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
8 # and optionally collect any number of outputs into links on an autogenerated HTML page.
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
9
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
10 # DO NOT install on a public or important site - please.
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
11
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
12 # installed generated tools are fine if the script is safe.
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
13 # They just run normally and their user cannot do anything unusually insecure
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
14 # but please, practice safe toolshed.
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
15 # Read the fucking code before you install any tool
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
16 # especially this one
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
17
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
18 # After you get the script working on some test data, you can
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
19 # optionally generate a toolshed compatible gzip file
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
20 # containing your script safely wrapped as an ordinary Galaxy script in your local toolshed for
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
21 # safe and largely automated installation in a production Galaxy.
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
22
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
23 # If you opt for an HTML output, you get all the script outputs arranged
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
24 # as a single Html history item - all output files are linked, thumbnails for all the pdfs.
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
25 # Ugly but really inexpensive.
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
26 #
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
27 # Patches appreciated please.
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
28 #
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
29 #
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
30 # long route to June 2012 product
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
31 # Behold the awesome power of Galaxy and the toolshed with the tool factory binds to bind them
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
32 # derived from an integrated script model
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
33 # called rgBaseScriptWrapper.py
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
34 # Note to the unwary:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
35 # This tool allows arbitrary scripting on your Galaxy as the Galaxy user
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
36 # There is nothing stopping a malicious user doing whatever they choose
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
37 # Extremely dangerous!!
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
38 # Totally insecure. So, trusted users only
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
39 #
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
40 # preferred model is a developer using their throw away workstation instance - ie a private site.
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
41 # no real risk. The universe_wsgi.ini admin_users string is checked - only admin users are permitted to run this tool.
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
42 #
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
43
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
44 #
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
45 # copyright ross lazarus (ross stop lazarus at gmail stop com) May 2012
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
46 #
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
47 # all rights reserved
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
48 # Licensed under the LGPL if you want to improve it, feel free https://bitbucket.org/fubar/galaxytoolfactory/wiki/Home
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
49
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
50 import sys
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
51 import shutil
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
52 import subprocess
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
53 import os
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
54 import time
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
55 import tempfile
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
56 import optparse
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
57 import tarfile
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
58 import re
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
59 import shutil
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
60
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
61 progname = os.path.split(sys.argv[0])[1]
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
62 myversion = 'V000.1 May 2012'
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
63 verbose = False
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
64 debug = False
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
65
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
66
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
67 def timenow():
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
68 """return current time as a string
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
69 """
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
70 return time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(time.time()))
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
71 # characters that are allowed but need to be escaped
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
72
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
73 class ScriptRunner:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
74 """class is a wrapper for an arbitrary script
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
75 """
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
76
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
77 def __init__(self,opts=None):
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
78 """
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
79 cleanup inputs, setup some outputs
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
80
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
81 """
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
82 if opts.output_dir: # simplify for the tool tarball
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
83 os.chdir(opts.output_dir)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
84 self.thumbformat = 'jpg'
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
85 self.opts = opts
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
86 self.toolname = re.sub('[^a-zA-Z0-9_]+', '', opts.tool_name)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
87 self.toolid = self.toolname
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
88 s = open(self.opts.script_path,'r').read()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
89 self.script = s
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
90 self.myname = sys.argv[0] # get our name because we write ourselves out as a tool later
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
91 self.pyfile = self.myname # crude but efficient - the cruft won't hurt much
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
92 self.xmlfile = '%s.xml' % self.toolname
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
93 self.sfile = '%s.%s' % (self.toolname,opts.interpreter)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
94 if opts.output_dir: # may not want these complexities
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
95 self.tlog = os.path.join(opts.output_dir,"%s_runner.log" % self.toolname)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
96 artifactpath = os.path.join(opts.output_dir,'%s_run.script' % self.toolname)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
97 artifact = open(artifactpath,'w')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
98 artifact.write(self.script)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
99 artifact.write('\n')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
100 artifact.close()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
101 if opts.make_Tool: # need this code and the user script for the tarball
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
102 localscript = open(self.sfile,'w')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
103 localscript.write(self.script)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
104 localscript.close()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
105 shutil.copyfile(self.myname,'%s.py' % self.toolname) # for tool and for user
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
106 self.cl = []
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
107 self.html = []
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
108 a = self.cl.append
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
109 a(opts.interpreter)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
110 a('-') # use stdin
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
111 a(opts.input_tab)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
112 a(opts.output_tab)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
113 self.outFormats = 'tabular' # TODO make this an option at tool generation time
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
114 self.inputFormats = 'tabular' # TODO make this an option at tool generation time
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
115 self.test1Input = 'test1_input.xls'
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
116 self.test1Output = 'test1_output.xls'
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
117 self.test1HTML = 'test1_output.html'
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
118
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
119 def makeXML(self):
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
120 """
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
121 Create a Galaxy xml tool wrapper for the new script as a string to write out
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
122 fixme - use templating or something less fugly than this example of what we produce
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
123
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
124 <tool id="reverse" name="reverse" version="0.01">
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
125 <description>a tabular file</description>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
126 <command interpreter="python">
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
127 reverse.py --script_path "$runMe" --interpreter "python"
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
128 --tool_name "reverse" --input_tab "$input1" --output_tab "$tab_file"
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
129 </command>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
130 <inputs>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
131 <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"/>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
132
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
133 </inputs>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
134 <outputs>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
135 <data format="tabular" name="tab_file" label="${job_name}"/>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
136
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
137 </outputs>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
138 <help>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
139
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
140 **What it Does**
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
141
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
142 Reverse the columns in a tabular file
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
143
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
144 </help>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
145 <configfiles>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
146 <configfile name="runMe">
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
147
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
148 # reverse order of columns in a tabular file
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
149 import sys
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
150 inp = sys.argv[1]
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
151 outp = sys.argv[2]
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
152 i = open(inp,'r')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
153 o = open(outp,'w')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
154 for row in i:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
155 rs = row.rstrip().split('\t')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
156 rs.reverse()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
157 o.write('\t'.join(rs))
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
158 o.write('\n')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
159 i.close()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
160 o.close()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
161
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
162
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
163 </configfile>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
164 </configfiles>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
165 </tool>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
166
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
167 """
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
168 newXML="""<tool id="%(toolid)s" name="%(toolname)s" version="0.01">
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
169 %(tooldesc)s
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
170 %(command)s
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
171 <inputs>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
172 %(inputs)s
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
173 </inputs>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
174 <outputs>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
175 %(outputs)s
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
176 </outputs>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
177 <help>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
178 %(help)s
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
179 </help>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
180 <configfiles>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
181 <configfile name="runMe">
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
182 %(script)s
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
183 </configfile>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
184 </configfiles>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
185 </tool>""" # needs a dict with toolname, toolid, interpreter, scriptname, command, inputs as a multi line string ready to write, outputs ditto, help ditto
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
186
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
187 newCommand="""<command interpreter="python">
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
188 %(toolname)s.py --script_path "$runMe" --interpreter "%(interpreter)s"
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
189 --tool_name "%(toolname)s" %(command_inputs)s %(command_outputs)s
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
190 </command>""" # may NOT be an input or htmlout
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
191 tooltests = """<tests><test>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
192 <param name="input1" value="%s" ftype="%s"/>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
193 <param name="job_name" value="test1"/>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
194 <param name="runMe" value="$runMe"/>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
195 </test><tests>"""
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
196 xdict = {}
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
197 xdict['script'] = self.script # configfile is least painful way to embed script to avoid external dependencies
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
198 if self.opts.help_text:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
199 xdict['help'] = open(self.opts.help_text,'r').read()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
200 else:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
201 xdict['help'] = 'Please ask the tool author for help as none was supplied at tool generation'
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
202 if self.opts.tool_desc:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
203 xdict['tooldesc'] = '<description>%s</description>' % self.opts.tool_desc
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
204 else:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
205 xdict['tooldesc'] = ''
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
206 xdict['command_outputs'] = ''
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
207 xdict['outputs'] = ''
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
208 if self.opts.input_tab <> 'None':
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
209 xdict['command_inputs'] = '--input_tab "$input1"'
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
210 xdict['inputs'] = '<param name="input1" type="data" format="%s" label="Select a suitable input file from your history"/>' % self.inputFormats
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
211 else:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
212 xdict['command_inputs'] = '' # assume no input - eg a random data generator
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
213 xdict['inputs'] = ''
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
214 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
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
215 xdict['toolname'] = self.toolname
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
216 xdict['toolid'] = self.toolid
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
217 xdict['interpreter'] = self.opts.interpreter
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
218 xdict['scriptname'] = self.sfile
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
219 if self.opts.make_HTML:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
220 xdict['command_outputs'] += '--output_dir "$html_file.files_path" --output_html "$html_file" --make_HTML "yes"'
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
221 xdict['outputs'] += '<data format="html" name="html_file" label="${job_name}.html"/>\n'
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
222 if self.opts.output_tab <> 'None':
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
223 xdict['command_outputs'] += '--output_tab "$tab_file"'
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
224 xdict['outputs'] += '<data format="%s" name="tab_file" label="${job_name}"/>\n' % self.outFormats
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
225 xdict['command'] = newCommand % xdict
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
226 xmls = newXML % xdict
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
227 xf = open(self.xmlfile,'w')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
228 xf.write(xmls)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
229 xf.write('\n')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
230 xf.close()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
231 # ready for the tarball
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
232
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
233
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
234 def makeTooltar(self):
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
235 """
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
236 a tool is a gz tarball with eg
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
237 /toolname/tool.xml /toolname/tool.py /toolname/test-data/test1_in.foo ...
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
238 """
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
239 retval = self.run()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
240 if retval:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
241 print >> sys.stderr,'## Run failed. Cannot build yet. Please fix and retry'
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
242 sys.exit(1)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
243 self.makeXML()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
244 tdir = self.toolname
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
245 os.mkdir(tdir)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
246 if self.opts.input_tab <> 'None': # we may have test data?
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
247 testdir = os.path.join(tdir,'test-data')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
248 os.mkdir(testdir) # make tests directory
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
249 shutil.copyfile(self.opts.input_tab,os.path.join(testdir,self.test1Input))
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
250 if self.opts.output_tab <> 'None':
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
251 shutil.copyfile(self.opts.output_tab,os.path.join(testdir,self.test1Output))
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
252 if self.opts.make_HTML:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
253 shutil.copyfile(self.opts.output_html,os.path.join(testdir,self.test1HTML))
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
254 if self.opts.output_dir:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
255 shutil.copyfile(self.tlog,os.path.join(testdir,'test1_out.log'))
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
256 shutil.copyfile(self.xmlfile,os.path.join(tdir,self.xmlfile))
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
257 shutil.copyfile(self.pyfile,os.path.join(tdir,'%s.py' % self.toolname))
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
258 shutil.copyfile(self.sfile,os.path.join(tdir,self.sfile))
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
259 tarpath = "%s.gz" % self.toolname
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
260 tar = tarfile.open(tarpath, "w:gz")
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
261 tar.add(tdir,arcname=self.toolname)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
262 tar.close()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
263 shutil.copy(tarpath,self.opts.new_tool)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
264 shutil.rmtree(tdir)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
265 ## TODO: replace with optional direct upload to local toolshed?
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
266 return retval
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
267
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
268 def compressPDF(self,inpdf=None,thumbformat='png'):
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
269 """need absolute path to pdf
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
270 """
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
271 assert os.path.isfile(inpdf), "## Input %s supplied to %s compressPDF not found" % (inpdf,self.myName)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
272 hf,hlog = tempfile.mkstemp(suffix="%s.log" % self.toolname)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
273 sto = open(hlog,'w')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
274 outpdf = '%s_compressed' % inpdf
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
275 cl = ["gs", "-sDEVICE=pdfwrite", "-dNOPAUSE", "-dBATCH", "-sOutputFile=%s" % outpdf,inpdf]
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
276 x = subprocess.Popen(cl,stdout=sto,stderr=sto,cwd=self.opts.output_dir)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
277 retval1 = x.wait()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
278 if retval1 == 0:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
279 os.unlink(inpdf)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
280 shutil.move(outpdf,inpdf)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
281 outpng = '%s.%s' % (os.path.splitext(inpdf)[0],thumbformat)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
282 cl2 = ['convert', inpdf, outpng]
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
283 x = subprocess.Popen(cl2,stdout=sto,stderr=sto,cwd=self.opts.output_dir)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
284 retval2 = x.wait()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
285 sto.close()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
286 retval = retval1 or retval2
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
287 return retval
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
288
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
289
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
290 def getfSize(self,fpath,outpath):
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
291 """
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
292 format a nice file size string
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
293 """
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
294 size = ''
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
295 fp = os.path.join(outpath,fpath)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
296 if os.path.isfile(fp):
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
297 n = float(os.path.getsize(fp))
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
298 if n > 2**20:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
299 size = ' (%1.1f MB)' % (n/2**20)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
300 elif n > 2**10:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
301 size = ' (%1.1f KB)' % (n/2**10)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
302 elif n > 0:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
303 size = ' (%d B)' % (int(n))
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
304 return size
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
305
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
306 def makeHtml(self):
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
307 """ Create an HTML file content to list all the artefacts found in the output_dir
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
308 """
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
309
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
310 galhtmlprefix = """<?xml version="1.0" encoding="utf-8" ?>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
311 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
312 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
313 <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
314 <meta name="generator" content="Galaxy %s tool output - see http://g2.trac.bx.psu.edu/" />
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
315 <title></title>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
316 <link rel="stylesheet" href="/static/style/base.css" type="text/css" />
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
317 </head>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
318 <body>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
319 <div class="document">
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
320 """
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
321 galhtmlattr = """<hr/><b><a href="https://bitbucket.org/fubar/galaxytoolfactory/overview">Galaxy Tool Factory Script Wrapper</a> tool output %s run at %s</b><br/>"""
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
322 galhtmlpostfix = """</div></body></html>\n"""
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
323
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
324 flist = os.listdir(self.opts.output_dir)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
325 flist = [x for x in flist if x <> 'Rplots.pdf']
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
326 flist.sort()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
327 html = [galhtmlprefix % progname,]
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
328 html.append('<h2>Galaxy %s outputs run at %s</h2><br/>\n' % (self.toolname,timenow()))
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
329 fhtml = []
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
330 if len(flist) > 0:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
331 html.append('<table cellpadding="3" cellspacing="3">\n')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
332 for fname in flist:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
333 dname,e = os.path.splitext(fname)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
334 sfsize = self.getfSize(fname,self.opts.output_dir)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
335 if e.lower() == '.pdf' : # compress and make a thumbnail
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
336 thumb = '%s.%s' % (dname,self.thumbformat)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
337 pdff = os.path.join(self.opts.output_dir,fname)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
338 retval = self.compressPDF(inpdf=pdff,thumbformat=self.thumbformat)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
339 if retval == 0:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
340 s= '<tr><td><a href="%s"><img src="%s" title="Click to download a PDF of %s" hspace="10" width="600"></a></td></tr>\n' % (fname,thumb,fname)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
341 html.append(s)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
342 fhtml.append('<li><a href="%s">%s %s</a></li>' % (fname,fname,sfsize))
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
343 else:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
344 fhtml.append('<li><a href="%s">%s %s</a></li>' % (fname,fname,sfsize))
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
345 html.append('</table>\n')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
346 if len(fhtml) > 0:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
347 fhtml.insert(0,'<ul>')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
348 fhtml.append('</ul><br/>')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
349 html += fhtml # add all non-pdf files to the end of the display
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
350 else:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
351 html.append('<h2>### Error - %s returned no files - please confirm that parameters are sane</h1>' % self.opts.interpreter)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
352 html.append('<h3>%s log follows below</h3><hr/><pre><br/>\n' % self.opts.interpreter)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
353 rlog = open(self.tlog,'r').readlines()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
354 html += rlog
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
355 html.append('<br/>%s CL = %s<br/>\n' % (self.toolname,' '.join(sys.argv)))
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
356 html.append('</pre>\n')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
357 html.append(galhtmlattr % (progname,timenow()))
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
358 html.append(galhtmlpostfix)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
359 htmlf = file(self.opts.output_html,'w')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
360 htmlf.write('\n'.join(html))
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
361 htmlf.write('\n')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
362 htmlf.close()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
363 self.html = html
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
364
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
365
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
366 def run(self):
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
367 """
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
368 """
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
369 if self.opts.output_dir:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
370 sto = open(self.tlog,'w')
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
371 p = subprocess.Popen(' '.join(self.cl),shell=True,stdout=sto,stderr=sto,stdin=subprocess.PIPE,cwd=self.opts.output_dir)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
372 else:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
373 p = subprocess.Popen(' '.join(self.cl),shell=True,stdin=subprocess.PIPE)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
374 p.stdin.write(self.script)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
375 p.stdin.close()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
376 retval = p.wait()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
377 if self.opts.output_dir:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
378 sto.close()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
379 if self.opts.make_HTML:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
380 self.makeHtml()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
381 return retval
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
382
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
383
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
384 def main():
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
385 u = """
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
386 This is a Galaxy wrapper. It expects to be called by a special purpose tool.xml as:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
387 <command interpreter="python">rgBaseScriptWrapper.py --script_path "$scriptPath" --tool_name "foo" --interpreter "Rscript"
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
388 </command>
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
389 """
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
390 op = optparse.OptionParser()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
391 a = op.add_option
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
392 a('--script_path',default=None)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
393 a('--tool_name',default=None)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
394 a('--interpreter',default=None)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
395 a('--output_dir',default=None)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
396 a('--output_html',default=None)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
397 a('--input_tab',default="None")
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
398 a('--output_tab',default="None")
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
399 a('--user_email',default=None)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
400 a('--bad_user',default=None)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
401 a('--make_Tool',default=None)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
402 a('--make_HTML',default=None)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
403 a('--help_text',default=None)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
404 a('--tool_desc',default=None)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
405 a('--new_tool',default=None)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
406 opts, args = op.parse_args()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
407 assert not opts.bad_user,'%s is NOT authorized to use this tool. Please ask your friendly admin to add their ID to admin_users in universe_wsgi.ini' % (opts.bad_user)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
408 assert opts.tool_name,'## Tool Factory expects a tool name - eg --tool_name=DESeq'
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
409 assert opts.interpreter,'## Tool Factory wrapper expects an interpreter - eg --interpreter=Rscript'
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
410 assert os.path.isfile(opts.script_path),'## Tool Factory wrapper expects a script path - eg --script_path=foo.R'
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
411 if opts.output_dir:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
412 try:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
413 os.makedirs(opts.output_dir)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
414 except:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
415 pass
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
416 r = ScriptRunner(opts)
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
417 if opts.make_Tool:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
418 retcode = r.makeTooltar()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
419 else:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
420 retcode = r.run()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
421 if retcode:
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
422 sys.exit(retcode) # indicate failure to job runner
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
423
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
424
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
425 if __name__ == "__main__":
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
426 main()
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
427
f82dd90a8c9e Initial install from a bitbucket gz of galaxytoolfactory
fubar
parents:
diff changeset
428