AGENT SKILL

bio-phylo-tree-io

Read, write, and convert phylogenetic tree files with Biopython Bio.Phylo, and choose an annotation-preserving parser (treeio, DendroPy) when metadata matters. Covers why a tree file is a lossy serialization, why format conversion silently drops BEAST/MrBayes node annotations (posteriors, HPD intervals, rates), the Newick support-vs-label ambiguity that mislabels bootstrap values, and the Nexus TRANSLATE and rooted/unrooted traps. Use when parsing Newick, Nexus, NHX, phyloXML, or NeXML, converting between formats, handling posterior tree sets, or moving annotated BEAST trees without losing the credible intervals. Routes annotation-critical reads to DendroPy or treeio and orthology/alignment context to sibling skills.

License 许可
Apache-2.0
最近更新
2026-10-12
Stars
★ 2
Forks
0

01 前言 Front Matter

name
bio-phylo-tree-io
tool_type
python
description
Read, write, and convert phylogenetic tree files with Biopython Bio.Phylo, and choose an annotation-preserving parser (treeio, DendroPy) when metadata matters. Covers why a tree file is a lossy serialization, why format conversion silently drops BEAST/MrBayes node annotations (posteriors, HPD intervals, rates), the Newick support-vs-label ambiguity that mislabels bootstrap values, and the Nexus TRANSLATE and rooted/unrooted traps. Use when parsing Newick, Nexus, NHX, phyloXML, or NeXML, converting between formats, handling posterior tree sets, or moving annotated BEAST trees without losing the credible intervals. Routes annotation-critical reads to DendroPy or treeio and orthology/alignment context to sibling skills.
primary_tool
Bio.Phylo

02 资源文件 4

03 SKILL.md 正文

Version Compatibility

Reference examples tested with: BioPython 1.83+. Annotation-preserving alternatives: DendroPy 5+ (Python), treeio 1.26+ / ape 5.8+ (R).

Before using code patterns, verify installed versions match. If versions differ:

  • Python: pip show biopython then help(module.function) to check signatures
  • R: packageVersion('treeio') then ?read.beast to verify parameters

If code throws ImportError, AttributeError, or TypeError, introspect the installed package and adapt the example to match the actual API rather than retrying.

Bio.Phylo stores a Newick [&...] bracket as opaque .comment text and does NOT parse BEAST key-values; DendroPy extract_comment_metadata=True and treeio read.beast do.

Tree I/O -- A Tree File Is a Lossy Serialization

"Read and convert my tree files" -> Parse a tree into an in-memory object and re-serialize it, knowing which annotations each format and parser preserves.

  • Python: Phylo.read('tree.nwk', 'newick'), Phylo.convert(...) (Bio.Phylo)
  • Annotation-critical: dendropy.Tree.get(..., extract_comment_metadata=True) or treeio read.beast()

Scope: reading, writing, converting, and inspecting tree files, and selecting a parser that keeps the annotations the analysis needs. Rooting, pruning, collapsing -> tree-manipulation. Plotting and mapping annotations onto branches -> tree-visualization. Producing BEAST/MrBayes annotated trees -> bayesian-inference, divergence-dating. Taxon-name sanitization shares the whitespace traps in sequence-io/read-sequences.

The Single Most Important Modern Insight

A tree file is a lossy serialization of a richer in-memory object. The biologist cares about the topology plus its annotations -- branch supports, posterior probabilities, 95% HPD intervals on node heights, per-branch rates, divergence dates, taxon metadata -- and formats differ enormously in which of these they can hold, while parsers differ in which they actually read back. Three load-bearing facts:

  1. Conversion is a silent data-destroying operation. Reading a BEAST MCC tree and writing plain Newick produces a topologically identical tree that plots fine, but the HPD intervals, clade posteriors, and per-branch rates are gone and unrecoverable without re-running a multi-day MCMC. The loss is invisible until a reviewer asks where the credible intervals went.
  2. The tool, not the format string, decides whether [&...] metadata survives. In Python the naive default (Bio.Phylo) does not parse BEAST key-values and corrupts them on Newick write; in R the naive default (ape::read.nexus) drops them; the tools built to preserve them are DendroPy (extract_comment_metadata=True) and treeio (read.beast). Route annotated trees through those.
  3. In plain Newick a bare number has no fixed meaning. In (A,B)95:0.3 the 95 could be a bootstrap, a posterior, an internal clade name, or a second branch length. Only the tool that wrote the file knows; a parser that guesses wrong turns supports into names silently. IQ-TREE overloads the slot further, writing SH-aLRT/UFBoot (e.g. 87.5/98), which a single-value parser truncates or chokes on.

Tool Taxonomy

Tool (lang) BEAST [&...] NHX phyloXML richness When
treeio (R) YES, structured (read.beast/read.mrbayes/read.iqtree) YES via tidytree/ggtree the default whenever annotations matter; feeds ggtree
DendroPy (Py) YES, structured (.annotations) YES no Python work needing metadata, posterior sets, tree distances, conversion with annotations
ETE3/ETE4 (Py) partial (custom features) YES, native no reconciliation, NHX round-trips, programmatic node features
Bio.Phylo (Py) NO key-value parse (opaque .comment) no YES, richest general pipelines, conversion among its 5 formats, phyloXML annotation
ape (R) NO (drops [&...]) no no fast topology/branch-length analysis; pair with treeio for annotated files

References: Bio.Phylo Talevich 2012; DendroPy Sukumaran 2010; ETE3 Huerta-Cepas 2016; ape Paradis 2019; treeio Wang 2020.

One-line decision rule: if the file came from BEAST, MrBayes, or RevBayes or carries [&...]/&&NHX that matters, read it with treeio (R) or DendroPy (Py); otherwise Bio.Phylo (Py) or ape (R) is fine. Never route a BEAST MCC tree through Bio.Phylo or ape::read.nexus when the HPDs are needed.

Format Capability (What Each Format Can Hold)

Capability Newick NHX Nexus (BEAST-annotated) phyloXML NeXML
Topology + branch lengths yes yes yes yes yes
One support value ambiguous slot B= tag comment key typed <confidence> typed meta
Multiple supports per node no (single slot) tags comment keys yes (n elements) yes
Posterior / HPD intervals no no yes (_95%_HPD={}) via property via meta
Per-branch rates / dates no tags yes yes yes
Taxonomy (NCBI id/rank) no S=/T= no yes, typed yes
Schema-validated no no loose yes (XSD) yes (XSD)

Newick/NHX are compact and grep-able; phyloXML/NeXML are verbose XML (often 5-20x larger) but typed and validatable -- prefer XML for archiving/exchange where machine-checkable semantics matter, Newick/Nexus for pipeline interchange. BEAST/FigTree metadata rides inside a Nexus (or Newick) [&...] comment, so "it is a Nexus file" says nothing about whether annotations survive -- only the parser does.

Reading, Writing, and Converting (Bio.Phylo)

Goal: Move trees between formats and inspect them without assuming a single tree or losing annotations.

Approach: Use Phylo.read for exactly one tree and Phylo.parse for many (posterior sets); use Phylo.convert only among formats of equal or greater capability; check .confidence vs .name to confirm support was read into the right slot.

from Bio import Phylo

tree = Phylo.read('tree.nwk', 'newick')          # exactly one tree; raises if 0 or >1
posterior = list(Phylo.parse('run.trees', 'nexus'))   # many trees: posterior/bootstrap set
Phylo.write(tree, 'tree.xml', 'phyloxml')        # phyloXML is Bio.Phylo's richest format

Phylo.convert('tree.nex', 'nexus', 'tree.nwk', 'newick')   # WARNING: [&...] is written as an escaped [\[&...\]] comment that DendroPy/treeio cannot parse -- annotations are effectively corrupted

for clade in tree.get_nonterminals():
    print(clade.confidence, clade.name)          # confirm the support landed in .confidence, not .name
    if clade.confidence is None and clade.name and '/' in clade.name:   # IQ-TREE -B + --alrt: '98.5/100' stays in .name
        sh_alrt, ufboot = (float(v) for v in clade.name.split('/')[-2:])   # last field = UFBoot

Supported format strings: newick, nexus, phyloxml, nexml, cdao (cdao raises KeyError unless pip install rdflib, and does not keep confidences). Colors and branch widths persist only in phyloXML.

Encoding: on Windows, path-based Phylo.read/Phylo.write (and DendroPy path=) use the locale codec (cp1252), garbling non-ASCII tip names. Pass explicit handles for both directions: with open(p, encoding='utf-8') as fh: Phylo.read(fh, 'newick'), and the same with 'w' for writing.

Preserve BEAST/MrBayes Annotations Before Down-Converting

Goal: Keep posteriors, HPD intervals, and rates when a downstream tool wants plain Newick.

Approach: Read with an annotation-aware parser, extract the numbers into a side table that travels with the analysis, and only then write a stripped topology -- never down-convert first.

import dendropy

tree = dendropy.Tree.get(path='mcc.tree', schema='nexus', extract_comment_metadata=True)
for node in tree:
    if node.annotations.get_value('posterior') is not None:
        post = float(node.annotations.get_value('posterior'))   # DendroPy returns strings; cast
        hpd = [float(v) for v in node.annotations.get_value('height_95%_HPD')]   # raw BEAST key; treeio typically exposes it as height_0.95_HPD (exact name varies by source program and version -- introspect the columns)
        # persist post/hpd to a side table keyed by the clade before any conversion
tree.write(path='topology.nwk', schema='newick', suppress_annotations=True, suppress_rooting=True)   # intentional, after extraction; suppress_rooting drops the leading [&R]

In R the equivalent is treeio read.beast('mcc.tree') then get.data() / as_tibble(), feeding ggtree (tree-visualization); write.beast() re-serializes with annotations intact.

Per-Method Failure Modes

BEAST/MrBayes MCC to Plain Newick Erases the Credible Intervals

Trigger: Phylo.convert, ape::read.nexus + write.tree, or any "just give me the topology" step on an annotated tree. Mechanism: The HPDs, posteriors, and rates live only in the [&...] comments; stripping parsers (ape::read.nexus) discard them, and Bio.Phylo writes them as escaped [\[&...\]] comments that DendroPy and treeio no longer parse. Symptom: The output plots fine but the credible intervals are gone, irrecoverable without re-running the MCMC. Fix: Read with treeio read.beast or DendroPy extract_comment_metadata=True; extract the numbers to a side table; keep the original .tree as the source of truth.

Support Value Read as a Node Name (or Truncated)

Trigger: Parsing a tree whose internal-node slot holds a bootstrap, a posterior, a clade name, or IQ-TREE's SH-aLRT/UFBoot dual value. Mechanism: The Newick grammar gives one slot for all of these; the parser must be told which it is. Bio.Phylo keeps a non-numeric label such as 98.5/100 whole in .name and leaves .confidence None. Symptom: Supports appear as .name strings, and any code reading .confidence silently gets None. Fix: Know what wrote the file; in Bio.Phylo inspect .confidence vs .name and split name.split('/') (SH-aLRT, UFBoot last) as in the snippet above; in treeio use read.iqtree/read.raxml, which split dual support correctly.

Whitespace, Underscore, or Non-ASCII Taxon Names

Trigger: Tip names with spaces, parentheses, commas, or accented characters; reliance on the Newick underscore-space convention. Mechanism: Naive CLI tools split unquoted spaces, and underscore-to-space auto-conversion silently desyncs tip labels from a metadata join key. Symptom: Downstream tools error or a metadata merge matches nothing. Fix: Read and write through UTF-8 handles; sanitize to [A-Za-z0-9_.] before ape or ete3 ever see the file (ape reads 'O''Brien isolate 7' as NA; ete3 raises on quoted names), keep a label map, and round-trip-test the labels against the metadata table before any join.

Nexus TRANSLATE-Table Desync and Rooted/Unrooted Confusion

Trigger: Hand-editing or merging Nexus tree blocks; assuming topology shape implies rootedness. Mechanism: The integer-to-name TRANSLATE map can decouple from the tree and silently relabel tips; Newick does not flag rootedness (a basal trifurcation conventionally signals unrooted, but tools disagree), while Nexus carries an explicit [&R]/[&U]. Symptom: Tips are mislabeled after a merge, or a rooting-sensitive analysis runs on the wrong assumption without erroring. Fix: Parse with a translate-aware reader (treeio/DendroPy/ape apply it); verify tip-label sets match across merged trees; set rootedness explicitly rather than trusting topology shape.

Quantitative and Practical Notes

Item Guidance Why
Support-value scales bootstrap/UFBoot in [0,100], posterior in [0,1], SH-aLRT in [0,100] a number is meaningless without knowing which test produced it; preserve provenance, not just the value
Multi-tree files use Phylo.parse / DendroPy TreeList / treeio read.beast; check object length a single-tree reader on a .trees posterior returns only the first or errors
Round-trip test read -> write -> read and diff the annotations, not just the topology topology almost always survives and gives false confidence
Posterior set vs MCC .trees is the full posterior; .tree/.mcc is the single annotated summary read.beast on a full posterior is huge; usually the MCC is wanted

Common Errors

Error / symptom Cause Solution
HPD bars missing after conversion converted a BEAST tree to Newick extract annotations with treeio/DendroPy first
Phylo.read raises on a .trees file multiple trees in the file use Phylo.parse and iterate
Bootstrap values show up as taxon names node-label slot read as .name set/inspect confidence parsing; use a software-specific reader
Metadata join matches nothing underscore/space relabeling of tips sanitize and round-trip-test labels before joining
Parser errors on [ strict parser chokes on FigTree comment strip comments only after extracting needed metadata
AssertionError: Two string taxonomies? from Phylo.read on a MrBayes .con.tre Bio.Phylo cannot parse MrBayes' annotated consensus layout read with DendroPy (extract_comment_metadata=True) or treeio read.mrbayes
NeXML tips named d7, d8... Bio.Phylo's NeXML reader names tips by otu id, not label check tip names; read NeXML from other tools with DendroPy
Non-ASCII tip garbled (Cercopithèque) path-based read/write used the Windows locale codec open files with encoding='utf-8' for read and write

References

Cock PJA, Antao T, Chang JT, et al. 2009. Biopython: freely available Python tools for computational molecular biology and bioinformatics. Bioinformatics 25(11):1422-1423. Talevich E, Invergo BM, Cock PJA, Chapman BA. 2012. Bio.Phylo: a unified toolkit for processing, analyzing and visualizing phylogenetic trees in Biopython. BMC Bioinformatics 13:209. Sukumaran J, Holder MT. 2010. DendroPy: a Python library for phylogenetic computing. Bioinformatics 26(12):1569-1571. Huerta-Cepas J, Serra F, Bork P. 2016. ETE 3: reconstruction, analysis, and visualization of phylogenomic data. Molecular Biology and Evolution 33(6):1635-1638. Paradis E, Schliep K. 2019. ape 5.0: an environment for modern phylogenetics and evolutionary analyses in R. Bioinformatics 35(3):526-528. Wang L-G, Lam TT-Y, Xu S, et al. 2020. Treeio: an R package for phylogenetic tree input and output with richly annotated and associated data. Molecular Biology and Evolution 37(2):599-603. Maddison DR, Swofford DL, Maddison WP. 1997. NEXUS: an extensible file format for systematic information. Systematic Biology 46(4):590-621. Han MV, Zmasek CM. 2009. phyloXML: XML for evolutionary biology and comparative genomics. BMC Bioinformatics 10:356. Vos RA, Balhoff JP, Caravas JA, et al. 2012. NeXML: rich, extensible, and verifiable representation of comparative data and metadata. Systematic Biology 61(4):675-689.

Related Skills

  • tree-manipulation - rooting, pruning, and collapsing where rooted/unrooted and polytomy choices bite
  • tree-visualization - ggtree and ETE consume the annotations preserved here
  • bayesian-inference - produces the BEAST/MrBayes annotated trees whose metadata must survive
  • divergence-dating - produces MCC trees with HPD intervals on node ages
  • sequence-io/read-sequences - taxon-name sanitization shares the whitespace and non-ASCII traps

04 同仓库更多 Skills

mrsonord2240
mrsonord2240/openscience-specialists

bio-workflows-metabolomics-pipeline

Orchestrates the untargeted LC-MS metabolomics pipeline end-to-end (xcms 4.x feature extraction, QC/drift/normalization, confidence-stratified annotation, permutation-validated statistics, background-aware pathway mapping), naming what each stage decides and where it silently fails. Use when running a full LC-MS metabolomics study from raw mzML to enriched pathways and needing the honest handoffs between stages. Each stage defers to its component skill for parameters and traps; for stable-isotope flux (a separate branch, not this untargeted flow) see metabolomics/isotope-tracing.

★ 2
mrsonord2240
mrsonord2240/openscience-specialists

bio-metabolomics-xcms-preprocessing

Programmatic untargeted LC-MS feature extraction in R with the modern xcms 4.x MsExperiment/XcmsExperiment API, taking raw mzML to a feature table via CentWave peak detection, retention-time alignment, peak-density correspondence, gap-filling, CAMERA redundancy collapse, and built-in QC feature filtering. Use when converting centroided LC-MS runs into a features-by-samples matrix and deciding centWave/grouping/alignment parameters. For drift correction and QC/CV filtering execution see metabolomics/normalization-qc; for metabolite identification see metabolomics/metabolite-annotation; for the MS-DIAL GUI alternative with MS2Dec deconvolution see metabolomics/msdial-preprocessing; for downstream statistics see metabolomics/statistical-analysis.

★ 2
mrsonord2240
mrsonord2240/openscience-specialists

bio-metabolomics-statistical-analysis

Decision-grade statistical analysis for metabolomics intensity tables. Covers transformation and scaling (Pareto vs unit-variance as a hidden hypothesis), unsupervised structure (PCA/HCA for QC), permutation-validated PLS-DA/OPLS-DA (R2 vs Q2, double CV, VIP as heuristic), univariate testing (Welch/Mann-Whitney/ANOVA/LMM with covariate adjustment), and dependence-aware multiple testing. Use when testing which metabolites differ, building or validating a discriminant model, choosing a scaling, or correcting many correlated tests. For sample-wise normalization/drift correction see metabolomics/normalization-qc; for ML classifiers and selection-inside-CV leakage see machine-learning/biomarker-discovery and machine-learning/model-validation; for pathway interpretation see metabolomics/pathway-mapping; for design/power/multiplicity regime see experimental-design/multiple-testing.

★ 2
mrsonord2240
mrsonord2240/openscience-specialists

bio-metabolomics-pathway-mapping

Maps metabolomics results to biological pathways via over-representation (ORA), metabolite-set enrichment (MSEA/QEA), mummichog/PSEA on raw m/z peaks, and network-diffusion enrichment (FELLA), with correct background-set construction and honest interpretive ceilings. Use when interpreting differential metabolites or an untargeted LC-MS feature table in pathway context, choosing ORA vs MSEA vs mummichog vs topology, or setting the reference/background set. For annotation confidence levels feeding ORA see metabolomics/metabolite-annotation; for gene-set concepts see pathway-analysis/go-enrichment and pathway-analysis/gsea; for joint gene+metabolite pathways see multi-omics-integration/mofa-integration.

★ 2
mrsonord2240
mrsonord2240/openscience-specialists

bio-metabolomics-normalization-qc

Designs QC, corrects signal drift, removes batch effects, filters features, normalizes samples, and imputes missing values for untargeted LC-MS/GC-MS metabolomics, framing each step as a measurement model that can create or erase biological signal. Use when processing a peak/feature table before statistical analysis, choosing a drift-correction or sample-normalization method, deciding QC RSD vs D-ratio filtering, or handling left-censored missing values. The feature table is produced by metabolomics/xcms-preprocessing or metabolomics/msdial-preprocessing; transformation/scaling for modeling defers to metabolomics/statistical-analysis; cross-study design issues link to experimental-design/batch-design.

★ 2
mrsonord2240
mrsonord2240/openscience-specialists

bio-metabolomics-msdial-preprocessing

Runs the MS-DIAL preprocessing workflow (peak picking, MS2Dec spectral deconvolution, alignment, gap-filling) and imports the alignment-result table into R or Python with honest filtering. Use when preprocessing LC-MS DDA/DIA (SWATH) or GC-MS raw data with MS-DIAL, deciding MS-DIAL vs XCMS, configuring the MSDIALCUI console run, or parsing an MS-DIAL export into a clean feature matrix. For programmatic R peak detection and the feature-table-as-artifact framing see metabolomics/xcms-preprocessing; for lipid annotation mode see metabolomics/lipidomics; for MSI-level confidence honesty see metabolomics/metabolite-annotation; for drift correction and QC see metabolomics/normalization-qc.

★ 2