3 # BioPerl module for Bio::TreeIO::newick
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
7 # Cared for by Jason Stajich <jason@bioperl.org>
9 # Copyright Jason Stajich
11 # You may distribute this module under the same terms as perl itself
13 # POD documentation - main docs before the code
17 Bio::TreeIO::newick - TreeIO implementation for parsing
18 Newick/New Hampshire/PHYLIP format.
22 # do not use this module directly
24 my $treeio = Bio::TreeIO->new(-format => 'newick',
25 -file => 't/data/LOAD_Ccd1.dnd');
26 my $tree = $treeio->next_tree;
30 This module handles parsing and writing of Newick/PHYLIP/New Hampshire format.
36 User feedback is an integral part of the evolution of this and other
37 Bioperl modules. Send your comments and suggestions preferably to the
38 Bioperl mailing list. Your participation is much appreciated.
40 bioperl-l@bioperl.org - General discussion
41 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
45 Please direct usage questions or support issues to the mailing list:
47 L<bioperl-l@bioperl.org>
49 rather than to the module maintainer directly. Many experienced and
50 reponsive experts will be able look at the problem and quickly
51 address it. Please include a thorough description of the problem
52 with code and data examples if at all possible.
56 Report bugs to the Bioperl bug tracking system to help us keep track
57 of the bugs and their resolution. Bug reports can be submitted via the
60 http://bugzilla.open-bio.org/
62 =head1 AUTHOR - Jason Stajich
64 Email jason-at-bioperl-dot-org
68 The rest of the documentation details each of the object methods.
69 Internal methods are usually preceded with a _
74 # Let the code begin...
77 package Bio
::TreeIO
::newick
;
78 use vars
qw($DefaultBootstrapStyle);
81 use Bio::Event::EventGeneratorI;
83 #initialize some package variables, could use 'our' but fails in perl < 5.6
85 $DefaultBootstrapStyle = 'traditional';
86 use base qw(Bio::TreeIO);
92 Args : -print_count => boolean default is false
93 -bootstrap_style => set the bootstrap style (one of nobranchlength,
95 -order_by => set the order by sort method
96 (see L<Bio::Node::Node::each_Descendent()> )
102 $self->SUPER::_initialize
(@_);
103 my ($print_count,$style,$order_by) = $self->_rearrange([qw(PRINT_COUNT
107 $self->print_tree_count($print_count || 0);
108 $self->bootstrap_style($style || $DefaultBootstrapStyle);
109 $self->order_by($order_by) if defined $order_by;
117 Usage : my $tree = $treeio->next_tree
118 Function: Gets the next tree in the stream
119 Returns : L<Bio::Tree::TreeI>
128 return unless $_ = $self->_readline;
131 my $despace = sub {my $dirty = shift; $dirty =~ s/\s+//gs; return $dirty};
132 my $dequote = sub {my $dirty = shift; $dirty =~ s/^"?\s*(.+?)\s*"?$/$1/; return $dirty};
133 s/([^"]*)(".+?")([^"]*)/$despace->($1) . $dequote->($2) . $despace->($3)/egsx;
134 if( s/^\s*\[([^\]]+)\]// ) {
138 if( $match =~ /([-\d\.+]+)/ ) {
143 $self->debug("entry is $_\n");
144 # my $empty = chr(20);
146 # replace empty labels with a tag
147 # s/\(,/\($empty,/ig;
150 # s/,\)/,$empty\)/ig;
154 $self->_eventHandler->start_document;
155 my ($prev_event,$lastevent,$id) = ('','','');
156 foreach my $ch ( split(//,$_) ) {
158 my $tree = $self->_eventHandler->end_document($chars);
159 $tree->score($score) if defined $score;
160 if( $self->internal_node_id eq 'bootstrap' ) {
161 $tree->move_id_to_bootstrap;
164 } elsif( $ch eq '(' ) {
166 $self->_eventHandler->start_element( {'Name' => 'tree'} );
167 } elsif($ch eq ')' ) {
168 if( length($chars) ) {
169 if( $lastevent eq ':' ) {
170 $self->_eventHandler->start_element( { 'Name' => 'branch_length'});
171 $self->_eventHandler->characters($chars);
172 $self->_eventHandler->end_element( {'Name' => 'branch_length'});
173 $lastevent = $prev_event;
175 $self->debug("internal node, id with no branchlength is $chars\n");
176 $self->_eventHandler->start_element( { 'Name' => 'node' } );
177 $self->_eventHandler->start_element( { 'Name' => 'id' } );
178 $self->_eventHandler->characters($chars);
179 $self->_eventHandler->end_element( { 'Name' => 'id' } );
183 if( $lastevent ne ')' ) {
187 $self->_eventHandler->start_element({'Name' => 'leaf'});
188 $self->_eventHandler->characters($leafstatus);
189 $self->_eventHandler->end_element({'Name' => 'leaf'});
192 $self->_eventHandler->start_element( {'Name' => 'node'} );
195 $self->_eventHandler->end_element( {'Name' => 'node'} );
196 $self->_eventHandler->end_element( {'Name' => 'tree'} );
198 } elsif ( $ch eq ',' ) {
199 if( length($chars) ) {
200 if( $lastevent eq ':' ) {
201 $self->_eventHandler->start_element( { 'Name' => 'branch_length'});
202 $self->_eventHandler->characters($chars);
203 $self->_eventHandler->end_element( {'Name' => 'branch_length'});
204 $lastevent = $prev_event;
207 $self->debug("leaf id with no branchlength is $chars\n");
208 $self->_eventHandler->start_element( { 'Name' => 'node' } );
209 $self->_eventHandler->start_element( { 'Name' => 'id' } );
210 $self->_eventHandler->characters($chars);
211 $self->_eventHandler->end_element( { 'Name' => 'id' } );
215 $self->_eventHandler->start_element( { 'Name' => 'node' } );
218 if( $lastevent ne ')' ) {
221 $self->_eventHandler->start_element({'Name' => 'leaf'});
222 $self->_eventHandler->characters($leafstatus);
223 $self->_eventHandler->end_element({'Name' => 'leaf'});
224 $self->_eventHandler->end_element( {'Name' => 'node'} );
227 } elsif( $ch eq ':' ) {
228 $self->debug("id with a branchlength coming is $chars\n");
229 $self->_eventHandler->start_element( { 'Name' => 'node' } );
230 $self->_eventHandler->start_element( { 'Name' => 'id' } );
231 $self->_eventHandler->characters($chars);
232 $self->_eventHandler->end_element( { 'Name' => 'id' } );
239 $prev_event = $lastevent;
242 my $tree = $self->_eventHandler->end_document($chars);
243 return $tree if $tree;
250 Usage : $treeio->write_tree($tree);
251 Function: Write a tree out to data stream in newick/phylip format
253 Args : L<Bio::Tree::TreeI> object
258 my ($self,@trees) = @_;
259 my $orderby = $self->order_by;
260 my $bootstrap_style = $self->bootstrap_style;
261 if( $self->print_tree_count ){
262 $self->_print(sprintf(" %d\n",scalar @trees));
264 my $nl = $self->newline_each_node;
265 foreach my $tree( @trees ) {
267 if( ! defined $tree || ref($tree) =~ /ARRAY/i ||
268 ! $tree->isa('Bio::Tree::TreeI') ) {
269 $self->throw("Calling write_tree with non Bio::Tree::TreeI object\n");
271 my @data = _write_tree_Helper
($tree->get_root_node,
276 chomp($data[-1]);# remove last newline
277 $self->_print(join(",\n", @data), ";\n");
279 $self->_print(join(',', @data), ";\n");
282 $self->flush if $self->_flush_on_write && defined $self->_fh;
286 sub _write_tree_Helper
{
287 my ($node,$style,$orderby,$nl) = @_;
288 $style = '' unless defined $style;
289 return () if (!defined $node);
292 foreach my $n ( $node->each_Descendent($orderby) ) {
293 push @data, _write_tree_Helper
($n,$style,$orderby,$nl);
296 # let's explicitly write out the bootstrap if we've got it
297 my $id = $node->id_output;
298 my $bs = $node->bootstrap; # bs better not have any spaces?
299 $bs =~ s/\s+//g if defined $bs;
300 my $bl = $node->branch_length;
303 $data[0] = "(\n" . $data[0];
306 $data[0] = "(" . $data[0];
310 if( $node->is_Leaf ) {
311 $node->debug("node is a leaf! This is unexpected...");
314 if( ! defined $bl || ! length($bl) ||
315 ($style && $style =~ /nobranchlength/i) ) {
317 } elsif( defined $bl && length($bl) ) {
318 $data[-1] .= "$id:$bl";
323 if( ! defined $bl || ! length($bl) ||
324 ($style && $style =~ /nobranchlength/i) ) {
326 if( defined $id || defined $bs ) {
327 $data[-1] .= defined $bs ?
$bs : $id;
329 } elsif( $style =~ /molphy/i ) {
339 $data[-1] .= "[$bs]";
342 # traditional style of
343 # ((A:1,B:2)81:3); where 3 is internal node branch length
344 # and 81 is bootstrap/node label
345 if( defined $bs || defined $id ) {
346 $data[-1] .= defined $bs ?
"$bs:$bl" : "$id:$bl";
347 } elsif( $bl =~ /\#/ ) {
354 } elsif( defined $id || defined $bl ) {
357 if( ! defined $bl || ! length($bl) ||
358 ($style && $style =~ /nobranchlength/i) ) {
360 } elsif( defined $bl && length($bl) ) {
370 =head2 print_tree_count
372 Title : print_tree_count
373 Usage : $obj->print_tree_count($newval)
374 Function: Get/Set flag for printing out the tree count (paml,protml way)
375 Returns : value of print_tree_count (a scalar)
376 Args : on set, new value (a scalar or undef, optional)
381 sub print_tree_count
{
383 return $self->{'_print_tree_count'} = shift if @_;
384 return $self->{'_print_tree_count'} || 0;
387 =head2 bootstrap_style
389 Title : bootstrap_style
390 Usage : $obj->bootstrap_style($newval)
391 Function: A description of how bootstraps and branch lengths are
392 written, as the ID part of the internal node or else in []
393 in the branch length (Molphy-like; I am sure there is a
394 better name for this but am not sure where to go for some
395 sort of format documentation)
397 If no branch lengths are requested then no bootstraps are usually
398 written (unless someone REALLY wants this functionality...)
400 Can take on strings which contain the possible values of
401 'nobranchlength' --> don't draw any branch lengths - this
402 is helpful if you don't want to have to
403 go through and delete branch len on all nodes
404 'molphy' --> draw bootstraps (100) like
405 (A:0.11,B:0.22):0.33[100];
406 'traditional' --> draw bootstraps (100) like
407 (A:0.11,B:0.22)100:0.33;
408 Returns : value of bootstrap_style (a scalar)
409 Args : on set, new value (a scalar or undef, optional)
419 if( $val !~ /^nobranchlength|molphy|traditional/i ) {
420 $self->warn("requested an unknown bootstrap style $val, expect one of nobranchlength,molphy,traditional, not updating value. Default is $DefaultBootstrapStyle\n");
422 $self->{'_bootstrap_style'} = $val;
425 return $self->{'_bootstrap_style'} || $DefaultBootstrapStyle;
431 Usage : $obj->order_by($newval)
432 Function: Allow node order to be specified (typically "alpha")
433 See L<Bio::Node::Node::each_Descendent()>
434 Returns : value of order_by (a scalar)
435 Args : on set, new value (a scalar or undef, optional)
443 return $self->{'order_by'} = shift if @_;
444 return $self->{'order_by'};