This is an advanced topic. It assumes that your are familiar with Dynamic programming table
The Needleman–Wunsch algorithm performs a global alignment on two sequences (called A and B here). It is commonly used in bioinformatics to align protein or nucleotide sequences. The algorithm was published in 1970 by Saul B. Needleman and Christian D. Wunsch^{[1]}.
The Needleman–Wunsch algorithm is an example of dynamic programming, and was the first application of dynamic programming to biological sequence comparison.
Scores for aligned characters are specified by a similarity matrix. Here, is the similarity of characters a and b. It uses a linear gap penalty, here called d.
For example, if the similarity matrix was
then the alignment:
AGACTAGTTAC CGA‒‒‒GACGT
with a gap penalty of -5, would have the following score:
To find the alignment with the highest score, a two-dimensional array (or matrix) F is allocated. The entry in row i and column j is denoted here by . There is one column for each character in sequence A, and one row for each character in sequence B. Thus, if we are aligning sequences of sizes n and m, the amount of memory used is in . (Hirschberg's algorithm can compute an optimal alignment in space, roughly doubling the running time.)
As the algorithm progresses, the will be assigned to be the optimal score for the alignment of the first characters in A and the first characters in B. The principle of optimality is then applied as follows.
Basis:
Recursion, based on the principle of optimality:
The pseudo-code for the algorithm to compute the F matrix therefore looks like this:
for i=0 to length(A) F(i,0) ← d*i for j=0 to length(B) F(0,j) ← d*j for i=1 to length(A) for j = 1 to length(B) { Match ← F(i-1,j-1) + S(A[i], B[j]) Delete ← F(i-1, j) + d Insert ← F(i, j-1) + d F(i,j) ← max(Match, Insert, Delete) }
Once the F matrix is computed, the entry gives the maximum score among all possible alignments. To compute an alignment that actually gives this score, you start from the bottom right cell, and compare the value with the three possible sources (Match, Insert, and Delete above) to see which it came from. If Match, then and are aligned, if Delete, then is aligned with a gap, and if Insert, then is aligned with a gap. (In general, more than one choices may have the same value, leading to alternative optimal alignments.)
AlignmentA ← "" AlignmentB ← "" i ← length(A) j ← length(B) while (i > 0 and j > 0) { Score ← F(i,j) ScoreDiag ← F(i - 1, j - 1) ScoreUp ← F(i, j - 1) ScoreLeft ← F(i - 1, j) if (Score == ScoreDiag + S(A[i], B[j])) { AlignmentA ← A[i] + AlignmentA AlignmentB ← B[j] + AlignmentB i ← i - 1 j ← j - 1 } else if (Score == ScoreLeft + d) { AlignmentA ← A[i] + AlignmentA AlignmentB ← "-" + AlignmentB i ← i - 1 } otherwise (Score == ScoreUp + d) { AlignmentA ← "-" + AlignmentA AlignmentB ← B[j] + AlignmentB j ← j - 1 } } while (i > 0) { AlignmentA ← A[i] + AlignmentA AlignmentB ← "-" + AlignmentB i ← i - 1 } while (j > 0) { AlignmentA ← "-" + AlignmentA AlignmentB ← B[j] + AlignmentB j ← j - 1 }
Needleman and Wunsch describe their algorithm explicitly for the case when the alignment is penalized solely by the matches and mismatches, and gaps have no penalty (d=0). The original Needleman and Wunsch publication from 1970 suggests to use recursion:
The corresponding dynamic programming algorithm takes cubic time. The paper also points out that the recursion can accommodate arbitrary gap penalization formulas:
A better dynamic programming algorithm with quadratic running time for the same problem (no gap penalty) was first introduced^{[2]} by David Sankoff in 1972. Similar quadratic-time algorithms were discovered independently by T. K. Vintsyuk^{[3]} in 1968 for speech processing ("time warping"), and by Robert A. Wagner and Michael J. Fischer^{[4]} in 1974 for string matching.
Needleman and Wunsch formulated their problem in terms of maximizing similarity. Another possibility is to minimize the edit distance between sequences, introduced by Vladimir Levenshtein. Peter H. Sellers showed^{[5]} in 1974 that the two problems are equivalent.
In modern terminology, "Needleman-Wunsch" refers to a global alignment algorithm that takes quadratic time for a linear or affine gap penalty.
Acknowledgements
This web page reuses material from Wikipedia page http://en.wikipedia.org/wiki/Needleman_wunsch under the rights of CC-BY-SA license. As a result, the content of this page is and will stay available under the rights of this license regardless of restrictions that apply to other pages of this website.