sync w/ main trunk
[bioperl-live.git] / Bio / Ontology / SimpleOntologyEngine.pm
blob516c80f5411bd0135d89f1a52eafa4212d1d0d0c
1 # $Id$
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
24 =head1 NAME
26 Bio::Ontology::SimpleOntologyEngine - Implementation of OntologyEngineI interface
28 =head1 SYNOPSIS
30 my $soe = Bio::Ontology::SimpleOntologyEngine->new;
32 =head1 DESCRIPTION
34 This is a "simple" implementation of Bio::Ontology::OntologyEngineI.
36 =head1 FEEDBACK
38 =head2 Mailing Lists
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
47 =head2 Support
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.
58 =head2 Reporting Bugs
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
62 the web:
64 http://bugzilla.open-bio.org/
66 =head1 AUTHOR - Peter Dimitrov
68 Email dimitrov@gnf.org
70 =head1 CONTRIBUTORS
72 Hilmar Lapp, hlapp at gmx.net
74 =head1 APPENDIX
76 The rest of the documentation details each of the object methods.
77 Internal methods are usually preceded with a _
79 =cut
82 # Let the code begin...
85 package Bio::Ontology::SimpleOntologyEngine;
86 use strict;
87 use Carp;
88 use Bio::Ontology::RelationshipFactory;
89 use Data::Dumper;
91 use base qw(Bio::Root::Root Bio::Ontology::OntologyEngineI);
93 =head2 new
95 Title : new
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.
100 Args :
102 =cut
104 sub new{
105 my ($class, @args) = @_;
106 my $self = $class->SUPER::new(@args);
107 # my %param = @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"));
118 return $self;
121 =head2 _instantiated_terms_store
123 Title : _instantiated_terms_store
124 Usage : $obj->_instantiated_terms_store($newval)
125 Function:
126 Example :
127 Returns : hash
128 Args : empty hash
130 =cut
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
148 to be marked.
149 Example : $self->mark_instantiated($term);
150 Returns : its argument or throws an exception if a term is not
151 in the term store.
152 Args : array of objects of class TermI.
154 =cut
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;
165 return @terms;
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.
178 =cut
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};
190 return @terms;
193 =head2 _term_store
195 Title : term_store
196 Usage : $obj->_term_store($newval)
197 Function:
198 Example :
199 Returns : reference to an array of Bio::Ontology::TermI objects
200 Args : reference to an array of Bio::Ontology::TermI objects
202 =cut
204 sub _term_store{
205 my ($self, $value) = @_;
207 if( defined $value) {
208 if ( defined $self->{'_term_store'}) {
209 $self->throw("_term_store already defined\n");
211 else {
212 $self->{'_term_store'} = $value;
216 return $self->{'_term_store'};
219 =head2 add_term
221 Title : add_term
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.
229 =cut
231 sub add_term{
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" );
238 else {
239 $term_store->{$term->identifier} = $term;
240 $self->_instantiated_terms_store->{$term->identifier} = 1;
243 return $term;
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
256 =cut
258 sub get_term_by_identifier{
259 my ($self, @ids) = @_;
260 my @ans = ();
262 foreach my $id (@ids) {
263 my $term = $self->_term_store->{$id};
264 push @ans, $term if defined $term;
267 return @ans;
270 =head2 _get_number_rels
272 Title : get_number_rels
273 Usage :
274 Function:
275 Example :
276 Returns :
277 Args :
279 =cut
281 sub _get_number_rels{
282 my ($self) = @_;
283 my $num_rels = 0;
285 foreach my $entry ($self->_relationship_store) {
286 $num_rels += scalar keys %$entry;
288 return $num_rels;
291 =head2 _get_number_terms
293 Title : _get_number_terms
294 Usage :
295 Function:
296 Example :
297 Returns :
298 Args :
300 =cut
302 sub _get_number_terms{
303 my ($self) = @_;
305 return scalar $self->_filter_unmarked( values %{$self->_term_store} );
309 =head2 _relationship_store
311 Title : _storerelationship_store
312 Usage : $obj->relationship_store($newval)
313 Function:
314 Example :
315 Returns : reference to an array of Bio::Ontology::TermI objects
316 Args : reference to an array of Bio::Ontology::TermI objects
318 =cut
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");
327 else {
328 $self->{'_relationship_store'} = $value;
332 return $self->{'_relationship_store'};
335 =head2 _inverted_relationship_store
337 Title : _inverted_relationship_store
338 Usage :
339 Function:
340 Example :
341 Returns : reference to an array of Bio::Ontology::TermI objects
342 Args : reference to an array of Bio::Ontology::TermI objects
344 =cut
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");
353 else {
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)
365 Function:
366 Example :
367 Returns : reference to an array of Bio::Ontology::RelationshipType objects
368 Args : reference to an array of Bio::Ontology::RelationshipType objects
370 =cut
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");
379 else {
380 $self->{'_relationship_type_store'} = $value;
384 return $self->{'_relationship_type_store'};
387 =head2 _add_relationship_simple
389 Title : _add_relationship_simple
390 Usage :
391 Function:
392 Example :
393 Returns :
394 Args :
396 =cut
398 sub _add_relationship_simple{
399 my ($self, $store, $rel, $inverted) = @_;
400 my $parent_id;
401 my $child_id;
403 if ($inverted) {
404 $parent_id = $rel->subject_term->identifier;
405 $child_id = $rel->object_term->identifier;
407 else {
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");
418 else {
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.
428 Example :
429 Returns : Its argument.
430 Args : A RelationshipI object.
432 =cut
434 sub add_relationship{
435 my ($self, $rel) = @_;
437 $self->_add_relationship_simple($self->_relationship_store,
438 $rel, 0);
439 $self->_add_relationship_simple($self->_inverted_relationship_store,
440 $rel, 1);
441 $self->_relationship_type_store->{
442 $self->_unique_termid($rel->predicate_term)} = $rel->predicate_term;
444 return $rel;
447 =head2 get_relationships
449 Title : get_relationships
450 Usage : get_relationships(): RelationshipI
451 Function: Retrieves all relationship objects.
452 Example :
453 Returns : Array of RelationshipI objects
454 Args :
456 =cut
458 sub get_relationships{
459 my $self = shift;
460 my $term = shift;
461 my @rels;
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
469 (keys %{$store});
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) {
481 push(@rels,
482 $relfact->create_object(-object_term => $parent_term,
483 -subject_term => $term,
484 -predicate_term =>
485 $parent_entry->{$term->identifier},
486 -ontology => $term->ontology())
490 } else {
491 # otherwise, i.e., no term supplied, or the parent equals the
492 # supplied term
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) {
500 push(@rels,
501 $relfact->create_object(-object_term => $parent_term,
502 -subject_term => $subj_term,
503 -predicate_term => $rel_info,
504 -ontology =>$parent_term->ontology
512 return @rels;
515 =head2 get_all_relationships
517 Title : get_all_relationships
518 Usage : get_all_relationships(): RelationshipI
519 Function: Retrieves all relationship objects.
520 Example :
521 Returns : Array of RelationshipI objects
522 Args :
524 =cut
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
535 Example :
536 Returns : reference to an array of Bio::Ontology::RelationshipType objects
537 Args :
539 =cut
541 sub get_predicate_terms{
542 my ($self) = @_;
544 return values %{$self->_relationship_type_store};
547 =head2 _is_rel_type
549 Title : _is_rel_type
550 Usage :
551 Function:
552 Example :
553 Returns :
554 Args :
556 =cut
558 sub _is_rel_type{
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;
564 } else {
565 return 1 if $rel_type->name eq $term->name;
569 return 0;
572 =head2 _typed_traversal
574 Title : _typed_traversal
575 Usage :
576 Function:
577 Example :
578 Returns :
579 Args :
581 =cut
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;
589 my @ans;
591 if (@rel_types > 0) {
592 @ans = ();
594 foreach my $child_id (@children) {
595 push @ans, $child_id
596 if $self->_is_rel_type( $rel_store->{$term_id}->{$child_id},
597 @rel_types);
600 else {
601 @ans = @children;
603 if ($level < 1) {
604 my @ans1 = ();
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};
611 push @ans, @ans1;
614 return @ans;
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
626 direct descendants.
627 Example :
628 Returns : Array of TermI objects.
629 Args : First argument is the term of interest, second is the list of
630 relationship type terms.
632 =cut
634 sub get_child_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,
644 $term->identifier,
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.
658 Example :
659 Returns : Array of TermI objects.
660 Args : First argument is the term of interest, second is the list of
661 relationship type terms.
663 =cut
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,
676 $term->identifier,
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
689 direct ancestors.
691 Example :
692 Returns : Array of TermI objects.
693 Args : First argument is the term of interest, second is the list of relationship type terms.
695 =cut
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,
705 $term->identifier,
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
717 all ancestors.
719 Example :
720 Returns : Array of TermI objects.
721 Args : First argument is the term of interest, second is the list
722 of relationship type terms.
724 =cut
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,
735 $term->identifier,
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.
746 Args :
748 =cut
750 sub get_leaf_terms{
751 my ($self) = @_;
752 my @leaf_terms;
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};
760 return @leaf_terms;
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.
770 Args :
772 =cut
774 sub get_root_terms{
775 my ($self) = @_;
776 my @root_terms;
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};
784 return @root_terms;
787 =head2 _filter_repeated
789 Title : _filter_repeated
790 Usage : @lst = $self->_filter_repeated(@old_lst);
791 Function: Removes repeated terms
792 Example :
793 Returns : List of unique TermI objects
794 Args : List of TermI objects
796 =cut
798 sub _filter_repeated{
799 my ($self, @args) = @_;
800 my %h;
802 foreach my $element (@args) {
803 $h{$element->identifier} = $element if !defined $h{$element->identifier};
806 return values %h;
809 =head2 get_all_terms
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.
816 Args :
818 =cut
820 sub get_all_terms{
821 my ($self) = @_;
823 return $self->_filter_unmarked( values %{$self->_term_store} );
826 =head2 find_terms
828 Title : find_terms
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
833 identifier.
835 Example :
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
843 =cut
845 sub find_terms{
846 my ($self,@args) = @_;
847 my @terms;
849 my ($id,$name) = $self->_rearrange([qw(IDENTIFIER NAME)],@args);
851 if(defined($id)) {
852 @terms = $self->get_term_by_identifier($id);
853 } else {
854 @terms = $self->get_all_terms();
856 if(defined($name)) {
857 @terms = grep { $_->name() eq $name; } @terms;
859 return @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.
870 Example :
871 Returns : value of relationship_factory (a Bio::Factory::ObjectFactoryI
872 compliant object)
873 Args : on set, a Bio::Factory::ObjectFactoryI compliant object
875 =cut
877 sub relationship_factory{
878 my $self = shift;
880 return $self->{'relationship_factory'} = shift if @_;
881 return $self->{'relationship_factory'};
884 =head2 term_factory
886 Title : term_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.
895 Example :
896 Returns : value of term_factory (a Bio::Factory::ObjectFactoryI
897 compliant object)
898 Args : on set, a Bio::Factory::ObjectFactoryI compliant object
900 =cut
902 sub term_factory{
903 my $self = shift;
905 if(@_) {
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
918 Example :
919 Returns : array of fully instantiated TermI objects
920 Args : array of TermI objects
922 =cut
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.
949 =cut
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};
957 return $term;
959 else {
960 $self->warn("Term with id '$id' is not in the term store");
961 return;
965 =head2 to_string
967 Title : to_string
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();
972 Returns :
973 Args :
975 =cut
977 sub to_string{
978 my ($self) = @_;
979 my $s = "";
981 $s .= "-- # Terms:\n";
982 $s .= scalar($self->get_all_terms)."\n";
983 $s .= "-- # Relationships:\n";
984 $s .= $self->_get_number_rels."\n";
986 return $s;
989 =head2 _unique_termid
991 Title : _unique_termid
992 Usage :
993 Function: Returns a string that can be used as ID using fail-over
994 approaches.
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.
1003 Example :
1004 Returns : a string
1005 Args : a Bio::Ontology::TermI compliant object
1007 =cut
1009 sub _unique_termid{
1010 my $self = shift;
1011 my $term = shift;
1013 return $term->identifier() if $term->identifier();
1014 my $id = $term->ontology->name() if $term->ontology();
1015 if($id) {
1016 $id .= '|';
1017 } else {
1018 $id = '';
1020 $id .= $term->name();
1024 #################################################################
1025 # aliases
1026 #################################################################
1028 *get_relationship_types = \&get_predicate_terms;