| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include <p7_config.h> |
| 25 |
|
| 26 |
#include "easel.h" |
| 27 |
#include "esl_alphabet.h" |
| 28 |
#include "esl_gumbel.h" |
| 29 |
|
| 30 |
#include "hmmer.h" |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
int |
| 64 |
p7_GViterbi(const ESL_DSQ *dsq, int L, const P7_PROFILE *gm, P7_GMX *gx, float *opt_sc) |
| 65 |
{ |
| 66 |
float const *tsc = gm->tsc; |
| 67 |
float **dp = gx->dp; |
| 68 |
float *xmx = gx->xmx; |
| 69 |
int M = gm->M; |
| 70 |
int i,k; |
| 71 |
float esc = p7_profile_IsLocal(gm) ? 0 : -eslINFINITY; |
| 72 |
|
| 73 |
|
| 74 |
XMX(0,p7G_N) = 0; |
| 75 |
XMX(0,p7G_B) = gm->xsc[p7P_N][p7P_MOVE]; |
| 76 |
XMX(0,p7G_E) = XMX(0,p7G_C) = XMX(0,p7G_J) = -eslINFINITY; |
| 77 |
for (k = 0; k <= gm->M; k++) |
| 78 |
MMX(0,k) = IMX(0,k) = DMX(0,k) = -eslINFINITY; |
| 79 |
|
| 80 |
|
| 81 |
for (i = 1; i <= L; i++) |
| 82 |
{ |
| 83 |
float const *rsc = gm->rsc[dsq[i]]; |
| 84 |
float sc; |
| 85 |
|
| 86 |
MMX(i,0) = IMX(i,0) = DMX(i,0) = -eslINFINITY; |
| 87 |
XMX(i,p7G_E) = -eslINFINITY; |
| 88 |
|
| 89 |
for (k = 1; k < gm->M; k++) |
| 90 |
{ |
| 91 |
|
| 92 |
sc = ESL_MAX( MMX(i-1,k-1) + TSC(p7P_MM,k-1), |
| 93 |
IMX(i-1,k-1) + TSC(p7P_IM,k-1)); |
| 94 |
sc = ESL_MAX(sc, DMX(i-1,k-1) + TSC(p7P_DM,k-1)); |
| 95 |
sc = ESL_MAX(sc, XMX(i-1,p7G_B) + TSC(p7P_BM,k-1)); |
| 96 |
MMX(i,k) = sc + MSC(k); |
| 97 |
|
| 98 |
|
| 99 |
XMX(i,p7G_E) = ESL_MAX(XMX(i,p7G_E), MMX(i,k) + esc); |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
sc = ESL_MAX(MMX(i-1,k) + TSC(p7P_MI,k), |
| 106 |
IMX(i-1,k) + TSC(p7P_II,k)); |
| 107 |
IMX(i,k) = sc + ISC(k); |
| 108 |
|
| 109 |
|
| 110 |
DMX(i,k) = ESL_MAX(MMX(i,k-1) + TSC(p7P_MD,k-1), |
| 111 |
DMX(i,k-1) + TSC(p7P_DD,k-1)); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
sc = ESL_MAX( MMX(i-1,M-1) + TSC(p7P_MM,M-1), |
| 116 |
IMX(i-1,M-1) + TSC(p7P_IM,M-1)); |
| 117 |
sc = ESL_MAX(sc, DMX(i-1,M-1 ) + TSC(p7P_DM,M-1)); |
| 118 |
sc = ESL_MAX(sc, XMX(i-1,p7G_B) + TSC(p7P_BM,M-1)); |
| 119 |
MMX(i,M) = sc + MSC(M); |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
DMX(i,M) = ESL_MAX(MMX(i,M-1) + TSC(p7P_MD,M-1), |
| 126 |
DMX(i,M-1) + TSC(p7P_DD,M-1)); |
| 127 |
|
| 128 |
|
| 129 |
sc = ESL_MAX(XMX(i,p7G_E), MMX(i,M)); |
| 130 |
XMX(i,p7G_E) = ESL_MAX(sc, DMX(i,M)); |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
sc = XMX(i-1,p7G_J) + gm->xsc[p7P_J][p7P_LOOP]; |
| 137 |
XMX(i,p7G_J) = ESL_MAX(sc, XMX(i, p7G_E) + gm->xsc[p7P_E][p7P_LOOP]); |
| 138 |
|
| 139 |
|
| 140 |
sc = XMX(i-1,p7G_C) + gm->xsc[p7P_C][p7P_LOOP]; |
| 141 |
XMX(i,p7G_C) = ESL_MAX(sc, XMX(i, p7G_E) + gm->xsc[p7P_E][p7P_MOVE]); |
| 142 |
|
| 143 |
|
| 144 |
XMX(i,p7G_N) = XMX(i-1,p7G_N) + gm->xsc[p7P_N][p7P_LOOP]; |
| 145 |
|
| 146 |
|
| 147 |
sc = XMX(i,p7G_N) + gm->xsc[p7P_N][p7P_MOVE]; |
| 148 |
XMX(i,p7G_B) = ESL_MAX(sc, XMX(i,p7G_J) + gm->xsc[p7P_J][p7P_MOVE]); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
if (opt_sc != NULL) *opt_sc = XMX(L,p7G_C) + gm->xsc[p7P_C][p7P_MOVE]; |
| 153 |
gx->M = gm->M; |
| 154 |
gx->L = L; |
| 155 |
return eslOK; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
int |
| 186 |
p7_GViterbi_longtarget(const ESL_DSQ *dsq, int L, const P7_PROFILE *gm, P7_GMX *gx, |
| 187 |
float filtersc, double P, P7_HMM_WINDOWLIST *windowlist) |
| 188 |
{ |
| 189 |
float const *tsc = gm->tsc; |
| 190 |
float **dp = gx->dp; |
| 191 |
float *xmx = gx->xmx; |
| 192 |
int M = gm->M; |
| 193 |
int i,k; |
| 194 |
float esc = p7_profile_IsLocal(gm) ? 0 : -eslINFINITY; |
| 195 |
|
| 196 |
int16_t sc_thresh; |
| 197 |
float invP; |
| 198 |
|
| 199 |
|
| 200 |
XMX(0,p7G_N) = 0; |
| 201 |
XMX(0,p7G_B) = gm->xsc[p7P_N][p7P_MOVE]; |
| 202 |
XMX(0,p7G_E) = XMX(0,p7G_C) = XMX(0,p7G_J) = -eslINFINITY; |
| 203 |
for (k = 0; k <= gm->M; k++) |
| 204 |
MMX(0,k) = IMX(0,k) = DMX(0,k) = -eslINFINITY; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
invP = esl_gumbel_invsurv(P, gm->evparam[p7_VMU], gm->evparam[p7_VLAMBDA]); |
| 220 |
sc_thresh = (int) ceil (filtersc + (eslCONST_LOG2 * invP) |
| 221 |
- gm->xsc[p7P_E][p7P_MOVE] - gm->xsc[p7P_C][p7P_MOVE] ); |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
for (i = 1; i <= L; i++) |
| 226 |
{ |
| 227 |
float const *rsc = gm->rsc[dsq[i]]; |
| 228 |
float sc; |
| 229 |
|
| 230 |
MMX(i,0) = IMX(i,0) = DMX(i,0) = -eslINFINITY; |
| 231 |
XMX(i,p7G_E) = -eslINFINITY; |
| 232 |
|
| 233 |
for (k = 1; k < gm->M; k++) |
| 234 |
{ |
| 235 |
|
| 236 |
sc = ESL_MAX( MMX(i-1,k-1) + TSC(p7P_MM,k-1), |
| 237 |
IMX(i-1,k-1) + TSC(p7P_IM,k-1)); |
| 238 |
sc = ESL_MAX(sc, DMX(i-1,k-1) + TSC(p7P_DM,k-1)); |
| 239 |
sc = ESL_MAX(sc, XMX(i-1,p7G_B) + TSC(p7P_BM,k-1)); |
| 240 |
MMX(i,k) = sc + MSC(k); |
| 241 |
|
| 242 |
|
| 243 |
XMX(i,p7G_E) = ESL_MAX(XMX(i,p7G_E), MMX(i,k) + esc); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
sc = ESL_MAX(MMX(i-1,k) + TSC(p7P_MI,k), |
| 250 |
IMX(i-1,k) + TSC(p7P_II,k)); |
| 251 |
IMX(i,k) = sc + ISC(k); |
| 252 |
|
| 253 |
|
| 254 |
DMX(i,k) = ESL_MAX(MMX(i,k-1) + TSC(p7P_MD,k-1), |
| 255 |
DMX(i,k-1) + TSC(p7P_DD,k-1)); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
sc = ESL_MAX( MMX(i-1,M-1) + TSC(p7P_MM,M-1), |
| 260 |
IMX(i-1,M-1) + TSC(p7P_IM,M-1)); |
| 261 |
sc = ESL_MAX(sc, DMX(i-1,M-1 ) + TSC(p7P_DM,M-1)); |
| 262 |
sc = ESL_MAX(sc, XMX(i-1,p7G_B) + TSC(p7P_BM,M-1)); |
| 263 |
MMX(i,M) = sc + MSC(M); |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
DMX(i,M) = ESL_MAX(MMX(i,M-1) + TSC(p7P_MD,M-1), |
| 270 |
DMX(i,M-1) + TSC(p7P_DD,M-1)); |
| 271 |
|
| 272 |
|
| 273 |
sc = ESL_MAX(XMX(i,p7G_E), MMX(i,M)); |
| 274 |
XMX(i,p7G_E) = ESL_MAX(sc, DMX(i,M)); |
| 275 |
|
| 276 |
|
| 277 |
if (XMX(i,p7G_E) >= sc_thresh) { |
| 278 |
|
| 279 |
|
| 280 |
for (k = 1; k <= gm->M; k++) { |
| 281 |
if (MMX(i,k) == XMX(i,p7G_E)) { |
| 282 |
p7_hmmwindow_new(windowlist, 0, i, 0, k, 1, 0.0, p7_NOCOMPLEMENT, L); |
| 283 |
} |
| 284 |
MMX(i,0) = IMX(i,0) = DMX(i,0) = -eslINFINITY; |
| 285 |
} |
| 286 |
} else { |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
sc = XMX(i-1,p7G_J) + gm->xsc[p7P_J][p7P_LOOP]; |
| 293 |
XMX(i,p7G_J) = ESL_MAX(sc, XMX(i, p7G_E) + gm->xsc[p7P_E][p7P_LOOP]); |
| 294 |
|
| 295 |
|
| 296 |
sc = XMX(i-1,p7G_C) + gm->xsc[p7P_C][p7P_LOOP]; |
| 297 |
XMX(i,p7G_C) = ESL_MAX(sc, XMX(i, p7G_E) + gm->xsc[p7P_E][p7P_MOVE]); |
| 298 |
|
| 299 |
|
| 300 |
XMX(i,p7G_N) = XMX(i-1,p7G_N) + gm->xsc[p7P_N][p7P_LOOP]; |
| 301 |
|
| 302 |
|
| 303 |
sc = XMX(i,p7G_N) + gm->xsc[p7P_N][p7P_MOVE]; |
| 304 |
XMX(i,p7G_B) = ESL_MAX(sc, XMX(i,p7G_J) + gm->xsc[p7P_J][p7P_MOVE]); |
| 305 |
|
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
gx->M = gm->M; |
| 311 |
gx->L = L; |
| 312 |
return eslOK; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
#ifdef p7GENERIC_VITERBI_BENCHMARK |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
#include <p7_config.h> |
| 334 |
|
| 335 |
#include "easel.h" |
| 336 |
#include "esl_alphabet.h" |
| 337 |
#include "esl_getopts.h" |
| 338 |
#include "esl_random.h" |
| 339 |
#include "esl_randomseq.h" |
| 340 |
#include "esl_stopwatch.h" |
| 341 |
|
| 342 |
#include "hmmer.h" |
| 343 |
|
| 344 |
static ESL_OPTIONS options[] = { |
| 345 |
|
| 346 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 347 |
{ "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 348 |
{ "-L", eslARG_INT, "400", NULL, "n>0", NULL, NULL, NULL, "length of random target seqs", 0 }, |
| 349 |
{ "-N", eslARG_INT, "20000", NULL, "n>0", NULL, NULL, NULL, "number of random target seqs", 0 }, |
| 350 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 351 |
}; |
| 352 |
static char usage[] = "[-options] <hmmfile>"; |
| 353 |
static char banner[] = "benchmark driver for generic Viterbi"; |
| 354 |
|
| 355 |
int |
| 356 |
main(int argc, char **argv) |
| 357 |
{ |
| 358 |
ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage); |
| 359 |
char *hmmfile = esl_opt_GetArg(go, 1); |
| 360 |
ESL_STOPWATCH *w = esl_stopwatch_Create(); |
| 361 |
ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s")); |
| 362 |
ESL_ALPHABET *abc = NULL; |
| 363 |
P7_HMMFILE *hfp = NULL; |
| 364 |
P7_HMM *hmm = NULL; |
| 365 |
P7_BG *bg = NULL; |
| 366 |
P7_PROFILE *gm = NULL; |
| 367 |
P7_GMX *gx = NULL; |
| 368 |
int L = esl_opt_GetInteger(go, "-L"); |
| 369 |
int N = esl_opt_GetInteger(go, "-N"); |
| 370 |
ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2)); |
| 371 |
int i; |
| 372 |
float sc; |
| 373 |
double base_time, bench_time, Mcs; |
| 374 |
|
| 375 |
if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile); |
| 376 |
if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM"); |
| 377 |
|
| 378 |
bg = p7_bg_Create(abc); |
| 379 |
p7_bg_SetLength(bg, L); |
| 380 |
gm = p7_profile_Create(hmm->M, abc); |
| 381 |
p7_ProfileConfig(hmm, bg, gm, L, p7_UNILOCAL); |
| 382 |
gx = p7_gmx_Create(gm->M, L); |
| 383 |
|
| 384 |
|
| 385 |
esl_stopwatch_Start(w); |
| 386 |
for (i = 0; i < N; i++) esl_rsq_xfIID(r, bg->f, abc->K, L, dsq); |
| 387 |
esl_stopwatch_Stop(w); |
| 388 |
base_time = w->user; |
| 389 |
|
| 390 |
|
| 391 |
esl_stopwatch_Start(w); |
| 392 |
for (i = 0; i < N; i++) |
| 393 |
{ |
| 394 |
esl_rsq_xfIID(r, bg->f, abc->K, L, dsq); |
| 395 |
p7_GViterbi (dsq, L, gm, gx, &sc); |
| 396 |
} |
| 397 |
esl_stopwatch_Stop(w); |
| 398 |
bench_time = w->user - base_time; |
| 399 |
Mcs = (double) N * (double) L * (double) gm->M * 1e-6 / (double) bench_time; |
| 400 |
esl_stopwatch_Display(stdout, w, "# CPU time: "); |
| 401 |
printf("# M = %d\n", gm->M); |
| 402 |
printf("# %.1f Mc/s\n", Mcs); |
| 403 |
|
| 404 |
|
| 405 |
free(dsq); |
| 406 |
p7_gmx_Destroy(gx); |
| 407 |
p7_profile_Destroy(gm); |
| 408 |
p7_bg_Destroy(bg); |
| 409 |
p7_hmm_Destroy(hmm); |
| 410 |
p7_hmmfile_Close(hfp); |
| 411 |
esl_alphabet_Destroy(abc); |
| 412 |
esl_stopwatch_Destroy(w); |
| 413 |
esl_randomness_Destroy(r); |
| 414 |
esl_getopts_Destroy(go); |
| 415 |
return 0; |
| 416 |
} |
| 417 |
#endif |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
#ifdef p7GENERIC_VITERBI_TESTDRIVE |
| 426 |
#include <string.h> |
| 427 |
#include "esl_getopts.h" |
| 428 |
#include "esl_msa.h" |
| 429 |
#include "esl_msafile.h" |
| 430 |
#include "esl_random.h" |
| 431 |
#include "esl_randomseq.h" |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
static void |
| 437 |
utest_basic(ESL_GETOPTS *go) |
| 438 |
{ |
| 439 |
char *query= "# STOCKHOLM 1.0\n\nseq1 GAATTC\nseq2 GAATTC\n//\n"; |
| 440 |
int fmt = eslMSAFILE_STOCKHOLM; |
| 441 |
char *targ = "GAATTC"; |
| 442 |
ESL_ALPHABET *abc = NULL; |
| 443 |
ESL_MSA *msa = NULL; |
| 444 |
P7_HMM *hmm = NULL; |
| 445 |
P7_PROFILE *gm = NULL; |
| 446 |
P7_BG *bg = NULL; |
| 447 |
P7_PRIOR *pri = NULL; |
| 448 |
ESL_DSQ *dsq = NULL; |
| 449 |
P7_GMX *gx = NULL; |
| 450 |
P7_TRACE *tr = NULL; |
| 451 |
int L = strlen(targ); |
| 452 |
float vsc, vsc2, fsc; |
| 453 |
|
| 454 |
if ((abc = esl_alphabet_Create(eslDNA)) == NULL) esl_fatal("failed to create alphabet"); |
| 455 |
if ((pri = p7_prior_CreateNucleic()) == NULL) esl_fatal("failed to create prior"); |
| 456 |
if ((msa = esl_msa_CreateFromString(query, fmt)) == NULL) esl_fatal("failed to create MSA"); |
| 457 |
if (esl_msa_Digitize(abc, msa, NULL) != eslOK) esl_fatal("failed to digitize MSA"); |
| 458 |
if (p7_Fastmodelmaker(msa, 0.5, NULL, &hmm, NULL) != eslOK) esl_fatal("failed to create GAATTC model"); |
| 459 |
if (p7_ParameterEstimation(hmm, pri) != eslOK) esl_fatal("failed to parameterize GAATTC model"); |
| 460 |
if (p7_hmm_SetConsensus(hmm, NULL) != eslOK) esl_fatal("failed to make consensus"); |
| 461 |
if ((bg = p7_bg_Create(abc)) == NULL) esl_fatal("failed to create DNA null model"); |
| 462 |
if ((gm = p7_profile_Create(hmm->M, abc)) == NULL) esl_fatal("failed to create GAATTC profile"); |
| 463 |
if (p7_ProfileConfig(hmm, bg, gm, L, p7_UNILOCAL)!= eslOK) esl_fatal("failed to config profile"); |
| 464 |
if (p7_profile_Validate(gm, NULL, 0.0001) != eslOK) esl_fatal("whoops, profile is bad!"); |
| 465 |
if (esl_abc_CreateDsq(abc, targ, &dsq) != eslOK) esl_fatal("failed to create GAATTC digital sequence"); |
| 466 |
if ((gx = p7_gmx_Create(gm->M, L)) == NULL) esl_fatal("failed to create DP matrix"); |
| 467 |
if ((tr = p7_trace_Create()) == NULL) esl_fatal("trace creation failed"); |
| 468 |
|
| 469 |
p7_GViterbi (dsq, L, gm, gx, &vsc); |
| 470 |
if (esl_opt_GetBoolean(go, "-v")) printf("Viterbi score: %.4f\n", vsc); |
| 471 |
if (esl_opt_GetBoolean(go, "-v")) p7_gmx_Dump(stdout, gx, p7_DEFAULT); |
| 472 |
|
| 473 |
p7_GTrace (dsq, L, gm, gx, tr); |
| 474 |
p7_trace_Score(tr, dsq, gm, &vsc2); |
| 475 |
if (esl_opt_GetBoolean(go, "-v")) p7_trace_Dump(stdout, tr, gm, dsq); |
| 476 |
|
| 477 |
if (esl_FCompare_old(vsc, vsc2, 1e-5) != eslOK) esl_fatal("trace score and Viterbi score don't agree."); |
| 478 |
|
| 479 |
p7_GForward (dsq, L, gm, gx, &fsc); |
| 480 |
if (esl_opt_GetBoolean(go, "-v")) printf("Forward score: %.4f\n", fsc); |
| 481 |
if (esl_opt_GetBoolean(go, "-v")) p7_gmx_Dump(stdout, gx, p7_DEFAULT); |
| 482 |
|
| 483 |
p7_trace_Destroy(tr); |
| 484 |
p7_gmx_Destroy(gx); |
| 485 |
free(dsq); |
| 486 |
p7_profile_Destroy(gm); |
| 487 |
p7_bg_Destroy(bg); |
| 488 |
p7_hmm_Destroy(hmm); |
| 489 |
esl_msa_Destroy(msa); |
| 490 |
p7_prior_Destroy(pri); |
| 491 |
esl_alphabet_Destroy(abc); |
| 492 |
return; |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
static void |
| 503 |
utest_viterbi(ESL_GETOPTS *go, ESL_RANDOMNESS *r, ESL_ALPHABET *abc, P7_BG *bg, P7_PROFILE *gm, int nseq, int L) |
| 504 |
{ |
| 505 |
float avg_sc = 0.; |
| 506 |
char errbuf[eslERRBUFSIZE]; |
| 507 |
ESL_DSQ *dsq = NULL; |
| 508 |
P7_GMX *gx = NULL; |
| 509 |
P7_TRACE *tr = NULL; |
| 510 |
int idx; |
| 511 |
float sc1, sc2; |
| 512 |
|
| 513 |
if ((dsq = malloc(sizeof(ESL_DSQ) *(L+2))) == NULL) esl_fatal("malloc failed"); |
| 514 |
if ((tr = p7_trace_Create()) == NULL) esl_fatal("trace creation failed"); |
| 515 |
if ((gx = p7_gmx_Create(gm->M, L)) == NULL) esl_fatal("matrix creation failed"); |
| 516 |
|
| 517 |
for (idx = 0; idx < nseq; idx++) |
| 518 |
{ |
| 519 |
if (esl_rsq_xfIID(r, bg->f, abc->K, L, dsq) != eslOK) esl_fatal("seq generation failed"); |
| 520 |
if (p7_GViterbi(dsq, L, gm, gx, &sc1) != eslOK) esl_fatal("viterbi failed"); |
| 521 |
if (p7_GTrace (dsq, L, gm, gx, tr) != eslOK) esl_fatal("trace failed"); |
| 522 |
if (p7_trace_Validate(tr, abc, dsq, errbuf) != eslOK) esl_fatal("trace invalid:\n%s", errbuf); |
| 523 |
if (p7_trace_Score(tr, dsq, gm, &sc2) != eslOK) esl_fatal("trace score failed"); |
| 524 |
if (esl_FCompare_old(sc1, sc2, 1e-6) != eslOK) esl_fatal("Trace score != Viterbi score"); |
| 525 |
if (p7_bg_NullOne(bg, dsq, L, &sc2) != eslOK) esl_fatal("null score failed"); |
| 526 |
|
| 527 |
avg_sc += (sc1 - sc2); |
| 528 |
|
| 529 |
if (esl_opt_GetBoolean(go, "--vv")) |
| 530 |
printf("utest_viterbi: Viterbi score: %.4f (null %.4f) (total so far: %.4f)\n", sc1, sc2, avg_sc); |
| 531 |
|
| 532 |
p7_trace_Reuse(tr); |
| 533 |
} |
| 534 |
|
| 535 |
avg_sc /= (float) nseq; |
| 536 |
if (avg_sc > 0.) esl_fatal("Viterbi scores have positive expectation (%f nats)", avg_sc); |
| 537 |
|
| 538 |
p7_gmx_Destroy(gx); |
| 539 |
p7_trace_Destroy(tr); |
| 540 |
free(dsq); |
| 541 |
return; |
| 542 |
} |
| 543 |
|
| 544 |
#endif |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
#ifdef p7GENERIC_VITERBI_TESTDRIVE |
| 554 |
#include "easel.h" |
| 555 |
#include "esl_getopts.h" |
| 556 |
#include "esl_msa.h" |
| 557 |
|
| 558 |
#include <p7_config.h> |
| 559 |
#include "hmmer.h" |
| 560 |
|
| 561 |
static ESL_OPTIONS options[] = { |
| 562 |
|
| 563 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 564 |
{ "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 565 |
{ "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be verbose", 0 }, |
| 566 |
{ "--vv", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be very verbose", 0 }, |
| 567 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 568 |
}; |
| 569 |
static char usage[] = "[-options]"; |
| 570 |
static char banner[] = "unit test driver for the generic Viterbi implementation"; |
| 571 |
|
| 572 |
int |
| 573 |
main(int argc, char **argv) |
| 574 |
{ |
| 575 |
ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 576 |
ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s")); |
| 577 |
ESL_ALPHABET *abc = NULL; |
| 578 |
P7_HMM *hmm = NULL; |
| 579 |
P7_PROFILE *gm = NULL; |
| 580 |
P7_BG *bg = NULL; |
| 581 |
int M = 100; |
| 582 |
int L = 200; |
| 583 |
int nseq = 20; |
| 584 |
char errbuf[eslERRBUFSIZE]; |
| 585 |
|
| 586 |
if ((abc = esl_alphabet_Create(eslAMINO)) == NULL) esl_fatal("failed to create alphabet"); |
| 587 |
if (p7_hmm_Sample(r, M, abc, &hmm) != eslOK) esl_fatal("failed to sample an HMM"); |
| 588 |
if ((bg = p7_bg_Create(abc)) == NULL) esl_fatal("failed to create null model"); |
| 589 |
if ((gm = p7_profile_Create(hmm->M, abc)) == NULL) esl_fatal("failed to create profile"); |
| 590 |
if (p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL) != eslOK) esl_fatal("failed to config profile"); |
| 591 |
if (p7_hmm_Validate (hmm, errbuf, 0.0001) != eslOK) esl_fatal("whoops, HMM is bad!: %s", errbuf); |
| 592 |
if (p7_profile_Validate(gm, errbuf, 0.0001) != eslOK) esl_fatal("whoops, profile is bad!: %s", errbuf); |
| 593 |
|
| 594 |
utest_basic (go); |
| 595 |
utest_viterbi(go, r, abc, bg, gm, nseq, L); |
| 596 |
|
| 597 |
p7_profile_Destroy(gm); |
| 598 |
p7_bg_Destroy(bg); |
| 599 |
p7_hmm_Destroy(hmm); |
| 600 |
esl_alphabet_Destroy(abc); |
| 601 |
esl_randomness_Destroy(r); |
| 602 |
esl_getopts_Destroy(go); |
| 603 |
return 0; |
| 604 |
} |
| 605 |
#endif |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
#ifdef p7GENERIC_VITERBI_EXAMPLE |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
#include <p7_config.h> |
| 620 |
|
| 621 |
#include "easel.h" |
| 622 |
#include "esl_alphabet.h" |
| 623 |
#include "esl_getopts.h" |
| 624 |
#include "esl_sq.h" |
| 625 |
#include "esl_sqio.h" |
| 626 |
|
| 627 |
#include "hmmer.h" |
| 628 |
|
| 629 |
static ESL_OPTIONS options[] = { |
| 630 |
|
| 631 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 632 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 633 |
}; |
| 634 |
static char usage[] = "[-options] <hmmfile> <seqfile>"; |
| 635 |
static char banner[] = "example of generic Viterbi"; |
| 636 |
|
| 637 |
|
| 638 |
int |
| 639 |
main(int argc, char **argv) |
| 640 |
{ |
| 641 |
ESL_GETOPTS *go = p7_CreateDefaultApp(options, 2, argc, argv, banner, usage); |
| 642 |
char *hmmfile = esl_opt_GetArg(go, 1); |
| 643 |
char *seqfile = esl_opt_GetArg(go, 2); |
| 644 |
ESL_ALPHABET *abc = NULL; |
| 645 |
P7_HMMFILE *hfp = NULL; |
| 646 |
P7_HMM *hmm = NULL; |
| 647 |
P7_BG *bg = NULL; |
| 648 |
P7_PROFILE *gm = NULL; |
| 649 |
P7_GMX *fwd = NULL; |
| 650 |
ESL_SQ *sq = NULL; |
| 651 |
ESL_SQFILE *sqfp = NULL; |
| 652 |
P7_TRACE *tr = NULL; |
| 653 |
int format = eslSQFILE_UNKNOWN; |
| 654 |
char errbuf[eslERRBUFSIZE]; |
| 655 |
float sc; |
| 656 |
int d; |
| 657 |
int status; |
| 658 |
|
| 659 |
|
| 660 |
if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile); |
| 661 |
if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM"); |
| 662 |
p7_hmmfile_Close(hfp); |
| 663 |
|
| 664 |
|
| 665 |
sq = esl_sq_CreateDigital(abc); |
| 666 |
status = esl_sqfile_Open(seqfile, format, NULL, &sqfp); |
| 667 |
if (status == eslENOTFOUND) p7_Fail("No such file."); |
| 668 |
else if (status == eslEFORMAT) p7_Fail("Format unrecognized."); |
| 669 |
else if (status == eslEINVAL) p7_Fail("Can't autodetect stdin or .gz."); |
| 670 |
else if (status != eslOK) p7_Fail("Open failed, code %d.", status); |
| 671 |
if (esl_sqio_Read(sqfp, sq) != eslOK) p7_Fail("Failed to read sequence"); |
| 672 |
esl_sqfile_Close(sqfp); |
| 673 |
|
| 674 |
|
| 675 |
bg = p7_bg_Create(abc); |
| 676 |
p7_bg_SetLength(bg, sq->n); |
| 677 |
gm = p7_profile_Create(hmm->M, abc); |
| 678 |
p7_ProfileConfig(hmm, bg, gm, sq->n, p7_LOCAL); |
| 679 |
|
| 680 |
|
| 681 |
fwd = p7_gmx_Create(gm->M, sq->n); |
| 682 |
tr = p7_trace_Create(); |
| 683 |
|
| 684 |
|
| 685 |
p7_GViterbi (sq->dsq, sq->n, gm, fwd, &sc); |
| 686 |
p7_GTrace (sq->dsq, sq->n, gm, fwd, tr); |
| 687 |
|
| 688 |
|
| 689 |
p7_trace_Dump(stdout, tr, gm, sq->dsq); |
| 690 |
if (p7_trace_Validate(tr, abc, sq->dsq, errbuf) != eslOK) p7_Die("trace fails validation:\n%s\n", errbuf); |
| 691 |
|
| 692 |
|
| 693 |
p7_trace_Index(tr); |
| 694 |
printf("# Viterbi: %d domains : ", tr->ndom); |
| 695 |
for (d = 0; d < tr->ndom; d++) printf("%6d %6d %6d %6d ", tr->sqfrom[d], tr->sqto[d], tr->hmmfrom[d], tr->hmmto[d]); |
| 696 |
printf("\n"); |
| 697 |
|
| 698 |
|
| 699 |
p7_trace_Destroy(tr); |
| 700 |
p7_gmx_Destroy(fwd); |
| 701 |
p7_profile_Destroy(gm); |
| 702 |
p7_bg_Destroy(bg); |
| 703 |
p7_hmm_Destroy(hmm); |
| 704 |
esl_sq_Destroy(sq); |
| 705 |
esl_alphabet_Destroy(abc); |
| 706 |
esl_getopts_Destroy(go); |
| 707 |
return 0; |
| 708 |
} |
| 709 |
#endif |
| 710 |
|
| 711 |
|
| 712 |
|