Bio/Ontology/SimpleOntologyEngine.pm

filter: Statementstmt 26.1% 59 / 226 Branchbran 13.8% 13 / 94 Conditioncond 3.7% 1 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 33.3% 13 / 39 Podpod 100% 21 / 21 totaltotal 25.2% 107 / 424 CC 72 SCAR 33.3
CC
72
Cov
21%
get_relationships
110.0
_typed_traversal
72.0
_add_relationship_simple
56.0
CRAP 27.9
« Bio/DB/InMemoryCache.pm ↑ Summary Bio/Search/Hit/PullHitI.pm »
1 #
2 # BioPerl module for Bio::Ontology::SimpleOntologyEngine
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Peter Dimitrov <dimitrov@gnf.org>
7 #
8 # Copyright Peter Dimitrov
9 # (c) Peter Dimitrov, dimitrov@gnf.org, 2002.
10 # (c) GNF, Genomics Institute of the Novartis Research Foundation, 2002.
11 #
12 # You may distribute this module under the same terms as perl itself.
13 # Refer to the Perl Artistic License (see the license accompanying this
14 # software package, or see http://www.perl.com/language/misc/Artistic.html)
15 # for the terms under which you may use, modify, and redistribute this module.
16 #
17 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
18 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 #
21 # POD documentation - main docs before the code
22  
23 =head1 NAME
24  
25 Bio::Ontology::SimpleOntologyEngine - Implementation of OntologyEngineI interface
26  
27 =head1 SYNOPSIS
28  
29 my $soe = Bio::Ontology::SimpleOntologyEngine->new;
30  
31 =head1 DESCRIPTION
32  
33 This is a "simple" implementation of Bio::Ontology::OntologyEngineI.
34  
35 =head1 FEEDBACK
36  
37 =head2 Mailing Lists
38  
39 User feedback is an integral part of the evolution of this and other
40 Bioperl modules. Send your comments and suggestions preferably to
41 the Bioperl mailing list. Your participation is much appreciated.
42  
43 bioperl-l@bioperl.org - General discussion
44 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
45  
46 =head2 Support
47  
48 Please direct usage questions or support issues to the mailing list:
49  
50 I<bioperl-l@bioperl.org>
51  
52 rather than to the module maintainer directly. Many experienced and
53 reponsive experts will be able look at the problem and quickly
54 address it. Please include a thorough description of the problem
55 with code and data examples if at all possible.
56  
57 =head2 Reporting Bugs
58  
59 Report bugs to the Bioperl bug tracking system to help us keep track
60 of the bugs and their resolution. Bug reports can be submitted via
61 the web:
62  
63 https://github.com/bioperl/bioperl-live/issues
64  
65 =head1 AUTHOR - Peter Dimitrov
66  
67 Email dimitrov@gnf.org
68  
69 =head1 CONTRIBUTORS
70  
71 Hilmar Lapp, hlapp at gmx.net
72  
73 =head1 APPENDIX
74  
75 The rest of the documentation details each of the object methods.
76 Internal methods are usually preceded with a _
77  
78 =cut
79  
80 # Let the code begin...
81  
82 package Bio::Ontology::SimpleOntologyEngine;
83 3 $Bio::Ontology::SimpleOntologyEngine::VERSION = '1.7.8';
84 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 3 ▶use Bio::Ontology::RelationshipFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 3 ▶use base qw(Bio::Root::Root Bio::Ontology::OntologyEngineI);
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 =head2 new
92  
93 Title : new
94 Usage : $soe = Bio::Ontology::SimpleOntologyEngine->new;
95 Function: Initializes the ontology engine.
96 Example : $soe = Bio::Ontology::SimpleOntologyEngine->new;
97 Returns : Object of class SimpleOntologyEngine.
98 Args :
99  
100 =cut
101  
102 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
103 1 my ( $class, @args ) = @_;
104 1 my $self = $class->SUPER::new(@args);
105  
106 # my %param = @args;
107  
108 1 $self->_term_store( {} );
109 1 $self->_relationship_store( {} );
110 1 $self->_inverted_relationship_store( {} );
111 1 $self->_relationship_type_store( {} );
112 1 $self->_instantiated_terms_store( {} );
113  
114 # set defaults for the factories
115 1 $self->relationship_factory(
116 Bio::Ontology::RelationshipFactory->new( -type => "Bio::Ontology::Relationship" ) );
117 1 return $self;
118 }
119  
120 =head2 _instantiated_terms_store
121  
122 Title : _instantiated_terms_store
123 Usage : $obj->_instantiated_terms_store($newval)
124 Function:
125 Example :
126 Returns : hash
127 Args : empty hash
128  
129 =cut
130  
131 5 ▶sub _instantiated_terms_store {
Subroutine _instantiated_terms_store: called CC=2 SCAR=6.9
132 5 my ( $self, $value ) = @_;
133  
134 5 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 1 4 5
135 1 $self->{'_instantiated_terms_store'} = $value;
136 }
137 5 return $self->{'_instantiated_terms_store'};
138 }
139  
140 =head2 mark_instantiated
141  
142 Title : mark_instantiated
143 Usage : $self->mark_instantiated(TermI terms): TermI
144 Function: Marks TermI objects as fully instantiated,
145 allowing for proper counting of the number of terms in the term store.
146 The TermI objects has to be already stored in the term store in order
147 to be marked.
148 Example : $self->mark_instantiated($term);
149 Returns : its argument or throws an exception if a term is not
150 in the term store.
151 Args : array of objects of class TermI.
152  
153 =cut
154  
155 0 ▶sub mark_instantiated {
Subroutine mark_instantiated: not called CC=3 SCAR=24.8
Pod documented
156 0 my ( $self, @terms ) = @_;
157  
158 0 foreach my $term (@terms) {
159 $self->throw( "term " . $term->identifier . " not in the term store\n" )
160 0 ▶ if !defined $self->_term_store->{ $term->identifier };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_term_store->{$term->identifier} 0 0 0
161 0 $self->_instantiated_terms_store->{ $term->identifier } = 1;
162 }
163  
164 0 return @terms;
165 }
166  
167 =head2 mark_uninstantiated
168  
169 Title : mark_uninstantiated
170 Usage : $self->mark_uninstantiated(TermI terms): TermI
171 Function: Marks TermI objects as not fully instantiated,
172 Example : $self->mark_uninstantiated($term);
173 Returns : its argument or throws an exception if a term is not
174 in the term store(if the term is not marked it does nothing).
175 Args : array of objects of class TermI.
176  
177 =cut
178  
179 0 ▶sub mark_uninstantiated {
Subroutine mark_uninstantiated: not called CC=4 SCAR=30.0
Pod documented
180 0 my ( $self, @terms ) = @_;
181  
182 0 foreach my $term (@terms) {
183 $self->throw( "term " . $term->identifier . " not in the term store\n" )
184 0 ▶ if !defined $self->_term_store->{ $term->identifier };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_term_store->{$term->identifier} 0 0 0
185 delete $self->_instantiated_terms_store->{ $term->identifier }
186 0 ▶ if defined $self->_instantiated_terms_store->{ $term->identifier };
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_instantiated_terms_store->{$term->identifier} 0 0 0
187 }
188  
189 0 return @terms;
190 }
191  
192 =head2 _term_store
193  
194 Title : term_store
195 Usage : $obj->_term_store($newval)
196 Function:
197 Example :
198 Returns : reference to an array of Bio::Ontology::TermI objects
199 Args : reference to an array of Bio::Ontology::TermI objects
200  
201 =cut
202  
203 5 ▶sub _term_store {
Subroutine _term_store: called CC=3 SCAR=11.2
204 5 my ( $self, $value ) = @_;
205  
206 5 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 1 4 5
207 1 ▶ if ( defined $self->{'_term_store'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_term_store'}) { } 0 1 1
208 0 $self->throw("_term_store already defined\n");
209 } else {
210 1 $self->{'_term_store'} = $value;
211 }
212 }
213  
214 5 return $self->{'_term_store'};
215 }
216  
217 =head2 add_term
218  
219 Title : add_term
220 Usage : add_term(TermI term): TermI
221 Function: Adds TermI object to the ontology engine term store.
222 Marks the term fully instantiated by default.
223 Example : $soe->add_term($term)
224 Returns : its argument.
225 Args : object of class TermI.
226  
227 =cut
228  
229 4 ▶sub add_term {
Subroutine add_term: called CC=3 SCAR=12.0
Pod documented
230 4 my ( $self, $term ) = @_;
231 4 my $term_store = $self->_term_store;
232  
233 4 ▶ if ( defined $term_store->{ $term->identifier } && $self->_instantiated_terms_store->{ $term->identifier }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$term_store{$term->identifier} and $self->_instantiated_terms_store->{$term->identifier}) { } 0 4 4
Condition33%1/3
defined $$term_store{$term->identifier} and $self->_instantiated_terms_store->{$term->identifier}
!ll&&!rl&&r
400
MC/DC0%0/2
defined $$term_store{$term->identifier} and $self->_instantiated_terms_store->{$term->identifier}
defined $$term_store{$term->identifier} $self->_instantiated_terms_store->{$term->identifier}
Truth table33%1/3
A and B
A= defined $$term_store{$term->identifier}B= $self->_instantiated_terms_store->{$term->identifier}
ABresult
0X0
100
111
234 0 $self->throw( "term " . $term->identifier . " already defined\n" );
235 } else {
236 4 $term_store->{ $term->identifier } = $term;
237 4 $self->_instantiated_terms_store->{ $term->identifier } = 1;
238 }
239  
240 4 return $term;
241 }
242  
243 =head2 get_term_by_identifier
244  
245 Title : get_term_by_identifier
246 Usage : get_term_by_identifier(String id): TermI
247 Function: Retrieves terms from the term store by their identifier
248 field, or an empty list if not there.
249 Example : $term = $soe->get_term_by_identifier("IPR000001");
250 Returns : An array of zero or more Bio::Ontology::TermI objects.
251 Args : An array of identifier strings
252  
253 =cut
254  
255 0 ▶sub get_term_by_identifier {
Subroutine get_term_by_identifier: not called CC=3 SCAR=24.8
Pod documented
256 0 my ( $self, @ids ) = @_;
257 0 my @ans = ();
258  
259 0 foreach my $id (@ids) {
260 0 my $term = $self->_term_store->{$id};
261 0 ▶ push @ans, $term if defined $term;
Branch: 1 branch
BranchTrueFalseTotal
if defined $term 0 0 0
262 }
263  
264 0 return @ans;
265 }
266  
267 =head2 _get_number_rels
268  
269 Title : get_number_rels
270 Usage :
271 Function:
272 Example :
273 Returns :
274 Args :
275  
276 =cut
277  
278 0 ▶sub _get_number_rels {
Subroutine _get_number_rels: not called CC=2 SCAR=17.9
279 0 my ($self) = @_;
280 0 my $num_rels = 0;
281  
282 0 foreach my $entry ( $self->_relationship_store ) {
283 0 $num_rels += scalar keys %$entry;
284 }
285 0 return $num_rels;
286 }
287  
288 =head2 _get_number_terms
289  
290 Title : _get_number_terms
291 Usage :
292 Function:
293 Example :
294 Returns :
295 Args :
296  
297 =cut
298  
299 0 ▶sub _get_number_terms {
Subroutine _get_number_terms: not called CC=1 SCAR=6.9
300 0 my ($self) = @_;
301  
302 0 return scalar $self->_filter_unmarked( values %{ $self->_term_store } );
303  
304 }
305  
306 =head2 _relationship_store
307  
308 Title : _storerelationship_store
309 Usage : $obj->relationship_store($newval)
310 Function:
311 Example :
312 Returns : reference to an array of Bio::Ontology::TermI objects
313 Args : reference to an array of Bio::Ontology::TermI objects
314  
315 =cut
316  
317 1 ▶sub _relationship_store {
Subroutine _relationship_store: called CC=3 SCAR=11.8
318 1 my ( $self, $value ) = @_;
319  
320 1 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 1 0 1
321 1 ▶ if ( defined $self->{'_relationship_store'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_relationship_store'}) { } 0 1 1
322 0 $self->throw("_relationship_store already defined\n");
323 } else {
324 1 $self->{'_relationship_store'} = $value;
325 }
326 }
327  
328 1 return $self->{'_relationship_store'};
329 }
330  
331 =head2 _inverted_relationship_store
332  
333 Title : _inverted_relationship_store
334 Usage :
335 Function:
336 Example :
337 Returns : reference to an array of Bio::Ontology::TermI objects
338 Args : reference to an array of Bio::Ontology::TermI objects
339  
340 =cut
341  
342 1 ▶sub _inverted_relationship_store {
Subroutine _inverted_relationship_store: called CC=3 SCAR=11.8
343 1 my ( $self, $value ) = @_;
344  
345 1 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 1 0 1
346 1 ▶ if ( defined $self->{'_inverted_relationship_store'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_inverted_relationship_store'}) { } 0 1 1
347 0 $self->throw("_inverted_relationship_store already defined\n");
348 } else {
349 1 $self->{'_inverted_relationship_store'} = $value;
350 }
351 }
352  
353 1 return $self->{'_inverted_relationship_store'};
354 }
355  
356 =head2 _relationship_type_store
357  
358 Title : _relationship_type_store
359 Usage : $obj->_relationship_type_store($newval)
360 Function:
361 Example :
362 Returns : reference to an array of Bio::Ontology::RelationshipType objects
363 Args : reference to an array of Bio::Ontology::RelationshipType objects
364  
365 =cut
366  
367 1 ▶sub _relationship_type_store {
Subroutine _relationship_type_store: called CC=3 SCAR=11.8
368 1 my ( $self, $value ) = @_;
369  
370 1 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 1 0 1
371 1 ▶ if ( defined $self->{'_relationship_type_store'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_relationship_type_store'}) { } 0 1 1
372 0 $self->throw("_relationship_type_store already defined\n");
373 } else {
374 1 $self->{'_relationship_type_store'} = $value;
375 }
376 }
377  
378 1 return $self->{'_relationship_type_store'};
379 }
380  
381 =head2 _add_relationship_simple
382  
383 Title : _add_relationship_simple
384 Usage :
385 Function:
386 Example :
387 Returns :
388 Args :
389  
390 =cut
391  
392 0 ▶sub _add_relationship_simple {
Subroutine _add_relationship_simple: not called CC=7 SCAR=40.3
393 0 my ( $self, $store, $rel, $inverted ) = @_;
394  
395 0 ▶ my $subject = $rel->subject_term
Branch: 1 branch
BranchTrueFalseTotal
my $subject = $rel->subject_term or $self->throw("cannot add relationship, relationship has no subject_term") 0 0 0
396 or $self->throw('cannot add relationship, relationship has no subject_term');
397 0 ▶ my $object = $rel->object_term
Branch: 1 branch
BranchTrueFalseTotal
my $object = $rel->object_term or $self->throw("cannot add relationship, relationship has no object_term") 0 0 0
398 or $self->throw('cannot add relationship, relationship has no object_term');
399  
400 0 my ( $parent_id, $child_id ) = ( $object->identifier, $subject->identifier );
401 0 ▶ ( $parent_id, $child_id ) = ( $child_id, $parent_id ) if $inverted;
Branch: 1 branch
BranchTrueFalseTotal
if $inverted 0 0 0
402  
403 0 ▶ if ( defined $store->{$parent_id}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $store->{$parent_id} and defined $store->{$parent_id}{$child_id} and $store->{$parent_id}{$child_id}->name ne $rel->predicate_term->name) 0 0 0
Condition0%0/6
defined $store->{$parent_id} and defined $store->{$parent_id}{$child_id} and $store->{$parent_id}{$child_id}->name ne $rel->predicate_term->name
!ll&&!rl&&r
000
defined $store->{$parent_id} and defined $store->{$parent_id}{$child_id}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $store->{$parent_id} and defined $store->{$parent_id}{$child_id} and $store->{$parent_id}{$child_id}->name ne $rel->predicate_term->name
defined $store->{$parent_id} defined $store->{$parent_id}{$child_id} $store->{$parent_id}{$child_id}->name ne $rel->predicate_term->name
Truth table0%0/4
A and B and C
A= defined $store->{$parent_id}B= defined $store->{$parent_id}{$child_id}C= $store->{$parent_id}{$child_id}->name ne $rel->predicate_term->name
ABCresult
0XX0
10X0
1100
1111
404 && defined $store->{$parent_id}->{$child_id}
405 && $store->{$parent_id}->{$child_id}->name ne $rel->predicate_term->name
406 ) {
407 $self->throw( "relationship "
408 . $rel->predicate_term->name
409 . " between "
410 . $parent_id . " and "
411 . $child_id
412 . " already defined as "
413 0 . $store->{$parent_id}->{$child_id}->name
414 . "\n" );
415 }
416  
417 # all is well if we get here
418 0 $store->{$parent_id}->{$child_id} = $rel->predicate_term;
419 }
420  
421 =head2 add_relationship
422  
423 Title : add_relationship
424 Usage : add_relationship(RelationshipI relationship): RelationshipI
425 Function: Adds a relationship object to the ontology engine.
426 Example :
427 Returns : Its argument.
428 Args : A RelationshipI object.
429  
430 =cut
431  
432 0 ▶sub add_relationship {
Subroutine add_relationship: not called CC=1 SCAR=6.9
Pod documented
433 0 my ( $self, $rel ) = @_;
434  
435 0 $self->_add_relationship_simple( $self->_relationship_store, $rel, 0 );
436 0 $self->_add_relationship_simple( $self->_inverted_relationship_store, $rel, 1 );
437 0 $self->_relationship_type_store->{ $self->_unique_termid( $rel->predicate_term ) } =
438 $rel->predicate_term;
439  
440 0 return $rel;
441 }
442  
443 =head2 get_relationships
444  
445 Title : get_relationships
446 Usage : get_relationships(): RelationshipI
447 Function: Retrieves all relationship objects.
448 Example :
449 Returns : Array of RelationshipI objects
450 Args :
451  
452 =cut
453  
454 0 ▶sub get_relationships {
Subroutine get_relationships: not called CC=10 SCAR=47.0
Pod documented
455 0 my $self = shift;
456 0 my $term = shift;
457 0 my @rels;
458 0 my $store = $self->_relationship_store;
459 0 my $relfact = $self->relationship_factory();
460  
461 my @parent_ids = $term
462 ?
463  
464 # if a term is supplied then only get the term's parents
465 0 ( map { $_->identifier(); } $self->get_parent_terms($term) )
466 :
467  
468 # otherwise use all parent ids
469 0 ▶ ( keys %{$store} );
Branch: 1 branch
BranchTrueFalseTotal
$term ? : 0 0 0
470  
471 # add the term as a parent too if one is supplied
472 0 ▶ push( @parent_ids, $term->identifier ) if $term;
Branch: 1 branch
BranchTrueFalseTotal
if $term 0 0 0
473  
474 0 foreach my $parent_id (@parent_ids) {
475 0 my $parent_entry = $store->{$parent_id};
476  
477 # if a term is supplied, add a relationship for the parent to the term
478 # except if the parent is the term itself (we added that one before)
479 0 ▶ if ( $term && ( $parent_id ne $term->identifier() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term and $parent_id ne $term->identifier) { } 0 0 0
Condition0%0/3
$term and $parent_id ne $term->identifier
!ll&&!rl&&r
000
MC/DC0%0/2
$term and $parent_id ne $term->identifier
$term $parent_id ne $term->identifier
Truth table0%0/3
A and B
A= $termB= $parent_id ne $term->identifier
ABresult
0X0
100
111
480 0 my @parent_terms = $self->get_term_by_identifier($parent_id);
481 0 foreach my $parent_term (@parent_terms) {
482 push(
483 @rels,
484 $relfact->create_object(
485 -object_term => $parent_term,
486 -subject_term => $term,
487 0 -predicate_term => $parent_entry->{ $term->identifier },
488 -ontology => $term->ontology()
489 )
490 );
491 }
492  
493 } else {
494  
495 # otherwise, i.e., no term supplied, or the parent equals the
496 # supplied term
497 0 ▶ my @parent_terms = $term ? ($term) : $self->get_term_by_identifier($parent_id);
Branch: 1 branch
BranchTrueFalseTotal
$term ? : 0 0 0
498 0 foreach my $child_id ( keys %$parent_entry ) {
499 0 my $rel_info = $parent_entry->{$child_id};
500 0 my ($subj_term) = $self->get_term_by_identifier($child_id);
501  
502 0 foreach my $parent_term (@parent_terms) {
503 0 push(
504 @rels,
505 $relfact->create_object(
506 -object_term => $parent_term,
507 -subject_term => $subj_term,
508 -predicate_term => $rel_info,
509 -ontology => $parent_term->ontology
510 )
511 );
512 }
513 }
514 }
515 }
516  
517 0 return @rels;
518 }
519  
520 =head2 get_all_relationships
521  
522 Title : get_all_relationships
523 Usage : get_all_relationships(): RelationshipI
524 Function: Retrieves all relationship objects.
525 Example :
526 Returns : Array of RelationshipI objects
527 Args :
528  
529 =cut
530  
531 0 ▶sub get_all_relationships {
Subroutine get_all_relationships: not called CC=1 SCAR=6.9
Pod documented
532 0 return shift->get_relationships();
533 }
534  
535 =head2 get_predicate_terms
536  
537 Title : get_predicate_terms
538 Usage : get_predicate_terms(): TermI
539 Function: Retrieves all relationship types stored in the engine
540 Example :
541 Returns : reference to an array of Bio::Ontology::RelationshipType objects
542 Args :
543  
544 =cut
545  
546 0 ▶sub get_predicate_terms {
Subroutine get_predicate_terms: not called CC=1 SCAR=6.9
Pod documented
547 0 my ($self) = @_;
548  
549 0 return values %{ $self->_relationship_type_store };
550 }
551  
552 =head2 _is_rel_type
553  
554 Title : _is_rel_type
555 Usage :
556 Function:
557 Example :
558 Returns :
559 Args :
560  
561 =cut
562  
563 0 ▶sub _is_rel_type {
Subroutine _is_rel_type: not called CC=6 SCAR=37.4
564 0 my ( $self, $term, @rel_types ) = @_;
565  
566 0 foreach my $rel_type (@rel_types) {
567 0 ▶ if ( $rel_type->identifier || $term->identifier ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_type->identifier or $term->identifier) { } 0 0 0
Condition0%0/3
$rel_type->identifier or $term->identifier
l!l&&r!l&&!r
000
MC/DC0%0/2
$rel_type->identifier or $term->identifier
$rel_type->identifier $term->identifier
Truth table0%0/3
A or B
A= $rel_type->identifierB= $term->identifier
ABresult
1X1
011
000
568 0 ▶ return 1 if $rel_type->identifier eq $term->identifier;
Branch: 1 branch
BranchTrueFalseTotal
if $rel_type->identifier eq $term->identifier 0 0 0
569 } else {
570 0 ▶ return 1 if $rel_type->name eq $term->name;
Branch: 1 branch
BranchTrueFalseTotal
if $rel_type->name eq $term->name 0 0 0
571 }
572 }
573  
574 0 return 0;
575 }
576  
577 =head2 _typed_traversal
578  
579 Title : _typed_traversal
580 Usage :
581 Function:
582 Example :
583 Returns :
584 Args :
585  
586 =cut
587  
588 0 ▶sub _typed_traversal {
Subroutine _typed_traversal: not called CC=8 SCAR=42.8
589 0 my ( $self, $rel_store, $level, $term_id, @rel_types ) = @_;
590 0 ▶ return if !defined( $rel_store->{$term_id} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rel_store->{$term_id} 0 0 0
591 0 my %parent_entry = %{ $rel_store->{$term_id} };
592 0 my @children = keys %parent_entry;
593  
594 0 my @ans;
595  
596 0 ▶ if ( @rel_types > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rel_types > 0) { } 0 0 0
597 0 @ans = ();
598  
599 0 foreach my $child_id (@children) {
600 push @ans, $child_id
601 0 ▶ if $self->_is_rel_type( $rel_store->{$term_id}->{$child_id}, @rel_types );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_rel_type($rel_store->{$term_id}{$child_id}, @rel_types) 0 0 0
602 }
603 } else {
604 0 @ans = @children;
605 }
606 0 ▶ if ( $level < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level < 1) 0 0 0
607 0 my @ans1 = ();
608  
609 0 foreach my $child_id (@ans) {
610 push @ans1, $self->_typed_traversal( $rel_store, $level - 1, $child_id, @rel_types )
611 0 ▶ if defined $rel_store->{$child_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rel_store->{$child_id} 0 0 0
612 }
613 0 push @ans, @ans1;
614 }
615  
616 0 return @ans;
617 }
618  
619 =head2 get_child_terms
620  
621 Title : get_child_terms
622 Usage : get_child_terms(TermI term, TermI predicate_terms): TermI
623 get_child_terms(TermI term, RelationshipType predicate_terms): TermI
624 Function: Retrieves all child terms of a given term, that satisfy a
625 relationship among those that are specified in the second
626 argument or undef otherwise. get_child_terms is a special
627 case of get_descendant_terms, limiting the search to the
628 direct descendants.
629 Example :
630 Returns : Array of TermI objects.
631 Args : First argument is the term of interest, second is the list of
632 relationship type terms.
633  
634 =cut
635  
636 0 ▶sub get_child_terms {
Subroutine get_child_terms: not called CC=3 SCAR=24.8
Pod documented
637 0 my ( $self, $term, @relationship_types ) = @_;
638  
639 0 ▶ $self->throw("must provide TermI compliant object")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $term and $term->isa("Bio::Ontology::TermI") 0 0 0
Condition0%0/3
defined $term and $term->isa("Bio::Ontology::TermI")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $term and $term->isa("Bio::Ontology::TermI")
defined $term $term->isa("Bio::Ontology::TermI")
Truth table0%0/3
A and B
A= defined $termB= $term->isa("Bio::Ontology::TermI")
ABresult
0X0
100
111
640 unless defined($term) && $term->isa("Bio::Ontology::TermI");
641  
642 0 return $self->_filter_unmarked(
643 $self->get_term_by_identifier(
644 $self->_typed_traversal(
645 $self->_relationship_store, 1, $term->identifier, @relationship_types
646 )
647 )
648 );
649 }
650  
651 =head2 get_descendant_terms
652  
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.
660  
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.
665  
666 =cut
667  
668 0 ▶sub get_descendant_terms {
Subroutine get_descendant_terms: not called CC=3 SCAR=24.8
Pod documented
669 0 my ( $self, $term, @relationship_types ) = @_;
670  
671 0 ▶ $self->throw("must provide TermI compliant object")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $term and $term->isa("Bio::Ontology::TermI") 0 0 0
Condition0%0/3
defined $term and $term->isa("Bio::Ontology::TermI")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $term and $term->isa("Bio::Ontology::TermI")
defined $term $term->isa("Bio::Ontology::TermI")
Truth table0%0/3
A and B
A= defined $termB= $term->isa("Bio::Ontology::TermI")
ABresult
0X0
100
111
672 unless defined($term) && $term->isa("Bio::Ontology::TermI");
673  
674 0 return $self->_filter_unmarked(
675 $self->_filter_repeated(
676 $self->get_term_by_identifier(
677 $self->_typed_traversal(
678 $self->_relationship_store, 0, $term->identifier, @relationship_types
679 )
680 )
681 )
682 );
683 }
684  
685 =head2 get_parent_terms
686  
687 Title : get_parent_terms
688 Usage : get_parent_terms(TermI term, TermI predicate_terms): TermI
689 get_child_terms(TermI term, RelationshipType predicate_terms): TermI
690 Function: Retrieves all parent terms of a given term, that satisfy a
691 relationship among those that are specified in the second
692 argument or undef otherwise. get_parent_terms is a special
693 case of get_ancestor_terms, limiting the search to the
694 direct ancestors.
695  
696 Example :
697 Returns : Array of TermI objects.
698 Args : First argument is the term of interest, second is the list of relationship type terms.
699  
700 =cut
701  
702 0 ▶sub get_parent_terms {
Subroutine get_parent_terms: not called CC=2 SCAR=17.9
Pod documented
703 0 my ( $self, $term, @relationship_types ) = @_;
704 0 ▶ $self->throw("term must be a valid object, not undef") unless defined $term;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $term 0 0 0
705  
706 0 return $self->_filter_unmarked(
707 $self->get_term_by_identifier(
708 $self->_typed_traversal(
709 $self->_inverted_relationship_store,
710 1, $term->identifier, @relationship_types
711 )
712 )
713 );
714 }
715  
716 =head2 get_ancestor_terms
717  
718 Title : get_ancestor_terms
719 Usage : get_ancestor_terms(TermI term, TermI predicate_terms): TermI
720 get_child_terms(TermI term, RelationshipType predicate_terms): TermI
721 Function: Retrieves all ancestor terms of a given term, that satisfy
722 a relationship among those that are specified in the second
723 argument or undef otherwise. Uses _typed_traversal to find
724 all ancestors.
725  
726 Example :
727 Returns : Array of TermI objects.
728 Args : First argument is the term of interest, second is the list
729 of relationship type terms.
730  
731 =cut
732  
733 0 ▶sub get_ancestor_terms {
Subroutine get_ancestor_terms: not called CC=2 SCAR=17.9
Pod documented
734 0 my ( $self, $term, @relationship_types ) = @_;
735 0 ▶ $self->throw("term must be a valid object, not undef") unless defined $term;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $term 0 0 0
736  
737 0 return $self->_filter_unmarked(
738 $self->_filter_repeated(
739 $self->get_term_by_identifier(
740 $self->_typed_traversal(
741 $self->_inverted_relationship_store, 0,
742 $term->identifier, @relationship_types
743 )
744 )
745 )
746 );
747 }
748  
749 =head2 get_leaf_terms
750  
751 Title : get_leaf_terms
752 Usage : get_leaf_terms(): TermI
753 Function: Retrieves all leaf terms from the ontology. Leaf term is a term w/o descendants.
754 Example : @leaf_terms = $obj->get_leaf_terms()
755 Returns : Array of TermI objects.
756 Args :
757  
758 =cut
759  
760 0 ▶sub get_leaf_terms {
Subroutine get_leaf_terms: not called CC=4 SCAR=30.0
Pod documented
761 0 my ($self) = @_;
762 0 my @leaf_terms;
763  
764 0 foreach my $term ( values %{ $self->_term_store } ) {
765 push @leaf_terms, $term
766 if !defined $self->_relationship_store->{ $term->identifier }
767 0 ▶ && defined $self->_instantiated_terms_store->{ $term->identifier };
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->_relationship_store->{$term->identifier} and defined $self->_instantiated_terms_store->{$term->identifier} 0 0 0
Condition0%0/3
not defined $self->_relationship_store->{$term->identifier} and defined $self->_instantiated_terms_store->{$term->identifier}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $self->_relationship_store->{$term->identifier} and defined $self->_instantiated_terms_store->{$term->identifier}
not defined $self->_relationship_store->{$term->identifier} defined $self->_instantiated_terms_store->{$term->identifier}
Truth table0%0/3
not(A) and B
A= not defined $self->_relationship_store->{$term->identifier}B= defined $self->_instantiated_terms_store->{$term->identifier}
ABresult
0X0
100
111
768 }
769  
770 0 return @leaf_terms;
771 }
772  
773 =head2 get_root_terms
774  
775 Title : get_root_terms
776 Usage : get_root_terms(): TermI
777 Function: Retrieves all root terms from the ontology. Root term is a term w/o descendants.
778 Example : @root_terms = $obj->get_root_terms()
779 Returns : Array of TermI objects.
780 Args :
781  
782 =cut
783  
784 0 ▶sub get_root_terms {
Subroutine get_root_terms: not called CC=4 SCAR=30.0
Pod documented
785 0 my ($self) = @_;
786 0 my @root_terms;
787  
788 0 foreach my $term ( values %{ $self->_term_store } ) {
789 push @root_terms, $term
790 if !defined $self->_inverted_relationship_store->{ $term->identifier }
791 0 ▶ && defined $self->_instantiated_terms_store->{ $term->identifier };
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->_inverted_relationship_store->{$term->identifier} and defined $self->_instantiated_terms_store->{$term->identifier} 0 0 0
Condition0%0/3
not defined $self->_inverted_relationship_store->{$term->identifier} and defined $self->_instantiated_terms_store->{$term->identifier}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $self->_inverted_relationship_store->{$term->identifier} and defined $self->_instantiated_terms_store->{$term->identifier}
not defined $self->_inverted_relationship_store->{$term->identifier} defined $self->_instantiated_terms_store->{$term->identifier}
Truth table0%0/3
not(A) and B
A= not defined $self->_inverted_relationship_store->{$term->identifier}B= defined $self->_instantiated_terms_store->{$term->identifier}
ABresult
0X0
100
111
792 }
793  
794 0 return @root_terms;
795 }
796  
797 =head2 _filter_repeated
798  
799 Title : _filter_repeated
800 Usage : @lst = $self->_filter_repeated(@old_lst);
801 Function: Removes repeated terms
802 Example :
803 Returns : List of unique TermI objects
804 Args : List of TermI objects
805  
806 =cut
807  
808 0 ▶sub _filter_repeated {
Subroutine _filter_repeated: not called CC=3 SCAR=24.8
809 0 my ( $self, @args ) = @_;
810 0 my %h;
811  
812 0 foreach my $element (@args) {
813 0 ▶ $h{ $element->identifier } = $element if !defined $h{ $element->identifier };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h{$element->identifier} 0 0 0
814 }
815  
816 0 return values %h;
817 }
818  
819 =head2 get_all_terms
820  
821 Title : get_all_terms
822 Usage : get_all_terms(): TermI
823 Function: Retrieves all terms currently stored in the ontology.
824 Example : @all_terms = $obj->get_all_terms()
825 Returns : Array of TermI objects.
826 Args :
827  
828 =cut
829  
830 0 ▶sub get_all_terms {
Subroutine get_all_terms: not called CC=1 SCAR=6.9
Pod documented
831 0 my ($self) = @_;
832  
833 0 return $self->_filter_unmarked( values %{ $self->_term_store } );
834 }
835  
836 =head2 find_terms
837  
838 Title : find_terms
839 Usage : ($term) = $oe->find_terms(-identifier => "SO:0000263");
840 Function: Find term instances matching queries for their attributes.
841  
842 This implementation can efficiently resolve queries by
843 identifier.
844  
845 Example :
846 Returns : an array of zero or more Bio::Ontology::TermI objects
847 Args : Named parameters. The following parameters should be recognized
848 by any implementations:
849  
850 -identifier query by the given identifier
851 -name query by the given name
852  
853 =cut
854  
855 0 ▶sub find_terms {
Subroutine find_terms: not called CC=3 SCAR=24.8
Pod documented
856 0 my ( $self, @args ) = @_;
857 0 my @terms;
858  
859 0 my ( $id, $name ) = $self->_rearrange( [qw(IDENTIFIER NAME)], @args );
860  
861 0 ▶ if ( defined($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) { } 0 0 0
862 0 @terms = $self->get_term_by_identifier($id);
863 } else {
864 0 @terms = $self->get_all_terms();
865 }
866 0 ▶ if ( defined($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 0 0 0
867 0 @terms = grep { $_->name() eq $name; } @terms;
868 }
869 0 return @terms;
870 }
871  
872 =head2 relationship_factory
873  
874 Title : relationship_factory
875 Usage : $fact = $obj->relationship_factory()
876 Function: Get/set the object factory to be used when relationship
877 objects are created by the implementation on-the-fly.
878  
879 Example :
880 Returns : value of relationship_factory (a Bio::Factory::ObjectFactoryI
881 compliant object)
882 Args : on set, a Bio::Factory::ObjectFactoryI compliant object
883  
884 =cut
885  
886 1 ▶sub relationship_factory {
Subroutine relationship_factory: called CC=2 SCAR=8.1
Pod documented
887 1 my $self = shift;
888  
889 1 ▶ return $self->{'relationship_factory'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 0 1
890 0 return $self->{'relationship_factory'};
891 }
892  
893 =head2 term_factory
894  
895 Title : term_factory
896 Usage : $fact = $obj->term_factory()
897 Function: Get/set the object factory to be used when term objects are
898 created by the implementation on-the-fly.
899  
900 Note that this ontology engine implementation does not
901 create term objects on the fly, and therefore setting this
902 attribute is meaningless.
903  
904 Example :
905 Returns : value of term_factory (a Bio::Factory::ObjectFactoryI
906 compliant object)
907 Args : on set, a Bio::Factory::ObjectFactoryI compliant object
908  
909 =cut
910  
911 0 ▶sub term_factory {
Subroutine term_factory: not called CC=2 SCAR=17.9
Pod documented
912 0 my $self = shift;
913  
914 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
915 0 $self->warn(
916 "setting term factory, but " . ref($self) . " does not create terms on-the-fly" );
917 0 return $self->{'term_factory'} = shift;
918 }
919 0 return $self->{'term_factory'};
920 }
921  
922 =head2 _filter_unmarked
923  
924 Title : _filter_unmarked
925 Usage : _filter_unmarked(TermI terms): TermI
926 Function: Removes the uninstantiated terms from the list of terms
927 Example :
928 Returns : array of fully instantiated TermI objects
929 Args : array of TermI objects
930  
931 =cut
932  
933 0 ▶sub _filter_unmarked {
Subroutine _filter_unmarked: not called CC=4 SCAR=30.0
934 0 my ( $self, @terms ) = @_;
935 0 my @filtered_terms = ();
936  
937 0 ▶ if ( scalar(@terms) >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @terms >= 1) 0 0 0
938 0 foreach my $term (@terms) {
939 push @filtered_terms, $term
940 0 ▶ if defined $self->_instantiated_terms_store->{ $term->identifier };
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_instantiated_terms_store->{$term->identifier} 0 0 0
941 }
942 }
943  
944 0 return @filtered_terms;
945 }
946  
947 =head2 remove_term_by_id
948  
949 Title : remove_term_by_id
950 Usage : remove_term_by_id(String id): TermI
951 Function: Removes TermI object from the ontology engine using the
952 string id as an identifier. Current implementation does not
953 enforce consistency of the relationships using that term.
954 Example : $term = $soe->remove_term_by_id($id);
955 Returns : Object of class TermI or undef if not found.
956 Args : The string identifier of a term.
957  
958 =cut
959  
960 0 ▶sub remove_term_by_id {
Subroutine remove_term_by_id: not called CC=2 SCAR=17.9
Pod documented
961 0 my ( $self, $id ) = @_;
962  
963 0 ▶ if ( $self->get_term_by_identifier($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_term_by_identifier($id)) { } 0 0 0
964 0 my $term = $self->{_term_store}->{$id};
965 0 delete $self->{_term_store}->{$id};
966 0 return $term;
967 } else {
968 0 $self->warn("Term with id '$id' is not in the term store");
969 0 return;
970 }
971 }
972  
973 =head2 to_string
974  
975 Title : to_string
976 Usage : print $sv->to_string();
977 Function: Currently returns formatted string containing the number of
978 terms and number of relationships from the ontology engine.
979 Example : print $sv->to_string();
980 Returns :
981 Args :
982  
983 =cut
984  
985 0 ▶sub to_string {
Subroutine to_string: not called CC=1 SCAR=6.9
Pod documented
986 0 my ($self) = @_;
987 0 my $s = "";
988  
989 0 $s .= "-- # Terms:\n";
990 0 $s .= scalar( $self->get_all_terms ) . "\n";
991 0 $s .= "-- # Relationships:\n";
992 0 $s .= $self->_get_number_rels . "\n";
993  
994 0 return $s;
995 }
996  
997 =head2 _unique_termid
998  
999 Title : _unique_termid
1000 Usage :
1001 Function: Returns a string that can be used as ID using fail-over
1002 approaches.
1003  
1004 If the identifier attribute is not set, it uses the
1005 combination of name and ontology name, provided both are
1006 set. If they are not, it returns the name alone.
1007  
1008 Note that this is a private method. Call from inheriting
1009 classes but not from outside.
1010  
1011 Example :
1012 Returns : a string
1013 Args : a Bio::Ontology::TermI compliant object
1014  
1015 =cut
1016  
1017 0 ▶sub _unique_termid {
Subroutine _unique_termid: not called CC=4 SCAR=30.0
1018 0 my $self = shift;
1019 0 my $term = shift;
1020  
1021 0 ▶ return $term->identifier() if $term->identifier();
Branch: 1 branch
BranchTrueFalseTotal
if $term->identifier 0 0 0
1022 0 ▶ my $id = $term->ontology->name() if $term->ontology();
Branch: 1 branch
BranchTrueFalseTotal
if $term->ontology 0 0 0
1023 0 ▶ if ($id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id) { } 0 0 0
1024 0 $id .= '|';
1025 } else {
1026 0 $id = '';
1027 }
1028 0 $id .= $term->name();
1029 }
1030  
1031 #################################################################
1032 # aliases
1033 #################################################################
1034  
1035 3 *get_relationship_types = \&get_predicate_terms;
1036  
1037 3 1;
« Bio/DB/InMemoryCache.pm ↑ Summary Bio/Search/Hit/PullHitI.pm »