File: label_nodes.py

package info (click to toggle)
python-ete3 3.1.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,148 kB
  • sloc: python: 52,375; javascript: 12,959; xml: 4,903; ansic: 69; sql: 65; makefile: 26; sh: 7
file content (16 lines) | stat: -rw-r--r-- 593 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from ete3 import Tree
tree = Tree( '(A:1,(B:1,(C:1,D:1):0.5):0.5);' )
# Prints the name of every leaf under the tree root
print("Leaf names:")
for leaf in tree.get_leaves():
    print(leaf.name)
# Label nodes as terminal or internal. If internal, saves also the
# number of leaves that it contains.
print("Labeled tree:")
for node in tree.get_descendants():
    if node.is_leaf():
        node.add_features(ntype="terminal")
    else:
        node.add_features(ntype="internal", size=len(node))
# Gets the extended newick of the tree including new node features
print(tree.write(features=[]))