Changeset 1578:9bc83a60db78 in orange-bioinformatics for obiGene.py


Ignore:
Timestamp:
02/22/12 11:49:26 (2 years ago)
Author:
markotoplak
Branch:
default
Children:
1579:364880120934, 1583:2b90e7c95085
rebase_source:
7341d6e68353eb1fad743387809483d39c88b813
Message:

Gene matcher now have a call function and return a Match object, which is used for matching. Backward compatibility was retained.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • obiGene.py

    r1468 r1578  
    418418    """ 
    419419 
     420    def copy(self): 
     421        return notImplemented() 
     422 
     423    def __call__(self, targets): 
     424        return self.set_targets(targets) 
     425 
    420426    def set_targets(self, targets): 
    421427        """ 
     
    518524    def set_targets(self, targets): 
    519525        """ 
    520         A reverse dictionary is made accordint to each target's membership 
     526        A reverse dictionary is made according to each target's membership 
    521527        in the sets of aliases. 
    522528        """ 
    523529        d = defaultdict(list) 
     530        #d = id: [ targets ], where id is index of the set of aliases 
    524531        for target in targets: 
    525532            ids = self.to_ids(target) 
     
    527534                for id in ids: 
    528535                    d[id].append(target) 
    529         self.to_targets = d 
     536        mo = MatchAliases(d, self) 
     537        self.matcho = mo #backward compatibility - default match object 
     538        return mo 
     539 
     540    #this two functions are solely for backward compatibility 
     541    def match(self, gene): 
     542        return self.matcho.match(gene) 
     543    def explain(self, gene): 
     544        return self.matcho.explain(gene) 
     545 
     546class Match(object): 
     547    pass 
     548  
     549class MatchAliases(Match): 
     550 
     551    def __init__(self, to_targets, parent): 
     552        self.to_targets = to_targets 
     553        self.parent = parent 
    530554 
    531555    def match(self, gene): 
     
    535559        as input's match. 
    536560        """ 
    537         inputgeneids = self.to_ids(gene) 
     561        inputgeneids = self.parent.to_ids(gene) 
    538562        #return target genes with same ids 
    539563        return list(set( \ 
    540             reduce(lambda x,y:x+y,  
     564            reduce(lambda x,y: x+y,  
    541565                [ self.to_targets[igid] for igid in inputgeneids ], []))) 
    542566 
    543567    def explain(self, gene): 
    544         inputgeneids = self.to_ids(gene) 
    545         return [ (self.to_targets[igid], self.aliases[igid]) for igid in inputgeneids ] 
     568        inputgeneids = self.parent.to_ids(gene) 
     569        return [ (self.to_targets[igid], self.parent.aliases[igid]) for igid in inputgeneids ] 
    546570 
    547571class MatcherAliasesPickled(MatcherAliases): 
     
    580604 
    581605    def set_targets(self, targets): 
    582         MatcherAliases.set_targets(self, targets) 
     606        return MatcherAliases.set_targets(self, targets) 
    583607 
    584608    def filename(self): 
     
    828852class MatcherSequence(Matcher): 
    829853    """ 
    830     Chaining of gene matchers. 
    831      
    832     User defines the order of gene matchers. Each gene is goes through 
    833     sequence of gene matchers until a match is found. 
     854    Each gene goes through sequence of gene matchers (in the same order 
     855    as in the matchers arguments) until a match is found. 
    834856    """ 
    835857     
     
    837859        self.matchers = matchers 
    838860 
     861    def set_targets(self, targets): 
     862        ms = [] 
     863        for matcher in self.matchers: 
     864            ms.append(matcher.set_targets(targets)) 
     865        om = MatchSequence(ms) 
     866        self.matcho = om 
     867        return om 
     868 
     869    #this two functions are solely for backward compatibility 
    839870    def match(self, gene): 
    840         for matcher in self.matchers: 
    841             m = matcher.match(gene) 
     871        return self.matcho.match(gene) 
     872    def explain(self, gene): 
     873        return self.matcho.explain(gene) 
     874 
     875class MatchSequence(Match): 
     876 
     877    def __init__(self, ms): 
     878        self.ms = ms 
     879 
     880    def match(self, gene): 
     881        for match in self.ms: 
     882            m = match.match(gene) 
    842883            if m:  
    843884                return m 
    844885        return [] 
    845886 
    846     def set_targets(self, targets): 
    847         for matcher in self.matchers: 
    848             matcher.set_targets(targets) 
    849  
    850887    def explain(self, gene): 
    851         for matcher in self.matchers: 
    852             m = matcher.match(gene) 
     888        for match in self.ms: 
     889            m = match.match(gene) 
    853890            if m:  
    854                 return matcher.explain(gene) 
     891                return match.explain(gene) 
    855892        return [] 
    856893 
     
    866903        aliases = [ set([a]) for a in targets] 
    867904        self.am = MatcherAliases(aliases, ignore_case=self.ignore_case) 
    868         self.am.set_targets(targets) 
    869  
     905        self.matcho = self.am.set_targets(targets) 
     906        return self.matcho 
     907 
     908    #this two functions are solely for backward compatibility 
    870909    def match(self, gene): 
    871         return self.am.match(gene) 
    872                  
     910        return self.matcho.match(gene) 
     911    def explain(self, gene): 
     912        return self.matcho.explain(gene) 
     913 
     914                
    873915GMDirect = MatcherDirect 
    874916GMKEGG = MatcherAliasesKEGG 
Note: See TracChangeset for help on using the changeset viewer.