Aleš Erjavec avatar Aleš Erjavec committed 54a9507

Added traversal methods, term_by_name.

Comments (0)

Files changed (1)

             elif id in self.alt2id:
                 return self.id2term[self.alt2id[id]]
             else:
-                raise ValueError("Unknown term id: %s" % id)
+                raise ValueError("Unknown term id: %r" % id)
         elif isinstance(id, OBOObject):
             return id
         
         """
         return [obj for obj in self.objects if obj.stanza_type == "Term"]
     
+    def term_by_name(self, name):
+        """ Return the term with name ``name``. 
+        """
+        terms = [t for t in self.terms() if t.name == name]
+        if len(terms) != 1:
+            raise ValueError("Unknown term name: %r" % name)
+        return terms[0]
+    
     def typedefs(self):
         """ Return all `Typedef` instances in the ontology.
         """
     def has_key(self, key):
         return self.id2tem.has_key(key)
     
+    def traverse_bf(self, term):
+        """ BF traverse of the ontology down from term.
+        """
+        queue = list(self.child_terms(term))
+        while queue:
+            term = queue.pop(0)
+            queue.extend(self.child_terms(term))
+            yield term
+                    
+    def traverse_df(self, term, depth=1e30):
+        """ DF traverse of the ontology down from term.
+        """
+        if depth >= 1:
+            for child in self.child_terms(term):
+                yield child
+                for t in self.traverse_df(child, depth-1):
+                    yield t
+        
+    
     def to_network(self, terms=None):
         """ Return an Orange.network.Network instance constructed from
         this ontology.
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.