small update
[bioperl-live.git] / Bio / Tree / NodeNHX.pm
blob76a636bccc5bd7d7ac663ff028bd8c15e6095245
1 # $Id$
3 # BioPerl module for Bio::Tree::NodeNHX
5 # Cared for by Aaron Mackey <amackey@virginia.edu>
7 # Copyright Aaron Mackey
9 # You may distribute this module under the same terms as perl itself
11 # POD documentation - main docs before the code
13 =head1 NAME
15 Bio::Tree::NodeNHX - A Simple Tree Node with support for NHX tags
17 =head1 SYNOPSIS
19 use Bio::Tree::NodeNHX;
20 my $nodeA = Bio::Tree::NodeNHX->new();
21 my $nodeL = Bio::Tree::NodeNHX->new();
22 my $nodeR = Bio::Tree::NodeNHX->new();
24 my $node = Bio::Tree::NodeNHX->new();
25 $node->add_Descendents($nodeL);
26 $node->add_Descendents($nodeR);
28 print "node is not a leaf \n" if( $node->is_leaf);
30 =head1 DESCRIPTION
32 Makes a Tree Node with NHX tags, suitable for building a Tree. See
33 L<Bio::Tree::Node> for a full list of functionality.
35 =head1 FEEDBACK
37 =head2 Mailing Lists
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.
43 bioperl-l@bioperl.org - General discussion
44 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
46 =head2 Reporting Bugs
48 Report bugs to the Bioperl bug tracking system to help us keep track
49 of the bugs and their resolution. Bug reports can be submitted via
50 the web:
52 http://bugzilla.open-bio.org/
54 =head1 AUTHOR - Aaron Mackey
56 Email amackey@virginia.edu
58 =head1 CONTRIBUTORS
60 The NHX (New Hampshire eXtended) format was created by Chris Zmasek,
61 and is described at:
63 http://www.genetics.wustl.edu/eddy/forester/NHX.html
65 =head1 APPENDIX
67 The rest of the documentation details each of the object methods.
68 Internal methods are usually preceded with a _
70 =cut
73 # Let the code begin...
75 package Bio::Tree::NodeNHX;
76 use strict;
79 use base qw(Bio::Tree::Node);
81 =head2 new
83 Title : new
84 Usage : my $obj = Bio::Tree::NodeNHX->new();
85 Function: Builds a new Bio::Tree::NodeNHX object
86 Returns : Bio::Tree::NodeNHX
87 Args : -left => pointer to Left descendent (optional)
88 -right => pointer to Right descenent (optional)
89 -branch_length => branch length [integer] (optional)
90 -bootstrap => bootstrap value (string)
91 -description => description of node
92 -id => unique id for node
93 -nhx => hashref of NHX tags and values
95 =cut
97 sub new {
98 my($class,@args) = @_;
100 my $self = $class->SUPER::new(@args);
101 my ($nhx) = $self->_rearrange([qw(NHX)], @args);
102 $self->nhx_tag($nhx);
103 return $self;
106 sub DESTROY {
107 my ($self) = @_;
108 # try to insure that everything is cleaned up
109 $self->SUPER::DESTROY();
110 if( defined $self->{'_desc'} &&
111 ref($self->{'_desc'}) =~ /ARRAY/i ) {
112 while( my ($nodeid,$node) = each %{ $self->{'_desc'} } ) {
113 $node->{'_ancestor'} = undef; # insure no circular references
114 $node->DESTROY();
115 $node = undef;
117 $self->{'_desc'} = {};
121 sub to_string{
122 my ($self) = @_;
123 my @tags = $self->get_all_tags;
124 my $tagstr = '';
125 if( @tags ) {
126 $tagstr = '[' . join(":", "&&NHX",
127 map { "$_=" .join(',',
128 $self->get_tag_values($_))}
129 @tags ) . ']';
131 return sprintf("%s%s%s",
132 defined $self->id ? $self->id : '',
133 defined $self->branch_length ? ':' .
134 $self->branch_length : ' ',
135 $tagstr);
138 =head2 nhx_tag
140 Title : nhx_tag
141 Usage : my $tag = $nodenhx->nhx_tag(%tags);
142 Function: Set tag-value pairs for NHX nodes
143 Returns : none
144 Args : hashref to update the tags/value pairs
146 with a scalar value update the bootstrap value by default
149 =cut
151 sub nhx_tag {
152 my ($self, $tags) = @_;
153 if (defined $tags && (ref($tags) =~ /HASH/i)) {
154 while( my ($tag,$val) = each %$tags ) {
155 if( ref($val) =~ /ARRAY/i ) {
156 for my $v ( @$val ) {
157 $self->add_tag_value($tag,$v);
159 } else {
160 $self->add_tag_value($tag,$val);
163 if (exists $tags->{'B'}) {
164 $self->bootstrap($tags->{'B'});
166 } elsif (defined $tags and ! ref ($tags)) {
167 $self->debug( "here with $tags\n");
168 # bootstrap by default
169 $self->bootstrap($tags);