annotate rgToolFactory2.py @ 26:db35d39e1de9 draft

Passes planemo test Uses galaxyxml to generate new tool. More outputs will be added...
author fubar
date Thu, 30 Jul 2020 06:48:45 -0400
parents 9fe74bd23af2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
1 # rgToolFactory.py
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
2 # see https://github.com/fubar2/toolfactory
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
3 #
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
4 # copyright ross lazarus (ross stop lazarus at gmail stop com) May 2012
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
5 #
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
6 # all rights reserved
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
7 # Licensed under the LGPL
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
8 # suggestions for improvement and bug fixes welcome at https://github.com/fubar2/toolfactory
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
9 #
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
10 # July 2020: BCC was fun and I feel like rip van winkle after 5 years.
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
11 # Decided to
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
12 # 1. Fix the toolfactory so it works - done for simplest case
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
13 # 2. Fix planemo so the toolfactory function works
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
14 # 3. Rewrite bits using galaxyxml functions where that makes sense - done
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
15 #
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
16 # removed all the old complications including making the new tool use this same script
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
17 # galaxyxml now generates the tool xml https://github.com/hexylena/galaxyxml
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
18 # No support for automatic HTML file creation from arbitrary outputs
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
19 # TODO: add option to run that code as a post execution hook
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
20 # TODO: add additional history input parameters - currently only one
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
21
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
22 import sys
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
23 import subprocess
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
24 import shutil
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
25 import os
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
26 import time
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
27 import tempfile
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
28 import argparse
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
29 import tarfile
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
30 import re
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
31 import math
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
32 import galaxyxml.tool as gxt
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
33 import galaxyxml.tool.parameters as gxtp
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
34 import logging
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
35
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
36
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
37 progname = os.path.split(sys.argv[0])[1]
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
38 myversion = 'V2.1 July 2020'
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
39 verbose = True
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
40 debug = True
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
41 toolFactoryURL = 'https://github.com/fubar2/toolfactory'
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
42 ourdelim = '~~~'
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
43
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
44
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
45 def timenow():
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
46 """return current time as a string
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
47 """
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
48 return time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(time.time()))
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
49
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
50 def quote_non_numeric(s):
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
51 """return a prequoted string for non-numerics
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
52 useful for perl and Rscript parameter passing?
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
53 """
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
54 try:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
55 res = float(s)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
56 return s
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
57 except ValueError:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
58 return '"%s"' % s
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
59
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
60 html_escape_table = {
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
61 "&": "&",
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
62 ">": ">",
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
63 "<": "&lt;",
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
64 "$": "\$"
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
65 }
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
66
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
67 def html_escape(text):
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
68 """Produce entities within text."""
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
69 return "".join(html_escape_table.get(c,c) for c in text)
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
70
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
71
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
72 def html_unescape(text):
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
73 """Revert entities within text. Multiple character targets so use replace"""
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
74 t = text.replace('&amp;','&').replace('&gt;','>').replace('&lt;','<').replace('\$','$')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
75 return t
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
76
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
77 def parse_citations(citations_text):
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
78 """
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
79 """
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
80 citations = [c for c in citations_text.split("**ENTRY**") if c.strip()]
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
81 citation_tuples = []
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
82 for citation in citations:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
83 if citation.startswith("doi"):
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
84 citation_tuples.append( ("doi", citation[len("doi"):].strip() ) )
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
85 else:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
86 citation_tuples.append( ("bibtex", citation[len("bibtex"):].strip() ) )
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
87 return citation_tuples
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
88
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
89
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
90 class ScriptRunner:
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
91 """Wrapper for an arbitrary script
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
92 uses galaxyxml
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
93
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
94 """
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
95
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
96
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
97 def __init__(self,args=None):
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
98 """
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
99 cleanup inputs, setup some outputs
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
100
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
101 """
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
102 lastclredirect = None
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
103 self.cl = []
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
104 aCL = self.cl.append
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
105 if args.output_dir: # simplify for the tool tarball
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
106 os.chdir(args.output_dir)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
107 self.args = args
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
108 self.tool_name = re.sub('[^a-zA-Z0-9_]+', '', args.tool_name) # a sanitizer now does this but..
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
109 self.tool_id = self.tool_name
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
110 self.xmlfile = '%s.xml' % self.tool_name
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
111 if self.args.interpreter_name == "Executable": # binary - no need
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
112 aCL(self.args.exe_package) # this little CL will just run
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
113 else: # a script has been provided
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
114 rx = open(self.args.script_path,'r').readlines()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
115 rx = [x.rstrip() for x in rx] # remove pesky dos line endings if needed
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
116 self.script = '\n'.join(rx)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
117 fhandle,self.sfile = tempfile.mkstemp(prefix=self.tool_name,suffix=".%s" % (args.interpreter_name))
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
118 tscript = open(self.sfile,'w') # use self.sfile as script source for Popen
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
119 tscript.write(self.script)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
120 tscript.close()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
121 self.indentedScript = " %s" % '\n'.join([' %s' % html_escape(x) for x in rx]) # for restructured text in help
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
122 self.escapedScript = "%s" % '\n'.join([' %s' % html_escape(x) for x in rx])
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
123 aCL(self.args.interpreter_name)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
124 aCL(self.sfile)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
125 self.elog = os.path.join(self.args.output_dir,"%s_error.log" % self.tool_name)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
126 if args.output_dir: # may not want these complexities
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
127 self.tlog = os.path.join(self.args.output_dir,"%s_runner.log" % self.tool_name)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
128 art = '%s.%s' % (self.tool_name,args.interpreter_name)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
129 artpath = os.path.join(self.args.output_dir,art) # need full path
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
130 artifact = open(artpath,'w') # use self.sfile as script source for Popen
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
131 artifact.write(self.script)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
132 artifact.close()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
133 self.infile_paths = []
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
134 self.infile_format = []
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
135 self.infile_cl = []
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
136 self.infile_label = []
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
137 self.infile_help = []
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
138 if self.args.input_files:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
139 aif = [x.split(ourdelim) for x in self.args.input_files]
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
140 laif = list(map(list, zip(*aif))) # transpose the input_files array --input_files="$input_files~~~$CL~~~$input_formats~~~$input_label~~~$input_help"
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
141 self.infile_paths,self.infile_cl,self.infile_format,self.infile_label,self.infile_help = laif
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
142 self.infile_name = []
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
143 for i,scl in enumerate(self.infile_cl): # positionals have integers indicating order - need valid internal names
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
144 if scl.isdigit():
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
145 scl = 'input%s' % scl
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
146 if scl.upper() in ['STDOUT','STDIN']:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
147 scl = 'input%d' % (i+1)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
148 self.infile_name.append(scl) # make a list of internal names for each input file
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
149 clsuffix = [] # list all (cl param) pairs - positional needs sorting by cl index
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
150 clsuffix.append([self.args.output_cl,self.args.output_tab])
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
151 if self.args.parampass == '0': # only need two
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
152 aCL('<')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
153 aCL('%s' % self.infile_paths[0])
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
154 aCL('>')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
155 aCL('%s' % self.args.output_tab)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
156 else:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
157 for i,p in enumerate(self.infile_paths):
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
158 clsuffix.append([self.infile_cl[i],p]) # decorator is cl - sort for positional
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
159 for p in self.args.additional_parameters:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
160 psplit = p.split(ourdelim)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
161 pform = psplit[5]
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
162 if pform == 'STDOUT':
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
163 lastclredirect = ['>',psplit[1]]
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
164 else:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
165 clsuffix.append([pform,psplit[1]]) # cl,value
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
166 clsuffix.sort()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
167 if self.args.parampass == "positional":
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
168 plist = [] # need to decorate with CL and sort
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
169 # inputs in order then params in order TODO fix ordering using self.infile_cl
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
170 for (k,v) in clsuffix:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
171 if ' ' in v:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
172 aCL("v")
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
173 else:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
174 aCL(v)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
175 elif self.args.parampass == "argparse":
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
176 # inputs then params in argparse named form
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
177 for (k,v) in clsuffix:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
178 if ' ' in v:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
179 aCL('--%s' % k)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
180 aCL('"%s"' % v)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
181 else:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
182 aCL('--%s' % k)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
183 aCL('%s' % v)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
184 if lastclredirect:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
185 for v in lastclredirect:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
186 aCL(v) # add the stdout parameter last
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
187 self.test1Output = '%s_test1_output.xls' % self.tool_name
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
188 self.test1HTML = '%s_test1_output.html' % self.tool_name
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
189
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
190 def makeXML(self):
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
191 """
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
192 Create a Galaxy xml tool wrapper for the new script
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
193 Uses galaxyhtml
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
194 .
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
195
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
196 WRONG - fixme!
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
197 <test>
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
198 <param ftype="tabular,txt" value="infile.tabular,txt" name="infile"/>
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
199 <param value="test_a" name="job_name"/>
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
200 <param value="$runMe" name="runMe"/>
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
201 <param value="hello world" name="prefix"/>
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
202 <output value="reverseargp2_test1_output.xls" name="/home/ross/galaxy/database/objects/7/d/3/dataset_7d32a4f7-e5af-4f6d-9241-8b5347118c73.dat"/>
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
203 </test>
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
204
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
205 """
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
206 # need interp and executable (?script) or else executable only
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
207 if self.args.interpreter_name:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
208 exe = "$runMe" # our dynamic script from the tool builder
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
209 interp = self.args.interpreter_name
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
210 else:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
211 interp = None
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
212 exe = self.args.exe_package
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
213 assert exe != None, 'No interpeter or executable passed in to makeXML'
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
214 tool = gxt.Tool(self.args.tool_name,self.tool_id,self.args.tool_version,self.args.tool_desc,exe)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
215 if interp:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
216 tool.interpreter=interp
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
217 if self.args.help_text:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
218 helptext = open(self.args.help_text,'r').readlines()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
219 helptext = [html_escape(x) for x in helptext] # must html escape here too - thanks to Marius van den Beek
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
220 tool.help = ''.join([x for x in helptext])
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
221 else:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
222 tool.help = 'Please ask the tool author (%s) for help as none was supplied at tool generation\n' % (user_email)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
223 tool.version_command = None # do not want
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
224 inputs = gxtp.Inputs()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
225 outputs = gxtp.Outputs()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
226 requirements = gxtp.Requirements()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
227 testparam = []
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
228 is_positional = (self.args.parampass == 'positional')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
229 if self.args.include_dependencies == "yes":
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
230 requirements.append(gxtp.Requirement('package', 'ghostscript'))
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
231 requirements.append(gxtp.Requirement('package', 'graphicsmagick'))
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
232 if self.args.interpreter_name:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
233 if self.args.interpreter_name == 'python': # always needed for this runner script
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
234 requirements.append(gxtp.Requirement('package', 'python',self.args.interpreter_version))
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
235 elif not self.args.interpreter_name in ['bash','sh']:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
236 requirements.append(gxtp.Requirement('package', self.args.interpreter_name,self.args.interpreter_version))
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
237 else:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
238 if self.args.exe_package: # uses exe not interpreter
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
239 requirements.append(gxtp.Requirement('package', self.args.exe_package,self.args.exe_package_version))
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
240 tool.requirements = requirements
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
241 for i,infpath in enumerate(self.infile_paths):
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
242 if self.args.parampass == 0:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
243 assert len(self.infile_name) == 1,'Maximum one "<" if parampass is 0 - more than one input files supplied'
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
244 newname = self.infile_name[i]
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
245 if len(newname) > 1:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
246 ndash = 2
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
247 else:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
248 ndash = 1
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
249 if not len(self.infile_label[i]) > 0:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
250 alab = self.infile_name[i]
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
251 else:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
252 alab = self.infile_label[i]
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
253 aninput = gxtp.DataParam(self.infile_name[i],optional=False, label=alab, help=self.infile_help[i], \
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
254 format=self.infile_format[i],multiple=False,num_dashes=ndash)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
255 if self.args.parampass == '0':
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
256 aninput.command_line_override = '< $%s' % self.infile_name[i]
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
257 aninput.positional = is_positional
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
258 inputs.append(aninput)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
259 for parm in self.args.additional_parameters:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
260 newname,newval,newlabel,newhelp,newtype,newcl = parm.split(ourdelim)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
261 if not len(newlabel) > 0:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
262 newlabel = newname
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
263 if len(newname) > 1:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
264 ndash = 2
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
265 else:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
266 ndash = 1
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
267 if newtype == "text":
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
268 aparm = gxtp.TextParam(newname,label=newlabel,help=newhelp,value=newval,num_dashes=ndash)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
269 elif newtype == "integer":
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
270 aparm = gxtp.IntegerParam(newname,label=newname,help=newhelp,value=newval,num_dashes=ndash)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
271 elif newtype == "float":
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
272 aparm = gxtp.FloatParam(newname,label=newname,help=newhelp,value=newval,num_dashes=ndash)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
273 else:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
274 raise ValueError('Unrecognised parameter type "%s" for additional parameter %s in makeXML' % (newtype,psplit[0]))
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
275 aparm.positional = is_positional
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
276 inputs.append(aparm)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
277 tparm = gxtp.TestParam(newname,value=newval)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
278 testparam.append(tparm)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
279 tool.inputs = inputs
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
280 configfiles = gxtp.Configfiles()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
281 configfiles.append(gxtp.Configfile(name="runMe",text=self.script))
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
282 tool.configfiles = configfiles
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
283 if self.args.output_tab:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
284 ext = self.args.output_format
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
285 aparm = gxtp.OutputData(self.args.output_cl, format=ext,num_dashes=ndash)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
286 if is_positional:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
287 aparm.command_line_override = '> $output1'
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
288 aparm.positional = is_positional
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
289 outputs.append(aparm)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
290 tool.outputs = outputs
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
291 tests = gxtp.Tests()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
292 test_a = gxtp.Test()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
293 ext = self.infile_format[0].split(',')[0]
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
294 if is_positional:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
295 param = gxtp.TestParam('input1',value='input1.%s' % ext,ftype=ext)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
296 else:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
297 param = gxtp.TestParam(self.infile_name[0],value='%s.%s' % (self.infile_name[0],ext),ftype=ext)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
298 test_a.append(param)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
299 param = gxtp.TestParam('job_name', value='test_a')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
300 test_a.append(param)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
301 param = gxtp.TestParam('runMe', value="$runMe")
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
302 test_a.append(param)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
303 for aparam in testparam:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
304 test_a.append(aparam)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
305 test_out = gxtp.TestOutput(name=self.args.output_cl, value=self.test1Output)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
306 test_a.append(test_out)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
307 tests.append(test_a)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
308 tool.tests = tests
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
309 tool.add_comment('Created by %s at %s using the Galaxy Tool Factory.' % (self.args.user_email,timenow()))
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
310 tool.add_comment('Source in git at: %s' % (toolFactoryURL))
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
311 tool.add_comment('Cite: Creating re-usable tools from scripts doi: 10.1093/bioinformatics/bts573')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
312 exml = tool.export()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
313 xf = open(self.xmlfile,'w')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
314 xf.write(exml)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
315 xf.write('\n')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
316 xf.close()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
317 # ready for the tarball
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
318
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
319
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
320 def makeTooltar(self):
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
321 """
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
322 a tool is a gz tarball with eg
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
323 /toolname/tool.xml /toolname/tool.py /toolname/test-data/test1_in.foo ...
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
324 """
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
325 retval = self.run()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
326 if retval:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
327 sys.stderr.write('## Run failed. Cannot build yet. Please fix and retry')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
328 sys.exit(1)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
329 tdir = 'tdir_%s' % self.tool_name
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
330 if not os.path.exists(tdir):
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
331 os.mkdir(tdir)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
332 self.makeXML()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
333 if self.args.help_text:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
334 hlp = open(self.args.help_text,'r').read()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
335 else:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
336 hlp = 'Please ask the tool author for help as none was supplied at tool generation\n'
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
337 readme_dict = {'readme':hlp,'interpreter_name':self.args.interpreter_name,'interpreter_version':self.args.interpreter_version}
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
338 testdir = os.path.join(tdir,'test-data')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
339 if not os.path.exists(testdir):
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
340 os.mkdir(testdir) # make tests directory
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
341 for i,infile in enumerate(self.infile_paths):
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
342 dest = os.path.join(testdir,'%s.%s' % (self.infile_name[i],self.infile_format[i]))
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
343 if infile != dest:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
344 shutil.copyfile(infile,dest)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
345 if self.args.output_tab and os.path.exists(self.args.output_tab):
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
346 shutil.copyfile(self.args.output_tab,os.path.join(testdir,self.test1Output))
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
347 else:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
348 print('#### no output_tab %s exists' % self.args.output_tab)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
349 if self.args.output_dir:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
350 if os.path.exists(self.tlog):
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
351 shutil.copyfile(self.tlog,os.path.join(testdir,'test1_out.log'))
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
352 stname = os.path.join(tdir,self.sfile)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
353 if not os.path.exists(stname):
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
354 shutil.copyfile(self.sfile, stname)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
355 xtname = os.path.join(tdir,self.xmlfile)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
356 if not os.path.exists(xtname):
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
357 shutil.copyfile(self.xmlfile,xtname)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
358 tarpath = "%s.tar.gz" % self.tool_name
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
359 tar = tarfile.open(tarpath, "w:gz")
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
360 tar.add(tdir,recursive=True,arcname='%s' % self.tool_name)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
361 tar.close()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
362 shutil.copyfile(tarpath,self.args.new_tool)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
363 shutil.rmtree(tdir)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
364 ## TODO: replace with optional direct upload to local toolshed?
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
365 return retval
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
366
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
367
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
368 def run(self):
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
369 """
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
370 Some devteam tools have this defensive stderr read so I'm keeping with the faith
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
371 Feel free to update.
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
372 """
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
373 logging.debug('run cl=%s' % str(self.cl))
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
374 scl = ' '.join(self.cl)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
375 err = None
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
376 if self.args.parampass != '0':
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
377 ste = open(self.elog,'wb')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
378 sto = open(self.tlog,'wb')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
379 sto.write(bytes('## Executing Toolfactory generated command line = %s\n' % scl,"utf8"))
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
380 sto.flush()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
381 p = subprocess.run(self.cl,shell=False,stdout=sto,stderr=ste,cwd=self.args.output_dir)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
382 sto.close()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
383 ste.close()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
384 tmp_stderr = open(self.elog, 'rb' )
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
385 err = ''
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
386 buffsize = 1048576
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
387 try:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
388 while True:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
389 err += str(tmp_stderr.read( buffsize ))
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
390 if not err or len( err ) % buffsize != 0:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
391 break
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
392 except OverflowError:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
393 pass
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
394 tmp_stderr.close()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
395 retval = p.returncode
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
396 else: # work around special case of simple scripts that take stdin and write to stdout
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
397 sti = open(self.infile_paths[0],'rb')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
398 sto = open(self.args.output_tab,'wb')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
399 p = subprocess.run(self.cl, shell=False, stdout=sto,stdin=sti) # must use shell to redirect
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
400 retval = p.returncode
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
401 sto.close()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
402 sti.close()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
403 if self.args.output_dir:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
404 if p.returncode != 0 and err: # problem
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
405 sys.stderr.write(err)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
406 logging.debug('run done')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
407 return retval
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
408
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
409
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
410
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
411 def main():
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
412 u = """
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
413 This is a Galaxy wrapper. It expects to be called by a special purpose tool.xml as:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
414 <command interpreter="python">rgBaseScriptWrapper.py --script_path "$scriptPath" --tool_name "foo" --interpreter "Rscript"
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
415 </command>
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
416 """
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
417 parser = argparse.ArgumentParser()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
418 a = parser.add_argument
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
419 a('--script_path',default='')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
420 a('--tool_name',default=None)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
421 a('--interpreter_name',default=None)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
422 a('--interpreter_version',default=None)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
423 a('--exe_package',default=None)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
424 a('--exe_package_version',default=None)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
425 a('--output_dir',default='./')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
426 a('--input_files',default=[], action="append") # these are "galaxypath,metadataname" pairs
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
427 a("--input_formats",default="tabular")
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
428 a('--output_tab',default=None)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
429 a('--output_format',default='tabular')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
430 a('--output_cl',default=None)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
431 a('--user_email',default='Unknown')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
432 a('--bad_user',default=None)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
433 a('--make_Tool',default=None)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
434 a('--help_text',default=None)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
435 a('--tool_desc',default=None)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
436 a('--new_tool',default=None)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
437 a('--tool_version',default=None)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
438 a('--include_dependencies',default=None)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
439 a('--citations',default=None)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
440 a('--additional_parameters', dest='additional_parameters', action='append', default=[])
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
441 a('--edit_additional_parameters', action="store_true", default=False)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
442 a('--parampass',default="positional")
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
443 args = parser.parse_args()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
444 assert not args.bad_user,'UNAUTHORISED: %s is NOT authorized to use this tool until Galaxy admin adds %s to "admin_users" in the Galaxy configuration file' % (args.bad_user,args.bad_user)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
445 assert args.tool_name,'## Tool Factory expects a tool name - eg --tool_name=DESeq'
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
446 assert (args.interpreter_name or args.exe_package),'## Tool Factory wrapper expects an interpreter - eg --interpreter_name=Rscript or an executable package findable by the dependency management package'
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
447 assert args.exe_package or (len(args.script_path) > 0 and os.path.isfile(args.script_path)),'## Tool Factory wrapper expects a script path - eg --script_path=foo.R if no executable'
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
448 if args.output_dir:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
449 try:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
450 os.makedirs(args.output_dir)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
451 except:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
452 pass
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
453 args.input_files = [x.replace('"','').replace("'",'') for x in args.input_files]
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
454 for i,x in enumerate(args.additional_parameters): # remove quotes we need to deal with spaces in CL params
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
455 args.additional_parameters[i] = args.additional_parameters[i].replace('"','')
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
456 r = ScriptRunner(args)
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
457 if args.make_Tool:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
458 retcode = r.makeTooltar()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
459 else:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
460 retcode = r.run()
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
461 if retcode:
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
462 sys.exit(retcode) # indicate failure to job runner
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
463
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
464
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
465 if __name__ == "__main__":
26
db35d39e1de9 Passes planemo test
fubar
parents: 25
diff changeset
466 main()
25
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
467
9fe74bd23af2 Uploaded
fubar
parents:
diff changeset
468