46 #include <thrust/device_vector.h>
76 std::string
rle(
const char* input)
84 for (
const char* p = input; *p !=
'\0'; ++p)
91 sprintf( buffer + strlen(buffer),
"%u%c", cnt, prev );
97 sprintf( buffer + strlen(buffer),
"%u%c", cnt, prev );
98 return std::string( buffer );
102 template <u
int32 BITS,
typename rand_type>
107 packed_stream_type
stream( storage );
109 for (
uint32 i = 0; i < n; ++i)
110 stream[i] = rand.next() % value_range;
114 template <u
int32 N, u
int32 M,
typename aligner_tag>
118 template <u
int32 N, u
int32 M>
122 char H_flow[N+1][M+1];
126 for (
uint32 i = 0; i <= N; ++i)
128 fprintf(stderr,
"H[%2u] : ", i);
129 for (
uint32 j = 0; j <= M; ++j)
130 fprintf(stderr,
" %3d",
H[i][j]);
131 fprintf(stderr,
"\n");
133 fprintf(stderr,
"\n");
134 for (
uint32 i = 0; i <= N; ++i)
136 fprintf(stderr,
"Hd[%2u] : ", i);
137 for (
uint32 j = 0; j <= M; ++j)
138 fprintf(stderr,
" %c", H_flow[i][j] );
139 fprintf(stderr,
"\n");
141 fprintf(stderr,
"\n");
146 template <u
int32 N, u
int32 M>
150 char H_flow[N+1][M+1];
154 for (
uint32 i = 0; i <= N; ++i)
156 fprintf(stderr,
"H[%2u] : ", i);
157 for (
uint32 j = 0; j <= M; ++j)
158 fprintf(stderr,
" %3d",
H[i][j]);
159 fprintf(stderr,
"\n");
161 fprintf(stderr,
"\n");
162 for (
uint32 i = 0; i <= N; ++i)
164 fprintf(stderr,
"Hd[%2u] : ", i);
165 for (
uint32 j = 0; j <= M; ++j)
166 fprintf(stderr,
" %c", H_flow[i][j] );
167 fprintf(stderr,
"\n");
169 fprintf(stderr,
"\n");
174 template <u
int32 N, u
int32 M>
180 char H_flow[N+1][M+1];
181 char E_flow[N+1][M+1];
182 char F_flow[N+1][M+1];
186 for (
uint32 i = 0; i <= N; ++i)
188 fprintf(stderr,
"H[%2u] : ", i);
189 for (
uint32 j = 0; j <= M; ++j)
190 fprintf(stderr,
" %3d",
H[i][j]);
191 fprintf(stderr,
"\n");
193 fprintf(stderr,
"\n");
194 for (
uint32 i = 0; i <= N; ++i)
196 fprintf(stderr,
"E[%2u] : ", i);
197 for (
uint32 j = 0; j <= M; ++j)
198 fprintf(stderr,
" %3d", E[i][j]);
199 fprintf(stderr,
"\n");
201 fprintf(stderr,
"\n");
202 for (
uint32 i = 0; i <= N; ++i)
204 fprintf(stderr,
"F[%2u] : ", i);
205 for (
uint32 j = 0; j <= M; ++j)
206 fprintf(stderr,
" %3d",
F[i][j]);
207 fprintf(stderr,
"\n");
209 fprintf(stderr,
"\n");
210 for (
uint32 i = 0; i <= N; ++i)
212 fprintf(stderr,
"Hd[%2u] : ", i);
213 for (
uint32 j = 0; j <= M; ++j)
214 fprintf(stderr,
" %c", H_flow[i][j] );
215 fprintf(stderr,
"\n");
217 fprintf(stderr,
"\n");
218 for (
uint32 i = 0; i <= N; ++i)
220 fprintf(stderr,
"Ed[%2u] : ", i);
221 for (
uint32 j = 0; j <= M; ++j)
222 fprintf(stderr,
" %c", E_flow[i][j] );
223 fprintf(stderr,
"\n");
225 fprintf(stderr,
"\n");
226 for (
uint32 i = 0; i <= N; ++i)
228 fprintf(stderr,
"Fd[%2u] : ", i);
229 for (
uint32 j = 0; j <= M; ++j)
230 fprintf(stderr,
" %c", F_flow[i][j] );
231 fprintf(stderr,
"\n");
236 template <u
int32 M, u
int32 N, u
int32 BAND_LEN, aln::AlignmentType TYPE,
typename scheme_type>
239 const scheme_type& scoring = aligner.
scheme;
241 const int32 G = scoring.deletion();
242 const int32 I = scoring.insertion();
243 const int32 S = scoring.mismatch();
244 const int32 V = scoring.match();
250 int32 band[BAND_LEN];
251 for (
int32 j = 0; j < BAND_LEN; ++j)
254 for (
int32 i = 0; i < M; ++i)
256 const uint8 q = str[i];
258 int32 top, left, diagonal, hi;
262 const int32 S_ij = (ref[start+i] == q) ? V : S;
263 diagonal = band[0] + S_ij;
274 for (
uint32 j = 1; j < BAND_LEN-1; ++j)
276 const int32 S_ij = (ref[start+i+j] == q) ? V : S;
277 diagonal = band[j] + S_ij;
279 left = band[j-1] +
I;
290 const int32 S_ij = (ref[start+i+BAND_LEN-1] == q) ? V : S;
291 diagonal = band[BAND_LEN-1] + S_ij;
292 left = band[BAND_LEN-2] +
I;
299 band[BAND_LEN-1] = hi;
303 best_score = band[ BAND_LEN-1 ];
307 best_score = band[0];
308 for (
uint32 j = 1; j < BAND_LEN; ++j)
309 best_score =
nvbio::max( best_score, band[j] );
314 template <u
int32 M, u
int32 N, u
int32 BAND_LEN, aln::AlignmentType TYPE,
typename scheme_type>
317 const scheme_type& scoring = aligner.
scheme;
319 const int32 G_o = scoring.pattern_gap_open();
320 const int32 G_e = scoring.pattern_gap_extension();
321 const int32 S = scoring.mismatch();
322 const int32 V = scoring.match();
329 int32 H_band[BAND_LEN];
330 int32 F_band[BAND_LEN];
333 for (
uint32 j = 1; j < BAND_LEN; ++j)
334 H_band[j] =
TYPE ==
GLOBAL ? scoring.text_gap_open() + (j-1)*scoring.text_gap_extension() : 0;
336 for (
uint32 j = 0; j < BAND_LEN; ++j)
340 #if defined(DEBUG_THIS)
341 FILE* file = fopen(
"debug.txt",
"w");
342 #define DEBUG_THIS_STATEMENT(x) x
344 #define DEBUG_THIS_STATEMENT(x)
347 for (
uint32 i = 0; i < M; ++i)
349 #if defined(DEBUG_THIS)
351 fprintf(file,
"F[%2u] = ", i);
352 for (
uint32 j = 0; j < BAND_LEN; ++j)
353 fprintf(file,
" %3d", F_band[j]);
356 fprintf(file,
"H[%2u] = ", i);
357 for (
uint32 j = 0; j < BAND_LEN; ++j)
358 fprintf(file,
" %3d", H_band[j]);
359 fprintf(file,
"\n\n");
363 const uint8 q = pattern[i];
368 for (
uint32 j = 0; j < BAND_LEN-1; ++j)
370 const int32 ftop = F_band[j+1] + G_e;
371 const int32 htop = H_band[j+1] + G_o;
373 if (i && (j == BAND_LEN-2))
374 assert( F_band[j] == htop );
378 F_band[BAND_LEN-1] = infimum;
383 const int32 S_ij = (text[start+i] == q) ? V : S;
384 const int32 diagonal = H_band[0] + S_ij;
385 const int32 top = F_band[0];
399 int32 E_j = H_band[0] + G_o;
401 for (
uint32 j = 1; j < BAND_LEN-1; ++j)
403 const uint32 g = text[start+i+j];
405 const int32 S_ij = (g == q) ? V : S;
406 const int32 diagonal = H_band[j] + S_ij;
407 const int32 top = F_band[j];
408 const int32 left = E_j;
416 DEBUG_THIS_STATEMENT( fprintf(file,
" %c", (top > left ? (top > diagonal ?
'I' :
'S') : (left > diagonal ?
'D' :
'S'))) );
419 const int32 eleft = E_j + G_e;
420 const int32 ediagonal = hi + G_o;
426 const uint8 g = text[start+i+BAND_LEN-1];
428 const int32 S_ij = (g == q) ? V : S;
429 const int32 diagonal = H_band[ BAND_LEN-1 ] + S_ij;
430 const int32 left = E_j;
437 H_band[ BAND_LEN-1 ] = hi;
447 best_score = H_band[ BAND_LEN-1 ];
451 best_score = H_band[0];
452 for (
uint32 j = 1; j < BAND_LEN; ++j)
453 best_score =
nvbio::max( best_score, H_band[j] );
457 #if defined(DEBUG_THIS)
458 #undef DEBUG_THIS_STATEMENT
463 template <u
int32 M, u
int32 N, aln::AlignmentType TYPE,
typename scheme_type>
470 const scheme_type& scoring = aligner.
scheme;
472 const int32 G = scoring.deletion();
473 const int32 I = scoring.insertion();
474 const int32 S = scoring.mismatch();
475 const int32 V = scoring.match();
479 for (
uint32 j = 1; j <= M; ++j)
484 for (
uint32 i = 1; i <= N; ++i)
492 for (
uint32 i = 1; i <= N; ++i)
494 for (
uint32 j = 1; j <= M; ++j)
496 const uint8 r_i = ref[i-1];
497 const uint8 s_j = str[j-1];
498 const int32 S_ij = (r_i == s_j) ? V : S;
500 const int32 top = mat->
H[i-1][j] +
G;
501 const int32 left = mat->
H[i][j-1] +
I;
502 const int32 diag = mat->
H[i-1][j-1] + S_ij;
506 mat->
H_flow[i][j] = top > left ?
507 (top > diag ?
'|' :
'\\') :
508 (left > diag ?
'-' :
'\\');
514 best_score =
nvbio::max( best_score, mat->
H[i][M] );
522 template <u
int32 M, u
int32 N, aln::AlignmentType TYPE>
536 template <u
int32 M, u
int32 N, aln::AlignmentType TYPE,
typename scheme_type>
543 const scheme_type& scoring = aligner.
scheme;
545 const int32 G_o = scoring.pattern_gap_open();
546 const int32 G_e = scoring.pattern_gap_extension();
547 const int32 S = scoring.mismatch();
548 const int32 V = scoring.match();
556 for (
uint32 j = 1; j <= M; ++j)
558 mat->
H[0][j] = (
TYPE !=
LOCAL) ? G_o + G_e * (j-1) : 0;
559 mat->
E[0][j] = mat->
F[0][j] = (
TYPE !=
LOCAL) ? -100000 : 0;
564 for (
uint32 i = 1; i <= N; ++i)
567 mat->
E[i][0] = mat->
F[i][0] = (
TYPE !=
LOCAL) ? -100000 : 0;
576 for (
uint32 i = 1; i <= N; ++i)
578 for (
uint32 j = 1; j <= M; ++j)
580 const uint8 r_i = ref[i-1];
581 const uint8 s_j = str[j-1];
582 const int32 S_ij = (r_i == s_j) ? V : S;
584 mat->
E_flow[i][j] = mat->
E[i][j-1] + G_e > mat->
H[i][j-1] + G_o ?
'-' :
'H';
585 mat->
F_flow[i][j] = mat->
F[i-1][j] + G_e > mat->
H[i-1][j] + G_o ?
'|' :
'H';
587 mat->
E[i][j] =
nvbio::max( mat->
E[i][j-1] + G_e, mat->
H[i][j-1] + G_o );
588 mat->
F[i][j] =
nvbio::max( mat->
F[i-1][j] + G_e, mat->
H[i-1][j] + G_o );
590 mat->
H_flow[i][j] = mat->
F[i][j] > mat->
E[i][j] ?
591 (mat->
F[i][j] > mat->
H[i-1][j-1] + S_ij ?
'F' :
'\\') :
592 (mat->
E[i][j] > mat->
H[i-1][j-1] + S_ij ?
'E' :
'\\');
594 mat->
H[i][j] =
nvbio::max3( mat->
H[i-1][j-1] + S_ij, mat->
E[i][j], mat->
F[i][j] );
600 if (mat->
H[i][j] == 0)
603 if (best_score < mat->
H[i][j])
605 best_score = mat->
H[i][j];
606 best_sink = make_uint2( i, j );
612 if (best_score < mat->
H[i][M])
614 best_score = mat->
H[i][M];
615 best_sink = make_uint2( i, M );
635 for (
uint32 i = 0; i < len; ++i)
647 template <AlignmentType TYPE>
658 const char op =
aln[aln_len-a-1];
681 score -= (str[j] == ref[k]) ? 0 : 1;
692 template <AlignmentType TYPE,
typename scoring_type>
699 const scoring_type& scoring = aligner.
scheme;
705 const char op =
aln[aln_len-a-1];
712 score += scoring.insertion();
720 score += scoring.deletion();
728 score += (str[j] == ref[k]) ? scoring.match() : scoring.mismatch();
739 template <AlignmentType TYPE,
typename scoring_type>
746 const scoring_type& scoring = aligner.
scheme;
753 const char op =
aln[aln_len-a-1];
760 score += (state != op) ? scoring.pattern_gap_open() : scoring.pattern_gap_extension();
768 score += (state != op) ? scoring.text_gap_open() : scoring.text_gap_extension();
776 score += (str[j] == ref[k]) ? scoring.match() : scoring.mismatch();