first implementation but far to be optimal

This commit is contained in:
Eric Coissac committed 2026-04-19 12:17:16 +02:00
commit de3f9b16cf
19336 files changed
+380276

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
[package]
name = "obikmer"
version = "0.1.0"
edition = "2024"
[[bin]]
name = "obikmer"
path = "src/main.rs"
[dependencies]
obikseq = { path = "../obikseq" }
obiread = { path = "../obiread" }
obiskbuilder = { path = "../obiskbuilder" }
obifastwrite = { path = "../obifastwrite" }
clap = { version = "4", features = ["derive"] }
crossbeam-channel = "0.5"
bytes = "1"
+1
View File
@@ -0,0 +1 @@
pub mod superkmer;
+166
View File
@@ -0,0 +1,166 @@
use std::io::{self, BufWriter, Write};
use std::thread;
use bytes::Bytes;
use clap::Args;
use crossbeam_channel::bounded;
use obifastwrite::write_scatter;
use obikseq::superkmer::SuperKmer;
use obiskbuilder::SuperKmerIter;
#[derive(Args)]
pub struct SuperkmerArgs {
/// Input: file path, URL (http/https), or `-` for stdin
pub input: String,
/// k-mer size
#[arg(short, long, default_value_t = 31)]
pub kmer_size: usize,
/// Minimizer size
#[arg(short, long, default_value_t = 11)]
pub minimizer_size: usize,
/// Entropy threshold (k-mers with score ≤ theta are rejected)
#[arg(long, default_value_t = 0.7)]
pub theta: f64,
/// Maximum sub-word size for entropy computation
#[arg(long, default_value_t = 6)]
pub level_max: usize,
/// Number of partitions (minimizer_hash % partitions → partition id)
#[arg(short, long, default_value_t = 256)]
pub partitions: u64,
/// Number of worker threads
#[arg(short = 'T', long, default_value_t = 4)]
pub threads: usize,
/// Force input format: fasta or fastq (default: auto-detect from extension)
#[arg(long)]
pub format: Option<String>,
}
#[derive(Clone, Copy)]
enum Format {
Fasta,
Fastq,
}
fn detect_format(source: &str, hint: Option<&str>) -> Format {
if let Some(h) = hint {
return match h.to_lowercase().as_str() {
"fastq" | "fq" => Format::Fastq,
_ => Format::Fasta,
};
}
if source.ends_with(".fq") || source.ends_with(".fastq")
|| source.ends_with(".fq.gz") || source.ends_with(".fastq.gz")
{
Format::Fastq
} else {
Format::Fasta
}
}
#[inline]
fn mix64(x: u64) -> u64 {
let x = x ^ (x >> 30);
let x = x.wrapping_mul(0xbf58476d1ce4e5b9);
let x = x ^ (x >> 27);
let x = x.wrapping_mul(0x94d049bb133111eb);
x ^ (x >> 31)
}
pub fn run(args: SuperkmerArgs) {
let format = detect_format(&args.input, args.format.as_deref());
let input = args.input.clone();
let k = args.kmer_size;
let m = args.minimizer_size;
let theta = args.theta;
let level_max = args.level_max;
let partitions = args.partitions;
let n_workers = args.threads.max(1);
// raw chunks (reader → workers)
let (raw_tx, raw_rx) = bounded::<Vec<Bytes>>(n_workers * 2);
// superkmer batches (workers → output)
let (sk_tx, sk_rx) = bounded::<Vec<(u64, SuperKmer)>>(n_workers * 2);
// ── reader thread ─────────────────────────────────────────────────────────
let reader = thread::spawn(move || {
let file = obiread::xopen(&input).expect("cannot open input");
match format {
Format::Fasta => {
for chunk in obiread::fasta_chunks(file) {
raw_tx.send(chunk.expect("read error")).unwrap();
}
}
Format::Fastq => {
for chunk in obiread::fastq_chunks(file) {
raw_tx.send(chunk.expect("read error")).unwrap();
}
}
}
// raw_tx drops here → workers observe disconnect
});
// ── worker threads ────────────────────────────────────────────────────────
let workers: Vec<_> = (0..n_workers)
.map(|_| {
let raw_rx = raw_rx.clone();
let sk_tx = sk_tx.clone();
thread::spawn(move || {
for raw_chunk in raw_rx {
let norm = match format {
Format::Fasta => {
obiread::normalize::normalize_fasta_chunk(raw_chunk, k)
}
Format::Fastq => {
obiread::normalize::normalize_fastq_chunk(raw_chunk, k)
}
};
const BATCH_SIZE: usize = 10_000;
let mut batch = Vec::with_capacity(BATCH_SIZE);
for sk in SuperKmerIter::new(norm, k, m, level_max, theta) {
batch.push(sk);
if batch.len() == BATCH_SIZE {
sk_tx.send(std::mem::replace(
&mut batch,
Vec::with_capacity(BATCH_SIZE),
)).unwrap();
}
}
if !batch.is_empty() {
sk_tx.send(batch).unwrap();
}
}
// sk_tx clone drops here
})
})
.collect();
// drop the extra sk_tx clone held by this thread so the output thread exits
drop(sk_tx);
// ── output thread ─────────────────────────────────────────────────────────
let output = thread::spawn(move || {
let stdout = io::stdout();
let mut out = BufWriter::new(stdout.lock());
for batch in sk_rx {
for (min_hash, sk) in batch {
let partition = (mix64(min_hash) % partitions) as u32;
write_scatter(&sk, &mut out, k, m, partition, min_hash)
.expect("write error");
}
}
out.flush().expect("flush error");
});
reader.join().expect("reader thread panicked");
for w in workers {
w.join().expect("worker thread panicked");
}
output.join().expect("output thread panicked");
}
+23
View File
@@ -0,0 +1,23 @@
mod cmd;
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(name = "obikmer", about = "DNA k-mer tools")]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Extract super-kmers from a sequence file (scatter phase)
Superkmer(cmd::superkmer::SuperkmerArgs),
}
fn main() {
let cli = Cli::parse();
match cli.command {
Commands::Superkmer(args) => cmd::superkmer::run(args),
}
}