def run(self): ### Main run method '''Main Run method. Calls wisdom method X times.''' try:### ~ Call and print wisdoms ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### wisdoms = [] for w in range(self.stat['Wisdoms']): zenwisdom = self.wisdom() wisdoms.append('%s.' % zenwisdom) if self.getStrLC('Rest') or not self.server(): self.log.printLog('#ZEN',zenwisdom) time.sleep(self.getInt('ZenSleep')) self.dict['Output']['wisdoms'] = string.join(wisdoms,'\n') if self.server(): return string.join(wisdoms,'\n') if self.stat['Interactive'] >= 0 and self.opt['Win32']: rje.choice('\n<ENTER> to Quit') except: self.log.errorLog('Bad vibes from Zen.run()')
def run(self): ### Main run method '''Main run method.''' try:### ~ [1] ~ Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### counter = ['>>'] # List containing count times menulist = [('F','Change output file name','outfile','OutFile'),('X','Exit','return',''),('R','Run','return','')] mchoice = rje_menu.menu(self,'WormPump Menu',menulist,choicetext='Please select:',changecase=True,default='R') if mchoice == 'X': return self.printLog('#OUT','Output will be to %s' % self.info['OutFile']) self.printLog('#START','Initialising counter...') ### ~ [2] ~ Perform counts ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### wormid = None while counter[-1] != 'X': if wormid: counter.append(rje.choice('ID <ENTER> for new worm | X <ENTER> to exit | <ENTER> for "%s" pump count' % wormid,default='').upper()) else: counter.append(rje.choice('ID <ENTER> for new worm | X <ENTER> to exit',default='').upper()) if counter[-1]: wormid = counter[-1] if wormid == 'X': break self.printLog('#WORM','Worm "%s"' % wormid) counter.append(time.time()) self.deBug(counter) ### ~ [3] ~ Output results ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### head = ['Worm','Count','WormTime','AbsTime'] rje.delimitedFileOutput(self,self.info['OutFile'],headers=head,rje_backup=True) wormstart = 0.0 wormid = None wtot = 0 while counter: x = counter.pop(0) if x in ['>>','X']: continue if x: wormid = x wormstart = counter[0] wx = 0 wtot += 1 else: if not wormid: continue wx += 1 t = counter.pop(0) tt = time.localtime(t) wdata = {'Worm':wormid,'Count':wx,'WormTime':t-wormstart, #'AbsTime':'%s/%s/%s %s:%s:%s' % (tt[2],tt[1],tt[0],rje.preZero(tt[3],24),rje.preZero(tt[4],60),rje.preZero(tt[5],60))} 'AbsTime':'%s:%s:%s' % (rje.preZero(tt[3],24),rje.preZero(tt[4],60),rje.preZero(tt[5],60))} rje.delimitedFileOutput(self,self.info['OutFile'],headers=head,datadict=wdata) self.printLog('#OUT','Counts for %d worms output to %s' % (wtot,self.info['OutFile'])) rje.choice('<ENTER> to exit') except: self.errorLog(rje_zen.Zen().wisdom()) raise # Delete this if method error not terrible
def interactiveUpdate(self): ### Interactive method for updating failed genes '''Interactive method for updating failed genes.''' try: ### ~ Setup failed lists and check interactivity ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### if self.stat['Interactive'] < 0: return failures = [] for gene in self.list['Genes']: if self.dict['GeneCard'][gene]['Symbol'] == '!FAILED!': failures.append(gene) if not failures or not rje.yesNo('Try manual mapping of %d failures?' % len(failures)): return ### ~ Manually map failures onto new gene list and try extracting ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### mymapping = {} newgenes = [] try: for gene in failures: new = rje.choice('New gene symbol for > %s <?' % gene) if not new: continue mymapping[gene] = new if new not in newgenes: newgenes.append(new) except KeyboardInterrupt: if rje.yesNo('Quit GeneCards?',default='N'): raise except: raise self.processGenes(newgenes) for gene in mymapping: self.dict['GeneCard'][gene] = self.dict['GeneCard'][mymapping[gene]] return self.interactiveUpdate() except: self.log.errorLog('Problem during rje_GeneCards.interactiveUpdate()')
def setup(self): ### Main class setup method. '''Main class setup method.''' try:### ~ [1] ~ Setup Program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### self.obj['Prog'] = None prog = self.getStrLC('Name') if prog in mod: i = self.obj['ProgInfo'] = mod[prog].makeInfo() self.printLog('#PROG','%s V%s: %s' % (i.program,i.version,i.description)) progcmd = rje.getCmdList([],info=i) + self.cmd_list + ['newlog=F'] out = rje.Out(cmd_list=progcmd) out.printIntro(i) #self.debug(prog); self.debug(progcmd) if self.getBool('Help'): progcmd = mod[prog].cmdHelp(i,out,['help']+progcmd) self.printLog('#CMD','Full %s CmdList: %s' % (i.program,rje.argString(rje.tidyArgs(progcmd,nopath=self.getStrLC('Rest') and not self.dev(),purgelist=purgelist))),screen=False) #self.debug(prog); self.debug(progcmd) ## ~ [1a] ~ Make self.obj['Prog'] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## if prog in ['seqlist','rje_seqlist']: self.obj['Prog'] = rje_seqlist.SeqList(self.log,progcmd) elif prog in ['uniprot','rje_uniprot']: self.obj['Prog'] = rje_uniprot.UniProt(self.log,progcmd) elif prog in ['taxonomy','rje_taxonomy']: self.obj['Prog'] = rje_taxonomy.Taxonomy(self.log,progcmd) elif prog in ['tree','rje_tree']: self.obj['Prog'] = rje_tree.Tree(self.log,progcmd) elif prog in ['xref','rje_xref']: self.obj['Prog'] = rje_xref.XRef(self.log,progcmd) elif prog in ['seq','rje_seq']: self.obj['Prog'] = rje_seq.SeqList(self.log,progcmd) elif prog in ['mitab','rje_mitab']: self.obj['Prog'] = rje_mitab.MITAB(self.log,progcmd) elif prog in ['dbase','database']: self.obj['Prog'] = rje_dbase.DatabaseController(self.log,progcmd) elif prog in ['pydocs']: self.obj['Prog'] = rje_pydocs.PyDoc(self.log,progcmd) elif prog in ['ensembl','rje_ensembl']: self.obj['Prog'] = rje_ensembl.EnsEMBL(self.log,progcmd) elif prog in ['genbank','rje_genbank']: self.obj['Prog'] = rje_genbank.GenBank(self.log,progcmd) elif prog in ['extatic']: self.obj['Prog'] = extatic.ExTATIC(self.log,progcmd) elif prog in ['revert']: self.obj['Prog'] = revert.REVERT(self.log,progcmd) elif prog in ['fiesta']: self.obj['Prog'] = fiesta.FIESTA(self.log,progcmd) elif prog in ['gablam']: self.obj['Prog'] = gablam.GABLAM(self.log,progcmd) elif prog in ['gopher']: self.obj['Prog'] = gopher.Gopher(self.log,progcmd) elif prog in ['haqesac']: self.obj['Prog'] = haqesac.HAQESAC(self.log,progcmd) elif prog in ['multihaq']: self.obj['Prog'] = multihaq.MultiHAQ(self.log,progcmd) elif prog in ['pingu']: self.obj['Prog'] = pingu.PINGU(self.log,progcmd) elif prog in ['pacbio']: self.obj['Prog'] = rje_pacbio.PacBio(self.log,progcmd) elif prog in ['rje_zen','zen']: self.obj['Prog'] = rje_zen.Zen(self.log,progcmd) ### ~ [2] ~ Failure to recognise program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### if not self.obj['Prog']: self.printLog('#ERR','Program "%s" not recognised.' % self.getStr('Name')) if self.i() < 0: return False if rje.yesNo('Show SeqSuite help with program options?'): extracmd = cmdHelp(cmd_list=['help'])[1:] if extracmd: self.cmd_list += extracmd self._cmdList() if prog != self.getStrLC('Name'): return self.setup() self.setStr({'Name':rje.choice('Give program name (Blank or CTRL+C to quit)')}) if self.getStrLC('Name'): return self.setup() else: return False return self.obj['Prog'] # Setup successful except KeyboardInterrupt: return False except SystemExit: raise except: self.errorLog('Problem during %s setup.' % self.prog()); return False # Setup failed
def setup(self,rest=False): ### Main class setup method. '''Main class setup method.''' try:### ~ [1] ~ Setup Program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### self.obj['Prog'] = None prog = self.getStrLC('Name') if prog in mod: i = self.obj['ProgInfo'] = mod[prog].makeInfo() self.printLog('#PROG','%s V%s: %s' % (i.program,i.version,i.description)) progcmd = rje.getCmdList([],info=i) + self.cmd_list + ['newlog=F'] out = rje.Out(cmd_list=progcmd) out.printIntro(i) if self.getBool('Help'): progcmd = mod[prog].cmdHelp(i,out,['help']+progcmd) purgelist = seqsuite.purgelist self.printLog('#CMD','Full %s CmdList: %s' % (i.program,rje.argString(rje.tidyArgs(progcmd,nopath=self.getStrLC('Rest') and not self.dev(),purgelist=purgelist))),screen=False) #self.debug(prog) ## ~ [1a] ~ Make self.obj['Prog'] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## if prog in ['slimcore','rje_slimcore','core']: self.obj['Prog'] = rje_slimcore.SLiMCore(self.log,progcmd) elif prog in ['rlc','iuscore']: self.obj['Prog'] = rje_slimcore.SLiMCore(self.log,progcmd+['prog=%s' % prog]) elif prog in ['slimlist','rje_slimlist']: self.obj['Prog'] = rje_slimlist.SLiMList(self.log,progcmd) elif prog in ['slimfinder']: self.obj['Prog'] = slimfinder.SLiMFinder(self.log,progcmd) elif prog in ['qslimfinder']: self.obj['Prog'] = qslimfinder.QSLiMFinder(self.log,progcmd) elif prog in ['slimprob']: self.obj['Prog'] = slimprob.SLiMProb(self.log,progcmd) elif prog in ['slimmaker']: self.obj['Prog'] = slimmaker.SLiMMaker(self.log,progcmd) elif prog in ['slimfarmer','farm']: self.obj['Prog'] = slimfarmer.SLiMFarmer(self.log,progcmd) elif prog in ['slimbench']: self.obj['Prog'] = slimbench.SLiMBench(self.log,progcmd) elif prog in ['comparimotif']: self.obj['Prog'] = comparimotif.CompariMotif(self.log,progcmd) elif prog in ['peptcluster']: self.obj['Prog'] = peptcluster.PeptCluster(self.log,progcmd) elif prog in ['peptalign']: self.obj['Prog'] = peptcluster.PeptCluster(self.log,['peptalign=T']+progcmd+['peptdis=None']) self.obj['Prog'].dict['Output']['help'] = mod[prog].__doc__ elif prog in seqsuite.mod: seqsuiteobj = seqsuite.SeqSuite(self.log,self.cmd_list) self.obj['Prog'] = seqsuiteobj.setup() self.obj['ProgInfo'] = seqsuiteobj.obj['ProgInfo'] self.obj['Prog'].dict['Output']['help'] = seqsuite.mod[prog].__doc__ ### ~ [2] ~ Failure to recognise program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### if not self.obj['Prog']: if self.getStrLC('Name') != 'help': if not rest: self.printLog('#ERR','Program "%s" not recognised.' % self.getStr('Name')) if self.i() < 0 or rest: return False #!# Try SeqSuite? #!# if self.getStrLC('Name') == 'help' or rje.yesNo('Show SLiMSuite help with program options?'): extracmd = cmdHelp(cmd_list=['help'])[1:] if extracmd: self.cmd_list += extracmd self._cmdList() if prog != self.getStrLC('Name'): return self.setup() self.setStr({'Name':rje.choice('Give program name (Blank or CTRL+C to quit)')}) if self.getStrLC('Name'): return self.setup() else: return False return self.obj['Prog'] # Setup successful except KeyboardInterrupt: return False except SystemExit: raise except: self.errorLog('Problem during %s setup.' % self.prog()); return False # Setup failed
def setup(self): ### Main class setup method. '''Main class setup method.''' try:### ~ [1] Read in Sequences ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### seqfile = self.getStr('SeqIn') seqs = rje_seq.SeqList(log=self.log,cmd_list=['i=0']+self.cmd_list+['autofilter=F','autoload=F','seqin=None']) self.printLog('#SEQS','Loading sequences from %s' % seqfile) if not seqs.loadSeqs(seqfile=seqfile,seqtype='protein',aln=True): raise IOError('Cannot load from %s' % seqfile) seqfile = seqs.info['Name'] basefile = rje.baseFile(seqfile) if not self.getStrLC('Basefile'): self.baseFile(basefile) self.printLog('#SEQ',"%s protein sequences read from %s\n" % (str(seqs.seqNum()),seqfile),1) #?# Add option to generate alignment? self.printLog('#SEQ',"Alignment = %s. (%d aa)\n" % (seqs.opt['Aligned'],seqs.seq[0].seqLen()),1) self.dict['Output']['seqin'] = seqfile ### ~ [1] Read in Tree ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### if self.getStrLC('NSFIn'): nsfin = self.getStr('NSFIn') else: nsfin = basefile + '.nsf' while not os.path.exists(nsfin): if self.i() >= 0: nsfin = rje.choice(text='Input tree file "%s" not found. Input filename? (Blank to exit.)' % nsfin) if nsfin == '': raise KeyboardInterrupt else: raise IOError('File %s not found. Cannot load tree!' % nsfin) self.dict['Output']['nsfin'] = nsfin self.cmd_list.append('nsfin=' + nsfin) self.printLog('#TREE','Loading tree from %s' % nsfin) self.obj['Tree'] = mytree = rje_tree.Tree(log=self.log,cmd_list=['root=yes']+self.cmd_list) mytree.mapSeq(seqlist=seqs) mytree.textTree() if mytree.opt['ReRooted']: mytree.saveTree(filename='%s.nsf' % basefile) return True # Setup successful except KeyboardInterrupt: self.printLog('#CANCEL','User terminated.'); return False except: self.errorLog('Problem during %s setup.' % self.prog()); return False # Setup failed
def setup(self): ### Main class setup method. '''Main class setup method.''' try:### ~ [1] Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### self.obj['DB'] = rje_db.Database(self.log,self.cmd_list) self.db().basefile(self.basefile()) self.list['Accuracy'] = [0,1.0 - self.getNum('ErrPerBase')] ## ~ [1a] SMRTReads ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## while self.getStrLC('SMRTUnits') not in ['reads','gb','mb']: txt = 'SMRTUnits "%s" not recognised' if self.getNum('SMRTReads') < 10: smrtunits = 'Gb' elif self.getNum('SMRTReads') > 10000: smrtunits = 'reads' else: smrtunits = 'Mb' if self.i() < 0 or rje.yesNo('%s: switch to (%s) %s?' % (txt,self.getNum('SMRTReads'),smrtunits)): self.setStr({'SMRTUnits':smrtunits}) elif self.i() >0: self.setStr({'SMRTUnits':rje.choice('SMRTUnits (reads/Gb/Mb)?')}) self.printLog('#UNITS','%s => %s' % (txt,self.getStr('SMRTUnits'))) if self.getStrLC('SMRTUnits') in ['gb','mb']: smrttotal = self.getNum('SMRTReads') * {'gb':1e9,'mb':1e6}[self.getStrLC('SMRTUnits')] txt = '%s %s @ %.3f kb/read' % (self.getNum('SMRTReads'),self.getStr('SMRTUnits'),self.getNum('AvRead')/1000.0) self.setNum({'SMRTReads':smrttotal/self.getNum('AvRead')}) txt += ' => %s reads' % rje.iStr(int(self.getNum('SMRTReads'))) self.printLog('#READS',txt) ## ~ [1b] XnList ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## xnlist = [] for xn in self.list['XnList']: if xn == '': continue try: ixn = int(xn) if xn not in [ixn,'%d' % ixn]: self.printLog('#XN','"%s" -> %dX' % (xn,ixn)) if ixn == 0: self.printLog('#XN','No point in 0X output: use 1-%Coverage.') elif ixn == 1: self.printLog('#XN','No point in 1X output: use %Coverage.') else: xnlist.append(ixn) except: self.errorLog('Could not process %s as part of XnList. (Integers only.)' % xn) xnlist.sort() if xnlist: self.printLog('#XN','XnList: %sX.' % string.join(string.split('%s' % xnlist,','),'X, ')[1:-1]) self.list['XnList'] = xnlist return True # Setup successful except: self.errorLog('Problem during %s setup.' % self.prog()); return False # Setup failed
def runMain(): try: ### <0> ### Basic Setup of Program [info,out,mainlog,cmd_list] = setupProgram() ### <1> ### Load Data ## <a> ## Read in Sequences try: out.verbose(1,3,'Loading sequences...',0) seqfile = 'infile.fas' nsfin = None for cmd in cmd_list: if cmd.find('seqin=') == 0: seqfile=cmd[len('seqin='):] if cmd.find('nsfin=') == 0: nsfin = cmd[len('nsfin='):] basefile = seqfile extension = seqfile[-4:] if (extension == '.fas') or (extension == '.phy') or (extension == '.aln'): basefile = seqfile[:-4] seqs = rje_seq.SeqList(log=mainlog,cmd_list=['i=0']+cmd_list+['autofilter=F','autoload=F','seqin=None']) out.verbose(1,3,"from %s" % seqfile,1) if not seqs.loadSeqs(seqfile=seqfile,seqtype='protein',aln=True): raise seqfile = seqs.info['Name'] basefile = rje.baseFile(seqfile) mainlog.printLog('#SEQ',"%s protein sequences read from %s\n" % (str(seqs.seqNum()),seqfile),1) mainlog.printLog('#SEQ',"Alignment = %s. (%d aa)\n" % (seqs.opt['Aligned'],seqs.seq[0].seqLen()),1) except: mainlog.errorLog("Fatal run Exception during Sequence Input\n") raise ## <b> ## Read in Tree try: if not nsfin: nsfin = basefile + '.nsf' while not os.path.exists(nsfin): if out.stat['Interactive'] >= 0: nsfin = rje.choice(text='Input tree file "%s" not found. Input filename? (Blank to exit.)' % nsfin) if nsfin == '': raise KeyboardInterrupt else: mainlog.log.errorLog('File %s not found. Cannot load tree!' % nsfin,printerror=False,quitchoice=True) raise cmd_list.append('nsfin=' + nsfin) out.verbose(1,3,'Loading tree from %s...' % nsfin,1) mytree = rje_tree.Tree(log=mainlog,cmd_list=['root=yes']+cmd_list) mytree.mapSeq(seqlist=seqs) mytree.textTree() if mytree.opt['ReRooted']: mytree.saveTree(filename='%s.nsf' % basefile) except KeyboardInterrupt: mainlog.errorLog("User terminated.\n") raise except: mainlog.errorLog("Fatal run Exception during Tree Input\n") raise ### <2> ### GASP try: ## <a> ## InDel Tree Setup indeltree = None for cmd in cmd_list: if cmd.find('indeltree=') == 0: indeltree=cmd[len('indeltree='):] ## <b> ## GASP if indeltree == None or mytree.node[-1].obj['Sequence'] == None: # Perform GASP out.verbose(0,2,'',3) mainlog.printLog('#SEQ','GASP: Gapped Ancestral Sequence Prediction',1) if basefile == 'infile': basefile = 'gasp' mygasp = rje_ancseq.Gasp(tree=mytree,ancfile='%s' % basefile,cmd_list=cmd_list,log=mainlog) out.verbose(0,2,'%s' % mygasp.details(),1) if out.stat['Interactive'] > 0: if rje.yesNo('Use these parameters?') == False: mygasp.edit() mygasp.gasp() out.verbose(0,1,"\n\nGASP run completed OK!",2) ## <c> ## InDel Tree if indeltree: mytree.indelTree(filename=indeltree) except KeyboardInterrupt: mainlog.errorLog("User terminated.\n") raise except: mainlog.errorLog("Fatal run Exception during GASP\n") raise ### <X> ### End except KeyboardInterrupt: mainlog.errorLog("User terminated.\n") except: print "Unexpected error:", sys.exc_info()[0] mainlog.printLog('#LOG', "%s V:%s End: %s\n" % (info.program, info.version, time.asctime(time.localtime(time.time()))), 1)
def setup(self): ### Main class setup method. '''Main class setup method.''' try: ### ~ [1] Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### self.obj['DB'] = rje_db.Database(self.log, self.cmd_list) self.db().basefile(self.basefile()) self.list['Accuracy'] = [0, 1.0 - self.getNum('ErrPerBase')] ## ~ [1a] SMRTReads ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## while self.getStrLC('SMRTUnits') not in ['reads', 'gb', 'mb']: txt = 'SMRTUnits "%s" not recognised' if self.getNum('SMRTReads') < 10: smrtunits = 'Gb' elif self.getNum('SMRTReads') > 10000: smrtunits = 'reads' else: smrtunits = 'Mb' if self.i() < 0 or rje.yesNo( '%s: switch to (%s) %s?' % (txt, self.getNum('SMRTReads'), smrtunits)): self.setStr({'SMRTUnits': smrtunits}) elif self.i() > 0: self.setStr( {'SMRTUnits': rje.choice('SMRTUnits (reads/Gb/Mb)?')}) self.printLog('#UNITS', '%s => %s' % (txt, self.getStr('SMRTUnits'))) if self.getStrLC('SMRTUnits') in ['gb', 'mb']: smrttotal = self.getNum('SMRTReads') * { 'gb': 1e9, 'mb': 1e6 }[self.getStrLC('SMRTUnits')] txt = '%s %s @ %.3f kb/read' % (self.getNum('SMRTReads'), self.getStr('SMRTUnits'), self.getNum('AvRead') / 1000.0) self.setNum({'SMRTReads': smrttotal / self.getNum('AvRead')}) txt += ' => %s reads' % rje.iStr(int(self.getNum('SMRTReads'))) self.printLog('#READS', txt) ## ~ [1b] XnList ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## xnlist = [] for xn in self.list['XnList']: if xn == '': continue try: ixn = int(xn) if xn not in [ixn, '%d' % ixn]: self.printLog('#XN', '"%s" -> %dX' % (xn, ixn)) if ixn == 0: self.printLog( '#XN', 'No point in 0X output: use 1-%Coverage.') elif ixn == 1: self.printLog('#XN', 'No point in 1X output: use %Coverage.') else: xnlist.append(ixn) except: self.errorLog( 'Could not process %s as part of XnList. (Integers only.)' % xn) xnlist.sort() if xnlist: self.printLog( '#XN', 'XnList: %sX.' % string.join(string.split('%s' % xnlist, ','), 'X, ')[1:-1]) self.list['XnList'] = xnlist return True # Setup successful except: self.errorLog('Problem during %s setup.' % self.prog()) return False # Setup failed
def farmHAQ(self): ### Uses SLiMFarmer to farm out the HAQESAC runs '''Uses SLiMFarmer to farm out the HAQESAC runs.''' try:### ~ [0] Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### batfile = os.path.abspath(rje.makePath('%shaqesac.bat' % self.info['HaqDir'],wholepath=True)) self.printLog('#FARM',batfile) if not rje.exists(batfile): raise IOError('Cannot find %s' % batfile) farmcmd = ['subjobs=%s' % batfile,'farm=batch','qsub=F','i=-1','runpath=%s' % os.path.abspath(self.info['HaqDir'])] if self.opt['MultiHAQ']: haqfarm = ['First round','Second round'] else: haqfarm = ['Complete run'] ### ~ [1] Peform HAQESAC runs ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### for farmrun in haqfarm: self.printLog('#CHDIR','Changing directory for %s farming: %s' % (farmrun,self.info['HaqDir'])) os.chdir(self.info['HaqDir']) farmer = slimfarmer.SLiMFarmer(self.log,self.cmd_list+farmcmd) farmer.slimFarm() os.chdir(self.info['RunPath']) self.printLog('#CHDIR','Changed directory post-farming: %s' % self.info['RunPath']) self.printLog('#FARM','HAQESAC %s farming complete.' % farmrun) return True #!# Add identifying and skipping of partial runs. for seq in self.seqs(): ## ~ [1a] Check AutoSkip ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## acc = seq.info['AccNum'] if finalrun and acc in processed and (self.opt['AutoSkip'] or (self.i() >=0 and rje.yesNo('%s already covered by previous HAQESAC. Skip?' % seq.shortName()))): self.printLog('#SKIP','%s already covered by previous HAQESAC: Skipped' % seq.shortName()); continue ## ~ [1b] Check Whether to run (re-runs and low sequence number) ~~~~~~~~~~~~~~~~~~ ## logfile = rje.makePath('%s%s.log' % (self.info['HaqDir'],acc),wholepath=True) infile = rje.makePath('%s%s.fas' % (self.info['HaqDir'],acc),wholepath=True) pkfile = rje.makePath('%s%s.pickle' % (self.info['HaqDir'],acc),wholepath=True) pkzfile = rje.makePath('%s%s.pickle.gz' % (self.info['HaqDir'],acc),wholepath=True) if not os.path.exists(infile): self.printLog('#SKIP','%s input file %s not found: Skipped' % (seq.shortName(),infile)); continue if not finalrun and not self.opt['Force'] and rje.isYounger(pkzfile,infile) == pkzfile: self.printLog('#SKIP','%s run detected: Skipped' % seq.shortName()); continue if not finalrun and not self.opt['Force'] and rje.isYounger(pkfile,infile) == pkfile: self.printLog('#SKIP','%s run detected: Skipped' % seq.shortName()); continue inseqx = rje_seq.SeqCount(self,infile) if inseqx < 2: self.printLog('#SKIP','Only one sequence found in %s: Skipped' % (infile)); continue ## ~ [1c] Pause if running in Chaser Mode and no Pickle ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## pickled = os.path.exists(pkfile) or os.path.exists('%s.gz' % pkfile); tm = 0 while secondrun and self.opt['Chaser'] and not pickled: self.progLog('#WAIT','No %s pickle. Sleeping for %d min.' % (acc,tm)) time.sleep(60*tm); tm += 1 pickled = os.path.exists(pkfile) or os.path.exists('%s.gz' % pkfile) if not pickled: try: rje.choice('Press <ENTER> to try again, or <CTRL+C> to Quit') except: self.printLog('#PICKLE','No %s pickle.' % (acc,tm)) self.printLog('\r#MULTI','Exiting multiHAQ "Chaser" run.'); return ## ~ [1d] Run HAQESAC ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## runhaqesac = True pngfile = rje.makePath('%s%s.png' % (self.info['HaqDir'],acc),wholepath=True) if not self.force() and rje.exists(pngfile): self.printLog('#SKIP','Found evidence of completed run: %s (force=F). Skipping.' % pngfile) runhaqesac = False ancfile = rje.makePath('%s%s.anc.fas' % (self.info['HaqDir'],acc),wholepath=True) if not self.force() and rje.exists(ancfile): self.printLog('#SKIP','Found evidence of completed run: %s (force=F). Skipping.' % ancfile) runhaqesac = False except: os.chdir(self.info['RunPath']) self.errorLog('Major problem with MultiHAQ.farmHAQ',quitchoice=True)
def farmHAQ(self): ### Uses SLiMFarmer to farm out the HAQESAC runs '''Uses SLiMFarmer to farm out the HAQESAC runs.''' try: ### ~ [0] Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### batfile = os.path.abspath( rje.makePath('%shaqesac.bat' % self.info['HaqDir'], wholepath=True)) self.printLog('#FARM', batfile) if not rje.exists(batfile): raise IOError('Cannot find %s' % batfile) farmcmd = [ 'subjobs=%s' % batfile, 'farm=batch', 'qsub=F', 'i=-1', 'runpath=%s' % os.path.abspath(self.info['HaqDir']) ] if self.opt['MultiHAQ']: haqfarm = ['First round', 'Second round'] else: haqfarm = ['Complete run'] ### ~ [1] Peform HAQESAC runs ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### for farmrun in haqfarm: self.printLog( '#CHDIR', 'Changing directory for %s farming: %s' % (farmrun, self.info['HaqDir'])) os.chdir(self.info['HaqDir']) farmer = slimfarmer.SLiMFarmer(self.log, self.cmd_list + farmcmd) farmer.slimFarm() os.chdir(self.info['RunPath']) self.printLog( '#CHDIR', 'Changed directory post-farming: %s' % self.info['RunPath']) self.printLog('#FARM', 'HAQESAC %s farming complete.' % farmrun) return True #!# Add identifying and skipping of partial runs. for seq in self.seqs(): ## ~ [1a] Check AutoSkip ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## acc = seq.info['AccNum'] if finalrun and acc in processed and ( self.opt['AutoSkip'] or (self.i() >= 0 and rje.yesNo( '%s already covered by previous HAQESAC. Skip?' % seq.shortName()))): self.printLog( '#SKIP', '%s already covered by previous HAQESAC: Skipped' % seq.shortName()) continue ## ~ [1b] Check Whether to run (re-runs and low sequence number) ~~~~~~~~~~~~~~~~~~ ## logfile = rje.makePath('%s%s.log' % (self.info['HaqDir'], acc), wholepath=True) infile = rje.makePath('%s%s.fas' % (self.info['HaqDir'], acc), wholepath=True) pkfile = rje.makePath('%s%s.pickle' % (self.info['HaqDir'], acc), wholepath=True) pkzfile = rje.makePath('%s%s.pickle.gz' % (self.info['HaqDir'], acc), wholepath=True) if not os.path.exists(infile): self.printLog( '#SKIP', '%s input file %s not found: Skipped' % (seq.shortName(), infile)) continue if not finalrun and not self.opt['Force'] and rje.isYounger( pkzfile, infile) == pkzfile: self.printLog('#SKIP', '%s run detected: Skipped' % seq.shortName()) continue if not finalrun and not self.opt['Force'] and rje.isYounger( pkfile, infile) == pkfile: self.printLog('#SKIP', '%s run detected: Skipped' % seq.shortName()) continue inseqx = rje_seq.SeqCount(self, infile) if inseqx < 2: self.printLog( '#SKIP', 'Only one sequence found in %s: Skipped' % (infile)) continue ## ~ [1c] Pause if running in Chaser Mode and no Pickle ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## pickled = os.path.exists(pkfile) or os.path.exists( '%s.gz' % pkfile) tm = 0 while secondrun and self.opt['Chaser'] and not pickled: self.progLog( '#WAIT', 'No %s pickle. Sleeping for %d min.' % (acc, tm)) time.sleep(60 * tm) tm += 1 pickled = os.path.exists(pkfile) or os.path.exists( '%s.gz' % pkfile) if not pickled: try: rje.choice( 'Press <ENTER> to try again, or <CTRL+C> to Quit' ) except: self.printLog('#PICKLE', 'No %s pickle.' % (acc, tm)) self.printLog('\r#MULTI', 'Exiting multiHAQ "Chaser" run.') return ## ~ [1d] Run HAQESAC ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## runhaqesac = True pngfile = rje.makePath('%s%s.png' % (self.info['HaqDir'], acc), wholepath=True) if not self.force() and rje.exists(pngfile): self.printLog( '#SKIP', 'Found evidence of completed run: %s (force=F). Skipping.' % pngfile) runhaqesac = False ancfile = rje.makePath('%s%s.anc.fas' % (self.info['HaqDir'], acc), wholepath=True) if not self.force() and rje.exists(ancfile): self.printLog( '#SKIP', 'Found evidence of completed run: %s (force=F). Skipping.' % ancfile) runhaqesac = False except: os.chdir(self.info['RunPath']) self.errorLog('Major problem with MultiHAQ.farmHAQ', quitchoice=True)
def run(self): ### Main run method '''Main run method.''' try:### ~ [1] ~ Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### infile = self.getStr('InFile') while not rje.exists(infile): infile = rje.choice('File "%s" not found. Input file name? (Blank to quit):' % infile) if not infile: return self.printLog('#QUIT','Execution terminated!') db = rje_db.Database(self.log,self.cmd_list) db.basefile(rje.baseFile(infile)) sdb = db.addTable(infile,mainkeys='#',delimit='\t',name='SPF.Mod') levels = {'Level_1':'k','Level_2':'p','Level_3':'c','Level_4':'o','Level_5':'f','Level_6':'g','Level_7':'s'} # k__Bacteria p__Proteobacteria c__Alphaproteobacteria o__Rhodospirillales f__Rhodospirillaceae g__ s__ denovo44 # Unassigned unclassified unclassified unclassified unclassified unclassified unclassified denovo49 ### ~ [1] Modify Text ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### dupnames = [] parents = {} # Parent for each term renamed = [] ex = 0.0; etot = sdb.entryNum() for entry in sdb.entries(): self.progLog('\r#SPF','Modifying SPF content: %.1f%%' % (ex/etot)); ex += 100.0 taxon = '' parent = '' #self.debug(entry) for lvl in ['Level_1','Level_2','Level_3','Level_4','Level_5','Level_6','Level_7']: entry[lvl] = string.replace(entry[lvl],'unidentified','unclassified') #entry[lvl] = string.replace(entry[lvl],'Incertae_sedis','Incertae_sedis-%s' % levels[lvl]) null = '%s__' % levels[lvl] #self.bugPrint(null) #self.bugPrint(entry[lvl]) if entry[lvl] in [null,'Unassigned','unclassified','%sunclassified' % null,'%sunidentified' % null,'%sunculturedfungus' % null,'%sIncertae_sedis' % null,'%sunclassified_sp.' % null]: if not taxon or taxon.endswith('unclassified'): entry[lvl] = '%sunclassified' % null #elif taxon.endswith('unassigned)'): entry[lvl] = '%s%s' % (null,taxon[3:]) #elif taxon.endswith('unassigned)'): entry[lvl] = '%s(%s;%s-unassigned)' % (null,string.split(taxon,'(')[1][:-1],levels[lvl]) elif taxon.endswith('unassigned)'): entry[lvl] = '%s%s;%s-unassigned)' % (null,taxon[3:][:-1],levels[lvl]) else: entry[lvl] = '%s%s(%s-unassigned)' % (null,taxon[3:],levels[lvl]) if entry[lvl] in parents: #self.debug(parents[entry[lvl]]) if parent in parents[entry[lvl]]: entry[lvl] = parents[entry[lvl]][parent] else: self.bugPrint(entry[lvl]) self.bugPrint(parents[entry[lvl]]) renamed.append(entry[lvl]) newtax = '%s%d' % (entry[lvl],renamed.count(entry[lvl])) self.warnLog('%s had multiple parents (%s & %s) -> %s' % (entry[lvl],string.join(parents[entry[lvl]],'|'),parent,newtax)) parents[newtax] = {parent:newtax} parents[entry[lvl]][parent] = newtax entry[lvl] = newtax self.deBug(parents[entry[lvl]]) elif parent: parents[entry[lvl]] = {parent:entry[lvl]} parent = entry[lvl] if entry[lvl][3:] == taxon[3:]: if (entry[lvl],taxon) not in dupnames: dupnames.append((entry[lvl],taxon)) #self.bugPrint(entry[lvl]) taxon = entry[lvl] #self.debug(entry) #self.debug(parents) self.printLog('\r#SPF','Modifying SPF content complete.') dupnames.sort() for (dupA,dupB) in dupnames: self.warnLog('Duplicate taxa names: %s & %s' % (dupA,dupB)) ### ~ [2] Save to file ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### sdb.saveToFile(savefields=sdb.list['Fields'][1:]) ### ~ [3] Compress to different taxonomic levels ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### compress = ['Level_1','Level_2','Level_3','Level_4','Level_5','Level_6','Level_7','#'] dump = compress.pop(-1) rules = {'Observation Ids':'list',dump:'str'} sdb.dropField('Observation Ids') while compress: sdb.compress(compress,rules=rules,default='sum',best=[],joinchar='|') #if dump == '#': sdb.dropField(dump) sdb.saveToFile('%s.SPF.%s.%s.spf' % (rje.baseFile(infile),compress[-1],levels[compress[-1]])) dump = compress.pop(-1); rules[dump] = 'list' return except: self.errorLog(self.zen()) raise # Delete this if method error not terrible
def runMain(): try: ### <0> ### Basic Setup of Program [info, out, mainlog, cmd_list] = setupProgram() ### <1> ### Load Data ## <a> ## Read in Sequences try: out.verbose(1, 3, 'Loading sequences...', 0) seqfile = 'infile.fas' nsfin = None for cmd in cmd_list: if cmd.find('seqin=') == 0: seqfile = cmd[len('seqin='):] if cmd.find('nsfin=') == 0: nsfin = cmd[len('nsfin='):] basefile = seqfile extension = seqfile[-4:] if (extension == '.fas') or (extension == '.phy') or (extension == '.aln'): basefile = seqfile[:-4] seqs = rje_seq.SeqList( log=mainlog, cmd_list=['i=0'] + cmd_list + ['autofilter=F', 'autoload=F', 'seqin=None']) out.verbose(1, 3, "from %s" % seqfile, 1) if not seqs.loadSeqs(seqfile=seqfile, seqtype='protein', aln=True): raise seqfile = seqs.info['Name'] basefile = rje.baseFile(seqfile) mainlog.printLog( '#SEQ', "%s protein sequences read from %s\n" % (str(seqs.seqNum()), seqfile), 1) mainlog.printLog( '#SEQ', "Alignment = %s. (%d aa)\n" % (seqs.opt['Aligned'], seqs.seq[0].seqLen()), 1) except: mainlog.errorLog("Fatal run Exception during Sequence Input\n") raise ## <b> ## Read in Tree try: if not nsfin: nsfin = basefile + '.nsf' while not os.path.exists(nsfin): if out.stat['Interactive'] >= 0: nsfin = rje.choice( text= 'Input tree file "%s" not found. Input filename? (Blank to exit.)' % nsfin) if nsfin == '': raise KeyboardInterrupt else: mainlog.log.errorLog( 'File %s not found. Cannot load tree!' % nsfin, printerror=False, quitchoice=True) raise cmd_list.append('nsfin=' + nsfin) out.verbose(1, 3, 'Loading tree from %s...' % nsfin, 1) mytree = rje_tree.Tree(log=mainlog, cmd_list=['root=yes'] + cmd_list) mytree.mapSeq(seqlist=seqs) mytree.textTree() if mytree.opt['ReRooted']: mytree.saveTree(filename='%s.nsf' % basefile) except KeyboardInterrupt: mainlog.errorLog("User terminated.\n") raise except: mainlog.errorLog("Fatal run Exception during Tree Input\n") raise ### <2> ### GASP try: ## <a> ## InDel Tree Setup indeltree = None for cmd in cmd_list: if cmd.find('indeltree=') == 0: indeltree = cmd[len('indeltree='):] ## <b> ## GASP if indeltree == None or mytree.node[-1].obj[ 'Sequence'] == None: # Perform GASP out.verbose(0, 2, '', 3) mainlog.printLog('#SEQ', 'GASP: Gapped Ancestral Sequence Prediction', 1) if basefile == 'infile': basefile = 'gasp' mygasp = rje_ancseq.Gasp(tree=mytree, ancfile='%s' % basefile, cmd_list=cmd_list, log=mainlog) out.verbose(0, 2, '%s' % mygasp.details(), 1) if out.stat['Interactive'] > 0: if rje.yesNo('Use these parameters?') == False: mygasp.edit() mygasp.gasp() out.verbose(0, 1, "\n\nGASP run completed OK!", 2) ## <c> ## InDel Tree if indeltree: mytree.indelTree(filename=indeltree) except KeyboardInterrupt: mainlog.errorLog("User terminated.\n") raise except: mainlog.errorLog("Fatal run Exception during GASP\n") raise ### <X> ### End except KeyboardInterrupt: mainlog.errorLog("User terminated.\n") except: print "Unexpected error:", sys.exc_info()[0] mainlog.printLog( '#LOG', "%s V:%s End: %s\n" % (info.program, info.version, time.asctime(time.localtime(time.time()))), 1)
def badasp(out,mainlog,cmd_list,tree=None): ### Main BADASP Method ''' Main BADASP Method. Automated run if interactive < 1 <1> Load Sequences and Tree <2> Define Subfamilies <3> GASP Ancestral Sequence Prediction <4> Peform Functional Specificity and Sequence Conservation Calculations <5> Output Results ''' try: ### <0> ### Setup _seqfile = None _treefile = None append_file = None basefile = None for cmd in cmd_list: if cmd.find('seqin=') == 0: _seqfile = cmd[len('seqin='):] if _seqfile[-4] == '.': _seqfile = _seqfile[:-4] if cmd.find('useanc=') == 0: _seqfile = cmd[len('useanc='):] if _seqfile[-8:] == '.anc.fas': _seqfile = _seqfile[:-8] if cmd.find('nsfin=') == 0: _treefile = cmd[len('nsfin='):] if cmd.find('append=') == 0: append_file = cmd[len('append='):] if cmd.find('basefile=') == 0: basefile = cmd[len('basefile='):] if _seqfile and os.path.exists('%s.grp' % _seqfile): cmd_list.append('group=%s.grp' % _seqfile) if _seqfile and _treefile == None: if rje.checkForFile('%s.nwk' % _seqfile): _treefile = '%s.nwk' % _seqfile else: _treefile = '%s.nsf' % _seqfile out.verbose(0,2,'Looking for treefile %s.' % _treefile,1) if rje.checkForFile(_treefile): cmd_list.append('nsfin=%s' % _treefile) if tree == None: mainlog.verbose(0,1,'Tree: %s' % cmd_list,2) tree = rje_tree.Tree(log=mainlog,cmd_list=cmd_list) #tree._setupFromCmd() if tree.stat['MinFamNum'] < 2: tree.stat['MinFamNum'] = 2 ### <1> ### Load Sequences and Tree while out.stat['Interactive'] > 0 or tree.obj['SeqList'] == None: tree = rje_tree.treeMenu(out,mainlog,['root=yes']+cmd_list,tree) if tree.obj['SeqList'] and tree.opt['Rooted']: break else: print '\n ** Must have loaded sequences and a rooted tree. ** \n' if out.stat['Interactive'] < 0 or rje.yesNo('Quit BADASP?',default='N'): sys.exit() basename = tree.obj['SeqList'].info['Name'] if basename[-4:] == '.fas': basename = basename[:-4] if basename[-4:] == '.anc': basename = basename[:-4] if basefile: basename = basefile except SystemExit: raise except: mainlog.errorLog('Major Error in badasp loading sequences and tree',True) try: ### <2> ### Define Subfamilies while out.stat['Interactive'] > 0 or tree.groupNum() < 2: tree.treeGroup(callmenu=True) if tree.groupNum() >= 2: break else: mainlog.errorLog('Must have at least two subfamilies for specificity analyses.',printerror=False) if out.stat['Interactive'] < 0 or rje.yesNo('Continue without specificity analyses?'): cmd_list.append('funcspec=') break elif rje.yesNo('Abort BADASP?'): sys.exit() except SystemExit: raise except: mainlog.errorLog('Major Error in BADASP subfamilies',True) try: ### <3> ### GASP Ancestral Sequence Prediction if tree.node[-1].obj['Sequence'] == None: # No ancseq loaded while out.stat['Interactive'] > 0 and rje.yesNo('Use %s for output filenames?' % basename) == False: basename = rje.choice('FILEname (FILE.anc.fas, FILE.anc.nsf, FILE.txt)?: ', default=basename) mygasp = rje_ancseq.Gasp(tree=tree,ancfile=basename,cmd_list=cmd_list,log=mainlog) out.verbose(0,2,'%s' % mygasp.details(),1) if out.stat['Interactive'] > 0: if rje.yesNo('Use these parameters?') == False: mygasp.edit() mygasp.gasp() except: mainlog.errorLog('Major Error in BADASP GASP',True) try: ### <4> ### Peform Functional Specificity and Sequence Conservation Calculations _stage = '<4> Specificity/Conservation Analyses' aaprop = rje_aaprop.AAPropMatrix(log=mainlog,cmd_list=cmd_list) query = tree.obj['SeqList'].obj['QuerySeq'] ## <a> ## Chosen Methods _stage = '<4a> Specificity/Conservation Analyses - Chosen Methods' funcspec = rje_specificity.methodlist # ['BAD','BADN','BADX'] seqcon = rje_conseq.methodlist # ['info'] for cmd in cmd_list: if cmd.find('funcspec=') == 0: funcspec = cmd[9:].split(',') if cmd.find('seqcon=') == 0: seqcon = cmd[len('seqcon='):].split(',') if 'all' in funcspec: funcspec = rje_specificity.methodlist if 'all' in seqcon: seqcon = rje_conseq.methodlist for method in ['BADX','BADN','QPCon_Mean','QPCon_Abs','QPCon_Mean_All']: while method in funcspec and query == None: if rje.yesNo('Method %s needs query but none given. Drop %s from specificity methods?' % (method,method)): funcspec.remove(method) break for seq in tree.obj['SeqList'].seq: if rje.yesNo('Method %s needs query but none given. Use sequence 1 (%s)?' % (method,seq.shortName()),default='N'): query = seq tree.obj['SeqList'].obj['Query'] = seq break while method in seqcon and query == None: if rje.yesNo('Method %s needs query but none given. Drop %s from conservation methods?' % (method,method)): seqcon.remove(method) break for seq in tree.obj['SeqList'].seq: if rje.yesNo('Method %s needs query but none given. Use sequence 1 (%s)?' % (method,seq.shortName()),default='N'): query = seq tree.obj['SeqList'].obj['Query'] = seq break qname = query if query: qname = query.info['Name'] out.verbose(0,3,'\nQuery = %s' % qname,2) ## <b> ## Spec Calculations _stage = '<4b> Specificity Calculations' specmatrix = rje_specificity.FuncSpec(log=mainlog,cmd_list=cmd_list,tree=tree,aaprop=aaprop) specmatrix.calcScore(query=query,methods=funcspec) ## <c> ## Conservation Calculations _stage = '<4c> Specificity/Conservation Analyses - Conservation Calculations' conseq = rje_conseq.SeqStat(log=mainlog,cmd_list=cmd_list,tree=tree,aaprop=aaprop) conseq.calcScore(query=query,methods=seqcon) ### Sends appropriate seqlist to self.calcScore() ## <d> ## Special Case: QPCon vs All seqs _stage = '<4d> Specificity/Conservation Analyses - QPCon vs All' qpconall = [] #if 'QPCon_Abs_All' in seqcon and query: # qpconall.append('QPCon_Abs') if 'QPCon_Mean_All' in seqcon and query: qpconall.append('QPCon_Mean') for qp in qpconall: conseq.score['%s_All' % qp] = conseq.score[qp] if conseq.alnwin.has_key(qp): conseq.alnwin['%s_All' % qp] = conseq.alnwin[qp] if conseq.qrywin.has_key(qp): conseq.qrywin['%s_All' % qp] = conseq.qrywin[qp] if conseq.rank.has_key(qp): conseq.rank['%s_All' % qp] = conseq.rank[qp] if conseq.alnrankwin.has_key(qp): conseq.alnrankwin['%s_All' % qp] = conseq.alnrankwin[qp] if conseq.qryrankwin.has_key(qp): conseq.qryrankwin['%s_All' % qp] = conseq.qryrankwin[qp] _stage = '<4d> Specificity/Conservation Analyses - FamQP' famqp = [] if 'QPCon_Mean' in seqcon: famqp.append('QPCon_Mean') if 'QPCon_Abs' in seqcon: famqp.append('QPCon_Abs') if len(famqp) > 0 and query: #!# And subfam option? qseq = [] for fam in tree.subfam: for node in tree._nodeClade(fam): if query == node.obj['Sequence']: for qnode in tree._nodeClade(fam): qseq.append(qnode.obj['Sequence']) conseq.calcScore(query=query,seqlist=qseq,methods=famqp) ### Sends appropriate seqlist to self.calcScore() except: mainlog.errorLog('Major Error in BADASP Specificity Analysis (%s):' % _stage,True) try: ### <5> ### Full Output Results _stage = '<5> Full Output' # This output is in a tab- or comma-delimited file for easy manipulation or viewing with other programs. # (1) statistics for a given residue; # (2) statistics for a given window size across # - (a) the whole alignment, (node=None) # - (b) the Query protein of interest (if given) and (node=QueryNode) # - (c) the ancestral sequence of each subfamily; (node=ancnode) # (3) Predicted ancestral sequences at # - (a) the root and # - (b) the ancestor of each subfamily. delimit = rje.getDelimit(cmd_list) ## <a> ## Setup _stage = '<5a> Output - Setup' rankout = specmatrix.opt['Rank'] #tree._regenerateSeqList(tree.obj['SeqList'],tree.node) root = tree.node[-1].obj['Sequence'] #!# At some point, make sure this is the most ancient duplication! out.verbose(0,3,'\nBADASP Results Output (%s.badasp) ...' % basename,0) ## <b> ## Header _stage = '<5b> Output - Header' _header = True if append_file: if rje.checkForFile(append_file): _header = False BADASP = open(append_file, 'a') else: BADASP = open('%s.badasp' % basename, 'w') BADASP.write("BADASP Output: %s\n" % (time.asctime(time.localtime(time.time())))) BADASP.write('%s\n\n' % cmd_list) header = ['aln_pos','anc_aa'] # Aln Pos and AA alnlen = 0 statlist = funcspec + seqcon _stage = '<5b-i> Output - Header Query' if query: header += ['qry_pos','qry_aa'] # Qry Pos and AA _stage = '<5b-ii> Output - Header Subfam' for f in range(len(tree.subfam)): header += ['fam%d_pos' % (f+1),'fam%d_aa' % (f+1)] # Subfam Pos and AA for func in statlist: _stage = '<5b-iii> Output - Header %s' % func statobj = statObj(method=func,objlist=[specmatrix,conseq]) fs = func.lower() alnlen = len(statobj.score[func]) header.append(fs) # Score if rankout: header.append('%s_rank' % fs) # Rank if statobj.stat['WinSize'] > 1: header.append('%s_alnwin' % fs) # Full align window if rankout: header.append('%s_alnrankwin' % fs) # Rank if query: header.append('%s_qrywin' % fs) # Qry window if rankout: header.append('%s_qryrankwin' % fs) # Rank if func in funcspec: for f in range(len(tree.subfam)): header.append('%s_fam%d_win' % (fs,f+1)) # Subfam windows if rankout: header.append('%s_fam%d_rankwin' % (fs,f+1)) # Subfam windows #if _header: BADASP.write('%s\n' % string.join(header, delimit)) out.verbose(1,3,'%s...' % string.join(header, delimit),0) ## <c> ## Stats _stage = '<5c> Stats' qr = 0 # Qry pos fr = [0] * len(tree.subfam) # List of subfam positions aa = '' # Root aa qa = '' # Qry aa fa = [''] * len(tree.subfam) # List of subfam aas for r in range(alnlen): # <i> # Positions and aas _stage = '<5c-i> Output - Stats, positions & aas' aa = root.info['Sequence'][r] if query: qa = query.info['Sequence'][r] if qa != '-': qr += 1 for f in range(len(tree.subfam)): fa[f] = tree.subfam[f].obj['Sequence'].info['Sequence'][r] if fa[f] != '-': fr[f] += 1 # <ii> # Positions and AAs ii _stage = '<5c-ii> Output - Pos & AA ii' line = ['%d' % (r+1), aa] # Aln Pos and AA if query: if qa == '-': line += ['-',qa] # Qry Pos and AA else: line += ['%d' % qr,qa] # Qry Pos and AA for f in range(len(tree.subfam)): if fa[f] == '-': line += ['-',fa[f]] # Subfam Pos and AA else: line += ['%d' % fr[f],fa[f]] # Subfam Pos and AA # <iii> # Stats _stage = '<5c-iii> Output - Stats' for func in statlist: statobj = statObj(method=func,objlist=[specmatrix,conseq]) fs = func.lower() line.append(str(statobj.score[func][r])) # Score if rankout: line.append(str(statobj.rank[func][r])) # Rank if specmatrix.stat['WinSize'] > 1: line.append(str(statobj.alnwin[func][r])) # Full align window if rankout: line.append(str(statobj.alnrankwin[func][r])) # Rank if query: line.append(str(statobj.qrywin[func][r])) # Qry window if rankout: line.append(str(statobj.qryrankwin[func][r])) # Rank if func in funcspec: for f in range(len(tree.subfam)): line.append(str(statobj.famwin[func][tree.subfam[f]][r])) # Subfam windows if rankout: line.append(str(statobj.famrankwin[func][tree.subfam[f]][r])) # Subfam windows # <iv> # Writing _stage = '<5c-iv> Output - Writing' BADASP.write('%s\n' % string.join(line, delimit)) BADASP.close() out.verbose(0,2,'Done!',2) except: mainlog.errorLog('Fatal Error in BADASP Full output (%s):' % _stage,True) BADASP.write('%s\n' % string.join(line, delimit)) BADASP.close() try: ### <6> ### Partial Results Output _stage = '<6> Partial Output' ## <a> ## Setup _stage = '<6a> Output - Setup' # statlist & alnlen from above _part_append = False if out.stat['Interactive'] > 0 and rje.yesNo('Output additional, filtered results?',default='N'): partfile = rje.choice('Name for partial results file?:','%s.partial.badasp' % basename,confirm=True) if rje.checkForFile(partfile) and rje.yesNo('File %s exists. Append file without headers?' % partfile): _part_append = True else: return if rje.yesNo('Filter output columns?',default='N'): if rje.yesNo('Output query details (pos,aa & win)?') == False: query = None f = 1 for fam in tree.subfam[0:]: if rje.yesNo('Output subfam %d (%s) details (pos,aa & win)?' % (f,fam.info['CladeName'])) == False: tree.subfam.remove(fam) f += 1 for func in statlist[0:]: if rje.yesNo('Output %s results?' % func) == False: statlist.remove(func) alnout = [True] * alnlen if rje.yesNo('Filter Rows by Results VALUES?'): out.verbose(0,0,'Initial Defaults are minmum values. Accept intital default for no filtering of given Stat.',1) for stat in statlist: ### Filter by value? ### statobj = statObj(method=stat,objlist=[specmatrix,conseq]) scores = statobj.score[stat][0:] scores.sort() cutoff = rje.getFloat('Min. value for %s?:' % stat,default='%f' % scores[0],confirm=True) for r in range(alnlen): if statobj.score[stat][r] < cutoff: alnout[r] = False if rankout and rje.yesNo('Filter Rows by Results RANKS?'): out.verbose(0,0,'Ranks range from 0 (low) to 1 (high).',1) for stat in statlist: ### Filter by Rank? ### statobj = statObj(method=stat,objlist=[specmatrix,conseq]) cutoff = rje.getFloat('Min. rank for %s?:' % stat,default='0.0',confirm=True) for r in range(alnlen): if statobj.rank[stat][r] < cutoff: alnout[r] = False out.verbose(0,3,'\nBADASP Partial Results Output (%s) ...' % partfile,0) ## <b> ## Header _stage = '<6b> Partial Output - Header' if _part_append: BADASP = open(partfile, 'a') else: BADASP = open(partfile, 'w') BADASP.write("Partial BADASP Output: %s\n" % (time.asctime(time.localtime(time.time())))) BADASP.write('%s\n\n' % cmd_list) header = ['aln_pos','anc_aa'] # Aln Pos and AA _stage = '<6b-i> Partial Output - Header Query' if query: header += ['qry_pos','qry_aa'] # Qry Pos and AA _stage = '<6b-ii> Partial Output - Header Subfam' for f in range(len(tree.subfam)): header += ['fam%d_pos' % (f+1),'fam%d_aa' % (f+1)] # Subfam Pos and AA for func in statlist: _stage = '<6b-iii> Partial Output - Header %s' % func statobj = statObj(method=func,objlist=[specmatrix,conseq]) fs = func.lower() header.append(fs) # Score if rankout: header.append('%s_rank' % fs) # Rank if statobj.stat['WinSize'] > 1: header.append('%s_alnwin' % fs) # Full align window if rankout: header.append('%s_alnrankwin' % fs) # Rank if query: header.append('%s_qrywin' % fs) # Qry window if rankout: header.append('%s_qryrankwin' % fs) # Rank if func in funcspec: for f in range(len(tree.subfam)): header.append('%s_fam%d_win' % (fs,f+1)) # Subfam windows if rankout: header.append('%s_fam%d_rankwin' % (fs,f+1)) # Subfam windows #if not _part_append: BADASP.write('%s\n' % string.join(header, delimit)) out.verbose(1,3,'%s...' % string.join(header, delimit),0) ## <c> ## Stats _stage = '<6c> Stats' qr = 0 # Qry pos fr = [0] * len(tree.subfam) # List of subfam positions aa = '' # Root aa qa = '' # Qry aa fa = [''] * len(tree.subfam) # List of subfam aas for r in range(alnlen): if alnout[r] == False: continue # <i> # Positions and aas _stage = '<6c-i> Partial Output - Stats, positions & aas' aa = root.info['Sequence'][r] if query: qa = query.info['Sequence'][r] if qa != '-': qr += 1 for f in range(len(tree.subfam)): fa[f] = tree.subfam[f].obj['Sequence'].info['Sequence'][r] if fa[f] != '-': fr[f] += 1 # <ii> # Positions and AAs ii _stage = '<6c-ii> Partial Output - Pos & AA ii' line = ['%d' % (r+1), aa] # Aln Pos and AA if query: if qa == '-': line += ['-',qa] # Qry Pos and AA else: line += ['%d' % qr,qa] # Qry Pos and AA for f in range(len(tree.subfam)): if fa[f] == '-': line += ['-',fa[f]] # Subfam Pos and AA else: line += ['%d' % fr[f],fa[f]] # Subfam Pos and AA # <iii> # Stats _stage = '<6c-iii> Partial Output - Stats' for func in statlist: statobj = statObj(method=func,objlist=[specmatrix,conseq]) fs = func.lower() line.append(str(statobj.score[func][r])) # Score if rankout: line.append(str(statobj.rank[func][r])) # Rank if specmatrix.stat['WinSize'] > 1: line.append(str(statobj.alnwin[func][r])) # Full align window if rankout: line.append(str(statobj.alnrankwin[func][r])) # Rank if query: line.append(str(statobj.qrywin[func][r])) # Qry window if rankout: line.append(str(statobj.qryrankwin[func][r])) # Rank if func in funcspec: for f in range(len(tree.subfam)): line.append(str(statobj.famwin[func][tree.subfam[f]][r])) # Subfam windows if rankout: line.append(str(statobj.famrankwin[func][tree.subfam[f]][r])) # Subfam windows # <iv> # Writing _stage = '<6c-iv> Partial Output - Writing' BADASP.write('%s\n' % string.join(line, delimit)) BADASP.close() out.verbose(0,2,'Done!',2) except: mainlog.errorLog('Fatal Error in BADASP Partial output (%s):' % _stage,True) BADASP.write('%s\n' % string.join(line, delimit)) BADASP.close()
def run(self): ### Main run method '''Main run method.''' try: ### ~ [1] ~ Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### infile = self.getStr('InFile') while not rje.exists(infile): infile = rje.choice( 'File "%s" not found. Input file name? (Blank to quit):' % infile) if not infile: return self.printLog('#QUIT', 'Execution terminated!') db = rje_db.Database(self.log, self.cmd_list) db.basefile(rje.baseFile(infile)) sdb = db.addTable(infile, mainkeys='#', delimit='\t', name='SPF.Mod') levels = { 'Level_1': 'k', 'Level_2': 'p', 'Level_3': 'c', 'Level_4': 'o', 'Level_5': 'f', 'Level_6': 'g', 'Level_7': 's' } # k__Bacteria p__Proteobacteria c__Alphaproteobacteria o__Rhodospirillales f__Rhodospirillaceae g__ s__ denovo44 # Unassigned unclassified unclassified unclassified unclassified unclassified unclassified denovo49 ### ~ [1] Modify Text ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### dupnames = [] parents = {} # Parent for each term renamed = [] ex = 0.0 etot = sdb.entryNum() for entry in sdb.entries(): self.progLog('\r#SPF', 'Modifying SPF content: %.1f%%' % (ex / etot)) ex += 100.0 taxon = '' parent = '' #self.debug(entry) for lvl in [ 'Level_1', 'Level_2', 'Level_3', 'Level_4', 'Level_5', 'Level_6', 'Level_7' ]: entry[lvl] = string.replace(entry[lvl], 'unidentified', 'unclassified') #entry[lvl] = string.replace(entry[lvl],'Incertae_sedis','Incertae_sedis-%s' % levels[lvl]) null = '%s__' % levels[lvl] #self.bugPrint(null) #self.bugPrint(entry[lvl]) if entry[lvl] in [ null, 'Unassigned', 'unclassified', '%sunclassified' % null, '%sunidentified' % null, '%sunculturedfungus' % null, '%sIncertae_sedis' % null, '%sunclassified_sp.' % null ]: if not taxon or taxon.endswith('unclassified'): entry[lvl] = '%sunclassified' % null #elif taxon.endswith('unassigned)'): entry[lvl] = '%s%s' % (null,taxon[3:]) #elif taxon.endswith('unassigned)'): entry[lvl] = '%s(%s;%s-unassigned)' % (null,string.split(taxon,'(')[1][:-1],levels[lvl]) elif taxon.endswith('unassigned)'): entry[lvl] = '%s%s;%s-unassigned)' % ( null, taxon[3:][:-1], levels[lvl]) else: entry[lvl] = '%s%s(%s-unassigned)' % ( null, taxon[3:], levels[lvl]) if entry[lvl] in parents: #self.debug(parents[entry[lvl]]) if parent in parents[entry[lvl]]: entry[lvl] = parents[entry[lvl]][parent] else: self.bugPrint(entry[lvl]) self.bugPrint(parents[entry[lvl]]) renamed.append(entry[lvl]) newtax = '%s%d' % (entry[lvl], renamed.count(entry[lvl])) self.warnLog( '%s had multiple parents (%s & %s) -> %s' % (entry[lvl], string.join(parents[entry[lvl]], '|'), parent, newtax)) parents[newtax] = {parent: newtax} parents[entry[lvl]][parent] = newtax entry[lvl] = newtax self.deBug(parents[entry[lvl]]) elif parent: parents[entry[lvl]] = {parent: entry[lvl]} parent = entry[lvl] if entry[lvl][3:] == taxon[3:]: if (entry[lvl], taxon) not in dupnames: dupnames.append((entry[lvl], taxon)) #self.bugPrint(entry[lvl]) taxon = entry[lvl] #self.debug(entry) #self.debug(parents) self.printLog('\r#SPF', 'Modifying SPF content complete.') dupnames.sort() for (dupA, dupB) in dupnames: self.warnLog('Duplicate taxa names: %s & %s' % (dupA, dupB)) ### ~ [2] Save to file ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### sdb.saveToFile(savefields=sdb.list['Fields'][1:]) ### ~ [3] Compress to different taxonomic levels ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### compress = [ 'Level_1', 'Level_2', 'Level_3', 'Level_4', 'Level_5', 'Level_6', 'Level_7', '#' ] dump = compress.pop(-1) rules = {'Observation Ids': 'list', dump: 'str'} sdb.dropField('Observation Ids') while compress: sdb.compress(compress, rules=rules, default='sum', best=[], joinchar='|') #if dump == '#': sdb.dropField(dump) sdb.saveToFile( '%s.SPF.%s.%s.spf' % (rje.baseFile(infile), compress[-1], levels[compress[-1]])) dump = compress.pop(-1) rules[dump] = 'list' return except: self.errorLog(self.zen()) raise # Delete this if method error not terrible
def menu(callobj, headtext='', menulist=[], choicetext='Please select:', changecase=True, default='', jointxt=' = ', confirm=False): ### Main Menu method ''' Main Menu method. >> callobj:Object for which attributes are to be read and altered. Also controls interactivity and log. >> headtext:str [''] = Introductory text for menu system. >> menulist:list [] = List of menu item tuples (edit code,description,optiontype,optionkey) - e.g. ('0','Sequence file','info','Name') would edit callobj.info['Name']) - If optiontype == 'return' then menu will return the value given in optionkey - If optiontype == '' then description will be printed as a breaker - If optiontype == 'infile' then callobj.info['Name'] would be changed using rje.getFileName(mustexist=True) - If optiontype == 'outfile' then callobj.info['Name'] would be changed using rje.getFileName(confirm=True) - If optiontype == 'showtext' then optionkey should contain text to be printed with verbose - If optiontype == 'addcmd' then commands can be added. >> choicetext:str ['Please select:'] = Text to display for choice option >> changecase:boolean [True] = change all choices and codes to upper text >> default:str [''] = What to return if nothing selected. >> jointxt:str [' = '] = What to join code and description with when listing options. >> confirm:bool [False] = Whether to confirm selection. << returns optionkey if appropriate, else True ''' try: ### ~ [0] Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### ## ~ [0a] Fill out partial (return) tuples ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## newlist = [] for mtuple in menulist: if len(mtuple) == 2: newlist.append(mtuple + ('return', mtuple[0])) elif len(mtuple) == 3: newlist.append(mtuple + (mtuple[0], )) else: newlist.append(mtuple) menulist = newlist ## ~ [0b] Choice Dictionary ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## choicedict = {} for (code, desc, vtype, key) in menulist: if not vtype: continue if changecase: choicedict[code.upper()] = (vtype, key) else: choicedict[code] = (vtype, key) ## ~ [0c] Setup Header Text ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## maxlen = 0 for line in string.split(headtext, '\n'): if len(line) > maxlen: maxlen = len(line) headlist = ['#' * (maxlen + 10)] for line in string.split(headtext, '\n')[0:]: while len(line) < maxlen: line += ' ' headlist.append('# #> %s <# #' % line) headlist.append(headlist[0]) ### ~ [1] Main Menu Loop ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### while menulist: ## ~ [1a] Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## mtxt = '\n%s' % string.join(headlist, '\n') while mtxt[-2:] != '\n\n': mtxt += '\n' for (code, desc, vtype, key) in menulist: if vtype and (code or desc): if code and desc: mtxt += '%s%s%s' % (code, jointxt, desc) elif code: mtxt += code elif desc: mtxt += desc if vtype in [ 'info', 'list', 'opt', 'stat', 'int', 'str', 'bool', 'num' ]: mtxt += ': %s' % callobj.getAttribute( vtype, key, default='#!#ERROR#!#') elif vtype in ['infile', 'outfile']: mtxt += ': %s' % callobj.getAttribute( 'info', key, default='#!#ERROR#!#') else: mtxt += desc mtxt += '\n' ## ~ [1b] Give Choices ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## print(mtxt) while mtxt: try: ## ~ Input user choice ~~~ ## choice = rje.choice(choicetext, default=default, confirm=confirm) if changecase: choice = choice.upper() ## ~ Process user choice ~ ## if choicedict.has_key(choice): (vtype, key) = choicedict[choice] if vtype in ['str', 'info']: callobj.setInfo({ key: callobj._editChoice(key, callobj.getStr(key)) }) if vtype in ['num', 'stat']: callobj.setStat({ key: callobj._editChoice(key, callobj.getNum(key), numeric=True) }) if vtype == 'int': callobj.setStat({ key: int( callobj._editChoice(key, callobj.getInt(key), numeric=True)) }) if vtype in ['bool', 'opt']: callobj.setOpt({key: not callobj.getBool(key)}) if vtype == 'list': callobj.list[key] = string.split( callobj._editChoice(key, callobj.list[key])) if vtype == 'infile': callobj.setInfo({ key: rje.getFileName('%s File Name?' % key, callobj.getStr(key)) }) if vtype == 'outfile': callobj.setInfo({ key: rje.getFileName('%s File Name?' % key, callobj.getStr(key), mustexist=False, confirm=True) }) if vtype == 'showtext': callobj.verbose(-1, -1, key) break if vtype == 'addcmd': prevcmd = callobj.cmd_list callobj.cmd_list = rje.inputCmds(out, choice) callobj.printLog( '#CMD', 'User Added commands: %s' % callobj.cmd_list) callobj._cmdList() callobj.cmd_list = prevcmd + callobj.cmd_list break if vtype in [ 'info', 'list', 'opt', 'stat', 'infile', 'outfile', 'str', 'bool', 'int', 'num' ]: callobj.printLog('#%s' % vtype.upper(), 'User edited %s parameter' % key) break elif vtype == 'return': return key print('Choice "%s" not recognised!\n' % choice) except KeyboardInterrupt: if rje.yesNo('Terminate program?'): raise SystemExit if rje.yesNo('Exit menu and proceed?'): return default except: raise ### End ### return True except KeyboardInterrupt: raise except SystemExit: raise except: if callobj: callobj.errorLog('Major disaster in rje_menu.menu()', quitchoice=True) else: raise
def multiHAQ(self, secondrun=False): ### Executes main HAQESAC runs '''Executes main HAQESAC runs.''' try: ### ~ [0] Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### finalrun = secondrun == self.opt[ 'MultiHAQ'] # Whether this is the manual HAQESAC phase qryacc = self.obj['SeqList'].accList( ) # Full list of Query accession numbers processed = [] # List of processed sequence accession numbers ### ~ [1] Peform HAQESAC runs ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### for seq in self.seqs(): ## ~ [1a] Check AutoSkip ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## acc = seq.info['AccNum'] if finalrun and acc in processed and ( self.opt['AutoSkip'] or (self.i() >= 0 and rje.yesNo( '%s already covered by previous HAQESAC. Skip?' % seq.shortName()))): self.printLog( '#SKIP', '%s already covered by previous HAQESAC: Skipped' % seq.shortName()) continue ## ~ [1b] Check Whether to run (re-runs and low sequence number) ~~~~~~~~~~~~~~~~~~ ## logfile = rje.makePath('%s%s.log' % (self.info['HaqDir'], acc), wholepath=True) infile = rje.makePath('%s%s.fas' % (self.info['HaqDir'], acc), wholepath=True) pkfile = rje.makePath('%s%s.pickle' % (self.info['HaqDir'], acc), wholepath=True) pkzfile = rje.makePath('%s%s.pickle.gz' % (self.info['HaqDir'], acc), wholepath=True) if not os.path.exists(infile): self.printLog( '#SKIP', '%s input file %s not found: Skipped' % (seq.shortName(), infile)) continue if not finalrun and not self.opt['Force'] and rje.isYounger( pkzfile, infile) == pkzfile: self.printLog('#SKIP', '%s run detected: Skipped' % seq.shortName()) continue if not finalrun and not self.opt['Force'] and rje.isYounger( pkfile, infile) == pkfile: self.printLog('#SKIP', '%s run detected: Skipped' % seq.shortName()) continue inseqx = rje_seq.SeqCount(self, infile) if inseqx < 2: self.printLog( '#SKIP', 'Only one sequence found in %s: Skipped' % (infile)) continue ## ~ [1c] Pause if running in Chaser Mode and no Pickle ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## pickled = os.path.exists(pkfile) or os.path.exists( '%s.gz' % pkfile) tm = 0 while secondrun and self.opt['Chaser'] and not pickled: self.progLog( '#WAIT', 'No %s pickle. Sleeping for %d min.' % (acc, tm)) time.sleep(60 * tm) tm += 1 pickled = os.path.exists(pkfile) or os.path.exists( '%s.gz' % pkfile) if not pickled: try: rje.choice( 'Press <ENTER> to try again, or <CTRL+C> to Quit' ) except: self.printLog('#PICKLE', 'No %s pickle.' % (acc, tm)) self.printLog('\r#MULTI', 'Exiting multiHAQ "Chaser" run.') return ## ~ [1d] Run HAQESAC ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## runhaqesac = True pngfile = rje.makePath('%s%s.png' % (self.info['HaqDir'], acc), wholepath=True) if not self.force() and rje.exists(pngfile): self.printLog( '#SKIP', 'Found evidence of completed run: %s (force=F). Skipping.' % pngfile) runhaqesac = False ancfile = rje.makePath('%s%s.anc.fas' % (self.info['HaqDir'], acc), wholepath=True) if not self.force() and rje.exists(ancfile): self.printLog( '#SKIP', 'Found evidence of completed run: %s (force=F). Skipping.' % ancfile) runhaqesac = False #if not finalrun or self.opt['Force'] or rje.isYounger(logfile,nsfile) != logfile: if runhaqesac: haqcmd = [ 'ini=haqesac.ini', 'seqin=%s.fas' % acc, 'query=%s' % acc, 'basefile=%s' % acc, 'newlog=F' ] self.printLog( '#HAQ', 'Running HAQESAC for %s - will have own log etc.' % seq.shortName(), log=False) os.chdir(self.info['HaqDir']) info = haqesac.makeInfo() haqcmd = rje.getCmdList(haqcmd, info=info) out = rje.Out( cmd_list=haqcmd ) # Sets up Out object for controlling output to screen out.printIntro( info ) # Prints intro text using details from Info object haqlog = rje.setLog( info, out, haqcmd ) # Sets up Log object for controlling log file output try: haqesac.HAQESAC(log=haqlog, cmd_list=haqcmd).run(setobjects=True) except: os.chdir(self.info['RunPath']) if self.i() >= 0 and rje.yesNo( 'Problem with %s HAQESAC run. Abort?' % seq.shortName()): raise KeyboardInterrupt os.chdir(self.info['RunPath']) if finalrun: self.printLog( '#HAQ', 'HAQESAC final round run for %s' % seq.shortName()) else: self.printLog( '#HAQ', 'HAQESAC first round run for %s' % seq.shortName()) ## ~ [1e] Update ScreenQry ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## if not self.opt['ScreenQry'] or not finalrun: continue qacclist = [] for qacc in rje_seq.SeqList( self.log, ['seqin=%s' % infile, 'autoload=T', 'autofilter=F' ]).accList(): if qacc in qryacc and qacc != acc: qacclist.append(qacc) if qacc in qryacc and qacc not in processed: processed.append(qacc) self.printLog( '#QRY', '%d other queries found in %s: [%s]' % (len(qacclist), infile, string.join(qacclist, '; '))) self.printLog( '#QRY', '%d of %d queries processed' % (len(processed), self.seqNum())) ### ~ [2] MultiHAQ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### if not finalrun: self.printLog('#MULTI', 'Executing second round of multiHAQ') self.multiHAQ(True) except: self.errorLog('Major problem with MultiHAQ.multiHAQ', quitchoice=True)
def run(self): ### Main run method '''Main run method.''' try: ### ~ [1] ~ Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### counter = ['>>'] # List containing count times menulist = [('F', 'Change output file name', 'outfile', 'OutFile'), ('X', 'Exit', 'return', ''), ('R', 'Run', 'return', '')] mchoice = rje_menu.menu(self, 'WormPump Menu', menulist, choicetext='Please select:', changecase=True, default='R') if mchoice == 'X': return self.printLog('#OUT', 'Output will be to %s' % self.info['OutFile']) self.printLog('#START', 'Initialising counter...') ### ~ [2] ~ Perform counts ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### wormid = None while counter[-1] != 'X': if wormid: counter.append( rje.choice( 'ID <ENTER> for new worm | X <ENTER> to exit | <ENTER> for "%s" pump count' % wormid, default='').upper()) else: counter.append( rje.choice( 'ID <ENTER> for new worm | X <ENTER> to exit', default='').upper()) if counter[-1]: wormid = counter[-1] if wormid == 'X': break self.printLog('#WORM', 'Worm "%s"' % wormid) counter.append(time.time()) self.deBug(counter) ### ~ [3] ~ Output results ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### head = ['Worm', 'Count', 'WormTime', 'AbsTime'] rje.delimitedFileOutput(self, self.info['OutFile'], headers=head, rje_backup=True) wormstart = 0.0 wormid = None wtot = 0 while counter: x = counter.pop(0) if x in ['>>', 'X']: continue if x: wormid = x wormstart = counter[0] wx = 0 wtot += 1 else: if not wormid: continue wx += 1 t = counter.pop(0) tt = time.localtime(t) wdata = { 'Worm': wormid, 'Count': wx, 'WormTime': t - wormstart, #'AbsTime':'%s/%s/%s %s:%s:%s' % (tt[2],tt[1],tt[0],rje.preZero(tt[3],24),rje.preZero(tt[4],60),rje.preZero(tt[5],60))} 'AbsTime': '%s:%s:%s' % (rje.preZero(tt[3], 24), rje.preZero( tt[4], 60), rje.preZero(tt[5], 60)) } rje.delimitedFileOutput(self, self.info['OutFile'], headers=head, datadict=wdata) self.printLog( '#OUT', 'Counts for %d worms output to %s' % (wtot, self.info['OutFile'])) rje.choice('<ENTER> to exit') except: self.errorLog(rje_zen.Zen().wisdom()) raise # Delete this if method error not terrible
def multiHAQ(self,secondrun=False): ### Executes main HAQESAC runs '''Executes main HAQESAC runs.''' try:### ~ [0] Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### finalrun = secondrun == self.opt['MultiHAQ'] # Whether this is the manual HAQESAC phase qryacc = self.obj['SeqList'].accList() # Full list of Query accession numbers processed = [] # List of processed sequence accession numbers ### ~ [1] Peform HAQESAC runs ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### for seq in self.seqs(): ## ~ [1a] Check AutoSkip ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## acc = seq.info['AccNum'] if finalrun and acc in processed and (self.opt['AutoSkip'] or (self.i() >=0 and rje.yesNo('%s already covered by previous HAQESAC. Skip?' % seq.shortName()))): self.printLog('#SKIP','%s already covered by previous HAQESAC: Skipped' % seq.shortName()); continue ## ~ [1b] Check Whether to run (re-runs and low sequence number) ~~~~~~~~~~~~~~~~~~ ## logfile = rje.makePath('%s%s.log' % (self.info['HaqDir'],acc),wholepath=True) infile = rje.makePath('%s%s.fas' % (self.info['HaqDir'],acc),wholepath=True) pkfile = rje.makePath('%s%s.pickle' % (self.info['HaqDir'],acc),wholepath=True) pkzfile = rje.makePath('%s%s.pickle.gz' % (self.info['HaqDir'],acc),wholepath=True) if not os.path.exists(infile): self.printLog('#SKIP','%s input file %s not found: Skipped' % (seq.shortName(),infile)); continue if not finalrun and not self.opt['Force'] and rje.isYounger(pkzfile,infile) == pkzfile: self.printLog('#SKIP','%s run detected: Skipped' % seq.shortName()); continue if not finalrun and not self.opt['Force'] and rje.isYounger(pkfile,infile) == pkfile: self.printLog('#SKIP','%s run detected: Skipped' % seq.shortName()); continue inseqx = rje_seq.SeqCount(self,infile) if inseqx < 2: self.printLog('#SKIP','Only one sequence found in %s: Skipped' % (infile)); continue ## ~ [1c] Pause if running in Chaser Mode and no Pickle ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## pickled = os.path.exists(pkfile) or os.path.exists('%s.gz' % pkfile); tm = 0 while secondrun and self.opt['Chaser'] and not pickled: self.progLog('#WAIT','No %s pickle. Sleeping for %d min.' % (acc,tm)) time.sleep(60*tm); tm += 1 pickled = os.path.exists(pkfile) or os.path.exists('%s.gz' % pkfile) if not pickled: try: rje.choice('Press <ENTER> to try again, or <CTRL+C> to Quit') except: self.printLog('#PICKLE','No %s pickle.' % (acc,tm)) self.printLog('\r#MULTI','Exiting multiHAQ "Chaser" run.'); return ## ~ [1d] Run HAQESAC ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## runhaqesac = True pngfile = rje.makePath('%s%s.png' % (self.info['HaqDir'],acc),wholepath=True) if not self.force() and rje.exists(pngfile): self.printLog('#SKIP','Found evidence of completed run: %s (force=F). Skipping.' % pngfile) runhaqesac = False ancfile = rje.makePath('%s%s.anc.fas' % (self.info['HaqDir'],acc),wholepath=True) if not self.force() and rje.exists(ancfile): self.printLog('#SKIP','Found evidence of completed run: %s (force=F). Skipping.' % ancfile) runhaqesac = False #if not finalrun or self.opt['Force'] or rje.isYounger(logfile,nsfile) != logfile: if runhaqesac: haqcmd = ['ini=haqesac.ini','seqin=%s.fas' % acc, 'query=%s' % acc, 'basefile=%s' % acc, 'newlog=F'] self.printLog('#HAQ','Running HAQESAC for %s - will have own log etc.' % seq.shortName(),log=False) os.chdir(self.info['HaqDir']) info = haqesac.makeInfo() haqcmd = rje.getCmdList(haqcmd,info=info) out = rje.Out(cmd_list=haqcmd) # Sets up Out object for controlling output to screen out.printIntro(info) # Prints intro text using details from Info object haqlog = rje.setLog(info,out,haqcmd) # Sets up Log object for controlling log file output try: haqesac.HAQESAC(log=haqlog, cmd_list=haqcmd).run(setobjects=True) except: os.chdir(self.info['RunPath']) if self.i() >= 0 and rje.yesNo('Problem with %s HAQESAC run. Abort?' % seq.shortName()): raise KeyboardInterrupt os.chdir(self.info['RunPath']) if finalrun: self.printLog('#HAQ','HAQESAC final round run for %s' % seq.shortName()) else: self.printLog('#HAQ','HAQESAC first round run for %s' % seq.shortName()) ## ~ [1e] Update ScreenQry ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## if not self.opt['ScreenQry'] or not finalrun: continue qacclist = [] for qacc in rje_seq.SeqList(self.log,['seqin=%s' % infile,'autoload=T','autofilter=F']).accList(): if qacc in qryacc and qacc != acc: qacclist.append(qacc) if qacc in qryacc and qacc not in processed: processed.append(qacc) self.printLog('#QRY','%d other queries found in %s: [%s]' % (len(qacclist),infile,string.join(qacclist,'; '))) self.printLog('#QRY','%d of %d queries processed' % (len(processed),self.seqNum())) ### ~ [2] MultiHAQ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### if not finalrun: self.printLog('#MULTI','Executing second round of multiHAQ'); self.multiHAQ(True) except: self.errorLog('Major problem with MultiHAQ.multiHAQ',quitchoice=True)
def setup(self): ### Main class setup method. '''Main class setup method.''' try: ### ~ [1] ~ Setup Program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### self.obj['Prog'] = None prog = self.getStrLC('Name') if prog in mod: i = self.obj['ProgInfo'] = mod[prog].makeInfo() self.printLog( '#PROG', '%s V%s: %s' % (i.program, i.version, i.description)) progcmd = rje.getCmdList( [], info=i) + self.cmd_list + ['newlog=F'] out = rje.Out(cmd_list=progcmd) out.printIntro(i) #self.debug(prog); self.debug(progcmd) if self.getBool('Help'): progcmd = mod[prog].cmdHelp(i, out, ['help'] + progcmd) self.printLog('#CMD', 'Full %s CmdList: %s' % (i.program, rje.argString( rje.tidyArgs(progcmd, nopath=self.getStrLC('Rest') and not self.dev(), purgelist=purgelist))), screen=False) #self.debug(prog); self.debug(progcmd) ## ~ [1a] ~ Make self.obj['Prog'] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## if prog in ['seqlist', 'rje_seqlist']: self.obj['Prog'] = rje_seqlist.SeqList(self.log, progcmd) elif prog in ['uniprot', 'rje_uniprot']: self.obj['Prog'] = rje_uniprot.UniProt(self.log, progcmd) elif prog in ['taxonomy', 'rje_taxonomy']: self.obj['Prog'] = rje_taxonomy.Taxonomy(self.log, progcmd) elif prog in ['tree', 'rje_tree']: self.obj['Prog'] = rje_tree.Tree(self.log, progcmd) elif prog in ['xref', 'rje_xref']: self.obj['Prog'] = rje_xref.XRef(self.log, progcmd) elif prog in ['seq', 'rje_seq']: self.obj['Prog'] = rje_seq.SeqList(self.log, progcmd) elif prog in ['mitab', 'rje_mitab']: self.obj['Prog'] = rje_mitab.MITAB(self.log, progcmd) elif prog in ['dbase', 'database']: self.obj['Prog'] = rje_dbase.DatabaseController( self.log, progcmd) elif prog in ['pydocs']: self.obj['Prog'] = rje_pydocs.PyDoc(self.log, progcmd) elif prog in ['ensembl', 'rje_ensembl']: self.obj['Prog'] = rje_ensembl.EnsEMBL(self.log, progcmd) elif prog in ['genbank', 'rje_genbank']: self.obj['Prog'] = rje_genbank.GenBank(self.log, progcmd) elif prog in ['extatic']: self.obj['Prog'] = extatic.ExTATIC(self.log, progcmd) elif prog in ['revert']: self.obj['Prog'] = revert.REVERT(self.log, progcmd) elif prog in ['fiesta']: self.obj['Prog'] = fiesta.FIESTA(self.log, progcmd) elif prog in ['gablam']: self.obj['Prog'] = gablam.GABLAM(self.log, progcmd) elif prog in ['gopher']: self.obj['Prog'] = gopher.Gopher(self.log, progcmd) elif prog in ['haqesac']: self.obj['Prog'] = haqesac.HAQESAC(self.log, progcmd) elif prog in ['multihaq']: self.obj['Prog'] = multihaq.MultiHAQ(self.log, progcmd) elif prog in ['pingu']: self.obj['Prog'] = pingu.PINGU(self.log, progcmd) elif prog in ['pacbio']: self.obj['Prog'] = rje_pacbio.PacBio(self.log, progcmd) elif prog in ['rje_zen', 'zen']: self.obj['Prog'] = rje_zen.Zen(self.log, progcmd) ### ~ [2] ~ Failure to recognise program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### if not self.obj['Prog']: self.printLog( '#ERR', 'Program "%s" not recognised.' % self.getStr('Name')) if self.i() < 0: return False if rje.yesNo('Show SeqSuite help with program options?'): extracmd = cmdHelp(cmd_list=['help'])[1:] if extracmd: self.cmd_list += extracmd self._cmdList() if prog != self.getStrLC('Name'): return self.setup() self.setStr({ 'Name': rje.choice('Give program name (Blank or CTRL+C to quit)') }) if self.getStrLC('Name'): return self.setup() else: return False return self.obj['Prog'] # Setup successful except KeyboardInterrupt: return False except SystemExit: raise except: self.errorLog('Problem during %s setup.' % self.prog()) return False # Setup failed
def setup(self): ### Main class setup method. '''Main class setup method.''' try: ### ~ [1] ~ Setup Program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### self.obj['Prog'] = None prog = self.getStrLC('Name') if prog in mod: i = self.obj['ProgInfo'] = mod[prog].makeInfo() self.printLog( '#PROG', '%s V%s: %s' % (i.program, i.version, i.description)) progcmd = rje.getCmdList( [], info=i) + self.cmd_list + ['newlog=F'] out = rje.Out(cmd_list=progcmd) out.printIntro(i) if self.getBool('Help'): progcmd = mod[prog].cmdHelp(i, out, ['help'] + progcmd) purgelist = seqsuite.purgelist self.printLog('#CMD', 'Full %s CmdList: %s' % (i.program, rje.argString( rje.tidyArgs(progcmd, nopath=self.getStrLC('Rest') and not self.dev(), purgelist=purgelist))), screen=False) #self.debug(prog) ## ~ [1a] ~ Make self.obj['Prog'] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## if prog in ['slimcore', 'rje_slimcore']: self.obj['Prog'] = rje_slimcore.SLiMCore(self.log, progcmd) elif prog in ['rlc', 'disorder']: self.obj['Prog'] = rje_slimcore.SLiMCore( self.log, progcmd + ['prog=%s' % prog]) elif prog in ['slimlist', 'rje_slimlist']: self.obj['Prog'] = rje_slimlist.SLiMList(self.log, progcmd) elif prog in ['slimfinder']: self.obj['Prog'] = slimfinder.SLiMFinder(self.log, progcmd) elif prog in ['qslimfinder']: self.obj['Prog'] = qslimfinder.QSLiMFinder( self.log, progcmd) elif prog in ['slimprob']: self.obj['Prog'] = slimprob.SLiMProb(self.log, progcmd) elif prog in ['slimmaker']: self.obj['Prog'] = slimmaker.SLiMMaker(self.log, progcmd) elif prog in ['slimfarmer', 'farm']: self.obj['Prog'] = slimfarmer.SLiMFarmer(self.log, progcmd) elif prog in ['slimbench']: self.obj['Prog'] = slimbench.SLiMBench(self.log, progcmd) elif prog in ['comparimotif']: self.obj['Prog'] = comparimotif.CompariMotif( self.log, progcmd) elif prog in ['peptcluster']: self.obj['Prog'] = peptcluster.PeptCluster( self.log, progcmd) elif prog in ['peptalign']: self.obj['Prog'] = peptcluster.PeptCluster( self.log, ['peptalign=T'] + progcmd) self.obj['Prog'].dict['Output']['help'] = mod[prog].__doc__ elif prog in seqsuite.mod: seqsuiteobj = seqsuite.SeqSuite(self.log, self.cmd_list) self.obj['Prog'] = seqsuiteobj.setup() self.obj['ProgInfo'] = seqsuiteobj.obj['ProgInfo'] self.obj['Prog'].dict['Output']['help'] = seqsuite.mod[ prog].__doc__ ### ~ [2] ~ Failure to recognise program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### if not self.obj['Prog']: self.printLog( '#ERR', 'Program "%s" not recognised.' % self.getStr('Name')) if self.i() < 0: return False #!# Try SeqSuite? #!# if rje.yesNo('Show SLiMSuite help with program options?'): extracmd = cmdHelp(cmd_list=['help'])[1:] if extracmd: self.cmd_list += extracmd self._cmdList() if prog != self.getStrLC('Name'): return self.setup() self.setStr({ 'Name': rje.choice('Give program name (Blank or CTRL+C to quit)') }) if self.getStrLC('Name'): return self.setup() else: return False return self.obj['Prog'] # Setup successful except KeyboardInterrupt: return False except SystemExit: raise except: self.errorLog('Problem during %s setup.' % self.prog()) return False # Setup failed
def menu(callobj,headtext='',menulist=[],choicetext='Please select:',changecase=True,default=''): ### Main Menu method ''' Main Menu method. >> callobj:Object for which attributes are to be read and altered. Also controls interactivity and log. >> headtext:str [''] = Introductory text for menu system. >> menulist:list [] = List of menu item tuples (edit code,description,optiontype,optionkey) - e.g. ('0','Sequence file','info','Name') would edit callobj.info['Name']) - If optiontype == 'return' then menu will return the value given in optionkey - If optiontype == '' then description will be printed as a breaker - If optiontype == 'infile' then callobj.info['Name'] would be changed using rje.getFileName(mustexist=True) - If optiontype == 'outfile' then callobj.info['Name'] would be changed using rje.getFileName(confirm=True) - If optiontype == 'showtext' then optionkey should contain text to be printed with verbose - If optiontype == 'addcmd' then commands can be added. >> choicetext:str ['Please select:'] = Text to display for choice option >> changecase:boolean [True] = change all choices and codes to upper text << returns optionkey if appropriate, else True ''' try:### ~ [0] Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### ## ~ [0a] Choice Dictionary ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## choicedict = {} for (code,desc,type,key) in menulist: if not type: continue if changecase: choicedict[code.upper()] = (type,key) else: choicedict[code] = (type,key) ## ~ [0b] Setup Header Text ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## maxlen = 0 for line in string.split(headtext,'\n'): if len(line) > maxlen: maxlen = len(line) headlist = ['#' * (maxlen + 10)] for line in string.split(headtext,'\n')[0:]: while len(line) < maxlen: line += ' ' headlist.append('# #> %s <# #' % line) headlist.append(headlist[0]) ### ~ [1] Main Menu Loop ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### while menulist: ## ~ [1a] Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## mtxt = '\n%s' % string.join(headlist,'\n') while mtxt[-2:] != '\n\n': mtxt += '\n' for (code,desc,type,key) in menulist: if type and (code or desc): mtxt += '<%s> %s' % (code,desc) if type in ['info','list','opt','stat','int','str','bool','num']: mtxt += ': %s' % callobj.getAttribute(type,key,default='#!#ERROR#!#') elif type in ['infile','outfile']: mtxt += ': %s' % callobj.getAttribute('info',key,default='#!#ERROR#!#') else: mtxt += desc mtxt += '\n' ## ~ [1b] Give Choices ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## print mtxt while mtxt: try:## ~ Input user choice ~~~ ## choice = rje.choice(choicetext,default=default) if changecase: choice = choice.upper() ## ~ Process user choice ~ ## if choicedict.has_key(choice): (type,key) = choicedict[choice] if type in ['str','info']: callobj.setInfo({key:callobj._editChoice(key,callobj.getStr(key))}) if type in ['num','stat']: callobj.setStat({key:callobj._editChoice(key,callobj.getNum(key),numeric=True)}) if type == 'int': callobj.setStat({key:int(callobj._editChoice(key,callobj.getInt(key),numeric=True))}) if type in ['bool','opt']: callobj.setOpt({key: not callobj.getBool(key)}) if type == 'list': callobj.list[key] = string.split(callobj._editChoice(key,callobj.list[key])) if type == 'infile': callobj.setInfo({key: rje.getFileName('%s File Name?' % key,callobj.getStr(key))}) if type == 'outfile': callobj.setInfo({key: rje.getFileName('%s File Name?' % key,callobj.getStr(key),mustexist=False,confirm=True)}) if type == 'showtext': callobj.verbose(-1,-1,key); break if type == 'addcmd': prevcmd = callobj.cmd_list callobj.cmd_list = rje.inputCmds(out,prevcmd) callobj.printLog('#CMD','User Added commands: %s' % callobj.cmd_list) callobj._cmdList() callobj.cmd_list = prevcmd + callobj.cmd_list break if type in ['info','list','opt','stat','infile','outfile','str','bool','int','num']: callobj.printLog('#%s' % type.upper(),'User edited %s parameter' % key); break elif type == 'return': return key print 'Choice "%s" not recognised!\n' % choice except KeyboardInterrupt: if rje.yesNo('Terminate program?'): raise if rje.yesNo('Exit menu and proceed?'): if default: return default else: return True except: raise ### End ### return True except KeyboardInterrupt: raise except: if callobj: callobj.errorLog('Major disaster in rje_menu.menu()',quitchoice=True) else: raise