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
- Giulio Ermanno Pibiri (ORCID: https://orcid.org/0000-0003-0724-7092)
- Pierre Peterlongo (ORCID: https://orcid.org/0000-0003-0776-6407)
- Rob Patro (ORCID: https://orcid.org/0000-0001-8463-1675)
- Victor Levallois (ORCID: https://orcid.org/0009-0003-8858-6578)
Institutions
- 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)
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