This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Phylo2Vec is a high-performance package for encoding, manipulating, and analyzing binary phylogenetic trees. It defines a bijection from any tree topology with n leaves into an integer vector of size n-1. The core is implemented in Rust with bindings for Python (PyO3/maturin) and R (extendr).
All commands use pixi. The -e flag specifies the
environment.
pixi run build # Debug Rust build pixi run build-release # Optimized release build pixi run -e py-phylo2vec install-python # Install Python package (required before running Python tests) pixi run -e r-phylo2vec install-r # Install R package
pixi run -e default test # Rust tests (must specify environment) pixi run -e py-phylo2vec test # Python tests (all) pixi run -e r-phylo2vec test # R tests # Run single Python test pixi run -e py-phylo2vec pytest -k "test_name" pixi run -e py-phylo2vec pytest ./py-phylo2vec/tests/test_base.py::test_function_name # Run single Rust test with output cargo test test_name -- --nocapture
pixi run fmt # cargo fmt pixi run lint # cargo clippy (runs fmt first)
pixi run benchmark # Rust benchmarks (Criterion) pixi run -e py-phylo2vec benchmark # Python benchmarks pixi run profile [vector|matrix] [n_leaves] # Profile with samply
phylo2vec/- Rust core librarysrc/vector/- Vector representation (base, convert, ops, graph, avl, distances)src/matrix/- Matrix representation (base, convert, ops, graph)src/newick/- Newick format parsing
py-phylo2vec/- Python packagesrc/lib.rs- PyO3 bindings exposed as_phylo2vec_corephylo2vec/- Python API wrapping Rust bindings
r-phylo2vec/- R packagesrc/rust/- extendr bindingsR/- R wrappers (structure mirrors Python)utils.R- Tree operations (sample_tree,get_common_ancestor,get_node_depth,get_node_depths)stats.R- Statistics (cophenetic_distances,vcovp,precision,incidence,robinson_foulds)base_newick.R- Newick conversion (to_newick,from_newick)io.R- File I/Oextendr-wrappers.R- Auto-generated Rust bindings
Vector (v): Integer vector of length k = n_leaves - 1. Element v[i]
encodes which branch leaf i+1 attaches to.
Pairs (pairs): Vec<(usize, usize)> of length k.
pairs[i] = (child1, child2) represents internal node n_leaves + i with its
two children.
Ancestry (ancestry): Vec<[usize; 3]> of length k.
ancestry[i] = [child1, child2, parent]. Ordered bottom-up (leaves to root).
Branch lengths (bls): Vec<[f64; 2]> of length k.
bls[i] = [bl_to_child1, bl_to_child2] for internal node n_leaves + i.
Matrix (m): 2D array of shape (k, 3). Column 0 is the vector, columns 1-2
are branch lengths.
Rust Core Access: Python modules import Rust functions via:
from phylo2vec import _phylo2vec_core as core
Adding Rust Functions:
- Implement in
phylo2vec/src/ - Add PyO3 binding in
py-phylo2vec/src/lib.rswith#[pyfunction] - Create Python wrapper in
py-phylo2vec/phylo2vec/ - Add tests:
- Python:
test_utils.pyfor tree ops,test_stats.pyfor statistics - R:
test_utils.Rfor tree ops (mirrorsutils.R),test_stats.Rfor statistics (mirrorsstats.R)
- Python:
Vector/Matrix API Pattern (see get_node_depth in vector/ops.rs and
matrix/ops.rs):
- Implement generic internal function
_func(v, bls: Option<&Vec<[f64; 2]>>)invector/module - When
blsisNone, use topological distances (all branch lengths = 1.0) - Create thin wrapper in
matrix/that parses matrix and calls generic function withSome(&bls) - Python wrapper checks
ndimand calls appropriate binding - For purely topological functions (e.g.,
get_common_ancestor), the Python wrapper can extract column 0 from matrices
R Wrapper Generation: R wrappers in r-phylo2vec/R/extendr-wrappers.R are
auto-generated. After adding new Rust functions, run
pixi run -e r-phylo2vec install-r to regenerate.
Cargo Workspace: Three members - phylo2vec, py-phylo2vec,
r-phylo2vec/src/rust
This library is built for efficiency. Avoid O(n²) or worse algorithms.
Tree traversal patterns:
get_ancestry_path_of_node: O(n) traversal UP from node to root via pairs- Avoid linear scans through ancestry for each node (causes O(n²))
- For repeated lookups, build HashMap<node, index> first
When adding tree algorithms:
- Prefer single-pass O(n) algorithms using pairs/ancestry ordering
- Ancestry is ordered bottom-up, enabling dynamic programming
- Test with n_leaves = 10, 100, 1000, 10000 to verify scaling
Always create a new branch for each feature or bugfix. Never commit directly
to main.
git checkout -b feat/feature-name # For new features git checkout -b fix/bug-description # For bug fixes
Use Conventional Commits with Angular tags:
feat(rs):/feat(py):/feat(r):- New featuresfix:- Bug fixesdocs:,test:,chore:,refactor: