1from ete3 import Tree
2
3# Loads a tree with branch lenght information. Note that if no
4# distance info is provided in the newick, it will be initialized with
5# the default dist value = 1.0
6nw = """(((A:0.1, B:0.01):0.001, C:0.0001):1.0,
7(((((D:0.00001,I:0):0,F:0):0,G:0):0,H:0):0,
8E:0.000001):0.0000001):2.0;"""
9t = Tree(nw)
10print t
11#                              /-A
12#                    /--------|
13#          /--------|          \-B
14#         |         |
15#         |          \-C
16#         |
17#         |                                                  /-D
18#         |                                        /--------|
19#---------|                              /--------|          \-I
20#         |                             |         |
21#         |                    /--------|          \-F
22#         |                   |         |
23#         |          /--------|          \-G
24#         |         |         |
25#          \--------|          \-H
26#                   |
27#                    \-E
28#
29# Locate some nodes
30A = t&"A"
31C = t&"C"
32# Calculate distance from current node
33print "The distance between A and C is",  A.get_distance("C")
34# Calculate distance between two descendants of current node
35print "The distance between A and C is",  t.get_distance("A","C")
36# Calculate the toplogical distance (number of nodes in between)
37print "The number of nodes between A and D is ",  \
38    t.get_distance("A","D", topology_only=True)
39# Calculate the farthest node from E within the whole structure
40farthest, dist = (t&"E").get_farthest_node()
41print "The farthest node from E is", farthest.name, "with dist=", dist
42# Calculate the farthest node from E within the whole structure,
43# regarding the number of nodes in between as distance value
44# Note that the result is differnt.
45farthest, dist = (t&"E").get_farthest_node(topology_only=True)
46print "The farthest (topologically) node from E is", \
47    farthest.name, "with", dist, "nodes in between"
48# Calculate farthest node from an internal node
49farthest, dist = t.get_farthest_node()
50print "The farthest node from root is", farthest.name, "with dist=", dist
51#
52# The program results in the following information:
53#
54# The distance between A and C is 0.1011
55# The distance between A and C is 0.1011
56# The number of nodes between A and D is  8.0
57# The farthest node from E is A with dist= 1.1010011
58# The farthest (topologically) node from E is I with 5.0 nodes in between
59# The farthest node from root is A with dist= 1.101
60