| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include <esl_config.h> |
| 13 |
#include <p7_config.h> |
| 14 |
#include "config.h" |
| 15 |
|
| 16 |
#include <stdio.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <math.h> |
| 19 |
|
| 20 |
#include "easel.h" |
| 21 |
#include "esl_sqio.h" |
| 22 |
#include "esl_stack.h" |
| 23 |
#include "esl_stopwatch.h" |
| 24 |
#include "esl_vectorops.h" |
| 25 |
|
| 26 |
#include "hmmer.h" |
| 27 |
|
| 28 |
#include "infernal.h" |
| 29 |
|
| 30 |
#define CP9TSC(s,k) (tsc[(k) * cp9O_NTRANS + (s)]) |
| 31 |
|
| 32 |
#define IAMI_M (1<<0) |
| 33 |
#define IAMI_I (1<<1) |
| 34 |
#define IAMI_D (1<<2) |
| 35 |
#define IAMI_EL (1<<3) |
| 36 |
#define nIAMI_S 4 |
| 37 |
|
| 38 |
#define IAMI_MM (1<<0) |
| 39 |
#define IAMI_IM (1<<1) |
| 40 |
#define IAMI_DM (1<<2) |
| 41 |
#define IAMI_BM (1<<3) |
| 42 |
#define IAMI_MI (1<<4) |
| 43 |
#define IAMI_II (1<<5) |
| 44 |
#define IAMI_DI (1<<6) |
| 45 |
#define IAMI_MD (1<<7) |
| 46 |
#define IAMI_ID (1<<8) |
| 47 |
#define IAMI_DD (1<<9) |
| 48 |
#define IAMI_ME (1<<10) |
| 49 |
#define IAMI_MEL (1<<11) |
| 50 |
#define nIAMI_T 12 |
| 51 |
|
| 52 |
#define TDIFF_MI_M 0 |
| 53 |
#define TDIFF_MI_I 1 |
| 54 |
#define TDIFF_MI_D 2 |
| 55 |
#define nTDIFF 3 |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
int |
| 74 |
cp9_Viterbi(CP9_t *cp9, char *errbuf, CP9_MX *mx, ESL_DSQ *dsq, int i0, int j0, int do_scan, int doing_align, |
| 75 |
int be_efficient, int **ret_psc, int *ret_maxres, CP9trace_t **ret_tr, float *ret_sc) |
| 76 |
{ |
| 77 |
int status; |
| 78 |
int j; |
| 79 |
int jp; |
| 80 |
int cur, prv; |
| 81 |
int k; |
| 82 |
int L; |
| 83 |
int **mmx; |
| 84 |
int **imx; |
| 85 |
int **dmx; |
| 86 |
int **elmx; |
| 87 |
int *erow; |
| 88 |
int *scA; |
| 89 |
float fsc; |
| 90 |
float best_sc; |
| 91 |
float best_pos; |
| 92 |
int nrows; |
| 93 |
int c; |
| 94 |
CP9trace_t *tr; |
| 95 |
int M; |
| 96 |
|
| 97 |
|
| 98 |
if(dsq == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_Viterbi, dsq is NULL."); |
| 99 |
if(mx == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_Viterbi, mx is NULL.\n"); |
| 100 |
|
| 101 |
best_sc = IMPOSSIBLE; |
| 102 |
best_pos = -1; |
| 103 |
L = j0-i0+1; |
| 104 |
M = cp9->M; |
| 105 |
|
| 106 |
int const *tsc = cp9->otsc; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
if(be_efficient) nrows = 1; |
| 111 |
else nrows = L; |
| 112 |
if((status = GrowCP9Matrix(mx, errbuf, nrows, M, NULL, NULL, &mmx, &imx, &dmx, &elmx, &erow)) != eslOK) return status; |
| 113 |
ESL_DPRINTF1(("#DEBUG: cp9_Viterbi(): CP9 matrix size: %.8f Mb rows: %d.\n", mx->size_Mb, mx->rows)); |
| 114 |
|
| 115 |
|
| 116 |
ESL_ALLOC(scA, sizeof(int) * (j0-i0+2)); |
| 117 |
|
| 118 |
|
| 119 |
mmx[0][0] = 0; |
| 120 |
imx[0][0] = -INFTY; |
| 121 |
dmx[0][0] = -INFTY; |
| 122 |
elmx[0][0]= -INFTY; |
| 123 |
erow[0] = -INFTY; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
int sc; |
| 128 |
for (k = 1; k <= M; k++) |
| 129 |
{ |
| 130 |
mmx[0][k] = imx[0][k] = elmx[0][k] = -INFTY; |
| 131 |
sc = ESL_MAX(mmx[0][k-1] + CP9TSC(cp9O_MD,k-1), |
| 132 |
imx[0][k-1] + CP9TSC(cp9O_ID,k-1)); |
| 133 |
sc = ESL_MAX(sc, dmx[0][k-1] + CP9TSC(cp9O_DD,k-1)); |
| 134 |
dmx[0][k] = sc; |
| 135 |
} |
| 136 |
|
| 137 |
erow[0] = dmx[0][M] + CP9TSC(cp9O_DM,M); |
| 138 |
scA[0] = erow[0]; |
| 139 |
fsc = Scorify(scA[0]); |
| 140 |
|
| 141 |
if(fsc > best_sc) { best_sc = fsc; best_pos= i0-1; } |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
for (j = i0; j <= j0; j++) |
| 149 |
{ |
| 150 |
int const *isc = cp9->isc[dsq[j]]; |
| 151 |
int const *msc = cp9->msc[dsq[j]]; |
| 152 |
|
| 153 |
int endsc = -INFTY; |
| 154 |
int el_selfsc = cp9->el_selfsc; |
| 155 |
int sc; |
| 156 |
|
| 157 |
jp = j-i0+1; |
| 158 |
cur = (j-i0+1); |
| 159 |
prv = (j-i0); |
| 160 |
|
| 161 |
if(be_efficient) { |
| 162 |
cur %= 2; |
| 163 |
prv %= 2; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
mmx[cur][0] = (do_scan == TRUE) ? 0 : -INFTY; |
| 171 |
dmx[cur][0] = -INFTY; |
| 172 |
elmx[cur][0] = -INFTY; |
| 173 |
|
| 174 |
sc = ESL_MAX(mmx[prv][0] + CP9TSC(cp9O_MI,0), |
| 175 |
imx[prv][0] + CP9TSC(cp9O_II,0)); |
| 176 |
sc = ESL_MAX(sc, dmx[prv][0] + CP9TSC(cp9O_DI,0)); |
| 177 |
imx[cur][0] = ESL_MAX(sc + isc[0], -INFTY); |
| 178 |
|
| 179 |
for (k = 1; k <= M; k++) |
| 180 |
{ |
| 181 |
|
| 182 |
sc = ESL_MAX( mmx[prv][k-1] + CP9TSC(cp9O_MM,k-1), |
| 183 |
imx[prv][k-1] + CP9TSC(cp9O_IM,k-1)); |
| 184 |
sc = ESL_MAX(sc, dmx[prv][k-1] + CP9TSC(cp9O_DM,k-1)); |
| 185 |
sc = ESL_MAX(sc, mmx[prv][0] + CP9TSC(cp9O_BM,k)); |
| 186 |
|
| 187 |
for(c = 0; c < cp9->el_from_ct[k]; c++) |
| 188 |
sc = ESL_MAX(sc, elmx[prv][cp9->el_from_idx[k][c]]); |
| 189 |
|
| 190 |
mmx[cur][k] = ESL_MAX(sc + msc[k], -INFTY); |
| 191 |
|
| 192 |
|
| 193 |
endsc = ESL_MAX(endsc, mmx[cur][k] + CP9TSC(cp9O_ME,k)); |
| 194 |
|
| 195 |
|
| 196 |
sc = ESL_MAX( mmx[prv][k] + CP9TSC(cp9O_MI,k), |
| 197 |
imx[prv][k] + CP9TSC(cp9O_II,k)); |
| 198 |
sc = ESL_MAX(sc, dmx[prv][k] + CP9TSC(cp9O_DI,k)); |
| 199 |
imx[cur][k] = ESL_MAX(sc + isc[k], -INFTY); |
| 200 |
|
| 201 |
|
| 202 |
sc = ESL_MAX( mmx[cur][k-1] + CP9TSC(cp9O_MD,k-1), |
| 203 |
imx[cur][k-1] + CP9TSC(cp9O_ID,k-1)); |
| 204 |
sc = ESL_MAX(sc, dmx[cur][k-1] + CP9TSC(cp9O_DD,k-1)); |
| 205 |
dmx[cur][k] = sc; |
| 206 |
|
| 207 |
|
| 208 |
sc = -INFTY; |
| 209 |
if((cp9->flags & CPLAN9_EL) && cp9->has_el[k]) |
| 210 |
|
| 211 |
{ |
| 212 |
sc = ESL_MAX(sc, mmx[cur][k] + CP9TSC(cp9O_MEL,k)); |
| 213 |
sc = ESL_MAX(sc, elmx[prv][k] + el_selfsc); |
| 214 |
} |
| 215 |
elmx[cur][k] = sc; |
| 216 |
} |
| 217 |
|
| 218 |
endsc = ESL_MAX(endsc, dmx[cur][M] + CP9TSC(cp9O_DM,M)); |
| 219 |
endsc = ESL_MAX(endsc, imx[cur][M] + CP9TSC(cp9O_IM,M)); |
| 220 |
for(c = 0; c < cp9->el_from_ct[M+1]; c++) |
| 221 |
|
| 222 |
endsc = ESL_MAX(endsc, elmx[cur][cp9->el_from_idx[M+1][c]]); |
| 223 |
|
| 224 |
erow[cur] = endsc; |
| 225 |
scA[jp] = endsc; |
| 226 |
fsc = Scorify(endsc); |
| 227 |
|
| 228 |
if(fsc > best_sc) { best_sc = fsc; best_pos= j; } |
| 229 |
} |
| 230 |
|
| 231 |
if(doing_align) { |
| 232 |
best_sc = Scorify(scA[(j0-i0+1)]); |
| 233 |
best_pos = i0; |
| 234 |
if(ret_tr != NULL) { |
| 235 |
CP9ViterbiTrace(cp9, dsq, i0, j0, mx, &tr); |
| 236 |
if(tr == NULL) ESL_FAIL(eslFAIL, errbuf, "CP9ViterbiTrace() returned NULL, problem with traceback."); |
| 237 |
|
| 238 |
*ret_tr = tr; |
| 239 |
} |
| 240 |
} |
| 241 |
if(ret_sc != NULL) *ret_sc = best_sc; |
| 242 |
if(ret_maxres != NULL) *ret_maxres = best_pos; |
| 243 |
if(ret_psc != NULL) *ret_psc = scA; |
| 244 |
else free(scA); |
| 245 |
ESL_DPRINTF1(("#DEBUG: cp9_Viterbi() return score: %10.4f\n", best_sc)); |
| 246 |
|
| 247 |
return eslOK; |
| 248 |
|
| 249 |
ERROR: |
| 250 |
ESL_FAIL(eslEMEM, errbuf, "Memory allocation error."); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
int |
| 271 |
cp9_ViterbiBackward(CP9_t *cp9, char *errbuf, CP9_MX *mx, ESL_DSQ *dsq, int i0, int j0, int do_scan, int doing_align, |
| 272 |
int be_efficient, int **ret_psc, int *ret_maxres, CP9trace_t **ret_tr, float *ret_sc) |
| 273 |
{ |
| 274 |
int status; |
| 275 |
int i; |
| 276 |
int ip; |
| 277 |
int cur, prv; |
| 278 |
int k; |
| 279 |
int L; |
| 280 |
int **mmx; |
| 281 |
int **imx; |
| 282 |
int **dmx; |
| 283 |
int **elmx; |
| 284 |
int *erow; |
| 285 |
int *scA; |
| 286 |
float fsc; |
| 287 |
float best_sc; |
| 288 |
float best_pos; |
| 289 |
int nrows; |
| 290 |
int c; |
| 291 |
int M; |
| 292 |
|
| 293 |
|
| 294 |
if(cp9 == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_ViterbiBackward, cp9 is NULL.\n"); |
| 295 |
if(dsq == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_ViterbiBackward, dsq is NULL."); |
| 296 |
if(mx == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_ViterbiBackward, mx is NULL.\n"); |
| 297 |
|
| 298 |
int const *tsc = cp9->otsc; |
| 299 |
|
| 300 |
best_sc = IMPOSSIBLE; |
| 301 |
best_pos = -1; |
| 302 |
L = j0-i0+1; |
| 303 |
M = cp9->M; |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
if(be_efficient) nrows = 1; |
| 308 |
else nrows = L; |
| 309 |
if((status = GrowCP9Matrix(mx, errbuf, nrows, M, NULL, NULL, &mmx, &imx, &dmx, &elmx, &erow)) != eslOK) return status; |
| 310 |
ESL_DPRINTF1(("#DEBUG: cp9_ViterbiBackward(): CP9 matrix size: %.8f Mb rows: %d.\n", mx->size_Mb, mx->rows)); |
| 311 |
|
| 312 |
|
| 313 |
ESL_ALLOC(scA, sizeof(int) * (j0-i0+2)); |
| 314 |
|
| 315 |
|
| 316 |
if(be_efficient) cur = 0; |
| 317 |
else cur = j0-i0+1; |
| 318 |
ip = j0-i0+1; |
| 319 |
i = j0; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
for (k = 1; k <= cp9->M; k++) elmx[cur][k] = -INFTY; |
| 327 |
if(cp9->flags & CPLAN9_EL) { |
| 328 |
for(c = 0; c < cp9->el_from_ct[cp9->M+1]; c++) |
| 329 |
elmx[cur][cp9->el_from_idx[cp9->M+1][c]] = 0.; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
mmx[cur][cp9->M] = ESL_MAX(elmx[cur][cp9->M] + CP9TSC(cp9O_MEL,cp9->M), |
| 339 |
CP9TSC(cp9O_ME,cp9->M)); |
| 340 |
mmx[cur][cp9->M] += cp9->msc[dsq[i]][cp9->M]; |
| 341 |
imx[cur][cp9->M] = CP9TSC(cp9O_IM,cp9->M); |
| 342 |
imx[cur][cp9->M] += cp9->isc[dsq[i]][cp9->M]; |
| 343 |
dmx[cur][cp9->M] = CP9TSC(cp9O_DM,cp9->M); |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
for (k = cp9->M-1; k >= 1; k--) |
| 351 |
{ |
| 352 |
mmx[cur][k] = CP9TSC(cp9O_ME,k); |
| 353 |
mmx[cur][k] = ESL_MAX(mmx[cur][k], dmx[cur][k+1] + CP9TSC(cp9O_MD,k)); |
| 354 |
if(cp9->flags & CPLAN9_EL) |
| 355 |
mmx[cur][k] = ESL_MAX(mmx[cur][k], elmx[cur][k] + CP9TSC(cp9O_MEL,k)); |
| 356 |
mmx[cur][k] += cp9->msc[dsq[i]][k]; |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
imx[cur][k] = dmx[cur][k+1] + CP9TSC(cp9O_ID,k); |
| 364 |
imx[cur][k] += cp9->isc[dsq[i]][k]; |
| 365 |
|
| 366 |
dmx[cur][k] = dmx[cur][k+1] + CP9TSC(cp9O_DD,k); |
| 367 |
|
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
mmx[cur][0] = dmx[cur][1] + CP9TSC(cp9O_MD,0); |
| 372 |
|
| 373 |
imx[cur][0] = dmx[cur][1] + CP9TSC(cp9O_ID,0); |
| 374 |
imx[cur][0] += cp9->isc[dsq[i]][0]; |
| 375 |
|
| 376 |
dmx[cur][0] = -INFTY; |
| 377 |
elmx[cur][0] = -INFTY; |
| 378 |
|
| 379 |
scA[ip] = mmx[cur][0]; |
| 380 |
fsc = Scorify(scA[ip]); |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
for (i = j0-1; i >= (i0-1); i--) |
| 396 |
{ |
| 397 |
ip = i-i0+1; |
| 398 |
if(be_efficient) { cur = (j0-i) % 2; prv = (j0-i+1) % 2; } |
| 399 |
else { cur = ip; prv = ip+1; } |
| 400 |
|
| 401 |
|
| 402 |
for (k = 1; k <= cp9->M; k++) elmx[cur][k] = -INFTY; |
| 403 |
if(ip > 0) { |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
if((cp9->flags & CPLAN9_EL) && (cp9->has_el[cp9->M])) |
| 408 |
elmx[cur][cp9->M] = elmx[cur][cp9->M] + cp9->el_selfsc; |
| 409 |
|
| 410 |
mmx[cur][cp9->M] = imx[prv][cp9->M] + CP9TSC(cp9O_MI,cp9->M); |
| 411 |
mmx[cur][cp9->M] += cp9->msc[dsq[i]][cp9->M]; |
| 412 |
|
| 413 |
if((cp9->flags & CPLAN9_EL) && (cp9->has_el[cp9->M])) |
| 414 |
mmx[cur][cp9->M] = ESL_MAX(mmx[cur][cp9->M], |
| 415 |
elmx[cur][cp9->M] + CP9TSC(cp9O_MEL,cp9->M)); |
| 416 |
|
| 417 |
imx[cur][cp9->M] = imx[prv][cp9->M] + CP9TSC(cp9O_II,cp9->M); |
| 418 |
imx[cur][cp9->M] += cp9->isc[dsq[i]][cp9->M]; |
| 419 |
} |
| 420 |
else { |
| 421 |
mmx[cur][cp9->M] = -INFTY; |
| 422 |
imx[cur][cp9->M] = -INFTY; |
| 423 |
elmx[cur][cp9->M]= -INFTY; |
| 424 |
} |
| 425 |
dmx[cur][cp9->M] = imx[prv][cp9->M] + CP9TSC(cp9O_DI,cp9->M); |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
if(cp9->flags & CPLAN9_EL) |
| 431 |
{ |
| 432 |
for(c = 0; c < cp9->el_from_ct[cp9->M]; c++) |
| 433 |
elmx[cur][cp9->el_from_idx[cp9->M][c]] = ESL_MAX(elmx[cur][cp9->el_from_idx[cp9->M][c]], mmx[prv][cp9->M]); |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
if(do_scan) { |
| 441 |
if(ip > 0) { |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
if(cp9->flags & CPLAN9_EL) { |
| 446 |
for(c = 0; c < cp9->el_from_ct[cp9->M+1]; c++) |
| 447 |
elmx[cur][cp9->el_from_idx[cp9->M+1][c]] = 0.; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
mmx[cur][cp9->M] = ESL_MAX(mmx[cur][cp9->M], |
| 457 |
elmx[cur][cp9->M] + CP9TSC(cp9O_MEL,cp9->M) + cp9->msc[dsq[i]][cp9->M]); |
| 458 |
mmx[cur][cp9->M] = ESL_MAX(mmx[cur][cp9->M], |
| 459 |
CP9TSC(cp9O_ME,cp9->M) + cp9->msc[dsq[i]][cp9->M]); |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
imx[cur][cp9->M] = ESL_MAX(imx[cur][cp9->M], |
| 465 |
(CP9TSC(cp9O_IM,cp9->M) + |
| 466 |
0 + |
| 467 |
cp9->isc[dsq[i]][cp9->M])); |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
} |
| 472 |
dmx[cur][cp9->M] = ESL_MAX(dmx[cur][cp9->M], |
| 473 |
(CP9TSC(cp9O_DM,cp9->M) + |
| 474 |
0)); |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
for (k = cp9->M-1; k >= 1; k--) { |
| 481 |
if(ip > 0) { |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
if(cp9->flags & CPLAN9_EL) { |
| 488 |
for(c = 0; c < cp9->el_from_ct[k]; c++) |
| 489 |
elmx[cur][cp9->el_from_idx[k][c]] = ESL_MAX(elmx[cur][cp9->el_from_idx[k][c]], mmx[prv][k]); |
| 490 |
|
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
if((cp9->flags & CPLAN9_EL) && (cp9->has_el[k])) |
| 498 |
elmx[cur][k] = ESL_MAX(elmx[cur][k], elmx[prv][k] + cp9->el_selfsc); |
| 499 |
|
| 500 |
mmx[cur][k] = ESL_MAX(ESL_MAX((mmx[prv][k+1] + CP9TSC(cp9O_MM,k)), |
| 501 |
(imx[prv][k] + CP9TSC(cp9O_MI,k))), |
| 502 |
(dmx[cur][k+1] + CP9TSC(cp9O_MD,k))); |
| 503 |
if((cp9->flags & CPLAN9_EL) && (cp9->has_el[k])) |
| 504 |
mmx[cur][k] = ESL_MAX(mmx[cur][k], elmx[cur][k] + CP9TSC(cp9O_MEL,k)); |
| 505 |
mmx[cur][k] += cp9->msc[dsq[i]][k]; |
| 506 |
|
| 507 |
imx[cur][k] = ESL_MAX(ESL_MAX((mmx[prv][k+1] + CP9TSC(cp9O_IM,k)), |
| 508 |
(imx[prv][k] + CP9TSC(cp9O_II,k))), |
| 509 |
(dmx[cur][k+1] + CP9TSC(cp9O_ID,k))); |
| 510 |
imx[cur][k] += cp9->isc[dsq[i]][k]; |
| 511 |
|
| 512 |
} |
| 513 |
else { |
| 514 |
mmx[cur][k] = -INFTY; |
| 515 |
imx[cur][k] = -INFTY; |
| 516 |
elmx[cur][k]= -INFTY; |
| 517 |
} |
| 518 |
if(do_scan && ip > 0) { |
| 519 |
mmx[cur][k] = |
| 520 |
ESL_MAX(mmx[cur][k], |
| 521 |
(CP9TSC(cp9O_ME,k) + |
| 522 |
0 + |
| 523 |
cp9->msc[dsq[i]][k])); |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
} |
| 531 |
dmx[cur][k] = ESL_MAX(ESL_MAX((mmx[prv][k+1] + CP9TSC(cp9O_DM,k)), |
| 532 |
(imx[prv][k] + CP9TSC(cp9O_DI,k))), |
| 533 |
(dmx[cur][k+1] + CP9TSC(cp9O_DD,k))); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
if(ip > 0) { |
| 538 |
imx[cur][0] = ESL_MAX(ESL_MAX((mmx[prv][1] + CP9TSC(cp9O_IM,0)), |
| 539 |
(imx[prv][0] + CP9TSC(cp9O_II,0))), |
| 540 |
(dmx[cur][1] + CP9TSC(cp9O_ID,k))); |
| 541 |
imx[cur][0] += cp9->isc[dsq[i]][k]; |
| 542 |
} |
| 543 |
else |
| 544 |
imx[cur][0] = -INFTY; |
| 545 |
dmx[cur][0] = -INFTY; |
| 546 |
elmx[cur][0] = -INFTY; |
| 547 |
|
| 548 |
|
| 549 |
mmx[cur][0] = -INFTY; |
| 550 |
for (k = cp9->M; k >= 1; k--) mmx[cur][0] = ESL_MAX(mmx[cur][0], (mmx[prv][k] + CP9TSC(cp9O_BM,k))); |
| 551 |
mmx[cur][0] = ESL_MAX(mmx[cur][0], (imx[prv][0] + CP9TSC(cp9O_MI,0))); |
| 552 |
mmx[cur][0] = ESL_MAX(mmx[cur][0], (dmx[cur][1] + CP9TSC(cp9O_MD,0))); |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
scA[ip] = mmx[cur][0]; |
| 558 |
fsc = Scorify(scA[ip]); |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
if(fsc > best_sc) { best_sc = fsc; best_pos= i+1; } |
| 574 |
} |
| 575 |
|
| 576 |
if(doing_align) { |
| 577 |
best_sc = Scorify(scA[(j0-i0+1)]); |
| 578 |
best_pos = i0; |
| 579 |
} |
| 580 |
if(ret_sc != NULL) *ret_sc = best_sc; |
| 581 |
if(ret_maxres != NULL) *ret_maxres = best_pos; |
| 582 |
if(ret_psc != NULL) *ret_psc = scA; |
| 583 |
else free(scA); |
| 584 |
ESL_DPRINTF1(("#DEBUG: cp9_ViterbiBackward() return score: %10.4f\n", best_sc)); |
| 585 |
|
| 586 |
return eslOK; |
| 587 |
|
| 588 |
ERROR: |
| 589 |
ESL_FAIL(eslEMEM, errbuf, "Memory allocation error."); |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
int |
| 666 |
cp9_Forward(CP9_t *cp9, char *errbuf, CP9_MX *mx, ESL_DSQ *dsq, int i0, int j0, int do_scan, int doing_align, |
| 667 |
int be_efficient, int **ret_psc, int *ret_maxres, float *ret_sc) |
| 668 |
{ |
| 669 |
int status; |
| 670 |
int j; |
| 671 |
int jp; |
| 672 |
int cur, prv; |
| 673 |
int k; |
| 674 |
int L; |
| 675 |
int **mmx; |
| 676 |
int **imx; |
| 677 |
int **dmx; |
| 678 |
int **elmx; |
| 679 |
int *erow; |
| 680 |
int *scA; |
| 681 |
float fsc; |
| 682 |
float best_sc; |
| 683 |
float best_pos; |
| 684 |
int nrows = 2; |
| 685 |
int c; |
| 686 |
int M; |
| 687 |
|
| 688 |
|
| 689 |
if(cp9 == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_Forward, cp9 is NULL.\n"); |
| 690 |
if(dsq == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_Forward, dsq is NULL."); |
| 691 |
if(mx == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_Forward, mx is NULL.\n"); |
| 692 |
|
| 693 |
best_sc = IMPOSSIBLE; |
| 694 |
best_pos = -1; |
| 695 |
L = j0-i0+1; |
| 696 |
M = cp9->M; |
| 697 |
|
| 698 |
int const *tsc = cp9->otsc; |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
if(be_efficient) nrows = 1; |
| 703 |
else nrows = L; |
| 704 |
if((status = GrowCP9Matrix(mx, errbuf, nrows, M, NULL, NULL, &mmx, &imx, &dmx, &elmx, &erow)) != eslOK) return status; |
| 705 |
ESL_DPRINTF1(("#DEBUG: cp9_Forward(): CP9 matrix size: %.8f Mb rows: %d.\n", mx->size_Mb, mx->rows)); |
| 706 |
ESL_DPRINTF1(("#DEBUG: cp9_Forward do_scan: %d\n", do_scan)); |
| 707 |
|
| 708 |
|
| 709 |
ESL_ALLOC(scA, sizeof(int) * (j0-i0+2)); |
| 710 |
|
| 711 |
|
| 712 |
mmx[0][0] = 0; |
| 713 |
imx[0][0] = -INFTY; |
| 714 |
dmx[0][0] = -INFTY; |
| 715 |
elmx[0][0]= -INFTY; |
| 716 |
erow[0] = -INFTY; |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
int sc; |
| 721 |
for (k = 1; k <= M; k++) |
| 722 |
{ |
| 723 |
mmx[0][k] = imx[0][k] = elmx[0][k] = -INFTY; |
| 724 |
sc = ILogsum(ILogsum(mmx[0][k-1] + CP9TSC(cp9O_MD,k-1), |
| 725 |
imx[0][k-1] + CP9TSC(cp9O_ID,k-1)), |
| 726 |
dmx[0][k-1] + CP9TSC(cp9O_DD,k-1)); |
| 727 |
dmx[0][k] = sc; |
| 728 |
} |
| 729 |
|
| 730 |
erow[0] = dmx[0][M] + CP9TSC(cp9O_DM,M); |
| 731 |
scA[0] = erow[0]; |
| 732 |
fsc = Scorify(scA[0]); |
| 733 |
|
| 734 |
if(fsc > best_sc) |
| 735 |
{ |
| 736 |
best_sc = fsc; |
| 737 |
best_pos= i0-1; |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
for (j = i0; j <= j0; j++) |
| 748 |
{ |
| 749 |
int const *isc = cp9->isc[dsq[j]]; |
| 750 |
int const *msc = cp9->msc[dsq[j]]; |
| 751 |
int endsc = -INFTY; |
| 752 |
int el_selfsc = cp9->el_selfsc; |
| 753 |
int sc; |
| 754 |
|
| 755 |
jp = j-i0+1; |
| 756 |
cur = (j-i0+1); |
| 757 |
prv = (j-i0); |
| 758 |
|
| 759 |
if(be_efficient) |
| 760 |
{ |
| 761 |
cur %= 2; |
| 762 |
prv %= 2; |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
mmx[cur][0] = (do_scan == TRUE) ? 0 : -INFTY; |
| 770 |
dmx[cur][0] = -INFTY; |
| 771 |
elmx[cur][0] = -INFTY; |
| 772 |
|
| 773 |
sc = ILogsum(ILogsum(mmx[prv][0] + CP9TSC(cp9O_MI,0), |
| 774 |
imx[prv][0] + CP9TSC(cp9O_II,0)), |
| 775 |
dmx[prv][0] + CP9TSC(cp9O_DI,0)); |
| 776 |
imx[cur][0] = sc + isc[0]; |
| 777 |
|
| 778 |
for (k = 1; k <= M; k++) |
| 779 |
{ |
| 780 |
|
| 781 |
sc = ILogsum(ILogsum(mmx[prv][k-1] + CP9TSC(cp9O_MM,k-1), |
| 782 |
imx[prv][k-1] + CP9TSC(cp9O_IM,k-1)), |
| 783 |
ILogsum(dmx[prv][k-1] + CP9TSC(cp9O_DM,k-1), |
| 784 |
mmx[prv][0] + CP9TSC(cp9O_BM,k ))); |
| 785 |
|
| 786 |
for(c = 0; c < cp9->el_from_ct[k]; c++) |
| 787 |
sc = ILogsum(sc, elmx[prv][cp9->el_from_idx[k][c]]); |
| 788 |
|
| 789 |
mmx[cur][k] = sc + msc[k]; |
| 790 |
|
| 791 |
|
| 792 |
endsc = ILogsum(endsc, mmx[cur][k] + CP9TSC(cp9O_ME,k)); |
| 793 |
|
| 794 |
|
| 795 |
sc = ILogsum(ILogsum(mmx[prv][k] + CP9TSC(cp9O_MI,k), |
| 796 |
imx[prv][k] + CP9TSC(cp9O_II,k)), |
| 797 |
dmx[prv][k] + CP9TSC(cp9O_DI,k)); |
| 798 |
imx[cur][k] = sc + isc[k]; |
| 799 |
|
| 800 |
|
| 801 |
sc = ILogsum(ILogsum(mmx[cur][k-1] + CP9TSC(cp9O_MD,k-1), |
| 802 |
imx[cur][k-1] + CP9TSC(cp9O_ID,k-1)), |
| 803 |
dmx[cur][k-1] + CP9TSC(cp9O_DD,k-1)); |
| 804 |
dmx[cur][k] = sc; |
| 805 |
|
| 806 |
|
| 807 |
sc = -INFTY; |
| 808 |
if((cp9->flags & CPLAN9_EL) && cp9->has_el[k]) |
| 809 |
|
| 810 |
{ |
| 811 |
sc = ILogsum(mmx[cur][k] + CP9TSC(cp9O_MEL,k), |
| 812 |
elmx[prv][k] + el_selfsc); |
| 813 |
} |
| 814 |
elmx[cur][k] = sc; |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
} |
| 820 |
endsc = ILogsum(ILogsum(endsc, dmx[cur][M] + CP9TSC(cp9O_DM,M)), |
| 821 |
imx[cur][M] + CP9TSC(cp9O_IM,M)); |
| 822 |
for(c = 0; c < cp9->el_from_ct[M+1]; c++) |
| 823 |
endsc = ILogsum(endsc, elmx[cur][cp9->el_from_idx[M+1][c]]); |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
erow[cur] = endsc; |
| 828 |
scA[jp] = endsc; |
| 829 |
fsc = Scorify(endsc); |
| 830 |
|
| 831 |
if(fsc > best_sc) { best_sc = fsc; best_pos= j; } |
| 832 |
} |
| 833 |
|
| 834 |
if(doing_align) { |
| 835 |
best_sc = Scorify(scA[(j0-i0+1)]); |
| 836 |
best_pos = i0; |
| 837 |
} |
| 838 |
if(ret_sc != NULL) *ret_sc = best_sc; |
| 839 |
if(ret_maxres != NULL) *ret_maxres = best_pos; |
| 840 |
if(ret_psc != NULL) *ret_psc = scA; |
| 841 |
else free(scA); |
| 842 |
ESL_DPRINTF1(("#DEBUG: cp9_Forward() return score: %10.4f\n", best_sc)); |
| 843 |
|
| 844 |
return eslOK; |
| 845 |
|
| 846 |
ERROR: |
| 847 |
ESL_FAIL(eslEMEM, errbuf, "Memory allocation error."); |
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
int |
| 973 |
cp9_Backward(CP9_t *cp9, char *errbuf, CP9_MX *mx, ESL_DSQ *dsq, int i0, int j0, int do_scan, int doing_align, |
| 974 |
int be_efficient, int **ret_psc, int *ret_maxres, float *ret_sc) |
| 975 |
{ |
| 976 |
int status; |
| 977 |
int i; |
| 978 |
int ip; |
| 979 |
int cur, prv; |
| 980 |
int k; |
| 981 |
int L; |
| 982 |
int **mmx; |
| 983 |
int **imx; |
| 984 |
int **dmx; |
| 985 |
int **elmx; |
| 986 |
int *erow; |
| 987 |
int *scA; |
| 988 |
float fsc; |
| 989 |
float best_sc; |
| 990 |
float best_pos; |
| 991 |
int nrows; |
| 992 |
int c; |
| 993 |
int M; |
| 994 |
|
| 995 |
|
| 996 |
if(cp9 == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_Backward, cp9 is NULL.\n"); |
| 997 |
if(dsq == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_Backward, dsq is NULL."); |
| 998 |
if(mx == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_Backward, mx is NULL.\n"); |
| 999 |
|
| 1000 |
int const *tsc = cp9->otsc; |
| 1001 |
|
| 1002 |
best_sc = IMPOSSIBLE; |
| 1003 |
best_pos = -1; |
| 1004 |
L = j0-i0+1; |
| 1005 |
M = cp9->M; |
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
if(be_efficient) nrows = 1; |
| 1010 |
else nrows = L; |
| 1011 |
if((status = GrowCP9Matrix(mx, errbuf, nrows, M, NULL, NULL, &mmx, &imx, &dmx, &elmx, &erow)) != eslOK) return status; |
| 1012 |
ESL_DPRINTF1(("#DEBUG: cp9_Backward(): CP9 matrix size: %.8f Mb rows: %d.\n", mx->size_Mb, mx->rows)); |
| 1013 |
ESL_DPRINTF1(("#DEBUG: cp9_Backward do_scan: %d\n", do_scan)); |
| 1014 |
|
| 1015 |
|
| 1016 |
ESL_ALLOC(scA, sizeof(int) * (j0-i0+3)); |
| 1017 |
|
| 1018 |
|
| 1019 |
if(be_efficient) cur = 0; |
| 1020 |
else cur = j0-i0+1; |
| 1021 |
ip = j0-i0+1; |
| 1022 |
i = j0; |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
for (k = 1; k <= cp9->M; k++) |
| 1030 |
elmx[cur][k] = -INFTY; |
| 1031 |
if(cp9->flags & CPLAN9_EL) |
| 1032 |
{ |
| 1033 |
for(c = 0; c < cp9->el_from_ct[cp9->M+1]; c++) |
| 1034 |
elmx[cur][cp9->el_from_idx[cp9->M+1][c]] = 0.; |
| 1035 |
} |
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
mmx[cur][cp9->M] = 0. + |
| 1044 |
ILogsum(elmx[cur][cp9->M] + CP9TSC(cp9O_MEL,cp9->M), |
| 1045 |
CP9TSC(cp9O_ME,cp9->M)); |
| 1046 |
mmx[cur][cp9->M] += cp9->msc[dsq[i]][cp9->M]; |
| 1047 |
imx[cur][cp9->M] = 0. + CP9TSC(cp9O_IM,cp9->M); |
| 1048 |
imx[cur][cp9->M] += cp9->isc[dsq[i]][cp9->M]; |
| 1049 |
dmx[cur][cp9->M] = CP9TSC(cp9O_DM,cp9->M); |
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
for (k = cp9->M-1; k >= 1; k--) |
| 1058 |
{ |
| 1059 |
mmx[cur][k] = 0 + CP9TSC(cp9O_ME,k); |
| 1060 |
mmx[cur][k] = ILogsum(mmx[cur][k], dmx[cur][k+1] + CP9TSC(cp9O_MD,k)); |
| 1061 |
if(cp9->flags & CPLAN9_EL) |
| 1062 |
mmx[cur][k] = ILogsum(mmx[cur][k], elmx[cur][k] + CP9TSC(cp9O_MEL,k)); |
| 1063 |
mmx[cur][k] += cp9->msc[dsq[i]][k]; |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
imx[cur][k] = dmx[cur][k+1] + CP9TSC(cp9O_ID,k); |
| 1071 |
imx[cur][k] += cp9->isc[dsq[i]][k]; |
| 1072 |
|
| 1073 |
dmx[cur][k] = dmx[cur][k+1] + CP9TSC(cp9O_DD,k); |
| 1074 |
|
| 1075 |
} |
| 1076 |
|
| 1077 |
|
| 1078 |
mmx[cur][0] = dmx[cur][1] + CP9TSC(cp9O_MD,0); |
| 1079 |
|
| 1080 |
imx[cur][0] = dmx[cur][1] + CP9TSC(cp9O_ID,0); |
| 1081 |
imx[cur][0] += cp9->isc[dsq[i]][0]; |
| 1082 |
|
| 1083 |
dmx[cur][0] = -INFTY; |
| 1084 |
elmx[cur][0] = -INFTY; |
| 1085 |
|
| 1086 |
scA[ip] = mmx[cur][0]; |
| 1087 |
fsc = Scorify(scA[ip]); |
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
for (i = j0-1; i >= i0; i--) |
| 1094 |
{ |
| 1095 |
ip = i-i0+1; |
| 1096 |
if(be_efficient) { cur = (j0-i) %2; prv = (j0-i+1)%2; } |
| 1097 |
else { cur = ip; prv = ip+1; } |
| 1098 |
|
| 1099 |
|
| 1100 |
for (k = 0; k <= cp9->M; k++) elmx[cur][k] = -INFTY; |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
if((cp9->flags & CPLAN9_EL) && (cp9->has_el[cp9->M])) |
| 1106 |
elmx[cur][cp9->M] = elmx[cur][cp9->M] + cp9->el_selfsc; |
| 1107 |
|
| 1108 |
mmx[cur][cp9->M] = imx[prv][cp9->M] + CP9TSC(cp9O_MI,cp9->M); |
| 1109 |
mmx[cur][cp9->M] += cp9->msc[dsq[i]][cp9->M]; |
| 1110 |
|
| 1111 |
if((cp9->flags & CPLAN9_EL) && (cp9->has_el[cp9->M])) |
| 1112 |
mmx[cur][cp9->M] = ILogsum(mmx[cur][cp9->M], |
| 1113 |
elmx[cur][cp9->M] + CP9TSC(cp9O_MEL,cp9->M)); |
| 1114 |
|
| 1115 |
imx[cur][cp9->M] = imx[prv][cp9->M] + CP9TSC(cp9O_II,cp9->M); |
| 1116 |
imx[cur][cp9->M] += cp9->isc[dsq[i]][cp9->M]; |
| 1117 |
|
| 1118 |
dmx[cur][cp9->M] = imx[prv][cp9->M] + CP9TSC(cp9O_DI,cp9->M); |
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
if(cp9->flags & CPLAN9_EL) |
| 1124 |
{ |
| 1125 |
for(c = 0; c < cp9->el_from_ct[cp9->M]; c++) |
| 1126 |
elmx[cur][cp9->el_from_idx[cp9->M][c]] = ILogsum(elmx[cur][cp9->el_from_idx[cp9->M][c]], mmx[prv][cp9->M]); |
| 1127 |
} |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
if(do_scan) { |
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
if(cp9->flags & CPLAN9_EL) |
| 1138 |
{ |
| 1139 |
for(c = 0; c < cp9->el_from_ct[cp9->M+1]; c++) |
| 1140 |
elmx[cur][cp9->el_from_idx[cp9->M+1][c]] = 0.; |
| 1141 |
} |
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
mmx[cur][cp9->M] = |
| 1149 |
ILogsum(mmx[cur][cp9->M], |
| 1150 |
ILogsum(elmx[cur][cp9->M] + CP9TSC(cp9O_MEL,cp9->M), |
| 1151 |
CP9TSC(cp9O_ME,cp9->M))); |
| 1152 |
|
| 1153 |
|
| 1154 |
imx[cur][cp9->M] = |
| 1155 |
ILogsum(imx[cur][cp9->M], |
| 1156 |
(CP9TSC(cp9O_IM,cp9->M) + |
| 1157 |
0)); |
| 1158 |
|
| 1159 |
dmx[cur][cp9->M] = |
| 1160 |
ILogsum(dmx[cur][cp9->M], |
| 1161 |
(CP9TSC(cp9O_DM,cp9->M) + |
| 1162 |
0)); |
| 1163 |
} |
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
for (k = cp9->M-1; k >= 1; k--) |
| 1169 |
{ |
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
if(cp9->flags & CPLAN9_EL) { |
| 1176 |
for(c = 0; c < cp9->el_from_ct[k]; c++) |
| 1177 |
elmx[cur][cp9->el_from_idx[k][c]] = ILogsum(elmx[cur][cp9->el_from_idx[k][c]], mmx[prv][k]); |
| 1178 |
|
| 1179 |
} |
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
if((cp9->flags & CPLAN9_EL) && (cp9->has_el[k])) |
| 1186 |
elmx[cur][k] = ILogsum(elmx[cur][k], elmx[prv][k] + cp9->el_selfsc); |
| 1187 |
|
| 1188 |
mmx[cur][k] = ILogsum(ILogsum((mmx[prv][k+1] + CP9TSC(cp9O_MM,k)), |
| 1189 |
(imx[prv][k] + CP9TSC(cp9O_MI,k))), |
| 1190 |
(dmx[cur][k+1] + CP9TSC(cp9O_MD,k))); |
| 1191 |
if((cp9->flags & CPLAN9_EL) && (cp9->has_el[k])) |
| 1192 |
mmx[cur][k] = ILogsum(mmx[cur][k], elmx[cur][k] + CP9TSC(cp9O_MEL,k)); |
| 1193 |
mmx[cur][k] += cp9->msc[dsq[i]][k]; |
| 1194 |
|
| 1195 |
imx[cur][k] = ILogsum(ILogsum((mmx[prv][k+1] + CP9TSC(cp9O_IM,k)), |
| 1196 |
(imx[prv][k] + CP9TSC(cp9O_II,k))), |
| 1197 |
(dmx[cur][k+1] + CP9TSC(cp9O_ID,k))); |
| 1198 |
imx[cur][k] += cp9->isc[dsq[i]][k]; |
| 1199 |
|
| 1200 |
if(do_scan) { |
| 1201 |
mmx[cur][k] = |
| 1202 |
ILogsum(mmx[cur][k], |
| 1203 |
(CP9TSC(cp9O_ME,k) + |
| 1204 |
0)); |
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
} |
| 1209 |
dmx[cur][k] = ILogsum(ILogsum((mmx[prv][k+1] + CP9TSC(cp9O_DM,k)), |
| 1210 |
(imx[prv][k] + CP9TSC(cp9O_DI,k))), |
| 1211 |
(dmx[cur][k+1] + CP9TSC(cp9O_DD,k))); |
| 1212 |
} |
| 1213 |
|
| 1214 |
|
| 1215 |
imx[cur][0] = ILogsum(ILogsum((mmx[prv][1] + CP9TSC(cp9O_IM,0)), |
| 1216 |
(imx[prv][0] + CP9TSC(cp9O_II,0))), |
| 1217 |
(dmx[cur][1] + CP9TSC(cp9O_ID,k))); |
| 1218 |
imx[cur][0] += cp9->isc[dsq[i]][k]; |
| 1219 |
dmx[cur][0] = -INFTY; |
| 1220 |
elmx[cur][0] = -INFTY; |
| 1221 |
|
| 1222 |
|
| 1223 |
mmx[cur][0] = -INFTY; |
| 1224 |
for (k = cp9->M; k >= 1; k--) |
| 1225 |
mmx[cur][0] = ILogsum(mmx[cur][0], (mmx[prv][k] + CP9TSC(cp9O_BM,k))); |
| 1226 |
mmx[cur][0] = ILogsum(mmx[cur][0], (imx[prv][0] + CP9TSC(cp9O_MI,0))); |
| 1227 |
mmx[cur][0] = ILogsum(mmx[cur][0], (dmx[cur][1] + CP9TSC(cp9O_MD,0))); |
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
scA[ip] = mmx[cur][0]; |
| 1233 |
fsc = Scorify(scA[ip]); |
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
if(fsc > best_sc) { best_sc = fsc; best_pos= i+1; } |
| 1249 |
} |
| 1250 |
|
| 1251 |
|
| 1252 |
ip = i-i0+1; |
| 1253 |
if(be_efficient) { |
| 1254 |
cur = (j0-i) %2; |
| 1255 |
prv = (j0-i+1)%2; |
| 1256 |
} |
| 1257 |
else { |
| 1258 |
cur = ip; |
| 1259 |
prv = ip+1; |
| 1260 |
} |
| 1261 |
|
| 1262 |
|
| 1263 |
for (k = 1; k <= cp9->M; k++) elmx[cur][k] = -INFTY; |
| 1264 |
|
| 1265 |
mmx[cur][cp9->M] = -INFTY; |
| 1266 |
imx[cur][cp9->M] = -INFTY; |
| 1267 |
elmx[cur][cp9->M]= -INFTY; |
| 1268 |
dmx[cur][cp9->M] = imx[prv][cp9->M] + CP9TSC(cp9O_DI,cp9->M); |
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
if(do_scan) |
| 1274 |
{ |
| 1275 |
dmx[cur][cp9->M] = |
| 1276 |
ILogsum(dmx[cur][cp9->M], |
| 1277 |
(CP9TSC(cp9O_DM,cp9->M) + |
| 1278 |
0)); |
| 1279 |
} |
| 1280 |
for (k = cp9->M-1; k >= 1; k--) |
| 1281 |
{ |
| 1282 |
mmx[cur][k] = -INFTY; |
| 1283 |
imx[cur][k] = -INFTY; |
| 1284 |
elmx[cur][k]= -INFTY; |
| 1285 |
dmx[cur][k] = ILogsum(ILogsum((mmx[prv][k+1] + CP9TSC(cp9O_DM,k)), |
| 1286 |
(imx[prv][k] + CP9TSC(cp9O_DI,k))), |
| 1287 |
(dmx[cur][k+1] + CP9TSC(cp9O_DD,k))); |
| 1288 |
} |
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
imx[cur][0] = -INFTY; |
| 1293 |
dmx[cur][0] = -INFTY; |
| 1294 |
elmx[cur][0] = -INFTY; |
| 1295 |
|
| 1296 |
|
| 1297 |
mmx[cur][0] = -INFTY; |
| 1298 |
for (k = cp9->M; k >= 1; k--) |
| 1299 |
mmx[cur][0] = ILogsum(mmx[cur][0], (mmx[prv][k] + CP9TSC(cp9O_BM,k))); |
| 1300 |
mmx[cur][0] = ILogsum(mmx[cur][0], (imx[prv][0] + CP9TSC(cp9O_MI,0))); |
| 1301 |
mmx[cur][0] = ILogsum(mmx[cur][0], (dmx[cur][1] + CP9TSC(cp9O_MD,0))); |
| 1302 |
|
| 1303 |
|
| 1304 |
scA[ip] = mmx[cur][0]; |
| 1305 |
fsc = Scorify(scA[ip]); |
| 1306 |
|
| 1307 |
|
| 1308 |
if(fsc > best_sc) { best_sc = fsc; best_pos= i+1; } |
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
if(doing_align) { |
| 1315 |
best_sc = Scorify(scA[0]); |
| 1316 |
best_pos = i0; |
| 1317 |
} |
| 1318 |
if(ret_sc != NULL) *ret_sc = best_sc; |
| 1319 |
if(ret_maxres != NULL) *ret_maxres = best_pos; |
| 1320 |
if(ret_psc != NULL) *ret_psc = scA; |
| 1321 |
else free(scA); |
| 1322 |
ESL_DPRINTF1(("#DEBUG: cp9_Backward() return score: %10.4f\n", best_sc)); |
| 1323 |
|
| 1324 |
return eslOK; |
| 1325 |
|
| 1326 |
ERROR: |
| 1327 |
ESL_FAIL(eslEMEM, errbuf, "Memory allocation error."); |
| 1328 |
} |
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
int |
| 1354 |
cp9_CheckFB(CP9_MX *fmx, CP9_MX *bmx, CP9_t *hmm, char *errbuf, float sc, int i0, int j0, ESL_DSQ *dsq) |
| 1355 |
{ |
| 1356 |
if(fmx == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_CheckFB(), fmx is NULL.\n"); |
| 1357 |
if(bmx == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_CheckFB(), bmx is NULL.\n"); |
| 1358 |
if(dsq == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_CheckFB(), dsq is NULL."); |
| 1359 |
|
| 1360 |
int k, i; |
| 1361 |
float max_diff; |
| 1362 |
|
| 1363 |
float diff; |
| 1364 |
int fb_sum; |
| 1365 |
float fb_sc; |
| 1366 |
int W; |
| 1367 |
int ip; |
| 1368 |
int to_add; |
| 1369 |
|
| 1370 |
W = j0-i0+1; |
| 1371 |
max_diff = 0.1; |
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
for (ip = 1; ip <= W; ip++) { |
| 1376 |
i = i0+ip-1; |
| 1377 |
fb_sum = -INFTY; |
| 1378 |
for (k = 0; k <= hmm->M; k++) { |
| 1379 |
if (fmx->mmx[ip][k] == -INFTY) to_add = -INFTY; |
| 1380 |
else if(bmx->mmx[ip][k] == -INFTY) to_add = -INFTY; |
| 1381 |
else { |
| 1382 |
to_add = fmx->mmx[ip][k] + bmx->mmx[ip][k]; |
| 1383 |
if(k > 0) to_add -= hmm->msc[dsq[i]][k]; |
| 1384 |
} |
| 1385 |
|
| 1386 |
|
| 1387 |
fb_sum = ILogsum(fb_sum, to_add); |
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
if (fmx->imx[ip][k] == -INFTY) to_add = -INFTY; |
| 1393 |
else if(bmx->imx[ip][k] == -INFTY) to_add = -INFTY; |
| 1394 |
else { |
| 1395 |
to_add = fmx->imx[ip][k] + bmx->imx[ip][k]; |
| 1396 |
to_add -= hmm->isc[dsq[i]][k]; |
| 1397 |
} |
| 1398 |
|
| 1399 |
fb_sum = ILogsum(fb_sum, to_add); |
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
if (fmx->elmx[ip][k] == -INFTY) to_add = -INFTY; |
| 1405 |
else if(bmx->elmx[ip][k] == -INFTY) to_add = -INFTY; |
| 1406 |
else { |
| 1407 |
to_add = fmx->elmx[ip][k] + bmx->elmx[ip][k]; |
| 1408 |
|
| 1409 |
} |
| 1410 |
fb_sum = ILogsum(fb_sum, to_add); |
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
} |
| 1416 |
fb_sc = Scorify(fb_sum); |
| 1417 |
diff = fabs(fb_sc - sc); |
| 1418 |
if((fabs(diff) > max_diff)) |
| 1419 |
ESL_FAIL(eslFAIL, errbuf, "cp9_CheckFB(), residue at posn i:%d violates sum_k f[i][k]*b[i][k]=P(x|hmm), sum_k = %.4f bits (should be %.4f)\n", i, fb_sc, sc); |
| 1420 |
} |
| 1421 |
ESL_DPRINTF1(("#DEBUG: cp9_CheckFB() passed, Forward/Backward matrices pass check.\n")); |
| 1422 |
return eslOK; |
| 1423 |
} |
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
#ifdef IMPL_CP9_DP_BENCHMARK |
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
#include <esl_config.h> |
| 1436 |
#include "config.h" |
| 1437 |
|
| 1438 |
#include <stdio.h> |
| 1439 |
#include <stdlib.h> |
| 1440 |
#include <string.h> |
| 1441 |
#include <time.h> |
| 1442 |
|
| 1443 |
#include "easel.h" |
| 1444 |
#include "esl_getopts.h" |
| 1445 |
#include "esl_histogram.h" |
| 1446 |
#include "esl_random.h" |
| 1447 |
#include "esl_randomseq.h" |
| 1448 |
#include "esl_sqio.h" |
| 1449 |
#include "esl_stats.h" |
| 1450 |
#include "esl_stopwatch.h" |
| 1451 |
#include "esl_vectorops.h" |
| 1452 |
#include "esl_wuss.h" |
| 1453 |
|
| 1454 |
#include "infernal.h" |
| 1455 |
|
| 1456 |
#include "old_funcs.h" |
| 1457 |
|
| 1458 |
|
| 1459 |
static ESL_OPTIONS options[] = { |
| 1460 |
|
| 1461 |
{ "-h", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 1462 |
{ "-r", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "set random number seed randomly", 0 }, |
| 1463 |
{ "-s", eslARG_INT, "33", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 1464 |
{ "-L", eslARG_INT, "500000", NULL, "n>0", NULL, NULL, NULL, "length of random target seqs", 0 }, |
| 1465 |
{ "-N", eslARG_INT, "1", NULL, "n>0", NULL, NULL, NULL, "number of random target seqs", 0 }, |
| 1466 |
{ "-f", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "also execute optimized Forward scan implementation", 0 }, |
| 1467 |
{ "-o", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "also execute old (version 0.8) Forward scan implementation", 0}, |
| 1468 |
{ "-w", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "also execute slow Viterbi scan implementation", 0 }, |
| 1469 |
{ "-z", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "also execute slow Viterbi backward scan implementation", 0 }, |
| 1470 |
{ "-g", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "configure HMM for glocal alignment [default: local]", 0 }, |
| 1471 |
{ "-a", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "do alignment, don't scan", 0 }, |
| 1472 |
{ "--noel", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "turn local ends off [default: on, unless -g]", 0 }, |
| 1473 |
{ "--full", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "calculate full matrix, not just 2 rows", 0 }, |
| 1474 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 1475 |
}; |
| 1476 |
static char usage[] = "[-options] <cmfile>"; |
| 1477 |
static char banner[] = "benchmark driver for the fast scanning CM plan 9 HMM Viterbi implementation"; |
| 1478 |
|
| 1479 |
int |
| 1480 |
main(int argc, char **argv) |
| 1481 |
{ |
| 1482 |
int status; |
| 1483 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 1, argc, argv, banner, usage); |
| 1484 |
CM_t *cm; |
| 1485 |
ESL_STOPWATCH *w = esl_stopwatch_Create(); |
| 1486 |
ESL_RANDOMNESS *r = NULL; |
| 1487 |
ESL_ALPHABET *abc = NULL; |
| 1488 |
int L = esl_opt_GetInteger(go, "-L"); |
| 1489 |
int N = esl_opt_GetInteger(go, "-N"); |
| 1490 |
ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2)); |
| 1491 |
int i; |
| 1492 |
float sc; |
| 1493 |
char *cmfile = esl_opt_GetArg(go, 1); |
| 1494 |
CMFILE *cmfp; |
| 1495 |
int do_scan; |
| 1496 |
int do_align; |
| 1497 |
int minL = 0; |
| 1498 |
int be_safe; |
| 1499 |
char errbuf[eslERRBUFSIZE]; |
| 1500 |
|
| 1501 |
if (esl_opt_GetBoolean(go, "-r")) r = esl_randomness_CreateTimeseeded(); |
| 1502 |
else r = esl_randomness_Create(esl_opt_GetInteger(go, "-s")); |
| 1503 |
|
| 1504 |
if ((cmfp = CMFileOpen(cmfile, NULL)) == NULL) cm_Fail("Failed to open covariance model save file %s\n", cmfile); |
| 1505 |
if (!(CMFileRead(cmfp, &abc, &cm))) cm_Fail("Failed to read CM"); |
| 1506 |
CMFileClose(cmfp); |
| 1507 |
|
| 1508 |
if(! esl_opt_GetBoolean(go, "-g")) { |
| 1509 |
cm->config_opts |= CM_CONFIG_LOCAL; |
| 1510 |
cm->config_opts |= CM_CONFIG_HMMLOCAL; |
| 1511 |
} |
| 1512 |
if(! esl_opt_GetBoolean(go, "--noel")) { |
| 1513 |
cm->config_opts |= CM_CONFIG_LOCAL; |
| 1514 |
cm->config_opts |= CM_CONFIG_HMMEL; |
| 1515 |
} |
| 1516 |
ConfigCM(cm, TRUE); |
| 1517 |
init_ilogsum(); |
| 1518 |
|
| 1519 |
if (esl_opt_GetBoolean(go, "-a")) { do_scan = FALSE; do_align = TRUE; } |
| 1520 |
else { do_scan = TRUE; do_align = FALSE; } |
| 1521 |
|
| 1522 |
for (i = 0; i < N; i++) |
| 1523 |
{ |
| 1524 |
esl_rsq_xfIID(r, cm->null, abc->K, L, dsq); |
| 1525 |
|
| 1526 |
esl_stopwatch_Start(w); |
| 1527 |
if((status = cp9_Viterbi(cm->cp9, errbuf, cm->cp9_mx, dsq, 1, L, |
| 1528 |
do_scan, |
| 1529 |
do_align, |
| 1530 |
(! esl_opt_GetBoolean(go, "--full")), |
| 1531 |
NULL, |
| 1532 |
NULL, |
| 1533 |
NULL, |
| 1534 |
&sc)) != eslOK) cm_Fail(errbuf); |
| 1535 |
printf("%4d %-30s %10.4f bits ", (i+1), "cp9_Viterbi(): ", sc); |
| 1536 |
esl_stopwatch_Stop(w); |
| 1537 |
esl_stopwatch_Display(stdout, w, " CPU time: "); |
| 1538 |
|
| 1539 |
if (esl_opt_GetBoolean(go, "-b")) |
| 1540 |
{ |
| 1541 |
esl_stopwatch_Start(w); |
| 1542 |
if((status = cp9_ViterbiBackward(cm->cp9, errbuf, cm->cp9_mx, dsq, 1, L, |
| 1543 |
do_scan, |
| 1544 |
do_align, |
| 1545 |
(! esl_opt_GetBoolean(go, "--full")), |
| 1546 |
NULL, |
| 1547 |
NULL, |
| 1548 |
NULL, |
| 1549 |
&sc)) != eslOK) cm_Fail(errbuf); |
| 1550 |
printf("%4d %-30s %10.4f bits ", (i+1), "cp9_ViterbiBackward(): ", sc); |
| 1551 |
esl_stopwatch_Stop(w); |
| 1552 |
esl_stopwatch_Display(stdout, w, " CPU time: "); |
| 1553 |
} |
| 1554 |
|
| 1555 |
if (esl_opt_GetBoolean(go, "-f")) |
| 1556 |
{ |
| 1557 |
esl_stopwatch_Start(w); |
| 1558 |
if((status = cp9_Forward(cm->cp9, errbuf, cm->cp9_mx, dsq, 1, L, |
| 1559 |
do_scan, |
| 1560 |
do_align, |
| 1561 |
(! esl_opt_GetBoolean(go, "--full")), |
| 1562 |
NULL, |
| 1563 |
NULL, |
| 1564 |
&sc)) != eslOK) cm_Fail(errbuf); |
| 1565 |
printf("%4d %-30s %10.4f bits ", (i+1), "cp9_Forward(): ", sc); |
| 1566 |
esl_stopwatch_Stop(w); |
| 1567 |
esl_stopwatch_Display(stdout, w, " CPU time: "); |
| 1568 |
} |
| 1569 |
} |
| 1570 |
FreeCM(cm); |
| 1571 |
free(dsq); |
| 1572 |
esl_alphabet_Destroy(abc); |
| 1573 |
esl_stopwatch_Destroy(w); |
| 1574 |
esl_randomness_Destroy(r); |
| 1575 |
esl_getopts_Destroy(go); |
| 1576 |
return 0; |
| 1577 |
} |
| 1578 |
#endif |
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
#ifdef DEBUG_CP9_DP |
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
#include <esl_config.h> |
| 1590 |
#include <p7_config.h> |
| 1591 |
#include "config.h" |
| 1592 |
|
| 1593 |
#include <stdio.h> |
| 1594 |
#include <stdlib.h> |
| 1595 |
#include <string.h> |
| 1596 |
#include <time.h> |
| 1597 |
|
| 1598 |
#include "easel.h" |
| 1599 |
#include "esl_getopts.h" |
| 1600 |
#include "esl_histogram.h" |
| 1601 |
#include "esl_random.h" |
| 1602 |
#include "esl_randomseq.h" |
| 1603 |
#include "esl_sqio.h" |
| 1604 |
#include "esl_stats.h" |
| 1605 |
#include "esl_stopwatch.h" |
| 1606 |
#include "esl_vectorops.h" |
| 1607 |
#include "esl_wuss.h" |
| 1608 |
|
| 1609 |
#include "infernal.h" |
| 1610 |
|
| 1611 |
static ESL_OPTIONS options[] = { |
| 1612 |
|
| 1613 |
{ "-h", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 1614 |
{ "-r", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "set random number seed randomly", 0 }, |
| 1615 |
{ "-s", eslARG_INT, "33", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 1616 |
{ "-L", eslARG_INT, "500000", NULL, "n>0", NULL, NULL, NULL, "length of random target seqs", 0 }, |
| 1617 |
{ "-N", eslARG_INT, "1", NULL, "n>0", NULL, NULL, NULL, "number of random target seqs", 0 }, |
| 1618 |
{ "-f", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "also execute optimized Forward scan implementation", 0 }, |
| 1619 |
{ "-g", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "configure HMM for glocal alignment [default: local]", 0 }, |
| 1620 |
{ "-a", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "do alignment, don't scan", 0 }, |
| 1621 |
{ "--noel", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "turn local ends off [default: on, unless -g]", 0 }, |
| 1622 |
{ "--full", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "calculate full matrix, not just 2 rows", 0 }, |
| 1623 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 1624 |
}; |
| 1625 |
static char usage[] = "[-options] <cmfile>"; |
| 1626 |
static char banner[] = "benchmark driver for the fast scanning CM plan 9 HMM Viterbi implementation"; |
| 1627 |
|
| 1628 |
int |
| 1629 |
main(int argc, char **argv) |
| 1630 |
{ |
| 1631 |
int status; |
| 1632 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 2, argc, argv, banner, usage); |
| 1633 |
CM_t *cm; |
| 1634 |
ESL_STOPWATCH *w = esl_stopwatch_Create(); |
| 1635 |
ESL_ALPHABET *abc = NULL; |
| 1636 |
ESL_SQ *sq = NULL; |
| 1637 |
float sc; |
| 1638 |
char *cmfile = esl_opt_GetArg(go, 1); |
| 1639 |
char *sqfile = esl_opt_GetArg(go, 2); |
| 1640 |
CMFILE *cmfp; |
| 1641 |
int do_scan; |
| 1642 |
int do_align; |
| 1643 |
char errbuf[eslERRBUFSIZE]; |
| 1644 |
ESL_SQFILE *sqfp; |
| 1645 |
int maxi, maxj, maxi2; |
| 1646 |
|
| 1647 |
|
| 1648 |
status = esl_sqfile_Open(sqfile, eslSQFILE_UNKNOWN, NULL, &sqfp); |
| 1649 |
if (status == eslENOTFOUND) ESL_FAIL(status, errbuf, "File %s doesn't exist or is not readable\n", sqfile); |
| 1650 |
else if (status == eslEFORMAT) ESL_FAIL(status, errbuf, "Couldn't determine format of sequence file %s\n", sqfile); |
| 1651 |
else if (status == eslEINVAL) ESL_FAIL(status, errbuf, "Can’t autodetect stdin or .gz."); |
| 1652 |
else if (status != eslOK) ESL_FAIL(status, errbuf, "Sequence file open failed with error %d\n", status); |
| 1653 |
|
| 1654 |
if ((cmfp = CMFileOpen(cmfile, NULL)) == NULL) cm_Fail("Failed to open covariance model save file %s\n", cmfile); |
| 1655 |
if ((status = (CMFileRead(cmfp, NULL, &abc, &cm))) != eslOK) cm_Fail("Failed to read CM"); |
| 1656 |
CMFileClose(cmfp); |
| 1657 |
esl_sqfile_SetDigital(sqfp, abc); |
| 1658 |
|
| 1659 |
if(! esl_opt_GetBoolean(go, "-g")) { |
| 1660 |
cm->config_opts |= CM_CONFIG_LOCAL; |
| 1661 |
cm->config_opts |= CM_CONFIG_HMMLOCAL; |
| 1662 |
if(! esl_opt_GetBoolean(go, "--noel")) { |
| 1663 |
cm->config_opts |= CM_CONFIG_HMMEL; |
| 1664 |
} |
| 1665 |
} |
| 1666 |
ConfigCM(cm, NULL, TRUE, NULL, NULL); |
| 1667 |
init_ilogsum(); |
| 1668 |
|
| 1669 |
if (esl_opt_GetBoolean(go, "-a")) { do_scan = FALSE; do_align = TRUE; } |
| 1670 |
else { do_scan = TRUE; do_align = FALSE; } |
| 1671 |
|
| 1672 |
sq = esl_sq_CreateDigital(abc); |
| 1673 |
while((status = esl_sqio_Read(sqfp, sq)) == eslOK) { |
| 1674 |
esl_stopwatch_Start(w); |
| 1675 |
if((status = cp9_Viterbi(cm->cp9, errbuf, cm->cp9_mx, sq->dsq, 1, sq->n, cm->W, cm->W, 0., NULL, |
| 1676 |
do_scan, |
| 1677 |
do_align, |
| 1678 |
(! esl_opt_GetBoolean(go, "--full")), |
| 1679 |
FALSE, |
| 1680 |
NULL, |
| 1681 |
&maxj, |
| 1682 |
NULL, |
| 1683 |
&sc)) != eslOK) cm_Fail(errbuf); |
| 1684 |
printf("%-30s %10.4f bits endpoint: %4d", "cp9_Viterbi(): ", sc, maxj); |
| 1685 |
esl_stopwatch_Stop(w); |
| 1686 |
esl_stopwatch_Display(stdout, w, " CPU time: "); |
| 1687 |
|
| 1688 |
esl_stopwatch_Start(w); |
| 1689 |
if((status = cp9_ViterbiBackward(cm->cp9, errbuf, cm->cp9_mx, sq->dsq, 1, |
| 1690 |
(do_align) ? sq->n : maxj, |
| 1691 |
cm->W, cm->W, 0., NULL, |
| 1692 |
do_scan, |
| 1693 |
do_align, |
| 1694 |
(! esl_opt_GetBoolean(go, "--full")), |
| 1695 |
FALSE, |
| 1696 |
NULL, |
| 1697 |
&maxi, |
| 1698 |
NULL, |
| 1699 |
&sc)) != eslOK) cm_Fail(errbuf); |
| 1700 |
printf("%-30s %10.4f bits startpnt: %4d", "cp9_ViterbiBackward(): ", sc, maxi); |
| 1701 |
esl_stopwatch_Stop(w); |
| 1702 |
esl_stopwatch_Display(stdout, w, " CPU time: "); |
| 1703 |
|
| 1704 |
esl_sq_Reuse(sq); |
| 1705 |
|
| 1706 |
printf("\n"); |
| 1707 |
} |
| 1708 |
FreeCM(cm); |
| 1709 |
esl_sq_Destroy(sq); |
| 1710 |
esl_alphabet_Destroy(abc); |
| 1711 |
esl_stopwatch_Destroy(w); |
| 1712 |
esl_getopts_Destroy(go); |
| 1713 |
return 0; |
| 1714 |
} |
| 1715 |
#endif |