This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Removing branch support values that meet certain requirements in a newick file

Hi!

I have a tree file in newick format that looks a bit like this:

(((A:0.5,B:0.5)30:0.3),((((C:0.3,D:0.6)80:0.4),F:0.3)100:0.2));

All the numbers between 0 and 1 are branch lengths.

The 30, 80, and 100 are bootstrap support values.

What I like to do is with a command or script or program, delete all bootstrap support values lower then 70. Thus, the resulting newick tree will look like.

(((A:0.5,B:0.5):0.3),((((C:0.3,D:0.6)80:0.4),F:0.3)100:0.2));

I feel like it should be quiet easy, but I simply can't get it.

newick bootstrap posterior probability perl

2 answers


use Bio::TreeIO; 
use strict;

my $in = Bio::TreeIO->new(-format => 'newick', -fh => \*DATA); 
# replace above with this to open a file
# my $in = Bio::TreeIO->new(-format => 'newick', -file => "mytree.tre");

# see Bio::TreeIO on how you can write directly to a file, otherwise to STDOUT by default

my $out = Bio::TreeIO->new(-format => 'newick');
while( my $tree = $in->next_tree ) {
# get all the internal nodes (i.e. those which are not leaf nodes)

 for my $node ( grep { ! $_->is_Leaf } $tree->get_nodes ) {
# bootstrap is stored in the id field for newick trees since there is no other defined slot  
  if($node->id < 70 ) {
    $node->id('');  
   }
 }
 $out->write_tree($tree);
}
__DATA__
(((A:0.5,B:0.5)30:0.3),((((C:0.3,D:0.6)80:0.4),F:0.3)100:0.2));

Awesome, it seems to do the trick! But for some reason I still commonly get error / warning messages like

Use of uninitialized value in numeric lt (<) at script.pl line 28, <GEN0> line 1.

Line 28 corresponds to the if($node->id < 70 ) { in my version of the script

Log in to answer this question.