3 # BioPerl module for Bio::Ontology::SimpleOntologyEngine
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
7 # Cared for by Peter Dimitrov <dimitrov@gnf.org>
9 # Copyright Peter Dimitrov
10 # (c) Peter Dimitrov, dimitrov@gnf.org, 2002.
11 # (c) GNF, Genomics Institute of the Novartis Research Foundation, 2002.
13 # You may distribute this module under the same terms as perl itself.
14 # Refer to the Perl Artistic License (see the license accompanying this
15 # software package, or see http://www.perl.com/language/misc/Artistic.html)
16 # for the terms under which you may use, modify, and redistribute this module.
18 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
19 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 # POD documentation - main docs before the code
26 Bio::Ontology::SimpleOntologyEngine - Implementation of OntologyEngineI interface
30 my $soe = Bio::Ontology::SimpleOntologyEngine->new;
34 This is a "simple" implementation of Bio::Ontology::OntologyEngineI.
40 User feedback is an integral part of the evolution of this and other
41 Bioperl modules. Send your comments and suggestions preferably to
42 the Bioperl mailing list. Your participation is much appreciated.
44 bioperl-l@bioperl.org - General discussion
45 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
49 Please direct usage questions or support issues to the mailing list:
51 L<bioperl-l@bioperl.org>
53 rather than to the module maintainer directly. Many experienced and
54 reponsive experts will be able look at the problem and quickly
55 address it. Please include a thorough description of the problem
56 with code and data examples if at all possible.
60 Report bugs to the Bioperl bug tracking system to help us keep track
61 of the bugs and their resolution. Bug reports can be submitted via
64 http://bugzilla.open-bio.org/
66 =head1 AUTHOR - Peter Dimitrov
68 Email dimitrov@gnf.org
72 Hilmar Lapp, hlapp at gmx.net
76 The rest of the documentation details each of the object methods.
77 Internal methods are usually preceded with a _
82 # Let the code begin...
85 package Bio
::Ontology
::SimpleOntologyEngine
;
88 use Bio
::Ontology
::RelationshipFactory
;
91 use base
qw(Bio::Root::Root Bio::Ontology::OntologyEngineI);
96 Usage : $soe = Bio::Ontology::SimpleOntologyEngine->new;
97 Function: Initializes the ontology engine.
98 Example : $soe = Bio::Ontology::SimpleOntologyEngine->new;
99 Returns : Object of class SimpleOntologyEngine.
105 my ($class, @args) = @_;
106 my $self = $class->SUPER::new
(@args);
109 $self->_term_store( {} );
110 $self->_relationship_store( {} );
111 $self->_inverted_relationship_store( {} );
112 $self->_relationship_type_store( {} );
113 $self->_instantiated_terms_store( {} );
115 # set defaults for the factories
116 $self->relationship_factory(Bio
::Ontology
::RelationshipFactory
->new(
117 -type
=> "Bio::Ontology::Relationship"));
121 =head2 _instantiated_terms_store
123 Title : _instantiated_terms_store
124 Usage : $obj->_instantiated_terms_store($newval)
132 sub _instantiated_terms_store
{
133 my ($self, $value) = @_;
135 if( defined $value) {
136 $self->{'_instantiated_terms_store'} = $value;
138 return $self->{'_instantiated_terms_store'};
141 =head2 mark_instantiated
143 Title : mark_instantiated
144 Usage : $self->mark_instantiated(TermI terms): TermI
145 Function: Marks TermI objects as fully instantiated,
146 allowing for proper counting of the number of terms in the term store.
147 The TermI objects has to be already stored in the term store in order
149 Example : $self->mark_instantiated($term);
150 Returns : its argument or throws an exception if a term is not
152 Args : array of objects of class TermI.
156 sub mark_instantiated
{
157 my ($self, @terms) = @_;
159 foreach my $term (@terms) {
160 $self->throw( "term ".$term->identifier." not in the term store\n" )
161 if !defined $self->_term_store->{$term->identifier};
162 $self->_instantiated_terms_store->{$term->identifier} = 1;
168 =head2 mark_uninstantiated
170 Title : mark_uninstantiated
171 Usage : $self->mark_uninstantiated(TermI terms): TermI
172 Function: Marks TermI objects as not fully instantiated,
173 Example : $self->mark_uninstantiated($term);
174 Returns : its argument or throws an exception if a term is not
175 in the term store(if the term is not marked it does nothing).
176 Args : array of objects of class TermI.
180 sub mark_uninstantiated
{
181 my ($self, @terms) = @_;
183 foreach my $term (@terms) {
184 $self->throw( "term ".$term->identifier." not in the term store\n" )
185 if !defined $self->_term_store->{$term->identifier};
186 delete $self->_instantiated_terms_store->{$term->identifier}
187 if defined $self->_instantiated_terms_store->{$term->identifier};
196 Usage : $obj->_term_store($newval)
199 Returns : reference to an array of Bio::Ontology::TermI objects
200 Args : reference to an array of Bio::Ontology::TermI objects
205 my ($self, $value) = @_;
207 if( defined $value) {
208 if ( defined $self->{'_term_store'}) {
209 $self->throw("_term_store already defined\n");
212 $self->{'_term_store'} = $value;
216 return $self->{'_term_store'};
222 Usage : add_term(TermI term): TermI
223 Function: Adds TermI object to the ontology engine term store.
224 Marks the term fully instantiated by default.
225 Example : $soe->add_term($term)
226 Returns : its argument.
227 Args : object of class TermI.
232 my ($self, $term) = @_;
233 my $term_store = $self->_term_store;
235 if ( defined $term_store -> {$term->identifier}) {
236 $self->throw( "term ".$term->identifier." already defined\n" );
239 $term_store->{$term->identifier} = $term;
240 $self->_instantiated_terms_store->{$term->identifier} = 1;
246 =head2 get_term_by_identifier
248 Title : get_term_by_identifier
249 Usage : get_term_by_identifier(String id): TermI
250 Function: Retrieves terms from the term store by their identifier
251 field, or an empty list if not there.
252 Example : $term = $soe->get_term_by_identifier("IPR000001");
253 Returns : An array of zero or more Bio::Ontology::TermI objects.
254 Args : An array of identifier strings
258 sub get_term_by_identifier
{
259 my ($self, @ids) = @_;
262 foreach my $id (@ids) {
263 my $term = $self->_term_store->{$id};
264 push @ans, $term if defined $term;
270 =head2 _get_number_rels
272 Title : get_number_rels
281 sub _get_number_rels
{
285 foreach my $entry ($self->_relationship_store) {
286 $num_rels += scalar keys %$entry;
291 =head2 _get_number_terms
293 Title : _get_number_terms
302 sub _get_number_terms
{
305 return scalar $self->_filter_unmarked( values %{$self->_term_store} );
309 =head2 _relationship_store
311 Title : _storerelationship_store
312 Usage : $obj->relationship_store($newval)
315 Returns : reference to an array of Bio::Ontology::TermI objects
316 Args : reference to an array of Bio::Ontology::TermI objects
320 sub _relationship_store
{
321 my ($self, $value) = @_;
323 if( defined $value) {
324 if ( defined $self->{'_relationship_store'}) {
325 $self->throw("_relationship_store already defined\n");
328 $self->{'_relationship_store'} = $value;
332 return $self->{'_relationship_store'};
335 =head2 _inverted_relationship_store
337 Title : _inverted_relationship_store
341 Returns : reference to an array of Bio::Ontology::TermI objects
342 Args : reference to an array of Bio::Ontology::TermI objects
346 sub _inverted_relationship_store
{
347 my ($self, $value) = @_;
349 if( defined $value) {
350 if ( defined $self->{'_inverted_relationship_store'}) {
351 $self->throw("_inverted_relationship_store already defined\n");
354 $self->{'_inverted_relationship_store'} = $value;
358 return $self->{'_inverted_relationship_store'};
361 =head2 _relationship_type_store
363 Title : _relationship_type_store
364 Usage : $obj->_relationship_type_store($newval)
367 Returns : reference to an array of Bio::Ontology::RelationshipType objects
368 Args : reference to an array of Bio::Ontology::RelationshipType objects
372 sub _relationship_type_store
{
373 my ($self, $value) = @_;
375 if( defined $value) {
376 if ( defined $self->{'_relationship_type_store'}) {
377 $self->throw("_relationship_type_store already defined\n");
380 $self->{'_relationship_type_store'} = $value;
384 return $self->{'_relationship_type_store'};
387 =head2 _add_relationship_simple
389 Title : _add_relationship_simple
398 sub _add_relationship_simple
{
399 my ($self, $store, $rel, $inverted) = @_;
404 $parent_id = $rel->subject_term->identifier;
405 $child_id = $rel->object_term->identifier;
408 $parent_id = $rel->object_term->identifier;
409 $child_id = $rel->subject_term->identifier;
411 if(defined $store->{$parent_id} && (defined $store->{$parent_id}->{$child_id}) &&
412 ($store->{$parent_id}->{$child_id}->name != $rel->predicate_term->name)){
413 $self->throw("relationship ".Dumper
($rel->predicate_term).
414 " between ".$parent_id." and ".$child_id.
415 " already defined as ".
416 Dumper
($store->{$parent_id}->{$child_id})."\n");
419 $store->{$parent_id}->{$child_id} = $rel->predicate_term;
423 =head2 add_relationship
425 Title : add_relationship
426 Usage : add_relationship(RelationshipI relationship): RelationshipI
427 Function: Adds a relationship object to the ontology engine.
429 Returns : Its argument.
430 Args : A RelationshipI object.
434 sub add_relationship
{
435 my ($self, $rel) = @_;
437 $self->_add_relationship_simple($self->_relationship_store,
439 $self->_add_relationship_simple($self->_inverted_relationship_store,
441 $self->_relationship_type_store->{
442 $self->_unique_termid($rel->predicate_term)} = $rel->predicate_term;
447 =head2 get_relationships
449 Title : get_relationships
450 Usage : get_relationships(): RelationshipI
451 Function: Retrieves all relationship objects.
453 Returns : Array of RelationshipI objects
458 sub get_relationships
{
462 my $store = $self->_relationship_store;
463 my $relfact = $self->relationship_factory();
465 my @parent_ids = $term ?
466 # if a term is supplied then only get the term's parents
467 (map { $_->identifier(); } $self->get_parent_terms($term)) :
468 # otherwise use all parent ids
470 # add the term as a parent too if one is supplied
471 push(@parent_ids,$term->identifier) if $term;
473 foreach my $parent_id (@parent_ids) {
474 my $parent_entry = $store->{$parent_id};
476 # if a term is supplied, add a relationship for the parent to the term
477 # except if the parent is the term itself (we added that one before)
478 if($term && ($parent_id ne $term->identifier())) {
479 my @parent_terms = $self->get_term_by_identifier($parent_id);
480 foreach my $parent_term (@parent_terms) {
482 $relfact->create_object(-object_term
=> $parent_term,
483 -subject_term
=> $term,
485 $parent_entry->{$term->identifier},
486 -ontology
=> $term->ontology())
491 # otherwise, i.e., no term supplied, or the parent equals the
493 my @parent_terms = $term ?
494 ($term) : $self->get_term_by_identifier($parent_id);
495 foreach my $child_id (keys %$parent_entry) {
496 my $rel_info = $parent_entry->{$child_id};
497 my ($subj_term) = $self->get_term_by_identifier($child_id);
499 foreach my $parent_term (@parent_terms) {
501 $relfact->create_object(-object_term
=> $parent_term,
502 -subject_term
=> $subj_term,
503 -predicate_term
=> $rel_info,
504 -ontology
=>$parent_term->ontology
515 =head2 get_all_relationships
517 Title : get_all_relationships
518 Usage : get_all_relationships(): RelationshipI
519 Function: Retrieves all relationship objects.
521 Returns : Array of RelationshipI objects
526 sub get_all_relationships
{
527 return shift->get_relationships();
530 =head2 get_predicate_terms
532 Title : get_predicate_terms
533 Usage : get_predicate_terms(): TermI
534 Function: Retrives all relationship types stored in the engine
536 Returns : reference to an array of Bio::Ontology::RelationshipType objects
541 sub get_predicate_terms
{
544 return values %{$self->_relationship_type_store};
559 my ($self, $term, @rel_types) = @_;
561 foreach my $rel_type (@rel_types) {
562 if($rel_type->identifier || $term->identifier) {
563 return 1 if $rel_type->identifier eq $term->identifier;
565 return 1 if $rel_type->name eq $term->name;
572 =head2 _typed_traversal
574 Title : _typed_traversal
583 sub _typed_traversal
{
584 my ($self, $rel_store, $level, $term_id, @rel_types) = @_;
585 return if !defined($rel_store->{$term_id});
586 my %parent_entry = %{$rel_store->{$term_id}};
587 my @children = keys %parent_entry;
591 if (@rel_types > 0) {
594 foreach my $child_id (@children) {
596 if $self->_is_rel_type( $rel_store->{$term_id}->{$child_id},
606 foreach my $child_id (@ans) {
607 push @ans1, $self->_typed_traversal($rel_store,
608 $level - 1, $child_id, @rel_types)
609 if defined $rel_store->{$child_id};
617 =head2 get_child_terms
619 Title : get_child_terms
620 Usage : get_child_terms(TermI term, TermI predicate_terms): TermI
621 get_child_terms(TermI term, RelationshipType predicate_terms): TermI
622 Function: Retrieves all child terms of a given term, that satisfy a
623 relationship among those that are specified in the second
624 argument or undef otherwise. get_child_terms is a special
625 case of get_descendant_terms, limiting the search to the
628 Returns : Array of TermI objects.
629 Args : First argument is the term of interest, second is the list of
630 relationship type terms.
635 my ($self, $term, @relationship_types) = @_;
637 $self->throw("must provide TermI compliant object")
638 unless defined($term) && $term->isa("Bio::Ontology::TermI");
640 return $self->_filter_unmarked(
641 $self->get_term_by_identifier(
642 $self->_typed_traversal($self->_relationship_store,
645 @relationship_types) ) );
648 =head2 get_descendant_terms
650 Title : get_descendant_terms
651 Usage : get_descendant_terms(TermI term, TermI rel_types): TermI
652 get_child_terms(TermI term, RelationshipType predicate_terms): TermI
653 Function: Retrieves all descendant terms of a given term, that
654 satisfy a relationship among those that are specified in
655 the second argument or undef otherwise. Uses
656 _typed_traversal to find all descendants.
659 Returns : Array of TermI objects.
660 Args : First argument is the term of interest, second is the list of
661 relationship type terms.
665 sub get_descendant_terms
{
666 my ($self, $term, @relationship_types) = @_;
668 $self->throw("must provide TermI compliant object")
669 unless defined($term) && $term->isa("Bio::Ontology::TermI");
671 return $self->_filter_unmarked(
672 $self->_filter_repeated(
673 $self->get_term_by_identifier(
674 $self->_typed_traversal($self->_relationship_store,
677 @relationship_types) ) ) );
680 =head2 get_parent_terms
682 Title : get_parent_terms
683 Usage : get_parent_terms(TermI term, TermI predicate_terms): TermI
684 get_child_terms(TermI term, RelationshipType predicate_terms): TermI
685 Function: Retrieves all parent terms of a given term, that satisfy a
686 relationship among those that are specified in the second
687 argument or undef otherwise. get_parent_terms is a special
688 case of get_ancestor_terms, limiting the search to the
692 Returns : Array of TermI objects.
693 Args : First argument is the term of interest, second is the list of relationship type terms.
697 sub get_parent_terms
{
698 my ($self, $term, @relationship_types) = @_;
699 $self->throw("term must be a valid object, not undef") unless defined $term;
701 return $self->_filter_unmarked(
702 $self->get_term_by_identifier(
703 $self->_typed_traversal($self->_inverted_relationship_store,
706 @relationship_types) ) );
709 =head2 get_ancestor_terms
711 Title : get_ancestor_terms
712 Usage : get_ancestor_terms(TermI term, TermI predicate_terms): TermI
713 get_child_terms(TermI term, RelationshipType predicate_terms): TermI
714 Function: Retrieves all ancestor terms of a given term, that satisfy
715 a relationship among those that are specified in the second
716 argument or undef otherwise. Uses _typed_traversal to find
720 Returns : Array of TermI objects.
721 Args : First argument is the term of interest, second is the list
722 of relationship type terms.
726 sub get_ancestor_terms
{
727 my ($self, $term, @relationship_types) = @_;
728 $self->throw("term must be a valid object, not undef") unless defined $term;
730 return $self->_filter_unmarked(
731 $self->_filter_repeated(
732 $self->get_term_by_identifier(
733 $self->_typed_traversal($self->_inverted_relationship_store,
736 @relationship_types) ) ) );
739 =head2 get_leaf_terms
741 Title : get_leaf_terms
742 Usage : get_leaf_terms(): TermI
743 Function: Retrieves all leaf terms from the ontology. Leaf term is a term w/o descendants.
744 Example : @leaf_terms = $obj->get_leaf_terms()
745 Returns : Array of TermI objects.
754 foreach my $term (values %{$self->_term_store}) {
755 push @leaf_terms, $term
756 if !defined $self->_relationship_store->{$term->identifier} &&
757 defined $self->_instantiated_terms_store->{$term->identifier};
763 =head2 get_root_terms
765 Title : get_root_terms
766 Usage : get_root_terms(): TermI
767 Function: Retrieves all root terms from the ontology. Root term is a term w/o descendants.
768 Example : @root_terms = $obj->get_root_terms()
769 Returns : Array of TermI objects.
778 foreach my $term (values %{$self->_term_store}) {
779 push @root_terms, $term
780 if !defined $self->_inverted_relationship_store->{$term->identifier} &&
781 defined $self->_instantiated_terms_store->{$term->identifier};
787 =head2 _filter_repeated
789 Title : _filter_repeated
790 Usage : @lst = $self->_filter_repeated(@old_lst);
791 Function: Removes repeated terms
793 Returns : List of unique TermI objects
794 Args : List of TermI objects
798 sub _filter_repeated
{
799 my ($self, @args) = @_;
802 foreach my $element (@args) {
803 $h{$element->identifier} = $element if !defined $h{$element->identifier};
811 Title : get_all_terms
812 Usage : get_all_terms(): TermI
813 Function: Retrieves all terms currently stored in the ontology.
814 Example : @all_terms = $obj->get_all_terms()
815 Returns : Array of TermI objects.
823 return $self->_filter_unmarked( values %{$self->_term_store} );
829 Usage : ($term) = $oe->find_terms(-identifier => "SO:0000263");
830 Function: Find term instances matching queries for their attributes.
832 This implementation can efficiently resolve queries by
836 Returns : an array of zero or more Bio::Ontology::TermI objects
837 Args : Named parameters. The following parameters should be recognized
838 by any implementations:
840 -identifier query by the given identifier
841 -name query by the given name
846 my ($self,@args) = @_;
849 my ($id,$name) = $self->_rearrange([qw(IDENTIFIER NAME)],@args);
852 @terms = $self->get_term_by_identifier($id);
854 @terms = $self->get_all_terms();
857 @terms = grep { $_->name() eq $name; } @terms;
863 =head2 relationship_factory
865 Title : relationship_factory
866 Usage : $fact = $obj->relationship_factory()
867 Function: Get/set the object factory to be used when relationship
868 objects are created by the implementation on-the-fly.
871 Returns : value of relationship_factory (a Bio::Factory::ObjectFactoryI
873 Args : on set, a Bio::Factory::ObjectFactoryI compliant object
877 sub relationship_factory
{
880 return $self->{'relationship_factory'} = shift if @_;
881 return $self->{'relationship_factory'};
887 Usage : $fact = $obj->term_factory()
888 Function: Get/set the object factory to be used when term objects are
889 created by the implementation on-the-fly.
891 Note that this ontology engine implementation does not
892 create term objects on the fly, and therefore setting this
893 attribute is meaningless.
896 Returns : value of term_factory (a Bio::Factory::ObjectFactoryI
898 Args : on set, a Bio::Factory::ObjectFactoryI compliant object
906 $self->warn("setting term factory, but ".ref($self).
907 " does not create terms on-the-fly");
908 return $self->{'term_factory'} = shift;
910 return $self->{'term_factory'};
913 =head2 _filter_unmarked
915 Title : _filter_unmarked
916 Usage : _filter_unmarked(TermI terms): TermI
917 Function: Removes the uninstantiated terms from the list of terms
919 Returns : array of fully instantiated TermI objects
920 Args : array of TermI objects
924 sub _filter_unmarked
{
925 my ($self, @terms) = @_;
926 my @filtered_terms = ();
928 if ( scalar(@terms) >= 1) {
929 foreach my $term (@terms) {
930 push @filtered_terms, $term
931 if defined $self->_instantiated_terms_store->{$term->identifier};
935 return @filtered_terms;
938 =head2 remove_term_by_id
940 Title : remove_term_by_id
941 Usage : remove_term_by_id(String id): TermI
942 Function: Removes TermI object from the ontology engine using the
943 string id as an identifier. Current implementation does not
944 enforce consistency of the relationships using that term.
945 Example : $term = $soe->remove_term_by_id($id);
946 Returns : Object of class TermI or undef if not found.
947 Args : The string identifier of a term.
951 sub remove_term_by_id
{
952 my ($self, $id) = @_;
954 if ( $self->get_term_by_identifier($id) ) {
955 my $term = $self->{_term_store
}->{$id};
956 delete $self->{_term_store
}->{$id};
960 $self->warn("Term with id '$id' is not in the term store");
968 Usage : print $sv->to_string();
969 Function: Currently returns formatted string containing the number of
970 terms and number of relationships from the ontology engine.
971 Example : print $sv->to_string();
981 $s .= "-- # Terms:\n";
982 $s .= scalar($self->get_all_terms)."\n";
983 $s .= "-- # Relationships:\n";
984 $s .= $self->_get_number_rels."\n";
989 =head2 _unique_termid
991 Title : _unique_termid
993 Function: Returns a string that can be used as ID using fail-over
996 If the identifier attribute is not set, it uses the
997 combination of name and ontology name, provided both are
998 set. If they are not, it returns the name alone.
1000 Note that this is a private method. Call from inheriting
1001 classes but not from outside.
1005 Args : a Bio::Ontology::TermI compliant object
1013 return $term->identifier() if $term->identifier();
1014 my $id = $term->ontology->name() if $term->ontology();
1020 $id .= $term->name();
1024 #################################################################
1026 #################################################################
1028 *get_relationship_types
= \
&get_predicate_terms
;