<p>The formula your slide shows describes a dynamic programming with a general gap cost. Finding the optimal solution takes $O(n^3)$ time. No one is using the algorithm these days. This is the first time I see an attempt to fill a matrix with this algorithm. It is interesting, but I am not sure how it works. Although affine gap cost is a special case of general gap cost, the algorithm to find the best score is very different, so is the time complexity. With modern affine gap formulations, each cell holds three values:</p>

<ol>
<li>The overall optimal score (or the optimal score if this cell is a match, which yields an equivalent but different formulation)</li>
<li>The optimal score when the cell ends at a deletion</li>
<li>The optimal score when the cell ends at an insertion</li>
</ol>

<p>The values in each cell only depends on the up, left and up-left cells in the matrix. You fill the matrix from the top left corner to the bottom right. This is an $O(n^2)$ algorithm, because you traverse each cell once. For more details, you can read <a href="https://figshare.com/articles/Notes_on_pairwise_alignment_with_dynamic_programming/5223973" rel="nofollow noreferrer">this note</a>. I would recommend to learn affine gap cost the normal way.</p>

CC BY-SA 4.0
2020-09-06T02:02:01.247Z
which algorithms produce local alignments under $O(n^3)$? Are there any R implementations of such algorithms?
Vass
CC BY-SA 3.0
2017-10-04T01:00:11.117Z
2017-10-04T01:00:11.117Z
user172818
<p><strong>The linear gap penalty</strong> has a cost which is directly proportional to the length of the gap: the gap costs 1 unit per consecutive missing base.<br>
The simplest case is assigning each unit (= each missing base) a penalty of 1:</p>

<blockquote>
  <p>g(k) = k</p>
</blockquote>

<p>Or more generally, if you want to increase the penalty per base to 2, 3 or n:</p>

<blockquote>
  <p>g(k) = n * k<br>
  with k = length of the gap, and n = cost of 1 missing base</p>
</blockquote>

<p><strong>The affine gap penalty</strong> has a cost that follows -as its name conveniently states- an affine function.<br>
The idea behind it, is that any gap in an alignment has a cost with two components:</p>

<ul>
<li><strong>the "gap opening" penalty</strong>: it is a cost payed only once per gap, basically just for existing.</li>
<li><strong>the "gap extension" penalty:</strong> equivalent to the linear gap penalty, it costs 1 unit for each consecutive missing base.</li>
</ul>

<p>The general formula for the cost is then:</p>

<blockquote>
  <p>g(k) = a + n*k<br>
  with a= opening penalty, n= extension penalty, k= length of the gap</p>
</blockquote>

<p>Compared to the linear gap penalty, this will give a small bonus to longer consecutive gaps compared to skipping one nucleotide, matching one, skipping one again etc (because in the case of longer gaps, you only pay the opening penalty once, so the global cost per base decreases).</p>

<p><strong>Cells on your diagram:</strong><br>
From what I understand, you're working with an affine penalty of g(k) = 1 + 1*k. For the [3,4] you're "coming" from the score of 2 on the left (cell 1,2). You either:</p>

<ul>
<li>match the A (row 1) with G (column 3) and get a penalty of 1 for substitution -> total of previous 2 + new 1 = 3</li>
<li>open a gap to skip the G (column 3) and get a penalty of 2 (1 for opening + 1 because gap length of 1) -> total of previous 2 + new 2 = 4.</li>
</ul>

<p>So you choose the lesser penalty and go with 3, which is the number selected in the bigger box.<br>
The explanation is the exact same one for the other cell you mentioned. </p>

CC BY-SA 3.0
2017-10-03T15:58:48.873Z
I believe I understand now. The reason why we can get 3 or 4 is because in the previous cell, the optimal score of 2 could have come from either the left or the diagonal. If it had come from the diagonal, we are starting a new gap and thus we get 2 + (1 + 1) = 4. However, if we come from the left, we are simply adding to the already existing gap so we get 2 + 1 = 3. Is this the correct way to think about it?
H5159
CC BY-SA 3.0
2017-10-03T13:41:56.080Z
2017-10-03T14:42:31.397Z
2017-10-03T14:42:31.397Z
Kerkyra
<p>I am having trouble understanding the affine gap penalty in the following example -</p>

<p><a href="https://i.sstatic.net/mZTnP.png" rel="noreferrer"><img src="https://i.sstatic.net/mZTnP.png" alt="enter image description here"></a>
<a href="https://i.sstatic.net/7f2Pr.png" rel="noreferrer"><img src="https://i.sstatic.net/7f2Pr.png" alt="enter image description here"></a>
<a href="https://i.sstatic.net/Pt01s.png" rel="noreferrer"><img src="https://i.sstatic.net/Pt01s.png" alt="enter image description here"></a></p>

<p>I am not sure where the 3 and 4 come from or the 4 and 5 in cells 1,3 and 2,4. I'm not understanding how the affine gap penalty works here. I understand how the linear gap penalty worked in the Needlemen_Wunsch but not this.</p>

<phylogenetics>
Waterman-Smith-Beyer Sequence Alignment
# Waterman-Smith-Beyer Sequence Alignment

(Asked by: username_0 on 2017-10-02)

I am having trouble understanding the affine gap penalty in the following example -

[![enter image description here](https://i.sstatic.net/mZTnP.png)](https://i.sstatic.net/mZTnP.png)
[![enter image description here](https://i.sstatic.net/7f2Pr.png)](https://i.sstatic.net/7f2Pr.png)
[![enter image description here](https://i.sstatic.net/Pt01s.png)](https://i.sstatic.net/Pt01s.png)

I am not sure where the 3 and 4 come from or the 4 and 5 in cells 1,3 and 2,4. I'm not understanding how the affine gap penalty works here. I understand how the linear gap penalty worked in the Needlemen\_Wunsch but not this.

**Tags:** phylogenetics

---

## 2 answers

### Answer by username_3 on 2017-10-03. Score: 5 | Accepted answer

**The linear gap penalty** has a cost which is directly proportional to the length of the gap: the gap costs 1 unit per consecutive missing base.  
The simplest case is assigning each unit (= each missing base) a penalty of 1:

> g(k) = k

Or more generally, if you want to increase the penalty per base to 2, 3 or n:

> g(k) = n \* k  
> with k = length of the gap, and n = cost of 1 missing base

**The affine gap penalty** has a cost that follows -as its name conveniently states- an affine function.  
The idea behind it, is that any gap in an alignment has a cost with two components:

* **the "gap opening" penalty**: it is a cost payed only once per gap, basically just for existing.
* **the "gap extension" penalty:** equivalent to the linear gap penalty, it costs 1 unit for each consecutive missing base.

The general formula for the cost is then:

> g(k) = a + n\*k  
> with a= opening penalty, n= extension penalty, k= length of the gap

Compared to the linear gap penalty, this will give a small bonus to longer consecutive gaps compared to skipping one nucleotide, matching one, skipping one again etc (because in the case of longer gaps, you only pay the opening penalty once, so the global cost per base decreases).

**Cells on your diagram:**  
From what I understand, you're working with an affine penalty of g(k) = 1 + 1\*k. For the [3,4] you're "coming" from the score of 2 on the left (cell 1,2). You either:

* match the A (row 1) with G (column 3) and get a penalty of 1 for substitution -> total of previous 2 + new 1 = 3
* open a gap to skip the G (column 3) and get a penalty of 2 (1 for opening + 1 because gap length of 1) -> total of previous 2 + new 2 = 4.

So you choose the lesser penalty and go with 3, which is the number selected in the bigger box.  
The explanation is the exact same one for the other cell you mentioned.

> **Comments:**
> **username_0**: I believe I understand now. The reason why we can get 3 or 4 is because in the previous cell, the optimal score of 2 could have come from either the left or the diagonal. If it had come from the diagonal, we are starting a new gap and thus we get 2 + (1 + 1) = 4. However, if we come from the left, we are simply adding to the already existing gap so we get 2 + 1 = 3. Is this the correct way to think about it?  

---

### Answer by username_1 on 2017-10-04. Score: 2

The formula your slide shows describes a dynamic programming with a general gap cost. Finding the optimal solution takes $O(n^3)$ time. No one is using the algorithm these days. This is the first time I see an attempt to fill a matrix with this algorithm. It is interesting, but I am not sure how it works. Although affine gap cost is a special case of general gap cost, the algorithm to find the best score is very different, so is the time complexity. With modern affine gap formulations, each cell holds three values:

1. The overall optimal score (or the optimal score if this cell is a match, which yields an equivalent but different formulation)
2. The optimal score when the cell ends at a deletion
3. The optimal score when the cell ends at an insertion

The values in each cell only depends on the up, left and up-left cells in the matrix. You fill the matrix from the top left corner to the bottom right. This is an $O(n^2)$ algorithm, because you traverse each cell once. For more details, you can read [this note](https://figshare.com/articles/Notes_on_pairwise_alignment_with_dynamic_programming/5223973). I would recommend to learn affine gap cost the normal way.

> **Comments:**
> **username_2**: which algorithms produce local alignments under $O(n^3)$? Are there any R implementations of such algorithms?  

---

