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
| Word | What it means | Example |
|---|---|---|
| Motif | a short pattern in a sequence | |
| k-mer | a piece of k bases | ACG |
| Window | the k letters you look at, then slide along | |
| Overlap | two matches that share a letter | |
| Frequency | how 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
| Start | Window | Letters |
|---|---|---|
| 0 | 0 to 2 | ACG |
| 1 | 1 to 3 | CGA |
| 2 | 2 to 4 | GAC |
| 3 | 3 to 5 | ACG |
| 4 | 4 to 6 | CGT |
The cells students fill in are left blank in this preview.
List every 3-mer with a sliding window
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-mer | Count | 2-mer | Count |
|---|---|---|---|
| ACG | 4 | AC | 4 |
| CGA | 2 | CG | 4 |
| GAC | 2 | GA | 2 |
| CGT | 1 | GT | 1 |
| GTA | 1 | TA | 1 |
| TAC | 1 |
The cells students fill in are left blank in this preview.
Count the k-mers with a dictionary
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
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
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
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.