Debian Codegraph @

src/cp9_modelmaker.c

raw symbols SCIP
1 /* cp9_modelmaker.c (formerly CP9_cm2wrhmm.c)
2 * EPN 11.28.05
3 *
4 * Functions to build a Weinberg/Ruzzo maximum likelihood HMM from a CM.
5 * Uses the "CM plan 9" (cp9) HMM architecture.
6 * These functions use ideas/equations from Zasha Weinberg's thesis
7 * (notably p.122-124), but a CM plan 9 HMM is not exactly a Weinberg-Ruzzo
8 * maximum likelihood heuristic HMM (though it's close).
9 *
10 * build_cp9_hmm() is the main function, it sets the probabilities of
11 * the HMM and (optionally) checks that the expected number of times
12 * each HMM state is entered is within a given threshold of the
13 * expected number of times each corresponding CM state is entered.
14 */
15
16 #include <esl_config.h>
17 #include <p7_config.h>
18 #include "config.h"
19
20 #include <math.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "easel.h"
26 #include "esl_msa.h"
27 #include "esl_random.h"
28 #include "esl_stats.h"
29 #include "esl_vectorops.h"
30
31 #include "hmmer.h"
32
33 #include "infernal.h"
34
35 static float cm2hmm_emit_prob(CM_t *cm, CP9Map_t *cp9map, int x, int i, int k);
36 static void cm2hmm_special_trans_cp9(CM_t *cm, CP9_t *hmm, CP9Map_t *cp9map, double *psi, char ***tmap);
37 static void cm2hmm_trans_probs_cp9(CM_t *cm, CP9_t *hmm, CP9Map_t *cp9map, int k, double *psi, char ***tmap);
38 static void hmm_add_single_trans_cp9(CM_t *cm, CP9_t *hmm, CP9Map_t *cp9map, int a, int b, int k, int hmm_trans_idx, double *psi, char ***tmap);
39 static float cm_sum_subpaths_cp9(CM_t *cm, CP9Map_t *cp9map, int start, int end, char ***tmap, int k, double *psi);
40 static int check_psi_vs_phi_cp9(CM_t *cm, char *errbuf, CP9Map_t *cp9map, double *psi, double **phi, double threshold, int debug_level);
41 static int CP9_node_chi_squared(CP9_t *ahmm, CP9_t *shmm, int nd, float threshold, int print_flag);
42 static int check_cm_adj_bp(CM_t *cm, CP9Map_t *cp9map);
43 static float FChiSquareFit(float *f1, float *f2, int N);
44
45 /* EPN 10.26.06
46 * Function: AllocCP9Map()
47 *
48 * Purpose: Allocate a CP9Map_t object that stores information mapping
49 * a CP9 HMM to a CM and vice versa. See infernal.h for
50 * description.
51 *
52 * Args:
53 * int hmm_M; - number of CP9 HMM nodes, the consensus length
54 * int cm_M; - number of states in the CM
55 * int cm_nodes - number of nodes in the CM
56 * Returns: CMSubInfo_t
57 */
58 CP9Map_t *
59 AllocCP9Map(CM_t *cm)
60 {
61 int status;
62 CP9Map_t *cp9map;
63 int v,ks,i;
64
65 ESL_ALLOC(cp9map, sizeof(struct cp9map_s));
66
67 /* Determine the consensus length (to be set as hmm_M) of the CM */
68 cp9map->hmm_M = 0;
69 for(v = 0; v <= cm->M; v++)
70 {
71 if(cm->stid[v] == MATP_MP)
72 cp9map->hmm_M += 2;
73 else if(cm->stid[v] == MATL_ML || cm->stid[v] == MATR_MR)
74 cp9map->hmm_M++;
75 }
76 cp9map->cm_M = cm->M;
77 cp9map->cm_nodes = cm->nodes;
78
79 /* Allocate and initialize arrays */
80 ESL_ALLOC(cp9map->nd2lpos, sizeof(int) * cp9map->cm_nodes);
81 ESL_ALLOC(cp9map->nd2rpos, sizeof(int) * cp9map->cm_nodes);
82 for(i = 0; i < cp9map->cm_nodes; i++)
83 cp9map->nd2lpos[i] = cp9map->nd2rpos[i] = -1;
84
85 ESL_ALLOC(cp9map->pos2nd, sizeof(int) * (cp9map->hmm_M+1));
86 ESL_ALLOC(cp9map->hns2cs, sizeof(int **) * (cp9map->hmm_M+1));
87 for(i = 0; i <= cp9map->hmm_M; i++)
88 {
89 cp9map->pos2nd[i] = -1;
90 ESL_ALLOC(cp9map->hns2cs[i], sizeof(int *) * 3);
91 for(ks = 0; ks < 3; ks++)
92 {
93 ESL_ALLOC(cp9map->hns2cs[i][ks], sizeof(int) * 2);
94 cp9map->hns2cs[i][ks][0] = cp9map->hns2cs[i][ks][1] = -1;
95 }
96 }
97
98 ESL_ALLOC(cp9map->cs2hn, sizeof(int *) * (cp9map->cm_M+1));
99 ESL_ALLOC(cp9map->cs2hs, sizeof(int *) * (cp9map->cm_M+1));
100 for(v = 0; v <= cp9map->cm_M; v++)
101 {
102 ESL_ALLOC(cp9map->cs2hn[v], sizeof(int) * 2);
103 ESL_ALLOC(cp9map->cs2hs[v], sizeof(int) * 2);
104 for(i = 0; i <= 1; i++)
105 cp9map->cs2hn[v][i] = cp9map->cs2hs[v][i] = -1;
106 }
107
108 return cp9map;
109
110 ERROR:
111 cm_Fail("Memory allocation error.");
112 return NULL; /* never reached */
113 }
114
115 /* Function: SizeofCP9Map()
116 * Incept: EPN, Wed Jan 18 09:50:56 2012
117 * Purpose: Calculate and return the size of
118 * a CP9Map_t object in megabytes.
119 */
120 float
121 SizeofCP9Map(CP9Map_t *cp9map)
122 {
123 float bytes = 0.;
124
125 bytes = sizeof(CP9Map_t);
126
127 /* following from AllocCP9Map() */
128 bytes += sizeof(int) * cp9map->cm_nodes; /* nd2lpos */
129 bytes += sizeof(int) * cp9map->cm_nodes; /* nd2rpos */
130 bytes += sizeof(int) * (cp9map->hmm_M+1); /* pos2nd */
131 bytes += sizeof(int **) * (cp9map->hmm_M+1); /* hns2cs */
132 bytes += sizeof(int *) * (cp9map->hmm_M+1) * 3; /* hns2cs */
133 bytes += sizeof(int) * (cp9map->hmm_M+1) * 3 * 2; /* hns2cs */
134 bytes += sizeof(int *) * (cp9map->cm_M+1); /* cs2hn */
135 bytes += sizeof(int *) * (cp9map->cm_M+1); /* cs2hs */
136 bytes += sizeof(int *) * (cp9map->cm_M+1) * 2; /* cs2hs */
137 bytes += sizeof(int *) * (cp9map->cm_M+1) * 2; /* cs2hn */
138
139 return (bytes / 1000000.);
140 }
141
142 /* Function: FreeCP9Map()
143 * Returns: void
144 */
145
146 void
147 FreeCP9Map(CP9Map_t *cp9map)
148 {
149 int v,k,ks;
150 for(v = 0; v <= cp9map->cm_M; v++)
151 {
152 free(cp9map->cs2hn[v]);
153 free(cp9map->cs2hs[v]);
154 }
155 free(cp9map->cs2hn);
156 free(cp9map->cs2hs);
157
158 for(k = 0; k <= cp9map->hmm_M; k++)
159 {
160 for(ks = 0; ks < 3; ks++)
161 free(cp9map->hns2cs[k][ks]);
162 free(cp9map->hns2cs[k]);
163 }
164 free(cp9map->hns2cs);
165
166 free(cp9map->nd2lpos);
167 free(cp9map->nd2rpos);
168 free(cp9map->pos2nd);
169 free(cp9map);
170 }
171
172 /**************************************************************************
173 * EPN 03.12.06
174 * Function: build_cp9_hmm()
175 *
176 * Purpose: Given a CM, build a CM Plan 9 HMM that mirrors the CM as closely
177 * as possible. This HMM is a Weinberg/Ruzzo style ML HMM; i.e. if
178 * we sampled an 'infinite MSA' from the CM and built a ML HMM from it
179 * (using no pseudo-counts), it would be the same as the HMM we construct
180 * here.
181 *
182 * Args:
183 * cm - the CM
184 * do_psi_test - TRUE to do a psi vs phi test, FALSE not to
185 * thresh - allowable difference in expected number of times mapping
186 * cm and hmm states are entered
187 * debug_level - level for debugging print statements
188 * ret_hmm - RETURN: CM Plan 9 HMM
189 * ret_cp9map - RETURN: map from the CP9 HMM to the CM and vice versa
190 *
191 * Returns: eslOK if CP9 is constructed (and passes the psi vs phi test if <do_psi_test>)
192 * eslEINCOMPAT on contract violation, errbuf is filled
193 * eslFAIL if we get some error, errbuf is filled.
194 */
195 int
196 build_cp9_hmm(CM_t *cm, char *errbuf, int do_psi_test, float thresh, int debug_level,
197 CP9_t **ret_hmm, CP9Map_t **ret_cp9map)
198 {
199 int status;
200 int k; /* counter of consensus columns (HMM nodes)*/
201 int i;
202 double *psi; /* expected num times each state visited in CM */
203 double **phi; /* expected num times each state visited in HMM*/
204 char ***tmap;
205 int *ap; /* CM state(s) (1 or 2) that maps to HMM state in node k*/
206 int k_state; /* 0, 1 or 2, state in hmm node k*/
207 CP9Map_t *cp9map;
208 CP9_t *hmm; /* CM plan 9 HMM we're going to construct from the sub_cm */
209
210 /* Contract check, we can't be in local mode in the CM */
211 if(cm->flags & CMH_LOCAL_BEGIN) ESL_FAIL(eslEINCOMPAT, errbuf, "build_cp9_hmm(): CMH_LOCAL_BEGIN flag is up.\n");
212 if(cm->flags & CMH_LOCAL_END) ESL_FAIL(eslEINCOMPAT, errbuf, "build_cp9_hmm(): CMH_LOCAL_END flag is up.\n");
213
214 /* Allocate and initialize the cp9map */
215 cp9map = AllocCP9Map(cm);
216 /* Map the CM states to CP9 states and nodes and vice versa */
217 CP9_map_cm2hmm(cm, cp9map, debug_level);
218
219 hmm = AllocCPlan9(cp9map->hmm_M, cm->abc);
220 ZeroCPlan9(hmm);
221 CPlan9SetNullModel(hmm, cm->null, 1.0); /* set p1 = 1.0 which corresponds to the CM */
222 CPlan9InitEL(hmm, cm); /* set up hmm->el_from_ct and hmm->el_from_idx data, which
223 * explains how the EL states are connected in the HMM. */
224 hmm->null2_omega = cm->null2_omega;
225 hmm->null3_omega = cm->null3_omega;
226
227 ESL_ALLOC(ap, sizeof(int) * 2);
228 if(debug_level > 1)
229 {
230 printf("-------------------------------------------------\n");
231 printf("In build_CP9_hmm()\n");
232 }
233
234 tmap = cm_CreateTransitionMap();
235 psi = cm_ExpectedStateOccupancy(cm);
236
237 /* Special case 1st insert state maps to state 1 in the CM */
238 for(i = 0; i < cm->abc->K; i++)
239 {
240 hmm->ins[0][i] = cm->e[1][i];
241 }
242 for(k = 1; k <= hmm->M; k++)
243 {
244 for(i = 0; i < cm->abc->K; i++)
245 {
246 hmm->mat[k][i] = 0.0;
247 hmm->ins[k][i] = 0.0;
248 }
249 /* First, take care of the match state. */
250 k_state = HMMMATCH;
251 ap[0] = cp9map->hns2cs[k][k_state][0];
252 ap[1] = cp9map->hns2cs[k][k_state][1];
253 /* ap[0] is a CM state that maps to HMM node k's match state */
254 /* ap[1] is potentially another CM state that maps to HMM node k's match state
255 (ex. if node k maps to the left half of a MATP node), and potentially = -1
256 if no other state maps to hmm node k's match state.*/
257 /* psi[ap[0]] is the expected number of times cm state ap[0] is entered. */
258 for(i = 0; i < cm->abc->K; i++)
259 {
260 hmm->mat[k][i] += psi[ap[0]] *
261 cm2hmm_emit_prob(cm, cp9map, ap[0], i, k);
262 if(ap[1] != -1)
263 hmm->mat[k][i] += psi[ap[1]] *
264 cm2hmm_emit_prob(cm, cp9map, ap[1], i, k);
265 }
266
267 /* Now, do the insert state. */
268 k_state = HMMINSERT;
269 ap[0] = cp9map->hns2cs[k][k_state][0];
270 ap[1] = cp9map->hns2cs[k][k_state][1];
271 /* ap[0] is the only CM state that maps to HMM node k's insert state */
272 /* ap[1] should be -1 unless k = hmm->M. */
273 /* psi[ap[0]] is the expected number of times cm state ap[0] is entered. */
274 for(i = 0; i < cm->abc->K; i++)
275 {
276 hmm->ins[k][i] += psi[ap[0]] *
277 cm2hmm_emit_prob(cm, cp9map, ap[0], i, k);
278 if(ap[1] != -1)
279 hmm->ins[k][i] += psi[ap[1]] *
280 cm2hmm_emit_prob(cm, cp9map, ap[1], i, k);
281 }
282 }
283
284 /* Done with emissions, fill in transitions of HMM (significantly more complex) */
285
286 /* Step 1. Fill 'special' transitions, those INTO node 1, the N->N and N->M_1 transitions,
287 * as well as transitions OUT of node M.
288 */
289 cm2hmm_special_trans_cp9(cm, hmm, cp9map, psi, tmap);
290
291 for(k = 1; k < hmm->M; k++)
292 {
293 cm2hmm_trans_probs_cp9(cm, hmm, cp9map, k, psi, tmap);
294 }
295
296 CPlan9Renormalize(hmm);
297
298 if(debug_level > 1)
299 debug_print_cp9_params(stdout, hmm, TRUE);
300 if(do_psi_test) {
301 /* Fill phi to check to make sure our HMM is "close enough" to our CM.
302 * phi[k][0..2] is the expected number of times HMM node k state 0 (match), 1(insert),
303 * or 2(delete) is entered. These should be *very close* (within 0.00001) to the psi
304 * values for the CM states that they map to (psi[v] is the expected number of times
305 * state v is entered in the CM).
306 */
307 fill_phi_cp9(hmm, &phi, 1, FALSE);
308 if((status = check_psi_vs_phi_cp9(cm, errbuf, cp9map, psi, phi, (double) thresh, debug_level)) != eslOK) return status;
309 for(k = 0; k <= hmm->M; k++) free(phi[k]);
310 free(phi);
311 }
312
313 free(ap);
314 free(psi);
315 cm_FreeTransitionMap(tmap);
316
317 *ret_hmm = hmm;
318 *ret_cp9map = cp9map;
319 return eslOK;
320
321 ERROR:
322 ESL_FAIL(eslEMEM, errbuf, "memory allocation error");
323 return 0; /* never reached */
324 }
325
326 /* Function to map an HMM to a CM:
327 * CP9_map_cm2hmm()
328 */
329
330 /**************************************************************************
331 * EPN 03.15.06
332 * Function: CP9_map_cm2hmm()
333 *
334 * Purpose: Determine maps between a CM and an HMM by filling 3 multi-dimensional
335 * arrays. All arrays must be pre-allocated and freed by caller.
336 * Args:
337 * CM_t *cm - the CM
338 * CP9Map *cp9map - map from the CM to the HMM and vice versa
339 * int debug_level - verbosity for debugging printf statements
340 * Returns: (void)
341 */
342 void
343 CP9_map_cm2hmm(CM_t *cm, CP9Map_t *cp9map, int debug_level)
344 {
345 int k; /* HMM node counter */
346 int ks; /* HMM state counter (0(Match) 1(insert) or 2(delete)*/
347 int n; /* CM node that maps to HMM node k */
348 int nn; /* CM node index */
349 int n_begr; /* CM node index */
350 int is_left; /* TRUE if HMM node k maps to left half of CM node n */
351 int is_right;/* TRUE if HMM node k maps to right half of CM node n */
352 int v; /* state index in CM */
353 int v1, v2;
354 CMEmitMap_t *emap; /* consensus emit map for the CM */
355
356 /* Map the nodes of each CM to consensus column indices and vice versa
357 * Prior to 10.26.06 I had a function called map_consensus_columns which did
358 * this, but it was replaced here by a CreateEmitMap() call, and a selective copying
359 * of the emitmap data to get the cp9map->nd2lpos and cp9map->nd2rpos data.
360 * When I implemented map_consensus_columns I was unaware CreateEmitMap() already
361 * did what I needed. (EPN) */
362 emap = CreateEmitMap(cm);
363
364 /* We copy the emitmap lpos and rpos values, but only for MATP, MATL, MATR,
365 * for any other node types cp9map->nd2lpos == cp9map->nd2rpos == -1 (this
366 * arrays are initialized to all -1 in AllocCP9Map()).
367 */
368 for(n = 0; n < cm->nodes; n++)
369 {
370 if(cm->ndtype[n] == MATP_nd ||
371 cm->ndtype[n] == MATL_nd)
372 {
373 cp9map->nd2lpos[n] = emap->lpos[n];
374 cp9map->pos2nd[cp9map->nd2lpos[n]] = n;
375 }
376 if(cm->ndtype[n] == MATP_nd ||
377 cm->ndtype[n] == MATR_nd)
378 {
379 cp9map->nd2rpos[n] = emap->rpos[n];
380 cp9map->pos2nd[cp9map->nd2rpos[n]] = n;
381 }
382 }
383 FreeEmitMap(emap);
384
385 /* Handle special case, HMM node k = 0 first */
386 /*ROOT_S*/
387 k = 0;
388 ks = 0;
389 v = 0;
390 map_helper(cm, cp9map, k, ks, v);
391
392 /*ROOT_IL*/
393 ks = 1;
394 v = 1;
395 map_helper(cm, cp9map, k, ks, v);
396
397 /*handle ROOT_IR at end of function*/
398
399 /* Step through HMM nodes, filling in maps as we go */
400 for(k = 1; k <= cp9map->hmm_M; k++)
401 {
402 n = cp9map->pos2nd[k];
403 if(cp9map->nd2lpos[n] == k)
404 {
405 is_left = TRUE;
406 is_right = FALSE;
407 }
408 else if(cp9map->nd2rpos[n] == k)
409 {
410 is_left = FALSE;
411 is_right = TRUE;
412 }
413 switch(cm->ndtype[n])
414 {
415 case ROOT_nd:
416 case BIF_nd:
417 case BEGL_nd:
418 case BEGR_nd:
419 case END_nd:
420 printf("ERROR: HMM node k doesn't map to MATP, MATR or MATL\n");
421 exit(1);
422 break;
423
424 case MATP_nd:
425 if(is_left)
426 {
427 ks = 0; /*match*/
428 v = cm->nodemap[n]; /*MATP_MP*/
429 map_helper(cm, cp9map, k, ks, v);
430 v = cm->nodemap[n] + 1; /*MATP_ML*/
431 map_helper(cm, cp9map, k, ks, v);
432
433 ks = 1; /*insert*/
434 v = cm->nodemap[n] + 4; /*MATP_IL*/
435 map_helper(cm, cp9map, k, ks, v);
436
437 ks = 2; /*delete*/
438 v = cm->nodemap[n] + 2; /*MATP_MR*/
439 map_helper(cm, cp9map, k, ks, v);
440 v = cm->nodemap[n] + 3; /*MATP_D*/
441 map_helper(cm, cp9map, k, ks, v);
442 }
443 else if(is_right)
444 {
445 ks = 0; /*match*/
446 v = cm->nodemap[n]; /*MATP_MP*/
447 map_helper(cm, cp9map, k, ks, v);
448 v = cm->nodemap[n] + 2; /*MATP_MR*/
449 map_helper(cm, cp9map, k, ks, v);
450
451 ks = 1; /*insert*/
452 /* whoa... careful, we want the CM state that will insert to the RIGHT
453 * of column k (the right consensus column modelled by MATP node n),
454 * but MATP_IR inserts to the LEFT of column k.
455 * What we need to determine is the CM node nn that models column k+1,
456 * and further which half (left or right) of nn models k+1, then
457 * we can map the HMM state to the correct CM state (see code).
458 */
459 if(k != cp9map->hmm_M) /* Special case if HMM node k is the last node (consensus column)
460 dealt below*/
461 {
462 nn = cp9map->pos2nd[k+1];
463 if(cp9map->nd2lpos[nn] == (k+1))
464 {
465 /* find the closest BEGR node above node nn */
466 n_begr = nn;
467 while(n_begr >= 0 && (cm->ndtype[n_begr] != BEGR_nd))
468 n_begr--;
469 if(n_begr == -1)
470 {
471 printf("ERROR: can't find BEGR node above node %d\n", nn);
472 printf("k is %d\n", k);
473 exit(1);
474 }
475 v = cm->nodemap[n_begr] + 1; /*BEGR_IL*/
476 map_helper(cm, cp9map, k, ks, v);
477 }
478 else if(cp9map->nd2rpos[nn] == (k+1))
479 {
480 /*simple*/
481 if(cm->ndtype[nn] == MATP_nd)
482 {
483 v = cm->nodemap[nn] + 5; /*MATP_IR*/
484 map_helper(cm, cp9map, k, ks, v);
485 }
486 else if(cm->ndtype[nn] == MATR_nd)
487 {
488 v = cm->nodemap[nn] + 2; /*MATR_IR*/
489 map_helper(cm, cp9map, k, ks, v);
490 }
491 }
492 } /* end of if (k != cp9map->hmm_M) */
493 else /* k == cp9map->hmm_M */
494 {
495 v = 2; /*ROOT_IR*/
496 map_helper(cm, cp9map, k, ks, v);
497 }
498 /* NOT DONE YET, the MATP_IR has to map to an HMM state,
499 * if the previous column (k-1) is modelled by a CM MATR or
500 * MATP node, then the above block will take care of this situation
501 * (in the previous iteration of this loop when k = k-1),
502 * HOWEVER, if (k-1) is modelled by a MATL, then this
503 * MATP_IR's contribution to the HMM will be ignored,
504 * unless we do something about it.
505 */
506 if(cp9map->nd2lpos[cp9map->pos2nd[k-1]] == (k-1)) /*k-1 modelled by MATL or MATP*/
507 {
508 if(cm->ndtype[cp9map->pos2nd[k-1]] != MATL_nd)
509 {
510 if(cm->ndtype[cp9map->pos2nd[k-1]] == MATP_nd)
511 {
512 /* A rare, but possible case. Previous column
513 * k-1 column is modelled by left half of the MATP
514 * node whose right half models column k.
515 * Proceed below.
516 */
517 }
518 else
519 {
520 printf("ERROR, full understanding of the CM architecture remains elusive (0)...\n");
521 exit(1);
522 }
523 }
524 v = cm->nodemap[n] + 5; /*MATP_IR*/
525 map_helper(cm, cp9map, (k-1), ks, v);
526 }
527
528 ks = 2; /*delete*/
529 v = cm->nodemap[n] + 1; /*MATP_ML*/
530 map_helper(cm, cp9map, k, ks, v);
531
532 v = cm->nodemap[n] + 3; /*MATP_D*/
533 map_helper(cm, cp9map, k, ks, v);
534 }
535 break;
536
537 case MATL_nd:
538 ks = 0; /*match*/
539 v = cm->nodemap[n]; /*MATL_ML*/
540 map_helper(cm, cp9map, k, ks, v);
541
542 ks = 1; /*insert*/
543 v = cm->nodemap[n] + 2; /*MATL_IL*/
544 map_helper(cm, cp9map, k, ks, v);
545
546 ks = 2; /*delete*/
547 v = cm->nodemap[n] + 1; /*MATL_D*/
548 map_helper(cm, cp9map, k, ks, v);
549
550 if(k == cp9map->hmm_M) /* can't forget about ROOT_IR */
551 {
552 ks = 1; /*insert*/
553 v = 2; /*ROOT_IR*/
554 map_helper(cm, cp9map, k, ks, v);
555 }
556
557 break;
558
559 case MATR_nd:
560 ks = 0; /*match*/
561 v = cm->nodemap[n]; /*MATR_MR*/
562 map_helper(cm, cp9map, k, ks, v);
563
564 ks = 1; /*insert*/
565 /* whoa... careful, we want the CM state that will insert to the RIGHT
566 * of column k (the consensus column modelled by MATR node n),
567 * but MATR_IR inserts to the LEFT of column k.
568 * What we need to determine is the CM node nn that models column k+1,
569 * and further which half (left or right) of nn models k+1, then
570 * we can map the HMM state to the correct CM state (see code).
571 */
572 /* Special case if HMM node k is the last node (consensus column) */
573 if(k != cp9map->hmm_M) /* we deal with situation if k == hmm_M below */
574 {
575 nn = cp9map->pos2nd[k+1];
576 if(cp9map->nd2lpos[nn] == (k+1))
577 {
578 /* find the closest BEGR node above node nn */
579 n_begr = nn;
580 while((cm->ndtype[n_begr] != BEGR_nd) && n_begr >= 0)
581 n_begr--;
582 if(n_begr == -1)
583 {
584 printf("ERROR: can't find BEGR node above node %d\n", nn);
585 exit(1);
586 }
587 v = cm->nodemap[n_begr] + 1; /*BEGR_IL*/
588 map_helper(cm, cp9map, k, ks, v);
589 }
590 else if(cp9map->nd2rpos[nn] == (k+1))
591 {
592 /*simple*/
593 if(cm->ndtype[nn] == MATP_nd)
594 {
595 v = cm->nodemap[nn] + 5;
596 map_helper(cm, cp9map, k, ks, v);
597 }
598 else if(cm->ndtype[nn] == MATR_nd)
599 {
600 v = cm->nodemap[nn] + 2; /*MATP_IR*/
601 map_helper(cm, cp9map, k, ks, v);
602 }
603 }
604 } /* end of if (k != cp9map->hmm_M) */
605 else /* k == cp9map->hmm_M */
606 {
607 v = 2; /*ROOT_IR*/
608 map_helper(cm, cp9map, k, ks, v);
609 }
610 if(cp9map->nd2lpos[cp9map->pos2nd[k-1]] == (k-1)) /*k-1 modelled by MATL*/
611 {
612 printf("ERROR, full understanding of the CM architecture remains elusive (1)...\n");
613 exit(1);
614 }
615
616 ks = 2; /*delete*/
617 v = cm->nodemap[n] + 1; /*MATR_D*/
618 map_helper(cm, cp9map, k, ks, v);
619 break;
620 }
621 }
622
623 /* Check to make sure that insert states map to exactly 1 HMM node state or 0 HMM states,
624 * if it's an ambiguity issue. */
625 for(v = 0; v <= cm->M; v++)
626 {
627 if((cm->sttype[v] == IL_st || cm->sttype[v] == IR_st) &&
628 ((cp9map->cs2hn[v][0] == -1) || cp9map->cs2hn[v][1] != -1))
629 {
630 if(cm->sttype[(v+1)] != E_st) /* v has been detached to remove ambiguities */
631 {
632 printf("ERROR during cp9map->cs2hn construction\ncp9map->cs2hn[%d][0]: %d | cp9map->cs2hn[%d][1]: %d AND v is an INSERT state\n", v, cp9map->cs2hn[v][0], v, cp9map->cs2hn[v][1]);
633 exit(1);
634 }
635 }
636 /* each CM state should map to only 1 HMM state. */
637 }
638
639 /* print cp9map->hns2cs, checking consistency with cp9map->cs2hn and cp9map->cs2hs along
640 the way. */
641 for(k = 0; k <= cp9map->hmm_M; k++)
642 {
643 for(ks = 0; ks < 3; ks++)
644 {
645 v1 = cp9map->hns2cs[k][ks][0];
646 v2 = cp9map->hns2cs[k][ks][1];
647 if(ks == 1 && v2 != -1)
648 {
649 printf("ERROR in CP9_map_cm2hmm: HMM insert state of node: %d\n\tmaps to 2 CM states (%d and %d)\n", k, v1, v2);
650 exit(1);
651 }
652
653 if(debug_level > 1)
654 printf("hns2cs[%3d][%3d][0]: %3d | hns2cs[%3d][%3d[1]: %3d\n", k, ks, v1, k, ks, v2);
655 if(v1 != -1 && (cp9map->cs2hn[v1][0] == k && cp9map->cs2hs[v1][0] == ks))
656 {
657 /* okay */
658 }
659 else if(v1 != -1 && (cp9map->cs2hn[v1][1] == k && cp9map->cs2hs[v1][1] == ks))
660 {
661 /* okay */
662 }
663 else if(v2 != -1 && (cp9map->cs2hn[v2][0] == k && cp9map->cs2hs[v2][0] == ks))
664 {
665 /* okay */
666 }
667 else if(v2 != -1 && (cp9map->cs2hn[v2][1] == k && cp9map->cs2hs[v2][1] == ks))
668 {
669 /* okay */
670 }
671 else if(v1 == -1 && v2 == -1 && (k == 0 && ks == 2))
672 {
673 /*okay - D_0 maps to nothing */
674 }
675 else if(v1 == -1 && v2 == -1 && (k != 0 || ks != 2))
676 /* only cp9map->hns2cs[0][2] (D_0) should map to nothing*/
677 {
678 /* not okay */
679 printf("maps inconsistent case 1, HMM node state (non D_0) maps to no CM state, v1: %d | v2: %d k: %d | ks: %d\n", v1, v2, k, ks);
680 exit(1);
681 }
682 else
683 {
684 /* not okay */
685 printf("maps inconsistent case 2 v1: %d | v2: %d k: %d | ks: %d\n", v1, v2, k, ks);
686 exit(1);
687 }
688 }
689 }
690 return;
691 }
692
693
694
695 /**************************************************************************
696 * EPN 03.15.06
697 * map_helper
698 *
699 * Helper function for map_cm2hmm_and_hmm2cm_cp9().
700 * UPDATED 11.13.06: Checks for, and refrains from mapping a CM insert state
701 * that has been detached (END_E-1 state) to remove ambiguities
702 * from the model to an HMM state.
703 *
704 * Purpose: Fill in specific parts of the maps, given k, ks, and v.
705 * Args:
706 * CM_t *cm - the CM
707 * CP9Map_t *cp9map - the CM to CP9 map
708 * int k - the hmm node coordinate we're filling maps in for
709 * int ks - the hmm state (0,1,or 2) coordinate we're filling maps in for
710 * int v - the CM state coordinate we're filling maps in for
711 * Returns: (void)
712 */
713 void
714 map_helper(CM_t *cm, CP9Map_t *cp9map, int k, int ks, int v)
715 {
716 if(ks == 1 && cm->sttype[(v+1)] == E_st) /* insert */
717 {
718 return;
719 }
720 if(cp9map->cs2hn[v][0] == -1)
721 {
722 cp9map->cs2hn[v][0] = k;
723 if(cp9map->cs2hs[v][0] != -1)
724 cm_Fail("ERROR in map_helper, cp9map->cs2hn[%d][0] is -1 but cp9map->cs2hs[%d][0] is not, this shouldn't happen.\n", v, v);
725 cp9map->cs2hs[v][0] = ks;
726 }
727 else if (cp9map->cs2hn[v][1] == -1)
728 {
729 cp9map->cs2hn[v][1] = k;
730 if(cp9map->cs2hs[v][1] != -1)
731 cm_Fail("ERROR in map_helper, cp9map->cs2hn[%d][0] is -1 but cp9map->cs2hs[%d][0] is not, this shouldn't happen.\n", v, v);
732 cp9map->cs2hs[v][1] = ks;
733 }
734 else
735 cm_Fail("ERROR in map_helper, cp9map->cs2hn[%d][1] is not -1, and we're trying to add to it, this shouldn't happen.\n", v);
736
737 if(cp9map->hns2cs[k][ks][0] == -1)
738 cp9map->hns2cs[k][ks][0] = v;
739 else if(cp9map->hns2cs[k][ks][1] == -1)
740 cp9map->hns2cs[k][ks][1] = v;
741 else
742 cm_Fail("ERROR in map_helper, cp9map->hns2cs[%d][%d][1] is not -1, and we're trying to add to it, this shouldn't happen.\n", k, ks);
743 return;
744 }
745
746 /**************************************************************************
747 * EPN 03.13.06
748 * cm2hmm_emit_prob()
749 *
750 * Purpose: For a specific CM state, determine the probability of emitting
751 * residue i of the 4-letter RNA alphabet {A,C,G,U}.
752 *
753 *
754 * Args:
755 * CM_t *cm - the CM
756 * CP9Map_t *cp9map - the map from the CM to HMM and vice versa
757 * int x - the CM state
758 * int i - the residue index in A=0, C=1, G=2, U=3
759 * int k - HMM node CM state x maps to
760 *
761 * Returns: (float) probability of emitting letter i from correct half of CM state x.
762 */
763 static float
764 cm2hmm_emit_prob(CM_t *cm, CP9Map_t *cp9map, int x, int i, int k)
765 {
766 float ret_eprob;
767 int is_left;
768 int j;
769
770 if(cp9map->nd2lpos[cp9map->pos2nd[k]] == k)
771 is_left = TRUE;
772 else
773 is_left = FALSE;
774
775 ret_eprob = 0.;
776
777 /* trivial for non MATP_MP */
778 if(cm->stid[x] != MATP_MP)
779 ret_eprob = cm->e[x][i];
780
781 else if(cm->stid[x] == MATP_MP)
782 {
783 /* determine which of the 16 indices to use */
784 if(is_left)
785 for(j = (i*cm->abc->K); j < ((i+1)*cm->abc->K); j++)
786 ret_eprob += cm->e[x][j];
787 else
788 for(j = i; j < (cm->abc->K*cm->abc->K); j+=cm->abc->K)
789 ret_eprob += cm->e[x][j];
790 }
791 return ret_eprob;
792 }
793
794
795
796 /**************************************************************************
797 * EPN 03.12.06
798 * cm2hmm_special_trans_cp9
799 *
800 * Purpose: Fill the special transition probabilities (those INTO HMM node 1
801 * and out of node M) for a CM plan 9 HMM given a CM.
802 *
803 * Args:
804 * CM_t *cm - the CM
805 * cplan9_s *hmm - the HMM
806 * CP9Map_t *cp9map - the map from the CM to HMM and vice versa
807 * double *psi - psi[v] is the expected number of times state v is entered
808 * in a CM parse
809 * char ***tmap; - the hard-coded transition map
810 *
811 * Returns: (void)
812 */
813 static void
814 cm2hmm_special_trans_cp9(CM_t *cm, CP9_t *hmm, CP9Map_t *cp9map, double *psi,
815 char ***tmap)
816 {
817 int status;
818 int *ap; /* CM states a' that map to HMM state a,
819 * ap[1] is -1 if only 1 CM state maps to a*/
820 int *bp; /* CM states b' that map to HMM state b,
821 * bp[1] is -1 if only 1 CM state maps to b*/
822 int k_state; /*either HMMMATCH, HMMINSERT, or HMMDELETE*/
823 int k; /* HMM node index */
824 int hmm_trans_idx; /*0-8; CTMM, CTMI, CTMD, CTIM, CTII, CTID, CTDM, CTDI, or CTDD*/
825 float d;
826
827 ESL_ALLOC(ap, sizeof (int) * 2);
828 ESL_ALLOC(bp, sizeof (int) * 2);
829
830 /* Fill all special transitions with virtual counts, later normalize these into
831 * probabilities. CM p9 Special transitions are: transitions into HMM node 1, and
832 * transitions out of HMM node hmm->M, and the N->N self transition.
833 *
834 * Strategy:
835 *
836 * for each HMM transition from special state B (M_0) (maps to CM's ROOT_S) and N (I_0)
837 * (maps to CM's ROOT_IL) to state b in HMM node 1:
838 * - determine CM state(s) b' (can be 1 or 2) that map to HMM state b.
839 * (we must be moving down the CM (i.e. state num (ROOT_S) = 0 and (ROOT_IL) = 1)
840 * - determine sum(B->b') or sum(N->b') the summed probability of all paths from state(s)
841 * B to b' or N to b'.
842 *
843 * set virtual counts of transitions as follows:
844 * hmm->t[0][CTMI] (B->N) = sum(over state(s) b' that map to N) psi[0] * ROOT_S -> b'
845 * (just ROOT_S->ROOT_IL)
846 * hmm->t[0][CTMD] (B->D_1) = sum(over state(s) b' that map to D_1) psi[0] * ROOT_S -> b'
847 * begin[1] (B->M_1) = sum(over state(s) b' that map to M_1) psi[0] * ROOT_S -> b'
848 * hmm->t[0][CTIM] = sum(over state(s) b' that map to M_1) psi[1] * ROOT_IL -> b'
849 * hmm->t[0][CTII] = sum(over state(s) b' that map to N) psi[1] * ROOT_IL -> b'
850 * (just ROOT_IL->ROOT_IL)
851 * hmm->t[0][CTID] = sum(over state(s) b' that map to D_1) psi[1] * ROOT_IL -> b'
852 *
853 * note: psi[0] = 1.0
854 *
855 * fill transitions out of node hmm->M.
856 * for each HMM transition from state a in HMM node M to either state END_E or state b
857 * in node M:
858 * - determine CM state(s) a' and b' (can be 1 or 2 for each a' and b') that map
859 * to HMM states a and b respectively
860 *
861 * - IF a' > b' (we're moving DOWN the CM)
862 * determine sum(a'->b') the summed probability of all paths from state(s)
863 * a' to b'.
864 *
865 * set virtual counts of HMM transition a->b as
866 * sum(over states a' and b') psi[a'] * a' -> b'
867 *
868 * - ELSE IF a' < b' (we're moving UP the CM)
869 * determine sum(b'->a') the summed probability of all paths from state(s)
870 * b' to a'
871 *
872 * set virtual counts of HMM transition a->b as
873 * sum(over states a' and b') psi[b'] * b' -> a'
874 *
875 * Where psi[x] is the expected number of times CM state x is entered, as calc'ed
876 * in cm_ExpectedStateOccapancy(). psi[0] = 1.0
877 *
878 */
879
880 /* Set up the probability of transitioning into the first HMM node's states. */
881
882 /* Transition 1 CTMM*/
883 /* Special case, transition prob into M_1 from B is actually
884 * hmm->begin[1]; first we calc what it should be, then
885 * we switch hmm->t[0][CTMM] and hmm->begin[1].
886 */
887 k = 0;
888 k_state = HMMMATCH;
889 ap[0] = cp9map->hns2cs[k][k_state][0];
890 ap[1] = cp9map->hns2cs[k][k_state][1];
891
892 k_state = HMMMATCH;
893 bp[0] = cp9map->hns2cs[k+1][k_state][0];
894 bp[1] = cp9map->hns2cs[k+1][k_state][1];
895 /*printf("0 CTMM: k: %4d | n: %4d | ap[0]: %4d ap[1]: %4d | bp[0]: %4d bp[1]: %4d\n", k, n, ap[0], ap[1], bp[0], bp[1]);*/
896 hmm_trans_idx = CTMM;
897 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
898 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
899 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
900 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
901 /*hmm_set_single_trans_cp9(cm, hmm, cp9map, ap, bp, k, hmm_trans_idx, psi, tmap);*/
902 /* switch 'em */
903 hmm->begin[1] = hmm->t[0][CTMM];
904 hmm->t[0][CTMM] = 0.;
905
906 /* Transition 2: CTMI; B -> N
907 */
908 k = 0;
909 k_state = HMMMATCH;
910 ap[0] = cp9map->hns2cs[k][k_state][0];
911 ap[1] = cp9map->hns2cs[k][k_state][1];
912
913 k_state = HMMINSERT;
914 bp[0] = cp9map->hns2cs[k][k_state][0];
915 bp[1] = cp9map->hns2cs[k][k_state][1];
916 hmm_trans_idx = CTMI;
917 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
918 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
919 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
920 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
921
922 /* Transition 3: CTMD; B -> D_1 */
923 k = 0;
924 k_state = HMMMATCH;
925 ap[0] = cp9map->hns2cs[k][k_state][0];
926 ap[1] = cp9map->hns2cs[k][k_state][1];
927
928 k_state = HMMDELETE;
929 bp[0] = cp9map->hns2cs[k+1][k_state][0];
930 bp[1] = cp9map->hns2cs[k+1][k_state][1];
931 hmm_trans_idx = CTMD;
932 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
933 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
934 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
935 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
936
937 /* Transition 4: CTIM; N -> M_1*/
938 k = 0;
939 k_state = HMMINSERT;
940 ap[0] = cp9map->hns2cs[k][k_state][0];
941 ap[1] = cp9map->hns2cs[k][k_state][1];
942
943 k_state = HMMMATCH;
944 bp[0] = cp9map->hns2cs[k+1][k_state][0];
945 bp[1] = cp9map->hns2cs[k+1][k_state][1];
946 hmm_trans_idx = CTIM;
947 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
948 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
949 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
950 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
951
952 /* Transition 5: CTII; N -> N */
953 k = 0;
954 k_state = HMMINSERT;
955 ap[0] = cp9map->hns2cs[k][k_state][0];
956 ap[1] = cp9map->hns2cs[k][k_state][1];
957
958 k_state = HMMINSERT;
959 bp[0] = cp9map->hns2cs[k][k_state][0];
960 bp[1] = cp9map->hns2cs[k][k_state][1];
961 hmm_trans_idx = CTII;
962 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
963 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
964 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
965 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
966
967 /* Transition 6: CTID; N -> D_1 */
968 k = 0;
969 k_state = HMMINSERT;
970 ap[0] = cp9map->hns2cs[k][k_state][0];
971 ap[1] = cp9map->hns2cs[k][k_state][1];
972
973 k_state = HMMDELETE;
974 bp[0] = cp9map->hns2cs[k+1][k_state][0];
975 bp[1] = cp9map->hns2cs[k+1][k_state][1];
976 hmm_trans_idx = CTID;
977 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
978 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
979 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
980 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
981
982 /* Transitions 7-9, CTDM, CTDI, CTDD, all 0.0, there's no D_0 state */
983 hmm->t[0][CTDM] = 0.;
984 hmm->t[0][CTDI] = 0.;
985 hmm->t[0][CTDD] = 0.;
986
987 /* Finally, normalize the transition probabilities
988 * Not strictly necessary, a CPlan9Renormalize() call will do this*/
989
990 d =esl_vec_FSum(hmm->begin+1, hmm->M) + hmm->t[0][CTMI] + hmm->t[0][CTMD];
991 esl_vec_FScale(hmm->begin+1, hmm->M, 1./d);
992 hmm->t[0][CTMI] /= d;
993 hmm->t[0][CTMD] /= d;
994
995 esl_vec_FNorm(hmm->t[0]+cp9_TRANS_INSERT_OFFSET, 3); /* transitions out of insert for node 0 (state N)*/
996
997 k = 0;
998 /*
999 printf("S hmm->t[%d][CTMM]: %f\n", k, hmm->t[k][CTMM]);
1000 printf("S hmm->begin[%3d]: %f\n", 1, hmm->begin[1]);
1001 printf("S hmm->t[%d][CTMI]: %f\n", k, hmm->t[k][CTMI]);
1002 printf("S hmm->t[%d][CTMD]: %f\n", k, hmm->t[k][CTMD]);
1003 printf("S hmm->t[%d][CTIM]: %f\n", k, hmm->t[k][CTIM]);
1004 printf("S hmm->t[%d][CTII]: %f\n", k, hmm->t[k][CTII]);
1005 printf("S hmm->t[%d][CTID]: %f\n", k, hmm->t[k][CTID]);
1006 printf("S hmm->t[%d][CTDM]: %f\n", k, hmm->t[k][CTDM]);
1007 printf("S hmm->t[%d][CTDI]: %f\n", k, hmm->t[k][CTDI]);
1008 printf("S hmm->t[%d][CTDD]: %f\n\n", k, hmm->t[k][CTDD]);
1009 printf("\n");
1010 */
1011 /*********************************************************************/
1012 /*********************************************************************/
1013 /* Now handle transitions OUT of HMM node M */
1014 /* Transition 1: CTMM; this is M_M -> E
1015 * a = node M, match state.
1016 * b = E state
1017 */
1018 k = hmm->M;
1019 k_state = HMMMATCH;
1020 ap[0] = cp9map->hns2cs[k][k_state][0];
1021 ap[1] = cp9map->hns2cs[k][k_state][1];
1022
1023 bp[0] = (cm->M) - 1;
1024 bp[1] = -1;
1025 hmm_trans_idx = CTMM;
1026 /* special case, this transition is hmm->end[hmm->M] NOT hmm->t[M][CTMM]. */
1027 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
1028 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
1029 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
1030 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
1031 /* switch 'em */
1032 hmm->end[hmm->M] = hmm->t[hmm->M][CTMM];
1033 hmm->t[hmm->M][CTMM] = 0.;
1034
1035 /* Transition 2: CTMI; this is M_M -> I_M
1036 * a = node M, match state
1037 * b = node M, insert state
1038 */
1039 k = hmm->M;
1040 k_state = HMMMATCH;
1041 ap[0] = cp9map->hns2cs[k][k_state][0];
1042 ap[1] = cp9map->hns2cs[k][k_state][1];
1043
1044 k_state = HMMINSERT;
1045 bp[0] = cp9map->hns2cs[k][k_state][0];
1046 bp[1] = cp9map->hns2cs[k][k_state][1];
1047 /*printf("CTMI: k: %4d | n: %4d | ap[0]: %4d ap[1]: %4d | bp[0]: %4d bp[1]: %4d\n", k, cp9map->pos2nd[k], ap[0], ap[1], bp[0], bp[1]);*/
1048 hmm_trans_idx = CTMI;
1049 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
1050 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
1051 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
1052 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
1053
1054 /* Transition 3: CTMD, no corresponding transitiona
1055 * This is an illegal HMM transition.
1056 */
1057 hmm->t[hmm->M][CTMD] = 0.;
1058
1059 /* Transition 4: CTIM; this is I_M -> E
1060 * a = node k, insert state.
1061 * b = E state
1062 */
1063 k = hmm->M;
1064 k_state = HMMINSERT;
1065 ap[0] = cp9map->hns2cs[k][k_state][0];
1066 ap[1] = cp9map->hns2cs[k][k_state][1];
1067
1068 bp[0] = (cm->M) - 1;
1069 bp[1] = -1;
1070 hmm_trans_idx = CTIM;
1071 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
1072 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
1073 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
1074 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
1075
1076 /* Transition 5: CTII; this is I_M -> I_M
1077 * a = node M, insert state.
1078 * b = node M, insert state.
1079 */
1080 k = hmm->M;
1081 k_state = HMMINSERT;
1082 ap[0] = cp9map->hns2cs[k][k_state][0];
1083 ap[1] = cp9map->hns2cs[k][k_state][1];
1084
1085 k_state = HMMINSERT;
1086 bp[0] = cp9map->hns2cs[k][k_state][0];
1087 bp[1] = cp9map->hns2cs[k][k_state][1];
1088 /*printf("CTII: k: %4d | n: %4d | ap[0]: %4d ap[1]: %4d | bp[0]: %4d bp[1]: %4d\n", k, cp9map->pos2nd[k], ap[0], ap[1], bp[0], bp[1]);*/
1089 hmm_trans_idx = CTII;
1090 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
1091 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
1092 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
1093 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
1094
1095 /* Transition 6: CTID, no corresponding transition.
1096 * This is an illegal HMM transition.
1097 */
1098 hmm->t[hmm->M][CTID] = 0.;
1099
1100 /* Transition 7: CTDM; this is D_M -> E
1101 * a = node M, delete state.
1102 * b = E state
1103 */
1104 k = hmm->M;
1105 k_state = HMMDELETE;
1106 ap[0] = cp9map->hns2cs[k][k_state][0];
1107 ap[1] = cp9map->hns2cs[k][k_state][1];
1108
1109 bp[0] = (cm->M) - 1;
1110 bp[1] = -1;
1111 hmm_trans_idx = CTDM;
1112 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
1113 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
1114 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
1115 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
1116
1117 /* Transition 8: CTDI - this is D_M -> I_M
1118 * a = node M, delete state.
1119 * b = node M, insert state.
1120 */
1121 k = hmm->M;
1122 k_state = HMMDELETE;
1123 ap[0] = cp9map->hns2cs[k][k_state][0];
1124 ap[1] = cp9map->hns2cs[k][k_state][1];
1125
1126 k_state = HMMINSERT;
1127 bp[0] = cp9map->hns2cs[k][k_state][0];
1128 bp[1] = cp9map->hns2cs[k][k_state][1];
1129 /*printf("CTDI: k: %4d | n: %4d | ap[0]: %4d ap[1]: %4d | bp[0]: %4d bp[1]: %4d\n", k, cp9map->pos2nd[k], ap[0], ap[1], bp[0], bp[1]);*/
1130 hmm_trans_idx = CTDI;
1131 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
1132 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
1133 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
1134 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
1135
1136 /* Transition 9: CTDD, no corresponding transition.
1137 * This is an illegal HMM transition.
1138 */
1139 hmm->t[hmm->M][CTDD] = 0.;
1140
1141 /* Finally, normalize the transition probabilities
1142 * Not strictly necessary, a CPlan9Renormalize() call will do this*/
1143 k = hmm->M;
1144 d =esl_vec_FSum(hmm->t[k], 4) + hmm->end[k];
1145 esl_vec_FScale(hmm->t[k], 4, 1./d);
1146 hmm->end[k] /= d;
1147 esl_vec_FNorm(hmm->t[k]+cp9_TRANS_INSERT_OFFSET, 3);
1148 esl_vec_FNorm(hmm->t[k]+cp9_TRANS_DELETE_OFFSET, 3);
1149
1150 /*
1151 printf("S hmm->t[%d][CTMM]: %f\n", k, hmm->t[k][CTMM]);
1152 printf("S hmm->end[%3d] : %f\n", k, hmm->end[k]);
1153 printf("S hmm->t[%d][CTMI]: %f\n", k, hmm->t[k][CTMI]);
1154 printf("S hmm->t[%d][CTMD]: %f\n", k, hmm->t[k][CTMD]);
1155 printf("S hmm->t[%d][CTIM]: %f\n", k, hmm->t[k][CTIM]);
1156 printf("S hmm->t[%d][CTII]: %f\n", k, hmm->t[k][CTII]);
1157 printf("S hmm->t[%d][CTID]: %f\n", k, hmm->t[k][CTID]);
1158 printf("S hmm->t[%d][CTDM]: %f\n", k, hmm->t[k][CTDM]);
1159 printf("S hmm->t[%d][CTDI]: %f\n", k, hmm->t[k][CTDI]);
1160 printf("S hmm->t[%d][CTDD]: %f\n\n", k, hmm->t[k][CTDD]);
1161 printf("\n");
1162 */
1163
1164 free(ap);
1165 free(bp);
1166 return;
1167
1168 ERROR:
1169 cm_Fail("Memory allocation error.");
1170 }
1171
1172 /**************************************************************************
1173 * EPN 03.12.06
1174 * cm2hmm_trans_probs_cp9()
1175 *
1176 * Purpose: Fill transition "virtual counts" from HMM node k to
1177 * the following HMM node (k+1) given a CM, then normalize to
1178 * probabilities.
1179 *
1180 * Reference: Zasha Weinberg thesis p.123-124
1181 *
1182 * Args:
1183 * CM_t *cm - the CM
1184 * cplan9_s *hmm - the CM plan 9 HMM
1185 * CP9Map_t *cp9map - the map from the CM to HMM and vice versa
1186 * int k - the HMM node we're filling transitions for
1187 * double *psi - psi[v] is the expected number of times state v is entered
1188 * in a CM parse
1189 * char ***tmap; - the hard-coded transition map
1190 *
1191 * Returns: (void)
1192 */
1193 static void
1194 cm2hmm_trans_probs_cp9(CM_t *cm, CP9_t *hmm, CP9Map_t *cp9map, int k, double *psi, char ***tmap)
1195 {
1196 int status;
1197 int *ap; /* CM states a' that map to HMM state a,
1198 * ap[1] is -1 if only 1 CM state maps to a*/
1199 int *bp; /* CM states b' that map to HMM state b,
1200 * bp[1] is -1 if only 1 CM state maps to b*/
1201
1202 int k_state; /*either HMMMATCH, HMMINSERT, or HMMDELETE*/
1203
1204 int hmm_trans_idx; /*0-8; CTMM, CTMI, CTMD, CTIM, CTII, CTID, CTDM, CTDI, or CTDD*/
1205 float d;
1206
1207 /*printf("in cm2hmm_trans_probs_cp9: k: %d\n", k);*/
1208
1209 ESL_ALLOC(ap, sizeof (int) * 2);
1210 ESL_ALLOC(bp, sizeof (int) * 2);
1211
1212 /* Fill all 9 transitions with virtual counts, later normalize these into
1213 * probabilities.
1214 *
1215 * Strategy:
1216 *
1217 * for each HMM transition from state a in node k to state b in either node k or k+1:
1218 * - determine CM state(s) a' and b' (can be 1 or 2 for each a' and b') that map
1219 * to HMM states a and b respectively
1220 *
1221 * - IF a' > b' (we're moving DOWN the CM)
1222 * determine sum(a'->b') the summed probability of all paths from state(s)
1223 * a' to b'.
1224 *
1225 * set virtual counts of HMM transition a->b as
1226 * sum(over states a' and b') psi[a'] * a' -> b'
1227 *
1228 * - ELSE IF a' < b' (we're moving UP the CM)
1229 * determine sum(b'->a') the summed probability of all paths from state(s)
1230 * b' to a'
1231 *
1232 * set virtual counts of HMM transition a->b as
1233 * sum(over states a' and b') psi[b'] * b' -> a'
1234 *
1235 * Where psi[x] is the expected number of times CM state x is entered, as calc'ed
1236 * in cm_ExpectedStateOccupancy().
1237 *
1238 */
1239
1240 /* Transition 1: CTMM
1241 * a = node k, match state.
1242 * b = node k+1, match state.
1243 */
1244 k_state = HMMMATCH;
1245 ap[0] = cp9map->hns2cs[k][k_state][0];
1246 ap[1] = cp9map->hns2cs[k][k_state][1];
1247
1248 k_state = HMMMATCH;
1249 bp[0] = cp9map->hns2cs[k+1][k_state][0];
1250 bp[1] = cp9map->hns2cs[k+1][k_state][1];
1251 hmm_trans_idx = CTMM;
1252 /*////printf("CTMM: k: %4d | n: %4d | ap[0]: %4d ap[1]: %4d | bp[0]: %4d bp[1]: %4d\n", k, cp9map->pos2nd[k], ap[0], ap[1], bp[0], bp[1]);*/
1253 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
1254 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
1255 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
1256 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
1257 /*hmm_set_single_trans_cp9(cm, hmm, cp9map, ap, bp, k, hmm_trans_idx, psi, tmap);*/
1258
1259 /* Transition 2: CTMI
1260 * a = node k, match state.
1261 * b = node k, insert state.
1262 */
1263 k_state = HMMMATCH;
1264 ap[0] = cp9map->hns2cs[k][k_state][0];
1265 ap[1] = cp9map->hns2cs[k][k_state][1];
1266
1267 k_state = HMMINSERT;
1268 bp[0] = cp9map->hns2cs[k][k_state][0];
1269 bp[1] = cp9map->hns2cs[k][k_state][1];
1270 /*////printf("CTMI: k: %4d | n: %4d | ap[0]: %4d ap[1]: %4d | bp[0]: %4d bp[1]: %4d\n", k, cp9map->pos2nd[k], ap[0], ap[1], bp[0], bp[1]);*/
1271 hmm_trans_idx = CTMI;
1272 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
1273 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
1274 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
1275 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
1276
1277 /* Transition 3: CTMD
1278 * a = node k, match state.
1279 * b = node k+1, delete state.
1280 */
1281 k_state = HMMMATCH;
1282 ap[0] = cp9map->hns2cs[k][k_state][0];
1283 ap[1] = cp9map->hns2cs[k][k_state][1];
1284
1285 k_state = HMMDELETE;
1286 bp[0] = cp9map->hns2cs[k+1][k_state][0];
1287 bp[1] = cp9map->hns2cs[k+1][k_state][1];
1288 /*////printf("CTMD: k: %4d | n: %4d | ap[0]: %4d ap[1]: %4d | bp[0]: %4d bp[1]: %4d\n", k, cp9map->pos2nd[k], ap[0], ap[1], bp[0], bp[1]);*/
1289 hmm_trans_idx = CTMD;
1290 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
1291 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
1292 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
1293 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
1294
1295 /* Transition 4: CTIM
1296 * a = node k, insert state.
1297 * b = node k+1, match state.
1298 */
1299 k_state = HMMINSERT;
1300 ap[0] = cp9map->hns2cs[k][k_state][0];
1301 ap[1] = cp9map->hns2cs[k][k_state][1];
1302
1303 k_state = HMMMATCH;
1304 bp[0] = cp9map->hns2cs[k+1][k_state][0];
1305 bp[1] = cp9map->hns2cs[k+1][k_state][1];
1306 /*////printf("CTIM: k: %4d | n: %4d | ap[0]: %4d ap[1]: %4d | bp[0]: %4d bp[1]: %4d\n", k, cp9map->pos2nd[k], ap[0], ap[1], bp[0], bp[1]);*/
1307 hmm_trans_idx = CTIM;
1308 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
1309 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
1310 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
1311 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
1312
1313 /* Transition 5: CTII
1314 * a = node k, insert state.
1315 * b = node k, insert state.
1316 */
1317 k_state = HMMINSERT;
1318 ap[0] = cp9map->hns2cs[k][k_state][0];
1319 ap[1] = cp9map->hns2cs[k][k_state][1];
1320
1321 k_state = HMMINSERT;
1322 bp[0] = cp9map->hns2cs[k][k_state][0];
1323 bp[1] = cp9map->hns2cs[k][k_state][1];
1324 /*////printf("CTII: k: %4d | n: %4d | ap[0]: %4d ap[1]: %4d | bp[0]: %4d bp[1]: %4d\n", k, cp9map->pos2nd[k], ap[0], ap[1], bp[0], bp[1]);*/
1325
1326 hmm_trans_idx = CTII;
1327 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
1328 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
1329 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
1330 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
1331
1332 /* Transition 6: CTID - a CM plan 9 transition not in plan 7
1333 * a = node k, insert state.
1334 * b = node k+1, delete state.
1335 */
1336 k_state = HMMINSERT;
1337 ap[0] = cp9map->hns2cs[k][k_state][0];
1338 ap[1] = cp9map->hns2cs[k][k_state][1];
1339
1340 k_state = HMMDELETE;
1341 bp[0] = cp9map->hns2cs[k+1][k_state][0];
1342 bp[1] = cp9map->hns2cs[k+1][k_state][1];
1343 /*////printf("CTID: k: %4d | n: %4d | ap[0]: %4d ap[1]: %4d | bp[0]: %4d bp[1]: %4d\n", k, cp9map->pos2nd[k], ap[0], ap[1], bp[0], bp[1]);*/
1344 hmm_trans_idx = CTID;
1345 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
1346 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
1347 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
1348 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
1349
1350 /* Transition 7: CTDM
1351 * a = node k, delete state.
1352 * b = node k+1, match state.
1353 */
1354 k_state = HMMDELETE;
1355 ap[0] = cp9map->hns2cs[k][k_state][0];
1356 ap[1] = cp9map->hns2cs[k][k_state][1];
1357
1358 k_state = HMMMATCH;
1359 bp[0] = cp9map->hns2cs[k+1][k_state][0];
1360 bp[1] = cp9map->hns2cs[k+1][k_state][1];
1361 /*////printf("CTDM: k: %4d | n: %4d | ap[0]: %4d ap[1]: %4d | bp[0]: %4d bp[1]: %4d\n", k, cp9map->pos2nd[k], ap[0], ap[1], bp[0], bp[1]);*/
1362 hmm_trans_idx = CTDM;
1363 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
1364 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
1365 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
1366 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
1367
1368 /* Transition 8: CTDI - a CM plan 9 transition not in plan 9
1369 * a = node k, delete state.
1370 * b = node k, insert state.
1371 */
1372 k_state = HMMDELETE;
1373 ap[0] = cp9map->hns2cs[k][k_state][0];
1374 ap[1] = cp9map->hns2cs[k][k_state][1];
1375
1376 k_state = HMMINSERT;
1377 bp[0] = cp9map->hns2cs[k][k_state][0];
1378 bp[1] = cp9map->hns2cs[k][k_state][1];
1379 /*////printf("CTDI: k: %4d | n: %4d | ap[0]: %4d ap[1]: %4d | bp[0]: %4d bp[1]: %4d\n", k, cp9map->pos2nd[k], ap[0], ap[1], bp[0], bp[1]);*/
1380 hmm_trans_idx = CTDI;
1381 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
1382 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
1383 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
1384 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
1385
1386 /* Transition 9: CTDD
1387 * a = node k, delete state.
1388 * b = node k+1, delete state.
1389 */
1390 k_state = HMMDELETE;
1391 ap[0] = cp9map->hns2cs[k][k_state][0];
1392 ap[1] = cp9map->hns2cs[k][k_state][1];
1393
1394 k_state = HMMDELETE;
1395 bp[0] = cp9map->hns2cs[k+1][k_state][0];
1396 bp[1] = cp9map->hns2cs[k+1][k_state][1];
1397 /*////printf("CTDD: k: %4d | n: %4d | ap[0]: %4d ap[1]: %4d | bp[0]: %4d bp[1]: %4d\n", k, cp9map->pos2nd[k], ap[0], ap[1], bp[0], bp[1]);*/
1398 hmm_trans_idx = CTDD;
1399 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[0], k, hmm_trans_idx, psi, tmap);
1400 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[0], bp[1], k, hmm_trans_idx, psi, tmap);
1401 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[0], k, hmm_trans_idx, psi, tmap);
1402 hmm_add_single_trans_cp9(cm, hmm, cp9map, ap[1], bp[1], k, hmm_trans_idx, psi, tmap);
1403
1404 /* Finally, normalize the transition probabilities
1405 * Not strictly necessary, a CPlan9Renormalize() call will do this*/
1406 d = esl_vec_FSum(hmm->t[k], 4) + hmm->end[k];
1407 esl_vec_FScale(hmm->t[k], 4, 1./d);
1408 hmm->end[k] /= d;
1409
1410 esl_vec_FNorm(hmm->t[k]+cp9_TRANS_INSERT_OFFSET, 3);
1411 esl_vec_FNorm(hmm->t[k]+cp9_TRANS_DELETE_OFFSET, 3);
1412 /* print transition probs for HMM */
1413 /*
1414 printf("hmm->t[%d][CTMM]: %f\n", k, hmm->t[k][CTMM]);
1415 printf("hmm->t[%d][CTMI]: %f\n", k, hmm->t[k][CTMI]);
1416 printf("hmm->t[%d][CTMD]: %f\n", k, hmm->t[k][CTMD]);
1417 printf("hmm->t[%d][CTMEL]: %f\n", k, hmm->t[k][CTMD]);
1418 printf("hmm->t[%d][CTIM]: %f\n", k, hmm->t[k][CTIM]);
1419 printf("hmm->t[%d][CTII]: %f\n", k, hmm->t[k][CTII]);
1420 printf("hmm->t[%d][CTID]: %f\n", k, hmm->t[k][CTID]);
1421 printf("hmm->t[%d][CTDM]: %f\n", k, hmm->t[k][CTDM]);
1422 printf("hmm->t[%d][CTDI]: %f\n", k, hmm->t[k][CTDI]);
1423 printf("hmm->t[%d][CTDD]: %f\n\n", k, hmm->t[k][CTDD]);
1424 printf("\n");
1425 */
1426 free(ap);
1427 free(bp);
1428 return;
1429
1430 ERROR:
1431 cm_Fail("Memory allocation error.");
1432 }
1433
1434 /**************************************************************************
1435 * EPN 03.14.06
1436 * fill_phi_cp9()
1437 *
1438 * Purpose: Fill phi matrix for a CM plan 9 HMM.
1439 * phi[k][v] is the expected number of times
1440 * HMM node k's state v (either 0 (match), 1 (insert) or
1441 * (2) delete) is entered in the HMM.
1442 *
1443 * Args:
1444 * cplan9_s *hmm - the HMM
1445 * double **phi - phi array, phi[k][v] is expected number of times
1446 * state v (0 = match, 1 insert, 2 = delete) in
1447 * node k is visited. Node 0 is special,
1448 * state 0 = B state, state 1 = N_state, state 2 = NULL
1449 * int spos - the first original consensus column this CP9 HMM
1450 * models (only != 1 if we're building a CP9 for a sub CM).
1451 * int entered_only - calculated expected number of times each state is
1452 * entered only, ignored insert->insert self transitions
1453 *
1454 * Notes:
1455 * - phi is allocated here, must be freed by caller.
1456 * Returns: ret_phi
1457 */
1458 void
1459 fill_phi_cp9(CP9_t *hmm, double ***ret_phi, int spos, int entered_only)
1460 {
1461 int status;
1462 int k;
1463 double **phi;
1464 double *phi_ins = NULL;
1465
1466 ESL_ALLOC(phi, sizeof(double *) * (hmm->M+1));
1467 for(k = 0; k <= hmm->M; k++)
1468 ESL_ALLOC(phi[k], sizeof(double) * 3);
1469
1470 if(entered_only) ESL_ALLOC(phi_ins, sizeof(double) * (hmm->M+1));
1471
1472 /* Initialize phi values as all 0.0 */
1473 for (k = 0; k <= hmm->M; k++)
1474 phi[k][0] = phi[k][1] = phi[k][2] = 0.;
1475
1476 /* the M_spos-1 is the B state, where all parses start */
1477 phi[spos-1][HMMMATCH] = 1.0;
1478 phi[spos-1][HMMINSERT] = phi[spos-1][HMMMATCH] * hmm->t[spos-1][CTMI];
1479 if(entered_only) phi_ins[spos-1] = phi[spos-1][HMMINSERT];
1480 phi[spos-1][HMMINSERT] += phi[spos-1][HMMINSERT] * (hmm->t[spos-1][CTII] /
1481 (1. - hmm->t[spos-1][CTII]));
1482 phi[spos-1][HMMDELETE] = 0.;
1483
1484 /* Handle all other nodes (including M) */
1485 for (k = 1; k <= hmm->M; k++)
1486 {
1487 if(k == (spos-1))
1488 continue;
1489
1490 /* match could've come from k-1 match, k-1 insert or k-1 delete */
1491 phi[k][HMMMATCH] += phi[k-1][HMMMATCH] * hmm->t[k-1][CTMM];
1492 phi[k][HMMMATCH] += phi[k-1][HMMDELETE] * hmm->t[k-1][CTDM];
1493 phi[k][HMMMATCH] += phi[k-1][HMMINSERT] * hmm->t[k-1][CTIM];
1494 phi[k][HMMMATCH] += hmm->begin[k];
1495
1496 /* again, we have to do deletes prior to inserts */
1497 /* deletes could've come from k-1 match, k-1 delete, k-1 insert */
1498 phi[k][HMMDELETE] += phi[k-1][HMMMATCH] * hmm->t[k-1][CTMD];
1499 phi[k][HMMDELETE] += phi[k-1][HMMINSERT] * hmm->t[k-1][CTID];
1500 phi[k][HMMDELETE] += phi[k-1][HMMDELETE] * hmm->t[k-1][CTDD];
1501
1502 /* inserts could've come from k match, k delete, or k insert */
1503 phi[k][HMMINSERT] += phi[k][HMMMATCH] * hmm->t[k][CTMI];
1504 phi[k][HMMINSERT] += phi[k][HMMDELETE] * hmm->t[k][CTDI];
1505 /* self loops are special */
1506 if(entered_only) phi_ins[k] = phi[k][HMMINSERT]; /* we'll copy it back at the end */
1507 phi[k][HMMINSERT] += (phi[k][HMMINSERT] * (hmm->t[k][CTII] / (1-hmm->t[k][CTII])));
1508
1509 /*printf("phi[%d][HMMMATCH]: %f\n", k, phi[k][HMMMATCH]);
1510 printf("phi[%d][HMMINSERT]: %f\n", k, phi[k][HMMINSERT]);
1511 printf("phi[%d][HMMDELETE]: %f\n", k, phi[k][HMMDELETE]);
1512 */
1513 }
1514 if(entered_only) { /* overwrite phi insert probabilities */
1515 for(k = 0; k <= hmm->M; k++) phi[k][HMMINSERT] = phi_ins[k];
1516 free(phi_ins);
1517 }
1518 *ret_phi = phi;
1519 return;
1520
1521 ERROR:
1522 cm_Fail("Memory allocation error.");
1523 }
1524
1525
1526 /**************************************************************************
1527 * EPN 03.15.06
1528 * Function: hmm_add_single_trans_cp9()
1529 *
1530 * Purpose: Add a virtual counts contribution to a single CM plan 9 HMM transition.
1531 *
1532 * Args:
1533 * CM_t *cm - the CM
1534 * cplan9_s *hmm - the HMM
1535 * CP9Map_t *cp9map - the map from the CM to HMM and vice versa
1536 * int a - a CM state that maps to HMM state we're transitioning out of
1537 * int b - a CM state that maps to HMM state we're transitioning into
1538 * int k - the HMM node we're setting a single transition for
1539 * int hmm_trans_idx - 0-8, the HMM transition index we're setting
1540 * double *psi - psi[v] is the expected number of times state v is entered
1541 * in a CM parse
1542 * char ***tmap; - the hard-coded transition map
1543 * Returns: (void)
1544 */
1545 static void
1546 hmm_add_single_trans_cp9(CM_t *cm, CP9_t *hmm, CP9Map_t *cp9map, int a, int b, int k, int hmm_trans_idx,
1547 double *psi, char ***tmap)
1548 {
1549 /* check if we've got real CM state ids */
1550 /*
1551 printf("\t\tin hmm_add_single_trans_cp9, a: %d | b: %d | k: %d | hmm_trans_idx: %d\n", a, b, k, hmm_trans_idx);
1552 printf("\t\tbeg hmm_cm->t[%d][%d] : %.9f\n", k, hmm_trans_idx, hmm->t[k][hmm_trans_idx]);
1553 */
1554 if(a == -1 || b == -1)
1555 return;
1556
1557 if(a <= b) /* going DOWN the CM */
1558 hmm->t[k][hmm_trans_idx] += psi[a] * cm_sum_subpaths_cp9(cm, cp9map, a, b, tmap, k, psi);
1559 else if (a > b) /* going UP the CM */
1560 hmm->t[k][hmm_trans_idx] += psi[b] * cm_sum_subpaths_cp9(cm, cp9map, b, a, tmap, k, psi);
1561 /*printf("\t\tend hmm_cm->t[%d][%d] : %.9f\n", k, hmm_trans_idx, hmm->t[k][hmm_trans_idx]);*/
1562 }
1563
1564 /**************************************************************************
1565 * EPN 02.24.06
1566 * cm_sum_subpaths_cp9()
1567 *
1568 * Purpose: Calculate probability of getting from one state (start) to
1569 * another (end) in a CM, taking special considerations.
1570 *
1571 * Sum the probability of all subpaths that start
1572 * at "start" and end at "end" (ignoring "end"->"end" and
1573 * "start" -> "start" transitions if they exist)
1574 *
1575 * This function is used to help determine CM plan 9 HMM
1576 * transition probabilities. If we're trying to set a particular
1577 * transition (1 of 9) out of HMM node k, we ignore the contribution
1578 * of subparses that correspond to other transitions out of node k.
1579 * For example, we don't want to include the probability of an
1580 * insert(node a) ->match (node a+1) sub parse when calculating
1581 * the transition probability for match(node a) -> match (node a+1),
1582 * (CTMM) because CTIM maps to that transition.
1583 *
1584 * Args:
1585 * CM_t *cm - the CM
1586 * CP9Map_t *cp9map - the map from the CM to HMM and vice versa
1587 * int start - state index we're starting at
1588 * int end - state index we're ending in
1589 * int ***tmap - the hard-coded transition map
1590 * int k - HMM node we're calc'ing transition (out of node k) for
1591 * double *psi - psi[v] is the expected number of times state v is entered
1592 * in a CM parse
1593 * Returns: Float, the summed probability of all subpaths through the CM
1594 * starting at "start" and ending at "end".
1595 */
1596 static float
1597 cm_sum_subpaths_cp9(CM_t *cm, CP9Map_t *cp9map, int start, int end, char ***tmap,
1598 int k, double *psi)
1599 {
1600 int status;
1601 int v; /* state index in CM */
1602
1603 double *sub_psi; /*sub_psi[v] is the expected number of times state v is
1604 * entered given we started at state "start", this is
1605 * the summed probability of all paths starting at "start"
1606 * and ending at v.
1607 */
1608 float to_return;
1609 int y;
1610 int x;
1611 char tmap_val;
1612 int is_insert; /* 1 if v is insert, 0 if not */
1613 float insert_to_start; /* is start is not an insert and the insert state of
1614 * HMM node k < start, this is the
1615 * contribution of insert -> start (which should be ignored
1616 * because the TMI and TDI transitions solely map to
1617 * this transition probability.
1618 */
1619
1620 /*printf("\t\t\tin cm_sum_subpaths_cp9, start: %d | end: %d\n", start, end);*/
1621 if(start > end)
1622 {
1623 printf("ERROR in cm_sum_subpaths_cp9: start: %d > end: %d\n", start, end);
1624 exit(1);
1625 }
1626 if(start == end)
1627 {
1628 if(cm->sttype[start] != IL_st && cm->sttype[start] != IR_st)
1629 {
1630 /* This is possible, though unlikely. It only happens (I think and hope) if
1631 * we've got two adjacent (k and k+1) columns modelled by the same MATP node n,
1632 * so when setting a transition from node k -> node k+1, we enter this function
1633 * three times with start == end. Once with each MATP_MP, MATP_ML, MATP_MR, and MATP_D
1634 * because these states map to the beginning and ending states of the M_k->M_k+1,
1635 * M_k->D_k+1, D_k->M_k+1, and D_k->D_k+1 transitions respectively.
1636 * The solution is to return 1.0,
1637 * so the contribution is simply psi[MATP_M*] (in the function
1638 * hmm_add_single_trans_cp9 that called this function).
1639 */
1640 if((cm->stid[start] != MATP_MP && cm->stid[start] != MATP_D) &&
1641 (cm->stid[start] != MATP_ML && cm->stid[start] != MATP_MR))
1642 {
1643 printf("ERROR asking for self transition of non-insert, non-MATP state: %d\n", start);
1644 exit(1);
1645 }
1646 return 1.0;
1647 }
1648 /* else we just return the self-insert probability */
1649 /*printf("\t\t\tReturning self insert prob: %f\n", cm->t[start][0]);*/
1650 return cm->t[start][0];
1651 }
1652 to_return = 0.;
1653
1654 ESL_ALLOC(sub_psi, sizeof(double) * (end - start + 1));
1655 /* Initialize sub_psi[0]. Need to check if we need to ignore the probability
1656 * mass from the CM insert state(s) that maps to the HMM insert state of this node
1657 * (these insert states are cp9map->hns2cs[k][1][0] and (potentially) cp9map->hns2cs[k][1][1])
1658 * that goes through "start" (which must map to either the M or
1659 * D state of this HMM node).
1660 */
1661 sub_psi[0] = 1.; /* have to start in "start" */
1662
1663 if((cm->sttype[start] != IL_st && cm->sttype[start] != IR_st) &&
1664 (cm->sttype[end] != IL_st && cm->sttype[end] != IR_st))
1665 {
1666 insert_to_start = 0.;
1667 if(cp9map->hns2cs[k][1][0] < start)
1668 insert_to_start = psi[cp9map->hns2cs[k][1][0]] * cm_sum_subpaths_cp9(cm, cp9map, cp9map->hns2cs[k][1][0], start, tmap, k, psi);
1669 if((cp9map->hns2cs[k][1][1] != -1) && (cp9map->hns2cs[k][1][1] < start))
1670 insert_to_start += psi[cp9map->hns2cs[k][1][1]] * cm_sum_subpaths_cp9(cm, cp9map, cp9map->hns2cs[k][1][1], start, tmap, k, psi);
1671 sub_psi[0] -= insert_to_start / psi[start];
1672 /*printf("\t\tinsert_to_start: %f sub_psi[0]: %f\n", insert_to_start, sub_psi[0]);*/
1673 }
1674 /* note: when cm_sum_subpaths_cp9 is called recursively above
1675 * it will never result in another recursive call,
1676 * because its "start" is an insert state.
1677 */
1678
1679 for (v = (start+1); v <= end; v++)
1680 {
1681 /*printf("\t\t\tv: %d\n", v);*/
1682 sub_psi[v-start] = 0.;
1683 if(cm->sttype[v] == IL_st || cm->sttype[v] == IR_st)
1684 is_insert = 1;
1685 else
1686 is_insert = 0;
1687
1688 if(cm->sttype[v] == S_st)
1689 {
1690 /* previous state is necessarily either a BIF_B or a END_E, either
1691 * way we handle as if the transition FROM previous state to this
1692 * state is 1.0 */
1693 sub_psi[v-start] = sub_psi[(v-1)-start] * 1.;
1694 }
1695 /* check if v is an insert state that maps to node k, if so we don't want
1696 * to double count its contribution (it will be counted in a subsequent cm_sum_subpaths_CP9()
1697 * call), so we skip it here.
1698 */
1699 if((v != end && is_insert) && ((cp9map->cs2hn[v][0] == k) || cp9map->cs2hn[v][1] == k))
1700 {
1701 /*skip the contribution*/
1702 /*
1703 printf("v: %d | skipping the contribution\n", v);
1704 printf("\tcs2hn_map[%d][0] : %d\n", v, cs2hn_map[v][0]);
1705 printf("\tcs2hn_map[%d][1] : %d\n", v, cs2hn_map[v][1]);
1706 printf("\tcs2hn_map[start][0] : %d\n", v, cs2hn_map[start][0]);
1707 printf("\tcs2hn_map[start][1] : %d\n", v, cs2hn_map[start][1]);
1708 */
1709 }
1710 else
1711 {
1712 for (y = cm->pnum[v]-1; y >= is_insert; y--)
1713 {
1714 x = cm->plast[v] - y;
1715 /* x is a parent of v, we're adding contribution
1716 * of transition from x to v. */
1717 tmap_val = tmap[(int) cm->stid[x]][(int) cm->ndtype[cm->ndidx[v]+is_insert]][(int) cm->stid[v]];
1718 #if eslDEBUGLEVEL >= 1
1719 if(tmap_val == -1)
1720 {
1721 printf("#DEBUG: tmap ERROR 1\n");
1722 printf("#DEBUG: v: %d | pnum[v]: %d | plast[v]: %d | y: %d | x: %d | d1: %d | d2: %d | d3: %d\n", v, cm->pnum[v], cm->plast[v], y, x, ((int) cm->stid[x]), ((int) (cm->ndtype[cm->ndidx[v]+is_insert])), ((int) cm->stid[v]));
1723 exit(1);
1724 }
1725 #endif
1726 if((x - start) < 0)
1727 sub_psi[v-start] += 0.;
1728 else
1729 sub_psi[v-start] += sub_psi[x-start] * cm->t[x][(int) tmap_val];
1730 }
1731 if(v != end && is_insert) /* we don't want to include the probability of an
1732 insert self-loop to end itself */
1733 {
1734 sub_psi[v-start] += sub_psi[v-start] * (cm->t[v][0] / (1-cm->t[v][0]));
1735 /*the contribution of the self insertion loops*/
1736 }
1737 }
1738 }
1739 to_return = (float) sub_psi[end-start];
1740 free(sub_psi);
1741 return to_return;
1742
1743 ERROR:
1744 cm_Fail("Memory allocation error.");
1745 return 0.; /* never reached */
1746 }
1747
1748 /**************************************************************************
1749 * EPN 03.19.06
1750 * check_psi_vs_phi_cp9()
1751 *
1752 * Purpose: Check that psi and phi values for CM states that map to HMM states
1753 * are within a certain threshold. Assumes all HMM insert state maps
1754 * to exactly 1 CM insert state - i.e. ambiguities have been removed.
1755 * (As of version 0.71 ambiguities are always removed).
1756 * Args:
1757 * CM_t *cm - the CM
1758 * errbuf - for error messages.
1759 * CP9Map_t *cp9map - the map from the CM to HMM and vice versa
1760 * double *psi - psi[v] is expected number of times CM state v is entered
1761 * double **phi - phi array, phi[k][v] is expected number of times
1762 * HMM state v (0 = match, 1 insert, 2 = delete) in
1763 * node k is visited.
1764 * double threshold - the threshold that mapping (potentially summed) psi and
1765 * phi values are allowed to be different by, without throwing an error.
1766 * int debug_level - level for debugging print statements
1767 *
1768 * Returns: eslOK: if CM and HMM are "close enough" (see code)
1769 * !eslOK: otherwise, errbuf is filled
1770 */
1771 static int
1772 check_psi_vs_phi_cp9(CM_t *cm, char *errbuf,CP9Map_t *cp9map, double *psi, double **phi, double threshold,
1773 int debug_level)
1774 {
1775 int status;
1776 int v; /* CM state index*/
1777 int k;
1778 int y;
1779 double summed_psi;
1780 int *ap; /*CM state that maps to HMM state in node k*/
1781 int k_state; /*0, 1 or 2, state in hmm node k*/
1782 int violation;
1783 int v_ct;
1784 double diff;
1785 int ret_val; /* return value */
1786 int adj_bp_flag; /* a special case in which we always return TRUE after printing a warning */
1787
1788 if (cm->flags & CMH_LOCAL_BEGIN) ESL_FAIL(eslEINCOMPAT, errbuf, "internal error: we're in CM local mode while trying to build a CP9 HMM");
1789
1790 adj_bp_flag = FALSE;
1791 if(check_cm_adj_bp(cm, cp9map))
1792 {
1793 adj_bp_flag = TRUE;
1794 /* if check_cm_adj_bp() returns TRUE, then the following rare (but possible) situation
1795 * is true: Two adjacent consensus columns are modelled by the same MATP node. In this
1796 * case it is difficult for the CM plan 9 architecture to mirror the insert state
1797 * that maps to both the MATP_IL and MATP_IR of this node. It is more difficult than
1798 * for any other possible CM topology situation, so we relax the threshold when
1799 * checking psi and phi.
1800 */
1801 }
1802 ret_val = TRUE;
1803 v_ct = 0;
1804 ESL_ALLOC(ap, sizeof(int) * 2);
1805
1806 for(v = 0; v < cm->M; v++)
1807 if(cm->stid[v] != BIF_B)
1808 {
1809 for(y = 0; y < cm->cnum[v]; y++)
1810 if(debug_level > 1) printf("cm->t[%d][%d]: %f\n", v, y, cm->t[v][y]);
1811 if(debug_level > 1) printf("\n");
1812 }
1813
1814 for (k = 0; k <= cp9map->hmm_M; k++)
1815 {
1816 k_state = HMMMATCH;
1817 ap[0] = cp9map->hns2cs[k][k_state][0];
1818 ap[1] = cp9map->hns2cs[k][k_state][1];
1819 summed_psi = psi[ap[0]];
1820 if(ap[1] != -1)
1821 summed_psi += psi[ap[1]];
1822 violation = FALSE;
1823 diff = phi[k][0] - summed_psi;
1824 if((diff > threshold) || ((-1. * diff) > threshold))
1825 {
1826 violation = TRUE;
1827 v_ct++;
1828 }
1829 if(violation)
1830 printf("M k: %4d | phi: %f | psi: %f VIOLATION (%f) (cm v1: %d cm v2: %d)\n", k, phi[k][0], summed_psi, diff, ap[0], ap[1]);
1831 else if(debug_level > 1)
1832 printf("M k: %4d | phi: %f | psi: %f\n", k, phi[k][0], summed_psi);
1833
1834 k_state = HMMINSERT;
1835 ap[0] = cp9map->hns2cs[k][k_state][0];
1836 ap[1] = cp9map->hns2cs[k][k_state][1];
1837 if(ap[1] != -1)
1838 cm_Fail("ERROR, HMM insert state of node %d maps to 2 CM insert states: %d and %d\n", k, ap[0], ap[1]);
1839 summed_psi = psi[ap[0]];
1840 violation = FALSE;
1841 diff = phi[k][1] - summed_psi;
1842 if((diff > threshold) || ((-1. * diff) > threshold))
1843 {
1844 violation = TRUE;
1845 v_ct++;
1846 }
1847 if(violation)
1848 printf("I k: %4d | phi: %f | psi: %f VIOLATION (%f) (cm v1: %d cm v2: %d)\n", k, phi[k][1], summed_psi, diff, ap[0], ap[1]);
1849 else if(debug_level > 1)
1850 printf("I k: %4d | phi: %f | psi: %f\n", k, phi[k][1], summed_psi);
1851
1852 k_state = HMMDELETE;
1853 ap[0] = cp9map->hns2cs[k][k_state][0];
1854 ap[1] = cp9map->hns2cs[k][k_state][1];
1855 if(k == 0)
1856 summed_psi = 0.; /*no such state in HMM or CM*/
1857 else
1858 {
1859 summed_psi = psi[ap[0]];
1860 if(ap[1] != -1)
1861 summed_psi += psi[ap[1]];
1862 }
1863 violation = FALSE;
1864 diff = phi[k][2] - summed_psi;
1865 if((diff > threshold) || ((-1. * diff) > threshold))
1866 {
1867 violation = TRUE;
1868 v_ct++;
1869 }
1870 if(violation)
1871 printf("D k: %4d | phi: %f | psi: %f VIOLATION (%f) (cm v1: %d cm v2: %d)\n", k, phi[k][2], summed_psi, diff, ap[0], ap[1]);
1872 else if(debug_level > 1)
1873 {
1874 printf("D k: %4d | phi: %f | psi: %f\n\n", k, phi[k][2], summed_psi);
1875 }
1876 }
1877 free(ap);
1878
1879 if(v_ct > 0)
1880 {
1881 printf("ERROR, %d HMM states violate the %f threshold b/t psi and phi.\n", v_ct, threshold);
1882 ret_val = FALSE;
1883 }
1884 if(adj_bp_flag == TRUE) return eslOK; /* always return TRUE for models with bps modelling adjacent columns (should be rare) */
1885
1886 if(ret_val == FALSE) ESL_FAIL(eslFAIL, errbuf, "cp9 hmm fails psi/phi test");
1887
1888 return eslOK;
1889
1890 ERROR:
1891 cm_Fail("Memory allocation error.");
1892 return 0.; /* never reached */
1893 }
1894
1895 /**************************************************************************
1896 * EPN 03.13.06
1897 * debug_print_cp9_params()
1898 *
1899 * Purpose: Print out emission and transition probabilities and scores
1900 * for a CM plan 9 HMM.
1901 *
1902 * Args:
1903 * fp - often stdout
1904 * cplan9_s *hmm - counts form CM plan 9 HMm
1905 * print_scores - TRUE to print scores, FALSE not to
1906 * Returns: (void)
1907 */
1908 void
1909 debug_print_cp9_params(FILE *fp, CP9_t *hmm, int print_scores)
1910 {
1911 int k, i;
1912
1913 /*fprintf(fp, "Printing CP9 HMM parameters in debug_print_cp9_params:\n\n");*/
1914
1915 fprintf(fp, "Consensus Length: %d\n", hmm->M);
1916 fprintf(fp, "Node: 0\n");
1917 for(i = 0; i < hmm->abc->K; i++)
1918 {
1919 if(print_scores) fprintf(fp, "ins[%3d][%3d] = %.3f | %10d\n", 0, i, hmm->ins[0][i], hmm->isc[i][0]);
1920 else fprintf(fp, "ins[%3d][%3d] = %.3f\n", 0, i, hmm->ins[0][i]);
1921 }
1922 fprintf(fp, "\n");
1923
1924 k=0;
1925 if(print_scores) {
1926 fprintf(fp, "\tCTMM[%d] = %f | %d\n", k, hmm->t[0][CTMM], hmm->tsc[CTMM][0]);
1927 fprintf(fp, "\tCTMI[%d] = %f | %d\n", k, hmm->t[0][CTMI], hmm->tsc[CTMI][0]);
1928 fprintf(fp, "\tCTMD[%d] = %f | %d\n", k, hmm->t[0][CTMD], hmm->tsc[CTMD][0]);
1929 fprintf(fp, "\tCTMEL[%d] = %f | %d\n", k, hmm->t[0][CTMEL], hmm->tsc[CTMEL][0]);
1930 fprintf(fp, "\tCTIM[%d] = %f | %d\n", k, hmm->t[0][CTIM], hmm->tsc[CTIM][0]);
1931 fprintf(fp, "\tCTII[%d] = %f | %d\n", k, hmm->t[0][CTII], hmm->tsc[CTII][0]);
1932 fprintf(fp, "\tCTID[%d] = %f | %d\n", k, hmm->t[0][CTID], hmm->tsc[CTID][0]);
1933 fprintf(fp, "\tCTDM[%d] = %f | %d\n", k, hmm->t[0][CTDM], hmm->tsc[CTDM][0]);
1934 fprintf(fp, "\tCTDI[%d] = %f | %d\n", k, hmm->t[0][CTDI], hmm->tsc[CTDI][0]);
1935 fprintf(fp, "\tCTDD[%d] = %f | %d\n", k, hmm->t[0][CTDD], hmm->tsc[CTDD][0]);
1936 }
1937 else {
1938 fprintf(fp, "\tCTMM[%d] = %f\n", k, hmm->t[0][CTMM]);
1939 fprintf(fp, "\tCTMI[%d] = %f\n", k, hmm->t[0][CTMI]);
1940 fprintf(fp, "\tCTMD[%d] = %f\n", k, hmm->t[0][CTMD]);
1941 fprintf(fp, "\tCTMEL[%d] = %f\n", k, hmm->t[0][CTMEL]);
1942 fprintf(fp, "\tCTIM[%d] = %f\n", k, hmm->t[0][CTIM]);
1943 fprintf(fp, "\tCTII[%d] = %f\n", k, hmm->t[0][CTII]);
1944 fprintf(fp, "\tCTID[%d] = %f\n", k, hmm->t[0][CTID]);
1945 fprintf(fp, "\tCTDM[%d] = %f\n", k, hmm->t[0][CTDM]);
1946 fprintf(fp, "\tCTDI[%d] = %f\n", k, hmm->t[0][CTDI]);
1947 fprintf(fp, "\tCTDD[%d] = %f\n", k, hmm->t[0][CTDD]);
1948 }
1949 for(k = 1; k <= hmm->M; k++)
1950 {
1951 fprintf(fp, "Node: %d\n", k);
1952 for(i = 0; i < hmm->abc->Kp; i++)
1953 {
1954 if(print_scores)
1955 fprintf(fp, "mat[%3d][%3d] = %.3f | %10d\n", k, i, hmm->mat[k][i], hmm->msc[i][k]);
1956 else
1957 fprintf(fp, "mat[%3d][%3d] = %.3f\n", k, i, hmm->mat[k][i]);
1958 }
1959 for(i = 0; i < hmm->abc->Kp; i++)
1960 {
1961 if(print_scores)
1962 fprintf(fp, "ins[%3d][%3d] = %.3f | %10d\n", k, i, hmm->ins[k][i], hmm->isc[i][k]);
1963 else
1964 fprintf(fp, "ins[%3d][%3d] = %.3f\n", k, i, hmm->ins[k][i]);
1965 }
1966 fprintf(fp, "\n");
1967 if(print_scores) {
1968 fprintf(fp, "\tCTMM[%d] = %f | %10d\n", k, hmm->t[k][CTMM], hmm->tsc[CTMM][k]);
1969 fprintf(fp, "\tCTMI[%d] = %f | %10d\n", k, hmm->t[k][CTMI], hmm->tsc[CTMI][k]);
1970 fprintf(fp, "\tCTMD[%d] = %f | %10d\n", k, hmm->t[k][CTMD], hmm->tsc[CTMD][k]);
1971 fprintf(fp, "\tCTMEL[%d] = %f | %10d\n", k, hmm->t[k][CTMEL], hmm->tsc[CTMEL][k]);
1972 fprintf(fp, "\tCTIM[%d] = %f | %10d\n", k, hmm->t[k][CTIM], hmm->tsc[CTIM][k]);
1973 fprintf(fp, "\tCTII[%d] = %f | %10d\n", k, hmm->t[k][CTII], hmm->tsc[CTII][k]);
1974 fprintf(fp, "\tCTID[%d] = %f | %10d\n", k, hmm->t[k][CTID], hmm->tsc[CTID][k]);
1975 fprintf(fp, "\tCTDM[%d] = %f | %10d\n", k, hmm->t[k][CTDM], hmm->tsc[CTDM][k]);
1976 fprintf(fp, "\tCTDI[%d] = %f | %10d\n", k, hmm->t[k][CTDI], hmm->tsc[CTDI][k]);
1977 fprintf(fp, "\tCTDD[%d] = %f | %10d\n", k, hmm->t[k][CTDD], hmm->tsc[CTDD][k]);
1978 fprintf(fp, "\t beg[%d] = %f | %10d\n", k, hmm->begin[k], hmm->bsc[k]);
1979 fprintf(fp, "\t end[%d] = %f | %10d\n", k, hmm->end[k], hmm->esc[k]);
1980 }
1981 else {
1982 fprintf(fp, "\tCTMM[%d] = %f\n", k, hmm->t[k][CTMM]);
1983 fprintf(fp, "\tCTMI[%d] = %f\n", k, hmm->t[k][CTMI]);
1984 fprintf(fp, "\tCTMD[%d] = %f\n", k, hmm->t[k][CTMD]);
1985 fprintf(fp, "\tCTMEL[%d] = %f\n", k, hmm->t[k][CTMEL]);
1986 fprintf(fp, "\tCTIM[%d] = %f\n", k, hmm->t[k][CTIM]);
1987 fprintf(fp, "\tCTII[%d] = %f\n", k, hmm->t[k][CTII]);
1988 fprintf(fp, "\tCTID[%d] = %f\n", k, hmm->t[k][CTID]);
1989 fprintf(fp, "\tCTDM[%d] = %f\n", k, hmm->t[k][CTDM]);
1990 fprintf(fp, "\tCTDI[%d] = %f\n", k, hmm->t[k][CTDI]);
1991 fprintf(fp, "\tCTDD[%d] = %f\n", k, hmm->t[k][CTDD]);
1992 fprintf(fp, "\t beg[%d] = %f\n", k, hmm->begin[k]);
1993 fprintf(fp, "\t end[%d] = %f\n", k, hmm->end[k]);
1994 }
1995 fprintf(fp, "\n");
1996 }
1997 }
1998
1999 /**************************************************************************
2000 * EPN 09.01.06
2001 * Function: CP9_check_by_sampling()
2002 *
2003 * Purpose: Given a CM and a CM plan 9 hmm that is supposed to mirror
2004 * the CM as closely as possible (Weinberg-Ruzzo style, with
2005 * differences due to differences in the CM Plan 9 architecture
2006 * and the architecture that they use), check if the CM and CP9
2007 * actually do correspond as closely as possible. Do this by
2008 * generating alignments from the CM, determining CP9 HMM
2009 * tracebacks implicit in the alignments and amassing counts
2010 * from the tracebacks to use to build a new CP9 HMM
2011 * without using pseudocounts (the infinite MSA idea introduced
2012 * by Zasha Weinberg in the ML-HMM Bioinformatics paper). Finally
2013 * compare the parameters of that CP9 HMM to the one we've
2014 * built.
2015 *
2016 * The option is given to truncate the alignments generated
2017 * from the CM before and after specified consensus columns
2018 * prior to building the ML HMM. This option was added to
2019 * allow testing of the subCM construction procedure.
2020 * To NOT truncate, simply pass 1 and hmm->M as the first
2021 * and last consensus columns to use.
2022 *
2023 * NOTE: code to sample an alignment from the CM taken from
2024 * cmemit.c (which was ported from HMMER's hmmemit.c).
2025 *
2026 * Returns: eslOK if CM and HMM are "close enough" (see code)
2027 * !eslOK otherwise, errbuf is filled.
2028 */
2029 int
2030 CP9_check_by_sampling(CM_t *cm, CP9_t *hmm, char *errbuf, ESL_RANDOMNESS *r, CMSubInfo_t *subinfo,
2031 int spos, int epos, float chi_thresh, int nsamples, int print_flag)
2032 {
2033 int status;
2034 Parsetree_t **tr; /* Parsetrees of emitted aligned sequences */
2035 ESL_SQ **sq; /* sequences */
2036 ESL_MSA *msa; /* alignment */
2037 float *wgt;
2038 char *name; /* name for emitted seqs */
2039 int i, nd;
2040 int L;
2041 int apos;
2042 int *matassign;
2043 int *useme;
2044 CP9trace_t **cp9_tr; /* fake tracebacks for each seq */
2045 CP9_t *shmm; /* the new, CM plan9 HMM; built by sampling*/
2046 int msa_nseq; /* this is the number of sequences per MSA,
2047 * current strategy is to sample (nseq/nseq_per_msa)
2048 * alignments from the CM, and add counts from
2049 * each to the shmm in counts form (to limit memory)
2050 */
2051 int nsampled; /* number of sequences sampled thus far */
2052 int cc;
2053 int v_ct; /* number of nodes that violate our threshold */
2054 int spredict_total_ct; /* total number of nodes we thought would be violations */
2055 int swrong_total_ct; /* total number of nodes we thought would be violations but were not */
2056 int namelen; /* max int size for name */
2057 char *tmp_name; /* name for the seqs */
2058
2059 spredict_total_ct = 0;
2060 swrong_total_ct = 0;
2061 v_ct = 0;
2062
2063 msa_nseq = 1000;
2064
2065 /* Allocate and zero the new HMM we're going to build by sampling from
2066 * the CM.
2067 */
2068 shmm = AllocCPlan9(hmm->M, hmm->abc);
2069 ZeroCPlan9(shmm);
2070 CPlan9SetNullModel(shmm, hmm->null, 1.0); /* set p1 = 1.0 which corresponds to the CM */
2071
2072 CPlan9Renormalize(hmm);
2073 CMRenormalize(cm);
2074
2075 /* sample MSA(s) from the CM */
2076 nsampled = 0;
2077 ESL_ALLOC(sq, sizeof(ESL_SQ *) * msa_nseq);
2078 ESL_ALLOC(tr, (sizeof(Parsetree_t *) * msa_nseq));
2079 ESL_ALLOC(wgt,(sizeof(float) * msa_nseq));
2080 esl_vec_FSet(wgt, msa_nseq, 1.0);
2081 namelen = 3 + IntMaxDigits() + 1; /* IntMaxDigits() returns number of digits in INT_MAX */
2082 ESL_ALLOC(tmp_name, sizeof(char) * namelen);
2083
2084 while(nsampled < nsamples)
2085 {
2086 if(nsampled != 0) {
2087 /* clean up from previous MSA */
2088 free(matassign);
2089 free(useme);
2090 for (i = 0; i < msa_nseq; i++) {
2091 CP9FreeTrace(cp9_tr[i]);
2092 FreeParsetree(tr[i]);
2093 esl_sq_Destroy(sq[i]);
2094 }
2095 free(cp9_tr);
2096 esl_msa_Destroy(msa);
2097 }
2098 /* Emit msa_nseq parsetrees from the CM */
2099 if(nsampled + msa_nseq > nsamples)
2100 msa_nseq = nsamples - nsampled;
2101 for (i = 0; i < msa_nseq; i++) {
2102 ESL_ALLOC(name, sizeof(char) * namelen);
2103 snprintf(name, namelen, "seq%d", i+1);
2104 if((status = EmitParsetree(cm, errbuf, r, name, TRUE, &(tr[i]), &(sq[i]), &L)) != eslOK) return status;
2105 free(name);
2106 }
2107 /* Build a new MSA from these parsetrees */
2108 Parsetrees2Alignment(cm, errbuf, cm->abc, sq, NULL, tr, NULL, msa_nseq, NULL, NULL,
2109 /*do_full=*/TRUE,
2110 /*do_matchonly=*/FALSE,
2111 /*allow_trunc=*/FALSE,
2112 &msa);
2113 /* MSA should be in text mode, not digitized */
2114 if(msa->flags & eslMSA_DIGITAL) ESL_FAIL(eslEINCOMPAT, errbuf, "CP9_check_by_sampling(): sampled MSA should NOT be digitized.\n");
2115
2116 /* Truncate the alignment prior to consensus column spos and after
2117 consensus column epos */
2118 ESL_ALLOC(useme, sizeof(int) * (msa->alen+1));
2119 for (apos = 0, cc = 0; apos < msa->alen; apos++) {
2120 /* Careful here, placement of cc++ increment is impt,
2121 * we want all inserts between cc=spos-1 and cc=spos,
2122 * and between cc=epos and cc=epos+1.
2123 */
2124 useme[apos] = (cc < (spos-1) || cc > epos) ? 0 : 1;
2125 if (!esl_abc_CIsGap(cm->abc, msa->rf[apos])) {
2126 cc++;
2127 if(cc == (epos+1)) useme[apos] = 0;
2128 /* we misassigned this guy, overwrite */
2129 }
2130 }
2131 if((status = esl_msa_ColumnSubset(msa, errbuf, useme)) != eslOK) return status;
2132
2133 /* Determine match assignment from RF annotation
2134 */
2135 ESL_ALLOC(matassign, sizeof(int) * (msa->alen+1));
2136 matassign[0] = 0;
2137 for (apos = 0; apos < msa->alen; apos++) {
2138 matassign[apos+1] = 0;
2139 if (!esl_abc_CIsGap(cm->abc, msa->rf[apos]))
2140 matassign[apos+1] = 1;
2141 }
2142 /* make fake tracebacks for each seq */
2143 if((status = esl_msa_Digitize(cm->abc, msa, NULL)) == eslEINVAL) ESL_FAIL(status, errbuf, "CP9_check_by_sampling(): esl_msa_Digitize() returned eslEINVAL, some characters must be invalid in msa.");
2144 CP9_fake_tracebacks(msa, matassign, &cp9_tr);
2145
2146 /* build model from tracebacks (code from HMMER's modelmakers.c::matassign2hmm() */
2147 for (i = 0; i < msa->nseq; i++)
2148 CP9TraceCount(shmm, sq[i]->dsq, wgt[i], cp9_tr[i]);
2149
2150 nsampled += msa_nseq;
2151 }
2152
2153 /* clean up from previous MSA */
2154 free(matassign);
2155 free(useme);
2156 free(tmp_name);
2157 for (i = 0; i < msa_nseq; i++) {
2158 CP9FreeTrace(cp9_tr[i]);
2159 FreeParsetree(tr[i]);
2160 esl_sq_Destroy(sq[i]);
2161 }
2162 esl_msa_Destroy(msa);
2163 free(cp9_tr);
2164 free(tr);
2165 free(sq);
2166 free(wgt);
2167
2168 /* The new shmm is in counts form, filled with observations from MSAs sampled
2169 * from the CM.
2170 * We want to do a series of chi-squared tests to determine the probability that
2171 * the observed samples from the CM were not taken from the corresponding CM Plan 9
2172 * HMM probability distributions (the CM Plan 9 is supposed to exactly mirror the
2173 * CM in this way).
2174 */
2175 if(print_flag)
2176 {
2177 printf("PRINTING BUILT HMM PARAMS:\n");
2178 debug_print_cp9_params(stdout, hmm, TRUE);
2179 printf("DONE PRINTING BUILT HMM PARAMS:\n");
2180
2181 printf("PRINTING SAMPLED HMM PARAMS:\n");
2182 debug_print_cp9_params(stdout, shmm, TRUE);
2183 printf("DONE PRINTING SAMPLED HMM PARAMS:\n");
2184 }
2185 for(nd = 0; nd <= shmm->M; nd++) {
2186 if(print_flag) printf("nd:%d\n", nd);
2187 if(!(CP9_node_chi_squared(hmm, shmm, nd, chi_thresh, print_flag))) {
2188 if(subinfo == NULL) {
2189 v_ct++;
2190 printf("SAMPLING VIOLATION[%3d]: TRUE | spos: %3d | epos: %3d\n", nd, spos, epos);
2191 }
2192 else if(subinfo != NULL && subinfo->imp_cc[nd] == 0) {
2193 v_ct++;
2194 printf("SAMPLING VIOLATION[%3d]: TRUE | spos: %3d | epos: %3d | subinfo->imp_cc: %d\n", nd, spos, epos, subinfo->imp_cc[nd]);
2195 }
2196 else if(subinfo != NULL && subinfo->imp_cc[nd] != 0) {
2197 spredict_total_ct++;
2198 subinfo->spredict_ct[subinfo->imp_cc[nd]]++;
2199 if(print_flag) printf("PREDICTED SAMPLING VIOLATION[%3d]: TRUE | spos: %3d | epos: %3d | subinfo->imp_cc: %d (spredict_total_ct: %d)\n", nd, spos, epos, subinfo->imp_cc[nd], spredict_total_ct);
2200 }
2201 }
2202 else if(subinfo != NULL && subinfo->imp_cc[nd] != 0) {
2203 /* We predicted this node would fail, but it didn't */
2204 spredict_total_ct++;
2205 subinfo->spredict_ct[subinfo->imp_cc[nd]]++;
2206 swrong_total_ct++;
2207 subinfo->swrong_ct[subinfo->imp_cc[nd]]++;
2208 if(print_flag) printf("NON-VIOLATION[%3d] %3d : spos: %3d | epos: %3d | non-subinfo->imp_cc: %d\n", nd, swrong_total_ct, spos, epos, subinfo->imp_cc[nd]);
2209 }
2210 }
2211
2212 /*Next, renormalize shmm and logoddisfy it */
2213 CPlan9Renormalize(shmm);
2214 CP9Logoddsify(shmm);
2215
2216 if(print_flag)
2217 {
2218 printf("PRINTING BUILT HMM PARAMS:\n");
2219 debug_print_cp9_params(stdout, hmm, TRUE);
2220 printf("DONE PRINTING BUILT HMM PARAMS:\n");
2221
2222
2223 printf("PRINTING SAMPLED HMM PARAMS:\n");
2224 debug_print_cp9_params(stdout, shmm, TRUE);
2225 printf("DONE PRINTING SAMPLED HMM PARAMS:\n");
2226
2227 /* Output the alignment */
2228 /*WriteStockholm(stdout, msa);*/
2229 }
2230 FreeCPlan9(shmm);
2231
2232 if(v_ct > 0) ESL_FAIL(eslFAIL, errbuf, "CP9_check_by_sampling(): check failed");
2233 return eslOK;
2234
2235 ERROR:
2236 ESL_FAIL(status, errbuf, "memory allocation error");
2237 return FALSE; /* never reached */
2238 }
2239
2240 /* Function: CP9_node_chi_squared()
2241 *
2242 * Purpose : Given two CM Plan 9 HMMs, one in normalized form (ahmm), and one in
2243 * raw counts form (shmm), determine for a specific node nd,
2244 * the probability the samples implicit in the shmm were taken
2245 * from the corresponding probability distribution in ahmm using
2246 * the chi-squared test. Return FALSE IFF the probability that some
2247 * set of counts was not taken from the corresponding ahmm distribution
2248 * (probability null hypothesis is rejected) is above the probability
2249 * threshold thresh.
2250 *
2251 * Args:
2252 * cplan9_s *ahmm - an HMM with the probability distributions that define
2253 * the null hypothesis for the chi-squared tests (we
2254 * think the counts in shmm were taken from these distributions).
2255 * cplan9_s *shmm - an HMM in counts form
2256 * int nd - node of the HMM to check.
2257 * float threshold - probability threshold for rejecting
2258 * int print_flag - TRUE to print useful debugging info
2259 */
2260 int
2261 CP9_node_chi_squared(CP9_t *ahmm, CP9_t *shmm, int nd, float threshold, int print_flag)
2262 {
2263 int status;
2264 double p;
2265 int x;
2266 float m_nseq, i_nseq, d_nseq;
2267 float check_m_nseq, check_i_nseq;
2268 float *temp_ahmm_trans;
2269 float *temp_shmm_trans;
2270 int ret_val;
2271
2272 ret_val = TRUE;
2273
2274 if(nd > shmm->M || nd > ahmm->M)
2275 cm_Fail("ERROR CP9_node_chi_squared() is being grossly misused.\n");
2276
2277 CPlan9Renormalize(ahmm);
2278 /*
2279 EPN, Thu Feb 7 17:49:46 2008
2280 I think this CPlan9GlobalConfig() call is superfluous, in my tests it did nothing.
2281 I could be wrong in some cases though. I'm not too too worried about it though,
2282 because this function was useful primarily during development of the CP9 and sub CM
2283 construction code, which I now trust. And more importantly, this function (CP9_node_chi_squared)
2284 CANNOT be called from anything but cp9-test and sub_cm-test which are testsuite
2285 executables.
2286
2287 The reason I wanted to get rid of this CPlan9GlobalConfig() call is b/c I've changed
2288 how CP9's are locally configured, and the M_0->I_0, M_0->D_1, M_M->I_M and D_M->I_M transitions
2289 are all set to IMPOSSIBLE (to make a local CP9 more like a local CM), and thus it makes it
2290 hard to change a locally configured CP9 back to global mode b/c the initial values of those
2291 transitions is lost (the solution would be to add vectors to the cp9 data structure that
2292 remember these transition probs, but I don't have to do that if I NEVER need to globalize
2293 a locally configured CP9 (and this was the only function call of CPlan9GlobalConfig().
2294 CPlan9GlobalConfig() has been relocated to old_miscfuncs.c where it is not compiled
2295 or used.
2296
2297 start of old code block
2298 ~~~~~~~~~~~~~~~~~~~~~~~~~
2299 CPlan9GlobalConfig(ahmm);
2300 ~~~~~~~~~~~~~~~~~~~~~~~~~
2301 end of old code block
2302 */
2303
2304 CP9Logoddsify(ahmm);
2305
2306 /* First determine the sum of the counts for each state. This
2307 * is the number of samples that visited match, insert and
2308 * delete state of this node.
2309 */
2310
2311 if(nd == 0)
2312 m_nseq = shmm->begin[1]; /* this is begin -> M_1 transition */
2313 else if(nd == shmm->M)
2314 m_nseq = shmm->end[nd]; /* this is M_M -> end transition */
2315 else
2316 m_nseq = shmm->t[nd][CTMM];
2317 m_nseq += shmm->t[nd][CTMI];
2318 m_nseq += shmm->t[nd][CTMD];
2319
2320 i_nseq = shmm->t[nd][CTIM];
2321 i_nseq += shmm->t[nd][CTII];
2322 i_nseq += shmm->t[nd][CTID];
2323
2324 if(nd != 0) /* node 0 has no delete state */
2325 {
2326 d_nseq = shmm->t[nd][CTDM];
2327 d_nseq += shmm->t[nd][CTDI];
2328 d_nseq += shmm->t[nd][CTDD];
2329 }
2330
2331 if(nd != 0) {
2332 check_m_nseq = 0.;
2333 for (x = 0; x < ahmm->abc->K; x++) check_m_nseq += shmm->mat[nd][x];
2334 if(fabs(check_m_nseq - m_nseq) > 0.0001) cm_Fail("ERROR: node: %d has different number of sampled match emissions and transitions.\n");
2335 }
2336 check_i_nseq = 0.;
2337 for (x = 0; x < ahmm->abc->K; x++) check_i_nseq += shmm->ins[nd][x];
2338 if((check_i_nseq >= i_nseq && ((check_i_nseq - i_nseq) > 0.0001)) ||
2339 (check_i_nseq < i_nseq && ((i_nseq - check_i_nseq) > 0.0001)))
2340 cm_Fail("ERROR: node: %d has different number of sampled insert emissions and transitions.\n");
2341
2342 /* Perform chi-squared tests using code borrowed from SRE in
2343 * infernal/testsuite/bandcyk-montecarlo-test.c */
2344 /* Check match emissions */
2345 if(nd != 0) {
2346 esl_vec_FScale(ahmm->mat[nd], ahmm->abc->K, esl_vec_FSum(shmm->mat[nd], ahmm->abc->K)); /* convert to #'s */
2347 p = FChiSquareFit(ahmm->mat[nd], shmm->mat[nd], ahmm->abc->K); /* compare #'s */
2348 if (p < threshold) {
2349 printf("Rejected match emission distribution for CP9 node %d: chi-squared p = %f\n", nd, p);
2350 ret_val = FALSE;
2351 }
2352 }
2353 /* check insert emissions */
2354 esl_vec_FScale(ahmm->ins[nd], ahmm->abc->K, esl_vec_FSum(shmm->ins[nd], ahmm->abc->K)); /* convert to #'s */
2355 p = FChiSquareFit(ahmm->ins[nd], shmm->ins[nd], ahmm->abc->K); /* compare #'s */
2356 if (p < threshold) {
2357 printf("Rejected insert emission distribution for CP9 node %d: chi-squared p = %f\n", nd, p);
2358 ret_val = FALSE;
2359 }
2360
2361 /* check transitions */
2362 /* out of match, we're in global NW mode, so only non-zero begin is begin[1],
2363 * and only non-zero end is end[hmm->M] */
2364 ESL_ALLOC(temp_ahmm_trans, sizeof(float) * cp9_NTRANS);
2365 ESL_ALLOC(temp_shmm_trans, sizeof(float) * cp9_NTRANS);
2366 if(nd == 0 || nd == shmm->M) {
2367 if(nd == 0) { /* careful, begin[1] is really hmm->t[0][CTMM] */
2368 temp_ahmm_trans[0] = ahmm->begin[1];
2369 temp_shmm_trans[0] = shmm->begin[1];
2370 for(x = 1; x < cp9_TRANS_NMATCH; x++) {
2371 temp_ahmm_trans[x] = ahmm->t[0][x];
2372 temp_shmm_trans[x] = shmm->t[0][x];
2373 }
2374 }
2375 if(nd == shmm->M) { /* careful, end[hmm->M] is really hmm->t[hmm->M][CTMM] */
2376 temp_ahmm_trans[0] = ahmm->end[ahmm->M];
2377 temp_shmm_trans[0] = shmm->end[shmm->M];
2378 for(x = 1; x < cp9_TRANS_NMATCH; x++) {
2379 temp_ahmm_trans[x] = ahmm->t[ahmm->M][x];
2380 temp_shmm_trans[x] = shmm->t[shmm->M][x];
2381 }
2382 }
2383 esl_vec_FScale(temp_ahmm_trans, cp9_TRANS_NMATCH, esl_vec_FSum(temp_shmm_trans, cp9_TRANS_NMATCH)); /* convert to #'s */
2384 p = FChiSquareFit(temp_ahmm_trans, temp_shmm_trans, cp9_TRANS_NMATCH); /* compare #'s */
2385 if (p < threshold) {
2386 printf("Rejected match transition distribution for CP9 node %d: chi-squared p = %f\n", nd, p);
2387 ret_val = FALSE;
2388 }
2389 }
2390 else {
2391 esl_vec_FScale(ahmm->t[nd], cp9_TRANS_NMATCH, esl_vec_FSum(shmm->t[nd], cp9_TRANS_NMATCH)); /* convert to #'s */
2392 p = FChiSquareFit(ahmm->t[nd], shmm->t[nd], cp9_TRANS_NMATCH); /* compare #'s */
2393 if (p < threshold) {
2394 printf("Rejected match transition distribution for CP9 node %d: chi-squared p = %f\n", nd, p);
2395 ret_val = FALSE;
2396 }
2397 }
2398 /* out of insert */
2399 esl_vec_FScale (ahmm->t[nd] + cp9_TRANS_INSERT_OFFSET, cp9_TRANS_NINSERT, esl_vec_FSum(shmm->t[nd]+cp9_TRANS_INSERT_OFFSET, cp9_TRANS_NINSERT)); /* convert to #'s */
2400 p = FChiSquareFit(ahmm->t[nd] + cp9_TRANS_INSERT_OFFSET, shmm->t[nd] + cp9_TRANS_INSERT_OFFSET, cp9_TRANS_NINSERT); /* compare #'s */
2401 if (p < threshold) {
2402 printf("Rejected insert transition distribution for CP9 node %d: chi-squared p = %f\n", nd, p);
2403 ret_val = FALSE;
2404 }
2405
2406 /* out of delete */
2407 if(nd != 0) { /* D_0 does not exist */
2408 esl_vec_FScale (ahmm->t[nd] + cp9_TRANS_DELETE_OFFSET, cp9_TRANS_NDELETE, esl_vec_FSum(shmm->t[nd] + cp9_TRANS_DELETE_OFFSET, cp9_TRANS_NDELETE)); /* convert to #'s */
2409 p = FChiSquareFit(ahmm->t[nd] + cp9_TRANS_DELETE_OFFSET, shmm->t[nd] + cp9_TRANS_DELETE_OFFSET, cp9_TRANS_NDELETE); /* compare #'s */
2410 if (p < threshold) {
2411 printf("Rejected delete transition distribution for CP9 node %d: chi-squared p = %f\n", nd, p);
2412 ret_val = FALSE;
2413 }
2414 }
2415 else if(print_flag) printf("d_nseq: %.3f", d_nseq); /* this is only to silence compiler warnings that d_nseq is never used */
2416
2417 free(temp_ahmm_trans);
2418 free(temp_shmm_trans);
2419
2420 /* we've scaled some probabilities into counts, we want to get back into prob form */
2421 CPlan9Renormalize(ahmm);
2422
2423 return ret_val;
2424
2425 ERROR:
2426 cm_Fail("Memory allocation error.");
2427 return 0.; /* never reached */
2428 }
2429
2430 /**************************************************************************
2431 * EPN 09.24.06 [AA 599 DC->STL]
2432 * debug_print_phi_cp9()
2433 *
2434 * Purpose: Print out phi values for a given CP9 HMM.
2435 *
2436 * Args:
2437 * cplan9_s *hmm - the HMM
2438 * double **phi - phi array, phi[k][v] is expected number of times
2439
2440 * HMM state v (0 = match, 1 insert, 2 = delete) in
2441 * node k is visited.
2442 * Returns: (void)
2443 */
2444 void
2445 debug_print_phi_cp9(CP9_t *hmm, double **phi)
2446 {
2447 int k;
2448
2449 for(k = 0; k <= hmm->M; k++)
2450 {
2451 printf("phi[%4d][M]: %f\n", k, phi[k][0]);
2452 printf("phi[%4d][I]: %f\n", k, phi[k][1]);
2453 if(k != 0)
2454 printf("phi[%4d][D]: %f\n\n", k, phi[k][2]);
2455 else
2456 printf("\n");
2457 }
2458 return;
2459 }
2460
2461 /**************************************************************************
2462 * FChiSquareFit()
2463 * Stolen line for line from SRE's infernal/testsuite/bandcyk-montecarlo-test.c
2464 * and changed from double's to float's.
2465 */
2466
2467 static float
2468 FChiSquareFit(float *f1, float *f2, int N)
2469 {
2470 int i;
2471 float diff;
2472 float chisq = 0.0;
2473 int n;
2474 double qax;
2475
2476 n = 0;
2477 for (i = 0; i < N; i++)
2478 {
2479 if (f1[i] == 0. && f2[i] == 0.) continue;
2480 diff = f1[i] - f2[i];
2481 chisq += diff * diff / (f1[i]+f2[i]);
2482 n++;
2483 }
2484
2485 if (n > 1)
2486 {
2487 if(esl_stats_IncompleteGamma(((float) n-1.)/2., chisq/2., NULL, &qax) != eslOK)
2488 cm_Fail("ERROR in FChiSquareFit() call to esl_stats_IncompleteGamma()");
2489 return (float) qax;
2490 }
2491 else
2492 return -1.;
2493 }
2494
2495 /**************************************************************************
2496 * EPN 03.13.06
2497 * check_cm_adj_bp()
2498 *
2499 * Purpose: Check if two adjacent consensus columns (HMM nodes) are modelled by the
2500 * same MATP node.
2501 *
2502 * Args:
2503 * CM_t *cm - the CM
2504 * CP9Map_t - map from CM to HMM and vice versa
2505 * Returns: TRUE if two adjacent consensus columns are modelled by the same MATP_nd
2506 * FALSE if not
2507 */
2508 static int
2509 check_cm_adj_bp(CM_t *cm, CP9Map_t *cp9map)
2510 {
2511 int k, prev_k;
2512 prev_k = cp9map->pos2nd[1];
2513 for(k = 2; k <= cp9map->hmm_M; k++)
2514 {
2515 if(cp9map->pos2nd[k] == prev_k)
2516 return TRUE;
2517 prev_k = cp9map->pos2nd[k];
2518 }
2519 return FALSE;
2520 }
2521
2522 /* Function: MakeDealignedString()
2523 * Incept: EPN, Mon Aug 6 10:21:49 2007
2524 * stolen from Squid during Easelization, there's no equivalent
2525 * in Easel.
2526 *
2527 * Purpose: Given an aligned text string of some type (either sequence or
2528 * secondary structure, for instance), dealign it relative
2529 * to a given aseq. Return a ptr to the new string.
2530 *
2531 * Args: abc : the alphabet
2532 * aseq : template alignment
2533 * alen : length of aseq
2534 * ss: : string to make dealigned copy of; same length as aseq
2535 * ret_s : RETURN: dealigned copy of ss
2536 *
2537 * Return: 1 on success, 0 on failure (and squid_errno is set)
2538 * ret_s is alloc'ed here and must be freed by caller
2539 */
2540 int
2541 MakeDealignedString(const ESL_ALPHABET *abc, char *aseq, int alen, char *ss, char **ret_s)
2542 {
2543 int status;
2544 char *new;
2545 int apos, rpos;
2546
2547 ESL_ALLOC(new, (alen+1) * sizeof(char));
2548 for (apos = rpos = 0; apos < alen; apos++)
2549 if (! esl_abc_CIsGap(abc, aseq[apos]))
2550 {
2551 new[rpos] = ss[apos];
2552 rpos++;
2553 }
2554 new[rpos] = '\0';
2555 if (alen != strlen(ss))
2556 { cm_Fail("ERROR dealigning sequence."); }
2557 *ret_s = new;
2558 return eslOK;
2559
2560 ERROR:
2561 cm_Fail("Memory allocation error.");
2562 return 0; /* never reached */
2563 }
2564
2565
2566 /* Function: sub_build_cp9_hmm_from_mother()
2567 * Date: EPN, Wed Aug 20 16:05:26 2008
2568 *
2569 * Purpose: Given a CM, and it's mother (CM is a sub CM of it's mother)
2570 * build a CM Plan 9 HMM that mirrors the CM as closely
2571 * as possible. This HMM is a Weinberg/Ruzzo style ML HMM; i.e. if
2572 * we sampled an 'infinite MSA' from the CM and built a ML HMM from it
2573 * (using no pseudo-counts), it would be the same as the HMM we construct
2574 * here.
2575 *
2576 * Use parameters from the mother CM's cp9 HMM where possible to
2577 * save time. Sub CM CP9 model construction takes a long time which
2578 * is bad b/c a new sub CM is built for each target sequence in
2579 * cmalign.
2580 *
2581 * Args:
2582 * CM_t *cm - the CM
2583 * char *errbuf - for error messages
2584 * CM_t *mother_cm - mother CM, cm is a sub CM of mother_cm
2585 * CMSubMap_t *mother_map - map from cm to mother_cm and vice versa
2586 * cplan9_s **ret_hmm - CM Plan 9 HMM to be allocated, filled in and returned
2587 * CP9Map_t **ret_cp9map - map from the CP9 HMM to the CM and vice versa
2588 * Allocated and returned from here, caller must free.
2589 * int do_psi_test - TRUE to do a psi vs phi test, FALSE not to
2590 * float psi_vs_phi_threshold - allowable difference in expected number of times mapping
2591 * cm and hmm states are entered.
2592 * Returns: eslOK if CP9 is constructed (and passes the psi vs phi test if do_psi_test is TRUE)
2593 * ! eslOK upon failure, with informative error message written to errbuf.
2594 */
2595 int
2596 sub_build_cp9_hmm_from_mother(CM_t *cm, char *errbuf, CM_t *mother_cm, CMSubMap_t *mother_map, CP9_t **ret_hmm, CP9Map_t **ret_cp9map, int do_psi_test,
2597 float psi_vs_phi_threshold, int debug_level)
2598 {
2599 int status;
2600 int k; /* counter of consensus columns (HMM nodes)*/
2601 int mk,x;
2602 double *psi; /* expected num times each state visited in CM */
2603 double **phi; /* expected num times each state visited in HMM*/
2604 CP9Map_t *cp9map;
2605 CP9_t *hmm; /* CM plan 9 HMM we're going to construct from the sub_cm */
2606 float d;
2607 char ***tmap = NULL;
2608
2609 /* Contract check, we can't be in local mode in the CM */
2610 if(cm->flags & CMH_LOCAL_BEGIN) ESL_FAIL(eslEINCOMPAT, errbuf, "sub_build_cp9_hmm_from_mother(), CMH_LOCAL_BEGIN flag is up");
2611 if(cm->flags & CMH_LOCAL_END) ESL_FAIL(eslEINCOMPAT, errbuf, "sub_build_cp9_hmm_from_mother(), CMH_LOCAL_END flag is up");
2612 if(mother_cm->cp9->flags & CPLAN9_EL) ESL_FAIL(eslEINCOMPAT, errbuf, "sub_build_cp9_hmm_from_mother(), mother_cm's cp9 has EL local ends on");
2613 if(!(mother_cm->cp9->flags & CPLAN9_LOCAL_BEGIN)) ESL_FAIL(eslEINCOMPAT, errbuf, "sub_build_cp9_hmm_from_mother(), mother_cm's cp9 does not have local begins on");
2614 if(!(mother_cm->cp9->flags & CPLAN9_LOCAL_END)) ESL_FAIL(eslEINCOMPAT, errbuf, "sub_build_cp9_hmm_from_mother(), mother_cm's cp9 does not have local ends on.");
2615
2616 /* Allocate and initialize the cp9map */
2617 cp9map = AllocCP9Map(cm);
2618 /* Map the CM states to CP9 states and nodes and vice versa */
2619 CP9_map_cm2hmm(cm, cp9map, debug_level);
2620
2621 hmm = AllocCPlan9(cp9map->hmm_M, cm->abc);
2622 ZeroCPlan9(hmm);
2623 CPlan9SetNullModel(hmm, cm->null, 1.0); /* set p1 = 1.0 which corresponds to the CM */
2624 CPlan9InitEL(hmm, cm); /* set up hmm->el_from_ct and hmm->el_from_idx data, which
2625 * explains how the EL states are connected in the HMM. */
2626 hmm->null2_omega = cm->null2_omega;
2627 hmm->null3_omega = cm->null3_omega;
2628
2629 /* THIS COULD BE REMOVED IF WE DON"T DO THE PHI/PSI TEST! */
2630 if((psi = cm_ExpectedStateOccupancy(cm)) == NULL) goto ERROR;
2631 tmap = cm_CreateTransitionMap();
2632
2633 /* fill in transitions into node 1 and out of node M */
2634 cm2hmm_special_trans_cp9(cm, hmm, cp9map, psi, tmap);
2635
2636 /* renormalize only node 0 and node M transitions */
2637 /* node 0 */
2638 /* match */
2639 d = hmm->begin[1] + hmm->t[0][CTMI] + hmm->t[0][CTMD] + hmm->t[0][CTMEL]; /* we know that begin[k>1] == 0, we set it as such, so we skip their contribution */
2640 hmm->begin[1] /= d;
2641 hmm->t[0][CTMI] /= d;
2642 hmm->t[0][CTMD] /= d;
2643 hmm->t[0][CTMEL] /= d;
2644 /* insert */
2645 esl_vec_FNorm(hmm->t[0] + cp9_TRANS_INSERT_OFFSET, cp9_TRANS_NINSERT); /* transitions out of insert for node 0 (state N)*/
2646 /* delete */
2647 esl_vec_FSet (hmm->t[0] + cp9_TRANS_DELETE_OFFSET, cp9_TRANS_NDELETE, 0.); /* D_0 does not exist */
2648
2649 /* node M */
2650 /* match */
2651 d = esl_vec_FSum(hmm->t[hmm->M], cp9_TRANS_NMATCH) + hmm->end[hmm->M];
2652 esl_vec_FScale(hmm->t[hmm->M], cp9_TRANS_NMATCH, 1./d);
2653 hmm->end[hmm->M] /= d;
2654 /* insert */
2655 esl_vec_FNorm(hmm->t[hmm->M] + cp9_TRANS_INSERT_OFFSET, cp9_TRANS_NINSERT); /* insert */
2656 /* delete */
2657 esl_vec_FNorm(hmm->t[hmm->M] + cp9_TRANS_DELETE_OFFSET, cp9_TRANS_NDELETE); /* delete */
2658
2659 /* logoddsify them */
2660 for(x = 0; x < cp9_NTRANS; x++) hmm->tsc[x][0] = Prob2Score(hmm->t[0][x], 1.0);
2661 for(x = 0; x < cp9_NTRANS; x++) hmm->tsc[x][hmm->M] = Prob2Score(hmm->t[hmm->M][x], 1.0);
2662 hmm->bsc[1] = Prob2Score(hmm->begin[1], 1.0);
2663 hmm->esc[hmm->M] = Prob2Score(hmm->end[hmm->M], 1.0);
2664
2665 /* now for 1..M-1, copy the parameters from the mother's template HMM. */
2666 for(k = 0; k <= (mother_map->epos-mother_map->spos+1); k++) {
2667 mk = k + mother_map->spos - 1;
2668 if(k > 0) {
2669 esl_vec_FCopy(mother_cm->cp9->mat[mk], mother_cm->cp9->abc->K, hmm->mat[k]);
2670 for(x = 0; x < mother_cm->cp9->abc->Kp; x++) {
2671 hmm->msc[x][k] = mother_cm->cp9->msc[x][mk];
2672 }
2673 }
2674 esl_vec_FCopy(mother_cm->cp9->ins[mk], mother_cm->cp9->abc->K, hmm->ins[k]);
2675 for(x = 0; x < mother_cm->cp9->abc->Kp; x++) {
2676 hmm->isc[x][k] = mother_cm->cp9->isc[x][mk];
2677 }
2678
2679 /* transitions, skip k == 0 and M, we filled them out in cm2hmm_special_trans_cp9() */
2680 if(k > 1) {
2681 hmm->begin[k] = 0.;
2682 hmm->bsc[k] = -INFTY;
2683 }
2684 if(k > 0 && k < hmm->M) {
2685 /* careful with transitions out of match, we have to renormalize as
2686 * we go b/c mother_cm's cp9 has local ends up */
2687 for(x = cp9_TRANS_MATCH_OFFSET; x < cp9_TRANS_NMATCH; x++) {
2688 hmm->t[k][x] = mother_cm->cp9->t[mk][x] / (1. - mother_cm->cp9->end[mk]);
2689 hmm->tsc[x][k] = Prob2Score(hmm->t[k][x], 1.0);
2690 }
2691
2692 /* the rest of the probs/scores we can just copy b/c they're unaffected by local begins/ends */
2693 esl_vec_FCopy(mother_cm->cp9->t[mk] + cp9_TRANS_INSERT_OFFSET, cp9_NTRANS - cp9_TRANS_INSERT_OFFSET, hmm->t[k] + cp9_TRANS_INSERT_OFFSET);
2694 for(x = cp9_TRANS_INSERT_OFFSET; x < cp9_NTRANS; x++) {
2695 hmm->tsc[x][k] = mother_cm->cp9->tsc[x][mk];
2696 }
2697 hmm->end[k] = 0.;
2698 hmm->esc[k] = -INFTY;
2699 }
2700 }
2701
2702 /* Ripped out of cp9Logoddsify(),
2703 * Finally, fill the efficiently reordered transition scores for this HMM. */
2704 for (k = 0 ; k <= hmm->M; k++) {
2705 int *otsc_k = hmm->otsc + k*cp9O_NTRANS;
2706 otsc_k[cp9O_MM] = hmm->tsc[CTMM][k];
2707 otsc_k[cp9O_MI] = hmm->tsc[CTMI][k];
2708 otsc_k[cp9O_MD] = hmm->tsc[CTMD][k];
2709 otsc_k[cp9O_IM] = hmm->tsc[CTIM][k];
2710 otsc_k[cp9O_II] = hmm->tsc[CTII][k];
2711 otsc_k[cp9O_DM] = hmm->tsc[CTDM][k];
2712 otsc_k[cp9O_DD] = hmm->tsc[CTDD][k];
2713 otsc_k[cp9O_ID] = hmm->tsc[CTID][k];
2714 otsc_k[cp9O_DI] = hmm->tsc[CTDI][k];
2715 otsc_k[cp9O_BM] = hmm->bsc[k];
2716 otsc_k[cp9O_MEL]= hmm->tsc[CTMEL][k];
2717 otsc_k[cp9O_ME] = hmm->esc[k];
2718 }
2719
2720 hmm->flags |= CPLAN9_HASBITS; /* raise the log-odds ready flag */
2721
2722 if(debug_level > 1) debug_print_cp9_params(stdout, hmm, TRUE);
2723 if(do_psi_test) {
2724 /* Fill phi to check to make sure our HMM is "close enough" to our CM.
2725 * phi[k][0..2] is the expected number of times HMM node k state 0 (match), 1(insert),
2726 * or 2(delete) is entered. These should be *very close* (within 0.00001) to the psi
2727 * values for the CM states that they map to (psi[v] is the expected number of times
2728 * state v is entered in the CM).
2729 */
2730 fill_phi_cp9(hmm, &phi, 1, FALSE);
2731 if((status = check_psi_vs_phi_cp9(cm, errbuf, cp9map, psi, phi, (double) psi_vs_phi_threshold, debug_level)) != eslOK) return status;
2732 for(k = 0; k <= hmm->M; k++) free(phi[k]);
2733 free(phi);
2734 }
2735
2736 *ret_hmm = hmm;
2737 *ret_cp9map = cp9map;
2738
2739 cm_FreeTransitionMap(tmap);
2740 free(psi);
2741
2742 return eslOK;
2743
2744 ERROR:
2745 ESL_FAIL(eslEMEM, errbuf, "memory allocation error.");
2746 return 0; /* NEVERREACHED */
2747 }
2748
2749
2750 /* Function: CPlan9InitEL()
2751 * Incept: EPN, Tue Jun 19 13:10:56 2007
2752 *
2753 * Purpose: Initialize a CP9 HMM for possible EL local ends
2754 * by determining how the EL states should be connected
2755 * based on the CM node topology.
2756 *
2757 * Args: cp9 - the CP9 HMM, built from cm
2758 * cm - the CM the cp9 was built from
2759 *
2760 * Return: (void)
2761 */
2762 void
2763 CPlan9InitEL(CP9_t *cp9, CM_t *cm)
2764 {
2765 int status;
2766 int k; /* counter over HMM nodes */
2767 int nd;
2768 int *tmp_el_from_ct;
2769
2770 if(cm->emap == NULL) cm_Fail("CPlan9InitEL() cm->emap is NULL");
2771
2772 /* First copy the CM el self transition score/probability: */
2773 cp9->el_self = sreEXP2(cm->el_selfsc);
2774 cp9->el_selfsc = Prob2Score(cp9->el_self, 1.0);
2775
2776 /* For each HMM node k, we can transit FROM >= 0 EL states from
2777 * HMM nodes kp. Determine how many such valid transitions exist
2778 * from each node, then allocate and fill cp9->el_from_idx[k] and
2779 * cp9->el_from_cmnd arrays based on that.
2780 * This two-pass method saves memory b/c we only allocate for
2781 * what we'll need.
2782 */
2783
2784 /* Initialize to 0 */
2785 for(k = 0; k <= cp9->M; k++)
2786 {
2787 cp9->el_from_ct[k] = 0;
2788 cp9->has_el[k] = FALSE;
2789 }
2790 cp9->el_from_ct[(cp9->M+1)] = 0; /* special case, we can get to E state from EL states */
2791
2792 /* first pass to get number of valid transitions */
2793 for(nd = 0; nd < cm->nodes; nd++)
2794 {
2795 if ((cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATL_nd ||
2796 cm->ndtype[nd] == MATR_nd || cm->ndtype[nd] == BEGL_nd ||
2797 cm->ndtype[nd] == BEGR_nd) &&
2798 cm->ndtype[nd+1] != END_nd)
2799 {
2800 /*printf("HMM node %d can be reached from HMM node %d's EL state\n", cm->emap->rpos[nd], cm->emap->lpos[nd]);*/
2801 cp9->el_from_ct[cm->emap->rpos[nd]]++;
2802 cp9->has_el[cm->emap->lpos[nd]] = TRUE;
2803 }
2804 }
2805
2806 /* allocate cp9->el_from_idx[k], cp9->el_from_cmnd for all k */
2807 for(k = 0; k <= (cp9->M+1); k++)
2808 {
2809 if(cp9->el_from_idx[k] != NULL) /* if !NULL we already filled it, shouldn't happen */
2810 cm_Fail("ERROR in CPlan9InitEL() el_from_idx has already been initialized\n");
2811 if(cp9->el_from_ct[k] > 0)
2812 {
2813 ESL_ALLOC(cp9->el_from_idx[k], sizeof(int) * cp9->el_from_ct[k]);
2814 ESL_ALLOC(cp9->el_from_cmnd[k],sizeof(int) * cp9->el_from_ct[k]);
2815 }
2816 /* else it remains NULL */
2817 }
2818
2819 /* now fill in cp9->el_from_idx, we need a new counter array */
2820 ESL_ALLOC(tmp_el_from_ct, sizeof(int) * (cp9->M+2));
2821 for(k = 0; k <= (cp9->M+1); k++)
2822 tmp_el_from_ct[k] = 0;
2823 for(nd = 0; nd < cm->nodes; nd++)
2824 {
2825 if ((cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATL_nd ||
2826 cm->ndtype[nd] == MATR_nd || cm->ndtype[nd] == BEGL_nd ||
2827 cm->ndtype[nd] == BEGR_nd) &&
2828 cm->ndtype[nd+1] != END_nd)
2829 {
2830 k = cm->emap->rpos[nd];
2831 cp9->el_from_idx[k][tmp_el_from_ct[k]] = cm->emap->lpos[nd];
2832 cp9->el_from_cmnd[k][tmp_el_from_ct[k]] = nd;
2833 tmp_el_from_ct[k]++;
2834 }
2835 }
2836
2837 /* Debugging printfs */
2838 /* for(k = 0; k <= (cp9->M+1); k++)
2839 {
2840 for(c = 0; c < cp9->el_from_ct[k]; c++)
2841 printf("cp9->el_from_idx[%3d][%2d]: %4d\n", k, c, cp9->el_from_idx[k][c]);
2842 if(cp9->has_el[k])
2843 printf("node k:%3d HAS an EL!\n", k);
2844 }*/
2845
2846 /* Free memory and exit */
2847 free(tmp_el_from_ct);
2848 return;
2849
2850 ERROR:
2851 cm_Fail("Memory allocation error.");
2852 }