collapse tree nodes if leafs does not contain a specific feature
1
0
Entering edit mode
10.4 years ago
Abdullah ▴ 100

Hi,

I have a random tree where I need to show only the interesting parts.

Each node in the tree contain a feature called feat, it only contains a value for some leafs.

What I want to do is:

Draw the tree, and only keep the parts where at least one leaf contain a value in this feature, i.e. collapse all the internal nodes where there are no leafs containing a value inside the feature feat.

I tried to use the collapsing functions used in the ETE2 documentation, but they don't seem to work well and they produce a non-complete newick tree which is not readable by the ete2 module (or maybe I did something wrong).

Can someone tell me how to do that?

python ete2 • 2.7k views
ADD COMMENT
1
Entering edit mode
10.4 years ago
jhc ★ 3.0k

I do similar things with ETE very often. Example:

from ete2 import Tree, TreeStyle
import random

def collapse_layout(node):
    # if none of the leaves in a given partition have at least a feat attribute
    # set to 1, collapse the node
    if not node.is_leaf() and 1 not in NODE2FEAT_CONTENT[node]:
        node.img_style["draw_descendants"] = False
        node.img_style["size"] = 20
        node.name = "Collapsed leaves"

t = Tree()
# generate a random tree and feat values
t.populate(100)
for leaf in t:
    leaf.add_features(feat=random.randint(0, 1))

# a cached dictionary informing about the feat attribute content for all nodes
NODE2FEAT_CONTENT = t.get_cached_content(store_attr="feat")

ts = TreeStyle()
ts.layout_fn = collapse_layout
t.show(tree_style=ts)
ADD COMMENT

Login before adding your answer.

Traffic: 2279 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