comparison rgToolFactory2.py @ 2:6a3c292412fa draft

Uploaded
author fubar
date Sun, 11 Jan 2015 21:32:15 -0500
parents c34063ab3735
children d75fd880fce2
comparison
equal deleted inserted replaced
1:323982517b30 2:6a3c292412fa
119 %s 119 %s
120 </readme> 120 </readme>
121 </tool_dependency> 121 </tool_dependency>
122 """ 122 """
123 123
124 toolhtmldepskel = """<?xml version="1.0"?> 124 emptytoolhtmldepskel = """<?xml version="1.0"?>
125 <tool_dependency> 125 <tool_dependency>
126 <readme> 126 <readme>
127 %s 127 %s
128 </readme> 128 </readme>
129 </tool_dependency> 129 </tool_dependency>
228 self.test1Inputs = '<param name="input_tab" value="%s" />' % (','.join(tests)) 228 self.test1Inputs = '<param name="input_tab" value="%s" />' % (','.join(tests))
229 else: 229 else:
230 self.test1Inputs = '' 230 self.test1Inputs = ''
231 # we always pass path,name pairs in using python optparse append 231 # we always pass path,name pairs in using python optparse append
232 # but the command line has to be different 232 # but the command line has to be different
233 self.infile_paths = ','.join([x.split(',')[0] for x in self.opts.input_tab]) 233 self.infile_paths = ''
234 self.infile_names = ','.join([x.split(',')[1] for x in self.opts.input_tab]) 234 self.infile_names = ''
235 if self.opts.input_tab:
236 self.infile_paths = ','.join([x.split(',')[0].strip() for x in self.opts.input_tab])
237 self.infile_names = ','.join([x.split(',')[1].strip() for x in self.opts.input_tab])
235 if self.opts.interpreter == 'python': 238 if self.opts.interpreter == 'python':
236 # yes, this is how additional parameters are always passed in python - to the TF itself and to 239 # yes, this is how additional parameters are always passed in python - to the TF itself and to
237 # scripts to avoid having unknown parameter names (yes, they can be parsed but...) on the command line 240 # scripts to avoid having unknown parameter names (yes, they can be parsed but...) on the command line
238 a('--INPATHS "%s"' % (self.infile_paths)) 241 if self.opts.input_tab:
239 a('--INNAMES "%s"' % (self.infile_names)) 242 a('--INPATHS "%s"' % (self.infile_paths))
243 a('--INNAMES "%s"' % (self.infile_names))
240 if self.opts.output_tab: 244 if self.opts.output_tab:
241 a('--OUTPATH "%s"' % self.opts.output_tab) 245 a('--OUTPATH "%s"' % self.opts.output_tab)
242 for p in opts.additional_parameters: 246 for p in opts.additional_parameters:
243 p = p.replace('"','') 247 p = p.replace('"','')
244 psplit=p.split(',') 248 ps = p.split(',')
245 param = psplit[0] 249 a('%s="%s"' % (ps[0],ps[1]))
246 value = psplit[1]
247 a('--additional_parameters "%s,%s"' % (param,value))
248 if (self.opts.interpreter == 'Rscript'): 250 if (self.opts.interpreter == 'Rscript'):
249 # pass params on command line 251 # pass params on command line
250 a('INPATHS "%s"' % self.infile_paths) 252 if self.opts.input_tab:
251 a('INNAMES "%s"' % self.infile_names) 253 a('INPATHS="%s"' % self.infile_paths)
254 a('INNAMES="%s"' % self.infile_names)
252 if self.opts.output_tab: 255 if self.opts.output_tab:
253 a('OUTPATH "%s"' % self.opts.output_tab) 256 a('OUTPATH="%s"' % self.opts.output_tab)
254 for param in opts.additional_parameters: 257 for p in opts.additional_parameters:
255 param, value=param.split(',') 258 p = p.replace('"','')
256 a('%s="%s"' % (param,value)) 259 ps = p.split(',')
260 a('%s="%s"' % (ps[0],ps[1]))
257 if (self.opts.interpreter == 'perl'): 261 if (self.opts.interpreter == 'perl'):
258 # pass params on command line 262 # pass params on command line
259 a('%s' % self.infile_paths) 263 if self.opts.input_tab:
260 a('%s' % self.infile_names) 264 a('%s' % self.infile_paths)
265 a('%s' % self.infile_names)
261 if self.opts.output_tab: 266 if self.opts.output_tab:
262 a('%s' % self.opts.output_tab) 267 a('%s' % self.opts.output_tab)
263 for param in opts.additional_parameters: 268 for p in opts.additional_parameters:
264 param, value=param.split(',') 269 p = p.replace('"','')
270 ps = p.split(',')
271 param = ps[0]
272 value = ps[1]
265 if (value.find(' ') <> -1): 273 if (value.find(' ') <> -1):
266 a('%s="%s"' % (param,value)) 274 a('%s="%s"' % (param,value))
267 else: 275 else:
268 a('%s=%s' % (param,value)) 276 a('%s=%s' % (param,value))
269
270 if self.opts.interpreter == 'sh' or self.opts.interpreter == 'bash': 277 if self.opts.interpreter == 'sh' or self.opts.interpreter == 'bash':
271 # more is better - now move all params into environment AND drop on to command line. 278 # more is better - now move all params into environment AND drop on to command line.
272 self.cl.insert(0,'env') 279 self.cl.insert(0,'env')
273 self.cl.insert(1,'INPATHS=%s' % (self.infile_paths)) 280 if self.opts.input_tab:
274 self.cl.insert(2,'INNAMES=%s' % (self.infile_names)) 281 self.cl.insert(1,'INPATHS=%s' % (self.infile_paths))
282 self.cl.insert(2,'INNAMES=%s' % (self.infile_names))
275 if self.opts.output_tab: 283 if self.opts.output_tab:
276 self.cl.insert(3,'OUTPATH=%s' % (self.opts.output_tab)) 284 self.cl.insert(3,'OUTPATH=%s' % (self.opts.output_tab))
277 a('OUTPATH=%s' % (self.opts.output_tab)) 285 a('OUTPATH=%s' % (self.opts.output_tab))
278 # sets those environment variables for the script 286 # sets those environment variables for the script
279 # additional params appear in CL - yes, it's confusing 287 # additional params appear in CL - yes, it's confusing
413 xdict = {} 421 xdict = {}
414 xdict['additionalParams'] = '' 422 xdict['additionalParams'] = ''
415 xdict['additionalInputs'] = '' 423 xdict['additionalInputs'] = ''
416 if self.opts.additional_parameters: 424 if self.opts.additional_parameters:
417 if self.opts.edit_additional_parameters: # add to new tool form with default value set to original value 425 if self.opts.edit_additional_parameters: # add to new tool form with default value set to original value
418 xdict['additionalInputs'] = '\n'.join(['<param name="%s" value="%s" label="%s" help="%s" type="%s"/>' % (x.split(',')[0],x.split(',')[1],x.split(',')[2], 426 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]),
419 x.split(',')[3], x.split(',')[4]) for x in self.opts.additional_parameters]) 427 html_escape(x.split(',')[3]), x.split(',')[4]) for x in self.opts.additional_parameters])
420 xdict['additionalParams'] = '\n'.join(['<param name="%s" value="%s" />' % (x.split(',')[0],x.split(',')[1]) for x in self.opts.additional_parameters]) 428 xdict['additionalParams'] = '\n'.join(['<param name="%s" value="%s" />' % (x.split(',')[0],html_escape(x.split(',')[1])) for x in self.opts.additional_parameters])
421 xdict['requirements'] = '' 429 xdict['requirements'] = ''
422 if self.opts.make_HTML: 430 if self.opts.make_HTML:
423 if self.opts.include_dependencies == "yes": 431 if self.opts.include_dependencies == "yes":
424 xdict['requirements'] = protorequirements 432 xdict['requirements'] = protorequirements
425 xdict['tool_version'] = self.opts.tool_version 433 xdict['tool_version'] = self.opts.tool_version
453 xdict['tooldesc'] = '<description>%s</description>' % self.opts.tool_desc 461 xdict['tooldesc'] = '<description>%s</description>' % self.opts.tool_desc
454 else: 462 else:
455 xdict['tooldesc'] = '' 463 xdict['tooldesc'] = ''
456 xdict['command_outputs'] = '' 464 xdict['command_outputs'] = ''
457 xdict['outputs'] = '' 465 xdict['outputs'] = ''
458 if self.opts.input_tab <> 'None': 466 if self.opts.input_tab:
459 cins = ['\n',] 467 cins = ['\n',]
468 cins.append('--input_formats %s' % self.opts.input_formats)
460 cins.append('#for intab in $input1:') 469 cins.append('#for intab in $input1:')
461 cins.append('--input_tab "$intab,$intab.name"') 470 cins.append('--input_tab "${intab},${intab.name}"')
462 cins.append('#end for\n') 471 cins.append('#end for\n')
463 xdict['command_inputs'] = '\n'.join(cins) 472 xdict['command_inputs'] = '\n'.join(cins)
464 xdict['inputs'] = '''<param name="input_tab" multiple="true" type="data" format="%s" label="Select one or more %s input files from your history" 473 xdict['inputs'] = '''<param name="input_tab" multiple="true" type="data" format="%s" label="Select one or more %s input files from your history"
465 help="Multiple inputs may be selected assuming the script can deal with them..."/> \n''' % (self.inputFormats,self.inputFormats) 474 help="Multiple inputs may be selected assuming the script can deal with them..."/> \n''' % (self.inputFormats,self.inputFormats)
466 else: 475 else:
467 xdict['command_inputs'] = '' # assume no input - eg a random data generator 476 xdict['command_inputs'] = '' # assume no input - eg a random data generator
468 xdict['inputs'] = '' 477 xdict['inputs'] = ''
469 if (len(self.opts.additional_parameters) > 0): 478 if (len(self.opts.additional_parameters) > 0):
470 cins = ['\n',] 479 cins = ['\n',]
471 for params in self.opts.additional_parameters: 480 for params in self.opts.additional_parameters:
472 if self.opts.edit_additional_parameters:
473 psplit = params.split(',') # name,value... 481 psplit = params.split(',') # name,value...
474 psplit[1] = '$%s' % psplit[0] # replace with form value 482 psplit[3] = html_escape(psplit[3])
475 cins.append('--additional_parameters "%s"' % ','.join(psplit)) 483 if self.opts.edit_additional_parameters:
476 else: 484 psplit[1] = '$%s' % psplit[0] # replace with form value
477 cins.append('--additional_parameters "%s"' % params) 485 else:
486 psplit[1] = html_escape(psplit[1]) # leave prespecified value
487 cins.append('--additional_parameters """%s"""' % ','.join(psplit))
478 xdict['command_inputs'] = '%s\n%s' % (xdict['command_inputs'],'\n'.join(cins)) 488 xdict['command_inputs'] = '%s\n%s' % (xdict['command_inputs'],'\n'.join(cins))
479 xdict['inputs'] += '<param name="job_name" type="text" size="60" label="Supply a name for the outputs to remind you what they contain" value="%s"/> \n' % self.toolname 489 xdict['inputs'] += '<param name="job_name" type="text" size="60" label="Supply a name for the outputs to remind you what they contain" value="%s"/> \n' % self.toolname
480 xdict['toolname'] = self.toolname 490 xdict['toolname'] = self.toolname
481 xdict['toolid'] = self.toolid 491 xdict['toolid'] = self.toolid
482 xdict['interpreter'] = self.opts.interpreter 492 xdict['interpreter'] = self.opts.interpreter
518 print >> sys.stderr,'## Run failed. Cannot build yet. Please fix and retry' 528 print >> sys.stderr,'## Run failed. Cannot build yet. Please fix and retry'
519 sys.exit(1) 529 sys.exit(1)
520 tdir = self.toolname 530 tdir = self.toolname
521 os.mkdir(tdir) 531 os.mkdir(tdir)
522 self.makeXML() 532 self.makeXML()
523 if self.opts.make_HTML: 533 if self.opts.help_text:
524 if self.opts.help_text: 534 hlp = open(self.opts.help_text,'r').read()
525 hlp = open(self.opts.help_text,'r').read() 535 else:
526 else: 536 hlp = 'Please ask the tool author for help as none was supplied at tool generation\n'
527 hlp = 'Please ask the tool author for help as none was supplied at tool generation\n' 537 if self.opts.include_dependencies == "yes":
528 if self.opts.include_dependencies == "yes": 538 tooldepcontent = toolhtmldepskel % hlp
529 tooldepcontent = toolhtmldepskel % hlp 539 else:
530 else: 540 tooldepcontent = emptytoolhtmldepskel % hlp
531 tooldepcontent = emptytoolhtmldepskel % hlp 541 depf = open(os.path.join(tdir,'tool_dependencies.xml'),'w')
532 depf = open(os.path.join(tdir,'tool_dependencies.xml'),'w') 542 depf.write(tooldepcontent)
533 depf.write(tooldepcontent) 543 depf.write('\n')
534 depf.write('\n') 544 depf.close()
535 depf.close() 545 if self.opts.input_tab: # no reproducible test otherwise? TODO: maybe..
536 if self.opts.input_tab <> 'None': # no reproducible test otherwise? TODO: maybe..
537 testdir = os.path.join(tdir,'test-data') 546 testdir = os.path.join(tdir,'test-data')
538 os.mkdir(testdir) # make tests directory 547 os.mkdir(testdir) # make tests directory
539 for i,intab in enumerate(self.opts.input_tab): 548 for i,intab in enumerate(self.opts.input_tab):
540 si = self.opts.input_tab[i] 549 si = self.opts.input_tab[i]
541 if si.find(',') <> -1: 550 if si.find(',') <> -1:
542 s = si.split(',')[0] 551 s = si.split(',')[0]
543 si = s 552 si = s
544 dest = os.path.join(testdir,os.path.basename(si)) 553 dest = os.path.join(testdir,os.path.basename(si))
545 if si <> dest: 554 if si <> dest:
546 shutil.copyfile(si,dest) 555 shutil.copyfile(si,dest)
547 if self.opts.output_tab <> None: 556 if self.opts.output_tab:
548 shutil.copyfile(self.opts.output_tab,os.path.join(testdir,self.test1Output)) 557 shutil.copyfile(self.opts.output_tab,os.path.join(testdir,self.test1Output))
549 if self.opts.make_HTML: 558 if self.opts.make_HTML:
550 shutil.copyfile(self.opts.output_html,os.path.join(testdir,self.test1HTML)) 559 shutil.copyfile(self.opts.output_html,os.path.join(testdir,self.test1HTML))
551 if self.opts.output_dir: 560 if self.opts.output_dir:
552 shutil.copyfile(self.tlog,os.path.join(testdir,'test1_out.log')) 561 shutil.copyfile(self.tlog,os.path.join(testdir,'test1_out.log'))