Debian Codegraph @

src/cm_mx.c

raw symbols SCIP
1 /* CM_MX, CM_TR_MX, CM_HB_MX, CM_TR_HB_MX, CM_TR_SHADOW_MX, CM_HB_SHADOW_MX,
2 * CM_TR_HB_SHADOW_MX, CM_EMIT_MX, CM_SCAN_MX, CM_TR_SCAN_MX and
3 * GammaHitMx_t implementations: dynamic programming matrices for CMs.
4 *
5 * CM_HB_MX is based heavily on HMMER 3's p7_gmx.c module.
6 * That was the first of the CM_*_MX's data structures written,
7 * and all other CM_*_MX's were derived from that one.
8 *
9 * Table of contents:
10 * 1. CM_MX data structure functions,
11 * matrix of float scores for nonbanded CM alignment.
12 * 2. CM_TR_MX data structure functions,
13 * matrix of float scores for truncated nonbanded CM alignment.
14 * 3. CM_HB_MX data structure functions,
15 * matrix of float scores for HMM banded CM alignment/search
16 * 4. CM_TR_HB_MX data structure functions,
17 * matrix of float scores for truncated HMM banded CM alignment/search
18 * 5. CM_SHADOW_MX data structure functions,
19 * shadow matrix for tracing back nonbanded CM alignment.
20 * 6. CM_TR_SHADOW_MX data structure functions,
21 * shadow matrix for tracing back truncated nonbanded CM alignment.
22 * 7. CM_HB_SHADOW_MX data structure functions
23 * HMM banded shadow matrix for tracing back HMM banded CM parses
24 * 8. CM_TR_HB_SHADOW_MX data structure functions
25 * HMM banded shadow matrix for tracing back truncated HMM banded CM parses
26 * 9. CM_EMIT_MX data structure functions,
27 * 2D matrix of float scores for nonbanded optimal accuracy alignment
28 * and posterior annotation of alignments
29 * 10. CM_TR_EMIT_MX data structure functions,
30 * 2D matrix of float scores for nonbanded truncated optimal accuracy
31 * alignment and posterior annotation of alignments
32 * 11. CM_HB_EMIT_MX data structure functions,
33 * 2D matrix of float scores for HMM-banded optimal accuracy alignment
34 * and posterior annotation of alignments
35 * 12. CM_TR_HB_EMIT_MX data structure functions,
36 * 2D matrix of float scores for HMM-banded truncated optimal accuracy alignment
37 * and posterior annotation of alignments
38 * 13. CM_SCAN_MX data structure functions,
39 * auxiliary info and matrix of float and/or int scores for
40 * query dependent banded or non-banded CM DP search functions
41 * 14. CM_TR_SCAN_MX data structure functions,
42 * auxiliary info and matrix of float and/or int scores for
43 * query dependent banded or non-banded truncated CM DP search functions
44 * 15. GammaHitMx_t data structure functions,
45 * semi-HMM data structure for optimal resolution of overlapping
46 * hits for CM DP search functions
47 *
48 * EPN, Fri Oct 26 05:04:34 2007
49 */
50
51 #include <esl_config.h>
52 #include <p7_config.h>
53 #include "config.h"
54
55 #include <stdio.h>
56 #include <stdlib.h>
57
58 #include "easel.h"
59 #include "esl_vectorops.h"
60
61 #include "hmmer.h"
62
63 #include "infernal.h"
64
65 static int cm_scan_mx_integerize (CM_t *cm, CM_SCAN_MX *smx, char *errbuf);
66 static int cm_scan_mx_floatize (CM_t *cm, CM_SCAN_MX *smx, char *errbuf);
67 static int cm_scan_mx_freefloats (CM_t *cm, CM_SCAN_MX *smx);
68 static int cm_scan_mx_freeintegers (CM_t *cm, CM_SCAN_MX *smx);
69 static int cm_tr_scan_mx_integerize (CM_t *cm, CM_TR_SCAN_MX *trsmx, char *errbuf);
70 static int cm_tr_scan_mx_floatize (CM_t *cm, CM_TR_SCAN_MX *trsmx, char *errbuf);
71 static int cm_tr_scan_mx_freefloats (CM_t *cm, CM_TR_SCAN_MX *trsmx);
72 static int cm_tr_scan_mx_freeintegers(CM_t *cm, CM_TR_SCAN_MX *trsmx);
73
74 /*****************************************************************
75 * 1. CM_MX data structure functions,
76 * matrix of float scores for nonbanded CM alignment.
77 *****************************************************************/
78
79 /* Function: cm_mx_Create()
80 * Incept: EPN, Wed Sep 14 04:33:17 2011
81 *
82 * Purpose: Allocate a reusable, resizeable <CM_MX> for a CM.
83 *
84 * We've set this up so it should be easy to allocate
85 * aligned memory, though we're not doing this yet.
86 *
87 * Returns: a pointer to the new <CM_MX>.
88 *
89 * Throws: <NULL> on allocation error.
90 */
91 CM_MX *
92 cm_mx_Create(int M)
93 {
94 int status;
95 CM_MX *mx = NULL;
96 int v;
97 int allocL = 1;
98 int allocW = 1;
99
100 /* level 1: the structure itself */
101 ESL_ALLOC(mx, sizeof(CM_MX));
102 mx->dp = NULL;
103 mx->dp_mem = NULL;
104
105 /* level 2: deck (state) pointers, 0.1..M, go all the way to M
106 */
107 ESL_ALLOC(mx->dp, sizeof(float **) * (M+1));
108
109 /* level 3: dp cell memory, when creating only allocate 1 cell per state, for j = 0, d = 0 */
110 ESL_ALLOC(mx->dp_mem, sizeof(float) * (int64_t) (M+1) * (int64_t) allocL * (int64_t) allocW);
111
112 for (v = 0; v < M; v++) {
113 ESL_ALLOC(mx->dp[v], sizeof(float *) * (int64_t) allocL);
114 mx->dp[v][0] = mx->dp_mem + (int64_t) v * (int64_t) allocL * (int64_t) allocW;
115 }
116 /* allocate EL deck */
117 ESL_ALLOC(mx->dp[M], sizeof(float *) * (int64_t) allocL);
118 mx->dp[M][0] = mx->dp_mem + (int64_t) M * (int64_t) allocL * (int64_t) allocW;
119
120 mx->M = M;
121 mx->ncells_alloc = (int64_t) (M+1) * (int64_t) allocL * (int64_t) allocW;
122 mx->L = allocL; /* allocL = 1 */
123
124
125 /* calculate size, these are in order of when they were allocated */
126 mx->size_Mb = (float)
127 (sizeof(CM_MX) +
128 (mx->M+1) * sizeof(float **) + /* mx->dp[] ptrs */
129 mx->ncells_alloc * sizeof(float) + /* mx->dp_mem */
130 (mx->M+1) * allocL * sizeof(float *)); /* mx->dp[v][] ptrs */
131 mx->size_Mb *= 0.000001; /* convert to Mb */
132
133 return mx;
134
135 ERROR:
136 if (mx != NULL) cm_mx_Destroy(mx);
137 return NULL;
138 }
139
140
141 /* Function: cm_mx_GrowTo()
142 * Incept: EPN, Wed Sep 14 04:40:25 2011
143 *
144 * Purpose: Assures that a CM_MX matrix <mx> is allocated for a
145 * model of exactly <mx->M> states and target sequence of
146 * length L, reallocating memory as necessary.
147 *
148 * If local ends are on (cm->flags & CMH_LOCAL_END), allocates
149 * a full non-banded EL deck for the matrix.
150 *
151 * Checks to make sure desired matrix isn't too big (see throws).
152 *
153 * Args: cm - the CM the matrix is for
154 * mx - the matrix to grow
155 * errbuf - char buffer for reporting errors
156 * L - the length of the current target sequence we're aligning
157 * size_limit- max number of Mb for DP matrix, if matrix is bigger -> return eslERANGE
158 *
159 * Returns: <eslOK> on success, and <mx> may be reallocated upon
160 * return; any data that may have been in <mx> must be
161 * assumed to be invalidated.
162 *
163 * Throws: <eslERANGE> if required size to grow to exceeds <size_limit>.
164 * This should be caught and appropriately handled by caller.
165 * <eslEINCOMPAT> on contract violation
166 * <eslEMEM> on memory allocation error.
167 */
168 int
169 cm_mx_GrowTo(CM_t *cm, CM_MX *mx, char *errbuf, int L, float size_limit)
170 {
171 int status;
172 void *p;
173 int v, jp;
174 int64_t cur_size = 0;
175 int64_t ncells;
176 float Mb_needed; /* required size of matrix */
177 float Mb_alloc; /* allocated size of matrix, >= Mb_needed */
178 int have_el;
179 int realloced; /* did we reallocate mx->dp_mem? */
180
181 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
182
183 if((status = cm_mx_SizeNeeded(cm, errbuf, L, &ncells, &Mb_needed)) != eslOK) return status;
184 /*printf("Non-banded matrix requested size: %.2f Mb\n", Mb_needed);*/
185 ESL_DPRINTF2(("#DEBUG: Non-banded matrix requested size: %.2f Mb\n", Mb_needed));
186 if(Mb_needed > size_limit) ESL_FAIL(eslERANGE, errbuf, "requested non-banded DP mx of %.2f Mb > %.2f Mb limit.\nUse --mxsize, --maxtau or --tau.", Mb_needed, (float) size_limit);
187
188 /* must we realloc the full matrix? or can we get away
189 * with just jiggering the pointers, if total required num cells is
190 * less than or equal to what we already have alloc'ed?
191 */
192 realloced = FALSE;
193 if (ncells > mx->ncells_alloc) {
194 ESL_RALLOC(mx->dp_mem, p, sizeof(float) * ncells);
195 mx->ncells_alloc = ncells;
196 realloced = TRUE;
197 }
198
199 /* Determine the size of our matrix based on the size it needed to be (Mb_needed).
200 */
201 Mb_alloc = Mb_needed * 1000000; /* convert to bytes */
202 if(! realloced) {
203 Mb_alloc -= (float) (sizeof(float) * ncells);
204 Mb_alloc += (float) (sizeof(float) * mx->ncells_alloc);
205 }
206 Mb_alloc *= 0.000001; /* convert to Mb */
207
208 mx->ncells_valid = ncells;
209
210 /* reallocate the dp[v] ptrs */
211 for(v = 0; v < mx->M; v++) {
212 ESL_RALLOC(mx->dp[v], p, sizeof(float *) * (L+1));
213 }
214 if(have_el) {
215 ESL_RALLOC(mx->dp[mx->M], p, sizeof(float *) * (L+1));
216 }
217 else if(mx->dp[mx->M] != NULL) {
218 free(mx->dp[mx->M]);
219 mx->dp[mx->M] = NULL;
220 }
221
222 /* reset the pointers, we keep a tally of cur_size as we go
223 */
224 cur_size = 0;
225 for(v = 0; v < mx->M; v++) {
226 for(jp = 0; jp <= L; jp++) {
227 mx->dp[v][jp] = mx->dp_mem + cur_size;
228 cur_size += jp+1;
229 }
230 }
231 if(have_el) {
232 for(jp = 0; jp <= L; jp++) {
233 mx->dp[mx->M][jp] = mx->dp_mem + cur_size;
234 cur_size += jp+1;
235 }
236 }
237 /*printf("ncells %10" PRId64 " %10" PRId64 "\n", cur_size, mx->ncells_valid);*/
238 assert(cur_size == mx->ncells_valid);
239 ESL_DASSERT1((cur_size == mx->ncells_valid));
240
241 /* now update L and size_Mb */
242 mx->L = L; /* length of current seq we're valid for */
243 mx->size_Mb = Mb_alloc;
244
245 return eslOK;
246
247 ERROR:
248 return status;
249 }
250
251 /* Function: cm_mx_Destroy()
252 * Synopsis: Frees a DP matrix.
253 * Incept: EPN, Wed Sep 14 04:43:42 2011
254 *
255 * Purpose: Frees a <CM_MX>.
256 *
257 * Returns: (void)
258 */
259 void
260 cm_mx_Destroy(CM_MX *mx)
261 {
262 if (mx == NULL) return;
263 int v;
264
265 if (mx->dp != NULL) {
266 for (v = 0; v <= mx->M; v++)
267 if(mx->dp[v] != NULL) free(mx->dp[v]);
268 }
269 free(mx->dp);
270
271 if (mx->dp_mem != NULL) free(mx->dp_mem);
272 free(mx);
273 return;
274 }
275
276 /* Function: cm_mx_Dump()
277 * Synopsis: Dump a DP matrix to a stream, for diagnostics.
278 * Incept: EPN, Wed Sep 14 04:44:02 2011
279 *
280 * Purpose: Dump matrix <mx> to stream <fp> for diagnostics.
281 */
282 int
283 cm_mx_Dump(FILE *ofp, CM_MX *mx, int print_mx)
284 {
285 int v, j, d;
286
287 fprintf(ofp, "M: %d\n", mx->M);
288 fprintf(ofp, "L: %d\n", mx->L);
289 fprintf(ofp, "ncells_alloc: %" PRId64 "\nncells_valid: %" PRId64 "\n", mx->ncells_alloc, mx->ncells_valid);
290
291 if(print_mx) {
292 /* DP matrix data */
293 for (v = 0; v < mx->M; v++) {
294 for(j = 0; j <= mx->L; j++) {
295 for(d = 0; d <= j; d++) {
296 if(mx->dp[v]) fprintf(ofp, "dp[v:%5d][j:%5d][d:%5d] %8.4f\n", v, j, d, mx->dp[v][j][d]);
297 }
298 fprintf(ofp, "\n");
299 }
300 fprintf(ofp, "\n\n");
301 }
302 /* print EL deck, if it's valid */
303 v = mx->M;
304 if(mx->dp[v]) {
305 for(j = 0; j <= mx->L; j++) {
306 for(d = 0; d <= j; d++) {
307 fprintf(ofp, "dp[v:%5d][j:%5d][d:%5d] %8.4f\n", v, j, d, mx->dp[v][j][d]);
308 }
309 fprintf(ofp, "\n");
310 }
311 fprintf(ofp, "\n\n");
312 }
313 }
314 return eslOK;
315 }
316
317 /* Function: cm_mx_SizeNeeded()
318 * Incept: EPN, Wed Sep 14 04:44:40 2011
319 *
320 * Purpose: Given a model and sequence length, determine the number of
321 * cells and total size in Mb required in a CM_MX for
322 * the target.
323 *
324 * Return number of cells required in <ret_ncells> and size
325 * of required matrix in Mb in <ret_Mb>.
326 *
327 * Args: cm - the CM the matrix is for
328 * errbuf - char buffer for reporting errors
329 * L - the length of the current target sequence we're aligning
330 * ret_ncells - RETURN: number of matrix cells required
331 * ret_Mb - RETURN: required size of matrix in Mb
332 *
333 * Returns: <eslOK> on success
334 *
335 */
336 int
337 cm_mx_SizeNeeded(CM_t *cm, char *errbuf, int L, int64_t *ret_ncells, float *ret_Mb)
338 {
339 int v;
340 int64_t ncells;
341 int have_el;
342 float Mb_needed;
343 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
344
345 ncells = 0;
346
347 Mb_needed = (float)
348 (sizeof(CM_MX) +
349 (cm->M+1) * sizeof(float **)); /* mx->dp[] ptrs */
350
351 for(v = 0; v < cm->M; v++) {
352 Mb_needed += (float) (sizeof(float *) * (L+1)); /* mx->dp[v][] ptrs */
353 ncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
354 }
355 if(have_el) ncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5); /* space for EL deck */
356
357 Mb_needed += sizeof(float) * ncells; /* mx->dp_mem */
358 Mb_needed *= 0.000001; /* convert to megabytes */
359
360 if(ret_ncells != NULL) *ret_ncells = ncells;
361 if(ret_Mb != NULL) *ret_Mb = Mb_needed;
362
363 return eslOK;
364 }
365
366 /*****************************************************************
367 * 2. CM_TR_MX data structure functions,
368 * matrix of float scores for nonbanded CM alignment
369 * using Kolbe and Eddy's 'truncated' DP CYK/Inside algorithms
370 *****************************************************************/
371
372 /* Function: cm_tr_mx_Create()
373 * Incept: EPN, Sat Sep 10 11:48:37 2011
374 *
375 * Purpose: Allocate a reusable, resizeable <CM_TR_MX> for a CM.
376 *
377 * We've set this up so it should be easy to allocate
378 * aligned memory, though we're not doing this yet.
379 *
380 * Returns: a pointer to the new <CM_TR_MX>.
381 *
382 * Throws: <NULL> on allocation error.
383 */
384 CM_TR_MX *
385 cm_tr_mx_Create(CM_t *cm)
386 {
387 int status;
388 CM_TR_MX *mx = NULL;
389 int v, b;
390 int allocL = 1;
391 int allocW = 1;
392 int B = CMCountNodetype(cm, BIF_nd);
393 int M = cm->M;
394
395 /* level 1: the structure itself */
396 ESL_ALLOC(mx, sizeof(CM_TR_MX));
397 mx->Jdp = NULL;
398 mx->Jdp_mem = NULL;
399 mx->Ldp = NULL;
400 mx->Ldp_mem = NULL;
401 mx->Rdp = NULL;
402 mx->Rdp_mem = NULL;
403 mx->Tdp = NULL;
404 mx->Tdp_mem = NULL;
405
406 /* level 2: deck (state) pointers, 0.1..M, go all the way to M
407 * remember deck M is special, it only exists in J mode,
408 * and we allocate it only if nec (if local ends are on)
409 * in cm_tr_mx_GrowTo(). We still allocate a pointer to
410 * the M state deck in all modes though, it will remain
411 * NULL always for L,R,T.
412 */
413 ESL_ALLOC(mx->Jdp, sizeof(float **) * (M+1));
414 ESL_ALLOC(mx->Ldp, sizeof(float **) * (M+1));
415 ESL_ALLOC(mx->Rdp, sizeof(float **) * (M+1));
416 ESL_ALLOC(mx->Tdp, sizeof(float **) * (M+1)); /* ptrs to non-ROOT_S and non-B states will be NULL */
417
418 /* level 3: dp cell memory, when creating only allocate 1 cell per state, for j = 0, d = 0 */
419 ESL_ALLOC(mx->Jdp_mem, sizeof(float) * (int64_t) (M+1) * (int64_t) allocL * (int64_t) allocW);
420 ESL_ALLOC(mx->Ldp_mem, sizeof(float) * (int64_t) (M+1) * (int64_t) allocL * (int64_t) allocW);
421 ESL_ALLOC(mx->Rdp_mem, sizeof(float) * (int64_t) (M+1) * (int64_t) allocL * (int64_t) allocW);
422 ESL_ALLOC(mx->Tdp_mem, sizeof(float) * (int64_t) (B+1) * (int64_t) allocL * (int64_t) allocW); /* +1 is for the special ROOT_S deck */
423
424 b = 0;
425 for (v = 0; v < M; v++) {
426 ESL_ALLOC(mx->Jdp[v], sizeof(float *) * (int64_t) allocL);
427 ESL_ALLOC(mx->Ldp[v], sizeof(float *) * (int64_t) allocL);
428 ESL_ALLOC(mx->Rdp[v], sizeof(float *) * (int64_t) allocL);
429 mx->Jdp[v][0] = mx->Jdp_mem + (int64_t) v * (int64_t) allocL * (int64_t) allocW;
430 mx->Ldp[v][0] = mx->Ldp_mem + (int64_t) v * (int64_t) allocL * (int64_t) allocW;
431 mx->Rdp[v][0] = mx->Rdp_mem + (int64_t) v * (int64_t) allocL * (int64_t) allocW;
432
433 if(cm->sttype[v] == B_st || v == 0) { /* only B states and ROOT_S are valid */
434 ESL_ALLOC(mx->Tdp[v], sizeof(float *) * (int64_t) allocL);
435 mx->Tdp[v][0] = mx->Tdp_mem + (int64_t) b * (int64_t) allocL * (int64_t) allocW;
436 b++;
437 }
438 else {
439 mx->Tdp[v] = NULL;
440 }
441 }
442 /* allocate EL deck, for J, L, and R */
443 ESL_ALLOC(mx->Jdp[M], sizeof(float *) * (int64_t) allocL);
444 ESL_ALLOC(mx->Ldp[M], sizeof(float *) * (int64_t) allocL);
445 ESL_ALLOC(mx->Rdp[M], sizeof(float *) * (int64_t) allocL);
446 mx->Jdp[M][0] = mx->Jdp_mem + (int64_t) M * (int64_t) allocL * (int64_t) allocW;
447 mx->Ldp[M][0] = mx->Ldp_mem + (int64_t) M * (int64_t) allocL * (int64_t) allocW;
448 mx->Rdp[M][0] = mx->Rdp_mem + (int64_t) M * (int64_t) allocL * (int64_t) allocW;
449
450 mx->Tdp[M] = NULL;
451
452 mx->M = M;
453 mx->B = B;
454 mx->Jncells_alloc = (int64_t) (M+1) * (int64_t) allocL * (int64_t) allocW;
455 mx->Lncells_alloc = (int64_t) (M+1) * (int64_t) allocL * (int64_t) allocW;
456 mx->Rncells_alloc = (int64_t) (M+1) * (int64_t) allocL * (int64_t) allocW;
457 mx->Tncells_alloc = (int64_t) (B+1) * (int64_t) allocL * (int64_t) allocW;
458 mx->Jncells_valid = 0;
459 mx->Lncells_valid = 0;
460 mx->Rncells_valid = 0;
461 mx->Tncells_valid = 0;
462 mx->L = allocL; /* allocL = 1 */
463
464
465 /* calculate size, these are in order of when they were allocated */
466 mx->size_Mb = (float)
467 (sizeof(CM_TR_MX) +
468 (4 * (mx->M+1) * sizeof(float **)) + /* mx->{J,L,R}dp[] ptrs */
469 mx->Jncells_alloc * sizeof(float) + /* mx->Jdp_mem */
470 mx->Lncells_alloc * sizeof(float) + /* mx->Ldp_mem */
471 mx->Rncells_alloc * sizeof(float) + /* mx->Rdp_mem */
472 mx->Tncells_alloc * sizeof(float) + /* mx->Tdp_mem */
473 (3 * (mx->M+1) * allocL * sizeof(float *)) + /* mx->{J,L,R}dp[v][] ptrs */
474 (1 * (mx->B+1) * allocL * sizeof(float *))); /* mx->Tdp[v][] ptrs */
475 mx->size_Mb *= 0.000001; /* convert to Mb */
476
477 return mx;
478
479 ERROR:
480 if (mx != NULL) cm_tr_mx_Destroy(mx);
481 return NULL;
482 }
483
484 /* Function: cm_tr_mx_GrowTo()
485 * Incept: EPN, Sat Sep 10 11:50:23 2011
486 *
487 * Purpose: Assures that a CM_TR_MX matrix <mx> is allocated for a
488 * model of exactly <mx->M> states and target sequence of
489 * length L, reallocating memory as necessary.
490 *
491 * If local ends are on (cm->flags & CMH_LOCAL_END), allocates
492 * a full non-banded EL deck for the J matrix.
493 *
494 * Checks to make sure desired matrix isn't too big (see throws).
495 *
496 * Args: cm - the CM the matrix is for
497 * mx - the matrix to grow
498 * errbuf - char buffer for reporting errors
499 * L - the length of the current target sequence we're aligning
500 * size_limit- max number of Mb for DP matrix, if matrix is bigger -> return eslERANGE
501 *
502 * Returns: <eslOK> on success, and <mx> may be reallocated upon
503 * return; any data that may have been in <mx> must be
504 * assumed to be invalidated.
505 *
506 * Throws: <eslERANGE> if required size to grow to exceeds <size_limit>.
507 * This should be caught and appropriately handled by caller.
508 * <eslEINCOMPAT> on contract violation
509 * <eslEMEM> on memory allocation error.
510 */
511 int
512 cm_tr_mx_GrowTo(CM_t *cm, CM_TR_MX *mx, char *errbuf, int L, float size_limit)
513 {
514 int status;
515 void *p;
516 int v, jp;
517 int64_t Jcur_size = 0;
518 int64_t Lcur_size = 0;
519 int64_t Rcur_size = 0;
520 int64_t Tcur_size = 0;
521 int64_t Jncells;
522 int64_t Lncells;
523 int64_t Rncells;
524 int64_t Tncells;
525 float Mb_needed; /* required size of matrix, given the bands */
526 float Mb_alloc; /* allocated size of matrix, >= Mb_needed */
527 int have_el;
528 int realloced_J; /* did we reallocate mx->Jdp_mem? */
529 int realloced_L; /* did we reallocate mx->Ldp_mem? */
530 int realloced_R; /* did we reallocate mx->Rdp_mem? */
531 int realloced_T; /* did we reallocate mx->Tdp_mem? */
532
533 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
534
535 if((status = cm_tr_mx_SizeNeeded(cm, errbuf, L, &Jncells, &Lncells, &Rncells, &Tncells, &Mb_needed)) != eslOK) return status;
536 /*printf("Non-banded Tr matrix requested size: %.2f Mb\n", Mb_needed);*/
537 ESL_DPRINTF2(("#DEBUG: Non-banded Tr matrix requested size: %.2f Mb\n", Mb_needed));
538 if(Mb_needed > size_limit) ESL_FAIL(eslERANGE, errbuf, "requested non-banded Tr DP mx of %.2f Mb > %.2f Mb limit.\nUse --mxsize, --maxtau or --tau.", Mb_needed, (float) size_limit);
539
540 /* must we realloc the full {J,L,R.T}matrices? or can we get away
541 * with just jiggering the pointers, if total required num cells is
542 * less than or equal to what we already have alloc'ed?
543 */
544 realloced_J = realloced_L = realloced_R = realloced_T = FALSE;
545 if (Jncells > mx->Jncells_alloc) {
546 ESL_RALLOC(mx->Jdp_mem, p, sizeof(float) * Jncells);
547 mx->Jncells_alloc = Jncells;
548 realloced_J = TRUE;
549 }
550 if (Lncells > mx->Lncells_alloc) {
551 ESL_RALLOC(mx->Ldp_mem, p, sizeof(float) * Lncells);
552 mx->Lncells_alloc = Lncells;
553 realloced_L = TRUE;
554 }
555 if (Rncells > mx->Rncells_alloc) {
556 ESL_RALLOC(mx->Rdp_mem, p, sizeof(float) * Rncells);
557 mx->Rncells_alloc = Rncells;
558 realloced_R = TRUE;
559 }
560 if (Tncells > mx->Tncells_alloc) {
561 ESL_RALLOC(mx->Tdp_mem, p, sizeof(float) * Tncells);
562 mx->Tncells_alloc = Tncells;
563 realloced_T = TRUE;
564 }
565
566 /* Determine the size of our matrix based on the size it needed to be (Mb_needed).
567 * This is tricky, for each matrix not reallocated, we have to adjust Mb_needed
568 * so it uses previously allocated size of that matrix.
569 */
570 Mb_alloc = Mb_needed * 1000000; /* convert to bytes */
571 if(! realloced_J) {
572 Mb_alloc -= (float) (sizeof(float) * Jncells);
573 Mb_alloc += (float) (sizeof(float) * mx->Jncells_alloc);
574 }
575 if(! realloced_L) {
576 Mb_alloc -= (float) (sizeof(float) * Lncells);
577 Mb_alloc += (float) (sizeof(float) * mx->Lncells_alloc);
578 }
579 if(! realloced_R) {
580 Mb_alloc -= (float) (sizeof(float) * Rncells);
581 Mb_alloc += (float) (sizeof(float) * mx->Rncells_alloc);
582 }
583 if(! realloced_T) {
584 Mb_alloc -= (float) (sizeof(float) * Tncells);
585 Mb_alloc += (float) (sizeof(float) * mx->Tncells_alloc);
586 }
587 /* note if we didn't reallocate any of the four matrices, Mb_alloc == Mb_needed */
588 Mb_alloc *= 0.000001; /* convert to Mb */
589
590 mx->Jncells_valid = Jncells;
591 mx->Lncells_valid = Lncells;
592 mx->Rncells_valid = Rncells;
593 mx->Tncells_valid = Tncells;
594
595 /* reallocate the {J,L,R,T}dp[v] ptrs */
596 for(v = 0; v < mx->M; v++) {
597 ESL_RALLOC(mx->Jdp[v], p, sizeof(float *) * (L+1));
598 ESL_RALLOC(mx->Ldp[v], p, sizeof(float *) * (L+1));
599 ESL_RALLOC(mx->Rdp[v], p, sizeof(float *) * (L+1));
600 if(cm->sttype[v] == B_st || v == 0) { /* valid for B states and the ROOT_S */
601 ESL_RALLOC(mx->Tdp[v], p, sizeof(float *) * (L+1));
602 }
603 else {
604 mx->Tdp[v] = NULL;
605 }
606 }
607 if(have_el) {
608 ESL_RALLOC(mx->Jdp[mx->M], p, sizeof(float *) * (L+1));
609 ESL_RALLOC(mx->Ldp[mx->M], p, sizeof(float *) * (L+1));
610 ESL_RALLOC(mx->Rdp[mx->M], p, sizeof(float *) * (L+1));
611 /* Tdp is NULL for cm->M */
612 }
613 else {
614 if(mx->Jdp[mx->M] != NULL) {
615 free(mx->Jdp[mx->M]);
616 mx->Jdp[mx->M] = NULL;
617 }
618 if(mx->Ldp[mx->M] != NULL) {
619 free(mx->Ldp[mx->M]);
620 mx->Ldp[mx->M] = NULL;
621 }
622 if(mx->Rdp[mx->M] != NULL) {
623 free(mx->Rdp[mx->M]);
624 mx->Rdp[mx->M] = NULL;
625 }
626 }
627
628 /* reset the pointers, we keep a tally of cur_size as we go
629 */
630 Jcur_size = 0;
631 Lcur_size = 0;
632 Rcur_size = 0;
633 Tcur_size = 0;
634 for(v = 0; v < mx->M; v++) {
635 for(jp = 0; jp <= L; jp++) {
636 mx->Jdp[v][jp] = mx->Jdp_mem + Jcur_size;
637 mx->Ldp[v][jp] = mx->Ldp_mem + Lcur_size;
638 mx->Rdp[v][jp] = mx->Rdp_mem + Rcur_size;
639 Jcur_size += jp+1;
640 Lcur_size += jp+1;
641 Rcur_size += jp+1;
642 if(cm->sttype[v] == B_st || v == 0) { /* valid for B states and the ROOT_S */
643 mx->Tdp[v][jp] = mx->Tdp_mem + Tcur_size;
644 Tcur_size += jp+1;
645 }
646 }
647 }
648 if(have_el) {
649 for(jp = 0; jp <= L; jp++) {
650 mx->Jdp[mx->M][jp] = mx->Jdp_mem + Jcur_size;
651 Jcur_size += jp+1;
652 }
653 for(jp = 0; jp <= L; jp++) {
654 mx->Ldp[mx->M][jp] = mx->Ldp_mem + Lcur_size;
655 Lcur_size += jp+1;
656 }
657 for(jp = 0; jp <= L; jp++) {
658 mx->Rdp[mx->M][jp] = mx->Rdp_mem + Rcur_size;
659 Rcur_size += jp+1;
660 }
661 }
662 /*printf("J ncells %10" PRId64 " %10" PRId64 "\n", Jcur_size, mx->Jncells_valid);
663 printf("L ncells %10" PRId64 " %10" PRId64 "\n", Lcur_size, mx->Lncells_valid);
664 printf("R ncells %10" PRId64 " %10" PRId64 "\n", Rcur_size, mx->Rncells_valid);
665 printf("T ncells %10" PRId64 " %10" PRId64 "\n", Tcur_size, mx->Tncells_valid);*/
666 assert(Jcur_size == mx->Jncells_valid);
667 assert(Lcur_size == mx->Lncells_valid);
668 assert(Rcur_size == mx->Rncells_valid);
669 assert(Tcur_size == mx->Tncells_valid);
670 ESL_DASSERT1((Jcur_size == mx->Jncells_valid));
671 ESL_DASSERT1((Lcur_size == mx->Lncells_valid));
672 ESL_DASSERT1((Rcur_size == mx->Rncells_valid));
673 ESL_DASSERT1((Tcur_size == mx->Tncells_valid));
674
675 /* now update L and size_Mb */
676 mx->L = L; /* length of current seq we're valid for */
677 mx->size_Mb = Mb_alloc;
678
679 return eslOK;
680
681 ERROR:
682 return status;
683 }
684
685 /* Function: cm_tr_mx_Destroy()
686 * Synopsis: Frees a DP matrix.
687 * Incept: EPN, Thu Aug 25 14:51:15 2011
688 *
689 * Purpose: Frees a <CM_TR_MX>.
690 *
691 * Returns: (void)
692 */
693 void
694 cm_tr_mx_Destroy(CM_TR_MX *mx)
695 {
696 if (mx == NULL) return;
697 int v;
698
699 if (mx->Jdp != NULL) {
700 for (v = 0; v <= mx->M; v++)
701 if(mx->Jdp[v] != NULL) free(mx->Jdp[v]);
702 }
703 free(mx->Jdp);
704
705 if (mx->Ldp != NULL) {
706 for (v = 0; v <= mx->M; v++)
707 if(mx->Ldp[v] != NULL) free(mx->Ldp[v]);
708 }
709 free(mx->Ldp);
710
711 if (mx->Rdp != NULL) {
712 for (v = 0; v <= mx->M; v++)
713 if(mx->Rdp[v] != NULL) free(mx->Rdp[v]);
714 }
715 free(mx->Rdp);
716
717 if (mx->Tdp != NULL) {
718 for (v = 0; v <= mx->M; v++)
719 if(mx->Tdp[v] != NULL) free(mx->Tdp[v]);
720 }
721 free(mx->Tdp);
722
723 if (mx->Jdp_mem != NULL) free(mx->Jdp_mem);
724 if (mx->Ldp_mem != NULL) free(mx->Ldp_mem);
725 if (mx->Rdp_mem != NULL) free(mx->Rdp_mem);
726 if (mx->Tdp_mem != NULL) free(mx->Tdp_mem);
727 free(mx);
728 return;
729 }
730
731 /* Function: cm_tr_mx_Dump()
732 * Synopsis: Dump a DP matrix to a stream, for diagnostics.
733 * Incept: EPN, Thu Aug 25 14:52:40 2011
734 *
735 * Purpose: Dump matrix <mx> to stream <fp> for diagnostics.
736 */
737 int
738 cm_tr_mx_Dump(FILE *ofp, CM_TR_MX *mx, char mode, int print_mx)
739 {
740 int status;
741 int v, j, d;
742 int fill_L, fill_R, fill_T; /* are the L, R, and T matrices valid? */
743
744 fprintf(ofp, "M: %d\n", mx->M);
745 fprintf(ofp, "B: %d\n", mx->B);
746 fprintf(ofp, "L: %d\n", mx->L);
747 fprintf(ofp, "Jncells_alloc: %" PRId64 "\nJncells_valid: %" PRId64 "\n", mx->Jncells_alloc, mx->Jncells_valid);
748 fprintf(ofp, "Lncells_alloc: %" PRId64 "\nLncells_valid: %" PRId64 "\n", mx->Lncells_alloc, mx->Lncells_valid);
749 fprintf(ofp, "Rncells_alloc: %" PRId64 "\nRncells_valid: %" PRId64 "\n", mx->Rncells_alloc, mx->Rncells_valid);
750 fprintf(ofp, "Tncells_alloc: %" PRId64 "\nTncells_valid: %" PRId64 "\n", mx->Tncells_alloc, mx->Tncells_valid);
751 fprintf(ofp, "mode: %d\n", mode);
752
753 if((status = cm_TrFillFromMode(mode, &fill_L, &fill_R, &fill_T)) != eslOK) return status;
754
755 if(print_mx) {
756 /* DP matrix data */
757 for (v = 0; v < mx->M; v++) {
758 for(j = 0; j <= mx->L; j++) {
759 for(d = 0; d <= j; d++) {
760 if(mx->Jdp[v]) fprintf(ofp, "Jdp[v:%5d][j:%5d][d:%5d] %8.4f\n", v, j, d, mx->Jdp[v][j][d]);
761 if(fill_L && mx->Ldp[v]) fprintf(ofp, "Ldp[v:%5d][j:%5d][d:%5d] %8.4f\n", v, j, d, mx->Ldp[v][j][d]);
762 if(fill_R && mx->Rdp[v]) fprintf(ofp, "Rdp[v:%5d][j:%5d][d:%5d] %8.4f\n", v, j, d, mx->Rdp[v][j][d]);
763 if(fill_T && mx->Tdp[v]) fprintf(ofp, "Tdp[v:%5d][j:%5d][d:%5d] %8.4f\n", v, j, d, mx->Tdp[v][j][d]);
764 }
765 fprintf(ofp, "\n");
766 }
767 fprintf(ofp, "\n\n");
768 }
769 /* print EL deck, if it's valid */
770 v = mx->M;
771 for(j = 0; j <= mx->L; j++) {
772 for(d = 0; d <= j; d++) {
773 if(mx->Jdp[v]) fprintf(ofp, "Jdp[v:%5d][j:%5d][d:%5d] %8.4f\n", v, j, d, mx->Jdp[v][j][d]);
774 if(fill_L && mx->Ldp[v]) fprintf(ofp, "Ldp[v:%5d][j:%5d][d:%5d] %8.4f\n", v, j, d, mx->Ldp[v][j][d]);
775 if(fill_R && mx->Rdp[v]) fprintf(ofp, "Rdp[v:%5d][j:%5d][d:%5d] %8.4f\n", v, j, d, mx->Rdp[v][j][d]);
776 }
777 fprintf(ofp, "\n");
778 }
779 fprintf(ofp, "\n\n");
780 }
781 return eslOK;
782 }
783
784 /* Function: cm_tr_mx_SizeNeeded()
785 * Incept: EPN, Thu Aug 25 14:55:25 2011
786 *
787 * Purpose: Given a model and sequence length, determine the number of
788 * cells and total size in Mb required in a CM_TR_MX for
789 * the target given the bands.
790 *
791 * Return number of cells required given the
792 * in <ret_n{J,L,R,T}cells> and size of required
793 * matrix in Mb in <ret_Mb>.
794 *
795 * Args: cm - the CM the matrix is for
796 * errbuf - char buffer for reporting errors
797 * L - the length of the current target sequence we're aligning
798 * ret_Jncells - RETURN: number of J matrix cells required
799 * ret_Lncells - RETURN: number of L matrix cells required
800 * ret_Rncells - RETURN: number of R matrix cells required
801 * ret_Tncells - RETURN: number of T matrix cells required
802 * ret_Mb - RETURN: required size of matrix in Mb
803 *
804 * Returns: <eslOK> on success
805 *
806 */
807 int
808 cm_tr_mx_SizeNeeded(CM_t *cm, char *errbuf, int L, int64_t *ret_Jncells, int64_t *ret_Lncells, int64_t *ret_Rncells, int64_t *ret_Tncells, float *ret_Mb)
809 {
810 int v;
811 int64_t Jncells, Lncells, Rncells, Tncells;
812 int have_el;
813 float Mb_needed;
814 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
815
816 Jncells = 0;
817 Lncells = 0;
818 Rncells = 0;
819 Tncells = 0;
820 Mb_needed = (float)
821 (sizeof(CM_TR_MX) +
822 (4 * (cm->M+1) * sizeof(float **))); /* mx->{J,L,R,T}dp[] ptrs */
823
824 for(v = 0; v < cm->M; v++) {
825 Mb_needed += (float) (sizeof(float *) * (L+1)); /* mx->Jdp[v][] ptrs */
826 Mb_needed += (float) (sizeof(float *) * (L+1)); /* mx->Ldp[v][] ptrs */
827 Mb_needed += (float) (sizeof(float *) * (L+1)); /* mx->Rdp[v][] ptrs */
828 if(cm->sttype[v] == B_st || v == 0) Mb_needed += (float) (sizeof(float *) * (L+1)); /* mx->Tdp[v][] ptrs */
829 Jncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
830 Lncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
831 Rncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
832 if(cm->sttype[v] == B_st || v == 0) Tncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
833 }
834 if(have_el) { /* space for EL deck */
835 Jncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
836 Lncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
837 Rncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
838 }
839
840 Mb_needed += sizeof(float) * Jncells; /* mx->Jdp_mem */
841 Mb_needed += sizeof(float) * Lncells; /* mx->Ldp_mem */
842 Mb_needed += sizeof(float) * Rncells; /* mx->Rdp_mem */
843 Mb_needed += sizeof(float) * Tncells; /* mx->Tdp_mem */
844 Mb_needed *= 0.000001; /* convert to megabytes */
845
846 if(ret_Jncells != NULL) *ret_Jncells = Jncells;
847 if(ret_Lncells != NULL) *ret_Lncells = Lncells;
848 if(ret_Rncells != NULL) *ret_Rncells = Rncells;
849 if(ret_Tncells != NULL) *ret_Tncells = Tncells;
850 if(ret_Mb != NULL) *ret_Mb = Mb_needed;
851
852 return eslOK;
853 }
854
855 /*****************************************************************
856 * 3. CM_HB_MX data structure functions,
857 * matrix of float scores for HMM banded CM alignment/search.
858 *****************************************************************/
859
860 /* Function: cm_hb_mx_Create()
861 * Incept: EPN, Fri Oct 26 05:05:07 2007
862 *
863 * Purpose: Allocate a reusable, resizeable <CM_HB_MX> for a CM
864 * given a CP9Bands_t object that defines the bands.
865 *
866 * We've set this up so it should be easy to allocate
867 * aligned memory, though we're not doing this yet.
868 *
869 * Returns: a pointer to the new <CM_HB_MX>.
870 *
871 * Throws: <NULL> on allocation error.
872 */
873 CM_HB_MX *
874 cm_hb_mx_Create(int M)
875 {
876 int status;
877 CM_HB_MX *mx = NULL;
878 int v;
879 int allocL = 1;
880 int allocW = 1;
881
882 /* level 1: the structure itself */
883 ESL_ALLOC(mx, sizeof(CM_HB_MX));
884 mx->dp = NULL;
885 mx->dp_mem = NULL;
886 mx->cp9b = NULL;
887
888 /* level 2: deck (state) pointers, 0.1..M, go all the way to M
889 * remember deck M is special, as it has no bands, we allocate
890 * it only if nec (if local ends are on) in cm_hb_mx_GrowTo()
891 */
892 ESL_ALLOC(mx->dp, sizeof(float **) * (M+1));
893
894 /* level 3: dp cell memory, when creating only allocate 1 cell per state, for j = 0, d = 0 */
895 ESL_ALLOC(mx->dp_mem, sizeof(float) * (int64_t) (M+1) * (int64_t) allocL * (int64_t) allocW);
896 ESL_ALLOC(mx->nrowsA, sizeof(int) * (int64_t) (M+1));
897 for (v = 0; v <= M; v++) {
898 ESL_ALLOC(mx->dp[v], sizeof(float *) * (int64_t) allocL);
899 mx->nrowsA[v] = allocL;
900 mx->dp[v][0] = mx->dp_mem + (int64_t) v * (int64_t) allocL * (int64_t) allocW;
901 }
902 mx->M = M;
903 mx->ncells_alloc = (int64_t) (M+1) * (int64_t) allocL * (int64_t) allocW;
904 mx->ncells_valid = 0;
905 mx->L = allocL; /* allocL = 1 */
906
907
908 /* calculate size, these are in order of when they were allocated */
909 mx->size_Mb = (float)
910 (sizeof(CM_HB_MX) +
911 ((mx->M+1) * sizeof(float **)) + /* mx->dp[] ptrs */
912 mx->ncells_alloc * sizeof(float) + /* mx->dp_mem */
913 ((mx->M+1) * sizeof(int)) + /* mx->nrowsA */
914 ((mx->M+1) * allocL * sizeof(float *))); /* mx->dp[v][] ptrs */
915 mx->size_Mb *= 0.000001; /* convert to Mb */
916
917 return mx;
918
919 ERROR:
920 if (mx != NULL) cm_hb_mx_Destroy(mx);
921 return NULL;
922 }
923
924 /* Function: cm_hb_mx_GrowTo()
925 * Incept: EPN, Fri Oct 26 05:19:49 2007
926 *
927 * Purpose: Assures that a DP matrix <mx> is allocated
928 * for a model of exactly <mx->M> states and required number of
929 * total cells. Determines new required size from
930 * the CP9Bands_t object passed in, and reallocates if
931 * necessary.
932 *
933 * If local ends are on (cm->flags & CMH_LOCAL_END), allocates
934 * a full non-banded EL deck.
935 *
936 * Checks to make sure desired matrix isn't too big (see throws).
937 *
938 * Args: cm - the CM the matrix is for
939 * mx - the matrix to grow
940 * errbuf - char buffer for reporting errors
941 * cp9b - the bands for the current target sequence
942 * L - the length of the current target sequence we're aligning
943 * size_limit- max number of Mb for DP matrix, if matrix is bigger -> return eslERANGE
944 *
945 * Returns: <eslOK> on success, and <mx> may be reallocated upon
946 * return; any data that may have been in <mx> must be
947 * assumed to be invalidated.
948 *
949 * Throws: <eslERANGE> if required size to grow to exceeds <size_limit>.
950 * This should be caught and appropriately handled by caller.
951 * <eslEINCOMPAT> on contract violation
952 * <eslEMEM> on memory allocation error.
953 */
954 int
955 cm_hb_mx_GrowTo(CM_t *cm, CM_HB_MX *mx, char *errbuf, CP9Bands_t *cp9b, int L, float size_limit)
956 {
957 int status;
958 void *p;
959 int v, jp;
960 int64_t cur_size = 0;
961 int64_t ncells;
962 int jbw;
963 float Mb_needed; /* required size of matrix, given the bands */
964 float Mb_alloc; /* allocated size of matrix, >= Mb_needed */
965 int have_el;
966 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
967
968 /* contract check, number of states (M) is something we don't change
969 * so check this matrix has same number of 1st dim state ptrs that
970 * cp9b has */
971 if(cp9b == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_hb_mx_GrowTo() entered with cp9b == NULL.\n");
972 if(cp9b->cm_M != mx->M) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_hb_mx_GrowTo() entered with mx->M: (%d) != cp9b->M (%d)\n", mx->M, cp9b->cm_M);
973
974 if((status = cm_hb_mx_SizeNeeded(cm, errbuf, cp9b, L, &ncells, &Mb_needed)) != eslOK) return status;
975 /* printf("HMM banded matrix requested size: %.2f Mb\n", Mb_needed); */
976 ESL_DPRINTF2(("#DEBUG: HMM banded matrix requested size: %.2f Mb\n", Mb_needed));
977 if(Mb_needed > size_limit) ESL_FAIL(eslERANGE, errbuf, "requested HMM banded DP mx of %.2f Mb > %.2f Mb limit.\nUse --mxsize, --maxtau or --tau.", Mb_needed, (float) size_limit);
978
979 /* check if we should free and reallocate the matrix */
980 if((mx->size_Mb > (0.5 * size_limit)) && /* matrix is >= 0.5 * size of our limit (based on bands from previous sequence) */
981 (mx->size_Mb > (1.25 * Mb_needed))) { /* matrix is at least 25% bigger than we need to process current sequence */
982 free(mx->dp_mem);
983 mx->dp_mem = NULL;
984 mx->ncells_alloc = 0;
985 }
986
987 /* must we realloc the full matrix? or can we get away with just
988 * jiggering the pointers, if total required num cells is less
989 * than or equal to what we already have alloc'ed?
990 */
991 if (ncells > mx->ncells_alloc) {
992 ESL_RALLOC(mx->dp_mem, p, sizeof(float) * ncells);
993 mx->ncells_alloc = ncells;
994 Mb_alloc = Mb_needed;
995 }
996 else {
997 /* mx->dp_mem remains as it is allocated, set Mb_alloc accordingly
998 * (this is not just mx->size_Mb, because size of pointer arrays
999 * may change, for example)
1000 */
1001 Mb_alloc = Mb_needed * 1000000.; /* convert to bytes */
1002 Mb_alloc -= ((float) (sizeof(float) * ncells));
1003 Mb_alloc += ((float) (sizeof(float) * mx->ncells_alloc));
1004 Mb_alloc *= 0.000001; /* convert to Mb */
1005 }
1006 mx->ncells_valid = ncells;
1007
1008 for(v = 0; v < mx->M; v++) {
1009 jbw = cp9b->jmax[v] - cp9b->jmin[v] + 1;
1010 if(jbw > mx->nrowsA[v]) {
1011 ESL_RALLOC(mx->dp[v], p, sizeof(float *) * jbw);
1012 mx->nrowsA[v] = jbw;
1013 }
1014 }
1015 if(have_el) {
1016 jbw = L+1;
1017 if(jbw > mx->nrowsA[mx->M]) {
1018 ESL_RALLOC(mx->dp[mx->M], p, sizeof(float *) * jbw);
1019 mx->nrowsA[mx->M] = jbw;
1020 }
1021 }
1022
1023 /* reset the pointers, we keep a tally of cur_size as we go,
1024 * we could precalc it and store it for each v,j, but that
1025 * would be wasteful, as we'll only use the matrix configured
1026 * this way once, in a banded CYK run.
1027 */
1028 cur_size = 0;
1029 for(v = 0; v < mx->M; v++) {
1030 for(jp = 0; jp <= (cp9b->jmax[v] - cp9b->jmin[v]); jp++) {
1031 mx->dp[v][jp] = mx->dp_mem + cur_size;
1032 cur_size += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
1033 }
1034 }
1035 if(have_el) {
1036 for(jp = 0; jp <= L; jp++) {
1037 mx->dp[mx->M][jp] = mx->dp_mem + cur_size;
1038 cur_size += jp + 1;
1039 }
1040 }
1041 ESL_DASSERT1((cur_size == mx->ncells_valid));
1042 /*printf("ncells %10" PRId64 " %10" PRId64 "\n", cur_size, mx->ncells_valid);*/
1043
1044 mx->cp9b = cp9b; /* just a reference */
1045
1046 /* now update L and size_Mb */
1047 mx->L = L; /* length of current seq we're valid for */
1048 mx->size_Mb = Mb_alloc;
1049
1050 return eslOK;
1051
1052 ERROR:
1053 return status;
1054 }
1055
1056 /* Function: cm_hb_mx_Destroy()
1057 * Synopsis: Frees a DP matrix.
1058 * Incept: EPN, Fri Oct 26 09:04:04 2007
1059 *
1060 * Purpose: Frees a <CM_HB_MX>.
1061 *
1062 * Returns: (void)
1063 */
1064 void
1065 cm_hb_mx_Destroy(CM_HB_MX *mx)
1066 {
1067 if (mx == NULL) return;
1068 int v;
1069
1070 if (mx->dp != NULL) {
1071 for (v = 0; v <= mx->M; v++)
1072 if(mx->dp[v] != NULL) free(mx->dp[v]);
1073 }
1074 free(mx->dp);
1075
1076 if (mx->nrowsA != NULL) free(mx->nrowsA);
1077 if (mx->dp_mem != NULL) free(mx->dp_mem);
1078 free(mx);
1079 return;
1080 }
1081
1082 /* Function: cm_hb_mx_Dump()
1083 * Synopsis: Dump a DP matrix to a stream, for diagnostics.
1084 * Incept: EPN, Fri Oct 26 09:04:46 2007
1085 *
1086 * Purpose: Dump matrix <mx> to stream <fp> for diagnostics.
1087 */
1088 int
1089 cm_hb_mx_Dump(FILE *ofp, CM_HB_MX *mx, int print_mx)
1090 {
1091 int v, jp, j, dp, d;
1092
1093 fprintf(ofp, "M: %d\nL: %d\ncells_alloc: %" PRId64 "\nncells_valid: %" PRId64 "\n", mx->M, mx->L, mx->ncells_alloc, mx->ncells_valid);
1094
1095 if(print_mx) {
1096 /* DP matrix data */
1097 for (v = 0; v < mx->M; v++) {
1098 for(jp = 0; jp <= mx->cp9b->jmax[v] - mx->cp9b->jmin[v]; jp++) {
1099 j = jp + mx->cp9b->jmin[v];
1100 for(dp = 0; dp <= mx->cp9b->hdmax[v][jp] - mx->cp9b->hdmin[v][jp]; dp++) {
1101 d = dp + mx->cp9b->hdmin[v][jp];
1102 fprintf(ofp, "dp[v:%5d][j:%5d][d:%5d] %8.4f\n", v, j, d, mx->dp[v][jp][dp]);
1103 }
1104 fprintf(ofp, "\n");
1105 }
1106 fprintf(ofp, "\n\n");
1107 }
1108 /* print EL deck, if it's valid */
1109 v = mx->M;
1110 if(mx->nrowsA[mx->M] == (mx->L+1)) {
1111 for(j = 0; j <= mx->L; j++) {
1112 for(d = 0; d <= j; d++) {
1113 fprintf(ofp, "dp[v:%5d][j:%5d][d:%5d] %8.4f\n", v, j, d, mx->dp[v][j][d]);
1114 }
1115 fprintf(ofp, "\n");
1116 }
1117 fprintf(ofp, "\n\n");
1118 }
1119 }
1120 return eslOK;
1121 }
1122
1123 /* Function: cm_hb_mx_SizeNeeded()
1124 * Incept: EPN, Thu Aug 11 14:51:04 2011
1125 *
1126 * Purpose: Given a model and CP9_bands_t object with
1127 * pre-calced bands for a target, determine the number
1128 * of cells and total size in Mb required in a CM_HB_MX
1129 * for the target given the bands.
1130 *
1131 * Return number of cells required given the bands
1132 * in <cp9b> in <ret_ncells> and size of required
1133 * matrix in Mb in <ret_Mb>.
1134 *
1135 * Args: cm - the CM the matrix is for
1136 * errbuf - char buffer for reporting errors
1137 * cp9b - the bands for the current target sequence
1138 * L - the length of the current target sequence we're aligning
1139 * ret_ncells - RETURN: number of cells required
1140 * ret_Mb - RETURN: required size of matrix in Mb
1141 *
1142 * Returns: <eslOK> on success
1143 *
1144 */
1145 int
1146 cm_hb_mx_SizeNeeded(CM_t *cm, char *errbuf, CP9Bands_t *cp9b, int L, int64_t *ret_ncells, float *ret_Mb)
1147 {
1148 int v, jp;
1149 int64_t ncells;
1150 int jbw;
1151 int have_el;
1152 float Mb_needed;
1153 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
1154
1155 /* contract check */
1156 if(cp9b == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_hb_mx_SizeNeeded() entered with cp9b == NULL.\n");
1157
1158 ncells = 0;
1159 Mb_needed = (float)
1160 (sizeof(CM_HB_MX) +
1161 ((cp9b->cm_M+1) * sizeof(float **)) + /* mx->dp[] ptrs */
1162 ((cp9b->cm_M+1) * sizeof(int))); /* mx->nrowsA */
1163
1164 for(v = 0; v < cp9b->cm_M; v++) {
1165 jbw = cp9b->jmax[v] - cp9b->jmin[v];
1166 Mb_needed += (float) (sizeof(float *) * (jbw+1)); /* mx->dp[v][] ptrs */
1167 for(jp = 0; jp <= jbw; jp++)
1168 ncells += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
1169 }
1170 if(have_el) ncells += (int64_t) ( (int64_t) (L+2) * (int64_t) (L+1) * 0.5); /* space for EL deck */
1171
1172 Mb_needed += sizeof(float) * ncells; /* mx->dp_mem */
1173 Mb_needed *= 0.000001; /* convert to megabytes */
1174
1175 if(ret_ncells != NULL) *ret_ncells = ncells;
1176 if(ret_Mb != NULL) *ret_Mb = Mb_needed;
1177
1178 return eslOK;
1179 }
1180
1181 /*****************************************************************
1182 * 4. CM_TR_HB_MX data structure functions,
1183 * matrix of float scores for HMM banded CM alignment/search
1184 * using Kolbe and Eddy's 'truncated' DP CYK/Inside algorithms
1185 *****************************************************************/
1186
1187 /* Function: cm_tr_hb_mx_Create()
1188 * Incept: EPN, Thu Aug 25 14:15:37 2011
1189 *
1190 * Purpose: Allocate a reusable, resizeable <CM_TR_HB_MX> for a CM
1191 * given a CP9Bands_t object that defines the bands.
1192 *
1193 * We've set this up so it should be easy to allocate
1194 * aligned memory, though we're not doing this yet.
1195 *
1196 * Returns: a pointer to the new <CM_TR_HB_MX>.
1197 *
1198 * Throws: <NULL> on allocation error.
1199 */
1200 CM_TR_HB_MX *
1201 cm_tr_hb_mx_Create(CM_t *cm)
1202 {
1203 int status;
1204 CM_TR_HB_MX *mx = NULL;
1205 int v, b;
1206 int allocL = 1;
1207 int allocW = 1;
1208 int B = CMCountNodetype(cm, BIF_nd);
1209 int M = cm->M;
1210
1211 /* level 1: the structure itself */
1212 ESL_ALLOC(mx, sizeof(CM_TR_HB_MX));
1213 mx->Jdp = NULL;
1214 mx->Jdp_mem = NULL;
1215 mx->Ldp = NULL;
1216 mx->Ldp_mem = NULL;
1217 mx->Rdp = NULL;
1218 mx->Rdp_mem = NULL;
1219 mx->Tdp = NULL;
1220 mx->Tdp_mem = NULL;
1221 mx->cp9b = NULL;
1222
1223 /* level 2: deck (state) pointers, 0.1..M, go all the way to M
1224 * remember deck M is special, as it has no bands, we allocate
1225 * it only if nec (if local ends are on) in cm_tr_hb_mx_GrowTo()
1226 */
1227 ESL_ALLOC(mx->Jdp, sizeof(float **) * (int64_t) (M+1));
1228 ESL_ALLOC(mx->Ldp, sizeof(float **) * (int64_t) (M+1));
1229 ESL_ALLOC(mx->Rdp, sizeof(float **) * (int64_t) (M+1));
1230 ESL_ALLOC(mx->Tdp, sizeof(float **) * (int64_t) (M+1)); /* ptrs to non-ROOT_S and non-B states will be NULL */
1231
1232 /* level 3: dp cell memory, when creating only allocate 1 cell per state, for j = 0, d = 0 */
1233 ESL_ALLOC(mx->Jdp_mem, sizeof(float) * (int64_t) (M+1) * (int64_t) allocL * (int64_t) allocW);
1234 ESL_ALLOC(mx->Ldp_mem, sizeof(float) * (int64_t) (M+1) * (int64_t) allocL * (int64_t) allocW);
1235 ESL_ALLOC(mx->Rdp_mem, sizeof(float) * (int64_t) (M+1) * (int64_t) allocL * (int64_t) allocW);
1236 ESL_ALLOC(mx->Tdp_mem, sizeof(float) * (int64_t) (B+1) * (int64_t) allocL * (int64_t) allocW); /* +1 is for the special ROOT_S deck */
1237 ESL_ALLOC(mx->JnrowsA, sizeof(int) * (int64_t) (M+1));
1238 ESL_ALLOC(mx->LnrowsA, sizeof(int) * (int64_t) (M+1));
1239 ESL_ALLOC(mx->RnrowsA, sizeof(int) * (int64_t) (M+1));
1240 ESL_ALLOC(mx->TnrowsA, sizeof(int) * (int64_t) (M+1));
1241
1242 b = 0;
1243 for (v = 0; v < M; v++) {
1244 ESL_ALLOC(mx->Jdp[v], sizeof(float *) * (int64_t) allocL);
1245 ESL_ALLOC(mx->Ldp[v], sizeof(float *) * (int64_t) allocL);
1246 ESL_ALLOC(mx->Rdp[v], sizeof(float *) * (int64_t) allocL);
1247 mx->Jdp[v][0] = mx->Jdp_mem + (int64_t) v * (int64_t) allocL * (int64_t) allocW;
1248 mx->Ldp[v][0] = mx->Ldp_mem + (int64_t) v * (int64_t) allocL * (int64_t) allocW;
1249 mx->Rdp[v][0] = mx->Rdp_mem + (int64_t) v * (int64_t) allocL * (int64_t) allocW;
1250
1251 if(cm->sttype[v] == B_st || v == 0) { /* only B states and ROOT_S are valid */
1252 ESL_ALLOC(mx->Tdp[v], sizeof(float *) * (allocL));
1253 mx->Tdp[v][0] = mx->Tdp_mem + (int64_t) b * (int64_t) allocL * (int64_t) allocW;
1254 b++;
1255 mx->TnrowsA[v] = allocL;
1256 }
1257 else {
1258 mx->Tdp[v] = NULL;
1259 mx->TnrowsA[v] = 0;
1260 }
1261 mx->JnrowsA[v] = allocL;
1262 mx->LnrowsA[v] = allocL;
1263 mx->RnrowsA[v] = allocL;
1264 }
1265 /* allocate EL deck, for J, L, and R */
1266 ESL_ALLOC(mx->Jdp[M], sizeof(float *) * (int64_t) allocL);
1267 mx->Jdp[M][0] = mx->Jdp_mem + (int64_t) M * (int64_t) allocL * (int64_t) allocW;
1268 mx->JnrowsA[M] = allocL;
1269
1270 ESL_ALLOC(mx->Ldp[M], sizeof(float *) * (int64_t) allocL);
1271 mx->Ldp[M][0] = mx->Ldp_mem + (int64_t) M * (int64_t) allocL * (int64_t) allocW;
1272 mx->LnrowsA[M] = allocL;
1273
1274 ESL_ALLOC(mx->Rdp[M], sizeof(float *) * (int64_t) allocL);
1275 mx->Rdp[M][0] = mx->Rdp_mem + (int64_t) M * (int64_t) allocL * (int64_t) allocW;
1276 mx->RnrowsA[M] = allocL;
1277
1278 mx->Tdp[M] = NULL;
1279
1280 mx->TnrowsA[M] = 0;
1281
1282 mx->M = M;
1283 mx->B = B;
1284 mx->Jncells_alloc = (int64_t) (M+1) * (int64_t) allocL * (int64_t) allocW;
1285 mx->Lncells_alloc = (int64_t) (M+1) * (int64_t) allocL * (int64_t) allocW;
1286 mx->Rncells_alloc = (int64_t) (M+1) * (int64_t) allocL * (int64_t) allocW;
1287 mx->Tncells_alloc = (int64_t) (B+1) * (int64_t) allocL * (int64_t) allocW;
1288 mx->Jncells_valid = 0;
1289 mx->Lncells_valid = 0;
1290 mx->Rncells_valid = 0;
1291 mx->Tncells_valid = 0;
1292 mx->L = allocL; /* allocL = 1 */
1293
1294
1295 /* calculate size, these are in order of when they were allocated */
1296 mx->size_Mb = (float)
1297 (sizeof(CM_TR_HB_MX) +
1298 (4 * (mx->M+1) * sizeof(float **)) + /* mx->{J,L,R}dp[] ptrs */
1299 mx->Jncells_alloc * sizeof(float) + /* mx->Jdp_mem */
1300 mx->Lncells_alloc * sizeof(float) + /* mx->Ldp_mem */
1301 mx->Rncells_alloc * sizeof(float) + /* mx->Rdp_mem */
1302 mx->Tncells_alloc * sizeof(float) + /* mx->Tdp_mem */
1303 (4 * (mx->M+1) * sizeof(int)) + /* mx->{J,L,R,T}nrowsA ptrs */
1304 (3 * (mx->M+1) * allocL * sizeof(float *)) + /* mx->{J,L,R}dp[v][] ptrs */
1305 (1 * (mx->B+1) * allocL * sizeof(float *))); /* mx->Tdp[v][] ptrs */
1306 mx->size_Mb *= 0.000001; /* convert to Mb */
1307
1308 return mx;
1309
1310 ERROR:
1311 if (mx != NULL) cm_tr_hb_mx_Destroy(mx);
1312 return NULL;
1313 }
1314
1315 /* Function: cm_tr_hb_mx_GrowTo()
1316 * Incept: EPN, Thu Aug 25 14:39:00 2011
1317 *
1318 * Purpose: Assures that a CM_TR_HB_MX matrix <mx> is allocated
1319 * for a model of exactly <mx->M> states and required number of
1320 * total cells. Determines new required size from
1321 * the CP9Bands_t object passed in, and reallocates if
1322 * necessary.
1323 *
1324 * If local ends are on (cm->flags & CMH_LOCAL_END), allocates
1325 * a full non-banded EL deck.
1326 *
1327 * Checks to make sure desired matrix isn't too big (see throws).
1328 *
1329 * Args: cm - the CM the matrix is for
1330 * mx - the matrix to grow
1331 * errbuf - char buffer for reporting errors
1332 * cp9b - the bands for the current target sequence
1333 * L - the length of the current target sequence we're aligning
1334 * size_limit- max number of Mb for DP matrix, if matrix is bigger -> return eslERANGE
1335 *
1336 * Returns: <eslOK> on success, and <mx> may be reallocated upon
1337 * return; any data that may have been in <mx> must be
1338 * assumed to be invalidated.
1339 *
1340 * Throws: <eslERANGE> if required size to grow to exceeds <size_limit>.
1341 * This should be caught and appropriately handled by caller.
1342 * <eslEINCOMPAT> on contract violation
1343 * <eslEMEM> on memory allocation error.
1344 */
1345 int
1346 cm_tr_hb_mx_GrowTo(CM_t *cm, CM_TR_HB_MX *mx, char *errbuf, CP9Bands_t *cp9b, int L, float size_limit)
1347 {
1348 int status;
1349 void *p;
1350 int v, jp;
1351 int64_t Jcur_size = 0;
1352 int64_t Lcur_size = 0;
1353 int64_t Rcur_size = 0;
1354 int64_t Tcur_size = 0;
1355 int64_t Jncells;
1356 int64_t Lncells;
1357 int64_t Rncells;
1358 int64_t Tncells;
1359 int jbw;
1360 float Mb_needed; /* required size of matrix, given the bands */
1361 float Mb_alloc; /* allocated size of matrix, >= Mb_needed */
1362 int have_el;
1363 int realloced_J; /* did we reallocate mx->Jdp_mem? */
1364 int realloced_L; /* did we reallocate mx->Ldp_mem? */
1365 int realloced_R; /* did we reallocate mx->Rdp_mem? */
1366 int realloced_T; /* did we reallocate mx->Tdp_mem? */
1367
1368 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
1369
1370 /* contract check, number of states (M) is something we don't change
1371 * so check this matrix has same number of 1st dim state ptrs that
1372 * cp9b has */
1373 if(cp9b == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_hb_mx_GrowTo() entered with cp9b == NULL.\n");
1374 if(cp9b->cm_M != mx->M) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_hb_mx_GrowTo() entered with mx->M: (%d) != cp9b->M (%d)\n", mx->M, cp9b->cm_M);
1375
1376 if((status = cm_tr_hb_mx_SizeNeeded(cm, errbuf, cp9b, L, &Jncells, &Lncells, &Rncells, &Tncells, &Mb_needed)) != eslOK) return status;
1377 /*printf("HMM banded Tr matrix requested size: %.2f Mb\n", Mb_needed);*/
1378 ESL_DPRINTF2(("#DEBUG: HMM banded Tr matrix requested size: %.2f Mb\n", Mb_needed));
1379 if(Mb_needed > size_limit) ESL_FAIL(eslERANGE, errbuf, "requested HMM banded Tr DP mx of %.2f Mb > %.2f Mb limit.\nUse --mxsize, --maxtau or --tau.", Mb_needed, (float) size_limit);
1380
1381 /* check if we should free the matrix */
1382 if((mx->size_Mb > (0.5 * size_limit)) && /* matrix is >= 0.5 * size of our limit (based on bands from previous sequence) */
1383 (mx->size_Mb > (1.25 * Mb_needed))) { /* matrix is at least 25% bigger than we need to process current sequence */
1384 free(mx->Jdp_mem);
1385 free(mx->Ldp_mem);
1386 free(mx->Rdp_mem);
1387 free(mx->Tdp_mem);
1388 mx->Jdp_mem = NULL;
1389 mx->Ldp_mem = NULL;
1390 mx->Rdp_mem = NULL;
1391 mx->Tdp_mem = NULL;
1392 mx->Jncells_alloc = 0;
1393 mx->Lncells_alloc = 0;
1394 mx->Rncells_alloc = 0;
1395 mx->Tncells_alloc = 0;
1396 }
1397
1398 /* must we realloc the full {J,L,R,T}matrices? or can we get away with just
1399 * jiggering the pointers, if total required num cells is less
1400 * than or equal to what we already have alloc'ed?
1401 */
1402 realloced_J = realloced_L = realloced_R = realloced_T = FALSE;
1403 if (Jncells > mx->Jncells_alloc) {
1404 ESL_RALLOC(mx->Jdp_mem, p, sizeof(float) * Jncells);
1405 mx->Jncells_alloc = Jncells;
1406 realloced_J = TRUE;
1407 }
1408 if (Lncells > mx->Lncells_alloc) {
1409 ESL_RALLOC(mx->Ldp_mem, p, sizeof(float) * Lncells);
1410 mx->Lncells_alloc = Lncells;
1411 realloced_L = TRUE;
1412 }
1413 if (Rncells > mx->Rncells_alloc) {
1414 ESL_RALLOC(mx->Rdp_mem, p, sizeof(float) * Rncells);
1415 mx->Rncells_alloc = Rncells;
1416 realloced_R = TRUE;
1417 }
1418 if (Tncells > mx->Tncells_alloc) {
1419 ESL_RALLOC(mx->Tdp_mem, p, sizeof(float) * Tncells);
1420 mx->Tncells_alloc = Tncells;
1421 realloced_T = TRUE;
1422 }
1423
1424 /* Determine the size of our matrix based on the size it needed to be (Mb_needed).
1425 * This is tricky, for each matrix not reallocated, we have to adjust Mb_needed
1426 * so it uses previously allocated size of that matrix.
1427 */
1428 Mb_alloc = Mb_needed * 1000000; /* convert to bytes */
1429 if(! realloced_J) {
1430 Mb_alloc -= (float) (sizeof(float) * Jncells);
1431 Mb_alloc += (float) (sizeof(float) * mx->Jncells_alloc);
1432 }
1433 if(! realloced_L) {
1434 Mb_alloc -= (float) (sizeof(float) * Lncells);
1435 Mb_alloc += (float) (sizeof(float) * mx->Lncells_alloc);
1436 }
1437 if(! realloced_R) {
1438 Mb_alloc -= (float) (sizeof(float) * Rncells);
1439 Mb_alloc += (float) (sizeof(float) * mx->Rncells_alloc);
1440 }
1441 if(! realloced_T) {
1442 Mb_alloc -= (float) (sizeof(float) * Tncells);
1443 Mb_alloc += (float) (sizeof(float) * mx->Tncells_alloc);
1444 }
1445 /* note if we didn't reallocate any of the four matrices, Mb_alloc == Mb_needed */
1446 Mb_alloc *= 0.000001; /* convert to Mb */
1447
1448 mx->Jncells_valid = Jncells;
1449 mx->Lncells_valid = Lncells;
1450 mx->Rncells_valid = Rncells;
1451 mx->Tncells_valid = Tncells;
1452
1453 /* make sure each row is big enough */
1454 for(v = 0; v < mx->M; v++) {
1455 jbw = cp9b->jmax[v] - cp9b->jmin[v] + 1;
1456
1457 if(cp9b->Jvalid[v]) {
1458 if(jbw > mx->JnrowsA[v]) {
1459 if(mx->Jdp[v] != NULL) ESL_RALLOC(mx->Jdp[v], p, sizeof(float *) * jbw);
1460 else ESL_ALLOC (mx->Jdp[v], sizeof(float *) * jbw);
1461 mx->JnrowsA[v] = jbw;
1462 }
1463 }
1464 else { /* cp9b->Jvalid[v] is FALSE */
1465 if(mx->Jdp[v] != NULL) free(mx->Jdp[v]);
1466 mx->Jdp[v] = NULL;
1467 mx->JnrowsA[v] = 0;
1468 }
1469
1470 if(cp9b->Lvalid[v]) {
1471 if(jbw > mx->LnrowsA[v]) {
1472 if(mx->Ldp[v] != NULL) ESL_RALLOC(mx->Ldp[v], p, sizeof(float *) * jbw);
1473 else ESL_ALLOC (mx->Ldp[v], sizeof(float *) * jbw);
1474 mx->LnrowsA[v] = jbw;
1475 }
1476 }
1477 else { /* cp9b->Lvalid[v] is FALSE */
1478 if(mx->Ldp[v] != NULL) free(mx->Ldp[v]);
1479 mx->Ldp[v] = NULL;
1480 mx->LnrowsA[v] = 0;
1481 }
1482
1483 if(cp9b->Rvalid[v]) {
1484 if(jbw > mx->RnrowsA[v]) {
1485 if(mx->Rdp[v] != NULL) ESL_RALLOC(mx->Rdp[v], p, sizeof(float *) * jbw);
1486 else ESL_ALLOC (mx->Rdp[v], sizeof(float *) * jbw);
1487 mx->RnrowsA[v] = jbw;
1488 }
1489 }
1490 else { /* cp9b->Rvalid[v] is FALSE */
1491 if(mx->Rdp[v] != NULL) free(mx->Rdp[v]);
1492 mx->Rdp[v] = NULL;
1493 mx->RnrowsA[v] = 0;
1494 }
1495
1496 if(cp9b->Tvalid[v]) {
1497 if(jbw > mx->TnrowsA[v]) {
1498 if(mx->Tdp[v] != NULL) ESL_RALLOC(mx->Tdp[v], p, sizeof(float *) * jbw);
1499 else ESL_ALLOC (mx->Tdp[v], sizeof(float *) * jbw);
1500 mx->TnrowsA[v] = jbw;
1501 }
1502 }
1503 else { /* cp9b->Tvalid[v] is FALSE */
1504 if(mx->Tdp[v] != NULL) free(mx->Tdp[v]);
1505 mx->Tdp[v] = NULL;
1506 mx->TnrowsA[v] = 0;
1507 }
1508 }
1509 if(have_el) {
1510 jbw = L+1;
1511 if(cp9b->Jvalid[mx->M]) {
1512 if(jbw > mx->JnrowsA[mx->M]) {
1513 if(mx->Jdp[mx->M] != NULL) ESL_RALLOC(mx->Jdp[mx->M], p, sizeof(float *) * jbw);
1514 else ESL_ALLOC (mx->Jdp[mx->M], sizeof(float *) * jbw);
1515 mx->JnrowsA[mx->M] = jbw;
1516 }
1517 }
1518 else { /* cp9b->Jvalid[mx->M] is FALSE */
1519 if(mx->Jdp[v] != NULL) free(mx->Jdp[v]);
1520 mx->Jdp[v] = NULL;
1521 mx->JnrowsA[v] = 0;
1522 }
1523 if(cp9b->Lvalid[mx->M]) {
1524 if(jbw > mx->LnrowsA[mx->M]) {
1525 if(mx->Ldp[mx->M] != NULL) ESL_RALLOC(mx->Ldp[mx->M], p, sizeof(float *) * jbw);
1526 else ESL_ALLOC (mx->Ldp[mx->M], sizeof(float *) * jbw);
1527 mx->LnrowsA[mx->M] = jbw;
1528 }
1529 }
1530 else { /* cp9b->Lvalid[mx->M] is FALSE */
1531 if(mx->Ldp[v] != NULL) free(mx->Ldp[v]);
1532 mx->Ldp[v] = NULL;
1533 mx->LnrowsA[v] = 0;
1534 }
1535 if(cp9b->Rvalid[mx->M]) {
1536 if(jbw > mx->RnrowsA[mx->M]) {
1537 if(mx->Rdp[mx->M] != NULL) ESL_RALLOC(mx->Rdp[mx->M], p, sizeof(float *) * jbw);
1538 else ESL_ALLOC (mx->Rdp[mx->M], sizeof(float *) * jbw);
1539 mx->RnrowsA[mx->M] = jbw;
1540 }
1541 }
1542 else { /* cp9b->Rvalid[mx->M] is FALSE */
1543 if(mx->Rdp[v] != NULL) free(mx->Rdp[v]);
1544 mx->Rdp[v] = NULL;
1545 mx->RnrowsA[v] = 0;
1546 }
1547 }
1548 /* Tdp is NULL for cm->M */
1549
1550 /* reset the pointers, we keep a tally of cur_size as we go
1551 */
1552 Jcur_size = 0;
1553 Lcur_size = 0;
1554 Rcur_size = 0;
1555 Tcur_size = 0;
1556 for(v = 0; v < mx->M; v++) {
1557 if(mx->Jdp[v] != NULL) {
1558 for(jp = 0; jp <= (cp9b->jmax[v] - cp9b->jmin[v]); jp++) {
1559 mx->Jdp[v][jp] = mx->Jdp_mem + Jcur_size;
1560 Jcur_size += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
1561 }
1562 }
1563 if(mx->Ldp[v] != NULL) {
1564 for(jp = 0; jp <= (cp9b->jmax[v] - cp9b->jmin[v]); jp++) {
1565 mx->Ldp[v][jp] = mx->Ldp_mem + Lcur_size;
1566 Lcur_size += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
1567 }
1568 }
1569 if(mx->Rdp[v] != NULL) {
1570 for(jp = 0; jp <= (cp9b->jmax[v] - cp9b->jmin[v]); jp++) {
1571 mx->Rdp[v][jp] = mx->Rdp_mem + Rcur_size;
1572 Rcur_size += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
1573 }
1574 }
1575 if(mx->Tdp[v] != NULL) {
1576 for(jp = 0; jp <= (cp9b->jmax[v] - cp9b->jmin[v]); jp++) {
1577 mx->Tdp[v][jp] = mx->Tdp_mem + Tcur_size;
1578 Tcur_size += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
1579 }
1580 }
1581 }
1582 if(have_el) {
1583 if(mx->Jdp[mx->M] != NULL) {
1584 for(jp = 0; jp <= L; jp++) {
1585 mx->Jdp[mx->M][jp] = mx->Jdp_mem + Jcur_size;
1586 Jcur_size += jp + 1;
1587 }
1588 }
1589 if(mx->Ldp[mx->M] != NULL) {
1590 for(jp = 0; jp <= L; jp++) {
1591 mx->Ldp[mx->M][jp] = mx->Ldp_mem + Lcur_size;
1592 Lcur_size += jp + 1;
1593 }
1594 }
1595 if(mx->Rdp[mx->M] != NULL) {
1596 for(jp = 0; jp <= L; jp++) {
1597 mx->Rdp[mx->M][jp] = mx->Rdp_mem + Rcur_size;
1598 Rcur_size += jp + 1;
1599 }
1600 }
1601 }
1602
1603 /*printf("J ncells %10" PRId64 " %10" PRId64 "\n", Jcur_size, mx->Jncells_valid);
1604 printf("L ncells %10" PRId64 " %10" PRId64 "\n", Lcur_size, mx->Lncells_valid);
1605 printf("R ncells %10" PRId64 " %10" PRId64 "\n", Rcur_size, mx->Rncells_valid);
1606 printf("T ncells %10" PRId64 " %10" PRId64 "\n", Tcur_size, mx->Tncells_valid);*/
1607 assert(Jcur_size == mx->Jncells_valid);
1608 assert(Lcur_size == mx->Lncells_valid);
1609 assert(Rcur_size == mx->Rncells_valid);
1610 assert(Tcur_size == mx->Tncells_valid);
1611 ESL_DASSERT1((Jcur_size == mx->Jncells_valid));
1612 ESL_DASSERT1((Lcur_size == mx->Lncells_valid));
1613 ESL_DASSERT1((Rcur_size == mx->Rncells_valid));
1614 ESL_DASSERT1((Tcur_size == mx->Tncells_valid));
1615
1616 mx->cp9b = cp9b; /* just a reference */
1617
1618 /* now update L and size_Mb */
1619 mx->L = L; /* length of current seq we're valid for */
1620 mx->size_Mb = Mb_alloc;
1621
1622 return eslOK;
1623
1624 ERROR:
1625 return status;
1626 }
1627
1628 /* Function: cm_tr_hb_mx_Destroy()
1629 * Synopsis: Frees a DP matrix.
1630 * Incept: EPN, Thu Aug 25 14:51:15 2011
1631 *
1632 * Purpose: Frees a <CM_TR_HB_MX>.
1633 *
1634 * Returns: (void)
1635 */
1636 void
1637 cm_tr_hb_mx_Destroy(CM_TR_HB_MX *mx)
1638 {
1639 if (mx == NULL) return;
1640 int v;
1641
1642 if (mx->Jdp != NULL) {
1643 for (v = 0; v <= mx->M; v++)
1644 if(mx->Jdp[v] != NULL) free(mx->Jdp[v]);
1645 }
1646 free(mx->Jdp);
1647
1648 if (mx->Ldp != NULL) {
1649 for (v = 0; v <= mx->M; v++)
1650 if(mx->Ldp[v] != NULL) free(mx->Ldp[v]);
1651 }
1652 free(mx->Ldp);
1653
1654 if (mx->Rdp != NULL) {
1655 for (v = 0; v <= mx->M; v++)
1656 if(mx->Rdp[v] != NULL) free(mx->Rdp[v]);
1657 }
1658 free(mx->Rdp);
1659
1660 if (mx->Tdp != NULL) {
1661 for (v = 0; v <= mx->M; v++)
1662 if(mx->Tdp[v] != NULL) free(mx->Tdp[v]);
1663 }
1664 free(mx->Tdp);
1665
1666 if (mx->JnrowsA != NULL) free(mx->JnrowsA);
1667 if (mx->LnrowsA != NULL) free(mx->LnrowsA);
1668 if (mx->RnrowsA != NULL) free(mx->RnrowsA);
1669 if (mx->TnrowsA != NULL) free(mx->TnrowsA);
1670 if (mx->Jdp_mem != NULL) free(mx->Jdp_mem);
1671 if (mx->Ldp_mem != NULL) free(mx->Ldp_mem);
1672 if (mx->Rdp_mem != NULL) free(mx->Rdp_mem);
1673 if (mx->Tdp_mem != NULL) free(mx->Tdp_mem);
1674 free(mx);
1675 return;
1676 }
1677
1678 /* Function: cm_tr_hb_mx_Dump()
1679 * Synopsis: Dump a DP matrix to a stream, for diagnostics.
1680 * Incept: EPN, Thu Aug 25 14:52:40 2011
1681 *
1682 * Purpose: Dump matrix <mx> to stream <fp> for diagnostics.
1683 */
1684 int
1685 cm_tr_hb_mx_Dump(FILE *ofp, CM_TR_HB_MX *mx, char mode, int print_mx)
1686 {
1687 int status;
1688 int v, jp, j, dp, d;
1689 int fill_L, fill_R, fill_T; /* are the L, R, and T matrices valid? */
1690
1691 fprintf(ofp, "M: %d\n", mx->M);
1692 fprintf(ofp, "B: %d\n", mx->B);
1693 fprintf(ofp, "L: %d\n", mx->L);
1694 fprintf(ofp, "Jncells_alloc: %" PRId64 "\nJncells_valid: %" PRId64 "\n", mx->Jncells_alloc, mx->Jncells_valid);
1695 fprintf(ofp, "Lncells_alloc: %" PRId64 "\nLncells_valid: %" PRId64 "\n", mx->Lncells_alloc, mx->Lncells_valid);
1696 fprintf(ofp, "Rncells_alloc: %" PRId64 "\nRncells_valid: %" PRId64 "\n", mx->Rncells_alloc, mx->Rncells_valid);
1697 fprintf(ofp, "Tncells_alloc: %" PRId64 "\nTncells_valid: %" PRId64 "\n", mx->Tncells_alloc, mx->Tncells_valid);
1698 fprintf(ofp, "mode: %d\n", mode);
1699
1700 if((status = cm_TrFillFromMode(mode, &fill_L, &fill_R, &fill_T)) != eslOK) return status;
1701
1702 /* DP matrix data */
1703 if(print_mx) {
1704 for (v = 0; v < mx->M; v++) {
1705 for(jp = 0; jp <= mx->cp9b->jmax[v] - mx->cp9b->jmin[v]; jp++) {
1706 j = jp + mx->cp9b->jmin[v];
1707 for(dp = 0; dp <= mx->cp9b->hdmax[v][jp] - mx->cp9b->hdmin[v][jp]; dp++) {
1708 d = dp + mx->cp9b->hdmin[v][jp];
1709 if(mx->Jdp[v]) fprintf(ofp, "Jdp[v:%5d][j:%5d][d:%5d] %8.4f\n", v, j, d, mx->Jdp[v][jp][dp]);
1710 if(fill_L && mx->Ldp[v]) fprintf(ofp, "Ldp[v:%5d][j:%5d][d:%5d] %8.4f\n", v, j, d, mx->Ldp[v][jp][dp]);
1711 if(fill_R && mx->Rdp[v]) fprintf(ofp, "Rdp[v:%5d][j:%5d][d:%5d] %8.4f\n", v, j, d, mx->Rdp[v][jp][dp]);
1712 if(fill_T && mx->Tdp[v]) fprintf(ofp, "Tdp[v:%5d][j:%5d][d:%5d] %8.4f\n", v, j, d, mx->Tdp[v][jp][dp]);
1713 }
1714 fprintf(ofp, "\n");
1715 }
1716 fprintf(ofp, "\n\n");
1717 }
1718 /* print EL deck, if it's valid */
1719 v = mx->M;
1720 for(j = 0; j <= mx->L; j++) {
1721 for(d = 0; d <= j; d++) {
1722 if(mx->Jdp[v] && mx->JnrowsA[v] == (mx->L+1)) fprintf(ofp, "Jdp[v:%5d][j:%5d][d:%5d] %8.4f\n", v, j, d, mx->Jdp[v][j][d]);
1723 if(fill_L && mx->Ldp[v] && mx->LnrowsA[v] == (mx->L+1)) fprintf(ofp, "Ldp[v:%5d][j:%5d][d:%5d] %8.4f\n", v, j, d, mx->Ldp[v][j][d]);
1724 if(fill_R && mx->Rdp[v] && mx->RnrowsA[v] == (mx->L+1)) fprintf(ofp, "Rdp[v:%5d][j:%5d][d:%5d] %8.4f\n", v, j, d, mx->Rdp[v][j][d]);
1725 }
1726 fprintf(ofp, "\n");
1727 }
1728 fprintf(ofp, "\n\n");
1729 }
1730 return eslOK;
1731 }
1732
1733 /* Function: cm_tr_hb_mx_SizeNeeded()
1734 * Incept: EPN, Thu Aug 25 14:55:25 2011
1735 *
1736 * Purpose: Given a model and CP9_bands_t object with
1737 * pre-calced bands for a target, determine the number
1738 * of cells and total size in Mb required in a CM_HB_MX
1739 * for the target given the bands.
1740 *
1741 * Return number of cells required given the bands
1742 * in <cp9b> in <ret_ncells> and size of required
1743 * matrix in Mb in <ret_Mb>.
1744 *
1745 * Args: cm - the CM the matrix is for
1746 * errbuf - char buffer for reporting errors
1747 * cp9b - the bands for the current target sequence
1748 * L - the length of the current target sequence we're aligning
1749 * ret_Jncells - RETURN: number of J matrix cells required
1750 * ret_Lncells - RETURN: number of L matrix cells required
1751 * ret_Rncells - RETURN: number of R matrix cells required
1752 * ret_Tncells - RETURN: number of T matrix cells required
1753 * ret_Mb - RETURN: required size of matrix in Mb
1754 *
1755 * Returns: <eslOK> on success
1756 *
1757 */
1758 int
1759 cm_tr_hb_mx_SizeNeeded(CM_t *cm, char *errbuf, CP9Bands_t *cp9b, int L, int64_t *ret_Jncells, int64_t *ret_Lncells, int64_t *ret_Rncells, int64_t *ret_Tncells, float *ret_Mb)
1760 {
1761 int v, jp;
1762 int64_t Jncells, Lncells, Rncells, Tncells;
1763 int jbw;
1764 int have_el;
1765 float Mb_needed;
1766 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
1767
1768 /* contract check */
1769 if(cp9b == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_tr_hb_mx_SizeNeeded() entered with cp9b == NULL.\n");
1770
1771 Jncells = 0;
1772 Lncells = 0;
1773 Rncells = 0;
1774 Tncells = 0;
1775 Mb_needed = (float)
1776 (sizeof(CM_TR_HB_MX) +
1777 (4 * (cp9b->cm_M+1) * sizeof(float **)) + /* mx->{J,L,R}dp[] ptrs */
1778 (4 * (cp9b->cm_M+1) * sizeof(int))); /* mx->{J,L,R,T}nrowsA ptrs */
1779
1780 for(v = 0; v < cp9b->cm_M; v++) {
1781 jbw = cp9b->jmax[v] - cp9b->jmin[v];
1782 if(cp9b->Jvalid[v]) {
1783 Mb_needed += (float) (sizeof(float *) * (jbw+1)); /* mx->Jdp[v][] ptrs */
1784 for(jp = 0; jp <= jbw; jp++) {
1785 Jncells += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
1786 }
1787 }
1788 if(cp9b->Lvalid[v]) {
1789 Mb_needed += (float) (sizeof(float *) * (jbw+1)); /* mx->Ldp[v][] ptrs */
1790 for(jp = 0; jp <= jbw; jp++) {
1791 Lncells += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
1792 }
1793 }
1794 if(cp9b->Rvalid[v]) {
1795 Mb_needed += (float) (sizeof(float *) * (jbw+1)); /* mx->Rdp[v][] ptrs */
1796 for(jp = 0; jp <= jbw; jp++) {
1797 Rncells += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
1798 }
1799 }
1800 if(cp9b->Tvalid[v]) {
1801 Mb_needed += (float) (sizeof(float *) * (jbw+1)); /* mx->Tdp[v][] ptrs */
1802 for(jp = 0; jp <= jbw; jp++) {
1803 Tncells += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
1804 }
1805 }
1806 }
1807 if(have_el) { /* space for EL deck */
1808 if(cp9b->Jvalid[cp9b->cm_M]) {
1809 Mb_needed += (float) (sizeof(float *) * (L+1)); /* mx->Jdp[cm->M][] ptrs */
1810 Jncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
1811 }
1812 if(cp9b->Lvalid[cp9b->cm_M]) {
1813 Mb_needed += (float) (sizeof(float *) * (L+1)); /* mx->Ldp[cm->M][] ptrs */
1814 Lncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
1815 }
1816 if(cp9b->Rvalid[cp9b->cm_M]) {
1817 Mb_needed += (float) (sizeof(float *) * (L+1)); /* mx->Ldp[cm->M][] ptrs */
1818 Rncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
1819 }
1820 }
1821
1822 Mb_needed += sizeof(float) * Jncells; /* mx->Jdp_mem */
1823 Mb_needed += sizeof(float) * Lncells; /* mx->Ldp_mem */
1824 Mb_needed += sizeof(float) * Rncells; /* mx->Rdp_mem */
1825 Mb_needed += sizeof(float) * Tncells; /* mx->Tdp_mem */
1826 Mb_needed *= 0.000001; /* convert to megabytes */
1827
1828 if(ret_Jncells != NULL) *ret_Jncells = Jncells;
1829 if(ret_Lncells != NULL) *ret_Lncells = Lncells;
1830 if(ret_Rncells != NULL) *ret_Rncells = Rncells;
1831 if(ret_Tncells != NULL) *ret_Tncells = Tncells;
1832 if(ret_Mb != NULL) *ret_Mb = Mb_needed;
1833
1834 /*printf("in cm_tr_hb_mx_SizeNeeded() returning %.2f total Mb (EL: %.2f Mb)\n",
1835 Mb_needed, (sizeof(float) * (L+2) * (L+1) * 0.5) * 0.000001); */
1836
1837 return eslOK;
1838 }
1839
1840 /*****************************************************************
1841 * 5. CM_SHADOW_MX data structure functions,
1842 * non-banded shadow matrix for tracing back CM parses.
1843 *****************************************************************/
1844
1845 /* Function: cm_shadow_mx_Create()
1846 * Incept: EPN, Wed Sep 14 04:46:22 2011
1847 *
1848 * Purpose: Allocate a reusable, resizeable <CM_SHADOW_MX> for a CM <cm>
1849 * The CM is needed so we know which decks need to be int's (BIF_B states)
1850 * and which need to be char's (all other states).
1851 *
1852 * We've set this up so it should be easy to allocate
1853 * aligned memory, though we're not doing this yet.
1854 *
1855 * Returns: a pointer to the new <CM_SHADOW_MX>.
1856 *
1857 * Throws: <NULL> on allocation error.
1858 */
1859 CM_SHADOW_MX *
1860 cm_shadow_mx_Create(CM_t *cm)
1861 {
1862 int status;
1863 CM_SHADOW_MX *mx = NULL;
1864 int v, b;
1865 int M = cm->M;
1866 int allocL = 1;
1867 int allocW = 1;
1868 int B = CMCountNodetype(cm, BIF_nd);
1869
1870 /* level 1: the structure itself */
1871 ESL_ALLOC(mx, sizeof(CM_SHADOW_MX));
1872 mx->yshadow = NULL;
1873 mx->yshadow_mem = NULL;
1874
1875 mx->kshadow = NULL;
1876 mx->kshadow_mem = NULL;
1877
1878 /* level 2: deck (state) pointers, 0.1..M-1, M (EL deck) is irrelevant for the
1879 * shadow matrix.
1880 */
1881 ESL_ALLOC(mx->yshadow, sizeof(char **) * M);
1882 ESL_ALLOC(mx->kshadow, sizeof(int **) * M);
1883
1884 /* level 3: matrix cell memory, when creating only allocate 1 cell per state, for j = 0, d = 0 */
1885 ESL_ALLOC(mx->yshadow_mem, sizeof(char) * (int64_t) (M-B) * (int64_t) allocL * (int64_t) allocW);
1886 ESL_ALLOC(mx->kshadow_mem, sizeof(int) * ESL_MAX(1, B * allocL * allocW)); // avoid 0 malloc
1887
1888 b = 0;
1889 for (v = 0; v < M; v++) {
1890 if(cm->sttype[v] == B_st) {
1891 ESL_ALLOC(mx->kshadow[v], sizeof(int *) * (int64_t) allocL);
1892 mx->kshadow[v][0] = mx->kshadow_mem + (int64_t) b * (int64_t) allocL * (int64_t) allocW;
1893 mx->yshadow[v] = NULL;
1894 b++;
1895 }
1896 else {
1897 ESL_ALLOC(mx->yshadow[v], sizeof(char *) * (int64_t) allocL);
1898 mx->yshadow[v][0] = mx->yshadow_mem + (int64_t) (v-b) * (int64_t) allocL * (int64_t) allocW;
1899 mx->kshadow[v] = NULL;
1900 }
1901 }
1902 mx->M = M;
1903 mx->B = B;
1904 mx->y_ncells_alloc = (int64_t) (M-B) * (int64_t) allocL * (int64_t) allocW;
1905 mx->y_ncells_valid = 0;
1906 mx->k_ncells_alloc = (int64_t) (B) * (int64_t) allocL * (int64_t) allocW;
1907 mx->k_ncells_valid = 0;
1908 mx->L = allocL; /* allocL = 1 */
1909
1910 /* calculate size, these are in order of when they were allocated */
1911 mx->size_Mb = (float)
1912 (sizeof(CM_SHADOW_MX) +
1913 ((mx->M) * sizeof(char **)) + /* mx->yshadow[] ptrs */
1914 ((mx->M) * sizeof(int **)) + /* mx->kshadow[] ptrs */
1915 mx->y_ncells_alloc * sizeof(char) + /* mx->yshadow_mem */
1916 mx->k_ncells_alloc * sizeof(int) + /* mx->kshadow_mem */
1917 (int64_t) ((int64_t) mx->B * (int64_t) allocL * sizeof(int *)) + /* mx->kshadow[v][] ptrs */
1918 (int64_t) ((int64_t) (mx->M - mx->B) * (int64_t) allocL * sizeof(char *))); /* mx->yshadow[v][] ptrs */
1919 mx->size_Mb *= 0.000001; /* convert to Mb */
1920
1921 return mx;
1922
1923 ERROR:
1924 if (mx != NULL) cm_shadow_mx_Destroy(mx);
1925 return NULL;
1926 }
1927
1928 /* Function: cm_shadow_mx_GrowTo()
1929 * Incept: EPN, Wed Sep 14 04:49:55 2011
1930 *
1931 * Purpose: Assures that a CM_SHADOW_MX <mx> is allocated
1932 * for a model of exactly <mx->M> states and a sequence
1933 * of length L, reallocating as necessary.
1934 *
1935 * Checks that the matrix has been created for the current CM.
1936 * Check is that mx->yshadow[v] == NULL when v is a B_st and
1937 * mx->kshadow[v] != NULL when v is a B_st.
1938 *
1939 * Checks to make sure desired matrix isn't too big (see throws).
1940 *
1941 * Args: cm - the CM the matrix is for
1942 * mx - the matrix to grow
1943 * errbuf - char buffer for reporting errors
1944 * L - the length of the current target sequence we're aligning
1945 * size_limit- max number of Mb for DP matrix, if matrix is bigger -> return eslERANGE
1946 *
1947 * Returns: <eslOK> on success, and <mx> may be reallocated upon
1948 * return; any data that may have been in <mx> must be
1949 * assumed to be invalidated.
1950 *
1951 * Throws: <eslERANGE> if required size to grow to exceeds <size_limit>.
1952 * This should be caught and appropriately handled by caller.
1953 * <eslEINCOMPAT> if mx does not appeared to be created for this cm
1954 * <eslEMEM> on memory allocation error.
1955 */
1956 int
1957 cm_shadow_mx_GrowTo(CM_t *cm, CM_SHADOW_MX *mx, char *errbuf, int L, float size_limit)
1958 {
1959 int status;
1960 void *p;
1961 int v, jp;
1962 int64_t y_cur_size;
1963 int64_t k_cur_size;
1964 int64_t y_ncells;
1965 int64_t k_ncells;
1966 float Mb_needed; /* required size of matrix, given the bands */
1967 float Mb_alloc; /* allocated size of matrix, >= Mb_needed */
1968 int realloced_y; /* did we reallocate mx->yshadow_mem? */
1969 int realloced_k; /* did we reallocate mx->kshadow_mem? */
1970
1971 if((status = cm_shadow_mx_SizeNeeded(cm, errbuf, L, &y_ncells, &k_ncells, &Mb_needed)) != eslOK) return status;
1972 /*printf("Non-banded shadow matrix requested size: %.2f Mb\n", Mb_needed);*/
1973 ESL_DPRINTF2(("#DEBUG: Non-banded shadow matrix requested size: %.2f Mb\n", Mb_needed));
1974 if(Mb_needed > size_limit) ESL_FAIL(eslERANGE, errbuf, "requested non-banded shadow DP mx of %.2f Mb > %.2f Mb limit.\nUse --mxsize, --maxtau or --tau.", Mb_needed, (float) size_limit);
1975
1976 /* must we realloc the full yshadow and kshadow matrices?
1977 * or can we get away with just jiggering the pointers, if
1978 * total required num cells is less than or equal to what
1979 * we already have alloc'ed?
1980 */
1981 realloced_y = FALSE;
1982 realloced_k = FALSE;
1983 if (y_ncells > mx->y_ncells_alloc) {
1984 ESL_RALLOC(mx->yshadow_mem, p, sizeof(char) * y_ncells);
1985 mx->y_ncells_alloc = y_ncells;
1986 realloced_y = TRUE;
1987 }
1988 if (k_ncells > mx->k_ncells_alloc) {
1989 ESL_RALLOC(mx->kshadow_mem, p, sizeof(int) * k_ncells);
1990 mx->k_ncells_alloc = k_ncells;
1991 realloced_k = TRUE;
1992 }
1993 /* Determine the size of our matrix based on the size it needed to be (Mb_needed).
1994 * This is tricky, for each matrix not reallocated, we have to adjust Mb_needed
1995 * so it uses previously allocated size of that matrix.
1996 */
1997 Mb_alloc = Mb_needed * 1000000; /* convert to bytes */
1998 if(! realloced_y) {
1999 Mb_alloc -= (float) (sizeof(char) * y_ncells);
2000 Mb_alloc += (float) (sizeof(char) * mx->y_ncells_alloc);
2001 }
2002 if(! realloced_k) {
2003 Mb_alloc -= (float) (sizeof(int) * k_ncells);
2004 Mb_alloc += (float) (sizeof(int) * mx->k_ncells_alloc);
2005 }
2006 /* note if we didn't reallocate any of the four matrices, Mb_alloc == Mb_needed */
2007 Mb_alloc *= 0.000001; /* convert to Mb */
2008
2009 mx->y_ncells_valid = y_ncells;
2010 mx->k_ncells_valid = k_ncells;
2011
2012 /* reallocate the yshadow,kshadow[v] ptrs */
2013 for(v = 0; v < mx->M; v++) {
2014 if(cm->sttype[v] != B_st) {
2015 ESL_RALLOC(mx->yshadow[v], p, sizeof(char *) * (L+1));
2016 }
2017 else {
2018 ESL_RALLOC(mx->kshadow[v], p, sizeof(int *) * (L+1));
2019 }
2020 }
2021
2022 /* reset the pointers, we keep a tally of number of cells
2023 * we've seen in each matrix (y_cur_size and k_cur_size) as we go,
2024 * we could precalc it and store it for each v,j, but that
2025 * would be wasteful, as we'll only use the matrix configured
2026 * this way once, in a banded CYK run.
2027 */
2028 y_cur_size = 0;
2029 k_cur_size = 0;
2030 for(v = 0; v < mx->M; v++) {
2031 if(cm->sttype[v] != B_st) {
2032 for(jp = 0; jp <= L; jp++) {
2033 mx->yshadow[v][jp] = mx->yshadow_mem + y_cur_size;
2034 y_cur_size += jp+1;
2035 }
2036 }
2037 else {
2038 for(jp = 0; jp <= L; jp++) {
2039 mx->kshadow[v][jp] = mx->kshadow_mem + k_cur_size;
2040 k_cur_size += jp+1;
2041 }
2042 }
2043 }
2044 /*printf("y ncells %10" PRId64 " %10" PRId64 "\n", y_cur_size, mx->y_ncells_valid);
2045 printf("k ncells %10" PRId64 " %10" PRId64 "\n", k_cur_size, mx->k_ncells_valid);*/
2046 assert(y_cur_size == mx->y_ncells_valid);
2047 assert(k_cur_size == mx->k_ncells_valid);
2048 ESL_DASSERT1((y_cur_size == mx->y_ncells_valid));
2049
2050 /* now update L and size_Mb */
2051 mx->L = L; /* length of current seq we're valid for */
2052 mx->size_Mb = Mb_alloc;
2053
2054 return eslOK;
2055
2056 ERROR:
2057 return status;
2058 }
2059
2060 /* Function: cm_shadow_mx_Destroy()
2061 * Synopsis: Frees a DP matrix.
2062 * Incept: EPN, Wed Sep 14 04:53:30 2011
2063 *
2064 * Purpose: Frees a <CM_SHADOW_MX>.
2065 *
2066 * Returns: (void)
2067 */
2068 void
2069 cm_shadow_mx_Destroy(CM_SHADOW_MX *mx)
2070 {
2071 if (mx == NULL) return;
2072 int v;
2073
2074 if (mx->yshadow != NULL) {
2075 for (v = 0; v < mx->M; v++)
2076 if(mx->yshadow[v] != NULL) free(mx->yshadow[v]);
2077 }
2078 free(mx->yshadow);
2079
2080 if (mx->kshadow != NULL) {
2081 for (v = 0; v < mx->M; v++)
2082 if(mx->kshadow[v] != NULL) free(mx->kshadow[v]);
2083 }
2084 free(mx->kshadow);
2085
2086 if (mx->yshadow_mem != NULL) free(mx->yshadow_mem);
2087 if (mx->kshadow_mem != NULL) free(mx->kshadow_mem);
2088 free(mx);
2089 return;
2090 }
2091
2092 /* Function: cm_shadow_mx_Dump()
2093 * Synopsis: Dump a DP matrix to a stream, for diagnostics.
2094 * Incept: EPN, Sat Sep 10 12:21:53 2011
2095 *
2096 * Purpose: Dump matrix <mx> to stream <fp> for diagnostics.
2097 */
2098 int
2099 cm_shadow_mx_Dump(FILE *ofp, CM_t *cm, CM_SHADOW_MX *mx, int print_mx)
2100 {
2101 int v, j, d;
2102
2103 fprintf(ofp, "M: %d\n", mx->M);
2104 fprintf(ofp, "B: %d\n", mx->B);
2105 fprintf(ofp, "L: %d\n", mx->L);
2106 fprintf(ofp, "y_ncells_alloc: %" PRId64 "\ny_ncells_valid: %" PRId64 "\n", mx->y_ncells_alloc, mx->y_ncells_valid);
2107 fprintf(ofp, "k_ncells_alloc: %" PRId64 "\nk_ncells_valid: %" PRId64 "\n", mx->k_ncells_alloc, mx->k_ncells_valid);
2108
2109 /* yshadow/kshadow matrix data */
2110 if(print_mx) {
2111 for(v = 0; v < mx->M; v++) {
2112 if(cm->sttype[v] == B_st) {
2113 for(j = 0; j <= mx->L; j++) {
2114 for(d = 0; d <= j; d++) {
2115 if(mx->kshadow[v]) fprintf(ofp, "kshad[v:%5d][j:%5d][d:%5d] %8d\n", v, j, d, mx->kshadow[v][j][d]);
2116 }
2117 fprintf(ofp, "\n");
2118 }
2119 fprintf(ofp, "\n\n");
2120 }
2121 else { /* ! B_st */
2122 for(j = 0; j <= mx->L; j++) {
2123 for(d = 0; d <= j; d++) {
2124 if(mx->yshadow[v]) fprintf(ofp, "yshad[v:%5d][j:%5d][d:%5d] %8d\n", v, j, d, mx->yshadow[v][j][d]);
2125 }
2126 fprintf(ofp, "\n");
2127 }
2128 fprintf(ofp, "\n\n");
2129 }
2130 }
2131 }
2132 return eslOK;
2133 }
2134
2135 /* Function: cm_shadow_mx_SizeNeeded()
2136 * Incept: EPN, Wed Sep 14 04:55:23 2011
2137 *
2138 * Purpose: Given a model, and a sequence length L determine the
2139 * number of cells and total size in Mb required for the
2140 * matrix for the target given the bands.
2141 *
2142 * Return number of yshadow (char) cells required in
2143 * <ret_ny_cells> and number of kshadow (int) cells
2144 * required in <ret_nk_cells) and size of required
2145 * matrix in Mb in <ret_Mb>.
2146 *
2147 * Args: cm - the CM the matrix is for
2148 * errbuf - char buffer for reporting errors
2149 * L - length of sequence we will align
2150 * ret_ny_cells - RETURN: number of required char cells (yshadow)
2151 * ret_nk_cells - RETURN: number of required int cells (kshadow)
2152 * ret_Mb - RETURN: required size of matrix in Mb
2153 *
2154 * Returns: <eslOK> on success
2155 *
2156 */
2157 int
2158 cm_shadow_mx_SizeNeeded(CM_t *cm, char *errbuf, int L, int64_t *ret_ny_cells, int64_t *ret_nk_cells, float *ret_Mb)
2159 {
2160 int v;
2161 int64_t y_ncells;
2162 int64_t k_ncells;
2163 float Mb_needed;
2164
2165 y_ncells = 0;
2166 k_ncells = 0;
2167 Mb_needed = (float)
2168 (sizeof(CM_SHADOW_MX) +
2169 ((cm->M) * sizeof(char **)) + /* mx->yshadow[] ptrs */
2170 ((cm->M) * sizeof(int **))); /* mx->kshadow[] ptrs */
2171
2172 for(v = 0; v < cm->M; v++) {
2173 if(cm->sttype[v] == B_st) {
2174 Mb_needed += (float) (sizeof(int *) * (L+1)); /* mx->kshadow[v][] ptrs */
2175 k_ncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
2176 }
2177 else {
2178 Mb_needed += (float) (sizeof(char *) * (L+1)); /* mx->yshadow[v][] ptrs */
2179 y_ncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
2180 }
2181 }
2182
2183 Mb_needed += sizeof(int) * k_ncells; /* mx->kshadow_mem */
2184 Mb_needed += sizeof(char) * y_ncells; /* mx->yshadow_mem */
2185 Mb_needed *= 0.000001; /* convert to megabytes */
2186
2187 if(ret_ny_cells != NULL) *ret_ny_cells = y_ncells;
2188 if(ret_nk_cells != NULL) *ret_nk_cells = k_ncells;
2189 if(ret_Mb != NULL) *ret_Mb = Mb_needed;
2190 return eslOK;
2191 }
2192
2193 /*****************************************************************
2194 * 6. CM_TR_SHADOW_MX data structure functions,
2195 * non-banded shadow matrix for tracing back truncated CM parses
2196 *****************************************************************/
2197
2198 /* Function: cm_tr_shadow_mx_Create()
2199 * Incept: EPN, Sat Sep 10 12:10:42 2011
2200 *
2201 * Purpose: Allocate a reusable, resizeable <CM_TR_SHADOW_MX> for a CM <cm>
2202 * The CM is needed so we know which decks need to be int's (BIF_B states)
2203 * and which need to be char's (all other states).
2204 *
2205 * We've set this up so it should be easy to allocate
2206 * aligned memory, though we're not doing this yet.
2207 *
2208 * Returns: a pointer to the new <CM_TR_SHADOW_MX>.
2209 *
2210 * Throws: <NULL> on allocation error.
2211 */
2212 CM_TR_SHADOW_MX *
2213 cm_tr_shadow_mx_Create(CM_t *cm)
2214 {
2215 int status;
2216 CM_TR_SHADOW_MX *mx = NULL;
2217 int v, b;
2218 int M = cm->M;
2219 int allocL = 1;
2220 int allocW = 1;
2221 int B = CMCountNodetype(cm, BIF_nd);
2222
2223 /* level 1: the structure itself */
2224 ESL_ALLOC(mx, sizeof(CM_TR_SHADOW_MX));
2225 mx->Jyshadow = NULL;
2226 mx->Jyshadow_mem = NULL;
2227 mx->Lyshadow = NULL;
2228 mx->Lyshadow_mem = NULL;
2229 mx->Ryshadow = NULL;
2230 mx->Ryshadow_mem = NULL;
2231
2232 mx->Jkshadow = NULL;
2233 mx->Jkshadow_mem = NULL;
2234 mx->Lkshadow = NULL;
2235 mx->Lkshadow_mem = NULL;
2236 mx->Rkshadow = NULL;
2237 mx->Rkshadow_mem = NULL;
2238 mx->Tkshadow = NULL;
2239 mx->Tkshadow_mem = NULL;
2240
2241 mx->Lkmode = NULL;
2242 mx->Lkmode_mem = NULL;
2243 mx->Rkmode = NULL;
2244 mx->Rkmode_mem = NULL;
2245
2246 /* level 2: deck (state) pointers, 0.1..M-1, M (EL deck) is irrelevant for the
2247 * shadow matrix.
2248 */
2249 ESL_ALLOC(mx->Jyshadow, sizeof(char **) * M);
2250 ESL_ALLOC(mx->Lyshadow, sizeof(char **) * M);
2251 ESL_ALLOC(mx->Ryshadow, sizeof(char **) * M);
2252
2253 ESL_ALLOC(mx->Jkshadow, sizeof(int **) * M);
2254 ESL_ALLOC(mx->Lkshadow, sizeof(int **) * M);
2255 ESL_ALLOC(mx->Rkshadow, sizeof(int **) * M);
2256 ESL_ALLOC(mx->Tkshadow, sizeof(int **) * M);
2257
2258 ESL_ALLOC(mx->Lkmode, sizeof(char **) * M);
2259 ESL_ALLOC(mx->Rkmode, sizeof(char **) * M);
2260
2261 /* level 3: matrix cell memory, when creating only allocate 1 cell per state, for j = 0, d = 0 */
2262 ESL_ALLOC(mx->Jyshadow_mem, (sizeof(char) * (int64_t) (M-B) * (int64_t) allocL * (int64_t) allocW));
2263 ESL_ALLOC(mx->Lyshadow_mem, (sizeof(char) * (int64_t) (M-B) * (int64_t) allocL * (int64_t) allocW));
2264 ESL_ALLOC(mx->Ryshadow_mem, (sizeof(char) * (int64_t) (M-B) * (int64_t) allocL * (int64_t) allocW));
2265
2266 ESL_ALLOC(mx->Jkshadow_mem, sizeof(int) * ESL_MAX(1, (int64_t) B * (int64_t) allocL * (int64_t) allocW)); // avoid 0 malloc
2267 ESL_ALLOC(mx->Lkshadow_mem, sizeof(int) * ESL_MAX(1, (int64_t) B * (int64_t) allocL * (int64_t) allocW));
2268 ESL_ALLOC(mx->Rkshadow_mem, sizeof(int) * ESL_MAX(1, (int64_t) B * (int64_t) allocL * (int64_t) allocW));
2269 ESL_ALLOC(mx->Tkshadow_mem, sizeof(int) * ESL_MAX(1, (int64_t) B * (int64_t) allocL * (int64_t) allocW));
2270
2271 ESL_ALLOC(mx->Lkmode_mem, sizeof(char) * ESL_MAX(1, (int64_t) B * (int64_t) allocL * (int64_t) allocW));
2272 ESL_ALLOC(mx->Rkmode_mem, sizeof(char) * ESL_MAX(1, (int64_t) B * (int64_t) allocL * (int64_t) allocW));
2273
2274 b = 0;
2275 for (v = 0; v < M; v++) {
2276 if(cm->sttype[v] == B_st) {
2277 ESL_ALLOC(mx->Jkshadow[v], sizeof(int *) * (int64_t) allocL);
2278 ESL_ALLOC(mx->Lkshadow[v], sizeof(int *) * (int64_t) allocL);
2279 ESL_ALLOC(mx->Rkshadow[v], sizeof(int *) * (int64_t) allocL);
2280 ESL_ALLOC(mx->Tkshadow[v], sizeof(int *) * (int64_t) allocL);
2281
2282 ESL_ALLOC(mx->Lkmode[v], sizeof(char *) * (int64_t) allocL);
2283 ESL_ALLOC(mx->Rkmode[v], sizeof(char *) * (int64_t) allocL);
2284
2285 mx->Jkshadow[v][0] = mx->Jkshadow_mem + (int64_t) b * (int64_t) allocL * (int64_t) allocW;
2286 mx->Lkshadow[v][0] = mx->Lkshadow_mem + (int64_t) b * (int64_t) allocL * (int64_t) allocW;
2287 mx->Rkshadow[v][0] = mx->Rkshadow_mem + (int64_t) b * (int64_t) allocL * (int64_t) allocW;
2288 mx->Tkshadow[v][0] = mx->Tkshadow_mem + (int64_t) b * (int64_t) allocL * (int64_t) allocW;
2289
2290 mx->Lkmode[v][0] = mx->Lkmode_mem + (int64_t) b * (int64_t) allocL * (int64_t) allocW;
2291 mx->Rkmode[v][0] = mx->Rkmode_mem + (int64_t) b * (int64_t) allocL * (int64_t) allocW;
2292
2293 mx->Jyshadow[v] = NULL;
2294 mx->Lyshadow[v] = NULL;
2295 mx->Ryshadow[v] = NULL;
2296 b++;
2297 }
2298 else {
2299 ESL_ALLOC(mx->Jyshadow[v], sizeof(char *) * (int64_t) allocL);
2300 ESL_ALLOC(mx->Lyshadow[v], sizeof(char *) * (int64_t) allocL);
2301 ESL_ALLOC(mx->Ryshadow[v], sizeof(char *) * (int64_t) allocL);
2302
2303 mx->Jyshadow[v][0] = mx->Jyshadow_mem + (int64_t) (v-b) * (int64_t) allocL * (int64_t) allocW;
2304 mx->Lyshadow[v][0] = mx->Lyshadow_mem + (int64_t) (v-b) * (int64_t) allocL * (int64_t) allocW;
2305 mx->Ryshadow[v][0] = mx->Ryshadow_mem + (int64_t) (v-b) * (int64_t) allocL * (int64_t) allocW;
2306 mx->Jkshadow[v] = NULL;
2307 mx->Lkshadow[v] = NULL;
2308 mx->Rkshadow[v] = NULL;
2309 mx->Tkshadow[v] = NULL;
2310
2311 mx->Lkmode[v] = NULL;
2312 mx->Rkmode[v] = NULL;
2313 }
2314 }
2315 mx->M = M;
2316 mx->B = B;
2317 mx->Jy_ncells_alloc = (int64_t) (M-B) * (int64_t) allocL * (int64_t) allocW;
2318 mx->Ly_ncells_alloc = (int64_t) (M-B) * (int64_t) allocL * (int64_t) allocW;
2319 mx->Ry_ncells_alloc = (int64_t) (M-B) * (int64_t) allocL * (int64_t) allocW;
2320 mx->Jy_ncells_valid = 0;
2321 mx->Ly_ncells_valid = 0;
2322 mx->Ry_ncells_valid = 0;
2323 mx->Jk_ncells_alloc = (int64_t) (B) * (int64_t) allocL * (int64_t) allocW;
2324 mx->Lk_ncells_alloc = (int64_t) (B) * (int64_t) allocL * (int64_t) allocW;
2325 mx->Rk_ncells_alloc = (int64_t) (B) * (int64_t) allocL * (int64_t) allocW;
2326 mx->Tk_ncells_alloc = (int64_t) (B) * (int64_t) allocL * (int64_t) allocW;
2327 mx->Jk_ncells_valid = 0;
2328 mx->Rk_ncells_valid = 0;
2329 mx->Tk_ncells_valid = 0;
2330 mx->L = allocL; /* allocL = 1 */
2331
2332 /* calculate size, these are in order of when they were allocated */
2333 mx->size_Mb = (float)
2334 (sizeof(CM_TR_SHADOW_MX) +
2335 (3 * (mx->M) * sizeof(char **)) + /* mx->{J,L,R}yshadow[] ptrs */
2336 (4 * (mx->M) * sizeof(int **)) + /* mx->{J,L,R,T}kshadow[] ptrs */
2337 mx->Jy_ncells_alloc * sizeof(char) + /* mx->Jyshadow_mem */
2338 mx->Ly_ncells_alloc * sizeof(char) + /* mx->Lyshadow_mem */
2339 mx->Ry_ncells_alloc * sizeof(char) + /* mx->Ryshadow_mem */
2340 mx->Jk_ncells_alloc * sizeof(int) + /* mx->Jkshadow_mem */
2341 mx->Lk_ncells_alloc * sizeof(int) + /* mx->Lkshadow_mem */
2342 mx->Rk_ncells_alloc * sizeof(int) + /* mx->Rkshadow_mem */
2343 mx->Tk_ncells_alloc * sizeof(int) + /* mx->Tkshadow_mem */
2344 mx->Lk_ncells_alloc * sizeof(char) + /* mx->Lkmode_mem */
2345 mx->Rk_ncells_alloc * sizeof(char) + /* mx->Rkmode_mem */
2346 (4 * mx->B * allocL * sizeof(int *)) + /* mx->{J,L,R,T}kshadow[v][] ptrs */
2347 (3 * (mx->M - mx->B) * allocL * sizeof(char *))); /* mx->{J,L,R,T}kshadow[v][] ptrs */
2348 mx->size_Mb *= 0.000001; /* convert to Mb */
2349
2350 return mx;
2351
2352 ERROR:
2353 if (mx != NULL) cm_tr_shadow_mx_Destroy(mx);
2354 return NULL;
2355 }
2356
2357 /* Function: cm_tr_shadow_mx_GrowTo()
2358 * Incept: EPN, Sat Sep 10 12:12:06 2011
2359 *
2360 * Purpose: Assures that a CM_TR_SHADOW_MX <mx> is allocated
2361 * for a model of exactly <mx->M> states and a sequence
2362 * of length L, reallocating as necessary.
2363 *
2364 * Checks that the matrix has been created for the current CM.
2365 * Check is that mx->yshadow[v] == NULL when v is a B_st and
2366 * mx->kshadow[v] != NULL when v is a B_st.
2367 *
2368 * Checks to make sure desired matrix isn't too big (see throws).
2369 *
2370 * Args: cm - the CM the matrix is for
2371 * mx - the matrix to grow
2372 * errbuf - char buffer for reporting errors
2373 * L - the length of the current target sequence we're aligning
2374 * size_limit- max number of Mb for DP matrix, if matrix is bigger -> return eslERANGE
2375 *
2376 * Returns: <eslOK> on success, and <mx> may be reallocated upon
2377 * return; any data that may have been in <mx> must be
2378 * assumed to be invalidated.
2379 *
2380 * Throws: <eslERANGE> if required size to grow to exceeds <size_limit>.
2381 * This should be caught and appropriately handled by caller.
2382 * <eslEINCOMPAT> if mx does not appeared to be created for this cm
2383 * <eslEMEM> on memory allocation error.
2384 */
2385 int
2386 cm_tr_shadow_mx_GrowTo(CM_t *cm, CM_TR_SHADOW_MX *mx, char *errbuf, int L, float size_limit)
2387 {
2388 int status;
2389 void *p;
2390 int v, jp;
2391 int64_t Jy_cur_size;
2392 int64_t Ly_cur_size;
2393 int64_t Ry_cur_size;
2394 int64_t Jk_cur_size;
2395 int64_t Lk_cur_size;
2396 int64_t Rk_cur_size;
2397 int64_t Tk_cur_size;
2398 int64_t Jy_ncells;
2399 int64_t Ly_ncells;
2400 int64_t Ry_ncells;
2401 int64_t Jk_ncells;
2402 int64_t Lk_ncells;
2403 int64_t Rk_ncells;
2404 int64_t Tk_ncells;
2405 float Mb_needed; /* required size of matrix, given the bands */
2406 float Mb_alloc; /* allocated size of matrix, >= Mb_needed */
2407 int realloced_Jy; /* did we reallocate mx->Jyshadow_mem? */
2408 int realloced_Ly; /* did we reallocate mx->Lyshadow_mem? */
2409 int realloced_Ry; /* did we reallocate mx->Ryshadow_mem? */
2410 int realloced_Jk; /* did we reallocate mx->Jkshadow_mem? */
2411 int realloced_Lk; /* did we reallocate mx->Lkshadow_mem & mx->Lkmode_mem? */
2412 int realloced_Rk; /* did we reallocate mx->Rkshadow_mem & mx->Rkmode_mem? */
2413 int realloced_Tk; /* did we reallocate mx->Tkshadow_mem? */
2414
2415 if((status = cm_tr_shadow_mx_SizeNeeded(cm, errbuf, L, &Jy_ncells, &Ly_ncells, &Ry_ncells, &Jk_ncells, &Lk_ncells, &Rk_ncells, &Tk_ncells, &Mb_needed)) != eslOK) return status;
2416 /*printf("Non-banded Tr shadow matrix requested size: %.2f Mb\n", Mb_needed);*/
2417 ESL_DPRINTF2(("#DEBUG: Non-banded Tr shadow matrix requested size: %.2f Mb\n", Mb_needed));
2418 if(Mb_needed > size_limit) ESL_FAIL(eslERANGE, errbuf, "requested non-banded Tr shadow DP mx of %.2f Mb > %.2f Mb limit.\nUse --mxsize, --maxtau or --tau.", Mb_needed, (float) size_limit);
2419
2420 /* must we realloc the full {J,L,R}yshadow and {J,L,R,T}kshadow matrices?
2421 * or can we get away with just jiggering the pointers, if
2422 * total required num cells is less than or equal to what
2423 * we already have alloc'ed?
2424 */
2425 realloced_Jy = realloced_Ly = realloced_Ry = FALSE;
2426 realloced_Jk = realloced_Lk = realloced_Rk = realloced_Tk = FALSE;
2427 if (Jy_ncells > mx->Jy_ncells_alloc) {
2428 ESL_RALLOC(mx->Jyshadow_mem, p, sizeof(char) * Jy_ncells);
2429 mx->Jy_ncells_alloc = Jy_ncells;
2430 realloced_Jy = TRUE;
2431 }
2432 if (Ly_ncells > mx->Ly_ncells_alloc) {
2433 ESL_RALLOC(mx->Lyshadow_mem, p, sizeof(char) * Ly_ncells);
2434 mx->Ly_ncells_alloc = Ly_ncells;
2435 realloced_Ly = TRUE;
2436 }
2437 if (Ry_ncells > mx->Ry_ncells_alloc) {
2438 ESL_RALLOC(mx->Ryshadow_mem, p, sizeof(char) * Ry_ncells);
2439 mx->Ry_ncells_alloc = Ry_ncells;
2440 realloced_Ry = TRUE;
2441 }
2442 if (Jk_ncells > mx->Jk_ncells_alloc) {
2443 ESL_RALLOC(mx->Jkshadow_mem, p, sizeof(int) * Jk_ncells);
2444 mx->Jk_ncells_alloc = Jk_ncells;
2445 realloced_Jk = TRUE;
2446 }
2447 if (Lk_ncells > mx->Lk_ncells_alloc) {
2448 ESL_RALLOC(mx->Lkshadow_mem, p, sizeof(int) * Lk_ncells);
2449 ESL_RALLOC(mx->Lkmode_mem, p, sizeof(char) * Lk_ncells);
2450 mx->Lk_ncells_alloc = Lk_ncells;
2451 realloced_Lk = TRUE;
2452 }
2453 if (Rk_ncells > mx->Rk_ncells_alloc) {
2454 ESL_RALLOC(mx->Rkshadow_mem, p, sizeof(int) * Rk_ncells);
2455 ESL_RALLOC(mx->Rkmode_mem, p, sizeof(char) * Rk_ncells);
2456 mx->Rk_ncells_alloc = Rk_ncells;
2457 realloced_Rk = TRUE;
2458 }
2459 if (Tk_ncells > mx->Tk_ncells_alloc) {
2460 ESL_RALLOC(mx->Tkshadow_mem, p, sizeof(int) * Tk_ncells);
2461 mx->Tk_ncells_alloc = Tk_ncells;
2462 realloced_Tk = TRUE;
2463 }
2464 /* Determine the size of our matrix based on the size it needed to be (Mb_needed).
2465 * This is tricky, for each matrix not reallocated, we have to adjust Mb_needed
2466 * so it uses previously allocated size of that matrix.
2467 */
2468 Mb_alloc = Mb_needed * 1000000; /* convert to bytes */
2469 if(! realloced_Jy) {
2470 Mb_alloc -= (float) (sizeof(char) * Jy_ncells);
2471 Mb_alloc += (float) (sizeof(char) * mx->Jy_ncells_alloc);
2472 }
2473 if(! realloced_Ly) {
2474 Mb_alloc -= (float) (sizeof(char) * Ly_ncells);
2475 Mb_alloc += (float) (sizeof(char) * mx->Ly_ncells_alloc);
2476 }
2477 if(! realloced_Ry) {
2478 Mb_alloc -= (float) (sizeof(char) * Ry_ncells);
2479 Mb_alloc += (float) (sizeof(char) * mx->Ry_ncells_alloc);
2480 }
2481 if(! realloced_Jk) {
2482 Mb_alloc -= (float) (sizeof(int) * Jk_ncells);
2483 Mb_alloc += (float) (sizeof(int) * mx->Jk_ncells_alloc);
2484 }
2485 if(! realloced_Lk) {
2486 Mb_alloc -= (float) (sizeof(int) * Lk_ncells);
2487 Mb_alloc += (float) (sizeof(int) * mx->Lk_ncells_alloc);
2488 }
2489 if(! realloced_Rk) {
2490 Mb_alloc -= (float) (sizeof(int) * Rk_ncells);
2491 Mb_alloc += (float) (sizeof(int) * mx->Rk_ncells_alloc);
2492 }
2493 if(! realloced_Tk) {
2494 Mb_alloc -= (float) (sizeof(int) * Tk_ncells);
2495 Mb_alloc += (float) (sizeof(int) * mx->Tk_ncells_alloc);
2496 }
2497 /* note if we didn't reallocate any of the four matrices, Mb_alloc == Mb_needed */
2498 Mb_alloc *= 0.000001; /* convert to Mb */
2499
2500 mx->Jy_ncells_valid = Jy_ncells;
2501 mx->Ly_ncells_valid = Ly_ncells;
2502 mx->Ry_ncells_valid = Ry_ncells;
2503 mx->Jk_ncells_valid = Jk_ncells;
2504 mx->Lk_ncells_valid = Lk_ncells;
2505 mx->Rk_ncells_valid = Rk_ncells;
2506 mx->Tk_ncells_valid = Tk_ncells;
2507
2508 /* reallocate the {J,L,R,T}dp[v] ptrs */
2509 for(v = 0; v < mx->M; v++) {
2510 if(cm->sttype[v] != B_st) {
2511 ESL_RALLOC(mx->Jyshadow[v], p, sizeof(char *) * (L+1));
2512 ESL_RALLOC(mx->Lyshadow[v], p, sizeof(char *) * (L+1));
2513 ESL_RALLOC(mx->Ryshadow[v], p, sizeof(char *) * (L+1));
2514 }
2515 else {
2516 ESL_RALLOC(mx->Jkshadow[v], p, sizeof(int *) * (L+1));
2517 ESL_RALLOC(mx->Lkshadow[v], p, sizeof(int *) * (L+1));
2518 ESL_RALLOC(mx->Rkshadow[v], p, sizeof(int *) * (L+1));
2519 ESL_RALLOC(mx->Tkshadow[v], p, sizeof(int *) * (L+1));
2520 ESL_RALLOC(mx->Lkmode[v], p, sizeof(char *) * (L+1));
2521 ESL_RALLOC(mx->Rkmode[v], p, sizeof(char *) * (L+1));
2522 }
2523 }
2524
2525 /* reset the pointers, we keep a tally of number of cells
2526 * we've seen in each matrix (y_cur_size and k_cur_size) as we go,
2527 * we could precalc it and store it for each v,j, but that
2528 * would be wasteful, as we'll only use the matrix configured
2529 * this way once, in a banded CYK run.
2530 */
2531 Jy_cur_size = 0;
2532 Ly_cur_size = 0;
2533 Ry_cur_size = 0;
2534 Jk_cur_size = 0;
2535 Lk_cur_size = 0;
2536 Rk_cur_size = 0;
2537 Tk_cur_size = 0;
2538 for(v = 0; v < mx->M; v++) {
2539 if(cm->sttype[v] != B_st) {
2540 for(jp = 0; jp <= L; jp++) {
2541 mx->Jyshadow[v][jp] = mx->Jyshadow_mem + Jy_cur_size;
2542 mx->Lyshadow[v][jp] = mx->Lyshadow_mem + Ly_cur_size;
2543 mx->Ryshadow[v][jp] = mx->Ryshadow_mem + Ry_cur_size;
2544 Jy_cur_size += (int64_t) (jp+1);
2545 Ly_cur_size += (int64_t) (jp+1);
2546 Ry_cur_size += (int64_t) (jp+1);
2547 }
2548 }
2549 else {
2550 for(jp = 0; jp <= L; jp++) {
2551 mx->Jkshadow[v][jp] = mx->Jkshadow_mem + Jk_cur_size;
2552 mx->Lkshadow[v][jp] = mx->Lkshadow_mem + Lk_cur_size;
2553 mx->Lkmode[v][jp] = mx->Lkmode_mem + Lk_cur_size;
2554 mx->Rkshadow[v][jp] = mx->Rkshadow_mem + Rk_cur_size;
2555 mx->Rkmode[v][jp] = mx->Rkmode_mem + Rk_cur_size;
2556 mx->Tkshadow[v][jp] = mx->Tkshadow_mem + Tk_cur_size;
2557 Jk_cur_size += (int64_t) (jp+1);
2558 Lk_cur_size += (int64_t) (jp+1);
2559 Rk_cur_size += (int64_t) (jp+1);
2560 Tk_cur_size += (int64_t) (jp+1);
2561 }
2562 }
2563 }
2564 /*printf("Jy ncells %10" PRId64 " %10" PRId64 "\n", Jy_cur_size, mx->Jy_ncells_valid);
2565 printf("Ly ncells %10" PRId64 " %10" PRId64 "\n", Ly_cur_size, mx->Ly_ncells_valid);
2566 printf("Ry ncells %10" PRId64 " %10" PRId64 "\n", Ry_cur_size, mx->Ry_ncells_valid);
2567 printf("Jk ncells %10" PRId64 " %10" PRId64 "\n", Jk_cur_size, mx->Jk_ncells_valid);
2568 printf("Lk ncells %10" PRId64 " %10" PRId64 "\n", Lk_cur_size, mx->Lk_ncells_valid);
2569 printf("Rk ncells %10" PRId64 " %10" PRId64 "\n", Rk_cur_size, mx->Rk_ncells_valid);
2570 printf("Tk ncells %10" PRId64 " %10" PRId64 "\n", Tk_cur_size, mx->Tk_ncells_valid);
2571 */
2572 assert(Jy_cur_size == mx->Jy_ncells_valid);
2573 assert(Ly_cur_size == mx->Ly_ncells_valid);
2574 assert(Ry_cur_size == mx->Ry_ncells_valid);
2575 assert(Jk_cur_size == mx->Jk_ncells_valid);
2576 assert(Lk_cur_size == mx->Lk_ncells_valid);
2577 assert(Rk_cur_size == mx->Rk_ncells_valid);
2578 assert(Tk_cur_size == mx->Tk_ncells_valid);
2579 ESL_DASSERT1((Jy_cur_size == mx->Jy_ncells_valid));
2580 ESL_DASSERT1((Ly_cur_size == mx->Ly_ncells_valid));
2581 ESL_DASSERT1((Ry_cur_size == mx->Ry_ncells_valid));
2582 ESL_DASSERT1((Jk_cur_size == mx->Jk_ncells_valid));
2583 ESL_DASSERT1((Lk_cur_size == mx->Lk_ncells_valid));
2584 ESL_DASSERT1((Rk_cur_size == mx->Rk_ncells_valid));
2585 ESL_DASSERT1((Tk_cur_size == mx->Tk_ncells_valid));
2586
2587 /* now update L and size_Mb */
2588 mx->L = L; /* length of current seq we're valid for */
2589 mx->size_Mb = Mb_alloc;
2590
2591 return eslOK;
2592
2593 ERROR:
2594 return status;
2595 }
2596
2597 /* Function: cm_tr_shadow_mx_Destroy()
2598 * Synopsis: Frees a DP matrix.
2599 * Incept: EPN, Sat Sep 10 12:21:26 2011
2600 *
2601 * Purpose: Frees a <CM_TR_SHADOW_MX>.
2602 *
2603 * Returns: (void)
2604 */
2605 void
2606 cm_tr_shadow_mx_Destroy(CM_TR_SHADOW_MX *mx)
2607 {
2608 if (mx == NULL) return;
2609 int v;
2610
2611 if (mx->Jyshadow != NULL) {
2612 for (v = 0; v < mx->M; v++)
2613 if(mx->Jyshadow[v] != NULL) free(mx->Jyshadow[v]);
2614 }
2615 free(mx->Jyshadow);
2616
2617 if (mx->Lyshadow != NULL) {
2618 for (v = 0; v < mx->M; v++)
2619 if(mx->Lyshadow[v] != NULL) free(mx->Lyshadow[v]);
2620 }
2621 free(mx->Lyshadow);
2622
2623 if (mx->Ryshadow != NULL) {
2624 for (v = 0; v < mx->M; v++)
2625 if(mx->Ryshadow[v] != NULL) free(mx->Ryshadow[v]);
2626 }
2627 free(mx->Ryshadow);
2628
2629 if (mx->Jkshadow != NULL) {
2630 for (v = 0; v < mx->M; v++)
2631 if(mx->Jkshadow[v] != NULL) free(mx->Jkshadow[v]);
2632 }
2633 free(mx->Jkshadow);
2634
2635 if (mx->Lkshadow != NULL) {
2636 for (v = 0; v < mx->M; v++)
2637 if(mx->Lkshadow[v] != NULL) free(mx->Lkshadow[v]);
2638 }
2639 free(mx->Lkshadow);
2640
2641 if (mx->Rkshadow != NULL) {
2642 for (v = 0; v < mx->M; v++)
2643 if(mx->Rkshadow[v] != NULL) free(mx->Rkshadow[v]);
2644 }
2645 free(mx->Rkshadow);
2646
2647 if (mx->Tkshadow != NULL) {
2648 for (v = 0; v < mx->M; v++)
2649 if(mx->Tkshadow[v] != NULL) free(mx->Tkshadow[v]);
2650 }
2651 free(mx->Tkshadow);
2652
2653 if (mx->Lkmode != NULL) {
2654 for (v = 0; v < mx->M; v++)
2655 if(mx->Lkmode[v] != NULL) free(mx->Lkmode[v]);
2656 }
2657 free(mx->Lkmode);
2658
2659 if (mx->Rkmode != NULL) {
2660 for (v = 0; v < mx->M; v++)
2661 if(mx->Rkmode[v] != NULL) free(mx->Rkmode[v]);
2662 }
2663 free(mx->Rkmode);
2664
2665 if (mx->Jyshadow_mem != NULL) free(mx->Jyshadow_mem);
2666 if (mx->Lyshadow_mem != NULL) free(mx->Lyshadow_mem);
2667 if (mx->Ryshadow_mem != NULL) free(mx->Ryshadow_mem);
2668 if (mx->Jkshadow_mem != NULL) free(mx->Jkshadow_mem);
2669 if (mx->Lkshadow_mem != NULL) free(mx->Lkshadow_mem);
2670 if (mx->Rkshadow_mem != NULL) free(mx->Rkshadow_mem);
2671 if (mx->Tkshadow_mem != NULL) free(mx->Tkshadow_mem);
2672 if (mx->Lkmode_mem != NULL) free(mx->Lkmode_mem);
2673 if (mx->Rkmode_mem != NULL) free(mx->Rkmode_mem);
2674 free(mx);
2675 return;
2676 }
2677
2678 /* Function: cm_tr_shadow_mx_Dump()
2679 * Synopsis: Dump a DP matrix to a stream, for diagnostics.
2680 * Incept: EPN, Sat Sep 10 12:21:53 2011
2681 *
2682 * Purpose: Dump matrix <mx> to stream <fp> for diagnostics.
2683 */
2684 int
2685 cm_tr_shadow_mx_Dump(FILE *ofp, CM_t *cm, CM_TR_SHADOW_MX *mx, char mode, int print_mx)
2686 {
2687 int status;
2688 int v, j, d;
2689 int fill_L, fill_R, fill_T; /* are the L, R, and T matrices valid? */
2690
2691 fprintf(ofp, "M: %d\n", mx->M);
2692 fprintf(ofp, "B: %d\n", mx->B);
2693 fprintf(ofp, "L: %d\n", mx->L);
2694 fprintf(ofp, "Jy_ncells_alloc: %" PRId64 "\nJy_ncells_valid: %" PRId64 "\n", mx->Jy_ncells_alloc, mx->Jy_ncells_valid);
2695 fprintf(ofp, "Ly_ncells_alloc: %" PRId64 "\nLy_ncells_valid: %" PRId64 "\n", mx->Ly_ncells_alloc, mx->Ly_ncells_valid);
2696 fprintf(ofp, "Ry_ncells_alloc: %" PRId64 "\nRy_ncells_valid: %" PRId64 "\n", mx->Ry_ncells_alloc, mx->Ry_ncells_valid);
2697 fprintf(ofp, "Jk_ncells_alloc: %" PRId64 "\nJk_ncells_valid: %" PRId64 "\n", mx->Jk_ncells_alloc, mx->Jk_ncells_valid);
2698 fprintf(ofp, "Lk_ncells_alloc: %" PRId64 "\nLk_ncells_valid: %" PRId64 "\n", mx->Lk_ncells_alloc, mx->Lk_ncells_valid);
2699 fprintf(ofp, "Rk_ncells_alloc: %" PRId64 "\nRk_ncells_valid: %" PRId64 "\n", mx->Rk_ncells_alloc, mx->Rk_ncells_valid);
2700 fprintf(ofp, "Tk_ncells_alloc: %" PRId64 "\nTk_ncells_valid: %" PRId64 "\n", mx->Tk_ncells_alloc, mx->Tk_ncells_valid);
2701 fprintf(ofp, "mode: %d\n", mode);
2702
2703 if((status = cm_TrFillFromMode(mode, &fill_L, &fill_R, &fill_T)) != eslOK) return status;
2704
2705 if(print_mx) {
2706 /* yshadow/kshadow matrix data */
2707 for (v = 0; v < mx->M; v++) {
2708 if(cm->sttype[v] == B_st) {
2709 for(j = 0; j <= mx->L; j++) {
2710 for(d = 0; d <= j; d++) {
2711 if(mx->Jkshadow[v]) fprintf(ofp, "Jkshad[v:%5d][j:%5d][d:%5d] %8d\n", v, j, d, mx->Jkshadow[v][j][d]);
2712 if(mx->Lkshadow[v] && fill_L) fprintf(ofp, "Lkshad[v:%5d][j:%5d][d:%5d] %8d\n", v, j, d, mx->Lkshadow[v][j][d]);
2713 if(mx->Rkshadow[v] && fill_R) fprintf(ofp, "Rkshad[v:%5d][j:%5d][d:%5d] %8d\n", v, j, d, mx->Rkshadow[v][j][d]);
2714 if(mx->Tkshadow[v] && fill_T) fprintf(ofp, "Tkshad[v:%5d][j:%5d][d:%5d] %8d\n", v, j, d, mx->Tkshadow[v][j][d]);
2715 }
2716 fprintf(ofp, "\n");
2717 }
2718 fprintf(ofp, "\n\n");
2719 }
2720 else { /* ! B_st */
2721 for(j = 0; j <= mx->L; j++) {
2722 for(d = 0; d <= j; d++) {
2723 if(mx->Jyshadow[v]) fprintf(ofp, "Jyshad[v:%5d][j:%5d][d:%5d] %8d\n", v, j, d, mx->Jyshadow[v][j][d]);
2724 if(mx->Lyshadow[v] && fill_L) fprintf(ofp, "Lyshad[v:%5d][j:%5d][d:%5d] %8d\n", v, j, d, mx->Lyshadow[v][j][d]);
2725 if(mx->Ryshadow[v] && fill_R) fprintf(ofp, "Ryshad[v:%5d][j:%5d][d:%5d] %8d\n", v, j, d, mx->Ryshadow[v][j][d]);
2726 }
2727 fprintf(ofp, "\n");
2728 }
2729 fprintf(ofp, "\n\n");
2730 }
2731 }
2732 }
2733 return eslOK;
2734 }
2735
2736 /* Function: cm_tr_shadow_mx_SizeNeeded()
2737 * Incept: EPN, Sat Sep 10 12:23:10 2011
2738 *
2739 * Purpose: Given a model, and a sequence length L determine the
2740 * number of cells and total size in Mb required for the
2741 * matrix for the target given the bands.
2742 *
2743 * Return number of {J,L,R}yshadow (char) cells required in
2744 * <ret_{J,L,R}ny_cells> and number of {J,L,R,T}kshadow (int) cells
2745 * required in <ret_{J,L,R,T}nk_cells) and size of required
2746 * matrix in Mb in <ret_Mb>.
2747 *
2748 * Args: cm - the CM the matrix is for
2749 * errbuf - char buffer for reporting errors
2750 * L - length of sequence we will align
2751 * ret_Jny_cells - RETURN: number of required char cells for J (Jyshadow)
2752 * ret_Lny_cells - RETURN: number of required char cells for L (Lyshadow)
2753 * ret_Rny_cells - RETURN: number of required char cells for R (Ryshadow)
2754 * ret_Jnk_cells - RETURN: number of required int cells for J (Jkshadow)
2755 * ret_Lnk_cells - RETURN: number of required int cells for L (Lkshadow)
2756 * ret_Rnk_cells - RETURN: number of required int cells for R (Rkshadow)
2757 * ret_Tnk_cells - RETURN: number of required int cells for T (Tkshadow)
2758 * ret_Mb - RETURN: required size of matrix in Mb
2759 *
2760 * Returns: <eslOK> on success
2761 *
2762 */
2763 int
2764 cm_tr_shadow_mx_SizeNeeded(CM_t *cm, char *errbuf, int L, int64_t *ret_Jny_cells, int64_t *ret_Lny_cells, int64_t *ret_Rny_cells,
2765 int64_t *ret_Jnk_cells, int64_t *ret_Lnk_cells, int64_t *ret_Rnk_cells, int64_t *ret_Tnk_cells, float *ret_Mb)
2766 {
2767 int v;
2768 int64_t Jy_ncells;
2769 int64_t Ly_ncells;
2770 int64_t Ry_ncells;
2771 int64_t Jk_ncells;
2772 int64_t Lk_ncells;
2773 int64_t Rk_ncells;
2774 int64_t Tk_ncells;
2775 float Mb_needed;
2776
2777 Jy_ncells = 0;
2778 Ly_ncells = 0;
2779 Ry_ncells = 0;
2780 Jk_ncells = 0;
2781 Lk_ncells = 0;
2782 Rk_ncells = 0;
2783 Tk_ncells = 0;
2784 Mb_needed = (float)
2785 (sizeof(CM_TR_SHADOW_MX) +
2786 (3 * (cm->M) * sizeof(char **)) + /* mx->{J,L,R}yshadow[] ptrs */
2787 (4 * (cm->M) * sizeof(int **))); /* mx->{J,L,R,T}kshadow[] ptrs */
2788
2789 for(v = 0; v < cm->M; v++) {
2790 if(cm->sttype[v] == B_st) {
2791 Mb_needed += (float) (sizeof(int *) * (L+1)); /* mx->Jkshadow[v][] ptrs */
2792 Mb_needed += (float) (sizeof(int *) * (L+1)); /* mx->Lkshadow[v][] ptrs */
2793 Mb_needed += (float) (sizeof(int *) * (L+1)); /* mx->Rkshadow[v][] ptrs */
2794 Mb_needed += (float) (sizeof(int *) * (L+1)); /* mx->Tkshadow[v][] ptrs */
2795 Mb_needed += (float) (sizeof(char *) * (L+1)); /* mx->Lkmode[v][] ptrs */
2796 Mb_needed += (float) (sizeof(char *) * (L+1)); /* mx->Rkmode[v][] ptrs */
2797 Jk_ncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
2798 Lk_ncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
2799 Rk_ncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
2800 Tk_ncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
2801 }
2802 else {
2803 Mb_needed += (float) (sizeof(char *) * (L+1)); /* mx->Jyshadow[v][] ptrs */
2804 Mb_needed += (float) (sizeof(char *) * (L+1)); /* mx->Lyshadow[v][] ptrs */
2805 Mb_needed += (float) (sizeof(char *) * (L+1)); /* mx->Ryshadow[v][] ptrs */
2806 Jy_ncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
2807 Ly_ncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
2808 Ry_ncells += (int64_t) ((int64_t) (L+2) * (int64_t) (L+1) * 0.5);
2809 }
2810 }
2811
2812 Mb_needed += sizeof(int) * Jk_ncells; /* mx->Jkshadow_mem */
2813 Mb_needed += sizeof(int) * Lk_ncells; /* mx->Jkshadow_mem */
2814 Mb_needed += sizeof(int) * Rk_ncells; /* mx->Jkshadow_mem */
2815 Mb_needed += sizeof(int) * Tk_ncells; /* mx->Jkshadow_mem */
2816 Mb_needed += sizeof(char) * Lk_ncells; /* mx->Jkmode_mem */
2817 Mb_needed += sizeof(char) * Rk_ncells; /* mx->Jkmode_mem */
2818 Mb_needed += sizeof(char) * Jy_ncells; /* mx->Jyshadow_mem */
2819 Mb_needed += sizeof(char) * Ly_ncells; /* mx->Jyshadow_mem */
2820 Mb_needed += sizeof(char) * Ry_ncells; /* mx->Jyshadow_mem */
2821 Mb_needed *= 0.000001; /* convert to megabytes */
2822
2823 if(ret_Jny_cells != NULL) *ret_Jny_cells = Jy_ncells;
2824 if(ret_Lny_cells != NULL) *ret_Lny_cells = Ly_ncells;
2825 if(ret_Rny_cells != NULL) *ret_Rny_cells = Ry_ncells;
2826 if(ret_Jnk_cells != NULL) *ret_Jnk_cells = Jk_ncells;
2827 if(ret_Lnk_cells != NULL) *ret_Lnk_cells = Lk_ncells;
2828 if(ret_Rnk_cells != NULL) *ret_Rnk_cells = Rk_ncells;
2829 if(ret_Tnk_cells != NULL) *ret_Tnk_cells = Tk_ncells;
2830 if(ret_Mb != NULL) *ret_Mb = Mb_needed;
2831 return eslOK;
2832 }
2833
2834 /*****************************************************************
2835 * 7. CM_HB_SHADOW_MX data structure functions,
2836 * HMM banded shadow matrix for tracing back HMM banded CM parses.
2837 *****************************************************************/
2838
2839 /* Function: cm_hb_shadow_mx_Create()
2840 * Incept: EPN, Fri Oct 26 05:05:07 2007
2841 *
2842 * Purpose: Allocate a reusable, resizeable <CM_HB_SHADOW_MX> for a CM <cm>
2843 * given a CP9Bands_t object that defines the bands. The CM
2844 * is needed so we know which decks need to be int's (BIF_B states)
2845 * and which need to be char's (all other states).
2846 *
2847 * We've set this up so it should be easy to allocate
2848 * aligned memory, though we're not doing this yet.
2849 *
2850 * Returns: a pointer to the new <CM_HB_SHADOW_MX>.
2851 *
2852 * Throws: <NULL> on allocation error.
2853 */
2854 CM_HB_SHADOW_MX *
2855 cm_hb_shadow_mx_Create(CM_t *cm)
2856 {
2857 int status;
2858 CM_HB_SHADOW_MX *mx = NULL;
2859 int v;
2860 int M = cm->M;
2861 int nbifs, nb;
2862 int allocL = 1;
2863 int allocW = 1;
2864
2865 /* level 1: the structure itself */
2866 ESL_ALLOC(mx, sizeof(CM_HB_SHADOW_MX));
2867 mx->yshadow = NULL;
2868 mx->yshadow_mem = NULL;
2869 mx->kshadow = NULL;
2870 mx->kshadow_mem = NULL;
2871 mx->cp9b = NULL;
2872
2873 /* level 2: deck (state) pointers, 0.1..M-1, M (EL deck) is irrelevant for the
2874 * shadow matrix.
2875 */
2876 ESL_ALLOC(mx->yshadow, sizeof(char **) * M);
2877 ESL_ALLOC(mx->kshadow, sizeof(int **) * M);
2878
2879 /* level 3: matrix cell memory, when creating only allocate 1 cell per state, for j = 0, d = 0 */
2880 nbifs = CMCountStatetype(cm, B_st);
2881
2882 ESL_ALLOC(mx->nrowsA, sizeof(int) * M);
2883 ESL_ALLOC(mx->yshadow_mem, (sizeof(char) * (M-nbifs)));
2884 ESL_ALLOC(mx->kshadow_mem, (sizeof(int) * ESL_MAX(1,nbifs))); // avoid 0 malloc
2885
2886 nb = 0;
2887 for (v = 0; v < M; v++) {
2888 if(cm->sttype[v] == B_st) {
2889 ESL_ALLOC(mx->kshadow[v], sizeof(int *) * (int64_t) allocL);
2890 mx->kshadow[v][0] = mx->kshadow_mem + (int64_t) nb * (int64_t) allocL * (int64_t) allocW;
2891 mx->yshadow[v] = NULL;
2892 nb++;
2893 }
2894 else {
2895 ESL_ALLOC(mx->yshadow[v], sizeof(char *) * (int64_t) allocL);
2896 mx->yshadow[v][0] = mx->yshadow_mem + (int64_t) (v-nb) * (int64_t) allocL * (int64_t) allocW;
2897 mx->kshadow[v] = NULL;
2898 }
2899 mx->nrowsA[v] = allocL;
2900 }
2901 mx->M = M;
2902 mx->B = nbifs;
2903 mx->y_ncells_alloc = (int64_t) (M-nbifs) * (int64_t) allocL * (int64_t) allocW;
2904 mx->y_ncells_valid = 0;
2905 mx->k_ncells_alloc = (int64_t) nbifs * (int64_t) allocL * (int64_t) allocW;
2906 mx->k_ncells_valid = 0;
2907 mx->L = allocL; /* allocL = 1 */
2908
2909 /* calculate size, these are in order of when they were allocated */
2910 mx->size_Mb = (float)
2911 (sizeof(CM_HB_SHADOW_MX) +
2912 (mx->M * sizeof(char **)) + /* mx->yshadow[] ptrs */
2913 (mx->M * sizeof(int **)) + /* mx->kshadow[] ptrs */
2914 mx->y_ncells_alloc * sizeof(char) + /* mx->yshadow_mem */
2915 mx->k_ncells_alloc * sizeof(int) + /* mx->kshadow_mem */
2916 (mx->M * sizeof(int)) + /* mx->nrowsA */
2917 ((int64_t) mx->B * (int64_t) allocL * sizeof(int *)) + /* mx->kshadow[v][] ptrs */
2918 ((int64_t) (mx->M-mx->B) * (int64_t) allocL * sizeof(int *))); /* mx->yshadow[v][] ptrs */
2919 mx->size_Mb *= 0.000001; /* convert to Mb */
2920
2921 return mx;
2922
2923 ERROR:
2924 if (mx != NULL) cm_hb_shadow_mx_Destroy(mx);
2925 return NULL;
2926 }
2927
2928 /* Function: cm_hb_shadow_mx_GrowTo()
2929 * Incept: EPN, Fri Oct 26 05:19:49 2007
2930 *
2931 * Purpose: Assures that a shadow matrix <mx> is allocated
2932 * for a model of exactly <mx->M> states and required number of
2933 * total cells. Determines new required size from
2934 * the CP9Bands_t object passed in, and reallocates if
2935 * necessary.
2936 *
2937 * Checks that the matrix has been created for the current CM.
2938 * Check is that mx->yshadow[v] == NULL when v is a B_st and
2939 * mx->kshadow[v] != NULL when v is a B_st.
2940 *
2941 * Checks to make sure desired matrix isn't too big (see throws).
2942 *
2943 * Args: cm - the CM the matrix is for
2944 * mx - the matrix to grow
2945 * errbuf - char buffer for reporting errors
2946 * cp9b - the bands for the current target sequence
2947 * L - the length of the current target sequence we're aligning
2948 * size_limit- max number of Mb for DP matrix, if matrix is bigger -> return eslERANGE
2949 *
2950 * Returns: <eslOK> on success, and <mx> may be reallocated upon
2951 * return; any data that may have been in <mx> must be
2952 * assumed to be invalidated.
2953 *
2954 * Throws: <eslERANGE> if required size to grow to exceeds <size_limit>.
2955 * This should be caught and appropriately handled by caller.
2956 * <eslEINCOMPAT> if mx does not appeared to be created for this cm
2957 * <eslEINCOMPAT> on contract violation
2958 * <eslEMEM> on memory allocation error.
2959 */
2960 int
2961 cm_hb_shadow_mx_GrowTo(CM_t *cm, CM_HB_SHADOW_MX *mx, char *errbuf, CP9Bands_t *cp9b, int L, float size_limit)
2962 {
2963 int status;
2964 void *p;
2965 int v, jp;
2966 int64_t y_cur_size, k_cur_size = 0;
2967 int64_t y_ncells, k_ncells;
2968 int jbw;
2969 float Mb_needed; /* required size of matrix, given the bands */
2970 float Mb_alloc; /* allocated size of matrix, >= Mb_needed */
2971
2972 /* contract check, number of states (M) is something we don't change
2973 * so check this matrix has same number of 1st dim state ptrs that
2974 * cp9b has */
2975 if(cp9b == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_hb_shadow_mx_GrowTo() entered with cp9b == NULL.\n");
2976 if(cp9b->cm_M != mx->M) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_hb_shadow_mx_GrowTo() entered with mx->M: (%d) != cp9b->M (%d)\n", mx->M, cp9b->cm_M);
2977
2978 if((status = cm_hb_shadow_mx_SizeNeeded(cm, errbuf, cp9b, &y_ncells, &k_ncells, &Mb_needed)) != eslOK) return status;
2979 /*printf("HMM banded shadow matrix requested size: %.2f Mb\n", Mb_needed);*/
2980 ESL_DPRINTF2(("#DEBUG: HMM banded shadow matrix requested size: %.2f Mb\n", Mb_needed));
2981 if(Mb_needed > size_limit) ESL_FAIL(eslERANGE, errbuf, "requested HMM banded shadow DP mx of %.2f Mb > %.2f Mb limit.\nUse --mxsize, --maxtau or --tau.", Mb_needed, (float) size_limit);
2982
2983 /* check if we should free the matrix, because it is significantly bigger than we need */
2984 if((mx->size_Mb > (0.5 * size_limit)) && /* matrix is >= 0.5 * size of our limit (based on bands from previous sequence) */
2985 (mx->size_Mb > (1.25 * Mb_needed))) { /* matrix is at least 25% bigger than we need to process current sequence */
2986 free(mx->yshadow_mem);
2987 free(mx->kshadow_mem);
2988 mx->yshadow_mem = NULL;
2989 mx->kshadow_mem = NULL;
2990 mx->y_ncells_alloc = 0;
2991 mx->k_ncells_alloc = 0;
2992 }
2993
2994 /* must we realloc the full yshadow and kshadow matrices?
2995 * or can we get away with just jiggering the pointers, if
2996 * total required num cells is less than or equal to what
2997 * we already have alloc'ed?
2998 */
2999
3000 /* handle yshadow */
3001 if (y_ncells > mx->y_ncells_alloc) {
3002 ESL_RALLOC(mx->yshadow_mem, p, sizeof(char) * y_ncells);
3003 mx->y_ncells_alloc = y_ncells;
3004 Mb_alloc = Mb_needed;
3005 }
3006 else {
3007 /* mx->yshadow_mem remains as it is allocated, set Mb_alloc
3008 * accordingly (this is not just mx->size_Mb, because size of
3009 * pointer arrays may change, for example)
3010 */
3011 Mb_alloc = Mb_needed * 1000000.; /* convert to bytes */
3012 Mb_alloc -= ((float) (sizeof(char) * y_ncells));
3013 Mb_alloc += ((float) (sizeof(char) * mx->y_ncells_alloc));
3014 Mb_alloc *= 0.000001; /* convert to Mb */
3015 }
3016 mx->y_ncells_valid = y_ncells;
3017
3018 /* handle kshadow */
3019 if (k_ncells > mx->k_ncells_alloc) {
3020 ESL_RALLOC(mx->kshadow_mem, p, sizeof(int) * k_ncells);
3021 mx->k_ncells_alloc = k_ncells;
3022 }
3023 else {
3024 /* mx->kshadow_mem remains as it is allocated, update Mb_alloc */
3025 Mb_alloc *= 1000000.; /* convert to bytes */
3026 Mb_alloc -= ((float) (sizeof(int) * k_ncells));
3027 Mb_alloc += ((float) (sizeof(int) * mx->k_ncells_alloc));
3028 Mb_alloc *= 0.000001; /* convert to Mb */
3029 }
3030 mx->k_ncells_valid = k_ncells;
3031
3032 /* make sure each row is big enough */
3033 for(v = 0; v < mx->M; v++) {
3034 jbw = cp9b->jmax[v] - cp9b->jmin[v] + 1;
3035 if(jbw > mx->nrowsA[v]) {
3036 if(cm->sttype[v] == B_st) {
3037 if(mx->kshadow[v] == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_hb_shadow_mx_GrowTo() v is a B st %d, but mx->kshadow[v] == NULL.\n", v);
3038 if(mx->yshadow[v] != NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_hb_shadow_mx_GrowTo() v is a B st %d, but mx->yshadow[v] != NULL.\n", v);
3039 ESL_RALLOC(mx->kshadow[v], p, sizeof(int *) * jbw);
3040 mx->nrowsA[v] = jbw;
3041 }
3042 else {
3043 if(mx->kshadow[v] != NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_hb_shadow_mx_GrowTo() v is not a B st %d, but mx->kshadow[v] != NULL.\n", v);
3044 if(mx->yshadow[v] == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_hb_shadow_mx_GrowTo() v is not a B st %d, but mx->kshadow[v] == NULL.\n", v);
3045 ESL_RALLOC(mx->yshadow[v], p, sizeof(char *) * jbw);
3046 mx->nrowsA[v] = jbw;
3047 }
3048 }
3049 }
3050
3051 /* reset the pointers, we keep a tally of number of cells
3052 * we've seen in each matrix (y_cur_size and k_cur_size) as we go,
3053 * we could precalc it and store it for each v,j, but that
3054 * would be wasteful, as we'll only use the matrix configured
3055 * this way once, in a banded CYK run.
3056 */
3057 y_cur_size = k_cur_size = 0;
3058 for(v = 0; v < mx->M; v++) {
3059 if(cm->sttype[v] == B_st) {
3060 for(jp = 0; jp <= (cp9b->jmax[v] - cp9b->jmin[v]); jp++) {
3061 mx->kshadow[v][jp] = mx->kshadow_mem + k_cur_size;
3062 k_cur_size += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
3063 }
3064 }
3065 else {
3066 for(jp = 0; jp <= (cp9b->jmax[v] - cp9b->jmin[v]); jp++) {
3067 mx->yshadow[v][jp] = mx->yshadow_mem + y_cur_size;
3068 y_cur_size += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
3069 }
3070 }
3071 }
3072 ESL_DASSERT1((y_cur_size == mx->y_ncells_valid));
3073 ESL_DASSERT1((k_cur_size == mx->k_ncells_valid));
3074
3075 mx->cp9b = cp9b; /* just a reference */
3076
3077 /* now update L and size_Mb */
3078 mx->L = L; /* length of current seq we're valid for */
3079 mx->size_Mb = Mb_alloc;
3080
3081 return eslOK;
3082
3083 ERROR:
3084 return status;
3085 }
3086
3087 /* Function: cm_hb_shadow_mx_Destroy()
3088 * Synopsis: Frees a DP matrix.
3089 * Incept: EPN, Fri Oct 26 09:04:04 2007
3090 *
3091 * Purpose: Frees a <CM_HB_SHADOW_MX>.
3092 *
3093 * Returns: (void)
3094 */
3095 void
3096 cm_hb_shadow_mx_Destroy(CM_HB_SHADOW_MX *mx)
3097 {
3098 if (mx == NULL) return;
3099 int v;
3100
3101 if (mx->yshadow != NULL) {
3102 for (v = 0; v < mx->M; v++)
3103 if(mx->yshadow[v] != NULL) free(mx->yshadow[v]);
3104 }
3105 free(mx->yshadow);
3106
3107 if (mx->kshadow != NULL) {
3108 for (v = 0; v < mx->M; v++)
3109 if(mx->kshadow[v] != NULL) free(mx->kshadow[v]);
3110 }
3111 free(mx->kshadow);
3112
3113 if (mx->nrowsA != NULL) free(mx->nrowsA);
3114 if (mx->yshadow_mem != NULL) free(mx->yshadow_mem);
3115 if (mx->kshadow_mem != NULL) free(mx->kshadow_mem);
3116 free(mx);
3117 return;
3118 }
3119
3120 /* Function: cm_hb_shadow_mx_Dump()
3121 * Synopsis: Dump a DP matrix to a stream, for diagnostics.
3122 * Incept: EPN, Fri Oct 26 09:04:46 2007
3123 *
3124 * Purpose: Dump matrix <mx> to stream <fp> for diagnostics.
3125 */
3126 int
3127 cm_hb_shadow_mx_Dump(FILE *ofp, CM_t *cm, CM_HB_SHADOW_MX *mx, int print_mx)
3128 {
3129 int v, jp, j, dp, d;
3130
3131 fprintf(ofp, "M: %d\nnbifs: %d\nL: %d\ny_ncells_alloc: %" PRId64 "\ny_ncells_valid: %" PRId64 "\nk_ncells_alloc: %" PRId64 "\nk_ncells_valid: %" PRId64 "\n", mx->M, mx->B, mx->L, mx->y_ncells_alloc, mx->y_ncells_valid, mx->k_ncells_alloc, mx->k_ncells_valid);
3132
3133 if(print_mx) {
3134 /* yshadow/kshadow matrix data */
3135 for (v = 0; v < mx->M; v++) {
3136 if(cm->sttype[v] == B_st) {
3137 for(jp = 0; jp <= mx->cp9b->jmax[v] - mx->cp9b->jmin[v]; jp++) {
3138 j = jp + mx->cp9b->jmin[v];
3139 for(dp = 0; dp <= mx->cp9b->hdmax[v][jp] - mx->cp9b->hdmin[v][jp]; dp++) {
3140 d = dp + mx->cp9b->hdmin[v][jp];
3141 fprintf(ofp, "kshad[v:%5d][j:%5d][d:%5d] %8d\n", v, j, d, mx->kshadow[v][jp][dp]);
3142 }
3143 fprintf(ofp, "\n");
3144 }
3145 fprintf(ofp, "\n\n");
3146 }
3147 else {
3148 for(jp = 0; jp <= mx->cp9b->jmax[v] - mx->cp9b->jmin[v]; jp++) {
3149 j = jp + mx->cp9b->jmin[v];
3150 for(dp = 0; dp <= mx->cp9b->hdmax[v][jp] - mx->cp9b->hdmin[v][jp]; dp++) {
3151 d = dp + mx->cp9b->hdmin[v][jp];
3152 fprintf(ofp, "yshad[v:%5d][j:%5d][d:%5d] %8c\n", v, j, d, mx->yshadow[v][jp][dp]);
3153 }
3154 fprintf(ofp, "\n");
3155 }
3156 fprintf(ofp, "\n\n");
3157 }
3158 }
3159 }
3160 return eslOK;
3161 }
3162
3163 /* Function: cm_hb_shadow_mx_SizeNeeded()
3164 * Incept: EPN, Fri Aug 12 04:19:36 2011
3165 *
3166 * Purpose: Given a model, and a CP9_bands_t object
3167 * with pre-calced bands for a target, determine the number
3168 * of cells and total size in Mb required for the matrix
3169 * for the target given the bands.
3170 *
3171 * Return number of yshadow (char) cells required in
3172 * <ret_ny_cells> and number of kshadow (int) cells
3173 * required in <ret_nk_cells) and size of required
3174 * matrix in Mb in <ret_Mb>.
3175 *
3176 * Args: cm - the CM the matrix is for
3177 * errbuf - char buffer for reporting errors
3178 * cp9b - the bands for the current target sequence
3179 * ret_ny_cells - RETURN: number of required char cells (yshadow)
3180 * ret_nk_cells - RETURN: number of required int cells (kshadow)
3181 * ret_Mb - RETURN: required size of matrix in Mb
3182 *
3183 * Returns: <eslOK> on success
3184 *
3185 */
3186 int
3187 cm_hb_shadow_mx_SizeNeeded(CM_t *cm, char *errbuf, CP9Bands_t *cp9b, int64_t *ret_ny_cells, int64_t *ret_nk_cells, float *ret_Mb)
3188 {
3189 int v, jp;
3190 int64_t y_ncells, k_ncells;
3191 int jbw;
3192 float Mb_needed;
3193
3194 /* contract check */
3195 if(cp9b == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_hb_shadow_mx_SizeNeeded() entered with cp9b == NULL.\n");
3196
3197 y_ncells = k_ncells = 0;
3198
3199 Mb_needed = (float)
3200 (sizeof(CM_HB_SHADOW_MX) +
3201 (cp9b->cm_M * sizeof(char **)) + /* mx->yshadow[] ptrs */
3202 (cp9b->cm_M * sizeof(int **)) + /* mx->kshadow[] ptrs */
3203 (cp9b->cm_M * sizeof(int))); /* mx->nrowsA */
3204
3205 for(v = 0; v < cp9b->cm_M; v++) {
3206 jbw = cp9b->jmax[v] - cp9b->jmin[v];
3207 if(cm->sttype[v] == B_st) {
3208 Mb_needed += (float) (sizeof(int *) * (jbw+1)); /* mx->kshadow[v][] ptrs */
3209 for(jp = 0; jp <= jbw; jp++)
3210 k_ncells += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
3211 }
3212 else {
3213 Mb_needed += (float) (sizeof(char *) * (jbw+1)); /* mx->yshadow[v][] ptrs */
3214 for(jp = 0; jp <= jbw; jp++)
3215 y_ncells += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
3216 }
3217 }
3218 Mb_needed += sizeof(char) * y_ncells; /* mx->yshadow_mem */
3219 Mb_needed += sizeof(int) * k_ncells; /* mx->kshadow_mem */
3220 Mb_needed *= 0.000001; /* convert to megabytes */
3221
3222 if(ret_ny_cells != NULL) *ret_ny_cells = y_ncells;
3223 if(ret_nk_cells != NULL) *ret_nk_cells = k_ncells;
3224 if(ret_Mb != NULL) *ret_Mb = Mb_needed;
3225
3226 return eslOK;
3227
3228 }
3229
3230 /*****************************************************************
3231 * 8. CM_TR_HB_SHADOW_MX data structure functions,
3232 * HMM banded shadow matrix for tracing back HMM banded CM parses.
3233 *****************************************************************/
3234
3235 /* Function: cm_tr_hb_shadow_mx_Create()
3236 * Incept: EPN, Wed Sep 7 15:21:02 2011
3237 *
3238 * Purpose: Allocate a reusable, resizeable <CM_TR_HB_SHADOW_MX> for a CM <cm>
3239 * given a CP9Bands_t object that defines the bands. The CM
3240 * is needed so we know which decks need to be int's (BIF_B states)
3241 * and which need to be char's (all other states).
3242 *
3243 * We've set this up so it should be easy to allocate
3244 * aligned memory, though we're not doing this yet.
3245 *
3246 * Returns: a pointer to the new <CM_HB_SHADOW_MX>.
3247 *
3248 * Throws: <NULL> on allocation error.
3249 */
3250 CM_TR_HB_SHADOW_MX *
3251 cm_tr_hb_shadow_mx_Create(CM_t *cm)
3252 {
3253 int status;
3254 CM_TR_HB_SHADOW_MX *mx = NULL;
3255 int v, b;
3256 int M = cm->M;
3257 int allocL = 1;
3258 int allocW = 1;
3259 int B = CMCountNodetype(cm, BIF_nd);
3260
3261 /* level 1: the structure itself */
3262 ESL_ALLOC(mx, sizeof(CM_TR_HB_SHADOW_MX));
3263 mx->Jyshadow = NULL;
3264 mx->Jyshadow_mem = NULL;
3265 mx->Lyshadow = NULL;
3266 mx->Lyshadow_mem = NULL;
3267 mx->Ryshadow = NULL;
3268 mx->Ryshadow_mem = NULL;
3269
3270 mx->Jkshadow = NULL;
3271 mx->Jkshadow_mem = NULL;
3272 mx->Lkshadow = NULL;
3273 mx->Lkshadow_mem = NULL;
3274 mx->Rkshadow = NULL;
3275 mx->Rkshadow_mem = NULL;
3276 mx->Tkshadow = NULL;
3277 mx->Tkshadow_mem = NULL;
3278
3279 mx->Lkmode = NULL;
3280 mx->Lkmode_mem = NULL;
3281 mx->Rkmode = NULL;
3282 mx->Rkmode_mem = NULL;
3283
3284 mx->cp9b = NULL;
3285
3286 /* level 2: deck (state) pointers, 0.1..M-1, M (EL deck) is irrelevant for the
3287 * shadow matrix.
3288 */
3289 ESL_ALLOC(mx->Jyshadow, sizeof(char **) * M);
3290 ESL_ALLOC(mx->Lyshadow, sizeof(char **) * M);
3291 ESL_ALLOC(mx->Ryshadow, sizeof(char **) * M);
3292
3293 ESL_ALLOC(mx->Jkshadow, sizeof(int **) * M);
3294 ESL_ALLOC(mx->Lkshadow, sizeof(int **) * M);
3295 ESL_ALLOC(mx->Rkshadow, sizeof(int **) * M);
3296 ESL_ALLOC(mx->Tkshadow, sizeof(int **) * M);
3297
3298 ESL_ALLOC(mx->Lkmode, sizeof(char **) * M);
3299 ESL_ALLOC(mx->Rkmode, sizeof(char **) * M);
3300
3301 /* level 3: matrix cell memory, when creating only allocate 1 cell per state, for j = 0, d = 0 */
3302 ESL_ALLOC(mx->Jyshadow_mem, (sizeof(char) * (int64_t) (M-B) * (int64_t) allocL * (int64_t) allocW));
3303 ESL_ALLOC(mx->Lyshadow_mem, (sizeof(char) * (int64_t) (M-B) * (int64_t) allocL * (int64_t) allocW));
3304 ESL_ALLOC(mx->Ryshadow_mem, (sizeof(char) * (int64_t) (M-B) * (int64_t) allocL * (int64_t) allocW));
3305
3306 ESL_ALLOC(mx->Jkshadow_mem, sizeof(int) * ESL_MAX(1, (int64_t) B * (int64_t) allocL * (int64_t) allocW)); // avoid 0 malloc
3307 ESL_ALLOC(mx->Lkshadow_mem, sizeof(int) * ESL_MAX(1, (int64_t) B * (int64_t) allocL * (int64_t) allocW));
3308 ESL_ALLOC(mx->Rkshadow_mem, sizeof(int) * ESL_MAX(1, (int64_t) B * (int64_t) allocL * (int64_t) allocW));
3309 ESL_ALLOC(mx->Tkshadow_mem, sizeof(int) * ESL_MAX(1, (int64_t) B * (int64_t) allocL * (int64_t) allocW));
3310
3311 ESL_ALLOC(mx->Lkmode_mem, sizeof(char) * ESL_MAX(1, (int64_t) B * (int64_t) allocL * (int64_t) allocW));
3312 ESL_ALLOC(mx->Rkmode_mem, sizeof(char) * ESL_MAX(1, (int64_t) B * (int64_t) allocL * (int64_t) allocW));
3313
3314 ESL_ALLOC(mx->JnrowsA, sizeof(int) * M);
3315 ESL_ALLOC(mx->LnrowsA, sizeof(int) * M);
3316 ESL_ALLOC(mx->RnrowsA, sizeof(int) * M);
3317 ESL_ALLOC(mx->TnrowsA, sizeof(int) * M);
3318
3319 b = 0;
3320 for (v = 0; v < M; v++) {
3321 if(cm->sttype[v] == B_st) {
3322 ESL_ALLOC(mx->Jkshadow[v], sizeof(int *) * (allocL));
3323 ESL_ALLOC(mx->Lkshadow[v], sizeof(int *) * (allocL));
3324 ESL_ALLOC(mx->Rkshadow[v], sizeof(int *) * (allocL));
3325 ESL_ALLOC(mx->Tkshadow[v], sizeof(int *) * (allocL));
3326
3327 ESL_ALLOC(mx->Lkmode[v], sizeof(char *) * (allocL));
3328 ESL_ALLOC(mx->Rkmode[v], sizeof(char *) * (allocL));
3329
3330 mx->Jkshadow[v][0] = mx->Jkshadow_mem + (int64_t) b * (int64_t) allocL * (int64_t) allocW;
3331 mx->Lkshadow[v][0] = mx->Lkshadow_mem + (int64_t) b * (int64_t) allocL * (int64_t) allocW;
3332 mx->Rkshadow[v][0] = mx->Rkshadow_mem + (int64_t) b * (int64_t) allocL * (int64_t) allocW;
3333 mx->Tkshadow[v][0] = mx->Tkshadow_mem + (int64_t) b * (int64_t) allocL * (int64_t) allocW;
3334
3335 mx->Lkmode[v][0] = mx->Lkmode_mem + (int64_t) b * (int64_t) allocL * (int64_t) allocW;
3336 mx->Rkmode[v][0] = mx->Rkmode_mem + (int64_t) b * (int64_t) allocL * (int64_t) allocW;
3337
3338 mx->Jyshadow[v] = NULL;
3339 mx->Lyshadow[v] = NULL;
3340 mx->Ryshadow[v] = NULL;
3341 b++;
3342 }
3343 else {
3344 ESL_ALLOC(mx->Jyshadow[v], sizeof(char *) * (allocL));
3345 ESL_ALLOC(mx->Lyshadow[v], sizeof(char *) * (allocL));
3346 ESL_ALLOC(mx->Ryshadow[v], sizeof(char *) * (allocL));
3347
3348 mx->Jyshadow[v][0] = mx->Jyshadow_mem + (int64_t) (v-b) * (int64_t) allocL * (int64_t) allocW;
3349 mx->Lyshadow[v][0] = mx->Lyshadow_mem + (int64_t) (v-b) * (int64_t) allocL * (int64_t) allocW;
3350 mx->Ryshadow[v][0] = mx->Ryshadow_mem + (int64_t) (v-b) * (int64_t) allocL * (int64_t) allocW;
3351 mx->Jkshadow[v] = NULL;
3352 mx->Lkshadow[v] = NULL;
3353 mx->Rkshadow[v] = NULL;
3354 mx->Tkshadow[v] = NULL;
3355
3356 mx->Lkmode[v] = NULL;
3357 mx->Rkmode[v] = NULL;
3358 }
3359 mx->JnrowsA[v] = allocL;
3360 mx->LnrowsA[v] = allocL;
3361 mx->RnrowsA[v] = allocL;
3362 mx->TnrowsA[v] = allocL;
3363 }
3364 mx->M = M;
3365 mx->B = B;
3366 mx->Jy_ncells_alloc = (int64_t) (M-B) * (int64_t) allocL * (int64_t) allocW;
3367 mx->Ly_ncells_alloc = (int64_t) (M-B) * (int64_t) allocL * (int64_t) allocW;
3368 mx->Ry_ncells_alloc = (int64_t) (M-B) * (int64_t) allocL * (int64_t) allocW;
3369 mx->Jy_ncells_valid = 0;
3370 mx->Ly_ncells_valid = 0;
3371 mx->Ry_ncells_valid = 0;
3372 mx->Jk_ncells_alloc = (int64_t) B * (int64_t) allocL * (int64_t) allocW;
3373 mx->Lk_ncells_alloc = (int64_t) B * (int64_t) allocL * (int64_t) allocW;
3374 mx->Rk_ncells_alloc = (int64_t) B * (int64_t) allocL * (int64_t) allocW;
3375 mx->Tk_ncells_alloc = (int64_t) B * (int64_t) allocL * (int64_t) allocW;
3376 mx->Jk_ncells_valid = 0;
3377 mx->Rk_ncells_valid = 0;
3378 mx->Tk_ncells_valid = 0;
3379 mx->L = allocL; /* allocL = 1 */
3380
3381 /* calculate size, these are in order of when they were allocated */
3382 mx->size_Mb = (float)
3383 (sizeof(CM_TR_HB_SHADOW_MX) +
3384 (3 * (mx->M) * sizeof(char **)) + /* mx->{J,L,R}yshadow[] ptrs */
3385 (4 * (mx->M) * sizeof(int **)) + /* mx->{J,L,R,T}kshadow[] ptrs */
3386 mx->Jy_ncells_alloc * sizeof(char) + /* mx->Jyshadow_mem */
3387 mx->Ly_ncells_alloc * sizeof(char) + /* mx->Lyshadow_mem */
3388 mx->Ry_ncells_alloc * sizeof(char) + /* mx->Ryshadow_mem */
3389 mx->Jk_ncells_alloc * sizeof(int) + /* mx->Jkshadow_mem */
3390 mx->Lk_ncells_alloc * sizeof(int) + /* mx->Lkshadow_mem */
3391 mx->Rk_ncells_alloc * sizeof(int) + /* mx->Rkshadow_mem */
3392 mx->Tk_ncells_alloc * sizeof(int) + /* mx->Tkshadow_mem */
3393 mx->Lk_ncells_alloc * sizeof(char) + /* mx->Lkmode_mem */
3394 mx->Rk_ncells_alloc * sizeof(char) + /* mx->Rkmode_mem */
3395 (4 * (int64_t) (mx->M) * sizeof(int)) + /* mx->{J,L,R,T}nrowsA */
3396 (4 * (int64_t) mx->B * (int64_t) allocL * sizeof(int *)) + /* mx->{J,L,R,T}kshadow[v][] ptrs */
3397 (3 * (int64_t) (mx->M - mx->B) * (int64_t) allocL * sizeof(char *))); /* mx->{J,L,R,T}kshadow[v][] ptrs */
3398 mx->size_Mb *= 0.000001; /* convert to Mb */
3399
3400 return mx;
3401
3402 ERROR:
3403 if (mx != NULL) cm_tr_hb_shadow_mx_Destroy(mx);
3404 return NULL;
3405 }
3406
3407 /* Function: cm_tr_hb_shadow_mx_GrowTo()
3408 * Incept: EPN, Wed Sep 7 12:54:32 2011
3409 *
3410 * Purpose: Assures that a CM_TR_HB_SHADOW_MX <mx> is allocated
3411 * for a model of exactly <mx->M> states and required number of
3412 * total cells. Determines new required size from
3413 * the CP9Bands_t object passed in, and reallocates if
3414 * necessary.
3415 *
3416 * Checks that the matrix has been created for the current CM.
3417 * Check is that mx->yshadow[v] == NULL when v is a B_st and
3418 * mx->kshadow[v] != NULL when v is a B_st.
3419 *
3420 * Checks to make sure desired matrix isn't too big (see throws).
3421 *
3422 * Args: cm - the CM the matrix is for
3423 * mx - the matrix to grow
3424 * errbuf - char buffer for reporting errors
3425 * cp9b - the bands for the current target sequence
3426 * L - the length of the current target sequence we're aligning
3427 * size_limit- max number of Mb for DP matrix, if matrix is bigger -> return eslERANGE
3428 *
3429 * Returns: <eslOK> on success, and <mx> may be reallocated upon
3430 * return; any data that may have been in <mx> must be
3431 * assumed to be invalidated.
3432 *
3433 * Throws: <eslERANGE> if required size to grow to exceeds <size_limit>.
3434 * This should be caught and appropriately handled by caller.
3435 * <eslEINCOMPAT> if mx does not appeared to be created for this cm
3436 * <eslEINCOMPAT> on contract violation
3437 * <eslEMEM> on memory allocation error.
3438 */
3439 int
3440 cm_tr_hb_shadow_mx_GrowTo(CM_t *cm, CM_TR_HB_SHADOW_MX *mx, char *errbuf, CP9Bands_t *cp9b, int L, float size_limit)
3441 {
3442 int status;
3443 void *p;
3444 int v, jp;
3445 int64_t Jy_cur_size;
3446 int64_t Ly_cur_size;
3447 int64_t Ry_cur_size;
3448 int64_t Jk_cur_size;
3449 int64_t Lk_cur_size;
3450 int64_t Rk_cur_size;
3451 int64_t Tk_cur_size;
3452 int64_t Jy_ncells;
3453 int64_t Ly_ncells;
3454 int64_t Ry_ncells;
3455 int64_t Jk_ncells;
3456 int64_t Lk_ncells;
3457 int64_t Rk_ncells;
3458 int64_t Tk_ncells;
3459 int jbw;
3460 float Mb_needed; /* required size of matrix, given the bands */
3461 float Mb_alloc; /* allocated size of matrix, >= Mb_needed */
3462 int realloced_Jy; /* did we reallocate mx->Jyshadow_mem? */
3463 int realloced_Ly; /* did we reallocate mx->Lyshadow_mem? */
3464 int realloced_Ry; /* did we reallocate mx->Ryshadow_mem? */
3465 int realloced_Jk; /* did we reallocate mx->Jkshadow_mem? */
3466 int realloced_Lk; /* did we reallocate mx->Lkshadow_mem & mx->Lkmode_mem? */
3467 int realloced_Rk; /* did we reallocate mx->Rkshadow_mem & mx->Rkmode_mem? */
3468 int realloced_Tk; /* did we reallocate mx->Tkshadow_mem? */
3469
3470 /* contract check, number of states (M) is something we don't change
3471 * so check this matrix has same number of 1st dim state ptrs that
3472 * cp9b has */
3473 if(cp9b == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_tr_hb_shadow_mx_GrowTo() entered with cp9b == NULL.\n");
3474 if(cp9b->cm_M != mx->M) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_tr_hb_shadow_mx_GrowTo() entered with mx->M: (%d) != cp9b->M (%d)\n", mx->M, cp9b->cm_M);
3475
3476 if((status = cm_tr_hb_shadow_mx_SizeNeeded(cm, errbuf, cp9b, &Jy_ncells, &Ly_ncells, &Ry_ncells, &Jk_ncells, &Lk_ncells, &Rk_ncells, &Tk_ncells, &Mb_needed)) != eslOK) return status;
3477 /*printf("HMM banded Tr shadow matrix requested size: %.2f Mb\n", Mb_needed);*/
3478 ESL_DPRINTF2(("#DEBUG: HMM banded Tr shadow matrix requested size: %.2f Mb\n", Mb_needed));
3479 if(Mb_needed > size_limit) ESL_FAIL(eslERANGE, errbuf, "requested HMM banded Tr shadow DP mx of %.2f Mb > %.2f Mb limit.\nUse --mxsize, --maxtau or --tau.", Mb_needed, (float) size_limit);
3480
3481 /* check if we should free the matrix */
3482 if((mx->size_Mb > (0.5 * size_limit)) && /* matrix is >= 0.5 * size of our limit (based on bands from previous sequence) */
3483 (mx->size_Mb > (1.25 * Mb_needed))) { /* matrix is at least 25% bigger than we need to process current sequence */
3484 free(mx->Jyshadow_mem);
3485 free(mx->Lyshadow_mem);
3486 free(mx->Ryshadow_mem);
3487 free(mx->Jkshadow_mem);
3488 free(mx->Lkshadow_mem);
3489 free(mx->Rkshadow_mem);
3490 free(mx->Tkshadow_mem);
3491 free(mx->Lkmode_mem);
3492 free(mx->Rkmode_mem);
3493 mx->Jyshadow_mem = NULL;
3494 mx->Lyshadow_mem = NULL;
3495 mx->Ryshadow_mem = NULL;
3496 mx->Jkshadow_mem = NULL;
3497 mx->Lkshadow_mem = NULL;
3498 mx->Rkshadow_mem = NULL;
3499 mx->Tkshadow_mem = NULL;
3500 mx->Lkmode_mem = NULL;
3501 mx->Rkmode_mem = NULL;
3502 mx->Jy_ncells_alloc = 0;
3503 mx->Ly_ncells_alloc = 0;
3504 mx->Ry_ncells_alloc = 0;
3505 mx->Jk_ncells_alloc = 0;
3506 mx->Lk_ncells_alloc = 0;
3507 mx->Rk_ncells_alloc = 0;
3508 mx->Tk_ncells_alloc = 0;
3509 }
3510
3511 /* must we realloc the full {J,L,R}yshadow and {J,L,R,T}kshadow matrices?
3512 * or can we get away with just jiggering the pointers, if
3513 * total required num cells is less than or equal to what
3514 * we already have alloc'ed?
3515 */
3516 realloced_Jy = realloced_Ly = realloced_Ry = FALSE;
3517 realloced_Jk = realloced_Lk = realloced_Rk = realloced_Tk = FALSE;
3518 if (Jy_ncells > mx->Jy_ncells_alloc) {
3519 ESL_RALLOC(mx->Jyshadow_mem, p, sizeof(char) * Jy_ncells);
3520 mx->Jy_ncells_alloc = Jy_ncells;
3521 realloced_Jy = TRUE;
3522 }
3523 if (Ly_ncells > mx->Ly_ncells_alloc) {
3524 ESL_RALLOC(mx->Lyshadow_mem, p, sizeof(char) * Ly_ncells);
3525 mx->Ly_ncells_alloc = Ly_ncells;
3526 realloced_Ly = TRUE;
3527 }
3528 if (Ry_ncells > mx->Ry_ncells_alloc) {
3529 ESL_RALLOC(mx->Ryshadow_mem, p, sizeof(char) * Ry_ncells);
3530 mx->Ry_ncells_alloc = Ry_ncells;
3531 realloced_Ry = TRUE;
3532 }
3533 if (Jk_ncells > mx->Jk_ncells_alloc) {
3534 ESL_RALLOC(mx->Jkshadow_mem, p, sizeof(int) * Jk_ncells);
3535 mx->Jk_ncells_alloc = Jk_ncells;
3536 realloced_Jk = TRUE;
3537 }
3538 if (Lk_ncells > mx->Lk_ncells_alloc) {
3539 ESL_RALLOC(mx->Lkshadow_mem, p, sizeof(int) * Lk_ncells);
3540 ESL_RALLOC(mx->Lkmode_mem, p, sizeof(char) * Lk_ncells);
3541 mx->Lk_ncells_alloc = Lk_ncells;
3542 realloced_Lk = TRUE;
3543 }
3544 if (Rk_ncells > mx->Rk_ncells_alloc) {
3545 ESL_RALLOC(mx->Rkshadow_mem, p, sizeof(int) * Rk_ncells);
3546 ESL_RALLOC(mx->Rkmode_mem, p, sizeof(char) * Rk_ncells);
3547 mx->Rk_ncells_alloc = Rk_ncells;
3548 realloced_Rk = TRUE;
3549 }
3550 if (Tk_ncells > mx->Tk_ncells_alloc) {
3551 ESL_RALLOC(mx->Tkshadow_mem, p, sizeof(int) * Tk_ncells);
3552 mx->Tk_ncells_alloc = Tk_ncells;
3553 realloced_Tk = TRUE;
3554 }
3555 /* Determine the size of our matrix based on the size it needed to be (Mb_needed).
3556 * This is tricky, for each matrix not reallocated, we have to adjust Mb_needed
3557 * so it uses previously allocated size of that matrix.
3558 */
3559 Mb_alloc = Mb_needed * 1000000; /* convert to bytes */
3560 if(! realloced_Jy) {
3561 Mb_alloc -= (float) (sizeof(char) * Jy_ncells);
3562 Mb_alloc += (float) (sizeof(char) * mx->Jy_ncells_alloc);
3563 }
3564 if(! realloced_Ly) {
3565 Mb_alloc -= (float) (sizeof(char) * Ly_ncells);
3566 Mb_alloc += (float) (sizeof(char) * mx->Ly_ncells_alloc);
3567 }
3568 if(! realloced_Ry) {
3569 Mb_alloc -= (float) (sizeof(char) * Ry_ncells);
3570 Mb_alloc += (float) (sizeof(char) * mx->Ry_ncells_alloc);
3571 }
3572 if(! realloced_Jk) {
3573 Mb_alloc -= (float) (sizeof(int) * Jk_ncells);
3574 Mb_alloc += (float) (sizeof(int) * mx->Jk_ncells_alloc);
3575 }
3576 if(! realloced_Lk) {
3577 Mb_alloc -= (float) (sizeof(int) * Lk_ncells);
3578 Mb_alloc += (float) (sizeof(int) * mx->Lk_ncells_alloc);
3579 }
3580 if(! realloced_Rk) {
3581 Mb_alloc -= (float) (sizeof(int) * Rk_ncells);
3582 Mb_alloc += (float) (sizeof(int) * mx->Rk_ncells_alloc);
3583 }
3584 if(! realloced_Tk) {
3585 Mb_alloc -= (float) (sizeof(int) * Tk_ncells);
3586 Mb_alloc += (float) (sizeof(int) * mx->Tk_ncells_alloc);
3587 }
3588 /* note if we didn't reallocate any of the four matrices, Mb_alloc == Mb_needed */
3589 Mb_alloc *= 0.000001; /* convert to Mb */
3590
3591 mx->Jy_ncells_valid = Jy_ncells;
3592 mx->Ly_ncells_valid = Ly_ncells;
3593 mx->Ry_ncells_valid = Ry_ncells;
3594 mx->Jk_ncells_valid = Jk_ncells;
3595 mx->Lk_ncells_valid = Lk_ncells;
3596 mx->Rk_ncells_valid = Rk_ncells;
3597 mx->Tk_ncells_valid = Tk_ncells;
3598
3599 /* make sure each row is big enough */
3600 for(v = 0; v < mx->M; v++) {
3601 jbw = cp9b->jmax[v] - cp9b->jmin[v] + 1;
3602
3603 if(cm->sttype[v] != B_st) {
3604 if(cp9b->Jvalid[v]) {
3605 if(jbw > mx->JnrowsA[v]) {
3606 if(mx->Jyshadow[v] != NULL) ESL_RALLOC(mx->Jyshadow[v], p, sizeof(char *) * jbw);
3607 else ESL_ALLOC (mx->Jyshadow[v], sizeof(char *) * jbw);
3608 mx->JnrowsA[v] = jbw;
3609 }
3610 }
3611 else { /* cp9b->Jvalid[v] is FALSE */
3612 if(mx->Jyshadow[v] != NULL) free(mx->Jyshadow[v]);
3613 mx->Jyshadow[v] = NULL;
3614 mx->JnrowsA[v] = 0;
3615 }
3616 if(cp9b->Lvalid[v]) {
3617 if(jbw > mx->LnrowsA[v]) {
3618 if(mx->Lyshadow[v] != NULL) ESL_RALLOC(mx->Lyshadow[v], p, sizeof(char *) * jbw);
3619 else ESL_ALLOC (mx->Lyshadow[v], sizeof(char *) * jbw);
3620 mx->LnrowsA[v] = jbw;
3621 }
3622 }
3623 else { /* cp9b->Lvalid[v] is FALSE */
3624 if(mx->Lyshadow[v] != NULL) free(mx->Lyshadow[v]);
3625 mx->Lyshadow[v] = NULL;
3626 mx->LnrowsA[v] = 0;
3627 }
3628 if(cp9b->Rvalid[v]) {
3629 if(jbw > mx->RnrowsA[v]) {
3630 if(mx->Ryshadow[v] != NULL) ESL_RALLOC(mx->Ryshadow[v], p, sizeof(char *) * jbw);
3631 else ESL_ALLOC (mx->Ryshadow[v], sizeof(char *) * jbw);
3632 mx->RnrowsA[v] = jbw;
3633 }
3634 }
3635 else { /* cp9b->Rvalid[v] is FALSE */
3636 if(mx->Ryshadow[v] != NULL) free(mx->Ryshadow[v]);
3637 mx->Ryshadow[v] = NULL;
3638 mx->RnrowsA[v] = 0;
3639 }
3640 }
3641 else { /* cm->sttype[v] == B_st */
3642 if(cp9b->Jvalid[v]) {
3643 if(jbw > mx->JnrowsA[v]) {
3644 if(mx->Jkshadow[v] != NULL) ESL_RALLOC(mx->Jkshadow[v], p, sizeof(int *) * jbw);
3645 else ESL_ALLOC (mx->Jkshadow[v], sizeof(int *) * jbw);
3646 mx->JnrowsA[v] = jbw;
3647 }
3648 }
3649 else { /* cp9b->Jvalid[v] is FALSE */
3650 if(mx->Jkshadow[v] != NULL) free(mx->Jkshadow[v]);
3651 mx->Jkshadow[v] = NULL;
3652 mx->JnrowsA[v] = 0;
3653 }
3654 if(cp9b->Lvalid[v]) {
3655 if(jbw > mx->LnrowsA[v]) {
3656 if(mx->Lkshadow[v] != NULL) ESL_RALLOC(mx->Lkshadow[v], p, sizeof(int *) * jbw);
3657 else ESL_ALLOC (mx->Lkshadow[v], sizeof(int *) * jbw);
3658 if(mx->Lkmode[v] != NULL) ESL_RALLOC(mx->Lkmode[v], p, sizeof(char *) * jbw);
3659 else ESL_ALLOC (mx->Lkmode[v], sizeof(char *) * jbw);
3660 mx->LnrowsA[v] = jbw;
3661 }
3662 }
3663 else { /* cp9b->Lvalid[v] is FALSE */
3664 if(mx->Lkshadow[v] != NULL) free(mx->Lkshadow[v]);
3665 if(mx->Lkmode[v] != NULL) free(mx->Lkmode[v]);
3666 mx->Lkshadow[v] = NULL;
3667 mx->Lkmode[v] = NULL;
3668 mx->LnrowsA[v] = 0;
3669 }
3670 if(cp9b->Rvalid[v]) {
3671 if(jbw > mx->RnrowsA[v]) {
3672 if(mx->Rkshadow[v] != NULL) ESL_RALLOC(mx->Rkshadow[v], p, sizeof(int *) * jbw);
3673 else ESL_ALLOC (mx->Rkshadow[v], sizeof(int *) * jbw);
3674 if(mx->Rkmode[v] != NULL) ESL_RALLOC(mx->Rkmode[v], p, sizeof(char *) * jbw);
3675 else ESL_ALLOC (mx->Rkmode[v], sizeof(char *) * jbw);
3676 mx->RnrowsA[v] = jbw;
3677 }
3678 }
3679 else { /* cp9b->Rvalid[v] is FALSE */
3680 if(mx->Rkshadow[v] != NULL) free(mx->Rkshadow[v]);
3681 if(mx->Rkmode[v] != NULL) free(mx->Rkmode[v]);
3682 mx->Rkshadow[v] = NULL;
3683 mx->Rkmode[v] = NULL;
3684 mx->RnrowsA[v] = 0;
3685 }
3686 if(cp9b->Tvalid[v]) {
3687 if(jbw > mx->TnrowsA[v]) {
3688 if(mx->Tkshadow[v] != NULL) ESL_RALLOC(mx->Tkshadow[v], p, sizeof(int *) * jbw);
3689 else ESL_ALLOC (mx->Tkshadow[v], sizeof(int *) * jbw);
3690 mx->TnrowsA[v] = jbw;
3691 }
3692 }
3693 else { /* cp9b->Tvalid[v] is FALSE */
3694 if(mx->Tkshadow[v] != NULL) free(mx->Tkshadow[v]);
3695 mx->Tkshadow[v] = NULL;
3696 mx->TnrowsA[v] = 0;
3697 }
3698 }
3699 }
3700
3701 /* reset the pointers, we keep a tally of number of cells
3702 * we've seen in each matrix (y_cur_size and k_cur_size) as we go,
3703 * we could precalc it and store it for each v,j, but that
3704 * would be wasteful, as we'll only use the matrix configured
3705 * this way once, in a banded CYK run.
3706 */
3707 Jy_cur_size = 0;
3708 Ly_cur_size = 0;
3709 Ry_cur_size = 0;
3710 Jk_cur_size = 0;
3711 Lk_cur_size = 0;
3712 Rk_cur_size = 0;
3713 Tk_cur_size = 0;
3714 for(v = 0; v < mx->M; v++) {
3715 if(cm->sttype[v] != B_st) {
3716 if(mx->Jyshadow[v] != NULL) {
3717 for(jp = 0; jp <= (cp9b->jmax[v] - cp9b->jmin[v]); jp++) {
3718 mx->Jyshadow[v][jp] = mx->Jyshadow_mem + Jy_cur_size;
3719 Jy_cur_size += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
3720 }
3721 }
3722 if(mx->Lyshadow[v] != NULL) {
3723 for(jp = 0; jp <= (cp9b->jmax[v] - cp9b->jmin[v]); jp++) {
3724 mx->Lyshadow[v][jp] = mx->Lyshadow_mem + Ly_cur_size;
3725 Ly_cur_size += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
3726 }
3727 }
3728 if(mx->Ryshadow[v] != NULL) {
3729 for(jp = 0; jp <= (cp9b->jmax[v] - cp9b->jmin[v]); jp++) {
3730 mx->Ryshadow[v][jp] = mx->Ryshadow_mem + Ry_cur_size;
3731 Ry_cur_size += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
3732 }
3733 }
3734 }
3735 else { /* cm->sttype[v] == B_st */
3736 if(mx->Jkshadow[v] != NULL) {
3737 for(jp = 0; jp <= (cp9b->jmax[v] - cp9b->jmin[v]); jp++) {
3738 mx->Jkshadow[v][jp] = mx->Jkshadow_mem + Jk_cur_size;
3739 Jk_cur_size += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
3740 }
3741 }
3742 if(mx->Lkshadow[v] != NULL) {
3743 assert(mx->Lkmode[v] != NULL);
3744 for(jp = 0; jp <= (cp9b->jmax[v] - cp9b->jmin[v]); jp++) {
3745 mx->Lkshadow[v][jp] = mx->Lkshadow_mem + Lk_cur_size;
3746 mx->Lkmode[v][jp] = mx->Lkmode_mem + Lk_cur_size;
3747 Lk_cur_size += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
3748 }
3749 }
3750 if(mx->Rkshadow[v] != NULL) {
3751 assert(mx->Rkmode[v] != NULL);
3752 for(jp = 0; jp <= (cp9b->jmax[v] - cp9b->jmin[v]); jp++) {
3753 mx->Rkshadow[v][jp] = mx->Rkshadow_mem + Rk_cur_size;
3754 mx->Rkmode[v][jp] = mx->Rkmode_mem + Rk_cur_size;
3755 Rk_cur_size += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
3756 }
3757 }
3758 if(mx->Tkshadow[v] != NULL) {
3759 for(jp = 0; jp <= (cp9b->jmax[v] - cp9b->jmin[v]); jp++) {
3760 mx->Tkshadow[v][jp] = mx->Tkshadow_mem + Tk_cur_size;
3761 Tk_cur_size += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
3762 }
3763 }
3764 }
3765 }
3766 /*printf("Jy ncells %10" PRId64 " %10" PRId64 "\n", Jy_cur_size, mx->Jy_ncells_valid);
3767 printf("Ly ncells %10" PRId64 " %10" PRId64 "\n", Ly_cur_size, mx->Ly_ncells_valid);
3768 printf("Ry ncells %10" PRId64 " %10" PRId64 "\n", Ry_cur_size, mx->Ry_ncells_valid);
3769 printf("Jk ncells %10" PRId64 " %10" PRId64 "\n", Jk_cur_size, mx->Jk_ncells_valid);
3770 printf("Lk ncells %10" PRId64 " %10" PRId64 "\n", Lk_cur_size, mx->Lk_ncells_valid);
3771 printf("Rk ncells %10" PRId64 " %10" PRId64 "\n", Rk_cur_size, mx->Rk_ncells_valid);
3772 printf("Tk ncells %10" PRId64 " %10" PRId64 "\n", Tk_cur_size, mx->Tk_ncells_valid);*/
3773 assert(Jy_cur_size == mx->Jy_ncells_valid);
3774 assert(Ly_cur_size == mx->Ly_ncells_valid);
3775 assert(Ry_cur_size == mx->Ry_ncells_valid);
3776 assert(Jk_cur_size == mx->Jk_ncells_valid);
3777 assert(Lk_cur_size == mx->Lk_ncells_valid);
3778 assert(Rk_cur_size == mx->Rk_ncells_valid);
3779 assert(Tk_cur_size == mx->Tk_ncells_valid);
3780 ESL_DASSERT1((Jy_cur_size == mx->Jy_ncells_valid));
3781 ESL_DASSERT1((Ly_cur_size == mx->Ly_ncells_valid));
3782 ESL_DASSERT1((Ry_cur_size == mx->Ry_ncells_valid));
3783 ESL_DASSERT1((Jk_cur_size == mx->Jk_ncells_valid));
3784 ESL_DASSERT1((Lk_cur_size == mx->Lk_ncells_valid));
3785 ESL_DASSERT1((Rk_cur_size == mx->Rk_ncells_valid));
3786 ESL_DASSERT1((Tk_cur_size == mx->Tk_ncells_valid));
3787
3788 mx->cp9b = cp9b; /* just a reference */
3789
3790 /* now update L and size_Mb */
3791 mx->L = L; /* length of current seq we're valid for */
3792 mx->size_Mb = Mb_alloc;
3793
3794 return eslOK;
3795
3796 ERROR:
3797 return status;
3798 }
3799
3800 /* Function: cm_tr_hb_shadow_mx_Destroy()
3801 * Synopsis: Frees a DP matrix.
3802 * Incept: EPN, Wed Sep 7 13:28:55 2011
3803 *
3804 * Purpose: Frees a <CM_TR_HB_SHADOW_MX>.
3805 *
3806 * Returns: (void)
3807 */
3808 void
3809 cm_tr_hb_shadow_mx_Destroy(CM_TR_HB_SHADOW_MX *mx)
3810 {
3811 if (mx == NULL) return;
3812 int v;
3813
3814 if (mx->Jyshadow != NULL) {
3815 for (v = 0; v < mx->M; v++)
3816 if(mx->Jyshadow[v] != NULL) free(mx->Jyshadow[v]);
3817 }
3818 free(mx->Jyshadow);
3819
3820 if (mx->Lyshadow != NULL) {
3821 for (v = 0; v < mx->M; v++)
3822 if(mx->Lyshadow[v] != NULL) free(mx->Lyshadow[v]);
3823 }
3824 free(mx->Lyshadow);
3825
3826 if (mx->Ryshadow != NULL) {
3827 for (v = 0; v < mx->M; v++)
3828 if(mx->Ryshadow[v] != NULL) free(mx->Ryshadow[v]);
3829 }
3830 free(mx->Ryshadow);
3831
3832 if (mx->Jkshadow != NULL) {
3833 for (v = 0; v < mx->M; v++)
3834 if(mx->Jkshadow[v] != NULL) free(mx->Jkshadow[v]);
3835 }
3836 free(mx->Jkshadow);
3837
3838 if (mx->Lkshadow != NULL) {
3839 for (v = 0; v < mx->M; v++)
3840 if(mx->Lkshadow[v] != NULL) free(mx->Lkshadow[v]);
3841 }
3842 free(mx->Lkshadow);
3843
3844 if (mx->Rkshadow != NULL) {
3845 for (v = 0; v < mx->M; v++)
3846 if(mx->Rkshadow[v] != NULL) free(mx->Rkshadow[v]);
3847 }
3848 free(mx->Rkshadow);
3849
3850 if (mx->Tkshadow != NULL) {
3851 for (v = 0; v < mx->M; v++)
3852 if(mx->Tkshadow[v] != NULL) free(mx->Tkshadow[v]);
3853 }
3854 free(mx->Tkshadow);
3855
3856 if (mx->Lkmode != NULL) {
3857 for (v = 0; v < mx->M; v++)
3858 if(mx->Lkmode[v] != NULL) free(mx->Lkmode[v]);
3859 }
3860 free(mx->Lkmode);
3861
3862 if (mx->Rkmode != NULL) {
3863 for (v = 0; v < mx->M; v++)
3864 if(mx->Rkmode[v] != NULL) free(mx->Rkmode[v]);
3865 }
3866 free(mx->Rkmode);
3867
3868 if (mx->JnrowsA != NULL) free(mx->JnrowsA);
3869 if (mx->LnrowsA != NULL) free(mx->LnrowsA);
3870 if (mx->RnrowsA != NULL) free(mx->RnrowsA);
3871 if (mx->TnrowsA != NULL) free(mx->TnrowsA);
3872 if (mx->Jyshadow_mem != NULL) free(mx->Jyshadow_mem);
3873 if (mx->Lyshadow_mem != NULL) free(mx->Lyshadow_mem);
3874 if (mx->Ryshadow_mem != NULL) free(mx->Ryshadow_mem);
3875 if (mx->Jkshadow_mem != NULL) free(mx->Jkshadow_mem);
3876 if (mx->Lkshadow_mem != NULL) free(mx->Lkshadow_mem);
3877 if (mx->Rkshadow_mem != NULL) free(mx->Rkshadow_mem);
3878 if (mx->Tkshadow_mem != NULL) free(mx->Tkshadow_mem);
3879 if (mx->Lkmode_mem != NULL) free(mx->Lkmode_mem);
3880 if (mx->Rkmode_mem != NULL) free(mx->Rkmode_mem);
3881 free(mx);
3882 return;
3883 }
3884
3885 /* Function: cm_tr_hb_shadow_mx_Dump()
3886 * Synopsis: Dump a DP matrix to a stream, for diagnostics.
3887 * Incept: EPN, Wed Sep 7 13:29:01 2011
3888 *
3889 * Purpose: Dump matrix <mx> to stream <fp> for diagnostics.
3890 */
3891 int
3892 cm_tr_hb_shadow_mx_Dump(FILE *ofp, CM_t *cm, CM_TR_HB_SHADOW_MX *mx, char mode, int print_mx)
3893 {
3894 int status;
3895 int v, jp, j, dp, d;
3896 int fill_L, fill_R, fill_T; /* are the L, R, and T matrices valid? */
3897
3898 fprintf(ofp, "M: %d\n", mx->M);
3899 fprintf(ofp, "B: %d\n", mx->B);
3900 fprintf(ofp, "L: %d\n", mx->L);
3901 fprintf(ofp, "Jy_ncells_alloc: %" PRId64 "\nJy_ncells_valid: %" PRId64 "\n", mx->Jy_ncells_alloc, mx->Jy_ncells_valid);
3902 fprintf(ofp, "Ly_ncells_alloc: %" PRId64 "\nLy_ncells_valid: %" PRId64 "\n", mx->Ly_ncells_alloc, mx->Ly_ncells_valid);
3903 fprintf(ofp, "Ry_ncells_alloc: %" PRId64 "\nRy_ncells_valid: %" PRId64 "\n", mx->Ry_ncells_alloc, mx->Ry_ncells_valid);
3904 fprintf(ofp, "Jk_ncells_alloc: %" PRId64 "\nJk_ncells_valid: %" PRId64 "\n", mx->Jk_ncells_alloc, mx->Jk_ncells_valid);
3905 fprintf(ofp, "Lk_ncells_alloc: %" PRId64 "\nLk_ncells_valid: %" PRId64 "\n", mx->Lk_ncells_alloc, mx->Lk_ncells_valid);
3906 fprintf(ofp, "Rk_ncells_alloc: %" PRId64 "\nRk_ncells_valid: %" PRId64 "\n", mx->Rk_ncells_alloc, mx->Rk_ncells_valid);
3907 fprintf(ofp, "Tk_ncells_alloc: %" PRId64 "\nTk_ncells_valid: %" PRId64 "\n", mx->Tk_ncells_alloc, mx->Tk_ncells_valid);
3908
3909 if((status = cm_TrFillFromMode(mode, &fill_L, &fill_R, &fill_T)) != eslOK) return status;
3910
3911 if(print_mx) {
3912 /* yshadow/kshadow matrix data */
3913 for (v = 0; v < mx->M; v++) {
3914 if(cm->sttype[v] == B_st) {
3915 for(jp = 0; jp <= mx->cp9b->jmax[v] - mx->cp9b->jmin[v]; jp++) {
3916 j = jp + mx->cp9b->jmin[v];
3917 for(dp = 0; dp <= mx->cp9b->hdmax[v][jp] - mx->cp9b->hdmin[v][jp]; dp++) {
3918 d = dp + mx->cp9b->hdmin[v][jp];
3919 if(mx->Jkshadow[v]) fprintf(ofp, "Jkshad[v:%5d][j:%5d][d:%5d] %8d\n", v, j, d, mx->Jkshadow[v][jp][dp]);
3920 if(mx->Lkshadow[v] && fill_L) fprintf(ofp, "Lkshad[v:%5d][j:%5d][d:%5d] %8d\n", v, j, d, mx->Lkshadow[v][jp][dp]);
3921 if(mx->Rkshadow[v] && fill_R) fprintf(ofp, "Rkshad[v:%5d][j:%5d][d:%5d] %8d\n", v, j, d, mx->Rkshadow[v][jp][dp]);
3922 if(mx->Tkshadow[v] && fill_T) fprintf(ofp, "Tkshad[v:%5d][j:%5d][d:%5d] %8d\n", v, j, d, mx->Tkshadow[v][jp][dp]);
3923 }
3924 fprintf(ofp, "\n");
3925 }
3926 fprintf(ofp, "\n\n");
3927 }
3928 else {
3929 for(jp = 0; jp <= mx->cp9b->jmax[v] - mx->cp9b->jmin[v]; jp++) {
3930 j = jp + mx->cp9b->jmin[v];
3931 for(dp = 0; dp <= mx->cp9b->hdmax[v][jp] - mx->cp9b->hdmin[v][jp]; dp++) {
3932 d = dp + mx->cp9b->hdmin[v][jp];
3933 if(mx->Jyshadow[v]) fprintf(ofp, "Jyshad[v:%5d][j:%5d][d:%5d] %8d\n", v, j, d, mx->Jyshadow[v][jp][dp]);
3934 if(mx->Lyshadow[v] && fill_L) fprintf(ofp, "Lyshad[v:%5d][j:%5d][d:%5d] %8d\n", v, j, d, mx->Lyshadow[v][jp][dp]);
3935 if(mx->Ryshadow[v] && fill_R) fprintf(ofp, "Ryshad[v:%5d][j:%5d][d:%5d] %8d\n", v, j, d, mx->Ryshadow[v][jp][dp]);
3936 }
3937 fprintf(ofp, "\n");
3938 }
3939 fprintf(ofp, "\n\n");
3940 }
3941 }
3942 }
3943 return eslOK;
3944 }
3945
3946 /* Function: cm_tr_hb_shadow_mx_SizeNeeded()
3947 * Incept: EPN, Wed Sep 7 15:15:04 2011
3948 *
3949 * Purpose: Given a model, and a CP9_bands_t object
3950 * with pre-calced bands for a target, determine the number
3951 * of cells and total size in Mb required for the matrix
3952 * for the target given the bands.
3953 *
3954 * Return number of {J,L,R}yshadow (char) cells required in
3955 * <ret_{J,L,R}ny_cells> and number of {J,L,R,T}kshadow (int) cells
3956 * required in <ret_{J,L,R,T}nk_cells) and size of required
3957 * matrix in Mb in <ret_Mb>.
3958 *
3959 * Args: cm - the CM the matrix is for
3960 * errbuf - char buffer for reporting errors
3961 * cp9b - the bands for the current target sequence
3962 * ret_Jny_cells - RETURN: number of required char cells for J (Jyshadow)
3963 * ret_Lny_cells - RETURN: number of required char cells for L (Lyshadow)
3964 * ret_Rny_cells - RETURN: number of required char cells for R (Ryshadow)
3965 * ret_Jnk_cells - RETURN: number of required int cells for J (Jkshadow)
3966 * ret_Lnk_cells - RETURN: number of required int cells for L (Lkshadow)
3967 * ret_Rnk_cells - RETURN: number of required int cells for R (Rkshadow)
3968 * ret_Tnk_cells - RETURN: number of required int cells for T (Tkshadow)
3969 * ret_Mb - RETURN: required size of matrix in Mb
3970 *
3971 * Returns: <eslOK> on success
3972 *
3973 */
3974 int
3975 cm_tr_hb_shadow_mx_SizeNeeded(CM_t *cm, char *errbuf, CP9Bands_t *cp9b, int64_t *ret_Jny_cells, int64_t *ret_Lny_cells, int64_t *ret_Rny_cells,
3976 int64_t *ret_Jnk_cells, int64_t *ret_Lnk_cells, int64_t *ret_Rnk_cells, int64_t *ret_Tnk_cells, float *ret_Mb)
3977 {
3978 int v, jp;
3979 int64_t Jy_ncells;
3980 int64_t Ly_ncells;
3981 int64_t Ry_ncells;
3982 int64_t Jk_ncells;
3983 int64_t Lk_ncells;
3984 int64_t Rk_ncells;
3985 int64_t Tk_ncells;
3986 int jbw;
3987 float Mb_needed;
3988
3989 /* contract check */
3990 if(cp9b == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cm_tr_hb_shadow_mx_SizeNeeded() entered with cp9b == NULL.\n");
3991
3992 Jy_ncells = 0;
3993 Ly_ncells = 0;
3994 Ry_ncells = 0;
3995 Jk_ncells = 0;
3996 Lk_ncells = 0;
3997 Rk_ncells = 0;
3998 Tk_ncells = 0;
3999 Mb_needed = (float)
4000 (sizeof(CM_TR_HB_SHADOW_MX) +
4001 (3 * (cp9b->cm_M) * sizeof(char **)) + /* mx->{J,L,R}yshadow[] ptrs */
4002 (4 * (cp9b->cm_M) * sizeof(int **)) + /* mx->{J,L,R,T}kshadow[] ptrs */
4003 (4 * (cp9b->cm_M) * sizeof(int))); /* mx->{J,L,R,T}nrowsA */
4004
4005 for(v = 0; v < cp9b->cm_M; v++) {
4006 jbw = cp9b->jmax[v] - cp9b->jmin[v];
4007 if(cm->sttype[v] == B_st) {
4008 if(cp9b->Jvalid[v]) {
4009 Mb_needed += (float) (sizeof(int *) * (jbw+1)); /* mx->Jkshadow[v][] ptrs */
4010 for(jp = 0; jp <= jbw; jp++) {
4011 Jk_ncells += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
4012 }
4013 }
4014 if(cp9b->Lvalid[v]) {
4015 Mb_needed += (float) (sizeof(int *) * (jbw+1)); /* mx->Lkshadow[v][] ptrs */
4016 Mb_needed += (float) (sizeof(char *) * (jbw+1)); /* mx->Lkmode[v][] ptrs */
4017 for(jp = 0; jp <= jbw; jp++) {
4018 Lk_ncells += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
4019 }
4020 }
4021 if(cp9b->Rvalid[v]) {
4022 Mb_needed += (float) (sizeof(int *) * (jbw+1)); /* mx->Rkshadow[v][] ptrs */
4023 Mb_needed += (float) (sizeof(char *) * (jbw+1)); /* mx->Rkmode[v][] ptrs */
4024 for(jp = 0; jp <= jbw; jp++) {
4025 Rk_ncells += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
4026 }
4027 }
4028 if(cp9b->Tvalid[v]) {
4029 Mb_needed += (float) (sizeof(int *) * (jbw+1)); /* mx->Tkshadow[v][] ptrs */
4030 for(jp = 0; jp <= jbw; jp++) {
4031 Tk_ncells += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
4032 }
4033 }
4034 }
4035 else { /* not a B state */
4036 if(cp9b->Jvalid[v]) {
4037 Mb_needed += (float) (sizeof(char *) * (jbw+1)); /* mx->Jyshadow[v][] ptrs */
4038 for(jp = 0; jp <= jbw; jp++) {
4039 Jy_ncells += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
4040 }
4041 }
4042 if(cp9b->Lvalid[v]) {
4043 Mb_needed += (float) (sizeof(char *) * (jbw+1)); /* mx->Lyshadow[v][] ptrs */
4044 for(jp = 0; jp <= jbw; jp++) {
4045 Ly_ncells += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
4046 }
4047 }
4048 if(cp9b->Rvalid[v]) {
4049 Mb_needed += (float) (sizeof(char *) * (jbw+1)); /* mx->Ryshadow[v][] ptrs */
4050 for(jp = 0; jp <= jbw; jp++) {
4051 Ry_ncells += cp9b->hdmax[v][jp] - cp9b->hdmin[v][jp] + 1;
4052 }
4053 }
4054 }
4055 }
4056 Mb_needed += sizeof(int) * Jk_ncells; /* mx->Jkshadow_mem */
4057 Mb_needed += sizeof(int) * Lk_ncells; /* mx->Lkshadow_mem */
4058 Mb_needed += sizeof(int) * Rk_ncells; /* mx->Rkshadow_mem */
4059 Mb_needed += sizeof(int) * Tk_ncells; /* mx->Tkshadow_mem */
4060 Mb_needed += sizeof(char) * Lk_ncells; /* mx->Lkmode_mem */
4061 Mb_needed += sizeof(char) * Rk_ncells; /* mx->Rkmode_mem */
4062 Mb_needed += sizeof(char) * Jy_ncells; /* mx->Jyshadow_mem */
4063 Mb_needed += sizeof(char) * Ly_ncells; /* mx->Lyshadow_mem */
4064 Mb_needed += sizeof(char) * Ry_ncells; /* mx->Ryshadow_mem */
4065 Mb_needed *= 0.000001; /* convert to megabytes */
4066
4067 if(ret_Jny_cells != NULL) *ret_Jny_cells = Jy_ncells;
4068 if(ret_Lny_cells != NULL) *ret_Lny_cells = Ly_ncells;
4069 if(ret_Rny_cells != NULL) *ret_Rny_cells = Ry_ncells;
4070 if(ret_Jnk_cells != NULL) *ret_Jnk_cells = Jk_ncells;
4071 if(ret_Lnk_cells != NULL) *ret_Lnk_cells = Lk_ncells;
4072 if(ret_Rnk_cells != NULL) *ret_Rnk_cells = Rk_ncells;
4073 if(ret_Tnk_cells != NULL) *ret_Tnk_cells = Tk_ncells;
4074 if(ret_Mb != NULL) *ret_Mb = Mb_needed;
4075 return eslOK;
4076 }
4077
4078 /*****************************************************************
4079 * 9. CM_EMIT_MX data structure functions,
4080 * matrix of float log posterior probabilities of emitted
4081 * residues. Used for optimal accuracy alignment and posterior
4082 * annotation of alignments.
4083 *****************************************************************/
4084
4085 /* Function: cm_emit_mx_Create()
4086 * Incept: EPN, Fri Sep 30 14:17:49 2011
4087 *
4088 * Purpose: Allocate a reusable, resizeable <CM_EMIT_MX> for a CM.
4089 *
4090 * Args: cm: the model
4091 *
4092 * Returns: a pointer to the new <CM_EMIT_MX>.
4093 *
4094 * Throws: <NULL> on allocation error.
4095 */
4096 CM_EMIT_MX *
4097 cm_emit_mx_Create(CM_t *cm)
4098 {
4099 int status;
4100 CM_EMIT_MX *mx = NULL;
4101 int v, l_n, r_n;
4102 int allocL = 2; /* this corresponds to a sequence of length 1; mx[v][0] is always IMPOSSIBLE, mx[v][1] is residue 1 */
4103 int M = cm->M;
4104 int l_nstates_valid; /* num states for which mx->l_pp[v] != NULL */
4105 int r_nstates_valid; /* num states for which mx->r_pp[v] != NULL */
4106
4107 /* level 1: the structure itself */
4108 ESL_ALLOC(mx, sizeof(CM_EMIT_MX));
4109 mx->l_pp = NULL;
4110 mx->l_pp_mem = NULL;
4111 mx->r_pp = NULL;
4112 mx->r_pp_mem = NULL;
4113
4114 /* level 2: row (state) pointers, 0.1..M, go all the way to M
4115 */
4116 ESL_ALLOC(mx->l_pp, sizeof(float *) * (M+1));
4117 ESL_ALLOC(mx->r_pp, sizeof(float *) * (M+1));
4118
4119 /* level 3: dp cell memory, when creating only allocate 2 cells per state, for i = 0 and 1 */
4120 /* first count the number of valid emitting states, left and right */
4121 l_nstates_valid = 0;
4122 r_nstates_valid = 0;
4123 for(v = 0; v < cm->M; v++) {
4124 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) l_nstates_valid++;
4125 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) r_nstates_valid++;
4126 }
4127 l_nstates_valid++; /* add 1 for the special left emitting EL state, cm->M */
4128
4129 ESL_ALLOC(mx->l_pp_mem, sizeof(float) * (int64_t) l_nstates_valid * (int64_t) allocL);
4130 ESL_ALLOC(mx->r_pp_mem, sizeof(float) * (int64_t) r_nstates_valid * (int64_t) allocL);
4131
4132 l_n = 0;
4133 r_n = 0;
4134 for (v = 0; v < M; v++) {
4135 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) {
4136 mx->l_pp[v] = mx->l_pp_mem + (int64_t) l_n * (int64_t) allocL;
4137 l_n++;
4138 }
4139 else {
4140 mx->l_pp[v] = NULL;
4141 }
4142
4143 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) {
4144 mx->r_pp[v] = mx->r_pp_mem + (int64_t) r_n * (int64_t) allocL;
4145 r_n++;
4146 }
4147 else {
4148 mx->r_pp[v] = NULL;
4149 }
4150 }
4151
4152 /* EL row */
4153 mx->l_pp[M] = mx->l_pp_mem + (int64_t) l_n * (int64_t) allocL;
4154 mx->r_pp[M] = NULL;
4155
4156 /* finally allocate the sum vector */
4157 ESL_ALLOC(mx->sum, sizeof(float) * allocL);
4158
4159 mx->M = M;
4160 mx->l_ncells_valid = 0;
4161 mx->l_ncells_alloc = (int64_t) l_nstates_valid * (int64_t) allocL;
4162 mx->r_ncells_valid = 0;
4163 mx->r_ncells_alloc = (int64_t) r_nstates_valid * (int64_t) allocL;
4164 mx->L = allocL-1; /* allocL = 2 */
4165
4166 /* calculate size, these are in order of when they were allocated */
4167 mx->size_Mb = (float)
4168 (sizeof(CM_EMIT_MX) +
4169 (mx->M+1) * sizeof(float *) + /* mx->l_pp[] ptrs */
4170 (mx->M+1) * sizeof(float *) + /* mx->r_pp[] ptrs */
4171 mx->l_ncells_alloc * sizeof(float) + /* mx->l_pp_mem */
4172 mx->r_ncells_alloc * sizeof(float) + /* mx->r_pp_mem */
4173 (mx->L+1) * sizeof(float)); /* mx->sum */
4174
4175 mx->size_Mb *= 0.000001; /* convert to Mb */
4176
4177 return mx;
4178
4179 ERROR:
4180 if (mx != NULL) cm_emit_mx_Destroy(mx);
4181 return NULL;
4182 }
4183
4184 /* Function: cm_emit_mx_GrowTo()
4185 * Incept: EPN, Fri Sep 30 14:35:21 2011
4186 *
4187 * Purpose: Assures that a CM_EMIT_MX matrix <mx> is allocated for a
4188 * model of exactly <mx->M> states and target sequence of
4189 * length L, reallocating memory as necessary.
4190 *
4191 * If local ends are on (cm->flags & CMH_LOCAL_END), allocates
4192 * a full EL row for the matrix.
4193 *
4194 * Checks to make sure desired matrix isn't too big (see throws).
4195 *
4196 * Args: cm - the CM the matrix is for
4197 * mx - the matrix to grow
4198 * errbuf - char buffer for reporting errors
4199 * L - the length of the current target sequence we're aligning
4200 * size_limit- max number of Mb for DP matrix, if matrix is bigger -> return eslERANGE
4201 *
4202 * Returns: <eslOK> on success, and <mx> may be reallocated upon
4203 * return; any data that may have been in <mx> must be
4204 * assumed to be invalidated.
4205 *
4206 * Throws: <eslERANGE> if required size to grow to exceeds <size_limit>.
4207 * This should be caught and appropriately handled by caller.
4208 * <eslEINCOMPAT> on contract violation
4209 * <eslEMEM> on memory allocation error.
4210 */
4211 int
4212 cm_emit_mx_GrowTo(CM_t *cm, CM_EMIT_MX *mx, char *errbuf, int L, float size_limit)
4213 {
4214 int status;
4215 void *p;
4216 int v;
4217 int64_t l_cur_size = 0;
4218 int64_t r_cur_size = 0;
4219 int64_t l_ncells;
4220 int64_t r_ncells;
4221 float Mb_needed; /* required size of matrix, given the bands */
4222 float Mb_alloc; /* allocated size of matrix, >= Mb_needed */
4223 int have_el;
4224 int l_realloced; /* did we reallocate mx->l_pp_mem? */
4225 int r_realloced; /* did we reallocate mx->r_pp_mem? */
4226 int sum_realloced; /* did we reallocate mx->sum? */
4227
4228 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
4229
4230 if((status = cm_emit_mx_SizeNeeded(cm, errbuf, L, &l_ncells, &r_ncells, &Mb_needed)) != eslOK) return status;
4231 /*printf("Non-banded matrix requested size: %.2f Mb\n", Mb_needed);*/
4232 ESL_DPRINTF2(("#DEBUG: Non-banded emit matrix requested size: %.2f Mb\n", Mb_needed));
4233 if(Mb_needed > size_limit) ESL_FAIL(eslERANGE, errbuf, "requested non-banded emit mx of %.2f Mb > %.2f Mb limit.\nUse --mxsize, --maxtau or --tau.", Mb_needed, (float) size_limit);
4234
4235 /* must we realloc the full matrix? or can we get away
4236 * with just jiggering the pointers, if total required num cells is
4237 * less than or equal to what we already have alloc'ed?
4238 */
4239 l_realloced = FALSE;
4240 r_realloced = FALSE;
4241 sum_realloced = FALSE;
4242 if (l_ncells > mx->l_ncells_alloc) {
4243 ESL_RALLOC(mx->l_pp_mem, p, sizeof(float) * l_ncells);
4244 mx->l_ncells_alloc = l_ncells;
4245 l_realloced = TRUE;
4246 }
4247 if (r_ncells > mx->r_ncells_alloc) {
4248 ESL_RALLOC(mx->r_pp_mem, p, sizeof(float) * r_ncells);
4249 mx->r_ncells_alloc = r_ncells;
4250 r_realloced = TRUE;
4251 }
4252 if (L > mx->L) {
4253 ESL_RALLOC(mx->sum, p, sizeof(float) * (L+1));
4254 sum_realloced = TRUE;
4255 }
4256
4257 /* Determine the size of our matrix based on the size it needed to be (Mb_needed).
4258 */
4259 Mb_alloc = Mb_needed * 1000000; /* convert to bytes */
4260 if(! l_realloced) {
4261 Mb_alloc -= (float) (sizeof(float) * l_ncells);
4262 Mb_alloc += (float) (sizeof(float) * mx->l_ncells_alloc);
4263 }
4264 if(! r_realloced) {
4265 Mb_alloc -= (float) (sizeof(float) * r_ncells);
4266 Mb_alloc += (float) (sizeof(float) * mx->r_ncells_alloc);
4267 }
4268 if(! sum_realloced) {
4269 Mb_alloc -= (float) (sizeof(float) * (L+1));
4270 Mb_alloc += (float) (sizeof(float) * (mx->L+1));
4271 }
4272 Mb_alloc *= 0.000001; /* convert to Mb */
4273
4274 mx->l_ncells_valid = l_ncells;
4275 mx->r_ncells_valid = r_ncells;
4276
4277 /* reset the pointers, we keep a tally of cur_size as we go
4278 */
4279 l_cur_size = 0;
4280 r_cur_size = 0;
4281 for(v = 0; v < mx->M; v++) {
4282 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) {
4283 mx->l_pp[v] = mx->l_pp_mem + l_cur_size;
4284 l_cur_size += L+1;
4285 }
4286 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) {
4287 mx->r_pp[v] = mx->r_pp_mem + r_cur_size;
4288 r_cur_size += L+1;
4289 }
4290 }
4291 if(have_el) {
4292 mx->l_pp[mx->M] = mx->l_pp_mem + l_cur_size;
4293 l_cur_size += L+1;
4294 }
4295 else {
4296 mx->l_pp[mx->M] = NULL;
4297 }
4298 mx->r_pp[mx->M] = NULL;
4299
4300 #if eslDEBUGLEVEL >= 1
4301 printf("#DEBUG: l_ncells %10" PRId64 " %10" PRId64 "\n", l_cur_size, mx->l_ncells_valid);
4302 printf("#DEBUG: r_ncells %10" PRId64 " %10" PRId64 "\n", r_cur_size, mx->r_ncells_valid);
4303 #endif
4304 assert(l_cur_size == mx->l_ncells_valid);
4305 assert(r_cur_size == mx->r_ncells_valid);
4306 ESL_DASSERT1((l_cur_size == mx->l_ncells_valid));
4307 ESL_DASSERT1((r_cur_size == mx->r_ncells_valid));
4308
4309 /* now update L and size_Mb */
4310 mx->L = L; /* length of current seq we're valid for */
4311 mx->size_Mb = Mb_alloc;
4312
4313 return eslOK;
4314
4315 ERROR:
4316 return status;
4317 }
4318
4319 /* Function: cm_emit_mx_Destroy()
4320 * Synopsis: Frees a CM_EMIT_MX.
4321 * Incept: EPN, Fri Sep 30 14:55:30 2011
4322 *
4323 * Purpose: Frees a <CM_EMIT_MX>.
4324 *
4325 * Returns: (void)
4326 */
4327 void
4328 cm_emit_mx_Destroy(CM_EMIT_MX *mx)
4329 {
4330 if (mx == NULL) return;
4331
4332 if (mx->l_pp != NULL) free(mx->l_pp);
4333 if (mx->r_pp != NULL) free(mx->r_pp);
4334 if (mx->l_pp_mem != NULL) free(mx->l_pp_mem);
4335 if (mx->r_pp_mem != NULL) free(mx->r_pp_mem);
4336 if (mx->sum != NULL) free(mx->sum);
4337
4338 free(mx);
4339 return;
4340 }
4341
4342 /* Function: cm_emit_mx_Dump()
4343 * Synopsis: Dump a emit matrix to a stream, for diagnostics.
4344 * Incept: EPN, Fri Sep 30 15:01:06 2011
4345 *
4346 * Purpose: Dump matrix <mx> to stream <fp> for diagnostics.
4347 */
4348 int
4349 cm_emit_mx_Dump(FILE *ofp, CM_t *cm, CM_EMIT_MX *mx, int print_mx)
4350 {
4351 int v, i;
4352
4353 fprintf(ofp, "M: %d\n", mx->M);
4354 fprintf(ofp, "L: %d\n", mx->L);
4355 fprintf(ofp, "l_ncells_alloc: %" PRId64 "\nl_ncells_valid: %" PRId64 "\n", mx->l_ncells_alloc, mx->l_ncells_valid);
4356 fprintf(ofp, "r_ncells_alloc: %" PRId64 "\nr_ncells_valid: %" PRId64 "\n", mx->r_ncells_alloc, mx->r_ncells_valid);
4357
4358 /* l_pp and r_pp matrix data */
4359 if(print_mx) {
4360 for (v = 0; v <= mx->M; v++) {
4361 for(i = 0; i <= mx->L; i++) {
4362 if(mx->l_pp[v]) fprintf(ofp, "l_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", v, i, sreEXP2(mx->l_pp[v][i]), mx->l_pp[v][i], Nodetype(cm->ndtype[cm->ndidx[v]]), Statetype(cm->sttype[v]));
4363 if(mx->r_pp[v]) fprintf(ofp, "r_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", v, i, sreEXP2(mx->r_pp[v][i]), mx->r_pp[v][i], Nodetype(cm->ndtype[cm->ndidx[v]]), Statetype(cm->sttype[v]));
4364 }
4365 fprintf(ofp, "\n");
4366 }
4367 }
4368 return eslOK;
4369 }
4370
4371 /* Function: cm_emit_mx_SizeNeeded()
4372 * Incept: EPN, Fri Sep 30 15:05:04 2011
4373 *
4374 * Purpose: Given a model and sequence length, determine the number of
4375 * cells and total size in Mb required in a CM_EMIT_MX for
4376 * the target sequence.
4377 *
4378 * Return number of l_pp, r_pp cells required in
4379 * <ret_l_ncells> and <ret_r_ncells> and size of required
4380 * matrix in Mb in <ret_Mb>.
4381 *
4382 * Args: cm - the CM the matrix is for
4383 * errbuf - char buffer for reporting errors
4384 * L - the length of the current target sequence we're aligning
4385 * ret_l_ncells - RETURN: number of matrix cells required
4386 * ret_r_ncells - RETURN: number of matrix cells required
4387 * ret_Mb - RETURN: required size of matrix in Mb
4388 *
4389 * Returns: <eslOK> on success
4390 *
4391 */
4392 int
4393 cm_emit_mx_SizeNeeded(CM_t *cm, char *errbuf, int L, int64_t *ret_l_ncells, int64_t *ret_r_ncells, float *ret_Mb)
4394 {
4395 int v;
4396 int64_t l_ncells;
4397 int64_t r_ncells;
4398 int have_el;
4399 float Mb_needed;
4400 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
4401
4402 l_ncells = 0;
4403 r_ncells = 0;
4404
4405 Mb_needed = (float)
4406 (sizeof(CM_EMIT_MX) +
4407 (cm->M+1) * sizeof(float *) + /* mx->l_pp ptrs */
4408 (cm->M+1) * sizeof(float *)); /* mx->r_pp ptrs */
4409
4410 for(v = 0; v < cm->M; v++) {
4411 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) {
4412 l_ncells += L+1;
4413 }
4414 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) {
4415 r_ncells += L+1;
4416 }
4417 }
4418 if(have_el) l_ncells += L+1; /* space for EL deck */
4419
4420 Mb_needed += sizeof(float) * (l_ncells + r_ncells + (int64_t) (L+1)); /* mx->l_pp_mem, mx->r_pp_mem, mx->sum */
4421 Mb_needed *= 0.000001; /* convert to megabytes */
4422
4423 if(ret_l_ncells != NULL) *ret_l_ncells = l_ncells;
4424 if(ret_r_ncells != NULL) *ret_r_ncells = r_ncells;
4425 if(ret_Mb != NULL) *ret_Mb = Mb_needed;
4426
4427 return eslOK;
4428 }
4429
4430 /*****************************************************************
4431 * 10. CM_TR_EMIT_MX data structure functions, matrix of float log
4432 * posterior probabilities of emitted residues in truncated
4433 * alignments. Used for optimal accuracy alignment and posterior
4434 * annotation of alignments.
4435 *****************************************************************/
4436
4437 /* Function: cm_tr_emit_mx_Create()
4438 * Incept: EPN, Thu Oct 6 14:45:32 2011
4439 *
4440 * Purpose: Allocate a reusable, resizeable <CM_TR_EMIT_MX> for a CM.
4441 *
4442 * Args: cm: the model
4443 *
4444 * Returns: a pointer to the new <CM_TR_EMIT_MX>.
4445 *
4446 * Throws: <NULL> on allocation error.
4447 */
4448 CM_TR_EMIT_MX *
4449 cm_tr_emit_mx_Create(CM_t *cm)
4450 {
4451 int status;
4452 CM_TR_EMIT_MX *mx = NULL;
4453 int v, l_n, r_n;
4454 int allocL = 2; /* this corresponds to a sequence of length 1; mx[v][0] is always IMPOSSIBLE, mx[v][1] is residue 1 */
4455 int M = cm->M;
4456 int l_nstates_valid; /* num states for which mx->Jl_pp[v] and mx->Ll_pp != NULL */
4457 int r_nstates_valid; /* num states for which mx->Jr_pp[v] and mx->Rr_pp != NULL */
4458
4459 /* level 1: the structure itself */
4460 ESL_ALLOC(mx, sizeof(CM_TR_EMIT_MX));
4461 mx->Jl_pp = NULL;
4462 mx->Jl_pp_mem = NULL;
4463 mx->Ll_pp = NULL;
4464 mx->Ll_pp_mem = NULL;
4465 mx->Jr_pp = NULL;
4466 mx->Jr_pp_mem = NULL;
4467 mx->Rr_pp = NULL;
4468 mx->Rr_pp_mem = NULL;
4469
4470 /* level 2: row (state) pointers, 0.1..M, go all the way to M
4471 */
4472 ESL_ALLOC(mx->Jl_pp, sizeof(float *) * (M+1));
4473 ESL_ALLOC(mx->Ll_pp, sizeof(float *) * (M+1));
4474 ESL_ALLOC(mx->Jr_pp, sizeof(float *) * (M+1));
4475 ESL_ALLOC(mx->Rr_pp, sizeof(float *) * (M+1));
4476
4477 /* level 3: dp cell memory, when creating only allocate 2 cells per state, for i = 0 and 1 */
4478 /* first count the number of valid emitting states, left and right */
4479 l_nstates_valid = 0;
4480 r_nstates_valid = 0;
4481 for(v = 0; v < cm->M; v++) {
4482 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) l_nstates_valid++;
4483 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) r_nstates_valid++;
4484 }
4485 l_nstates_valid++; /* add 1 for the EL state, cm->M */
4486 r_nstates_valid++; /* add 1 for the EL state, cm->M */
4487
4488 ESL_ALLOC(mx->Jl_pp_mem, sizeof(float) * (l_nstates_valid) * (allocL));
4489 ESL_ALLOC(mx->Ll_pp_mem, sizeof(float) * (l_nstates_valid) * (allocL));
4490 ESL_ALLOC(mx->Jr_pp_mem, sizeof(float) * (r_nstates_valid) * (allocL));
4491 ESL_ALLOC(mx->Rr_pp_mem, sizeof(float) * (r_nstates_valid) * (allocL));
4492
4493 l_n = 0;
4494 r_n = 0;
4495 for (v = 0; v < M; v++) {
4496 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) {
4497 mx->Jl_pp[v] = mx->Jl_pp_mem + (int64_t) l_n * (int64_t) allocL;
4498 mx->Ll_pp[v] = mx->Ll_pp_mem + (int64_t) l_n * (int64_t) allocL;
4499 l_n++;
4500 }
4501 else {
4502 mx->Jl_pp[v] = NULL;
4503 mx->Ll_pp[v] = NULL;
4504 }
4505
4506 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) {
4507 mx->Jr_pp[v] = mx->Jr_pp_mem + (int64_t) r_n * (int64_t) allocL;
4508 mx->Rr_pp[v] = mx->Rr_pp_mem + (int64_t) r_n * (int64_t) allocL;
4509 r_n++;
4510 }
4511 else {
4512 mx->Jr_pp[v] = NULL;
4513 mx->Rr_pp[v] = NULL;
4514 }
4515 }
4516
4517 /* setup EL row, invalid in Jr */
4518 mx->Jl_pp[M] = mx->Jl_pp_mem + (int64_t) l_n * (int64_t) allocL;
4519 mx->Ll_pp[M] = mx->Ll_pp_mem + (int64_t) l_n * (int64_t) allocL;
4520 mx->Rr_pp[M] = mx->Rr_pp_mem + (int64_t) r_n * (int64_t) allocL;
4521
4522 mx->Jr_pp[M] = NULL;
4523
4524 /* finally allocate the sum vector */
4525 ESL_ALLOC(mx->sum, sizeof(float) * allocL);
4526
4527 mx->M = M;
4528 mx->l_ncells_valid = 0;
4529 mx->l_ncells_alloc = (int64_t) l_nstates_valid * (int64_t) allocL;
4530 mx->r_ncells_valid = 0;
4531 mx->r_ncells_alloc = (int64_t) r_nstates_valid * (int64_t) allocL;
4532 mx->L = allocL-1; /* allocL = 2 */
4533
4534 /* calculate size, these are in order of when they were allocated */
4535 mx->size_Mb = (float)
4536 (sizeof(CM_EMIT_MX) +
4537 (mx->M+1) * sizeof(float *) + /* mx->Jl_pp[] ptrs */
4538 (mx->M+1) * sizeof(float *) + /* mx->Ll_pp[] ptrs */
4539 (mx->M+1) * sizeof(float *) + /* mx->Jr_pp[] ptrs */
4540 (mx->M+1) * sizeof(float *) + /* mx->Rr_pp[] ptrs */
4541 mx->l_ncells_alloc * sizeof(float) + /* mx->Jl_pp_mem */
4542 mx->l_ncells_alloc * sizeof(float) + /* mx->Ll_pp_mem */
4543 mx->r_ncells_alloc * sizeof(float) + /* mx->Jr_pp_mem */
4544 mx->r_ncells_alloc * sizeof(float) + /* mx->Rr_pp_mem */
4545 (mx->L+1) * sizeof(float)); /* mx->sum */
4546
4547 mx->size_Mb *= 0.000001; /* convert to Mb */
4548
4549 return mx;
4550
4551 ERROR:
4552 if (mx != NULL) cm_tr_emit_mx_Destroy(mx);
4553 return NULL;
4554 }
4555
4556 /* Function: cm_tr_emit_mx_GrowTo()
4557 * Incept: EPN, Fri Oct 7 05:42:51 2011
4558 *
4559 * Purpose: Assures that a CM_TR_EMIT_MX matrix <mx> is allocated for a
4560 * model of exactly <mx->M> states and target sequence of
4561 * length L, reallocating memory as necessary.
4562 *
4563 * If local ends are on (cm->flags & CMH_LOCAL_END), allocates
4564 * a full EL row for the matrix.
4565 *
4566 * Checks to make sure desired matrix isn't too big (see throws).
4567 *
4568 * We could save a bit of space here by demanding that we
4569 * know the marginal alignment mode when entering this
4570 * function and only allocating cells that we will use. For
4571 * example, if we know we're in Left marginal mode we don't
4572 * use the Rr_pp matrix at all, and so don't need to
4573 * allocate any cells for it. However, if we did this, a
4574 * subsequent alignment that did use Right marginal mode
4575 * would need to reallocate the Rr_pp matrix. Since these
4576 * matrices are only 2D (much smaller than many of the 3D
4577 * matrices used during alignment) we don't care so much
4578 * about space here and so we always allocate Jl_pp, Ll_pp,
4579 * Jr_pp and Rr_pp to full size even though we rarely need
4580 * all of them.
4581 *
4582 * One wrinkle with this strategy is that it forces us to
4583 * allocate space for Ll_pp[cm->M] for local end alignment
4584 * in Left marginal mode. These cells will never be used
4585 * but are allocated for because the Jl_pp[cm->M] deck is
4586 * used, and we only have a single <l_ncells_allocated>
4587 * and <l_ncells_valid> count.
4588 *
4589 * Args: cm - the CM the matrix is for
4590 * mx - the matrix to grow
4591 * errbuf - char buffer for reporting errors
4592 * L - the length of the current target sequence we're aligning
4593 * size_limit- max number of Mb for DP matrix, if matrix is bigger -> return eslERANGE
4594 *
4595 * Returns: <eslOK> on success, and <mx> may be reallocated upon
4596 * return; any data that may have been in <mx> must be
4597 * assumed to be invalidated.
4598 *
4599 * Throws: <eslERANGE> if required size to grow to exceeds <size_limit>.
4600 * This should be caught and appropriately handled by caller.
4601 * <eslEINCOMPAT> on contract violation
4602 * <eslEMEM> on memory allocation error.
4603 */
4604 int
4605 cm_tr_emit_mx_GrowTo(CM_t *cm, CM_TR_EMIT_MX *mx, char *errbuf, int L, float size_limit)
4606 {
4607 int status;
4608 void *p;
4609 int v;
4610 int64_t l_cur_size = 0;
4611 int64_t r_cur_size = 0;
4612 int64_t l_ncells;
4613 int64_t r_ncells;
4614 float Mb_needed; /* required size of matrix, given the bands */
4615 float Mb_alloc; /* allocated size of matrix, >= Mb_needed */
4616 int have_el;
4617 int l_realloced; /* did we reallocate mx->Jl_pp_mem and mx->Ll_pp_mem? */
4618 int r_realloced; /* did we reallocate mx->Jr_pp_mem and mx->Rr_pp_mem? */
4619 int sum_realloced; /* did we reallocate mx->sum? */
4620
4621 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
4622
4623 if((status = cm_tr_emit_mx_SizeNeeded(cm, errbuf, L, &l_ncells, &r_ncells, &Mb_needed)) != eslOK) return status;
4624 /*printf("Non-banded truncated emit matrix requested size: %.2f Mb\n", Mb_needed);*/
4625 ESL_DPRINTF2(("#DEBUG: Non-banded truncated emit matrix requested size: %.2f Mb\n", Mb_needed));
4626 if(Mb_needed > size_limit) ESL_FAIL(eslERANGE, errbuf, "requested non-banded truncated emit mx of %.2f Mb > %.2f Mb limit.\nUse --mxsize, --maxtau or --tau.", Mb_needed, (float) size_limit);
4627
4628 /* must we realloc the full matrix? or can we get away
4629 * with just jiggering the pointers, if total required num cells is
4630 * less than or equal to what we already have alloc'ed?
4631 */
4632 l_realloced = FALSE;
4633 r_realloced = FALSE;
4634 sum_realloced = FALSE;
4635 if (l_ncells > mx->l_ncells_alloc) {
4636 ESL_RALLOC(mx->Jl_pp_mem, p, sizeof(float) * l_ncells);
4637 ESL_RALLOC(mx->Ll_pp_mem, p, sizeof(float) * l_ncells);
4638 mx->l_ncells_alloc = l_ncells;
4639 l_realloced = TRUE;
4640 }
4641 if (r_ncells > mx->r_ncells_alloc) {
4642 ESL_RALLOC(mx->Jr_pp_mem, p, sizeof(float) * r_ncells);
4643 ESL_RALLOC(mx->Rr_pp_mem, p, sizeof(float) * r_ncells);
4644 mx->r_ncells_alloc = r_ncells;
4645 r_realloced = TRUE;
4646 }
4647 if (L > mx->L) {
4648 ESL_RALLOC(mx->sum, p, sizeof(float) * (int64_t) (L+1));
4649 sum_realloced = TRUE;
4650 }
4651
4652 /* Determine the size of our matrix based on the size it needed to be (Mb_needed).
4653 */
4654 Mb_alloc = Mb_needed * 1000000; /* convert to bytes */
4655 if(! l_realloced) {
4656 Mb_alloc -= (float) (sizeof(float) * l_ncells); /* Jl_pp */
4657 Mb_alloc -= (float) (sizeof(float) * l_ncells); /* Ll_pp */
4658 Mb_alloc += (float) (sizeof(float) * mx->l_ncells_alloc); /* Jl_pp */
4659 Mb_alloc += (float) (sizeof(float) * mx->l_ncells_alloc); /* Ll_pp */
4660 }
4661 if(! r_realloced) {
4662 Mb_alloc -= (float) (sizeof(float) * r_ncells); /* Jr_pp */
4663 Mb_alloc -= (float) (sizeof(float) * r_ncells); /* Rr_pp */
4664 Mb_alloc += (float) (sizeof(float) * mx->r_ncells_alloc); /* Jr_pp */
4665 Mb_alloc += (float) (sizeof(float) * mx->r_ncells_alloc); /* Rr_pp */
4666 }
4667 if(! sum_realloced) {
4668 Mb_alloc -= (float) (sizeof(float) * (L+1));
4669 Mb_alloc += (float) (sizeof(float) * (mx->L+1));
4670 }
4671 Mb_alloc *= 0.000001; /* convert to Mb */
4672
4673 mx->l_ncells_valid = l_ncells;
4674 mx->r_ncells_valid = r_ncells;
4675
4676 /* reset the pointers, we keep a tally of cur_size as we go
4677 */
4678 l_cur_size = 0;
4679 r_cur_size = 0;
4680 for(v = 0; v < mx->M; v++) {
4681 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) {
4682 mx->Jl_pp[v] = mx->Jl_pp_mem + l_cur_size;
4683 mx->Ll_pp[v] = mx->Ll_pp_mem + l_cur_size;
4684 l_cur_size += (int64_t) (L+1);
4685 }
4686 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) {
4687 mx->Jr_pp[v] = mx->Jr_pp_mem + r_cur_size;
4688 mx->Rr_pp[v] = mx->Rr_pp_mem + r_cur_size;
4689 r_cur_size += (int64_t) (L+1);
4690 }
4691 }
4692 if(have_el) {
4693 mx->Jl_pp[mx->M] = mx->Jl_pp_mem + l_cur_size;
4694 mx->Ll_pp[mx->M] = mx->Ll_pp_mem + l_cur_size;
4695 mx->Rr_pp[mx->M] = mx->Rr_pp_mem + r_cur_size;
4696 l_cur_size += (int64_t) (L+1);
4697 r_cur_size += (int64_t) (L+1);
4698 }
4699 else {
4700 mx->Jl_pp[mx->M] = NULL;
4701 mx->Ll_pp[mx->M] = NULL;
4702 mx->Rr_pp[mx->M] = NULL;
4703 }
4704 mx->Jr_pp[mx->M] = NULL;
4705
4706 #if eslDEBUGLEVEL >= 1
4707 printf("#DEBUG: l_ncells %10" PRId64 " %10" PRId64 "\n", l_cur_size, mx->l_ncells_valid);
4708 printf("#DEBUG: r_ncells %10" PRId64 " %10" PRId64 "\n", r_cur_size, mx->r_ncells_valid);
4709 #endif
4710 assert(l_cur_size == mx->l_ncells_valid);
4711 assert(r_cur_size == mx->r_ncells_valid);
4712 ESL_DASSERT1((l_cur_size == mx->l_ncells_valid));
4713 ESL_DASSERT1((r_cur_size == mx->r_ncells_valid));
4714
4715 /* now update L and size_Mb */
4716 mx->L = L; /* length of current seq we're valid for */
4717 mx->size_Mb = Mb_alloc;
4718
4719 return eslOK;
4720
4721 ERROR:
4722 return status;
4723 }
4724
4725 /* Function: cm_tr_emit_mx_Destroy()
4726 * Synopsis: Frees a CM_TR_EMIT_MX.
4727 * Incept: EPN, Fri Oct 7 05:51:14 2011
4728 *
4729 * Purpose: Frees a <CM_TR_EMIT_MX>.
4730 *
4731 * Returns: (void)
4732 */
4733 void
4734 cm_tr_emit_mx_Destroy(CM_TR_EMIT_MX *mx)
4735 {
4736 if (mx == NULL) return;
4737
4738 if (mx->Jl_pp != NULL) free(mx->Jl_pp);
4739 if (mx->Ll_pp != NULL) free(mx->Ll_pp);
4740 if (mx->Jr_pp != NULL) free(mx->Jr_pp);
4741 if (mx->Rr_pp != NULL) free(mx->Rr_pp);
4742 if (mx->Jl_pp_mem != NULL) free(mx->Jl_pp_mem);
4743 if (mx->Ll_pp_mem != NULL) free(mx->Ll_pp_mem);
4744 if (mx->Jr_pp_mem != NULL) free(mx->Jr_pp_mem);
4745 if (mx->Rr_pp_mem != NULL) free(mx->Rr_pp_mem);
4746 if (mx->sum != NULL) free(mx->sum);
4747
4748 free(mx);
4749 return;
4750 }
4751
4752 /* Function: cm_tr_emit_mx_Dump()
4753 * Synopsis: Dump a truncated emit matrix to a stream, for diagnostics.
4754 * Incept: EPN, Fri Oct 7 05:52:13 2011
4755 *
4756 * Purpose: Dump matrix <mx> to stream <fp> for diagnostics.
4757 */
4758 int
4759 cm_tr_emit_mx_Dump(FILE *ofp, CM_t *cm, CM_TR_EMIT_MX *mx, char mode, int print_mx)
4760 {
4761 int status;
4762 int v, i;
4763 int fill_L, fill_R, fill_T; /* are the L, R, and T matrices valid? */
4764
4765 fprintf(ofp, "M: %d\n", mx->M);
4766 fprintf(ofp, "L: %d\n", mx->L);
4767 fprintf(ofp, "l_ncells_alloc: %" PRId64 "\nl_ncells_valid: %" PRId64 "\n", mx->l_ncells_alloc, mx->l_ncells_valid);
4768 fprintf(ofp, "r_ncells_alloc: %" PRId64 "\nr_ncells_valid: %" PRId64 "\n", mx->r_ncells_alloc, mx->r_ncells_valid);
4769 fprintf(ofp, "mode: %d\n", mode);
4770
4771 if((status = cm_TrFillFromMode(mode, &fill_L, &fill_R, &fill_T)) != eslOK) return status;
4772
4773 if(print_mx) {
4774 /* l_pp and r_pp matrix data */
4775 for (v = 0; v < mx->M; v++) {
4776 for(i = 0; i <= mx->L; i++) {
4777 if(mx->Jl_pp[v]) fprintf(ofp, "Jl_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", v, i, sreEXP2(mx->Jl_pp[v][i]), mx->Jl_pp[v][i], Nodetype(cm->ndtype[cm->ndidx[v]]), Statetype(cm->sttype[v]));
4778 if(mx->Ll_pp[v] && fill_L) fprintf(ofp, "Ll_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", v, i, sreEXP2(mx->Ll_pp[v][i]), mx->Ll_pp[v][i], Nodetype(cm->ndtype[cm->ndidx[v]]), Statetype(cm->sttype[v]));
4779 if(mx->Jr_pp[v]) fprintf(ofp, "Jr_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", v, i, sreEXP2(mx->Jr_pp[v][i]), mx->Jr_pp[v][i], Nodetype(cm->ndtype[cm->ndidx[v]]), Statetype(cm->sttype[v]));
4780 if(mx->Rr_pp[v] && fill_R) fprintf(ofp, "Rr_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", v, i, sreEXP2(mx->Rr_pp[v][i]), mx->Rr_pp[v][i], Nodetype(cm->ndtype[cm->ndidx[v]]), Statetype(cm->sttype[v]));
4781 }
4782 fprintf(ofp, "\n");
4783 }
4784 /* EL state */
4785 for(i = 0; i <= mx->L; i++) {
4786 if(mx->Jl_pp[cm->M]) fprintf(ofp, "Jl_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", cm->M, i, sreEXP2(mx->Jl_pp[cm->M][i]), mx->Jl_pp[cm->M][i], "EL", Statetype(cm->sttype[v]));
4787 if(mx->Ll_pp[cm->M] && fill_L) fprintf(ofp, "Ll_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", cm->M, i, sreEXP2(mx->Ll_pp[cm->M][i]), mx->Ll_pp[cm->M][i], "EL", Statetype(cm->sttype[v]));
4788 if(mx->Rr_pp[cm->M] && fill_R) fprintf(ofp, "Rr_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", cm->M, i, sreEXP2(mx->Rr_pp[cm->M][i]), mx->Rr_pp[cm->M][i], "EL", Statetype(cm->sttype[v]));
4789 }
4790 }
4791 return eslOK;
4792 }
4793
4794 /* Function: cm_tr_emit_mx_SizeNeeded()
4795 * Incept: EPN, Fri Oct 7 05:53:30 2011
4796 *
4797 * Purpose: Given a model and sequence length, determine the number of
4798 * cells and total size in Mb required in a CM_TR_EMIT_MX for
4799 * the target sequence.
4800 *
4801 * Return number of l_pp, r_pp cells required in
4802 * <ret_l_ncells> and <ret_r_ncells> and size of required
4803 * matrix in Mb in <ret_Mb>.
4804 *
4805 * Args: cm - the CM the matrix is for
4806 * errbuf - char buffer for reporting errors
4807 * L - the length of the current target sequence we're aligning
4808 * ret_l_ncells - RETURN: number of matrix cells required
4809 * ret_r_ncells - RETURN: number of matrix cells required
4810 * ret_Mb - RETURN: required size of matrix in Mb
4811 *
4812 * Returns: <eslOK> on success
4813 *
4814 */
4815 int
4816 cm_tr_emit_mx_SizeNeeded(CM_t *cm, char *errbuf, int L, int64_t *ret_l_ncells, int64_t *ret_r_ncells, float *ret_Mb)
4817 {
4818 int v;
4819 int64_t l_ncells;
4820 int64_t r_ncells;
4821 int have_el;
4822 float Mb_needed;
4823 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
4824
4825 l_ncells = 0;
4826 r_ncells = 0;
4827
4828 Mb_needed = (float)
4829 (sizeof(CM_EMIT_MX) +
4830 (cm->M+1) * sizeof(float *) + /* mx->Jl_pp ptrs */
4831 (cm->M+1) * sizeof(float *) + /* mx->Ll_pp ptrs */
4832 (cm->M+1) * sizeof(float *) + /* mx->Jr_pp ptrs */
4833 (cm->M+1) * sizeof(float *)); /* mx->Rr_pp ptrs */
4834
4835 for(v = 0; v < cm->M; v++) {
4836 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) {
4837 l_ncells += L+1;
4838 }
4839 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) {
4840 r_ncells += L+1;
4841 }
4842 }
4843 if(have_el) {
4844 l_ncells += L+1; /* space for EL deck */
4845 r_ncells += L+1; /* space for EL deck */
4846 }
4847
4848 Mb_needed += sizeof(float) * (l_ncells + l_ncells + r_ncells + r_ncells + (L+1));
4849 /* mx->Jl_pp_mem, mx->Ll_pp_mem, mx->Jr_pp_mem, mx->Rr_pp_mem, mx->sum */
4850 Mb_needed *= 0.000001; /* convert to megabytes */
4851
4852 if(ret_l_ncells != NULL) *ret_l_ncells = l_ncells;
4853 if(ret_r_ncells != NULL) *ret_r_ncells = r_ncells;
4854 if(ret_Mb != NULL) *ret_Mb = Mb_needed;
4855
4856 return eslOK;
4857 }
4858
4859 /*****************************************************************
4860 * 11. CM_HB_EMIT_MX data structure functions,
4861 * matrix of float log posterior probabilities of emitted
4862 * residues. Used for optimal accuracy alignment and posterior
4863 * annotation of alignments. HMM-banded version.
4864 *****************************************************************/
4865
4866 /* Function: cm_hb_emit_mx_Create()
4867 * Incept: EPN, Thu Oct 6 06:43:57 2011
4868 *
4869 * Purpose: Allocate a reusable, resizeable <CM_HB_EMIT_MX> for a CM.
4870 *
4871 * Args: cm: the model
4872 *
4873 * Returns: a pointer to the new <CM_HB_EMIT_MX>.
4874 *
4875 * Throws: <NULL> on allocation error.
4876 */
4877 CM_HB_EMIT_MX *
4878 cm_hb_emit_mx_Create(CM_t *cm)
4879 {
4880 int status;
4881 CM_HB_EMIT_MX *mx = NULL;
4882 int v, l_n, r_n;
4883 int allocL = 1; /* this corresponds to a sequence of length 1 */
4884 int M = cm->M;
4885 int l_nstates_valid; /* num states for which mx->l_pp[v] != NULL */
4886 int r_nstates_valid; /* num states for which mx->r_pp[v] != NULL */
4887
4888 /* level 1: the structure itself */
4889 ESL_ALLOC(mx, sizeof(CM_HB_EMIT_MX));
4890 mx->l_pp = NULL;
4891 mx->l_pp_mem = NULL;
4892 mx->r_pp = NULL;
4893 mx->r_pp_mem = NULL;
4894
4895 /* level 2: row (state) pointers, 0.1..M, go all the way to M
4896 */
4897 ESL_ALLOC(mx->l_pp, sizeof(float *) * (M+1));
4898 ESL_ALLOC(mx->r_pp, sizeof(float *) * (M+1));
4899
4900 /* level 3: dp cell memory, when creating only allocate 2 cells per state, for i = 0 and 1 */
4901 /* first count the number of valid emitting states, left and right */
4902 l_nstates_valid = 0;
4903 r_nstates_valid = 0;
4904 for(v = 0; v < cm->M; v++) {
4905 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) l_nstates_valid++;
4906 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) r_nstates_valid++;
4907 }
4908 l_nstates_valid++; /* add 1 for the special left emitting EL state, cm->M */
4909
4910 ESL_ALLOC(mx->l_pp_mem, sizeof(float) * (l_nstates_valid) * (allocL));
4911 ESL_ALLOC(mx->r_pp_mem, sizeof(float) * (r_nstates_valid) * (allocL));
4912
4913 l_n = 0;
4914 r_n = 0;
4915 for (v = 0; v < M; v++) {
4916 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) {
4917 mx->l_pp[v] = mx->l_pp_mem + (int64_t) l_n * (int64_t) allocL;
4918 l_n++;
4919 }
4920 else {
4921 mx->l_pp[v] = NULL;
4922 }
4923
4924 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) {
4925 mx->r_pp[v] = mx->r_pp_mem + (int64_t) r_n * (int64_t) allocL;
4926 r_n++;
4927 }
4928 else {
4929 mx->r_pp[v] = NULL;
4930 }
4931 }
4932
4933 /* EL row */
4934 mx->l_pp[M] = mx->l_pp_mem + (int64_t) l_n * (int64_t) allocL;
4935 mx->r_pp[M] = NULL;
4936
4937 /* finally allocate the sum vector */
4938 ESL_ALLOC(mx->sum, sizeof(float) * allocL);
4939
4940 mx->M = M;
4941 mx->l_ncells_valid = 0;
4942 mx->l_ncells_alloc = (int64_t) l_nstates_valid * (int64_t) allocL;
4943 mx->r_ncells_valid = 0;
4944 mx->r_ncells_alloc = (int64_t) r_nstates_valid * (int64_t) allocL;
4945 mx->L = allocL-1; /* allocL = 2 */
4946 mx->cp9b = NULL;
4947 /* calculate size, these are in order of when they were allocated */
4948 mx->size_Mb = (float)
4949 (sizeof(CM_HB_EMIT_MX) +
4950 (mx->M+1) * sizeof(float *) + /* mx->l_pp[] ptrs */
4951 (mx->M+1) * sizeof(float *) + /* mx->r_pp[] ptrs */
4952 mx->l_ncells_alloc * sizeof(float) + /* mx->l_pp_mem */
4953 mx->r_ncells_alloc * sizeof(float) + /* mx->r_pp_mem */
4954 (mx->L+1) * sizeof(float)); /* mx->sum */
4955
4956 mx->size_Mb *= 0.000001; /* convert to Mb */
4957
4958 return mx;
4959
4960 ERROR:
4961 if (mx != NULL) cm_hb_emit_mx_Destroy(mx);
4962 return NULL;
4963 }
4964
4965 /* Function: cm_hb_emit_mx_GrowTo()
4966 * Incept: EPN, Thu Oct 6 06:51:57 2011
4967 *
4968 * Purpose: Assures that a CM_HB_EMIT_MX matrix <mx> is allocated for a
4969 * model of exactly <mx->M> states and target sequence of
4970 * length L, given bands in <cp9b>, reallocating memory as
4971 * necessary.
4972 *
4973 * If local ends are on (cm->flags & CMH_LOCAL_END), allocates
4974 * a full non-banded EL row for the matrix.
4975 *
4976 * Checks to make sure desired matrix isn't too big (see throws).
4977 *
4978 * Args: cm - the CM the matrix is for
4979 * cp9b - HMM bands for current sequence
4980 * mx - the matrix to grow
4981 * errbuf - char buffer for reporting errors
4982 * cp9b - the HMM bands for the target sequence we're growing for
4983 * L - the length of the current target sequence we're aligning
4984 * size_limit- max number of Mb for DP matrix, if matrix is bigger -> return eslERANGE
4985 *
4986 * Returns: <eslOK> on success, and <mx> may be reallocated upon
4987 * return; any data that may have been in <mx> must be
4988 * assumed to be invalidated.
4989 *
4990 * Throws: <eslERANGE> if required size to grow to exceeds <size_limit>.
4991 * This should be caught and appropriately handled by caller.
4992 * <eslEINCOMPAT> on contract violation
4993 * <eslEMEM> on memory allocation error.
4994 */
4995 int
4996 cm_hb_emit_mx_GrowTo(CM_t *cm, CM_HB_EMIT_MX *mx, char *errbuf, CP9Bands_t *cp9b, int L, float size_limit)
4997 {
4998 int status;
4999 void *p;
5000 int v;
5001 int64_t l_cur_size = 0;
5002 int64_t r_cur_size = 0;
5003 int64_t l_ncells;
5004 int64_t r_ncells;
5005 float Mb_needed; /* required size of matrix, given the bands */
5006 float Mb_alloc; /* allocated size of matrix, >= Mb_needed */
5007 int have_el;
5008 int l_realloced; /* did we reallocate mx->l_pp_mem? */
5009 int r_realloced; /* did we reallocate mx->r_pp_mem? */
5010 int sum_realloced; /* did we reallocate mx->sum? */
5011
5012 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
5013
5014 if((status = cm_hb_emit_mx_SizeNeeded(cm, errbuf, cp9b, L, &l_ncells, &r_ncells, &Mb_needed)) != eslOK) return status;
5015 /*printf("HMM banded emit matrix requested size: %.2f Mb\n", Mb_needed);*/
5016 ESL_DPRINTF2(("#DEBUG: HMM banded emit matrix requested size: %.2f Mb\n", Mb_needed));
5017 if(Mb_needed > size_limit) ESL_FAIL(eslERANGE, errbuf, "requested HMM banded emit mx of %.2f Mb > %.2f Mb limit.\nUse --mxsize, --maxtau or --tau.", Mb_needed, (float) size_limit);
5018
5019 /* must we realloc the full matrix? or can we get away
5020 * with just jiggering the pointers, if total required num cells is
5021 * less than or equal to what we already have alloc'ed?
5022 */
5023 l_realloced = FALSE;
5024 r_realloced = FALSE;
5025 sum_realloced = FALSE;
5026 if (l_ncells > mx->l_ncells_alloc) {
5027 ESL_RALLOC(mx->l_pp_mem, p, sizeof(float) * l_ncells);
5028 mx->l_ncells_alloc = l_ncells;
5029 l_realloced = TRUE;
5030 }
5031 if (r_ncells > mx->r_ncells_alloc) {
5032 ESL_RALLOC(mx->r_pp_mem, p, sizeof(float) * r_ncells);
5033 mx->r_ncells_alloc = r_ncells;
5034 r_realloced = TRUE;
5035 }
5036 if (L > mx->L) {
5037 ESL_RALLOC(mx->sum, p, sizeof(float) * (L+1));
5038 sum_realloced = TRUE;
5039 }
5040
5041 /* Determine the size of our matrix based on the size it needed to be (Mb_needed).
5042 */
5043 Mb_alloc = Mb_needed * 1000000; /* convert to bytes */
5044 if(! l_realloced) {
5045 Mb_alloc -= (float) (sizeof(float) * l_ncells);
5046 Mb_alloc += (float) (sizeof(float) * mx->l_ncells_alloc);
5047 }
5048 if(! r_realloced) {
5049 Mb_alloc -= (float) (sizeof(float) * r_ncells);
5050 Mb_alloc += (float) (sizeof(float) * mx->r_ncells_alloc);
5051 }
5052 if(! sum_realloced) {
5053 Mb_alloc -= (float) (sizeof(float) * (L+1));
5054 Mb_alloc += (float) (sizeof(float) * (mx->L+1));
5055 }
5056 Mb_alloc *= 0.000001; /* convert to Mb */
5057
5058 mx->l_ncells_valid = l_ncells;
5059 mx->r_ncells_valid = r_ncells;
5060
5061 /* reset the pointers, we keep a tally of cur_size as we go
5062 */
5063 l_cur_size = 0;
5064 r_cur_size = 0;
5065 for(v = 0; v < mx->M; v++) {
5066 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) {
5067 mx->l_pp[v] = mx->l_pp_mem + l_cur_size;
5068 if(cp9b->imax[v] >= cp9b->imin[v] && cp9b->imax[v] >= 1) {
5069 l_cur_size += cp9b->imax[v] - cp9b->imin[v] + 1;
5070 }
5071 }
5072 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) {
5073 mx->r_pp[v] = mx->r_pp_mem + r_cur_size;
5074 if(cp9b->jmax[v] >= cp9b->jmin[v] && cp9b->jmax[v] >= 1) {
5075 r_cur_size += cp9b->jmax[v] - cp9b->jmin[v] + 1;
5076 }
5077 }
5078 }
5079 if(have_el) { /* EL state is non-banded */
5080 mx->l_pp[mx->M] = mx->l_pp_mem + l_cur_size;
5081 l_cur_size += L+1;
5082 }
5083 else {
5084 mx->l_pp[mx->M] = NULL;
5085 }
5086 mx->r_pp[mx->M] = NULL;
5087
5088 #if eslDEBUGLEVEL >= 1
5089 printf("#DEBUG: l_ncells %10" PRId64 " %10" PRId64 "\n", l_cur_size, mx->l_ncells_valid);
5090 printf("#DEBUG: r_ncells %10" PRId64 " %10" PRId64 "\n", r_cur_size, mx->r_ncells_valid);
5091 #endif
5092 assert(l_cur_size == mx->l_ncells_valid);
5093 assert(r_cur_size == mx->r_ncells_valid);
5094 ESL_DASSERT1((l_cur_size == mx->l_ncells_valid));
5095 ESL_DASSERT1((r_cur_size == mx->r_ncells_valid));
5096
5097 /* now update L and size_Mb */
5098 mx->L = L; /* length of current seq we're valid for */
5099 mx->size_Mb = Mb_alloc;
5100 mx->cp9b = cp9b;
5101
5102 return eslOK;
5103
5104 ERROR:
5105 return status;
5106 }
5107
5108 /* Function: cm_hb_emit_mx_Destroy()
5109 * Synopsis: Frees a CM_HB_EMIT_MX.
5110 * Incept: EPN, Thu Oct 6 09:06:58 2011
5111 *
5112 * Purpose: Frees a CM_HB_EMIT_MX.
5113 *
5114 * Returns: (void)
5115 */
5116 void
5117 cm_hb_emit_mx_Destroy(CM_HB_EMIT_MX *mx)
5118 {
5119 if (mx == NULL) return;
5120
5121 if (mx->l_pp != NULL) free(mx->l_pp);
5122 if (mx->r_pp != NULL) free(mx->r_pp);
5123 if (mx->l_pp_mem != NULL) free(mx->l_pp_mem);
5124 if (mx->r_pp_mem != NULL) free(mx->r_pp_mem);
5125 if (mx->sum != NULL) free(mx->sum);
5126
5127 /* don't free cp9b, that's just a reference */
5128 free(mx);
5129 return;
5130 }
5131
5132 /* Function: cm_hb_emit_mx_Dump()
5133 * Synopsis: Dump a HMM banded emit matrix to a stream, for diagnostics.
5134 * Incept: EPN, Thu Oct 6 09:07:35 2011
5135 *
5136 * Purpose: Dump matrix <mx> to stream <fp> for diagnostics.
5137 */
5138 int
5139 cm_hb_emit_mx_Dump(FILE *ofp, CM_t *cm, CM_HB_EMIT_MX *mx, int print_mx)
5140 {
5141 int v, i, j, ip_v, jp_v;
5142
5143 fprintf(ofp, "M: %d\n", mx->M);
5144 fprintf(ofp, "L: %d\n", mx->L);
5145 fprintf(ofp, "l_ncells_alloc: %" PRId64 "\nl_ncells_valid: %" PRId64 "\n", mx->l_ncells_alloc, mx->l_ncells_valid);
5146 fprintf(ofp, "r_ncells_alloc: %" PRId64 "\nr_ncells_valid: %" PRId64 "\n", mx->r_ncells_alloc, mx->r_ncells_valid);
5147
5148 if(print_mx) {
5149 /* l_pp and r_pp matrix data */
5150 for (v = 0; v < mx->M; v++) {
5151 if(mx->l_pp[v]) {
5152 for(i = mx->cp9b->imin[v]; i <= mx->cp9b->imax[v]; i++) {
5153 ip_v = i - mx->cp9b->imin[v];
5154 fprintf(ofp, "l_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", v, i, sreEXP2(mx->l_pp[v][ip_v]), mx->l_pp[v][ip_v], Nodetype(cm->ndtype[cm->ndidx[v]]), Statetype(cm->sttype[v]));
5155 }
5156 }
5157 if(mx->r_pp[v]) {
5158 for(j = mx->cp9b->jmin[v]; j <= mx->cp9b->jmax[v]; j++) {
5159 jp_v = j - mx->cp9b->jmin[v];
5160 fprintf(ofp, "r_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", v, j, sreEXP2(mx->r_pp[v][jp_v]), mx->r_pp[v][jp_v], Nodetype(cm->ndtype[cm->ndidx[v]]), Statetype(cm->sttype[v]));
5161 }
5162 }
5163 fprintf(ofp, "\n");
5164 }
5165 /* EL state */
5166 for(i = 0; i <= mx->L; i++) {
5167 if(mx->l_pp[cm->M]) fprintf(ofp, "l_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", cm->M, i, sreEXP2(mx->l_pp[cm->M][i]), mx->l_pp[cm->M][i], "EL", Statetype(cm->sttype[v]));
5168 }
5169 }
5170 return eslOK;
5171 }
5172
5173 /* Function: cm_hb_emit_mx_SizeNeeded()
5174 * Incept: EPN, Thu Oct 6 09:11:15 2011
5175 *
5176 * Purpose: Given a model, sequence length, and HMM bands object,
5177 * determine the number of cells and total size in Mb
5178 * required in a CM_HB_EMIT_MX for the target sequence.
5179 *
5180 * Return number of l_pp, r_pp cells required in
5181 * <ret_l_ncells> and <ret_r_ncells> and size of required
5182 * matrix in Mb in <ret_Mb>.
5183 *
5184 * Args: cm - the CM the matrix is for
5185 * errbuf - char buffer for reporting errors
5186 * cp9b - the HMM bands for this sequence
5187 * L - the length of the current target sequence we're aligning
5188 * ret_l_ncells - RETURN: number of matrix cells required
5189 * ret_r_ncells - RETURN: number of matrix cells required
5190 * ret_Mb - RETURN: required size of matrix in Mb
5191 *
5192 * Returns: <eslOK> on success
5193 *
5194 */
5195 int
5196 cm_hb_emit_mx_SizeNeeded(CM_t *cm, char *errbuf, CP9Bands_t *cp9b, int L, int64_t *ret_l_ncells, int64_t *ret_r_ncells, float *ret_Mb)
5197 {
5198 int v;
5199 int64_t l_ncells;
5200 int64_t r_ncells;
5201 int have_el;
5202 float Mb_needed;
5203 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
5204
5205 l_ncells = 0;
5206 r_ncells = 0;
5207
5208 Mb_needed = (float)
5209 (sizeof(CM_HB_EMIT_MX) +
5210 (cm->M+1) * sizeof(float *) + /* mx->l_pp ptrs */
5211 (cm->M+1) * sizeof(float *)); /* mx->r_pp ptrs */
5212
5213 for(v = 0; v < cm->M; v++) {
5214 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) {
5215 l_ncells += cp9b->imax[v] - cp9b->imin[v] + 1;
5216 }
5217 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) {
5218 r_ncells += cp9b->jmax[v] - cp9b->jmin[v] + 1;
5219 }
5220 }
5221 if(have_el) l_ncells += L+1; /* space for EL deck */
5222
5223 Mb_needed += sizeof(float) * (l_ncells + r_ncells + (L+1)); /* mx->l_pp_mem, mx->r_pp_mem, mx->sum */
5224 Mb_needed *= 0.000001; /* convert to megabytes */
5225
5226 if(ret_l_ncells != NULL) *ret_l_ncells = l_ncells;
5227 if(ret_r_ncells != NULL) *ret_r_ncells = r_ncells;
5228 if(ret_Mb != NULL) *ret_Mb = Mb_needed;
5229
5230 return eslOK;
5231 }
5232
5233
5234 /*****************************************************************
5235 * 12. CM_TR_HB_EMIT_MX data structure functions, matrix of float log
5236 * posterior probabilities of emitted residues in truncated
5237 * alignments. Used for optimal accuracy alignment and posterior
5238 * annotation of alignments. HMM-banded version.
5239 *****************************************************************/
5240
5241 /* Function: cm_tr_hb_emit_mx_Create()
5242 * Incept: EPN, Fri Oct 7 09:55:24 2011
5243 *
5244 * Purpose: Allocate a reusable, resizeable <CM_TR_HB_EMIT_MX> for a CM.
5245 *
5246 * Args: cm: the model
5247 *
5248 * Returns: a pointer to the new <CM_TR_HB_EMIT_MX>.
5249 *
5250 * Throws: <NULL> on allocation error.
5251 */
5252 CM_TR_HB_EMIT_MX *
5253 cm_tr_hb_emit_mx_Create(CM_t *cm)
5254 {
5255 int status;
5256 CM_TR_HB_EMIT_MX *mx = NULL;
5257 int v, l_n, r_n;
5258 int allocL = 1; /* this corresponds to a sequence of length 1 */
5259 int M = cm->M;
5260 int l_nstates_valid; /* num states for which mx->l_pp[v] != NULL */
5261 int r_nstates_valid; /* num states for which mx->r_pp[v] != NULL */
5262
5263 /* level 1: the structure itself */
5264 ESL_ALLOC(mx, sizeof(CM_TR_HB_EMIT_MX));
5265 mx->Jl_pp = NULL;
5266 mx->Jl_pp_mem = NULL;
5267 mx->Ll_pp = NULL;
5268 mx->Ll_pp_mem = NULL;
5269 mx->Jr_pp = NULL;
5270 mx->Jr_pp_mem = NULL;
5271 mx->Rr_pp = NULL;
5272 mx->Rr_pp_mem = NULL;
5273
5274 /* level 2: row (state) pointers, 0.1..M, go all the way to M
5275 */
5276 ESL_ALLOC(mx->Jl_pp, sizeof(float *) * (M+1));
5277 ESL_ALLOC(mx->Ll_pp, sizeof(float *) * (M+1));
5278 ESL_ALLOC(mx->Jr_pp, sizeof(float *) * (M+1));
5279 ESL_ALLOC(mx->Rr_pp, sizeof(float *) * (M+1));
5280
5281 /* level 3: dp cell memory, when creating only allocate 2 cells per state, for i = 0 and 1 */
5282 /* first count the number of valid emitting states, left and right */
5283 l_nstates_valid = 0;
5284 r_nstates_valid = 0;
5285 for(v = 0; v < cm->M; v++) {
5286 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) l_nstates_valid++;
5287 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) r_nstates_valid++;
5288 }
5289 l_nstates_valid++; /* add 1 for the EL state, cm->M */
5290 r_nstates_valid++; /* add 1 for the EL state, cm->M */
5291
5292 ESL_ALLOC(mx->Jl_pp_mem, sizeof(float) * (l_nstates_valid) * (allocL));
5293 ESL_ALLOC(mx->Ll_pp_mem, sizeof(float) * (l_nstates_valid) * (allocL));
5294 ESL_ALLOC(mx->Jr_pp_mem, sizeof(float) * (r_nstates_valid) * (allocL));
5295 ESL_ALLOC(mx->Rr_pp_mem, sizeof(float) * (r_nstates_valid) * (allocL));
5296
5297 l_n = 0;
5298 r_n = 0;
5299 for (v = 0; v < M; v++) {
5300 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) {
5301 mx->Jl_pp[v] = mx->Jl_pp_mem + (int64_t) l_n * (int64_t) allocL;
5302 mx->Ll_pp[v] = mx->Ll_pp_mem + (int64_t) l_n * (int64_t) allocL;
5303 l_n++;
5304 }
5305 else {
5306 mx->Jl_pp[v] = NULL;
5307 mx->Ll_pp[v] = NULL;
5308 }
5309
5310 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) {
5311 mx->Jr_pp[v] = mx->Jr_pp_mem + (int64_t) r_n * (int64_t) allocL;
5312 mx->Rr_pp[v] = mx->Rr_pp_mem + (int64_t) r_n * (int64_t) allocL;
5313 r_n++;
5314 }
5315 else {
5316 mx->Jr_pp[v] = NULL;
5317 mx->Rr_pp[v] = NULL;
5318 }
5319 }
5320
5321 /* allocate EL row, invalid in Jr */
5322 mx->Jl_pp[M] = mx->Jl_pp_mem + (int64_t) l_n * (int64_t) allocL;
5323 mx->Ll_pp[M] = mx->Ll_pp_mem + (int64_t) l_n * (int64_t) allocL;
5324 mx->Rr_pp[M] = mx->Ll_pp_mem + (int64_t) l_n * (int64_t) allocL;
5325 mx->Jr_pp[M] = NULL;
5326
5327 /* finally allocate the sum vector */
5328 ESL_ALLOC(mx->sum, sizeof(float) * allocL);
5329
5330 mx->M = M;
5331 mx->l_ncells_valid = 0;
5332 mx->l_ncells_alloc = (int64_t) l_nstates_valid * (int64_t) allocL;
5333 mx->r_ncells_valid = 0;
5334 mx->r_ncells_alloc = (int64_t) r_nstates_valid * (int64_t) allocL;
5335 mx->L = allocL-1; /* allocL = 2 */
5336 mx->cp9b = NULL;
5337 /* calculate size, these are in order of when they were allocated */
5338 mx->size_Mb = (float)
5339 (sizeof(CM_TR_HB_EMIT_MX) +
5340 (mx->M+1) * sizeof(float *) + /* mx->Jl_pp[] ptrs */
5341 (mx->M+1) * sizeof(float *) + /* mx->Ll_pp[] ptrs */
5342 (mx->M+1) * sizeof(float *) + /* mx->Jr_pp[] ptrs */
5343 (mx->M+1) * sizeof(float *) + /* mx->Rr_pp[] ptrs */
5344 mx->l_ncells_alloc * sizeof(float) + /* mx->Jl_pp_mem */
5345 mx->l_ncells_alloc * sizeof(float) + /* mx->Ll_pp_mem */
5346 mx->r_ncells_alloc * sizeof(float) + /* mx->Jr_pp_mem */
5347 mx->r_ncells_alloc * sizeof(float) + /* mx->Rr_pp_mem */
5348 (mx->L+1) * sizeof(float)); /* mx->sum */
5349
5350 mx->size_Mb *= 0.000001; /* convert to Mb */
5351
5352 return mx;
5353
5354 ERROR:
5355 if (mx != NULL) cm_tr_hb_emit_mx_Destroy(mx);
5356 return NULL;
5357 }
5358
5359 /* Function: cm_tr_hb_emit_mx_GrowTo()
5360 * Incept: EPN, Thu Oct 6 06:51:57 2011
5361 *
5362 * Purpose: Assures that a CM_TR_HB_EMIT_MX matrix <mx> is allocated for a
5363 * model of exactly <mx->M> states and target sequence of
5364 * length L, given bands in <cp9b>, reallocating memory as
5365 * necessary.
5366 *
5367 * If local ends are on (cm->flags & CMH_LOCAL_END), allocates
5368 * a full non-banded EL row for the matrix.
5369 *
5370 * Checks to make sure desired matrix isn't too big (see throws).
5371 *
5372 * Args: cm - the CM the matrix is for
5373 * cp9b - HMM bands for current sequence
5374 * mx - the matrix to grow
5375 * errbuf - char buffer for reporting errors
5376 * cp9b - the HMM bands for the target sequence we're growing for
5377 * L - the length of the current target sequence we're aligning
5378 * size_limit- max number of Mb for DP matrix, if matrix is bigger -> return eslERANGE
5379 *
5380 * Returns: <eslOK> on success, and <mx> may be reallocated upon
5381 * return; any data that may have been in <mx> must be
5382 * assumed to be invalidated.
5383 *
5384 * Throws: <eslERANGE> if required size to grow to exceeds <size_limit>.
5385 * This should be caught and appropriately handled by caller.
5386 * <eslEINCOMPAT> on contract violation
5387 * <eslEMEM> on memory allocation error.
5388 */
5389 int
5390 cm_tr_hb_emit_mx_GrowTo(CM_t *cm, CM_TR_HB_EMIT_MX *mx, char *errbuf, CP9Bands_t *cp9b, int L, float size_limit)
5391 {
5392 int status;
5393 void *p;
5394 int v;
5395 int64_t l_cur_size = 0;
5396 int64_t r_cur_size = 0;
5397 int64_t l_ncells;
5398 int64_t r_ncells;
5399 float Mb_needed; /* required size of matrix, given the bands */
5400 float Mb_alloc; /* allocated size of matrix, >= Mb_needed */
5401 int have_el;
5402 int l_realloced; /* did we reallocate mx->l_pp_mem? */
5403 int r_realloced; /* did we reallocate mx->r_pp_mem? */
5404 int sum_realloced; /* did we reallocate mx->sum? */
5405
5406 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
5407
5408 if((status = cm_tr_hb_emit_mx_SizeNeeded(cm, errbuf, cp9b, L, &l_ncells, &r_ncells, &Mb_needed)) != eslOK) return status;
5409 /*printf("HMM banded truncated emit matrix requested size: %.2f Mb\n", Mb_needed);*/
5410 ESL_DPRINTF2(("#DEBUG: HMM banded truncated emit matrix requested size: %.2f Mb\n", Mb_needed));
5411 if(Mb_needed > size_limit) ESL_FAIL(eslERANGE, errbuf, "requested HMM banded emit mx of %.2f Mb > %.2f Mb limit.\nUse --mxsize, --maxtau or --tau.", Mb_needed, (float) size_limit);
5412
5413 /* must we realloc the full matrix? or can we get away
5414 * with just jiggering the pointers, if total required num cells is
5415 * less than or equal to what we already have alloc'ed?
5416 */
5417 l_realloced = FALSE;
5418 r_realloced = FALSE;
5419 sum_realloced = FALSE;
5420 if (l_ncells > mx->l_ncells_alloc) {
5421 ESL_RALLOC(mx->Jl_pp_mem, p, sizeof(float) * l_ncells);
5422 ESL_RALLOC(mx->Ll_pp_mem, p, sizeof(float) * l_ncells);
5423 mx->l_ncells_alloc = l_ncells;
5424 l_realloced = TRUE;
5425 }
5426 if (r_ncells > mx->r_ncells_alloc) {
5427 ESL_RALLOC(mx->Jr_pp_mem, p, sizeof(float) * r_ncells);
5428 ESL_RALLOC(mx->Rr_pp_mem, p, sizeof(float) * r_ncells);
5429 mx->r_ncells_alloc = r_ncells;
5430 r_realloced = TRUE;
5431 }
5432 if (L > mx->L) {
5433 ESL_RALLOC(mx->sum, p, sizeof(float) * (L+1));
5434 sum_realloced = TRUE;
5435 }
5436
5437 /* Determine the size of our matrix based on the size it needed to be (Mb_needed).
5438 */
5439 Mb_alloc = Mb_needed * 1000000; /* convert to bytes */
5440 if(! l_realloced) {
5441 Mb_alloc -= (float) (sizeof(float) * l_ncells); /* Jl_pp */
5442 Mb_alloc -= (float) (sizeof(float) * l_ncells); /* Ll_pp */
5443 Mb_alloc += (float) (sizeof(float) * mx->l_ncells_alloc); /* Jl_pp */
5444 Mb_alloc += (float) (sizeof(float) * mx->l_ncells_alloc); /* Ll_pp */
5445 }
5446 if(! r_realloced) {
5447 Mb_alloc -= (float) (sizeof(float) * r_ncells); /* Jr_pp */
5448 Mb_alloc -= (float) (sizeof(float) * r_ncells); /* Rr_pp */
5449 Mb_alloc += (float) (sizeof(float) * mx->r_ncells_alloc); /* Jr_pp */
5450 Mb_alloc += (float) (sizeof(float) * mx->r_ncells_alloc); /* Rr_pp */
5451 }
5452 if(! sum_realloced) {
5453 Mb_alloc -= (float) (sizeof(float) * (L+1));
5454 Mb_alloc += (float) (sizeof(float) * (mx->L+1));
5455 }
5456 Mb_alloc *= 0.000001; /* convert to Mb */
5457
5458 mx->l_ncells_valid = l_ncells;
5459 mx->r_ncells_valid = r_ncells;
5460
5461 /* reset the pointers, we keep a tally of cur_size as we go
5462 */
5463 l_cur_size = 0;
5464 r_cur_size = 0;
5465 for(v = 0; v < mx->M; v++) {
5466 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) {
5467 mx->Jl_pp[v] = mx->Jl_pp_mem + l_cur_size;
5468 mx->Ll_pp[v] = mx->Ll_pp_mem + l_cur_size;
5469 if(cp9b->imax[v] >= cp9b->imin[v] && cp9b->imax[v] >= 1) {
5470 l_cur_size += cp9b->imax[v] - cp9b->imin[v] + 1;
5471 }
5472 }
5473 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) {
5474 mx->Jr_pp[v] = mx->Jr_pp_mem + r_cur_size;
5475 mx->Rr_pp[v] = mx->Rr_pp_mem + r_cur_size;
5476 if(cp9b->jmax[v] >= cp9b->jmin[v] && cp9b->jmax[v] >= 1) {
5477 r_cur_size += cp9b->jmax[v] - cp9b->jmin[v] + 1;
5478 }
5479 }
5480 }
5481 if(have_el) { /* EL state is non-banded */
5482 mx->Jl_pp[mx->M] = mx->Jl_pp_mem + l_cur_size;
5483 mx->Ll_pp[mx->M] = mx->Ll_pp_mem + l_cur_size;
5484 mx->Rr_pp[mx->M] = mx->Rr_pp_mem + r_cur_size;
5485 l_cur_size += (int64_t) (L+1);
5486 r_cur_size += (int64_t) (L+1);
5487 }
5488 else {
5489 mx->Jl_pp[mx->M] = NULL;
5490 mx->Ll_pp[mx->M] = NULL;
5491 mx->Rr_pp[mx->M] = NULL;
5492 }
5493 mx->Jr_pp[mx->M] = NULL;
5494
5495 #if eslDEBUGLEVEL >= 1
5496 printf("#DEBUG: l_ncells %10" PRId64 " %10" PRId64 "\n", l_cur_size, mx->l_ncells_valid);
5497 printf("#DEBUG: r_ncells %10" PRId64 " %10" PRId64 "\n", r_cur_size, mx->r_ncells_valid);
5498 #endif
5499 assert(l_cur_size == mx->l_ncells_valid);
5500 assert(r_cur_size == mx->r_ncells_valid);
5501 ESL_DASSERT1((l_cur_size == mx->l_ncells_valid));
5502 ESL_DASSERT1((r_cur_size == mx->r_ncells_valid));
5503
5504 /* now update L and size_Mb */
5505 mx->L = L; /* length of current seq we're valid for */
5506 mx->size_Mb = Mb_alloc;
5507 mx->cp9b = cp9b;
5508
5509 return eslOK;
5510
5511 ERROR:
5512 return status;
5513 }
5514
5515 /* Function: cm_tr_hb_emit_mx_Destroy()
5516 * Synopsis: Frees a CM_TR_HB_EMIT_MX.
5517 * Incept: EPN, Thu Oct 6 09:06:58 2011
5518 *
5519 * Purpose: Frees a CM_TR_HB_EMIT_MX.
5520 *
5521 * Returns: (void)
5522 */
5523 void
5524 cm_tr_hb_emit_mx_Destroy(CM_TR_HB_EMIT_MX *mx)
5525 {
5526 if (mx == NULL) return;
5527
5528 if (mx->Jl_pp != NULL) free(mx->Jl_pp);
5529 if (mx->Ll_pp != NULL) free(mx->Ll_pp);
5530 if (mx->Jr_pp != NULL) free(mx->Jr_pp);
5531 if (mx->Rr_pp != NULL) free(mx->Rr_pp);
5532 if (mx->Jl_pp_mem != NULL) free(mx->Jl_pp_mem);
5533 if (mx->Ll_pp_mem != NULL) free(mx->Ll_pp_mem);
5534 if (mx->Jr_pp_mem != NULL) free(mx->Jr_pp_mem);
5535 if (mx->Rr_pp_mem != NULL) free(mx->Rr_pp_mem);
5536 if (mx->sum != NULL) free(mx->sum);
5537
5538 /* don't free cp9b, that's just a reference */
5539 free(mx);
5540 return;
5541 }
5542
5543 /* Function: cm_tr_hb_emit_mx_Dump()
5544 * Synopsis: Dump a HMM banded emit matrix to a stream, for diagnostics.
5545 * Incept: EPN, Thu Oct 6 09:07:35 2011
5546 *
5547 * Purpose: Dump matrix <mx> to stream <fp> for diagnostics.
5548 */
5549 int
5550 cm_tr_hb_emit_mx_Dump(FILE *ofp, CM_t *cm, CM_TR_HB_EMIT_MX *mx, char mode, int print_mx)
5551 {
5552 int status;
5553 int v, i, j, ip_v, jp_v;
5554 int fill_L, fill_R, fill_T; /* are the L, R, and T matrices valid? */
5555
5556 fprintf(ofp, "M: %d\n", mx->M);
5557 fprintf(ofp, "L: %d\n", mx->L);
5558 fprintf(ofp, "l_ncells_alloc: %" PRId64 "\nl_ncells_valid: %" PRId64 "\n", mx->l_ncells_alloc, mx->l_ncells_valid);
5559 fprintf(ofp, "r_ncells_alloc: %" PRId64 "\nr_ncells_valid: %" PRId64 "\n", mx->r_ncells_alloc, mx->r_ncells_valid);
5560 fprintf(ofp, "mode: %d\n", mode);
5561
5562 if((status = cm_TrFillFromMode(mode, &fill_L, &fill_R, &fill_T)) != eslOK) return status;
5563
5564 if(print_mx) {
5565 /* l_pp and r_pp matrix data */
5566 for (v = 0; v < mx->M; v++) {
5567 if(mx->Jl_pp[v]) {
5568 for(i = mx->cp9b->imin[v]; i <= mx->cp9b->imax[v]; i++) {
5569 ip_v = i - mx->cp9b->imin[v];
5570 fprintf(ofp, "Jl_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", v, i, sreEXP2(mx->Jl_pp[v][ip_v]), mx->Jl_pp[v][ip_v], Nodetype(cm->ndtype[cm->ndidx[v]]), Statetype(cm->sttype[v]));
5571 }
5572 }
5573 if(mx->Ll_pp[v] && fill_L) {
5574 for(i = mx->cp9b->imin[v]; i <= mx->cp9b->imax[v]; i++) {
5575 ip_v = i - mx->cp9b->imin[v];
5576 fprintf(ofp, "Ll_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", v, i, sreEXP2(mx->Ll_pp[v][ip_v]), mx->Ll_pp[v][ip_v], Nodetype(cm->ndtype[cm->ndidx[v]]), Statetype(cm->sttype[v]));
5577 }
5578 }
5579 if(mx->Jr_pp[v]) {
5580 for(j = mx->cp9b->jmin[v]; j <= mx->cp9b->jmax[v]; j++) {
5581 jp_v = j - mx->cp9b->jmin[v];
5582 fprintf(ofp, "Jr_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", v, j, sreEXP2(mx->Jr_pp[v][jp_v]), mx->Jr_pp[v][jp_v], Nodetype(cm->ndtype[cm->ndidx[v]]), Statetype(cm->sttype[v]));
5583 }
5584 }
5585 if(mx->Rr_pp[v] && fill_R) {
5586 for(j = mx->cp9b->jmin[v]; j <= mx->cp9b->jmax[v]; j++) {
5587 jp_v = j - mx->cp9b->jmin[v];
5588 fprintf(ofp, "Rr_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", v, j, sreEXP2(mx->Rr_pp[v][jp_v]), mx->Rr_pp[v][jp_v], Nodetype(cm->ndtype[cm->ndidx[v]]), Statetype(cm->sttype[v]));
5589 }
5590 }
5591 fprintf(ofp, "\n");
5592 }
5593
5594 /* EL state */
5595 for(i = 0; i <= mx->L; i++) {
5596 if(mx->Jl_pp[cm->M]) fprintf(ofp, "Jl_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", cm->M, i, sreEXP2(mx->Jl_pp[cm->M][i]), mx->Jl_pp[cm->M][i], "EL", Statetype(cm->sttype[v]));
5597 if(mx->Ll_pp[cm->M] && fill_L) fprintf(ofp, "Ll_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", cm->M, i, sreEXP2(mx->Ll_pp[cm->M][i]), mx->Ll_pp[cm->M][i], "EL", Statetype(cm->sttype[v]));
5598 if(mx->Rr_pp[cm->M] && fill_R) fprintf(ofp, "Rr_pp[v:%5d][i:%5d] %8.4f (2^%8.4f) (%4s %2s)\n", cm->M, i, sreEXP2(mx->Rr_pp[cm->M][i]), mx->Rr_pp[cm->M][i], "EL", Statetype(cm->sttype[v]));
5599 }
5600 }
5601 return eslOK;
5602 }
5603
5604 /* Function: cm_tr_hb_emit_mx_SizeNeeded()
5605 * Incept: EPN, Thu Oct 6 09:11:15 2011
5606 *
5607 * Purpose: Given a model, sequence length, and HMM bands object,
5608 * determine the number of cells and total size in Mb
5609 * required in a CM_TR_HB_EMIT_MX for the target sequence.
5610 *
5611 * Return number of l_pp, r_pp cells required in
5612 * <ret_l_ncells> and <ret_r_ncells> and size of required
5613 * matrix in Mb in <ret_Mb>.
5614 *
5615 * Args: cm - the CM the matrix is for
5616 * errbuf - char buffer for reporting errors
5617 * cp9b - the HMM bands for this sequence
5618 * L - the length of the current target sequence we're aligning
5619 * ret_l_ncells - RETURN: number of matrix cells required
5620 * ret_r_ncells - RETURN: number of matrix cells required
5621 * ret_Mb - RETURN: required size of matrix in Mb
5622 *
5623 * Returns: <eslOK> on success
5624 *
5625 */
5626 int
5627 cm_tr_hb_emit_mx_SizeNeeded(CM_t *cm, char *errbuf, CP9Bands_t *cp9b, int L, int64_t *ret_l_ncells, int64_t *ret_r_ncells, float *ret_Mb)
5628 {
5629 int v;
5630 int64_t l_ncells;
5631 int64_t r_ncells;
5632 int have_el;
5633 float Mb_needed;
5634 have_el = (cm->flags & CMH_LOCAL_END) ? TRUE : FALSE;
5635
5636 l_ncells = 0;
5637 r_ncells = 0;
5638
5639 Mb_needed = (float)
5640 (sizeof(CM_TR_HB_EMIT_MX) +
5641 (cm->M+1) * sizeof(float *) + /* mx->Jl_pp ptrs */
5642 (cm->M+1) * sizeof(float *) + /* mx->Ll_pp ptrs */
5643 (cm->M+1) * sizeof(float *) + /* mx->Jr_pp ptrs */
5644 (cm->M+1) * sizeof(float *)); /* mx->Rr_pp ptrs */
5645
5646 for(v = 0; v < cm->M; v++) {
5647 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) {
5648 l_ncells += cp9b->imax[v] - cp9b->imin[v] + 1;
5649 }
5650 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) {
5651 r_ncells += cp9b->jmax[v] - cp9b->jmin[v] + 1;
5652 }
5653 }
5654 if(have_el) {
5655 l_ncells += L+1; /* space for EL deck */
5656 r_ncells += L+1; /* space for EL deck */
5657 }
5658
5659 Mb_needed += sizeof(float) * (l_ncells + l_ncells + r_ncells + r_ncells + (int64_t) (L+1));
5660 /* mx->Jl_pp_mem, mx->Ll_pp_mem, mx->Jr_pp_mem, mx->Rr_pp_mem, mx->sum */
5661 Mb_needed *= 0.000001; /* convert to megabytes */
5662
5663 if(ret_l_ncells != NULL) *ret_l_ncells = l_ncells;
5664 if(ret_r_ncells != NULL) *ret_r_ncells = r_ncells;
5665 if(ret_Mb != NULL) *ret_Mb = Mb_needed;
5666
5667 return eslOK;
5668 }
5669
5670 /*****************************************************************
5671 * 13. CM_SCAN_MX data structure functions,
5672 * auxiliary info and matrix of float and/or int scores for
5673 * query dependent banded or non-banded CM DP search functions
5674 *****************************************************************/
5675
5676 /* Function: cm_scan_mx_Create()
5677 * Date: EPN, Sun Nov 4 19:56:58 2007
5678 *
5679 * Purpose: Given relevant info, allocate and initialize CM_SCAN_MX.
5680 *
5681 * Returns: eslOK on success.
5682 * eslEINVAL upon contract error, *ret_smx is set to NULL.
5683 * eslEMEM if out of memory, *ret_smx is set to NULL.
5684 */
5685 int
5686 cm_scan_mx_Create(CM_t *cm, char *errbuf, int do_float, int do_int, CM_SCAN_MX **ret_smx)
5687 {
5688 int status;
5689 CM_SCAN_MX *smx = NULL;
5690 int v,j,n;
5691
5692 if((!do_float) && (!do_int)) ESL_FAIL(eslEINVAL, errbuf, "cm_scan_mx_Create(), do_float and do_int both FALSE.");
5693 if(cm->qdbinfo == NULL || cm->qdbinfo->setby == CM_QDBINFO_SETBY_INIT || cm->qdbinfo->setby == CM_QDBINFO_SETBY_SUBINIT) {
5694 ESL_FAIL(eslEINVAL, errbuf, "cm_scan_mx_Create(), qdbinfo is invalid");
5695 }
5696
5697 ESL_ALLOC(smx, sizeof(CM_SCAN_MX));
5698
5699 /* copy M and W */
5700 smx->M = cm->M;
5701 smx->W = cm->W;
5702
5703 /* point to the all-important qdbinfo */
5704 smx->qdbinfo = cm->qdbinfo;
5705
5706 /* Allocate dnAAA and dxAAA */
5707 ESL_ALLOC(smx->dnAAA, sizeof(int **) * NSMX_QDB_IDX);
5708 ESL_ALLOC(smx->dxAAA, sizeof(int **) * NSMX_QDB_IDX);
5709 for(n = 0; n < NSMX_QDB_IDX; n++) {
5710 ESL_ALLOC(smx->dnAAA[n], sizeof(int *) * (smx->W+1));
5711 ESL_ALLOC(smx->dxAAA[n], sizeof(int *) * (smx->W+1));
5712 for(j = 1; j <= smx->W; j++) {
5713 ESL_ALLOC(smx->dnAAA[n][j], sizeof(int) * smx->M);
5714 ESL_ALLOC(smx->dxAAA[n][j], sizeof(int) * smx->M);
5715 }
5716 }
5717
5718 /* For each set of bands, precalculate minimum and maximum d for
5719 * each state and each sequence index (1..j..W). this is not always
5720 * just dmin, dmax, (for ex. if j < W).
5721 */
5722 smx->dnAAA[SMX_NOQDB][0] = smx->dxAAA[SMX_NOQDB][0] = NULL; /* corresponds to j == 0, which is out of bounds */
5723 smx->dnAAA[SMX_QDB1_TIGHT][0] = smx->dxAAA[SMX_QDB1_TIGHT][0] = NULL; /* corresponds to j == 0, which is out of bounds */
5724 smx->dnAAA[SMX_QDB2_LOOSE][0] = smx->dxAAA[SMX_QDB2_LOOSE][0] = NULL; /* corresponds to j == 0, which is out of bounds */
5725 for(j = 1; j <= smx->W; j++) {
5726 for(v = 0; v < smx->M; v++) {
5727 smx->dnAAA[SMX_NOQDB][j][v] = (cm->sttype[v] == MP_st) ? 2 : 1;
5728 smx->dnAAA[SMX_QDB1_TIGHT][j][v] = (cm->sttype[v] == MP_st) ? ESL_MAX(smx->qdbinfo->dmin1[v], 2) : ESL_MAX(smx->qdbinfo->dmin1[v], 1);
5729 smx->dnAAA[SMX_QDB2_LOOSE][j][v] = (cm->sttype[v] == MP_st) ? ESL_MAX(smx->qdbinfo->dmin2[v], 2) : ESL_MAX(smx->qdbinfo->dmin2[v], 1);
5730
5731 smx->dnAAA[SMX_NOQDB][j][v] = ESL_MIN(smx->dnAAA[SMX_NOQDB][j][v], smx->W);
5732 smx->dnAAA[SMX_QDB1_TIGHT][j][v] = ESL_MIN(smx->dnAAA[SMX_QDB1_TIGHT][j][v], smx->W);
5733 smx->dnAAA[SMX_QDB2_LOOSE][j][v] = ESL_MIN(smx->dnAAA[SMX_QDB2_LOOSE][j][v], smx->W);
5734
5735 smx->dxAAA[SMX_NOQDB][j][v] = j;
5736 smx->dxAAA[SMX_QDB1_TIGHT][j][v] = ESL_MIN(j, ESL_MIN(smx->qdbinfo->dmax1[v], smx->W));
5737 smx->dxAAA[SMX_QDB2_LOOSE][j][v] = ESL_MIN(j, ESL_MIN(smx->qdbinfo->dmax2[v], smx->W));
5738 }
5739 }
5740
5741 /* allocate bestr and bestsc */
5742 ESL_ALLOC(smx->bestr, (sizeof(int) * (smx->W+1)));
5743 ESL_ALLOC(smx->bestsc, (sizeof(float) * (smx->W+1)));
5744
5745 /* initialize bestr, bestsc (probably not strictly necessary) */
5746 esl_vec_ISet(smx->bestr, (smx->W+1), 0);
5747 esl_vec_FSet(smx->bestsc, (smx->W+1), IMPOSSIBLE);
5748
5749 /* Some info about the falpha/ialpha matrix
5750 * The alpha matrix holds data for all states EXCEPT BEGL_S states
5751 * The alpha scanning matrix is indexed [j][v][d].
5752 * j takes values 0 or 1: only the previous (prv) or current (cur) row
5753 * v ranges from 0..M-1 over states in the model.
5754 * d ranges from 0..W over subsequence lengths.
5755 * Note if v is a BEGL_S alpha[j][v] == NULL
5756 * Note that old convention of sharing E memory is no longer,
5757 * each E state has it's own deck.
5758 *
5759 * alpha_begl matrix holds data for ONLY BEGL_S states
5760 * j takes value of 0..W
5761 * v ranges from 0..M-1 over states in the model
5762 * d ranges from 0..W over subsequence lengths.
5763 * Note if v is NOT a BEGL_S alpha_begl[j][v] == NULL
5764 *
5765 * alpha and alpha_begl are allocated in contiguous blocks
5766 * of memory in {f,i}alpha_mem and {f,i}alpha_begl_mem
5767 */
5768
5769 /* Some info on alpha initialization
5770 * We initialize on d=0, subsequences of length 0; these are
5771 * j-independent. Any generating state (P,L,R) is impossible on d=0.
5772 * E=0 for d=0. B,S,D must be calculated.
5773 * Also, for MP, d=1 is impossible.
5774 * Also, for E, all d>0 are impossible.
5775 *
5776 * and, for banding: any cell outside our bands is impossible.
5777 * These inits are never changed in the recursion, so even with the
5778 * rolling, matrix face reuse strategy, this works.
5779 *
5780 * The way we initialize is just to set the entire matrix
5781 * to -INFTY or IMPOSSIBLE (for ints and floats, respectively),
5782 * and then reset those cells that should not be -INFTY or
5783 * IMPOSSIBLE as listed above. This way we don't have to
5784 * step through the bands, setting cells outside them to IMPOSSIBLE
5785 * or -INFY;
5786 */
5787
5788 smx->falpha = NULL;
5789 smx->falpha_begl = NULL;
5790 smx->falpha_mem = NULL;
5791 smx->falpha_begl_mem = NULL;
5792
5793 smx->ialpha = NULL;
5794 smx->ialpha_begl = NULL;
5795 smx->ialpha_mem = NULL;
5796 smx->ialpha_begl_mem = NULL;
5797
5798 smx->ncells_alpha = 0;
5799 smx->ncells_alpha_begl = 0;
5800
5801 if(do_float) {
5802 if((status = cm_scan_mx_floatize(cm, smx, errbuf)) != eslOK) goto ERROR;
5803 }
5804 if(do_int) {
5805 if((status = cm_scan_mx_integerize(cm, smx, errbuf)) != eslOK) goto ERROR;
5806 }
5807
5808 /* tally up size */
5809 smx->size_Mb = (float) sizeof(CM_SCAN_MX);
5810 smx->size_Mb += (float) sizeof(int **) * NSMX_QDB_IDX; /* dnAAA (1st dim) */
5811 smx->size_Mb += (float) sizeof(int **) * NSMX_QDB_IDX; /* dxAAA (1st dim) */
5812 smx->size_Mb += (float) sizeof(int *) * NSMX_QDB_IDX * (smx->W+1); /* dnAAA (2nd dim) */
5813 smx->size_Mb += (float) sizeof(int *) * NSMX_QDB_IDX * (smx->W+1); /* dxAAA (2nd dim) */
5814 smx->size_Mb += (float) sizeof(int) * NSMX_QDB_IDX * (smx->W+1) * smx->M; /* dnAAA (3rd dim) */
5815 smx->size_Mb += (float) sizeof(int) * NSMX_QDB_IDX * (smx->W+1) * smx->M; /* dnAAA (3rd dim) */
5816 smx->size_Mb += (float) sizeof(int) * (smx->W+1); /* bestr */
5817 smx->size_Mb += (float) sizeof(float) * (smx->W+1); /* bestsc */
5818 if(do_float) {
5819 smx->size_Mb += (float) sizeof(float) * smx->ncells_alpha; /* falpha */
5820 smx->size_Mb += (float) sizeof(float) * smx->ncells_alpha_begl; /* falpha_begl */
5821 }
5822 if(do_int) {
5823 smx->size_Mb += (float) sizeof(int) * smx->ncells_alpha; /* ialpha */
5824 smx->size_Mb += (float) sizeof(int) * smx->ncells_alpha_begl; /* ialpha_begl */
5825 }
5826 smx->size_Mb *= 0.000001; /* convert to Mb */
5827
5828 *ret_smx = smx;
5829 return eslOK;
5830
5831 ERROR:
5832 cm_scan_mx_Destroy(cm, smx);
5833 *ret_smx = NULL;
5834 if(status == eslEMEM) ESL_FAIL(status, errbuf, "out of memory (creating scan matrix)");
5835 return status;
5836 }
5837
5838 /* Function: cm_scan_mx_floatize()
5839 * Date: EPN, Wed Nov 7 10:05:55 2007
5840 *
5841 * Returns: eslOK on success.
5842 * eslEINVAL if internal consistency check of cell count fails.
5843 * eslEMEM if out of memory.
5844 */
5845 int
5846 cm_scan_mx_floatize(CM_t *cm, CM_SCAN_MX *smx, char *errbuf)
5847 {
5848 int status;
5849 int j, v;
5850 int n_begl;
5851 int n_non_begl;
5852 int64_t cur_cell;
5853
5854 /* allocate alpha
5855 * we allocate only as many cells as necessary,
5856 * for falpha, we only allocate for non-BEGL_S states,
5857 * for falpha_begl, we only allocate for BEGL_S states
5858 *
5859 * note: deck for the EL state, cm->M is never used for scanners
5860 */
5861 n_begl = 0;
5862 for (v = 0; v < cm->M; v++) if (cm->stid[v] == BEGL_S) n_begl++;
5863 n_non_begl = cm->M - n_begl;
5864
5865 /* allocate falpha */
5866 /* j == 0 v == 0 cells, followed by j == 1 v == 0, then j == 0 v == 1 etc.. */
5867 ESL_ALLOC(smx->falpha, sizeof(float **) * 2);
5868 ESL_ALLOC(smx->falpha[0], sizeof(float *) * (cm->M)); /* we still allocate cm->M ptrs, if v == BEGL_S, falpha[0][v] will be NULL */
5869 ESL_ALLOC(smx->falpha[1], sizeof(float *) * (cm->M)); /* we still allocate cm->M ptrs, if v == BEGL_S, falpha[0][v] will be NULL */
5870 /* define ncells_alpha in two statements to avoid potential overflow
5871 * (this is probably unnec, but is nec for ncells_alpha_begl so stay consistent)
5872 */
5873 smx->ncells_alpha = 2 * n_non_begl;
5874 smx->ncells_alpha *= (smx->W+1);
5875 ESL_ALLOC(smx->falpha_mem, sizeof(float) * smx->ncells_alpha);
5876
5877 cur_cell = 0;
5878 for (v = 0; v < cm->M; v++) {
5879 if (cm->stid[v] != BEGL_S) {
5880 smx->falpha[0][v] = smx->falpha_mem + cur_cell;
5881 cur_cell += smx->W+1;
5882 smx->falpha[1][v] = smx->falpha_mem + cur_cell;
5883 cur_cell += smx->W+1;
5884 }
5885 else {
5886 smx->falpha[0][v] = NULL;
5887 smx->falpha[1][v] = NULL;
5888 }
5889 }
5890 if(cur_cell != smx->ncells_alpha) ESL_FAIL(eslEINVAL, errbuf, "problem laying out float scan matrix");
5891
5892 /* allocate falpha_begl */
5893 /* j == d, v == 0 cells, followed by j == d+1, v == 0, etc. */
5894 ESL_ALLOC(smx->falpha_begl, sizeof(float **) * (smx->W+1));
5895 for (j = 0; j <= smx->W; j++)
5896 ESL_ALLOC(smx->falpha_begl[j], sizeof(float *) * (cm->M)); /* we still allocate cm->M ptrs, if v != BEGL_S, falpha_begl[0][v] will be NULL */
5897 /* define ncells_alpha_begl with three separate statements: */
5898 smx->ncells_alpha_begl = (smx->W+1);
5899 smx->ncells_alpha_begl *= n_begl;
5900 smx->ncells_alpha_begl *= (smx->W+1);
5901 ESL_ALLOC(smx->falpha_begl_mem, sizeof(float) * ESL_MAX(1, smx->ncells_alpha_begl));
5902 /* we used to define ncells_alpha_begl this way:
5903 * smx->ncells_alpha_begl = (smx->W+1) * n_begl * (smx->W+1);
5904 * but that overflows for large models (even though ncells_alpha_begl is an int64_t, I guess
5905 * the temporary value stored on the RHS overflows?). This was bug i40.
5906 */
5907
5908 cur_cell = 0;
5909 for (v = 0; v < cm->M; v++) {
5910 for (j = 0; j <= smx->W; j++) {
5911 if (cm->stid[v] == BEGL_S) {
5912 smx->falpha_begl[j][v] = smx->falpha_begl_mem + cur_cell;
5913 cur_cell += smx->W+1;
5914 }
5915 else smx->falpha_begl[j][v] = NULL;
5916 }
5917 }
5918 if(cur_cell != smx->ncells_alpha_begl) ESL_FAIL(eslEINVAL, errbuf, "problem laying out float scan matrix");
5919
5920 /* set the flag that tells us we've got valid floats */
5921 smx->floats_valid = TRUE;
5922
5923 /* Initialize matrix */
5924 if((status = cm_scan_mx_InitializeFloats(cm, smx, errbuf)) != eslOK) return status;
5925
5926 return eslOK;
5927
5928 ERROR:
5929 ESL_FAIL(eslEMEM, errbuf, "out of memory (creating float scan matrix)");
5930 return status; /* NOT REACHED */
5931 }
5932
5933
5934 /* Function: cm_scan_mx_integerize()
5935 * Date: EPN, Wed Nov 7 10:10:39 2007
5936 *
5937 * Returns: eslOK on success.
5938 * eslEINVAL if internal consistency check of cell count fails.
5939 * eslEMEM if out of memory.
5940 */
5941 int
5942 cm_scan_mx_integerize(CM_t *cm, CM_SCAN_MX *smx, char *errbuf)
5943 {
5944 int status;
5945 int n_begl;
5946 int n_non_begl;
5947 int64_t cur_cell;
5948 int v, j;
5949
5950 /* allocate alpha
5951 * we allocate only as many cells as necessary,
5952 * for ialpha, we only allocate for non-BEGL_S states,
5953 * for ialpha_begl, we only allocate for BEGL_S states
5954 *
5955 * note: deck for the EL state, cm->M is never used for scanners
5956 */
5957 n_begl = 0;
5958 for (v = 0; v < cm->M; v++) if (cm->stid[v] == BEGL_S) n_begl++;
5959 n_non_begl = cm->M - n_begl;
5960
5961 /* allocate ialpha */
5962 /* j == 0 v == 0 cells, followed by j == 1 v == 0, then j == 0 v == 1 etc.. */
5963 ESL_ALLOC(smx->ialpha, sizeof(int **) * 2);
5964 ESL_ALLOC(smx->ialpha[0], sizeof(int *) * (cm->M)); /* we still allocate cm->M ptrs, if v == BEGL_S, ialpha[0][v] will be NULL */
5965 ESL_ALLOC(smx->ialpha[1], sizeof(int *) * (cm->M)); /* we still allocate cm->M ptrs, if v == BEGL_S, ialpha[0][v] will be NULL */
5966 /* define ncells_alpha in two statements to avoid potential overflow
5967 * (this is probably unnec, but is nec for ncells_alpha_begl so stay consistent)
5968 */
5969 smx->ncells_alpha = 2 * n_non_begl;
5970 smx->ncells_alpha *= (smx->W+1);
5971 ESL_ALLOC(smx->ialpha_mem, sizeof(float) * smx->ncells_alpha);
5972
5973 cur_cell = 0;
5974 for (v = 0; v < cm->M; v++) {
5975 if (cm->stid[v] != BEGL_S) {
5976 smx->ialpha[0][v] = smx->ialpha_mem + cur_cell;
5977 cur_cell += smx->W+1;
5978 smx->ialpha[1][v] = smx->ialpha_mem + cur_cell;
5979 cur_cell += smx->W+1;
5980 }
5981 else {
5982 smx->ialpha[0][v] = NULL;
5983 smx->ialpha[1][v] = NULL;
5984 }
5985 }
5986 if(cur_cell != smx->ncells_alpha) ESL_FAIL(eslEINVAL, errbuf, "problem laying out int scan matrix");
5987
5988 /* allocate ialpha_begl */
5989 /* j == d, v == 0 cells, followed by j == d+1, v == 0, etc. */
5990 ESL_ALLOC(smx->ialpha_begl, sizeof(int **) * (smx->W+1));
5991 for (j = 0; j <= smx->W; j++)
5992 ESL_ALLOC(smx->ialpha_begl[j], sizeof(int *) * (cm->M)); /* we still allocate cm->M ptrs, if v != BEGL_S, ialpha_begl[0][v] will be NULL */
5993 /* define ncells_alpha_begl with three separate statements: */
5994 smx->ncells_alpha_begl = (smx->W+1);
5995 smx->ncells_alpha_begl *= n_begl;
5996 smx->ncells_alpha_begl *= (smx->W+1);
5997 ESL_ALLOC(smx->ialpha_begl_mem, sizeof(int) * ESL_MAX(1, smx->ncells_alpha_begl)); // avoid 0 malloc
5998 /* we used to define ncells_alpha_begl this way:
5999 * smx->ncells_alpha_begl = (smx->W+1) * n_begl * (smx->W+1);
6000 * but that overflows for large models (even though ncells_alpha_begl is an int64_t, I guess
6001 * the temporary value stored on the RHS overflows?). This was bug i40.
6002 */
6003 cur_cell = 0;
6004 for (v = 0; v < cm->M; v++) {
6005 for (j = 0; j <= smx->W; j++) {
6006 if (cm->stid[v] == BEGL_S) {
6007 smx->ialpha_begl[j][v] = smx->ialpha_begl_mem + cur_cell;
6008 cur_cell += smx->W+1;
6009 }
6010 else smx->ialpha_begl[j][v] = NULL;
6011 }
6012 }
6013 if(cur_cell != smx->ncells_alpha_begl) ESL_FAIL(eslEINVAL, errbuf, "problem laying out int scan matrix");
6014
6015 /* set the flag that tells us we've got valid ints */
6016 smx->ints_valid = TRUE;
6017
6018 /* Initialize matrix */
6019 if((status = cm_scan_mx_InitializeIntegers(cm, smx, errbuf)) != eslOK) return status;
6020
6021 return eslOK;
6022
6023 ERROR:
6024 ESL_FAIL(eslEMEM, errbuf, "out of memory (creating int scan matrix)");
6025 return status; /* NOT REACHED */
6026 }
6027
6028 /* Function: cm_scan_mx_InitializeFloats()
6029 * Date: EPN, Tue Dec 27 10:41:53 2011
6030 *
6031 * Purpose: Initialize float scores in a CM_SCAN_MX. This
6032 * should be done before using the scan matrix for
6033 * a new target sequence.
6034 *
6035 * Returns: eslOK on success.
6036 * eslEINVAL if float matrix is not allocated.
6037 */
6038 int
6039 cm_scan_mx_InitializeFloats(CM_t *cm, CM_SCAN_MX *smx, char *errbuf)
6040 {
6041 int v, j, y, w, yoffset;
6042
6043 if(! smx->floats_valid) ESL_FAIL(eslEINVAL, errbuf, "cm_scan_mx_InitializeFloats(), smx->floats_valid is FALSE");
6044
6045 /* First, init entire matrix to IMPOSSIBLE */
6046 esl_vec_FSet(smx->falpha_mem, smx->ncells_alpha, IMPOSSIBLE);
6047 esl_vec_FSet(smx->falpha_begl_mem, smx->ncells_alpha_begl, IMPOSSIBLE);
6048
6049 /* Now, initialize cells that should not be IMPOSSIBLE in falpha and falpha_begl */
6050 for(v = cm->M-1; v >= 0; v--) {
6051 if(cm->stid[v] != BEGL_S) {
6052 if (cm->sttype[v] == E_st) {
6053 smx->falpha[0][v][0] = smx->falpha[1][v][0] = 0.;
6054 /* rest of E deck is IMPOSSIBLE, it's already set */
6055 }
6056 else if (cm->sttype[v] == S_st || cm->sttype[v] == D_st) {
6057 y = cm->cfirst[v];
6058 smx->falpha[0][v][0] = cm->endsc[v];
6059 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++)
6060 smx->falpha[0][v][0] = ESL_MAX(smx->falpha[0][v][0], (smx->falpha[0][y+yoffset][0] + cm->tsc[v][yoffset]));
6061 smx->falpha[0][v][0] = ESL_MAX(smx->falpha[0][v][0], IMPOSSIBLE);
6062 }
6063 else if (cm->sttype[v] == B_st) {
6064 w = cm->cfirst[v]; /* BEGL_S, left child state */
6065 y = cm->cnum[v];
6066 smx->falpha[0][v][0] = smx->falpha_begl[0][w][0] + smx->falpha[0][y][0];
6067 }
6068 smx->falpha[1][v][0] = smx->falpha[0][v][0];
6069 }
6070 else { /* v == BEGL_S */
6071 y = cm->cfirst[v];
6072 smx->falpha_begl[0][v][0] = cm->endsc[v];
6073 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++)
6074 smx->falpha_begl[0][v][0] = ESL_MAX(smx->falpha_begl[0][v][0], (smx->falpha[0][y+yoffset][0] + cm->tsc[v][yoffset])); /* careful: y is in smx->falpha */
6075 smx->falpha_begl[0][v][0] = ESL_MAX(smx->falpha_begl[0][v][0], IMPOSSIBLE);
6076 for (j = 1; j <= smx->W; j++)
6077 smx->falpha_begl[j][v][0] = smx->falpha_begl[0][v][0];
6078 }
6079 }
6080
6081 return eslOK;
6082 }
6083
6084 /* Function: cm_scan_mx_InitializeIntegers()
6085 * Date: EPN, Tue Dec 27 10:45:02 2011
6086 *
6087 * Purpose: Initialize integer scores in a CM_SCAN_MX. This
6088 * should be done before using the scan matrix for
6089 * a new target sequence.
6090 *
6091 * Returns: eslOK on success.
6092 * eslEINVAL if float matrix is not allocated.
6093 */
6094 int
6095 cm_scan_mx_InitializeIntegers(CM_t *cm, CM_SCAN_MX *smx, char *errbuf)
6096 {
6097 int v, j, y, w, yoffset;
6098
6099 if(! smx->ints_valid) ESL_FAIL(eslEINVAL, errbuf, "cm_scan_mx_InitializeIntegers(), smx->ints_valid is FALSE");
6100
6101 /* First, init entire matrix to -INFTY */
6102 esl_vec_ISet(smx->ialpha_mem, smx->ncells_alpha, -INFTY);
6103 esl_vec_ISet(smx->ialpha_begl_mem, smx->ncells_alpha_begl, -INFTY);
6104
6105 /* Now, initialize cells that should not be -INFTY in ialpha and ialpha_begl */
6106 for(v = cm->M-1; v >= 0; v--) {
6107 if(cm->stid[v] != BEGL_S) {
6108 if (cm->sttype[v] == E_st) {
6109 smx->ialpha[0][v][0] = smx->ialpha[1][v][0] = 0.;
6110 /* rest of E deck is -INFTY, it's already set */
6111 }
6112 else if (cm->sttype[v] == S_st || cm->sttype[v] == D_st) {
6113 y = cm->cfirst[v];
6114 smx->ialpha[0][v][0] = cm->iendsc[v];
6115 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++)
6116 smx->ialpha[0][v][0] = ESL_MAX(smx->ialpha[0][v][0], (smx->ialpha[0][y+yoffset][0] + cm->itsc[v][yoffset]));
6117 smx->ialpha[0][v][0] = ESL_MAX(smx->ialpha[0][v][0], -INFTY);
6118 }
6119 else if (cm->sttype[v] == B_st) {
6120 w = cm->cfirst[v]; /* BEGL_S, left child state */
6121 y = cm->cnum[v];
6122 smx->ialpha[0][v][0] = smx->ialpha_begl[0][w][0] + smx->ialpha[0][y][0];
6123 }
6124 smx->ialpha[1][v][0] = smx->ialpha[0][v][0];
6125 }
6126 else { /* v == BEGL_S */
6127 y = cm->cfirst[v];
6128 smx->ialpha_begl[0][v][0] = cm->iendsc[v];
6129 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++)
6130 smx->ialpha_begl[0][v][0] = ESL_MAX(smx->ialpha_begl[0][v][0], (smx->ialpha[0][y+yoffset][0] + cm->itsc[v][yoffset])); /* careful: y is in alpha */
6131 smx->ialpha_begl[0][v][0] = ESL_MAX(smx->ialpha_begl[0][v][0], -INFTY);
6132 for (j = 1; j <= smx->W; j++)
6133 smx->ialpha_begl[j][v][0] = smx->ialpha_begl[0][v][0];
6134 }
6135 }
6136
6137 return eslOK;
6138 }
6139
6140 /* Function: cm_scan_mx_SizeNeeded()
6141 * Date: EPN, Tue Dec 13 04:33:42 2011
6142 *
6143 * Purpose: Predict the size needed in Mb to create a
6144 * CM_SCAN_MX for <cm>.
6145 *
6146 * Returns: Size needed in Mb.
6147 */
6148 float
6149 cm_scan_mx_SizeNeeded(CM_t *cm, int do_float, int do_int)
6150 {
6151 int n_begl = 0;
6152 int n_non_begl = 0;
6153 int64_t ncells_alpha = 0;
6154 int64_t ncells_alpha_begl = 0;
6155 int v;
6156 float Mb_needed = 0.;
6157
6158 /* calculate number of cells in alpha and alpha_begl */
6159 n_begl = 0;
6160 for (v = 0; v < cm->M; v++) if (cm->stid[v] == BEGL_S) n_begl++;
6161 n_non_begl = cm->M - n_begl;
6162 /* use several statements to calculate ncells_alpha_*, otherwise we could overflow,
6163 * (the overflow was part of bug i40).
6164 */
6165 ncells_alpha = 2;
6166 ncells_alpha *= n_non_begl;
6167 ncells_alpha *= (cm->W+1);
6168 ncells_alpha_begl = (cm->W+1);
6169 ncells_alpha_begl *= n_begl;
6170 ncells_alpha_begl *= (cm->W+1);
6171
6172 /* tally up size */
6173 Mb_needed = (float) sizeof(CM_SCAN_MX);
6174 Mb_needed += (float) sizeof(int **) * NSMX_QDB_IDX; /* dnAAA (1st dim) */
6175 Mb_needed += (float) sizeof(int **) * NSMX_QDB_IDX; /* dxAAA (1st dim) */
6176 Mb_needed += (float) sizeof(int *) * NSMX_QDB_IDX * (cm->W+1); /* dnAAA (2nd dim) */
6177 Mb_needed += (float) sizeof(int *) * NSMX_QDB_IDX * (cm->W+1); /* dxAAA (2nd dim) */
6178 Mb_needed += (float) sizeof(int) * NSMX_QDB_IDX * (cm->W+1) * cm->M; /* dnAAA (3rd dim) */
6179 Mb_needed += (float) sizeof(int) * NSMX_QDB_IDX * (cm->W+1) * cm->M; /* dnAAA (3rd dim) */
6180 Mb_needed += (float) sizeof(int) * (cm->W+1); /* bestr */
6181 Mb_needed += (float) sizeof(float) * (cm->W+1); /* bestsc */
6182 if(do_float) {
6183 Mb_needed += (float) sizeof(float) * ncells_alpha; /* falpha */
6184 Mb_needed += (float) sizeof(float) * ncells_alpha_begl; /* falpha_begl */
6185 }
6186 if(do_int) {
6187 Mb_needed += (float) sizeof(int) * ncells_alpha; /* ialpha */
6188 Mb_needed += (float) sizeof(int) * ncells_alpha_begl; /* ialpha_begl */
6189 }
6190 Mb_needed *= 0.000001; /* convert to Mb */
6191
6192 return Mb_needed;
6193 }
6194
6195 /* Function: cm_scan_mx_Destroy()
6196 * Date: EPN, Sun Nov 4 20:57:32 2007
6197 *
6198 * Purpose: Free a CM_SCAN_MX object corresponding
6199 * to CM <cm>.
6200 *
6201 * Returns: void
6202 */
6203 void
6204 cm_scan_mx_Destroy(CM_t *cm, CM_SCAN_MX *smx)
6205 {
6206 int n, j;
6207
6208 for(n = 0; n < NSMX_QDB_IDX; n++) {
6209 for(j = 1; j <= smx->W; j++) {
6210 free(smx->dnAAA[n][j]);
6211 free(smx->dxAAA[n][j]);
6212 }
6213 free(smx->dnAAA[n]);
6214 free(smx->dxAAA[n]);
6215 }
6216 free(smx->dnAAA);
6217 free(smx->dxAAA);
6218 free(smx->bestr);
6219 free(smx->bestsc);
6220
6221 if(smx->floats_valid) cm_scan_mx_freefloats (cm, smx);
6222 if(smx->ints_valid) cm_scan_mx_freeintegers(cm, smx);
6223
6224 free(smx);
6225 return;
6226 }
6227
6228 /* Function: cm_scan_mx_freefloats()
6229 * Date: EPN, Wed Nov 7 10:03:55 2007
6230 *
6231 * Purpose: Free float data structures in a CM_SCAN_MX object
6232 * corresponding to <cm>.
6233 *
6234 * Returns: eslOK on success.
6235 */
6236 int
6237 cm_scan_mx_freefloats(CM_t *cm, CM_SCAN_MX *smx)
6238 {
6239 int j;
6240
6241 if(smx->falpha_mem != NULL) free(smx->falpha_mem);
6242 if(smx->falpha != NULL) {
6243 if(smx->falpha[1] != NULL) free(smx->falpha[1]);
6244 if(smx->falpha[0] != NULL) free(smx->falpha[0]);
6245 free(smx->falpha);
6246 smx->falpha = NULL;
6247 }
6248
6249 if(smx->falpha_begl_mem != NULL) {
6250 free(smx->falpha_begl_mem);
6251 }
6252 if(smx->falpha_begl != NULL) {
6253 for (j = 0; j <= smx->W; j++) {
6254 if(smx->falpha_begl[j] != NULL) free(smx->falpha_begl[j]);
6255 }
6256 free(smx->falpha_begl);
6257 smx->falpha_begl = NULL;
6258 }
6259
6260 smx->floats_valid = FALSE;
6261 return eslOK;
6262 }
6263
6264 /* Function: cm_scan_mx_freeintegers()
6265 * Date: EPN, Wed Nov 7 09:56:01 2007
6266 *
6267 * Purpose: Free int data structures in a CM_SCAN_MX object
6268 * corresponding to <cm>.
6269 *
6270 * Returns: eslOK on success, dies immediately on an error.
6271 */
6272 int
6273 cm_scan_mx_freeintegers(CM_t *cm, CM_SCAN_MX *smx)
6274 {
6275 int j;
6276
6277 if(smx->ialpha_mem != NULL) free(smx->ialpha_mem);
6278 if(smx->ialpha != NULL) {
6279 if(smx->ialpha[1] != NULL) free(smx->ialpha[1]);
6280 if(smx->ialpha[0] != NULL) free(smx->ialpha[0]);
6281 free(smx->ialpha);
6282 smx->ialpha = NULL;
6283 }
6284
6285 if(smx->ialpha_begl_mem != NULL) {
6286 free(smx->ialpha_begl_mem);
6287 }
6288 if(smx->ialpha_begl != NULL) {
6289 for (j = 0; j <= smx->W; j++) {
6290 if(smx->ialpha_begl[j] != NULL) free(smx->ialpha_begl[j]);
6291 }
6292 free(smx->ialpha_begl);
6293 smx->ialpha_begl = NULL;
6294 }
6295
6296 smx->ints_valid = FALSE;
6297 return eslOK;
6298 }
6299
6300 /* Function: cm_scan_mx_Dump()
6301 * Date: EPN, Tue Nov 6 05:11:26 2007
6302 *
6303 * Purpose: Dump current alpha matrix (either float or int).
6304 *
6305 * Returns: void, dies upon an error.
6306 */
6307 void
6308 cm_scan_mx_Dump(FILE *ofp, CM_t *cm, int j, int i0, int qdbidx, int doing_float)
6309 {
6310 int d, v;
6311 int jp_g = j-i0+1; /* j is actual index in j, jp_g is offset j relative to start i0 (index in gamma* data structures) */
6312 int cur = j%2;
6313 int prv = (j-1)%2;
6314 int *dnA, *dxA;
6315
6316 CM_SCAN_MX *smx = cm->smx;
6317 if( doing_float && (! smx->floats_valid)) cm_Fail("cm_scan_mx_Dump(), trying to print float alpha, but floats are not valid");
6318 if((! doing_float) && (! smx->ints_valid)) cm_Fail("cm_scan_mx_Dump(), trying to print int alpha, but ints are not valid");
6319
6320 int begl_prv = j-1 % (smx->W+1);
6321 int begl_cur = j % (smx->W+1);
6322
6323 fprintf(ofp, "Dumping Alpha: j: %d\n", j);
6324 if(jp_g >= smx->W) { dnA = smx->dnAAA[qdbidx][smx->W]; dxA = smx->dxAAA[qdbidx][smx->W]; }
6325 else { dnA = smx->dnAAA[qdbidx][jp_g]; dxA = smx->dxAAA[qdbidx][jp_g]; }
6326 if(doing_float) {
6327 for (v = smx->M-1; v >= 0; v--) {
6328 if (cm->stid[v] == BEGL_S) {
6329 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "A[j-1:%4d][%4d][%4d]: %10.4f\n", (j-1), v, d, smx->falpha_begl[begl_prv][v][d]);
6330 }
6331 else {
6332 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "A[j-1:%4d][%4d][%4d]: %10.4f\n", (j-1), v, d, smx->falpha[prv][v][d]);
6333 }
6334 fprintf(ofp, "\n");
6335 }
6336 for (v = smx->M-1; v >= 0; v--) {
6337 if (cm->stid[v] == BEGL_S) {
6338 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "A[j :%4d][%4d][%4d]: %10.4f\n", j, v, d, smx->falpha_begl[begl_cur][v][d]);
6339 }
6340 else {
6341 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "A[j :%4d][%4d][%4d]: %10.4f\n", j, v, d, smx->falpha[cur][v][d]);
6342 }
6343 fprintf(ofp, "\n");
6344 }
6345 }
6346 else { /* doing int */
6347 for (v = smx->M-1; v >= 0; v--) {
6348 if (cm->stid[v] == BEGL_S) {
6349 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "A[j-1:%4d][%4d][%4d]: %10d\n", (j-1), v, d, smx->ialpha_begl[begl_prv][v][d]);
6350 }
6351 else {
6352 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "A[j-1:%4d][%4d][%4d]: %10d\n", (j-1), v, d, smx->ialpha[prv][v][d]);
6353 }
6354 fprintf(ofp, "\n\n");
6355 }
6356 for (v = smx->M-1; v >= 0; v--) {
6357 if (cm->stid[v] == BEGL_S) {
6358 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "A[j :%4d][%4d][%4d]: %10d\n", j, v, d, smx->ialpha_begl[begl_cur][v][d]);
6359 }
6360 else {
6361 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "A[j :%4d][%4d][%4d]: %10d\n", j, v, d, smx->ialpha[cur][v][d]);
6362 }
6363 fprintf(ofp, "\n\n");
6364 }
6365 }
6366 return;
6367 }
6368
6369 /*****************************************************************
6370 * 14. CM_TR_SCAN_MX data structure functions,
6371 * auxiliary info and matrix of float and/or int scores for
6372 * truncated query dependent banded or non-banded CM DP search
6373 * functions.
6374 *****************************************************************/
6375
6376 /* Function: cm_tr_scan_mx_Create()
6377 * Date: EPN, Tue Aug 16 04:23:41 2011
6378 *
6379 * Purpose: Given relevant info, allocate and initialize
6380 * CM_TR_SCAN_MX object. Note that unlike a CM_SCAN_MX,
6381 * dmin is not used to set minimum values, even if we're
6382 * going to use QDBs, because minimum subtree lengths are
6383 * illogical with the truncated version of CYK/Inside, but
6384 * maximum lengths are not, so <dmax> is considered here.
6385 *
6386 * Returns: eslOK on success.
6387 * eslEINVAL upon contract error, *ret_trsmx is set to NULL.
6388 * eslEMEM if out of memory, *ret_trsmx is set to NULL.
6389 */
6390 int
6391 cm_tr_scan_mx_Create(CM_t *cm, char *errbuf, int do_float, int do_int, CM_TR_SCAN_MX **ret_trsmx)
6392 {
6393 int status;
6394 CM_TR_SCAN_MX *trsmx = NULL;
6395 int v,j,n;
6396
6397 if((!do_float) && (!do_int)) ESL_FAIL(eslEINVAL, errbuf, "cm_tr_scan_mx_Create(), do_float and do_int both FALSE.");
6398 if(cm->qdbinfo == NULL || cm->qdbinfo->setby == CM_QDBINFO_SETBY_INIT || cm->qdbinfo->setby == CM_QDBINFO_SETBY_SUBINIT) {
6399 ESL_FAIL(eslEINVAL, errbuf, "cm_tr_scan_mx_Create(), qdbinfo is invalid");
6400 }
6401
6402 ESL_ALLOC(trsmx, sizeof(CM_TR_SCAN_MX));
6403
6404 /* copy W */
6405 trsmx->W = cm->W;
6406
6407 /* point to the all-important qdbinfo */
6408 trsmx->qdbinfo = cm->qdbinfo;
6409
6410 /* Allocate dnAAA and dxAAA */
6411 ESL_ALLOC(trsmx->dnAAA, sizeof(int **) * NSMX_QDB_IDX);
6412 ESL_ALLOC(trsmx->dxAAA, sizeof(int **) * NSMX_QDB_IDX);
6413 for(n = 0; n < NSMX_QDB_IDX; n++) {
6414 ESL_ALLOC(trsmx->dnAAA[n], sizeof(int *) * (trsmx->W+1));
6415 ESL_ALLOC(trsmx->dxAAA[n], sizeof(int *) * (trsmx->W+1));
6416 for(j = 1; j <= trsmx->W; j++) {
6417 ESL_ALLOC(trsmx->dnAAA[n][j], sizeof(int) * cm->M);
6418 ESL_ALLOC(trsmx->dxAAA[n][j], sizeof(int) * cm->M);
6419 }
6420 }
6421
6422 /* For each set of bands, precalculate minimum and maximum d for
6423 * each state and each sequence index (1..j..W). this is not always
6424 * just dmin, dmax, (for ex. if j < W).
6425 */
6426 trsmx->dnAAA[SMX_NOQDB][0] = trsmx->dxAAA[SMX_NOQDB][0] = NULL; /* corresponds to j == 0, which is out of bounds */
6427 trsmx->dnAAA[SMX_QDB1_TIGHT][0] = trsmx->dxAAA[SMX_QDB1_TIGHT][0] = NULL; /* corresponds to j == 0, which is out of bounds */
6428 trsmx->dnAAA[SMX_QDB2_LOOSE][0] = trsmx->dxAAA[SMX_QDB2_LOOSE][0] = NULL; /* corresponds to j == 0, which is out of bounds */
6429 for(j = 1; j <= trsmx->W; j++) {
6430 for(v = 0; v < cm->M; v++) {
6431 /* dnAAA[j][v] is 1 for all states, even MATP, b/c d == 1 is valid for MATP in L,R matrices */
6432 trsmx->dnAAA[SMX_NOQDB][j][v] = 1;
6433 trsmx->dnAAA[SMX_QDB1_TIGHT][j][v] = 1;
6434 trsmx->dnAAA[SMX_QDB2_LOOSE][j][v] = 1;
6435
6436 trsmx->dxAAA[SMX_NOQDB][j][v] = ESL_MIN(j, trsmx->W);
6437 trsmx->dxAAA[SMX_QDB1_TIGHT][j][v] = ESL_MIN(j, ESL_MIN(trsmx->qdbinfo->dmax1[v], trsmx->W));
6438 trsmx->dxAAA[SMX_QDB2_LOOSE][j][v] = ESL_MIN(j, ESL_MIN(trsmx->qdbinfo->dmax2[v], trsmx->W));
6439 }
6440 }
6441
6442 /* allocate bestr, bestsc, bestmode */
6443 ESL_ALLOC(trsmx->bestr, (sizeof(int) * (trsmx->W+1)));
6444 ESL_ALLOC(trsmx->bestsc, (sizeof(float) * (trsmx->W+1)));
6445 ESL_ALLOC(trsmx->bestmode, (sizeof(char) * (trsmx->W+1)));
6446 /* initialize bestr, bestsc, bestmode (probably not strictly necessary) */
6447 esl_vec_ISet(trsmx->bestr, (trsmx->W+1), 0);
6448 esl_vec_FSet(trsmx->bestsc, (trsmx->W+1), IMPOSSIBLE);
6449 for(j = 0; j <= trsmx->W; j++) trsmx->bestmode[j] = TRMODE_UNKNOWN;
6450
6451 /* Some info about the falpha/ialpha matrix
6452 * The alpha matrix holds data for all states EXCEPT BEGL_S states
6453 * The alpha scanning matrix is indexed [j][v][d].
6454 * j takes values 0 or 1: only the previous (prv) or current (cur) row
6455 * v ranges from 0..M-1 over states in the model.
6456 * d ranges from 0..W over subsequence lengths.
6457 * Note if v is a BEGL_S alpha[j][v] == NULL
6458 * Note that old convention of sharing E memory is no longer,
6459 * each E state has it's own deck.
6460 *
6461 * alpha_begl matrix holds data for ONLY BEGL_S states
6462 * j takes value of 0..W
6463 * v ranges from 0..M-1 over states in the model
6464 * d ranges from 0..W over subsequence lengths.
6465 * Note if v is NOT a BEGL_S alpha_begl[j][v] == NULL
6466 *
6467 * alpha and alpha_begl are allocated in contiguous blocks
6468 * of memory in {f,i}alpha_mem and {f,i}alpha_begl_mem
6469 */
6470
6471 /* Some info on alpha initialization
6472 * We initialize on d=0, subsequences of length 0; these are
6473 * j-independent. Any generating state (P,L,R) is impossible on d=0.
6474 * E=0 for d=0. B,S,D must be calculated.
6475 * Also, for MP, d=1 is impossible.
6476 * Also, for E, all d>0 are impossible.
6477 *
6478 * and, for banding: any cell outside our bands is impossible.
6479 * These inits are never changed in the recursion, so even with the
6480 * rolling, matrix face reuse strategy, this works.
6481 *
6482 * The way we initialize is just to set the entire matrix
6483 * to -INFTY or IMPOSSIBLE (for ints and floats, respectively),
6484 * and then reset those cells that should not be -INFTY or
6485 * IMPOSSIBLE as listed above. This way we don't have to
6486 * step through the bands, setting cells outside them to IMPOSSIBLE
6487 * or -INFY;
6488 */
6489
6490 trsmx->fJalpha = trsmx->fLalpha = trsmx->fRalpha = trsmx->fTalpha = NULL;
6491 trsmx->fJalpha_begl = trsmx->fLalpha_begl = trsmx->fRalpha_begl = NULL;
6492 trsmx->fJalpha_mem = trsmx->fLalpha_mem = trsmx->fRalpha_mem = trsmx->fTalpha_mem = NULL;
6493 trsmx->fJalpha_begl_mem = trsmx->fLalpha_begl_mem = trsmx->fRalpha_begl_mem = NULL;
6494
6495 trsmx->iJalpha = trsmx->iLalpha = trsmx->iRalpha = trsmx->iTalpha = NULL;
6496 trsmx->iJalpha_begl = trsmx->iLalpha_begl = trsmx->iRalpha_begl = NULL;
6497 trsmx->iJalpha_mem = trsmx->iLalpha_mem = trsmx->iRalpha_mem = trsmx->iTalpha_mem = NULL;
6498 trsmx->iJalpha_begl_mem = trsmx->iLalpha_begl_mem = trsmx->iRalpha_begl_mem = NULL;
6499
6500 trsmx->ncells_alpha = 0;
6501 trsmx->ncells_alpha_begl = 0;
6502 trsmx->ncells_Talpha = 0;
6503
6504 if(do_float) {
6505 if((status = cm_tr_scan_mx_floatize(cm, trsmx, errbuf)) != eslOK) goto ERROR;
6506 }
6507 if(do_int) {
6508 if((status = cm_tr_scan_mx_integerize(cm, trsmx, errbuf)) != eslOK) goto ERROR;
6509 }
6510
6511 /* tally up size */
6512 trsmx->size_Mb = (float) sizeof(CM_SCAN_MX);
6513 trsmx->size_Mb += (float) sizeof(int **) * NSMX_QDB_IDX; /* dnAAA (1st dim) */
6514 trsmx->size_Mb += (float) sizeof(int **) * NSMX_QDB_IDX; /* dxAAA (1st dim) */
6515 trsmx->size_Mb += (float) sizeof(int *) * NSMX_QDB_IDX * (trsmx->W+1); /* dnAAA (2nd dim) */
6516 trsmx->size_Mb += (float) sizeof(int *) * NSMX_QDB_IDX * (trsmx->W+1); /* dxAAA (2nd dim) */
6517 trsmx->size_Mb += (float) sizeof(int) * NSMX_QDB_IDX * (trsmx->W+1) * trsmx->M; /* dnAAA (3rd dim) */
6518 trsmx->size_Mb += (float) sizeof(int) * NSMX_QDB_IDX * (trsmx->W+1) * trsmx->M; /* dnAAA (3rd dim) */
6519 trsmx->size_Mb += (float) sizeof(int) * (trsmx->W+1); /* bestr */
6520 trsmx->size_Mb += (float) sizeof(float) * (trsmx->W+1); /* bestsc */
6521 if(do_float) {
6522 trsmx->size_Mb += (float) sizeof(float) * trsmx->ncells_alpha; /* fJalpha */
6523 trsmx->size_Mb += (float) sizeof(float) * trsmx->ncells_alpha; /* fLalpha */
6524 trsmx->size_Mb += (float) sizeof(float) * trsmx->ncells_alpha; /* fRalpha */
6525 trsmx->size_Mb += (float) sizeof(float) * trsmx->ncells_Talpha; /* fTalpha */
6526 trsmx->size_Mb += (float) sizeof(float) * trsmx->ncells_alpha_begl; /* fJalpha_begl */
6527 trsmx->size_Mb += (float) sizeof(float) * trsmx->ncells_alpha_begl; /* fLalpha_begl */
6528 trsmx->size_Mb += (float) sizeof(float) * trsmx->ncells_alpha_begl; /* fRalpha_begl */
6529 }
6530 if(do_int) {
6531 trsmx->size_Mb += (float) sizeof(int) * trsmx->ncells_alpha; /* iJalpha */
6532 trsmx->size_Mb += (float) sizeof(int) * trsmx->ncells_alpha; /* iLalpha */
6533 trsmx->size_Mb += (float) sizeof(int) * trsmx->ncells_alpha; /* iRalpha */
6534 trsmx->size_Mb += (float) sizeof(int) * trsmx->ncells_Talpha; /* iTalpha */
6535 trsmx->size_Mb += (float) sizeof(int) * trsmx->ncells_alpha_begl; /* iJalpha_begl */
6536 trsmx->size_Mb += (float) sizeof(int) * trsmx->ncells_alpha_begl; /* iLalpha_begl */
6537 trsmx->size_Mb += (float) sizeof(int) * trsmx->ncells_alpha_begl; /* iRalpha_begl */
6538 }
6539 trsmx->size_Mb *= 0.000001; /* convert to Mb */
6540
6541 *ret_trsmx = trsmx;
6542 return eslOK;
6543
6544 ERROR:
6545 cm_tr_scan_mx_Destroy(cm, trsmx);
6546 *ret_trsmx = NULL;
6547 if(status == eslEMEM) ESL_FAIL(status, errbuf, "out of memory (creating tr scan matrix)");
6548 return status;
6549 }
6550
6551 /* Function: cm_tr_scan_mx_floatize()
6552 * Date: EPN, Tue Aug 16 04:36:29 2011
6553 *
6554 * Returns: eslOK on success.
6555 * eslEINVAL if internal consistency check of cell count fails.
6556 * eslEMEM if out of memory.
6557 */
6558 int
6559 cm_tr_scan_mx_floatize(CM_t *cm, CM_TR_SCAN_MX *trsmx, char *errbuf)
6560 {
6561 int status;
6562 int j, v;
6563 int n_begl, n_bif;
6564 int n_non_begl;
6565 int64_t cur_cell;
6566
6567 /* allocate alpha
6568 * we allocate only as many cells as necessary,
6569 * for f{J,L,R,T}alpha, we only allocate for non-BEGL_S states,
6570 * for f{J,L,R,T}alpha_begl, we only allocate for BEGL_S states
6571 * for fTalpha, we only allocate for BIF_B and ROOT_S states
6572 *
6573 * note: deck for the EL state, cm->M is never used for scanners
6574 */
6575 n_begl = 0;
6576 n_bif = 0;
6577 for (v = 0; v < cm->M; v++) if (cm->stid[v] == BEGL_S) n_begl++;
6578 for (v = 0; v < cm->M; v++) if (cm->stid[v] == BIF_B) n_bif++;
6579 n_non_begl = cm->M - n_begl;
6580
6581 /* allocate f{J,L,R,T}alpha */
6582 /* j == 0 v == 0 cells, followed by j == 1 v == 0, then j == 0 v == 1 etc.. */
6583 ESL_ALLOC(trsmx->fJalpha, sizeof(float **) * 2);
6584 ESL_ALLOC(trsmx->fJalpha[0], sizeof(float *) * (cm->M)); /* we still allocate cm->M ptrs, if v == BEGL_S, fJalpha[0][v] will be NULL */
6585 ESL_ALLOC(trsmx->fJalpha[1], sizeof(float *) * (cm->M)); /* we still allocate cm->M ptrs, if v == BEGL_S, fJalpha[1][v] will be NULL */
6586 ESL_ALLOC(trsmx->fJalpha_mem, sizeof(float) * 2 * n_non_begl * (trsmx->W+1));
6587
6588 ESL_ALLOC(trsmx->fLalpha, sizeof(float **) * 2);
6589 ESL_ALLOC(trsmx->fLalpha[0], sizeof(float *) * (cm->M)); /* we still allocate cm->M ptrs, if v == BEGL_S, fLalpha[0][v] will be NULL */
6590 ESL_ALLOC(trsmx->fLalpha[1], sizeof(float *) * (cm->M)); /* we still allocate cm->M ptrs, if v == BEGL_S, fLalpha[1][v] will be NULL */
6591 ESL_ALLOC(trsmx->fLalpha_mem, sizeof(float) * 2 * n_non_begl * (trsmx->W+1));
6592
6593 ESL_ALLOC(trsmx->fRalpha, sizeof(float **) * 2);
6594 ESL_ALLOC(trsmx->fRalpha[0], sizeof(float *) * (cm->M)); /* we still allocate cm->M ptrs, if v == BEGL_S, fRalpha[0][v] will be NULL */
6595 ESL_ALLOC(trsmx->fRalpha[1], sizeof(float *) * (cm->M)); /* we still allocate cm->M ptrs, if v == BEGL_S, fRalpha[1][v] will be NULL */
6596 ESL_ALLOC(trsmx->fRalpha_mem, sizeof(float) * 2 * n_non_begl * (trsmx->W+1));
6597
6598 ESL_ALLOC(trsmx->fTalpha, sizeof(float **) * 2);
6599 ESL_ALLOC(trsmx->fTalpha[0], sizeof(float *) * (cm->M)); /* we still allocate cm->M ptrs, if v != BIF_B and v != ROOT_S, fTalpha[0][v] will be NULL */
6600 ESL_ALLOC(trsmx->fTalpha[1], sizeof(float *) * (cm->M)); /* we still allocate cm->M ptrs, if v != BIF_B and v != ROOT_S, fTalpha[1][v] will be NULL */
6601 ESL_ALLOC(trsmx->fTalpha_mem, sizeof(float) * 2 * n_non_begl * (trsmx->W+1));
6602
6603 /* define ncells_alpha in two statements to avoid potential overflow
6604 * (this is probably unnec, but is nec for ncells_alpha_begl so stay consistent)
6605 */
6606 trsmx->ncells_alpha = 2 * n_non_begl;
6607 trsmx->ncells_alpha *= (trsmx->W+1);
6608 cur_cell = 0;
6609 for (v = 0; v < cm->M; v++) {
6610 if (cm->stid[v] != BEGL_S) {
6611 trsmx->fJalpha[0][v] = trsmx->fJalpha_mem + cur_cell;
6612 trsmx->fLalpha[0][v] = trsmx->fLalpha_mem + cur_cell;
6613 trsmx->fRalpha[0][v] = trsmx->fRalpha_mem + cur_cell;
6614 cur_cell += trsmx->W+1;
6615 trsmx->fJalpha[1][v] = trsmx->fJalpha_mem + cur_cell;
6616 trsmx->fLalpha[1][v] = trsmx->fLalpha_mem + cur_cell;
6617 trsmx->fRalpha[1][v] = trsmx->fRalpha_mem + cur_cell;
6618 cur_cell += trsmx->W+1;
6619 }
6620 else {
6621 trsmx->fJalpha[0][v] = NULL;
6622 trsmx->fJalpha[1][v] = NULL;
6623 trsmx->fLalpha[0][v] = NULL;
6624 trsmx->fLalpha[1][v] = NULL;
6625 trsmx->fRalpha[0][v] = NULL;
6626 trsmx->fRalpha[1][v] = NULL;
6627 }
6628 }
6629 if(cur_cell != trsmx->ncells_alpha) { status = eslEINVAL; goto ERROR; }
6630
6631 trsmx->ncells_Talpha = 2 * (n_bif+1) * (trsmx->W+1);
6632 cur_cell = 0;
6633 for (v = 0; v < cm->M; v++) {
6634 if (cm->stid[v] == BIF_B || cm->stid[v] == ROOT_S) {
6635 trsmx->fTalpha[0][v] = trsmx->fTalpha_mem + cur_cell;
6636 cur_cell += trsmx->W+1;
6637 trsmx->fTalpha[1][v] = trsmx->fTalpha_mem + cur_cell;
6638 cur_cell += trsmx->W+1;
6639 }
6640 else {
6641 trsmx->fTalpha[0][v] = NULL;
6642 trsmx->fTalpha[1][v] = NULL;
6643 }
6644 }
6645 if(cur_cell != trsmx->ncells_Talpha) ESL_FAIL(eslEINVAL, errbuf, "problem laying out float truncated scan matrix");
6646
6647 /* allocate falpha_begl */
6648 /* j == d, v == 0 cells, followed by j == d+1, v == 0, etc. */
6649 ESL_ALLOC(trsmx->fJalpha_begl, sizeof(float **) * (trsmx->W+1));
6650 ESL_ALLOC(trsmx->fLalpha_begl, sizeof(float **) * (trsmx->W+1));
6651 ESL_ALLOC(trsmx->fRalpha_begl, sizeof(float **) * (trsmx->W+1));
6652 for (j = 0; j <= trsmx->W; j++) {
6653 ESL_ALLOC(trsmx->fJalpha_begl[j], sizeof(float *) * (cm->M)); /* we still allocate cm->M ptrs, if v != BEGL_S, fJalpha_begl[0][v] will be NULL */
6654 ESL_ALLOC(trsmx->fLalpha_begl[j], sizeof(float *) * (cm->M)); /* we still allocate cm->M ptrs, if v != BEGL_S, fLalpha_begl[0][v] will be NULL */
6655 ESL_ALLOC(trsmx->fRalpha_begl[j], sizeof(float *) * (cm->M)); /* we still allocate cm->M ptrs, if v != BEGL_S, fRalpha_begl[0][v] will be NULL */
6656 }
6657 /* define ncells_alpha_begl with three separate statements (so we don't overflow, that was bug i40): */
6658 trsmx->ncells_alpha_begl = (trsmx->W+1);
6659 trsmx->ncells_alpha_begl *= n_begl;
6660 trsmx->ncells_alpha_begl *= (trsmx->W+1);
6661 ESL_ALLOC(trsmx->fJalpha_begl_mem, sizeof(float) * ESL_MAX(1, trsmx->ncells_alpha_begl)); // avoid 0 malloc
6662 ESL_ALLOC(trsmx->fLalpha_begl_mem, sizeof(float) * ESL_MAX(1, trsmx->ncells_alpha_begl));
6663 ESL_ALLOC(trsmx->fRalpha_begl_mem, sizeof(float) * ESL_MAX(1, trsmx->ncells_alpha_begl));
6664
6665 cur_cell = 0;
6666 for (v = 0; v < cm->M; v++) {
6667 for (j = 0; j <= trsmx->W; j++) {
6668 if (cm->stid[v] == BEGL_S) {
6669 trsmx->fJalpha_begl[j][v] = trsmx->fJalpha_begl_mem + cur_cell;
6670 trsmx->fLalpha_begl[j][v] = trsmx->fLalpha_begl_mem + cur_cell;
6671 trsmx->fRalpha_begl[j][v] = trsmx->fRalpha_begl_mem + cur_cell;
6672 cur_cell += trsmx->W+1;
6673 }
6674 else {
6675 trsmx->fJalpha_begl[j][v] = NULL;
6676 trsmx->fLalpha_begl[j][v] = NULL;
6677 trsmx->fRalpha_begl[j][v] = NULL;
6678 }
6679 }
6680 }
6681 if(cur_cell != trsmx->ncells_alpha_begl) ESL_FAIL(eslEINVAL, errbuf, "problem laying out truncated float scan matrix");
6682
6683 /* set the flag that tells us we've got valid floats */
6684 trsmx->floats_valid = TRUE;
6685
6686 /* Initialize matrix */
6687 if((status = cm_tr_scan_mx_InitializeFloats(cm, trsmx, errbuf)) != eslOK) return status;
6688
6689 return eslOK;
6690
6691 ERROR:
6692 ESL_FAIL(eslEMEM, errbuf, "out of memory (creating float truncated scan matrix)");
6693 return status; /* NOT REACHED */
6694 }
6695
6696
6697 /* Function: cm_tr_scan_mx_integerize()
6698 * Date: EPN, Wed Aug 24 15:00:32 2011
6699 *
6700 * Returns: eslOK on success.
6701 * eslEINVAL if internal consistency check of cell count fails.
6702 * eslEMEM if out of memory.
6703 */
6704 int
6705 cm_tr_scan_mx_integerize(CM_t *cm, CM_TR_SCAN_MX *trsmx, char *errbuf)
6706 {
6707 int status;
6708 int j, v;
6709 int n_begl, n_bif;
6710 int n_non_begl;
6711 int64_t cur_cell;
6712
6713 /* allocate alpha
6714 * we allocate only as many cells as necessary,
6715 * for i{J,L,R}alpha, we only allocate for non-BEGL_S states,
6716 * for i{J,L,R}alpha_begl, we only allocate for BEGL_S states
6717 * for iTalpha, we only allocate for BIF_B and ROOT_S states
6718 *
6719 * note: deck for the EL state, cm->M is never used for scanners
6720 */
6721 n_begl = 0;
6722 n_bif = 0;
6723 for (v = 0; v < cm->M; v++) if (cm->stid[v] == BEGL_S) n_begl++;
6724 for (v = 0; v < cm->M; v++) if (cm->stid[v] == BIF_B) n_bif++;
6725 n_non_begl = cm->M - n_begl;
6726
6727 /* allocate f{J,L,R,T}alpha */
6728 /* j == 0 v == 0 cells, followed by j == 1 v == 0, then j == 0 v == 1 etc.. */
6729 ESL_ALLOC(trsmx->iJalpha, sizeof(int **) * 2);
6730 ESL_ALLOC(trsmx->iJalpha[0], sizeof(int *) * (cm->M)); /* we still allocate cm->M ptrs, if v == BEGL_S, fJalpha[0][v] will be NULL */
6731 ESL_ALLOC(trsmx->iJalpha[1], sizeof(int *) * (cm->M)); /* we still allocate cm->M ptrs, if v == BEGL_S, fJalpha[1][v] will be NULL */
6732 ESL_ALLOC(trsmx->iJalpha_mem, sizeof(int) * 2 * n_non_begl * (trsmx->W+1));
6733
6734 ESL_ALLOC(trsmx->iLalpha, sizeof(int **) * 2);
6735 ESL_ALLOC(trsmx->iLalpha[0], sizeof(int *) * (cm->M)); /* we still allocate cm->M ptrs, if v == BEGL_S, fLalpha[0][v] will be NULL */
6736 ESL_ALLOC(trsmx->iLalpha[1], sizeof(int *) * (cm->M)); /* we still allocate cm->M ptrs, if v == BEGL_S, fLalpha[1][v] will be NULL */
6737 ESL_ALLOC(trsmx->iLalpha_mem, sizeof(int) * 2 * n_non_begl * (trsmx->W+1));
6738
6739 ESL_ALLOC(trsmx->iRalpha, sizeof(int **) * 2);
6740 ESL_ALLOC(trsmx->iRalpha[0], sizeof(int *) * (cm->M)); /* we still allocate cm->M ptrs, if v == BEGL_S, fRalpha[0][v] will be NULL */
6741 ESL_ALLOC(trsmx->iRalpha[1], sizeof(int *) * (cm->M)); /* we still allocate cm->M ptrs, if v == BEGL_S, fRalpha[1][v] will be NULL */
6742 ESL_ALLOC(trsmx->iRalpha_mem, sizeof(int) * 2 * n_non_begl * (trsmx->W+1));
6743
6744 ESL_ALLOC(trsmx->iTalpha, sizeof(int **) * 2);
6745 ESL_ALLOC(trsmx->iTalpha[0], sizeof(int *) * (cm->M)); /* we still allocate cm->M ptrs, if v != BIF_B and v != ROOT_S, fTalpha[0][v] will be NULL */
6746 ESL_ALLOC(trsmx->iTalpha[1], sizeof(int *) * (cm->M)); /* we still allocate cm->M ptrs, if v != BIF_B and v != ROOT_S, fTalpha[1][v] will be NULL */
6747 ESL_ALLOC(trsmx->iTalpha_mem, sizeof(int) * 2 * n_non_begl * (trsmx->W+1));
6748
6749 /* define ncells_alpha in two statements to avoid potential overflow
6750 * (this is probably unnec, but is nec for ncells_alpha_begl so stay consistent)
6751 */
6752 trsmx->ncells_alpha = 2 * n_non_begl;
6753 trsmx->ncells_alpha *= (trsmx->W+1);
6754 cur_cell = 0;
6755 for (v = 0; v < cm->M; v++) {
6756 if (cm->stid[v] != BEGL_S) {
6757 trsmx->iJalpha[0][v] = trsmx->iJalpha_mem + cur_cell;
6758 trsmx->iLalpha[0][v] = trsmx->iLalpha_mem + cur_cell;
6759 trsmx->iRalpha[0][v] = trsmx->iRalpha_mem + cur_cell;
6760 cur_cell += trsmx->W+1;
6761 trsmx->iJalpha[1][v] = trsmx->iJalpha_mem + cur_cell;
6762 trsmx->iLalpha[1][v] = trsmx->iLalpha_mem + cur_cell;
6763 trsmx->iRalpha[1][v] = trsmx->iRalpha_mem + cur_cell;
6764 cur_cell += trsmx->W+1;
6765 }
6766 else {
6767 trsmx->iJalpha[0][v] = NULL;
6768 trsmx->iJalpha[1][v] = NULL;
6769 trsmx->iLalpha[0][v] = NULL;
6770 trsmx->iLalpha[1][v] = NULL;
6771 trsmx->iRalpha[0][v] = NULL;
6772 trsmx->iRalpha[1][v] = NULL;
6773 }
6774 }
6775 if(cur_cell != trsmx->ncells_alpha) ESL_FAIL(eslEINVAL, errbuf, "problem laying out truncated int scan matrix");
6776
6777 trsmx->ncells_Talpha = 2 * (n_bif+1) * (trsmx->W+1);
6778 cur_cell = 0;
6779 for (v = 0; v < cm->M; v++) {
6780 if (cm->stid[v] == BIF_B || cm->stid[v] == ROOT_S) {
6781 trsmx->iTalpha[0][v] = trsmx->iTalpha_mem + cur_cell;
6782 cur_cell += trsmx->W+1;
6783 trsmx->iTalpha[1][v] = trsmx->iTalpha_mem + cur_cell;
6784 cur_cell += trsmx->W+1;
6785 }
6786 else {
6787 trsmx->iTalpha[0][v] = NULL;
6788 trsmx->iTalpha[1][v] = NULL;
6789 }
6790 }
6791 if(cur_cell != trsmx->ncells_Talpha) ESL_FAIL(eslEINVAL, errbuf, "problem laying out int truncated scan matrix");
6792
6793 /* allocate falpha_begl */
6794 /* j == d, v == 0 cells, followed by j == d+1, v == 0, etc. */
6795 ESL_ALLOC(trsmx->iJalpha_begl, sizeof(int **) * (trsmx->W+1));
6796 ESL_ALLOC(trsmx->iLalpha_begl, sizeof(int **) * (trsmx->W+1));
6797 ESL_ALLOC(trsmx->iRalpha_begl, sizeof(int **) * (trsmx->W+1));
6798 for (j = 0; j <= trsmx->W; j++) {
6799 ESL_ALLOC(trsmx->iJalpha_begl[j], sizeof(int *) * (cm->M)); /* we still allocate cm->M ptrs, if v != BEGL_S, fJalpha_begl[0][v] will be NULL */
6800 ESL_ALLOC(trsmx->iLalpha_begl[j], sizeof(int *) * (cm->M)); /* we still allocate cm->M ptrs, if v != BEGL_S, fLalpha_begl[0][v] will be NULL */
6801 ESL_ALLOC(trsmx->iRalpha_begl[j], sizeof(int *) * (cm->M)); /* we still allocate cm->M ptrs, if v != BEGL_S, fRalpha_begl[0][v] will be NULL */
6802 }
6803 /* define ncells_alpha_begl with three separate statements (so we don't overflow, that was bug i40): */
6804 trsmx->ncells_alpha_begl = (trsmx->W+1);
6805 trsmx->ncells_alpha_begl *= n_begl;
6806 trsmx->ncells_alpha_begl *= (trsmx->W+1);
6807 ESL_ALLOC(trsmx->iJalpha_begl_mem, sizeof(int) * ESL_MAX(1, trsmx->ncells_alpha_begl));
6808 ESL_ALLOC(trsmx->iLalpha_begl_mem, sizeof(int) * ESL_MAX(1, trsmx->ncells_alpha_begl));
6809 ESL_ALLOC(trsmx->iRalpha_begl_mem, sizeof(int) * ESL_MAX(1, trsmx->ncells_alpha_begl));
6810
6811 cur_cell = 0;
6812 for (v = 0; v < cm->M; v++) {
6813 for (j = 0; j <= trsmx->W; j++) {
6814 if (cm->stid[v] == BEGL_S) {
6815 trsmx->iJalpha_begl[j][v] = trsmx->iJalpha_begl_mem + cur_cell;
6816 trsmx->iLalpha_begl[j][v] = trsmx->iLalpha_begl_mem + cur_cell;
6817 trsmx->iRalpha_begl[j][v] = trsmx->iRalpha_begl_mem + cur_cell;
6818 cur_cell += trsmx->W+1;
6819 }
6820 else {
6821 trsmx->iJalpha_begl[j][v] = NULL;
6822 trsmx->iLalpha_begl[j][v] = NULL;
6823 trsmx->iRalpha_begl[j][v] = NULL;
6824 }
6825 }
6826 }
6827 if(cur_cell != trsmx->ncells_alpha_begl) ESL_FAIL(eslEINVAL, errbuf, "problem laying out truncated int scan matrix");
6828
6829 /* set the flag that tells us we've got valid ints */
6830 trsmx->ints_valid = TRUE;
6831
6832 /* Initialize matrix */
6833 if((status = cm_tr_scan_mx_InitializeIntegers(cm, trsmx, errbuf)) != eslOK) return status;
6834
6835 return eslOK;
6836
6837 ERROR:
6838 ESL_FAIL(eslEMEM, errbuf, "out of memory (creating int truncated scan matrix)");
6839 return status; /* NOT REACHED */
6840 }
6841
6842 /* Function: cm_tr_scan_mx_InitializeFloats()
6843 * Date: EPN, Tue Dec 27 11:09:42 2011
6844 *
6845 * Purpose: Initialize float scores in a CM_TR_SCAN_MX. This
6846 * should be done before using the scan matrix for
6847 * a new target sequence.
6848 *
6849 * Returns: eslOK on success.
6850 * eslEINVAL if float matrix is not allocated.
6851 */
6852 int
6853 cm_tr_scan_mx_InitializeFloats(CM_t *cm, CM_TR_SCAN_MX *trsmx, char *errbuf)
6854 {
6855 int v, j, y, w, yoffset;
6856
6857 if(! trsmx->floats_valid) ESL_FAIL(eslEINVAL, errbuf, "cm_tr_scan_mx_InitializeFloats(), trsmx->floats_valid is FALSE");
6858
6859 /* First, init entire matrix to IMPOSSIBLE */
6860 esl_vec_FSet(trsmx->fJalpha_mem, trsmx->ncells_alpha, IMPOSSIBLE);
6861 esl_vec_FSet(trsmx->fLalpha_mem, trsmx->ncells_alpha, IMPOSSIBLE);
6862 esl_vec_FSet(trsmx->fRalpha_mem, trsmx->ncells_alpha, IMPOSSIBLE);
6863 esl_vec_FSet(trsmx->fTalpha_mem, trsmx->ncells_Talpha, IMPOSSIBLE);
6864 esl_vec_FSet(trsmx->fJalpha_begl_mem, trsmx->ncells_alpha_begl, IMPOSSIBLE);
6865 esl_vec_FSet(trsmx->fLalpha_begl_mem, trsmx->ncells_alpha_begl, IMPOSSIBLE);
6866 esl_vec_FSet(trsmx->fRalpha_begl_mem, trsmx->ncells_alpha_begl, IMPOSSIBLE);
6867 /* Now, initialize cells that should not be IMPOSSIBLE in f{J,L,RT}alpha and f{J,L,R}alpha_begl */
6868 for(v = cm->M-1; v >= 0; v--) {
6869 if(cm->stid[v] != BEGL_S) {
6870 if (cm->sttype[v] == E_st) {
6871 trsmx->fJalpha[0][v][0] = trsmx->fJalpha[1][v][0] = 0.;
6872 trsmx->fLalpha[0][v][0] = trsmx->fLalpha[1][v][0] = 0.;
6873 trsmx->fRalpha[0][v][0] = trsmx->fRalpha[1][v][0] = 0.;
6874 /* rest of E deck is IMPOSSIBLE, it's already set */
6875 }
6876 else if (cm->sttype[v] == S_st || cm->sttype[v] == D_st) {
6877 y = cm->cfirst[v];
6878 trsmx->fJalpha[0][v][0] = cm->endsc[v];
6879 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++)
6880 trsmx->fJalpha[0][v][0] = ESL_MAX(trsmx->fJalpha[0][v][0], (trsmx->fJalpha[0][y+yoffset][0] + cm->tsc[v][yoffset]));
6881 trsmx->fJalpha[0][v][0] = ESL_MAX(trsmx->fJalpha[0][v][0], IMPOSSIBLE);
6882 /* {L,R}alpha[0][v][0] remain IMPOSSIBLE */
6883 }
6884 else if (cm->sttype[v] == B_st) {
6885 w = cm->cfirst[v]; /* BEGL_S, left child state */
6886 y = cm->cnum[v];
6887 trsmx->fJalpha[0][v][0] = trsmx->fJalpha_begl[0][w][0] + trsmx->fJalpha[0][y][0];
6888 }
6889 trsmx->fJalpha[1][v][0] = trsmx->fJalpha[0][v][0];
6890 /* {L,R,T}alpha[{0,1}][v][0] remain IMPOSSIBLE */
6891 }
6892 else { /* v == BEGL_S */
6893 y = cm->cfirst[v];
6894 trsmx->fJalpha_begl[0][v][0] = cm->endsc[v];
6895 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++)
6896 trsmx->fJalpha_begl[0][v][0] = ESL_MAX(trsmx->fJalpha_begl[0][v][0], (trsmx->fJalpha[0][y+yoffset][0] + cm->tsc[v][yoffset])); /* careful: y is in trsmx->fJalpha */
6897 trsmx->fJalpha_begl[0][v][0] = ESL_MAX(trsmx->fJalpha_begl[0][v][0], IMPOSSIBLE);
6898 for (j = 1; j <= trsmx->W; j++)
6899 trsmx->fJalpha_begl[j][v][0] = trsmx->fJalpha_begl[0][v][0];
6900 /* {L,R}alpha_begl[j][v][0] remain IMPOSSIBLE for all j */
6901 }
6902 }
6903 return eslOK;
6904 }
6905
6906 /* Function: cm_tr_scan_mx_InitializeIntegers()
6907 * Date: EPN, Tue Dec 27 11:10:35 2011
6908 *
6909 * Purpose: Initialize integer scores in a CM_TR_SCAN_MX. This
6910 * should be done before using the scan matrix for
6911 * a new target sequence.
6912 *
6913 * Returns: eslOK on success.
6914 * eslEINVAL if float matrix is not allocated.
6915 */
6916 int
6917 cm_tr_scan_mx_InitializeIntegers(CM_t *cm, CM_TR_SCAN_MX *trsmx, char *errbuf)
6918 {
6919 int v, j, y, w, yoffset;
6920
6921 if(! trsmx->ints_valid) ESL_FAIL(eslEINVAL, errbuf, "cm_tr_scan_mx_InitializeIntegers(), trsmx->ints_valid is FALSE");
6922
6923 /* First, init entire matrix to -INFTY */
6924 esl_vec_ISet(trsmx->iJalpha_mem, trsmx->ncells_alpha, -INFTY);
6925 esl_vec_ISet(trsmx->iLalpha_mem, trsmx->ncells_alpha, -INFTY);
6926 esl_vec_ISet(trsmx->iRalpha_mem, trsmx->ncells_alpha, -INFTY);
6927 esl_vec_ISet(trsmx->iTalpha_mem, trsmx->ncells_Talpha, -INFTY);
6928 esl_vec_ISet(trsmx->iJalpha_begl_mem, trsmx->ncells_alpha_begl, -INFTY);
6929 esl_vec_ISet(trsmx->iLalpha_begl_mem, trsmx->ncells_alpha_begl, -INFTY);
6930 esl_vec_ISet(trsmx->iRalpha_begl_mem, trsmx->ncells_alpha_begl, -INFTY);
6931 /* Now, initialize cells that should not be -INFTY in f{J,L,RT}alpha and f{J,L,R}alpha_begl */
6932 for(v = cm->M-1; v >= 0; v--) {
6933 if(cm->stid[v] != BEGL_S) {
6934 if (cm->sttype[v] == E_st) {
6935 trsmx->iJalpha[0][v][0] = trsmx->iJalpha[1][v][0] = 0.;
6936 trsmx->iLalpha[0][v][0] = trsmx->iLalpha[1][v][0] = 0.;
6937 trsmx->iRalpha[0][v][0] = trsmx->iRalpha[1][v][0] = 0.;
6938 /* rest of E deck is -INFTY, it's already set */
6939 }
6940 else if (cm->sttype[v] == S_st || cm->sttype[v] == D_st) {
6941 y = cm->cfirst[v];
6942 trsmx->iJalpha[0][v][0] = cm->endsc[v];
6943 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++)
6944 trsmx->iJalpha[0][v][0] = ESL_MAX(trsmx->iJalpha[0][v][0], (trsmx->iJalpha[0][y+yoffset][0] + cm->itsc[v][yoffset]));
6945 trsmx->iJalpha[0][v][0] = ESL_MAX(trsmx->iJalpha[0][v][0], -INFTY);
6946 /* {L,R}alpha[0][v][0] remain -INFTY */
6947 }
6948 else if (cm->sttype[v] == B_st) {
6949 w = cm->cfirst[v]; /* BEGL_S, left child state */
6950 y = cm->cnum[v];
6951 trsmx->iJalpha[0][v][0] = trsmx->iJalpha_begl[0][w][0] + trsmx->iJalpha[0][y][0];
6952 }
6953 trsmx->iJalpha[1][v][0] = trsmx->iJalpha[0][v][0];
6954 /* {L,R,T}alpha[{0,1}][v][0] remain -INFTY */
6955 }
6956 else { /* v == BEGL_S */
6957 y = cm->cfirst[v];
6958 trsmx->iJalpha_begl[0][v][0] = cm->endsc[v];
6959 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++)
6960 trsmx->iJalpha_begl[0][v][0] = ESL_MAX(trsmx->iJalpha_begl[0][v][0], (trsmx->iJalpha[0][y+yoffset][0] + cm->itsc[v][yoffset])); /* careful: y is in trsmx->iJalpha */
6961 trsmx->iJalpha_begl[0][v][0] = ESL_MAX(trsmx->iJalpha_begl[0][v][0], -INFTY);
6962 for (j = 1; j <= trsmx->W; j++)
6963 trsmx->iJalpha_begl[j][v][0] = trsmx->iJalpha_begl[0][v][0];
6964 /* {L,R}alpha_begl[j][v][0] remain -INFTY for all j */
6965 }
6966 }
6967
6968 return eslOK;
6969 }
6970
6971 /* Function: cm_tr_scan_mx_SizeNeeded()
6972 * Date: EPN, Tue Dec 13 05:01:54 2011
6973 *
6974 * Purpose: Predict the size needed in Mb to create a
6975 * CM_TR_SCAN_MX for <cm>.
6976 *
6977 * Returns: Size needed in Mb.
6978 */
6979 float
6980 cm_tr_scan_mx_SizeNeeded(CM_t *cm, int do_float, int do_int)
6981 {
6982 int n_begl = 0;
6983 int n_non_begl = 0;
6984 int64_t ncells_alpha = 0;
6985 int64_t ncells_alpha_begl = 0;
6986 int64_t ncells_Talpha = 0;
6987 int v;
6988 float Mb_needed = 0.;
6989
6990 /* calculate number of cells in alpha and alpha_begl */
6991 n_begl = 0;
6992 for (v = 0; v < cm->M; v++) if (cm->stid[v] == BEGL_S) n_begl++;
6993 n_non_begl = cm->M - n_begl;
6994 /* use several statements to calculate ncells_alpha_*, otherwise we could overflow,
6995 * (the overflow was part of bug i40).
6996 */
6997 ncells_alpha = 2;
6998 ncells_alpha *= n_non_begl;
6999 ncells_alpha *= (cm->W+1);
7000 ncells_alpha_begl = (cm->W+1);
7001 ncells_alpha_begl *= n_begl;
7002 ncells_alpha_begl *= (cm->W+1);
7003
7004 /* tally up size */
7005 Mb_needed = (float) sizeof(CM_SCAN_MX);
7006 Mb_needed += (float) sizeof(int **) * NSMX_QDB_IDX; /* dnAAA (1st dim) */
7007 Mb_needed += (float) sizeof(int **) * NSMX_QDB_IDX; /* dxAAA (1st dim) */
7008 Mb_needed += (float) sizeof(int *) * NSMX_QDB_IDX * (cm->W+1); /* dnAAA (2nd dim) */
7009 Mb_needed += (float) sizeof(int *) * NSMX_QDB_IDX * (cm->W+1); /* dxAAA (2nd dim) */
7010 Mb_needed += (float) sizeof(int) * NSMX_QDB_IDX * (cm->W+1) * cm->M; /* dnAAA (3rd dim) */
7011 Mb_needed += (float) sizeof(int) * NSMX_QDB_IDX * (cm->W+1) * cm->M; /* dnAAA (3rd dim) */
7012 Mb_needed += (float) sizeof(int) * (cm->W+1); /* bestr */
7013 Mb_needed += (float) sizeof(float) * (cm->W+1); /* bestsc */
7014 if(do_float) {
7015 Mb_needed += (float) sizeof(float) * ncells_alpha; /* fJalpha */
7016 Mb_needed += (float) sizeof(float) * ncells_alpha; /* fLalpha */
7017 Mb_needed += (float) sizeof(float) * ncells_alpha; /* fRalpha */
7018 Mb_needed += (float) sizeof(float) * ncells_Talpha; /* fTalpha */
7019 Mb_needed += (float) sizeof(float) * ncells_alpha_begl; /* fJalpha_begl */
7020 Mb_needed += (float) sizeof(float) * ncells_alpha_begl; /* fLalpha_begl */
7021 Mb_needed += (float) sizeof(float) * ncells_alpha_begl; /* fRalpha_begl */
7022 }
7023 if(do_int) {
7024 Mb_needed += (float) sizeof(int) * ncells_alpha; /* iJalpha */
7025 Mb_needed += (float) sizeof(int) * ncells_alpha; /* iLalpha */
7026 Mb_needed += (float) sizeof(int) * ncells_alpha; /* iRalpha */
7027 Mb_needed += (float) sizeof(int) * ncells_Talpha; /* iTalpha */
7028 Mb_needed += (float) sizeof(int) * ncells_alpha_begl; /* iJalpha_begl */
7029 Mb_needed += (float) sizeof(int) * ncells_alpha_begl; /* iLalpha_begl */
7030 Mb_needed += (float) sizeof(int) * ncells_alpha_begl; /* iRalpha_begl */
7031 }
7032 Mb_needed *= 0.000001; /* convert to Mb */
7033
7034 return Mb_needed;
7035 }
7036
7037
7038 /* Function: cm_tr_scan_mx_Destroy()
7039 * Date: EPN, Wed Aug 17 14:22:45 2011
7040 *
7041 * Purpose: Free a CM_TR_SCAN_MX object corresponding
7042 * to CM <cm>.
7043 *
7044 * Returns: void
7045 */
7046 void
7047 cm_tr_scan_mx_Destroy(CM_t *cm, CM_TR_SCAN_MX *trsmx)
7048 {
7049 int n, j;
7050
7051 for(n = 0; n < NSMX_QDB_IDX; n++) {
7052 for(j = 1; j <= trsmx->W; j++) {
7053 free(trsmx->dnAAA[n][j]);
7054 free(trsmx->dxAAA[n][j]);
7055 }
7056 free(trsmx->dnAAA[n]);
7057 free(trsmx->dxAAA[n]);
7058 }
7059 free(trsmx->dnAAA);
7060 free(trsmx->dxAAA);
7061 free(trsmx->bestr);
7062 free(trsmx->bestsc);
7063 free(trsmx->bestmode);
7064
7065 if(trsmx->floats_valid) cm_tr_scan_mx_freefloats (cm, trsmx);
7066 if(trsmx->ints_valid) cm_tr_scan_mx_freeintegers(cm, trsmx);
7067
7068 free(trsmx);
7069 return;
7070 }
7071
7072 /* Function: cm_tr_scan_mx_freefloats()
7073 * Date: EPN, Wed Aug 17 14:19:21 2011
7074 *
7075 * Purpose: Free float data structures in a CM_TR_SCAN_MX object
7076 * corresponding to <cm>.
7077 *
7078 * Returns: eslOK on success
7079 */
7080 int
7081 cm_tr_scan_mx_freefloats(CM_t *cm, CM_TR_SCAN_MX *trsmx)
7082 {
7083 int j;
7084
7085 if(trsmx->fJalpha_mem != NULL) free(trsmx->fJalpha_mem);
7086 if(trsmx->fJalpha != NULL) {
7087 if(trsmx->fJalpha[1] != NULL) free(trsmx->fJalpha[1]);
7088 if(trsmx->fJalpha[0] != NULL) free(trsmx->fJalpha[0]);
7089 free(trsmx->fJalpha);
7090 trsmx->fJalpha = NULL;
7091 }
7092 if(trsmx->fLalpha_mem != NULL) free(trsmx->fLalpha_mem);
7093 if(trsmx->fLalpha != NULL) {
7094 if(trsmx->fLalpha[1] != NULL) free(trsmx->fLalpha[1]);
7095 if(trsmx->fLalpha[0] != NULL) free(trsmx->fLalpha[0]);
7096 free(trsmx->fLalpha);
7097 trsmx->fLalpha = NULL;
7098 }
7099 if(trsmx->fRalpha_mem != NULL) free(trsmx->fRalpha_mem);
7100 if(trsmx->fRalpha != NULL) {
7101 if(trsmx->fRalpha[1] != NULL) free(trsmx->fRalpha[1]);
7102 if(trsmx->fRalpha[0] != NULL) free(trsmx->fRalpha[0]);
7103 free(trsmx->fRalpha);
7104 trsmx->fRalpha = NULL;
7105 }
7106 if(trsmx->fTalpha_mem != NULL) free(trsmx->fTalpha_mem);
7107 if(trsmx->fTalpha != NULL) {
7108 if(trsmx->fTalpha[1] != NULL) free(trsmx->fTalpha[1]);
7109 if(trsmx->fTalpha[0] != NULL) free(trsmx->fTalpha[0]);
7110 free(trsmx->fTalpha);
7111 trsmx->fTalpha = NULL;
7112 }
7113
7114 if(trsmx->fJalpha_begl_mem != NULL) {
7115 free(trsmx->fJalpha_begl_mem);
7116 }
7117 if(trsmx->fJalpha_begl != NULL) {
7118 for (j = 0; j <= trsmx->W; j++) {
7119 if(trsmx->fJalpha_begl[j] != NULL) free(trsmx->fJalpha_begl[j]);
7120 }
7121 free(trsmx->fJalpha_begl);
7122 trsmx->fJalpha_begl = NULL;
7123 }
7124 if(trsmx->fLalpha_begl_mem != NULL) {
7125 free(trsmx->fLalpha_begl_mem);
7126 }
7127 if(trsmx->fLalpha_begl != NULL) {
7128 for (j = 0; j <= trsmx->W; j++) {
7129 if(trsmx->fLalpha_begl[j] != NULL) free(trsmx->fLalpha_begl[j]);
7130 }
7131 free(trsmx->fLalpha_begl);
7132 trsmx->fLalpha_begl = NULL;
7133 }
7134 if(trsmx->fRalpha_begl_mem != NULL) {
7135 free(trsmx->fRalpha_begl_mem);
7136 }
7137 if(trsmx->fRalpha_begl != NULL) {
7138 for (j = 0; j <= trsmx->W; j++) {
7139 if(trsmx->fRalpha_begl[j] != NULL) free(trsmx->fRalpha_begl[j]);
7140 }
7141 free(trsmx->fRalpha_begl);
7142 trsmx->fRalpha_begl = NULL;
7143 }
7144
7145 trsmx->floats_valid = FALSE;
7146 return eslOK;
7147 }
7148
7149
7150 /* Function: cm_tr_scan_mx_freeintegers()
7151 * Date: EPN, Wed Aug 24 14:56:18 2011
7152 *
7153 * Purpose: Free int data structures in a CM_TR_SCAN_MX object
7154 * corresponding to <cm>.
7155 *
7156 * Returns: eslOK on success
7157 */
7158 int
7159 cm_tr_scan_mx_freeintegers(CM_t *cm, CM_TR_SCAN_MX *trsmx)
7160 {
7161 int j;
7162
7163 if(trsmx->iJalpha_mem != NULL) free(trsmx->iJalpha_mem);
7164 if(trsmx->iJalpha != NULL) {
7165 if(trsmx->iJalpha[1] != NULL) free(trsmx->iJalpha[1]);
7166 if(trsmx->iJalpha[0] != NULL) free(trsmx->iJalpha[0]);
7167 free(trsmx->iJalpha);
7168 trsmx->iJalpha = NULL;
7169 }
7170 if(trsmx->iLalpha_mem != NULL) free(trsmx->iLalpha_mem);
7171 if(trsmx->iLalpha != NULL) {
7172 if(trsmx->iLalpha[1] != NULL) free(trsmx->iLalpha[1]);
7173 if(trsmx->iLalpha[0] != NULL) free(trsmx->iLalpha[0]);
7174 free(trsmx->iLalpha);
7175 trsmx->iLalpha = NULL;
7176 }
7177 if(trsmx->iRalpha_mem != NULL) free(trsmx->iRalpha_mem);
7178 if(trsmx->iRalpha != NULL) {
7179 if(trsmx->iRalpha[1] != NULL) free(trsmx->iRalpha[1]);
7180 if(trsmx->iRalpha[0] != NULL) free(trsmx->iRalpha[0]);
7181 free(trsmx->iRalpha);
7182 trsmx->iRalpha = NULL;
7183 }
7184 if(trsmx->iTalpha_mem != NULL) free(trsmx->iTalpha_mem);
7185 if(trsmx->iTalpha != NULL) {
7186 if(trsmx->iTalpha[1] != NULL) free(trsmx->iTalpha[1]);
7187 if(trsmx->iTalpha[0] != NULL) free(trsmx->iTalpha[0]);
7188 free(trsmx->iTalpha);
7189 trsmx->iTalpha = NULL;
7190 }
7191
7192 if(trsmx->iJalpha_begl_mem != NULL) {
7193 free(trsmx->iJalpha_begl_mem);
7194 }
7195 if(trsmx->iJalpha_begl != NULL) {
7196 for (j = 0; j <= trsmx->W; j++) {
7197 if(trsmx->iJalpha_begl[j] != NULL) free(trsmx->iJalpha_begl[j]);
7198 }
7199 free(trsmx->iJalpha_begl);
7200 trsmx->iJalpha_begl = NULL;
7201 }
7202 if(trsmx->iLalpha_begl_mem != NULL) {
7203 free(trsmx->iLalpha_begl_mem);
7204 }
7205 if(trsmx->iLalpha_begl != NULL) {
7206 for (j = 0; j <= trsmx->W; j++) {
7207 if(trsmx->iLalpha_begl[j] != NULL) free(trsmx->iLalpha_begl[j]);
7208 }
7209 free(trsmx->iLalpha_begl);
7210 trsmx->iLalpha_begl = NULL;
7211 }
7212 if(trsmx->iRalpha_begl_mem != NULL) {
7213 free(trsmx->iRalpha_begl_mem);
7214 }
7215 if(trsmx->iRalpha_begl != NULL) {
7216 for (j = 0; j <= trsmx->W; j++) {
7217 if(trsmx->iRalpha_begl[j] != NULL) free(trsmx->iRalpha_begl[j]);
7218 }
7219 free(trsmx->iRalpha_begl);
7220 trsmx->iRalpha_begl = NULL;
7221 }
7222
7223 trsmx->ints_valid = FALSE;
7224 return eslOK;
7225 }
7226
7227 /* Function: cm_tr_scan_mx_Dump()
7228 * Date: EPN, Thu Aug 18 07:35:20 2011
7229 *
7230 * Purpose: Dump current {J,L,R,T}alpha matrices from a CM_TR_SCAN_MX (either float or int).
7231 *
7232 * Returns: void.
7233 */
7234 void
7235 cm_tr_scan_mx_Dump(FILE *ofp, CM_t *cm, int j, int i0, int qdbidx, int doing_float)
7236 {
7237 int d, v;
7238 int jp_g = j-i0+1; /* j is actual index in j, jp_g is offset j relative to start i0 (index in gamma* data structures) */
7239 int cur = j%2;
7240 int prv = (j-1)%2;
7241 int *dnA, *dxA;
7242
7243 CM_TR_SCAN_MX *trsmx = cm->trsmx;
7244 if( doing_float && (! trsmx->floats_valid)) cm_Fail("cm_tr_scan_mx_Dump(), trying to print float alpha, but floats are not valid");
7245 if((! doing_float) && (! trsmx->ints_valid)) cm_Fail("cm_tr_scan_mx_Dump(), trying to print int alpha, but ints are not valid");
7246
7247 int begl_prv = j-1 % (trsmx->W+1);
7248 int begl_cur = j % (trsmx->W+1);
7249
7250 fprintf(ofp, "Dumping {J,L,R,T}Alpha: j: %d\n", j);
7251 if(jp_g >= trsmx->W) { dnA = trsmx->dnAAA[qdbidx][trsmx->W]; dxA = trsmx->dxAAA[qdbidx][trsmx->W]; }
7252 else { dnA = trsmx->dnAAA[qdbidx][jp_g]; dxA = trsmx->dxAAA[qdbidx][jp_g]; }
7253 if(doing_float) {
7254 for (v = trsmx->M-1; v >= 0; v--) {
7255 if (cm->stid[v] == BEGL_S) {
7256 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "JA[j-1:%4d][%4d][%4d]: %10.4f\n", (j-1), v, d, trsmx->fJalpha_begl[begl_prv][v][d]);
7257 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "LA[j-1:%4d][%4d][%4d]: %10.4f\n", (j-1), v, d, trsmx->fLalpha_begl[begl_prv][v][d]);
7258 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "RA[j-1:%4d][%4d][%4d]: %10.4f\n", (j-1), v, d, trsmx->fRalpha_begl[begl_prv][v][d]);
7259 }
7260 else {
7261 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "JA[j-1:%4d][%4d][%4d]: %10.4f\n", (j-1), v, d, trsmx->fJalpha[prv][v][d]);
7262 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "LA[j-1:%4d][%4d][%4d]: %10.4f\n", (j-1), v, d, trsmx->fLalpha[prv][v][d]);
7263 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "RA[j-1:%4d][%4d][%4d]: %10.4f\n", (j-1), v, d, trsmx->fRalpha[prv][v][d]);
7264 }
7265 if(cm->stid[v] == BIF_B) {
7266 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "TA[j-1:%4d][%4d][%4d]: %10.4f\n", (j-1), v, d, trsmx->fTalpha[prv][v][d]);
7267 }
7268 fprintf(ofp, "\n");
7269 }
7270 for (v = trsmx->M-1; v >= 0; v--) {
7271 if (cm->stid[v] == BEGL_S) {
7272 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "JA[j :%4d][%4d][%4d]: %10.4f\n", j, v, d, trsmx->fJalpha_begl[begl_cur][v][d]);
7273 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "LA[j :%4d][%4d][%4d]: %10.4f\n", j, v, d, trsmx->fLalpha_begl[begl_cur][v][d]);
7274 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "RA[j :%4d][%4d][%4d]: %10.4f\n", j, v, d, trsmx->fRalpha_begl[begl_cur][v][d]);
7275 }
7276 else {
7277 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "JA[j :%4d][%4d][%4d]: %10.4f\n", j, v, d, trsmx->fJalpha[cur][v][d]);
7278 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "LA[j :%4d][%4d][%4d]: %10.4f\n", j, v, d, trsmx->fLalpha[cur][v][d]);
7279 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "RA[j :%4d][%4d][%4d]: %10.4f\n", j, v, d, trsmx->fRalpha[cur][v][d]);
7280 }
7281 if(cm->stid[v] == BIF_B) {
7282 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "TA[j :%4d][%4d][%4d]: %10.4f\n", j, v, d, trsmx->fTalpha[cur][v][d]);
7283 }
7284 fprintf(ofp, "\n");
7285 }
7286 }
7287 else { /* doing int */
7288 for (v = trsmx->M-1; v >= 0; v--) {
7289 if (cm->stid[v] == BEGL_S) {
7290 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "JA[j-1:%4d][%4d][%4d]: %10d\n", (j-1), v, d, trsmx->iJalpha_begl[begl_prv][v][d]);
7291 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "LA[j-1:%4d][%4d][%4d]: %10d\n", (j-1), v, d, trsmx->iLalpha_begl[begl_prv][v][d]);
7292 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "RA[j-1:%4d][%4d][%4d]: %10d\n", (j-1), v, d, trsmx->iRalpha_begl[begl_prv][v][d]);
7293 }
7294 else {
7295 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "JA[j-1:%4d][%4d][%4d]: %10d\n", (j-1), v, d, trsmx->iJalpha[prv][v][d]);
7296 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "LA[j-1:%4d][%4d][%4d]: %10d\n", (j-1), v, d, trsmx->iLalpha[prv][v][d]);
7297 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "RA[j-1:%4d][%4d][%4d]: %10d\n", (j-1), v, d, trsmx->iRalpha[prv][v][d]);
7298 }
7299 if(cm->stid[v] == BIF_B) {
7300 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "TA[j-1:%4d][%4d][%4d]: %10d\n", (j-1), v, d, trsmx->iTalpha[prv][v][d]);
7301 }
7302 fprintf(ofp, "\n\n");
7303 }
7304 for (v = trsmx->M-1; v >= 0; v--) {
7305 if (cm->stid[v] == BEGL_S) {
7306 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "JA[j :%4d][%4d][%4d]: %10d\n", j, v, d, trsmx->iJalpha_begl[begl_cur][v][d]);
7307 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "LA[j :%4d][%4d][%4d]: %10d\n", j, v, d, trsmx->iLalpha_begl[begl_cur][v][d]);
7308 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "RA[j :%4d][%4d][%4d]: %10d\n", j, v, d, trsmx->iRalpha_begl[begl_cur][v][d]);
7309 }
7310 else {
7311 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "JA[j :%4d][%4d][%4d]: %10d\n", j, v, d, trsmx->iJalpha[cur][v][d]);
7312 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "LA[j :%4d][%4d][%4d]: %10d\n", j, v, d, trsmx->iLalpha[cur][v][d]);
7313 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "RA[j :%4d][%4d][%4d]: %10d\n", j, v, d, trsmx->iRalpha[cur][v][d]);
7314 }
7315 if(cm->stid[v] == BIF_B) {
7316 for(d = dnA[v]; d <= dxA[v]; d++) fprintf(ofp, "TA[j :%4d][%4d][%4d]: %10d\n", j, v, d, trsmx->iTalpha[cur][v][d]);
7317 }
7318 fprintf(ofp, "\n\n");
7319 }
7320 }
7321 return;
7322 }
7323
7324 /*****************************************************************
7325 * 15. GammaHitMx_t data structure functions,
7326 * Semi HMM data structure for optimal resolution of overlapping
7327 * hits for CM DP search functions.
7328 *****************************************************************/
7329
7330 /* Function: CreateGammaHitMx()
7331 * Date: EPN, Mon Nov 5 05:22:56 2007
7332 *
7333 * Purpose: Allocate and initialize a gamma semi-HMM for
7334 * optimal hit resolution of a CM based scan.
7335 *
7336 * Returns: Newly allocated GammaHitMx_t object:
7337 */
7338 GammaHitMx_t *
7339 CreateGammaHitMx(int L, int64_t i0, float cutoff)
7340 {
7341 int status;
7342 GammaHitMx_t *gamma;
7343 ESL_ALLOC(gamma, sizeof(GammaHitMx_t));
7344
7345 gamma->L = L;
7346 gamma->i0 = i0;
7347 gamma->cutoff = cutoff;
7348 /* allocate/initialize for CYK/Inside */
7349 ESL_ALLOC(gamma->mx, sizeof(float) * (L+1));
7350 ESL_ALLOC(gamma->gback, sizeof(int) * (L+1));
7351 ESL_ALLOC(gamma->savesc, sizeof(float) * (L+1));
7352 ESL_ALLOC(gamma->saver, sizeof(int) * (L+1));
7353 ESL_ALLOC(gamma->savemode, sizeof(int) * (L+1));
7354
7355 gamma->mx[0] = 0;
7356 gamma->gback[0] = -1;
7357
7358 return gamma;
7359
7360 ERROR:
7361 return NULL;
7362 }
7363
7364 /* Function: FreeGammaHitMx()
7365 * Date: EPN, Mon Nov 5 05:32:00 2007
7366 *
7367 * Purpose: Free a gamma semi-HMM.
7368 *
7369 * Returns: void;
7370 */
7371 void
7372 FreeGammaHitMx(GammaHitMx_t *gamma)
7373 {
7374 free(gamma->mx);
7375 free(gamma->gback);
7376 free(gamma->savesc);
7377 free(gamma->saver);
7378 free(gamma->savemode);
7379 free(gamma);
7380
7381 return;
7382 }
7383
7384 /* Function: UpdateGammaHitMx()
7385 * Date: EPN, Mon Nov 5 05:41:14 2007
7386 * EPN, Mon Aug 22 08:54:56 2011 (search_results_t --> CM_TOPHITS)
7387 *
7388 * Purpose: Update a non-greedy gamma semi-HMM for CM hits that end
7389 * at gamma-relative position <j>.
7390 *
7391 * Can be called from either standard or truncated scanning
7392 * functions. <bestsc[d]>, <bestr[d]>, <bestmode[d]> report
7393 * the maximum scoring hit of length d, its root state (0
7394 * for glocal, !=0 for local or truncated), and its marginal
7395 * mode (if truncated) . They are all allocated for 0..W,
7396 * but only dmin..dmax should be considered valid.
7397 *
7398 * If caller is a truncated scanner <bestmode> will be
7399 * non-NULL, and include info on the alignment mode of each
7400 * hit (<bestmode>). Some hits will be disallowed based
7401 * on their alignment mode (e.g. 5' truncated hits must
7402 * include residue i0 in their alignment to be allowed,
7403 * see cm_tophits.c:cm_hit_AllowTruncation() for more
7404 * information).
7405
7406 * If bestmode is NULL (caller is non-truncated scanner),
7407 * all hits are implictly TRMODE_J mode.
7408 *
7409 * If <bestsc> is NULL (caller is an HMM banded scanner) then
7410 * no hit ending at j is possible given the HMM bands, so
7411 * position j of the gamma matrix is simply initialized and
7412 * then we return. Also if dmin > dmax, no valid d for this
7413 * j exists, so we also just initialize and return.
7414 *
7415 * This function should only be called if gamma->i_am_greedy
7416 * is FALSE. In this case we store information on the best
7417 * score at each position and then traceback later with a
7418 * TBackGammaHitMx() call. For greedy gamma matrices, we use
7419 * ReportHitsGreedily(), which reports hits immediately.
7420 *
7421 * Args: cm - the model, used only for its alphabet and null model
7422 * errbuf - for reporting errors
7423 * pass_idx - pipeline pass index we're currently in, dictates which
7424 * types of marginal hits to allow
7425 * gamma - the gamma data structure
7426 * j - end point of hit, in actual sequence coordinate space
7427 * dmin - minimum d to consider
7428 * dmax - maximum d to consider
7429 * bestsc - [0..W]; only [dmin..dmax] valid: best scores for current j, copied from alpha matrix(es) by caller
7430 * bestr - [0..W]; only [dmin..dmax] valid: root state (0 or local entry) corresponding to hit stored in alpha_row
7431 * bestmode - [0..W]; only [dmin..dmax] valid: marginal mode that gives bestsc[d], if NULL, all modes are implictly TRMODE_J
7432 * tro - truncated scanner info, must we enforce i0/j0 be in any valid hit?, NULL if caller is not a truncated scanner
7433 * W - window size, max size of a hit, only used if we're doing a NULL3 correction (act != NULL)
7434 * act - [0..j..W-1][0..a..abc->K-1], alphabet count, count of residue a in dsq from 1..jp where j = jp%(W+1)
7435 *
7436 * Returns: eslOK on success
7437 * Throws: eslEMEM on memory allocation error
7438 */
7439 int
7440 UpdateGammaHitMx(CM_t *cm, char *errbuf, int pass_idx, GammaHitMx_t *gamma, int j, int dmin, int dmax,
7441 float *bestsc, int *bestr, char *bestmode, int W, double **act)
7442 {
7443 int status; /* easel status */
7444 int i; /* position of first residue in hit, in actual sequence coordinates */
7445 int ip; /* position of first residue in hit, in gamma/act coordinates */
7446 int jp; /* position of final residue in hit, in gamma/act coordinates */
7447 int d; /* hit length, d=j-i+1 */
7448 char mode; /* marginal alignment mode */
7449 int do_report_hit; /* should we add info on this hit to gamma? */
7450 float hit_sc; /* score for this hit, possibly null3-corrected */
7451 float cumulative_sc; /* cumulative score of all hits in gamma, up to j */
7452 int j0 = gamma->i0+gamma->L-1;
7453
7454 /* variables related to NULL3 penalty */
7455 float *comp = NULL; /* 0..a..cm->abc-K-1, the composition of residue a within the hit being reported */
7456 int a; /* counter for alphabet */
7457 float null3_correction = 0.; /* null 3 penalty */
7458
7459 /* j is in actual sequence coordinates, jp will be in gamma coordinates (offset by gamma->i0) */
7460 jp = j - gamma->i0 + 1;
7461
7462 /* initialize */
7463 gamma->mx[jp] = gamma->mx[jp-1] + 0;
7464 gamma->gback[jp] = -1;
7465 gamma->savesc[jp] = IMPOSSIBLE;
7466 gamma->saver[jp] = -1;
7467 gamma->savemode[jp] = -1;
7468
7469 if(bestsc == NULL || dmin > dmax) return eslOK; /* don't report any hits */
7470
7471 if(act != NULL) ESL_ALLOC(comp, sizeof(float) * cm->abc->K);
7472 for (d = ESL_MAX(1, dmin); d <= dmax; d++) { /* don't allow length 0 hits, they cause all sorts of problems */
7473 i = j -d+1;
7474 ip = jp-d+1;
7475 mode = (bestmode == NULL) ? TRMODE_J : bestmode[d];
7476 hit_sc = bestsc[d];
7477 cumulative_sc = gamma->mx[ip-1] + hit_sc;
7478 /* printf("CAND hit %3d..%3d: %8.2f\n", i, j, hit_sc); */
7479 if (cumulative_sc > gamma->mx[jp] && NOT_IMPOSSIBLE(hit_sc)) {
7480 do_report_hit = TRUE;
7481
7482 if(bestmode != NULL) {
7483 /* we're in truncated mode, check if we should allow the hit.
7484 * this enforces that i0/j0 is included if necessary based on the
7485 * marginal alignment mode of the hit
7486 */
7487 do_report_hit = cm_hit_AllowTruncation(cm, pass_idx, i, j, gamma->i0, j0, mode, bestr[d]);
7488 }
7489
7490 if(do_report_hit && act != NULL) {
7491 /* do a NULL3 score correction */
7492 for(a = 0; a < cm->abc->K; a++) {
7493 comp[a] = act[jp%(W+1)][a] - act[(ip-1)%(W+1)][a];
7494 /*printf("a: %5d jp/W: %5d ip-1/W: %5d jp[a]: %.3f ip-1[a]: %.3f c[a]: %.3f\n", a, jp%(W+1), (ip-1%W), act[(jp%(W+1))][a], act[((ip-1)%(W+1))][a], comp[a]);*/
7495 }
7496 esl_vec_FNorm(comp, cm->abc->K);
7497 ScoreCorrectionNull3(cm->abc, cm->null, comp, d, cm->null3_omega, &null3_correction);
7498 hit_sc -= null3_correction;
7499 cumulative_sc -= null3_correction;
7500 do_report_hit = (cumulative_sc > gamma->mx[jp]) ? TRUE : FALSE;
7501 /* printf("GOOD hit %3d..%3d: %8.2f %10.6f %8.2f\n", i, j, hit_sc+null3_correction, null3_correction, hit_sc); */
7502 }
7503
7504 if(do_report_hit) {
7505 /* printf("\t%.3f %.3f\n", hit_sc+null3_correction, hit_sc); */
7506 gamma->mx[jp] = cumulative_sc;
7507 gamma->gback[jp] = i;
7508 gamma->savesc[jp] = hit_sc;
7509 gamma->saver[jp] = bestr[d];
7510 gamma->savemode[jp] = mode;
7511 }
7512 }
7513 }
7514 if(comp != NULL) free(comp);
7515 return eslOK;
7516
7517 ERROR:
7518 ESL_FAIL(eslEMEM, errbuf, "Memory allocation error.\n");
7519 return eslEMEM; /* NEVERREACHED */
7520 }
7521
7522
7523 /* Function: ReportHitsGreedily()
7524 * Date: EPN, Fri Nov 4 14:01:10 2011
7525 *
7526 * Purpose: Report hits when using the greedy hit resolution
7527 * strategy. For the non-greedy strategy, see
7528 * UpdateGammaHitMx().
7529 *
7530 * If caller is a truncated scanner <bestmode> will be
7531 * non-NULL, and include info on the alignment mode of each
7532 * hit (<bestmode>). Some hits will be disallowed based on
7533 * their alignment mode and the pass_idx (e.g. 5' truncated
7534 * hits in the PLI_PASS_5P_ONLY_FORCE pass must include
7535 * residue i0 in their alignment to be allowed, see
7536 * cm_tophits.c:cm_hit_AllowTruncation() for more
7537 * information).
7538 *
7539 * If caller is a standard (non-truncated) scanner
7540 * <bestmode> will be NULL. All hits will implicitly be of
7541 * the TRMODE_J mode and no hits are disallowed.
7542 *
7543 * Returns: eslOK on success
7544 * Throws: eslEMEM on memory allocation error
7545 */
7546 int
7547 ReportHitsGreedily(CM_t *cm, char *errbuf, int pass_idx, int j, int dmin, int dmax, float *bestsc, int *bestr,
7548 char *bestmode, int W, double **act, int64_t i0, int64_t j0, float cutoff, CM_TOPHITS *hitlist)
7549 {
7550 int status; /* easel status */
7551 int i; /* first residue in hit, in actual sequence coords */
7552 int ip; /* first residue in hit, in act vector coords */
7553 int jp; /* first residue in hit, in act vector coords */
7554 int d; /* hit length, d=j-i+1 */
7555 char mode; /* marginal alignment mode */
7556 int do_report_hit; /* should we add create a hit for current d? */
7557 float hit_sc; /* score for this hit, possibly null3-corrected */
7558 float max_sc_reported; /* max score of all hits thus far reported */
7559 CM_HIT *hit = NULL; /* a hit */
7560
7561 /* variables related to NULL3 penalty */
7562 float *comp = NULL; /* 0..a..cm->abc-K-1, the composition of residue a within the hit being reported */
7563 int a; /* counter for alphabet */
7564 float null3_correction = 0.; /* null 3 penalty */
7565
7566 if(bestsc == NULL || dmin > dmax) return eslOK; /* don't report any hits */
7567
7568 /* In greedy mode, we are resolving overlaps greedily (RSEARCH
7569 * style). We'll have to remove overlaps after all hits are
7570 * reported (i.e. many calls to this function with many different
7571 * j values) but we don't have to report all hits above cutoff for
7572 * this j. Specifically, at the given j, any hit with a d of d1 is
7573 * guaranteed to mask any hit of lesser score with a d > d1. So,
7574 * we step through all d starting at dmin and going up to dmax,
7575 * only reporting those that exceed our cutoffs *and* are greater
7576 * than maximum seen thus far.
7577 */
7578 max_sc_reported = IMPOSSIBLE;
7579 if(act != NULL) ESL_ALLOC(comp, sizeof(float) * cm->abc->K);
7580 for (d = ESL_MAX(1, dmin); d <= dmax; d++) { /* don't allow length 0 hits, they cause all sorts of problems */
7581 i = j-d+1;
7582 mode = (bestmode == NULL) ? TRMODE_J : bestmode[d];
7583 hit_sc = bestsc[d];
7584 if (hit_sc > max_sc_reported && /* hit of length d is best seen so far */
7585 hit_sc >= cutoff && /* hit of length d has sc >= cutoff */
7586 NOT_IMPOSSIBLE(hit_sc)) /* safety: hit does not have IMPOSSIBLE sc */
7587 {
7588 do_report_hit = TRUE;
7589
7590 if(bestmode != NULL) {
7591 /* we're in truncated mode, check if we should allow the hit.
7592 * this enforces that i0/j0 is included if necessary based on the
7593 * marginal alignment mode of the hit
7594 */
7595 do_report_hit = cm_hit_AllowTruncation(cm, pass_idx, i, j, i0, j0, mode, bestr[d]);
7596 }
7597
7598 if(do_report_hit && act != NULL) { /* do NULL3 score correction and see if we still want to allow it */
7599 ip = i - i0 + 1;
7600 jp = j - i0 + 1;
7601 for(a = 0; a < cm->abc->K; a++) comp[a] = act[jp%(W+1)][a] - act[(ip-1)%(W+1)][a];
7602 esl_vec_FNorm(comp, cm->abc->K);
7603 ScoreCorrectionNull3(cm->abc, cm->null, comp, d, cm->null3_omega, &null3_correction);
7604 hit_sc -= null3_correction;
7605 /* reevaluate do_report_hit: has null3_correction dropped us below our cutoffs? */
7606 do_report_hit = (hit_sc > max_sc_reported && hit_sc >= cutoff) ? TRUE : FALSE;
7607 }
7608
7609 if(do_report_hit) { /* this may have been set to FALSE if we did a null3 sc correction */
7610 /*printf("\t%.3f %.3f i: %d j: %d r: %d mode: %d\n", hit_sc+null3_correction, hit_sc, i, j, bestr[d], mode);*/
7611 cm_tophits_CreateNextHit(hitlist, &hit);
7612 hit->start = i;
7613 hit->stop = j;
7614 hit->root = bestr[d];
7615 hit->mode = mode;
7616 hit->score = hit_sc;
7617 hit->hmmonly = FALSE;
7618 max_sc_reported = hit_sc;
7619 }
7620 }
7621 }
7622 if(comp != NULL) free(comp);
7623 return eslOK;
7624
7625 ERROR:
7626 ESL_FAIL(eslEMEM, errbuf, "Memory allocation error.\n");
7627 return eslEMEM; /* NEVERREACHED */
7628 }
7629
7630
7631 /* Function: TBackGammaHitMx()
7632 * Date: EPN, Mon Nov 5 10:14:30 2007
7633 *
7634 * Purpose: Traceback with a gamma semi-HMM for CM hits.
7635 * gamma->iamgreedy should be FALSE.
7636 *
7637 * Returns: void; dies immediately upon an error.
7638 */
7639 void
7640 TBackGammaHitMx(GammaHitMx_t *gamma, CM_TOPHITS *hitlist, int64_t i0, int64_t j0)
7641 {
7642 int j, jp_g;
7643 CM_HIT *hit = NULL;
7644
7645 if(hitlist == NULL) cm_Fail("cm_TBackGammaHitMx(), hitlist == NULL");
7646 /* Recover all hits: an (i,j,sc) triple for each one.
7647 */
7648 j = j0;
7649 while (j >= i0) {
7650 jp_g = j-i0+1;
7651 /*printf("TBACK j: %d sc: %.2f\n", j, gamma->savesc[jp_g]);*/
7652 if (gamma->gback[jp_g] == -1) j--; /* no hit */
7653 else { /* a hit, a palpable hit */
7654 if(gamma->savesc[jp_g] >= gamma->cutoff) {
7655 /* report the hit */
7656 /*ReportHit(gamma->gback[jp_g], j, gamma->saver[jp_g], gamma->savesc[jp_g], results);*/
7657 cm_tophits_CreateNextHit(hitlist, &hit);
7658 hit->start = gamma->gback[jp_g];
7659 hit->stop = j;
7660 hit->root = gamma->saver[jp_g];
7661 hit->mode = gamma->savemode[jp_g];
7662 hit->score = gamma->savesc[jp_g];
7663 hit->hmmonly = FALSE;
7664 }
7665 j = gamma->gback[jp_g]-1;
7666 }
7667 }
7668 return;
7669 }
7670
7671