The Needleman-Wunsch algorithm is the one of the most popular algorithms for global sequence alignment. Its variant, Smith-Waterman algorithm is used for local alignment.

## Needleman-Wunsch

### Creating the Matrix and Utility Methods

The first step of the Needleman-Wunsch algorithm is to set up the alignment matrix. We add one extra row and column in case the alignment starts with a gap.

```
m = len(seq1)
n = len(seq2)
mat = [[0 for _ in range(m+1)] for _ in range(n+1)]
```

We also define a utility method for calculating the score based on the given scoring matrix.

### Populate the First Row and Column of the Matrix

We first populate the first row and column with gap penalty scores.

```
for i in range(len(seq1) + 1):
mat[0][i] = gap_penalty * i
for j in range(len(seq2) + 1):
mat[j][0] = gap_penalty * j
```

### Filling in the Rest of the Matrix

We fill in the rest of the matrix with scores calculated using this formula:

$$ M(i,j)=\max\begin{cases} M(i-1,j-1)+s(i,j) \\ M(i-1,j)+g \\ M(i,j-1)+g \end{cases} $$

\(s(i,j)\) is the score of \(i\) aligning with \(j\). \(M(i,j)\) is the value at location \(i,j\) in the Needleman-Wunsch matrix. \(g\) is the gap penalty (assuming linear penalty).

The diagonal path from \(M(i-1,j-1)\) to \(M(i,j)\) indicates a match. A horizontal path or vertical path indicates an indel (insertion or deletion, gap).

```
for i in range(1, len(seq2) + 1):
for j in range(1, len(seq1) + 1):
# seq2 insertion/seq1 deletion
score_horizontal = mat[i][j-1] + gap_penalty
# seq2 deletion/seq1 insertion
score_vertical = mat[i-1][j] + gap_penalty
score_diagonal = mat[i-1][j-1] + score_nt(seq1[j-1], seq2[i-1])
mat[i][j] = max(score_horizontal, score_vertical, score_diagonal)
```

The formula used to calculate each individual score ensures that the assigned score is always produced from the highest scoring path. By the principles of dynamic programming, we can combine the optimal solutions of subproblems (local highest-scoring paths) to find the solution to the overall problem (highest-scoring global alignment).

### Tracing Back

Since we know that by combining optimal subproblem solutions, we can get the optimal overall solution, we can find the best global alignment by tracing back along the highest-scoring path from the bottom-right corner to the origin.

```
while i > 0 and j > 0:
score_current = mat[i][j]
score_up = mat[i-1][j]
score_left = mat[i][j-1]
score_diagonal = mat[i-1][j-1]
if score_current == score_diagonal + score_nt(seq1[j-1], seq2[i-1]):
aligned1 += seq1[j-1]
aligned2 += seq2[i-1]
i -= 1
j -= 1
elif score_current == score_up + gap_penalty:
aligned1 += '-'
aligned2 += seq2[i-1]
i -= 1
elif score_current == score_left + gap_penalty:
aligned1 += seq1[j-1]
aligned2 += '-'
j -= 1
```

In case of leading gaps, we prepend the gaps.

```
while i > 0:
aligned1 += '-'
aligned2 += seq2[i-1]
i -= 1
while j > 0:
aligned1 += seq1[j-1]
aligned2 += '-'
j -= 1
```

Since we get the result by tracing backwards, the result is reversed. To get the actual result, we simply invert the string.

```
aligned1, aligned2 = aligned1[::-1], aligned2[::-1]
```

## Time Complexity Analysis

The big-O time complexity of the Needleman-Wunsch algorithm is \(O(mn)\) because ultimately we have to go through every element in the matrix. However, by doing some optimization and preprocessing, we can improve the runtime complexity and optimize the constant terms and factors.

## Smith-Waterman

Smith-Waterman is a modified version of Needleman-Wunsch algorithm used for local alignment problems. Rather than attempting to align the whole sequence, local alignment algorithms align the most similar (conserved) regions/segments within the sequences to be aligned. Smith-Waterman's main difference from Needleman-Wunsch is that it uses a slightly different formula to fill in the matrix.

$$ M(i,j)=\max\begin{cases} M(i-1,j-1)+s(i,j) \\ M(i-1,j)+g \\ M(i,j-1)+g \\ 0 \end{cases} $$

Basically, whenever the score falls below zero, the score is set equal to zero. By doing so, the high-scoring segments can easily stand out from the rest of the matrix.

For the traceback step, we starts from the element with the highest score and stops when we hit zero.

```
# locate the max
i = 0
j = 0
max_temp = mat[i][j]
for i_temp in range(0, len(seq2)+1):
for j_temp in range(0, len(seq1)+1):
if mat[i_temp][j_temp] > max_temp:
max_temp = mat[i_temp][j_temp]
i, j = i_temp, j_temp
# backtrace to find the alignment
while i > 0 and j > 0 and mat[i][j] != 0:
score_current = mat[i][j]
score_up = mat[i-1][j]
score_left = mat[i][j-1]
score_diagonal = mat[i-1][j-1]
if score_current == score_diagonal + score_nt(seq1[j-1], seq2[i-1]):
aligned1 += seq1[j-1]
aligned2 += seq2[i-1]
i -= 1
j -= 1
elif score_current == score_up + gap_penalty:
aligned1 += '-'
aligned2 += seq2[i-1]
i -= 1
elif score_current == score_left + gap_penalty:
aligned1 += seq1[j-1]
aligned2 += '-'
j -= 1
while i > 0:
aligned1 += '-'
aligned2 += seq2[i-1]
i -= 1
while j > 0:
aligned1 += seq1[j-1]
aligned2 += '-'
j -= 1
aligned1, aligned2 = aligned1[::-1], aligned2[::-1]
```

## EMBOSS - Needle, Water, Stretcher, and Matcher

EMBOSS (European Molecular Biology Open Software Suite) is a set of bioinformatics tools. Its online version is hosted by EMBL-EBI on https://www.ebi.ac.uk/Tools/emboss/

EMBOSS includes Needle and Water, two alignment tools developed using the Needleman-Wunsch and Smith-Waterman, respectively.

To use Needle or Water only, simply go to the website and paste sequence. We can choose the scoring matrix used to align two sequences as well as gap penalties. They use an affine penalty for gap extension, which is also the more widely used gap penalty function. The affine gap penalty increases by a certain amount when a gap is extended. It is implemented by keeping track of three set of scores, as opposed to one set for linear penalty. The score is assigned using the following set of formulas:

$$ M(i,j)=\max\begin{cases} M(i-1,j-1)+s(i,j) \\ I_x(i-1,j-1)+s(i,j) \\ I_y(i-1,j-1)+s(i,j) \end{cases} $$

$$ I_x(i,j)=\max\begin{cases} M(i-1,j)+h+g \\ I_x(i-1,j)+g \end{cases} $$

$$ I_y(i,j)=\max\begin{cases} M(i,j-1)+h+g \\ I_y(i,j-1)+g \end{cases} $$

where $h$ is the gap opening penalty and $g$ is the gap extension penalty.

EMBOSS also has a command-line version. The mandatory qualifiers are `asequence`

, `bsequence`

, `gapopen`

, `gapextend`

, and `outfile`

.

```
needle -asequence=SEQUENCE1 -bsequence=SEQUENCE2 -gapopen=GAP_OPEN_PENALTY -gapextend=GAP_EXTENSION_PENALTY -outfile=OUTPUT
```

For example, we use the following command to align two sequences named `sarsspike.fa`

and `sars2spike.fa`

with a gap open penalty of 10, gap extension penalty of 0.5, and print the result to `stdout`

.

```
needle -asequence=sarsspike.fa -bsequence=sars2spike.fa -gapopen=10 -gapextend=0.5 -outfile=stdout
```

We can also specify the scoring matrix using the qualifier `-datafile`

. The built-in data files are located at `.../EMBOSS/data`

.

Stretcher is a rapid global alignment tool built using optimized Needleman-Wunsch algorithm, and Matcher is a rapid local alignment tool built using optimized Smith-Waterman.

## Reference

Pevsner, J. (2016). Bioinformatics and Functional Genomics. Chichester, West Sussex: Wiley Blackwell.

Zvelebil, M. J., & Baum, J. O. (2008). Understanding Bioinformatics. New York: Garland Science/Taylor & Francis Group.

Christopher Burge, David Gifford, and Ernest Fraenkel. 7.91J Foundations of Computational and Systems Biology. Spring 2014. Massachusetts Institute of Technology: MIT OpenCourseWare, https://ocw.mit.edu. License: Creative Commons BY-NC-SA.