Debian Codegraph @

hmmer/src/generic_optacc.c

raw symbols SCIP
1 /* Optimal accuracy alignment; generic version.
2 *
3 * Contents:
4 * 1. Optimal alignment accuracy fill.
5 * 2. Optimal alignment accuracy traceback.
6 * 3. Benchmark driver
7 * 4. Unit tests
8 * 5. Test driver
9 * 6. Example
10 *
11 * SRE, Fri Feb 29 12:48:46 2008 [Janelia]
12 */
13 #include <p7_config.h>
14
15 #include <float.h>
16
17 #include "easel.h"
18 #include "esl_vectorops.h"
19
20 #include "hmmer.h"
21
22 /*****************************************************************
23 * 1. Optimal alignment fill and traceback.
24 *****************************************************************/
25
26 #define MMX(i,k) (dp[(i)][(k) * p7G_NSCELLS + p7G_M])
27 #define IMX(i,k) (dp[(i)][(k) * p7G_NSCELLS + p7G_I])
28 #define DMX(i,k) (dp[(i)][(k) * p7G_NSCELLS + p7G_D])
29 #define XMX(i,s) (xmx[(i) * p7G_NXCELLS + (s)])
30 #define TSCDELTA(s,k) ( (tsc[(k) * p7P_NTRANS + (s)] == -eslINFINITY) ? FLT_MIN : 1.0)
31
32 /* The TSCDELTA is used to make impossible paths impossible in the
33 * optimal accuracy decoding algorithm; see Kall et al (2005). What we
34 * want to do is multiply by a Kronecker delta that's 1 when the
35 * transition probability is finite, and 0 when it's zero (when the
36 * log prob is -eslINFINITY). But we can't do that easily, when we're
37 * in log space, because 0 * -eslINFINITY = NaN. Instead, we use a
38 * tiny number (FLT_MIN, ~1e-37).
39 *
40 * A side concern is that we don't want to put a bunch of if-else
41 * branches in the code; compilers should be able to generate more
42 * efficient code from the TSCDELTA() construction.
43 */
44
45
46 /* Function: p7_GOptimalAccuracy()
47 * Synopsis: Optimal accuracy decoding: fill.
48 * Incept: SRE, Fri Feb 29 11:56:49 2008 [Janelia]
49 *
50 * Purpose: Calculates the fill step of the optimal accuracy decoding
51 * algorithm \citep{Kall05}.
52 *
53 * Caller provides the posterior decoding matrix <pp>,
54 * which was calculated by Forward/Backward on a target sequence
55 * of length <L> using the query model <gm>.
56 *
57 * Caller also provides a DP matrix <gx>, allocated for the
58 * <gm->M> by <pp->L> comparison. The routine fills this in
59 * with OA scores.
60 *
61 * Args: gm - query profile
62 * pp - posterior decoding matrix created by <p7_GPosteriorDecoding()>
63 * gx - RESULT: caller provided DP matrix for <gm->M> by <L>
64 * ret_e - RETURN: expected number of correctly decoded positions
65 *
66 * Returns: <eslOK> on success, and <*ret_e> contains the final OA
67 * score, which is the expected number of correctly decoded
68 * positions in the target sequence (up to <L>).
69 *
70 * Throws: (no abnormal error conditions)
71 */
72 int
73 p7_GOptimalAccuracy(const P7_PROFILE *gm, const P7_GMX *pp, P7_GMX *gx, float *ret_e)
74 {
75 int L = pp->L;
76 float **dp = gx->dp;
77 float *xmx = gx->xmx;
78 float const *tsc = gm->tsc;
79 int i,k;
80 int M = gm->M;
81 float esc = p7_profile_IsLocal(gm) ? 1.0 : 0.0;
82 float t1, t2;
83
84 /* Initialization of the zero row (i=0; no residues to account for. */
85 XMX(0,p7G_N) = 0.; /* S->N, p=1 */
86 XMX(0,p7G_B) = 0.; /* S->N->B, no N-tail */
87 XMX(0,p7G_E) = XMX(0,p7G_C) = XMX(0,p7G_J) = -eslINFINITY; /* need seq to get here */
88 for (k = 0; k <= M; k++)
89 MMX(0,k) = IMX(0,k) = DMX(0,k) = -eslINFINITY; /* need seq to get here */
90
91 for (i = 1; i <= L; i++)
92 {
93 MMX(i,0) = IMX(i,0) = DMX(i,0) = XMX(i,p7G_E) = -eslINFINITY;
94
95 for (k = 1; k < M; k++)
96 {
97 MMX(i,k) = ESL_MAX(ESL_MAX(TSCDELTA(p7P_MM, k-1) * (MMX(i-1,k-1) + pp->dp[i][k*p7G_NSCELLS + p7G_M]),
98 TSCDELTA(p7P_IM, k-1) * (IMX(i-1,k-1) + pp->dp[i][k*p7G_NSCELLS + p7G_M])),
99 ESL_MAX(TSCDELTA(p7P_DM, k-1) * (DMX(i-1,k-1) + pp->dp[i][k*p7G_NSCELLS + p7G_M]),
100 TSCDELTA(p7P_BM, k-1) * (XMX(i-1,p7G_B)+ pp->dp[i][k*p7G_NSCELLS + p7G_M])));
101
102 XMX(i,p7G_E) = ESL_MAX(XMX(i,p7G_E),
103 esc * MMX(i,k));
104
105 IMX(i,k) = ESL_MAX(TSCDELTA(p7P_MI, k) * (MMX(i-1,k) + pp->dp[i][k*p7G_NSCELLS + p7G_I]),
106 TSCDELTA(p7P_II, k) * (IMX(i-1,k) + pp->dp[i][k*p7G_NSCELLS + p7G_I]));
107
108 DMX(i,k) = ESL_MAX(TSCDELTA(p7P_MD, k-1) * MMX(i,k-1),
109 TSCDELTA(p7P_DD, k-1) * DMX(i,k-1));
110 }
111
112 /* last node (k=M) is unrolled; it has no I state, and it has a p=1.0 {MD}->E transition even in local mode */
113 MMX(i,M) = ESL_MAX(ESL_MAX(TSCDELTA(p7P_MM, M-1) * (MMX(i-1,M-1) + pp->dp[i][M*p7G_NSCELLS + p7G_M]),
114 TSCDELTA(p7P_IM, M-1) * (IMX(i-1,M-1) + pp->dp[i][M*p7G_NSCELLS + p7G_M])),
115 ESL_MAX(TSCDELTA(p7P_DM, M-1) * (DMX(i-1,M-1) + pp->dp[i][M*p7G_NSCELLS + p7G_M]),
116 TSCDELTA(p7P_BM, M-1) * (XMX(i-1,p7G_B)+ pp->dp[i][M*p7G_NSCELLS + p7G_M])));
117
118 DMX(i,M) = ESL_MAX(TSCDELTA(p7P_MD, M-1) * MMX(i,M-1),
119 TSCDELTA(p7P_DD, M-1) * DMX(i,M-1));
120
121 /* note: we calculated XMX before DMX in the loop, because we probably had MMX(i,k) in a register.
122 * but now we can't do that, because XMX depends on DMX
123 */
124 XMX(i,p7G_E) = ESL_MAX(XMX(i,p7G_E), ESL_MAX(MMX(i,M), DMX(i, M)));
125
126 /* now the special states; it's important that E is already done, and B is done after N,J */
127 t1 = ( (gm->xsc[p7P_J][p7P_LOOP] == -eslINFINITY) ? FLT_MIN : 1.0);
128 t2 = ( (gm->xsc[p7P_E][p7P_LOOP] == -eslINFINITY) ? FLT_MIN : 1.0);
129 XMX(i, p7G_J) = ESL_MAX( t1 * (XMX(i-1,p7G_J) + pp->xmx[i*p7G_NXCELLS + p7G_J]),
130 t2 * XMX(i, p7G_E));
131
132 t1 = ( (gm->xsc[p7P_C][p7P_LOOP] == -eslINFINITY) ? FLT_MIN : 1.0);
133 t2 = ( (gm->xsc[p7P_E][p7P_MOVE] == -eslINFINITY) ? FLT_MIN : 1.0);
134 XMX(i,p7G_C) = ESL_MAX( t1 * (XMX(i-1,p7G_C) + pp->xmx[i*p7G_NXCELLS + p7G_C]),
135 t2 * XMX(i, p7G_E));
136
137 t1 = ( (gm->xsc[p7P_N][p7P_LOOP] == -eslINFINITY) ? FLT_MIN : 1.0);
138 XMX(i,p7G_N) = t1 * (XMX(i-1,p7G_N) + pp->xmx[i*p7G_NXCELLS + p7G_N]);
139
140 t1 = ( (gm->xsc[p7P_N][p7P_MOVE] == -eslINFINITY) ? FLT_MIN : 1.0);
141 t2 = ( (gm->xsc[p7P_J][p7P_MOVE] == -eslINFINITY) ? FLT_MIN : 1.0);
142 XMX(i,p7G_B) = ESL_MAX( t1 * XMX(i, p7G_N),
143 t2 * XMX(i, p7G_J));
144 }
145
146 *ret_e = XMX(L,p7G_C);
147 return eslOK;
148 }
149 /*---------------------- end, oa fill ---------------------------*/
150
151 /*****************************************************************
152 * 2. Optimal alignment accuracy, traceback
153 *****************************************************************/
154
155 static inline float get_postprob(const P7_GMX *pp, int scur, int sprv, int k, int i);
156
157 static inline int select_m(const P7_PROFILE *gm, const P7_GMX *gx, int i, int k);
158 static inline int select_d(const P7_PROFILE *gm, const P7_GMX *gx, int i, int k);
159 static inline int select_i(const P7_PROFILE *gm, const P7_GMX *gx, int i, int k);
160 static inline int select_n(int i);
161 static inline int select_c(const P7_PROFILE *gm, const P7_GMX *pp, const P7_GMX *gx, int i);
162 static inline int select_j(const P7_PROFILE *gm, const P7_GMX *pp, const P7_GMX *gx, int i);
163 static inline int select_e(const P7_PROFILE *gm, const P7_GMX *gx, int i, int *ret_k);
164 static inline int select_b(const P7_PROFILE *gm, const P7_GMX *gx, int i);
165
166
167 /* Function: p7_GOATrace()
168 * Synopsis: Optimal accuracy decoding: traceback.
169 * Incept: SRE, Fri Feb 29 12:59:11 2008 [Janelia]
170 *
171 * Purpose: The traceback stage of the optimal accuracy decoding algorithm
172 * \citep{Kall05}.
173 *
174 * Caller provides the OA DP matrix <gx> that was just
175 * calculated by <p7_GOptimalAccuracy()>, as well as the
176 * posterior decoding matrix <pp>, which was calculated by
177 * Forward/Backward on a target sequence of length <L>
178 * using the query model <gm>.
179 *
180 * Caller provides an empty traceback structure <tr> to
181 * hold the result, allocated to hold optional posterior
182 * probability annotation on residues (with
183 * <p7_trace_CreateWithPP()>, generally). This will be
184 * internally reallocated as needed for larger traces.
185 *
186 * Args: gm - query profile
187 * pp - posterior decoding matrix created by <p7_PosteriorDecoding()>
188 * gx - OA DP matrix calculated by <p7_OptimalAccuracyDP()>
189 * tr - RESULT: OA traceback, allocated with posterior probs
190 *
191 * Returns: <eslOK> on success, and <tr> contains the OA traceback.
192 *
193 * Throws: <eslEMEM> on allocation error.
194 */
195 int
196 p7_GOATrace(const P7_PROFILE *gm, const P7_GMX *pp, const P7_GMX *gx, P7_TRACE *tr)
197 {
198 int i = gx->L; /* position in seq (1..L) */
199 int k = 0; /* position in model (1..M) */
200 float postprob;
201 int sprv, scur;
202 int status;
203
204 #if eslDEBUGLEVEL > 0
205 if (tr->N != 0) ESL_EXCEPTION(eslEINVAL, "trace isn't empty: forgot to Reuse()?");
206 #endif
207
208 if ((status = p7_trace_AppendWithPP(tr, p7T_T, k, i, 0.0)) != eslOK) return status;
209 if ((status = p7_trace_AppendWithPP(tr, p7T_C, k, i, 0.0)) != eslOK) return status;
210
211 sprv = p7T_C;
212 while (sprv != p7T_S)
213 {
214 switch (sprv) {
215 case p7T_M: scur = select_m(gm, gx, i, k); k--; i--; break;
216 case p7T_D: scur = select_d(gm, gx, i, k); k--; break;
217 case p7T_I: scur = select_i(gm, gx, i, k); i--; break;
218 case p7T_N: scur = select_n(i); break;
219 case p7T_C: scur = select_c(gm, pp, gx, i); break;
220 case p7T_J: scur = select_j(gm, pp, gx, i); break;
221 case p7T_E: scur = select_e(gm, gx, i, &k); break;
222 case p7T_B: scur = select_b(gm, gx, i); break;
223 default: ESL_EXCEPTION(eslEINVAL, "bogus state in traceback");
224 }
225 if (scur == -1) ESL_EXCEPTION(eslEINVAL, "OA traceback choice failed");
226
227 postprob = get_postprob(pp, scur, sprv, k, i);
228 if ((status = p7_trace_AppendWithPP(tr, scur, k, i, postprob)) != eslOK) return status;
229
230 /* For NCJ, we had to defer i decrement. */
231 if ( (scur == p7T_N || scur == p7T_J || scur == p7T_C) && scur == sprv) i--;
232 sprv = scur;
233 }
234 tr->M = gm->M;
235 tr->L = gx->L;
236 return p7_trace_Reverse(tr);
237 }
238
239 static inline float
240 get_postprob(const P7_GMX *pp, int scur, int sprv, int k, int i)
241 {
242 float **dp = pp->dp;
243 float *xmx = pp->xmx;
244
245 switch (scur) {
246 case p7T_M: return MMX(i,k);
247 case p7T_I: return IMX(i,k);
248 case p7T_N: if (sprv == scur) return XMX(i,p7G_N);
249 case p7T_C: if (sprv == scur) return XMX(i,p7G_C);
250 case p7T_J: if (sprv == scur) return XMX(i,p7G_J);
251 default: return 0.0;
252 }
253 }
254
255 static inline int
256 select_m(const P7_PROFILE *gm, const P7_GMX *gx, int i, int k)
257 {
258 float **dp = gx->dp; /* so {MDI}MX() macros work */
259 float *xmx = gx->xmx; /* so XMX() macro works */
260 float const *tsc = gm->tsc; /* so TSCDELTA() macro works */
261 float path[4];
262 int state[4] = { p7T_M, p7T_I, p7T_D, p7T_B };
263
264 path[0] = TSCDELTA(p7P_MM, k-1) * MMX(i-1,k-1);
265 path[1] = TSCDELTA(p7P_IM, k-1) * IMX(i-1,k-1);
266 path[2] = TSCDELTA(p7P_DM, k-1) * DMX(i-1,k-1);
267 path[3] = TSCDELTA(p7P_BM, k-1) * XMX(i-1,p7G_B);
268 return state[esl_vec_FArgMax(path, 4)];
269 }
270
271 static inline int
272 select_d(const P7_PROFILE *gm, const P7_GMX *gx, int i, int k)
273 {
274 float **dp = gx->dp; /* so {MDI}MX() macros work */
275 float const *tsc = gm->tsc; /* so TSCDELTA() macro works */
276 float path[2];
277
278 path[0] = TSCDELTA(p7P_MD, k-1) * MMX(i, k-1);
279 path[1] = TSCDELTA(p7P_DD, k-1) * DMX(i, k-1);
280 return ((path[0] >= path[1]) ? p7T_M : p7T_D);
281 }
282
283 static inline int
284 select_i(const P7_PROFILE *gm, const P7_GMX *gx, int i, int k)
285 {
286 float **dp = gx->dp; /* so {MDI}MX() macros work */
287 float const *tsc = gm->tsc; /* so TSCDELTA() macro works */
288 float path[2];
289
290 path[0] = TSCDELTA(p7P_MI, k) * MMX(i-1,k);
291 path[1] = TSCDELTA(p7P_II, k) * IMX(i-1,k);
292 return ((path[0] >= path[1]) ? p7T_M : p7T_I);
293 }
294
295 static inline int
296 select_n(int i)
297 {
298 return ((i==0) ? p7T_S : p7T_N);
299 }
300
301 static inline int
302 select_c(const P7_PROFILE *gm, const P7_GMX *pp, const P7_GMX *gx, int i)
303 {
304 float t1 = ( (gm->xsc[p7P_C][p7P_LOOP] == -eslINFINITY) ? FLT_MIN : 1.0);
305 float t2 = ( (gm->xsc[p7P_E][p7P_MOVE] == -eslINFINITY) ? FLT_MIN : 1.0);
306 float *xmx = gx->xmx; /* so XMX() macro works */
307 float path[2];
308
309 path[0] = t1 * (XMX(i-1, p7G_C) + pp->xmx[i*p7G_NXCELLS + p7G_C]);
310 path[1] = t2 * XMX(i,p7G_E);
311 return ((path[0] > path[1]) ? p7T_C : p7T_E);
312 }
313
314 static inline int
315 select_j(const P7_PROFILE *gm, const P7_GMX *pp, const P7_GMX *gx, int i)
316 {
317 float t1 = ( (gm->xsc[p7P_J][p7P_LOOP] == -eslINFINITY) ? FLT_MIN : 1.0);
318 float t2 = ( (gm->xsc[p7P_E][p7P_LOOP] == -eslINFINITY) ? FLT_MIN : 1.0);
319 float *xmx = gx->xmx; /* so XMX() macro works */
320 float path[2];
321
322 path[0] = t1 * (XMX(i-1,p7G_J) + pp->xmx[i*p7G_NXCELLS + p7G_J]);
323 path[1] = t2 * XMX(i,p7G_E);
324 return ((path[0] > path[1]) ? p7T_J : p7T_E);
325 }
326
327 static inline int
328 select_e(const P7_PROFILE *gm, const P7_GMX *gx, int i, int *ret_k)
329 {
330 float **dp = gx->dp; /* so {MDI}MX() macros work */
331 float max = -eslINFINITY;
332 int smax = -1; /* will be returned as "error code" if no max found */
333 int kmax = -1;
334 int k;
335
336 if (! p7_profile_IsLocal(gm)) /* glocal/global is easier */
337 {
338 *ret_k = gm->M;
339 return ((MMX(i,gm->M) >= DMX(i,gm->M)) ? p7T_M : p7T_D);
340 }
341
342 for (k = 1; k <= gm->M; k++)
343 {
344 if (MMX(i,k) >= max) { max = MMX(i,k); smax = p7T_M; kmax = k; }
345 if (DMX(i,k) > max) { max = DMX(i,k); smax = p7T_D; kmax = k; }
346 }
347 *ret_k = kmax;
348 return smax;
349 }
350
351 static inline int
352 select_b(const P7_PROFILE *gm, const P7_GMX *gx, int i)
353 {
354 float t1 = ( (gm->xsc[p7P_N][p7P_MOVE] == -eslINFINITY) ? FLT_MIN : 1.0);
355 float t2 = ( (gm->xsc[p7P_J][p7P_MOVE] == -eslINFINITY) ? FLT_MIN : 1.0);
356 float *xmx = gx->xmx; /* so XMX() macro works */
357 float path[2];
358
359 path[0] = t1 * XMX(i, p7G_N);
360 path[1] = t2 * XMX(i, p7G_J);
361 return ((path[0] > path[1]) ? p7T_N : p7T_J);
362 }
363
364
365
366 /*------------------------ end, oa traceback --------------------*/
367
368
369
370
371 /*****************************************************************
372 * 3. Benchmark driver
373 *****************************************************************/
374 #ifdef p7GENERIC_OPTACC_BENCHMARK
375 /*
376 icc -O3 -static -o generic_optacc_benchmark -I. -L. -I../easel -L../easel -Dp7GENERIC_OPTACC_BENCHMARK generic_optacc.c -lhmmer -leasel -lm
377 ./benchmark-generic-optacc <hmmfile>
378 RRM_1 (M=72) Caudal_act (M=136) SMC_N (M=1151)
379 ----------------- ------------------ -------------------
380 20 Aug 08: 67.96u (21.2 Mc/s) 128.14u (21.2 Mc/s) 1091.90u (21.1 Mc/s)
381 */
382 #include <p7_config.h>
383
384 #include "easel.h"
385 #include "esl_alphabet.h"
386 #include "esl_getopts.h"
387 #include "esl_random.h"
388 #include "esl_randomseq.h"
389 #include "esl_stopwatch.h"
390
391 #include "hmmer.h"
392
393 static ESL_OPTIONS options[] = {
394 /* name type default env range toggles reqs incomp help docgroup*/
395 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
396 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
397 { "-L", eslARG_INT, "400", NULL, "n>0", NULL, NULL, NULL, "length of random target seqs", 0 },
398 { "-N", eslARG_INT, "5000", NULL, "n>0", NULL, NULL, NULL, "number of random target seqs", 0 },
399 { "--notrace", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "only benchmark the DP fill stage", 0 },
400 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
401 };
402 static char usage[] = "[-options] <hmmfile>";
403 static char banner[] = "benchmark driver for optimal accuracy alignment, generic version";
404
405 int
406 main(int argc, char **argv)
407 {
408 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
409 char *hmmfile = esl_opt_GetArg(go, 1);
410 ESL_STOPWATCH *w = esl_stopwatch_Create();
411 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
412 ESL_ALPHABET *abc = NULL;
413 P7_HMMFILE *hfp = NULL;
414 P7_HMM *hmm = NULL;
415 P7_BG *bg = NULL;
416 P7_PROFILE *gm = NULL;
417 P7_GMX *gx1 = NULL;
418 P7_GMX *gx2 = NULL;
419 P7_TRACE *tr = NULL;
420 int L = esl_opt_GetInteger(go, "-L");
421 int N = esl_opt_GetInteger(go, "-N");
422 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2));
423 int i;
424 float fsc, bsc, accscore;
425 double Mcs;
426
427 p7_FLogsumInit();
428
429 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
430 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
431
432 bg = p7_bg_Create(abc);
433 p7_bg_SetLength(bg, L);
434 gm = p7_profile_Create(hmm->M, abc);
435 p7_ProfileConfig(hmm, bg, gm, L, p7_UNILOCAL);
436 gx1 = p7_gmx_Create(gm->M, L);
437 gx2 = p7_gmx_Create(gm->M, L);
438 tr = p7_trace_CreateWithPP();
439
440 esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
441 p7_GForward (dsq, L, gm, gx1, &fsc);
442 p7_GBackward(dsq, L, gm, gx2, &bsc);
443 p7_GDecoding(gm, gx1, gx2, gx2); /* <gx2> is now the posterior decoding matrix */
444
445 esl_stopwatch_Start(w);
446 for (i = 0; i < N; i++)
447 {
448 p7_GOptimalAccuracy(gm, gx2, gx1, &accscore); /* <gx1> is now the OA matrix */
449
450 if (! esl_opt_GetBoolean(go, "--notrace"))
451 {
452 p7_GOATrace(gm, gx2, gx1, tr);
453 p7_trace_Reuse(tr);
454 }
455 }
456 esl_stopwatch_Stop(w);
457 Mcs = (double) N * (double) L * (double) gm->M * 1e-6 / w->user;
458 esl_stopwatch_Display(stdout, w, "# CPU time: ");
459 printf("# M = %d\n", gm->M);
460 printf("# %.1f Mc/s\n", Mcs);
461
462 free(dsq);
463 p7_trace_Destroy(tr);
464 p7_gmx_Destroy(gx1);
465 p7_gmx_Destroy(gx2);
466 p7_profile_Destroy(gm);
467 p7_bg_Destroy(bg);
468 p7_hmm_Destroy(hmm);
469 p7_hmmfile_Close(hfp);
470 esl_alphabet_Destroy(abc);
471 esl_stopwatch_Destroy(w);
472 esl_randomness_Destroy(r);
473 esl_getopts_Destroy(go);
474 return 0;
475 }
476 #endif /*p7GENERIC_OPTACC_BENCHMARK*/
477 /*---------------- end, benchmark driver ------------------------*/
478
479
480 /*****************************************************************
481 * 4. Unit tests
482 *****************************************************************/
483 #ifdef p7GENERIC_OPTACC_TESTDRIVE
484
485 #endif /*p7GENERIC_OPTACC_TESTDRIVE*/
486 /*------------------- end, unit tests ---------------------------*/
487
488
489 /*****************************************************************
490 * 5. Test driver
491 *****************************************************************/
492 #ifdef p7GENERIC_OPTACC_TESTDRIVE
493
494 #endif /*p7GENERIC_OPTACC_TESTDRIVE*/
495 /*------------------ end, test driver ---------------------------*/
496
497
498
499
500 /*****************************************************************
501 * 6. Example
502 *****************************************************************/
503 #ifdef p7GENERIC_OPTACC_EXAMPLE
504 /*
505 gcc -g -Wall -o generic_optacc_example -Dp7GENERIC_OPTACC_EXAMPLE -I. -I../easel -L. -L../easel generic_optacc.c -lhmmer -leasel -lm
506 ./generic_optacc_example <hmmfile> <seqfile>
507 */
508 #include <p7_config.h>
509
510 #include "easel.h"
511 #include "esl_alphabet.h"
512 #include "esl_getopts.h"
513 #include "esl_sq.h"
514 #include "esl_sqio.h"
515
516 #include "hmmer.h"
517
518 static ESL_OPTIONS options[] = {
519 /* name type default env range toggles reqs incomp help docgroup*/
520 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
521 { "-d", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "dump posterior residue decoding matrix", 0 },
522 { "-m", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "dump OA matrix", 0 },
523 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
524 };
525 static char usage[] = "[-options] <hmmfile> <seqfile>";
526 static char banner[] = "example of optimal accuracy alignment, generic implementation";
527
528 int
529 main(int argc, char **argv)
530 {
531 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 2, argc, argv, banner, usage);
532 char *hmmfile = esl_opt_GetArg(go, 1);
533 char *seqfile = esl_opt_GetArg(go, 2);
534 ESL_ALPHABET *abc = NULL;
535 P7_HMMFILE *hfp = NULL;
536 P7_HMM *hmm = NULL;
537 P7_BG *bg = NULL;
538 P7_PROFILE *gm = NULL;
539 P7_GMX *gx1 = NULL;
540 P7_GMX *gx2 = NULL;
541 ESL_SQ *sq = NULL;
542 ESL_SQFILE *sqfp = NULL;
543 P7_TRACE *tr = NULL;
544 int format = eslSQFILE_UNKNOWN;
545 char errbuf[eslERRBUFSIZE];
546 float fsc, bsc, vsc;
547 float accscore;
548 int status;
549
550 /* Read in one HMM */
551 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
552 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
553 p7_hmmfile_Close(hfp);
554
555 /* Read in one sequence */
556 sq = esl_sq_CreateDigital(abc);
557 status = esl_sqfile_OpenDigital(abc, seqfile, format, NULL, &sqfp);
558 if (status == eslENOTFOUND) p7_Fail("No such file.");
559 else if (status == eslEFORMAT) p7_Fail("Format unrecognized.");
560 else if (status == eslEINVAL) p7_Fail("Can't autodetect stdin or .gz.");
561 else if (status != eslOK) p7_Fail("Open failed, code %d.", status);
562 if (esl_sqio_Read(sqfp, sq) != eslOK) p7_Fail("Failed to read sequence");
563 esl_sqfile_Close(sqfp);
564
565 /* Configure a profile from the HMM */
566 bg = p7_bg_Create(abc);
567 p7_bg_SetLength(bg, sq->n);
568 gm = p7_profile_Create(hmm->M, abc);
569 p7_ProfileConfig(hmm, bg, gm, sq->n, p7_LOCAL); /* multihit local: H3 default */
570
571 /* Allocations */
572 gx1 = p7_gmx_Create(gm->M, sq->n);
573 gx2 = p7_gmx_Create(gm->M, sq->n);
574 tr = p7_trace_CreateWithPP();
575 p7_FLogsumInit();
576
577 /* Run Forward, Backward; do OA fill and trace */
578 p7_GForward (sq->dsq, sq->n, gm, gx1, &fsc);
579 p7_GBackward(sq->dsq, sq->n, gm, gx2, &bsc);
580 p7_GDecoding(gm, gx1, gx2, gx2); /* <gx2> is now the posterior decoding matrix */
581 p7_GOptimalAccuracy(gm, gx2, gx1, &accscore); /* <gx1> is now the OA matrix */
582 p7_GOATrace(gm, gx2, gx1, tr);
583
584 if (esl_opt_GetBoolean(go, "-d")) p7_gmx_Dump(stdout, gx2, p7_DEFAULT);
585 if (esl_opt_GetBoolean(go, "-m")) p7_gmx_Dump(stdout, gx1, p7_DEFAULT);
586
587 p7_trace_Dump(stdout, tr, gm, sq->dsq);
588 if (p7_trace_Validate(tr, abc, sq->dsq, errbuf) != eslOK) p7_Die("trace fails validation:\n%s\n", errbuf);
589
590 printf("fwd = %.4f nats\n", fsc);
591 printf("bck = %.4f nats\n", bsc);
592 printf("acc = %.4f (%.2f%%)\n", accscore, accscore * 100. / (float) sq->n);
593
594 p7_trace_Reuse(tr);
595
596 p7_GViterbi(sq->dsq, sq->n, gm, gx1, &vsc);
597 p7_GTrace (sq->dsq, sq->n, gm, gx1, tr);
598 p7_trace_SetPP(tr, gx2);
599 p7_trace_Dump(stdout, tr, gm, sq->dsq);
600
601 printf("vit = %.4f nats\n", vsc);
602 printf("acc = %.4f\n", p7_trace_GetExpectedAccuracy(tr));
603
604 /* Cleanup */
605 esl_sq_Destroy(sq);
606 p7_trace_Destroy(tr);
607 p7_gmx_Destroy(gx1);
608 p7_gmx_Destroy(gx2);
609 p7_profile_Destroy(gm);
610 p7_bg_Destroy(bg);
611 p7_hmm_Destroy(hmm);
612 esl_alphabet_Destroy(abc);
613 esl_getopts_Destroy(go);
614 return 0;
615 }
616 #endif /*p7GENERIC_OPTACC_EXAMPLE*/
617 /*-------------------- end, example -----------------------------*/
618
619
620
621
622
623