perf: enable zero-allocation queries and memory-mapped indexes
Introduce zero-allocation row extraction and query result buffers across `obicompactvec` and `obikpartitionner` to eliminate per-kmer heap allocations. Replace in-memory MPHF deserialization with memory-mapped, zero-copy views to reduce runtime memory footprint. Add configurable I/O chunking, a RAM-aware `--chunk-size` parameter, and system memory monitoring via the new `sysinfo` dependency. Re-export `PreloadedIndex` for external consumers.
This commit is contained in:
1 parent
1661dd6b1c
commit
de1a41810a
11 files changed
+403
-274
No files matched your search
+277
-167
@@ -5,13 +5,13 @@ use std::sync::Arc;
|
||||
|
||||
use clap::Args;
|
||||
use obikindex::KmerIndex;
|
||||
use obikpartitionner::PreloadedIndex;
|
||||
use obilayeredmap::IndexMode;
|
||||
use obiread::chunk::read_sequence_chunks;
|
||||
use obiread::record::{SeqRecord, parse_chunk};
|
||||
use obikrope::Rope;
|
||||
use obikseq::{RoutableSuperKmer, set_k, set_m};
|
||||
use obilayeredmap::IndexMode;
|
||||
use obiread::chunk::read_sequence_chunks_sized;
|
||||
use obiread::record::{SeqRecord, parse_chunk};
|
||||
use obiskbuilder::SuperKmerIter;
|
||||
use obisys::available_memory_bytes;
|
||||
use tracing::info;
|
||||
|
||||
// ── Pipeline data ─────────────────────────────────────────────────────────────
|
||||
@@ -70,6 +70,10 @@ pub struct QueryArgs {
|
||||
.unwrap_or(1)
|
||||
)]
|
||||
pub threads: usize,
|
||||
|
||||
/// I/O chunk size in MiB (default: auto-sized from available RAM and thread count)
|
||||
#[arg(long)]
|
||||
pub chunk_size: Option<usize>,
|
||||
}
|
||||
|
||||
// ── SKDesc — one occurrence of a superkmer in the batch ───────────────────────
|
||||
@@ -98,25 +102,24 @@ pub struct QueryBatch {
|
||||
|
||||
impl QueryBatch {
|
||||
/// Build a batch from a vec of parsed sequence records.
|
||||
pub fn from_records(
|
||||
records: Vec<SeqRecord>,
|
||||
k: usize,
|
||||
level_max: usize,
|
||||
theta: f64,
|
||||
) -> Self {
|
||||
let mut ids = Vec::with_capacity(records.len());
|
||||
let mut seqs = Vec::with_capacity(records.len());
|
||||
pub fn from_records(records: Vec<SeqRecord>, k: usize, level_max: usize, theta: f64) -> Self {
|
||||
let mut ids = Vec::with_capacity(records.len());
|
||||
let mut seqs = Vec::with_capacity(records.len());
|
||||
let mut n_kmers = Vec::with_capacity(records.len());
|
||||
let mut map: HashMap<RoutableSuperKmer, Vec<SKDesc>> = HashMap::new();
|
||||
// Upper-bound estimate: at most one superkmer per k bases.
|
||||
// Avoids repeated rehash on large chunks.
|
||||
let cap = records.iter().map(|r| r.normalized.len()).sum::<usize>() / k.max(1);
|
||||
let mut map: HashMap<RoutableSuperKmer, Vec<SKDesc>> = HashMap::with_capacity(cap);
|
||||
|
||||
for (seq_idx, record) in records.into_iter().enumerate() {
|
||||
let mut kmer_offset = 0u32;
|
||||
|
||||
for rsk in SuperKmerIter::new(&record.normalized, k, level_max, theta) {
|
||||
let n = (rsk.seql() - k + 1) as u32;
|
||||
map.entry(rsk)
|
||||
.or_default()
|
||||
.push(SKDesc { seq_idx: seq_idx as u32, kmer_offset });
|
||||
map.entry(rsk).or_default().push(SKDesc {
|
||||
seq_idx: seq_idx as u32,
|
||||
kmer_offset,
|
||||
});
|
||||
kmer_offset += n;
|
||||
}
|
||||
|
||||
@@ -125,7 +128,12 @@ impl QueryBatch {
|
||||
n_kmers.push(kmer_offset);
|
||||
}
|
||||
|
||||
Self { ids, seqs, n_kmers, map }
|
||||
Self {
|
||||
ids,
|
||||
seqs,
|
||||
n_kmers,
|
||||
map,
|
||||
}
|
||||
}
|
||||
|
||||
/// Split the superkmer map by partition index.
|
||||
@@ -140,88 +148,90 @@ impl QueryBatch {
|
||||
}
|
||||
}
|
||||
|
||||
// ── KmerResults — allocation-free ragged result matrix ───────────────────────
|
||||
|
||||
/// Flat storage for per-kmer query results across all sequences in a chunk.
|
||||
///
|
||||
/// Replaces `Vec<Vec<Option<Box<[u32]>>>>` — a single allocation for the whole
|
||||
/// chunk instead of one `Box<[u32]>` per found k-mer.
|
||||
struct KmerResults {
|
||||
data: Vec<u32>, // total_kmers × n_genomes, row-major
|
||||
in_index: Vec<bool>, // total_kmers — true if the kmer was found in the index
|
||||
offsets: Vec<usize>, // offsets[i]..offsets[i+1] = kmer range for sequence i
|
||||
n_genomes: usize,
|
||||
}
|
||||
|
||||
impl KmerResults {
|
||||
fn new(n_kmers_per_seq: &[u32], n_genomes: usize) -> Self {
|
||||
let mut offsets = Vec::with_capacity(n_kmers_per_seq.len() + 1);
|
||||
let mut total = 0usize;
|
||||
offsets.push(0);
|
||||
for &n in n_kmers_per_seq {
|
||||
total += n as usize;
|
||||
offsets.push(total);
|
||||
}
|
||||
Self {
|
||||
data: vec![0u32; total * n_genomes],
|
||||
in_index: vec![false; total],
|
||||
offsets,
|
||||
n_genomes,
|
||||
}
|
||||
}
|
||||
|
||||
fn n_kmers_for(&self, seq: usize) -> usize {
|
||||
self.offsets[seq + 1] - self.offsets[seq]
|
||||
}
|
||||
|
||||
fn set(&mut self, seq: usize, kmer: usize, row: &[u32]) {
|
||||
let abs = self.offsets[seq] + kmer;
|
||||
self.in_index[abs] = true;
|
||||
let base = abs * self.n_genomes;
|
||||
self.data[base..base + self.n_genomes].copy_from_slice(row);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_in_index(&self, seq: usize, kmer: usize) -> bool {
|
||||
self.in_index[self.offsets[seq] + kmer]
|
||||
}
|
||||
|
||||
/// Value for genome `g` at (seq, kmer); meaningful only when `is_in_index`.
|
||||
#[inline]
|
||||
fn val(&self, seq: usize, kmer: usize, g: usize) -> u32 {
|
||||
self.data[(self.offsets[seq] + kmer) * self.n_genomes + g]
|
||||
}
|
||||
}
|
||||
|
||||
// ── Per-sequence accumulator ──────────────────────────────────────────────────
|
||||
|
||||
struct SeqAcc {
|
||||
kmer_count: u32,
|
||||
kmer_missing: u32,
|
||||
kmer_count: u32,
|
||||
kmer_missing: u32,
|
||||
genome_totals: Vec<u32>,
|
||||
}
|
||||
|
||||
impl SeqAcc {
|
||||
fn new(n_genomes: usize) -> Self {
|
||||
Self {
|
||||
kmer_count: 0,
|
||||
kmer_missing: 0,
|
||||
kmer_count: 0,
|
||||
kmer_missing: 0,
|
||||
genome_totals: vec![0u32; n_genomes],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── Findere z-window filter ───────────────────────────────────────────────────
|
||||
|
||||
/// Apply the Findere z-window filter to per-kmer query results for one superkmer.
|
||||
/// Aggregate s-mer query results into k-mer answers using a Findere z-window.
|
||||
///
|
||||
/// Input: N s-mer results (indexed kmer size s = k − z + 1).
|
||||
/// Output: N − z + 1 k-mer results (user kmer size k).
|
||||
///
|
||||
/// For each genome g independently: k-mer at position i is confirmed iff all z values
|
||||
/// results[i..i+z][g] are nonzero (None counts as zero for all genomes).
|
||||
/// Output values are taken from results[i]; genomes not confirmed are zeroed.
|
||||
fn apply_findere(
|
||||
results: &[Option<Box<[u32]>>],
|
||||
z: usize,
|
||||
n_genomes: usize,
|
||||
) -> Vec<Option<Box<[u32]>>> {
|
||||
let n = results.len();
|
||||
if z <= 1 {
|
||||
return results.iter().map(|r| r.as_ref().map(|row| row.clone())).collect();
|
||||
}
|
||||
if n < z {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let out_n = n - z + 1;
|
||||
let mut confirmed = vec![vec![false; n_genomes]; out_n];
|
||||
|
||||
for g in 0..n_genomes {
|
||||
let hit = |i: usize| results[i].as_ref().map_or(false, |r| r[g] > 0);
|
||||
|
||||
let mut count: u32 = (0..z).filter(|&j| hit(j)).count() as u32;
|
||||
if count == z as u32 { confirmed[0][g] = true; }
|
||||
|
||||
for i in 1..out_n {
|
||||
if hit(i - 1) { count -= 1; }
|
||||
if hit(i + z - 1) { count += 1; }
|
||||
if count == z as u32 { confirmed[i][g] = true; }
|
||||
}
|
||||
}
|
||||
|
||||
(0..out_n).map(|i| {
|
||||
let first = results[i].as_ref()?;
|
||||
let mut row: Box<[u32]> = first.clone();
|
||||
for g in 0..n_genomes {
|
||||
if !confirmed[i][g] { row[g] = 0; }
|
||||
}
|
||||
if row.iter().any(|&v| v > 0) { Some(row) } else { None }
|
||||
}).collect()
|
||||
}
|
||||
|
||||
// ── process_chunk ─────────────────────────────────────────────────────────────
|
||||
|
||||
fn process_chunk(
|
||||
idx: &KmerIndex,
|
||||
preloaded: &PreloadedIndex,
|
||||
rope: Rope,
|
||||
k: usize,
|
||||
n_genomes: usize,
|
||||
n_partitions: usize,
|
||||
with_counts: bool,
|
||||
effective_z: usize,
|
||||
detail: bool,
|
||||
count_missing: bool,
|
||||
force_presence: bool,
|
||||
idx: &KmerIndex,
|
||||
rope: Rope,
|
||||
k: usize,
|
||||
n_genomes: usize,
|
||||
n_partitions: usize,
|
||||
with_counts: bool,
|
||||
effective_z: usize,
|
||||
detail: bool,
|
||||
count_missing: bool,
|
||||
force_presence: bool,
|
||||
presence_threshold: u32,
|
||||
) -> Vec<u8> {
|
||||
let records = parse_chunk(&rope, k);
|
||||
@@ -229,14 +239,11 @@ fn process_chunk(
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let batch = QueryBatch::from_records(records, k, 6, 0.7);
|
||||
let batch = QueryBatch::from_records(records, k, 6, 0.7);
|
||||
let n_seqs = batch.ids.len();
|
||||
|
||||
// Per-sequence s-mer result vectors in global sequence position order.
|
||||
// All partitions fill into this structure before Findere is applied.
|
||||
let mut seq_results: Vec<Vec<Option<Box<[u32]>>>> = batch.n_kmers.iter()
|
||||
.map(|&n| vec![None; n as usize])
|
||||
.collect();
|
||||
// Flat result matrix — one allocation for the whole chunk.
|
||||
let mut results = KmerResults::new(&batch.n_kmers, n_genomes);
|
||||
|
||||
let by_part = batch.split_by_partition(n_partitions);
|
||||
|
||||
@@ -245,76 +252,170 @@ fn process_chunk(
|
||||
continue;
|
||||
}
|
||||
|
||||
let kmer_results = preloaded
|
||||
.query_partition(part_idx, part_sks, k, n_genomes)
|
||||
idx.partition()
|
||||
.query_partition_with(
|
||||
part_idx,
|
||||
part_sks,
|
||||
k,
|
||||
n_genomes,
|
||||
with_counts,
|
||||
|sk_idx, kmer_idx, row| {
|
||||
let rsk = part_sks[sk_idx];
|
||||
let descs = batch.map.get(rsk).expect("rsk must be in map");
|
||||
for desc in descs {
|
||||
results.set(
|
||||
desc.seq_idx as usize,
|
||||
desc.kmer_offset as usize + kmer_idx,
|
||||
row,
|
||||
);
|
||||
}
|
||||
},
|
||||
)
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("query error on partition {part_idx}: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
|
||||
for (rsk, sk_kmer_results) in part_sks.iter().zip(kmer_results.iter()) {
|
||||
let descs = batch.map.get(*rsk).expect("rsk must be in map");
|
||||
for desc in descs {
|
||||
let offset = desc.kmer_offset as usize;
|
||||
let dst = &mut seq_results[desc.seq_idx as usize];
|
||||
for (j, hit) in sk_kmer_results.iter().enumerate() {
|
||||
dst[offset + j] = hit.as_ref().map(|r| r.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply Findere on each complete sequence vector, then accumulate.
|
||||
let n_kmers_out: Vec<usize> = batch.n_kmers.iter()
|
||||
.map(|&n| { let n = n as usize; if n >= effective_z { n - effective_z + 1 } else { 0 } })
|
||||
// Findere z-window filter + accumulation — no intermediate allocations.
|
||||
// One `confirmed` buffer reused across all sequences.
|
||||
let max_n_kmers = batch.n_kmers.iter().map(|&n| n as usize).max().unwrap_or(0);
|
||||
let mut confirmed = vec![false; max_n_kmers * n_genomes];
|
||||
|
||||
let mut accs: Vec<SeqAcc> = (0..n_seqs).map(|_| SeqAcc::new(n_genomes)).collect();
|
||||
|
||||
let n_kmers_out: Vec<usize> = batch
|
||||
.n_kmers
|
||||
.iter()
|
||||
.map(|&n| {
|
||||
let n = n as usize;
|
||||
if n >= effective_z {
|
||||
n - effective_z + 1
|
||||
} else {
|
||||
0
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut accs: Vec<SeqAcc> =
|
||||
(0..n_seqs).map(|_| SeqAcc::new(n_genomes)).collect();
|
||||
|
||||
let mut cov: Vec<Vec<Vec<u32>>> = if detail {
|
||||
n_kmers_out.iter()
|
||||
n_kmers_out
|
||||
.iter()
|
||||
.map(|&n| vec![vec![0u32; n]; n_genomes])
|
||||
.collect()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
|
||||
let presence = force_presence || !with_counts;
|
||||
let presence = force_presence || !with_counts;
|
||||
let threshold = presence_threshold;
|
||||
let z = effective_z;
|
||||
|
||||
for seq_idx in 0..n_seqs {
|
||||
let filtered = apply_findere(&seq_results[seq_idx], effective_z, n_genomes);
|
||||
let acc = &mut accs[seq_idx];
|
||||
let n = results.n_kmers_for(seq_idx);
|
||||
let out_n = n_kmers_out[seq_idx];
|
||||
if out_n == 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (pos, hit) in filtered.iter().enumerate() {
|
||||
match hit {
|
||||
None => {
|
||||
if seq_results[seq_idx][pos].is_none() {
|
||||
acc.kmer_missing += 1;
|
||||
if z <= 1 {
|
||||
// No Findere — every indexed s-mer is confirmed.
|
||||
let acc = &mut accs[seq_idx];
|
||||
for pos in 0..n {
|
||||
if !results.is_in_index(seq_idx, pos) {
|
||||
acc.kmer_missing += 1;
|
||||
continue;
|
||||
}
|
||||
acc.kmer_count += 1;
|
||||
for g in 0..n_genomes {
|
||||
let v = results.val(seq_idx, pos, g);
|
||||
if v == 0 {
|
||||
continue;
|
||||
}
|
||||
let c = if presence {
|
||||
u32::from(v >= threshold)
|
||||
} else {
|
||||
v
|
||||
};
|
||||
acc.genome_totals[g] += c;
|
||||
if detail {
|
||||
cov[seq_idx][g][pos] += c;
|
||||
}
|
||||
}
|
||||
Some(row) => {
|
||||
acc.kmer_count += 1;
|
||||
for (g, &v) in row.iter().enumerate() {
|
||||
if v == 0 { continue; }
|
||||
let contribution = if presence {
|
||||
u32::from(v >= threshold)
|
||||
} else {
|
||||
v
|
||||
};
|
||||
acc.genome_totals[g] += contribution;
|
||||
if detail {
|
||||
cov[seq_idx][g][pos] += contribution;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Build confirmed[pos * n_genomes + g] via sliding window.
|
||||
let conf = &mut confirmed[..out_n * n_genomes];
|
||||
conf.fill(false);
|
||||
|
||||
for g in 0..n_genomes {
|
||||
let hit =
|
||||
|i: usize| results.is_in_index(seq_idx, i) && results.val(seq_idx, i, g) > 0;
|
||||
|
||||
let mut cnt: u32 = (0..z).filter(|&j| hit(j)).count() as u32;
|
||||
if cnt == z as u32 {
|
||||
conf[g] = true;
|
||||
}
|
||||
|
||||
for i in 1..out_n {
|
||||
if hit(i - 1) {
|
||||
cnt -= 1;
|
||||
}
|
||||
if hit(i + z - 1) {
|
||||
cnt += 1;
|
||||
}
|
||||
if cnt == z as u32 {
|
||||
conf[i * n_genomes + g] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let acc = &mut accs[seq_idx];
|
||||
for pos in 0..out_n {
|
||||
let any = (0..n_genomes).any(|g| conf[pos * n_genomes + g]);
|
||||
if !any {
|
||||
if !results.is_in_index(seq_idx, pos) {
|
||||
acc.kmer_missing += 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
acc.kmer_count += 1;
|
||||
for g in 0..n_genomes {
|
||||
if !conf[pos * n_genomes + g] {
|
||||
continue;
|
||||
}
|
||||
let v = results.val(seq_idx, pos, g);
|
||||
if v == 0 {
|
||||
continue;
|
||||
}
|
||||
let c = if presence {
|
||||
u32::from(v >= threshold)
|
||||
} else {
|
||||
v
|
||||
};
|
||||
acc.genome_totals[g] += c;
|
||||
if detail {
|
||||
cov[seq_idx][g][pos] += c;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut buf = Vec::new();
|
||||
emit_batch(&batch, &accs, idx.meta(), count_missing, detail, &cov, &mut buf);
|
||||
// Capacity estimate: actual sequence + ID bytes, plus JSON overhead per record.
|
||||
// JSON per record ≈ 50 fixed chars + ~20 per genome (label + count value) + 100 (overhead).
|
||||
let seq_bytes: usize = batch.seqs.iter().map(|s| s.len()).sum();
|
||||
let id_bytes: usize = batch.ids.iter().map(|s| s.len()).sum();
|
||||
let cap = seq_bytes + id_bytes + n_seqs * (4 + 50 + n_genomes * 20) + 100;
|
||||
let mut buf = Vec::with_capacity(cap);
|
||||
emit_batch(
|
||||
&batch,
|
||||
&accs,
|
||||
idx.meta(),
|
||||
count_missing,
|
||||
detail,
|
||||
&cov,
|
||||
&mut buf,
|
||||
);
|
||||
buf
|
||||
}
|
||||
|
||||
@@ -329,18 +430,30 @@ pub fn run(args: QueryArgs) {
|
||||
set_k(idx.kmer_size());
|
||||
set_m(idx.minimizer_size());
|
||||
|
||||
let k = idx.kmer_size();
|
||||
let n_genomes = idx.meta().genomes.len();
|
||||
let k = idx.kmer_size();
|
||||
let n_genomes = idx.meta().genomes.len();
|
||||
let n_partitions = idx.n_partitions();
|
||||
let with_counts = idx.meta().config.with_counts;
|
||||
let n_workers = args.threads.max(1);
|
||||
let with_counts = idx.meta().config.with_counts;
|
||||
let n_workers = args.threads.max(1);
|
||||
|
||||
let effective_z: usize = args.findere_z.unwrap_or_else(|| {
|
||||
match idx.meta().config.evidence {
|
||||
// Chunk size: each chunk stays in memory for its entire processing lifetime.
|
||||
// Overhead per raw byte is ~8× (Rope + parsed records + superkmers + results).
|
||||
// We target ≤ 50 % of available RAM across all concurrent workers.
|
||||
let chunk_bytes = args
|
||||
.chunk_size
|
||||
.map(|mb| mb * 1024 * 1024)
|
||||
.unwrap_or_else(|| {
|
||||
let avail = available_memory_bytes();
|
||||
let computed = avail / (n_workers as u64 * 16);
|
||||
computed.clamp(4 * 1024 * 1024, 256 * 1024 * 1024) as usize
|
||||
});
|
||||
|
||||
let effective_z: usize = args
|
||||
.findere_z
|
||||
.unwrap_or_else(|| match idx.meta().config.evidence {
|
||||
IndexMode::Approx { z, .. } | IndexMode::Hybrid { z, .. } => z as usize,
|
||||
IndexMode::Exact => 1,
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
info!(
|
||||
"query: k={k}, {} genome(s), with_counts={with_counts}, z={effective_z}, \
|
||||
@@ -352,28 +465,25 @@ pub fn run(args: QueryArgs) {
|
||||
eprintln!("warning: --mismatch not yet implemented, ignored");
|
||||
}
|
||||
|
||||
let preloaded = Arc::new(
|
||||
PreloadedIndex::new(idx.partition(), n_partitions, with_counts)
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("error loading index layers: {e}");
|
||||
std::process::exit(1);
|
||||
})
|
||||
);
|
||||
|
||||
let detail = args.detail;
|
||||
let count_missing = args.count_missing;
|
||||
let force_presence = args.force_presence;
|
||||
let detail = args.detail;
|
||||
let count_missing = args.count_missing;
|
||||
let force_presence = args.force_presence;
|
||||
let presence_threshold = args.presence_threshold;
|
||||
|
||||
// Flat iterator over all Rope chunks from all input files.
|
||||
// I/O runs in the source thread; chunk processing is parallelised by the pipe.
|
||||
info!("query: chunk_size={}MiB", chunk_bytes / (1024 * 1024));
|
||||
|
||||
let paths: Vec<PathBuf> = args.inputs.iter().map(PathBuf::from).collect();
|
||||
let all_chunks = paths.into_iter().flat_map(|path| {
|
||||
let all_chunks = paths.into_iter().flat_map(move |path| {
|
||||
let path_str = path.to_str().unwrap_or("").to_owned();
|
||||
match read_sequence_chunks(&path_str) {
|
||||
match read_sequence_chunks_sized(&path_str, chunk_bytes) {
|
||||
Ok(iter) => Box::new(iter.filter_map(|r| match r {
|
||||
Ok(rope) => Some(rope),
|
||||
Err(e) => { eprintln!("read error: {e}"); None }
|
||||
Err(e) => {
|
||||
eprintln!("read error: {e}");
|
||||
None
|
||||
}
|
||||
})) as Box<dyn Iterator<Item = Rope> + Send>,
|
||||
Err(e) => {
|
||||
eprintln!("error opening {path_str}: {e}");
|
||||
@@ -385,11 +495,10 @@ pub fn run(args: QueryArgs) {
|
||||
let pipe = obipipeline::make_pipe! {
|
||||
QueryData : Rope => Vec<u8>,
|
||||
| {
|
||||
let idx = Arc::clone(&idx);
|
||||
let preloaded = Arc::clone(&preloaded);
|
||||
let idx = Arc::clone(&idx);
|
||||
move |rope: Rope| {
|
||||
process_chunk(
|
||||
&idx, &preloaded, rope, k, n_genomes, n_partitions, with_counts,
|
||||
&idx, rope, k, n_genomes, n_partitions, with_counts,
|
||||
effective_z, detail, count_missing, force_presence, presence_threshold,
|
||||
)
|
||||
}
|
||||
@@ -408,13 +517,13 @@ pub fn run(args: QueryArgs) {
|
||||
// ── Output ────────────────────────────────────────────────────────────────────
|
||||
|
||||
fn emit_batch(
|
||||
batch: &QueryBatch,
|
||||
accs: &[SeqAcc],
|
||||
meta: &obikindex::meta::IndexMeta,
|
||||
batch: &QueryBatch,
|
||||
accs: &[SeqAcc],
|
||||
meta: &obikindex::meta::IndexMeta,
|
||||
count_missing: bool,
|
||||
detail: bool,
|
||||
cov: &[Vec<Vec<u32>>],
|
||||
out: &mut impl Write,
|
||||
detail: bool,
|
||||
cov: &[Vec<Vec<u32>>],
|
||||
out: &mut impl Write,
|
||||
) {
|
||||
for (seq_idx, (id, seq)) in batch.ids.iter().zip(batch.seqs.iter()).enumerate() {
|
||||
let acc = &accs[seq_idx];
|
||||
@@ -434,17 +543,18 @@ fn emit_batch(
|
||||
if detail && !cov.is_empty() {
|
||||
let mut cov_map = serde_json::Map::new();
|
||||
for (g, genome) in meta.genomes.iter().enumerate() {
|
||||
let v: Vec<serde_json::Value> =
|
||||
cov[seq_idx][g].iter().map(|&x| x.into()).collect();
|
||||
let v: Vec<serde_json::Value> = cov[seq_idx][g].iter().map(|&x| x.into()).collect();
|
||||
cov_map.insert(genome.label.clone(), v.into());
|
||||
}
|
||||
ann.insert("coverage".into(), cov_map.into());
|
||||
}
|
||||
|
||||
let ann_str = serde_json::to_string(&ann).unwrap_or_else(|_| "{}".to_string());
|
||||
|
||||
// OBITools4 FASTA format: >id {"key":value,...}
|
||||
let _ = writeln!(out, ">{id} {ann_str}");
|
||||
let _ = out.write_all(b">");
|
||||
let _ = out.write_all(id.as_bytes());
|
||||
let _ = out.write_all(b" ");
|
||||
let _ = serde_json::to_writer(&mut *out, &ann);
|
||||
let _ = out.write_all(b"\n");
|
||||
let _ = out.write_all(seq);
|
||||
let _ = out.write_all(b"\n");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user