A test to ensure Bio::PrimarySeqI->trunc() doesn't use clone() for a Bio::Seq::RichSe...
[bioperl-live.git] / Bio / Annotation / OntologyTerm.pm
bloba9c339ae1140918614e4b3ff17dd3b116cde3605
2 # BioPerl module for Bio::Annotation::OntologyTerm
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Hilmar Lapp <hlapp at gmx.net>
8 # Copyright Hilmar Lapp
10 # You may distribute this module under the same terms as perl itself
13 # (c) Hilmar Lapp, hlapp at gmx.net, 2002.
14 # (c) GNF, Genomics Institute of the Novartis Research Foundation, 2002.
16 # You may distribute this module under the same terms as perl itself.
17 # Refer to the Perl Artistic License (see the license accompanying this
18 # software package, or see http://www.perl.com/language/misc/Artistic.html)
19 # for the terms under which you may use, modify, and redistribute this module.
21 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
22 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
23 # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26 # POD documentation - main docs before the code
28 =head1 NAME
30 Bio::Annotation::OntologyTerm - An ontology term adapted to AnnotationI
32 =head1 SYNOPSIS
34 use Bio::Annotation::OntologyTerm;
35 use Bio::Annotation::Collection;
36 use Bio::Ontology::Term;
38 my $coll = Bio::Annotation::Collection->new();
40 # this also implements a tag/value pair, where tag _and_ value are treated
41 # as ontology terms
42 my $annterm = Bio::Annotation::OntologyTerm->new(-label => 'ABC1',
43 -tagname => 'Gene Name');
44 # ontology terms can be added directly - they implicitly have a tag
45 $coll->add_Annotation($annterm);
47 # implementation is by composition - you can get/set the term object
48 # e.g.
49 my $term = $annterm->term(); # term is-a Bio::Ontology::TermI
50 print "ontology term ",$term->name()," (ID ",$term->identifier(),
51 "), ontology ",$term->ontology()->name(),"\n";
52 $term = Bio::Ontology::Term->new(-name => 'ABC2',
53 -ontology => 'Gene Name');
54 $annterm->term($term);
56 =head1 DESCRIPTION
58 Ontology term annotation object
60 =head1 FEEDBACK
62 =head2 Mailing Lists
64 User feedback is an integral part of the evolution of this and other
65 Bioperl modules. Send your comments and suggestions preferably to one
66 of the Bioperl mailing lists. Your participation is much appreciated.
68 bioperl-l@bioperl.org - General discussion
69 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
71 =head2 Support
73 Please direct usage questions or support issues to the mailing list:
75 I<bioperl-l@bioperl.org>
77 rather than to the module maintainer directly. Many experienced and
78 reponsive experts will be able look at the problem and quickly
79 address it. Please include a thorough description of the problem
80 with code and data examples if at all possible.
82 =head2 Reporting Bugs
84 Report bugs to the Bioperl bug tracking system to help us keep track
85 the bugs and their resolution. Bug reports can be submitted via
86 the web:
88 https://github.com/bioperl/bioperl-live/issues
90 =head1 AUTHOR - Hilmar Lapp
92 Email hlapp at gmx.net
95 =head1 APPENDIX
97 The rest of the documentation details each of the object methods.
98 Internal methods are usually preceded with a _
100 =cut
103 # Let the code begin...
106 package Bio::Annotation::OntologyTerm;
107 use strict;
109 # Object preamble - inherits from Bio::Root::Root
111 use Bio::Ontology::Term;
113 use base qw(Bio::Root::Root Bio::AnnotationI Bio::Ontology::TermI);
115 =head2 new
117 Title : new
118 Usage : my $sv = Bio::Annotation::OntologyTerm->new();
119 Function: Instantiate a new OntologyTerm object
120 Returns : Bio::Annotation::OntologyTerm object
121 Args : -term => $term to initialize the term data field [optional]
122 Most named arguments that Bio::Ontology::Term accepts will work
123 here too. -label is a synonym for -name, -tagname is a synonym for
124 -ontology.
126 =cut
128 sub new{
129 my ($class,@args) = @_;
131 my $self = $class->SUPER::new(@args);
132 my ($term,$name,$label,$identifier,$definition,$ont,$tag) =
133 $self->_rearrange([qw(TERM
134 NAME
135 LABEL
136 IDENTIFIER
137 DEFINITION
138 ONTOLOGY
139 TAGNAME)],
140 @args);
141 if($term) {
142 $self->term($term);
143 } else {
144 $self->name($name || $label) if $name || $label;
145 $self->identifier($identifier) if $identifier;
146 $self->definition($definition) if $definition;
148 $self->ontology($ont || $tag) if $ont || $tag;
149 return $self;
153 =head1 AnnotationI implementing functions
155 =cut
157 =head2 as_text
159 Title : as_text
160 Usage : my $text = $obj->as_text
161 Function: Returns a textual representation of the annotation that
162 this object holds. Presently, it is tag name, name of the
163 term, and the is_obsolete attribute concatenated togather
164 with a delimiter (|).
166 Returns : string
167 Args : none
170 =cut
172 sub as_text{
173 my ($self) = @_;
175 return $self->tagname()."|".$self->name()."|".($self->is_obsolete()||'');
178 =head2 display_text
180 Title : display_text
181 Usage : my $str = $ann->display_text();
182 Function: returns a string. Unlike as_text(), this method returns a string
183 formatted as would be expected for te specific implementation.
185 One can pass a callback as an argument which allows custom text
186 generation; the callback is passed the current instance and any text
187 returned
188 Example :
189 Returns : a string
190 Args : [optional] callback
192 =cut
195 my $DEFAULT_CB = sub { $_[0]->identifier || ''};
197 sub display_text {
198 my ($self, $cb) = @_;
199 $cb ||= $DEFAULT_CB;
200 $self->throw("Callback must be a code reference") if ref $cb ne 'CODE';
201 return $cb->($self);
206 =head2 hash_tree
208 Title : hash_tree
209 Usage : my $hashtree = $value->hash_tree
210 Function: For supporting the AnnotationI interface just returns the value
211 as a hashref with the key 'value' pointing to the value
212 Returns : hashrf
213 Args : none
216 =cut
218 sub hash_tree{
219 my ($self) = @_;
221 my $h = {};
222 $h->{'name'} = $self->name();
223 $h->{'identifier'} = $self->identifier();
224 $h->{'definition'} = $self->definition();
225 $h->{'synonyms'} = [$self->get_synonyms()];
229 =head2 tagname
231 Title : tagname
232 Usage : $obj->tagname($newval)
233 Function: Get/set the tagname for this annotation value.
235 Setting this is optional. If set, it obviates the need to provide
236 a tag to AnnotationCollection when adding this object.
238 This is aliased to ontology() here.
239 Example :
240 Returns : value of tagname (a scalar)
241 Args : new value (a scalar, optional)
244 =cut
246 sub tagname{
247 my $self = shift;
249 return $self->ontology(@_) if @_;
250 # if in get mode we need to get the name from the ontology
251 my $ont = $self->ontology();
252 return ref($ont) ? $ont->name() : $ont;
255 =head1 Methods for Bio::Ontology::TermI compliance
257 =cut
259 =head2 term
261 Title : term
262 Usage : $obj->term($newval)
263 Function: Get/set the Bio::Ontology::TermI implementing object.
265 We implement TermI by composition, and this method sets/gets the
266 object we delegate to.
267 Example :
268 Returns : value of term (a Bio::Ontology::TermI compliant object)
269 Args : new value (a Bio::Ontology::TermI compliant object, optional)
272 =cut
274 sub term{
275 my ($self,$value) = @_;
276 if( defined $value) {
277 $self->{'term'} = $value;
279 if(! exists($self->{'term'})) {
280 $self->{'term'} = Bio::Ontology::Term->new();
282 return $self->{'term'};
285 =head2 identifier
287 Title : identifier
288 Usage : $term->identifier( "0003947" );
290 print $term->identifier();
291 Function: Set/get for the identifier of this Term.
292 Returns : The identifier [scalar].
293 Args : The identifier [scalar] (optional).
295 =cut
297 sub identifier {
298 return shift->term()->identifier(@_);
299 } # identifier
301 =head2 name
303 Title : name
304 Usage : $term->name( "N-acetylgalactosaminyltransferase" );
306 print $term->name();
307 Function: Set/get for the name of this Term.
308 Returns : The name [scalar].
309 Args : The name [scalar] (optional).
311 =cut
313 sub name {
314 return shift->term()->name(@_);
315 } # name
318 =head2 definition
320 Title : definition
321 Usage : $term->definition( "Catalysis of ..." );
323 print $term->definition();
324 Function: Set/get for the definition of this Term.
325 Returns : The definition [scalar].
326 Args : The definition [scalar] (optional).
328 =cut
330 sub definition {
331 return shift->term()->definition(@_);
332 } # definition
334 =head2 ontology
336 Title : ontology
337 Usage : $term->ontology( $top );
339 $top = $term->ontology();
340 Function: Set/get for a relationship between this Term and
341 another Term (e.g. the top level of the ontology).
342 Returns : The ontology of this Term [TermI].
343 Args : The ontology of this Term [TermI or scalar -- which
344 becomes the name of the catagory term] (optional).
346 =cut
348 sub ontology {
349 return shift->term()->ontology(@_);
352 =head2 is_obsolete
354 Title : is_obsolete
355 Usage : $term->is_obsolete( 1 );
357 if ( $term->is_obsolete() )
358 Function: Set/get for the obsoleteness of this Term.
359 Returns : the obsoleteness [0 or 1].
360 Args : the obsoleteness [0 or 1] (optional).
362 =cut
364 sub is_obsolete {
365 return shift->term()->is_obsolete(@_);
366 } # is_obsolete
368 =head2 comment
370 Title : comment
371 Usage : $term->comment( "Consider the term ..." );
373 print $term->comment();
374 Function: Set/get for an arbitrary comment about this Term.
375 Returns : A comment.
376 Args : A comment (optional).
378 =cut
380 sub comment {
381 return shift->term()->comment(@_);
382 } # comment
384 =head2 get_synonyms
386 Title : get_synonyms()
387 Usage : @aliases = $term->get_synonyms();
388 Function: Returns a list of aliases of this Term.
389 Returns : A list of aliases [array of [scalar]].
390 Args :
392 =cut
394 sub get_synonyms {
395 return shift->term()->get_synonyms(@_);
396 } # get_synonyms
398 =head2 add_synonym
400 Title : add_synonym
401 Usage : $term->add_synonym( @asynonyms );
403 $term->add_synonym( $synonym );
404 Function: Pushes one or more synonyms into the list of synonyms.
405 Returns :
406 Args : One synonym [scalar] or a list of synonyms [array of [scalar]].
408 =cut
410 sub add_synonym {
411 return shift->term()->add_synonym(@_);
412 } # add_synonym
415 =head2 remove_synonyms
417 Title : remove_synonyms()
418 Usage : $term->remove_synonyms();
419 Function: Deletes (and returns) the synonyms of this Term.
420 Returns : A list of synonyms [array of [scalar]].
421 Args :
423 =cut
425 sub remove_synonyms {
426 return shift->term()->remove_synonyms(@_);
427 } # remove_synonyms
429 =head2 get_dblinks
431 Title : get_dblinks()
432 Usage : @ds = $term->get_dblinks();
433 Function: Returns a list of each dblinks of this GO term.
434 Returns : A list of dblinks [array of [scalars]].
435 Args :
436 Note : this is deprecated in favor of get_dbxrefs(), which works with strings
437 or L<Bio::Annotation::DBLink> instances
439 =cut
441 sub get_dblinks {
442 my $self = shift;
443 $self->deprecated('get_dblinks() is deprecated; use get_dbxrefs()');
444 return $self->term->get_dbxrefs(@_);
445 } # get_dblinks
447 =head2 get_dbxrefs
449 Title : get_dbxrefs()
450 Usage : @ds = $term->get_dbxrefs();
451 Function: Returns a list of each dblinks of this GO term.
452 Returns : A list of dblinks [array of [scalars] or Bio::Annotation::DBLink instances].
453 Args :
455 =cut
457 sub get_dbxrefs {
458 return shift->term->get_dbxrefs(@_);
459 } # get_dblinks
461 =head2 add_dblink
463 Title : add_dblink
464 Usage : $term->add_dblink( @dbls );
466 $term->add_dblink( $dbl );
467 Function: Pushes one or more dblinks
468 into the list of dblinks.
469 Returns :
470 Args : One dblink [scalar] or a list of
471 dblinks [array of [scalars]].
472 Note : this is deprecated in favor of add_dbxref(), which works with strings
473 or L<Bio::Annotation::DBLink> instances
475 =cut
477 sub add_dblink {
478 my $self = shift;
479 $self->deprecated('add_dblink() is deprecated; use add_dbxref()');
480 return $self->term->add_dbxref(@_);
481 } # add_dblink
483 =head2 add_dbxref
485 Title : add_dbxref
486 Usage : $term->add_dbxref( @dbls );
488 $term->add_dbxref( $dbl );
489 Function: Pushes one or more dblinks
490 into the list of dblinks.
491 Returns :
492 Args :
494 =cut
496 sub add_dbxref {
497 return shift->term->add_dbxref(@_);
500 =head2 remove_dblinks
502 Title : remove_dblinks()
503 Usage : $term->remove_dblinks();
504 Function: Deletes (and returns) the definition references of this GO term.
505 Returns : A list of definition references [array of [scalars]].
506 Args :
507 Note : this is deprecated in favor of remove_dbxrefs(), which works with strings
508 or L<Bio::Annotation::DBLink> instances
510 =cut
512 sub remove_dblinks {
513 my $self = shift;
514 $self->deprecated('remove_dblinks() is deprecated; use remove_dbxrefs()');
515 return $self->term->remove_dbxrefs(@_);
516 } # remove_dblinks
518 =head2 remove_dbxrefs
520 Title : remove_dbxrefs()
521 Usage : $term->remove_dbxrefs();
522 Function: Deletes (and returns) the definition references of this GO term.
523 Returns : A list of definition references [array of [scalars]].
524 Args :
526 =cut
528 sub remove_dbxrefs {
529 return shift->term->remove_dbxrefs(@_);
532 =head2 get_secondary_ids
534 Title : get_secondary_ids
535 Usage : @ids = $term->get_secondary_ids();
536 Function: Returns a list of secondary identifiers of this Term.
538 Secondary identifiers mostly originate from merging terms,
539 or possibly also from splitting terms.
541 Returns : A list of secondary identifiers [array of [scalar]]
542 Args :
544 =cut
546 sub get_secondary_ids {
547 return shift->term->get_secondary_ids(@_);
548 } # get_secondary_ids
551 =head2 add_secondary_id
553 Title : add_secondary_id
554 Usage : $term->add_secondary_id( @ids );
556 $term->add_secondary_id( $id );
557 Function: Adds one or more secondary identifiers to this term.
558 Returns :
559 Args : One or more secondary identifiers [scalars]
561 =cut
563 sub add_secondary_id {
564 return shift->term->add_secondary_id(@_);
565 } # add_secondary_id
568 =head2 remove_secondary_ids
570 Title : remove_secondary_ids
571 Usage : $term->remove_secondary_ids();
572 Function: Deletes (and returns) the secondary identifiers of this Term.
573 Returns : The previous list of secondary identifiers [array of [scalars]]
574 Args :
576 =cut
578 sub remove_secondary_ids {
579 return shift->term->remove_secondary_ids(@_);
580 } # remove_secondary_ids