This is the Smith-Waterman algorithm (a variation of * Needleman-Wunsch algorithm), finds one optimal local alignment * Modified to find overlap of seq_a and seq_b (alignment that is * pinned at the end of seq_a and beginning of seq_b). * Actually, this should be a variation of needleman algorithm, that * looks for a global alignment, but without penalizing overhangs... * and make sure the a
| 177 | * of seqB). |
| 178 | */ |
| 179 | void alignOverlap(const string& seq_a, const string& seq_b, unsigned seq_a_start_pos, |
| 180 | vector<overlap_align>& overlaps, bool multi_align, bool verbose) |
| 181 | { |
| 182 | // get the actual lengths of the sequences |
| 183 | int N_a = seq_a.length(); |
| 184 | int N_b = seq_b.length(); |
| 185 | |
| 186 | // initialize H |
| 187 | int i, j; |
| 188 | double** H; |
| 189 | int **I_i, **I_j; |
| 190 | H = new double*[N_a+1]; |
| 191 | I_i = new int*[N_a+1]; |
| 192 | I_j = new int*[N_a+1]; |
| 193 | bool** V = new bool*[N_a+1]; |
| 194 | |
| 195 | for(i=0;i<=N_a;i++){ |
| 196 | H[i] = new double[N_b+1]; |
| 197 | I_i[i] = new int[N_b+1]; |
| 198 | I_j[i] = new int[N_b+1]; |
| 199 | H[i][0]=0; //only need to initialize first row and first column |
| 200 | I_i[i][0] = i-1; |
| 201 | V[i] = new bool[N_b+1]; |
| 202 | V[i][0] = true; //valid start |
| 203 | } |
| 204 | |
| 205 | for (j = 0; j <= N_b; j++) { |
| 206 | H[0][j] = 0; //initialize first column |
| 207 | I_j[0][j] = j-1; |
| 208 | V[0][j] = false; //wrong start, not overlap |
| 209 | } |
| 210 | V[0][0] = true; |
| 211 | |
| 212 | for(i=1;i<=N_a;i++){ |
| 213 | for(j=1;j<=N_b;j++){ |
| 214 | char a = seq_a[i-1], b = seq_b[j-1]; |
| 215 | double scores[3] = { |
| 216 | V[i-1][j-1] ? H[i-1][j-1] + matchScore(a, b) |
| 217 | : -DBL_MAX, // match or mismatch |
| 218 | V[i-1][j] ? H[i-1][j] + gapScore(I_j[i-1][j] == j) |
| 219 | : -DBL_MAX, // deletion in sequence A |
| 220 | V[i][j-1] ? H[i][j-1] + gapScore(I_i[i][j-1] == i) |
| 221 | : -DBL_MAX // deletion in sequence B |
| 222 | }; |
| 223 | double* pMax = max_element(scores, scores + 3); |
| 224 | H[i][j] = *pMax; |
| 225 | switch (pMax - scores) { |
| 226 | case 0: // match or mismatch |
| 227 | I_i[i][j] = i-1; |
| 228 | I_j[i][j] = j-1; |
| 229 | break; |
| 230 | case 1: // deletion in sequence A |
| 231 | I_i[i][j] = i-1; |
| 232 | I_j[i][j] = j; |
| 233 | break; |
| 234 | case 2: // deletion in sequence B |
| 235 | I_i[i][j] = i; |
| 236 | I_j[i][j] = j-1; |
no test coverage detected