Debian Codegraph @

hmmer/src/impl_neon/stotrace.c

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