Debian Codegraph @

hmmer/src/impl_vmx/stotrace.c

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