Debian Codegraph @

src/cm_dpsearch_trunc.c

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