{"data":{"slug":"github-aipoch-medical-research-skills-scientific-skills-data-analysis-etetoolkit-9c5621203f20","title":"Phylogenetic Tree Analysis and Manipulation with ETE Toolkit","summary":"Parse, manipulate, and analyze phylogenetic trees in Newick\/NHX formats using the ETE toolkit, including duplication\/speciation detection, NCBI taxonomy integration, and visualization of publication-quality figures.","url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-scientific-skills-data-analysis-etetoolkit-9c5621203f20","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-aipoch-medical-research-skills-scientific-skills-data-analysis-etetoolkit-9c5621203f20","markdown_url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-scientific-skills-data-analysis-etetoolkit-9c5621203f20.md","download_url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-scientific-skills-data-analysis-etetoolkit-9c5621203f20\/download","category":{"slug":"data-research","name":"Data & Research"},"editor_note":"Ensure `ete3` version 3.1.0 or higher is installed. For visualization, install PyQt5 and Qt SVG support (e.g., `python3-pyqt5.qtsvg` on Debian\/Ubuntu).","instructions":"---\nname: etetoolkit\ndescription: 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.\nlicense: MIT\nauthor: AIPOCH\n---\n> **Source**: [https:\/\/github.com\/aipoch\/medical-research-skills](https:\/\/github.com\/aipoch\/medical-research-skills)\n\n## When to Use\n\n- **Preprocess phylogenetic trees**: convert formats (Newick\/NHX\/PhyloXML), reroot (midpoint\/outgroup), prune taxa, and resolve polytomies before downstream analyses.\n- **Detect evolutionary events in gene trees**: infer **duplication vs. speciation** events and derive **ortholog\/paralog** relationships for phylogenomics.\n- **Annotate trees with taxonomy**: map species names to **NCBI TaxIDs**, retrieve lineages\/ranks, and build minimal taxonomy topologies connecting a set of taxa.\n- **Generate publication-quality visualizations**: render trees to **PDF\/SVG\/PNG** with custom styles, support-based coloring, and node \u201cfaces\u201d (labels, shapes, heatmaps).\n- **Compare alternative topologies**: quantify differences between trees using **Robinson\u2013Foulds (RF)** distance and partition\/bipartition analysis.\n\n## Key Features\n\n- **Tree I\/O and manipulation**\n  - Read\/write: Newick, NHX, PhyloXML, NeXML\n  - Traversals: preorder, postorder, levelorder\n  - Operations: prune, reroot, collapse, resolve polytomies\n  - Metrics: branch\/topological distances, RF distance\n- **Phylogenetic (gene tree) analysis**\n  - Alignment association (FASTA\/Phylip)\n  - Species name extraction from gene IDs\n  - Duplication\/speciation detection (e.g., species overlap \/ reconciliation-style workflows)\n  - Orthology\/paralogy extraction and gene-family splitting\n- **NCBI taxonomy integration**\n  - Auto-download + local cache of taxonomy DB\n  - TaxID \u2194 scientific name translation\n  - Lineage\/rank retrieval and taxonomy-based topology building\n  - Tree annotation with taxonomic metadata\n- **Visualization**\n  - Rectangular\/circular layouts, GUI exploration\n  - NodeStyle\/TreeStyle customization\n  - Faces (text, shapes, charts\/heatmaps) and layout functions\n  - Export to PDF\/SVG\/PNG\n- **Clustering support**\n  - ClusterTree for dendrograms linked to numeric matrices\n  - Cluster quality metrics (e.g., silhouette, Dunn index)\n  - Heatmap + tree combined views\n\n## Dependencies\n\n- `ete3` (recommended: `>=3.1.0`)\n- Optional GUI\/rendering dependencies (platform-specific):\n  - `PyQt5` (e.g., `>=5.15`)\n  - Qt SVG support (often packaged as `python3-pyqt5.qtsvg` on Debian\/Ubuntu)\n\n## Example Usage\n\nThe following example is designed to be runnable end-to-end (it uses an in-memory Newick string and does not require external files).\n\n```python\n# pip install ete3\n\nfrom ete3 import Tree, TreeStyle, NodeStyle\n\n# 1) Load a tree (Newick)\nnw = \"((A:0.1,B:0.2)90:0.3,(C:0.2,D:0.4)70:0.1);\"\nt = Tree(nw, format=1)\n\n# 2) Basic stats\nprint(\"Leaves:\", len(t))\nprint(\"Total nodes:\", sum(1 for _ in t.traverse()))\n\n# 3) Midpoint rooting\nmid = t.get_midpoint_outgroup()\nt.set_outgroup(mid)\n\n# 4) Prune to taxa of interest (preserve branch lengths)\nt.prune([\"A\", \"C\", \"D\"], preserve_branch_length=True)\n\n# 5) Style nodes (color internal nodes by support)\nts = TreeStyle()\nts.show_leaf_name = True\nts.show_branch_support = True\n\nfor n in t.traverse():\n    st = NodeStyle()\n    if n.is_leaf():\n        st[\"fgcolor\"] = \"blue\"\n        st[\"size\"] = 8\n    else:\n        # ETE stores internal support in n.support when present\n        st[\"fgcolor\"] = \"darkgreen\" if getattr(n, \"support\", 0) >= 80 else \"red\"\n        st[\"size\"] = 5\n    n.set_style(st)\n\n# 6) Render (PDF\/SVG\/PNG supported depending on your environment)\nt.render(\"example_tree.pdf\", tree_style=ts)\nprint(\"Wrote: example_tree.pdf\")\n```\n\n## Implementation Details\n\n### Tree parsing formats (Newick \u201cformat\u201d codes)\nETE uses a `format` integer to control how node attributes are interpreted when reading\/writing Newick. Common patterns:\n\n- `format=0`: flexible default (often includes branch lengths)\n- `format=1`: includes internal node names\n- `format=2`: includes support\/bootstrap values\n- `format=5`: internal node names + branch lengths\n- `format=8`: name + distance + support (maximal common usage)\n- `format=9`: leaf names only\n- `format=100`: topology only\n\nExample:\n\n```python\nfrom ete3 import Tree\n\nt = Tree(\"tree.nw\", format=1)\nt.write(outfile=\"out.nw\", format=5)\n```\n\n### NHX feature preservation\nNHX is used to store custom per-node features. When writing, specify which features to serialize:\n\n```python\nt.write(outfile=\"tree.nhx\", features=[\"taxid\", \"habitat\", \"lineage\"])\n```\n\n### Rerooting and pruning behavior\n- **Midpoint rooting** uses `get_midpoint_outgroup()` to select an outgroup that balances path lengths.\n- **Pruning** should typically use `preserve_branch_length=True` to avoid distorting distances in phylogenetic contexts.\n\n### Evolutionary event detection (gene trees)\nFor gene trees, `PhyloTree` supports event labeling on internal nodes (commonly:\n- `evoltype == \"D\"` for duplication\n- `evoltype == \"S\"` for speciation)\n\nA typical workflow is:\n1. Load a gene tree (optionally with an alignment).\n2. Provide a **species naming function** to map gene IDs \u2192 species.\n3. Run descendant event detection.\n4. Extract ortholog groups (speciation subtrees) or query ortholog\/paralog sets from events.\n\n### Tree comparison (Robinson\u2013Foulds)\n`Tree.robinson_foulds(other_tree)` returns:\n- `rf`: RF distance (number of differing bipartitions)\n- `max_rf`: maximum possible RF given shared leaves\n- plus shared leaves and partition sets for deeper inspection\n\nNormalized RF is typically computed as `rf \/ max_rf` (when `max_rf > 0`).","use_cases":["Preprocess phylogenetic trees by converting formats, rerooting, pruning, and resolving polytomies.","Detect duplication vs. speciation events in gene trees for phylogenomics analysis.","Integrate NCBI taxonomy data for taxonomic annotation and lineage-based topology building."],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/aipoch\/medical-research-skills\/blob\/686e09dfdb45d9fa1a08664f06bb93769b3c9d91\/scientific-skills\/Data%20Analysis\/etetoolkit\/SKILL.md","source_commit":"686e09dfdb45d9fa1a08664f06bb93769b3c9d91","license":"MIT","version":null,"listing_approved_at":"2026-11-24","created_at":"2026-11-23T11:26:59.000000Z","updated_at":"2026-11-24T07:14:56.000000Z","seo_title":"Phylogenetic Tree Analysis with ETE Toolkit","seo_description":"Parse, manipulate, and analyze phylogenetic trees using ETE toolkit for Newick\/NHX formats, duplication\/speciation detection, and visualization.","seo_keywords":["phylogenetic tree analysis","ete toolkit usage","duplication speciation detection","ncbi taxonomy integration","tree manipulation tools"]}}