Mercurial > repos > mahtabm > ensembl
comparison variant_effect_predictor/Bio/Ontology/Ontology.pm @ 0:1f6dce3d34e0
Uploaded
author | mahtabm |
---|---|
date | Thu, 11 Apr 2013 02:01:53 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1f6dce3d34e0 |
---|---|
1 # $Id: Ontology.pm,v 1.2.2.4 2003/03/27 10:07:56 lapp Exp $ | |
2 # | |
3 # BioPerl module for Bio::Ontology::Ontology | |
4 # | |
5 # Cared for by Hilmar Lapp <hlapp at gmx.net> | |
6 # | |
7 # Copyright Hilmar Lapp | |
8 # | |
9 # You may distribute this module under the same terms as perl itself | |
10 | |
11 # | |
12 # (c) Hilmar Lapp, hlapp at gmx.net, 2003. | |
13 # (c) GNF, Genomics Institute of the Novartis Research Foundation, 2003. | |
14 # | |
15 # You may distribute this module under the same terms as perl itself. | |
16 # Refer to the Perl Artistic License (see the license accompanying this | |
17 # software package, or see http://www.perl.com/language/misc/Artistic.html) | |
18 # for the terms under which you may use, modify, and redistribute this module. | |
19 # | |
20 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | |
21 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | |
22 # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
23 # | |
24 | |
25 # POD documentation - main docs before the code | |
26 | |
27 =head1 NAME | |
28 | |
29 Bio::Ontology::Ontology - standard implementation of an Ontology | |
30 | |
31 =head1 SYNOPSIS | |
32 | |
33 use Bio::Ontology::Ontology; | |
34 | |
35 # create ontology object | |
36 my $ont = Bio::Ontology::Ontology->new(-name => "OBF"); | |
37 | |
38 # add terms, relationships ... | |
39 my $bp = Bio::Ontology::Term->new(-name => "Bioperl"); | |
40 my $obf = Bio::Ontology::Term->new(-name => "OBF"); | |
41 my $partof = Bio::Ontology::RelationshipType->get_instance("PART_OF"); | |
42 $ont->add_term($bp); | |
43 $ont->add_term($obf); | |
44 $ont->add_relationship($bp, $obf, $partof); | |
45 | |
46 # then query | |
47 my @terms = $ont->get_root_terms(); # "OBF" | |
48 my @desc = $ont->get_descendant_terms($terms[0], $partof); # "Bioperl" | |
49 # ... see methods for other ways to query | |
50 | |
51 # for advanced users, you can re-use the query engine outside of an | |
52 # ontology to let one instance manage multiple ontologies | |
53 my $ont2 = Bio::Ontology::Ontology->new(-name => "Foundations", | |
54 -engine => $ont->engine()); | |
55 | |
56 | |
57 =head1 DESCRIPTION | |
58 | |
59 This is a no-frills implementation of L<Bio::Ontology::OntologyI>. | |
60 | |
61 The query functions are implemented by delegation to an | |
62 OntologyEngineI implementation. | |
63 | |
64 =head1 FEEDBACK | |
65 | |
66 =head2 Mailing Lists | |
67 | |
68 User feedback is an integral part of the evolution of this and other | |
69 Bioperl modules. Send your comments and suggestions preferably to | |
70 the Bioperl mailing list. Your participation is much appreciated. | |
71 | |
72 bioperl-l@bioperl.org - General discussion | |
73 http://bioperl.org/MailList.shtml - About the mailing lists | |
74 | |
75 =head2 Reporting Bugs | |
76 | |
77 Report bugs to the Bioperl bug tracking system to help us keep track | |
78 of the bugs and their resolution. Bug reports can be submitted via | |
79 the web: | |
80 | |
81 http://bugzilla.bioperl.org/ | |
82 | |
83 =head1 AUTHOR - Hilmar Lapp | |
84 | |
85 Email hlapp at gmx.net | |
86 | |
87 =head1 CONTRIBUTORS | |
88 | |
89 Additional contributors names and emails here | |
90 | |
91 =head1 APPENDIX | |
92 | |
93 The rest of the documentation details each of the object methods. | |
94 Internal methods are usually preceded with a _ | |
95 | |
96 =cut | |
97 | |
98 | |
99 # Let the code begin... | |
100 | |
101 | |
102 package Bio::Ontology::Ontology; | |
103 use vars qw(@ISA); | |
104 use strict; | |
105 | |
106 # Object preamble - inherits from Bio::Root::Root | |
107 | |
108 use Bio::Root::Root; | |
109 use Bio::Ontology::OntologyI; | |
110 use Bio::Ontology::SimpleOntologyEngine; | |
111 | |
112 @ISA = qw(Bio::Root::Root Bio::Ontology::OntologyI); | |
113 | |
114 =head2 new | |
115 | |
116 Title : new | |
117 Usage : my $obj = new Bio::Ontology::Ontology(); | |
118 Function: Builds a new Bio::Ontology::Ontology object | |
119 Returns : an instance of Bio::Ontology::Ontology | |
120 Args : | |
121 | |
122 | |
123 =cut | |
124 | |
125 sub new { | |
126 my($class,@args) = @_; | |
127 | |
128 my $self = $class->SUPER::new(@args); | |
129 my ($name,$auth,$def,$id,$engine) = | |
130 $self->_rearrange([qw(NAME | |
131 AUTHORITY | |
132 DEFINITION | |
133 IDENTIFIER | |
134 ENGINE) | |
135 ], | |
136 @args); | |
137 defined($name) && $self->name($name); | |
138 defined($auth) && $self->authority($auth); | |
139 defined($def) && $self->definition($def); | |
140 defined($id) && $self->identifier($id); | |
141 $engine = Bio::Ontology::SimpleOntologyEngine->new() unless $engine; | |
142 $self->engine($engine); | |
143 | |
144 return $self; | |
145 } | |
146 | |
147 =head1 Methods from L<Bio::Ontology::OntologyI> | |
148 | |
149 =cut | |
150 | |
151 =head2 name | |
152 | |
153 Title : name | |
154 Usage : $obj->name($newval) | |
155 Function: Get/set the name of the ontology. | |
156 Example : | |
157 Returns : value of name (a scalar) | |
158 Args : on set, new value (a scalar or undef, optional) | |
159 | |
160 | |
161 =cut | |
162 | |
163 sub name{ | |
164 my $self = shift; | |
165 | |
166 return $self->{'name'} = shift if @_; | |
167 return $self->{'name'}; | |
168 } | |
169 | |
170 =head2 authority | |
171 | |
172 Title : authority | |
173 Usage : $obj->authority($newval) | |
174 Function: Get/set the authority for this ontology, for instance the | |
175 DNS base for the organization granting the name of the | |
176 ontology and identifiers for the terms. | |
177 | |
178 This attribute is optional and should not generally | |
179 expected by applications to have been set. It is here to | |
180 follow the rules for namespaces, which ontologies serve as | |
181 for terms. | |
182 | |
183 Example : | |
184 Returns : value of authority (a scalar) | |
185 Args : on set, new value (a scalar or undef, optional) | |
186 | |
187 | |
188 =cut | |
189 | |
190 sub authority{ | |
191 my $self = shift; | |
192 | |
193 return $self->{'authority'} = shift if @_; | |
194 return $self->{'authority'}; | |
195 } | |
196 | |
197 =head2 definition | |
198 | |
199 Title : definition | |
200 Usage : $obj->definition($newval) | |
201 Function: Get/set a descriptive definition of the ontology. | |
202 Example : | |
203 Returns : value of definition (a scalar) | |
204 Args : on set, new value (a scalar or undef, optional) | |
205 | |
206 | |
207 =cut | |
208 | |
209 sub definition{ | |
210 my $self = shift; | |
211 | |
212 return $self->{'definition'} = shift if @_; | |
213 return $self->{'definition'}; | |
214 } | |
215 | |
216 =head2 identifier | |
217 | |
218 Title : identifier | |
219 Usage : $id = $obj->identifier() | |
220 Function: Get an identifier for this ontology. | |
221 | |
222 This is primarily intended for look-up purposes. The value | |
223 is not modifiable and is determined automatically by the | |
224 implementation. Also, the identifier's uniqueness will only | |
225 hold within the scope of a particular application's run | |
226 time since it is derived from a memory location. | |
227 | |
228 Example : | |
229 Returns : value of identifier (a scalar) | |
230 Args : | |
231 | |
232 | |
233 =cut | |
234 | |
235 sub identifier{ | |
236 my $self = shift; | |
237 | |
238 if(@_) { | |
239 $self->throw("cannot modify identifier for ".ref($self)) | |
240 if exists($self->{'identifier'}); | |
241 my $id = shift; | |
242 $self->{'identifier'} = $id if $id; | |
243 } | |
244 if(! exists($self->{'identifier'})) { | |
245 ($self->{'identifier'}) = "$self" =~ /(0x[0-9a-fA-F]+)/; | |
246 } | |
247 return $self->{'identifier'}; | |
248 } | |
249 | |
250 =head2 close | |
251 | |
252 Title : close | |
253 Usage : | |
254 Function: Release any resources this ontology may occupy. In order | |
255 to efficiently release unused memory or file handles, you | |
256 should call this method once you are finished with an | |
257 ontology. | |
258 | |
259 Example : | |
260 Returns : TRUE on success and FALSE otherwise | |
261 Args : none | |
262 | |
263 | |
264 =cut | |
265 | |
266 sub close{ | |
267 my $self = shift; | |
268 | |
269 # if it is in the ontology store, remove it from there | |
270 my $store = Bio::Ontology::OntologyStore->get_instance(); | |
271 $store->remove_ontology($self); | |
272 # essentially we need to dis-associate from the engine here | |
273 $self->engine(undef); | |
274 return 1; | |
275 } | |
276 | |
277 =head1 Implementation-specific public methods | |
278 | |
279 =cut | |
280 | |
281 =head2 engine | |
282 | |
283 Title : engine | |
284 Usage : $engine = $obj->engine() | |
285 Function: Get/set the ontology engine to which all the query methods | |
286 delegate. | |
287 Example : | |
288 Returns : an object implementing L<Bio::Ontology::OntologyEngineI> | |
289 Args : on set, new value (an object implementing | |
290 L<Bio::Ontology::OntologyEngineI>, or undef) | |
291 | |
292 | |
293 =cut | |
294 | |
295 sub engine{ | |
296 my $self = shift; | |
297 | |
298 if(@_) { | |
299 my $engine = shift; | |
300 if($engine && (! (ref($engine) && | |
301 $engine->isa("Bio::Ontology::OntologyEngineI")))) { | |
302 $self->throw("object of class ".ref($engine)." does not implement". | |
303 " Bio::Ontology::OntologyEngineI. Bummer!"); | |
304 } | |
305 $self->{'engine'} = $engine; | |
306 } | |
307 return $self->{'engine'}; | |
308 } | |
309 | |
310 =head1 Methods defined in L<Bio::Ontology::OntologyEngineI> | |
311 | |
312 =cut | |
313 | |
314 =head2 add_term | |
315 | |
316 Title : add_term | |
317 Usage : add_term(TermI term): TermI | |
318 Function: Adds TermI object to the ontology engine term store | |
319 | |
320 If the ontology property of the term object was not set, | |
321 this implementation will set it to itself upon adding the | |
322 term. | |
323 | |
324 Example : $oe->add_term($term) | |
325 Returns : its argument. | |
326 Args : object of class TermI. | |
327 | |
328 | |
329 =cut | |
330 | |
331 sub add_term{ | |
332 my $self = shift; | |
333 my $term = shift; | |
334 | |
335 # set ontology if not set already | |
336 $term->ontology($self) if $term && (! $term->ontology()); | |
337 return $self->engine->add_term($term,@_); | |
338 } | |
339 | |
340 =head2 add_relationship | |
341 | |
342 Title : add_relationship | |
343 Usage : add_relationship(RelationshipI relationship): RelationshipI | |
344 add_relatioship(TermI subject, TermI predicate, TermI object) | |
345 Function: Adds a relationship object to the ontology engine. | |
346 Example : | |
347 Returns : Its argument. | |
348 Args : A RelationshipI object. | |
349 | |
350 | |
351 =cut | |
352 | |
353 sub add_relationship{ | |
354 my $self = shift; | |
355 my $rel = shift; | |
356 | |
357 if($rel && $rel->isa("Bio::Ontology::TermI")) { | |
358 # we need to construct the relationship object on the fly | |
359 my ($predicate,$object) = @_; | |
360 $rel = Bio::Ontology::Relationship->new(-subject_term => $rel, | |
361 -object_term => $object, | |
362 -predicate_term => $predicate, | |
363 -ontology => $self); | |
364 } | |
365 # set ontology if not set already | |
366 $rel->ontology($self) unless $rel->ontology(); | |
367 return $self->engine->add_relationship($rel); | |
368 } | |
369 | |
370 =head2 get_relationships | |
371 | |
372 Title : get_relationships | |
373 Usage : get_relationships(TermI term): RelationshipI[] | |
374 Function: Retrieves all relationship objects in the ontology, or all | |
375 relationships of a given term. | |
376 Example : | |
377 Returns : Array of Bio::Ontology::RelationshipI objects | |
378 Args : Optionally, a Bio::Ontology::TermI compliant object | |
379 | |
380 | |
381 =cut | |
382 | |
383 sub get_relationships{ | |
384 my $self = shift; | |
385 my $term = shift; | |
386 if($term) { | |
387 # we don't need to filter in this case | |
388 return $self->engine->get_relationships($term); | |
389 } | |
390 # else we need to filter by ontology | |
391 return grep { my $ont = $_->ontology; | |
392 # the first condition is a superset of the second, but | |
393 # we add it here for efficiency reasons, as many times | |
394 # it will short-cut to true and is supposedly faster than | |
395 # string comparison | |
396 ($ont == $self) || ($ont->name eq $self->name); | |
397 } $self->engine->get_relationships(@_); | |
398 } | |
399 | |
400 =head2 get_predicate_terms | |
401 | |
402 Title : get_predicate_terms | |
403 Usage : get_predicate_terms(): TermI[] | |
404 Function: Retrieves all relationship types. | |
405 Example : | |
406 Returns : Array of TermI objects | |
407 Args : | |
408 | |
409 | |
410 =cut | |
411 | |
412 sub get_predicate_terms{ | |
413 my $self = shift; | |
414 return grep { $_->ontology->name eq $self->name; | |
415 } $self->engine->get_predicate_terms(@_); | |
416 } | |
417 | |
418 =head2 get_child_terms | |
419 | |
420 Title : get_child_terms | |
421 Usage : get_child_terms(TermI term, TermI[] predicate_terms): TermI[] | |
422 Function: Retrieves all child terms of a given term, that satisfy a | |
423 relationship among those that are specified in the second | |
424 argument or undef otherwise. get_child_terms is a special | |
425 case of get_descendant_terms, limiting the search to the | |
426 direct descendants. | |
427 | |
428 Note that a returned term may possibly be in another | |
429 ontology than this one, because the underlying engine may | |
430 manage multiple ontologies and the relationships of terms | |
431 between them. If you only want descendants within this | |
432 ontology, you need to filter the returned array. | |
433 | |
434 Example : | |
435 Returns : Array of TermI objects. | |
436 Args : First argument is the term of interest, second is the list | |
437 of relationship type terms. | |
438 | |
439 | |
440 =cut | |
441 | |
442 sub get_child_terms{ | |
443 return shift->engine->get_child_terms(@_); | |
444 } | |
445 | |
446 =head2 get_descendant_terms | |
447 | |
448 Title : get_descendant_terms | |
449 Usage : get_descendant_terms(TermI term, TermI[] rel_types): TermI[] | |
450 Function: Retrieves all descendant terms of a given term, that | |
451 satisfy a relationship among those that are specified in | |
452 the second argument or undef otherwise. | |
453 | |
454 Note that a returned term may possibly be in another | |
455 ontology than this one, because the underlying engine may | |
456 manage multiple ontologies and the relationships of terms | |
457 between them. If you only want descendants within this | |
458 ontology, you need to filter the returned array. | |
459 | |
460 Example : | |
461 Returns : Array of TermI objects. | |
462 Args : First argument is the term of interest, second is the list | |
463 of relationship type terms. | |
464 | |
465 | |
466 =cut | |
467 | |
468 sub get_descendant_terms{ | |
469 return shift->engine->get_descendant_terms(@_); | |
470 } | |
471 | |
472 =head2 get_parent_terms | |
473 | |
474 Title : get_parent_terms | |
475 Usage : get_parent_terms(TermI term, TermI[] predicate_terms): TermI[] | |
476 Function: Retrieves all parent terms of a given term, that satisfy a | |
477 relationship among those that are specified in the second | |
478 argument or undef otherwise. get_parent_terms is a special | |
479 case of get_ancestor_terms, limiting the search to the | |
480 direct ancestors. | |
481 | |
482 Note that a returned term may possibly be in another | |
483 ontology than this one, because the underlying engine may | |
484 manage multiple ontologies and the relationships of terms | |
485 between them. If you only want descendants within this | |
486 ontology, you need to filter the returned array. | |
487 | |
488 Example : | |
489 Returns : Array of TermI objects. | |
490 Args : First argument is the term of interest, second is the list | |
491 of relationship type terms. | |
492 | |
493 | |
494 =cut | |
495 | |
496 sub get_parent_terms{ | |
497 return shift->engine->get_parent_terms(@_); | |
498 } | |
499 | |
500 =head2 get_ancestor_terms | |
501 | |
502 Title : get_ancestor_terms | |
503 Usage : get_ancestor_terms(TermI term, TermI[] predicate_terms): TermI[] | |
504 Function: Retrieves all ancestor terms of a given term, that satisfy | |
505 a relationship among those that are specified in the second | |
506 argument or undef otherwise. | |
507 | |
508 Note that a returned term may possibly be in another | |
509 ontology than this one, because the underlying engine may | |
510 manage multiple ontologies and the relationships of terms | |
511 between them. If you only want descendants within this | |
512 ontology, you need to filter the returned array. | |
513 | |
514 Example : | |
515 Returns : Array of TermI objects. | |
516 Args : First argument is the term of interest, second is the list | |
517 of relationship type terms. | |
518 | |
519 | |
520 =cut | |
521 | |
522 sub get_ancestor_terms{ | |
523 return shift->engine->get_ancestor_terms(@_); | |
524 } | |
525 | |
526 =head2 get_leaf_terms | |
527 | |
528 Title : get_leaf_terms | |
529 Usage : get_leaf_terms(): TermI[] | |
530 Function: Retrieves all leaf terms from the ontology. Leaf term is a | |
531 term w/o descendants. | |
532 | |
533 Example : @leaf_terms = $obj->get_leaf_terms() | |
534 Returns : Array of TermI objects. | |
535 Args : | |
536 | |
537 | |
538 =cut | |
539 | |
540 sub get_leaf_terms{ | |
541 my $self = shift; | |
542 return grep { my $ont = $_->ontology; | |
543 # the first condition is a superset of the second, but | |
544 # we add it here for efficiency reasons, as many times | |
545 # it will short-cut to true and is supposedly faster than | |
546 # string comparison | |
547 ($ont == $self) || ($ont->name eq $self->name); | |
548 } $self->engine->get_leaf_terms(@_); | |
549 } | |
550 | |
551 =head2 get_root_terms() | |
552 | |
553 Title : get_root_terms | |
554 Usage : get_root_terms(): TermI[] | |
555 Function: Retrieves all root terms from the ontology. Root term is a | |
556 term w/o descendants. | |
557 | |
558 Example : @root_terms = $obj->get_root_terms() | |
559 Returns : Array of TermI objects. | |
560 Args : | |
561 | |
562 | |
563 =cut | |
564 | |
565 sub get_root_terms{ | |
566 my $self = shift; | |
567 return grep { my $ont = $_->ontology; | |
568 # the first condition is a superset of the second, but | |
569 # we add it here for efficiency reasons, as many times | |
570 # it will short-cut to true and is supposedly faster than | |
571 # string comparison | |
572 ($ont == $self) || ($ont->name eq $self->name); | |
573 } $self->engine->get_root_terms(@_); | |
574 } | |
575 | |
576 =head2 get_all_terms | |
577 | |
578 Title : get_all_terms | |
579 Usage : get_all_terms: TermI[] | |
580 Function: Retrieves all terms from the ontology. | |
581 | |
582 We do not mandate an order here in which the terms are | |
583 returned. In fact, the default implementation will return | |
584 them in unpredictable order. | |
585 | |
586 Example : @terms = $obj->get_all_terms() | |
587 Returns : Array of TermI objects. | |
588 Args : | |
589 | |
590 | |
591 =cut | |
592 | |
593 sub get_all_terms{ | |
594 my $self = shift; | |
595 return grep { my $ont = $_->ontology; | |
596 # the first condition is a superset of the second, but | |
597 # we add it here for efficiency reasons, as many times | |
598 # it will short-cut to true and is supposedly faster than | |
599 # string comparison | |
600 ($ont == $self) || ($ont->name eq $self->name); | |
601 } $self->engine->get_all_terms(@_); | |
602 } | |
603 | |
604 =head2 find_terms | |
605 | |
606 Title : find_terms | |
607 Usage : ($term) = $oe->find_terms(-identifier => "SO:0000263"); | |
608 Function: Find term instances matching queries for their attributes. | |
609 | |
610 An implementation may not support querying for arbitrary | |
611 attributes, but can generally be expected to accept | |
612 -identifier and -name as queries. If both are provided, | |
613 they are implicitly intersected. | |
614 | |
615 Example : | |
616 Returns : an array of zero or more Bio::Ontology::TermI objects | |
617 Args : Named parameters. The following parameters should be recognized | |
618 by any implementations: | |
619 | |
620 -identifier query by the given identifier | |
621 -name query by the given name | |
622 | |
623 | |
624 =cut | |
625 | |
626 sub find_terms{ | |
627 my $self = shift; | |
628 return grep { $_->ontology->name eq $self->name; | |
629 } $self->engine->find_terms(@_); | |
630 } | |
631 | |
632 =head1 Factory for relationships and terms | |
633 | |
634 =cut | |
635 | |
636 =head2 relationship_factory | |
637 | |
638 Title : relationship_factory | |
639 Usage : $fact = $obj->relationship_factory() | |
640 Function: Get (and set, if the engine supports it) the object | |
641 factory to be used when relationship objects are created by | |
642 the implementation on-the-fly. | |
643 | |
644 Example : | |
645 Returns : value of relationship_factory (a Bio::Factory::ObjectFactoryI | |
646 compliant object) | |
647 Args : | |
648 | |
649 | |
650 =cut | |
651 | |
652 sub relationship_factory{ | |
653 return shift->engine->relationship_factory(@_); | |
654 } | |
655 | |
656 =head2 term_factory | |
657 | |
658 Title : term_factory | |
659 Usage : $fact = $obj->term_factory() | |
660 Function: Get (and set, if the engine supports it) the object | |
661 factory to be used when term objects are created by | |
662 the implementation on-the-fly. | |
663 | |
664 Example : | |
665 Returns : value of term_factory (a Bio::Factory::ObjectFactoryI | |
666 compliant object) | |
667 Args : | |
668 | |
669 | |
670 =cut | |
671 | |
672 sub term_factory{ | |
673 return shift->engine->term_factory(@_); | |
674 } | |
675 | |
676 | |
677 ################################################################# | |
678 # aliases | |
679 ################################################################# | |
680 | |
681 *get_relationship_types = \&get_predicate_terms; | |
682 | |
683 1; |