Debian Codegraph @

hmmer/src/generic_fwdback_chk.c

raw symbols SCIP
1 /* Forward/Backward, checkpointed: generic (non-SIMD) demonstration version.
2 *
3 * Contents:
4 * 1. Forwards: checkpointed fill, Forwards nat score.
5 * 2. Backwards: linear-memory back pass, recovering posterior-decoded bands.
6 * 3. Benchmark driver.
7 * 4. Example main().
8 * 5. References.
9 */
10 #include <p7_config.h>
11
12 #include "easel.h"
13
14 #include "hmmer.h"
15 #include "p7_gmxchk.h"
16 #include "p7_gbands.h"
17
18 static inline void posterior_decode_row(int rowi, float *fwd, float *bck, int M, float overall_sc, P7_GBANDS *bnd);
19
20 /*****************************************************************
21 *= 1. Forwards: checkpointed fill, Forwards nat score
22 *****************************************************************/
23
24 /* forward_row()
25 *
26 * inlined function, because we have to call the row calculation in
27 * several places in a checkpointed calculation, including in the Backwards()
28 * routine.
29 *
30 */
31 static inline void
32 forward_row(const ESL_DSQ *dsq, const P7_PROFILE *gm, P7_GMXCHK *gxc, const float *dpp, float *dpc, int i)
33 {
34 const float *tsc = gm->tsc;
35 const float *rsc = gm->rsc[dsq[i]] + p7P_NR; /* +NR skips _0, ahead to _1 */
36 int M = gm->M;
37 float esc = p7_profile_IsLocal(gm) ? 0. : -eslINFINITY;
38 int k;
39 float sc, dc;
40 float xB = *(dpp + (M+1)*p7G_NSCELLS + p7GC_B);
41 float xE = -eslINFINITY;
42 float mvp, ivp, dvp;
43
44 *dpc++ = -eslINFINITY; /* M_0 */
45 *dpc++ = -eslINFINITY; /* I_0 */
46 *dpc++ = -eslINFINITY; /* D_0 */
47 dc = -eslINFINITY;
48
49 mvp = *dpp++; ivp = *dpp++; dvp = *dpp++;
50
51 for (k = 1; k < M; k++)
52 {
53 /* match state */
54 *dpc++ = sc = *rsc++ /* emission(M_k, x_i) */
55 + p7_FLogsum(p7_FLogsum( mvp + *(tsc+p7P_MM), /* M(i-1,k-1) * t_M(k-1)->M(k) */
56 ivp + *(tsc+p7P_IM)), /* I(i-1,k-1) * t_I(k-1)->M(k) */
57 p7_FLogsum( dvp + *(tsc+p7P_DM), /* D(i-1,k-1) * t_D(k-1)->M(k) */
58 xB + *(tsc+p7P_BM))); /* B(i-1) * t_B->M_k */
59 tsc += p7P_NTRANS; /* advance to the t_X(k) transitions now */
60
61 /* pick up values from prev row, (k)... at next loop iteration they're magically the k-1 values we need for M calc */
62 mvp = *dpp++; ivp = *dpp++; dvp = *dpp++;
63
64 /* insert state */
65 *dpc++ = *rsc++ /* emission(I_k, x_i) */
66 + p7_FLogsum( mvp + *(tsc+p7P_MI), /* M(i-1,k) * t_M(k)->I(k) */
67 ivp + *(tsc+p7P_II)); /* I(i-1,k) * t_I(k)->I(k) */
68
69 /* E state accumulation */
70 xE = p7_FLogsum( p7_FLogsum(sc + esc, dc + esc), xE);
71
72 /* delayed store of delete state; then calculate/push NEXT D_k+1 */
73 *dpc++ = dc;
74 dc = p7_FLogsum( sc + *(tsc+p7P_MD), /* M(i,k) * t_M(k)->D(k+1) */
75 dc + *(tsc+p7P_DD)); /* D(i,k) * t_D(k)->D(k+1) */
76 }
77
78 /* unrolled match state M_M */
79 *dpc++ = sc = *rsc++ /* emission(M_M, x_i) */
80 + p7_FLogsum(p7_FLogsum( mvp + *(tsc + p7P_MM), /* M(i-1,M-1) * t_MM */
81 ivp + *(tsc + p7P_IM)), /* I(i-1,M-1) * t_IM */
82 p7_FLogsum( dvp + *(tsc + p7P_DM), /* D(i-1,M-1) * t_DM */
83 xB + *(tsc + p7P_BM))); /* B(i-1) * t_BM_M */
84 *dpc++ = -eslINFINITY; /* I_M: no such state */
85 *dpc++ = dc; /* delayed store of D_M */
86 dpp += 3;
87
88 /* now dpc and dpp are sitting on [ENJBC] special state arrays for current, prev row. */
89 dpc[p7GC_E] = p7_FLogsum( p7_FLogsum(sc, dc), xE); /* E state update += D_M + M_M */
90 dpc[p7GC_N] = dpp[p7GC_N] + gm->xsc[p7P_N][p7P_LOOP]; /* N state = N(i-1)->N(i) */
91 dpc[p7GC_JJ] = dpp[p7GC_J] + gm->xsc[p7P_J][p7P_LOOP]; /* J->J emission partial path */
92 dpc[p7GC_J] = p7_FLogsum( dpc[p7GC_JJ], dpc[p7GC_E] + gm->xsc[p7P_E][p7P_LOOP]); /* J state = JJ + E->J */
93 dpc[p7GC_B] = p7_FLogsum( dpc[p7GC_N] + gm->xsc[p7P_N][p7P_MOVE], dpc[p7GC_J] + gm->xsc[p7P_J][p7P_MOVE]); /* B state = N->B + J->B */
94 dpc[p7GC_CC] = dpp[p7GC_C] + gm->xsc[p7P_C][p7P_LOOP]; /* C->C emission partial path */
95 dpc[p7GC_C] = p7_FLogsum( dpc[p7GC_CC], dpc[p7GC_E] + gm->xsc[p7P_E][p7P_MOVE]); /* C state = CC + E->C */
96 }
97
98
99 /* Function: p7_GForwardCheckpointed()
100 * Synopsis: Forward pass in a checkpointed generic DP matrix
101 *
102 * Purpose: Compute the Forward pass of a comparison of model <gm>
103 * against digital sequence <dsq> of length <L>, resulting
104 * in a filled DP matrix <gxc> and (optionally)
105 * a Forward score <opt_sc> in nats.
106 *
107 * The caller has already allocated and laid out <gxc>
108 * appropriately for the <gm->M> by <L> comparison, either
109 * with <p7_gmxchk_Create()> or <p7_gmxchk_GrowTo()>.
110 *
111 * The caller has also already configured the length model
112 * in <gm> for the target sequence length <L>, for example
113 * by calling <p7_ReconfigLength()>.
114 *
115 * Args: dsq : digital sequence target
116 * L : length of <dsq> in residues
117 * gm : query profile
118 * gxc : checkpointed DP matrix to fill
119 * opt_sc : optRETURN: Forward raw lod score, in nats
120 *
121 * Returns: <eslOK> on success. <gxc> contains checkpointed DP
122 * Forward matrix, ready for backwards pass. <opt_sc>
123 * is the raw lod score in nats.
124 *
125 * Throws: (no abnormal error conditions)
126 */
127 int
128 p7_GForwardCheckpointed(const ESL_DSQ *dsq, int L, const P7_PROFILE *gm, P7_GMXCHK *gxc, float *opt_sc)
129 {
130 float *dpc; /* ptr to current row */
131 float *dpp; /* ptr to previous row */
132 int M = gm->M;
133 int w; /* size of a segment of DP rows ended w/ one checkpointed row, inclusive */
134 int b; /* counts down over segment number (Rb+Rc..1); each segment has r+1 rows */
135 int i,k;
136
137 /* Initialization of the zero row, fwd[0], inc. ENJBC */
138 dpc = dpp = gxc->dp[gxc->R0-1]; /* i.e., dp[2], the reserved fwd[0] row; 0,1 are reserved for tmp space and backwards calculation */
139 for (k = 0; k < (M+1)*p7G_NSCELLS; k++) /* MID[0..M] are all impossible */
140 *dpc++ = -eslINFINITY;
141 *dpc++ = -eslINFINITY; /* E; need seq to get here */
142 *dpc++ = 0; /* N */
143 *dpc++ = -eslINFINITY; /* JJ; need seq to get here */
144 *dpc++ = -eslINFINITY; /* J; need seq to get here */
145 *dpc++ = gm->xsc[p7P_N][p7P_MOVE]; /* B; from N->B only */
146 *dpc++ = -eslINFINITY; /* CC; need seq to get here */
147 *dpc = -eslINFINITY; /* C; need seq to get here */
148
149 /* Phase one: "a" region: uncheckpointed rows of matrix */
150 for (i = 1; i <= gxc->La; i++)
151 {
152 dpc = gxc->dp[gxc->R0+gxc->R]; gxc->R++; /* idiomatic for "get next saved row" */
153 forward_row(dsq, gm, gxc, dpp, dpc, i);
154 dpp = dpc;
155 }
156
157 /* Phase two: "b" and "c" regions: partially and fully checkpointed */
158 /* i= gxc->La+1, from previous loop's end */
159 for (b = gxc->Rb + gxc->Rc, w = (gxc->Rb ? gxc->Lb : gxc->Rc+1); i <= L; i++)
160 {
161 if (! (--w))
162 { /* we're on the last row in segment: this row is saved */
163 dpc = gxc->dp[gxc->R0+gxc->R]; gxc->R++; /* idiomatic for "get next saved row" */
164 w=b; /* next segment has this many rows, ending in a saved row */
165 b--; /* decrement segment number counter; last segment is r=1 */
166 }
167 else
168 dpc = gxc->dp[i%2]; /* idiomatic for "get next temp row", 0 or 1; i%2 is to make sure dpp != dpc */
169
170 forward_row(dsq, gm, gxc, dpp, dpc, i);
171 dpp = dpc;
172 }
173
174 gxc->M = M;
175 gxc->L = L;
176 gxc->R = gxc->Ra + gxc->Rb + gxc->Rc;
177 if (opt_sc) *opt_sc = gxc->dp[gxc->R0+gxc->R-1][(M+1)*p7G_NSCELLS + p7GC_C] + gm->xsc[p7P_C][p7P_MOVE];
178 return eslOK;
179 }
180 /*-------------------- end, forwards ----------------------------*/
181
182
183 /*****************************************************************
184 *= 2. Backwards: linear-memory back pass, recovering posterior-decoded bands
185 *****************************************************************/
186
187 static inline void
188 backward_row(const ESL_DSQ *dsq, const P7_PROFILE *gm, P7_GMXCHK *gxc, const float *dpp, float *dpc, int i)
189 {
190 const float * const tsc = gm->tsc; /* both the parameters and the pointer itself are constant */
191 const float * const rsc = gm->rsc[dsq[i+1]];
192 int M = gm->M;
193 float esc = p7_profile_IsLocal(gm) ? 0 : -eslINFINITY;
194 int k;
195
196 XMR(dpc,p7GC_C) = XMR(dpc,p7GC_CC) = XMR(dpp,p7GC_C) + gm->xsc[p7P_C][p7P_LOOP];
197
198 XMR(dpc,p7GC_B) = MMR(dpp,1) + TSC(p7P_BM,0) + MSC(1); /* t_BM index = 0 because it's stored off by one */
199 for (k = 2; k <= M; k++)
200 XMR(dpc,p7GC_B) = p7_FLogsum( XMR(dpc,p7GC_B), MMR(dpp,k) + TSC(p7P_BM,k-1) + MSC(k));
201
202 XMR(dpc,p7GC_J) = XMR(dpc,p7GC_JJ) = p7_FLogsum( XMR(dpp,p7GC_J) + gm->xsc[p7P_J][p7P_LOOP], XMR(dpc,p7GC_B) + gm->xsc[p7P_J][p7P_MOVE]);
203
204 XMR(dpc,p7GC_N) = p7_FLogsum( XMR(dpp,p7GC_N) + gm->xsc[p7P_N][p7P_LOOP], XMR(dpc,p7GC_B) + gm->xsc[p7P_N][p7P_MOVE]);
205 XMR(dpc,p7GC_E) = p7_FLogsum( XMR(dpc,p7GC_J) + gm->xsc[p7P_E][p7P_LOOP], XMR(dpc,p7GC_C) + gm->xsc[p7P_E][p7P_MOVE]);
206
207 DMR(dpc,M) = XMR(dpc,p7GC_E);
208 IMR(dpc,M) = -eslINFINITY;
209 MMR(dpc,M) = XMR(dpc,p7GC_E);
210
211 for (k = M-1; k >= 1; k--)
212 {
213 DMR(dpc,k) = p7_FLogsum( p7_FLogsum( DMR(dpc, k+1) + TSC(p7P_DD,k),
214 XMR(dpc, p7GC_E) + esc),
215 MMR(dpp,k+1) + TSC(p7P_DM,k) + MSC(k+1));
216
217 IMR(dpc,k) = p7_FLogsum( MMR(dpp,k+1) + TSC(p7P_IM,k) + MSC(k+1),
218 IMR(dpp,k) + TSC(p7P_II,k) + ISC(k));
219
220 MMR(dpc,k) = p7_FLogsum( p7_FLogsum(MMR(dpp,k+1) + TSC(p7P_MM,k) + MSC(k+1),
221 IMR(dpp,k) + TSC(p7P_MI,k) + ISC(k)),
222 p7_FLogsum(XMR(dpc,p7GC_E) + esc,
223 DMR(dpc,k+1) + TSC(p7P_MD,k)));
224 }
225 }
226
227 /* worry about small seq cases: L=0..2 */
228 int
229 p7_GBackwardCheckpointed(const ESL_DSQ *dsq, int L, const P7_PROFILE *gm, P7_GMXCHK *gxc, P7_GBANDS *bnd, float *opt_sc)
230 {
231 float const *tsc = gm->tsc;
232 float const *rsc = NULL;
233 int M = gm->M;
234 float esc = p7_profile_IsLocal(gm) ? 0 : -eslINFINITY;
235 float *fwd;
236 float *bck;
237 float *dpp; /* "previous" row (i-1 for forward, i+1 for backward) */
238 int i,i2,k,b,w,s;
239 float overall_sc = gxc->dp[gxc->R0+gxc->R-1][(M+1)*p7G_NSCELLS + p7GC_C] + gm->xsc[p7P_C][p7P_MOVE];
240
241 #if eslDEBUGLEVEL > 0
242 if (gxc->do_debugging) p7_gmxchk_DumpHeader(gxc->dfp, gxc, 0, gxc->M, gxc->dbg_flags);
243 #endif
244
245 /* this initialization could be in gmxchk: nothing else touches these boundary cells */
246 for (s = 0; s < p7G_NSCELLS; s++) gxc->dp[0][s] = gxc->dp[1][s] = -eslINFINITY;
247
248 /* We have to handle the first block b=1 as a special case (rows L-1, L)
249 * because row L is a special case.
250 */
251
252 /* Initialize backwards row L */
253 i = L;
254 gxc->R--;
255 fwd = gxc->dp[gxc->R0+gxc->R]; /* pop row for fwd[L] off "stack" */
256 bck = gxc->dp[L%2]; /* get tmp space for bck[L] */
257 XMR(bck, p7GC_C) = gm->xsc[p7P_C][p7P_MOVE]; /* C<-T */
258 XMR(bck, p7GC_CC) = gm->xsc[p7P_C][p7P_MOVE]; /* CC<-T */
259 XMR(bck, p7GC_B) = -eslINFINITY; /* B<-T not possible */
260 XMR(bck, p7GC_J) = -eslINFINITY; /* J<-T not possible */
261 XMR(bck, p7GC_JJ) = -eslINFINITY; /* JJ<-T not possible*/
262 XMR(bck, p7GC_N) = -eslINFINITY; /* N<-T not possible */
263 XMR(bck, p7GC_E) = XMR(bck, p7GC_C) + gm->xsc[p7P_E][p7P_MOVE]; /* E<-C, no tail */
264
265 DMR(bck, M) = XMR(bck, p7GC_E); /* D_M <- E (t = 1.0) */
266 IMR(bck, M) = -eslINFINITY; /* I_M nonexistent */
267 MMR(bck, M) = XMR(bck, p7GC_E); /* M_M <- E */
268
269 for (k = M-1; k >= 1; k--)
270 {
271 DMR(bck, k) = p7_FLogsum( XMR(bck, p7GC_E) + esc,
272 DMR(bck, k+1) + TSC(p7P_DD, k));
273 IMR(bck, k) = -eslINFINITY;
274 MMR(bck, k) = p7_FLogsum( XMR(bck, p7GC_E) + esc,
275 DMR(bck, k+1) + TSC(p7P_MD, k));
276 }
277 #if eslDEBUGLEVEL > 0
278 if (gxc->do_debugging) p7_gmxchk_DumpRow(gxc->dfp, gxc, bck, i, 0, gxc->M, gxc->dbg_flags);
279 #endif
280 posterior_decode_row(i, fwd, bck, M, overall_sc, bnd);
281 i--; /* i is now L-1 */
282 dpp = bck;
283
284 /* If there's any checkpointing at all, there's an L-1 row to fill in */
285 if (gxc->Rb+gxc->Rc > 0)
286 { /* i=L-1 as we enter */
287 /* Compute forwards from last checkpoint (which we know is L-2) */
288 dpp = gxc->dp[gxc->R0+gxc->R-1];
289 fwd = gxc->dp[gxc->R0+gxc->R]; /* new row, from top of stack */
290 forward_row(dsq, gm, gxc, dpp, fwd, i);
291
292 /* Compute backwards: L-1 row from L row */
293 dpp = bck;
294 bck = gxc->dp[(L-1)%2];
295 backward_row(dsq, gm, gxc, dpp, bck, i);
296 #if eslDEBUGLEVEL > 0
297 if (gxc->do_debugging) p7_gmxchk_DumpRow(gxc->dfp, gxc, bck, i, 0, gxc->M, gxc->dbg_flags);
298 #endif
299
300 posterior_decode_row(i, fwd, bck, M, overall_sc, bnd);
301 dpp = bck;
302 i--; /* i is now L-2 if there was any checkpointing; L-1 if not. */
303 }
304
305 /* Checkpointed regions (b,c) */
306 for (b = 2; b <= gxc->Rb+gxc->Rc; b++)
307 { /* i=L-2 as we enter here. */
308 w = (b <= gxc->Rc ? b+1 : gxc->Lb);
309
310 /* current row i (r=R0+R-1) ends a block and is checkpointed. */
311 gxc->R--;
312 fwd = gxc->dp[gxc->R0+gxc->R]; /* pop last forward row off "stack" */
313 bck = gxc->dp[i%2];
314 backward_row(dsq, gm, gxc, dpp, bck, i);
315 #if eslDEBUGLEVEL > 0
316 if (gxc->do_debugging) p7_gmxchk_DumpRow(gxc->dfp, gxc, bck, i, 0, gxc->M, gxc->dbg_flags);
317 #endif
318 posterior_decode_row(i, fwd, bck, M, overall_sc, bnd);
319
320 /* compute Forwards from last checkpoint */
321 dpp = gxc->dp[gxc->R0+gxc->R-1];
322 for (i2 = i-w+1; i2 <= i-1; i2++)
323 {
324 fwd = gxc->dp[gxc->R0+gxc->R]; gxc->R++; /* push new forward row on "stack" */
325 forward_row(dsq, gm, gxc, dpp, fwd, i2);
326 dpp = fwd;
327 }
328
329 /* now compute Backwards over the block we just calculated */
330 dpp = bck;
331 for (i2 = i-1; i2 >= i-w+1; i2--)
332 {
333 gxc->R--;
334 fwd = gxc->dp[gxc->R0+gxc->R]; /* pop last forward row off "stack" */
335 bck = gxc->dp[i2%2];
336
337 backward_row(dsq, gm, gxc, dpp, bck, i2);
338 #if eslDEBUGLEVEL > 0
339 if (gxc->do_debugging) p7_gmxchk_DumpRow(gxc->dfp, gxc, bck, i2, 0, gxc->M, gxc->dbg_flags);
340 #endif
341
342 posterior_decode_row(i2, fwd, bck, M, overall_sc, bnd);
343 dpp = bck;
344 }
345
346 i -= w;
347 }
348 /* now i=La as we leave the checkpointed regions; or i=L-1 if there was no checkpointing */
349
350
351 /* The uncheckpointed "a" region */
352 for (; i >= 1; i--)
353 {
354 gxc->R--;
355 fwd = gxc->dp[gxc->R0+gxc->R];
356 bck = gxc->dp[i%2];
357
358 backward_row(dsq, gm, gxc, dpp, bck, i);
359 #if eslDEBUGLEVEL > 0
360 if (gxc->do_debugging) p7_gmxchk_DumpRow(gxc->dfp, gxc, bck, i, 0, gxc->M, gxc->dbg_flags);
361 #endif
362 posterior_decode_row(i, fwd, bck, M, overall_sc, bnd);
363
364 dpp = bck;
365 }
366
367 /* now i=0. At i=0, only N,B states are reachable. */
368 bck = gxc->dp[0];
369 rsc = gm->rsc[dsq[1]];
370 XMR(bck,p7GC_B) = MMR(dpp,1) + TSC(p7P_BM,0) + MSC(1); /* t_BM index is 0 because it's stored off-by-one. */
371 for (k = 2; k <= M; k++)
372 XMR(bck,p7GC_B) = p7_FLogsum(XMR(bck,p7GC_B), MMR(dpp,k) + TSC(p7P_BM,k-1) + MSC(k));
373 XMR(bck,p7GC_J) = -eslINFINITY;
374 XMR(bck,p7GC_C) = -eslINFINITY;
375 XMR(bck,p7GC_N) = p7_FLogsum( XMR(dpp, p7GC_N) + gm->xsc[p7P_N][p7P_LOOP],
376 XMR(bck, p7GC_B) + gm->xsc[p7P_N][p7P_MOVE]);
377 XMR(bck,p7GC_E) = -eslINFINITY;
378
379 for (k = M; k >= 1; k--)
380 MMR(bck,k) = IMR(bck,k) = DMR(bck,k) = -eslINFINITY;
381 #if eslDEBUGLEVEL > 0
382 if (gxc->do_debugging) p7_gmxchk_DumpRow(gxc->dfp, gxc, bck, i, 0, gxc->M, gxc->dbg_flags);
383 #endif
384
385 bnd->L = L;
386 bnd->M = M;
387 p7_gbands_Reverse(bnd);
388 if (opt_sc != NULL) *opt_sc = XMR(bck,p7GC_N);
389 return eslOK;
390 }
391 /*--------------------- end, backwards --------------------------*/
392
393
394 /*****************************************************************
395 * x. Posterior decoding in linear memory
396 *****************************************************************/
397
398 /* remember, the row layout is (see note 2 in p7_gmxchk.h):
399 * dpc: [M I D] [M I D] [M I D] ... [M I D] [E N JJ J B CC C]
400 * k: |-- 0 --| |-- 1 --| |-- 2 --| ... |-- M --|
401 * |------------- (M+1)*p7G_NSCELLS -----------| |---- p7GC_NXCELLS ---|
402 */
403
404 static inline void
405 posterior_decode_row(int rowi, float *fwd, float *bck, int M, float overall_sc, P7_GBANDS *bnd)
406 {
407 int k;
408 int ka, kb;
409
410 XMR(fwd, p7GC_N) = expf(XMR(fwd, p7GC_N) + XMR(bck, p7GC_N) - overall_sc);
411 XMR(fwd, p7GC_JJ) = expf(XMR(fwd, p7GC_JJ) + XMR(bck, p7GC_JJ) - overall_sc);
412 XMR(fwd, p7GC_CC) = expf(XMR(fwd, p7GC_CC) + XMR(bck, p7GC_CC) - overall_sc);
413
414 if (XMR(fwd, p7GC_N) + XMR(fwd, p7GC_JJ) + XMR(fwd, p7GC_CC) >= 0.9) return;
415
416 for (k = 1; k < M; k++)
417 {
418 MMR(fwd, k) = expf(MMR(fwd,k) + MMR(bck,k) - overall_sc);
419 IMR(fwd, k) = expf(IMR(fwd,k) + IMR(bck,k) - overall_sc);
420 }
421 MMR(fwd, M) = expf(MMR(fwd, M) + MMR(bck,M) - overall_sc);
422 IMR(fwd, M) = 0.0f;
423
424 for (ka = 1; ka <= M; ka++) if (MMR(fwd, ka) + IMR(fwd, ka) >= 0.02) break;
425 if (ka == M+1) return;
426 for (kb = M; kb >= 1; kb--) if (MMR(fwd, kb) + IMR(fwd, kb) >= 0.02) break;
427
428 p7_gbands_Prepend(bnd, rowi, ka, kb);
429 //printf("%4d %.4f %.4f %.4f %4d %4d\n", rowi, 1-outp, outp, denom, ka, kb);
430 }
431
432
433 /*****************************************************************
434 * 3. Benchmark driver.
435 *****************************************************************/
436 #ifdef p7GENERIC_FWDBACK_CHK_BENCHMARK
437 /*
438 gcc -g -O2 -o generic_fwdback_chk_benchmark -I. -L. -I../easel -L../easel -Dp7GENERIC_FWDBACK_CHK_BENCHMARK generic_fwdback_chk.c -lhmmer -leasel -lm
439 icc -O3 -static -o generic_fwdback_chk_benchmark -I. -L. -I../easel -L../easel -Dp7GENERIC_FWDBACK_CHK_BENCHMARK generic_fwdback_chk.c -lhmmer -leasel -lm
440 ./generic_fwdback_chk_benchmark <hmmfile>
441 */
442 #include <p7_config.h>
443
444 #include "easel.h"
445 #include "esl_alphabet.h"
446 #include "esl_getopts.h"
447 #include "esl_random.h"
448 #include "esl_randomseq.h"
449 #include "esl_stopwatch.h"
450
451 #include "hmmer.h"
452 #include "p7_gmxchk.h"
453
454 static ESL_OPTIONS options[] = {
455 /* name type default env range toggles reqs incomp help docgroup*/
456 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
457 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
458 { "-L", eslARG_INT, "400", NULL, "n>0", NULL, NULL, NULL, "length of random target seqs", 0 },
459 { "-N", eslARG_INT, "2000", NULL, "n>0", NULL, NULL, NULL, "number of random target seqs", 0 },
460 { "-F", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "only benchmark GForwardCheckpointed()", 0 },
461 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
462 };
463 static char usage[] = "[-options] <hmmfile>";
464 static char banner[] = "benchmark driver for checkpointed generic Forward/Backward";
465
466 int
467 main(int argc, char **argv)
468 {
469 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
470 char *hmmfile = esl_opt_GetArg(go, 1);
471 ESL_STOPWATCH *w = esl_stopwatch_Create();
472 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
473 ESL_ALPHABET *abc = NULL;
474 P7_HMMFILE *hfp = NULL;
475 P7_HMM *hmm = NULL;
476 P7_BG *bg = NULL;
477 P7_PROFILE *gm = NULL;
478 P7_GMXCHK *gxc = NULL;
479 P7_GBANDS *bnd = NULL;
480 int L = esl_opt_GetInteger(go, "-L");
481 int N = esl_opt_GetInteger(go, "-N");
482 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2));
483 int i;
484 float sc;
485 double base_time, bench_time, Mcs;
486
487 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
488 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
489
490 bg = p7_bg_Create(abc);
491 p7_bg_SetLength(bg, L);
492 gm = p7_profile_Create(hmm->M, abc);
493 p7_ProfileConfig(hmm, bg, gm, L, p7_UNILOCAL);
494
495 gxc = p7_gmxchk_Create(gm->M, L, ESL_MBYTES(32));
496 bnd = p7_gbands_Create();
497
498 /* Baseline time. */
499 esl_stopwatch_Start(w);
500 for (i = 0; i < N; i++) esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
501 esl_stopwatch_Stop(w);
502 base_time = w->user;
503
504 /* Benchmark time. */
505 esl_stopwatch_Start(w);
506 for (i = 0; i < N; i++)
507 {
508 esl_rsq_xfIID(r, bg->f, abc->K, L, dsq);
509 p7_GForwardCheckpointed (dsq, L, gm, gxc, &sc);
510 if (! esl_opt_GetBoolean(go, "-F"))
511 p7_GBackwardCheckpointed(dsq, L, gm, gxc, bnd, &sc);
512
513 p7_gmxchk_Reuse(gxc);
514 p7_gbands_Reuse(bnd);
515 }
516 esl_stopwatch_Stop(w);
517 bench_time = w->user - base_time;
518 Mcs = (double) N * (double) L * (double) gm->M * 1e-6 / (double) bench_time;
519 esl_stopwatch_Display(stdout, w, "# CPU time: ");
520 printf("# M = %d\n", gm->M);
521 printf("# %.1f Mc/s\n", Mcs);
522
523 free(dsq);
524 p7_gmxchk_Destroy(gxc);
525 p7_gbands_Destroy(bnd);
526 p7_profile_Destroy(gm);
527 p7_bg_Destroy(bg);
528 p7_hmm_Destroy(hmm);
529 p7_hmmfile_Close(hfp);
530 esl_alphabet_Destroy(abc);
531 esl_stopwatch_Destroy(w);
532 esl_randomness_Destroy(r);
533 esl_getopts_Destroy(go);
534 return 0;
535 }
536 #endif /*p7GENERIC_FWDBACK_CHK_BENCHMARK*/
537 /*----------------- end, benchmark ------------------------------*/
538
539
540
541 /*****************************************************************
542 * x. Unit tests.
543 *****************************************************************/
544 #ifdef p7GENERIC_FWDBACK_CHK_TESTDRIVE
545 #include "esl_sq.h"
546 #include "esl_randomseq.h"
547
548 static void
549 scoring_comparison(ESL_DSQ *dsq, int L, P7_PROFILE *gm, P7_GMX *gx, P7_GMXCHK *gxc, P7_GBANDS *bnd)
550 {
551 char msg[] = "generic_fwdback_chk: score comparison test failure";
552 float tolerance = 0.01; /* nats (absolute score difference) */
553 float fsc1, fsc2;
554 float bsc1, bsc2;
555
556 if ( p7_gmx_GrowTo(gx, gm->M, L) != eslOK) esl_fatal(msg);
557 if ( p7_gmxchk_GrowTo(gxc, gm->M, L) != eslOK) esl_fatal(msg);
558
559 if ( p7_GForward(dsq, L, gm, gx, &fsc1) != eslOK) esl_fatal(msg);
560 if ( p7_gmx_Reuse(gx) != eslOK) esl_fatal(msg);
561 if ( p7_GBackward(dsq, L, gm, gx, &bsc1) != eslOK) esl_fatal(msg);
562 if ( p7_GForwardCheckpointed (dsq, L, gm, gxc, &fsc2) != eslOK) esl_fatal(msg);
563 if ( p7_GBackwardCheckpointed(dsq, L, gm, gxc, bnd, &bsc2) != eslOK) esl_fatal(msg);
564
565 if ( fabs(fsc1-bsc1) > tolerance) esl_fatal(msg);
566 if ( fabs(fsc1-fsc2) > tolerance) esl_fatal(msg);
567 if ( fabs(bsc1-bsc2) > tolerance) esl_fatal(msg);
568
569 if ( p7_gmx_Reuse(gx) != eslOK) esl_fatal(msg);
570 if ( p7_gmxchk_Reuse(gxc) != eslOK) esl_fatal(msg);
571 if ( p7_gbands_Reuse(bnd) != eslOK) esl_fatal(msg);
572 }
573
574 static void
575 utest_randomseq(ESL_RANDOMNESS *rng, ESL_ALPHABET *abc,
576 P7_PROFILE *gm, P7_BG *bg, int nseq, int L)
577 {
578 char msg[] = "generic_fwdback_chk: random seq test failure";
579 ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2));
580 P7_GMX *gx = p7_gmx_Create(gm->M, 100);
581 P7_GMXCHK *gxc = p7_gmxchk_Create(gm->M, 100, ESL_MBYTES(32));
582 P7_GBANDS *bnd = p7_gbands_Create();
583 int idx;
584
585 for (idx = 0; idx < nseq; idx++)
586 {
587 if (esl_rsq_xfIID(rng, bg->f, abc->K, L, dsq) != eslOK) esl_fatal(msg);
588 scoring_comparison(dsq, L, gm, gx, gxc, bnd);
589 }
590
591 p7_gmxchk_Destroy(gxc);
592 p7_gmx_Destroy(gx);
593 p7_gbands_Destroy(bnd);
594 free(dsq);
595 }
596
597 static void
598 utest_emitseq(ESL_RANDOMNESS *rng, ESL_ALPHABET *abc,
599 P7_HMM *hmm, P7_PROFILE *gm, P7_BG *bg, int nseq)
600 {
601 char msg[] = "generic_fwdback_chk: emitted seq test failure";
602 ESL_SQ *sq = esl_sq_CreateDigital(abc);
603 P7_GMX *gx = p7_gmx_Create(gm->M, 100);
604 P7_GMXCHK *gxc = p7_gmxchk_Create(gm->M, 100, ESL_MBYTES(32));
605 P7_GBANDS *bnd = p7_gbands_Create();
606 int idx;
607
608 for (idx = 0; idx < nseq; idx++)
609 {
610 if ( p7_ProfileEmit(rng, hmm, gm, bg, sq, NULL) != eslOK) esl_fatal(msg);
611
612 scoring_comparison(sq->dsq, sq->n, gm, gx, gxc, bnd);
613 }
614
615 p7_gmxchk_Destroy(gxc);
616 p7_gmx_Destroy(gx);
617 p7_gbands_Destroy(bnd);
618 esl_sq_Destroy(sq);
619 }
620
621
622 #endif /*p7GENERIC_FWDBACK_CHK_TESTDRIVE*/
623 /*----------------- end, unit tests -----------------------------*/
624
625
626
627 /*****************************************************************
628 * x. Test driver.
629 *****************************************************************/
630 #ifdef p7GENERIC_FWDBACK_CHK_TESTDRIVE
631
632 #include <p7_config.h>
633 #include "easel.h"
634 #include "esl_getopts.h"
635
636 #include "hmmer.h"
637 #include "p7_gmxchk.h"
638
639 static ESL_OPTIONS options[] = {
640 /* name type default env range toggles reqs incomp help docgroup*/
641 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
642 { "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
643 { "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be verbose", 0 },
644 { "--vv", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be very verbose", 0 },
645 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
646 };
647 static char usage[] = "[-options]";
648 static char banner[] = "tests for generic checkpointed Forward/Backward implementation";
649
650 int
651 main(int argc, char **argv)
652 {
653 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage);
654 ESL_RANDOMNESS *rng = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
655 ESL_ALPHABET *abc = NULL;
656 P7_HMM *hmm = NULL;
657 P7_PROFILE *gm = NULL;
658 P7_BG *bg = NULL;
659 int M = 100;
660 int L = 200;
661 int nseq = 20;
662 char errbuf[eslERRBUFSIZE];
663
664 p7_FLogsumInit();
665
666 if ((abc = esl_alphabet_Create(eslAMINO)) == NULL) esl_fatal("failed to create alphabet");
667 if (p7_hmm_Sample(rng, M, abc, &hmm) != eslOK) esl_fatal("failed to sample an HMM");
668 if ((bg = p7_bg_Create(abc)) == NULL) esl_fatal("failed to create null model");
669 if ((gm = p7_profile_Create(hmm->M, abc)) == NULL) esl_fatal("failed to create profile");
670 if (p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL) != eslOK) esl_fatal("failed to config profile");
671 if (p7_hmm_Validate (hmm, errbuf, 0.0001) != eslOK) esl_fatal("whoops, HMM is bad!: %s", errbuf);
672 if (p7_profile_Validate(gm, errbuf, 0.0001) != eslOK) esl_fatal("whoops, profile is bad!: %s", errbuf);
673
674 utest_randomseq(rng, abc, gm, bg, nseq, L);
675 utest_emitseq (rng, abc, hmm, gm, bg, nseq);
676
677 p7_profile_Destroy(gm);
678 p7_bg_Destroy(bg);
679 p7_hmm_Destroy(hmm);
680 esl_alphabet_Destroy(abc);
681 esl_randomness_Destroy(rng);
682 esl_getopts_Destroy(go);
683 return 0;
684 }
685
686 #endif /*p7GENERIC_FWDBACK_CHK_TESTDRIVE*/
687 /*------------------ end, test driver ---------------------------*/
688
689
690
691 /*****************************************************************
692 * 4. Example main()
693 *****************************************************************/
694 #ifdef p7GENERIC_FWDBACK_CHK_EXAMPLE
695 /*
696 gcc -g -O2 -o generic_fwdback_chk_example -Dp7GENERIC_FWDBACK_CHK_EXAMPLE -I. -I../easel -L. -L../easel generic_fwdback_chk.c -lhmmer -leasel -lm
697 */
698 #include <p7_config.h>
699
700 #include "easel.h"
701 #include "esl_alphabet.h"
702 #include "esl_getopts.h"
703 #include "esl_sq.h"
704 #include "esl_sqio.h"
705
706 #include "hmmer.h"
707 #include "p7_gmxchk.h"
708
709 static ESL_OPTIONS options[] = {
710 /* name type default env range toggles reqs incomp help docgroup*/
711 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
712 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
713 };
714 static char usage[] = "[-options] <hmmfile> <seqfile>";
715 static char banner[] = "example of checkpointed Forward/Backward, generic implementation";
716
717 int
718 main(int argc, char **argv)
719 {
720 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 2, argc, argv, banner, usage);
721 char *hmmfile = esl_opt_GetArg(go, 1);
722 char *seqfile = esl_opt_GetArg(go, 2);
723 ESL_ALPHABET *abc = NULL;
724 P7_HMMFILE *hfp = NULL;
725 P7_HMM *hmm = NULL;
726 P7_BG *bg = NULL;
727 P7_PROFILE *gm = NULL;
728 P7_GMX *fwd = NULL;
729 P7_GMX *bck = NULL;
730 P7_GMXCHK *gxc = NULL;
731 P7_GBANDS *bnd = NULL;
732 ESL_SQ *sq = NULL;
733 ESL_SQFILE *sqfp = NULL;
734 int format = eslSQFILE_UNKNOWN;
735 float fsc, fsc2;
736 float bsc, bsc2;
737 float nullsc;
738 int status;
739
740 /* Initialize log-sum calculator */
741 p7_FLogsumInit();
742
743 /* Read in one HMM */
744 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
745 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
746 p7_hmmfile_Close(hfp);
747
748 /* Read in one sequence */
749 sq = esl_sq_CreateDigital(abc);
750 status = esl_sqfile_Open(seqfile, format, NULL, &sqfp);
751 if (status == eslENOTFOUND) p7_Fail("No such file, or file open failed");
752 else if (status == eslEFORMAT) p7_Fail("Format unrecognized.");
753 else if (status != eslOK) p7_Fail("Open failed, code %d.", status);
754
755 /* Configure a profile from the HMM */
756 bg = p7_bg_Create(abc);
757 gm = p7_profile_Create(hmm->M, abc);
758 p7_ProfileConfig(hmm, bg, gm, 400, p7_LOCAL);
759
760 /* Allocate matrices */
761 fwd = p7_gmx_Create(gm->M, 400);
762 bck = p7_gmx_Create(gm->M, 400);
763 gxc = p7_gmxchk_Create(gm->M, 400, ESL_MBYTES(32));
764 bnd = p7_gbands_Create();
765
766 while ( (status = esl_sqio_Read(sqfp, sq)) != eslEOF)
767 {
768 if (status == eslEFORMAT) p7_Fail("Parse failed (sequence file %s)\n%s\n", sqfp->filename, sqfp->get_error(sqfp));
769 else if (status != eslOK) p7_Fail("Unexpected error %d reading sequence file %s", status, sqfp->filename);
770
771 /* Resize the DP matrices if necessary */
772 p7_gmx_GrowTo (fwd, gm->M, sq->n);
773 p7_gmx_GrowTo (bck, gm->M, sq->n);
774 p7_gmxchk_GrowTo(gxc, gm->M, sq->n);
775
776 //printf("Allocation: %ld\n", p7_gmxchk_Sizeof(fwdc));
777
778 /* Set the profile and null model's target length models */
779 p7_bg_SetLength(bg, sq->n);
780 p7_ReconfigLength(gm, sq->n);
781
782 /* Run Forward in both modes */
783 p7_GForward (sq->dsq, sq->n, gm, fwd, &fsc);
784 p7_GForwardCheckpointed(sq->dsq, sq->n, gm, gxc, &fsc2);
785
786 /* Dump the DP matrices. (Voluminous; only small examples are reasonable) */
787 //p7_gmx_Dump(stdout, fwd, p7_DEFAULT);
788 //p7_gmxchk_Dump(stdout, gxc, p7_DEFAULT);
789
790 /* Run Backward in both modes */
791 p7_GBackward (sq->dsq, sq->n, gm, bck, &bsc);
792 //p7_gmx_Dump(stdout, bck, p7_DEFAULT);
793
794 //p7_gmxchk_SetDumpMode(gxc, stdout, p7_DEFAULT);
795 p7_GBackwardCheckpointed(sq->dsq, sq->n, gm, gxc, bnd, &bsc2);
796 //p7_gmxchk_SetDumpMode(gxc, NULL, 0);
797
798 p7_gbands_Dump(stdout, bnd);
799
800 /* Those scores are partial log-odds likelihoods in nats.
801 * Subtract off the rest of the null model, convert to bits.
802 */
803 p7_bg_NullOne(bg, sq->dsq, sq->n, &nullsc);
804
805 printf("%-30s %10.4f %10.4f %10.4f %10.4f %10.4f %10.4f %10.4f %10.4f\n",
806 sq->name,
807 fsc, fsc2,
808 (fsc - nullsc) / eslCONST_LOG2, (fsc2 - nullsc) / eslCONST_LOG2,
809 bsc, bsc2,
810 (bsc - nullsc) / eslCONST_LOG2, (bsc2 - nullsc) / eslCONST_LOG2);
811
812 p7_gmx_Reuse(fwd);
813 p7_gmx_Reuse(bck);
814 p7_gmxchk_Reuse(gxc);
815 p7_gbands_Reuse(bnd);
816 esl_sq_Reuse(sq);
817 }
818
819 /* Cleanup */
820 esl_sqfile_Close(sqfp);
821 esl_sq_Destroy(sq);
822 p7_gmx_Destroy(fwd);
823 p7_gmx_Destroy(bck);
824 p7_gmxchk_Destroy(gxc);
825 p7_gbands_Destroy(bnd);
826 p7_profile_Destroy(gm);
827 p7_bg_Destroy(bg);
828 p7_hmm_Destroy(hmm);
829 esl_alphabet_Destroy(abc);
830 esl_getopts_Destroy(go);
831 return 0;
832 }
833 #endif /*p7GENERIC_FWDBACK_CHK_EXAMPLE*/
834 /*------------------- end, example main() -----------------------*/
835
836
837 /* References:
838 * SRE J8/109-112, Oct 2011: implementation plan.
839 */
840
841