Commits

Marko Toplak committed 9bc83a6

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

Comments (0)

Files changed (2)

     Gene matcher tries to match an input gene to some target.
     """
 
+    def copy(self):
+        return notImplemented()
+
+    def __call__(self, targets):
+        return self.set_targets(targets)
+
     def set_targets(self, targets):
         """
         Set input list of gene names as targets. 
 
     def set_targets(self, targets):
         """
-        A reverse dictionary is made accordint to each target's membership
+        A reverse dictionary is made according to each target's membership
         in the sets of aliases.
         """
         d = defaultdict(list)
+        #d = id: [ targets ], where id is index of the set of aliases
         for target in targets:
             ids = self.to_ids(target)
             if ids != None:
                 for id in ids:
                     d[id].append(target)
-        self.to_targets = d
+        mo = MatchAliases(d, self)
+        self.matcho = mo #backward compatibility - default match object
+        return mo
+
+    #this two functions are solely for backward compatibility
+    def match(self, gene):
+        return self.matcho.match(gene)
+    def explain(self, gene):
+        return self.matcho.explain(gene)
+
+class Match(object):
+    pass
+ 
+class MatchAliases(Match):
+
+    def __init__(self, to_targets, parent):
+        self.to_targets = to_targets
+        self.parent = parent
 
     def match(self, gene):
         """
         it. Target genes belonding to the same sets of aliases are returned
         as input's match.
         """
-        inputgeneids = self.to_ids(gene)
+        inputgeneids = self.parent.to_ids(gene)
         #return target genes with same ids
         return list(set( \
-            reduce(lambda x,y:x+y, 
+            reduce(lambda x,y: x+y, 
                 [ self.to_targets[igid] for igid in inputgeneids ], [])))
 
     def explain(self, gene):
-        inputgeneids = self.to_ids(gene)
-        return [ (self.to_targets[igid], self.aliases[igid]) for igid in inputgeneids ]
+        inputgeneids = self.parent.to_ids(gene)
+        return [ (self.to_targets[igid], self.parent.aliases[igid]) for igid in inputgeneids ]
 
 class MatcherAliasesPickled(MatcherAliases):
     """
     mdict = property(get_mdict, set_mdict)
 
     def set_targets(self, targets):
-        MatcherAliases.set_targets(self, targets)
+        return MatcherAliases.set_targets(self, targets)
 
     def filename(self):
         """ Returns file name for saving aliases. """
         
 class MatcherSequence(Matcher):
     """
-    Chaining of gene matchers.
-    
-    User defines the order of gene matchers. Each gene is goes through
-    sequence of gene matchers until a match is found.
+    Each gene goes through sequence of gene matchers (in the same order
+    as in the matchers arguments) until a match is found.
     """
     
     def __init__(self, matchers):
         self.matchers = matchers
 
+    def set_targets(self, targets):
+        ms = []
+        for matcher in self.matchers:
+            ms.append(matcher.set_targets(targets))
+        om = MatchSequence(ms)
+        self.matcho = om
+        return om
+
+    #this two functions are solely for backward compatibility
     def match(self, gene):
-        for matcher in self.matchers:
-            m = matcher.match(gene)
+        return self.matcho.match(gene)
+    def explain(self, gene):
+        return self.matcho.explain(gene)
+
+class MatchSequence(Match):
+
+    def __init__(self, ms):
+        self.ms = ms
+
+    def match(self, gene):
+        for match in self.ms:
+            m = match.match(gene)
             if m: 
                 return m
         return []
 
-    def set_targets(self, targets):
-        for matcher in self.matchers:
-            matcher.set_targets(targets)
-
     def explain(self, gene):
-        for matcher in self.matchers:
-            m = matcher.match(gene)
+        for match in self.ms:
+            m = match.match(gene)
             if m: 
-                return matcher.explain(gene)
+                return match.explain(gene)
         return []
 
 class MatcherDirect(Matcher):
     def set_targets(self, targets):
         aliases = [ set([a]) for a in targets]
         self.am = MatcherAliases(aliases, ignore_case=self.ignore_case)
-        self.am.set_targets(targets)
+        self.matcho = self.am.set_targets(targets)
+        return self.matcho
 
+    #this two functions are solely for backward compatibility
     def match(self, gene):
-        return self.am.match(gene)
-                
+        return self.matcho.match(gene)
+    def explain(self, gene):
+        return self.matcho.explain(gene)
+
+               
 GMDirect = MatcherDirect
 GMKEGG = MatcherAliasesKEGG
 GMGO = MatcherAliasesGO
 import Orange
 import obiAssess
 import Orange.misc
-import orange
 import obiGeneSets
 import obiGene
 import numpy
 import statc
 import stats
 
+def selectGenesetsData(data, matcher, geneSets, minSize=3, maxSize=1000, minPart=0.1, classValues=None):
+    """
+    Returns gene sets and data which falling under upper criteria.
+    """
+    gso = obiGsea.GSEA(data, matcher=matcher, classValues=classValues, atLeast=0)
+    gso.addGenesets(geneSets)
+    okgenesets = gso.selectGenesets(minSize=minSize, maxSize=maxSize, minPart=minPart).keys()
+    gsetsnum = gso.to_gsetsnum(okgenesets)
+    return gso.data, okgenesets, gsetsnum
+
 class SetSig(object):
 
     __new__ = Orange.misc._orange__new__(object)
 
-    def __init__(self, matcher, geneSets, minSize=3, maxSize=1000, minPart=0.1, classValues=None):
+    def __init__(self, matcher, gene_sets, min_size=3, max_size=1000, min_part=0.1, class_values=None):
         self.matcher = matcher
-        self.geneSets = geneSets
-        self.minSize = minSize
-        self.maxSize = maxSize
-        self.minPart = minPart
-        self.classValues = classValues
+        self.gene_sets = gene_sets
+        self.min_size = min_size
+        self.max_size = max_size
+        self.min_part = min_part
+        self.class_values = class_values
 
     def __call__(self, data, weight_id=None):
         data, oknames, gsetsnum = obiAssess.selectGenesetsData(data, 
-            self.matcher, self.geneSets,
-            minSize=self.minSize, maxSize=self.maxSize, 
-            minPart=self.minPart, classValues=self.classValues)
+            self.matcher, self.gene_sets,
+            minSize=self.min_size, maxSize=self.max_size, 
+            minPart=self.min_part, classValues=self.class_values)
 
         def setSig_example_geneset(ex, data):
             """ ex contains only selected genes """
             at = Orange.feature.Continuous(name=name.id)
 
             def t(ex, w, gs=gs, ldata=data):
-                domain = orange.Domain([ldata.domain.attributes[ai] for ai in gs], ldata.domain.classVar)
-                datao = orange.ExampleTable(domain, ldata)
-                example = orange.Example(domain, ex) #domains need to be the same
+                domain = Orange.data.Domain([ldata.domain.attributes[ai] for ai in gs], ldata.domain.classVar)
+                datao = Orange.data.Table(domain, ldata)
+                example = Orange.data.Instance(domain, ex) #domains need to be the same
                 return setSig_example_geneset(example, datao)
          
             at.get_value_from = t
         })
 
     fp = 120
-    ldata = orange.ExampleTable(data.domain, data[:fp])
-    tdata = orange.ExampleTable(data.domain, data[fp:])
+    ldata = Orange.data.Table(data.domain, data[:fp])
+    tdata = Orange.data.Table(data.domain, data[fp:])
 
     matcher = obiGene.matcher([])
 
         ol =  sorted(ar.items())
         print '\n'.join([ a + ": " +str(b) for a,b in ol])
 
-    ass = SetSig(ldata, matcher=matcher, geneSets=gsets, classValues=choosen_cv, minPart=0.0)
+    ass = SetSig(ldata, matcher=matcher, gene_sets=gsets, class_values=choosen_cv, min_part=0.0)
     print ass.domain
     ar = to_old_dic(ass.domain, data[:5])
     pp2(ar)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.