| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#include <p7_config.h> |
| 14 |
|
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
#include "easel.h" |
| 18 |
#include "esl_alphabet.h" |
| 19 |
#include "esl_hmm.h" |
| 20 |
#include "esl_vectorops.h" |
| 21 |
|
| 22 |
#include "hmmer.h" |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
P7_BG * |
| 53 |
p7_bg_Create(const ESL_ALPHABET *abc) |
| 54 |
{ |
| 55 |
P7_BG *bg = NULL; |
| 56 |
int status; |
| 57 |
|
| 58 |
ESL_ALLOC(bg, sizeof(P7_BG)); |
| 59 |
bg->f = NULL; |
| 60 |
bg->fhmm = NULL; |
| 61 |
|
| 62 |
ESL_ALLOC(bg->f, sizeof(float) * abc->K); |
| 63 |
if ((bg->fhmm = esl_hmm_Create(abc, 2)) == NULL) goto ERROR; |
| 64 |
|
| 65 |
if (abc->type == eslAMINO) |
| 66 |
{ |
| 67 |
if (p7_AminoFrequencies(bg->f) != eslOK) goto ERROR; |
| 68 |
} |
| 69 |
else |
| 70 |
esl_vec_FSet(bg->f, abc->K, 1. / (float) abc->K); |
| 71 |
|
| 72 |
bg->p1 = 350./351.; |
| 73 |
bg->omega = 1./256.; |
| 74 |
bg->abc = abc; |
| 75 |
return bg; |
| 76 |
|
| 77 |
ERROR: |
| 78 |
p7_bg_Destroy(bg); |
| 79 |
return NULL; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
P7_BG * |
| 90 |
p7_bg_CreateUniform(const ESL_ALPHABET *abc) |
| 91 |
{ |
| 92 |
P7_BG *bg = NULL; |
| 93 |
int status; |
| 94 |
|
| 95 |
ESL_ALLOC(bg, sizeof(P7_BG)); |
| 96 |
bg->f = NULL; |
| 97 |
bg->fhmm = NULL; |
| 98 |
|
| 99 |
ESL_ALLOC(bg->f, sizeof(float) * abc->K); |
| 100 |
if ((bg->fhmm = esl_hmm_Create(abc, 2)) == NULL) goto ERROR; |
| 101 |
|
| 102 |
esl_vec_FSet(bg->f, abc->K, 1. / (float) abc->K); |
| 103 |
bg->p1 = 350./351.; |
| 104 |
bg->omega = 1./256.; |
| 105 |
bg->abc = (ESL_ALPHABET *) abc; |
| 106 |
return bg; |
| 107 |
|
| 108 |
ERROR: |
| 109 |
p7_bg_Destroy(bg); |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
P7_BG * |
| 124 |
p7_bg_Clone(const P7_BG *bg) |
| 125 |
{ |
| 126 |
P7_BG *dup = NULL; |
| 127 |
int status; |
| 128 |
|
| 129 |
ESL_ALLOC(dup, sizeof(P7_BG)); |
| 130 |
dup->f = NULL; |
| 131 |
dup->fhmm = NULL; |
| 132 |
dup->abc = bg->abc; |
| 133 |
|
| 134 |
ESL_ALLOC(dup->f, sizeof(float) * bg->abc->K); |
| 135 |
memcpy(dup->f, bg->f, sizeof(float) * bg->abc->K); |
| 136 |
if ((dup->fhmm = esl_hmm_Clone(bg->fhmm)) == NULL) goto ERROR; |
| 137 |
|
| 138 |
dup->p1 = bg->p1; |
| 139 |
dup->omega = bg->omega; |
| 140 |
return dup; |
| 141 |
|
| 142 |
ERROR: |
| 143 |
p7_bg_Destroy(dup); |
| 144 |
return NULL; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
int |
| 154 |
p7_bg_Dump(FILE *ofp, const P7_BG *bg) |
| 155 |
{ |
| 156 |
esl_vec_FDump(ofp, bg->f, bg->abc->K, bg->abc->sym); |
| 157 |
return eslOK; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
void |
| 171 |
p7_bg_Destroy(P7_BG *bg) |
| 172 |
{ |
| 173 |
if (bg != NULL) { |
| 174 |
if (bg->f != NULL) free(bg->f); |
| 175 |
if (bg->fhmm != NULL) esl_hmm_Destroy(bg->fhmm); |
| 176 |
free(bg); |
| 177 |
} |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
int |
| 189 |
p7_bg_SetLength(P7_BG *bg, int L) |
| 190 |
{ |
| 191 |
bg->p1 = (float) L / (float) (L+1); |
| 192 |
|
| 193 |
bg->fhmm->t[0][0] = bg->p1; |
| 194 |
bg->fhmm->t[0][1] = 1.0f - bg->p1; |
| 195 |
|
| 196 |
return eslOK; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
int |
| 237 |
p7_bg_Read(char *bgfile, P7_BG *bg, char *errbuf) |
| 238 |
{ |
| 239 |
ESL_FILEPARSER *efp = NULL; |
| 240 |
float *fq = NULL; |
| 241 |
int n = 0; |
| 242 |
char *tok; |
| 243 |
int toklen; |
| 244 |
int alphatype; |
| 245 |
ESL_DSQ x; |
| 246 |
int status; |
| 247 |
|
| 248 |
if (errbuf) errbuf[0] = '\0'; |
| 249 |
|
| 250 |
status = esl_fileparser_Open(bgfile, NULL, &efp); |
| 251 |
if (status == eslENOTFOUND) ESL_XFAIL(eslENOTFOUND, errbuf, "couldn't open bg file %s for reading", bgfile); |
| 252 |
else if (status != eslOK) goto ERROR; |
| 253 |
|
| 254 |
esl_fileparser_SetCommentChar(efp, '#'); |
| 255 |
|
| 256 |
|
| 257 |
status = esl_fileparser_GetToken(efp, &tok, &toklen); |
| 258 |
if (status == eslEOF) ESL_XFAIL(eslEFORMAT, errbuf, "premature end of file [line %d of bgfile %s]", efp->linenumber, bgfile); |
| 259 |
else if (status != eslOK) goto ERROR; |
| 260 |
|
| 261 |
alphatype = esl_abc_EncodeType(tok); |
| 262 |
if (alphatype == eslUNKNOWN) ESL_XFAIL(eslEFORMAT, errbuf, "expected alphabet type but saw \"%s\" [line %d of bgfile %s]", tok, efp->linenumber, bgfile); |
| 263 |
else if (alphatype != bg->abc->type) ESL_XFAIL(eslEFORMAT, errbuf, "bg file's alphabet is %s; expected %s [line %d, %s]", tok, esl_abc_DecodeType(bg->abc->type), efp->linenumber, bgfile); |
| 264 |
|
| 265 |
ESL_ALLOC(fq, sizeof(float) * bg->abc->K); |
| 266 |
esl_vec_FSet(fq, bg->abc->K, -1.0); |
| 267 |
|
| 268 |
while ((status = esl_fileparser_NextLine(efp)) == eslOK) |
| 269 |
{ |
| 270 |
status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen); |
| 271 |
if (status == eslEOL) ESL_XFAIL(eslEFORMAT, errbuf, "premature end of file [line %d of bgfile %s", efp->linenumber, bgfile); |
| 272 |
else if (status != eslOK) goto ERROR; |
| 273 |
|
| 274 |
if (toklen != 1 || ! esl_abc_CIsCanonical(bg->abc, *tok)) |
| 275 |
ESL_XFAIL(eslEFORMAT, errbuf, "expected to parse a residue letter; saw %s [line %d of bgfile %s]", tok, efp->linenumber, bgfile); |
| 276 |
|
| 277 |
x = esl_abc_DigitizeSymbol(bg->abc, *tok); |
| 278 |
if (fq[x] != -1.0) ESL_XFAIL(eslEFORMAT, errbuf, "already parsed probability of %c [line %d of bgfile %s]", bg->abc->sym[x], efp->linenumber, bgfile); |
| 279 |
n++; |
| 280 |
|
| 281 |
status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen); |
| 282 |
if (status == eslEOL) ESL_XFAIL(eslEFORMAT, errbuf, "premature end of file, expected a probability [line %d of bgfile %s]", efp->linenumber, bgfile); |
| 283 |
else if (status != eslOK) goto ERROR; |
| 284 |
if (! esl_str_IsReal(tok)) ESL_XFAIL(eslEFORMAT, errbuf, "expected a probability, saw %s [line %d of bgfile %s]", tok, efp->linenumber, bgfile); |
| 285 |
|
| 286 |
fq[x] = atof(tok); |
| 287 |
|
| 288 |
status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen); |
| 289 |
if (status == eslOK) ESL_XFAIL(eslEFORMAT, errbuf, "extra unexpected data found [line %d of bgfile %s]", efp->linenumber, bgfile); |
| 290 |
else if (status != eslEOL) goto ERROR; |
| 291 |
} |
| 292 |
if (status != eslEOF) goto ERROR; |
| 293 |
|
| 294 |
if ( n != bg->abc->K) |
| 295 |
ESL_XFAIL(eslEFORMAT, errbuf, "expected %d residue frequencies, but found %d in bgfile %s", bg->abc->K, n, bgfile); |
| 296 |
if ( esl_FCompare_old(esl_vec_FSum(fq, bg->abc->K), 1.0, 0.001) != eslOK) |
| 297 |
ESL_XFAIL(eslEFORMAT, errbuf, "residue frequencies do not sum to 1.0 in bgfile %s", bgfile); |
| 298 |
|
| 299 |
|
| 300 |
esl_vec_FNorm(fq, bg->abc->K); |
| 301 |
esl_vec_FCopy(fq, bg->abc->K, bg->f); |
| 302 |
|
| 303 |
free(fq); |
| 304 |
esl_fileparser_Close(efp); |
| 305 |
return eslOK; |
| 306 |
|
| 307 |
ERROR: |
| 308 |
if (fq) free(fq); |
| 309 |
if (efp) esl_fileparser_Close(efp); |
| 310 |
return status; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
int |
| 328 |
p7_bg_Write(FILE *fp, P7_BG *bg) |
| 329 |
{ |
| 330 |
int x; |
| 331 |
if (fprintf(fp, "%s\n", esl_abc_DecodeType(bg->abc->type)) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "bg model write failed"); |
| 332 |
for (x = 0; x < bg->abc->K; x++) |
| 333 |
{ if (fprintf(fp, "%c %.5f\n", bg->abc->sym[x], bg->f[x]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "bg model write failed"); } |
| 334 |
return eslOK; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
int |
| 357 |
p7_bg_NullOne(const P7_BG *bg, const ESL_DSQ *dsq, int L, float *ret_sc) |
| 358 |
{ |
| 359 |
*ret_sc = (float) L * log(bg->p1) + log(1.-bg->p1); |
| 360 |
return eslOK; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
int |
| 429 |
p7_bg_SetFilter(P7_BG *bg, int M, const float *compo) |
| 430 |
{ |
| 431 |
float L0 = 400.0; |
| 432 |
float L1 = (float) M / 8.0; |
| 433 |
|
| 434 |
|
| 435 |
bg->fhmm->t[0][0] = L0 / (L0+1.0f); |
| 436 |
bg->fhmm->t[0][1] = 1.0f / (L0+1.0f); |
| 437 |
bg->fhmm->t[0][2] = 1.0f; |
| 438 |
esl_vec_FCopy(bg->f, bg->abc->K, bg->fhmm->e[0]); |
| 439 |
|
| 440 |
|
| 441 |
bg->fhmm->t[1][0] = 1.0f / (L1+1.0f); |
| 442 |
bg->fhmm->t[1][1] = L1 / (L1+1.0f); |
| 443 |
bg->fhmm->t[1][2] = 1.0f; |
| 444 |
esl_vec_FCopy(compo, bg->abc->K, bg->fhmm->e[1]); |
| 445 |
|
| 446 |
bg->fhmm->pi[0] = 0.999; |
| 447 |
bg->fhmm->pi[1] = 0.001; |
| 448 |
|
| 449 |
esl_hmm_Configure(bg->fhmm, bg->f); |
| 450 |
return eslOK; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
int |
| 471 |
p7_bg_FilterScore(P7_BG *bg, const ESL_DSQ *dsq, int L, float *ret_sc) |
| 472 |
{ |
| 473 |
ESL_HMX *hmx = esl_hmx_Create(L, bg->fhmm->M); |
| 474 |
float nullsc; |
| 475 |
|
| 476 |
esl_hmm_Forward(dsq, L, bg->fhmm, hmx, &nullsc); |
| 477 |
|
| 478 |
|
| 479 |
*ret_sc = nullsc + (float) L * logf(bg->p1) + logf(1.-bg->p1); |
| 480 |
esl_hmx_Destroy(hmx); |
| 481 |
return eslOK; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
#ifdef p7BG_BENCHMARK |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
#include <p7_config.h> |
| 496 |
|
| 497 |
#include "easel.h" |
| 498 |
#include "esl_alphabet.h" |
| 499 |
#include "esl_getopts.h" |
| 500 |
#include "esl_sq.h" |
| 501 |
#include "esl_stopwatch.h" |
| 502 |
|
| 503 |
#include "hmmer.h" |
| 504 |
|
| 505 |
static ESL_OPTIONS options[] = { |
| 506 |
|
| 507 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 508 |
{ "-L", eslARG_INT, "400", NULL, "n>0", NULL, NULL, NULL, "length of random target seqs", 0 }, |
| 509 |
{ "-N", eslARG_INT, "100", NULL, "n>0", NULL, NULL, NULL, "number of random target seqs", 0 }, |
| 510 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 511 |
}; |
| 512 |
static char usage[] = "[-options] <hmmfile>"; |
| 513 |
static char banner[] = "benchmark timing for calculating null model scores"; |
| 514 |
|
| 515 |
int |
| 516 |
main(int argc, char **argv) |
| 517 |
{ |
| 518 |
ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage); |
| 519 |
ESL_STOPWATCH *w = esl_stopwatch_Create(); |
| 520 |
char *hmmfile = esl_opt_GetArg(go, 1); |
| 521 |
ESL_ALPHABET *abc = NULL; |
| 522 |
P7_HMMFILE *hfp = NULL; |
| 523 |
P7_HMM *hmm = NULL; |
| 524 |
P7_BG *bg = NULL; |
| 525 |
int L = esl_opt_GetInteger(go, "-L"); |
| 526 |
int N = esl_opt_GetInteger(go, "-N"); |
| 527 |
int i; |
| 528 |
|
| 529 |
|
| 530 |
if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile); |
| 531 |
if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM"); |
| 532 |
p7_hmmfile_Close(hfp); |
| 533 |
|
| 534 |
bg = p7_bg_Create(abc); |
| 535 |
|
| 536 |
esl_stopwatch_Start(w); |
| 537 |
for (i = 0; i < N; i++) |
| 538 |
p7_bg_SetFilterByHMM(bg, hmm); |
| 539 |
esl_stopwatch_Stop(w); |
| 540 |
esl_stopwatch_Display(stdout, w, "# CPU time: "); |
| 541 |
|
| 542 |
p7_bg_Destroy(bg); |
| 543 |
p7_hmm_Destroy(hmm); |
| 544 |
esl_alphabet_Destroy(abc); |
| 545 |
esl_stopwatch_Destroy(w); |
| 546 |
esl_getopts_Destroy(go); |
| 547 |
return 0; |
| 548 |
} |
| 549 |
#endif |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
#ifdef p7BG_TESTDRIVE |
| 556 |
#include "esl_dirichlet.h" |
| 557 |
#include "esl_random.h" |
| 558 |
|
| 559 |
static void |
| 560 |
utest_ReadWrite(ESL_RANDOMNESS *rng) |
| 561 |
{ |
| 562 |
char msg[] = "bg Read/Write unit test failed"; |
| 563 |
char tmpfile[32] = "esltmpXXXXXX"; |
| 564 |
FILE *fp = NULL; |
| 565 |
ESL_ALPHABET *abc = NULL; |
| 566 |
float *fq = NULL; |
| 567 |
P7_BG *bg = NULL; |
| 568 |
|
| 569 |
if ((abc = esl_alphabet_Create(esl_rnd_Roll(rng, 5) + 1)) == NULL) esl_fatal(msg); |
| 570 |
if (( bg = p7_bg_Create(abc)) == NULL) esl_fatal(msg); |
| 571 |
if (( fq = malloc(sizeof(float) * abc->K)) == NULL) esl_fatal(msg); |
| 572 |
do { |
| 573 |
if (esl_dirichlet_FSampleUniform(rng, abc->K, fq) != eslOK) esl_fatal(msg); |
| 574 |
} while (esl_vec_FMin(fq, abc->K) < 0.001); |
| 575 |
esl_vec_FCopy(fq, abc->K, bg->f); |
| 576 |
|
| 577 |
if (esl_tmpfile_named(tmpfile, &fp) != eslOK) esl_fatal(msg); |
| 578 |
if ( p7_bg_Write(fp, bg) != eslOK) esl_fatal(msg); |
| 579 |
fclose(fp); |
| 580 |
|
| 581 |
esl_vec_FSet(bg->f, bg->abc->K, 0.0); |
| 582 |
if ( p7_bg_Read(tmpfile, bg, NULL) != eslOK) esl_fatal(msg); |
| 583 |
if ( esl_vec_FCompare(fq, bg->f, bg->abc->K, 0.01) != eslOK) esl_fatal(msg); |
| 584 |
|
| 585 |
p7_bg_Destroy(bg); |
| 586 |
esl_alphabet_Destroy(abc); |
| 587 |
free(fq); |
| 588 |
remove(tmpfile); |
| 589 |
} |
| 590 |
#endif |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
#ifdef p7BG_TESTDRIVE |
| 598 |
#include <esl_config.h> |
| 599 |
|
| 600 |
#include <stdio.h> |
| 601 |
|
| 602 |
#include "easel.h" |
| 603 |
#include "esl_getopts.h" |
| 604 |
#include "esl_random.h" |
| 605 |
|
| 606 |
#include "hmmer.h" |
| 607 |
|
| 608 |
static ESL_OPTIONS options[] = { |
| 609 |
|
| 610 |
{"-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0}, |
| 611 |
{"-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0}, |
| 612 |
{"-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show verbose commentary/output", 0}, |
| 613 |
{ 0,0,0,0,0,0,0,0,0,0}, |
| 614 |
}; |
| 615 |
static char usage[] = "[-options]"; |
| 616 |
static char banner[] = "test driver for p7_bg"; |
| 617 |
|
| 618 |
int |
| 619 |
main(int argc, char **argv) |
| 620 |
{ |
| 621 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 622 |
ESL_RANDOMNESS *rng = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s")); |
| 623 |
int be_verbose = esl_opt_GetBoolean(go, "-v"); |
| 624 |
|
| 625 |
if (be_verbose) printf("p7_bg unit test: rng seed %" PRIu32 "\n", esl_randomness_GetSeed(rng)); |
| 626 |
|
| 627 |
utest_ReadWrite(rng); |
| 628 |
|
| 629 |
esl_randomness_Destroy(rng); |
| 630 |
esl_getopts_Destroy(go); |
| 631 |
return 0; |
| 632 |
} |
| 633 |
#endif |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
#ifdef p7BG_EXAMPLE |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
#include <p7_config.h> |
| 645 |
|
| 646 |
#include "easel.h" |
| 647 |
#include "esl_alphabet.h" |
| 648 |
#include "esl_getopts.h" |
| 649 |
#include "esl_sq.h" |
| 650 |
#include "esl_sqio.h" |
| 651 |
|
| 652 |
#include "hmmer.h" |
| 653 |
|
| 654 |
static ESL_OPTIONS options[] = { |
| 655 |
|
| 656 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 657 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 658 |
}; |
| 659 |
static char usage[] = "[-options] <hmmfile> <seqfile>"; |
| 660 |
static char banner[] = "example of calculating null model scores"; |
| 661 |
|
| 662 |
int |
| 663 |
main(int argc, char **argv) |
| 664 |
{ |
| 665 |
ESL_GETOPTS *go = p7_CreateDefaultApp(options, 2, argc, argv, banner, usage); |
| 666 |
char *hmmfile = esl_opt_GetArg(go, 1); |
| 667 |
char *seqfile = esl_opt_GetArg(go, 2); |
| 668 |
ESL_ALPHABET *abc = NULL; |
| 669 |
P7_HMMFILE *hfp = NULL; |
| 670 |
P7_HMM *hmm = NULL; |
| 671 |
P7_BG *bg = NULL; |
| 672 |
ESL_SQFILE *sqfp = NULL; |
| 673 |
int format = eslSQFILE_UNKNOWN; |
| 674 |
ESL_SQ *sq = NULL; |
| 675 |
float nullsc, filtersc, H; |
| 676 |
int status; |
| 677 |
|
| 678 |
|
| 679 |
if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile); |
| 680 |
if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM"); |
| 681 |
p7_hmmfile_Close(hfp); |
| 682 |
|
| 683 |
|
| 684 |
status = esl_sqfile_OpenDigital(abc, seqfile, format, NULL, &sqfp); |
| 685 |
if (status == eslENOTFOUND) esl_fatal("No such file."); |
| 686 |
else if (status == eslEFORMAT) esl_fatal("Format unrecognized."); |
| 687 |
else if (status != eslOK) esl_fatal("Open failed, code %d.", status); |
| 688 |
|
| 689 |
sq = esl_sq_CreateDigital(abc); |
| 690 |
bg = p7_bg_Create(abc); |
| 691 |
|
| 692 |
p7_bg_SetFilter(bg, hmm->M, hmm->compo); |
| 693 |
|
| 694 |
H = esl_vec_FEntropy(bg->f, bg->abc->K); |
| 695 |
printf("bg iid H = %.4f\n", H); |
| 696 |
|
| 697 |
H = esl_vec_FEntropy(hmm->compo, bg->abc->K); |
| 698 |
printf("modelcomp H = %.4f\n", H); |
| 699 |
|
| 700 |
while ((status = esl_sqio_Read(sqfp, sq)) == eslOK) |
| 701 |
{ |
| 702 |
p7_bg_SetLength(bg, sq->n); |
| 703 |
|
| 704 |
p7_bg_NullOne (bg, sq->dsq, sq->n, &nullsc); |
| 705 |
p7_bg_FilterScore(bg, sq->dsq, sq->n, &filtersc); |
| 706 |
|
| 707 |
printf("%-20s %5d %8.5f %8.5f %8.5f\n", sq->name, (int) sq->n, nullsc, filtersc, filtersc-nullsc); |
| 708 |
|
| 709 |
esl_sq_Reuse(sq); |
| 710 |
} |
| 711 |
if (status == eslEFORMAT) esl_fatal("Parse failed (sequence file %s)\n%s\n", |
| 712 |
sqfp->filename, sqfp->get_error(sqfp)); |
| 713 |
else if (status != eslEOF) esl_fatal("Unexpected error %d reading sequence file %s", |
| 714 |
status, sqfp->filename); |
| 715 |
|
| 716 |
esl_sqfile_Close(sqfp); |
| 717 |
esl_sq_Destroy(sq); |
| 718 |
p7_bg_Destroy(bg); |
| 719 |
p7_hmm_Destroy(hmm); |
| 720 |
esl_alphabet_Destroy(abc); |
| 721 |
esl_getopts_Destroy(go); |
| 722 |
return 0; |
| 723 |
} |
| 724 |
#endif |
| 725 |
|
| 726 |
#ifdef p7BG_EXAMPLE2 |
| 727 |
#include <stdio.h> |
| 728 |
#include "easel.h" |
| 729 |
#include "esl_alphabet.h" |
| 730 |
#include "hmmer.h" |
| 731 |
|
| 732 |
int |
| 733 |
main(int argc, char **argv) |
| 734 |
{ |
| 735 |
char *bgfile = argv[1]; |
| 736 |
char *alphabet = argv[2]; |
| 737 |
ESL_ALPHABET *abc = esl_alphabet_Create(esl_abc_EncodeType(alphabet)); |
| 738 |
P7_BG *bg = p7_bg_Create(abc); |
| 739 |
char errbuf[eslERRBUFSIZE]; |
| 740 |
int status; |
| 741 |
|
| 742 |
status = p7_bg_Read(bgfile, bg, errbuf); |
| 743 |
if (status == eslENOTFOUND) esl_fatal("open failed: %s", errbuf); |
| 744 |
else if (status == eslEFORMAT) esl_fatal("parse failed: %s", errbuf); |
| 745 |
else if (status != eslOK) esl_fatal("failed to read bg file %s (error %d)\n", bgfile, status); |
| 746 |
|
| 747 |
p7_bg_Write(stdout, bg); |
| 748 |
return 0; |
| 749 |
} |
| 750 |
#endif |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|