To sort or to hash? Revisiting k -mer counting

Motivation: Counting k -mers in large genomic datasets is a foundational primitive in bioinformatics, supporting applications such as genome assembly, variant discovery, metagenomic profiling, and sequence database indexing. As sequencing throughput grows, k -mer counting is routinely applied to collections of hundreds to thousands of files. Even a modest per-file speedup compounds into hours of saved computation across such collections. Methods: Sorting and hashing are the two main ways of counting k -mers, and practice has largely settled on the former, of which KMC3 (Kokot et al., 2017) is the most refined expression. We reconsider this choice and analyze both approaches using a cost model that charges computation and memory transfers together. We show that the outcome of the comparison rests on two matters: how abundant the k -mers are, and whether the hash tables fit in cache. Sorting is faster only when data is mostly unique and the hash tables do not fit in cache. We then design a k -mer counting algorithm in the light of this analysis. We adopt the consolidated paradigm based on first partitioning the k -mers by their minimizer, thus obtaining partitions of super- k -mers, and then counting each partition independently in parallel. In particular, super- k -mers are deduplicated before counting, and the number of partitions is chosen so that the hash tables fit in cache whenever possible. The algorithm avoids writing temporary data to disk if enough internal memory is available. Results: We implemented this strategy in a tool named Tuna and show that it is faster than KMC3 on all but one of our inputs, spanning bacterial and human assemblies, metagenomes and read sets, by up to 4.3×. The advantage of Tuna compared to KMC3 in the counting step alone widens to 6.8× as k -mer abundance grows, as the analysis predicts. On the largest input tested, 614 GB of human sequencing reads, Tuna counts within 4 GB of memory and 0.7 TB of temporary disk. On that input the two tools finish within 1.1× of each other under the full 256 GB budget, and restricting every tool to the same 8 GB budget widens the advantage of Tuna over KMC3 to 1.8×. Software: The software is freely available at github.com/vicLeva/tuna under the MIT license.

Authors

Institutions

Publication Details

Journal
bioRxiv (Cold Spring Harbor Laboratory)
Published
2026-11-24
DOI
https://doi.org/10.64898/2026.11.23.768625
Primary Topic
Algorithms and Data Compression
Type
preprint
Controls
|||
ALL TIME
JAN
FEB
MAR
APR
MAY
JUN
JUL
AUG
SEP
OCT
NOV
preprint

To sort or to hash? Revisiting k -mer counting

Giulio Ermanno Pibiri, Pierre Peterlongo, Rob Patro, Victor Levallois
bioRxiv (Cold Spring Harbor Laboratory)
Algorithms and Data Compression
preprint

To sort or to hash? Revisiting k -mer counting

Giulio Ermanno Pibiri, Pierre Peterlongo, Rob Patro, Victor Levallois
preprint en

Abstract

Motivation: Counting k -mers in large genomic datasets is a foundational primitive in bioinformatics, supporting applications such as genome assembly, variant discovery, metagenomic profiling, and sequence database indexing. As sequencing throughput grows, k -mer counting is routinely applied to collections of hundreds to thousands of files. Even a modest per-file speedup compounds into hours of saved computation across such collections. Methods: Sorting and hashing are the two main ways of counting k -mers, and practice has largely settled on the former, of which KMC3 (Kokot et al., 2017) is the most refined expression. We reconsider this choice and analyze both approaches using a cost model that charges computation and memory transfers together. We show that the outcome of the comparison rests on two matters: how abundant the k -mers are, and whether the hash tables fit in cache. Sorting is faster only when data is mostly unique and the hash tables do not fit in cache. We then design a k -mer counting algorithm in the light of this analysis. We adopt the consolidated paradigm based on first partitioning the k -mers by their minimizer, thus obtaining partitions of super- k -mers, and then counting each partition independently in parallel. In particular, super- k -mers are deduplicated before counting, and the number of partitions is chosen so that the hash tables fit in cache whenever possible. The algorithm avoids writing temporary data to disk if enough internal memory is available. Results: We implemented this strategy in a tool named Tuna and show that it is faster than KMC3 on all but one of our inputs, spanning bacterial and human assemblies, metagenomes and read sets, by up to 4.3×. The advantage of Tuna compared to KMC3 in the counting step alone widens to 6.8× as k -mer abundance grows, as the analysis predicts. On the largest input tested, 614 GB of human sequencing reads, Tuna counts within 4 GB of memory and 0.7 TB of temporary disk. On that input the two tools finish within 1.1× of each other under the full 256 GB budget, and restricting every tool to the same 8 GB budget widens the advantage of Tuna over KMC3 to 1.8×. Software: The software is freely available at github.com/vicLeva/tuna under the MIT license.

bioRxiv (Cold Spring Harbor Laboratory)
University of Maryland, Baltimore (US), Institut national de recherche en sciences et technologies du numérique (FR), Ca' Foscari University of Venice (IT), Centre Inria de l'Université de Rennes (FR), Université de Rennes (FR), University of Maryland, College Park (US)
Algorithms and Data Compression
AI Navigator

Ask Laika to Summarize, Analyze, and Connect papers live on the map.

Summarize Papers & Methodologies

Extract key findings, datasets, and comparative methods across publications.

Benchmark Rankings & Visual Analytics

Rank top research institutions, authors, funders, topics, and journals by Field-Weighted Citation Impact (FWCI) and paper volume with instant charts.

Connect Distant Disciplines

Bridge topological clusters on the map to find hidden collaborative intersections.

Laika access coming soon