Question: Delete A Node From Newick Tree
2
gravatar for Maria
7.1 years ago by
Maria170
Maria170 wrote:

I'm trying to remove a node from a newick tree using bioperl. The tree file contains this data :

(((A:5,B:5)90:2,C:4)25:3,D:10);

Below is the code:

use Bio::TreeIO;
use Bio::Tree::TreeFunctionsI;
use strict;
use warnings;
my $input = new Bio::TreeIO(-file   => "tree.newick", -format => "newick");
my $tree = $input->next_tree;
my $bool = $tree-> remove_Node('B');
print $bool;

output: 1;

So the remove_Node is working, but the problem is when I open the tree file I find the node is still present!... What is wrong with the code ? how to remove the node from the tree??

Thanks in advance.

perl phylogenetics bioperl tree • 5.6k views
ADD COMMENTlink modified 6.8 years ago by SES8.4k • written 7.1 years ago by Maria170
7
gravatar for Leszek
7.1 years ago by
Leszek4.0k
IIMCB, Poland
Leszek4.0k wrote:

for python && ETE fans:

from ete2 import Tree
t=Tree("(((A:5,B:5)90:2,C:4)25:3,D:10);")
t.prune(("A","C","D"))
print t
ADD COMMENTlink written 7.1 years ago by Leszek4.0k
1

+1 for Python/ETE - excellent package!

ADD REPLYlink written 7.1 years ago by Steve Moss2.3k

Cool, Sphinx'd version of the BioPython tutorial phylogenetics chapter here http://www.bio-cloud.info/Biopython/en/ch12.html

ADD REPLYlink written 7.1 years ago by Steve Moss2.3k
5
gravatar for Giovanni M Dall'Olio
7.1 years ago by
London, UK
Giovanni M Dall'Olio27k wrote:

You need to save the tree to a file:

my $treeout = Bio::TreeIO->new(-format => 'newick', -file   => ">outputree.nw");
$treeout->write_tree($tree)
ADD COMMENTlink modified 7.1 years ago • written 7.1 years ago by Giovanni M Dall'Olio27k

That's pretty rubbish! Can you not view the updated tree without writing to a new file?

ADD REPLYlink written 7.1 years ago by Steve Moss2.3k
1

I don't think so because $tree is a Bio::Tree::TreeI object. I know in Bio::Phylo you can do something like print $tree->as_string but I'm not sure you can do that with BioPerl tree objects. EDIT: This is only partially true, see my latest answer for a more thorough description of these methods.

ADD REPLYlink modified 6.8 years ago • written 7.1 years ago by SES8.4k
4
gravatar for SES
6.8 years ago by
SES8.4k
Vancouver, BC
SES8.4k wrote:

This question has an accepted answer, but it is not complete. Bio::TreeIO has an as_text method that is available if you upgrade your BioPerl to a recent version, and you may use this method instead of saving to a file.

#!/usr/bin/env perl

use strict;
use warnings;
use Bio::TreeIO;

my $treeio = Bio::TreeIO->new(-fh => \*DATA, -format => 'newick');
my $tree = $treeio->next_tree;
$tree->remove_Node('B');
print $tree->as_text('newick');

__DATA__
(((A:5,B:5)90:2,C:4)25:3,D:10);

Explanation: If we had tried to print $treeio above, instead of calling as_text on the tree, we will see that $treeio is Bio::TreeIO::newick object.

print ref $treeio; # prints Bio::TreeIO::newick

Printing the bare object handle will technically print the object name and a reference showing the underlying data structure, and while you can inspect this, it is usually not very informative unless you know what you are looking at.


Similar to the above, Bio::Phylo has the ability to write the tree string to STDOUT. Here is the equivalent code using Bio::Phylo, which is slightly different.

#!/usr/bin/env perl

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

my $tree = Bio::Phylo::IO->parse( -handle => \*DATA, 
                                  -format => 'newick',
                                  -keep   => ['A','C','D'])->first;

print $tree->to_newick, "\n";

__DATA__
(((A:5,B:5)90:2,C:4)25:3,D:10);

The same applies here about printing with a method versus printing the object itself.

print ref $treeio; # prints Bio::Phylo::Forest::Tree

The actual answer as to why your print statement is printing '1' is that you are trying to assign the modified tree to a variable, but really you are assigning the return value of the method, which is '1' (meaning success). Just call the method on the tree object itself (as in the code above), without assignment, and print or save the tree.

ADD COMMENTlink modified 6.8 years ago • written 6.8 years ago by SES8.4k

How would you flip this example to specifically remove a node?  Something like this?

#!/usr/bin/env perl

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

my $tree = Bio::Phylo::IO->parse( -handle => \*DATA, 
                                  -format => 'newick',
                                  -remove   => ['A'])->first; #####

print $tree->to_newick, "\n";

__DATA__
(((A:5,B:5)90:2,C:4)25:3,D:10);
ADD REPLYlink written 5.9 years ago by Lee Katz3.0k

I guess that would be more expressive and easier to write (also a little redundant), but '-remove' is not an argument that is available to the Bio::Phylo::Parsers::Newick class. So, I think using '-keep' is the way to go.

ADD REPLYlink written 5.9 years ago by SES8.4k
2
gravatar for SES
7.1 years ago by
SES8.4k
Vancouver, BC
SES8.4k wrote:

I would recommend you also consider Bio::Phylo for doing phylogenetic analyses in Perl. For simplicity, I like to do these things in R because you can really see what you are doing. Something like this should work:

library(ape)
phylo <- read.tree("mytree.newick")
drop.tip(phylo, taxon) ## taxon can be a single taxon or a vector of taxon names
ADD COMMENTlink written 7.1 years ago by SES8.4k

+1 for Bio::Phylo mention - much better package for working with trees in Perl!

ADD REPLYlink written 7.1 years ago by Steve Moss2.3k

I tried the bove and R displayed the msg with the taxon removed from the tree.. But I am unable to find the new tree file. do i need to print the new tree again. M using R for the first time. please help

ADD REPLYlink written 3.4 years ago by sansritisinha0
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 840 users visited in the last hour