Question: Is There A Fast Hashing Function For Nucleotide K-Mers (Q-Grams)?
gravatar for Ketil
7.7 years ago by
Ketil3.9k wrote:

I would like to index all k-mers in a set of nucleotide sequences. I could use a generic string based hash function, but my experiments indicate that a function leveraging the fact that the k-mers are overlapping might be faster. Also, I'd like a k-mer and its reverse complement to hash to the same value.

To be precise, I am not looking for hash table implementations, or applications, just a fast function from k-mer to a hash value, so that the value for a k-mer and its reverse complement is the same.

I can invent my own function for this (I have, as a matter of fact), but surely, something like this exists already?

index • 9.9k views
ADD COMMENTlink modified 7.7 years ago by Micans270 • written 7.7 years ago by Ketil3.9k

have you any solution for this? what's the hashing functions for DNA k-mers?? Thanks

ADD REPLYlink written 2.8 years ago by midox200
gravatar for Brad Chapman
7.7 years ago by
Brad Chapman9.2k
Boston, MA
Brad Chapman9.2k wrote:

Titus Brown's khmer is C++ wrapped in Python:

and is used for attacking next-gen assembly challenges:

ADD COMMENTlink written 7.7 years ago by Brad Chapman9.2k

Fun to see bloom filters gaining some use! This is in fact what I want the hashing function for (although it will be rehashed by some other function later).

ADD REPLYlink written 7.7 years ago by Ketil3.9k

khmer uses darcs as its RCS. Yay!

ADD REPLYlink written 7.7 years ago by Ketil3.9k

FYI: The correct url for the khmer project is

ADD REPLYlink written 5.2 years ago by Michael R. Crusoe1.1k
gravatar for Micans
7.7 years ago by
Micans270 wrote:

I have used the simple map from A,C,G,T onto {00, 01, 10, 11} (in bits), allowing four bases encoded in a byte. You could then use hash(word) + hash(rc(word)), or any other symmetric function. This approach has the advantage that you can use the current hash to find the next hash; as you shift bases in and out of your k-mer, you shift bits in and out of your hash or hash constituents. This is the approach taken in sylamer, and it's pretty fast.

ADD COMMENTlink written 7.7 years ago by Micans270

I guess this is a variation of rolling hash? Can you give more specific description on implementation? Thanks!

ADD REPLYlink written 4.5 years ago by Griffan90

Still here? :-) Yes, I maintain the current hash in forward and reverse complement, shift left/right and chop off the end, add next base, and store the numerically smallest hash. Code at

ADD REPLYlink written 23 months ago by Ketil3.9k

I used this approach recently in my final year project and my lecturer was left speechless at the speed/accuracy of my hash function. Not sure why this isn't a standard!

ADD REPLYlink written 7.7 years ago by Mark Anthony Gibbins130

If you want identical hashes for reverse-complements, couldn't you just use 0 for A/T and 1 for G/C, instead of using two bits per nucleotide?

ADD REPLYlink written 7.7 years ago by Ryan Thompson3.4k

Yes, I've used this too. The main problems with this is that you risk overflowing a fixed size data type (so k must not be greater than bits/2). For large k, you need to use an arbitrary precision integer, which slows things down. Also, the incremental calculation of next hash involves two divisions and some other arithmetic. In short - I think it's possible to do better.

ADD REPLYlink written 7.7 years ago by Ketil3.9k

Yes, those limitations are evident. In the context of Sylamer we do not support K-mers of length > 16 currently, but on 64-bit machines you can support words of lenght 32 natively. For large k I do not use all bits and resort to true hashing (the size of the hash array defined by the amount of sequence to be analysed). This solution obviously fits some applications and not others, but in terms of speed I will be well impressed if you can do substantially better than 'hid = ((hid << 2) | b) & LOMEGA(K)' (no divisions).

ADD REPLYlink written 7.7 years ago by Micans270

No, that's pretty good. I though that to support simultaneous calculation of the reverse complement hash you'd need a division. But now I think you can do that too with shifts and bitwise ops. Good catch!

ADD REPLYlink written 7.7 years ago by Ketil3.9k

I did an implementation of this in Haskell - normally, I'd expect a high level language to be less efficient for this, but it turns out it is fast enough (meaning that I haven't found an associative data structure that won't be dramatically slower than the hashing). I can hash about 40MB/s on my laptop, doing both forward and reverse complement, and avoiding Ns. Almost twice as fast without those restrictions.

Oh, and I map A: 00, C: 11, G: 01, T:10 - this makes it easy (bitwise operations) to complement and count GC content, and differentiate pyrimidines and purines.

ADD REPLYlink modified 5.2 years ago • written 5.2 years ago by Ketil3.9k
gravatar for Drio
7.7 years ago by
United States
Drio910 wrote:

Take a look to these benchmark results. Particularly the last benchmark, dict. It compares different hashing libraries in different programming languages.

If you want speed, use khash. That's the hash table implementation used in the c entry for that benchmark.

Also check this post. It has a very nice review of C/C++ hash table libraries.

ADD COMMENTlink written 7.7 years ago by Drio910

Thanks - but I don't really want a hash table implementation, I just what a hashing function.

ADD REPLYlink written 7.7 years ago by Ketil3.9k
gravatar for Jts
7.7 years ago by
Jts1.2k wrote:

I have used one of Bob Jenkin's hash functions (lookup3/8) for k-mer hashing with good results:

I have also heard good things about murmer hash but I have not tried it myself:

As for dedicated k-mer hashes see this paper by Guillaume Marçais and Carl Kingsford:

In section 3.4 they discuss their design of a k-mer specific hashing function.

ADD COMMENTlink written 7.7 years ago by Jts1.2k

What hash compression technique do you use for the hash values returned by lookup3/8? How do you determine the hashtable size?

Thanks in advance.

ADD REPLYlink written 22 months ago by germelcar20
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1384 users visited in the last hour