3 # BioPerl module for OntologyEngineI
5 # Cared for by Peter Dimitrov <dimitrov@gnf.org>
8 # (c) GNF, Genomics Institute of the Novartis Research Foundation, 2002.
10 # You may distribute this module under the same terms as perl itself.
11 # Refer to the Perl Artistic License (see the license accompanying this
12 # software package, or see http://www.perl.com/language/misc/Artistic.html)
13 # for the terms under which you may use, modify, and redistribute this module.
15 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
16 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
17 # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 # You may distribute this module under the same terms as perl itself
21 # POD documentation - main docs before the code
25 Bio::Ontology::OntologyEngineI - Interface a minimal Ontology implementation should satisfy
29 # see documentation of methods
33 This describes the minimal interface an ontology query engine should
34 provide. It intentionally does not make explicit references to the
35 ontology being a DAG, nor does it mandate that the ontology be a
36 vocabulary. Rather, it tries to generically express what should be
37 accessible (queriable) about an ontology.
39 The idea is to allow for different implementations for different
40 purposes, which may then differ as to which operations are efficient
41 and which are not, and how much richer the functionality is on top of
42 this minimalistic set of methods. Check modules in the Bio::Ontology
43 namespace to find out which implementations exist. At the time of
44 writing, there is a SimpleOntologyEngine (which does not use
45 Graph.pm), and a Graph.pm-based implementation in SimpleGOEngine.
47 Ontology parsers in Bio::OntologyIO are required to return an
48 implementation of this interface.
54 User feedback is an integral part of the evolution of this and other
55 Bioperl modules. Send your comments and suggestions preferably to
56 the Bioperl mailing list. Your participation is much appreciated.
58 bioperl-l@bioperl.org - General discussion
59 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
63 Report bugs to the Bioperl bug tracking system to help us keep track
64 of the bugs and their resolution. Bug reports can be submitted via
67 http://bugzilla.open-bio.org/
69 =head1 AUTHOR - Peter Dimitrov
71 Email dimitrov@gnf.org
75 The rest of the documentation details each of the object methods.
76 Internal methods are usually preceded with a _
81 # Let the code begin...
84 package Bio
::Ontology
::OntologyEngineI
;
88 use base
qw(Bio::Root::RootI);
93 Usage : add_term(TermI term): TermI
94 Function: Adds TermI object to the ontology engine term store
95 Example : $oe->add_term($term)
96 Returns : its argument.
97 Args : object of class TermI.
102 shift->throw_not_implemented();
105 =head2 add_relationship
107 Title : add_relationship
108 Usage : add_relationship(RelationshipI relationship): RelationshipI
109 Function: Adds a relationship object to the ontology engine.
111 Returns : Its argument.
112 Args : A RelationshipI object.
116 sub add_relationship
{
117 shift->throw_not_implemented();
120 =head2 add_relationship_type
122 Title : add_relationship_type
123 Usage : add_relationship_type(scalar,OntologyI ontology)
124 Function: Adds a relationshiptype object to the ontology engine.
126 Returns : 1 on success, undef on failure
127 Args : The name(scalar) of the relationshiptype, and the OntologyI
128 it is to be added to.
132 sub add_relationship_type
{
133 shift->throw_not_implemented();
136 =head2 get_relationship_type
138 Title : get_relationship_type
139 Usage : get_relationship_type(scalar): RelationshipTypeI
140 Function: Get a relationshiptype object from the ontology engine.
142 Returns : A RelationshipTypeI object.
143 Args : The name (scalar) of the RelationshipTypeI object desired.
147 sub get_relationship_type
{
148 shift->throw_not_implemented();
151 =head2 get_relationships
153 Title : get_relationships
154 Usage : get_relationships(TermI term): RelationshipI
155 Function: Retrieves all relationship objects from this ontology engine,
156 or all relationships of a term if a term is supplied.
158 Returns : Array of Bio::Ontology::RelationshipI objects
159 Args : None, or a Bio::Ontology::TermI compliant object for which
160 to retrieve the relationships.
164 sub get_relationships
{
165 shift->throw_not_implemented();
168 =head2 get_predicate_terms
170 Title : get_predicate_terms
171 Usage : get_predicate_terms(): TermI
179 sub get_predicate_terms
{
180 shift->throw_not_implemented();
183 =head2 get_child_terms
185 Title : get_child_terms
186 Usage : get_child_terms(TermI term, TermI predicate_terms): TermI
187 Function: Retrieves all child terms of a given term, that satisfy a
188 relationship among those that are specified in the second
189 argument or undef otherwise. get_child_terms is a special
190 case of get_descendant_terms, limiting the search to the
194 Returns : Array of TermI objects.
195 Args : First argument is the term of interest, second is the list
196 of relationship type terms.
201 shift->throw_not_implemented();
204 =head2 get_descendant_terms
206 Title : get_descendant_terms
207 Usage : get_descendant_terms(TermI term, TermI rel_types): TermI
208 Function: Retrieves all descendant terms of a given term, that
209 satisfy a relationship among those that are specified in
210 the second argument or undef otherwise.
212 Returns : Array of TermI objects.
213 Args : First argument is the term of interest, second is the list
214 of relationship type terms.
218 sub get_descendant_terms
{
219 shift->throw_not_implemented();
222 =head2 get_parent_terms
224 Title : get_parent_terms
225 Usage : get_parent_terms(TermI term, TermI predicate_terms): TermI
226 Function: Retrieves all parent terms of a given term, that satisfy a
227 relationship among those that are specified in the second
228 argument or undef otherwise. get_parent_terms is a special
229 case of get_ancestor_terms, limiting the search to the
233 Returns : Array of TermI objects.
234 Args : First argument is the term of interest, second is the list
235 of relationship type terms.
239 sub get_parent_terms
{
240 shift->throw_not_implemented();
243 =head2 get_ancestor_terms
245 Title : get_ancestor_terms
246 Usage : get_ancestor_terms(TermI term, TermI predicate_terms): TermI
247 Function: Retrieves all ancestor terms of a given term, that satisfy
248 a relationship among those that are specified in the second
249 argument or undef otherwise.
252 Returns : Array of TermI objects.
253 Args : First argument is the term of interest, second is the list
254 of relationship type terms.
258 sub get_ancestor_terms
{
259 shift->throw_not_implemented();
262 =head2 get_leaf_terms
264 Title : get_leaf_terms
265 Usage : get_leaf_terms(): TermI
266 Function: Retrieves all leaf terms from the ontology. Leaf term is a
267 term w/o descendants.
269 Example : @leaf_terms = $obj->get_leaf_terms()
270 Returns : Array of TermI objects.
276 shift->throw_not_implemented();
279 =head2 get_root_terms
281 Title : get_root_terms
282 Usage : get_root_terms(): TermI
283 Function: Retrieves all root terms from the ontology. Root term is a
286 Example : @root_terms = $obj->get_root_terms()
287 Returns : Array of TermI objects.
293 shift->throw_not_implemented();
296 =head1 Factory for relationships and terms
300 =head2 relationship_factory
302 Title : relationship_factory
303 Usage : $fact = $obj->relationship_factory()
304 Function: Get (and set, if the implementation supports it) the object
305 factory to be used when relationship objects are created by
306 the implementation on-the-fly.
309 Returns : value of relationship_factory (a Bio::Factory::ObjectFactory
315 sub relationship_factory
{
316 return shift->throw_not_implemented();
322 Usage : $fact = $obj->term_factory()
323 Function: Get (and set, if the implementation supports it) the object
324 factory to be used when term objects are created by
325 the implementation on-the-fly.
328 Returns : value of term_factory (a Bio::Factory::ObjectFactory
335 return shift->throw_not_implemented();
338 =head1 Decorator Methods
340 These methods come with a default implementation that uses the
341 abstract methods defined for this interface. This may not be very
342 efficient, and hence implementors are encouraged to override these
343 methods if they can provide more efficient implementations.
349 Title : get_all_terms
350 Usage : get_all_terms: TermI
351 Function: Retrieves all terms from the ontology.
353 This is more a decorator method. We provide a default
354 implementation here that loops over all root terms and gets
355 all descendants for each root term. The overall union of
356 terms is then made unique by name and ontology.
358 We do not mandate an order here in which the terms are
359 returned. In fact, the default implementation will return
360 them in unpredictable order.
362 Engine implementations that can provide a more efficient
363 method for obtaining all terms should definitely override
366 Example : @terms = $obj->get_all_terms()
367 Returns : Array of TermI objects.
375 my @roots = $self->get_root_terms();
376 # accumulate all descendants for each root term
377 my @terms = map { $self->get_descendant_terms($_); } @roots;
378 # add on the root terms themselves
379 push(@terms, @roots);
380 # make unique by name and ontology
381 my %name_map = map { ($_->name."@".$_->ontology->name, $_); } @terms;
383 return values %name_map;
389 Usage : ($term) = $oe->find_terms(-identifier => "SO:0000263");
390 Function: Find term instances matching queries for their attributes.
392 An implementation may not support querying for arbitrary
393 attributes, but can generally be expected to accept
394 -identifier and -name as queries. If both are provided,
395 they are implicitly intersected.
398 Returns : an array of zero or more Bio::Ontology::TermI objects
399 Args : Named parameters. The following parameters should be recognized
400 by any implementation:
402 -identifier query by the given identifier
403 -name query by the given name
410 @params{ map { lc $_; } keys %params } = values %params; # lowercase keys
413 my $ok = exists($params{-identifier
}) ?
414 $_->identifier() eq $params{-identifier
} : 1;
415 $ok && ((! exists($params{-name
})) ||
416 ($_->name() eq $params{-name
}));
417 } $self->get_all_terms();
421 =head1 Experimental API method proposals
423 Ontologies are a very new domain in bioperl, and we are not sure yet
424 what we will want to do on and with ontologies in which
425 situation. The methods from here on downwards are solely API
426 descriptions to solicit comment and feedback; the chance of any of
427 those being actually implemented already is very slim.
429 Disclaimer: As long as an API method stays in this section, it is
430 subject to change, possibly even radical change or complete
431 deletion. If it's not implemented yet (most likely it isn't),
432 implement yourself at your own risk.
434 So far for the disclaimer. The reason the API description is here,
435 however, is to solicit feedback. Please feel encouraged to share your
436 opinion, regardless of what it is (a notable difference of this API
437 method to others is that there is actually no working code behind it
438 - so the defense line is non-existent for practical purposes).
442 =head2 common_ancestor_path
444 Title : common_ancestor_path
446 Function: Get the paths from two terms A and B to term C, such that
447 there is no other term D to which A and B would have a shorter
448 path, provided there is a term C to which both A and B are
451 Note that the path to the common ancestor between A and A
452 exists, has distance zero, and predicate "identity".
454 The search for the common ancestor C can be further
455 constrained by supplying a predicate term. If supplied, the
456 predicates of the two paths (A,C) and (B,C) must have a
457 common ancestor identical to the predicate, or that has a
458 path to the predicate.
461 Returns : The path of the first term to the common ancestor in scalar
462 context, and both paths in list context. Paths are
463 Bio::Ontology::PathI compliant objects.
464 Args : The two terms (Bio::Ontology::TermI objects), and optionally
465 a constraining common predicate (Bio::Ontology::TermI object).
466 The latter may also be given as a scalar, in which case it
467 is treated as a boolean that, if TRUE, means that the two paths
468 must have identical predicates in order to be returned.
472 sub common_ancestor_path
{
473 return shift->throw_not_implemented();