\chapter{Section alignment} \section{Needleman - Wunsch algorithm} \begin{algorithm} \caption{Needleman-Wunsch Algorithm} \begin{algorithmic}[1] \Procedure{FillMatrix}{$S_{1}$: Array($m$), $S_{2}$: Array($n$)} \Comment{$sub(a, b)$ is the substitution score, $del(a)$ and $ins(a)$ are the deletion and insertion penalty, in regard with the reference $S_{1}$ sequence} \State $M = $ Array($m+1$, $n+1$) \Comment{Initialize the matrix first column and first row} \State $P = $ Array($m$, $n$) \Comment{Store the direction of the cell we chose to build the next cell up on.} \For {($i = 0$; $i < m+1$; $i++$)} \State $M[i][0] = i * del(S_{1}[i])$ \EndFor \For {($j = 0$; $j < n+1$; $j++$)} \State $M[0][j] = j * ins(S_{2}[j])$ \EndFor \Comment{Fill the remaining matrix} \For {($i = 1$; $i < m+1$; $i++$)} \For {($j = 1$; $j < n+1$; $j++$)} \State $delete = M[i-1][j] + del(S_{1}[i-1])$ \State $insert = M[i][j-1] + ins(S_{2}[j-1])$ \State $substitute = M[i-1][j-1] + sub(S_{1}[i-1], S_{2}[j-1])$ \State $choice = \max \{delete, insert, substitute\}$ \If {$substitute = choice$} \State $P[i-1][j-1] = '\nwarrow'$ \ElsIf {$insertion = choice$} \State $P[i-1][j-1] = '\uparrow'$ \Else \State $P[i-1][j-1] = '\leftarrow'$ \EndIf \State $M[i][j] = choice$ \EndFor \EndFor \EndProcedure \Procedure{ShowAlignment}{$S_{1}$: Array($m$), $S_{2}$: Array($n$)} \State $extend_{1} = ''$ \State $extend_{2} = ''$ \State $i = m$ \State $j = n$ \While{$i > 0$ and $j > 0$} \If {$P[i-1][j-1] = '\nwarrow'$} \State $extend_{1} = S_{1}[i-1] \circ extend_{1}$ \State $extend_{2} = S_{2}[j-1] \circ extend_{2}$ \State $i--$ \State $j--$ \ElsIf {$P[i-1][j-1] = '\uparrow'$} \State $extend_{1} = S_{1}[i-1] \circ extend_{1}$ \State $extend_{2} =\quad '-' \circ extend_{2}$ \State $i--$ \Else \State $extend_{1} =\quad '-' \circ extend_{1}$ \State $extend_{2} = S_{2}[j-1] \circ extend_{2}$ \State $j--$ \EndIf \EndWhile \State \Call{print}{$extend_{1}$} \State \Call{print}{$extend_{2}$} \EndProcedure \State \Call{FillMatrix}{$S_{1}$, $S_{2}$} \State \Call ShowAlignment($S_{1}$, $S_{2}$) \end{algorithmic} \end{algorithm}