| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include <p7_config.h> |
| 10 |
|
| 11 |
#include "hmmer.h" |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
P7_GMX * |
| 31 |
p7_gmx_Create(int allocM, int allocL) |
| 32 |
{ |
| 33 |
int status; |
| 34 |
P7_GMX *gx = NULL; |
| 35 |
int i; |
| 36 |
|
| 37 |
|
| 38 |
if ( (uint64_t) (allocM+1) * (uint64_t) (allocL+1) * sizeof(float) * p7G_NSCELLS > SIZE_MAX / 2) |
| 39 |
return NULL; |
| 40 |
|
| 41 |
|
| 42 |
ESL_ALLOC(gx, sizeof(P7_GMX)); |
| 43 |
gx->dp = NULL; |
| 44 |
gx->xmx = NULL; |
| 45 |
gx->dp_mem = NULL; |
| 46 |
|
| 47 |
|
| 48 |
ESL_ALLOC(gx->dp, sizeof(float *) * (allocL+1)); |
| 49 |
ESL_ALLOC(gx->xmx, sizeof(float) * (allocL+1) * p7G_NXCELLS); |
| 50 |
ESL_ALLOC(gx->dp_mem, sizeof(float) * (allocL+1) * (allocM+1) * p7G_NSCELLS); |
| 51 |
|
| 52 |
|
| 53 |
for (i = 0; i <= allocL; i++) |
| 54 |
gx->dp[i] = gx->dp_mem + (ptrdiff_t) i * (ptrdiff_t) (allocM+1) * (ptrdiff_t) p7G_NSCELLS; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
for (i = 0; i <= allocL; i++) |
| 60 |
{ |
| 61 |
gx->dp[i][0 * p7G_NSCELLS + p7G_M] = -eslINFINITY; |
| 62 |
gx->dp[i][0 * p7G_NSCELLS + p7G_I] = -eslINFINITY; |
| 63 |
gx->dp[i][0 * p7G_NSCELLS + p7G_D] = -eslINFINITY; |
| 64 |
gx->dp[i][1 * p7G_NSCELLS + p7G_D] = -eslINFINITY; |
| 65 |
gx->dp[i][allocM * p7G_NSCELLS + p7G_I] = -eslINFINITY; |
| 66 |
} |
| 67 |
|
| 68 |
gx->M = 0; |
| 69 |
gx->L = 0; |
| 70 |
gx->allocW = allocM+1; |
| 71 |
gx->allocR = allocL+1; |
| 72 |
gx->validR = allocL+1; |
| 73 |
gx->ncells = (uint64_t) (allocM+1)* (uint64_t) (allocL+1); |
| 74 |
return gx; |
| 75 |
|
| 76 |
ERROR: |
| 77 |
if (gx != NULL) p7_gmx_Destroy(gx); |
| 78 |
return NULL; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
int |
| 100 |
p7_gmx_GrowTo(P7_GMX *gx, int M, int L) |
| 101 |
{ |
| 102 |
int status; |
| 103 |
void *p; |
| 104 |
int i; |
| 105 |
uint64_t ncells; |
| 106 |
int do_reset = FALSE; |
| 107 |
|
| 108 |
if (M < gx->allocW && L < gx->validR) return eslOK; |
| 109 |
|
| 110 |
|
| 111 |
if ( (uint64_t) (M+1) * (uint64_t) (L+1) * sizeof(float) * p7G_NSCELLS > SIZE_MAX / 2) return eslEMEM; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
ncells = (uint64_t) (M+1) * (uint64_t) (L+1); |
| 118 |
if (ncells > gx->ncells) |
| 119 |
{ |
| 120 |
ESL_RALLOC(gx->dp_mem, p, sizeof(float) * ncells * p7G_NSCELLS); |
| 121 |
gx->ncells = ncells; |
| 122 |
do_reset = TRUE; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
if (L >= gx->allocR) |
| 127 |
{ |
| 128 |
ESL_RALLOC(gx->xmx, p, sizeof(float) * (L+1) * p7G_NXCELLS); |
| 129 |
ESL_RALLOC(gx->dp, p, sizeof(float *) * (L+1)); |
| 130 |
gx->allocR = L+1; |
| 131 |
do_reset = TRUE; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
if (M >= gx->allocW) do_reset = TRUE; |
| 136 |
|
| 137 |
|
| 138 |
if (L >= gx->validR) do_reset = TRUE; |
| 139 |
|
| 140 |
|
| 141 |
if (do_reset) |
| 142 |
{ |
| 143 |
gx->allocW = M+1; |
| 144 |
gx->validR = ESL_MIN(gx->ncells / gx->allocW, gx->allocR); |
| 145 |
for (i = 0; i < gx->validR; i++) |
| 146 |
gx->dp[i] = gx->dp_mem + (ptrdiff_t) i * (ptrdiff_t) (gx->allocW) * (ptrdiff_t) p7G_NSCELLS; |
| 147 |
} |
| 148 |
|
| 149 |
gx->M = 0; |
| 150 |
gx->L = 0; |
| 151 |
return eslOK; |
| 152 |
|
| 153 |
ERROR: |
| 154 |
return status; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
size_t |
| 161 |
p7_gmx_Sizeof(P7_GMX *gx) |
| 162 |
{ |
| 163 |
size_t n = 0; |
| 164 |
|
| 165 |
n += sizeof(P7_GMX); |
| 166 |
n += gx->ncells * p7G_NSCELLS * sizeof(float); |
| 167 |
n += gx->allocR * sizeof(float *); |
| 168 |
n += gx->allocR * p7G_NXCELLS * sizeof(float); |
| 169 |
return n; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
int |
| 182 |
p7_gmx_Reuse(P7_GMX *gx) |
| 183 |
{ |
| 184 |
|
| 185 |
gx->M = 0; |
| 186 |
gx->L = 0; |
| 187 |
return eslOK; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
void |
| 199 |
p7_gmx_Destroy(P7_GMX *gx) |
| 200 |
{ |
| 201 |
if (gx == NULL) return; |
| 202 |
|
| 203 |
if (gx->dp != NULL) free(gx->dp); |
| 204 |
if (gx->xmx != NULL) free(gx->xmx); |
| 205 |
if (gx->dp_mem != NULL) free(gx->dp_mem); |
| 206 |
free(gx); |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
int |
| 224 |
p7_gmx_Compare(P7_GMX *gx1, P7_GMX *gx2, float tolerance) |
| 225 |
{ |
| 226 |
int i,k,x; |
| 227 |
if (gx1->M != gx2->M) return eslFAIL; |
| 228 |
if (gx1->L != gx2->L) return eslFAIL; |
| 229 |
|
| 230 |
for (i = 0; i <= gx1->L; i++) |
| 231 |
{ |
| 232 |
for (k = 1; k <= gx1->M; k++) |
| 233 |
{ |
| 234 |
if (esl_FCompare_old(gx1->dp[i][k * p7G_NSCELLS + p7G_M], gx2->dp[i][k * p7G_NSCELLS + p7G_M], tolerance) != eslOK) return eslFAIL; |
| 235 |
if (esl_FCompare_old(gx1->dp[i][k * p7G_NSCELLS + p7G_I], gx2->dp[i][k * p7G_NSCELLS + p7G_I], tolerance) != eslOK) return eslFAIL; |
| 236 |
if (esl_FCompare_old(gx1->dp[i][k * p7G_NSCELLS + p7G_D], gx2->dp[i][k * p7G_NSCELLS + p7G_D], tolerance) != eslOK) return eslFAIL; |
| 237 |
} |
| 238 |
for (x = 0; x < p7G_NXCELLS; x++) |
| 239 |
if (esl_FCompare_old(gx1->xmx[i * p7G_NXCELLS + x], gx2->xmx[i * p7G_NXCELLS + x], tolerance) != eslOK) return eslFAIL; |
| 240 |
} |
| 241 |
return eslOK; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
int |
| 256 |
p7_gmx_Dump(FILE *ofp, P7_GMX *gx, int flags) |
| 257 |
{ |
| 258 |
return p7_gmx_DumpWindow(ofp, gx, 0, gx->L, 0, gx->M, flags); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
int |
| 278 |
p7_gmx_DumpWindow(FILE *ofp, P7_GMX *gx, int istart, int iend, int kstart, int kend, int flags) |
| 279 |
{ |
| 280 |
int width = 9; |
| 281 |
int precision = 4; |
| 282 |
int i, k, x; |
| 283 |
float val; |
| 284 |
|
| 285 |
|
| 286 |
fprintf(ofp, " "); |
| 287 |
for (k = kstart; k <= kend; k++) fprintf(ofp, "%*d ", width, k); |
| 288 |
if (! (flags & p7_HIDE_SPECIALS)) fprintf(ofp, "%*s %*s %*s %*s %*s\n", width, "E", width, "N", width, "J", width, "B", width, "C"); |
| 289 |
fprintf(ofp, " "); |
| 290 |
for (k = kstart; k <= kend; k++) fprintf(ofp, "%*.*s ", width, width, "----------"); |
| 291 |
if (! (flags & p7_HIDE_SPECIALS)) |
| 292 |
for (x = 0; x < 5; x++) fprintf(ofp, "%*.*s ", width, width, "----------"); |
| 293 |
fprintf(ofp, "\n"); |
| 294 |
|
| 295 |
|
| 296 |
for (i = istart; i <= iend; i++) |
| 297 |
{ |
| 298 |
fprintf(ofp, "%3d M ", i); |
| 299 |
for (k = kstart; k <= kend; k++) |
| 300 |
{ |
| 301 |
val = gx->dp[i][k * p7G_NSCELLS + p7G_M]; |
| 302 |
if (flags & p7_SHOW_LOG) val = log(val); |
| 303 |
fprintf(ofp, "%*.*f ", width, precision, val); |
| 304 |
} |
| 305 |
if (! (flags & p7_HIDE_SPECIALS)) |
| 306 |
{ |
| 307 |
for (x = 0; x < p7G_NXCELLS; x++) |
| 308 |
{ |
| 309 |
val = gx->xmx[ i * p7G_NXCELLS + x]; |
| 310 |
if (flags & p7_SHOW_LOG) val = log(val); |
| 311 |
fprintf(ofp, "%*.*f ", width, precision, val); |
| 312 |
} |
| 313 |
} |
| 314 |
fprintf(ofp, "\n"); |
| 315 |
|
| 316 |
fprintf(ofp, "%3d I ", i); |
| 317 |
for (k = kstart; k <= kend; k++) |
| 318 |
{ |
| 319 |
val = gx->dp[i][k * p7G_NSCELLS + p7G_I]; |
| 320 |
if (flags & p7_SHOW_LOG) val = log(val); |
| 321 |
fprintf(ofp, "%*.*f ", width, precision, val); |
| 322 |
} |
| 323 |
fprintf(ofp, "\n"); |
| 324 |
|
| 325 |
fprintf(ofp, "%3d D ", i); |
| 326 |
for (k = kstart; k <= kend; k++) |
| 327 |
{ |
| 328 |
val = gx->dp[i][k * p7G_NSCELLS + p7G_D]; |
| 329 |
if (flags & p7_SHOW_LOG) val = log(val); |
| 330 |
fprintf(ofp, "%*.*f ", width, precision, val); |
| 331 |
} |
| 332 |
fprintf(ofp, "\n\n"); |
| 333 |
} |
| 334 |
return eslOK; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
#ifdef p7GMX_TESTDRIVE |
| 342 |
#include "esl_random.h" |
| 343 |
#include "esl_randomseq.h" |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
static void |
| 357 |
gmx_testpattern(P7_GMX *gx, int M, int L) |
| 358 |
{ |
| 359 |
int64_t i; |
| 360 |
int64_t n,n2; |
| 361 |
int k,s; |
| 362 |
int maxL = 16777216 / (3 * (M+1)); |
| 363 |
int64_t start_row = (L > maxL ? L-maxL+1 : 0); |
| 364 |
|
| 365 |
|
| 366 |
n = 0; |
| 367 |
for (i = start_row; i <= L; i++) |
| 368 |
for (k = 0; k <= M; k++) |
| 369 |
for (s = 0; s < p7G_NSCELLS; s++) |
| 370 |
gx->dp[i][k*p7G_NSCELLS+s] = (float) n++; |
| 371 |
|
| 372 |
|
| 373 |
n = 0; |
| 374 |
for (i = start_row; i <= L; i++) |
| 375 |
for (k = 0; k <= M; k++) |
| 376 |
for (s = 0; s < p7G_NSCELLS; s++) |
| 377 |
{ |
| 378 |
if ((int) gx->dp[i][k*p7G_NSCELLS+s] != n) esl_fatal("gmx unit test failed: test pattern corrupted"); |
| 379 |
n++; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
if (gx->allocR == gx->validR && gx->ncells == (int64_t) gx->validR * (int64_t) gx->allocW) |
| 384 |
{ |
| 385 |
n2 = 0; |
| 386 |
for (i = start_row * gx->allocW; i < gx->ncells; i++) |
| 387 |
for (s = 0; s < p7G_NSCELLS; s++) |
| 388 |
{ |
| 389 |
if (gx->dp_mem[i*p7G_NSCELLS+s] != n2) esl_fatal("gmx unit test failed: test pattern corrupted (2nd test)"); |
| 390 |
n2++; |
| 391 |
} |
| 392 |
|
| 393 |
if (n != n2) esl_fatal("gmx unit test failed: unexpected # of cells"); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
static void |
| 399 |
utest_GrowTo(void) |
| 400 |
{ |
| 401 |
P7_GMX *gx = NULL; |
| 402 |
int M, L; |
| 403 |
#if !defined(eslENABLE_ASAN) && !defined(eslENABLE_TSAN) |
| 404 |
int64_t nbytes; |
| 405 |
#endif |
| 406 |
|
| 407 |
M = 20; L = 20; gx= p7_gmx_Create(M, L); gmx_testpattern(gx, M, L); |
| 408 |
M = 40; L = 20; p7_gmx_GrowTo(gx, M, L); gmx_testpattern(gx, M, L); |
| 409 |
M = 40; L = 40; p7_gmx_GrowTo(gx, M, L); gmx_testpattern(gx, M, L); |
| 410 |
M = 80; L = 10; p7_gmx_GrowTo(gx, M, L); gmx_testpattern(gx, M, L); |
| 411 |
M = 10; L = 80; p7_gmx_GrowTo(gx, M, L); gmx_testpattern(gx, M, L); |
| 412 |
M = 100; L = 100; p7_gmx_GrowTo(gx, M, L); gmx_testpattern(gx, M, L); |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
M = 179; L = 55; p7_gmx_GrowTo(gx, M, L); gmx_testpattern(gx, M, L); |
| 418 |
M = 87; L = 57; p7_gmx_GrowTo(gx, M, L); gmx_testpattern(gx, M, L); |
| 419 |
|
| 420 |
|
| 421 |
#if !defined(eslENABLE_ASAN) && !defined(eslENABLE_TSAN) |
| 422 |
M = 71582; L = 10000; |
| 423 |
nbytes = (int64_t) (M+1) * (int64_t) (L+1) * (int64_t) p7G_NSCELLS * (int64_t) sizeof(float); |
| 424 |
if ( nbytes < SIZE_MAX / 2) |
| 425 |
{ |
| 426 |
void *p = malloc(nbytes); |
| 427 |
if (p != NULL) |
| 428 |
{ |
| 429 |
free(p); |
| 430 |
p7_gmx_GrowTo(gx, M, L); gmx_testpattern(gx, M, L); |
| 431 |
} |
| 432 |
} |
| 433 |
#endif |
| 434 |
|
| 435 |
p7_gmx_Destroy(gx); |
| 436 |
} |
| 437 |
|
| 438 |
static void |
| 439 |
utest_Compare(ESL_RANDOMNESS *r, P7_PROFILE *gm, P7_BG *bg, int L, float tolerance) |
| 440 |
{ |
| 441 |
char *msg = "gmx_Compare unit test failure"; |
| 442 |
ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) *(L+2)); |
| 443 |
P7_GMX *gx1 = p7_gmx_Create(gm->M, L); |
| 444 |
P7_GMX *gx2 = p7_gmx_Create(5, 4); |
| 445 |
float fsc; |
| 446 |
|
| 447 |
if (!r || !gm || !dsq || !gx1 || !gx2 ) esl_fatal(msg); |
| 448 |
if (esl_rsq_xfIID(r, bg->f, gm->abc->K, L, dsq) != eslOK) esl_fatal(msg); |
| 449 |
if (p7_gmx_GrowTo(gx2, gm->M, L) != eslOK) esl_fatal(msg); |
| 450 |
if (p7_GForward(dsq, L, gm, gx1, &fsc) != eslOK) esl_fatal(msg); |
| 451 |
if (p7_GForward(dsq, L, gm, gx2, &fsc) != eslOK) esl_fatal(msg); |
| 452 |
if (p7_gmx_Compare(gx1, gx2, tolerance) != eslOK) esl_fatal(msg); |
| 453 |
|
| 454 |
p7_gmx_Destroy(gx1); |
| 455 |
p7_gmx_Destroy(gx2); |
| 456 |
free(dsq); |
| 457 |
} |
| 458 |
|
| 459 |
#endif |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
#ifdef p7GMX_TESTDRIVE |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
#include <p7_config.h> |
| 472 |
|
| 473 |
#include <stdio.h> |
| 474 |
#include <math.h> |
| 475 |
|
| 476 |
#include "easel.h" |
| 477 |
#include "esl_getopts.h" |
| 478 |
#include "esl_random.h" |
| 479 |
#include "esl_randomseq.h" |
| 480 |
|
| 481 |
#include "hmmer.h" |
| 482 |
|
| 483 |
static ESL_OPTIONS options[] = { |
| 484 |
|
| 485 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0}, |
| 486 |
{ "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 487 |
{ "-t", eslARG_REAL, "1e-5", NULL, NULL, NULL, NULL, NULL, "floating point comparison tolerance", 0 }, |
| 488 |
{ "-L", eslARG_INT, "40", NULL, NULL, NULL, NULL, NULL, "length of sampled sequences", 0 }, |
| 489 |
{ "-M", eslARG_INT, "40", NULL, NULL, NULL, NULL, NULL, "length of sampled test profile", 0 }, |
| 490 |
{ 0,0,0,0,0,0,0,0,0,0}, |
| 491 |
}; |
| 492 |
static char usage[] = "[-options]"; |
| 493 |
static char banner[] = "test driver for p7_gmx.c"; |
| 494 |
|
| 495 |
int |
| 496 |
main(int argc, char **argv) |
| 497 |
{ |
| 498 |
char *msg = "p7_gmx unit test driver failed"; |
| 499 |
ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 500 |
ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s")); |
| 501 |
ESL_ALPHABET *abc = esl_alphabet_Create(eslAMINO); |
| 502 |
P7_BG *bg = p7_bg_Create(abc); |
| 503 |
P7_HMM *hmm = NULL; |
| 504 |
P7_PROFILE *gm = NULL; |
| 505 |
int M = esl_opt_GetInteger(go, "-M"); |
| 506 |
int L = esl_opt_GetInteger(go, "-L"); |
| 507 |
float tol = esl_opt_GetReal (go, "-t"); |
| 508 |
|
| 509 |
p7_FLogsumInit(); |
| 510 |
|
| 511 |
if (p7_hmm_Sample(r, M, abc, &hmm) != eslOK) esl_fatal(msg); |
| 512 |
if ((gm = p7_profile_Create(hmm->M, abc)) == NULL) esl_fatal(msg); |
| 513 |
if (p7_bg_SetLength(bg, L) != eslOK) esl_fatal(msg); |
| 514 |
if (p7_ProfileConfig(hmm, bg, gm, L, p7_UNILOCAL) != eslOK) esl_fatal(msg); |
| 515 |
|
| 516 |
utest_GrowTo(); |
| 517 |
utest_Compare(r, gm, bg, L, tol); |
| 518 |
|
| 519 |
esl_getopts_Destroy(go); |
| 520 |
esl_randomness_Destroy(r); |
| 521 |
esl_alphabet_Destroy(abc); |
| 522 |
p7_bg_Destroy(bg); |
| 523 |
p7_hmm_Destroy(hmm); |
| 524 |
p7_profile_Destroy(gm); |
| 525 |
return eslOK; |
| 526 |
} |
| 527 |
#endif |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|