kmerust
A fast, parallel k-mer counter for DNA sequences in FASTA and FASTQ files.
Features
- Fast parallel processing using rayon and dashmap
- FASTA and FASTQ support with automatic format detection from file extension
- Canonical k-mers - outputs the lexicographically smaller of each k-mer and its reverse complement
- Flexible k-mer lengths from 1 to 32
- Handles N bases by skipping invalid k-mers
- Jellyfish-compatible output format for easy integration with existing pipelines
- Tested for accuracy against Jellyfish
Installation
From crates.io
From source
Usage
Arguments
<k>- K-mer length (1-32)<path>- Path to a FASTA or FASTQ file (use-or omit for stdin)
Options
-f, --format <FORMAT>- Output format:fasta(default),tsv,json, orhistogram-i, --input-format <FORMAT>- Input format:auto(default),fasta, orfastq-m, --min-count <N>- Minimum count threshold (default: 1)-Q, --min-quality <N>- Minimum Phred quality score for FASTQ (0-93); bases below this are skipped--save <PATH>- Save k-mer counts to a binary index file for fast querying-q, --quiet- Suppress informational output-h, --help- Print help information-V, --version- Print version information
Examples
Count 21-mers in a FASTA file:
Count 21-mers in a FASTQ file (format auto-detected):
Count 5-mers:
Unix Pipeline Integration
kmerust supports reading from stdin, enabling seamless integration with Unix pipelines:
# Pipe from another command
|
# Decompress and count
|
# Sample reads and count
|
# Explicit stdin marker
|
# FASTQ from stdin (specify format explicitly)
|
|
Output Formats
Use --format to choose the output format:
# TSV format (tab-separated)
# JSON format
# FASTA-like format (default)
# Histogram format (k-mer frequency spectrum)
Histogram Output
The histogram format outputs the k-mer frequency spectrum (count of counts), useful for genome size estimation and error detection:
Output is tab-separated with columns count and frequency:
1 1523456 # 1.5M k-mers appear exactly once (likely errors)
2 234567 # 234K k-mers appear twice
10 45678 # 45K k-mers appear 10 times
...
Quality Filtering (FASTQ)
For FASTQ files, use --min-quality to filter out k-mers containing low-quality bases:
# Skip k-mers with any base below Q20
# Higher threshold for stricter filtering
K-mers containing bases with Phred quality scores below the threshold are skipped entirely.
Index Serialization
For large genomes, save k-mer counts to a binary index file to avoid re-counting:
# Count and save to index
# Counts are also written to stdout as usual
The index file uses a compact binary format with CRC32 checksums for integrity verification. Gzip compression is auto-detected from the .gz extension:
# Save with gzip compression
Querying a Saved Index
Use the query subcommand to look up k-mer counts from a saved index:
# Query a single k-mer
# Output: 42 (or 0 if not found)
# Queries are case-insensitive and canonicalized
The query k-mer length must match the index's k value.
Sequence Readers
kmerust supports two sequence readers via feature flags, both supporting FASTA and FASTQ:
rust-bio(default) - Uses the rust-bio libraryneedletail- Uses the needletail library
To use needletail instead:
With needletail, format is auto-detected from file content. With rust-bio, format is detected from file extension (.fa, .fasta, .fna for FASTA; .fq, .fastq for FASTQ).
Production Features
Enable production features for additional capabilities:
Or enable individual features:
gzip- Read gzip-compressed FASTA files (.fa.gz)mmap- Memory-mapped I/O for large filestracing- Structured logging and diagnostics
Gzip Compressed Input
With the gzip feature, kmerust can directly read gzip-compressed files:
Tracing/Logging
With the tracing feature, use the RUST_LOG environment variable for diagnostic output:
RUST_LOG=kmerust=debug
Output Format
Output is written to stdout in FASTA-like format:
>{count}
{canonical_kmer}
Example output:
>114928
ATGCC
>289495
AATCA
Library Usage
kmerust can also be used as a library:
use count_kmers;
use PathBuf;
Explicit Format Selection
When using the builder API, you can explicitly specify the input format:
use KmerCounter;
use SequenceFormat;
Progress Reporting
Monitor progress during long-running operations:
use count_kmers_with_progress;
Memory-Mapped I/O
For large files, use memory-mapped I/O (requires mmap feature):
use count_kmers_mmap;
Streaming API
For memory-efficient processing:
use count_kmers_streaming;
Reading from Any Source
Count k-mers from any BufRead source, including stdin or in-memory data:
use count_kmers_from_reader;
use BufReader;
Performance
kmerust uses parallel processing to efficiently count k-mers:
- Sequences are processed in parallel using rayon
- A concurrent hash map (dashmap) allows lock-free updates
- FxHash provides fast hashing for 64-bit packed k-mers
License
MIT License - see LICENSE for details.