Greedy Motif Search

Advanced Computational Biology Motif Finding Greedy Algorithms Profile Matrices Regulatory DNA
Significance:

Transcription factors bind short, degenerate sequence motifs scattered across the
regulatory regions of co-expressed genes. Finding those motifs from sequence alone is one of the
foundational problems in computational biology, and the greedy approach — build a profile from what you
have, use it to pick the next best match, repeat — is the conceptual ancestor of Gibbs sampling and the
expectation-maximisation methods behind MEME. Laplace pseudocounts matter here: without them a single
missing base zeroes out an otherwise excellent candidate.

Statement

Given a collection of DNA strings and integers k and t, find a k-mer motif in each
of the t strings using greedy motif search with Laplace pseudocounts.

The algorithm:

  1. For each k-mer in the first string, treat it as the first motif.
  2. Build a profile matrix from the motifs chosen so far, adding 1 to every count (Laplace rule).
  3. For each subsequent string, choose the k-mer that the current profile scores highest. On ties,
    choose the leftmost.
  4. Score the resulting motif set by total Hamming distance from its consensus, and keep the best set.
  5. On tied scores, keep the set found earliest.

Print the t chosen motifs, one per line, in the order of their source strings.

Input — read from standard input
Variable Type Description
k
line 1
int The motif length
2 <= k <= 12
t
line 1
int The number of DNA strings
2 <= t <= 25
dna
line 2..t+1
str One DNA string per line, all of equal length
k <= length <= 200, uppercase A, C, G, T only

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

Output

str t motif strings of length k, one per line, in input string order

Sample Cases
Sample 1
Input
3 5
GGCGTTCAGGCA
AAGAATCAGTCA
CAAGGAGTTCGC
CACGTCAATCAC
CAATAATATTCG
Expected Output
TTC
ATC
TTC
ATC
TTC
The classic textbook instance; pseudocounts change the answer relative to the naive version.
Sample 2
Input
2 2
ACGT
TGCA
Expected Output
AC
GC
A minimal instance with two short strings.

Submit also runs your code against 3 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
  • k and t appear on the first line, separated by a space
  • All t DNA strings have equal length and contain only A, C, G, T
  • Profile probabilities use Laplace pseudocounts: every count starts at 1, denominator is t + 4
  • All ties — both in profile-most-probable selection and in final scoring — resolve to the earliest
    candidate, which makes the answer unique
Further Reading
  • Build the profile as (count + 1) / (number_of_motifs + 4) for each base at each column.
  • The profile-most-probable k-mer is the one maximising the product of per-column probabilities.
  • Score is the sum over columns of (number of motifs) - (count of the most common base).

My Notes
Log in to save personal notes.
Console output will appear here when you click Run Code or Submit...
Expected: k (int), t (int), dna (remaining lines)
Next Problem
Median String Motif Finder