Changeset 1355:cfa7394cb76b in orangebioinformatics
 Timestamp:
 03/25/11 14:33:44 (3 years ago)
 Branch:
 default
 Convert:
 22e5d5c93288099fdbc5b738bf3e17815249ba62
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

obiOntology.py
r1349 r1355 5 5 Python module for manipulating OBO Ontology files (http://www.obofoundry.org/) 6 6 7 Example::7 You can construct an ontology from scratch with custom terms :: 8 8 9 9 >>> term = OBOObject("Term", id="foo:bar", name="Foo bar") … … 15 15 >>> ontology = OBOOntology() 16 16 >>> ontology.add_object(term) 17 17 >>> ontology.add_header_tag("createdby", "ales") # add a header tag 18 >>> from StringIO import StringIO 19 >>> buffer = StringIO() 20 >>> ontology.dump(buffer) # Save the ontology to a file like object 21 >>> print buffer.getvalue() # Print the contents of the buffer 22 createdby: ales 23 <BLANKLINE> 24 [Term] 25 id: foo:bar 26 name: Foo bar 27 <BLANKLINE> 28 29 To load an ontology from a file pass the file or filename to the OBOOntology 30 constructor or call its load method :: 31 32 >>> buffer.seek(0) # rewind 33 >>> ontology = OBOOntology(buffer) 34 >>> # Or 35 >>> buffer.seek(0) # rewind 36 >>> ontology = OBOOntology() 37 >>> ontology.load(buffer) 38 39 See OBOOntology docstring for more details. 40 18 41 """ 19 42 … … 347 370 ... print event, value 348 371 ... 349 HEADER_TAG ['header_tag', ' 372 HEADER_TAG ['header_tag', 'header_value'] 350 373 START_STANZA Term 351 374 TAG_VALUE ('id', 'FOO', 'modifier=bar', 'comment') … … 359 382 """ Parse the file and yield parse events. 360 383 361 .. TODO: List events 384 .. TODO: List events and values 362 385 """ 363 386 data = self.file.read() … … 366 389 for line in header.splitlines(): 367 390 if line.strip(): 368 yield "HEADER_TAG", line.split(": ", 1)391 yield "HEADER_TAG", line.split(": ", 1) 369 392 370 393 current = None … … 396 419 397 420 class OBOOntology(object): 421 """ The main ontology object. 422 """ 423 398 424 BUILTINS = BUILTIN_OBO_OBJECTS 399 400 425 def __init__(self, file=None): 401 426 """ Init an ontology instance from a file like object (.obo format) … … 466 491 467 492 for key, value in self.header_tags: 468 file.write(key + ":" + value + "\n") 469 for object in self.objects: 493 file.write(key + ": " + value + "\n") 494 495 # Skip the builtins 496 for object in self.objects[len(self.BUILTINS):]: 470 497 file.write("\n") 471 498 file.write(object.format_stanza()) … … 545 572 related.append(tuple(rel.split(None, 1))) 546 573 return related 547 548 def to_network(self): 574 575 def edge_types(self): 576 """ Return a list of all edge types in the ontology 577 """ 578 return [obj.id for obj in self.objects if obj.stanza_type == "Typedef"] 579 580 def parent_edges(self, term): 581 """ Return a list of (rel_type, parent_term) tuples 582 """ 583 term = self.term(term) 584 parents = [] 585 for rel_type, parent in self.related_terms(term): 586 parents.append((rel_type, self.term(parent))) 587 return parents 588 589 def child_edges(self, term): 590 """ Return a list of (rel_type, source_term) tuples 591 """ 592 self._cache_validate() 593 term = self.term(term) 594 return self._related_to.get(term, []) 595 596 597 def super_terms(self, term): 598 """ Return a set of all super terms of `term` up to the most general one. 599 """ 600 terms = self.parent_terms(term) 601 visited = set() 602 queue = set(terms) 603 while queue: 604 term = queue.pop() 605 visited.add(term) 606 queue.update(self.parent_terms(term)  visited) 607 return visited 608 609 def sub_terms(self, term): 610 """ Return a set of all sub terms for `term`. 611 """ 612 terms = self.child_terms(term) 613 visited = set() 614 queue = set(terms) 615 while queue: 616 term = queue.pop() 617 visited.add(term) 618 queue.update(self.child_terms(term)  visited) 619 return visited 620 621 def child_terms(self, term): 622 """ Return a set of all child terms for this `term`. 623 """ 624 self._cache_validate() 625 term = self.term(term) 626 children = [] 627 for rel_type, term in self.child_edges(term): 628 children.append(term) 629 return set(children) 630 631 def parent_terms(self, term): 632 """ Return a set of all parent terms for this `term` 633 """ 634 term = self.term(term) 635 parents = [] 636 for rel_type, id in self.parent_edges(term): #term.related_objects(): 637 parents.append(self.term(id)) 638 return set(parents) 639 640 def relations(self): 641 """ Return a list of all relations in the ontology. 642 """ 643 relations = [] 644 for obj in self.objects: 645 for type_id, id in obj.related: 646 target_term = self.term(id) 647 relations.append((obj, type_id, target_term)) 648 return relations 649 650 def __len__(self): 651 return len(self.objects) 652 653 def __iter__(self): 654 return iter(self.objects) 655 656 def __contains__(self, obj): 657 if isinstance(obj, basestring): 658 return obj in self.id2term 659 else: 660 return obj in self.objects 661 662 def __getitem__(self, key): 663 return self.id2term[key] 664 665 def has_key(self, key): 666 return self.id2tem.has_key(key) 667 668 def to_network(self, terms=None): 549 669 """ Return an Orange.network.Network instance constructed from 550 670 this ontology. … … 595 715 return network 596 716 597 def to_networkx(self ):717 def to_networkx(self, terms=None): 598 718 """ Return a NetworkX graph of this ontology 599 719 """ 600 raise NotImplementedError 601 602 def edge_types(self): 603 """ Return a list of all edge types in the ontology 604 """ 605 return [obj.id for obj in self.objects if obj.stanza_type == "Typedef"] 606 607 def parent_edges(self, term): 608 """ Return a list of (rel_type, parent_term) tuples 609 """ 610 term = self.term(term) 611 parents = [] 612 for rel_type, parent in self.related_terms(term): 613 parents.append((rel_type, self.term(parent))) 614 return parents 615 616 def child_edges(self, term): 617 """ Return a list of (rel_type, source_term) tuples 618 """ 619 self._cache_validate() 620 term = self.term(term) 621 return self._related_to.get(term, []) 622 623 624 def super_terms(self, term): 625 """ Return a set of all super terms of `term` up to the most general one. 626 """ 627 terms = self.parent_terms(term) 628 visited = set() 629 queue = set(terms) 630 while queue: 631 term = queue.pop() 632 visited.add(term) 633 queue.update(self.parent_terms(term)  visited) 634 return visited 635 636 def sub_terms(self, term): 637 """ Return a set of all sub terms for `term`. 638 """ 639 terms = self.child_terms(term) 640 visited = set() 641 queue = set(terms) 642 while queue: 643 term = queue.pop() 644 visited.add(term) 645 queue.update(self.child_terms(term)  visited) 646 return visited 647 648 def child_terms(self, term): 649 """ Return a set of all child terms for this `term`. 650 """ 651 self._cache_validate() 652 term = self.term(term) 653 children = [] 654 for rel_type, term in self.child_edges(term): 655 children.append(term) 656 return set(children) 657 658 659 def parent_terms(self, term): 660 """ Return a set of all parent terms for this `term` 661 """ 662 term = self.term(term) 663 parents = [] 664 for rel_type, id in self.parent_edges(term): #term.related_objects(): 665 parents.append(self.term(id)) 666 return set(parents) 667 668 def relations(self): 669 """ Return a list of all relations in the ontology. 670 """ 671 relations = [] 672 for obj in self.objects: 673 for type_id, id in obj.related: 674 target_term = self.term(id) 675 relations.append((obj, type_id, target_term)) 676 return relations 677 678 def __len__(self): 679 return len(self.objects) 680 681 def __iter__(self): 682 return iter(self.objects) 683 684 def __contains__(self, obj): 685 if isinstance(obj, basestring): 686 return obj in self.id2term 720 import networkx 721 graph = networkx.Graph() 722 723 edge_types = self.edge_types() 724 725 edge_colors = {"is_a": "red"} 726 727 if terms is None: 728 terms = self.terms() 687 729 else: 688 return obj in self.objects 689 690 def __getitem__(self, key): 691 return self.id2term[key] 692 693 def has_key(self, key): 694 return self.id2tem.has_key(key) 730 terms = [self.term(term) for term in terms] 731 super_terms = [self.super_terms(term) for term in terms] 732 terms = reduce(set.union, super_terms, set(terms)) 733 734 for term in terms: 735 graph.add_node(term.id, name=term.name) 736 737 for term in terms: 738 for rel_type, rel_term in self.related_terms(term): 739 rel_term = self.term(rel_term) 740 if rel_term in terms: 741 graph.add_edge(term.id, rel_term.id, label=rel_type, color=edge_colors.get(rel_type, "blue")) 742 743 return graph 744 745 def to_graphviz(self, terms=None): 746 """ Return an pygraphviz.AGraph representation of the ontology in. 747 If `terms` is not `None` it must be a list of terms in the ontology. 748 The graph will in this case contain only the super graph of those 749 terms. 750 751 """ 752 import pygraphviz as pgv 753 graph = pgv.AGraph(directed=True, name="ontology") 754 755 edge_types = self.edge_types() 756 757 edge_colors = {"is_a": "red"} 758 759 if terms is None: 760 terms = self.terms() 761 else: 762 terms = [self.term(term) for term in terms] 763 super_terms = [self.super_terms(term) for term in terms] 764 terms = reduce(set.union, super_terms, set(terms)) 765 766 for term in terms: 767 graph.add_node(term.id, name=term.name) 768 769 for term in terms: 770 for rel_type, rel_term in self.related_terms(term): 771 rel_term = self.term(rel_term) 772 if rel_term in terms: 773 graph.add_edge(term.id, rel_term.id, label=rel_type, color=edge_colors.get(rel_type, "blue")) 774 775 return graph 695 776 696 777
Note: See TracChangeset
for help on using the changeset viewer.