rvosa / bio-phylo

Bio::Phylo - Phyloinformatic analysis using Perl
http://search.cpan.org/dist/Bio-Phylo
GNU General Public License v3.0
16 stars 6 forks source link

Rerooting issue #10

Closed fangly closed 11 years ago

fangly commented 11 years ago

Hi Rutger,

I am trying to reroot a tree, in the way shown here: http://www.jstor.org/action/showPopup?citid=citart1&id=fg2&doi=10.1086%2F303327 http://www.ploscompbiol.org/article/info:doi/10.1371/journal.pcbi.1002743.g002/largerimage

In the following code, I have attemped to use the $tree->reroot() method for this purpose, but as you can see, I do not get the expected results. I am not using the proper method for this purpose or am I missing something else?

Thanks,

Florent

#! /usr/bin/env perl

use strict;
use warnings;
use Bio::Phylo::IO;

my $nw_string = '((A:0.11,B:0.12)Int1:0.13,(C:0.14,X:0.15)Int2:0.16)LUCA;';

my $tree = Bio::Phylo::IO->parse(
      -string => $nw_string,
      -format => 'newick',
)->first;

print "Original tree:\n";
my $ori_root = $tree->get_root;
print_tree($tree);
print "\n";

print "Rerooted tree:\n";
my $new_root = $tree->get_by_name('X');
$tree->reroot($new_root);
print_tree($tree);
print "\n";

# got string  : ((((A:0.110000,B:0.120000)Int1:0.130000,C:0.300000)LUCA)X:0.310000)root;
# but expected: ((((A:0.110000,B:0.120000)Int1:0.130000)LUCA:0.160000,C:0.140000)Int2:0.150000)X;

sub print_tree {
   my ($tree) = @_;
   print Bio::Phylo::IO->unparse(
      -phylo      => $tree,
      -format     => 'newick',
      -nodelabels => 1, # report name of internal nodes
   )."\n";
   return 1;
}
rvosa commented 11 years ago

To get the effect shown in the JSTOR image I think you should get the immediate parent for SP4 and SPH and call $node->collapse() on it.

fangly commented 11 years ago

Funny, I did not realize how ambiguous the term rerooting can be. Cheers for the explanations, Rutger.