start from $M_{(i,j)}$ where $i$ and $j$ are

the last indices in 1st and 2nd dimension for NW

the indices for the matrix with the maximum score in SW

Check the score from

$M_{(i,j-1)}$ (cell to the left), a gap score

$M_{(i-1,j)}$ (cell above), a gap score

$M_{(i-1,j-1)}$ (cell from diagonal), a match or mismatch

If any match your current cell, push correct characters to alignments

push gap to

`seq1`

, character at $j$ to`seq2`

push character at $i$ to

`seq1`

, gap to`seq2`

push character at $i$ to

`seq1`

, character at $j$ to =seq2=

update indices

Be mindful of what happens when you hit the first row or first column

$i-1$ or $j-1$ may throw bounds error

When should your loop stop?

It will be different for Needleman-Wunsch than for Smith-Waterman

```
@testset "Lab07" begin
@test nwalign("AATTGGCC", "AATTTGCC") == ("AATTGGCC", "AATTTGCC")
@test any(aln -> aln == nwalign("AATTGGCC", "AAGGTTCC"), [
("AA--TTGGCC", "AAGGTT--CC"),
("AATTGGCC", "AAGGTTCC")])
@test nwalign("AATTGGCC", "AAGGTTCC", gap=-2) == ("AATTGGCC", "AAGGTTCC")
@test nwalign("AATTGGCC", "AAGGTTCC", mismatch=-2) == ("AA--TTGGCC", "AAGGTT--CC")
@test swalign("AAT", "AA") == ("AA", "AA")
@test swalign("AAAAATTGGCCAAAAA", "ATTGGCCA") == ("ATTGGCCA", "ATTGGCCA")
@test swalign("AAAAATTGGCCAAAAA", "ATTGGCAAA") == ("ATTGGCCAAA", "ATTGGC-AAA")
end
```

© Kevin Bonham, PhD. Last modified: August 18, 2021. Website built with Franklin.jl and the Julia programming language.