Crate nthash_rs

Source
Expand description

§ntHash‑rs

An idiomatic, pure‑Rust port of the classic ntHash rolling‑hash suite, focused on contiguous k‑mer hashing for DNA sequences.

This crate currently provides:

  • kmer::NtHash: the canonical contiguous‑k‑mer hasher that skips over non‑ACGT bases (N or other characters).

All heavy bit‑twiddling is delegated to low‑level modules (tables and constants), which mirror the original C++ reference implementation, and helper functionality in util for canonicalization and hash extension.

§Example

use nthash_rs::{NtHash, Result};

fn main() -> Result<()> {
    // Create a new NtHash over "ACGTNACGT", k=4, emit 2 hashes per k‑mer, start at pos=0
    let mut hasher = NtHash::new(b"ACGTNACGT", 4, 2, 0)?;

    // First call to roll() initializes and returns true if a valid k‑mer was found
    assert!(hasher.roll());
    // Retrieve the two hash values for the first valid 4‑mer
    let hashes = hasher.hashes();
    println!("First k‑mer hashes: {:#x}, {:#x}", hashes[0], hashes[1]);

    // Advance through the sequence
    while hasher.roll() {
        let h = hasher.hashes()[0];
        println!("Next k‑mer forward hash: {:#x}", h);
    }
    Ok(())
}

Re-exports§

pub use util::canonical;
pub use util::extend_hashes;
pub use kmer::NtHash;
pub use kmer::NtHashBuilder;
pub use blind::BlindNtHash;
pub use blind::BlindNtHashBuilder;
pub use seed::SeedNtHash;
pub use seed::SeedNtHashBuilder;

Modules§

blind
Streaming (“blind”) ntHash for contiguous k‑mers.
kmer
High‑level contiguous k‑mer rolling hasher. Skips over non‑ACGT bases exactly as the original reference. Canonical ntHash implementation for contiguous k‑mers.
seed
Streaming spaced-seed ntHash for non-contiguous k‑mers.
util
Miscellaneous helpers shared across all ntHash variants.

Enums§

NtHashError
Errors common to all ntHash k‑mer hashers.

Functions§

srol
One‑bit split‑rotate left (33 + 31 halves). One‑bit split‑rotate left (33 + 31 bit halves).
srol_table
Arbitrary split‑rotate via lookup tables. Lookup‑based split‑rotate left.
sror
One‑bit split‑rotate right (33 + 31 halves). One‑bit split‑rotate right (33 + 31 bit halves).

Type Aliases§

Result
Shorthand Result alias for this crate’s operations.