Introduce the `obikphylo` crate to support genome-vs-genome distance matrix computation and phylogenetic tree inference via Neighbor-Joining and UPGMA algorithms. Extend the `obikmer2` CLI with a new `phylo` command that exposes configurable metrics, presence thresholds, and CSV/Newick output options. Refactor `obikindex` to expose trait-based partial aggregation for efficient distance metric finalization without full matrix materialization. Update dependency graphs and remove obsolete storage modules.
89 lines
2.9 KiB
Rust
89 lines
2.9 KiB
Rust
//! A minimal, write-only phylogenetic tree: just enough structure to unify
|
|
//! whatever a reconstruction algorithm produces (NJ, UPGMA, ...) into one
|
|
//! shared Newick serialisation, instead of each algorithm hand-rolling its
|
|
//! own Newick writer. No reader — nothing in this crate ever needs to parse
|
|
//! a Newick string back in, so there is no `from_newick`.
|
|
|
|
mod newick;
|
|
mod nj;
|
|
mod upgma;
|
|
|
|
pub use nj::neighbor_joining_newick;
|
|
pub use upgma::upgma_newick;
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
struct NodeId(usize);
|
|
|
|
#[derive(Debug, Default)]
|
|
struct Node {
|
|
name: Option<String>,
|
|
branch_length: Option<f64>,
|
|
bootstrap: Option<f64>,
|
|
children: Vec<NodeId>,
|
|
}
|
|
|
|
/// Arena-based tree: nodes are created detached ([`new_leaf`](Tree::new_leaf)/
|
|
/// [`new_node`](Tree::new_node)) and wired together with
|
|
/// [`attach`](Tree::attach) — the shape every bottom-up reconstruction
|
|
/// algorithm (UPGMA's successive merges, NJ's already-built graph) needs:
|
|
/// a node's parent is only known once *it* already exists.
|
|
#[derive(Debug, Default)]
|
|
struct Tree {
|
|
nodes: Vec<Node>,
|
|
root: Option<NodeId>,
|
|
}
|
|
|
|
impl Tree {
|
|
fn new() -> Self {
|
|
Self::default()
|
|
}
|
|
|
|
/// Create a new, detached leaf node.
|
|
fn new_leaf(&mut self, name: impl Into<String>) -> NodeId {
|
|
let id = self.new_node();
|
|
self.nodes[id.0].name = Some(name.into());
|
|
id
|
|
}
|
|
|
|
/// Create a new, detached internal node (no name).
|
|
fn new_node(&mut self) -> NodeId {
|
|
let id = NodeId(self.nodes.len());
|
|
self.nodes.push(Node::default());
|
|
id
|
|
}
|
|
|
|
/// Attach `child` under `parent`, recording the branch length between them.
|
|
fn attach(&mut self, parent: NodeId, child: NodeId, branch_length: f64) {
|
|
self.nodes[child.0].branch_length = Some(branch_length);
|
|
self.nodes[parent.0].children.push(child);
|
|
}
|
|
|
|
/// Record a bootstrap/support value for `node` — meaningful on an
|
|
/// internal node only; printed right after its closing parenthesis, per
|
|
/// Newick convention.
|
|
#[allow(dead_code)] // no current caller sets one (NJ/UPGMA don't produce support values); kept for the next algorithm that does.
|
|
fn set_bootstrap(&mut self, node: NodeId, value: f64) {
|
|
self.nodes[node.0].bootstrap = Some(value);
|
|
}
|
|
|
|
/// Mark `node` as the tree's root. Required exactly once before
|
|
/// [`to_newick`](Tree::to_newick).
|
|
fn set_root(&mut self, node: NodeId) {
|
|
self.root = Some(node);
|
|
}
|
|
|
|
/// Serialise to Newick (terminated with `;`). Panics if
|
|
/// [`set_root`](Tree::set_root) was never called — a tree with no
|
|
/// designated root is a builder bug, not a recoverable input.
|
|
fn to_newick(&self) -> String {
|
|
let root = self.root.expect("Tree::to_newick: root not set");
|
|
let mut out = newick::write_node(self, root);
|
|
out.push(';');
|
|
out
|
|
}
|
|
|
|
fn node(&self, id: NodeId) -> &Node {
|
|
&self.nodes[id.0]
|
|
}
|
|
}
|