| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#include <p7_config.h> |
| 15 |
|
| 16 |
#include <stdio.h> |
| 17 |
#include <math.h> |
| 18 |
|
| 19 |
#include <xmmintrin.h> |
| 20 |
#include <emmintrin.h> |
| 21 |
|
| 22 |
#include "easel.h" |
| 23 |
#include "esl_random.h" |
| 24 |
#include "esl_sse.h" |
| 25 |
#include "esl_vectorops.h" |
| 26 |
|
| 27 |
#include "hmmer.h" |
| 28 |
#include "impl_sse.h" |
| 29 |
|
| 30 |
static inline int select_m(ESL_RANDOMNESS *rng, const P7_OPROFILE *om, const P7_OMX *ox, int i, int k); |
| 31 |
static inline int select_d(ESL_RANDOMNESS *rng, const P7_OPROFILE *om, const P7_OMX *ox, int i, int k); |
| 32 |
static inline int select_i(ESL_RANDOMNESS *rng, const P7_OPROFILE *om, const P7_OMX *ox, int i, int k); |
| 33 |
static inline int select_n(int i); |
| 34 |
static inline int select_c(ESL_RANDOMNESS *rng, const P7_OPROFILE *om, const P7_OMX *ox, int i); |
| 35 |
static inline int select_j(ESL_RANDOMNESS *rng, const P7_OPROFILE *om, const P7_OMX *ox, int i); |
| 36 |
static inline int select_e(ESL_RANDOMNESS *rng, const P7_OPROFILE *om, const P7_OMX *ox, int i, int *ret_k); |
| 37 |
static inline int select_b(ESL_RANDOMNESS *rng, const P7_OPROFILE *om, const P7_OMX *ox, int i); |
| 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 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
int |
| 71 |
p7_StochasticTrace(ESL_RANDOMNESS *rng, const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, const P7_OMX *ox, |
| 72 |
P7_TRACE *tr) |
| 73 |
{ |
| 74 |
int i; |
| 75 |
int k; |
| 76 |
int s0, s1; |
| 77 |
int status; |
| 78 |
|
| 79 |
if (tr->N != 0) ESL_EXCEPTION(eslEINVAL, "trace not empty; needs to be Reuse()'d?"); |
| 80 |
|
| 81 |
i = L; |
| 82 |
k = 0; |
| 83 |
if ((status = p7_trace_Append(tr, p7T_T, k, i)) != eslOK) return status; |
| 84 |
if ((status = p7_trace_Append(tr, p7T_C, k, i)) != eslOK) return status; |
| 85 |
s0 = tr->st[tr->N-1]; |
| 86 |
while (s0 != p7T_S) |
| 87 |
{ |
| 88 |
switch (s0) { |
| 89 |
case p7T_M: s1 = select_m(rng, om, ox, i, k); k--; i--; break; |
| 90 |
case p7T_D: s1 = select_d(rng, om, ox, i, k); k--; break; |
| 91 |
case p7T_I: s1 = select_i(rng, om, ox, i, k); i--; break; |
| 92 |
case p7T_N: s1 = select_n(i); break; |
| 93 |
case p7T_C: s1 = select_c(rng, om, ox, i); break; |
| 94 |
case p7T_J: s1 = select_j(rng, om, ox, i); break; |
| 95 |
case p7T_E: s1 = select_e(rng, om, ox, i, &k); break; |
| 96 |
case p7T_B: s1 = select_b(rng, om, ox, i); break; |
| 97 |
default: ESL_EXCEPTION(eslEINVAL, "bogus state in traceback"); |
| 98 |
} |
| 99 |
if (s1 == -1) ESL_EXCEPTION(eslEINVAL, "Stochastic traceback choice failed"); |
| 100 |
|
| 101 |
if ((status = p7_trace_Append(tr, s1, k, i)) != eslOK) return status; |
| 102 |
|
| 103 |
if ( (s1 == p7T_N || s1 == p7T_J || s1 == p7T_C) && s1 == s0) i--; |
| 104 |
s0 = s1; |
| 105 |
} |
| 106 |
|
| 107 |
tr->M = om->M; |
| 108 |
tr->L = L; |
| 109 |
return p7_trace_Reverse(tr); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
static inline int |
| 125 |
select_m(ESL_RANDOMNESS *rng, const P7_OPROFILE *om, const P7_OMX *ox, int i, int k) |
| 126 |
{ |
| 127 |
int Q = p7O_NQF(ox->M); |
| 128 |
int q = (k-1) % Q; |
| 129 |
int r = (k-1) / Q; |
| 130 |
__m128 *tp = om->tfv + 7*q; |
| 131 |
__m128 xBv = _mm_set1_ps(ox->xmx[(i-1)*p7X_NXCELLS+p7X_B]); |
| 132 |
__m128 mpv, dpv, ipv; |
| 133 |
union { __m128 v; float p[4]; } u; |
| 134 |
float path[4]; |
| 135 |
int state[4] = { p7T_B, p7T_M, p7T_I, p7T_D }; |
| 136 |
|
| 137 |
if (q > 0) { |
| 138 |
mpv = ox->dpf[i-1][(q-1)*3 + p7X_M]; |
| 139 |
dpv = ox->dpf[i-1][(q-1)*3 + p7X_D]; |
| 140 |
ipv = ox->dpf[i-1][(q-1)*3 + p7X_I]; |
| 141 |
} else { |
| 142 |
mpv = esl_sse_rightshiftz_float(ox->dpf[i-1][(Q-1)*3 + p7X_M]); |
| 143 |
dpv = esl_sse_rightshiftz_float(ox->dpf[i-1][(Q-1)*3 + p7X_D]); |
| 144 |
ipv = esl_sse_rightshiftz_float(ox->dpf[i-1][(Q-1)*3 + p7X_I]); |
| 145 |
} |
| 146 |
|
| 147 |
u.v = _mm_mul_ps(xBv, *tp); tp++; path[0] = u.p[r]; |
| 148 |
u.v = _mm_mul_ps(mpv, *tp); tp++; path[1] = u.p[r]; |
| 149 |
u.v = _mm_mul_ps(ipv, *tp); tp++; path[2] = u.p[r]; |
| 150 |
u.v = _mm_mul_ps(dpv, *tp); path[3] = u.p[r]; |
| 151 |
esl_vec_FNorm(path, 4); |
| 152 |
return state[esl_rnd_FChoose(rng, path, 4)]; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
static inline int |
| 157 |
select_d(ESL_RANDOMNESS *rng, const P7_OPROFILE *om, const P7_OMX *ox, int i, int k) |
| 158 |
{ |
| 159 |
int Q = p7O_NQF(ox->M); |
| 160 |
int q = (k-1) % Q; |
| 161 |
int r = (k-1) / Q; |
| 162 |
__m128 mpv, dpv; |
| 163 |
__m128 tmdv, tddv; |
| 164 |
union { __m128 v; float p[4]; } u; |
| 165 |
float path[2]; |
| 166 |
int state[2] = { p7T_M, p7T_D }; |
| 167 |
|
| 168 |
if (q > 0) { |
| 169 |
mpv = ox->dpf[i][(q-1)*3 + p7X_M]; |
| 170 |
dpv = ox->dpf[i][(q-1)*3 + p7X_D]; |
| 171 |
tmdv = om->tfv[7*(q-1) + p7O_MD]; |
| 172 |
tddv = om->tfv[7*Q + (q-1)]; |
| 173 |
} else { |
| 174 |
mpv = esl_sse_rightshiftz_float(ox->dpf[i][(Q-1)*3 + p7X_M]); |
| 175 |
dpv = esl_sse_rightshiftz_float(ox->dpf[i][(Q-1)*3 + p7X_D]); |
| 176 |
tmdv = esl_sse_rightshiftz_float(om->tfv[7*(Q-1) + p7O_MD]); |
| 177 |
tddv = esl_sse_rightshiftz_float(om->tfv[8*Q-1]); |
| 178 |
} |
| 179 |
|
| 180 |
u.v = _mm_mul_ps(mpv, tmdv); path[0] = u.p[r]; |
| 181 |
u.v = _mm_mul_ps(dpv, tddv); path[1] = u.p[r]; |
| 182 |
esl_vec_FNorm(path, 2); |
| 183 |
return state[esl_rnd_FChoose(rng, path, 2)]; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
static inline int |
| 188 |
select_i(ESL_RANDOMNESS *rng, const P7_OPROFILE *om, const P7_OMX *ox, int i, int k) |
| 189 |
{ |
| 190 |
int Q = p7O_NQF(ox->M); |
| 191 |
int q = (k-1) % Q; |
| 192 |
int r = (k-1) / Q; |
| 193 |
__m128 mpv = ox->dpf[i-1][q*3 + p7X_M]; |
| 194 |
__m128 ipv = ox->dpf[i-1][q*3 + p7X_I]; |
| 195 |
__m128 *tp = om->tfv + 7*q + p7O_MI; |
| 196 |
union { __m128 v; float p[4]; } u; |
| 197 |
float path[2]; |
| 198 |
int state[2] = { p7T_M, p7T_I }; |
| 199 |
|
| 200 |
u.v = _mm_mul_ps(mpv, *tp); tp++; path[0] = u.p[r]; |
| 201 |
u.v = _mm_mul_ps(ipv, *tp); path[1] = u.p[r]; |
| 202 |
esl_vec_FNorm(path, 2); |
| 203 |
return state[esl_rnd_FChoose(rng, path, 2)]; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
static inline int |
| 208 |
select_n(int i) |
| 209 |
{ |
| 210 |
if (i == 0) return p7T_S; |
| 211 |
else return p7T_N; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
static inline int |
| 216 |
select_c(ESL_RANDOMNESS *rng, const P7_OPROFILE *om, const P7_OMX *ox, int i) |
| 217 |
{ |
| 218 |
float path[2]; |
| 219 |
int state[2] = { p7T_C, p7T_E }; |
| 220 |
|
| 221 |
path[0] = ox->xmx[(i-1)*p7X_NXCELLS+p7X_C] * om->xf[p7O_C][p7O_LOOP]; |
| 222 |
path[1] = ox->xmx[ i*p7X_NXCELLS+p7X_E] * om->xf[p7O_E][p7O_MOVE] * ox->xmx[i*p7X_NXCELLS+p7X_SCALE]; |
| 223 |
esl_vec_FNorm(path, 2); |
| 224 |
return state[esl_rnd_FChoose(rng, path, 2)]; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
static inline int |
| 229 |
select_j(ESL_RANDOMNESS *rng, const P7_OPROFILE *om, const P7_OMX *ox, int i) |
| 230 |
{ |
| 231 |
float path[2]; |
| 232 |
int state[2] = { p7T_J, p7T_E }; |
| 233 |
|
| 234 |
path[0] = ox->xmx[(i-1)*p7X_NXCELLS+p7X_J] * om->xf[p7O_J][p7O_LOOP]; |
| 235 |
path[1] = ox->xmx[ i*p7X_NXCELLS+p7X_E] * om->xf[p7O_E][p7O_LOOP] * ox->xmx[i*p7X_NXCELLS+p7X_SCALE]; |
| 236 |
esl_vec_FNorm(path, 2); |
| 237 |
return state[esl_rnd_FChoose(rng, path, 2)]; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
static inline int |
| 248 |
select_e(ESL_RANDOMNESS *rng, const P7_OPROFILE *om, const P7_OMX *ox, int i, int *ret_k) |
| 249 |
{ |
| 250 |
int Q = p7O_NQF(ox->M); |
| 251 |
double sum = 0.0; |
| 252 |
double roll = esl_random(rng); |
| 253 |
double norm = 1.0 / ox->xmx[i*p7X_NXCELLS+p7X_E]; |
| 254 |
__m128 xEv = _mm_set1_ps(norm); |
| 255 |
union { __m128 v; float p[4]; } u; |
| 256 |
int q,r; |
| 257 |
|
| 258 |
while (1) { |
| 259 |
for (q = 0; q < Q; q++) |
| 260 |
{ |
| 261 |
u.v = _mm_mul_ps(ox->dpf[i][q*3 + p7X_M], xEv); |
| 262 |
for (r = 0; r < 4; r++) { |
| 263 |
sum += u.p[r]; |
| 264 |
if (roll < sum) { *ret_k = r*Q + q + 1; return p7T_M;} |
| 265 |
} |
| 266 |
|
| 267 |
u.v = _mm_mul_ps(ox->dpf[i][q*3 + p7X_D], xEv); |
| 268 |
for (r = 0; r < 4; r++) { |
| 269 |
sum += u.p[r]; |
| 270 |
if (roll < sum) { *ret_k = r*Q + q + 1; return p7T_D;} |
| 271 |
} |
| 272 |
} |
| 273 |
ESL_DASSERT1((sum > 0.99)); |
| 274 |
} |
| 275 |
|
| 276 |
ESL_EXCEPTION(-1, "unreached code was reached. universe collapses."); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
static inline int |
| 281 |
select_b(ESL_RANDOMNESS *rng, const P7_OPROFILE *om, const P7_OMX *ox, int i) |
| 282 |
{ |
| 283 |
float path[2]; |
| 284 |
int state[2] = { p7T_N, p7T_J }; |
| 285 |
|
| 286 |
path[0] = ox->xmx[i*p7X_NXCELLS+p7X_N] * om->xf[p7O_N][p7O_MOVE]; |
| 287 |
path[1] = ox->xmx[i*p7X_NXCELLS+p7X_J] * om->xf[p7O_J][p7O_MOVE]; |
| 288 |
esl_vec_FNorm(path, 2); |
| 289 |
return state[esl_rnd_FChoose(rng, path, 2)]; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
#ifdef p7STOTRACE_BENCHMARK |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
#include <p7_config.h> |
| 303 |
|
| 304 |
#include "easel.h" |
| 305 |
#include "esl_alphabet.h" |
| 306 |
#include "esl_getopts.h" |
| 307 |
#include "esl_random.h" |
| 308 |
#include "esl_randomseq.h" |
| 309 |
#include "esl_stopwatch.h" |
| 310 |
|
| 311 |
#include "hmmer.h" |
| 312 |
|
| 313 |
static ESL_OPTIONS options[] = { |
| 314 |
|
| 315 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 316 |
{ "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 317 |
{ "-L", eslARG_INT, "400", NULL, "n>0", NULL, NULL, NULL, "length of random target seq" , 0 }, |
| 318 |
{ "-N", eslARG_INT, "50000", NULL, "n>0", NULL, NULL, NULL, "number of sampled tracebacks", 0 }, |
| 319 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 320 |
}; |
| 321 |
static char usage[] = "[-options] <hmmfile>"; |
| 322 |
static char banner[] = "benchmark driver for stochastic traceback, SSE version"; |
| 323 |
|
| 324 |
int |
| 325 |
main(int argc, char **argv) |
| 326 |
{ |
| 327 |
ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage); |
| 328 |
char *hmmfile = esl_opt_GetArg(go, 1); |
| 329 |
ESL_STOPWATCH *w = esl_stopwatch_Create(); |
| 330 |
ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s")); |
| 331 |
ESL_ALPHABET *abc = NULL; |
| 332 |
P7_HMMFILE *hfp = NULL; |
| 333 |
P7_HMM *hmm = NULL; |
| 334 |
P7_BG *bg = NULL; |
| 335 |
P7_PROFILE *gm = NULL; |
| 336 |
P7_OPROFILE *om = NULL; |
| 337 |
P7_GMX *gx = NULL; |
| 338 |
P7_OMX *fwd = NULL; |
| 339 |
P7_TRACE *tr = NULL; |
| 340 |
int L = esl_opt_GetInteger(go, "-L"); |
| 341 |
int N = esl_opt_GetInteger(go, "-N"); |
| 342 |
ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2)); |
| 343 |
int i; |
| 344 |
float sc, fsc, vsc; |
| 345 |
float bestsc = -eslINFINITY; |
| 346 |
|
| 347 |
if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile); |
| 348 |
if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM"); |
| 349 |
|
| 350 |
bg = p7_bg_Create(abc); p7_bg_SetLength(bg, L); |
| 351 |
gm = p7_profile_Create(hmm->M, abc); p7_ProfileConfig(hmm, bg, gm, L, p7_UNILOCAL); |
| 352 |
om = p7_oprofile_Create(gm->M, abc); p7_oprofile_Convert(gm, om); |
| 353 |
|
| 354 |
fwd = p7_omx_Create(gm->M, L, L); |
| 355 |
gx = p7_gmx_Create(gm->M, L); |
| 356 |
tr = p7_trace_Create(); |
| 357 |
esl_rsq_xfIID(r, bg->f, abc->K, L, dsq); |
| 358 |
|
| 359 |
p7_GViterbi(dsq, L, gm, gx, &vsc); |
| 360 |
p7_Forward (dsq, L, om, fwd, &fsc); |
| 361 |
|
| 362 |
esl_stopwatch_Start(w); |
| 363 |
for (i = 0; i < N; i++) |
| 364 |
{ |
| 365 |
p7_StochasticTrace(r, dsq, L, om, fwd, tr); |
| 366 |
p7_trace_Score(tr, dsq, gm, &sc); |
| 367 |
bestsc = ESL_MAX(bestsc, sc); |
| 368 |
p7_trace_Reuse(tr); |
| 369 |
} |
| 370 |
esl_stopwatch_Stop(w); |
| 371 |
esl_stopwatch_Display(stdout, w, "# CPU time: "); |
| 372 |
|
| 373 |
printf("forward sc = %.4f nats\n", fsc); |
| 374 |
printf("viterbi sc = %.4f nats\n", vsc); |
| 375 |
printf("max trace sc = %.4f nats\n", bestsc); |
| 376 |
|
| 377 |
free(dsq); |
| 378 |
p7_trace_Destroy(tr); |
| 379 |
p7_gmx_Destroy(gx); |
| 380 |
p7_omx_Destroy(fwd); |
| 381 |
p7_oprofile_Destroy(om); |
| 382 |
p7_profile_Destroy(gm); |
| 383 |
p7_bg_Destroy(bg); |
| 384 |
p7_hmm_Destroy(hmm); |
| 385 |
p7_hmmfile_Close(hfp); |
| 386 |
esl_alphabet_Destroy(abc); |
| 387 |
esl_stopwatch_Destroy(w); |
| 388 |
esl_randomness_Destroy(r); |
| 389 |
esl_getopts_Destroy(go); |
| 390 |
return 0; |
| 391 |
} |
| 392 |
#endif |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
#ifdef p7STOTRACE_TESTDRIVE |
| 400 |
#include "esl_getopts.h" |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
static void |
| 410 |
utest_stotrace(ESL_GETOPTS *go, ESL_RANDOMNESS *rng, ESL_ALPHABET *abc, P7_PROFILE *gm, P7_OPROFILE *om, ESL_DSQ *dsq, int L, int ntrace) |
| 411 |
{ |
| 412 |
P7_GMX *gx = NULL; |
| 413 |
P7_OMX *ox = NULL; |
| 414 |
P7_TRACE *vtr = NULL; |
| 415 |
P7_TRACE *tr = NULL; |
| 416 |
char errbuf[eslERRBUFSIZE]; |
| 417 |
int idx; |
| 418 |
float maxsc = -eslINFINITY; |
| 419 |
float vsc, sc; |
| 420 |
|
| 421 |
if ((gx = p7_gmx_Create(gm->M, L)) == NULL) esl_fatal("generic DP matrix creation failed"); |
| 422 |
if ((ox = p7_omx_Create(gm->M, L, L)) == NULL) esl_fatal("optimized DP matrix create failed"); |
| 423 |
if ((tr = p7_trace_Create()) == NULL) esl_fatal("trace creation failed"); |
| 424 |
if ((vtr = p7_trace_Create()) == NULL) esl_fatal("trace creation failed"); |
| 425 |
|
| 426 |
if (p7_GViterbi(dsq, L, gm, gx, &vsc) != eslOK) esl_fatal("viterbi failed"); |
| 427 |
if (p7_GTrace (dsq, L, gm, gx, vtr) != eslOK) esl_fatal("viterbi trace failed"); |
| 428 |
if (p7_Forward (dsq, L, om, ox, NULL) != eslOK) esl_fatal("forward failed"); |
| 429 |
|
| 430 |
for (idx = 0; idx < ntrace; idx++) |
| 431 |
{ |
| 432 |
if (p7_StochasticTrace(rng, dsq, L, om, ox, tr) != eslOK) esl_fatal("stochastic trace failed"); |
| 433 |
if (p7_trace_Validate(tr, abc, dsq, errbuf) != eslOK) esl_fatal("trace invalid:\n%s", errbuf); |
| 434 |
if (p7_trace_Score(tr, dsq, gm, &sc) != eslOK) esl_fatal("trace scoring failed"); |
| 435 |
|
| 436 |
maxsc = ESL_MAX(sc, maxsc); |
| 437 |
if (sc > vsc + 0.001){ |
| 438 |
|
| 439 |
|
| 440 |
esl_fatal("sampled trace has score > optimal Viterbi path; not possible (%f > %f)", sc, vsc); |
| 441 |
} |
| 442 |
p7_trace_Reuse(tr); |
| 443 |
} |
| 444 |
if (esl_FCompare_old(maxsc, vsc, 0.1) != eslOK) esl_fatal("stochastic trace failed to sample the Viterbi path"); |
| 445 |
|
| 446 |
p7_trace_Destroy(tr); |
| 447 |
p7_trace_Destroy(vtr); |
| 448 |
p7_omx_Destroy(ox); |
| 449 |
p7_gmx_Destroy(gx); |
| 450 |
} |
| 451 |
#endif |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
#ifdef p7STOTRACE_TESTDRIVE |
| 460 |
|
| 461 |
|
| 462 |
#include "easel.h" |
| 463 |
#include "esl_getopts.h" |
| 464 |
#include "esl_randomseq.h" |
| 465 |
|
| 466 |
#include <p7_config.h> |
| 467 |
#include "hmmer.h" |
| 468 |
|
| 469 |
static ESL_OPTIONS options[] = { |
| 470 |
|
| 471 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 472 |
{ "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 473 |
{ "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be verbose", 0 }, |
| 474 |
{ "--vv", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be very verbose", 0 }, |
| 475 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 476 |
}; |
| 477 |
static char usage[] = "[-options]"; |
| 478 |
static char banner[] = "unit test driver for stochastic Viterbi traceback (optimized version)"; |
| 479 |
|
| 480 |
int |
| 481 |
main(int argc, char **argv) |
| 482 |
{ |
| 483 |
ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 484 |
ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s")); |
| 485 |
ESL_ALPHABET *abc = NULL; |
| 486 |
P7_HMM *hmm = NULL; |
| 487 |
P7_PROFILE *gm = NULL; |
| 488 |
P7_OPROFILE *om = NULL; |
| 489 |
P7_BG *bg = NULL; |
| 490 |
ESL_DSQ *dsq = NULL; |
| 491 |
ESL_SQ *sq = NULL; |
| 492 |
int M = 6; |
| 493 |
int L = 10; |
| 494 |
int ntrace = 1000; |
| 495 |
|
| 496 |
if ((abc = esl_alphabet_Create(eslAMINO)) == NULL) esl_fatal("failed to create alphabet"); |
| 497 |
if (p7_hmm_Sample(r, M, abc, &hmm) != eslOK) esl_fatal("failed to sample an HMM"); |
| 498 |
if ((bg = p7_bg_Create(abc)) == NULL) esl_fatal("failed to create null model"); |
| 499 |
if ((gm = p7_profile_Create(hmm->M, abc)) == NULL) esl_fatal("failed to create profile"); |
| 500 |
if (p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL) != eslOK) esl_fatal("failed to config profile"); |
| 501 |
if ((om = p7_oprofile_Create(gm->M, abc)) == NULL) esl_fatal("failed to create optimized profile"); |
| 502 |
if (p7_oprofile_Convert(gm, om) != eslOK) esl_fatal("failed to convert profile"); |
| 503 |
|
| 504 |
|
| 505 |
if ((dsq = malloc(sizeof(ESL_DSQ) *(L+2))) == NULL) esl_fatal("malloc failed"); |
| 506 |
if (esl_rsq_xfIID(r, bg->f, abc->K, L, dsq) != eslOK) esl_fatal("seq generation failed"); |
| 507 |
utest_stotrace(go, r, abc, gm, om, dsq, L, ntrace); |
| 508 |
|
| 509 |
|
| 510 |
if ((sq = esl_sq_CreateDigital(abc)) == NULL) esl_fatal("sequence allocation failed"); |
| 511 |
if (p7_ProfileEmit(r, hmm, gm, bg, sq, NULL) != eslOK) esl_fatal("profile emission failed"); |
| 512 |
utest_stotrace(go, r, abc, gm, om, sq->dsq, sq->n, ntrace); |
| 513 |
|
| 514 |
esl_sq_Destroy(sq); |
| 515 |
free(dsq); |
| 516 |
p7_oprofile_Destroy(om); |
| 517 |
p7_profile_Destroy(gm); |
| 518 |
p7_bg_Destroy(bg); |
| 519 |
p7_hmm_Destroy(hmm); |
| 520 |
esl_alphabet_Destroy(abc); |
| 521 |
esl_randomness_Destroy(r); |
| 522 |
esl_getopts_Destroy(go); |
| 523 |
return 0; |
| 524 |
} |
| 525 |
#endif |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
#ifdef p7STOTRACE_EXAMPLE |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
#include <p7_config.h> |
| 539 |
|
| 540 |
#include "easel.h" |
| 541 |
#include "esl_alphabet.h" |
| 542 |
#include "esl_getopts.h" |
| 543 |
#include "esl_random.h" |
| 544 |
#include "esl_sq.h" |
| 545 |
#include "esl_sqio.h" |
| 546 |
|
| 547 |
#include "hmmer.h" |
| 548 |
#include "impl_sse.h" |
| 549 |
|
| 550 |
static ESL_OPTIONS options[] = { |
| 551 |
|
| 552 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 553 |
{ "-m", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "dump the DP matrix to stdout", 0 }, |
| 554 |
{ "-p", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "dump the profile to stdout", 0 }, |
| 555 |
{ "-t", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "dump the sampled trace to stdout", 0 }, |
| 556 |
{ "-N", eslARG_INT, "1", NULL, NULL, NULL, NULL, NULL, "number of traces to sample", 0 }, |
| 557 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 558 |
}; |
| 559 |
static char usage[] = "[-options] <hmmfile> <seqfile>"; |
| 560 |
static char banner[] = "example of stochastic backtrace (SSE version)"; |
| 561 |
|
| 562 |
int |
| 563 |
main(int argc, char **argv) |
| 564 |
{ |
| 565 |
ESL_GETOPTS *go = p7_CreateDefaultApp(options, 2, argc, argv, banner, usage); |
| 566 |
char *hmmfile = esl_opt_GetArg(go, 1); |
| 567 |
char *seqfile = esl_opt_GetArg(go, 2); |
| 568 |
ESL_ALPHABET *abc = NULL; |
| 569 |
ESL_RANDOMNESS *rng = esl_randomness_CreateFast(0); |
| 570 |
P7_HMMFILE *hfp = NULL; |
| 571 |
P7_HMM *hmm = NULL; |
| 572 |
P7_BG *bg = NULL; |
| 573 |
P7_PROFILE *gm = NULL; |
| 574 |
P7_OPROFILE *om = NULL; |
| 575 |
P7_GMX *gx = NULL; |
| 576 |
P7_OMX *fwd = NULL; |
| 577 |
P7_TRACE *tr = NULL; |
| 578 |
ESL_SQ *sq = NULL; |
| 579 |
ESL_SQFILE *sqfp = NULL; |
| 580 |
int format = eslSQFILE_UNKNOWN; |
| 581 |
int N = esl_opt_GetInteger(go, "-N"); |
| 582 |
int i; |
| 583 |
float vsc, fsc, tsc; |
| 584 |
char errbuf[eslERRBUFSIZE]; |
| 585 |
int status; |
| 586 |
|
| 587 |
|
| 588 |
if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile); |
| 589 |
if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM"); |
| 590 |
|
| 591 |
|
| 592 |
sq = esl_sq_CreateDigital(abc); |
| 593 |
status = esl_sqfile_Open(seqfile, format, NULL, &sqfp); |
| 594 |
if (status == eslENOTFOUND) p7_Fail("No such file."); |
| 595 |
else if (status == eslEFORMAT) p7_Fail("Format unrecognized."); |
| 596 |
else if (status == eslEINVAL) p7_Fail("Can't autodetect stdin or .gz."); |
| 597 |
else if (status != eslOK) p7_Fail("Open failed, code %d.", status); |
| 598 |
if (esl_sqio_Read(sqfp, sq) != eslOK) p7_Fail("Failed to read sequence"); |
| 599 |
|
| 600 |
|
| 601 |
bg = p7_bg_Create(abc); p7_bg_SetLength(bg, sq->n); |
| 602 |
gm = p7_profile_Create(hmm->M, abc); p7_ProfileConfig(hmm, bg, gm, sq->n, p7_LOCAL); |
| 603 |
om = p7_oprofile_Create(gm->M, abc); p7_oprofile_Convert(gm, om); |
| 604 |
|
| 605 |
if (esl_opt_GetBoolean(go, "-p")) p7_oprofile_Dump(stdout, om); |
| 606 |
|
| 607 |
fwd = p7_omx_Create(gm->M, sq->n, sq->n); |
| 608 |
gx = p7_gmx_Create(gm->M, sq->n); |
| 609 |
tr = p7_trace_Create(); |
| 610 |
|
| 611 |
if (esl_opt_GetBoolean(go, "-m") == TRUE) p7_omx_SetDumpMode(stdout, fwd, TRUE); |
| 612 |
p7_GViterbi(sq->dsq, sq->n, gm, gx, &vsc); |
| 613 |
p7_Forward (sq->dsq, sq->n, om, fwd, &fsc); |
| 614 |
|
| 615 |
for (i = 0; i < N; i++) |
| 616 |
{ |
| 617 |
p7_StochasticTrace(rng, sq->dsq, sq->n, om, fwd, tr); |
| 618 |
p7_trace_Score(tr, sq->dsq, gm, &tsc); |
| 619 |
|
| 620 |
if (esl_opt_GetBoolean(go, "-t") == TRUE) p7_trace_Dump(stdout, tr, gm, sq->dsq); |
| 621 |
if (p7_trace_Validate(tr, abc, sq->dsq, errbuf) != eslOK) p7_Die("trace %d fails validation:\n%s\n", i, errbuf); |
| 622 |
|
| 623 |
printf("Sampled trace: %.4f nats\n", tsc); |
| 624 |
p7_trace_Reuse(tr); |
| 625 |
} |
| 626 |
printf("Forward score: %.4f nats\n", fsc); |
| 627 |
printf("Viterbi score: %.4f nats\n", vsc); |
| 628 |
|
| 629 |
|
| 630 |
esl_sq_Destroy(sq); |
| 631 |
esl_sqfile_Close(sqfp); |
| 632 |
p7_trace_Destroy(tr); |
| 633 |
p7_omx_Destroy(fwd); |
| 634 |
p7_gmx_Destroy(gx); |
| 635 |
p7_oprofile_Destroy(om); |
| 636 |
p7_profile_Destroy(gm); |
| 637 |
p7_bg_Destroy(bg); |
| 638 |
p7_hmm_Destroy(hmm); |
| 639 |
p7_hmmfile_Close(hfp); |
| 640 |
esl_randomness_Destroy(rng); |
| 641 |
esl_alphabet_Destroy(abc); |
| 642 |
esl_getopts_Destroy(go); |
| 643 |
return 0; |
| 644 |
} |
| 645 |
#endif |
| 646 |
|
| 647 |
|
| 648 |
|