🌳 phylo
A fast, extensible, WebAssembly-ready phylogenetics library for Rust.
phylo provides memory-efficient data structures and algorithms for phylogenetic
analysis and inference — from tree manipulation (SPR, NNI, rerooting) to tree
statistics (phylogenetic diversity, RF distance, cophenetic distance). It leans
on Rust's memory safety, speed, and native WebAssembly support to stay both fast
and portable.
Tree traversals and operations are exposed as derivable traits, so you get
DFS/BFS/pre-/post-order, Euler tours, LCA queries, and distance metrics for free
on your own types — and a ready-made SimpleRootedTree when you don't want to
implement one.
Highlights
- Trait-first design — compose narrow traits (
RootedTree,RootedMetaTree,EulerWalk,DFS,Clusters, …) onto any type, or use the batteries-includedPhyloTree. - Arena-allocated trees — cache-friendly
Vec-backed storage withusizenode IDs. - Constant-time LCA — an
LcaOracleborrows the tree immutably and answers LCA queries in O(1) via an Euler tour + RMQ. - Tree comparison — Robinson-Foulds, weighted RF, cluster affinity, and cophenetic distance, with distance-matrix builders.
- Maximum-likelihood modeling — GTR+I+G substitution models (JC69 through GTR), Felsenstein-pruning log-likelihood, and marginal/joint ancestral sequence reconstruction.
- I/O — Newick and Nexus parsing and serialization.
- Simulation — random trees (Yule, uniform).
- Optional parallelism — opt into
rayon-backed computation with theparallelfeature.
Installation
Or add it to Cargo.toml:
[]
= "5"
Feature flags
| Feature | Default | Description |
|---|---|---|
simple_rooted_tree |
Yes | The concrete SimpleRootedTree / PhyloTree implementation. |
non_crypto_hash |
Yes | Use fxhash maps/sets instead of std for speed. |
parallel |
rayon-based parallel computation for the heavy metrics. |
|
serde |
Serialize/Deserialize for trees. |
Quick start
Everything you need is in the prelude:
use *;
Build a tree
Create an empty tree, then attach children to node IDs:
use *;
let mut tree = new;
tree.add_child;
tree.add_child;
tree.add_child;
tree.add_child;
Read and write Newick
use *;
let tree = from_newick.unwrap;
let newick = tree.to_newick;
Traverse
Traversals return an Iterator of nodes or node IDs in visiting order:
use *;
let tree = from_newick.unwrap;
let dfs = tree.dfs;
let bfs = tree.bfs_ids;
let postorder = tree.postord_ids;
Constant-time LCA
Build an LcaOracle with tree.lca(); it borrows the tree immutably (so
staleness is a compile error, not a runtime bug) and answers queries in O(1):
use *;
let tree = from_newick.unwrap;
let a = tree.get_taxa_node_id.unwrap;
let b = tree.get_taxa_node_id.unwrap;
let lca = tree.lca;
let ancestor = lca.get_lca_id;
Compare trees
Metrics account for both topology and branch lengths:
use *;
let mut tree_1 = from_newick.unwrap;
let mut tree_2 = from_newick.unwrap;
let _ = tree_1.set_zeta;
let _ = tree_2.set_zeta;
let cluster_affinity = tree_1.ca;
let cophenetic = tree_1.cophen_dist;
Likelihood and ancestral reconstruction
Score an alignment against a tree under a substitution model, or reconstruct
ancestral sequences at the internal nodes. log_likelihood runs Felsenstein's
pruning algorithm alone (no reconstruction); marginal_asr / joint_asr build
on the same pruning core:
use *;
let tree = from_newick.unwrap;
// A nucleotide alignment in FASTA — one sequence per leaf taxon.
let fasta = b">A\nACGTACGT\n>B\nACGTATGT\n>C\nACGAACGT\n>D\nTCGTACGA\n";
let aln = from_fasta_bytes.unwrap;
// HKY85 with gamma-distributed rate heterogeneity (+G, 4 categories).
let model = hky85
.unwrap
.with_gamma
.unwrap;
// Log-likelihood of the alignment given the tree and model (pruning only).
let log_lik = tree..unwrap;
// Marginal ancestral sequence reconstruction fills the internal nodes.
let recon = tree..unwrap;
let root_sequence = recon.sequence_string;
Module map
| Module | What it does |
|---|---|
tree::simple_rtree |
Core tree traits and SimpleRootedTree. |
tree::ops |
Mutating operations: SPR, NNI, reroot, contraction, subtree extraction. |
tree::distances |
RF, weighted RF, cluster affinity, cophenetic distance, distance matrices. |
tree::io |
Newick and Nexus reading/writing. |
tree::simulation |
Random tree generation. |
iter |
Traversals, Euler walks, and the LCA oracle. |
models |
GTR+I+G substitution models and their named special cases. |
tree::likelihood |
Felsenstein-pruning log-likelihood. |
tree::asr |
Marginal and joint ancestral sequence reconstruction. |
Examples
Runnable analyses live in the examples/ directory. To visualize
their output, install the Python requirements first:
Quantifying phylogenetic diversity — the Faith index across a set of trees.
Run it, then plot with examples/visualization/pd.py:
Visualizing tree space — all pairwise distances across a set of trees. Run
it, then plot with examples/visualization/tree-space.py:
WebAssembly
phylo builds for wasm32 targets out of the box, making it suitable for
in-browser phylogenetics. Use your usual wasm toolchain — e.g. wasm-pack, or
cargo build --target wasm32-unknown-unknown.
Citation
If you use phylo in your work, please cite
this paper:
License
Licensed under the MIT License.