Refactor the core compact vector library to support sparse matrices, overflow handling, and improved distance metrics. Update phylogenetic algorithms to handle ambiguity policies and quartet conventions, and standardize error handling and CLI argument parsing across all tools. - `src/obicompactvec`: Add support for sparse bit matrices and overflow handling in compact vectors; update distance metrics (Jaccard, Bray, Euclidean) to accept explicit sums and thresholds; add `transpose` module and refine `PersistentBitMatrix`/`PersistentSparseBitMatrix` APIs. - `src/obicompactvec/src/bitmatrix`: Implement `PersistentSparseBitMatrix` and `PersistentSparseBitMatrixBuilder`; update pairwise distance calculations to handle implicit and sparse types; add `partial_group_presence_count` for group operations. - `src/obicompactvec/src/tests`: Update tests to use new sparse matrix APIs, explicit `.close().unwrap()` chains, and refined assertions for distance symmetry and diagonal checks. - `src/obidebruinj`: Refactor De Bruijn graph traversal to use atomic fetch-or for visited masks and explicit direction checks in `unitig_nucleotides` and `for_each_unitig`. - `src/obifastwrite`: Update `annotation` to accept slice of tuples; refine `write_scatter` and `write_count` output structures; improve ID extraction in `different_sequences_different_id`. - `src/obikdump`: Refactor `dump` and `write_unitigs` to use parallel iterators (`rayon::into_par_iter`) and updated `IndexCache` imports for kmer partitioning. - `src/obikentropy`: Update `emit_f64_2d` formatting; refine entropy tests to use `Kmer::from_ascii` directly and multi-line assertions for range checks. - `src/obikfilter`: Add `MinComplexity` filter; update `passes` logic for ingroup/outgroup counts with explicit threshold checks; refactor `filter_partition` to use closures for kmer insertion. - `src/obikidxcache`: Update `iter_partition` and `iter_indexed` to handle explicit partition arguments and collect layer cache keys before iteration. - `src/obikindex`: Refactor `IndexBuilder` and `KmerIndex` to explicitly initialize `meta` and `lock`; update `OKIError` to include static context and string details; add `TypedLayer` row presence histogram support. - `src/obikindexer`: Update `build_index_layer` to handle zero layers and flatten `mphf1.bin` loading; refactor `graph_pipeline` to explicitly define transformations and sinks for `KmerGraphData`. - `src/obikmer`: Refactor CLI commands (`annotate`, `convert`, `dump`, `filter`, `pack`, `select`, `unitig`) to use explicit error handling with `unwrap_or_else` and updated metadata access; add `query` and `select` modules. - `src/obikmer/src/cmd/phylo`: Update phylogenetic output writers (`iqtree`, `phyg`, `phylip`, `sankoff`, `tnt`) to handle explicit genome indices, sites, and alignment structures; add `shared` module for shared kmer writing. - `src/obikmerge`: Refactor `merge` to validate source compatibility with specific error messages; update pass 2 logic to stream layer unitigs to column builders. - `src/obikphylo`: Overhaul distance metrics to support ambiguity policies and quartet conventions; update `SnpAlignment` to include sites; refactor sibling algorithms to handle masking, entropy, and pair weights explicitly. - `src/obikphylo/src/siblings`: Update `alignment`, `cardcomp`, `entropy`, `masking`, and `pairwise` algorithms to use explicit policy arguments and refined assertions; add `sample_stats` and `subsample` modules. - `src/obikphylo/src/tree`: Refactor `neighbor_joining` and `upgma` to explicitly handle node creation, edge finding, and label cloning. - `src/obikquery`: Add `partition_kmers`, `query_layer`, and `smer_index` modules; update `process_chunk` to use explicit iterator chaining for statistics; refine `from_records` to push kmer descriptions. - `src/obikrebuild`: Refactor `compact_partition` and `reindex` to use explicit block structures and updated error mapping for metadata evidence. - `src/obikselect`: Update `build_output_cols` to handle group operations and default aggregation; refine `select_partition` to explicitly pass thresholds and presence flags. - `src/obikseq`: Update `Kmer` push methods to use bitwise operations with `PhantomData`; refine `PackedSeq` initialization and error handling; add `RoutableSuperKmer` to public API. - `src/obiksession`: Update `Session` to accept `force` parameter for opening; refine `Memoize` to use `get_or_compute_with_key` and explicit byte conversion. - `src/obikstats`: Refactor `SharingNullModel` to directly construct probability vectors; update tests to use fixed `SharingSpectrum` definitions. - `src/obipipeline`: Refactor scheduler macros to wrap transformations in `Box<dyn Fn>` with `Send + Sync`; update `Pipe` and `Pool` to handle explicit error signaling and stage indexing. - `src/obiread`: Add `text/gbff` MIME type; update `read_sequence_chunks_sized` to use explicit `SeqChunkIter` constructors; refine `SeqRecord` initialization. - `src/obiskbuilder`: Add `stream_iter` module for streaming superkmer iteration; update benchmarks to use direct `.count()` calls. - `src/obiskio`: Refactor `SKError` to include static context and string details; update `UnitigFileReader`/`Writer` to use explicit error mapping and block offset handling. - `src/obisys`: Refactor `DirLock` to use explicit `acquire_mode` and `promote`/`demote` logic; update `LockSlot` to handle exclusive/shared states with mutexes; refine `NodeConfig` and `effective_parallelism` tracing. - `src/obitaxonomy`: Refactor `TaxPath` and `TaxPattern` parsing to handle segments and ranks explicitly; update error messages to include segment information; add `segment` and `segment_pattern` modules.
101 lines
2.7 KiB
Rust
101 lines
2.7 KiB
Rust
use std::fs;
|
|
use std::path::PathBuf;
|
|
|
|
const K_MAX: usize = 32;
|
|
const WS_MAX: usize = 6;
|
|
|
|
fn build_n_log_n() -> [f64; K_MAX + 1] {
|
|
let mut t = [0.0f64; K_MAX + 1];
|
|
for n in 1..=K_MAX {
|
|
t[n] = (n as f64) * (n as f64).ln();
|
|
}
|
|
t
|
|
}
|
|
|
|
/// Max achievable entropy over `4^ws` raw sub-words given only `nwords`
|
|
/// observations (most-uniform integer partition), per
|
|
/// `DevDocMD/theory/entropy.md`.
|
|
fn build_emax() -> [[f64; WS_MAX + 1]; K_MAX + 1] {
|
|
let mut t = [[0.0f64; WS_MAX + 1]; K_MAX + 1];
|
|
for k in 2..=K_MAX {
|
|
for ws in 1..=WS_MAX.min(k - 1) {
|
|
let n_raw = 1usize << (ws * 2);
|
|
let nwords = k - ws + 1;
|
|
let c = nwords / n_raw;
|
|
let r = nwords % n_raw;
|
|
let nf = nwords as f64;
|
|
let t1 = if c == 0 || n_raw == r {
|
|
0.0
|
|
} else {
|
|
let f1 = c as f64 / nf;
|
|
(n_raw - r) as f64 * f1 * f1.ln()
|
|
};
|
|
let t2 = if r == 0 {
|
|
0.0
|
|
} else {
|
|
let f2 = (c + 1) as f64 / nf;
|
|
r as f64 * f2 * f2.ln()
|
|
};
|
|
t[k][ws] = -(t1 + t2);
|
|
}
|
|
}
|
|
t
|
|
}
|
|
|
|
fn build_log_nwords() -> [[f64; WS_MAX + 1]; K_MAX + 1] {
|
|
let mut t = [[0.0f64; WS_MAX + 1]; K_MAX + 1];
|
|
for k in 2..=K_MAX {
|
|
for ws in 1..=WS_MAX.min(k - 1) {
|
|
t[k][ws] = ((k - ws + 1) as f64).ln();
|
|
}
|
|
}
|
|
t
|
|
}
|
|
|
|
fn emit_f64_1d(out: &mut String, name: &str, n: usize, values: &[f64]) {
|
|
out.push_str(&format!("pub(crate) const {name}: [f64; {n}] = [\n"));
|
|
for v in values {
|
|
out.push_str(&format!(" {v:?},\n"));
|
|
}
|
|
out.push_str("];\n");
|
|
}
|
|
|
|
fn emit_f64_2d(
|
|
out: &mut String,
|
|
name: &str,
|
|
rows: usize,
|
|
cols: usize,
|
|
values: &[[f64; WS_MAX + 1]],
|
|
) {
|
|
out.push_str(&format!(
|
|
"pub(crate) const {name}: [[f64; {cols}]; {rows}] = [\n"
|
|
));
|
|
for row in values {
|
|
out.push_str(" [");
|
|
for (i, v) in row.iter().enumerate() {
|
|
if i > 0 {
|
|
out.push_str(", ");
|
|
}
|
|
out.push_str(&format!("{v:?}"));
|
|
}
|
|
out.push_str("],\n");
|
|
}
|
|
out.push_str("];\n");
|
|
}
|
|
|
|
fn main() {
|
|
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
|
|
let mut out = String::new();
|
|
|
|
let n_log_n = build_n_log_n();
|
|
emit_f64_1d(&mut out, "N_LOG_N", K_MAX + 1, &n_log_n);
|
|
|
|
let emax = build_emax();
|
|
emit_f64_2d(&mut out, "EMAX", K_MAX + 1, WS_MAX + 1, &emax);
|
|
|
|
let log_nwords = build_log_nwords();
|
|
emit_f64_2d(&mut out, "LOG_NWORDS", K_MAX + 1, WS_MAX + 1, &log_nwords);
|
|
|
|
fs::write(out_dir.join("entropy_tables.rs"), out).unwrap();
|
|
}
|