Question: collapse tree nodes if leafs does not contain a specific feature
gravatar for Abdullah
5.8 years ago by
Abdullah100 wrote:


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?




ete2 python • 1.6k views
ADD COMMENTlink modified 5.7 years ago by jhc2.8k • written 5.8 years ago by Abdullah100
gravatar for jhc
5.7 years ago by
jhc2.8k wrote:

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 = "Collapsed leaves"

t = Tree()
# generate a random tree and feat values
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
ADD COMMENTlink written 5.7 years ago by jhc2.8k
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1138 users visited in the last hour