kmerust 0.3.2

A fast, parallel k-mer counter for DNA sequences in FASTA and FASTQ files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
//! K-mer counting and output.
//!
//! This module provides the main k-mer counting functionality, using parallel
//! processing to efficiently count canonical k-mers across all sequences in FASTA or FASTQ files.

use crate::{
    cli::OutputFormat,
    format::SequenceFormat,
    input::Input,
    kmer::{pack_canonical, unpack_to_string, KmerLength},
    progress::{Progress, ProgressTracker},
    reader::{read, read_with_quality, SequenceWithQuality},
    streaming::count_kmers_stdin_with_format,
};
use bytes::Bytes;
use dashmap::DashMap;
use rayon::prelude::{ParallelBridge, ParallelIterator};
use rustc_hash::FxHasher;
use serde::Serialize;
use std::{
    collections::HashMap,
    error::Error,
    fmt::Debug,
    hash::BuildHasherDefault,
    io::{stdout, BufWriter, Error as IoError, Write},
    path::Path,
};
use thiserror::Error;

#[cfg(feature = "tracing")]
#[allow(unused_imports)]
use tracing::{debug, info, info_span};

/// Errors that can occur during k-mer processing.
#[derive(Debug, Error)]
pub enum ProcessError {
    /// Error reading input FASTA file.
    #[error("Unable to read input: {0}")]
    ReadError(#[from] Box<dyn Error>),

    /// Error writing output.
    #[error("Unable to write output: {0}")]
    WriteError(#[from] IoError),

    /// Error serializing JSON output.
    #[error("Unable to serialize JSON: {0}")]
    JsonError(#[from] serde_json::Error),
}

/// A k-mer with its count, used for JSON serialization.
#[derive(Serialize)]
struct KmerCount {
    kmer: String,
    count: u64,
}

/// Counts k-mers in a FASTA file and writes results to stdout.
///
/// Reads sequences from the FASTA file at `path`, counts all canonical k-mers
/// of length `k`, and writes the results to stdout in FASTA-like format.
///
/// # Errors
///
/// Returns `ProcessError::ReadError` if the file cannot be read.
/// Returns `ProcessError::WriteError` if output cannot be written.
pub fn run<P>(path: P, k: usize) -> Result<(), ProcessError>
where
    P: AsRef<Path> + Debug,
{
    run_with_options(path, k, OutputFormat::Fasta, 1)
}

/// Counts k-mers with configurable output format and filtering.
///
/// # Arguments
///
/// * `path` - Path to the FASTA file
/// * `k` - K-mer length
/// * `format` - Output format (fasta, tsv, or json)
/// * `min_count` - Minimum count threshold (k-mers below this are excluded)
///
/// # Errors
///
/// Returns `ProcessError` on read, write, or serialization errors.
pub fn run_with_options<P>(
    path: P,
    k: usize,
    format: OutputFormat,
    min_count: u64,
) -> Result<(), ProcessError>
where
    P: AsRef<Path> + Debug,
{
    let counts = count_kmers(&path, k)?;
    output_counts(counts, format, min_count)
}

/// Counts k-mers from an input source (file or stdin) and writes results to stdout.
///
/// This is the main entry point for input-agnostic k-mer counting with output.
/// Input format is auto-detected from file extension.
///
/// # Arguments
///
/// * `input` - The input source (file path or stdin)
/// * `k` - K-mer length
/// * `output_format` - Output format (fasta, tsv, or json)
/// * `min_count` - Minimum count threshold (k-mers below this are excluded)
///
/// # Errors
///
/// Returns `ProcessError` on read, write, or serialization errors.
///
/// # Example
///
/// ```rust,no_run
/// use kmerust::run::run_with_input;
/// use kmerust::input::Input;
/// use kmerust::cli::OutputFormat;
/// use std::path::Path;
///
/// // From file
/// let input = Input::from_path(Path::new("genome.fa"));
/// run_with_input(&input, 21, OutputFormat::Tsv, 1)?;
///
/// // From stdin
/// let input = Input::Stdin;
/// // run_with_input(&input, 21, OutputFormat::Tsv, 1)?;
/// # Ok::<(), kmerust::run::ProcessError>(())
/// ```
pub fn run_with_input(
    input: &Input,
    k: usize,
    output_format: OutputFormat,
    min_count: u64,
) -> Result<(), ProcessError> {
    run_with_input_format(input, k, output_format, min_count, SequenceFormat::Auto)
}

/// Counts k-mers from an input source with explicit input format specification.
///
/// # Arguments
///
/// * `input` - The input source (file path or stdin)
/// * `k` - K-mer length
/// * `output_format` - Output format (fasta, tsv, or json)
/// * `min_count` - Minimum count threshold (k-mers below this are excluded)
/// * `input_format` - Input file format (Auto, Fasta, or Fastq)
///
/// # Errors
///
/// Returns `ProcessError` on read, write, or serialization errors.
pub fn run_with_input_format(
    input: &Input,
    k: usize,
    output_format: OutputFormat,
    min_count: u64,
    input_format: SequenceFormat,
) -> Result<(), ProcessError> {
    let counts = match input {
        Input::File(path) => count_kmers_with_format(path, k, input_format)?,
        Input::Stdin => count_kmers_stdin_with_format(k, input_format)
            .map_err(|e| ProcessError::ReadError(e.into()))?,
    };
    output_counts(counts, output_format, min_count)
}

/// Counts k-mers from an input source with quality filtering and writes to stdout.
///
/// For FASTQ input, k-mers containing bases with quality below `min_quality`
/// are skipped. For FASTA input, the quality parameter is ignored.
///
/// # Arguments
///
/// * `input` - The input source (file path or stdin)
/// * `k` - K-mer length
/// * `output_format` - Output format (fasta, tsv, or json)
/// * `min_count` - Minimum count threshold
/// * `input_format` - Input file format
/// * `min_quality` - Optional minimum Phred quality score (0-93)
///
/// # Errors
///
/// Returns `ProcessError` on read, write, or serialization errors.
pub fn run_with_quality(
    input: &Input,
    k: usize,
    output_format: OutputFormat,
    min_count: u64,
    input_format: SequenceFormat,
    min_quality: Option<u8>,
) -> Result<(), ProcessError> {
    let counts = match input {
        Input::File(path) => count_kmers_with_quality(path, k, input_format, min_quality)?,
        // For stdin, quality filtering is not yet supported
        Input::Stdin => count_kmers_stdin_with_format(k, input_format)
            .map_err(|e| ProcessError::ReadError(e.into()))?,
    };
    output_counts(counts, output_format, min_count)
}

/// Counts k-mers and returns them as a `HashMap`.
///
/// This is the main library API for counting k-mers without writing to stdout.
/// Input format is auto-detected from the file extension.
///
/// # Arguments
///
/// * `path` - Path to the FASTA or FASTQ file
/// * `k` - K-mer length (must be 1-32)
///
/// # Returns
///
/// A `HashMap` mapping k-mer strings to their counts.
///
/// # Errors
///
/// Returns an error if:
/// - `k` is outside the valid range (1-32)
/// - The file cannot be read
pub fn count_kmers<P>(path: P, k: usize) -> Result<HashMap<String, u64>, Box<dyn Error>>
where
    P: AsRef<Path> + Debug,
{
    count_kmers_with_format(path, k, SequenceFormat::Auto)
}

/// Counts k-mers with explicit format specification.
///
/// # Arguments
///
/// * `path` - Path to the sequence file
/// * `k` - K-mer length (must be 1-32)
/// * `format` - Input file format (Auto, Fasta, or Fastq)
///
/// # Returns
///
/// A `HashMap` mapping k-mer strings to their counts.
///
/// # Errors
///
/// Returns an error if:
/// - `k` is outside the valid range (1-32)
/// - The file cannot be read
pub fn count_kmers_with_format<P>(
    path: P,
    k: usize,
    format: SequenceFormat,
) -> Result<HashMap<String, u64>, Box<dyn Error>>
where
    P: AsRef<Path> + Debug,
{
    #[cfg(feature = "tracing")]
    info!(k = k, path = ?path, "Starting k-mer counting");

    // Validate k-mer length upfront to provide a clear error
    let k_len = KmerLength::new(k)?;

    #[cfg(feature = "tracing")]
    let read_span = info_span!("read_sequences", path = ?path).entered();

    let sequences = read(&path, format)?;

    #[cfg(feature = "tracing")]
    drop(read_span);

    #[cfg(feature = "tracing")]
    let process_span = info_span!("process_sequences").entered();

    let kmer_map = KmerMap::new().build(sequences, k);

    #[cfg(feature = "tracing")]
    drop(process_span);

    let result = kmer_map.into_hashmap(k_len);

    #[cfg(feature = "tracing")]
    info!(unique_kmers = result.len(), "K-mer counting complete");

    Ok(result)
}

/// Counts k-mers with quality filtering.
///
/// For FASTQ input, k-mers containing bases with quality below `min_quality`
/// are skipped. For FASTA input, the quality parameter is ignored.
///
/// # Arguments
///
/// * `path` - Path to the FASTA or FASTQ file
/// * `k` - K-mer length (must be 1-32)
/// * `format` - Input file format (Auto, Fasta, or Fastq)
/// * `min_quality` - Optional minimum Phred quality score (0-93)
///
/// # Returns
///
/// A `HashMap` mapping k-mer strings to their counts.
///
/// # Errors
///
/// Returns an error if:
/// - `k` is outside the valid range (1-32)
/// - The file cannot be read
pub fn count_kmers_with_quality<P>(
    path: P,
    k: usize,
    format: SequenceFormat,
    min_quality: Option<u8>,
) -> Result<HashMap<String, u64>, Box<dyn Error>>
where
    P: AsRef<Path> + Debug,
{
    #[cfg(feature = "tracing")]
    info!(k = k, path = ?path, min_quality = ?min_quality, "Starting k-mer counting with quality filter");

    // Validate k-mer length upfront to provide a clear error
    let k_len = KmerLength::new(k)?;

    #[cfg(feature = "tracing")]
    let read_span = info_span!("read_sequences", path = ?path).entered();

    let sequences = read_with_quality(&path, format)?;

    #[cfg(feature = "tracing")]
    drop(read_span);

    #[cfg(feature = "tracing")]
    let process_span = info_span!("process_sequences").entered();

    let kmer_map = KmerMap::new().build_with_quality(sequences, k, min_quality);

    #[cfg(feature = "tracing")]
    drop(process_span);

    let result = kmer_map.into_hashmap(k_len);

    #[cfg(feature = "tracing")]
    info!(
        unique_kmers = result.len(),
        "K-mer counting with quality complete"
    );

    Ok(result)
}

/// Counts k-mers with progress callback.
///
/// Similar to [`count_kmers`], but invokes a callback after processing each sequence,
/// allowing callers to monitor progress during long-running operations.
/// Input format is auto-detected from the file extension.
///
/// # Arguments
///
/// * `path` - Path to the FASTA or FASTQ file
/// * `k` - K-mer length (must be 1-32)
/// * `callback` - Function called with a [`Progress`] snapshot after each sequence
///
/// # Returns
///
/// A `HashMap` mapping k-mer strings to their counts.
///
/// # Errors
///
/// Returns an error if:
/// - `k` is outside the valid range (1-32)
/// - The file cannot be read
///
/// # Example
///
/// ```rust,no_run
/// use kmerust::run::count_kmers_with_progress;
///
/// let counts = count_kmers_with_progress("genome.fa", 21, |progress| {
///     eprintln!(
///         "Processed {} sequences ({} bases)",
///         progress.sequences_processed,
///         progress.bases_processed
///     );
/// })?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn count_kmers_with_progress<P, F>(
    path: P,
    k: usize,
    callback: F,
) -> Result<HashMap<String, u64>, Box<dyn Error>>
where
    P: AsRef<Path> + Debug,
    F: Fn(Progress) + Send + Sync + 'static,
{
    use std::sync::Arc;

    #[cfg(feature = "tracing")]
    info!(k = k, path = ?path, "Starting k-mer counting with progress");

    // Validate k-mer length upfront to provide a clear error
    let k_len = KmerLength::new(k)?;

    #[cfg(feature = "tracing")]
    let read_span = info_span!("read_sequences", path = ?path).entered();

    let sequences = read(&path, SequenceFormat::Auto)?;

    #[cfg(feature = "tracing")]
    drop(read_span);

    #[cfg(feature = "tracing")]
    let process_span = info_span!("process_sequences").entered();

    let tracker = Arc::new(ProgressTracker::new());
    let callback = Arc::new(callback);
    let kmer_map = KmerMapWithProgress::new(tracker, callback).build(sequences, k);

    #[cfg(feature = "tracing")]
    drop(process_span);

    let result = kmer_map.into_hashmap(k_len);

    #[cfg(feature = "tracing")]
    info!(
        unique_kmers = result.len(),
        "K-mer counting with progress complete"
    );

    Ok(result)
}

/// Writes k-mer counts to stdout in the specified format.
///
/// # Arguments
///
/// * `counts` - `HashMap` of k-mer strings to their counts
/// * `format` - Output format (Fasta, Tsv, Json, or Histogram)
/// * `min_count` - Minimum count threshold (k-mers below this are excluded)
///
/// # Errors
///
/// Returns `ProcessError::WriteError` if output cannot be written.
/// Returns `ProcessError::JsonError` if JSON serialization fails.
#[allow(clippy::implicit_hasher)]
pub fn output_counts(
    counts: HashMap<String, u64>,
    format: OutputFormat,
    min_count: u64,
) -> Result<(), ProcessError> {
    let mut buf = BufWriter::new(stdout());
    let filtered: Vec<_> = counts
        .into_iter()
        .filter(|(_, count)| *count >= min_count)
        .collect();

    match format {
        OutputFormat::Fasta => {
            for (kmer, count) in filtered {
                writeln!(buf, ">{count}\n{kmer}")?;
            }
        }
        OutputFormat::Tsv => {
            for (kmer, count) in filtered {
                writeln!(buf, "{kmer}\t{count}")?;
            }
        }
        OutputFormat::Json => {
            let json_data: Vec<KmerCount> = filtered
                .into_iter()
                .map(|(kmer, count)| KmerCount { kmer, count })
                .collect();
            serde_json::to_writer_pretty(&mut buf, &json_data)?;
            writeln!(buf)?;
        }
        OutputFormat::Histogram => {
            use crate::histogram::compute_histogram;

            // Build a HashMap for compute_histogram
            let counts_map: HashMap<String, u64> = filtered.into_iter().collect();
            let histogram = compute_histogram(&counts_map);

            for (count, frequency) in histogram {
                writeln!(buf, "{count}\t{frequency}")?;
            }
        }
    }

    buf.flush()?;
    Ok(())
}

/// A custom [`DashMap`] w/ [`FxHasher`].
type DashFx = DashMap<u64, u64, BuildHasherDefault<FxHasher>>;

struct KmerMap(DashFx);

impl KmerMap {
    fn new() -> Self {
        Self(DashMap::with_hasher(
            BuildHasherDefault::<FxHasher>::default(),
        ))
    }

    fn build(self, sequences: rayon::vec::IntoIter<Bytes>, k: usize) -> Self {
        sequences.for_each(|seq| self.process_sequence(&seq, k));
        self
    }

    fn build_with_quality(
        self,
        sequences: rayon::vec::IntoIter<SequenceWithQuality>,
        k: usize,
        min_quality: Option<u8>,
    ) -> Self {
        sequences.for_each(|seq_qual| {
            self.process_sequence_with_quality(
                &seq_qual.seq,
                seq_qual.qual.as_deref(),
                k,
                min_quality,
            );
        });
        self
    }

    fn process_sequence(&self, seq: &Bytes, k: usize) {
        self.process_sequence_with_quality(seq, None, k, None);
    }

    fn process_sequence_with_quality(
        &self,
        seq: &Bytes,
        qual: Option<&[u8]>,
        k: usize,
        min_quality: Option<u8>,
    ) {
        if seq.len() < k {
            return;
        }

        // Pre-compute quality threshold as ASCII value (Phred+33)
        let quality_threshold = min_quality.map(|q| q.saturating_add(33));

        let mut i = 0;
        while i <= seq.len() - k {
            // Check quality if filtering is enabled
            if let (Some(q), Some(threshold)) = (qual, quality_threshold) {
                if let Some(bad_pos) = q[i..i + k].iter().position(|&qv| qv < threshold) {
                    i += bad_pos + 1; // Skip past low-quality base
                    continue;
                }
            }

            match pack_canonical(&seq[i..i + k]) {
                Ok(canonical_bits) => {
                    self.0
                        .entry(canonical_bits)
                        .and_modify(|c| *c = c.saturating_add(1))
                        .or_insert(1);
                    i += 1;
                }
                Err(err) => {
                    i += err.position + 1;
                }
            }
        }
    }

    fn into_hashmap(self, k: KmerLength) -> HashMap<String, u64> {
        self.0
            .into_iter()
            .par_bridge()
            .map(|(packed_bits, count)| {
                let kmer_string = unpack_to_string(packed_bits, k);
                (kmer_string, count)
            })
            .collect()
    }
}

/// A k-mer map with progress tracking.
struct KmerMapWithProgress<F: Fn(Progress) + Send + Sync + 'static> {
    map: DashFx,
    tracker: std::sync::Arc<ProgressTracker>,
    callback: std::sync::Arc<F>,
}

impl<F: Fn(Progress) + Send + Sync + 'static> KmerMapWithProgress<F> {
    fn new(tracker: std::sync::Arc<ProgressTracker>, callback: std::sync::Arc<F>) -> Self {
        Self {
            map: DashMap::with_hasher(BuildHasherDefault::<FxHasher>::default()),
            tracker,
            callback,
        }
    }

    #[allow(clippy::cast_possible_truncation)]
    fn build(self, sequences: rayon::vec::IntoIter<Bytes>, k: usize) -> Self {
        use rayon::prelude::ParallelIterator;

        sequences.for_each(|seq| {
            let len = seq.len() as u64;
            self.process_sequence(&seq, k);
            self.tracker.record_sequence(len);
            (self.callback)(self.tracker.snapshot());
        });
        self
    }

    fn process_sequence(&self, seq: &Bytes, k: usize) {
        if seq.len() < k {
            return;
        }
        let mut i = 0;

        while i <= seq.len() - k {
            match pack_canonical(&seq[i..i + k]) {
                Ok(canonical_bits) => {
                    self.map
                        .entry(canonical_bits)
                        .and_modify(|c| *c = c.saturating_add(1))
                        .or_insert(1);
                    i += 1;
                }
                Err(err) => {
                    i += err.position + 1;
                }
            }
        }
    }

    fn into_hashmap(self, k: KmerLength) -> HashMap<String, u64> {
        self.map
            .into_iter()
            .par_bridge()
            .map(|(packed_bits, count)| {
                let kmer_string = unpack_to_string(packed_bits, k);
                (kmer_string, count)
            })
            .collect()
    }
}

/// Counts k-mers using memory-mapped I/O for potentially faster file access.
///
/// Memory-maps the FASTA file and processes it directly from the mapped region.
/// This can be more efficient for large files on systems with sufficient RAM.
///
/// # Arguments
///
/// * `path` - Path to the FASTA file
/// * `k` - K-mer length (must be 1-32)
///
/// # Returns
///
/// A `HashMap` mapping k-mer strings to their counts.
///
/// # Errors
///
/// Returns an error if:
/// - `k` is outside the valid range (1-32)
/// - The file cannot be opened or memory-mapped
/// - The file cannot be parsed as FASTA
///
/// # Safety
///
/// The underlying file must not be modified while being processed.
/// Modifying a mapped file leads to undefined behavior.
///
/// # Example
///
/// ```rust,no_run
/// use kmerust::run::count_kmers_mmap;
///
/// let counts = count_kmers_mmap("large_genome.fa", 21)?;
/// println!("Found {} unique k-mers", counts.len());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[cfg(feature = "mmap")]
pub fn count_kmers_mmap<P>(path: P, k: usize) -> Result<HashMap<String, u64>, Box<dyn Error>>
where
    P: AsRef<Path> + Debug,
{
    use bio::io::fasta;
    use rayon::iter::IntoParallelIterator;
    use std::io::Cursor;

    #[cfg(feature = "tracing")]
    info!(k = k, path = ?path, "Starting memory-mapped k-mer counting");

    // Validate k-mer length upfront
    let k_len = KmerLength::new(k)?;

    #[cfg(feature = "tracing")]
    let mmap_span = info_span!("mmap_fasta", path = ?path).entered();

    let mmap =
        crate::mmap::MmapFasta::open(&path).map_err(|e| crate::error::KmeRustError::MmapError {
            source: e,
            path: path.as_ref().to_path_buf(),
        })?;

    #[cfg(feature = "tracing")]
    {
        drop(mmap_span);
        debug!(size_bytes = mmap.len(), "Memory-mapped file");
    }

    #[cfg(feature = "tracing")]
    let process_span = info_span!("process_sequences").entered();

    // Parse FASTA from the memory-mapped bytes
    let cursor = Cursor::new(mmap.as_bytes());
    let reader = fasta::Reader::new(cursor);
    let records: Vec<_> = reader
        .records()
        .collect::<Result<Vec<_>, _>>()
        .map_err(|e| crate::error::KmeRustError::SequenceParse {
            details: e.to_string(),
        })?;

    let kmer_map = KmerMap::new();
    let sequences: Vec<Bytes> = records
        .iter()
        .map(|r| Bytes::copy_from_slice(r.seq()))
        .collect();

    sequences
        .into_par_iter()
        .for_each(|seq| kmer_map.process_sequence(&seq, k));

    #[cfg(feature = "tracing")]
    drop(process_span);

    let result = kmer_map.into_hashmap(k_len);

    #[cfg(feature = "tracing")]
    info!(
        unique_kmers = result.len(),
        "Memory-mapped k-mer counting complete"
    );

    Ok(result)
}