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.
+1 for Python/ETE - excellent package!
Cool, Sphinx'd version of the BioPython tutorial phylogenetics chapter here http://www.bio-cloud.info/Biopython/en/ch12.html