Debian Codegraph @

src/hmmband.c

raw symbols SCIP
1
2 /* hmmband.c
3 * EPN 12.16.05
4 *
5 * Functions to support deriving bands for a constrained CM
6 * parse of a target sequence using CM. Bands are derived
7 * from CM plan 9 HMM (CP9 HMM) Forward/Backward parses of
8 * the target.
9 */
10
11 #include <esl_config.h>
12 #include <p7_config.h>
13 #include "config.h"
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include <float.h>
20 #include <limits.h>
21 #include <math.h>
22
23 #include "easel.h"
24 #include "esl_stack.h"
25 #include "esl_vectorops.h"
26
27 #include "hmmer.h"
28
29 #include "infernal.h"
30
31
32 static int cp9_FB2HMMBands (CP9_t *hmm, char *errbuf, ESL_DSQ *dsq, CP9_MX *fmx, CP9_MX *bmx, CP9_MX *pmx, CP9Bands_t *cp9b,
33 int i0, int j0, int M, double p_thresh, int did_fwd_scan, int did_bck_scan, int do_old_hmm2ij, int debug_level);
34 static int cp9_FB2HMMBandsWithSums(CP9_t *hmm, char *errbuf, ESL_DSQ *dsq, CP9_MX *fmx, CP9_MX *bmx, CP9_MX *pmx, CP9Bands_t *cp9b,
35 int i0, int j0, int M, double p_thresh, int did_fwd_scan, int did_bck_scan, int do_old_hmm2ij, int debug_level);
36 static void cp9_Posterior(ESL_DSQ *dsq, int i0, int j0, CP9_t *hmm, CP9_MX *fmx, CP9_MX *bmx, CP9_MX *mx, int did_fwd_scan);
37 static void cp9_IFillPostSums(CP9_MX *post, CP9Bands_t *cp9, int i0, int j0);
38 static int HMMBandsEnforceValidParse(CP9_t *cp9, CP9Bands_t *cp9b, CP9Map_t *cp9map, char *errbuf, int i0, int j0, int doing_search,
39 int **ret_r_mn, int **ret_r_mx, int **ret_r_in, int **ret_r_ix, int **ret_r_dn, int **ret_r_dx,
40 int **ret_r_nn_i, int **ret_r_nx_i, int **ret_r_nn_j, int **ret_r_nx_j);
41 static int HMMBandsFixUnreachable(CP9Bands_t *cp9b, char *errbuf, int k, int r_prv_min, int r_prv_max, int r_insert_prv_min);
42 static int HMMBandsFillGap(CP9Bands_t *cp9b, char *errbuf, int k, int min1, int max1, int min2, int max2, int prv_nd_r_mn, int prv_nd_r_dn);
43 #if eslDEBUGLEVEL >= 3
44 static int CMBandsCheckValidParse(CM_t *cm, CP9Bands_t *cp9b, char *errbuf, int i0, int j0, int doing_search);
45 #endif
46
47 /* EPN 10.28.06
48 * Function: AllocCP9Bands()
49 *
50 * Purpose: Allocate the arrays needed for creating i and j
51 * bands on a CM based on a CP9 parse. See infernal.h
52 * for description of this structure.
53 *
54 * Args:
55 * cm_M - number of states in the CM
56 * hmm_M - number of nodes in the CP9 HMM for the CM
57 * Returns: (void)
58 *
59 */
60
61 CP9Bands_t *
62 AllocCP9Bands(int cm_M, int hmm_M)
63 {
64 int status;
65 CP9Bands_t *cp9bands;
66
67 ESL_ALLOC(cp9bands, sizeof(CP9Bands_t));
68
69 cp9bands->cm_M = cm_M;
70 cp9bands->hmm_M = hmm_M;
71
72 cp9bands->sp1 = cp9bands->sp2 = cp9bands->ep1 = cp9bands->ep2 = -1;
73 cp9bands->thresh1 = DEFAULT_CP9BANDS_THRESH1; /* 0.01 */
74 cp9bands->thresh2 = DEFAULT_CP9BANDS_THRESH2; /* 0.98 */
75 cp9bands->Rmarg_imin = cp9bands->Lmarg_jmin = -1;
76 cp9bands->Rmarg_imax = cp9bands->Lmarg_jmax = -2;
77
78 ESL_ALLOC(cp9bands->Jvalid, sizeof(int) * (cm_M+1));
79 ESL_ALLOC(cp9bands->Lvalid, sizeof(int) * (cm_M+1));
80 ESL_ALLOC(cp9bands->Rvalid, sizeof(int) * (cm_M+1));
81 ESL_ALLOC(cp9bands->Tvalid, sizeof(int) * (cm_M+1));
82 esl_vec_ISet(cp9bands->Jvalid, cm_M+1, TRUE);
83 esl_vec_ISet(cp9bands->Lvalid, cm_M+1, TRUE);
84 esl_vec_ISet(cp9bands->Rvalid, cm_M+1, TRUE);
85 esl_vec_ISet(cp9bands->Tvalid, cm_M, TRUE);
86 cp9bands->Tvalid[cm_M] = FALSE;
87
88 ESL_ALLOC(cp9bands->pn_min_m, sizeof(int) * (cp9bands->hmm_M+1));
89 ESL_ALLOC(cp9bands->pn_max_m, sizeof(int) * (cp9bands->hmm_M+1));
90 ESL_ALLOC(cp9bands->pn_min_i, sizeof(int) * (cp9bands->hmm_M+1));
91 ESL_ALLOC(cp9bands->pn_max_i, sizeof(int) * (cp9bands->hmm_M+1));
92 ESL_ALLOC(cp9bands->pn_min_d, sizeof(int) * (cp9bands->hmm_M+1));
93 ESL_ALLOC(cp9bands->pn_max_d, sizeof(int) * (cp9bands->hmm_M+1));
94 ESL_ALLOC(cp9bands->isum_pn_m,sizeof(int) * (cp9bands->hmm_M+1));
95 ESL_ALLOC(cp9bands->isum_pn_i,sizeof(int) * (cp9bands->hmm_M+1));
96 ESL_ALLOC(cp9bands->isum_pn_d,sizeof(int) * (cp9bands->hmm_M+1));
97
98 ESL_ALLOC(cp9bands->imin, sizeof(int) * cp9bands->cm_M);
99 ESL_ALLOC(cp9bands->imax, sizeof(int) * cp9bands->cm_M);
100 ESL_ALLOC(cp9bands->jmin, sizeof(int) * cp9bands->cm_M);
101 ESL_ALLOC(cp9bands->jmax, sizeof(int) * cp9bands->cm_M);
102 ESL_ALLOC(cp9bands->safe_hdmin, sizeof(int) * cp9bands->cm_M);
103 ESL_ALLOC(cp9bands->safe_hdmax, sizeof(int) * cp9bands->cm_M);
104 ESL_ALLOC(cp9bands->hdmin, sizeof(int *) * cp9bands->cm_M);
105 ESL_ALLOC(cp9bands->hdmax, sizeof(int *) * cp9bands->cm_M);
106 cp9bands->hdmin_mem = NULL;
107 cp9bands->hdmax_mem = NULL;
108 /* NOTE: cp9bands->hdmin and hdmax are 2D arrays, the ptrs are
109 * alloc'ed here, but the actually memory is alloc'ed by
110 * hmmband.c:cp9_Seq2Bands() with a call to hmmband.c:cp9_GrowHDBands().
111 */
112 cp9bands->hd_needed = 0;
113 cp9bands->hd_alloced = 0;
114
115 cp9bands->tau = -1.; /* invalid, reset each time bands are calculated */
116 return cp9bands;
117
118 ERROR:
119 cm_Fail("Memory allocation error.\n");
120 return NULL; /* never reached */
121 }
122
123 /* Function: SizeofCP9Bands()
124 * Returns: Size (Mb) of cp9b.
125 */
126 float
127 SizeofCP9Bands(CP9Bands_t *cp9b)
128 {
129 float bytes = 0.;
130
131 bytes += sizeof(CP9Bands_t);
132
133 /* following from AllocCP9Bands() */
134 bytes += sizeof(int) * (cp9b->cm_M+1); /* Jvalid */
135 bytes += sizeof(int) * (cp9b->cm_M+1); /* Lvalid */
136 bytes += sizeof(int) * (cp9b->cm_M+1); /* Rvalid */
137 bytes += sizeof(int) * (cp9b->cm_M+1); /* Tvalid */
138
139 bytes += sizeof(int) * (cp9b->hmm_M+1); /* pn_min_m */
140 bytes += sizeof(int) * (cp9b->hmm_M+1); /* pn_max_m */
141 bytes += sizeof(int) * (cp9b->hmm_M+1); /* pn_min_i */
142 bytes += sizeof(int) * (cp9b->hmm_M+1); /* pn_max_i */
143 bytes += sizeof(int) * (cp9b->hmm_M+1); /* pn_min_d */
144 bytes += sizeof(int) * (cp9b->hmm_M+1); /* pn_max_d */
145 bytes += sizeof(int) * (cp9b->hmm_M+1); /* isum_pn_m */
146 bytes += sizeof(int) * (cp9b->hmm_M+1); /* isum_pn_i */
147 bytes += sizeof(int) * (cp9b->hmm_M+1); /* isum_pn_d */
148
149 bytes += sizeof(int) * cp9b->cm_M; /* imin */
150 bytes += sizeof(int) * cp9b->cm_M; /* imax */
151 bytes += sizeof(int) * cp9b->cm_M; /* jmin */
152 bytes += sizeof(int) * cp9b->cm_M; /* jmax */
153 bytes += sizeof(int) * cp9b->cm_M; /* safe_hdmin */
154 bytes += sizeof(int) * cp9b->cm_M; /* safe_hdmax */
155 bytes += sizeof(int *) * cp9b->cm_M; /* hdmin */
156 bytes += sizeof(int *) * cp9b->cm_M; /* hdmax */
157
158 bytes += sizeof(int) * cp9b->hd_alloced; /* hdmin */
159 bytes += sizeof(int) * cp9b->hd_alloced; /* hdmax */
160
161 return bytes / 1000000.;
162 }
163
164 /* Function: FreeCP9Bands()
165 * Returns: (void)
166 */
167 void
168 FreeCP9Bands(CP9Bands_t *cp9bands)
169 {
170 free(cp9bands->imin);
171 free(cp9bands->imax);
172 free(cp9bands->jmin);
173 free(cp9bands->jmax);
174 free(cp9bands->safe_hdmin);
175 free(cp9bands->safe_hdmax);
176 if(cp9bands->hdmin_mem != NULL)
177 free(cp9bands->hdmin_mem); /* all v were malloc'ed as a block */
178 if(cp9bands->hdmax_mem != NULL)
179 free(cp9bands->hdmax_mem); /* all v were malloc'ed as a block */
180 free(cp9bands->hdmin);
181 free(cp9bands->hdmax);
182
183 free(cp9bands->pn_min_m);
184 free(cp9bands->pn_max_m);
185 free(cp9bands->pn_min_i);
186 free(cp9bands->pn_max_i);
187 free(cp9bands->pn_min_d);
188 free(cp9bands->pn_max_d);
189 free(cp9bands->isum_pn_m);
190 free(cp9bands->isum_pn_i);
191 free(cp9bands->isum_pn_d);
192
193 free(cp9bands->Jvalid);
194 free(cp9bands->Lvalid);
195 free(cp9bands->Rvalid);
196 free(cp9bands->Tvalid);
197
198 free(cp9bands);
199 }
200
201 /* Function: cp9_Seq2Bands
202 * Date : EPN, Mon Jan 8 07:23:34 2007
203 * EPN, Wed Oct 17 04:53:58 2007 [updated/optimized]
204 *
205 * Purpose: Given a CM with precalc'ed CP9 HMM and CP9Map, a sequence and
206 * a CP9Bands_t structure, calculate the HMM bands and store them
207 * in the CP9Bands_t structure.
208 *
209 * Args: cm - the covariance model
210 * errbuf - char buffer for reporting errors
211 * fmx - CP9 dp matrix for Forward()
212 * bmx - CP9 dp matrix for Backward()
213 * pmx - CP9 dp matrix to fill with posteriors, can == bmx
214 * dsq - sequence in digitized form
215 * i0 - start of target subsequence (often 1, beginning of sq)
216 * j0 - end of target subsequence (often L, end of sq)
217 * cp9b - PRE-ALLOCATED, the HMM bands for this sequence, filled here.
218 * doing_search - TRUE if we're going to use these HMM bands for search, not alignment
219 * pass_idx - pipeline pass index, tells us which truncation modes to allow, if any
220 * debug_level - verbosity level for debugging printf()s
221 *
222 * Return: eslOK on success;
223 *
224 */
225 int
226 cp9_Seq2Bands(CM_t *cm, char *errbuf, CP9_MX *fmx, CP9_MX *bmx, CP9_MX *pmx, ESL_DSQ *dsq, int i0, int j0, CP9Bands_t *cp9b, int doing_search, int pass_idx, int debug_level)
227 {
228 int status;
229 int use_sums; /* TRUE to fill and use posterior sums during HMM band calc, yields wider bands */
230 float sc;
231 int do_old_hmm2ij;
232 int do_trunc; /* are we allowing truncated alignments (either L or R)? */
233 int do_fwd_scan; /* run Forward in scanning mode? (see long comment on this below by assignment of do_fwd_scan) */
234 int do_bck_scan; /* run Backward in scanning mode? (see long comment on this below by assignment of do_fwd_scan) */
235 CP9_t *cp9 = NULL; /* ptr to cp9 HMM (cm->cp9, cm->Lcp9, cm->Rcp9, or cm->Tcp9) we'll use for deriving bands */
236
237 /* Contract checks */
238 if(cm->cp9map == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_Seq2Bands, but cm->cp9map is NULL.\n");
239 if(dsq == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_Seq2Bands, dsq is NULL.");
240 if(i0 > j0) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_Seq2Bands, i0: %d > j0: %d\n", i0, j0);
241 if(cm->tau > 0.5) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_Seq2Bands, cm->tau (%f) > 0.5, we can't deal.", cm->tau);
242
243 use_sums = ((cm->align_opts & CM_ALIGN_SUMS) || (cm->search_opts & CM_SEARCH_SUMS)) ? TRUE : FALSE;
244 do_old_hmm2ij = ((cm->align_opts & CM_ALIGN_HMM2IJOLD) || (cm->search_opts & CM_SEARCH_HMM2IJOLD)) ? TRUE : FALSE;
245
246 /* Determine which cp9 HMM to use and whether or not we're doing
247 * truncated alignment, based on value of pass_idx.
248 */
249 switch(pass_idx) {
250 case PLI_PASS_5P_ONLY_FORCE: do_trunc = TRUE; cp9 = cm->Rcp9; break;
251 case PLI_PASS_3P_ONLY_FORCE: do_trunc = TRUE; cp9 = cm->Lcp9; break;
252 case PLI_PASS_5P_AND_3P_FORCE: do_trunc = TRUE; cp9 = cm->Tcp9; break;
253 case PLI_PASS_5P_AND_3P_ANY: do_trunc = TRUE; cp9 = cm->Tcp9; break;
254 default: do_trunc = FALSE; cp9 = cm->cp9; break;
255 }
256 if(cp9 == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_Seq2Bands, relevant cp9 is NULL.\n");
257
258 /* Determine if we should do Forward and Backward in scan mode.
259 * When in scan mode, Forward will allow parses to start at any
260 * position, else they must start at i0 (first res).
261 * When in scan mode, Backward will allow parses to end at any
262 * position, else they must end at j0 (final res).
263 *
264 * We should only scan in Forward if i0 does not need to be in any
265 * eventual CM parsetree we derive using these bands. This is only
266 * true if we'll use these bands for a CM search
267 * (doing_search==TRUE) and that search won't be a special truncated
268 * search where i0 must be in any valid parsetree. We will be doing
269 * a truncated search enforcing i0 inclusion if pass_idx is either
270 * PLI_PASS_5P_AND_3P or PLI_PASS_5P_ONLY.
271 *
272 * Likewise, we should only scan in Backward if j0 does not need to
273 * be in any eventual CM parsetree we derive using these bands.
274 * This is only true if we'll use these bands for a CM search
275 * (doing_search==TRUE) and that search won't be a special truncated
276 * search where j0 must be in any valid parsetree. We will be doing
277 * a truncated search enforcing j0 inclusion if pass_idx is either
278 * PLI_PASS_5P_AND_3P or PLI_PASS_3P_ONLY.
279 */
280 if((! doing_search) || (cm->search_opts & CM_SEARCH_HMMALNBANDS)) {
281 do_fwd_scan = do_bck_scan = FALSE;
282 }
283 else {
284 do_fwd_scan = cm_pli_PassEnforcesFirstRes(pass_idx) ? FALSE : TRUE;
285 do_bck_scan = cm_pli_PassEnforcesFinalRes(pass_idx) ? FALSE : TRUE;
286 }
287
288 /* Step 1: Get HMM Forward/Backward DP matrices.
289 * Step 2: F/B -> HMM bands.
290 * Step 3: Calculate candidate states for truncated alignments
291 * Step 4: HMM bands -> CM bands.
292 */
293
294 /* Step 1: Get HMM Forward/Backward DP matrices. */
295 if((status = cp9_Forward(cp9, errbuf, fmx, dsq, i0, j0,
296 do_fwd_scan, /* allow parses to start at any posn? */
297 (! doing_search), /* are we going to use bands to align? */
298 FALSE, /* don't be memory efficient */
299 NULL, NULL,
300 &sc)) != eslOK) return status;
301
302 if((status = cp9_Backward(cp9, errbuf, bmx, dsq, i0, j0,
303 do_bck_scan, /* allow parses to end at any posn? */
304 (! doing_search), /* are we going to use posteriors to align? */
305 FALSE, /* don't be memory efficient */
306 NULL, NULL,
307 &sc)) != eslOK) return status;
308
309 if(cm->align_opts & CM_ALIGN_CHECKFB) {
310 if((status = cp9_CheckFB(fmx, bmx, cp9, errbuf, sc, i0, j0, dsq)) != eslOK) return status;
311 printf("Forward/Backward matrices checked.\n");
312 }
313
314 /* Step 2: F/B -> HMM bands. */
315 if(use_sums){
316 if((status = cp9_FB2HMMBandsWithSums(cp9, errbuf, dsq, fmx, bmx, pmx, cp9b, i0, j0, cp9b->hmm_M,
317 (1.-cm->tau), do_fwd_scan, do_bck_scan, do_old_hmm2ij, debug_level)) != eslOK) return status;
318 }
319 else {
320 if((status = cp9_FB2HMMBands(cp9, errbuf, dsq, fmx, bmx, pmx, cp9b, i0, j0, cp9b->hmm_M,
321 (1.-cm->tau), do_fwd_scan, do_bck_scan, do_old_hmm2ij, debug_level)) != eslOK) return status;
322 }
323 if(debug_level > 0) cp9_DebugPrintHMMBands(stdout, j0, cp9b, cm->tau, 1);
324
325 /* Step 3: (only if truncated alignments are possible)
326 * Calculate occupancy and candidate states for marginal alignments
327 */
328 if(do_trunc) {
329 cp9_PredictStartAndEndPositions(pmx, cp9b, i0, j0);
330 if((status = cp9_MarginalCandidatesFromStartEndPositions(cm, cp9b, pass_idx, errbuf)) != eslOK) return status;
331 /* xref: ELN2 notebook, p.146-147; ~nawrockie/notebook/11_0816_inf_banded_trcyk/00LOG */
332 }
333 else {
334 /* reset all Jvalid values to TRUE */
335 esl_vec_ISet(cp9b->Jvalid, cm->M+1, TRUE);
336 /* and all {L,R,T}valid values to FALSE */
337 esl_vec_ISet(cp9b->Lvalid, cm->M+1, FALSE);
338 esl_vec_ISet(cp9b->Rvalid, cm->M+1, FALSE);
339 esl_vec_ISet(cp9b->Tvalid, cm->M+1, FALSE);
340 }
341
342 /* Step 4: HMM bands -> CM bands. */
343 if(do_old_hmm2ij) {
344 if((status = cp9_HMM2ijBands_OLD(cm, errbuf, cp9b, cm->cp9map, i0, j0, doing_search, debug_level)) != eslOK) return status;
345 }
346 else {
347 if((status = cp9_HMM2ijBands(cm, errbuf, cp9, cp9b, cm->cp9map, i0, j0, doing_search, do_trunc, debug_level)) != eslOK) return status;
348 }
349
350 /* Use the CM bands on i and j to get bands on d, specific to j. */
351 /* cp9_GrowHDBands() must be called before ij2d_bands() so hdmin, hdmax are adjusted for new seq */
352 if((status = cp9_GrowHDBands(cp9b, errbuf)) != eslOK) return status;
353 ij2d_bands(cm, (j0-i0+1), cp9b->imin, cp9b->imax, cp9b->jmin, cp9b->jmax, cp9b->hdmin, cp9b->hdmax, do_trunc, debug_level);
354
355 #if eslDEBUGLEVEL >= 1
356 if((status = cp9_ValidateBands(cm, errbuf, cp9b, i0, j0, do_trunc)) != eslOK) return status;
357 ESL_DPRINTF1(("#DEBUG: bands validated.\n"));
358 #endif
359 if(debug_level > 0) debug_print_ij_bands(cm);
360 if(debug_level > 0) PrintDPCellsSaved_jd(cm, cp9b->jmin, cp9b->jmax, cp9b->hdmin, cp9b->hdmax, (j0-i0+1));
361
362 return eslOK;
363 }
364
365 /* Function: cp9_IterateSeq2Bands()
366 * Incept: EPN, Thu Mar 1 17:56:42 2012
367 *
368 * Purpose: Increase cm->tau (tighten HMM bands) by multiplying it
369 * by TAU_MULTIPLIER (2.0) until required HMM banded matrix
370 * size is below <size_limit> Mb, or cm->tau is greater than
371 * <maxtau>.
372 *
373 * If we're doing a truncated alignment (which we can figure
374 * out based on the value of <pass_idx>) then we also increase
375 * cp9b->thresh1 and decrease cp9b->thresh2 by a hard-coded
376 * value into the maximum/minimum is reached for them as
377 * wel..
378 *
379 * Since we can't determine the required size of a HB
380 * matrix unless we have filled a CP9Bands_t object
381 * (cm->cp9b), we need to recalculate bands each time tau,
382 * (and possibly thresh1 and thresh2) are modified and then
383 * check size of resulting matrix given the bands.
384 *
385 * Upon returning cm->tau, cm->cp9b->tau, cm->cp9b->thresh1
386 * and cm->cp9b->thresh2 may have been changed.
387 *
388 * Args cm - the CM
389 * errbuf - for error messages
390 * dsq - sequence we're aligning
391 * i0 - first position in dsq to align (usually 1)
392 * j0 - final position in dsq to align (usually sq->n)
393 * pass_idx - pipeline pass index
394 * size_limit - max allowed size of an HB mx, in Mb
395 * doing_search - TRUE if we're going to use these HMM bands for search, not alignment
396 * do_sample - TRUE if bands will eventually be used for sampling a parsetree
397 * do_post - TRUE if bands will eventually be used for posterior alignment
398 * do_iterate - TRUE to attempt to iteratively tighten bands until matrix is small enough
399 * maxtau - max value allowed for cm->tau
400 * xtau - we multiply tau by this at each iteration (must be > 1.1)
401 * ret_Mb - RETURN: required Mb for HB mx for cm->tau upon exit.
402 *
403 * Returns: <eslOK> on success.
404 * <eslERANGE> if required matrix size is > <size_limit>,
405 * for cm->tau = maxtau.
406 * A different error code upon an error, errbuf is filled.
407 */
408 int
409 cp9_IterateSeq2Bands(CM_t *cm, char *errbuf, ESL_DSQ *dsq, int64_t i0, int64_t j0, int pass_idx, float size_limit, int doing_search, int do_sample, int do_post, int do_iterate, double maxtau, float *ret_Mb)
410 {
411 int status;
412 int do_trunc = cm_pli_PassAllowsTruncation(pass_idx);
413 /* sizes of matrices, note enforce hbmx_Mb < size_limit, not tot_Mb < size_limit */
414 float cp9mx_Mb = 0.; /* approximate size in Mb required for CP9 matrices */
415 float hbmx_Mb; /* approximate size in Mb required for HMM banded matrix */
416 float tot_Mb; /* cp9mx_Mb + hbmx_Mb */
417 int tau_at_limit = FALSE;
418 int thresh1_at_limit = (do_trunc) ? FALSE : TRUE;
419 int thresh2_at_limit = (do_trunc) ? FALSE : TRUE;
420
421 while(1) {
422 if((status = cp9_Seq2Bands(cm, errbuf, cm->cp9_mx, cm->cp9_bmx, cm->cp9_bmx, dsq, i0, j0, cm->cp9b, doing_search, pass_idx, 0)) != eslOK) goto ERROR;
423 if(doing_search) {
424 if(do_trunc) { if((status = cm_tr_hb_mx_SizeNeeded(cm, errbuf, cm->cp9b, j0-i0+1, NULL, NULL, NULL, NULL, &hbmx_Mb)) != eslOK) goto ERROR; }
425 else { if((status = cm_hb_mx_SizeNeeded (cm, errbuf, cm->cp9b, j0-i0+1, NULL, &hbmx_Mb)) != eslOK) goto ERROR; }
426 }
427 else {
428 if(do_trunc) { status = cm_TrAlignSizeNeededHB(cm, errbuf, j0-i0+1, size_limit, do_sample, do_post, NULL, NULL, NULL, &cp9mx_Mb, &hbmx_Mb, &tot_Mb); }
429 else { status = cm_AlignSizeNeededHB (cm, errbuf, j0-i0+1, size_limit, do_sample, do_post, NULL, NULL, NULL, &cp9mx_Mb, &hbmx_Mb, &tot_Mb); }
430 if(status != eslOK && status != eslERANGE) return status;
431 }
432 /*printf("cm->tau: %10.2g thresh1: %4.2f thresh2: %4.2f mxsize: %.2f\n", cm->tau, cm->cp9b->thresh1, cm->cp9b->thresh2, hbmx_Mb);*/
433 /* check if we can stop iterating, 4 ways we can
434 * case 1: matrix is now smaller than our limit.
435 * case 2: do_iterate == FALSE
436 * case 3: do_trunc == FALSE && tau has reached its limit
437 * case 4: do_trunc == TRUE && tau, thresh1 and thresh have all reached their limits
438 */
439 if(hbmx_Mb < size_limit) {
440 break; /* our matrix will be small enough, break out of while(1) */
441 }
442 if(! do_iterate) {
443 break; /* do_iterate is FALSE */
444 }
445 if(tau_at_limit && thresh1_at_limit && thresh2_at_limit) { /* if do_trunc is FALSE, thresh{1,2}_at_limit were init'ed as TRUE */
446 break; /* tau, thresh1 and thresh2 have all reached their limits, break out of while (1) */
447 }
448 if(! tau_at_limit) {
449 cm->tau *= TAU_MULTIPLIER;
450 if(cm->tau >= maxtau) { cm->tau = maxtau; tau_at_limit = TRUE; }
451 }
452 if(! thresh1_at_limit) {
453 cm->cp9b->thresh1 += DELTA_CP9BANDS_THRESH1;
454 if(cm->cp9b->thresh1 >= MAX_CP9BANDS_THRESH1) { cm->cp9b->thresh1 = MAX_CP9BANDS_THRESH1; thresh1_at_limit = TRUE; }
455 }
456 if(! thresh2_at_limit) {
457 cm->cp9b->thresh2 -= DELTA_CP9BANDS_THRESH2;
458 if(cm->cp9b->thresh2 <= MIN_CP9BANDS_THRESH2) { cm->cp9b->thresh2 = MIN_CP9BANDS_THRESH2; thresh2_at_limit = TRUE; }
459 }
460 }
461
462 if(ret_Mb != NULL) *ret_Mb = hbmx_Mb;
463
464 if(hbmx_Mb > size_limit) return eslERANGE;
465
466 return eslOK;
467
468 ERROR:
469 if(ret_Mb != NULL) *ret_Mb = 0.;
470 return status;
471 }
472
473 /* Function: cp9_Seq2Posteriors
474 * Date : EPN, Mon Jan 8 07:27:21 2007
475 *
476 * Purpose: Given a CM with precalc'ed CP9 HMM and CP9Map, and a sequence,
477 * run HMM Forward and Backward algorithms, and return a CP9 posterior
478 * matrix.
479 *
480 * Note: this function was never updated to handle
481 * truncated alignment (b/c it's no longer hooked up
482 * to any of the Infernal applications).
483 *
484 * Args: cm - the covariance model
485 * errbuf - char buffer for error messages
486 * fmx - CP9 dp matrix for Forward()
487 * bmx - CP9 dp matrix for Backward()
488 * pmx - CP9 dp matrix to fill with posteriors, can == bmx
489 * dsq - sequence in digitized form
490 * i0 - start of target subsequence (often 1, beginning of dsq)
491 * j0 - end of target subsequence (often L, end of dsq)
492 * debug_level - verbosity level for debugging printf()s
493 *
494 * Return: eslOK on success
495 */
496 int
497 cp9_Seq2Posteriors(CM_t *cm, char *errbuf, CP9_MX *fmx, CP9_MX *bmx, CP9_MX *pmx, ESL_DSQ *dsq, int i0, int j0, int debug_level)
498 {
499 int status;
500 float sc;
501 CP9_t *cp9 = NULL; /* ptr to cp9 HMM (this could be Lcp9, Rcp9, Tcp9 if we update this function to possibly handle truncated alignment) */
502
503 /* Contract checks */
504 if(dsq == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "in cp9_Seq2Posteriors(), dsq is NULL.");
505 if(cm->cp9 == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "in cp9_Seq2Posteriors, but cm->cp9 is NULL.\n");
506 if(cm->cp9map == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "in cp9_Seq2Posteriors, but cm->cp9map is NULL.\n");
507 if((cm->search_opts & CM_SEARCH_HMMALNBANDS) && (! (cm->search_opts & CM_SEARCH_HBANDED)))
508 ESL_FAIL(eslEINCOMPAT, errbuf, "in cp9_Seq2Posteriors, CM_SEARCH_HMMALNBANDS flag raised, but not CM_SEARCH_HBANDED flag, this doesn't make sense\n");
509
510 cp9 = cm->cp9;
511 if(cp9 == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_Seq2Posteriors, relevant cp9 is NULL.\n");
512
513 /* Step 1: Get HMM posteriors.*/
514 if((status = cp9_Forward(cp9, errbuf, fmx, dsq, i0, j0,
515 FALSE, /* don't use scanning Forward/Backward */
516 TRUE, /* we are going to use posteriors to align */
517 FALSE, /* don't be memory efficient */
518 NULL, NULL,
519 &sc)) != eslOK) return status;
520 if(debug_level > 0) printf("CP9 Forward score : %.4f\n", sc);
521 if((status = cp9_Backward(cp9, errbuf, bmx, dsq, i0, j0,
522 FALSE, /* don't use scanning Forward/Backward */
523 TRUE, /* we are going to use posteriors to align */
524 FALSE, /* don't be memory efficient */
525 NULL, NULL,
526 &sc)) != eslOK) return status;
527 if(debug_level > 0) printf("CP9 Backward score : %.4f\n", sc);
528
529 if(cm->align_opts & CM_ALIGN_CHECKFB) {
530 if((status = cp9_CheckFB(fmx, bmx, cp9, errbuf, sc, i0, j0, dsq)) != eslOK) return status;
531 printf("Forward/Backward matrices checked.\n");
532 }
533
534 /* Get posteriors */
535 cp9_Posterior(dsq, i0, j0, cp9, fmx, bmx, pmx, FALSE);
536
537 return eslOK;
538 }
539
540
541 /* Function: cp9_FB2HMMBands()
542 * Date: EPN, 04.03.06
543 * EPN, Mon Oct 15 18:20:42 2007 [updated/optimized]
544 *
545 * Purpose: Determine the band on all HMM states given a Forward and
546 * Backward matrix. Do this by calculating and summing log posterior
547 * probabilities that each state emitted/was visited at each posn,
548 * starting at the sequence ends, and creeping in, until the half the
549 * maximum allowable probability excluded is reached on each side.
550 *
551 * Args:
552 *
553 * CP9_t hmm the HMM
554 * errbuf char buffer for error messages
555 * CP9_MX fmx: forward DP matrix, already calc'ed
556 * CP9_MX bmx: backward DP matrix, already calc'ed
557 * CP9_MX pmx: DP matrix for posteriors, filled here, can == bmx
558 * dsq the digitized sequence
559 * CP9Bands_t cp9b CP9 bands data structure
560 * int i0 start of target subsequence (often 1, beginning of dsq)
561 * int j0 end of target subsequence (often L, end of dsq)
562 * int M number of nodes in HMM (num columns of pmx matrix)
563 * double p_thresh the probability mass we're requiring is within each band
564 * int did_fwd_scan TRUE if Forward was run in 'scan mode' (parses could start anywhere)
565 * int did_bck_scan TRUE if Backward was run in 'scan mode' (parses could end anywhere)
566 * int do_old_hmm2ij TRUE if we'll use old cp9_HMM2ijBands_OLD() function downstream
567 * int debug_level [0..3] tells the function what level of debugging print
568 * statements to print.
569 *
570 * Returns: eslOK on success;
571 */
572 int
573 cp9_FB2HMMBands(CP9_t *hmm, char *errbuf, ESL_DSQ *dsq, CP9_MX *fmx, CP9_MX *bmx, CP9_MX *pmx, CP9Bands_t *cp9b,
574 int i0, int j0, int M, double p_thresh, int did_fwd_scan, int did_bck_scan, int do_old_hmm2ij, int debug_level)
575 {
576 int status;
577 int k; /* counter over nodes of the model */
578 int L = j0-i0+1; /* length of sequence */
579 int thresh = Prob2Score(((1. - p_thresh)/2.), 1.); /* allowable prob mass excluded on each side */
580 int max; /* temporary max value */
581 int pnmax; /* position that gives max */
582
583 /* *_m = match, *_i = insert, *_d = delete */
584 int *kthresh_m, *kthresh_i, *kthresh_d; /* [0..k..hmm->M], individual thresholds for each state */
585 int *nset_m, *nset_i, *nset_d; /* [0..k..hmm->M], has minimum been set for this state? */
586 int *xset_m, *xset_i, *xset_d; /* [0..k..hmm->M], has maximum been set for this state? */
587 int *mass_m, *mass_i, *mass_d; /* [0..k..hmm->M], summed log prob of pmx->mx[i][k] from 0..k or k..L */
588 int i, ip; /* actual position and relative position in sequence, ip = i-i0+1 */
589 int sc; /* summed score of all parses (derived from backward matrix)
590 * if(cm->search_opts & CM_SEARCH_HMMALNBANDS) Forward and Backward
591 * were run in 'scan mode' where each residue can be begin/end of a parse,
592 * so we have to sum up parses that end at each posn,
593 * if ! (cm->search_opts & CM_SEARCH_HMMALNBANDS) we know we have
594 * to start at residue i0 and end at residue j0, so sc is simply bmx->mmx[0][0]
595 */
596 int hmm_is_localized; /* TRUE if HMM has local begins, ends or ELs on */
597 hmm_is_localized = ((hmm->flags & CPLAN9_LOCAL_BEGIN) || (hmm->flags & CPLAN9_LOCAL_END) || (hmm->flags & CPLAN9_EL)) ? TRUE : FALSE;
598
599 if(bmx != pmx) GrowCP9Matrix(pmx, errbuf, L, M, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
600
601 /* allocations and initializations */
602 ESL_ALLOC(nset_m, sizeof(int) * (M+1));
603 ESL_ALLOC(nset_i, sizeof(int) * (M+1));
604 ESL_ALLOC(nset_d, sizeof(int) * (M+1));
605 ESL_ALLOC(xset_m, sizeof(int) * (M+1));
606 ESL_ALLOC(xset_i, sizeof(int) * (M+1));
607 ESL_ALLOC(xset_d, sizeof(int) * (M+1));
608 ESL_ALLOC(mass_m, sizeof(int) * (M+1));
609 ESL_ALLOC(mass_i, sizeof(int) * (M+1));
610 ESL_ALLOC(mass_d, sizeof(int) * (M+1));
611 ESL_ALLOC(kthresh_m, sizeof(int) * (M+1));
612 ESL_ALLOC(kthresh_i, sizeof(int) * (M+1));
613 ESL_ALLOC(kthresh_d, sizeof(int) * (M+1));
614
615 esl_vec_ISet(mass_m, M+1, -INFTY);
616 esl_vec_ISet(mass_i, M+1, -INFTY);
617 esl_vec_ISet(mass_d, M+1, -INFTY);
618 esl_vec_ISet(nset_m, M+1, FALSE);
619 esl_vec_ISet(nset_i, M+1, FALSE);
620 esl_vec_ISet(nset_d, M+1, FALSE);
621 esl_vec_ISet(xset_m, M+1, FALSE);
622 esl_vec_ISet(xset_i, M+1, FALSE);
623 esl_vec_ISet(xset_d, M+1, FALSE);
624
625 if(did_fwd_scan) { /* parses were allowed to begin anywhere */
626 sc = -INFTY;
627 for (ip = 0; ip <= L; ip++) {
628 /*printf("bmx->mmx[i:%d][0]: %d\n", ip+i0-1, bmx->mmx[ip][0]); */
629 sc = ILogsum(sc, (bmx->mmx[ip][0]));
630 }
631 }
632 else sc = bmx->mmx[0][0]; /* Forward/Backward run in 'align mode' parses must start at i0, end at j0 */
633 /* sc is summed log prob of all possible parses of seq i0..j0 */
634
635 /* note boundary conditions, ip = 0, i = i0-1 */
636 pmx->mmx[0][0] = fmx->mmx[0][0] + bmx->mmx[0][0] - sc; /* fmx->mmx[0][0] is 0, bmx->mmx[0][0] is overall score */
637 pmx->imx[0][0] = -INFTY; /*need seq to get here*/
638 pmx->dmx[0][0] = -INFTY; /*D_0 does not exist*/
639 if((mass_m[0] = pmx->mmx[0][0]) > thresh) {
640 cp9b->pn_min_m[0] = ESL_MAX(i0-1, 0);
641 nset_m[0] = TRUE;
642 }
643 mass_i[0] = -INFTY; /* b/c pmx->imx[0][0] is -INFTY, set above */
644 mass_d[0] = -INFTY; /* b/c pmx->dmx[0][0] is -INFTY, set above */
645
646 for (k = 1; k <= M; k++) {
647 pmx->mmx[0][k] = -INFTY; /*need seq to get here*/
648 pmx->imx[0][k] = -INFTY; /*need seq to get here*/
649 pmx->dmx[0][k] = fmx->dmx[0][k] + bmx->dmx[0][k] - sc;
650 /* mass_m[k] doesn't change b/c pmx->mmx[0][k] is -INFTY */
651 /* mass_i[k] doesn't change b/c pmx->imx[0][k] is -INFTY */
652 if((mass_d[k] = pmx->dmx[0][k]) > thresh) {
653 cp9b->pn_min_d[k] = ESL_MAX(i0-1, 0);
654 nset_d[k] = TRUE;
655 }
656 }
657
658 /* Find minimum position in band for each state (M,I,D) of each node (0..M) */
659 for (ip = 1; ip <= L; ip++) /* ip is the relative position in the seq */
660 {
661 i = i0+ip-1; /* e.g. i is actual index in dsq, runs from i0 to j0 */
662 k = 0;
663 /* new block EPN, Wed Feb 13 11:58:52 2008 */
664 pmx->mmx[ip][0] = ESL_MAX(fmx->mmx[ip][0] + bmx->mmx[ip][0] - sc, -INFTY); /* M_0 doesn't emit */
665 if(! nset_m[0]) {
666 if((mass_m[0] = ILogsum(mass_m[0], pmx->mmx[ip][0])) > thresh) {
667 cp9b->pn_min_m[0] = i;
668 nset_m[0] = TRUE;
669 }
670 }
671 /* end of new block, old line used to be: pmx->mmx[ip][0] = -INFTY; */
672
673 pmx->imx[ip][0] = ESL_MAX(fmx->imx[ip][0] + bmx->imx[ip][0] - hmm->isc[dsq[i]][0] - sc, -INFTY);
674 /*hmm->isc[dsq[i]][k] will have been counted in both fmx->mmx and bmx->mmx*/
675 if(! nset_i[0]) {
676 if((mass_i[0] = ILogsum(mass_i[0], pmx->imx[ip][0])) > thresh) {
677 cp9b->pn_min_i[0] = i;
678 nset_i[0] = TRUE;
679 }
680 }
681 pmx->dmx[ip][0] = -INFTY; /* D_0 doesn't exist */
682
683 for(k = 1; k <= M; k++)
684 {
685 pmx->mmx[ip][k] = ESL_MAX(fmx->mmx[ip][k] + bmx->mmx[ip][k] - hmm->msc[dsq[i]][k] - sc, -INFTY);
686 /*hmm->msc[dsq[i]][k] will have been counted in both fmx->mmx and bmx->mmx*/
687 pmx->imx[ip][k] = ESL_MAX(fmx->imx[ip][k] + bmx->imx[ip][k] - hmm->isc[dsq[i]][k] - sc, -INFTY);
688 /*hmm->isc[dsq[i]][k] will have been counted in both fmx->mmx and bmx->mmx*/
689 pmx->dmx[ip][k] = ESL_MAX(fmx->dmx[ip][k] + bmx->dmx[ip][k] - sc, -INFTY);
690
691 if(! nset_m[k]) {
692 if((mass_m[k] = ILogsum(mass_m[k], pmx->mmx[ip][k])) > thresh) {
693 cp9b->pn_min_m[k] = i;
694 nset_m[k] = TRUE;
695 }
696 }
697 if(! nset_i[k]) {
698 if((mass_i[k] = ILogsum(mass_i[k], pmx->imx[ip][k])) > thresh) {
699 cp9b->pn_min_i[k] = i;
700 nset_i[k] = TRUE;
701 }
702 }
703 if(! nset_d[k]) {
704 if((mass_d[k] = ILogsum(mass_d[k], pmx->dmx[ip][k])) > thresh) {
705 cp9b->pn_min_d[k] = i;
706 nset_d[k] = TRUE;
707 }
708 }
709 }
710 }
711 esl_vec_ISet(mass_m, M+1, -INFTY);
712 esl_vec_ISet(mass_i, M+1, -INFTY);
713 esl_vec_ISet(mass_d, M+1, -INFTY);
714 /* Find maximum position in band for each state (M,I,D) of each node (0..M)
715 * by moving from L down to 1 */
716 for (ip = L; ip >= 1; ip--) /* ip is the relative position in the seq */
717 {
718 i = i0+ip-1; /* e.g. i is actual index in dsq, runs from i0 to j0 */
719 for(k = 0; k <= M; k++)
720 {
721 if(! xset_m[k]) {
722 if((mass_m[k] = ILogsum(mass_m[k], pmx->mmx[ip][k])) > thresh) {
723 cp9b->pn_max_m[k] = i;
724 xset_m[k] = TRUE;
725 }
726 }
727 if(! xset_i[k]) {
728 if((mass_i[k] = ILogsum(mass_i[k], pmx->imx[ip][k])) > thresh) {
729 cp9b->pn_max_i[k] = i;
730 xset_i[k] = TRUE;
731 }
732 }
733 if(! xset_d[k]) {
734 if((mass_d[k] = ILogsum(mass_d[k], pmx->dmx[ip][k])) > thresh) {
735 cp9b->pn_max_d[k] = i;
736 xset_d[k] = TRUE;
737 }
738 }
739 }
740 }
741 ip = 0;
742 i = i0-1;
743 /* note boundary conditions, ip = 0, i = i0-1 */
744 if(! xset_m[0]) {
745 if((mass_m[0] = ILogsum(mass_m[0], pmx->mmx[0][0])) > thresh) {
746 cp9b->pn_max_m[0] = ESL_MAX(i0-1, 0);
747 xset_m[0] = TRUE;
748 }
749 }
750 /* mass_i[0] is unchanged because b/c pmx->imx[0][0] is -INFTY, set above */
751 /* mass_d[0] is unchanged because b/c pmx->dmx[0][0] is -INFTY, set above */
752 for (k = 1; k <= M; k++) {
753 /* mass_m[k] doesn't change b/c pmx->mmx[0][k] is -INFTY */
754 /* mass_i[k] doesn't change b/c pmx->mmx[0][k] is -INFTY */
755 if(!xset_d[k]) {
756 if((mass_d[k] = ILogsum(mass_d[k], pmx->dmx[0][k])) > thresh) {
757 cp9b->pn_max_d[k] = ESL_MAX(i0-1, 0);
758 xset_d[k] = TRUE;
759 }
760 }
761 }
762
763 if(! do_old_hmm2ij) {
764 /* new way as of EPN, Sun Jan 27 08:48:34 2008 */
765 /* Some states may not have had their min/max set. This occurs if the entire
766 * state is outside the band (i.e. the summed probablity the state is entered for ANY i
767 * is less than our threshold. Current strategy in this situation is to set the
768 * pn_min_* and pn_max_* values as special flags, (-2) so the function that
769 * uses them to derive i and j bands knows this is the case and handles it
770 * accordingly.
771 */
772 int mset;
773 int dset;
774 for(k = 0; k <= M; k++)
775 {
776 mset = dset = TRUE;
777 /* theoretically either nset_*[k] and xset_*[k] should be either both TRUE or both
778 * FALSE, but I'm slightly worried about rare precision issues, so we check if one
779 * or the other is unset, and if so, we set both to argmax position */
780 if(((! nset_m[k])) || (! xset_m[k]) || (cp9b->pn_max_m[k] < cp9b->pn_min_m[k])) {
781 cp9b->pn_min_m[k] = cp9b->pn_max_m[k] = -1;
782 mset = FALSE;
783 }
784 if(((! nset_i[k])) || (! xset_i[k]) || (cp9b->pn_max_i[k] < cp9b->pn_min_i[k])) {
785 cp9b->pn_min_i[k] = cp9b->pn_max_i[k] = -1;
786 }
787 if(((! nset_d[k])) || (! xset_d[k]) || (cp9b->pn_max_d[k] < cp9b->pn_min_d[k])) {
788 cp9b->pn_min_d[k] = cp9b->pn_max_d[k] = -1;
789 dset = FALSE;
790 }
791 if((!hmm_is_localized && !did_fwd_scan && !did_bck_scan) && (mset == FALSE && dset == FALSE)) ESL_FAIL(eslEINCONCEIVABLE, errbuf, "node: %d match nor delete HMM state bands were set in non-localized, non-scanning HMM, lower tau (should be << 0.5).\n", k);
792 }
793 }
794 else {
795 /* old way, prior to Sun Jan 27 08:46:16 2008 */
796 /* Some states may not have had their min/max set. This occurs if the entire
797 * state is outside the band (i.e. the summed probablity the state is entered for ANY i
798 * is less than our threshold. Current strategy in this situation is to set the
799 * band to width 1 of the most likely position for that state, but to do that we
800 * need to find what the most likely posn is, we could do this in the loop above,
801 * but this is a rare situation, and so that turns out to be wasteful.
802 *
803 * Note: the off-by-one issue mentioned below is dealt with differently with the
804 * new code, when we're setting i and j CM bands using the HMM bands.
805 */
806 for(k = 0; k <= M; k++)
807 {
808 /* comment *: off-by-one issue with non-emitters (includes all D states and M_0):
809 * pn_min_d[k] = i, means posn i was last residue emitted
810 * prior to entering node k's delete state. However, for a CM,
811 * if a delete states sub-parsetree is bounded by i' and j', then
812 * positions i' and j' HAVE YET TO BE EMITTED.
813 * For M_0, so we don't have to check each node to see if k == 0, we
814 * do the off-by-one correction at the end of the function.
815 */
816 if(k != 0) {
817 if(cp9b->pn_min_d[k] != -1) cp9b->pn_min_d[k]++;
818 if(cp9b->pn_min_d[k] != -1) cp9b->pn_max_d[k]++;
819 }
820 /* theoretically either nset_*[k] and xset_*[k] should be either both TRUE or both
821 * FALSE, but I'm slightly worried about rare precision issues, so we check if one
822 * or the other is unset, and if so, we set both to argmax position */
823 if((! nset_m[k]) || (! xset_m[k])) {
824 max = pmx->mmx[0][k];
825 for(ip = 1; ip <= L; ip++)
826 if(pmx->mmx[ip][k] > max) { pnmax = i0+ip-1; max = pmx->mmx[ip][k]; } /* i = i0+ip-1 */
827 cp9b->pn_min_m[k] = cp9b->pn_max_m[k] = pnmax;
828 }
829 if((! nset_i[k]) || (! xset_i[k])) {
830 max = pmx->imx[0][k];
831 for(ip = 1; ip <= L; ip++)
832 if(pmx->imx[ip][k] > max) { pnmax = i0+ip-1; max = pmx->imx[ip][k]; } /* i = i0+ip-1 */
833 cp9b->pn_min_i[k] = cp9b->pn_max_i[k] = pnmax;
834 }
835 if((! nset_d[k]) || (! xset_d[k])) {
836 max = pmx->dmx[0][k];
837 for(ip = 1; ip <= L; ip++)
838 if(pmx->dmx[ip][k] > max) { pnmax = i0+ip-1; max = pmx->dmx[ip][k]; } /* i = i0+ip-1 */
839 cp9b->pn_min_d[k] = cp9b->pn_max_d[k] = pnmax;
840 }
841 }
842 cp9b->pn_min_m[0]++; /* non emitter */
843 cp9b->pn_max_m[0]++; /* non emitter */
844 }
845
846 cp9b->pn_min_d[0] = -1; /* D_0 doesn't exist */
847 cp9b->pn_max_d[0] = -1; /* D_0 doesn't exist */
848
849 if(debug_level > 0) cp9_DebugPrintHMMBands(stdout, j0, cp9b, (1.-p_thresh), 1);
850
851 free(mass_m);
852 free(mass_i);
853 free(mass_d);
854 free(nset_m);
855 free(nset_i);
856 free(nset_d);
857 free(xset_m);
858 free(xset_i);
859 free(xset_d);
860 free(kthresh_m);
861 free(kthresh_i);
862 free(kthresh_d);
863
864 return eslOK;
865
866 ERROR:
867 ESL_FAIL(status, errbuf, "Memory allocation error.\n");
868 }
869
870
871 /* Function: cp9_FB2HMMBandsWithSums()
872 * Date: EPN, Wed Oct 17 10:22:44 2007
873 *
874 * Purpose: Determine the band on all HMM states given a Forward and
875 * Backward matrix. Do this by calculating and summing log posterior
876 * probabilities that each state emitted/was visited at each posn,
877 * starting at the sequence ends, and creeping in, until the half the
878 * maximum allowable probability excluded is reached on each side.
879 *
880 * CP9_t hmm the HMM
881 * errbuf char buffer for error messages
882 * CP9_MX fmx: forward DP matrix, already calc'ed
883 * CP9_MX bmx: backward DP matrix, already calc'ed
884 * CP9_MX pmx: DP matrix for posteriors, filled here, can == bmx
885 * dsq the digitized sequence
886 * CP9Bands_t cp9b CP9 bands data structure
887 * int i0 start of target subsequence (often 1, beginning of dsq)
888 * int j0 end of target subsequence (often L, end of dsq)
889 * int M number of nodes in HMM (num columns of post matrix)
890 * double p_thresh the probability mass we're requiring is within each band
891 * int did_fwd_scan TRUE if Forward was run in 'scan mode' (parses could start at any posn)
892 * int did_bck_scan TRUE if Backward was run in 'scan mode' (parses could end at any posn)
893 * int do_old_hmm2ij TRUE if we'll use old cp9_HMM2ijBands_OLD() function downstream
894 * int debug_level [0..3] tells the function what level of debugging print
895 * statements to print.
896 *
897 * Returns: eslOK on success;
898 */
899 int
900 cp9_FB2HMMBandsWithSums(CP9_t *hmm, char *errbuf, ESL_DSQ *dsq, CP9_MX *fmx, CP9_MX *bmx, CP9_MX *pmx, CP9Bands_t *cp9b,
901 int i0, int j0, int M, double p_thresh, int did_fwd_scan, int did_bck_scan, int do_old_hmm2ij, int debug_level)
902 {
903 int status;
904 int k; /* counter over nodes of the model */
905 int L = j0-i0+1; /* length of sequence */
906 int thresh = Prob2Score(((1. - p_thresh)/2.), 1.); /* allowable prob mass excluded on each side */
907
908 /* *_m = match, *_i = insert, *_d = delete */
909 int i, ip; /* actual position and relative position in sequence, ip = i-i0+1 */
910 int *kthresh_m, *kthresh_i, *kthresh_d; /* [0..k..hmm->M], individual thresholds for each state */
911 int *nset_m, *nset_i, *nset_d; /* [0..k..hmm->M], has minimum been set for this state? */
912 int *xset_m, *xset_i, *xset_d; /* [0..k..hmm->M], has maximum been set for this state? */
913 int *mass_m, *mass_i, *mass_d; /* [0..k..hmm->M], summed log prob of pmx->mx[i][k] from 0..k or k..L */
914
915 if(bmx != pmx) GrowCP9Matrix(pmx, errbuf, L, M, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
916
917 /* allocations and initializations */
918 ESL_ALLOC(nset_m, sizeof(int) * (M+1));
919 ESL_ALLOC(nset_i, sizeof(int) * (M+1));
920 ESL_ALLOC(nset_d, sizeof(int) * (M+1));
921 ESL_ALLOC(xset_m, sizeof(int) * (M+1));
922 ESL_ALLOC(xset_i, sizeof(int) * (M+1));
923 ESL_ALLOC(xset_d, sizeof(int) * (M+1));
924 ESL_ALLOC(mass_m, sizeof(int) * (M+1));
925 ESL_ALLOC(mass_i, sizeof(int) * (M+1));
926 ESL_ALLOC(mass_d, sizeof(int) * (M+1));
927 ESL_ALLOC(kthresh_m, sizeof(int) * (M+1));
928 ESL_ALLOC(kthresh_i, sizeof(int) * (M+1));
929 ESL_ALLOC(kthresh_d, sizeof(int) * (M+1));
930
931 esl_vec_ISet(mass_m, M+1, -INFTY);
932 esl_vec_ISet(mass_i, M+1, -INFTY);
933 esl_vec_ISet(mass_d, M+1, -INFTY);
934 esl_vec_ISet(nset_m, M+1, FALSE);
935 esl_vec_ISet(nset_i, M+1, FALSE);
936 esl_vec_ISet(nset_d, M+1, FALSE);
937 esl_vec_ISet(xset_m, M+1, FALSE);
938 esl_vec_ISet(xset_i, M+1, FALSE);
939 esl_vec_ISet(xset_d, M+1, FALSE);
940
941 /* get the posterior matrix first, we need it b/c each state will have a different log prob threshold */
942 cp9_Posterior(dsq, i0, j0, hmm, fmx, bmx, pmx, did_fwd_scan);
943
944 /* fill ipost_sums in cp9bands data structure */
945 cp9_IFillPostSums(pmx, cp9b, i0, j0);
946
947 /* set state dependent cutoff thresholds for log prob mass we need on each side (this is unique to
948 * WithSums() function */
949 for(k = 0; k <= M; k++) {
950 kthresh_m[k] = thresh + cp9b->isum_pn_m[k];
951 kthresh_i[k] = thresh + cp9b->isum_pn_i[k];
952 kthresh_d[k] = thresh + cp9b->isum_pn_d[k];
953 }
954
955 /* Find minimum position in band for each state (M,I,D) of each node (0..M) */
956 for (ip = 0; ip <= L; ip++) /* ip is the relative position in the seq */
957 {
958 i = i0+ip-1; /* e.g. i is actual index in dsq, runs from i0 to j0 */
959 for(k = 0; k <= M; k++)
960 {
961 if(! nset_m[k]) {
962 if((mass_m[k] = ILogsum(mass_m[k], pmx->mmx[ip][k])) > kthresh_m[k]) {
963 cp9b->pn_min_m[k] = i;
964 nset_m[k] = TRUE;
965 }
966 }
967 if(! nset_i[k]) {
968 if((mass_i[k] = ILogsum(mass_i[k], pmx->imx[ip][k])) > kthresh_i[k]) {
969 cp9b->pn_min_i[k] = i;
970 nset_i[k] = TRUE;
971 }
972 }
973 if(! nset_d[k]) {
974 if((mass_d[k] = ILogsum(mass_d[k], pmx->dmx[ip][k])) > kthresh_d[k]) {
975 cp9b->pn_min_d[k] = i;
976 nset_d[k] = TRUE;
977 }
978 }
979 }
980 }
981 /* Find maximum position in band for each state (M,I,D) of each node (0..M)
982 * by moving from L down to 0 */
983 /* reset mass_* arrays */
984 esl_vec_ISet(mass_m, M+1, -INFTY);
985 esl_vec_ISet(mass_i, M+1, -INFTY);
986 esl_vec_ISet(mass_d, M+1, -INFTY);
987 for (ip = L; ip >= 0; ip--) /* ip is the relative position in the seq */
988 {
989 i = i0+ip-1; /* e.g. i is actual index in dsq, runs from i0 to j0 */
990 for(k = 0; k <= M; k++)
991 {
992 if(! xset_m[k]) {
993 if((mass_m[k] = ILogsum(mass_m[k], pmx->mmx[ip][k])) > kthresh_m[k]) {
994 cp9b->pn_max_m[k] = i;
995 xset_m[k] = TRUE;
996 }
997 }
998 if(! xset_i[k]) {
999 if((mass_i[k] = ILogsum(mass_i[k], pmx->imx[ip][k])) > kthresh_i[k]) {
1000 cp9b->pn_max_i[k] = i;
1001 xset_i[k] = TRUE;
1002 }
1003 }
1004 if(! xset_d[k]) {
1005 if((mass_d[k] = ILogsum(mass_d[k], pmx->dmx[ip][k])) > kthresh_d[k]) {
1006 cp9b->pn_max_d[k] = i;
1007 xset_d[k] = TRUE;
1008 }
1009 }
1010 }
1011 }
1012
1013 if(do_old_hmm2ij) { /* we have to correct for an off-by-one to be consistent with the 'old' way code */
1014 for(k = 1; k <= M; k++)
1015 {
1016 /* comment *: off-by-one issue with non-emitters (includes all D states and M_0):
1017 * pn_min_d[k] = i, means posn i was last residue emitted
1018 * prior to entering node k's delete state. However, for a CM,
1019 * if a delete states sub-parsetree is bounded by i' and j', then
1020 * positions i' and j' HAVE YET TO BE EMITTED.
1021 * For M_0, so we don't have to check each node to see if k == 0, we
1022 * do the off-by-one correction at the end of the function.
1023 */
1024 if(cp9b->pn_min_d[k] != -1) cp9b->pn_min_d[k]++;
1025 if(cp9b->pn_min_d[k] != -1) cp9b->pn_max_d[k]++;
1026 }
1027 cp9b->pn_min_m[0]++; /* non-emitter */
1028 cp9b->pn_max_m[0]++; /* non-emitter */
1029 }
1030
1031 #if eslDEBUGLEVEL >= 1
1032 /* all states should have their min/max set because we've normalized the probability
1033 * of entering each state to 1.0, so we assert this to be true */
1034 ESL_DASSERT1((nset_m[0]));
1035 ESL_DASSERT1((nset_i[0]));
1036 ESL_DASSERT1((xset_m[0]));
1037 ESL_DASSERT1((xset_i[0]));
1038 /* D_0 state does not exist */
1039 for(k = 1; k <= M; k++)
1040 {
1041 ESL_DASSERT1((nset_m[k]));
1042 ESL_DASSERT1((nset_i[k]));
1043 ESL_DASSERT1((nset_d[k]));
1044 ESL_DASSERT1((xset_m[k]));
1045 ESL_DASSERT1((xset_i[k]));
1046 ESL_DASSERT1((xset_d[k]));
1047 }
1048 #endif
1049
1050 cp9b->pn_min_d[0] = -1; /* D_0 doesn't exist */
1051 cp9b->pn_max_d[0] = -1; /* D_0 doesn't exist */
1052
1053 if(debug_level > 0) cp9_DebugPrintHMMBands(stdout, j0, cp9b, (1.-p_thresh), 1);
1054
1055 free(mass_m);
1056 free(mass_i);
1057 free(mass_d);
1058 free(nset_m);
1059 free(nset_i);
1060 free(nset_d);
1061 free(xset_m);
1062 free(xset_i);
1063 free(xset_d);
1064 free(kthresh_m);
1065 free(kthresh_i);
1066 free(kthresh_d);
1067
1068 return eslOK;
1069
1070 ERROR:
1071 ESL_FAIL(status, errbuf, "Memory allocation error.\n");
1072 }
1073
1074 /* Function: cp9_Posterior()
1075 * based on Ian Holmes' hmmer/src/postprob.c::P7EmitterPosterior()
1076 *
1077 * Purpose: Combines Forward and Backward matrices into a posterior
1078 * probability matrix. For emitters (match and inserts) the
1079 * entries in row i of this matrix are the logs of the posterior
1080 * probabilities of each state emitting symbol i of the sequence.
1081 * For non-emitters the entries in row i of this matrix are the
1082 * logs of the posterior probabilities of each state being 'visited'
1083 * when the last emitted residue in the parse was symbol i of the
1084 * sequence.
1085 * The last point distinguishes this function from P7EmitterPosterior()
1086 * which set all posterior values for for non-emitting states to -INFTY.
1087 * The caller must allocate space for the matrix, although the
1088 * backward matrix can be used instead (overwriting it will not
1089 * compromise the algorithm).
1090 *
1091 * if(did_fwd_scan == TRUE) forward was run in scan mode, which allowed
1092 * parses to start at any position of sequence, this changes how
1093 * we calculate summed prob of all parses (calculation of 'sc', see code).
1094 *
1095 * Args: dsq - sequence in digitized form
1096 * i0 - start of target subsequence (often 1, beginning of dsq)
1097 * j0 - end of target subsequence (often L, end of dsq)
1098 * hmm - the model
1099 * forward - pre-calculated forward matrix
1100 * backward - pre-calculated backward matrix
1101 * mx - pre-allocated dynamic programming matrix
1102 * did_fwd_scan - TRUE if Forward was run in 'scan' mode, which means
1103 * parses can start at any position of the sequence
1104 *
1105 * Return: void
1106 */
1107 void
1108 cp9_Posterior(ESL_DSQ *dsq, int i0, int j0, CP9_t *hmm, CP9_MX *fmx, CP9_MX *bmx, CP9_MX *mx, int did_fwd_scan)
1109 {
1110 if(dsq == NULL) cm_Fail("in cp9_posterior(), dsq is NULL.");
1111
1112 int i;
1113 int k;
1114 int sc;
1115 int L; /* subsequence length */
1116 int ip; /* i': relative position in the subsequence */
1117 /*float temp_sc;*/
1118
1119 L = j0-i0+1; /* the length of the subsequence */
1120
1121 if(did_fwd_scan) { /* parses could start/stop anywhere */
1122 sc = -INFTY;
1123 for (ip = 0; ip <= L; ip++) {
1124 /*printf("bmx->mmx[i:%d][0]: %d\n", i, bmx->mmx[ip][0]);*/
1125 sc = ILogsum(sc, (bmx->mmx[ip][0]));
1126 }
1127 } /* parses must start/stop at (i = i0)/(j = j0) */
1128 else sc = bmx->mmx[0][0];
1129
1130 /* note boundary conditions, case by case by case... */
1131 mx->mmx[0][0] = fmx->mmx[0][0] + bmx->mmx[0][0] - sc; /* fmx->mmx[0][0] is 0, bmx->mmx[1][0] is overall score */
1132 mx->imx[0][0] = -INFTY; /*need seq to get here*/
1133 mx->dmx[0][0] = -INFTY; /*D_0 does not exist*/
1134 for (k = 1; k <= hmm->M; k++) {
1135 mx->mmx[0][k] = -INFTY; /*need seq to get here*/
1136 mx->imx[0][k] = -INFTY; /*need seq to get here*/
1137 mx->dmx[0][k] = fmx->dmx[0][k] + bmx->dmx[0][k] - sc;
1138 }
1139
1140 for (ip = 1; ip <= L; ip++) /* ip is the relative position in the seq */
1141 {
1142 i = i0+ip-1; /* e.g. i is actual index in dsq, runs from i0 to j0 */
1143 mx->mmx[ip][0] = -INFTY; /*M_0 does not emit*/
1144 mx->imx[ip][0] = fmx->imx[ip][0] + bmx->imx[ip][0] - hmm->isc[dsq[i]][0] - sc;
1145 /*hmm->isc[dsq[i]][0] will have been counted in both fmx->imx and bmx->imx*/
1146 mx->dmx[ip][0] = -INFTY; /*D_0 does not exist*/
1147
1148 /*printf("fmx->mmx[ip:%d][0]: %d\n bmx->mmx[ip:%d][0]: %d\n", ip, fmx->mmx[ip][0], ip, bmx->mmx[ip][0]);
1149 printf("fmx->imx[ip:%d][0]: %d\n bmx->imx[ip:%d][0]: %d\n", ip, fmx->imx[ip][0], ip, bmx->imx[ip][0]);
1150 printf("fmx->dmx[ip:%d][0]: %d\n bmx->dmx[ip:%d][0]: %d\n", ip, fmx->dmx[ip][0], ip, bmx->dmx[ip][0]);*/
1151 for (k = 1; k <= hmm->M; k++)
1152 {
1153 mx->mmx[ip][k] = ESL_MAX(fmx->mmx[ip][k] + bmx->mmx[ip][k] - hmm->msc[dsq[i]][k] - sc, -INFTY);
1154 /*hmm->msc[dsq[i]][k] will have been counted in both fmx->mmx and bmx->mmx*/
1155 mx->imx[ip][k] = ESL_MAX(fmx->imx[ip][k] + bmx->imx[ip][k] - hmm->isc[dsq[i]][k] - sc, -INFTY);
1156 /*hmm->isc[dsq[i]][k] will have been counted in both fmx->imx and bmx->imx*/
1157 mx->dmx[ip][k] = ESL_MAX(fmx->dmx[ip][k] + bmx->dmx[ip][k] - sc, -INFTY);
1158 /*printf("fmx->mmx[ip:%d][%d]: %d\n bmx->mmx[ip:%d][%d]: %d\n", ip, k, fmx->mmx[ip][k], ip, k, bmx->mmx[ip][k]);
1159 printf("fmx->imx[ip:%d][%d]: %d\n bmx->imx[ip:%d][%d]: %d\n", ip, k, fmx->imx[ip][k], ip, k, bmx->imx[ip][k]);
1160 printf("fmx->dmx[ip:%d][%d]: %d\n bmx->dmx[ip:%d][%d]: %d\n\n", ip, k, fmx->dmx[ip][k], ip, k, bmx->dmx[ip][k]);*/
1161 }
1162 }
1163
1164 /*
1165 float temp_sc;
1166 for(i = 0; i <= L; i++)
1167 {
1168 for(k = 0; k <= hmm->M; k++)
1169 {
1170 temp_sc = Score2Prob(mx->mmx[i][k], 1.);
1171 if(temp_sc > .0001)
1172 printf("mx->mmx[%3d][%3d]: %9d | %8f\n", i, k, mx->mmx[i][k], temp_sc);
1173 temp_sc = Score2Prob(mx->imx[i][k], 1.);
1174 if(temp_sc > .0001)
1175 printf("mx->imx[%3d][%3d]: %9d | %8f\n", i, k, mx->imx[i][k], temp_sc);
1176 temp_sc = Score2Prob(mx->dmx[i][k], 1.);
1177 if(temp_sc > .0001)
1178 printf("mx->dmx[%3d][%3d]: %9d | %8f\n", i, k, mx->dmx[i][k], temp_sc);
1179 }
1180 }*/
1181 }
1182
1183 /*****************************************************************************
1184 * EPN 03.23.06
1185 * Function: cp9_IFillPostSums()
1186 * based on: ifill_post_sums_del() (deprecated) 11.23.05
1187 *
1188 * Purpose: Given a posterior matrix post, where post->mmx[i][k]
1189 * is the log odds score of the probability that
1190 * match state k emitted position i of the sequence,
1191 * sum the log probabilities that each state emitted
1192 * each position. Do this for inserts, matches, and
1193 * and deletes.
1194 *
1195 * arguments:
1196 * cp9_dpmatrix_s *post dpmatrix_s posterior matrix, xmx, mmx, imx, dmx
1197 * 2D int arrays. [0.1..N][0.1..M]
1198 * CP9Bands_t *cp9b - the cp9 bands data structure
1199 * int i0 start of target subsequence (often 1, beginning of dsq)
1200 * int j0 end of target subsequence (often L, end of dsq)
1201 *****************************************************************************/
1202 void
1203 cp9_IFillPostSums(CP9_MX *post, CP9Bands_t *cp9b, int i0, int j0)
1204 {
1205 int i; /* counter over positions of the sequence */
1206 int k; /* counter over nodes of the model */
1207 int L; /* subsequence length */
1208 int M; /* consensus length of cp9 */
1209 M = cp9b->hmm_M;
1210 L = j0-i0+1; /* the length of the subsequence */
1211
1212 /* step through each node, fill the post sum structures */
1213 for(k = 0; k <= M; k++)
1214 {
1215 cp9b->isum_pn_m[k] = -INFTY;
1216 cp9b->isum_pn_i[k] = -INFTY;
1217 cp9b->isum_pn_d[k] = -INFTY;
1218 for(i = 0; i <= L; i++) {
1219 cp9b->isum_pn_m[k] = ILogsum(cp9b->isum_pn_m[k], post->mmx[i][k]);
1220 cp9b->isum_pn_i[k] = ILogsum(cp9b->isum_pn_i[k], post->imx[i][k]);
1221 cp9b->isum_pn_d[k] = ILogsum(cp9b->isum_pn_d[k], post->dmx[i][k]);
1222 }
1223 }
1224 }
1225
1226 /* Function: cp9_ValidateBands()
1227 * Incept: EPN, Wed Nov 14 15:49:08 2007
1228 * Purpose: Validate the info in CP9Bands_t data structure is internally
1229 * consistent.
1230 *
1231 * Args: cm the cm
1232 * errbuf char buffer for error message
1233 * cp9b the CP9 bands object
1234 * i0 first residue we can possibly allow as valid j
1235 * j0 final residue we can possibly allow as valid j
1236 *
1237 * Returns: eslOK, or, if error, other status code and filled errbuf
1238 */
1239 int
1240 cp9_ValidateBands(CM_t *cm, char *errbuf, CP9Bands_t *cp9b, int i0, int j0, int do_trunc)
1241 {
1242 int v; /* counter over states of the CM */
1243 int jp; /* counter over valid j's, but offset. jp+jmin[v] = actual j */
1244 int sd; /* minimum d allowed for a state, ex: MP_st = 2, ML_st = 1. etc. */
1245 int max_sdl_sdr; /* maximum of StateLeftDelta, StateRightDelta for a state */
1246 int dn; /* max_sdl_sdr if do_trunc, else sd */
1247 int hd_needed;
1248 int j;
1249
1250
1251 if(cm->M != cp9b->cm_M) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), cm->M != cp9b->cm_M\n");
1252 if(cm->clen != cp9b->hmm_M) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), cm->clen != cp9b->hmm_M\n");
1253
1254 hd_needed = 0;
1255 for(v = 0; v < cp9b->cm_M; v++) {
1256 hd_needed += cp9b->jmax[v] - cp9b->jmin[v] + 1;
1257 }
1258 if(hd_needed != cp9b->hd_needed) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), cp9b->hd_needed inconsistent.");
1259
1260 for(v = 0; v < cm->M; v++) {
1261 assert((cp9b->imin[v] == -1 && cp9b->imax[v] == -2) || (cp9b->imin[v] >= 0 && cp9b->imax[v] >= 0));
1262 assert((cp9b->jmin[v] == -1 && cp9b->jmax[v] == -2) || (cp9b->jmin[v] >= 0 && cp9b->jmax[v] >= 0));
1263 }
1264
1265 for(v = 0; v < cm->M; v++) {
1266 sd = StateDelta(cm->sttype[v]);
1267 max_sdl_sdr = ESL_MAX(StateLeftDelta(cm->sttype[v]), StateRightDelta(cm->sttype[v]));
1268 dn = do_trunc ? max_sdl_sdr : sd;
1269 /* if (do_trunc) d can be 1 for MP states, this is why we use dn
1270 * here. Note: d can't be 0 for ML/IL in R mode, MR/IR in L
1271 * mode even though you might think it could be. We'll always do
1272 * a truncated begin with d=1 for L,R marginal alignments. */
1273 if(cm->sttype[v] == E_st) {
1274 for(jp = 0; jp <= (cp9b->jmax[v]-cp9b->jmin[v]); jp++) {
1275 if(cp9b->hdmin[v][jp] != 0) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), cp9b->hdmin for E state is inconsistent.");
1276 if(cp9b->hdmax[v][jp] != 0) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), cp9b->hdmin for E state is inconsistent.");
1277 }
1278 }
1279 else {
1280 if(cp9b->jmin[v] != -1) {
1281 for(jp = 0; jp <= (cp9b->jmax[v]-cp9b->jmin[v]); jp++) {
1282 j = jp+cp9b->jmin[v];
1283 if(cp9b->hdmin[v][jp] == -1) {
1284 if(cp9b->hdmax[v][jp] != -2) { ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), cp9b->hdmin is -1 for state %d, j: %d, but hdmax is not -2 (it's %d).", v, j, cp9b->hdmax[v][jp]); }
1285 }
1286 else {
1287 if(cp9b->hdmin[v][jp] != ESL_MAX((j - cp9b->imax[v] + 1), dn)) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), cp9b->hdmin %d (dn: %d) for state %d, j: %d imax[v]: %d is inconsistent.", cp9b->hdmin[v][jp], dn, v, j, cp9b->imax[v]);
1288 if(cp9b->hdmax[v][jp] != ESL_MAX((j - cp9b->imin[v] + 1), dn)) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), cp9b->hdmax %d (dn: %d) for state %d, j: %d imin[v]: %d is inconsistent.", cp9b->hdmax[v][jp], dn, v, j, cp9b->imin[v]);
1289 }
1290 }
1291 }
1292 }
1293 /* get rid of StateIsDetached once old band construction method is deprecated */
1294 if(cp9b->imin[v] == -1 && !StateIsDetached(cm, v)) { /* ensure all unreachable states have 0 width bands */
1295 if(cp9b->imax[v] != -2) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), v: %d imin[v] == -1, but imax[v] != -2 but rather %d\n", v, cp9b->imax[v]);
1296 if(cp9b->jmin[v] != -1) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), v: %d imin[v] == -1, but jmin[v] != -1 but rather %d\n", v, cp9b->jmin[v]);
1297 if(cp9b->jmax[v] != -2) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), v: %d imin[v] == -1, but jmax[v] != -2 but rather %d\n", v, cp9b->jmax[v]);
1298 }
1299 else if(!StateIsDetached(cm, v)){
1300 if(cp9b->imax[v] == -2) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), v: %d imin[v] != -1, but imax[v] == -2!\n", v);
1301 if(cp9b->jmin[v] == -1) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), v: %d imin[v] != -1, but jmin[v] == -1!\n", v);
1302 if(cp9b->jmax[v] == -2) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), v: %d imin[v] != -1, but jmax[v] == -2!\n", v);
1303 }
1304
1305 if(i0 == j0 && cm->sttype[v] == MP_st) { /* special case, MPs are impossible in this case */
1306 if(cp9b->imin[v] != -1) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), exceedingly rare case, i0==j0==%d v: %d is MP but imin[v]: %d != -1\n", i0, v, cp9b->imin[v]);
1307 if(cp9b->imax[v] != -2) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), exceedingly rare case, i0==j0==%d v: %d is MP but imax[v]: %d != -2\n", i0, v, cp9b->imax[v]);
1308 if(cp9b->jmin[v] != -1) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), exceedingly rare case, i0==j0==%d v: %d is MP but jmin[v]: %d != -1\n", i0, v, cp9b->jmin[v]);
1309 if(cp9b->jmax[v] != -2) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), exceedingly rare case, i0==j0==%d v: %d is MP but jmax[v]: %d != -2\n", i0, v, cp9b->jmax[v]);
1310 }
1311 else {
1312 if(cp9b->jmin[v] != -1) {
1313 for(j = cp9b->jmin[v]; j <= cp9b->jmax[v]; j++) {
1314 if(j < (i0-1)) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), j: %d outside i0-1:%d..j0:%d is within v's j band: jmin[%d]: %d jmax[%d]: %d\n", j, i0-1, j0, v, cp9b->jmin[v], v, cp9b->jmax[v]);
1315 if(j > j0) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), j: %d outside i0-1:%d..j0:%d is within v's j band: jmin[%d]: %d jmax[%d]: %d\n", j, i0-1, j0, v, cp9b->jmin[v], v, cp9b->jmax[v]);
1316 if(cp9b->hdmin[v][(j-cp9b->jmin[v])] == -1) {
1317 if(cp9b->hdmax[v][(j-cp9b->jmin[v])] != -2) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), v: %d j: %d hdmin[v][jp_v:%d] == -1, but hdmax[v][jp_v:%d] != -2 (it's %d)\n", v, j, (j-cp9b->jmin[v]), (j-cp9b->jmin[v]), cp9b->hdmax[v][(j-cp9b->jmin[v])]);
1318 }
1319 else {
1320 if(cp9b->hdmin[v][(j-cp9b->jmin[v])] < dn) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), v: %d j: %d hdmin[v][jp_v:%d] : %d less than StateDelta for v: %d\n", v, j, (j-cp9b->jmin[v]), cp9b->hdmin[v][(j-cp9b->jmin[v])], dn);
1321 if(cp9b->hdmax[v][(j-cp9b->jmin[v])] < dn) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), v: %d j: %d hdmax[v][jp_v:%d] : %d less than StateDelta for v: %d\n", v, j, (j-cp9b->jmin[v]), cp9b->hdmax[v][(j-cp9b->jmin[v])], dn);
1322 }
1323 }
1324 if(cp9b->jmax[v] > cp9b->jmax[0]) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), jmax[v:%d]:%d > jmax[0]:%d.", v, cp9b->jmax[v], cp9b->jmax[0]);
1325 if(cp9b->imin[v] < cp9b->imin[0]) ESL_FAIL(eslEINVAL, errbuf, "cp9_ValidateBands(), imin[v:%d]:%d < imin[0]:%d, i0:%d j0:%d jmin[v]:%d jmax[v]:%d jmin[0]:%d jmax[0]:%d imax[v]:%d", v, cp9b->imin[v], cp9b->imin[0], i0, j0, cp9b->jmin[v], cp9b->jmax[v], cp9b->jmin[0], cp9b->jmax[0], cp9b->imax[v]);
1326 }
1327 }
1328 }
1329 return eslOK;
1330 }
1331
1332 /*
1333 * Function: cp9_GrowHDBands()
1334 *
1335 * Incept: EPN, Thu Oct 25 13:24:29 2007
1336 * Purpose: Rearrange CP9 hdmin and hdmax pointers for a new sequence
1337 * based on j bands (jmin and jmax). If the currently allocated
1338 * size for hdmin, hdmax is not big enough, reallocate them.
1339 *
1340 * Args:
1341 * CP9Bands_t cp9b the CP9 Bands object.
1342 * errbuf char buffer for error messages
1343 *
1344 * Returns: eslOK on success, eslEMEM if memory allocation error
1345 */
1346 int
1347 cp9_GrowHDBands(CP9Bands_t *cp9b, char *errbuf)
1348 {
1349 int status;
1350 int v;
1351 int cur_size = 0;
1352 int jbw;
1353
1354 /* count size we need for hdmin/hdmax given current jmin, jmax */
1355 cp9b->hd_needed = 0; /* we'll rewrite this */
1356 for(v = 0; v < cp9b->cm_M; v++) {
1357 cp9b->hd_needed += cp9b->jmax[v] - cp9b->jmin[v] + 1;
1358 /* printf("hd needed v: %4d bw: %4d total: %5d\n", v, cp9b->jmax[v] - cp9b->jmin[v] + 1, cp9b->hd_needed); */
1359 }
1360 if(cp9b->hd_alloced < cp9b->hd_needed) {
1361 void *tmp;
1362 if(cp9b->hdmin_mem == NULL) ESL_ALLOC(cp9b->hdmin_mem, sizeof(int) * cp9b->hd_needed);
1363 else ESL_RALLOC(cp9b->hdmin_mem, tmp, sizeof(int) * cp9b->hd_needed);
1364 if(cp9b->hdmax_mem == NULL) ESL_ALLOC(cp9b->hdmax_mem, sizeof(int) * cp9b->hd_needed);
1365 else ESL_RALLOC(cp9b->hdmax_mem, tmp, sizeof(int) * cp9b->hd_needed);
1366 }
1367
1368 /* set pointers */
1369 cur_size = 0;
1370 for(v = 0; v < cp9b->cm_M; v++) {
1371 cp9b->hdmin[v] = cp9b->hdmin_mem + cur_size;
1372 cp9b->hdmax[v] = cp9b->hdmax_mem + cur_size;
1373 jbw = cp9b->jmax[v] - cp9b->jmin[v] + 1;
1374 assert(jbw >= 0);
1375 ESL_DASSERT1((jbw >= 0));
1376 cur_size += jbw;
1377 }
1378 cp9b->hd_alloced = cur_size;
1379 ESL_DASSERT1((cp9b->hd_alloced == cp9b->hd_needed));
1380 return eslOK;
1381
1382 ERROR:
1383 ESL_FAIL(status, errbuf, "Memory allocation error.");
1384 }
1385
1386
1387 /*****************************************************************************
1388 * EPN 11.03.05
1389 * Function: ij2d_bands()
1390 *
1391 * Purpose: Determine the band for each cm state v on d (the band on the
1392 * length of the subsequence emitted from the subtree rooted
1393 * at state v). These are easily calculated given the bands on i
1394 * and j.
1395 *
1396 * arguments:
1397 *
1398 * CM_t *cm the CM
1399 * int W length of sequence we're aligning
1400 * int *imin imin[v] = first position in band on i for state v
1401 * int *imax imax[v] = last position in band on i for state v
1402 * int *jmin jmin[v] = first position in band on j for state v
1403 * int *jmax jmax[v] = last position in band on j for state v
1404 * int **hdmin hdmin[v][jp] = first position in band on d for state v
1405 * and j position: j = jp+jmin[v].
1406 * Filled in this function.
1407 * int **hdmax hdmax[v][jp] = last position in band on d for state v
1408 * and j position: j = jp+jmin[v].
1409 * Filled in this function.
1410 * int do_trunc TRUE if we'll use these bands in a truncated version of CYK/Inside/Outside
1411 * int debug_level [0..3] tells the function what level of debugging print
1412 * statements to print.
1413 *****************************************************************************/
1414 void
1415 ij2d_bands(CM_t *cm, int W, int *imin, int *imax, int *jmin, int *jmax,
1416 int **hdmin, int **hdmax, int do_trunc, int debug_level)
1417 {
1418 int v; /* counter over states of the CM */
1419 int jp; /* counter over valid j's, but offset. jp+jmin[v] = actual j */
1420 int j; /* actual j */
1421 int sd; /* minimum d allowed for a state, ex: MP_st = 2, ML_st = 1. etc. */
1422 int max_sdl_sdr; /* maximum of StateLeftDelta, StateRightDelta for a state */
1423 int dn; /* max_sdl_sdr if do_trunc, else sd */
1424 int hdn, hdx; /* temporary hdmin/hdmax */
1425 for(v = 0; v < cm->M; v++) {
1426 if(cm->sttype[v] == E_st) {
1427 for(jp = 0; jp <= (jmax[v]-jmin[v]); jp++) {
1428 hdmin[v][jp] = 0;
1429 hdmax[v][jp] = 0;
1430 }
1431 }
1432 else {
1433 sd = StateDelta(cm->sttype[v]);
1434 max_sdl_sdr = ESL_MAX(StateLeftDelta(cm->sttype[v]), StateRightDelta(cm->sttype[v]));
1435 dn = do_trunc ? max_sdl_sdr : sd;
1436 /* if (do_trunc) d can be 1 for MP states, this is why we use dn
1437 * here. Note: d can't be 0 for ML/IL in R mode, MR/IR in L
1438 * mode even though you might think it could be. We'll always do
1439 * a truncated begin with d=1 for L,R marginal alignments. */
1440
1441 for(jp = 0; jp <= (jmax[v]-jmin[v]); jp++) {
1442 j = jp+jmin[v];
1443 hdn = j-imax[v]+1;
1444 hdx = j-imin[v]+1;
1445 if(hdx < dn) {
1446 hdmin[v][jp] = -1;
1447 hdmax[v][jp] = -2;
1448 }
1449 else {
1450 hdmin[v][jp] = ESL_MAX(hdn, dn);
1451 hdmax[v][jp] = hdx;
1452 }
1453 /* printf("hd[%d][j=%d]: min: %d | max: %d\n", v, (jp+jmin[v]), hdmin[v][jp], hdmax[v][jp]); */
1454 }
1455 }
1456 }
1457 }
1458
1459 /* Function: cp9_HMM2ijBands()
1460 * Synopsis: Derive bands on i and j for all CM states given HMM bands.
1461 * Incept: EPN, Thu Feb 7 12:05:01 2008
1462 *
1463 * Purpose: Given HMM bands, determine the corresponding bands on the
1464 * CM. Both for i: the left border of the subsequence emitted
1465 * from the subtree rooted at v, the band is imin[v]..imax[v]
1466 * inclusive. And also for j: the right border of the subseq
1467 * emitted from the subtree rooted at v, the band is
1468 * jmin[v]..jmax[v] inclusive.
1469 *
1470 * This is done by first enforcing that the HMM bands allow
1471 * at least 1 possible HMM parse. A valid parse given the
1472 * HMM bands is not guaranteed, although it's nearly always
1473 * likely even for relatively high values of tau (the
1474 * probability mass allowed outside the band for each state,
1475 * relatively high is 0.01). With very tight bands, for
1476 * example from a tau of 0.49, the chance that all parses
1477 * are impossible given the bands is much more likely (especially
1478 * with non-homologous sequences). *If* the HMM bands exclude
1479 * all possible HMM parses, they are expanded in a greedy,
1480 * stupid way to allow at least 1 parse (we could be smarter,
1481 * but this case only arises for impractical tau values, in
1482 * fact I only implemented it to verify the rest of the HMM
1483 * banding implementation is robust, and will always work
1484 * for tau values up to 0.5).
1485 *
1486 * Once we know an HMM parse is possible given the HMM bands,
1487 * we also know if we impose those exact bands on the CM
1488 * we will also have a valid CM parse, b/c there is a 1:1
1489 * mapping between HMM parses and CM parsetrees. So, we
1490 * impose the HMM bands onto the CM to get the i and j
1491 * bands using a stack and mapping 'explicit' bands,
1492 * the i or j bands of CM states that map to an HMM
1493 * state (for example the i band of MATL_ML states,
1494 * or the j bands of MATR_MR states). The other bands
1495 * that are not explicitly set (ex: the j band of a
1496 * MATL_ML state and the i band of a MATR_MR state), are
1497 * implicitly set based on the explicit ones.
1498 *
1499 * Note: This code is ugly, even more than usual for me.
1500 * There's a plethora of special cases, which are maddening
1501 * during development/debugging. The code starts out simple
1502 * and balloons as you add code to handle the special cases.
1503 * [EPN, Thu Feb 7 12:17:53 2008].
1504 *
1505 * Args: <cm> - the model
1506 * <errbuf> - for returning error messages
1507 * <cp9> - the CP9 HMM used to determine the bands
1508 * <cp9b> - the bands data structure
1509 * <cp9map> - map between the CM and HMM
1510 * <i0> - first position in the sequence we're considering
1511 * <j0> - final position in the sequence we're considering
1512 * <doing_search> - TRUE if we're searching the target sequence, not aligning it,
1513 * relevant b/c iff we're aligning the parsetree *must* span i0..j0
1514 * <do_trunc> - TRUE if we're going to use these bands for truncated CYK/Inside/Outside
1515 * <debug_level> - verbosity level for debuggint printf() statements
1516 *
1517 * Returns: <eslOK> on success.
1518 *
1519 * Throws: <eslEINCOMPAT> on contract violation
1520 * <eslEMEM> on memory error
1521 */
1522 int
1523 cp9_HMM2ijBands(CM_t *cm, char *errbuf, CP9_t *cp9, CP9Bands_t *cp9b, CP9Map_t *cp9map, int i0, int j0, int doing_search, int do_trunc, int debug_level)
1524 {
1525
1526 int status;
1527 int v;
1528
1529 /* ptrs to cp9b data, for convenience */
1530 int *imin; /* imin[v] = first position in band on i for state v to be filled in this function. [1..M] */
1531 int *imax; /* imax[v] = last position in band on i for state v to be filled in this function. [1..M] */
1532 int *jmin; /* jmin[v] = first position in band on j for state v to be filled in this function. [1..M] */
1533 int *jmax; /* jmax[v] = last position in band on j for state v to be filled in this function. [1..M] */
1534
1535 int nd; /* counter over CM nodes. */
1536 int y; /* counters over children states */
1537 int hmm_M; /* number of nodes in the HMM */
1538 ESL_STACK *nd_pda; /* used to traverse the CM from left to right in consensus positions, cpos = 0..clen */
1539 ESL_STACK *lpos_pda; /* used to store lpos for BIF nodes */
1540 int on_right; /* TRUE if we're on the right for current node during our CM traversal */
1541 int w; /* a state index */
1542 int lpos, rpos; /* left/right border of subtree for current node */
1543 /*int k;*/ /* counter of HMM nodes, for debugging print statements, currently not used */
1544 int hmm_is_localized; /* TRUE if HMM has local begins, ends or ELs on */
1545 int cm_is_fully_localized; /* TRUE if CM has local begins and ends on */
1546
1547 /* r_* arrays, these are filled in HMMBandsEnforceValidParse(), they are the band on 'reachable'
1548 * residues for each HMM state as we move from left to right through the HMM.
1549 * For example, r_mn[k] = 3, r_mx[k] = 5, means that for all possible HMM parses within the bands
1550 * in the cp9b pn_* arrays that reach the match state of node k, the residue emitted by that match
1551 * must be either 3, 4, or 5.
1552 */
1553 int *r_mn; /* [0..k..hmm_M] minimal residue position for which we can reach M_k (match state of node k) */
1554 int *r_mx; /* [0..k..hmm_M] maximal residue position for which we can reach M_k */
1555 int *r_in; /* [0..k..hmm_M] minimal residue position for which we can reach I_k (insert state of node k) */
1556 int *r_ix; /* [0..k..hmm_M] maximal residue position for which we can reach I_k */
1557 int *r_dn; /* [0..k..hmm_M] minimal residue position for which we can reach D_k (delete state of node k) */
1558 int *r_dx; /* [0..k..hmm_M] maximal residue position for which we can reach D_k */
1559 int *r_nn_i; /* [0..k..hmm_M] minimal residue position for which we can reach node k (any of M_k, I_k, D_k) */
1560 int *r_nx_i; /* [0..k..hmm_M] maximal residue position for which we can reach node k (any of M_k, I_k, D_k) */
1561 int *r_nn_j; /* [0..k..hmm_M] minimal residue position for which we can reach node k (any of M_k, I_k, D_k) */
1562 int *r_nx_j; /* [0..k..hmm_M] maximal residue position for which we can reach node k (any of M_k, I_k, D_k) */
1563 /* r_nn_i and r_nx_i are used when setting i bands, and r_nn_j and r_nx_j are used when setting j bands .
1564 * the values can differ vecause of an off-by-one issue with the non-emitting (delete and M_0) states of the HMM:
1565 * pn_min_d[k] = i, means posn i was last residue emitted prior to entering node k's delete state. However, for a CM,
1566 * if a delete states sub-parsetree is bounded by i' and j', this means positions i' and j' HAVE YET TO BE EMITTED.
1567 * For i states this means we have to add 1 to the delete band positions, but for j states we do not, the off-by-one
1568 * is taken care of because the HMM is moving left to right, while j positions move right to left (confusing as hell,
1569 * bad explanation, i know... write out an example, its the only way to get it).
1570 */
1571
1572 /* Contract checks */
1573 if (cp9b == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_HMM2ijBands(), cp9b is NULL.\n");
1574 if(i0 < 1) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_HMM2ijBands(), i0 < 1: %d\n", i0);
1575 if(j0 < 1) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_HMM2ijBands(), j0 < 1: %d\n", j0);
1576 if(j0 < i0) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_HMM2ijBands(), i0 (%d) < j0 (%d)\n", i0, j0);
1577 hmm_is_localized = ((cp9->flags & CPLAN9_LOCAL_BEGIN) || (cp9->flags & CPLAN9_LOCAL_END) || (cp9->flags & CPLAN9_EL)) ? TRUE : FALSE;
1578 cm_is_fully_localized = ((cm->flags & CMH_LOCAL_BEGIN) && (cm->flags & CMH_LOCAL_END)) ? TRUE : FALSE;
1579
1580 /* ptrs to cp9b arrays, for convenience */
1581 imin = cp9b->imin;
1582 imax = cp9b->imax;
1583 jmin = cp9b->jmin;
1584 jmax = cp9b->jmax;
1585 hmm_M = cp9b->hmm_M;
1586 /* Initialize all bands to -1 */
1587 esl_vec_ISet(imin, cm->M, -1);
1588 esl_vec_ISet(imax, cm->M, -2);
1589 esl_vec_ISet(jmin, cm->M, -1);
1590 esl_vec_ISet(jmax, cm->M, -2);
1591
1592 /* Step 1: Check for valid HMM parse within the HMM bands, if there isn't one messily expand the bands so that there is one */
1593 if((status = HMMBandsEnforceValidParse(cp9, cp9b, cp9map, errbuf, i0, j0, doing_search,
1594 &r_mn, &r_mx, &r_in, &r_ix, &r_dn, &r_dx, &r_nn_i, &r_nx_i, &r_nn_j, &r_nx_j)) != eslOK) return status;
1595
1596 /* debugging printf block */
1597 /*
1598 for(k = 0; k <= cp9b->hmm_M;k ++) {
1599 printf("k: %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d\n", k, r_mn[k], r_mx[k], r_in[k], r_ix[k], r_dn[k], r_dx[k], r_nn_i[k], r_nx_i[k], r_nn_j[k], r_nx_j[k]);
1600 }
1601 cp9_DebugPrintHMMBands(stdout, j0, cp9b, cm->tau, 1);
1602 */
1603
1604 /* Step 2: Traverse the CM from left to right in consensus position coordinates. Fill in the
1605 * i and j bands (imin, imax, jmin, jmax) for all states as we go. The CM is traversed
1606 * using a stack, each node is visited twice (this is based on Sean's cleaner:
1607 * display.c::CreateEmitMap(). The first time a node <nd> is visited we're 'on the left'
1608 * and then we push it back to the stack, and visit it again 'on the right' later. We
1609 * are moving around the perimeter of the guide tree, stepping one position at a time
1610 * in the consensus sequence coordinates, from left to right. We mainly set bands
1611 * when we're 'on the right', with the exception of Left emitting states, which are
1612 * set when we're on the HMM. All emitting states and delete states v have either
1613 * i, or j or both bands that can be set 'explicitly' based on the HMM bands for
1614 * the HMM state that maps to v. For example we can set the i bands for MATL_ML
1615 * states, and the j bands for MATR_MR states. All other bands (and both i
1616 * and j bands for S states, B states, E states) are set 'implicitly based on the
1617 * explicit bands, and the r_* data structures we filled in HMMBandsEnforceValidParse().
1618 * The goal was to make this function as clean and simple as possible, and although
1619 * it doesn't look it, this is as good as I can get it. There are many special
1620 * cases that make an elegant implementation beyond me.
1621 */
1622 if(! doing_search) {
1623 assert(r_mn[0] == (i0-1));
1624 if(!hmm_is_localized) assert(r_mx[hmm_M] == j0 || r_ix[hmm_M] == j0 || r_dx[hmm_M] == j0);
1625 }
1626 nd = 0;
1627 lpos = 0;
1628 rpos = 0;
1629
1630 if ((nd_pda = esl_stack_ICreate()) == NULL) goto ERROR;
1631 if ((lpos_pda = esl_stack_ICreate()) == NULL) goto ERROR;
1632 if ((status = esl_stack_IPush(nd_pda, 0)) != eslOK) goto ERROR; /* 0 = left side. 1 would = right side. */
1633 if ((status = esl_stack_IPush(nd_pda, nd)) != eslOK) goto ERROR;
1634 while (esl_stack_IPop(nd_pda, &nd) != eslEOD)
1635 {
1636 esl_stack_IPop(nd_pda, &on_right);
1637 if (on_right) {
1638 switch(cm->ndtype[nd]) { /* this is a massive switch, we set i and j bands for almost all
1639 * states here when we're on the right (sole exceptions are i bands for
1640 * MATP_nd states (except MATP_IR), and MATL_nd states) */
1641
1642 case BIF_nd: /* special case, set i bands based on left child, j bands based on right child */
1643 v = cm->nodemap[nd];
1644 w = cm->cfirst[v]; /* BEGL_S */
1645 y = cm->cnum[v]; /* BEGR_S */
1646
1647 /* set v's i band based on left child w, and v's j band based on right child y */
1648 imin[v] = (imin[w] != -1) ? imin[w] : imin[y]; /* if imin[w] == imin[y] == -1, then imin[v] will be set as -1 */
1649 imax[v] = (imax[w] != -2) ? imax[w] : imax[y]; /* if imax[w] == imax[y] == -2, then imax[v] will be set as -2 */
1650 jmin[v] = (jmin[y] != -1) ? jmin[y] : jmin[w]; /* if jmin[y] == jmin[w] == -1, then jmin[v] will be set as -1 */
1651 jmax[v] = (jmax[y] != -2) ? jmax[y] : jmax[w]; /* if jmax[y] == jmax[w] == -2, then jmax[v] will be set as -2 */
1652
1653 if(! do_trunc) {
1654 /* check for possibility that either child is not reachable, will only possibly happen with local on */
1655 if(imin[w] == -1 || jmin[w] == -1 || imin[y] == -1 || jmin[y] == -1 ||
1656 imax[w] == -2 || imax[w] == -2 || jmax[y] == -2 || jmax[y] == -2) {
1657 /* either the left child, or right child is not reachable, make them both unreachable as well as the BIF state */
1658 imin[v] = imin[w] = imin[y] = jmin[v] = jmin[w] = jmin[y] = -1;
1659 imax[v] = imax[w] = imax[y] = jmax[v] = jmax[w] = jmax[y] = -2;
1660 /* also make the BEGR_IL unreachable */
1661 imin[y+1] = jmin[y+1] = -1;
1662 imax[y+1] = jmax[y+1] = -2;
1663 }
1664 }
1665 break;
1666
1667 case MATP_nd:
1668 lpos = cp9map->nd2lpos[nd];
1669 rpos = cp9map->nd2rpos[nd];
1670
1671 v = cm->nodemap[nd]; /* v is MATP_MP */
1672 jmin[v] = r_mn[rpos];
1673 jmax[v] = r_mx[rpos];
1674
1675 v++; /* v is MATP_ML */
1676 jmin[v] = r_dn[rpos];
1677 jmax[v] = r_dx[rpos];
1678
1679 v++; /* v is MATP_MR */
1680 jmin[v] = r_mn[rpos];
1681 jmax[v] = r_mx[rpos];
1682
1683 v++; /* v is MATP_D */
1684 jmin[v] = r_dn[rpos];
1685 jmax[v] = r_dx[rpos];
1686
1687 v++; /* v is MATP_IL */
1688 jmin[v] = r_nn_j[rpos-1];
1689 jmax[v] = r_nx_j[rpos-1];
1690
1691 v++; /* v is MATP_IR */
1692 jmin[v] = r_in[rpos-1];
1693 jmax[v] = r_ix[rpos-1];
1694 imin[v] = r_nn_i[lpos+1]; /* look at band on lpos *+1* b/c we enter MATP_IR AFTER the MATP_MP, MATP_MR, MATP_ML, or MATP_IL insert (if any) */
1695 imax[v] = r_nx_i[lpos+1]; /* look at band on lpos *+1* b/c we enter MATP_IR AFTER the MATP_MP, MATP_MR, MATP_ML, or MATP_IL insert (if any) */
1696 ESL_DASSERT1(((lpos+1) <= cm->clen)); /* note: we know lpos+1 <= cm->clen b/c we're in a MATP node, and the ccol the right half of the node maps to
1697 * must be to the right of the ccol the left half of the node maps to */
1698 if(imin[v] == 0) { cm_Fail("v: %d lpos: %d\n", v, lpos); }
1699 break; /* case MATP_nd */
1700
1701 case MATL_nd: /* i bands were set when we were on the left, non-right emitter, set implicit j bands */
1702 lpos = cp9map->nd2lpos[nd];
1703
1704 v = cm->nodemap[nd]; /* v is MATL_ML */
1705 jmin[v] = r_nn_j[rpos];
1706 jmax[v] = r_nx_j[rpos];
1707
1708 v++; /* v is MATL_D, the MATL_ML and MATL_IL concerns don't apply, D's don't emit */
1709 jmin[v] = r_nn_j[rpos];
1710 jmax[v] = r_nx_j[rpos];
1711
1712 v++; /* v is MATL_IL */
1713 jmin[v] = r_nn_j[rpos];
1714 jmax[v] = r_nx_j[rpos];
1715 break;
1716
1717 case MATR_nd: /* set j bands explicitly from HMM bands, i bands implicitly */
1718 rpos = cp9map->nd2rpos[nd];
1719 v = cm->nodemap[nd]; /* v is MATR_MR */
1720 jmin[v] = r_mn[rpos];
1721 jmax[v] = r_mx[rpos];
1722 imin[v] = r_nn_i[lpos];
1723 imax[v] = r_nx_i[lpos];
1724
1725 v++; /* v is MATR_D */
1726 jmin[v] = r_dn[rpos];
1727 jmax[v] = r_dx[rpos];
1728 imin[v] = r_nn_i[lpos];
1729 imax[v] = r_nx_i[lpos];
1730
1731 v++; /* v is MATR_IR */
1732 jmin[v] = r_in[rpos-1];
1733 jmax[v] = r_ix[rpos-1];
1734 imin[v] = r_nn_i[lpos];
1735 imax[v] = r_nx_i[lpos];
1736 break;
1737
1738 case BEGL_nd:
1739 case BEGR_nd: /* set i and j bands implicitly, except for BEGR_IL, whose i bands are set explicitly based on HMM */
1740 v = cm->nodemap[nd]; /* set i and j band for BEG{L,R}_S based on children */
1741 imin[v] = jmin[v] = INT_MAX;
1742 imax[v] = jmax[v] = INT_MIN;
1743 for(y = cm->cfirst[v]; y < cm->cfirst[v]+cm->cnum[v]; y++) {
1744 /* if y is reachable, make sure we can get there from v */
1745 if(imin[y] != -1) {
1746 imin[v] = ESL_MIN(imin[v], imin[y]);
1747 imax[v] = ESL_MAX(imax[v], imax[y]);
1748 }
1749 if(jmin[y] != -1) {
1750 jmin[v] = ESL_MIN(jmin[v], jmin[y]);
1751 jmax[v] = ESL_MAX(jmax[v], jmax[y]);
1752 }
1753 }
1754 if(imin[v] == INT_MAX) {
1755 imin[v] = -1;
1756 imax[v] = -2;
1757 }
1758 if(jmin[v] == INT_MAX) {
1759 jmin[v] = -1;
1760 jmax[v] = -2;
1761 }
1762
1763 /* set BEGR_IL's i and j band */
1764 if(cm->ndtype[nd] == BEGR_nd) {
1765 v++;
1766 imin[v] = r_in[lpos-1]; /* BEGR_IL emits before lpos */
1767 imax[v] = r_ix[lpos-1];
1768 if(imin[v-1] != -1 && imin[v] != -1) { /* if BEGR_S and BEGR_IL is reachable */
1769 imin[v-1] = ESL_MIN(imin[v-1], imin[v]); /* expand BEGR_S so it can reach BEGR_IL */
1770 jmin[v] = (jmin[v-1] == -1) ? -1 : ESL_MAX(jmin[v-1], i0); /* can't get to a BEGR_IL without emitting at least i0 */
1771 jmax[v] = jmax[v-1];
1772 }
1773 else {
1774 imin[v] = jmin[v] = -1;
1775 imax[v] = jmax[v] = -2;
1776 }
1777 esl_stack_IPop(lpos_pda, &lpos); /* pop the remembered lpos from our sister BEGL_nd to use for parent BIF_nd and above */
1778 }
1779 else { /* BEGL_nd */
1780 if ((status = esl_stack_IPush(lpos_pda, lpos)) != eslOK) goto ERROR;
1781 lpos = rpos+1; /* next node we pop from stack will be our BEGR sister, on the right, switch lpos to rpos+1 */
1782 }
1783 break;
1784
1785 case END_nd:
1786 v = cm->nodemap[nd]; /* v is END_E */
1787 imin[v] = r_nn_i[lpos];
1788 imax[v] = (r_nx_i[lpos] == -2) ? r_nx_i[lpos] : ESL_MIN(r_nx_i[lpos]+1, j0+1); /* +1 is for StateDelta */
1789 if(r_in[lpos] != -1) { /* we could come from an IR above us (tricky case) */
1790 imin[v] = ESL_MIN(imin[v], ESL_MAX(r_in[lpos] - 1, i0));
1791 imax[v] = ESL_MAX(imax[v], ESL_MAX(r_ix[lpos] - 1, i0));
1792 }
1793 rpos = lpos;
1794 if(imin[v] != -1) {
1795 jmin[v] = imin[v]-1; /* E must emit d = 0 residues, so j ==i-1 */
1796 jmax[v] = imax[v]-1; /* E must emit d = 0 residues, so j ==i-1 */
1797 }
1798 break;
1799
1800 case ROOT_nd: /* ROOT is a special case, set i and j bands */
1801 /* lpos == 1 and rpos == hmm_M */
1802 assert(lpos == 1);
1803 assert(rpos == hmm_M);
1804 v = cm->nodemap[nd]; /* v is ROOT_S */
1805 imin[v] = r_nn_i[1];
1806 imax[v] = r_nx_i[1];
1807 jmin[v] = r_nn_j[hmm_M];
1808 jmax[v] = r_nx_j[hmm_M];
1809
1810 v++; /* v is ROOT_IL */
1811 imin[v] = r_in[0]; /* ROOT_IL maps to HMM insert state of HMM node 0 */
1812 imax[v] = r_ix[0]; /* ROOT_IL maps to HMM insert state of HMM node 0 */
1813 /* ROOT_IL's j bands will be same as ROOT_S's, after ensuring state delta of 1 is respected */
1814 jmin[v] = (r_nn_j[hmm_M] == -1) ? -1 : ESL_MAX(r_nn_j[hmm_M], i0); /* can't get to ROOT_IL without emitting at least i0 */
1815 jmax[v] = r_nx_j[hmm_M];
1816 if(r_in[hmm_M] != -1) {
1817 jmin[v] = ESL_MIN(jmin[v], r_in[hmm_M]);
1818 jmax[v] = ESL_MIN(jmax[v], r_ix[hmm_M]);
1819 }
1820
1821 v++; /* v is ROOT_IR */
1822 if(r_in[hmm_M] != -1) { /* if r_in[hmm_M] == -1, this state is unreachable */
1823 imin[v] = r_nn_i[1]; /* HMM state M_0 is silent */
1824 imax[v] = r_nx_i[1]; /* HMM state M_0 is silent */
1825 if(imin[v-1] != -1) {
1826 imin[v] = ESL_MIN(imin[v], imin[v-1]+1);
1827 imax[v] = ESL_MAX(imax[v], imax[v-1]+1);
1828 }
1829 jmin[v] = r_in[hmm_M]; /* ROOT_IR maps to HMM insert state of HMM node hmm_M */
1830 jmax[v] = r_ix[hmm_M]; /* ROOT_IR maps to HMM insert state of HMM node hmm_M */
1831 }
1832 break;
1833 } /* end of switch(cm->ndtype[nd]) */
1834 } /* end of if(on_right) */
1835
1836 else { /* on left */
1837 /* set i bands for MATP_nd, MATL_nd only */
1838 switch(cm->ndtype[nd]) {
1839 case MATP_nd:
1840 lpos = cp9map->nd2lpos[nd];
1841 v = cm->nodemap[nd]; /* v is MATP_MP */
1842 imin[v] = r_mn[lpos];
1843 imax[v] = r_mx[lpos];
1844 v++; /* v is MATP_ML */
1845 imin[v] = r_mn[lpos];
1846 imax[v] = r_mx[lpos];
1847 v++; /* v is MATP_MR */
1848 imin[v] = r_dn[lpos] == -1 ? -1 : r_dn[lpos]+1;
1849 imax[v] = r_dx[lpos] == -2 ? -2 : r_dx[lpos]+1;
1850 v++; /* v is MATP_D */
1851 imin[v] = r_dn[lpos] == -1 ? -1 : r_dn[lpos]+1;
1852 imax[v] = r_dx[lpos] == -2 ? -2 : r_dx[lpos]+1;
1853 v++; /* v is MATP_IL */
1854 imin[v] = r_in[lpos];
1855 imax[v] = r_ix[lpos];
1856 /* we deal with setting imin/imax for MATP_IR when we're on the right */
1857 break;
1858
1859 case MATL_nd:
1860 lpos = cp9map->nd2lpos[nd];
1861 v = cm->nodemap[nd]; /* v is MATL_ML */
1862 imin[v] = r_mn[lpos];
1863 imax[v] = r_mx[lpos];
1864 v++; /* v is MATL_D */
1865 imin[v] = r_dn[lpos] == -1 ? -1 : r_dn[lpos]+1;
1866 imax[v] = r_dx[lpos] == -2 ? -2 : r_dx[lpos]+1;
1867 v++; /* v is MATL_IL */
1868 imin[v] = r_in[lpos];
1869 imax[v] = r_ix[lpos];
1870 break;
1871 } /* end of switch(cm->ndtype[nd]) */
1872
1873 if(cm->ndtype[nd] == BIF_nd) {
1874 /* push the BIF back on for its right side */
1875 if ((status = esl_stack_IPush(nd_pda, 1)) != eslOK) goto ERROR;
1876 if ((status = esl_stack_IPush(nd_pda, nd)) != eslOK) goto ERROR;
1877 /* push node index for right child */
1878 if ((status = esl_stack_IPush(nd_pda, 0)) != eslOK) goto ERROR;
1879 if ((status = esl_stack_IPush(nd_pda, cm->ndidx[cm->cnum[cm->nodemap[nd]]])) != eslOK) goto ERROR;
1880 /* push node index for left child */
1881 if ((status = esl_stack_IPush(nd_pda, 0)) != eslOK) goto ERROR;
1882 if ((status = esl_stack_IPush(nd_pda, cm->ndidx[cm->cfirst[cm->nodemap[nd]]])) != eslOK) goto ERROR;
1883 }
1884 else {
1885 /* push the node back on for right side */
1886 if ((status = esl_stack_IPush(nd_pda, 1)) != eslOK) goto ERROR;
1887 if ((status = esl_stack_IPush(nd_pda, nd)) != eslOK) goto ERROR;
1888 /* push child node on */
1889 if (cm->ndtype[nd] != END_nd) {
1890 if ((status = esl_stack_IPush(nd_pda, 0)) != eslOK) goto ERROR;
1891 if ((status = esl_stack_IPush(nd_pda, nd+1)) != eslOK) goto ERROR;
1892 }
1893 }
1894 }
1895 }
1896
1897 /* If we're allowing truncated alignments, do a final pass through all states, expanding bands to allow for
1898 * L and/or R and/or T marginal alignments, as necessary,
1899 * cp9b->{L,R}marg_{i,j}{min,max} were defined in cp9_PredictStartAndEndPositions().
1900 */
1901 if(do_trunc) {
1902 for(v = 0; v < cm->M; v++) {
1903 if(cp9b->Lvalid[v] || cp9b->Tvalid[v]) { /* allow for left marginal alignment by expanding j band */
1904 jmin[v] = (jmin[v] == -1) ? cp9b->Lmarg_jmin : ESL_MIN(jmin[v], cp9b->Lmarg_jmin);
1905 jmax[v] = (jmax[v] == -2) ? cp9b->Lmarg_jmax : ESL_MAX(jmax[v], cp9b->Lmarg_jmax);
1906 }
1907 if(cp9b->Rvalid[v] || cp9b->Tvalid[v]) { /* allow for right marginal alignment by expanding i band */
1908 imin[v] = (imin[v] == -1) ? cp9b->Rmarg_imin : ESL_MIN(imin[v], cp9b->Rmarg_imin);
1909 imax[v] = (imax[v] == -2) ? cp9b->Rmarg_imax : ESL_MAX(imax[v], cp9b->Rmarg_imax);
1910 }
1911 }
1912 }
1913
1914 if(! doing_search) { /* if we're aligning the full seq must be aligned at the root state */
1915 imin[0] = i0; /* first residue must be in subtree of ROOT_S */
1916 if(imin[1] != -1) imin[1] = i0; /* first residue must be in subtree of ROOT_IL, if it is used */
1917 jmax[0] = j0; /* final residue must be in subtree of ROOT_S */
1918 if(jmin[1] != -1) jmax[1] = j0; /* final residue must be in subtree of ROOT_IL if it is used */
1919 if(jmin[2] != -1) jmax[2] = j0; /* final residue must be in subtree of ROOT_IR if it is used */
1920 }
1921
1922 /* Final pass through all states:
1923 * 1. if any band value implies a state is unreachable, make it so by setting imin[v]=jmin[v]=-1, imax[v]=jmax[v]=-2;
1924 * 2. set detached inserts unreachable.
1925 * 3. if(!do_trunc) for left emitters enforce jmin/jmax allow at least 1 residue to be emitted
1926 * 4. if(!do_trunc) for MP states, enforce jmin/jmax allow at least 2 residues to be emitted
1927 */
1928 for(v = 0; v < cm->M; v++) {
1929 ESL_DASSERT1(((imin[v] == -1 && imax[v] == -2) || (imin[v] >= 0 && imax[v] >= 0)));
1930 ESL_DASSERT1(((jmin[v] == -1 && jmax[v] == -2) || (jmin[v] >= 0 && jmax[v] >= 0)));
1931 if(imin[v] == -1 || jmin[v] == -1) {
1932 imin[v] = jmin[v] = -1;
1933 imax[v] = jmax[v] = -2;
1934 }
1935 if(StateIsDetached(cm, v)) {
1936 imin[v] = jmin[v] = -1;
1937 imax[v] = jmax[v] = -2;
1938 }
1939 if(! do_trunc) {
1940 if(cm->sttype[v] == MP_st) {
1941 if(jmax[v] == i0) { /* HMM tells us right half of MP state must emit first residue in the sequence, we know better, make state unreachable */
1942 ESL_DASSERT1((jmin[v] == i0));
1943 imin[v] = jmin[v] = -1; /* ignore hmm */
1944 imax[v] = jmax[v] = -2; /* ignore hmm */
1945 }
1946 else if (jmin[v] == i0) { /* HMM tells us right half of MP state could possibly first residue (i0), but we know it can't (see comment above). */
1947 jmin[v]++; /* pad 1 onto what the hmm thought, make first emittable residue i0+1 */
1948 /* leave jmax[v] alone, we konw it's not == i0, we checked for that case above */
1949 }
1950 }
1951 /* if a left emitter, enforce jmin/jmax require at least 1 residue is emitted */
1952 if((StateLeftDelta(cm->sttype[v]) == 1) && imin[v] != -1) {
1953 if(jmax[v] == (i0-1)) { /* HMM bands implied state must be entered after emitting exactly 0 residues, we know better, make it unreachable */
1954 ESL_DASSERT1((jmin[v] == (i0-1)));
1955 imin[v] = jmin[v] = -1; /* ignore hmm */
1956 imax[v] = jmax[v] = -2; /* ignore hmm */
1957 }
1958 else if (jmin[v] == (i0-1)) {
1959 jmin[v] = i0; /* pad 1 onto what the hmm thought */
1960 /* leave jmax[v] alone, we know it's not == i0-1, we checked for that case above */
1961 }
1962 }
1963 }
1964 }
1965
1966 #if 0
1967 if(do_trunc) {
1968 for(v = 0; v < cm->M; v++) {
1969 printf("dotrunc: %d ijband v: %4d nd: %4d %4s %2s i: %5d - %5d j: %5d - %5d\n", do_trunc, v, cm->ndidx[v],
1970 Nodetype(cm->ndtype[cm->ndidx[v]]),
1971 Statetype(cm->sttype[v]),
1972 imin[v], imax[v], jmin[v], jmax[v]);
1973 }
1974 }
1975 #endif
1976
1977 /* A final, brutal hack. If the hmm used to derive bands has local
1978 * begins, ends and ELs on, it's possible (but extremely rare
1979 * empirically, even with very high tau values (0.49!)) that no
1980 * valid CM parse exists within the i and j bands. To avoid this, if
1981 * the CM has local begins and ends on then we use a brutal hack
1982 * here to enable at least one valid parse from the ROOT_S to a
1983 * state from which the EL can be reached and able to emit all
1984 * residues.
1985 *
1986 * There's 2 relevant cases.
1987 *
1988 * Case 1: node 1 is a MATP, MATR, or MATL node (this is the easier case)
1989 * Case 2: node 1 is a BIF node
1990 *
1991 * Case 1: node 1 is a MATP, MATR, or MATL node (this is the easier case)
1992 * A. assert CM local begins and ends are on (they should be if we're using a localized HMM to get bands).
1993 * and we can do a local begin into and a local end out of node 1. This will be TRUE unless there
1994 * are only 3 nodes in the CM (which is impossible, cmbuild won't build a 3 node CM - the reason is that
1995 * such a CM would suck at local alignment b/c no local ends are possible (not to mention they're too small
1996 * to be useful, and that if node 1 == MATL the CM can only emit/align 1 residue in local mode b/c the
1997 * ROOT_IL, ROOT_IR are unreachable and the MATL_IL is detached!).
1998 *
1999 * B. if we're doing alignment (full target must be accounted for):
2000 * v = cm->nodemap[nd]
2001 * set imin[v] = ESL_MIN(imin[v], i0)
2002 * imax[v] = ESL_MAX(imax[v], i0)
2003 * jmin[v] = ESL_MIN(jmin[v], j0)
2004 * jmax[v] = ESL_MAX(jmax[v], j0)
2005 * else if we're doing search and v is unreachable, make it reachable by setting
2006 * imin[v] = imin[0];
2007 * imax[v] = imax[0];
2008 * jmin[v] = jmin[0];
2009 * jmax[v] = jmax[0];
2010 * then we'll be able to emit some residues from v, (so we're guaranteed a valid parse.)
2011 *
2012 * Case 2: node 1 is a BIF node
2013 * v = cm->nodemap[nd] (the BIF_B state)
2014 * if v is reachable and we're doing alignment, expand it's bands so that it can
2015 * account for the full seq:
2016 * set imin[v] = ESL_MIN(imin[v], i0)
2017 * imax[v] = ESL_MAX(imax[v], i0)
2018 * jmin[v] = ESL_MIN(jmin[v], j0)
2019 * jmax[v] = ESL_MAX(jmax[v], j0)
2020 * else if v is reachable and we're doing search, ensure that one contiguous chunk of
2021 * seq can be emitted by BIF's children (see code)
2022 *
2023 * if v is not reachable (if we're doing search or not), we enforce 1 valid parse,
2024 * the BIF must emit the full target, residues i0..j0-1 from its' BEGL_S's EL state, and
2025 * residue j0 from it's BEGR_S EL state.
2026 */
2027 if(hmm_is_localized && cm_is_fully_localized) {
2028 if(do_trunc) cp9b->Jvalid[0] = TRUE;
2029 if(imin[0] == -1) { /* ROOT_S is unreachable, uhh... */
2030 imin[0] = imax[0] = i0;
2031 jmin[0] = jmax[0] = j0;
2032 }
2033 if(cm->nodes == 3) ESL_FAIL(eslEINCONCEIVABLE, errbuf, "cp9_HMM2ijBands(), cm/hmm are locally configured, only 3 nodes in the CM, this is an illegal CM b/c local ENDs are impossible.");
2034 nd = 1;
2035 if(i0 == j0) {
2036 while((nd < cm->nodes) && (cm->ndtype[nd] == MATP_nd)) nd++; /* a local begin into a MATP_MP state can't happen when the target is 1 residue, it must emit 2 residues */
2037 if(cm->ndtype[nd] == END_nd) ESL_FAIL(eslEINCONCEIVABLE, errbuf, "cp9_HMM2ijBands(), CM has no MATL, MATR or BIF nodes, this shouldn't happen (cmbuild forbids it)!\n");
2038 }
2039 if(cm->ndtype[nd] == BIF_nd) {
2040 v = cm->nodemap[nd];
2041 w = cm->cfirst[v]; /* BEGL_S */
2042 y = cm->cnum[v]; /* BEGR_S */
2043 if(do_trunc) {
2044 cp9b->Jvalid[v] = TRUE;
2045 cp9b->Jvalid[w] = TRUE;
2046 cp9b->Jvalid[y] = TRUE;
2047 }
2048 if(imin[v] != -1 && imin[w] != -1 && imin[y] != -1) { /* v and its children w and y are all reachable */
2049 if(!doing_search) { /* we need to be able to account for the full sequence */
2050 imin[v] = ESL_MIN(imin[v], i0);
2051 imax[v] = ESL_MAX(imax[v], i0);
2052 jmin[v] = ESL_MIN(jmin[v], j0);
2053 jmax[v] = ESL_MAX(jmax[v], j0);
2054 imin[w] = imin[v];
2055 imax[w] = imax[v];
2056 jmax[w] = ESL_MAX(jmax[w], ESL_MIN(j0, imax[w]));
2057 jmin[y] = jmin[v];
2058 jmax[y] = jmax[v];
2059 imin[y] = ESL_MIN(imin[y], ESL_MAX(i0, jmin[y]));
2060 /* now ensure that imin[y] <= jmax[w]+1, so we can definitely emit the full seq */
2061 imin[y] = ESL_MIN(imin[y], ESL_MAX(i0, jmax[w]+1));
2062 imax[y] = ESL_MAX(imin[y], imax[y]);
2063 }
2064 else { /* doing search, we only need to be able to emit some range of residues from BEGL and BEGR's EL states */
2065 imin[y] = ESL_MIN(imin[y], ESL_MAX(i0, jmax[w]+1));
2066 imax[y] = ESL_MAX(imin[y], imax[y]);
2067 }
2068 } /* end of if(imin[v] != -1) */
2069 else { /* v, w or y are unreachable, make them reachable */
2070 if(! doing_search) {
2071 /* if we're doing alignment, we enforce that the full seq must be emittable
2072 * by BIF and it's children's (BEGL_S and BEGR_S) EL states */
2073 imin[v] = i0;
2074 imax[v] = i0;
2075 jmin[v] = j0;
2076 jmax[v] = j0;
2077 imin[w] = i0; /* w will emit i0..j0-1 (which may be 0 residues if i0==j0) */
2078 imax[w] = i0;
2079 jmin[w] = j0-1;
2080 jmax[w] = j0-1;
2081 imin[y] = j0; /* y will emit only j0 */
2082 imax[y] = j0;
2083 jmin[y] = j0;
2084 jmax[y] = j0;
2085 }
2086 else {
2087 /* if we're doing search we enforce that the residues from imin[0]..jmax[0] are emittable
2088 * by BIF and it's children's (BEGL_S and BEGR_S) EL states */
2089 imin[v] = imin[0];
2090 imax[v] = imin[0];
2091 jmin[v] = jmax[0];
2092 jmax[v] = jmax[0];
2093 imin[w] = imin[0]; /* w will emit imin[0]..jmax[0]-1 (which may be 0 residues if imin[0]==jmax[0]) */
2094 imax[w] = imin[0];
2095 jmin[w] = jmax[0]-1;
2096 jmax[w] = jmax[0]-1;
2097 imin[y] = jmax[0]; /* y will emit only jmax[0] */
2098 imax[y] = jmax[0]; /* y will emit only jmax[0] */
2099 jmin[y] = jmax[0];
2100 jmax[y] = jmax[0];
2101 }
2102 }
2103 } /* end of if(cm->ndtype[nd] == BIF_nd) */
2104 else {
2105 /* node nd is a MATL, MATR or MATP */
2106 ESL_DASSERT1((cm->ndtype[nd] == MATL_nd || cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATR_nd));
2107 assert(cm->ndtype[nd] == MATL_nd || cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATR_nd);
2108 v = cm->nodemap[nd];
2109 if(do_trunc) cp9b->Jvalid[v] = TRUE;
2110 /* we can do a local begin into and local end out of v */
2111 ESL_DASSERT1((NOT_IMPOSSIBLE(cm->beginsc[v])));
2112 ESL_DASSERT1((NOT_IMPOSSIBLE(cm->endsc[v])));
2113 assert(NOT_IMPOSSIBLE(cm->beginsc[v]));
2114 assert(NOT_IMPOSSIBLE(cm->endsc[v]));
2115 if(!doing_search) { /* we need to be able to account for the full sequence */
2116 if(imin[v] == -1) { /* v is unreachable, make it reachable only for emitting the full seq */
2117 imin[v] = imax[v] = i0;
2118 jmin[v] = jmax[v] = j0;
2119 }
2120 else { /* v is reachable, expand it's band so it can emit the full seq */
2121 imin[v] = ESL_MIN(imin[v], i0);
2122 imax[v] = ESL_MAX(imax[v], i0);
2123 jmin[v] = ESL_MIN(jmin[v], j0);
2124 jmax[v] = ESL_MAX(jmax[v], j0);
2125 }
2126 }
2127 else { /* doing search, do not need to account for full target sequence, make it so we can reach v for some i and j (this will guarantee >= 1 valid parse) */
2128 if(imin[v] == -1) { /* v is unreachable */
2129 imin[v] = imin[0];
2130 imax[v] = imax[0];
2131 jmin[v] = jmin[0];
2132 jmax[v] = jmax[0];
2133 }
2134 else { /* v is reachable, make sure it's reachable from the ROOT_S state, expand the ROOT_S band */
2135 imin[0] = ESL_MIN(imin[0], imin[v]);
2136 imax[0] = ESL_MAX(imax[0], imax[v]);
2137 jmin[0] = ESL_MIN(jmin[0], jmin[v]);
2138 jmax[0] = ESL_MAX(jmax[0], jmax[v]);
2139 }
2140 }
2141 }
2142 }
2143 /* end of brutal hack */
2144 #if eslDEBUGLEVEL >= 3
2145 /* check for valid CM parse, there should be one, unless do_trunc is true, then we may not... */
2146 if((status = CMBandsCheckValidParse(cm, cp9b, errbuf, i0, j0, doing_search)) != eslOK) {
2147 if(do_trunc) {
2148 printf("No valid parse found in cp9_HMM2ijBands()");
2149 /* but don't fail, and don't return !eslOK status */
2150 }
2151 else {
2152 cm_Fail("in cp9_HMM2ijBands() no valid parse and do_trunc is 0");
2153 }
2154 }
2155 #endif
2156
2157 esl_stack_Destroy(nd_pda);
2158 esl_stack_Destroy(lpos_pda);
2159 free(r_mn);
2160 free(r_mx);
2161 free(r_dn);
2162 free(r_dx);
2163 free(r_in);
2164 free(r_ix);
2165 free(r_nn_i);
2166 free(r_nx_i);
2167 free(r_nn_j);
2168 free(r_nx_j);
2169
2170 return eslOK;
2171
2172 ERROR:
2173 ESL_FAIL(status, errbuf, "Memory allocation error.\n");
2174 }
2175
2176 /* Function: HMMBandsEnforceValidParse()
2177 * Incept: EPN, Fri Feb 1 16:46:50 2008
2178 *
2179 * Purpose: Given bands on HMM states for a target sequence,
2180 * check for a valid HMM parse within those bands.
2181 * If no valid parse exists, expand the bands such that
2182 * one does exist, in a greedy manner.
2183 *
2184 * Bands are expanded using the HMMBandsFixUnreachable()
2185 * function. These take a node that is unreachable
2186 * and modify bands on current node and nearby nodes
2187 * to make it reachable. This is awful hack number 1.
2188 * (see HMMBandsFixUnreachable() for details.
2189 * Note: the technique used for expanding the bands was
2190 * selected for it's relative simplicity. It does not
2191 * expand the bands in any smart way that is aware of
2192 * probability mass or score of the newly possible parses
2193 * during the band expansion. You could try to do that,
2194 * but it's not likely to be worth it, when the default
2195 * bands before expansion do not allow a single parse,
2196 * the real solution is to lower tau, the tail loss parameter
2197 * used during band calculation. This function is really
2198 * only nec so the HMM banding technique is robust to
2199 * high values of tau, higher than any reasonable application
2200 * should use.
2201 *
2202 * Awful hack #2 occurs when two different transitions to the
2203 * same state imply reachable bands that have a 'gap' in the
2204 * middle. For example if node D_3 can reach node M_4 with
2205 * i = 3 or 4, and node M_3 can reach node M_4 with i equal
2206 * to 6 or 7. This means that node M_4 cannot be reached for
2207 * i == 5, but this implementation is much easier if we can
2208 * just set the reachable band for M_4 to 3..7. So, that's
2209 * what we do, and we doctor the band of I_3 so that M_4 *can*
2210 * be reached for i == 5. This is done in HMMBandsFillGap().
2211 * See that function for details. This hack is only performed
2212 * for models NOT in local mode. If we are in local mode,
2213 * this 'gap' situation comes up much more often, but when
2214 * we're in local mode, we can use an EL state in the CM
2215 * to basically always get a valid parse, so we're not
2216 * so worried about enforcing a valid parse and we skip
2217 * this hack.
2218 *
2219 * Args: cp9 - the HMM the bands were derived from
2220 * cp9b - the CP9 bands object
2221 * cp9map - map from CM to cp9
2222 * errbuf - for error messages
2223 * i0 - first residue of sequence we're using bands for
2224 * j0 - final residue of sequence we're using bands for
2225 * doing_search - TRUE if we're going to use these HMM bands for search, not alignment
2226 * ret_r_mn; - RETURN: [0..k..hmm_M] minimal residue position for which we can reach M_k (match state of node k)
2227 * ret_r_mx; - RETURN: [0..k..hmm_M] maximal residue position for which we can reach M_k (match state of node k)
2228 * ret_r_in; - RETURN: [0..k..hmm_M] minimal residue position for which we can reach I_k (insert state of node k)
2229 * ret_r_ix; - RETURN: [0..k..hmm_M] maximal residue position for which we can reach I_k (insert state of node k)
2230 * ret_r_dn; - RETURN: [0..k..hmm_M] minimal residue position for which we can reach D_k (delete state of node k)
2231 * ret_r_dx; - RETURN: [0..k..hmm_M] maximal residue position for which we can reach D_k (delete state of node k)
2232 * ret_r_nn_i; - RETURN [0..k..hmm_M] minimal residue position for which we can reach node k (any of M_k, I_k, D_k)
2233 * ret_r_nx_j; - RETURN [0..k..hmm_M] minimal residue position for which we can reach node k (any of M_k, I_k, D_k)
2234 * ret_r_nn_j; - RETURN [0..k..hmm_M] minimal residue position for which we can reach node k (any of M_k, I_k, D_k)
2235 * (see notes in cp9_HMM2ijBands() to see diff between ret_r_n{n,x}_i and ret_r_n{n,x}_j
2236 *
2237 * Returns: eslOK on success
2238 * eslEINCONCEIVABLE if we can't expand the bands to make a valid parse (shouldn't happen)
2239 * eslEMEM if a memory allocation error occurs
2240 */
2241 int
2242 HMMBandsEnforceValidParse(CP9_t *cp9, CP9Bands_t *cp9b, CP9Map_t *cp9map, char *errbuf, int i0, int j0, int doing_search,
2243 int **ret_r_mn, int **ret_r_mx, int **ret_r_in, int **ret_r_ix, int **ret_r_dn, int **ret_r_dx,
2244 int **ret_r_nn_i, int **ret_r_nx_i, int **ret_r_nn_j, int **ret_r_nx_j)
2245 {
2246 int status;
2247 /* r_* arrays, these are the bands on 'reachable' residues for each HMM state as we move
2248 * from left to right through the HMM.
2249 * For example, r_mn[k] = 3, r_mx[k] = 5, means that for all possible HMM parses within the bands
2250 * in the cp9b pn_* arrays that reach the match state of node k, the residue emitted by that match
2251 * must be either 3, 4, or 5.
2252 */
2253 int *r_mn; /* [0..k..hmm_M] minimal residue position for which we can reach M_k (match state of node k) */
2254 int *r_mx; /* [0..k..hmm_M] maximal residue position for which we can reach M_k */
2255 int *r_in; /* [0..k..hmm_M] minimal residue position for which we can reach I_k (insert state of node k) */
2256 int *r_ix; /* [0..k..hmm_M] maximal residue position for which we can reach I_k */
2257 int *r_dn; /* [0..k..hmm_M] minimal residue position for which we can reach D_k (delete state of node k) */
2258 int *r_dx; /* [0..k..hmm_M] maximal residue position for which we can reach D_k */
2259 int r_begn; /* minimal first residue position for which we can exit the BEGIN state */
2260 int r_begx; /* minimal first residue position for which we can exit the BEGIN state */
2261 int r_endn; /* minimal final residue position for which we can reach the END state */
2262 int r_endx; /* maximal final residue position for which we can reach the END state */
2263 int *r_nn_i; /* [0..k..hmm_M] minimal residue position for which we can reach node k (any of M_k, I_k, D_k) */
2264 int *r_nx_i; /* [0..k..hmm_M] maximal residue position for which we can reach node k (any of M_k, I_k, D_k) */
2265 int *r_nn_j; /* [0..k..hmm_M] minimal residue position for which we can reach node k (any of M_k, I_k, D_k) */
2266 int *r_nx_j; /* [0..k..hmm_M] maximal residue position for which we can reach node k (any of M_k, I_k, D_k) */
2267 /* r_nn_i and r_nx_i are used when setting i bands, and r_nn_j and r_nx_j are used when setting j bands .
2268 * The values can differ because of an off-by-one issue with the non-emitting (delete and M_0) states of the HMM:
2269 * pn_min_d[k] = i, means posn i was last residue emitted prior to entering node k's delete state. However, for a CM,
2270 * if a delete states sub-parsetree is bounded by i' and j', this means positions i' and j' HAVE YET TO BE EMITTED.
2271 * For i states this means we have to add 1 to the delete band positions, but for j states we do not, the off-by-one
2272 * is taken care of because the HMM is moving left to right, while j positions move right to left (confusing as hell,
2273 * bad explanation, i know... write out an example, it's the only way to get it).
2274 */
2275 int *r_nn_hmm; /* [0..k..hmm_M] min reachable position i in HMM node k from the HMM's perspective */
2276 int *r_nx_hmm; /* [0..k..hmm_M] max reachable position i in HMM node k from the HMM's perspective */
2277 int *was_unr; /* [0..k..hmm_M] TRUE if node k was unreachable, then we expanded bands, now it should be reachable */
2278 int *filled_gap; /* [0..k..hmm_M] TRUE if we filled a gap in the reachable bands for node k */
2279 int just_filled_gap; /* TRUE if we filled a gap for the current node */
2280 int hmm_M = cp9b->hmm_M; /* number of nodes in the model */
2281 int k, kp; /* node counters */
2282 int n; /* a temporary minimum residue position */
2283 int x; /* a temporary maximum residue position */
2284 int c; /* counter */
2285 int sd; /* state delta, number of emissions for each state */
2286 int local_begins_ends_on; /* TRUE if HMM has local begins (M_0(B) -> M_k for k = 1..M and local ends (M_k -> E) for k = 1..M-1 */
2287 /*int j0_is_reachable = FALSE; */ /* TRUE if we can reach j0 for some node */
2288 /* ptrs to cp9b data, for convenience */
2289 int *pn_min_m; /* pn_min_m[k] = first position in HMM band for match state of HMM node k */
2290 int *pn_max_m; /* pn_max_m[k] = final position in HMM band for match state of HMM node k */
2291 int *pn_min_i; /* pn_min_i[k] = first position in HMM band for insert state of HMM node k */
2292 int *pn_max_i; /* pn_max_i[k] = final position in HMM band for insert state of HMM node k */
2293 int *pn_min_d; /* pn_min_d[k] = first position in HMM band for delete state of HMM node k */
2294 int *pn_max_d; /* pn_max_d[k] = final position in HMM band for delete state of HMM node k */
2295
2296 if((cp9->flags & CPLAN9_LOCAL_BEGIN) && (! (cp9->flags & CPLAN9_LOCAL_END))) ESL_FAIL(eslEINCOMPAT, errbuf, "HMMBandsEnforceValidParse(), HMM has local begins ON but local ends OFF. Both must be on, or both must be off.");
2297 local_begins_ends_on = ((cp9->flags & CPLAN9_LOCAL_BEGIN) && (cp9->flags & CPLAN9_LOCAL_END)) ? TRUE : FALSE;
2298
2299 pn_min_m = cp9b->pn_min_m;
2300 pn_max_m = cp9b->pn_max_m;
2301 pn_min_i = cp9b->pn_min_i;
2302 pn_max_i = cp9b->pn_max_i;
2303 pn_min_d = cp9b->pn_min_d;
2304 pn_max_d = cp9b->pn_max_d;
2305
2306 /* allocate and initialize */
2307 ESL_ALLOC(r_mn, sizeof(int) * (hmm_M+1));
2308 ESL_ALLOC(r_mx, sizeof(int) * (hmm_M+1));
2309 ESL_ALLOC(r_in, sizeof(int) * (hmm_M+1));
2310 ESL_ALLOC(r_ix, sizeof(int) * (hmm_M+1));
2311 ESL_ALLOC(r_dn, sizeof(int) * (hmm_M+1));
2312 ESL_ALLOC(r_dx, sizeof(int) * (hmm_M+1));
2313 ESL_ALLOC(r_nn_i, sizeof(int) * (hmm_M+1));
2314 ESL_ALLOC(r_nx_i, sizeof(int) * (hmm_M+1));
2315 ESL_ALLOC(r_nn_j, sizeof(int) * (hmm_M+1));
2316 ESL_ALLOC(r_nx_j, sizeof(int) * (hmm_M+1));
2317 ESL_ALLOC(r_nn_hmm, sizeof(int) * (hmm_M+1));
2318 ESL_ALLOC(r_nx_hmm, sizeof(int) * (hmm_M+1));
2319
2320 for(k = 0; k <= hmm_M; k++) {
2321 r_mn[k] = r_in[k] = r_dn[k] = r_nn_i[k] = r_nn_j[k] = r_nn_hmm[k] = INT_MAX;
2322 r_mx[k] = r_ix[k] = r_dx[k] = r_nx_i[k] = r_nx_j[k] = r_nx_hmm[k] = INT_MIN;
2323 }
2324 r_begn = INT_MAX;
2325 r_begx = INT_MIN;
2326 r_endn = INT_MAX;
2327 r_endx = INT_MIN;
2328
2329 ESL_ALLOC(was_unr, sizeof(int) * (hmm_M+1));
2330 ESL_ALLOC(filled_gap, sizeof(int) * (hmm_M+1));
2331 esl_vec_ISet(was_unr, (hmm_M+1), FALSE);
2332 esl_vec_ISet(filled_gap, (hmm_M+1), FALSE);
2333
2334 /* Note on comment nomenclature:
2335 * M_k: match state of node k
2336 * I_k: insert state of node k
2337 * D_k: detele state of node k
2338 */
2339
2340 if(! doing_search) assert(pn_min_m[0] == (i0-1));
2341 if(pn_min_m[0] != -1) {
2342 r_mn[0] = pn_min_m[0]; /* initialize min reachable residue for M_0 as pn_min_m[0] */
2343 r_mx[0] = pn_max_m[0]; /* initialize min reachable residue for M_0 as pn_max_m[0] */
2344 }
2345
2346 /* The main loop: for each node, for each state, determine which residues are reachable given
2347 * the reachable residues for the states in the previous node and current node.
2348 * The order is important: first we account for all transitions to the insert state of the same
2349 * node, as the reachable band on the insert will affect later transitions.
2350 * Then we do all transitions to the match of the next node, and finally to the delete of the
2351 * next node.
2352 */
2353 for(k = 0; k <= hmm_M; k++) {
2354 if(pn_min_m[k] == -1) ESL_DASSERT1((pn_max_m[k] == -1));
2355 if(pn_min_i[k] == -1) ESL_DASSERT1((pn_max_i[k] == -1));
2356 if(pn_min_d[k] == -1) ESL_DASSERT1((pn_max_d[k] == -1));
2357 just_filled_gap = FALSE;
2358
2359 /* transitions to insert of node k (I_k) */
2360 if(r_mn[k] <= r_mx[k]) { /* M_k is reachable */
2361 /* M_k->I_k transition */
2362 if(pn_min_i[k] != -1) {
2363 n = r_mn[k]+1;
2364 x = r_mx[k]+1;
2365 if((ESL_MIN(x, pn_max_i[k]) - ESL_MAX(n, pn_min_i[k])) >= 0) { /* TRUE if n..x overlaps with pn_min_i[k]..pn_max_i[k] by at least 1 residue */
2366 n = ESL_MAX(n, pn_min_i[k]); /* n can't be less than pn_min_i[k] */
2367 n = ESL_MIN(n, pn_max_i[k]); /* n can't be more than pn_max_i[k] */
2368 x = ESL_MIN(x, pn_max_i[k]); /* x can't be more than pn_max_i[k] */
2369 /* no need to check if we need to fill a gap, not an issue for inserts which can self-transit and fill their own gaps */
2370 r_in[k] = ESL_MIN(r_in[k], n);
2371 r_ix[k] = ESL_MAX(r_ix[k], x);
2372 ESL_DASSERT1((r_in[k] <= r_ix[k]));
2373 }
2374 }
2375 }
2376 if(r_dn[k] <= r_dx[k]) {
2377 /* D_k->I_k transition */
2378 if(pn_min_i[k] != -1) {
2379 n = r_dn[k]+1;
2380 x = r_dx[k]+1;
2381 if((ESL_MIN(x, pn_max_i[k]) - ESL_MAX(n, pn_min_i[k])) >= 0) { /* TRUE if n..x overlaps with pn_min_i[k]..pn_max_i[k] by at least 1 residue */
2382 n = ESL_MAX(n, pn_min_i[k]); /* n can't be less than pn_min_i[k] */
2383 n = ESL_MIN(n, pn_max_i[k]); /* n can't be more than pn_max_i[k] */
2384 x = ESL_MIN(x, pn_max_i[k]); /* x can't be more than pn_max_i[k] */
2385 /* no need to check if we need to fill a gap, not an issue for inserts which can self-transit and fill their own gaps */
2386 r_in[k] = ESL_MIN(r_in[k], n);
2387 r_ix[k] = ESL_MAX(r_ix[k], x);
2388 ESL_DASSERT1((r_in[k] <= r_ix[k]));
2389 }
2390 }
2391 }
2392 if(r_in[k] <= r_ix[k]) {
2393 /* I_k -> I_k transition */
2394 ESL_DASSERT1((r_ix[k] <= pn_max_i[k]));
2395 /* I_k->I_k transition (first b/c self transitions are possible) */
2396 if(pn_min_i[k] != -1) { /* special case, self emitter, if we can enter this INSERT state, for any valid residue, we can emit residues until we reach pn_max_i[k] */
2397 if(r_in[k] <= pn_max_i[k]) { /* we can reach this insert for i == r_in[k], then emit until pn_max_i[k] */
2398 r_ix[k] = pn_max_i[k];
2399 }
2400 else {
2401 r_in[k] = INT_MAX;
2402 r_ix[k] = INT_MIN;
2403 }
2404 }
2405 }
2406 /* done with transitions to I_k */
2407
2408 /* transitions to match of node k+1 (M_k+1) */
2409 if(k < hmm_M) { /* state M_M+1 is special, it's the END state, we deal with that below */
2410 if(r_mn[k] <= r_mx[k]) {
2411 /* M_k->M_k+1 transition */
2412 if(pn_min_m[k+1] != -1) {
2413 n = r_mn[k]+1;
2414 x = r_mx[k]+1;
2415 if((ESL_MIN(x, pn_max_m[k+1]) - ESL_MAX(n, pn_min_m[k+1])) >= 0) { /* TRUE if n..x overlaps with pn_min_m[k+1]..pn_max_m[k+1] by at least 1 residue */
2416 n = ESL_MAX(n, pn_min_m[k+1]); /* n can't be less than pn_min_m[k+1] */
2417 n = ESL_MIN(n, pn_max_m[k+1]); /* n can't be more than pn_max_m[k+1] */
2418 x = ESL_MIN(x, pn_max_m[k+1]); /* x can't be more than pn_max_m[k+1] */
2419 if(r_mn[k+1] != INT_MAX) {
2420 if(!local_begins_ends_on && ESL_MIN(x, r_mx[k+1]) - ESL_MAX(n, r_mn[k+1]) < -1) {
2421 /* there's a 'gap' of >= 1 residue between n..x and r_mn[k+1].._r_mx[k+1], fill the gap by expanding band of I_k */
2422 if((status = HMMBandsFillGap(cp9b, errbuf, k, n, x, r_mn[k+1], r_mx[k+1], r_mn[k-1], r_dn[k-1])) != eslOK) return status;
2423 just_filled_gap = TRUE;
2424 }
2425 }
2426 r_mn[k+1] = ESL_MIN(r_mn[k+1], n);
2427 r_mx[k+1] = ESL_MAX(r_mx[k+1], x);
2428 ESL_DASSERT1((r_mn[k+1] <= r_mx[k+1]));
2429 }
2430 }
2431 }
2432 /* D_k->M_k+1 transition */
2433 if(r_dn[k] <= r_dx[k]) {
2434 if(pn_min_m[k+1] != -1) {
2435 n = r_dn[k]+1;
2436 x = r_dx[k]+1;
2437 if((ESL_MIN(x, pn_max_m[k+1]) - ESL_MAX(n, pn_min_m[k+1])) >= 0) { /* TRUE if n..x overlaps with pn_min_m[k+1]..pn_max_m[k+1] by at least 1 residue */
2438 n = ESL_MAX(n, pn_min_m[k+1]); /* n can't be less than pn_min_m[k+1] */
2439 n = ESL_MIN(n, pn_max_m[k+1]); /* n can't be more than pn_max_m[k+1] */
2440 x = ESL_MIN(x, pn_max_m[k+1]); /* x can't be more than pn_max_m[k+1] */
2441 if(r_mn[k+1] != INT_MAX) {
2442 if(!local_begins_ends_on && ESL_MIN(x, r_mx[k+1]) - ESL_MAX(n, r_mn[k+1]) < -1) {
2443 /* there's a 'gap' of >= 1 residue between n..x and r_mn[k+1].._r_mx[k+1], fill the gap by expanding band of I_k */
2444 ESL_DASSERT1((k != 0));
2445 if((status = HMMBandsFillGap(cp9b, errbuf, k, n, x, r_mn[k+1], r_mx[k+1], r_mn[k-1], r_dn[k-1])) != eslOK) return status;
2446 just_filled_gap = TRUE;
2447 }
2448 }
2449 r_mn[k+1] = ESL_MIN(r_mn[k+1], n);
2450 r_mx[k+1] = ESL_MAX(r_mx[k+1], x);
2451 ESL_DASSERT1((r_mn[k+1] <= r_mx[k+1]));
2452 }
2453 }
2454 }
2455 /* I_k->M_k+1transition */
2456 if(r_in[k] <= r_ix[k]) {
2457 if(pn_min_m[k+1] != -1) {
2458 n = r_in[k]+1;
2459 x = r_ix[k]+1;
2460 if((ESL_MIN(x, pn_max_m[k+1]) - ESL_MAX(n, pn_min_m[k+1])) >= 0) { /* TRUE if n..x overlaps with pn_min_m[k+1]..pn_max_m[k+1] by at least 1 residue */
2461 n = ESL_MAX(n, pn_min_m[k+1]); /* n can't be less than pn_min_m[k+1] */
2462 n = ESL_MIN(n, pn_max_m[k+1]); /* n can't be more than pn_max_m[k+1] */
2463 x = ESL_MIN(x, pn_max_m[k+1]); /* x can't be more than pn_max_m[k+1] */
2464 if(!local_begins_ends_on && ESL_MIN(x, r_mx[k+1]) - ESL_MAX(n, r_mn[k+1]) < -1) {
2465 /* there's a 'gap' of >= 1 residue between n..x and r_mn[k+1].._r_mx[k+1], fill the gap by expanding band of I_k */
2466 ESL_DASSERT1((k != 0));
2467 if((status = HMMBandsFillGap(cp9b, errbuf, k, n, x, r_mn[k+1], r_mx[k+1], r_mn[k-1], r_dn[k-1])) != eslOK) return status;
2468 just_filled_gap = TRUE;
2469 }
2470 r_mn[k+1] = ESL_MIN(r_mn[k+1], n);
2471 r_mx[k+1] = ESL_MAX(r_mx[k+1], x);
2472 ESL_DASSERT1((r_mn[k+1] <= r_mx[k+1]));
2473 }
2474 }
2475 }
2476 /* EL_kp->M_k+1 transition, we could have come from 1 or more EL states */
2477 if(cp9->flags & CPLAN9_EL) {
2478 if(pn_min_m[k+1] != -1) {
2479 for(c = 0; c < cp9->el_from_ct[k+1]; c++) { /* el_from_ct[k+1] holds # ELs that can go to k+1 */
2480 kp = cp9->el_from_idx[k+1][c];
2481 if(r_mn[kp] <= r_mx[kp]) {
2482 n = r_mn[kp]; /* EL's can emit 0 or more residues */
2483 x = j0; /* EL's can emit 0 or more residues */
2484 if((ESL_MIN(x, pn_max_m[k+1]) - ESL_MAX(n, pn_min_m[k+1])) >= 0) { /* TRUE if n..x overlaps with pn_min_m[k+1]..pn_max_m[k+1] by at least 1 residue */
2485 n = ESL_MAX(n, pn_min_m[k+1]); /* n can't be less than pn_min_m[k+1] */
2486 n = ESL_MIN(n, pn_max_m[k+1]); /* n can't be more than pn_max_m[k+1] */
2487 x = ESL_MIN(x, pn_max_m[k+1]); /* x can't be more than pn_max_m[k+1] */
2488 if(r_mn[k+1] != INT_MAX) {
2489 if(!local_begins_ends_on && ESL_MIN(x, r_mx[k+1]) - ESL_MAX(n, r_mn[k+1]) < -1) {
2490 /* there's a 'gap' of >= 1 residue between n..x and r_mn[k+1].._r_mx[k+1], fill the gap by expanding band of I_k */
2491 ESL_DASSERT1((k != 0));
2492 if((status = HMMBandsFillGap(cp9b, errbuf, k, n, x, r_mn[k+1], r_mx[k+1], r_mn[k-1], r_dn[k-1])) != eslOK) return status;
2493 just_filled_gap = TRUE;
2494 }
2495 }
2496 r_mn[k+1] = ESL_MIN(r_mn[k+1], n);
2497 r_mx[k+1] = ESL_MAX(r_mx[k+1], x);
2498 ESL_DASSERT1((r_mn[k+1] <= r_mx[k+1]));
2499 }
2500 }
2501 }
2502 }
2503 }
2504 /* Begin ->M_k+1 transition, if local begins are on, we could go B->M_k+1, this is always true if M_k+1 is reachbale and (doing_search),
2505 * if we're doing alignment this is true only if the first residue is within the band on M_k+1
2506 */
2507 if(local_begins_ends_on) {
2508 if(pn_min_m[k+1] != -1) {
2509 if(doing_search) {
2510 n = pn_min_m[k+1];
2511 x = pn_max_m[k+1];
2512 r_mn[k+1] = ESL_MIN(r_mn[k+1], n);
2513 r_mx[k+1] = ESL_MAX(r_mx[k+1], x);
2514 }
2515 else { /* doing alignment, we can only do a local begin into M_k+1 if the first residue is within it's band */
2516 if(pn_min_m[k+1] == r_mn[0]+1) {
2517 r_mn[k+1] = ESL_MIN(r_mn[k+1], r_mn[0]+1);
2518 r_mx[k+1] = ESL_MAX(r_mx[k+1], r_mn[0]+1); /* not a typo, r_mx[0] == r_mn[0] (it's the begin state) */
2519 }
2520 }
2521 }
2522 }
2523 } /* end of if(k < hmm_M) */
2524 /* transitions to END state */
2525 if(k == hmm_M || local_begins_ends_on) { /* handle transitions from M_k to END */
2526 if(r_mn[k] <= r_mx[k] && cp9->esc[k] != -INFTY) { /* if M_k is reachable and we're allowed to transit to E */
2527 /* M_k->E transition */
2528 n = r_mn[k];
2529 x = r_mx[k];
2530 /* note: we don't have to worry about filling gaps here (that is if gap of >= 1 residue between [n..x] and [r_endn..r_endx]
2531 * because end state is last state we care about, if we can reach it for residue in n..x or r_endn..r_endx, set band to
2532 * include all those residues (min(n, r_end_n)..max(x, r_endx)) is harmless, a CM parse WILL exist for some residue in that range
2533 * and the CM will be able to find it */
2534 r_endn = ESL_MIN(r_endn, n);
2535 r_endx = ESL_MAX(r_endx, x);
2536 /*////printf("0 r_endn,x: %d..%d (k: %d) \n", r_endn, r_endx, k);*/
2537 ESL_DASSERT1((r_endn <= r_endx));
2538 }
2539 }
2540 if(k == hmm_M) { /* if we're at the last node, we could also get to END from D_k, or I_k */
2541 if(r_dn[k] <= r_dx[k] && cp9->tsc[CTDM][k] != -INFTY) { /* if D_k is reachable and we're allowed to transit to E */
2542 /* D_M->E transition */
2543 n = r_dn[k];
2544 x = r_dx[k];
2545 /* note: we don't have to worry about filling gaps here (see more verbose comment above for M_k->E transition) */
2546 r_endn = ESL_MIN(r_endn, n);
2547 r_endx = ESL_MAX(r_endx, x);
2548 /*////printf("1 r_endn,x: %d..%d\n", r_endn, r_endx);*/
2549 ESL_DASSERT1((r_endn <= r_endx));
2550 }
2551 if(r_in[k] <= r_ix[k] && cp9->tsc[CTIM][k] != -INFTY) { /* if I_k is reachable and we're allowed to transit to E */
2552 /* I_M->E transition */
2553 n = r_in[k];
2554 x = r_in[k];
2555 /* note: we don't have to worry about filling gaps here (see more verbose comment above for M_k->E transition) */
2556 r_endn = ESL_MIN(r_endn, n);
2557 r_endx = ESL_MAX(r_endx, x);
2558 /*////printf("2 r_endn,x: %d..%d\n", r_endn, r_endx);*/
2559 ESL_DASSERT1((r_endn <= r_endx));
2560 }
2561 /* finally, deal with the possibility that we go to E from an EL state */
2562 if(cp9->flags & CMH_LOCAL_END) {
2563 for(c = 0; c < cp9->el_from_ct[k+1]; c++) { /* el_from_ct[k+1] holds # ELs that can go to k+1 */
2564 kp = cp9->el_from_idx[k+1][c];
2565 if(r_mn[kp] <= r_mx[kp]) {
2566 n = r_mn[kp]; /* EL's can emit 0 or more residues */
2567 x = j0;
2568 r_endn = ESL_MIN(r_endn, n);
2569 r_endx = ESL_MAX(r_endx, x);
2570 /*////printf("3 c: %d..%d r_endn: %d\n", c, r_endn, r_endx);*/
2571 ESL_DASSERT1((r_endn <= r_endx));
2572 }
2573 }
2574 }
2575 } /* end of if k == hmm_M, done with transitions to match of node k+1 */
2576
2577 /* transitions to delete of node k+1 (D_k+1)*/
2578 if(k < hmm_M) {
2579 /* M_k -> D_k+1 transition */
2580 if(r_mn[k] <= r_mx[k]) {
2581 if(pn_min_d[k+1] != -1) {
2582 n = r_mn[k];
2583 x = r_mx[k];
2584 if((ESL_MIN(x, pn_max_d[k+1]) - ESL_MAX(n, pn_min_d[k+1])) >= 0) { /* TRUE if n..x overlaps with pn_min_d[k+1]..pn_max_d[k+1] by at least 1 residue */
2585 n = ESL_MAX(n, pn_min_d[k+1]); /* n can't be less than pn_min_d[k+1] */
2586 n = ESL_MIN(n, pn_max_d[k+1]); /* n can't be more than pn_max_d[k+1] */
2587 x = ESL_MIN(x, pn_max_d[k+1]); /* x can't be more than pn_max_d[k+1] */
2588 if(r_dn[k+1] != INT_MAX) {
2589 if(!local_begins_ends_on && ESL_MIN(x, r_dx[k+1]) - ESL_MAX(n, r_dn[k+1]) < -1) {
2590 /* there's a 'gap' of >= 1 residue between n..x and r_dn[k+1].._r_dx[k+1], fill the gap by expanding band of I_k */
2591 ESL_DASSERT1((k != 0));
2592 if((status = HMMBandsFillGap(cp9b, errbuf, k, n, x, r_dn[k+1], r_dx[k+1], r_mn[k-1], r_dn[k-1])) != eslOK) return status;
2593 just_filled_gap = TRUE;
2594 }
2595 }
2596 r_dn[k+1] = ESL_MIN(r_dn[k+1], n);
2597 r_dx[k+1] = ESL_MAX(r_dx[k+1], x);
2598 ESL_DASSERT1((r_dn[k+1] <= r_dx[k+1]));
2599 }
2600 }
2601 }
2602 /* I_k -> D_k+1 transition */
2603 if(r_in[k] <= r_ix[k]) {
2604 /* I_k->D_k+1 transition */
2605 if(pn_min_d[k+1] != -1) {
2606 n = r_in[k];
2607 x = r_ix[k];
2608 if((ESL_MIN(x, pn_max_d[k+1]) - ESL_MAX(n, pn_min_d[k+1])) >= 0) { /* TRUE if n..x overlaps with pn_min_d[k+1]..pn_max_d[k+1] by at least 1 residue */
2609 n = ESL_MAX(n, pn_min_d[k+1]); /* n can't be less than pn_min_d[k+1] */
2610 n = ESL_MIN(n, pn_max_d[k+1]); /* n can't be more than pn_max_d[k+1] */
2611 x = ESL_MIN(x, pn_max_d[k+1]); /* x can't be more than pn_max_d[k+1] */
2612 if(r_dn[k+1] != INT_MAX) {
2613 if(!local_begins_ends_on && ESL_MIN(x, r_dx[k+1]) - ESL_MAX(n, r_dn[k+1]) < -1) {
2614 /* there's a 'gap' of >= 1 residue between n..x and r_dn[k+1].._r_dx[k+1], fill the gap by expanding band of I_k */
2615 ESL_DASSERT1((k != 0));
2616 if((status = HMMBandsFillGap(cp9b, errbuf, k, n, x, r_dn[k+1], r_dx[k+1], r_mn[k-1], r_dn[k-1])) != eslOK) return status;
2617 just_filled_gap = TRUE;
2618 }
2619 }
2620 r_dn[k+1] = ESL_MIN(r_dn[k+1], n);
2621 r_dx[k+1] = ESL_MAX(r_dx[k+1], x);
2622 ESL_DASSERT1((r_dn[k+1] <= r_dx[k+1]));
2623 }
2624 }
2625 }
2626 /* D_k -> D_k+1 */
2627 if(r_dn[k] <= r_dx[k]) {
2628 if(pn_min_d[k+1] != -1) {
2629 n = r_dn[k];
2630 x = r_dx[k];
2631 if((ESL_MIN(x, pn_max_d[k+1]) - ESL_MAX(n, pn_min_d[k+1])) >= 0) { /* TRUE if n..x overlaps with pn_min_d[k+1]..pn_max_d[k+1] by at least 1 residue */
2632 n = ESL_MAX(n, pn_min_d[k+1]); /* n can't be less than pn_min_d[k+1] */
2633 n = ESL_MIN(n, pn_max_d[k+1]); /* n can't be more than pn_max_d[k+1] */
2634 x = ESL_MIN(x, pn_max_d[k+1]); /* x can't be more than pn_max_d[k+1] */
2635 if(r_dn[k+1] != INT_MAX) {
2636 if(!local_begins_ends_on && ESL_MIN(x, r_dx[k+1]) - ESL_MAX(n, r_dn[k+1]) < -1) { /* FALSE if n..x overlaps with r_mn[k+1].._r_mx[k+1] by at least 1 residue, if FAILs we have to pick to either NOT change r_mn, r_mx, or change them to n and x */
2637 ESL_DASSERT1((k != 0));
2638 if((status = HMMBandsFillGap(cp9b, errbuf, k, n, x, r_dn[k+1], r_dx[k+1], r_mn[k-1], r_dn[k-1])) != eslOK) return status;
2639 just_filled_gap = TRUE;
2640 }
2641 }
2642 r_dn[k+1] = ESL_MIN(r_dn[k+1], n);
2643 r_dx[k+1] = ESL_MAX(r_dx[k+1], x);
2644 ESL_DASSERT1((r_dn[k+1] <= r_dx[k+1]));
2645 }
2646 }
2647 }
2648 }
2649
2650 /* update the reachable-by-node bands, which residues can we reach this node for?
2651 * inside the following if's we don't have to check if r_*n[k], r_*x[k] == INT_MAX or
2652 * INT_MIN, b/c we only enter the ifs if r_*n[k] <= r_*x[k]
2653 */
2654 if(r_mn[k] <= r_mx[k]) { /* M_k is reachable for i = r_mn[k]..r_mx[k] */
2655 r_nn_hmm[k] = ESL_MIN(r_nn_hmm[k], r_mn[k]);
2656 r_nx_hmm[k] = ESL_MAX(r_nx_hmm[k], r_mx[k]);
2657
2658 sd = 1;
2659 if(k != hmm_M) {
2660 r_nn_i[k+1] = ESL_MIN(r_nn_i[k+1], r_mn[k]+sd);
2661 r_nx_i[k+1] = ESL_MAX(r_nx_i[k+1], r_mx[k]+sd);
2662 }
2663 if(k != 0) {
2664 r_nn_j[k-1] = ESL_MIN(r_nn_j[k-1], r_mn[k]-sd);
2665 r_nx_j[k-1] = ESL_MAX(r_nx_j[k-1], r_mx[k]-sd);
2666 }
2667 if((local_begins_ends_on && k > 0) || k == hmm_M) { /* we can go to end from M_k with i from r_mn[k]..r_mx[k] */
2668 if(doing_search) {
2669 r_nn_j[k] = ESL_MIN(r_nn_j[k], r_mn[k]);
2670 r_nx_j[k] = ESL_MAX(r_nx_j[k], r_mx[k]);
2671 }
2672 else { /* have to emit j0 from last match state visited */
2673 if(r_mx[k] == j0) {
2674 r_nn_j[k] = ESL_MIN(r_nn_j[k], j0);
2675 r_nx_j[k] = ESL_MAX(r_nx_j[k], j0);
2676 }
2677 }
2678 }
2679 if((local_begins_ends_on && k > 0) || k == 1) { /* we can go from begin to M_k with i to r_mn[k]..r_mx[k] */
2680 if(doing_search) {
2681 r_nn_i[k] = ESL_MIN(r_nn_i[k], r_mn[k]);
2682 r_nx_i[k] = ESL_MAX(r_nx_i[k], r_mx[k]);
2683 /* superfluous */
2684 r_begn = ESL_MIN(r_begn, r_mn[k]);
2685 r_begx = ESL_MAX(r_begx, r_mx[k]);
2686 }
2687 else { /* have to emit i0 from first match state entered */
2688 if(r_mn[k] == i0) {
2689 r_nn_i[k] = ESL_MIN(r_nn_i[k], i0);
2690 r_nx_i[k] = ESL_MAX(r_nx_i[k], i0);
2691 }
2692 }
2693 }
2694 }
2695 if(r_in[k] <= r_ix[k]) { /* I_k is reachable for i = r_in[k]..r_ix[k] */
2696 r_nn_hmm[k] = ESL_MIN(r_nn_hmm[k], r_in[k]);
2697 r_nx_hmm[k] = ESL_MAX(r_nx_hmm[k], r_ix[k]);
2698
2699 sd = 1;
2700 if(k != hmm_M) {
2701 r_nn_i[k+1] = ESL_MIN(r_nn_i[k+1], r_in[k]+sd);
2702 r_nx_i[k+1] = ESL_MAX(r_nx_i[k+1], r_ix[k]+sd);
2703 }
2704 r_nn_j[k] = ESL_MIN(r_nn_j[k], r_in[k]-sd);
2705 r_nx_j[k] = ESL_MAX(r_nx_j[k], r_ix[k]-sd);
2706
2707 /* superfluous */
2708 if(k == 0) {
2709 r_begn = ESL_MIN(r_begn, r_in[k]);
2710 r_begx = ESL_MAX(r_begx, r_ix[k]);
2711 }
2712 }
2713 if(r_dn[k] <= r_dx[k]) { /* D_k is reachable for i = r_dn[k]..r_dx[k] */
2714 r_nn_hmm[k] = ESL_MIN(r_nn_hmm[k], r_dn[k]);
2715 r_nx_hmm[k] = ESL_MAX(r_nx_hmm[k], r_dx[k]);
2716
2717 sd = 0;
2718 if(k != hmm_M) {
2719 r_nn_i[k+1] = ESL_MIN(r_nn_i[k+1], r_dn[k]+1); /* off-by-one */
2720 r_nx_i[k+1] = ESL_MAX(r_nx_i[k+1], r_dx[k]+1); /* off-by-one */
2721 }
2722 if(k != 0) {
2723 r_nn_j[k-1] = ESL_MIN(r_nn_j[k-1], r_dn[k]);
2724 r_nx_j[k-1] = ESL_MAX(r_nx_j[k-1], r_dx[k]);
2725 }
2726 if(k == 1) {
2727 r_begn = ESL_MIN(r_begn, r_dn[k]+1);
2728 r_begx = ESL_MAX(r_begx, r_dx[k]+1);
2729 }
2730 }
2731 /* is the node reachable? (it doesn't matter if we're in local mode) */
2732 if((!local_begins_ends_on) && (r_mn[k] > r_mx[k]) && (r_dn[k] > r_dx[k])) {
2733 assert(k != 0);
2734 ESL_DASSERT1((just_filled_gap == FALSE));
2735 ESL_DPRINTF1(("#DEBUG: ! HMM node %d is unreachable hmm!\n", k));
2736 if(was_unr[k]) ESL_FAIL(eslEINCONCEIVABLE, errbuf, "HMMBandsEnforceValidParse() node k %d was determined unreachable in second pass! Shouldn't happen (coding error).\n", k);
2737 was_unr[k] = TRUE;
2738 /* expand the bands so k becomes reachable, using a greedy technique */
2739 if((status = HMMBandsFixUnreachable(cp9b, errbuf, k, r_nn_hmm[k-1], r_nx_hmm[k-1], r_in[k-1])) != eslOK) return status;
2740 /* to ensure we can now reach node k, we simply decrement k by 2, then
2741 * we'll reenter the loop above for k=k-1, and check if k is reachable with
2742 * new band on I_k-1. This is unnecessary if the code is right, used here just
2743 * to check.
2744 */
2745 k -= 2;
2746 }
2747 else if(just_filled_gap == TRUE) {
2748 ESL_DPRINTF1(("#DEBUG: ! HMM node %d filled a gap!\n", k));
2749 if(filled_gap[k] == TRUE) ESL_FAIL(eslEINCONCEIVABLE, errbuf, "HMMBandsEnforceValidParse() node k %d needed a gap filled in second pass! Shouldn't happen (coding error).\n", k);
2750 filled_gap[k] = TRUE;
2751 /* to ensure we can now reach node k, we simply decrement k by 2, then
2752 * we'll reenter the loop above for k=k, and check if k is reachable with
2753 * new band on I_k. This is unnecessary if the code is right, used here just
2754 * to check.
2755 */
2756 k -= 1;
2757 }
2758 /*else if(r_nx_hmm[k] == j0) j0_is_reachable = TRUE;*/
2759 }
2760 /* final check, if we're doing alignment, the first residue i0, must be first emitted
2761 * residue, and the final residue, j0 must be final emittable residue. Enforce it.
2762 */
2763 if(! doing_search) {
2764 r_begn = i0;
2765 r_begx = i0;
2766 r_endn = j0;
2767 r_endx = j0;
2768 }
2769
2770 /* A hack! set r_nn_j[hmm_M] to rend_n and r_nx_j[hmm_M] to rend_x, b/c we
2771 * only use r_nn_j[hmm_M] and r_nx_j[hmm_M] to set j bands on states of non-right
2772 * emitting CM nodes (non-MATR MATP nodes) and we need the ones above all non
2773 * emitters (where rpos == hmm_M) to have the j bands equal to the band on the
2774 * HMM END state. This is a hack b/c there should be a band on the E state itself,
2775 * which should map to right half of ROOT_S, but I didn't implement it that way.
2776 */
2777 r_nn_i[1] = ESL_MIN(r_nn_i[1], r_begn);
2778 r_nx_i[1] = ESL_MAX(r_nx_i[1], r_begx);
2779 r_nn_j[hmm_M] = ESL_MIN(r_nn_j[hmm_M], r_endn);
2780 r_nx_j[hmm_M] = ESL_MAX(r_nx_j[hmm_M], r_endx);
2781
2782 for(k = 0; k <= hmm_M; k++) {
2783 if(r_mn[k] == INT_MAX) r_mn[k] = -1;
2784 if(r_mx[k] == INT_MIN) r_mx[k] = -2;
2785 if(r_in[k] == INT_MAX) r_in[k] = -1;
2786 if(r_ix[k] == INT_MIN) r_ix[k] = -2;
2787 if(r_dn[k] == INT_MAX) r_dn[k] = -1;
2788 if(r_dx[k] == INT_MIN) r_dx[k] = -2;
2789
2790 if(!local_begins_ends_on) {
2791 ESL_DASSERT1((r_nn_i[k] != INT_MAX || k == 0));
2792 ESL_DASSERT1((r_nx_i[k] != INT_MIN || k == 0));
2793 ESL_DASSERT1((r_nn_j[k] != INT_MAX));
2794 ESL_DASSERT1((r_nx_j[k] != INT_MIN));
2795 }
2796 else {
2797 if(r_nn_i[k] == INT_MAX) r_nn_i[k] = -1;
2798 if(r_nx_i[k] == INT_MIN) r_nx_i[k] = -2;
2799 if(r_nn_j[k] == INT_MAX) r_nn_j[k] = -1;
2800 if(r_nx_j[k] == INT_MIN) r_nx_j[k] = -2;
2801 }
2802 }
2803
2804
2805 *ret_r_mn = r_mn;
2806 *ret_r_mx = r_mx;
2807 *ret_r_in = r_in;
2808 *ret_r_ix = r_ix;
2809 *ret_r_dn = r_dn;
2810 *ret_r_dx = r_dx;
2811 *ret_r_nn_i = r_nn_i;
2812 *ret_r_nx_i = r_nx_i;
2813 *ret_r_nn_j = r_nn_j;
2814 *ret_r_nx_j = r_nx_j;
2815 free(was_unr);
2816 free(filled_gap);
2817 free(r_nn_hmm);
2818 free(r_nx_hmm);
2819
2820 return eslOK;
2821
2822 ERROR:
2823 ESL_FAIL(status, errbuf, "HMMBandsEnforceValidParse(): memory allocation error.");
2824 return eslOK; /* neverreached */
2825 }
2826
2827 /* Function: HMMBandsFixUnreachable()
2828 * Incept: EPN, Fri Feb 1 17:12:55 2008
2829 *
2830 * Purpose: Expand the HMM bands such that a parse becomes
2831 * possible up through node <k>. We know that a parse
2832 * is possible up through node <k-1>, the reachable
2833 * range of residues for all possible parses up to
2834 * node <k-1> is from <r_prv_min> to <r_prv_max>.
2835 *
2836 * Note: The technique used for expanding the bands was
2837 * selected for it's *relative* simplicity. It does not
2838 * expand the bands in any smart way that is aware of
2839 * probability mass or score of the newly possible parses
2840 * during the band expansion. You could try to do that,
2841 * but I don't think it's worth it. This function is only
2842 * entered if the default bands (prior to expansion)
2843 * do not allow a single parse, in which case the bands are
2844 * too tight, and the smart solution is to lower tau,
2845 * the tail loss parameter. In other words this function is
2846 * only very rarely used for reasonable values of tau
2847 * ('reasonable' determined from empirical expts, and
2848 * enforced by getopts). This function is necessary
2849 * for the HMM banding technique to be robust though,
2850 * otherwise it's possible that the HMM bands make all
2851 * parses impossible, which is bad, because that means
2852 * all CM parses are impossible too.
2853 *
2854 * There are two possible scenarios for why node k
2855 * is unreachable, each with a different solution
2856 * this function determines which scenario node k is
2857 * in and then fixes it. The scenarios are described
2858 * in comments in the code below.a
2859 *
2860 * Args: cp9b - the CP9 bands object
2861 * errbuf - for error messages
2862 * k - the node we want to make reachable
2863 * r_prv_min - minimal possible residue index accounted for in any parse up to and including node k-1
2864 * r_prv_max - maximal possible residue index accounted for in any parse up to and including node k-1
2865 * r_insert_prv_min - minimal possible residue index accounted for in any parse up to and state I_k-1
2866 *
2867 * Returns: eslOK on success
2868 * eslEMEM if a memory allocation error occurs
2869 */
2870 int
2871 HMMBandsFixUnreachable(CP9Bands_t *cp9b, char *errbuf, int k, int r_prv_min, int r_prv_max, int r_insert_prv_min)
2872 {
2873
2874 int kp; /* k prime, a node counter */
2875 int nxt_m; /* minimal possible residue index we must account for before entering M_k */
2876 int nxt_d; /* minimal possible residue index we must account for before entering D_k */
2877 int nxt_n; /* minimal possible residue index we must account for before entering either M_k or D_k */
2878
2879 ESL_DASSERT1((k != 0));
2880 ESL_DASSERT1((r_prv_min != INT_MAX));
2881 ESL_DASSERT1((r_prv_max != INT_MIN));
2882 ESL_DASSERT1((r_prv_min <= r_prv_max));
2883
2884 /* scenario 1: there's a 'hole' of at least 1 residue between the residue posns that can be reached
2885 * for node k-1 (these are r_prv_min..r_prv_max) and by node k's match or delete state.
2886 * our solution is to allow the I_k-1 (node k-1 insert state) to emit the residues in
2887 * the 'hole', then we know we can reach either node k's match or delete.
2888 */
2889 /* check if we're in scenario 1 */
2890
2891 /* initialize, if neither nxt_m nor nxt_d doesn't change, we know we're not in scenario 1 */
2892 nxt_m = -1;
2893 nxt_d = -1;
2894 if(cp9b->pn_min_m[k] != -1 && cp9b->pn_max_m[k] != -1) {
2895 ESL_DASSERT1((cp9b->pn_max_m[k] >= cp9b->pn_min_m[k]));
2896 if(cp9b->pn_max_m[k]-1 > r_prv_min) { /* if we go from I_k-1 to M_k, we have to emit 1 residue from M_k, that's
2897 * why we have cp9b->pn_max_m[k]-1 (i.e. the -1 is for the StateDelta) */
2898 nxt_m = ESL_MAX(cp9b->pn_min_m[k]-1, r_prv_min); /* we could get from node k-1 to node k's match state by using I_k-1 to fill the 'hole' */
2899 }
2900 }
2901 if(cp9b->pn_min_d[k] != -1 && cp9b->pn_max_d[k] != -1) {
2902 ESL_DASSERT1((cp9b->pn_max_d[k] >= cp9b->pn_min_d[k]));
2903 if(cp9b->pn_max_d[k] > r_prv_min) { /* if we go from I_k-1 to D_k, we don't emit from D_k so there's no -1 as above with M_k */
2904 nxt_d = ESL_MAX(cp9b->pn_min_d[k], r_prv_min);/* we could get from node k-1 to node k's delete state by using I_k-1 to fill the 'hole' */
2905 }
2906 }
2907 if(nxt_m != -1 || nxt_d != -1) {
2908 /* we're in scenario 1, there's a 'hole' of missing residues we have to account for before entering node k,
2909 * determine the easier route, to M_k or D_k? (pick route with less required I_k-1 emissions) */
2910 if (nxt_m == -1) nxt_n = nxt_d;
2911 else if (nxt_d == -1) nxt_n = nxt_m;
2912 else nxt_n = ESL_MIN(nxt_m, nxt_d);
2913
2914 /* now doctor I_k-1's bands so that:
2915 * (a) I_k-1 is reachable from at least one of M_k-1, D_k-1
2916 * (b) I_k-1 can transit to M_k or D_k
2917 */
2918 if(cp9b->pn_min_i[k-1] != -1) cp9b->pn_min_i[k-1] = ESL_MIN(cp9b->pn_min_i[k-1], r_prv_min+1);
2919 else cp9b->pn_min_i[k-1] = r_prv_min+1;
2920 if(cp9b->pn_max_i[k-1] != -1) cp9b->pn_max_i[k-1] = ESL_MAX(cp9b->pn_max_i[k-1], nxt_n);
2921 else cp9b->pn_max_i[k-1] = nxt_n;
2922 ESL_DASSERT1((cp9b->pn_max_i[k-1] >= cp9b->pn_min_i[k-1]));
2923 ESL_DPRINTF1(("#DEBUG: scenario 1 reset k from %d to %d\n", k+2, k));
2924 }
2925 else {
2926 /* scenario 2: the opposite of scenario 1. All possible parses that reach node k-1 have already emitted too many
2927 * residues to reach node k. In other words, the maximal residue in the HMM band on node k's match
2928 * and delete states has been already been emitted by all possible parses that end at node k-1.
2929 * We have to use the delete states of nodes k...kp, where kp is the leftmost node that we can reach
2930 * M_kp and emit residue i==r_prv_min+1 or visit D_kp with i == r_prv_min.
2931 */
2932 kp = k;
2933 while(kp <= cp9b->hmm_M && ((cp9b->pn_max_m[kp] < (r_prv_min+1)) && (cp9b->pn_max_d[kp] < (r_prv_min)))) { /* note cp9b->pn_max_{m,d}[kp] may be == -1, that's okay */
2934 cp9b->pn_min_d[kp] = cp9b->pn_max_d[kp] = r_prv_min; /* enforce this delete state is used */
2935 kp++;
2936 }
2937 ESL_DPRINTF1(("#DEBUG: scenario 2 reset k from %d to %d (kp: %d r_prv_min: %d (+1=%d for match))\n", k, k-2, kp, r_prv_min, r_prv_min+1));
2938 }
2939 return eslOK;
2940 }
2941
2942 /* Function: HMMBandsFillGap()
2943 * Incept: EPN, Fri Feb 1 17:12:55 2008
2944 *
2945 * Purpose: In HMMBandsEnforceValidParse() it's possible (but rare) that two
2946 * different transitions to the same state imply reachable bands that have
2947 * a 'gap' in the middle. For example if node D_3 can reach node M_4 with
2948 * i = 3 or 4, and node M_3 can reach node M_4 with i equal to 6 or 7.
2949 * This means that node M_4 cannot be reached for
2950 * i == 5, but the HMMBandsEnforceValidParse() implementation is
2951 * much easier if we can just set the reachable band for M_4 to
2952 * 3..7. So, that's what we do, and we doctor the band of I_3 so
2953 * that M_4 *can* be reached for i == 5. This band doctoring is
2954 * done in this function.
2955 *
2956 * Args: cp9b - the CP9 bands object
2957 * errbuf - for error messages
2958 * k - the node we want to make reachable
2959 * min1 - min in the reachable band for first of the two relevant transitions
2960 * max1 - max in the reachable band for first of the two relevant transitions
2961 * min2 - min in the reachable band for second of the two relevant transitions
2962 * max2 - max in the reachable band for second of the two relevant transitions
2963 * prv_nd_r_mn - min residue posn in reachable band of M_k-1, -1 if M_k-1 is unreachable
2964 * prv_nd_r_dn - min residue posn in reachable band of D_k-1, -1 if D_k-1 is unreachable
2965 *
2966 * Returns: eslOK on success
2967 */
2968 int
2969 HMMBandsFillGap(CP9Bands_t *cp9b, char *errbuf, int k, int min1, int max1, int min2, int max2, int prv_nd_r_mn, int prv_nd_r_dn)
2970 {
2971 int right_min; /* min2/max2 if min1 <= min2, else min1/max1 */
2972 int in, ix; /* min/max residue for I_k, calc'ed here */
2973
2974 ESL_DASSERT1((k != 0));
2975 ESL_DASSERT1((max1 >= min1));
2976 ESL_DASSERT1((max2 >= min2));
2977
2978 if (min1 <= min2) { right_min = min2; }
2979 else { right_min = min1; }
2980
2981 #if (eslDEBUGLEVEL >= 1)
2982 int left_max; /* min1/max1 if min1 <= min2, else min2/max2 */
2983 if (min1 <= min2) { left_max = max1; }
2984 else { left_max = max2; }
2985 assert(right_min - left_max > 1);
2986 #endif
2987
2988 /* determine in and ix */
2989 in = INT_MAX;
2990 if(prv_nd_r_mn != INT_MAX) in = ESL_MIN(in, prv_nd_r_mn+1);
2991 if(prv_nd_r_dn != INT_MAX) in = ESL_MIN(in, prv_nd_r_dn);
2992 ESL_DASSERT1((in != INT_MAX));
2993 assert(in != INT_MAX);
2994 ix = right_min-1;
2995
2996 /* doctor I_k's bands so that it:
2997 * (a) I_k is reachable from at M_k or D_k (whichever has leftmost reachable band)
2998 * (b) I_k can transit to M_k+1 or D_k+1 (whichever has rightmost reachable band)
2999 */
3000 if(cp9b->pn_min_i[k] != -1) cp9b->pn_min_i[k] = ESL_MIN(cp9b->pn_min_i[k], in);
3001 else cp9b->pn_min_i[k] = in;
3002 if(cp9b->pn_max_i[k] != -1) cp9b->pn_max_i[k] = ESL_MAX(cp9b->pn_max_i[k], ix);
3003 else cp9b->pn_max_i[k] = ix;
3004 assert(cp9b->pn_min_i[k] <= cp9b->pn_max_i[k]);
3005 ESL_DASSERT1((cp9b->pn_min_i[k] <= cp9b->pn_max_i[k]));
3006
3007 return eslOK;
3008 }
3009
3010 #if eslDEBUGLEVEL >= 3
3011 /* Function: CMBandsCheckValidParse()
3012 * Incept: EPN, Tue Feb 5 07:59:48 2008
3013 *
3014 * Purpose: Given bands on CM states for a target sequence,
3015 * check for a valid CM parse within those bands.
3016 * Return eslFAIL if there is no valid parse.
3017 *
3018 * Args: cm - the model
3019 * cp9b - the CP9 bands object
3020 * errbuf - for error messages
3021 * i0 - first residue we're concerned with in target sequence
3022 * j0 - final residue we're concerned with in target sequence
3023 * doing_search - TRUE if we're searching, and a local hit is okay,
3024 * if FALSE, the full sequence i0..j0 must be in the subtree of ROOT_S
3025 *
3026 * Returns: eslOK on success
3027 * eslEINCOMPAT if contract is violated
3028 * eslFAIL if no valid parse exists within the i and j bands
3029 * eslEMEM if a memory allocation error occurs
3030 */
3031 int
3032 CMBandsCheckValidParse(CM_t *cm, CP9Bands_t *cp9b, char *errbuf, int i0, int j0, int doing_search)
3033 {
3034 int status; /* easel status code */
3035 int v, w, y; /* state indices */
3036 int nd; /* nd counter */
3037 int sd, sdl, sdr; /* state deltas, number of residues emitted by current state, total, to the left, and to the right */
3038 int *imin, *imax; /* [0..v..M-1] i band for state v, min/max i position allowed for state v */
3039 int *jmin, *jmax; /* [0..v..M-1] j band for state v, min/max j position allowed for state v */
3040 int child_imin, child_imax; /* imin, imax for child of current state, after accouting for emissions (state deltas) */
3041 int child_jmin, child_jmax; /* jmin, jmax for child of current state, after accouting for emissions (state deltas) */
3042 int *v_is_r; /* [0..v..M-1] TRUE if state v is reachable for at least one i,j pair */
3043 int *nd_is_r; /* [0..nd..cm->nodes-1] TRUE if any state (incl. insert) in node nd is reachable for at least one i,j pair */
3044 int *r_imin, *r_imax; /* [0..v..M-1] reachable i bands, for which i positions can we reach state v */
3045 int *r_jmin, *r_jmax; /* [0..v..M-1] reachable j bands, for which j positions can we reach state v */
3046 int *nd_r_imin, *nd_r_imax; /* [0..nd..M-1] reachable i bands, for which i positions can we reach at least 1 state (incl. insert) in nd */
3047 int *nd_r_jmin, *nd_r_jmax; /* [0..nd..M-1] reachable j bands, for which j positions can we reach at least 1 state (incl. insert) in nd */
3048 int y_nd, w_nd; /* node index */
3049 int cm_is_localized; /* TRUE if local begins and ends are on, if we can reach a state v with a non-impossible endsc[v], we can finish the parse for any i,j reachable for v */
3050
3051 /*printf("TEMP in CMBandsCheckValidParse() i0: %d j0: %d\n", i0, j0);*/
3052
3053 if((cm->flags & CMH_LOCAL_BEGIN) && (! (cm->flags & CMH_LOCAL_END))) ESL_FAIL(eslEINCOMPAT, errbuf, "CMBandsCheckValidParse(), cm flag CMH_LOCAL_BEGIN is up and cm flag CMH_LOCAL_END is down. This is unexpected, we can't deal.");
3054 if((! (cm->flags & CMH_LOCAL_BEGIN)) && ((cm->flags & CMH_LOCAL_END))) ESL_FAIL(eslEINCOMPAT, errbuf, "CMBandsCheckValidParse(), cm flag CMH_LOCAL_BEGIN is down and cm flag CMH_LOCAL_END is up. This is unexpected, we can't deal.");
3055
3056 cm_is_localized = ((cm->flags & CMH_LOCAL_BEGIN) && (cm->flags & CMH_LOCAL_END)) ? TRUE : FALSE;
3057
3058 /* pointers to cp9b arrays, for convenience */
3059 imin = cp9b->imin;
3060 imax = cp9b->imax;
3061 jmin = cp9b->jmin;
3062 jmax = cp9b->jmax;
3063
3064 /* allocate and initialize */
3065 ESL_ALLOC(v_is_r, sizeof(int) * cm->M);
3066 ESL_ALLOC(r_imin, sizeof(int) * cm->M);
3067 ESL_ALLOC(r_imax, sizeof(int) * cm->M);
3068 ESL_ALLOC(r_jmin, sizeof(int) * cm->M);
3069 ESL_ALLOC(r_jmax, sizeof(int) * cm->M);
3070 ESL_ALLOC(nd_is_r, sizeof(int) * cm->nodes);
3071 ESL_ALLOC(nd_r_imin, sizeof(int) * cm->nodes);
3072 ESL_ALLOC(nd_r_imax, sizeof(int) * cm->nodes);
3073 ESL_ALLOC(nd_r_jmin, sizeof(int) * cm->nodes);
3074 ESL_ALLOC(nd_r_jmax, sizeof(int) * cm->nodes);
3075
3076 esl_vec_ISet(v_is_r, cm->M, FALSE);
3077 esl_vec_ISet(nd_is_r, cm->nodes, FALSE);
3078
3079 for (v = 0; v < cm->M; v++) {
3080 r_imin[v] = INT_MAX;
3081 r_imax[v] = INT_MIN;
3082 r_jmin[v] = INT_MAX;
3083 r_jmax[v] = INT_MIN;
3084 }
3085 for (nd = 0; nd < cm->nodes; nd++) {
3086 nd_r_imin[nd] = INT_MAX;
3087 nd_r_imax[nd] = INT_MIN;
3088 nd_r_jmin[nd] = INT_MAX;
3089 nd_r_jmax[nd] = INT_MIN;
3090 }
3091
3092 nd_is_r[0] = TRUE;
3093 v_is_r[0] = TRUE;
3094 r_imin[0] = nd_r_imin[0] = imin[0];
3095 r_imax[0] = nd_r_imax[0] = imax[0];
3096 r_jmin[0] = nd_r_jmin[0] = jmin[0];
3097 r_jmax[0] = nd_r_jmax[0] = jmax[0];
3098
3099 if(! doing_search) { /* we're aligning the full sequence from i0..j0, that means imin[0] must == i0 and jmax[0] must == j0, if not we can't align the full seq */
3100 if(imin[0] != i0) ESL_FAIL(eslFAIL, errbuf, "CMBandsCheckValidParse(), doing_search is FALSE, but imin[0] == %d, it should be i0 (%d)\n", imin[0], i0);
3101 if(jmax[0] != j0) ESL_FAIL(eslFAIL, errbuf, "CMBandsCheckValidParse(), doing_search is FALSE, but jmax[0] == %d, it should be j0 (%d)\n", jmax[0], j0);
3102 }
3103
3104 /* deal with local begins, if they're active, we can jump into any local begin state with:
3105 * i within imin[0]..imax[0] and j within jmin[0]..jmax[0], as long as i,j are within
3106 * imin[v]..imax[v] and jmin[v]..jmax[v].
3107 */
3108 if(cm->flags & CMH_LOCAL_BEGIN) {
3109 for(v = 0; v < cm->M; v++) {
3110 if(NOT_IMPOSSIBLE(cm->beginsc[v])) {
3111 if(imin[v] != -1 && jmin[v] != -1) {
3112 if(((ESL_MIN(imax[v], imax[0]) - ESL_MAX(imin[v], imin[0])) >= 0) && /* TRUE if imin[v]..imax[v] overlaps with imin[0]..imax[0] by at least 1 residue */
3113 ((ESL_MIN(jmax[v], jmax[0]) - ESL_MAX(jmin[v], jmin[0])) >= 0)) { /* TRUE if jmin[v]..jmax[v] overlaps with jmin[0]..jmax[0] by at least 1 residue */
3114 r_imin[v] = ESL_MAX(imin[v], imin[0]);
3115 r_imax[v] = ESL_MIN(imax[v], imax[0]);
3116 r_jmin[v] = ESL_MAX(jmin[v], jmin[0]);
3117 r_jmax[v] = ESL_MIN(jmax[v], jmax[0]);
3118 v_is_r[v] = TRUE;
3119 nd_is_r[cm->ndidx[v]] = TRUE;
3120 }
3121 }
3122 ESL_DASSERT1(((cm->stid[v] == MATP_MP) || (cm->stid[v] == MATR_MR) || (cm->stid[v] == MATL_ML) || (cm->stid[v] == BIF_B)));
3123 assert((cm->stid[v] == MATP_MP) || (cm->stid[v] == MATR_MR) || (cm->stid[v] == MATL_ML) || (cm->stid[v] == BIF_B));
3124 }
3125 }
3126 }
3127
3128 /* The main loop: step through the CM, node by node, state by state,
3129 * for reachable-states v, determine which i,j residues are reachable for each child state of v
3130 */
3131 for (nd = 0; nd < cm->nodes; nd++) {
3132 for (v = cm->nodemap[nd]; v < (cm->nodemap[nd] + TotalStatesInNode(cm->ndtype[nd])); v++) {
3133 if(! StateIsDetached(cm, v)) {
3134 if(cm->sttype[v] == E_st) {
3135 if((r_imin[v] <= r_imax[v] && r_jmin[v] <= r_jmax[v]) && ((r_imax[v] - r_jmin[v] - 1) >= 0)) {
3136 /* END state v is reachable for some i, j such that j-i+1 = d = 0 (which is required for E states) */
3137 v_is_r[v] = TRUE;
3138 nd_is_r[nd] = TRUE;
3139 }
3140 }
3141 else if (cm->sttype[v] == B_st) {
3142 /* same loop as if v != B_st, (the else case below) but we know sdl = sdr = 0, and we have two children BEGL_S and BEGR_S */
3143 if((r_imin[v] <= r_imax[v] && r_jmin[v] <= r_jmax[v]) && ((r_jmax[v] - r_imin[v] + 1) >= sd)) {
3144 /* v is reachable for some i, j */
3145 v_is_r[v] = TRUE;
3146 nd_is_r[nd] = TRUE;
3147 w = cm->cfirst[v]; /* BEGL_S */
3148 y = cm->cnum[v]; /* BEGR_S */
3149
3150 /* only way to get to a BEGL_S is through it's BIF parent, even with local begins (no local begin in BEGL_S) */
3151 r_imin[w] = ESL_MAX(imin[w], imin[v]);
3152 r_imax[w] = ESL_MIN(imax[w], imax[v]);
3153 r_jmin[w] = jmin[w];
3154 r_jmax[w] = jmax[w];
3155 w_nd = cm->ndidx[w];
3156 nd_r_imin[w_nd] = r_imin[w];
3157 nd_r_imax[w_nd] = r_imax[w];
3158 nd_r_jmin[w_nd] = r_jmin[w];
3159 nd_r_jmax[w_nd] = r_jmax[w];
3160
3161 /* only way to get to a BEGR_S is through it's BIF parent, even with local begins (no local begin in BEGR_S) */
3162 r_imin[y] = imin[y];
3163 r_imax[y] = imax[y];
3164 r_jmin[y] = ESL_MAX(jmin[y], jmin[v]);
3165 r_jmax[y] = ESL_MIN(jmax[y], jmax[v]);
3166 y_nd = cm->ndidx[y];
3167 nd_r_imin[y_nd] = r_imin[y];
3168 nd_r_imax[y_nd] = r_imax[y];
3169 nd_r_jmin[y_nd] = r_jmin[y];
3170 nd_r_jmax[y_nd] = r_jmax[y];
3171 }
3172 }
3173 else { /* state is not a B_st nor an E_st */
3174 sdl = StateLeftDelta(cm->sttype[v]);
3175 sdr = StateRightDelta(cm->sttype[v]);
3176 sd = sdl + sdr;
3177
3178 if((r_imin[v] <= r_imax[v] && r_jmin[v] <= r_jmax[v]) && ((r_jmax[v] - r_imin[v] + 1) >= sd)) {
3179 /* v is reachable for some i, j */
3180 ///if(NOT_IMPOSSIBLE(cm->endsc[v])) {
3181
3182 v_is_r[v] = TRUE;
3183 nd_is_r[nd] = TRUE;
3184 child_imin = r_imin[v] + sdl;
3185 child_imax = r_imax[v] + sdl;
3186 child_jmin = r_jmin[v] - sdr;
3187 child_jmax = r_jmax[v] - sdr;
3188 if(cm->sttype[v] == IL_st) child_imax = ESL_MAX(child_imax, (imax[v]+1));
3189 if(cm->sttype[v] == IR_st) child_jmin = ESL_MIN(child_jmin, (jmin[v]-1));
3190 ///printf("\nv: %4d %4s %2s (%4d %4d %4d %4d)\n", v, Nodetype(cm->ndtype[cm->ndidx[v]]), Statetype(cm->sttype[v]), r_imin[v], r_imax[v], r_jmin[v], r_jmax[v]);
3191 for(y = cm->cfirst[v]; y < (cm->cfirst[v] + cm->cnum[v]); y++) {
3192 if(imin[y] != -1) {
3193 r_imin[y] = ESL_MIN(r_imin[y], ESL_MAX(imin[y], child_imin));
3194 r_imax[y] = ESL_MAX(r_imax[y], ESL_MIN(imax[y], child_imax));
3195 r_jmin[y] = ESL_MIN(r_jmin[y], ESL_MAX(jmin[y], child_jmin));
3196 r_jmax[y] = ESL_MAX(r_jmax[y], ESL_MIN(jmax[y], child_jmax));
3197
3198 if((r_imin[y] <= r_imax[y] && r_jmin[y] <= r_jmax[y]) && ((r_jmax[y] - r_imin[y] + 1) >= StateDelta(cm->sttype[y]))) {
3199 ///printf("y: %4d %4s %2s (%4d %4d %4d %4d)\n", y, Nodetype(cm->ndtype[cm->ndidx[y]]), Statetype(cm->sttype[y]), r_imin[y], r_imax[y], r_jmin[y], r_jmax[y]);
3200 y_nd = cm->ndidx[y];
3201 nd_r_imin[y_nd] = ESL_MIN(nd_r_imin[y_nd], r_imin[y]);
3202 nd_r_imax[y_nd] = ESL_MAX(nd_r_imax[y_nd], r_imax[y]);
3203 nd_r_jmin[y_nd] = ESL_MIN(nd_r_jmin[y_nd], r_jmin[y]);
3204 nd_r_jmax[y_nd] = ESL_MAX(nd_r_jmax[y_nd], r_jmax[y]);
3205 }
3206 else {
3207 r_imin[y] = INT_MAX;
3208 r_imax[y] = INT_MIN;
3209 r_jmin[y] = INT_MAX;
3210 r_jmax[y] = INT_MIN;
3211 }
3212 }
3213 }
3214 }
3215 } /* end of else that's entered if v != E_st nor B_st */
3216 } /* end of if(!StateIsDetached) */
3217 /*////if(v_is_r[v]) printf("ck v %4s %2s %4d R %d (%11d %11d %11d %11d) (HMM nd: %4d %4d)\n", Nodetype(cm->ndtype[nd]), Statetype(cm->sttype[v]), v, v_is_r[v], r_imin[v], r_imax[v], r_jmin[v], r_jmax[v], cm->cp9map->cs2hn[v][0], cm->cp9map->cs2hn[v][1]);*/
3218 } /* end of for (v) loop */
3219
3220 /*////printf("ck nd %4s %4d R %d (%11d %11d %11d %11d)\n\n", Nodetype(cm->ndtype[nd]), nd, nd_is_r[nd], nd_r_imin[nd], nd_r_imax[nd], nd_r_jmin[nd], nd_r_jmax[nd]); */
3221 }
3222 /* now we know what states are reachable for what i and j, check if a valid parse exists */
3223 if(! cm_is_localized) { /* local begins/ends are off, all nodes must be reachable to get a valid parse */
3224 for(nd = 0; nd < cm->nodes; nd++) {
3225 if(nd_is_r[nd] == FALSE) ESL_FAIL(eslFAIL, errbuf, "CMBandsCheckValidParse(), CM is not locally configured and node %d (%4s) is unreachable\n", nd, Nodetype(cm->ndtype[nd]));
3226 if(cm->ndtype[nd] == BIF_nd) {
3227 v = cm->nodemap[nd];
3228 w = cm->cfirst[v]; /* BEGL_S */
3229 y = cm->cnum[v]; /* BEGR_S */
3230 if(r_jmax[w] < (r_imin[y]-1)) {
3231 ESL_FAIL(eslFAIL, errbuf, "CMBandsCheckValidParse(), CM not local, BEGL_S w:%d nd:%d & BEGR_S y:%d nd:%d bands don't touch, res %d..%d unemittable!\n", w, w_nd, y, y_nd, r_jmax[w]+1, r_imin[y]-1);
3232 }
3233 }
3234 }
3235 }
3236 else if(doing_search && cm_is_localized) { /* we're doing a local search, we have a valid parse if any state from which a local end is possible is reachable */
3237 v = 0;
3238 while(v < cm->M && !(v_is_r[v] && NOT_IMPOSSIBLE(cm->endsc[v]))) v++; /* increment v until we come to a state that is reachable and can go to EL, or we run out of states */
3239 if(v == cm->M && i0 != j0) ESL_FAIL(eslFAIL, errbuf, "CMBandsCheckValidParse(), doing_search=TRUE, CM is local, i0 != j0, but no CM state is reachable from which an EL is possible.\n");
3240 }
3241
3242 free(v_is_r);
3243 free(r_imin);
3244 free(r_imax);
3245 free(r_jmin);
3246 free(r_jmax);
3247 free(nd_is_r);
3248 free(nd_r_imin);
3249 free(nd_r_imax);
3250 free(nd_r_jmin);
3251 free(nd_r_jmax);
3252 return eslOK;
3253
3254 ERROR:
3255 ESL_FAIL(status, errbuf, "CMBandsCheckValidParse(), memory allocation error.");
3256 return status; /* NEVER REACHED */
3257 }
3258 #endif
3259
3260 /**************************************************************************
3261 * cp9_HMM2ijBands_OLD() and helper functions.
3262 * This was how bands were calculated up until revision 2318 (02.07.2008)
3263 *
3264 */
3265 /* helper functions for cp9_HMM2ijBands_OLD() */
3266 static void hmm2ij_prestate_step0_initialize(int n, int *nss_max_imin, int *nss_min_jmax, int i0, int j0);
3267 static void hmm2ij_prestate_step1_set_node_inserts(int n, int *nis_imin, int *nis_imax,
3268 int *nis_jmin, int *nis_jmax,
3269 int *nss_imin, int *nss_imax,
3270 int *nss_jmin, int *nss_jmax,
3271 int *pn_min_i, int *pn_max_i,
3272 CP9Map_t *cp9map);
3273 static void hmm2ij_prestate_step2_determine_safe(int n,
3274 int nss_max_imin_np1, int nss_min_jmax_np1,
3275 int nis_imin_n,
3276 int nis_jmax_n,
3277 int *safe_imax, int *safe_jmin);
3278 static void hmm2ij_prestate_step3_preset_node_splits(int n, int *nis_imin, int *nis_imax,
3279 int *nis_jmin, int *nis_jmax,
3280 int *nss_imin, int *nss_imax,
3281 int *nss_jmin, int *nss_jmax,
3282 int *pn_min_m, int *pn_max_m,
3283 int *pn_min_d, int *pn_max_d,
3284 CP9Map_t *cp9map);
3285 static void hmm2ij_split_state_step1_set_state_bands(int v, int n,
3286 int tmp_imin, int tmp_imax,
3287 int tmp_jmin, int tmp_jmax,
3288 int *imin, int *imax, int *jmin, int *jmax,
3289 int *nss_imin, int *nss_imax,
3290 int *nss_jmin, int *nss_jmax);
3291 static void hmm2ij_insert_state_step1_set_state_bands(int v,
3292 int tmp_imin, int tmp_imax,
3293 int tmp_jmin, int tmp_jmax,
3294 int *imin, int *imax, int *jmin, int *jmax);
3295 static void hmm2ij_state_step2_enforce_safe_trans(CM_t *cm, int v, int n, int *imax, int *jmin,
3296 int *nss_imax, int *nss_jmin,
3297 int safe_imax, int safe_jmin);
3298 static void hmm2ij_state_step3_enforce_state_delta(CM_t *cm, int v, int *jmin, int *jmax);
3299 static void hmm2ij_state_step4_update_safe_holders(int v, int n, int imin_v, int jmax_v, int *nss_max_imin,
3300 int *nss_min_jmax);
3301 static void hmm2ij_state_step5_non_emitter_d0_hack(int v, int imax_v, int *jmin);
3302
3303 /*****************************************************************************
3304 * Functions to go from HMM bands to i and j bands on a CM
3305 * cp9_HMM2ijBands_OLD()
3306 */
3307 /*
3308 * Function: cp9_HMM2ijBands_OLD()
3309 * EPN 12.21.05
3310 *
3311 * Purpose: Determine the band for each cm state v on i (the band on the
3312 * starting index in the subsequence emitted from the subtree rooted
3313 * at state v), and on j (the band on the ending index in the
3314 * subsequence emitted from the subtree rooted at state v).
3315 *
3316 * Some i and d bands are calculated from HMM bands on match and insert
3317 * and delete states from each node of the HMM that maps to a left emitting
3318 * node of the CM (including MATP nodes). The HMM bands were
3319 * calculated previously from the posterior matrices for mmx,
3320 * imx and dmx from a CP9 HMM.
3321 *
3322 * Some j bands are calculated from HMM bands on match and insert and
3323 * delete states from each node of the HMM that maps to a right emitting
3324 * node of the CM (including MATP nodes).
3325 *
3326 * i and j bands that cannot be directly determined from the
3327 * HMM bands are inferred based on the constraints imposed
3328 * on them by the i and j bands that CAN be determined from
3329 * the HMM bands.
3330 *
3331 * Our strategy is to set i and j bands for each state v
3332 * such that at least one state y (y \in C_v (y is reachable
3333 * from v)) can be reached from v while staying within the i
3334 * and j bands for v and y. This constraint is enforced by
3335 * determining the min and max i and j bands across all
3336 * states y (into safe* data structures) for a given v, and
3337 * then enforcing that at least one cell in the i and j
3338 * bands of v can transit to at least one cell in a band for
3339 * a y state after accounting for the direction specific
3340 * StateDelta() values for v.
3341 *
3342 * This function needs to be called only once, it determines
3343 * bands for ALL states. Its unclear the best way to handle
3344 * any states that don't have an explicit mapping to an HMM
3345 * state that we have a band on (i.e. all delete states, and
3346 * ROOT_IR, ROOT_IL, BEGR_IL, BIF_B, and start states).
3347 * (11.02.05) I take a simple approach, and set the bands on i
3348 * for such states to the same as those for states in a close
3349 * proximity. (see code for exact definitions)
3350 *
3351 * This function uses HMM derived bands on delete states.
3352 *
3353 * arguments:
3354 *
3355 * CM_t *cm the CM, must have valid cp9b (CP9 bands object)
3356 * errbuf char buffer for error messages
3357 * CP9Bands_t *cp9b the CP9 bands object, usually cm->cp9b
3358 * CP9Map_t *cp9map map from CM to CP9 HMM and vice versa
3359 * int i0 start of target subsequence (often 1, beginning of dsq)
3360 * int j0 end of target subsequence (often L, end of dsq)
3361 * int doing_search TRUE if the bands will be used for a scanning CYK/Inside
3362 * int debug_level [0..3] tells the function what level of debugging print
3363 * statements to print.
3364 *
3365 * Returns: eslOK on success;
3366 */
3367 int
3368 cp9_HMM2ijBands_OLD(CM_t *cm, char *errbuf, CP9Bands_t *cp9b, CP9Map_t *cp9map, int i0, int j0, int doing_search, int debug_level)
3369 {
3370 int v; /* counter over states of the CM */
3371
3372 int status;
3373 int safe_imax;
3374 int safe_jmin;
3375
3376 int tmp_imin;
3377 int tmp_imax;
3378 int tmp_jmin;
3379 int tmp_jmax;
3380
3381 /* ptrs to cp9b data, for convenience */
3382 int *pn_min_m; /* pn_min_m[k] = first position in HMM band for match state of HMM node k */
3383 int *pn_max_m; /* pn_max_m[k] = last position in HMM band for match state of HMM node k */
3384 int *pn_min_i; /* pn_min_i[k] = first position in HMM band for insert state of HMM node k */
3385 int *pn_max_i; /* pn_max_i[k] = last position in HMM band for insert state of HMM node k */
3386 int *pn_min_d; /* pn_min_d[k] = first position in HMM band for delete state of HMM node k */
3387 int *pn_max_d; /* pn_max_d[k] = last position in HMM band for delete state of HMM node k */
3388 int *imin; /* imin[v] = first position in band on i for state v to be filled in this function. [1..M] */
3389 int *imax; /* imax[v] = last position in band on i for state v to be filled in this function. [1..M] */
3390 int *jmin; /* jmin[v] = first position in band on j for state v to be filled in this function. [1..M] */
3391 int *jmax; /* jmax[v] = last position in band on j for state v to be filled in this function. [1..M] */
3392
3393 int *nss_imin; /* nss_imin[n] = imin of each split set state in node n*/
3394 int *nss_imax; /* nss_imax[n] = imax of each split set state in node n*/
3395 int *nss_jmin; /* nss_jmin[n] = jmin of each split set state in node n*/
3396 int *nss_jmax; /* nss_jmax[n] = jmax of each split set state in node n*/
3397
3398 int *nis_imin; /* nss_imin[n] = imin of each insert set state in node n*/
3399 int *nis_imax; /* nss_imax[n] = imax of each insert set state in node n*/
3400 int *nis_jmin; /* nss_jmin[n] = jmin of each insert set state in node n*/
3401 int *nis_jmax; /* nss_jmax[n] = jmax of each insert set state in node n*/
3402
3403 int *nss_max_imin; /* nss_max_imin[n] = max imin over split set states in node n*/
3404 int *nss_min_jmax; /* nss_min_jmax[n] = min jmax over split set states in node n*/
3405
3406 int n; /* counter over CM nodes. */
3407 int y, yoffset; /* counters over children states */
3408
3409 /* Contract checks */
3410 if (cp9b == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_HMM2ijBands_OLD(), cp9b is NULL.\n");
3411 if(i0 < 1) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_HMM2ijBands_OLD(), i0 < 1: %d\n", i0);
3412 if(j0 < 1) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_HMM2ijBands_OLD(), j0 < 1: %d\n", j0);
3413 if(j0 < i0) ESL_FAIL(eslEINCOMPAT, errbuf, "cp9_HMM2ijBands_OLD(), i0 (%d) < j0 (%d)\n", i0, j0);
3414
3415 /* set pointers to cp9b data
3416 * note: these arrays used to be allocated here, but that was wasteful, now it's allocated
3417 * once per model (instead of once per sequence) in AllocCP9Bands()
3418 */
3419
3420 pn_min_m = cp9b->pn_min_m;
3421 pn_max_m = cp9b->pn_max_m;
3422 pn_min_i = cp9b->pn_min_i;
3423 pn_max_i = cp9b->pn_max_i;
3424 pn_min_d = cp9b->pn_min_d;
3425 pn_max_d = cp9b->pn_max_d;
3426 imin = cp9b->imin;
3427 imax = cp9b->imax;
3428 jmin = cp9b->jmin;
3429 jmax = cp9b->jmax;
3430
3431 ESL_ALLOC(nss_imin, sizeof(int) * cm->nodes);
3432 ESL_ALLOC(nss_imax, sizeof(int) * cm->nodes);
3433 ESL_ALLOC(nss_jmin, sizeof(int) * cm->nodes);
3434 ESL_ALLOC(nss_jmax, sizeof(int) * cm->nodes);
3435
3436 ESL_ALLOC(nis_imin, sizeof(int) * cm->nodes);
3437 ESL_ALLOC(nis_imax, sizeof(int) * cm->nodes);
3438 ESL_ALLOC(nis_jmin, sizeof(int) * cm->nodes);
3439 ESL_ALLOC(nis_jmax, sizeof(int) * cm->nodes);
3440
3441 ESL_ALLOC(nss_max_imin, sizeof(int) * cm->nodes);
3442 ESL_ALLOC(nss_min_jmax, sizeof(int) * cm->nodes);
3443
3444 esl_vec_ISet(nss_imin, cm->nodes, -1);
3445 esl_vec_ISet(nss_imax, cm->nodes, -1);
3446 esl_vec_ISet(nss_jmin, cm->nodes, -1);
3447 esl_vec_ISet(nss_jmax, cm->nodes, -1);
3448
3449 esl_vec_ISet(nis_imin, cm->nodes, -1);
3450 esl_vec_ISet(nis_imax, cm->nodes, -1);
3451 esl_vec_ISet(nis_jmin, cm->nodes, -1);
3452 esl_vec_ISet(nis_jmax, cm->nodes, -1);
3453
3454 esl_vec_ISet(nss_max_imin, cm->nodes, -1);
3455 esl_vec_ISet(nss_min_jmax, cm->nodes, -1);
3456
3457 /* Initialize all bands to -1. */
3458 esl_vec_ISet(imin, cm->M, -1);
3459 esl_vec_ISet(imax, cm->M, -1);
3460 esl_vec_ISet(jmin, cm->M, -1);
3461 esl_vec_ISet(jmax, cm->M, -1);
3462
3463 /* We go node by node, bottom up, and fill in the bands on each
3464 * state for each node. Keeping track of the node split set min and max i's
3465 * and j's, as well as the node insert set's
3466 * also because they influence all nodes above (until a BEGL or BEGR at least).
3467 */
3468
3469 /* For match nodes (MATP, MATL, MATR):
3470 * First calc the split set node mins and maxes, then impose these
3471 * on each state v in the split set of the node, requiring that any valid
3472 * d resulting from the i and j bands on state v
3473 * is least dv = StateDelta(v).
3474 * This is done by ensuring that jmin[v] >= dv & jmax[v] >= dv.
3475 * (We don't have to worry about i as we check again when we create
3476 * the d bands from the i and j bands in ij2d_bands()).
3477 * We really only have to enforce the StateDelta issue here so we
3478 * don't run into d band on j that is 0 cells in ij2d_bands().
3479 * Alternatively, we could ignore the StateDelta() issue here, and
3480 * allow ij2d_bands() to modify j bands when it enforces the StateDelta()
3481 * issue.
3482 */
3483
3484 for(n = (cm->nodes-1); n >= 0; n--) {
3485 switch (cm->ndtype[n]) {
3486 case END_nd:
3487 /* Special case, we need to know the bands on the states
3488 * in the node ABOVE this one. Node above MUST be MATP, MATL
3489 * or MATR. For END states, the band on i = the band on j,
3490 * this is because d must be 0, so i must be (j+1), so its pointless
3491 * to allow an i value that (j+1) is not allowed to be or vice versa.
3492 * If the node above is MATL, we use the HMM band that maps
3493 * to the ML state - these correspond to bands on i. If its a MATR,
3494 * we use the HMM band that maps to the MR state - these correspond
3495 * to bands on j. If its a MATP, we get fancy (see below).
3496 */
3497 v = cm->nodemap[n];
3498 if(cm->ndtype[n-1] == MATL_nd) {
3499 /* tricky. we keep the n_*m** structures ignorant of the fact that we're in
3500 * an end state, i.e. we don't force a d=0 (j-i+1=0). This way when
3501 * the node immediately above the end (the MATL) looks at it when its determining
3502 * the correct bands on i, it doesn't get screwed up (as it would if j < i).
3503 */
3504
3505 /*minimum of delete and match states of node above*/
3506 nss_imin[n] = (pn_min_m[cp9map->nd2lpos[n-1]] <= (pn_min_d[cp9map->nd2lpos[n-1]])) ?
3507 pn_min_m[cp9map->nd2lpos[n-1]] : (pn_min_d[cp9map->nd2lpos[n-1]]);
3508 /*for the max, we must allow possibility of inserts and deletes.*/
3509 nss_imax[n] = (pn_max_m[cp9map->nd2lpos[n-1]] >= pn_max_i[cp9map->nd2lpos[n-1]]) ?
3510 pn_max_m[cp9map->nd2lpos[n-1]] : pn_max_i[cp9map->nd2lpos[n-1]];
3511 /* deletes max bands may always be less than match max bands...(not sure)*/
3512 if(nss_imax[n] < (pn_max_d[cp9map->nd2lpos[n-1]]))
3513 nss_imax[n] = (pn_max_d[cp9map->nd2lpos[n-1]]);
3514
3515 nss_jmin[n] = nss_imin[n];
3516 nss_jmax[n] = nss_imax[n];
3517
3518 imin[v] = nss_imin[n];
3519 imax[v] = nss_imax[n] + 1; /* we add 1 because we have to figure in the emission
3520 * of the MATL_ML (or final MATL_IL), which would increase
3521 * i by 1 potentially relative to the imax of that state.
3522 */
3523 jmin[v] = imin[v] - 1; /* d must be 0 for end states. */
3524 jmax[v] = imax[v] - 1; /* d must be 0 for end states. */
3525
3526 nss_max_imin[n] = imin[v];
3527 nss_min_jmax[n] = jmax[v];
3528 }
3529 else if(cm->ndtype[n-1] == MATR_nd) {
3530 /* tricky. we keep the nss_*m** structures ignorant of the fact that we're in
3531 * an end state, i.e. we don't force a d=0 (j-i+1=0). This way when
3532 * the node immediately above the end (the MATR) looks at it when its determining
3533 * the correct bands on i, it doesn't get screwed up (as it would if j < i).
3534 */
3535
3536 /*minimum of delete and match states of node above */
3537 nss_jmin[n] = (pn_min_m[cp9map->nd2rpos[n-1]] <= pn_min_d[cp9map->nd2rpos[n-1]]) ?
3538 pn_min_m[cp9map->nd2rpos[n-1]] : pn_min_d[cp9map->nd2rpos[n-1]];
3539 /*for the max, we must allow possibility of inserts.*/
3540 nss_jmax[n] = (pn_max_m[cp9map->nd2rpos[n-1]] >= pn_max_i[cp9map->nd2rpos[n-1]]) ?
3541 pn_max_m[cp9map->nd2rpos[n-1]] : pn_max_i[cp9map->nd2rpos[n-1]];
3542 /* deletes max bands may always be less than match max bands...(not sure)*/
3543 if(nss_jmax[n] < pn_max_d[cp9map->nd2rpos[n-1]])
3544 nss_jmax[n] = pn_max_d[cp9map->nd2rpos[n-1]];
3545 nss_imin[n] = nss_jmin[n];
3546 nss_imax[n] = nss_jmax[n];
3547
3548 jmin[v] = nss_jmin[v] - 1; /* we subtract 1 because of we have to figure
3549 * in the emission of the MATR_MR (or final MATR_IR), which would
3550 * decrease j by 1 potentially relative to jmin of that state.
3551 */
3552 jmax[v] = nss_jmax[n];
3553 imin[v] = jmin[v] + 1; /*d (j-i+1) must be 0 for end states*/
3554 imax[v] = jmax[v] + 1; /*d (j-i+1) must be 0 for end states*/
3555
3556 nss_max_imin[n] = imin[v];
3557 nss_min_jmax[n] = jmax[v];
3558 }
3559 else if(cm->ndtype[n-1] == MATP_nd) {
3560 /* Very rare case, only if the last bp in a stem is the last left consensus
3561 * column (respecting gap_thresh) in that alignment. Does happen though,
3562 * (at least in RFAM 6.1) because the training counts for transition priors
3563 * had counts for MATP_* state -> END_nd transition sets.
3564 */
3565
3566 /* tricky. we keep the nss_*m** structures ignorant of the fact that we're in
3567 * an end state, i.e. we don't force a d=0 (j-i+1=0). This way when
3568 * the node immediately above the end (the MATP) looks at it when its determining
3569 * the correct bands on j, it doesn't get screwed up (as it would if j < i).
3570 */
3571 /*minimum of delete and match states of node above*/
3572 nss_imin[n] = (pn_min_m[cp9map->nd2lpos[n-1]] <= (pn_min_d[cp9map->nd2lpos[n-1]])) ?
3573 pn_min_m[cp9map->nd2lpos[n-1]] : (pn_min_d[cp9map->nd2lpos[n-1]]);
3574 /*for the max, we must allow possibility of inserts and deletes.*/
3575 nss_imax[n] = (pn_max_m[cp9map->nd2lpos[n-1]] >= pn_max_i[cp9map->nd2lpos[n-1]]) ?
3576 pn_max_m[cp9map->nd2lpos[n-1]] : pn_max_i[cp9map->nd2lpos[n-1]];
3577 /* deletes max bands may always be less than match max bands...(not sure)*/
3578 if(nss_imax[n] < (pn_max_d[cp9map->nd2lpos[n-1]]))
3579 nss_imax[n] = (pn_max_d[cp9map->nd2lpos[n-1]]);
3580
3581 /*minimum of delete and match states of node above*/
3582 nss_jmin[n] = (pn_min_m[cp9map->nd2rpos[n-1]] <= pn_min_d[cp9map->nd2rpos[n-1]]) ?
3583 pn_min_m[cp9map->nd2rpos[n-1]] : pn_min_d[cp9map->nd2rpos[n-1]];
3584 /*for the max, we must allow possibility of inserts.*/
3585 nss_jmax[n] = (pn_max_m[cp9map->nd2rpos[n-1]] >= pn_max_i[cp9map->nd2rpos[n-1]]) ?
3586 pn_max_m[cp9map->nd2rpos[n-1]] : pn_max_i[cp9map->nd2rpos[n-1]];
3587 /* deletes max bands may always be less than match max bands...(not sure)*/
3588 if(nss_jmax[n] < pn_max_d[cp9map->nd2rpos[n-1]])
3589 nss_jmax[n] = pn_max_d[cp9map->nd2rpos[n-1]];
3590
3591 /* unique situation. end's d must be 0, so we are constrained on what
3592 * i can be relative to j, and j can be relative to i, but what we want
3593 * are the constraints on what i can be, and j can be.
3594 * because d=0 => j-i+1 = 0. then imin should equal = jmin + 1 and imax = jmax + 1.
3595 * so we really just want to know a min over i and j, and a max over i and j.
3596 * below we take min of imin and jmin (should always be imin i think) as the min,
3597 * and max of imax and jmax (should always be jmax i think) after accounting for
3598 * the possibility that a single base was just emitted left and/or right.
3599 */
3600 imax[v] = ((nss_imax[n] + 1) > nss_jmax[n]) ?
3601 (nss_imax[n] + 1) : nss_jmax[n];
3602 imin[v] = ((nss_imin[n]) < (nss_jmin[n] - 1)) ?
3603 (nss_imin[n]) : (nss_jmin[n] - 1);
3604 /* we can't have an i < i0 */
3605 imin[v] = ESL_MAX(imin[v], i0);
3606 imax[v] = ESL_MAX(imax[v], i0);
3607 jmin[v] = imin[v] - 1; /* d must be 0 for end states. */
3608 jmax[v] = imax[v] - 1; /* d must be 0 for end states. */
3609
3610 nss_max_imin[n] = imin[v];
3611 nss_min_jmax[n] = jmax[v];
3612 }
3613 break;
3614
3615 case MATP_nd:
3616 hmm2ij_prestate_step0_initialize(n, nss_max_imin, nss_min_jmax, i0, j0);
3617 hmm2ij_prestate_step1_set_node_inserts(n, nis_imin, nis_imax, nis_jmin, nis_jmax,
3618 nss_imin, nss_imax, nss_jmin, nss_jmax,
3619 pn_min_i, pn_max_i, cp9map);
3620
3621 hmm2ij_prestate_step2_determine_safe(n, nss_max_imin[n+1], nss_min_jmax[n+1],
3622 nis_imin[n], nis_jmax[n],
3623 &safe_imax, &safe_jmin);
3624 hmm2ij_prestate_step3_preset_node_splits(n, nis_imin, nis_imax, nis_jmin, nis_jmax,
3625 nss_imin, nss_imax, nss_jmin, nss_jmax,
3626 pn_min_m, pn_max_m, pn_min_d, pn_max_d,
3627 cp9map);
3628 /* 6 states MATP_MP, MATP_ML, MATP_MR, MATP_D, MATP_IL, MATP_IR */
3629 v = cm->nodemap[n]; /* MATP_MP */
3630 /* Determine implied v bands using hmm for mapped 'direction(s)' and
3631 * next node's bands for non-mapped direction(s).
3632 */
3633 tmp_imin = pn_min_m[cp9map->nd2lpos[n]];
3634 tmp_imax = pn_max_m[cp9map->nd2lpos[n]];
3635 tmp_jmin = pn_min_m[cp9map->nd2rpos[n]];
3636 tmp_jmax = pn_max_m[cp9map->nd2rpos[n]];
3637 hmm2ij_split_state_step1_set_state_bands(v, n, tmp_imin, tmp_imax, tmp_jmin,
3638 tmp_jmax, imin, imax, jmin, jmax,
3639 nss_imin, nss_imax, nss_jmin, nss_jmax);
3640 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, safe_imax,
3641 safe_jmin);
3642 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
3643 hmm2ij_state_step4_update_safe_holders(v, n, imin[v], jmax[v], nss_max_imin, nss_min_jmax);
3644
3645 v++; /*MATP_ML*/
3646 /* Determine implied v bands using hmm for mapped 'direction(s)' and
3647 * next node's bands for non-mapped direction(s).
3648 */
3649 tmp_imin = pn_min_m[cp9map->nd2lpos[n]];
3650 tmp_imax = pn_max_m[cp9map->nd2lpos[n]];
3651 /* 12.19.05 - trying to deal with the right delete off-by-one
3652 * inverted relative to left delete issue.
3653 */
3654 tmp_jmin = (pn_min_d[cp9map->nd2rpos[n]] < nss_jmin[n+1]) ?
3655 pn_min_d[cp9map->nd2rpos[n]] : nss_jmin[n+1];
3656 tmp_jmax = (pn_max_d[cp9map->nd2rpos[n]] > nss_jmax[n+1]) ?
3657 pn_max_d[cp9map->nd2rpos[n]] : nss_jmax[n+1];
3658
3659 hmm2ij_split_state_step1_set_state_bands(v, n, tmp_imin, tmp_imax, tmp_jmin,
3660 tmp_jmax, imin, imax, jmin, jmax,
3661 nss_imin, nss_imax, nss_jmin, nss_jmax);
3662 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, safe_imax,
3663 safe_jmin);
3664 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
3665 hmm2ij_state_step4_update_safe_holders(v, n, imin[v], jmax[v], nss_max_imin, nss_min_jmax);
3666
3667 v++; /*MATP_MR*/
3668 /* this D-left state gets the delete band from the HMM node
3669 * that maps to the left side.
3670 */
3671 tmp_imin = pn_min_d[cp9map->nd2lpos[n]];
3672 tmp_imax = pn_max_d[cp9map->nd2lpos[n]];
3673 tmp_jmin = pn_min_m[cp9map->nd2rpos[n]];
3674 tmp_jmax = pn_max_m[cp9map->nd2rpos[n]];
3675 hmm2ij_split_state_step1_set_state_bands(v, n, tmp_imin, tmp_imax, tmp_jmin,
3676 tmp_jmax, imin, imax, jmin, jmax,
3677 nss_imin, nss_imax, nss_jmin, nss_jmax);
3678 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, safe_imax,
3679 safe_jmin);
3680 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
3681 hmm2ij_state_step4_update_safe_holders(v, n, imin[v], jmax[v], nss_max_imin, nss_min_jmax);
3682
3683 v++; /*MATP_D*/
3684 tmp_imin = pn_min_d[cp9map->nd2lpos[n]];
3685 tmp_imax = pn_max_d[cp9map->nd2lpos[n]];
3686 /* 12.19.05 - trying to deal with the right delete off-by-one
3687 * inverted relative to left delete issue.
3688 */
3689 tmp_jmin = (pn_min_d[cp9map->nd2rpos[n]] < nss_jmin[n+1]) ?
3690 pn_min_d[cp9map->nd2rpos[n]] : nss_jmin[n+1];
3691 tmp_jmax = (pn_max_d[cp9map->nd2rpos[n]] > nss_jmax[n+1]) ?
3692 pn_max_d[cp9map->nd2rpos[n]] : nss_jmax[n+1];
3693 hmm2ij_split_state_step1_set_state_bands(v, n, tmp_imin, tmp_imax, tmp_jmin,
3694 tmp_jmax, imin, imax, jmin, jmax,
3695 nss_imin, nss_imax, nss_jmin, nss_jmax);
3696 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, safe_imax,
3697 safe_jmin);
3698 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
3699 hmm2ij_state_step4_update_safe_holders(v, n, imin[v], jmax[v], nss_max_imin, nss_min_jmax);
3700 hmm2ij_state_step5_non_emitter_d0_hack(v, imax[v], jmin);
3701
3702 v++; /*MATP_IL*/
3703 /* This state maps to the insert state of HMM node cp9map->cs2hn[v][0]*/
3704 tmp_imin = pn_min_i[cp9map->cs2hn[v][0]]; /* insert states can only map to 1 HMM node */
3705 tmp_imax = pn_max_i[cp9map->cs2hn[v][0]]; /* insert states can only map to 1 HMM node */
3706 tmp_jmin = nss_jmin[n];
3707 tmp_jmax = nss_jmax[n];
3708 hmm2ij_insert_state_step1_set_state_bands(v, tmp_imin, tmp_imax, tmp_jmin,
3709 tmp_jmax, imin, imax, jmin, jmax);
3710 /* Enforce safe transitions, this makes sure that at least one state
3711 * y \in C_v is reachable from v. And further (special case for inserts)
3712 * make sure that we don't consider v as a possible y. IF we did, we might
3713 * be faced with a situation where v could only transit to itself, and then
3714 * we'd be in the same situation, but we may no longer be able to transit ANYWHERE
3715 * including to itself.
3716 */
3717 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, (nss_max_imin[n+1]), nss_min_jmax[n+1]);
3718 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
3719
3720 v++; /*MATP_IR*/
3721 /* skip detached inserts */
3722 if(cp9map->cs2hn[v][0] == -1)
3723 continue;
3724 /* Special case, one of only two situations (other is ROOT_IR)
3725 * we could have come where v is an insert, and a possible
3726 * state x that we came from is an insert, but x != y (x can be the MATP_IL).
3727 * So we have to determine imin and imax carefully.
3728 */
3729 tmp_imin = (nss_imin[n] < imin[v-1]) ?
3730 nss_imin[n] : imin[v-1];
3731 tmp_imax = (nss_imax[n] > imax[v-1]) ?
3732 nss_imax[n] : imax[v-1];
3733 /* This state maps to the insert state of HMM node cp9map->cs2hn[v][0]*/
3734 tmp_jmin = pn_min_i[cp9map->cs2hn[v][0]];
3735 tmp_jmax = pn_max_i[cp9map->cs2hn[v][0]];
3736 hmm2ij_insert_state_step1_set_state_bands(v, tmp_imin, tmp_imax, tmp_jmin,
3737 tmp_jmax, imin, imax, jmin, jmax);
3738 /* Enforce safe transitions, this makes sure that at least one state
3739 * y \in C_v is reachable from v. And further (special case for inserts)
3740 * make sure that we don't consider v as a possible y. IF we did, we might
3741 * be faced with a situation where v could only transit to itself, and then
3742 * we'd be in the same situation, but we may no longer be able to transit ANYWHERE
3743 * including to itself.
3744 */
3745 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, (nss_max_imin[n+1]), nss_min_jmax[n+1]);
3746 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
3747 break;
3748
3749 case MATL_nd:
3750 hmm2ij_prestate_step0_initialize(n, nss_max_imin, nss_min_jmax, i0, j0);
3751 hmm2ij_prestate_step1_set_node_inserts(n, nis_imin, nis_imax, nis_jmin, nis_jmax,
3752 nss_imin, nss_imax, nss_jmin, nss_jmax,
3753 pn_min_i, pn_max_i, cp9map);
3754
3755 hmm2ij_prestate_step2_determine_safe(n, nss_max_imin[n+1], nss_min_jmax[n+1],
3756 nis_imin[n], nis_jmax[n],
3757 &safe_imax, &safe_jmin);
3758 hmm2ij_prestate_step3_preset_node_splits(n, nis_imin, nis_imax, nis_jmin, nis_jmax,
3759 nss_imin, nss_imax, nss_jmin, nss_jmax,
3760 pn_min_m, pn_max_m, pn_min_d, pn_max_d,
3761 cp9map);
3762
3763 /* 3 states MATL_ML, MATL_D, MATL_IL */
3764 v = cm->nodemap[n]; /* MATL_ML */
3765 tmp_imin = pn_min_m[cp9map->nd2lpos[n]];
3766 tmp_imax = pn_max_m[cp9map->nd2lpos[n]];
3767 tmp_jmin = nss_jmin[n+1];
3768 tmp_jmax = nss_jmax[n+1];
3769 hmm2ij_split_state_step1_set_state_bands(v, n, tmp_imin, tmp_imax, tmp_jmin,
3770 tmp_jmax, imin, imax, jmin, jmax,
3771 nss_imin, nss_imax, nss_jmin, nss_jmax);
3772 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, safe_imax,
3773 safe_jmin);
3774 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
3775 hmm2ij_state_step4_update_safe_holders(v, n, imin[v], jmax[v], nss_max_imin, nss_min_jmax);
3776
3777 v++; /*MATL_D*/
3778 /* this D-left state gets the delete band from the HMM node
3779 * that maps to the left side.
3780 */
3781 tmp_imin = pn_min_d[cp9map->nd2lpos[n]];
3782 tmp_imax = pn_max_d[cp9map->nd2lpos[n]];
3783 tmp_jmin = nss_jmin[n+1];
3784 tmp_jmax = nss_jmax[n+1];
3785 hmm2ij_split_state_step1_set_state_bands(v, n, tmp_imin, tmp_imax, tmp_jmin,
3786 tmp_jmax, imin, imax, jmin, jmax,
3787 nss_imin, nss_imax, nss_jmin, nss_jmax);
3788 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, safe_imax,
3789 safe_jmin);
3790 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
3791 hmm2ij_state_step4_update_safe_holders(v, n, imin[v], jmax[v], nss_max_imin, nss_min_jmax);
3792 hmm2ij_state_step5_non_emitter_d0_hack(v, imax[v], jmin);
3793
3794 v++; /*MATL_IL*/
3795 /* skip detached inserts */
3796 if(cp9map->cs2hn[v][0] == -1)
3797 continue;
3798 /* This state maps to the insert state of HMM node cp9map->cs2hn[v][0]*/
3799 tmp_imin = pn_min_i[cp9map->cs2hn[v][0]];
3800 tmp_imax = pn_max_i[cp9map->cs2hn[v][0]];
3801 tmp_jmin = nss_jmin[n];
3802 tmp_jmax = nss_jmax[n];
3803
3804 hmm2ij_insert_state_step1_set_state_bands(v, tmp_imin, tmp_imax, tmp_jmin,
3805 tmp_jmax, imin, imax, jmin, jmax);
3806 /* Enforce safe transitions, this makes sure that at least one state
3807 * y \in C_v is reachable from v. And further (special case for inserts)
3808 * make sure that we don't consider v as a possible y. IF we did, we might
3809 * be faced with a situation where v could only transit to itself, and then
3810 * we'd be in the same situation, but we may no longer be able to transit ANYWHERE
3811 * including to itself.
3812 */
3813 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, (nss_max_imin[n+1]), nss_min_jmax[n+1]);
3814 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
3815 break;
3816
3817 case MATR_nd:
3818 hmm2ij_prestate_step0_initialize(n, nss_max_imin, nss_min_jmax, i0, j0);
3819 hmm2ij_prestate_step1_set_node_inserts(n, nis_imin, nis_imax, nis_jmin, nis_jmax,
3820 nss_imin, nss_imax, nss_jmin, nss_jmax,
3821 pn_min_i, pn_max_i, cp9map);
3822
3823 hmm2ij_prestate_step2_determine_safe(n, nss_max_imin[n+1], nss_min_jmax[n+1],
3824 nis_imin[n], nis_jmax[n],
3825 &safe_imax, &safe_jmin);
3826 hmm2ij_prestate_step3_preset_node_splits(n, nis_imin, nis_imax, nis_jmin, nis_jmax,
3827 nss_imin, nss_imax, nss_jmin, nss_jmax,
3828 pn_min_m, pn_max_m, pn_min_d, pn_max_d,
3829 cp9map);
3830
3831 /* 3 states MATR_MR, MATR_D, MATR_IR */
3832 v = cm->nodemap[n]; /* MATR_MR */
3833 tmp_imin = nss_imin[n+1];
3834 tmp_imax = nss_imax[n+1];
3835 tmp_jmin = pn_min_m[cp9map->nd2rpos[n]];
3836 tmp_jmax = pn_max_m[cp9map->nd2rpos[n]];
3837 hmm2ij_split_state_step1_set_state_bands(v, n, tmp_imin, tmp_imax, tmp_jmin,
3838 tmp_jmax, imin, imax, jmin, jmax,
3839 nss_imin, nss_imax, nss_jmin, nss_jmax);
3840 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, safe_imax,
3841 safe_jmin);
3842 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
3843 hmm2ij_state_step4_update_safe_holders(v, n, imin[v], jmax[v], nss_max_imin, nss_min_jmax);
3844
3845 v++; /*MATR_D*/
3846 /* this D-left state gets the delete band from the HMM node
3847 * that maps to the left side.
3848 */
3849 tmp_imin = nss_imin[n+1];
3850 tmp_imax = nss_imax[n+1];
3851 /* 12.19.05 - trying to deal with the right delete off-by-one
3852 * inverted relative to left delete issue.
3853 */
3854 tmp_jmin = (pn_min_d[cp9map->nd2rpos[n]] < nss_jmin[n+1]) ?
3855 pn_min_d[cp9map->nd2rpos[n]] : nss_jmin[n+1];
3856 tmp_jmax = (pn_max_d[cp9map->nd2rpos[n]] > nss_jmax[n+1]) ?
3857 pn_max_d[cp9map->nd2rpos[n]] : nss_jmax[n+1];
3858 hmm2ij_split_state_step1_set_state_bands(v, n, tmp_imin, tmp_imax, tmp_jmin,
3859 tmp_jmax, imin, imax, jmin, jmax,
3860 nss_imin, nss_imax, nss_jmin, nss_jmax);
3861 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, safe_imax,
3862 safe_jmin);
3863 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
3864 hmm2ij_state_step4_update_safe_holders(v, n, imin[v], jmax[v], nss_max_imin, nss_min_jmax);
3865 hmm2ij_state_step5_non_emitter_d0_hack(v, imax[v], jmin);
3866
3867 v++; /*MATR_IR*/
3868 /* skip detached inserts */
3869 if(cp9map->cs2hn[v][0] == -1)
3870 continue;
3871 tmp_imin = nss_imin[n];
3872 tmp_imax = nss_imax[n];
3873 /* This state maps to the insert state of HMM node cshn_map[v]*/
3874 tmp_jmin = pn_min_i[cp9map->cs2hn[v][0]];
3875 tmp_jmax = pn_max_i[cp9map->cs2hn[v][0]];
3876 hmm2ij_insert_state_step1_set_state_bands(v, tmp_imin, tmp_imax, tmp_jmin,
3877 tmp_jmax, imin, imax, jmin, jmax);
3878 /* Enforce safe transitions, this makes sure that at least one state
3879 * y \in C_v is reachable from v. And further (special case for inserts)
3880 * make sure that we don't consider v as a possible y. IF we did, we might
3881 * be faced with a situation where v could only transit to itself, and then
3882 * we'd be in the same situation, but we may no longer be able to transit ANYWHERE
3883 * including to itself.
3884 */
3885 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, (nss_max_imin[n+1]), nss_min_jmax[n+1]);
3886 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
3887 break;
3888
3889 case ROOT_nd:
3890 hmm2ij_prestate_step0_initialize(n, nss_max_imin, nss_min_jmax, i0, j0);
3891 hmm2ij_prestate_step1_set_node_inserts(n, nis_imin, nis_imax, nis_jmin, nis_jmax,
3892 nss_imin, nss_imax, nss_jmin, nss_jmax,
3893 pn_min_i, pn_max_i, cp9map);
3894
3895 hmm2ij_prestate_step2_determine_safe(n, nss_max_imin[n+1], nss_min_jmax[n+1],
3896 nis_imin[n], nis_jmax[n],
3897 &safe_imax, &safe_jmin);
3898 hmm2ij_prestate_step3_preset_node_splits(n, nis_imin, nis_imax, nis_jmin, nis_jmax,
3899 nss_imin, nss_imax, nss_jmin, nss_jmax,
3900 pn_min_m, pn_max_m, pn_min_d, pn_max_d,
3901 cp9map);
3902 /* 3 states, ROOT_S, ROOT_IL, and ROOT_IR*/
3903 v = cm->nodemap[n]; /* ROOT_S SPECIAL CASE */
3904 if(doing_search) { /* we're doing search, ROOT_S doesn't necessarily emit full sequence */
3905 tmp_imin = nss_imin[n+1];
3906 tmp_imax = nss_imax[n+1];
3907 tmp_jmin = nss_jmin[n+1];
3908 tmp_jmax = nss_jmax[n+1];
3909 }
3910 else { /* we're doing alignment, enforce ROOT_S emits full sequence */
3911 /* for now, enforce ROOT_S emits full sequence at end of the function, we'll relax this if doing_search==TRUE */
3912 tmp_imin = i0;
3913 tmp_imax = i0;
3914 tmp_jmin = j0;
3915 tmp_jmax = j0;
3916 }
3917 hmm2ij_split_state_step1_set_state_bands(v, n, tmp_imin, tmp_imax, tmp_jmin,
3918 tmp_jmax, imin, imax, jmin, jmax,
3919 nss_imin, nss_imax, nss_jmin, nss_jmax);
3920 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, safe_imax,
3921 safe_jmin);
3922 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
3923 hmm2ij_state_step4_update_safe_holders(v, n, imin[v], jmax[v], nss_max_imin, nss_min_jmax);
3924
3925 v++; /*ROOT_IL SPECIAL CASE*/
3926 /* This state maps to the insert state of HMM node cp9map->cs2hn[v][0], which is HMM node 0*/
3927 if(doing_search)
3928 tmp_imin = pn_min_i[cp9map->cs2hn[v][0]]; /* should this be imin[0]? */
3929 else
3930 tmp_imin = i0; /* Have to be able to transit here from ROOT_S */
3931 tmp_imax = nss_imax[n+1];
3932 if(doing_search) {
3933 tmp_jmin = nss_jmin[n+1];
3934 tmp_jmax = nss_jmax[n+1];
3935 }
3936 else {
3937 tmp_jmin = j0; /* we never emit to the right in this state */
3938 tmp_jmax = j0; /* we never emit to the right in this state */
3939 }
3940 hmm2ij_insert_state_step1_set_state_bands(v, tmp_imin, tmp_imax, tmp_jmin,
3941 tmp_jmax, imin, imax, jmin, jmax);
3942 /* Enforce safe transitions, this makes sure that at least one state
3943 * y \in C_v is reachable from v. And further (special case for inserts)
3944 * make sure that we don't consider v as a possible y. IF we did, we might
3945 * be faced with a situation where v could only transit to itself, and then
3946 * we'd be in the same situation, but we may no longer be able to transit ANYWHERE
3947 * including to itself.
3948 */
3949 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, (nss_max_imin[n+1]), nss_min_jmax[n+1]);
3950 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
3951
3952 v++; /*ROOT_IR SPECIAL CASE analagous to ROOT_IL*/
3953 if(doing_search)
3954 tmp_imin = nss_imin[n+1]; /* same tmp_imin as ROOT_S */
3955 else
3956 tmp_imin = i0; /* we never emit to the left in this state */
3957 tmp_imax = nss_imax[n+1];
3958 tmp_jmin = nss_jmin[n+1];
3959 tmp_jmax = j0; /* Have to be able to transit here from ROOT_S */
3960 hmm2ij_insert_state_step1_set_state_bands(v, tmp_imin, tmp_imax, tmp_jmin,
3961 tmp_jmax, imin, imax, jmin, jmax);
3962 /* Enforce safe transitions, this makes sure that at least one state
3963 * y \in C_v is reachable from v. And further (special case for inserts)
3964 * make sure that we don't consider v as a possible y. IF we did, we might
3965 * be faced with a situation where v could only transit to itself, and then
3966 * we'd be in the same situation, but we may no longer be able to transit ANYWHERE
3967 * including to itself.
3968 */
3969 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, (nss_max_imin[n+1]), nss_min_jmax[n+1]);
3970 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
3971 break;
3972
3973 case BEGL_nd:
3974 hmm2ij_prestate_step0_initialize(n, nss_max_imin, nss_min_jmax, i0, j0);
3975 hmm2ij_prestate_step1_set_node_inserts(n, nis_imin, nis_imax, nis_jmin, nis_jmax,
3976 nss_imin, nss_imax, nss_jmin, nss_jmax,
3977 pn_min_i, pn_max_i, cp9map);
3978
3979 hmm2ij_prestate_step2_determine_safe(n, nss_max_imin[n+1], nss_min_jmax[n+1],
3980 nis_imin[n], nis_jmax[n],
3981 &safe_imax, &safe_jmin);
3982 hmm2ij_prestate_step3_preset_node_splits(n, nis_imin, nis_imax, nis_jmin, nis_jmax,
3983 nss_imin, nss_imax, nss_jmin, nss_jmax,
3984 pn_min_m, pn_max_m, pn_min_d, pn_max_d,
3985 cp9map);
3986 /* 1 state BEGL_S */
3987 v = cm->nodemap[n];
3988 /* The next node MUST be a match node (MATP
3989 * specifically due to model building
3990 * algorithm) or a BIF node. We derive imin, imax,
3991 * jmin and jmax from that node.
3992 */
3993 /* Use the next nodes split set band, which
3994 * will be wider of match and delete states bands
3995 * for split set states in next node.
3996 */
3997 tmp_imin = nss_imin[n+1];
3998 tmp_imax = nss_imax[n+1];
3999 tmp_jmin = nss_jmin[n+1];
4000 tmp_jmax = nss_jmax[n+1];
4001 hmm2ij_split_state_step1_set_state_bands(v, n, tmp_imin, tmp_imax, tmp_jmin,
4002 tmp_jmax, imin, imax, jmin, jmax,
4003 nss_imin, nss_imax, nss_jmin, nss_jmax);
4004 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, safe_imax,
4005 safe_jmin);
4006 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
4007 hmm2ij_state_step4_update_safe_holders(v, n, imin[v], jmax[v], nss_max_imin, nss_min_jmax);
4008 hmm2ij_state_step5_non_emitter_d0_hack(v, imax[v], jmin);
4009 break;
4010
4011 case BEGR_nd:
4012 hmm2ij_prestate_step0_initialize(n, nss_max_imin, nss_min_jmax, i0, j0);
4013 hmm2ij_prestate_step1_set_node_inserts(n, nis_imin, nis_imax, nis_jmin, nis_jmax,
4014 nss_imin, nss_imax, nss_jmin, nss_jmax,
4015 pn_min_i, pn_max_i, cp9map);
4016
4017 hmm2ij_prestate_step2_determine_safe(n, nss_max_imin[n+1], nss_min_jmax[n+1],
4018 nis_imin[n], nis_jmax[n],
4019 &safe_imax, &safe_jmin);
4020 hmm2ij_prestate_step3_preset_node_splits(n, nis_imin, nis_imax, nis_jmin, nis_jmax,
4021 nss_imin, nss_imax, nss_jmin, nss_jmax,
4022 pn_min_m, pn_max_m, pn_min_d, pn_max_d,
4023 cp9map);
4024 /* 2 states BEGR_S and BEGR_IL */
4025 v = cm->nodemap[n]; /*BEGR_S*/
4026 /* Use either the next nodes split set band, which
4027 * will be wider of match and delete states bands
4028 * for split set states in next node OR
4029 * the band on the insert state that maps to the
4030 * BEGR_IL, erring on the safe side (wider band).
4031 */
4032 tmp_imin = nss_imin[n+1];
4033 tmp_imax = nss_imax[n+1];
4034 if(pn_min_i[cp9map->cs2hn[v+1][0]] < tmp_imin)
4035 tmp_imin = pn_min_i[cp9map->cs2hn[v+1][0]];
4036 if(pn_max_i[cp9map->cs2hn[v+1][0]] > tmp_imax)
4037 tmp_imax = pn_max_i[cp9map->cs2hn[v+1][0]];
4038 tmp_jmin = nss_jmin[n+1];
4039 tmp_jmax = nss_jmax[n+1];
4040 hmm2ij_split_state_step1_set_state_bands(v, n, tmp_imin, tmp_imax, tmp_jmin,
4041 tmp_jmax, imin, imax, jmin, jmax,
4042 nss_imin, nss_imax, nss_jmin, nss_jmax);
4043 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, safe_imax,
4044 safe_jmin);
4045 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
4046 hmm2ij_state_step4_update_safe_holders(v, n, imin[v], jmax[v], nss_max_imin, nss_min_jmax);
4047 hmm2ij_state_step5_non_emitter_d0_hack(v, imax[v], jmin);
4048
4049 v++; /*BEGR_IL*/
4050 /* This state maps to the insert state of HMM node cp9map->cs2hn[v][0]*/
4051 tmp_imin = pn_min_i[cp9map->cs2hn[v][0]];
4052 tmp_imax = pn_max_i[cp9map->cs2hn[v][0]];
4053 tmp_jmin = nss_jmin[n+1];
4054 tmp_jmax = nss_jmax[n+1];
4055 hmm2ij_insert_state_step1_set_state_bands(v, tmp_imin, tmp_imax, tmp_jmin,
4056 tmp_jmax, imin, imax, jmin, jmax);
4057 /* Enforce safe transitions, this makes sure that at least one state
4058 * y \in C_v is reachable from v. And further (special case for inserts)
4059 * make sure that we don't consider v as a possible y. IF we did, we might
4060 * be faced with a situation where v could only transit to itself, and then
4061 * we'd be in the same situation, but we may no longer be able to transit ANYWHERE
4062 * including to itself.
4063 */
4064 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, (nss_max_imin[n+1]), nss_min_jmax[n+1]);
4065 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
4066 break;
4067
4068 case BIF_nd:
4069 hmm2ij_prestate_step0_initialize(n, nss_max_imin, nss_min_jmax, i0, j0);
4070
4071 /* 1 state BIF_B */
4072 v = cm->nodemap[n]; /*BIF_B*/
4073 /* The only two connected states are BEGL_S and BEGR_S.
4074 * We can derive our imin, imax, jmin, and jmax from
4075 * those two states.
4076 * cm->cfirst[v] is the state index of the left child.
4077 * cm->cnum[v] is the state index of the right child.
4078 */
4079 nis_imin[n] = imin[cm->cfirst[v]];
4080 nis_imax[n] = imax[cm->cfirst[v]];
4081 nis_jmin[n] = jmin[cm->cnum[v]];
4082 nis_jmax[n] = jmax[cm->cnum[v]];
4083
4084 nss_imin[n] = imin[cm->cfirst[v]];
4085 nss_imax[n] = imax[cm->cfirst[v]];
4086 nss_jmin[n] = jmin[cm->cnum[v]];
4087 nss_jmax[n] = jmax[cm->cnum[v]];
4088
4089 hmm2ij_prestate_step2_determine_safe(n, nss_max_imin[n+1], nss_min_jmax[n+1],
4090 nis_imin[n], nis_jmax[n],
4091 &safe_imax, &safe_jmin);
4092 tmp_imin = imin[cm->cfirst[v]];
4093 tmp_imax = imax[cm->cfirst[v]];
4094 tmp_jmin = jmin[cm->cnum[v]];
4095 tmp_jmax = jmax[cm->cnum[v]];
4096 hmm2ij_split_state_step1_set_state_bands(v, n, tmp_imin, tmp_imax, tmp_jmin,
4097 tmp_jmax, imin, imax, jmin, jmax,
4098 nss_imin, nss_imax, nss_jmin, nss_jmax);
4099 hmm2ij_state_step2_enforce_safe_trans(cm, v, n, imax, jmin, nss_imax, nss_jmin, safe_imax,
4100 safe_jmin);
4101 hmm2ij_state_step3_enforce_state_delta(cm, v, jmin, jmax);
4102 hmm2ij_state_step4_update_safe_holders(v, n, imin[v], jmax[v], nss_max_imin, nss_min_jmax);
4103 hmm2ij_state_step5_non_emitter_d0_hack(v, imax[v], jmin);
4104 break;
4105 }
4106 }
4107
4108 /* Tie up some loose ends:
4109 * 1. Ensure that all valid i are >= i0 and all valid j are <= j0
4110 * 2. Ensure all bands have bandwidth >= 0 (see code)
4111 * 3. Set detached inserts states to imin=imax=jmin=jmax=i0 to avoid
4112 * problems in downstream functions. These states WILL NEVER BE ENTERED
4113 * 4. Do a quick check to make sure we've assigned the bands
4114 * on i and j for all states to positive values (none were
4115 * left as -1 EXCEPT for end states which should have i bands left as -1).
4116 * 5. Ensure imin[0] <= imin[v] for all v and jmax[0] >= jmax[v] for all v.
4117 * 6. If doing_search==TRUE, rewrite the bands on the
4118 * ROOT_S state so they allow any possible transition to a child
4119 * that the child's bands would allow.
4120 */
4121
4122 /* 1. Ensure that all valid i are >= i0 and all valid j are <= j0 */
4123 for(v = 0; v < cm->M; v++) {
4124 imin[v] = ESL_MAX(imin[v], i0); /* imin[v] can't be less than i0 */
4125 imax[v] = ESL_MAX(imax[v], i0); /* imax[v] can't be less than i0 */
4126
4127 imin[v] = ESL_MIN(imin[v], j0); /* imin[v] can't be more than j0 */
4128 imax[v] = ESL_MIN(imax[v], j0); /* imax[v] can't be more than j0 */
4129
4130 imax[v] = ESL_MIN(imax[v], j0); /* imax[v] can't be more than j0 */
4131
4132 jmin[v] = ESL_MIN(jmin[v], j0); /* jmin[v] can't be more than j0 */
4133 jmax[v] = ESL_MIN(jmax[v], j0); /* jmax[v] can't be more than j0 */
4134
4135 jmin[v] = ESL_MAX(jmin[v], i0); /* jmin[v] can't be less than i0 */
4136 jmax[v] = ESL_MAX(jmax[v], i0); /* jmax[v] can't be less than i0 */
4137
4138 /* 2. Ensure all bands have bandwidth >= 0
4139 * Ensure: jmax[v] - jmin[v] + 1 >= 0
4140 * imax[v] - imin[v] + 1 >= 0
4141 * jmax[v] - jmin[v] + 1 == 0 means there are no valid j's for state v,
4142 * so state v is not allowed to be in the parse, we allow this (maybe we shouldn't)
4143 */
4144 imax[v] = ESL_MAX(imax[v], imin[v]-1);
4145 jmin[v] = ESL_MIN(jmin[v], jmax[v]+1);
4146
4147 /* 3. Set detached inserts states to imin=imax=jmin=jmax=i0 to avoid
4148 * problems in downstream functions. These states WILL NEVER BE ENTERED
4149 */
4150 if(cm->sttype[v+1] == E_st) imin[v] = imax[v] = jmin[v] = jmax[v] = i0;
4151
4152 /* 4. Do a quick check to make sure we've assigned the bands
4153 * on i and j for all states to positive values (none were
4154 * left as -1 EXCEPT for end states which should have i bands left as -1).
4155 */
4156 ESL_DASSERT1((! ((cm->sttype[v] != E_st) && (imin[v] == -1))));
4157 ESL_DASSERT1((! ((cm->sttype[v] != E_st) && (imax[v] == -1))));
4158 ESL_DASSERT1((! ((cm->sttype[v] != E_st) && (jmin[v] == -1))));
4159 ESL_DASSERT1((! ((cm->sttype[v] != E_st) && (jmax[v] == -1))));
4160
4161 /* 5. Ensure imin[0] <= imin[v] for all v and jmax[0] >= jmax[v] for all v. */
4162 imin[0] = ESL_MIN(imin[0], imin[v]);
4163 jmax[0] = ESL_MAX(jmax[0], jmax[v]);
4164 }
4165
4166 /* 6. If doing_search==TRUE, rewrite the bands on the
4167 * ROOT_S state so they allow any possible transition to a child
4168 * that the child's bands would allow.
4169 */
4170 if(doing_search) {
4171 /* First look at children of 0 (these probs will be 0. if local begins on, but it doesn't matter for our purposes here) */
4172 for (yoffset = 0; yoffset < cm->cnum[0]; yoffset++) {
4173 y = cm->cnum[0] + yoffset;
4174 imin[0] = ESL_MIN(imin[0], imin[y]);
4175 imax[0] = ESL_MAX(imax[0], imax[y]);
4176 jmin[0] = ESL_MIN(jmin[0], jmin[y]);
4177 jmax[0] = ESL_MAX(jmax[0], jmax[y]);
4178 }
4179 /* now for possible local begins */
4180 if(cm->flags & CMH_LOCAL_BEGIN) {
4181 for (y = 1; y < cm->M; y++) {
4182 if(NOT_IMPOSSIBLE(cm->beginsc[y])) {
4183 imin[0] = ESL_MIN(imin[0], imin[y]);
4184 imax[0] = ESL_MAX(imax[0], imax[y]);
4185 jmin[0] = ESL_MIN(jmin[0], jmin[y]);
4186 jmax[0] = ESL_MAX(jmax[0], jmax[y]);
4187 }
4188 }
4189 }
4190 }
4191 /* Final, exceedingly rare, special case */
4192 if(i0 == j0) { /* special case that breaks DP recursion for MP states
4193 * b/c target seq is length 1, and all MPs are impossible,
4194 * yet above code just forced jmin[v] <= j0 and jmax[v] <= j0,
4195 * which says that MPs are possible.
4196 */
4197 for(v = 0; v < cm->M; v++) {
4198 if(cm->sttype[v] == MP_st) {
4199 jmin[v] = j0+1;
4200 jmax[v] = j0;
4201 /* now 'for (j = jmin[v]; j <= jmax[v]; j++)' { loops will never be entered, b/c jmin[v] == 2, jmax[v] == 1 */
4202 }
4203 }
4204 }
4205
4206 #if 0
4207 /* OLD CODE EPN, Fri Dec 21 09:14:32 2007 */
4208 /* Tie up some loose ends:
4209 * 1. Set detached inserts states to imin=imax=jmin=jmax=i0 to avoid
4210 * problems in downstream functions. These states WILL NEVER BE ENTERED
4211 * 2. Do a quick check to make sure we've assigned the bands
4212 * on i and j for all states to positive values (none were
4213 * left as -1 EXCEPT for end states which should have i bands left as -1).
4214 * 3. Ensure that all *max[v] and *min[v] values are <= L, values greater
4215 * than this don't make sense.
4216 */
4217
4218 for(v = 0; v < cm->M; v++) {
4219 /* set bands for detached inserts */
4220 if(cm->sttype[v+1] == E_st) imin[v] = imax[v] = jmin[v] = jmax[v] = i0;
4221
4222 /* Ensure: for all i imin[v]..i..imax[v]
4223 * i0 <= i <= j0+1
4224 * for all j jmin[v]..j..jmax[v]
4225 * i0 <= j <= j0
4226 * Note: i can be j0+1 to allow delete states to be entered with
4227 * d = 0, after the entire seq has been emitted.
4228 */
4229 imin[v] = ESL_MAX(imin[v], i0);
4230 imin[v] = ESL_MIN(imin[v], j0+1);
4231 imax[v] = ESL_MAX(imax[v], i0);
4232 imax[v] = ESL_MIN(imax[v], j0+1);
4233 jmin[v] = ESL_MAX(jmin[v], i0);
4234 jmin[v] = ESL_MIN(jmin[v], j0);
4235 jmax[v] = ESL_MAX(jmax[v], i0);
4236 jmax[v] = ESL_MIN(jmax[v], j0);
4237
4238 /* Ensure: for all v imin[v] >= imin[0],
4239 * jmax[v] <= jmax[0].
4240 */
4241 imin[v] = ESL_MAX(imin[v], imin[0]);
4242 imax[v] = ESL_MAX(imax[v], imin[0]);
4243 jmax[v] = ESL_MIN(jmax[v], jmax[0]);
4244 jmin[v] = ESL_MIN(jmin[v], jmax[0]);
4245
4246 /* Ensure: jmax[v] - jmin[v] + 1 >= 0
4247 * imax[v] - imin[v] + 1 >= 0
4248 * jmax[v] - jmin[v] + 1 == 0 means there are no valid j's for state v,
4249 * so state v is not allowed to be in the parse, we allow this (maybe we shouldn't)
4250 */
4251 imin[v] = ESL_MIN(imin[v], imax[v]+1);
4252 jmin[v] = ESL_MIN(jmin[v], jmax[v]+1);
4253
4254 ESL_DASSERT1((! ((cm->sttype[v] != E_st) && (imin[v] == -1))));
4255 ESL_DASSERT1((! ((cm->sttype[v] != E_st) && (imax[v] == -1))));
4256 ESL_DASSERT1((! ((cm->sttype[v] != E_st) && (jmin[v] == -1))));
4257 ESL_DASSERT1((! ((cm->sttype[v] != E_st) && (jmax[v] == -1))));
4258 }
4259 #endif
4260
4261 /* debug_print_ij_bands(cm); */
4262
4263 free(nss_imin);
4264 free(nss_imax);
4265 free(nss_jmin);
4266 free(nss_jmax);
4267 free(nis_imin);
4268 free(nis_imax);
4269 free(nis_jmin);
4270 free(nis_jmax);
4271 free(nss_max_imin);
4272 free(nss_min_jmax);
4273 return eslOK;
4274
4275 ERROR:
4276 ESL_FAIL(status, errbuf, "Memory allocation error.\n");
4277 }
4278
4279 /**************************************************************************
4280 * Helper functions for *_cp9_HMM2ijBands_OLD()
4281 * hmm2ij_prestate_step0_initialize()
4282 * hmm2ij_prestate_step1_set_node_inserts()
4283 * hmm2ij_prestate_step2_determine_safe()
4284 * hmm2ij_prestate_step3_preset_node_splits()
4285 * hmm2ij_split_state_step1_set_state_bands()
4286 * hmm2ij_insert_state_step1_set_state_bands()
4287 * hmm2ij_state_step2_enforce_safe_trans()
4288 * hmm2ij_state_step5_non_emitter_d0_hack()
4289 */
4290
4291 /*****************************************************************************
4292 * EPN 12.21.05
4293 * Function: hmm2ij_prestate_step0_initialize
4294 *
4295 * Purpose: cp9_HMM2ijBands_OLD*() function helper function.
4296 *
4297 *****************************************************************************/
4298 void
4299 hmm2ij_prestate_step0_initialize(int n, int *nss_max_imin, int *nss_min_jmax, int i0, int j0)
4300 {
4301 nss_max_imin[n] = i0-1;
4302 nss_min_jmax[n] = j0;
4303 }
4304
4305 /*****************************************************************************
4306 * EPN 12.21.05
4307 * Function: hmm2ij_prestate_step1_set_node_inserts
4308 *
4309 * Purpose: cp9_HMM2ijBands_OLD*() function helper function.
4310 *
4311 *****************************************************************************/
4312 void
4313 hmm2ij_prestate_step1_set_node_inserts(int n, int *nis_imin, int *nis_imax,
4314 int *nis_jmin, int *nis_jmax,
4315 int *nss_imin, int *nss_imax,
4316 int *nss_jmin, int *nss_jmax,
4317 int *pn_min_i, int *pn_max_i,
4318 CP9Map_t *cp9map)
4319
4320 {
4321 if(cp9map->nd2lpos[n] != -1)
4322 {
4323 nis_imin[n] = pn_min_i[cp9map->nd2lpos[n]];
4324 nis_imax[n] = pn_max_i[cp9map->nd2lpos[n]];
4325 }
4326 else
4327 {
4328 nis_imin[n] = nss_imin[n+1];
4329 nis_imax[n] = nss_imax[n+1];
4330 }
4331 if(cp9map->nd2rpos[n] != -1)
4332 {
4333 nis_jmin[n] = pn_min_i[cp9map->nd2rpos[n]];
4334 nis_jmax[n] = pn_max_i[cp9map->nd2rpos[n]];
4335 }
4336 else
4337 {
4338 nis_jmin[n] = nss_jmin[n+1];
4339 nis_jmax[n] = nss_jmax[n+1];
4340 }
4341 }
4342 /*****************************************************************************
4343 * EPN 12.21.05
4344 * Function: hmm2ij_prestate_step1_set_node_inserts
4345 *
4346 * Purpose: cp9_HMM2ijBands_OLD*() function helper function.
4347 *
4348 *****************************************************************************/
4349 void
4350 hmm2ij_prestate_step2_determine_safe(int n,
4351 int nss_max_imin_np1, int nss_min_jmax_np1,
4352 int nis_imin_n,
4353 int nis_jmax_n,
4354 int *safe_imax, int *safe_jmin)
4355 {
4356 *safe_imax = (nss_max_imin_np1 < nis_imin_n) ?
4357 nss_max_imin_np1 : nis_imin_n;
4358 *safe_jmin = (nss_min_jmax_np1 > nis_jmax_n) ?
4359 nss_min_jmax_np1 : nis_jmax_n;
4360 }
4361 /*****************************************************************************
4362 * EPN 12.21.05
4363 * Function: hmm2ij_prestate_step1_set_node_inserts
4364 *
4365 * Purpose: cp9_HMM2ijBands_OLD*() function helper function.
4366 *
4367 *****************************************************************************/
4368 void
4369 hmm2ij_prestate_step3_preset_node_splits(int n, int *nis_imin, int *nis_imax,
4370 int *nis_jmin, int *nis_jmax,
4371 int *nss_imin, int *nss_imax,
4372 int *nss_jmin, int *nss_jmax,
4373 int *pn_min_m, int *pn_max_m,
4374 int *pn_min_d, int *pn_max_d,
4375 CP9Map_t *cp9map)
4376 {
4377 if(cp9map->nd2lpos[n] != -1)
4378 {
4379 nss_imin[n] = (pn_min_m[cp9map->nd2lpos[n]] < (pn_min_d[cp9map->nd2lpos[n]])) ?
4380 pn_min_m[cp9map->nd2lpos[n]] : (pn_min_d[cp9map->nd2lpos[n]]);
4381 nss_imax[n] = (pn_max_m[cp9map->nd2lpos[n]] > (pn_max_d[cp9map->nd2lpos[n]])) ?
4382 pn_max_m[cp9map->nd2lpos[n]] : (pn_max_d[cp9map->nd2lpos[n]]);
4383 }
4384 else
4385 {
4386 nss_imin[n] = nss_imin[n+1];
4387 nss_imax[n] = nss_imax[n+1];
4388 }
4389 if(cp9map->nd2rpos[n] != -1)
4390 {
4391 nss_jmin[n] = (pn_min_m[cp9map->nd2rpos[n]] < pn_min_d[cp9map->nd2rpos[n]]) ?
4392 pn_min_m[cp9map->nd2rpos[n]] : pn_min_d[cp9map->nd2rpos[n]];
4393 nss_jmax[n] = (pn_max_m[cp9map->nd2rpos[n]] > pn_max_d[cp9map->nd2rpos[n]]) ?
4394 pn_max_m[cp9map->nd2rpos[n]] : pn_max_d[cp9map->nd2rpos[n]];
4395 }
4396 else
4397 {
4398 nss_jmin[n] = nss_jmin[n+1];
4399 nss_jmax[n] = nss_jmax[n+1];
4400 }
4401 }
4402
4403 /*****************************************************************************
4404 * EPN 12.21.05
4405 * Function: hmm2ij_split_state_step1_set_state_bands
4406 *
4407 * Purpose: cp9_HMM2ijBands_OLD*() function helper function.
4408 *
4409 *****************************************************************************/
4410 void
4411 hmm2ij_split_state_step1_set_state_bands(int v, int n,
4412 int tmp_imin, int tmp_imax,
4413 int tmp_jmin, int tmp_jmax,
4414 int *imin, int *imax, int *jmin, int *jmax,
4415 int *nss_imin, int *nss_imax,
4416 int *nss_jmin, int *nss_jmax)
4417 {
4418 imin[v] = tmp_imin;
4419 imax[v] = tmp_imax;
4420 jmin[v] = tmp_jmin;
4421 jmax[v] = tmp_jmax;
4422 if(imin[v] < nss_imin[n])
4423 nss_imin[n] = imin[v];
4424 if(imax[v] > nss_imax[n])
4425 nss_imax[n] = imax[v];
4426 if(jmin[v] < nss_jmin[n])
4427 nss_jmin[n] = jmin[v];
4428 if(jmax[v] > nss_jmax[n])
4429 nss_jmax[n] = jmax[v];
4430
4431 }
4432 /*****************************************************************************
4433 * EPN 12.21.05
4434 * Function: hmm2ij_prestate_step1_set_node_inserts
4435 *
4436 * Purpose: cp9_HMM2ijBands_OLD*() function helper function.
4437 *
4438 *****************************************************************************/
4439 void hmm2ij_insert_state_step1_set_state_bands(int v,
4440 int tmp_imin, int tmp_imax,
4441 int tmp_jmin, int tmp_jmax,
4442 int *imin, int *imax, int *jmin, int *jmax)
4443 {
4444 imin[v] = tmp_imin;
4445 imax[v] = tmp_imax;
4446 jmin[v] = tmp_jmin;
4447 jmax[v] = tmp_jmax;
4448 }
4449 /*****************************************************************************
4450 * EPN 12.21.05
4451 * Function: hmm2ij_state_step2_enforce_safe_trans
4452 *
4453 * Purpose: cp9_HMM2ijBands_OLD*() function helper function.
4454 *
4455 *****************************************************************************/
4456 void
4457 hmm2ij_state_step2_enforce_safe_trans(CM_t *cm, int v, int n, int *imax, int *jmin,
4458 int *nss_imax, int *nss_jmin,
4459 int safe_imax, int safe_jmin)
4460 {
4461 int dv_l;
4462 int dv_r;
4463 if((cm->sttype[v] == ML_st) ||
4464 (cm->sttype[v] == IL_st) ||
4465 (cm->sttype[v] == MP_st))
4466 dv_l = 1;
4467 else
4468 dv_l = 0;
4469 if((cm->sttype[v] == MR_st) ||
4470 (cm->sttype[v] == IR_st) ||
4471 (cm->sttype[v] == MP_st))
4472 dv_r = 1;
4473 else
4474 dv_r = 0;
4475 if(imax[v] < safe_imax - dv_l)
4476 {
4477 imax[v] = safe_imax - dv_l;
4478 if(imax[v] > nss_imax[n])
4479 nss_imax[n] = imax[v];
4480 }
4481 if(jmin[v] > safe_jmin + dv_r)
4482 {
4483 jmin[v] = safe_jmin + dv_r;
4484 if(jmin[v] < nss_jmin[n])
4485 nss_jmin[n] = jmin[v];
4486 }
4487 }
4488
4489 /*****************************************************************************
4490 * EPN 12.21.05
4491 * Function: hmm2ij_state_step3_enforce_state_delta
4492 *
4493 * Purpose: cp9_HMM2ijBands_OLD*() function helper function.
4494 *
4495 *****************************************************************************/
4496 void
4497 hmm2ij_state_step3_enforce_state_delta(CM_t *cm, int v, int *jmin, int *jmax)
4498 {
4499 int dv_l;
4500 int dv_r;
4501 if((cm->sttype[v] == ML_st) ||
4502 (cm->sttype[v] == IL_st) ||
4503 (cm->sttype[v] == MP_st))
4504 dv_l = 1;
4505 else
4506 dv_l = 0;
4507 if((cm->sttype[v] == MR_st) ||
4508 (cm->sttype[v] == IR_st) ||
4509 (cm->sttype[v] == MP_st))
4510 dv_r = 1;
4511 else
4512 dv_r = 0;
4513 if(jmin[v] < (dv_l + dv_r))
4514 jmin[v] = dv_l + dv_r;
4515 if(jmax[v] < (dv_l + dv_r))
4516 jmax[v] = dv_l + dv_r;
4517 }
4518 /*****************************************************************************
4519 * EPN 12.21.05
4520 * Function: hmm2ij_state_step4_update_safe_holders
4521 *
4522 * Purpose: cp9_HMM2ijBands_OLD*() function helper function.
4523 *
4524 *****************************************************************************/
4525 void
4526 hmm2ij_state_step4_update_safe_holders(int v, int n, int imin_v, int jmax_v, int *nss_max_imin,
4527 int *nss_min_jmax)
4528 {
4529 if(imin_v > nss_max_imin[n])
4530 nss_max_imin[n] = imin_v;
4531 if(jmax_v < nss_min_jmax[n])
4532 nss_min_jmax[n] = jmax_v;
4533 }
4534
4535 /*****************************************************************************
4536 * EPN 12.21.05
4537 * Function: hmm2ij_state_step5_non_emitter_d0_hack
4538 *
4539 * Purpose: cp9_HMM2ijBands_OLD*() function helper function.
4540 *
4541 *****************************************************************************/
4542 void
4543 hmm2ij_state_step5_non_emitter_d0_hack(int v, int imax_v, int *jmin)
4544 {
4545 /* allow for possibility that d=0 for delete states*/
4546 if(jmin[v] <= imax_v && jmin[v] > 0)
4547 jmin[v]--;
4548 /* if imax = L, allow possibility for
4549 if(imax[v] == Limax_v && jmin[v] > 0)
4550 jmin[v]--;*/
4551 }
4552
4553 /* Function: cp9_ShiftCMBands()
4554 *
4555 * Description: Given a CM with a valid cm->cp9b CP9 bands object
4556 * calculated for a sequence in coordinates 1..i..j..L,
4557 * subtract a fixed offset (i-1) from all CM positions
4558 * in cp9b (cp9b->imin, cp9b->imax, cp9b->jmin, cp9b->jmax)
4559 * so the bands will now pertain to the same hit if
4560 * its coordinates were shifted to 1..j-i+1. This is used
4561 * prior to alignment of a pre-defined hit from i..j
4562 * using bands calculated when i..j was within its larger
4563 * context of 1..i..j..L. During alignment hits always
4564 * start at position 1.
4565 *
4566 * Because only positions i..j are possible in the subsequent
4567 * alignment, bands that allow residues before i or after
4568 * j are tightened to only include within i..j. This will
4569 * make states that were possible to reach only with residues
4570 * before i or after j now impossible to reach.
4571 *
4572 * Once all i and j bands are updated, ij2d_bands()
4573 * is used to update the d bands.
4574 *
4575 * NOTE: after calling this function cp9b will fail
4576 * a cp9_ValidateBands() function call.
4577 *
4578 * Args: CM - the CM, with a valid cm->cp9b CP9Bands_t object
4579 * i - first position of hit, this is the offset
4580 * j - final position of hit, used only to determine hit length
4581 * Returns: (void)
4582 */
4583 void
4584 cp9_ShiftCMBands(CM_t *cm, int i, int j, int do_trunc)
4585 {
4586 int v;
4587 int ip = i-1;
4588 int Lp = j-i+1;
4589 int sd, sdl, sdr;
4590 int min_i, max_i, min_j, max_j;
4591
4592 #if eslDEBUGLEVEL >= 1
4593 printf("#DEBUG: cp9_ShiftCMBands(), i: %d j: %d Lp: %d\n", i, j, Lp);
4594 #endif
4595
4596 for(v = 0; v < cm->M; v++) {
4597 sd = StateDelta(cm->sttype[v]);
4598 sdl = StateLeftDelta(cm->sttype[v]);
4599 sdr = StateRightDelta(cm->sttype[v]);
4600 if(cm->cp9b->imin[v] > 0) { /* state is currently possible to reach */
4601 min_i = 1;
4602 max_i = do_trunc ? Lp+1-ESL_MAX(sdl, sdr) : Lp+1-sd;
4603 /* careful! if do_trunc, d can be 1 for MP states, so i can be
4604 * at most Lp. Note: d can't be 0 for ML/IL in R mode, MR/IR in
4605 * L mode even though you might think it could be. We'll always
4606 * do a truncated begin with d=1 for L,R marginal alignments.
4607 *
4608 * If ! do_trunc, then d must be at least sd for all states, hence
4609 * the max i of Lp+1-sd.
4610 *
4611 * This is bug i37, one of the two bugs in 1.1rc2, which previously
4612 * had this as 'max_i = Lp'.
4613 */
4614
4615 min_j = do_trunc ? ESL_MAX(sdl, sdr) : sd;
4616 max_j = ESL_MAX(Lp, min_j);
4617 /* if (do_trunc) d can be 1 for MP states, this is why we use
4618 * ESL_MAX() call for min_j above. Note: d can't be 0 for ML/IL
4619 * in R mode, MR/IR in L mode even though you might think it
4620 * could be. We'll always do a truncated begin with d=1 for L,R
4621 * marginal alignments. */
4622
4623 cm->cp9b->imin[v] = ESL_MAX(cm->cp9b->imin[v] - ip, min_i);
4624 cm->cp9b->imax[v] = ESL_MIN(cm->cp9b->imax[v] - ip, max_i);
4625
4626 cm->cp9b->jmin[v] = ESL_MAX(cm->cp9b->jmin[v] - ip, min_j);
4627 cm->cp9b->jmax[v] = ESL_MIN(cm->cp9b->jmax[v] - ip, max_j);
4628
4629 if(cm->cp9b->imax[v] < min_i || cm->cp9b->jmax[v] < min_j ||
4630 cm->cp9b->imin[v] > max_i || cm->cp9b->jmin[v] > max_j) {
4631 /* this state is now impossible to reach */
4632 cm->cp9b->imin[v] = cm->cp9b->jmin[v] = -1;
4633 cm->cp9b->imax[v] = cm->cp9b->jmax[v] = -2;
4634 }
4635 }
4636 }
4637 ij2d_bands(cm, Lp, cm->cp9b->imin, cm->cp9b->imax, cm->cp9b->jmin, cm->cp9b->jmax, cm->cp9b->hdmin, cm->cp9b->hdmax, do_trunc, 0);
4638 /* Note that this will not update hdmin bands that are no longer within jmin..jmax, that's okay */
4639
4640 return;
4641 }
4642
4643 /* Function: cp9_CloneBands()
4644 *
4645 * Description: Clone a CP9Bands_t *cp9b object and return it.
4646 *
4647 * Args: cp9b - the CP9Bands_t object to clone
4648 *
4649 * Returns: the clone CP9Bands_t object.
4650 */
4651 CP9Bands_t *
4652 cp9_CloneBands(CP9Bands_t *src_cp9b, char *errbuf)
4653 {
4654 int status;
4655 CP9Bands_t *dest_cp9b = NULL;
4656 dest_cp9b = AllocCP9Bands(src_cp9b->cm_M, src_cp9b->hmm_M);
4657
4658 esl_vec_ICopy(src_cp9b->pn_min_m, src_cp9b->hmm_M+1, dest_cp9b->pn_min_m);
4659 esl_vec_ICopy(src_cp9b->pn_max_m, src_cp9b->hmm_M+1, dest_cp9b->pn_max_m);
4660 esl_vec_ICopy(src_cp9b->pn_min_i, src_cp9b->hmm_M+1, dest_cp9b->pn_min_i);
4661 esl_vec_ICopy(src_cp9b->pn_max_i, src_cp9b->hmm_M+1, dest_cp9b->pn_max_i);
4662 esl_vec_ICopy(src_cp9b->pn_min_d, src_cp9b->hmm_M+1, dest_cp9b->pn_min_d);
4663 esl_vec_ICopy(src_cp9b->pn_max_d, src_cp9b->hmm_M+1, dest_cp9b->pn_max_d);
4664 esl_vec_ICopy(src_cp9b->isum_pn_m, src_cp9b->hmm_M+1, dest_cp9b->isum_pn_m);
4665 esl_vec_ICopy(src_cp9b->isum_pn_i, src_cp9b->hmm_M+1, dest_cp9b->isum_pn_i);
4666 esl_vec_ICopy(src_cp9b->isum_pn_d, src_cp9b->hmm_M+1, dest_cp9b->isum_pn_d);
4667
4668 dest_cp9b->sp1 = src_cp9b->sp1;
4669 dest_cp9b->ep1 = src_cp9b->ep1;
4670 dest_cp9b->sp2 = src_cp9b->sp2;
4671 dest_cp9b->ep2 = src_cp9b->ep2;
4672
4673 dest_cp9b->thresh1 = src_cp9b->thresh1;
4674 dest_cp9b->thresh2 = src_cp9b->thresh2;
4675
4676 dest_cp9b->Rmarg_imin = src_cp9b->Rmarg_imin;
4677 dest_cp9b->Rmarg_imax = src_cp9b->Rmarg_imax;
4678 dest_cp9b->Lmarg_jmin = src_cp9b->Lmarg_jmin;
4679 dest_cp9b->Lmarg_jmax = src_cp9b->Lmarg_jmax;
4680
4681 esl_vec_ICopy(src_cp9b->Jvalid, (src_cp9b->cm_M+1), dest_cp9b->Jvalid);
4682 esl_vec_ICopy(src_cp9b->Lvalid, (src_cp9b->cm_M+1), dest_cp9b->Lvalid);
4683 esl_vec_ICopy(src_cp9b->Rvalid, (src_cp9b->cm_M+1), dest_cp9b->Rvalid);
4684 esl_vec_ICopy(src_cp9b->Tvalid, (src_cp9b->cm_M+1), dest_cp9b->Tvalid);
4685
4686 esl_vec_ICopy(src_cp9b->imin, src_cp9b->cm_M, dest_cp9b->imin);
4687 esl_vec_ICopy(src_cp9b->imax, src_cp9b->cm_M, dest_cp9b->imax);
4688 esl_vec_ICopy(src_cp9b->jmin, src_cp9b->cm_M, dest_cp9b->jmin);
4689 esl_vec_ICopy(src_cp9b->jmax, src_cp9b->cm_M, dest_cp9b->jmax);
4690
4691 if(src_cp9b->hd_alloced > 0) {
4692 /* set hdmin, hdmax ptrs and hd_needed and hd_alloced (all set in cp9GrowHDBands()) */
4693 if((status = cp9_GrowHDBands(dest_cp9b, errbuf)) != eslOK) goto ERROR;
4694 esl_vec_ICopy(src_cp9b->hdmin_mem, dest_cp9b->hd_alloced, dest_cp9b->hdmin_mem);
4695 esl_vec_ICopy(src_cp9b->hdmax_mem, dest_cp9b->hd_alloced, dest_cp9b->hdmax_mem);
4696 }
4697
4698 esl_vec_ICopy(src_cp9b->safe_hdmin, src_cp9b->cm_M, dest_cp9b->safe_hdmin);
4699 esl_vec_ICopy(src_cp9b->safe_hdmax, src_cp9b->cm_M, dest_cp9b->safe_hdmax);
4700
4701 dest_cp9b->tau = src_cp9b->tau;
4702
4703 return dest_cp9b;
4704
4705 ERROR:
4706 if(dest_cp9b != NULL) FreeCP9Bands(dest_cp9b);
4707 return NULL;
4708 }
4709
4710 /* Function: cp9_PredictStartAndEndPositions()
4711 * Date: EPN, Tue Sep 6 11:43:18 2011
4712 *
4713 * Purpose: Given a filled HMM posterior matrix and a CP9Bands_t
4714 * object with valid pn_{min,max}{m,i,d} bands, determine the
4715 * first and final HMM nodes that have a probability of being
4716 * occupied that exceeds <cp9b->thresh1> and <cp9b->thresh2>.
4717 * Store these four values in:
4718 * <cp9b->sp1>: minimum position that might be used (p > cp9b->thresh1, typically 0.01)
4719 * <cp9b->sp2>: minimum position that will likely be used (p > cp9b->thresh2, typically 0.98)
4720 * <cp9b->ep1>: maximum position that might be used (p > cp9b->thresh1, typically 0.01)
4721 * <cp9b->ep2>: maximum position that will likely be used (p > cp9b->thresh2, typically 0.98)
4722 *
4723 * If no HMM node has an occupancy probability that exceeds
4724 * <cp9b->thresh2> then sp2 and ep2 are set as out-of-bounds
4725 * values M+1 and 0 respectively.
4726 *
4727 * If no HMM node has an occupancy probability that exceeds
4728 * <cp9b->thresh1> then sp1 and ep1 are set as out-of-bounds
4729 * values M+1 and 0 respectively, though this should be
4730 * very rare.
4731 *
4732 * Using out-of-bounds values means we can't get any
4733 * information about where the alignment starts and ends from
4734 * the HMM. This has the effect that in a downstream call to
4735 * cp9_MarginalCandidatesFromStartEndPositions() all marginal
4736 * modes will be possible for all states and the eventual
4737 * alignment will essentially mimic a non-banded one.
4738 *
4739 * Also determine the CM bands on i and j that
4740 * will be used to allow for marginal alignments,
4741 * store these in <cp9b->{L,R}marg{i,j}_{min,max}.
4742 *
4743 * CP9_MX pmx: DP matrix for posteriors, already calc'ed
4744 * CP9Bands_t cp9b: the cp9 bands
4745 * int i0 start of target subsequence (often 1, beginning of dsq)
4746 * int j0 end of target subsequence (often L, end of dsq)
4747 *
4748 * Returns: void
4749 *
4750 * xref: ELN2 notebook, p.146-147; ~nawrockie/notebook/11_0816_inf_banded_trcyk/00LOG
4751 */
4752 void
4753 cp9_PredictStartAndEndPositions(CP9_MX *pmx, CP9Bands_t *cp9b, int i0, int j0)
4754 {
4755 int i;
4756 int k; /* counter over nodes of the model */
4757 int L = j0-i0+1; /* length of sequence */
4758 int iocc; /* occupancy probability, scaled int form */
4759 float pocc; /* occupancy probability, probability form */
4760
4761 /* Calculate minimum start positions: */
4762 k = 1;
4763 cp9b->sp1 = cp9b->sp2 = -1;
4764 while(k <= cp9b->hmm_M && (cp9b->sp1 == -1 || cp9b->sp2 == -1)) {
4765 if(cp9b->pn_min_m[k] == -1 && cp9b->pn_min_i[k] == -1 && cp9b->pn_min_d[k] == -1) {
4766 /*printf("k: %4d pocc IRRELEVANT (k unreachable, skipping)\n", k);*/
4767 k++;
4768 /* M, I, D states in node k are unreachable (no posterior cells had more than
4769 * cm->tau probability mass), k won't be our sp1 or sp2 */
4770 }
4771 else {
4772 iocc = -INFTY;
4773 for(i = 0; i <= L; i++) {
4774 iocc = ILogsum(iocc, ILogsum(pmx->mmx[i][k], pmx->dmx[i][k]));
4775 }
4776 pocc = Score2Prob(iocc, 1.);
4777 /*printf("k: %4d pocc: %.4f\n", k, pocc);*/
4778 if((cp9b->sp1 == -1) && (pocc > cp9b->thresh1)) cp9b->sp1 = k;
4779 if((cp9b->sp2 == -1) && (pocc > cp9b->thresh2)) cp9b->sp2 = k;
4780 k++;
4781 }
4782 }
4783 if(k == cp9b->hmm_M+1) {
4784 if(cp9b->sp1 == -1) { cp9b->sp1 = cp9b->hmm_M+1; } /* no node k has occupancy > thresh1, set as out-of-bounds value M+1 */
4785 if(cp9b->sp2 == -1) { cp9b->sp2 = cp9b->hmm_M+1; } /* no node k has occupancy > thresh2, set as out-of-bounds value M+1 */
4786 }
4787
4788 /* Calculate maximum end positions: */
4789 if((cp9b->sp1 == cp9b->hmm_M+1) &&
4790 (cp9b->sp2 == cp9b->hmm_M+1)) {
4791 /* we already know that there's no nodes that satisfy either thresh1 or thresh2, we can save time here */
4792 cp9b->ep1 = 0;
4793 cp9b->ep2 = 0;
4794 }
4795 else {
4796 cp9b->ep1 = cp9b->ep2 = -1;
4797 k = cp9b->hmm_M;
4798 while(k >= 1 && (cp9b->ep1 == -1 || cp9b->ep2 == -1)) {
4799 if(cp9b->pn_min_m[k] == -1 && cp9b->pn_min_i[k] == -1 && cp9b->pn_min_d[k] == -1) {
4800 /*printf("k: %4d pocc IRRELEVANT (k unreachable, skipping)\n", k);*/
4801 k--;
4802 /* M, I, D states in node k are unreachable (no posterior cells had more than
4803 * cm->tau probability mass), k won't be our ep1 or ep2 */
4804 }
4805 else {
4806 iocc = -INFTY;
4807 for(i = 0; i <= L; i++) {
4808 iocc = ILogsum(iocc, ILogsum(pmx->mmx[i][k], pmx->dmx[i][k]));
4809 }
4810 pocc = Score2Prob(iocc, 1.);
4811 /*printf("k: %4d pocc: %.4f\n", k, pocc);*/
4812 if((cp9b->ep1 == -1) && (pocc > cp9b->thresh1)) cp9b->ep1 = k;
4813 if((cp9b->ep2 == -1) && (pocc > cp9b->thresh2)) cp9b->ep2 = k;
4814 k--;
4815 }
4816 }
4817 if(k == 0) {
4818 if(cp9b->ep1 == -1) { cp9b->ep1 = 0; } /* no node k has occupancy > thresh1, set as out-of-bounds value 0 */
4819 if(cp9b->ep2 == -1) { cp9b->ep2 = 0; } /* no node k has occupancy > thresh2, set as out-of-bounds value 0 */
4820 }
4821 }
4822
4823 /* determine cp9b->{R,L}marg_{i,j}{min,max}, the i and j bands that will be used to allow for marginal left (Lmarg_j{min,max}
4824 * and marginal right (Rmarg_i{min,max} alignment. */
4825 /* set cp9b->Rmarg_imin */
4826 if(cp9b->sp1 == cp9b->hmm_M+1) { cp9b->Rmarg_imin = i0; }
4827 else {
4828 cp9b->Rmarg_imin = INT_MAX;
4829 if(cp9b->sp1 != (cp9b->hmm_M+1) && cp9b->pn_min_m[cp9b->sp1] >= 0) cp9b->Rmarg_imin = ESL_MIN(cp9b->Rmarg_imin, cp9b->pn_min_m[cp9b->sp1]);
4830 if(cp9b->sp1 != (cp9b->hmm_M+1) && cp9b->pn_min_i[cp9b->sp1] >= 0) cp9b->Rmarg_imin = ESL_MIN(cp9b->Rmarg_imin, cp9b->pn_min_i[cp9b->sp1]);
4831 if(cp9b->sp1 != (cp9b->hmm_M+1) && cp9b->pn_min_d[cp9b->sp1] >= 0) cp9b->Rmarg_imin = ESL_MIN(cp9b->Rmarg_imin, cp9b->pn_min_d[cp9b->sp1]);
4832 if(cp9b->sp2 != (cp9b->hmm_M+1) && cp9b->pn_min_m[cp9b->sp2] >= 0) cp9b->Rmarg_imin = ESL_MIN(cp9b->Rmarg_imin, cp9b->pn_min_m[cp9b->sp2]);
4833 if(cp9b->sp2 != (cp9b->hmm_M+1) && cp9b->pn_min_i[cp9b->sp2] >= 0) cp9b->Rmarg_imin = ESL_MIN(cp9b->Rmarg_imin, cp9b->pn_min_i[cp9b->sp2]);
4834 if(cp9b->sp2 != (cp9b->hmm_M+1) && cp9b->pn_min_d[cp9b->sp2] >= 0) cp9b->Rmarg_imin = ESL_MIN(cp9b->Rmarg_imin, cp9b->pn_min_d[cp9b->sp2]);
4835 if(cp9b->Rmarg_imin == INT_MAX || cp9b->sp1 == (cp9b->hmm_M+1) || cp9b->sp2 == (cp9b->hmm_M+1)) cp9b->Rmarg_imin = i0;
4836 cp9b->Rmarg_imin = ESL_MAX(i0, cp9b->Rmarg_imin); /* i can't be less than i0 */
4837 cp9b->Rmarg_imin = ESL_MIN(j0+1, cp9b->Rmarg_imin); /* i can't be more than j0+1 */
4838 }
4839
4840 /* set cp9b->Rmarg_imax */
4841 if(cp9b->sp1 == cp9b->hmm_M+1) { cp9b->Rmarg_imax = j0; }
4842 else {
4843 cp9b->Rmarg_imax = INT_MIN;
4844 if(cp9b->sp1 != (cp9b->hmm_M+1) && cp9b->pn_max_m[cp9b->sp1] >= 0) cp9b->Rmarg_imax = ESL_MAX(cp9b->Rmarg_imax, cp9b->pn_max_m[cp9b->sp1]);
4845 if(cp9b->sp1 != (cp9b->hmm_M+1) && cp9b->pn_max_i[cp9b->sp1] >= 0) cp9b->Rmarg_imax = ESL_MAX(cp9b->Rmarg_imax, cp9b->pn_max_i[cp9b->sp1]);
4846 if(cp9b->sp1 != (cp9b->hmm_M+1) && cp9b->pn_max_d[cp9b->sp1] >= 0) cp9b->Rmarg_imax = ESL_MAX(cp9b->Rmarg_imax, cp9b->pn_max_d[cp9b->sp1]);
4847 if(cp9b->sp2 != (cp9b->hmm_M+1) && cp9b->pn_max_m[cp9b->sp2] >= 0) cp9b->Rmarg_imax = ESL_MAX(cp9b->Rmarg_imax, cp9b->pn_max_m[cp9b->sp2]);
4848 if(cp9b->sp2 != (cp9b->hmm_M+1) && cp9b->pn_max_i[cp9b->sp2] >= 0) cp9b->Rmarg_imax = ESL_MAX(cp9b->Rmarg_imax, cp9b->pn_max_i[cp9b->sp2]);
4849 if(cp9b->sp2 != (cp9b->hmm_M+1) && cp9b->pn_max_d[cp9b->sp2] >= 0) cp9b->Rmarg_imax = ESL_MAX(cp9b->Rmarg_imax, cp9b->pn_max_d[cp9b->sp2]);
4850 if(cp9b->Rmarg_imax == INT_MIN || cp9b->sp1 == (cp9b->hmm_M+1) || cp9b->sp2 == (cp9b->hmm_M+1)) cp9b->Rmarg_imax = j0+1;
4851 cp9b->Rmarg_imax = ESL_MAX(i0, cp9b->Rmarg_imax); /* i can't be less than i0 */
4852 cp9b->Rmarg_imax = ESL_MIN(j0+1, cp9b->Rmarg_imax); /* i can't be more than j0+1 */
4853 }
4854
4855 /* set cp9b->Lmarg_jmin */
4856 if(cp9b->ep1 == 0) { cp9b->Lmarg_jmin = i0-1; }
4857 else {
4858 cp9b->Lmarg_jmin = INT_MAX;
4859 if(cp9b->ep1 != 0 && cp9b->pn_min_m[cp9b->ep1] >= 0) cp9b->Lmarg_jmin = ESL_MIN(cp9b->Lmarg_jmin, cp9b->pn_min_m[cp9b->ep1]);
4860 if(cp9b->ep1 != 0 && cp9b->pn_min_i[cp9b->ep1] >= 0) cp9b->Lmarg_jmin = ESL_MIN(cp9b->Lmarg_jmin, cp9b->pn_min_i[cp9b->ep1]);
4861 if(cp9b->ep1 != 0 && cp9b->pn_min_d[cp9b->ep1] >= 0) cp9b->Lmarg_jmin = ESL_MIN(cp9b->Lmarg_jmin, cp9b->pn_min_d[cp9b->ep1]-1); /* off-by-one with deletes in HMM vs CM */
4862 if(cp9b->ep2 != 0 && cp9b->pn_min_m[cp9b->ep2] >= 0) cp9b->Lmarg_jmin = ESL_MIN(cp9b->Lmarg_jmin, cp9b->pn_min_m[cp9b->ep2]);
4863 if(cp9b->ep2 != 0 && cp9b->pn_min_i[cp9b->ep2] >= 0) cp9b->Lmarg_jmin = ESL_MIN(cp9b->Lmarg_jmin, cp9b->pn_min_i[cp9b->ep2]);
4864 if(cp9b->ep2 != 0 && cp9b->pn_min_d[cp9b->ep2] >= 0) cp9b->Lmarg_jmin = ESL_MIN(cp9b->Lmarg_jmin, cp9b->pn_min_d[cp9b->ep2]-1); /* off-by-one with deletes in HMM vs CM */
4865 if(cp9b->Lmarg_jmin == INT_MAX || cp9b->ep1 == 0 || cp9b->ep2 == 0) cp9b->Lmarg_jmin = i0-1;
4866 cp9b->Lmarg_jmin = ESL_MAX(i0-1, cp9b->Lmarg_jmin); /* j can't be less than i0-1 */
4867 cp9b->Lmarg_jmin = ESL_MIN(j0, cp9b->Lmarg_jmin); /* j can't be more than j0 */
4868 }
4869
4870 /* set cp9b->Lmarg_jmax */
4871 if(cp9b->ep1 == 0) { cp9b->Lmarg_jmax = j0; }
4872 else {
4873 cp9b->Lmarg_jmax = INT_MIN;
4874 if(cp9b->ep1 != 0 && cp9b->pn_max_m[cp9b->ep1] >= 0) cp9b->Lmarg_jmax = ESL_MAX(cp9b->Lmarg_jmax, cp9b->pn_max_m[cp9b->ep1]);
4875 if(cp9b->ep1 != 0 && cp9b->pn_max_i[cp9b->ep1] >= 0) cp9b->Lmarg_jmax = ESL_MAX(cp9b->Lmarg_jmax, cp9b->pn_max_i[cp9b->ep1]);
4876 if(cp9b->ep1 != 0 && cp9b->pn_max_d[cp9b->ep1] >= 0) cp9b->Lmarg_jmax = ESL_MAX(cp9b->Lmarg_jmax, cp9b->pn_max_d[cp9b->ep1]-1); /* off-by-one with deletes in HMM vs CM */
4877 if(cp9b->ep2 != 0 && cp9b->pn_max_m[cp9b->ep2] >= 0) cp9b->Lmarg_jmax = ESL_MAX(cp9b->Lmarg_jmax, cp9b->pn_max_m[cp9b->ep2]);
4878 if(cp9b->ep2 != 0 && cp9b->pn_max_i[cp9b->ep2] >= 0) cp9b->Lmarg_jmax = ESL_MAX(cp9b->Lmarg_jmax, cp9b->pn_max_i[cp9b->ep2]);
4879 if(cp9b->ep2 != 0 && cp9b->pn_max_d[cp9b->ep2] >= 0) cp9b->Lmarg_jmax = ESL_MAX(cp9b->Lmarg_jmax, cp9b->pn_max_d[cp9b->ep2]-1); /* off-by-one with deletes in HMM vs CM */
4880 if(cp9b->Lmarg_jmax == INT_MIN || cp9b->ep1 == 0 || cp9b->ep2 == 0) cp9b->Lmarg_jmax = j0;
4881 cp9b->Lmarg_jmax = ESL_MAX(i0-1, cp9b->Lmarg_jmax); /* j can't be less than i0-1 */
4882 cp9b->Lmarg_jmax = ESL_MIN(j0, cp9b->Lmarg_jmax); /* j can't be more than j0 */
4883 }
4884
4885 #if eslDEBUGLEVEL >= 1
4886 printf("Returning from cp9_PredictStartAndEndPositions():\n\t");
4887 printf("sp1: %4d\n\t", cp9b->sp1);
4888 printf("sp2: %4d\n\t", cp9b->sp2);
4889 printf("ep2: %4d\n\t", cp9b->ep2);
4890 printf("ep1: %4d\n\t", cp9b->ep1);
4891 printf("Ljn: %4d\n\t", cp9b->Lmarg_jmin);
4892 printf("Ljx: %4d\n\t", cp9b->Lmarg_jmax);
4893 printf("Rin: %4d\n\t", cp9b->Rmarg_imin);
4894 printf("Rix: %4d\n\n", cp9b->Rmarg_imax);
4895 #endif
4896
4897 return;
4898 }
4899
4900
4901 /* Function: cp9_MarginalCandidatesFromStartEndPositions()
4902 * Date: EPN, Tue Sep 6 14:50:16 2011
4903 *
4904 * Purpose: Given a CP9Bands_t object with valid sp1, sp2, ep1, and
4905 * ep2 values from cp9_PredictStartAndEndPositions(),
4906 * determine for each CM state v, whether a joint (J), left
4907 * marginal (L) right marginal (R), or terminal marginal
4908 * alignment that includes v should be allowed. For any
4909 * disallowed type of alignment we will be able to skip the
4910 * corresponding calculations in a trCYK/trInside/trOutside
4911 * DP recursion. And we won't have to allocate memory for
4912 * that state in the corresponding (J,L,R,T) DP matrix.
4913 *
4914 * We can determine from passed-in <pass_idx>, which type of
4915 * marginal alignments will be allowed. If L alignments
4916 * are not allowed, Lvalid[] will be FALSE for all v.
4917 * Likewise for R alignments and Rvalid[] and T alignments
4918 * and Tvalid[].
4919 *
4920 * Args: cm - the model
4921 * cp9b - the cp9 bands
4922 * pass_idx - the pipeline pass index we're on, dictates
4923 * which modes of marginal alns to allow
4924 * errbuf - for error messages
4925 *
4926 * Returns: eslOK on success; eslEINVAL if pass_idx is invalid (errbuf filled).
4927 *
4928 * xref: ELN2 notebook, p.146-147; ~nawrockie/notebook/11_0816_inf_banded_trcyk/00LOG
4929 */
4930 int
4931 cp9_MarginalCandidatesFromStartEndPositions(CM_t *cm, CP9Bands_t *cp9b, int pass_idx, char *errbuf)
4932 {
4933 int status;
4934 int v;
4935 int nd;
4936 int lpos = 1;
4937 int rpos = cm->clen;
4938 int allow_L, allow_R, allow_T; /* will we allow L, R, and T alignments? */
4939
4940 if((status = cm_TrFillFromPassIdx(pass_idx, &allow_L, &allow_R, &allow_T)) != eslOK) ESL_FAIL(status, errbuf, "cp9_MarginalCandidatesFromStartEndPositions(), unexpected pass idx: %d", pass_idx);
4941
4942 for(v = 0; v < cp9b->cm_M; v++) {
4943 nd = cm->ndidx[v];
4944 /* Careful, emitmap is off-by-one for our purposes for lpos if v is not MATP_MP or MATL_ML, and rpos if v is not MATP_MP or MATR_MR */
4945 lpos = (cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATL_nd) ? cm->emap->lpos[nd] : cm->emap->lpos[nd]+1;
4946 rpos = (cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATR_nd) ? cm->emap->rpos[nd] : cm->emap->rpos[nd]-1;
4947
4948 /* below: 'possibly' means probability > cp9b->thresh1 (typically 0.01) */
4949 /* 'probably' means probability > cp9b->thresh2 (typically 0.98) */
4950
4951 /* Jvalid if both lpos and rpos are possibly used */
4952 cp9b->Jvalid[v] = ((lpos >= cp9b->sp1) && (rpos <= cp9b->ep1)) ? TRUE : FALSE;
4953
4954 /* Lvalid if lpos is possibly used and rpos is possibly not used */
4955 cp9b->Lvalid[v] = (allow_L && (lpos >= cp9b->sp1 && lpos <= cp9b->ep1) && (rpos > cp9b->ep2)) ? TRUE : FALSE;
4956
4957 /* Rvalid if rpos is possibly used and lpos is possibly not used */
4958 cp9b->Rvalid[v] = (allow_R && (rpos <= cp9b->ep1 && rpos >= cp9b->sp1) && (lpos < cp9b->sp2)) ? TRUE : FALSE;
4959
4960 if(cm->sttype[v] == B_st) {
4961 /* Tvalid if lpos and rpos are possibly not used */
4962 cp9b->Tvalid[v] = (allow_T && (lpos < cp9b->sp2) && (rpos > cp9b->ep2)) ? TRUE : FALSE;
4963 }
4964 else {
4965 cp9b->Tvalid[v] = FALSE;
4966 }
4967 #if eslDEBUGLEVEL >= 3
4968 printf("#DEBUG: v: %4d [%4d..%4d] %4s %2s %d%d%d%d\n", v, lpos, rpos, Nodetype(cm->ndtype[cm->ndidx[v]]), Statetype(cm->sttype[v]),
4969 cp9b->Jvalid[v], cp9b->Lvalid[v], cp9b->Rvalid[v], cp9b->Tvalid[v]);
4970 #endif
4971 }
4972
4973 /* The ROOT_S state is special, all hits are rooted there, if we can do a
4974 * truncated {J,L,R,T} begin into v, we need to set do_{J,L,R,T}[0] to TRUE.
4975 */
4976 for(v = 0; v < cp9b->cm_M; v++) {
4977 switch(cm->sttype[v]) {
4978 case B_st:
4979 if(cp9b->Jvalid[v]) cp9b->Jvalid[0] = TRUE;
4980 if(cp9b->Lvalid[v]) cp9b->Lvalid[0] = TRUE;
4981 if(cp9b->Rvalid[v]) cp9b->Rvalid[0] = TRUE;
4982 if(cp9b->Tvalid[v]) cp9b->Tvalid[0] = TRUE;
4983 break;
4984 case MP_st:
4985 if(cp9b->Jvalid[v]) cp9b->Jvalid[0] = TRUE;
4986 if(cp9b->Lvalid[v]) cp9b->Lvalid[0] = TRUE;
4987 if(cp9b->Rvalid[v]) cp9b->Rvalid[0] = TRUE;
4988 break;
4989 case ML_st:
4990 case IL_st:
4991 if(cp9b->Jvalid[v]) cp9b->Jvalid[0] = TRUE;
4992 if(cp9b->Lvalid[v]) cp9b->Lvalid[0] = TRUE;
4993 break;
4994 case MR_st:
4995 case IR_st:
4996 if(cp9b->Jvalid[v]) cp9b->Jvalid[0] = TRUE;
4997 if(cp9b->Rvalid[v]) cp9b->Rvalid[0] = TRUE;
4998 break;
4999 }
5000 if(cp9b->Jvalid[0] &&
5001 cp9b->Lvalid[0] &&
5002 cp9b->Rvalid[0] &&
5003 cp9b->Tvalid[0]) {
5004 v = cp9b->cm_M;
5005 }
5006 }
5007
5008 /* The EL state is special, if local ends are on, make J, L and R
5009 * modes all valid. (We could only make those modes valid for
5010 * which there's a local end possible (e.g. make cm->M invalid for
5011 * R if R is not valid for any states), but empirically this is
5012 * rare, so I've opted to always allow all types to avoid allowing
5013 * the possibility that we turn {J,L,R}valid[cm->M] on and off
5014 * as we process seqs, thus avoiding all the possible complications
5015 * of doing that.
5016 */
5017 if(cm->flags & CMH_LOCAL_END) {
5018 cp9b->Jvalid[cm->M] = TRUE;
5019 cp9b->Lvalid[cm->M] = TRUE;
5020 cp9b->Rvalid[cm->M] = TRUE;
5021 }
5022
5023 return eslOK;
5024 }
5025
5026
5027
5028 /****************************************************************************
5029 * Debugging print functions
5030 *
5031 * cp9_DebugPrintHMMBands()
5032 * PrintDPCellsSaved_jd()
5033 *
5034 * Currently not compiled (#if 0'ed out) but saved for ref:
5035 * ijBandedTraceInfoDump()
5036 * ijdBandedTraceInfoDump()
5037 * debug_print_hd_bands()
5038 * debug_print_ij_bands()
5039 * debug_print_parsetree_and_ij_bands()
5040 *
5041 */
5042 #if 0
5043 static void ijBandedTraceInfoDump(CM_t *cm, Parsetree_t *tr, int *imin, int *imax,
5044 int *jmin, int *jmax, int debug_level);
5045 static void ijdBandedTraceInfoDump(CM_t *cm, Parsetree_t *tr, int *imin, int *imax,
5046 int *jmin, int *jmax, int **hdmin, int **hdmax,
5047 int debug_level);
5048 static void debug_print_hd_bands(CM_t *cm, int **hdmin, int **hdmax, int *jmin, int *jmax);
5049 static void debug_print_parsetree_and_ij_bands(FILE *fp, Parsetree_t *tr, CM_t *cm, ESL_DSQ *dsq, CP9Bands_t *cp9b);
5050 static void cp9_RelaxRootBandsForSearch(CM_t *cm, int i0, int j0, int *imin, int *imax, int *jmin, int *jmax);
5051 #endif
5052
5053 /* EPN 12.18.05
5054 * cp9_DebugPrintHMMBands()
5055 * based loosely on: cmbuild.c's
5056 * Function: model_trace_info_dump
5057 *
5058 * Purpose: Print out the bands derived from the posteriors for the
5059 * insert and match states of each HMM node.
5060 *
5061 * Args:
5062 * FILE *ofp - filehandle to print to (can by STDOUT)
5063 * int L - length of sequence
5064 * CP9Bands_t - the CP9 bands data structure
5065 * double hmm_bandp - fraction of probability mass allowed outside each band.
5066 * int debug_level [0..3] tells the function what level of debugging print
5067 * statements to print.
5068 * Returns: (void)
5069 */
5070
5071 void
5072 cp9_DebugPrintHMMBands(FILE *ofp, int L, CP9Bands_t *cp9b, double hmm_bandp, int debug_level)
5073 {
5074 int M;
5075 int k;
5076 int cells_in_bands_m; /* number of cells within all the bands for match states*/
5077 int cells_in_bands_i; /* number of cells within all the bands for insert states*/
5078 int cells_in_bands_d; /* number of cells within all the bands for delete states*/
5079 int cells_in_bands_all; /* number of cells within all the bands for match and insert states*/
5080 int bw; /* band width of current band */
5081
5082 M = cp9b->hmm_M;
5083 cells_in_bands_m = cells_in_bands_i = cells_in_bands_d = cells_in_bands_all = 0;
5084
5085 /* first print the bands on the match states */
5086 fprintf(ofp, "***********************************************************\n");
5087 if(debug_level > 0)
5088 fprintf(ofp, "printing hmm bands\n");
5089 fprintf(ofp, "hmm_bandp: %f\n", hmm_bandp);
5090 if(debug_level > 0)
5091 {
5092 fprintf(ofp, "\n");
5093 fprintf(ofp, "match states\n");
5094 }
5095 for(k = 0; k <= cp9b->hmm_M; k++)
5096 {
5097 bw = (cp9b->pn_min_m[k] == -1) ? 0 : cp9b->pn_max_m[k] - cp9b->pn_min_m[k] + 1;
5098 if(debug_level > 0 || debug_level == -1)
5099 fprintf(ofp, "M node: %3d | min %3d | max %3d | w %3d \n", k, cp9b->pn_min_m[k], cp9b->pn_max_m[k], bw);
5100 cells_in_bands_m += bw;
5101 }
5102 if(debug_level > 0)
5103 fprintf(ofp, "\n");
5104 if(debug_level > 0)
5105 fprintf(ofp, "insert states\n");
5106 for(k = 0; k <= cp9b->hmm_M; k++)
5107 {
5108 bw = (cp9b->pn_min_i[k] == -1) ? 0 : cp9b->pn_max_i[k] - cp9b->pn_min_i[k] + 1;
5109 if(debug_level > 0 || debug_level == -1)
5110 fprintf(ofp, "I node: %3d | min %3d | max %3d | w %3d\n", k, cp9b->pn_min_i[k], cp9b->pn_max_i[k], bw);
5111 cells_in_bands_i += bw;
5112 }
5113 if(debug_level > 0)
5114 fprintf(ofp, "\n");
5115 if(debug_level > 0)
5116 fprintf(ofp, "delete states\n");
5117 for(k = 1; k <= cp9b->hmm_M; k++)
5118 {
5119 bw = (cp9b->pn_min_d[k] == -1) ? 0 : cp9b->pn_max_d[k] - cp9b->pn_min_d[k] + 1;
5120 if(debug_level > 0 || debug_level == -1)
5121 fprintf(ofp, "D node: %3d | min %3d | max %3d | w %3d\n", k, cp9b->pn_min_d[k], cp9b->pn_max_d[k], bw);
5122 cells_in_bands_d += bw;
5123 }
5124 if(debug_level > 0)
5125 {
5126 fprintf(ofp, "\n");
5127 printf("cells_in_bands_m : %d\n", cells_in_bands_m);
5128 printf("cells_in_bands_i : %d\n", cells_in_bands_i);
5129 printf("cells_in_bands_d : %d\n", cells_in_bands_d);
5130 }
5131
5132 cells_in_bands_all = cells_in_bands_m + cells_in_bands_i + cells_in_bands_d;
5133 printf("fraction match excluded : %f\n", (1 - ((float) cells_in_bands_m / (M * L))));
5134 printf("fraction insert excluded : %f\n", (1 - ((float) cells_in_bands_i / ((M-1) * L))));
5135 printf("fraction delete excluded : %f\n", (1 - ((float) cells_in_bands_d / ((M-1) * L))));
5136 printf("fraction total excluded : %f\n", (1 - ((float) (cells_in_bands_all) / (((M-1) * L) + ((M-1) * L) + (M *L)))));
5137 fprintf(ofp, "***********************************************************\n");
5138
5139 }
5140
5141 /* Function: PrintDPCellsSaved_jd()
5142 * Prints out an estimate of the speed up due to j and d bands */
5143 void
5144 PrintDPCellsSaved_jd(CM_t *cm, int *jmin, int *jmax, int **hdmin, int **hdmax,
5145 int W)
5146 {
5147 int v;
5148 int j;
5149 int max;
5150 int64_t after, before;
5151
5152 printf("Printing DP cells saved using j and d bands:\n");
5153 before = after = 0;
5154 for (v = 0; v < cm->M; v++)
5155 {
5156 for(j = 0; j <= W; j++)
5157 if (cm->sttype[v] != E_st)
5158 before += j + 1;
5159 for(j = jmin[v]; j <= jmax[v]; j++)
5160 if (cm->sttype[v] != E_st)
5161 {
5162 max = (j < hdmax[v][j-jmin[v]]) ? j : hdmax[v][j-jmin[v]];
5163 after += max - hdmin[v][j-jmin[v]] + 1;
5164 }
5165 }
5166 printf("Before: something like %" PRId64 "\n", before);
5167 printf("After: something like %" PRId64 "\n", after);
5168 printf("Speedup: maybe %.2f fold\n\n", (double) before / (double) after);
5169 }
5170
5171 /* Function: debug_print_ij_bands
5172 *
5173 * Purpose: Print out i and j bands for all states v.
5174 *
5175 */
5176 void
5177 debug_print_ij_bands(CM_t *cm)
5178 {
5179 int v;
5180 printf("%5s %-7s %5s %5s %5s %5s %4s\n", "v", "type", "imin", "imax", "jmin", "jmax", "JLRT");
5181 printf("%5s %-7s %5s %5s %5s %5s %4s\n", "-----", "-------", "-----", "-----", "-----", "-----", "----");
5182 for(v = 0; v < cm->M; v++)
5183 printf("%5d %-7s %5d %5d %5d %5d %d%d%d%d\n", v, CMStateid(cm->stid[v]), cm->cp9b->imin[v], cm->cp9b->imax[v], cm->cp9b->jmin[v], cm->cp9b->jmax[v],
5184 cm->cp9b->Jvalid[v], cm->cp9b->Lvalid[v], cm->cp9b->Rvalid[v], cm->cp9b->Tvalid[v]);
5185 return;
5186 }
5187
5188
5189 #if 0
5190 /* EPN 11.03.05
5191 * Function: ijBandedTraceInfoDump()
5192 *
5193 * Purpose: Experimental HMMERNAL function used in development.
5194 * This function determines how close the
5195 * trace was to the bands for i and j at each state in the trace,
5196 * and prints out that information in differing levels
5197 * of verbosity depending on an input parameter
5198 * (debug_level).
5199 *
5200 * Args: cm - the CM (useful for determining which states are E states)
5201 * tr - the parsetree (trace)
5202 * imin - minimum i bound for each state v; [0..v..M-1]
5203 * imax - maximum i bound for each state v; [0..v..M-1]
5204 * jmin - minimum j bound for each state v; [0..v..M-1]
5205 * jmax - maximum j bound for each state v; [0..v..M-1]
5206 * debug_level - level of verbosity
5207 * Returns: (void)
5208 */
5209
5210 void
5211 ijBandedTraceInfoDump(CM_t *cm, Parsetree_t *tr, int *imin, int *imax,
5212 int *jmin, int *jmax, int debug_level)
5213 {
5214 int v, i, j, d, tpos;
5215 int imindiff; /* i - imin[v] */
5216 int imaxdiff; /* imax[v] - i */
5217 int jmindiff; /* j - jmin[v] */
5218 int jmaxdiff; /* jmax[v] - j */
5219 int imin_out;
5220 int imax_out;
5221 int jmin_out;
5222 int jmax_out;
5223
5224 imin_out = 0;
5225 imax_out = 0;
5226 jmin_out = 0;
5227 jmax_out = 0;
5228
5229 debug_level = 2;
5230
5231 for (tpos = 0; tpos < tr->n; tpos++)
5232 {
5233 v = tr->state[tpos];
5234 i = tr->emitl[tpos];
5235 j = tr->emitr[tpos];
5236 d = j-i+1;
5237 imindiff = i-imin[v];
5238 imaxdiff = imax[v]-i;
5239 jmindiff = j-jmin[v];
5240 jmaxdiff = jmax[v]-j;
5241 if(cm->sttype[v] != E_st)
5242 {
5243 if(imindiff < 0)
5244 imin_out++;
5245 if(imaxdiff < 0)
5246 imax_out++;
5247 if(jmindiff < 0)
5248 jmin_out++;
5249 if(jmaxdiff < 0)
5250 jmax_out++;
5251
5252 if(debug_level > 1 || ((imindiff < 0) || (imaxdiff < 0) || (jmindiff < 0) || (jmaxdiff < 0)))
5253 {
5254 printf("v: %4d %-4s %-2s | d: %4d | i: %4d | in: %4d | ix: %4d | %3d | %3d |\n", v, Nodetype(cm->ndtype[cm->ndidx[v]]), Statetype(cm->sttype[v]), d, i, imin[v], imax[v], imindiff, imaxdiff);
5255 printf(" | j: %4d | jn: %4d | jx: %4d | %3d | %3d |\n", j, jmin[v], jmax[v], jmindiff, jmaxdiff);
5256
5257 }
5258 }
5259 else if(cm->sttype[v] == E_st)
5260 {
5261 if(debug_level > 1)
5262 {
5263 printf("v: %4d %-4s %-2s | d: %4d | i: %4d | in: %4d | ix: %4d | %3d | %3d |\n", v, Nodetype(cm->ndtype[cm->ndidx[v]]), Statetype(cm->sttype[v]), d, i, imin[v], imax[v], imindiff, imaxdiff);
5264 printf(" | j: %4d | jn: %4d | jx: %4d | %3d | %3d |\n", j, jmin[v], jmax[v], jmindiff, jmaxdiff);
5265 }
5266 }
5267 }
5268 printf("\nimin out: %d\n", imin_out);
5269 printf("imax out: %d\n", imax_out);
5270 printf("jmin out: %d\n", jmin_out);
5271 printf("jmax out: %d\n", jmax_out);
5272
5273 if((imin_out + imax_out + jmin_out + jmax_out) > 0)
5274 {
5275 printf("ERROR, some of the i and j bands are going to prevent optimal alignment. Sorry.\n");
5276 }
5277
5278 return;
5279 }
5280
5281
5282 /* EPN 11.03.05
5283 * Function: ijdBandedTraceInfoDump()
5284 *
5285 * Purpose: Experimental HMMERNAL function used in development.
5286 * This function determines how close the
5287 * trace was to the bands for i and j and d at each state in the trace,
5288 * and prints out that information in differing levels
5289 * of verbosity depending on an input parameter
5290 * (debug_level).
5291 *
5292 * Args: cm - the CM (useful for determining which states are E states)
5293 * tr - the parsetree (trace)
5294 * imin - minimum i bound for each state v; [0..v..M-1]
5295 * imax - maximum i bound for each state v; [0..v..M-1]
5296 * jmin - minimum j bound for each state v; [0..v..M-1]
5297 * jmax - maximum j bound for each state v; [0..v..M-1]
5298 * hdmin - minimum d bound for each state v and offset j;
5299 * [0..v..M-1][0..(jmax[v]-jmin[v])]
5300 * hdmax - maximum d bound for each state v and offset j;
5301 * [0..v..M-1][0..(jmax[v]-jmin[v])]
5302 * debug_level - level of verbosity
5303 * Returns: (void)
5304 */
5305
5306 void
5307 ijdBandedTraceInfoDump(CM_t *cm, Parsetree_t *tr, int *imin, int *imax,
5308 int *jmin, int *jmax, int **hdmin, int **hdmax, int debug_level)
5309 {
5310 int v, i, j, d, tpos;
5311 int imindiff; /* i - imin[v] */
5312 int imaxdiff; /* imax[v] - i */
5313 int jmindiff; /* j - jmin[v] */
5314 int jmaxdiff; /* jmax[v] - j */
5315 int hdmindiff; /* d - hdmin[v][j] */
5316 int hdmaxdiff; /* hdmax[v][j] - d */
5317
5318 int imin_out;
5319 int imax_out;
5320 int jmin_out;
5321 int jmax_out;
5322 int hdmin_out;
5323 int hdmax_out;
5324 int local_used;
5325
5326 imin_out = 0;
5327 imax_out = 0;
5328 jmin_out = 0;
5329 jmax_out = 0;
5330 hdmin_out = 0;
5331 hdmax_out = 0;
5332 local_used = 0;
5333
5334 debug_level = 2;
5335
5336 for (tpos = 0; tpos < tr->n; tpos++)
5337 {
5338 v = tr->state[tpos];
5339 i = tr->emitl[tpos];
5340 j = tr->emitr[tpos];
5341 d = j-i+1;
5342 if(cm->sttype[v] == EL_st) /*END LOCAL state*/
5343 {
5344 if(debug_level > 1)
5345 {
5346 printf("v: %4d NA %-2s ( NA) | d: %4d | i: %4d | in: NA | ix: NA | NA | NA |\n", v, Statetype(cm->sttype[v]), d, i);
5347 printf(" | j: %4d | jn: NA | jx: NA | NA | NA |\n", j);
5348 printf(" | d: %4d | dn: NA | dx: NA | NA | NA |\n", d);
5349
5350 local_used++;
5351 }
5352 }
5353 else
5354 {
5355 imindiff = i-imin[v];
5356 imaxdiff = imax[v]-i;
5357 jmindiff = j-jmin[v];
5358 jmaxdiff = jmax[v]-j;
5359 if(j >= jmin[v] && j <= jmax[v])
5360 {
5361 hdmindiff = d - hdmin[v][j-jmin[v]];
5362 hdmaxdiff = hdmax[v][j-jmin[v]] - d;
5363 }
5364 else
5365 {
5366 hdmindiff = -1000;
5367 hdmaxdiff = -1000;
5368 }
5369 if(imindiff < 0)
5370 imin_out++;
5371 if(imaxdiff < 0)
5372 imax_out++;
5373 if(jmindiff < 0)
5374 jmin_out++;
5375 if(jmaxdiff < 0)
5376 jmax_out++;
5377 if(hdmindiff < 0)
5378 hdmin_out++;
5379 if(hdmaxdiff < 0)
5380 hdmax_out++;
5381
5382 if(debug_level > 1 || ((imindiff < 0) || (imaxdiff < 0) || (jmindiff < 0) || (jmaxdiff < 0) ||
5383 (hdmindiff < 0) || (hdmaxdiff < 0)))
5384 {
5385 printf("v: %4d %-4s %-2s (%4d) | d: %4d | i: %4d | in: %4d | ix: %4d | %3d | %3d |\n", v, Nodetype(cm->ndtype[cm->ndidx[v]]), Statetype(cm->sttype[v]), cm->ndidx[v], d, i, imin[v], imax[v], imindiff, imaxdiff);
5386 printf(" | j: %4d | jn: %4d | jx: %4d | %3d | %3d |\n", j, jmin[v], jmax[v], jmindiff, jmaxdiff);
5387 if(j >= jmin[v] && j <= jmax[v])
5388 {
5389 printf(" | d: %4d | dn: %4d | dx: %4d | %3d | %3d |\n", d, hdmin[v][j-jmin[v]], hdmax[v][j-jmin[v]], hdmindiff, hdmaxdiff);
5390 }
5391 else
5392 {
5393 printf(" | d: %4d | dn: jout | dx: jout | %3d | %3d |\n", d, hdmindiff, hdmaxdiff);
5394 }
5395 }
5396 }
5397 }
5398 printf("\nimin out : %d\n", imin_out);
5399 printf("imax out : %d\n", imax_out);
5400 printf("jmin out : %d\n", jmin_out);
5401 printf("jmax out : %d\n", jmax_out);
5402 printf("hdmin out : %d\n", hdmin_out);
5403 printf("hdmax out : %d\n", hdmax_out);
5404 printf("local used: %d\n", local_used);
5405
5406 if((imin_out + imax_out + jmin_out + jmax_out) > 0)
5407 {
5408 printf("ERROR, some of the i and j bands are going to prevent optimal alignment. Sorry.\n");
5409 }
5410
5411 return;
5412 }
5413
5414 /* EPN 01.18.06
5415 * Function: debug_print_hd_bands
5416 *
5417 * Purpose: Print out the v and j dependent hd bands.
5418 */
5419 void
5420 debug_print_hd_bands(CM_t *cm, int **hdmin, int **hdmax, int *jmin, int *jmax)
5421 {
5422 int v, j;
5423
5424 printf("\nPrinting hd bands :\n");
5425 printf("****************\n");
5426 for(v = 0; v < cm->M; v++)
5427 {
5428 for(j = jmin[v]; j <= jmax[v]; j++)
5429 {
5430 printf("band v:%d j:%d n:%d %-4s %-2s min:%d max:%d\n", v, j, cm->ndidx[v], Nodetype(cm->ndtype[cm->ndidx[v]]), Statetype(cm->sttype[v]), hdmin[v][j-jmin[v]], hdmax[v][j-jmin[v]]);
5431 }
5432 printf("\n");
5433 }
5434 printf("****************\n\n");
5435
5436 return;
5437 }
5438
5439 /* Function: debug_print_parsetree_and_ij_bands()
5440 * Date: EPN, Sun Jan 27 16:38:14 2008
5441 *
5442 * Purpose: Print a parsetree a la ParseTreeDump() but supplement it
5443 * with details on where the parsetree violates i and j bands
5444 * (if at all) from a cp9bands data structure.
5445 *
5446 * Args: fp - FILE to write output to.
5447 * tr - parsetree to examine.
5448 * cm - model that was aligned to dsq to generate the parsetree
5449 * dsq - digitized sequence that was aligned to cm to generate the parsetree
5450 * gamma - cumulative subsequence length probability distributions
5451 * used to generate the bands; from BandDistribution(); [0..v..M-1][0..W]
5452 * W - maximum window length W (gamma distributions range up to this)
5453 * cp9b - CP9 bands object with i and j bands
5454 *
5455 * Returns: (void)
5456 */
5457 void
5458 debug_print_parsetree_and_ij_bands(FILE *fp, Parsetree_t *tr, CM_t *cm, ESL_DSQ *dsq, CP9Bands_t *cp9b)
5459 {
5460 int x;
5461 char syml, symr;
5462 float tsc;
5463 float esc;
5464 int v,y;
5465 char mode;
5466
5467 /* Contract check */
5468 if(dsq == NULL) cm_Fail("In debug_print_parsetree_and_ij_bands(), dsq is NULL");
5469
5470 fprintf(fp, "%5s %6s %6s %7s %5s %5s %5s %5s %5s %5s %5s %5s %5s %5s %5s\n",
5471 " idx ", "emitl", "emitr", "state", " nxtl", " nxtr", " prv ", " tsc ", " esc ",
5472 " imin", " imax", "idiff", "jmin", "jmax", "jdiff");
5473 fprintf(fp, "%5s %6s %6s %7s %5s %5s %5s %5s %5s %5s %5s %5s %5s %5s %5s\n",
5474 "-----", "------", "------", "-------", "-----","-----", "-----","-----", "-----",
5475 "-----", "-----", "-----", "-----", "-----", "-----");
5476 for (x = 0; x < tr->n; x++)
5477 {
5478 v = tr->state[x];
5479 mode = tr->mode[x];
5480
5481 /* Set syml, symr: one char representation of what we emit, or ' '.
5482 * Set esc: emission score, or 0.
5483 * Only P, L, R states have emissions.
5484 */
5485 syml = symr = ' ';
5486 esc = 0.;
5487 if (cm->sttype[v] == MP_st) {
5488 if (mode == TRMODE_J || mode == TRMODE_L) syml = cm->abc->sym[dsq[tr->emitl[x]]];
5489 if (mode == TRMODE_J || mode == TRMODE_R) symr = cm->abc->sym[dsq[tr->emitr[x]]];
5490 if (mode == TRMODE_J) esc = DegeneratePairScore(cm->abc, cm->esc[v], dsq[tr->emitl[x]], dsq[tr->emitr[x]]);
5491 else if (mode == TRMODE_L) esc = cm->lmesc[v][dsq[tr->emitl[x]]];
5492 else if (mode == TRMODE_R) esc = cm->rmesc[v][dsq[tr->emitr[x]]];
5493 } else if ( (cm->sttype[v] == IL_st || cm->sttype[v] == ML_st) && (mode == TRMODE_J || mode == TRMODE_L) ) {
5494 syml = cm->abc->sym[dsq[tr->emitl[x]]];
5495 esc = esl_abc_FAvgScore(cm->abc, dsq[tr->emitl[x]], cm->esc[v]);
5496 } else if ( (cm->sttype[v] == IR_st || cm->sttype[v] == MR_st) && (mode == TRMODE_J || mode == TRMODE_R) ) {
5497 symr = cm->abc->sym[dsq[tr->emitr[x]]];
5498 esc = esl_abc_FAvgScore(cm->abc, dsq[tr->emitr[x]], cm->esc[v]);
5499 }
5500
5501 /* Set tsc: transition score, or 0.
5502 * B, E, and the special EL state (M, local end) have no transitions.
5503 */
5504 tsc = 0.;
5505 if (v != cm->M && cm->sttype[v] != B_st && cm->sttype[v] != E_st) {
5506 y = tr->state[tr->nxtl[x]];
5507
5508 if (tr->nxtl[x] == -1)
5509 ;
5510 else if (v == 0 && (cm->flags & CMH_LOCAL_BEGIN))
5511 tsc = cm->beginsc[y];
5512 else if (y == cm->M) /* CMH_LOCAL_END is presumably set, else this wouldn't happen */
5513 tsc = cm->endsc[v] + (cm->el_selfsc * (tr->emitr[x] - tr->emitl[x] + 1 - StateDelta(cm->sttype[v])));
5514 else /* y - cm->first[v] gives us the offset in the transition vector */
5515 tsc = cm->tsc[v][y - cm->cfirst[v]];
5516 }
5517
5518 /* Print the info line for this state
5519 */
5520 fprintf(fp, "%5d %5d%c %5d%c %5d%-2s %5d %5d %5d %5.2f %5.2f ",
5521 x, tr->emitl[x], syml, tr->emitr[x], symr, tr->state[x],
5522 Statetype(cm->sttype[v]), tr->nxtl[x], tr->nxtr[x], tr->prv[x], tsc, esc);
5523 if(tr->emitl[x] < cp9b->imin[tr->state[x]]) {
5524 fprintf(fp, "%5d %5d %5d ",
5525 cp9b->imin[tr->state[x]], cp9b->imax[tr->state[x]], (tr->emitl[x] - cp9b->imin[tr->state[x]]));
5526 }
5527 else if(tr->emitl[x] > cp9b->imax[tr->state[x]]) {
5528 fprintf(fp, "%5d %5d %5d ",
5529 cp9b->imin[tr->state[x]], cp9b->imax[tr->state[x]], (tr->emitl[x] - cp9b->imax[tr->state[x]]));
5530 }
5531 else {
5532 fprintf(fp, "%5d %5d %5s ",
5533 cp9b->imin[tr->state[x]], cp9b->imax[tr->state[x]], "");
5534 }
5535 if(tr->emitr[x] < cp9b->jmin[tr->state[x]]) {
5536 fprintf(fp, "%5d %5d %5d\n",
5537 cp9b->jmin[tr->state[x]], cp9b->jmax[tr->state[x]], (tr->emitr[x] - cp9b->jmin[tr->state[x]]));
5538 }
5539 else if(tr->emitr[x] > cp9b->jmax[tr->state[x]]) {
5540 fprintf(fp, "%5d %5d %5d\n",
5541 cp9b->jmin[tr->state[x]], cp9b->jmax[tr->state[x]], (tr->emitr[x] - cp9b->jmax[tr->state[x]]));
5542 }
5543 else {
5544 fprintf(fp, "%5d %5d %5s\n",
5545 cp9b->jmin[tr->state[x]], cp9b->jmax[tr->state[x]], "");
5546 }
5547 }
5548
5549 fprintf(fp, "%5s %6s %6s %7s %5s %5s %5s %5s %5s %5s %5s %5s %5s %5s %5s %5s\n",
5550 "-----", "------", "------", "-------", "-----","-----", "-----","-----", "-----",
5551 "-----", "-----", "-----", "-----", "-----", "-----", "-----");
5552
5553 fflush(fp);
5554 }
5555
5556 /*********************************************************************
5557 * Function: cp9_RelaxRootBandsForSearch()
5558 *
5559 * Purpose: In cp9_HMM2ijBands_OLD(), ROOT_S (state 0) sets imin[0]=imax[0]=i0,
5560 * and jmin[0]=jmax[0]=j0, which is important for alignment,
5561 * but during search enforces that the optimal alignment start
5562 * at i0 and end at j0, but when searching we want to relax this
5563 * requirement in case a higher scoring parse has different endpoints.
5564 * See code for details.
5565 *
5566 * Args:
5567 * cm the cm
5568 * i0 first position of seq
5569 * j0 last position of seq
5570 * int *imin imin[v] = first position in band on i for state v
5571 * int *imax imax[v] = last position in band on i for state v
5572 * int *jmin jmin[v] = first position in band on j for state v
5573 * int *jmax jmax[v] = last position in band on j for state v
5574 */
5575 void
5576 cp9_RelaxRootBandsForSearch(CM_t *cm, int i0, int j0, int *imin, int *imax, int *jmin, int *jmax)
5577 {
5578 int y, yoffset;
5579
5580 if(i0 == j0) return; /* this is a special vanishingly rare case, we've set otherwise illegal jmin, jmax values for MP states
5581 * b/c all MPs are impossible for a length 1 seq, do nothing in this case.
5582 */
5583 /* look at all children y of ROOT_S (v == 0) and set:
5584 * imin[0] = min_y imin[y];
5585 * imax[0] = max_y imax[y];
5586 * jmin[0] = min_y jmin[y];
5587 * jmax[0] = max_y jmax[y];
5588 */
5589 /* First look at children of 0 (these probs will be 0. if local begins on, but it doesn't matter for our purposes here) */
5590 for (yoffset = 0; yoffset < cm->cnum[0]; yoffset++) {
5591 y = cm->cnum[0] + yoffset;
5592 imin[0] = ESL_MIN(imin[0], imin[y]);
5593 imax[0] = ESL_MAX(imax[0], imax[y]);
5594 jmin[0] = ESL_MIN(jmin[0], jmin[y]);
5595 jmax[0] = ESL_MAX(jmax[0], jmax[y]);
5596 }
5597 /* now for possible local begins */
5598 if(cm->flags & CMH_LOCAL_BEGIN) {
5599 for (y = 1; y < cm->M; y++) {
5600 if(NOT_IMPOSSIBLE(cm->beginsc[y])) {
5601 imin[0] = ESL_MIN(imin[0], imin[y]);
5602 imax[0] = ESL_MAX(imax[0], imax[y]);
5603 jmin[0] = ESL_MIN(jmin[0], jmin[y]);
5604 jmax[0] = ESL_MAX(jmax[0], jmax[y]);
5605 }
5606 }
5607 }
5608 }
5609
5610 #endif
5611