Introduce a new `Sparse` format alongside existing `Columnar` and `Packed` variants, enabling optimized row-major pairwise counting for distance and similarity metrics via the `CountPartials` trait. Update storage detection priorities, extend matrix dispatch logic to sparse backends, and correct diagonal/off-diagonal formulas in bit matrix partial computations. Expand layer APIs with format-agnostic `nonzero_iter`, update usage documentation for the `--sparse` flag, and add comprehensive tests verifying roundtrip integrity and metric equivalence against dense implementations.
618 lines
26 KiB
Rust
618 lines
26 KiB
Rust
use std::fs::{self, File};
|
|
use std::io::{self, BufWriter, Read as _, Write as _};
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use memmap2::Mmap;
|
|
use ndarray::{Array1, Array2};
|
|
|
|
use crate::bitmatrix::{fill_row_generic, par_col_reduce, pairwise2_matrix, pairwise_matrix, row_generic};
|
|
use crate::builder::PersistentCompactIntVecBuilder;
|
|
use crate::colgroup::{chunked_presence_count, ColGroup, MatrixGroupOps};
|
|
use crate::format::{HEADER_SIZE, OVERFLOW_ENTRY_SIZE};
|
|
use crate::meta::MatrixMeta;
|
|
use crate::reader::PersistentCompactIntVec;
|
|
use crate::sparse_intmatrix::{is_present as sparse_is_present, PersistentSparseCompactIntMatrix};
|
|
use crate::storage_kind::StorageKind;
|
|
use crate::tempbitvec::{TempBitVec, TempBitVecBuilder};
|
|
use crate::tempintvec::{TempCompactIntVec, TempCompactIntVecBuilder};
|
|
use crate::views::{nonzero_triples, IntSliceView};
|
|
|
|
pub(crate) fn col_path(dir: &Path, col: usize) -> PathBuf {
|
|
dir.join(format!("col_{col:06}.pciv"))
|
|
}
|
|
|
|
// ── ColumnarCompactIntMatrix ──────────────────────────────────────────────────
|
|
|
|
pub struct ColumnarCompactIntMatrix {
|
|
cols: Vec<PersistentCompactIntVec>,
|
|
n: usize,
|
|
}
|
|
|
|
impl ColumnarCompactIntMatrix {
|
|
pub(crate) fn open(dir: &Path) -> io::Result<Self> {
|
|
let meta = MatrixMeta::load(dir)?;
|
|
let cols = (0..meta.n_cols)
|
|
.map(|c| PersistentCompactIntVec::open(&col_path(dir, c)))
|
|
.collect::<io::Result<Vec<_>>>()?;
|
|
Ok(Self { cols, n: meta.n })
|
|
}
|
|
|
|
#[inline]
|
|
pub(crate) fn n(&self) -> usize { self.n }
|
|
#[inline]
|
|
pub(crate) fn n_cols(&self) -> usize { self.cols.len() }
|
|
#[inline]
|
|
pub(crate) fn col(&self, c: usize) -> &PersistentCompactIntVec { &self.cols[c] }
|
|
|
|
#[inline]
|
|
pub(crate) fn row(&self, slot: usize) -> Box<[u32]> {
|
|
row_generic(slot, self.n_cols(), |c, s| self.col(c).get(s))
|
|
}
|
|
|
|
#[inline]
|
|
pub(crate) fn fill_row(&self, slot: usize, buf: &mut [u32]) {
|
|
fill_row_generic(slot, buf, |c, s| self.col(c).get(s))
|
|
}
|
|
|
|
pub(crate) fn sum(&self) -> Array1<u64> {
|
|
par_col_reduce(self.n_cols(), |c| self.col(c).sum())
|
|
}
|
|
|
|
pub(crate) fn count_nonzero(&self) -> Array1<u64> {
|
|
par_col_reduce(self.n_cols(), |c| self.col(c).count_nonzero())
|
|
}
|
|
|
|
pub(crate) fn partial_bray_dist_matrix(&self) -> Array2<u64> {
|
|
pairwise_matrix(self.n_cols(), |i, j| self.col(i).partial_bray_dist(self.col(j)))
|
|
}
|
|
pub(crate) fn partial_euclidean_dist_matrix(&self) -> Array2<f64> {
|
|
pairwise_matrix(self.n_cols(), |i, j| self.col(i).partial_euclidean_dist(self.col(j)))
|
|
}
|
|
pub(crate) fn partial_threshold_jaccard_dist_matrix(&self, threshold: u32) -> (Array2<u64>, Array2<u64>) {
|
|
pairwise2_matrix(self.n_cols(), |i, j| self.col(i).partial_threshold_jaccard_dist(self.col(j), threshold))
|
|
}
|
|
pub(crate) fn partial_relfreq_bray_dist_matrix(&self, col_sums: &Array1<u64>) -> Array2<f64> {
|
|
pairwise_matrix(self.n_cols(), |i, j| {
|
|
self.col(i).partial_relfreq_bray_dist(self.col(j), col_sums[i] as f64, col_sums[j] as f64)
|
|
})
|
|
}
|
|
pub(crate) fn partial_relfreq_euclidean_dist_matrix(&self, col_sums: &Array1<u64>) -> Array2<f64> {
|
|
pairwise_matrix(self.n_cols(), |i, j| {
|
|
self.col(i).partial_relfreq_euclidean_dist(self.col(j), col_sums[i] as f64, col_sums[j] as f64)
|
|
})
|
|
}
|
|
pub(crate) fn partial_hellinger_euclidean_dist_matrix(&self, col_sums: &Array1<u64>) -> Array2<f64> {
|
|
pairwise_matrix(self.n_cols(), |i, j| {
|
|
self.col(i).partial_hellinger_euclidean_dist(self.col(j), col_sums[i] as f64, col_sums[j] as f64)
|
|
})
|
|
}
|
|
|
|
pub(crate) fn append_column(dir: &Path, value_of: impl Fn(usize) -> u32) -> io::Result<()> {
|
|
let mut meta = MatrixMeta::load(dir)?;
|
|
let mut b = PersistentCompactIntVecBuilder::new(meta.n, &col_path(dir, meta.n_cols))?;
|
|
for slot in 0..meta.n { b.set(slot, value_of(slot)); }
|
|
b.close()?;
|
|
meta.n_cols += 1;
|
|
meta.save(dir)
|
|
}
|
|
}
|
|
|
|
// ── PackedCompactIntMatrix ────────────────────────────────────────────────────
|
|
|
|
const PCMX_MAGIC: [u8; 4] = *b"PCMX";
|
|
const PCMX_HEADER: usize = 24; // magic(4) + pad(4) + n_rows(8) + n_cols(8)
|
|
|
|
struct ColInfo {
|
|
primary_start: usize,
|
|
data_offset: usize,
|
|
n_overflow: usize,
|
|
}
|
|
|
|
pub struct PackedCompactIntMatrix {
|
|
mmap: Mmap,
|
|
n_rows: usize,
|
|
n_cols: usize,
|
|
columns: Vec<ColInfo>,
|
|
}
|
|
|
|
impl PackedCompactIntMatrix {
|
|
pub(crate) fn open(path: &Path) -> io::Result<Self> {
|
|
let mmap = unsafe { Mmap::map(&File::open(path)?)? };
|
|
if mmap.len() < PCMX_HEADER {
|
|
return Err(io::Error::new(io::ErrorKind::InvalidData, "PCMX file too short"));
|
|
}
|
|
if &mmap[0..4] != &PCMX_MAGIC {
|
|
return Err(io::Error::new(io::ErrorKind::InvalidData, "bad PCMX magic"));
|
|
}
|
|
let n_rows = u64::from_le_bytes(mmap[8..16].try_into().unwrap()) as usize;
|
|
let n_cols = u64::from_le_bytes(mmap[16..24].try_into().unwrap()) as usize;
|
|
|
|
let mut columns = Vec::with_capacity(n_cols);
|
|
for c in 0..n_cols {
|
|
let off_pos = PCMX_HEADER + c * 8;
|
|
let col_base = u64::from_le_bytes(mmap[off_pos..off_pos+8].try_into().unwrap()) as usize;
|
|
let n_ov = u64::from_le_bytes(mmap[col_base+16..col_base+24].try_into().unwrap()) as usize;
|
|
let n_pciv = u64::from_le_bytes(mmap[col_base+8..col_base+16].try_into().unwrap()) as usize;
|
|
let primary_start = col_base + HEADER_SIZE;
|
|
let data_offset = primary_start + n_pciv;
|
|
columns.push(ColInfo { primary_start, data_offset, n_overflow: n_ov });
|
|
}
|
|
Ok(Self { mmap, n_rows, n_cols, columns })
|
|
}
|
|
|
|
#[inline]
|
|
pub(crate) fn col_view(&self, c: usize) -> IntSliceView<'_> {
|
|
let ci = &self.columns[c];
|
|
let primary = &self.mmap[ci.primary_start..ci.primary_start + self.n_rows];
|
|
let overflow_raw = &self.mmap[ci.data_offset..ci.data_offset + ci.n_overflow * OVERFLOW_ENTRY_SIZE];
|
|
IntSliceView::new(primary, overflow_raw, ci.n_overflow, self.n_rows)
|
|
}
|
|
|
|
pub(crate) fn col_persist(&self, c: usize, path: &Path) -> io::Result<PersistentCompactIntVecBuilder> {
|
|
let view = self.col_view(c);
|
|
let overflow: std::collections::HashMap<usize, u32> = view.overflow_entries().collect();
|
|
PersistentCompactIntVecBuilder::from_raw_primary(view.primary_bytes(), overflow, path)
|
|
}
|
|
|
|
#[inline]
|
|
pub(crate) fn get(&self, col: usize, slot: usize) -> u32 { self.col_view(col).get(slot) }
|
|
|
|
#[inline]
|
|
pub(crate) fn fill_row(&self, slot: usize, buf: &mut [u32]) {
|
|
fill_row_generic(slot, buf, |c, s| self.get(c, s))
|
|
}
|
|
|
|
#[inline]
|
|
pub(crate) fn row(&self, slot: usize) -> Box<[u32]> {
|
|
row_generic(slot, self.n_cols, |c, s| self.get(c, s))
|
|
}
|
|
|
|
pub(crate) fn sum(&self) -> Array1<u64> {
|
|
par_col_reduce(self.n_cols, |c| self.col_view(c).sum())
|
|
}
|
|
|
|
pub(crate) fn count_nonzero(&self) -> Array1<u64> {
|
|
par_col_reduce(self.n_cols, |c| self.col_view(c).count_nonzero())
|
|
}
|
|
|
|
// Every pair formula below delegates to `IntSliceView` (`views.rs`) —
|
|
// the canonical implementation, also used by `ColumnarCompactIntMatrix`
|
|
// via `PersistentCompactIntVec`. No formula is re-derived here.
|
|
|
|
pub(crate) fn partial_bray_dist_matrix(&self) -> Array2<u64> {
|
|
pairwise_matrix(self.n_cols, |i, j| self.col_view(i).partial_bray_dist(self.col_view(j)))
|
|
}
|
|
pub(crate) fn partial_euclidean_dist_matrix(&self) -> Array2<f64> {
|
|
pairwise_matrix(self.n_cols, |i, j| self.col_view(i).partial_euclidean_dist(self.col_view(j)))
|
|
}
|
|
pub(crate) fn partial_threshold_jaccard_dist_matrix(&self, t: u32) -> (Array2<u64>, Array2<u64>) {
|
|
pairwise2_matrix(self.n_cols, |i, j| self.col_view(i).partial_threshold_jaccard_dist(self.col_view(j), t))
|
|
}
|
|
pub(crate) fn partial_relfreq_bray_dist_matrix(&self, col_sums: &Array1<u64>) -> Array2<f64> {
|
|
pairwise_matrix(self.n_cols, |i, j| {
|
|
self.col_view(i).partial_relfreq_bray_dist(self.col_view(j), col_sums[i] as f64, col_sums[j] as f64)
|
|
})
|
|
}
|
|
pub(crate) fn partial_relfreq_euclidean_dist_matrix(&self, col_sums: &Array1<u64>) -> Array2<f64> {
|
|
pairwise_matrix(self.n_cols, |i, j| {
|
|
self.col_view(i).partial_relfreq_euclidean_dist(self.col_view(j), col_sums[i] as f64, col_sums[j] as f64)
|
|
})
|
|
}
|
|
pub(crate) fn partial_hellinger_euclidean_dist_matrix(&self, col_sums: &Array1<u64>) -> Array2<f64> {
|
|
pairwise_matrix(self.n_cols, |i, j| {
|
|
self.col_view(i).partial_hellinger_euclidean_dist(self.col_view(j), col_sums[i] as f64, col_sums[j] as f64)
|
|
})
|
|
}
|
|
}
|
|
|
|
/// Reads just the `n_cols` field from an existing packed matrix's header,
|
|
/// without mapping the file. Used by `pack_compact_int_matrix` to tell a
|
|
/// genuinely complete pack from a stale one that predates a later
|
|
/// column-widening.
|
|
fn packed_int_matrix_n_cols(path: &Path) -> io::Result<usize> {
|
|
let mut f = File::open(path)?;
|
|
let mut header = [0u8; PCMX_HEADER];
|
|
f.read_exact(&mut header)?;
|
|
Ok(u64::from_le_bytes(header[16..24].try_into().unwrap()) as usize)
|
|
}
|
|
|
|
/// Build `counts/matrix.pcmx` from existing `col_*.pciv` files.
|
|
pub fn pack_compact_int_matrix(dir: &Path) -> io::Result<()> {
|
|
let packed_path = dir.join("matrix.pcmx");
|
|
|
|
let meta = match MatrixMeta::load(dir) {
|
|
Ok(meta) => meta,
|
|
Err(e) => {
|
|
// No columnar data pending: either this layer was already
|
|
// packed and cleaned up (matrix.pcmx complete, nothing left to
|
|
// do), or genuinely nothing was ever written here.
|
|
return if packed_path.exists() { Ok(()) } else { Err(e) };
|
|
}
|
|
};
|
|
|
|
// A `matrix.pcmx` can already exist here even though columnar data is
|
|
// still pending — e.g. copied verbatim from a merge's base source
|
|
// before this layer was widened with more genome columns (see
|
|
// `obikindex::index::merge_layer::merge_partition`). Only skip (re-)packing if the
|
|
// existing file already reflects the current column count; otherwise
|
|
// the columnar files are newer and must be (re-)packed, overwriting the
|
|
// stale one — never silently discarded as "leftover cleanup".
|
|
if packed_int_matrix_n_cols(&packed_path).ok() == Some(meta.n_cols) {
|
|
for c in 0..meta.n_cols { let _ = fs::remove_file(col_path(dir, c)); }
|
|
let _ = fs::remove_file(dir.join("meta.json"));
|
|
return Ok(());
|
|
}
|
|
|
|
let n_cols = meta.n_cols;
|
|
let col_sizes: Vec<u64> = (0..n_cols)
|
|
.map(|c| fs::metadata(col_path(dir, c)).map(|m| m.len()))
|
|
.collect::<io::Result<_>>()?;
|
|
let header_size = (PCMX_HEADER + n_cols * 8) as u64;
|
|
let mut col_offset = header_size;
|
|
let mut offsets = Vec::with_capacity(n_cols);
|
|
for &size in &col_sizes { offsets.push(col_offset); col_offset += size; }
|
|
let tmp_path = dir.join("matrix.pcmx.tmp");
|
|
let mut out = BufWriter::new(File::create(&tmp_path)?);
|
|
out.write_all(&PCMX_MAGIC)?;
|
|
out.write_all(&[0u8; 4])?;
|
|
out.write_all(&(meta.n as u64).to_le_bytes())?;
|
|
out.write_all(&(n_cols as u64).to_le_bytes())?;
|
|
for &off in &offsets { out.write_all(&off.to_le_bytes())?; }
|
|
for c in 0..n_cols { io::copy(&mut File::open(col_path(dir, c))?, &mut out)?; }
|
|
out.flush()?;
|
|
drop(out);
|
|
fs::rename(&tmp_path, &packed_path)?;
|
|
for c in 0..n_cols { fs::remove_file(col_path(dir, c))?; }
|
|
fs::remove_file(dir.join("meta.json"))?;
|
|
Ok(())
|
|
}
|
|
|
|
// ── PersistentCompactIntMatrix — public enum ──────────────────────────────────
|
|
|
|
pub enum PersistentCompactIntMatrix {
|
|
Columnar(ColumnarCompactIntMatrix),
|
|
Packed(PackedCompactIntMatrix),
|
|
Sparse(PersistentSparseCompactIntMatrix),
|
|
}
|
|
|
|
impl PersistentCompactIntMatrix {
|
|
/// Checks (in order): `counts/matrix.pcmx` → Packed; `counts/meta.json`
|
|
/// → Columnar; `counts/singleton_values.pciv` → Sparse. Mirrors
|
|
/// `PersistentBitMatrix::open`'s own Packed → Columnar → Sparse
|
|
/// priority order.
|
|
pub fn open(layer_dir: &Path) -> io::Result<Self> {
|
|
let counts_dir = layer_dir.join("counts");
|
|
if counts_dir.join("matrix.pcmx").exists() {
|
|
return Ok(Self::Packed(PackedCompactIntMatrix::open(&counts_dir.join("matrix.pcmx"))?));
|
|
}
|
|
if MatrixMeta::load(&counts_dir).is_ok() {
|
|
return Ok(Self::Columnar(ColumnarCompactIntMatrix::open(&counts_dir)?));
|
|
}
|
|
if sparse_is_present(&counts_dir) {
|
|
return Ok(Self::Sparse(PersistentSparseCompactIntMatrix::open(&counts_dir)?));
|
|
}
|
|
Err(io::Error::new(
|
|
io::ErrorKind::NotFound,
|
|
format!("no count matrix found in {} — run 'obikmer upgrade'", layer_dir.display()),
|
|
))
|
|
}
|
|
|
|
/// This already-open matrix's storage format — reads the discriminant
|
|
/// already in memory, no disk access.
|
|
#[inline]
|
|
pub fn storage_kind(&self) -> StorageKind {
|
|
match self {
|
|
Self::Columnar(_) => StorageKind::Columnar,
|
|
Self::Packed(_) => StorageKind::Packed,
|
|
Self::Sparse(_) => StorageKind::Sparse,
|
|
}
|
|
}
|
|
|
|
/// Lightweight disk probe: which format `open` would pick, without
|
|
/// opening (mmap'ing) anything. Mirrors `open`'s own priority order by
|
|
/// hand — keep the two in sync if that order ever changes.
|
|
pub fn detect_storage(layer_dir: &Path) -> io::Result<StorageKind> {
|
|
let counts_dir = layer_dir.join("counts");
|
|
if counts_dir.join("matrix.pcmx").exists() {
|
|
return Ok(StorageKind::Packed);
|
|
}
|
|
if MatrixMeta::load(&counts_dir).is_ok() {
|
|
return Ok(StorageKind::Columnar);
|
|
}
|
|
if sparse_is_present(&counts_dir) {
|
|
return Ok(StorageKind::Sparse);
|
|
}
|
|
Err(io::Error::new(
|
|
io::ErrorKind::NotFound,
|
|
format!("no count matrix found in {}", layer_dir.display()),
|
|
))
|
|
}
|
|
|
|
#[inline]
|
|
pub fn n(&self) -> usize {
|
|
match self { Self::Columnar(m) => m.n(), Self::Packed(m) => m.n_rows, Self::Sparse(m) => m.n() }
|
|
}
|
|
#[inline]
|
|
pub fn n_cols(&self) -> usize {
|
|
match self { Self::Columnar(m) => m.n_cols(), Self::Packed(m) => m.n_cols, Self::Sparse(m) => m.n_cols() }
|
|
}
|
|
|
|
#[inline]
|
|
pub fn col(&self, c: usize) -> &PersistentCompactIntVec {
|
|
match self {
|
|
Self::Columnar(m) => m.col(c),
|
|
_ => panic!("col() only available on Columnar PersistentCompactIntMatrix"),
|
|
}
|
|
}
|
|
|
|
#[inline]
|
|
pub fn col_view(&self, c: usize) -> IntSliceView<'_> {
|
|
match self {
|
|
Self::Columnar(m) => m.col(c).view(),
|
|
Self::Packed(m) => m.col_view(c),
|
|
Self::Sparse(_) => panic!("col_view() not available on Sparse PersistentCompactIntMatrix"),
|
|
}
|
|
}
|
|
|
|
pub fn col_persist(&self, c: usize, path: &Path) -> io::Result<PersistentCompactIntVecBuilder> {
|
|
match self {
|
|
Self::Columnar(m) => PersistentCompactIntVecBuilder::build_from(m.col(c), path),
|
|
Self::Packed(m) => m.col_persist(c, path),
|
|
Self::Sparse(_) => Err(io::Error::new(io::ErrorKind::Unsupported,
|
|
"col_persist not available on Sparse PersistentCompactIntMatrix")),
|
|
}
|
|
}
|
|
|
|
#[inline]
|
|
pub fn row(&self, slot: usize) -> Box<[u32]> {
|
|
match self { Self::Columnar(m) => m.row(slot), Self::Packed(m) => m.row(slot), Self::Sparse(m) => m.row(slot) }
|
|
}
|
|
#[inline]
|
|
pub fn fill_row(&self, slot: usize, buf: &mut [u32]) {
|
|
match self {
|
|
Self::Columnar(m) => m.fill_row(slot, buf),
|
|
Self::Packed(m) => m.fill_row(slot, buf),
|
|
Self::Sparse(m) => m.fill_row(slot, buf),
|
|
}
|
|
}
|
|
|
|
/// Extract a sub-matrix containing only the rows at `slots`.
|
|
///
|
|
/// Returns a column-first `Vec<Vec<u32>>`: the outer `Vec` has one entry per
|
|
/// column, and each inner `Vec` contains the values for the requested rows
|
|
/// in the same order as `slots`. Column access is sequential to maximize
|
|
/// cache efficiency on the underlying mmap.
|
|
pub fn sub_matrix(&self, slots: &[usize]) -> Vec<Vec<u32>> {
|
|
let mut out: Vec<Vec<u32>> = (0..self.n_cols()).map(|_| Vec::new()).collect();
|
|
self.fill_sub_matrix(slots, &mut out);
|
|
out
|
|
}
|
|
|
|
/// Like [`sub_matrix`](Self::sub_matrix), but fills caller-provided column
|
|
/// buffers to avoid allocating the outer `Vec`.
|
|
///
|
|
/// `out` must have length `self.n_cols()`. Each `out[c]` is cleared,
|
|
/// resized to `slots.len()`, and filled with the values for column `c`
|
|
/// in the same order as `slots`.
|
|
///
|
|
/// Derived from [`nonzero_iter`](Self::nonzero_iter) — one traversal per
|
|
/// format, no separate `Sparse`-specific scatter loop duplicated here.
|
|
/// Mirrors `PersistentBitMatrix::fill_sub_matrix`'s own reasoning (see
|
|
/// that method's doc comment).
|
|
pub fn fill_sub_matrix(&self, slots: &[usize], out: &mut [Vec<u32>]) {
|
|
assert_eq!(out.len(), self.n_cols());
|
|
for col in out.iter_mut() {
|
|
col.clear();
|
|
col.resize(slots.len(), 0);
|
|
}
|
|
for (i, c, v) in self.nonzero_iter(slots) {
|
|
out[c][i] = v;
|
|
}
|
|
}
|
|
|
|
/// Yields every nonzero `(idx into slots, col, value)` triple.
|
|
///
|
|
/// One primitive per format, each choosing its own natural traversal —
|
|
/// same shape as `PersistentBitMatrix::nonzero_iter`: `Sparse` delegates
|
|
/// to its own row-major decode (no `n_cols`-wide buffer, ever);
|
|
/// `Columnar`/`Packed` reuse the shared sorted-slot batching in
|
|
/// `views::nonzero_triples`. Boxed, not `impl Iterator`, because the
|
|
/// match arms are genuinely different concrete types.
|
|
pub fn nonzero_iter<'a>(&'a self, slots: &'a [usize]) -> Box<dyn Iterator<Item = (usize, usize, u32)> + 'a> {
|
|
match self {
|
|
Self::Sparse(m) => Box::new(m.nonzero_iter(slots)),
|
|
Self::Columnar(_) | Self::Packed(_) => {
|
|
Box::new(nonzero_triples(slots, self.n_cols(), |c| self.col_view(c), false))
|
|
}
|
|
}
|
|
}
|
|
|
|
#[inline]
|
|
pub fn sum(&self) -> Array1<u64> {
|
|
match self { Self::Columnar(m) => m.sum(), Self::Packed(m) => m.sum(), Self::Sparse(m) => m.sum() }
|
|
}
|
|
#[inline]
|
|
pub fn count_nonzero(&self) -> Array1<u64> {
|
|
match self {
|
|
Self::Columnar(m) => m.count_nonzero(),
|
|
Self::Packed(m) => m.count_nonzero(),
|
|
Self::Sparse(m) => m.count_nonzero(),
|
|
}
|
|
}
|
|
#[inline]
|
|
pub fn partial_bray_dist_matrix(&self) -> Array2<u64> {
|
|
match self {
|
|
Self::Columnar(m) => m.partial_bray_dist_matrix(),
|
|
Self::Packed(m) => m.partial_bray_dist_matrix(),
|
|
Self::Sparse(m) => CountPartials::partial_bray(m),
|
|
}
|
|
}
|
|
#[inline]
|
|
pub fn partial_euclidean_dist_matrix(&self) -> Array2<f64> {
|
|
match self {
|
|
Self::Columnar(m) => m.partial_euclidean_dist_matrix(),
|
|
Self::Packed(m) => m.partial_euclidean_dist_matrix(),
|
|
Self::Sparse(m) => CountPartials::partial_euclidean(m),
|
|
}
|
|
}
|
|
#[inline]
|
|
pub fn partial_threshold_jaccard_dist_matrix(&self, threshold: u32) -> (Array2<u64>, Array2<u64>) {
|
|
match self {
|
|
Self::Columnar(m) => m.partial_threshold_jaccard_dist_matrix(threshold),
|
|
Self::Packed(m) => m.partial_threshold_jaccard_dist_matrix(threshold),
|
|
Self::Sparse(m) => CountPartials::partial_threshold_jaccard(m, threshold),
|
|
}
|
|
}
|
|
#[inline]
|
|
pub fn partial_relfreq_bray_dist_matrix(&self, col_sums: &Array1<u64>) -> Array2<f64> {
|
|
match self {
|
|
Self::Columnar(m) => m.partial_relfreq_bray_dist_matrix(col_sums),
|
|
Self::Packed(m) => m.partial_relfreq_bray_dist_matrix(col_sums),
|
|
Self::Sparse(m) => CountPartials::partial_relfreq_bray(m, col_sums),
|
|
}
|
|
}
|
|
#[inline]
|
|
pub fn partial_relfreq_euclidean_dist_matrix(&self, col_sums: &Array1<u64>) -> Array2<f64> {
|
|
match self {
|
|
Self::Columnar(m) => m.partial_relfreq_euclidean_dist_matrix(col_sums),
|
|
Self::Packed(m) => m.partial_relfreq_euclidean_dist_matrix(col_sums),
|
|
Self::Sparse(m) => CountPartials::partial_relfreq_euclidean(m, col_sums),
|
|
}
|
|
}
|
|
#[inline]
|
|
pub fn partial_hellinger_euclidean_dist_matrix(&self, col_sums: &Array1<u64>) -> Array2<f64> {
|
|
match self {
|
|
Self::Columnar(m) => m.partial_hellinger_euclidean_dist_matrix(col_sums),
|
|
Self::Packed(m) => m.partial_hellinger_euclidean_dist_matrix(col_sums),
|
|
Self::Sparse(m) => CountPartials::partial_hellinger(m, col_sums),
|
|
}
|
|
}
|
|
#[inline]
|
|
pub fn append_column(dir: &Path, value_of: impl Fn(usize) -> u32) -> io::Result<()> {
|
|
ColumnarCompactIntMatrix::append_column(dir, value_of)
|
|
}
|
|
}
|
|
|
|
// ── Trait impls ───────────────────────────────────────────────────────────────
|
|
|
|
use crate::traits::{ColumnWeights, CountPartials};
|
|
|
|
impl ColumnWeights for PersistentCompactIntMatrix {
|
|
#[inline]
|
|
fn col_weights(&self) -> Array1<u64> { self.sum() }
|
|
#[inline]
|
|
fn partial_kmer_counts(&self) -> Array1<u64> { self.count_nonzero() }
|
|
}
|
|
|
|
impl CountPartials for PersistentCompactIntMatrix {
|
|
#[inline]
|
|
fn partial_bray(&self) -> Array2<u64> { self.partial_bray_dist_matrix() }
|
|
#[inline]
|
|
fn partial_euclidean(&self) -> Array2<f64> { self.partial_euclidean_dist_matrix() }
|
|
#[inline]
|
|
fn partial_threshold_jaccard(&self, t: u32) -> (Array2<u64>, Array2<u64>) { self.partial_threshold_jaccard_dist_matrix(t) }
|
|
#[inline]
|
|
fn partial_relfreq_bray(&self, g: &Array1<u64>) -> Array2<f64> { self.partial_relfreq_bray_dist_matrix(g) }
|
|
#[inline]
|
|
fn partial_relfreq_euclidean(&self, g: &Array1<u64>) -> Array2<f64> { self.partial_relfreq_euclidean_dist_matrix(g) }
|
|
#[inline]
|
|
fn partial_hellinger(&self, g: &Array1<u64>) -> Array2<f64> { self.partial_hellinger_euclidean_dist_matrix(g) }
|
|
}
|
|
|
|
// ── Builder ───────────────────────────────────────────────────────────────────
|
|
|
|
pub struct PersistentCompactIntMatrixBuilder {
|
|
dir: PathBuf,
|
|
n: usize,
|
|
n_cols: usize,
|
|
}
|
|
|
|
impl PersistentCompactIntMatrixBuilder {
|
|
pub fn new(n: usize, dir: &Path) -> io::Result<Self> {
|
|
fs::create_dir_all(dir)?;
|
|
Ok(Self { dir: dir.to_path_buf(), n, n_cols: 0 })
|
|
}
|
|
|
|
/// Resume appending columns to a Columnar matrix directory already
|
|
/// closed by a previous builder session — reads `n`/`n_cols` from
|
|
/// `meta.json` instead of starting a fresh matrix at `n_cols == 0`.
|
|
/// Lets callers that build columns incrementally over time (e.g. a
|
|
/// streamed merge pipeline) reach `add_col`/`close` without knowing the
|
|
/// on-disk column naming or `MatrixMeta` themselves.
|
|
pub fn resume(dir: &Path) -> io::Result<Self> {
|
|
let meta = MatrixMeta::load(dir)?;
|
|
Ok(Self { dir: dir.to_path_buf(), n: meta.n, n_cols: meta.n_cols })
|
|
}
|
|
|
|
#[inline]
|
|
pub fn n(&self) -> usize { self.n }
|
|
#[inline]
|
|
pub fn n_cols(&self) -> usize { self.n_cols }
|
|
pub fn add_col(&mut self) -> io::Result<PersistentCompactIntVecBuilder> {
|
|
let path = col_path(&self.dir, self.n_cols);
|
|
self.n_cols += 1;
|
|
PersistentCompactIntVecBuilder::new(self.n, &path)
|
|
}
|
|
|
|
pub fn add_col_from(&mut self, src: &TempCompactIntVec) -> io::Result<()> {
|
|
src.make_persistent(&col_path(&self.dir, self.n_cols))?;
|
|
self.n_cols += 1;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn add_col_from_bit(&mut self, src: &TempBitVec) -> io::Result<()> {
|
|
let path = col_path(&self.dir, self.n_cols);
|
|
self.n_cols += 1;
|
|
let mut b = PersistentCompactIntVecBuilder::new(self.n, &path)?;
|
|
b.inc_present(src.view());
|
|
b.close()
|
|
}
|
|
|
|
pub fn close(self) -> io::Result<()> {
|
|
MatrixMeta { n: self.n, n_cols: self.n_cols }.save(&self.dir)
|
|
}
|
|
}
|
|
|
|
// ── MatrixGroupOps ────────────────────────────────────────────────────────────
|
|
|
|
impl MatrixGroupOps for PersistentCompactIntMatrix {
|
|
fn partial_group_presence_count(&self, g: &ColGroup, threshold: u32) -> io::Result<TempCompactIntVec> {
|
|
chunked_presence_count(self.n(), &g.indices, |b, c| {
|
|
b.inc_predicate_fast(self.col_view(c), |v| v >= threshold)
|
|
})
|
|
}
|
|
|
|
fn partial_group_sum(&self, g: &ColGroup) -> io::Result<TempCompactIntVec> {
|
|
let n = self.n();
|
|
let mut result = TempCompactIntVecBuilder::new(n)?;
|
|
for &c in &g.indices { result.add(self.col_view(c)); }
|
|
result.freeze()
|
|
}
|
|
|
|
fn partial_group_any(&self, g: &ColGroup, threshold: u32) -> io::Result<TempBitVec> {
|
|
let n = self.n();
|
|
let mut result = TempBitVecBuilder::new(n)?;
|
|
for &c in &g.indices {
|
|
result.or_where(self.col_view(c), |v| v >= threshold);
|
|
}
|
|
result.freeze()
|
|
}
|
|
|
|
fn partial_group_min(&self, g: &ColGroup) -> io::Result<TempCompactIntVec> {
|
|
let n = self.n();
|
|
let mut result = TempCompactIntVecBuilder::new(n)?;
|
|
if let Some((&first, rest)) = g.indices.split_first() {
|
|
result.add(self.col_view(first));
|
|
for &c in rest { result.min(self.col_view(c)); }
|
|
}
|
|
result.freeze()
|
|
}
|
|
|
|
fn partial_group_max(&self, g: &ColGroup) -> io::Result<TempCompactIntVec> {
|
|
let n = self.n();
|
|
let mut result = TempCompactIntVecBuilder::new(n)?;
|
|
for &c in &g.indices { result.max(self.col_view(c)); }
|
|
result.freeze()
|
|
}
|
|
}
|