Etetoolkit

From aipoch/medical-research-skills · View interactive version

Etetoolkit is an AI agent skill from the GitHub repository aipoch/medical-research-skills. ETE (Environment for Tree Exploration) toolkit for phylogenetic and hierarchical tree analysis; use it when you need to parse/manipulate Newick/NHX trees, detect duplication/speciation events, integrate NCBI taxonomy, and render publication-quality figures.

How to use this skill

Skill file

Source: https://github.com/aipoch/medical-research-skills

When to Use

Key Features

Dependencies

Example Usage

The following example is designed to be runnable end-to-end (it uses an in-memory Newick string and does not require external files).

# pip install ete3

from ete3 import Tree, TreeStyle, NodeStyle

# 1) Load a tree (Newick)
nw = "((A:0.1,B:0.2)90:0.3,(C:0.2,D:0.4)70:0.1);"
t = Tree(nw, format=1)

# 2) Basic stats
print("Leaves:", len(t))
print("Total nodes:", sum(1 for _ in t.traverse()))

# 3) Midpoint rooting
mid = t.get_midpoint_outgroup()
t.set_outgroup(mid)

# 4) Prune to taxa of interest (preserve branch lengths)
t.prune(["A", "C", "D"], preserve_branch_length=True)

# 5) Style nodes (color internal nodes by support)
ts = TreeStyle()
ts.show_leaf_name = True
ts.show_branch_support = True

for n in t.traverse():
    st = NodeStyle()
    if n.is_leaf():
        st["fgcolor"] = "blue"
        st["size"] = 8
    else:
        # ETE stores internal support in n.support when present
        st["fgcolor"] = "darkgreen" if getattr(n, "support", 0) >= 80 else "red"
        st["size"] = 5
    n.set_style(st)

# 6) Render (PDF/SVG/PNG supported depending on your environment)
t.render("example_tree.pdf", tree_style=ts)
print("Wrote: example_tree.pdf")

Implementation Details

Tree parsing formats (Newick “format” codes)

ETE uses a format integer to control how node attributes are interpreted when reading/writing Newick. Common patterns:

Example:

from ete3 import Tree

t = Tree("tree.nw", format=1)
t.write(outfile="out.nw", format=5)

NHX feature preservation

NHX is used to store custom per-node features. When writing, specify which features to serialize:

t.write(outfile="tree.nhx", features=["taxid", "habitat", "lineage"])

Rerooting and pruning behavior

Evolutionary event detection (gene trees)

For gene trees, PhyloTree supports event labeling on internal nodes (commonly:

A typical workflow is:

  1. Load a gene tree (optionally with an alignment).
  2. Provide a species naming function to map gene IDs → species.
  3. Run descendant event detection.
  4. Extract ortholog groups (speciation subtrees) or query ortholog/paralog sets from events.

Tree comparison (Robinson–Foulds)

Tree.robinson_foulds(other_tree) returns:

Normalized RF is typically computed as rf / max_rf (when max_rf > 0).

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.