1 # BioPerl module for Bio::Tree::AnnotatableNode
3 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 # Cared for by Mira Han <mirhan@indiana.edu>
9 # You may distribute this module under the same terms as perl itself
11 # POD documentation - main docs before the code
15 Bio::Tree::AnnotatableNode - A Tree Node with support for annotation
19 use Bio::Tree::AnnotatableNode;
20 my $nodeA = Bio::Tree::AnnotatableNode->new();
21 my $nodeL = Bio::Tree::AnnotatableNode->new();
22 my $nodeR = Bio::Tree::AnnotatableNode->new();
24 my $node = Bio::Tree::AnnotatableNode->new();
25 $node->add_Descendents($nodeL);
26 $node->add_Descendents($nodeR);
28 print "node is not a leaf \n" if( $node->is_leaf);
30 # $node is-a Bio::AnnotatableI, hence:
31 my $ann_coll = $node->annotation();
32 # $ann_coll is-a Bio::AnnotationCollectionI, hence:
33 my @all_anns = $ann_coll->get_Annotations();
34 # do something with the annotation objects
38 Makes a Tree Node with Annotations, suitable for building a Tree. See
39 L<Bio::Tree::Node> for a full list of functionality.
45 User feedback is an integral part of the evolution of this and other
46 Bioperl modules. Send your comments and suggestions preferably to
47 the Bioperl mailing list. Your participation is much appreciated.
49 bioperl-l@bioperl.org - General discussion
50 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
54 Please direct usage questions or support issues to the mailing list:
56 I<bioperl-l@bioperl.org>
58 rather than to the module maintainer directly. Many experienced and
59 reponsive experts will be able look at the problem and quickly
60 address it. Please include a thorough description of the problem
61 with code and data examples if at all possible.
65 Report bugs to the Bioperl bug tracking system to help us keep track
66 of the bugs and their resolution. Bug reports can be submitted via
69 https://github.com/bioperl/bioperl-live/issues
71 =head1 AUTHOR - Mira Han
73 Email mirhan@indiana.edu
77 The rest of the documentation details each of the object methods.
78 Internal methods are usually preceded with a _
83 # Let the code begin...
85 package Bio
::Tree
::AnnotatableNode
;
88 use Bio
::Annotation
::Collection
;
90 use base
qw(Bio::Tree::Node Bio::AnnotatableI);
95 Usage : my $obj = Bio::Tree::AnnotatableNode->new();
96 Function: Builds a new Bio::Tree::AnnotatableNode object
97 Returns : Bio::Tree::AnnotatableNode
98 Args : -tostring => code reference to the tostring callback function (optional)
103 my ($class,@args) = @_;
104 my $self = $class->SUPER::new
(@args);
105 my $to_string_cb = $self->_rearrange([qw(TOSTRING)], @args);
107 $self->to_string_callback($to_string_cb);
114 # try to insure that everything is cleaned up
115 $self->SUPER::DESTROY
();
118 =head1 Methods for implementing Bio::AnnotatableI
125 Usage : $ann = $node->annotation or
126 $node->annotation($ann)
127 Function: Gets or sets the annotation
128 Returns : Bio::AnnotationCollectionI object
129 Args : None or Bio::AnnotationCollectionI object
130 See L<Bio::AnnotationCollectionI> and L<Bio::Annotation::Collection>
137 my ($self,$value) = @_;
138 if( defined $value ) {
139 $self->throw("object of class ".ref($value)." does not implement ".
140 "Bio::AnnotationCollectionI. Too bad.") unless $value->isa("Bio::AnnotationCollectionI");
141 $self->{'_annotation'} = $value;
143 elsif( ! defined $self->{'_annotation'})
145 $self->{'_annotation'} = Bio
::Annotation
::Collection
->new();
147 return $self->{'_annotation'};
151 =head1 Methods for implementing tag access through Annotation::SimpleValue
157 Title : add_tag_value
158 Usage : $node->add_tag_value($tag,$value)
159 Function: Adds a tag value to a node
160 Returns : number of values stored for this tag
161 Args : $tag - tag name
162 $value - value to store for the tag
168 my ($self,$tag,$value) = @_;
169 if( ! defined $tag || ! defined $value ) {
170 $self->warn("cannot call add_tag_value with an undefined value");
172 my $ac = $self->annotation();
173 my $sv = Bio
::Annotation
::SimpleValue
->new(-value
=> $value);
174 $ac->add_Annotation($tag, $sv);
175 return scalar $ac->get_Annotations($tag);
181 Usage : $node->remove_tag($tag)
182 Function: Remove the tag and all values for this tag
183 Returns : boolean representing success (0 if tag does not exist)
184 Args : $tag - tagname to remove
191 my ($self,$tag) = @_;
192 my $ac = $self->annotation();
193 if( @
{$ac->get_Annotations($tag)} ) {
194 $ac->remove_Annotations($tag);
200 =head2 remove_all_tags
202 Title : remove_all_tags
203 Usage : $node->remove_all_tags()
204 Function: Removes all tags
213 my $ac = $self->annotation();
214 $ac->remove_Annotations();
221 Usage : my @tags = $node->get_all_tags()
222 Function: Gets all the tag names for this Node
223 Returns : Array of tagnames
230 my $ac = $self->annotation();
231 my @tags = sort $ac->get_all_annotation_keys();
232 # how to restrict it to SimpleValues?
236 =head2 get_tag_values
238 Title : get_tag_values
239 Usage : my @values = $node->get_tag_value($tag)
240 Function: Gets the values for given tag ($tag)
241 Returns : Array of values or empty list if tag does not exist
242 Args : $tag - tag name
247 my ($self,$tag) = @_;
248 my $ac = $self->annotation();
249 my @values = map {$_->value()} $ac->get_Annotations($tag);
256 Usage : $node->has_tag($tag)
257 Function: Boolean test if tag exists in the Node
259 Args : $tag - tagname
264 my ($self,$tag) = @_;
265 my $ac = $self->annotation();
266 return ( scalar $ac->get_Annotations($tag) > 0);
270 =head1 Methods for implementing to_string
274 =head2 to_string_callback
276 Title : to_string_callback
277 Usage : $node->to_string_callback(\&func)
278 Function: get/set callback for to_string
279 Returns : code reference for the to_string callback function
280 Args : \&func - code reference to be set as the callback function
284 sub to_string_callback
{
285 # get/set callback, using $DEFAULT_CB if nothing is set
286 my ($self, $foo) = @_;
288 # $foo is callback code ref, self as first arg (so you have access to object data)
289 $self->{'_to_string_cb'} = $foo;
292 if (! defined $self->{'_to_string_cb'}) {
293 $self->{'_to_string_cb'} = \
&Bio
::Tree
::NodeI
::to_string
;
296 return $self->{'_to_string_cb'};
301 my $cb = $self->to_string_callback();
305 =head1 Methods for accessing Bio::Seq
312 Usage : $ann = $node->sequence or
313 $node->sequence($seq)
314 Function: Gets or sets the sequence
315 Returns : array reference of Bio::SeqI objects
316 Args : None or Bio::SeqI object
317 See L<Bio::SeqI> and L<Bio::Seq>
324 my ($self,$value) = @_;
325 if( defined $value ) {
326 $self->throw("object of class ".ref($value)." does not implement ".
327 "Bio::SeqI. Too bad.") unless $value->isa("Bio::SeqI");
328 push (@
{$self->{'_sequence'}}, $value);
330 #elsif( ! defined $self->{'_sequence'})
332 # $self->{'_sequence'} = Bio::Seq->new();
334 return $self->{'_sequence'};
340 Usage : if( $node->has_sequence) { # do something }
341 Function: tells if node has sequence attached
342 Returns : Boolean for whether or not node has Bio::SeqI attached.
350 return $self->{'_sequence'} && @
{$self->{'_sequence'}};