Fix bug which prevents the parsing of the InterPro XML file
[bioperl-live.git] / Bio / Ontology / SimpleOntologyEngine.pm
blobc2008e56493eb42157378aa6bf5afdb9d0bd4f76
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 I<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;
402 my $subject = $rel->subject_term;
403 my $object = $rel->object_term;
406 if ($inverted) {
407 $parent_id = $subject ? $subject->identifier : '';
408 $child_id = $object ? $object->identifier : '';
410 else {
411 $parent_id = $object ? $object->identifier : '';
412 $child_id = $subject ? $subject->identifier : '';
414 if(defined $store->{$parent_id} && (defined $store->{$parent_id}->{$child_id}) &&
415 ($store->{$parent_id}->{$child_id}->name != $rel->predicate_term->name)){
416 $self->throw("relationship ".Dumper($rel->predicate_term).
417 " between ".$parent_id." and ".$child_id.
418 " already defined as ".
419 Dumper($store->{$parent_id}->{$child_id})."\n");
421 else {
422 $store->{$parent_id}->{$child_id} = $rel->predicate_term;
426 =head2 add_relationship
428 Title : add_relationship
429 Usage : add_relationship(RelationshipI relationship): RelationshipI
430 Function: Adds a relationship object to the ontology engine.
431 Example :
432 Returns : Its argument.
433 Args : A RelationshipI object.
435 =cut
437 sub add_relationship{
438 my ($self, $rel) = @_;
440 $self->_add_relationship_simple($self->_relationship_store,
441 $rel, 0);
442 $self->_add_relationship_simple($self->_inverted_relationship_store,
443 $rel, 1);
444 $self->_relationship_type_store->{
445 $self->_unique_termid($rel->predicate_term)} = $rel->predicate_term;
447 return $rel;
450 =head2 get_relationships
452 Title : get_relationships
453 Usage : get_relationships(): RelationshipI
454 Function: Retrieves all relationship objects.
455 Example :
456 Returns : Array of RelationshipI objects
457 Args :
459 =cut
461 sub get_relationships{
462 my $self = shift;
463 my $term = shift;
464 my @rels;
465 my $store = $self->_relationship_store;
466 my $relfact = $self->relationship_factory();
468 my @parent_ids = $term ?
469 # if a term is supplied then only get the term's parents
470 (map { $_->identifier(); } $self->get_parent_terms($term)) :
471 # otherwise use all parent ids
472 (keys %{$store});
473 # add the term as a parent too if one is supplied
474 push(@parent_ids,$term->identifier) if $term;
476 foreach my $parent_id (@parent_ids) {
477 my $parent_entry = $store->{$parent_id};
479 # if a term is supplied, add a relationship for the parent to the term
480 # except if the parent is the term itself (we added that one before)
481 if($term && ($parent_id ne $term->identifier())) {
482 my @parent_terms = $self->get_term_by_identifier($parent_id);
483 foreach my $parent_term (@parent_terms) {
484 push(@rels,
485 $relfact->create_object(-object_term => $parent_term,
486 -subject_term => $term,
487 -predicate_term =>
488 $parent_entry->{$term->identifier},
489 -ontology => $term->ontology())
493 } else {
494 # otherwise, i.e., no term supplied, or the parent equals the
495 # supplied term
496 my @parent_terms = $term ?
497 ($term) : $self->get_term_by_identifier($parent_id);
498 foreach my $child_id (keys %$parent_entry) {
499 my $rel_info = $parent_entry->{$child_id};
500 my ($subj_term) = $self->get_term_by_identifier($child_id);
502 foreach my $parent_term (@parent_terms) {
503 push(@rels,
504 $relfact->create_object(-object_term => $parent_term,
505 -subject_term => $subj_term,
506 -predicate_term => $rel_info,
507 -ontology =>$parent_term->ontology
515 return @rels;
518 =head2 get_all_relationships
520 Title : get_all_relationships
521 Usage : get_all_relationships(): RelationshipI
522 Function: Retrieves all relationship objects.
523 Example :
524 Returns : Array of RelationshipI objects
525 Args :
527 =cut
529 sub get_all_relationships{
530 return shift->get_relationships();
533 =head2 get_predicate_terms
535 Title : get_predicate_terms
536 Usage : get_predicate_terms(): TermI
537 Function: Retrives all relationship types stored in the engine
538 Example :
539 Returns : reference to an array of Bio::Ontology::RelationshipType objects
540 Args :
542 =cut
544 sub get_predicate_terms{
545 my ($self) = @_;
547 return values %{$self->_relationship_type_store};
550 =head2 _is_rel_type
552 Title : _is_rel_type
553 Usage :
554 Function:
555 Example :
556 Returns :
557 Args :
559 =cut
561 sub _is_rel_type{
562 my ($self, $term, @rel_types) = @_;
564 foreach my $rel_type (@rel_types) {
565 if($rel_type->identifier || $term->identifier) {
566 return 1 if $rel_type->identifier eq $term->identifier;
567 } else {
568 return 1 if $rel_type->name eq $term->name;
572 return 0;
575 =head2 _typed_traversal
577 Title : _typed_traversal
578 Usage :
579 Function:
580 Example :
581 Returns :
582 Args :
584 =cut
586 sub _typed_traversal{
587 my ($self, $rel_store, $level, $term_id, @rel_types) = @_;
588 return if !defined($rel_store->{$term_id});
589 my %parent_entry = %{$rel_store->{$term_id}};
590 my @children = keys %parent_entry;
592 my @ans;
594 if (@rel_types > 0) {
595 @ans = ();
597 foreach my $child_id (@children) {
598 push @ans, $child_id
599 if $self->_is_rel_type( $rel_store->{$term_id}->{$child_id},
600 @rel_types);
603 else {
604 @ans = @children;
606 if ($level < 1) {
607 my @ans1 = ();
609 foreach my $child_id (@ans) {
610 push @ans1, $self->_typed_traversal($rel_store,
611 $level - 1, $child_id, @rel_types)
612 if defined $rel_store->{$child_id};
614 push @ans, @ans1;
617 return @ans;
620 =head2 get_child_terms
622 Title : get_child_terms
623 Usage : get_child_terms(TermI term, TermI predicate_terms): TermI
624 get_child_terms(TermI term, RelationshipType predicate_terms): TermI
625 Function: Retrieves all child terms of a given term, that satisfy a
626 relationship among those that are specified in the second
627 argument or undef otherwise. get_child_terms is a special
628 case of get_descendant_terms, limiting the search to the
629 direct descendants.
630 Example :
631 Returns : Array of TermI objects.
632 Args : First argument is the term of interest, second is the list of
633 relationship type terms.
635 =cut
637 sub get_child_terms{
638 my ($self, $term, @relationship_types) = @_;
640 $self->throw("must provide TermI compliant object")
641 unless defined($term) && $term->isa("Bio::Ontology::TermI");
643 return $self->_filter_unmarked(
644 $self->get_term_by_identifier(
645 $self->_typed_traversal($self->_relationship_store,
647 $term->identifier,
648 @relationship_types) ) );
651 =head2 get_descendant_terms
653 Title : get_descendant_terms
654 Usage : get_descendant_terms(TermI term, TermI rel_types): TermI
655 get_child_terms(TermI term, RelationshipType predicate_terms): TermI
656 Function: Retrieves all descendant terms of a given term, that
657 satisfy a relationship among those that are specified in
658 the second argument or undef otherwise. Uses
659 _typed_traversal to find all descendants.
661 Example :
662 Returns : Array of TermI objects.
663 Args : First argument is the term of interest, second is the list of
664 relationship type terms.
666 =cut
668 sub get_descendant_terms{
669 my ($self, $term, @relationship_types) = @_;
671 $self->throw("must provide TermI compliant object")
672 unless defined($term) && $term->isa("Bio::Ontology::TermI");
674 return $self->_filter_unmarked(
675 $self->_filter_repeated(
676 $self->get_term_by_identifier(
677 $self->_typed_traversal($self->_relationship_store,
679 $term->identifier,
680 @relationship_types) ) ) );
683 =head2 get_parent_terms
685 Title : get_parent_terms
686 Usage : get_parent_terms(TermI term, TermI predicate_terms): TermI
687 get_child_terms(TermI term, RelationshipType predicate_terms): TermI
688 Function: Retrieves all parent terms of a given term, that satisfy a
689 relationship among those that are specified in the second
690 argument or undef otherwise. get_parent_terms is a special
691 case of get_ancestor_terms, limiting the search to the
692 direct ancestors.
694 Example :
695 Returns : Array of TermI objects.
696 Args : First argument is the term of interest, second is the list of relationship type terms.
698 =cut
700 sub get_parent_terms{
701 my ($self, $term, @relationship_types) = @_;
702 $self->throw("term must be a valid object, not undef") unless defined $term;
704 return $self->_filter_unmarked(
705 $self->get_term_by_identifier(
706 $self->_typed_traversal($self->_inverted_relationship_store,
708 $term->identifier,
709 @relationship_types) ) );
712 =head2 get_ancestor_terms
714 Title : get_ancestor_terms
715 Usage : get_ancestor_terms(TermI term, TermI predicate_terms): TermI
716 get_child_terms(TermI term, RelationshipType predicate_terms): TermI
717 Function: Retrieves all ancestor terms of a given term, that satisfy
718 a relationship among those that are specified in the second
719 argument or undef otherwise. Uses _typed_traversal to find
720 all ancestors.
722 Example :
723 Returns : Array of TermI objects.
724 Args : First argument is the term of interest, second is the list
725 of relationship type terms.
727 =cut
729 sub get_ancestor_terms{
730 my ($self, $term, @relationship_types) = @_;
731 $self->throw("term must be a valid object, not undef") unless defined $term;
733 return $self->_filter_unmarked(
734 $self->_filter_repeated(
735 $self->get_term_by_identifier(
736 $self->_typed_traversal($self->_inverted_relationship_store,
738 $term->identifier,
739 @relationship_types) ) ) );
742 =head2 get_leaf_terms
744 Title : get_leaf_terms
745 Usage : get_leaf_terms(): TermI
746 Function: Retrieves all leaf terms from the ontology. Leaf term is a term w/o descendants.
747 Example : @leaf_terms = $obj->get_leaf_terms()
748 Returns : Array of TermI objects.
749 Args :
751 =cut
753 sub get_leaf_terms{
754 my ($self) = @_;
755 my @leaf_terms;
757 foreach my $term (values %{$self->_term_store}) {
758 push @leaf_terms, $term
759 if !defined $self->_relationship_store->{$term->identifier} &&
760 defined $self->_instantiated_terms_store->{$term->identifier};
763 return @leaf_terms;
766 =head2 get_root_terms
768 Title : get_root_terms
769 Usage : get_root_terms(): TermI
770 Function: Retrieves all root terms from the ontology. Root term is a term w/o descendants.
771 Example : @root_terms = $obj->get_root_terms()
772 Returns : Array of TermI objects.
773 Args :
775 =cut
777 sub get_root_terms{
778 my ($self) = @_;
779 my @root_terms;
781 foreach my $term (values %{$self->_term_store}) {
782 push @root_terms, $term
783 if !defined $self->_inverted_relationship_store->{$term->identifier} &&
784 defined $self->_instantiated_terms_store->{$term->identifier};
787 return @root_terms;
790 =head2 _filter_repeated
792 Title : _filter_repeated
793 Usage : @lst = $self->_filter_repeated(@old_lst);
794 Function: Removes repeated terms
795 Example :
796 Returns : List of unique TermI objects
797 Args : List of TermI objects
799 =cut
801 sub _filter_repeated{
802 my ($self, @args) = @_;
803 my %h;
805 foreach my $element (@args) {
806 $h{$element->identifier} = $element if !defined $h{$element->identifier};
809 return values %h;
812 =head2 get_all_terms
814 Title : get_all_terms
815 Usage : get_all_terms(): TermI
816 Function: Retrieves all terms currently stored in the ontology.
817 Example : @all_terms = $obj->get_all_terms()
818 Returns : Array of TermI objects.
819 Args :
821 =cut
823 sub get_all_terms{
824 my ($self) = @_;
826 return $self->_filter_unmarked( values %{$self->_term_store} );
829 =head2 find_terms
831 Title : find_terms
832 Usage : ($term) = $oe->find_terms(-identifier => "SO:0000263");
833 Function: Find term instances matching queries for their attributes.
835 This implementation can efficiently resolve queries by
836 identifier.
838 Example :
839 Returns : an array of zero or more Bio::Ontology::TermI objects
840 Args : Named parameters. The following parameters should be recognized
841 by any implementations:
843 -identifier query by the given identifier
844 -name query by the given name
846 =cut
848 sub find_terms{
849 my ($self,@args) = @_;
850 my @terms;
852 my ($id,$name) = $self->_rearrange([qw(IDENTIFIER NAME)],@args);
854 if(defined($id)) {
855 @terms = $self->get_term_by_identifier($id);
856 } else {
857 @terms = $self->get_all_terms();
859 if(defined($name)) {
860 @terms = grep { $_->name() eq $name; } @terms;
862 return @terms;
866 =head2 relationship_factory
868 Title : relationship_factory
869 Usage : $fact = $obj->relationship_factory()
870 Function: Get/set the object factory to be used when relationship
871 objects are created by the implementation on-the-fly.
873 Example :
874 Returns : value of relationship_factory (a Bio::Factory::ObjectFactoryI
875 compliant object)
876 Args : on set, a Bio::Factory::ObjectFactoryI compliant object
878 =cut
880 sub relationship_factory{
881 my $self = shift;
883 return $self->{'relationship_factory'} = shift if @_;
884 return $self->{'relationship_factory'};
887 =head2 term_factory
889 Title : term_factory
890 Usage : $fact = $obj->term_factory()
891 Function: Get/set the object factory to be used when term objects are
892 created by the implementation on-the-fly.
894 Note that this ontology engine implementation does not
895 create term objects on the fly, and therefore setting this
896 attribute is meaningless.
898 Example :
899 Returns : value of term_factory (a Bio::Factory::ObjectFactoryI
900 compliant object)
901 Args : on set, a Bio::Factory::ObjectFactoryI compliant object
903 =cut
905 sub term_factory{
906 my $self = shift;
908 if(@_) {
909 $self->warn("setting term factory, but ".ref($self).
910 " does not create terms on-the-fly");
911 return $self->{'term_factory'} = shift;
913 return $self->{'term_factory'};
916 =head2 _filter_unmarked
918 Title : _filter_unmarked
919 Usage : _filter_unmarked(TermI terms): TermI
920 Function: Removes the uninstantiated terms from the list of terms
921 Example :
922 Returns : array of fully instantiated TermI objects
923 Args : array of TermI objects
925 =cut
927 sub _filter_unmarked{
928 my ($self, @terms) = @_;
929 my @filtered_terms = ();
931 if ( scalar(@terms) >= 1) {
932 foreach my $term (@terms) {
933 push @filtered_terms, $term
934 if defined $self->_instantiated_terms_store->{$term->identifier};
938 return @filtered_terms;
941 =head2 remove_term_by_id
943 Title : remove_term_by_id
944 Usage : remove_term_by_id(String id): TermI
945 Function: Removes TermI object from the ontology engine using the
946 string id as an identifier. Current implementation does not
947 enforce consistency of the relationships using that term.
948 Example : $term = $soe->remove_term_by_id($id);
949 Returns : Object of class TermI or undef if not found.
950 Args : The string identifier of a term.
952 =cut
954 sub remove_term_by_id{
955 my ($self, $id) = @_;
957 if ( $self->get_term_by_identifier($id) ) {
958 my $term = $self->{_term_store}->{$id};
959 delete $self->{_term_store}->{$id};
960 return $term;
962 else {
963 $self->warn("Term with id '$id' is not in the term store");
964 return;
968 =head2 to_string
970 Title : to_string
971 Usage : print $sv->to_string();
972 Function: Currently returns formatted string containing the number of
973 terms and number of relationships from the ontology engine.
974 Example : print $sv->to_string();
975 Returns :
976 Args :
978 =cut
980 sub to_string{
981 my ($self) = @_;
982 my $s = "";
984 $s .= "-- # Terms:\n";
985 $s .= scalar($self->get_all_terms)."\n";
986 $s .= "-- # Relationships:\n";
987 $s .= $self->_get_number_rels."\n";
989 return $s;
992 =head2 _unique_termid
994 Title : _unique_termid
995 Usage :
996 Function: Returns a string that can be used as ID using fail-over
997 approaches.
999 If the identifier attribute is not set, it uses the
1000 combination of name and ontology name, provided both are
1001 set. If they are not, it returns the name alone.
1003 Note that this is a private method. Call from inheriting
1004 classes but not from outside.
1006 Example :
1007 Returns : a string
1008 Args : a Bio::Ontology::TermI compliant object
1010 =cut
1012 sub _unique_termid{
1013 my $self = shift;
1014 my $term = shift;
1016 return $term->identifier() if $term->identifier();
1017 my $id = $term->ontology->name() if $term->ontology();
1018 if($id) {
1019 $id .= '|';
1020 } else {
1021 $id = '';
1023 $id .= $term->name();
1027 #################################################################
1028 # aliases
1029 #################################################################
1031 *get_relationship_types = \&get_predicate_terms;