Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Finding k-mers and motifs in DNA

After this lesson: I can count the k-mers of a sequence and find where a motif starts.

60 min lesson2 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

A motif is a short pattern

A motif is a short pattern that turns up in a DNA sequence, such as a short stretch a protein recognises. To find motifs, we cut a sequence into k-mers, which are pieces of the same length, and count them.

Counting k-mers is the first step of many sequence tools. You will write the count with a dictionary.

By the end of this lesson

Note

I can count the k-mers of a sequence and find where a motif starts.

Key words

WordWhat it meansExample
Motifa short pattern in a sequence
k-mera piece of k basesACG
Windowthe k letters you look at, then slide along
Overlaptwo matches that share a letter
Frequencyhow many times something occurs

The cells students fill in are left blank in this preview.

A k-mer is a window of k letters

A k-mer is a piece of a sequence that is k bases long. A 3-mer has three bases, like ACG.

To list them all, put a window of k letters at the start, note it, slide one letter along and repeat. A sequence of length n has n - k + 1 windows.

Worked example: the 3-mers of ACGACGT

StartWindowLetters
00 to 2ACG
11 to 3CGA
22 to 4GAC
33 to 5ACG
44 to 6CGT

The cells students fill in are left blank in this preview.

List every 3-mer with a sliding window

Code: python
seq = "ACGACGTACGACG"
k = 3
for start in range(len(seq) - k + 1):
    print(start, seq[start:start + k])
print("number of 3-mers:", len(seq) - k + 1)
# Change k to 4. How many windows are there now?

A dictionary counts how often each k-mer occurs

Once you can list k-mers, you can count them. Keep a dictionary with the k-mer as the key and its count as the value.

For each window, add one to its count, or start the count at 1 the first time you see it. The most frequent k-mer is a good candidate for a motif.

Worked example: 3-mers and 2-mers of ACGACGTACGACG

3-merCount2-merCount
ACG4AC4
CGA2CG4
GAC2GA2
CGT1GT1
GTA1TA1
TAC1

The cells students fill in are left blank in this preview.

Count the k-mers with a dictionary

Code: python
seq = "ACGACGTACGACG"
k = 3
counts = {}
for start in range(len(seq) - k + 1):
    kmer = seq[start:start + k]
    if kmer in counts:
        counts[kmer] = counts[kmer] + 1
    else:
        counts[kmer] = 1
for kmer in counts:
    print(kmer, counts[kmer])
# Change k to 2 and check that AC and CG both appear 4 times.

A motif can appear at several positions

To find a motif, slide a window of the motif's length along the sequence. Each time the window equals the motif, note the start position.

The first start is 0, not 1. The list of positions is the answer, and its length is the count.

Find where a motif starts

Code: python
seq = "ACGACGTACGACG"
motif = "ACG"
positions = []
for start in range(len(seq) - len(motif) + 1):
    if seq[start:start + len(motif)] == motif:
        positions.append(start)
print(positions)
# Try the motif "CG". Where does it start?

Count AA in AAAA with the count method

Command
docker exec jupyter python3 -c "print('AAAA'.count('AA'))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Count AA in AAAA with a sliding window

Command
docker exec jupyter python3 -c "print(sum('AAAA'[i:i + 2] == 'AA' for i in range(3)))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think the count method counts every occurrence of a motif. It skips overlaps: in AAAA it finds AA twice, but a window starting at 0, 1 and 2 finds it three times. Use a sliding window when overlaps matter.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.