This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Ultrametric tree in ete3 (python)

Hi all,

does anyone know how to check if a tree has been correctly converted to ultrametric with ete3 in python? Kinda like the is.ultrametric() function of the R ape package?

Thanks!

phylogeny python

1 answer

try something like this:

from ete3 import Tree

def is_ultrametric(tree):
    last_distance = None
    distance = 0
    for post, node in tree.iter_prepostorder():
        if post:
            distance -= node.dist
        else:
            if not node.is_leaf():
                distance += node.dist
            else:
                if last_distance is None:
                    print(last_distance)
                    last_distance = distance + node.dist
                elif last_distance != distance + node.dist:
                    return False, last_distance
    return True, last_distance

t = Tree('((a:1,(b:0.5, d:0.5):0.5):1,c:2);')
print(is_ultrametric(t))

t = Tree()
t.populate(10, random_branches=True)
print(is_ultrametric(t))

Thanks, so there is not existing method. It would be a good idea to add one I guess.

Log in to answer this question.