Debian Codegraph @

src/cm_dpalign_trunc.c

raw symbols SCIP
1 /* cm_dpalign_trunc.c
2 *
3 * DP functions for truncated HMM banded and non-banded, non-D&C CM
4 * alignment of a full target sequence.
5 *
6 * All functions use a DP matrix and or shadow matrix, either
7 * non-banded (CM_TR_MX, CM_TR_SHADOW_MX) or HMM banded (CM_TR_HB_MX,
8 * CM_TR_HB_SHADOW_MX). The HMM banded matrices only have cells
9 * within bands allocated, and further for each state v, J, L, R, or T
10 * matrix cells are only available if cp9b->do_{J,L,R,T}[v]. The bands
11 * are derived from a HMM Forward/Backward alignment of the target
12 * sequence and are stored in a CP9Bands_t object, a pointer to which
13 * must exist in the cm (CM_t object).
14 *
15 * The non-banded, non-D&C alignment functions are mainly useful for
16 * understanding and/or debugging the HMM banded versions. These are
17 * consistent (same logic/code organization) with their HMM banded
18 * counterparts. They are memory intensive. For small memory
19 * non-banded alignment functions see truncyk.c.
20 *
21 * Many of the functions here were based on analogous ones for
22 * standard (non-truncated) CYK/Inside/Outside alignment in
23 * cm_dpalign.c.
24 *
25 * List of functions:
26 * non-banded version HMM banded version
27 * ------------------------- -----------------------
28 * cm_tr_alignT() cm_tr_alignT_hb()
29 * cm_TrAlignSizeNeeded() cm_TrAlignSizeNeededHB()
30 * cm_TrAlign() cm_TrAlignHB()
31 * cm_TrCYKInsideAlign() cm_TrCYKInsideAlignHB()
32 * cm_TrInsideAlign() cm_TrInsideAlignHB()
33 * cm_TrOptAccAlign() cm_TrOptAccAlignHB()
34 * cm_TrCYKOutsideAlign()* cm_TrCYKOutsideAlignHB()*
35 * cm_TrOutsideAlign() cm_TrOutsideAlignHB()
36 * cm_TrPosterior() cm_TrPosteriorHB()
37 * cm_TrEmitterPosterior() cm_TrEmitterPosteriorHB()
38 *
39 * * cm_TrCYKOutsideAlign() and cm_TrCYKOutsideAlignHB() are for
40 * reference and debugging only. They're not called by any of the main
41 * Infernal programs, only by test programs.
42 *
43 * Notes specific to truncated alignment:
44 *
45 * In truncated alignment, four matrices (J, L, R, and T) exist where
46 * there was only one in standard alignment (which is essentially
47 * implicitly the J matrix). For each mode, TrCYKInside and TrInside
48 * functions will find the optimal alignment for that mode and store
49 * it in {J,L,R,T}alpha[0][L][L] (or the HMM banded equivalent
50 * {J,L,R,T}alpha[0][jp_0][Lp_0]). The overall optimal alignment is
51 * the highest scoring optimal alignment in any mode. That is, in
52 * TrInside we have to choose the mode and then the Inside score is
53 * the score of all alignments in that mode, not the score of all
54 * alignments in any mode.
55 *
56 * Naively, in non-banded mode we have to fill in all four matrices,
57 * but in many cases we already know the marginal mode of the entire
58 * alignment. This will always be TRUE in TrOutside, TrPosterior and
59 * TrEmitterPosterior because we must have already done TrCYKInside or
60 * TrInside and so we already know the optimal mode. In TrOutside it
61 * is vital that we only consider alignments in the optimal mode or
62 * else the TrPosterior values will be invalid (see ELN3 p.10-11). In
63 * some cases we we will know the optimal mode when we enter
64 * TrCYKInside or TrInside functions, i.e. if we're in a search
65 * pipeline and we've already determined there is a hit in a
66 * particular marginal mode by running a scanning TrCYK or TrInside
67 * function (see cm_dpsearch_trunc.c).
68 *
69 * If we know the optimal mode, stored in <optimal_mode>, upon
70 * entering any of these functions we can usually save time by only
71 * filling in a subset of the four matrices, this is controlled within
72 * the functions by the <fill_J>, <fill_L>, <fill_R> and <fill_T>
73 * parameters. Specifically:
74 *
75 * <optimal_mode> <fill_J> <fill_L> <fill_R> <fill_T>
76 * --------------- -------- -------- -------- --------
77 * TRMODE_J TRUE FALSE FALSE FALSE
78 * TRMODE_L TRUE TRUE FALSE FALSE
79 * TRMODE_R TRUE FALSE TRUE FALSE
80 * TRMODE_T TRUE TRUE TRUE TRUE
81 * TRMODE_UNKNOWN TRUE TRUE TRUE TRUE
82 *
83 * The <fill_*> values are set in cm_TrFillFromMode(). We then use the
84 * <fill_*> variables to save time in the DP recursions by skipping
85 * unnecessary matrices. Since fill_J is always TRUE we don't actually
86 * need a fill_J parameter. (For Outside functions fill_T
87 * is implicitly TRUE but we only have to set Tbeta values for a
88 * single cell per B state, the one corresponding to a full alignment
89 * of all 1..L residues are that state. This is done when initializing
90 * the Tbeta matrix.)
91 *
92 * <fill_*> values are used in both non-banded and HMM banded
93 * matrices. In HMM banded functions we have similar per-state
94 * information stored in cp9b->Jvalid[], cp9b->Lvalid[],
95 * cp9b->Rvalid[], cp9b->Tvalid[] arrays which dictate if we need to
96 * fill in J,L,R,T decks for each state. These were determined based
97 * on the HMM posterior values for the start and end positions of the
98 * alignment (see
99 * hmmband.c:cp9_MarginalCandidatesFromStartEndPositions()). These
100 * values can be used in combination with the fill_* values, that is,
101 * both are relevant. For example, if fill_L is FALSE, the we never
102 * fill in L matrix values for any state. But if it is TRUE, we only
103 * fill in L matrix values for those states for which cp9b->Lvalid[]
104 * is TRUE.
105 *
106 * EPN, Wed Sep 7 12:13:00 2011
107 */
108
109 #include <esl_config.h>
110 #include <p7_config.h>
111 #include "config.h"
112
113 #include <stdio.h>
114 #include <stdlib.h>
115 #include <assert.h>
116 #include <math.h>
117
118 #include "easel.h"
119 #include "esl_sqio.h"
120 #include "esl_stack.h"
121 #include "esl_stopwatch.h"
122 #include "esl_vectorops.h"
123
124 #include "hmmer.h"
125
126 #include "infernal.h"
127
128 static int cm_tr_alignT (CM_t *cm, char *errbuf, ESL_DSQ *dsq, int L, float size_limit, char optimal_mode, int pass_idx, int do_optacc, CM_TR_MX *mx, CM_TR_SHADOW_MX *shmx, CM_TR_EMIT_MX *emit_mx, Parsetree_t **ret_tr, char *ret_mode, float *ret_sc_or_pp);
129 static int cm_tr_alignT_hb(CM_t *cm, char *errbuf, ESL_DSQ *dsq, int L, float size_limit, char optimal_mode, int pass_idx, int do_optacc, CM_TR_HB_MX *mx, CM_TR_HB_SHADOW_MX *shmx, CM_TR_HB_EMIT_MX *emit_mx, Parsetree_t **ret_tr, char *ret_mode, float *ret_sc_or_pp);
130
131 /* Function: cm_tr_alignT()
132 * Date: EPN, Sat Sep 10 11:25:37 2011
133 * EPN, Sun Nov 18 19:21:30 2007 [cm_alignT()]
134 *
135 * Note: based on insideT() [SRE, Fri Aug 11 12:08:18 2000 [Pittsburgh]]
136 *
137 * Purpose: Call either cm_TrCYKInsideAlign() (if !<do_optacc>), or
138 * cm_TrOptAccAlign() (if <do_optacc>), get vjd shadow
139 * matrix; then trace back and append to an existing but
140 * empty parsetree tr. The full sequence 1..L will be
141 * aligned. This function is nearly identical to
142 * cm_tr_alignT_hb() with the important difference that HMM
143 * bands are not used here.
144 *
145 * If <do_optacc>==TRUE then <emit_mx> must != NULL and
146 * <optimal_mode> must not be TRMODE_UNKNOWN, it will have been
147 * determined by caller from a cm_TrInsideAlign() call and
148 * passed in. If <do_optacc> is FALSE, then we're doing CYK
149 * alignment and we may or may not know the truncation mode
150 * of the alignment yet. If we know (e.g. if we're being
151 * called from a search/scan pipeline that already ran a
152 * scanning trCYK) then <optimal_mode> will be TRMODE_J,
153 * TRMODE_L, TRMODE_R or TRMODE_T, if not (e.g. if we're
154 * being called for 'cmalign') then <optimal_mode> will be
155 * TRMODE_UNKNOWN and we'll determine it via CYK.
156 *
157 * Args: cm - the model
158 * errbuf - char buffer for reporting errors
159 * dsq - the digitized sequence [1..L]
160 * L - length of the dsq to align
161 * size_limit - max size in Mb for DP matrix
162 * optimal_mode - the optimal alignment mode, TRMODE_UNKNOWN if unknown
163 * pass_idx - pipeline pass index, indicates what truncation penalty to use
164 * do_optacc - TRUE to align with optimal accuracy, else use CYK
165 * mx - the DP matrix to fill in
166 * shmx - the shadow matrix to fill in
167 * emit_mx - the pre-filled emit matrix, must be non-NULL if do_optacc
168 * ret_tr - RETURN: the optimal parsetree
169 * ret_mode - RETURN: mode of optimal alignment (TRMODE_J | TRMODE_L | TRMODE_R | TRMODE_T)
170 * ret_sc_or_pp - RETURN: optimal score (CYK if !do_optacc, else avg PP of all 1..L residues)
171 *
172 * Returns: <eslOK> on success.
173 * Throws: <eslERANGE> if required DP matrix size exceeds <size_limit>, in
174 * this case, alignment has been aborted, ret_* variables are not valid
175 * <eslEINVAL> on invalid tro or traceback problem: bogus state
176 */
177 int
178 cm_tr_alignT(CM_t *cm, char *errbuf, ESL_DSQ *dsq, int L, float size_limit, char optimal_mode, int pass_idx, int do_optacc,
179 CM_TR_MX *mx, CM_TR_SHADOW_MX *shmx, CM_TR_EMIT_MX *emit_mx, Parsetree_t **ret_tr, char *ret_mode, float *ret_sc_or_pp)
180 {
181 int status;
182 Parsetree_t *tr = NULL; /* the parsetree */
183 float sc; /* the score of the CYK alignment */
184 float pp; /* avg pp of all emitted residues in optacc alignment */
185 ESL_STACK *pda_i; /* stack that tracks bifurc parent of a right start */
186 ESL_STACK *pda_c; /* stack that tracks mode of bifurc parent of a right start */
187 int v,j,d,i; /* indices for state, seq positions */
188 int k; /* right subtree len for bifurcs */
189 int y, yoffset; /* child state y, it's offset */
190 int bifparent; /* B_st parent */
191 /* variables specific to truncated version */
192 char mode; /* current truncation mode: TRMODE_J | TRMODE_L | TRMODE_R | TRMODE_T */
193 char prvmode, nxtmode; /* previous, next truncation mode */
194 int b; /* local entry state for best overall alignment */
195 int pty_idx; /* index for truncation penalty, determined by pass_idx */
196
197 if(do_optacc) {
198 if((status = cm_TrOptAccAlign(cm, errbuf, dsq, L,
199 size_limit, /* max size of DP matrix */
200 optimal_mode, /* marginal mode of optimal alignment */
201 pass_idx, /* truncation penalty index */
202 mx, /* the DP matrix, to expand and fill-in */
203 shmx, /* the shadow matrix, to expand and fill-in */
204 emit_mx, /* pre-calc'ed emit matrix */
205 &b, /* the entry point for optimal alignment */
206 &pp)) /* avg post prob of all emissions in optimally accurate parsetree */
207 != eslOK) return status;
208 mode = optimal_mode;
209 }
210 else {
211 if((status = cm_TrCYKInsideAlign(cm, errbuf, dsq, L,
212 size_limit, /* max size of DP matrix */
213 optimal_mode, /* marginal mode of optimal alignment, TRMODE_UNKNOWN if unknown */
214 pass_idx, /* truncation penalty index */
215 mx, /* the HMM banded mx */
216 shmx, /* the HMM banded shadow matrix */
217 &b, /* entry point for optimal alignment */
218 &mode, &sc)) /* mode (J,L,R or T) and score of CYK parsetree */
219 != eslOK) return status;
220 optimal_mode = mode;
221 }
222
223 /* Create and initialize the parsetree */
224 tr = CreateParsetree(100);
225 /* set the 2 truncation-specific parsetree values:
226 * is_std is always set to FALSE for truncation mode,
227 * trpenalty is truncation penalty assessed in DP function, differs if we're local or global
228 * and if we allowed 5' OR 3' truncations or 5' AND 3' truncations
229 */
230 tr->is_std = FALSE; /* lower is_std flag, now we'll know this parsetree was created by a truncated (non-standard) alignment function */
231 tr->pass_idx = pass_idx;
232 if((pty_idx = cm_tr_penalties_IdxForPass(pass_idx)) == -1) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_tr_alignT(), unexpected pass idx: %d", pass_idx);
233 tr->trpenalty = (cm->flags & CMH_LOCAL_BEGIN) ? cm->trp->l_ptyAA[pty_idx][b] : cm->trp->g_ptyAA[pty_idx][b];
234 InsertTraceNodewithMode(tr, -1, TRACE_LEFT_CHILD, 1, L, 0, mode); /* init: attach the root S */
235
236 pda_i = esl_stack_ICreate();
237 pda_c = esl_stack_CCreate();
238 if(pda_i == NULL) goto ERROR;
239 if(pda_c == NULL) goto ERROR;
240 v = 0;
241 i = 1;
242 j = d = L;
243
244 while (1) {
245 #if eslDEBUGLEVEL >= 1
246 if(cm->sttype[v] != EL_st) printf("#DEBUG: v: %4d mode: %4d j: %4d d: %4d\n", v, mode, j, d);
247 else printf("#DEBUG: v: %4d mode: %4d j: %4d d: %4d EL\n", v, mode, j, d);
248 #endif
249
250 if (cm->sttype[v] == B_st) {
251 /* get k, the len of right fragment */
252 if (mode == TRMODE_J) k = shmx->Jkshadow[v][j][d];
253 else if(mode == TRMODE_L) k = shmx->Lkshadow[v][j][d];
254 else if(mode == TRMODE_R) k = shmx->Rkshadow[v][j][d];
255 else if(mode == TRMODE_T) k = shmx->Tkshadow[v][j][d];
256 else ESL_FAIL(eslEINVAL, errbuf, "bogus truncation mode for B state: %d\n", mode);
257 /* if k is 0, right fragment is of length 0 */
258 /* determine mode of right child */
259 prvmode = mode;
260 if (mode == TRMODE_J) ; /* do nothing, in J mode, right child mode remains TRMODE_J */
261 else if(mode == TRMODE_L) mode = TRMODE_L; /* in TRMODE_L, right child is always Left marginal */
262 else if(mode == TRMODE_R) mode = shmx->Rkmode[v][j][d];
263 else if(mode == TRMODE_T) mode = TRMODE_L; /* in TRMODE_T, right child is always Left marginal */
264
265 /* Store info about the right fragment that we'll retrieve later:
266 */
267 if((status = esl_stack_CPush(pda_c, mode)) != eslOK) goto ERROR; /* remember the mode of right child */
268 if((status = esl_stack_IPush(pda_i, j)) != eslOK) goto ERROR; /* remember the end j */
269 if((status = esl_stack_IPush(pda_i, k)) != eslOK) goto ERROR; /* remember the subseq length k for right child */
270 if((status = esl_stack_IPush(pda_i, tr->n-1)) != eslOK) goto ERROR; /* remember the trace index of the parent B state */
271
272 /* Determine mode of left start state */
273 if (prvmode == TRMODE_J) mode = TRMODE_J;
274 else if(prvmode == TRMODE_L) mode = shmx->Lkmode[v][j][d];
275 else if(prvmode == TRMODE_R) mode = TRMODE_R; /* for R mode, left child is always Right marginal */
276 else if(prvmode == TRMODE_T) mode = TRMODE_R; /* for T mode, left child is always Right marginal */
277
278 /* Deal with attaching left start state.
279 */
280 j = j-k;
281 d = d-k;
282 i = j-d+1;
283
284 y = cm->cfirst[v];
285 InsertTraceNodewithMode(tr, tr->n-1, TRACE_LEFT_CHILD, i, j, y, mode);
286 v = y;
287
288 #if eslDEBUGLEVEL >= 2
289 /* Uncomment to dump parsetree */
290 /* printf("added BEGL_S, dumping parsetree (prvmode: %d mode: %d:\n", prvmode, mode); */
291 /* ParsetreeDump(stdout, tr, cm, dsq); */
292 #endif
293 } else if (cm->sttype[v] == E_st || cm->sttype[v] == EL_st) {
294 /* We don't trace back from an E or EL. Instead, we're done with the
295 * left branch of the tree, and we try to swing over to the right
296 * branch by popping a right start off the stack and attaching
297 * it. If the stack is empty, then we're done with the
298 * traceback altogether. This is the only way to break the
299 * while (1) loop.
300 */
301 if (esl_stack_IPop(pda_i, &bifparent) == eslEOD) break;
302 esl_stack_IPop(pda_i, &d);
303 esl_stack_IPop(pda_i, &j);
304 esl_stack_CPop(pda_c, &mode);
305 v = tr->state[bifparent]; /* recover state index of B */
306 y = cm->cnum[v]; /* find state index of right S */
307 i = j-d+1;
308 /* attach the S to the right */
309 InsertTraceNodewithMode(tr, bifparent, TRACE_RIGHT_CHILD, i, j, y, mode);
310
311 #if eslDEBUGLEVEL >= 2
312 /* Uncomment to dump parsetree */
313 /* printf("added E or EL, dumping parsetree:\n"); */
314 /* ParsetreeDump(stdout, tr, cm, dsq); */
315 #endif
316
317 v = y;
318 }
319 else {
320 /* get yoffset */
321 if (mode == TRMODE_J) yoffset = shmx->Jyshadow[v][j][d];
322 else if(mode == TRMODE_L) yoffset = shmx->Lyshadow[v][j][d];
323 else if(mode == TRMODE_R) yoffset = shmx->Ryshadow[v][j][d];
324 else if(mode == TRMODE_T) {
325 /* v==0 is a special case, must be a local begin (shmx->Tyshadow[] doesn't exist) */
326 if(v == 0) yoffset = USED_TRUNC_BEGIN;
327 else ESL_FAIL(eslEINVAL, errbuf, "truncation mode T for non B, not ROOT_S state");
328 }
329 else {
330 ESL_FAIL(eslEINVAL, errbuf, "bogus truncation mode %d\n", mode);
331 }
332 #if eslDEBUGLEVEL >= 2
333 printf("#DEBUG: v: %d std mode: %d yoffset: %d ", v, mode, yoffset);
334 #endif
335 /* determine nxtmode, and correct yoffset */
336 if (yoffset == USED_TRUNC_BEGIN) { yoffset = USED_TRUNC_BEGIN; nxtmode = mode; } /* yoffset, mode don't change */
337 else if(yoffset == USED_TRUNC_END) { yoffset = USED_TRUNC_END; } /* nxtmode is irrelevant in this case */
338 else if(yoffset == USED_EL) { yoffset = USED_EL; } /* nxtmode is irrelevant in this case */
339 else if(yoffset >= TRMODE_R_OFFSET) { nxtmode = TRMODE_R; yoffset -= TRMODE_R_OFFSET; }
340 else if(yoffset >= TRMODE_L_OFFSET) { nxtmode = TRMODE_L; yoffset -= TRMODE_L_OFFSET; }
341 else if(yoffset >= TRMODE_J_OFFSET) { nxtmode = TRMODE_J; yoffset -= TRMODE_J_OFFSET; }
342 else ESL_FAIL(eslEINVAL, errbuf, "yoffset out of bounds: %d\n", yoffset);
343 #if eslDEBUGLEVEL >= 2
344 printf("new yoffset: %d nxtmode: %d\n", yoffset, nxtmode);
345 if(mode == TRMODE_J) printf("HEYA J v: %4d j: %4d d: %4d mode: %4d yoffset: %4d nxtmode: %4d\n", v, j, d, mode, yoffset, nxtmode);
346 if(mode == TRMODE_L) printf("HEYA L v: %4d j: %4d d: %4d mode: %4d yoffset: %4d nxtmode: %4d\n", v, j, d, mode, yoffset, nxtmode);
347 if(mode == TRMODE_R) printf("HEYA R v: %4d j: %4d d: %4d mode: %4d yoffset: %4d nxtmode: %4d\n", v, j, d, mode, yoffset, nxtmode);
348 #endif
349 switch (cm->sttype[v]) {
350 case D_st:
351 break;
352 case MP_st:
353 if ( mode == TRMODE_J ) i++;
354 if ( mode == TRMODE_L && d > 0 ) i++;
355 if ( mode == TRMODE_J ) j--;
356 if ( mode == TRMODE_R && d > 0 ) j--;
357 break;
358 case ML_st:
359 if ( mode == TRMODE_J ) i++;
360 if ( mode == TRMODE_L && d > 0 ) i++;
361 break;
362 case MR_st:
363 if ( mode == TRMODE_J ) j--;
364 if ( mode == TRMODE_R && d > 0 ) j--;
365 break;
366 case IL_st:
367 if ( mode == TRMODE_J ) i++;
368 if ( mode == TRMODE_L && d > 0 ) i++;
369 break;
370 case IR_st:
371 if ( mode == TRMODE_J ) j--;
372 if ( mode == TRMODE_R && d > 0 ) j--;
373 break;
374 case S_st:
375 break;
376 default: ESL_FAIL(eslEINVAL, errbuf, "bogus state type %d \n", cm->sttype[v]);
377 }
378 d = j-i+1;
379
380 if (yoffset == USED_EL || yoffset == USED_TRUNC_END)
381 { /* a local alignment end or a truncation end */
382 if(yoffset == USED_EL) {
383 InsertTraceNodewithMode(tr, tr->n-1, TRACE_LEFT_CHILD, i, j, cm->M, mode);
384 #if eslDEBUGLEVEL >= 2
385 /* Uncomment to dump parsetree */
386 /* printf("added USED_EL or USED_TRUNC_END, dumping parsetree:\n"); */
387 /* ParsetreeDump(stdout, tr, cm, dsq); */
388 #endif
389 }
390 v = cm->M; /* now we're in EL (if USED_TRUNC_END, we act like we are) */
391 }
392 else if (yoffset == USED_TRUNC_BEGIN)
393 { /* local begin; can only happen once, from root */
394 InsertTraceNodewithMode(tr, tr->n-1, TRACE_LEFT_CHILD, i, j, b, mode);
395 v = b;
396 }
397 else
398 {
399 mode = nxtmode;
400 y = cm->cfirst[v] + yoffset;
401 InsertTraceNodewithMode(tr, tr->n-1, TRACE_LEFT_CHILD, i, j, y, mode);
402 #if eslDEBUGLEVEL >= 2
403 /* Uncomment to dump parsetree */
404 /* printf("STD yoffset: %d\n", yoffset); */
405 /* printf("added standard, dumping parsetree:\n"); */
406 /* ParsetreeDump(stdout, tr, cm, dsq); */
407 #endif
408 v = y;
409 }
410 }
411 }
412 esl_stack_Destroy(pda_i); /* it should be empty; we could check; naaah. */
413 esl_stack_Destroy(pda_c); /* it should be empty; we could check; naaah. */
414
415 if(ret_tr != NULL) *ret_tr = tr; else FreeParsetree(tr);
416 if(ret_mode != NULL) *ret_mode = optimal_mode;
417 if(ret_sc_or_pp != NULL) *ret_sc_or_pp = do_optacc ? pp : sc;
418
419 return eslOK;
420
421 ERROR:
422 ESL_FAIL(status, errbuf, "out of memory");
423 return status; /* NEVERREACHED */
424 }
425
426 /* Function: cm_tr_alignT_hb()
427 * Date: EPN, Thu Sep 8 07:59:10 2011
428 * EPN 03.29.06 (cm_alignT_hb())
429 *
430 * Purpose: Call either cm_TrCYKInsideAlignHB() (if !<do_optacc>), or
431 * cm_TrOptAccAlignHB() (if <do_optacc>), get vjd shadow
432 * matrix; then trace back and append to an existing but
433 * empty parsetree tr. The full sequence 1..L will be
434 * aligned. This function is nearly identical to
435 * cm_tr_alignT() with the important difference that HMM
436 * bands are used here.
437 *
438 * If <do_optacc>==TRUE then <emit_mx> must != NULL and
439 * <optimal_mode> must not be TRMODE_UNKNOWN, it will have been
440 * determined by caller from a cm_TrInsideAlign() call and
441 * passed in. If <do_optacc> is FALSE, then we're doing CYK
442 * alignment and we may or may not know the truncation mode
443 * of the alignment yet. If we know (e.g. if we're being
444 * called from a search/scan pipeline that already ran a
445 * scanning trCYK) then <optimal_mode> will be TRMODE_J,
446 * TRMODE_L, TRMODE_R or TRMODE_T, if not (e.g. if we're
447 * being called for 'cmalign') then <optimal_mode> will be
448 * TRMODE_UNKNOWN and we'll determine it via CYK.
449 *
450 * Args: cm - the model
451 * errbuf - char buffer for reporting errors
452 * dsq - the digitized sequence [1..L]
453 * L - length of the dsq to align
454 * size_limit - max size in Mb for DP matrix
455 * optimal_mode - the optimal alignment mode, TRMODE_UNKNOWN if unknown
456 * pass_idx - pipeline pass index, indicates what truncation penalty to use
457 * do_optacc - TRUE to align with optimal accuracy, else use CYK
458 * mx - the DP matrix to fill in
459 * shmx - the shadow matrix to fill in
460 * emit_mx - the pre-filled emit matrix, must be non-NULL if do_optacc
461 * ret_tr - RETURN: the optimal parsetree
462 * ret_mode - RETURN: mode of optimal alignment (TRMODE_J | TRMODE_L | TRMODE_R | TRMODE_T)
463 * ret_sc_or_pp - RETURN: optimal score (CYK if !do_optacc, else avg PP of all 1..L residues)
464 *
465 * Returns: <eslOK> on success.
466 * Throws: <eslERANGE> if required DP matrix size exceeds <size_limit>, in
467 * this case, alignment has been aborted, ret_* variables are not valid
468 * <eslEINVAL> on invalide tro or traceback problem: bogus state
469 */
470 int
471 cm_tr_alignT_hb(CM_t *cm, char *errbuf, ESL_DSQ *dsq, int L, float size_limit, char optimal_mode, int pass_idx, int do_optacc,
472 CM_TR_HB_MX *mx, CM_TR_HB_SHADOW_MX *shmx, CM_TR_HB_EMIT_MX *emit_mx, Parsetree_t **ret_tr, char *ret_mode, float *ret_sc_or_pp)
473 {
474 int status;
475 Parsetree_t *tr = NULL; /* the parsetree */
476 float sc; /* the score of the CYK alignment */
477 float pp; /* avg pp of all emitted residues in optacc alignment */
478 ESL_STACK *pda_i; /* stack that tracks bifurc parent of a right start */
479 ESL_STACK *pda_c; /* stack that tracks mode of bifurc parent of a right start */
480 int v,j,d,i; /* indices for state, seq positions */
481 int k; /* right subtree len for bifurcs */
482 int y, yoffset; /* child state y, it's offset */
483 int bifparent; /* B_st parent */
484 int jp_v; /* j-jmin[v] for current j, and current v */
485 int dp_v; /* d-hdmin[v][jp_v] for current j, current v, current d*/
486 char mode; /* current truncation mode: TRMODE_J | TRMODE_L | TRMODE_R | TRMODE_T */
487 char prvmode, nxtmode; /* previous, next truncation mode */
488 int allow_S_trunc_end; /* set to true to allow d==0 BEGL_S and BEGR_S truncated ends */
489 int allow_S_local_end; /* set to true to allow d==0 BEGL_S and BEGR_S local ends if(do_optacc) */
490 int b; /* local entry state for best overall alignment */
491 int pty_idx; /* index for truncation penalty, determined by pass_idx */
492
493 /* pointers to cp9b data for convenience */
494 CP9Bands_t *cp9b = cm->cp9b;
495 int *jmin = cp9b->jmin;
496 int *jmax = cp9b->jmax;
497 int **hdmin = cp9b->hdmin;
498 int **hdmax = cp9b->hdmax;
499
500 /* ensure full sequence is within bands */
501 if (cp9b->jmin[0] > L || cp9b->jmax[0] < L) ESL_FAIL(eslEINVAL, errbuf, "cm_tr_alignT_hb(): L (%d) is outside ROOT_S's j band (%d..%d)\n", L, cp9b->jmin[0], cp9b->jmax[0]);
502 if (cp9b->hdmin[0][L-jmin[0]] > L || cp9b->hdmax[0][L-jmin[0]] < L) ESL_FAIL(eslEINVAL, errbuf, "cm_tr_alignT_hb(): L (%d) is outside ROOT_S's d band (%d..%d)\n", L, cp9b->hdmin[0][L-jmin[0]], cp9b->hdmax[0][L-jmin[0]]);
503
504 if(do_optacc) {
505 if((status = cm_TrOptAccAlignHB(cm, errbuf, dsq, L,
506 size_limit, /* max size of DP matrix */
507 optimal_mode, /* marginal mode of optimal alignment */
508 pass_idx, /* truncation penalty index */
509 mx, /* the DP matrix, to expand and fill-in */
510 shmx, /* the shadow matrix, to expand and fill-in */
511 emit_mx, /* pre-calc'ed emit matrix */
512 &b, /* the entry point for optimal alignment */
513 &pp)) /* avg post prob of all emissions in optimally accurate parsetree */
514 != eslOK) return status;
515 mode = optimal_mode;
516 }
517 else {
518 if((status = cm_TrCYKInsideAlignHB(cm, errbuf, dsq, L,
519 size_limit, /* max size of DP matrix */
520 optimal_mode, /* marginal mode of optimal alignment, TRMODE_UNKNOWN if unknown */
521 pass_idx, /* truncation penalty index */
522 mx, /* the HMM banded mx */
523 shmx, /* the HMM banded shadow matrix */
524 &b, /* entry point for optimal alignment */
525 &mode, &sc)) /* mode (J,L,R or T) and score of CYK parsetree */
526 != eslOK) return status;
527 optimal_mode = mode;
528 }
529
530 /* Create and initialize the parsetree */
531 tr = CreateParsetree(100);
532 /* set the 2 truncation-specific parsetree values:
533 * is_std is always set to FALSE for truncation mode,
534 * trpenalty is truncation penalty assessed in DP function, differs if we're local or global
535 * and if we allowed 5' OR 3' truncations or 5' AND 3' truncations
536 */
537 tr->is_std = FALSE; /* lower is_std flag, now we'll know this parsetree was created by a truncated (non-standard) alignment function */
538 tr->pass_idx = pass_idx;
539 if((pty_idx = cm_tr_penalties_IdxForPass(pass_idx)) == -1) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_tr_alignT_hb(), unexpected pass idx: %d", pass_idx);
540 tr->trpenalty = (cm->flags & CMH_LOCAL_BEGIN) ? cm->trp->l_ptyAA[pty_idx][b] : cm->trp->g_ptyAA[pty_idx][b];
541 InsertTraceNodewithMode(tr, -1, TRACE_LEFT_CHILD, 1, L, 0, mode); /* init: attach the root S */
542
543 pda_i = esl_stack_ICreate();
544 pda_c = esl_stack_CCreate();
545 if(pda_i == NULL) goto ERROR;
546 if(pda_c == NULL) goto ERROR;
547 v = 0;
548 i = 1;
549 j = d = L;
550
551 while (1) {
552 #if eslDEBUGLEVEL >= 1
553 if(cm->sttype[v] != EL_st) printf("#DEBUG: v: %4d mode: %4d j: %4d (%4d..%4d) d: %4d\n", v, mode, j, jmin[v], jmax[v], d);
554 else printf("#DEBUG: v: %4d mode: %4d j: %4d d: %4d EL\n", v, mode, j, d);
555 #endif
556 /* super special case for HMM banded truncated mode, explained below, after the crazy if */
557 if((cm->stid[v] == BEGL_S || cm->stid[v] == BEGR_S) && d == 0 &&
558 ((mode == TRMODE_J && (! cp9b->Jvalid[v])) || /* J mode, but J mode is disallowed for state v */
559 (mode == TRMODE_L && (! cp9b->Lvalid[v])) || /* L mode, but L mode is disallowed for state v */
560 (mode == TRMODE_R && (! cp9b->Rvalid[v])) || /* R mode, but R mode is disallowed for state v */
561 (j < jmin[v] || j > jmax[v]) || /* j is outside v's j band */
562 (d < hdmin[v][j-jmin[v]] || d > hdmax[v][j-jmin[v]]))) { /* j is within v's j band, but d is outside j's d band */
563 /* special case: v is a BEGL_S or BEGR_S and either we're in a
564 * truncated alignment mode for v that is disallowed by the
565 * bands or j is outside v's j band or j is within the band but
566 * d is outside j's d band. We allow this case if d == 0 b/c
567 * we're doing a truncated end out of this state immediately,
568 * i.e. we're not really using the state at all we're just using
569 * it so we can use its parent B state and its sister left or
570 * right start state. This only occurs if the parent bif state
571 * emitted the full sequence via the other child (BEGR_S or
572 * BEGL_S).
573 *
574 * This will usually occur if v is a BEGL_S and we're in R mode,
575 * or v is a BEGR_S and we're in L mode, but not always. We need
576 * to also allow a similar case that also occurs in
577 * *non-truncated* optimal accuracy alignment. See
578 * cm_dpalign.c::cm_alignT_hb() at the analogous point in that
579 * function for details.
580 */
581 ESL_DASSERT1(((cm->stid[v] == BEGL_S && mode == TRMODE_R) || (cm->stid[v] == BEGR_S && mode == TRMODE_L)));
582 if((cm->stid[v] == BEGL_S && mode == TRMODE_R) || (cm->stid[v] == BEGR_S && mode == TRMODE_L)) {
583 allow_S_trunc_end = TRUE; /* this sets yoffset to USED_TRUNC_END in the final 'else' of below code block */
584 allow_S_local_end = FALSE;
585 }
586 else if (do_optacc) {
587 allow_S_local_end = TRUE; /* this sets yoffset to USED_EL in the final 'else' of below code block */
588 allow_S_trunc_end = FALSE;
589 }
590 }
591 else if (cm->sttype[v] != EL_st) { /* normal case, determine jp_v, dp_v; j, d offset values given bands */
592 jp_v = j - jmin[v];
593 dp_v = d - hdmin[v][jp_v];
594 allow_S_trunc_end = FALSE;
595 allow_S_local_end = FALSE;
596 assert(j >= jmin[v] && j <= jmax[v]);
597 assert(d >= hdmin[v][jp_v] && d <= hdmax[v][jp_v]);
598 ESL_DASSERT1((j >= jmin[v] && j <= jmax[v]));
599 ESL_DASSERT1((d >= hdmin[v][jp_v] && d <= hdmax[v][jp_v]));
600 }
601
602 if (cm->sttype[v] == B_st) {
603 /* get k, the len of right fragment */
604 if (mode == TRMODE_J) k = shmx->Jkshadow[v][jp_v][dp_v];
605 else if(mode == TRMODE_L) k = shmx->Lkshadow[v][jp_v][dp_v];
606 else if(mode == TRMODE_R) k = shmx->Rkshadow[v][jp_v][dp_v];
607 else if(mode == TRMODE_T) k = shmx->Tkshadow[v][jp_v][dp_v];
608 else ESL_FAIL(eslEINVAL, errbuf, "bogus truncation mode for B state: %d\n", mode);
609 /* if k is 0, right fragment is of length 0 */
610 /* determine mode of right child */
611 prvmode = mode;
612 if (mode == TRMODE_J) ; /* do nothing, in J mode, right child mode remains TRMODE_J */
613 else if(mode == TRMODE_L) mode = TRMODE_L; /* in TRMODE_L, right child is always Left marginal */
614 else if(mode == TRMODE_R) mode = shmx->Rkmode[v][jp_v][dp_v];
615 else if(mode == TRMODE_T) mode = TRMODE_L; /* in TRMODE_T, right child is always Left marginal */
616
617 /* Store info about the right fragment that we'll retrieve later:
618 */
619 if((status = esl_stack_CPush(pda_c, mode)) != eslOK) goto ERROR; /* remember the mode of right child */
620 if((status = esl_stack_IPush(pda_i, j)) != eslOK) goto ERROR; /* remember the end j */
621 if((status = esl_stack_IPush(pda_i, k)) != eslOK) goto ERROR; /* remember the subseq length k for right child */
622 if((status = esl_stack_IPush(pda_i, tr->n-1)) != eslOK) goto ERROR; /* remember the trace index of the parent B state */
623
624 /* Determine mode of left start state */
625 if (prvmode == TRMODE_J) mode = TRMODE_J;
626 else if(prvmode == TRMODE_L) mode = shmx->Lkmode[v][jp_v][dp_v];
627 else if(prvmode == TRMODE_R) mode = TRMODE_R; /* for R mode, left child is always Right marginal */
628 else if(prvmode == TRMODE_T) mode = TRMODE_R; /* for T mode, left child is always Right marginal */
629
630 /* Deal with attaching left start state.
631 */
632 j = j-k;
633 d = d-k;
634 i = j-d+1;
635
636 y = cm->cfirst[v];
637 InsertTraceNodewithMode(tr, tr->n-1, TRACE_LEFT_CHILD, i, j, y, mode);
638 v = y;
639
640 #if eslDEBUGLEVEL >= 2
641 /* Uncomment to dump parsetree */
642 /* printf("added BEGL_S, dumping parsetree (prvmode: %d mode: %d:\n", prvmode, mode); */
643 /* ParsetreeDump(stdout, tr, cm, dsq); */
644 #endif
645 }
646 else if (cm->sttype[v] == E_st || cm->sttype[v] == EL_st) {
647 /* We don't trace back from an E or EL. Instead, we're done with the
648 * left branch of the tree, and we try to swing over to the right
649 * branch by popping a right start off the stack and attaching
650 * it. If the stack is empty, then we're done with the
651 * traceback altogether. This is the only way to break the
652 * while (1) loop.
653 */
654 if (esl_stack_IPop(pda_i, &bifparent) == eslEOD) break;
655 esl_stack_IPop(pda_i, &d);
656 esl_stack_IPop(pda_i, &j);
657 esl_stack_CPop(pda_c, &mode);
658 v = tr->state[bifparent]; /* recover state index of B */
659 y = cm->cnum[v]; /* find state index of right S */
660 i = j-d+1;
661 /* attach the S to the right */
662 InsertTraceNodewithMode(tr, bifparent, TRACE_RIGHT_CHILD, i, j, y, mode);
663 #if eslDEBUGLEVEL >= 2
664 /* Uncomment to dump parsetree */
665 /* printf("added E or EL, dumping parsetree:\n"); */
666 /* ParsetreeDump(stdout, tr, cm, dsq); */
667 #endif
668
669 v = y;
670 }
671 else {
672 /* get yoffset */
673 if(allow_S_trunc_end) {
674 yoffset = USED_TRUNC_END; /* nxt mode is irrelevant in this case */
675 }
676 else if(allow_S_local_end) {
677 yoffset = USED_EL; /* nxt mode is irrelevant in this case */
678 }
679 else {
680 if (mode == TRMODE_J) yoffset = shmx->Jyshadow[v][jp_v][dp_v];
681 else if(mode == TRMODE_L) yoffset = shmx->Lyshadow[v][jp_v][dp_v];
682 else if(mode == TRMODE_R) yoffset = shmx->Ryshadow[v][jp_v][dp_v];
683 else if(mode == TRMODE_T) {
684 /* v==0 is a special case, must be a local begin (shmx->Tyshadow[] doesn't exist) */
685 if(v == 0) yoffset = USED_TRUNC_BEGIN;
686 else ESL_FAIL(eslEINVAL, errbuf, "truncation mode T for non B, not ROOT_S state");
687 }
688 else {
689 ESL_FAIL(eslEINVAL, errbuf, "bogus truncation mode %d\n", mode);
690 }
691 }
692 #if eslDEBUGLEVEL >= 2
693 printf("#DEBUG: v: %d std mode: %d yoffset: %d ", v, mode, yoffset);
694 #endif
695 /* determine nxtmode, and correct yoffset */
696 if (yoffset == USED_TRUNC_BEGIN) { yoffset = USED_TRUNC_BEGIN; nxtmode = mode; } /* yoffset, mode don't change */
697 else if(yoffset == USED_TRUNC_END) { yoffset = USED_TRUNC_END; } /* nxtmode is irrelevant in this case */
698 else if(yoffset == USED_EL) { yoffset = USED_EL; } /* nxtmode is irrelevant in this case */
699 else if(yoffset >= TRMODE_R_OFFSET) { nxtmode = TRMODE_R; yoffset -= TRMODE_R_OFFSET; }
700 else if(yoffset >= TRMODE_L_OFFSET) { nxtmode = TRMODE_L; yoffset -= TRMODE_L_OFFSET; }
701 else if(yoffset >= TRMODE_J_OFFSET) { nxtmode = TRMODE_J; yoffset -= TRMODE_J_OFFSET; }
702 else ESL_FAIL(eslEINVAL, errbuf, "yoffset out of bounds: %d\n", yoffset);
703 #if eslDEBUGLEVEL >= 2
704 printf("new yoffset: %d nxtmode: %d\n", yoffset, nxtmode);
705 if(mode == TRMODE_J) printf("HEYA J v: %4d j: %4d d: %4d mode: %4d yoffset: %4d nxtmode: %4d\n", v, j, d, mode, yoffset, nxtmode);
706 if(mode == TRMODE_L) printf("HEYA L v: %4d j: %4d d: %4d mode: %4d yoffset: %4d nxtmode: %4d\n", v, j, d, mode, yoffset, nxtmode);
707 if(mode == TRMODE_R) printf("HEYA R v: %4d j: %4d d: %4d mode: %4d yoffset: %4d nxtmode: %4d\n", v, j, d, mode, yoffset, nxtmode);
708 #endif
709 switch (cm->sttype[v]) {
710 case D_st:
711 break;
712 case MP_st:
713 if ( mode == TRMODE_J ) i++;
714 if ( mode == TRMODE_L && d > 0 ) i++;
715 if ( mode == TRMODE_J ) j--;
716 if ( mode == TRMODE_R && d > 0 ) j--;
717 break;
718 case ML_st:
719 if ( mode == TRMODE_J ) i++;
720 if ( mode == TRMODE_L && d > 0 ) i++;
721 break;
722 case MR_st:
723 if ( mode == TRMODE_J ) j--;
724 if ( mode == TRMODE_R && d > 0 ) j--;
725 break;
726 case IL_st:
727 if ( mode == TRMODE_J ) i++;
728 if ( mode == TRMODE_L && d > 0 ) i++;
729 break;
730 case IR_st:
731 if ( mode == TRMODE_J ) j--;
732 if ( mode == TRMODE_R && d > 0 ) j--;
733 break;
734 case S_st:
735 break;
736 default: ESL_FAIL(eslEINVAL, errbuf, "bogus state type %d \n", cm->sttype[v]);
737 }
738 d = j-i+1;
739
740 if (yoffset == USED_EL || yoffset == USED_TRUNC_END)
741 { /* a local alignment end or a truncation end */
742 if(yoffset == USED_EL) {
743 InsertTraceNodewithMode(tr, tr->n-1, TRACE_LEFT_CHILD, i, j, cm->M, mode);
744 #if eslDEBUGLEVEL >= 2
745 /* Uncomment to dump parsetree */
746 /* printf("added USED_EL or USED_TRUNC_END, dumping parsetree:\n"); */
747 /* ParsetreeDump(stdout, tr, cm, dsq); */
748 #endif
749 }
750 v = cm->M; /* now we're in EL (if USED_TRUNC_END, we act like we are) */
751 }
752 else if (yoffset == USED_TRUNC_BEGIN)
753 { /* local begin; can only happen once, from root */
754 InsertTraceNodewithMode(tr, tr->n-1, TRACE_LEFT_CHILD, i, j, b, mode);
755 v = b;
756 }
757 else
758 {
759 mode = nxtmode;
760 y = cm->cfirst[v] + yoffset;
761 InsertTraceNodewithMode(tr, tr->n-1, TRACE_LEFT_CHILD, i, j, y, mode);
762 #if eslDEBUGLEVEL >= 2
763 /* Uncomment to dump parsetree */
764 /* printf("STD yoffset: %d\n", yoffset); */
765 /* printf("added standard, dumping parsetree:\n"); */
766 /* ParsetreeDump(stdout, tr, cm, dsq); */
767 #endif
768 v = y;
769 }
770 }
771 /*ParsetreeDump(stdout, tr, cm, dsq);*/
772 }
773 esl_stack_Destroy(pda_i); /* it should be empty; we could check; naaah. */
774 esl_stack_Destroy(pda_c); /* it should be empty; we could check; naaah. */
775
776 if(ret_tr != NULL) *ret_tr = tr; else FreeParsetree(tr);
777 if(ret_mode != NULL) *ret_mode = optimal_mode;
778 if(ret_sc_or_pp != NULL) *ret_sc_or_pp = do_optacc ? pp : sc;
779
780 return eslOK;
781
782 ERROR:
783 ESL_FAIL(status, errbuf, "out of memory");
784 return status; /* NEVERREACHED */
785 }
786
787
788 /* Function: cm_TrAlignSizeNeeded()
789 * Date: EPN, Thu Jan 12 10:15:08 2012
790 *
791 * Purpose: Determine size in Mb required to successfully call
792 * cm_TrAlign() for a given model <cm>, sequence length
793 * <L> and alignment options in <do_sample> and <do_post>.
794 *
795 * We are ignorant of any preset marginal alignment mode,
796 * because that doesn't affect how the matrices are
797 * allocated (although it can affect which cells are filled
798 * in).
799 *
800 * Return <eslERANGE> if required size exceeds size_limit.
801 *
802 * Args: cm - the covariance model
803 * errbuf - char buffer for reporting errors
804 * L - length of sequence
805 * size_limit - max size in Mb for all required matrices, return eslERANGE if exceeded
806 * do_sample - TRUE to sample a parsetree from the Inside matrix
807 * do_post - TRUE to do posteriors
808 * ret_mxmb - RETURN: size in Mb of required CM_TR_MX (we'll need 2 of these if do_post)
809 * ret_emxmb - RETURN: size in Mb of required CM_TR_EMIT_MX (0. if we won't need one)
810 * ret_shmxmb - RETURN: size in Mb of required CM_TR_SHADOW_MX (0. if we won't need one)
811 * ret_totmb - RETURN: size in Mb of all required matrices
812 *
813 * Returns: <eslOK> on success.
814 *
815 * Throws: <eslEINVAL> on contract violation
816 * <eslERANGE> if total size of all matrices exceeds <size_limit>
817 */
818 int
819 cm_TrAlignSizeNeeded(CM_t *cm, char *errbuf, int L, float size_limit, int do_sample, int do_post,
820 float *ret_mxmb, float *ret_emxmb, float *ret_shmxmb, float *ret_totmb)
821 {
822 int status;
823 float totmb = 0.; /* total Mb required for all matrices (that must be simultaneously in memory) */
824 float mxmb = 0.; /* Mb required for CM_MX */
825 float emxmb = 0.; /* Mb required for CM_EMIT_MX */
826 float shmxmb = 0.; /* Mb required for CM_SHADOW_MX */
827
828 /* we pass NULL values to the *_mx_SizeNeeded() functions because we don't care about cell counts */
829
830 /* we will always need an Inside or CYK matrix */
831 if((status = cm_tr_mx_SizeNeeded(cm, errbuf, L, NULL, NULL, NULL, NULL, &mxmb)) != eslOK) return status;
832 totmb = mxmb;
833
834 /* if calc'ing posteriors, we'll also need an Outside matrix (which
835 * we'll reuse as the Posterior matrix, so only count it once) and
836 * an emit matrix.
837 */
838 if(do_post) {
839 totmb += mxmb;
840 if((status = cm_tr_emit_mx_SizeNeeded(cm, errbuf, L, NULL, NULL, &emxmb)) != eslOK) return status;
841 totmb += emxmb;
842 }
843
844 /* if we're not sampling an alignment, we'll also need a shadow
845 * matrix for the traceback.
846 */
847 if(! do_sample) {
848 if((status = cm_tr_shadow_mx_SizeNeeded(cm, errbuf, L, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &shmxmb)) != eslOK) return status;
849 totmb += shmxmb;
850 }
851
852 if (ret_mxmb != NULL) *ret_mxmb = mxmb;
853 if (ret_emxmb != NULL) *ret_emxmb = emxmb;
854 if (ret_shmxmb != NULL) *ret_shmxmb = shmxmb;
855 if (ret_totmb != NULL) *ret_totmb = totmb;
856
857 #if eslDEBUGLEVEL >= 1
858 printf("#DEBUG: cm_TrAlignSizeNeeded()\n");
859 printf("#DEBUG: \t mxmb: %.2f\n", mxmb);
860 printf("#DEBUG: \t emxmb: %.2f\n", emxmb);
861 printf("#DEBUG: \t shmxmb:%.2f\n", shmxmb);
862 printf("#DEBUG: \t totmb: %.2f\n", totmb);
863 printf("#DEBUG: \t limit: %.2f\n", size_limit);
864 #endif
865
866 if(totmb > size_limit) ESL_FAIL(eslERANGE, errbuf, "non-banded truncated alignment mxes need %.2f Mb > %.2f Mb limit.\nUse --mxsize, --maxtau or --tau.", totmb, (float) size_limit);
867
868 return eslOK;
869 }
870
871 /* Function: cm_TrAlignSizeNeededHB()
872 * Date: EPN, Thu Jan 12 10:24:20 2012
873 *
874 * Purpose: Determine size in Mb required to successfully call
875 * cm_TrAlignHB() for a given model <cm>, sequence length
876 * <L>, HMM bands <cm->cp9b> and alignment options
877 * in <do_sample> and <do_post>.
878 *
879 * We are ignorant of any preset marginal alignment mode,
880 * because that doesn't affect how the matrices are
881 * allocated (although it can affect which cells are filled
882 * in).
883 *
884 * Return <eslERANGE> if required size exceeds size_limit.
885 *
886 * Args: cm - the covariance model
887 * errbuf - char buffer for reporting errors
888 * L - length of sequence
889 * size_limit - max size in Mb for all required matrices, return eslERANGE if exceeded
890 * do_sample - TRUE to sample a parsetree from the Inside matrix
891 * do_post - TRUE to do posteriors
892 * ret_mxmb - RETURN: size in Mb of required CM_TR_HB_MX (we'll need 2 of these if do_post)
893 * ret_emxmb - RETURN: size in Mb of required CM_TR_HB_EMIT_MX (0. if we won't need one)
894 * ret_shmxmb - RETURN: size in Mb of required CM_TR_HB_SHADOW_MX (0. if we won't need one)
895 * ret_cp9mxmb - RETURN: size in Mb of all CP9 matrices needed (fwd and possibly bck)
896 * ret_cmtotmb - RETURN: size in Mb of all CM matrices (ret_mxmb + ret_emxmb + ret_shmxmb)
897 * ret_totmb - RETURN: size in Mb of all required matrices
898 *
899 * Returns: <eslOK> on success.
900 *
901 * Throws: <eslEINVAL> on contract violation
902 * <eslERANGE> if total size of all matrices exceeds <size_limit>
903 */
904 int
905 cm_TrAlignSizeNeededHB(CM_t *cm, char *errbuf, int L, float size_limit, int do_sample, int do_post,
906 float *ret_mxmb, float *ret_emxmb, float *ret_shmxmb, float *ret_cp9mxmb,
907 float *ret_cmtotmb, float *ret_totmb)
908 {
909 int status;
910 float totmb = 0.; /* total Mb required for all matrices including cp9 matrices (that must be simultaneously in memory) */
911 float cmtotmb = 0.; /* total Mb required for all CM matrices */
912 float mxmb = 0.; /* Mb required for CM_MX */
913 float emxmb = 0.; /* Mb required for CM_EMIT_MX */
914 float shmxmb = 0.; /* Mb required for CM_SHADOW_MX */
915 float cp9mxmb = 0.; /* Mb required for two CP9_MX */
916
917 /* we pass NULL values to the *_mx_SizeNeeded() functions because we don't care about cell counts */
918
919 /* we will always need an Inside or CYK matrix */
920 if((status = cm_tr_hb_mx_SizeNeeded(cm, errbuf, cm->cp9b, L, NULL, NULL, NULL, NULL, &mxmb)) != eslOK) return status;
921 cmtotmb = mxmb;
922
923 /* if calc'ing posteriors, we'll also need an Outside matrix (which
924 * we'll reuse as the Posterior matrix, so only count it once) and
925 * an emit matrix.
926 */
927 if(do_post) {
928 cmtotmb += mxmb;
929 if((status = cm_tr_hb_emit_mx_SizeNeeded(cm, errbuf, cm->cp9b, L, NULL, NULL, &emxmb)) != eslOK) return status;
930 cmtotmb += emxmb;
931 }
932
933 /* if we're not sampling an alignment, we'll also need a shadow
934 * matrix for the traceback.
935 */
936 if(! do_sample) {
937 if((status = cm_tr_hb_shadow_mx_SizeNeeded(cm, errbuf, cm->cp9b, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &shmxmb)) != eslOK) return status;
938 cmtotmb += shmxmb;
939 }
940
941 cp9mxmb = SizeNeededCP9Matrix(L, cm->cp9->M, NULL, NULL);
942 cp9mxmb += cp9mxmb; /* add size of bck matrix */
943
944 totmb = cmtotmb + cp9mxmb;
945
946 if (ret_mxmb != NULL) *ret_mxmb = mxmb;
947 if (ret_emxmb != NULL) *ret_emxmb = emxmb;
948 if (ret_shmxmb != NULL) *ret_shmxmb = shmxmb;
949 if (ret_cp9mxmb != NULL) *ret_cp9mxmb = cp9mxmb;
950 if (ret_cmtotmb != NULL) *ret_cmtotmb = cmtotmb;
951 if (ret_totmb != NULL) *ret_totmb = totmb;
952
953 #if eslDEBUGLEVEL >= 1
954 printf("#DEBUG: cm_TrAlignSizeNeededHB()\n");
955 printf("#DEBUG: \t mxmb: %.2f\n", mxmb);
956 printf("#DEBUG: \t emxmb: %.2f\n", emxmb);
957 printf("#DEBUG: \t shmxmb: %.2f\n", shmxmb);
958 printf("#DEBUG: \t cp9mxmb: %.2f\n", cp9mxmb);
959 printf("#DEBUG: \t cmtotmb: %.2f\n", cmtotmb);
960 printf("#DEBUG: \t totmb: %.2f\n", totmb);
961 printf("#DEBUG: \t limit: %.2f\n", size_limit);
962 #endif
963
964 /*printf("cm_TrAlignSizeNeededHB() returning %.2f\n", mxmb);*/
965
966 if(cmtotmb > size_limit) ESL_FAIL(eslERANGE, errbuf, "HMM banded trc DP mxes need %.1f>%.1f Mb limit (HMM mxes need an extra %.1f Mb).\nUse --mxsize, --maxtau or --tau.", cmtotmb, (float) size_limit, cp9mxmb);
967
968 return eslOK;
969 }
970
971 /* Function: cm_TrAlign()
972 * Incept: EPN, Sat Sep 10 12:58:09 2011
973 *
974 * Purpose: Wrapper for the cm_tr_alignTb() routine - solve a full
975 * alignment problem using trCYK, truncated optimal accuracy
976 * or sampling and return the traceback and the score.
977 *
978 * Identical to cm_TrAlignHB() but HMM bands are not used here.
979 *
980 * Input arguments allow this function to be run in 6 'modes':
981 *
982 * mode returns arguments
983 * ---- ---------------- ----------------------------------------
984 * tr ppstrs do_optacc do_sample post_mx ret_ppstr
985 * ---------------- ----------------------------------------
986 * 1. CYK no FALSE FALSE NULL NULL
987 * 2. CYK yes FALSE FALSE !NULL !NULL
988 * 3. Opt acc no TRUE FALSE !NULL NULL
989 * 4. Opt acc yes TRUE FALSE !NULL !NULL
990 * 5. sampled no FALSE TRUE NULL NULL
991 * 6. sampled yes FALSE TRUE !NULL !NULL
992 *
993 * CYK parsetrees are most the likely parsetree, 'Opt acc'
994 * parsetrees are Holmes/Durbin optimally accurate
995 * parsetrees, the parse that maximizes the summed posterior
996 * probability of emitted residues. A sampled parsetree
997 * is a parsetree sampled from an Inside matrix based on
998 * its probability.
999 *
1000 * We can enforce that the parsetree found be in a
1001 * particular marginal alignment mode via a <preset_mode>
1002 * value other than TRMODE_UNKNOWN. This can be useful we're
1003 * called from a search/scan pipeline and a scanning
1004 * truncated DP search algorithm has already determined the
1005 * optimal truncation mode for the alignment, as we'll save
1006 * time by only performing the required DP calculations for
1007 * that mode in the DP functions we call here. In that case,
1008 * <ret_mode> will necessarily be equal to <preset_mode>.
1009 * Alternatively, if <preset_mode> is TRMODE_UNKNOWN then
1010 * we'll determine it here and return a known mode (TRMODE_J
1011 * | TRMODE_L | TRMODE_R | TRMODE_T) in <ret_mode>.
1012 *
1013 * Args: cm - the covariance model
1014 * errbuf - char buffer for reporting errors
1015 * dsq - the digitized sequence, 1..L
1016 * L - length of sequence
1017 * size_limit - max number of Mb for DP matrix, if matrix is bigger return eslERANGE
1018 * preset_mode - the alignment mode to enforce, if TRMODE_UNKNOWN we determine mode here
1019 * pass_idx - pipeline pass index, indicates what truncation penalty to use
1020 * do_optacc - TRUE to not do CYK alignment, determine the Holmes/Durbin optimally
1021 * accurate parsetree in ret_tr, requires post_mx != NULL
1022 * do_sample - TRUE to sample a parsetree from the Inside matrix
1023 * mx - the main dp matrix, only cells within bands in cm->cp9b will be valid.
1024 * shmx - the HMM banded shadow matrix to fill in and traceback, same cells as mx are valid.
1025 * post_mx - dp matrix for posterior calculation, can be NULL only if !do_optacc
1026 * emit_mx - emit matrix to fill
1027 * r - source of randomness, must be non-NULL only if do_sample==TRUE
1028 * ret_ppstr - RETURN: posterior code 1, (pass NULL if not wanted, must be NULL if post_mx == NULL)
1029 * ret_tr - RETURN: parsetree (either optimal or sampled, pass NULL if not wanted)
1030 * ret_mode - RETURN: mode of ret_tr, will be <preset_mode> unless that was TRMODE_UNKNOWN
1031 * ret_avgpp - RETURN: avg PP of emitted residues in parsetree (CYK or optacc) if ret_ppstr == NULL, set as 0.
1032 * ret_sc - RETURN: score of the alignment in bits (Inside score if do_optacc)
1033 *
1034 * Returns: <eslOK> on success.
1035 *
1036 * Throws: <eslEINVAL> on contract violation
1037 * <eslERANGE> if required CM_TR_MX for Inside/Outside/CYK/Posterior exceeds <size_limit>
1038 */
1039 int
1040 cm_TrAlign(CM_t *cm, char *errbuf, ESL_DSQ *dsq, int L, float size_limit, char preset_mode, int pass_idx,
1041 int do_optacc, int do_sample, CM_TR_MX *mx, CM_TR_SHADOW_MX *shmx, CM_TR_MX *post_mx,
1042 CM_TR_EMIT_MX *emit_mx, ESL_RANDOMNESS *r, char **ret_ppstr, Parsetree_t **ret_tr,
1043 char *ret_mode, float *ret_avgpp, float *ret_sc)
1044 {
1045 int status;
1046 Parsetree_t *tr = NULL;
1047 float sc = 0.;
1048 float avgpp = 0.;
1049 float ins_sc = 0.;
1050 int do_post;
1051 char *ppstr = NULL;
1052 int have_ppstr;
1053 char mode = TRMODE_UNKNOWN; /* mode of tr, <ret_mode> set as this */
1054
1055 have_ppstr = (ret_ppstr != NULL) ? TRUE : FALSE;
1056 do_post = (do_optacc || have_ppstr) ? TRUE : FALSE;
1057
1058 /* Contract check */
1059 if(do_optacc && do_sample) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_TrAlign(), do_optacc and do_sample are both TRUE.");
1060 if(do_optacc && post_mx == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_TrAlign(), do_optacc is TRUE, but post_mx == NULL.\n");
1061 if(do_sample && r == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_TrAlign(), do_sample but r is NULL.");
1062
1063 /* if do_post: fill Inside, Outside, Posterior matrices, in that order.
1064 * if do_sample: fill Inside and sample from it.
1065 */
1066 if(do_post || do_sample) {
1067 if((status = cm_TrInsideAlign(cm, errbuf, dsq, L, size_limit, preset_mode, pass_idx, mx, &mode, &ins_sc)) != eslOK) return status;
1068 /* mode will equal preset_mode unless preset_mode is TRMODE_UNKNOWN, in which case it will be mode that gives max inside score */
1069 if(do_sample) {
1070 if((status = cm_TrStochasticParsetree(cm, errbuf, dsq, L, preset_mode, pass_idx, mx, r, &tr, &mode, &sc)) != eslOK) return status;
1071 /* mode may be changed if preset_mode is TRMODE_UNKNOWN, else it will equal preset mode */
1072 }
1073 if(do_post) { /* Inside was called above, now do Outside, then Posterior */
1074 if((status = cm_TrOutsideAlign (cm, errbuf, dsq, L, size_limit, mode, pass_idx, (cm->align_opts & CM_ALIGN_CHECKINOUT), post_mx, mx)) != eslOK) return status;
1075 if((status = cm_TrPosterior (cm, errbuf, L, size_limit, mode, mx, post_mx, post_mx)) != eslOK) return status;
1076 if((status = cm_TrEmitterPosterior(cm, errbuf, L, size_limit, mode, (cm->align_opts & CM_ALIGN_CHECKINOUT), post_mx, emit_mx)) != eslOK) return status;
1077 }
1078 }
1079 else {
1080 mode = preset_mode; /* this allows us to pass <mode> (not <preset_mode>) into cm_tr_alignT() below for all cases */
1081 }
1082
1083 if(!do_sample) { /* if do_sample, we already have a parsetree */
1084 if((status = cm_tr_alignT(cm, errbuf, dsq, L, size_limit, mode, pass_idx, do_optacc, mx, shmx, emit_mx, &tr, &mode, (do_optacc) ? NULL : &sc)) != eslOK) return status;
1085 }
1086
1087 if(have_ppstr || do_optacc) { /* call cm_PostCode to get average PP and optionally a PP string (if have_ppstr) */
1088 if((status = cm_TrPostCode(cm, errbuf, L, emit_mx, tr, (have_ppstr) ? &ppstr : NULL, &avgpp)) != eslOK) return status;
1089 }
1090
1091 if (ret_ppstr != NULL) *ret_ppstr = ppstr; else if(ppstr != NULL) free(ppstr);
1092 if (ret_tr != NULL) *ret_tr = tr; else if(tr != NULL) FreeParsetree(tr);
1093 if (ret_mode != NULL) *ret_mode = mode;
1094 if (ret_avgpp != NULL) *ret_avgpp = avgpp;
1095 if (ret_sc != NULL) *ret_sc = (do_optacc) ? ins_sc : sc;
1096
1097 ESL_DPRINTF1(("#DEBUG: returning from cm_TrAlign() sc : %f\n", sc));
1098 return eslOK;
1099 }
1100
1101 /* Function: cm_TrAlignHB()
1102 * Incept: EPN, Thu Sep 8 08:55:26 2011
1103 * EPN, Fri Oct 26 09:31:43 2007 [FastAlignHB()]
1104 *
1105 * Purpose: Wrapper for the cm_tr_alignT_hb() routine - solve a full
1106 * alignment problem using trCYK, truncated optimal accuracy
1107 * or sampling and return the traceback and the score,
1108 * without dividing & conquering, but by using bands on the
1109 * j and d dimensions of the DP matrix. Bands derived by
1110 * HMM Forward/Backward runs. Optionally return a posterior
1111 * code string.
1112 *
1113 * Identical to cm_TrAlign() but HMM bands are used here.
1114 * See that function's 'Purpose' for more details.
1115 *
1116 * Args: cm - the covariance model
1117 * errbuf - char buffer for reporting errors
1118 * dsq - the digitized sequence, 1..L
1119 * L - length of sequence
1120 * size_limit - max number of Mb for DP matrix, if matrix is bigger return eslERANGE
1121 * preset_mode - the alignment mode to enforce, if TRMODE_UNKNOWN we determine mode here
1122 * pass_idx - pipeline pass index, indicates what truncation penalty to use
1123 * do_optacc - TRUE to not do CYK alignment, determine the Holmes/Durbin optimally
1124 * accurate parsetree in ret_tr, requires post_mx != NULL
1125 * do_sample - TRUE to sample a parsetree from the Inside matrix
1126 * mx - the main dp matrix, only cells within bands in cm->cp9b will be valid.
1127 * shmx - the HMM banded shadow matrix to fill in and traceback, same cells as mx are valid.
1128 * post_mx - dp matrix for posterior calculation, can be NULL only if !do_optacc
1129 * emit_mx - emit matrix to fill
1130 * r - source of randomness, must be non-NULL only if do_sample==TRUE
1131 * ret_ppstr - RETURN: posterior code 1, (pass NULL if not wanted, must be NULL if post_mx == NULL)
1132 * ret_ins_sc - RETURN: if(do_optacc || ret_ppstr != NULL): inside score of sequence in bits
1133 * else: should be NULL (inside will not be run)
1134 * ret_tr - RETURN: traceback (pass NULL if trace isn't wanted)
1135 * ret_mode - RETURN: mode of ret_tr, will be <preset_mode> unless that was TRMODE_UNKNOWN
1136 * ret_sc - RETURN: score of the alignment in bits (Inside score if do_optacc)
1137 *
1138 * Returns: <ret_tr>, <ret_ppstr>, <ret_sc>, see 'Args' section
1139 *
1140 * Returns: <eslOK> on success
1141 *
1142 * Throws: <eslEINVAL> on contract violation
1143 * <eslERANGE> if required CM_TR_HB_MX for Inside/Outside/CYK/Posterior exceeds <size_limit>
1144 */
1145 int
1146 cm_TrAlignHB(CM_t *cm, char *errbuf, ESL_DSQ *dsq, int L, float size_limit, char preset_mode, int pass_idx,
1147 int do_optacc, int do_sample, CM_TR_HB_MX *mx, CM_TR_HB_SHADOW_MX *shmx, CM_TR_HB_MX *post_mx,
1148 CM_TR_HB_EMIT_MX *emit_mx, ESL_RANDOMNESS *r, char **ret_ppstr, Parsetree_t **ret_tr,
1149 char *ret_mode, float *ret_avgpp, float *ret_sc)
1150 {
1151 int status;
1152 Parsetree_t *tr = NULL;
1153 float sc = 0.;
1154 float avgpp = 0.;
1155 float ins_sc = 0.;
1156 int do_post;
1157 char *ppstr = NULL;
1158 int have_ppstr;
1159 char mode = TRMODE_UNKNOWN; /* mode of tr, <ret_mode> set as this */
1160
1161 have_ppstr = (ret_ppstr != NULL) ? TRUE : FALSE;
1162 do_post = (do_optacc || have_ppstr) ? TRUE : FALSE;
1163
1164 /* Contract check */
1165 if(do_optacc && do_sample) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_TrAlignHB(), do_optacc and do_sample are both TRUE.");
1166 if(do_optacc && post_mx == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_TrAlignHB(), do_optacc is TRUE, but post_mx == NULL.\n");
1167 if(do_sample && r == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_TrAlignHB(), do_sample but r is NULL.");
1168
1169 /* if do_post, fill Inside, Outside, Posterior matrices, in that order */
1170 /* if do_sample (and !do_post) fill Inside and sample from it */
1171 if(do_post || do_sample) {
1172 if((status = cm_TrInsideAlignHB (cm, errbuf, dsq, L, size_limit, preset_mode, pass_idx, mx, &mode, &ins_sc)) != eslOK) return status;
1173 /* mode will equal preset_mode unless preset_mode is TRMODE_UNKNOWN, in which case it will be mode that gives max inside score */
1174 if(do_sample) {
1175 if((status = cm_TrStochasticParsetreeHB(cm, errbuf, dsq, L, preset_mode, pass_idx, mx, r, &tr, &mode, &sc)) != eslOK) return status;
1176 /* mode may be changed if preset_mode is TRMODE_UNKNOWN, else it will equal preset mode */
1177 }
1178 if(do_post) { /* Inside was called above, now do Outside, then Posterior, then EmitterPosterior */
1179 if((status = cm_TrOutsideAlignHB (cm, errbuf, dsq, L, size_limit, mode, pass_idx, (cm->align_opts & CM_ALIGN_CHECKINOUT), post_mx, mx)) != eslOK) return status;
1180 if((status = cm_TrPosteriorHB (cm, errbuf, L, size_limit, mode, mx, post_mx, post_mx)) != eslOK) return status;
1181 if((status = cm_TrEmitterPosteriorHB(cm, errbuf, L, size_limit, mode, (cm->align_opts & CM_ALIGN_CHECKINOUT), post_mx, emit_mx)) != eslOK) return status;
1182 }
1183 }
1184 else {
1185 mode = preset_mode; /* this allows us to pass <mode> (not <preset_mode>) into cm_tr_alignT() below for all cases */
1186 }
1187
1188 if(!do_sample) { /* if do_sample, we already have a parsetree */
1189 if((status = cm_tr_alignT_hb(cm, errbuf, dsq, L, size_limit, mode, pass_idx, do_optacc, mx, shmx, emit_mx, &tr, &mode, (do_optacc) ? NULL : &sc)) != eslOK) return status;
1190 }
1191
1192 if(have_ppstr || do_optacc) {
1193 if((status = cm_TrPostCodeHB(cm, errbuf, L, emit_mx, tr, (have_ppstr) ? &ppstr : NULL, &avgpp)) != eslOK) return status;
1194 }
1195
1196 #if eslDEBUGLEVEL >= 2
1197 /* Uncomment to dump emitmap and parsetree */
1198 /* CMEmitMap_t *emap; */
1199 /* emap = CreateEmitMap(cm); */
1200 /* DumpEmitMap(stdout, emap, cm); */
1201 /* FreeEmitMap(emap); */
1202 /* ParsetreeDump(stdout, tr, cm, dsq); */
1203 #endif
1204
1205 if (ret_ppstr != NULL) *ret_ppstr = ppstr; else if(ppstr != NULL) free(ppstr);
1206 if (ret_tr != NULL) *ret_tr = tr; else if(tr != NULL) FreeParsetree(tr);
1207 if (ret_mode != NULL) *ret_mode = mode;
1208 if (ret_avgpp != NULL) *ret_avgpp = avgpp;
1209 if (ret_sc != NULL) *ret_sc = (do_optacc) ? ins_sc : sc;
1210
1211 ESL_DPRINTF1(("#DEBUG: returning from cm_TrAlignHB() sc : %f\n", sc));
1212 return eslOK;
1213 }
1214
1215 /* Function: cm_TrCYKInsideAlign()
1216 * based on cm_CYKInsideAlign()
1217 *
1218 * Date: EPN, Fri Sep 9 15:35:06 2011
1219 *
1220 * Note: Very similar to inside(), but slightly more efficient.
1221 * Identical to cm_TrCYKInsideAlignHB() but HMM bands are not
1222 * used.
1223 *
1224 * Purpose: Perform trCYK alignment on a full sequence 1..L
1225 * rooted at state 0. Very similar to cm_CYKInsideAlign()
1226 * except we're doing truncated alignment and marginal
1227 * alignment modes are possible.
1228 *
1229 * The caller may already know the mode of the optimal
1230 * alignment, passed in as <preset_mode>. This will happen if
1231 * we're being called from within a search pipeline, for
1232 * example. If the caller does not know the optimal mode yet
1233 * (e.g. if we're being called for 'cmalign'), <preset_mode>
1234 * will be TRMODE_UNKNOWN. In this case, we allow all modes.
1235 *
1236 * The mode of the optimal parsetree is returned in <ret_mode>,
1237 * it has score <ret_sc>.
1238 *
1239 * We deal with truncated begins by keeping track of the
1240 * optimal state that we could enter and account for the
1241 * whole target sequence in each mode: {J,L,R,T}b = argmax_v
1242 * {J,L,R,T}alpha_v(1,L) + log t_0(v), and
1243 * {J,L,R,T}alpha[0][L][L] is the score for that. For the
1244 * mode that gives the optimal alignment, <ret_b> is that
1245 * mode's b and <ret_sc> is that modes alpha[0][L][L]
1246 * sc. For example if Jalpha[0][L][L] is the optimal score,
1247 * a local alignment into state Jb in joint marginal mode is
1248 * optimal and <ret_b> = Jb and <ret_sc> = Jbsc.
1249 *
1250 * All alignments must use truncated begins when computing
1251 * truncated alignments. The penalty for the begin is
1252 * different depending on if we're in local mode or not and
1253 * what the value of <pass_idx> is.
1254 *
1255 * Args: cm - the model [0..M-1]
1256 * errbuf - char buffer for reporting errors
1257 * dsq - the digitaized sequence [1..L]
1258 * L - length of target sequence, we align 1..L
1259 * size_limit - max number of Mb for DP matrix, if matrix is bigger return eslERANGE
1260 * preset_mode - the pre-determined alignment mode, or TRMODE_UNKNOWN to allow any mode
1261 * pass_idx - pipeline pass index, indicates what truncation penalty to use
1262 * mx - dp matrix
1263 * shmx - shadow matrix
1264 * ret_b - RETURN: best internal entry state for optimal mode, if local begins are on
1265 * ret_mode - RETURN: mode of optimal CYK parsetree (TRMODE_J | TRMODE_L | TRMODE_R | TRMODE_T)
1266 * ret_sc - RETURN: score of optimal, CYK parsetree in any mode (max of mx->{J,L,R,T}alpha[0][L][L])
1267 *
1268 * Returns: <eslOK> on success.
1269 *
1270 * Throws: <eslERANGE> if required mx or shmx size exceeds <size_limit>
1271 * In this case alignment has been aborted, <ret_*> variables are not valid
1272 */
1273 int
1274 cm_TrCYKInsideAlign(CM_t *cm, char *errbuf, ESL_DSQ *dsq, int L, float size_limit, char preset_mode, int pass_idx,
1275 CM_TR_MX *mx, CM_TR_SHADOW_MX *shmx, int *ret_b, char *ret_mode, float *ret_sc)
1276 {
1277 int status; /* easel status code */
1278 int v,y,z; /* indices for states */
1279 int j,d,i,k; /* indices in sequence dimensions */
1280 float sc; /* a temporary variable holding a score */
1281 int yoffset; /* y=base+offset -- counter in child states that v can transit to */
1282 float *el_scA; /* [0..d..W-1] probability of local end emissions of length d */
1283 int sd; /* StateDelta(cm->sttype[v]) */
1284 int sdl; /* StateLeftDelta(cm->sttype[v] */
1285 int sdr; /* StateRightDelta(cm->sttype[v] */
1286 int j_sdr; /* j - sdr */
1287 int d_sd; /* d - sd */
1288 int d_sdl; /* d - sdl */
1289 int d_sdr; /* d - sdr */
1290 float tsc; /* a transition score */
1291 int64_t c; /* 64-bit int counter */
1292
1293 /* other variables used in truncated version, but not standard version (not in cm_CYKInsideAlign()) */
1294 int b, Jb, Lb, Rb, Tb; /* local entry state rooting overall and {J,L,R,T} optimal parsetrees using */
1295 char mode = TRMODE_UNKNOWN; /* truncation mode for obtaining optimal score <ret_sc> */
1296 int Lyoffset0; /* first yoffset to use for updating L matrix in IR/MR states, 1 if IR, 0 if MR */
1297 int Ryoffset0; /* first yoffset to use for updating R matrix in IL/ML states, 1 if IL, 0 if ML */
1298 int fill_L, fill_R, fill_T; /* must we fill in the L, R, and T matrices? */
1299 int pty_idx; /* index for truncation penalty, determined by pass_idx */
1300 float trpenalty; /* truncation penalty, differs based on pty_idx and if we're local or global */
1301
1302 /* the DP matrix */
1303 float ***Jalpha = mx->Jdp; /* pointer to the Jalpha DP matrix */
1304 float ***Lalpha = mx->Ldp; /* pointer to the Lalpha DP matrix */
1305 float ***Ralpha = mx->Rdp; /* pointer to the Ralpha DP matrix */
1306 float ***Talpha = mx->Tdp; /* pointer to the Talpha DP matrix */
1307
1308 char ***Jyshadow = shmx->Jyshadow; /* pointer to the Jyshadow matrix */
1309 char ***Lyshadow = shmx->Lyshadow; /* pointer to the Lyshadow matrix */
1310 char ***Ryshadow = shmx->Ryshadow; /* pointer to the Ryshadow matrix */
1311 int ***Jkshadow = shmx->Jkshadow; /* pointer to the Jkshadow matrix */
1312 int ***Lkshadow = shmx->Lkshadow; /* pointer to the Lkshadow matrix */
1313 int ***Rkshadow = shmx->Rkshadow; /* pointer to the Rkshadow matrix */
1314 int ***Tkshadow = shmx->Tkshadow; /* pointer to the Tkshadow matrix */
1315 char ***Lkmode = shmx->Lkmode; /* pointer to the Lkmode matrix */
1316 char ***Rkmode = shmx->Rkmode; /* pointer to the Rkmode matrix */
1317
1318 /* Determine which matrices we need to fill in, based on <preset_mode>, if TRMODE_UNKNOWN, fill_L, fill_R, fill_T will all be set as TRUE */
1319 if((status = cm_TrFillFromMode(preset_mode, &fill_L, &fill_R, &fill_T)) != eslOK) ESL_FAIL(status, errbuf, "cm_TrCYKInsideAlign(), bogus mode: %d", preset_mode);
1320
1321 /* Determine the truncation penalty index, from the pass_idx */
1322 if((pty_idx = cm_tr_penalties_IdxForPass(pass_idx)) == -1) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_TrCYKInsideAlign(), unexpected pass idx: %d", pass_idx);
1323
1324 /* Allocations and initializations */
1325 Jb = Lb = Rb = Tb = b = 0; /* will be unchanged if local begins are off, *ret_b will be set as 0 */
1326
1327 /* grow the matrices based on the current sequence and bands */
1328 if((status = cm_tr_mx_GrowTo (cm, mx, errbuf, L, size_limit)) != eslOK) return status;
1329 if((status = cm_tr_shadow_mx_GrowTo(cm, shmx, errbuf, L, size_limit)) != eslOK) return status;
1330
1331 /* precalcuate all possible local end scores, for local end emits of 1..L residues */
1332 ESL_ALLOC(el_scA, sizeof(float) * (L+1));
1333 for(d = 0; d <= L; d++) el_scA[d] = cm->el_selfsc * d;
1334
1335 /* initialize all cells of the matrix to IMPOSSIBLE */
1336 if( mx->Jncells_valid > 0) esl_vec_FSet(mx->Jdp_mem, mx->Jncells_valid, IMPOSSIBLE);
1337 if( mx->Lncells_valid > 0 && fill_L) esl_vec_FSet(mx->Ldp_mem, mx->Lncells_valid, IMPOSSIBLE);
1338 if( mx->Rncells_valid > 0 && fill_R) esl_vec_FSet(mx->Rdp_mem, mx->Rncells_valid, IMPOSSIBLE);
1339 if( mx->Tncells_valid > 0 && fill_T) esl_vec_FSet(mx->Tdp_mem, mx->Tncells_valid, IMPOSSIBLE);
1340 if(shmx->Jy_ncells_valid > 0) for(c = 0; c < shmx->Jy_ncells_valid; c++) shmx->Jyshadow_mem[c] = USED_EL;
1341 if(shmx->Ly_ncells_valid > 0 && fill_L) for(c = 0; c < shmx->Ly_ncells_valid; c++) shmx->Lyshadow_mem[c] = USED_EL;
1342 if(shmx->Ry_ncells_valid > 0 && fill_R) for(c = 0; c < shmx->Ry_ncells_valid; c++) shmx->Ryshadow_mem[c] = USED_EL;
1343 /* for B states, shadow matrix holds k, length of right fragment, this will almost certainly be overwritten */
1344 if(shmx->Jk_ncells_valid > 0) esl_vec_ISet(shmx->Jkshadow_mem, shmx->Jk_ncells_valid, 0);
1345 if(shmx->Lk_ncells_valid > 0 && fill_L) esl_vec_ISet(shmx->Lkshadow_mem, shmx->Lk_ncells_valid, 0);
1346 if(shmx->Rk_ncells_valid > 0 && fill_R) esl_vec_ISet(shmx->Rkshadow_mem, shmx->Rk_ncells_valid, 0);
1347 if(shmx->Tk_ncells_valid > 0 && fill_T) esl_vec_ISet(shmx->Tkshadow_mem, shmx->Tk_ncells_valid, 0);
1348 if(shmx->Lk_ncells_valid > 0 && fill_L) for(c = 0; c < shmx->Lk_ncells_valid; c++) shmx->Lkmode_mem[c] = TRMODE_J;
1349 if(shmx->Rk_ncells_valid > 0 && fill_R) for(c = 0; c < shmx->Rk_ncells_valid; c++) shmx->Rkmode_mem[c] = TRMODE_J;
1350
1351 /* if local ends are on, replace the EL deck IMPOSSIBLEs with EL scores */
1352 if(cm->flags & CMH_LOCAL_END) {
1353 for (j = 0; j <= L; j++) {
1354 for (d = 0; d <= j; d++) {
1355 Jalpha[cm->M][j][d] = el_scA[d];
1356 }
1357 }
1358 if(fill_L) {
1359 for (j = 0; j <= L; j++) {
1360 for (d = 0; d <= j; d++) {
1361 Lalpha[cm->M][j][d] = el_scA[d];
1362 }
1363 }
1364 }
1365 if(fill_R) {
1366 for (j = 0; j <= L; j++) {
1367 for (d = 0; d <= j; d++) {
1368 Ralpha[cm->M][j][d] = el_scA[d];
1369 }
1370 }
1371 }
1372 }
1373
1374 /* Main recursion */
1375 for (v = cm->M-1; v > 0; v--) { /* almost to ROOT_S, we handle that differently */
1376 float const *esc_v = cm->oesc[v]; /* emission scores for state v */
1377 float const *tsc_v = cm->tsc[v]; /* transition scores for state v */
1378 float const *lmesc_v = cm->lmesc[v]; /* marginal left emission scores for state v */
1379 float const *rmesc_v = cm->rmesc[v]; /* marginal right emission scores for state v */
1380 sd = StateDelta(cm->sttype[v]);
1381 sdl = StateLeftDelta(cm->sttype[v]);
1382 sdr = StateRightDelta(cm->sttype[v]);
1383
1384 /* re-initialize the J, L and R deck if we can do a local end from v */
1385 if(NOT_IMPOSSIBLE(cm->endsc[v])) {
1386 for (j = 0; j <= L; j++) {
1387 for (d = sd; d <= j; d++) {
1388 Jalpha[v][j][d] = el_scA[d-sd] + cm->endsc[v];
1389 }
1390 }
1391 if(fill_L) {
1392 for (j = 0; j <= L; j++) {
1393 for (d = sdl; d <= j; d++) {
1394 Lalpha[v][j][d] = el_scA[d-sdl] + cm->endsc[v];
1395 }
1396 }
1397 }
1398 if(fill_R) {
1399 for (j = 0; j <= L; j++) {
1400 for (d = sdr; d <= j; d++) {
1401 Ralpha[v][j][d] = el_scA[d-sdr] + cm->endsc[v];
1402 }
1403 }
1404 }
1405 }
1406 /* otherwise this state's deck has already been initialized to IMPOSSIBLE */
1407
1408 if(cm->sttype[v] == E_st) {
1409 for (j = 0; j <= L; j++) {
1410 Jalpha[v][j][0] = 0.;
1411 if(fill_L) Lalpha[v][j][0] = 0.;
1412 if(fill_R) Ralpha[v][j][0] = 0.;
1413 /* rest of deck remains IMPOSSIBLE */
1414 }
1415 }
1416 else if(cm->sttype[v] == IL_st || cm->sttype[v] == ML_st) {
1417 /* update alpha[v][j][d] cells, for IL states, loop nesting order is:
1418 * for j { for d { for y { } } } because they can self transit, and a
1419 * alpha[v][j][d] cell must be complete (that is we must have looked at all children y)
1420 * before can start calc'ing for alpha[v][j][d+1]
1421 * We do ML states as well as IL states b/c they follow the same rules,
1422 * and we're not worried about efficiency here.
1423 */
1424
1425 /* In TrCYK: we need to treat R differently from and J and L
1426 * here, by doing separate 'for (yoffset...' loops for J and R
1427 * because we have to fully calculate Jalpha[v][j][d]) before we
1428 * can start to calculate Ralpha[v][j][d].
1429 */
1430
1431 if(! StateIsDetached(cm, v)) { /* if we're detached (unreachable), leave all {J,L,R}alpha values as they were initialized, as IMPOSSIBLE */
1432 Ryoffset0 = cm->sttype[v] == IL_st ? 1 : 0; /* don't allow IL self transits in R mode */
1433 for (j = sdr; j <= L; j++) {
1434 j_sdr = j - sdr;
1435 for (d = sd; d <= j; d++) {
1436 d_sd = d - sd;
1437 i = j - d + 1;
1438 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++) {
1439 y = cm->cfirst[v] + yoffset;
1440 if ((sc = Jalpha[y][j_sdr][d_sd] + tsc_v[yoffset]) > Jalpha[v][j][d]) {
1441 Jalpha[v][j][d] = sc;
1442 Jyshadow[v][j][d] = yoffset + TRMODE_J_OFFSET;
1443 }
1444 if (fill_L && (sc = Lalpha[y][j_sdr][d_sd] + tsc_v[yoffset]) > Lalpha[v][j][d]) {
1445 Lalpha[v][j][d] = sc;
1446 Lyshadow[v][j][d] = yoffset + TRMODE_L_OFFSET;
1447 }
1448 }
1449 Jalpha[v][j][d] += esc_v[dsq[i]];
1450 Jalpha[v][j][d] = ESL_MAX(Jalpha[v][j][d], IMPOSSIBLE);
1451 if(fill_L) {
1452 if(d >= 2) {
1453 Lalpha[v][j][d] += esc_v[dsq[i]];
1454 }
1455 else {
1456 Lalpha[v][j][d] = esc_v[dsq[i]];
1457 Lyshadow[v][j][d] = USED_TRUNC_END;
1458 }
1459 Lalpha[v][j][d] = ESL_MAX(Lalpha[v][j][d], IMPOSSIBLE);
1460 }
1461 i--;
1462
1463 /* handle R separately */
1464 if(fill_R) {
1465 /* note we use 'd', not 'd_sd' (which we used in the corresponding loop for J,L above) */
1466 for (yoffset = Ryoffset0; yoffset < cm->cnum[v]; yoffset++) { /* using Ryoffset0 instead of 0 disallows IL self transits in R mode */
1467 y = cm->cfirst[v] + yoffset;
1468 if ((sc = Jalpha[y][j_sdr][d] + tsc_v[yoffset]) > Ralpha[v][j][d]) {
1469 Ralpha[v][j][d] = sc;
1470 Ryshadow[v][j][d]= yoffset + TRMODE_J_OFFSET;
1471 }
1472 if ((sc = Ralpha[y][j_sdr][d] + tsc_v[yoffset]) > Ralpha[v][j][d]) {
1473 Ralpha[v][j][d] = sc;
1474 Ryshadow[v][j][d] = yoffset + TRMODE_R_OFFSET;
1475 }
1476 }
1477 Ralpha[v][j][d] = ESL_MAX(Ralpha[v][j][d], IMPOSSIBLE);
1478 }
1479 }
1480 }
1481 } /* end of if(! StateIsDetached(cm,v )) */
1482 }
1483 else if(cm->sttype[v] == IR_st || cm->sttype[v] == MR_st) {
1484 /* update alpha[v][j][d] cells, for IR states, loop nesting order is:
1485 * for j { for d { for y { } } } because they can self transit, and a
1486 * alpha[v][j][d] cell must be complete (that is we must have looked at all children y)
1487 * before can start calc'ing for alpha[v][j][d+1].
1488 * We do MR states as well as IR states b/c they follow the same rules,
1489 * and we're not worried about efficiency here.
1490 */
1491
1492 /* In TrCYK: we need to treat L differently from and J and R
1493 * here, by doing separate 'for (yoffset...' loops for J and R
1494 * because we have to fully calculate Jalpha[v][j][d]) before we
1495 * can start to calculate Lalpha[v][j][d].
1496 */
1497
1498 if(! StateIsDetached(cm, v)) { /* if we're detached (unreachable), leave all {J,L,R}alpha values as they were initialized, as IMPOSSIBLE */
1499 Lyoffset0 = cm->sttype[v] == IR_st ? 1 : 0; /* don't allow IR self transits in L mode */
1500 for (j = sdr; j <= L; j++) {
1501 j_sdr = j - sdr;
1502 for (d = sd; d <= j; d++) {
1503 d_sd = d - sd;
1504 i = j - d + 1;
1505 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++) {
1506 y = cm->cfirst[v] + yoffset;
1507 if ((sc = Jalpha[y][j_sdr][d_sd] + tsc_v[yoffset]) > Jalpha[v][j][d]) {
1508 Jalpha[v][j][d] = sc;
1509 Jyshadow[v][j][d] = yoffset + TRMODE_J_OFFSET;
1510 }
1511 if (fill_R && (sc = Ralpha[y][j_sdr][d_sd] + tsc_v[yoffset]) > Ralpha[v][j][d]) {
1512 Ralpha[v][j][d] = sc;
1513 Ryshadow[v][j][d] = yoffset + TRMODE_R_OFFSET;
1514 }
1515 }
1516 Jalpha[v][j][d] += esc_v[dsq[j]];
1517 Jalpha[v][j][d] = ESL_MAX(Jalpha[v][j][d], IMPOSSIBLE);
1518 if(fill_R) {
1519 if(d >= 2) {
1520 Ralpha[v][j][d] += esc_v[dsq[j]];
1521 }
1522 else {
1523 Ralpha[v][j][d] = esc_v[dsq[j]];
1524 Ryshadow[v][j][d] = USED_TRUNC_END;
1525 }
1526 Ralpha[v][j][d] = ESL_MAX(Ralpha[v][j][d], IMPOSSIBLE);
1527 }
1528
1529 /* handle L separately */
1530 if(fill_L) {
1531 /* note we use 'j' and 'd', not 'j_sdr' and 'd_sd' (which we used in the corresponding loop for J,R above) */
1532 for (yoffset = Lyoffset0; yoffset < cm->cnum[v]; yoffset++) { /* using Lyoffset0, instead of 0 disallows IR self transits in L mode */
1533 y = cm->cfirst[v] + yoffset;
1534 if ((sc = Jalpha[y][j][d] + tsc_v[yoffset]) > Lalpha[v][j][d]) {
1535 Lalpha[v][j][d] = sc;
1536 Lyshadow[v][j][d] = yoffset + TRMODE_J_OFFSET;
1537 }
1538 if ((sc = Lalpha[y][j][d] + tsc_v[yoffset]) > Lalpha[v][j][d]) {
1539 Lalpha[v][j][d] = sc;
1540 Lyshadow[v][j][d] = yoffset + TRMODE_L_OFFSET;
1541 }
1542 }
1543 Lalpha[v][j][d] = ESL_MAX(Lalpha[v][j][d], IMPOSSIBLE);
1544 }
1545 }
1546 }
1547 } /* end of if(! StateIsDetached(cm, v)) */
1548 }
1549 else if(cm->sttype[v] == MP_st) {
1550 /* MP states cannot self transit, this means that all cells in
1551 * alpha[v] are independent of each other, only depending on
1552 * alpha[y] for previously calc'ed y. We can do the for loops
1553 * in any nesting order, this implementation does what I think
1554 * is most efficient: for y { for j { for d { } } }
1555 */
1556 for (y = cm->cfirst[v]; y < (cm->cfirst[v] + cm->cnum[v]); y++) {
1557 yoffset = y - cm->cfirst[v];
1558 tsc = tsc_v[yoffset];
1559
1560 for (j = sdr; j <= L; j++) {
1561 j_sdr = j - sdr;
1562
1563 for (d = sd; d <= j; d++) { /* sd == 2 for MP state */
1564 d_sd = d-sd;
1565 if((sc = Jalpha[y][j_sdr][d_sd] + tsc) > Jalpha[v][j][d]) {
1566 Jalpha[v][j][d] = sc;
1567 Jyshadow[v][j][d] = yoffset + TRMODE_J_OFFSET;
1568 }
1569 }
1570 if(fill_L) {
1571 /* note we use 'j' and 'd_sdl' not 'j_sdr' for 'd_sd' for L, plus minimum d is sdl (1) */
1572 for (d = sdl; d <= j; d++) { /* sdl == 1 for MP state */
1573 d_sdl = d-sdl;
1574 if((sc = Jalpha[y][j][d_sdl] + tsc) > Lalpha[v][j][d]) {
1575 Lalpha[v][j][d] = sc;
1576 Lyshadow[v][j][d] = yoffset + TRMODE_J_OFFSET;
1577 }
1578 if((sc = Lalpha[y][j][d_sdl] + tsc) > Lalpha[v][j][d]) {
1579 Lalpha[v][j][d] = sc;
1580 Lyshadow[v][j][d] = yoffset + TRMODE_L_OFFSET;
1581 }
1582 }
1583 }
1584 if(fill_R) {
1585 /* note we use 'd_sdr' not 'd_sd' for R, plus minimum d is sdr (1) */
1586 for (d = sdr; d <= j; d++) { /* sdr == 1 for MP state */
1587 d_sdr = d - sdr;
1588 if((sc = Jalpha[y][j_sdr][d_sdr] + tsc) > Ralpha[v][j][d]) {
1589 Ralpha[v][j][d] = sc;
1590 Ryshadow[v][j][d] = yoffset + TRMODE_J_OFFSET;
1591 }
1592 if((sc = Ralpha[y][j_sdr][d_sdr] + tsc) > Ralpha[v][j][d]) {
1593 Ralpha[v][j][d] = sc;
1594 Ryshadow[v][j][d] = yoffset + TRMODE_R_OFFSET;
1595 }
1596 }
1597 }
1598 }
1599 }
1600 /* add in emission score */
1601 for (j = 0; j <= L; j++) {
1602 i = j;
1603 Jalpha[v][j][1] = IMPOSSIBLE;
1604 if(fill_L) {
1605 Lalpha[v][j][1] = lmesc_v[dsq[i]];
1606 Lyshadow[v][j][1] = USED_TRUNC_END;
1607 }
1608 if(fill_R) {
1609 Ralpha[v][j][1] = rmesc_v[dsq[j]];
1610 Ryshadow[v][j][1] = USED_TRUNC_END;
1611 }
1612 i--;
1613 for (d = 2; d <= j; d++) {
1614 Jalpha[v][j][d] += esc_v[dsq[i]*cm->abc->Kp+dsq[j]];
1615 if(fill_L) Lalpha[v][j][d] += lmesc_v[dsq[i]];
1616 if(fill_R) Ralpha[v][j][d] += rmesc_v[dsq[j]];
1617 i--;
1618 }
1619 }
1620 /* ensure all cells are >= IMPOSSIBLE */
1621 for (j = 0; j <= L; j++) {
1622 for (d = 1; d <= j; d++) {
1623 Jalpha[v][j][d] = ESL_MAX(Jalpha[v][j][d], IMPOSSIBLE);
1624 if(fill_L) Lalpha[v][j][d] = ESL_MAX(Lalpha[v][j][d], IMPOSSIBLE);
1625 if(fill_R) Ralpha[v][j][d] = ESL_MAX(Ralpha[v][j][d], IMPOSSIBLE);
1626 }
1627 }
1628 }
1629 else if(cm->sttype[v] != B_st) { /* entered if state v is D or S */
1630 /* D, S states cannot self transit, this means that all cells in
1631 * alpha[v] are independent of each other, only depending on
1632 * alpha[y] for previously calc'ed y. We can do the for loops
1633 * in any nesting order, this implementation does what I think
1634 * is most efficient: for y { for j { for d { } } }
1635 */
1636 for (y = cm->cfirst[v]; y < (cm->cfirst[v] + cm->cnum[v]); y++) {
1637 yoffset = y - cm->cfirst[v];
1638 tsc = tsc_v[yoffset];
1639 for (j = sdr; j <= L; j++) {
1640 j_sdr = j - sdr;
1641
1642 for (d = sd; d <= j; d++) {
1643 d_sd = d-sd;
1644 if((sc = Jalpha[y][j_sdr][d_sd] + tsc) > Jalpha[v][j][d]) {
1645 Jalpha[v][j][d] = sc;
1646 Jyshadow[v][j][d] = yoffset + TRMODE_J_OFFSET;
1647 }
1648 if(fill_L && (sc = Lalpha[y][j_sdr][d_sd] + tsc) > Lalpha[v][j][d]) {
1649 Lalpha[v][j][d] = sc;
1650 Lyshadow[v][j][d] = yoffset + TRMODE_L_OFFSET;
1651 }
1652 if(fill_R && (sc = Ralpha[y][j_sdr][d_sd] + tsc) > Ralpha[v][j][d]) {
1653 Ralpha[v][j][d] = sc;
1654 Ryshadow[v][j][d] = yoffset + TRMODE_R_OFFSET;
1655 }
1656 }
1657 /* an easy to overlook case: if d == 0, ensure L and R values are IMPOSSIBLE */
1658 if(fill_L) Lalpha[v][j][0] = IMPOSSIBLE;
1659 if(fill_R) Ralpha[v][j][0] = IMPOSSIBLE;
1660 /* And another special case for BEGL_S and BEGR_S states,
1661 * reset shadow matrix values for d == 0 (which were
1662 * initialized to USED_EL above), even though the score of
1663 * these cells is impossible we may use them as a
1664 * zero-length left or right half of a BIF_B subtree during
1665 * construction of the parsetree.
1666 */
1667 if(cm->sttype[v] == S_st) {
1668 if(fill_L) Lyshadow[v][j][0] = USED_TRUNC_END;
1669 if(fill_R) Ryshadow[v][j][0] = USED_TRUNC_END;
1670 }
1671 }
1672 }
1673 /* no emission score to add */
1674 }
1675 else { /* B_st */
1676 assert(cm->sttype[v] == B_st);
1677 y = cm->cfirst[v]; /* left subtree */
1678 z = cm->cnum[v]; /* right subtree */
1679
1680 for (j = 0; j <= L; j++) {
1681 for (d = 0; d <= j; d++) {
1682 for (k = 0; k <= d; k++) {
1683 if((sc = Jalpha[y][j-k][d-k] + Jalpha[z][j][k]) > Jalpha[v][j][d]) {
1684 Jalpha[v][j][d] = sc;
1685 Jkshadow[v][j][d] = k;
1686 }
1687 if(fill_L && (sc = Jalpha[y][j-k][d-k] + Lalpha[z][j][k]) > Lalpha[v][j][d]) {
1688 Lalpha[v][j][d] = sc;
1689 Lkshadow[v][j][d] = k;
1690 Lkmode[v][j][d] = TRMODE_J;
1691 }
1692 if(fill_R && (sc = Ralpha[y][j-k][d-k] + Jalpha[z][j][k]) > Ralpha[v][j][d]) {
1693 Ralpha[v][j][d] = sc;
1694 Rkshadow[v][j][d] = k;
1695 Rkmode[v][j][d] = TRMODE_J;
1696 }
1697 }
1698 if(fill_T) {
1699 for(k = 1; k < d; k++) { /* special boundary case for T matrix */
1700 if(fill_T && (sc = Ralpha[y][j-k][d-k] + Lalpha[z][j][k]) > Talpha[v][j][d]) {
1701 Talpha[v][j][d] = sc;
1702 Tkshadow[v][j][d] = k;
1703 }
1704 }
1705 }
1706 /* two additional special cases in trCYK (these are not in standard CYK) */
1707 /* special case 1: k == 0 (full sequence aligns to BEGL_S left child */
1708 if(fill_L) {
1709 if((sc = Jalpha[y][j][d]) > Lalpha[v][j][d]) {
1710 Lalpha[v][j][d] = sc;
1711 Lkshadow[v][j][d] = 0; /* k == 0 for this case, full sequence is on left */
1712 Lkmode[v][j][d] = TRMODE_J;
1713 }
1714 if((sc = Lalpha[y][j][d]) > Lalpha[v][j][d]) {
1715 Lalpha[v][j][d] = sc;
1716 Lkshadow[v][j][d] = 0; /* k == 0 for this case, full sequence is on left */
1717 Lkmode[v][j][d] = TRMODE_L;
1718 }
1719 }
1720 /* special case 2: k == d (full sequence aligns to BEGR_S right child */
1721 if(fill_R) {
1722 if((sc = Jalpha[z][j][d]) > Ralpha[v][j][d]) {
1723 Ralpha[v][j][d] = sc;
1724 Rkshadow[v][j][d] = d; /* k == d in this case, full sequence is on right */
1725 Rkmode[v][j][d] = TRMODE_J;
1726 }
1727 if((sc = Ralpha[z][j][d]) > Ralpha[v][j][d]) {
1728 Ralpha[v][j][d] = sc;
1729 Rkshadow[v][j][d] = d; /* k == d in this case, full sequence is on right */
1730 Rkmode[v][j][d] = TRMODE_R;
1731 }
1732 }
1733 }
1734 }
1735 } /* end of B_st recursion */
1736
1737 /* Now handle from ROOT_S, state 0. So far we haven't touched
1738 * the {J,L,R,T}alpha[0] decks at all since initialization and here
1739 * we'll only update at most 1 cell in each, the one pertaining
1740 * to a full alignment [0][L][L].
1741 *
1742 * In truncated alignment the only way out of ROOT_S in local or
1743 * global mode is via a 'truncated begin' with a score (penalty)
1744 * from cm->trp into any emitting state. The penalty was
1745 * calculated in cm_tr_penalties_Create() and differs depending on
1746 * whether we are in local or global mode and the value of
1747 * 'pty_idx' which was passed in.
1748 */
1749 trpenalty = (cm->flags & CMH_LOCAL_BEGIN) ? cm->trp->l_ptyAA[pty_idx][v] : cm->trp->g_ptyAA[pty_idx][v];
1750 if(NOT_IMPOSSIBLE(trpenalty)) {
1751 /* check if we have a new optimally scoring Joint alignment in J matrix */
1752 sc = Jalpha[v][L][L] + trpenalty;
1753 if (sc > Jalpha[0][L][L]) {
1754 Jalpha[0][L][L] = sc;
1755 Jb = v;
1756 }
1757 /* check if we have a new optimally scoring Left alignment in L matrix */
1758 if(fill_L) {
1759 sc = Lalpha[v][L][L] + trpenalty;
1760 if (sc > Lalpha[0][L][L]) {
1761 Lalpha[0][L][L] = sc;
1762 Lb = v;
1763 }
1764 }
1765 /* check if we have a new optimally scoring Right alignment in R matrix */
1766 if(fill_R) {
1767 sc = Ralpha[v][L][L] + trpenalty;
1768 if (sc > Ralpha[0][L][L]) {
1769 Ralpha[0][L][L] = sc;
1770 Rb = v;
1771 }
1772 }
1773 /* check if we have a new optimally scoring Terminal alignment in T matrix */
1774 if(fill_T && cm->sttype[v] == B_st) {
1775 sc = Talpha[v][L][L] + trpenalty;
1776 if (sc > Talpha[0][L][L]) {
1777 Talpha[0][L][L] = sc;
1778 Tb = v;
1779 }
1780 }
1781 }
1782 } /* end loop for (v = cm->M-1; v > 0; v--) */
1783
1784 /* all valid alignments must use a truncated begin */
1785 Jyshadow[0][L][L] = USED_TRUNC_BEGIN;
1786 if(fill_L) Lyshadow[0][L][L] = USED_TRUNC_BEGIN;
1787 if(fill_R) Ryshadow[0][L][L] = USED_TRUNC_BEGIN;
1788 /* Tyshadow[0] doesn't exist, caller must know how to deal */
1789
1790 /* determine mode of optimal alignment, if it was preset then use that */
1791 if(preset_mode == TRMODE_J) {
1792 sc = Jalpha[0][L][L];
1793 mode = TRMODE_J;
1794 b = Jb;
1795 }
1796 else if(preset_mode == TRMODE_L) {
1797 sc = Lalpha[0][L][L];
1798 mode = TRMODE_L;
1799 b = Lb;
1800 }
1801 else if(preset_mode == TRMODE_R) {
1802 sc = Ralpha[0][L][L];
1803 mode = TRMODE_R;
1804 b = Rb;
1805 }
1806 else if(preset_mode == TRMODE_T) {
1807 sc = Talpha[0][L][L];
1808 mode = TRMODE_T;
1809 b = Tb;
1810 }
1811 else { /* preset_mode was unknown, max score determines mode */
1812 sc = Jalpha[0][L][L];
1813 mode = TRMODE_J;
1814 b = Jb;
1815 if (fill_L && Lalpha[0][L][L] > sc) {
1816 sc = Lalpha[0][L][L];
1817 mode = TRMODE_L;
1818 b = Lb;
1819 }
1820 if (fill_R && Ralpha[0][L][L] > sc) {
1821 sc = Ralpha[0][L][L];
1822 mode = TRMODE_R;
1823 b = Rb;
1824 }
1825 if (fill_T && Talpha[0][L][L] > sc) {
1826 sc = Talpha[0][L][L];
1827 mode = TRMODE_T;
1828 b = Tb;
1829 }
1830 }
1831
1832 #if eslDEBUGLEVEL >= 2
1833 /* Uncomment to dump matrix to file. Careful...this could be very large. */
1834 /* FILE *fp1; fp1 = fopen("tmp.tru_cykmx", "w"); cm_tr_mx_Dump(fp1, mx, preset_mode, TRUE); fclose(fp1); */
1835 /* FILE *fp2; fp2 = fopen("tmp.tru_cykshmx", "w"); cm_tr_shadow_mx_Dump(fp2, cm, shmx, preset_mode, TRUE); fclose(fp2); */
1836 #endif
1837
1838 if(ret_b != NULL) *ret_b = b;
1839 if(ret_mode != NULL) *ret_mode = mode;
1840 if(ret_sc != NULL) *ret_sc = sc;
1841
1842 free(el_scA);
1843
1844 ESL_DPRINTF1(("#DEBUG: cm_TrCYKInsideAlign return sc: %f\n", sc));
1845 return eslOK;
1846
1847 ERROR:
1848 ESL_FAIL(status, errbuf, "Memory allocation error.\n");
1849 }
1850
1851 /* Function: cm_TrCYKInsideAlignHB()
1852 *
1853 * Date: EPN, Wed Sep 7 12:13:43 2011
1854 *
1855 * Purpose: Run the inside phase of a trCYK alignment using bands in
1856 * the j and d dimensions of the DP matrix. Bands were
1857 * obtained from an HMM Forward-Backward parse of the target
1858 * sequence. Uses float log odds scores.
1859 *
1860 * A CM_TR_HB_MX DP matrix must be passed in. Only cells
1861 * valid within the bands given in the CP9Bands_t <cm->cp9b>
1862 * will be valid.
1863 *
1864 * Otherwise, the same as cm_TrCYKInsideAlign(), see that
1865 * functions 'Purpose' for more information, including
1866 * important caveats regarding handling local begins.
1867 *
1868 * Args: cm - the model [0..M-1]
1869 * errbuf - char buffer for reporting errors
1870 * dsq - the digitaized sequence [1..L]
1871 * L - length of target sequence, we align 1..L
1872 * size_limit - max number of Mb for DP matrix, if matrix is bigger return eslERANGE
1873 * preset_mode - the pre-determined alignment mode, TRMODE_UNKNOWN to allow any mode
1874 * pass_idx - pipeline pass index, indicates what truncation penalty to use
1875 * mx - the dp matrix, only cells within bands in cm->cp9b will be valid.
1876 * shmx - the HMM banded shadow matrix to fill in, only cells within bands are valid
1877 * ret_b - RETURN: best internal entry state for optimal mode, if local begins are on
1878 * ret_mode - mode of optimal CYK parsetree (TRMODE_J | TRMODE_L | TRMODE_R | TRMODE_T)
1879 * ret_sc - score of optimal, CYK parsetree in any mode (max of mx->{J,L,R,T}alpha[0][L][L])
1880 *
1881 * Returns: <eslOK> on success.
1882 *
1883 * Throws: <eslERANGE> if required mx or shmx size exceeds <size_limit>
1884 * <eslEINVAL> if the full sequence is not within the bands for state 0
1885 * <eslEAMBIGUOUS> if no valid alignment is possible due to bands (score of sequence is IMPOSSIBLE)
1886 * In any of these three cases, alignment has been aborted, ret variables are not valid.
1887 */
1888 int
1889 cm_TrCYKInsideAlignHB(CM_t *cm, char *errbuf, ESL_DSQ *dsq, int L, float size_limit, char preset_mode, int pass_idx,
1890 CM_TR_HB_MX *mx, CM_TR_HB_SHADOW_MX *shmx, int *ret_b, char *ret_mode, float *ret_sc)
1891 {
1892 int status;
1893 int v,y,z; /* indices for states */
1894 int j,d,i,k; /* indices in sequence dimensions */
1895 float sc; /* temporary score */
1896 int yoffset; /* y=base+offset -- counter in child states that v can transit to */
1897 int *yvalidA; /* [0..MAXCONNECT-1] TRUE if v->yoffset is legal transition (within bands) */
1898 float *el_scA; /* [0..d..W-1] probability of local end emissions of length d */
1899 int sd; /* StateDelta(cm->sttype[v]) */
1900 int sdl; /* StateLeftDelta(cm->sttype[v]) */
1901 int sdr; /* StateRightDelta(cm->sttype[v]) */
1902 int j_sdr; /* j - sdr */
1903 int64_t c; /* 64-bit int counter */
1904
1905 /* indices used for handling band-offset issues, and in the depths of the DP recursion */
1906 int jp_v, jp_y, jp_z; /* offset j index for states v, y, z */
1907 int jp_y_sdr; /* jp_y - sdr */
1908 int jn, jx; /* current minimum/maximum j allowed */
1909 int jpn, jpx; /* minimum/maximum jp_v */
1910 int dp_v, dp_y, dp_z; /* d index for state v/y/z in alpha */
1911 int dn, dx; /* current minimum/maximum d allowed */
1912 int dp_y_sd; /* dp_y - sd */
1913 int dp_y_sdr; /* dp_y - sdr */
1914 int dp_y_sdl; /* dp_y - sdl */
1915 int dpn, dpx; /* minimum/maximum dp_v */
1916 int kp_z; /* k (in the d dim) index for state z in alpha w/mem eff bands */
1917 int kn, kx; /* current minimum/maximum k value */
1918 int Lp; /* L index also changes depending on state */
1919 float tsc; /* a transition score */
1920 int yvalid_idx; /* for keeping track of which children are valid */
1921 int yvalid_ct; /* for keeping track of which children are valid */
1922 int jp_0; /* L offset in ROOT_S's (v==0) j band */
1923 int Lp_0; /* L offset in ROOT_S's (v==0) d band */
1924
1925 /* variables related to truncated alignment (not in cm_CYKInsideAlignHB() */
1926 int b, Jb, Lb, Rb, Tb; /* local entry state rooting overall and {J,L,R,T} optimal parsetrees using */
1927 char mode = TRMODE_UNKNOWN; /* truncation mode for obtaining optimal score <ret_sc> */
1928 int fill_L, fill_R, fill_T; /* must we fill in the L, R, and T matrices? */
1929 int do_J_v, do_J_y, do_J_z; /* must we fill J matrix deck for state v, y, z? */
1930 int do_L_v, do_L_y, do_L_z; /* must we fill L matrix deck for state v, y, z? */
1931 int do_R_v, do_R_y, do_R_z; /* must we fill R matrix deck for state v, y, z? */
1932 int do_T_v; /* must we fill T matrix deck for state v? */
1933 int pty_idx; /* index for truncation penalty, determined by pass_idx */
1934 float trpenalty; /* truncation penalty, differs based on pty_idx and if we're local or global */
1935
1936 /* variables used for memory efficient bands */
1937 /* ptrs to cp9b info, for convenience */
1938 CP9Bands_t *cp9b = cm->cp9b;
1939 int *jmin = cp9b->jmin;
1940 int *jmax = cp9b->jmax;
1941 int **hdmin = cp9b->hdmin;
1942 int **hdmax = cp9b->hdmax;
1943
1944 /* the DP matrix */
1945 float ***Jalpha = mx->Jdp; /* pointer to the Jalpha DP matrix */
1946 float ***Lalpha = mx->Ldp; /* pointer to the Lalpha DP matrix */
1947 float ***Ralpha = mx->Rdp; /* pointer to the Ralpha DP matrix */
1948 float ***Talpha = mx->Tdp; /* pointer to the Talpha DP matrix */
1949
1950 char ***Jyshadow = shmx->Jyshadow; /* pointer to the Jyshadow matrix */
1951 char ***Lyshadow = shmx->Lyshadow; /* pointer to the Lyshadow matrix */
1952 char ***Ryshadow = shmx->Ryshadow; /* pointer to the Ryshadow matrix */
1953 int ***Jkshadow = shmx->Jkshadow; /* pointer to the Jkshadow matrix */
1954 int ***Lkshadow = shmx->Lkshadow; /* pointer to the Lkshadow matrix */
1955 int ***Rkshadow = shmx->Rkshadow; /* pointer to the Rkshadow matrix */
1956 int ***Tkshadow = shmx->Tkshadow; /* pointer to the Tkshadow matrix */
1957 char ***Lkmode = shmx->Lkmode; /* pointer to the Lkmode matrix */
1958 char ***Rkmode = shmx->Rkmode; /* pointer to the Rkmode matrix */
1959
1960 /* Determine which matrices we need to fill in, based on <preset_mode>, if TRMODE_UNKNOWN, fill_L, fill_R, fill_T will all be set as TRUE */
1961 if((status = cm_TrFillFromMode(preset_mode, &fill_L, &fill_R, &fill_T)) != eslOK) ESL_FAIL(status, errbuf, "cm_TrCYKInsideAlignHB(), bogus mode: %d", preset_mode);
1962
1963 /* Determine the truncation penalty index, from the pass_idx */
1964 if((pty_idx = cm_tr_penalties_IdxForPass(pass_idx)) == -1) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_TrCYKInsideAlignHB(), unexpected pass idx: %d", pass_idx);
1965
1966 /* Allocations and initializations */
1967 Jb = Lb = Rb = Tb = b = 0; /* will be unchanged if local begins are off, *ret_b will be set as 0 */
1968
1969 /* ensure a full alignment to ROOT_S (v==0) is possible, remember In CYK <preset_mode> may be known or unknown */
1970 if (preset_mode == TRMODE_J && (! cp9b->Jvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrCYKInsideAlignHB(): preset_mode is J mode, but cp9b->Jvalid[v] is FALSE");
1971 if (preset_mode == TRMODE_L && (! cp9b->Lvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrCYKInsideAlignHB(): preset_mode is L mode, but cp9b->Lvalid[v] is FALSE");
1972 if (preset_mode == TRMODE_R && (! cp9b->Rvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrCYKInsideAlignHB(): preset_mode is R mode, but cp9b->Rvalid[v] is FALSE");
1973 if (preset_mode == TRMODE_T && (! cp9b->Tvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrCYKInsideAlignHB(): preset_mode is T mode, but cp9b->Tvalid[v] is FALSE");
1974 if (preset_mode == TRMODE_UNKNOWN && (! (cp9b->Jvalid[0] || cp9b->Lvalid[0] || cp9b->Rvalid[0] || cp9b->Tvalid[0]))) {
1975 ESL_FAIL(eslEINVAL, errbuf, "cm_TrCYKInsideAlignHB(): no marginal mode is allowed for state 0");
1976 }
1977 if (cp9b->jmin[0] > L || cp9b->jmax[0] < L) ESL_FAIL(eslEINVAL, errbuf, "cm_TrCYKInsideAlignHB(): L (%d) is outside ROOT_S's j band (%d..%d)\n", L, cp9b->jmin[0], cp9b->jmax[0]);
1978 jp_0 = L - jmin[0];
1979 if (cp9b->hdmin[0][jp_0] > L || cp9b->hdmax[0][jp_0] < L) ESL_FAIL(eslEINVAL, errbuf, "cm_TrCYKInsideAlignHB(): L (%d) is outside ROOT_S's d band (%d..%d)\n", L, cp9b->hdmin[0][jp_0], cp9b->hdmax[0][jp_0]);
1980 Lp_0 = L - hdmin[0][jp_0];
1981
1982 /* grow the matrices based on the current sequence and bands */
1983 if((status = cm_tr_hb_mx_GrowTo (cm, mx, errbuf, cp9b, L, size_limit)) != eslOK) return status;
1984 if((status = cm_tr_hb_shadow_mx_GrowTo(cm, shmx, errbuf, cp9b, L, size_limit)) != eslOK) return status;
1985
1986 /* precalcuate all possible local end scores, for local end emits of 1..L residues */
1987 ESL_ALLOC(el_scA, sizeof(float) * (L+1));
1988 for(d = 0; d <= L; d++) el_scA[d] = cm->el_selfsc * d;
1989
1990 /* yvalidA[0..cnum[v]] will hold TRUE for states y for which a transition is legal
1991 * (some transitions are impossible due to the bands) */
1992 ESL_ALLOC(yvalidA, sizeof(int) * MAXCONNECT);
1993 esl_vec_ISet(yvalidA, MAXCONNECT, FALSE);
1994
1995 /* initialize all cells of the matrix to IMPOSSIBLE, all cells of shadow matrix to USED_EL or USED_TRUNC_END */
1996 if( mx->Jncells_valid > 0) esl_vec_FSet(mx->Jdp_mem, mx->Jncells_valid, IMPOSSIBLE);
1997 if( mx->Lncells_valid > 0 && fill_L) esl_vec_FSet(mx->Ldp_mem, mx->Lncells_valid, IMPOSSIBLE);
1998 if( mx->Rncells_valid > 0 && fill_R) esl_vec_FSet(mx->Rdp_mem, mx->Rncells_valid, IMPOSSIBLE);
1999 if( mx->Tncells_valid > 0 && fill_T) esl_vec_FSet(mx->Tdp_mem, mx->Tncells_valid, IMPOSSIBLE);
2000 if(shmx->Jy_ncells_valid > 0) for(c = 0; c < shmx->Jy_ncells_valid; c++) shmx->Jyshadow_mem[c] = USED_EL;
2001 if(shmx->Ly_ncells_valid > 0 && fill_L) for(c = 0; c < shmx->Ly_ncells_valid; c++) shmx->Lyshadow_mem[c] = USED_EL;
2002 if(shmx->Ry_ncells_valid > 0 && fill_R) for(c = 0; c < shmx->Ry_ncells_valid; c++) shmx->Ryshadow_mem[c] = USED_EL;
2003 /* for B states, shadow matrix holds k, length of right fragment, this will be overwritten */
2004 if(shmx->Jk_ncells_valid > 0) esl_vec_ISet(shmx->Jkshadow_mem, shmx->Jk_ncells_valid, 0);
2005 if(shmx->Lk_ncells_valid > 0 && fill_L) esl_vec_ISet(shmx->Lkshadow_mem, shmx->Lk_ncells_valid, 0);
2006 if(shmx->Rk_ncells_valid > 0 && fill_R) esl_vec_ISet(shmx->Rkshadow_mem, shmx->Rk_ncells_valid, 0);
2007 if(shmx->Tk_ncells_valid > 0 && fill_T) esl_vec_ISet(shmx->Tkshadow_mem, shmx->Tk_ncells_valid, 0);
2008 if(shmx->Lk_ncells_valid > 0 && fill_L) for(c = 0; c < shmx->Lk_ncells_valid; c++) shmx->Lkmode_mem[c] = TRMODE_J;
2009 if(shmx->Rk_ncells_valid > 0 && fill_R) for(c = 0; c < shmx->Rk_ncells_valid; c++) shmx->Rkmode_mem[c] = TRMODE_J;
2010
2011 /* if local ends are on, replace the EL deck IMPOSSIBLEs with EL scores,
2012 * Note: we could optimize by skipping this step and using el_scA[d] to
2013 * initialize ELs for each state in the first step of the main recursion
2014 * below. We fill in the EL deck here for completeness and so that
2015 * a check of this alpha matrix with a CYKOutside matrix will pass.
2016 */
2017 if(cm->flags & CMH_LOCAL_END) {
2018 if(cp9b->Jvalid[cm->M]) {
2019 for (j = 0; j <= L; j++) {
2020 for (d = 0; d <= j; d++) Jalpha[cm->M][j][d] = el_scA[d];
2021 }
2022 }
2023 if(fill_L && cp9b->Lvalid[cm->M]) {
2024 for (j = 0; j <= L; j++) {
2025 for (d = 0; d <= j; d++) Lalpha[cm->M][j][d] = el_scA[d];
2026 }
2027 }
2028 if(fill_R && cp9b->Rvalid[cm->M]) {
2029 for (j = 0; j <= L; j++) {
2030 for (d = 0; d <= j; d++) Ralpha[cm->M][j][d] = el_scA[d];
2031 }
2032 }
2033 }
2034
2035 /* Main recursion */
2036 for (v = cm->M-1; v > 0; v--) { /* almost to ROOT_S, we handle that differently */
2037 float const *esc_v = cm->oesc[v]; /* emission scores for state v */
2038 float const *tsc_v = cm->tsc[v]; /* transition scores for state v */
2039 float const *lmesc_v = cm->lmesc[v]; /* marginal left emission scores for state v */
2040 float const *rmesc_v = cm->rmesc[v]; /* marginal right emission scores for state v */
2041 sd = StateDelta(cm->sttype[v]);
2042 sdl = StateLeftDelta(cm->sttype[v]);
2043 sdr = StateRightDelta(cm->sttype[v]);
2044 jn = jmin[v];
2045 jx = jmax[v];
2046 do_J_v = cp9b->Jvalid[v] ? TRUE : FALSE;
2047 do_L_v = cp9b->Lvalid[v] && fill_L ? TRUE : FALSE;
2048 do_R_v = cp9b->Rvalid[v] && fill_R ? TRUE : FALSE;
2049 do_T_v = cp9b->Tvalid[v] && fill_T ? TRUE : FALSE;
2050 /* re-initialize the J, L and R decks if we can do a local end from v */
2051 if(NOT_IMPOSSIBLE(cm->endsc[v])) {
2052 if(do_J_v && cp9b->Jvalid[cm->M]) {
2053 for (j = jmin[v]; j <= jmax[v]; j++) {
2054 jp_v = j - jmin[v];
2055 if(hdmin[v][jp_v] >= sd) {
2056 d = hdmin[v][jp_v];
2057 dp_v = 0;
2058 }
2059 else {
2060 d = sd;
2061 dp_v = sd - hdmin[v][jp_v];
2062 }
2063 for (; d <= hdmax[v][jp_v]; dp_v++, d++) {
2064 Jalpha[v][jp_v][dp_v] = Jalpha[cm->M][j][d-sd] + cm->endsc[v];
2065 /* If we optimize by skipping the filling of the
2066 * EL deck the above line would become:
2067 * 'Jalpha[v][jp_v][dp_v] = el_scA[d-sd] + cm->endsc[v];'
2068 */
2069 }
2070 }
2071 }
2072 if(do_L_v && cp9b->Lvalid[cm->M]) {
2073 for (j = jmin[v]; j <= jmax[v]; j++) {
2074 jp_v = j - jmin[v];
2075 if(hdmin[v][jp_v] >= sdl) {
2076 d = hdmin[v][jp_v];
2077 dp_v = 0;
2078 }
2079 else {
2080 d = sdl;
2081 dp_v = sdl - hdmin[v][jp_v];
2082 }
2083 for (; d <= hdmax[v][jp_v]; dp_v++, d++) {
2084 Lalpha[v][jp_v][dp_v] = Lalpha[cm->M][j][d-sdl] + cm->endsc[v];
2085 }
2086 }
2087 }
2088 if(do_R_v && cp9b->Rvalid[cm->M]) {
2089 for (j = jmin[v]; j <= jmax[v]; j++) {
2090 jp_v = j - jmin[v];
2091 if(hdmin[v][jp_v] >= sdr) {
2092 d = hdmin[v][jp_v];
2093 dp_v = 0;
2094 }
2095 else {
2096 d = sdr;
2097 dp_v = sdr - hdmin[v][jp_v];
2098 }
2099 for (; d <= hdmax[v][jp_v]; dp_v++, d++) {
2100 Ralpha[v][jp_v][dp_v] = Ralpha[cm->M][j][d-sdr] + cm->endsc[v];
2101 }
2102 }
2103 }
2104 }
2105 /* otherwise this state's deck has already been initialized to IMPOSSIBLE */
2106
2107 if(cm->sttype[v] == E_st) {
2108 for (j = jmin[v]; j <= jmax[v]; j++) {
2109 jp_v = j-jmin[v];
2110 ESL_DASSERT1((hdmin[v][jp_v] == 0));
2111 ESL_DASSERT1((hdmax[v][jp_v] == 0));
2112 if(do_J_v) Jalpha[v][jp_v][0] = 0.; /* for End states, d must be 0 */
2113 if(do_L_v) Lalpha[v][jp_v][0] = 0.; /* for End states, d must be 0 */
2114 if(do_R_v) Ralpha[v][jp_v][0] = 0.; /* for End states, d must be 0 */
2115 }
2116 }
2117 else if(cm->sttype[v] == IL_st || cm->sttype[v] == ML_st) {
2118 /* update {J,L,R}alpha[v][jp_v][dp_v] cells, for IL states, loop
2119 * nesting order is: for j { for d { for y { } } } because they
2120 * can self transit, and a {J,L,R}alpha[v][j][d] cell must be
2121 * complete (that is we must have looked at all children y)
2122 * before can start calc'ing for {J,L,R}alpha[v][j][d+1]
2123 * We could be slightly more efficient if we separated out
2124 * MR from IR b/c self-transits in MRs are impossible, but
2125 * we don't do that here. */
2126 if(! StateIsDetached(cm, v)) { /* if we're detached (unreachable), leave all {J,L,R}alpha values as they were initialized, as IMPOSSIBLE */
2127 for (j = jmin[v]; j <= jmax[v]; j++) {
2128 jp_v = j - jmin[v];
2129 yvalid_ct = 0;
2130 j_sdr = j - sdr;
2131
2132 /* determine which children y we can legally transit to for v, j */
2133 for (y = cm->cfirst[v], yoffset = 0; y < (cm->cfirst[v] + cm->cnum[v]); y++, yoffset++)
2134 if((j_sdr) >= jmin[y] && ((j_sdr) <= jmax[y])) yvalidA[yvalid_ct++] = yoffset; /* is j-sdr valid for state y? */
2135
2136 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) { /* for each valid d for v, j */
2137 i = j - d + 1;
2138 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha */
2139
2140 /* We need to treat R differently from and J and L here, by
2141 * doing separate 'for (yoffset...' loops for J and R
2142 * because we have to fully calculate Jalpha[v][jp_v][dp_v])
2143 * before we can start to calculate Ralpha[v][jp_v][dp_v].
2144 */
2145 /* Handle J and L first */
2146 if(do_J_v || do_L_v) {
2147 for (yvalid_idx = 0; yvalid_idx < yvalid_ct; yvalid_idx++) { /* for each valid child y, for v, j */
2148 yoffset = yvalidA[yvalid_idx];
2149 y = cm->cfirst[v] + yoffset;
2150 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
2151 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
2152 if(do_J_y || do_L_y) {
2153 jp_y_sdr = j - jmin[y] - sdr;
2154
2155 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 */
2156 dp_y_sd = d - sd - hdmin[y][jp_y_sdr];
2157 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
2158 ESL_DASSERT1((dp_y_sd >= 0 && dp_y_sd <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
2159 if((do_J_v && do_J_y) &&
2160 ((sc = Jalpha[y][jp_y_sdr][dp_y_sd] + tsc_v[yoffset]) > Jalpha[v][jp_v][dp_v])) {
2161 Jalpha[v][jp_v][dp_v] = sc;
2162 Jyshadow[v][jp_v][dp_v] = yoffset + TRMODE_J_OFFSET;
2163 }
2164 if((do_L_v && do_L_y) &&
2165 ((sc = Lalpha[y][jp_y_sdr][dp_y_sd] + tsc_v[yoffset]) > Lalpha[v][jp_v][dp_v])) {
2166 Lalpha[v][jp_v][dp_v] = sc;
2167 Lyshadow[v][jp_v][dp_v] = yoffset + TRMODE_L_OFFSET;
2168 }
2169 }
2170 }
2171 }
2172 if(do_J_v) {
2173 Jalpha[v][jp_v][dp_v] += esc_v[dsq[i]];
2174 Jalpha[v][jp_v][dp_v] = ESL_MAX(Jalpha[v][jp_v][dp_v], IMPOSSIBLE);
2175 }
2176 if(do_L_v) {
2177 if(d >= 2) {
2178 Lalpha[v][jp_v][dp_v] += esc_v[dsq[i]];
2179 }
2180 else {
2181 Lalpha[v][jp_v][dp_v] = esc_v[dsq[i]];
2182 Lyshadow[v][jp_v][dp_v] = USED_TRUNC_END;
2183 }
2184 Lalpha[v][jp_v][dp_v] = ESL_MAX(Lalpha[v][jp_v][dp_v], IMPOSSIBLE);
2185 }
2186 i--;
2187 }
2188
2189 if(do_R_v) {
2190 /* Handle R separately */
2191 for (yvalid_idx = 0; yvalid_idx < yvalid_ct; yvalid_idx++) { /* for each valid child y, for v, j */
2192 yoffset = yvalidA[yvalid_idx];
2193 y = cm->cfirst[v] + yoffset;
2194 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
2195 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
2196 if((do_J_y || do_R_y) && (y != v)) { /* (y != v) part is to disallow IL self transits in R mode */
2197 jp_y_sdr = j - jmin[y] - sdr;
2198
2199 /* 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) */
2200 if((d) >= hdmin[y][jp_y_sdr] && (d) <= hdmax[y][jp_y_sdr]) { /* make sure d is valid for this v, j and y */
2201 dp_y = d - hdmin[y][jp_y_sdr];
2202 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
2203 ESL_DASSERT1((dp_y >= 0 && dp_y <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
2204
2205 if(do_J_y &&
2206 ((sc = Jalpha[y][jp_y_sdr][dp_y] + tsc_v[yoffset]) > Ralpha[v][jp_v][dp_v])) {
2207 Ralpha[v][jp_v][dp_v] = sc;
2208 Ryshadow[v][jp_v][dp_v] = yoffset + TRMODE_J_OFFSET;
2209 }
2210 if(do_R_y &&
2211 ((sc = Ralpha[y][jp_y_sdr][dp_y] + tsc_v[yoffset]) > Ralpha[v][jp_v][dp_v])) {
2212 Ralpha[v][jp_v][dp_v] = sc;
2213 Ryshadow[v][jp_v][dp_v] = yoffset + TRMODE_R_OFFSET;
2214 }
2215 }
2216 }
2217 } /* end of for (yvalid_idx = 0... loop */
2218 }
2219 }
2220 }
2221 } /* end of if(! StateIsDetached(cm, v)) */
2222 }
2223 else if(cm->sttype[v] == IR_st || cm->sttype[v] == MR_st) {
2224 /* update {J,L,R}alpha[v][jp_v][dp_v] cells, for IR states, loop
2225 * nesting order is: for j { for d { for y { } } } because they
2226 * can self transit, and a {J,L,R}alpha[v][j][d] cell must be
2227 * complete (that is we must have looked at all children y)
2228 * before can start calc'ing for {J,L,R}alpha[v][j][d+1].
2229 * We could be slightly more efficient if we separated out
2230 * MR from IR b/c self-transits in MRs are impossible, but
2231 * we don't do that here. */
2232
2233 if(! StateIsDetached(cm, v)) { /* if we're detached (unreachable), leave all {J,L,R}alpha values as they were initialized, as IMPOSSIBLE */
2234 /* The first MR_st/IR_st 'for (j...' loop is for J and R matrices which use the same set of j values */
2235 if(do_J_v || do_R_v) {
2236 for (j = jmin[v]; j <= jmax[v]; j++) {
2237 jp_v = j - jmin[v];
2238 yvalid_ct = 0;
2239 j_sdr = j - sdr;
2240
2241 /* determine which children y we can legally transit to for v, j */
2242 for (y = cm->cfirst[v], yoffset = 0; y < (cm->cfirst[v] + cm->cnum[v]); y++, yoffset++)
2243 if((j_sdr) >= jmin[y] && ((j_sdr) <= jmax[y])) yvalidA[yvalid_ct++] = yoffset; /* is j-sdr is valid for state y? */
2244
2245 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) { /* for each valid d for v, j */
2246 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha */
2247
2248 /* We need to treat L differently from and J and R here, by
2249 * doing separate 'for (yoffset...' loops for J because we
2250 * have to fully calculate Jalpha[v][jp_v][dp_v]) before we
2251 * can start to calculate Lalpha[v][jp_v][dp_v].
2252 */
2253 /* Handle J and R first */
2254 for (yvalid_idx = 0; yvalid_idx < yvalid_ct; yvalid_idx++) { /* for each valid child y, for v, j */
2255 yoffset = yvalidA[yvalid_idx];
2256 y = cm->cfirst[v] + yoffset;
2257 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
2258 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
2259 if(do_J_y || do_R_y) {
2260 jp_y_sdr = j - jmin[y] - sdr;
2261
2262 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 */
2263 dp_y_sd = d - sd - hdmin[y][jp_y_sdr];
2264 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
2265 ESL_DASSERT1((dp_y_sd >= 0 && dp_y_sd <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
2266
2267 if((do_J_v && do_J_y) &&
2268 ((sc = Jalpha[y][jp_y_sdr][dp_y_sd] + tsc_v[yoffset]) > Jalpha[v][jp_v][dp_v])) {
2269 Jalpha[v][jp_v][dp_v] = sc;
2270 Jyshadow[v][jp_v][dp_v] = yoffset + TRMODE_J_OFFSET;
2271 }
2272 if((do_R_v && do_R_y) &&
2273 ((sc = Ralpha[y][jp_y_sdr][dp_y_sd] + tsc_v[yoffset]) > Ralpha[v][jp_v][dp_v])) {
2274 Ralpha[v][jp_v][dp_v] = sc;
2275 Ryshadow[v][jp_v][dp_v] = yoffset + TRMODE_R_OFFSET;
2276 }
2277 }
2278 }
2279 }
2280 if(do_J_v) {
2281 Jalpha[v][jp_v][dp_v] += esc_v[dsq[j]];
2282 Jalpha[v][jp_v][dp_v] = ESL_MAX(Jalpha[v][jp_v][dp_v], IMPOSSIBLE);
2283 }
2284 if(do_R_v) {
2285 if(d >= 2) {
2286 Ralpha[v][jp_v][dp_v] += esc_v[dsq[j]];
2287 }
2288 else {
2289 Ralpha[v][jp_v][dp_v] = esc_v[dsq[j]];
2290 Ryshadow[v][jp_v][dp_v] = USED_TRUNC_END;
2291 }
2292 Ralpha[v][jp_v][dp_v] = ESL_MAX(Ralpha[v][jp_v][dp_v], IMPOSSIBLE);
2293 }
2294 }
2295 }
2296 }
2297 /* Handle L separately */
2298 if(do_L_v) {
2299 /* The second MR_st/IR_st 'for (j...' loop is for the L matrix which use a different set of j values */
2300 for (j = jmin[v]; j <= jmax[v]; j++) {
2301 jp_v = j - jmin[v];
2302 yvalid_ct = 0;
2303
2304 /* determine which children y we can legally transit to for v, j */
2305 /* we use 'j' and not 'j_sdr' here for the L matrix, differently from J and R matrices above */
2306 for (y = cm->cfirst[v], yoffset = 0; y < (cm->cfirst[v] + cm->cnum[v]); y++, yoffset++)
2307 if(j >= jmin[y] && j <= jmax[y]) yvalidA[yvalid_ct++] = yoffset; /* is j is valid for state y? */
2308
2309 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) { /* for each valid d for v, j */
2310 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha */
2311
2312 for (yvalid_idx = 0; yvalid_idx < yvalid_ct; yvalid_idx++) { /* for each valid child y, for v, j */
2313 /* 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)*/
2314 yoffset = yvalidA[yvalid_idx];
2315 y = cm->cfirst[v] + yoffset;
2316 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
2317 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
2318 if((do_J_y || do_L_y) && (y != v)) { /* (y != v) part is to disallow IR self transits in L mode */
2319
2320 /* 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) */
2321 jp_y = j - jmin[y];
2322
2323 /* 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) */
2324 if((d) >= hdmin[y][jp_y] && (d) <= hdmax[y][jp_y]) { /* make sure d is valid for this v, j and y */
2325 dp_y = d - hdmin[y][jp_y];
2326 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
2327 ESL_DASSERT1((dp_y >= 0 && dp_y <= (hdmax[y][jp_y] - hdmin[y][jp_y])));
2328
2329 if(do_J_y &&
2330 (sc = Jalpha[y][jp_y][dp_y] + tsc_v[yoffset]) > Lalpha[v][jp_v][dp_v]) {
2331 Lalpha[v][jp_v][dp_v] = sc;
2332 Lyshadow[v][jp_v][dp_v] = yoffset + TRMODE_J_OFFSET;
2333 }
2334 if(do_L_y &&
2335 (sc = Lalpha[y][jp_y][dp_y] + tsc_v[yoffset]) > Lalpha[v][jp_v][dp_v]) {
2336 Lalpha[v][jp_v][dp_v] = sc;
2337 Lyshadow[v][jp_v][dp_v] = yoffset + TRMODE_L_OFFSET;
2338 }
2339 }
2340 }
2341 } /* end of for (yvalid_idx = 0... loop */
2342 }
2343 }
2344 }
2345 } /* end of if(! StateIsDetached(cm, v) */
2346 }
2347 else if(cm->sttype[v] == MP_st) {
2348 /* MP states cannot self transit, this means that all cells in
2349 * alpha[v] are independent of each other, only depending on
2350 * alpha[y] for previously calc'ed y. We can do the for loops
2351 * in any nesting order, this implementation does what I think
2352 * is most efficient: for y { for j { for d { } } }
2353 */
2354 for (y = cm->cfirst[v]; y < (cm->cfirst[v] + cm->cnum[v]); y++) {
2355 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
2356 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
2357 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
2358 yoffset = y - cm->cfirst[v];
2359 tsc = tsc_v[yoffset];
2360
2361 /* The first MP_st 'for (jp_v...' loop is for J and R matrices which use the same set of j values */
2362 /* j must satisfy:
2363 * j >= jmin[v]
2364 * j >= jmin[y]+sdr (follows from (j-sdr >= jmin[y]))
2365 * j <= jmax[v]
2366 * j <= jmax[y]+sdr (follows from (j-sdr <= jmax[y]))
2367 * this reduces to two ESL_MAX calls
2368 */
2369 jn = ESL_MAX(jmin[v], jmin[y]+sdr);
2370 jx = ESL_MIN(jmax[v], jmax[y]+sdr);
2371 jpn = jn - jmin[v];
2372 jpx = jx - jmin[v];
2373 jp_y_sdr = jn - jmin[y] - sdr;
2374 /* for Lalpha, we use 'jp_y=j-min[y]' instead of 'jp_y_sdr=j-jmin[y]-sdr' */
2375
2376 if((do_J_v && do_J_y) || (do_R_v && (do_J_y || do_R_y))) {
2377 for (jp_v = jpn; jp_v <= jpx; jp_v++, jp_y_sdr++, jp_y++) {
2378 ESL_DASSERT1((jp_v >= 0 && jp_v <= (jmax[v]-jmin[v])));
2379 ESL_DASSERT1((jp_y_sdr >= 0 && jp_y_sdr <= (jmax[y]-jmin[y])));
2380
2381 if(do_J_v && do_J_y) {
2382 /* J matrix: */
2383 /* d must satisfy:
2384 * d >= hdmin[v][jp_v]
2385 * d >= hdmin[y][jp_y_sdr]+sd (follows from (d-sd >= hdmin[y][jp_y_sdr]))
2386 * d <= hdmax[v][jp_v]
2387 * d <= hdmax[y][jp_y_sdr]+sd (follows from (d-sd <= hdmax[y][jp_y_sdr]))
2388 * this reduces to two ESL_MAX calls
2389 */
2390 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y_sdr] + sd);
2391 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y_sdr] + sd);
2392 dpn = dn - hdmin[v][jp_v];
2393 dpx = dx - hdmin[v][jp_v];
2394 dp_y_sd = dn - hdmin[y][jp_y_sdr] - sd;
2395
2396 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y_sd++) {
2397 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
2398 ESL_DASSERT1((dp_y_sd >= 0 && dp_y_sd <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
2399
2400 if((sc = Jalpha[y][jp_y_sdr][dp_y_sd] + tsc) > Jalpha[v][jp_v][dp_v]) {
2401 Jalpha[v][jp_v][dp_v] = sc;
2402 Jyshadow[v][jp_v][dp_v] = yoffset + TRMODE_J_OFFSET;
2403 }
2404 }
2405 }
2406
2407 if(do_R_v && (do_R_y || do_J_y)) {
2408 /* R matrix: */
2409 /* d must satisfy:
2410 * d >= hdmin[v][jp_v]
2411 * d >= hdmin[y][jp_y_sd]+sd (follows from (d-sd >= hdmin[y][jp_y_sd]))
2412 * d <= hdmax[v][jp_v]
2413 * d <= hdmax[y][jp_y_sd]+sd (follows from (d-sd <= hdmax[y][jp_y_sd]))
2414 * this reduces to two ESL_MAX calls
2415 */
2416 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y_sdr] + sdr);
2417 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y_sdr] + sdr);
2418 dpn = dn - hdmin[v][jp_v];
2419 dpx = dx - hdmin[v][jp_v];
2420 dp_y_sdr = dn - hdmin[y][jp_y_sdr] - sdr;
2421 /* for {L,R}alpha, we use 'dp_y_sdr' instead of 'dy_y_sd' */
2422
2423 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y_sdr++) {
2424 /* we use 'dp_y_sdr' here, not 'dp_y_sd' (which we used in the corresponding loop for J above) */
2425 ESL_DASSERT1((dp_y_sdr >= 0 && dp_y_sdr <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
2426 if(do_J_y &&
2427 ((sc = Jalpha[y][jp_y_sdr][dp_y_sdr] + tsc) > Ralpha[v][jp_v][dp_v])) {
2428 Ralpha[v][jp_v][dp_v] = sc;
2429 Ryshadow[v][jp_v][dp_v] = yoffset + TRMODE_J_OFFSET;
2430 }
2431 if(do_R_y &&
2432 ((sc = Ralpha[y][jp_y_sdr][dp_y_sdr] + tsc) > Ralpha[v][jp_v][dp_v])) {
2433 Ralpha[v][jp_v][dp_v] = sc;
2434 Ryshadow[v][jp_v][dp_v] = yoffset + TRMODE_R_OFFSET;
2435 }
2436 }
2437 }
2438 }
2439 }
2440
2441 if(do_L_v && (do_L_y || do_J_y)) {
2442 /* The second MP_st 'for (jp_v...' loop is for L matrix, which uses a different set of j values from J and R */
2443 /* j must satisfy:
2444 * j >= jmin[v]
2445 * j >= jmin[y] (follows from (j >= jmin[y]))
2446 * j <= jmax[v]
2447 * j <= jmax[y] (follows from (j <= jmax[y]))
2448 * this reduces to two ESL_MAX calls
2449 */
2450 jn = ESL_MAX(jmin[v], jmin[y]);
2451 jx = ESL_MIN(jmax[v], jmax[y]);
2452 jpn = jn - jmin[v];
2453 jpx = jx - jmin[v];
2454 jp_y = jn - jmin[y];
2455 /* for Lalpha, we use 'jp_y=j-min[y]' instead of 'jp_y_sdr=j-jmin[y]-sdr' */
2456
2457 for (jp_v = jpn; jp_v <= jpx; jp_v++, jp_y++) {
2458 ESL_DASSERT1((jp_v >= 0 && jp_v <= (jmax[v]-jmin[v])));
2459
2460 /* d must satisfy:
2461 * d >= hdmin[v][jp_v]
2462 * d >= hdmin[y][jp_y]+sdl (follows from (d-sdl >= hdmin[y][jp_y]))
2463 * d <= hdmax[v][jp_v]
2464 * d <= hdmax[y][jp_y]+sdl (follows from (d-sdl <= hdmax[y][jp_y]))
2465 * this reduces to two ESL_MAX calls
2466 */
2467 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y] + sdl);
2468 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y] + sdl);
2469 dpn = dn - hdmin[v][jp_v];
2470 dpx = dx - hdmin[v][jp_v];
2471 dp_y_sdl = dn - hdmin[y][jp_y] - sdl;
2472 /* for Lalpha, we use 'dp_y_sdl' instead of 'dy_y_sd' */
2473
2474 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y_sdl++) {
2475 /* we use 'dp_y_sdl' here, not 'dp_y_sd' (which we used in the corresponding loop for J above) */
2476 ESL_DASSERT1((dp_y_sdl >= 0 && dp_y_sdl <= (hdmax[y][jp_y] - hdmin[y][jp_y])));
2477 if(do_J_y &&
2478 ((sc = Jalpha[y][jp_y][dp_y_sdl] + tsc) > Lalpha[v][jp_v][dp_v])) {
2479 Lalpha[v][jp_v][dp_v] = sc;
2480 Lyshadow[v][jp_v][dp_v] = yoffset + TRMODE_J_OFFSET;
2481 }
2482 if(do_L_y &&
2483 ((sc = Lalpha[y][jp_y][dp_y_sdl] + tsc) > Lalpha[v][jp_v][dp_v])) {
2484 Lalpha[v][jp_v][dp_v] = sc;
2485 Lyshadow[v][jp_v][dp_v] = yoffset + TRMODE_L_OFFSET;
2486 }
2487 }
2488 }
2489 }
2490 }
2491 /* add in emission score */
2492 for (j = jmin[v]; j <= jmax[v]; j++) {
2493 jp_v = j - jmin[v];
2494 i = j - hdmin[v][jp_v] + 1;
2495 for (d = hdmin[v][jp_v], dp_v = 0; d <= hdmax[v][jp_v]; d++, dp_v++)
2496 {
2497 if(d >= 2) {
2498 if(do_J_v) Jalpha[v][jp_v][dp_v] += esc_v[dsq[i]*cm->abc->Kp+dsq[j]];
2499 if(do_L_v) Lalpha[v][jp_v][dp_v] += lmesc_v[dsq[i]];
2500 if(do_R_v) Ralpha[v][jp_v][dp_v] += rmesc_v[dsq[j]];
2501 }
2502 else {
2503 if(do_J_v) { Jalpha[v][jp_v][dp_v] = IMPOSSIBLE; }
2504 if(do_L_v) { Lalpha[v][jp_v][dp_v] = lmesc_v[dsq[i]]; Lyshadow[v][jp_v][dp_v] = USED_TRUNC_END; }
2505 if(do_R_v) { Ralpha[v][jp_v][dp_v] = rmesc_v[dsq[j]]; Ryshadow[v][jp_v][dp_v] = USED_TRUNC_END; }
2506 }
2507 i--;
2508 }
2509 }
2510 /* ensure all cells are >= IMPOSSIBLE */
2511 for (j = jmin[v]; j <= jmax[v]; j++) {
2512 jp_v = j - jmin[v];
2513 for (dp_v = 0; dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v]); dp_v++) {
2514 if(do_J_v) Jalpha[v][jp_v][dp_v] = ESL_MAX(Jalpha[v][jp_v][dp_v], IMPOSSIBLE);
2515 if(do_L_v) Lalpha[v][jp_v][dp_v] = ESL_MAX(Lalpha[v][jp_v][dp_v], IMPOSSIBLE);
2516 if(do_R_v) Ralpha[v][jp_v][dp_v] = ESL_MAX(Ralpha[v][jp_v][dp_v], IMPOSSIBLE);
2517 }
2518 }
2519 }
2520 else if(cm->sttype[v] != B_st) { /* entered if state v is D or S */
2521 /* D, S states cannot self transit, this means that all cells in
2522 * alpha[v] are independent of each other, only depending on
2523 * alpha[y] for previously calc'ed y. We can do the for loops
2524 * in any nesting order, this implementation does what I think
2525 * is most efficient: for y { for j { for d { } } }
2526 */
2527 for (y = cm->cfirst[v]; y < (cm->cfirst[v] + cm->cnum[v]); y++) {
2528 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
2529 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
2530 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
2531 yoffset = y - cm->cfirst[v];
2532 tsc = tsc_v[yoffset];
2533
2534 if((do_J_v && do_J_y) || (do_L_v && do_L_y) || (do_R_v && do_R_y)) {
2535 /* j must satisfy:
2536 * j >= jmin[v]
2537 * j >= jmin[y]+sdr (follows from (j-sdr >= jmin[y]))
2538 * j <= jmax[v]
2539 * j <= jmax[y]+sdr (follows from (j-sdr <= jmax[y]))
2540 * this reduces to two ESL_MAX calls
2541 */
2542 jn = ESL_MAX(jmin[v], jmin[y]+sdr);
2543 jx = ESL_MIN(jmax[v], jmax[y]+sdr);
2544 jpn = jn - jmin[v];
2545 jpx = jx - jmin[v];
2546 jp_y_sdr = jn - jmin[y] - sdr;
2547
2548 for (jp_v = jpn; jp_v <= jpx; jp_v++, jp_y_sdr++) {
2549 ESL_DASSERT1((jp_v >= 0 && jp_v <= (jmax[v]-jmin[v])));
2550 ESL_DASSERT1((jp_y_sdr >= 0 && jp_y_sdr <= (jmax[y]-jmin[y])));
2551
2552 /* d must satisfy:
2553 * d >= hdmin[v][jp_v]
2554 * d >= hdmin[y][jp_y_sdr]+sd (follows from (d-sd >= hdmin[y][jp_y_sdr]))
2555 * d <= hdmax[v][jp_v]
2556 * d <= hdmax[y][jp_y_sdr]+sd (follows from (d-sd <= hdmax[y][jp_y_sdr]))
2557 * this reduces to two ESL_MAX calls
2558 */
2559 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y_sdr] + sd);
2560 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y_sdr] + sd);
2561 dpn = dn - hdmin[v][jp_v];
2562 dpx = dx - hdmin[v][jp_v];
2563 dp_y_sd = dn - hdmin[y][jp_y_sdr] - sd;
2564
2565 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y_sd++) {
2566 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
2567 ESL_DASSERT1((dp_y_sd >= 0 && dp_y_sd <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
2568
2569 if((do_J_v && do_J_y) &&
2570 ((sc = Jalpha[y][jp_y_sdr][dp_y_sd] + tsc) > Jalpha[v][jp_v][dp_v])) {
2571 Jalpha[v][jp_v][dp_v] = sc;
2572 Jyshadow[v][jp_v][dp_v] = yoffset + TRMODE_J_OFFSET;
2573 }
2574 if((do_L_v && do_L_y) &&
2575 ((sc = Lalpha[y][jp_y_sdr][dp_y_sd] + tsc) > Lalpha[v][jp_v][dp_v])) {
2576 Lalpha[v][jp_v][dp_v] = sc;
2577 Lyshadow[v][jp_v][dp_v] = yoffset + TRMODE_L_OFFSET;
2578 }
2579 if((do_R_v && do_R_y) &&
2580 ((sc = Ralpha[y][jp_y_sdr][dp_y_sd] + tsc) > Ralpha[v][jp_v][dp_v])) {
2581 Ralpha[v][jp_v][dp_v] = sc;
2582 Ryshadow[v][jp_v][dp_v] = yoffset + TRMODE_R_OFFSET;
2583 }
2584 /* an easy to overlook case: if d == 0, ensure L and R values are IMPOSSIBLE */
2585 if(dp_v == dpn && dn == 0) { /* d is 0 */
2586 if(do_L_v) Lalpha[v][jp_v][dp_v] = IMPOSSIBLE;
2587 if(do_R_v) Ralpha[v][jp_v][dp_v] = IMPOSSIBLE;
2588 /* And another special case for BEGL_S and BEGR_S states,
2589 * reset shadow matrix values for d == 0 (which were
2590 * initialized to USED_EL above), even though the score of
2591 * these cells is impossible we may use them as a
2592 * zero-length left or right half of a BIF_B subtree during
2593 * construction of the parsetree.
2594 */
2595 if(cm->sttype[v] == S_st) {
2596 if(do_L_v) Lyshadow[v][jp_v][dp_v] = USED_TRUNC_END;
2597 if(do_R_v) Ryshadow[v][jp_v][dp_v] = USED_TRUNC_END;
2598 }
2599 }
2600 }
2601 }
2602 }
2603 }
2604 /* no emission score to add */
2605 }
2606 else { /* B_st */
2607 y = cm->cfirst[v]; /* left subtree */
2608 z = cm->cnum[v]; /* right subtree */
2609
2610 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
2611 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
2612 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
2613
2614 do_J_z = cp9b->Jvalid[z] ? TRUE : FALSE;
2615 do_L_z = cp9b->Lvalid[z] && fill_L ? TRUE : FALSE;
2616 do_R_z = cp9b->Rvalid[z] && fill_R ? TRUE : FALSE;
2617
2618 /* Any valid j must be within both state v and state z's j band
2619 * I think jmin[v] <= jmin[z] is guaranteed by the way bands are
2620 * constructed, but we'll check anyway.
2621 */
2622 jn = (jmin[v] > jmin[z]) ? jmin[v] : jmin[z];
2623 jx = (jmax[v] < jmax[z]) ? jmax[v] : jmax[z];
2624 /* the main j loop */
2625 for (j = jn; j <= jx; j++) {
2626 jp_v = j - jmin[v];
2627 jp_y = j - jmin[y];
2628 jp_z = j - jmin[z];
2629 kn = ((j-jmax[y]) > (hdmin[z][jp_z])) ? (j-jmax[y]) : hdmin[z][jp_z];
2630 kn = ESL_MAX(kn, 0); /* kn must be non-negative, added with fix to bug i36 */
2631 /* kn satisfies inequalities (1) and (3) (listed below)*/
2632 kx = ( jp_y < (hdmax[z][jp_z])) ? jp_y : hdmax[z][jp_z];
2633 /* kn satisfies inequalities (2) and (4) (listed below)*/
2634 i = j - hdmin[v][jp_v] + 1;
2635 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++, i--) {
2636 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha w/mem eff bands */
2637
2638 /* Find the first k value that implies a valid cell in the {J,L,R} matrix y and z decks.
2639 * This k must satisfy the following 6 inequalities (some may be redundant):
2640 * (1) k >= j-jmax[y];
2641 * (2) k <= j-jmin[y];
2642 * 1 and 2 guarantee (j-k) is within state y's j band
2643 *
2644 * (3) k >= hdmin[z][j-jmin[z]];
2645 * (4) k <= hdmax[z][j-jmin[z]];
2646 * 3 and 4 guarantee k is within z's j=(j), d band
2647 *
2648 * (5) k >= d-hdmax[y][j-jmin[y]-k];
2649 * (6) k <= d-hdmin[y][j-jmin[y]-k];
2650 * 5 and 6 guarantee (d-k) is within state y's j=(j-k) d band
2651 *
2652 * kn and kx were set above (outside (for (dp_v...) loop) that
2653 * satisfy 1-4 (b/c 1-4 are d-independent and k-independent)
2654 * RHS of inequalities 5 and 6 are dependent on k, so we check
2655 * for these within the next for loop.
2656 *
2657 * To update a cell in the T matrix with a sum of an R matrix value for y
2658 * and a L matrix value for z, there are 2 additional inequalities to satisfy:
2659 * (7) k != 0
2660 * (8) k != d
2661 * We ensure 7 and 8 in the loop below.
2662 */
2663 for(k = kn; k <= kx; k++) {
2664 if((k >= d - hdmax[y][jp_y-k]) && k <= d - hdmin[y][jp_y-k]) {
2665 /* for current k, all 6 inequalities have been satisified
2666 * so we know the cells corresponding to the platonic
2667 * matrix cells alpha[v][j][d], alpha[y][j-k][d-k], and
2668 * alpha[z][j][k] are all within the bands. These
2669 * cells correspond to alpha[v][jp_v][dp_v],
2670 * alpha[y][jp_y-k][d-hdmin[jp_y-k]-k],
2671 * and alpha[z][jp_z][k-hdmin[jp_z]];
2672 */
2673 kp_z = k-hdmin[z][jp_z];
2674 dp_y = d-hdmin[y][jp_y-k];
2675 if((do_J_v && do_J_y && do_J_z) &&
2676 ((sc = Jalpha[y][jp_y-k][dp_y - k] + Jalpha[z][jp_z][kp_z]) > Jalpha[v][jp_v][dp_v])) {
2677 Jalpha[v][jp_v][dp_v] = sc;
2678 Jkshadow[v][jp_v][dp_v] = k;
2679 }
2680 if((do_L_v && do_J_y && do_L_z) &&
2681 ((sc = Jalpha[y][jp_y-k][dp_y - k] + Lalpha[z][jp_z][kp_z]) > Lalpha[v][jp_v][dp_v])) {
2682 Lalpha[v][jp_v][dp_v] = sc;
2683 Lkshadow[v][jp_v][dp_v] = k;
2684 Lkmode[v][jp_v][dp_v] = TRMODE_J;
2685 }
2686 if((do_R_v && do_R_y && do_J_z) &&
2687 ((sc = Ralpha[y][jp_y-k][dp_y - k] + Jalpha[z][jp_z][kp_z]) > Ralpha[v][jp_v][dp_v])) {
2688 Ralpha[v][jp_v][dp_v] = sc;
2689 Rkshadow[v][jp_v][dp_v] = k;
2690 Rkmode[v][jp_v][dp_v] = TRMODE_J;
2691 }
2692 if(k != 0 && k != d) {
2693 if((do_T_v && do_R_y && do_L_z) &&
2694 ((sc = Ralpha[y][jp_y-k][dp_y - k] + Lalpha[z][jp_z][kp_z]) > Talpha[v][jp_v][dp_v])) {
2695 Talpha[v][jp_v][dp_v] = sc;
2696 Tkshadow[v][jp_v][dp_v] = k;
2697 }
2698 }
2699 }
2700 }
2701 }
2702 }
2703
2704 /* two additional special cases in trCYK (these are not in standard CYK).
2705 * we do these in their own for(j.. { for(d.. { } } loops b/c one
2706 * is independent of z, the other of y, unlike the above loop which is dependent
2707 * on both.
2708 */
2709 if(do_L_v && (do_J_y || do_L_y)) {
2710 jn = (jmin[v] > jmin[y]) ? jmin[v] : jmin[y];
2711 jx = (jmax[v] < jmax[y]) ? jmax[v] : jmax[y];
2712 for (j = jn; j <= jx; j++) {
2713 jp_v = j - jmin[v];
2714 jp_y = j - jmin[y];
2715 ESL_DASSERT1((j >= jmin[v] && j <= jmax[v]));
2716 ESL_DASSERT1((j >= jmin[y] && j <= jmax[y]));
2717 dn = (hdmin[v][jp_v] > hdmin[y][jp_y]) ? hdmin[v][jp_v] : hdmin[y][jp_y];
2718 dx = (hdmax[v][jp_v] < hdmax[y][jp_y]) ? hdmax[v][jp_v] : hdmax[y][jp_y];
2719 for(d = dn; d <= dx; d++) {
2720 dp_v = d - hdmin[v][jp_v];
2721 dp_y = d - hdmin[y][jp_y];
2722 ESL_DASSERT1((d >= hdmin[v][jp_v] && d <= hdmax[v][jp_v]));
2723 ESL_DASSERT1((d >= hdmin[y][jp_y] && d <= hdmax[y][jp_y]));
2724 if(do_J_y &&
2725 ((sc = Jalpha[y][jp_y][dp_y]) > Lalpha[v][jp_v][dp_v])) {
2726 Lalpha[v][jp_v][dp_v] = sc;
2727 Lkshadow[v][jp_v][dp_v] = 0; /* k == 0 for this case, full sequence is on left */
2728 Lkmode[v][jp_v][dp_v] = TRMODE_J;
2729 /* consider making a different mode here, to let the traceback know that right child emits 0 residues,
2730 * this should then effect the alignment display, no? it is a different case from the
2731 * >0 residues from right child TRMODE_J case for Lalpha checked for in (for k) loop above.
2732 */
2733 }
2734 if(do_L_y &&
2735 ((sc = Lalpha[y][jp_y][dp_y]) > Lalpha[v][jp_v][dp_v])) {
2736 Lalpha[v][jp_v][dp_v] = sc;
2737 Lkshadow[v][jp_v][dp_v] = 0; /* k == 0 for this case, full sequence is on left */
2738 Lkmode[v][jp_v][dp_v] = TRMODE_L;
2739 /* consider making a different mode here, to let the traceback know that right child emits 0 residues,
2740 * this should then effect the alignment display, no? it is a different case from the
2741 * >0 residues from right child TRMODE_L case for Lalpha checked for in (for k) loop above.
2742 */
2743 }
2744 }
2745 }
2746 }
2747 if(do_R_v && (do_J_z || do_R_z)) {
2748 jn = (jmin[v] > jmin[z]) ? jmin[v] : jmin[z];
2749 jx = (jmax[v] < jmax[z]) ? jmax[v] : jmax[z];
2750 for (j = jn; j <= jx; j++) {
2751 jp_v = j - jmin[v];
2752 jp_z = j - jmin[z];
2753 ESL_DASSERT1((j >= jmin[v] && j <= jmax[v]));
2754 ESL_DASSERT1((j >= jmin[z] && j <= jmax[z]));
2755 dn = (hdmin[v][jp_v] > hdmin[z][jp_z]) ? hdmin[v][jp_v] : hdmin[z][jp_z];
2756 dx = (hdmax[v][jp_v] < hdmax[z][jp_z]) ? hdmax[v][jp_v] : hdmax[z][jp_z];
2757 for(d = dn; d <= dx; d++) {
2758 dp_v = d - hdmin[v][jp_v];
2759 dp_z = d - hdmin[z][jp_z];
2760 ESL_DASSERT1((d >= hdmin[v][jp_v] && d <= hdmax[v][jp_v]));
2761 ESL_DASSERT1((d >= hdmin[z][jp_z] && d <= hdmax[z][jp_z]));
2762 if(do_J_z &&
2763 ((sc = Jalpha[z][jp_z][dp_z]) > Ralpha[v][jp_v][dp_v])) {
2764 Ralpha[v][jp_v][dp_v] = sc;
2765 Rkshadow[v][jp_v][dp_v] = d; /* k == d in this case, full sequence is on right */
2766 Rkmode[v][jp_v][dp_v] = TRMODE_J;
2767 /* consider making a different mode here, to let the traceback know that left child emits 0 residues,
2768 * this should then effect the alignment display, no? it is a different case from the
2769 * >0 residues from left child TRMODE_J case for Ralpha checked for in (for k) loop above.
2770 */
2771 }
2772 if(do_R_z &&
2773 ((sc = Ralpha[z][jp_z][dp_z]) > Ralpha[v][jp_v][dp_v])) {
2774 Ralpha[v][jp_v][dp_v] = sc;
2775 Rkshadow[v][jp_v][dp_v] = d; /* k == d in this case, full sequence is on right */
2776 Rkmode[v][jp_v][dp_v] = TRMODE_R;
2777 /* consider making a different mode here, to let the traceback know that left child emits 0 residues,
2778 * this should then effect the alignment display, no? it is a different case from the
2779 * >0 residues from left child TRMODE_R case for Ralpha checked for in (for k) loop above.
2780 */
2781 }
2782 }
2783 }
2784 }
2785 } /* end of B_st recursion */
2786
2787 /* Now handle from ROOT_S, state 0. So far we haven't touched
2788 * the {J,L,R,T}alpha[0] decks at all since initialization and here
2789 * we'll only update at most 1 cell in each, the one pertaining
2790 * to a full alignment [0][L][L].
2791 *
2792 * In truncated alignment the only way out of ROOT_S in local or
2793 * global mode is via a 'truncated begin' with a score (penalty)
2794 * from cm->trp into any emitting state. The penalty was
2795 * calculated in cm_tr_penalties_Create() and differs depending on
2796 * whether we are in local or global mode and the value of
2797 * 'pty_idx' which was passed in.
2798 */
2799 if(L >= jmin[v] && L <= jmax[v]) {
2800 jp_v = L - jmin[v];
2801 Lp = L - hdmin[v][jp_v];
2802 if(L >= hdmin[v][jp_v] && L <= hdmax[v][jp_v]) {
2803
2804 /* If we get here alpha[v][jp_v][Lp] and alpha[0][jp_0][Lp_0]
2805 * are valid cells in the banded alpha matrix, corresponding to
2806 * alpha[v][L][L] and alpha[0][L][L] in the platonic matrix.
2807 * (Le've already made sure alpha[0][jp_0][Lp_0] was valid
2808 * at the beginning of the function.)
2809 */
2810 trpenalty = (cm->flags & CMH_LOCAL_BEGIN) ? cm->trp->l_ptyAA[pty_idx][v] : cm->trp->g_ptyAA[pty_idx][v];
2811 if(NOT_IMPOSSIBLE(trpenalty)) {
2812 /* check if we have a new optimally scoring Joint alignment in J matrix */
2813 if(do_J_v && cp9b->Jvalid[0]) {
2814 sc = Jalpha[v][jp_v][Lp] + trpenalty;
2815 if (sc > Jalpha[0][jp_0][Lp_0]) {
2816 Jalpha[0][jp_0][Lp_0] = sc;
2817 Jb = v;
2818 }
2819 }
2820 /* check if we have a new optimally scoring Left alignment in L matrix */
2821 if(do_L_v && cp9b->Lvalid[0]) {
2822 sc = Lalpha[v][jp_v][Lp] + trpenalty;
2823 if (sc > Lalpha[0][jp_0][Lp_0]) {
2824 Lalpha[0][jp_0][Lp_0] = sc;
2825 Lb = v;
2826 }
2827 }
2828 /* check if we have a new optimally scoring Right alignment in R matrix */
2829 if(do_R_v && cp9b->Rvalid[0]) {
2830 sc = Ralpha[v][jp_v][Lp] + trpenalty;
2831 if (sc > Ralpha[0][jp_0][Lp_0]) {
2832 Ralpha[0][jp_0][Lp_0] = sc;
2833 Rb = v;
2834 }
2835 }
2836 /* check if we have a new optimally scoring Terminal alignment in T matrix */
2837 if(do_T_v && cp9b->Tvalid[0]) {
2838 sc = Talpha[v][jp_v][Lp] + trpenalty;
2839 if (sc > Talpha[0][jp_0][Lp_0]) {
2840 Talpha[0][jp_0][Lp_0] = sc;
2841 Tb = v;
2842 }
2843 }
2844 }
2845 }
2846 }
2847 } /* end loop for (v = cm->M-1; v > 0; v--) */
2848
2849 /* all valid alignments must use a truncated begin */
2850 if ( cp9b->Jvalid[0]) Jyshadow[0][jp_0][Lp_0] = USED_TRUNC_BEGIN;
2851 if (fill_L && cp9b->Lvalid[0]) Lyshadow[0][jp_0][Lp_0] = USED_TRUNC_BEGIN;
2852 if (fill_R && cp9b->Rvalid[0]) Ryshadow[0][jp_0][Lp_0] = USED_TRUNC_BEGIN;
2853 /* Tyshadow[0] doesn't exist, caller must know how to deal */
2854
2855 /* determine mode of optimal alignment, if it was preset then use that */
2856 if(preset_mode == TRMODE_J) {
2857 sc = Jalpha[0][jp_0][Lp_0];
2858 mode = TRMODE_J;
2859 b = Jb;
2860 }
2861 else if(preset_mode == TRMODE_L) {
2862 sc = Lalpha[0][jp_0][Lp_0];
2863 mode = TRMODE_L;
2864 b = Lb;
2865 }
2866 else if(preset_mode == TRMODE_R) {
2867 sc = Ralpha[0][jp_0][Lp_0];
2868 mode = TRMODE_R;
2869 b = Rb;
2870 }
2871 else if(preset_mode == TRMODE_T) {
2872 sc = Talpha[0][jp_0][Lp_0];
2873 mode = TRMODE_T;
2874 b = Tb;
2875 }
2876 else { /* preset_mode was unknown, max score determines mode */
2877 sc = IMPOSSIBLE;
2878 mode = TRMODE_UNKNOWN;
2879 if (cp9b->Jvalid[0] && Jalpha[0][jp_0][Lp_0] > sc) {
2880 sc = Jalpha[0][jp_0][Lp_0];
2881 mode = TRMODE_J;
2882 b = Jb;
2883 }
2884 if (fill_L && cp9b->Lvalid[0] && Lalpha[0][jp_0][Lp_0] > sc) {
2885 sc = Lalpha[0][jp_0][Lp_0];
2886 mode = TRMODE_L;
2887 b = Lb;
2888 }
2889 if (fill_R && cp9b->Rvalid[0] && Ralpha[0][jp_0][Lp_0] > sc) {
2890 sc = Ralpha[0][jp_0][Lp_0];
2891 mode = TRMODE_R;
2892 b = Rb;
2893 }
2894 if (fill_T && cp9b->Tvalid[0] && Talpha[0][jp_0][Lp_0] > sc) {
2895 sc = Talpha[0][jp_0][Lp_0];
2896 mode = TRMODE_T;
2897 b = Tb;
2898 }
2899 }
2900
2901 #if eslDEBUGLEVEL >= 2
2902 /* Uncomment to dump matrix to file. Careful...this could be very large. */
2903 /*FILE *fp1; fp1 = fopen("tmp.tru_cykhbmx", "w"); cm_tr_hb_mx_Dump(fp1, mx, preset_mode, TRUE); fclose(fp1);*/
2904 /*FILE *fp2; fp2 = fopen("tmp.tru_cykhbshmx", "w"); cm_tr_hb_shadow_mx_Dump(fp2, cm, shmx, preset_mode, TRUE); fclose(fp2);*/
2905 #endif
2906
2907 if(ret_b != NULL) *ret_b = b;
2908 if(ret_mode != NULL) *ret_mode = mode;
2909 if(ret_sc != NULL) *ret_sc = sc;
2910
2911 free(el_scA);
2912 free(yvalidA);
2913
2914 ESL_DPRINTF1(("#DEBUG: cm_TrCYKInsideAlignHB return sc: %f\n", sc));
2915
2916 if(*ret_mode == TRMODE_UNKNOWN) ESL_FAIL(eslEAMBIGUOUS, errbuf, "cm_TrCYKInsideAlignHB() no valid parsetree found");
2917
2918 return eslOK;
2919
2920 ERROR:
2921 ESL_FAIL(status, errbuf, "Memory allocation error.\n");
2922 }
2923
2924 /* Function: cm_TrInsideAlign()
2925 * Date: EPN, Mon Sep 12 04:31:43 2011
2926 *
2927 * Purpose: Run the truncated inside algorithm on a target sequence
2928 * without using bands. The full target sequence 1..L is
2929 * aligned (only full alignments will contribute to the
2930 * Inside score).
2931 *
2932 * Identical to cm_InsideAlign() but no bands are used.
2933 *
2934 * Very similar to cm_TrCYKInsideAlign(), see 'Purpose'
2935 * of that function for more details. Only differences with
2936 * that function is:
2937 * - we do TrInside, not TrCYK
2938 * - can't return a shadow matrix (we're not aligning)
2939 * - doesn't return bsc, b info about truncated begins
2940 *
2941 * The caller may already know the mode of the alignment,
2942 * passed in as <preset_mode>. This will happen if we're
2943 * being called from within a search pipeline, for
2944 * example. If the caller does not know the optimal mode yet
2945 * (e.g. if we're being called for 'cmalign'), <preset_mode>
2946 * will be TRMODE_UNKNOWN.
2947 *
2948 * This function complements cm_TrOutsideAlign().
2949 *
2950 * Args: cm - the model
2951 * errbuf - char buffer for reporting errors
2952 * dsq - the digitized sequence
2953 * L - target sequence length
2954 * size_limit - max number of Mb for DP matrix, if matrix is bigger return eslERANGE
2955 * preset_mode - the pre-determined alignment mode, TRMODE_UNKNOWN to allow any mode
2956 * pass_idx - pipeline pass index, indicates what truncation penalty to use
2957 * mx - the dp matrix, grown and filled here
2958 * ret_mode - RETURN: mode of optimal truncation mode, TRMODE_{J,L,R,T} if {J,L,R,T}alpha[0][L][L] is max scoring.
2959 * ret_sc - RETURN: log P(S|M)/P(S|R), as a bit score
2960 * NOTE: we don't sum over different marginal modes, we pick the highest scoring
2961 * one (J,L,R or T) and return {J,L,R,T}alpha[0][L][L] the sum of all complete
2962 * J,L,R, or T alignments.
2963 *
2964 * Returns: <eslOK> on success.
2965 *
2966 * Throws: <eslERANGE> if required CM_TR_MX size exceeds <size_limit>
2967 * In this case alignment has been aborted, ret_sc is not valid
2968 */
2969 int
2970 cm_TrInsideAlign(CM_t *cm, char *errbuf, ESL_DSQ *dsq, int L, float size_limit, char preset_mode, int pass_idx,
2971 CM_TR_MX *mx, char *ret_mode, float *ret_sc)
2972 {
2973 int status; /* easel status code */
2974 int v,y,z; /* indices for states */
2975 int j,d,i,k; /* indices in sequence dimensions */
2976 float sc; /* a temporary variable holding a score */
2977 int yoffset; /* y=base+offset -- counter in child states that v can transit to */
2978 float *el_scA; /* [0..d..W-1] probability of local end emissions of length d */
2979 int sd; /* StateDelta(cm->sttype[v]) */
2980 int sdl; /* StateLeftDelta(cm->sttype[v] */
2981 int sdr; /* StateRightDelta(cm->sttype[v] */
2982 int j_sdr; /* j - sdr */
2983 int d_sd; /* d - sd */
2984 int d_sdl; /* d - sdl */
2985 int d_sdr; /* d - sdr */
2986 float tsc; /* a transition score */
2987
2988 /* other variables used in truncated version, but not standard version (not in cm_CYKInsideAlign()) */
2989 char mode = TRMODE_UNKNOWN; /* truncation mode for obtaining optimal score <ret_sc> */
2990 int Lyoffset0; /* first yoffset to use for updating L matrix in IR/MR states, 1 if IR, 0 if MR */
2991 int Ryoffset0; /* first yoffset to use for updating R matrix in IL/ML states, 1 if IL, 0 if ML */
2992 int fill_L, fill_R, fill_T; /* must we fill in the L, R, and T matrices? */
2993 int pty_idx; /* index for truncation penalty, determined by pass_idx */
2994 float trpenalty; /* truncation penalty, differs based on pty_idx and if we're local or global */
2995
2996 /* the DP matrix */
2997 float ***Jalpha = mx->Jdp; /* pointer to the Jalpha DP matrix */
2998 float ***Lalpha = mx->Ldp; /* pointer to the Lalpha DP matrix */
2999 float ***Ralpha = mx->Rdp; /* pointer to the Ralpha DP matrix */
3000 float ***Talpha = mx->Tdp; /* pointer to the Talpha DP matrix */
3001
3002 /* Determine which matrices we need to fill in, based on <preset_mode>, if TRMODE_UNKNOWN, fill_L, fill_R, fill_T will all be set as TRUE */
3003 if((status = cm_TrFillFromMode(preset_mode, &fill_L, &fill_R, &fill_T)) != eslOK) ESL_FAIL(status, errbuf, "cm_TrInsideAlign(), bogus mode: %d", preset_mode);
3004
3005 /* Determine the truncation penalty index, from the pass_idx */
3006 if((pty_idx = cm_tr_penalties_IdxForPass(pass_idx)) == -1) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_TrInsideAlign(), unexpected pass idx: %d", pass_idx);
3007
3008 /* Allocations and initializations */
3009
3010 /* grow the matrices for current sequence */
3011 if((status = cm_tr_mx_GrowTo(cm, mx, errbuf, L, size_limit)) != eslOK) return status;
3012
3013 /* precalcuate all possible local end scores, for local end emits of 1..L residues */
3014 ESL_ALLOC(el_scA, sizeof(float) * (L+1));
3015 for(d = 0; d <= L; d++) el_scA[d] = cm->el_selfsc * d;
3016
3017 /* initialize all cells of the matrix to IMPOSSIBLE */
3018 if(mx->Jncells_valid > 0) esl_vec_FSet(mx->Jdp_mem, mx->Jncells_valid, IMPOSSIBLE);
3019 if(mx->Lncells_valid > 0 && fill_L) esl_vec_FSet(mx->Ldp_mem, mx->Lncells_valid, IMPOSSIBLE);
3020 if(mx->Rncells_valid > 0 && fill_R) esl_vec_FSet(mx->Rdp_mem, mx->Rncells_valid, IMPOSSIBLE);
3021 if(mx->Tncells_valid > 0 && fill_T) esl_vec_FSet(mx->Tdp_mem, mx->Tncells_valid, IMPOSSIBLE);
3022
3023 /* if local ends are on, replace the EL deck IMPOSSIBLEs with EL scores */
3024 if(cm->flags & CMH_LOCAL_END) {
3025 for (j = 0; j <= L; j++) {
3026 for (d = 0; d <= j; d++) {
3027 Jalpha[cm->M][j][d] = el_scA[d];
3028 }
3029 }
3030 if(fill_L) {
3031 for (j = 0; j <= L; j++) {
3032 for (d = 0; d <= j; d++) {
3033 Lalpha[cm->M][j][d] = el_scA[d];
3034 }
3035 }
3036 }
3037 if(fill_R) {
3038 for (j = 0; j <= L; j++) {
3039 for (d = 0; d <= j; d++) {
3040 Ralpha[cm->M][j][d] = el_scA[d];
3041 }
3042 }
3043 }
3044 }
3045
3046 /* Main recursion */
3047 for (v = cm->M-1; v > 0; v--) { /* almost to ROOT_S, we handle that differently */
3048 float const *esc_v = cm->oesc[v]; /* emission scores for state v */
3049 float const *tsc_v = cm->tsc[v]; /* transition scores for state v */
3050 float const *lmesc_v = cm->lmesc[v]; /* marginal left emission scores for state v */
3051 float const *rmesc_v = cm->rmesc[v]; /* marginal right emission scores for state v */
3052 sd = StateDelta(cm->sttype[v]);
3053 sdl = StateLeftDelta(cm->sttype[v]);
3054 sdr = StateRightDelta(cm->sttype[v]);
3055
3056 /* re-initialize the J, L and R decks if we can do a local end from v */
3057 if(NOT_IMPOSSIBLE(cm->endsc[v])) {
3058 for (j = 0; j <= L; j++) {
3059 for (d = sd; d <= j; d++) {
3060 Jalpha[v][j][d] = Jalpha[cm->M][j][d-sd] + cm->endsc[v];
3061 }
3062 }
3063 if(fill_L) {
3064 for (j = 0; j <= L; j++) {
3065 for (d = sdl; d <= j; d++) {
3066 Lalpha[v][j][d] = Lalpha[cm->M][j][d-sdl] + cm->endsc[v];
3067 }
3068 }
3069 }
3070 if(fill_R) {
3071 for (j = 0; j <= L; j++) {
3072 for (d = sdr; d <= j; d++) {
3073 Ralpha[v][j][d] = Ralpha[cm->M][j][d-sdr] + cm->endsc[v];
3074 }
3075 }
3076 }
3077 }
3078 /* otherwise this state's deck has already been initialized to IMPOSSIBLE */
3079
3080 if(cm->sttype[v] == E_st) {
3081 for (j = 0; j <= L; j++) {
3082 Jalpha[v][j][0] = 0.;
3083 if(fill_L) Lalpha[v][j][0] = 0.;
3084 if(fill_R) Ralpha[v][j][0] = 0.;
3085 /* rest of deck remains IMPOSSIBLE */
3086 }
3087 }
3088 else if(cm->sttype[v] == IL_st || cm->sttype[v] == ML_st) {
3089 /* update alpha[v][j][d] cells, for IL states, loop nesting order is:
3090 * for j { for d { for y { } } } because they can self transit, and a
3091 * alpha[v][j][d] cell must be complete (that is we must have looked at all children y)
3092 * before can start calc'ing for alpha[v][j][d+1]
3093 * We do ML states as well as IL states b/c they follow the same rules,
3094 * and we're not worried about efficiency here.
3095 */
3096
3097 /* In TrCYK: we need to treat R differently from and J and L
3098 * here, by doing separate 'for (yoffset...' loops for J and R
3099 * because we have to fully calculate Jalpha[v][j][d]) before we
3100 * can start to calculate Ralpha[v][j][d].
3101 */
3102 if(! StateIsDetached(cm, v)) { /* if we're detached (unreachable), leave all {J,L,R}alpha values as they were initialized, as IMPOSSIBLE */
3103 Ryoffset0 = cm->sttype[v] == IL_st ? 1 : 0; /* don't allow IL self transits in R mode */
3104 for (j = sdr; j <= L; j++) {
3105 j_sdr = j - sdr;
3106 for (d = sd; d <= j; d++) {
3107 d_sd = d - sd;
3108 i = j - d + 1;
3109 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++) {
3110 y = cm->cfirst[v] + yoffset;
3111 Jalpha[v][j][d] = FLogsum(Jalpha[v][j][d], Jalpha[y][j_sdr][d_sd] + tsc_v[yoffset]);
3112 if(fill_L) Lalpha[v][j][d] = FLogsum(Lalpha[v][j][d], Lalpha[y][j_sdr][d_sd] + tsc_v[yoffset]);
3113 }
3114 Jalpha[v][j][d] += esc_v[dsq[i]];
3115 if(fill_L) Lalpha[v][j][d] = (d >= 2) ? Lalpha[v][j][d] + esc_v[dsq[i]] : esc_v[dsq[i]];
3116
3117 Jalpha[v][j][d] = ESL_MAX(Jalpha[v][j][d], IMPOSSIBLE);
3118 if(fill_L) Lalpha[v][j][d] = ESL_MAX(Lalpha[v][j][d], IMPOSSIBLE);
3119 i--;
3120
3121 /* handle R separately */
3122 if(fill_R) {
3123 /* note we use 'd', not 'd_sd' (which we used in the corresponding loop for J,L above) */
3124 for (yoffset = Ryoffset0; yoffset < cm->cnum[v]; yoffset++) { /* using Ryoffset0 instead of 0 disallows IL self transits in R mode */
3125 y = cm->cfirst[v] + yoffset;
3126 Ralpha[v][j][d] = FLogsum(Ralpha[v][j][d], Jalpha[y][j_sdr][d] + tsc_v[yoffset]);
3127 Ralpha[v][j][d] = FLogsum(Ralpha[v][j][d], Ralpha[y][j_sdr][d] + tsc_v[yoffset]);
3128 }
3129 Ralpha[v][j][d] = ESL_MAX(Ralpha[v][j][d], IMPOSSIBLE);
3130 }
3131 }
3132 }
3133 } /* end of if(! StateIsDetached(cm, v)) */
3134 }
3135 else if(cm->sttype[v] == IR_st || cm->sttype[v] == MR_st) {
3136 /* update alpha[v][j][d] cells, for IR states, loop nesting order is:
3137 * for j { for d { for y { } } } because they can self transit, and a
3138 * alpha[v][j][d] cell must be complete (that is we must have looked at all children y)
3139 * before can start calc'ing for alpha[v][j][d+1].
3140 * We do MR states as well as IR states b/c they follow the same rules,
3141 * and we're not worried about efficiency here.
3142 */
3143
3144 /* In TrCYK: we need to treat L differently from and J and R
3145 * here, by doing separate 'for (yoffset...' loops for J and R
3146 * because we have to fully calculate Jalpha[v][j][d]) before we
3147 * can start to calculate Lalpha[v][j][d].
3148 */
3149 if(! StateIsDetached(cm, v)) { /* if we're detached (unreachable), leave all {J,L,R}alpha values as they were initialized, as IMPOSSIBLE */
3150 Lyoffset0 = cm->sttype[v] == IR_st ? 1 : 0; /* don't allow IR self transits in L mode */
3151 for (j = sdr; j <= L; j++) {
3152 j_sdr = j - sdr;
3153 for (d = sd; d <= j; d++) {
3154 d_sd = d - sd;
3155 i = j - d + 1;
3156 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++) {
3157 y = cm->cfirst[v] + yoffset;
3158 Jalpha[v][j][d] = FLogsum(Jalpha[v][j][d], Jalpha[y][j_sdr][d_sd] + tsc_v[yoffset]);
3159 if(fill_R) Ralpha[v][j][d] = FLogsum(Ralpha[v][j][d], Ralpha[y][j_sdr][d_sd] + tsc_v[yoffset]);
3160 }
3161
3162 Jalpha[v][j][d] += esc_v[dsq[j]];
3163 if(fill_R) Ralpha[v][j][d] = (d >= 2) ? Ralpha[v][j][d] + esc_v[dsq[j]] : esc_v[dsq[j]];
3164
3165 Jalpha[v][j][d] = ESL_MAX(Jalpha[v][j][d], IMPOSSIBLE);
3166 if(fill_R) Ralpha[v][j][d] = ESL_MAX(Ralpha[v][j][d], IMPOSSIBLE);
3167
3168 /* handle L separately */
3169 if(fill_L) {
3170 /* note we use 'j' and 'd', not 'j_sdr' and 'd_sd' (which we used in the corresponding loop for J,R above) */
3171 for (yoffset = Lyoffset0; yoffset < cm->cnum[v]; yoffset++) { /* using Lyoffset0, instead of 0 disallows IR self transits in L mode */
3172 y = cm->cfirst[v] + yoffset;
3173 Lalpha[v][j][d] = FLogsum(Lalpha[v][j][d], Jalpha[y][j][d] + tsc_v[yoffset]);
3174 Lalpha[v][j][d] = FLogsum(Lalpha[v][j][d], Lalpha[y][j][d] + tsc_v[yoffset]);
3175 }
3176 Lalpha[v][j][d] = ESL_MAX(Lalpha[v][j][d], IMPOSSIBLE);
3177 }
3178 }
3179 }
3180 } /* end of if(! StateIsDetached(cm, v)) */
3181 }
3182 else if(cm->sttype[v] == MP_st) {
3183 /* MP states cannot self transit, this means that all cells in
3184 * alpha[v] are independent of each other, only depending on
3185 * alpha[y] for previously calc'ed y. We can do the for loops
3186 * in any nesting order, this implementation does what I think
3187 * is most efficient: for y { for j { for d { } } }
3188 */
3189 for (y = cm->cfirst[v]; y < (cm->cfirst[v] + cm->cnum[v]); y++) {
3190 yoffset = y - cm->cfirst[v];
3191 tsc = tsc_v[yoffset];
3192
3193 for (j = sdr; j <= L; j++) {
3194 j_sdr = j - sdr;
3195
3196 for (d = sd; d <= j; d++) { /* sd == 2 for MP state */
3197 d_sd = d - sd;
3198 Jalpha[v][j][d] = FLogsum(Jalpha[v][j][d], Jalpha[y][j_sdr][d_sd] + tsc_v[yoffset]);
3199 }
3200 if(fill_L) {
3201 /* note we use 'j' and 'd_sdl' not 'j_sdr' for 'd_sd' for L, plus minimum d is sdl (1) */
3202 for (d = sdl; d <= j; d++) { /* sdl == 1 for MP state */
3203 d_sdl = d-sdl;
3204 Lalpha[v][j][d] = FLogsum(Lalpha[v][j][d], Jalpha[y][j][d_sdl] + tsc_v[yoffset]);
3205 Lalpha[v][j][d] = FLogsum(Lalpha[v][j][d], Lalpha[y][j][d_sdl] + tsc_v[yoffset]);
3206 }
3207 }
3208 if(fill_R) {
3209 /* note we use 'd_sdr' not 'd_sd' for R, plus minimum d is sdr (1) */
3210 for (d = sdr; d <= j; d++) { /* sdr == 1 for MP state */
3211 d_sdr = d - sdr;
3212 Ralpha[v][j][d] = FLogsum(Ralpha[v][j][d], Jalpha[y][j_sdr][d_sdr] + tsc_v[yoffset]);
3213 Ralpha[v][j][d] = FLogsum(Ralpha[v][j][d], Ralpha[y][j_sdr][d_sdr] + tsc_v[yoffset]);
3214 }
3215 }
3216 }
3217 }
3218 /* add in emission score */
3219 for (j = 0; j <= L; j++) {
3220 i = j;
3221 Jalpha[v][j][1] = IMPOSSIBLE;
3222 if(fill_L) Lalpha[v][j][1] = lmesc_v[dsq[i]];
3223 if(fill_R) Ralpha[v][j][1] = rmesc_v[dsq[j]];
3224 i--;
3225 for (d = 2; d <= j; d++) {
3226 Jalpha[v][j][d] += esc_v[dsq[i]*cm->abc->Kp+dsq[j]];
3227 if(fill_L) Lalpha[v][j][d] += lmesc_v[dsq[i]];
3228 if(fill_R) Ralpha[v][j][d] += rmesc_v[dsq[j]];
3229 i--;
3230 }
3231 }
3232 /* ensure all cells are >= IMPOSSIBLE */
3233 for (j = 0; j <= L; j++) {
3234 for (d = 1; d <= j; d++) {
3235 Jalpha[v][j][d] = ESL_MAX(Jalpha[v][j][d], IMPOSSIBLE);
3236 if(fill_L) Lalpha[v][j][d] = ESL_MAX(Lalpha[v][j][d], IMPOSSIBLE);
3237 if(fill_R) Ralpha[v][j][d] = ESL_MAX(Ralpha[v][j][d], IMPOSSIBLE);
3238 }
3239 }
3240 }
3241 else if(cm->sttype[v] != B_st) { /* entered if state v is D or S */
3242 /* D, S states cannot self transit, this means that all cells in
3243 * alpha[v] are independent of each other, only depending on
3244 * alpha[y] for previously calc'ed y. We can do the for loops
3245 * in any nesting order, this implementation does what I think
3246 * is most efficient: for y { for j { for d { } } }
3247 */
3248 for (y = cm->cfirst[v]; y < (cm->cfirst[v] + cm->cnum[v]); y++) {
3249 yoffset = y - cm->cfirst[v];
3250 tsc = tsc_v[yoffset];
3251
3252 for (j = sdr; j <= L; j++) {
3253 j_sdr = j - sdr;
3254
3255 for (d = sd; d <= j; d++) {
3256 d_sd = d-sd;
3257 Jalpha[v][j][d] = FLogsum(Jalpha[v][j][d], Jalpha[y][j_sdr][d_sd] + tsc);
3258 if(fill_L) Lalpha[v][j][d] = FLogsum(Lalpha[v][j][d], Lalpha[y][j_sdr][d_sd] + tsc);
3259 if(fill_R) Ralpha[v][j][d] = FLogsum(Ralpha[v][j][d], Ralpha[y][j_sdr][d_sd] + tsc);
3260 }
3261 /* an easy to overlook case: if d == 0, ensure L and R values are IMPOSSIBLE */
3262 if(fill_L) Lalpha[v][j][0] = IMPOSSIBLE;
3263 if(fill_R) Ralpha[v][j][0] = IMPOSSIBLE;
3264 }
3265 }
3266 /* no emission score to add */
3267 }
3268 else { /* B_st */
3269 assert(cm->sttype[v] == B_st);
3270 y = cm->cfirst[v]; /* left subtree */
3271 z = cm->cnum[v]; /* right subtree */
3272
3273 for (j = 0; j <= L; j++) {
3274 for (d = 0; d <= j; d++) {
3275 for (k = 0; k <= d; k++) {
3276 Jalpha[v][j][d] = FLogsum(Jalpha[v][j][d], Jalpha[y][j-k][d-k] + Jalpha[z][j][k]);
3277 if(fill_L) Lalpha[v][j][d] = FLogsum(Lalpha[v][j][d], Jalpha[y][j-k][d-k] + Lalpha[z][j][k]);
3278 if(fill_R) Ralpha[v][j][d] = FLogsum(Ralpha[v][j][d], Ralpha[y][j-k][d-k] + Jalpha[z][j][k]);
3279 }
3280 if(fill_T) {
3281 for(k = 1; k < d; k++) { /* special boundary case for T matrix */
3282 Talpha[v][j][d] = FLogsum(Talpha[v][j][d], Ralpha[y][j-k][d-k] + Lalpha[z][j][k]);
3283 }
3284 }
3285 /* two additional special cases in trCYK (these are not in standard CYK) */
3286 /* special case 1: k == 0 (full sequence aligns to BEGL_S left child */
3287 if(fill_L) {
3288 Lalpha[v][j][d] = FLogsum(Lalpha[v][j][d], Jalpha[y][j][d]);
3289 Lalpha[v][j][d] = FLogsum(Lalpha[v][j][d], Lalpha[y][j][d]);
3290 }
3291 /* special case 2: k == d (full sequence aligns to BEGR_S right child */
3292 if(fill_R) {
3293 Ralpha[v][j][d] = FLogsum(Ralpha[v][j][d], Jalpha[z][j][d]);
3294 Ralpha[v][j][d] = FLogsum(Ralpha[v][j][d], Ralpha[z][j][d]);
3295 }
3296 }
3297 }
3298 } /* end of B_st recursion */
3299
3300 /* Now handle from ROOT_S, state 0. So far we haven't touched
3301 * the {J,L,R,T}alpha[0] decks at all since initialization and here
3302 * we'll only update at most 1 cell in each, the one pertaining
3303 * to a full alignment [0][L][L].
3304 *
3305 * In truncated alignment the only way out of ROOT_S in local or
3306 * global mode is via a 'truncated begin' with a score (penalty)
3307 * from cm->trp into any emitting state. The penalty was
3308 * calculated in cm_tr_penalties_Create() and differs depending on
3309 * whether we are in local or global mode and the value of
3310 * 'pty_idx' which was passed in.
3311 */
3312 trpenalty = (cm->flags & CMH_LOCAL_BEGIN) ? cm->trp->l_ptyAA[pty_idx][v] : cm->trp->g_ptyAA[pty_idx][v];
3313 if(NOT_IMPOSSIBLE(trpenalty)) {
3314 /* include full length hits in J matrix */
3315 Jalpha[0][L][L] = FLogsum(Jalpha[0][L][L], Jalpha[v][L][L] + trpenalty);
3316 /* include full length hits in L matrix */
3317 if(fill_L) {
3318 Lalpha[0][L][L] = FLogsum(Lalpha[0][L][L], Lalpha[v][L][L] + trpenalty);
3319 }
3320 /* include full length hits in R matrix */
3321 if(fill_R) {
3322 Ralpha[0][L][L] = FLogsum(Ralpha[0][L][L], Ralpha[v][L][L] + trpenalty);
3323 }
3324 /* include full length hits in T matrix */
3325 if(fill_T && cm->sttype[v] == B_st) {
3326 Talpha[0][L][L] = FLogsum(Talpha[0][L][L], Talpha[v][L][L] + trpenalty);
3327 }
3328 }
3329 } /* end of for (v = cm->M-1; v > 0; v--) */
3330
3331 /* determine mode of optimal alignment, if it was preset then use that */
3332 if(preset_mode == TRMODE_J) {
3333 sc = Jalpha[0][L][L];
3334 mode = TRMODE_J;
3335 }
3336 else if(preset_mode == TRMODE_L) {
3337 sc = Lalpha[0][L][L];
3338 mode = TRMODE_L;
3339 }
3340 else if(preset_mode == TRMODE_R) {
3341 sc = Ralpha[0][L][L];
3342 mode = TRMODE_R;
3343 }
3344 else if(preset_mode == TRMODE_T) {
3345 sc = Talpha[0][L][L];
3346 mode = TRMODE_T;
3347 }
3348 else { /* preset_mode was unknown, max score determines mode */
3349 sc = Jalpha[0][L][L];
3350 mode = TRMODE_J;
3351 if (fill_L && Lalpha[0][L][L] > sc) {
3352 sc = Lalpha[0][L][L];
3353 mode = TRMODE_L;
3354 }
3355 if (fill_R && Ralpha[0][L][L] > sc) {
3356 sc = Ralpha[0][L][L];
3357 mode = TRMODE_R;
3358 }
3359 if (fill_T && Talpha[0][L][L] > sc) {
3360 sc = Talpha[0][L][L];
3361 mode = TRMODE_T;
3362 }
3363 }
3364
3365 #if eslDEBUGLEVEL >= 2
3366 /* Uncomment to dump matrix to file. Careful...this could be very large. */
3367 /* FILE *fp1; fp1 = fopen("tmp.tru_imx", "w"); cm_tr_mx_Dump(fp1, mx, mode, TRUE); fclose(fp1); */
3368 #endif
3369
3370 if(ret_mode != NULL) *ret_mode = mode;
3371 if(ret_sc != NULL) *ret_sc = sc;
3372
3373 free(el_scA);
3374
3375 ESL_DPRINTF1(("#DEBUG: cm_TrInsideAlign() return sc: %f\n", sc));
3376 return eslOK;
3377
3378 ERROR:
3379 ESL_FAIL(status, errbuf, "Memory allocation error.\n");
3380 }
3381
3382
3383 /* Function: cm_TrInsideAlignHB()
3384 * Date: EPN, Mon Sep 12 04:32:00 2011
3385 *
3386 * Purpose: Run the truncated inside algorithm on a target sequence
3387 * using bands in the j and d dimensions of the DP
3388 * matrix. Bands were obtained from an HMM Forward-Backward
3389 * parse of the target sequence. Uses float log odds scores.
3390 * The full target sequence 1..L is aligned (only full
3391 * alignments will contribute to the Inside score).
3392 *
3393 * Very similar to cm_TrCYKInsideAlignHB(), see 'Purpose'
3394 * of that function for more details. Only differences with
3395 * that function is:
3396 * - we do TrInside, not TrCYK
3397 * - can't return a shadow matrix (we're not aligning)
3398 * - doesn't return b, info about local begins
3399 *
3400 * The caller may already know the mode of the alignment,
3401 * passed in as <preset_mode>. This will happen if we're
3402 * being called from within a search pipeline, for
3403 * example. If the caller does not know the optimal mode yet
3404 * (e.g. if we're being called for 'cmalign'), <preset_mode>
3405 * will be TRMODE_UNKNOWN.
3406 *
3407 * This function complements cm_TrOutsideAlignHB().
3408 *
3409 * Args: cm - the model [0..M-1]
3410 * errbuf - char buffer for reporting errors
3411 * dsq - the digitized sequence
3412 * L - target sequence length
3413 * size_limit - max number of Mb for DP matrix, if matrix is bigger return eslERANGE
3414 * preset_mode - the pre-determined alignment mode, TRMODE_UNKNOWN to allow any mode
3415 * pass_idx - pipeline pass index, indicates what truncation penalty to use
3416 * mx - the dp matrix, only cells within bands in cp9b will be valid
3417 * ret_mode - RETURN: mode of optimal truncation mode, TRMODE_{J,L,R,T} if {J,L,R,T}alpha[0][L][L] is max scoring.
3418 * ret_sc - RETURN: log P(S|M)/P(S|R), as a bit score
3419 * NOTE: we don't sum over different marginal modes, we pick the highest scoring
3420 * one (J,L,R or T) and return {J,L,R,T}alpha[0][L][L] the sum of all complete
3421 * J,L,R, or T alignments.
3422 *
3423 * Returns: <eslOK> on success.
3424 *
3425 * Throws: <eslERANGE> if required CM_TR_HB_MX size exceeds <size_limit>
3426 * <eslEINVAL> if the full sequence is not within the bands for state 0
3427 * <eslEAMBIGUOUS> if no valid alignment is possible due to bands (score of sequence is IMPOSSIBLE)
3428 * In any of these three cases, alignment has been aborted, ret variables are not valid.
3429 */
3430 int
3431 cm_TrInsideAlignHB(CM_t *cm, char *errbuf, ESL_DSQ *dsq, int L, float size_limit, char preset_mode, int pass_idx,
3432 CM_TR_HB_MX *mx, char *ret_mode, float *ret_sc)
3433 {
3434 int status;
3435 int v,y,z; /* indices for states */
3436 int j,d,i,k; /* indices in sequence dimensions */
3437 float sc; /* temporary score */
3438 float tsc; /* a temporary variable holding a transition score */
3439 int yoffset; /* y=base+offset -- counter in child states that v can transit to */
3440 int sd; /* StateDelta(cm->sttype[v]) */
3441 int sdl; /* StateLeftDelta(cm->sttype[v]) */
3442 int sdr; /* StateRightDelta(cm->sttype[v]) */
3443 int *yvalidA; /* [0..MAXCONNECT-1] TRUE if v->yoffset is legal transition (within bands) */
3444 float *el_scA; /* [0..d..W-1] probability of local end emissions of length d */
3445
3446 /* indices used for handling band-offset issues, and in the depths of the DP recursion */
3447 int jp_v, jp_y, jp_z; /* offset j index for states v, y, z */
3448 int jp_y_sdr; /* jp_y - sdr */
3449 int j_sdr; /* j - sdr */
3450 int jn, jx; /* current minimum/maximum j allowed */
3451 int jpn, jpx; /* minimum/maximum jp_v */
3452 int dp_v, dp_y, dp_z; /* d index for state v/y/z in alpha w/mem eff bands */
3453 int dn, dx; /* current minimum/maximum d allowed */
3454 int dp_y_sd; /* dp_y - sd */
3455 int dp_y_sdl; /* dp_y - sdl */
3456 int dp_y_sdr; /* dp_y - sdr */
3457 int dpn, dpx; /* minimum/maximum dp_v */
3458 int kp_z; /* k (in the d dim) index for state z in alpha w/mem eff bands */
3459 int kn, kx; /* current minimum/maximum k value */
3460 int Lp; /* L also changes depending on state */
3461 int yvalid_idx; /* for keeping track of which children are valid */
3462 int yvalid_ct; /* for keeping track of which children are valid */
3463 int jp_0; /* L offset in ROOT_S's (v==0) j band */
3464 int Lp_0; /* L offset in ROOT_S's (v==0) d band */
3465
3466 /* variables related to truncated alignment (not in cm_InsideAlignHB()) */
3467 char mode = TRMODE_UNKNOWN; /* truncation mode for obtaining optimal score <ret_sc> */
3468 int fill_L, fill_R, fill_T; /* must we fill in the L, R, and T matrices? */
3469 int do_J_v, do_J_y, do_J_z; /* must we fill J matrix deck for state v, y, z? */
3470 int do_L_v, do_L_y, do_L_z; /* must we fill L matrix deck for state v, y, z? */
3471 int do_R_v, do_R_y, do_R_z; /* must we fill R matrix deck for state v, y, z? */
3472 int do_T_v; /* must we fill T matrix deck for state v? */
3473 int pty_idx; /* index for truncation penalty, determined by pass_idx */
3474 float trpenalty; /* truncation penalty, differs based on pty_idx and if we're local or global */
3475
3476 /* ptrs to cp9b info, for convenience */
3477 CP9Bands_t *cp9b = cm->cp9b;
3478 int *jmin = cp9b->jmin;
3479 int *jmax = cp9b->jmax;
3480 int **hdmin = cp9b->hdmin;
3481 int **hdmax = cp9b->hdmax;
3482
3483 /* the DP matrix */
3484 float ***Jalpha = mx->Jdp; /* pointer to the Jalpha DP matrix */
3485 float ***Lalpha = mx->Ldp; /* pointer to the Lalpha DP matrix */
3486 float ***Ralpha = mx->Rdp; /* pointer to the Ralpha DP matrix */
3487 float ***Talpha = mx->Tdp; /* pointer to the Talpha DP matrix */
3488
3489 /* Determine which matrices we need to fill in, based on <preset_mode>, if TRMODE_UNKNOWN, fill_L, fill_R, fill_T will all be set as TRUE */
3490 if((status = cm_TrFillFromMode(preset_mode, &fill_L, &fill_R, &fill_T)) != eslOK) ESL_FAIL(status, errbuf, "cm_TrInsideAlignHB(), bogus mode: %d", preset_mode);
3491
3492 /* Determine the truncation penalty index, from the pass_idx */
3493 if((pty_idx = cm_tr_penalties_IdxForPass(pass_idx)) == -1) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_TrInsideAlignHB(), unexpected pass idx: %d", pass_idx);
3494
3495 /* Allocations and initializations */
3496
3497 /* ensure a full alignment to ROOT_S (v==0) is possible, remember In Inside <preset_mode> may be known or unknown */
3498 if (preset_mode == TRMODE_J && (! cp9b->Jvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrInsideAlignHB(): preset_mode is J mode, but cp9b->Jvalid[v] is FALSE");
3499 if (preset_mode == TRMODE_L && (! cp9b->Lvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrInsideAlignHB(): preset_mode is L mode, but cp9b->Lvalid[v] is FALSE");
3500 if (preset_mode == TRMODE_R && (! cp9b->Rvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrInsideAlignHB(): preset_mode is R mode, but cp9b->Rvalid[v] is FALSE");
3501 if (preset_mode == TRMODE_T && (! cp9b->Tvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrInsideAlignHB(): preset_mode is T mode, but cp9b->Tvalid[v] is FALSE");
3502 if (preset_mode == TRMODE_UNKNOWN && (! (cp9b->Jvalid[0] || cp9b->Lvalid[0] || cp9b->Rvalid[0] || cp9b->Tvalid[0]))) {
3503 ESL_FAIL(eslEINVAL, errbuf, "cm_TrInsideAlignHB(): no marginal mode is allowed for state 0");
3504 }
3505 if (cp9b->jmin[0] > L || cp9b->jmax[0] < L) ESL_FAIL(eslEINVAL, errbuf, "cm_TrInsideAlignHB(): L (%d) is outside ROOT_S's j band (%d..%d)\n", L, cp9b->jmin[0], cp9b->jmax[0]);
3506 jp_0 = L - jmin[0];
3507 if (cp9b->hdmin[0][jp_0] > L || cp9b->hdmax[0][jp_0] < L) ESL_FAIL(eslEINVAL, errbuf, "cm_TrInsideAlignHB(): L (%d) is outside ROOT_S's d band (%d..%d)\n", L, cp9b->hdmin[0][jp_0], cp9b->hdmax[0][jp_0]);
3508 Lp_0 = L - hdmin[0][jp_0];
3509
3510 /* grow the matrix based on the current sequence and bands */
3511 if((status = cm_tr_hb_mx_GrowTo(cm, mx, errbuf, cp9b, L, size_limit)) != eslOK) return status;
3512
3513 /* precalcuate all possible local end scores, for local end emits of 1..L residues */
3514 ESL_ALLOC(el_scA, sizeof(float) * (L+1));
3515 for(d = 0; d <= L; d++) el_scA[d] = cm->el_selfsc * d;
3516
3517 /* yvalidA[0..cnum[v]] will hold TRUE for states y for which a transition is legal
3518 * (some transitions are impossible due to the bands) */
3519 ESL_ALLOC(yvalidA, sizeof(int) * MAXCONNECT);
3520 esl_vec_ISet(yvalidA, MAXCONNECT, FALSE);
3521
3522 /* initialize all cells of the matrix to IMPOSSIBLE */
3523 if(mx->Jncells_valid > 0) esl_vec_FSet(mx->Jdp_mem, mx->Jncells_valid, IMPOSSIBLE);
3524 if(mx->Lncells_valid > 0 && fill_L) esl_vec_FSet(mx->Ldp_mem, mx->Lncells_valid, IMPOSSIBLE);
3525 if(mx->Rncells_valid > 0 && fill_R) esl_vec_FSet(mx->Rdp_mem, mx->Rncells_valid, IMPOSSIBLE);
3526 if(mx->Tncells_valid > 0 && fill_T) esl_vec_FSet(mx->Tdp_mem, mx->Tncells_valid, IMPOSSIBLE);
3527
3528 /* if local ends are on, replace the EL deck IMPOSSIBLEs with EL scores,
3529 * Note: we could optimize by skipping this step and using el_scA[d] to
3530 * initialize ELs for each state in the first step of the main recursion
3531 * below. We fill in the EL deck here for completeness and so that
3532 * a check of this alpha matrix with a Outside matrix will pass.
3533 */
3534 if(cm->flags & CMH_LOCAL_END) {
3535 if(cp9b->Jvalid[cm->M]) {
3536 for (j = 0; j <= L; j++) {
3537 for (d = 0; d <= j; d++) Jalpha[cm->M][j][d] = el_scA[d];
3538 /* remember, the EL deck is non-banded */
3539 }
3540 }
3541 if(fill_L && cp9b->Lvalid[cm->M]) {
3542 for (j = 0; j <= L; j++) {
3543 for (d = 0; d <= j; d++) Lalpha[cm->M][j][d] = el_scA[d];
3544 }
3545 }
3546 if(fill_R && cp9b->Rvalid[cm->M]) {
3547 for (j = 0; j <= L; j++) {
3548 for (d = 0; d <= j; d++) Ralpha[cm->M][j][d] = el_scA[d];
3549 }
3550 }
3551 }
3552
3553 /* Main recursion */
3554 for (v = cm->M-1; v > 0; v--) { /* almost to ROOT_S, we handle that differently */
3555 float const *esc_v = cm->oesc[v]; /* emission scores for state v */
3556 float const *tsc_v = cm->tsc[v]; /* transition scores for state v */
3557 float const *lmesc_v = cm->lmesc[v];
3558 float const *rmesc_v = cm->rmesc[v];
3559 sd = StateDelta(cm->sttype[v]);
3560 sdl = StateLeftDelta(cm->sttype[v]);
3561 sdr = StateRightDelta(cm->sttype[v]);
3562 jn = jmin[v];
3563 jx = jmax[v];
3564 do_J_v = cp9b->Jvalid[v] ? TRUE : FALSE;
3565 do_L_v = cp9b->Lvalid[v] && fill_L ? TRUE : FALSE;
3566 do_R_v = cp9b->Rvalid[v] && fill_R ? TRUE : FALSE;
3567 do_T_v = cp9b->Tvalid[v] && fill_T ? TRUE : FALSE;
3568
3569 /* re-initialize the J, L or R decks if we can do a local end from v */
3570 if(NOT_IMPOSSIBLE(cm->endsc[v])) {
3571 for (j = jmin[v]; j <= jmax[v]; j++) {
3572 jp_v = j - jmin[v];
3573 if(do_J_v && cp9b->Jvalid[cm->M]) {
3574 if(hdmin[v][jp_v] >= sd) {
3575 d = hdmin[v][jp_v];
3576 dp_v = 0;
3577 }
3578 else {
3579 d = sd;
3580 dp_v = sd - hdmin[v][jp_v];
3581 }
3582 for (; d <= hdmax[v][jp_v]; dp_v++, d++) {
3583 Jalpha[v][jp_v][dp_v] = el_scA[d-sd] + cm->endsc[v];
3584 }
3585 }
3586
3587 if(do_L_v && cp9b->Lvalid[cm->M]) {
3588 if(hdmin[v][jp_v] >= sdl) {
3589 d = hdmin[v][jp_v];
3590 dp_v = 0;
3591 }
3592 else {
3593 d = sdl;
3594 dp_v = sdl - hdmin[v][jp_v];
3595 }
3596 for (; d <= hdmax[v][jp_v]; dp_v++, d++) {
3597 Lalpha[v][jp_v][dp_v] = el_scA[d-sdl] + cm->endsc[v];
3598 }
3599 }
3600
3601 if(do_R_v && cp9b->Rvalid[cm->M]) {
3602 if(hdmin[v][jp_v] >= sdr) {
3603 d = hdmin[v][jp_v];
3604 dp_v = 0;
3605 }
3606 else {
3607 d = sdr;
3608 dp_v = sdr - hdmin[v][jp_v];
3609 }
3610 for (; d <= hdmax[v][jp_v]; dp_v++, d++) {
3611 Ralpha[v][jp_v][dp_v] = el_scA[d-sdr] + cm->endsc[v];
3612 }
3613 }
3614 }
3615 }
3616 /* otherwise this state's deck has already been initialized to IMPOSSIBLE */
3617
3618 if(cm->sttype[v] == E_st) {
3619 for (j = jmin[v]; j <= jmax[v]; j++) {
3620 jp_v = j-jmin[v];
3621 ESL_DASSERT1((hdmin[v][jp_v] == 0));
3622 ESL_DASSERT1((hdmax[v][jp_v] == 0));
3623 if(do_J_v) Jalpha[v][jp_v][0] = 0.; /* for End states, d must be 0 */
3624 if(do_L_v) Lalpha[v][jp_v][0] = 0.; /* for End states, d must be 0 */
3625 if(do_R_v) Ralpha[v][jp_v][0] = 0.; /* for End states, d must be 0 */
3626 }
3627 }
3628 else if(cm->sttype[v] == IL_st || cm->sttype[v] == ML_st) {
3629 /* update {J,L,R}alpha[v][jp_v][dp_v] cells, for IL states, loop
3630 * nesting order is: for j { for d { for y { } } } because they
3631 * can self transit, and a {J,L,R}alpha[v][j][d] cell must be
3632 * complete (that is we must have looked at all children y)
3633 * before can start calc'ing for {J,L,R}alpha[v][j][d+1]
3634 * We could be slightly more efficient if we separated out
3635 * MR from IR b/c self-transits in MRs are impossible, but
3636 * we don't do that here. */
3637 if(! StateIsDetached(cm, v)) { /* if we're detached (unreachable), leave all {J,L,R}alpha values as they were initialized, as IMPOSSIBLE */
3638 for (j = jmin[v]; j <= jmax[v]; j++) {
3639 jp_v = j - jmin[v];
3640 yvalid_ct = 0;
3641 j_sdr = j - sdr;
3642
3643 /* determine which children y we can legally transit to for v, j */
3644 for (y = cm->cfirst[v], yoffset = 0; y < (cm->cfirst[v] + cm->cnum[v]); y++, yoffset++)
3645 if((j_sdr) >= jmin[y] && ((j_sdr) <= jmax[y])) yvalidA[yvalid_ct++] = yoffset; /* is j-sdr valid for state y? */
3646
3647 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) { /* for each valid d for v, j */
3648 i = j - d + 1;
3649 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha */
3650
3651 /* We need to treat R differently from and J and L here, by
3652 * doing separate 'for (yoffset...' loops for J and R
3653 * because we have to fully calculate Jalpha[v][jp_v][dp_v])
3654 * before we can start to calculate Ralpha[v][jp_v][dp_v].
3655 */
3656 /* Handle J and L first */
3657 if(do_J_v || do_L_v) {
3658 for (yvalid_idx = 0; yvalid_idx < yvalid_ct; yvalid_idx++) { /* for each valid child y, for v, j */
3659 yoffset = yvalidA[yvalid_idx];
3660 y = cm->cfirst[v] + yoffset;
3661 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
3662 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
3663 if(do_J_y || do_L_y) {
3664 jp_y_sdr = j - jmin[y] - sdr;
3665
3666 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 */
3667 dp_y_sd = d - sd - hdmin[y][jp_y_sdr];
3668 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
3669 ESL_DASSERT1((dp_y_sd >= 0 && dp_y_sd <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
3670 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]);
3671 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]);
3672 }
3673 }
3674 }
3675 if(do_J_v) {
3676 Jalpha[v][jp_v][dp_v] += esc_v[dsq[i]];
3677 Jalpha[v][jp_v][dp_v] = ESL_MAX(Jalpha[v][jp_v][dp_v], IMPOSSIBLE);
3678 }
3679 if(do_L_v) {
3680 Lalpha[v][jp_v][dp_v] = (d >= 2) ? Lalpha[v][jp_v][dp_v] + esc_v[dsq[i]] : esc_v[dsq[i]];
3681 Lalpha[v][jp_v][dp_v] = ESL_MAX(Lalpha[v][jp_v][dp_v], IMPOSSIBLE);
3682 }
3683 i--;
3684 }
3685
3686 if(do_R_v) {
3687 /* Handle R separately */
3688 for (yvalid_idx = 0; yvalid_idx < yvalid_ct; yvalid_idx++) { /* for each valid child y, for v, j */
3689 yoffset = yvalidA[yvalid_idx];
3690 y = cm->cfirst[v] + yoffset;
3691 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
3692 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
3693 if((do_J_y || do_R_y) && (y != v)) { /* (y != v) part is to disallow IL self transits in R mode */
3694 jp_y_sdr = j - jmin[y] - sdr;
3695
3696 /* 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) */
3697 if((d) >= hdmin[y][jp_y_sdr] && (d) <= hdmax[y][jp_y_sdr]) { /* make sure d is valid for this v, j and y */
3698 dp_y = d - hdmin[y][jp_y_sdr];
3699 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
3700 ESL_DASSERT1((dp_y >= 0 && dp_y <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
3701 if(do_J_y) Ralpha[v][jp_v][dp_v] = FLogsum(Ralpha[v][jp_v][dp_v], Jalpha[y][jp_y_sdr][dp_y] + tsc_v[yoffset]);
3702 if(do_R_y) Ralpha[v][jp_v][dp_v] = FLogsum(Ralpha[v][jp_v][dp_v], Ralpha[y][jp_y_sdr][dp_y] + tsc_v[yoffset]);
3703 }
3704 }
3705 } /* end of for (yvalid_idx = 0... loop */
3706 Ralpha[v][jp_v][dp_v] = ESL_MAX(Ralpha[v][jp_v][dp_v], IMPOSSIBLE);
3707 }
3708 }
3709 }
3710 } /* end of if(! StateIsDetached(cm, v) */
3711 }
3712 else if(cm->sttype[v] == IR_st || cm->sttype[v] == MR_st) {
3713 /* update {J,L,R}alpha[v][jp_v][dp_v] cells, for IR states, loop
3714 * nesting order is: for j { for d { for y { } } } because they
3715 * can self transit, and a {J,L,R}alpha[v][j][d] cell must be
3716 * complete (that is we must have looked at all children y)
3717 * before can start calc'ing for {J,L,R}alpha[v][j][d+1].
3718 * We could be slightly more efficient if we separated out
3719 * MR from IR b/c self-transits in MRs are impossible, but
3720 * we don't do that here. */
3721
3722 if(! StateIsDetached(cm, v)) { /* if we're detached (unreachable), leave all {J,L,R}alpha values as they were initialized, as IMPOSSIBLE */
3723 /* The first MR_st/IR_st 'for (j...' loop is for J and R matrices which use the same set of j values */
3724 if(do_J_v || do_R_v) {
3725 for (j = jmin[v]; j <= jmax[v]; j++) {
3726 jp_v = j - jmin[v];
3727 yvalid_ct = 0;
3728 j_sdr = j - sdr;
3729
3730 /* determine which children y we can legally transit to for v, j */
3731 for (y = cm->cfirst[v], yoffset = 0; y < (cm->cfirst[v] + cm->cnum[v]); y++, yoffset++)
3732 if((j_sdr) >= jmin[y] && ((j_sdr) <= jmax[y])) yvalidA[yvalid_ct++] = yoffset; /* is j-sdr is valid for state y? */
3733
3734 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) { /* for each valid d for v, j */
3735 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha */
3736
3737 /* We need to treat L differently from and J and R here, by
3738 * doing separate 'for (yoffset...' loops for J because we
3739 * have to fully calculate Jalpha[v][jp_v][dp_v]) before we
3740 * can start to calculate Lalpha[v][jp_v][dp_v].
3741 */
3742 /* Handle J and R first */
3743 for (yvalid_idx = 0; yvalid_idx < yvalid_ct; yvalid_idx++) { /* for each valid child y, for v, j */
3744 yoffset = yvalidA[yvalid_idx];
3745 y = cm->cfirst[v] + yoffset;
3746 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
3747 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
3748 if(do_J_y || do_R_y) {
3749 jp_y_sdr = j - jmin[y] - sdr;
3750
3751 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 */
3752 dp_y_sd = d - sd - hdmin[y][jp_y_sdr];
3753 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
3754 ESL_DASSERT1((dp_y_sd >= 0 && dp_y_sd <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
3755 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]);
3756 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]);
3757 }
3758 }
3759 }
3760 if(do_J_v) {
3761 Jalpha[v][jp_v][dp_v] += esc_v[dsq[j]];
3762 Jalpha[v][jp_v][dp_v] = ESL_MAX(Jalpha[v][jp_v][dp_v], IMPOSSIBLE);
3763 }
3764 if(do_R_v) {
3765 Ralpha[v][jp_v][dp_v] = (d >= 2) ? Ralpha[v][jp_v][dp_v] + esc_v[dsq[j]] : esc_v[dsq[j]];
3766 Ralpha[v][jp_v][dp_v] = ESL_MAX(Ralpha[v][jp_v][dp_v], IMPOSSIBLE);
3767 }
3768 }
3769 }
3770 }
3771 /* Handle L separately */
3772 if(do_L_v) {
3773 /* The second MR_st/IR_st 'for (j...' loop is for the L matrix which use a different set of j values */
3774 for (j = jmin[v]; j <= jmax[v]; j++) {
3775 jp_v = j - jmin[v];
3776 yvalid_ct = 0;
3777
3778 /* determine which children y we can legally transit to for v, j */
3779 /* we use 'j' and not 'j_sdr' here for the L matrix, differently from J and R matrices above */
3780 for (y = cm->cfirst[v], yoffset = 0; y < (cm->cfirst[v] + cm->cnum[v]); y++, yoffset++)
3781 if(j >= jmin[y] && j <= jmax[y]) yvalidA[yvalid_ct++] = yoffset; /* is j is valid for state y? */
3782
3783 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) { /* for each valid d for v, j */
3784 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha */
3785
3786 for (yvalid_idx = 0; yvalid_idx < yvalid_ct; yvalid_idx++) { /* for each valid child y, for v, j */
3787 /* 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)*/
3788 yoffset = yvalidA[yvalid_idx];
3789 y = cm->cfirst[v] + yoffset;
3790 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
3791 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
3792 if((do_J_y || do_L_y) && (y != v)) { /* (y != v) part is to disallow IR self transits in L mode */
3793 /* 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) */
3794 jp_y = j - jmin[y];
3795
3796 /* 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) */
3797 if((d) >= hdmin[y][jp_y] && (d) <= hdmax[y][jp_y]) { /* make sure d is valid for this v, j and y */
3798 dp_y = d - hdmin[y][jp_y];
3799 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
3800 ESL_DASSERT1((dp_y >= 0 && dp_y <= (hdmax[y][jp_y] - hdmin[y][jp_y])));
3801 if(do_J_y) Lalpha[v][jp_v][dp_v] = FLogsum(Lalpha[v][jp_v][dp_v], Jalpha[y][jp_y][dp_y] + tsc_v[yoffset]);
3802 if(do_L_y) Lalpha[v][jp_v][dp_v] = FLogsum(Lalpha[v][jp_v][dp_v], Lalpha[y][jp_y][dp_y] + tsc_v[yoffset]);
3803 }
3804 }
3805 } /* end of for (yvalid_idx = 0... loop */
3806 Lalpha[v][jp_v][dp_v] = ESL_MAX(Lalpha[v][jp_v][dp_v], IMPOSSIBLE);
3807 }
3808 }
3809 }
3810 } /* end of if(! StateIsDetached(cm, v)) */
3811 }
3812 else if(cm->sttype[v] == MP_st) {
3813 /* MP states cannot self transit, this means that all cells in
3814 * alpha[v] are independent of each other, only depending on
3815 * alpha[y] for previously calc'ed y. We can do the for loops
3816 * in any nesting order, this implementation does what I think
3817 * is most efficient: for y { for j { for d { } } }
3818 */
3819 for (y = cm->cfirst[v]; y < (cm->cfirst[v] + cm->cnum[v]); y++) {
3820 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
3821 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
3822 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
3823 yoffset = y - cm->cfirst[v];
3824 tsc = tsc_v[yoffset];
3825
3826 /* The first MP_st 'for (jp_v...' loop is for J and R matrices which use the same set of j values */
3827 /* j must satisfy:
3828 * j >= jmin[v]
3829 * j >= jmin[y]+sdr (follows from (j-sdr >= jmin[y]))
3830 * j <= jmax[v]
3831 * j <= jmax[y]+sdr (follows from (j-sdr <= jmax[y]))
3832 * this reduces to two ESL_MAX calls
3833 */
3834 jn = ESL_MAX(jmin[v], jmin[y]+sdr);
3835 jx = ESL_MIN(jmax[v], jmax[y]+sdr);
3836 jpn = jn - jmin[v];
3837 jpx = jx - jmin[v];
3838 jp_y_sdr = jn - jmin[y] - sdr;
3839 /* for Lalpha, we use 'jp_y=j-min[y]' instead of 'jp_y_sdr=j-jmin[y]-sdr' */
3840
3841 if((do_J_v && do_J_y) || (do_R_v && (do_J_y || do_R_y))) {
3842 for (jp_v = jpn; jp_v <= jpx; jp_v++, jp_y_sdr++, jp_y++) {
3843 ESL_DASSERT1((jp_v >= 0 && jp_v <= (jmax[v]-jmin[v])));
3844 ESL_DASSERT1((jp_y_sdr >= 0 && jp_y_sdr <= (jmax[y]-jmin[y])));
3845
3846 if(do_J_v && do_J_y) {
3847 /* J matrix: */
3848 /* d must satisfy:
3849 * d >= hdmin[v][jp_v]
3850 * d >= hdmin[y][jp_y_sdr]+sd (follows from (d-sd >= hdmin[y][jp_y_sdr]))
3851 * d <= hdmax[v][jp_v]
3852 * d <= hdmax[y][jp_y_sdr]+sd (follows from (d-sd <= hdmax[y][jp_y_sdr]))
3853 * this reduces to two ESL_MAX calls
3854 */
3855 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y_sdr] + sd);
3856 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y_sdr] + sd);
3857 dpn = dn - hdmin[v][jp_v];
3858 dpx = dx - hdmin[v][jp_v];
3859 dp_y_sd = dn - hdmin[y][jp_y_sdr] - sd;
3860
3861 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y_sd++) {
3862 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
3863 ESL_DASSERT1((dp_y_sd >= 0 && dp_y_sd <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
3864 Jalpha[v][jp_v][dp_v] = FLogsum(Jalpha[v][jp_v][dp_v], Jalpha[y][jp_y_sdr][dp_y_sd] + tsc);
3865 }
3866 }
3867
3868 if(do_R_v && (do_R_y || do_J_y)) {
3869 /* R matrix: */
3870 /* d must satisfy:
3871 * d >= hdmin[v][jp_v]
3872 * d >= hdmin[y][jp_y_sd]+sd (follows from (d-sd >= hdmin[y][jp_y_sd]))
3873 * d <= hdmax[v][jp_v]
3874 * d <= hdmax[y][jp_y_sd]+sd (follows from (d-sd <= hdmax[y][jp_y_sd]))
3875 * this reduces to two ESL_MAX calls
3876 */
3877 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y_sdr] + sdr);
3878 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y_sdr] + sdr);
3879 dpn = dn - hdmin[v][jp_v];
3880 dpx = dx - hdmin[v][jp_v];
3881 dp_y_sdr = dn - hdmin[y][jp_y_sdr] - sdr;
3882 /* for {L,R}alpha, we use 'dp_y_sdr' instead of 'dy_y_sd' */
3883
3884 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y_sdr++) {
3885 /* we use 'dp_y_sdr' here, not 'dp_y_sd' (which we used in the corresponding loop for J above) */
3886 ESL_DASSERT1((dp_y_sdr >= 0 && dp_y_sdr <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
3887 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);
3888 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);
3889 }
3890 }
3891 }
3892 }
3893
3894 if(do_L_v && (do_L_y || do_J_y)) {
3895 /* The second MP_st 'for (jp_v...' loop is for L matrix, which uses a different set of j values from J and R */
3896 /* j must satisfy:
3897 * j >= jmin[v]
3898 * j >= jmin[y] (follows from (j >= jmin[y]))
3899 * j <= jmax[v]
3900 * j <= jmax[y] (follows from (j <= jmax[y]))
3901 * this reduces to two ESL_MAX calls
3902 */
3903 jn = ESL_MAX(jmin[v], jmin[y]);
3904 jx = ESL_MIN(jmax[v], jmax[y]);
3905 jpn = jn - jmin[v];
3906 jpx = jx - jmin[v];
3907 jp_y = jn - jmin[y];
3908 /* for Lalpha, we use 'jp_y=j-min[y]' instead of 'jp_y_sdr=j-jmin[y]-sdr' */
3909
3910 for (jp_v = jpn; jp_v <= jpx; jp_v++, jp_y++) {
3911 ESL_DASSERT1((jp_v >= 0 && jp_v <= (jmax[v]-jmin[v])));
3912
3913 /* d must satisfy:
3914 * d >= hdmin[v][jp_v]
3915 * d >= hdmin[y][jp_y_sd]+sd (follows from (d-sd >= hdmin[y][jp_y_sd]))
3916 * d <= hdmax[v][jp_v]
3917 * d <= hdmax[y][jp_y_sd]+sd (follows from (d-sd <= hdmax[y][jp_y_sd]))
3918 * this reduces to two ESL_MAX calls
3919 */
3920 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y] + sdr);
3921 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y] + sdr);
3922 dpn = dn - hdmin[v][jp_v];
3923 dpx = dx - hdmin[v][jp_v];
3924 dp_y_sdl = dn - hdmin[y][jp_y] - sdl;
3925 /* for Lalpha, we use 'dp_y_sdl' instead of 'dy_y_sd' */
3926
3927 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y_sdl++) {
3928 /* we use 'dp_y_sdl' here, not 'dp_y_sd' (which we used in the corresponding loop for J above) */
3929 ESL_DASSERT1((dp_y_sdl >= 0 && dp_y_sdl <= (hdmax[y][jp_y] - hdmin[y][jp_y])));
3930 if(do_J_y) Lalpha[v][jp_v][dp_v] = FLogsum(Lalpha[v][jp_v][dp_v], Jalpha[y][jp_y][dp_y_sdl] + tsc);
3931 if(do_L_y) Lalpha[v][jp_v][dp_v] = FLogsum(Lalpha[v][jp_v][dp_v], Lalpha[y][jp_y][dp_y_sdl] + tsc);
3932 }
3933 }
3934 }
3935 }
3936 /* add in emission score */
3937 for (j = jmin[v]; j <= jmax[v]; j++) {
3938 jp_v = j - jmin[v];
3939 i = j - hdmin[v][jp_v] + 1;
3940 for (d = hdmin[v][jp_v], dp_v = 0; d <= hdmax[v][jp_v]; d++, dp_v++)
3941 {
3942 if(d >= 2) {
3943 if(do_J_v) Jalpha[v][jp_v][dp_v] += esc_v[dsq[i]*cm->abc->Kp+dsq[j]];
3944 if(do_L_v) Lalpha[v][jp_v][dp_v] += lmesc_v[dsq[i]];
3945 if(do_R_v) Ralpha[v][jp_v][dp_v] += rmesc_v[dsq[j]];
3946 }
3947 else {
3948 if(do_J_v) Jalpha[v][jp_v][dp_v] = IMPOSSIBLE;
3949 if(do_L_v) Lalpha[v][jp_v][dp_v] = lmesc_v[dsq[i]];
3950 if(do_R_v) Ralpha[v][jp_v][dp_v] = rmesc_v[dsq[j]];
3951 }
3952 i--;
3953 }
3954 }
3955 /* ensure all cells are >= IMPOSSIBLE */
3956 for (j = jmin[v]; j <= jmax[v]; j++) {
3957 jp_v = j - jmin[v];
3958 for (dp_v = 0; dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v]); dp_v++) {
3959 if(do_J_v) Jalpha[v][jp_v][dp_v] = ESL_MAX(Jalpha[v][jp_v][dp_v], IMPOSSIBLE);
3960 if(do_L_v) Lalpha[v][jp_v][dp_v] = ESL_MAX(Lalpha[v][jp_v][dp_v], IMPOSSIBLE);
3961 if(do_R_v) Ralpha[v][jp_v][dp_v] = ESL_MAX(Ralpha[v][jp_v][dp_v], IMPOSSIBLE);
3962 }
3963 }
3964 }
3965 else if(cm->sttype[v] != B_st) { /* entered if state v is D or S (! E && ! B && ! ML && ! IL && ! MR && ! IR) */
3966 /* D, S states cannot self transit, this means that all cells in
3967 * alpha[v] are independent of each other, only depending on
3968 * alpha[y] for previously calc'ed y. We can do the for loops
3969 * in any nesting order, this implementation does what I think
3970 * is most efficient: for y { for j { for d { } } }
3971 */
3972 for (y = cm->cfirst[v]; y < (cm->cfirst[v] + cm->cnum[v]); y++) {
3973 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
3974 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
3975 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
3976 yoffset = y - cm->cfirst[v];
3977 tsc = tsc_v[yoffset];
3978
3979 if((do_J_v && do_J_y) || (do_L_v && do_L_y) || (do_R_v && do_R_y)) {
3980 /* j must satisfy:
3981 * j >= jmin[v]
3982 * j >= jmin[y]+sdr (follows from (j-sdr >= jmin[y]))
3983 * j <= jmax[v]
3984 * j <= jmax[y]+sdr (follows from (j-sdr <= jmax[y]))
3985 * this reduces to two ESL_MAX calls
3986 */
3987 jn = ESL_MAX(jmin[v], jmin[y]+sdr);
3988 jx = ESL_MIN(jmax[v], jmax[y]+sdr);
3989 jpn = jn - jmin[v];
3990 jpx = jx - jmin[v];
3991 jp_y_sdr = jn - jmin[y] - sdr;
3992
3993 for (jp_v = jpn; jp_v <= jpx; jp_v++, jp_y_sdr++) {
3994 ESL_DASSERT1((jp_v >= 0 && jp_v <= (jmax[v]-jmin[v])));
3995 ESL_DASSERT1((jp_y_sdr >= 0 && jp_y_sdr <= (jmax[y]-jmin[y])));
3996
3997 /* d must satisfy:
3998 * d >= hdmin[v][jp_v]
3999 * d >= hdmin[y][jp_y_sdr]+sd (follows from (d-sd >= hdmin[y][jp_y_sdr]))
4000 * d <= hdmax[v][jp_v]
4001 * d <= hdmax[y][jp_y_sdr]+sd (follows from (d-sd <= hdmax[y][jp_y_sdr]))
4002 * this reduces to two ESL_MAX calls
4003 */
4004 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y_sdr] + sd);
4005 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y_sdr] + sd);
4006 dpn = dn - hdmin[v][jp_v];
4007 dpx = dx - hdmin[v][jp_v];
4008 dp_y_sd = dn - hdmin[y][jp_y_sdr] - sd;
4009
4010 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y_sd++) {
4011 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
4012 ESL_DASSERT1((dp_y_sd >= 0 && dp_y_sd <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
4013 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);
4014 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);
4015 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);
4016
4017 /* an easy to overlook case: if d == 0, set L and R values to IMPOSSIBLE */
4018 if(dp_v == dpn && dn == 0) { /* d is 0 */
4019 if(do_L_v) Lalpha[v][jp_v][dp_v] = IMPOSSIBLE;
4020 if(do_R_v) Ralpha[v][jp_v][dp_v] = IMPOSSIBLE;
4021 }
4022 }
4023 }
4024 }
4025 }
4026 /* no emission score to add */
4027 }
4028 else { /* B_st */
4029 y = cm->cfirst[v]; /* left subtree */
4030 z = cm->cnum[v]; /* right subtree */
4031
4032 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
4033 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
4034 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
4035
4036 do_J_z = cp9b->Jvalid[z] ? TRUE : FALSE;
4037 do_L_z = cp9b->Lvalid[z] && fill_L ? TRUE : FALSE;
4038 do_R_z = cp9b->Rvalid[z] && fill_R ? TRUE : FALSE;
4039
4040 /* Any valid j must be within both state v and state z's j band
4041 * I think jmin[v] <= jmin[z] is guaranteed by the way bands are
4042 * constructed, but we'll check anyway.
4043 */
4044 jn = (jmin[v] > jmin[z]) ? jmin[v] : jmin[z];
4045 jx = (jmax[v] < jmax[z]) ? jmax[v] : jmax[z];
4046 /* the main j loop */
4047 for (j = jn; j <= jx; j++) {
4048 jp_v = j - jmin[v];
4049 jp_y = j - jmin[y];
4050 jp_z = j - jmin[z];
4051 kn = ((j-jmax[y]) > (hdmin[z][jp_z])) ? (j-jmax[y]) : hdmin[z][jp_z];
4052 kn = ESL_MAX(kn, 0); /* kn must be non-negative, added with fix to bug i36 */
4053 /* kn satisfies inequalities (1) and (3) (listed below)*/
4054 kx = ( jp_y < (hdmax[z][jp_z])) ? jp_y : hdmax[z][jp_z];
4055 /* kn satisfies inequalities (2) and (4) (listed below)*/
4056 i = j - hdmin[v][jp_v] + 1;
4057 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++, i--) {
4058 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha w/mem eff bands */
4059
4060 /* Find the first k value that implies a valid cell in the {J,L,R} matrix y and z decks.
4061 * This k must satisfy the following 6 inequalities (some may be redundant):
4062 * (1) k >= j-jmax[y];
4063 * (2) k <= j-jmin[y];
4064 * 1 and 2 guarantee (j-k) is within state y's j band
4065 *
4066 * (3) k >= hdmin[z][j-jmin[z]];
4067 * (4) k <= hdmax[z][j-jmin[z]];
4068 * 3 and 4 guarantee k is within z's j=(j), d band
4069 *
4070 * (5) k >= d-hdmax[y][j-jmin[y]-k];
4071 * (6) k <= d-hdmin[y][j-jmin[y]-k];
4072 * 5 and 6 guarantee (d-k) is within state y's j=(j-k) d band
4073 *
4074 * kn and kx were set above (outside (for (dp_v...) loop) that
4075 * satisfy 1-4 (b/c 1-4 are d-independent and k-independent)
4076 * RHS of inequalities 5 and 6 are dependent on k, so we check
4077 * for these within the next for loop.
4078 *
4079 * To update a cell in the T matrix with a sum of an R matrix value for y
4080 * and a L matrix value for z, there are 2 additional inequalities to satisfy:
4081 * (7) k != 0
4082 * (8) k != d
4083 * We ensure 7 and 8 in the loop below.
4084 */
4085 for(k = kn; k <= kx; k++) {
4086 if((k >= d - hdmax[y][jp_y-k]) && k <= d - hdmin[y][jp_y-k]) {
4087 /* for current k, all 6 inequalities have been satisified
4088 * so we know the cells corresponding to the platonic
4089 * matrix cells alpha[v][j][d], alpha[y][j-k][d-k], and
4090 * alpha[z][j][k] are all within the bands. These
4091 * cells correspond to alpha[v][jp_v][dp_v],
4092 * alpha[y][jp_y-k][d-hdmin[jp_y-k]-k],
4093 * and alpha[z][jp_z][k-hdmin[jp_z]];
4094 */
4095 kp_z = k-hdmin[z][jp_z];
4096 dp_y = d-hdmin[y][jp_y-k];
4097 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]);
4098 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]);
4099 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]);
4100 if((k != 0) && (k != d)) {
4101 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]);
4102 }
4103 }
4104 }
4105 }
4106 }
4107
4108 /* two additional special cases in trCYK (these are not in standard CYK).
4109 * we do these in their own for(j.. { for(d.. { } } loops b/c one
4110 * is independent of z, the other of y, unlike the above loop which is dependent
4111 * on both.
4112 */
4113 if(do_L_v && (do_J_y || do_L_y)) {
4114 jn = (jmin[v] > jmin[y]) ? jmin[v] : jmin[y];
4115 jx = (jmax[v] < jmax[y]) ? jmax[v] : jmax[y];
4116 for (j = jn; j <= jx; j++) {
4117 jp_v = j - jmin[v];
4118 jp_y = j - jmin[y];
4119 ESL_DASSERT1((j >= jmin[v] && j <= jmax[v]));
4120 ESL_DASSERT1((j >= jmin[y] && j <= jmax[y]));
4121 dn = (hdmin[v][jp_v] > hdmin[y][jp_y]) ? hdmin[v][jp_v] : hdmin[y][jp_y];
4122 dx = (hdmax[v][jp_v] < hdmax[y][jp_y]) ? hdmax[v][jp_v] : hdmax[y][jp_y];
4123 for(d = dn; d <= dx; d++) {
4124 dp_v = d - hdmin[v][jp_v];
4125 dp_y = d - hdmin[y][jp_y];
4126 ESL_DASSERT1((d >= hdmin[v][jp_v] && d <= hdmax[v][jp_v]));
4127 ESL_DASSERT1((d >= hdmin[y][jp_y] && d <= hdmax[y][jp_y]));
4128 if(do_J_y) Lalpha[v][jp_v][dp_v] = FLogsum(Lalpha[v][jp_v][dp_v], Jalpha[y][jp_y][dp_y]);
4129 if(do_L_y) Lalpha[v][jp_v][dp_v] = FLogsum(Lalpha[v][jp_v][dp_v], Lalpha[y][jp_y][dp_y]);
4130 }
4131 }
4132 }
4133 if(do_R_v && (do_J_z || do_R_z)) {
4134 jn = (jmin[v] > jmin[z]) ? jmin[v] : jmin[z];
4135 jx = (jmax[v] < jmax[z]) ? jmax[v] : jmax[z];
4136 for (j = jn; j <= jx; j++) {
4137 jp_v = j - jmin[v];
4138 jp_z = j - jmin[z];
4139 ESL_DASSERT1((j >= jmin[v] && j <= jmax[v]));
4140 ESL_DASSERT1((j >= jmin[z] && j <= jmax[z]));
4141 dn = (hdmin[v][jp_v] > hdmin[z][jp_z]) ? hdmin[v][jp_v] : hdmin[z][jp_z];
4142 dx = (hdmax[v][jp_v] < hdmax[z][jp_z]) ? hdmax[v][jp_v] : hdmax[z][jp_z];
4143 for(d = dn; d <= dx; d++) {
4144 dp_v = d - hdmin[v][jp_v];
4145 dp_z = d - hdmin[z][jp_z];
4146 ESL_DASSERT1((d >= hdmin[v][jp_v] && d <= hdmax[v][jp_v]));
4147 ESL_DASSERT1((d >= hdmin[z][jp_z] && d <= hdmax[z][jp_z]));
4148 if(do_J_z) Ralpha[v][jp_v][dp_v] = FLogsum(Ralpha[v][jp_v][dp_v], Jalpha[z][jp_z][dp_z]);
4149 if(do_R_z) Ralpha[v][jp_v][dp_v] = FLogsum(Ralpha[v][jp_v][dp_v], Ralpha[z][jp_z][dp_z]);
4150 }
4151 }
4152 }
4153 } /* end of B_st recursion */
4154
4155 /* Now handle from ROOT_S, state 0. So far we haven't touched
4156 * the {J,L,R,T}alpha[0] decks at all since initialization and here
4157 * we'll only update at most 1 cell in each, the one pertaining
4158 * to a full alignment [0][L][L].
4159 *
4160 * In truncated alignment the only way out of ROOT_S in local or
4161 * global mode is via a 'truncated begin' with a score (penalty)
4162 * from cm->trp into any emitting state. The penalty was
4163 * calculated in cm_tr_penalties_Create() and differs depending on
4164 * whether we are in local or global mode and the value of
4165 * 'pty_idx' which was passed in.
4166 */
4167 if(L >= jmin[v] && L <= jmax[v]) {
4168 jp_v = L - jmin[v];
4169 Lp = L - hdmin[v][jp_v];
4170 if(L >= hdmin[v][jp_v] && L <= hdmax[v][jp_v]) {
4171 /* If we get here alpha[v][jp_v][Lp] and alpha[0][jp_0][Lp0]
4172 * are valid cells in the banded alpha matrix, corresponding to
4173 * alpha[v][L][L] and alpha[0][L][L] in the platonic matrix.
4174 * (We've already made sure alpha[0][jp_0][Lp_0] was valid
4175 * at the beginning of the function.)
4176 */
4177 trpenalty = (cm->flags & CMH_LOCAL_BEGIN) ? cm->trp->l_ptyAA[pty_idx][v] : cm->trp->g_ptyAA[pty_idx][v];
4178 if(NOT_IMPOSSIBLE(trpenalty)) {
4179 /* include full length hits in J matrix */
4180 if(do_J_v && cp9b->Jvalid[0]) {
4181 Jalpha[0][jp_0][Lp_0] = FLogsum(Jalpha[0][jp_0][Lp_0], Jalpha[v][jp_v][Lp] + trpenalty);
4182 }
4183 /* include full length hits in L matrix */
4184 if(do_L_v && cp9b->Lvalid[0]) {
4185 Lalpha[0][jp_0][Lp_0] = FLogsum(Lalpha[0][jp_0][Lp_0], Lalpha[v][jp_v][Lp] + trpenalty);
4186 }
4187 /* include full length hits in R matrix */
4188 if(do_R_v && cp9b->Rvalid[0]) {
4189 Ralpha[0][jp_0][Lp_0] = FLogsum(Ralpha[0][jp_0][Lp_0], Ralpha[v][jp_v][Lp] + trpenalty);
4190 }
4191 /* include full length hits in T matrix */
4192 if(do_T_v && cp9b->Tvalid[0]) {
4193 Talpha[0][jp_0][Lp_0] = FLogsum(Talpha[0][jp_0][Lp_0], Talpha[v][jp_v][Lp] + trpenalty);
4194 }
4195 }
4196 }
4197 }
4198 } /* end of for (v = cm->M-1; v > 0; v--) */
4199
4200 /* determine mode of optimal alignment, if it was preset then use that */
4201 if(preset_mode == TRMODE_J) {
4202 sc = Jalpha[0][jp_0][Lp_0];
4203 mode = TRMODE_J;
4204 }
4205 else if(preset_mode == TRMODE_L) {
4206 sc = Lalpha[0][jp_0][Lp_0];
4207 mode = TRMODE_L;
4208 }
4209 else if(preset_mode == TRMODE_R) {
4210 sc = Ralpha[0][jp_0][Lp_0];
4211 mode = TRMODE_R;
4212 }
4213 else if(preset_mode == TRMODE_T) {
4214 sc = Talpha[0][jp_0][Lp_0];
4215 mode = TRMODE_T;
4216 }
4217 else { /* preset_mode was unknown, max score determines mode */
4218 sc = IMPOSSIBLE;
4219 mode = TRMODE_UNKNOWN;
4220
4221 if (cp9b->Jvalid[0] && Jalpha[0][jp_0][Lp_0] > sc) {
4222 sc = Jalpha[0][jp_0][Lp_0];
4223 mode = TRMODE_J;
4224 }
4225 if (fill_L && cp9b->Lvalid[0] && Lalpha[0][jp_0][Lp_0] > sc) {
4226 sc = Lalpha[0][jp_0][Lp_0];
4227 mode = TRMODE_L;
4228 }
4229 if (fill_R && cp9b->Rvalid[0] && Ralpha[0][jp_0][Lp_0] > sc) {
4230 sc = Ralpha[0][jp_0][Lp_0];
4231 mode = TRMODE_R;
4232 }
4233 if (fill_T && cp9b->Tvalid[0] && Talpha[0][jp_0][Lp_0] > sc) {
4234 sc = Talpha[0][jp_0][Lp_0];
4235 mode = TRMODE_T;
4236 }
4237 }
4238
4239
4240 #if eslDEBUGLEVEL >= 2
4241 /* Uncomment to dump matrix to file. Careful...this could be very large. */
4242 /* FILE *fp1; fp1 = fopen("tmp.tru_ihbmx", "w"); cm_tr_hb_mx_Dump(fp1, mx, mode, TRUE); fclose(fp1); */
4243 #endif
4244
4245 if(ret_mode != NULL) *ret_mode = mode;
4246 if(ret_sc != NULL) *ret_sc = sc;
4247
4248 free(el_scA);
4249 free(yvalidA);
4250
4251 if(ret_sc != NULL) *ret_sc = sc;
4252
4253 ESL_DPRINTF1(("#DEBUG: cm_TrInsideAlignHB() return sc: %f\n", sc));
4254
4255 if(*ret_mode == TRMODE_UNKNOWN) ESL_FAIL(eslEAMBIGUOUS, errbuf, "cm_TrInsideAlignHB() no valid parsetree found");
4256
4257 return eslOK;
4258
4259 ERROR:
4260 ESL_FAIL(status, errbuf, "Memory allocation error.\n");
4261 }
4262
4263 /* Function: cm_TrOptAccAlign()
4264 * based on cm_OptAccAlign()
4265 *
4266 * Date: EPN, Wed Sep 28 13:16:12 2011
4267 *
4268 * Purpose: Run the truncated version of the Holmes/Durbin optimal
4269 * accuracy algorithm on a full target sequence 1..L, given
4270 * a pre-filled posterior matrix. Uses float log odds
4271 * scores. Non-banded version. See cm_OptAccAlignHB() for
4272 * HMM banded version.
4273 *
4274 * A CM_TR_EMIT_MX matrix <emit_mx> must be passed in,
4275 * filled by cm_TrEmitterPosterior(), with values:
4276 *
4277 * {J,L}l_pp[v][i]: log of the posterior probability that
4278 * state v emitted residue i leftwise either at (if a match
4279 * state) or *after* (if an insert state) the left consensus
4280 * position modeled by state v's node, either in Joint
4281 * marginal or Left marginal mode {J or L}.
4282 *
4283 * {J,R}r_pp[v][i]: log of the posterior probability that
4284 * state v emitted residue i rightwise either at (if a match
4285 * state) or *before* (if an insert state) the right
4286 * consensus position modeled by state v's node, either in
4287 * Joint marginal or Right marginal mode {J or R}.
4288 *
4289 * {J,L}l_pp[v] is NULL for states that do not emit leftwise
4290 * {J,r}r_pp[v] is NULL for states that do not emit rightwise
4291 *
4292 * Additionally, a CM_TR_MX DP matrix <mx> and
4293 * CM_TR_SHADOW_MX <shmx> must be passed in. <shmx> will be
4294 * expanded and filled here with traceback pointers to allow
4295 * the optimally accurate parsetree to be recovered in
4296 * cm_alignT() and <mx> will be expanded and filled with the
4297 * optimal accuracy scores, where:
4298 *
4299 * mx->{J,L,R,T}dp[v][j][d]: log of the sum of the posterior
4300 * probabilities of emitting residues i..j in the subtree
4301 * rooted at v given that v is in marginal mode J,L,R, or T.
4302 *
4303 * The optimally accurate parsetree in marginal mode
4304 * <preset_mode>, i.e. the parsetree that maximizes the sum
4305 * of the posterior probabilities of all 1..L emitted
4306 * residues, will be found. Its score is returned in
4307 * <ret_sc>. The optimal truncated entry state is returned in
4308 * <ret_b>, regardless of if we're in global or local mode
4309 * because all truncated alignments must use a truncated
4310 * begin.
4311 *
4312 * Args: cm - the model
4313 * errbuf - char buffer for reporting errors
4314 * dsq - the digitaized sequence [1..L]
4315 * L - length of the dsq
4316 * size_limit - max number of Mb for DP matrix, if matrix is bigger return eslERANGE
4317 * preset_mode - the pre-determined alignment mode, must not be TRMODE_UNKNOWN
4318 * pass_idx - pipeline pass index, indicates what truncation penalty to use
4319 * mx - the DP matrix to fill in
4320 * shmx - the shadow matrix to fill in
4321 * emit_mx - pre-filled emit matrix
4322 * ret_b - optimal entry point (state) for the alignment
4323 * ret_pp - RETURN: average posterior probability of aligned residues
4324 * in the optimally accurate parsetree
4325 *
4326 * Returns: <eslOK> on success.
4327 * Throws: <eslERANGE> if required CM_TR_HB_MX size exceeds <size_limit>
4328 * If !eslOK: alignment has been aborted, ret_* variables are not valid
4329 */
4330 int
4331 cm_TrOptAccAlign(CM_t *cm, char *errbuf, ESL_DSQ *dsq, int L, float size_limit, char preset_mode, int pass_idx,
4332 CM_TR_MX *mx, CM_TR_SHADOW_MX *shmx, CM_TR_EMIT_MX *emit_mx, int *ret_b, float *ret_pp)
4333 {
4334 int status; /* easel status code */
4335 int v,y,z; /* indices for states */
4336 int j,d,i,k; /* indices in sequence dimensions */
4337 float sc; /* temporary log odds score */
4338 float pp; /* average posterior probability of all emitted residues */
4339 int yoffset; /* y=base+offset -- counter in child states that v can transit to */
4340 int sd; /* StateDelta(cm->sttype[v]) */
4341 int sdl; /* StateLeftDelta(cm->sttype[v] */
4342 int sdr; /* StateRightDelta(cm->sttype[v] */
4343 int j_sdr; /* j - sdr */
4344 int d_sd; /* d - sd */
4345 int d_sdl; /* d - sdl */
4346 int d_sdr; /* d - sdr */
4347 int have_el; /* TRUE if local ends are on in the CM, otherwise FALSE */
4348 int64_t c; /* 64-bit int counter */
4349
4350 /* other variables used in truncated version, but not standard version (not in cm_OptAccAlign()) */
4351 int b = 0; /* best truncated entry state */
4352 int Lyoffset0; /* first yoffset to use for updating L matrix in IR/MR states, 1 if IR, 0 if MR */
4353 int Ryoffset0; /* first yoffset to use for updating R matrix in IL/ML states, 1 if IL, 0 if ML */
4354 int fill_L, fill_R, fill_T; /* must we fill in the L, R, and T matrices? */
4355 int pty_idx; /* index for truncation penalty, determined by pass_idx */
4356 float trpenalty; /* truncation penalty, differs based on pty_idx and if we're local or global */
4357 int nins_v; /* number of insert states reachable from current state */
4358 int yctr; /* used for special for(y) loops in TrOptAcc (see code) */
4359
4360 /* the DP matrices */
4361 float ***Jalpha = mx->Jdp; /* pointer to the Jalpha DP matrix */
4362 float ***Lalpha = mx->Ldp; /* pointer to the Lalpha DP matrix */
4363 float ***Ralpha = mx->Rdp; /* pointer to the Ralpha DP matrix */
4364 float ***Talpha = mx->Tdp; /* pointer to the Talpha DP matrix */
4365
4366 char ***Jyshadow = shmx->Jyshadow; /* pointer to the Jyshadow matrix */
4367 char ***Lyshadow = shmx->Lyshadow; /* pointer to the Lyshadow matrix */
4368 char ***Ryshadow = shmx->Ryshadow; /* pointer to the Ryshadow matrix */
4369 int ***Jkshadow = shmx->Jkshadow; /* pointer to the Jkshadow matrix */
4370 int ***Lkshadow = shmx->Lkshadow; /* pointer to the Lkshadow matrix */
4371 int ***Rkshadow = shmx->Rkshadow; /* pointer to the Rkshadow matrix */
4372 int ***Tkshadow = shmx->Tkshadow; /* pointer to the Tkshadow matrix */
4373 char ***Lkmode = shmx->Lkmode; /* pointer to the Lkmode matrix */
4374 char ***Rkmode = shmx->Rkmode; /* pointer to the Rkmode matrix */
4375
4376 float **Jl_pp = emit_mx->Jl_pp; /* pointer to the prefilled posterior values for left emitters in Joint mode */
4377 float **Ll_pp = emit_mx->Ll_pp; /* pointer to the prefilled posterior values for left emitters in Left mode */
4378 float **Jr_pp = emit_mx->Jr_pp; /* pointer to the prefilled posterior values for right emitters in Joint mode */
4379 float **Rr_pp = emit_mx->Rr_pp; /* pointer to the prefilled posterior values for right emitters in Right mode */
4380
4381 /* Determine which matrices we need to fill in, based on <preset_mode> */
4382 if (preset_mode != TRMODE_J && preset_mode != TRMODE_L && preset_mode != TRMODE_R && preset_mode != TRMODE_T) ESL_FAIL(eslEINVAL, errbuf, "cm_TrCYKOutsideAlign(): preset_mode is not J, L, R, or T");
4383 if((status = cm_TrFillFromMode(preset_mode, &fill_L, &fill_R, &fill_T)) != eslOK) ESL_FAIL(status, errbuf, "cm_TrOptAccAlign(), bogus mode: %d", preset_mode);
4384
4385 /* Determine the truncation penalty index, from the pass_idx */
4386 if((pty_idx = cm_tr_penalties_IdxForPass(pass_idx)) == -1) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_TrOptAccAlign(), unexpected pass idx: %d", pass_idx);
4387
4388 /* we need an emitmap in this function */
4389 if(cm->emap == NULL) ESL_FAIL(eslEINVAL, errbuf, "cm_TrOptAccAlign(), emit map is NULL");
4390
4391 /* Allocations and initializations */
4392 /* grow the matrices based on the current sequence and bands */
4393 if((status = cm_tr_mx_GrowTo (cm, mx, errbuf, L, size_limit)) != eslOK) return status;
4394 if((status = cm_tr_shadow_mx_GrowTo(cm, shmx, errbuf, L, size_limit)) != eslOK) return status;
4395
4396 /* initialize all cells of the matrix to IMPOSSIBLE */
4397 if( mx->Jncells_valid > 0) esl_vec_FSet(mx->Jdp_mem, mx->Jncells_valid, IMPOSSIBLE);
4398 if( mx->Lncells_valid > 0 && fill_L) esl_vec_FSet(mx->Ldp_mem, mx->Lncells_valid, IMPOSSIBLE);
4399 if( mx->Rncells_valid > 0 && fill_R) esl_vec_FSet(mx->Rdp_mem, mx->Rncells_valid, IMPOSSIBLE);
4400 if( mx->Tncells_valid > 0 && fill_T) esl_vec_FSet(mx->Tdp_mem, mx->Tncells_valid, IMPOSSIBLE);
4401 if(shmx->Jy_ncells_valid > 0) for(c = 0; c < shmx->Jy_ncells_valid; c++) shmx->Jyshadow_mem[c] = USED_EL;
4402 if(shmx->Ly_ncells_valid > 0 && fill_L) for(c = 0; c < shmx->Ly_ncells_valid; c++) shmx->Lyshadow_mem[c] = USED_EL;
4403 if(shmx->Ry_ncells_valid > 0 && fill_R) for(c = 0; c < shmx->Ry_ncells_valid; c++) shmx->Ryshadow_mem[c] = USED_EL;
4404 /* for B states, shadow matrix holds k, length of right fragment, this will almost certainly be overwritten */
4405 if(shmx->Jk_ncells_valid > 0) esl_vec_ISet(shmx->Jkshadow_mem, shmx->Jk_ncells_valid, 0);
4406 if(shmx->Lk_ncells_valid > 0 && fill_L) esl_vec_ISet(shmx->Lkshadow_mem, shmx->Lk_ncells_valid, 0);
4407 if(shmx->Rk_ncells_valid > 0 && fill_R) esl_vec_ISet(shmx->Rkshadow_mem, shmx->Rk_ncells_valid, 0);
4408 if(shmx->Tk_ncells_valid > 0 && fill_T) esl_vec_ISet(shmx->Tkshadow_mem, shmx->Tk_ncells_valid, 0);
4409 if(shmx->Lk_ncells_valid > 0 && fill_L) for(c = 0; c < shmx->Lk_ncells_valid; c++) shmx->Lkmode_mem[c] = TRMODE_J;
4410 if(shmx->Rk_ncells_valid > 0 && fill_R) for(c = 0; c < shmx->Rk_ncells_valid; c++) shmx->Rkmode_mem[c] = TRMODE_J;
4411
4412 /* a special optimal accuracy specific step, initialize Jyshadow intelligently for d == 0
4413 * (necessary b/c zero length parsetees have 0 emits and so always score IMPOSSIBLE)
4414 */
4415 if((status = cm_InitializeOptAccShadowDZero(cm, errbuf, Jyshadow, L)) != eslOK) return status;
4416
4417 /* start with the EL state */
4418 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
4419 if(have_el) {
4420 for (j = 0; j <= L; j++) {
4421 if(Jl_pp[cm->M] != NULL) Jalpha[cm->M][j][0] = Jl_pp[cm->M][0];
4422 if(Ll_pp[cm->M] != NULL && fill_L) Lalpha[cm->M][j][0] = Ll_pp[cm->M][0];
4423 if(Rr_pp[cm->M] != NULL && fill_R) Ralpha[cm->M][j][0] = Rr_pp[cm->M][0];
4424 if(Jl_pp[cm->M] != NULL) {
4425 i = j;
4426 for (d = 1; d <= j; d++) Jalpha[cm->M][j][d] = FLogsum(Jalpha[cm->M][j][d-1], Jl_pp[cm->M][i--]);
4427 }
4428 if(Ll_pp[cm->M] != NULL && fill_L) {
4429 i = j;
4430 for (d = 1; d <= j; d++) Lalpha[cm->M][j][d] = FLogsum(Lalpha[cm->M][j][d-1], Ll_pp[cm->M][i--]);
4431 }
4432 if(Rr_pp[cm->M] != NULL && fill_R) {
4433 i = j;
4434 for (d = 1; d <= j; d++) Ralpha[cm->M][j][d] = FLogsum(Ralpha[cm->M][j][d-1], Rr_pp[cm->M][i--]);
4435 }
4436 }
4437 }
4438
4439 /* Main recursion */
4440 for (v = cm->M-1; v > 0; v--) { /* almost done to ROOT_S, we handle that differently */
4441 sd = StateDelta(cm->sttype[v]);
4442 sdl = StateLeftDelta(cm->sttype[v]);
4443 sdr = StateRightDelta(cm->sttype[v]);
4444 nins_v = NumReachableInserts(cm->stid[v]);
4445
4446 /* re-initialize if we can do a local end from v */
4447 if(have_el && NOT_IMPOSSIBLE(cm->endsc[v])) {
4448 for (j = 0; j <= L; j++) {
4449 /* copy values from saved EL deck */
4450 for (d = sd; d <= j; d++) Jalpha[v][j][d] = Jalpha[cm->M][j-sdr][d-sd];
4451 for (d = sdl; d <= j; d++) Lalpha[v][j][d] = Lalpha[cm->M][j][d-sdl];
4452 for (d = sdr; d <= j; d++) Ralpha[v][j][d] = Ralpha[cm->M][j-sdr][d-sdr];
4453 }
4454 }
4455 /* note there's no E state update here, those cells all remain IMPOSSIBLE */
4456
4457 if(cm->sttype[v] == IL_st || cm->sttype[v] == ML_st) {
4458 /* update alpha[v][j][d] cells, for IL states, loop nesting order is:
4459 * for j { for d { for y { } } } because they can self transit, and a
4460 * alpha[v][j][d] cell must be complete (that is we must have looked at all children y)
4461 * before can start calc'ing for alpha[v][j][d+1]
4462 * We do ML states as well as IL states b/c they follow the same rules,
4463 * and we're not worried about efficiency here.
4464 */
4465
4466 /* In TrCYK: we need to treat R differently from and J and L
4467 * here, by doing separate 'for (yoffset...' loops for J and R
4468 * because we have to fully calculate Jalpha[v][j][d]) before we
4469 * can start to calculate Ralpha[v][j][d].
4470 */
4471
4472 if(! StateIsDetached(cm, v)) { /* if we're detached (unreachable), leave all {J,L,R}alpha values as they were initialized, as IMPOSSIBLE */
4473 Ryoffset0 = cm->sttype[v] == IL_st ? 1 : 0; /* don't allow IL self transits in R mode */
4474 for (j = sdr; j <= L; j++) {
4475 i = j-sd+1;
4476 j_sdr = j - sdr;
4477 for (d = sd; d <= j; d++, i--) {
4478 d_sd = d - sd;
4479 for (yctr = 0; yctr < cm->cnum[v]; yctr++) {
4480 yoffset = (yctr + nins_v) % cm->cnum[v]; /* special y ordering for TrOptAcc, consider consensus state first, not inserts */
4481 y = cm->cfirst[v] + yoffset;
4482 if ((sc = Jalpha[y][j_sdr][d_sd]) > Jalpha[v][j][d]) {
4483 Jalpha[v][j][d] = sc;
4484 Jyshadow[v][j][d] = yoffset + TRMODE_J_OFFSET;
4485 }
4486 if (fill_L && ((sc = Lalpha[y][j_sdr][d_sd]) > Lalpha[v][j][d])) {
4487 Lalpha[v][j][d] = sc;
4488 Lyshadow[v][j][d] = yoffset + TRMODE_L_OFFSET;
4489 }
4490 }
4491 Jalpha[v][j][d] = FLogsum(Jalpha[v][j][d], Jl_pp[v][i]);
4492 Jalpha[v][j][d] = ESL_MAX(Jalpha[v][j][d], IMPOSSIBLE);
4493 /* special case if local ends are off: explicitly disallow transitions to EL that require EL emissions
4494 * (we do allow an 'illegal' transition to EL in OptAcc but only if no EL emissions are req'd)
4495 */
4496 if((! have_el) && Jyshadow[v][j][d] == USED_EL && d > sd) {
4497 Jalpha[v][j][d] = IMPOSSIBLE;
4498 }
4499
4500 if(fill_L) {
4501 if(d >= 2) {
4502 Lalpha[v][j][d] = FLogsum(Lalpha[v][j][d], Ll_pp[v][i]);
4503 /* special case if local ends are off: explicitly disallow transitions to EL that require EL emissions *
4504 * (we do allow an 'illegal' transition to EL in OptAcc but only if no EL emissions are req'd)
4505 */
4506 if((! have_el) && Lyshadow[v][j][d] == USED_EL) {
4507 Lalpha[v][j][d] = IMPOSSIBLE;
4508 }
4509 }
4510 else {
4511 Lalpha[v][j][d] = Ll_pp[v][i]; /* actually I think this will give the same value as d >= 2 case above */
4512 Lyshadow[v][j][d] = USED_TRUNC_END;
4513 }
4514 Lalpha[v][j][d] = ESL_MAX(Lalpha[v][j][d], IMPOSSIBLE);
4515 }
4516
4517 /* handle R separately */
4518 if(fill_R) {
4519 /* note we use 'd', not 'd_sd' (which we used in the corresponding loop for J,L above) */
4520 for (yctr = Ryoffset0; yctr < cm->cnum[v]; yctr++) { /* using Ryoffset0 instead of 0 disallows IL self transits in R mode */
4521 yoffset = (yctr + nins_v - Ryoffset0) % cm->cnum[v]; /* special y ordering for TrOptAcc, consider consensus state first, not inserts */
4522 y = cm->cfirst[v] + yoffset;
4523 if ((sc = Jalpha[y][j_sdr][d]) > Ralpha[v][j][d]) {
4524 Ralpha[v][j][d] = sc;
4525 Ryshadow[v][j][d]= yoffset + TRMODE_J_OFFSET;
4526 }
4527 if ((sc = Ralpha[y][j_sdr][d]) > Ralpha[v][j][d]) {
4528 Ralpha[v][j][d] = sc;
4529 Ryshadow[v][j][d] = yoffset + TRMODE_R_OFFSET;
4530 }
4531 }
4532 /* no residue was emitted if we're in R mode */
4533 Ralpha[v][j][d] = ESL_MAX(Ralpha[v][j][d], IMPOSSIBLE);
4534 }
4535 }
4536 }
4537 } /* end of if(! StateIsDetached(cm,v )) */
4538 }
4539 else if(cm->sttype[v] == IR_st || cm->sttype[v] == MR_st) {
4540 /* update alpha[v][j][d] cells, for IR states, loop nesting order is:
4541 * for j { for d { for y { } } } because they can self transit, and a
4542 * alpha[v][j][d] cell must be complete (that is we must have looked at all children y)
4543 * before can start calc'ing for alpha[v][j][d+1].
4544 * We do MR states as well as IR states b/c they follow the same rules,
4545 * and we're not worried about efficiency here.
4546 */
4547
4548 /* In TrCYK: we need to treat L differently from and J and R
4549 * here, by doing separate 'for (yoffset...' loops for J and R
4550 * because we have to fully calculate Jalpha[v][j][d]) before we
4551 * can start to calculate Lalpha[v][j][d].
4552 */
4553
4554 if(! StateIsDetached(cm, v)) { /* if we're detached (unreachable), leave all {J,L,R}alpha values as they were initialized, as IMPOSSIBLE */
4555 Lyoffset0 = cm->sttype[v] == IR_st ? 1 : 0; /* don't allow IR self transits in L mode */
4556 for (j = sdr; j <= L; j++) {
4557 j_sdr = j - sdr;
4558 for (d = sd; d <= j; d++) {
4559 d_sd = d - sd;
4560 for (yctr = 0; yctr < cm->cnum[v]; yctr++) {
4561 yoffset = (yctr + nins_v) % cm->cnum[v]; /* special y ordering for TrOptAcc, consider consensus state first, not inserts */
4562 y = cm->cfirst[v] + yoffset;
4563 if ((sc = Jalpha[y][j_sdr][d_sd]) > Jalpha[v][j][d]) {
4564 Jalpha[v][j][d] = sc;
4565 Jyshadow[v][j][d] = yoffset + TRMODE_J_OFFSET;
4566 }
4567 if (fill_R && ((sc = Ralpha[y][j_sdr][d_sd]) > Ralpha[v][j][d])) {
4568 Ralpha[v][j][d] = sc;
4569 Ryshadow[v][j][d] = yoffset + TRMODE_R_OFFSET;
4570 }
4571 }
4572 Jalpha[v][j][d] = FLogsum(Jalpha[v][j][d], Jr_pp[v][j]);
4573 Jalpha[v][j][d] = ESL_MAX(Jalpha[v][j][d], IMPOSSIBLE);
4574 /* special case if local ends are off: explicitly disallow transitions to EL that require EL emissions
4575 * (we do allow an 'illegal' transition to EL in OptAcc but only if no EL emissions are req'd)
4576 */
4577 if((! have_el) && Jyshadow[v][j][d] == USED_EL && d > sd) {
4578 Jalpha[v][j][d] = IMPOSSIBLE;
4579 }
4580
4581 if(fill_R) {
4582 if(d >= 2) {
4583 Ralpha[v][j][d] = FLogsum(Ralpha[v][j][d], Rr_pp[v][j]);
4584 /* special case if local ends are off: explicitly disallow transitions to EL that require EL emissions
4585 * (we do allow an 'illegal' transition to EL in OptAcc but only if no EL emissions are req'd)
4586 */
4587 if((! have_el) && Ryshadow[v][j][d] == USED_EL) {
4588 Ralpha[v][j][d] = IMPOSSIBLE;
4589 }
4590 }
4591 else {
4592 Ralpha[v][j][d] = Rr_pp[v][j]; /* actually I think this will give the same value as d >= 2 case above */
4593 Ryshadow[v][j][d] = USED_TRUNC_END;
4594 }
4595 Ralpha[v][j][d] = ESL_MAX(Ralpha[v][j][d], IMPOSSIBLE);
4596 }
4597
4598 /* handle L separately */
4599 if(fill_L) {
4600 /* note we use 'j' and 'd', not 'j_sdr' and 'd_sd' (which we used in the corresponding loop for J,R above) */
4601 for (yctr = Lyoffset0; yctr < cm->cnum[v]; yctr++) { /* using Lyoffset0, instead of 0 disallows IR self transits in L mode */
4602 yoffset = (yctr + nins_v - Lyoffset0) % cm->cnum[v]; /* special y ordering for TrOptAcc, consider consensus state first, not inserts */
4603 y = cm->cfirst[v] + yoffset;
4604 if ((sc = Jalpha[y][j][d]) > Lalpha[v][j][d]) {
4605 Lalpha[v][j][d] = sc;
4606 Lyshadow[v][j][d] = yoffset + TRMODE_J_OFFSET;
4607 }
4608 if ((sc = Lalpha[y][j][d]) > Lalpha[v][j][d]) {
4609 Lalpha[v][j][d] = sc;
4610 Lyshadow[v][j][d] = yoffset + TRMODE_L_OFFSET;
4611 }
4612 }
4613 /* no residue was emitted if we're in R mode */
4614 Lalpha[v][j][d] = ESL_MAX(Lalpha[v][j][d], IMPOSSIBLE);
4615 }
4616 }
4617 }
4618 } /* end of if(! StateIsDetached(cm, v)) */
4619 }
4620 else if(cm->sttype[v] == MP_st) {
4621 /* MP states cannot self transit, this means that all cells in
4622 * alpha[v] are independent of each other, only depending on
4623 * alpha[y] for previously calc'ed y. We can do the for loops
4624 * in any nesting order, this implementation does what I think
4625 * is most efficient: for y { for j { for d { } } }
4626 */
4627 for (yctr = 0; yctr < cm->cnum[v]; yctr++) {
4628 yoffset = (yctr + nins_v) % cm->cnum[v]; /* special y ordering for TrOptAcc, consider consensus state first, not inserts */
4629 y = cm->cfirst[v] + yoffset;
4630
4631 for (j = sdr; j <= L; j++) {
4632 j_sdr = j - sdr;
4633 for (d = sd; d <= j; d++) { /* sd == 2 for MP state */
4634 d_sd = d-sd;
4635 if((sc = Jalpha[y][j_sdr][d_sd]) > Jalpha[v][j][d]) {
4636 Jalpha[v][j][d] = sc;
4637 Jyshadow[v][j][d] = yoffset + TRMODE_J_OFFSET;
4638 }
4639 }
4640 if(fill_L) {
4641 /* note we use 'j' and 'd_sdl' not 'j_sdr' for 'd_sd' for L, plus minimum d is sdl (1) */
4642 for (d = sdl; d <= j; d++) { /* sdl == 1 for MP state */
4643 d_sdl = d-sdl;
4644 if((sc = Jalpha[y][j][d_sdl]) > Lalpha[v][j][d]) {
4645 Lalpha[v][j][d] = sc;
4646 Lyshadow[v][j][d] = yoffset + TRMODE_J_OFFSET;
4647 }
4648 if((sc = Lalpha[y][j][d_sdl]) > Lalpha[v][j][d]) {
4649 Lalpha[v][j][d] = sc;
4650 Lyshadow[v][j][d] = yoffset + TRMODE_L_OFFSET;
4651 }
4652 }
4653 }
4654 if(fill_R) {
4655 /* note we use 'd_sdr' not 'd_sd' for R, plus minimum d is sdr (1) */
4656 for (d = sdr; d <= j; d++) { /* sdr == 1 for MP state */
4657 d_sdr = d - sdr;
4658 if((sc = Jalpha[y][j_sdr][d_sdr]) > Ralpha[v][j][d]) {
4659 Ralpha[v][j][d] = sc;
4660 Ryshadow[v][j][d] = yoffset + TRMODE_J_OFFSET;
4661 }
4662 if((sc = Ralpha[y][j_sdr][d_sdr]) > Ralpha[v][j][d]) {
4663 Ralpha[v][j][d] = sc;
4664 Ryshadow[v][j][d] = yoffset + TRMODE_R_OFFSET;
4665 }
4666 }
4667 }
4668 }
4669 }
4670 /* add in emission score */
4671 for (j = 0; j <= L; j++) {
4672 Jalpha[v][j][1] = IMPOSSIBLE;
4673 if(fill_L) {
4674 i = j-1+1;
4675 Lalpha[v][j][1] = Ll_pp[v][i];
4676 Lyshadow[v][j][1] = USED_TRUNC_END;
4677 }
4678 if(fill_R) {
4679 Ralpha[v][j][1] = Rr_pp[v][j];
4680 Ryshadow[v][j][1] = USED_TRUNC_END;
4681 }
4682 i = j-2+1;
4683 for (d = 2; d <= j; d++, i--) {
4684 Jalpha[v][j][d] = FLogsum(Jalpha[v][j][d], FLogsum(Jl_pp[v][i], Jr_pp[v][j]));
4685 }
4686 if(fill_L) {
4687 i = j-2+1;
4688 for (d = 2; d <= j; d++, i--) {
4689 Lalpha[v][j][d] = FLogsum(Lalpha[v][j][d], Ll_pp[v][i]);
4690 }
4691 }
4692 if(fill_R) {
4693 for (d = 2; d <= j; d++) {
4694 Ralpha[v][j][d] = FLogsum(Ralpha[v][j][d], Rr_pp[v][j]);
4695 }
4696 }
4697 }
4698 /* ensure all cells are >= IMPOSSIBLE */
4699 for (j = 0; j <= L; j++) {
4700 for (d = 1; d <= j; d++) Jalpha[v][j][d] = ESL_MAX(Jalpha[v][j][d], IMPOSSIBLE);
4701 if(fill_L) for (d = 1; d <= j; d++) Lalpha[v][j][d] = ESL_MAX(Lalpha[v][j][d], IMPOSSIBLE);
4702 if(fill_R) for (d = 1; d <= j; d++) Ralpha[v][j][d] = ESL_MAX(Ralpha[v][j][d], IMPOSSIBLE);
4703 }
4704 /* special case if local ends are off: explicitly disallow transitions to EL that require EL emissions
4705 * (we do allow an 'illegal' transition to EL in OptAcc but only if no EL emissions are req'd)
4706 */
4707 if(! have_el) {
4708 for (j = 0; j <= L; j++) {
4709 for (d = sd+1; d <= j; d++) {
4710 if(Jyshadow[v][j][d] == USED_EL) Jalpha[v][j][d] = IMPOSSIBLE;
4711 }
4712 if(fill_L) {
4713 for (d = sdl+1; d <= j; d++) {
4714 if(Lyshadow[v][j][d] == USED_EL) Lalpha[v][j][d] = IMPOSSIBLE;
4715 }
4716 }
4717 if(fill_R) {
4718 for (d = sdr+1; d <= j; d++) {
4719 if(Ryshadow[v][j][d] == USED_EL) Ralpha[v][j][d] = IMPOSSIBLE;
4720 }
4721 }
4722 }
4723 }
4724 }
4725 else if(cm->sttype[v] != B_st) { /* entered if state v is D or S */
4726 /* D, S states cannot self transit, this means that all cells in
4727 * alpha[v] are independent of each other, only depending on
4728 * alpha[y] for previously calc'ed y. We can do the for loops
4729 * in any nesting order, this implementation does what I think
4730 * is most efficient: for y { for j { for d { } } }
4731 */
4732 for (yctr = 0; yctr < cm->cnum[v]; yctr++) {
4733 yoffset = (yctr + nins_v) % cm->cnum[v]; /* special y ordering for TrOptAcc, consider consensus state first, not inserts */
4734 y = cm->cfirst[v] + yoffset;
4735
4736 for (j = sdr; j <= L; j++) {
4737 j_sdr = j - sdr;
4738
4739 for (d = sd; d <= j; d++) {
4740 d_sd = d-sd;
4741 if((sc = Jalpha[y][j_sdr][d_sd]) > Jalpha[v][j][d]) {
4742 Jalpha[v][j][d] = sc;
4743 Jyshadow[v][j][d] = yoffset + TRMODE_J_OFFSET;
4744 }
4745 }
4746 if(fill_L) {
4747 for (d = sd; d <= j; d++) {
4748 d_sd = d-sd;
4749 if((sc = Lalpha[y][j_sdr][d_sd]) > Lalpha[v][j][d]) {
4750 Lalpha[v][j][d] = sc;
4751 Lyshadow[v][j][d] = yoffset + TRMODE_L_OFFSET;
4752 }
4753 }
4754 }
4755 if(fill_R) {
4756 for (d = sd; d <= j; d++) {
4757 d_sd = d-sd;
4758 if((sc = Ralpha[y][j_sdr][d_sd]) > Ralpha[v][j][d]) {
4759 Ralpha[v][j][d] = sc;
4760 Ryshadow[v][j][d] = yoffset + TRMODE_R_OFFSET;
4761 }
4762 }
4763 }
4764 /* an easy to overlook case: if d == 0, ensure L and R values are IMPOSSIBLE */
4765 if(fill_L) Lalpha[v][j][0] = IMPOSSIBLE;
4766 if(fill_R) Ralpha[v][j][0] = IMPOSSIBLE;
4767 /* And another special case for BEGL_S and BEGR_S states,
4768 * reset shadow matrix values for d == 0 (which were
4769 * initialized to USED_EL above), even though the score of
4770 * these cells is impossible we may use them as a
4771 * zero-length left or right half of a BIF_B subtree during
4772 * construction of the parsetree.
4773 */
4774 if(cm->sttype[v] == S_st) {
4775 if(fill_L) Lyshadow[v][j][0] = USED_TRUNC_END;
4776 if(fill_R) Ryshadow[v][j][0] = USED_TRUNC_END;
4777 }
4778 }
4779 }
4780 /* no emission score to add */
4781 }
4782 else { /* B_st */
4783 assert(cm->sttype[v] == B_st);
4784 y = cm->cfirst[v]; /* left subtree */
4785 z = cm->cnum[v]; /* right subtree */
4786
4787 for (j = 0; j <= L; j++) {
4788 for (d = 0; d <= j; d++) {
4789 for (k = 0; k <= d; k++) {
4790 if((NOT_IMPOSSIBLE(Jalpha[y][j-k][d-k]) || d == k) && /* left subtree is not IMPOSSIBLE or IMPOSSIBLE w/length 0 (latter is ok b/c only emits contribute to score) */
4791 (NOT_IMPOSSIBLE(Jalpha[z][j][k]) || k == 0) && /* right subtree is not IMPOSSIBLE or IMPOSSIBLE w/length 0 (latter is ok b/c only emits contribute to score) */
4792 ((sc = FLogsum(Jalpha[y][j-k][d-k], Jalpha[z][j][k])) > Jalpha[v][j][d])) {
4793 Jalpha[v][j][d] = sc;
4794 Jkshadow[v][j][d] = k;
4795 }
4796 }
4797 if(fill_L) {
4798 for (k = 0; k <= d; k++) {
4799 if((NOT_IMPOSSIBLE(Jalpha[y][j-k][d-k]) || d == k) && /* left subtree is not IMPOSSIBLE or IMPOSSIBLE w/length 0 (latter is ok b/c only emits contribute to score) */
4800 (NOT_IMPOSSIBLE(Lalpha[z][j][k]) || k == 0) && /* right subtree is not IMPOSSIBLE or IMPOSSIBLE w/length 0 (latter is ok b/c only emits contribute to score) */
4801 ((sc = FLogsum(Jalpha[y][j-k][d-k], Jalpha[z][j][k])) > Jalpha[v][j][d])) {
4802 Lalpha[v][j][d] = sc;
4803 Lkshadow[v][j][d] = k;
4804 Lkmode[v][j][d] = TRMODE_J;
4805 }
4806 }
4807 }
4808 if(fill_R) {
4809 for (k = 0; k <= d; k++) {
4810 if((NOT_IMPOSSIBLE(Ralpha[y][j-k][d-k]) || d == k) && /* left subtree is not IMPOSSIBLE or IMPOSSIBLE w/length 0 (latter is ok b/c only emits contribute to score) */
4811 (NOT_IMPOSSIBLE(Jalpha[z][j][k]) || k == 0) && /* right subtree is not IMPOSSIBLE or IMPOSSIBLE w/length 0 (latter is ok b/c only emits contribute to score) */
4812 ((sc = FLogsum(Ralpha[y][j-k][d-k], Jalpha[z][j][k])) > Ralpha[v][j][d])) {
4813 Ralpha[v][j][d] = sc;
4814 Rkshadow[v][j][d] = k;
4815 Rkmode[v][j][d] = TRMODE_J;
4816 }
4817 }
4818 }
4819 if(fill_T) {
4820 for (k = 1; k < d; k++) { /* special boundary case for T matrix */
4821 if((NOT_IMPOSSIBLE(Ralpha[y][j-k][d-k])) && /* left subtree is not IMPOSSIBLE (no check for 'd==k' b/c of special T mx boundary case (see for loop above)) */
4822 (NOT_IMPOSSIBLE(Lalpha[z][j][k])) && /* right subtree is not IMPOSSIBLE (no check for 'k==0' b/c of special T mx boundary case (see for loop above)) */
4823 ((sc = FLogsum(Ralpha[y][j-k][d-k], Lalpha[z][j][k])) > Talpha[v][j][d])) {
4824 Talpha[v][j][d] = sc;
4825 Tkshadow[v][j][d] = k;
4826 }
4827 }
4828 }
4829 /* two additional special cases in trCYK (these are not in standard CYK) */
4830 /* special case 1: k == 0 (full sequence aligns to BEGL_S left child */
4831 if(fill_L) {
4832 if((sc = Jalpha[y][j][d]) > Lalpha[v][j][d]) {
4833 Lalpha[v][j][d] = sc;
4834 Lkshadow[v][j][d] = 0; /* k == 0 for this case, full sequence is on left */
4835 Lkmode[v][j][d] = TRMODE_J;
4836 }
4837 if((sc = Lalpha[y][j][d]) > Lalpha[v][j][d]) {
4838 Lalpha[v][j][d] = sc;
4839 Lkshadow[v][j][d] = 0; /* k == 0 for this case, full sequence is on left */
4840 Lkmode[v][j][d] = TRMODE_L;
4841 }
4842 }
4843 /* special case 2: k == d (full sequence aligns to BEGR_S right child */
4844 if(fill_R) {
4845 if((sc = Jalpha[z][j][d]) > Ralpha[v][j][d]) {
4846 Ralpha[v][j][d] = sc;
4847 Rkshadow[v][j][d] = d; /* k == d in this case, full sequence is on right */
4848 Rkmode[v][j][d] = TRMODE_J;
4849 }
4850 if((sc = Ralpha[z][j][d]) > Ralpha[v][j][d]) {
4851 Ralpha[v][j][d] = sc;
4852 Rkshadow[v][j][d] = d; /* k == d in this case, full sequence is on right */
4853 Rkmode[v][j][d] = TRMODE_R;
4854 }
4855 }
4856 }
4857 }
4858 }/* end of B_st recursion */
4859
4860 /* Now handle from ROOT_S, state 0. So far we haven't touched
4861 * the {J,L,R,T}alpha[0] decks at all since initialization and here
4862 * we'll only update at most 1 cell in each, the one pertaining
4863 * to a full alignment [0][L][L].
4864 *
4865 * In truncated alignment the only way out of ROOT_S in local or
4866 * global mode is via a 'truncated begin' with a score (penalty)
4867 * from cm->trp into any emitting state. The penalty was
4868 * calculated in cm_tr_penalties_Create() and differs depending on
4869 * whether we are in local or global mode and the value of
4870 * 'pty_idx' which was passed in.
4871 *
4872 * Since we're in OptAcc alignment we don't assess the
4873 * penalty but we still need to know if it's non-IMPOSSIBLE,
4874 * to know which states we're allowed to do a truncated
4875 * begin into.
4876 */
4877 trpenalty = (have_el) ? cm->trp->l_ptyAA[pty_idx][v] : cm->trp->g_ptyAA[pty_idx][v];
4878 if(NOT_IMPOSSIBLE(trpenalty)) {
4879 if(preset_mode == TRMODE_J) {
4880 if(Jalpha[v][L][L] > Jalpha[0][L][L]) {
4881 Jalpha[0][L][L] = Jalpha[v][L][L];
4882 b = v;
4883 }
4884 }
4885 else if(preset_mode == TRMODE_L) {
4886 if(Lalpha[v][L][L] > Lalpha[0][L][L]) {
4887 Lalpha[0][L][L] = Lalpha[v][L][L];
4888 b = v;
4889 }
4890 }
4891 else if(preset_mode == TRMODE_R) {
4892 if(Ralpha[v][L][L] > Ralpha[0][L][L]) {
4893 Ralpha[0][L][L] = Ralpha[v][L][L];
4894 b = v;
4895 }
4896 }
4897 else if(preset_mode == TRMODE_T) {
4898 if(cm->sttype[v] == B_st) {
4899 if(Talpha[v][L][L] > Talpha[0][L][L]) {
4900 Talpha[0][L][L] = Talpha[v][L][L];
4901 b = v;
4902 }
4903 }
4904 }
4905 }
4906 } /* end loop for (v = cm->M-1; v > 0; v--) */
4907
4908 /* all valid alignments must use a truncated begin */
4909 Jyshadow[0][L][L] = USED_TRUNC_BEGIN;
4910 if(fill_L) Lyshadow[0][L][L] = USED_TRUNC_BEGIN;
4911 if(fill_R) Ryshadow[0][L][L] = USED_TRUNC_BEGIN;
4912 /* Tyshadow[0] doesn't exist, caller must know how to deal */
4913
4914 if (preset_mode == TRMODE_J) sc = Jalpha[0][L][L];
4915 if (preset_mode == TRMODE_L) sc = Lalpha[0][L][L];
4916 if (preset_mode == TRMODE_R) sc = Ralpha[0][L][L];
4917 if (preset_mode == TRMODE_T) sc = Talpha[0][L][L];
4918
4919 /* convert pp, a log probability, into the average posterior probability of all L aligned residues */
4920 pp = sreEXP2(sc) / (float) L;
4921
4922 #if eslDEBUGLEVEL >= 2
4923 /* Uncomment to dump matrix to file. Careful...this could be very large. */
4924 /* FILE *fp1; fp1 = fopen("tmp.tru_oamx", "w"); cm_tr_mx_Dump(fp1, mx, preset_mode, TRUE); fclose(fp1); */
4925 /* FILE *fp2; fp2 = fopen("tmp.tru_oashmx", "w"); cm_tr_shadow_mx_Dump(fp2, cm, shmx, preset_mode, TRUE); fclose(fp2); */
4926 #endif
4927
4928 if(ret_b != NULL) *ret_b = b;
4929 if(ret_pp != NULL) *ret_pp = pp;
4930
4931 ESL_DPRINTF1(("#DEBUG: cm_TrOptAccAlign() return pp: %f\n", pp));
4932 return eslOK;
4933 }
4934
4935
4936 /* Function: cm_TrOptAccAlignHB()
4937 * Date: EPN, Tue Oct 11 10:05:24 2011
4938 *
4939 * Purpose: Run the truncated version of the Holmes/Durbin optimal
4940 * accuracy algorithm on a full target sequence 1..L, given
4941 * a pre-filled posterior matrix. Uses float log odds
4942 * scores. HMM banded version. cm_OptAccAlign() is the
4943 * non-banded version. See that function's 'Purpose' for
4944 * more information. The only difference is that we use
4945 * HMM bands from cm->cp9b here. All cells outside the
4946 * bands don't exist in memory, so we have to be careful
4947 * with offset issues.
4948 *
4949 * Args: cm - the model
4950 * errbuf - char buffer for reporting errors
4951 * dsq - the digitaized sequence [1..L]
4952 * L - length of the dsq
4953 * size_limit - max number of Mb for DP matrix, if matrix is bigger return eslERANGE
4954 * preset_mode - the pre-determined alignment mode, can't be TRMODE_UNKNOWN
4955 * pass_idx - pipeline pass index, indicates what truncation penalty to use
4956 * mx - the DP matrix to fill in
4957 * shmx - the shadow matrix to fill in
4958 * emit_mx - pre-filled emit matrix
4959 * ret_b - optimal entry point (state) for the alignment
4960 * ret_pp - RETURN: average posterior probability of aligned residues
4961 * in the optimally accurate parsetree
4962 *
4963 * Returns: <eslOK> on success.
4964 * Throws: <eslERANGE> if required CM_TR_HB_MX size exceeds <size_limit>
4965 * If !eslOK: alignment has been aborted, ret_* variables are not valid
4966 */
4967 int
4968 cm_TrOptAccAlignHB(CM_t *cm, char *errbuf, ESL_DSQ *dsq, int L, float size_limit, char preset_mode, int pass_idx,
4969 CM_TR_HB_MX *mx, CM_TR_HB_SHADOW_MX *shmx, CM_TR_HB_EMIT_MX *emit_mx, int *ret_b, float *ret_pp)
4970 {
4971 int status; /* easel status code */
4972 int v,y,z; /* indices for states */
4973 int j,d,i,k; /* indices in sequence dimensions */
4974 float sc; /* temporary log odds score */
4975 float pp; /* average posterior probability of all emitted residues */
4976 int yoffset; /* y=base+offset -- counter in child states that v can transit to */
4977 int sd; /* StateDelta(cm->sttype[v]) */
4978 int sdl; /* StateLeftDelta(cm->sttype[v] */
4979 int sdr; /* StateRightDelta(cm->sttype[v] */
4980 int j_sdr; /* j - sdr */
4981 int have_el; /* TRUE if local ends are on in the CM, otherwise FALSE */
4982 int64_t c; /* 64-bit int counter */
4983
4984 /* indices used for handling band-offset issues, and in the depths of the DP recursion */
4985 int ip_v; /* offset i index for state v */
4986 int jp_v, jp_y, jp_z; /* offset j index for states v, y, z */
4987 int jp_y_sdr; /* jp_y - sdr */
4988 int jn, jx; /* current minimum/maximum j allowed */
4989 int jpn, jpx; /* minimum/maximum jp_v */
4990 int dp_y_sd; /* dp_y - sd */
4991 int dp_y_sdl; /* dp_y - sdl */
4992 int dp_y_sdr; /* dp_y - sdr */
4993 int dp_v, dp_y, dp_z; /* d index for state v,y,z in HMM banded matrix */
4994 int dn, dx; /* current minimum/maximum d allowed */
4995 int dpn, dpx; /* minimum/maximum dp_v */
4996 int kp_z; /* k (in the d dim) index for state z in alpha w/mem eff bands */
4997 int kn, kx; /* current minimum/maximum k value */
4998 int *yvalidA = NULL; /* [0..MAXCONNECT-1] TRUE if v->yoffset is legal transition (within bands) */
4999 int yvalid_idx; /* for keeping track of which children are valid */
5000 int yvalid_ct; /* for keeping track of which children are valid */
5001 int Lp; /* L index also changes depending on state */
5002 int jp_0; /* L offset in ROOT_S's (v==0) j band */
5003 int Lp_0; /* L offset in ROOT_S's (v==0) d band */
5004
5005 /* other variables used in truncated version, but not standard version (not in cm_OptAccAlign()) */
5006 int b = 0; /* best truncated entry state */
5007 int fill_L, fill_R, fill_T; /* must we fill in the L, R, and T matrices? */
5008 int pty_idx; /* index for truncation penalty, determined by pass_idx */
5009 float trpenalty; /* truncation penalty, differs based on pty_idx and if we're local or global */
5010 int nins_v; /* number of insert states reachable from current state */
5011 int yctr; /* used for special for(y) loops in TrOptAcc (see code) */
5012
5013 /* variables related to truncated alignment (not in cm_OptAccAlignHB() */
5014 int do_J_v, do_J_y, do_J_z; /* must we fill J matrix deck for state v, y, z? */
5015 int do_L_v, do_L_y, do_L_z; /* must we fill L matrix deck for state v, y, z? */
5016 int do_R_v, do_R_y, do_R_z; /* must we fill R matrix deck for state v, y, z? */
5017 int do_T_v; /* must we fill T matrix deck for state v? */
5018
5019 /* variables used for memory efficient bands */
5020 /* ptrs to cp9b info, for convenience */
5021 CP9Bands_t *cp9b = cm->cp9b;
5022 int *imin = cp9b->imin;
5023 int *imax = cp9b->imax;
5024 int *jmin = cp9b->jmin;
5025 int *jmax = cp9b->jmax;
5026 int **hdmin = cp9b->hdmin;
5027 int **hdmax = cp9b->hdmax;
5028
5029 /* the DP matrices */
5030 float ***Jalpha = mx->Jdp; /* pointer to the Jalpha DP matrix */
5031 float ***Lalpha = mx->Ldp; /* pointer to the Lalpha DP matrix */
5032 float ***Ralpha = mx->Rdp; /* pointer to the Ralpha DP matrix */
5033 float ***Talpha = mx->Tdp; /* pointer to the Talpha DP matrix */
5034
5035 char ***Jyshadow = shmx->Jyshadow; /* pointer to the Jyshadow matrix */
5036 char ***Lyshadow = shmx->Lyshadow; /* pointer to the Lyshadow matrix */
5037 char ***Ryshadow = shmx->Ryshadow; /* pointer to the Ryshadow matrix */
5038 int ***Jkshadow = shmx->Jkshadow; /* pointer to the Jkshadow matrix */
5039 int ***Lkshadow = shmx->Lkshadow; /* pointer to the Lkshadow matrix */
5040 int ***Rkshadow = shmx->Rkshadow; /* pointer to the Rkshadow matrix */
5041 int ***Tkshadow = shmx->Tkshadow; /* pointer to the Tkshadow matrix */
5042 char ***Lkmode = shmx->Lkmode; /* pointer to the Lkmode matrix */
5043 char ***Rkmode = shmx->Rkmode; /* pointer to the Rkmode matrix */
5044
5045 float **Jl_pp = emit_mx->Jl_pp; /* pointer to the prefilled posterior values for left emitters in Joint mode */
5046 float **Ll_pp = emit_mx->Ll_pp; /* pointer to the prefilled posterior values for left emitters in Left mode */
5047 float **Jr_pp = emit_mx->Jr_pp; /* pointer to the prefilled posterior values for right emitters in Joint mode */
5048 float **Rr_pp = emit_mx->Rr_pp; /* pointer to the prefilled posterior values for right emitters in Right mode */
5049
5050 /* Determine which matrices we need to fill in, based on <preset_mode> */
5051 if((status = cm_TrFillFromMode(preset_mode, &fill_L, &fill_R, &fill_T)) != eslOK) ESL_FAIL(status, errbuf, "cm_TrOptAccAlignHB(), bogus mode: %d", preset_mode);
5052
5053 /* Determine the truncation penalty index, from the pass_idx */
5054 if((pty_idx = cm_tr_penalties_IdxForPass(pass_idx)) == -1) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_TrOptAccAlignHB(), unexpected pass idx: %d", pass_idx);
5055
5056 /* Allocations and initializations */
5057 /* In OptAcc <preset_mode> must be known, ensure a full alignment to ROOT_S (v==0) in the optimal mode is allowed by the bands */
5058 if (preset_mode != TRMODE_J && preset_mode != TRMODE_L && preset_mode != TRMODE_R && preset_mode != TRMODE_T) ESL_FAIL(eslEINVAL, errbuf, "cm_TrOptAccAlignHB(): preset_mode is not J, L, R, or T");
5059 if (preset_mode == TRMODE_J && (! cp9b->Jvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrOptAccAlignHB(): preset_mode is J mode, but cp9b->Jvalid[v] is FALSE");
5060 if (preset_mode == TRMODE_L && (! cp9b->Lvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrOptAccAlignHB(): preset_mode is L mode, but cp9b->Lvalid[v] is FALSE");
5061 if (preset_mode == TRMODE_R && (! cp9b->Rvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrOptAccAlignHB(): preset_mode is R mode, but cp9b->Rvalid[v] is FALSE");
5062 if (preset_mode == TRMODE_T && (! cp9b->Tvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrOptAccAlignHB(): preset_mode is T mode, but cp9b->Tvalid[v] is FALSE");
5063 if (cp9b->jmin[0] > L || cp9b->jmax[0] < L) ESL_FAIL(eslEINVAL, errbuf, "cm_TrOptAccAlignHB(): L (%d) is outside ROOT_S's j band (%d..%d)\n", L, cp9b->jmin[0], cp9b->jmax[0]);
5064 jp_0 = L - jmin[0];
5065 if (cp9b->hdmin[0][jp_0] > L || cp9b->hdmax[0][jp_0] < L) ESL_FAIL(eslEINVAL, errbuf, "cm_TrOptAccAlignHB(): L (%d) is outside ROOT_S's d band (%d..%d)\n", L, cp9b->hdmin[0][jp_0], cp9b->hdmax[0][jp_0]);
5066 Lp_0 = L - hdmin[0][jp_0];
5067
5068 /* grow the matrices based on the current sequence and bands */
5069 if((status = cm_tr_hb_mx_GrowTo (cm, mx, errbuf, cm->cp9b, L, size_limit)) != eslOK) return status;
5070 if((status = cm_tr_hb_shadow_mx_GrowTo(cm, shmx, errbuf, cm->cp9b, L, size_limit)) != eslOK) return status;
5071
5072 /* initialize all cells of the matrix to IMPOSSIBLE, all cells of shadow matrix to USED_EL or USED_TRUNC_END */
5073 if( mx->Jncells_valid > 0) esl_vec_FSet(mx->Jdp_mem, mx->Jncells_valid, IMPOSSIBLE);
5074 if( mx->Lncells_valid > 0 && fill_L) esl_vec_FSet(mx->Ldp_mem, mx->Lncells_valid, IMPOSSIBLE);
5075 if( mx->Rncells_valid > 0 && fill_R) esl_vec_FSet(mx->Rdp_mem, mx->Rncells_valid, IMPOSSIBLE);
5076 if( mx->Tncells_valid > 0 && fill_T) esl_vec_FSet(mx->Tdp_mem, mx->Tncells_valid, IMPOSSIBLE);
5077 if(shmx->Jy_ncells_valid > 0) for(c = 0; c < shmx->Jy_ncells_valid; c++) shmx->Jyshadow_mem[c] = USED_EL;
5078 if(shmx->Ly_ncells_valid > 0 && fill_L) for(c = 0; c < shmx->Ly_ncells_valid; c++) shmx->Lyshadow_mem[c] = USED_EL;
5079 if(shmx->Ry_ncells_valid > 0 && fill_R) for(c = 0; c < shmx->Ry_ncells_valid; c++) shmx->Ryshadow_mem[c] = USED_EL;
5080 /* for B states, shadow matrix holds k, length of right fragment, this will be overwritten */
5081 if(shmx->Jk_ncells_valid > 0) esl_vec_ISet(shmx->Jkshadow_mem, shmx->Jk_ncells_valid, 0);
5082 if(shmx->Lk_ncells_valid > 0 && fill_L) esl_vec_ISet(shmx->Lkshadow_mem, shmx->Lk_ncells_valid, 0);
5083 if(shmx->Rk_ncells_valid > 0 && fill_R) esl_vec_ISet(shmx->Rkshadow_mem, shmx->Rk_ncells_valid, 0);
5084 if(shmx->Tk_ncells_valid > 0 && fill_T) esl_vec_ISet(shmx->Tkshadow_mem, shmx->Tk_ncells_valid, 0);
5085 if(shmx->Lk_ncells_valid > 0 && fill_L) for(c = 0; c < shmx->Lk_ncells_valid; c++) shmx->Lkmode_mem[c] = TRMODE_J;
5086 if(shmx->Rk_ncells_valid > 0 && fill_R) for(c = 0; c < shmx->Rk_ncells_valid; c++) shmx->Rkmode_mem[c] = TRMODE_J;
5087
5088 /* a special optimal accuracy specific step, initialize Jyshadow intelligently for d == 0
5089 * (necessary b/c zero length parsetees have 0 emits and so always score IMPOSSIBLE)
5090 */
5091 if((status = cm_InitializeOptAccShadowDZeroHB(cm, cp9b, errbuf, Jyshadow, L)) != eslOK) return status;
5092
5093 /* start with the EL state (remember the EL deck is non-banded) */
5094 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
5095 if(have_el) {
5096 do_J_v = (cp9b->Jvalid[cm->M] && Jl_pp[cm->M] != NULL) ? TRUE : FALSE;
5097 do_L_v = (cp9b->Lvalid[cm->M] && Ll_pp[cm->M] != NULL && fill_L) ? TRUE : FALSE;
5098 do_R_v = (cp9b->Rvalid[cm->M] && Rr_pp[cm->M] != NULL && fill_R) ? TRUE : FALSE;
5099 for (j = 0; j <= L; j++) {
5100 if(do_J_v) Jalpha[cm->M][j][0] = Jl_pp[cm->M][0];
5101 if(do_L_v) Lalpha[cm->M][j][0] = Ll_pp[cm->M][0];
5102 if(do_R_v) Ralpha[cm->M][j][0] = Rr_pp[cm->M][0];
5103 if(do_J_v) {
5104 i = j;
5105 for (d = 1; d <= j; d++) Jalpha[cm->M][j][d] = FLogsum(Jalpha[cm->M][j][d-1], Jl_pp[cm->M][i--]);
5106 }
5107 if(do_L_v) {
5108 i = j;
5109 for (d = 1; d <= j; d++) Lalpha[cm->M][j][d] = FLogsum(Lalpha[cm->M][j][d-1], Ll_pp[cm->M][i--]);
5110 }
5111 if(do_R_v) {
5112 i = j;
5113 for (d = 1; d <= j; d++) Ralpha[cm->M][j][d] = FLogsum(Ralpha[cm->M][j][d-1], Rr_pp[cm->M][i--]);
5114 }
5115 }
5116 }
5117
5118 /* yvalidA[0..cnum[v]] will hold TRUE for states y for which a transition is legal
5119 * (some transitions are impossible due to the bands)
5120 */
5121 ESL_ALLOC(yvalidA, sizeof(int) * MAXCONNECT);
5122 esl_vec_ISet(yvalidA, MAXCONNECT, FALSE);
5123
5124 /* Main recursion */
5125 for (v = cm->M-1; v > 0; v--) { /* almost to ROOT_S, we handle that differently */
5126 sd = StateDelta(cm->sttype[v]);
5127 sdl = StateLeftDelta(cm->sttype[v]);
5128 sdr = StateRightDelta(cm->sttype[v]);
5129 nins_v = NumReachableInserts(cm->stid[v]);
5130 do_J_v = cp9b->Jvalid[v] ? TRUE : FALSE;
5131 do_L_v = cp9b->Lvalid[v] && fill_L ? TRUE : FALSE;
5132 do_R_v = cp9b->Rvalid[v] && fill_R ? TRUE : FALSE;
5133 do_T_v = cp9b->Tvalid[v] && fill_T ? TRUE : FALSE;
5134
5135 /* re-initialize if we can do a local end from v,
5136 * copy values from saved EL deck.
5137 * shadow values remain as initialized: USED_EL
5138 */
5139 if(have_el && NOT_IMPOSSIBLE(cm->endsc[v])) {
5140 if(do_J_v && cp9b->Jvalid[cm->M]) {
5141 for (j = jmin[v]; j <= jmax[v]; j++) {
5142 jp_v = j - jmin[v];
5143 for(d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) {
5144 dp_v = d - hdmin[v][jp_v];
5145 Jalpha[v][jp_v][dp_v] = Jalpha[cm->M][j-sdr][d-sd];
5146 }
5147 }
5148 }
5149 if(do_L_v && cp9b->Lvalid[cm->M]) {
5150 for (j = jmin[v]; j <= jmax[v]; j++) {
5151 jp_v = j - jmin[v];
5152 for(d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) {
5153 dp_v = d - hdmin[v][jp_v];
5154 Lalpha[v][jp_v][dp_v] = Lalpha[cm->M][j][d-sdl];
5155 }
5156 }
5157 }
5158 if(do_R_v && cp9b->Rvalid[cm->M]) {
5159 for (j = jmin[v]; j <= jmax[v]; j++) {
5160 jp_v = j - jmin[v];
5161 for(d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) {
5162 dp_v = d - hdmin[v][jp_v];
5163 Ralpha[v][jp_v][dp_v] = Ralpha[cm->M][j-sdr][d-sdr];
5164 }
5165 }
5166 }
5167 }
5168 /* note there's no E state update here, those cells all remain IMPOSSIBLE */
5169
5170 if(cm->sttype[v] == IL_st || cm->sttype[v] == ML_st) {
5171 /* update alpha[v][j][d] cells, for IL and ML states, loop
5172 * nesting order is: for j { for d { for y { } } } because they
5173 * can self transit, and a alpha[v][j][d] cell must be complete
5174 * (that is we must have looked at all children y) before can
5175 * start calc'ing for alpha[v][j][d+1] We do ML states as well
5176 * as IL states b/c they follow the same rules. We could possibly
5177 * separate them out and get a small speedup, but I don't think
5178 * it's worth further complicating the code.
5179 */
5180
5181 /* In TrCYK: we need to treat R differently from and J and L
5182 * here, by doing separate 'for (d...' loops for J and R
5183 * because we have to fully calculate Jalpha[v][j][d]) before we
5184 * can start to calculate Ralpha[v][j][d].
5185 */
5186 if(! StateIsDetached(cm, v)) { /* if we're detached (unreachable), leave all {J,L,R}alpha values as they were initialized, as IMPOSSIBLE */
5187 if(do_J_v || do_L_v || do_R_v) {
5188 for (j = jmin[v]; j <= jmax[v]; j++) {
5189 jp_v = j - jmin[v];
5190
5191 /* determine which children y we can legally transit to for v, j in J and L mode */
5192 yvalid_ct = 0;
5193 for (yctr = 0; yctr < cm->cnum[v]; yctr++) {
5194 yoffset = (yctr + nins_v) % cm->cnum[v]; /* special y ordering for TrOptAcc, consider consensus state first, not inserts */
5195 y = cm->cfirst[v] + yoffset;
5196 if(j >= jmin[y] && j <= jmax[y]) yvalidA[yvalid_ct++] = yoffset; /* is j valid for state y? */
5197 }
5198
5199 if(do_J_v || do_L_v) {
5200 i = j - hdmin[v][jp_v] + 1;
5201 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++, i--) { /* for each valid d for v, j */
5202 assert(i >= imin[v] && i <= imax[v]);
5203 ESL_DASSERT1((i >= imin[v] && i <= imax[v]));
5204 ip_v = i - imin[v]; /* i index for state v in emit_mx->{J,L}l_pp */
5205 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha */
5206
5207 for (yvalid_idx = 0; yvalid_idx < yvalid_ct; yvalid_idx++) { /* for each valid child y, for v, j */
5208 yoffset = yvalidA[yvalid_idx];
5209 y = cm->cfirst[v] + yoffset;
5210 jp_y = j - jmin[y];
5211 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
5212 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
5213
5214 if(do_J_y || do_L_y) {
5215 if((d-sd) >= hdmin[y][jp_y] && (d-sd) <= hdmax[y][jp_y]) { /* make sure d is valid for this v, j and y */
5216 dp_y_sd = d - hdmin[y][jp_y] - sd;
5217 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
5218 ESL_DASSERT1((dp_y_sd >= 0 && dp_y_sd <= (hdmax[y][jp_y] - hdmin[y][jp_y])));
5219 if(do_J_v && do_J_y) {
5220 if ((sc = Jalpha[y][jp_y][dp_y_sd]) > Jalpha[v][jp_v][dp_v]) {
5221 Jalpha[v][jp_v][dp_v] = sc;
5222 Jyshadow[v][jp_v][dp_v] = yoffset + TRMODE_J_OFFSET;
5223 }
5224 }
5225 if(do_L_v && do_L_y) {
5226 if ((sc = Lalpha[y][jp_y][dp_y_sd]) > Lalpha[v][jp_v][dp_v]) {
5227 Lalpha[v][jp_v][dp_v] = sc;
5228 Lyshadow[v][jp_v][dp_v] = yoffset + TRMODE_L_OFFSET;
5229 }
5230 }
5231 }
5232 }
5233 } /* end of 'for (yvalid_idx = 0'... */
5234 /* add emission PP */
5235 if(do_J_v) {
5236 Jalpha[v][jp_v][dp_v] = FLogsum(Jalpha[v][jp_v][dp_v], Jl_pp[v][ip_v]);
5237 Jalpha[v][jp_v][dp_v] = ESL_MAX(Jalpha[v][jp_v][dp_v], IMPOSSIBLE);
5238 /* special case if local ends are off: explicitly disallow transitions to EL that require EL emissions
5239 * (we do allow an 'illegal' transition to EL in OptAcc but only if no EL emissions are req'd)
5240 */
5241 if((! have_el) && Jyshadow[v][jp_v][dp_v] == USED_EL && d > sd) {
5242 Jalpha[v][jp_v][dp_v] = IMPOSSIBLE;
5243 }
5244 }
5245 if(do_L_v) {
5246 if(d >= 2) {
5247 Lalpha[v][jp_v][dp_v] = FLogsum(Lalpha[v][jp_v][dp_v], Ll_pp[v][ip_v]);
5248 /* special case if local ends are off: explicitly disallow transitions to EL that require EL emissions *
5249 * (we do allow an 'illegal' transition to EL in OptAcc but only if no EL emissions are req'd)
5250 */
5251 if((! have_el) && Lyshadow[v][jp_v][dp_v] == USED_EL) {
5252 Lalpha[v][jp_v][dp_v] = IMPOSSIBLE;
5253 }
5254 }
5255 else {
5256 Lalpha[v][jp_v][dp_v] = Ll_pp[v][ip_v]; /* actually I think this will give the same value as d >= 2 case above */
5257 Lyshadow[v][jp_v][dp_v] = USED_TRUNC_END;
5258 }
5259 Lalpha[v][jp_v][dp_v] = ESL_MAX(Lalpha[v][jp_v][dp_v], IMPOSSIBLE);
5260 }
5261 }
5262 }
5263 /* handle R separately */
5264 if(do_R_v) {
5265 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) { /* for each valid d for v, j */
5266 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha */
5267
5268 for (yvalid_idx = 0; yvalid_idx < yvalid_ct; yvalid_idx++) { /* for each valid child y, for v, j */
5269 yoffset = yvalidA[yvalid_idx];
5270 y = cm->cfirst[v] + yoffset;
5271 jp_y = j - jmin[y];
5272 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
5273 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
5274
5275 if((do_J_y || do_R_y) && (y != v)) { /* (y != v) part is to disallow IL self transits in R mode */
5276 /* note we use 'd', not 'd_sd' (which we used in the corresponding loop for J,L above) */
5277 if(d >= hdmin[y][jp_y] && d <= hdmax[y][jp_y]) { /* make sure d is valid for this v, j and y */
5278 dp_y = d - hdmin[y][jp_y];
5279 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
5280 if(do_J_y) {
5281 if ((sc = Jalpha[y][jp_y][dp_y]) > Ralpha[v][jp_v][dp_v]) {
5282 Ralpha[v][jp_v][dp_v] = sc;
5283 Ryshadow[v][jp_v][dp_v] = yoffset + TRMODE_J_OFFSET;
5284 }
5285 }
5286 if(do_R_y) {
5287 if ((sc = Ralpha[y][jp_y][dp_y]) > Ralpha[v][jp_v][dp_v]) {
5288 Ralpha[v][jp_v][dp_v] = sc;
5289 Ryshadow[v][jp_v][dp_v] = yoffset + TRMODE_R_OFFSET;
5290 }
5291 }
5292 }
5293 }
5294 }
5295 /* no residue was emitted if we're in R mode */
5296 Ralpha[v][jp_v][dp_v] = ESL_MAX(Ralpha[v][jp_v][dp_v], IMPOSSIBLE);
5297 }
5298 }
5299 } /* end of for j loop */
5300 }
5301 } /* end of if(! StateIsDetached(cm,v )) */
5302 } /* end of if IL/ML state */
5303 else if(cm->sttype[v] == IR_st || cm->sttype[v] == MR_st) {
5304 /* update alpha[v][j][d] cells, for IR and MR states, loop
5305 * nesting order is: for j { for d { for y { } } } because they
5306 * can self transit, and a alpha[v][j][d] cell must be complete
5307 * (that is we must have looked at all children y) before can
5308 * start calc'ing for alpha[v][j][d+1]. We do MR states as well
5309 * as IR states here b/c they follow the same rules. We could
5310 * possibly separate them out and get a small speedup, but I
5311 * don't think it's worth further complicating the code. and
5312 * we're not worried about efficiency here.
5313 */
5314
5315 /* In TrCYK: we need to treat L differently from and J and R
5316 * here, by doing separate 'for (d..' loops for J and R
5317 * because we have to fully calculate Jalpha[v][j][d]) before we
5318 * can start to calculate Lalpha[v][j][d].
5319 */
5320
5321 if(! StateIsDetached(cm, v)) { /* if we're detached (unreachable), leave all {J,L,R}alpha values as they were initialized, as IMPOSSIBLE */
5322 if(do_J_v || do_L_v || do_R_v) {
5323 for (j = jmin[v]; j <= jmax[v]; j++) {
5324 jp_v = j - jmin[v];
5325 j_sdr = j - sdr;
5326
5327 /* determine which children y we can legally transit to for v, j in J and R mode */
5328 yvalid_ct = 0;
5329 for (yctr = 0; yctr < cm->cnum[v]; yctr++) {
5330 yoffset = (yctr + nins_v) % cm->cnum[v]; /* special y ordering for TrOptAcc, consider consensus state first, not inserts */
5331 y = cm->cfirst[v] + yoffset;
5332 if((j_sdr) >= jmin[y] && ((j_sdr) <= jmax[y])) yvalidA[yvalid_ct++] = yoffset; /* is j-sdr valid for state y? */
5333 }
5334
5335 if(do_J_v || do_R_v) {
5336 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) { /* for each valid d for v, j */
5337 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha */
5338 for (yvalid_idx = 0; yvalid_idx < yvalid_ct; yvalid_idx++) { /* for each valid child y, for v, j */
5339 yoffset = yvalidA[yvalid_idx];
5340 y = cm->cfirst[v] + yoffset;
5341 jp_y_sdr = j - jmin[y] - sdr;
5342 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
5343 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
5344
5345 if(do_J_y || do_R_y) {
5346 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 */
5347 dp_y_sd = d - hdmin[y][jp_y_sdr] - sd;
5348 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
5349 ESL_DASSERT1((dp_y_sd >= 0 && dp_y_sd <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
5350 if(do_J_v && do_J_y) {
5351 if ((sc = Jalpha[y][jp_y_sdr][dp_y_sd]) > Jalpha[v][jp_v][dp_v]) {
5352 Jalpha[v][jp_v][dp_v] = sc;
5353 Jyshadow[v][jp_v][dp_v] = yoffset + TRMODE_J_OFFSET;
5354 }
5355 }
5356 if(do_R_v && do_R_y) {
5357 if ((sc = Ralpha[y][jp_y_sdr][dp_y_sd]) > Ralpha[v][jp_v][dp_v]) {
5358 Ralpha[v][jp_v][dp_v] = sc;
5359 Ryshadow[v][jp_v][dp_v] = yoffset + TRMODE_R_OFFSET;
5360 }
5361 }
5362 }
5363 }
5364 } /* end of 'for (yvalid_idx = 0'... */
5365 /* add emission PP */
5366 if(do_J_v) {
5367 Jalpha[v][jp_v][dp_v] = FLogsum(Jalpha[v][jp_v][dp_v], Jr_pp[v][jp_v]);
5368 Jalpha[v][jp_v][dp_v] = ESL_MAX(Jalpha[v][jp_v][dp_v], IMPOSSIBLE);
5369 /* special case if local ends are off: explicitly disallow transitions to EL that require EL emissions
5370 * (we do allow an 'illegal' transition to EL in OptAcc but only if no EL emissions are req'd)
5371 */
5372 if((! have_el) && Jyshadow[v][jp_v][dp_v] == USED_EL && d > sd) {
5373 Jalpha[v][jp_v][dp_v] = IMPOSSIBLE;
5374 }
5375 }
5376 if(do_R_v) {
5377 if(d >= 2) {
5378 Ralpha[v][jp_v][dp_v] = FLogsum(Ralpha[v][jp_v][dp_v], Rr_pp[v][jp_v]);
5379 /* special case if local ends are off: explicitly disallow transitions to EL that require EL emissions
5380 * (we do allow an 'illegal' transition to EL in OptAcc but only if no EL emissions are req'd)
5381 */
5382 if((! have_el) && Ryshadow[v][jp_v][dp_v] == USED_EL) {
5383 Ralpha[v][jp_v][dp_v] = IMPOSSIBLE;
5384 }
5385 }
5386 else {
5387 Ralpha[v][jp_v][dp_v] = Rr_pp[v][jp_v]; /* actually I think this will give the same value as d >= 2 case above */
5388 Ryshadow[v][jp_v][dp_v] = USED_TRUNC_END;
5389 }
5390 Ralpha[v][jp_v][dp_v] = ESL_MAX(Ralpha[v][jp_v][dp_v], IMPOSSIBLE);
5391 }
5392 } /* end of for(d... */
5393 } /* end of if(do_J_v || do_R_v) */
5394 /* handle L separately */
5395 if(do_L_v) {
5396 /* determine which children y we can legally transit to for v, j, this is different for L, b/c j is different,
5397 * note we use 'j' and not 'j_sdr' because IR and MR are silent in L marginal mode
5398 */
5399 yvalid_ct = 0;
5400 for (yctr = 0; yctr < cm->cnum[v]; yctr++) {
5401 yoffset = (yctr + nins_v) % cm->cnum[v]; /* special y ordering for TrOptAcc, consider consensus state first, not inserts */
5402 y = cm->cfirst[v] + yoffset;
5403 if(j >= jmin[y] && j <= jmax[y]) yvalidA[yvalid_ct++] = yoffset; /* is j valid for state y? */
5404 }
5405
5406 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) { /* for each valid d for v, j */
5407 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha */
5408 for (yvalid_idx = 0; yvalid_idx < yvalid_ct; yvalid_idx++) { /* for each valid child y, for v, j */
5409 yoffset = yvalidA[yvalid_idx];
5410 y = cm->cfirst[v] + yoffset;
5411 jp_y = j - jmin[y];
5412 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
5413 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
5414
5415 /* note we use 'd' and not 'd-sd' below because IR/MR are silent in L marginal mode */
5416 if((do_J_y || do_L_y) && (y != v)) { /* (y != v) part is to disallow IR self transits in L mode */
5417 if(d >= hdmin[y][jp_y] && d <= hdmax[y][jp_y]) { /* make sure d is valid for this v, j and y */
5418 dp_y = d - hdmin[y][jp_y] ;
5419 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
5420 ESL_DASSERT1((dp_y >= 0 && dp_y <= (hdmax[y][jp_y] - hdmin[y][jp_y])));
5421 if(do_J_y) {
5422 if ((sc = Jalpha[y][jp_y][dp_y]) > Lalpha[v][jp_v][dp_v]) {
5423 Lalpha[v][jp_v][dp_v] = sc;
5424 Lyshadow[v][jp_v][dp_v] = yoffset + TRMODE_J_OFFSET;
5425 }
5426 }
5427 if(do_L_y) {
5428 if ((sc = Lalpha[y][jp_y][dp_y]) > Lalpha[v][jp_v][dp_v]) {
5429 Lalpha[v][jp_v][dp_v] = sc;
5430 Lyshadow[v][jp_v][dp_v] = yoffset + TRMODE_L_OFFSET;
5431 }
5432 }
5433 }
5434 }
5435 } /* end of 'for (yvalid_idx = 0'... */
5436 /* no residue was emitted if we're in L mode */
5437 Lalpha[v][jp_v][dp_v] = ESL_MAX(Lalpha[v][jp_v][dp_v], IMPOSSIBLE);
5438 }
5439 }
5440 }
5441 }
5442 } /* end of if(! StateIsDetached(cm, v)) */
5443 } /* end of if IR/MR state */
5444 else if(cm->sttype[v] == MP_st) {
5445 /* MP states cannot self transit, this means that all cells in
5446 * alpha[v] are independent of each other, only depending on
5447 * alpha[y] for previously calc'ed y. We can do the for loops
5448 * in any nesting order, this implementation does what I think
5449 * is most efficient: for y { for j { for d { } } }
5450 */
5451 if(do_J_v || do_L_v || do_R_v) {
5452 for (yctr = 0; yctr < cm->cnum[v]; yctr++) {
5453 yoffset = (yctr + nins_v) % cm->cnum[v]; /* special y ordering for TrOptAcc, consider consensus state first, not inserts */
5454 y = cm->cfirst[v] + yoffset;
5455 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
5456 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
5457 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
5458
5459 if(do_J_v && do_J_y) {
5460 jn = ESL_MAX(jmin[v], jmin[y]+sdr);
5461 jx = ESL_MIN(jmax[v], jmax[y]+sdr);
5462 jpn = jn - jmin[v];
5463 jpx = jx - jmin[v];
5464 jp_y_sdr = jn - jmin[y] - sdr;
5465 for (jp_v = jpn; jp_v <= jpx; jp_v++, jp_y_sdr++) {
5466 ESL_DASSERT1((jp_v >= 0 && jp_v <= (jmax[v]-jmin[v])));
5467 ESL_DASSERT1((jp_y_sdr >= 0 && jp_y_sdr <= (jmax[y]-jmin[y])));
5468
5469 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y_sdr] + sd);
5470 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y_sdr] + sd);
5471 dpn = dn - hdmin[v][jp_v];
5472 dpx = dx - hdmin[v][jp_v];
5473 dp_y_sd = dn - hdmin[y][jp_y_sdr] - sd;
5474
5475 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y_sd++) {
5476 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
5477 ESL_DASSERT1((dp_y_sd >= 0 && dp_y_sd <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
5478 if((sc = Jalpha[y][jp_y_sdr][dp_y_sd]) > Jalpha[v][jp_v][dp_v]) {
5479 Jalpha[v][jp_v][dp_v] = sc;
5480 Jyshadow[v][jp_v][dp_v] = yoffset + TRMODE_J_OFFSET;
5481 }
5482 }
5483 }
5484 }
5485 if(do_L_v && (do_J_y || do_L_y)) {
5486 /* note we use 'j' and 'd_sdl' not 'j_sdr' for 'd_sd' for L */
5487 jn = ESL_MAX(jmin[v], jmin[y]);
5488 jx = ESL_MIN(jmax[v], jmax[y]);
5489 jpn = jn - jmin[v];
5490 jpx = jx - jmin[v];
5491 jp_y = jn - jmin[y];
5492 for (jp_v = jpn; jp_v <= jpx; jp_v++, jp_y++) {
5493 ESL_DASSERT1((jp_v >= 0 && jp_v <= (jmax[v]-jmin[v])));
5494 ESL_DASSERT1((jp_y >= 0 && jp_y <= (jmax[y]-jmin[y])));
5495
5496 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y] + sdl);
5497 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y] + sdl);
5498 dpn = dn - hdmin[v][jp_v];
5499 dpx = dx - hdmin[v][jp_v];
5500
5501 if (do_J_y) {
5502 dp_y_sdl = dn - hdmin[y][jp_y] - sdl;
5503 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y_sdl++) {
5504 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
5505 ESL_DASSERT1((dp_y_sdl >= 0 && dp_y_sdl <= (hdmax[y][jp_y] - hdmin[y][jp_y])));
5506 if((sc = Jalpha[y][jp_y][dp_y_sdl]) > Lalpha[v][jp_v][dp_v]) {
5507 Lalpha[v][jp_v][dp_v] = sc;
5508 Lyshadow[v][jp_v][dp_v] = yoffset + TRMODE_J_OFFSET;
5509 }
5510 }
5511 }
5512 if (do_L_y) {
5513 dp_y_sdl = dn - hdmin[y][jp_y] - sdl;
5514 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y_sdl++) {
5515 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
5516 ESL_DASSERT1((dp_y_sdl >= 0 && dp_y_sdl <= (hdmax[y][jp_y] - hdmin[y][jp_y])));
5517 if((sc = Lalpha[y][jp_y][dp_y_sdl]) > Lalpha[v][jp_v][dp_v]) {
5518 Lalpha[v][jp_v][dp_v] = sc;
5519 Lyshadow[v][jp_v][dp_v] = yoffset + TRMODE_L_OFFSET;
5520 }
5521 }
5522 }
5523 }
5524 }
5525 if(do_R_v && (do_J_y || do_R_y)) {
5526 /* note we use 'd_sdr' not 'd_sd' for R, plus minimum d is sdr (1) */
5527 jn = ESL_MAX(jmin[v], jmin[y]+sdr);
5528 jx = ESL_MIN(jmax[v], jmax[y]+sdr);
5529 jpn = jn - jmin[v];
5530 jpx = jx - jmin[v];
5531 jp_y_sdr = jn - jmin[y] - sdr;
5532 for (jp_v = jpn; jp_v <= jpx; jp_v++, jp_y_sdr++) {
5533 ESL_DASSERT1((jp_v >= 0 && jp_v <= (jmax[v]-jmin[v])));
5534 ESL_DASSERT1((jp_y_sdr >= 0 && jp_y_sdr <= (jmax[y]-jmin[y])));
5535
5536 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y_sdr] + sdr);
5537 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y_sdr] + sdr);
5538 dpn = dn - hdmin[v][jp_v];
5539 dpx = dx - hdmin[v][jp_v];
5540
5541 if (do_J_y) {
5542 dp_y_sdr = dn - hdmin[y][jp_y_sdr] - sdr;
5543 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y_sdr++) {
5544 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
5545 ESL_DASSERT1((dp_y_sdr >= 0 && dp_y_sdr <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
5546 if((sc = Jalpha[y][jp_y_sdr][dp_y_sdr]) > Ralpha[v][jp_v][dp_v]) {
5547 Ralpha[v][jp_v][dp_v] = sc;
5548 Ryshadow[v][jp_v][dp_v] = yoffset + TRMODE_J_OFFSET;
5549 }
5550 }
5551 }
5552 if (do_R_y) {
5553 dp_y_sdr = dn - hdmin[y][jp_y_sdr] - sdr;
5554 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y_sdr++) {
5555 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
5556 ESL_DASSERT1((dp_y_sdr >= 0 && dp_y_sdr <= (hdmax[y][jp_y_sdr] - hdmin[y][jp_y_sdr])));
5557 if((sc = Ralpha[y][jp_y_sdr][dp_y_sdr]) > Ralpha[v][jp_v][dp_v]) {
5558 Ralpha[v][jp_v][dp_v] = sc;
5559 Ryshadow[v][jp_v][dp_v] = yoffset + TRMODE_R_OFFSET;
5560 }
5561 }
5562 }
5563 }
5564 }
5565 }
5566 }
5567 /* add in emission score */
5568 if(do_J_v) {
5569 for (j = jmin[v]; j <= jmax[v]; j++) {
5570 jp_v = j - jmin[v];
5571 i = j - hdmin[v][jp_v] + 1;
5572 ip_v = i - imin[v];
5573 for (d = hdmin[v][jp_v], dp_v = 0; d <= hdmax[v][jp_v]; d++, dp_v++, ip_v--) {
5574 if(d >= 2) {
5575 Jalpha[v][jp_v][dp_v] = FLogsum(Jalpha[v][jp_v][dp_v], FLogsum(Jl_pp[v][ip_v], Jr_pp[v][jp_v]));
5576 }
5577 else {
5578 Jalpha[v][jp_v][dp_v] = IMPOSSIBLE;
5579 }
5580 }
5581 }
5582 }
5583 if(do_L_v) {
5584 for (j = jmin[v]; j <= jmax[v]; j++) {
5585 jp_v = j - jmin[v];
5586 i = j - hdmin[v][jp_v] + 1;
5587 ip_v = i - imin[v];
5588 for (d = hdmin[v][jp_v], dp_v = 0; d <= hdmax[v][jp_v]; d++, dp_v++, ip_v--) {
5589 if(d >= 2) {
5590 Lalpha[v][jp_v][dp_v] = FLogsum(Lalpha[v][jp_v][dp_v], Ll_pp[v][ip_v]);
5591 }
5592 else {
5593 Lalpha[v][jp_v][dp_v] = Ll_pp[v][ip_v];
5594 Lyshadow[v][jp_v][dp_v] = USED_TRUNC_END;
5595 }
5596 }
5597 }
5598 }
5599 if(do_R_v) {
5600 for (j = jmin[v]; j <= jmax[v]; j++) {
5601 jp_v = j - jmin[v];
5602 for (d = hdmin[v][jp_v], dp_v = 0; d <= hdmax[v][jp_v]; d++, dp_v++) {
5603 if(d >= 2) {
5604 Ralpha[v][jp_v][dp_v] = FLogsum(Ralpha[v][jp_v][dp_v], Rr_pp[v][jp_v]);
5605 }
5606 else {
5607 Ralpha[v][jp_v][dp_v] = Rr_pp[v][jp_v];
5608 Ryshadow[v][jp_v][dp_v] = USED_TRUNC_END;
5609 }
5610 }
5611 }
5612 }
5613 /* ensure all cells are >= IMPOSSIBLE */
5614 if(do_J_v) {
5615 for (j = jmin[v]; j <= jmax[v]; j++) {
5616 jp_v = j - jmin[v];
5617 for (dp_v = 0; dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v]); dp_v++, d++) {
5618 Jalpha[v][jp_v][dp_v] = ESL_MAX(Jalpha[v][jp_v][dp_v], IMPOSSIBLE);
5619 }
5620 /* special case if local ends are off: explicitly disallow transitions to EL that require EL emissions
5621 * (we do allow an 'illegal' transition to EL in OptAcc but only if no EL emissions are req'd)
5622 */
5623 if(! have_el) {
5624 d = hdmin[v][jp_v];
5625 for (dp_v = 0; dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v]); dp_v++) {
5626 if(Jyshadow[v][jp_v][dp_v] == USED_EL && d > sd) Jalpha[v][jp_v][dp_v] = IMPOSSIBLE;
5627 d++;
5628 }
5629 }
5630 }
5631 }
5632 if(do_L_v) {
5633 for (j = jmin[v]; j <= jmax[v]; j++) {
5634 jp_v = j - jmin[v];
5635 for (dp_v = 0; dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v]); dp_v++) {
5636 Lalpha[v][jp_v][dp_v] = ESL_MAX(Lalpha[v][jp_v][dp_v], IMPOSSIBLE);
5637 }
5638 /* special case if local ends are off: explicitly disallow transitions to EL that require EL emissions
5639 * (we do allow an 'illegal' transition to EL in OptAcc but only if no EL emissions are req'd)
5640 */
5641 if(! have_el) {
5642 d = hdmin[v][jp_v];
5643 for (dp_v = 0; dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v]); dp_v++) {
5644 if(Lyshadow[v][jp_v][dp_v] == USED_EL && d > sdl) Lalpha[v][jp_v][dp_v] = IMPOSSIBLE;
5645 d++;
5646 }
5647 }
5648 }
5649 }
5650 if(do_R_v) {
5651 for (j = jmin[v]; j <= jmax[v]; j++) {
5652 jp_v = j - jmin[v];
5653 for (dp_v = 0; dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v]); dp_v++) {
5654 Ralpha[v][jp_v][dp_v] = ESL_MAX(Ralpha[v][jp_v][dp_v], IMPOSSIBLE);
5655 }
5656 /* special case if local ends are off: explicitly disallow transitions to EL that require EL emissions
5657 * (we do allow an 'illegal' transition to EL in OptAcc but only if no EL emissions are req'd)
5658 */
5659 if(! have_el) {
5660 d = hdmin[v][jp_v];
5661 for (dp_v = 0; dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v]); dp_v++) {
5662 if(Ryshadow[v][jp_v][dp_v] == USED_EL && d > sdr) Ralpha[v][jp_v][dp_v] = IMPOSSIBLE;
5663 d++;
5664 }
5665 }
5666 }
5667 }
5668 }
5669 else if(cm->sttype[v] != B_st) { /* entered if state v is D or S */
5670 /* D, S states cannot self transit, this means that all cells in
5671 * alpha[v] are independent of each other, only depending on
5672 * alpha[y] for previously calc'ed y. We can do the for loops
5673 * in any nesting order, this implementation does what I think
5674 * is most efficient: for y { for j { for d { } } }
5675 */
5676 if(do_J_v || do_L_v || do_R_v) {
5677 for (yctr = 0; yctr < cm->cnum[v]; yctr++) {
5678 yoffset = (yctr + nins_v) % cm->cnum[v]; /* special y ordering for TrOptAcc, consider consensus state first, not inserts */
5679 y = cm->cfirst[v] + yoffset;
5680 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
5681 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
5682 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
5683
5684 /*printf("v: %4d y: %4d yoffset: %4d\n", v, y, yoffset);*/
5685 if(do_J_v && do_J_y) {
5686 jn = ESL_MAX(jmin[v], jmin[y]);
5687 jx = ESL_MIN(jmax[v], jmax[y]);
5688 jpn = jn - jmin[v];
5689 jpx = jx - jmin[v];
5690 jp_y = jn - jmin[y];
5691
5692 for (jp_v = jpn; jp_v <= jpx; jp_v++, jp_y++) {
5693 ESL_DASSERT1((jp_v >= 0 && jp_v <= (jmax[v]-jmin[v])));
5694 ESL_DASSERT1((jp_y >= 0 && jp_y <= (jmax[y]-jmin[y])));
5695 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y]);
5696 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y]);
5697 dpn = dn - hdmin[v][jp_v];
5698 dpx = dx - hdmin[v][jp_v];
5699 dp_y = dn - hdmin[y][jp_y];
5700
5701 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y++) {
5702 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
5703 ESL_DASSERT1((dp_y >= 0 && dp_y <= (hdmax[y][jp_y] - hdmin[y][jp_y])));
5704 if((sc = Jalpha[y][jp_y][dp_y]) > Jalpha[v][jp_v][dp_v]) {
5705 Jalpha[v][jp_v][dp_v] = sc;
5706 Jyshadow[v][jp_v][dp_v] = yoffset + TRMODE_J_OFFSET;
5707 }
5708 }
5709 }
5710 }
5711 if(do_L_v && do_L_y) {
5712 jn = ESL_MAX(jmin[v], jmin[y]);
5713 jx = ESL_MIN(jmax[v], jmax[y]);
5714 jpn = jn - jmin[v];
5715 jpx = jx - jmin[v];
5716 jp_y = jn - jmin[y];
5717
5718 for (jp_v = jpn; jp_v <= jpx; jp_v++, jp_y++) {
5719 ESL_DASSERT1((jp_v >= 0 && jp_v <= (jmax[v]-jmin[v])));
5720 ESL_DASSERT1((jp_y >= 0 && jp_y <= (jmax[y]-jmin[y])));
5721 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y]);
5722 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y]);
5723 dpn = dn - hdmin[v][jp_v];
5724 dpx = dx - hdmin[v][jp_v];
5725 dp_y = dn - hdmin[y][jp_y];
5726
5727 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y++) {
5728 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
5729 ESL_DASSERT1((dp_y >= 0 && dp_y <= (hdmax[y][jp_y] - hdmin[y][jp_y])));
5730 if((sc = Lalpha[y][jp_y][dp_y]) > Lalpha[v][jp_v][dp_v]) {
5731 Lalpha[v][jp_v][dp_v] = sc;
5732 Lyshadow[v][jp_v][dp_v] = yoffset + TRMODE_L_OFFSET;
5733 }
5734 }
5735 }
5736 }
5737 /* a couple of special considerations for d == 0 */
5738 if(do_L_v) {
5739 jn = jmin[v];
5740 jx = jmax[v];
5741 jpn = jn - jmin[v];
5742 jpx = jx - jmin[v];
5743 for (jp_v = jpn; jp_v <= jpx; jp_v++) {
5744 /* an easy to overlook case: if d == 0, ensure L value is IMPOSSIBLE */
5745 if(hdmin[v][jp_v] == 0) {
5746 Lalpha[v][jp_v][0] = IMPOSSIBLE;
5747 /* And another special case for BEGL_S states,
5748 * reset shadow matrix values for d == 0 (which were
5749 * initialized to USED_EL above), even though the score of
5750 * these cells is impossible we may use them as a
5751 * zero-length left half of a BIF_B subtree during
5752 * construction of the parsetree.
5753 */
5754 if(cm->sttype[v] == S_st) Lyshadow[v][jp_v][0] = USED_TRUNC_END;
5755 }
5756 }
5757 }
5758 if(do_R_v && do_R_y) {
5759 jn = ESL_MAX(jmin[v], jmin[y]);
5760 jx = ESL_MIN(jmax[v], jmax[y]);
5761 jpn = jn - jmin[v];
5762 jpx = jx - jmin[v];
5763 jp_y = jn - jmin[y];
5764
5765 for (jp_v = jpn; jp_v <= jpx; jp_v++, jp_y++) {
5766 ESL_DASSERT1((jp_v >= 0 && jp_v <= (jmax[v]-jmin[v])));
5767 ESL_DASSERT1((jp_y >= 0 && jp_y <= (jmax[y]-jmin[y])));
5768 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y]);
5769 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y]);
5770 dpn = dn - hdmin[v][jp_v];
5771 dpx = dx - hdmin[v][jp_v];
5772 dp_y = dn - hdmin[y][jp_y];
5773
5774 for (dp_v = dpn; dp_v <= dpx; dp_v++, dp_y++) {
5775 ESL_DASSERT1((dp_v >= 0 && dp_v <= (hdmax[v][jp_v] - hdmin[v][jp_v])));
5776 ESL_DASSERT1((dp_y >= 0 && dp_y <= (hdmax[y][jp_y] - hdmin[y][jp_y])));
5777 if((sc = Ralpha[y][jp_y][dp_y]) > Ralpha[v][jp_v][dp_v]) {
5778 Ralpha[v][jp_v][dp_v] = sc;
5779 Ryshadow[v][jp_v][dp_v] = yoffset + TRMODE_R_OFFSET;
5780 }
5781 }
5782 }
5783 }
5784 /* a couple of special considerations for d == 0 */
5785 if(do_R_v) {
5786 jn = jmin[v];
5787 jx = jmax[v];
5788 jpn = jn - jmin[v];
5789 jpx = jx - jmin[v];
5790 for (jp_v = jpn; jp_v <= jpx; jp_v++) {
5791 /* an easy to overlook case: if d == 0, ensure R value is IMPOSSIBLE */
5792 if(hdmin[v][jp_v] == 0) {
5793 Ralpha[v][jp_v][0] = IMPOSSIBLE;
5794 /* And another special case for BEGL_S states,
5795 * reset shadow matrix values for d == 0 (which were
5796 * initialized to USED_EL above), even though the score of
5797 * these cells is impossible we may use them as a
5798 * zero-length left half of a BIF_B subtree during
5799 * construction of the parsetree.
5800 */
5801 if(cm->sttype[v] == S_st) Ryshadow[v][jp_v][0] = USED_TRUNC_END;
5802 }
5803 }
5804 }
5805 }
5806 }
5807 /* no emission score to add */
5808 } /* end of 'else if(cm->sttype[v] != B_st)' which is entered for S and D states */
5809 else { /* B_st */
5810 if(do_J_v || do_L_v || do_R_v || do_T_v) {
5811 y = cm->cfirst[v]; /* left subtree */
5812 z = cm->cnum[v]; /* right subtree */
5813
5814 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
5815 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
5816 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
5817
5818 do_J_z = cp9b->Jvalid[z] ? TRUE : FALSE;
5819 do_L_z = cp9b->Lvalid[z] && fill_L ? TRUE : FALSE;
5820 do_R_z = cp9b->Rvalid[z] && fill_R ? TRUE : FALSE;
5821
5822 /* Any valid j must be within both state v and state z's j band
5823 * I think jmin[v] <= jmin[z] is guaranteed by the way bands are
5824 * constructed, but we'll check anyway.
5825 */
5826 jn = (jmin[v] > jmin[z]) ? jmin[v] : jmin[z];
5827 jx = (jmax[v] < jmax[z]) ? jmax[v] : jmax[z];
5828 /* the main j loop */
5829 for (j = jn; j <= jx; j++) {
5830 jp_v = j - jmin[v];
5831 jp_y = j - jmin[y];
5832 jp_z = j - jmin[z];
5833 kn = ((j-jmax[y]) > (hdmin[z][jp_z])) ? (j-jmax[y]) : hdmin[z][jp_z];
5834 kn = ESL_MAX(kn, 0); /* kn must be non-negative, added with fix to bug i36 */
5835 /* kn satisfies inequalities (1) and (3) (listed below)*/
5836 kx = ( jp_y < (hdmax[z][jp_z])) ? jp_y : hdmax[z][jp_z];
5837 /* kn satisfies inequalities (2) and (4) (listed below)*/
5838 i = j - hdmin[v][jp_v] + 1;
5839 for (d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++, i--) {
5840 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha w/mem eff bands */
5841
5842 /* Find the first k value that implies a valid cell in the {J,L,R} matrix y and z decks.
5843 * This k must satisfy the following 6 inequalities (some may be redundant):
5844 * (1) k >= j-jmax[y];
5845 * (2) k <= j-jmin[y];
5846 * 1 and 2 guarantee (j-k) is within state y's j band
5847 *
5848 * (3) k >= hdmin[z][j-jmin[z]];
5849 * (4) k <= hdmax[z][j-jmin[z]];
5850 * 3 and 4 guarantee k is within z's j=(j), d band
5851 *
5852 * (5) k >= d-hdmax[y][j-jmin[y]-k];
5853 * (6) k <= d-hdmin[y][j-jmin[y]-k];
5854 * 5 and 6 guarantee (d-k) is within state y's j=(j-k) d band
5855 *
5856 * kn and kx were set above (outside (for (dp_v...) loop) that
5857 * satisfy 1-4 (b/c 1-4 are d-independent and k-independent)
5858 * RHS of inequalities 5 and 6 are dependent on k, so we check
5859 * for these within the next for loop.
5860 *
5861 * To update a cell in the T matrix with a sum of an R matrix value for y
5862 * and a L matrix value for z, there are 2 additional inequalities to satisfy:
5863 * (7) k != 0
5864 * (8) k != d
5865 * We ensure 7 and 8 in the loop below.
5866 */
5867 for(k = kn; k <= kx; k++) {
5868 if((k >= d - hdmax[y][jp_y-k]) && k <= d - hdmin[y][jp_y-k]) {
5869 /* for current k, all 6 inequalities have been satisified
5870 * so we know the cells corresponding to the platonic
5871 * matrix cells alpha[v][j][d], alpha[y][j-k][d-k], and
5872 * alpha[z][j][k] are all within the bands. These
5873 * cells correspond to alpha[v][jp_v][dp_v],
5874 * alpha[y][jp_y-k][d-hdmin[jp_y-k]-k],
5875 * and alpha[z][jp_z][k-hdmin[jp_z]];
5876 */
5877 kp_z = k-hdmin[z][jp_z];
5878 dp_y = d-hdmin[y][jp_y-k];
5879 if(do_J_v && do_J_y && do_J_z &&
5880 (NOT_IMPOSSIBLE(Jalpha[y][jp_y-k][dp_y-k]) || d == k) && /* left subtree is not-IMPOSSIBLE or IMPOSSIBLE w/length 0 (latter is ok b/c only emits contribute to score) */
5881 (NOT_IMPOSSIBLE(Jalpha[z][jp_z][kp_z]) || k == 0) && /* right subtree is not-IMPOSSIBLE or IMPOSSIBLE w/length 0 (latter is ok b/c only emits contribute to score) */
5882 (sc = FLogsum(Jalpha[y][jp_y-k][dp_y-k], Jalpha[z][jp_z][kp_z])) > Jalpha[v][jp_v][dp_v]) {
5883 Jalpha[v][jp_v][dp_v] = sc;
5884 Jkshadow[v][jp_v][dp_v] = k;
5885 }
5886 if(do_L_v && do_J_y && do_L_z &&
5887 (NOT_IMPOSSIBLE(Jalpha[y][jp_y-k][dp_y-k]) || d == k) && /* left subtree is not-IMPOSSIBLE or IMPOSSIBLE w/length 0 (latter is ok b/c only emits contribute to score) */
5888 (NOT_IMPOSSIBLE(Lalpha[z][jp_z][kp_z]) || k == 0) && /* right subtree is not-IMPOSSIBLE or IMPOSSIBLE w/length 0 (latter is ok b/c only emits contribute to score) */
5889 (sc = FLogsum(Jalpha[y][jp_y-k][dp_y-k], Lalpha[z][jp_z][kp_z])) > Lalpha[v][jp_v][dp_v]) {
5890 Lalpha[v][jp_v][dp_v] = sc;
5891 Lkshadow[v][jp_v][dp_v] = k;
5892 }
5893 if(do_R_v && do_R_y && do_J_z &&
5894 (NOT_IMPOSSIBLE(Ralpha[y][jp_y-k][dp_y-k]) || d == k) && /* left subtree is not-IMPOSSIBLE or IMPOSSIBLE w/length 0 (latter is ok b/c only emits contribute to score) */
5895 (NOT_IMPOSSIBLE(Jalpha[z][jp_z][kp_z]) || k == 0) && /* right subtree is not-IMPOSSIBLE or IMPOSSIBLE w/length 0 (latter is ok b/c only emits contribute to score) */
5896 (sc = FLogsum(Ralpha[y][jp_y-k][dp_y-k], Jalpha[z][jp_z][kp_z])) > Ralpha[v][jp_v][dp_v]) {
5897 Ralpha[v][jp_v][dp_v] = sc;
5898 Rkshadow[v][jp_v][dp_v] = k;
5899 }
5900 if((k != 0) && (k != d)) { /* special boundary case for T matrix */
5901 if(do_T_v && do_R_y && do_L_z &&
5902 (NOT_IMPOSSIBLE(Ralpha[y][jp_y-k][dp_y-k])) && /* left subtree is not-IMPOSSIBLE (no check for 'd==k' b/c of special T mx boundary case (see 3 lines up)) */
5903 (NOT_IMPOSSIBLE(Lalpha[z][jp_z][kp_z])) && /* right subtree is not-IMPOSSIBLE (no check for 'k==0' b/c of special T mx boundary case (see 3 lines up)) */
5904 (sc = FLogsum(Ralpha[y][jp_y-k][dp_y-k], Lalpha[z][jp_z][kp_z])) > Talpha[v][jp_v][dp_v]) {
5905 Talpha[v][jp_v][dp_v] = sc;
5906 Tkshadow[v][jp_v][dp_v] = k;
5907 }
5908 }
5909 }
5910 }
5911 }
5912 }
5913 }
5914 /* two additional special cases in trCYK (these are not in standard CYK).
5915 * we do these in their own for(j.. { for(d.. { } } loops b/c one
5916 * is independent of z, the other of y, unlike the above loop which is dependent
5917 * on both.
5918 */
5919 if(do_L_v && (do_J_y || do_L_y)) {
5920 jn = ESL_MAX(jmin[v], jmin[y]);
5921 jx = ESL_MIN(jmax[v], jmax[y]);
5922 for (j = jn; j <= jx; j++) {
5923 jp_v = j - jmin[v];
5924 jp_y = j - jmin[y];
5925 ESL_DASSERT1((j >= jmin[v] && j <= jmax[v]));
5926 ESL_DASSERT1((j >= jmin[y] && j <= jmax[y]));
5927 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y]);
5928 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y]);
5929 for(d = dn; d <= dx; d++) {
5930 dp_v = d - hdmin[v][jp_v];
5931 dp_y = d - hdmin[y][jp_y];
5932 ESL_DASSERT1((d >= hdmin[v][jp_v] && d <= hdmax[v][jp_v]));
5933 ESL_DASSERT1((d >= hdmin[y][jp_y] && d <= hdmax[y][jp_y]));
5934 if(do_J_y && (sc = Jalpha[y][jp_y][dp_y]) > Lalpha[v][jp_v][dp_v]) {
5935 Lalpha[v][jp_v][dp_v] = sc;
5936 Lkshadow[v][jp_v][dp_v] = 0; /* k == 0 for this case, full sequence is on left */
5937 Lkmode[v][jp_v][dp_v] = TRMODE_J;
5938 }
5939 if(do_L_y && (sc = Lalpha[y][jp_y][dp_y]) > Lalpha[v][jp_v][dp_v]) {
5940 Lalpha[v][jp_v][dp_v] = sc;
5941 Lkshadow[v][jp_v][dp_v] = 0; /* k == 0 for this case, full sequence is on left */
5942 Lkmode[v][jp_v][dp_v] = TRMODE_L;
5943 }
5944 }
5945 }
5946 }
5947 if(do_R_v && (do_J_z || do_R_z)) {
5948 jn = ESL_MAX(jmin[v], jmin[z]);
5949 jx = ESL_MIN(jmax[v], jmax[z]);
5950 for (j = jn; j <= jx; j++) {
5951 jp_v = j - jmin[v];
5952 jp_z = j - jmin[z];
5953 ESL_DASSERT1((j >= jmin[v] && j <= jmax[v]));
5954 ESL_DASSERT1((j >= jmin[z] && j <= jmax[z]));
5955 dn = ESL_MAX(hdmin[v][jp_v], hdmin[z][jp_z]);
5956 dx = ESL_MIN(hdmax[v][jp_v], hdmax[z][jp_z]);
5957 for(d = dn; d <= dx; d++) {
5958 dp_v = d - hdmin[v][jp_v];
5959 dp_z = d - hdmin[z][jp_z];
5960 ESL_DASSERT1((d >= hdmin[v][jp_v] && d <= hdmax[v][jp_v]));
5961 ESL_DASSERT1((d >= hdmin[z][jp_z] && d <= hdmax[z][jp_z]));
5962 if(do_J_z && (sc = Jalpha[z][jp_z][dp_z]) > Ralpha[v][jp_v][dp_v]) {
5963 Ralpha[v][jp_v][dp_v] = sc;
5964 Rkshadow[v][jp_v][dp_v] = d; /* k == d for this case, full sequence is on right */
5965 Rkmode[v][jp_v][dp_v] = TRMODE_J;
5966 }
5967 if(do_R_z && (sc = Ralpha[z][jp_z][dp_z]) > Ralpha[v][jp_v][dp_v]) {
5968 Ralpha[v][jp_v][dp_v] = sc;
5969 Rkshadow[v][jp_v][dp_v] = d; /* k == d for this case, full sequence is on right */
5970 Rkmode[v][jp_v][dp_v] = TRMODE_R;
5971 }
5972 }
5973 }
5974 }
5975 } /* end of 'else' that is entered if v is a B st */
5976
5977 /* Now handle from ROOT_S, state 0. So far we haven't touched
5978 * the {J,L,R,T}alpha[0] decks at all since initialization and here
5979 * we'll only update at most 1 cell in each, the one pertaining
5980 * to a full alignment [0][L][L].
5981 *
5982 * In truncated alignment the only way out of ROOT_S in local or
5983 * global mode is via a 'truncated begin' with a score (penalty)
5984 * from cm->trp into any emitting state. The penalty was
5985 * calculated in cm_tr_penalties_Create() and differs depending on
5986 * whether we are in local or global mode and the value of
5987 * 'pty_idx' which was passed in.
5988 *
5989 * Since we're in OptAcc alignment we don't assess the
5990 * penalty but we still need to know if it's non-IMPOSSIBLE,
5991 * to know which states we're allowed to do a truncated
5992 * begin into.
5993 */
5994 if(L >= jmin[v] && L <= jmax[v]) {
5995 jp_v = L - jmin[v];
5996 Lp = L - hdmin[v][jp_v];
5997 if(L >= hdmin[v][jp_v] && L <= hdmax[v][jp_v]) {
5998 /* If we get here alpha[v][jp_v][Lp] and alpha[0][jp_0][Lp_0]
5999 * are valid cells in the banded alpha matrix, corresponding to
6000 * alpha[v][L][L] and alpha[0][L][L] in the platonic matrix.
6001 * (We've already made sure alpha[0][jp_0][Lp_0] was valid
6002 * at the beginning of the function.)
6003 */
6004 trpenalty = have_el ? cm->trp->l_ptyAA[pty_idx][v] : cm->trp->g_ptyAA[pty_idx][v];
6005 if(NOT_IMPOSSIBLE(trpenalty)) {
6006 if(preset_mode == TRMODE_J && do_J_v) {
6007 if(Jalpha[v][jp_v][Lp] > Jalpha[0][jp_0][Lp_0]) {
6008 Jalpha[0][jp_0][Lp_0] = Jalpha[v][jp_v][Lp];
6009 b = v;
6010 }
6011 }
6012 if(preset_mode == TRMODE_L && do_L_v) {
6013 if(Lalpha[v][jp_v][Lp] > Lalpha[0][jp_0][Lp_0]) {
6014 Lalpha[0][jp_0][Lp_0] = Lalpha[v][jp_v][Lp];
6015 b = v;
6016 }
6017 }
6018 if(preset_mode == TRMODE_R && do_R_v) {
6019 if(Ralpha[v][jp_v][Lp] > Ralpha[0][jp_0][Lp_0]) {
6020 Ralpha[0][jp_0][Lp_0] = Ralpha[v][jp_v][Lp];
6021 b = v;
6022 }
6023 }
6024 if(preset_mode == TRMODE_T && do_T_v && cm->sttype[v] == B_st) {
6025 if(Talpha[v][jp_v][Lp] > Talpha[0][jp_0][Lp_0]) {
6026 Talpha[0][jp_0][Lp_0] = Talpha[v][jp_v][Lp];
6027 b = v;
6028 }
6029 }
6030 }
6031 }
6032 }
6033 } /* end loop for (v = cm->M-1; v > 0; v--) */
6034
6035 /* all valid alignments must use a truncated begin */
6036 if ( cp9b->Jvalid[0]) Jyshadow[0][jp_0][Lp_0] = USED_TRUNC_BEGIN;
6037 if (fill_L && cp9b->Lvalid[0]) Lyshadow[0][jp_0][Lp_0] = USED_TRUNC_BEGIN;
6038 if (fill_R && cp9b->Rvalid[0]) Ryshadow[0][jp_0][Lp_0] = USED_TRUNC_BEGIN;
6039 /* Tyshadow[0] doesn't exist, caller must know how to deal */
6040
6041 if (preset_mode == TRMODE_J) sc = Jalpha[0][jp_0][Lp_0];
6042 if (preset_mode == TRMODE_L) sc = Lalpha[0][jp_0][Lp_0];
6043 if (preset_mode == TRMODE_R) sc = Ralpha[0][jp_0][Lp_0];
6044 if (preset_mode == TRMODE_T) sc = Talpha[0][jp_0][Lp_0];
6045
6046 /* convert sc, a log probability, into the average posterior probability of all L aligned residues */
6047 pp = sreEXP2(sc) / (float) L;
6048
6049 #if eslDEBUGLEVEL >= 2
6050 /* Uncomment to dump matrix to file. Careful...this could be very large. */
6051 /* FILE *fp1; fp1 = fopen("tmp.tru_oahbmx", "w"); cm_tr_hb_mx_Dump(fp1, mx, preset_mode, TRUE); fclose(fp1); */
6052 /* FILE *fp2; fp2 = fopen("tmp.tru_oahbshmx", "w"); cm_tr_hb_shadow_mx_Dump(fp2, cm, shmx, preset_mode, TRUE); fclose(fp2); */
6053 #endif
6054
6055 if(ret_b != NULL) *ret_b = b;
6056 if(ret_pp != NULL) *ret_pp = pp;
6057
6058 free(yvalidA);
6059
6060 ESL_DPRINTF1(("#DEBUG: cm_TrOptAccAlignHB() return pp: %f\n", pp));
6061 return eslOK;
6062
6063 ERROR:
6064 ESL_FAIL(status, errbuf, "out of memory");
6065 return status; /* NEVERREACHED */
6066 }
6067
6068 /* Function: cm_TrCYKOutsideAlign()
6069 * Date: EPN, Wed Sep 14 14:20:20 2011
6070 *
6071 * Purpose: Run the outside TrCYK algorithm on a target sequence.
6072 * Non-banded version. See cm_TrCYKOutsideAlignHB() for
6073 * the HMM banded version. The full target sequence
6074 * 1..L is aligned.
6075 *
6076 * Very similar to cm_TrOutsideAlign() but calculates
6077 * beta[v][j][d]: log probability of the most likely parse
6078 * that emits 1..i-1 and j+1..L and passes through v at j,d
6079 * (where i = j-d+1) instead of the log of the summed
6080 * probability of all such parses. This means max operations
6081 * are used instead of logsums.
6082 *
6083 * Meaning of cells:
6084 *
6085 * Jbeta[v][j][d]: log prob of the most likely parse that
6086 * emits 1..i-1 and j+1..L and passes through
6087 * v in Joint marginal mode at j,d.
6088 * Lbeta[v][j][d]: log prob of the most likely parse that
6089 * emits 1..i-1 and j+1..L and passes through
6090 * v in Left marginal mode at j,d.
6091 * Rbeta[v][j][d]: log prob of the most likely parse that
6092 * emits 1..i-1 and j+1..L and passes through
6093 * v in Right marginal mode at j,d.
6094 *
6095 * This function complements cm_TrCYKInsideAlign() but is
6096 * mainly useful for testing and reference. It can be used
6097 * with do_check=TRUE to verify that the implementation of
6098 * CYKTrInside and CYKTrOutside are consistent. Because the
6099 * structure of CYKTrInside and TrInside, and CYKTrOutside
6100 * and TrOutside are so similar and the CYK variants are
6101 * easier to debug (because only the optimal parsetree is
6102 * considered instead of all possible parsetrees) this
6103 * function can be useful for finding bugs in Outside. It
6104 * is currently not hooked up to any of the main Infernal
6105 * programs.
6106 *
6107 * Args: cm - the model
6108 * errbuf - char buffer for reporting errors
6109 * dsq - the digitized sequence
6110 * L - length of the dsq to align
6111 * size_limit - max number of Mb for DP matrix, if matrix is bigger return eslERANGE
6112 * do_check - TRUE to attempt to check
6113 * preset_mode - TRMODE_J, TRMODE_L, TRMODE_R, or TRMODE_T, the pre-determined
6114 * alignment mode, we'll only allow alignments in this mode.
6115 * pass_idx - pipeline pass index, indicates what truncation penalty to use
6116 * mx - the dp matrix, grown and filled here
6117 * inscyk_mx - the pre-filled dp matrix from the CYK Inside calculation
6118 * (performed by cm_CYKInsideAlign(), required)
6119 *
6120 * Returns: <eslOK> on success.
6121 *
6122 * Throws: <eslERANGE> if required CM_TR_HB_MX size exceeds <size_limit>
6123 * <eslEMEM> if we run out of memory
6124 * <eslFAIL> if <do_check>==TRUE and we fail a test
6125 */
6126 int
6127 cm_TrCYKOutsideAlign(CM_t *cm, char *errbuf, ESL_DSQ *dsq, int L, float size_limit, char preset_mode, int pass_idx, int do_check, CM_TR_MX *mx, CM_TR_MX *inscyk_mx)
6128 {
6129 int status;
6130 int v,y,z; /* indices for states */
6131 float Jsc,Lsc,Rsc,Tsc; /* a temporary variable holding a float score */
6132 int j,d,i,k; /* indices in sequence dimensions */
6133 float optsc; /* the optimal score from the Inside matrix */
6134 float escore; /* an emission score, tmp variable */
6135 int voffset; /* index of v in t_v(y) transition scores */
6136 int sd; /* StateDelta(cm->sttype[y]) */
6137 int sdl; /* StateLeftDelta(cm->sttype[y] */
6138 int sdr; /* StateRightDelta(cm->sttype[y] */
6139
6140 /* variables used only if do_check */
6141 int fail1_flag = FALSE; /* set to TRUE if do_check and we see a problem in check 1 */
6142 int fail2_flag = FALSE; /* set to TRUE if do_check and we see a problem in check 2 */
6143 int vmax; /* i, offset in the matrix */
6144 float tol; /* tolerance for differences in bit scores */
6145 int *optseen = NULL; /* [1..i..L] TRUE is residue i is accounted for in optimal parse */
6146
6147 /* other variables used in truncated version, but not standard version (not in cm_CYKOutsideAlign()) */
6148 int fill_L, fill_R, fill_T; /* must we fill in the L, R, and T matrices? */
6149 int pty_idx; /* index for truncation penalty, determined by pass_idx */
6150 float trpenalty; /* truncation penalty, differs based on pty_idx and if we're local or global */
6151
6152 /* DP matrix variables */
6153 float ***Jbeta = mx->Jdp; /* pointer to the outside Jbeta DP matrix */
6154 float ***Lbeta = mx->Ldp; /* pointer to the outside Lbeta DP matrix */
6155 float ***Rbeta = mx->Rdp; /* pointer to the outside Rbeta DP matrix */
6156 float ***Tbeta = mx->Tdp; /* pointer to the outside Tbeta DP matrix */
6157
6158 float ***Jalpha = inscyk_mx->Jdp; /* pointer to the precalc'ed inside Jalpha DP matrix */
6159 float ***Lalpha = inscyk_mx->Ldp; /* pointer to the precalc'ed inside Lalpha DP matrix */
6160 float ***Ralpha = inscyk_mx->Rdp; /* pointer to the precalc'ed inside Ralpha DP matrix */
6161 float ***Talpha = inscyk_mx->Tdp; /* pointer to the precalc'ed inside Talpha DP matrix, only used to possibly get optsc */
6162
6163 /* Allocations and initializations */
6164
6165 /* Determine which matrices we need to fill in, based on <preset_mode> */
6166 if (preset_mode != TRMODE_J && preset_mode != TRMODE_L && preset_mode != TRMODE_R && preset_mode != TRMODE_T) ESL_FAIL(eslEINVAL, errbuf, "cm_TrCYKOutsideAlign(): preset_mode is not J, L, R, or T");
6167 if((status = cm_TrFillFromMode(preset_mode, &fill_L, &fill_R, &fill_T)) != eslOK) ESL_FAIL(status, errbuf, "cm_TrCYKOutsideAlign(), bogus mode: %d", preset_mode);
6168
6169 /* Determine the truncation penalty index, from the pass_idx */
6170 if((pty_idx = cm_tr_penalties_IdxForPass(pass_idx)) == -1) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_TrCYKOussideAlign(), unexpected pass idx: %d", pass_idx);
6171
6172 /* grow the matrices based on the current sequence and bands */
6173 if((status = cm_tr_mx_GrowTo(cm, mx, errbuf, L, size_limit)) != eslOK) return status;
6174
6175 /* initialize all cells of the matrix to IMPOSSIBLE */
6176 if(mx->Jncells_valid > 0) esl_vec_FSet(mx->Jdp_mem, mx->Jncells_valid, IMPOSSIBLE);
6177 if(mx->Lncells_valid > 0 && fill_L) esl_vec_FSet(mx->Ldp_mem, mx->Lncells_valid, IMPOSSIBLE);
6178 if(mx->Rncells_valid > 0 && fill_R) esl_vec_FSet(mx->Rdp_mem, mx->Rncells_valid, IMPOSSIBLE);
6179 if(mx->Tncells_valid > 0 && fill_T) esl_vec_FSet(mx->Tdp_mem, mx->Tncells_valid, IMPOSSIBLE);
6180
6181 /* initialize cells in the special ROOT_S deck corresponding to full sequence alignments to 0.0 */
6182 if (preset_mode == TRMODE_J) Jbeta[0][L][L] = 0.; /* a full Joint alignment is outside this cell */
6183 else if(preset_mode == TRMODE_L) Lbeta[0][L][L] = 0.; /* a full Left alignment is outside this cell */
6184 else if(preset_mode == TRMODE_R) Rbeta[0][L][L] = 0.; /* a full Right alignment is outside this cell */
6185 else if(preset_mode == TRMODE_T) Tbeta[0][L][L] = 0.; /* a full Terminal alignment is outside this cell */
6186 else ESL_FAIL(eslEINVAL, errbuf, "cm_TrCYKOutsideAlign() preset_mode %d is invalid", preset_mode);
6187
6188 /* set cells corresponding to legal truncated begin entry states to
6189 * the appropriate penalty. In truncated alignment the only way out
6190 * of ROOT_S in local or global mode is via a 'truncated begin' with
6191 * a score (penalty) from cm->trp into any emitting state. The
6192 * penalty was calculated in cm_tr_penalties_Create() and differs
6193 * depending on whether we are in local or global mode and the value
6194 * of 'pty_idx' which was passed in.
6195 */
6196 for(v = 0; v < cm->M; v++) {
6197 trpenalty = (cm->flags & CMH_LOCAL_BEGIN) ? cm->trp->l_ptyAA[pty_idx][v] : cm->trp->g_ptyAA[pty_idx][v];
6198 if(NOT_IMPOSSIBLE(trpenalty)) {
6199 if(preset_mode == TRMODE_J) Jbeta[v][L][L] = trpenalty; /* a full Joint alignment is outside this cell */
6200 if(preset_mode == TRMODE_L) Lbeta[v][L][L] = trpenalty; /* a full Left alignment is outside this cell */
6201 if(preset_mode == TRMODE_R) Rbeta[v][L][L] = trpenalty; /* a full Right alignment is outside this cell */
6202 if(preset_mode == TRMODE_T && cm->sttype[v] == B_st) {
6203 Tbeta[v][L][L] = trpenalty; /* a full Terminal alignment is outside this cell */
6204 }
6205 }
6206 }
6207
6208 /* main loop down through the decks */
6209 for (v = 1; v < cm->M; v++) { /* start at state 1 because we set all values for ROOT_S state 0 above */
6210 if(! StateIsDetached(cm, v)) {
6211 sd = StateDelta(cm->sttype[v]);
6212 sdr = StateRightDelta(cm->sttype[v]);
6213
6214 if (cm->stid[v] == BEGL_S) { /* BEGL_S */
6215 y = cm->plast[v]; /* the parent bifurcation */
6216 z = cm->cnum[y]; /* the other (right) S state */
6217 for(j = 0; j <= L; j++) {
6218 for (d = 0; d <= j; d++) {
6219 for (k = 0; k <= (L-j); k++) {
6220 Jbeta[v][j][d] = ESL_MAX(Jbeta[v][j][d], Jbeta[y][j+k][d+k] + Jalpha[z][j+k][k]); /* A */
6221 if(fill_L) {
6222 Jbeta[v][j][d] = ESL_MAX(Jbeta[v][j][d], Lbeta[y][j+k][d+k] + Lalpha[z][j+k][k]); /* B */
6223 }
6224 if(fill_R) {
6225 Rbeta[v][j][d] = ESL_MAX(Rbeta[v][j][d], Rbeta[y][j+k][d+k] + Jalpha[z][j+k][k]); /* C */
6226 if(fill_T && fill_L && d == j && (j+k) == L) {
6227 Rbeta[v][j][d] = ESL_MAX(Rbeta[v][j][d], Tbeta[y][j+k][d+k] + Lalpha[z][j+k][k]); /* D */
6228 /* Note: Tbeta[y][j+k==L][d+k==L] will be 0.0 or
6229 * IMPOSSIBLE because it was initialized that
6230 * way. That T cell includes the full target 1..L
6231 * (any valid T alignment must because we must
6232 * account for the full target) rooted at a B state,
6233 * and a transition from that B state to this BEGL_S
6234 * is always probability 1.0.
6235 */
6236 }
6237 }
6238 } /* end of for k loop */
6239 if(fill_L) {
6240 Jbeta[v][j][d] = ESL_MAX(Jbeta[v][j][d], Lbeta[y][j][d]); /* entire sequence on left, no sequence on right, k == 0 */
6241 Lbeta[v][j][d] = ESL_MAX(Lbeta[v][j][d], Lbeta[y][j][d]); /* entire sequence on left, no sequence on right, k == 0 */
6242 }
6243 }
6244 }
6245 } /* end of 'if (cm->stid[v] == BEGL_S */
6246 else if (cm->stid[v] == BEGR_S) {
6247 y = cm->plast[v]; /* the parent bifurcation */
6248 z = cm->cfirst[y]; /* the other (left) S state */
6249 for(j = 0; j <= L; j++) {
6250 for (d = 0; d <= j; d++) {
6251 i = j-d+1;
6252 for (k = 0; k <= (j-d); k++) {
6253 Jbeta[v][j][d] = ESL_MAX(Jbeta[v][j][d], Jbeta[y][j][d+k] + Jalpha[z][j-d][k]); /* A */
6254 if(fill_R) {
6255 Jbeta[v][j][d] = ESL_MAX(Jbeta[v][j][d], Rbeta[y][j][d+k] + Ralpha[z][j-d][k]); /* C */
6256 }
6257 if(fill_L) {
6258 Lbeta[v][j][d] = ESL_MAX(Lbeta[v][j][d], Lbeta[y][j][d+k] + Jalpha[z][j-d][k]); /* B */
6259 if(fill_T && fill_R && k == (i-1) && j == L) {
6260 Lbeta[v][j][d] = ESL_MAX(Lbeta[v][j][d], Tbeta[y][j][d+k] + Ralpha[z][j-d][k]); /* D */
6261 /* Note: Tbeta[y][j==L][d+k==L] will be 0.0 or
6262 * IMPOSSIBLE because it was initialized that
6263 * way. That T cell includes the full target 1..L (any
6264 * valid T alignment must because we must account for
6265 * the full target) rooted at a B state, and a
6266 * transition from that B state to this BEGR_S is
6267 * always probability 1.0.
6268 */
6269 }
6270 }
6271 }
6272 if(fill_R) {
6273 Jbeta[v][j][d] = ESL_MAX(Jbeta[v][j][d], Rbeta[y][j][d]); /* entire sequence on right, no sequence on left, k == 0 */
6274 Rbeta[v][j][d] = ESL_MAX(Rbeta[v][j][d], Rbeta[y][j][d]); /* entire sequence on right, no sequence on left, k == 0 */
6275 }
6276 }
6277 }
6278 } /* end of 'else if (cm->stid[v] == BEGR_S */
6279 else { /* (cm->sttype[v] != BEGL_S && cm->sttype[v] != BEGR_S */
6280 for (j = L; j >= 0; j--) {
6281 i = 1;
6282 for (d = j; d >= 0; d--, i++) {
6283 for (y = cm->plast[v]; y > cm->plast[v]-cm->pnum[v]; y--) {
6284 /* mind the following sneaky if statement: in truncated
6285 * aln, the only way out of state 0 is through a
6286 * truncated begin, which we handled above (search for
6287 * 'trpenalty'). If we're in local mode transitions out
6288 * of 0 will have IMPOSSIBLE scores, but NOT if we're in
6289 * glocal mode, so we need this 'if'.
6290 */
6291 if(y != 0) {
6292 voffset = v - cm->cfirst[y]; /* gotta calculate the transition score index for t_y(v) */
6293 sd = StateDelta(cm->sttype[y]);
6294 sdl = StateLeftDelta(cm->sttype[y]);
6295 sdr = StateRightDelta(cm->sttype[y]);
6296 switch(cm->sttype[y]) {
6297 case MP_st:
6298 if(j != L && d != j) {
6299 escore = cm->oesc[y][dsq[i-1]*cm->abc->Kp+dsq[j+1]];
6300 Jbeta[v][j][d] = ESL_MAX(Jbeta[v][j][d], Jbeta[y][j+sdr][d+sd] + cm->tsc[y][voffset] + escore);
6301 }
6302 if(fill_L && j == L && d != j) { /* only allow transition from L if we haven't emitted any residues rightwise (j==L) */
6303 escore = cm->lmesc[y][dsq[i-1]];
6304 Jbeta[v][j][d] = ESL_MAX(Jbeta[v][j][d], Lbeta[y][j][d+sdl] + cm->tsc[y][voffset] + escore);
6305 Lbeta[v][j][d] = ESL_MAX(Lbeta[v][j][d], Lbeta[y][j][d+sdl] + cm->tsc[y][voffset] + escore);
6306 }
6307 if(fill_R && i == 1 && j != L) { /* only allow transition from R if we haven't emitted any residues leftwise (i==1) */
6308 escore = cm->rmesc[y][dsq[j+1]];
6309 Jbeta[v][j][d] = ESL_MAX(Jbeta[v][j][d], Rbeta[y][j+sdr][d+sdr] + cm->tsc[y][voffset] + escore);
6310 Rbeta[v][j][d] = ESL_MAX(Rbeta[v][j][d], Rbeta[y][j+sdr][d+sdr] + cm->tsc[y][voffset] + escore);
6311 }
6312 break;
6313 case ML_st:
6314 case IL_st:
6315 if (d != j) {
6316 escore = cm->oesc[y][dsq[i-1]];
6317 Jbeta[v][j][d] = ESL_MAX(Jbeta[v][j][d], Jbeta[y][j][d+sd] + cm->tsc[y][voffset] + escore);
6318 if(fill_L) Lbeta[v][j][d] = ESL_MAX(Lbeta[v][j][d], Lbeta[y][j][d+sd] + cm->tsc[y][voffset] + escore);
6319 }
6320 if(fill_R && i == 1 && /* only allow transition from R if we're emitting first residue 1 from y */
6321 v != y) { /* will only happen if v == IL, we don't allow silent self transitions from IL->IL */
6322 Jbeta[v][j][d] = ESL_MAX(Jbeta[v][j][d], Rbeta[y][j][d] + cm->tsc[y][voffset]);
6323 Rbeta[v][j][d] = ESL_MAX(Rbeta[v][j][d], Rbeta[y][j][d] + cm->tsc[y][voffset]);
6324 }
6325 break;
6326 case MR_st:
6327 case IR_st:
6328 if (j != L) {
6329 escore = cm->oesc[y][dsq[j+1]];
6330 Jbeta[v][j][d] = ESL_MAX(Jbeta[v][j][d], Jbeta[y][j+sdr][d+sd] + cm->tsc[y][voffset] + escore);
6331 if(fill_R) Rbeta[v][j][d] = ESL_MAX(Rbeta[v][j][d], Rbeta[y][j+sdr][d+sd] + cm->tsc[y][voffset] + escore);
6332 }
6333 if(fill_L && j == L && /* only allow transition from L if we're emitting final residue L from y */
6334 v != y) { /* will only happen if v == IR, we don't allow silent self transitions from IR->IR */
6335 Jbeta[v][j][d] = ESL_MAX(Jbeta[v][j][d], Lbeta[y][j][d] + cm->tsc[y][voffset]);
6336 Lbeta[v][j][d] = ESL_MAX(Lbeta[v][j][d], Lbeta[y][j][d] + cm->tsc[y][voffset]);
6337 }
6338 break;
6339 case S_st:
6340 case E_st:
6341 case D_st:
6342 Jbeta[v][j][d] = ESL_MAX(Jbeta[v][j][d], Jbeta[y][j][d] + cm->tsc[y][voffset]);
6343 if(fill_L) Lbeta[v][j][d] = ESL_MAX(Lbeta[v][j][d], Lbeta[y][j][d] + cm->tsc[y][voffset]);
6344 if(fill_R) Rbeta[v][j][d] = ESL_MAX(Rbeta[v][j][d], Rbeta[y][j][d] + cm->tsc[y][voffset]);
6345 break;
6346 } /* end of switch(cm->sttype[y] */
6347 } /* end of sneaky if y != 0 */
6348 } /* ends for loop over parent states. we now know beta[v][j][d] for this d */
6349 if (Jbeta[v][j][d] < IMPOSSIBLE) Jbeta[v][j][d] = IMPOSSIBLE;
6350 } /* ends loop over d. We know all beta[v][j][d] in this row j and state v */
6351 } /* end loop over j. We know beta for this whole state */
6352 } /* end of 'else' (if cm->sttype[v] != BEGL_S, BEGR_S) */
6353 } /* end of 'if(! StateIsDetached(cm, v))' */
6354 /* we're done calculating deck v for everything but local ends */
6355
6356 /* deal with local alignment end transitions v->EL J matrix only (EL = deck at M.) */
6357 if ((cm->flags & CMH_LOCAL_END) && NOT_IMPOSSIBLE(cm->endsc[v])) {
6358 sd = StateDelta(cm->sttype[v]); /* note sd is for state v */
6359 sdl = StateLeftDelta(cm->sttype[v]); /* note sdl is for state v */
6360 sdr = StateRightDelta(cm->sttype[v]); /* note sdr is for state v */
6361
6362 for (j = 0; j <= L; j++) {
6363 for (d = 0; d <= j; d++) {
6364 i = j-d+1;
6365 switch (cm->sttype[v]) {
6366 case MP_st:
6367 if (j != L && d != j) {
6368 escore = cm->oesc[v][dsq[i-1]*cm->abc->Kp+dsq[j+1]];
6369 Jbeta[cm->M][j][d] = ESL_MAX(Jbeta[cm->M][j][d], (Jbeta[v][j+sdr][d+sd] + cm->endsc[v] + escore));
6370 }
6371 if(fill_L && j == L && d != j) { /* only allow transition from L if we haven't emitted any residues rightwise (j==L) */
6372 escore = cm->lmesc[v][dsq[i-1]];
6373 Lbeta[cm->M][j][d] = ESL_MAX(Lbeta[cm->M][j][d], (Lbeta[v][j][d+sdl] + cm->endsc[v] + escore));
6374 }
6375 if(fill_R && i == 1 && j != L) { /* only allow transition from R if we haven't emitted any residues leftwise (i==1) */
6376 escore = cm->rmesc[v][dsq[j+1]];
6377 Rbeta[cm->M][j][d] = ESL_MAX(Rbeta[cm->M][j][d], (Rbeta[v][j+sdr][d+sdr] + cm->endsc[v] + escore));
6378 }
6379 break;
6380 case ML_st:
6381 case IL_st:
6382 if (d != j) {
6383 escore = cm->oesc[v][dsq[i-1]];
6384 Jbeta[cm->M][j][d] = ESL_MAX(Jbeta[cm->M][j][d], (Jbeta[v][j][d+sd] + cm->endsc[v] + escore));
6385 if(fill_L) Lbeta[cm->M][j][d] = ESL_MAX(Lbeta[cm->M][j][d], (Lbeta[v][j][d+sd] + cm->endsc[v] + escore));
6386 }
6387 if(fill_R && i == 1) { /* only allow transition from R if we haven't emitted any residues leftwise (i == 1) */
6388 Rbeta[cm->M][j][d] = ESL_MAX(Rbeta[cm->M][j][d], Rbeta[v][j][d] + cm->endsc[v]);
6389 }
6390 break;
6391 case MR_st:
6392 case IR_st:
6393 if(j != L) {
6394 escore = cm->oesc[v][dsq[j+1]];
6395 Jbeta[cm->M][j][d] = ESL_MAX(Jbeta[cm->M][j][d], (Jbeta[v][j+sdr][d+sd] + cm->endsc[v] + escore));
6396 if(fill_R) Rbeta[cm->M][j][d] = ESL_MAX(Rbeta[cm->M][j][d], (Rbeta[v][j+sdr][d+sd] + cm->endsc[v] + escore));
6397 }
6398 if(fill_L && j == L) { /* only allow transition from L if we haven't emitted any residues rightwise (j == L) */
6399 Lbeta[cm->M][j][d] = ESL_MAX(Lbeta[cm->M][j][d], Lbeta[v][j][d] + cm->endsc[v]);
6400 }
6401 break;
6402 case S_st:
6403 case D_st:
6404 case E_st:
6405 Jbeta[cm->M][j][d] = ESL_MAX(Jbeta[cm->M][j][d], (Jbeta[v][j+sdr][d+sd] + cm->endsc[v]));
6406 if(fill_L) Lbeta[cm->M][j][d] = ESL_MAX(Lbeta[cm->M][j][d], (Lbeta[v][j+sdr][d+sd] + cm->endsc[v]));
6407 if(fill_R) Rbeta[cm->M][j][d] = ESL_MAX(Rbeta[cm->M][j][d], (Rbeta[v][j+sdr][d+sd] + cm->endsc[v]));
6408 break;
6409 }
6410 }
6411 }
6412 }
6413 }
6414 /* Deal with last step needed for local alignment
6415 * w.r.t. ends: left-emitting, EL->EL transitions. (EL = deck at M.)
6416 */
6417 if (cm->flags & CMH_LOCAL_END) {
6418 for (j = L; j > 0; j--) { /* careful w/ boundary here */
6419 for (d = j-1; d >= 0; d--) { /* careful w/ boundary here */
6420 Jbeta[cm->M][j][d] = ESL_MAX(Jbeta[cm->M][j][d], Jbeta[cm->M][j][d+1] + cm->el_selfsc);
6421 if(fill_L) Lbeta[cm->M][j][d] = ESL_MAX(Lbeta[cm->M][j][d], Lbeta[cm->M][j][d+1] + cm->el_selfsc);
6422 if(fill_R) Rbeta[cm->M][j][d] = ESL_MAX(Rbeta[cm->M][j][d], Rbeta[cm->M][j][d+1] + cm->el_selfsc);
6423 }
6424 }
6425 }
6426
6427 fail1_flag = FALSE;
6428 fail2_flag = FALSE;
6429 if(do_check) {
6430 /* Check for consistency between the Inside alpha matrix and the
6431 * Outside beta matrix. we assume the Inside CYK parse score
6432 * (optsc) is the optimal score, so for all v,j,d:
6433 *
6434 * Jalpha[v][j][d] + Jbeta[v][j][d] <= optsc
6435 * Lalpha[v][j][d] + Lbeta[v][j][d] <= optsc
6436 * Ralpha[v][j][d] + Rbeta[v][j][d] <= optsc
6437 *
6438 * Further, we know that each residue must be emitted by a state
6439 * in the optimal parse. So as we do the above check, we determine
6440 * when we're in a cell that may be involved in the optimal parse
6441 * (the sum of the Inside and Outside scores are equal to the
6442 * optimal parse score), if that cell corresponds to a left
6443 * emitter emitting position i, we know an emitted i has been
6444 * observed in an optimal parse and set optseen[i] to TRUE.
6445 * Likewise, if that cell corresponds to a right emitter emitting
6446 * position j, we update optseen[j] to TRUE. At the end of the
6447 * check optseen[i] should be TRUE for all i in the range
6448 * [1..L].
6449 *
6450 * Note that we don't ensure that all of our presumed optimal
6451 * cells make up a valid parse, so it is possible we could pass
6452 * this check even if the Inside and Outside matrices are
6453 * inconsistent (i.e. there's a bug in the implementation of one
6454 * and/or the other) but that should be extremely unlikely. If we
6455 * do this test many times for many different models and pass, we
6456 * should be confident we have consistent implementations.
6457 *
6458 * Note that we don't check fill_L and fill_R variables
6459 * here, although they will have dictated whether we've filled
6460 * in the L and R matrices. If they're FALSE, those matrices
6461 * should remain as they've been initialized as all IMPOSSIBLE
6462 * values, so they won't cause us to fail our tests here.
6463 *
6464 * This is an expensive check and should only be done while
6465 * debugging.
6466 */
6467 ESL_ALLOC(optseen, sizeof(int) * (L+1));
6468 esl_vec_ISet(optseen, L+1, FALSE);
6469 vmax = (cm->flags & CMH_LOCAL_END) ? cm->M : cm->M-1;
6470 if (preset_mode == TRMODE_J) optsc = Jalpha[0][L][L];
6471 else if(preset_mode == TRMODE_L) optsc = Lalpha[0][L][L];
6472 else if(preset_mode == TRMODE_R) optsc = Ralpha[0][L][L];
6473 else if(preset_mode == TRMODE_T) optsc = Talpha[0][L][L];
6474 /* define bit score difference tolerance, somewhat arbitrarily:
6475 * clen <= 200: tolerance is 0.001; then a function of clen:
6476 * clen == 1000 tolerance is 0.005,
6477 * clen == 2000, tolerance is 0.01.
6478 *
6479 * I did this b/c with tests with SSU_rRNA_eukarya I noticed
6480 * failures with bit score differences up to 0.004 or so. This
6481 * could mean a bug, but I couldn't get any average sized model to
6482 * fail with a difference above 0.001, so I blamed it on
6483 * precision. I'm not entirely convinced it isn't a bug but
6484 * until I see a failure on a smaller model it seems precision
6485 * is the most likely explanation, right?
6486 */
6487 tol = ESL_MAX(1e-3, (float) cm->clen / 200000.);
6488 for(v = 0; v <= vmax; v++) {
6489 for(j = 1; j <= L; j++) {
6490 for(d = 0; d <= j; d++) {
6491 Jsc = Jalpha[v][j][d] + Jbeta[v][j][d] - optsc;
6492 Lsc = (fill_L) ? Lalpha[v][j][d] + Lbeta[v][j][d] - optsc : IMPOSSIBLE;
6493 Rsc = (fill_R) ? Ralpha[v][j][d] + Rbeta[v][j][d] - optsc : IMPOSSIBLE;
6494 Tsc = (fill_T && cm->sttype[v] == B_st) ? Talpha[v][j][d] + Tbeta[v][j][d] - optsc : IMPOSSIBLE;
6495 if(Jsc > tol) {
6496 printf("Check 1 J failure: v: %4d j: %4d d: %4d (%.4f + %.4f) %.4f > %.4f\n",
6497 v, j, d, Jalpha[v][j][d], Jbeta[v][j][d], Jalpha[v][j][d] + Jbeta[v][j][d], optsc);
6498 fail1_flag = TRUE;
6499 }
6500 if(Lsc > tol) {
6501 printf("Check 1 L failure: v: %4d j: %4d d: %4d (%.4f + %.4f) %.4f > %.4f\n",
6502 v, j, d, Lalpha[v][j][d], Lbeta[v][j][d], Lalpha[v][j][d] + Lbeta[v][j][d], optsc);
6503 fail1_flag = TRUE;
6504 }
6505 if(Rsc > tol) {
6506 printf("Check 1 R failure: v: %4d j: %4d d: %4d (%.4f + %.4f) %.4f > %.4f\n",
6507 v, j, d, Ralpha[v][j][d], Rbeta[v][j][d], Ralpha[v][j][d] + Rbeta[v][j][d], optsc);
6508 fail1_flag = TRUE;
6509 }
6510 if(cm->sttype[v] == B_st && Tsc > tol) {
6511 printf("Check 1 T failure: v: %4d j: %4d d: %4d (%.4f + %.4f) %.4f > %.4f\n",
6512 v, j, d, Talpha[v][j][d], Tbeta[v][j][d], Talpha[v][j][d] + Tbeta[v][j][d], optsc);
6513 fail1_flag = TRUE;
6514 }
6515 if(((fabs(Jsc) < tol || fabs(Lsc) < tol) &&
6516 (cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st || (cm->sttype[v] == EL_st && d >0))) ||
6517 ((fabs(Rsc) < tol && cm->sttype[v] == EL_st && d >0))) {
6518 i = j-d+1;
6519 /* i is accounted for by a parse with an optimal score */
6520 optseen[i] = TRUE;
6521 /*
6522 if (fabs(Jsc) < tol) printf("\tResidue %4d possibly accounted for by J matrix Left emitter %2s cell [v:%4d][j:%4d][d:%4d]\n", i, Statetype(cm->sttype[v]), v, j, d);
6523 else if(fabs(Lsc) < tol) printf("\tResidue %4d possibly accounted for by L matrix Left emitter %2s cell [v:%4d][j:%4d][d:%4d]\n", i, Statetype(cm->sttype[v]), v, j, d);
6524 else if(fabs(Rsc) < tol) printf("\tResidue %4d possibly accounted for by R matrix Left emitter %2s cell [v:%4d][j:%4d][d:%4d]\n", i, Statetype(cm->sttype[v]), v, j, d);
6525 */
6526 }
6527 if((fabs(Jsc) < tol || fabs(Rsc) < tol) &&
6528 (cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st)) {
6529 /* j is accounted for by a parse with an optimal score */
6530 optseen[j] = TRUE;
6531 /*
6532 if (fabs(Jsc) < tol) printf("\tResidue %4d possibly accounted for by J matrix Right emitter %2s cell [v:%4d][j:%4d][d:%4d]\n", j, Statetype(cm->sttype[v]), v, j, d);
6533 else if(fabs(Rsc) < tol) printf("\tResidue %4d possibly accounted for by R matrix Right emitter %2s cell [v:%4d][j:%4d][d:%4d]\n", j, Statetype(cm->sttype[v]), v, j, d);
6534 */
6535 }
6536 }
6537 }
6538 }
6539 for(j = 1; j <= L; j++) {
6540 if(optseen[j] == FALSE) {
6541 printf("Check 2 failure: residue %d not emitted in the optimal parsetree\n", j);
6542 fail2_flag = TRUE;
6543 }
6544 }
6545 free(optseen);
6546 }
6547 if(fail1_flag || fail2_flag) for(j = 1; j <= L; j++) printf("dsq[%4d]: %4d\n", j, dsq[j]);
6548
6549 #if eslDEBUGLEVEL >= 2
6550 /* Uncomment to dump matrix to file. Careful...this could be very large. */
6551 /* FILE *fp1; fp1 = fopen("tmp.tru_ocykmx", "w"); cm_tr_mx_Dump(fp1, mx, preset_mode, TRUE); fclose(fp1); */
6552 #endif
6553
6554 if(do_check) {
6555 if (fail1_flag) ESL_FAIL(eslFAIL, errbuf, "TrCYK Inside/Outside check1 FAILED.");
6556 else if(fail2_flag) ESL_FAIL(eslFAIL, errbuf, "TrCYK Inside/Outside check2 FAILED.");
6557 /*else printf("SUCCESS! TrCYK Inside/Outside checks PASSED.\n");*/
6558 }
6559
6560 if (preset_mode == TRMODE_J) optsc = Jalpha[0][L][L];
6561 else if(preset_mode == TRMODE_L) optsc = Lalpha[0][L][L];
6562 else if(preset_mode == TRMODE_R) optsc = Ralpha[0][L][L];
6563 else if(preset_mode == TRMODE_T) optsc = Talpha[0][L][L];
6564 ESL_DPRINTF1(("#DEBUG: \tcm_TrCYKOutsideAlign() sc : %f (sc is from Inside!)\n", optsc));
6565
6566 return eslOK;
6567
6568 ERROR:
6569 ESL_FAIL(status, errbuf, "Out of memory");
6570 return status; /* NEVER REACHED */
6571 }
6572
6573 /* Function: cm_TrCYKOutsideAlignHB()
6574 * Date: EPN, Sat Oct 8 15:42:48 2011
6575 *
6576 * Purpose: Run the outside TrCYK algorithm on a target sequence.
6577 * HMM banded version. See cm_TrCYKOutsideAlign() for
6578 * the non-banded version. The full target sequence
6579 * 1..L is aligned.
6580 *
6581 * Very similar to cm_TrOutsideAlignHB() but calculates
6582 * beta[v][j][d]: log probability of the most likely parse
6583 * that emits 1..i-1 and j+1..L and passes through v at j,d
6584 * (where i = j-d+1) instead of the log of the summed
6585 * probability of all such parses. This means max operations
6586 * are used instead of logsums.
6587 *
6588 * This function complements cm_TrCYKInsideAlignHB() but is
6589 * mainly useful for testing and reference. It can be used
6590 * with do_check=TRUE to verify that the implementation of
6591 * TrCYKInsideAlignHB and TrCYKOutsideAlignHB are
6592 * consistent. Because the structure of TrCYKInsideAlignHB
6593 * and TrInsideAlignHB, and TrCYKOutsideAlignHB and
6594 * TrOutsideAlignHB are so similar and the TrCYK variants
6595 * are easier to debug (because only the optimal parsetree
6596 * is considered instead of all possible parsetrees) this
6597 * function can be useful for finding bugs in
6598 * TrOutsideAlignHB. It is currently not hooked up to any of
6599 * the main Infernal programs.
6600 *
6601 * Args: cm - the model
6602 * errbuf - char buffer for reporting errors
6603 * dsq - the digitized sequence
6604 * L - length of the dsq to align
6605 * size_limit - max number of Mb for DP matrix, if matrix is bigger return eslERANGE
6606 * do_check - TRUE to attempt to check
6607 * mx - the dp matrix, only cells within bands in cp9b will be valid
6608 * preset_mode - TRMODE_J, TRMODE_L, TRMODE_R, or TRMODE_T, the pre-determined
6609 * alignment mode, we'll only allow alignments in this mode.
6610 * pass_idx - pipeline pass index, indicates what truncation penalty to use
6611 * ins_mx - the dp matrix from the Inside run calculation (required)
6612 *
6613 * Returns: <eslOK> on success
6614 *
6615 * Throws: <eslERANGE> if required CM_TR_HB_MX size exceeds <size_limit>
6616 * <eslEMEM> if we run out of memory
6617 * <eslFAIL> if <do_check>==TRUE and we fail a test
6618 * In either of these cases, alignment has been aborted.
6619 */
6620 int
6621 cm_TrCYKOutsideAlignHB(CM_t *cm, char *errbuf, ESL_DSQ *dsq, int L, float size_limit, char preset_mode, int pass_idx,
6622 int do_check, CM_TR_HB_MX *mx, CM_TR_HB_MX *inscyk_mx)
6623 {
6624 int status;
6625 int v,y,z; /* indices for states */
6626 float Jsc,Lsc,Rsc,Tsc; /* temporary variables holding a float score */
6627 int j,d,i,k; /* indices in sequence dimensions */
6628 float **esc_vAA; /* ptr to cm->oesc, optimized emission scores */
6629 float optsc; /* optimal score in <preset_mode>, from Inside */
6630 float escore; /* an emission score, tmp variable */
6631 int voffset; /* index of v in t_v(y) transition scores */
6632 int emitmode; /* EMITLEFT, EMITRIGHT, EMITPAIR, EMITNONE, for state y */
6633 int sd; /* StateDelta(cm->sttype[y]) */
6634 int sdl; /* StateLeftDelta(cm->sttype[y] */
6635 int sdr; /* StateRightDelta(cm->sttype[y] */
6636
6637 /* variables used only if do_check */
6638 int fail1_flag = FALSE; /* set to TRUE if do_check and we see a problem with check 1*/
6639 int fail2_flag = FALSE; /* set to TRUE if do_check and we see a problem with check 2*/
6640 int vmax; /* i, offset in the matrix */
6641 float tol; /* tolerance for differences in bit scores */
6642 int *optseen = NULL; /* [1..i..W] TRUE is residue i is accounted for in optimal parse */
6643
6644 /* band related variables */
6645 int dp_v; /* d index for state v in alpha w/mem eff bands */
6646 int dp_y; /* d index for state y in alpha w/mem eff bands */
6647 int kp_z; /* k (in the d dim) index for state z in alpha w/mem eff bands */
6648 int Lp; /* L index also changes depending on state */
6649 int jp_v, jp_y, jp_z; /* offset j index for states v, y, z */
6650 int kmin, kmax; /* temporary minimum/maximum allowed k */
6651 int jn, jx; /* current minimum/maximum j allowed */
6652 int dn, dx; /* current minimum/maximum d allowed */
6653 int jp_0; /* L offset in ROOT_S's (v==0) j band */
6654 int Lp_0; /* L offset in ROOT_S's (v==0) d band */
6655
6656 /* variables related to truncated alignment (not in cm_CYKInsideAlignHB() */
6657 int fill_L, fill_R, fill_T; /* must we fill in the L, R, and T matrices? */
6658 int do_J_v, do_J_y, do_J_z; /* must we fill J matrix deck for state v, y, z? */
6659 int do_L_v, do_L_y, do_L_z; /* must we fill L matrix deck for state v, y, z? */
6660 int do_R_v, do_R_y, do_R_z; /* must we fill R matrix deck for state v, y, z? */
6661 int do_T_v, do_T_y; /* is T matrix valid for state v, y? */
6662 int pty_idx; /* index for truncation penalty, determined by pass_idx */
6663 float trpenalty; /* truncation penalty, differs based on pty_idx and if we're local or global */
6664
6665 /* DP matrix variables */
6666 float ***Jbeta = mx->Jdp; /* pointer to the outside Jbeta DP matrix */
6667 float ***Lbeta = mx->Ldp; /* pointer to the outside Lbeta DP matrix */
6668 float ***Rbeta = mx->Rdp; /* pointer to the outside Rbeta DP matrix */
6669 float ***Tbeta = mx->Tdp; /* pointer to the outside Tbeta DP matrix */
6670
6671 float ***Jalpha = inscyk_mx->Jdp; /* pointer to the precalc'ed inside Jalpha DP matrix */
6672 float ***Lalpha = inscyk_mx->Ldp; /* pointer to the precalc'ed inside Lalpha DP matrix */
6673 float ***Ralpha = inscyk_mx->Rdp; /* pointer to the precalc'ed inside Ralpha DP matrix */
6674 float ***Talpha = inscyk_mx->Tdp; /* pointer to the precalc'ed inside Talpha DP matrix, only used to possibly get optsc */
6675
6676 /* ptrs to cp9b info, for convenience */
6677 CP9Bands_t *cp9b = cm->cp9b;
6678 int *jmin = cm->cp9b->jmin;
6679 int *jmax = cm->cp9b->jmax;
6680 int **hdmin = cm->cp9b->hdmin;
6681 int **hdmax = cm->cp9b->hdmax;
6682
6683 /* Allocations and initializations */
6684 esc_vAA = cm->oesc; /* a ptr to the optimized emission scores */
6685
6686 /* Determine which matrices we need to fill in, based on <preset_mode> */
6687 if(preset_mode != TRMODE_J && preset_mode != TRMODE_L && preset_mode != TRMODE_R && preset_mode != TRMODE_T) ESL_FAIL(eslEINVAL, errbuf, "cm_TrCYKOutsideAlignHB(): preset_mode is not J, L, R, or T");
6688 if((status = cm_TrFillFromMode(preset_mode, &fill_L, &fill_R, &fill_T)) != eslOK) ESL_FAIL(status, errbuf, "cm_TrCYKOutsideAlignHB(), bogus mode: %d", preset_mode);
6689
6690 /* Determine the truncation penalty index, from the pass_idx */
6691 if((pty_idx = cm_tr_penalties_IdxForPass(pass_idx)) == -1) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_TrCYKOutsideAlignHB(), unexpected pass idx: %d", pass_idx);
6692
6693 /* grow the matrix based on the current sequence and bands */
6694 if((status = cm_tr_hb_mx_GrowTo(cm, mx, errbuf, cm->cp9b, L, size_limit)) != eslOK) return status;
6695
6696 /* initialize all cells of the matrix to IMPOSSIBLE */
6697 if(mx->Jncells_valid > 0) esl_vec_FSet(mx->Jdp_mem, mx->Jncells_valid, IMPOSSIBLE);
6698 if(mx->Lncells_valid > 0 && fill_L) esl_vec_FSet(mx->Ldp_mem, mx->Lncells_valid, IMPOSSIBLE);
6699 if(mx->Rncells_valid > 0 && fill_R) esl_vec_FSet(mx->Rdp_mem, mx->Rncells_valid, IMPOSSIBLE);
6700 if(mx->Tncells_valid > 0 && fill_T) esl_vec_FSet(mx->Tdp_mem, mx->Tncells_valid, IMPOSSIBLE);
6701
6702 /* ensure a full alignment in <preset_mode> to ROOT_S (v==0) is allowed by the bands */
6703 if (preset_mode == TRMODE_J && (! cp9b->Jvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrCYKOutsideAlignHB() preset_mode is J but cp9b->Jvalid[0] is FALSE");
6704 else if (preset_mode == TRMODE_L && (! cp9b->Lvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrCYKOutsideAlignHB() preset_mode is L but cp9b->Lvalid[0] is FALSE");
6705 else if (preset_mode == TRMODE_R && (! cp9b->Rvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrCYKOutsideAlignHB() preset_mode is R but cp9b->Rvalid[0] is FALSE");
6706 else if (preset_mode == TRMODE_T && (! cp9b->Tvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrCYKOutsideAlignHB() preset_mode is T but cp9b->Tvalid[0] is FALSE");
6707 if (jmin[0] > L || jmax[0] < L) ESL_FAIL(eslEINVAL, errbuf, "cm_TrCYKOutsideAlignHB(): L (%d) is outside ROOT_S's j band (%d..%d)\n", L, jmin[0], jmax[0]);
6708 jp_0 = L - jmin[0];
6709 if (hdmin[0][jp_0] > L || hdmax[0][jp_0] < L) ESL_FAIL(eslEINVAL, errbuf, "cm_TrCYKOutsideAlignHB(): L (%d) is outside ROOT_S's d band (%d..%d)\n", L, hdmin[0][jp_0], hdmax[0][jp_0]);
6710 Lp_0 = L - hdmin[0][jp_0];
6711
6712 /* initialize cells in the special ROOT_S deck corresponding to full sequence alignments to 0.0 */
6713 if (preset_mode == TRMODE_J) Jbeta[0][jp_0][Lp_0] = 0.; /* a full Joint alignment is outside this cell */
6714 else if(preset_mode == TRMODE_L) Lbeta[0][jp_0][Lp_0] = 0.; /* a full Left alignment is outside this cell */
6715 else if(preset_mode == TRMODE_R) Rbeta[0][jp_0][Lp_0] = 0.; /* a full Right alignment is outside this cell */
6716 else if(preset_mode == TRMODE_T) Tbeta[0][jp_0][Lp_0] = 0.; /* a full Terminal alignment is outside this cell */
6717 else ESL_FAIL(eslEINVAL, errbuf, "cm_TrCYKOutsideAlignHB() preset_mode %d is invalid", preset_mode);
6718
6719 /* set cells corresponding to legal truncated begin entry states to
6720 * the appropriate penalty. In truncated alignment the only way out
6721 * of ROOT_S in local or global mode is via a 'truncated begin' with
6722 * a score (penalty) from cm->trp into any emitting state. The
6723 * penalty was calculated in cm_tr_penalties_Create() and differs
6724 * depending on whether we are in local or global mode and the value
6725 * of 'pty_idx' which was passed in.
6726 */
6727 for(v = 0; v < cm->M; v++) {
6728 if((L >= jmin[v]) && (L <= jmax[v])) {
6729 jp_v = L - jmin[v];
6730 if((L >= hdmin[v][jp_v]) && L <= hdmax[v][jp_v]) {
6731 Lp = L - hdmin[v][jp_v];
6732
6733 trpenalty = (cm->flags & CMH_LOCAL_BEGIN) ? cm->trp->l_ptyAA[pty_idx][v] : cm->trp->g_ptyAA[pty_idx][v];
6734 if(NOT_IMPOSSIBLE(trpenalty)) {
6735 do_J_v = cp9b->Jvalid[v] ? TRUE : FALSE;
6736 do_L_v = cp9b->Lvalid[v] && fill_L ? TRUE : FALSE;
6737 do_R_v = cp9b->Rvalid[v] && fill_R ? TRUE : FALSE;
6738 do_T_v = cp9b->Tvalid[v] && fill_T ? TRUE : FALSE;
6739 if(preset_mode == TRMODE_J && do_J_v) Jbeta[v][jp_v][Lp] = trpenalty; /* a full Joint alignment is outside this cell */
6740 if(preset_mode == TRMODE_L && do_L_v) Lbeta[v][jp_v][Lp] = trpenalty; /* a full Left alignment is outside this cell */
6741 if(preset_mode == TRMODE_R && do_R_v) Rbeta[v][jp_v][Lp] = trpenalty; /* a full Right alignment is outside this cell */
6742 if(preset_mode == TRMODE_T && do_T_v && cm->sttype[v] == B_st) {
6743 Tbeta[v][jp_v][Lp] = trpenalty; /* a full Terminal alignment is outside this cell */
6744 }
6745 }
6746 }
6747 }
6748 }
6749 /* done allocation/initialization */
6750
6751 /* Recursion: main loop down through the decks */
6752 for (v = 1; v < cm->M; v++) { /* start at state 1 because we set all values for ROOT_S state 0 above */
6753 if(! StateIsDetached(cm, v)) {
6754 sd = StateDelta(cm->sttype[v]);
6755 sdr = StateRightDelta(cm->sttype[v]);
6756 do_J_v = cp9b->Jvalid[v] ? TRUE : FALSE;
6757 do_L_v = cp9b->Lvalid[v] && fill_L ? TRUE : FALSE;
6758 do_R_v = cp9b->Rvalid[v] && fill_R ? TRUE : FALSE;
6759 do_T_v = cp9b->Tvalid[v] && fill_T ? TRUE : FALSE;
6760
6761 /* if the v deck is invalid in J, L R and T mode, all states for v will remain impossible */
6762 if(! (do_J_v || do_L_v || do_R_v || do_T_v)) continue;
6763
6764 if (cm->stid[v] == BEGL_S) { /* BEGL_S */
6765 y = cm->plast[v]; /* the parent bifurcation */
6766 z = cm->cnum[y]; /* the other (right) S state */
6767
6768 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
6769 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
6770 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
6771 do_T_y = cp9b->Tvalid[y] && fill_T ? TRUE : FALSE; /* will be FALSE, y is not a B_st */
6772
6773 do_J_z = cp9b->Jvalid[z] ? TRUE : FALSE;
6774 do_L_z = cp9b->Lvalid[z] && fill_L ? TRUE : FALSE;
6775 do_R_z = cp9b->Rvalid[z] && fill_R ? TRUE : FALSE;
6776
6777 for (j = jmax[v]; j >= jmin[v]; j--) {
6778 ESL_DASSERT1((j >= 0 && j <= L));
6779 jp_v = j - jmin[v];
6780 jp_y = j - jmin[y];
6781 jp_z = j - jmin[z];
6782 i = j-d+1;
6783 for (d = hdmax[v][jp_v]; d >= hdmin[v][jp_v]; d--) {
6784 dp_v = d - hdmin[v][jp_v];
6785 /* Find the first k value that implies a valid cell in the y and z decks.
6786 * This k must satisfy the following 8 inequalities (some may be redundant):
6787 * NOTE: these are different from those in Inside() (for one thing, v and y
6788 * (BEGL_S and BIF_B here respectively) are switched relative to Inside.
6789 *
6790 * (1) k <= jmax[y] - j;
6791 * (2) k >= jmin[y] - j;
6792 * (3) k <= jmax[z] - j;
6793 * (4) k >= jmin[z] - j;
6794 * 1 and 2 guarantee (j+k) is within state y's j band
6795 * 3 and 4 guarantee (j+k) is within state z's j band
6796 *
6797 * (5) k >= hdmin[y][j-jmin[y]+k] - d;
6798 * (6) k <= hdmax[y][j-jmin[y]+k] - d;
6799 * 5 and 6 guarantee k+d is within y's j=(j+k), d band
6800 *
6801 * (7) k >= hdmin[z][j-jmin[z]+k];
6802 * (8) k <= hdmax[z][j-jmin[z]+k];
6803 * 5 and 6 guarantee k is within state z's j=(j+k) d band
6804 */
6805 kmin = ESL_MAX(jmin[y], jmin[z]) - j;
6806 kmax = ESL_MIN(jmax[y], jmax[z]) - j;
6807 /* kmin and kmax satisfy inequalities (1-4) */
6808 /* RHS of inequalities 5-8 are dependent on k, so we check
6809 * for these within the next for loop. */
6810 for(k = kmin; k <= kmax; k++) {
6811 if(k < (hdmin[y][jp_y+k] - d) || k > (hdmax[y][jp_y+k] - d)) continue;
6812 /* above line continues if inequality 5 or 6 is violated */
6813 if(k < (hdmin[z][jp_z+k]) || k > (hdmax[z][jp_z+k])) continue;
6814 /* above line continues if inequality 7 or 8 is violated */
6815
6816 /* if we get here for current k, all 8 inequalities have been satisified
6817 * so we know the cells corresponding to the platonic
6818 * matrix cells alpha[v][j][d], alpha[y][j+k][d+k], and
6819 * alpha[z][j+k][k] are all within the bands. These
6820 * cells correspond to beta[v][jp_v][dp_v],
6821 * beta[y][jp_y+k][d-hdmin[y][jp_y+k]+k],
6822 * and alpha[z][jp_z][k-hdmin[z][jp_z+k]];
6823 */
6824 kp_z = k-hdmin[z][jp_z+k];
6825 dp_y = d-hdmin[y][jp_y+k];
6826
6827 if(do_J_v && do_J_y && do_J_z) Jbeta[v][jp_v][dp_v] = ESL_MAX(Jbeta[v][jp_v][dp_v], Jbeta[y][jp_y+k][dp_y+k] + Jalpha[z][jp_z+k][kp_z]); /* A */
6828 if(do_J_v && do_L_y && do_L_z) Jbeta[v][jp_v][dp_v] = ESL_MAX(Jbeta[v][jp_v][dp_v], Lbeta[y][jp_y+k][dp_y+k] + Lalpha[z][jp_z+k][kp_z]); /* B */
6829 if(do_R_v && do_R_y && do_J_z) Rbeta[v][jp_v][dp_v] = ESL_MAX(Rbeta[v][jp_v][dp_v], Rbeta[y][jp_y+k][dp_y+k] + Jalpha[z][jp_z+k][kp_z]); /* C */
6830 if(d == j && (j+k) == L &&
6831 do_R_v && do_T_y && do_L_z) Rbeta[v][jp_v][dp_v] = ESL_MAX(Rbeta[v][jp_v][dp_v], Tbeta[y][jp_y+k][dp_y+k] + Lalpha[z][jp_z+k][kp_z]); /* D */
6832 /* Note: Tbeta[y][j+k==L][d+k==L] will be 0.0 because it
6833 * was initialized that way. That T cell includes the
6834 * full target 1..L (any valid T alignment must because
6835 * we must account for the full target) rooted at a B
6836 * state, and a transition from that B state to this
6837 * BEGL_S is always probability 1.0.
6838 */
6839 } /* end of for k loop */
6840 } /* end of for d loop */
6841 } /* end of for j loop */
6842 /* Two more special cases in truncated alignment, we have to
6843 * do these within their own for j and for d loops because j
6844 * and d has different restrictions than it does in the
6845 * above for j and for d loops we just closed.
6846 */
6847 if(do_L_y && (do_J_v || do_L_v)) {
6848 jn = ESL_MAX(jmin[v], jmin[y]);
6849 jx = ESL_MIN(jmax[v], jmax[y]);
6850 for (j = jx; j >= jn; j--) {
6851 jp_v = j - jmin[v];
6852 jp_y = j - jmin[y];
6853 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y]);
6854 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y]);
6855 for (d = dx; d >= dn; d--) {
6856 dp_v = d-hdmin[v][jp_v];
6857 dp_y = d-hdmin[y][jp_y];
6858 if(do_J_v) Jbeta[v][jp_v][dp_v] = ESL_MAX(Jbeta[v][jp_v][dp_v], Lbeta[y][jp_y][dp_y]); /* entire sequence on left, no sequence on right, k == 0 */
6859 if(do_L_v) Lbeta[v][jp_v][dp_v] = ESL_MAX(Lbeta[v][jp_v][dp_v], Lbeta[y][jp_y][dp_y]); /* entire sequence on left, no sequence on right, k == 0 */
6860 }
6861 }
6862 }
6863 } /* end of 'if (cm->stid[v] == BEGL_S */
6864 else if (cm->stid[v] == BEGR_S) {
6865 y = cm->plast[v]; /* the parent bifurcation */
6866 z = cm->cfirst[y]; /* the other (left) S state */
6867
6868 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
6869 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
6870 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
6871 do_T_y = cp9b->Tvalid[y] && fill_T ? TRUE : FALSE;
6872
6873 do_J_z = cp9b->Jvalid[z] ? TRUE : FALSE;
6874 do_L_z = cp9b->Lvalid[z] && fill_L ? TRUE : FALSE;
6875 do_R_z = cp9b->Rvalid[z] && fill_R ? TRUE : FALSE;
6876
6877 jn = ESL_MAX(jmin[v], jmin[y]);
6878 jx = ESL_MIN(jmax[v], jmax[y]);
6879 for (j = jx; j >= jn; j--) {
6880 ESL_DASSERT1((j >= 0 && j <= L));
6881 jp_v = j - jmin[v];
6882 jp_y = j - jmin[y];
6883 jp_z = j - jmin[z];
6884
6885 dn = ESL_MAX(hdmin[v][jp_v], j-jmax[z]);
6886 dx = ESL_MIN(hdmax[v][jp_v], jp_z);
6887 /* above makes sure that j,d are valid for state z: (jmin[z] + d) >= j >= (jmax[z] + d) */
6888 i = j-dx+1;
6889 for (d = dx; d >= dn; d--, i++) {
6890 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha w/mem eff bands */
6891 /* Find the first k value that implies a valid cell in the y and z decks.
6892 * This k must satisfy the following 4 inequalities (some may be redundant):
6893 * NOTE: these are different from those in Inside() (for one thing, v and y
6894 * (BEGR_S and BIF_B here respectively) are switched relative to Inside.
6895 *
6896 * (1) k >= hdmin[y][j-jmin[y]] - d;
6897 * (2) k <= hdmax[y][j-jmin[y]] - d;
6898 * 1 and 2 guarantee (d+k) is within state y's j=(j) d band
6899 *
6900 * (3) k >= hdmin[z][j-jmin[z]-d];
6901 * (4) k <= hdmax[z][j-jmin[z]-d];
6902 * 3 and 4 guarantee k is within z's j=(j-d) d band
6903 *
6904 */
6905 kmin = ESL_MAX((hdmin[y][jp_y]-d), (hdmin[z][jp_z-d]));
6906 kmax = ESL_MIN((hdmax[y][jp_y]-d), (hdmax[z][jp_z-d]));
6907 /* kmin and kmax satisfy inequalities (1-4) */
6908 for(k = kmin; k <= kmax; k++) {
6909 /* for current k, all 4 inequalities have been satisified
6910 * so we know the cells corresponding to the platonic
6911 * matrix cells beta[v][j][d], beta[y][j][d+k], and
6912 * alpha[z][j-d][k] are all within the bands. These
6913 * cells correspond to beta[v][jp_v][dp_v],
6914 * beta[y][jp_y+k][d-hdmin[y][jp_y]+k],
6915 * and alpha[z][jp_z-d][k-hdmin[z][jp_z-d]];
6916 */
6917 kp_z = k-hdmin[z][jp_z-d];
6918 dp_y = d-hdmin[y][jp_y];
6919
6920 if(do_J_v && do_J_y && do_J_z) Jbeta[v][jp_v][dp_v] = ESL_MAX(Jbeta[v][jp_v][dp_v], Jbeta[y][jp_y][dp_y+k] + Jalpha[z][jp_z-d][kp_z]); /* A */
6921 if(do_J_v && do_R_y && do_R_z) Jbeta[v][jp_v][dp_v] = ESL_MAX(Jbeta[v][jp_v][dp_v], Rbeta[y][jp_y][dp_y+k] + Ralpha[z][jp_z-d][kp_z]); /* C */
6922 if(do_L_v && do_L_y && do_J_z) Lbeta[v][jp_v][dp_v] = ESL_MAX(Lbeta[v][jp_v][dp_v], Lbeta[y][jp_y][dp_y+k] + Jalpha[z][jp_z-d][kp_z]); /* B */
6923 if(k == (i-1) && j == L &&
6924 do_L_v && do_T_y && do_R_z) Lbeta[v][jp_v][dp_v] = ESL_MAX(Lbeta[v][jp_v][dp_v], Tbeta[y][jp_y][dp_y+k] + Ralpha[z][jp_z-d][kp_z]); /* D */
6925 /* Note: Tbeta[y][j==L][d+k==L] will be 0.0 because it
6926 * was initialized that way. That T cell includes the
6927 * full target 1..L (any valid T alignment must because
6928 * we must account for the full target) rooted at a B
6929 * state, and a transition from that B state to this
6930 * BEGR_S is always probability 1.0.
6931 */
6932 } /* end of for k loop */
6933 } /* end of for d loop */
6934 /* Two more special cases in truncated alignment, we have to
6935 * do these within their own for d loop because d has
6936 * different restrictions than it does in the above for d
6937 * loop we just closed. j's restrictions are the same
6938 * though, so we stay inside the for j loop.
6939 */
6940 if(do_R_y && (do_J_v || do_R_v)) {
6941 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y]);
6942 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y]);
6943 for (d = dx; d >= dn; d--) {
6944 dp_v = d-hdmin[v][jp_v];
6945 dp_y = d-hdmin[y][jp_y];
6946 if(do_J_v) Jbeta[v][jp_v][dp_v] = ESL_MAX(Jbeta[v][jp_v][dp_v], Rbeta[y][jp_y][dp_y]); /* entire sequence on right, no sequence on left, k == 0 */
6947 if(do_R_v) Rbeta[v][jp_v][dp_v] = ESL_MAX(Rbeta[v][jp_v][dp_v], Rbeta[y][jp_y][dp_y]); /* entire sequence on right, no sequence on left, k == 0 */
6948 }
6949 }
6950 } /* end of for j loop */
6951 } /* end of 'else if (cm->stid[v] == BEGR_S */
6952 else { /* (cm->sttype[v] != BEGL_S && cm->sttype[v] != BEGR_S */
6953 /* in cm_CYKOutsideAlignHB(), IL and IR states are separated
6954 * out from the other states at this stage because only they
6955 * can self-transit, making it slightly more efficient to
6956 * handle non-inserts differently. In truncated mode there's
6957 * more special cases so I've decided to collapse all states
6958 * together here. An analogous form of the following block is
6959 * used only for IL/IR states in cm_CYKOutsideAlignHB().
6960 *
6961 * ILs and IRs can self transit, this means that
6962 * {J,L,R}beta[v][j][d] must be fully calculated before
6963 * {J,L,R}beta[v][j][d+1] can be started to be calculated,
6964 * forcing the following nesting order: for j { for d { for y
6965 * { } } } for non-self-transitioners, we could do a more
6966 * efficient nesting order (you can see it in
6967 * cm_CYKOutsideAlignHB() but we don't here because truncation
6968 * makes it more complex).
6969 */
6970 for (j = jmax[v]; j >= jmin[v]; j--) {
6971 ESL_DASSERT1((j >= 0 && j <= L));
6972 jp_v = j - jmin[v];
6973 for (d = hdmax[v][jp_v]; d >= hdmin[v][jp_v]; d--) {
6974 i = j-d+1;
6975 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha w/mem eff bands */
6976
6977 for (y = cm->plast[v]; y > cm->plast[v]-cm->pnum[v]; y--) {
6978 /* mind the following sneaky if statement: in truncated
6979 * aln, the only way out of state 0 is through a
6980 * truncated begin, which we handled above (search for
6981 * 'trpenalty'). If we're in local mode transitions out
6982 * of 0 will have IMPOSSIBLE scores, but NOT if we're in
6983 * glocal mode, so we need this 'if'.
6984 */
6985 if(y != 0) {
6986 voffset = v - cm->cfirst[y]; /* gotta calculate the transition score index for t_y(v) */
6987 sd = StateDelta(cm->sttype[y]);
6988 sdl = StateLeftDelta(cm->sttype[y]);
6989 sdr = StateRightDelta(cm->sttype[y]);
6990
6991 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
6992 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
6993 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
6994 do_T_y = cp9b->Tvalid[y] && fill_T ? TRUE : FALSE; /* will be FALSE, y is not a B_st */
6995
6996 /* if the y deck is invalid in J, L and R mode, we don't have to update v based on transitions from y */
6997 if (! (do_J_y || do_L_y || do_R_y)) continue;
6998
6999 /* Note: this looks like it can be optimized, I tried but my 'optimization' slowed the code, so I reverted [EPN] */
7000 switch(cm->sttype[y]) {
7001 case MP_st:
7002 jp_y = j - jmin[y];
7003 if(j != L && d != j && /* boundary condition */
7004 do_J_v && do_J_y && /* J deck is valid for v and y */
7005 (j+sdr >= jmin[y] && j+sdr <= jmax[y]) && /* j+sdr is within y's j band */
7006 (d+sd >= hdmin[y][jp_y+sdr] && d+sd <= hdmax[y][jp_y+sdr])) /* d+sd is within y's d band for j+sdr */
7007 {
7008 dp_y = d - hdmin[y][jp_y+sdr]; /* d index for state y */
7009 escore = esc_vAA[y][dsq[i-1]*cm->abc->Kp+dsq[j+1]];
7010 Jbeta[v][jp_v][dp_v] = ESL_MAX(Jbeta[v][jp_v][dp_v], Jbeta[y][jp_y+sdr][dp_y+sd] + cm->tsc[y][voffset] + escore);
7011 }
7012 if(j == L && d != j && /* boundary condition, only allow transition from L if we haven't emitted any residues rightwise (j==L) */
7013 do_L_y && /* L deck is valid for y */
7014 (j >= jmin[y] && j <= jmax[y]) && /* j is within y's j band */
7015 (d+sdl >= hdmin[y][jp_y] && d+sdl <= hdmax[y][jp_y])) /* d+sdl is within y's d band for j */
7016 {
7017 dp_y = d - hdmin[y][jp_y]; /* d index for state y */
7018 escore = cm->lmesc[y][dsq[i-1]];
7019 if(do_J_v) Jbeta[v][jp_v][dp_v] = ESL_MAX(Jbeta[v][jp_v][dp_v], Lbeta[y][jp_y][dp_y+sdl] + cm->tsc[y][voffset] + escore);
7020 if(do_L_v) Lbeta[v][jp_v][dp_v] = ESL_MAX(Lbeta[v][jp_v][dp_v], Lbeta[y][jp_y][dp_y+sdl] + cm->tsc[y][voffset] + escore);
7021 }
7022 if(i == 1 && j != L && /* boundary condition, only allow transition from R if we haven't emitted any residues leftwise (i==1) */
7023 do_R_y && /* R deck is valid for y */
7024 (j+sdr >= jmin[y] && j+sdr <= jmax[y]) && /* j+sdr is within y's j band */
7025 (d+sdr >= hdmin[y][jp_y+sdr] && d+sdr <= hdmax[y][jp_y+sdr])) /* d+sdr is within y's d band for j+sdr */
7026 {
7027 dp_y = d - hdmin[y][jp_y+sdr]; /* d index for state y */
7028 escore = cm->rmesc[y][dsq[j+1]];
7029 if(do_J_v) Jbeta[v][jp_v][dp_v] = ESL_MAX(Jbeta[v][jp_v][dp_v], Rbeta[y][jp_y+sdr][dp_y+sdr] + cm->tsc[y][voffset] + escore);
7030 if(do_R_v) Rbeta[v][jp_v][dp_v] = ESL_MAX(Rbeta[v][jp_v][dp_v], Rbeta[y][jp_y+sdr][dp_y+sdr] + cm->tsc[y][voffset] + escore);
7031 }
7032 break;
7033
7034 case ML_st:
7035 case IL_st:
7036 jp_y = j - jmin[y];
7037 if(d != j && /* boundary case */
7038 (j >= jmin[y] && j <= jmax[y]) && /* j is within y's j band */
7039 (d+sdl >= hdmin[y][jp_y] && d+sdl <= hdmax[y][jp_y])) /* d+sdl is within y's d band for j */
7040 {
7041 dp_y = d - hdmin[y][jp_y];
7042 escore = cm->oesc[y][dsq[i-1]];
7043 if(do_J_v && do_J_y) Jbeta[v][jp_v][dp_v] = ESL_MAX(Jbeta[v][jp_v][dp_v], Jbeta[y][jp_y][dp_y+sd] + cm->tsc[y][voffset] + escore);
7044 if(do_L_v && do_L_y) Lbeta[v][jp_v][dp_v] = ESL_MAX(Lbeta[v][jp_v][dp_v], Lbeta[y][jp_y][dp_y+sd] + cm->tsc[y][voffset] + escore);
7045 }
7046 if(i == 1 && /* boundary condition, only allow transition from R if we're emitting first residue 1 from y */
7047 v != y && /* will only happen if v == IL, we don't allow silent self transitions from IL->IL */
7048 do_R_y && /* R deck is valid for y */
7049 (j >= jmin[y] && j <= jmax[y]) && /* j is within y's j band */
7050 (d >= hdmin[y][jp_y] && d <= hdmax[y][jp_y])) /* d+sdr(==d) is within y's d band for j */
7051 {
7052 dp_y = d - hdmin[y][jp_y];
7053 if(do_J_v) Jbeta[v][jp_v][dp_v] = ESL_MAX(Jbeta[v][jp_v][dp_v], Rbeta[y][jp_y][dp_y] + cm->tsc[y][voffset]);
7054 if(do_R_v) Rbeta[v][jp_v][dp_v] = ESL_MAX(Rbeta[v][jp_v][dp_v], Rbeta[y][jp_y][dp_y] + cm->tsc[y][voffset]);
7055 }
7056 break;
7057
7058 case MR_st:
7059 case IR_st:
7060 jp_y = j - jmin[y];
7061 if (j != L && /* boundary condition */
7062 (j+sdr >= jmin[y] && j+sdr <= jmax[y]) && /* j+sdr is within y's j band */
7063 (d+sd >= hdmin[y][jp_y+sdr] && d+sd <= hdmax[y][jp_y+sdr])) /* d+sd is within y's d band for j+sdr */
7064 {
7065 dp_y = d - hdmin[y][jp_y+sdr];
7066 escore = cm->oesc[y][dsq[j+1]];
7067 if(do_J_v && do_J_y) Jbeta[v][jp_v][dp_v] = ESL_MAX(Jbeta[v][jp_v][dp_v], Jbeta[y][jp_y+sdr][dp_y+sd] + cm->tsc[y][voffset] + escore);
7068 if(do_R_v && do_R_y) Rbeta[v][jp_v][dp_v] = ESL_MAX(Rbeta[v][jp_v][dp_v], Rbeta[y][jp_y+sdr][dp_y+sd] + cm->tsc[y][voffset] + escore);
7069 }
7070 if(j == L && /* boundary condition, only allow transition from L if we're emitting final residue L from y */
7071 v != y && /* will only happen if v == IR, we don't allow silent self transitions from IR->IR */
7072 do_L_y && /* L deck is valid for y */
7073 (j >= jmin[y] && j <= jmax[y]) && /* j is within y's j band */
7074 (d >= hdmin[y][jp_y] && d <= hdmax[y][jp_y])) /* d+sdl(==d) is within y's d band for j */
7075 {
7076 dp_y = d - hdmin[y][jp_y];
7077 if(do_J_v) Jbeta[v][jp_v][dp_v] = ESL_MAX(Jbeta[v][jp_v][dp_v], Lbeta[y][jp_y][dp_y] + cm->tsc[y][voffset]);
7078 if(do_L_v) Lbeta[v][jp_v][dp_v] = ESL_MAX(Lbeta[v][jp_v][dp_v], Lbeta[y][jp_y][dp_y] + cm->tsc[y][voffset]);
7079 }
7080 break;
7081 case S_st:
7082 case E_st:
7083 case D_st:
7084 jp_y = j - jmin[y];
7085 if((j >= jmin[y] && j <= jmax[y]) &&
7086 (d >= hdmin[y][jp_y] && d <= hdmax[y][jp_y]))
7087 {
7088 dp_y = d - hdmin[y][jp_y]; /* d index for state y */
7089 if(do_J_v && do_J_y) Jbeta[v][jp_v][dp_v] = ESL_MAX(Jbeta[v][jp_v][dp_v], Jbeta[y][jp_y][dp_y] + cm->tsc[y][voffset]);
7090 if(do_L_v && do_L_y) Lbeta[v][jp_v][dp_v] = ESL_MAX(Lbeta[v][jp_v][dp_v], Lbeta[y][jp_y][dp_y] + cm->tsc[y][voffset]);
7091 if(do_R_v && do_R_y) Rbeta[v][jp_v][dp_v] = ESL_MAX(Rbeta[v][jp_v][dp_v], Rbeta[y][jp_y][dp_y] + cm->tsc[y][voffset]);
7092 }
7093 break;
7094 } /* end of switch(cm->sttype[y] */
7095 } /* end of sneaky if y != 0 */
7096 } /* ends for loop over parent states. we now know beta[v][j][d] for this d */
7097 if (do_J_v && Jbeta[v][jp_v][dp_v] < IMPOSSIBLE) Jbeta[v][jp_v][dp_v] = IMPOSSIBLE;
7098 if (do_L_v && Lbeta[v][jp_v][dp_v] < IMPOSSIBLE) Lbeta[v][jp_v][dp_v] = IMPOSSIBLE;
7099 if (do_R_v && Rbeta[v][jp_v][dp_v] < IMPOSSIBLE) Rbeta[v][jp_v][dp_v] = IMPOSSIBLE;
7100 } /* ends loop over d. We know all beta[v][j][d] in this row j and state v */
7101 } /* end loop over jp. We know beta for this whole state */
7102 } /* end of 'else' (entered if cm->sttype[v] != BEGL_S nor BEGR_S */
7103 /* we're done calculating deck v for everything but local ends */
7104
7105 /* deal with local alignment end transitions v->EL (EL = deck at M.) */
7106 if ((cm->flags & CMH_LOCAL_END) && NOT_IMPOSSIBLE(cm->endsc[v])) {
7107 sd = StateDelta(cm->sttype[v]); /* note sd is for state v */
7108 sdl = StateLeftDelta(cm->sttype[v]); /* note sdl is for state v */
7109 sdr = StateRightDelta(cm->sttype[v]); /* note sdr is for state v */
7110 emitmode = Emitmode(cm->sttype[v]); /* note emitmode is for state v */
7111
7112 /* we handle all three possible modes (J,L,R) differently because they have different boundary conditions */
7113
7114 /* J mode */
7115 if(do_J_v && cp9b->Jvalid[cm->M]) {
7116 jn = jmin[v] - sdr;
7117 jx = jmax[v] - sdr;
7118 for (j = jn; j <= jx; j++) {
7119 jp_v = j - jmin[v];
7120 dn = hdmin[v][jp_v + sdr] - sd;
7121 dx = hdmax[v][jp_v + sdr] - sd;
7122 i = j-dn+1; /* we'll decrement this in for (d... loops inside switch below */
7123 dp_v = dn - hdmin[v][jp_v + sdr]; /* we'll increment this in for (d... loops inside switch below */
7124
7125 switch (emitmode) {
7126 case EMITPAIR:
7127 for (d = dn; d <= dx; d++, dp_v++, i--) {
7128 escore = esc_vAA[v][dsq[i-1]*cm->abc->Kp+dsq[j+1]];
7129 Jbeta[cm->M][j][d] = ESL_MAX(Jbeta[cm->M][j][d], (Jbeta[v][jp_v+sdr][dp_v+sd] + cm->endsc[v] + escore));
7130 }
7131 break;
7132
7133 case EMITLEFT:
7134 for (d = dn; d <= dx; d++, dp_v++, i--) {
7135 escore = esc_vAA[v][dsq[i-1]];
7136 Jbeta[cm->M][j][d] = ESL_MAX(Jbeta[cm->M][j][d], (Jbeta[v][jp_v+sdr][dp_v+sd] + cm->endsc[v] + escore));
7137 }
7138 break;
7139
7140 case EMITRIGHT:
7141 escore = esc_vAA[v][dsq[j+1]];
7142 for (d = dn; d <= dx; d++, dp_v++) {
7143 Jbeta[cm->M][j][d] = ESL_MAX(Jbeta[cm->M][j][d], (Jbeta[v][jp_v+sdr][dp_v+sd] + cm->endsc[v] + escore));
7144 }
7145 break;
7146
7147 case EMITNONE:
7148 for (d = dn; d <= dx; d++, dp_v++) {
7149 Jbeta[cm->M][j][d] = ESL_MAX(Jbeta[cm->M][j][d], (Jbeta[v][jp_v+sdr][dp_v+sd] + cm->endsc[v]));
7150 }
7151 break;
7152 }
7153 }
7154 }
7155
7156 /* L mode: again, this code is inefficient, but I chose not to try to optimize lest it get more complex */
7157 if(do_L_v && cp9b->Lvalid[cm->M]) {
7158 jn = jmin[v];
7159 jx = jmax[v];
7160 for (j = jn; j <= jx; j++) {
7161 jp_v = j - jmin[v];
7162 dn = hdmin[v][jp_v] - sdl;
7163 dx = hdmax[v][jp_v] - sdl;
7164 i = j-dn+1; /* we'll decrement this in for (d... loops inside switch below */
7165 dp_v = dn - hdmin[v][jp_v]; /* we'll increment this in for (d... loops inside switch below */
7166
7167 switch (emitmode) {
7168 case EMITPAIR:
7169 if(j == L) { /* only allow transition from L if we haven't emitted any residues rightwise (j==L) */
7170 for (d = dn; d <= dx; d++, dp_v++, i--) {
7171 escore = cm->lmesc[v][dsq[i-1]];
7172 Lbeta[cm->M][j][d] = ESL_MAX(Lbeta[cm->M][j][d], (Lbeta[v][jp_v][dp_v+sdl] + cm->endsc[v] + escore));
7173 }
7174 }
7175 break;
7176
7177 case EMITLEFT:
7178 for (d = dn; d <= dx; d++, dp_v++, i--) {
7179 escore = esc_vAA[v][dsq[i-1]];
7180 Lbeta[cm->M][j][d] = ESL_MAX(Lbeta[cm->M][j][d], (Lbeta[v][jp_v][dp_v+sdl] + cm->endsc[v] + escore));
7181 }
7182 break;
7183
7184 case EMITRIGHT:
7185 if(j == L) { /* only allow transition from L if we haven't emitted any residues rightwise (j==L) */
7186 for (d = dn; d <= dx; d++, dp_v++) {
7187 Lbeta[cm->M][j][d] = ESL_MAX(Lbeta[cm->M][j][d], (Lbeta[v][jp_v][dp_v] + cm->endsc[v]));
7188 }
7189 }
7190 break;
7191
7192 case EMITNONE:
7193 for (d = dn; d <= dx; d++, dp_v++) {
7194 Lbeta[cm->M][j][d] = ESL_MAX(Lbeta[cm->M][j][d], (Lbeta[v][jp_v][dp_v] + cm->endsc[v]));
7195 }
7196 break;
7197 }
7198 }
7199 } /* end of if(do_L_v) */
7200
7201 /* R mode: again, this code is inefficient, but I chose not to try to optimize lest it get more complex */
7202 if(do_R_v && cp9b->Rvalid[cm->M]) {
7203 jn = jmin[v] - sdr;
7204 jx = jmax[v] - sdr;
7205 for (j = jn; j <= jx; j++) {
7206 jp_v = j - jmin[v];
7207 dn = hdmin[v][jp_v + sdr] - sdr;
7208 dx = hdmax[v][jp_v + sdr] - sdr;
7209 i = j-dn+1; /* we'll decrement this in for (d... loops inside switch below */
7210 dp_v = dn - hdmin[v][jp_v + sdr]; /* we'll increment this in for (d... loops inside switch below */
7211
7212 switch (emitmode) {
7213 case EMITPAIR:
7214 for (d = dn; d <= dx; d++, dp_v++, i--) {
7215 if(i == 1) { /* only allow transition from R if we haven't emitted any residues leftwise (i==1) */
7216 escore = cm->rmesc[v][dsq[j+1]];
7217 Rbeta[cm->M][j][d] = ESL_MAX(Rbeta[cm->M][j][d], (Rbeta[v][jp_v+sdr][dp_v+sdr] + cm->endsc[v] + escore));
7218 }
7219 }
7220 break;
7221 case EMITLEFT:
7222 for (d = dn; d <= dx; d++, dp_v++, i--) {
7223 if(i == 1) { /* only allow transition from R if we haven't emitted any residues leftwise (i==1) */
7224 Rbeta[cm->M][j][d] = ESL_MAX(Rbeta[cm->M][j][d], (Rbeta[v][jp_v][dp_v] + cm->endsc[v]));
7225 }
7226 }
7227 break;
7228
7229 case EMITRIGHT:
7230 escore = esc_vAA[v][dsq[j+1]];
7231 for (d = dn; d <= dx; d++, dp_v++) {
7232 Rbeta[cm->M][j][d] = ESL_MAX(Rbeta[cm->M][j][d], (Rbeta[v][jp_v+sdr][dp_v+sdr] + cm->endsc[v] + escore));
7233 }
7234 break;
7235
7236 case EMITNONE:
7237 for (d = dn; d <= dx; d++, dp_v++) {
7238 Rbeta[cm->M][j][d] = ESL_MAX(Rbeta[cm->M][j][d], (Rbeta[v][jp_v+sdr][dp_v+sdr] + cm->endsc[v]));
7239 }
7240 break;
7241 }
7242 }
7243 } /* end of if(do_R_v) */
7244 } /* end of calculating EL scores */
7245 } /* end of if !StateIsDetached() */
7246 } /* end loop over decks v. */
7247
7248 /* Deal with last step needed for local alignment
7249 * w.r.t. ends: left-emitting, EL->EL transitions. (EL = deck at M.)
7250 */
7251 if (cm->flags & CMH_LOCAL_END) {
7252 if(cp9b->Jvalid[cm->M]) {
7253 for (j = L; j > 0; j--) { /* careful w/ boundary here */
7254 for (d = j-1; d >= 0; d--) { /* careful w/ boundary here */
7255 Jbeta[cm->M][j][d] = ESL_MAX(Jbeta[cm->M][j][d], (Jbeta[cm->M][j][d+1] + cm->el_selfsc));
7256 }
7257 }
7258 }
7259 if(fill_L && cp9b->Lvalid[cm->M]) {
7260 for (j = L; j > 0; j--) { /* careful w/ boundary here */
7261 for (d = j-1; d >= 0; d--) { /* careful w/ boundary here */
7262 Lbeta[cm->M][j][d] = ESL_MAX(Lbeta[cm->M][j][d], (Lbeta[cm->M][j][d+1] + cm->el_selfsc));
7263 }
7264 }
7265 }
7266 if(fill_R && cp9b->Rvalid[cm->M]) {
7267 for (j = L; j > 0; j--) { /* careful w/ boundary here */
7268 for (d = j-1; d >= 0; d--) { /* careful w/ boundary here */
7269 Rbeta[cm->M][j][d] = ESL_MAX(Rbeta[cm->M][j][d], (Rbeta[cm->M][j][d+1] + cm->el_selfsc));
7270 }
7271 }
7272 }
7273 }
7274
7275 fail1_flag = FALSE;
7276 fail2_flag = FALSE;
7277 if(do_check) {
7278 /* Check for consistency between the Inside alpha matrix and the
7279 * Outside beta matrix. we assume the Inside CYK parse score
7280 * (optsc) is the optimal score, so for all v,j,d:
7281 *
7282 * Jalpha[v][j][d] + Jbeta[v][j][d] <= optsc
7283 * Lalpha[v][j][d] + Lbeta[v][j][d] <= optsc
7284 * Ralpha[v][j][d] + Rbeta[v][j][d] <= optsc
7285 *
7286 * Further, we know that each residue must be emitted by a state
7287 * in the optimal parse. So as we do the above check, we determine
7288 * when we're in a cell that may be involved in the optimal parse
7289 * (the sum of the Inside and Outside scores are equal to the
7290 * optimal parse score), if that cell corresponds to a left
7291 * emitter emitting position i, we know an emitted i has been
7292 * observed in an optimal parse and set optseen[i] to TRUE.
7293 * Likewise, if that cell corresponds to a right emitter emitting
7294 * position j, we update optseen[j] to TRUE. At the end of the
7295 * check optseen[i] should be TRUE for all i in the range
7296 * [1..L].
7297 *
7298 * Note that we don't ensure that all of our presumed optimal
7299 * cells make up a valid parse, so it is possible we could pass
7300 * this check even if the Inside and Outside matrices are
7301 * inconsistent (i.e. there's a bug in the implementation of one
7302 * and/or the other) but that should be extremely unlikely. If we
7303 * do this test many times for many different models and pass, we
7304 * should be confident we have consistent implementations.
7305 *
7306 * This is an expensive check and should only be done while
7307 * debugging.
7308 */
7309 ESL_ALLOC(optseen, sizeof(int) * (L+1));
7310 esl_vec_ISet(optseen, L+1, FALSE);
7311 vmax = (cm->flags & CMH_LOCAL_END) ? cm->M : cm->M-1;
7312 if (preset_mode == TRMODE_J) optsc = Jalpha[0][jp_0][Lp_0];
7313 else if(preset_mode == TRMODE_L) optsc = Lalpha[0][jp_0][Lp_0];
7314 else if(preset_mode == TRMODE_R) optsc = Ralpha[0][jp_0][Lp_0];
7315 else if(preset_mode == TRMODE_T) optsc = Talpha[0][jp_0][Lp_0];
7316 /* define bit score difference tolerance, somewhat arbitrarily:
7317 * clen <= 200: tolerance is 0.001; then a function of clen:
7318 * clen == 1000 tolerance is 0.005,
7319 * clen == 2000, tolerance is 0.01.
7320 *
7321 * I did this b/c with tests with SSU_rRNA_eukarya I noticed
7322 * failures with bit score differences up to 0.004 or so. This
7323 * could mean a bug, but I couldn't get any average sized model to
7324 * fail with a difference above 0.001, so I blamed it on
7325 * precision. I'm not entirely convinced it isn't a bug but
7326 * until I see a failure on a smaller model it seems precision
7327 * is the most likely explanation, right?
7328 */
7329 tol = ESL_MAX(1e-3, (float) cm->clen / 200000.);
7330 for(v = 0; v <= vmax; v++) {
7331 do_J_v = cp9b->Jvalid[v] ? TRUE : FALSE;
7332 do_L_v = cp9b->Lvalid[v] && fill_L ? TRUE : FALSE;
7333 do_R_v = cp9b->Rvalid[v] && fill_R ? TRUE : FALSE;
7334 do_T_v = cp9b->Tvalid[v] && fill_T ? TRUE : FALSE;
7335 jn = (v == cm->M) ? 1 : jmin[v];
7336 jx = (v == cm->M) ? L : jmax[v];
7337 for(j = jn; j <= jx; j++) {
7338 jp_v = (v == cm->M) ? j : j - jmin[v];
7339 dn = (v == cm->M) ? 0 : hdmin[v][jp_v];
7340 dx = (v == cm->M) ? j : hdmax[v][jp_v];
7341 for(d = dn; d <= dx; d++) {
7342 dp_v = (v == cm->M) ? d : d - hdmin[v][jp_v];
7343 Jsc = (do_J_v) ? Jalpha[v][jp_v][dp_v] + Jbeta[v][jp_v][dp_v] - optsc : IMPOSSIBLE;
7344 Lsc = (do_L_v) ? Lalpha[v][jp_v][dp_v] + Lbeta[v][jp_v][dp_v] - optsc : IMPOSSIBLE;
7345 Rsc = (do_R_v) ? Ralpha[v][jp_v][dp_v] + Rbeta[v][jp_v][dp_v] - optsc : IMPOSSIBLE;
7346 Tsc = (do_T_v) ? Talpha[v][jp_v][dp_v] + Tbeta[v][jp_v][dp_v] - optsc : IMPOSSIBLE;
7347 if(Jsc > tol) {
7348 printf("v: %d j: %d d: %d\n", v, j, d);
7349 printf("Check 1 J failure: v: %4d j: %4d d: %4d (%.4f + %.4f) %.4f > %.4f\n",
7350 v, j, d, Jalpha[v][jp_v][dp_v], Jbeta[v][jp_v][dp_v], Jalpha[v][jp_v][dp_v] + Jbeta[v][jp_v][dp_v], optsc);
7351 fail1_flag = TRUE;
7352 }
7353 if(Lsc > tol) {
7354 printf("Check 1 L failure: v: %4d j: %4d d: %4d (%.4f + %.4f) %.4f > %.4f\n",
7355 v, j, d, Lalpha[v][jp_v][dp_v], Lbeta[v][jp_v][dp_v], Lalpha[v][jp_v][dp_v] + Lbeta[v][jp_v][dp_v], optsc);
7356 fail1_flag = TRUE;
7357 }
7358 if(Rsc > tol) {
7359 printf("Check 1 R failure: v: %4d j: %4d d: %4d (%.4f + %.4f) %.4f > %.4f\n",
7360 v, j, d, Ralpha[v][jp_v][dp_v], Rbeta[v][jp_v][dp_v], Ralpha[v][jp_v][dp_v] + Rbeta[v][jp_v][dp_v], optsc);
7361 fail1_flag = TRUE;
7362 }
7363 if(Tsc > tol) {
7364 printf("Check 1 T failure: v: %4d j: %4d d: %4d (%.4f + %.4f) %.4f > %.4f\n",
7365 v, j, d, Talpha[v][jp_v][dp_v], Tbeta[v][jp_v][dp_v], Talpha[v][jp_v][dp_v] + Tbeta[v][jp_v][dp_v], optsc);
7366 fail1_flag = TRUE;
7367 }
7368 if((((do_J_v && fabs(Jsc) < tol) || (do_L_v && fabs(Lsc) < tol)) &&
7369 (cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st || (cm->sttype[v] == EL_st && d >0))) ||
7370 ((do_R_v && fabs(Rsc) < tol) && cm->sttype[v] == EL_st && d >0)) {
7371 i = j-d+1;
7372 /* i is accounted for by a parse with an optimal score */
7373 optseen[i] = TRUE;
7374 /*
7375 if (fabs(Jsc) < tol) printf("\tResidue %4d possibly accounted for by J matrix Left emitter %2s cell [v:%4d][j:%4d][d:%4d]\n", i, Statetype(cm->sttype[v]), v, j, d);
7376 else if(fabs(Lsc) < tol) printf("\tResidue %4d possibly accounted for by L matrix Left emitter %2s cell [v:%4d][j:%4d][d:%4d]\n", i, Statetype(cm->sttype[v]), v, j, d);
7377 else if(fabs(Rsc) < tol) printf("\tResidue %4d possibly accounted for by R matrix Left emitter %2s cell [v:%4d][j:%4d][d:%4d]\n", i, Statetype(cm->sttype[v]), v, j, d);
7378 */
7379 }
7380 if(((do_J_v && fabs(Jsc) < tol) || (do_R_v && fabs(Rsc) < tol)) &&
7381 (cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st)) {
7382 /* j is accounted for by a parse with an optimal score */
7383 optseen[j] = TRUE;
7384 /*
7385 if (fabs(Jsc) < tol) printf("\tResidue %4d possibly accounted for by J matrix Right emitter %2s cell [v:%4d][j:%4d][d:%4d]\n", j, Statetype(cm->sttype[v]), v, j, d);
7386 else if(fabs(Rsc) < tol) printf("\tResidue %4d possibly accounted for by R matrix Right emitter %2s cell [v:%4d][j:%4d][d:%4d]\n", j, Statetype(cm->sttype[v]), v, j, d);
7387 */
7388 }
7389 }
7390 }
7391 }
7392 for(j = 1; j <= L; j++) {
7393 if(optseen[j] == FALSE) {
7394 printf("Check 2 failure: residue %d not emitted in the optimal parsetree\n", j);
7395 fail2_flag = TRUE;
7396 }
7397 }
7398 free(optseen);
7399 }
7400 if(fail1_flag || fail2_flag) for(j = 1; j <= L; j++) printf("dsq[%4d]: %4d\n", j, dsq[j]);
7401
7402 #if eslDEBUGLEVEL >= 2
7403 /* Uncomment to dump matrix to file. Careful...this could be very large. */
7404 /* FILE *fp1; fp1 = fopen("tmp.tru_ocykhbmx", "w"); cm_tr_hb_mx_Dump(fp1, mx, preset_mode, TRUE); fclose(fp1); */
7405 #endif
7406
7407 if(do_check) {
7408 if (fail1_flag) ESL_FAIL(eslFAIL, errbuf, "TrCYKHB Inside/Outside check1 FAILED.");
7409 else if(fail2_flag) ESL_FAIL(eslFAIL, errbuf, "TrCYKHB Inside/Outside check2 FAILED.");
7410 ESL_DPRINTF1(("#DEBUG: SUCCESS! TrCYKHB Inside/Outside checks PASSED.\n"));
7411 }
7412
7413 if (preset_mode == TRMODE_J) optsc = Jalpha[0][jp_0][Lp_0];
7414 else if(preset_mode == TRMODE_L) optsc = Lalpha[0][jp_0][Lp_0];
7415 else if(preset_mode == TRMODE_R) optsc = Ralpha[0][jp_0][Lp_0];
7416 else if(preset_mode == TRMODE_T) optsc = Talpha[0][jp_0][Lp_0];
7417 ESL_DPRINTF1(("#DEBUG: \tcm_TrCYKOutsideAlignHB() sc : %f (sc is from Inside!)\n", optsc));
7418
7419 return eslOK;
7420
7421 ERROR:
7422 ESL_FAIL(status, errbuf, "Out of memory");
7423 return status; /* NEVER REACHED */
7424 }
7425
7426 /* Function: cm_TrOutsideAlign()
7427 * Date: EPN, Mon Sep 19 14:48:30 2011
7428 *
7429 * Purpose: Run the truncated outside algorithm. Non-banded version.
7430 * A CM_TR_MX DP matrix must be passed in. Very similar to
7431 * cm_TrCYKOutsideAlign() but calculates summed log probs of
7432 * all likely parses instead of the most likely parse.
7433 * i.e. uses log sum operations instead of max's. Meaning of
7434 * cells:
7435 *
7436 * Jbeta[v][j][d]: summed log prob of all parsetrees that
7437 * emit 1..i-1 and j+1..L and pass through
7438 * v in Joint marginal mode at j,d.
7439 * Lbeta[v][j][d]: summed log prob of all parsetrees that
7440 * emit 1..i-1 and j+1..L and pass through
7441 * v in Left marginal mode at j,d.
7442 * Rbeta[v][j][d]: summed log prob of all parsetrees that
7443 * emit 1..i-1 and j+1..L and pass through
7444 * v in Right marginal mode at j,d.
7445 *
7446 * Args: cm - the model [0..M-1]
7447 * errbuf - char buffer for reporting errors
7448 * dsq - the digitized sequence
7449 * L - length of the dsq to align
7450 * size_limit - max number of Mb for DP matrix, if matrix is bigger return eslERANGE
7451 * preset_mode - TRMODE_J, TRMODE_L, TRMODE_R, or TRMODE_T, the pre-determined
7452 * alignment mode, we'll only allow alignments in this mode.
7453 * pass_idx - pipeline pass index, indicates what truncation penalty to use
7454 * do_check - TRUE to attempt to check matrices for correctness
7455 * mx - the dp matrix, only cells within bands in cp9b will be valid
7456 * ins_mx - the dp matrix from the CYK Inside run calculation
7457 * (performed by cm_TrCYKInsideAlign(), required)
7458 *
7459 * Returns: <eslOK> on success
7460 *
7461 * Throws: <eslERANGE> if required CM_TR_HB_MX size exceeds <size_limit>
7462 * <eslFAIL> if <do_check>==TRUE and we fail a test
7463 * In either of these cases, alignment has been aborted.
7464 */
7465 int
7466 cm_TrOutsideAlign(CM_t *cm, char *errbuf, ESL_DSQ *dsq, int L, float size_limit, char preset_mode, int pass_idx,
7467 int do_check, CM_TR_MX *mx, CM_TR_MX *ins_mx)
7468 {
7469 int status;
7470 int v,y,z; /* indices for states */
7471 int j,d,i,k; /* indices in sequence dimensions */
7472 float Jsc,Lsc,Rsc,Tsc; /* temporary variables holding a float score */
7473 float optsc; /* the Inside score */
7474 float escore; /* an emission score, tmp variable */
7475 int voffset; /* index of v in t_v(y) transition scores */
7476
7477 /* variables used only if do_check */
7478 int fail_flag = FALSE; /* set to TRUE if do_check and we see a problem */
7479 int vmax; /* i, offset in the matrix */
7480 float tol; /* tolerance for differences in bit scores */
7481
7482 /* indices used in the depths of the DP recursion */
7483 int sd; /* StateDelta(cm->sttype[y]) */
7484 int sdl; /* StateLeftDelta(cm->sttype[y] */
7485 int sdr; /* StateRightDelta(cm->sttype[y] */
7486
7487 /* other variables used in truncated version, but not standard version (not in cm_OutsideAlign()) */
7488 int fill_L, fill_R, fill_T; /* must we fill in the L, R, and T matrices? */
7489 int pty_idx; /* index for truncation penalty, determined by pass_idx */
7490 float trpenalty; /* truncation penalty, differs based on pty_idx and if we're local or global */
7491
7492 /* DP matrix variables */
7493 float ***Jbeta = mx->Jdp; /* pointer to the outside Jbeta DP matrix */
7494 float ***Lbeta = mx->Ldp; /* pointer to the outside Lbeta DP matrix */
7495 float ***Rbeta = mx->Rdp; /* pointer to the outside Rbeta DP matrix */
7496 float ***Tbeta = mx->Tdp; /* pointer to the outside Tbeta DP matrix */
7497
7498 float ***Jalpha = ins_mx->Jdp; /* pointer to the precalc'ed inside Jalpha DP matrix */
7499 float ***Lalpha = ins_mx->Ldp; /* pointer to the precalc'ed inside Lalpha DP matrix */
7500 float ***Ralpha = ins_mx->Rdp; /* pointer to the precalc'ed inside Ralpha DP matrix */
7501 float ***Talpha = ins_mx->Tdp; /* pointer to the precalc'ed inside Talpha DP matrix, only used to possibly get optsc */
7502
7503 /* Allocations and initializations */
7504 if (preset_mode != TRMODE_J && preset_mode != TRMODE_L && preset_mode != TRMODE_R && preset_mode != TRMODE_T) ESL_FAIL(eslEINVAL, errbuf, "cm_TrOutsideAlign(): preset_mode is not J, L, R, or T");
7505 if((status = cm_TrFillFromMode(preset_mode, &fill_L, &fill_R, &fill_T)) != eslOK) ESL_FAIL(status, errbuf, "cm_TrOutsideAlign(), bogus mode: %d", preset_mode);
7506
7507 /* Determine the truncation penalty index, from the pass_idx */
7508 if((pty_idx = cm_tr_penalties_IdxForPass(pass_idx)) == -1) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_TrOutsideAlign(), unexpected pass idx: %d", pass_idx);
7509
7510 /* grow the matrices based on the current sequence and bands */
7511 if((status = cm_tr_mx_GrowTo(cm, mx, errbuf, L, size_limit)) != eslOK) return status;
7512
7513 /* initialize all cells of the matrix to IMPOSSIBLE */
7514 if(mx->Jncells_valid > 0) esl_vec_FSet(mx->Jdp_mem, mx->Jncells_valid, IMPOSSIBLE);
7515 if(mx->Lncells_valid > 0 && fill_L) esl_vec_FSet(mx->Ldp_mem, mx->Lncells_valid, IMPOSSIBLE);
7516 if(mx->Rncells_valid > 0 && fill_R) esl_vec_FSet(mx->Rdp_mem, mx->Rncells_valid, IMPOSSIBLE);
7517 if(mx->Tncells_valid > 0 && fill_T) esl_vec_FSet(mx->Tdp_mem, mx->Tncells_valid, IMPOSSIBLE);
7518
7519 /* initialize cells in the special ROOT_S deck corresponding to full sequence alignments to 0.0 */
7520 if (preset_mode == TRMODE_J) Jbeta[0][L][L] = 0.; /* a full Joint alignment is outside this cell */
7521 else if(preset_mode == TRMODE_L) Lbeta[0][L][L] = 0.; /* a full Left alignment is outside this cell */
7522 else if(preset_mode == TRMODE_R) Rbeta[0][L][L] = 0.; /* a full Right alignment is outside this cell */
7523 else if(preset_mode == TRMODE_T) Tbeta[0][L][L] = 0.; /* a full Terminal alignment is outside this cell */
7524 else ESL_FAIL(eslEINVAL, errbuf, "cm_TrOutsideAlign() preset_mode %d is invalid", preset_mode);
7525
7526 /* set cells corresponding to legal truncated begin entry states to
7527 * the appropriate penalty. In truncated alignment the only way out
7528 * of ROOT_S in local or global mode is via a 'truncated begin' with
7529 * a score (penalty) from cm->trp into any emitting state. The
7530 * penalty was calculated in cm_tr_penalties_Create() and differs
7531 * depending on whether we are in local or global mode and the value
7532 * of 'pty_idx' which was passed in.
7533 */
7534 for(v = 0; v < cm->M; v++) {
7535 trpenalty = (cm->flags & CMH_LOCAL_BEGIN) ? cm->trp->l_ptyAA[pty_idx][v] : cm->trp->g_ptyAA[pty_idx][v];
7536 if(NOT_IMPOSSIBLE(trpenalty)) {
7537 if(preset_mode == TRMODE_J) Jbeta[v][L][L] = trpenalty; /* a full Joint alignment is outside this cell */
7538 if(preset_mode == TRMODE_L) Lbeta[v][L][L] = trpenalty; /* a full Left alignment is outside this cell */
7539 if(preset_mode == TRMODE_R) Rbeta[v][L][L] = trpenalty; /* a full Right alignment is outside this cell */
7540 if(preset_mode == TRMODE_T && cm->sttype[v] == B_st) {
7541 Tbeta[v][L][L] = trpenalty; /* a full Terminal alignment is outside this cell */
7542 }
7543 }
7544 }
7545
7546 /* main loop down through the decks */
7547 for (v = 1; v < cm->M; v++) { /* start at state 1 because we set all values for ROOT_S state 0 above */
7548 if(! StateIsDetached(cm, v)) { /* skip detached inserts, they're cells will remain IMPOSSIBLE */
7549 sd = StateDelta(cm->sttype[v]);
7550 sdr = StateRightDelta(cm->sttype[v]);
7551
7552 if (cm->stid[v] == BEGL_S) { /* BEGL_S */
7553 y = cm->plast[v]; /* the parent bifurcation */
7554 z = cm->cnum[y]; /* the other (right) S state */
7555 for(j = 0; j <= L; j++) {
7556 for (d = 0; d <= j; d++) {
7557 for (k = 0; k <= (L-j); k++) {
7558 Jbeta[v][j][d] = FLogsum(Jbeta[v][j][d], Jbeta[y][j+k][d+k] + Jalpha[z][j+k][k]); /* A */
7559 if(fill_L) {
7560 Jbeta[v][j][d] = FLogsum(Jbeta[v][j][d], Lbeta[y][j+k][d+k] + Lalpha[z][j+k][k]); /* B */
7561 }
7562 if(fill_R) {
7563 Rbeta[v][j][d] = FLogsum(Rbeta[v][j][d], Rbeta[y][j+k][d+k] + Jalpha[z][j+k][k]); /* C */
7564 if(fill_T && fill_L && d == j && (j+k) == L) {
7565 Rbeta[v][j][d] = FLogsum(Rbeta[v][j][d], Tbeta[y][j+k][d+k] + Lalpha[z][j+k][k]); /* D */
7566 /* Note: Tbeta[y][j+k==L][d+k==L] will be 0.0 or
7567 * IMPOSSIBLE because it was initialized that
7568 * way. That T cell includes the full target 1..L
7569 * (any valid T alignment must because we must
7570 * account for the full target) rooted at a B state,
7571 * and a transition from that B state to this BEGL_S
7572 * is always probability 1.0.
7573 */
7574 }
7575 }
7576 }
7577 if(fill_L) {
7578 Jbeta[v][j][d] = FLogsum(Jbeta[v][j][d], Lbeta[y][j][d]); /* entire sequence on left, no sequence on right, k == 0 */
7579 Lbeta[v][j][d] = FLogsum(Lbeta[v][j][d], Lbeta[y][j][d]); /* entire sequence on left, no sequence on right, k == 0 */
7580 }
7581 }
7582 }
7583 } /* end of 'if (cm->stid[v] == BEGL_S */
7584 else if (cm->stid[v] == BEGR_S) {
7585 y = cm->plast[v]; /* the parent bifurcation */
7586 z = cm->cfirst[y]; /* the other (left) S state */
7587 for(j = 0; j <= L; j++) {
7588 for (d = 0; d <= j; d++) {
7589 i = j-d+1;
7590 for (k = 0; k <= (j-d); k++) {
7591 Jbeta[v][j][d] = FLogsum(Jbeta[v][j][d], Jbeta[y][j][d+k] + Jalpha[z][j-d][k]); /* A */
7592 if(fill_R) {
7593 Jbeta[v][j][d] = FLogsum(Jbeta[v][j][d], Rbeta[y][j][d+k] + Ralpha[z][j-d][k]); /* C */
7594 }
7595 if(fill_L) {
7596 Lbeta[v][j][d] = FLogsum(Lbeta[v][j][d], Lbeta[y][j][d+k] + Jalpha[z][j-d][k]); /* B */
7597 if(fill_R && fill_T && k == (i-1) && j == L) {
7598 Lbeta[v][j][d] = FLogsum(Lbeta[v][j][d], Tbeta[y][j][d+k] + Ralpha[z][j-d][k]); /* D */
7599 /* Note: Tbeta[y][j==L][d+k==L] will be 0.0 or
7600 * IMPOSSIBLE because it was initialized that
7601 * way. That T cell includes the full target 1..L
7602 * (any valid T alignment must because we must
7603 * account for the full target) rooted at a B state,
7604 * and a transition from that B state to this BEGR_S
7605 * is always probability 1.0.
7606 */
7607 }
7608 }
7609 }
7610 if(fill_R) {
7611 Jbeta[v][j][d] = FLogsum(Jbeta[v][j][d], Rbeta[y][j][d]); /* entire sequence on right, no sequence on left, k == 0 */
7612 Rbeta[v][j][d] = FLogsum(Rbeta[v][j][d], Rbeta[y][j][d]); /* entire sequence on right, no sequence on left, k == 0 */
7613 }
7614 }
7615 }
7616 } /* end of 'else if (cm->stid[v] == BEGR_S */
7617 else { /* (cm->sttype[v] != BEGL_S && cm->sttype[v] != BEGR_S */
7618 for (j = L; j >= 0; j--) {
7619 i = 1;
7620 for (d = j; d >= 0; d--, i++) {
7621 for (y = cm->plast[v]; y > cm->plast[v]-cm->pnum[v]; y--) {
7622 /* mind the following sneaky if statement: in truncated
7623 * aln, the only way out of state 0 is through a
7624 * truncated begin, which we handled above (search for
7625 * 'trpenalty'). If we're in local mode transitions out
7626 * of 0 will have IMPOSSIBLE scores, but NOT if we're in
7627 * glocal mode, so we need this 'if'.
7628 */
7629 if(y != 0) {
7630 voffset = v - cm->cfirst[y]; /* gotta calculate the transition score index for t_y(v) */
7631 sd = StateDelta(cm->sttype[y]);
7632 sdl = StateLeftDelta(cm->sttype[y]);
7633 sdr = StateRightDelta(cm->sttype[y]);
7634 switch(cm->sttype[y]) {
7635 case MP_st:
7636 if(j != L && d != j) {
7637 escore = cm->oesc[y][dsq[i-1]*cm->abc->Kp+dsq[j+1]];
7638 Jbeta[v][j][d] = FLogsum(Jbeta[v][j][d], Jbeta[y][j+sdr][d+sd] + cm->tsc[y][voffset] + escore);
7639 }
7640 if(fill_L && j == L && d != j) { /* only allow transition from L if we haven't emitted any residues rightwise (j==L) */
7641 escore = cm->lmesc[y][dsq[i-1]];
7642 Jbeta[v][j][d] = FLogsum(Jbeta[v][j][d], Lbeta[y][j][d+sdl] + cm->tsc[y][voffset] + escore);
7643 Lbeta[v][j][d] = FLogsum(Lbeta[v][j][d], Lbeta[y][j][d+sdl] + cm->tsc[y][voffset] + escore);
7644 }
7645 if(fill_R && i == 1 && j != L) { /* only allow transition from R if we haven't emitted any residues leftwise (i==1) */
7646 escore = cm->rmesc[y][dsq[j+1]];
7647 Jbeta[v][j][d] = FLogsum(Jbeta[v][j][d], Rbeta[y][j+sdr][d+sdr] + cm->tsc[y][voffset] + escore);
7648 Rbeta[v][j][d] = FLogsum(Rbeta[v][j][d], Rbeta[y][j+sdr][d+sdr] + cm->tsc[y][voffset] + escore);
7649 }
7650 break;
7651 case ML_st:
7652 case IL_st:
7653 if (d != j) {
7654 escore = cm->oesc[y][dsq[i-1]];
7655 Jbeta[v][j][d] = FLogsum(Jbeta[v][j][d], Jbeta[y][j][d+sd] + cm->tsc[y][voffset] + escore);
7656 if(fill_L) Lbeta[v][j][d] = FLogsum(Lbeta[v][j][d], Lbeta[y][j][d+sd] + cm->tsc[y][voffset] + escore);
7657 }
7658 if(fill_R && i == 1 && /* only allow transition from R if we haven't emitted any residues leftwise (i==1) */
7659 v != y ) { /* will only happen if v == IL, we don't allow silent self transitions from IL->IL */
7660 Jbeta[v][j][d] = FLogsum(Jbeta[v][j][d], Rbeta[y][j][d] + cm->tsc[y][voffset]);
7661 Rbeta[v][j][d] = FLogsum(Rbeta[v][j][d], Rbeta[y][j][d] + cm->tsc[y][voffset]);
7662 }
7663 break;
7664 case MR_st:
7665 case IR_st:
7666 if (j != L) {
7667 escore = cm->oesc[y][dsq[j+1]];
7668 Jbeta[v][j][d] = FLogsum(Jbeta[v][j][d], Jbeta[y][j+sdr][d+sd] + cm->tsc[y][voffset] + escore);
7669 if(fill_R) Rbeta[v][j][d] = FLogsum(Rbeta[v][j][d], Rbeta[y][j+sdr][d+sd] + cm->tsc[y][voffset] + escore);
7670 }
7671 if(fill_L && j == L && /* only allow transition from R if we haven't emitted any residues rightwise (j==L) */
7672 v != y) { /* will only happen if v == IR, we don't allow silent self transitions from IR->IR */
7673 Jbeta[v][j][d] = FLogsum(Jbeta[v][j][d], Lbeta[y][j][d] + cm->tsc[y][voffset]);
7674 Lbeta[v][j][d] = FLogsum(Lbeta[v][j][d], Lbeta[y][j][d] + cm->tsc[y][voffset]);
7675 }
7676 break;
7677 case S_st:
7678 case E_st:
7679 case D_st:
7680 Jbeta[v][j][d] = FLogsum(Jbeta[v][j][d], Jbeta[y][j][d] + cm->tsc[y][voffset]);
7681 if(fill_L) Lbeta[v][j][d] = FLogsum(Lbeta[v][j][d], Lbeta[y][j][d] + cm->tsc[y][voffset]);
7682 if(fill_R) Rbeta[v][j][d] = FLogsum(Rbeta[v][j][d], Rbeta[y][j][d] + cm->tsc[y][voffset]);
7683 break;
7684 } /* end of switch(cm->sttype[y] */
7685 } /* end of sneaky if y != 0 */
7686 } /* ends for loop over parent states. we now know beta[v][j][d] for this d */
7687 if (Jbeta[v][j][d] < IMPOSSIBLE) Jbeta[v][j][d] = IMPOSSIBLE;
7688 } /* ends loop over d. We know all beta[v][j][d] in this row j and state v */
7689 } /* end loop over j. We know beta for this whole state */
7690 } /* end of 'else' (if cm->sttype[v] != BEGL_S, BEGR_S) */
7691 } /* end of 'if(! StateIsDetached(cm, v))' */
7692 /* we're done calculating deck v for everything but local ends */
7693
7694 /* deal with local end transitions v->EL J matrix only (EL = deck at M.) */
7695 if ((cm->flags & CMH_LOCAL_END) && NOT_IMPOSSIBLE(cm->endsc[v])) {
7696 sd = StateDelta(cm->sttype[v]); /* note sd is for state v */
7697 sdl = StateLeftDelta(cm->sttype[v]); /* note sdl is for state v */
7698 sdr = StateRightDelta(cm->sttype[v]); /* note sdr is for state v */
7699
7700 for (j = 0; j <= L; j++) {
7701 for (d = 0; d <= j; d++) {
7702 i = j-d+1;
7703 switch (cm->sttype[v]) {
7704 case MP_st:
7705 if (j != L && d != j) {
7706 escore = cm->oesc[v][dsq[i-1]*cm->abc->Kp+dsq[j+1]];
7707 Jbeta[cm->M][j][d] = FLogsum(Jbeta[cm->M][j][d], (Jbeta[v][j+sdr][d+sd] + cm->endsc[v] + escore));
7708 }
7709 if(fill_L && j == L && d != j) { /* only allow transition from L if we haven't emitted any residues rightwise (j==L) */
7710 escore = cm->lmesc[v][dsq[i-1]];
7711 Lbeta[cm->M][j][d] = FLogsum(Lbeta[cm->M][j][d], (Lbeta[v][j][d+sdl] + cm->endsc[v] + escore));
7712 }
7713 if(fill_R && i == 1 && j != L) { /* only allow transition from R if we haven't emitted any residues leftwise (i==1) */
7714 escore = cm->rmesc[v][dsq[j+1]];
7715 Rbeta[cm->M][j][d] = FLogsum(Rbeta[cm->M][j][d], (Rbeta[v][j+sdr][d+sdr] + cm->endsc[v] + escore));
7716 }
7717 break;
7718 case ML_st:
7719 case IL_st:
7720 if (d != j) {
7721 escore = cm->oesc[v][dsq[i-1]];
7722 Jbeta[cm->M][j][d] = FLogsum(Jbeta[cm->M][j][d], (Jbeta[v][j][d+sd] + cm->endsc[v] + escore));
7723 if(fill_L) Lbeta[cm->M][j][d] = FLogsum(Lbeta[cm->M][j][d], (Lbeta[v][j][d+sdl] + cm->endsc[v] + escore));
7724 }
7725 if(fill_R && i == 1) { /* only allow transition from R if we haven't emitted any residues leftwise (i == 1) */
7726 Rbeta[cm->M][j][d] = FLogsum(Rbeta[cm->M][j][d], Rbeta[v][j][d] + cm->endsc[v]);
7727 }
7728 break;
7729 case MR_st:
7730 case IR_st:
7731 if(j != L) {
7732 escore = cm->oesc[v][dsq[j+1]];
7733 Jbeta[cm->M][j][d] = FLogsum(Jbeta[cm->M][j][d], (Jbeta[v][j+sdr][d+sd] + cm->endsc[v] + escore));
7734 if(fill_R) Rbeta[cm->M][j][d] = FLogsum(Rbeta[cm->M][j][d], (Rbeta[v][j+sdr][d+sdr] + cm->endsc[v] + escore));
7735 }
7736 if(fill_L && j == L) { /* only allow transition from L if we haven't emitted any residues rightwise (j == L) */
7737 Lbeta[cm->M][j][d] = FLogsum(Lbeta[cm->M][j][d], Lbeta[v][j][d] + cm->endsc[v]);
7738 }
7739 break;
7740 case S_st:
7741 case D_st:
7742 case E_st:
7743 Jbeta[cm->M][j][d] = FLogsum(Jbeta[cm->M][j][d], (Jbeta[v][j+sdr][d+sd] + cm->endsc[v]));
7744 if(fill_L) Lbeta[cm->M][j][d] = FLogsum(Lbeta[cm->M][j][d], (Lbeta[v][j+sdr][d+sd] + cm->endsc[v]));
7745 if(fill_R) Rbeta[cm->M][j][d] = FLogsum(Rbeta[cm->M][j][d], (Rbeta[v][j+sdr][d+sd] + cm->endsc[v]));
7746 break;
7747 }
7748 }
7749 }
7750 }
7751 }
7752 /* Deal with last step needed for local alignment
7753 * w.r.t. ends: left-emitting, EL->EL transitions. (EL = deck at M.)
7754 */
7755 if (cm->flags & CMH_LOCAL_END) {
7756 for (j = L; j > 0; j--) { /* careful w/ boundary here */
7757 for (d = j-1; d >= 0; d--) { /* careful w/ boundary here */
7758 Jbeta[cm->M][j][d] = FLogsum(Jbeta[cm->M][j][d], Jbeta[cm->M][j][d+1] + cm->el_selfsc);
7759 if(fill_L) Lbeta[cm->M][j][d] = FLogsum(Lbeta[cm->M][j][d], Lbeta[cm->M][j][d+1] + cm->el_selfsc);
7760 if(fill_R) Rbeta[cm->M][j][d] = FLogsum(Rbeta[cm->M][j][d], Rbeta[cm->M][j][d+1] + cm->el_selfsc);
7761 }
7762 }
7763 }
7764
7765 fail_flag = FALSE;
7766 if(do_check) {
7767 /* Check for consistency between the Inside alpha matrix and the
7768 * Outside beta matrix. If the Inside score (optsc) really is
7769 * the log sum of all possible parsetrees that emit the full
7770 * target sequence 1..L, then for all v,j,d:
7771 *
7772 * Jalpha[v][j][d] + Jbeta[v][j][d] <= optsc
7773 * Lalpha[v][j][d] + Lbeta[v][j][d] <= optsc
7774 * Ralpha[v][j][d] + Rbeta[v][j][d] <= optsc
7775 * Talpha[v][j][d] + Tbeta[v][j][d] <= optsc
7776 *
7777 * We do a more extensive check in cm_TrCYKOutsideAlign(), but
7778 * it doesn't apply here, because we've summed all parsetrees
7779 * instead of finding only the optimal one.
7780 *
7781 * Note that we don't check fill_L and fill_R variables
7782 * here, although they will have dictated whether we've filled
7783 * in the L and R matrices. If they're FALSE, those matrices
7784 * should remain as they've been initialized as all IMPOSSIBLE
7785 * values, so they won't cause us to fail our tests here.
7786 *
7787 * This is an expensive check and should only be done while
7788 * debugging.
7789 */
7790 vmax = (cm->flags & CMH_LOCAL_END) ? cm->M : cm->M-1;
7791 if (preset_mode == TRMODE_J) optsc = Jalpha[0][L][L];
7792 else if(preset_mode == TRMODE_L) optsc = Lalpha[0][L][L];
7793 else if(preset_mode == TRMODE_R) optsc = Ralpha[0][L][L];
7794 else if(preset_mode == TRMODE_T) optsc = Talpha[0][L][L];
7795 /* define bit score difference tolerance, somewhat arbitrarily:
7796 * clen <= 200: tolerance is 0.001; then a function of clen:
7797 * clen == 1000 tolerance is 0.005,
7798 * clen == 2000, tolerance is 0.01.
7799 *
7800 * I did this b/c with tests with SSU_rRNA_eukarya I noticed
7801 * failures with bit score differences up to 0.004 or so. This
7802 * could mean a bug, but I couldn't get any average sized model to
7803 * fail with a difference above 0.001, so I blamed it on
7804 * precision. I'm not entirely convinced it isn't a bug but
7805 * until I see a failure on a smaller model it seems precision
7806 * is the most likely explanation, right?
7807 */
7808 tol = ESL_MAX(1e-3, (float) cm->clen / 200000.);
7809 for(v = 0; v <= vmax; v++) {
7810 for(j = 1; j <= L; j++) {
7811 for(d = 0; d <= j; d++) {
7812 Jsc = Jalpha[v][j][d] + Jbeta[v][j][d] - optsc;
7813 Lsc = (fill_L) ? Lalpha[v][j][d] + Lbeta[v][j][d] - optsc : IMPOSSIBLE;
7814 Rsc = (fill_R) ? Ralpha[v][j][d] + Rbeta[v][j][d] - optsc : IMPOSSIBLE;
7815 Tsc = (fill_T && cm->sttype[v] == B_st) ? Talpha[v][j][d] + Tbeta[v][j][d] - optsc : IMPOSSIBLE;
7816 if(Jsc > tol) {
7817 printf("Check J failure: v: %4d j: %4d d: %4d (%.4f + %.4f) %.4f > %.4f\n",
7818 v, j, d, Jalpha[v][j][d], Jbeta[v][j][d], Jalpha[v][j][d] + Jbeta[v][j][d], optsc);
7819 fail_flag = TRUE;
7820 }
7821 if(Lsc > tol) {
7822 printf("Check L failure: v: %4d j: %4d d: %4d (%.4f + %.4f) %.4f > %.4f\n",
7823 v, j, d, Lalpha[v][j][d], Lbeta[v][j][d], Lalpha[v][j][d] + Lbeta[v][j][d], optsc);
7824 fail_flag = TRUE;
7825 }
7826 if(Rsc > tol) {
7827 printf("Check R failure: v: %4d j: %4d d: %4d (%.4f + %.4f) %.4f > %.4f\n",
7828 v, j, d, Ralpha[v][j][d], Rbeta[v][j][d], Ralpha[v][j][d] + Rbeta[v][j][d], optsc);
7829 fail_flag = TRUE;
7830 }
7831 if(cm->sttype[v] == B_st && Tsc > tol) {
7832 printf("Check 1 T failure: v: %4d j: %4d d: %4d (%.4f + %.4f) %.4f > %.4f\n",
7833 v, j, d, ins_mx->Tdp[v][j][d], Tbeta[v][j][d], ins_mx->Tdp[v][j][d] + Tbeta[v][j][d], optsc);
7834 fail_flag = TRUE;
7835 }
7836 }
7837 }
7838 }
7839 if(fail_flag) for(j = 1; j <= L; j++) printf("dsq[%4d]: %4d\n", j, dsq[j]);
7840 }
7841
7842 #if eslDEBUGLEVEL >= 2
7843 /* Uncomment to dump matrix to file. Careful...this could be very large. */
7844 /* FILE *fp1; fp1 = fopen("tmp.tru_omx", "w"); cm_tr_mx_Dump(fp1, mx, preset_mode, TRUE); fclose(fp1); */
7845 #endif
7846
7847 if(do_check) {
7848 if (fail_flag) ESL_FAIL(eslFAIL, errbuf, "Tr Inside/Outside check FAILED.");
7849 ESL_DPRINTF1(("#DEBUG: SUCCESS! Tr Inside/Outside check PASSED.\n"));
7850 /*printf("SUCCESS! Tr Inside/Outside check PASSED.\n");*/
7851 }
7852
7853 if (preset_mode == TRMODE_J) optsc = Jalpha[0][L][L];
7854 else if(preset_mode == TRMODE_L) optsc = Lalpha[0][L][L];
7855 else if(preset_mode == TRMODE_R) optsc = Ralpha[0][L][L];
7856 else if(preset_mode == TRMODE_T) optsc = Talpha[0][L][L];
7857 ESL_DPRINTF1(("#DEBUG: \tcm_TrOutsideAlign() sc : %f (sc is from Inside!)\n", optsc));
7858
7859 return eslOK;
7860 }
7861
7862
7863 /* Function: cm_TrOutsideAlignHB()
7864 * Date: EPN, Tue Oct 11 09:13:17 2011
7865 *
7866 * Purpose: Run the truncated outside algorithm. HMM banded version.
7867 * See cm_TrOutsideAlign() for the non-banded version. The
7868 * full target sequence 1..L is aligned.
7869 *
7870 * A CM_TR_HB_MX DP matrix must be passed in. Very similar to
7871 * cm_TrCYKOutsideAlignHB() but calculates summed log probs of
7872 * all likely parses instead of the most likely parse.
7873 * i.e. uses log sum operations instead of max's. Meaning of
7874 * cells:
7875 *
7876 * Jbeta[v][jp_v][dp_v]: summed log prob of all parsetrees that
7877 * emit 1..i-1 and j+1..L and pass through
7878 * v in Joint marginal mode at j,d.
7879 * Lbeta[v][jp_v][dp_v]: summed log prob of all parsetrees that
7880 * emit 1..i-1 and j+1..L and pass through
7881 * v in Left marginal mode at j,d.
7882 * Rbeta[v][jp_v][dp_v]: summed log prob of all parsetrees that
7883 * emit 1..i-1 and j+1..L and pass through
7884 * v in Right marginal mode at j,d.
7885 *
7886 * Where jp_v = j-jmin[v] and dp_v = d-hdmin[v][jp_v];
7887 *
7888 * Args: cm - the model
7889 * errbuf - char buffer for reporting errors
7890 * dsq - the digitized sequence
7891 * L - length of the dsq to align
7892 * size_limit - max number of Mb for DP matrix, if matrix is bigger return eslERANGE
7893 * preset_mode - TRMODE_J, TRMODE_L, TRMODE_R, or TRMODE_T, the pre-determined
7894 * alignment mode, we'll only allow alignments in this mode.
7895 * pass_idx - pipeline pass index, indicates what truncation penalty to use
7896 * do_check - TRUE to attempt to check
7897 * mx - the dp matrix, only cells within bands in cp9b will be valid
7898 * ins_mx - the dp matrix from the Inside run calculation (required)
7899 *
7900 * Returns: <eslOK> on success
7901 *
7902 * Throws: <eslERANGE> if required CM_TR_HB_MX size exceeds <size_limit>
7903 * <eslFAIL> if <do_check>==TRUE and we fail a test
7904 * In either of these cases, alignment has been aborted.
7905 */
7906 int
7907 cm_TrOutsideAlignHB(CM_t *cm, char *errbuf, ESL_DSQ *dsq, int L, float size_limit, char preset_mode, int pass_idx,
7908 int do_check, CM_TR_HB_MX *mx, CM_TR_HB_MX *ins_mx)
7909 {
7910 int status;
7911 int v,y,z; /* indices for states */
7912 float Jsc,Lsc,Rsc,Tsc; /* temporary variables holding a float score */
7913 int j,d,i,k; /* indices in sequence dimensions */
7914 float **esc_vAA; /* ptr to cm->oesc, optimized emission scores */
7915 float optsc; /* optimal score in <preset_mode>, from Inside */
7916 float escore; /* an emission score, tmp variable */
7917 int voffset; /* index of v in t_v(y) transition scores */
7918 int emitmode; /* EMITLEFT, EMITRIGHT, EMITPAIR, EMITNONE, for state y */
7919 int sd; /* StateDelta(cm->sttype[y]) */
7920 int sdl; /* StateLeftDelta(cm->sttype[y] */
7921 int sdr; /* StateRightDelta(cm->sttype[y] */
7922
7923 /* variables used only if do_check */
7924 int fail_flag = FALSE; /* set to TRUE if do_check and we see a problem */
7925 int vmax; /* i, offset in the matrix */
7926 float tol; /* tolerance for differences in bit scores */
7927
7928 /* band related variables */
7929 int dp_v; /* d index for state v in alpha w/mem eff bands */
7930 int dp_y; /* d index for state y in alpha w/mem eff bands */
7931 int kp_z; /* k (in the d dim) index for state z in alpha w/mem eff bands */
7932 int Lp; /* L index also changes depending on state */
7933 int jp_v, jp_y, jp_z; /* offset j index for states v, y, z */
7934 int kmin, kmax; /* temporary minimum/maximum allowed k */
7935 int jn, jx; /* current minimum/maximum j allowed */
7936 int dn, dx; /* current minimum/maximum d allowed */
7937 int jp_0; /* L offset in ROOT_S's (v==0) j band */
7938 int Lp_0; /* L offset in ROOT_S's (v==0) d band */
7939
7940 /* variables related to truncated alignment (not in cm_CYKInsideAlignHB() */
7941 int fill_L, fill_R, fill_T; /* must we fill in the L, R, and T matrices? */
7942 int do_J_v, do_J_y, do_J_z; /* must we fill J matrix deck for state v, y, z? */
7943 int do_L_v, do_L_y, do_L_z; /* must we fill L matrix deck for state v, y, z? */
7944 int do_R_v, do_R_y, do_R_z; /* must we fill R matrix deck for state v, y, z? */
7945 int do_T_v, do_T_y; /* is T matrix valid for state v, y? */
7946 int pty_idx; /* index for truncation penalty, determined by pass_idx */
7947 float trpenalty; /* truncation penalty, differs based on pty_idx and if we're local or global */
7948
7949 /* DP matrix variables */
7950 float ***Jbeta = mx->Jdp; /* pointer to the outside Jbeta DP matrix */
7951 float ***Lbeta = mx->Ldp; /* pointer to the outside Lbeta DP matrix */
7952 float ***Rbeta = mx->Rdp; /* pointer to the outside Rbeta DP matrix */
7953 float ***Tbeta = mx->Tdp; /* pointer to the outside Tbeta DP matrix */
7954
7955 float ***Jalpha = ins_mx->Jdp; /* pointer to the precalc'ed inside Jalpha DP matrix */
7956 float ***Lalpha = ins_mx->Ldp; /* pointer to the precalc'ed inside Lalpha DP matrix */
7957 float ***Ralpha = ins_mx->Rdp; /* pointer to the precalc'ed inside Ralpha DP matrix */
7958 float ***Talpha = ins_mx->Tdp; /* pointer to the precalc'ed inside Talpha DP matrix, only used to possibly get optsc */
7959
7960 /* ptrs to cp9b info, for convenience */
7961 CP9Bands_t *cp9b = cm->cp9b;
7962 int *jmin = cm->cp9b->jmin;
7963 int *jmax = cm->cp9b->jmax;
7964 int **hdmin = cm->cp9b->hdmin;
7965 int **hdmax = cm->cp9b->hdmax;
7966
7967 /* Allocations and initializations */
7968 esc_vAA = cm->oesc; /* a ptr to the optimized emission scores */
7969
7970 /* Determine which matrices we need to fill in, based on <preset_mode> */
7971 if (preset_mode != TRMODE_J && preset_mode != TRMODE_L && preset_mode != TRMODE_R && preset_mode != TRMODE_T) ESL_FAIL(eslEINVAL, errbuf, "cm_TrOutsideAlignHB(): preset_mode is not J, L, R, or T");
7972 if((status = cm_TrFillFromMode(preset_mode, &fill_L, &fill_R, &fill_T)) != eslOK) ESL_FAIL(status, errbuf, "cm_TrOutsideAlignHB(), bogus mode: %d", preset_mode);
7973
7974 /* Determine the truncation penalty index, from the pass_idx */
7975 if((pty_idx = cm_tr_penalties_IdxForPass(pass_idx)) == -1) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_TrOutsideAlignHB(), unexpected pass idx: %d", pass_idx);
7976
7977 /* grow the matrix based on the current sequence and bands */
7978 if((status = cm_tr_hb_mx_GrowTo(cm, mx, errbuf, cm->cp9b, L, size_limit)) != eslOK) return status;
7979
7980 /* initialize all cells of the matrix to IMPOSSIBLE */
7981 if(mx->Jncells_valid > 0) esl_vec_FSet(mx->Jdp_mem, mx->Jncells_valid, IMPOSSIBLE);
7982 if(mx->Lncells_valid > 0 && fill_L) esl_vec_FSet(mx->Ldp_mem, mx->Lncells_valid, IMPOSSIBLE);
7983 if(mx->Rncells_valid > 0 && fill_R) esl_vec_FSet(mx->Rdp_mem, mx->Rncells_valid, IMPOSSIBLE);
7984 if(mx->Tncells_valid > 0 && fill_T) esl_vec_FSet(mx->Tdp_mem, mx->Tncells_valid, IMPOSSIBLE);
7985
7986 /* ensure a full alignment in <preset_mode> to ROOT_S (v==0) is allowed by the bands */
7987 if (preset_mode == TRMODE_J && (! cp9b->Jvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrOutsideAlignHB() preset_mode is J but cp9b->Jvalid[0] is FALSE");
7988 else if (preset_mode == TRMODE_L && (! cp9b->Lvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrOutsideAlignHB() preset_mode is L but cp9b->Lvalid[0] is FALSE");
7989 else if (preset_mode == TRMODE_R && (! cp9b->Rvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrOutsideAlignHB() preset_mode is R but cp9b->Rvalid[0] is FALSE");
7990 else if (preset_mode == TRMODE_T && (! cp9b->Tvalid[0])) ESL_FAIL(eslEINVAL, errbuf, "cm_TrOutsideAlignHB() preset_mode is T but cp9b->Tvalid[0] is FALSE");
7991
7992 if (jmin[0] > L || jmax[0] < L) ESL_FAIL(eslEINVAL, errbuf, "cm_TrOutsideAlignHB(): L (%d) is outside ROOT_S's j band (%d..%d)\n", L, jmin[0], jmax[0]);
7993 jp_0 = L - jmin[0];
7994 if (hdmin[0][jp_0] > L || hdmax[0][jp_0] < L) ESL_FAIL(eslEINVAL, errbuf, "cm_TrOutsideAlignHB(): L (%d) is outside ROOT_S's d band (%d..%d)\n", L, hdmin[0][jp_0], hdmax[0][jp_0]);
7995 Lp_0 = L - hdmin[0][jp_0];
7996
7997 /* initialize cells in the special ROOT_S deck corresponding to full sequence alignments to 0.0 */
7998 if (preset_mode == TRMODE_J) Jbeta[0][jp_0][Lp_0] = 0.; /* a full Joint alignment is outside this cell */
7999 else if(preset_mode == TRMODE_L) Lbeta[0][jp_0][Lp_0] = 0.; /* a full Left alignment is outside this cell */
8000 else if(preset_mode == TRMODE_R) Rbeta[0][jp_0][Lp_0] = 0.; /* a full Right alignment is outside this cell */
8001 else if(preset_mode == TRMODE_T) Tbeta[0][jp_0][Lp_0] = 0.; /* a full Terminal alignment is outside this cell */
8002 else ESL_FAIL(eslEINVAL, errbuf, "cm_TrOutsideAlignHB() preset_mode %d is invalid", preset_mode);
8003
8004 /* set cells corresponding to legal truncated begin entry states to
8005 * the appropriate penalty. In truncated alignment the only way out
8006 * of ROOT_S in local or global mode is via a 'truncated begin' with
8007 * a score (penalty) from cm->trp into any emitting state. The
8008 * penalty was calculated in cm_tr_penalties_Create() and differs
8009 * depending on whether we are in local or global mode and the value
8010 * of 'pty_idx' which was passed in.
8011 */
8012 for(v = 0; v < cm->M; v++) {
8013 if((L >= jmin[v]) && (L <= jmax[v])) {
8014 jp_v = L - jmin[v];
8015 if((L >= hdmin[v][jp_v]) && L <= hdmax[v][jp_v]) {
8016 Lp = L - hdmin[v][jp_v];
8017
8018 trpenalty = (cm->flags & CMH_LOCAL_BEGIN) ? cm->trp->l_ptyAA[pty_idx][v] : cm->trp->g_ptyAA[pty_idx][v];
8019 if(NOT_IMPOSSIBLE(trpenalty)) {
8020 do_J_v = cp9b->Jvalid[v] ? TRUE : FALSE;
8021 do_L_v = cp9b->Lvalid[v] && fill_L ? TRUE : FALSE;
8022 do_R_v = cp9b->Rvalid[v] && fill_R ? TRUE : FALSE;
8023 do_T_v = cp9b->Tvalid[v] && fill_T ? TRUE : FALSE;
8024 if(preset_mode == TRMODE_J && do_J_v) Jbeta[v][jp_v][Lp] = trpenalty; /* a full Joint alignment is outside this cell */
8025 if(preset_mode == TRMODE_L && do_L_v) Lbeta[v][jp_v][Lp] = trpenalty; /* a full Left alignment is outside this cell */
8026 if(preset_mode == TRMODE_R && do_R_v) Rbeta[v][jp_v][Lp] = trpenalty; /* a full Right alignment is outside this cell */
8027 if(preset_mode == TRMODE_T && do_T_v && cm->sttype[v] == B_st) {
8028 Tbeta[v][jp_v][Lp] = trpenalty; /* a full Terminal alignment is outside this cell */
8029 }
8030 }
8031 }
8032 }
8033 }
8034 /* done allocation/initialization */
8035
8036 /* Recursion: main loop down through the decks */
8037 for (v = 1; v < cm->M; v++) { /* start at state 1 because we set all values for ROOT_S state 0 above */
8038 if(! StateIsDetached(cm, v)) {
8039 sd = StateDelta(cm->sttype[v]);
8040 sdr = StateRightDelta(cm->sttype[v]);
8041 do_J_v = cp9b->Jvalid[v] ? TRUE : FALSE;
8042 do_L_v = cp9b->Lvalid[v] && fill_L ? TRUE : FALSE;
8043 do_R_v = cp9b->Rvalid[v] && fill_R ? TRUE : FALSE;
8044 do_T_v = cp9b->Tvalid[v] && fill_T ? TRUE : FALSE;
8045
8046 /* if the v deck is invalid in J, L R and T mode, all states for v will remain impossible */
8047 if(! (do_J_v || do_L_v || do_R_v || do_T_v)) continue;
8048
8049 if (cm->stid[v] == BEGL_S) { /* BEGL_S */
8050 y = cm->plast[v]; /* the parent bifurcation */
8051 z = cm->cnum[y]; /* the other (right) S state */
8052
8053 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
8054 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
8055 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
8056 do_T_y = cp9b->Tvalid[y] && fill_T ? TRUE : FALSE; /* will be FALSE, y is not a B_st */
8057
8058 do_J_z = cp9b->Jvalid[z] ? TRUE : FALSE;
8059 do_L_z = cp9b->Lvalid[z] && fill_L ? TRUE : FALSE;
8060 do_R_z = cp9b->Rvalid[z] && fill_R ? TRUE : FALSE;
8061
8062 for (j = jmax[v]; j >= jmin[v]; j--) {
8063 ESL_DASSERT1((j >= 0 && j <= L));
8064 jp_v = j - jmin[v];
8065 jp_y = j - jmin[y];
8066 jp_z = j - jmin[z];
8067 i = j-d+1;
8068 for (d = hdmax[v][jp_v]; d >= hdmin[v][jp_v]; d--) {
8069 dp_v = d - hdmin[v][jp_v];
8070 /* Find the first k value that implies a valid cell in the y and z decks.
8071 * This k must satisfy the following 8 inequalities (some may be redundant):
8072 * NOTE: these are different from those in Inside() (for one thing, v and y
8073 * (BEGL_S and BIF_B here respectively) are switched relative to Inside.
8074 *
8075 * (1) k <= jmax[y] - j;
8076 * (2) k >= jmin[y] - j;
8077 * (3) k <= jmax[z] - j;
8078 * (4) k >= jmin[z] - j;
8079 * 1 and 2 guarantee (j+k) is within state y's j band
8080 * 3 and 4 guarantee (j+k) is within state z's j band
8081 *
8082 * (5) k >= hdmin[y][j-jmin[y]+k] - d;
8083 * (6) k <= hdmax[y][j-jmin[y]+k] - d;
8084 * 5 and 6 guarantee k+d is within y's j=(j+k), d band
8085 *
8086 * (7) k >= hdmin[z][j-jmin[z]+k];
8087 * (8) k <= hdmax[z][j-jmin[z]+k];
8088 * 5 and 6 guarantee k is within state z's j=(j+k) d band
8089 */
8090 kmin = ESL_MAX(jmin[y], jmin[z]) - j;
8091 kmax = ESL_MIN(jmax[y], jmax[z]) - j;
8092 /* kmin and kmax satisfy inequalities (1-4) */
8093 /* RHS of inequalities 5-8 are dependent on k, so we check
8094 * for these within the next for loop. */
8095 for(k = kmin; k <= kmax; k++) {
8096 if(k < (hdmin[y][jp_y+k] - d) || k > (hdmax[y][jp_y+k] - d)) continue;
8097 /* above line continues if inequality 5 or 6 is violated */
8098 if(k < (hdmin[z][jp_z+k]) || k > (hdmax[z][jp_z+k])) continue;
8099 /* above line continues if inequality 7 or 8 is violated */
8100
8101 /* if we get here for current k, all 8 inequalities have been satisified
8102 * so we know the cells corresponding to the platonic
8103 * matrix cells alpha[v][j][d], alpha[y][j+k][d+k], and
8104 * alpha[z][j+k][k] are all within the bands. These
8105 * cells correspond to beta[v][jp_v][dp_v],
8106 * beta[y][jp_y+k][d-hdmin[y][jp_y+k]+k],
8107 * and alpha[z][jp_z][k-hdmin[z][jp_z+k]];
8108 */
8109 kp_z = k-hdmin[z][jp_z+k];
8110 dp_y = d-hdmin[y][jp_y+k];
8111
8112 if(do_J_v && do_J_y && do_J_z) Jbeta[v][jp_v][dp_v] = FLogsum(Jbeta[v][jp_v][dp_v], Jbeta[y][jp_y+k][dp_y+k] + Jalpha[z][jp_z+k][kp_z]); /* A */
8113 if(do_J_v && do_L_y && do_L_z) Jbeta[v][jp_v][dp_v] = FLogsum(Jbeta[v][jp_v][dp_v], Lbeta[y][jp_y+k][dp_y+k] + Lalpha[z][jp_z+k][kp_z]); /* B */
8114 if(do_R_v && do_R_y && do_J_z) Rbeta[v][jp_v][dp_v] = FLogsum(Rbeta[v][jp_v][dp_v], Rbeta[y][jp_y+k][dp_y+k] + Jalpha[z][jp_z+k][kp_z]); /* C */
8115 if(d == j && (j+k) == L &&
8116 do_R_v && do_T_y && do_L_z) Rbeta[v][jp_v][dp_v] = FLogsum(Rbeta[v][jp_v][dp_v], Tbeta[y][jp_y+k][dp_y+k] + Lalpha[z][jp_z+k][kp_z]); /* D */
8117 /* Note: Tbeta[y][j+k==L][d+k==L] will be 0.0 because it
8118 * was initialized that way. That T cell includes the
8119 * full target 1..L (any valid T alignment must because
8120 * we must account for the full target) rooted at a B
8121 * state, and a transition from that B state to this
8122 * BEGL_S is always probability 1.0.
8123 */
8124 } /* end of for k loop */
8125 } /* end of for d loop */
8126 } /* end of for j loop */
8127 /* Two more special cases in truncated alignment, we have to
8128 * do these within their own for j and for d loops because j
8129 * and d has different restrictions than it does in the
8130 * above for j and for d loops we just closed.
8131 */
8132 if(do_L_y && (do_J_v || do_L_v)) {
8133 jn = ESL_MAX(jmin[v], jmin[y]);
8134 jx = ESL_MIN(jmax[v], jmax[y]);
8135 for (j = jx; j >= jn; j--) {
8136 jp_v = j - jmin[v];
8137 jp_y = j - jmin[y];
8138 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y]);
8139 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y]);
8140 for (d = dx; d >= dn; d--) {
8141 dp_v = d-hdmin[v][jp_v];
8142 dp_y = d-hdmin[y][jp_y];
8143 if(do_J_v) Jbeta[v][jp_v][dp_v] = FLogsum(Jbeta[v][jp_v][dp_v], Lbeta[y][jp_y][dp_y]); /* entire sequence on left, no sequence on right, k == 0 */
8144 if(do_L_v) Lbeta[v][jp_v][dp_v] = FLogsum(Lbeta[v][jp_v][dp_v], Lbeta[y][jp_y][dp_y]); /* entire sequence on left, no sequence on right, k == 0 */
8145 }
8146 }
8147 }
8148 } /* end of 'if (cm->stid[v] == BEGL_S */
8149 else if (cm->stid[v] == BEGR_S) {
8150 y = cm->plast[v]; /* the parent bifurcation */
8151 z = cm->cfirst[y]; /* the other (left) S state */
8152
8153 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
8154 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
8155 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
8156 do_T_y = cp9b->Tvalid[y] && fill_T ? TRUE : FALSE;
8157
8158 do_J_z = cp9b->Jvalid[z] ? TRUE : FALSE;
8159 do_L_z = cp9b->Lvalid[z] && fill_L ? TRUE : FALSE;
8160 do_R_z = cp9b->Rvalid[z] && fill_R ? TRUE : FALSE;
8161
8162 jn = ESL_MAX(jmin[v], jmin[y]);
8163 jx = ESL_MIN(jmax[v], jmax[y]);
8164 for (j = jx; j >= jn; j--) {
8165 ESL_DASSERT1((j >= 0 && j <= L));
8166 jp_v = j - jmin[v];
8167 jp_y = j - jmin[y];
8168 jp_z = j - jmin[z];
8169
8170 dn = ESL_MAX(hdmin[v][jp_v], j-jmax[z]);
8171 dx = ESL_MIN(hdmax[v][jp_v], jp_z);
8172 /* above makes sure that j,d are valid for state z: (jmin[z] + d) >= j >= (jmax[z] + d) */
8173 i = j-dx+1;
8174 for (d = dx; d >= dn; d--, i++) {
8175 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha w/mem eff bands */
8176 /* Find the first k value that implies a valid cell in the y and z decks.
8177 * This k must satisfy the following 4 inequalities (some may be redundant):
8178 * NOTE: these are different from those in Inside() (for one thing, v and y
8179 * (BEGR_S and BIF_B here respectively) are switched relative to Inside.
8180 *
8181 * (1) k >= hdmin[y][j-jmin[y]] - d;
8182 * (2) k <= hdmax[y][j-jmin[y]] - d;
8183 * 1 and 2 guarantee (d+k) is within state y's j=(j) d band
8184 *
8185 * (3) k >= hdmin[z][j-jmin[z]-d];
8186 * (4) k <= hdmax[z][j-jmin[z]-d];
8187 * 3 and 4 guarantee k is within z's j=(j-d) d band
8188 *
8189 */
8190 kmin = ESL_MAX((hdmin[y][jp_y]-d), (hdmin[z][jp_z-d]));
8191 kmax = ESL_MIN((hdmax[y][jp_y]-d), (hdmax[z][jp_z-d]));
8192 /* kmin and kmax satisfy inequalities (1-4) */
8193 for(k = kmin; k <= kmax; k++) {
8194 /* for current k, all 4 inequalities have been satisified
8195 * so we know the cells corresponding to the platonic
8196 * matrix cells beta[v][j][d], beta[y][j][d+k], and
8197 * alpha[z][j-d][k] are all within the bands. These
8198 * cells correspond to beta[v][jp_v][dp_v],
8199 * beta[y][jp_y+k][d-hdmin[y][jp_y]+k],
8200 * and alpha[z][jp_z-d][k-hdmin[z][jp_z-d]];
8201 */
8202 kp_z = k-hdmin[z][jp_z-d];
8203 dp_y = d-hdmin[y][jp_y];
8204
8205 if(do_J_v && do_J_y && do_J_z) Jbeta[v][jp_v][dp_v] = FLogsum(Jbeta[v][jp_v][dp_v], Jbeta[y][jp_y][dp_y+k] + Jalpha[z][jp_z-d][kp_z]); /* A */
8206 if(do_J_v && do_R_y && do_R_z) Jbeta[v][jp_v][dp_v] = FLogsum(Jbeta[v][jp_v][dp_v], Rbeta[y][jp_y][dp_y+k] + Ralpha[z][jp_z-d][kp_z]); /* C */
8207 if(do_L_v && do_L_y && do_J_z) Lbeta[v][jp_v][dp_v] = FLogsum(Lbeta[v][jp_v][dp_v], Lbeta[y][jp_y][dp_y+k] + Jalpha[z][jp_z-d][kp_z]); /* B */
8208 if(k == (i-1) && j == L &&
8209 do_L_v && do_T_y && do_R_z) Lbeta[v][jp_v][dp_v] = FLogsum(Lbeta[v][jp_v][dp_v], Tbeta[y][jp_y][dp_y+k] + Ralpha[z][jp_z-d][kp_z]); /* D */
8210 /* Note: Tbeta[y][j==L][d+k==L] will be 0.0 because it
8211 * was initialized that way. That T cell includes the
8212 * full target 1..L (any valid T alignment must because
8213 * we must account for the full target) rooted at a B
8214 * state, and a transition from that B state to this
8215 * BEGR_S is always probability 1.0.
8216 */
8217 } /* end of for k loop */
8218 } /* end of for d loop */
8219 /* Two more special cases in truncated alignment, we have to
8220 * do these within their own for d loop because d has
8221 * different restrictions than it does in the above for d
8222 * loop we just closed. j's restrictions are the same
8223 * though, so we stay inside the for j loop.
8224 */
8225 if(do_R_y && (do_J_v || do_R_v)) {
8226 dn = ESL_MAX(hdmin[v][jp_v], hdmin[y][jp_y]);
8227 dx = ESL_MIN(hdmax[v][jp_v], hdmax[y][jp_y]);
8228 for (d = dx; d >= dn; d--) {
8229 dp_v = d-hdmin[v][jp_v];
8230 dp_y = d-hdmin[y][jp_y];
8231 if(do_J_v) Jbeta[v][jp_v][dp_v] = FLogsum(Jbeta[v][jp_v][dp_v], Rbeta[y][jp_y][dp_y]); /* entire sequence on right, no sequence on left, k == 0 */
8232 if(do_R_v) Rbeta[v][jp_v][dp_v] = FLogsum(Rbeta[v][jp_v][dp_v], Rbeta[y][jp_y][dp_y]); /* entire sequence on right, no sequence on left, k == 0 */
8233 }
8234 }
8235 } /* end of for j loop */
8236 } /* end of 'else if (cm->stid[v] == BEGR_S */
8237 else { /* (cm->sttype[v] != BEGL_S && cm->sttype[v] != BEGR_S */
8238 /* in cm_CYKOutsideAlignHB(), IL and IR states are separated
8239 * out from the other states at this stage because only they
8240 * can self-transit, making it slightly more efficient to
8241 * handle non-inserts differently. In truncated mode there's
8242 * more special cases so I've decided to collapse all states
8243 * together here. An analogous form of the following block is
8244 * used only for IL/IR states in cm_CYKOutsideAlignHB().
8245 *
8246 * ILs and IRs can self transit, this means that
8247 * {J,L,R}beta[v][j][d] must be fully calculated before
8248 * {J,L,R}beta[v][j][d+1] can be started to be calculated,
8249 * forcing the following nesting order: for j { for d { for y
8250 * { } } } for non-self-transitioners, we could do a more
8251 * efficient nesting order (you can see it in
8252 * cm_CYKOutsideAlignHB() but we don't here because truncation
8253 * makes it more complex.
8254 */
8255 for (j = jmax[v]; j >= jmin[v]; j--) {
8256 ESL_DASSERT1((j >= 0 && j <= L));
8257 jp_v = j - jmin[v];
8258 for (d = hdmax[v][jp_v]; d >= hdmin[v][jp_v]; d--) {
8259 i = j-d+1;
8260 dp_v = d - hdmin[v][jp_v]; /* d index for state v in alpha w/mem eff bands */
8261 for (y = cm->plast[v]; y > cm->plast[v]-cm->pnum[v]; y--) {
8262 /* mind the following sneaky if statement: in truncated
8263 * aln, the only way out of state 0 is through a
8264 * truncated begin, which we handled above (search for
8265 * 'trpenalty'). If we're in local mode transitions out
8266 * of 0 will have IMPOSSIBLE scores, but NOT if we're in
8267 * glocal mode, so we need this 'if'.
8268 */
8269 if(y != 0) {
8270 voffset = v - cm->cfirst[y]; /* gotta calculate the transition score index for t_y(v) */
8271 sd = StateDelta(cm->sttype[y]);
8272 sdl = StateLeftDelta(cm->sttype[y]);
8273 sdr = StateRightDelta(cm->sttype[y]);
8274
8275 do_J_y = cp9b->Jvalid[y] ? TRUE : FALSE;
8276 do_L_y = cp9b->Lvalid[y] && fill_L ? TRUE : FALSE;
8277 do_R_y = cp9b->Rvalid[y] && fill_R ? TRUE : FALSE;
8278 do_T_y = cp9b->Tvalid[y] && fill_T ? TRUE : FALSE; /* will be FALSE, y is not a B_st */
8279
8280 /* if the y deck is invalid in J, L and R mode, we don't have to update v based on transitions from y */
8281 if (! (do_J_y || do_L_y || do_R_y)) continue;
8282
8283 /* Note: this looks like it can be optimized, I tried but my 'optimization' slowed the code, so I reverted [EPN] */
8284 switch(cm->sttype[y]) {
8285 case MP_st:
8286 jp_y = j - jmin[y];
8287 if(j != L && d != j && /* boundary condition */
8288 do_J_v && do_J_y && /* J deck is valid for v and y */
8289 (j+sdr >= jmin[y] && j+sdr <= jmax[y]) && /* j+sdr is within y's j band */
8290 (d+sd >= hdmin[y][jp_y+sdr] && d+sd <= hdmax[y][jp_y+sdr])) /* d+sd is within y's d band for j+sdr */
8291 {
8292 dp_y = d - hdmin[y][jp_y+sdr]; /* d index for state y */
8293 escore = esc_vAA[y][dsq[i-1]*cm->abc->Kp+dsq[j+1]];
8294 Jbeta[v][jp_v][dp_v] = FLogsum(Jbeta[v][jp_v][dp_v], Jbeta[y][jp_y+sdr][dp_y+sd] + cm->tsc[y][voffset] + escore);
8295 }
8296 if(j == L && d != j && /* boundary condition, only allow transition from L if we haven't emitted any residues rightwise (j==L) */
8297 do_L_y && /* L deck is valid for y */
8298 (j >= jmin[y] && j <= jmax[y]) && /* j is within y's j band */
8299 (d+sdl >= hdmin[y][jp_y] && d+sdl <= hdmax[y][jp_y])) /* d+sdl is within y's d band for j */
8300 {
8301 dp_y = d - hdmin[y][jp_y]; /* d index for state y */
8302 escore = cm->lmesc[y][dsq[i-1]];
8303 if(do_J_v) Jbeta[v][jp_v][dp_v] = FLogsum(Jbeta[v][jp_v][dp_v], Lbeta[y][jp_y][dp_y+sdl] + cm->tsc[y][voffset] + escore);
8304 if(do_L_v) Lbeta[v][jp_v][dp_v] = FLogsum(Lbeta[v][jp_v][dp_v], Lbeta[y][jp_y][dp_y+sdl] + cm->tsc[y][voffset] + escore);
8305 }
8306 if(i == 1 && j != L && /* boundary condition, only allow transition from R if we haven't emitted any residues leftwise (i==1) */
8307 do_R_y && /* R deck is valid for y */
8308 (j+sdr >= jmin[y] && j+sdr <= jmax[y]) && /* j+sdr is within y's j band */
8309 (d+sdr >= hdmin[y][jp_y+sdr] && d+sdr <= hdmax[y][jp_y+sdr])) /* d+sdr is within y's d band for j+sdr */
8310 {
8311 dp_y = d - hdmin[y][jp_y+sdr]; /* d index for state y */
8312 escore = cm->rmesc[y][dsq[j+1]];
8313 if(do_J_v) Jbeta[v][jp_v][dp_v] = FLogsum(Jbeta[v][jp_v][dp_v], Rbeta[y][jp_y+sdr][dp_y+sdr] + cm->tsc[y][voffset] + escore);
8314 if(do_R_v) Rbeta[v][jp_v][dp_v] = FLogsum(Rbeta[v][jp_v][dp_v], Rbeta[y][jp_y+sdr][dp_y+sdr] + cm->tsc[y][voffset] + escore);
8315 }
8316 break;
8317
8318 case ML_st:
8319 case IL_st:
8320 jp_y = j - jmin[y];
8321 if(d != j && /* boundary case */
8322 (j >= jmin[y] && j <= jmax[y]) && /* j is within y's j band */
8323 (d+sdl >= hdmin[y][jp_y] && d+sdl <= hdmax[y][jp_y])) /* d+sdl is within y's d band for j */
8324 {
8325 dp_y = d - hdmin[y][jp_y];
8326 escore = cm->oesc[y][dsq[i-1]];
8327 if(do_J_v && do_J_y) Jbeta[v][jp_v][dp_v] = FLogsum(Jbeta[v][jp_v][dp_v], Jbeta[y][jp_y][dp_y+sd] + cm->tsc[y][voffset] + escore);
8328 if(do_L_v && do_L_y) Lbeta[v][jp_v][dp_v] = FLogsum(Lbeta[v][jp_v][dp_v], Lbeta[y][jp_y][dp_y+sd] + cm->tsc[y][voffset] + escore);
8329 }
8330 if(i == 1 && /* boundary condition, only allow transition from R if we're emitting first residue 1 from y */
8331 v != y && /* will only happen if v == IL, we don't allow silent self transitions from IL->IL */
8332 do_R_y && /* R deck is valid for y */
8333 (j >= jmin[y] && j <= jmax[y]) && /* j is within y's j band */
8334 (d >= hdmin[y][jp_y] && d <= hdmax[y][jp_y])) /* d+sdr(==d) is within y's d band for j */
8335 {
8336 dp_y = d - hdmin[y][jp_y];
8337 if(do_J_v) Jbeta[v][jp_v][dp_v] = FLogsum(Jbeta[v][jp_v][dp_v], Rbeta[y][jp_y][dp_y] + cm->tsc[y][voffset]);
8338 if(do_R_v) Rbeta[v][jp_v][dp_v] = FLogsum(Rbeta[v][jp_v][dp_v], Rbeta[y][jp_y][dp_y] + cm->tsc[y][voffset]);
8339 }
8340 break;
8341
8342 case MR_st:
8343 case IR_st:
8344 jp_y = j - jmin[y];
8345 if (j != L && /* boundary condition */
8346 (j+sdr >= jmin[y] && j+sdr <= jmax[y]) && /* j+sdr is within y's j band */
8347 (d+sd >= hdmin[y][jp_y+sdr] && d+sd <= hdmax[y][jp_y+sdr])) /* d+sd is within y's d band for j+sdr */
8348 {
8349 dp_y = d - hdmin[y][jp_y+sdr];
8350 escore = cm->oesc[y][dsq[j+1]];
8351 if(do_J_v && do_J_y) Jbeta[v][jp_v][dp_v] = FLogsum(Jbeta[v][jp_v][dp_v], Jbeta[y][jp_y+sdr][dp_y+sd] + cm->tsc[y][voffset] + escore);
8352 if(do_R_v && do_R_y) Rbeta[v][jp_v][dp_v] = FLogsum(Rbeta[v][jp_v][dp_v], Rbeta[y][jp_y+sdr][dp_y+sd] + cm->tsc[y][voffset] + escore);
8353 }
8354 if(j == L && /* boundary condition, only allow transition from L if we're emitting final residue L from y */
8355 v != y && /* will only happen if v == IR, we don't allow silent self transitions from IR->IR */
8356 do_L_y && /* L deck is valid for y */
8357 (j >= jmin[y] && j <= jmax[y]) && /* j is within y's j band */
8358 (d >= hdmin[y][jp_y] && d <= hdmax[y][jp_y])) /* d+sdl(==d) is within y's d band for j */
8359 {
8360 dp_y = d - hdmin[y][jp_y];
8361 if(do_J_v) Jbeta[v][jp_v][dp_v] = FLogsum(Jbeta[v][jp_v][dp_v], Lbeta[y][jp_y][dp_y] + cm->tsc[y][voffset]);
8362 if(do_L_v) Lbeta[v][jp_v][dp_v] = FLogsum(Lbeta[v][jp_v][dp_v], Lbeta[y][jp_y][dp_y] + cm->tsc[y][voffset]);
8363 }
8364 break;
8365 case S_st:
8366 case E_st:
8367 case D_st:
8368 jp_y = j - jmin[y];
8369 if((j >= jmin[y] && j <= jmax[y]) &&
8370 (d >= hdmin[y][jp_y] && d <= hdmax[y][jp_y]))
8371 {
8372 dp_y = d - hdmin[y][jp_y]; /* d index for state y */
8373 if(do_J_v && do_J_y) Jbeta[v][jp_v][dp_v] = FLogsum(Jbeta[v][jp_v][dp_v], Jbeta[y][jp_y][dp_y] + cm->tsc[y][voffset]);
8374 if(do_L_v && do_L_y) Lbeta[v][jp_v][dp_v] = FLogsum(Lbeta[v][jp_v][dp_v], Lbeta[y][jp_y][dp_y] + cm->tsc[y][voffset]);
8375 if(do_R_v && do_R_y) Rbeta[v][jp_v][dp_v] = FLogsum(Rbeta[v][jp_v][dp_v], Rbeta[y][jp_y][dp_y] + cm->tsc[y][voffset]);
8376 }
8377 break;
8378 } /* end of switch(cm->sttype[y] */
8379 } /* end of sneaky if y != 0 */
8380 } /* ends for loop over parent states. we now know beta[v][j][d] for this d */
8381 if (do_J_v && Jbeta[v][jp_v][dp_v] < IMPOSSIBLE) Jbeta[v][jp_v][dp_v] = IMPOSSIBLE;
8382 if (do_L_v && Lbeta[v][jp_v][dp_v] < IMPOSSIBLE) Lbeta[v][jp_v][dp_v] = IMPOSSIBLE;
8383 if (do_R_v && Rbeta[v][jp_v][dp_v] < IMPOSSIBLE) Rbeta[v][jp_v][dp_v] = IMPOSSIBLE;
8384 } /* ends loop over d. We know all beta[v][j][d] in this row j and state v */
8385 } /* end loop over jp. We know beta for this whole state */
8386 } /* end of 'else' (entered if cm->sttype[v] != BEGL_S nor BEGR_S */
8387 /* we're done calculating deck v for everything but local ends */
8388
8389 /* deal with local alignment end transitions v->EL (EL = deck at M.) */
8390 if ((cm->flags & CMH_LOCAL_END) && NOT_IMPOSSIBLE(cm->endsc[v])) {
8391 sd = StateDelta(cm->sttype[v]); /* note sd is for state v */
8392 sdl = StateLeftDelta(cm->sttype[v]); /* note sdl is for state v */
8393 sdr = StateRightDelta(cm->sttype[v]); /* note sdr is for state v */
8394 emitmode = Emitmode(cm->sttype[v]); /* note emitmode is for state v */
8395
8396 /* we handle all three possible modes (J,L,R) differently because they have different boundary conditions */
8397
8398 /* J mode */
8399 if(do_J_v && cp9b->Jvalid[cm->M]) {
8400 jn = jmin[v] - sdr;
8401 jx = jmax[v] - sdr;
8402 for (j = jn; j <= jx; j++) {
8403 jp_v = j - jmin[v];
8404 dn = hdmin[v][jp_v + sdr] - sd;
8405 dx = hdmax[v][jp_v + sdr] - sd;
8406 i = j-dn+1; /* we'll decrement this in for (d... loops inside switch below */
8407 dp_v = dn - hdmin[v][jp_v + sdr]; /* we'll increment this in for (d... loops inside switch below */
8408
8409 switch (emitmode) {
8410 case EMITPAIR:
8411 for (d = dn; d <= dx; d++, dp_v++, i--) {
8412 escore = esc_vAA[v][dsq[i-1]*cm->abc->Kp+dsq[j+1]];
8413 Jbeta[cm->M][j][d] = FLogsum(Jbeta[cm->M][j][d], (Jbeta[v][jp_v+sdr][dp_v+sd] + cm->endsc[v] + escore));
8414 }
8415 break;
8416 case EMITLEFT:
8417 for (d = dn; d <= dx; d++, dp_v++, i--) {
8418 escore = esc_vAA[v][dsq[i-1]];
8419 Jbeta[cm->M][j][d] = FLogsum(Jbeta[cm->M][j][d], (Jbeta[v][jp_v+sdr][dp_v+sd] + cm->endsc[v] + escore));
8420 }
8421 break;
8422
8423 case EMITRIGHT:
8424 escore = esc_vAA[v][dsq[j+1]];
8425 for (d = dn; d <= dx; d++, dp_v++) {
8426 Jbeta[cm->M][j][d] = FLogsum(Jbeta[cm->M][j][d], (Jbeta[v][jp_v+sdr][dp_v+sd] + cm->endsc[v] + escore));
8427 }
8428 break;
8429
8430 case EMITNONE:
8431 for (d = dn; d <= dx; d++, dp_v++) {
8432 Jbeta[cm->M][j][d] = FLogsum(Jbeta[cm->M][j][d], (Jbeta[v][jp_v+sdr][dp_v+sd] + cm->endsc[v]));
8433 }
8434 break;
8435 }
8436 }
8437 }
8438
8439 /* L mode: again, this code is inefficient, but I chose not to try to optimize lest it get more complex */
8440 if(do_L_v && cp9b->Lvalid[cm->M]) {
8441 jn = jmin[v];
8442 jx = jmax[v];
8443 for (j = jn; j <= jx; j++) {
8444 jp_v = j - jmin[v];
8445 dn = hdmin[v][jp_v] - sdl;
8446 dx = hdmax[v][jp_v] - sdl;
8447 i = j-dn+1; /* we'll decrement this in for (d... loops inside switch below */
8448 dp_v = dn - hdmin[v][jp_v]; /* we'll increment this in for (d... loops inside switch below */
8449
8450 switch (emitmode) {
8451 case EMITPAIR:
8452 if(j == L) { /* only allow transition from L if we haven't emitted any residues rightwise (j==L) */
8453 for (d = dn; d <= dx; d++, dp_v++, i--) {
8454 escore = cm->lmesc[v][dsq[i-1]];
8455 Lbeta[cm->M][j][d] = FLogsum(Lbeta[cm->M][j][d], (Lbeta[v][jp_v][dp_v+sdl] + cm->endsc[v] + escore));
8456 }
8457 }
8458 break;
8459
8460 case EMITLEFT:
8461 for (d = dn; d <= dx; d++, dp_v++, i--) {
8462 escore = esc_vAA[v][dsq[i-1]];
8463 Lbeta[cm->M][j][d] = FLogsum(Lbeta[cm->M][j][d], (Lbeta[v][jp_v][dp_v+sdl] + cm->endsc[v] + escore));
8464 }
8465 break;
8466
8467 case EMITRIGHT:
8468 if(j == L) { /* only allow transition from L if we haven't emitted any residues rightwise (j==L) */
8469 for (d = dn; d <= dx; d++, dp_v++) {
8470 Lbeta[cm->M][j][d] = FLogsum(Lbeta[cm->M][j][d], (Lbeta[v][jp_v][dp_v] + cm->endsc[v]));
8471 }
8472 }
8473 break;
8474
8475 case EMITNONE:
8476 for (d = dn; d <= dx; d++, dp_v++) {
8477 Lbeta[cm->M][j][d] = FLogsum(Lbeta[cm->M][j][d], (Lbeta[v][jp_v][dp_v] + cm->endsc[v]));
8478 }
8479 break;
8480 }
8481 }
8482 } /* end of if(do_L_v) */
8483
8484 /* R mode: again, this code is inefficient, but I chose not to try to optimize lest it get more complex */
8485 if(do_R_v && cp9b->Rvalid[cm->M]) {
8486 jn = jmin[v] - sdr;
8487 jx = jmax[v] - sdr;
8488 for (j = jn; j <= jx; j++) {
8489 jp_v = j - jmin[v];
8490 dn = hdmin[v][jp_v + sdr] - sdr;
8491 dx = hdmax[v][jp_v + sdr] - sdr;
8492 i = j-dn+1; /* we'll decrement this in for (d... loops inside switch below */
8493 dp_v = dn - hdmin[v][jp_v + sdr]; /* we'll increment this in for (d... loops inside switch below */
8494
8495 switch (emitmode) {
8496 case EMITPAIR:
8497 for (d = dn; d <= dx; d++, dp_v++, i--) {
8498 if(i == 1) { /* only allow transition from R if we haven't emitted any residues leftwise (i==1) */
8499 escore = cm->rmesc[v][dsq[j+1]];
8500 Rbeta[cm->M][j][d] = FLogsum(Rbeta[cm->M][j][d], (Rbeta[v][jp_v+sdr][dp_v+sdr] + cm->endsc[v] + escore));
8501 }
8502 }
8503 break;
8504 case EMITLEFT:
8505 for (d = dn; d <= dx; d++, dp_v++, i--) {
8506 if(i == 1) { /* only allow transition from R if we haven't emitted any residues leftwise (i==1) */
8507 Rbeta[cm->M][j][d] = FLogsum(Rbeta[cm->M][j][d], (Rbeta[v][jp_v][dp_v] + cm->endsc[v]));
8508 }
8509 }
8510 break;
8511
8512 case EMITRIGHT:
8513 escore = esc_vAA[v][dsq[j+1]];
8514 for (d = dn; d <= dx; d++, dp_v++) {
8515 Rbeta[cm->M][j][d] = FLogsum(Rbeta[cm->M][j][d], (Rbeta[v][jp_v+sdr][dp_v+sdr] + cm->endsc[v] + escore));
8516 }
8517 break;
8518
8519 case EMITNONE:
8520 for (d = dn; d <= dx; d++, dp_v++) {
8521 Rbeta[cm->M][j][d] = FLogsum(Rbeta[cm->M][j][d], (Rbeta[v][jp_v+sdr][dp_v+sdr] + cm->endsc[v]));
8522 }
8523 break;
8524 }
8525 }
8526 } /* end of if(do_R_v) */
8527 } /* end of calculating EL scores */
8528 } /* end of if !StateIsDetached() */
8529 } /* end loop over decks v. */
8530
8531 /* Deal with last step needed for local alignment
8532 * w.r.t. ends: left-emitting, EL->EL transitions. (EL = deck at M.)
8533 */
8534 if (cm->flags & CMH_LOCAL_END) {
8535 if(cp9b->Jvalid[cm->M]) {
8536 for (j = L; j > 0; j--) { /* careful w/ boundary here */
8537 for (d = j-1; d >= 0; d--) { /* careful w/ boundary here */
8538 Jbeta[cm->M][j][d] = FLogsum(Jbeta[cm->M][j][d], (Jbeta[cm->M][j][d+1] + cm->el_selfsc));
8539 }
8540 }
8541 }
8542 if(fill_L && cp9b->Lvalid[cm->M]) {
8543 for (j = L; j > 0; j--) { /* careful w/ boundary here */
8544 for (d = j-1; d >= 0; d--) { /* careful w/ boundary here */
8545 Lbeta[cm->M][j][d] = FLogsum(Lbeta[cm->M][j][d], (Lbeta[cm->M][j][d+1] + cm->el_selfsc));
8546 }
8547 }
8548 }
8549 if(fill_R && cp9b->Rvalid[cm->M]) {
8550 for (j = L; j > 0; j--) { /* careful w/ boundary here */
8551 for (d = j-1; d >= 0; d--) { /* careful w/ boundary here */
8552 Rbeta[cm->M][j][d] = FLogsum(Rbeta[cm->M][j][d], (Rbeta[cm->M][j][d+1] + cm->el_selfsc));
8553 }
8554 }
8555 }
8556 }
8557 fail_flag = FALSE;
8558 if(do_check) {
8559 /* Check for consistency between the Inside alpha matrix and the
8560 * Outside beta matrix. we assume the Inside CYK parse score
8561 * (optsc) is the optimal score, so for all v,j,d:
8562 *
8563 * Jalpha[v][j][d] + Jbeta[v][j][d] <= optsc
8564 * Lalpha[v][j][d] + Lbeta[v][j][d] <= optsc
8565 * Ralpha[v][j][d] + Rbeta[v][j][d] <= optsc
8566 *
8567 * We do a more extensive check in cm_TrCYKOutsideAlignHB(), but
8568 * it doesn't apply here, because we've summed all parsetrees
8569 * instead of finding only the optimal one.
8570 *
8571 * This is an expensive check and should only be done while
8572 * debugging.
8573 */
8574 vmax = (cm->flags & CMH_LOCAL_END) ? cm->M : cm->M-1;
8575 if (preset_mode == TRMODE_J) optsc = Jalpha[0][jp_0][Lp_0];
8576 else if(preset_mode == TRMODE_L) optsc = Lalpha[0][jp_0][Lp_0];
8577 else if(preset_mode == TRMODE_R) optsc = Ralpha[0][jp_0][Lp_0];
8578 else if(preset_mode == TRMODE_T) optsc = Talpha[0][jp_0][Lp_0];
8579 /* define bit score difference tolerance, somewhat arbitrarily:
8580 * clen <= 200: tolerance is 0.001; then a function of clen:
8581 * clen == 1000 tolerance is 0.005,
8582 * clen == 2000, tolerance is 0.01.
8583 *
8584 * I did this b/c with tests with SSU_rRNA_eukarya I noticed
8585 * failures with bit score differences up to 0.004 or so. This
8586 * could mean a bug, but I couldn't get any average sized model to
8587 * fail with a difference above 0.001, so I blamed it on
8588 * precision. I'm not entirely convinced it isn't a bug but
8589 * until I see a failure on a smaller model it seems precision
8590 * is the most likely explanation, right?
8591 */
8592 tol = ESL_MAX(1e-3, (float) cm->clen / 200000.);
8593 for(v = 0; v <= vmax; v++) {
8594 do_J_v = cp9b->Jvalid[v] ? TRUE : FALSE;
8595 do_L_v = cp9b->Lvalid[v] && fill_L ? TRUE : FALSE;
8596 do_R_v = cp9b->Rvalid[v] && fill_R ? TRUE : FALSE;
8597 do_T_v = cp9b->Tvalid[v] && fill_T ? TRUE : FALSE;
8598 jn = (v == cm->M) ? 1 : jmin[v];
8599 jx = (v == cm->M) ? L : jmax[v];
8600 for(j = jn; j <= jx; j++) {
8601 jp_v = (v == cm->M) ? j : j - jmin[v];
8602 dn = (v == cm->M) ? 0 : hdmin[v][jp_v];
8603 dx = (v == cm->M) ? j : hdmax[v][jp_v];
8604 for(d = dn; d <= dx; d++) {
8605 dp_v = (v == cm->M) ? d : d - hdmin[v][jp_v];
8606 Jsc = (do_J_v) ? Jalpha[v][jp_v][dp_v] + Jbeta[v][jp_v][dp_v] - optsc : IMPOSSIBLE;
8607 Lsc = (do_L_v) ? Lalpha[v][jp_v][dp_v] + Lbeta[v][jp_v][dp_v] - optsc : IMPOSSIBLE;
8608 Rsc = (do_R_v) ? Ralpha[v][jp_v][dp_v] + Rbeta[v][jp_v][dp_v] - optsc : IMPOSSIBLE;
8609 Tsc = (do_T_v) ? Talpha[v][jp_v][dp_v] + Tbeta[v][jp_v][dp_v] - optsc : IMPOSSIBLE;
8610 if(Jsc > tol) {
8611 printf("Check 1 J failure: v: %4d j: %4d d: %4d (%.4f + %.4f) %.4f > %.4f\n",
8612 v, j, d, Jalpha[v][jp_v][dp_v], Jbeta[v][jp_v][dp_v], Jalpha[v][jp_v][dp_v] + Jbeta[v][jp_v][dp_v], optsc);
8613 fail_flag = TRUE;
8614 }
8615 if(Lsc > tol) {
8616 printf("Check 1 L failure: v: %4d j: %4d d: %4d (%.4f + %.4f) %.4f > %.4f\n",
8617 v, j, d, Lalpha[v][jp_v][dp_v], Lbeta[v][jp_v][dp_v], Lalpha[v][jp_v][dp_v] + Lbeta[v][jp_v][dp_v], optsc);
8618 fail_flag = TRUE;
8619 }
8620 if(Rsc > tol) {
8621 printf("Check 1 R failure: v: %4d j: %4d d: %4d (%.4f + %.4f) %.4f > %.4f\n",
8622 v, j, d, Ralpha[v][jp_v][dp_v], Rbeta[v][jp_v][dp_v], Ralpha[v][jp_v][dp_v] + Rbeta[v][jp_v][dp_v], optsc);
8623 fail_flag = TRUE;
8624 }
8625 if(Tsc > tol) {
8626 printf("Check 1 T failure: v: %4d j: %4d d: %4d (%.4f + %.4f) %.4f > %.4f\n",
8627 v, j, d, Talpha[v][jp_v][dp_v], Tbeta[v][jp_v][dp_v], Talpha[v][jp_v][dp_v] + Tbeta[v][jp_v][dp_v], optsc);
8628 fail_flag = TRUE;
8629 }
8630 }
8631 }
8632 }
8633 }
8634 if(fail_flag) for(j = 1; j <= L; j++) printf("dsq[%4d]: %4d\n", j, dsq[j]);
8635
8636 #if eslDEBUGLEVEL >= 2
8637 /* Uncomment to dump matrix to file. Careful...this could be very large. */
8638 /* FILE *fp1; fp1 = fopen("tmp.tru_ohbmx", "w"); cm_tr_hb_mx_Dump(fp1, mx, preset_mode, TRUE); fclose(fp1); */
8639 #endif
8640
8641 if(do_check) {
8642 if(fail_flag) ESL_FAIL(eslFAIL, errbuf, "Tr Inside/Outside HB check FAILED.");
8643 ESL_DPRINTF1(("#DEBUG: SUCCESS! Tr Inside/Outside HB check PASSED.\n"));
8644 printf("SUCCESS! Tr Inside/Outside HB check PASSED.\n");
8645 }
8646
8647 if (preset_mode == TRMODE_J) optsc = Jalpha[0][jp_0][Lp_0];
8648 else if(preset_mode == TRMODE_L) optsc = Lalpha[0][jp_0][Lp_0];
8649 else if(preset_mode == TRMODE_R) optsc = Ralpha[0][jp_0][Lp_0];
8650 else if(preset_mode == TRMODE_T) optsc = Talpha[0][jp_0][Lp_0];
8651 ESL_DPRINTF1(("#DEBUG: \tcm_TrOutsideAlignHB() sc : %f (sc is from Inside!)\n", optsc));
8652
8653 return eslOK;
8654 }
8655
8656 /* Function: cm_TrPosterior()
8657 * Date: EPN, Tue Sep 13 16:18:25 2011
8658 * Note: based on Ian Holmes' P7EmitterPosterior() from HMMER's 2.x postprob.c
8659 *
8660 * Purpose: Combines non-banded cm_TrInside and cm_TrOutside matrices
8661 * into a posterior probability matrix. The value in
8662 * post->{J,L,R}[v][j][d] is the log of the posterior
8663 * probability of a parse subtree rooted at v emitting the
8664 * subsequence i..j (i=j-d+1) and being in J, L, or R mode
8665 * at at state v. The caller must provide a <post> float
8666 * matrix, but this matrix may be the same matrix as that
8667 * provided as Outside <out_mx>, (overwriting it will not
8668 * compromise the algorithm).
8669 *
8670 * Args: cm - the model
8671 * errbuf - char buffer for reporting errors
8672 * L - length of the target sequence we're aligning
8673 * size_limit - max number of Mb for DP matrix, if matrix is bigger return eslERANGE
8674 * preset_mode - mode of alignment: TRMODE_J, TRMODE_L, TRMODE_R, or TRMODE_T
8675 * ins_mx - pre-calculated Inside matrix
8676 * out_mx - pre-calculated Outside matrix
8677 * post_mx - pre-allocated matrix for Posteriors
8678 *
8679 * Return: eslOK on success, eslEINCOMPAT on contract violation
8680 */
8681 int
8682 cm_TrPosterior(CM_t *cm, char *errbuf, int L, float size_limit, char preset_mode,
8683 CM_TR_MX *ins_mx, CM_TR_MX *out_mx, CM_TR_MX *post_mx)
8684 {
8685 int status; /* Easel status code */
8686 int v; /* state index */
8687 int j; /* position */
8688 int d; /* subsequence length */
8689 float sc; /* optimal Inside score */
8690 int fill_L, fill_R, fill_T; /* should we fill-in values for L, R, T? (we always fill in J) */
8691
8692 /* Determine which matrices we need to fill-in, and the optimal score */
8693 if (preset_mode != TRMODE_J && preset_mode != TRMODE_L && preset_mode != TRMODE_R && preset_mode != TRMODE_T) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPosterior(): preset_mode is not J, L, R, or T");
8694 if((status = cm_TrFillFromMode(preset_mode, &fill_L, &fill_R, &fill_T)) != eslOK) ESL_FAIL(status, errbuf, "cm_TrPosterior, bogus preset_mode: %d", preset_mode);
8695 if(preset_mode == TRMODE_J) sc = ins_mx->Jdp[0][L][L];
8696 if(preset_mode == TRMODE_L) sc = ins_mx->Ldp[0][L][L];
8697 if(preset_mode == TRMODE_R) sc = ins_mx->Rdp[0][L][L];
8698 if(preset_mode == TRMODE_T) sc = ins_mx->Tdp[0][L][L];
8699
8700 /* grow our post matrix, but only if isn't also our out_mx in which
8701 * case we know we're already big enought (also in that case we
8702 * don't want to call GrowTo b/c it can potentially free the DP
8703 * matrix memory and reallocate it, which would be bad b/c we
8704 * need the out_mx!)
8705 */
8706 if(post_mx != out_mx) {
8707 if((status = cm_tr_mx_GrowTo(cm, post_mx, errbuf, L, size_limit)) != eslOK) return status;
8708 }
8709
8710 /* If local ends are on, start with the EL state (cm->M), otherwise
8711 * it's not a valid deck. */
8712 if(cm->flags & CMH_LOCAL_END) {
8713 for (j = 0; j <= L; j++) {
8714 for (d = 0; d <= j; d++) {
8715 post_mx->Jdp[cm->M][j][d] = ins_mx->Jdp[cm->M][j][d] + out_mx->Jdp[cm->M][j][d] - sc;
8716 }
8717 }
8718 if(fill_L) {
8719 for (j = 0; j <= L; j++) {
8720 for (d = 0; d <= j; d++) {
8721 post_mx->Ldp[cm->M][j][d] = ins_mx->Ldp[cm->M][j][d] + out_mx->Ldp[cm->M][j][d] - sc;
8722 }
8723 }
8724 }
8725 if(fill_R) {
8726 for (j = 0; j <= L; j++) {
8727 for (d = 0; d <= j; d++) {
8728 post_mx->Rdp[cm->M][j][d] = ins_mx->Rdp[cm->M][j][d] + out_mx->Rdp[cm->M][j][d] - sc;
8729 }
8730 }
8731 }
8732 }
8733
8734 /* Fill in the rest of the matrices */
8735 for (v = cm->M-1; v >= 0; v--) {
8736 for (j = 0; j <= L; j++) {
8737 for (d = 0; d <= j; d++) {
8738 post_mx->Jdp[v][j][d] = ins_mx->Jdp[v][j][d] + out_mx->Jdp[v][j][d] - sc;
8739 }
8740 }
8741 }
8742 if (fill_L) {
8743 for (v = cm->M-1; v >= 0; v--) {
8744 for (j = 0; j <= L; j++) {
8745 for (d = 0; d <= j; d++) {
8746 post_mx->Ldp[v][j][d] = ins_mx->Ldp[v][j][d] + out_mx->Ldp[v][j][d] - sc;
8747 }
8748 }
8749 }
8750 }
8751 if (fill_R) {
8752 for (v = cm->M-1; v >= 0; v--) {
8753 for (j = 0; j <= L; j++) {
8754 for (d = 0; d <= j; d++) {
8755 post_mx->Rdp[v][j][d] = ins_mx->Rdp[v][j][d] + out_mx->Rdp[v][j][d] - sc;
8756 }
8757 }
8758 }
8759 }
8760 if (fill_T) {
8761 for (v = cm->M-1; v >= 0; v--) {
8762 if (v == 0 || cm->sttype[v] == B_st) {
8763 for (j = 0; j <= L; j++) {
8764 for (d = 0; d <= j; d++) {
8765 post_mx->Tdp[v][j][d] = ins_mx->Tdp[v][j][d] + out_mx->Tdp[v][j][d] - sc;
8766 }
8767 }
8768 }
8769 }
8770 }
8771 #if eslDEBUGLEVEL >= 2
8772 /* Uncomment to dump matrix to file. Careful...this could be very large. */
8773 /* FILE *fp1; fp1 = fopen("tmp.tru_pmx", "w"); cm_tr_mx_Dump(fp1, post_mx, preset_mode, TRUE); fclose(fp1); */
8774 #endif
8775
8776 return eslOK;
8777 }
8778
8779
8780 /* Function: cm_TrPosteriorHB()
8781 * Date: EPN, Tue Oct 11 09:24:07 2011
8782 * Note: based on Ian Holmes' P7EmitterPosterior() from HMMER's 2.x postprob.c
8783 *
8784 * Purpose: Combines HMM banded cm_TrInside and cm_TrOutside matrices
8785 * into a posterior probability matrix. Any cells outside of
8786 * the HMM bands do not exist in memory. The value in
8787 * post->{J,L,R}[v][jp_v][dp_v] is the log of the posterior
8788 * probability of a parse subtree rooted at v emitting the
8789 * subsequence i..j (i=j-d+1) and being in J, L, or R mode
8790 * at at state v, with jp_v = j-jmin[v] and dp_v =
8791 * d-hdmin[v][jp_v]. The caller must provide a <post> float
8792 * matrix, but this matrix may be the same matrix as that
8793 * provided as Outside <out_mx>, (overwriting it will not
8794 * compromise the algorithm).
8795 *
8796 * Args: cm - the model
8797 * errbuf - char buffer for reporting errors
8798 * L - length of the target sequence we're aligning
8799 * size_limit - max number of Mb for DP matrix, if matrix is bigger return eslERANGE
8800 * preset_mode - mode of alignment: TRMODE_J, TRMODE_L, TRMODE_R, or TRMODE_T
8801 * ins_mx - pre-calculated Inside matrix
8802 * out_mx - pre-calculated Outside matrix
8803 * post_mx - pre-allocated matrix for Posteriors
8804 *
8805 * Return: <eslOK> on success.
8806 * Throws: <eslERANGE> if required DP matrix size exceeds <size_limit>
8807 * <eslEINVAL> if the full sequence is not within the bands for state 0
8808 * In either case the post_mx is not filled
8809 */
8810 int
8811 cm_TrPosteriorHB(CM_t *cm, char *errbuf, int L, float size_limit, char preset_mode,
8812 CM_TR_HB_MX *ins_mx, CM_TR_HB_MX *out_mx, CM_TR_HB_MX *post_mx)
8813 {
8814 int status; /* Easel status code */
8815 int v; /* state index */
8816 int j; /* position */
8817 int d; /* subsequence length */
8818 int jp_v; /* j offset in HMM banded matrix */
8819 int dp_v; /* d offset in HMM banded matrix */
8820 int jx; /* max j */
8821 int dx; /* max d */
8822 int jp_0; /* L offset in ROOT_S's (v==0) j band */
8823 int Lp_0; /* L offset in ROOT_S's (v==0) d band */
8824 float sc; /* optimal Inside score */
8825 int fill_L, fill_R, fill_T; /* must we fill in the L, R, and T matrices? */
8826
8827 /* ptrs to cp9b info, for convenience */
8828 CP9Bands_t *cp9b = cm->cp9b;
8829 int *jmin = cp9b->jmin;
8830 int *jmax = cp9b->jmax;
8831 int **hdmin = cp9b->hdmin;
8832 int **hdmax = cp9b->hdmax;
8833
8834 /* Determine which matrices we need to fill in, based on <preset_mode> */
8835 if (preset_mode != TRMODE_J && preset_mode != TRMODE_L && preset_mode != TRMODE_R && preset_mode != TRMODE_T) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPosteriorHB(): preset_mode is not J, L, R, or T");
8836 if((status = cm_TrFillFromMode(preset_mode, &fill_L, &fill_R, &fill_T)) != eslOK) ESL_FAIL(status, errbuf, "cm_TrPosteriorHB(), bogus mode: %d", preset_mode);
8837
8838 /* ensure a full alignment to ROOT_S (v==0) is allowed by the bands */
8839 if (cm->cp9b->jmin[0] > L || cm->cp9b->jmax[0] < L) ESL_FAIL(eslEINVAL, errbuf, "cm_CYKInsideAlignHB(): L (%d) is outside ROOT_S's j band (%d..%d)\n", L, cm->cp9b->jmin[0], cm->cp9b->jmax[0]);
8840 jp_0 = L - jmin[0];
8841 if (cm->cp9b->hdmin[0][jp_0] > L || cm->cp9b->hdmax[0][jp_0] < L) ESL_FAIL(eslEINVAL, errbuf, "cm_CYKInsideAlignHB(): L (%d) is outside ROOT_S's d band (%d..%d)\n", L, cm->cp9b->hdmin[0][jp_0], cm->cp9b->hdmax[0][jp_0]);
8842 Lp_0 = L - hdmin[0][jp_0];
8843
8844 /* Determine the optimal score */
8845 if(preset_mode == TRMODE_J) sc = ins_mx->Jdp[0][jp_0][Lp_0];
8846 if(preset_mode == TRMODE_L) sc = ins_mx->Ldp[0][jp_0][Lp_0];
8847 if(preset_mode == TRMODE_R) sc = ins_mx->Rdp[0][jp_0][Lp_0];
8848 if(preset_mode == TRMODE_T) sc = ins_mx->Tdp[0][jp_0][Lp_0];
8849
8850 /* grow our post matrix, but only if isn't also our out_mx in which
8851 * case we know we're already big enought (also in that case we
8852 * don't want to call GrowTo b/c it can potentially free the DP
8853 * matrix memory and reallocate it, which would be bad b/c we
8854 * need the out_mx!)
8855 */
8856 if(post_mx != out_mx) {
8857 if((status = cm_tr_hb_mx_GrowTo(cm, post_mx, errbuf, cm->cp9b, L, size_limit)) != eslOK) return status;
8858 }
8859
8860 /* If local ends are on, start with the non-banded EL state (cm->M), otherwise it's not a valid deck. */
8861 if(cm->flags & CMH_LOCAL_END) {
8862 if(cp9b->Jvalid[cm->M]) {
8863 for (j = 0; j <= L; j++) {
8864 for (d = 0; d <= j; d++) {
8865 post_mx->Jdp[cm->M][j][d] = ins_mx->Jdp[cm->M][j][d] + out_mx->Jdp[cm->M][j][d] - sc;
8866 }
8867 }
8868 }
8869 if(fill_L && cp9b->Lvalid[cm->M]) {
8870 for (j = 0; j <= L; j++) {
8871 for (d = 0; d <= j; d++) {
8872 post_mx->Ldp[cm->M][j][d] = ins_mx->Ldp[cm->M][j][d] + out_mx->Ldp[cm->M][j][d] - sc;
8873 }
8874 }
8875 }
8876 if(fill_R && cp9b->Rvalid[cm->M]) {
8877 for (j = 0; j <= L; j++) {
8878 for (d = 0; d <= j; d++) {
8879 post_mx->Rdp[cm->M][j][d] = ins_mx->Rdp[cm->M][j][d] + out_mx->Rdp[cm->M][j][d] - sc;
8880 }
8881 }
8882 }
8883 }
8884 /* Fill in the rest of the matrices */
8885 for (v = cm->M-1; v >= 0; v--) {
8886 if(cp9b->Jvalid[v]) {
8887 jx = jmax[v]-jmin[v];
8888 for (jp_v = 0; jp_v <= jx; jp_v++) {
8889 dx = hdmax[v][jp_v]-hdmin[v][jp_v];
8890 for (dp_v = 0; dp_v <= dx; dp_v++) {
8891 post_mx->Jdp[v][jp_v][dp_v] = ins_mx->Jdp[v][jp_v][dp_v] + out_mx->Jdp[v][jp_v][dp_v] - sc;
8892 }
8893 }
8894 }
8895 }
8896 if(fill_L) {
8897 for (v = cm->M-1; v >= 0; v--) {
8898 if(cp9b->Lvalid[v]) {
8899 jx = jmax[v]-jmin[v];
8900 for (jp_v = 0; jp_v <= jx; jp_v++) {
8901 dx = hdmax[v][jp_v]-hdmin[v][jp_v];
8902 for (dp_v = 0; dp_v <= dx; dp_v++) {
8903 post_mx->Ldp[v][jp_v][dp_v] = ins_mx->Ldp[v][jp_v][dp_v] + out_mx->Ldp[v][jp_v][dp_v] - sc;
8904 }
8905 }
8906 }
8907 }
8908 }
8909 if(fill_R) {
8910 for (v = cm->M-1; v >= 0; v--) {
8911 if(cp9b->Rvalid[v]) {
8912 jx = jmax[v]-jmin[v];
8913 for (jp_v = 0; jp_v <= jx; jp_v++) {
8914 dx = hdmax[v][jp_v]-hdmin[v][jp_v];
8915 for (dp_v = 0; dp_v <= dx; dp_v++) {
8916 post_mx->Rdp[v][jp_v][dp_v] = ins_mx->Rdp[v][jp_v][dp_v] + out_mx->Rdp[v][jp_v][dp_v] - sc;
8917 }
8918 }
8919 }
8920 }
8921 }
8922 if(fill_T) {
8923 for (v = cm->M-1; v >= 0; v--) {
8924 if(cp9b->Tvalid[v]) {
8925 jx = jmax[v]-jmin[v];
8926 for (jp_v = 0; jp_v <= jx; jp_v++) {
8927 dx = hdmax[v][jp_v]-hdmin[v][jp_v];
8928 for (dp_v = 0; dp_v <= dx; dp_v++) {
8929 post_mx->Tdp[v][jp_v][dp_v] = ins_mx->Tdp[v][jp_v][dp_v] + out_mx->Tdp[v][jp_v][dp_v] - sc;
8930 }
8931 }
8932 }
8933 }
8934 }
8935 #if eslDEBUGLEVEL >= 2
8936 /* Uncomment to dump matrix to file. Careful...this could be very large. */
8937 /* FILE *fp1; fp1 = fopen("tmp.tru_phbmx", "w"); cm_tr_hb_mx_Dump(fp1, post_mx, preset_mode, TRUE); fclose(fp1); */
8938 #endif
8939 return eslOK;
8940 }
8941
8942 /* Function: cm_TrEmitterPosterior()
8943 * Date: EPN, Fri Oct 7 05:30:31 2011
8944 *
8945 * Purpose: Given a posterior probability cube, where the value in
8946 * post[v][j][d] is the log of the posterior probability of
8947 * a parse subtree rooted at v emitting the subsequence i..j
8948 * (i=j-d+1), fill a CM_EMIT_MX <emit_mx> with
8949 * matrices with values:
8950 *
8951 * emit_mx->*l_pp[v][i]: log of the posterior probability
8952 * that state v emitted residue i leftwise while in * (J or
8953 * L, Joint of Left) marginal mode either at (if a match
8954 * state) or *after* (if an insert state) the left consensus
8955 * position modeled by state v's node.
8956 *
8957 * emit_mx->*r_pp[v][i]: log of the posterior probability
8958 * that state v emitted residue i rightwise while in * (J
8959 * or R, Joint or Right) marginal mode either at (if a match
8960 * state) or *before* (if an insert state) the right
8961 * consensus position modeled by state v's node.
8962 *
8963 * *l_pp[v] is NULL for states that do not emit leftwise
8964 * *r_pp[v] is NULL for states that do not emit rightwise
8965 *
8966 * We only need to fill a subset of the *l_pp and *r_pp
8967 * matrices, depending on the <preset_mode> of the alignment
8968 * which is known and passed in:
8969 * <preset_mode> == TRMODE_J, fill Jl_pp, Jr_pp
8970 * <preset_mode> == TRMODE_L, fill Jl_pp, Jr_pp and Ll_pp
8971 * <preset_mode> == TRMODE_R, fill Jl_pp, Jr_pp and Rr_pp
8972 * <preset_mode> == TRMODE_T, fill Jl_pp, Jr_pp, Ll_pp, and Rr_pp
8973 *
8974 * This is done in 3 steps:
8975 * 1. Fill *l_pp[v][i] and *r_pp[v][i] with the posterior
8976 * probability that state v emitted residue i either
8977 * leftwise (l_pp) or rightwise (r_pp).
8978 *
8979 * 2. Normalize *l_pp and *r_pp so that probability that
8980 * each residue was emitted by any state is exactly
8981 * 1.0.
8982 *
8983 * 3. Combine *l_pp values for MATP_MP (v) and MATP_ML (y=v+1)
8984 * states in the same node so they give the value defined
8985 * above (i.e. *l_pp[v] == *l_pp[y] = the PP that either v
8986 * or y emitted residue i) instead of *l_pp[v] = PP that v
8987 * emitted i, and *l_pp[y] = PP that y emitted i. And
8988 * combine *r_pp values for MATP_MP (v) and MATP_MR (y=v+2)
8989 * states in an analogous way.
8990 *
8991 * If <do_check> we check to make sure the summed probability
8992 * of any residue is > 0.98 and < 1.02 prior the step 2
8993 * normalization, and throw eslFAIL if not.
8994 *
8995 * Note: A failure of this test does not necessarily mean a
8996 * bug in the code, because this check is known to fail for
8997 * some cases with parsetrees that contain inserts of 100s of
8998 * residues from the same IL or IR state (that utilize 100s
8999 * of IL->IL or IR->IR self transitions). These cases were
9000 * looked at in detail to determine if they were due to a bug
9001 * in the DP code. This was logged in
9002 * ~nawrockie/notebook/8_1016_inf-1rc3_bug_alignment/00LOG.
9003 * The conclusion was that the failure of the posterior check
9004 * is due completely to lack of precision in the float scores
9005 * (not just in the logsum look-up table but also with using
9006 * real log() and exp() calls). If this function returns an
9007 * error, please check to see if the parsetree has a large
9008 * insertion in it, if so you can expect probabilities up to
9009 * 1.03 due solely to this precision issue. See the notebook
9010 * 00LOG for more, included a check I performed to change the
9011 * relevant IL->IL transition probability by very small
9012 * values (~0.0001) and you can observe the posteriors change
9013 * dramatically which demonstrates that precision of floats
9014 * is the culprit. (EPN, Sun Oct 26 14:54:31 2008
9015 * (originally added to cm_Posterior() function 'Purpose'
9016 * function which no longer exists, having been replaced by
9017 * this function.)
9018 *
9019 * Args: cm - the model
9020 * errbuf - for error messages
9021 * L - length of the sequence
9022 * size_limit - max number of Mb for DP matrix, if matrix is bigger return eslERANGE
9023 * post - pre-filled posterior cube
9024 * emit_mx - pre-allocated emit matrix, grown and filled-in here
9025 * preset_mode - known (pre-determined) mode of the alignment
9026 * do_check - if TRUE, return eslEFAIL if summed prob of any residue
9027 * (before normalization) is < 0.98 or > 1.02.
9028 *
9029 * Returns: <eslOK> on success.
9030 * Throws: <eslERANGE> if required DP matrix size exceeds <size_limit>
9031 * <eslFAIL> if (do_check) and any residue check fails
9032 * <eslEMEM> if we run out of memory.
9033 * If !eslOK the l_pp and r_pp values are invalid.
9034 */
9035 int
9036 cm_TrEmitterPosterior(CM_t *cm, char *errbuf, int L, float size_limit, char preset_mode, int do_check, CM_TR_MX *post, CM_TR_EMIT_MX *emit_mx)
9037 {
9038 int status;
9039 int v, j, d; /* state, position, subseq length */
9040 int i; /* sequence position */
9041 int sd; /* StateDelta(v) */
9042 int sdl; /* StateLeftDelta(v) */
9043 int sdr; /* StateRightDelta(v) */
9044 int fill_L, fill_R; /* do we need to fill Ll_pp/Rr_pp matrices? */
9045
9046 /* determine which matrices we need to fill in, based on <preset_mode> */
9047 if (preset_mode != TRMODE_J && preset_mode != TRMODE_L && preset_mode != TRMODE_R && preset_mode != TRMODE_T) ESL_FAIL(eslEINVAL, errbuf, "cm_TrEmitterPosterior(): preset_mode is not J, L, R, or T");
9048 if((status = cm_TrFillFromMode(preset_mode, &fill_L, &fill_R, NULL)) != eslOK) ESL_FAIL(status, errbuf, "cm_TrCheckFromPosterior, bogus mode: %d", preset_mode);
9049
9050 /* grow the emit matrices based on the current sequence */
9051 if((status = cm_tr_emit_mx_GrowTo(cm, emit_mx, errbuf, L, size_limit)) != eslOK) return status;
9052
9053 /* initialize all cells of the emit matrices to IMPOSSIBLE */
9054 esl_vec_FSet(emit_mx->Jl_pp_mem, emit_mx->l_ncells_valid, IMPOSSIBLE);
9055 if(fill_L) esl_vec_FSet(emit_mx->Ll_pp_mem, emit_mx->l_ncells_valid, IMPOSSIBLE);
9056 esl_vec_FSet(emit_mx->Jr_pp_mem, emit_mx->r_ncells_valid, IMPOSSIBLE);
9057 if(fill_R) esl_vec_FSet(emit_mx->Rr_pp_mem, emit_mx->r_ncells_valid, IMPOSSIBLE);
9058
9059 /* Step 1. Fill *l_pp[v][i] and *r_pp[v][i] with the posterior
9060 * probability that state v emitted residue i either
9061 * leftwise (*l_pp) or rightwise (*r_pp).
9062 */
9063 for(v = 0; v < cm->M; v++) {
9064 sd = StateDelta(cm->sttype[v]);
9065 sdl = StateLeftDelta(cm->sttype[v]);
9066 sdr = StateRightDelta(cm->sttype[v]);
9067 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) {
9068 for(j = 1; j <= L; j++) {
9069 i = j-sd+1;
9070 for(d = sd; d <= j; d++, i--) {
9071 emit_mx->Jl_pp[v][i] = FLogsum(emit_mx->Jl_pp[v][i], post->Jdp[v][j][d]);
9072 }
9073 if(fill_L) {
9074 i = j-sdl+1; /* careful, use sdl, not sd */
9075 for(d = sdl; d <= j; d++, i--) {
9076 emit_mx->Ll_pp[v][i] = FLogsum(emit_mx->Ll_pp[v][i], post->Ldp[v][j][d]);
9077 }
9078 }
9079 }
9080 }
9081 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) {
9082 for(j = 1; j <= L; j++) {
9083 for(d = sd; d <= j; d++) {
9084 emit_mx->Jr_pp[v][j] = FLogsum(emit_mx->Jr_pp[v][j], post->Jdp[v][j][d]);
9085 }
9086 if(fill_R) {
9087 for(d = sdr; d <= j; d++) { /* careful, use sdr, not sd */
9088 emit_mx->Rr_pp[v][j] = FLogsum(emit_mx->Rr_pp[v][j], post->Rdp[v][j][d]);
9089 }
9090 }
9091 }
9092 }
9093 }
9094 /* factor in contribution of local ends, the EL state may have emitted this residue. */
9095 if (cm->flags & CMH_LOCAL_END) {
9096 for (j = 1; j <= L; j++) {
9097 i = j;
9098 for (d = 1; d <= j; d++, i--) { /* note: d >= 1, b/c EL emits 1 residue */
9099 emit_mx->Jl_pp[cm->M][i] = FLogsum(emit_mx->Jl_pp[cm->M][i], post->Jdp[cm->M][j][d]);
9100 }
9101 }
9102 if(fill_L) {
9103 for (j = 1; j <= L; j++) {
9104 i = j;
9105 for (d = 1; d <= j; d++, i--) { /* note: d >= 1, b/c EL emits 1 residue */
9106 emit_mx->Ll_pp[cm->M][i] = FLogsum(emit_mx->Ll_pp[cm->M][i], post->Ldp[cm->M][j][d]);
9107 }
9108 }
9109 }
9110 if(fill_R) {
9111 for (j = 1; j <= L; j++) {
9112 i = j;
9113 for (d = 1; d <= j; d++, i--) { /* note: d >= 1, b/c EL emits 1 residue */
9114 emit_mx->Rr_pp[cm->M][i] = FLogsum(emit_mx->Rr_pp[cm->M][i], post->Rdp[cm->M][j][d]);
9115 }
9116 }
9117 }
9118 }
9119 #if eslDEBUGLEVEL >= 2
9120 /* Uncomment to dump matrix to file. Careful...this could be very large. */
9121 /* FILE *fp1; fp1 = fopen("tmp.tru_unnorm_emitmx", "w"); cm_tr_emit_mx_Dump(fp1, cm, emit_mx, preset_mode, TRUE); fclose(fp1); */
9122 #endif
9123
9124 /* Step 2. Normalize *l_pp and *r_pp so that probability that
9125 * each residue was emitted by any state is exactly
9126 * 1.0.
9127 */
9128 esl_vec_FSet(emit_mx->sum, (L+1), IMPOSSIBLE);
9129 for(v = 0; v <= cm->M; v++) {
9130 if(emit_mx->Jl_pp[v] != NULL) {
9131 for(i = 1; i <= L; i++) {
9132 emit_mx->sum[i] = FLogsum(emit_mx->sum[i], emit_mx->Jl_pp[v][i]);
9133 }
9134 }
9135 if(emit_mx->Ll_pp[v] != NULL && fill_L) {
9136 for(i = 1; i <= L; i++) {
9137 emit_mx->sum[i] = FLogsum(emit_mx->sum[i], emit_mx->Ll_pp[v][i]);
9138 }
9139 }
9140 if(emit_mx->Jr_pp[v] != NULL) {
9141 for(j = 1; j <= L; j++) {
9142 emit_mx->sum[j] = FLogsum(emit_mx->sum[j], emit_mx->Jr_pp[v][j]);
9143 }
9144 }
9145 if(emit_mx->Rr_pp[v] != NULL && fill_R) {
9146 for(j = 1; j <= L; j++) {
9147 emit_mx->sum[j] = FLogsum(emit_mx->sum[j], emit_mx->Rr_pp[v][j]);
9148 }
9149 }
9150 }
9151 /* perform the check, if nec */
9152 if(do_check) {
9153 for(i = 1; i <= L; i++) {
9154 if((sreEXP2(emit_mx->sum[i]) < 0.98) || (sreEXP2(emit_mx->sum[i]) > 1.02)) {
9155 ESL_FAIL(eslFAIL, errbuf, "residue %d has summed prob of %5.4f (2^%5.4f).\nMay not be a DP coding bug, see 'Note:' on precision in cm_TrEmitterPosterior", i, (sreEXP2(emit_mx->sum[i])), emit_mx->sum[i]);
9156 }
9157 /*printf("i: %d | total: %10.4f\n", i, (sreEXP2(emit_mx->sum[i])));*/
9158 }
9159 ESL_DPRINTF1(("#DEBUG: cm_TrEmitterPosterior() check passed, all residues have summed probability of emission of between 0.98 and 1.02.\n"));
9160 }
9161
9162 /* normalize, using the sum vector */
9163 for(v = 0; v <= cm->M; v++) {
9164 if(emit_mx->Jl_pp[v] != NULL) {
9165 for(i = 1; i <= L; i++) emit_mx->Jl_pp[v][i] -= emit_mx->sum[i];
9166 }
9167 if(emit_mx->Ll_pp[v] != NULL && fill_L) {
9168 for(i = 1; i <= L; i++) emit_mx->Ll_pp[v][i] -= emit_mx->sum[i];
9169 }
9170 if(emit_mx->Jr_pp[v] != NULL) {
9171 for(j = 1; j <= L; j++) emit_mx->Jr_pp[v][j] -= emit_mx->sum[j];
9172 }
9173 if(emit_mx->Rr_pp[v] != NULL && fill_R) {
9174 for(j = 1; j <= L; j++) emit_mx->Rr_pp[v][j] -= emit_mx->sum[j];
9175 }
9176 }
9177
9178 /* Step 3. Combine *l_pp values for MATP_MP (v) and MATP_ML (y=v+1)
9179 * states in the same node so they give the value defined
9180 * above (i.e. *l_pp[v] == *l_pp[y] = the PP that either v or
9181 * y emitted residue i) instead of *l_pp[v] = PP that v
9182 * emitted i, and *l_pp[y] = PP that y emitted i. And
9183 * combine *r_pp values for MATP_MP (v) and MATP_MR (y=v+2)
9184 * states in an analogous way.
9185 */
9186 for(v = 0; v <= cm->M; v++) {
9187 if(cm->sttype[v] == MP_st) {
9188 for(i = 1; i <= L; i++) {
9189 emit_mx->Jl_pp[v][i] = FLogsum(emit_mx->Jl_pp[v][i], emit_mx->Jl_pp[v+1][i]);
9190 emit_mx->Jl_pp[v+1][i] = emit_mx->Jl_pp[v][i];
9191 }
9192 if(fill_L) {
9193 for(i = 1; i <= L; i++) {
9194 emit_mx->Ll_pp[v][i] = FLogsum(emit_mx->Ll_pp[v][i], emit_mx->Ll_pp[v+1][i]);
9195 emit_mx->Ll_pp[v+1][i] = emit_mx->Ll_pp[v][i];
9196 }
9197 }
9198 for(j = 1; j <= L; j++) {
9199 emit_mx->Jr_pp[v][j] = FLogsum(emit_mx->Jr_pp[v][j], emit_mx->Jr_pp[v+2][j]);
9200 emit_mx->Jr_pp[v+2][j] = emit_mx->Jr_pp[v][j];
9201 }
9202 if(fill_R){
9203 for(j = 1; j <= L; j++) {
9204 emit_mx->Rr_pp[v][j] = FLogsum(emit_mx->Rr_pp[v][j], emit_mx->Rr_pp[v+2][j]);
9205 emit_mx->Rr_pp[v+2][j] = emit_mx->Rr_pp[v][j];
9206 }
9207 }
9208 }
9209 }
9210 #if eslDEBUGLEVEL >= 2
9211 /* Uncomment to dump matrix to file. Careful...this could be very large. */
9212 /* FILE *fp2; fp2 = fopen("tmp.tru_emitmx", "w"); cm_tr_emit_mx_Dump(fp2, cm, emit_mx, preset_mode, TRUE); fclose(fp2); */
9213 #endif
9214
9215 return eslOK;
9216 }
9217
9218
9219 /* Function: cm_TrEmitterPosteriorHB()
9220 * Date: EPN, Tue Oct 11 09:36:55 2011
9221 *
9222 * Purpose: Same as cm_TrEmitterPosterior() except HMM banded matrices
9223 * are used. The main difference is that we have to be careful
9224 * to stay within the bands because matrix cells outside
9225 * the bands do not exist (are not allocated). This requires
9226 * keeping careful track of our offsets between the sequence
9227 * position index and the corresponding indices in the matrix.
9228 *
9229 * Args: cm - the model
9230 * errbuf - for error messages
9231 * L - length of the sequence
9232 * size_limit - max number of Mb for DP matrix, if matrix is bigger return eslERANGE
9233 * post - pre-filled posterior cube
9234 * emit_mx - pre-allocated emit matrix, grown and filled-in here
9235 * preset_mode - known optimal mode of the alignment
9236 * do_check - if TRUE, return eslEFAIL if summed prob of any residue
9237 * (before normalization) is < 0.98 or > 1.02.
9238 *
9239 * Returns: <eslOK> on success.
9240 * Throws: <eslERANGE> if required DP matrix size exceeds <size_limit>
9241 * <eslFAIL> if (do_check) and any residue check fails
9242 * <eslEMEM> if we run out of memory.
9243 * If !eslOK the *l_pp and *r_pp values are invalid.
9244 */
9245 int
9246 cm_TrEmitterPosteriorHB(CM_t *cm, char *errbuf, int L, float size_limit, char preset_mode, int do_check, CM_TR_HB_MX *post, CM_TR_HB_EMIT_MX *emit_mx)
9247 {
9248 int status;
9249 int v, j, d; /* state, position, subseq length */
9250 int i; /* sequence position */
9251 int fill_L, fill_R; /* do we need to fill Ll_pp/Rr_pp matrices? */
9252 int jp_v; /* j-jmin[v] for current j, and current v */
9253 int jp_v2; /* another offset j in banded matrix */
9254 int ip_v; /* i-imin[v] for current i, and current v */
9255 int ip_v2; /* another offset i in banded matrix */
9256 int dp_v; /* d-hdmin[v][jp_v] for current j, current v, current d*/
9257 int in, ix; /* temp min/max i */
9258 int jn, jx; /* temp min/max j */
9259
9260 /* ptrs to band info, for convenience */
9261 int *imin = cm->cp9b->imin;
9262 int *imax = cm->cp9b->imax;
9263 int *jmin = cm->cp9b->jmin;
9264 int *jmax = cm->cp9b->jmax;
9265 int **hdmin = cm->cp9b->hdmin;
9266 int **hdmax = cm->cp9b->hdmax;
9267
9268 /* determine which matrices we need to fill in, based on <preset_mode> */
9269 if (preset_mode != TRMODE_J && preset_mode != TRMODE_L && preset_mode != TRMODE_R && preset_mode != TRMODE_T) ESL_FAIL(eslEINVAL, errbuf, "cm_TrEmitterPosteriorHB(): preset_mode is not J, L, R, or T");
9270 if((status = cm_TrFillFromMode(preset_mode, &fill_L, &fill_R, NULL)) != eslOK) ESL_FAIL(status, errbuf, "cm_TrCheckFromPosterior, bogus mode: %d", preset_mode);
9271
9272 /* grow the emit matrices based on the current sequence */
9273 if((status = cm_tr_hb_emit_mx_GrowTo(cm, emit_mx, errbuf, cm->cp9b, L, size_limit)) != eslOK) return status;
9274
9275 /* initialize all cells of the emit matrices to IMPOSSIBLE */
9276 esl_vec_FSet(emit_mx->Jl_pp_mem, emit_mx->l_ncells_valid, IMPOSSIBLE);
9277 if(fill_L) esl_vec_FSet(emit_mx->Ll_pp_mem, emit_mx->l_ncells_valid, IMPOSSIBLE);
9278 esl_vec_FSet(emit_mx->Jr_pp_mem, emit_mx->r_ncells_valid, IMPOSSIBLE);
9279 if(fill_R) esl_vec_FSet(emit_mx->Rr_pp_mem, emit_mx->r_ncells_valid, IMPOSSIBLE);
9280
9281 /* Step 1. Fill *l_pp[v][i] and *r_pp[v][i] with the posterior
9282 * probability that state v emitted residue i either
9283 * leftwise (*l_pp) or rightwise (*r_pp).
9284 */
9285 for(v = 0; v < cm->M; v++) {
9286 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) {
9287 if(cm->cp9b->Jvalid[v]) {
9288 for(j = jmin[v]; j <= jmax[v]; j++) {
9289 jp_v = j - jmin[v];
9290 for(d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) {
9291 dp_v = d-hdmin[v][jp_v];
9292 i = j-d+1;
9293 assert(i >= imin[v] && i <= imax[v]);
9294 ip_v = i - imin[v];
9295 emit_mx->Jl_pp[v][ip_v] = FLogsum(emit_mx->Jl_pp[v][ip_v], post->Jdp[v][jp_v][dp_v]);
9296 }
9297 }
9298 }
9299 if(cm->cp9b->Lvalid[v] && fill_L) {
9300 for(j = jmin[v]; j <= jmax[v]; j++) {
9301 jp_v = j - jmin[v];
9302 for(d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) {
9303 dp_v = d-hdmin[v][jp_v];
9304 i = j-d+1;
9305 assert(i >= imin[v] && i <= imax[v]);
9306 ip_v = i - imin[v];
9307 emit_mx->Ll_pp[v][ip_v] = FLogsum(emit_mx->Ll_pp[v][ip_v], post->Ldp[v][jp_v][dp_v]);
9308 }
9309 }
9310 }
9311 }
9312 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) {
9313 if(cm->cp9b->Jvalid[v]) {
9314 for(j = jmin[v]; j <= jmax[v]; j++) {
9315 jp_v = j - jmin[v];
9316 for(d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) {
9317 dp_v = d-hdmin[v][jp_v];
9318 emit_mx->Jr_pp[v][jp_v] = FLogsum(emit_mx->Jr_pp[v][jp_v], post->Jdp[v][jp_v][dp_v]);
9319 }
9320 }
9321 }
9322 if(cm->cp9b->Rvalid[v] && fill_R) {
9323 for(j = jmin[v]; j <= jmax[v]; j++) {
9324 jp_v = j - jmin[v];
9325 for(d = hdmin[v][jp_v]; d <= hdmax[v][jp_v]; d++) {
9326 dp_v = d-hdmin[v][jp_v];
9327 emit_mx->Rr_pp[v][jp_v] = FLogsum(emit_mx->Rr_pp[v][jp_v], post->Rdp[v][jp_v][dp_v]);
9328 }
9329 }
9330 }
9331 }
9332 }
9333 /* factor in contribution of local ends, the EL state may have emitted this residue. */
9334 /* Remember, the EL deck is non-banded */
9335 if (cm->flags & CMH_LOCAL_END) {
9336 if(cm->cp9b->Jvalid[cm->M]) {
9337 for (j = 1; j <= L; j++) {
9338 i = j;
9339 for (d = 1; d <= j; d++, i--) { /* note: d >= 1, b/c EL emits 1 residue */
9340 emit_mx->Jl_pp[cm->M][i] = FLogsum(emit_mx->Jl_pp[cm->M][i], post->Jdp[cm->M][j][d]);
9341 }
9342 }
9343 }
9344 if(fill_L && cm->cp9b->Lvalid[cm->M]) {
9345 for (j = 1; j <= L; j++) {
9346 i = j;
9347 for (d = 1; d <= j; d++, i--) { /* note: d >= 1, b/c EL emits 1 residue */
9348 emit_mx->Ll_pp[cm->M][i] = FLogsum(emit_mx->Ll_pp[cm->M][i], post->Ldp[cm->M][j][d]);
9349 }
9350 }
9351 }
9352 if(fill_R && cm->cp9b->Rvalid[cm->M]) {
9353 for (j = 1; j <= L; j++) {
9354 i = j;
9355 for (d = 1; d <= j; d++, i--) { /* note: d >= 1, b/c EL emits 1 residue */
9356 emit_mx->Rr_pp[cm->M][i] = FLogsum(emit_mx->Rr_pp[cm->M][i], post->Rdp[cm->M][j][d]);
9357 }
9358 }
9359 }
9360 }
9361 #if eslDEBUGLEVEL >= 2
9362 /* Uncomment to dump matrix to file. Careful...this could be very large. */
9363 /* FILE *fp1; fp1 = fopen("tmp.tru_unnorm_hbemitmx", "w"); cm_tr_hb_emit_mx_Dump(fp1, cm, emit_mx, preset_mode, TRUE); fclose(fp1); */
9364 #endif
9365
9366 /* Step 2. Normalize *l_pp and *r_pp so that probability that
9367 * each residue was emitted by any state is exactly
9368 * 1.0.
9369 */
9370 esl_vec_FSet(emit_mx->sum, (L+1), IMPOSSIBLE);
9371 for(v = 0; v < cm->M; v++) { /* we'll handle EL special */
9372 if(emit_mx->Jl_pp[v] != NULL && cm->cp9b->Jvalid[v]) {
9373 in = ESL_MAX(imin[v], 1);
9374 ix = ESL_MIN(imax[v], L);
9375 for(i = in; i <= ix; i++) {
9376 ip_v = i - imin[v];
9377 emit_mx->sum[i] = FLogsum(emit_mx->sum[i], emit_mx->Jl_pp[v][ip_v]);
9378 }
9379 }
9380 if(emit_mx->Ll_pp[v] != NULL && cm->cp9b->Lvalid[v] && fill_L) {
9381 in = ESL_MAX(imin[v], 1);
9382 ix = ESL_MIN(imax[v], L);
9383 for(i = in; i <= ix; i++) {
9384 ip_v = i - imin[v];
9385 emit_mx->sum[i] = FLogsum(emit_mx->sum[i], emit_mx->Ll_pp[v][ip_v]);
9386 }
9387 }
9388 if(emit_mx->Jr_pp[v] != NULL && cm->cp9b->Jvalid[v]) {
9389 jn = ESL_MAX(jmin[v], 1);
9390 jx = ESL_MIN(jmax[v], L);
9391 for(j = jn; j <= jx; j++) {
9392 jp_v = j - jmin[v];
9393 emit_mx->sum[j] = FLogsum(emit_mx->sum[j], emit_mx->Jr_pp[v][jp_v]);
9394 }
9395 }
9396 if(emit_mx->Rr_pp[v] != NULL && cm->cp9b->Rvalid[v] && fill_R) {
9397 jn = ESL_MAX(jmin[v], 1);
9398 jx = ESL_MIN(jmax[v], L);
9399 for(j = jn; j <= jx; j++) {
9400 jp_v = j - jmin[v];
9401 emit_mx->sum[j] = FLogsum(emit_mx->sum[j], emit_mx->Rr_pp[v][jp_v]);
9402 }
9403 }
9404 }
9405 /* Handle EL deck, remember it is non-banded, and only valid for Jl_pp, Ll_pp and Rr_pp */
9406 if(emit_mx->Jl_pp[cm->M] != NULL && cm->cp9b->Jvalid[cm->M]) {
9407 for(i = 1; i <= L; i++) {
9408 emit_mx->sum[i] = FLogsum(emit_mx->sum[i], emit_mx->Jl_pp[cm->M][i]);
9409 }
9410 }
9411 if(emit_mx->Ll_pp[cm->M] != NULL && cm->cp9b->Lvalid[cm->M] && fill_L) {
9412 for(i = 1; i <= L; i++) {
9413 emit_mx->sum[i] = FLogsum(emit_mx->sum[i], emit_mx->Ll_pp[cm->M][i]);
9414 }
9415 }
9416 if(emit_mx->Rr_pp[cm->M] != NULL && cm->cp9b->Rvalid[cm->M] && fill_R) {
9417 for(i = 1; i <= L; i++) {
9418 emit_mx->sum[i] = FLogsum(emit_mx->sum[i], emit_mx->Rr_pp[cm->M][i]);
9419 }
9420 }
9421
9422 /* perform the check, if nec */
9423 if(do_check) {
9424 for(i = 1; i <= L; i++) {
9425 if((sreEXP2(emit_mx->sum[i]) < 0.98) || (sreEXP2(emit_mx->sum[i]) > 1.02)) {
9426 ESL_FAIL(eslFAIL, errbuf, "residue %d has summed prob of %5.4f (2^%5.4f).\nMay not be a DP coding bug, see 'Note:' on precision in cm_TrEmitterPosterior().\n", i, (sreEXP2(emit_mx->sum[i])), emit_mx->sum[i]);
9427 }
9428 /*printf("i: %d | total: %10.4f\n", i, (sreEXP2(emit_mx->sum[i])));*/
9429 }
9430 ESL_DPRINTF1(("#DEBUG: cm_TrEmitterPosteriorHB() check passed, all residues have summed probability of emission of between 0.98 and 1.02.\n"));
9431 }
9432
9433 /* normalize, using the sum vector */
9434 for(v = 0; v < cm->M; v++) {
9435 if(emit_mx->Jl_pp[v] != NULL && cm->cp9b->Jvalid[v]) {
9436 in = ESL_MAX(imin[v], 1);
9437 ix = ESL_MIN(imax[v], L);
9438 for(i = in; i <= ix; i++) {
9439 ip_v = i - imin[v];
9440 emit_mx->Jl_pp[v][ip_v] -= emit_mx->sum[i];
9441 }
9442 }
9443 if(emit_mx->Ll_pp[v] != NULL && cm->cp9b->Lvalid[v] && fill_L) {
9444 in = ESL_MAX(imin[v], 1);
9445 ix = ESL_MIN(imax[v], L);
9446 for(i = in; i <= ix; i++) {
9447 ip_v = i - imin[v];
9448 emit_mx->Ll_pp[v][ip_v] -= emit_mx->sum[i];
9449 }
9450 }
9451 if(emit_mx->Jr_pp[v] != NULL && cm->cp9b->Jvalid[v]) {
9452 jn = ESL_MAX(jmin[v], 1);
9453 jx = ESL_MIN(jmax[v], L);
9454 for(j = jn; j <= jx; j++) {
9455 jp_v = j - jmin[v];
9456 emit_mx->Jr_pp[v][jp_v] -= emit_mx->sum[j];
9457 }
9458 }
9459 if(emit_mx->Rr_pp[v] != NULL && cm->cp9b->Rvalid[v] && fill_R) {
9460 jn = ESL_MAX(jmin[v], 1);
9461 jx = ESL_MIN(jmax[v], L);
9462 for(j = jn; j <= jx; j++) {
9463 jp_v = j - jmin[v];
9464 emit_mx->Rr_pp[v][jp_v] -= emit_mx->sum[j];
9465 }
9466 }
9467 }
9468 /* Handle EL deck, remember it is non-banded */
9469 if(emit_mx->Jl_pp[cm->M] != NULL && cm->cp9b->Jvalid[cm->M]) {
9470 for(i = 1; i <= L; i++) {
9471 emit_mx->Jl_pp[cm->M][i] -= emit_mx->sum[i];
9472 }
9473 }
9474 if(emit_mx->Ll_pp[cm->M] != NULL && cm->cp9b->Lvalid[cm->M] && fill_L) {
9475 for(i = 1; i <= L; i++) {
9476 emit_mx->Ll_pp[cm->M][i] -= emit_mx->sum[i];
9477 }
9478 }
9479 if(emit_mx->Rr_pp[cm->M] != NULL && cm->cp9b->Rvalid[cm->M] && fill_R) {
9480 for(i = 1; i <= L; i++) {
9481 emit_mx->Rr_pp[cm->M][i] -= emit_mx->sum[i];
9482 }
9483 }
9484 /* Step 3. Combine *l_pp values for MATP_MP (v) and MATP_ML (y=v+1)
9485 * states in the same node so they give the value defined
9486 * above (i.e. *l_pp[v] == *l_pp[y] = the PP that either v or
9487 * y emitted residue i) instead of *l_pp[v] = PP that v
9488 * emitted i, and *l_pp[y] = PP that y emitted i. And
9489 * combine *r_pp values for MATP_MP (v) and MATP_MR (y=v+2)
9490 * states in an analogous way.
9491 */
9492 for(v = 0; v <= cm->M; v++) {
9493 if(cm->sttype[v] == MP_st) {
9494 /* we only change {J,L}l_pp[v][i] and {J,L}l_pp[v+1][i] if i is within both
9495 * state v and v+1's i band.
9496 */
9497 if(cm->cp9b->Jvalid[v]) {
9498 if(imax[v] >= 1 && imax[v+1] >= 1) {
9499 in = ESL_MAX(imin[v], imin[v+1]);
9500 ix = ESL_MIN(imax[v], imax[v+1]);
9501 for(i = in; i <= ix; i++) {
9502 ip_v = i - imin[v];
9503 ip_v2 = i - imin[v+1];
9504 emit_mx->Jl_pp[v][ip_v] = FLogsum(emit_mx->Jl_pp[v][ip_v], emit_mx->Jl_pp[v+1][ip_v2]);
9505 emit_mx->Jl_pp[v+1][ip_v2] = emit_mx->Jl_pp[v][ip_v];
9506 }
9507 }
9508 }
9509 if(cm->cp9b->Lvalid[v] && fill_L) {
9510 if(imax[v] >= 1 && imax[v+1] >= 1) {
9511 in = ESL_MAX(imin[v], imin[v+1]);
9512 ix = ESL_MIN(imax[v], imax[v+1]);
9513 for(i = in; i <= ix; i++) {
9514 ip_v = i - imin[v];
9515 ip_v2 = i - imin[v+1];
9516 emit_mx->Ll_pp[v][ip_v] = FLogsum(emit_mx->Ll_pp[v][ip_v], emit_mx->Ll_pp[v+1][ip_v2]);
9517 emit_mx->Ll_pp[v+1][ip_v2] = emit_mx->Ll_pp[v][ip_v];
9518 }
9519 }
9520 }
9521 /* we only change {J,R}r_pp[v][j] and {J,R}r_pp[v+2][j] if j is within both
9522 * state v and v+2's j band.
9523 */
9524 if(cm->cp9b->Jvalid[v]) {
9525 if(jmax[v] >= 1 && jmax[v+2] >= 1) {
9526 jn = ESL_MAX(jmin[v], jmin[v+2]);
9527 jx = ESL_MIN(jmax[v], jmax[v+2]);
9528 for(j = jn; j <= jx; j++) {
9529 jp_v = j - jmin[v];
9530 jp_v2 = j - jmin[v+2];
9531 emit_mx->Jr_pp[v][jp_v] = FLogsum(emit_mx->Jr_pp[v][jp_v], emit_mx->Jr_pp[v+2][jp_v2]);
9532 emit_mx->Jr_pp[v+2][jp_v2] = emit_mx->Jr_pp[v][jp_v];
9533 }
9534 }
9535 }
9536 if(cm->cp9b->Rvalid[v] && fill_R) {
9537 if(jmax[v] >= 1 && jmax[v+2] >= 1) {
9538 jn = ESL_MAX(jmin[v], jmin[v+2]);
9539 jx = ESL_MIN(jmax[v], jmax[v+2]);
9540 for(j = jn; j <= jx; j++) {
9541 jp_v = j - jmin[v];
9542 jp_v2 = j - jmin[v+2];
9543 emit_mx->Rr_pp[v][jp_v] = FLogsum(emit_mx->Rr_pp[v][jp_v], emit_mx->Rr_pp[v+2][jp_v2]);
9544 emit_mx->Rr_pp[v+2][jp_v2] = emit_mx->Rr_pp[v][jp_v];
9545 }
9546 }
9547 }
9548 }
9549 }
9550 #if eslDEBUGLEVEL >= 2
9551 /* Uncomment to dump matrix to file. Careful...this could be very large. */
9552 /* FILE *fp2; fp2 = fopen("tmp.tru_hbemitmx", "w"); cm_tr_hb_emit_mx_Dump(fp2, cm, emit_mx, preset_mode, TRUE); fclose(fp2); */
9553 #endif
9554
9555 return eslOK;
9556 }
9557
9558 /* Function: cm_TrPostCode()
9559 * Date: EPN, Fri Oct 7 14:30:32 2011
9560 *
9561 * Purpose: Given a parse tree and a filled emit matrix calculate two
9562 * strings that represents the confidence values on each
9563 * aligned residue in the sequence.
9564 *
9565 * The emit_mx values are:
9566 * {J,L}l_pp[v][i]: log of the posterior probability that state v emitted
9567 * residue i leftwise either at (if a match state) or
9568 * *after* (if an insert state) the left consensus
9569 * position modeled by state v's node in Joint marginal
9570 * mode (for Jl_pp) or Left marginal mode (for Ll_pp).
9571 *
9572 * {J,R}r_pp[v][i]: log of the posterior probability that state v emitted
9573 * residue i rightwise either at (if a match state) or
9574 * *before* (if an insert state) the right consensus
9575 * position modeled by state v's node in Joint marginal
9576 * mode (for Jr_pp) or Right marginal mode (for Rr_pp).
9577 *
9578 * {J,L}l_pp[v] is NULL for states that do not emit leftwise (B,S,D,E,IR,MR)
9579 * {J,R}r_pp[v] is NULL for states that do not emit rightwise (B,S,D,E,IL,ML)
9580 *
9581 * The PP string is 0..L-1 (L = len of target seq),
9582 * so its in the coordinate system of the sequence string;
9583 * off by one from dsq.
9584 *
9585 * Values are 0,1,2,3,4,5,6,7,8,9,*:
9586 * '0' = [0.00-0.05)
9587 * '1' = [0.05-0.15)
9588 * '2' = [0.15-0.25)
9589 * '3' = [0.25-0.35)
9590 * '4' = [0.35-0.45)
9591 * '5' = [0.45-0.55)
9592 * '6' = [0.55-0.65)
9593 * '7' = [0.65-0.75)
9594 * '8' = [0.75-0.85)
9595 * '9' = [0.85-0.95)
9596 * '*' = [0.95-1.00)
9597 *
9598 * cm_TrPostCodeHB() is nearly the same function with the
9599 * difference that HMM bands were used for the alignment,
9600 * so we have to deal with offset issues.
9601 *
9602 * Renamed from CMPostCode() [EPN, Wed Sep 14 06:20:35 2011].
9603 *
9604 * Args: cm - the model
9605 * errbuf - char buffer for reporting errors
9606 * dsq - the digitized sequence [1..L]
9607 * L - length of the dsq to align
9608 * emit_mx - the pre-filled emit matrix
9609 * tr - the parstree with the emissions we're setting PPs for
9610 * ret_ppstr - RETURN: a string of the PP code values (0..L-1)
9611 * ret_avgp - RETURN: the average PP of all aligned residues
9612 *
9613 * Returns: <eslOK> on success.
9614 * Throws: <eslEINVAL> if a posterior probability is > 1.01 or less than -0.01.
9615 * or if we get a marginal mode in the parsetree that doesn't
9616 * make sense.
9617 */
9618 int
9619 cm_TrPostCode(CM_t *cm, char *errbuf, int L, CM_TR_EMIT_MX *emit_mx, Parsetree_t *tr, char **ret_ppstr, float *ret_avgp)
9620 {
9621 int status;
9622 int x, v, i, j, r; /* counters */
9623 char *ppstr; /* the PP string, created here */
9624 float p; /* a probability */
9625 float sum_logp; /* log of summed probability of all residues emitted thus far */
9626 float cur_log_pp; /* current log probability of emitting a residue */
9627 char mode; /* marginal mode: TRMODE_J, TRMODE_L or TRMODE_R */
9628 int have_el; /* TRUE if CM has local ends, otherwise FALSE */
9629
9630 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
9631
9632 ESL_ALLOC(ppstr, (L+1) * sizeof(char));
9633 sum_logp = IMPOSSIBLE;
9634
9635 /* go through each node of the parsetree and determine post code for emissions */
9636 for (x = 0; x < tr->n; x++) {
9637 v = tr->state[x];
9638 i = tr->emitl[x];
9639 j = tr->emitr[x];
9640 mode = tr->mode[x];
9641
9642 /* Only P, L, R, and EL states have emissions. */
9643 if(cm->sttype[v] == EL_st) { /* EL state, we have to handle this guy special */
9644 if(mode == TRMODE_J || mode == TRMODE_L || mode == TRMODE_R) {
9645 for(r = i; r <= j; r++) { /* we have to annotate from residues i..j */
9646 if(! have_el) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCode() using EL state to emit residue %d, but ELs are turned off!\n", r);
9647 switch (mode) {
9648 case TRMODE_J: cur_log_pp = emit_mx->Jl_pp[v][r]; break;
9649 case TRMODE_L: cur_log_pp = emit_mx->Ll_pp[v][r]; break;
9650 case TRMODE_R: cur_log_pp = emit_mx->Rr_pp[v][r]; break;
9651 }
9652 ppstr[r-1] = Fscore2postcode(cur_log_pp);
9653 sum_logp = FLogsum(sum_logp, cur_log_pp);
9654 /* make sure we've got a valid probability */
9655 p = FScore2Prob(cur_log_pp, 1.);
9656 if(p > 1.01) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCode(): probability for EL state v: %d residue r: %d > 1.00 (%.2f)", v, r, p);
9657 if(p < -0.01) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCode(): probability for EL state v: %d residue r: %d < 0.00 (%.2f)", v, r, p);
9658 }
9659 }
9660 else ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCode(): invalid mode for EL state in the parsetree: %d\n", mode);
9661 }
9662 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) {
9663 if(mode == TRMODE_J || mode == TRMODE_L) {
9664 cur_log_pp = (mode == TRMODE_J) ? emit_mx->Jl_pp[v][i] : emit_mx->Ll_pp[v][i];
9665 ppstr[i-1] = Fscore2postcode(cur_log_pp);
9666 sum_logp = FLogsum(sum_logp, cur_log_pp);
9667 /* make sure we've got a valid probability */
9668 p = FScore2Prob(cur_log_pp, 1.);
9669 if(p > 1.01) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCode(): probability for left state v: %d residue i: %d > 1.00 (%.2f)", v, i, p);
9670 if(p < -0.01) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCode(): probability for left state v: %d residue i: %d < 0.00 (%.2f)", v, i, p);
9671 }
9672 else if(mode != TRMODE_R) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCode(): non-sensical mode for MP, ML, IL state: %d", mode);
9673 }
9674 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) {
9675 if(mode == TRMODE_J || mode == TRMODE_R) {
9676 cur_log_pp = (mode == TRMODE_J) ? emit_mx->Jr_pp[v][j] : emit_mx->Rr_pp[v][j];
9677 ppstr[j-1] = Fscore2postcode(cur_log_pp);
9678 sum_logp = FLogsum(sum_logp, cur_log_pp);
9679 /* make sure we've got a valid probability */
9680 p = FScore2Prob(cur_log_pp, 1.);
9681 if(p > 1.01) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCode(): probability for right state v: %d residue i: %d > 1.00 (%.2f)", v, j, p);
9682 if(p < -0.01) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCode(): probability for right state v: %d residue i: %d < 0.00 (%.2f)", v, j, p);
9683 }
9684 else if(mode != TRMODE_L) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCode(): non-sensical mode for MP, MR, IR state: %d", mode);
9685 }
9686 }
9687 ppstr[L] = '\0';
9688
9689 if(ret_ppstr != NULL) *ret_ppstr = ppstr; else free(ppstr);
9690 if(ret_avgp != NULL) *ret_avgp = sreEXP2(sum_logp) / (float) L;
9691 return eslOK;
9692
9693 ERROR:
9694 ESL_FAIL(eslEMEM, errbuf, "cm_TrPostcode(): Memory allocation error.");
9695 return status; /* never reached */
9696 }
9697
9698 /* Function: cm_TrPostCodeHB()
9699 * Date: EPN, Tue Oct 11 09:58:47 2011
9700 *
9701 * Purpose: Same as cm_TrPostCode() except HMM banded matrices are
9702 * used. The main difference is that we have to be careful to
9703 * stay within the bands because matrix cells outside the
9704 * bands do not exist (are not allocated). This requires
9705 * keeping careful track of our offsets between the sequence
9706 * position index and the corresponding indices in the
9707 * matrix.
9708 *
9709 * Args: cm - the model
9710 * errbuf - char buffer for reporting errors
9711 * dsq - the digitized sequence [1..L]
9712 * L - length of the dsq to align
9713 * emit_mx - the pre-filled emit matrix
9714 * tr - the parstree with the emissions we're setting PPs for
9715 * ret_ppstr - RETURN: a string of the PP code values (0..L-1)
9716 * ret_avgp - RETURN: the average PP of all aligned residues
9717 *
9718 * Returns: <eslOK> on success.
9719 * Throws: <eslEINVAL> if a posterior probability is > 1.01 or less than -0.01.
9720 * or if we get a marginal mode in the parsetree that doesn't
9721 * make sense.
9722 */
9723 int
9724 cm_TrPostCodeHB(CM_t *cm, char *errbuf, int L, CM_TR_HB_EMIT_MX *emit_mx, Parsetree_t *tr, char **ret_ppstr, float *ret_avgp)
9725 {
9726 int status;
9727 int x, v, i, j, r; /* counters */
9728 char *ppstr; /* the PP string, created here */
9729 float p; /* a probability */
9730 float sum_logp; /* log of summed probability of all residues emitted thus far */
9731 float cur_log_pp; /* current log probability of emitting a residue */
9732 char mode; /* marginal mode: TRMODE_J, TRMODE_L or TRMODE_R */
9733 int have_el; /* TRUE if CM has local ends, otherwise FALSE */
9734
9735 /* variables used for HMM bands */
9736 int ip_v, jp_v; /* i, j offset within bands */
9737 /* ptrs to cp9b info, for convenience */
9738 CP9Bands_t *cp9b = cm->cp9b;
9739 int *imin = cp9b->imin;
9740 int *imax = cp9b->imax;
9741 int *jmin = cp9b->jmin;
9742 int *jmax = cp9b->jmax;
9743
9744 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
9745
9746 ESL_ALLOC(ppstr, (L+1) * sizeof(char));
9747 sum_logp = IMPOSSIBLE;
9748
9749 /* go through each node of the parsetree and determine post code for emissions */
9750 for (x = 0; x < tr->n; x++) {
9751 v = tr->state[x];
9752 i = tr->emitl[x];
9753 j = tr->emitr[x];
9754 mode = tr->mode[x];
9755
9756 /* Only P, L, R, and EL states have emissions. */
9757 if(cm->sttype[v] == EL_st) { /* EL state, we have to handle this guy special */
9758 /* Note it is possible to use an EL state in an HMM banded
9759 * truncated optimal accuracy parsetree if d == 0 (no EL
9760 * emissions) even when local ends are off! (This is also true
9761 * in non-truncated HMM banded OA, see the note in
9762 * cm_dpalign.c:cm_alignT_hb() regarding the special case
9763 * involving allow_S_local_end'.) This means we don't fail
9764 * if local ends are off and we see EL unless we see that we've
9765 * emitted >= 1 residues from EL.
9766 */
9767 if(mode == TRMODE_J || mode == TRMODE_L || mode == TRMODE_R) {
9768 for(r = i; r <= j; r++) { /* we have to annotate from residues i..j */
9769 if(! have_el) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCodeHB() using EL state to emit residue %d, but ELs are turned off!\n", r);
9770 /* remember the EL deck is non-banded */
9771 switch (mode) {
9772 case TRMODE_J: cur_log_pp = emit_mx->Jl_pp[v][r]; break;
9773 case TRMODE_L: cur_log_pp = emit_mx->Ll_pp[v][r]; break;
9774 case TRMODE_R: cur_log_pp = emit_mx->Rr_pp[v][r]; break;
9775 }
9776 ppstr[r-1] = Fscore2postcode(cur_log_pp);
9777 sum_logp = FLogsum(sum_logp, cur_log_pp);
9778 /* make sure we've got a valid probability */
9779 p = FScore2Prob(cur_log_pp, 1.);
9780 if(p > 1.01) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCodeHB(): probability for EL state v: %d residue r: %d > 1.00 (%.2f)", v, r, p);
9781 if(p < -0.01) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCodeHB(): probability for EL state v: %d residue r: %d < 0.00 (%.2f)", v, r, p);
9782 }
9783 }
9784 else ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCodeHB(): invalid mode for EL state in the parsetree: %d\n", mode);
9785 }
9786 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) {
9787 if(mode == TRMODE_J || mode == TRMODE_L) {
9788 ip_v = i - imin[v];
9789 assert(i >= imin[v] && i <= imax[v]);
9790 ESL_DASSERT1((i >= imin[v] && i <= imax[v]));
9791 cur_log_pp = (mode == TRMODE_J) ? emit_mx->Jl_pp[v][ip_v] : emit_mx->Ll_pp[v][ip_v];
9792 ppstr[i-1] = Fscore2postcode(cur_log_pp);
9793 sum_logp = FLogsum(sum_logp, cur_log_pp);
9794 /* make sure we've got a valid probability */
9795 p = FScore2Prob(cur_log_pp, 1.);
9796 if(p > 1.01) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCodeHB(): probability for left state v: %d residue i: %d > 1.00 (%.2f)", v, i, p);
9797 if(p < -0.01) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCodeHB(): probability for left state v: %d residue i: %d < 0.00 (%.2f)", v, i, p);
9798 }
9799 else if(mode != TRMODE_R) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCodeHB(): non-sensical mode for MP, ML, IL state: %d", mode);
9800 }
9801 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) {
9802 if(mode == TRMODE_J || mode == TRMODE_R) {
9803 jp_v = j - jmin[v];
9804 assert(j >= jmin[v] && j <= jmax[v]);
9805 ESL_DASSERT1((j >= jmin[v] && j <= jmax[v]));
9806 cur_log_pp = (mode == TRMODE_J) ? emit_mx->Jr_pp[v][jp_v] : emit_mx->Rr_pp[v][jp_v];
9807 ppstr[j-1] = Fscore2postcode(cur_log_pp);
9808 sum_logp = FLogsum(sum_logp, cur_log_pp);
9809 /* make sure we've got a valid probability */
9810 p = FScore2Prob(cur_log_pp, 1.);
9811 if(p > 1.01) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCodeHB(): probability for right state v: %d residue i: %d > 1.00 (%.2f)", v, j, p);
9812 if(p < -0.01) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCodeHB(): probability for right state v: %d residue i: %d < 0.00 (%.2f)", v, j, p);
9813 }
9814 else if(mode != TRMODE_L) ESL_FAIL(eslEINVAL, errbuf, "cm_TrPostCode(): non-sensical mode for MP, MR, IR state: %d", mode);
9815 }
9816 }
9817 ppstr[L] = '\0';
9818
9819 /*printf("cm_TrPostCodeHB() return avgpp: %f\n", sreEXP2(sum_logp) / (float) L);*/
9820 ESL_DPRINTF1(("#DEBUG: cm_TrPostCodeHB() return avgpp: %f\n", sreEXP2(sum_logp) / (float) L));
9821
9822 if(ret_ppstr != NULL) *ret_ppstr = ppstr; else free(ppstr);
9823 if(ret_avgp != NULL) *ret_avgp = sreEXP2(sum_logp) / (float) L;
9824 return eslOK;
9825
9826 ERROR:
9827 ESL_FAIL(eslEMEM, errbuf, "cm_TrPostcodeHB(): Memory allocation error.");
9828 return status; /* never reached */
9829 }
9830
9831
9832 /* Function: cm_TrFillFromMode()
9833 * Date: EPN, Wed Sep 28 05:29:19 2011
9834 *
9835 * Purpose: Given an optimal marginal alignment mode
9836 * (could be TRMODE_UNKNOWN), determine which
9837 * of the marginal matrices we need to fill
9838 * in to find the alignment in that mode.
9839 *
9840 * If mode == TRMODE_J: fill J matrix only
9841 * If mode == TRMODE_L: fill J and L matrices only
9842 * If mode == TRMODE_R: fill J and R matrices only
9843 * If mode == TRMODE_T: fill J, L, R, and T matrices
9844 * If mode == TRMODE_UNKNOWN: fill J, L, R, and T matrices
9845 *
9846 * Return TRUE/FALSE values in <ret_fill_{L,R,T}>.
9847 * Note that we always must fill in J matrices so a fill_J
9848 * value is unnecessary, it's implicitly true.
9849 *
9850 * Args: mode - optimal mode
9851 * ret_fill_L - RETURN: should we fill in L based on <ret_mode>?
9852 * ret_fill_R - RETURN: should we fill in R based on <ret_mode>?
9853 * ret_fill_T - RETURN: should we fill in T based on <ret_mode>?
9854 *
9855 * Throws: eslEINVAL if mode is not TRMODE_J, TRMODE_L, TRMODE_R, TRMODE_T nor TRMODE_UNKNOWN.
9856 */
9857 int
9858 cm_TrFillFromMode(char mode, int *ret_fill_L, int *ret_fill_R, int *ret_fill_T)
9859 {
9860 int fill_L, fill_R, fill_T;
9861 int invalid_mode = FALSE;
9862
9863 fill_L = fill_R = fill_T = FALSE;
9864 switch(mode) {
9865 case TRMODE_J:
9866 break;
9867 case TRMODE_L:
9868 fill_L = TRUE;
9869 break;
9870 case TRMODE_R:
9871 fill_R = TRUE;
9872 break;
9873 case TRMODE_T:
9874 case TRMODE_UNKNOWN:
9875 fill_L = fill_R = fill_T = TRUE;
9876 break;
9877 default:
9878 invalid_mode = TRUE;
9879 break;
9880 }
9881
9882 if(ret_fill_L != NULL) *ret_fill_L = fill_L;
9883 if(ret_fill_R != NULL) *ret_fill_R = fill_R;
9884 if(ret_fill_T != NULL) *ret_fill_T = fill_T;
9885
9886 if(invalid_mode) return eslEINVAL;
9887 return eslOK;
9888 }
9889
9890 /*****************************************************************
9891 * Benchmark driver
9892 *****************************************************************/
9893 #ifdef IMPL_TRUNC_ALIGN_BENCHMARK
9894 /* Next line is optimized (debugging on) on MacBook Pro:
9895 * gcc -o benchmark-trunc-align -std=gnu99 -g -Wall -I. -L. -I../hmmer/src -L../hmmer/src -I../easel -L../easel -DIMPL_TRUNC_ALIGN_BENCHMARK cm_dpalign_trunc.c -linfernal -lhmmer -leasel -lm
9896 * Next line is optimized (debugging not on) on wyvern:
9897 * gcc -o benchmark-trunc-align -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_ALIGN_BENCHMARK cm_dpalign_trunc.c -linfernal -lhmmer -leasel -lm
9898 * ./benchmark-trunc-align <cmfile>
9899 */
9900
9901 #include <esl_config.h>
9902 #include <p7_config.h>
9903 #include "config.h"
9904
9905 #include <stdio.h>
9906 #include <stdlib.h>
9907 #include <string.h>
9908 #include <time.h>
9909
9910 #include "easel.h"
9911 #include <esl_getopts.h>
9912 #include <esl_histogram.h>
9913 #include <esl_sqio.h>
9914 #include <esl_stats.h>
9915 #include <esl_stopwatch.h>
9916 #include <esl_vectorops.h>
9917 #include <esl_wuss.h>
9918
9919 #include "hmmer.h"
9920
9921 #include "infernal.h"
9922
9923 static ESL_OPTIONS options[] = {
9924 /* name type default env range toggles reqs incomp help docgroup*/
9925 { "-h", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
9926 { "-l", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "configure CM/HMM for local alignment", 0 },
9927 { "--cykout", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL,"--optacc", "run TrCYKOutside, to make sure it agrees with TrCYKInside", 0 },
9928 { "--std", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "also do standard (non-truncated) alignments", 0},
9929 { "--orig", eslARG_NONE, FALSE, NULL, NULL, NULL,"--search", NULL, "also do search with original trCYK", 0},
9930 { "--hb", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "also do HMM banded alignments", 0},
9931 { "--failok", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "allow failures of Inside vs Outside checks", 0},
9932 { "--search", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "also run search algorithms", 0},
9933 { "--noqdb", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "don't use QDBs", 0},
9934 { "--sums", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "use posterior sums during HMM band calculation (widens bands)", 0},
9935 { "--onlyhb", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "only run HMM banded scanning trCYK", 0},
9936 { "--optacc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "run optimal accuracy alignment instead of CYK", 0},
9937 { "--compacc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "run optimal accuracy and CYK and ensure OA avg pp >= CYK avg pp", 0},
9938 { "--tau", eslARG_REAL, "5e-6",NULL, "0<x<1",NULL, NULL, NULL, "set tail loss prob for HMM bands to <x>", 0},
9939 { "--cp9noel", eslARG_NONE, FALSE, NULL, NULL, NULL, "-l", NULL, "turn OFF local ends in cp9 HMMs", 0},
9940 { "--cp9gloc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--cp9noel", "configure CP9 HMM in glocal mode", 0},
9941 { "--thresh1", eslARG_REAL, "0.01", NULL, NULL, NULL, NULL, NULL, "set HMM bands thresh1 to <x>", 0},
9942 { "--thresh2", eslARG_REAL, "0.99", NULL, NULL, NULL, NULL, NULL, "set HMM bands thresh2 to <x>", 0},
9943 { "--mxsize", eslARG_REAL, "128.", NULL, "x>0", NULL, NULL, NULL, "set maximum allowed size of HB matrices to <x> Mb", 0},
9944 { "--tr", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "dump parsetrees to stdout", 0},
9945 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
9946 };
9947 static char usage[] = "[-options] <cmfile> <seqfile>";
9948 static char banner[] = "benchmark driver for truncated alignment implementations";
9949
9950 int
9951 main(int argc, char **argv)
9952 {
9953 int status;
9954 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 2, argc, argv, banner, usage);
9955 CM_t *cm;
9956 ESL_STOPWATCH *w = esl_stopwatch_Create();
9957 ESL_ALPHABET *abc = NULL;
9958 ESL_DSQ *dsq;
9959 int i;
9960 float sc, sc_oa, sc_cyk;
9961 float pp_oa, pp_cyk;
9962 char *cmfile = esl_opt_GetArg(go, 1);
9963 char *seqfile = esl_opt_GetArg(go, 2);
9964 CM_FILE *cmfp; /* open input CM file stream */
9965 int L; /* length of sequence */
9966 char errbuf[eslERRBUFSIZE];
9967 int pass_idx = PLI_PASS_5P_AND_3P_ANY; /* this only affects the truncation penalty, not really impt */
9968 ESL_SQFILE *sqfp = NULL; /* open sequence input file stream */
9969 ESL_SQ *sq = NULL; /* a sequence */
9970 Parsetree_t *tr = NULL;
9971 float size_limit = esl_opt_GetReal(go, "--mxsize");
9972 float save_tau, save_cp9b_thresh1, save_cp9b_thresh2;
9973 float hbmx_Mb, trhbmx_Mb;
9974 float parsetree_sc, parsetree_struct_sc;
9975 char mode;
9976 int qdbidx;
9977 int do_optacc = ( esl_opt_GetBoolean(go, "--optacc") || esl_opt_GetBoolean(go, "--compacc")) ? TRUE : FALSE;
9978 int do_cyk = ((! esl_opt_GetBoolean(go, "--optacc")) || esl_opt_GetBoolean(go, "--compacc")) ? TRUE : FALSE;
9979 int do_compacc = esl_opt_GetBoolean(go, "--compacc") ? TRUE : FALSE;
9980 char *ppstr = NULL; /* just so cm_*Align*() will return avg PP for CYK */
9981
9982 /* open CM file */
9983 if ((status = cm_file_Open(cmfile, NULL, FALSE, &(cmfp), errbuf)) != eslOK) cm_Fail(errbuf);
9984 if ((status = cm_file_Read(cmfp, TRUE, &abc, &cm)) != eslOK) cm_Fail(cmfp->errbuf);
9985 cm_file_Close(cmfp);
9986
9987 /* open the sequence file */
9988 status = esl_sqfile_OpenDigital(cm->abc, seqfile, eslSQFILE_UNKNOWN, NULL, &sqfp);
9989 if (status == eslENOTFOUND) esl_fatal("File %s doesn't exist or is not readable\n", seqfile);
9990 else if (status == eslEFORMAT) esl_fatal("Couldn't determine format of sequence file %s\n", seqfile);
9991 else if (status == eslEINVAL) esl_fatal("Can't autodetect stdin or .gz.");
9992 else if (status != eslOK) esl_fatal("Sequence file open failed with error %d.\n", status);
9993
9994 cm->config_opts |= CM_CONFIG_TRUNC;
9995 cm->align_opts |= CM_ALIGN_HBANDED;
9996 if(esl_opt_GetBoolean(go, "--sums")) cm->align_opts |= CM_ALIGN_SUMS;
9997
9998 if(esl_opt_GetBoolean(go, "-l")) {
9999 cm->config_opts |= CM_CONFIG_LOCAL;
10000 if(! esl_opt_GetBoolean(go, "--cp9gloc")) {
10001 cm->config_opts |= CM_CONFIG_HMMLOCAL;
10002 if(! esl_opt_GetBoolean(go, "--cp9noel")) cm->config_opts |= CM_CONFIG_HMMEL;
10003 }
10004 }
10005 if(esl_opt_GetBoolean(go, "--search")) {
10006 cm->config_opts |= CM_CONFIG_SCANMX;
10007 cm->config_opts |= CM_CONFIG_TRSCANMX;
10008 }
10009
10010 cm->align_opts |= CM_ALIGN_CHECKINOUT;
10011
10012 if((status = cm_Configure(cm, errbuf, -1)) != eslOK) cm_Fail(errbuf);
10013
10014 /* setup logsum lookups (could do this only if nec based on options, but this is safer) */
10015 init_ilogsum();
10016 FLogsumInit();
10017
10018 /* create nonbanded matrices if nec */
10019 if(esl_opt_GetBoolean(go, "--std")) {
10020 cm->nb_mx = cm_mx_Create(cm->M);
10021 cm->nb_omx = cm_mx_Create(cm->M);
10022 cm->nb_shmx = cm_shadow_mx_Create(cm);
10023 cm->nb_emx = cm_emit_mx_Create(cm);
10024 }
10025
10026 if (esl_opt_IsUsed(go, "--thresh1")) { cm->cp9b->thresh1 = esl_opt_GetReal(go, "--thresh1"); }
10027 if (esl_opt_IsUsed(go, "--thresh2")) { cm->cp9b->thresh2 = esl_opt_GetReal(go, "--thresh2"); }
10028
10029 if (esl_opt_GetBoolean(go, "--noqdb")) {
10030 cm->search_opts |= CM_SEARCH_NONBANDED; /* don't use QDB to search */
10031 qdbidx = SMX_NOQDB;
10032 }
10033 else {
10034 qdbidx = SMX_QDB1_TIGHT;
10035 }
10036
10037 cm->tau = esl_opt_GetReal(go, "--tau");
10038
10039 if(! esl_opt_GetBoolean(go, "--onlyhb")) {
10040 printf("%-30s", "Creating tr matrix...");
10041 fflush(stdout);
10042 esl_stopwatch_Start(w);
10043 cm->trnb_mx = cm_tr_mx_Create(cm);
10044 cm->trnb_omx = cm_tr_mx_Create(cm);
10045 cm->trnb_emx = cm_tr_emit_mx_Create(cm);
10046 cm->trnb_shmx = cm_tr_shadow_mx_Create(cm);
10047 printf("done. ");
10048 fflush(stdout);
10049 esl_stopwatch_Stop(w);
10050 esl_stopwatch_Display(stdout, w, " CPU time: ");
10051 }
10052
10053 save_tau = cm->tau;
10054 save_cp9b_thresh1 = cm->cp9b->thresh1;
10055 save_cp9b_thresh2 = cm->cp9b->thresh2;
10056
10057 i = 0;
10058 sq = esl_sq_CreateDigital(cm->abc);
10059 while((status = esl_sqio_Read(sqfp, sq)) == eslOK) {
10060 i++;
10061 L = sq->n;
10062 dsq = sq->dsq;
10063 cm->search_opts &= ~CM_SEARCH_INSIDE;
10064
10065 cm->tau = save_tau;
10066 cm->cp9b->thresh1 = save_cp9b_thresh1;
10067 cm->cp9b->thresh2 = save_cp9b_thresh2;
10068
10069 cm->align_opts |= CM_ALIGN_HBANDED;
10070
10071 /* 1. non-banded truncated alignment, unless --onlyhb
10072 * 2. non-banded standard alignment, if requested
10073 * 3. HMM banded truncated alignment, if requested
10074 * 4. HMM banded standard alignment, if requested
10075 * 5. non-banded truncated search, if requested
10076 * 6. non-banded standard search, if requested
10077 * 7. HMM banded truncated search, if requested
10078 * 8. HMM banded standard search, if requested
10079 */
10080
10081 /* 1. non-banded truncated alignment, unless --onlyhb */
10082 if(! esl_opt_GetBoolean(go, "--onlyhb")) {
10083 /*********************Begin cm_TrAlign****************************/
10084 if(do_optacc) {
10085 esl_stopwatch_Start(w);
10086 if((status = cm_TrAlign(cm, errbuf, dsq, L, size_limit, TRMODE_UNKNOWN, pass_idx, TRUE, FALSE, cm->trnb_mx, cm->trnb_shmx, cm->trnb_omx, cm->trnb_emx, NULL, &ppstr, &tr, &mode, &pp_oa, &sc_oa)) != eslOK) cm_Fail(errbuf);
10087 printf("%4d %-30s %10.4f PP (mode: %s) (FULL LENGTH OPTACC)\n", i, "cm_TrAlign(): ", pp_oa, MarginalMode(mode));
10088 esl_stopwatch_Stop(w);
10089 esl_stopwatch_Display(stdout, w, " CPU time: ");
10090 if(esl_opt_GetBoolean(go, "--tr")) ParsetreeDump(stdout, tr, cm, dsq);
10091 ParsetreeScore(cm, NULL, NULL, tr, dsq, FALSE, &parsetree_sc, &parsetree_struct_sc, NULL, NULL, NULL);
10092 FreeParsetree(tr);
10093 free(ppstr); ppstr = NULL;
10094 printf("Parsetree score : %.4f (FULL LENGTH OPTACC)\n", parsetree_sc);
10095 }
10096 if(do_cyk) {
10097 esl_stopwatch_Start(w);
10098 if((status = cm_TrAlign(cm, errbuf, dsq, L, size_limit, TRMODE_UNKNOWN, pass_idx, FALSE, FALSE, cm->trnb_mx, cm->trnb_shmx, cm->trnb_omx, cm->trnb_emx, NULL,
10099 (do_compacc) ? &ppstr : NULL,
10100 &tr, &mode, &pp_cyk, &sc_cyk)) != eslOK) cm_Fail(errbuf);
10101 printf("%4d %-30s %10.4f pp %10.4f bits (mode: %s) (FULL LENGTH CYK)\n", i, "cm_TrAlign(): ", pp_cyk, sc_cyk, MarginalMode(mode));
10102 esl_stopwatch_Stop(w);
10103 esl_stopwatch_Display(stdout, w, " CPU time: ");
10104 if(esl_opt_GetBoolean(go, "--tr")) ParsetreeDump(stdout, tr, cm, dsq);
10105 ParsetreeScore(cm, NULL, NULL, tr, dsq, FALSE, &parsetree_sc, &parsetree_struct_sc, NULL, NULL, NULL);
10106 FreeParsetree(tr);
10107 if(ppstr != NULL) { free(ppstr); ppstr = NULL; }
10108 printf("Parsetree score : %.4f (FULL LENGTH CYK)\n", parsetree_sc);
10109 }
10110 if(do_compacc) {
10111 if((pp_oa - pp_cyk) < -0.0001) cm_Fail("ERROR OA PP: %f < CYK PP: %f\n", pp_oa, pp_cyk);
10112 }
10113 /*********************End cm_TrAlign****************************/
10114
10115 if(esl_opt_GetBoolean(go, "--cykout")) {
10116 /*********************Begin cm_TrCYKOutsideAlign****************************/
10117 esl_stopwatch_Start(w);
10118 status = cm_TrCYKOutsideAlign(cm, errbuf, dsq, L, size_limit, mode, pass_idx, TRUE, cm->trnb_omx, cm->trnb_mx);
10119 if (status != eslOK && esl_opt_GetBoolean(go, "--failok")) printf("%s\nError detected, but continuing thanks to --failok\n", errbuf);
10120 else if(status != eslOK) cm_Fail(errbuf);
10121 printf("%4d %-30s %10s bits ", i, "cm_TrCYKOutsideAlign() CYK:", "?");
10122 esl_stopwatch_Stop(w);
10123 esl_stopwatch_Display(stdout, w, " CPU time: ");
10124 /*********************End cm_TrCYKOutsideAlign****************************/
10125 }
10126
10127 /*********************Begin cm_TrInsideAlign()****************************/
10128 if((status = cm_TrInsideAlign(cm, errbuf, dsq, L, size_limit, TRMODE_UNKNOWN, pass_idx, cm->trnb_mx, NULL, &sc)) != eslOK) cm_Fail(errbuf);
10129 printf("%4d %-30s %10.4f bits (FULL LENGTH INSIDE)", i, "cm_TrInsideAlign(): ", sc);
10130 esl_stopwatch_Stop(w);
10131 esl_stopwatch_Display(stdout, w, " CPU time: ");
10132 /*********************End cm_TrInsideAlign*****************************/
10133 }
10134
10135 /* 2. non-banded standard (non-truncated) alignment, if requested */
10136 if(esl_opt_GetBoolean(go, "--std") && (! esl_opt_GetBoolean(go, "--onlyhb"))) {
10137 /*********************Begin cm_Align()****************************/
10138 if(do_optacc) {
10139 esl_stopwatch_Start(w);
10140 if((status = cm_Align (cm, errbuf, dsq, L, size_limit, TRUE, FALSE, cm->nb_mx, cm->nb_shmx, cm->nb_omx, cm->nb_emx, NULL, &ppstr, &tr, &pp_oa, &sc_oa)) != eslOK) return status;
10141 esl_stopwatch_Stop(w);
10142 esl_stopwatch_Display(stdout, w, " CPU time: ");
10143 if(esl_opt_GetBoolean(go, "--tr")) ParsetreeDump(stdout, tr, cm, dsq);
10144 ParsetreeScore(cm, NULL, NULL, tr, dsq, FALSE, &parsetree_sc, &parsetree_struct_sc, NULL, NULL, NULL);
10145 mode = ParsetreeMode(tr);
10146 FreeParsetree(tr);
10147 free(ppstr); ppstr = NULL;
10148 printf("%4d %-30s %10.4f pp (FULL LENGTH OPTACC)\n", i, "cm_Align(): ", pp_oa);
10149 printf("Parsetree score : %.4f (FULL LENGTH OPTACC)\n", parsetree_sc);
10150 }
10151 if(do_cyk) {
10152 esl_stopwatch_Start(w);
10153 if((status = cm_Align (cm, errbuf, dsq, L, size_limit, FALSE, FALSE, cm->nb_mx, cm->nb_shmx, cm->nb_omx, cm->nb_emx, NULL,
10154 (do_compacc) ? &ppstr : NULL,
10155 &tr, &pp_cyk, &sc_cyk)) != eslOK) return status;
10156 esl_stopwatch_Stop(w);
10157 esl_stopwatch_Display(stdout, w, " CPU time: ");
10158 if(esl_opt_GetBoolean(go, "--tr")) ParsetreeDump(stdout, tr, cm, dsq);
10159 ParsetreeScore(cm, NULL, NULL, tr, dsq, FALSE, &parsetree_sc, &parsetree_struct_sc, NULL, NULL, NULL);
10160 mode = ParsetreeMode(tr);
10161 FreeParsetree(tr);
10162 if(ppstr != NULL) { free(ppstr); ppstr = NULL; }
10163 printf("%4d %-30s %10.4f pp %10.4f bits (FULL LENGTH OPTACC)\n", i, "cm_Align(): ", pp_cyk, sc_cyk);
10164 printf("Parsetree score : %.4f (FULL LENGTH CYK)\n", parsetree_sc);
10165 }
10166 if(do_compacc) {
10167 if((pp_oa - pp_cyk) < -0.0001) cm_Fail("ERROR OA PP: %f < CYK PP: %f\n", pp_oa, pp_cyk);
10168 }
10169 /*********************End cm_Align*****************************/
10170
10171 if(esl_opt_GetBoolean(go, "--cykout")) {
10172 /*********************Begin cm_CYKOutsideAlign****************************/
10173 esl_stopwatch_Start(w);
10174 status = cm_CYKOutsideAlign(cm, errbuf, dsq, L, size_limit, TRUE, cm->nb_omx, cm->nb_mx, &sc);
10175 if (status != eslOK && esl_opt_GetBoolean(go, "--failok")) printf("%s\nError detected, but continuing thanks to --failok\n", errbuf);
10176 else if(status != eslOK) cm_Fail(errbuf);
10177 printf("%4d %-30s %10.4f bits ", i, "cm_CYKOutsideAlign() CYK:", sc);
10178 esl_stopwatch_Stop(w);
10179 esl_stopwatch_Display(stdout, w, " CPU time: ");
10180 /*********************End cm_CYKOutsideAlign****************************/
10181 }
10182
10183
10184 /*********************Begin cm_InsideAlign()****************************/
10185 esl_stopwatch_Start(w);
10186 if((status = cm_InsideAlign (cm, errbuf, dsq, L, size_limit, cm->nb_mx, &sc)) != eslOK) return status;
10187 printf("%4d %-30s %10.4f bits (FULL LENGTH INSIDE)", i, "cm_InsideAlign(): ", sc);
10188 esl_stopwatch_Stop(w);
10189 esl_stopwatch_Display(stdout, w, " CPU time: ");
10190 /*********************End cm_InsideAlign*****************************/
10191
10192 /*********************Begin cm_OutsideAlign*****************************/
10193 esl_stopwatch_Start(w);
10194 if((status = cm_OutsideAlign(cm, errbuf, dsq, L, size_limit, TRUE, cm->nb_omx, cm->nb_mx, &sc)) != eslOK) return status;
10195 printf("%4d %-30s %10.4f bits (FULL LENGTH OUTSIDE)", i, "cm_OutsideAlign(): ", sc);
10196 esl_stopwatch_Stop(w);
10197 esl_stopwatch_Display(stdout, w, " CPU time: ");
10198 /*********************End cm_OutsideAlign*****************************/
10199 if((status = cm_Posterior(cm, errbuf, L, size_limit, cm->nb_mx, cm->nb_omx, cm->nb_omx)) != eslOK) return status;
10200 }
10201
10202 /* 3. HMM banded truncated alignment, if requested */
10203 if(esl_opt_GetBoolean(go, "--hb") || esl_opt_GetBoolean(go, "--onlyhb")) {
10204 /*********************Begin cm_TrAlignHB()****************************/
10205 esl_stopwatch_Start(w);
10206 /* Calculate HMM bands. We'll tighten tau and recalculate bands until
10207 * the resulting HMM banded matrix is under our size limit.
10208 */
10209 cm->tau = save_tau;
10210 while(1) {
10211 if((status = cp9_Seq2Bands(cm, errbuf, cm->cp9_mx, cm->cp9_bmx, cm->cp9_bmx, dsq, 1, L, cm->cp9b,
10212 FALSE, /* doing search? */
10213 pass_idx, 0)) != eslOK) cm_Fail(errbuf);
10214 if((status = cm_tr_hb_mx_SizeNeeded(cm, errbuf, cm->cp9b, L, NULL, NULL, NULL, NULL, &trhbmx_Mb)) != eslOK) return status;
10215 if(trhbmx_Mb < size_limit) break; /* our matrix will be small enough, break out of while(1) */
10216 if(cm->tau > 0.01) cm_Fail("tau reached limit, unable to create matrix smaller than size limit of %.2f Mb\n", size_limit);
10217 printf("TrCYK 0 tau: %10g thresh1: %10g thresh2: %10g trhbmx_Mb: %10.2f\n", cm->tau, cm->cp9b->thresh1, cm->cp9b->thresh2, trhbmx_Mb);
10218 cm->tau *= 2.;
10219 cm->cp9b->thresh1 *= 2.;
10220 cm->cp9b->thresh2 -= (1.0-cm->cp9b->thresh2);
10221 cm->cp9b->thresh1 = ESL_MIN(0.25, cm->cp9b->thresh1);
10222 cm->cp9b->thresh2 = ESL_MAX(0.25, cm->cp9b->thresh2);
10223 }
10224 printf("TrCYK 1 tau: %10g thresh1: %10g thresh2: %10g trhbmx_Mb: %10.2f\n", cm->tau, cm->cp9b->thresh1, cm->cp9b->thresh2, trhbmx_Mb);
10225 esl_stopwatch_Stop(w);
10226 printf("%4d %-30s %17s", i, "HMM Band calc:", "");
10227 esl_stopwatch_Display(stdout, w, "CPU time: ");
10228
10229 /*PrintDPCellsSaved_jd(cm, cm->cp9b->jmin, cm->cp9b->jmax, cm->cp9b->hdmin, cm->cp9b->hdmax, L);*/
10230
10231
10232 if(do_optacc) {
10233 esl_stopwatch_Start(w);
10234 if((status = cm_TrAlignHB(cm, errbuf, dsq, L, size_limit, TRMODE_UNKNOWN, pass_idx, TRUE, FALSE, cm->trhb_mx, cm->trhb_shmx, cm->trhb_omx, cm->trhb_emx, NULL, &ppstr, &tr, &mode, &pp_oa, &sc_oa)) != eslOK) cm_Fail(errbuf);
10235 printf("%4d %-30s %10.4f PP (mode: %s) (FULL LENGTH OPTACC)", i, "cm_TrAlignHB(): ", pp_oa, MarginalMode(mode));
10236 esl_stopwatch_Stop(w);
10237 esl_stopwatch_Display(stdout, w, " CPU time: ");
10238 if(esl_opt_GetBoolean(go, "--tr")) ParsetreeDump(stdout, tr, cm, dsq);
10239 ParsetreeScore(cm, NULL, NULL, tr, dsq, FALSE, &parsetree_sc, &parsetree_struct_sc, NULL, NULL, NULL);
10240 mode = ParsetreeMode(tr);
10241 FreeParsetree(tr);
10242 free(ppstr); ppstr = NULL;
10243 printf("Parsetree score : %.4f (FULL LENGTH OPTACC)\n", parsetree_sc);
10244 }
10245 if(do_cyk) {
10246 esl_stopwatch_Start(w);
10247 if((status = cm_TrAlignHB(cm, errbuf, dsq, L, size_limit, TRMODE_UNKNOWN, pass_idx, FALSE, FALSE, cm->trhb_mx, cm->trhb_shmx, cm->trhb_omx, cm->trhb_emx, NULL,
10248 (do_compacc) ? &ppstr : NULL,
10249 &tr, &mode, &pp_cyk, &sc_cyk)) != eslOK) cm_Fail(errbuf);
10250 printf("%4d %-30s %10.4f pp %10.4f bits (mode: %s) (FULL LENGTH CYK)", i, "cm_TrAlignHB(): ", pp_cyk, sc_cyk, MarginalMode(mode));
10251 esl_stopwatch_Stop(w);
10252 esl_stopwatch_Display(stdout, w, " CPU time: ");
10253 if(esl_opt_GetBoolean(go, "--tr")) ParsetreeDump(stdout, tr, cm, dsq);
10254 ParsetreeScore(cm, NULL, NULL, tr, dsq, FALSE, &parsetree_sc, &parsetree_struct_sc, NULL, NULL, NULL);
10255 mode = ParsetreeMode(tr);
10256 FreeParsetree(tr);
10257 if(ppstr != NULL) { free(ppstr); ppstr = NULL; }
10258 printf("Parsetree score : %.4f (FULL LENGTH CYK)\n", parsetree_sc);
10259 }
10260 if(do_compacc) {
10261 if((pp_oa - pp_cyk) < -0.0001) cm_Fail("ERROR OA PP: %f < CYK PP: %f\n", pp_oa, pp_cyk);
10262 }
10263 /*********************End cm_TrAlignHB*****************************/
10264
10265 if(esl_opt_GetBoolean(go, "--cykout")) {
10266 /*********************Begin cm_TrCYKOutsideAlignHB****************************/
10267 esl_stopwatch_Start(w);
10268 status = cm_TrCYKOutsideAlignHB(cm, errbuf, dsq, L, size_limit, mode, pass_idx, TRUE, cm->trhb_omx, cm->trhb_mx);
10269 if (status != eslOK && esl_opt_GetBoolean(go, "--failok")) printf("%s\nError detected, but continuing thanks to --failok\n", errbuf);
10270 else if(status != eslOK) cm_Fail(errbuf);
10271 printf("%4d %-30s %10s bits ", i, "cm_TrCYKOutsideAlignHB() CYK:", "?");
10272 esl_stopwatch_Stop(w);
10273 esl_stopwatch_Display(stdout, w, " CPU time: ");
10274 /*********************End cm_TrCYKOutsideAlignHB****************************/
10275 }
10276
10277 /*********************Begin cm_TrInsideAlignHB()****************************/
10278 esl_stopwatch_Start(w);
10279 if((status = cm_TrInsideAlignHB(cm, errbuf, dsq, L, size_limit, TRMODE_UNKNOWN, pass_idx, cm->trhb_mx, NULL, &sc)) != eslOK) cm_Fail(errbuf);
10280 printf("%4d %-30s %10.4f bits (FULL LENGTH INSIDE)", i, "cm_TrInsideAlignHB(): ", sc);
10281 esl_stopwatch_Stop(w);
10282 esl_stopwatch_Display(stdout, w, " CPU time: ");
10283 /*********************End cm_TrInsideAlignHB*****************************/
10284 }
10285
10286 /* 4. HMM banded standard alignment, if requested */
10287 if(esl_opt_GetBoolean(go, "--std") && (esl_opt_GetBoolean(go, "--hb") || esl_opt_GetBoolean(go, "--onlyhb"))) {
10288 /*********************Begin cm_AlignHB()***************************/
10289 esl_stopwatch_Start(w);
10290 while(1) {
10291 if((status = cp9_Seq2Bands(cm, errbuf, cm->cp9_mx, cm->cp9_bmx, cm->cp9_bmx, dsq, 1, L, cm->cp9b,
10292 FALSE, /* doing search? */
10293 PLI_PASS_STD_ANY, /* we are not allowing truncated alignments */
10294 0)) != eslOK) cm_Fail(errbuf);
10295 if((status = cm_hb_mx_SizeNeeded(cm, errbuf, cm->cp9b, L, NULL, &hbmx_Mb)) != eslOK) return status;
10296 if(hbmx_Mb < size_limit) break; /* our matrix will be small enough, break out of while(1) */
10297 if(cm->tau > 0.01) cm_Fail("tau reached limit, unable to create matrix smaller than size limit of %.2f Mb\n", size_limit);
10298 printf(" CYK 0 tau: %10g hbmx_Mb: %10.2f\n", cm->tau, hbmx_Mb);
10299 cm->tau *= 2.;
10300 }
10301
10302 esl_stopwatch_Stop(w);
10303 printf("%4d %-30s %17s", i+1, "HMM Band calc:", "");
10304 esl_stopwatch_Display(stdout, w, "CPU time: ");
10305
10306 /*PrintDPCellsSaved_jd(cm, cm->cp9b->jmin, cm->cp9b->jmax, cm->cp9b->hdmin, cm->cp9b->hdmax, L);*/
10307
10308 if(do_optacc) {
10309 esl_stopwatch_Start(w);
10310 if((status = cm_AlignHB(cm, errbuf, dsq, L, size_limit, TRUE, FALSE, cm->hb_mx, cm->hb_shmx, cm->hb_omx, cm->hb_emx, NULL, &ppstr, &tr, &pp_oa, &sc_oa)) != eslOK) cm_Fail(errbuf);
10311 esl_stopwatch_Stop(w);
10312 esl_stopwatch_Display(stdout, w, " CPU time: ");
10313 if(esl_opt_GetBoolean(go, "--tr")) ParsetreeDump(stdout, tr, cm, dsq);
10314 ParsetreeScore(cm, NULL, NULL, tr, dsq, FALSE, &parsetree_sc, &parsetree_struct_sc, NULL, NULL, NULL);
10315 FreeParsetree(tr);
10316 free(ppstr);
10317 printf("%4d %-30s %10.4f pp (FULL LENGTH OPTACC)\n", i, "cm_AlignHB(): ", pp_oa);
10318 printf("Parsetree score : %.4f (FULL LENGTH OPTACC)\n", parsetree_sc);
10319 }
10320 if(do_cyk) {
10321 if((status = cm_AlignHB(cm, errbuf, dsq, L, size_limit, FALSE, FALSE, cm->hb_mx, cm->hb_shmx, cm->hb_omx, cm->hb_emx, NULL,
10322 (do_compacc) ? &ppstr : NULL, &tr, &pp_cyk, &sc_cyk)) != eslOK) cm_Fail(errbuf);
10323 esl_stopwatch_Stop(w);
10324 esl_stopwatch_Display(stdout, w, " CPU time: ");
10325 if(esl_opt_GetBoolean(go, "--tr")) ParsetreeDump(stdout, tr, cm, dsq);
10326 ParsetreeScore(cm, NULL, NULL, tr, dsq, FALSE, &parsetree_sc, &parsetree_struct_sc, NULL, NULL, NULL);
10327 FreeParsetree(tr);
10328 if(ppstr != NULL) { free(ppstr); ppstr = NULL; }
10329 printf("%4d %-30s %10.4f pp %10.4f bits (FULL LENGTH CYK)\n", i, "cm_AlignHB(): ", pp_cyk, sc_cyk);
10330 printf("Parsetree score : %.4f (FULL LENGTH CYK)\n", parsetree_sc);
10331 }
10332 if(do_compacc) {
10333 if((pp_oa - pp_cyk) < -0.0001) cm_Fail("ERROR OA PP: %f < CYK PP: %f\n", pp_oa, pp_cyk);
10334 }
10335 /*********************End cm_AlignHB()***************************/
10336 }
10337
10338 if(esl_opt_GetBoolean(go, "--search")) {
10339 /* 5. non-banded truncated search, if requested */
10340 /*********************Begin RefTrCYKScan****************************/
10341 esl_stopwatch_Start(w);
10342 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);
10343 printf("%4d %-30s %10.4f bits (mode: %s)", i, "RefTrCYKScan(): ", sc, MarginalMode(mode));
10344 esl_stopwatch_Stop(w);
10345 esl_stopwatch_Display(stdout, w, " CPU time: ");
10346 /*********************End RefTrCYKScan****************************/
10347
10348 /*********************Begin RefITrInsideScan****************************/
10349 cm->search_opts |= CM_SEARCH_INSIDE;
10350 esl_stopwatch_Start(w);
10351 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);
10352 printf("%4d %-30s %10.4f bits (mode: %s)", i, "RefITrInsideScan(): ", sc, MarginalMode(mode));
10353 esl_stopwatch_Stop(w);
10354 esl_stopwatch_Display(stdout, w, " CPU time: ");
10355 cm->search_opts &= ~CM_SEARCH_INSIDE;
10356 /*********************End RefITrInsideScan****************************/
10357
10358 if(esl_opt_GetBoolean(go, "--orig")) {
10359 /*********************Begin TrCYK_Inside****************************/
10360 esl_stopwatch_Start(w);
10361 sc = TrCYK_Inside(cm, dsq, L, 0, 1, L, pass_idx, FALSE, FALSE, NULL);
10362 printf("%4d %-30s %10.4f bits ", i, "TrCYK_Inside(): ", sc);
10363 esl_stopwatch_Stop(w);
10364 esl_stopwatch_Display(stdout, w, " CPU time: ");
10365 /*********************End TrCYK_Inside****************************/
10366 }
10367
10368 /* 6. non-banded standard search, if requested */
10369 /*********************Begin FastCYKScan****************************/
10370 esl_stopwatch_Start(w);
10371 if((status = FastCYKScan(cm, errbuf, cm->smx, qdbidx, dsq, 1, L, 0., NULL, FALSE, 0., NULL, NULL, NULL, &sc)) != eslOK) cm_Fail(errbuf);
10372 printf("%4d %-30s %10.4f bits ", i, "FastCYKScan(): ", sc);
10373 esl_stopwatch_Stop(w);
10374 esl_stopwatch_Display(stdout, w, " CPU time: ");
10375 /*********************End FastCYKScan****************************/
10376
10377 /*********************Begin FastIInsideScan****************************/
10378 cm->search_opts |= CM_SEARCH_INSIDE;
10379 esl_stopwatch_Start(w);
10380 if((status = FastIInsideScan(cm, errbuf, cm->smx, qdbidx, dsq, 1, L, 0., NULL, FALSE, 0., NULL, NULL, NULL, &sc)) != eslOK) cm_Fail(errbuf);
10381 printf("%4d %-30s %10.4f bits ", i, "FastIInsideScan(): ", sc);
10382 esl_stopwatch_Stop(w);
10383 esl_stopwatch_Display(stdout, w, " CPU time: ");
10384 cm->search_opts &= ~CM_SEARCH_INSIDE;
10385 /*********************End RefITrInsideScan****************************/
10386
10387 /* 7. HMM banded truncated search, if requested */
10388 if(esl_opt_GetBoolean(go, "--hb") || esl_opt_GetBoolean(go, "--onlyhb")) {
10389 /*********************Begin TrCYKScanHB****************************/
10390 esl_stopwatch_Start(w);
10391 /* Calculate HMM bands. We'll tighten tau and recalculate bands until
10392 * the resulting HMM banded matrix is under our size limit.
10393 */
10394 cm->tau = save_tau;
10395 while(1) {
10396 if((status = cp9_Seq2Bands(cm, errbuf, cm->cp9_mx, cm->cp9_bmx, cm->cp9_bmx, dsq, 1, L, cm->cp9b,
10397 TRUE, /* doing search? */
10398 pass_idx, 0)) != eslOK) cm_Fail(errbuf);
10399 if((status = cm_tr_hb_mx_SizeNeeded(cm, errbuf, cm->cp9b, L, NULL, NULL, NULL, NULL, &trhbmx_Mb)) != eslOK) return status;
10400 if(trhbmx_Mb < size_limit) break; /* our matrix will be small enough, break out of while(1) */
10401 if(cm->tau > 0.01) cm_Fail("tau reached limit, unable to create matrix smaller than size limit of %.2f Mb\n", size_limit);
10402 printf("TrCYK 0 tau: %10g thresh1: %10g thresh2: %10g trhbmx_Mb: %10.2f\n", cm->tau, cm->cp9b->thresh1, cm->cp9b->thresh2, trhbmx_Mb);
10403 cm->tau *= 2.;
10404 cm->cp9b->thresh1 *= 2.;
10405 cm->cp9b->thresh2 -= (1.0-cm->cp9b->thresh2);
10406 cm->cp9b->thresh1 = ESL_MIN(0.25, cm->cp9b->thresh1);
10407 cm->cp9b->thresh2 = ESL_MAX(0.25, cm->cp9b->thresh2);
10408 }
10409 printf("TrCYK 1 tau: %10g thresh1: %10g thresh2: %10g trhbmx_Mb: %10.2f\n", cm->tau, cm->cp9b->thresh1, cm->cp9b->thresh2, trhbmx_Mb);
10410 esl_stopwatch_Stop(w);
10411 printf("%4d %-30s %17s", i+1, "HMM Band calc:", "");
10412 esl_stopwatch_Display(stdout, w, "CPU time: ");
10413
10414 /*PrintDPCellsSaved_jd(cm, cm->cp9b->jmin, cm->cp9b->jmax, cm->cp9b->hdmin, cm->cp9b->hdmax, L);*/
10415
10416 esl_stopwatch_Start(w);
10417 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);
10418 printf("%4d %-30s %10.4f bits (mode: %s)", i, "TrCYKScanHB(): ", sc, MarginalMode(mode));
10419 esl_stopwatch_Stop(w);
10420 esl_stopwatch_Display(stdout, w, " CPU time: ");
10421 /*********************End TrCYKScanHB****************************/
10422
10423 /*********************Begin FTrInsideScanHB****************************/
10424 esl_stopwatch_Start(w);
10425 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);
10426 printf("%4d %-30s %10.4f bits (mode: %s)", i, "FTrInsideScanHB(): ", sc, MarginalMode(mode));
10427 esl_stopwatch_Stop(w);
10428 esl_stopwatch_Display(stdout, w, " CPU time: ");
10429 /*********************End FTrInsideScanHB***********************/
10430
10431 /* 8. HMM banded standard search, if requested */
10432 if(esl_opt_GetBoolean(go, "--std") && (esl_opt_GetBoolean(go, "--hb") || esl_opt_GetBoolean(go, "--onlyhb"))) {
10433 /*********************Begin FastCYKScanHB****************************/
10434 esl_stopwatch_Start(w);
10435 cm->tau = save_tau;
10436 while(1) {
10437 if((status = cp9_Seq2Bands(cm, errbuf, cm->cp9_mx, cm->cp9_bmx, cm->cp9_bmx, dsq, 1, L, cm->cp9b,
10438 TRUE, /* doing search? */
10439 PLI_PASS_STD_ANY, /* we are not allowing truncated alignments */
10440 0)) != eslOK) cm_Fail(errbuf);
10441 if((status = cm_hb_mx_SizeNeeded(cm, errbuf, cm->cp9b, L, NULL, &hbmx_Mb)) != eslOK) return status;
10442 if(hbmx_Mb < size_limit) break; /* our matrix will be small enough, break out of while(1) */
10443 if(cm->tau > 0.01) cm_Fail("tau reached limit, unable to create matrix smaller than size limit of %.2f Mb\n", size_limit);
10444 printf(" CYK 0 tau: %10g hbmx_Mb: %10.2f\n", cm->tau, hbmx_Mb);
10445 cm->tau *= 2.;
10446 }
10447
10448 esl_stopwatch_Stop(w);
10449 printf("%4d %-30s %17s", i+1, "HMM Band calc:", "");
10450 esl_stopwatch_Display(stdout, w, "CPU time: ");
10451
10452 /*PrintDPCellsSaved_jd(cm, cm->cp9b->jmin, cm->cp9b->jmax, cm->cp9b->hdmin, cm->cp9b->hdmax, L);*/
10453
10454 esl_stopwatch_Start(w);
10455 if((status = FastCYKScanHB(cm, errbuf, cm->hb_mx, size_limit, dsq, 1, L, 0., NULL, FALSE, 0., NULL, NULL, &sc)) != eslOK) cm_Fail(errbuf);
10456 printf("%4d %-30s %10.4f bits ", i, "FastCYKScanHB(): ", sc);
10457 esl_stopwatch_Stop(w);
10458 esl_stopwatch_Display(stdout, w, " CPU time: ");
10459 /*********************End FastCYKScanHB****************************/
10460 }
10461 }
10462 }
10463 printf("\n");
10464 esl_sq_Reuse(sq);
10465 }
10466 if(status != eslEOF) cm_Fail("ERROR reading sequence file, sequence number %d\n", i);
10467
10468 FreeCM(cm);
10469 esl_sq_Destroy(sq);
10470 esl_alphabet_Destroy(abc);
10471 esl_stopwatch_Destroy(w);
10472 esl_getopts_Destroy(go);
10473 esl_sqfile_Close(sqfp);
10474
10475 return 0;
10476 }
10477 #endif /*IMPL_TRUNC_ALIGN_BENCHMARK*/