Beispiel #1
0
 def rankScore(self, scorelist):  ### Returns rank of scores as list
     '''
     Returns rank of scores as list.
     >> scorelist = list of scores
     << ranklist:list of ranks (0 = Lowest, 1 = Highest)
     '''
     if self.opt['Rank']:
         return rje.rankList(scorelist)
     else:
         return [0] * len(scorelist)
Beispiel #2
0
 def rankScore(self,scorelist):  ### Returns rank of scores as list
     '''
     Returns rank of scores as list.
     >> scorelist = list of scores
     << ranklist:list of ranks (0 = Lowest, 1 = Highest)
     '''
     if self.opt['Rank']:
         return rje.rankList(scorelist)
     else:
         return [0] * len(scorelist)
Beispiel #3
0
    try:
        ### NewRanks ###
        scores = []
        objlist = objlist[0:]
        for obj in objlist[0:]:
            score = obj.getData(dkey,dlist,case,str=False,default=default,dp=-1)
            if convert:
                try:
                    score = float(score)
                except:
                    score = default
            if score == None:
                objlist.remove(obj)
            else:
                scores.append(score)
        newranks = rje.rankList(scores,rev,absolute,lowest)

        ### Rerank ###
        rankdict = {}
        for i in range(len(objlist)):
            obj = objlist[i]
            r = newranks[i]
            if rankdict.has_key(r):
                rankdict[r].append(obj)
            else:
                rankdict[r] = [obj]
            if addstat:
                obj.stat[addstat] = r
                obj.dict['Data'][addstat] = r
        newlist = []
        for r in rje.sortKeys(rankdict):
Beispiel #4
0
 def orderHits(self,seq,hits,hitseq):    ### Returns orderd hits and fuller hitdict with GABLAM Stats etc.
     '''
     Returns orderd hits and fuller hitdict with GABLAM Stats etc.
     >> seq:Query Sequence Object (name,sequence)
     >> hits:List of hit entries 
     >> hitseq:Dictionary of {Hit:Sequence}
     << Tuple of (hits,hitdict)
     '''
     try:### ~ [0] Order hits by BLAST Rank ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
         sorted = hits[0:]
         for hit in hits: sorted[hit['Rank']-1] = hit            
         ### ~ [1] Screen Species ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
         hits = sorted
         hits.reverse()
         mapspec = None
         qryspec = rje_sequence.specCodeFromName(seq[0])
         if self.getStr('MapSpec').lower() == 'self': mapspec = qryspec
         elif self.getStr('MapSpec').lower() not in ['any','none']: mapspec = self.getStr('MapSpec').upper()
         for hit in hits[0:]:
             #self.debug(hit)
             #self.debug('%s vs %s' % (rje_sequence.specCodeFromName(hitseq[hit['Hit']][0]), mapspec))
             if mapspec and rje_sequence.specCodeFromName(hitseq[hit['Hit']][0]) != mapspec:
                 hits.remove(hit)
                 hitseq.pop(hit['Hit'])
             elif not mapspec and rje_sequence.specCodeFromName(hitseq[hit['Hit']][0]) == qryspec:
                 hits.remove(hit)
                 hits.append(hit)    # => Move hits from same species to top of list
         hits.reverse()
         ### ~ [2] Convert GABLAM Stats to Percentages and move to hitdict ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
         self.debug(self.db())
         gstat = 'GABLAMO'
         if not self.bool['Ordered']: gstat = 'GABLAM'
         hitdict = {}
         for hit in hits[0:]:
             hitname = hit['Hit']
             hitdict[hitname] = {'Seq':hitseq[hitname],'MapRank':hits.index(hit)+1,'EVal':hit['E-Value'],'Score':hit['BitScore']}
             hitgab = self.db('GABLAM').data(self.db('GABLAM').makeKey({'Query':hit['Query'],'Hit':hitname,'QryHit':'Hit'}))
             qrygab = self.db('GABLAM').data(self.db('GABLAM').makeKey({'Query':hit['Query'],'Hit':hitname,'QryHit':'Query'}))
             for st in ['ID','Len','Sim']:
                 hitdict[hitname]['Query_%s' % st] = 100 * float(qrygab['%s %s' % (gstat,st)]) / len(seq[1])
                 hitdict[hitname]['Hit_%s' % st] = 100 * float(hitgab['%s %s' % (gstat,st)]) / hit['Length']
                 hitdict[hitname]['Both_%s' % st] = (hitdict[hitname]['Query_%s' % st] + hitdict[hitname]['Hit_%s' % st]) / 2.0
                 if hitdict[hitname]['Query_%s' % st] >= hitdict[hitname]['Hit_%s' % st]:
                     hitdict[hitname]['Either_%s' % st] = hitdict[hitname]['Query_%s' % st]
                 else:
                     hitdict[hitname]['Either_%s' % st] = hitdict[hitname]['Hit_%s' % st]
         ### ~ [3] Rank Hits ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
         focus = self.str['MapFocus'][:1].upper() + self.str['MapFocus'][1:].lower()
         scorelist = []
         multorder = ['Both_Len','Both_Sim','Both_ID','%s_Len' % focus,'%s_Sim' % focus,'%s_ID' % focus]
         for hit in hits:
             hitname = hit['Hit']
             s = 0
             m = 1           
             for mult in multorder:
                 s += (hitdict[hitname][mult] * m)
                 m *= 1000
             scorelist.append(s)
         ranks = rje.rankList(scorelist,rev=True,absolute=True,lowest=True)  ### Returns rank of scores as list
         rankdict = {}
         for h in range(len(hits)):
             hit = hits[h]
             hitname = hit['Hit']
             r = ranks[h]
             hitdict[hitname]['HitRank'] = r
             if rankdict.has_key(r): rankdict[r].append(hit)
             else: rankdict[r] = [hit]
         newlist = []
         for r in rje.sortKeys(rankdict): newlist += rankdict[r]
         return (newlist,hitdict)
     except: self.errorLog('Bad goings on in SeqMapper.orderHits()',quitchoice=True)
Beispiel #5
0
 def orderHits(self,seq,hits,hitseq):    ### Returns orderd hits and fuller hitdict with GABLAM Stats etc.
     '''
     Returns orderd hits and fuller hitdict with GABLAM Stats etc.
     >> seq:Query Sequence Object (name,sequence)
     >> hits:List of hit entries 
     >> hitseq:Dictionary of {Hit:Sequence}
     << Tuple of (hits,hitdict)
     '''
     try:### ~ [0] Order hits by BLAST Rank ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
         sorted = hits[0:]
         for hit in hits: sorted[hit['Rank']-1] = hit            
         ### ~ [1] Screen Species ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
         hits = sorted
         hits.reverse()
         mapspec = None
         qryspec = rje_sequence.specCodeFromName(seq[0])
         if self.getStr('MapSpec').lower() == 'self': mapspec = qryspec
         elif self.getStr('MapSpec').lower() not in ['any','none']: mapspec = self.getStr('MapSpec').upper()
         for hit in hits[0:]:
             #self.debug(hit)
             #self.debug('%s vs %s' % (rje_sequence.specCodeFromName(hitseq[hit['Hit']][0]), mapspec))
             if mapspec and rje_sequence.specCodeFromName(hitseq[hit['Hit']][0]) != mapspec:
                 hits.remove(hit)
                 hitseq.pop(hit['Hit'])
             elif not mapspec and rje_sequence.specCodeFromName(hitseq[hit['Hit']][0]) == qryspec:
                 hits.remove(hit)
                 hits.append(hit)    # => Move hits from same species to top of list
         hits.reverse()
         ### ~ [2] Convert GABLAM Stats to Percentages and move to hitdict ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
         self.debug(self.db())
         gstat = 'GABLAMO'
         if not self.bool['Ordered']: gstat = 'GABLAM'
         hitdict = {}
         for hit in hits[0:]:
             hitname = hit['Hit']
             hitdict[hitname] = {'Seq':hitseq[hitname],'MapRank':hits.index(hit)+1,'EVal':hit['E-Value'],'Score':hit['BitScore']}
             hitgab = self.db('GABLAM').data(self.db('GABLAM').makeKey({'Query':hit['Query'],'Hit':hitname,'QryHit':'Hit'}))
             qrygab = self.db('GABLAM').data(self.db('GABLAM').makeKey({'Query':hit['Query'],'Hit':hitname,'QryHit':'Query'}))
             for st in ['ID','Len','Sim']:
                 hitdict[hitname]['Query_%s' % st] = 100 * float(qrygab['%s %s' % (gstat,st)]) / len(seq[1])
                 hitdict[hitname]['Hit_%s' % st] = 100 * float(hitgab['%s %s' % (gstat,st)]) / hit['Length']
                 hitdict[hitname]['Both_%s' % st] = (hitdict[hitname]['Query_%s' % st] + hitdict[hitname]['Hit_%s' % st]) / 2.0
                 if hitdict[hitname]['Query_%s' % st] >= hitdict[hitname]['Hit_%s' % st]:
                     hitdict[hitname]['Either_%s' % st] = hitdict[hitname]['Query_%s' % st]
                 else:
                     hitdict[hitname]['Either_%s' % st] = hitdict[hitname]['Hit_%s' % st]
         ### ~ [3] Rank Hits ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ###
         focus = self.str['MapFocus'][:1].upper() + self.str['MapFocus'][1:].lower()
         scorelist = []
         multorder = ['Both_Len','Both_Sim','Both_ID','%s_Len' % focus,'%s_Sim' % focus,'%s_ID' % focus]
         for hit in hits:
             hitname = hit['Hit']
             s = 0
             m = 1           
             for mult in multorder:
                 s += (hitdict[hitname][mult] * m)
                 m *= 1000
             scorelist.append(s)
         ranks = rje.rankList(scorelist,rev=True,absolute=True,lowest=True)  ### Returns rank of scores as list
         rankdict = {}
         for h in range(len(hits)):
             hit = hits[h]
             hitname = hit['Hit']
             r = ranks[h]
             hitdict[hitname]['HitRank'] = r
             if rankdict.has_key(r): rankdict[r].append(hit)
             else: rankdict[r] = [hit]
         newlist = []
         for r in rje.sortKeys(rankdict): newlist += rankdict[r]
         return (newlist,hitdict)
     except: self.errorLog('Bad goings on in SeqMapper.orderHits()',quitchoice=True)
Beispiel #6
0
def rankObj(
    callobj,
    objlist,
    dkey,
    dlist=["stat"],
    case=False,
    default=None,
    rev=True,
    absolute=True,
    lowest=True,
    addstat="Rank",
    cutoff=0,
    convert=True,
):  ### Ranks objects using numerical data
    """
    Ranks objects using data in object and rje.getData()
    >> objlist:list of objects to be ranked, ordered and returned
    >> dkey:str = Key for dictionaries
    >> dlist:list [self.stat] = list of dictionaries to try after dict['Data']
    >> case:bool [False] = whether to match case for dkey
    >> default [None] = what value to give entry if no dictionary has key (if None, will not be returned in ranked list)
    >> rev:bool [True] = whether high values should be ranked number 1
    >> absolute:boolean [True] = return 1 to n, rather than 0 to 1
    >> lowest:boolean [True] = returns lowest rank rather mean rank in case of ties
    >> addstat:str ['Rank'] = key for callobj.stat to add rank to (will not add if None)
    >> cutoff:int [0] = only returns top X ranked motifs (if given)  (Can be float if absolute=False)
    >> convert:bool [True] = convert returned data into numeric
    << returns list of ranked objects
    """
    try:
        ### NewRanks ###
        scores = []
        objlist = objlist[0:]
        for obj in objlist[0:]:
            score = obj.getData(dkey, dlist, case, str=False, default=default, dp=-1)
            if convert:
                try:
                    score = float(score)
                except:
                    score = default
            if score == None:
                objlist.remove(obj)
            else:
                scores.append(score)
        newranks = rje.rankList(scores, rev, absolute, lowest)

        ### Rerank ###
        rankdict = {}
        for i in range(len(objlist)):
            obj = objlist[i]
            r = newranks[i]
            if rankdict.has_key(r):
                rankdict[r].append(obj)
            else:
                rankdict[r] = [obj]
            if addstat:
                obj.stat[addstat] = r
                obj.dict["Data"][addstat] = r
        newlist = []
        for r in rje.sortKeys(rankdict):
            if cutoff <= 0 or r <= cutoff:
                newlist += rankdict[r]

        ### Finish ###
        return newlist
    except:
        callobj.log.errorLog("Problem during rje_scoring.rankObj()", quitchoice=True)
        return objlist[0:]