[Bioperl-l] Check the hierarchy (level) of nodes in a Newick tree

Ross KK Leung ross at cuhk.edu.hk
Fri Oct 7 13:16:12 UTC 2011


I use the following codes to test and find that the root node G is not
assigned state "Opter". Moreover, I find that when a tree looks like this,

        +---I (Wing)
        |
    +---M
    |   |
    |   +---H (Wing)
    |
    O
    |
    |  
    |  
    +---L (No-Wing)


L cannot match with internal node M as L is already a leaf node.


use Bio::TreeIO;
use Bio::Tree::TreeFunctionsI;

my ($testtree) = @ARGV;
system("cat $testtree");
my $treein = Bio::TreeIO->new( -file => $testtree, -format => 'newick' );
while( my $tree = $treein->next_tree ){
	%dist_matrix = ();

	my @leaves = $tree->get_leaf_nodes;

	foreach my $leaf( @leaves ){  
		$leaf->add_tag_value("state", "Wing");
	}
	$rootnode=$tree->get_root_node; 
	Traverse($rootnode);
	@tmpnodes = $tree->get_nodes;

	foreach $prnnode (@tmpnodes) {
		$prnval = $prnnode->get_tag_values("state");
		print $prnnode->id, "\t";
		print $prnval, "\n";
	}
}

sub Traverse {
	($node) = @_;
	my $state = "dummy";
	foreach my $desnode ( $node->each_Descendent() ) {
		if (!($desnode->is_Leaf)) {
			Traverse($desnode);
		}
		push @cmpnodes, $desnode;
	}
	#assume binary?
	if ($cmpnodes[0]->get_tag_values("state") eq
$cmpnodes[1]->get_tag_values("state")) {
	#	$node->add_tag_value("state",
$cmpnodes[0]->get_tag_values("state"));
		$node->add_tag_value("state", "Opter");
	}
}

__DATA__
((A:0.1,B:0.2)E:0.3,(C:0.3,D:0.4)F:0.5)G;




More information about the Bioperl-l mailing list