$npx -y skills add K-Dense-AI/scientific-agent-skills --skill etetoolkitPhylogenetic tree toolkit (ETE). Tree manipulation (Newick/NHX), evolutionary event detection, orthology/paralogy, NCBI taxonomy, visualization (PDF/SVG), for phylogenomics.
| 1 | # ETE Toolkit Skill |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | ETE (Environment for Tree Exploration) is a toolkit for phylogenetic and hierarchical tree analysis. Manipulate trees, analyze evolutionary events, visualize results, and integrate with biological databases for phylogenomic research and clustering analysis. |
| 6 | |
| 7 | ## Core Capabilities |
| 8 | |
| 9 | ### 1. Tree Manipulation and Analysis |
| 10 | |
| 11 | Load, manipulate, and analyze hierarchical tree structures with support for: |
| 12 | |
| 13 | - **Tree I/O**: Read and write Newick, NHX, PhyloXML, and NeXML formats |
| 14 | - **Tree traversal**: Navigate trees using preorder, postorder, or levelorder strategies |
| 15 | - **Topology modification**: Prune, root, collapse nodes, resolve polytomies |
| 16 | - **Distance calculations**: Compute branch lengths and topological distances between nodes |
| 17 | - **Tree comparison**: Calculate Robinson-Foulds distances and identify topological differences |
| 18 | |
| 19 | **Common patterns:** |
| 20 | |
| 21 | ```python |
| 22 | from ete3 import Tree |
| 23 | |
| 24 | # Load tree from file |
| 25 | tree = Tree("tree.nw", format=1) |
| 26 | |
| 27 | # Basic statistics |
| 28 | print(f"Leaves: {len(tree)}") |
| 29 | print(f"Total nodes: {len(list(tree.traverse()))}") |
| 30 | |
| 31 | # Prune to taxa of interest |
| 32 | taxa_to_keep = ["species1", "species2", "species3"] |
| 33 | tree.prune(taxa_to_keep, preserve_branch_length=True) |
| 34 | |
| 35 | # Midpoint root |
| 36 | midpoint = tree.get_midpoint_outgroup() |
| 37 | tree.set_outgroup(midpoint) |
| 38 | |
| 39 | # Save modified tree |
| 40 | tree.write(outfile="rooted_tree.nw") |
| 41 | ``` |
| 42 | |
| 43 | Use `scripts/tree_operations.py` for command-line tree manipulation: |
| 44 | |
| 45 | ```bash |
| 46 | # Display tree statistics |
| 47 | python scripts/tree_operations.py stats tree.nw |
| 48 | |
| 49 | # Convert format |
| 50 | python scripts/tree_operations.py convert tree.nw output.nw --in-format 0 --out-format 1 |
| 51 | |
| 52 | # Reroot tree |
| 53 | python scripts/tree_operations.py reroot tree.nw rooted.nw --midpoint |
| 54 | |
| 55 | # Prune to specific taxa |
| 56 | python scripts/tree_operations.py prune tree.nw pruned.nw --keep-taxa "sp1,sp2,sp3" |
| 57 | |
| 58 | # Show ASCII visualization |
| 59 | python scripts/tree_operations.py ascii tree.nw |
| 60 | ``` |
| 61 | |
| 62 | ### 2. Phylogenetic Analysis |
| 63 | |
| 64 | Analyze gene trees with evolutionary event detection: |
| 65 | |
| 66 | - **Sequence alignment integration**: Link trees to multiple sequence alignments (FASTA, Phylip) |
| 67 | - **Species naming**: Automatic or custom species extraction from gene names |
| 68 | - **Evolutionary events**: Detect duplication and speciation events using Species Overlap or tree reconciliation |
| 69 | - **Orthology detection**: Identify orthologs and paralogs based on evolutionary events |
| 70 | - **Gene family analysis**: Split trees by duplications, collapse lineage-specific expansions |
| 71 | |
| 72 | **Workflow for gene tree analysis:** |
| 73 | |
| 74 | ```python |
| 75 | from ete3 import PhyloTree |
| 76 | |
| 77 | # Load gene tree with alignment |
| 78 | tree = PhyloTree("gene_tree.nw", alignment="alignment.fasta") |
| 79 | |
| 80 | # Set species naming function |
| 81 | def get_species(gene_name): |
| 82 | return gene_name.split("_")[0] |
| 83 | |
| 84 | tree.set_species_naming_function(get_species) |
| 85 | |
| 86 | # Detect evolutionary events |
| 87 | events = tree.get_descendant_evol_events() |
| 88 | |
| 89 | # Analyze events |
| 90 | for node in tree.traverse(): |
| 91 | if hasattr(node, "evoltype"): |
| 92 | if node.evoltype == "D": |
| 93 | print(f"Duplication at {node.name}") |
| 94 | elif node.evoltype == "S": |
| 95 | print(f"Speciation at {node.name}") |
| 96 | |
| 97 | # Extract ortholog groups |
| 98 | ortho_groups = tree.get_speciation_trees() |
| 99 | for i, ortho_tree in enumerate(ortho_groups): |
| 100 | ortho_tree.write(outfile=f"ortholog_group_{i}.nw") |
| 101 | ``` |
| 102 | |
| 103 | **Finding orthologs and paralogs:** |
| 104 | |
| 105 | ```python |
| 106 | # Find orthologs to query gene |
| 107 | query = tree & "species1_gene1" |
| 108 | |
| 109 | orthologs = [] |
| 110 | paralogs = [] |
| 111 | |
| 112 | for event in events: |
| 113 | if query in event.in_seqs: |
| 114 | if event.etype == "S": |
| 115 | orthologs.extend([s for s in event.out_seqs if s != query]) |
| 116 | elif event.etype == "D": |
| 117 | paralogs.extend([s for s in event.out_seqs if s != query]) |
| 118 | ``` |
| 119 | |
| 120 | ### 3. NCBI Taxonomy Integration |
| 121 | |
| 122 | Integrate taxonomic information from NCBI Taxonomy database: |
| 123 | |
| 124 | - **Database access**: Automatic download and local caching of NCBI taxonomy (~300MB) |
| 125 | - **Taxid/name translation**: Convert between taxonomic IDs and scientific names |
| 126 | - **Lineage retrieval**: Get complete evolutionary lineages |
| 127 | - **Taxonomy trees**: Build species trees connecting specified taxa |
| 128 | - **Tree annotation**: Automatically annotate trees with taxonomic information |
| 129 | |
| 130 | **Building taxonomy-based trees:** |
| 131 | |
| 132 | ```python |
| 133 | from ete3 import NCBITaxa |
| 134 | |
| 135 | ncbi = NCBITaxa() |
| 136 | |
| 137 | # Build tree from species names |
| 138 | species = ["Homo sapiens", "Pan troglodytes", "Mus musculus"] |
| 139 | name2taxid = ncbi.get_name_translator(species) |
| 140 | taxids = [name2taxid[sp][0] for sp in species] |
| 141 | |
| 142 | # Get minimal tree connecting taxa |
| 143 | tree = ncbi.get_topology(taxids) |
| 144 | |
| 145 | # Annotate nodes with taxonomy info |
| 146 | for node in tree.traverse(): |
| 147 | if hasattr(node, "sci_name"): |
| 148 | print(f"{node.sci_name} - Rank: {node.rank} - TaxID: {node.taxid}") |
| 149 | ``` |
| 150 | |
| 151 | **Annotating existing trees:** |
| 152 | |
| 153 | ```python |
| 154 | # |