I would like to iterate through every possible k-length string (called k-mer) given a list of symbols. For example, if k = 3 and symbols = {A, C, G, T}, then:
AAA
AAC
AAG
...
TTG
TTT
Here's my code to generate the string:
local k = 3
local bases = {'A', 'C', 'T', 'G'}
-- Generate the string (AAA...AAA)
local kmer_gen = {}
for i = 1,k do kmer_gen[i] = "A" end
local kmer = table.concat(kmer_gen)
It works but it surely does not look good. Can this be achieved more elegantly?
Now, I'm not sure how to iterate through the possible k-mers. One solution would be keeping substituting each character, but this doesn't seen efficient. Another way would be decoding from binary (each 2 bits represent a base), but the implementation is confusing and requires bitwise operations. Any other thoughts?