1

I would need to get the node number within a tree in ete3.

Here is an example of a tree:

rooted_tree = Tree( "((A,B),(C,D));" )

print rooted_tree
#
#          /-A
#     /---|
#    |     \-B
#----|
#    |     /-C
#     \---|
#          \-D

then I calculate some stuff from this tree (not important for the question), and this stuff gives me values for each node in the tree, which then I plot in ggplot tree. But ggplot tree need the node number to plot this information, the problem here is that I cannot manage to find the code in order to get the node number of the tree "rooted_tree"

Let say I want the node number of the ancestor of A and B , how can I get it ? I only know to do that:

ancestor = tree.get_common_ancestor("A","B")

but something like ancestor.num of ancestor.node_number does not work...

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
chippycentra
  • 3,396
  • 1
  • 6
  • 24

1 Answers1

2

all the nodes in ete tree are objects, they don't have a number but they have a hash ID labeled for topological perspect

you can access those topology id by

ancestor = tree.get_common_ancestor("A","B")
print(ancestor.get_topology_id())

quote from ete3 documentation:

get_topology_id(attr='name') New in version 2.3.

Returns the unique ID representing the topology of the current tree. Two trees with the same topology will produce the same id. If trees are unrooted, make sure that the root node is not binary or use the tree.unroot() function before generating the topology id.

This is useful to detect the number of unique topologies over a bunch of trees, without requiring full distance methods.

The id is, by default, calculated based on the terminal node’s names. Any other node attribute could be used instead.

further reference: http://etetoolkit.org/docs/latest/reference/reference_tree.html#get_topology_id

ziqi deng
  • 46
  • 2