Commit bdf447e8 authored by Blaise Li's avatar Blaise Li
Browse files

Fix print, comment solution for kmer counting.

parent f7903932
Pipeline #58185 passed with stages
in 13 seconds
...@@ -391,7 +391,7 @@ Compute the 6 mers occurences of the sequence above, and print each 6mer and it' ...@@ -391,7 +391,7 @@ Compute the 6 mers occurences of the sequence above, and print each 6mer and it'
>>> s = s.replace('\n', '') >>> s = s.replace('\n', '')
>>> kmers = get_kmer_occurences(s, 6) >>> kmers = get_kmer_occurences(s, 6)
>>> for kmer in kmers: >>> for kmer in kmers:
>>> print kmer[0], '..', kmer[1] >>> print(kmer[0], '..', kmer[1])
gcagag .. 2 gcagag .. 2
aacttc .. 1 aacttc .. 1
gcaact .. 1 gcaact .. 1
......
def get_kmer_occurences(seq, kmer_len): def get_kmer_occurences(seq, kmer_len):
""" """
return a list of tuple return a list of tuple
each tuple contains a kmers present in seq and its occurence each tuple contains a kmers present in seq and its occurence
""" """
# Counter dictionary (keys will be kmers, values will be counts)
kmers = {} kmers = {}
stop = len(seq) - kmer_len stop = len(seq) - kmer_len
for i in range(stop + 1): for i in range(stop + 1):
# Extract kmer starting at position i
kmer = seq[i: i + kmer_len] kmer = seq[i: i + kmer_len]
# Increment the count for kmer
# or create the entry the first time this kmer is seen
# using default value 0, plus 1
kmers[kmer] = kmers.get(kmer, 0) + 1 kmers[kmer] = kmers.get(kmer, 0) + 1
return kmers.items() # pairs (kmer, count)
\ No newline at end of file return kmers.items()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment