From 294f132a0ad03be9e63422c5d4cbcbc43ee9dc41 Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Wed, 26 Aug 2026 07:01:55 +0200 Subject: [PATCH] refactor: relocate partition iterator and expose graph builder API Move the partition iterator implementation from obikdump to obikfilter, updating imports and exposing the type publicly. Add obikidxcache as a local dependency for obikfilter. In obikindexer, expose the new build_layer_from_kmers function to enable shared graph-building logic across pipelines without code duplication. --- src/Cargo.lock | 1 + src/obikdump/src/dump.rs | 2 +- src/obikdump/src/lib.rs | 3 --- src/obikfilter/Cargo.toml | 1 + src/obikfilter/src/lib.rs | 15 ++++++------- .../src/partition_iter.rs | 4 ++-- src/obikindexer/src/graph_pipeline.rs | 21 +++++++++++++++++++ src/obikindexer/src/lib.rs | 2 +- 8 files changed, 35 insertions(+), 14 deletions(-) rename src/{obikdump => obikfilter}/src/partition_iter.rs (97%) diff --git a/src/Cargo.lock b/src/Cargo.lock index 1e1fcdfc..33d3ddb3 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -1543,6 +1543,7 @@ version = "0.1.0" dependencies = [ "obicompactvec", "obikentropy", + "obikidxcache", "obikindex", "obikseq", "obiskio", diff --git a/src/obikdump/src/dump.rs b/src/obikdump/src/dump.rs index c3ae6b05..960a0772 100644 --- a/src/obikdump/src/dump.rs +++ b/src/obikdump/src/dump.rs @@ -8,7 +8,7 @@ use obikindex::KmerIndex; use obikidxcache::index_cache::IndexCache; use obikfilter::KmerFilter; -use crate::partition_iter::FilteredPartitionIter; +use obikfilter::FilteredPartitionIter; /// Raw content export of a `KmerIndex` — `KmerIndex` is a foreign type /// (`obikindex`), so this is an extension trait rather than an inherent `impl`. diff --git a/src/obikdump/src/lib.rs b/src/obikdump/src/lib.rs index 45a7124d..f953444f 100644 --- a/src/obikdump/src/lib.rs +++ b/src/obikdump/src/lib.rs @@ -6,6 +6,3 @@ //! reverse), same pattern as `obikindexer`/`obikquery`. mod dump; -mod partition_iter; - -pub use partition_iter::FilteredPartitionIter; diff --git a/src/obikfilter/Cargo.toml b/src/obikfilter/Cargo.toml index 750e9f25..7643cad7 100644 --- a/src/obikfilter/Cargo.toml +++ b/src/obikfilter/Cargo.toml @@ -10,3 +10,4 @@ obikseq = { path = "../obikseq" } obiskio = { path = "../obiskio" } obitaxonomy = { path = "../obitaxonomy" } obikentropy = { path = "../obikentropy" } +obikidxcache = { path = "../obikidxcache" } diff --git a/src/obikfilter/src/lib.rs b/src/obikfilter/src/lib.rs index a6b5736f..38a7b296 100644 --- a/src/obikfilter/src/lib.rs +++ b/src/obikfilter/src/lib.rs @@ -3,19 +3,20 @@ //! Orthogonal to genome-column selection/aggregation (`obikselect`), which //! operates on already-retained k-mers. //! -//! [`filter`] (the `KmerFilter` trait + its implementations) depends only -//! on `obicompactvec`/`obikseq`, not on `obikindex`. The partition/layer -//! iteration that actually runs these filters over an index -//! (`iter_partition_kmers`, `iter_partition_kmers_located`) lives in -//! `obikdump` instead (`FilteredPartitionIter`) — it needs `obikidxcache`'s -//! `IndexCache` to read a *complete* source index, which this crate has no -//! reason to depend on. +//! [`filter`] (the `KmerFilter` trait + its implementations) is the pure +//! judging logic. [`partition_iter`] (`FilteredPartitionIter`) is the read +//! side that actually runs those filters over a *complete* source index's +//! partitions/layers, batched through `obikidxcache::IndexCache` for +//! locality — used both by `obikdump` (CSV export) and by this crate's own +//! `Filter` algorithm (index-to-index rebuild). mod filter; +mod partition_iter; mod predicate; pub use filter::{ GroupQuorumFilter, KmerFilter, MaxGenomeCount, MaxGenomeFraction, MaxTotalCount, MinComplexity, MinGenomeCount, MinGenomeFraction, MinTotalCount, passes_all, }; +pub use partition_iter::FilteredPartitionIter; pub use predicate::{GenomeSelector, GroupFilterParams, MetaPred, Selection}; diff --git a/src/obikdump/src/partition_iter.rs b/src/obikfilter/src/partition_iter.rs similarity index 97% rename from src/obikdump/src/partition_iter.rs rename to src/obikfilter/src/partition_iter.rs index b419c3e9..6e4d640e 100644 --- a/src/obikdump/src/partition_iter.rs +++ b/src/obikfilter/src/partition_iter.rs @@ -1,5 +1,5 @@ //! Filtered, batch-oriented iteration over an already-cached index's -//! partitions/layers — the read side of `obikfilter`'s `KmerFilter`s. +//! partitions/layers — the read side of [`crate::KmerFilter`]s. //! `IndexCache` is a foreign type (`obikidxcache`), so this is an extension //! trait rather than an inherent `impl`. //! @@ -13,7 +13,7 @@ use obikindex::layer::{KmerLayer, LayerContent}; use obikidxcache::index_cache::IndexCache; use obikseq::CanonicalKmer; -use obikfilter::{KmerFilter, passes_all}; +use crate::filter::{KmerFilter, passes_all}; /// Kmers pulled per batch from a layer before filtering — keeps matrix reads /// grouped by (partition, layer) for locality instead of hopping row to row diff --git a/src/obikindexer/src/graph_pipeline.rs b/src/obikindexer/src/graph_pipeline.rs index 5fd9ae9b..c7e842f1 100644 --- a/src/obikindexer/src/graph_pipeline.rs +++ b/src/obikindexer/src/graph_pipeline.rs @@ -148,3 +148,24 @@ pub fn materialize_layer( debug!("materialize_layer: MPHF build done"); Ok(n) } + +// ── build_layer_from_kmers ────────────────────────────────────────────────── + +/// Build a layer's identity (unitigs + MPHF + evidence) straight from an +/// already-filtered kmer iterator — no file I/O, no abundance filtering: +/// the caller has already decided which kmers survive (e.g. abundance +/// filtering in [`crate::extensions::build_index_layer`], metadata-based +/// filtering in `obikfilter::Filter`). Both share this same construction +/// tail instead of duplicating the graph-building mechanism. +pub fn build_layer_from_kmers( + kmers: impl Iterator, + layer_dir: &Path, + block_bits: u8, + evidence: &IndexMode, +) -> OKIResult { + let mut g = GraphDeBruijn::new(); + for kmer in kmers { + g.push(kmer); + } + materialize_layer(g, layer_dir, block_bits, evidence) +} diff --git a/src/obikindexer/src/lib.rs b/src/obikindexer/src/lib.rs index 2b6bc442..a6f0f5bc 100644 --- a/src/obikindexer/src/lib.rs +++ b/src/obikindexer/src/lib.rs @@ -16,4 +16,4 @@ pub mod algorithms; pub(crate) mod extensions; pub mod graph_pipeline; -pub use graph_pipeline::{build_graph, materialize_layer, write_graph_as_unitigs}; +pub use graph_pipeline::{build_graph, build_layer_from_kmers, materialize_layer, write_graph_as_unitigs};