I just want to be able to input a tree to a program and have it output the same tree but with numbers written on the internal branches, so that I can identify them easily. e.g. branch 1, 2 etc.
Thanks, John
I just want to be able to input a tree to a program and have it output the same tree but with numbers written on the internal branches, so that I can identify them easily. e.g. branch 1, 2 etc.
Thanks, John
The ETE package (Python language) allows for tree node annotation using the extended newick format.
from ete2 import Tree
tree = Tree('(((A,B),C),D);')
edge = 0
for node in tree.traverse():
if not node.is_leaf():
node.name = "NODE_%d" %edge
edge += 1
print tree.write(format=8)
#
# (((A,B)NODE_2,C)NODE_1,D);
#
Or, if you want to add more information to the nodes:
from ete2 import Tree
tree = Tree('(((A,B),C),D);')
edge = 0
for node in tree.traverse():
if not node.is_leaf():
node.add_feature("label", "NODE_%d" %edge )
node.add_feature("confidence", 1.0 )
edge += 1
print tree.write(format=9, features=["label", "confidence"])
#
# (((A,B)[&&NHX:label=node_2:confidence=1.0],C)[&&NHX:label=node_1:confidence=1.0],D)[&&NHX:label=node_0:confidence=1.0];
#
Newick 'format' allows this: http://en.wikipedia.org/wiki/Newick_format See also: http://evolution.genetics.washington.edu/phylip/newicktree.html
For example:
((raccoon:19.19959,bear:6.80041)InnerNode1:0.84600,((sea_lion:11.99700, seal:12.00300)InnerNode2:7.52973,((monkey:100.85930,cat:47.14069):20.59201, weasel:18.87953):2.09460):3.87382,dog:25.46154);
There was a similar question about a tree-viewer here and I tested with the above tree with Epos and it worked.
Er, yeah that's true! I didn't see the difference between 'branches' (edges would be the correct term and should be used instead of branches), inner nodes, and leaves. That was a bit confusing. However, it doesn't really matter, I think we should better label the inner nodes instead of the edges, because the edges are only implicitly modeled by most formats. I used Epos
This can be done easily using Bio::Phylo:
#!/usr/bin/perl
use strict;
use Bio::Phylo::IO 'parse';
my $newick = '(((A,B),C),D);';
my $tree = parse( '-format' => 'newick', '-string' => $newick )->first;
my $counter = 1;
$tree->visit(
sub {
my $node = shift;
$node->set_name( $counter++ ) if $node->is_internal;
}
);
print $tree->to_newick( '-nodelabels' => 1 );
Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
the idea of labeling edges is not exactly a phylogenetic need, but a descriptive aid for displaying trees with lots of information in it. it would be really useful for me if I could write comments on certain edges of a tree we use to give as reference, since there's no automatic way of doing so (right now we are forced to export the tree as an image and then manually edit such image to write notes on it).
Newick allows node labels. If you want edge labels, i.e. separate from node labels, you can use NeXML. Here is an example file with both node labels and edge labels.
Which file format do you use?
I haven't found any format that allows this, Lars. in fact I've discussed this on a similar post, stating that the only thing I found related to branch labelling was storing confidence values in phyloxml format, although this is not perfect (confidence values are limited to double values, but strings are not allowed). any light on this matter will definitely be of great help to me too.
branches is confusing, should use 'nodes' and 'edges' instead, so you want to put a label on the edges? Why not just label the nodes, that would result in the same outcome because in a tree, the edges are often only modeled implicitly.