Ultrametric tree in ete3 (python)
1
1
Entering edit mode
6.9 years ago

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 • 2.8k views
ADD COMMENT
6
Entering edit mode
6.9 years ago
jhc ★ 3.0k

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))
ADD COMMENT
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 2681 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6