1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
from ete3 import Tree
t = Tree( '((H:0.3,I:0.1):0.5, A:1, (B:0.4,(C:1,D:1):0.5):0.5);' )
# Create a small function to filter your nodes
def conditional_function(node):
if node.dist > 0.3:
return True
else:
return False
# Use previous function to find matches. Note that we use the traverse
# method in the filter function. This will iterate over all nodes to
# assess if they meet our custom conditions and will return a list of
# matches.
matches = list(filter(conditional_function, t.traverse()))
print(len(matches), "nodes have distance >0.3")
# depending on the complexity of your conditions you can do the same
# in just one line with the help of lambda functions:
matches = [n for n in t.traverse() if n.dist>0.3 and n.is_leaf()]
print(len(matches), "nodes have distance >0.3 and are leaves")
|