71
64
package Bio::TreeIO::newick;
65
use vars qw($DefaultBootstrapStyle);
75
# Object preamble - inherits from Bio::Root::Root
78
68
use Bio::Event::EventGeneratorI;
79
#use XML::Handler::Subs;
82
@ISA = qw(Bio::TreeIO );
70
#initialize some package variables, could use 'our' but fails in perl < 5.6
72
$DefaultBootstrapStyle = 'traditional';
73
use base qw(Bio::TreeIO);
79
Args : -print_count => boolean default is false
80
-bootstrap_style => set the bootstrap style (one of nobranchlength,
82
-order_by => set the order by sort method
83
(see L<Bio::Node::Node::each_Descendent()> )
89
$self->SUPER::_initialize(@_);
90
my ($print_count,$style,$order_by) = $self->_rearrange([qw(PRINT_COUNT
94
$self->print_tree_count($print_count || 0);
95
$self->bootstrap_style($style || $DefaultBootstrapStyle);
96
$self->order_by($order_by) if defined $order_by;
87
104
Usage : my $tree = $treeio->next_tree
88
105
Function: Gets the next tree in the stream
89
Returns : Bio::Tree::TreeI
106
Returns : L<Bio::Tree::TreeI>
204
232
Usage : $treeio->write_tree($tree);
205
233
Function: Write a tree out to data stream in newick/phylip format
207
Args : Bio::Tree::TreeI object
235
Args : L<Bio::Tree::TreeI> object
212
my ($self,@trees) = @_;
240
my ($self,@trees) = @_;
241
my $orderby = $self->order_by;
242
my $bootstrap_style = $self->bootstrap_style;
243
if( $self->print_tree_count ){
244
$self->_print(sprintf(" %d\n",scalar @trees));
246
my $nl = $self->newline_each_node;
213
247
foreach my $tree( @trees ) {
214
my @data = _write_tree_Helper($tree->get_root_node);
215
if($data[-1] !~ /\)$/ ) {
216
$data[0] = "(".$data[0];
248
my @data = _write_tree_Helper($tree->get_root_node,
253
chomp($data[-1]);# remove last newline
254
$self->_print(join(",\n", @data), ";\n");
256
$self->_print(join(',', @data), ";\n");
219
$self->_print(join(',', @data), ";\n");
221
259
$self->flush if $self->_flush_on_write && defined $self->_fh;
225
263
sub _write_tree_Helper {
264
my ($node,$style,$orderby,$nl) = @_;
265
$style = '' unless defined $style;
227
266
return () if (!defined $node);
269
foreach my $n ( $node->each_Descendent($orderby) ) {
270
push @data, _write_tree_Helper($n,$style,$orderby,$nl);
231
foreach my $n ( $node->each_Descendent() ) {
232
push @data, _write_tree_Helper($n);
236
$data[0] = "(" . $data[0];
238
# let's explicitly write out the bootstrap if we've got it
240
if( defined ($b = $node->bootstrap) ) {
242
} elsif( defined ($b = $node->id) ) {
245
$data[-1] .= ":". $node->branch_length if( defined $node->branch_length);
248
if( defined $node->id || defined $node->branch_length ) {
249
push @data, sprintf("%s%s",
250
defined $node->id ? $node->id : '',
251
defined $node->branch_length ? ":" .
252
$node->branch_length : '');
273
# let's explicitly write out the bootstrap if we've got it
274
my $id = $node->id_output;
275
my $bs = $node->bootstrap; # bs better not have any spaces?
276
$bs =~ s/\s+//g if defined $bs;
277
my $bl = $node->branch_length;
280
$data[0] = "(\n" . $data[0];
283
$data[0] = "(" . $data[0];
287
if( $node->is_Leaf ) {
288
$node->debug("node is a leaf! This is unexpected...");
291
if( ! defined $bl || ! length($bl) ||
292
($style && $style =~ /nobranchlength/i) ) {
294
} elsif( defined $bl && length($bl) ) {
295
$data[-1] .= "$id:$bl";
300
if( ! defined $bl || ! length($bl) ||
301
($style && $style =~ /nobranchlength/i) ) {
303
if( defined $id || defined $bs ) {
304
$data[-1] .= defined $bs ? $bs : $id;
306
} elsif( $style =~ /molphy/i ) {
316
$data[-1] .= "[$bs]";
319
# traditional style of
320
# ((A:1,B:2)81:3); where 3 is internal node branch length
321
# and 81 is bootstrap/node label
322
if( defined $bs || defined $id ) {
323
$data[-1] .= defined $bs ? "$bs:$bl" : "$id:$bl";
324
} elsif( $bl =~ /\#/ ) {
331
} elsif( defined $id || defined $bl ) {
334
if( ! defined $bl || ! length($bl) ||
335
($style && $style =~ /nobranchlength/i) ) {
337
} elsif( defined $bl && length($bl) ) {
347
=head2 print_tree_count
349
Title : print_tree_count
350
Usage : $obj->print_tree_count($newval)
351
Function: Get/Set flag for printing out the tree count (paml,protml way)
352
Returns : value of print_tree_count (a scalar)
353
Args : on set, new value (a scalar or undef, optional)
358
sub print_tree_count{
360
return $self->{'_print_tree_count'} = shift if @_;
361
return $self->{'_print_tree_count'} || 0;
364
=head2 bootstrap_style
366
Title : bootstrap_style
367
Usage : $obj->bootstrap_style($newval)
368
Function: A description of how bootstraps and branch lengths are
369
written, as the ID part of the internal node or else in []
370
in the branch length (Molphy-like; I am sure there is a
371
better name for this but am not sure where to go for some
372
sort of format documentation)
374
If no branch lengths are requested then no bootstraps are usually
375
written (unless someone REALLY wants this functionality...)
377
Can take on strings which contain the possible values of
378
'nobranchlength' --> don't draw any branch lengths - this
379
is helpful if you don't want to have to
380
go through and delete branch len on all nodes
381
'molphy' --> draw bootstraps (100) like
382
(A:0.11,B:0.22):0.33[100];
383
'traditional' --> draw bootstraps (100) like
384
(A:0.11,B:0.22)100:0.33;
385
Returns : value of bootstrap_style (a scalar)
386
Args : on set, new value (a scalar or undef, optional)
396
if( $val !~ /^nobranchlength|molphy|traditional/i ) {
397
$self->warn("requested an unknown bootstrap style $val, expect one of nobranchlength,molphy,traditional, not updating value. Default is $DefaultBootstrapStyle\n");
399
$self->{'_bootstrap_style'} = $val;
402
return $self->{'_bootstrap_style'} || $DefaultBootstrapStyle;
408
Usage : $obj->order_by($newval)
409
Function: Allow node order to be specified (typically "alpha")
410
See L<Bio::Node::Node::each_Descendent()>
411
Returns : value of order_by (a scalar)
412
Args : on set, new value (a scalar or undef, optional)
420
return $self->{'order_by'} = shift if @_;
421
return $self->{'order_by'};