|
ADS
Algorithms and Data Structures
|
The alignments of two strings \( P \) and \( T \) of exercises 13.7 to 13.9 of the book. More...
Files | |
| file | ADS/string/Alignment.h |
| The alignments of two strings \( P \) and \( T \) of exercises 13.7 to 13.9 of the book: spaces are inserted in \( P \) or \( T \), never one in front of the other, and each column scores \( +1 \) if the characters are equal, \( -1 \) if they differ and \( -2 \) if one is a space. | |
Enumerations | |
| enum class | ADS::Alignment { ADS::Alignment::GLOBAL , ADS::Alignment::SEMIGLOBAL , ADS::Alignment::LOCAL } |
| the kind of alignment More... | |
Functions | |
| template<class S> | |
| int | ADS::alignmentScore (const S &P, const S &T, Alignment kind) |
| the best score of an alignment of \( P \) and \( T \) | |
| template<class S> | |
| std::pair< S, S > | ADS::globalAlignment (const S &P, const S &T, typename S::value_type gap=' ') |
| a best global alignment, the two strings with the spaces as gap | |
The alignments of two strings \( P \) and \( T \) of exercises 13.7 to 13.9 of the book.
The definitions, the invariants and the costs of the area are in Theory of strings.
|
strong |
| int ADS::alignmentScore | ( | const S & | P, |
| const S & | T, | ||
| Alignment | kind ) |
the best score of an alignment of \( P \) and \( T \)
In pseudocode (exercises 13.7 to 13.9), \( \delta_{ij} = +1 \) if \( P[ i - 1 ] = T[ j - 1 ] \) and \( -1 \) otherwise:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{alignmentScore}( P , T , \mathit{kind} )\quad \triangleright\ \text{a gap costs } 2 \text{, } \delta_{ij} \text{ scores } P[ i - 1 ] , T[ j - 1 ] \cr 2 & \quad \mathit{DP}[ i ][ 0 ] \gets -2 i \text{ and } \mathit{DP}[ 0 ][ j ] \gets -2 j \text{ if } \mathit{kind} = \text{GLOBAL, else all } 0\quad \triangleright\ \text{free ends} \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ m\ \textbf{do} \cr 4 & \qquad \textbf{for}\ j \gets 1\ \textbf{to}\ n\ \textbf{do} \cr 5 & \qquad\quad x \gets \max\{ \mathit{DP}[ i - 1 ][ j - 1 ] + \delta_{ij} , \mathit{DP}[ i - 1 ][ j ] - 2 , \mathit{DP}[ i ][ j - 1 ] - 2 \} \cr 6 & \qquad\quad \mathit{DP}[ i ][ j ] \gets \max\{ x , 0 \} \text{ if } \mathit{kind} = \text{LOCAL} \text{, else } x\quad \triangleright \ \text{a local alignment may restart} \cr 7 & \quad \textbf{return}\ \mathit{DP}[ m ][ n ] \text{ for GLOBAL, the largest entry of } \mathit{DP} \text{ for LOCAL,} \cr & \qquad \text{the largest entry of the last row } \text{and of the last column otherwise} \end{array}} \]
| std::pair< S, S > ADS::globalAlignment | ( | const S & | P, |
| const S & | T, | ||
| typename S::value_type | gap = ' ' ) |
a best global alignment, the two strings with the spaces as gap
In pseudocode (exercises 13.7 to 13.9), the table of the global alignment of alignmentScore() read back from \( ( m , n ) \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{globalAlignment}( P , T ) \cr 2 & \quad \mathit{DP} \gets \text{the table of the global alignment};\ ( i , j ) \gets ( m , n );\ A \gets ();\ B \gets () \cr 3 & \quad \textbf{while}\ ( i , j ) \ne ( 0 , 0 )\ \textbf{do}\quad \triangleright\ \text{back along a choice that gives } \mathit{DP}[ i ][ j ] \cr 4 & \qquad \textbf{if}\ i , j > 0 \text{ and } \mathit{DP}[ i ][ j ] = \mathit{DP}[ i - 1 ][ j - 1 ] + \delta_{ij}\ \textbf{then} \cr & \qquad\quad \text{put } P[ i - 1 ] \text{ over } T[ j - 1 ]; \ ( i , j ) \gets ( i - 1 , j - 1 ) \cr 5 & \qquad \textbf{else if}\ i > 0 \text{ and } \mathit{DP}[ i ][ j ] = \mathit{DP}[ i - 1 ][ j ] - 2\ \textbf{then}\ \text{put } P[ i - 1 ] \text{ over a space};\ i \gets i - 1 \cr 6 & \qquad \textbf{else}\ \text{put a space over } T[ j - 1 ];\ j \gets j - 1 \cr 7 & \quad \textbf{return}\ ( A , B )\quad \triangleright \ \text{the two rows, each pair put at their front} \end{array}} \]