- Introduce lazy_static dependency - Refactor encoding: rename encode_base →encode_nuc and make it pub(crate) - Add from_raw_right/raw Right methods to Kmer for right-aligned handling - Improve error message formatting and code readability in kmod.rs tests - Replace inline entropy computation with precomputed tables (entropy_table module)—using LazyLock for static lookup arrays - Simplify EntropyFilter by removing redundant tables and delegating to new entropy_table API - Add RollingStat module for real-time kmer statistics and minimizer tracking - Reorganize modules: move iter, encoding to pub(crate), add entropy_table and rolling_stat - Update imports across obiskbuilder crate accordingly
176 lines
5.8 KiB
Rust
176 lines
5.8 KiB
Rust
use obikseq::kmer::Kmer;
|
|
use std::sync::LazyLock;
|
|
|
|
pub(crate) static NORMK1: LazyLock<[u64; 4]> = LazyLock::new(|| build_normalized_kmer::<4>());
|
|
pub(crate) static NORMK2: LazyLock<[u64; 16]> = LazyLock::new(|| build_normalized_kmer::<16>());
|
|
pub(crate) static NORMK3: LazyLock<[u64; 64]> = LazyLock::new(|| build_normalized_kmer::<64>());
|
|
pub(crate) static NORMK4: LazyLock<[u64; 256]> = LazyLock::new(|| build_normalized_kmer::<256>());
|
|
pub(crate) static NORMK5: LazyLock<[u64; 1024]> = LazyLock::new(|| build_normalized_kmer::<1024>());
|
|
pub(crate) static NORMK6: LazyLock<[u64; 4096]> = LazyLock::new(|| build_normalized_kmer::<4096>());
|
|
|
|
pub(crate) static LN_CARD_ROT1: LazyLock<[f64; 4]> =
|
|
LazyLock::new(|| build_log_class_size::<4>(&NORMK1));
|
|
pub(crate) static LN_CARD_ROT2: LazyLock<[f64; 16]> =
|
|
LazyLock::new(|| build_log_class_size::<16>(&NORMK2));
|
|
pub(crate) static LN_CARD_ROT3: LazyLock<[f64; 64]> =
|
|
LazyLock::new(|| build_log_class_size::<64>(&NORMK3));
|
|
pub(crate) static LN_CARD_ROT4: LazyLock<[f64; 256]> =
|
|
LazyLock::new(|| build_log_class_size::<256>(&NORMK4));
|
|
pub(crate) static LN_CARD_ROT5: LazyLock<[f64; 1024]> =
|
|
LazyLock::new(|| build_log_class_size::<1024>(&NORMK5));
|
|
pub(crate) static LN_CARD_ROT6: LazyLock<[f64; 4096]> =
|
|
LazyLock::new(|| build_log_class_size::<4096>(&NORMK6));
|
|
|
|
fn ln0(x: f64) -> f64 {
|
|
if x == 0.0 { 0.0 } else { x.ln() }
|
|
}
|
|
|
|
fn normalize_circular(kmer: u64, ws: usize) -> u64 {
|
|
let mask = (1u64 << (ws * 2)) - 1;
|
|
let mut canonical = kmer & mask;
|
|
let mut current = canonical;
|
|
for _ in 0..ws - 1 {
|
|
let top = (current >> ((ws - 1) * 2)) & 3;
|
|
current = ((current << 2) | top) & mask;
|
|
if current < canonical {
|
|
canonical = current;
|
|
}
|
|
}
|
|
canonical
|
|
}
|
|
|
|
fn build_normalized_kmer<const N: usize>() -> [u64; N] {
|
|
let mut result = [0u64; N];
|
|
let k = N.ilog(4) as usize;
|
|
let shift = 64 - k * 2;
|
|
for i in 0..N {
|
|
let la = (i as u64) << shift;
|
|
let ra = i as u64;
|
|
let rc_ra = Kmer::from_raw(la).revcomp(k).raw() >> shift;
|
|
let circ = normalize_circular(ra, k);
|
|
let circ_rc = normalize_circular(rc_ra, k);
|
|
result[i] = circ.min(circ_rc);
|
|
}
|
|
result
|
|
}
|
|
|
|
fn build_log_class_size<const N: usize>(norm: &[u64; N]) -> [f64; N] {
|
|
let mut sizes = [0u32; N];
|
|
for &c in norm {
|
|
sizes[c as usize] += 1;
|
|
}
|
|
let mut result = [0.0f64; N];
|
|
for i in 0..N {
|
|
if sizes[i] > 0 {
|
|
result[i] = ln0(sizes[i] as f64);
|
|
}
|
|
}
|
|
result
|
|
}
|
|
|
|
pub(crate) fn entropy_norm_kmer(kmer: u64, k: usize, left: bool) -> u64 {
|
|
let shift = 64 - k * 2;
|
|
let ra = if left { kmer >> shift } else { kmer }; // left-aligned → right-aligned index
|
|
let canonical_ra = match k {
|
|
1 => NORMK1[ra as usize],
|
|
2 => NORMK2[ra as usize],
|
|
3 => NORMK3[ra as usize],
|
|
4 => NORMK4[ra as usize],
|
|
5 => NORMK5[ra as usize],
|
|
6 => NORMK6[ra as usize],
|
|
_ => panic!("k must be 1..=6"),
|
|
};
|
|
if left {
|
|
canonical_ra << shift
|
|
} else {
|
|
canonical_ra
|
|
} // right-aligned → left-aligned
|
|
}
|
|
|
|
pub(crate) fn ln_class_size(kmer: u64, k: usize, left: bool) -> f64 {
|
|
let ra = if left { kmer >> (64 - k * 2) } else { kmer }; // left-aligned → right-aligned index
|
|
match k {
|
|
1 => LN_CARD_ROT1[NORMK1[ra as usize] as usize],
|
|
2 => LN_CARD_ROT2[NORMK2[ra as usize] as usize],
|
|
3 => LN_CARD_ROT3[NORMK3[ra as usize] as usize],
|
|
4 => LN_CARD_ROT4[NORMK4[ra as usize] as usize],
|
|
5 => LN_CARD_ROT5[NORMK5[ra as usize] as usize],
|
|
6 => LN_CARD_ROT6[NORMK6[ra as usize] as usize],
|
|
_ => panic!("k must be 1..=6"),
|
|
}
|
|
}
|
|
|
|
// ── k-dependent tables (k ≤ K_MAX, ws ≤ WS_MAX) ──────────────────────────────
|
|
|
|
pub(crate) const K_MAX: usize = 32;
|
|
pub(crate) const WS_MAX: usize = 6;
|
|
|
|
/// n·ln(n), with n_log_n[0] = 0. Indexed by n = 0..=K_MAX.
|
|
pub(crate) static N_LOG_N: LazyLock<[f64; K_MAX + 1]> =
|
|
LazyLock::new(|| build_n_log_n());
|
|
|
|
/// H_max[k][ws]: maximum entropy for kmer length k and word size ws.
|
|
pub(crate) static EMAX: LazyLock<[[f64; WS_MAX + 1]; K_MAX + 1]> =
|
|
LazyLock::new(|| build_emax());
|
|
|
|
/// ln(k − ws + 1): log of the number of ws-words in a kmer of length k.
|
|
pub(crate) static LOG_NWORDS: LazyLock<[[f64; WS_MAX + 1]; K_MAX + 1]> =
|
|
LazyLock::new(|| build_log_nwords());
|
|
|
|
pub(crate) fn n_log_n(n: usize) -> f64 {
|
|
N_LOG_N[n]
|
|
}
|
|
|
|
pub(crate) fn emax(k: usize, ws: usize) -> f64 {
|
|
EMAX[k][ws]
|
|
}
|
|
|
|
pub(crate) fn log_nwords(k: usize, ws: usize) -> f64 {
|
|
LOG_NWORDS[k][ws]
|
|
}
|
|
|
|
fn build_n_log_n() -> [f64; K_MAX + 1] {
|
|
let mut result = [0.0f64; K_MAX + 1];
|
|
for n in 1..=K_MAX {
|
|
result[n] = (n as f64) * (n as f64).ln();
|
|
}
|
|
result
|
|
}
|
|
|
|
fn build_emax() -> [[f64; WS_MAX + 1]; K_MAX + 1] {
|
|
let mut result = [[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); // 4^ws
|
|
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()
|
|
};
|
|
result[k][ws] = -(t1 + t2);
|
|
}
|
|
}
|
|
result
|
|
}
|
|
|
|
fn build_log_nwords() -> [[f64; WS_MAX + 1]; K_MAX + 1] {
|
|
let mut result = [[0.0f64; WS_MAX + 1]; K_MAX + 1];
|
|
for k in 2..=K_MAX {
|
|
for ws in 1..=WS_MAX.min(k - 1) {
|
|
result[k][ws] = ((k - ws + 1) as f64).ln();
|
|
}
|
|
}
|
|
result
|
|
}
|