Algorithm Search⌘K

Problemintro

Median string problem

Also called motif median string.

Find a string of length l with the smallest total Hamming distance to its closest window in each sequence. It is equivalent to motif finding with the total Hamming distance score only.

The median string problem asks for a string vv of length LL that is close to all of a set of sequences at once: for each sequence, take the Hamming distance from vv to its closest window, and add these distances. A string with the smallest sum is a median string. The problem has the same optimum as motif finding with the total Hamming distance score, and it turns a search over WnW^n choices of sites into a search over 4L4^L strings. With other scores, such as information content, the two problems differ.

In plain words

Think of a candidate word of length LL. Slide it along each sequence and find the place where it fits best, the place with the fewest different letters. Write down that number of different letters for each sequence and add them up. That sum says how well the word fits all the sequences.

The median string is the word with the smallest sum. You can find it by trying all the words of length LL. That is a lot of words when LL is large, but it is far fewer than all the ways to pick one window in each sequence, because the number of words does not grow with the number of sequences.

Definition

Input. Sequences s1,…,sns_1, \ldots, s_n over {A,C,G,T}\{A, C, G, T\}, each of length GG, and a length L≤GL \le G. Each sis_i has W=G−L+1W = G - L + 1 windows si[q..q+L−1]s_i[q .. q + L - 1], 1≤q≤W1 \le q \le W. Positions are 1-based on this page.

Distance to a sequence. For a string vv of length LL, let d(v,si)=min⁡1≤q≤WdH(v,si[q..q+L−1])d(v, s_i) = \min_{1 \le q \le W} d_H(v, s_i[q .. q + L - 1]).

Total distance. D(v)=∑i=1nd(v,si)D(v) = \sum_{i=1}^{n} d(v, s_i).

The question. Find a string v∈{A,C,G,T}Lv \in \{A, C, G, T\}^L with the smallest D(v)D(v). Such a vv is a median string. The decision form asks whether some vv has D(v)≤kD(v) \le k.

The median vv is any string of length LL; it does not have to occur in any sequence.

Variants

  • Over the windows only. Restrict vv to the windows of the input. This takes O((nW)2L)O((nW)^2 L) time, but the result can be worse than the true median.
  • The largest distance. Replace the sum by max⁡id(v,si)\max_i d(v, s_i). This is the closest substring problem, the core of the planted motif problem.
  • A median under edit distance. In string algorithms, a median string of whole strings often means a string with the smallest sum of edit distances to them. That is a different problem with no windows, and it is not the subject of this page.

Examples

Take the five sequences of Rosalind problem BA2D with L=3L = 3: GGCGTTCAGGCA, AAGAATCAGTCA, CAAGGAGTTCGC, CACGTCAATCAC and CAATAATATTCG. A computation of D(v)D(v) for all 64 strings gives this distribution:

D(v)D(v) 2 3 4 5 6 7 8
strings 3 8 7 17 17 6 6

The three median strings, with D=2D = 2, are:

vv d(v,s1),…,d(v,s5)d(v, s_1), \ldots, d(v, s_5) first closest windows (start)
CAA 1, 1, 0, 0, 0 CAG (7), GAA (3), CAA (1), CAA (6), CAA (1)
TCA 0, 0, 1, 0, 1 TCA (6), TCA (6), TCG (9), TCA (5), TAA (4)
TTC 0, 1, 0, 1, 0 TTC (5), ATC (5), TTC (8), GTC (4), TTC (9)

TCA has no exact copy in s3s_3 or s5s_5, and CAA has none in s1s_1 or s2s_2. CAG occurs exactly in s1s_1 and s2s_2 but has D=3D = 3. The windows CAG, CAG, CAA, CAA, CAA of the greedy motif search have the consensus CAA and the score 2, so they are optimal sites, and their consensus is a median string.

Known algorithms

method type time source
all 4L4^L strings, each scored against all windows exact O(4LnWL)O(4^L n W L) Jones and Pevzner, Section 4.9; Compeau and Pevzner, Section 2.4
the same on the tree of prefixes, one column per tree node exact O(4LnW)O(4^L n W) Jones and Pevzner, Sections 4.7 to 4.9
branch and bound on the tree of prefixes exact O(4LnW)O(4^L n W) worst case, often less Jones and Pevzner, Section 4.9
the windows of the input as candidates heuristic O((nW)2L)O((nW)^2 L) folklore

On the tree of prefixes, a node of depth kk keeps for each window the number of mismatches of its first kk letters with the prefix. A child adds one column, which takes O(nW)O(nW) time, and the tree has fewer than 434L\frac{4}{3} 4^L nodes. This gives the O(4LnW)O(4^L n W) bound.

Bounds

Theorem (equivalence). Let Score(p)=∑j(n−max⁡acj(a))\mathrm{Score}(p) = \sum_j (n - \max_a c_j(a)) be the motif score of a choice of sites pp, as on the motif finding page. Then min⁡pScore(p)=min⁡vD(v)\min_p \mathrm{Score}(p) = \min_v D(v). A consensus of an optimal choice is a median string, and the closest windows of a median string are an optimal choice.

Proof. For a choice pp with windows t1,…,tnt_1, \ldots, t_n and any string vv, column jj adds n−cj(vj)n - c_j(v_j) mismatches to ∑idH(v,ti)\sum_i d_H(v, t_i). So ∑idH(v,ti)≥Score(p)\sum_i d_H(v, t_i) \ge \mathrm{Score}(p), with equality when vv is a consensus of pp.

First, let vv be any string and let p(v)p(v) pick a closest window of vv in each sequence. Then Score(p(v))≤∑idH(v,ti)=D(v)\mathrm{Score}(p(v)) \le \sum_i d_H(v, t_i) = D(v). So min⁡pScore(p)≤min⁡vD(v)\min_p \mathrm{Score}(p) \le \min_v D(v).

Second, let pp be any choice and vv a consensus of pp. Each d(v,si)≤dH(v,ti)d(v, s_i) \le d_H(v, t_i), so D(v)≤∑idH(v,ti)=Score(p)D(v) \le \sum_i d_H(v, t_i) = \mathrm{Score}(p). So min⁡vD(v)≤min⁡pScore(p)\min_v D(v) \le \min_p \mathrm{Score}(p), and the two minima are equal.

If pp is optimal, its consensus vv has D(v)≤Score(p)=min⁡DD(v) \le \mathrm{Score}(p) = \min D, so vv is a median string. If vv is a median string, Score(p(v))≤D(v)=min⁡Score\mathrm{Score}(p(v)) \le D(v) = \min \mathrm{Score}, so p(v)p(v) is optimal. □\square

  • NP-hard. The proof maps each instance to itself and keeps the optimal value, so the decision forms "is min⁡D≤k\min D \le k" and "is min⁡Score≤k\min \mathrm{Score} \le k" are the same question. The second is Consensus Patterns, which Li, Ma and Wang (2002) proved NP-hard, so the median string problem with LL in the input is NP-hard. See NP-hard.
  • Fixed LL. The enumeration takes O(4LnW)O(4^L n W) time, which is linear in the input size nGnG for a fixed LL.
  • A lower bound for each prefix. For a prefix uu of length kk, every extension vv has D(v)≥∑imin⁡qdH(u,si[q..q+k−1])D(v) \ge \sum_i \min_q d_H(u, s_i[q .. q + k - 1]), because the mismatches of the first kk letters never go away. This is the bound of the branch and bound search.

Reductions

  • From motif finding. By the theorem, any motif finding method for the total score gives median strings, and back, with O(nWL)O(nWL) extra time to find the closest windows or the consensus.
  • To the planted motif problem. A planted motif MM has D(M)≤ndD(M) \le nd. If the total distance of the planted motif is the smallest, the median string recovers it. On random data with subtle signals this can fail, because other strings can have a smaller sum while some sequence has no window within dd of them.
  • To exact pattern counting. The question whether some vv has D(v)=0D(v) = 0 asks for a k-mer that occurs exactly in all sequences. A hash set of the windows of each sequence answers it in O(nWL)O(nWL) time.

Applications

  • Exact motif finding for short motifs. For short motifs the 4L4^L strings can be enumerated (410=1,048,5764^{10} = 1{,}048{,}576), and the median string gives the optimal sites under the total score.
  • Seeds for other methods. A median string or the best strings of the enumeration can start a Gibbs sampler or an EM method such as MEME.
  • Teaching. The problem is the standard example of how a change of the search space, from sites to strings, makes an exponential search practical.

References

  1. Neil C. Jones and Pavel A. Pevzner, An Introduction to Bioinformatics Algorithms, MIT Press, 2004, Chapter 4, Section 4.9 (finding a median string), with the problem statements of Section 4.6 and the search trees of Sections 4.7 and 4.8
  2. Phillip Compeau and Pavel Pevzner, Bioinformatics Algorithms, An Active Learning Approach, Volume I, 2nd edition, Active Learning Publishers, 2015, Chapter 2, Section 2.4, from motif finding to finding a median string
  3. Ming Li, Bin Ma and Lusheng Wang, Finding similar regions in many sequences, Journal of Computer and System Sciences 65(1), 2002, NP-hardness of Consensus Patterns, the same problem under the total Hamming distance

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a known algorithm
  • a contract.yaml