Add obikselect crate for k-mer selection and aggregation

Introduces a new `obikselect` module to handle k-mer selection and column aggregation. Adds a `select` CLI command in `obikmer2` that supports group predicates, aggregate operators, and output column filtering. Implements operator parsing with case-insensitive matching and default rules, along with a centralized module for resolving group specifications from metadata or genome filters. Updates dependencies to include the new crate and its prerequisites.
This commit is contained in:
Eric Coissac committed 2026-08-26 14:27:19 +02:00
1 parent 354e6f9bf1
commit 737966899f
9 files changed
+291

No files matched your search

+1
View File
@@ -5,6 +5,7 @@ edition = "2024"
[dependencies]
obikindex = { path = "../obikindex" }
obikfilter = { path = "../obikfilter" }
obikalgorithm = { path = "../obikalgorithm" }
obicompactvec = { path = "../obicompactvec" }
obisys = { path = "../obisys" }
+116
View File
@@ -0,0 +1,116 @@
//! Resolving CLI-level group/column definitions into a concrete
//! `Vec<OutputCol>` — the logic `obikmer2`'s `select` command delegates to,
//! so the command itself stays argument parsing + the `Select` call. Named
//! groups defined by a metadata predicate reuse `obikfilter::GenomeSelector`
//! (the same predicate parser `filter`'s `--ingroup`/`--outgroup` use)
//! instead of a separate ad hoc resolver.
use std::collections::{BTreeMap, HashMap};
use obikfilter::GenomeSelector;
use obikindex::IndexMeta;
use crate::select_layer::{AggOp, OutputCol};
pub struct ColumnSpecParams<'a> {
/// Named groups defined by a metadata predicate: `(name, predicate)`.
/// Mutually exclusive with `aggregate_by` (enforced at the CLI layer).
pub group_preds: &'a [(String, String)],
/// One group per unique value of this metadata key, instead of `group_preds`.
pub aggregate_by: Option<&'a str>,
/// Per-group operator override: `(name, op string)`.
pub group_ops: &'a [(String, String)],
/// Operator applied to every group without its own override — already
/// resolved (e.g. from a `clap::ValueEnum`), unlike `group_ops`'
/// free-form strings (embedded in a combined `name:op` argument).
pub aggregate_op: Option<AggOp>,
/// Explicit output column order — group names or genome labels. Defaults
/// to group definition order, or every source genome if there are no groups.
pub select: Option<&'a [String]>,
pub src_is_count: bool,
}
/// Resolve `p` against `meta` into an ordered list of output columns.
///
/// Returns `(specs, output_presence)`.
pub fn build_output_cols(meta: &IndexMeta, p: ColumnSpecParams) -> Result<(Vec<OutputCol>, bool), String> {
let genomes = meta.genomes().map_err(|e| e.to_string())?;
// ── 1. group_indices: name → Vec<usize>, in definition order ──────────
let mut group_order: Vec<String> = Vec::new();
let mut group_indices: HashMap<String, Vec<usize>> = HashMap::new();
if let Some(key) = p.aggregate_by {
let mut value_to_indices: BTreeMap<String, Vec<usize>> = BTreeMap::new();
for (i, g) in genomes.iter().enumerate() {
if let Some(v) = g.meta.get(key) {
value_to_indices.entry(v.clone()).or_default().push(i);
}
}
for (v, idxs) in value_to_indices {
group_order.push(v.clone());
group_indices.insert(v, idxs);
}
} else {
for (name, pred) in p.group_preds {
let idxs = GenomeSelector::parse(std::slice::from_ref(pred), &[])?
.run(meta)?
.ingroup_idx;
if !group_indices.contains_key(name) {
group_order.push(name.clone());
}
group_indices.insert(name.clone(), idxs);
}
}
// ── 2. per-group operators ──────────────────────────────────────────
let global_op = p.aggregate_op;
let mut group_op: HashMap<String, AggOp> = HashMap::new();
for (name, op_str) in p.group_ops {
if !group_indices.contains_key(name) {
return Err(format!("--group-op references undefined group: {name}"));
}
group_op.insert(name.clone(), AggOp::parse(op_str)?);
}
// ── 3. genome label → index, for pass-through columns ───────────────
let label_to_idx: HashMap<&str, usize> = genomes.iter().enumerate()
.map(|(i, g)| (g.label.as_str(), i))
.collect();
// ── 4. output column order ───────────────────────────────────────────
let col_names: Vec<String> = if let Some(sel) = p.select {
sel.to_vec()
} else if !group_order.is_empty() {
group_order
} else {
genomes.iter().map(|g| g.label.clone()).collect()
};
// ── 5. build OutputCol list ───────────────────────────────────────────
let mut specs: Vec<OutputCol> = Vec::with_capacity(col_names.len());
for name in &col_names {
if let Some(idxs) = group_indices.get(name) {
let op = group_op.get(name).copied()
.or(global_op)
.unwrap_or_else(|| AggOp::default_for(p.src_is_count));
specs.push(OutputCol { label: name.clone(), indices: idxs.clone(), op });
} else if let Some(&idx) = label_to_idx.get(name.as_str()) {
specs.push(OutputCol {
label: name.clone(),
indices: vec![idx],
op: AggOp::default_for(p.src_is_count),
});
} else {
return Err(format!("--select: unknown column '{name}' (not a group name or genome label)"));
}
}
if specs.is_empty() {
return Err("select: no output columns defined".to_string());
}
// ── 6. output content kind ────────────────────────────────────────────
let output_presence = !p.src_is_count || specs.iter().all(|s| s.op.is_logical());
Ok((specs, output_presence))
}
+2
View File
@@ -7,8 +7,10 @@
//! `obikmerge`: [`Select`] implements `obikalgorithm::Algorithm` (two-phase
//! `new` + setters, then `run`).
mod group_specs;
mod select;
mod select_layer;
pub use group_specs::{ColumnSpecParams, build_output_cols};
pub use select::Select;
pub use select_layer::{AggOp, OutputCol};
+21
View File
@@ -33,6 +33,27 @@ impl AggOp {
pub fn is_logical(self) -> bool {
matches!(self, AggOp::Any | AggOp::All | AggOp::None)
}
/// Parse an aggregation operator name (`any`/`all`/`none`/`sum`/`min`/`max`,
/// case-insensitive) — the CLI-facing string form, e.g. `--group-op`/
/// `--aggregate-op`.
pub fn parse(s: &str) -> Result<Self, String> {
match s.to_lowercase().as_str() {
"any" => Ok(AggOp::Any),
"all" => Ok(AggOp::All),
"none" => Ok(AggOp::None),
"sum" => Ok(AggOp::Sum),
"min" => Ok(AggOp::Min),
"max" => Ok(AggOp::Max),
other => Err(format!("unknown aggregation operator: {other}; valid: any, all, none, sum, min, max")),
}
}
/// Default operator for a source of the given content kind: `Sum` for
/// counts, `Any` for presence.
pub fn default_for(src_is_count: bool) -> Self {
if src_is_count { AggOp::Sum } else { AggOp::Any }
}
}
// ── OutputCol ─────────────────────────────────────────────────────────────────