/genome-annotation:tree-formattingfrom genome-annotation
Visualizes and formats phylogenetic trees using ggtree (R) or iTOL (web). Handles layout, branch coloring, clade collapsing, support values, and overlays.
Ships in genome-annotation. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Conventions for rendering phylogenetic trees using ggtree (R/Bioconductor) or iTOL (Interactive Tree of Life, web-based).
Ask the user which backend to use based on their needs:
| Backend | Best for | Output | Language |
|---|---|---|---|
| ggtree | Publication figures, full programmatic control, offline use | PDF/PNG/SVG | R (.qmd script) |
| iTOL | Interactive exploration, quick iteration, web sharing, UI tweaking | Web + PDF/SVG/PNG exports | R (.qmd annotations) + Python (.qmd upload) |
| Feature | ggtree | iTOL |
|---|---|---|
| Interactive exploration | No | Yes (web UI) |
| Label alignment control | Full (programmatic) | Limited (UI toggle only, not via API) |
| Collapse triangle labels | Manual geom_text() | Built-in LABELS for internal nodes |
| Circular label positioning | Complex (manual angle computation) | Automatic |
| Branch length display | Yes (phylogram/cladogram toggle) | Yes (via UI) |
| Offline/reproducible | Fully offline | Requires iTOL API + internet |
| Two-script workflow | No (single .qmd) | Yes (R .qmd annotations + Python .qmd upload) |
Before rendering, validate the tree file. Tree-formatting scripts should include a validation step early on.
ape::read.tree() (R) or ete3.Tree() (Python)ape::is.rooted())ape::is.binary())| characters (breaks iTOL), spaces, unusual characters#| label: validate-tree
library(ape)
tree_path <- here("data/phylogenetics/tree.treefile")
stopifnot("Tree file not found" = file.exists(tree_path))
tree <- read.tree(tree_path)
cat("Tips:", Ntip(tree), "\n")
cat("Rooted:", is.rooted(tree), "\n")
cat("Binary:", is.binary(tree), "\n")
# Zero-length branches
if (!is.null(tree$edge.length)) {
n_zero <- sum(tree$edge.length == 0)
if (n_zero > 0) cat("WARNING:", n_zero, "zero-length branches\n")
}
# Tip label issues (pipe breaks iTOL)
has_pipe <- grepl("\\|", tree$tip.label)
if (any(has_pipe)) {
cat("WARNING:", sum(has_pipe), "tips contain '|' — will break iTOL annotations\n")
}
#| label: validate-tree
from ete3 import Tree
tree_path = PROJECT_ROOT / "data/phylogenetics/tree.treefile"
assert tree_path.exists(), f"Tree file not found: {tree_path}"
tree = Tree(str(tree_path))
tips = tree.get_leaf_names()
print(f"Tips: {len(tips)}")
# Check for pipe characters
pipe_tips = [t for t in tips if "|" in t]
if pipe_tips:
print(f"WARNING: {len(pipe_tips)} tips contain '|' — must relabel before iTOL")
Help the user select the right visualization. Ask about purpose and tree size, then recommend from the options below.
| Type | Best for | Tips | Key features |
|---|---|---|---|
| Collapsed rectangular phylogram | Large family trees; showing branch-length variation and gene family structure | 250-2000+ | Collapsed pure clades, branch lengths, selective labels |
| Collapsed rectangular cladogram | Large family trees; topology focus, cleaner labels | 250-2000+ | Same as phylogram but no branch lengths, narrower page |
| Collapsed circular | Large trees; compact overview showing overall structure | 250-2000+ | Circular layout, collapsed clades, optional selective labels |
| Simple rectangular phylogram | Small-medium trees where all tips are readable | < 250 | All tips labeled, no collapsing needed |
| Unrooted | Networks, showing relationships without root assumption | Any | No directionality implied |
Gather these decisions before writing any code:
collapse_groups parameter controls which
taxonomic groups are eligible. Common choices:
c("Bilateria") — only collapse bilaterians (keeps sponges/cnidarians expanded)c("Bilateria", "Protostomia", "Deuterostomia") — collapse specific groupsNULL — all groups eligible for collapsing"Bilateria (36 tips: LAMA1, LAMA2, LAMB1)". This ensures key gene family
members remain visible even when the clade is collapsed.sp| entries. Entry mnemonics are not gene symbols. Reuse a
verified mapping or see gene-lookup for lookup procedures. Set gene_map_file
to the separate lookup output; if absent or names are missing, retain accessions
with diagnostics. Preserve each full tip ID even when gene names repeat.ITOL_PROJECT. Review files and
visibility before upload; a missing key never chooses an anonymous destination.Resolve template paths below relative to this installed skill directory.
All ggtree templates are Quarto .qmd documents following the project's data
science conventions (declared inputs, committed producing source, PNG/PDF/SVG output and
BUILD_INFO.txt).
Reference template: templates/ggtree/collapsed_rectangular.qmd
This template is a complete, runnable .qmd with all tuned style parameters. Copy it
into the project's scripts/ directory and adapt the sections marked PROJECT-SPECIFIC:
collapse_groups parameter (which taxonomic groups to collapse)The template handles: tree loading, midpoint rooting, pure-clade collapsing by taxonomic group, branch coloring by taxonomy, all visible tips labeled, model species gene names on collapsed triangle labels, formula-based page sizing, and PDF output.
Key features:
INCHES_PER_TIP = 0.12, height = max(8, n_visible * INCHES_PER_TIP)collapse_groups parameter — controls which taxonomic groups are eligible for
collapsing (e.g., c("Bilateria") to only collapse bilaterians, or NULL for all)"Group (N tips: GENE1, GENE2, ...)" so key gene family members remain visiblemax(pre_data$x[tip_ids]) (triangle tip),
not at internal node x (triangle base)Reference template: templates/ggtree/collapsed_circular.qmd
Same structure as rectangular — adapt PROJECT-SPECIFIC sections. Produces:
Critical circular gotcha: Labels must be positioned BEFORE collapse() is called.
The template handles this by computing angles from y-position (y / max_y * 360),
flipping text on the left half of the circle, and using geom_text() with explicit
angle/hjust values instead of geom_tiplab2().
For simple rectangular or unrooted trees, no template exists yet. Build from ggtree basics:
# Simple rectangular (all tips labeled)
p <- ggtree(tree) + geom_tiplab(size = 2)
# Unrooted
p <- ggtree(tree, layout = "unrooted")
All style parameters are defined as named constants at the top of each template
(e.g., BRANCH_LINE_WIDTH, LABEL_SIZE, INCHES_PER_TIP). Do not scatter
magic numbers through the code.
iTOL requires separate R and Python steps (do not mix in one .qmd):
Reference template: templates/itol/annotations.R
Copy into project and adapt PROJECT-SPECIFIC sections. Generates these files:
GENE.tree — relabeled Newick (short display labels, no | characters)GENE_tip_map.tsv — every full original tip ID, accession and unique display labelGENE_branch_colors.txt — TREE_COLORS with clade + branch entriesGENE_label_colors.txt — TREE_COLORS label color entriesGENE_collapse.txt — COLLAPSE entries for pure cladesGENE_collapse_labels.txt — LABELS for collapsed clade internal nodesReference template: templates/itol/upload_export.py
Uploads two versions:
Exports multiple layout/format combinations (circular PDF/SVG/PNG, rectangular PDF/SVG, unrooted PDF).
Run the upload template without --upload first to preview the exact tree/annotation
files, both intended tree names and destination. Verify the destination project and
its sharing/visibility settings in iTOL; do not assume a key makes data private.
Obtain the user's approval of those files, destination and visibility before running
with --upload. For an anonymous destination, preview with --anonymous and use
--anonymous --upload only after approval. That explicit choice ignores any account
key and project in the environment. Without a key or --anonymous, preview reports
an unselected destination and --upload refuses before client loading or writes.
The script does not verify service visibility, sharing or retention. A general
request to format a tree does not authorize an upload.
Keep the API key in ITOL_API_KEY; never embed or print its value.
ITOL_API_KEY env varITOL_PROJECT env var (default: "misc"). The project must
already exist — the iTOL API cannot create projects, only the web UI can
(My Trees > New Project). Prompt the user to create it if needed.After rendering the upload script, always read the BUILD_INFO.txt and report the iTOL URLs back to the user in chat. These clickable links are essential for quick iteration. Format:
**Uncollapsed:** http://itol.embl.de/external.cgi?tree=TREE_ID&restore_saved=1
**Collapsed:** http://itol.embl.de/external.cgi?tree=TREE_ID&restore_saved=1
Tip label formats vary substantially depending on data source. Do not assume a fixed format. Inspect the actual tip labels first, then write parsing functions tailored to what's present.
| Source | Example | Species part | ID part |
|---|---|---|---|
| UniProt (sp) | sp|O95631|NET1_HUMAN | Suffix: HUMAN | Accession: O95631; actual gene: NTN1 |
| UniProt (tr) | tr|Q23158|Q23158_CAEEL | Suffix: CAEEL | Accession: Q23158 |
| Species|taxid.acc | Mus_musculus|10090.Q9R1A3 | Before | | After taxid. |
| Species|acc | Nematostella|XP_032238380.2 | Before | | After | |
| BLAST-annotated | Hydra|8692.t25743aep_EHBP1_HUMAN_... | Before | | Transcript ID only |
sp| and tr| entries.
Never derive a gene from the entry mnemonic; retain the accession when unresolved.G._species_GENE_OR_ID (e.g., H._sapiens_SPTB1,
E._muelleri_Em0014g869a)| Taxonomic Group | Hex |
|---|---|
| Demosponges | #2ca02c |
| Calcarea + Homoscleromorpha | #98df8a |
| Ctenophora | #9467bd |
| Cnidaria + Placozoa | #ff7f0e |
| Deuterostomia | #d62728 |
| Protostomia | #1f77b4 |
| Non-metazoan eukaryotes | #555555 |
| Unclassified (unmatched/missing taxonomy) | #8c564b |
| Mixed (internal nodes) | #999999 |
Use non-metazoan categories only for explicitly mapped taxa. An unmatched species, or a blank/missing taxonomic assignment, is Unclassified. Keep all tips and report the count with representative full IDs. The templates give this class its own color; existing ggtree legends include it when present. Update project taxonomy from evidence before interpreting group membership or collapsed clades.
Species that are commonly misclassified:
| Species | Correct group | Notes |
|---|---|---|
| Thelohanellus_kitauei | Cnidaria + Placozoa | Myxozoan = cnidarian |
| Meara_stichopi, Waminoa | Deuterostomia | Xenacoelomorpha |
| Spadella_cephaloptera | Protostomia | Chaetognath |
| Monosiga, Salpingoeca | Non-metazoan | Choanoflagellates |
These are hard-won lessons — do not skip:
Pre-compute label positions BEFORE collapse() — collapse modifies p$data
coordinates. Extract x/y from p$data first. This applies to BOTH rectangular
and circular layouts.
Match on node column, not row index — p$data rows may not be ordered by
node ID. Always use match(tip_node_ids, pre_data$node).
Collapse label x-position: use max(pre_data$x[tip_ids]), NOT node x —
The internal node sits at the base of the collapsed triangle, but the label
should appear at the triangle tip (where descendant tips extend to). Using the
node x places labels at the triangle base, which looks wrong.
Never cap branch lengths — Branch lengths represent real evolutionary distances. Capping or truncating them is data manipulation. If long branches compress internal structure, offer a cladogram as the honest alternative.
Circular labels: use geom_text() with manual angles, NOT geom_tiplab2() —
compute angles as y / max_y * 360, flip text on left half (angles > 90 & < 270),
and pass angle/hjust outside aes().
show.legend = FALSE on geom_text — prevents "a" character artifacts
appearing in the color legend.
branch.length = "none" for cladogram — cannot pass NULL. Must use
if/else to conditionally include this argument.
coord_cartesian(clip = "off") — required for rectangular labels that extend
beyond the plot area. Pair with wide right margin. Not needed for circular.
Daylight layout — produces unusable output for large trees (branches crossing, triangles overlapping). Avoid it.
Page sizing formula — Use INCHES_PER_TIP = 0.12 with
PAGE_HEIGHT = max(8, n_visible * INCHES_PER_TIP) where n_visible counts
non-collapsed tips plus collapsed triangles. This formula keeps labels readable
without excess whitespace. Hardcoded page sizes invariably need adjustment.
Never collapse by gene family — Unless eggNOG orthogroup data is available to intelligently define ortholog groups, only collapse by taxonomic group. Gene families within a tree are the object of study, not noise to be hidden.
Accession filtering for collapse labels — When building collapse triangle
labels from tip names, use an is_gene_symbol() helper that excludes UniProt
accession patterns (A0A..., P12345, Q-prefixed, etc.). Only sp| Swiss-Prot
entries produce real gene symbols; tr| TrEMBL entries produce accessions that
are not informative as labels. Filter these out so collapsed triangles show
gene names, not accession numbers.
Hard-won lessons from iTOL annotation file development:
Tip labels must NOT contain | characters — iTOL uses | as the MRCA
separator in TREE_COLORS clade entries (tipA|tipB clade ...), COLLAPSE entries,
and LABELS internal node entries. If tip labels contain |, all clade/collapse
specifications silently break (wrong MRCA selected, or entries ignored entirely).
Solution: relabel tips to short display names before writing the Newick tree.
ape::write.tree() converts spaces to underscores — display labels must use
underscores from the start (H._sapiens_SPTN2 not H. sapiens SPTN2), or
annotation file IDs will not match the tree.
itol.toolkit R package is incompatible with | in tip labels — the toolkit
also uses | internally and cannot escape it. Write annotation files manually
(plain text with TAB separator) instead of using itol.toolkit.
MRCA pair selection: to specify an internal node, provide one tip from each
child subtree (tipA|tipB). Using tips[1] and tips[N] (first/last by array
index) can give two tips from the same child, which specifies a different MRCA.
Label alignment is NOT controllable via batch export API — the "Align tip
labels" toggle is UI-only. The label_display export parameter controls
visibility (0=hide, 1=show) but not alignment. Users must toggle alignment
manually in the iTOL web interface.
Collapsed triangle labels — use LABELS annotation type with MRCA specification
(tipA|tipB\tLabel text). These render as the displayed name on collapsed
triangles.
Two uploads for collapsed vs uncollapsed — upload annotation files are baked into the tree on upload. To have both an uncollapsed and collapsed version, upload twice: once without collapse files, once with all files.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
phylogenetic-tree, tree-visualization, ggtree, itol, bioinformatics
5 skills in this plugin
/buscoRun BUSCO completeness assessment on genomes, transcriptomes, or predicted proteomes. Generates conda env setup, shared-lineage download, SLURM array scripts with sample-sheet-driven (input × lineage) fan-out, a local single-input fallback, and an aggregator. Use when running BUSCO, assessing genome/transcriptome/proteome completeness, scoring against an odb10 lineage, or when the user mentions "BUSCO", "BUSCO score", "completeness", "C/S/D/F/M", "ortholog completeness", "genome/transcriptome/assembly QC", or asks whether an assembly is complete. Sanitizes FASTA headers automatically — BUSCO 6.x rejects "/" in headers, which trips up IsoSeq3 transcripts. Do NOT load for FCS-GX foreign-sequence screening (use fcs-gx), eggNOG-mapper functional annotation (use eggnog-mapper), or PROST structural homology (use prost-annotation)./gene-lookupLook up gene or protein information from biological database IDs and accessions. Use when working with gene IDs, protein accessions, or identifiers from UniProt, Ensembl, FlyBase, WormBase, NCBI/RefSeq, or similar databases. Covers: identifying what database an ID comes from, converting IDs to gene symbols or names, retrieving protein function or annotation, batch querying APIs, and cross-referencing between databases. Use whenever someone asks "what gene is this", "look up this protein", "get info on these accessions", or needs to map between identifier systems. Also use for phylogenetic tree tip label gene name resolution.
Other plugins' skills for the same job
npx claudepluginhub musserlab/lab-claude-skills --plugin genome-annotation