This is a test version of Biostars. For the public version, visit https://www.biostars.org.
python function to kmer with windown size

Hello guys, I was reading about build-in functions in python to work with kmer, and I found this one:

mySeq = 'AAATTAAAGACAAAATCCCAGAATGCCCG'

def getKmers(sequence, size):
    return [sequence[x:x+size].upper() for x in range(len(sequence) - size + 1)]

Which returns:

['AAATTA', 'AATTAA', 'ATTAAA', 'TTAAAG', 'TAAAGA', 'AAAGAC', 'AAGACA', 'AGACAA', 'GACAAA', 'ACAAAA', 'CAAAAT', 'AAAATC', 'AAATCC', 'AATCCC', 'ATCCCA', 'TCCCAG', 'CCCAGA', 'CCAGAA', 'CAGAAT', 'AGAATG', 'GAATGC', 'AATGCC', 'ATGCCC', 'TGCCCG']

As we can see, the kmers are created in a windown range equals to 1, I'm thinking how I can define a windown range major than 1, for example, 3, to generate kmers in that form:

['AAATTA', 'TTAAAG', 'AAGACA', 'ACAAAA', 'AAATCC'...']

Can anyone help?

python kmer biopython fasta

2 answers

def getKmers(sequence, size, step):    
  return [sequence[x:x+size] for x in range(0, len(sequence) - size, step)]

You should probably write it as a generator, though:

def getKmers(sequence, size, step):    
  for x in range(0, len(sequence) - size, step):
    yield sequence[x:x+size]

thanks cschu181, it's exactly it !

Consider using this fast parser:

https://github.com/moorembioinfo/KmerAperture/tree/main/parser

Log in to answer this question.