Implement MPHF migration tool and sparse matrix permutation logic
Refactors sparse matrix handling to implement row permutation logic and introduces a standalone tool for migrating indexes from MPHF 1.x to 2.x formats.
This commit is contained in:
1 parent
d686d39506
commit
8b87078478
24 files changed
+3982
-19
No files matched your search
@@ -0,0 +1,121 @@
|
||||
//! One-shot converter for obikmer indexes whose layer MPHFs were built with
|
||||
//! `ptr_hash` 1.x: rebuilds each MPHF with `ptr_hash` 2.x and carries the
|
||||
//! per-slot data (evidence, fingerprint, presence/count matrices) over to the
|
||||
//! new slot numbering, in place, layer by layer, verified before installing.
|
||||
//!
|
||||
//! Presence and count matrices come out in the compact row-major format.
|
||||
//! Annexes derived from slot numbering (sibling, entropy, pair-weight,
|
||||
//! quartet, family-overlap) are set aside under `stale_annexes_pre_ptrhash2/`
|
||||
//! and must be rebuilt.
|
||||
|
||||
mod commit;
|
||||
mod data;
|
||||
mod layer;
|
||||
mod old;
|
||||
mod plan;
|
||||
mod util;
|
||||
mod verify;
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
use anyhow::{Result, ensure};
|
||||
use clap::Parser;
|
||||
use obikindex::KmerIndex;
|
||||
use obisys::PartitionRunner;
|
||||
use tracing::{info, warn};
|
||||
|
||||
use layer::{Options, Outcome};
|
||||
use util::oki;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(about = "Convert an obikmer index from ptr_hash 1.x to ptr_hash 2.x MPHFs, in place")]
|
||||
struct Cli {
|
||||
/// Index directory (the one holding `index.meta` and `part_*`).
|
||||
index: PathBuf,
|
||||
|
||||
/// Limit the number of CPUs used.
|
||||
#[arg(long)]
|
||||
cpu_max: Option<usize>,
|
||||
|
||||
/// K-mers compared old-vs-new per layer before installing it.
|
||||
#[arg(long, default_value_t = 200_000)]
|
||||
verify_sample: usize,
|
||||
|
||||
/// Compare every k-mer of every layer (slow).
|
||||
#[arg(long)]
|
||||
verify_full: bool,
|
||||
|
||||
/// Do everything except installing: convert into staging, verify, discard.
|
||||
#[arg(long)]
|
||||
dry_run: bool,
|
||||
|
||||
/// Only these partitions (repeatable). The index-level annexes are then left alone.
|
||||
#[arg(long)]
|
||||
partition: Vec<usize>,
|
||||
|
||||
/// Only these layers of each partition (repeatable). The index-level annexes are then left alone.
|
||||
#[arg(long)]
|
||||
layer: Vec<usize>,
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| "info".into()))
|
||||
.init();
|
||||
let cli = Cli::parse();
|
||||
if let Some(n) = cli.cpu_max {
|
||||
obisys::set_cpu_cap(n);
|
||||
}
|
||||
|
||||
// Holds the index's directory lock for the whole run, and sets k.
|
||||
let index = oki(KmerIndex::open_lock(&cli.index))?;
|
||||
let n_partitions = index.n_partitions();
|
||||
let partial = !cli.partition.is_empty() || !cli.layer.is_empty();
|
||||
let order: Vec<usize> = if cli.partition.is_empty() { (0..n_partitions).collect() } else { cli.partition.clone() };
|
||||
ensure!(order.iter().all(|&p| p < n_partitions), "index has only {n_partitions} partitions");
|
||||
|
||||
let opts = Options { verify_sample: cli.verify_sample, verify_full: cli.verify_full, dry_run: cli.dry_run };
|
||||
let converted = AtomicUsize::new(0);
|
||||
let skipped = AtomicUsize::new(0);
|
||||
|
||||
let mut done = 0usize;
|
||||
PartitionRunner::new().run(
|
||||
&order,
|
||||
|p| -> Result<Vec<(usize, Outcome)>> {
|
||||
let n_layers = oki(index.n_layers(p))?;
|
||||
let mut outcomes = Vec::new();
|
||||
for l in (0..n_layers).filter(|l| cli.layer.is_empty() || cli.layer.contains(l)) {
|
||||
let dir = index.layer_path(p, l);
|
||||
let outcome = layer::convert(&dir, &opts)
|
||||
.map_err(|e| e.context(format!("partition {p}, layer {l} ({})", dir.display())))?;
|
||||
match outcome {
|
||||
Outcome::Skipped => skipped.fetch_add(1, Ordering::Relaxed),
|
||||
_ => converted.fetch_add(1, Ordering::Relaxed),
|
||||
};
|
||||
outcomes.push((l, outcome));
|
||||
}
|
||||
Ok(outcomes)
|
||||
},
|
||||
|p, outcomes, elapsed| {
|
||||
done += 1;
|
||||
info!("partition {p} ({done}/{}) in {:.1?}: {outcomes:?}", order.len(), elapsed);
|
||||
},
|
||||
)?;
|
||||
|
||||
if !cli.dry_run && !partial {
|
||||
let moved = commit::set_aside(index.dir(), &commit::INDEX_ANNEXES)?;
|
||||
if moved > 0 {
|
||||
info!("{moved} index-level annex(es) set aside under {}", commit::STALE_DIR);
|
||||
}
|
||||
} else if !cli.dry_run {
|
||||
warn!("partial run: index-level annexes (family overlap) not set aside; do it on the final pass");
|
||||
}
|
||||
info!(
|
||||
"done: {} layer(s) {}, {} already converted",
|
||||
converted.load(Ordering::Relaxed),
|
||||
if cli.dry_run { "checked" } else { "converted" },
|
||||
skipped.load(Ordering::Relaxed)
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user