Debian Codegraph @

src/cm_dpsearch.c

raw symbols SCIP
1 /* cm_dpsearch.c
2 *
3 * DP functions for CYK and Inside CM similarity search, includes
4 * fast (optimized) and reference versions.
5 *
6 * All CYK/Inside scanning functions were rewritten between
7 * versions 0.81 and 1.0 Here's a list of the 1.x functions
8 * and their 0.81 analogs. All the 1.x functions listed are in
9 * this file.
10 *
11 * 1.x fast version 1.x slow version 0.81 version
12 * ---------------- ---------------- -------------
13 * FastCYKScan() RefCYKScan() scancyk.c:CYKScan()
14 * bandcyk.c:CYKBandedScan()
15 * FastIInsideScan() RefIInsideScan() scaninside.c:InsideScan()
16 * scaninside.c:InsideBandedScan()
17 * FastFInsideScan() RefFInsideScan() NONE
18 * FastCYKScanHB() NONE hbandcyk.c:CYKBandedScan_jd()
19 * NONE NONE hbandcyk.c:iInsideBandedScan_jd()
20 * FastFInsideScanHB() NONE NONE
21 *
22 * The 1.0 functions that end in 'HB()' use HMM bands to perform
23 * the search.
24 * The 1.0 non-HB functions can be run with QDB on or off, which
25 * is implicit in the cm->smx CM_SCAN_MX data structure,
26 * which includes min/max d values for each state.
27 *
28 * Note: Prior to 1.1 release, I removed three experimental Inside
29 * scanners which hadn't been touched since the 1.0 release.
30 * [EPN, Fri Nov 4 14:48:41 2011]:
31 * XFastIInsideScan()
32 * X2FastIInsideScan()
33 * XRefIInsideScan()
34 *
35 * EPN, Wed Sep 12 16:53:32 2007
36 */
37
38 #include <esl_config.h>
39 #include <p7_config.h>
40 #include "config.h"
41
42 #include <stdio.h>
43 #include <stdlib.h>
44
45 #include "easel.h"
46 #include "esl_sqio.h"
47 #include "esl_stack.h"
48 #include "esl_vectorops.h"
49
50 #include "hmmer.h"
51
52 #include "infernal.h"
53
54 #define AMX(j,v,d) (alphap[(j * cm->M * (W+1)) + ((v) * (W+1) + d)])
55
56 /* Function: FastCYKScan()
57 * Date: EPN, Wed Sep 12 16:55:28 2007
58 *
59 * Purpose: Scan a sequence for matches to a covariance model, using
60 * an optimized scanning CYK scanning implementation.
61 *
62 * The choice of using one of two sets of query-dependent
63 * bands (QDBs) or not using QDBs is controlled by
64 * <qdbidx>. The QDBs are stored in <smx>.
65 *
66 * Args: cm - the covariance model
67 * errbuf - char buffer for reporting errors
68 * smx - CM_SCAN_MX for this search w/this model (incl. DP matrix, qdbands etc.)
69 * qdbidx - controls which QDBs to use: SMX_NOQDB | SMX_QDB1_TIGHT | SMX_QDB2_LOOSE
70 * dsq - the digitized sequence
71 * i0 - start of target subsequence (1 for full seq)
72 * j0 - end of target subsequence (L for full seq)
73 * cutoff - minimum score to report
74 * hitlist - CM_TOPHITS hitlist to add to; if NULL, don't add to it
75 * do_null3 - TRUE to do NULL3 score correction, FALSE not to
76 * env_cutoff - ret_envi..ret_envj will include all hits that exceed this bit sc
77 * ret_envi - RETURN: min position in any hit w/sc >= env_cutoff, set to -1 if no such hits exist, NULL if not wanted
78 * ret_envj - RETURN: max position in any hit w/sc >= env_cutoff, set to -1 if no such hits exist, NULL if not wanted
79 * ret_vsc - RETURN: [0..v..M-1] best score at each state v, NULL if not-wanted
80 * ret_sc - RETURN: score of best overall hit (vsc[0])
81 *
82 * Note: This function is heavily synchronized with FastFInsideScan() and FastIInsideScan(),
83 * any change to this function should be mirrored in those functions.
84 *
85 * Returns: eslOK on success and RETURN variables updated (or not if NULL).
86 * eslEINCOMPAT on contract violation, errbuf if filled with informative error message.
87 * eslEMEM if out of memory, errbuf if filled with informative error message.
88 */
89 int
90 FastCYKScan(CM_t *cm, char *errbuf, CM_SCAN_MX *smx, int qdbidx, ESL_DSQ *dsq, int64_t i0, int64_t j0, float cutoff, CM_TOPHITS *hitlist,
91 int do_null3, float env_cutoff, int64_t *ret_envi, int64_t *ret_envj, float **ret_vsc, float *ret_sc)
92 {
93 int status;
94 GammaHitMx_t *gamma = NULL; /* semi-HMM for hit resoultion */
95 float *vsc; /* best score for each state (float) */
96 float vsc_root = IMPOSSIBLE; /* score of best hit */
97 int yoffset; /* offset to a child state */
98 int i,j; /* index of start/end positions in sequence, 0..L */
99 int d; /* a subsequence length, 0..W */
100 int k; /* used in bifurc calculations: length of right subseq */
101 int prv, cur; /* previous, current j row (0 or 1) */
102 int v, w, y; /* state indices */
103 int jp_v; /* offset j for state v */
104 int jp_y; /* offset j for state y */
105 int jp_g; /* offset j for gamma (j-i0+1) */
106 int dp_y; /* offset d for state y */
107 int kmin, kmax; /* for B_st's, min/max value consistent with bands*/
108 int L; /* length of the subsequence (j0-i0+1) */
109 int W; /* max d; max size of a hit, this is min(L, smx->W) */
110 int sd; /* StateDelta(cm->sttype[v]), # emissions from v */
111 int do_banded = FALSE; /* TRUE: use QDBs, FALSE: don't */
112 int *dnA, *dxA; /* tmp ptr to 1 row of dnAA, dxAA */
113 int dn, dx; /* minimum/maximum valid d for current state */
114 int dn_y, dx_y; /* minimum/maximum valid d for state y */
115 int dn_w, dx_w; /* minimum/maximum valid d for state w */
116 int *dmin; /* [0..v..cm->M-1] minimum d allowed for this state */
117 int *dmax; /* [0..v..cm->M-1] maximum d allowed for this state */
118 int cnum; /* number of children for current state */
119 int *jp_wA; /* rolling pointer index for B states, gets precalc'ed */
120 float *sc_v; /* [0..d..W] temporary score vec for each d for current j & v */
121 float **init_scAA; /* [0..v..cm->M-1][0..d..W] initial score for each v, d for all j */
122 double **act; /* [0..j..W-1][0..a..abc->K-1], alphabet count, count of residue a in dsq from 1..jp where j = jp%(W+1) */
123 int do_env_defn; /* TRUE to calculate envi, envj, FALSE not to (TRUE if ret_envi != NULL or ret_envj != NULL */
124 int64_t envi, envj; /* min/max positions that exist in any hit with sc >= env_cutoff */
125 CM_TOPHITS *tmp_hitlist = NULL; /* temporary hitlist, containing possibly overlapping hits */
126 int h; /* counter over hits */
127
128 /* Contract check */
129 if(! (cm->flags & CMH_BITS)) ESL_FAIL(eslEINCOMPAT, errbuf, "FastCYKScan, CMH_BITS flag is not raised.\n");
130 if(j0 < i0) ESL_FAIL(eslEINCOMPAT, errbuf, "FastCYKScan, i0: %" PRId64 " j0: %" PRId64 "d\n", i0, j0);
131 if(dsq == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "FastCYKScan, dsq is NULL\n");
132 if(cm->search_opts & CM_SEARCH_INSIDE) ESL_FAIL(eslEINCOMPAT, errbuf, "FastCYKScan, CM_SEARCH_INSIDE flag raised");
133 if(smx == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "FastCYKScan, smx == NULL\n");
134 if(! smx->floats_valid) ESL_FAIL(eslEINCOMPAT, errbuf, "FastCYKScan, smx->floats_valid if FALSE");
135 if(cm->qdbinfo == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "FastCYKScan, cm->qdbinfo == NULL\n");
136
137 /* make pointers to the ScanMatrix/CM data for convenience */
138 float ***alpha = smx->falpha; /* [0..j..1][0..v..cm->M-1][0..d..W] alpha DP matrix, NULL for v == BEGL_S */
139 float ***alpha_begl = smx->falpha_begl; /* [0..j..W][0..v..cm->M-1][0..d..W] alpha DP matrix, NULL for v != BEGL_S */
140 int **dnAA = smx->dnAAA[qdbidx]; /* [0..j..W][0..v..cm->M-1] minimum d for v, j (for j > W use [W][v]) */
141 int **dxAA = smx->dxAAA[qdbidx]; /* [0..j..W][0..v..cm->M-1] maximum d for v, j (for j > W use [W][v]) */
142 float *bestsc = smx->bestsc; /* [0..d..W] best score for this d, recalc'ed for each j endpoint */
143 int *bestr = smx->bestr; /* [0..d..W] best root state (for local begins or 0) for this d, recalc'ed for each j endpoint */
144 float **esc_vAA = cm->oesc; /* [0..v..cm->M-1][0..a..(cm->abc->Kp | cm->abc->Kp**2)] optimized emission scores for v
145 * and all possible emissions a (including ambiguities) */
146
147 /* Determine if we're doing banded/non-banded and get pointers to
148 * dmin/dmax. (We only need dmin/dmax so we can compute kmin/kmax
149 * for B states.)
150 */
151 if (qdbidx == SMX_NOQDB) { do_banded = FALSE; dmin = NULL; dmax = NULL; }
152 else if(qdbidx == SMX_QDB1_TIGHT) { do_banded = TRUE; dmin = cm->qdbinfo->dmin1; dmax = cm->qdbinfo->dmax1; }
153 else if(qdbidx == SMX_QDB2_LOOSE) { do_banded = TRUE; dmin = cm->qdbinfo->dmin2; dmax = cm->qdbinfo->dmax2; }
154 else ESL_FAIL(eslEINCOMPAT, errbuf, "FastCYKScan, qdbidx is invalid");
155
156 L = j0-i0+1;
157 W = smx->W;
158 if (W > L) W = L;
159
160 /* initialize the scan matrix */
161 if((status = cm_scan_mx_InitializeFloats(cm, smx, errbuf)) != eslOK) return status;
162
163 /* set vsc array */
164 vsc = NULL;
165 if(ret_vsc != NULL) {
166 ESL_ALLOC(vsc, sizeof(float) * cm->M);
167 esl_vec_FSet(vsc, cm->M, IMPOSSIBLE);
168 }
169
170 /* If we were passed a master hitlist <hitlist>, either create a
171 * gamma hit matrix for resolving overlaps optimally (if
172 * cm->search_opts & CM_SEARCH_CMNOTGREEDY) or create a temporary
173 * hitlist that will store overlapping hits, in that case, we'll
174 * remove overlaps greedily before copying the hits to the master
175 * <hitlist>.
176 */
177 gamma = NULL;
178 tmp_hitlist = NULL;
179 if(hitlist != NULL) {
180 if(cm->search_opts & CM_SEARCH_CMNOTGREEDY) {
181 gamma = CreateGammaHitMx(L, i0, cutoff);
182 }
183 else {
184 tmp_hitlist = cm_tophits_Create();
185 }
186 }
187
188 /* allocate array for precalc'ed rolling ptrs into BEGL deck, filled inside 'for(j...' loop */
189 ESL_ALLOC(jp_wA, sizeof(float) * (W+1));
190
191 /* Initialize sc_v to size of M */
192 ESL_ALLOC(sc_v, (sizeof(float) * (W+1)));
193 esl_vec_FSet(sc_v, (W+1), IMPOSSIBLE);
194
195 /* precalculate the initial scores for all cells */
196 init_scAA = FCalcInitDPScores(cm);
197
198 /* if do_null3: allocate and initialize act vector */
199 if(do_null3) {
200 ESL_ALLOC(act, sizeof(double *) * (W+1));
201 for(i = 0; i <= W; i++) {
202 ESL_ALLOC(act[i], sizeof(double) * cm->abc->K);
203 esl_vec_DSet(act[i], cm->abc->K, 0.);
204 }
205 }
206 else act = NULL;
207
208 /* initialize envelope boundary variables */
209 do_env_defn = (ret_envi != NULL || ret_envj != NULL) ? TRUE : FALSE;
210 envi = j0+1;
211 envj = i0-1;
212
213 /* The main loop: scan the sequence from position i0 to j0.
214 */
215 for (j = i0; j <= j0; j++)
216 {
217 float sc;
218 jp_g = j-i0+1; /* j is actual index in dsq, jp_g is offset j relative to start i0 (index in gamma* data structures) */
219 cur = j%2;
220 prv = (j-1)%2;
221 if(jp_g >= W) { dnA = dnAA[W]; dxA = dxAA[W]; }
222 else { dnA = dnAA[jp_g]; dxA = dxAA[jp_g]; }
223 /* precalcuate all possible rolling ptrs into the BEGL deck, so we don't wastefully recalc them inside inner DP loop */
224 for(d = 0; d <= W; d++) jp_wA[d] = (j-d)%(W+1);
225
226 /* if do_null3 (act != NULL), update act */
227 if(act != NULL) {
228 esl_vec_DCopy(act[(jp_g-1)%(W+1)], cm->abc->K, act[jp_g%(W+1)]);
229 esl_abc_DCount(cm->abc, act[jp_g%(W+1)], dsq[j], 1.);
230 /*printf("j: %3d jp_g: %3d jp_g/W: %3d act[0]: %.3f act[1]: %.3f act[2]: %.3f act[3]: %.3f\n", j, jp_g, jp_g%(W+1), act[jp_g%(W+1)][0], act[jp_g%(W+1)][1], act[jp_g%(W+1)][2], act[jp_g%(W+1)][3]);*/
231 }
232
233 for (v = cm->M-1; v > 0; v--) /* ...almost to ROOT; we handle ROOT specially... */
234 {
235 /* printf("dnA[v:%d]: %d\ndxA[v:%d]: %d\n", v, dnA[v], v, dxA[v]); */
236 if(cm->sttype[v] == E_st) continue;
237 float const *esc_v = esc_vAA[v];
238 float const *tsc_v = cm->tsc[v];
239 int emitmode = Emitmode(cm->sttype[v]);
240
241 /* float sc; */
242 jp_v = (cm->stid[v] == BEGL_S) ? (j % (W+1)) : cur;
243 jp_y = (StateRightDelta(cm->sttype[v]) > 0) ? prv : cur;
244 sd = StateDelta(cm->sttype[v]);
245 cnum = cm->cnum[v];
246 dn = dnA[v];
247 dx = dxA[v];
248 /* if we emit right, precalc score of emitting res j from state v */
249 float esc_j = IMPOSSIBLE;
250 if(cm->sttype[v] == IR_st || cm->sttype[v] == MR_st)
251 esc_j = esc_v[dsq[j]];
252
253 if(cm->sttype[v] == B_st) {
254 w = cm->cfirst[v]; /* BEGL_S */
255 y = cm->cnum[v]; /* BEGR_S */
256 for (d = dnA[v]; d <= dxA[v]; d++) {
257 /* k is the length of the right fragment */
258 if(do_banded) {
259 /* Careful, make sure k is consistent with bands in
260 * state w and state y, and don't forget that
261 * dmin/dmax values can exceed W. */
262 dn_y = ESL_MIN(dmin[y], smx->W);
263 dx_y = ESL_MIN(dmax[y], smx->W);
264 dn_w = ESL_MIN(dmin[w], smx->W);
265 dx_w = ESL_MIN(dmax[w], smx->W);
266 kmin = ESL_MAX(0, ESL_MAX(dn_y, d-dx_w));
267 kmax = ESL_MIN(dx_y, d-dn_w);
268 }
269 else { kmin = 0; kmax = d; }
270
271 sc = init_scAA[v][d-sd]; /* state delta (sd) is 0 for B_st */
272 for (k = kmin; k <= kmax; k++)
273 sc = ESL_MAX(sc, (alpha_begl[jp_wA[k]][w][d-k] + alpha[jp_y][y][k]));
274 alpha[jp_v][v][d] = sc;
275 /* careful: scores for w, the BEGL_S child of v, are in alpha_begl, not alpha */
276 }
277 }
278 else if (cm->stid[v] == BEGL_S) {
279 y = cm->cfirst[v];
280 for (d = dnA[v]; d <= dxA[v]; d++) {
281 sc = init_scAA[v][d-sd]; /* state delta (sd) is 0 for BEGL_S */
282 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++)
283 sc = ESL_MAX (sc, alpha[jp_y][y+yoffset][d - sd] + cm->tsc[v][yoffset]);
284 alpha_begl[jp_v][v][d] = sc;
285 /* careful: y is in alpha (all children of a BEGL_S must be non BEGL_S) */
286 }
287 }
288 else if (cm->sttype[v] == IL_st || cm->sttype[v] == IR_st) {
289 y = cm->cfirst[v];
290 dp_y = dn - sd; /* initial dp_y, we increment it at end of 'for(d = ...' loop */
291 i = j-dn+1; /* initial i, we decrement it when we access it, inside each possible case of the switch (cnum) below */
292
293 float const *arow0;
294 float const *arow1;
295 float const *arow2;
296 float const *arow3;
297 float const *arow4;
298 float const *arow5;
299
300 /* Note: order of cnum cases in switch and cases in each
301 * nested emitmode switch is based on empirical
302 * frequency in large test set, more frequent guys come
303 * earlier, so average num calcs in each switch is
304 * minimized.
305 */
306
307 switch (cnum) {
308 case 3:
309 arow0 = alpha[jp_y][y];
310 arow1 = alpha[jp_y][y+1];
311 arow2 = alpha[jp_y][y+2];
312 for (d = dn; d <= dx; d++, dp_y++) {
313 sc = ESL_MAX(arow2[dp_y] + tsc_v[2],
314 arow1[dp_y] + tsc_v[1]);
315 sc = ESL_MAX(sc, init_scAA[v][dp_y]);
316 sc = ESL_MAX(sc, arow0[dp_y] + tsc_v[0]);
317
318 switch (emitmode) {
319 case EMITLEFT:
320 sc += esc_v[dsq[i--]];
321 break;
322 case EMITRIGHT:
323 sc += esc_j;
324 break;
325 } /* end of switch (cm->sttype[v]) */
326 alpha[jp_v][v][d] = sc;
327 } /* end of for(d = dn; d <= dx; d++) */
328 break;
329
330 case 6:
331 arow0 = alpha[jp_y][y];
332 arow1 = alpha[jp_y][y+1];
333 arow2 = alpha[jp_y][y+2];
334 arow3 = alpha[jp_y][y+3];
335 arow4 = alpha[jp_y][y+4];
336 arow5 = alpha[jp_y][y+5];
337 for (d = dn; d <= dx; d++, dp_y++) {
338 sc = ESL_MAX(arow5[dp_y] + tsc_v[5],
339 init_scAA[v][dp_y]);
340 sc = ESL_MAX(sc, arow4[dp_y] + tsc_v[4]);
341 sc = ESL_MAX(sc, arow3[dp_y] + tsc_v[3]);
342 sc = ESL_MAX(sc, arow2[dp_y] + tsc_v[2]);
343 sc = ESL_MAX(sc, arow1[dp_y] + tsc_v[1]);
344 sc = ESL_MAX(sc, arow0[dp_y] + tsc_v[0]);
345
346 switch (emitmode) {
347 case EMITLEFT:
348 sc += esc_v[dsq[i--]];
349 break;
350 case EMITRIGHT:
351 sc += esc_j;
352 break;
353 } /* end of switch (cm->sttype[v]) */
354 alpha[jp_v][v][d] = sc;
355 } /* end of for(d = dn; d <= dx; d++) */
356 break;
357
358 case 4:
359 arow0 = alpha[jp_y][y];
360 arow1 = alpha[jp_y][y+1];
361 arow2 = alpha[jp_y][y+2];
362 arow3 = alpha[jp_y][y+3];
363 for (d = dn; d <= dx; d++, dp_y++) {
364 sc = ESL_MAX(arow3[dp_y] + tsc_v[3],
365 arow2[dp_y] + tsc_v[2]);
366 sc = ESL_MAX(sc, init_scAA[v][dp_y]);
367 sc = ESL_MAX(sc, arow1[dp_y] + tsc_v[1]);
368 sc = ESL_MAX(sc, arow0[dp_y] + tsc_v[0]);
369
370 switch (emitmode) {
371 case EMITLEFT:
372 sc += esc_v[dsq[i--]];
373 break;
374 case EMITRIGHT:
375 sc += esc_j;
376 break;
377 } /* end of switch (cm->sttype[v]) */
378 alpha[jp_v][v][d] = sc;
379 } /* end of for(d = dn; d <= dx; d++) */
380 break;
381
382 case 5:
383 arow0 = alpha[jp_y][y];
384 arow1 = alpha[jp_y][y+1];
385 arow2 = alpha[jp_y][y+2];
386 arow3 = alpha[jp_y][y+3];
387 arow4 = alpha[jp_y][y+4];
388 for (d = dn; d <= dx; d++, dp_y++) {
389 sc = ESL_MAX(arow4[dp_y] + tsc_v[4],
390 arow3[dp_y] + tsc_v[3]);
391 sc = ESL_MAX(sc, init_scAA[v][dp_y]);
392 sc = ESL_MAX(sc, arow1[dp_y] + tsc_v[1]);
393 sc = ESL_MAX(sc, arow2[dp_y] + tsc_v[2]);
394 sc = ESL_MAX(sc, arow0[dp_y] + tsc_v[0]);
395
396 switch (emitmode) {
397 case EMITRIGHT:
398 sc += esc_j;
399 break;
400 case EMITLEFT:
401 sc += esc_v[dsq[i--]];
402 break;
403 /* MP states can't have 5 children */
404 } /* end of switch (cm->sttype[v]) */
405 alpha[jp_v][v][d] = sc;
406 } /* end of for(d = dn; d <= dx; d++) */
407 break;
408
409 case 2:
410 arow0 = alpha[jp_y][y];
411 arow1 = alpha[jp_y][y+1];
412 for (d = dn; d <= dx; d++, dp_y++) {
413 sc = ESL_MAX(arow1[dp_y] + tsc_v[1],
414 init_scAA[v][dp_y]);
415 sc = ESL_MAX(sc, arow0[dp_y] + tsc_v[0]);
416 switch (emitmode) {
417 case EMITLEFT:
418 sc += esc_v[dsq[i--]];
419 break;
420 case EMITRIGHT:
421 sc += esc_j;
422 break;
423 } /* end of switch (cm->sttype[v]) */
424 alpha[jp_v][v][d] = sc;
425 } /* end of for(d = dn; d <= dx; d++) */
426 break;
427 } /* end of switch(cnum) */
428 /* for (d = dn; d <= dx; d++)
429 printf("alpha[j:%d][v:%d][d:%d]: %10.4f\n", j, v, d, alpha[jp_v][v][d]); */
430 } /* end of else if (v == IL_st || v == IR_st) */
431 else { /* this else is entered if cm->sttype[v] != B_st && cm->stid[v] != BEGL_S st && cm->sttype[v] != IL_st && cm->sttype[v] != IR_st */
432 y = cm->cfirst[v];
433 dp_y = dn - sd; /* initial dp_y, we increment it at end of 'for(d = ...' loop */
434 i = j-dn+1; /* initial i, we decrement it when we access it, inside each possible case of the switch (cnum) below */
435
436 float const *arow0;
437 float const *arow1;
438 float const *arow2;
439 float const *arow3;
440 float const *arow4;
441 float const *arow5;
442
443 /* Note: order of cnum cases in switch and cases in each
444 * nested emitmode switch is based on empirical
445 * frequency in large test set, more frequent guys come
446 * earlier, so average num calcs in each switch is
447 * minimized.
448 */
449
450 switch (cnum) {
451 case 3:
452 arow0 = alpha[jp_y][y];
453 arow1 = alpha[jp_y][y+1];
454 arow2 = alpha[jp_y][y+2];
455 for (d = dn; d <= dx; d++, dp_y++) {
456 sc_v[d] = ESL_MAX(arow2[dp_y] + tsc_v[2],
457 arow1[dp_y] + tsc_v[1]);
458 sc_v[d] = ESL_MAX(sc_v[d], init_scAA[v][dp_y]);
459 sc_v[d] = ESL_MAX(sc_v[d], arow0[dp_y] + tsc_v[0]);
460 } /* end of for(d = dn; d <= dx; d++) */
461 break;
462
463 case 6:
464 arow0 = alpha[jp_y][y];
465 arow1 = alpha[jp_y][y+1];
466 arow2 = alpha[jp_y][y+2];
467 arow3 = alpha[jp_y][y+3];
468 arow4 = alpha[jp_y][y+4];
469 arow5 = alpha[jp_y][y+5];
470 for (d = dn; d <= dx; d++, dp_y++) {
471 sc_v[d] = ESL_MAX(arow5[dp_y] + tsc_v[5],
472 init_scAA[v][dp_y]);
473 sc_v[d] = ESL_MAX(sc_v[d], arow4[dp_y] + tsc_v[4]);
474 sc_v[d] = ESL_MAX(sc_v[d], arow3[dp_y] + tsc_v[3]);
475 sc_v[d] = ESL_MAX(sc_v[d], arow2[dp_y] + tsc_v[2]);
476 sc_v[d] = ESL_MAX(sc_v[d], arow1[dp_y] + tsc_v[1]);
477 sc_v[d] = ESL_MAX(sc_v[d], arow0[dp_y] + tsc_v[0]);
478 } /* end of for(d = dn; d <= dx; d++) */
479 break;
480
481 case 4:
482 arow0 = alpha[jp_y][y];
483 arow1 = alpha[jp_y][y+1];
484 arow2 = alpha[jp_y][y+2];
485 arow3 = alpha[jp_y][y+3];
486 for (d = dn; d <= dx; d++, dp_y++) {
487 sc_v[d] = ESL_MAX(arow3[dp_y] + tsc_v[3],
488 arow2[dp_y] + tsc_v[2]);
489 sc_v[d] = ESL_MAX(sc_v[d], init_scAA[v][dp_y]);
490 sc_v[d] = ESL_MAX(sc_v[d], arow1[dp_y] + tsc_v[1]);
491 sc_v[d] = ESL_MAX(sc_v[d], arow0[dp_y] + tsc_v[0]);
492 } /* end of for(d = dn; d <= dx; d++) */
493 break;
494
495 case 5:
496 arow0 = alpha[jp_y][y];
497 arow1 = alpha[jp_y][y+1];
498 arow2 = alpha[jp_y][y+2];
499 arow3 = alpha[jp_y][y+3];
500 arow4 = alpha[jp_y][y+4];
501 for (d = dn; d <= dx; d++, dp_y++) {
502 sc_v[d] = ESL_MAX(arow4[dp_y] + tsc_v[4],
503 arow3[dp_y] + tsc_v[3]);
504 sc_v[d] = ESL_MAX(sc_v[d], init_scAA[v][dp_y]);
505 sc_v[d] = ESL_MAX(sc_v[d], arow1[dp_y] + tsc_v[1]);
506 sc_v[d] = ESL_MAX(sc_v[d], arow2[dp_y] + tsc_v[2]);
507 sc_v[d] = ESL_MAX(sc_v[d], arow0[dp_y] + tsc_v[0]);
508 } /* end of for (d = dn; d <= dx; d++, dp_y++) */
509 break;
510
511 case 2:
512 arow0 = alpha[jp_y][y];
513 arow1 = alpha[jp_y][y+1];
514 for (d = dn; d <= dx; d++, dp_y++) {
515 sc_v[d] = ESL_MAX(arow1[dp_y] + tsc_v[1],
516 init_scAA[v][dp_y]);
517 sc_v[d] = ESL_MAX(sc_v[d], arow0[dp_y] + tsc_v[0]);
518 }
519 break;
520 } /* end of switch(cnum) */
521 /* add in emission score (if any), and set alpha[jp_v][v][d] cell */
522 switch (emitmode) {
523 case EMITLEFT:
524 for (d = dn; d <= dx; d++) {
525 alpha[jp_v][v][d] = sc_v[d] + esc_v[dsq[i--]];
526 }
527 break;
528 case EMITNONE:
529 for (d = dn; d <= dx; d++)
530 alpha[jp_v][v][d] = sc_v[d];
531 break;
532 case EMITRIGHT:
533 for (d = dn; d <= dx; d++) {
534 alpha[jp_v][v][d] = sc_v[d] + esc_j;
535 }
536 break;
537 case EMITPAIR:
538 for (d = dn; d <= dx; d++) {
539 alpha[jp_v][v][d] = sc_v[d] + esc_v[dsq[i--]*cm->abc->Kp+dsq[j]];
540 }
541 break;
542 } /* end of switch (emitmode) */
543 } /* end of else (cm->sttype[v] != B_st && cm->stid[v] != BEGL_S st && cm->sttype[v] != IL_st && cm->sttype[v] != IR_st) */
544 if(vsc != NULL) {
545 if(cm->stid[v] != BEGL_S) for (d = dn; d <= dx; d++) vsc[v] = ESL_MAX(vsc[v], alpha[jp_v][v][d]);
546 else for (d = dn; d <= dx; d++) vsc[v] = ESL_MAX(vsc[v], alpha_begl[jp_v][v][d]);
547 }
548 /* if(cm->stid[v] != BEGL_S)
549 for (d = dn; d <= dx; d++) { printf("alpha[j:%4d][v:%4d][d:%4d]: %.5f\n", j, v, d, alpha[jp_v][v][d]); }*/
550 } /*loop over decks v>=0 */
551
552 /* Finish up with the ROOT_S, state v=0; and deal w/ local begins.
553 *
554 * If local begins are off, the hit must be rooted at v=0.
555 * With local begins on, the hit is rooted at the second state in
556 * the traceback (e.g. after 0), the internal entry point. Divide & conquer
557 * can only handle this if it's a non-insert state; this is guaranteed
558 * by the way local alignment is parameterized (other transitions are
559 * -INFTY), which is probably a little too fragile of a method.
560 */
561
562 float const *tsc_v = cm->tsc[0];
563 esl_vec_ISet(bestr, (W+1), -1);
564 esl_vec_FSet(bestsc, (W+1), IMPOSSIBLE);
565
566 jp_v = cur;
567 for (d = dnA[0]; d <= dxA[0]; d++) {
568 bestr[d] = 0; /* root of the traceback = root state 0 */
569 y = cm->cfirst[0];
570 alpha[jp_v][0][d] = ESL_MAX(IMPOSSIBLE, alpha[cur][y][d] + tsc_v[0]);
571 for (yoffset = 1; yoffset < cm->cnum[0]; yoffset++) {
572 alpha[jp_v][0][d] = ESL_MAX (alpha[jp_v][0][d], (alpha[cur][y+yoffset][d] + tsc_v[yoffset]));
573 }
574 }
575
576 if (cm->flags & CMH_LOCAL_BEGIN) {
577 for (y = 1; y < cm->M; y++) {
578 if(NOT_IMPOSSIBLE(cm->beginsc[y])) {
579 dn = ESL_MAX(dnA[0], dnA[y]);
580 dx = ESL_MIN(dxA[0], dxA[y]);
581 if(cm->stid[y] == BEGL_S)
582 {
583 jp_y = j % (W+1);
584 for (d = dn; d <= dx; d++) {
585 /* Is this more efficient:?
586 bestr[d] = (alpha[jp_v][0][d] > (alpha_begl[jp_y][y][d] + cm->beginsc[y])) ? bestr[d] : y;
587 alpha[jp_v][0][d] = ESL_MAX(alpha[jp_v][0][d], alpha_begl[jp_y][y][d] + cm->beginsc[y]); */
588 if(alpha[jp_v][0][d] < (alpha_begl[jp_y][y][d] + cm->beginsc[y])) {
589 alpha[jp_v][0][d] = alpha_begl[jp_y][y][d] + cm->beginsc[y];
590 bestr[d] = y;
591 }
592 }
593 }
594 else { /* y != BEGL_S */
595 jp_y = cur;
596 for (d = dn; d <= dx; d++) {
597 {
598 /* Is this more efficient:?
599 bestr[d] = (alpha[jp_v][0][d] > (alpha[jp_y][y][d] + cm->beginsc[y])) ? bestr[d] : y;
600 alpha[jp_v][0][d] = ESL_MAX(alpha[jp_v][0][d], alpha[jp_y][y][d] + cm->beginsc[y]); */
601 if(alpha[jp_v][0][d] < (alpha[jp_y][y][d] + cm->beginsc[y])) {
602 alpha[jp_v][0][d] = alpha[jp_y][y][d] + cm->beginsc[y];
603 bestr[d] = y;
604 }
605 }
606 }
607 }
608 }
609 }
610 }
611 /* fill in bestsc for all valid d values, and update vsc_root (best overall score) */
612 for (d = dnA[0]; d <= dxA[0]; d++) {
613 bestsc[d] = alpha[jp_v][0][d];
614 vsc_root = ESL_MAX(vsc_root, alpha[jp_v][0][d]);
615 /* Note: currently we NOT do a null3 correction for vsc_root */
616 }
617
618 /* update envi, envj, if nec */
619 if(do_env_defn) {
620 for (d = dnA[0]; d <= dxA[0]; d++) {
621 if(alpha[jp_v][0][d] >= env_cutoff) {
622 envi = ESL_MIN(envi, j-d+1);
623 envj = ESL_MAX(envj, j);
624 }
625 }
626 }
627
628 /* done with this endpoint j, if necessary, update gamma or tmp_hitlist */
629 if(gamma != NULL) {
630 if((status = UpdateGammaHitMx (cm, errbuf, PLI_PASS_STD_ANY, gamma, j, dnA[0], dxA[0], bestsc, bestr, NULL, W, act)) != eslOK) return status;
631 }
632 if(tmp_hitlist != NULL) {
633 if((status = ReportHitsGreedily(cm, errbuf, PLI_PASS_STD_ANY, j, dnA[0], dxA[0], bestsc, bestr, NULL, W, act, i0, j0, cutoff, tmp_hitlist)) != eslOK) return status;
634 }
635 /*cm_scan_mx_Dump(stdout, cm, j, i0, qdbidx, TRUE);*/
636 } /* end loop over end positions j */
637 if(vsc != NULL) vsc[0] = vsc_root;
638
639 /* set envelope return variables if nec */
640 if(ret_envi != NULL) { *ret_envi = (envi == j0+1) ? -1 : envi; }
641 if(ret_envj != NULL) { *ret_envj = (envj == i0-1) ? -1 : envj; }
642
643 /* If recovering hits in a non-greedy manner, do the gamma traceback, then free gamma */
644 if(gamma != NULL) {
645 TBackGammaHitMx(gamma, hitlist, i0, j0);
646 FreeGammaHitMx(gamma);
647 }
648 /* If reporting hits in a greedy manner, remove overlaps greedily from the tmp_hitlist
649 * then copy remaining hits to master <hitlist>. Then free tmp_hitlist.
650 */
651 if(tmp_hitlist != NULL) {
652 for(h = 0; h < tmp_hitlist->N; h++) tmp_hitlist->unsrt[h].srcL = j0; /* so overlaps can be removed */
653 cm_tophits_SortForOverlapRemoval(tmp_hitlist);
654 if((status = cm_tophits_RemoveOrMarkOverlaps(tmp_hitlist, FALSE, errbuf)) != eslOK) return status;
655 for(h = 0; h < tmp_hitlist->N; h++) {
656 if(! (tmp_hitlist->hit[h]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) {
657 if((status = cm_tophits_CloneHitMostly(tmp_hitlist, h, hitlist)) != eslOK) ESL_FAIL(status, errbuf, "problem copying hit to hitlist, out of memory?");
658 }
659 }
660 cm_tophits_Destroy(tmp_hitlist);
661 }
662
663 /* clean up and return */
664 if (act != NULL) {
665 for(i = 0; i <= W; i++) free(act[i]);
666 free(act);
667 }
668 free(jp_wA);
669 free(sc_v);
670 free(init_scAA[0]);
671 free(init_scAA);
672 if (ret_vsc != NULL) *ret_vsc = vsc;
673 else free(vsc);
674 if (ret_sc != NULL) *ret_sc = vsc_root;
675 ESL_DPRINTF1(("#DEBUG: FastCYKScan() return score: %10.4f\n", vsc_root));
676 return eslOK;
677
678 ERROR:
679 ESL_FAIL(eslEMEM, errbuf, "Memory allocation error.\n");
680 return status; /* NEVERREACHED */
681 }
682
683 /* Function: RefCYKScan()
684 * Date: EPN, Wed Sep 12 16:55:28 2007
685 *
686 * Purpose: Scan a sequence for matches to a covariance model, using
687 * a reference scanning CYK implementation. This function
688 * is slower but easier to understand than FastCYKScan().
689 *
690 * The choice of using one of two sets of query-dependent
691 * bands (QDBs) or not using QDBs is controlled by
692 * <qdbidx>. The QDBs are stored in <smx>.
693 *
694 * Args: cm - the covariance model
695 * errbuf - char buffer for reporting errors
696 * smx - CM_SCAN_MX for this search w/this model (incl. DP matrix, qdbands etc.)
697 * qdbidx - controls which QDBs to use: SMX_NOQDB | SMX_QDB1_TIGHT | SMX_QDB2_LOOSE
698 * dsq - the digitized sequence
699 * i0 - start of target subsequence (1 for full seq)
700 * j0 - end of target subsequence (L for full seq)
701 * cutoff - minimum score to report
702 * hitlist - CM_TOPHITS hitlist to add to; if NULL, don't add to it
703 * do_null3 - TRUE to do NULL3 score correction, FALSE not to
704 * env_cutoff - ret_envi..ret_envj will include all hits that exceed this bit sc
705 * ret_envi - RETURN: min position in any hit w/sc >= env_cutoff, set to -1 if no such hits exist, NULL if not wanted
706 * ret_envj - RETURN: max position in any hit w/sc >= env_cutoff, set to -1 if no such hits exist, NULL if not wanted
707 * ret_vsc - RETURN: [0..v..M-1] best score at each state v, NULL if not-wanted
708 * ret_sc - RETURN: score of best overall hit (vsc[0])
709 *
710 * Note: This function is heavily synchronized with RefIInsideScan() and RefCYKScan()
711 * any change to this function should be mirrored in those functions.
712 *
713 * Returns: eslOK on success and RETURN variables updated (or not if NULL).
714 * eslEINCOMPAT on contract violation, errbuf if filled with informative error message.
715 * eslEMEM if out of memory, errbuf if filled with informative error message.
716 */
717 int
718 RefCYKScan(CM_t *cm, char *errbuf, CM_SCAN_MX *smx, int qdbidx, ESL_DSQ *dsq, int64_t i0, int64_t j0, float cutoff, CM_TOPHITS *hitlist,
719 int do_null3, float env_cutoff, int64_t *ret_envi, int64_t *ret_envj, float **ret_vsc, float *ret_sc)
720 {
721 int status;
722 GammaHitMx_t *gamma = NULL; /* semi-HMM for hit resoultion */
723 float *vsc; /* best score for each state (float) */
724 float vsc_root = IMPOSSIBLE; /* best overall score (score at ROOT_S) */
725 int yoffset; /* offset to a child state */
726 int i,j; /* index of start/end positions in sequence, 0..L */
727 int d; /* a subsequence length, 0..W */
728 int k; /* used in bifurc calculations: length of right subseq */
729 int prv, cur; /* previous, current j row (0 or 1) */
730 int v, w, y; /* state indices */
731 int jp_v; /* offset j for state v */
732 int jp_y; /* offset j for state y */
733 int jp_g; /* offset j for gamma (j-i0+1) */
734 int kmin, kmax; /* for B_st's, min/max value consistent with bands*/
735 int L; /* length of the subsequence (j0-i0+1) */
736 int W; /* max d; max size of a hit, this is min(L, smx->W) */
737 int sd; /* StateDelta(cm->sttype[v]), # emissions from v */
738 int do_banded = FALSE; /* TRUE: use QDBs, FALSE: don't */
739 int *dnA, *dxA; /* tmp ptr to 1 row of dnAA, dxAA */
740 int dn, dx; /* minimum/maximum valid d for current state */
741 int dn_y, dx_y; /* minimum/maximum valid d for state y */
742 int dn_w, dx_w; /* minimum/maximum valid d for state w */
743 int *dmin; /* [0..v..cm->M-1] minimum d allowed for this state */
744 int *dmax; /* [0..v..cm->M-1] maximum d allowed for this state */
745 int *jp_wA; /* rolling pointer index for B states, gets precalc'ed */
746 float **init_scAA; /* [0..v..cm->M-1][0..d..W] initial score for each v, d for all j */
747 double **act; /* [0..j..W-1][0..a..abc->K-1], alphabet count, count of residue a in dsq from 1..jp where j = jp%(W+1) */
748 int do_env_defn; /* TRUE to calculate envi, envj, FALSE not to (TRUE if ret_envi != NULL or ret_envj != NULL */
749 int64_t envi, envj; /* min/max positions that exist in any hit with sc >= env_cutoff */
750 CM_TOPHITS *tmp_hitlist = NULL; /* temporary hitlist, containing possibly overlapping hits */
751 int h; /* counter over hits */
752
753 /*printf("in RefCYKScan() local: %s\n", (cm->flags & CMH_LOCAL_BEGIN) ? "TRUE" : "FALSE");*/
754
755 /* Contract check */
756 if(! (cm->flags & CMH_BITS)) ESL_FAIL(eslEINCOMPAT, errbuf, "RefCYKScan, CMH_BITS flag is not raised.\n");
757 if(j0 < i0) ESL_FAIL(eslEINCOMPAT, errbuf, "RefCYKScan, i0: %" PRId64 " j0: %" PRId64 "d\n", i0, j0);
758 if(dsq == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "RefCYKScan, dsq is NULL\n");
759 if(cm->search_opts & CM_SEARCH_INSIDE) ESL_FAIL(eslEINCOMPAT, errbuf, "RefCYKScan, CM_SEARCH_INSIDE flag raised");
760 if(smx == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "RefCYKScan, smx == NULL\n");
761 if(! smx->floats_valid) ESL_FAIL(eslEINCOMPAT, errbuf, "RefCYKScan, smx->floats_valid if FALSE");
762 if(cm->qdbinfo == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "RefCYKScan, cm->qdbinfo == NULL\n");
763
764 /* make pointers to the ScanMatrix/CM data for convenience */
765 float ***alpha = smx->falpha; /* [0..j..1][0..v..cm->M-1][0..d..W] alpha DP matrix, NULL for v == BEGL_S */
766 float ***alpha_begl = smx->falpha_begl; /* [0..j..W][0..v..cm->M-1][0..d..W] alpha DP matrix, NULL for v != BEGL_S */
767 int **dnAA = smx->dnAAA[qdbidx]; /* [0..j..W][0..v..cm->M-1] minimum d for v, j (for j > W use [W][v]) */
768 int **dxAA = smx->dxAAA[qdbidx]; /* [0..j..W][0..v..cm->M-1] maximum d for v, j (for j > W use [W][v]) */
769 float *bestsc = smx->bestsc; /* [0..d..W] best score for this d, recalc'ed for each j endpoint */
770 int *bestr = smx->bestr; /* [0..d..W] best root state (for local begins or 0) for this d, recalc'ed for each j endpoint */
771 float **esc_vAA = cm->oesc; /* [0..v..cm->M-1][0..a..(cm->abc->Kp | cm->abc->Kp**2)] optimized emission scores for v
772 * and all possible emissions a (including ambiguities) */
773
774 /* Determine if we're doing banded/non-banded and get pointers to
775 * dmin/dmax. (We only need dmin/dmax so we can compute kmin/kmax
776 * for B states.)
777 */
778 if (qdbidx == SMX_NOQDB) { do_banded = FALSE; dmin = NULL; dmax = NULL; }
779 else if(qdbidx == SMX_QDB1_TIGHT) { do_banded = TRUE; dmin = cm->qdbinfo->dmin1; dmax = cm->qdbinfo->dmax1; }
780 else if(qdbidx == SMX_QDB2_LOOSE) { do_banded = TRUE; dmin = cm->qdbinfo->dmin2; dmax = cm->qdbinfo->dmax2; }
781 else ESL_FAIL(eslEINCOMPAT, errbuf, "RefCYKScan, qdbidx is invalid");
782
783 L = j0-i0+1;
784 W = smx->W;
785 if (W > L) W = L;
786
787 /* initialize the scan matrix */
788 if((status = cm_scan_mx_InitializeFloats(cm, smx, errbuf)) != eslOK) return status;
789
790 /* set vsc array */
791 vsc = NULL;
792 if(ret_vsc != NULL) {
793 ESL_ALLOC(vsc, sizeof(float) * cm->M);
794 esl_vec_FSet(vsc, cm->M, IMPOSSIBLE);
795 }
796
797 /* If we were passed a master hitlist <hitlist>, either create a
798 * gamma hit matrix for resolving overlaps optimally (if
799 * cm->search_opts & CM_SEARCH_CMNOTGREEDY) or create a temporary
800 * hitlist that will store overlapping hits, in that case, we'll
801 * remove overlaps greedily before copying the hits to the master
802 * <hitlist>.
803 */
804 gamma = NULL;
805 tmp_hitlist = NULL;
806 if(hitlist != NULL) {
807 if(cm->search_opts & CM_SEARCH_CMNOTGREEDY) {
808 gamma = CreateGammaHitMx(L, i0, cutoff);
809 }
810 else {
811 tmp_hitlist = cm_tophits_Create();
812 }
813 }
814
815 /* allocate array for precalc'ed rolling ptrs into BEGL deck, filled inside 'for(j...' loop */
816 ESL_ALLOC(jp_wA, sizeof(float) * (W+1));
817
818 /* precalculate the initial scores for all cells */
819 init_scAA = FCalcInitDPScores(cm);
820
821 /* if do_null3: allocate and initialize act vector */
822 if(do_null3) {
823 ESL_ALLOC(act, sizeof(double *) * (W+1));
824 for(i = 0; i <= W; i++) {
825 ESL_ALLOC(act[i], sizeof(double) * cm->abc->K);
826 esl_vec_DSet(act[i], cm->abc->K, 0.);
827 }
828 }
829 else act = NULL;
830
831 /* initialize envelope boundary variables */
832 do_env_defn = (ret_envi != NULL || ret_envj != NULL) ? TRUE : FALSE;
833 envi = j0+1;
834 envj = i0-1;
835
836 /* The main loop: scan the sequence from position i0 to j0.
837 */
838 for (j = i0; j <= j0; j++)
839 {
840 float sc;
841 jp_g = j-i0+1; /* j is actual index in dsq, jp_g is offset j relative to start i0 (index in gamma* data structures) */
842 cur = j%2;
843 prv = (j-1)%2;
844 if(jp_g >= W) { dnA = dnAA[W]; dxA = dxAA[W]; }
845 else { dnA = dnAA[jp_g]; dxA = dxAA[jp_g]; }
846 /* precalcuate all possible rolling ptrs into the BEGL deck, so we don't wastefully recalc them inside inner DP loop */
847 for(d = 0; d <= W; d++) jp_wA[d] = (j-d)%(W+1);
848
849 /* if do_null3 (act != NULL), update act */
850 if(act != NULL) {
851 esl_vec_DCopy(act[(jp_g-1)%(W+1)], cm->abc->K, act[jp_g%(W+1)]);
852 esl_abc_DCount(cm->abc, act[jp_g%(W+1)], dsq[j], 1.);
853 /*printf("j: %3d jp_g: %3d jp_g/W: %3d act[0]: %.3f act[1]: %.3f act[2]: %.3f act[3]: %.3f\n", j, jp_g, jp_g%(W+1), act[jp_g%(W+1)][0], act[jp_g%(W+1)][1], act[jp_g%(W+1)][2], act[jp_g%(W+1)][3]);*/
854 }
855
856 for (v = cm->M-1; v > 0; v--) /* ...almost to ROOT; we handle ROOT specially... */
857 {
858 /* printf("dnA[v:%d]: %d\ndxA[v:%d]: %d\n", v, dnA[v], v, dxA[v]); */
859 if(cm->sttype[v] == E_st) continue;
860 float const *esc_v = esc_vAA[v];
861 float const *tsc_v = cm->tsc[v];
862 int emitmode = Emitmode(cm->sttype[v]);
863
864 /* float sc; */
865 jp_v = (cm->stid[v] == BEGL_S) ? (j % (W+1)) : cur;
866 jp_y = (StateRightDelta(cm->sttype[v]) > 0) ? prv : cur;
867 sd = StateDelta(cm->sttype[v]);
868 dn = dnA[v];
869 dx = dxA[v];
870 /* if we emit right, precalc score of emitting res j from state v */
871 float esc_j = IMPOSSIBLE;
872 if(cm->sttype[v] == IR_st || cm->sttype[v] == MR_st)
873 esc_j = esc_v[dsq[j]];
874
875 if(cm->sttype[v] == B_st) {
876 w = cm->cfirst[v]; /* BEGL_S */
877 y = cm->cnum[v]; /* BEGR_S */
878 for (d = dnA[v]; d <= dxA[v]; d++) {
879 /* k is the length of the right fragment */
880 if(do_banded) {
881 /* Careful, make sure k is consistent with bands in
882 * state w and state y, and don't forget that
883 * dmin/dmax values can exceed W. */
884 dn_y = ESL_MIN(dmin[y], smx->W);
885 dx_y = ESL_MIN(dmax[y], smx->W);
886 dn_w = ESL_MIN(dmin[w], smx->W);
887 dx_w = ESL_MIN(dmax[w], smx->W);
888 kmin = ESL_MAX(0, ESL_MAX(dn_y, d-dx_w));
889 kmax = ESL_MIN(dx_y, d-dn_w);
890 }
891 else { kmin = 0; kmax = d; }
892
893 sc = init_scAA[v][d-sd]; /* state delta (sd) is 0 for B_st */
894 for (k = kmin; k <= kmax; k++)
895 sc = ESL_MAX(sc, (alpha_begl[jp_wA[k]][w][d-k] + alpha[jp_y][y][k]));
896 alpha[jp_v][v][d] = sc;
897 /* careful: scores for w, the BEGL_S child of v, are in alpha_begl, not alpha */
898 }
899 }
900 else if (cm->stid[v] == BEGL_S) {
901 y = cm->cfirst[v];
902 for (d = dnA[v]; d <= dxA[v]; d++) {
903 sc = init_scAA[v][d-sd]; /* state delta (sd) is 0 for BEGL_S st */
904 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++)
905 sc = ESL_MAX(sc, alpha[jp_y][y+yoffset][d - sd] + tsc_v[yoffset]);
906 alpha_begl[jp_v][v][d] = sc;
907 /* careful: y is in alpha (all children of a BEGL_S must be non BEGL_S) */
908 }
909 }
910 else { /* ! B_st, ! BEGL_S st */
911 y = cm->cfirst[v];
912 i = j - dnA[v] + 1;
913 for (d = dnA[v]; d <= dxA[v]; d++) {
914 sc = init_scAA[v][d-sd];
915 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++)
916 sc = ESL_MAX(sc, alpha[jp_y][y+yoffset][d - sd] + tsc_v[yoffset]);
917
918 switch (emitmode) {
919 case EMITLEFT:
920 alpha[jp_v][v][d] = sc + esc_v[dsq[i--]];
921 break;
922 case EMITNONE:
923 alpha[jp_v][v][d] = sc;
924 break;
925 case EMITRIGHT:
926 alpha[jp_v][v][d] = sc + esc_j;
927 break;
928 case EMITPAIR:
929 alpha[jp_v][v][d] = sc + esc_v[dsq[i--]*cm->abc->Kp+dsq[j]];
930 break;
931 } /* end of switch emitmode */
932 } /* end of for d loop */
933 } /* end of else (which was entered if ! B_st && ! BEGL_S st) */
934 if(vsc != NULL) {
935 if(cm->stid[v] != BEGL_S) for (d = dn; d <= dx; d++) vsc[v] = ESL_MAX(vsc[v], alpha[jp_v][v][d]);
936 else for (d = dn; d <= dx; d++) vsc[v] = ESL_MAX(vsc[v], alpha_begl[jp_v][v][d]);
937 }
938 } /*loop over decks v>0 */
939
940 /* Finish up with the ROOT_S, state v=0; and deal w/ local begins.
941 *
942 * If local begins are off, the hit must be rooted at v=0.
943 * With local begins on, the hit is rooted at the second state in
944 * the traceback (e.g. after 0), the internal entry point. Divide & conquer
945 * can only handle this if it's a non-insert state; this is guaranteed
946 * by the way local alignment is parameterized (other transitions are
947 * -INFTY), which is probably a little too fragile of a method.
948 */
949
950 float const *tsc_v = cm->tsc[0];
951 esl_vec_ISet(bestr, (W+1), -1);
952 esl_vec_FSet(bestsc, (W+1), IMPOSSIBLE);
953
954 jp_v = cur;
955 for (d = dnA[0]; d <= dxA[0]; d++) {
956 bestr[d] = 0; /* root of the traceback = root state 0 */
957 y = cm->cfirst[0];
958 alpha[jp_v][0][d] = ESL_MAX(IMPOSSIBLE, alpha[cur][y][d] + tsc_v[0]);
959 for (yoffset = 1; yoffset < cm->cnum[0]; yoffset++)
960 alpha[jp_v][0][d] = ESL_MAX (alpha[jp_v][0][d], (alpha[cur][y+yoffset][d] + tsc_v[yoffset]));
961 }
962
963 if (cm->flags & CMH_LOCAL_BEGIN) {
964 for (y = 1; y < cm->M; y++) {
965 if(NOT_IMPOSSIBLE(cm->beginsc[y])) {
966 dn = ESL_MAX(dnA[0], dnA[y]);
967 dx = ESL_MIN(dxA[0], dxA[y]);
968 if(cm->stid[y] == BEGL_S)
969 {
970 jp_y = j % (W+1);
971 for (d = dn; d <= dx; d++) {
972 /* Is this more efficient:?
973 bestr[d] = (alpha[jp_v][0][d] > (alpha_begl[jp_y][y][d] + cm->beginsc[y])) ? bestr[d] : y;
974 alpha[jp_v][0][d] = ESL_MAX(alpha[jp_v][0][d], alpha_begl[jp_y][y][d] + cm->beginsc[y]); */
975 if(alpha[jp_v][0][d] < (alpha_begl[jp_y][y][d] + cm->beginsc[y])) {
976 alpha[jp_v][0][d] = alpha_begl[jp_y][y][d] + cm->beginsc[y];
977 bestr[d] = y;
978 }
979 }
980 }
981 else { /* y != BEGL_S */
982 jp_y = cur;
983 for (d = dn; d <= dx; d++) {
984 {
985 /* Is this more efficient:?
986 bestr[d] = (alpha[jp_v][0][d] > (alpha[jp_y][y][d] + cm->beginsc[y])) ? bestr[d] : y;
987 alpha[jp_v][0][d] = ESL_MAX(alpha[jp_v][0][d], alpha[jp_y][y][d] + cm->beginsc[y]); */
988 if(alpha[jp_v][0][d] < (alpha[jp_y][y][d] + cm->beginsc[y])) {
989 alpha[jp_v][0][d] = alpha[jp_y][y][d] + cm->beginsc[y];
990 bestr[d] = y;
991 }
992 }
993 }
994 }
995 }
996 }
997 }
998 /* fill in bestsc for all valid d values, and update vsc_root (best overall score) */
999 for (d = dnA[0]; d <= dxA[0]; d++) {
1000 bestsc[d] = alpha[jp_v][0][d];
1001 vsc_root = ESL_MAX(vsc_root, alpha[jp_v][0][d]);
1002 /* Note: currently we NOT do a null3 correction for vsc_root */
1003 }
1004
1005 /* update envi, envj, if nec */
1006 if(do_env_defn) {
1007 for (d = dnA[0]; d <= dxA[0]; d++) {
1008 if(alpha[jp_v][0][d] >= env_cutoff) {
1009 envi = ESL_MIN(envi, j-d+1);
1010 envj = ESL_MAX(envj, j);
1011 }
1012 }
1013 }
1014
1015 /* done with this endpoint j, if necessary, update gamma or tmp_hitlist */
1016 if(gamma != NULL) {
1017 if((status = UpdateGammaHitMx (cm, errbuf, PLI_PASS_STD_ANY, gamma, j, dnA[0], dxA[0], bestsc, bestr, NULL, W, act)) != eslOK) return status;
1018 }
1019 if(tmp_hitlist != NULL) {
1020 if((status = ReportHitsGreedily(cm, errbuf, PLI_PASS_STD_ANY, j, dnA[0], dxA[0], bestsc, bestr, NULL, W, act, i0, j0, cutoff, tmp_hitlist)) != eslOK) return status;
1021 }
1022
1023 /* cm_DumpScanMatrixAlpha(cm, si, j, i0, TRUE); */
1024 } /* end loop over end positions j */
1025 if(vsc != NULL) vsc[0] = vsc_root;
1026
1027 /* set envelope return variables if nec */
1028 if(ret_envi != NULL) { *ret_envi = (envi == j0+1) ? -1 : envi; }
1029 if(ret_envj != NULL) { *ret_envj = (envj == i0-1) ? -1 : envj; }
1030
1031 /* If recovering hits in a non-greedy manner, do the gamma traceback, then free gamma */
1032 if(gamma != NULL) {
1033 TBackGammaHitMx(gamma, hitlist, i0, j0);
1034 FreeGammaHitMx(gamma);
1035 }
1036 /* If reporting hits in a greedy manner, remove overlaps greedily from the tmp_hitlist
1037 * then copy remaining hits to master <hitlist>. Then free tmp_hitlist.
1038 */
1039 if(tmp_hitlist != NULL) {
1040 for(h = 0; h < tmp_hitlist->N; h++) tmp_hitlist->unsrt[h].srcL = j0; /* so overlaps can be removed */
1041 cm_tophits_SortForOverlapRemoval(tmp_hitlist);
1042 if((status = cm_tophits_RemoveOrMarkOverlaps(tmp_hitlist, FALSE, errbuf)) != eslOK) return status;
1043 for(h = 0; h < tmp_hitlist->N; h++) {
1044 if(! (tmp_hitlist->hit[h]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) {
1045 if((status = cm_tophits_CloneHitMostly(tmp_hitlist, h, hitlist)) != eslOK) ESL_FAIL(status, errbuf, "problem copying hit to hitlist, out of memory?");
1046 }
1047 }
1048 cm_tophits_Destroy(tmp_hitlist);
1049 }
1050
1051 /* clean up and return */
1052 if (act != NULL) {
1053 for(i = 0; i <= W; i++) free(act[i]);
1054 free(act);
1055 }
1056 free(jp_wA);
1057 free(init_scAA[0]);
1058 free(init_scAA);
1059 if (ret_vsc != NULL) *ret_vsc = vsc;
1060 else free(vsc);
1061 if (ret_sc != NULL) *ret_sc = vsc_root;
1062
1063 ESL_DPRINTF1(("#DEBUG: RefCYKScan() return score: %10.4f\n", vsc_root));
1064 return eslOK;
1065
1066 ERROR:
1067 ESL_FAIL(eslEMEM, errbuf, "Memory allocation error.\n");
1068 return status; /* NEVERREACHED */
1069 }
1070
1071 /* Function: FastIInsideScan()
1072 * Date: EPN, Tue Nov 6 05:42:44 2007
1073 *
1074 * Purpose: Scan a sequence for matches to a covariance model, using
1075 * an optimized scanning Inside implementation that uses
1076 * integer scores.
1077 *
1078 * The choice of using one of two sets of query-dependent
1079 * bands (QDBs) or not using QDBs is controlled by
1080 * <qdbidx>. The QDBs are stored in <smx>.
1081 *
1082 * Args: cm - the covariance model
1083 * errbuf - char buffer for reporting errors
1084 * smx - CM_SCAN_MX for this search w/this model (incl. DP matrix, qdbands etc.)
1085 * qdbidx - controls which QDBs to use: SMX_NOQDB | SMX_QDB1_TIGHT | SMX_QDB2_LOOSE
1086 * dsq - the digitized sequence
1087 * i0 - start of target subsequence (1 for full seq)
1088 * j0 - end of target subsequence (L for full seq)
1089 * cutoff - minimum score to report
1090 * hitlist - CM_TOPHITS to add to; if NULL, don't add to it
1091 * do_null3 - TRUE to do NULL3 score correction, FALSE not to
1092 * env_cutoff - ret_envi..ret_envj will include all hits that exceed this bit sc
1093 * ret_envi - RETURN: min position in any hit w/sc >= env_cutoff, set to -1 if no such hits exist, NULL if not wanted
1094 * ret_envj - RETURN: max position in any hit w/sc >= env_cutoff, set to -1 if no such hits exist, NULL if not wanted
1095 * ret_vsc - RETURN: [0..v..M-1] best score at each state v, NULL if not-wanted
1096 * ret_sc - RETURN: score of best overall hit (vsc[0])
1097 *
1098 * Note: This function is heavily synchronized with FastCYKScan() and FastFInsideScan(),
1099 * any change to this function should be mirrored in those functions.
1100 *
1101 * Returns: eslOK on success and RETURN variables updated (or not if NULL).
1102 * eslEINCOMPAT on contract violation, errbuf if filled with informative error message.
1103 * eslEMEM if out of memory, errbuf if filled with informative error message.
1104 */
1105 int
1106 FastIInsideScan(CM_t *cm, char *errbuf, CM_SCAN_MX *smx, int qdbidx, ESL_DSQ *dsq, int64_t i0, int64_t j0, float cutoff, CM_TOPHITS *hitlist,
1107 int do_null3, float env_cutoff, int64_t *ret_envi, int64_t *ret_envj, float **ret_vsc, float *ret_sc)
1108 {
1109 int status;
1110 GammaHitMx_t *gamma = NULL; /* semi-HMM for hit resoultion */
1111 float *vsc; /* best score for each state (float) */
1112 float vsc_root = IMPOSSIBLE; /* best overall score (score at ROOT_S) */
1113 int yoffset; /* offset to a child state */
1114 int i,j; /* index of start/end positions in sequence, 0..L */
1115 int d; /* a subsequence length, 0..W */
1116 int k; /* used in bifurc calculations: length of right subseq */
1117 int prv, cur; /* previous, current j row (0 or 1) */
1118 int v, w, y; /* state indices */
1119 int jp_v; /* offset j for state v */
1120 int jp_y; /* offset j for state y */
1121 int jp_g; /* offset j for gamma (j-i0+1) */
1122 int dp_y; /* offset d for state y */
1123 int kmin, kmax; /* for B_st's, min/max value consistent with bands*/
1124 int L; /* length of the subsequence (j0-i0+1) */
1125 int W; /* max d; max size of a hit, this is min(L, smx->W) */
1126 int sd; /* StateDelta(cm->sttype[v]), # emissions from v */
1127 int do_banded = FALSE; /* TRUE: use QDBs, FALSE: don't */
1128 int *dnA, *dxA; /* tmp ptr to 1 row of dnAA, dxAA */
1129 int dn, dx; /* minimum/maximum valid d for current state */
1130 int dn_y, dx_y; /* minimum/maximum valid d for state y */
1131 int dn_w, dx_w; /* minimum/maximum valid d for state w */
1132 int *dmin; /* [0..v..cm->M-1] minimum d allowed for this state */
1133 int *dmax; /* [0..v..cm->M-1] maximum d allowed for this state */
1134 int cnum; /* number of children for current state */
1135 int *jp_wA; /* rolling pointer index for B states, gets precalc'ed */
1136 int *sc_v; /* [0..d..W] temporary score vec for each d for current j & v */
1137 int **init_scAA; /* [0..v..cm->M-1][0..d..W] initial score for each v, d for all j */
1138 double **act; /* [0..j..W-1][0..a..abc->K-1], alphabet count, count of residue a in dsq from 1..jp where j = jp%(W+1) */
1139 int do_env_defn; /* TRUE to calculate envi, envj, FALSE not to (TRUE if ret_envi != NULL or ret_envj != NULL */
1140 int64_t envi, envj; /* min/max positions that exist in any hit with sc >= env_cutoff */
1141 CM_TOPHITS *tmp_hitlist = NULL; /* temporary hitlist, containing possibly overlapping hits */
1142 int h; /* counter over hits */
1143
1144 /*printf("in FastIInsideScan() local: %s\n", (cm->flags & CMH_LOCAL_BEGIN) ? "TRUE" : "FALSE");*/
1145
1146 /* Contract check */
1147 if(! (cm->flags & CMH_BITS)) ESL_FAIL(eslEINCOMPAT, errbuf, "FastIInsideScan, CMH_BITS flag is not raised.\n");
1148 if(j0 < i0) ESL_FAIL(eslEINCOMPAT, errbuf, "FastIInsideScan, i0: %" PRId64 " j0: %" PRId64 "d\n", i0, j0);
1149 if(dsq == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "FastIInsideScan, dsq is NULL\n");
1150 if(! (cm->search_opts & CM_SEARCH_INSIDE)) ESL_FAIL(eslEINCOMPAT, errbuf, "FastIInsideScan, CM_SEARCH_INSIDE flag not raised");
1151 if(smx == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "FastIInsideScan, smx == NULL\n");
1152 if(! smx->ints_valid) ESL_FAIL(eslEINCOMPAT, errbuf, "FastIInsideScan, smx->ints_valid if FALSE");
1153
1154 /* make pointers to the ScanMatrix/CM data for convenience */
1155 int ***alpha = smx->ialpha; /* [0..j..1][0..v..cm->M-1][0..d..W] alpha DP matrix, NULL for v == BEGL_S */
1156 int ***alpha_begl = smx->ialpha_begl; /* [0..j..W][0..v..cm->M-1][0..d..W] alpha DP matrix, NULL for v != BEGL_S */
1157 int **dnAA = smx->dnAAA[qdbidx]; /* [0..j..W][0..v..cm->M-1] minimum d for v, j (for j > W use [W][v]) */
1158 int **dxAA = smx->dxAAA[qdbidx]; /* [0..j..W][0..v..cm->M-1] maximum d for v, j (for j > W use [W][v]) */
1159 float *bestsc = smx->bestsc; /* [0..d..W] best score for this d, recalc'ed for each j endpoint */
1160 int *bestr = smx->bestr; /* [0..d..W] best root state (for local begins or 0) for this d, recalc'ed for each j endpoint */
1161 int **esc_vAA = cm->ioesc; /* [0..v..cm->M-1][0..a..(cm->abc->Kp | cm->abc->Kp**2)] optimized emission scores for v
1162 * and all possible emissions a (including ambiguities) */
1163
1164 /* Determine if we're doing banded/non-banded and get pointers to
1165 * dmin/dmax. (We only need dmin/dmax so we can compute kmin/kmax
1166 * for B states.)
1167 */
1168 if (qdbidx == SMX_NOQDB) { do_banded = FALSE; dmin = NULL; dmax = NULL; }
1169 else if(qdbidx == SMX_QDB1_TIGHT) { do_banded = TRUE; dmin = cm->qdbinfo->dmin1; dmax = cm->qdbinfo->dmax1; }
1170 else if(qdbidx == SMX_QDB2_LOOSE) { do_banded = TRUE; dmin = cm->qdbinfo->dmin2; dmax = cm->qdbinfo->dmax2; }
1171 else ESL_FAIL(eslEINCOMPAT, errbuf, "FastIInsideScan, qdbidx is invalid");
1172
1173 L = j0-i0+1;
1174 W = smx->W;
1175 if (W > L) W = L;
1176
1177 /* initialize the scan matrix */
1178 if((status = cm_scan_mx_InitializeIntegers(cm, smx, errbuf)) != eslOK) return status;
1179
1180 /* set vsc array */
1181 vsc = NULL;
1182 if(ret_vsc != NULL) {
1183 ESL_ALLOC(vsc, sizeof(float) * cm->M);
1184 esl_vec_FSet(vsc, cm->M, IMPOSSIBLE);
1185 }
1186
1187 /* If we were passed a master hitlist <hitlist>, either create a
1188 * gamma hit matrix for resolving overlaps optimally (if
1189 * cm->search_opts & CM_SEARCH_CMNOTGREEDY) or create a temporary
1190 * hitlist that will store overlapping hits, in that case, we'll
1191 * remove overlaps greedily before copying the hits to the master
1192 * <hitlist>.
1193 */
1194 gamma = NULL;
1195 tmp_hitlist = NULL;
1196 if(hitlist != NULL) {
1197 if(cm->search_opts & CM_SEARCH_CMNOTGREEDY) {
1198 gamma = CreateGammaHitMx(L, i0, cutoff);
1199 }
1200 else {
1201 tmp_hitlist = cm_tophits_Create();
1202 }
1203 }
1204
1205 /* allocate array for precalc'ed rolling ptrs into BEGL deck, filled inside 'for(j...' loop */
1206 ESL_ALLOC(jp_wA, sizeof(float) * (W+1));
1207
1208 /* Initialize sc_v to size of M */
1209 ESL_ALLOC(sc_v, (sizeof(float) * (W+1)));
1210 esl_vec_ISet(sc_v, (W+1), -INFTY);
1211
1212 /* precalculate the initial scores for all cells */
1213 init_scAA = ICalcInitDPScores(cm);
1214
1215 /* if do_null3: allocate and initialize act vector */
1216 if(do_null3) {
1217 ESL_ALLOC(act, sizeof(double *) * (W+1));
1218 for(i = 0; i <= W; i++) {
1219 ESL_ALLOC(act[i], sizeof(double) * cm->abc->K);
1220 esl_vec_DSet(act[i], cm->abc->K, 0.);
1221 }
1222 }
1223 else act = NULL;
1224
1225 /* initialize envelope boundary variables */
1226 do_env_defn = (ret_envi != NULL || ret_envj != NULL) ? TRUE : FALSE;
1227 envi = j0+1;
1228 envj = i0-1;
1229
1230 /* The main loop: scan the sequence from position i0 to j0.
1231 */
1232 for (j = i0; j <= j0; j++)
1233 {
1234 int sc;
1235 jp_g = j-i0+1; /* j is actual index in dsq, jp_g is offset j relative to start i0 (index in gamma* data structures) */
1236 cur = j%2;
1237 prv = (j-1)%2;
1238 if(jp_g >= W) { dnA = dnAA[W]; dxA = dxAA[W]; }
1239 else { dnA = dnAA[jp_g]; dxA = dxAA[jp_g]; }
1240 /* precalcuate all possible rolling ptrs into the BEGL deck, so we don't wastefully recalc them inside inner DP loop */
1241 for(d = 0; d <= W; d++) jp_wA[d] = (j-d)%(W+1);
1242 /* if do_null3 (act != NULL), update act */
1243 if(act != NULL) {
1244 esl_vec_DCopy(act[(jp_g-1)%(W+1)], cm->abc->K, act[jp_g%(W+1)]);
1245 esl_abc_DCount(cm->abc, act[jp_g%(W+1)], dsq[j], 1.);
1246 /*printf("j: %3d jp_g: %3d jp_g/W: %3d act[0]: %.3f act[1]: %.3f act[2]: %.3f act[3]: %.3f\n", j, jp_g, jp_g%(W+1), act[jp_g%(W+1)][0], act[jp_g%(W+1)][1], act[jp_g%(W+1)][2], act[jp_g%(W+1)][3]);*/
1247 }
1248 for (v = cm->M-1; v > 0; v--) /* ...almost to ROOT; we handle ROOT specially... */
1249 {
1250 /* printf("dnA[v:%d]: %d\ndxA[v:%d]: %d\n", v, dnA[v], v, dxA[v]); */
1251 if(cm->sttype[v] == E_st) continue;
1252 int const *esc_v = esc_vAA[v];
1253 int const *tsc_v = cm->itsc[v];
1254 int emitmode = Emitmode(cm->sttype[v]);
1255
1256 /* float sc; */
1257 jp_v = (cm->stid[v] == BEGL_S) ? (j % (W+1)) : cur;
1258 jp_y = (StateRightDelta(cm->sttype[v]) > 0) ? prv : cur;
1259 sd = StateDelta(cm->sttype[v]);
1260 cnum = cm->cnum[v];
1261 dn = dnA[v];
1262 dx = dxA[v];
1263 /* if we emit right, precalc score of emitting res j from state v */
1264 int esc_j = -INFTY;
1265 if(cm->sttype[v] == IR_st || cm->sttype[v] == MR_st)
1266 esc_j = esc_v[dsq[j]];
1267
1268 if(cm->sttype[v] == B_st) {
1269 w = cm->cfirst[v]; /* BEGL_S */
1270 y = cm->cnum[v]; /* BEGR_S */
1271 for (d = dnA[v]; d <= dxA[v]; d++) {
1272 /* k is the length of the right fragment */
1273 if(do_banded) {
1274 /* Careful, make sure k is consistent with bands in
1275 * state w and state y, and don't forget that
1276 * dmin/dmax values can exceed W. */
1277 dn_y = ESL_MIN(dmin[y], smx->W);
1278 dx_y = ESL_MIN(dmax[y], smx->W);
1279 dn_w = ESL_MIN(dmin[w], smx->W);
1280 dx_w = ESL_MIN(dmax[w], smx->W);
1281 kmin = ESL_MAX(0, ESL_MAX(dn_y, d-dx_w));
1282 kmax = ESL_MIN(dx_y, d-dn_w);
1283 }
1284 else { kmin = 0; kmax = d; }
1285
1286 sc = init_scAA[v][d-sd]; /* state delta (sd) is 0 for B st */
1287 for (k = kmin; k <= kmax; k++)
1288 sc = ILogsum(sc, (alpha_begl[jp_wA[k]][w][d-k] + alpha[jp_y][y][k]));
1289 alpha[jp_v][v][d] = sc;
1290 /* careful: scores for w, the BEGL_S child of v, are in alpha_begl, not alpha */
1291 }
1292 }
1293 else if (cm->stid[v] == BEGL_S) {
1294 y = cm->cfirst[v];
1295 for (d = dnA[v]; d <= dxA[v]; d++) {
1296 sc = init_scAA[v][d-sd]; /* state delta (sd) is 0 for BEGL_S */
1297 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++)
1298 sc = ILogsum (sc, alpha[jp_y][y+yoffset][d - sd] + tsc_v[yoffset]);
1299 alpha_begl[jp_v][v][d] = sc;
1300 /* careful: y is in alpha (all children of a BEGL_S must be non BEGL_S) */
1301 }
1302 }
1303 else if (cm->sttype[v] == IL_st || cm->sttype[v] == IR_st) {
1304 y = cm->cfirst[v];
1305 dp_y = dn - sd; /* initial dp_y, we increment it at end of 'for(d = ...' loop */
1306 i = j-dn+1; /* initial i, we decrement it when we access it, inside each possible case of the switch (cnum) below */
1307
1308 int const *arow0;
1309 int const *arow1;
1310 int const *arow2;
1311 int const *arow3;
1312 int const *arow4;
1313 int const *arow5;
1314
1315 /* Note: order of cnum cases in switch and cases in each
1316 * nested emitmode switch is based on empirical
1317 * frequency in large test set, more frequent guys come
1318 * earlier, so average num calcs in each switch is
1319 * minimized.
1320 */
1321
1322 switch (cnum) {
1323 case 3:
1324 arow0 = alpha[jp_y][y];
1325 arow1 = alpha[jp_y][y+1];
1326 arow2 = alpha[jp_y][y+2];
1327 for (d = dn; d <= dx; d++, dp_y++) {
1328 sc = ILogsum(arow2[dp_y] + tsc_v[2],
1329 arow1[dp_y] + tsc_v[1]);
1330 sc = ILogsum(sc, init_scAA[v][dp_y]);
1331 sc = ILogsum(sc, arow0[dp_y] + tsc_v[0]);
1332
1333 /* add in emission score, if any */
1334 switch (emitmode) {
1335 case EMITLEFT:
1336 sc += esc_v[dsq[i--]];
1337 break;
1338 case EMITRIGHT:
1339 sc += esc_j;
1340 break;
1341 } /* end of switch (cm->sttype[v]) */
1342 alpha[jp_v][v][d] = sc;
1343 } /* end of for(d = dn; d <= dx; d++) */
1344 break;
1345
1346 case 6:
1347 arow0 = alpha[jp_y][y];
1348 arow1 = alpha[jp_y][y+1];
1349 arow2 = alpha[jp_y][y+2];
1350 arow3 = alpha[jp_y][y+3];
1351 arow4 = alpha[jp_y][y+4];
1352 arow5 = alpha[jp_y][y+5];
1353 for (d = dn; d <= dx; d++, dp_y++) {
1354 sc = ILogsum(arow5[dp_y] + tsc_v[5],
1355 init_scAA[v][dp_y]);
1356 sc = ILogsum(sc, arow4[dp_y] + tsc_v[4]);
1357 sc = ILogsum(sc, arow3[dp_y] + tsc_v[3]);
1358 sc = ILogsum(sc, arow2[dp_y] + tsc_v[2]);
1359 sc = ILogsum(sc, arow1[dp_y] + tsc_v[1]);
1360 sc = ILogsum(sc, arow0[dp_y] + tsc_v[0]);
1361 /* add in emission score, if any */
1362 switch (emitmode) {
1363 case EMITLEFT:
1364 sc += esc_v[dsq[i--]];
1365 break;
1366 case EMITRIGHT:
1367 sc += esc_j;
1368 break;
1369 } /* end of switch (cm->sttype[v]) */
1370 alpha[jp_v][v][d] = sc;
1371 } /* end of for(d = dn; d <= dx; d++) */
1372 break;
1373
1374 case 4:
1375 arow0 = alpha[jp_y][y];
1376 arow1 = alpha[jp_y][y+1];
1377 arow2 = alpha[jp_y][y+2];
1378 arow3 = alpha[jp_y][y+3];
1379 for (d = dn; d <= dx; d++, dp_y++) {
1380 sc = ILogsum(arow3[dp_y] + tsc_v[3],
1381 arow2[dp_y] + tsc_v[2]);
1382 sc = ILogsum(sc, init_scAA[v][dp_y]);
1383 sc = ILogsum(sc, arow1[dp_y] + tsc_v[1]);
1384 sc = ILogsum(sc, arow0[dp_y] + tsc_v[0]);
1385
1386 /* add in emission score, if any */
1387 switch (emitmode) {
1388 case EMITLEFT:
1389 sc += esc_v[dsq[i--]];
1390 break;
1391 case EMITRIGHT:
1392 sc += esc_j;
1393 break;
1394 } /* end of switch (cm->sttype[v]) */
1395 alpha[jp_v][v][d] = sc;
1396 } /* end of for(d = dn; d <= dx; d++) */
1397 break;
1398
1399 case 5:
1400 arow0 = alpha[jp_y][y];
1401 arow1 = alpha[jp_y][y+1];
1402 arow2 = alpha[jp_y][y+2];
1403 arow3 = alpha[jp_y][y+3];
1404 arow4 = alpha[jp_y][y+4];
1405 for (d = dn; d <= dx; d++, dp_y++) {
1406 sc = ILogsum(arow4[dp_y] + tsc_v[4],
1407 arow3[dp_y] + tsc_v[3]);
1408 sc = ILogsum(sc, init_scAA[v][dp_y]);
1409 sc = ILogsum(sc, arow1[dp_y] + tsc_v[1]);
1410 sc = ILogsum(sc, arow2[dp_y] + tsc_v[2]);
1411 sc = ILogsum(sc, arow0[dp_y] + tsc_v[0]);
1412
1413 /* add in emission score, if any */
1414 switch (emitmode) {
1415 case EMITRIGHT:
1416 sc += esc_j;
1417 break;
1418 case EMITLEFT:
1419 sc += esc_v[dsq[i--]];
1420 break;
1421 /* MP states can't have 5 children */
1422 } /* end of switch (cm->sttype[v]) */
1423 alpha[jp_v][v][d] = sc;
1424 } /* end of for(d = dn; d <= dx; d++) */
1425 break;
1426
1427 case 2:
1428 arow0 = alpha[jp_y][y];
1429 arow1 = alpha[jp_y][y+1];
1430 for (d = dn; d <= dx; d++, dp_y++) {
1431 sc = ILogsum(arow1[dp_y] + tsc_v[1],
1432 init_scAA[v][dp_y]);
1433 sc = ILogsum(sc, arow0[dp_y] + tsc_v[0]);
1434 switch (emitmode) {
1435 case EMITLEFT:
1436 sc += esc_v[dsq[i--]];
1437 break;
1438 case EMITRIGHT:
1439 sc += esc_j;
1440 break;
1441 } /* end of switch (cm->sttype[v]) */
1442 alpha[jp_v][v][d] = sc;
1443 } /* end of for(d = dn; d <= dx; d++) */
1444 break;
1445 } /* end of switch(cnum) */
1446 /* for (d = dn; d <= dx; d++)
1447 printf("alpha[j:%d][v:%d][d:%d]: %10.4f\n", j, v, d, alpha[jp_v][v][d]); */
1448 } /* end of else if (v == IL_st || v == IR_st) */
1449 else { /* this else is entered if cm->sttype[v] != B_st && cm->stid[v] != BEGL_S st && cm->sttype[v] != IL_st && cm->sttype[v] != IR_st */
1450 y = cm->cfirst[v];
1451 dp_y = dn - sd; /* initial dp_y, we increment it at end of 'for(d = ...' loop */
1452 i = j-dn+1; /* initial i, we decrement it when we access it, inside each possible case of the switch (cnum) below */
1453
1454 int const *arow0;
1455 int const *arow1;
1456 int const *arow2;
1457 int const *arow3;
1458 int const *arow4;
1459 int const *arow5;
1460
1461 /* Note: order of cnum cases in switch and cases in each
1462 * nested emitmode switch is based on empirical
1463 * frequency in large test set, more frequent guys come
1464 * earlier, so average num calcs in each switch is
1465 * minimized.
1466 */
1467
1468 switch (cnum) {
1469 case 3:
1470 arow0 = alpha[jp_y][y];
1471 arow1 = alpha[jp_y][y+1];
1472 arow2 = alpha[jp_y][y+2];
1473 for (d = dn; d <= dx; d++, dp_y++) {
1474 sc_v[d] = ILogsum(arow2[dp_y] + tsc_v[2],
1475 arow1[dp_y] + tsc_v[1]);
1476 sc_v[d] = ILogsum(sc_v[d], init_scAA[v][dp_y]);
1477 sc_v[d] = ILogsum(sc_v[d], arow0[dp_y] + tsc_v[0]);
1478 } /* end of for(d = dn; d <= dx; d++) */
1479 break;
1480
1481 case 6:
1482 arow0 = alpha[jp_y][y];
1483 arow1 = alpha[jp_y][y+1];
1484 arow2 = alpha[jp_y][y+2];
1485 arow3 = alpha[jp_y][y+3];
1486 arow4 = alpha[jp_y][y+4];
1487 arow5 = alpha[jp_y][y+5];
1488 for (d = dn; d <= dx; d++, dp_y++) {
1489 sc_v[d] = ILogsum(arow5[dp_y] + tsc_v[5],
1490 init_scAA[v][dp_y]);
1491 sc_v[d] = ILogsum(sc_v[d], arow4[dp_y] + tsc_v[4]);
1492 sc_v[d] = ILogsum(sc_v[d], arow3[dp_y] + tsc_v[3]);
1493 sc_v[d] = ILogsum(sc_v[d], arow2[dp_y] + tsc_v[2]);
1494 sc_v[d] = ILogsum(sc_v[d], arow1[dp_y] + tsc_v[1]);
1495 sc_v[d] = ILogsum(sc_v[d], arow0[dp_y] + tsc_v[0]);
1496 } /* end of for(d = dn; d <= dx; d++) */
1497 break;
1498
1499 case 4:
1500 arow0 = alpha[jp_y][y];
1501 arow1 = alpha[jp_y][y+1];
1502 arow2 = alpha[jp_y][y+2];
1503 arow3 = alpha[jp_y][y+3];
1504 for (d = dn; d <= dx; d++, dp_y++) {
1505 sc_v[d] = ILogsum(arow3[dp_y] + tsc_v[3],
1506 arow2[dp_y] + tsc_v[2]);
1507 sc_v[d] = ILogsum(sc_v[d], init_scAA[v][dp_y]);
1508 sc_v[d] = ILogsum(sc_v[d], arow1[dp_y] + tsc_v[1]);
1509 sc_v[d] = ILogsum(sc_v[d], arow0[dp_y] + tsc_v[0]);
1510 } /* end of for(d = dn; d <= dx; d++) */
1511 break;
1512
1513 case 5:
1514 arow0 = alpha[jp_y][y];
1515 arow1 = alpha[jp_y][y+1];
1516 arow2 = alpha[jp_y][y+2];
1517 arow3 = alpha[jp_y][y+3];
1518 arow4 = alpha[jp_y][y+4];
1519 for (d = dn; d <= dx; d++, dp_y++) {
1520 sc_v[d] = ILogsum(arow4[dp_y] + tsc_v[4],
1521 arow3[dp_y] + tsc_v[3]);
1522 sc_v[d] = ILogsum(sc_v[d], init_scAA[v][dp_y]);
1523 sc_v[d] = ILogsum(sc_v[d], arow1[dp_y] + tsc_v[1]);
1524 sc_v[d] = ILogsum(sc_v[d], arow2[dp_y] + tsc_v[2]);
1525 sc_v[d] = ILogsum(sc_v[d], arow0[dp_y] + tsc_v[0]);
1526 } /* end of for (d = dn; d <= dx; d++, dp_y++) */
1527 break;
1528
1529 case 2:
1530 arow0 = alpha[jp_y][y];
1531 arow1 = alpha[jp_y][y+1];
1532 for (d = dn; d <= dx; d++, dp_y++) {
1533 sc_v[d] = ILogsum(arow1[dp_y] + tsc_v[1],
1534 init_scAA[v][dp_y]);
1535 sc_v[d] = ILogsum(sc_v[d], arow0[dp_y] + tsc_v[0]);
1536 }
1537 break;
1538 } /* end of switch(cnum) */
1539 /* add in emission score (if any), and set alpha[jp_v][v][d] cell */
1540 switch (emitmode) {
1541 case EMITLEFT:
1542 for (d = dn; d <= dx; d++) {
1543 alpha[jp_v][v][d] = sc_v[d] + esc_v[dsq[i--]];
1544 }
1545 break;
1546 case EMITNONE:
1547 for (d = dn; d <= dx; d++)
1548 alpha[jp_v][v][d] = sc_v[d];
1549 break;
1550 case EMITRIGHT:
1551 for (d = dn; d <= dx; d++) {
1552 alpha[jp_v][v][d] = sc_v[d] + esc_j;
1553 }
1554 break;
1555 case EMITPAIR:
1556 for (d = dn; d <= dx; d++) {
1557 alpha[jp_v][v][d] = sc_v[d] + esc_v[dsq[i--]*cm->abc->Kp+dsq[j]];
1558 }
1559 break;
1560 } /* end of switch (emitmode) */
1561 } /* end of else (cm->sttype[v] != B_st && cm->stid[v] != BEGL_S st && cm->sttype[v] != IL_st && cm->sttype[v] != IR_st) */
1562 if(vsc != NULL) {
1563 if(cm->stid[v] != BEGL_S) for (d = dn; d <= dx; d++) vsc[v] = ESL_MAX(vsc[v], Scorify(alpha[jp_v][v][d]));
1564 else for (d = dn; d <= dx; d++) vsc[v] = ESL_MAX(vsc[v], Scorify(alpha_begl[jp_v][v][d]));
1565 }
1566 /*if(cm->stid[v] != BEGL_S)
1567 for (d = dn; d <= dx; d++) { printf("alpha[j:%4d][v:%4d][d:%4d]: %.5f\n", j, v, d, alpha[jp_v][v][d]); }*/
1568 } /*loop over decks v>0 */
1569
1570 /* Finish up with the ROOT_S, state v=0; and deal w/ local begins.
1571 *
1572 * If local begins are off, the hit must be rooted at v=0.
1573 * With local begins on, the hit is rooted at the second state in
1574 * the traceback (e.g. after 0), the internal entry point. Divide & conquer
1575 * can only handle this if it's a non-insert state; this is guaranteed
1576 * by the way local alignment is parameterized (other transitions are
1577 * -INFTY), which is probably a little too fragile of a method.
1578 */
1579
1580 int const *tsc_v = cm->itsc[0];
1581 esl_vec_ISet(bestr, (W+1), -1);
1582 esl_vec_FSet(bestsc, (W+1), IMPOSSIBLE);
1583
1584 /* determine min/max d we're allowing for the root state and this position j */
1585 jp_v = cur;
1586 for (d = dnA[0]; d <= dxA[0]; d++) {
1587 bestr[d] = 0; /* root of the traceback = root state 0 */
1588 y = cm->cfirst[0];
1589 alpha[jp_v][0][d] = ESL_MAX(-INFTY, alpha[cur][y][d] + tsc_v[0]);
1590 for (yoffset = 1; yoffset < cm->cnum[0]; yoffset++)
1591 alpha[jp_v][0][d] = ILogsum (alpha[jp_v][0][d], (alpha[cur][y+yoffset][d] + tsc_v[yoffset]));
1592 }
1593
1594 if (cm->flags & CMH_LOCAL_BEGIN) {
1595 for (y = 1; y < cm->M; y++) {
1596 if(cm->ibeginsc[y] != -INFTY) {
1597 dn = ESL_MAX(dnA[0], dnA[y]);
1598 dx = ESL_MIN(dxA[0], dxA[y]);
1599 if(cm->stid[y] == BEGL_S) {
1600 jp_y = jp_wA[0];
1601 for (d = dn; d <= dx; d++) {
1602 /*alpha[jp_v][0][d] = ILogsum(alpha[jp_v][0][d], alpha_begl[jp_y][y][d] + cm->ibeginsc[y]);*/
1603 if(alpha[jp_v][0][d] < (alpha_begl[jp_y][y][d] + cm->ibeginsc[y])) {
1604 alpha[jp_v][0][d] = alpha_begl[jp_y][y][d] + cm->ibeginsc[y];
1605 bestr[d] = y;
1606 }
1607 }
1608 }
1609 else { /* y != BEGL_S */
1610 jp_y = cur;
1611 for (d = dn; d <= dx; d++) {
1612 /*alpha[jp_v][0][d] = ILogsum(alpha[jp_v][0][d], alpha[jp_y][y][d] + cm->ibeginsc[y]);*/
1613 if(alpha[jp_v][0][d] < (alpha[jp_y][y][d] + cm->ibeginsc[y])) {
1614 alpha[jp_v][0][d] = alpha[jp_y][y][d] + cm->ibeginsc[y];
1615 bestr[d] = y;
1616 }
1617 }
1618 }
1619 }
1620 }
1621 }
1622 /* fill in bestsc for all valid d values, and update vsc_root (best overall score) */
1623 for (d = dnA[0]; d <= dxA[0]; d++) {
1624 bestsc[d] = Scorify(alpha[jp_v][0][d]);
1625 vsc_root = ESL_MAX(vsc_root, Scorify(alpha[jp_v][0][d]));
1626 /* Note: currently we NOT do a null3 correction for vsc_root */
1627 }
1628
1629 /* update envi, envj, if nec */
1630 if(do_env_defn) {
1631 for (d = dnA[0]; d <= dxA[0]; d++) {
1632 if(alpha[jp_v][0][d] >= env_cutoff) {
1633 envi = ESL_MIN(envi, j-d+1);
1634 envj = ESL_MAX(envj, j);
1635 }
1636 }
1637 }
1638
1639 /* done with this endpoint j, if necessary, update gamma or tmp_hitlist */
1640 if(gamma != NULL) {
1641 if((status = UpdateGammaHitMx (cm, errbuf, PLI_PASS_STD_ANY, gamma, j, dnA[0], dxA[0], bestsc, bestr, NULL, W, act)) != eslOK) return status;
1642 }
1643 if(tmp_hitlist != NULL) {
1644 if((status = ReportHitsGreedily(cm, errbuf, PLI_PASS_STD_ANY, j, dnA[0], dxA[0], bestsc, bestr, NULL, W, act, i0, j0, cutoff, tmp_hitlist)) != eslOK) return status;
1645 }
1646 /* cm_scan_mx_Dump(stdout, cm, j, i0, qdbidx, FALSE); */
1647 } /* end loop over end positions j */
1648 if(vsc != NULL) vsc[0] = vsc_root;
1649
1650 /* set envelope return variables if nec */
1651 if(ret_envi != NULL) { *ret_envi = (envi == j0+1) ? -1 : envi; }
1652 if(ret_envj != NULL) { *ret_envj = (envj == i0-1) ? -1 : envj; }
1653
1654 /* If recovering hits in a non-greedy manner, do the gamma traceback, then free gamma */
1655 if(gamma != NULL) {
1656 TBackGammaHitMx(gamma, hitlist, i0, j0);
1657 FreeGammaHitMx(gamma);
1658 }
1659 /* If reporting hits in a greedy manner, remove overlaps greedily from the tmp_hitlist
1660 * then copy remaining hits to master <hitlist>. Then free tmp_hitlist.
1661 */
1662 if(tmp_hitlist != NULL) {
1663 for(h = 0; h < tmp_hitlist->N; h++) tmp_hitlist->unsrt[h].srcL = j0; /* so overlaps can be removed */
1664 cm_tophits_SortForOverlapRemoval(tmp_hitlist);
1665 if((status = cm_tophits_RemoveOrMarkOverlaps(tmp_hitlist, FALSE, errbuf)) != eslOK) return status;
1666 for(h = 0; h < tmp_hitlist->N; h++) {
1667 if(! (tmp_hitlist->hit[h]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) {
1668 if((status = cm_tophits_CloneHitMostly(tmp_hitlist, h, hitlist)) != eslOK) ESL_FAIL(status, errbuf, "problem copying hit to hitlist, out of memory?");
1669 }
1670 }
1671 cm_tophits_Destroy(tmp_hitlist);
1672 }
1673
1674 /* clean up and return */
1675 if (act != NULL) {
1676 for(i = 0; i <= W; i++) free(act[i]);
1677 free(act);
1678 }
1679 free(jp_wA);
1680 free(sc_v);
1681 free(init_scAA[0]);
1682 free(init_scAA);
1683 if (ret_vsc != NULL) *ret_vsc = vsc;
1684 else free(vsc);
1685 if (ret_sc != NULL) *ret_sc = vsc_root;
1686
1687 ESL_DPRINTF1(("#DEBUG: FastIInsideScan() return score: %10.4f\n", vsc_root));
1688 return eslOK;
1689
1690 ERROR:
1691 ESL_FAIL(eslEMEM, errbuf, "Memory allocation error.\n");
1692 return status; /* NEVERREACHED */
1693 }
1694
1695
1696
1697 /* Function: FastFInsideScan()
1698 * Date: EPN, Wed Sep 12 16:55:28 2007
1699 *
1700 * Purpose: Scan a sequence for matches to a covariance model, using
1701 * an optimized scanning Inside implementation that uses
1702 * float scores.
1703 *
1704 * The choice of using one of two sets of query-dependent
1705 * bands (QDBs) or not using QDBs is controlled by
1706 * <qdbidx>. The QDBs are stored in <smx>.
1707 *
1708 * Args: cm - the covariance model
1709 * errbuf - char buffer for reporting errors
1710 * smx - CM_SCAN_MX for this search w/this model (incl. DP matrix, qdbands etc.)
1711 * dsq - the digitized sequence
1712 * i0 - start of target subsequence (1 for full seq)
1713 * j0 - end of target subsequence (L for full seq)
1714 * cutoff - minimum score to report
1715 * hitlist - CM_TOPHITS to add to; if NULL, don't add to it
1716 * do_null3 - TRUE to do NULL3 score correction, FALSE not to
1717 * env_cutoff - ret_envi..ret_envj will include all hits that exceed this bit sc
1718 * ret_envi - RETURN: min position in any hit w/sc >= env_cutoff, set to -1 if no such hits exist, NULL if not wanted
1719 * ret_envj - RETURN: max position in any hit w/sc >= env_cutoff, set to -1 if no such hits exist, NULL if not wanted
1720 * ret_vsc - RETURN: [0..v..M-1] best score at each state v, NULL if not-wanted
1721 * ret_sc - RETURN: score of best overall hit (vsc[0])
1722 *
1723 * Note: This function is heavily synchronized with FastCYKScan() and FastIInsideScan(),
1724 * any change to this function should be mirrored in those functions.
1725 *
1726 * Returns: eslOK on success and RETURN variables updated (or not if NULL).
1727 * eslEINCOMPAT on contract violation, errbuf if filled with informative error message.
1728 * eslEMEM if out of memory, errbuf if filled with informative error message.
1729 */
1730 int
1731 FastFInsideScan(CM_t *cm, char *errbuf, CM_SCAN_MX *smx, int qdbidx, ESL_DSQ *dsq, int64_t i0, int64_t j0, float cutoff, CM_TOPHITS *hitlist,
1732 int do_null3, float env_cutoff, int64_t *ret_envi, int64_t *ret_envj, float **ret_vsc, float *ret_sc)
1733 {
1734 int status;
1735 GammaHitMx_t *gamma = NULL; /* semi-HMM for hit resoultion */
1736 float *vsc; /* best score for each state (float) */
1737 float vsc_root = IMPOSSIBLE; /* best overall score (score at ROOT_S) */
1738 int yoffset; /* offset to a child state */
1739 int i,j; /* index of start/end positions in sequence, 0..L */
1740 int d; /* a subsequence length, 0..W */
1741 int k; /* used in bifurc calculations: length of right subseq */
1742 int prv, cur; /* previous, current j row (0 or 1) */
1743 int v, w, y; /* state indices */
1744 int jp_v; /* offset j for state v */
1745 int jp_y; /* offset j for state y */
1746 int jp_g; /* offset j for gamma (j-i0+1) */
1747 int dp_y; /* offset d for state y */
1748 int kmin, kmax; /* for B_st's, min/max value consistent with bands*/
1749 int L; /* length of the subsequence (j0-i0+1) */
1750 int W; /* max d; max size of a hit, this is min(L, smx->W) */
1751 int sd; /* StateDelta(cm->sttype[v]), # emissions from v */
1752 int do_banded = FALSE; /* TRUE: use QDBs, FALSE: don't */
1753 int *dnA, *dxA; /* tmp ptr to 1 row of dnAA, dxAA */
1754 int dn, dx; /* minimum/maximum valid d for current state */
1755 int dn_y, dx_y; /* minimum/maximum valid d for state y */
1756 int dn_w, dx_w; /* minimum/maximum valid d for state w */
1757 int *dmin; /* [0..v..cm->M-1] minimum d allowed for this state */
1758 int *dmax; /* [0..v..cm->M-1] maximum d allowed for this state */
1759 int cnum; /* number of children for current state */
1760 int *jp_wA; /* rolling pointer index for B states, gets precalc'ed */
1761 float *sc_v; /* [0..d..W] temporary score vec for each d for current j & v */
1762 float **init_scAA; /* [0..v..cm->M-1][0..d..W] initial score for each v, d for all j */
1763 double **act; /* [0..j..W-1][0..a..abc->K-1], alphabet count, count of residue a in dsq from 1..jp where j = jp%(W+1) */
1764 int do_env_defn; /* TRUE to calculate envi, envj, FALSE not to (TRUE if ret_envi != NULL or ret_envj != NULL */
1765 int64_t envi, envj; /* min/max positions that exist in any hit with sc >= env_cutoff */
1766 CM_TOPHITS *tmp_hitlist = NULL; /* temporary hitlist, containing possibly overlapping hits */
1767 int h; /* counter over hits */
1768
1769 /*printf("in FastFInsideScan() local: %s\n", (cm->flags & CMH_LOCAL_BEGIN) ? "TRUE" : "FALSE");*/
1770
1771 /* Contract check */
1772 if(! (cm->flags & CMH_BITS)) ESL_FAIL(eslEINCOMPAT, errbuf, "FastFInsideScan, CMH_BITS flag is not raised.\n");
1773 if(j0 < i0) ESL_FAIL(eslEINCOMPAT, errbuf, "FastFInsideScan, i0: %" PRId64 " j0: %" PRId64 "d\n", i0, j0);
1774 if(dsq == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "FastFInsideScan, dsq is NULL\n");
1775 if(! (cm->search_opts & CM_SEARCH_INSIDE)) ESL_FAIL(eslEINCOMPAT, errbuf, "FastFInsideScan, CM_SEARCH_INSIDE flag not raised");
1776 if(smx == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "FastFInsideScan, smx == NULL\n");
1777 if(! smx->ints_valid) ESL_FAIL(eslEINCOMPAT, errbuf, "FastFInsideScan, smx->ints_valid if FALSE");
1778
1779 /* make pointers to the ScanMatrix/CM data for convenience */
1780 float ***alpha = smx->falpha; /* [0..j..1][0..v..cm->M-1][0..d..W] alpha DP matrix, NULL for v == BEGL_S */
1781 float ***alpha_begl = smx->falpha_begl; /* [0..j..W][0..v..cm->M-1][0..d..W] alpha DP matrix, NULL for v != BEGL_S */
1782 int **dnAA = smx->dnAAA[qdbidx]; /* [0..j..W][0..v..cm->M-1] minimum d for v, j (for j > W use [W][v]) */
1783 int **dxAA = smx->dxAAA[qdbidx]; /* [0..j..W][0..v..cm->M-1] maximum d for v, j (for j > W use [W][v]) */
1784 float *bestsc = smx->bestsc; /* [0..d..W] best score for this d, recalc'ed for each j endpoint */
1785 int *bestr = smx->bestr; /* [0..d..W] best root state (for local begins or 0) for this d, recalc'ed for each j endpoint */
1786 float **esc_vAA = cm->oesc; /* [0..v..cm->M-1][0..a..(cm->abc->Kp | cm->abc->Kp**2)] optimized emission scores for v
1787 * and all possible emissions a (including ambiguities) */
1788
1789 /* Determine if we're doing banded/non-banded and get pointers to
1790 * dmin/dmax. (We only need dmin/dmax so we can compute kmin/kmax
1791 * for B states.)
1792 */
1793 if (qdbidx == SMX_NOQDB) { do_banded = FALSE; dmin = NULL; dmax = NULL; }
1794 else if(qdbidx == SMX_QDB1_TIGHT) { do_banded = TRUE; dmin = cm->qdbinfo->dmin1; dmax = cm->qdbinfo->dmax1; }
1795 else if(qdbidx == SMX_QDB2_LOOSE) { do_banded = TRUE; dmin = cm->qdbinfo->dmin2; dmax = cm->qdbinfo->dmax2; }
1796 else ESL_FAIL(eslEINCOMPAT, errbuf, "FastFInsideScan, qdbidx is invalid");
1797
1798 L = j0-i0+1;
1799 W = smx->W;
1800 if (W > L) W = L;
1801
1802 /* initialize the scan matrix */
1803 if((status = cm_scan_mx_InitializeFloats(cm, smx, errbuf)) != eslOK) return status;
1804
1805 /* set vsc array */
1806 vsc = NULL;
1807 if(ret_vsc != NULL) {
1808 ESL_ALLOC(vsc, sizeof(float) * cm->M);
1809 esl_vec_FSet(vsc, cm->M, IMPOSSIBLE);
1810 }
1811
1812 /* If we were passed a master hitlist <hitlist>, either create a
1813 * gamma hit matrix for resolving overlaps optimally (if
1814 * cm->search_opts & CM_SEARCH_CMNOTGREEDY) or create a temporary
1815 * hitlist that will store overlapping hits, in that case, we'll
1816 * remove overlaps greedily before copying the hits to the master
1817 * <hitlist>.
1818 */
1819 gamma = NULL;
1820 tmp_hitlist = NULL;
1821 if(hitlist != NULL) {
1822 if(cm->search_opts & CM_SEARCH_CMNOTGREEDY) {
1823 gamma = CreateGammaHitMx(L, i0, cutoff);
1824 }
1825 else {
1826 tmp_hitlist = cm_tophits_Create();
1827 }
1828 }
1829
1830 /* allocate array for precalc'ed rolling ptrs into BEGL deck, filled inside 'for(j...' loop */
1831 ESL_ALLOC(jp_wA, sizeof(float) * (W+1));
1832
1833 /* Initialize sc_v to size of M */
1834 ESL_ALLOC(sc_v, (sizeof(float) * (W+1)));
1835 esl_vec_FSet(sc_v, (W+1), IMPOSSIBLE);
1836
1837 /* precalculate the initial scores for all cells */
1838 init_scAA = FCalcInitDPScores(cm);
1839
1840 /* if do_null3: allocate and initialize act vector */
1841 if(do_null3) {
1842 ESL_ALLOC(act, sizeof(double *) * (W+1));
1843 for(i = 0; i <= W; i++) {
1844 ESL_ALLOC(act[i], sizeof(double) * cm->abc->K);
1845 esl_vec_DSet(act[i], cm->abc->K, 0.);
1846 }
1847 }
1848 else act = NULL;
1849
1850 /* initialize envelope boundary variables */
1851 do_env_defn = (ret_envi != NULL || ret_envj != NULL) ? TRUE : FALSE;
1852 envi = j0+1;
1853 envj = i0-1;
1854
1855 /* The main loop: scan the sequence from position i0 to j0.
1856 */
1857 for (j = i0; j <= j0; j++)
1858 {
1859 float sc;
1860 jp_g = j-i0+1; /* j is actual index in dsq, jp_g is offset j relative to start i0 (index in gamma* data structures) */
1861 cur = j%2;
1862 prv = (j-1)%2;
1863 if(jp_g >= W) { dnA = dnAA[W]; dxA = dxAA[W]; }
1864 else { dnA = dnAA[jp_g]; dxA = dxAA[jp_g]; }
1865 /* precalcuate all possible rolling ptrs into the BEGL deck, so we don't wastefully recalc them inside inner DP loop */
1866 for(d = 0; d <= W; d++) jp_wA[d] = (j-d)%(W+1);
1867
1868 /* if do_null3 (act != NULL), update act */
1869 if(act != NULL) {
1870 esl_vec_DCopy(act[(jp_g-1)%(W+1)], cm->abc->K, act[jp_g%(W+1)]);
1871 esl_abc_DCount(cm->abc, act[jp_g%(W+1)], dsq[j], 1.);
1872 /*printf("j: %3d jp_g: %3d jp_g/W: %3d act[0]: %.3f act[1]: %.3f act[2]: %.3f act[3]: %.3f\n", j, jp_g, jp_g%(W+1), act[jp_g%(W+1)][0], act[jp_g%(W+1)][1], act[jp_g%(W+1)][2], act[jp_g%(W+1)][3]);*/
1873 }
1874
1875 for (v = cm->M-1; v > 0; v--) /* ...almost to ROOT; we handle ROOT specially... */
1876 {
1877 /* printf("dnA[v:%d]: %d\ndxA[v:%d]: %d\n", v, dnA[v], v, dxA[v]); */
1878 if(cm->sttype[v] == E_st) continue;
1879 float const *esc_v = esc_vAA[v];
1880 float const *tsc_v = cm->tsc[v];
1881 int emitmode = Emitmode(cm->sttype[v]);
1882
1883 /* float sc; */
1884 jp_v = (cm->stid[v] == BEGL_S) ? (j % (W+1)) : cur;
1885 jp_y = (StateRightDelta(cm->sttype[v]) > 0) ? prv : cur;
1886 sd = StateDelta(cm->sttype[v]);
1887 cnum = cm->cnum[v];
1888 dn = dnA[v];
1889 dx = dxA[v];
1890 /* if we emit right, precalc score of emitting res j from state v */
1891 float esc_j = IMPOSSIBLE;
1892 if(cm->sttype[v] == IR_st || cm->sttype[v] == MR_st)
1893 esc_j = esc_v[dsq[j]];
1894
1895 if(cm->sttype[v] == B_st) {
1896 w = cm->cfirst[v]; /* BEGL_S */
1897 y = cm->cnum[v]; /* BEGR_S */
1898 for (d = dnA[v]; d <= dxA[v]; d++) {
1899 /* k is the length of the right fragment */
1900 if(do_banded) {
1901 /* Careful, make sure k is consistent with bands in
1902 * state w and state y, and don't forget that
1903 * dmin/dmax values can exceed W. */
1904 dn_y = ESL_MIN(dmin[y], smx->W);
1905 dx_y = ESL_MIN(dmax[y], smx->W);
1906 dn_w = ESL_MIN(dmin[w], smx->W);
1907 dx_w = ESL_MIN(dmax[w], smx->W);
1908 kmin = ESL_MAX(0, ESL_MAX(dn_y, d-dx_w));
1909 kmax = ESL_MIN(dx_y, d-dn_w);
1910 }
1911 else { kmin = 0; kmax = d; }
1912
1913 sc = init_scAA[v][d-sd]; /* state delta (sd) is 0 for B_st */
1914 for (k = kmin; k <= kmax; k++)
1915 sc = FLogsum(sc, (alpha_begl[jp_wA[k]][w][d-k] + alpha[jp_y][y][k]));
1916 alpha[jp_v][v][d] = sc;
1917 /* careful: scores for w, the BEGL_S child of v, are in alpha_begl, not alpha */
1918 }
1919 }
1920 else if (cm->stid[v] == BEGL_S) {
1921 y = cm->cfirst[v];
1922 for (d = dnA[v]; d <= dxA[v]; d++) {
1923 sc = init_scAA[v][d-sd]; /* state delta (sd) is 0 for BEGL_S st */
1924 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++)
1925 sc = FLogsum (sc, alpha[jp_y][y+yoffset][d - sd] + tsc_v[yoffset]);
1926 alpha_begl[jp_v][v][d] = sc;
1927 /* careful: y is in alpha (all children of a BEGL_S must be non BEGL_S) */
1928 }
1929 }
1930 else if (cm->sttype[v] == IL_st || cm->sttype[v] == IR_st) {
1931 y = cm->cfirst[v];
1932 dp_y = dn - sd; /* initial dp_y, we increment it at end of 'for(d = ...' loop */
1933 i = j-dn+1; /* initial i, we decrement it when we access it, inside each possible case of the switch (cnum) below */
1934
1935 float const *arow0;
1936 float const *arow1;
1937 float const *arow2;
1938 float const *arow3;
1939 float const *arow4;
1940 float const *arow5;
1941
1942 /* Note: order of cnum cases in switch and cases in each
1943 * nested emitmode switch is based on empirical
1944 * frequency in large test set, more frequent guys come
1945 * earlier, so average num calcs in each switch is
1946 * minimized.
1947 */
1948
1949 switch (cnum) {
1950 case 3:
1951 arow0 = alpha[jp_y][y];
1952 arow1 = alpha[jp_y][y+1];
1953 arow2 = alpha[jp_y][y+2];
1954 for (d = dn; d <= dx; d++, dp_y++) {
1955 sc = FLogsum(arow2[dp_y] + tsc_v[2],
1956 arow1[dp_y] + tsc_v[1]);
1957 sc = FLogsum(sc, init_scAA[v][dp_y]);
1958 sc = FLogsum(sc, arow0[dp_y] + tsc_v[0]);
1959
1960 /* add in emission score, if any */
1961 switch (emitmode) {
1962 case EMITLEFT:
1963 sc += esc_v[dsq[i--]];
1964 break;
1965 case EMITRIGHT:
1966 sc += esc_j;
1967 break;
1968 } /* end of switch (cm->sttype[v]) */
1969 alpha[jp_v][v][d] = sc;
1970 } /* end of for(d = dn; d <= dx; d++) */
1971 break;
1972
1973 case 6:
1974 arow0 = alpha[jp_y][y];
1975 arow1 = alpha[jp_y][y+1];
1976 arow2 = alpha[jp_y][y+2];
1977 arow3 = alpha[jp_y][y+3];
1978 arow4 = alpha[jp_y][y+4];
1979 arow5 = alpha[jp_y][y+5];
1980 for (d = dn; d <= dx; d++, dp_y++) {
1981 sc = FLogsum(arow5[dp_y] + tsc_v[5],
1982 init_scAA[v][dp_y]);
1983 sc = FLogsum(sc, arow4[dp_y] + tsc_v[4]);
1984 sc = FLogsum(sc, arow3[dp_y] + tsc_v[3]);
1985 sc = FLogsum(sc, arow2[dp_y] + tsc_v[2]);
1986 sc = FLogsum(sc, arow1[dp_y] + tsc_v[1]);
1987 sc = FLogsum(sc, arow0[dp_y] + tsc_v[0]);
1988 /* add in emission score, if any */
1989 switch (emitmode) {
1990 case EMITLEFT:
1991 sc += esc_v[dsq[i--]];
1992 break;
1993 case EMITRIGHT:
1994 sc += esc_j;
1995 break;
1996 } /* end of switch (cm->sttype[v]) */
1997 alpha[jp_v][v][d] = sc;
1998 } /* end of for(d = dn; d <= dx; d++) */
1999 break;
2000
2001 case 4:
2002 arow0 = alpha[jp_y][y];
2003 arow1 = alpha[jp_y][y+1];
2004 arow2 = alpha[jp_y][y+2];
2005 arow3 = alpha[jp_y][y+3];
2006 for (d = dn; d <= dx; d++, dp_y++) {
2007 sc = FLogsum(arow3[dp_y] + tsc_v[3],
2008 arow2[dp_y] + tsc_v[2]);
2009 sc = FLogsum(sc, init_scAA[v][dp_y]);
2010 sc = FLogsum(sc, arow1[dp_y] + tsc_v[1]);
2011 sc = FLogsum(sc, arow0[dp_y] + tsc_v[0]);
2012
2013 /* add in emission score, if any */
2014 switch (emitmode) {
2015 case EMITLEFT:
2016 sc += esc_v[dsq[i--]];
2017 break;
2018 case EMITRIGHT:
2019 sc += esc_j;
2020 break;
2021 } /* end of switch (cm->sttype[v]) */
2022 alpha[jp_v][v][d] = sc;
2023 } /* end of for(d = dn; d <= dx; d++) */
2024 break;
2025
2026 case 5:
2027 arow0 = alpha[jp_y][y];
2028 arow1 = alpha[jp_y][y+1];
2029 arow2 = alpha[jp_y][y+2];
2030 arow3 = alpha[jp_y][y+3];
2031 arow4 = alpha[jp_y][y+4];
2032 for (d = dn; d <= dx; d++, dp_y++) {
2033 sc = FLogsum(arow4[dp_y] + tsc_v[4],
2034 arow3[dp_y] + tsc_v[3]);
2035 sc = FLogsum(sc, init_scAA[v][dp_y]);
2036 sc = FLogsum(sc, arow1[dp_y] + tsc_v[1]);
2037 sc = FLogsum(sc, arow2[dp_y] + tsc_v[2]);
2038 sc = FLogsum(sc, arow0[dp_y] + tsc_v[0]);
2039
2040 /* add in emission score, if any */
2041 switch (emitmode) {
2042 case EMITRIGHT:
2043 sc += esc_j;
2044 break;
2045 case EMITLEFT:
2046 sc += esc_v[dsq[i--]];
2047 break;
2048 /* MP states can't have 5 children */
2049 } /* end of switch (cm->sttype[v]) */
2050 alpha[jp_v][v][d] = sc;
2051 } /* end of for(d = dn; d <= dx; d++) */
2052 break;
2053
2054 case 2:
2055 arow0 = alpha[jp_y][y];
2056 arow1 = alpha[jp_y][y+1];
2057 for (d = dn; d <= dx; d++, dp_y++) {
2058 sc = FLogsum(arow1[dp_y] + tsc_v[1],
2059 init_scAA[v][dp_y]);
2060 sc = FLogsum(sc, arow0[dp_y] + tsc_v[0]);
2061 switch (emitmode) {
2062 case EMITLEFT:
2063 sc += esc_v[dsq[i--]];
2064 break;
2065 case EMITRIGHT:
2066 sc += esc_j;
2067 break;
2068 } /* end of switch (cm->sttype[v]) */
2069 alpha[jp_v][v][d] = sc;
2070 } /* end of for(d = dn; d <= dx; d++) */
2071 break;
2072 } /* end of switch(cnum) */
2073 /* for (d = dn; d <= dx; d++)
2074 printf("alpha[j:%d][v:%d][d:%d]: %10.4f\n", j, v, d, alpha[jp_v][v][d]); */
2075 } /* end of else if (v == IL_st || v == IR_st) */
2076 else { /* this else is entered if cm->sttype[v] != B_st && cm->stid[v] != BEGL_S st && cm->sttype[v] != IL_st && cm->sttype[v] != IR_st */
2077 y = cm->cfirst[v];
2078 dp_y = dn - sd; /* initial dp_y, we increment it at end of 'for(d = ...' loop */
2079 i = j-dn+1; /* initial i, we decrement it when we access it, inside each possible case of the switch (cnum) below */
2080
2081 float const *arow0;
2082 float const *arow1;
2083 float const *arow2;
2084 float const *arow3;
2085 float const *arow4;
2086 float const *arow5;
2087
2088 /* Note: order of cnum cases in switch and cases in each
2089 * nested emitmode switch is based on empirical
2090 * frequency in large test set, more frequent guys come
2091 * earlier, so average num calcs in each switch is
2092 * minimized.
2093 */
2094
2095 switch (cnum) {
2096 case 3:
2097 arow0 = alpha[jp_y][y];
2098 arow1 = alpha[jp_y][y+1];
2099 arow2 = alpha[jp_y][y+2];
2100 for (d = dn; d <= dx; d++, dp_y++) {
2101 sc_v[d] = FLogsum(arow2[dp_y] + tsc_v[2],
2102 arow1[dp_y] + tsc_v[1]);
2103 sc_v[d] = FLogsum(sc_v[d], init_scAA[v][dp_y]);
2104 sc_v[d] = FLogsum(sc_v[d], arow0[dp_y] + tsc_v[0]);
2105 } /* end of for(d = dn; d <= dx; d++) */
2106 break;
2107
2108 case 6:
2109 arow0 = alpha[jp_y][y];
2110 arow1 = alpha[jp_y][y+1];
2111 arow2 = alpha[jp_y][y+2];
2112 arow3 = alpha[jp_y][y+3];
2113 arow4 = alpha[jp_y][y+4];
2114 arow5 = alpha[jp_y][y+5];
2115 for (d = dn; d <= dx; d++, dp_y++) {
2116 sc_v[d] = FLogsum(arow5[dp_y] + tsc_v[5],
2117 init_scAA[v][dp_y]);
2118 sc_v[d] = FLogsum(sc_v[d], arow4[dp_y] + tsc_v[4]);
2119 sc_v[d] = FLogsum(sc_v[d], arow3[dp_y] + tsc_v[3]);
2120 sc_v[d] = FLogsum(sc_v[d], arow2[dp_y] + tsc_v[2]);
2121 sc_v[d] = FLogsum(sc_v[d], arow1[dp_y] + tsc_v[1]);
2122 sc_v[d] = FLogsum(sc_v[d], arow0[dp_y] + tsc_v[0]);
2123 } /* end of for(d = dn; d <= dx; d++) */
2124 break;
2125
2126 case 4:
2127 arow0 = alpha[jp_y][y];
2128 arow1 = alpha[jp_y][y+1];
2129 arow2 = alpha[jp_y][y+2];
2130 arow3 = alpha[jp_y][y+3];
2131 for (d = dn; d <= dx; d++, dp_y++) {
2132 sc_v[d] = FLogsum(arow3[dp_y] + tsc_v[3],
2133 arow2[dp_y] + tsc_v[2]);
2134 sc_v[d] = FLogsum(sc_v[d], init_scAA[v][dp_y]);
2135 sc_v[d] = FLogsum(sc_v[d], arow1[dp_y] + tsc_v[1]);
2136 sc_v[d] = FLogsum(sc_v[d], arow0[dp_y] + tsc_v[0]);
2137 } /* end of for(d = dn; d <= dx; d++) */
2138 break;
2139
2140 case 5:
2141 arow0 = alpha[jp_y][y];
2142 arow1 = alpha[jp_y][y+1];
2143 arow2 = alpha[jp_y][y+2];
2144 arow3 = alpha[jp_y][y+3];
2145 arow4 = alpha[jp_y][y+4];
2146 for (d = dn; d <= dx; d++, dp_y++) {
2147 sc_v[d] = FLogsum(arow4[dp_y] + tsc_v[4],
2148 arow3[dp_y] + tsc_v[3]);
2149 sc_v[d] = FLogsum(sc_v[d], init_scAA[v][dp_y]);
2150 sc_v[d] = FLogsum(sc_v[d], arow1[dp_y] + tsc_v[1]);
2151 sc_v[d] = FLogsum(sc_v[d], arow2[dp_y] + tsc_v[2]);
2152 sc_v[d] = FLogsum(sc_v[d], arow0[dp_y] + tsc_v[0]);
2153 } /* end of for (d = dn; d <= dx; d++, dp_y++) */
2154 break;
2155
2156 case 2:
2157 arow0 = alpha[jp_y][y];
2158 arow1 = alpha[jp_y][y+1];
2159 for (d = dn; d <= dx; d++, dp_y++) {
2160 sc_v[d] = FLogsum(arow1[dp_y] + tsc_v[1],
2161 init_scAA[v][dp_y]);
2162 sc_v[d] = FLogsum(sc_v[d], arow0[dp_y] + tsc_v[0]);
2163 }
2164 break;
2165 } /* end of switch(cnum) */
2166 /* add in emission score (if any), and set alpha[jp_v][v][d] cell */
2167 switch (emitmode) {
2168 case EMITLEFT:
2169 for (d = dn; d <= dx; d++) {
2170 alpha[jp_v][v][d] = sc_v[d] + esc_v[dsq[i--]];
2171 }
2172 break;
2173 case EMITNONE:
2174 for (d = dn; d <= dx; d++)
2175 alpha[jp_v][v][d] = sc_v[d];
2176 break;
2177 case EMITRIGHT:
2178 for (d = dn; d <= dx; d++) {
2179 alpha[jp_v][v][d] = sc_v[d] + esc_j;
2180 }
2181 break;
2182 case EMITPAIR:
2183 for (d = dn; d <= dx; d++) {
2184 alpha[jp_v][v][d] = sc_v[d] + esc_v[dsq[i--]*cm->abc->Kp+dsq[j]];
2185 }
2186 break;
2187 } /* end of switch (emitmode) */
2188 } /* end of else (cm->sttype[v] != B_st && cm->stid[v] != BEGL_S st && cm->sttype[v] != IL_st && cm->sttype[v] != IR_st) */
2189 if(vsc != NULL) {
2190 if(cm->stid[v] != BEGL_S) for (d = dn; d <= dx; d++) vsc[v] = ESL_MAX(vsc[v], alpha[jp_v][v][d]);
2191 else for (d = dn; d <= dx; d++) vsc[v] = ESL_MAX(vsc[v], alpha_begl[jp_v][v][d]);
2192 }
2193 /*if(cm->stid[v] != BEGL_S)
2194 for (d = dn; d <= dx; d++) { printf("alpha[j:%4d][v:%4d][d:%4d]: %.5f\n", j, v, d, alpha[jp_v][v][d]); }*/
2195 } /*loop over decks v>0 */
2196
2197 /* Finish up with the ROOT_S, state v=0; and deal w/ local begins.
2198 *
2199 * If local begins are off, the hit must be rooted at v=0.
2200 * With local begins on, the hit is rooted at the second state in
2201 * the traceback (e.g. after 0), the internal entry point. Divide & conquer
2202 * can only handle this if it's a non-insert state; this is guaranteed
2203 * by the way local alignment is parameterized (other transitions are
2204 * -INFTY), which is probably a little too fragile of a method.
2205 */
2206
2207 float const *tsc_v = cm->tsc[0];
2208 esl_vec_ISet(bestr, (W+1), -1);
2209 esl_vec_FSet(bestsc, (W+1), IMPOSSIBLE);
2210
2211 /* determine min/max d we're allowing for the root state and this position j */
2212 jp_v = cur;
2213 for (d = dnA[0]; d <= dxA[0]; d++) {
2214 bestr[d] = 0; /* root of the traceback = root state 0 */
2215 y = cm->cfirst[0];
2216 alpha[jp_v][0][d] = ESL_MAX(IMPOSSIBLE, alpha[cur][y][d] + tsc_v[0]);
2217 for (yoffset = 1; yoffset < cm->cnum[0]; yoffset++)
2218 alpha[jp_v][0][d] = FLogsum (alpha[jp_v][0][d], (alpha[cur][y+yoffset][d] + tsc_v[yoffset]));
2219 }
2220
2221 if (cm->flags & CMH_LOCAL_BEGIN) {
2222 for (y = 1; y < cm->M; y++) {
2223 if(NOT_IMPOSSIBLE(cm->beginsc[y])) {
2224 dn = ESL_MAX(dnA[0], dnA[y]);
2225 dx = ESL_MIN(dxA[0], dxA[y]);
2226 if(cm->stid[y] == BEGL_S) {
2227 jp_y = jp_wA[0];
2228 for (d = dn; d <= dx; d++) {
2229 /*alpha[jp_v][0][d] = ILogsum(alpha[jp_v][0][d], alpha_begl[jp_y][y][d] + cm->beginsc[y]);*/
2230 if(alpha[jp_v][0][d] < (alpha_begl[jp_y][y][d] + cm->beginsc[y])) {
2231 alpha[jp_v][0][d] = alpha_begl[jp_y][y][d] + cm->beginsc[y];
2232 bestr[d] = y;
2233 }
2234 }
2235 }
2236 else { /* y != BEGL_S */
2237 jp_y = cur;
2238 for (d = dn; d <= dx; d++) {
2239 /*alpha[jp_v][0][d] = FLogsum(alpha[jp_v][0][d], alpha[jp_y][y][d] + cm->beginsc[y]);*/
2240 if(alpha[jp_v][0][d] < (alpha[jp_y][y][d] + cm->beginsc[y])) {
2241 alpha[jp_v][0][d] = alpha[jp_y][y][d] + cm->beginsc[y];
2242 bestr[d] = y;
2243 }
2244 }
2245 }
2246 }
2247 }
2248 }
2249 /* fill in bestsc for all valid d values, and update vsc_root (best overall score) */
2250 for (d = dnA[0]; d <= dxA[0]; d++) {
2251 bestsc[d] = alpha[jp_v][0][d];
2252 vsc_root = ESL_MAX(vsc_root, alpha[jp_v][0][d]);
2253 /* Note: currently we NOT do a null3 correction for vsc_root */
2254 }
2255
2256 /* update envi, envj, if nec */
2257 if(do_env_defn) {
2258 for (d = dnA[0]; d <= dxA[0]; d++) {
2259 if(alpha[jp_v][0][d] >= env_cutoff) {
2260 envi = ESL_MIN(envi, j-d+1);
2261 envj = ESL_MAX(envj, j);
2262 }
2263 }
2264 }
2265
2266 /* done with this endpoint j, if necessary, update gamma or tmp_hitlist */
2267 if(gamma != NULL) {
2268 if((status = UpdateGammaHitMx (cm, errbuf, PLI_PASS_STD_ANY, gamma, j, dnA[0], dxA[0], bestsc, bestr, NULL, W, act)) != eslOK) return status;
2269 }
2270 if(tmp_hitlist != NULL) {
2271 if((status = ReportHitsGreedily(cm, errbuf, PLI_PASS_STD_ANY, j, dnA[0], dxA[0], bestsc, bestr, NULL, W, act, i0, j0, cutoff, tmp_hitlist)) != eslOK) return status;
2272 }
2273 /*FILE *fp; fp = fopen("tmp.ffins.smx", "w"); cm_scan_mx_Dump(fp, cm, j, i0, qdbidx, TRUE); fclose(fp); */
2274 } /* end loop over end positions j */
2275 if(vsc != NULL) vsc[0] = vsc_root;
2276
2277 /* set envelope return variables if nec */
2278 if(ret_envi != NULL) { *ret_envi = (envi == j0+1) ? -1 : envi; }
2279 if(ret_envj != NULL) { *ret_envj = (envj == i0-1) ? -1 : envj; }
2280
2281 /* If recovering hits in a non-greedy manner, do the gamma traceback, then free gamma */
2282 if(gamma != NULL) {
2283 TBackGammaHitMx(gamma, hitlist, i0, j0);
2284 FreeGammaHitMx(gamma);
2285 }
2286 /* If reporting hits in a greedy manner, remove overlaps greedily from the tmp_hitlist
2287 * then copy remaining hits to master <hitlist>. Then free tmp_hitlist.
2288 */
2289 if(tmp_hitlist != NULL) {
2290 for(h = 0; h < tmp_hitlist->N; h++) tmp_hitlist->unsrt[h].srcL = j0; /* so overlaps can be removed */
2291 cm_tophits_SortForOverlapRemoval(tmp_hitlist);
2292 if((status = cm_tophits_RemoveOrMarkOverlaps(tmp_hitlist, FALSE, errbuf)) != eslOK) return status;
2293 for(h = 0; h < tmp_hitlist->N; h++) {
2294 if(! (tmp_hitlist->hit[h]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) {
2295 if((status = cm_tophits_CloneHitMostly(tmp_hitlist, h, hitlist)) != eslOK) ESL_FAIL(status, errbuf, "problem copying hit to hitlist, out of memory?");
2296 }
2297 }
2298 cm_tophits_Destroy(tmp_hitlist);
2299 }
2300
2301 /* clean up and return */
2302 if (act != NULL) {
2303 for(i = 0; i <= W; i++) free(act[i]);
2304 free(act);
2305 }
2306 free(jp_wA);
2307 free(sc_v);
2308 free(init_scAA[0]);
2309 free(init_scAA);
2310 if (ret_vsc != NULL) *ret_vsc = vsc;
2311 else free(vsc);
2312 if (ret_sc != NULL) *ret_sc = vsc_root;
2313
2314 ESL_DPRINTF1(("#DEBUG: FastFInsideScan() return score: %10.4f\n", vsc_root));
2315 return eslOK;
2316
2317 ERROR:
2318 ESL_FAIL(eslEINCOMPAT, errbuf, "Memory allocation error.\n");
2319 return status; /* NEVERREACHED */
2320 }
2321
2322
2323 /* Function: RefIInsideScan()
2324 * Date: EPN, Tue Nov 6 06:13:35 2007
2325 *
2326 * Purpose: Scan a sequence for matches to a covariance model, using
2327 * a reference scanning Inside that uses integer scores.
2328 * This function is slower but easier to understand than
2329 * FastIInsideScan().
2330 *
2331 * The choice of using one of two sets of query-dependent
2332 * bands (QDBs) or not using QDBs is controlled by
2333 * <qdbidx>. The QDBs are stored in <smx>.
2334 *
2335 * Args: cm - the covariance model
2336 * errbuf - char buffer for reporting errors
2337 * smx - CM_SCAN_MX for this search w/this model (incl. DP matrix, qdbands etc.)
2338 * qdbidx - controls which QDBs to use: SMX_NOQDB | SMX_QDB1_TIGHT | SMX_QDB2_LOOSE
2339 * dsq - the digitized sequence
2340 * i0 - start of target subsequence (1 for full seq)
2341 * j0 - end of target subsequence (L for full seq)
2342 * cutoff - minimum score to report
2343 * hitlist - CM_TOPHITS hitlist to add to; if NULL, don't add to it
2344 * do_null3 - TRUE to do NULL3 score correction, FALSE not to
2345 * env_cutoff - ret_envi..ret_envj will include all hits that exceed this bit sc
2346 * ret_envi - RETURN: min position in any hit w/sc >= env_cutoff, set to -1 if no such hits exist, NULL if not wanted
2347 * ret_envj - RETURN: max position in any hit w/sc >= env_cutoff, set to -1 if no such hits exist, NULL if not wanted
2348 * ret_vsc - RETURN: [0..v..M-1] best score at each state v, NULL if not-wanted
2349 * ret_sc - RETURN: score of best overall hit (vsc[0])
2350 *
2351 * Note: This function is heavily synchronized with RefCYKScan() and RefFInsideScan(),
2352 * any change to this function should be mirrored in those functions.
2353 *
2354 * Returns: eslOK on success and RETURN variables updated (or not if NULL).
2355 * eslEINCOMPAT on contract violation, errbuf if filled with informative error message.
2356 * eslEMEM if out of memory, errbuf if filled with informative error message.
2357 */
2358 int
2359 RefIInsideScan(CM_t *cm, char *errbuf, CM_SCAN_MX *smx, int qdbidx, ESL_DSQ *dsq, int64_t i0, int64_t j0, float cutoff, CM_TOPHITS *hitlist,
2360 int do_null3, float env_cutoff, int64_t *ret_envi, int64_t *ret_envj, float **ret_vsc, float *ret_sc)
2361 {
2362 int status;
2363 GammaHitMx_t *gamma = NULL; /* semi-HMM for hit resoultion */
2364 float *vsc; /* best score for each state (float) */
2365 float vsc_root = IMPOSSIBLE; /* best overall score (score at ROOT_S) */
2366 int yoffset; /* offset to a child state */
2367 int i,j; /* index of start/end positions in sequence, 0..L */
2368 int d; /* a subsequence length, 0..W */
2369 int k; /* used in bifurc calculations: length of right subseq */
2370 int prv, cur; /* previous, current j row (0 or 1) */
2371 int v, w, y; /* state indices */
2372 int jp_v; /* offset j for state v */
2373 int jp_y; /* offset j for state y */
2374 int jp_g; /* offset j for gamma (j-i0+1) */
2375 int kmin, kmax; /* for B_st's, min/max value consistent with bands*/
2376 int L; /* length of the subsequence (j0-i0+1) */
2377 int W; /* max d; max size of a hit, this is min(L, smx->W) */
2378 int sd; /* StateDelta(cm->sttype[v]), # emissions from v */
2379 int do_banded = FALSE; /* TRUE: use QDBs, FALSE: don't */
2380 int *dnA, *dxA; /* tmp ptr to 1 row of dnAA, dxAA */
2381 int dn, dx; /* minimum/maximum valid d for current state */
2382 int dn_y, dx_y; /* minimum/maximum valid d for state y */
2383 int dn_w, dx_w; /* minimum/maximum valid d for state w */
2384 int *dmin; /* [0..v..cm->M-1] minimum d allowed for this state */
2385 int *dmax; /* [0..v..cm->M-1] maximum d allowed for this state */
2386 int *jp_wA; /* rolling pointer index for B states, gets precalc'ed */
2387 int **init_scAA; /* [0..v..cm->M-1][0..d..W] initial score for each v, d for all j */
2388 double **act; /* [0..j..W-1][0..a..abc->K-1], alphabet count, count of residue a in dsq from 1..jp where j = jp%(W+1) */
2389 int do_env_defn; /* TRUE to calculate envi, envj, FALSE not to (TRUE if ret_envi != NULL or ret_envj != NULL */
2390 int64_t envi, envj; /* min/max positions that exist in any hit with sc >= env_cutoff */
2391 CM_TOPHITS *tmp_hitlist = NULL; /* temporary hitlist, containing possibly overlapping hits */
2392 int h; /* counter over hits */
2393
2394
2395 /*printf("in RefIInsideScan() local: %s\n", (cm->flags & CMH_LOCAL_BEGIN) ? "TRUE" : "FALSE");*/
2396
2397 /* Contract check */
2398 if(! (cm->flags & CMH_BITS)) ESL_FAIL(eslEINCOMPAT, errbuf, "RefIInsideScan, CMH_BITS flag is not raised.\n");
2399 if(j0 < i0) ESL_FAIL(eslEINCOMPAT, errbuf, "RefIInsideScan, i0: %" PRId64 " j0: %" PRId64 "d\n", i0, j0);
2400 if(dsq == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "RefIInsideScan, dsq is NULL\n");
2401 if(! (cm->search_opts & CM_SEARCH_INSIDE)) ESL_FAIL(eslEINCOMPAT, errbuf, "RefIInsideScan, CM_SEARCH_INSIDE flag not raised");
2402 if(smx == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "RefIInsideScan, smx == NULL\n");
2403 if(! smx->ints_valid) ESL_FAIL(eslEINCOMPAT, errbuf, "RefIInsideScan, smx->ints_valid if FALSE");
2404
2405 /* make pointers to the ScanMatrix/CM data for convenience */
2406 int ***alpha = smx->ialpha; /* [0..j..1][0..v..cm->M-1][0..d..W] alpha DP matrix, NULL for v == BEGL_S */
2407 int ***alpha_begl = smx->ialpha_begl; /* [0..j..W][0..v..cm->M-1][0..d..W] alpha DP matrix, NULL for v != BEGL_S */
2408 int **dnAA = smx->dnAAA[qdbidx]; /* [0..j..W][0..v..cm->M-1] minimum d for v, j (for j > W use [W][v]) */
2409 int **dxAA = smx->dxAAA[qdbidx]; /* [0..j..W][0..v..cm->M-1] maximum d for v, j (for j > W use [W][v]) */
2410 float *bestsc = smx->bestsc; /* [0..d..W] best score for this d, recalc'ed for each j endpoint */
2411 int *bestr = smx->bestr; /* [0..d..W] best root state (for local begins or 0) for this d, recalc'ed for each j endpoint */
2412 int **esc_vAA = cm->ioesc; /* [0..v..cm->M-1][0..a..(cm->abc->Kp | cm->abc->Kp**2)] optimized emission scores for v
2413 * and all possible emissions a (including ambiguities) */
2414
2415 /* Determine if we're doing banded/non-banded and get pointers to
2416 * dmin/dmax. (We only need dmin/dmax so we can compute kmin/kmax
2417 * for B states.)
2418 */
2419 if (qdbidx == SMX_NOQDB) { do_banded = FALSE; dmin = NULL; dmax = NULL; }
2420 else if(qdbidx == SMX_QDB1_TIGHT) { do_banded = TRUE; dmin = cm->qdbinfo->dmin1; dmax = cm->qdbinfo->dmax1; }
2421 else if(qdbidx == SMX_QDB2_LOOSE) { do_banded = TRUE; dmin = cm->qdbinfo->dmin2; dmax = cm->qdbinfo->dmax2; }
2422 else ESL_FAIL(eslEINCOMPAT, errbuf, "RefIInsideScan, qdbidx is invalid");
2423
2424 L = j0-i0+1;
2425 W = smx->W;
2426 if (W > L) W = L;
2427
2428 /* initialize the scan matrix */
2429 if((status = cm_scan_mx_InitializeIntegers(cm, smx, errbuf)) != eslOK) return status;
2430
2431 /* set vsc array */
2432 vsc = NULL;
2433 if(ret_vsc != NULL) {
2434 ESL_ALLOC(vsc, sizeof(float) * cm->M);
2435 esl_vec_FSet(vsc, cm->M, IMPOSSIBLE);
2436 }
2437
2438 /* If we were passed a master hitlist <hitlist>, either create a
2439 * gamma hit matrix for resolving overlaps optimally (if
2440 * cm->search_opts & CM_SEARCH_CMNOTGREEDY) or create a temporary
2441 * hitlist that will store overlapping hits, in that case, we'll
2442 * remove overlaps greedily before copying the hits to the master
2443 * <hitlist>.
2444 */
2445 gamma = NULL;
2446 tmp_hitlist = NULL;
2447 if(hitlist != NULL) {
2448 if(cm->search_opts & CM_SEARCH_CMNOTGREEDY) {
2449 gamma = CreateGammaHitMx(L, i0, cutoff);
2450 }
2451 else {
2452 tmp_hitlist = cm_tophits_Create();
2453 }
2454 }
2455
2456 /* allocate array for precalc'ed rolling ptrs into BEGL deck, filled inside 'for(j...' loop */
2457 ESL_ALLOC(jp_wA, sizeof(float) * (W+1));
2458
2459 /* precalculate the initial scores for all cells */
2460 init_scAA = ICalcInitDPScores(cm);
2461
2462 /* if do_null3: allocate and initialize act vector */
2463 if(do_null3) {
2464 ESL_ALLOC(act, sizeof(double *) * (W+1));
2465 for(i = 0; i <= W; i++) {
2466 ESL_ALLOC(act[i], sizeof(double) * cm->abc->K);
2467 esl_vec_DSet(act[i], cm->abc->K, 0.);
2468 }
2469 }
2470 else act = NULL;
2471
2472 /* initialize envelope boundary variables */
2473 do_env_defn = (ret_envi != NULL || ret_envj != NULL) ? TRUE : FALSE;
2474 envi = j0+1;
2475 envj = i0-1;
2476
2477 /* The main loop: scan the sequence from position i0 to j0.
2478 */
2479 for (j = i0; j <= j0; j++)
2480 {
2481 int sc;
2482 jp_g = j-i0+1; /* j is actual index in dsq, jp_g is offset j relative to start i0 (index in gamma* data structures) */
2483 cur = j%2;
2484 prv = (j-1)%2;
2485 if(jp_g >= W) { dnA = dnAA[W]; dxA = dxAA[W]; }
2486 else { dnA = dnAA[jp_g]; dxA = dxAA[jp_g]; }
2487 /* precalcuate all possible rolling ptrs into the BEGL deck, so we don't wastefully recalc them inside inner DP loop */
2488 for(d = 0; d <= W; d++) jp_wA[d] = (j-d)%(W+1);
2489
2490 /* if do_null3 (act != NULL), update act */
2491 if(act != NULL) {
2492 esl_vec_DCopy(act[(jp_g-1)%(W+1)], cm->abc->K, act[jp_g%(W+1)]);
2493 esl_abc_DCount(cm->abc, act[jp_g%(W+1)], dsq[j], 1.);
2494 /*printf("j: %3d jp_g: %3d jp_g/W: %3d act[0]: %.3f act[1]: %.3f act[2]: %.3f act[3]: %.3f\n", j, jp_g, jp_g%(W+1), act[jp_g%(W+1)][0], act[jp_g%(W+1)][1], act[jp_g%(W+1)][2], act[jp_g%(W+1)][3]);*/
2495 }
2496
2497 for (v = cm->M-1; v > 0; v--) /* ...almost to ROOT; we handle ROOT specially... */
2498 {
2499 /* printf("dnA[v:%d]: %d\ndxA[v:%d]: %d\n", v, dnA[v], v, dxA[v]); */
2500 if(cm->sttype[v] == E_st) continue;
2501 int const *esc_v = esc_vAA[v];
2502 int const *tsc_v = cm->itsc[v];
2503 int emitmode = Emitmode(cm->sttype[v]);
2504
2505 /* float sc; */
2506 jp_v = (cm->stid[v] == BEGL_S) ? (j % (W+1)) : cur;
2507 jp_y = (StateRightDelta(cm->sttype[v]) > 0) ? prv : cur;
2508 sd = StateDelta(cm->sttype[v]);
2509 dn = dnA[v];
2510 dx = dxA[v];
2511 /* if we emit right, precalc score of emitting res j from state v */
2512 int esc_j = -INFTY;
2513 if(cm->sttype[v] == IR_st || cm->sttype[v] == MR_st)
2514 esc_j = esc_v[dsq[j]];
2515
2516 if(cm->sttype[v] == B_st) {
2517 w = cm->cfirst[v]; /* BEGL_S */
2518 y = cm->cnum[v]; /* BEGR_S */
2519 for (d = dnA[v]; d <= dxA[v]; d++) {
2520 /* k is the length of the right fragment */
2521 if(do_banded) {
2522 /* Careful, make sure k is consistent with bands in
2523 * state w and state y, and don't forget that
2524 * dmin/dmax values can exceed W. */
2525 dn_y = ESL_MIN(dmin[y], smx->W);
2526 dx_y = ESL_MIN(dmax[y], smx->W);
2527 dn_w = ESL_MIN(dmin[w], smx->W);
2528 dx_w = ESL_MIN(dmax[w], smx->W);
2529 kmin = ESL_MAX(0, ESL_MAX(dn_y, d-dx_w));
2530 kmax = ESL_MIN(dx_y, d-dn_w);
2531 }
2532 else { kmin = 0; kmax = d; }
2533
2534 sc = init_scAA[v][d-sd]; /* state delta (sd) is 0 for B_st */
2535 for (k = kmin; k <= kmax; k++)
2536 sc = ILogsum(sc, (alpha_begl[jp_wA[k]][w][d-k] + alpha[jp_y][y][k]));
2537 alpha[jp_v][v][d] = sc;
2538 /* careful: scores for w, the BEGL_S child of v, are in alpha_begl, not alpha */
2539 }
2540 }
2541 else if (cm->stid[v] == BEGL_S) {
2542 y = cm->cfirst[v];
2543 for (d = dnA[v]; d <= dxA[v]; d++) {
2544 sc = init_scAA[v][d-sd]; /* state delta (sd) is 0 for BEGL_S st */
2545 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++)
2546 sc = ILogsum(sc, alpha[jp_y][y+yoffset][d - sd] + tsc_v[yoffset]);
2547 alpha_begl[jp_v][v][d] = sc;
2548 /* careful: y is in alpha (all children of a BEGL_S must be non BEGL_S) */
2549 }
2550 }
2551 else { /* ! B_st, ! BEGL_S st */
2552 y = cm->cfirst[v];
2553 i = j - dnA[v] + 1;
2554 for (d = dnA[v]; d <= dxA[v]; d++) {
2555 sc = init_scAA[v][d-sd];
2556 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++)
2557 sc = ILogsum(sc, alpha[jp_y][y+yoffset][d - sd] + tsc_v[yoffset]);
2558
2559 switch (emitmode) {
2560 case EMITLEFT:
2561 alpha[jp_v][v][d] = sc + esc_v[dsq[i--]];
2562 break;
2563 case EMITNONE:
2564 alpha[jp_v][v][d] = sc;
2565 break;
2566 case EMITRIGHT:
2567 alpha[jp_v][v][d] = sc + esc_j;
2568 break;
2569 case EMITPAIR:
2570 alpha[jp_v][v][d] = sc + esc_v[dsq[i--]*cm->abc->Kp+dsq[j]];
2571 break;
2572 } /* end of switch emitmode */
2573 } /* end of for d loop */
2574 } /* end of else (which was entered if ! B_st && ! BEGL_S st) */
2575 if(vsc != NULL) {
2576 if(cm->stid[v] != BEGL_S) for (d = dn; d <= dx; d++) vsc[v] = ESL_MAX(vsc[v], Scorify(alpha[jp_v][v][d]));
2577 else for (d = dn; d <= dx; d++) vsc[v] = ESL_MAX(vsc[v], Scorify(alpha_begl[jp_v][v][d]));
2578 }
2579 } /*loop over decks v>0 */
2580
2581 /* Finish up with the ROOT_S, state v=0; and deal w/ local begins.
2582 *
2583 * If local begins are off, the hit must be rooted at v=0.
2584 * With local begins on, the hit is rooted at the second state in
2585 * the traceback (e.g. after 0), the internal entry point. Divide & conquer
2586 * can only handle this if it's a non-insert state; this is guaranteed
2587 * by the way local alignment is parameterized (other transitions are
2588 * -INFTY), which is probably a little too fragile of a method.
2589 */
2590
2591 int const *tsc_v = cm->itsc[0];
2592 esl_vec_ISet(bestr, (W+1), -1);
2593 esl_vec_FSet(bestsc, (W+1), IMPOSSIBLE);
2594
2595 /* determine min/max d we're allowing for the root state and this position j */
2596 jp_v = cur;
2597 for (d = dnA[0]; d <= dxA[0]; d++) {
2598 bestr[d] = 0; /* root of the traceback = root state 0 */
2599 y = cm->cfirst[0];
2600 alpha[jp_v][0][d] = ESL_MAX(-INFTY, alpha[cur][y][d] + tsc_v[0]);
2601 for (yoffset = 1; yoffset < cm->cnum[0]; yoffset++)
2602 alpha[jp_v][0][d] = ILogsum (alpha[jp_v][0][d], (alpha[cur][y+yoffset][d] + tsc_v[yoffset]));
2603 }
2604
2605 if (cm->flags & CMH_LOCAL_BEGIN) {
2606 for (y = 1; y < cm->M; y++) {
2607 if(cm->ibeginsc[y] != -INFTY) {
2608 dn = ESL_MAX(dnA[0], dnA[y]);
2609 dx = ESL_MIN(dxA[0], dxA[y]);
2610 if(cm->stid[y] == BEGL_S) {
2611 jp_y = j % (W+1);
2612 for (d = dn; d <= dx; d++) {
2613 /*alpha[jp_v][0][d] = ILogsum(alpha[jp_v][0][d], alpha_begl[jp_y][y][d] + cm->ibeginsc[y]);*/
2614 if(alpha[jp_v][0][d] < (alpha_begl[jp_y][y][d] + cm->ibeginsc[y])) {
2615 alpha[jp_v][0][d] = alpha_begl[jp_y][y][d] + cm->ibeginsc[y];
2616 bestr[d] = y;
2617 }
2618 }
2619 }
2620 else { /* y != BEGL_S */
2621 jp_y = cur;
2622 for (d = dn; d <= dx; d++) {
2623 /*alpha[jp_v][0][d] = ILogsum(alpha[jp_v][0][d], alpha[jp_y][y][d] + cm->ibeginsc[y]);*/
2624 if(alpha[jp_v][0][d] < (alpha[jp_y][y][d] + cm->ibeginsc[y])) {
2625 alpha[jp_v][0][d] = alpha[jp_y][y][d] + cm->ibeginsc[y];
2626 bestr[d] = y;
2627 }
2628 }
2629 }
2630 }
2631 }
2632 }
2633 /* fill in bestsc for all valid d values, and update vsc_root (best overall score) */
2634 for (d = dnA[0]; d <= dxA[0]; d++) {
2635 bestsc[d] = Scorify(alpha[jp_v][0][d]);
2636 vsc_root = ESL_MAX(vsc_root, Scorify(alpha[jp_v][0][d]));
2637 /* Note: currently we NOT do a null3 correction for vsc_root */
2638 }
2639
2640 /* update envi, envj, if nec */
2641 if(do_env_defn) {
2642 for (d = dnA[0]; d <= dxA[0]; d++) {
2643 if(alpha[jp_v][0][d] >= env_cutoff) {
2644 envi = ESL_MIN(envi, j-d+1);
2645 envj = ESL_MAX(envj, j);
2646 }
2647 }
2648 }
2649
2650 /* done with this endpoint j, if necessary, update gamma or tmp_hitlist */
2651 if(gamma != NULL) {
2652 if((status = UpdateGammaHitMx (cm, errbuf, PLI_PASS_STD_ANY, gamma, j, dnA[0], dxA[0], bestsc, bestr, NULL, W, act)) != eslOK) return status;
2653 }
2654 if(tmp_hitlist != NULL) {
2655 if((status = ReportHitsGreedily(cm, errbuf, PLI_PASS_STD_ANY, j, dnA[0], dxA[0], bestsc, bestr, NULL, W, act, i0, j0, cutoff, tmp_hitlist)) != eslOK) return status;
2656 }
2657
2658 /* cm_DumpScanMatrixAlpha(cm, si, j, i0, FALSE);*/
2659 } /* end loop over end positions j */
2660 if(vsc != NULL) vsc[0] = vsc_root;
2661
2662 /* set envelope return variables if nec */
2663 if(ret_envi != NULL) { *ret_envi = (envi == j0+1) ? -1 : envi; }
2664 if(ret_envj != NULL) { *ret_envj = (envj == i0-1) ? -1 : envj; }
2665
2666 /* If reporting hits in a greedy manner, remove overlaps greedily from the tmp_hitlist
2667 * then copy remaining hits to master <hitlist>. Then free tmp_hitlist.
2668 */
2669 if(tmp_hitlist != NULL) {
2670 for(h = 0; h < tmp_hitlist->N; h++) tmp_hitlist->unsrt[h].srcL = j0; /* so overlaps can be removed */
2671 cm_tophits_SortForOverlapRemoval(tmp_hitlist);
2672 if((status = cm_tophits_RemoveOrMarkOverlaps(tmp_hitlist, FALSE, errbuf)) != eslOK) return status;
2673 for(h = 0; h < tmp_hitlist->N; h++) {
2674 if(! (tmp_hitlist->hit[h]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) {
2675 if((status = cm_tophits_CloneHitMostly(tmp_hitlist, h, hitlist)) != eslOK) ESL_FAIL(status, errbuf, "problem copying hit to hitlist, out of memory?");
2676 }
2677 }
2678 cm_tophits_Destroy(tmp_hitlist);
2679 }
2680
2681 /* clean up and return */
2682 if (act != NULL) {
2683 for(i = 0; i <= W; i++) free(act[i]);
2684 free(act);
2685 }
2686 free(jp_wA);
2687 free(init_scAA[0]);
2688 free(init_scAA);
2689 if (ret_vsc != NULL) *ret_vsc = vsc;
2690 else free(vsc);
2691 if (ret_sc != NULL) *ret_sc = vsc_root;
2692
2693 ESL_DPRINTF1(("#DEBUG: RefIInsideScan() return score: %10.4f\n", vsc_root));
2694 return eslOK;
2695
2696 ERROR:
2697 ESL_FAIL(eslEMEM, errbuf, "Memory allocation error.\n");
2698 return status; /* NEVERREACHED */
2699 }
2700
2701 /* Function: RefFInsideScan()
2702 * Date: EPN, Sun Nov 4 16:02:17 2007
2703 *
2704 * Purpose: Scan a sequence for matches to a covariance model, using
2705 * a reference scanning Inside that uses float scores.
2706 * This function is slower but easier to understand than
2707 * FastIInsideScan().
2708 *
2709 * The choice of using one of two sets of query-dependent
2710 * bands (QDBs) or not using QDBs is controlled by
2711 * <qdbidx>. The QDBs are stored in <smx>.
2712 *
2713 * Args: cm - the covariance model
2714 * errbuf - char buffer for reporting errors
2715 * smx - CM_SCAN_MX for this search w/this model (incl. DP matrix, qdbands etc.)
2716 * qdbidx - controls which QDBs to use: SMX_NOQDB | SMX_QDB1_TIGHT | SMX_QDB2_LOOSE
2717 * dsq - the digitized sequence
2718 * i0 - start of target subsequence (1 for full seq)
2719 * j0 - end of target subsequence (L for full seq)
2720 * cutoff - minimum score to report
2721 * hitlist - CM_TOPHITS hitlist to add to; if NULL, don't add to it
2722 * do_null3 - TRUE to do NULL3 score correction, FALSE not to
2723 * env_cutoff - ret_envi..ret_envj will include all hits that exceed this bit sc
2724 * ret_envi - RETURN: min position in any hit w/sc >= env_cutoff, set to -1 if no such hits exist, NULL if not wanted
2725 * ret_envj - RETURN: max position in any hit w/sc >= env_cutoff, set to -1 if no such hits exist, NULL if not wanted
2726 * ret_vsc - RETURN: [0..v..M-1] best score at each state v, NULL if not-wanted
2727 * ret_sc - RETURN: score of best overall hit (vsc[0])
2728 *
2729 * Note: This function is heavily synchronized with RefCYKScan() and RefIInsideScan(),
2730 * any change to this function should be mirrored in those functions.
2731 *
2732 * Returns: eslOK on success and RETURN variables updated (or not if NULL).
2733 * eslEINCOMPAT on contract violation, errbuf if filled with informative error message.
2734 * eslEMEM if out of memory, errbuf if filled with informative error message.
2735 */
2736 int
2737 RefFInsideScan(CM_t *cm, char *errbuf, CM_SCAN_MX *smx, int qdbidx, ESL_DSQ *dsq, int64_t i0, int64_t j0, float cutoff, CM_TOPHITS *hitlist,
2738 int do_null3, float env_cutoff, int64_t *ret_envi, int64_t *ret_envj, float **ret_vsc, float *ret_sc)
2739 {
2740 int status;
2741 GammaHitMx_t *gamma = NULL; /* semi-HMM for hit resoultion */
2742 float *vsc; /* best score for each state (float) */
2743 float vsc_root = IMPOSSIBLE; /* best overall score (score at ROOT_S) */
2744 int yoffset; /* offset to a child state */
2745 int i,j; /* index of start/end positions in sequence, 0..L */
2746 int d; /* a subsequence length, 0..W */
2747 int k; /* used in bifurc calculations: length of right subseq */
2748 int prv, cur; /* previous, current j row (0 or 1) */
2749 int v, w, y; /* state indices */
2750 int jp_v; /* offset j for state v */
2751 int jp_y; /* offset j for state y */
2752 int jp_g; /* offset j for gamma (j-i0+1) */
2753 int kmin, kmax; /* for B_st's, min/max value consistent with bands*/
2754 int L; /* length of the subsequence (j0-i0+1) */
2755 int W; /* max d; max size of a hit, this is min(L, smx->W) */
2756 int sd; /* StateDelta(cm->sttype[v]), # emissions from v */
2757 int do_banded = FALSE; /* TRUE: use QDBs, FALSE: don't */
2758 int *dnA, *dxA; /* tmp ptr to 1 row of dnAA, dxAA */
2759 int dn, dx; /* minimum/maximum valid d for current state */
2760 int dn_y, dx_y; /* minimum/maximum valid d for state y */
2761 int dn_w, dx_w; /* minimum/maximum valid d for state w */
2762 int *dmin; /* [0..v..cm->M-1] minimum d allowed for this state */
2763 int *dmax; /* [0..v..cm->M-1] maximum d allowed for this state */
2764 int *jp_wA; /* rolling pointer index for B states, gets precalc'ed */
2765 float **init_scAA; /* [0..v..cm->M-1][0..d..W] initial score for each v, d for all j */
2766 double **act; /* [0..j..W-1][0..a..abc->K-1], alphabet count, count of residue a in dsq from 1..jp where j = jp%(W+1) */
2767 int do_env_defn; /* TRUE to calculate envi, envj, FALSE not to (TRUE if ret_envi != NULL or ret_envj != NULL */
2768 int64_t envi, envj; /* min/max positions that exist in any hit with sc >= env_cutoff */
2769 CM_TOPHITS *tmp_hitlist = NULL; /* temporary hitlist, containing possibly overlapping hits */
2770 int h; /* counter over hits */
2771
2772 /*printf("in RefFInsideScan() local: %s\n", (cm->flags & CMH_LOCAL_BEGIN) ? "TRUE" : "FALSE");*/
2773
2774 /* Contract check */
2775 if(! (cm->flags & CMH_BITS)) ESL_FAIL(eslEINCOMPAT, errbuf, "RefFInsideScan, CMH_BITS flag is not raised.\n");
2776 if(j0 < i0) ESL_FAIL(eslEINCOMPAT, errbuf, "RefFInsideScan, i0: %" PRId64 " j0: %" PRId64 "d\n", i0, j0);
2777 if(dsq == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "RefFInsideScan, dsq is NULL\n");
2778 if(! (cm->search_opts & CM_SEARCH_INSIDE)) ESL_FAIL(eslEINCOMPAT, errbuf, "RefFInsideScan, CM_SEARCH_INSIDE flag not raised");
2779 if(smx == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "RefFInsideScan, smx == NULL\n");
2780 if(! smx->floats_valid) ESL_FAIL(eslEINCOMPAT, errbuf, "RefFInsideScan, smx->floats_valid if FALSE");
2781
2782 /* make pointers to the ScanMatrix/CM data for convenience */
2783 float ***alpha = smx->falpha; /* [0..j..1][0..v..cm->M-1][0..d..W] alpha DP matrix, NULL for v == BEGL_S */
2784 float ***alpha_begl = smx->falpha_begl; /* [0..j..W][0..v..cm->M-1][0..d..W] alpha DP matrix, NULL for v != BEGL_S */
2785 int **dnAA = smx->dnAAA[qdbidx]; /* [0..j..W][0..v..cm->M-1] minimum d for v, j (for j > W use [W][v]) */
2786 int **dxAA = smx->dxAAA[qdbidx]; /* [0..j..W][0..v..cm->M-1] maximum d for v, j (for j > W use [W][v]) */
2787 float *bestsc = smx->bestsc; /* [0..d..W] best score for this d, recalc'ed for each j endpoint */
2788 int *bestr = smx->bestr; /* [0..d..W] best root state (for local begins or 0) for this d, recalc'ed for each j endpoint */
2789 float **esc_vAA = cm->oesc; /* [0..v..cm->M-1][0..a..(cm->abc->Kp | cm->abc->Kp**2)] optimized emission scores for v
2790 * and all possible emissions a (including ambiguities) */
2791
2792 /* Determine if we're doing banded/non-banded and get pointers to
2793 * dmin/dmax. (We only need dmin/dmax so we can compute kmin/kmax
2794 * for B states.)
2795 */
2796 if (qdbidx == SMX_NOQDB) { do_banded = FALSE; dmin = NULL; dmax = NULL; }
2797 else if(qdbidx == SMX_QDB1_TIGHT) { do_banded = TRUE; dmin = cm->qdbinfo->dmin1; dmax = cm->qdbinfo->dmax1; }
2798 else if(qdbidx == SMX_QDB2_LOOSE) { do_banded = TRUE; dmin = cm->qdbinfo->dmin2; dmax = cm->qdbinfo->dmax2; }
2799 else ESL_FAIL(eslEINCOMPAT, errbuf, "RefFInsideScan, qdbidx is invalid");
2800
2801 L = j0-i0+1;
2802 W = smx->W;
2803 if (W > L) W = L;
2804
2805 /* initialize the scan matrix */
2806 if((status = cm_scan_mx_InitializeFloats(cm, smx, errbuf)) != eslOK) return status;
2807
2808 /* set vsc array */
2809 vsc = NULL;
2810 if(ret_vsc != NULL) {
2811 ESL_ALLOC(vsc, sizeof(float) * cm->M);
2812 esl_vec_FSet(vsc, cm->M, IMPOSSIBLE);
2813 }
2814
2815 /* If we were passed a master hitlist <hitlist>, either create a
2816 * gamma hit matrix for resolving overlaps optimally (if
2817 * cm->search_opts & CM_SEARCH_CMNOTGREEDY) or create a temporary
2818 * hitlist that will store overlapping hits, in that case, we'll
2819 * remove overlaps greedily before copying the hits to the master
2820 * <hitlist>.
2821 */
2822 gamma = NULL;
2823 tmp_hitlist = NULL;
2824 if(hitlist != NULL) {
2825 if(cm->search_opts & CM_SEARCH_CMNOTGREEDY) {
2826 gamma = CreateGammaHitMx(L, i0, cutoff);
2827 }
2828 else {
2829 tmp_hitlist = cm_tophits_Create();
2830 }
2831 }
2832
2833 /* allocate array for precalc'ed rolling ptrs into BEGL deck, filled inside 'for(j...' loop */
2834 ESL_ALLOC(jp_wA, sizeof(float) * (W+1));
2835
2836 /* precalculate the initial scores for all cells */
2837 init_scAA = FCalcInitDPScores(cm);
2838
2839 /* if do_null3: allocate and initialize act vector */
2840 if(do_null3) {
2841 ESL_ALLOC(act, sizeof(double *) * (W+1));
2842 for(i = 0; i <= W; i++) {
2843 ESL_ALLOC(act[i], sizeof(double) * cm->abc->K);
2844 esl_vec_DSet(act[i], cm->abc->K, 0.);
2845 }
2846 }
2847 else act = NULL;
2848
2849 /* initialize envelope boundary variables */
2850 do_env_defn = (ret_envi != NULL || ret_envj != NULL) ? TRUE : FALSE;
2851 envi = j0+1;
2852 envj = i0-1;
2853
2854 /* The main loop: scan the sequence from position i0 to j0.
2855 */
2856 for (j = i0; j <= j0; j++)
2857 {
2858 float sc;
2859 jp_g = j-i0+1; /* j is actual index in dsq, jp_g is offset j relative to start i0 (index in gamma* data structures) */
2860 cur = j%2;
2861 prv = (j-1)%2;
2862 if(jp_g >= W) { dnA = dnAA[W]; dxA = dxAA[W]; }
2863 else { dnA = dnAA[jp_g]; dxA = dxAA[jp_g]; }
2864 /* precalcuate all possible rolling ptrs into the BEGL deck, so we don't wastefully recalc them inside inner DP loop */
2865 for(d = 0; d <= W; d++) jp_wA[d] = (j-d)%(W+1);
2866
2867 /* if do_null3 (act != NULL), update act */
2868 if(act != NULL) {
2869 esl_vec_DCopy(act[(jp_g-1)%(W+1)], cm->abc->K, act[jp_g%(W+1)]);
2870 esl_abc_DCount(cm->abc, act[jp_g%(W+1)], dsq[j], 1.);
2871 /*printf("j: %3d jp_g: %3d jp_g/W: %3d act[0]: %.3f act[1]: %.3f act[2]: %.3f act[3]: %.3f\n", j, jp_g, jp_g%(W+1), act[jp_g%(W+1)][0], act[jp_g%(W+1)][1], act[jp_g%(W+1)][2], act[jp_g%(W+1)][3]);*/
2872 }
2873
2874 for (v = cm->M-1; v > 0; v--) /* ...almost to ROOT; we handle ROOT specially... */
2875 {
2876 /* printf("dnA[v:%d]: %d\ndxA[v:%d]: %d\n", v, dnA[v], v, dxA[v]); */
2877 if(cm->sttype[v] == E_st) continue;
2878 float const *esc_v = esc_vAA[v];
2879 float const *tsc_v = cm->tsc[v];
2880 int emitmode = Emitmode(cm->sttype[v]);
2881
2882 /* float sc; */
2883 jp_v = (cm->stid[v] == BEGL_S) ? (j % (W+1)) : cur;
2884 jp_y = (StateRightDelta(cm->sttype[v]) > 0) ? prv : cur;
2885 sd = StateDelta(cm->sttype[v]);
2886 dn = dnA[v];
2887 dx = dxA[v];
2888 /* if we emit right, precalc score of emitting res j from state v */
2889 float esc_j = IMPOSSIBLE;
2890 if(cm->sttype[v] == IR_st || cm->sttype[v] == MR_st)
2891 esc_j = esc_v[dsq[j]];
2892
2893 if(cm->sttype[v] == B_st) {
2894 w = cm->cfirst[v]; /* BEGL_S */
2895 y = cm->cnum[v]; /* BEGR_S */
2896 for (d = dnA[v]; d <= dxA[v]; d++) {
2897 /* k is the length of the right fragment */
2898 if(do_banded) {
2899 /* Careful, make sure k is consistent with bands in
2900 * state w and state y, and don't forget that
2901 * dmin/dmax values can exceed W. */
2902 dn_y = ESL_MIN(dmin[y], smx->W);
2903 dx_y = ESL_MIN(dmax[y], smx->W);
2904 dn_w = ESL_MIN(dmin[w], smx->W);
2905 dx_w = ESL_MIN(dmax[w], smx->W);
2906 kmin = ESL_MAX(0, ESL_MAX(dn_y, d-dx_w));
2907 kmax = ESL_MIN(dx_y, d-dn_w);
2908 }
2909 else { kmin = 0; kmax = d; }
2910
2911 sc = init_scAA[v][d-sd]; /* state delta (sd) is 0 for B_st */
2912 for (k = kmin; k <= kmax; k++)
2913 sc = FLogsum(sc, (alpha_begl[jp_wA[k]][w][d-k] + alpha[jp_y][y][k]));
2914 alpha[jp_v][v][d] = sc;
2915 /* careful: scores for w, the BEGL_S child of v, are in alpha_begl, not alpha */
2916 }
2917 }
2918 else if (cm->stid[v] == BEGL_S) {
2919 y = cm->cfirst[v];
2920 for (d = dnA[v]; d <= dxA[v]; d++) {
2921 sc = init_scAA[v][d-sd]; /* state delta (sd) is 0 for BEGL_S st */
2922 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++)
2923 sc = FLogsum(sc, alpha[jp_y][y+yoffset][d - sd] + tsc_v[yoffset]);
2924 alpha_begl[jp_v][v][d] = sc;
2925 /* careful: y is in alpha (all children of a BEGL_S must be non BEGL_S) */
2926 }
2927 }
2928 else { /* ! B_st, ! BEGL_S st */
2929 y = cm->cfirst[v];
2930 i = j - dnA[v] + 1;
2931 for (d = dnA[v]; d <= dxA[v]; d++) {
2932 sc = init_scAA[v][d-sd];
2933 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++)
2934 sc = FLogsum(sc, alpha[jp_y][y+yoffset][d - sd] + tsc_v[yoffset]);
2935
2936 switch (emitmode) {
2937 case EMITLEFT:
2938 alpha[jp_v][v][d] = sc + esc_v[dsq[i--]];
2939 break;
2940 case EMITNONE:
2941 alpha[jp_v][v][d] = sc;
2942 break;
2943 case EMITRIGHT:
2944 alpha[jp_v][v][d] = sc + esc_j;
2945 break;
2946 case EMITPAIR:
2947 alpha[jp_v][v][d] = sc + esc_v[dsq[i--]*cm->abc->Kp+dsq[j]];
2948 break;
2949 } /* end of switch emitmode */
2950 } /* end of for d loop */
2951 } /* end of else (which was entered if ! B_st && ! BEGL_S st) */
2952 if(vsc != NULL) {
2953 if(cm->stid[v] != BEGL_S) for (d = dn; d <= dx; d++) vsc[v] = ESL_MAX(vsc[v], alpha[jp_v][v][d]);
2954 else for (d = dn; d <= dx; d++) vsc[v] = ESL_MAX(vsc[v], alpha_begl[jp_v][v][d]);
2955 }
2956 } /*loop over decks v>0 */
2957
2958 /* Finish up with the ROOT_S, state v=0; and deal w/ local begins.
2959 *
2960 * If local begins are off, the hit must be rooted at v=0.
2961 * With local begins on, the hit is rooted at the second state in
2962 * the traceback (e.g. after 0), the internal entry point. Divide & conquer
2963 * can only handle this if it's a non-insert state; this is guaranteed
2964 * by the way local alignment is parameterized (other transitions are
2965 * IMPOSSIBLE), which is probably a little too fragile of a method.
2966 */
2967
2968 float const *tsc_v = cm->tsc[0];
2969 esl_vec_ISet(bestr, (W+1), -1);
2970 esl_vec_FSet(bestsc, (W+1), IMPOSSIBLE);
2971
2972 /* determine min/max d we're allowing for the root state and this position j */
2973 jp_v = cur;
2974 for (d = dnA[0]; d <= dxA[0]; d++) {
2975 bestr[d] = 0; /* root of the traceback = root state 0 */
2976 y = cm->cfirst[0];
2977 alpha[jp_v][0][d] = ESL_MAX(IMPOSSIBLE, alpha[cur][y][d] + tsc_v[0]);
2978 for (yoffset = 1; yoffset < cm->cnum[0]; yoffset++)
2979 alpha[jp_v][0][d] = FLogsum (alpha[jp_v][0][d], (alpha[cur][y+yoffset][d] + tsc_v[yoffset]));
2980 }
2981
2982 if (cm->flags & CMH_LOCAL_BEGIN) {
2983 for (y = 1; y < cm->M; y++) {
2984 if(NOT_IMPOSSIBLE(cm->beginsc[y])) {
2985 dn = ESL_MAX(dnA[0], dnA[y]);
2986 dx = ESL_MIN(dxA[0], dxA[y]);
2987 if(cm->stid[y] == BEGL_S) {
2988 jp_y = j % (W+1);
2989 for (d = dn; d <= dx; d++) {
2990 /*alpha[jp_v][0][d] = FLogsum(alpha[jp_v][0][d], alpha_begl[jp_y][y][d] + cm->beginsc[y]);*/
2991 if(alpha[jp_v][0][d] < (alpha_begl[jp_y][y][d] + cm->beginsc[y])) {
2992 alpha[jp_v][0][d] = alpha_begl[jp_y][y][d] + cm->beginsc[y];
2993 bestr[d] = y;
2994 }
2995 }
2996 }
2997 else { /* y != BEGL_S */
2998 jp_y = cur;
2999 for (d = dn; d <= dx; d++) {
3000 /*alpha[jp_v][0][d] = FLogsum(alpha[jp_v][0][d], alpha[jp_y][y][d] + cm->beginsc[y]);*/
3001 if(alpha[jp_v][0][d] < (alpha[jp_y][y][d] + cm->beginsc[y])) {
3002 alpha[jp_v][0][d] = alpha[jp_y][y][d] + cm->beginsc[y];
3003 bestr[d] = y;
3004 }
3005 }
3006 }
3007 }
3008 }
3009 }
3010 /* fill in bestsc for all valid d values, and update vsc_root (best overall score) */
3011 for (d = dnA[0]; d <= dxA[0]; d++) {
3012 bestsc[d] = alpha[jp_v][0][d];
3013 vsc_root = ESL_MAX(vsc_root, alpha[jp_v][0][d]);
3014 /* Note: currently we NOT do a null3 correction for vsc_root */
3015 }
3016
3017 /* update envi, envj, if nec */
3018 if(do_env_defn) {
3019 for (d = dnA[0]; d <= dxA[0]; d++) {
3020 if(alpha[jp_v][0][d] >= env_cutoff) {
3021 envi = ESL_MIN(envi, j-d+1);
3022 envj = ESL_MAX(envj, j);
3023 }
3024 }
3025 }
3026
3027 /* done with this endpoint j, if necessary, update gamma or tmp_hitlist */
3028 if(gamma != NULL) {
3029 if((status = UpdateGammaHitMx (cm, errbuf, PLI_PASS_STD_ANY, gamma, j, dnA[0], dxA[0], bestsc, bestr, NULL, W, act)) != eslOK) return status;
3030 }
3031 if(tmp_hitlist != NULL) {
3032 if((status = ReportHitsGreedily(cm, errbuf, PLI_PASS_STD_ANY, j, dnA[0], dxA[0], bestsc, bestr, NULL, W, act, i0, j0, cutoff, tmp_hitlist)) != eslOK) return status;
3033 }
3034 /*FILE *fp; fp = fopen("tmp.rfins.smx", "w"); cm_scan_mx_Dump(fp, cm, j, i0, qdbidx, TRUE); fclose(fp); */
3035 } /* end loop over end positions j */
3036 if(vsc != NULL) vsc[0] = vsc_root;
3037
3038 /* set envelope return variables if nec */
3039 if(ret_envi != NULL) { *ret_envi = (envi == j0+1) ? -1 : envi; }
3040 if(ret_envj != NULL) { *ret_envj = (envj == i0-1) ? -1 : envj; }
3041
3042 /* If reporting hits in a greedy manner, remove overlaps greedily from the tmp_hitlist
3043 * then copy remaining hits to master <hitlist>. Then free tmp_hitlist.
3044 */
3045 if(tmp_hitlist != NULL) {
3046 for(h = 0; h < tmp_hitlist->N; h++) tmp_hitlist->unsrt[h].srcL = j0; /* so overlaps can be removed */
3047 cm_tophits_SortForOverlapRemoval(tmp_hitlist);
3048 if((status = cm_tophits_RemoveOrMarkOverlaps(tmp_hitlist, FALSE, errbuf)) != eslOK) return status;
3049 for(h = 0; h < tmp_hitlist->N; h++) {
3050 if(! (tmp_hitlist->hit[h]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) {
3051 if((status = cm_tophits_CloneHitMostly(tmp_hitlist, h, hitlist)) != eslOK) ESL_FAIL(status, errbuf, "problem copying hit to hitlist, out of memory?");
3052 }
3053 }
3054 cm_tophits_Destroy(tmp_hitlist);
3055 }
3056
3057 /* clean up and return */
3058 if (act != NULL) {
3059 for(i = 0; i <= W; i++) free(act[i]);
3060 free(act);
3061 }
3062 free(jp_wA);
3063 free(init_scAA[0]);
3064 free(init_scAA);
3065 if (ret_vsc != NULL) *ret_vsc = vsc;
3066 else free(vsc);
3067 if (ret_sc != NULL) *ret_sc = vsc_root;
3068
3069 ESL_DPRINTF1(("#DEBUG: RefFInsideScan() return score: %10.4f\n", vsc_root));
3070 return eslOK;
3071
3072 ERROR:
3073 ESL_FAIL(eslEMEM, errbuf, "Memory allocation error.\n");
3074 return status; /* NEVERREACHED */
3075 }
3076
3077 /* Function: cm_CountSearchDPCalcs()
3078 * Date: EPN, Tue Oct 30 14:48:00 2007
3079 *
3080 * Purpose: Determine the number of millions of DP calcs needed to scan a seq of length <L>
3081 * for the subtree rooted at each state, either using bands <dmin> and <dmax>,
3082 * or not (if <dmin> == <dmax> == NULL).
3083 * <ret_vcalcs[0]> = number of dp calcs for entire model.
3084 *
3085 * Args: cm - the covariance model
3086 * errbuf - char buffer for error messages
3087 * L - length of the sequence to search
3088 * dmin - minimum bound on d for state v; 0..M
3089 * dmax - maximum bound on d for state v; 0..M
3090 * W - max d: max size of a hit
3091 * correct_for_first_W - TRUE: to only count search for j=W+1..L because first W residues require
3092 * fewer DP calcs b/c d <= j for all j.
3093 * ret_vcalcs- RETURN: [0..v..M-1] number of Millions of DP calcs per residue for scanning with sub-CM at v
3094 * ret_calcs - RETURN: number of Millions of calcs per residue to search L residues with full model (ret_vcalcs[0]).
3095 *
3096 * Returns: eslOK
3097 */
3098 int
3099 cm_CountSearchDPCalcs(CM_t *cm, char *errbuf, int L, int *dmin, int *dmax, int W, int correct_for_first_W, float **ret_vcalcs, float *ret_calcs)
3100 {
3101 int status;
3102 float *vcalcs; /* [0..v..cm->M-1] # of millions of calcs for subtree rooted at v */
3103 int d; /* a subsequence length, 0..W */
3104 int j; /* seq index */
3105 int v, w, y; /* state indices */
3106 int kmin, kmax; /* for B_st's, min/max value consistent with bands*/
3107 int dn; /* temporary value for min d in for loops */
3108 int dx; /* temporary value for max d in for loops */
3109 int do_banded = FALSE; /* TRUE: use QDBs, FALSE: don't */
3110 int jfirst; /* first j to consider (1 unless correct_for_first_W) */
3111 int Leff; /* effective L, this is L unless correct_for_first_W */
3112
3113 if ((W > L) && (correct_for_first_W)) ESL_FAIL(eslFAIL, errbuf, "gross misuse of cm_CountSearchDPCalcs(), W: %d > L: %d and correct_for_first_W is TRUE.\n", W, L);
3114
3115 if(dmin != NULL && dmax != NULL) do_banded = TRUE;
3116 if (W > L) W = L;
3117
3118 ESL_ALLOC(vcalcs, sizeof(float) * cm->M);
3119 esl_vec_FSet(vcalcs, cm->M, 0.);
3120
3121 /* we ignore initialization and band imposition, a little imprecise */
3122 /* Recursion. */
3123 Leff = correct_for_first_W ? (L-W): L;
3124 jfirst = correct_for_first_W ? (W+1) : 1;
3125 for (j = jfirst; j <= L; j++) {
3126 for (v = cm->M-1; v > 0; v--) { /* ...almost to ROOT; we handle ROOT specially... */
3127 if(do_banded) {
3128 dn = (cm->sttype[v] == MP_st) ? ESL_MAX(dmin[v], 2) : ESL_MAX(dmin[v], 1);
3129 dx = ESL_MIN(j, dmax[v]);
3130 dx = ESL_MIN(dx, W);
3131 }
3132 else {
3133 dn = (cm->sttype[v] == MP_st) ? 2 : 1;
3134 dx = ESL_MIN(j, W);
3135 }
3136 if(cm->sttype[v] == E_st) continue;
3137
3138 if(cm->sttype[v] == B_st) {
3139 w = cm->cfirst[v]; /* BEGL_S */
3140 y = cm->cnum[v]; /* BEGR_S */
3141 for (d = dn; d <= dx; d++) {
3142 if(do_banded) {
3143 kmin = ESL_MAX(dmin[y], (d-dmax[w]));
3144 kmin = ESL_MAX(kmin, 0);
3145 kmax = ESL_MIN(dmax[y], (d-dmin[w]));
3146 }
3147 else { kmin = 0; kmax = d; }
3148 if(kmax >= kmin) vcalcs[v] += ((float) ((1+(kmax-kmin+1)))) / 1000000.; /* initial '1 +' is for initialization calc */
3149 } /* ! B_st */
3150 }
3151 else if(dx >= dn) { /* if cm->sttype[v] != B_st */
3152 vcalcs[v] += ((float) (1 + (cm->cnum[v]+1) * (dx-dn+1))) / 1000000.; /* 1 is for initialization calc */
3153 if(StateDelta(cm->sttype[v]) > 0) vcalcs[v] += ((float) (dx-dn+1)) / 1000000.;
3154 } /* end of else (v != B_st) */
3155 } /*loop over decks v>0 */
3156
3157 /* determine min/max d we're allowing for the root state and this position j */
3158 if(do_banded) {
3159 dn = ESL_MAX(dmin[0], 1);
3160 dx = ESL_MIN(j, dmax[0]);
3161 dx = ESL_MIN(dx, W);
3162 }
3163 else {
3164 dn = 1;
3165 dx = ESL_MIN(j, W);
3166 }
3167 if(dx >= dn) vcalcs[0] += ((float) ((cm->cnum[0]+1) * (dx-dn+1))) / 1000000.;
3168
3169 if (cm->flags & CMH_LOCAL_BEGIN) {
3170 for (y = 1; y < cm->M; y++) {
3171 if(do_banded) {
3172 dn = (cm->sttype[y] == MP_st) ? ESL_MAX(dmin[y], 2) : ESL_MAX(dmin[y], 1);
3173 dn = ESL_MAX(dn, dmin[y]);
3174 dx = ESL_MIN(j, dmax[y]);
3175 dx = ESL_MIN(dx, W);
3176 }
3177 else {
3178 dn = 1;
3179 dx = ESL_MIN(j, W);
3180 }
3181 if((dx >= dn) && (NOT_IMPOSSIBLE(cm->beginsc[y]))) vcalcs[0] += ((float) (dx - dn + 1)) / 1000000.;
3182 }
3183 }
3184 } /* end loop over end positions j */
3185
3186 /* sum up the megacells for all states under each v */
3187 for (v = cm->M-1; v >= 0; v--) {
3188 if (cm->sttype[v] == B_st) vcalcs[v] += vcalcs[cm->cnum[v]] + vcalcs[cm->cfirst[v]];
3189 else if(cm->sttype[v] != E_st) vcalcs[v] += vcalcs[v+1];
3190 }
3191 /* convert to per residue */
3192 for (v = cm->M-1; v >= 0; v--) vcalcs[v] /= Leff;
3193
3194 ESL_DPRINTF1(("#DEBUG: cm_CountSearchDPCalcs(), vcalcs[0]: %f\n", vcalcs[0]));
3195 /* for (v = cm->M-1; v >= 0; v--) printf("vcalcs[%4d]: %.3f\n", v, vcalcs[v]); */
3196
3197 if(ret_calcs != NULL) *ret_calcs = vcalcs[0];
3198 if(ret_vcalcs != NULL) *ret_vcalcs = vcalcs;
3199 else free(vcalcs);
3200 return eslOK;
3201
3202 ERROR:
3203 ESL_FAIL(eslEMEM, errbuf, "cm_CountSearchDPCalcs(): memory error.");
3204 return status; /* NEVERREACHED */
3205 }
3206
3207
3208 /* Function: FastCYKScanHB()
3209 * Incept: EPN, Mon Nov 12 17:45:57 2007
3210 *
3211 * Purpose: An HMM banded scanning CYK implementation. Takes a
3212 * CM_HB_MX data structure which is indexed [v][j][d] with
3213 * only cells within the bands allocated (different than
3214 * other (non-HB) scanning function's convention of
3215 * [j][v][d]). QDBs are not used.
3216 *
3217 * Args: cm - the model [0..M-1]
3218 * errbuf - char buffer for reporting errors
3219 * mx - the HMM banded dp matrix, usually cm->hbmx.
3220 * size_limit- max number of Mb for DP matrix, if matrix is bigger return eslERANGE
3221 * dsq - the sequence [1..L]
3222 * i0 - first position in subseq to align (1, for whole seq)
3223 * j0 - last position in subseq to align (L, for whole seq)
3224 * cutoff - minimum score to report
3225 * hitlist - CM_TOPHITS hitlist to add to; if NULL, don't add to it
3226 * do_null3 - TRUE to do NULL3 score correction, FALSE not to
3227 * env_cutoff- ret_envi..ret_envj will include all hits that exceed this bit sc
3228 * ret_envi - RETURN: min position in any hit w/sc >= env_cutoff, set to -1 if no such hits exist, NULL if not wanted
3229 * ret_envj - RETURN: max position in any hit w/sc >= env_cutoff, set to -1 if no such hits exist, NULL if not wanted
3230 * ret_sc - RETURN: score of best overall hit (vsc[0])
3231 *
3232 * Returns: eslOK on success and RETURN variables updated (or not if NULL).
3233 * eslEINCOMPAT on contract violation, errbuf if filled with informative error message.
3234 * eslEINCONCEIVABLE if bands allow a hit > L, errbuf filled.
3235 * eslERANGE if required HMM banded matrix size exceeds <size_limit>, errbuf filled.
3236 * eslEMEM if out of memory, errbuf if filled with informative error message.
3237 */
3238 int
3239 FastCYKScanHB(CM_t *cm, char *errbuf, CM_HB_MX *mx, float size_limit, ESL_DSQ *dsq, int64_t i0, int64_t j0, float cutoff, CM_TOPHITS *hitlist,
3240 int do_null3, float env_cutoff, int64_t *ret_envi, int64_t *ret_envj, float *ret_sc)
3241 {
3242
3243 int status;
3244 GammaHitMx_t *gamma = NULL; /* semi-HMM for hit resoultion */
3245 int *bestr; /* best root state for d at current j */
3246 float *bestsc; /* best score for d at current j */
3247 int v,y,z; /* indices for states */
3248 int j,d,i,k; /* indices in sequence dimensions */
3249 float sc; /* a temporary variable holding a score */
3250 int yoffset; /* y=base+offset -- counter in child states that v can transit to */
3251 int *yvalidA; /* [0..MAXCONNECT-1] TRUE if v->yoffset is legal transition (within bands) */
3252 float *el_scA; /* [0..d..W-1] probability of local end emissions of length d */
3253 /* indices used for handling band-offset issues, and in the depths of the DP recursion */
3254 int sd; /* StateDelta(cm->sttype[v]) */
3255 int sdr; /* StateRightDelta(cm->sttype[v] */
3256 int jp_v, jp_y, jp_z; /* offset j index for states v, y, z */
3257 int jp_y_sdr; /* jp_y - sdr */
3258 int j_sdr; /* j - sdr */
3259 int jn, jx; /* current minimum/maximum j allowed */
3260 int jpn, jpx; /* minimum/maximum jp_v */
3261 int dp_v, dp_y; /* d index for state v/y in alpha w/mem eff bands */
3262 int dn, dx; /* current minimum/maximum d allowed */
3263 int dp; /* ESL_MAX(d-sd, 0) */
3264 int dp_y_sd; /* dp_y - sd */
3265 int dpn, dpx; /* minimum/maximum dp_v */
3266 int kp_z; /* k (in the d dim) index for state z in alpha w/mem eff bands */
3267 int kn, kx; /* current minimum/maximum k value */
3268 float tsc; /* a transition score */
3269 int yvalid_idx; /* for keeping track of which children are valid */
3270 int yvalid_ct; /* for keeping track of which children are valid */
3271 float vsc_root = IMPOSSIBLE; /* score of best hit */
3272 int W; /* max d over all hdmax[v][j] for all valid v, j */
3273 double **act; /* [0..j..W-1][0..a..abc->K-1], alphabet count, count of residue a in dsq from 1..jp where j = jp%(W+1) */
3274 int jp; /* j index in act */
3275 int do_env_defn; /* TRUE to calculate envi, envj, FALSE not to (TRUE if ret_envi != NULL or ret_envj != NULL */
3276 int64_t envi, envj; /* min/max positions that exist in any hit with sc >= env_cutoff */
3277 CM_TOPHITS *tmp_hitlist = NULL; /* temporary hitlist, containing possibly overlapping hits */
3278 int h; /* counter over hits */
3279
3280 /* Contract check */
3281 if(dsq == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "FastCYKScanHB(), dsq is NULL.\n");
3282 if (mx == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "FastCYKScanHB(), mx is NULL.\n");
3283 if (cm->cp9b == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "FastCYKScanHB(), cm->cp9b is NULL.\n");
3284
3285 ESL_DPRINTF1(("#DEBUG: cm->search_opts & CM_SEARCH_HMMALNBANDS: %d\n", cm->search_opts & CM_SEARCH_HMMALNBANDS));
3286
3287 /* variables used for memory efficient bands */
3288 /* ptrs to cp9b info, for convenience */
3289 CP9Bands_t *cp9b = cm->cp9b;
3290 int *jmin = cp9b->jmin;
3291 int *jmax = cp9b->jmax;
3292 int **hdmin = cp9b->hdmin;
3293 int **hdmax = cp9b->hdmax;
3294 /* the DP matrix */
3295 float ***alpha = mx->dp; /* pointer to the alpha DP matrix */
3296
3297 /* Allocations and initializations */
3298 /* grow the matrix based on the current sequence and bands */
3299 if((status = cm_hb_mx_GrowTo(cm, mx, errbuf, cp9b, (j0-i0+1), size_limit)) != eslOK) return status;
3300
3301 /* set W as j0-i0+1 (this may exceed max size of a hit our bands will allow,
3302 * but that's okay b/c W is only used for sizing of act, bestr, bestsc vectors */
3303 W = j0-i0+1;
3304 /* make sure our bands won't allow a hit bigger than W (this could be modified to only execute in debugging mode) */
3305 for(j = jmin[0]; j <= jmax[0]; j++) {
3306 if(W < (hdmax[0][(j-jmin[0])])) ESL_FAIL(eslEINCONCEIVABLE, errbuf, "FastCYKScanHB(), band allows a hit (j:%d hdmax[0][j]:%d) greater than j0-i0+1 (%" PRId64 "d)", j, hdmax[0][(j-jmin[0])], j0-i0+1);
3307 }
3308
3309 /* precalcuate all possible local end scores, for local end emits of 1..W residues */
3310 ESL_ALLOC(el_scA, sizeof(float) * (W+1));
3311 for(d = 0; d <= W; d++) el_scA[d] = cm->el_selfsc * d;
3312
3313 /* yvalidA[0..cnum[v]] will hold TRUE for states y for which a transition is legal
3314 * (some transitions are impossible due to the bands) */
3315 ESL_ALLOC(yvalidA, sizeof(int) * MAXCONNECT);
3316 esl_vec_ISet(yvalidA, MAXCONNECT, FALSE);
3317
3318 /* initialize all cells of the matrix to IMPOSSIBLE */
3319 esl_vec_FSet(alpha[0][0], mx->ncells_valid, IMPOSSIBLE);
3320
3321 /* If we were passed a master hitlist <hitlist>, either create a
3322 * gamma hit matrix for resolving overlaps optimally (if
3323 * cm->search_opts & CM_SEARCH_CMNOTGREEDY) or create a temporary
3324 * hitlist that will store overlapping hits, in that case, we'll
3325 * remove overlaps greedily before copying the hits to the master
3326 * <hitlist>.
3327 */
3328 gamma = NULL;
3329 tmp_hitlist = NULL;
3330 if(hitlist != NULL) {
3331 if(cm->search_opts & CM_SEARCH_CMNOTGREEDY) {
3332 gamma = CreateGammaHitMx(j0-i0+1, i0, cutoff);
3333 }
3334 else {
3335 tmp_hitlist = cm_tophits_Create();
3336 }
3337 }
3338
3339 /* if do_null3: allocate and initialize act vector */
3340 if(do_null3) {
3341 ESL_ALLOC(act, sizeof(double *) * (W+1));
3342 for(i = 0; i <= W; i++) {
3343 ESL_ALLOC(act[i], sizeof(double) * cm->abc->K);
3344 esl_vec_DSet(act[i], cm->abc->K, 0.);
3345 }
3346 /* pre-fill act, different than non-HMM banded scanner b/c our main loop doesn't step j through residues */
3347 for(j = i0; j <= j0; j++) {
3348 jp = j-i0+1; /* j is actual index in dsq, jp is offset j relative to start i0 (j index for act) */
3349 esl_vec_DCopy(act[(jp-1)%(W+1)], cm->abc->K, act[jp%(W+1)]);
3350 esl_abc_DCount(cm->abc, act[jp%(W+1)], dsq[j], 1.);
3351 }
3352 }
3353 else act = NULL;
3354
3355 /* initialize envelope boundary variables */
3356 do_env_defn = (ret_envi != NULL || ret_envj != NULL) ? TRUE : FALSE;
3357 envi = j0+1;
3358 envj = i0-1;
3359
3360 /* Main recursion */
3361 for (v = cm->M-1; v >= 0; v--) { /* all the way down to root, different from other scanners */
3362 float const *esc_v = cm->oesc[v]; /* emission scores for state v */
3363 float const *tsc_v = cm->tsc[v]; /* transition scores for state v */
3364 sd = StateDelta(cm->sttype[v]);
3365 sdr = StateRightDelta(cm->sttype[v]);
3366 jn = jmin[v];
3367 jx = jmax[v];
3368
3369 /* re-initialize the deck if we can do a local end from v */
3370 if(NOT_IMPOSSIBLE(cm->endsc[v])) {
3371 for (j = jmin[v]; j <= jmax[v]; j++) {
3372 jp_v = j - jmin[v];
3373 for (dp_v = 0, d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; dp_v++, d++) {
3374 dp = ESL_MAX(d-sd, 0);
3375 alpha[v][jp_v][dp_v] = el_scA[dp] + cm->endsc[v];
3376 }
3377 }
3378 }
3379 /* otherwise this state's deck has already been initialized to IMPOSSIBLE */
3380
3381 if(cm->sttype[v] == E_st) {
3382 for (j = jmin[v]; j <= jmax[v]; j++) {
3383 jp_v = j-jmin[v];
3384 ESL_DASSERT1((hdmin[v][jp_v] == 0));
3385 ESL_DASSERT1((hdmax[v][jp_v] == 0));
3386 alpha[v][jp_v][0] = 0.; /* for End states, d must be 0 */
3387 }
3388 }
3389 else if(cm->sttype[v] == IL_st) {
3390 /* update alpha[v][jp_v][dp_v] cells, for IL states, loop nesting order is:
3391 * for j { for d { for y { } } } because they can self transit, and a
3392 * alpha[v][j][d] cell must be complete (that is we must have looked at all children y)
3393 * before can start calc'ing for alpha[v][j][d+1] */
3394 for (j = jmin[v]; j <= jmax[v]; j++) {
3395 jp_v = j - jmin[v];
3396 yvalid_ct = 0;
3397 j_sdr = j - sdr;
3398
3399 /* determine which children y we can legally transit to for v, j */
3400 for (y = cm->cfirst[v], yoffset = 0; y < (cm->cfirst[v] + cm->cnum[v]); y++, yoffset++)
3401 if((j_sdr) >= jmin[y] && ((j_sdr) <= jmax[y])) yvalidA[yvalid_ct++] = yoffset; /* is j-sdr valid for state y? */
3402
3403 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) { /* for each valid d for v, j */
3404 i = j - d + 1;
3405 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha */
3406 for (yvalid_idx = 0; yvalid_idx < yvalid_ct; yvalid_idx++) { /* for each valid child y, for v, j */
3407 yoffset = yvalidA[yvalid_idx];
3408 y = cm->cfirst[v] + yoffset;
3409 jp_y_sdr = j - jmin[y] - sdr;
3410
3411 if((d-sd) >= hdmin[y][jp_y_sdr] && (d-sd) <= hdmax[y][jp_y_sdr]) { /* make sure d is valid for this v, j and y */
3412 dp_y_sd = d - sd - hdmin[y][jp_y_sdr];
3413 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
3414 ESL_DASSERT1((dp_y_sd >= 0 && dp_y_sd <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
3415 alpha[v][jp_v][dp_v] = ESL_MAX(alpha[v][jp_v][dp_v], alpha[y][jp_y_sdr][dp_y_sd] + tsc_v[yoffset]);
3416 }
3417 }
3418 alpha[v][jp_v][dp_v] += esc_v[dsq[i--]];
3419 alpha[v][jp_v][dp_v] = ESL_MAX(alpha[v][jp_v][dp_v], IMPOSSIBLE);
3420 }
3421 }
3422 }
3423 else if(cm->sttype[v] == IR_st) {
3424 /* update alpha[v][jp_v][dp_v] cells, for IR states, loop nesting order is:
3425 * for j { for d { for y { } } } because they can self transit, and a
3426 * alpha[v][j][d] cell must be complete (that is we must have looked at all children y)
3427 * before can start calc'ing for alpha[v][j][d+1] */
3428 for (j = jmin[v]; j <= jmax[v]; j++) {
3429 jp_v = j - jmin[v];
3430 yvalid_ct = 0;
3431 j_sdr = j - sdr;
3432
3433 /* determine which children y we can legally transit to for v, j */
3434 for (y = cm->cfirst[v], yoffset = 0; y < (cm->cfirst[v] + cm->cnum[v]); y++, yoffset++)
3435 if((j_sdr) >= jmin[y] && ((j_sdr) <= jmax[y])) yvalidA[yvalid_ct++] = yoffset; /* is j-sdr is valid for state y? */
3436
3437 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) { /* for each valid d for v, j */
3438 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha */
3439 for (yvalid_idx = 0; yvalid_idx < yvalid_ct; yvalid_idx++) { /* for each valid child y, for v, j */
3440 yoffset = yvalidA[yvalid_idx];
3441 y = cm->cfirst[v] + yoffset;
3442 jp_y_sdr = j - jmin[y] - sdr;
3443
3444 if((d-sd) >= hdmin[y][jp_y_sdr] && (d-sd) <= hdmax[y][jp_y_sdr]) { /* make sure d is valid for this v, j and y */
3445 dp_y_sd = d - sd - hdmin[y][jp_y_sdr];
3446 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
3447 ESL_DASSERT1((dp_y_sd >= 0 && dp_y_sd <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
3448 alpha[v][jp_v][dp_v] = ESL_MAX(alpha[v][jp_v][dp_v], alpha[y][jp_y_sdr][dp_y_sd] + tsc_v[yoffset]);
3449 }
3450 }
3451 alpha[v][jp_v][dp_v] += esc_v[dsq[j]];
3452 alpha[v][jp_v][dp_v] = ESL_MAX(alpha[v][jp_v][dp_v], IMPOSSIBLE);
3453 }
3454 }
3455 }
3456 else if(cm->sttype[v] != B_st) { /* entered if state v is (! IL && ! IR && ! B) */
3457 /* ML, MP, MR, D, S, E states cannot self transit, this means that all cells
3458 * in beta[v] are independent of each other, only depending on beta[y] for previously calc'ed y.
3459 * We can do the for loops in any nesting order, this implementation does what I think is most efficient:
3460 * for y { for j { for d { } } }
3461 */
3462 for (y = cm->cfirst[v]; y < (cm->cfirst[v] + cm->cnum[v]); y++) {
3463 yoffset = y - cm->cfirst[v];
3464 tsc = tsc_v[yoffset];
3465
3466 /* j must satisfy:
3467 * j >= jmin[v]
3468 * j >= jmin[y]+sdr (follows from (j-sdr >= jmin[y]))
3469 * j <= jmax[v]
3470 * j <= jmax[y]+sdr (follows from (j-sdr <= jmax[y]))
3471 * this reduces to two ESL_MAX calls
3472 */
3473 jn = ESL_MAX(jmin[v], jmin[y]+sdr);
3474 jx = ESL_MIN(jmax[v], jmax[y]+sdr);
3475 jpn = jn - jmin[v];
3476 jpx = jx - jmin[v];
3477 jp_y_sdr = jn - jmin[y] - sdr;
3478
3479 for (jp_v = jpn; jp_v <= jpx; jp_v++, jp_y_sdr++) {
3480 ESL_DASSERT1((jp_v >= 0 && jp_v <= (jmax[v]-jmin[v])));
3481 ESL_DASSERT1((jp_y_sdr >= 0 && jp_y_sdr <= (jmax[y]-jmin[y])));
3482
3483 /* d must satisfy:
3484 * d >= hdmin[v][jp_v]
3485 * d >= hdmin[y][jp_y_sdr]+sd (follows from (d-sd >= hdmin[y][jp_y_sdr]))
3486 * d <= hdmax[v][jp_v]
3487 * d <= hdmax[y][jp_y_sdr]+sd (follows from (d-sd <= hdmax[y][jp_y_sdr]))
3488 * this reduces to two ESL_MAX calls
3489 */
3490 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y_sdr] + sd);
3491 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y_sdr] + sd);
3492 dpn = dn - hdmin[v][jp_v];
3493 dpx = dx - hdmin[v][jp_v];
3494 dp_y_sd = dn - hdmin[y][jp_y_sdr] - sd;
3495
3496 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y_sd++) {
3497 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
3498 ESL_DASSERT1((dp_y_sd >= 0 && dp_y_sd <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
3499 alpha[v][jp_v][dp_v] = ESL_MAX(alpha[v][jp_v][dp_v], alpha[y][jp_y_sdr][dp_y_sd] + tsc);
3500 }
3501 }
3502 }
3503 /* add in emission score, if any */
3504 switch(cm->sttype[v]) {
3505 case ML_st:
3506 for (j = jmin[v]; j <= jmax[v]; j++) {
3507 jp_v = j - jmin[v];
3508 i = j - hdmin[v][jp_v] + 1;
3509 for (dp_v = 0; dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v]); dp_v++)
3510 alpha[v][jp_v][dp_v] += esc_v[dsq[i--]];
3511 }
3512 break;
3513 case MR_st:
3514 for (j = jmin[v]; j <= jmax[v]; j++) {
3515 jp_v = j - jmin[v];
3516 for (dp_v = 0; dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v]); dp_v++)
3517 alpha[v][jp_v][dp_v] += esc_v[dsq[j]];
3518 }
3519 break;
3520 case MP_st:
3521 for (j = jmin[v]; j <= jmax[v]; j++) {
3522 jp_v = j - jmin[v];
3523 i = j - hdmin[v][jp_v] + 1;
3524 for (dp_v = 0; dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v]); dp_v++)
3525 {
3526 /*if(i < i0 || j > j0) {
3527 printf("dsq[i:%d]: %d\n", i, dsq[i]);
3528 printf("dsq[j:%d]: %d\n", j, dsq[j]);
3529 printf("esc_v[%d]: %.5f\n", dsq[i]*cm->abc->Kp+dsq[j], esc_v[dsq[i]*cm->abc->Kp+dsq[j]]);;
3530 printf("i0: %" PRId64 " j0: %" PRId64 "d\n", i0, j0);
3531 }*/
3532 alpha[v][jp_v][dp_v] += esc_v[dsq[i--]*cm->abc->Kp+dsq[j]];
3533 }
3534 }
3535 default:
3536 break;
3537 }
3538 /* ensure all cells are >= IMPOSSIBLE */
3539 for (j = jmin[v]; j <= jmax[v]; j++) {
3540 jp_v = j - jmin[v];
3541 for (dp_v = 0; dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v]); dp_v++)
3542 alpha[v][jp_v][dp_v] = ESL_MAX(alpha[v][jp_v][dp_v], IMPOSSIBLE);
3543 }
3544 }
3545 else { /* B_st */
3546 y = cm->cfirst[v]; /* left subtree */
3547 z = cm->cnum[v]; /* right subtree */
3548
3549 /* Any valid j must be within both state v and state z's j band
3550 * I think jmin[v] <= jmin[z] is guaranteed by the way bands are
3551 * constructed, but we'll check anyway.
3552 */
3553 jn = (jmin[v] > jmin[z]) ? jmin[v] : jmin[z];
3554 jx = (jmax[v] < jmax[z]) ? jmax[v] : jmax[z];
3555 /* the main j loop */
3556 for (j = jn; j <= jx; j++) {
3557 jp_v = j - jmin[v];
3558 jp_y = j - jmin[y];
3559 jp_z = j - jmin[z];
3560 kn = ((j-jmax[y]) > (hdmin[z][jp_z])) ? (j-jmax[y]) : hdmin[z][jp_z];
3561 kn = ESL_MAX(kn, 0); /* kn must be non-negative, added with fix to bug i36 */
3562 /* kn satisfies inequalities (1) and (3) (listed below)*/
3563 kx = ( jp_y < (hdmax[z][jp_z])) ? jp_y : hdmax[z][jp_z];
3564 /* kn satisfies inequalities (2) and (4) (listed below)*/
3565 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) {
3566 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha w/mem eff bands */
3567
3568 /* Find the first k value that implies a valid cell in the y and z decks.
3569 * This k must satisfy the following 6 inequalities (some may be redundant):
3570 * (1) k >= j-jmax[y];
3571 * (2) k <= j-jmin[y];
3572 * 1 and 2 guarantee (j-k) is within state y's j band
3573 *
3574 * (3) k >= hdmin[z][j-jmin[z]];
3575 * (4) k <= hdmax[z][j-jmin[z]];
3576 * 3 and 4 guarantee k is within z's j=(j), d band
3577 *
3578 * (5) k >= d-hdmax[y][j-jmin[y]-k];
3579 * (6) k <= d-hdmin[y][j-jmin[y]-k];
3580 * 5 and 6 guarantee (d-k) is within state y's j=(j-k) d band
3581 *
3582 * kn and kx were set above (outside (for (dp_v...) loop) that
3583 * satisfy 1-4 (b/c 1-4 are d-independent and k-independent)
3584 * RHS of inequalities 5 and 6 are dependent on k, so we check
3585 * for these within the next for loop.
3586 */
3587 for(k = kn; k <= kx; k++) {
3588 if((k >= d - hdmax[y][jp_y-k]) && k <= d - hdmin[y][jp_y-k]) {
3589 /* for current k, all 6 inequalities have been satisified
3590 * so we know the cells corresponding to the platonic
3591 * matrix cells alpha[v][j][d], alpha[y][j-k][d-k], and
3592 * alpha[z][j][k] are all within the bands. These
3593 * cells correspond to alpha[v][jp_v][dp_v],
3594 * alpha[y][jp_y-k][d-hdmin[jp_y-k]-k],
3595 * and alpha[z][jp_z][k-hdmin[jp_z]];
3596 */
3597 kp_z = k-hdmin[z][jp_z];
3598 dp_y = d-hdmin[y][jp_y-k];
3599 alpha[v][jp_v][dp_v] = ESL_MAX(alpha[v][jp_v][dp_v], alpha[y][jp_y-k][dp_y - k] + alpha[z][jp_z][kp_z]);
3600 }
3601 }
3602 }
3603 }
3604 } /* finished calculating deck v. */
3605 } /* end of for (v = cm->M-1; v >= 0; v--) */
3606
3607 /* Deal with local begins.
3608 * If local begins are off, all hits must be rooted at v=0.
3609 * With local begins on, the hit is rooted at the second state in
3610 * the traceback (e.g. after 0), the internal entry point.
3611 *
3612 * Hits rooted at 0 that not involved with local begins are
3613 * already calc'ed from the v loop with v == 0
3614 */
3615
3616 /* Report all possible hits, but only after looking at local begins (if they're on) */
3617 v = 0;
3618 jpn = 0;
3619 jpx = jmax[v] - jmin[v];
3620 j = jmin[v];
3621
3622 ESL_ALLOC(bestr, sizeof(int) * (W+1));
3623 ESL_ALLOC(bestsc, sizeof(float) * (W+1));
3624
3625 /* update gamma, by specifying all hits with j < jmin[0] are impossible */
3626 if(gamma != NULL) {
3627 for(j = i0; j < jmin[v]; j++) {
3628 if((status = UpdateGammaHitMx (cm, errbuf, PLI_PASS_STD_ANY, gamma, j, -1, -1,
3629 NULL, /* NULL for bestsc tells UpdateGammaHitMx() no hits are possible for this j */
3630 bestr, NULL, W, act)) != eslOK) return status;
3631 }
3632 }
3633
3634 for (jp_v = jpn; jp_v <= jpx; jp_v++, jp_y++, j++) {
3635 esl_vec_ISet(bestr, (W+1), 0); /* init bestr to 0, all hits are rooted at 0 unless we find a better local begin below */
3636 esl_vec_FSet(bestsc, (W+1), IMPOSSIBLE);
3637 if (cm->flags & CMH_LOCAL_BEGIN) {
3638 for (y = 1; y < cm->M; y++) {
3639 if(NOT_IMPOSSIBLE(cm->beginsc[y]) && (j >= jmin[y] && j <= jmax[y])) {
3640 assert(cm->sttype[v] != BEGL_S); /* local begins into BEGL_S are impossible */
3641 jp_y = j - jmin[y];
3642
3643 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y]);
3644 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y]);
3645 dpn = dn - hdmin[v][jp_v];
3646 dpx = dx - hdmin[v][jp_v];
3647 dp_y = dn - hdmin[y][jp_y];
3648 d = dn;
3649 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y++, d++) {
3650 sc = alpha[y][jp_y][dp_y] + cm->beginsc[y];
3651 if(sc > alpha[0][jp_v][dp_v]) {
3652 alpha[0][jp_v][dp_v] = sc;
3653 bestsc[d] = sc;
3654 bestr[d] = y;
3655 }
3656 }
3657 }
3658 } /* end of for(y = 1; y < cm->M; y++) */
3659 } /* end of if(cm->flags & CMH_LOCAL_BEGIN */
3660
3661 /* for this j, fill in bestsc for all valid d values, update
3662 * vsc_root (best overall score), and update envelope boundaries
3663 * (if necessary)
3664 */
3665 dpn = 0;
3666 dpx = hdmax[v][jp_v] - hdmin[v][jp_v];
3667 for(dp_v = dpn; dp_v <= dpx; dp_v++) {
3668 d = dp_v + hdmin[v][jp_v];
3669 bestsc[d] = alpha[0][jp_v][dp_v];
3670 vsc_root = ESL_MAX(vsc_root, alpha[0][jp_v][dp_v]);
3671 }
3672 /* update envelope boundaries, if nec */
3673 if(do_env_defn) {
3674 j = jp_v + jmin[v];
3675 for(dp_v = dpn; dp_v <= dpx; dp_v++) {
3676 if(alpha[0][jp_v][dp_v] >= env_cutoff) {
3677 d = dp_v + hdmin[v][jp_v];
3678 i = j - d + 1;
3679 envi = ESL_MIN(envi, i);
3680 envj = ESL_MAX(envj, j);
3681 }
3682 }
3683 }
3684
3685 /* if necessary, report all hits with valid d for this j, either to gamma or tmp_hitlist */
3686 if(gamma != NULL) {
3687 if((status = UpdateGammaHitMx (cm, errbuf, PLI_PASS_STD_ANY, gamma, j, hdmin[0][jp_v], hdmax[0][jp_v], bestsc, bestr, NULL, W, act)) != eslOK) return status;
3688 }
3689 if(tmp_hitlist != NULL) {
3690 if((status = ReportHitsGreedily(cm, errbuf, PLI_PASS_STD_ANY, j, hdmin[0][jp_v], hdmax[0][jp_v], bestsc, bestr, NULL, W, act, i0, j0, cutoff, tmp_hitlist)) != eslOK) return status;
3691 }
3692 } /* end of 'for (jp_v = jpn; jp_v <= jpx; jp_v++, jp_y++, j++) {' */
3693
3694 /* update gamma, by specifying all hits with j > jmax[0] are impossible */
3695 if(gamma != NULL) {
3696 for(j = jmax[v]+1; j <= j0; j++) {
3697 if((status = UpdateGammaHitMx(cm, errbuf, PLI_PASS_STD_ANY, gamma, j, -1, -1,
3698 NULL, /* NULL for bestsc tells UpdateGammaHitMx() no hits are possible for this j */
3699 bestr, NULL, W, act)) != eslOK) return status;
3700 }
3701 }
3702
3703 free(el_scA);
3704 free(yvalidA);
3705 free(bestr);
3706 free(bestsc);
3707 if (act != NULL) {
3708 for(i = 0; i <= W; i++) free(act[i]);
3709 free(act);
3710 }
3711
3712 /* If recovering hits in a non-greedy manner, do the gamma traceback, then free gamma */
3713 if(gamma != NULL) {
3714 TBackGammaHitMx(gamma, hitlist, i0, j0);
3715 FreeGammaHitMx(gamma);
3716 }
3717 /* If reporting hits in a greedy manner, remove overlaps greedily from the tmp_hitlist
3718 * then copy remaining hits to master <hitlist>. Then free tmp_hitlist.
3719 */
3720 if(tmp_hitlist != NULL) {
3721 for(h = 0; h < tmp_hitlist->N; h++) tmp_hitlist->unsrt[h].srcL = j0; /* so overlaps can be removed */
3722 cm_tophits_SortForOverlapRemoval(tmp_hitlist);
3723 if((status = cm_tophits_RemoveOrMarkOverlaps(tmp_hitlist, FALSE, errbuf)) != eslOK) return status;
3724 for(h = 0; h < tmp_hitlist->N; h++) {
3725 if(! (tmp_hitlist->hit[h]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) {
3726 if((status = cm_tophits_CloneHitMostly(tmp_hitlist, h, hitlist)) != eslOK) ESL_FAIL(status, errbuf, "problem copying hit to hitlist, out of memory?");
3727 }
3728 }
3729 cm_tophits_Destroy(tmp_hitlist);
3730 }
3731
3732 /* set envelope return variables if nec */
3733 if(ret_envi != NULL) { *ret_envi = (envi == j0+1) ? -1 : envi; }
3734 if(ret_envj != NULL) { *ret_envj = (envj == i0-1) ? -1 : envj; }
3735
3736 if (ret_sc != NULL) *ret_sc = vsc_root;
3737 ESL_DPRINTF1(("#DEBUG: FastCYKScanHB() return sc: %f\n", vsc_root));
3738 return eslOK;
3739
3740 ERROR:
3741 ESL_FAIL(eslEMEM, errbuf, "Memory allocation error.\n");
3742 return 0.; /* never reached */
3743 }
3744
3745 /* Function: FastFInsideScanHB()
3746 * Incept: EPN, Wed Nov 14 18:17:28 2007
3747 *
3748 * Purpose: An HMM banded scanning Inside implementation that uses
3749 * float scores. Takes a CM_HB_MX data structure which is
3750 * indexed [v][j][d] with only cells within the bands
3751 * allocated (different than other (non-HB) scanning
3752 * function's convention of [j][v][d]). QDBs are not used.
3753 *
3754 * Args: cm - the model [0..M-1]
3755 * errbuf - char buffer for reporting errors
3756 * mx - the HMM banded dp matrix, usually cm->hbmx.
3757 * size_limit- max number of Mb for DP matrix, if matrix is bigger return eslERANGE
3758 * dsq - the sequence [1..L]
3759 * i0 - first position in subseq to align (1, for whole seq)
3760 * j0 - last position in subseq to align (L, for whole seq)
3761 * cutoff - minimum score to report
3762 * hitlist - CM_TOPHITS hitlist to add to; if NULL, don't add to it
3763 * do_null3 - TRUE to do NULL3 score correction, FALSE not to
3764 * env_cutoff- ret_envi..ret_envj will include all hits that exceed this bit sc
3765 * ret_envi - RETURN: min position in any hit w/sc >= env_cutoff, set to -1 if no such hits exist, NULL if not wanted
3766 * ret_envj - RETURN: max position in any hit w/sc >= env_cutoff, set to -1 if no such hits exist, NULL if not wanted
3767 * ret_sc - RETURN: score of best overall hit (vsc[0])
3768 *
3769 * Returns: eslOK on success and RETURN variables updated (or not if NULL).
3770 * eslEINCOMPAT on contract violation, errbuf if filled with informative error message.
3771 * eslEINCONCEIVABLE if bands allow a hit > L, errbuf filled.
3772 * eslERANGE if required HMM banded matrix size exceeds <size_limit>, errbuf filled.
3773 * eslEMEM if out of memory, errbuf if filled with informative error message.
3774 */
3775 int
3776 FastFInsideScanHB(CM_t *cm, char *errbuf, CM_HB_MX *mx, float size_limit, ESL_DSQ *dsq, int64_t i0, int64_t j0, float cutoff, CM_TOPHITS *hitlist,
3777 int do_null3, float env_cutoff, int64_t *ret_envi, int64_t *ret_envj, float *ret_sc)
3778 {
3779
3780 int status;
3781 GammaHitMx_t *gamma = NULL; /* semi-HMM for hit resoultion */
3782 int *bestr; /* best root state for d at current j */
3783 float *bestsc; /* best score for d at current j */
3784 int v,y,z; /* indices for states */
3785 int j,d,i,k; /* indices in sequence dimensions */
3786 float sc; /* a temporary variable holding a score */
3787 int yoffset; /* y=base+offset -- counter in child states that v can transit to */
3788 int *yvalidA; /* [0..MAXCONNECT-1] TRUE if v->yoffset is legal transition (within bands) */
3789 float *el_scA; /* [0..d..W-1] probability of local end emissions of length d */
3790 /* indices used for handling band-offset issues, and in the depths of the DP recursion */
3791 int sd; /* StateDelta(cm->sttype[v]) */
3792 int sdr; /* StateRightDelta(cm->sttype[v] */
3793 int jp_v, jp_y, jp_z; /* offset j index for states v, y, z */
3794 int jp_y_sdr; /* jp_y - sdr */
3795 int j_sdr; /* j - sdr */
3796 int jn, jx; /* current minimum/maximum j allowed */
3797 int jpn, jpx; /* minimum/maximum jp_v */
3798 int dp_v, dp_y; /* d index for state v/y in alpha w/mem eff bands */
3799 int dn, dx; /* current minimum/maximum d allowed */
3800 int dp; /* ESL_MAX(d-sd, 0) */
3801 int dp_y_sd; /* dp_y - sd */
3802 int dpn, dpx; /* minimum/maximum dp_v */
3803 int kp_z; /* k (in the d dim) index for state z in alpha w/mem eff bands */
3804 int kn, kx; /* current minimum/maximum k value */
3805 float tsc; /* a transition score */
3806 int yvalid_idx; /* for keeping track of which children are valid */
3807 int yvalid_ct; /* for keeping track of which children are valid */
3808 float vsc_root = IMPOSSIBLE; /* score of best hit */
3809 int W; /* max d over all hdmax[v][j] for all valid v, j */
3810 double **act; /* [0..j..W-1][0..a..abc->K-1], alphabet count, count of residue a in dsq from 1..jp where j = jp%(W+1) */
3811 int jp; /* j index in act */
3812 int do_env_defn; /* TRUE to calculate envi, envj, FALSE not to (TRUE if ret_envi != NULL or ret_envj != NULL */
3813 int64_t envi, envj; /* min/max positions that exist in any hit with sc >= env_cutoff */
3814 CM_TOPHITS *tmp_hitlist = NULL; /* temporary hitlist, containing possibly overlapping hits */
3815 int h; /* counter over hits */
3816
3817 /* Contract check */
3818 if(dsq == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "FastFInsideScanHB(), dsq is NULL.\n");
3819 if (mx == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "FastFInsideScanHB(), mx is NULL.\n");
3820 if (cm->cp9b == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "FastFInsideScanHB(), mx is NULL.\n");
3821
3822 /* variables used for memory efficient bands */
3823 /* ptrs to cp9b info, for convenience */
3824 CP9Bands_t *cp9b = cm->cp9b;
3825 int *jmin = cp9b->jmin;
3826 int *jmax = cp9b->jmax;
3827 int **hdmin = cp9b->hdmin;
3828 int **hdmax = cp9b->hdmax;
3829 /* the DP matrix */
3830 float ***alpha = mx->dp; /* pointer to the alpha DP matrix */
3831
3832 /* Allocations and initializations */
3833 /* grow the matrix based on the current sequence and bands */
3834 if((status = cm_hb_mx_GrowTo(cm, mx, errbuf, cp9b, (j0-i0+1), size_limit)) != eslOK) return status;
3835
3836 /* set W as j0-i0+1 (this may exceed max size of a hit our bands will allow,
3837 * but that's okay b/c W is only used for sizing of act, bestr, bestsc vectors */
3838 W = j0-i0+1;
3839 /* make sure our bands won't allow a hit bigger than W (this could be modified to only execute in debugging mode) */
3840 for(j = jmin[0]; j <= jmax[0]; j++) {
3841 if(W < (hdmax[0][(j-jmin[0])])) ESL_FAIL(eslEINCONCEIVABLE, errbuf, "FastCYKScanHB(), band allows a hit (j:%d hdmax[0][j]:%d) greater than j0-i0+1 (%" PRId64 ")", j, hdmax[0][(j-jmin[0])], j0-i0+1);
3842 }
3843
3844 /* precalcuate all possible local end scores, for local end emits of 1..W residues */
3845 ESL_ALLOC(el_scA, sizeof(float) * (W+1));
3846 for(d = 0; d <= W; d++) el_scA[d] = cm->el_selfsc * d;
3847
3848 /* yvalidA[0..cnum[v]] will hold TRUE for states y for which a transition is legal
3849 * (some transitions are impossible due to the bands) */
3850 ESL_ALLOC(yvalidA, sizeof(int) * MAXCONNECT);
3851 esl_vec_ISet(yvalidA, MAXCONNECT, FALSE);
3852
3853 /* initialize all cells of the matrix to IMPOSSIBLE */
3854 esl_vec_FSet(alpha[0][0], mx->ncells_valid, IMPOSSIBLE);
3855
3856 /* If we were passed a master hitlist <hitlist>, either create a
3857 * gamma hit matrix for resolving overlaps optimally (if
3858 * cm->search_opts & CM_SEARCH_CMNOTGREEDY) or create a temporary
3859 * hitlist that will store overlapping hits, in that case, we'll
3860 * remove overlaps greedily before copying the hits to the master
3861 * <hitlist>.
3862 */
3863 gamma = NULL;
3864 tmp_hitlist = NULL;
3865 if(hitlist != NULL) {
3866 if(cm->search_opts & CM_SEARCH_CMNOTGREEDY) {
3867 gamma = CreateGammaHitMx(j0-i0+1, i0, cutoff);
3868 }
3869 else {
3870 tmp_hitlist = cm_tophits_Create();
3871 }
3872 }
3873
3874 /* if do_null3: allocate and initialize act vector */
3875 if(do_null3) {
3876 ESL_ALLOC(act, sizeof(double *) * (W+1));
3877 for(i = 0; i <= W; i++) {
3878 ESL_ALLOC(act[i], sizeof(double) * cm->abc->K);
3879 esl_vec_DSet(act[i], cm->abc->K, 0.);
3880 }
3881 /* pre-fill act, different than non-HMM banded scanner b/c our main loop doesn't step j through residues */
3882 for(j = i0; j <= j0; j++) {
3883 jp = j-i0+1; /* j is actual index in dsq, jp is offset j relative to start i0 (j index for act) */
3884 esl_vec_DCopy(act[(jp-1)%(W+1)], cm->abc->K, act[jp%(W+1)]);
3885 esl_abc_DCount(cm->abc, act[jp%(W+1)], dsq[j], 1.);
3886 }
3887 }
3888 else act = NULL;
3889
3890 /* initialize envelope boundary variables */
3891 do_env_defn = (ret_envi != NULL || ret_envj != NULL) ? TRUE : FALSE;
3892 envi = j0+1;
3893 envj = i0-1;
3894
3895 /* Main recursion */
3896 for (v = cm->M-1; v >= 0; v--) { /* all the way down to root, different from other scanners */
3897 float const *esc_v = cm->oesc[v]; /* emission scores for state v */
3898 float const *tsc_v = cm->tsc[v]; /* transition scores for state v */
3899 sd = StateDelta(cm->sttype[v]);
3900 sdr = StateRightDelta(cm->sttype[v]);
3901 jn = jmin[v];
3902 jx = jmax[v];
3903
3904 /* re-initialize the deck if we can do a local end from v */
3905 if(NOT_IMPOSSIBLE(cm->endsc[v])) {
3906 for (j = jmin[v]; j <= jmax[v]; j++) {
3907 jp_v = j - jmin[v];
3908 for (dp_v = 0, d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; dp_v++, d++) {
3909 dp = ESL_MAX(d-sd, 0);
3910 alpha[v][jp_v][dp_v] = el_scA[dp] + cm->endsc[v];
3911 }
3912 }
3913 }
3914 /* otherwise this state's deck has already been initialized to IMPOSSIBLE */
3915
3916 if(cm->sttype[v] == E_st) {
3917 for (j = jmin[v]; j <= jmax[v]; j++) {
3918 jp_v = j-jmin[v];
3919 ESL_DASSERT1((hdmin[v][jp_v] == 0));
3920 ESL_DASSERT1((hdmax[v][jp_v] == 0));
3921 alpha[v][jp_v][0] = 0.; /* for End states, d must be 0 */
3922 }
3923 }
3924 else if(cm->sttype[v] == IL_st) {
3925 /* update alpha[v][jp_v][dp_v] cells, for IL states, loop nesting order is:
3926 * for j { for d { for y { } } } because they can self transit, and a
3927 * alpha[v][j][d] cell must be complete (that is we must have looked at all children y)
3928 * before can start calc'ing for alpha[v][j][d+1] */
3929 for (j = jmin[v]; j <= jmax[v]; j++) {
3930 jp_v = j - jmin[v];
3931 yvalid_ct = 0;
3932 j_sdr = j - sdr;
3933
3934 /* determine which children y we can legally transit to for v, j */
3935 for (y = cm->cfirst[v], yoffset = 0; y < (cm->cfirst[v] + cm->cnum[v]); y++, yoffset++)
3936 if((j_sdr) >= jmin[y] && ((j_sdr) <= jmax[y])) yvalidA[yvalid_ct++] = yoffset; /* is j-sdr valid for state y? */
3937
3938 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) { /* for each valid d for v, j */
3939 i = j - d + 1;
3940 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha */
3941 for (yvalid_idx = 0; yvalid_idx < yvalid_ct; yvalid_idx++) { /* for each valid child y, for v, j */
3942 yoffset = yvalidA[yvalid_idx];
3943 y = cm->cfirst[v] + yoffset;
3944 jp_y_sdr = j - jmin[y] - sdr;
3945
3946 if((d-sd) >= hdmin[y][jp_y_sdr] && (d-sd) <= hdmax[y][jp_y_sdr]) { /* make sure d is valid for this v, j and y */
3947 dp_y_sd = d - sd - hdmin[y][jp_y_sdr];
3948 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
3949 ESL_DASSERT1((dp_y_sd >= 0 && dp_y_sd <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
3950 alpha[v][jp_v][dp_v] = FLogsum(alpha[v][jp_v][dp_v], alpha[y][jp_y_sdr][dp_y_sd] + tsc_v[yoffset]);
3951 }
3952 }
3953 alpha[v][jp_v][dp_v] += esc_v[dsq[i--]];
3954 alpha[v][jp_v][dp_v] = ESL_MAX(alpha[v][jp_v][dp_v], IMPOSSIBLE);
3955 }
3956 }
3957 }
3958 else if(cm->sttype[v] == IR_st) {
3959 /* update alpha[v][jp_v][dp_v] cells, for IR states, loop nesting order is:
3960 * for j { for d { for y { } } } because they can self transit, and a
3961 * alpha[v][j][d] cell must be complete (that is we must have looked at all children y)
3962 * before can start calc'ing for alpha[v][j][d+1] */
3963 for (j = jmin[v]; j <= jmax[v]; j++) {
3964 jp_v = j - jmin[v];
3965 yvalid_ct = 0;
3966 j_sdr = j - sdr;
3967
3968 /* determine which children y we can legally transit to for v, j */
3969 for (y = cm->cfirst[v], yoffset = 0; y < (cm->cfirst[v] + cm->cnum[v]); y++, yoffset++)
3970 if((j_sdr) >= jmin[y] && ((j_sdr) <= jmax[y])) yvalidA[yvalid_ct++] = yoffset; /* is j-sdr is valid for state y? */
3971
3972 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) { /* for each valid d for v, j */
3973 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha */
3974 for (yvalid_idx = 0; yvalid_idx < yvalid_ct; yvalid_idx++) { /* for each valid child y, for v, j */
3975 yoffset = yvalidA[yvalid_idx];
3976 y = cm->cfirst[v] + yoffset;
3977 jp_y_sdr = j - jmin[y] - sdr;
3978
3979 if((d-sd) >= hdmin[y][jp_y_sdr] && (d-sd) <= hdmax[y][jp_y_sdr]) { /* make sure d is valid for this v, j and y */
3980 dp_y_sd = d - sd - hdmin[y][jp_y_sdr];
3981 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
3982 ESL_DASSERT1((dp_y_sd >= 0 && dp_y_sd <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
3983 alpha[v][jp_v][dp_v] = FLogsum(alpha[v][jp_v][dp_v], alpha[y][jp_y_sdr][dp_y_sd] + tsc_v[yoffset]);
3984 }
3985 }
3986 alpha[v][jp_v][dp_v] += esc_v[dsq[j]];
3987 alpha[v][jp_v][dp_v] = ESL_MAX(alpha[v][jp_v][dp_v], IMPOSSIBLE);
3988 }
3989 }
3990 }
3991 else if(cm->sttype[v] != B_st) { /* entered if state v is (! IL && ! IR && ! B) */
3992 /* ML, MP, MR, D, S, E states cannot self transit, this means that all cells
3993 * in beta[v] are independent of each other, only depending on beta[y] for previously calc'ed y.
3994 * We can do the for loops in any nesting order, this implementation does what I think is most efficient:
3995 * for y { for j { for d { } } }
3996 */
3997 for (y = cm->cfirst[v]; y < (cm->cfirst[v] + cm->cnum[v]); y++) {
3998 yoffset = y - cm->cfirst[v];
3999 tsc = tsc_v[yoffset];
4000
4001 jn = ESL_MAX(jmin[v], jmin[y]+sdr);
4002 jx = ESL_MIN(jmax[v], jmax[y]+sdr);
4003 jpn = jn - jmin[v];
4004 jpx = jx - jmin[v];
4005 jp_y_sdr = jn - jmin[y] - sdr;
4006
4007 for (jp_v = jpn; jp_v <= jpx; jp_v++, jp_y_sdr++) {
4008 ESL_DASSERT1((jp_v >= 0 && jp_v <= (jmax[v]-jmin[v])));
4009 ESL_DASSERT1((jp_y_sdr >= 0 && jp_y_sdr <= (jmax[y]-jmin[y])));
4010
4011 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y_sdr] + sd);
4012 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y_sdr] + sd);
4013 dpn = dn - hdmin[v][jp_v];
4014 dpx = dx - hdmin[v][jp_v];
4015 dp_y_sd = dn - hdmin[y][jp_y_sdr] - sd;
4016
4017 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y_sd++) {
4018 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
4019 ESL_DASSERT1((dp_y_sd >= 0 && dp_y_sd <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
4020 alpha[v][jp_v][dp_v] = FLogsum(alpha[v][jp_v][dp_v], alpha[y][jp_y_sdr][dp_y_sd] + tsc);
4021 }
4022 }
4023 }
4024 /* add in emission score, if any */
4025 switch(cm->sttype[v]) {
4026 case ML_st:
4027 for (j = jmin[v]; j <= jmax[v]; j++) {
4028 jp_v = j - jmin[v];
4029 i = j - hdmin[v][jp_v] + 1;
4030 for (dp_v = 0; dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v]); dp_v++)
4031 alpha[v][jp_v][dp_v] += esc_v[dsq[i--]];
4032 }
4033 break;
4034 case MR_st:
4035 for (j = jmin[v]; j <= jmax[v]; j++) {
4036 jp_v = j - jmin[v];
4037 for (dp_v = 0; dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v]); dp_v++)
4038 alpha[v][jp_v][dp_v] += esc_v[dsq[j]];
4039 }
4040 break;
4041 case MP_st:
4042 for (j = jmin[v]; j <= jmax[v]; j++) {
4043 jp_v = j - jmin[v];
4044 i = j - hdmin[v][jp_v] + 1;
4045 for (dp_v = 0; dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v]); dp_v++)
4046 alpha[v][jp_v][dp_v] += esc_v[dsq[i--]*cm->abc->Kp+dsq[j]];
4047 }
4048 break;
4049 default:
4050 break;
4051 }
4052 /* ensure all cells are >= IMPOSSIBLE */
4053 for (j = jmin[v]; j <= jmax[v]; j++) {
4054 jp_v = j - jmin[v];
4055 for (dp_v = 0; dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v]); dp_v++)
4056 alpha[v][jp_v][dp_v] = ESL_MAX(alpha[v][jp_v][dp_v], IMPOSSIBLE);
4057 }
4058 }
4059 else { /* B_st */
4060 y = cm->cfirst[v]; /* left subtree */
4061 z = cm->cnum[v]; /* right subtree */
4062
4063 /* Any valid j must be within both state v and state z's j band
4064 * I think jmin[v] <= jmin[z] is guaranteed by the way bands are
4065 * constructed, but we'll check anyway.
4066 */
4067 jn = (jmin[v] > jmin[z]) ? jmin[v] : jmin[z];
4068 jx = (jmax[v] < jmax[z]) ? jmax[v] : jmax[z];
4069 /* the main j loop */
4070 for (j = jn; j <= jx; j++) {
4071 jp_v = j - jmin[v];
4072 jp_y = j - jmin[y];
4073 jp_z = j - jmin[z];
4074 kn = ((j-jmax[y]) > (hdmin[z][jp_z])) ? (j-jmax[y]) : hdmin[z][jp_z];
4075 kn = ESL_MAX(kn, 0); /* kn must be non-negative, added with fix to bug i36 */
4076 /* kn satisfies inequalities (1) and (3) (listed below)*/
4077 kx = ( jp_y < (hdmax[z][jp_z])) ? jp_y : hdmax[z][jp_z];
4078 /* kn satisfies inequalities (2) and (4) (listed below)*/
4079 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) {
4080 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha w/mem eff bands */
4081
4082 /* Find the first k value that implies a valid cell in the y and z decks.
4083 * This k must satisfy the following 6 inequalities (some may be redundant):
4084 * (1) k >= j-jmax[y];
4085 * (2) k <= j-jmin[y];
4086 * 1 and 2 guarantee (j-k) is within state y's j band
4087 *
4088 * (3) k >= hdmin[z][j-jmin[z]];
4089 * (4) k <= hdmax[z][j-jmin[z]];
4090 * 3 and 4 guarantee k is within z's j=(j), d band
4091 *
4092 * (5) k >= d-hdmax[y][j-jmin[y]-k];
4093 * (6) k <= d-hdmin[y][j-jmin[y]-k];
4094 * 5 and 6 guarantee (d-k) is within state y's j=(j-k) d band
4095 *
4096 * kn and kx were set above (outside (for (dp_v...) loop) that
4097 * satisfy 1-4 (b/c 1-4 are d-independent and k-independent)
4098 * RHS of inequalities 5 and 6 are dependent on k, so we check
4099 * for these within the next for loop.
4100 */
4101 for(k = kn; k <= kx; k++) {
4102 if((k >= d - hdmax[y][jp_y-k]) && k <= d - hdmin[y][jp_y-k]) {
4103 /* for current k, all 6 inequalities have been satisified
4104 * so we know the cells corresponding to the platonic
4105 * matrix cells alpha[v][j][d], alpha[y][j-k][d-k], and
4106 * alpha[z][j][k] are all within the bands. These
4107 * cells correspond to alpha[v][jp_v][dp_v],
4108 * alpha[y][jp_y-k][d-hdmin[jp_y-k]-k],
4109 * and alpha[z][jp_z][k-hdmin[jp_z]];
4110 */
4111 kp_z = k-hdmin[z][jp_z];
4112 dp_y = d-hdmin[y][jp_y-k];
4113 alpha[v][jp_v][dp_v] = FLogsum(alpha[v][jp_v][dp_v], alpha[y][jp_y-k][dp_y - k] + alpha[z][jp_z][kp_z]);
4114 }
4115 }
4116 }
4117 }
4118 } /* finished calculating deck v. */
4119
4120 } /* end of for (v = cm->M-1; v >= 0; v--) */
4121
4122 /* Deal with local begins.
4123 * If local begins are off, all hits must be rooted at v=0.
4124 * With local begins on, the hit is rooted at the second state in
4125 * the traceback (e.g. after 0), the internal entry point.
4126 *
4127 * Hits rooted at 0 that not involved with local begins are
4128 * already calc'ed from the v loop with v == 0
4129 */
4130
4131 /* Report all possible hits, but only after looking at local begins (if they're on) */
4132 v = 0;
4133 sd = sdr = 0;
4134 jpn = 0;
4135 jpx = jmax[v] - jmin[v];
4136 j = jmin[v];
4137
4138 ESL_ALLOC(bestr, sizeof(int) * (W+1));
4139 ESL_ALLOC(bestsc, sizeof(float) * (W+1));
4140
4141 /* update gamma, by specifying all hits with j < jmin[0] are impossible */
4142 if(gamma != NULL) {
4143 for(j = i0; j < jmin[v]; j++) {
4144 if((status = UpdateGammaHitMx (cm, errbuf, PLI_PASS_STD_ANY, gamma, j, -1, -1,
4145 NULL, /* NULL for bestsc tells UpdateGammaHitMx() no hits are possible for this j */
4146 bestr, NULL, W, act)) != eslOK) return status;
4147 }
4148 }
4149
4150 for (jp_v = jpn; jp_v <= jpx; jp_v++, jp_y++, j++) {
4151 esl_vec_ISet(bestr, (W+1), 0); /* init bestr to 0, all hits are rooted at 0 unless we find a better local begin below */
4152 esl_vec_FSet(bestsc, (W+1), IMPOSSIBLE); /* init bestr to 0, all hits are rooted at 0 unless we find a better local begin below */
4153 if (cm->flags & CMH_LOCAL_BEGIN) {
4154 for (y = 1; y < cm->M; y++) {
4155 if(NOT_IMPOSSIBLE(cm->beginsc[y]) && (j >= jmin[y] && j <= jmax[y])) {
4156 assert(cm->sttype[v] != BEGL_S); /* local begins into BEGL_S are impossible */
4157 jp_y = j - jmin[y];
4158 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y]);
4159 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y]);
4160 dpn = dn - hdmin[v][jp_v];
4161 dpx = dx - hdmin[v][jp_v];
4162 dp_y = dn - hdmin[y][jp_y];
4163 d = dn;
4164 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y++, d++) {
4165 /*alpha[0][jp_v][dp_v] = FLogsum(alpha[0][jp_v][dp_v], alpha[y][jp_y][dp_y] + cm->beginsc[y]);*/
4166 sc = alpha[y][jp_y][dp_y] + cm->beginsc[y];
4167 if(sc > alpha[0][jp_v][dp_v]) {
4168 alpha[0][jp_v][dp_v] = sc;
4169 bestr[d] = y;
4170 }
4171 }
4172 }
4173 } /* end of for(y = 1; y < cm->M; y++) */
4174 } /* end of if(cm->flags & CMH_LOCAL_BEGIN */
4175
4176 /* for this j, fill in bestsc for all valid d values, update
4177 * vsc_root (best overall score), and update envelope boundaries
4178 * (if necessary)
4179 */
4180 dpn = 0;
4181 dpx = hdmax[v][jp_v] - hdmin[v][jp_v];
4182 for(dp_v = dpn; dp_v <= dpx; dp_v++) {
4183 d = dp_v + hdmin[v][jp_v];
4184 bestsc[d] = alpha[0][jp_v][dp_v];
4185 vsc_root = ESL_MAX(vsc_root, alpha[0][jp_v][dp_v]);
4186 }
4187 /* update envelope boundaries, if nec */
4188 if(do_env_defn) {
4189 j = jp_v + jmin[v];
4190 for(dp_v = dpn; dp_v <= dpx; dp_v++) {
4191 if(alpha[0][jp_v][dp_v] >= env_cutoff) {
4192 d = dp_v + hdmin[v][jp_v];
4193 i = j - d + 1;
4194 envi = ESL_MIN(envi, i);
4195 envj = ESL_MAX(envj, j);
4196 }
4197 }
4198 }
4199
4200 /* if necessary, report all hits with valid d for this j, either to gamma or tmp_hitlist */
4201 if(gamma != NULL) {
4202 if((status = UpdateGammaHitMx (cm, errbuf, PLI_PASS_STD_ANY, gamma, j, hdmin[0][jp_v], hdmax[0][jp_v], bestsc, bestr, NULL, W, act)) != eslOK) return status;
4203 }
4204 if(tmp_hitlist != NULL) {
4205 if((status = ReportHitsGreedily(cm, errbuf, PLI_PASS_STD_ANY, j, hdmin[0][jp_v], hdmax[0][jp_v], bestsc, bestr, NULL, W, act, i0, j0, cutoff, tmp_hitlist)) != eslOK) return status;
4206 }
4207 } /* end of 'for (jp_v = jpn; jp_v <= jpx; jp_v++, jp_y++, j++) {' */
4208
4209 /* update gamma, by specifying all hits with j > jmax[0] are impossible */
4210 if(gamma != NULL) {
4211 for(j = jmax[v]+1; j <= j0; j++) {
4212 if((status = UpdateGammaHitMx(cm, errbuf, PLI_PASS_STD_ANY, gamma, j, -1, -1,
4213 NULL, /* NULL for bestsc tells UpdateGammaHitMx() no hits are possible for this j */
4214 bestr, NULL, W, act)) != eslOK) return status;
4215 }
4216 }
4217
4218 free(el_scA);
4219 free(yvalidA);
4220 free(bestr);
4221 free(bestsc);
4222 if (act != NULL) {
4223 for(i = 0; i <= W; i++) free(act[i]);
4224 free(act);
4225 }
4226
4227 /* If recovering hits in a non-greedy manner, do the gamma traceback, then free gamma */
4228 if(gamma != NULL) {
4229 TBackGammaHitMx(gamma, hitlist, i0, j0);
4230 FreeGammaHitMx(gamma);
4231 }
4232 /* If reporting hits in a greedy manner, remove overlaps greedily from the tmp_hitlist
4233 * then copy remaining hits to master <hitlist>. Then free tmp_hitlist.
4234 */
4235 if(tmp_hitlist != NULL) {
4236 for(h = 0; h < tmp_hitlist->N; h++) tmp_hitlist->unsrt[h].srcL = j0; /* so overlaps can be removed */
4237 cm_tophits_SortForOverlapRemoval(tmp_hitlist);
4238 if((status = cm_tophits_RemoveOrMarkOverlaps(tmp_hitlist, FALSE, errbuf)) != eslOK) return status;
4239 /*cm_tophits_Dump(stdout, tmp_hitlist);*/
4240 for(h = 0; h < tmp_hitlist->N; h++) {
4241 if(! (tmp_hitlist->hit[h]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) {
4242 if((status = cm_tophits_CloneHitMostly(tmp_hitlist, h, hitlist)) != eslOK) ESL_FAIL(status, errbuf, "problem copying hit to hitlist, out of memory?");
4243 }
4244 }
4245 cm_tophits_Destroy(tmp_hitlist);
4246 }
4247
4248 /* set envelope return variables if nec */
4249 if(ret_envi != NULL) { *ret_envi = (envi == j0+1) ? -1 : envi; }
4250 if(ret_envj != NULL) { *ret_envj = (envj == i0-1) ? -1 : envj; }
4251
4252 ESL_DPRINTF1(("#DEBUG: FastFInsideScanHB() return sc: %f\n", vsc_root));
4253 if (ret_sc != NULL) *ret_sc = vsc_root;
4254 return eslOK;
4255
4256 ERROR:
4257 ESL_FAIL(eslEMEM, errbuf, "Memory allocation error.\n");
4258 return 0.; /* never reached */
4259 }
4260
4261 /*****************************************************************
4262 * Benchmark driver
4263 *****************************************************************/
4264 #ifdef IMPL_SEARCH_BENCHMARK
4265 /* Next line is not optimized (debugging on) on MacBook Pro:
4266 * gcc -o benchmark-search -std=gnu99 -g -Wall -I. -L. -I../hmmer/src -L../hmmer/src -I../easel -L../easel -DIMPL_SEARCH_BENCHMARK cm_dpsearch.c -linfernal -lhmmer -leasel -lm
4267 * ./benchmark-search <cmfile>
4268 */
4269
4270 #include <esl_config.h>
4271 #include "config.h"
4272
4273 #include <stdio.h>
4274 #include <stdlib.h>
4275 #include <string.h>
4276 #include <time.h>
4277
4278 #include "easel.h"
4279 #include <esl_getopts.h>
4280 #include <esl_histogram.h>
4281 #include <esl_sqio.h>
4282 #include <esl_stats.h>
4283 #include <esl_stopwatch.h>
4284 #include <esl_vectorops.h>
4285 #include <esl_wuss.h>
4286
4287 #include "hmmer.h"
4288
4289 #include "infernal.h"
4290
4291 static ESL_OPTIONS options[] = {
4292 /* name type default env range toggles reqs incomp help docgroup*/
4293 { "-h", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
4294 { "-g", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "search in glocal mode [default: local]", 0 },
4295 { "-w", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "also execute reference CYK scan implementation", 0 },
4296 { "--noqdb", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "also execute non-banded optimized CYK scan implementation", 0 },
4297 { "--iins", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "also execute optimized int inside scan implementation", 0 },
4298 { "--riins", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "also execute reference int inside scan implementation", 0 },
4299 { "--fins", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "also execute optimized float inside scan implementation", 0 },
4300 { "--rfins", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "also execute reference float inside scan implementation", 0 },
4301 { "--hbanded", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "also execute HMM banded CYK scan implementation", 0 },
4302 { "--ihbanded",eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "also execute HMM banded Inside scan implementation", 0 },
4303 { "--tau", eslARG_REAL, "1e-7",NULL, "0<x<1",NULL,"--hbanded", NULL, "set tail loss prob for --hbanded to <x>", 0 },
4304 { "--scan2bands",eslARG_NONE, FALSE, NULL, NULL, NULL,"--hbanded", NULL, "derive HMM bands from scanning Forward/Backward", 0 },
4305 { "--sums", eslARG_NONE, FALSE, NULL, NULL, NULL,"--hbanded", NULL, "use posterior sums during HMM band calculation (widens bands)", 0 },
4306 { "--mxsize", eslARG_REAL, "256.0", NULL, "x>0.",NULL, NULL, NULL, "set maximum allowable DP matrix size to <x> (Mb)", 0 },
4307 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
4308 };
4309 static char usage[] = "[-options] <cmfile>";
4310 static char banner[] = "benchmark driver for an optimized scanning CYK implementation";
4311
4312 int
4313 main(int argc, char **argv)
4314 {
4315 int status;
4316 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 2, argc, argv, banner, usage);
4317 CM_t *cm;
4318 ESL_STOPWATCH *w = esl_stopwatch_Create();
4319 ESL_ALPHABET *abc = NULL;
4320 ESL_DSQ *dsq;
4321 int i;
4322 float sc;
4323 char *cmfile = esl_opt_GetArg(go, 1);
4324 char *seqfile = esl_opt_GetArg(go, 2);
4325 CM_FILE *cmfp; /* open input CM file stream */
4326 ESL_SQFILE *sqfp = NULL; /* open sequence input file stream */
4327 ESL_SQ *sq = NULL; /* a sequence */
4328 int qdbidx;
4329 char errbuf[eslERRBUFSIZE];
4330 int L; /* sequence length */
4331 float size_limit = esl_opt_GetReal(go, "--mxsize");
4332
4333 /* open CM file */
4334 if ((status = cm_file_Open(cmfile, NULL, FALSE, &cmfp, errbuf)) != eslOK) cm_Fail("Failed to open covariance model save file\n", cmfile);
4335 if ((status = cm_file_Read(cmfp, TRUE, &abc, &cm)) != eslOK) cm_Fail("Failed to read a CM from cm file\n");
4336 cm_file_Close(cmfp);
4337
4338 /* open the sequence file */
4339 status = esl_sqfile_OpenDigital(cm->abc, seqfile, eslSQFILE_UNKNOWN, NULL, &sqfp);
4340 if (status == eslENOTFOUND) esl_fatal("File %s doesn't exist or is not readable\n", seqfile);
4341 else if (status == eslEFORMAT) esl_fatal("Couldn't determine format of sequence file %s\n", seqfile);
4342 else if (status == eslEINVAL) esl_fatal("Can't autodetect stdin or .gz.");
4343 else if (status != eslOK) esl_fatal("Sequence file open failed with error %d.\n", status);
4344
4345 /* configure cm */
4346 if(! esl_opt_GetBoolean(go, "-g")) cm->config_opts |= CM_CONFIG_LOCAL;
4347 if( esl_opt_GetBoolean(go, "--sums")) cm->search_opts |= CM_SEARCH_SUMS;
4348 if( esl_opt_GetBoolean(go, "--hbanded")) cm->search_opts |= CM_SEARCH_HBANDED;
4349 if( esl_opt_GetBoolean(go, "--ihbanded")) cm->search_opts |= CM_SEARCH_HBANDED;
4350 if( esl_opt_GetBoolean(go, "--noqdb")) cm->search_opts |= CM_SEARCH_NONBANDED;
4351 else cm->search_opts |= CM_SEARCH_QDB;
4352 cm->config_opts |= CM_CONFIG_SCANMX;
4353 cm->tau = esl_opt_GetReal(go, "--tau"); /* this will be DEFAULT_TAU unless changed at command line */
4354 qdbidx = esl_opt_GetBoolean(go, "--noqdb") ? SMX_NOQDB : SMX_QDB1_TIGHT;
4355
4356 if((status = cm_Configure(cm, errbuf, -1)) != eslOK) cm_Fail(errbuf);
4357
4358 /* setup logsum lookups (could do this only if nec based on options, but this is safer) */
4359 init_ilogsum();
4360 FLogsumInit();
4361
4362 i = 0;
4363 sq = esl_sq_CreateDigital(cm->abc);
4364 while((status = esl_sqio_Read(sqfp, sq)) == eslOK) {
4365 i++;
4366 L = sq->n;
4367 dsq = sq->dsq;
4368 cm->search_opts &= ~CM_SEARCH_INSIDE;
4369
4370 esl_stopwatch_Start(w);
4371 if((status = FastCYKScan(cm, errbuf, cm->smx, qdbidx, dsq, 1, L, 0., NULL, FALSE, 0., NULL, NULL, NULL, &sc)) != eslOK) cm_Fail(errbuf);
4372 printf("%4d %-30s %10.4f bits ", i, "FastCYKScan(): ", sc);
4373 esl_stopwatch_Stop(w);
4374 esl_stopwatch_Display(stdout, w, " CPU time: ");
4375
4376 if (esl_opt_GetBoolean(go, "-w")) {
4377 esl_stopwatch_Start(w);
4378 if((status = RefCYKScan(cm, errbuf, cm->smx, qdbidx, dsq, 1, L, 0., NULL, FALSE, 0., NULL, NULL, NULL, &sc)) != eslOK) cm_Fail(errbuf);
4379 printf("%4d %-30s %10.4f bits ", i, "RefCYKScan(): ", sc);
4380 esl_stopwatch_Stop(w);
4381 esl_stopwatch_Display(stdout, w, " CPU time: ");
4382 }
4383 /* integer inside implementations */
4384 if (esl_opt_GetBoolean(go, "--iins")) {
4385 cm->search_opts |= CM_SEARCH_INSIDE;
4386 esl_stopwatch_Start(w);
4387 if((status = FastIInsideScan(cm, errbuf, cm->smx, qdbidx, dsq, 1, L, 0., NULL, FALSE, 0., NULL, NULL, NULL, &sc)) != eslOK) cm_Fail(errbuf);
4388 printf("%4d %-30s %10.4f bits ", i, "FastIInsideScan(): ", sc);
4389 esl_stopwatch_Stop(w);
4390 esl_stopwatch_Display(stdout, w, " CPU time: ");
4391 }
4392
4393 if (esl_opt_GetBoolean(go, "--riins")) {
4394 cm->search_opts |= CM_SEARCH_INSIDE;
4395 esl_stopwatch_Start(w);
4396 if((status = RefIInsideScan(cm, errbuf, cm->smx, qdbidx, dsq, 1, L, 0., NULL, FALSE, 0., NULL, NULL, NULL, &sc)) != eslOK) cm_Fail(errbuf);
4397 printf("%4d %-30s %10.4f bits ", i, "RefIInsideScan(): ", sc);
4398 esl_stopwatch_Stop(w);
4399 esl_stopwatch_Display(stdout, w, " CPU time: ");
4400 }
4401 /* float inside implementations */
4402 if (esl_opt_GetBoolean(go, "--fins")) {
4403 cm->search_opts |= CM_SEARCH_INSIDE;
4404 esl_stopwatch_Start(w);
4405 if((status = FastFInsideScan(cm, errbuf, cm->smx, qdbidx, dsq, 1, L, 0., NULL, FALSE, 0., NULL, NULL, NULL, &sc)) != eslOK) cm_Fail(errbuf);
4406 printf("%4d %-30s %10.4f bits ", i, "FastFInsideScan(): ", sc);
4407 esl_stopwatch_Stop(w);
4408 esl_stopwatch_Display(stdout, w, " CPU time: ");
4409 }
4410
4411 if (esl_opt_GetBoolean(go, "--rfins")) {
4412 cm->search_opts |= CM_SEARCH_INSIDE;
4413 esl_stopwatch_Start(w);
4414 if((status = RefFInsideScan(cm, errbuf, cm->smx, qdbidx, dsq, 1, L, 0., NULL, FALSE, 0., NULL, NULL, NULL, &sc)) != eslOK) cm_Fail(errbuf);
4415 printf("%4d %-30s %10.4f bits ", i, "RefFInsideScan(): ", sc);
4416 esl_stopwatch_Stop(w);
4417 esl_stopwatch_Display(stdout, w, " CPU time: ");
4418 }
4419 if (esl_opt_GetBoolean(go, "--hbanded")) {
4420 esl_stopwatch_Start(w);
4421 if((status = cp9_Seq2Bands(cm, errbuf, cm->cp9_mx, cm->cp9_bmx, cm->cp9_bmx, dsq, 1, L, cm->cp9b, TRUE, NULL, 0)) != eslOK) cm_Fail(errbuf);
4422 if((status = FastCYKScanHB(cm, errbuf, cm->hbmx, size_limit, dsq, 1, L, 0., NULL, FALSE, 0., NULL, NULL, &sc)) != eslOK) cm_Fail(errbuf);
4423 printf("%4d %-30s %10.4f bits ", i, "FastCYKScanHB(): ", sc);
4424 esl_stopwatch_Stop(w);
4425 esl_stopwatch_Display(stdout, w, " CPU time: ");
4426 }
4427 if (esl_opt_GetBoolean(go, "--ihbanded")) {
4428 esl_stopwatch_Start(w);
4429 if((status = cp9_Seq2Bands(cm, errbuf, cm->cp9_mx, cm->cp9_bmx, cm->cp9_bmx, dsq, 1, L, cm->cp9b, TRUE, NULL, 0)) != eslOK) cm_Fail(errbuf);
4430 if((status = FastFInsideScanHB(cm, errbuf, cm->hbmx, size_limit, dsq, 1, L, 0., NULL, FALSE, 0., NULL, NULL, &sc)) != eslOK) cm_Fail(errbuf);
4431 printf("%4d %-30s %10.4f bits ", i, "FastFInsideScanHB(): ", sc);
4432 esl_stopwatch_Stop(w);
4433 esl_stopwatch_Display(stdout, w, " CPU time: ");
4434 }
4435 printf("\n");
4436 esl_sq_Reuse(sq);
4437 }
4438
4439 FreeCM(cm);
4440 esl_sq_Destroy(sq);
4441 esl_alphabet_Destroy(abc);
4442 esl_stopwatch_Destroy(w);
4443 esl_getopts_Destroy(go);
4444 return 0;
4445 }
4446 #endif /*IMPL_SEARCH_BENCHMARK*/
4447