How to collapse leaves with the same name in ETE3 tree?

0

I'm trying to collapse leaves that have the same name in ETE3's tree. I think I almost got it. Two leaves in my example (A. thaliana) have been combined into a single leaf, but not named. I would like it to have the same name as its components.

from ete3 import Tree

def collapsed_leaf(node):
    if len(node2labels[node]) == 1:
       return True
    else:
       return False


t1 = Tree('(("A. thaliana":0.723274,"A. thaliana":0.567784):0.067192,("B":0.279326,"H":0.756049):0.807788);', quoted_node_names=True)
node2labels = t1.get_cached_content(store_attr="name")
print(t1)
#       /-A. thaliana
#    /-|
#   |   -A. thaliana
# --|
#   |   /-B
#    -|
#       -H

t2 = Tree(t1.write(is_leaf_fn=collapsed_leaf, quoted_node_names=True), quoted_node_names=True)
print(t2)
#    /-
# --|
#   |   /-B
#    -|
#       -H


collapse


python


ete3


tree

• 20 views



Source link