Approximate Motif Matching

Advanced Computational Biology Motif Mismatches Approximate Matching
Significance:

Real binding sites are degenerate: a transcription factor tolerates a mismatch or two, so exact
matching misses most genuine occurrences. Approximate matching with a mismatch budget is
therefore what motif finders actually do, and it's the reason a naive exact search under-reports
regulatory elements.

Statement

Given a DNA string s, a motif t, and a non-negative integer d, find every position where t
occurs in s with at most d mismatches. Occurrences may overlap.

The three values arrive on three separate lines.

Print all starting positions using 1-based numbering, separated by single spaces, in
increasing order, on one line. If there are none, print an empty line.

Input — read from standard input
Variable Type Description
s
line 1
str The sequence to search in
1 <= len(s) <= 1000, uppercase A, C, G, T only
t
line 2
str The motif to search for
1 <= len(t) <= len(s)
d
line 3
int The maximum number of mismatches allowed
0 <= d <= len(t)

These variables are already read for you in the starter code on the right.

Output

str space-separated 1-based start positions in increasing order, on one line

Sample Cases
Sample 1
Input
GATATATGCATATACTTATA
ATAT
1
Expected Output
2 4 10 12 16
With one mismatch allowed, ATAT matches at several more positions than an exact search finds.
Sample 2
Input
ACGT
ACGT
0
Expected Output
1
With d = 0 this is exact matching, so only position 1 matches.

Submit also runs your code against 4 hidden test cases. Hidden inputs are never shown — if one fails you'll get its number and a description of the mismatch, not the data.

Constraints
  • 1 <= length(t) <= length(s) <= 1000
  • 0 <= d <= length(t)
  • With d = 0 this reduces to exact matching
  • Occurrences may overlap
Further Reading
  • Reuse your Hamming distance routine on each window s[i:i+len(t)].
  • You can stop counting mismatches for a window as soon as the count exceeds d.

My Notes
Log in to save personal notes.
Console output will appear here when you click Run Code or Submit...
Expected: s (str), t (str), d (int)
Next Problem
Global Alignment Score with Affine Gap Penalty