comparison rgToolFactory2.py @ 9:ce5ec1d989fd draft

Uploaded
author fubar
date Wed, 14 Jan 2015 19:17:11 -0500
parents 9d58cc32a12d
children 8de2b7571d98
comparison
equal deleted inserted replaced
8:9d58cc32a12d 9:ce5ec1d989fd
170 citation_tuples.append( ("doi", citation[len("doi"):].strip() ) ) 170 citation_tuples.append( ("doi", citation[len("doi"):].strip() ) )
171 else: 171 else:
172 citation_tuples.append( ("bibtex", citation[len("bibtex"):].strip() ) ) 172 citation_tuples.append( ("bibtex", citation[len("bibtex"):].strip() ) )
173 return citation_tuples 173 return citation_tuples
174 174
175 175 def shell_source(script):
176 """need a way to source a Galaxy tool interpreter env.sh so we can use that dependency
177 package
178 see http://pythonwise.blogspot.fr/2010/04/sourcing-shell-script.html
179 Sometime you want to emulate the action of "source" in bash,
180 settings some environment variables. Here is a way to do it.
181 Note that we have to finesse the automagic exports using nulls as newlines for env"""
182 pipe = subprocess.Popen("env -i ; . %s ; env -0" % script, stdout=subprocess.PIPE, shell=True)
183 output = pipe.communicate()[0]
184 outl = output.split('\0')
185 outl = [x for x in outl if len(x.split("=")) == 2]
186 newenv = dict((line.split("=", 1) for line in outl))
187 os.environ.update(newenv)
188
176 class ScriptRunner: 189 class ScriptRunner:
177 """class is a wrapper for an arbitrary script 190 """class is a wrapper for an arbitrary script
178 """ 191 """
179 192
180 def __init__(self,opts=None,treatbashSpecial=True): 193 def __init__(self,opts=None,treatbashSpecial=True):
216 self.cl = [] 229 self.cl = []
217 self.html = [] 230 self.html = []
218 self.test1Inputs = [] # now a list 231 self.test1Inputs = [] # now a list
219 a = self.cl.append 232 a = self.cl.append
220 a(opts.interpreter) 233 a(opts.interpreter)
221 if self.treatbashSpecial and opts.interpreter in ['bash','sh']: 234 a(self.sfile)
222 a(self.sfile)
223 else:
224 a('-') # stdin
225 # if multiple inputs - positional or need to distinguish them with cl params 235 # if multiple inputs - positional or need to distinguish them with cl params
226 if opts.input_tab: 236 if opts.input_tab:
227 tests = [] 237 tests = []
228 for i,intab in enumerate(opts.input_tab): # if multiple, make tests 238 for i,intab in enumerate(opts.input_tab): # if multiple, make tests
229 if intab.find(',') <> -1: 239 if intab.find(',') <> -1:
389 </help> 399 </help>
390 <citations> 400 <citations>
391 %(citations)s 401 %(citations)s
392 <citation type="doi">10.1093/bioinformatics/bts573</citation> 402 <citation type="doi">10.1093/bioinformatics/bts573</citation>
393 </citations> 403 </citations>
394 </tool>""" # needs a dict with toolname, toolid, interpreter, scriptname, command, inputs as a multi line string ready to write, outputs ditto, help ditto 404 </tool>"""
405 # needs a dict with toolname, toolid, interpreter, scriptname, command, inputs as a multi line string ready to write, outputs ditto, help ditto
395 406
396 newCommand=""" 407 newCommand="""
397 %(toolname)s.py --script_path "$runMe" --interpreter "%(interpreter)s" 408 %(toolname)s.py --script_path "$runMe" --interpreter "%(interpreter)s"
398 --tool_name "%(toolname)s" 409 --tool_name "%(toolname)s"
399 %(command_inputs)s 410 %(command_inputs)s
431 xdict = {} 442 xdict = {}
432 xdict['additionalParams'] = '' 443 xdict['additionalParams'] = ''
433 xdict['additionalInputs'] = '' 444 xdict['additionalInputs'] = ''
434 if self.opts.additional_parameters: 445 if self.opts.additional_parameters:
435 if self.opts.edit_additional_parameters: # add to new tool form with default value set to original value 446 if self.opts.edit_additional_parameters: # add to new tool form with default value set to original value
436 xdict['additionalInputs'] = '\n'.join(['<param name="%s" value="%s" label="%s" help="%s" type="%s"/>' % (x.split(',')[0],html_escape(x.split(',')[1]),html_escape(x.split(',')[2]), 447 xdict['additionalInputs'] = '\n'.join(['<param name="%s" value="%s" label="%s" help="%s" type="%s"/>' % \
437 html_escape(x.split(',')[3]), x.split(',')[4]) for x in self.opts.additional_parameters]) 448 (x.split(',')[0],html_escape(x.split(',')[1]),html_escape(x.split(',')[2]),html_escape(x.split(',')[3]), x.split(',')[4]) for x in self.opts.additional_parameters])
438 xdict['additionalParams'] = '\n'.join(['<param name="%s" value="%s" />' % (x.split(',')[0],html_escape(x.split(',')[1])) for x in self.opts.additional_parameters]) 449 xdict['additionalParams'] = '\n'.join(['<param name="%s" value="%s" />' % (x.split(',')[0],html_escape(x.split(',')[1])) for x in self.opts.additional_parameters])
439 xdict['requirements'] = '' 450 xdict['requirements'] = ''
440 if self.opts.make_HTML: 451 if self.opts.make_HTML:
441 if self.opts.include_dependencies == "yes": 452 if self.opts.include_dependencies == "yes":
442 xdict['requirements'] = protorequirements 453 xdict['requirements'] = protorequirements
772 htmlf.write('\n') 783 htmlf.write('\n')
773 htmlf.close() 784 htmlf.close()
774 self.html = html 785 self.html = html
775 786
776 787
788
777 def run(self): 789 def run(self):
778 """ 790 """
779 scripts must be small enough not to fill the pipe! 791 scripts must be small enough not to fill the pipe!
780 """ 792 """
793 if self.opts.envshpath <> 'system':
794 shell_source(self.opts.envshpath)
781 if self.treatbashSpecial and self.opts.interpreter in ['bash','sh']: 795 if self.treatbashSpecial and self.opts.interpreter in ['bash','sh']:
782 retval = self.runBash() 796 retval = self.runBash()
783 else: 797 else:
784 if self.opts.output_dir: 798 if self.opts.output_dir:
785 ste = open(self.elog,'w') 799 ste = open(self.elog,'w')
786 sto = open(self.tlog,'w') 800 sto = open(self.tlog,'w')
787 sto.write('## Toolfactory generated command line = %s\n' % ' '.join(self.cl)) 801 sto.write('## Toolfactory generated command line = %s\n' % ' '.join(self.cl))
788 sto.flush() 802 sto.flush()
789 p = subprocess.Popen(self.cl,shell=False,stdout=sto,stderr=ste,stdin=subprocess.PIPE,cwd=self.opts.output_dir) 803 p = subprocess.Popen(self.cl,shell=False,stdout=sto,stderr=ste,cwd=self.opts.output_dir)
790 else: 804 else:
791 p = subprocess.Popen(self.cl,shell=False,stdin=subprocess.PIPE) 805 p = subprocess.Popen(self.cl,shell=False)
792 p.stdin.write(self.script)
793 p.stdin.close()
794 retval = p.wait() 806 retval = p.wait()
795 if self.opts.output_dir: 807 if self.opts.output_dir:
796 sto.close() 808 sto.close()
797 ste.close() 809 ste.close()
798 err = open(self.elog,'r').readlines() 810 err = open(self.elog,'r').readlines()
849 a('--tool_version',default=None) 861 a('--tool_version',default=None)
850 a('--include_dependencies',default="yes") 862 a('--include_dependencies',default="yes")
851 a('--citations',default=None) 863 a('--citations',default=None)
852 a('--additional_parameters', dest='additional_parameters', action='append', default=[]) 864 a('--additional_parameters', dest='additional_parameters', action='append', default=[])
853 a('--edit_additional_parameters', action="store_true", default=False) 865 a('--edit_additional_parameters', action="store_true", default=False)
866 a('--envshpath',default="system")
854 opts, args = op.parse_args() 867 opts, args = op.parse_args()
855 assert not opts.bad_user,'UNAUTHORISED: %s is NOT authorized to use this tool until Galaxy admin adds %s to admin_users in universe_wsgi.ini' % (opts.bad_user,opts.bad_user) 868 assert not opts.bad_user,'UNAUTHORISED: %s is NOT authorized to use this tool until Galaxy admin adds %s to admin_users in universe_wsgi.ini' % (opts.bad_user,opts.bad_user)
856 assert opts.tool_name,'## Tool Factory expects a tool name - eg --tool_name=DESeq' 869 assert opts.tool_name,'## Tool Factory expects a tool name - eg --tool_name=DESeq'
857 assert opts.interpreter,'## Tool Factory wrapper expects an interpreter - eg --interpreter=Rscript' 870 assert opts.interpreter,'## Tool Factory wrapper expects an interpreter - eg --interpreter=Rscript'
858 assert os.path.isfile(opts.script_path),'## Tool Factory wrapper expects a script path - eg --script_path=foo.R' 871 assert os.path.isfile(opts.script_path),'## Tool Factory wrapper expects a script path - eg --script_path=foo.R'