Entering edit mode
3.4 years ago
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
Thank you, Kevin. This is exactly what I was looking for. You saved my life :)
Hi, I have tried to adapt this to collapse a tree based on the first string in leaf labels (being a genus name), but it is not being collapsed or renamed. Can you suggest what the problem might be?
Thanks for any help.
Ok, think I solved it - had to also rename the leaves: