Most closely related leaves in a tree (ete3 package)

87 Views Asked by At

Hello I have a tree such as :

>>> print(tree)

   /-A
--|
  |   /-B
   \-|
     |   /-C
      \-|
        |   /-D
         \-|
            \-E

 tree=Tree("(A,(B,C,(D,E)));") (ete3 function)

And I'm looking for a way to see the closest leaves to a particular leaf.

Here for instance the leaves most closely related to C are D and E. The leaf most closely related to D is E The the leaves most closely related to B are C, D and E.

1

There are 1 best solutions below

0
On

the definition of "closest" is tricky in this context, but what you describe can easily be achieved by the following code (note that the tree in your code was missing a parenthesis):

In [1]: from ete3 import Tree
   ...:
   ...: tree=Tree("(A,(B,(C,(D,E))));")
   ...: c_node  = tree & 'C'
   ...: for sister_node in c_node.get_sisters(): # there might be multifurcations therefore the loop
   ...:     print(sister_node.get_leaf_names())
   ...:
['D', 'E']