Debian Codegraph @

src/cm_submodel.c

raw symbols SCIP
1 /*****************************************************************
2 * cm_submodel.c (formerly sub_cm.c)
3 * EPN 07.25.06 (Benasque)
4 *
5 * Building submodels (sub CMs) from a template CM, that represent
6 * a contiguous subset of the consensus columns that were modelled
7 * by the template CM.
8 *
9 * These functions are still under development. No guarantees.
10 *
11 * NOTE: 'sub CM' here does not correspond to Zasha Weinberg's use
12 * of the term for filtering with subtrees of the model. To
13 * be unambiguous, I should have not used 'sub CM', it's bad
14 * form on my part. However 'sub CM' is so engrained in the
15 * codebase at this point, I'm wary to change it, so it stays.
16 *
17 */
18
19 #include <esl_config.h>
20 #include <p7_config.h>
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <math.h>
27
28 #include "easel.h"
29 #include "esl_alphabet.h"
30 #include "esl_random.h"
31 #include "esl_stack.h"
32 #include "esl_vectorops.h"
33 #include "esl_wuss.h"
34
35 #include "hmmer.h"
36
37 #include "infernal.h"
38
39 static void map_orig2sub_cm(CM_t *orig_cm, CM_t *sub_cm, CMSubMap_t *submap, int print_flag);
40 static int map_orig2sub_cm_helper(CM_t *orig_cm, CM_t *sub_cm, CMSubMap_t *submap, int orig_v, int sub_v);
41 static int cm2sub_cm_check_id_next_node(CM_t *orig_cm, CM_t *sub_cm, int orig_nd, int sub_nd,
42 CMSubMap_t *submap, CP9Map_t *orig_cp9map, CP9Map_t *sub_cp9map,
43 int print_flag);
44 static void cm2sub_cm_emit_probs(CM_t *orig_cm, CM_t *sub_cm, double *orig_psi, int v_s, int v_o1, int v_o2,
45 CMSubMap_t *submap);
46 static void cm2sub_cm_trans_probs(CM_t *orig_cm, CM_t *sub_cm, double *orig_psi, char ***tmap, int v_s,
47 CMSubMap_t *submap);
48 static void cm2sub_cm_trans_probs_S(CM_t *orig_cm, CM_t *sub_cm, double *orig_psi, char ***tmap, int v_start,
49 CMSubMap_t *submap);
50 static void cm2sub_cm_trans_probs_B_E(CM_t *orig_cm, CM_t *sub_cm, double *orig_psi, char ***tmap, int v_end,
51 CMSubMap_t *submap, int print_flag);
52 static void cm2sub_cm_add_single_trans(CM_t *orig_cm, CM_t *sub_cm, CMSubMap_t *submap, int orig_v, int orig_y,
53 int sub_v, int yoffset, double *orig_psi, char ***tmap);
54 static float cm2sub_cm_sum_subpaths(CM_t *orig_cm, CM_t *sub_cm, CMSubMap_t *submap, int start, int end,
55 int init_sub_start, char ***tmap, double *orig_psi);
56 static int cm_trans_check(CM_t *cm, int a, int b);
57 static void cm2sub_cm_subtract_root_subpaths(CM_t *orig_cm, CM_t *sub_cm, double *orig_psi, char ***tmap,
58 CMSubMap_t *submap, int print_flag);
59 static void cm2sub_cm_find_impossible_misc_cases(CM_t *orig_cm, CM_t *sub_cm, CMSubMap_t *submap, CMSubInfo_t *subinfo,
60 CP9Map_t *orig_cp9map, CP9Map_t *sub_cp9map, int print_flag);
61 static void cm2sub_cm_find_impossible_matr_cases(CM_t *orig_cm, CM_t *sub_cm, CMSubMap_t *submap, CMSubInfo_t *subinfo,
62 CP9Map_t *orig_cp9map, CP9Map_t *sub_cp9map, int print_flag);
63
64
65 /**************************************************************************
66 * EPN 10.25.06
67 * Function: AllocSubMap()
68 *
69 * Purpose: Determine maps between a template CM and a sub CM, which probably
70 * has less structure (less MATP nodes) than the template. Do this
71 * a bit indirectly, first get maps from each CM to a CP9 HMM,
72 * then use these maps to map the CMs to each other.
73 * See infernal.h for more info on the submap data structure.
74 *
75 * Args
76 * CM_t *sub_cm - the sub CM
77 * CM_t *orig_cm - the original template CM
78 * int sstruct - the first (leftmost) consensus posn we're keeping structure for
79 * int estruct - the last (rightmost) consensus posn we're keeping structure for
80 */
81
82 CMSubMap_t *
83 AllocSubMap(CM_t *sub_cm, CM_t *orig_cm, int sstruct, int estruct)
84 {
85 CMSubMap_t *submap;
86 int v;
87 int status;
88
89 ESL_ALLOC(submap, sizeof(struct submap_s));
90
91 submap->sub_M = sub_cm->M;
92 submap->orig_M = orig_cm->M;
93 /* Determine clen of the orig_cm */
94 submap->orig_clen = 0;
95 for(v = 0; v <= orig_cm->M; v++)
96 {
97 if(orig_cm->stid[v] == MATP_MP)
98 submap->orig_clen += 2;
99 else if(orig_cm->stid[v] == MATL_ML || orig_cm->stid[v] == MATR_MR)
100 submap->orig_clen++;
101 }
102 submap->sstruct = sstruct;
103 submap->estruct = estruct;
104 submap->sub_clen = (submap->estruct-submap->sstruct+1);
105 submap->spos = submap->sstruct;
106 submap->epos = submap->estruct;
107
108 /* Allocate and initialize arrays */
109 ESL_ALLOC(submap->s2o_id, sizeof(int) * (sub_cm->M+1));
110 ESL_ALLOC(submap->s2o_smap, sizeof(int *) * (sub_cm->M+1));
111 for(v = 0; v <= sub_cm->M; v++)
112 {
113 submap->s2o_id[v] = FALSE;
114 ESL_ALLOC(submap->s2o_smap[v], sizeof(int) * 2);
115 submap->s2o_smap[v][0] = -1;
116 submap->s2o_smap[v][1] = -1;
117 }
118
119 ESL_ALLOC(submap->o2s_smap, sizeof(int *) * (orig_cm->M+1));
120 for(v = 0; v <= orig_cm->M; v++)
121 {
122 ESL_ALLOC(submap->o2s_smap[v], sizeof(int) * 2);
123 submap->o2s_smap[v][0] = -1;
124 submap->o2s_smap[v][1] = -1;
125 }
126 return submap;
127
128 ERROR:
129 cm_Fail("Memory allocation error.");
130 return NULL; /* never reached */
131 }
132
133 /* Function: FreeSubMap()
134 * Returns: (void)
135 */
136
137 void
138 FreeSubMap(CMSubMap_t *submap)
139 {
140 int v;
141 for(v = 0; v <= submap->sub_M; v++)
142 free(submap->s2o_smap[v]);
143 free(submap->s2o_smap);
144 for(v = 0; v <= submap->orig_M; v++)
145 free(submap->o2s_smap[v]);
146 free(submap->o2s_smap);
147 free(submap->s2o_id);
148 free(submap);
149 }
150
151 /**************************************************************************
152 * EPN 10.26.06
153 * Function: AllocSubInfo()
154 *
155 * Purpose: Store information about a sub CM that is useful for testing
156 * if it constructed correctly.
157 *
158 * Args:
159 * int clen; - consensus length of the sub_cm
160 * Returns: CMSubInfo_t
161 */
162
163 CMSubInfo_t *
164 AllocSubInfo(int clen)
165 {
166 int status;
167 CMSubInfo_t *subinfo;
168 int i;
169 int ncases;
170
171 ESL_ALLOC(subinfo, sizeof(struct subinfo_s));
172 /* Allocate and initialize arrays */
173 ESL_ALLOC(subinfo->imp_cc, sizeof(int) * (clen + 2));
174 for(i = 0; i <= clen+1; i++)
175 subinfo->imp_cc[i] = FALSE;
176
177 /* 6 possible cases for predicting we get HMM distros wrong */
178 ncases = 6;
179 ESL_ALLOC(subinfo->apredict_ct, sizeof(int) * (ncases+1));
180 ESL_ALLOC(subinfo->spredict_ct, sizeof(int) * (ncases+1));
181 ESL_ALLOC(subinfo->awrong_ct, sizeof(int) * (ncases+1));
182 ESL_ALLOC(subinfo->swrong_ct, sizeof(int) * (ncases+1));
183 for(i = 0; i <= ncases; i++)
184 {
185 subinfo->apredict_ct[i] = 0;
186 subinfo->spredict_ct[i] = 0;
187 subinfo->awrong_ct[i] = 0;
188 subinfo->swrong_ct[i] = 0;
189 }
190 return subinfo;
191
192 ERROR:
193 cm_Fail("Memory allocation error.");
194 return NULL; /* never reached */
195 }
196
197 /* Function: FreeSubInfo()
198 * Returns: void
199 */
200
201 void
202 FreeSubInfo(CMSubInfo_t *subinfo)
203 {
204 free(subinfo->imp_cc);
205 free(subinfo->apredict_ct);
206 free(subinfo->spredict_ct);
207 free(subinfo->awrong_ct);
208 free(subinfo->swrong_ct);
209 free(subinfo);
210 }
211
212 /**************************************************************************
213 * EPN 09.22.06
214 * Function: map_orig2sub_cm()
215 *
216 * Purpose: Determine maps between a template CM and a sub CM, which probably
217 * has less structure (less MATP nodes) than the template. Do this
218 * a bit indirectly, first get maps from each CM to a CP9 HMM,
219 * then use these maps to map the CMs to each other.
220 * Args:
221 * CM_t *orig_cm - the original template CM
222 * CM_t *sub_cm - the sub CM
223 * CMSubMap_t *submap - the sub CM map
224 * int print_flag - TRUE to print out useful debugging info
225 * Returns: (void)
226 */
227 void
228 map_orig2sub_cm(CM_t *orig_cm, CM_t *sub_cm, CMSubMap_t *submap, int print_flag)
229 {
230 int status;
231 int k_s; /* HMM node counter */
232 int v_o;
233 int v_s;
234 int v; /* state index in CM */
235 int n_o;
236 int n_s;
237 char **nodetypes;
238 char **sttypes;
239 int sub_k;
240 int orig_k;
241 int sub_nd;
242 int orig_nd;
243 int x, y;
244 CP9Map_t *orig_cp9map;
245 CP9Map_t *sub_cp9map;
246
247 ESL_ALLOC(sttypes, sizeof(char *) * 10);
248 sttypes[0] = "D";
249 sttypes[1] = "MP";
250 sttypes[2] = "ML";
251 sttypes[3] = "MR";
252 sttypes[4] = "IL";
253 sttypes[5] = "IR";
254 sttypes[6] = "S";
255 sttypes[7] = "E";
256 sttypes[8] = "B";
257 sttypes[9] = "EL";
258
259 ESL_ALLOC(nodetypes, sizeof(char *) * 8);
260 nodetypes[0] = "BIF";
261 nodetypes[1] = "MATP";
262 nodetypes[2] = "MATL";
263 nodetypes[3] = "MATR";
264 nodetypes[4] = "BEGL";
265 nodetypes[5] = "BEGR";
266 nodetypes[6] = "ROOT";
267 nodetypes[7] = "END";
268
269 /* sanity check */
270 if(sub_cm->M > orig_cm->M)
271 cm_Fail("ERROR: sub_cm has more states than orig_cm in map_orig2sub_cm()\n");
272
273 /* We want maps from the orig_cm to a CP9 HMM and from the
274 * sub_cm to a CP9 HMM, but we don't need the actual HMMs, just
275 * the maps. */
276 /* Allocate and initialize the cp9maps */
277 orig_cp9map = AllocCP9Map(orig_cm);
278 sub_cp9map = AllocCP9Map(sub_cm);
279 /* Map the CM states to CP9 states and nodes, and vice versa */
280 CP9_map_cm2hmm(orig_cm, orig_cp9map, print_flag);
281 CP9_map_cm2hmm(sub_cm, sub_cp9map, print_flag);
282
283 /* Step through the consensus columns, filling in maps */
284 /* ROOT is special: */
285 v_s = 0; /* ROOT_S */
286 v_o = 0; /* ROOT_S */
287 map_orig2sub_cm_helper(orig_cm, sub_cm, submap, v_o, v_s);
288
289 /* ROOT_IL inserts before orig cc submap->spos */
290 k_s = 1; /* insert */
291 for(x = 0; x <= 1; x++)
292 for(y = 0; y <= 1; y++)
293 map_orig2sub_cm_helper(orig_cm, sub_cm, submap,
294 orig_cp9map->hns2cs[submap->spos-1][k_s][x],
295 sub_cp9map->hns2cs[0][k_s][y]);
296
297 /* ROOT_IR inserts after orig cc submap->epos */
298 k_s = 1; /* insert */
299 for(x = 0; x <= 1; x++)
300 for(y = 0; y <= 1; y++)
301 map_orig2sub_cm_helper(orig_cm, sub_cm, submap,
302 orig_cp9map->hns2cs[submap->epos][k_s][x],
303 sub_cp9map->hns2cs[submap->epos-submap->spos+1][k_s][y]);
304
305 for(sub_k = 1; sub_k <= sub_cp9map->hmm_M; sub_k++)
306 {
307 orig_k = sub_k + submap->spos - 1;
308 sub_nd = sub_cp9map->pos2nd[sub_k];
309 orig_nd = orig_cp9map->pos2nd[orig_k];
310
311 /* Check for an easy case to save time in the future: when
312 * the orig_cm and sub_cm nodes that map to this column are
313 * of the same type, and also the orig_cm and sub_cm nodes
314 * that map to the next column are also of the same type.
315 * We check for this in cm2sub_cm_check_id_next_node() if TRUE,
316 * then submap->s2o_id[sub_v] is set to TRUE for all states
317 * in this node. Later this will save time by just copying
318 * transition and emission parameters from the orig_cm for
319 * these states.
320 */
321 cm2sub_cm_check_id_next_node(orig_cm, sub_cm, orig_nd, sub_nd, submap, orig_cp9map,
322 sub_cp9map, print_flag);
323 for(k_s = 0; k_s < 3; k_s++) /* k_s = 0 (match), k_s = 1 (insert),
324 k_s = 2 (delete) */
325 for(x = 0; x <= 1; x++)
326 for(y = 0; y <= 1; y++)
327 map_orig2sub_cm_helper(orig_cm, sub_cm, submap,
328 orig_cp9map->hns2cs[orig_k][k_s][x],
329 sub_cp9map->hns2cs[sub_k][k_s][y]);
330 }
331 /* NOTE: We ignore mapping B states, E states and S states (except ROOT_S). We'll handle these guys
332 * specially when we fill in transition probabilities. The reason we don't map them is that
333 * I think its impossible to do it robustly. I saw cases with SSU where there were BIF nodes in the
334 * sub_cm for which I couldn't figure out which BIF nodes (and correspondingly BEGL and BEGR nodes)
335 * in the orig_cm they should map to. Regardless, even if there is a pretty, nice way I'm abandoning
336 * an attempt to find it for a crude way - we will handle the transitions involving these guys
337 * specially, without a need for a mapping between CMs.
338 */
339
340 /* If we're in debugging mode and print_flag == TRUE, we print and check the map */
341 if(print_flag)
342 {
343 /* First print submap->s2o_id */
344 for(v = 0; v <= sub_cm->M; v++)
345 if(submap->s2o_id[v] == TRUE)
346 printf("submap->s2o_id[%d] TRUE\n", v);
347 else
348 printf("submap->s2o_id[%d] FALSE\n", v);
349
350 printf("\n\n\nMAP\n\n\n");
351 for(v_s = 0; v_s < sub_cm->M; v_s++)
352 {
353 n_s = sub_cm->ndidx[v_s];
354 v_o = submap->s2o_smap[v_s][0];
355 if(sub_cm->sttype[v_s] == E_st)
356 printf("sub v:%4d END\n", v_s);
357 if(sub_cm->sttype[v_s] == S_st)
358 printf("sub v:%4d START\n", v_s);
359 if(sub_cm->sttype[v_s] == B_st)
360 printf("sub v:%4d BIF_B\n", v_s);
361 if((sub_cm->sttype[v_s] != B_st &&
362 sub_cm->sttype[v_s] != S_st) &&
363 sub_cm->sttype[v_s] != E_st)
364 {
365 if(v_o == -1 && sub_cm->sttype[(v_s+1)] == E_st) /* v_s is a dead insert */
366 continue;
367 if(v_o == -1 && sub_cm->sttype[v_s] != E_st)
368 cm_Fail("ERROR sub_cm state: %d type: %s node type: %s doesn't map to any state in orig_cm\n", v_s, sttypes[(int) sub_cm->sttype[v_s]], nodetypes[(int) sub_cm->ndtype[n_s]]);
369
370 n_o = orig_cm->ndidx[v_o];
371 if(print_flag) printf("sub v:%4d(%4d) %6s%6s | orig v:%4d(%4d) %6s%6s\n", v_s, n_s, nodetypes[(int) sub_cm->ndtype[n_s]], sttypes[(int) sub_cm->sttype[v_s]], v_o, n_o, nodetypes[(int) orig_cm->ndtype[n_o]], sttypes[(int) orig_cm->sttype[v_o]]);
372 /* check to make sure submap->o2s_smap is consistent */
373 if(submap->o2s_smap[v_o][0] != v_s && submap->o2s_smap[v_o][1] != v_s)
374 cm_Fail("ERROR inconsistency; neither o2s_smap[%d][0] and [1] is %d\n", v_o, v_s);
375
376 v_o = submap->s2o_smap[v_s][1];
377 if(v_o != -1)
378 {
379 n_o = orig_cm->ndidx[v_o];
380 if(print_flag) printf(" | orig v:%4d(%4d) %6s%6s\n", v_o, n_o, nodetypes[(int) orig_cm->ndtype[n_o]], sttypes[(int) orig_cm->sttype[v_o]]);
381 /* check to make sure o2s_smap is consistent */
382 if(submap->o2s_smap[v_o][0] != v_s && submap->o2s_smap[v_o][1] != v_s)
383 {
384 cm_Fail("ERROR inconsistency; neither o2s_smap[%d][0] and [1] is %d\n", v_o, v_s);
385 }
386 }
387 }
388 }
389 }
390
391 /* Clean up and return */
392 FreeCP9Map(orig_cp9map);
393 FreeCP9Map(sub_cp9map);
394 free(sttypes);
395 free(nodetypes);
396 return;
397
398 ERROR:
399 cm_Fail("Memory allocation error.");
400 }
401
402 /**************************************************************************
403 * EPN 08.30.06
404 * map_orig2sub_cm_helper()
405 *
406 * helper function for map_orig2sub_cm(),
407 *
408 * Purpose: Fill in specific part of the map, given orig_v (orig_cm state),
409 * sub_v (sub_cm state)
410 * Args:
411 * CM_t *orig_cm
412 * CM_t *sub_cm
413 * CMSubMap_t *submap
414 * int orig_v;
415 * int sub_v;
416 * Returns: 1 if a new mapping was made, 0 if not
417 */
418 static int
419 map_orig2sub_cm_helper(CM_t *orig_cm, CM_t *sub_cm, CMSubMap_t *submap, int orig_v, int sub_v)
420 {
421 int sub_nd;
422 int orig_nd;
423
424 /*printf("\nin helper: orig_v: %d sub_v: %d\n", orig_v, sub_v);*/
425
426 if(orig_v == -1 || sub_v == -1)
427 return 0;
428
429 /* check to see if we already have this mapping */
430 if(submap->o2s_smap[orig_v][0] == sub_v || submap->o2s_smap[orig_v][1] == sub_v)
431 return 0;
432
433 orig_nd = orig_cm->ndidx[orig_v];
434 sub_nd = sub_cm->ndidx[sub_v];
435
436 if(sub_cm->sttype[sub_v] == IL_st || sub_cm->sttype[sub_v] == IR_st)
437 {
438 /* Make sure that neither orig_v nor sub_v is a detached insert state,
439 * if either is, we return b/c it's irrelevant, and we don't store that info in the maps */
440 if(orig_cm->sttype[(orig_v+1)] == E_st || sub_cm->sttype[(sub_v+1)] == E_st)
441 return 0;
442 }
443 /* 09.14.06 I think that some of the code that checks for cases where we wnat to avoid mapping is unnecessary!,
444 * but I'm not sure what...*/
445
446 /* Check for a case we want to avoid mapping. We exploit the fact that we know a MATP_nd in a sub_cm MUST be mapped
447 * to a corresponding MATP_nd in the original template CM: */
448 if(sub_cm->ndtype[sub_nd] == MATP_nd)
449 if(orig_cm->ndtype[orig_nd] == MATP_nd && sub_cm->sttype[sub_v] != orig_cm->sttype[orig_v])
450 return 0;
451
452 /* Fill in submap->o2s_smap */
453 if(submap->o2s_smap[orig_v][0] == -1)
454 {
455 if (submap->o2s_smap[orig_v][1] != -1)
456 cm_Fail("ERROR in map_orig2sub_cm_helper, submap->o2s_smap[%d][0] is -1 but submap->o2s_smap[%d][1] is not, this shouldn't happen.\n", orig_v, orig_v);
457 else
458 submap->o2s_smap[orig_v][0] = sub_v;
459 }
460 else if (submap->o2s_smap[orig_v][1] != -1)
461 {
462 if(submap->o2s_smap[orig_v][0] == sub_v || submap->o2s_smap[orig_v][1] == sub_v)
463 /* abort!, we already have this mapping */
464 return 0;
465 else
466 cm_Fail("ERROR in map_orig2sub_cm_helper, submap->o2s_smap[%d][0] is not -1 and submap->o2s_smap[%d][1] is not -1, this shouldn't happen.\n", orig_v, orig_v);
467 }
468 else /* submap->o2s_smap[orig_v][0] != -1 && submap->o2s_smap[orig_v][1] == -1 */
469 {
470 if(submap->o2s_smap[orig_v][0] == sub_v || submap->o2s_smap[orig_v][1] == sub_v)
471 /* abort!, we already have this mapping */
472 return 0;
473 submap->o2s_smap[orig_v][1] = sub_v;
474 }
475
476 /* now fill in submap->s2o_smap */
477 if(submap->s2o_smap[sub_v][0] == -1)
478 if (submap->s2o_smap[sub_v][1] != -1)
479 cm_Fail("ERROR in map_sub2orig_cm_helper, submap->s2o_smap[%d][0] is -1 but submap->s2o_smap[%d][1] is not, this shouldn't happen.\n", sub_v, sub_v);
480 else
481 submap->s2o_smap[sub_v][0] = orig_v;
482 else if (submap->s2o_smap[sub_v][1] != -1)
483 cm_Fail("ERROR in map_sub2orig_cm_helper, submap->s2o_smap[%d][0] is not -1 and submap->s2o_smap[%d][1] is not -1, this shouldn't happen.\n", sub_v, sub_v);
484 else /* submap->s2o_smap[sub_v][0] != -1 && submap->s2o_smap[sub_v][1] == -1 */
485 submap->s2o_smap[sub_v][1] = orig_v;
486 return 1;
487 }
488
489
490 /****************************************************************************
491 * Function: build_sub_cm()
492 * EPN 08.28.06
493 *
494 * Purpose: Given a template CM (orig_cm) built from an MSA of consensus columns
495 * 1..M, build a new, "sub CM" (sub_cm), that only models well-nested base pairs
496 * that occur (have both left and right halves) between consensus columns
497 * [sstruct..estruct]. The sub_cm will only model the consensus
498 * columns between [sstruct..estruct].
499 *
500 * We attempt to construct the sub CM such that:
501 *
502 * 1. All 'mapped' states in the orig_cm and sub_cm have identical
503 * 'psi' values, i.e. the expected number of times entered in a single parse
504 * is identical.
505 *
506 * 2. Alignments sampled from it should 'follow the same probability distributions'
507 * as alignments sampled from the template CM and then truncated,
508 * removing columns outside [sstruct..estruct] Specifically, 'follow
509 * the same probability distributions' means if we built a ML HMM (Weinberg
510 * style) from the resulting infinite alignments, we would have identical
511 * HMMs for the sub-CM alignments and for the truncated template CMs. It turns
512 * out this is impossible for certain situations
513 * that cause differences in the topology of the orig_cm and sub_cm. These
514 * situations, so-called 'impossible cases', cause the distributions out of a
515 * single ML HMM node to be different between the orig_cm, but are relatively
516 * rare (a rough, rough estimate is 1% of ML HMM nodes are affected). We can
517 * predict when all of these situations will occur, but sometimes when we
518 * predict a ML HMM node will be off in this way, it is in fact identical
519 * between the sub_cm and orig_cm for reasons I can't quite grasp.
520 *
521 * This function builds a sub_cm and then potentially performs up to 3 checks
522 * to see if 1 and 2 above are satisfied (it only checks if 2 holds for
523 * non-impossible cases, and that we can accurately predict the impossible
524 * cases). The 3 potential checks are:
525 *
526 * A. Calculate the expected number of times that each state in the orig_cm
527 * and sub_cm is entered (psi[v] = exp # times state v is entered), and
528 * make sure these values are within 'threshold' of each other for all
529 * states that map between the orig_cm and sub_cm. This is performed
530 * by the 'check_orig_psi_vs_sub_psi' function, which is called within
531 * this function if either the 'do_acheck' and/or 'do_scheck' parameters
532 * passed in are TRUE.
533 *
534 * B. Analytically (not by sampling) building 2 ML HMMs, one from the orig_cm
535 * and one from the sub_cm, predicting the impossible cases and testing t
536 * o make sure all the corresponding parameters for non-impossible cases
537 * are within 'threshold' of each other. This is done within the
538 * check_sub_cm() function which is called from this function if the
539 * 'do_acheck' parameter passed in is set to TRUE.
540 *
541 * C. Build a CP9 ML HMM analytically from the sub_cm. Sample a deep MSA
542 * from the orig_cm and potentially truncate outside [sstruct..estruct]. Use
543 * counts from the MSA to build a ML HMM for the orig_cm. Then perform
544 * chi-squared tests to see if the counts in the orig_cm's HMM could
545 * have been generated by the distributions in the sub_cm's HMM. This
546 * check is performed if the 'do_scheck' parameter passed in is set
547 * to TRUE.
548 *
549 * Args: orig_cm - the original model, which we're going to (potentially)
550 * remove some structure from
551 * errbuf - for error messages
552 * ret_sub_cm - the new sub_cm built from orig_cm with some structure removed.
553 * sstruct - the first position (consensus column) we want to model
554 * estruct - the last position we will model
555 * print_flag - TRUE to print debugging statements
556 *
557 * Returns: eslOK on success
558 * eslEINCONCEIVABLE if something inconceivable happens
559 * eslEINCOMPAT on contract violation
560 * eslEMEM on memory error
561 */
562 int
563 build_sub_cm(CM_t *orig_cm, char *errbuf, CM_t **ret_cm, int sstruct, int estruct, CMSubMap_t **ret_submap, int print_flag)
564 {
565 int status;
566 CM_t *sub_cm; /* new covariance model, a submodel of the template */
567 CMConsensus_t *con; /* growing consensus info for orig_cm */
568 Parsetree_t *mtr; /* master structure tree from the alignment */
569 char *sub_cstr; /* consensus substructure display string */
570 int *sub_ct; /* 0..con->clen-1 base pair partners array */
571 int cpos; /* position counter within orig_cm */
572 int sub_cpos; /* position counter within sub_cm */
573 char ***tmap; /* hard-coded transition map, for convenience */
574 double *orig_psi; /* expected num times each state visited in orig_cm */
575 int v_s; /* state counter for sub_cm */
576 int n_s; /* node counter for sub_cm */
577 FILE *ofp; /* an open output file */
578 int spos; /* first consensus (match) column of the orig_cm to
579 * model with the sub_cm */
580 int epos; /* last consensus (match) column of the orig_cm to
581 * model with the sub_cm */
582 CMSubMap_t *submap;
583
584 /* check to make sure that we can actually build a sub CM of this model */
585 if((orig_cm->flags & CMH_LOCAL_BEGIN) || (orig_cm->flags & CMH_LOCAL_END)) ESL_FAIL(eslEINCOMPAT, errbuf, "build_sub_cm() trying to build a sub CM of a CM already in local mode, not yet supported.\n");
586 if(orig_cm->flags & CM_IS_SUB) ESL_FAIL(eslEINCOMPAT, errbuf, "build_sub_cm(), trying to build a sub CM of a CM that is itself a sub CM.");
587
588 /* Much of the code for building and checking sub CMs relies on the fact that every insert
589 * state in the sub CM maps exactly 1 insert state in the original CM. This is fine if we
590 * have removed ambiguities by detaching all original CM insert states that are 1 state
591 * before an END_E state. This was probably done when the CM was built, but we redo it here
592 * in case it was not.
593 */
594 cm_find_and_detach_dual_inserts(orig_cm,
595 FALSE, /* DON'T check that these states have 0 counts (they may not due to priors) */
596 TRUE); /* DO detach END_E-1 insert states, making them unreachable */
597
598 /* Get the consensus sequence and consensus structure information from the original CM */
599 if((con = CreateCMConsensus(orig_cm, orig_cm->abc)) == NULL) ESL_FAIL(eslFAIL, errbuf, "build_sub_cm(), unable to create cm consensus data");
600 if(print_flag)
601 {
602 printf("con->cseq : %s\n", con->cseq);
603 printf("con->cstr : %s\n", con->cstr);
604 printf("clen : %d\n", con->clen);
605 }
606
607 spos = sstruct;
608 epos = estruct;
609
610 /* Fill a new ct array for the sub_cm. The sub_cm will only model the consensus columns
611 * between spos and epos, and only the structure between spos
612 * and epos. First copy the template (original) CMs ct array but only for the
613 * appropriate consensus columns that lie in between both structure and model boundarIes
614 * Next, eliminate any structure that lies outside the structure boundaries.
615 */
616
617 ESL_ALLOC(sub_ct, sizeof(int) * (epos - spos + 1));
618 /* First just copy ct array for model boundaries from con->ct */
619 for (cpos = (spos-1); cpos < epos; cpos++)
620 {
621 sub_cpos = cpos - (spos-1);
622 if(con->ct[cpos] != -1 &&
623 (con->ct[cpos] < (spos-1) ||
624 con->ct[cpos] >= epos))
625 sub_ct[sub_cpos] = -1;
626 else
627 sub_ct[sub_cpos] = con->ct[cpos];
628 }
629 /* Second remove structure outside structural boundaries */
630 for (cpos = (spos-1); cpos < epos; cpos++)
631 {
632 sub_cpos = cpos - (spos-1);
633 if ((cpos+1) < sstruct || (cpos+1) > estruct) /* cpos goes 1..clen, but ct is indexed
634 * 0..clen-1.*/
635 {
636 /* CreateCMConsensus() uses -1 in ct[] to indicate single
637 * stranded (different convention than WUSS2ct()). */
638 if (sub_ct[sub_cpos] != -1)
639 sub_ct[sub_ct[sub_cpos]] = -1;
640 sub_ct[sub_cpos] = -1;
641 }
642 }
643
644 /* Construct the new structure ss_cons based on the template CM ct array.
645 * We could do this similar to how display.c::CreateCMConsensus()
646 * does it to get the fully formatted WUSS ([{<>}]) string but
647 * lazily we just do <> bps here.
648 */
649 ESL_ALLOC(sub_cstr, sizeof(char) * (epos - spos + 2));
650 for (cpos = (spos-1); cpos < epos; cpos++)
651 {
652 sub_cpos = cpos - (spos-1);
653 if(sub_ct[sub_cpos] == -1) sub_cstr[sub_cpos] = '.';
654 else if (sub_ct[sub_cpos] > cpos) sub_cstr[sub_cpos] = '<';
655 else if (sub_ct[sub_cpos] < cpos) sub_cstr[sub_cpos] = '>';
656 else cm_Fail("ERROR: weird error in build_sub_cm()\n");
657 }
658 sub_cstr[(epos-spos+1)] = '\0';
659
660 /* Build the new sub_cm given the new consensus structure. But don't
661 * parameterize it yet.
662 */
663 if((status = ConsensusModelmaker(orig_cm->abc, errbuf, sub_cstr, (epos-spos+1), TRUE, &sub_cm, &mtr)) != eslOK) return status;
664 /* TRUE in ConsensusModelmaker() call says 'yes, we're building a sub CM, allow invalid CMs in cm_from_guide() (see comments in that function for details)*/
665
666 /* Set creation time */
667 if ((status = cm_SetCtime(sub_cm)) != eslOK) ESL_FAIL(status, errbuf, "Failed to record timestamp");
668
669 /* Rebalance the CM for optimization of D&C */
670 CM_t *new;
671 if((status = CMRebalance(sub_cm, errbuf, &new)) != eslOK) return status;
672 FreeCM(sub_cm);
673 sub_cm = new;
674
675 submap = AllocSubMap(sub_cm, orig_cm, sstruct, estruct);
676 if(print_flag)
677 {
678 printf("\n\norig struct: %s\n", con->cstr);
679 printf("\n\nnew struct : %s\n", sub_cstr);
680 }
681
682 /* Map states from orig_cm to sub_cm and vice versa. */
683 map_orig2sub_cm(orig_cm, sub_cm, submap, print_flag);
684
685 /* Fill orig_psi, which we need to determine the sub_cm parameters. */
686 orig_psi = cm_ExpectedStateOccupancy(orig_cm);
687 /* we need a transition map too */
688 tmap = cm_CreateTransitionMap();
689
690 CMZero(sub_cm);
691 CMSetNullModel(sub_cm, orig_cm->null);
692 sub_cm->el_selfsc = orig_cm->el_selfsc;
693 sub_cm->beta_W = orig_cm->beta_W;
694 sub_cm->tau = orig_cm->tau;
695
696 /* copy the options from the template CM, but turn off the CM_ALIGN_SUB and
697 * CM_CONFIG_SUB options and turn on the CM_IS_SUB flag */
698 sub_cm->config_opts = orig_cm->config_opts;
699 sub_cm->align_opts = orig_cm->align_opts;
700 sub_cm->search_opts = orig_cm->search_opts;
701 sub_cm->flags = 0;
702 if(sub_cm->config_opts & CM_CONFIG_SUB) sub_cm->config_opts &= ~CM_CONFIG_SUB;
703 if(sub_cm->align_opts & CM_ALIGN_SUB) sub_cm->align_opts &= ~CM_ALIGN_SUB;
704 sub_cm->flags |= CM_IS_SUB;
705
706 /* Fill in emission probabilities */
707 for(v_s = 0; v_s < sub_cm->M; v_s++)
708 {
709 if(sub_cm->sttype[(v_s+1)] == E_st) /* detached insert */
710 esl_vec_FNorm(sub_cm->e[v_s], orig_cm->abc->K); /* equiprobable, but irrelevant, this state will never be reached */
711 else if(sub_cm->sttype[v_s] != S_st &&
712 sub_cm->sttype[v_s] != D_st &&
713 sub_cm->sttype[v_s] != B_st &&
714 sub_cm->sttype[v_s] != E_st)
715 cm2sub_cm_emit_probs(orig_cm, sub_cm, orig_psi, v_s, submap->s2o_smap[v_s][0], submap->s2o_smap[v_s][1], submap);
716 }
717 /* Fill in transition virtual counts.
718 * First handle non-B,S,E states, we'll deal with B,S,Es later.
719 * The reason we have to wait is that we can't (I don't think at least)
720 * unambiguously map the sub_cm B, S, or E states to orig_cm states.
721 */
722 for(v_s = 0; v_s < sub_cm->M; v_s++)
723 {
724 if(sub_cm->sttype[(v_s+1)] == E_st) /* detached insert */
725 esl_vec_FNorm(sub_cm->t[v_s], sub_cm->cnum[v_s]); /* equiprobable, but irrelevant, this state will never be reached */
726 else if(v_s == 0 ||
727 (sub_cm->sttype[v_s] != S_st &&
728 sub_cm->sttype[v_s] != B_st &&
729 sub_cm->sttype[v_s] != E_st))
730 cm2sub_cm_trans_probs(orig_cm, sub_cm, orig_psi, tmap, v_s, submap);
731 }
732
733
734 /* Address problem 090806 (in the 00LOG of ~/notebook/6_0725_inf_sub_cm/), by
735 * retraversing the structure and subtracting out subpaths that have been counted twice
736 * for a special situation involving the two inserts of ROOT and MATP states
737 */
738 for(n_s = 0; n_s < sub_cm->nodes; n_s++)
739 {
740 if(sub_cm->ndtype[n_s] == MATP_nd && (sub_cm->sttype[(sub_cm->nodemap[n_s] + 5)+1] != E_st))
741 {
742
743 if((submap->s2o_smap[sub_cm->nodemap[n_s] + 4][1] != -1) ||
744 (submap->s2o_smap[sub_cm->nodemap[n_s] + 5][1] != -1))
745 ESL_FAIL(eslEINCONCEIVABLE, errbuf, "build_sub_cm(), MATP_IL or MATP_IR node: %d map to 2 cm states\n", n_s);
746 if(submap->s2o_smap[sub_cm->nodemap[n_s] + 4][0] != (submap->s2o_smap[sub_cm->nodemap[n_s] + 5][0] - 1))
747 ESL_FAIL(eslEINCONCEIVABLE, errbuf, "build_sub_cm(), MATP_IL or MATP_IR node: %d don't map to adjacent orig_cm states\n", n_s);
748 }
749 if(sub_cm->ndtype[n_s] == ROOT_nd && sub_cm->ndtype[n_s+1] != BIF_nd) /* ROOT->BIFs are handled special
750 * (see next loop) */
751 cm2sub_cm_subtract_root_subpaths(orig_cm, sub_cm, orig_psi, tmap, submap, print_flag);
752 }
753
754 /* Go back through and fill in the transitions into E and B states and out of S states */
755 for(v_s = 0; v_s < sub_cm->M; v_s++)
756 {
757 if(sub_cm->sttype[v_s] == S_st)
758 cm2sub_cm_trans_probs_S(orig_cm, sub_cm, orig_psi, tmap, v_s, submap);
759
760 if(sub_cm->sttype[v_s] == E_st || sub_cm->sttype[v_s] == B_st)
761 cm2sub_cm_trans_probs_B_E(orig_cm, sub_cm, orig_psi, tmap, v_s, submap, print_flag);
762 /* convention is to leave transitions out of BIF_B as 0.0, all the code knows they're obligate */
763 }
764
765 /* Remove sub_cm ambiguities by finding and detaching sub CM insert states
766 * that are 1 state before END_E states by setting transitions into
767 * such states as 0.0.
768 */
769 cm_find_and_detach_dual_inserts(sub_cm,
770 FALSE, /* DON'T check that these states have 0 counts (they won't due to priors) */
771 TRUE); /* DO detach END_E-1 insert states */
772
773 /*debug_sub_cm_check_all_trans(orig_cm, sub_cm, submap);*/
774
775 /* reset sub_cm->qdbinfo->setby, we don't want to have to calculate QDBs for the sub CM unless we have to */
776 if(sub_cm->qdbinfo->setby == CM_QDBINFO_SETBY_INIT) sub_cm->qdbinfo->setby = CM_QDBINFO_SETBY_SUBINIT;
777 if(sub_cm->W == 0) {
778 sub_cm->W = orig_cm->W;
779 sub_cm->W_setby = CM_W_SETBY_SUBCOPY;
780 }
781
782 /* Finally renormalize the CM */
783 CMRenormalize(sub_cm);
784 /* DO NOT LOGODDSIFY YET, we'll do this when we call ConfigCM() for this CM,
785 * the logsoddsification step takes a significant amount of time.
786 * CMLogoddsify(sub_cm);
787 */
788
789 if(print_flag) {
790 ofp = fopen("sub.cm", "w");
791 printf("%-40s ... ", "Saving model to file");
792 fflush(stdout);
793 cm_file_WriteASCII(ofp, -1, sub_cm);
794 printf("done.\n");
795
796 printf("\nDEBUG PRINT OF ORIG_CM PARAMETERS:\n");
797 debug_print_cm_params(stdout, orig_cm);
798 printf("\nDEBUG PRINT OF SUB_CM PARAMETERS:\n");
799 debug_print_cm_params(stdout, sub_cm);
800 }
801
802 /* Cleanup and exit. */
803 cm_FreeTransitionMap(tmap);
804 free(sub_cstr);
805 free(sub_ct);
806 FreeCMConsensus(con);
807 FreeParsetree(mtr);
808 free(orig_psi);
809
810 *ret_cm = sub_cm;
811 *ret_submap = submap;
812
813 return eslOK;
814
815 ERROR:
816 ESL_FAIL(eslEMEM, errbuf, "build_sub_cm() memory allocation error.");
817 return FALSE; /* never reached */
818 }
819
820 /**************************************************************
821 * Function: CP9NodeForPosn()
822 * EPN 07.25.06 Benasque, Spain
823 *
824 * Purpose: Determine the node of the CP9 HMM that is most likely to
825 * have emitted (from either its Match or Insert state)
826 * a given posn in the target sequence.
827 *
828 * Args: hmm - the CM plan 9 HMM
829 * i0 - first posn of target subseq with info in posterior matrix
830 * j0 - last posn of target subseq with info in posterior matrix
831 * x - posn of target subsequence we're interested in
832 * L - last position of target sequence
833 * post - the posterior matrix for the hmm
834 * ret_node - RETURN: index of node with highest probability of emitting x
835 * ret_type - RETURN: type of state in ret_node with highest probability
836 * pmass - probability mass to require on left of start or right of end
837 * is_start - TRUE if we're doing left of start,
838 * print_flag- TRUE to print out info on most likely node
839 *
840 * Returns: eslOK on success;
841 * eslEINVAL on contract violation.
842 */
843 void
844 CP9NodeForPosn(CP9_t *hmm, int i0, int j0, int x, CP9_MX *post,
845 int *ret_node, int *ret_type, float pmass, int is_start,
846 int print_flag)
847 {
848 /* post->mmx[i][k]: posterior probability that posn i was emitted from node k's
849 match state */
850 int max_k; /* node index with highest posterior probability of emitting posn x */
851 int max_type; /* type of state in max_k node with max probability '0' for match,
852 '1' for insert */
853 int max_sc; /* score (log probability) from post matrix for max_k node max_type state type */
854 int k; /* counter over nodes */
855
856 if(!is_start) pmass = 1. - pmass; /* we move left to right */
857
858 /*printf("in CP9NodeForPosn is_start: %d pmass: %f\n", is_start, pmass);*/
859 if(x > j0 || x < i0)
860 /*ESL_XFAIL(eslEINVAL, "ERROR in CP9NodeForPosn(), asking for position x: %d outside subseq bounds i0: %d j0: %d\n", x, i0, j0);*/
861 cm_Fail("ERROR in CP9NodeForPosn(), asking for position x: %d outside subseq bounds i0: %d j0: %d\n", x, i0, j0);
862
863 if(post->mmx[x][0] > post->imx[x][0])
864 {
865 max_sc = post->mmx[x][0];
866 max_type = 0; /* match */
867 }
868 else
869 {
870 max_sc = post->imx[x][0];
871 max_type = 1; /* insert */
872 }
873 max_k = 0;
874 /* move left to right through HMM nodes */
875 for(k = 1; k <= hmm->M; k++)
876 {
877 if(post->mmx[x][k] > max_sc)
878 {
879 max_k = k;
880 max_sc = post->mmx[x][k];
881 max_type = 0; /* match */
882 }
883 if(post->imx[x][k] > max_sc)
884 {
885 max_k = k;
886 max_sc = post->imx[x][k];
887 max_type = 1; /* insert */
888 }
889 }
890
891 if(print_flag)
892 {
893 if(max_type == 0)
894 printf("MATCH | mx->mmx[%3d][%3d]: %9d | %8f\n", x, max_k, post->mmx[x][max_k], Score2Prob(post->mmx[x][max_k], 1.));
895 else
896 printf("INSERT | mx->imx[%3d][%3d]: %9d | %8f\n", x, max_k, post->imx[x][max_k], Score2Prob(post->imx[x][max_k], 1.));
897 }
898 *ret_node = max_k;
899 *ret_type = max_type;
900 return;
901 }
902
903
904 /**********************************************************
905 * Function: StripWUSSGivenCC()
906 * EPN 09.07.05
907 *
908 * Purpose: Strips a secondary structure string in WUSS notation
909 * of base pair information for specific match (consensus) columns.
910 * namely those before the first match column given by first_match,
911 * and after the last match column, given by last_match
912 * The msa->ss_cons secondary structure string is modified.
913 *
914 * Characters <([{ are converted to : (left base of base pairs)
915 * Characters >)]} are converted to : (right base of base pairs)
916 * Characters _-, are converted to : (unpaired bases)
917 * Characters .:~ are untouched
918 * Pseudoknot characters are converted to : as well.
919 *
920 * Args: msa - the multiple sequence alignment
921 * gapthresh - the gap threshold for calling a match column
922 * first_match - first match column to keep structure for
923 * last_match - last match column to keep structure for
924 * Returns: (void)
925 */
926 void
927 StripWUSSGivenCC(ESL_MSA *msa, float gapthresh, int first_match, int last_match)
928 {
929 int status;
930 int *matassign; /* 0..alen-1 array; 0=insert col, 1=match col */
931 int gaps;
932 int apos;
933 int idx;
934 int cc;
935 int *ct; /* 0..alen-1 base pair partners array */
936
937 /* Contract check */
938 if(msa->flags & eslMSA_DIGITAL)
939 cm_Fail("ERROR in StripWUSSGivenCC, MSA is digitized.\n");
940
941 /* 1. Determine match/insert assignments
942 * matassign is 1..alen. Values are 1 if a match column, 0 if insert column.
943 */
944 ESL_ALLOC(matassign, sizeof(int) * (msa->alen+1));
945 matassign[0] = 0; /* no 0th column in MSA */
946 for (apos = 0; apos < msa->alen; apos++)
947 {
948 for (gaps = 0, idx = 0; idx < msa->nseq; idx++)
949 if (esl_abc_CIsGap(msa->abc, msa->aseq[idx][apos])) gaps++;
950 matassign[apos+1] = ((double) gaps / (double) msa->nseq > gapthresh) ? 0 : 1;
951 }
952
953 /* 2. Determine a "ct" array, base-pairing partners for each position.
954 * Disallow/ignore pseudoknots. (That's what the FALSE flag does.)
955 * ct[] values give the index of a base pairing partner, or 0 for unpaired positions.
956 * Even though msa->ss_cons is in the 0..alen-1 coord system of msa, ct[]
957 * comes back in the 1..alen coord system of dsq.
958 */
959 ESL_ALLOC(ct, (msa->alen+1) * sizeof(int));
960 if (esl_wuss2ct(msa->ss_cons, msa->alen, ct) != eslOK)
961 cm_Fail("Consensus structure string is inconsistent");
962
963 /* 3. Make sure the consensus structure "ct" is consistent with the match assignments.
964 * Wipe out all structure in insert columns; including the base-paired
965 * partner of insert-assigned columns.
966 * Also, remove structure outside of the consensus columns that
967 * map to the HMM nodes first_match and last_match.
968 */
969 cc = 0;
970 for (apos = 1; apos <= msa->alen; apos++)
971 {
972 if (! matassign[apos])
973 {
974 if (ct[apos] != 0) ct[ct[apos]] = 0;
975 ct[apos] = 0;
976 }
977 else /* matassign[apos] == 1 */
978 {
979 cc++;
980 if(cc < first_match || cc > last_match)
981 {
982 if (ct[apos] != 0) ct[ct[apos]] = 0;
983 ct[apos] = 0;
984 }
985 }
986 }
987
988 /* Next construct the new msa->ss_cons based on the ct array.
989 * We should do this similar to display.c::CreateCMConsensus()
990 * does it to get the fully formatted WUSS ([{<>}]) string but
991 * lazily we just do <> bps here.
992 */
993 for (apos = 1; apos <= msa->alen; apos++)
994 {
995 if (ct[apos] == 0 ) msa->ss_cons[apos-1] = '.';
996 else if (ct[apos] > apos) msa->ss_cons[apos-1] = '<';
997 else if (ct[apos] < apos) msa->ss_cons[apos-1] = '>';
998 else cm_Fail("ERROR: weird error in StripWUSSGivenCC\n");
999 }
1000
1001 return;
1002
1003 ERROR:
1004 cm_Fail("Memory allocation error.");
1005 }
1006
1007
1008 /**************************************************************************
1009 * EPN 08.31.06
1010 * cm2sub_cm_emit_probs()
1011 *
1012 * Purpose: For a specific sub CM state v_s, determine the
1013 * emission probabilities using the emission probs
1014 * of state(s) (up to 2) in the original CM that
1015 * map to it. We weight the contribution of each
1016 * state to the emission probability by it's psi
1017 * value in orig_cm (psi[v] is expected number of
1018 * times state v is entered in a parse).
1019 *
1020 * Args:
1021 * CM_t *orig_cm - the original, template CM
1022 * CM_t *sub_cm - the sub CM
1023 * double *orig_psi - orig_psi[v] is the expected number of times state v is entered
1024 * in a CM parse
1025 * int v_s - the sub_cm state we're filling emissions for
1026 * int v_o1 - orig_cm state v_s maps to
1027 * int v_o2 - orig_cm state v_s maps to (-1 if v_s maps to only 1 state)
1028 * Returns: void
1029 */
1030 static void
1031 cm2sub_cm_emit_probs(CM_t *orig_cm, CM_t *sub_cm, double *orig_psi, int v_s, int v_o1, int v_o2,
1032 CMSubMap_t *submap)
1033 {
1034 int is_left;
1035 int i, j;
1036
1037 /*printf("\nin cm2sub_cm_emit_probs v_s: %d, v_o1: %d, v_o2: %d\n", v_s, v_o1, v_o2);*/
1038
1039 if(v_o1 == -1)
1040 {
1041 cm_Fail("ERROR in cm2sub_cm_emit_probs, sub_cm state %d maps to 0 states in orig_cm (spos: %d epos: %d)\n", v_s, submap->spos, submap->epos);
1042 }
1043
1044 if(sub_cm->sttype[v_s] == MP_st)
1045 {
1046 for(i = 0; i < (orig_cm->abc->K*orig_cm->abc->K); i++)
1047 sub_cm->e[v_s][i] = orig_cm->e[v_o1][i];
1048 return;
1049 }
1050
1051 if(submap->s2o_id[v_s] == TRUE)
1052 {
1053 /* must be a singlet emitter */
1054 for(i = 0; i < orig_cm->abc->K; i++)
1055 sub_cm->e[v_s][i] = orig_cm->e[v_o1][i];
1056 /* No FNorm's necessary (assuming the orig_cm is normalized), since we're
1057 * building a new CM for each sequence in --sub mode, we skip it for speed.
1058 */
1059 return;
1060 }
1061
1062 /* If we get here, v_s is a singlet emitter. */
1063
1064 /* There are two cases when two states can map to v_s.
1065 * Case 1: one of them is an MP_st,
1066 * Case 2: one is an IL_st and one is an IR_st (ambiguity in CM architecture)
1067 * These are the only cases where we need to weight emission probs by orig_psi values,
1068 * and subsequently only cases we need to call FNorm() for */
1069 if(orig_cm->sttype[v_o1] == MP_st)
1070 {
1071 if(orig_cm->sttype[v_o2] == ML_st)
1072 is_left = TRUE;
1073 else if(orig_cm->sttype[v_o2] == MR_st)
1074 is_left = FALSE;
1075 else
1076 cm_Fail("ERROR v_s: %d maps to a MP_st and another non-ML and non-MR state\n");
1077
1078 for(i = 0; i < orig_cm->abc->K; i++)
1079 if(is_left)
1080 for(j = (i*orig_cm->abc->K); j < ((i+1)*orig_cm->abc->K); j++)
1081 sub_cm->e[v_s][i] += orig_psi[v_o1] * orig_cm->e[v_o1][j];
1082 else
1083 for(j = i; j < (orig_cm->abc->K*orig_cm->abc->K); j+=orig_cm->abc->K)
1084 sub_cm->e[v_s][i] += orig_psi[v_o1] * orig_cm->e[v_o1][j];
1085 if(orig_cm->sttype[v_o2] == MP_st)
1086 cm_Fail("ERROR sub_cm state: %d maps to two MATP_MP states\n", v_s);
1087
1088 /*v_o2 must be ML or MR, which can all be handled identically */
1089 for(i = 0; i < orig_cm->abc->K; i++)
1090 sub_cm->e[v_s][i] += orig_psi[v_o2] * orig_cm->e[v_o2][i];
1091 esl_vec_FNorm(sub_cm->e[v_s], orig_cm->abc->K);
1092 return;
1093 }
1094 else if(v_o2 != -1)
1095 cm_Fail("ERROR sub_cm state: %d maps to two states (%d and %d), but neither is a MATP_MP\n", v_s, v_o1, v_o2);
1096
1097 /* If we get here, v_s maps to a single singlet emitter in orig_cm, v_o1 */
1098 for(i = 0; i < orig_cm->abc->K; i++)
1099 sub_cm->e[v_s][i] = orig_cm->e[v_o1][i];
1100
1101 return;
1102 }
1103
1104 /**************************************************************************
1105 * EPN 08.31.06
1106 * cm2sub_cm_trans_probs()
1107 *
1108 * Purpose: For a specific sub CM state v_s, determine the
1109 * transition probabilities going out of v_s,
1110 * using the psi values for the original template
1111 * CM (orig_cm) (psi[v] is expected number of
1112 * times state v is entered in a parse).
1113 * We fill the sub_cm->t[v_s] with 'virtual
1114 * counts', then we'll normalize to probabilities
1115 * later (outside this function).
1116 *
1117 * This is based on cp9_modelmaker.c::cm2hmm_trans_probs_cp9()
1118 * which was based on formulas/ideas in Zasha Weinberg's
1119 * thesis (p.123).
1120 *
1121 * Args:
1122 * CM_t *orig_cm - the original, template CM
1123 * CM_t *sub_cm - the sub CM
1124 * double *orig_psi - orig_psi[v] is the expected number of times state v is entered
1125 * in a CM parse
1126 * char ***tmap - the hard-coded transition map
1127 * int v_s - the sub_cm state we're filling transitions for
1128 * submap - the map from the sub CM to the template CM
1129 * Returns: void
1130 */
1131 static void
1132 cm2sub_cm_trans_probs(CM_t *orig_cm, CM_t *sub_cm, double *orig_psi, char ***tmap, int v_s, CMSubMap_t *submap)
1133 {
1134 int v_o;
1135 int yoffset;
1136 int y_s;
1137
1138 /*printf("in cm2sub_cm_trans_probs: v_s: %d\n", v_s);*/
1139
1140 if(submap->s2o_id[v_s] == TRUE) /* v_s is identical to submap->s2o_smap[v_s][0] */
1141 {
1142 v_o = submap->s2o_smap[v_s][0];
1143 for(yoffset = 0; yoffset < sub_cm->cnum[v_s]; yoffset++)
1144 {
1145 /* we can just copy the transitions */
1146 sub_cm->t[v_s][yoffset] = orig_psi[v_o] * orig_cm->t[v_o][yoffset];
1147 }
1148 return;
1149 }
1150
1151 /* start with the first orig_cm state that maps to v_s */
1152 v_o = submap->s2o_smap[v_s][0];
1153 /*printf("\tv_o: %d\n", v_o);*/
1154 if(v_o == -1)
1155 {
1156 if(sub_cm->sttype[v_s] != S_st &&
1157 sub_cm->sttype[v_s] != E_st &&
1158 sub_cm->sttype[v_s] != B_st)
1159 /* special cases, S_st, E_st, B_st */
1160 cm_Fail("ERROR, sub_cm state v_s: %d maps to no state in sub_cm, but it's not a B, E or S state\n", v_s);
1161 }
1162 else
1163 {
1164 if(sub_cm->sttype[v_s] == S_st ||
1165 sub_cm->sttype[v_s] == E_st ||
1166 sub_cm->sttype[v_s] == B_st)
1167 if(v_s != 0)
1168 cm_Fail("ERROR, sub_cm state v_s: %d is S, E or B but maps to a orig_cm state: v_o:%d\n", v_o);
1169
1170 for(yoffset = 0; yoffset < sub_cm->cnum[v_s]; yoffset++)
1171 {
1172 y_s = sub_cm->cfirst[v_s] + yoffset;
1173 if(sub_cm->sttype[(y_s+1)] != E_st) /* if y_s+1 is an E, y_s is a detached insert state, we want
1174 * it to be impossible to reach this guy, leave counts as 0.0 */
1175 {
1176 cm2sub_cm_add_single_trans(orig_cm, sub_cm, submap, v_o, submap->s2o_smap[y_s][0], v_s, yoffset, orig_psi, tmap);
1177 cm2sub_cm_add_single_trans(orig_cm, sub_cm, submap, v_o, submap->s2o_smap[y_s][1], v_s, yoffset, orig_psi, tmap);
1178 }
1179 }
1180 }
1181
1182 /* move on to the second orig_cm state that maps to v_s */
1183 v_o = submap->s2o_smap[v_s][1];
1184 if(v_o != -1)
1185 {
1186 for(yoffset = 0; yoffset < sub_cm->cnum[v_s]; yoffset++)
1187 {
1188 y_s = sub_cm->cfirst[v_s] + yoffset;
1189 if(sub_cm->sttype[(y_s+1)] != E_st) /* if y_s+1 is an E, y_s is a detached insert state, we want
1190 * it to be impossible to reach this guy, leave counts as 0.0 */
1191 {
1192 cm2sub_cm_add_single_trans(orig_cm, sub_cm, submap, v_o, submap->s2o_smap[y_s][0], v_s, yoffset, orig_psi, tmap);
1193 cm2sub_cm_add_single_trans(orig_cm, sub_cm, submap, v_o, submap->s2o_smap[y_s][1], v_s, yoffset, orig_psi, tmap);
1194 }
1195 }
1196 }
1197 return;
1198 }
1199
1200 /**************************************************************************
1201 * EPN 09.15.06
1202 * cm2sub_cm_trans_probs_S()
1203 *
1204 * Purpose: For a specific sub CM S state v_start, fill in virtual counts
1205 * for transitions out of v_s. We do this in its own seperate function
1206 * because we can't robustly map S states in a sub_cm to S states
1207 * in an orig CM (if its possible - I can't figure out how to do it).
1208 *
1209 * Args:
1210 * CM_t *orig_cm - the original, template CM
1211 * CM_t *sub_cm - the sub CM
1212 * double *orig_psi - orig_psi[v] is the expected number of times state v is entered
1213 * in a CM parse
1214 * char ***tmap - the hard-coded transition map
1215 * int v_start - the sub_cm start state we're filling virtual counts of transitions into
1216 * submap - the map from the sub CM to the template CM
1217 * Returns: void
1218 */
1219 static void
1220 cm2sub_cm_trans_probs_S(CM_t *orig_cm, CM_t *sub_cm, double *orig_psi, char ***tmap, int v_start, CMSubMap_t *submap)
1221 {
1222 int yoffset;
1223 int y_s;
1224
1225 int sub_nd;
1226
1227 int v_s_insert;
1228 int v_o_insert;
1229
1230 float sum;
1231 float il_psi;
1232 float temp_psi;
1233 float temp_psi_sum;
1234 float diff;
1235
1236 int orig_il1, orig_il2, orig_ir1, orig_ir2;
1237
1238 /*printf("in cm2sub_cm_trans_probs_S: v_start: %d\n", v_start);*/
1239
1240 sub_nd = sub_cm->ndidx[v_start];
1241
1242 if(sub_cm->ndtype[sub_nd] == BEGL_nd)
1243 {
1244 /* This is the easy case, we have transitions into each of the states in the split-set
1245 * of the next node. The only way to reach each of these states is from the BEGL
1246 * so we just weight each by the psi values for the matching orig_cm states.
1247 */
1248 if(sub_cm->ndtype[sub_nd + 1] == BIF_nd)
1249 sub_cm->t[v_start][0] = 1.0; /* BEGL_S -> BIF_B */
1250 else
1251 for(yoffset = 0; yoffset < sub_cm->cnum[v_start]; yoffset++)
1252 {
1253 y_s = sub_cm->cfirst[v_start] + yoffset;
1254 /*printf("updating sub_cm->t[%d][%d]\n", v_start, yoffset);*/
1255 sub_cm->t[v_start][yoffset] = orig_psi[submap->s2o_smap[y_s][0]];
1256 if(submap->s2o_smap[y_s][1] != -1)
1257 sub_cm->t[v_start][yoffset] += orig_psi[submap->s2o_smap[y_s][1]];
1258 }
1259 }
1260
1261 else if(sub_cm->ndtype[sub_nd] == BEGR_nd)
1262 {
1263 /* More complicated than the BEGL case b/c we need to handle the
1264 * BEGR_S -> BEGR_IL transition as well as BEGR_S -> next node
1265 * split set transitions.
1266 * We know the BEGR_IL -> BEGR_IL self transition though because
1267 * we were able to map BEGR_IL to 1 or 2 orig_cm states.
1268 */
1269
1270 v_s_insert = v_start + 1;
1271 if(sub_cm->ndtype[sub_nd + 1] == BIF_nd)
1272 {
1273 /*printf("!!!SPECIAL CASE BEGR -> BIF! v_ct\n");*/
1274
1275 v_o_insert = submap->s2o_smap[v_s_insert][0];
1276 if(submap->s2o_smap[v_s_insert][1] == -1)
1277 {
1278 diff = sub_cm->t[v_s_insert][0] - (orig_psi[v_o_insert] * orig_cm->t[v_o_insert][0]);
1279 if(diff >= 0. && diff > 0.0001)
1280 cm_Fail("ERROR, code for calc'ing BEGR_IL -> BIF_B is wrong, sub_cm->t[v_s_insert:%d][0] should be %f (based on your understanding) but its really %f\n", v_s_insert, (orig_psi[v_o_insert] * orig_cm->t[v_o_insert][0]), (sub_cm->t[v_s_insert][0]));
1281 if(diff <= 0. && diff < -0.0001)
1282 cm_Fail("ERROR, code for calc'ing BEGR_IL -> BIF_B is wrong, sub_cm->t[v_s_insert:%d][0] should be %f (based on your understanding) but its really %f\n", v_s_insert, (orig_psi[v_o_insert] * orig_cm->t[v_o_insert][0]), (sub_cm->t[v_s_insert][0]));
1283 }
1284
1285 /* BEGR_IL -> BEGR_IL will be the sum over possibly two orig_cm states v_o_insert that
1286 * map to sub_cm state BEGR_IL of:
1287 * orig_psi[v_o_insert] * orig_cm->t[v_o_insert][0],
1288 * so BEGR_IL -> BIF_B is calc'ed as follows */
1289 sub_cm->t[v_s_insert][1] = orig_psi[v_o_insert] * (1. - orig_cm->t[v_o_insert][0]);
1290 if(submap->s2o_smap[v_s_insert][1] != -1)
1291 {
1292 /* we have to factor in the other state as well. */
1293 cm_Fail("ERROR, BEGR_IL: %d maps to 2 orig_cm states, was hoping this was impossible - need to implement.\n", v_s_insert);
1294 v_o_insert = submap->s2o_smap[v_s_insert][1];
1295 sub_cm->t[v_s_insert][1] += orig_psi[v_o_insert] * (1. - orig_cm->t[v_o_insert][0]);
1296 }
1297 /* Now we normalize the transitions out of BEGR_IL, so we can calculate BEGR_S -> BEGR_IL */
1298 esl_vec_FNorm(sub_cm->t[v_s_insert], sub_cm->cnum[v_s_insert]);
1299 il_psi = orig_psi[submap->s2o_smap[v_s_insert][0]];
1300 if(submap->s2o_smap[v_s_insert][1] != -1)
1301 {
1302 cm_Fail("ERROR, BEGR_IL: %d maps to 2 orig_cm states, was hoping this was impossible - need to implement.\n", v_s_insert);
1303 il_psi+= orig_psi[submap->s2o_smap[v_s_insert][1]];
1304 }
1305 sub_cm->t[v_start][0] = (1. - sub_cm->t[v_s_insert][0]) * il_psi; /* set BEGR_S -> BEGR_IL */
1306 sub_cm->t[v_start][1] = 1. - sub_cm->t[v_start][0]; /* set BEGR_S -> BIF_B */
1307 }
1308
1309 else
1310 { /* next node is not a BIF node */
1311 /* First, normalize the probabilities out of the BEGR_IL, so
1312 * we can calculate what BEGR_S -> BEGR_IL should be (we need
1313 * to know BEGR_IL -> BEGR_IL probability and orig_psi of
1314 * the states that map to BEGR_IL to do this).
1315 */
1316 sum = 0.;
1317 esl_vec_FNorm(sub_cm->t[v_s_insert], sub_cm->cnum[v_s_insert]);
1318 il_psi = orig_psi[submap->s2o_smap[v_s_insert][0]];
1319 if(submap->s2o_smap[v_s_insert][1] != -1)
1320 {
1321 cm_Fail("ERROR, BEGR_IL: %d maps to 2 orig_cm states, was hoping this was impossible - need to implement.\n", v_s_insert);
1322 il_psi += orig_psi[submap->s2o_smap[v_s_insert][1]];
1323 }
1324 sub_cm->t[v_start][0] = (1. - sub_cm->t[v_s_insert][0]) * il_psi; /* set BEGR_S -> BEGR_IL */
1325 sum = sub_cm->t[v_start][0];
1326
1327 for(yoffset = 1; yoffset < sub_cm->cnum[v_start]; yoffset++) /* note we start at yoffset = 1
1328 * BEGR_S -> first state of next
1329 * node's split set. */
1330 {
1331 y_s = sub_cm->cfirst[v_start] + yoffset;
1332 temp_psi = orig_psi[submap->s2o_smap[y_s][0]];
1333 if(submap->s2o_smap[y_s][1] != -1)
1334 temp_psi += orig_psi[submap->s2o_smap[y_s][1]];
1335
1336 sub_cm->t[v_start][yoffset] = temp_psi - il_psi * sub_cm->t[v_s_insert][yoffset];
1337 sum += sub_cm->t[v_start][yoffset];
1338 }
1339 /*printf("BEGR->NON BIF SUM: %f\n", sum);*/
1340 if(sum < 1.0 && ((1.0 - sum) > 0.001))
1341 cm_Fail("ERROR calculating transitions out of BEGR_S incorrectly\n");
1342 if(sum > 1.0 && ((sum - 1.0) > 0.001))
1343 cm_Fail("ERROR calculating transitions out of BEGR_S incorrectly\n");
1344 }
1345 }
1346 else if(sub_cm->ndtype[sub_nd] == ROOT_nd)
1347 {
1348 /*printf("in cm2sub_cm_trans_probs_S(), ROOT_nd\n");*/
1349 /* the only case we have to worry about is if the next node is BIF node,
1350 * otherwise the transitions out of ROOT_S have already been set.
1351 */
1352 if(sub_cm->ndtype[sub_nd + 1] == BIF_nd)
1353 {
1354 /*printf("!!!SPECIAL CASE ROOT -> BIF!\n");*/
1355 /* Before we do anything we have to check to see if we need to subtract
1356 * any subpaths from ROOT_S -> ROOT_IR that have been double counted:
1357 */
1358 /* if orig_ir < orig_il, we've counted paths from
1359 * ir -> il correctly for ROOT_IL -> ROOT_IR and
1360 * incorrectly for ROOT_S -> ROOT_IR
1361 */
1362
1363 orig_il1 = submap->s2o_smap[1][0];
1364 orig_il2 = submap->s2o_smap[1][1];
1365
1366 orig_ir1 = submap->s2o_smap[2][0];
1367 orig_ir2 = submap->s2o_smap[2][1];
1368
1369 if(orig_ir1 < orig_il1)
1370 {
1371 sub_cm->t[0][1] -= orig_psi[orig_ir1] *
1372 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_ir1, orig_il1, 0, tmap, orig_psi);
1373 }
1374 if(orig_ir2 != -1 && orig_ir2 < orig_il1)
1375 {
1376 sub_cm->t[0][1] -= orig_psi[orig_ir2] *
1377 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_ir2, orig_il1, 0, tmap, orig_psi);
1378 }
1379 if(orig_il2 != -1 && orig_ir1 < orig_il2)
1380 {
1381 sub_cm->t[0][1] -= orig_psi[orig_ir1] *
1382 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_ir1, orig_il2, 0, tmap, orig_psi);
1383 }
1384 if((orig_ir2 != -1 && orig_il2 != -1) && (orig_ir2 < orig_il2))
1385 {
1386 sub_cm->t[0][1] -= orig_psi[orig_ir2] *
1387 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_ir2, orig_il2, 0, tmap, orig_psi);
1388 }
1389
1390 /* Next, set transition from ROOT_S -> BIF_B, we know that
1391 * ROOT_S -> ROOT_IL and ROOT_S -> ROOT_IR were set using
1392 * orig_cm subpaths originating at ROOT_S, so we know that
1393 * the virtual counts out of ROOT_S should NOT be scaled, in
1394 * other words, they can be treated as probabilities and should
1395 * sum to 1.0.
1396 */
1397 sub_cm->t[0][2] = 1.0 - (sub_cm->t[0][0] + sub_cm->t[0][1]); /* set ROOT_S->BIF_B */
1398
1399 v_s_insert = v_start + 1; /* ROOT_IL */
1400 v_o_insert = submap->s2o_smap[v_s_insert][0];
1401 if(submap->s2o_smap[v_s_insert][1] == -1)
1402 {
1403 diff = sub_cm->t[v_s_insert][0] - (orig_psi[v_o_insert] * orig_cm->t[v_o_insert][0]);
1404 if(diff >= 0. && diff >= 0.0001)
1405 cm_Fail("ERROR, code for calc'ing ROOT_IL -> BIF_B is wrong, sub_cm->t[v_s_insert:%d][0] should be %f (based on your understanding) but its really %f\n", v_s_insert, (orig_psi[v_o_insert] * orig_cm->t[v_o_insert][0]), (sub_cm->t[v_s_insert][0]));
1406 if(diff <= 0. && diff <= -0.0001)
1407 cm_Fail("ERROR, code for calc'ing ROOT_IL -> BIF_B is wrong, sub_cm->t[v_s_insert:%d][0] should be %f (based on your understanding) but its really %f\n", v_s_insert, (orig_psi[v_o_insert] * orig_cm->t[v_o_insert][0]), (sub_cm->t[v_s_insert][0]));
1408 }
1409 /* We want to figure out what the virtual counts for the traansition ROOT_IL -> BIF_B should
1410 * be, but this depends on how we filled the virtual counts for the other two
1411 * transitions (-> ROOT_IL and -> ROOT_IR) out of ROOT_IL, so we SHOULD revisit
1412 * their calculation. Here, I'm attempting a trick that SHOULD work: figuring out
1413 * what the self_insert probability IL->IL should be based on the self insert probabilities
1414 * of the up to 2 orig_cm states that sub_cm ROOT_IL maps to, then using that scaling
1415 * factor (the actual probability / the virtual counts currently in sub_cm->t[ROOT_IL][0])
1416 * to scale both ROOT_IL->ROOT_IL and ROOT_IL->ROOT_IR, then we can just set ROOT_IL->BIF
1417 * as 1.0 - (ROOT_IL->ROOT_IL + ROOT_IL->ROOT_IR).
1418 */
1419
1420 temp_psi_sum = orig_psi[v_o_insert];
1421
1422 if(submap->s2o_smap[v_s_insert][1] != -1)
1423 {
1424 v_o_insert = submap->s2o_smap[v_s_insert][1];
1425 temp_psi_sum += orig_psi[v_o_insert];
1426 }
1427 sub_cm->t[v_s_insert][0] /= temp_psi_sum; /* ROOT_IL -> ROOT_IL */
1428 sub_cm->t[v_s_insert][1] /= temp_psi_sum; /* ROOT_IL -> ROOT_IR */
1429 sub_cm->t[v_s_insert][2] = 1. - (sub_cm->t[v_s_insert][0] + sub_cm->t[v_s_insert][1]);
1430 /* ROOT_IL -> BIF_B */
1431
1432 /* move on to calc'ing ROOT_IR -> BIF */
1433 v_s_insert = v_start + 2; /* ROOT_IR */
1434 v_o_insert = submap->s2o_smap[v_s_insert][0];
1435 if(submap->s2o_smap[v_s_insert][1] == -1)
1436 {
1437 diff = sub_cm->t[v_s_insert][0] - (orig_psi[v_o_insert] * orig_cm->t[v_o_insert][0]);
1438 if(diff >= 0. && diff > 0.0001)
1439 cm_Fail("ERROR, code for calc'ing ROOT_IR -> BIF_B is wrong, sub_cm->t[v_s_insert:%d][0] should be %f (based on your understanding) but its really %f\n", v_s_insert, (orig_psi[v_o_insert] * orig_cm->t[v_o_insert][0]), (sub_cm->t[v_s_insert][0]));
1440 if(diff <= 0. && diff < -0.0001)
1441 cm_Fail("ERROR, code for calc'ing ROOT_IR -> BIF_B is wrong, sub_cm->t[v_s_insert:%d][0] should be %f (based on your understanding) but its really %f\n", v_s_insert, (orig_psi[v_o_insert] * orig_cm->t[v_o_insert][0]), (sub_cm->t[v_s_insert][0]));
1442 }
1443 /* We can calculate what the ROOT_IR -> ROOT_IR probability should be, and then
1444 * just take 1.0 minus that probability to set ROOT_IR -> BIF.
1445 */
1446 temp_psi_sum = orig_psi[v_o_insert];
1447
1448 if(submap->s2o_smap[v_s_insert][1] != -1)
1449 {
1450 v_o_insert = submap->s2o_smap[v_s_insert][1];
1451 temp_psi_sum += orig_psi[v_o_insert];
1452 }
1453 sub_cm->t[v_s_insert][0] /= temp_psi_sum; /* ROOT_IR -> ROOT_IR */
1454 sub_cm->t[v_s_insert][1] = 1. - (sub_cm->t[v_s_insert][0]);
1455 /* ROOT_IR -> BIF_B */
1456 }
1457 else
1458 {
1459 /* ROOT -> non-BIF node, we have already handled this, so we return. */
1460 }
1461 }
1462 /*printf("leaving cm2sub_cm_trans_probs_S\n\n");*/
1463 return;
1464 }
1465
1466 /**************************************************************************
1467 * EPN 09.21.06
1468 * cm2sub_cm_trans_probs_B_E()
1469 *
1470 * Purpose: For a specific sub CM B or E state v_be fill in virtual counts
1471 * for transitions into v_be. We do this in its own seperate function,
1472 * because we can't robustly map E states in a sub_cm to E states
1473 * in an orig CM (if its possible - I can't figure out how to do it).
1474 *
1475 * Args:
1476 * CM_t *orig_cm - the original, template CM
1477 * CM_t *sub_cm - the sub CM
1478 * double *orig_psi - orig_psi[v] is the expected number of times state v is entered
1479 * in a CM parse
1480 * char ***tmap - the hard-coded transition map
1481 * int v_be - the sub_cm END state we're filling virtual counts of transitions into
1482 * submap - the map from the sub CM to the template CM
1483 * print_flag - TRUE to print useful debugging info
1484 * Returns: void
1485 */
1486 static void
1487 cm2sub_cm_trans_probs_B_E(CM_t *orig_cm, CM_t *sub_cm, double *orig_psi, char ***tmap, int v_be, CMSubMap_t *submap,
1488 int print_flag)
1489 {
1490 int orig_v;
1491 int sub_v;
1492
1493 int sub_nd;
1494 int psub_nd;
1495 int sub_i;
1496 int bif_end_yoffset;
1497 int orig_v1, orig_v2, orig_i;
1498 float contribution;
1499 int orig_il, orig_ir, sub_il, sub_ir;
1500 int into_end_flag;
1501
1502 if(print_flag) printf("in cm2sub_cm_trans_probs_B_E: sub_v: %d\n", v_be);
1503
1504 sub_nd = sub_cm->ndidx[v_be];
1505 psub_nd = sub_nd - 1;
1506
1507 into_end_flag = FALSE;
1508 if(sub_cm->sttype[v_be] == E_st)
1509 into_end_flag = TRUE;
1510 /* We're moving from into an END, so the END_E - 1 state is a detached insert,
1511 * and we have to handle this in a special way */
1512
1513 switch (sub_cm->ndtype[psub_nd]) {
1514 case MATP_nd:
1515 if(print_flag) printf("prev node type MATP\n");
1516 /* psub_nd is a MATP: each state in psub_nd transits to
1517 * exactly 3 states, the MATP_IL, MATP_IR and the BIF or END state v_be, for which
1518 * we are trying to fill virtual transition counts into.
1519 *
1520 * The case for MATP_nd's is actually simpler than for other nodes because we
1521 * can exploit the fact that if a MATP node exists in the sub_cm it necessarily
1522 * must map to a MATP node in the original CM (we're not adding or changing any
1523 * base pairs, just deleting some possibly), so every sub MATP state must correspond
1524 * to exactly 1 original MATP state.
1525 */
1526
1527 bif_end_yoffset = 2; /* cm->t[][2] goes to BIF_B or END_E */
1528 sub_il = sub_cm->nodemap[psub_nd] + 4;
1529 sub_ir = sub_cm->nodemap[psub_nd] + 5;
1530
1531 orig_il = submap->s2o_smap[sub_il][0];
1532 orig_ir = submap->s2o_smap[sub_ir][0];
1533 if(into_end_flag && orig_ir != -1)
1534 cm_Fail("ERROR in cm2sub_cm_trans_probs_B_E(), into_end_flag is TRUE but MATP_IR maps to a orig_cm state.\n");
1535 if(orig_ir == -1 && !into_end_flag)
1536 cm_Fail("ERROR in cm2sub_cm_trans_probs_B_E(), into_end_flag is FALSE but MATP_IR doesn't map to a orig_cm state.\n");
1537
1538 for(sub_v = sub_cm->nodemap[psub_nd]; sub_v < sub_il; sub_v++)
1539 {
1540 orig_v = submap->s2o_smap[sub_v][0]; /* submap->s2o_smap[sub_v][1] will nec. be -1 in a MATP */
1541 sub_cm->t[sub_v][bif_end_yoffset] = orig_psi[orig_v] -
1542 (sub_cm->t[sub_v][0] + sub_cm->t[sub_v][1]); /* if into_end_flag is TRUE, sub_cm->t[sub_v][1] is 0. */
1543 }
1544 /* now do MATP_IL and MATP_IR */
1545 sub_cm->t[sub_il][bif_end_yoffset] = orig_psi[orig_il] -
1546 (sub_cm->t[sub_il][0] + sub_cm->t[sub_il][1]);
1547
1548 bif_end_yoffset = 1;
1549 if(into_end_flag)
1550 sub_cm->t[sub_ir][bif_end_yoffset] = 1.0;
1551 else
1552 sub_cm->t[sub_ir][bif_end_yoffset] = orig_psi[orig_ir] - sub_cm->t[sub_ir][0];
1553 break;
1554
1555 case MATL_nd:
1556 case MATR_nd:
1557 if(print_flag) printf("prev node type MATL or MATR\n");
1558 /* psub_nd is a MATL or MATR and we know each state in psub_nd transits to
1559 * exactly 2 states, the insert state of psub_nd and the BIF or END state v_be, which
1560 * we are trying to fill in transitions to.
1561 */
1562 bif_end_yoffset = 1; /* cm->t[][1] goes to BIF_B or END_E */
1563 sub_i = sub_cm->nodemap[psub_nd] + 2; /* sub_i is MATL_IL or MATR_IR */
1564 orig_i = submap->s2o_smap[sub_i][0];
1565 if(into_end_flag && orig_i != -1)
1566 cm_Fail("ERROR in cm2sub_cm_trans_probs_B_E(), into_end_flag is TRUE but MAT*_I* maps to a orig_cm state.\n");
1567
1568 for(sub_v = sub_cm->nodemap[psub_nd]; sub_v < sub_i; sub_v++)
1569 {
1570 orig_v1 = submap->s2o_smap[sub_v][0];
1571 orig_v2 = submap->s2o_smap[sub_v][1];
1572 if(into_end_flag)
1573 sub_cm->t[sub_v][bif_end_yoffset] = 1.0;
1574 else
1575 {
1576 if(orig_v1 < orig_i)
1577 {
1578 contribution = cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap,
1579 orig_v1, orig_i, sub_v, tmap, orig_psi);
1580 sub_cm->t[sub_v][bif_end_yoffset] = orig_psi[orig_v1] * (1. - contribution);
1581 if(print_flag) printf("curr v1 < i1 sub_cm->t[sub_v:%d][1] now: %f (added: psi:%f * 1-cont: %f (%f))\n", sub_v, sub_cm->t[sub_v][1], orig_psi[orig_v1], (1.-contribution), (orig_psi[orig_v1] * (1. - contribution)));
1582 }
1583 else
1584 {
1585 contribution = orig_psi[orig_i] * cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap,
1586 orig_i, orig_v1, sub_v, tmap, orig_psi);
1587 sub_cm->t[sub_v][bif_end_yoffset] = orig_psi[orig_v1] * (1. - (contribution / orig_psi[orig_v1]));
1588 if(print_flag) printf("curr i1 < v1 sub_cm->t[sub_v:%d][1] now: %f (added: psi:%f * 1-cont: %f (%f))\n", sub_v, sub_cm->t[sub_v][1], orig_psi[orig_v1], (1.-contribution), (orig_psi[orig_v1] * (1. - contribution)));
1589 }
1590 if(orig_v2 != -1)
1591 {
1592 if(orig_v2 < orig_i)
1593 {
1594 contribution = cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap,
1595 orig_v2, orig_i, sub_v, tmap, orig_psi);
1596 sub_cm->t[sub_v][bif_end_yoffset] += orig_psi[orig_v2] * (1. - contribution);
1597 if(print_flag) printf("curr v2 < i1 sub_cm->t[sub_v:%d][1] now: %f (added: psi:%f * 1-cont: %f (%f))\n", sub_v, sub_cm->t[sub_v][1], orig_psi[orig_v2], (1.-contribution), (orig_psi[orig_v2] * (1. - contribution)));
1598 }
1599 else
1600 {
1601 contribution = orig_psi[orig_i] * cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap,
1602 orig_i, orig_v2, sub_v, tmap, orig_psi);
1603 sub_cm->t[sub_v][bif_end_yoffset] += orig_psi[orig_v2] * (1. - (contribution / orig_psi[orig_v2]));
1604 if(print_flag) printf("curr i1 < v2 sub_cm->t[sub_v:%d][1] now: %f (added: psi:%f * 1-cont: %f (%f))\n", sub_v, sub_cm->t[sub_v][1], orig_psi[orig_v2], (1.-contribution), (orig_psi[orig_v2] * (1. - contribution)));
1605 }
1606 }
1607 }
1608 }
1609 /* now set the sub_i -> B or E transition prob */
1610 if(into_end_flag) /* The transition probability out of the MAT{L,R}_I{L,R} is irrelevant,
1611 * because the state is detached. */
1612 sub_cm->t[sub_i][1] = 1.;
1613 else
1614 sub_cm->t[sub_i][1] = orig_psi[orig_i] *
1615 (1. - cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap,
1616 orig_i, orig_i, sub_i,tmap, orig_psi));
1617 break;
1618
1619 case ROOT_nd:
1620 case BEGL_nd:
1621 case BEGR_nd:
1622 { /* we handle these cases in cm2sub_cm_trans_probs_S() */ }
1623 break;
1624
1625 default:
1626 cm_Fail("ERROR bogus node type transiting to END or BIF\n");
1627 break;
1628 }
1629
1630 if(print_flag) printf("Returning from cm2sub_cm_trans_probs_B_E\n");
1631 return;
1632 }
1633
1634 /**************************************************************************
1635 * EPN 08.31.06
1636 * Function: cm2sub_cm_add_single_trans()
1637 *
1638 * Purpose: Add a virtual counts contribution to a single CM transition.
1639 *
1640 * See related functions for explanation of parameters.
1641 * Returns: (void)
1642 */
1643 static void
1644 cm2sub_cm_add_single_trans(CM_t *orig_cm, CM_t *sub_cm, CMSubMap_t *submap, int orig_v, int orig_y,
1645 int sub_v, int yoffset, double *orig_psi, char ***tmap)
1646 {
1647 int start;
1648 /* check if we've got real CM state ids */
1649 if(orig_v == -1 || orig_y == -1)
1650 return;
1651 start = orig_v;
1652 if(orig_y < start)
1653 start = orig_y;
1654 sub_cm->t[sub_v][yoffset] += orig_psi[start] *
1655 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_v, orig_y, sub_v, tmap, orig_psi);
1656 return;
1657 }
1658
1659 /**************************************************************************
1660 * EPN 08.31.06
1661 * Function: cm2sub_cm_sum_subpaths()
1662 *
1663 * Purpose: Calculate probability of getting from one state (start) to
1664 * another (end) in a CM, taking special considerations involving
1665 * insert states.
1666 *
1667 * This function is similar to CP9_cm2wrhmm::cm_sum_subpaths_cp9()
1668 * but was written for mapping transitions in a template CM (orig_cm)
1669 * to those in a sub CM built from that template. The sub_cm conversion
1670 * process is more complex and this function requires more functionality
1671 * than the *_cp9() version.
1672 *
1673 * When getting a transition probability for the sub_cm state 'v' in node
1674 * 'n', we ignore the contribution of subpaths that correspond to other
1675 * transitions out of states in 'n'.
1676 * For example, we don't want to include the probability of an
1677 * IL (nd 'n') -> MATL_ML (nd 'n'+1) sub parse when calculating
1678 * the transition probability for MATL_ML (nd 'n') -> MATL_ML (nd 'n'+1)
1679 * because the IL (nd 'n') -> MATL_ML (nd 'n'+1) exists and should
1680 * contain that probability mass. This is an easy example to skip,
1681 * there are several other instances where it's more complex to
1682 * ignore subpaths involving inserts like this.
1683 *
1684 * Importantly, this function does not correctly calculate the transition
1685 * virtual counts for only the states in the ROOT_nd. This is because
1686 * the ROOT_nd has 2 insert states, which makes it much more complex to
1687 * properly ignore subparses involving both these inserts. The
1688 * cm2sub_cm_subtract_root_subpaths() function corrects the counts for
1689 * the ROOT states. MATP_nd's also have 2 insert states but when constructing
1690 * sub_cm's the only MATP_nd's that exist have an exact mapping MATP_nd in
1691 * the orig_cm, which makes them easier to handle.
1692 *
1693 * In some cases, this function calls itself to determine the probabilities
1694 * of subparses that it should ignore. It is for these cases that it's
1695 * necessary to have the init_sub_start parameter passed in, which is
1696 * the sub_cm state the initial call (non-recursive call) of this function
1697 * was calculating transitions out of.
1698 *
1699 * Args:
1700 * CM_t *orig_cm - the original, template CM
1701 * CM_t *sub_cm - the sub CM
1702 * int orig_v - orig_cm state that maps to sub_v (1 of potentially 2)
1703 * int orig_y - orig_cm state that maps to sub_y (1 of potentially 2)
1704 * int sub_v - sub_cm state; we're calc'ing transitions out of sub_v
1705 * int sub_y - sub_cm state; we're calc'ing transitions into sub_y
1706 * int init_sub_start - the sub_cm state the initial (non-recursive call) had as sub_v.
1707 * double *orig_psi - for orig_cm: orig_psi[v] is the expected number of times state v
1708 * is entered in a CM parse
1709 *
1710 * Returns: FLOAT, the summed probability of all subpaths through the CM
1711 * starting at "start" and ending at "end" that don't pass through
1712 * orig_cm inserts that map to sub_cm insert states in the same node as init_sub_start.
1713 */
1714 static float
1715 cm2sub_cm_sum_subpaths(CM_t *orig_cm, CM_t *sub_cm, CMSubMap_t *submap, int orig_v, int orig_y,
1716 int init_sub_start, char ***tmap, double *orig_psi)
1717 {
1718 int status;
1719 int v,x,y; /* state indices in the orig_cm */
1720 int start, end; /* min(orig_v, orig_y) and max(orig_v, orig_y) respectively */
1721 float to_return; /* the probability mass we're returning */
1722 char tmap_val; /* a value in the hard-coded tmap */
1723 int is_insert; /* TRUE if v is insert, FALSE if not */
1724 float insert_to_start; /* prob mass going into start that we must ignore (see code)*/
1725 float end_to_insert; /* prob mass going out of end that we must ignore (see code)*/
1726 int skip_flag; /* TRUE to skip state v's contribution (see code) */
1727 int init_sub_nd; /* sub_cm node containing init_sub_start */
1728 int sub_insert1; /* a sub_cm insert state in sub_cm node init_sub_nd, or -1 */
1729 int sub_insert2; /* a sub_cm insert state in sub_cm node init_sub_nd, or -1 */
1730 int orig_insert1; /* the orig_cm state that map to sub_insert1, or -1 */
1731 int orig_insert2; /* the orig_cm state that map to sub_insert2, or -1 */
1732 float self_loop_factor;/* for dealing with self-insert loops */
1733 int sub_start1; /* a sub_cm state that maps to orig_cm's 'start' */
1734 int sub_start2; /* a sub_cm state that maps to orig_cm's 'start' or -1 */
1735 int sub_end1; /* a sub_cm state that maps to orig_cm's 'end' */
1736 int sub_end2; /* a sub_cm state that maps to orig_cm's 'end' or -1 */
1737 double *sub_psi; /* sub_psi[v] is the expected number of times state v is *
1738 * entered given we started at state "start", and we *
1739 * didn't go through orig_insert1 or orig_insert2 */
1740
1741 if(orig_v == -1 || orig_y == -1)
1742 return 0.;
1743
1744 start = orig_v;
1745 end = orig_y;
1746 if(start > end)
1747 { start = orig_y; end = orig_v; }
1748 if(start == end)
1749 return orig_cm->t[start][0]; /* return self-insert probability */
1750
1751 /*printf("\nin cm2sub_cm_sum_subpaths2: start: %d | end: %d\n", start, end);*/
1752
1753 ESL_ALLOC(sub_psi, sizeof(double) * (end - start + 1));
1754 sub_psi[0] = 1.; /* Initialize sub_psi[0]. We have to start in "start" */
1755
1756 /* First we store some useful information for later in the function,
1757 * we do this and use descriptive variable names to make the code
1758 * later easier to follow. */
1759
1760 /* Determine the 1 or 2 sub_cm states that map to
1761 * the orig_cm state start and end, and store these
1762 * in sub_start{1,2} and sub_end{1,2} respectively. */
1763 sub_start1 = submap->o2s_smap[start][0];
1764 sub_start2 = submap->o2s_smap[start][1];
1765 sub_end1 = submap->o2s_smap[end ][0];
1766 sub_end2 = submap->o2s_smap[end ][1];
1767
1768 /* Determine the 0, 1, or 2 sub_cm insert states in the same
1769 * node as init_sub_start, store these in sub_insert1 and
1770 * sub_insert2.
1771 */
1772 init_sub_nd = sub_cm->ndidx[init_sub_start];
1773 orig_insert1 = orig_insert2 = sub_insert1 = sub_insert2 = -1;
1774 if(sub_cm->ndtype[init_sub_nd] == MATP_nd)
1775 {
1776 sub_insert1 = sub_cm->nodemap[init_sub_nd] + 4; /* MATP_IL */
1777 sub_insert2 = sub_cm->nodemap[init_sub_nd] + 5; /* MATP_IR */
1778 }
1779 else if(sub_cm->ndtype[init_sub_nd] == ROOT_nd)
1780 {
1781 sub_insert1 = 1; /* ROOT_IL */
1782 sub_insert2 = 2; /* ROOT_IR */
1783 }
1784 else if(sub_cm->ndtype[init_sub_nd] == MATL_nd ||
1785 sub_cm->ndtype[init_sub_nd] == MATR_nd ||
1786 sub_cm->ndtype[init_sub_nd] == BEGR_nd)
1787 sub_insert1 = sub_cm->cfirst[init_sub_start]; /* MAT{L,R}_I{L,R} or BEGR_IL */
1788
1789 /* Set orig_insert{1,2} as the orig_cm states that map to sub_insert{1,2}. */
1790 if(sub_insert1 != -1) orig_insert1 = submap->s2o_smap[sub_insert1][0];
1791 if(sub_insert2 != -1) orig_insert2 = submap->s2o_smap[sub_insert2][0];
1792
1793 /* Step through states between start and end, keeping track of prob mass. */
1794 for (v = (start+1); v <= end; v++)
1795 {
1796 sub_psi[v-start] = 0.; /* initialize */
1797 is_insert = FALSE;
1798 if(orig_cm->sttype[v] == IL_st || orig_cm->sttype[v] == IR_st)
1799 is_insert = TRUE;
1800 if(orig_cm->sttype[v] == S_st)
1801 {
1802 /* Previous state is either a BIF_B or a END_E, and there's no transitions
1803 * FROM previous state to this state, so we handle this in a special way.*/
1804 sub_psi[v-start] = sub_psi[(v-1)-start];
1805 }
1806 /* Determine if we should skip the contribution of state v because
1807 * it will be correctly counted in a subsequent call of this function
1808 * for a different sub_cm transition. We want to do this if:
1809 * (1) v is not end
1810 * (2) v is an orig_cm insert state (orig_insert{1,2}) that maps
1811 * to a sub_cm insert state (sub_insert{1,2}) in the same node
1812 * as init_sub_start.
1813 * (3) sub_insert{1,2} is equal to or downstream of init_sub_start
1814 * (4) a sub_cm transition exists between the sub_insert{1,2} and
1815 * a sub_cm state that maps to orig_cm start (sub_start{1,2})
1816 * or end (sub_end{1,2}).
1817 */
1818 skip_flag = FALSE;
1819 if (v != end && v == orig_insert1 && sub_insert1 >= init_sub_start)
1820 {
1821 if(cm_trans_check(sub_cm, sub_insert1, sub_end1 ) ||
1822 cm_trans_check(sub_cm, sub_insert1, sub_end2 ) ||
1823 cm_trans_check(sub_cm, sub_insert1, sub_start1) ||
1824 cm_trans_check(sub_cm, sub_insert1, sub_start2))
1825 skip_flag = TRUE;
1826 }
1827 else if (v != end && v == orig_insert2 && sub_insert2 >= init_sub_start)
1828 {
1829 if(cm_trans_check(sub_cm, sub_insert2, sub_end1 ) ||
1830 cm_trans_check(sub_cm, sub_insert2, sub_end2 ) ||
1831 cm_trans_check(sub_cm, sub_insert2, sub_start1) ||
1832 cm_trans_check(sub_cm, sub_insert2, sub_start2))
1833 skip_flag = TRUE;
1834 }
1835 if(!skip_flag)
1836 {
1837 for (y = orig_cm->pnum[v]-1; y >= is_insert; y--)
1838 {
1839 x = orig_cm->plast[v] - y;
1840 /* x is a parent of v, we're adding contribution of a transition from x to v. */
1841 tmap_val = tmap[(int) orig_cm->stid[x]][(int) orig_cm->ndtype[orig_cm->ndidx[v]+is_insert]][(int) orig_cm->stid[v]];
1842 /* assert(tmap_val != -1); */
1843 if((x - start) < 0) sub_psi[v-start] += 0.;
1844 else sub_psi[v-start] += sub_psi[x-start] * orig_cm->t[x][(int) tmap_val];
1845 }
1846 if(v != end && is_insert) /* if v is end, we don't include the self loop contribution */
1847 sub_psi[v-start] += sub_psi[v-start] *
1848 (orig_cm->t[v][0] / (1-orig_cm->t[v][0])); /* else we include the self loop contribution */
1849 }
1850 }
1851 to_return = sub_psi[end-start];
1852
1853 /* If start and end are both not inserts, we need to ignore some of the
1854 * probability mass that comes INTO 'start' and goes OUT OF 'end'.
1855 * Specifically we need to ignore the probability mass that
1856 * is accounted for by transitions either TO OR FROM the sub_cm insert
1857 * states in the same sub_cm node as init_sub_start (sub_insert{1,2}).
1858 * This code block is related to the block with the (for(v = start+1..) block
1859 * above involving the 'skip_flag' which skips states accounted for by
1860 * the sub_cm insert states sub_insert{1,2} between start..end, this
1861 * block handles the case where the orig_cm insert states that map
1862 * to sub_insert{1,2}, namely orig_insert{1,2}, fall outside start..end.
1863 */
1864 insert_to_start = 0.;
1865 end_to_insert = 0.;
1866
1867 if((orig_cm->sttype[start] != IL_st && orig_cm->sttype[start] != IR_st) &&
1868 (orig_cm->sttype[end] != IL_st && orig_cm->sttype[end] != IR_st))
1869 {
1870 if(orig_insert1 != -1 && orig_insert1 < start)
1871 {
1872 if(orig_cm->sttype[start] == IL_st || orig_cm->sttype[start] == IR_st)
1873 self_loop_factor = (1. + (orig_cm->t[start][0] / (1. - orig_cm->t[start][0])));
1874 else
1875 self_loop_factor = 1.0;
1876 insert_to_start += self_loop_factor * orig_psi[orig_insert1] *
1877 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_insert1, start, init_sub_start, tmap, orig_psi);
1878 }
1879 if(orig_insert1 != -1 && orig_insert1 > end)
1880 end_to_insert += cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, end, orig_insert1, init_sub_start, tmap, orig_psi);
1881
1882 if(orig_insert2 != -1 && orig_insert2 < start)
1883 {
1884 self_loop_factor = 1.0;
1885 if(orig_cm->sttype[start] == IL_st ||
1886 orig_cm->sttype[start] == IR_st)
1887 self_loop_factor = (1. + (orig_cm->t[start][0] / (1. - orig_cm->t[start][0])));
1888 insert_to_start += self_loop_factor * orig_psi[orig_insert2] * cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_insert2, start, init_sub_start, tmap, orig_psi);
1889 }
1890 if(orig_insert2 != -1 && orig_insert2 > end)
1891 end_to_insert += cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, end, orig_insert2, init_sub_start, tmap, orig_psi);
1892
1893 /*printf("\t\tinsert_to_start: %f sub_psi[0]: %f\n", insert_to_start, sub_psi[0]);
1894 printf("\t\tend_to_insert: %f sub_psi[end-start]: %f\n", end_to_insert, sub_psi[(end-start)]);*/
1895 }
1896 to_return *= (1. - (insert_to_start / orig_psi[start]));
1897 to_return *= (1. - end_to_insert);
1898 /*printf("***returning from cm2sub_cm_sum_subpaths (s: %d | e: %d): %f\n", start, end, to_return);*/
1899
1900 free(sub_psi);
1901 return (float) to_return;
1902
1903 ERROR:
1904 cm_Fail("Memory allocation error.");
1905 return 0.; /* never reached */
1906 }
1907
1908 /**************************************************************************
1909 * EPN 09.01.06
1910 * debug_print_cm_params()
1911 *
1912 * Purpose: Print out emission and transition probabilities and scores
1913 * for a CM.
1914 *
1915 * Args:
1916 * fp stdout often
1917 * CM_t *cm
1918 * Returns: (void)
1919 */
1920 void
1921 debug_print_cm_params(FILE *fp, CM_t *cm)
1922 {
1923 int status;
1924 int v, i;
1925 int yoffset;
1926
1927 char **nodetypes;
1928 char **sttypes;
1929
1930 ESL_ALLOC(nodetypes, sizeof(char *) * 8);
1931 nodetypes[0] = "BIF";
1932 nodetypes[1] = "MATP";
1933 nodetypes[2] = "MATL";
1934 nodetypes[3] = "MATR";
1935 nodetypes[4] = "BEGL";
1936 nodetypes[5] = "BEGR";
1937 nodetypes[6] = "ROOT";
1938 nodetypes[7] = "END";
1939
1940 ESL_ALLOC(sttypes, sizeof(char *) * 10);
1941 sttypes[0] = "D";
1942 sttypes[1] = "MP";
1943 sttypes[2] = "ML";
1944 sttypes[3] = "MR";
1945 sttypes[4] = "IL";
1946 sttypes[5] = "IR";
1947 sttypes[6] = "S";
1948 sttypes[7] = "E";
1949 sttypes[8] = "B";
1950 sttypes[9] = "EL";
1951
1952 fprintf(fp, "cm->nodes: %d\n", cm->nodes);
1953 fprintf(fp, "cm->M: %d\n", cm->M);
1954 for(v = 0; v < cm->M; v++)
1955 {
1956 fprintf(fp, "v:%4d:%4d %4s %2s\n", v, cm->ndidx[v], nodetypes[(int) cm->ndtype[cm->ndidx[v]]], sttypes[(int) cm->sttype[v]]);
1957 if(cm->nodemap[cm->ndidx[v]] == v)
1958 fprintf(fp, "beg: %0.6f (%.6f %10d)| end %0.6f (%.6f %10d)\n",
1959 cm->begin[v], cm->beginsc[v], cm->ibeginsc[v],
1960 cm->end[v], cm->endsc[v], cm->iendsc[v]);
1961 if(cm->sttype[v] == MP_st)
1962 {
1963 fprintf(fp, "\tE: ");
1964 for(i = 0; i < cm->abc->K*cm->abc->K; i++)
1965 fprintf(fp, "%0.6f (%.6f %6d) ", cm->e[v][i], cm->esc[v][i], cm->iesc[v][i]);
1966 fprintf(fp, "\n");
1967 }
1968 else if(cm->sttype[v] == ML_st ||
1969 cm->sttype[v] == MR_st ||
1970 cm->sttype[v] == IL_st ||
1971 cm->sttype[v] == IR_st)
1972 {
1973 fprintf(fp, "\tE: ");
1974 for(i = 0; i < cm->abc->K; i++)
1975 fprintf(fp, "%0.6f (%0.6f %10d) ", cm->e[v][i], cm->esc[v][i], cm->iesc[v][i]);
1976 fprintf(fp, "\n");
1977 }
1978 if(cm->sttype[v] != B_st && cm->sttype[v] != E_st)
1979 {
1980 fprintf(fp, "\tT: ");
1981 for(yoffset = 0; yoffset < cm->cnum[v]; yoffset++)
1982 fprintf(fp, "%0.6f (%0.6f %10d) ", cm->t[v][yoffset], cm->tsc[v][yoffset], cm->itsc[v][yoffset]);
1983 fprintf(fp, "\n");
1984 }
1985 else if(cm->sttype[v] == B_st)
1986 {
1987 fprintf(fp, "\tL: %d | R: %d\n", cm->cfirst[v], cm->cnum[v]);
1988 }
1989 else if(cm->sttype[v] == E_st)
1990 fprintf(fp, "\n\n");
1991 }
1992 fprintf(fp, "\n\n");
1993 free(nodetypes);
1994 free(sttypes);
1995 return;
1996
1997 ERROR:
1998 cm_Fail("Memory allocation error.");
1999 }
2000
2001 /**************************************************************************
2002 * EPN 09.01.06
2003 * Function: check_sub_cm_by_sampling()
2004 *
2005 * Purpose: Given a CM and a sub CM that is supposed to mirror
2006 * the CM as closely as possible between two given consensus
2007 * columns (spos and epos), check that the sub_cm was correctly
2008 * constructed.
2009 *
2010 * The current approach is to build a CM Plan 9 HMM from the
2011 * sub CM, then sample from the CM and see if the samples
2012 * were likely drawn from the CM Plan 9 distributions.
2013 * This is done inside CP9_cm2wrhmm::CP9_check_by_sampling().
2014 *
2015 * Args:
2016 * orig_cm - the original, template CM
2017 * sub_cm - the sub CM built from the orig_cm
2018 * errbuf - for error messages
2019 * r - source of randomness
2020 * submap - map from orig_cm to sub_cm and vice versa
2021 * subinfo - sub cm information
2022 * chi_thresh - rejection threshold for chi-squared tests
2023 * nsamples - number of samples to use to build the ML HMM
2024 * print_flag - TRUE to print useful info for debugging
2025 *
2026 * Returns: eslOK if CM and sub CM are "close enough" (see code)
2027 * eslFAIL otherwise, errbuf is filled
2028 */
2029 int
2030 check_sub_cm_by_sampling(CM_t *orig_cm, CM_t *sub_cm, char *errbuf, ESL_RANDOMNESS *r, CMSubMap_t *submap, CMSubInfo_t *subinfo,
2031 float chi_thresh, int nsamples, int print_flag)
2032 {
2033 int status;
2034 CP9_t *orig_hmm; /* constructed CP9 HMM from the original cm */
2035 CP9_t *sub_hmm; /* constructed CP9 HMM from the sub_cm */
2036 CP9Map_t *orig_cp9map; /* maps the orig_cm to the orig_hmm and vice versa */
2037 CP9Map_t *sub_cp9map; /* maps the sub_cm to the sub_hmm and vice versa */
2038
2039 /* Build two CP9 HMMs, one for the orig_cm and one for the sub_cm */
2040 if((status = build_cp9_hmm(orig_cm, errbuf, FALSE, 0.0001, print_flag, &orig_hmm, &orig_cp9map)) != eslOK) return status;
2041 if((status = build_cp9_hmm(sub_cm, errbuf, FALSE, 0.0001, print_flag, &sub_hmm, &sub_cp9map)) != eslOK) return status;
2042 CP9Logoddsify(orig_hmm);
2043 CP9Logoddsify(sub_hmm);
2044
2045 /* Look for 'impossible' cases where we know the sub_cm
2046 * construction procedure fails, in that the distribution of transitions out of CP9 nodes
2047 * built from the sub_cm will be the same distros out of corresponding CP9 nodes built from
2048 * the full CM. */
2049 cm2sub_cm_find_impossible_misc_cases(orig_cm, sub_cm, submap, subinfo, orig_cp9map, sub_cp9map, print_flag);
2050 cm2sub_cm_find_impossible_matr_cases(orig_cm, sub_cm, submap, subinfo, orig_cp9map, sub_cp9map, print_flag);
2051
2052 if((status = CP9_check_by_sampling(orig_cm, sub_hmm, errbuf, r, subinfo, submap->spos, submap->epos, chi_thresh, nsamples, print_flag)) != eslOK) return status;
2053 if(print_flag) printf("CM Plan 9 built from sub_cm passed sampling check; sub_cm was built correctly.\n");
2054
2055 FreeCPlan9(orig_hmm);
2056 FreeCPlan9(sub_hmm);
2057 FreeCP9Map(orig_cp9map);
2058 FreeCP9Map(sub_cp9map);
2059 return eslOK;
2060 }
2061
2062
2063 /**************************************************************************
2064 * EPN 09.07.06
2065 * check_orig_psi_vs_sub_psi()
2066 *
2067 * Purpose: Check that the psi values for an original, template CM and
2068 * a sub CM are withing a given leeway threshold, given maps
2069 * from the states of the original to the sub and vice versa.
2070 *
2071 * Args:
2072 * orig_cm - the original, template CM
2073 * sub_cm - the sub CM
2074 * errbuf - for error messages
2075 * submap - the map data structure for the sub CM
2076 * threshold - the threshold that mapping (potentially summed) psi
2077 * values are allowed to be different by, without throwing an error.
2078 * print_flag - TRUE to print out the values, FALSE not to
2079 *
2080 * Returns: eslOK if CM and sub CM are "close enough" (see code)
2081 * eslFAIL otherwise, errbuf is filled
2082 */
2083 int
2084 check_orig_psi_vs_sub_psi(CM_t *orig_cm, CM_t *sub_cm, char *errbuf,CMSubMap_t *submap, double threshold,
2085 int print_flag)
2086 {
2087 int v_s; /* sub_cm state index*/
2088 int v_o; /* orig_cm state index*/
2089 double temp_psi;
2090 int violation;
2091 int v_ct; /* Number of violations */
2092 int detached_insert;
2093 int is_insert;
2094 int root_v_ct;
2095 float diff;
2096 double *orig_psi; /* expected num times each state visited in orig_cm */
2097 double *sub_psi; /* expected num times each state visited in sub_cm */
2098
2099 /* Fill orig_psi and sub_psi parameters. */
2100 if((orig_psi = cm_ExpectedStateOccupancy(orig_cm)) == NULL) ESL_FAIL(eslFAIL, errbuf, "check_orig_psi_vs_sub_cm(), unable to calculate expected state occupancy for orig_cm()");
2101 if((sub_psi = cm_ExpectedStateOccupancy(sub_cm)) == NULL) ESL_FAIL(eslFAIL, errbuf, "check_orig_psi_vs_sub_cm(), unable to calculate expected state occupancy for sub_cm()");
2102
2103 if(print_flag)
2104 {
2105 printf("Printing psi in check_orig_psi_vs_sub_psi():\n");
2106 for(v_o = 0; v_o < orig_cm->M; v_o++)
2107 printf("orig_psi[%4d]: %.6f\n", v_o, orig_psi[v_o]);
2108
2109 }
2110
2111 v_ct = 0;
2112 root_v_ct = 0;
2113 if(print_flag == TRUE) printf("\n");
2114 for(v_s = 0; v_s < sub_cm->M; v_s++)
2115 {
2116 detached_insert = FALSE;
2117 if(sub_cm->sttype[v_s] == IL_st || sub_cm->sttype[v_s] == IR_st)
2118 is_insert = TRUE;
2119 else
2120 is_insert = FALSE;
2121
2122 if(print_flag) printf("\tv_s: %4d (%.6f) ", v_s, sub_psi[v_s]);
2123 v_o = submap->s2o_smap[v_s][0];
2124 if(sub_cm->sttype[v_s+1] == E_st) detached_insert = TRUE;
2125 if(v_o != -1)
2126 {
2127 if(print_flag) printf("v_o1: %4d (%.6f) ", v_o, orig_psi[v_o]);
2128 temp_psi = orig_psi[v_o];
2129 v_o = submap->s2o_smap[v_s][1];
2130 if(v_o != -1)
2131 {
2132 if(is_insert) /* this insert state maps to 2 orig_cm inserts */
2133 ESL_FAIL(eslFAIL, errbuf, "check_orig_psi_vs_sub_psi(), sub insert state maps to 2 orig_cm inserts.");
2134 temp_psi += orig_psi[v_o];
2135 if(print_flag)
2136 printf("v_o2: %4d (%.6f)\n", v_o, orig_psi[v_o]);
2137 }
2138 else
2139 if(print_flag) printf("\n");
2140
2141 violation = FALSE;
2142 if(detached_insert)
2143 temp_psi = 0.0;
2144
2145 /* 10.20.06 Found an exceedingly rare case (2 cases in all possible sub CM
2146 * models of RMARK) where psi test fails with diff < 0.00002 but > 0.00001
2147 * (our default). Both cases involve insert self loops with p > 0.9,
2148 * the reason (I'm pretty sure) these guys fail is because when the
2149 * contribution of the self insertion loop is included in fill_psi, even
2150 * if the self-insert probs for a sub_cm and orig_cm state are equal, if
2151 * the psi values for that state BEFORE the contribution of the self insert
2152 * is added are > 0.0000001 or so, the self insert contributions amplifies
2153 * that difference above our 0.00001 threshold. This only happens if the
2154 * self insert probs are really high and explains the rareness of this case.
2155 * The approach to fixing it is to subtract out the self loop contribution
2156 * prior to checking if we exceed our threshold.
2157 */
2158 if(is_insert && !detached_insert)
2159 {
2160 diff = (sub_psi[v_s] - (sub_psi[v_s] * sub_cm->t[v_s][0])) -
2161 (temp_psi - (temp_psi * orig_cm->t[submap->s2o_smap[v_s][0]][0]));
2162 }
2163 else
2164 diff = sub_psi[v_s] - temp_psi;
2165 if((diff > threshold) || ((-1. * diff) > threshold))
2166 {
2167 violation = TRUE;
2168 v_ct++;
2169 if((sub_cm->ndidx[v_s] == 0) || (sub_cm->ndidx[v_s] == 1))
2170 {
2171 root_v_ct++;
2172 }
2173 }
2174 if(violation)
2175 printf("sub: %.6f | orig: %.6f | diff: %.6f VIOLATION\n\n", sub_psi[v_s], temp_psi, (sub_psi[v_s]-temp_psi));
2176 else if(detached_insert && print_flag)
2177 printf("sub: %.6f | orig: %.6f | diff: %.6f (DEAD INSERT)\n\n", sub_psi[v_s], temp_psi, (sub_psi[v_s]-temp_psi));
2178 else if(print_flag)
2179 printf("sub: %.6f | orig: %.6f | diff: %.6f\n\n", sub_psi[v_s], temp_psi, (sub_psi[v_s]-temp_psi));
2180 }
2181 else
2182 {
2183 if(!detached_insert &&
2184 sub_cm->sttype[v_s] != E_st &&
2185 sub_cm->sttype[v_s] != B_st &&
2186 sub_cm->sttype[v_s] != S_st &&
2187 sub_cm->sttype[v_s] != EL_st)
2188 ESL_FAIL(eslFAIL, errbuf, "check_orig_psi_vs_sub_psi() state v_s:%d maps to nothing and its not E,B,S,EL\n", v_s);
2189 if(print_flag) printf("E B S or EL\n");
2190 }
2191 }
2192
2193 if(v_ct > 0) ESL_FAIL(eslFAIL, errbuf, "check_orig_psi_vs_sub_psi(): check failed");
2194 else
2195 if(print_flag) printf("v_ct is 0 with thresh: %f!\n", threshold);
2196 if(root_v_ct > 0)
2197 printf("ROOT v_ct is %d with thresh: %f!\n", root_v_ct, threshold);
2198
2199 /* Cleanup and exit. */
2200 free(orig_psi);
2201 free(sub_psi);
2202
2203 return eslOK;
2204 }
2205
2206 /**************************************************************************
2207 * EPN 09.11.06
2208 * cm_trans_check()
2209 *
2210 * Return TRUE if there's a transition in the CM from state a to state b.
2211 *
2212 * Args:
2213 * CM_t cm,
2214 * int a;
2215 * int b;
2216 * Returns: TRUE if b is a child of a (a->b exists)
2217 * FALSE otherwise
2218 */
2219 int
2220 cm_trans_check(CM_t *cm, int a, int b)
2221 {
2222 /*printf("\t**in cm_trans_check a: %d | b: %d\n", a, b);*/
2223
2224 if((a == -1 || b == -1) || (a > b))
2225 return FALSE;
2226
2227 if((b - cm->cfirst[a]) < cm->cnum[a])
2228 { /*printf("returning TRUE") ;*/ return TRUE; }
2229
2230 return FALSE;
2231
2232 }
2233
2234 /**************************************************************************
2235 * EPN 09.21.06
2236 * Function: cm2sub_cm_subtract_root_subpaths()
2237 *
2238 * Purpose: When building a sub CM (sub_cm) from an original, template
2239 * CM (orig_cm), there's special considerations that must be
2240 * taken involving sub_cm nodes with 2 insert states where there
2241 * isn't an exactly identical node in the original CM. The only
2242 * node that potentially meets this criteria is the ROOT_nd because
2243 * all MATP nodes in sub_cm must necessarily also exist in the
2244 * orig_cm.
2245 *
2246 * The problem is that we have overcounted certain subpaths when
2247 * transitioning out of the sub_cm ROOT_S and ROOT_IL states.
2248 *
2249 * For each sub_cm ROOT_IL state, there are up to 2 orig_cm inserts
2250 * that map to it (orig_il1 and orig_il2), and analagously for sub_cm
2251 * ROOT_IR (orig_ir1 and orig_ir2). Then there are up to 2 sub_cm states
2252 * that map to each of the split set states in sub_cm node 1 (orig_ss1
2253 * and orig_ss2). The subpaths that we have overcounted is dependent
2254 * on the relationship between orig_il*, orig_ir*, and orig_ss*.
2255 * There are six cases of this relationship:
2256 *
2257 * cases 1A, 1B, 1C apply when il < ir.
2258 * case 1A: il < ir < ss (this is correctly handled by cm2sub_cm_sum_subpaths())
2259 * case 1B: il < ss < ir
2260 * case 1C: ss < il < ir
2261 *
2262 * cases 2A, 2B, 2C apply when ir < il.
2263 * case 2A: ir < il < ss
2264 * case 2B: ir < ss < il
2265 * case 2C: ss < ir < il
2266 *
2267 * These cases are not explicitly checked for in the code but were
2268 * useful for determining the correct strategy to use here, and
2269 * are mentioned in comments throughout the code.
2270 *
2271 * This function calls a helper function for each pair of orig_il* and
2272 * orig_ir* (up to 4 possible pairs), which actually subtracts
2273 * the path based on the relationships of orig_il*, orig_ir* and
2274 * orig_ss*.
2275 *
2276 * Importantly, though there are potentially many orig_ss* states
2277 * (up to 2 that map to each sub_cm node 1 split set state),
2278 * they all must be close enough in state indices that we'll
2279 * never have a case where for a specific il* and ir* we get more
2280 * than 1 of the 6 possible cases for all orig_ss*. This is
2281 * because either the orig_ss are in a sub_cm MATP node - in
2282 * which case they MUST map to exactly 1 state each in the orig_cm
2283 * (to the corresponding MATP node) and are contiguous state indices,
2284 * or they are a MATL or MATR which either map to a corresponding
2285 * MATL, MATR (in which case they're contiguous) or each sub_cm
2286 * split state maps to exactly 2 states in the same orig_cm MATP
2287 * node, (for example MATL_ML might map to MATP_MP and MATP_ML)
2288 * which are all in the same node and thus have no insert states
2289 * with a state index between the indices of the 2 states they
2290 * map to (ouch).
2291 *
2292 *
2293 * Args:
2294 * CM_t *orig_cm - the original, template CM
2295 * CM_t *sub_cm - the sub CM
2296 * double *orig_psi - for orig_cm: orig_psi[v] is the expected number of times state v is entered
2297 * in a CM parse
2298 * char ***tmap - the hard-coded transition map
2299 * CMSubMap_t *submap- the map from the sub CM to the template CM
2300 * int print_flag - TRUE to print useful debugging info
2301 *
2302 * Returns: VOID
2303 */
2304 static void
2305 cm2sub_cm_subtract_root_subpaths(CM_t *orig_cm, CM_t *sub_cm, double *orig_psi, char ***tmap,
2306 CMSubMap_t *submap, int print_flag)
2307
2308 {
2309 int sub_root_s;
2310 int sub_il;
2311 int sub_ir;
2312 int yoffset;
2313 int sub_y;
2314 int orig_y;
2315 int orig_il;
2316 int orig_ir;
2317 int orig_ss;
2318 int orig_ss1;
2319 int orig_ss2;
2320
2321 sub_root_s = 0; /* sub_cm ROOT_S index */
2322 sub_il = 1; /* sub ROOT_IL index */
2323 sub_ir = 2; /* sub ROOT_IR index */
2324 orig_il = submap->s2o_smap[sub_il][0];
2325 orig_ir = submap->s2o_smap[sub_ir][0];
2326
2327 if(print_flag) printf("\n\nin cm2sum_cm_subtract_root_subpaths_helper: orig_il: %d orig_ir: %d\n", orig_il, orig_ir);
2328
2329 for(yoffset = 0; yoffset < sub_cm->cnum[0]; yoffset++)
2330 if(print_flag) printf("Before t[0][%d] = %f\n", yoffset, sub_cm->t[0][yoffset]);
2331
2332 orig_ss = submap->s2o_smap[3][0]; /* orig_ss is the 1 (of possibly 2) orig_cm states that map to the first
2333 * state in sub_cm node 1 (sub_cm state 3)
2334 */
2335 /* Check to make sure that all the split states meet our guarantee (not necessary) */
2336 for(yoffset = 2; yoffset < sub_cm->cnum[0]; yoffset++) /* note we start at yoffset = 2
2337 * ROOT_S -> first state of next
2338 * node's split set. */
2339 {
2340 sub_y = yoffset + sub_cm->cfirst[0];
2341 orig_y = submap->s2o_smap[sub_y][0];
2342 if((orig_il > orig_ss && orig_il < orig_y) ||
2343 (orig_il < orig_ss && orig_il > orig_y) ||
2344 (orig_ir > orig_ss && orig_ir < orig_y) ||
2345 (orig_ir < orig_ss && orig_ir > orig_y))
2346 cm_Fail("ERROR in cm2sub_cm_subtract_root_subpaths_helper() split set state guarantee violated!\n");
2347 }
2348
2349
2350 /* Check for which of the 6 cases we have (not actually necessary) */
2351 if((orig_il < orig_ir) && (orig_ir < orig_ss))
2352 if(print_flag) printf("ROOT NODE case 1A\n");
2353 if((orig_il < orig_ss) && (orig_ss < orig_ir))
2354 if(print_flag) printf("ROOT NODE case 1B\n");
2355 if((orig_ss < orig_il) && (orig_il < orig_ir))
2356 if(print_flag) printf("ROOT NODE case 1C\n");
2357
2358 if((orig_ir < orig_il) && (orig_il < orig_ss))
2359 if(print_flag) printf("ROOT NODE case 2A\n");
2360 if((orig_ir < orig_ss) && (orig_ss < orig_il))
2361 if(print_flag) printf("ROOT NODE case 2B\n");
2362 if((orig_ss < orig_ir) && (orig_ir < orig_il))
2363 if(print_flag) printf("ROOT NODE case 2C\n");
2364
2365
2366 /* First adjust counts out of sub_cm ROOT_S */
2367
2368 /* if orig_ir < orig_il, we've counted paths from
2369 * ir -> il correctly for ROOT_IL -> ROOT_IR and
2370 * incorrectly for ROOT_S -> ROOT_IR
2371 */
2372 if(orig_ir < orig_il)
2373 {
2374 if(print_flag) printf("0 sub from S->IR, IR -> IL\n"); /* cases 2A, 2B, 2C */
2375 sub_cm->t[sub_root_s][1] -= orig_psi[orig_ir] *
2376 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_ir, orig_il, 0, tmap, orig_psi);
2377 }
2378
2379 /* All the remaining paths to be subtracted involve the split set state of
2380 * node 1. */
2381 for(yoffset = 0; yoffset < sub_cm->cnum[0]; yoffset++)
2382 {
2383 sub_y = sub_cm->cfirst[0] + yoffset;
2384 orig_ss1 = submap->s2o_smap[sub_y][0];
2385 orig_ss2 = submap->s2o_smap[sub_y][1];
2386 if(sub_cm->ndidx[sub_y] != 0)
2387 {
2388 /* Adjust counts out of ROOT_IL if necessary */
2389
2390 /* if orig_ir < orig_ss < orig_il, we've counted paths from
2391 * ir -> ss -> il correctly for ROOT_IL -> ROOT_IR and
2392 * incorrectly for ROOT_IL -> ROOT_SS
2393 */
2394 if(orig_ir < orig_ss && orig_ss < orig_il) /* case 2B only */
2395 {
2396 if(print_flag) printf("3 (2b) sub from IL, IR -> SS -> IL\n");
2397
2398 /* subtract paths from orig_ir -> orig_ss1 -> orig_il */
2399 sub_cm->t[sub_il][yoffset] -= orig_psi[orig_ir] *
2400 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_ir, orig_ss1,
2401 sub_il, tmap, orig_psi) *
2402 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_ss1, orig_il,
2403 sub_il, tmap, orig_psi);
2404 if(orig_ss2 != -1)
2405 /* subtract paths from orig_ir -> orig_ss2 -> orig_il */
2406 sub_cm->t[1][yoffset] -= orig_psi[orig_ir] *
2407 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_ir, orig_ss2,
2408 sub_il, tmap, orig_psi) *
2409 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_ss2, orig_il,
2410 sub_il, tmap, orig_psi);
2411
2412 }
2413
2414 /* if orig_ss < orig_il < orig_ir, we've counted paths from
2415 * ss -> il -> ir correctly for ROOT_IR -> ROOT_SS and
2416 * incorrectly for ROOT_IL -> ROOT_SS
2417 */
2418 if(orig_ss < orig_il && orig_il < orig_ir) /* case 1C only */
2419 {
2420 if(print_flag) printf("4 (1c) sub from IL, SS -> IL -> IR\n");
2421
2422 /* subtract paths from orig_ss1 -> orig_il (add self insert) -> orig_ir */
2423 sub_cm->t[sub_il][yoffset] -= orig_psi[orig_ss1] *
2424 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_ss1, orig_il,
2425 sub_il, tmap, orig_psi) *
2426 (1. + (orig_cm->t[orig_il][0] / (1 - orig_cm->t[orig_il][0]))) *
2427 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_il, orig_ir,
2428 sub_il, tmap, orig_psi);
2429
2430 if(orig_ss2 != -1)
2431 /* subtract paths from orig_ss2 -> orig_il (add self insert) -> orig_ir */
2432 sub_cm->t[sub_il][yoffset] -= orig_psi[orig_ss2] *
2433 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_ss2, orig_il,
2434 sub_il, tmap, orig_psi) *
2435 (1. + (orig_cm->t[orig_il][0] / (1 - orig_cm->t[orig_il][0]))) *
2436 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_il, orig_ir,
2437 sub_il, tmap, orig_psi);
2438 }
2439
2440 /* if orig_ir < orig_il < orig_ss, we've counted paths from
2441 * ir -> il -> ss correctly for ROOT_IR -> ROOT_SS and
2442 * incorrectly for ROOT_IL -> ROOT_SS
2443 */
2444 if(orig_ir < orig_il && orig_il < orig_ss) /* case 2A only */
2445 {
2446 if(print_flag) printf("5 (2a) sub from IL, IR -> IL -> SS\n");
2447
2448 /* subtract paths from orig_ir -> orig_il (add self insert) -> orig_ss1 */
2449 sub_cm->t[sub_il][yoffset] -= orig_psi[orig_ir] *
2450 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_ir, orig_il,
2451 sub_il, tmap, orig_psi) *
2452 (1. + (orig_cm->t[orig_il][0] / (1 - orig_cm->t[orig_il][0]))) *
2453 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_il, orig_ss1,
2454 sub_il, tmap, orig_psi);
2455
2456 if(orig_ss2 != -1)
2457 /* subtract paths from orig_ir -> orig_il (add self insert) -> orig_ss2 */
2458 sub_cm->t[sub_il][yoffset] -= orig_psi[orig_ir] *
2459 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_ir, orig_il,
2460 sub_il, tmap, orig_psi) *
2461 (1. + (orig_cm->t[orig_il][0] / (1 - orig_cm->t[orig_il][0]))) *
2462 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_il, orig_ss2,
2463 sub_il, tmap, orig_psi);
2464
2465 }
2466
2467 /* if orig_il < orig_ss < orig_ir, we've counted paths from
2468 * il -> ss -> ir correctly for ROOT_IL -> ROOT_IR and
2469 * incorrectly for ROOT_IL -> ROOT_SS
2470 */
2471 if(orig_il < orig_ss && orig_ss < orig_ir) /* case 1B only */
2472 {
2473 if(print_flag) printf("6 (1b) sub from IL, IL -> SS -> IR\n");
2474 if(print_flag) printf("1B before sub 1: sub_cm->t[sub_il:%d][yoffset:%d]: %f\n", sub_il, yoffset, sub_cm->t[sub_il][yoffset]);
2475 /* subtract paths from orig_il -> orig_ss1 -> orig_ir */
2476 sub_cm->t[sub_il][yoffset] -= orig_psi[orig_il] *
2477 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_il, orig_ss1,
2478 sub_il, tmap, orig_psi) *
2479 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_ss1, orig_ir,
2480 sub_il, tmap, orig_psi);
2481
2482 if(print_flag) printf("1B before sub 2: sub_cm->t[sub_il:%d][yoffset:%d]: %f\n", sub_il, yoffset, sub_cm->t[sub_il][yoffset]);
2483 if(orig_ss2 != -1)
2484 /* subtract paths from orig_il -> orig_ss2 -> orig_ir */
2485 sub_cm->t[sub_il][yoffset] -= orig_psi[orig_il] *
2486 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_il, orig_ss2,
2487 sub_il, tmap, orig_psi) *
2488 cm2sub_cm_sum_subpaths(orig_cm, sub_cm, submap, orig_ss2, orig_ir,
2489 sub_il, tmap, orig_psi);
2490 if(print_flag) printf("1B after sub 2: sub_cm->t[sub_il:%d][yoffset:%d]: %f\n", sub_il, yoffset, sub_cm->t[sub_il][yoffset]);
2491
2492 }
2493 }
2494 }
2495 for(yoffset = 0; yoffset < sub_cm->cnum[0]; yoffset++)
2496 if(print_flag) printf("After t[0][%d] = %f\n", yoffset, sub_cm->t[0][yoffset]);
2497
2498 return;
2499 }
2500
2501
2502 /**************************************************************************
2503 * EPN 09.11.06
2504 * cm2sub_cm_check_id_next_node()
2505 *
2506 * It's known that sub_nd and orig_nd are identical nodes, in that they
2507 * are of the same type and model the same column of the seed alignment.
2508 * In this function, we check if the next node of the sub_cm and orig_nd
2509 * have the same relationship and if so, we update information in
2510 * the submap->s2o_id array, setting the sub_cm states of this node to
2511 * TRUE.
2512 *
2513 * Returns: void
2514 *
2515 * Args:
2516 * CM_t orig_cm
2517 * CM_t sub_cm
2518 * int orig_nd
2519 * int sub_nd
2520 * CMSubMap_t *submap
2521 * int sub_start
2522 */
2523
2524 static int
2525 cm2sub_cm_check_id_next_node(CM_t *orig_cm, CM_t *sub_cm, int orig_nd, int sub_nd,
2526 CMSubMap_t *submap, CP9Map_t *orig_cp9map,
2527 CP9Map_t *sub_cp9map, int print_flag)
2528 {
2529 int v_s;
2530 int left_check, right_check;
2531 left_check = FALSE;
2532 right_check = FALSE;
2533
2534 if((orig_nd+1) > (orig_cm->nodes-1))
2535 return FALSE;
2536 if((sub_nd+1) > (sub_cm->nodes-1))
2537 return FALSE;
2538 if(orig_cm->ndtype[orig_nd] != sub_cm->ndtype[sub_nd])
2539 return FALSE;
2540 if(orig_cm->ndtype[orig_nd+1] != sub_cm->ndtype[sub_nd+1])
2541 return FALSE;
2542
2543 if(orig_cp9map->nd2lpos[orig_nd+1] == -1 && sub_cp9map->nd2lpos[sub_nd+1] == -1)
2544 left_check = TRUE;
2545 if(orig_cp9map->nd2lpos[orig_nd+1] == (sub_cp9map->nd2lpos[sub_nd+1] + submap->spos -1))
2546 left_check = TRUE;
2547 if(orig_cp9map->nd2rpos[orig_nd+1] == -1 && sub_cp9map->nd2rpos[sub_nd+1] == -1)
2548 right_check = TRUE;
2549 if(orig_cp9map->nd2rpos[orig_nd+1] == (sub_cp9map->nd2rpos[sub_nd+1] + submap->spos -1))
2550 right_check = TRUE;
2551
2552 if(left_check && right_check)
2553 {
2554 v_s = sub_cm->nodemap[sub_nd];
2555 while(sub_cm->ndidx[v_s] == sub_nd)
2556 {
2557 if(print_flag) printf("setting submap->s2o_id[v_s:%d] to TRUE\n", v_s);
2558 if(sub_cm->sttype[v_s+1] != E_st) submap->s2o_id[v_s] = TRUE; /* if v+1 is an E_st, it's a detached insert don't set s2o_id[v] to TRUE */
2559 v_s++;
2560 }
2561 return TRUE;
2562 }
2563 return FALSE;
2564 }
2565
2566 /**************************************************************************
2567 * EPN 10.05.06
2568 * cm2sub_cm_find_impossible_misc_cases
2569 *
2570 * For certain situations, the conversion of an orig_cm to a sub_cm loses
2571 * some information that makes it impossible for a CP9 trained from the sub_cm
2572 * to exactly match a CP9 trained from the orig_cm for the corresponding
2573 * columns. One case where it is impossible involves start states as follows:
2574 *
2575 * if for any k k=spos..epos-1
2576 * X >= 0 start states exists in the orig_cm in a node between:
2577 * orig_cp9map->pos2nd[k] -> orig_cp9map->pos2nd[k+1]
2578 * AND Y >= 1 start states exist in the sub_cm in a node between:
2579 * sub_cp9map->pos2nd[k-spos+1] -> sub_cp9map->pos2nd[k-spos+1+1]
2580 * where X != Y
2581 *
2582 * AND further one or both of the two nodes in the sub_cm (sub_cp9map->pos2nd[k-spos+1]
2583 * OR sub_cp9map->pos2nd[k-spos+1+1] must be a MATP.
2584 *
2585 * This is because for the sub_cm paths that would go from CP9 node k to k+1
2586 * were forced to go through a start state where as for the orig_cm there
2587 * was not a requirement to go through a start. Therefore when the
2588 * sub_cm was constructed it lost some information about the original
2589 * transitions.
2590 *
2591 * Also, a special situation of this case occurs with transitions out of
2592 * CP9 node k=spos-1 to k=spos, and out of CP9 node k=epos to k=epos+1.
2593 * The sub_cm node that models columns spos-1 and epos-1 is the ROOT node,
2594 * which only really models inserts in those columns. It turns out that
2595 * the transition distributions will nearly always be screwy out of these
2596 * two nodes, except in the case when the sub_cm ROOT_IL and ROOT_IR map
2597 * to an IL and IR state respectivley in the original *within the same
2598 * orig_cm node*. In which case the transitions out of node 0 will be
2599 * identical to those out of node spos-1 in the orig_cm.
2600 * There are a few other rare cases where the transitions will be
2601 * identical, but an exhaustive understanding of them eludes me.
2602 * (see ~nawrockie/notebook/6_0725_inf_sub_cm/00LOG for more).
2603 *
2604 * Returns: void
2605 *
2606 * Args:
2607 * CM_t orig_cm
2608 * CM_t sub_cm
2609 * int *orig_cp9map->pos2nd
2610 * int *sub_cp9map->pos2nd
2611 * int *imp_cc
2612 * int spos;
2613 * int epos;
2614 */
2615
2616 static void
2617 cm2sub_cm_find_impossible_misc_cases(CM_t *orig_cm, CM_t *sub_cm, CMSubMap_t *submap, CMSubInfo_t *subinfo,
2618 CP9Map_t *orig_cp9map, CP9Map_t *sub_cp9map, int print_flag)
2619
2620 {
2621 int status;
2622 int k;
2623 int sub_starts;
2624 int orig_starts;
2625 int orig_nd1;
2626 int orig_nd2;
2627 int sub_nd1;
2628 int sub_nd2;
2629 int temp;
2630 int nd;
2631 int orig_special_matps;
2632 int sub_special_matl;
2633 int sub_both_matps;
2634 CMEmitMap_t *orig_emap; /* consensus emit map for the original, template CM */
2635 CMEmitMap_t *sub_emap; /* consensus emit map for the sub CM */
2636
2637 int orig_il1;
2638 int orig_il2;
2639 int orig_ir1;
2640 int orig_ir2;
2641
2642 char **nodetypes;
2643 ESL_ALLOC(nodetypes, sizeof(char *) * 8);
2644 nodetypes[0] = "BIF";
2645 nodetypes[1] = "MATP";
2646 nodetypes[2] = "MATL";
2647 nodetypes[3] = "MATR";
2648 nodetypes[4] = "BEGL";
2649 nodetypes[5] = "BEGR";
2650 nodetypes[6] = "ROOT";
2651 nodetypes[7] = "END";
2652
2653 if(print_flag) printf("in cm2sub_find_impossible_misc_cases()\n");
2654
2655 orig_emap = CreateEmitMap(orig_cm);
2656 sub_emap = CreateEmitMap(sub_cm);
2657
2658 /* We know that the sub CM node 0 and node submap->sub_clen are nearly always going to
2659 * get the transition distributions wrong (see comments above in the function
2660 * explanation). There are a couple of cases they should get right
2661 * (see ~nawrockie/notebook/6_0725_inf_sub_cm/00LOG for details), we
2662 * check for these here:
2663 */
2664 subinfo->imp_cc[0] = 1;
2665 subinfo->imp_cc[submap->sub_clen] = 1;
2666
2667 /* check if the orig_cm states that model sub_cm ROOT_IL and ROOT_IR are
2668 * from the same node. */
2669 orig_il1 = submap->s2o_smap[1][0]; /* 1st of up to 2 states that maps to sub_cm's ROOT_IL */
2670 orig_il2 = submap->s2o_smap[1][1]; /* 2nd state that maps to sub_cm's ROOT_IL or -1 if only 1 maps*/
2671 orig_ir1 = submap->s2o_smap[2][0]; /* 1st of up to 2 states that maps to sub_cm's ROOT_IR */
2672 orig_ir2 = submap->s2o_smap[2][1]; /* 2nd state that maps to sub_cm's ROOT_IR or -1 if only 1 maps*/
2673
2674 /* We ASSUME that ambiguities have been removed, i.e. if two insert states map to either ROOT_IL
2675 * or ROOT_IR, one of them has been detached. We exploit this knowledge.
2676 */
2677 if(orig_il2 != -1)
2678 {
2679 if(orig_cm->sttype[orig_il1+1] == E_st)
2680 orig_il1 = orig_il2; /* orig_il1 was detached */
2681 else if(orig_cm->sttype[orig_il2+1] == E_st)
2682 {
2683 /* do nothing */
2684 }
2685 else
2686 cm_Fail("ERROR, can't determine which state was detached\n");
2687 }
2688 if(orig_ir2 != -1)
2689 {
2690 if(orig_cm->sttype[orig_ir1+1] == E_st)
2691 orig_ir1 = orig_ir2; /* orig_ir1 was detached */
2692 else if(orig_cm->sttype[orig_ir2+1] == E_st)
2693 {
2694 /* do nothing */
2695 }
2696 else
2697 cm_Fail("ERROR, can't determine which state was detached\n");
2698 }
2699
2700 /* Now orig_il1 and orig_ir1 map to the ONLY insert states that map to sub_cm
2701 * ROOT_IL and ROOT_IR respectively. */
2702 if(orig_cm->ndidx[orig_il1] == orig_cm->ndidx[orig_ir1])
2703 {
2704 /* we can get the distro out of 0 right in this case */
2705 subinfo->imp_cc[0] = FALSE;
2706 if(submap->spos == submap->sstruct && submap->epos == submap->estruct)
2707 subinfo->imp_cc[submap->sub_clen] = FALSE;
2708 }
2709
2710 for(k = 1; k < submap->sub_clen; k++)
2711 {
2712 if(print_flag) printf("k: %d\n", k);
2713 if((k+submap->spos-1) == 0)
2714 orig_nd1 = 0;
2715 else
2716 orig_nd1 = orig_cp9map->pos2nd[k+submap->spos-1];
2717 orig_nd2 = orig_cp9map->pos2nd[k+submap->spos-1+1];
2718
2719 orig_special_matps = FALSE;
2720 if((orig_cm->ndtype[orig_nd1] == MATP_nd &&
2721 orig_cm->ndtype[orig_nd2] == MATP_nd) &&
2722 (orig_cp9map->nd2rpos[orig_nd1] == (k+submap->spos-1) &&
2723 orig_cp9map->nd2lpos[orig_nd2] == (k+submap->spos)))
2724 {
2725 if((orig_cp9map->nd2lpos[orig_nd1] < submap->spos) ||
2726 (orig_cp9map->nd2rpos[orig_nd2] > submap->epos))
2727 {
2728 /* This is a special case */
2729 orig_special_matps = TRUE;
2730 }
2731 }
2732
2733 if(orig_nd2 < orig_nd1)
2734 {
2735 temp = orig_nd1;
2736 orig_nd1 = orig_nd2;
2737 orig_nd2 = temp;
2738 }
2739 orig_starts = 0;
2740
2741 if(print_flag) printf("orig_nd1: %d | orig_nd2: %d\n", orig_nd1, orig_nd2);
2742 for(nd = orig_nd1; nd <= orig_nd2; nd++)
2743 {
2744 if(print_flag) printf("orig_cm->ndtype[%d]: %s L: %4d R: %4d (submap->spos: %4d) (submap->epos: %4d)\n", nd, nodetypes[(int) orig_cm->ndtype[nd]], orig_emap->lpos[nd], orig_emap->rpos[nd], submap->spos, submap->epos);
2745 if(orig_cm->ndtype[nd] == BEGL_nd ||
2746 orig_cm->ndtype[nd] == BEGR_nd)
2747 { orig_starts++; }
2748 }
2749 sub_nd1 = sub_cp9map->pos2nd[k];
2750 sub_nd2 = sub_cp9map->pos2nd[k+1];
2751
2752 sub_special_matl = FALSE;
2753 if(sub_cm->ndtype[sub_nd1] == MATL_nd)
2754 sub_special_matl = TRUE;
2755
2756 if(sub_nd2 < sub_nd1)
2757 {
2758 temp = sub_nd1;
2759 sub_nd1 = sub_nd2;
2760 sub_nd2 = temp;
2761 }
2762
2763 sub_both_matps = FALSE;
2764 if((sub_cm->ndtype[sub_nd1] == MATP_nd && sub_cm->ndtype[sub_nd2] == MATP_nd) &&
2765 (!(sub_cp9map->nd2lpos[sub_nd1] < sub_cp9map->nd2lpos[sub_nd2] && sub_cp9map->nd2rpos[sub_nd1] > sub_cp9map->nd2rpos[sub_nd2])))
2766 sub_both_matps = TRUE;
2767
2768 sub_starts = 0;
2769 if(print_flag) printf("sub_nd1: %d | sub_nd2: %d\n", sub_nd1, sub_nd2);
2770 for(nd = sub_nd1; nd <= sub_nd2; nd++)
2771 {
2772 if(print_flag) printf("sub_cm->ndtype[%d]: %s L: %4d R: %4d (submap->spos: %4d) (submap->epos: %4d)\n", nd, nodetypes[(int) sub_cm->ndtype[nd]], (sub_emap->lpos[nd]+submap->spos-1), (sub_emap->rpos[nd]+submap->spos-1), submap->spos, submap->epos);
2773 if(sub_cm->ndtype[nd] == BEGL_nd || sub_cm->ndtype[nd] == BEGR_nd)
2774 {
2775 sub_starts++;
2776 if(sub_cm->ndtype[sub_nd1] != MATP_nd && sub_cm->ndtype[sub_nd2] != MATP_nd)
2777 cm_Fail("ERROR in cm2sub_cm_find_impossible_misc_cases() found impossible case not involving any MATP in the sub_cm, k: %d submap->epos-submap->spos+1: %d\n", k, submap->sub_clen);
2778 if(orig_cm->ndtype[orig_nd1] != MATP_nd && orig_cm->ndtype[orig_nd2] != MATP_nd)
2779 cm_Fail("ERROR in cm2sub_cm_find_impossible_misc_cases() found impossible case not involving any MATP in the orig_cm\n, k: %d | submap->epos-submap->spos+1: %d", k, submap->sub_clen);
2780 }
2781 }
2782
2783 if(print_flag) printf("sub_starts: %d | orig_starts: %d\n", sub_starts, orig_starts);
2784
2785 if(sub_starts > 0 && orig_starts == 0)
2786 subinfo->imp_cc[k] = 3;
2787 else if((sub_starts > 0 && sub_starts > orig_starts) &&
2788 sub_both_matps == TRUE)
2789 subinfo->imp_cc[k] = 4;
2790 else if(sub_starts > 0 && orig_special_matps && sub_special_matl)
2791 subinfo->imp_cc[k] = 5;
2792 else if(sub_starts == 0 && orig_starts > 0 && orig_special_matps && sub_special_matl)
2793 subinfo->imp_cc[k] = 6;
2794
2795 }
2796 FreeEmitMap(orig_emap);
2797 FreeEmitMap(sub_emap);
2798 free(nodetypes);
2799 return;
2800
2801 ERROR:
2802 cm_Fail("Memory allocation error.");
2803 }
2804
2805 /**************************************************************************
2806 * EPN 10.18.06
2807 * cm2sub_cm_find_impossible_matr_cases
2808 *
2809 * For certain situations, the conversion of an orig_cm to a sub_cm loses
2810 * some information that makes it impossible for a CP9 trained from the sub_cm
2811 * to exactly match a CP9 trained from the orig_cm for the corresponding
2812 * columns. One case where it is impossible involves MATR nodes as follows:
2813 *
2814 * if for any k k=spos..epos-1:
2815 * orig_cp9map->pos2nd[k] + 1 = orig_cp9map->pos2nd[k+1] AND (NOT TRUE!)
2816 * sub_cp9map->pos2nd[k] + 1 != sub_cp9map->pos2nd[k+1] AND
2817 * orig_cp9map->pos2nd[k] == sub_cp9map->pos2nd[k] == MATL AND
2818 * orig_cp9map->pos2nd[k+1] == sub_cp9map->pos2nd[k+1] == MATP AND
2819 * all nodes between sub_cp9map->pos2nd[k] .. cc_node_map[k+1] are BIF, BEG*, or MATR nodes AND
2820 * the orig_cm nodes that the stretch of MATR nodes map to are all MATP nodes
2821 * for which the other half of the nodes map to positions outside the sub_cm.
2822 *
2823 * Here we explicitly check for these situations and set imp_cc[k] = TRUE for
2824 * any k that satisfy all the criteria.
2825 * Returns: void
2826 *
2827 * Args:
2828 * CM_t orig_cm
2829 * CM_t sub_cm
2830 * int *orig_cp9map->pos2nd
2831 * int *sub_cp9map->pos2nd
2832 * int *imp_cc
2833 * int spos;
2834 * int epos;
2835 */
2836
2837 static void
2838 cm2sub_cm_find_impossible_matr_cases(CM_t *orig_cm, CM_t *sub_cm, CMSubMap_t *submap, CMSubInfo_t *subinfo,
2839 CP9Map_t *orig_cp9map, CP9Map_t *sub_cp9map, int print_flag)
2840 {
2841 int status;
2842 int sub_k;
2843 int orig_k;
2844 int orig_nd;
2845 int sub_nd;
2846 int next_sub_nd;
2847 int next_orig_nd;
2848 int sub_nd1;
2849 int sub_nd2;
2850 int orig_nd1;
2851 int orig_nd2;
2852 int tmp_k;
2853 int tmp_sub_nd;
2854 int tmp_orig_nd;
2855 int correct_node_types_flag;
2856 int orig_matr_stretch_flag;
2857 int sub_matr_stretch_flag;
2858
2859 char **nodetypes;
2860 ESL_ALLOC(nodetypes, sizeof(char *) * 8);
2861 nodetypes[0] = "BIF";
2862 nodetypes[1] = "MATP";
2863 nodetypes[2] = "MATL";
2864 nodetypes[3] = "MATR";
2865 nodetypes[4] = "BEGL";
2866 nodetypes[5] = "BEGR";
2867 nodetypes[6] = "ROOT";
2868 nodetypes[7] = "END";
2869
2870 for(sub_k = 1; sub_k < submap->sub_clen; sub_k++)
2871 {
2872 orig_k = sub_k + submap->spos - 1;
2873 if(print_flag) printf("CASE 2 k: %d\n", sub_k);
2874 /* Strategy: we check each of our criteria independently */
2875
2876 /* Find out what the node that models the next column is */
2877 sub_nd = sub_cp9map->pos2nd[sub_k];
2878 next_sub_nd = sub_cp9map->pos2nd[sub_k+1];
2879 orig_nd = orig_cp9map->pos2nd[orig_k];
2880 next_orig_nd = orig_cp9map->pos2nd[orig_k+1];
2881 if(sub_nd < next_sub_nd)
2882 {
2883 sub_nd1 = sub_nd;
2884 sub_nd2 = next_sub_nd;
2885 }
2886 else
2887 {
2888 sub_nd1 = next_sub_nd;
2889 sub_nd2 = sub_nd;
2890 }
2891 if(orig_nd < next_orig_nd)
2892 {
2893 orig_nd1 = orig_nd;
2894 orig_nd2 = next_orig_nd;
2895 }
2896 else
2897 {
2898 orig_nd1 = next_orig_nd;
2899 orig_nd2 = orig_nd;
2900 }
2901
2902 if(print_flag) printf("CASE 2 k: %d sub_nd: %d next_sub_nd: %d\n", sub_k, sub_nd, next_sub_nd);
2903 if(print_flag) printf("CASE 2 k: %d orig_nd: %d next_orig_nd: %d\n", sub_k, orig_nd, next_orig_nd);
2904
2905 /* Make sure that next_sub_nd is and sub_nd are not consecutive */
2906 if(sub_nd1 != sub_nd2-1)
2907 {
2908 /* Check if min(sub_nd, next_sub_nd) and min(orig_nd, next_orig_nd) are both MATLs and
2909 if max(sub_nd, next_sub_nd) and max(orig_nd, next_orig_nd) are both MATPs */
2910 if((orig_cm->ndtype[orig_nd1] == MATL_nd && sub_cm->ndtype[sub_nd1] == MATL_nd) &&
2911 (orig_cm->ndtype[orig_nd2] == MATP_nd && sub_cm->ndtype[sub_nd2] == MATP_nd))
2912 correct_node_types_flag = TRUE;
2913 else
2914 correct_node_types_flag = FALSE;
2915 if(print_flag) printf("CASE 2 k: %d correct_node_types_flag: %d\n", sub_k, correct_node_types_flag);
2916
2917 if(correct_node_types_flag == TRUE)
2918 {
2919 /* Determine if the next_orig_nd is the next left emitting
2920 * node of the orig_cm after orig_nd */
2921 orig_matr_stretch_flag = TRUE;
2922 for(tmp_orig_nd = orig_nd1+1; tmp_orig_nd < orig_nd2; tmp_orig_nd++)
2923 {
2924 if(orig_cm->ndtype[tmp_orig_nd] != MATR_nd)
2925 {
2926 orig_matr_stretch_flag = FALSE;
2927 break;
2928 }
2929 }
2930 if(print_flag) printf("CASE 2 k: %d orig_matr_stretch_flag: %d\n", sub_k, orig_matr_stretch_flag);
2931 if(orig_matr_stretch_flag == TRUE)
2932 {
2933 /* Check if all the sub_cm nodes between sub_nd and
2934 * next_sub_nd are MATRs. */
2935 sub_matr_stretch_flag = TRUE;
2936 for(tmp_sub_nd = sub_nd1+1; tmp_sub_nd < sub_nd2; tmp_sub_nd++)
2937 if(sub_cm->ndtype[tmp_sub_nd] != MATR_nd)
2938 {
2939 sub_matr_stretch_flag = FALSE;
2940 break;
2941 }
2942 if(print_flag) printf("CASE 2 k: %d sub_matr_stretch_flag: %d\n", sub_k, sub_matr_stretch_flag);
2943 if(sub_matr_stretch_flag == TRUE)
2944 {
2945 /* This should be a MATR impossible case,
2946 * Check all the criteria we *think* are always true in this situation,
2947 * cm_Fail if what we think is wrong.
2948 *
2949 * The orig_cm nodes that map to the same consensus columns
2950 * as the MATR nodes in the sub_cm must either be:
2951
2952 * as the MATR nodes in the sub_cm must either be:
2953 *
2954 * 1 orig_cm MATPs or MATRs for which the RIGHT half maps to the same column
2955 * modelled by the corresponding sub_cm MATR, and in the case of the
2956 * MATPs the LEFT half maps to consensus columns before spos.
2957 * 2 orig_cm nodes that map to the same consensus columns
2958 * as the MATR nodes in the sub_cm are ALL either orig_cm MATPs
2959 * or MATLs for which the LEFT half maps to the same
2960 * column modelled by the corresponding sub_cm MATR, and in the
2961 * case of the MATPs, the RIGHT half maps to consensus columns
2962 * after epos.
2963 *
2964 * Usually the entire set of orig_cm nodes that map to the sub_cm
2965 * MATRs are either one or the other type, but I've seen rare cases
2966 * where there's a stretch of one type, and then a stretch of the
2967 * other.
2968 */
2969 tmp_sub_nd = sub_cp9map->pos2nd[sub_k] + 1;
2970 tmp_k = sub_cp9map->nd2rpos[tmp_sub_nd] + submap->spos - 1;
2971 tmp_orig_nd = orig_cp9map->pos2nd[tmp_k];
2972
2973 for(tmp_sub_nd = sub_nd1+1; tmp_sub_nd < sub_nd2; tmp_sub_nd++)
2974 {
2975 tmp_k = sub_cp9map->nd2rpos[tmp_sub_nd] + submap->spos - 1;
2976 tmp_orig_nd = orig_cp9map->pos2nd[tmp_k];
2977
2978 if(print_flag) printf("10.18.06: %s %3d %3d | %s %3d %3d (xcc: %3d)\n", nodetypes[(int) sub_cm->ndtype[tmp_sub_nd]], tmp_sub_nd, (sub_cp9map->nd2rpos[tmp_sub_nd]+submap->spos-1), nodetypes[(int) orig_cm->ndtype[tmp_orig_nd]], orig_cp9map->nd2lpos[tmp_orig_nd], orig_cp9map->nd2rpos[tmp_orig_nd], tmp_k);
2979 if(orig_cp9map->nd2rpos[tmp_orig_nd] == (sub_cp9map->nd2rpos[tmp_sub_nd]+submap->spos-1)) /* Case 1 above */
2980 {
2981 if(orig_cm->ndtype[tmp_orig_nd] != MATP_nd && orig_cm->ndtype[tmp_orig_nd] != MATR_nd)
2982 cm_Fail("ERROR 2 in cm2sub_cm_find_impossible_matr_cases() found impossible MATR case that can't be classified as case 1 or case 2, k: %d | submap->spos: %d submap->epos: %d", sub_k, submap->spos, submap->epos);
2983 if(orig_cm->ndtype[tmp_orig_nd] == MATP_nd && orig_cp9map->nd2lpos[tmp_orig_nd] >= submap->spos)
2984 cm_Fail("ERROR 3 in cm2sub_cm_find_impossible_matr_cases() found impossible MATR case that can't be classified as case 1 or case 2, k: %d | submap->spos: %d submap->epos: %d", sub_k, submap->spos, submap->epos);
2985 }
2986 else if(orig_cp9map->nd2lpos[tmp_orig_nd] == (sub_cp9map->nd2rpos[tmp_sub_nd]+submap->spos-1)) /* Case 2 above */
2987 {
2988 if(orig_cm->ndtype[tmp_orig_nd] != MATP_nd && orig_cm->ndtype[tmp_orig_nd] != MATL_nd)
2989 cm_Fail("ERROR 4 in cm2sub_cm_find_impossible_matr_cases() found impossible MATR case that can't be classified as case 1 or case 2, k: %d | submap->spos: %d submap->epos: %d", sub_k, submap->spos, submap->epos);
2990 if(orig_cm->ndtype[tmp_orig_nd] == MATP_nd && orig_cp9map->nd2rpos[tmp_orig_nd] <= submap->epos)
2991 cm_Fail("ERROR 5 in cm2sub_cm_find_impossible_matr_cases() found impossible MATR case that can't be classified as case 1 or case 2, k: %d | submap->spos: %d submap->epos: %d | orig_cp9map->nd2rpos[%d]: %d", sub_k, submap->spos, submap->epos, tmp_orig_nd, orig_cp9map->nd2rpos[tmp_orig_nd]);
2992 }
2993 }
2994 /* if we get here, we've satisfied all of our criteria */
2995 subinfo->imp_cc[sub_k] = 2;
2996 }
2997 }
2998 }
2999 }
3000 }
3001 free(nodetypes);
3002 return;
3003
3004 ERROR:
3005 cm_Fail("Memory allocation error.");
3006 }
3007
3008
3009
3010 /**************************************************************************
3011 * EPN 10.16.06
3012 * Function: check_sub_cm()
3013 *
3014 * Purpose: Given a CM and a sub CM that is supposed to mirror
3015 * the CM as closely as possible between two given consensus
3016 * columns (spos and epos), check that the sub_cm was correctly
3017 * constructed.
3018 *
3019 * 1. Build a CP9 HMM (cp9_1) from the sub_cm.
3020 * 2. Build a CP9 HMM (cp9_2) from the full cm.
3021 * 3. Reconfig cp9_2 so start node is spos and end node is epos.
3022 * 4. Check corresponding parameters of cp9_1 and cp9_2 to make
3023 * sure they're within pthresh, allowing nodes we predict
3024 * to be wrong, as stored in subinfo->imp_cc[] to be wrong,
3025 * but keeping statistics on these cases.
3026 *
3027 * Args:
3028 * orig_cm - the original, template CM
3029 * sub_cm - the sub CM built from the orig_cm
3030 * errbuf - for error messages
3031 * submap - map from orig_cm to sub_cm and vice versa
3032 * subinfo - sub cm information
3033 * pthresh - the allowed difference in probability between HMMs
3034 * print_flag - TRUE to print useful debugging info
3035 *
3036 * Returns: eslOK if CM and sub CM are "close enough" (see code)
3037 * eslFAIL otherwise, errbuf is filled
3038 */
3039 int
3040 check_sub_cm(CM_t *orig_cm, CM_t *sub_cm, char *errbuf, CMSubMap_t *submap, CMSubInfo_t *subinfo, float pthresh, int print_flag)
3041 {
3042 int status;
3043 CP9_t *sub_hmm; /* constructed CP9 HMM from the sub_cm */
3044 CP9_t *orig_hmm; /* constructed CP9 HMM from the original cm
3045 this will be reconfiged to match the sub_hmm */
3046 CP9Map_t *orig_cp9map; /* maps the orig_cm to the orig_hmm and vice versa */
3047 CP9Map_t *sub_cp9map; /* maps the sub_cm to the sub_hmm and vice versa */
3048
3049 int k;
3050 double **orig_phi;
3051 int *violation;
3052 int v_ct;
3053 int apredict_total_ct;
3054 int awrong_total_ct;
3055 int i;
3056 float diff;
3057 int nd;
3058
3059 v_ct = 0;
3060 apredict_total_ct = 0;
3061 awrong_total_ct = 0;
3062
3063 /* Build two CP9 HMMs, one for the orig_cm and one for the sub_cm */
3064 if((status = build_cp9_hmm(orig_cm, errbuf, FALSE, 0.0001, print_flag, &orig_hmm, &orig_cp9map)) != eslOK) return status;
3065 if((status = build_cp9_hmm(sub_cm, errbuf, FALSE, 0.0001, print_flag, &sub_hmm, &sub_cp9map)) != eslOK) return status;
3066 CP9Logoddsify(orig_hmm);
3067 CP9Logoddsify(sub_hmm);
3068
3069 /* Look for 'impossible' cases where we know the sub_cm
3070 * construction procedure fails, in that the distribution of transitions out of CP9 nodes
3071 * built from the sub_cm will be the same distros out of corresponding CP9 nodes built from
3072 * the full CM. */
3073 cm2sub_cm_find_impossible_misc_cases(orig_cm, sub_cm, submap, subinfo, orig_cp9map, sub_cp9map, print_flag);
3074 cm2sub_cm_find_impossible_matr_cases(orig_cm, sub_cm, submap, subinfo, orig_cp9map, sub_cp9map, print_flag);
3075
3076 /* Reconfig the orig_hmm so that it can only start in the spos node, and end from the epos node */
3077 /* Build the sub CP9 HMM by copying as much of the original cp9_hmm as possible */
3078 fill_phi_cp9(orig_hmm, &orig_phi, 1, FALSE);
3079 CP9_reconfig2sub(orig_hmm, submap->spos, submap->epos, submap->spos, submap->epos, orig_phi);
3080
3081 if(print_flag)
3082 {
3083 printf("PRINTING BUILT SUB HMM PARAMS:\n");
3084 debug_print_cp9_params(stdout, sub_hmm, TRUE);
3085 printf("DONE PRINTING BUILT SUB HMM PARAMS:\n");
3086
3087 printf("PRINTING BUILT & RECONFIGED ORIG HMM PARAMS:\n");
3088 debug_print_cp9_params(stdout, orig_hmm, TRUE);
3089 printf("DONE PRINTING BUILT & RECONFIGED ORIG HMM PARAMS:\n");
3090 }
3091
3092 /* Check the parameters of the two CP9 HMMs */
3093 if(print_flag)
3094 {
3095 printf("COMPARING CP9 HMM parameters in check_sub_cm()\n");
3096 printf("orig | sub\n");
3097 }
3098 ESL_ALLOC(violation, sizeof(int) * (submap->sub_clen+1));
3099 for(k = 0; k <= sub_hmm->M; k++)
3100 {
3101 violation[k] = FALSE;
3102 if(print_flag) printf("Node: %d\n", k);
3103 if(k > 0)
3104 {
3105 for(i = 0; i < orig_cm->abc->K; i++)
3106 {
3107 diff = orig_hmm->mat[(submap->spos+k-1)][i] - sub_hmm->mat[k][i];
3108 if(print_flag) printf("mat[%d][%d] = %8.5f | %8.5f | (%8.5f)\n", 0, i, orig_hmm->mat[(submap->spos+k-1)][i], sub_hmm->mat[k][i], diff);
3109 if((diff > 0 && diff > pthresh) || (diff < 0 && diff < (-1. * pthresh)))
3110 {
3111 ESL_FAIL(eslFAIL, errbuf, "check_sub_cm(): emission probability incorrect");
3112 }
3113 }
3114 }
3115 for(i = 0; i < orig_cm->abc->K; i++)
3116 {
3117 diff = orig_hmm->ins[(submap->spos+k-1)][i] - sub_hmm->ins[k][i];
3118 if(print_flag) printf("ins[%d][%d] = %8.5f | %8.5f | (%8.5f)\n", 0, i, orig_hmm->ins[(submap->spos+k-1)][i], sub_hmm->ins[k][i], diff);
3119 if((diff > 0 && diff > pthresh) || (diff < 0 && diff < (-1. * pthresh)))
3120 {
3121 ESL_FAIL(eslFAIL, errbuf, "check_sub_cm(): emission probability incorrect");
3122 }
3123 }
3124
3125 /* Transitions */
3126 if(print_flag) printf("\n");
3127 diff = orig_hmm->t[(submap->spos+k-1)][CTMM] - sub_hmm->t[k][CTMM];
3128 if((diff > 0 && diff > pthresh) || (diff < 0 && diff < (-1. * pthresh)))
3129 {
3130 violation[k] = TRUE;
3131 if(print_flag) printf("\tCTMM[%d] = %8.5f | %8.5f | %8.5f VIOLATION\n", k, orig_hmm->t[(submap->spos+k-1)][CTMM], sub_hmm->t[k][CTMM], diff);
3132 }
3133 else
3134 if(print_flag) printf("\tCTMM[%d] = %8.5f | %8.5f | %8.5f\n", k, orig_hmm->t[(submap->spos+k-1)][CTMM], sub_hmm->t[k][CTMM], diff);
3135
3136 diff = orig_hmm->t[(submap->spos+k-1)][CTMI] - sub_hmm->t[k][CTMI];
3137 if((diff > 0 && diff > pthresh) || (diff < 0 && diff < (-1. * pthresh)))
3138 {
3139 violation[k] = TRUE;
3140 if(print_flag) printf("\tCTMI[%d] = %8.5f | %8.5f | %8.5f VIOLATION\n", k, orig_hmm->t[(submap->spos+k-1)][CTMI], sub_hmm->t[k][CTMI], diff);
3141 }
3142 else
3143 if(print_flag) printf("\tCTMI[%d] = %8.5f | %8.5f | %8.5f\n", k, orig_hmm->t[(submap->spos+k-1)][CTMI], sub_hmm->t[k][CTMI], diff);
3144
3145 diff = orig_hmm->t[(submap->spos+k-1)][CTMD] - sub_hmm->t[k][CTMD];
3146 if((diff > 0 && diff > pthresh) || (diff < 0 && diff < (-1. * pthresh)))
3147 {
3148 violation[k] = TRUE;
3149 if(print_flag) printf("\tCTMD[%d] = %8.5f | %8.5f | %8.5f VIOLATION\n", k, orig_hmm->t[(submap->spos+k-1)][CTMD], sub_hmm->t[k][CTMD], diff);
3150 }
3151 else
3152 if(print_flag) printf("\tCTMD[%d] = %8.5f | %8.5f | %8.5f\n", k, orig_hmm->t[(submap->spos+k-1)][CTMD], sub_hmm->t[k][CTMD], diff);
3153
3154 diff = orig_hmm->t[(submap->spos+k-1)][CTIM] - sub_hmm->t[k][CTIM];
3155 if((diff > 0 && diff > pthresh) || (diff < 0 && diff < (-1. * pthresh)))
3156 {
3157 violation[k] = TRUE;
3158 if(print_flag) printf("\tCTIM[%d] = %8.5f | %8.5f | %8.5f VIOLATION\n", k, orig_hmm->t[(submap->spos+k-1)][CTIM], sub_hmm->t[k][CTIM], diff);
3159 }
3160 else
3161 if(print_flag) printf("\tCTIM[%d] = %8.5f | %8.5f | %8.5f\n", k, orig_hmm->t[(submap->spos+k-1)][CTIM], sub_hmm->t[k][CTIM], diff);
3162
3163 diff = orig_hmm->t[(submap->spos+k-1)][CTII] - sub_hmm->t[k][CTII];
3164 if((diff > 0 && diff > pthresh) || (diff < 0 && diff < (-1. * pthresh)))
3165 {
3166 violation[k] = TRUE;
3167 if(print_flag) printf("\tCTII[%d] = %8.5f | %8.5f | %8.5f VIOLATION\n", k, orig_hmm->t[(submap->spos+k-1)][CTII], sub_hmm->t[k][CTII], diff);
3168 }
3169 else
3170 if(print_flag) printf("\tCTII[%d] = %8.5f | %8.5f | %8.5f\n", k, orig_hmm->t[(submap->spos+k-1)][CTII], sub_hmm->t[k][CTII], diff);
3171
3172 diff = orig_hmm->t[(submap->spos+k-1)][CTID] - sub_hmm->t[k][CTID];
3173 if((diff > 0 && diff > pthresh) || (diff < 0 && diff < (-1. * pthresh)))
3174 {
3175 violation[k] = TRUE;
3176 if(print_flag) printf("\tCTID[%d] = %8.5f | %8.5f | %8.5f VIOLATION\n", k, orig_hmm->t[(submap->spos+k-1)][CTID], sub_hmm->t[k][CTID], diff);
3177 }
3178 else
3179 if(print_flag) printf("\tCTID[%d] = %8.5f | %8.5f | %8.5f\n", k, orig_hmm->t[(submap->spos+k-1)][CTID], sub_hmm->t[k][CTID], diff);
3180
3181 diff = orig_hmm->t[(submap->spos+k-1)][CTDM] - sub_hmm->t[k][CTDM];
3182 if((diff > 0 && diff > pthresh) || (diff < 0 && diff < (-1. * pthresh)))
3183 {
3184 violation[k] = TRUE;
3185 if(print_flag) printf("\tCTDM[%d] = %8.5f | %8.5f | %8.5f VIOLATION\n", k, orig_hmm->t[(submap->spos+k-1)][CTDM], sub_hmm->t[k][CTDM], diff);
3186 }
3187 else
3188 if(print_flag) printf("\tCTDM[%d] = %8.5f | %8.5f | %8.5f\n", k, orig_hmm->t[(submap->spos+k-1)][CTDM], sub_hmm->t[k][CTDM], diff);
3189
3190 diff = orig_hmm->t[(submap->spos+k-1)][CTDI] - sub_hmm->t[k][CTDI];
3191 if((diff > 0 && diff > pthresh) || (diff < 0 && diff < (-1. * pthresh)))
3192 {
3193 violation[k] = TRUE;
3194 if(print_flag) printf("\tCTDI[%d] = %8.5f | %8.5f | %8.5f VIOLATION\n", k, orig_hmm->t[(submap->spos+k-1)][CTDI], sub_hmm->t[k][CTDI], diff);
3195 }
3196 else
3197 if(print_flag) printf("\tCTDI[%d] = %8.5f | %8.5f | %8.5f\n", k, orig_hmm->t[(submap->spos+k-1)][CTDI], sub_hmm->t[k][CTDI], diff);
3198
3199 diff = orig_hmm->t[(submap->spos+k-1)][CTDD] - sub_hmm->t[k][CTDD];
3200 if((diff > 0 && diff > pthresh) || (diff < 0 && diff < (-1. * pthresh)))
3201 {
3202 violation[k] = TRUE;
3203 if(print_flag) printf("\tCTDD[%d] = %8.5f | %8.5f | %8.5f VIOLATION\n", k, orig_hmm->t[(submap->spos+k-1)][CTDD], sub_hmm->t[k][CTDD], diff);
3204 }
3205 else
3206 if(print_flag) printf("\tCTDD[%d] = %8.5f | %8.5f | %8.5f\n", k, orig_hmm->t[(submap->spos+k-1)][CTDD], sub_hmm->t[k][CTDD], diff);
3207
3208 if(k > 0)
3209 {
3210 diff = orig_hmm->begin[(submap->spos+k-1)] - sub_hmm->begin[k];
3211 if((diff > 0 && diff > pthresh) || (diff < 0 && diff < (-1. * pthresh)))
3212 {
3213 violation[0] = TRUE; /* begin actually has to do with the transition distro out of node 0 */
3214 if(print_flag) printf("\t beg[%d] = %8.5f | %8.5f | %8.5f VIOLATION\n", k, orig_hmm->begin[(submap->spos+k-1)], sub_hmm->begin[k], diff);
3215 }
3216 else
3217 if(print_flag) printf("\t beg[%d] = %8.5f | %8.5f | %8.5f\n", k, orig_hmm->begin[(submap->spos+k-1)], sub_hmm->begin[k], diff);
3218
3219 diff = orig_hmm->end[(submap->spos+k-1)] - sub_hmm->end[k];
3220 if((diff > 0 && diff > pthresh) || (diff < 0 && diff < (-1. * pthresh)))
3221 {
3222 violation[k] = TRUE;
3223 if(print_flag) printf("\t end[%d] = %8.5f | %8.5f | %8.5f VIOLATION\n", k, orig_hmm->end[(submap->spos+k-1)], sub_hmm->end[k], diff);
3224 }
3225 else
3226 if(print_flag) printf("\t end[%d] = %8.5f | %8.5f | %8.5f\n", k, orig_hmm->end[(submap->spos+k-1)], sub_hmm->end[k], diff);
3227 }
3228 }
3229
3230 /* Add-up the violations */
3231 for(nd = 0; nd <= (submap->epos-submap->spos+1); nd++)
3232 {
3233 if(violation[nd] && subinfo->imp_cc[nd] == 0)
3234 {
3235 v_ct++;
3236 printf("VIOLATION[%3d]: TRUE | submap->spos: %3d | submap->epos: %3d | subinfo->imp_cc: %d\n", nd, submap->spos, submap->epos, subinfo->imp_cc[nd]);
3237 }
3238 else if(violation[nd] && subinfo->imp_cc[nd] != 0)
3239 {
3240 subinfo->apredict_ct[subinfo->imp_cc[nd]]++;
3241 apredict_total_ct++;
3242 if(print_flag)
3243 printf("PREDICTED VIOLATION[%3d]: TRUE | submap->spos: %3d | submap->epos: %3d | subinfo->imp_cc: %d (apredict_total_ct: %d)\n", nd, submap->spos, submap->epos, subinfo->imp_cc[nd], apredict_total_ct);
3244 }
3245 else if(!violation[nd] && subinfo->imp_cc[nd] != 0)
3246 {
3247 subinfo->apredict_ct[subinfo->imp_cc[nd]]++;
3248 apredict_total_ct++;
3249 subinfo->awrong_ct[subinfo->imp_cc[nd]]++;
3250 awrong_total_ct++;
3251 if(print_flag) printf("NON-VIOLATION[%3d] %3d : submap->spos: %3d | submap->epos: %3d | subinfo->imp_cc: %d\n", nd, awrong_total_ct, submap->spos, submap->epos, subinfo->imp_cc[nd]);
3252 }
3253 }
3254
3255 /* Clean up and return */
3256 for(k = 0; k <= orig_hmm->M; k++)
3257 free(orig_phi[k]);
3258 free(orig_phi);
3259
3260 free(violation);
3261 FreeCPlan9(orig_hmm);
3262 FreeCPlan9(sub_hmm);
3263 FreeCP9Map(orig_cp9map);
3264 FreeCP9Map(sub_cp9map);
3265
3266 if(v_ct > 0) ESL_FAIL(eslFAIL, errbuf, "check_sub_cm(): check failed");
3267 return eslOK;
3268
3269 ERROR:
3270 ESL_FAIL(status, errbuf, "memory allocation error");
3271 return FALSE; /* never reached */
3272 }
3273
3274 /**************************************************************************
3275 * EPN 10.23.06
3276 * Function: sub_cm2cm_parsetree()
3277 * Returns: void
3278 *
3279 * Purpose: Convert a parstree to a sub_cm to a parsetree for an original CM.
3280 * We assume we're NOT in local mode. For any node n in the original
3281 * CM for which 0 states in the sub_cm map to n, we declare that
3282 * the delete state of that node was used in the converted original
3283 * CM parse (or the S, B or E state if n is not MATP, MATL or MATR).
3284 *
3285 * Args:
3286 * CM_t *orig_cm - the original, template CM
3287 * CM_t *sub_cm - the sub CM built from the orig_cm
3288 * Parsetree_t **ret_orig_tr - orig_cm parstree allocated, filled and returned here
3289 * Parsetree_t *sub_tr - the sub_cm parstree already filled
3290 * CMSubMap_t *submap - map from the sub_cm to orig_cm and vice versa
3291 * int print_flag - TRUE to print useful debugging info
3292 *
3293 * Returns: eslOK on success
3294 * eslEMEM if we run out of memory
3295 */
3296
3297 int
3298 sub_cm2cm_parsetree(CM_t *orig_cm, CM_t *sub_cm, Parsetree_t **ret_orig_tr, Parsetree_t *sub_tr,
3299 CMSubMap_t *submap, int print_flag)
3300 {
3301 int status;
3302 Parsetree_t *orig_tr; /* the parsetree we're creating for the original CM */
3303 int *ss_used; /* [0..orig_cm->nodes-1], split state idx used in converted parsetree for each orig_cm nd */
3304 int *ss_emitl; /* [0..orig_cm->nodes-1], tr->emitl[n] for each orig_cm node n */
3305 int *ss_emitr; /* [0..orig_cm->nodes-1], tr->emitr[n] for each orig_cm node n */
3306 int *il_ct; /* [0..orig_cm->nodes-1], number of times IL state of orig_cm node n was visited */
3307 int *ir_ct; /* [0..orig_cm->nodes-1], number of times IR state of orig_cm node n was visited */
3308 int *il_used; /* [0..orig_cm->nodes-1], idx of IL state in node n */
3309 int *ir_used; /* [0..orig_cm->nodes-1], idx of IR state in node n */
3310 int *tr_nd_for_bifs; /* [0..orig_cm->nodes-1], if n is a BIF node, tr node of this BIF node, else -1 */
3311 int x;
3312 int nd;
3313 int sub_v;
3314 int orig_v1;
3315 int orig_v2;
3316 int orig_nd1;
3317 int orig_nd2;
3318 int cm_nd;
3319 int i;
3320 int parent_tr_nd;
3321 ESL_STACK *pda;
3322 int pos;
3323 int ss;
3324 int on_right;
3325 int emitl_flag;
3326 int emitr_flag;
3327
3328 char **nodetypes;
3329 ESL_ALLOC(nodetypes, sizeof(char *) * 8);
3330 nodetypes[0] = "BIF";
3331 nodetypes[1] = "MATP";
3332 nodetypes[2] = "MATL";
3333 nodetypes[3] = "MATR";
3334 nodetypes[4] = "BEGL";
3335 nodetypes[5] = "BEGR";
3336 nodetypes[6] = "ROOT";
3337 nodetypes[7] = "END";
3338
3339 char **sttypes;
3340 ESL_ALLOC(sttypes, sizeof(char *) * 10);
3341 sttypes[0] = "D";
3342 sttypes[1] = "MP";
3343 sttypes[2] = "ML";
3344 sttypes[3] = "MR";
3345 sttypes[4] = "IL";
3346 sttypes[5] = "IR";
3347 sttypes[6] = "S";
3348 sttypes[7] = "E";
3349 sttypes[8] = "B";
3350 sttypes[9] = "EL";
3351
3352 if(print_flag) printf("orig_cm nodes: %d\n", orig_cm->nodes);
3353
3354 ESL_ALLOC(ss_used, sizeof(int) * orig_cm->nodes + 1);
3355 ESL_ALLOC(ss_emitl, sizeof(int) * orig_cm->nodes + 1);
3356 ESL_ALLOC(ss_emitr, sizeof(int) * orig_cm->nodes + 1);
3357 ESL_ALLOC(il_used, sizeof(int) * orig_cm->nodes + 1);
3358 ESL_ALLOC(ir_used, sizeof(int) * orig_cm->nodes + 1);
3359 ESL_ALLOC(il_ct, sizeof(int) * orig_cm->nodes + 1);
3360 ESL_ALLOC(ir_ct, sizeof(int) * orig_cm->nodes + 1);
3361 ESL_ALLOC(tr_nd_for_bifs,sizeof(int) * orig_cm->nodes + 1);
3362 /* i*_emitl[nd] is the last residue emitted by the i* state
3363 * of node nd, the first is (il_emitl[nd] - il_ct[nd] + 1)
3364 * or (ir_emitr[nd] + ir_ct[nd] - 1)
3365 */
3366
3367 for(nd = 0; nd < orig_cm->nodes; nd++)
3368 {
3369 ss_used[nd] = -1;
3370 ss_emitl[nd] = -1;
3371 ss_emitr[nd] = -1;
3372 il_ct[nd] = 0; /* the number of times the IL state was used in the sub_cm parse */
3373 ir_ct[nd] = 0; /* the number of times the IR state was used in the sub_cm parse */
3374 il_used[nd] = -1;
3375 ir_used[nd] = -1;
3376 tr_nd_for_bifs[nd] = -1; /* this will remain -1 except for bif nodes */
3377 }
3378
3379 for(x = 0; x < sub_tr->n; x++)
3380 {
3381 sub_v = sub_tr->state[x];
3382 if(print_flag) printf("x: %d sub_v: %d\n", x, sub_v);
3383 orig_v1 = submap->s2o_smap[sub_v][0];
3384 orig_v2 = submap->s2o_smap[sub_v][1];
3385 if(print_flag) printf("orig_v1: %d | orig_v2: %d\n", orig_v1, orig_v2);
3386 if(orig_v1 == -1)
3387 {
3388 if(sub_cm->sttype[sub_v] != S_st &&
3389 sub_cm->sttype[sub_v] != E_st &&
3390 sub_cm->sttype[sub_v] != B_st &&
3391 sub_cm->sttype[sub_v] != EL_st)
3392 cm_Fail("ERROR 0 in sub_cm2cm_parstree()\n");
3393 continue;
3394 }
3395 orig_nd1 = orig_cm->ndidx[orig_v1];
3396 if(orig_v2 != -1)
3397 orig_nd2 = orig_cm->ndidx[orig_v2];
3398 else
3399 orig_nd2 = -1;
3400
3401 /* No sub_cm insert states can map to 2 orig_cm inserts */
3402 if(orig_cm->sttype[orig_v1] == IL_st)
3403 {
3404 il_used[orig_nd1] = orig_v1;
3405 il_ct[orig_nd1]++;
3406 if(orig_v2 != -1)
3407 cm_Fail("ERROR 1 in sub_cm2cm_parstree()\n");
3408 }
3409 else if(orig_cm->sttype[orig_v1] == IR_st)
3410 {
3411 ir_used[orig_nd1] = orig_v1;
3412 ir_ct[orig_nd1]++;
3413 if(orig_v2 != -1)
3414 cm_Fail("ERROR 2 in sub_cm2cm_parstree()\n");
3415 }
3416 else if(sub_cm->ndtype[sub_cm->ndidx[sub_v]] == MATP_nd)
3417 {
3418 ss_used[orig_nd1] = orig_v1;
3419 if(orig_v2 != -1)
3420 cm_Fail("ERROR 3 in sub_cm2cm_parsetree()\n");
3421 }
3422 else if(orig_cm->ndtype[orig_nd1] == MATP_nd)
3423 {
3424 if(orig_v2 == -1)
3425 cm_Fail("ERROR 4 in sub_cm2cm_parsetree()\n");
3426 /* We have to figure out which MATP split state sub_v corresponds to. */
3427 if(sub_cm->ndtype[sub_cm->ndidx[sub_v]] != MATL_nd &&
3428 sub_cm->ndtype[sub_cm->ndidx[sub_v]] != MATR_nd)
3429 cm_Fail("ERROR 5 in sub_cm2cm_parsetree()\n");
3430 if(orig_cm->ndtype[orig_nd2] != MATP_nd)
3431 cm_Fail("ERROR 6 in sub_cm2cm_parsetree()\n");
3432
3433 if(sub_cm->sttype[sub_v] == D_st)
3434 {
3435 if(ss_used[orig_nd1] == -1 ||
3436 orig_cm->sttype[ss_used[orig_nd1]] == D_st)
3437 ss_used[orig_nd1] = orig_cm->nodemap[orig_nd1] + 3; /* MATP_D */
3438
3439 /* Else we do nothing, orig_cm->sttype[ss_used[orig_nd1]] is already
3440 * either a ML_st or an MR_st */
3441 }
3442 else /* sub_cm->sttype[sub_v] != D_st */
3443 {
3444 if(ss_used[orig_nd1] == -1 ||
3445 orig_cm->sttype[ss_used[orig_nd1]] == D_st)
3446 {
3447 /* Figure out if sub_v maps to the left or right half of the MATP node */
3448 if(orig_cm->sttype[orig_v1] == ML_st ||
3449 orig_cm->sttype[orig_v2] == ML_st)
3450 ss_used[orig_nd1] = orig_cm->nodemap[orig_nd1] + 1; /* MATP_ML */
3451 else if(orig_cm->sttype[orig_v1] == MR_st ||
3452 orig_cm->sttype[orig_v2] == MR_st)
3453 ss_used[orig_nd1] = orig_cm->nodemap[orig_nd1] + 2; /* MATP_MR */
3454 else
3455 cm_Fail("ERROR 7 in sub_cm2cm_parsetree()\n");
3456 }
3457 /* below is the only line we really need:
3458 else
3459 ss_used[orig_nd1] = orig_cm->nodemap[orig_nd1]; */
3460
3461 else if(orig_cm->sttype[ss_used[orig_nd1]] == ML_st) /* just for safety; should erase eventually */
3462 {
3463 if(orig_cm->sttype[orig_v1] == MR_st ||
3464 orig_cm->sttype[orig_v2] == MR_st) /* just for safety; should erase eventually */
3465 {
3466 ss_used[orig_nd1] = orig_cm->nodemap[orig_nd1]; /* MATP_MP */
3467 }
3468 else
3469 cm_Fail("ERROR 8 in sub_cm2cm_parsetree()\n");
3470 }
3471 else if(orig_cm->sttype[ss_used[orig_nd1]] == MR_st) /* just for safety; should erase eventually */
3472 {
3473 if(orig_cm->sttype[orig_v1] == ML_st ||
3474 orig_cm->sttype[orig_v2] == ML_st) /* just for safety; should erase eventually */
3475 {
3476 ss_used[orig_nd1] = orig_cm->nodemap[orig_nd1]; /* MATP_MP */
3477 }
3478 else
3479 cm_Fail("ERROR 9 in sub_cm2cm_parsetree()\n");
3480 }
3481 }
3482 }
3483 else
3484 {
3485 if(orig_v2 != -1)
3486 cm_Fail("ERROR 5 in sub_cm2cm_parsetree()\n");
3487 ss_used[orig_nd1] = orig_v1;
3488 }
3489 }
3490
3491 /* Some MATL, MATR, and MATP nodes in the orig_cm might have 0 states that map to any state
3492 * used in the sub_cm parse. If we're not allowing local begins and ends, our strategy is
3493 * to claim that in the orig_cm parse the D state of these nodes was used.
3494 */
3495 for(nd = 0; nd < orig_cm->nodes; nd++)
3496 {
3497 if(ss_used[nd] == -1)
3498 {
3499 if(orig_cm->ndtype[nd] == MATP_nd)
3500 ss_used[nd] = orig_cm->nodemap[nd] + 3; /* MATP_D */
3501 if(orig_cm->ndtype[nd] == MATL_nd ||
3502 orig_cm->ndtype[nd] == MATR_nd)
3503 ss_used[nd] = orig_cm->nodemap[nd] + 1; /* MAT{L,R}_D */
3504 if(orig_cm->ndtype[nd] == BIF_nd ||
3505 orig_cm->ndtype[nd] == BEGL_nd ||
3506 orig_cm->ndtype[nd] == BEGR_nd ||
3507 orig_cm->ndtype[nd] == END_nd)
3508 ss_used[nd] = orig_cm->nodemap[nd]; /* BIF_B, END_E or BEG{L,R}_S */
3509 }
3510 }
3511
3512 /* Determine emitl and emitr for each state in the orig_cm parse */
3513 /* This code is noticeably cleaner than the rest - it's Sean's, from
3514 * CreateEmitMap() adapted for our purposes here.
3515 */
3516
3517 pos = 1;
3518 ss = 0;
3519 if((pda = esl_stack_ICreate()) == NULL) goto ERROR;
3520 if((status = esl_stack_IPush(pda, 0)) != eslOK) goto ERROR; /* 0 = left side. 1 would = right side. */
3521 if((status = esl_stack_IPush(pda, ss)) != eslOK) goto ERROR;
3522 while (esl_stack_IPop(pda, &ss) != eslEOD)
3523 {
3524 esl_stack_IPop(pda, &on_right);
3525
3526 if (on_right)
3527 {
3528 pos += ir_ct[orig_cm->ndidx[ss]]; /* account for right inserts */
3529 if (orig_cm->sttype[ss] == MP_st || orig_cm->sttype[ss] == MR_st)
3530 pos++;
3531 ss_emitr[orig_cm->ndidx[ss]] = pos - 1;
3532 }
3533 else
3534 {
3535 ss_emitl[orig_cm->ndidx[ss]] = pos;
3536 if (orig_cm->sttype[ss] == MP_st || orig_cm->sttype[ss] == ML_st)
3537 pos++;
3538
3539 if (orig_cm->sttype[ss] == B_st)
3540 {
3541 /* push the BIF back on for its right side */
3542 if((status = esl_stack_IPush(pda, 1)) != eslOK) goto ERROR;
3543 if((status = esl_stack_IPush(pda, ss)) != eslOK) goto ERROR;
3544 /* push node index for right child */
3545 if((status = esl_stack_IPush(pda, 0)) != eslOK) goto ERROR;
3546 if((status = esl_stack_IPush(pda, orig_cm->cnum[ss])) != eslOK) goto ERROR;
3547 /* push node index for left child */
3548 if((status = esl_stack_IPush(pda, 0)) != eslOK) goto ERROR;
3549 if((status = esl_stack_IPush(pda, orig_cm->cfirst[ss])) != eslOK) goto ERROR;
3550 }
3551 else
3552 {
3553 /* push the node back on for right side */
3554 if((status = esl_stack_IPush(pda, 1)) != eslOK) goto ERROR;
3555 if((status = esl_stack_IPush(pda, ss)) != eslOK) goto ERROR;
3556 /* push split state of child node on */
3557 if (orig_cm->sttype[ss] != E_st) {
3558 if((status = esl_stack_IPush(pda, 0)) != eslOK) goto ERROR;
3559 if((status = esl_stack_IPush(pda, ss_used[orig_cm->ndidx[ss]+1])) != eslOK) goto ERROR;
3560 }
3561 }
3562 pos += il_ct[orig_cm->ndidx[ss]]; /* account for left inserts */
3563 }
3564 }
3565
3566 if(print_flag)
3567 {
3568 for(nd = 0; nd < orig_cm->nodes; nd++)
3569 {
3570 printf("ss_used[%4d] (%4s) first state(%4d) | ", nd, nodetypes[(int) orig_cm->ndtype[nd]], orig_cm->nodemap[nd]);
3571 if(ss_used[nd] != -1)
3572 printf("%4d (%2s) | L: %3d R: %3d\n", ss_used[nd], sttypes[(int) orig_cm->sttype[ss_used[nd]]], ss_emitl[nd], ss_emitr[nd]);
3573 else
3574 printf("%4d\n", -1);
3575 }
3576 for(nd = 0; nd < orig_cm->nodes; nd++)
3577 printf("il_ct[%4d] (st used: %4d) ct: %4d | ir_ct[%4d] ct: %4d (st used: %4d)\n", nd, il_used[nd], il_ct[nd], nd, ir_used[nd], ir_ct[nd]);
3578
3579 }
3580
3581 orig_tr = CreateParsetree(100);
3582 for(cm_nd = 0; cm_nd < orig_cm->nodes; cm_nd++)
3583 {
3584 emitl_flag = 0;
3585 emitr_flag = 0;
3586 if(orig_cm->sttype[ss_used[cm_nd]] == MP_st ||
3587 orig_cm->sttype[ss_used[cm_nd]] == ML_st)
3588 emitl_flag = 1;
3589 if(orig_cm->sttype[ss_used[cm_nd]] == MP_st ||
3590 orig_cm->sttype[ss_used[cm_nd]] == MR_st)
3591 emitr_flag = 1;
3592
3593 /* At least 1 state in each node must be visited (if we're not in local mode) */
3594 if(orig_cm->ndtype[cm_nd] == BEGR_nd)
3595 {
3596 parent_tr_nd = tr_nd_for_bifs[orig_cm->ndidx[orig_cm->plast[orig_cm->nodemap[cm_nd]]]];
3597 if(print_flag) printf("tr_nd_for_bifs[%d]\n", (orig_cm->ndidx[orig_cm->plast[orig_cm->nodemap[cm_nd]]]));
3598 if(print_flag) printf("parent_tr_nd for cm_nd %d: %d\n", cm_nd, parent_tr_nd);
3599 InsertTraceNode(orig_tr, parent_tr_nd, TRACE_RIGHT_CHILD, ss_emitl[cm_nd], ss_emitr[cm_nd], ss_used[cm_nd]);
3600 orig_tr->nxtr[parent_tr_nd] = orig_tr->n - 1; /* Go back and fix nxtr for the BIF parent of this BEGR */
3601 }
3602 else
3603 {
3604 InsertTraceNode(orig_tr, orig_tr->n-1, TRACE_LEFT_CHILD, ss_emitl[cm_nd], ss_emitr[cm_nd], ss_used[cm_nd]);
3605 if(print_flag) printf("inserted trace node for orig_cm st %4s | emitl: %d | emitr: %d\n", sttypes[(int) orig_cm->sttype[ss_used[cm_nd]]], ss_emitl[cm_nd], ss_emitr[cm_nd]);
3606 }
3607
3608 /* Note: if we've just added a trace node for a BIF state, it's incomplete, in that it
3609 * doesn't have the nextr correctly set. We'll go back and set this when we get to the
3610 * right child (BEGR) of this BIF */
3611 if(orig_cm->ndtype[cm_nd] == BIF_nd)
3612 {
3613 tr_nd_for_bifs[cm_nd] = orig_tr->n - 1;
3614 if(print_flag) printf("set tr_nd_for_bifs[%d]: %d\n", cm_nd, orig_tr->n);
3615 }
3616
3617 /* Add left inserts, if any */
3618 for(i = 0; i < il_ct[cm_nd]; i++)
3619 {
3620 InsertTraceNode(orig_tr, orig_tr->n-1, TRACE_LEFT_CHILD, (ss_emitl[cm_nd] + emitl_flag + i), (ss_emitr[cm_nd] - emitr_flag), il_used[cm_nd]);
3621 if(print_flag) printf("inserted trace node for orig_cm st %4s | emitl: %d | emitr: %d\n", sttypes[(int) orig_cm->sttype[il_used[cm_nd]]], orig_tr->emitl[orig_tr->n-1], orig_tr->emitr[orig_tr->n+1]);
3622 }
3623 /* Add right inserts, if any */
3624 for(i = 0; i < ir_ct[cm_nd]; i++)
3625 {
3626 InsertTraceNode(orig_tr, orig_tr->n-1, TRACE_LEFT_CHILD, (ss_emitl[cm_nd] + emitl_flag + il_ct[cm_nd]), (ss_emitr[cm_nd] - emitr_flag - i), ir_used[cm_nd]);
3627 if(print_flag) printf("inserted trace node for orig_cm st %4s | emitl: %d | emitr: %d\n", sttypes[(int) orig_cm->sttype[ir_used[cm_nd]]], orig_tr->emitl[orig_tr->n-1], orig_tr->emitr[orig_tr->n+1]);
3628 }
3629 if(print_flag) printf("END nd: %4d\n", cm_nd);
3630 }
3631 *ret_orig_tr = orig_tr;
3632
3633 free(ss_used);
3634 free(ss_emitl);
3635 free(ss_emitr);
3636 free(il_used);
3637 free(ir_used);
3638 free(il_ct);
3639 free(ir_ct);
3640 free(tr_nd_for_bifs);
3641 free(nodetypes);
3642 free(sttypes);
3643 esl_stack_Destroy(pda);
3644 return eslOK;
3645
3646 ERROR:
3647 return eslEMEM;
3648 }
3649
3650 /* Function: SubCMLogoddsify()
3651 * Date: EPN, Wed Aug 20 08:14:18 2008
3652 *
3653 * Purpose: Convert the probabilities in a sub CM built
3654 * from <mother_cm> to log-odds. Copy the <mother_cm>
3655 * parameters where possible to save time.
3656 *
3657 * Returns: eslOK on success;
3658 * Throws: eslEINCOMPAT on contract violation.
3659 * eslFAIL if we can't create CMConsensus_t object
3660 * at end of the function.
3661 */
3662 int
3663 SubCMLogoddsify(CM_t *cm, char *errbuf, CM_t *mother_cm, CMSubMap_t *mother_map)
3664 {
3665 if(!(mother_cm->flags & CMH_BITS)) ESL_FAIL(eslEINCOMPAT, errbuf, "SubCMLogoddsify(), mother_cm's CMH_BITS flag down, it's bit scores are invalid.");
3666
3667 int v, mv, x, y;
3668
3669 for (v = 0; v < cm->M; v++) {
3670 if (mother_map->s2o_id[v] == TRUE) { /* this state maps identically to a mother_cm state, copy parameters */
3671 if (cm->sttype[v] != B_st && cm->sttype[v] != E_st) {
3672 mv = mother_map->s2o_smap[v][0];
3673 esl_vec_FCopy(mother_cm->tsc[mv], cm->cnum[v], cm->tsc[v]);
3674 esl_vec_ICopy(mother_cm->itsc[mv], cm->cnum[v], cm->itsc[v]);
3675 #if eslDEBUGLEVEL >= 1
3676 for(x = 0; x < cm->cnum[v]; x++) {
3677 if(esl_FCompare_old(mother_cm->t[mv][x], cm->t[v][x], 1E-5) != eslOK) ESL_FAIL(eslEINCONCEIVABLE, errbuf, "You've got it wrong, mother_cm->t[mv:%d][x:%d] %.3f != cm->t[v:%d][x:%d] %.3f\n", mv, x, mother_cm->t[mv][x], v, x, cm->t[v][x]);
3678 }
3679 #endif
3680 }
3681 if (cm->sttype[v] == MP_st) {
3682 esl_vec_FCopy(mother_cm->esc[mv], cm->abc->K * cm->abc->K, cm->esc[v]);
3683 esl_vec_ICopy(mother_cm->iesc[mv], cm->abc->K * cm->abc->K, cm->iesc[v]);
3684 }
3685 if (cm->sttype[v] == ML_st || cm->sttype[v] == MR_st ||
3686 cm->sttype[v] == IL_st || cm->sttype[v] == IR_st) {
3687 esl_vec_FCopy(mother_cm->esc[mv], cm->abc->K, cm->esc[v]);
3688 esl_vec_ICopy(mother_cm->iesc[mv], cm->abc->K, cm->iesc[v]);
3689 }
3690 cm->beginsc[v] = mother_cm->beginsc[mv];
3691 cm->ibeginsc[v] = mother_cm->ibeginsc[mv];
3692
3693 cm->endsc[v] = mother_cm->endsc[mv];
3694 cm->iendsc[v] = mother_cm->iendsc[mv];
3695 }
3696 else { /* this state does not map identically to a mother_cm state, we have to do the work calculate the parameters */
3697 if (cm->sttype[v] != B_st && cm->sttype[v] != E_st) {
3698 for (x = 0; x < cm->cnum[v]; x++) {
3699 cm->tsc[v][x] = sreLOG2(cm->t[v][x]);
3700 cm->itsc[v][x] = Prob2Score(cm->t[v][x], 1.0);
3701 /*printf("cm->t[%4d][%2d]: %f itsc->e: %f itsc: %d\n", v, x, cm->t[v][x], Score2Prob(cm->itsc[v][x], 1.0), cm->itsc[v][x]);*/
3702 }
3703 }
3704 if (cm->sttype[v] == MP_st) {
3705 for (x = 0; x < cm->abc->K; x++) {
3706 for (y = 0; y < cm->abc->K; y++) {
3707 cm->esc[v][x*cm->abc->K+y] = sreLOG2(cm->e[v][x*cm->abc->K+y] / (cm->null[x]*cm->null[y]));
3708 cm->iesc[v][x*cm->abc->K+y] = Prob2Score(cm->e[v][x*cm->abc->K+y], (cm->null[x]*cm->null[y]));
3709 /*printf("cm->e[%4d][%2d]: %f iesc->e: %f iesc: %d\n", v, (x*cm->abc->K+y), cm->e[v][(x*cm->abc->K+y)], Score2Prob(cm->iesc[v][x*cm->abc->K+y], (cm->null[x]*cm->null[y])), cm->iesc[v][(x*cm->abc->K+y)]);*/
3710 }
3711 }
3712 }
3713 if (cm->sttype[v] == ML_st || cm->sttype[v] == MR_st ||
3714 cm->sttype[v] == IL_st || cm->sttype[v] == IR_st) {
3715 for (x = 0; x < cm->abc->K; x++) {
3716 cm->esc[v][x] = sreLOG2(cm->e[v][x] / cm->null[x]);
3717 cm->iesc[v][x] = Prob2Score(cm->e[v][x], cm->null[x]);
3718 /*printf("cm->e[%4d][%2d]: %f esc: %f null[%d]: %f\n", v, x, cm->e[v][x], cm->esc[v][x], x, cm->null[x]);*/
3719 /*printf("cm->e[%4d][%2d]: %f iesc->e: %f iesc: %d\n", v, x, cm->e[v][x], Score2Prob(cm->iesc[v][x], (cm->null[x])), cm->iesc[v][x]);*/
3720 }
3721 }
3722 /* These work even if begin/end distributions are inactive 0's,
3723 * sreLOG2 will set beginsc, endsc to -infinity.
3724 */
3725 cm->beginsc[v] = sreLOG2(cm->begin[v]);
3726 cm->ibeginsc[v] = Prob2Score(cm->begin[v], 1.0);
3727 /*printf("cm->begin[%4d]: %f ibeginsc->e: %f ibeginsc: %d\n", v, cm->begin[v], Score2Prob(cm->ibeginsc[v], 1.0), cm->ibeginsc[v]);*/
3728
3729 cm->endsc[v] = sreLOG2(cm->end[v]);
3730 cm->iendsc[v] = Prob2Score(cm->end[v], 1.0);
3731 /*printf("cm->end[%4d]: %f iendsc->e: %f iendsc: %d\n\n", v, cm->end[v], Score2Prob(cm->iendsc[v], 1.0), cm->iendsc[v]);*/
3732 }
3733 }
3734 cm->iel_selfsc = Prob2Score(sreEXP2(cm->el_selfsc), 1.0);
3735 /*printf("cm->el_selfsc: %f prob: %f cm->iel_selfsc: %d prob: %f\n", cm->el_selfsc,
3736 (sreEXP2(cm->el_selfsc)), cm->iel_selfsc, (Score2Prob(cm->iel_selfsc, 1.0)));
3737 printf("-INFTY: %d prob: %f 2^: %f\n", -INFTY, (Score2Prob(-INFTY, 1.0)), sreEXP2(-INFTY));*/
3738
3739 /* Allocate and fill optimized emission scores for this CM.
3740 * If they already exist, free them and recalculate them, slightly wasteful, oh well.
3741 */
3742 if(cm->oesc != NULL || cm->ioesc != NULL) FreeOptimizedEmitScores(cm->oesc, cm->ioesc, cm->M);
3743
3744 cm->oesc = SubFCalcAndCopyOptimizedEmitScoresFromMother(cm, mother_cm, mother_map);
3745
3746 /* EPN, Wed Aug 20 15:26:31 2008
3747 * old, slow way:
3748 * cm->ioesc = ICalcOptimizedEmitScores(cm);
3749 */
3750 cm->ioesc = ICopyOptimizedEmitScoresFromFloats(cm, cm->oesc);
3751
3752 /* Potentially, overwrite transitions with non-probabilistic
3753 * RSEARCH transitions. Currently only default transition
3754 * parameters are allowed, these are defined as DEFAULT_R*
3755 * in infernal.h
3756 * Note: This is untouched from CMLogoddsify(), we don't try
3757 * to accelerate it, it's unusual that it will be executed.
3758 */
3759 if(cm->flags & CM_RSEARCHTRANS) {
3760 float alpha = DEFAULT_RALPHA;
3761 float beta = DEFAULT_RBETA;
3762 float alphap = DEFAULT_RALPHAP;
3763 float betap = DEFAULT_RBETAP;
3764 float beginsc = DEFAULT_RBEGINSC;
3765 float endsc = DEFAULT_RENDSC;
3766 int nd;
3767 /* First do the normal transitions */
3768 for (v=0; v<cm->M; v++)
3769 {
3770 if (cm->sttype[v] != B_st && cm->sttype[v] != E_st)
3771 {
3772 for (x=0; x<cm->cnum[v]; x++)
3773 {
3774 cm->tsc[v][x] = -1. * rsearch_calculate_gap_penalty
3775 (cm->stid[v], cm->stid[cm->cfirst[v]+x],
3776 cm->ndtype[cm->ndidx[v]], cm->ndtype[cm->ndidx[cm->cfirst[v]+x]],
3777 alpha, beta, alphap, betap);
3778 /* alphas and rbetas were positive -- gap score is a penalty, so
3779 multiply by -1 */
3780 cm->itsc[v][x] = (int) floor(0.5 + INTSCALE * cm->tsc[v][x]);
3781 }
3782 }
3783 }
3784 /* Overwrite local begin and end scores */
3785 for (v=cm->M - 1; v>=0; v--) {
3786 cm->beginsc[v] = IMPOSSIBLE;
3787 cm->endsc[v] = IMPOSSIBLE;
3788 }
3789
3790 /* beginsc states */
3791 for (nd = 2; nd < cm->nodes; nd++) {
3792 if (cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATL_nd ||
3793 cm->ndtype[nd] == MATR_nd || cm->ndtype[nd] == BIF_nd)
3794 {
3795 cm->beginsc[cm->nodemap[nd]] = beginsc;
3796 cm->ibeginsc[cm->nodemap[nd]] = INTSCALE * beginsc;
3797 }
3798 }
3799
3800 /* endsc states */
3801 for (nd = 1; nd < cm->nodes; nd++) {
3802 if ((cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATL_nd ||
3803 cm->ndtype[nd] == MATR_nd || cm->ndtype[nd] == BEGL_nd ||
3804 cm->ndtype[nd] == BEGR_nd) &&
3805 cm->ndtype[nd+1] != END_nd)
3806 {
3807 cm->endsc[cm->nodemap[nd]] = endsc;
3808 cm->iendsc[cm->nodemap[nd]] = INTSCALE * endsc;
3809 }
3810 }
3811
3812 cm->flags |= CMH_LOCAL_BEGIN;
3813 cm->flags |= CMH_LOCAL_END;
3814 }
3815 /* raise flag saying we have valid log odds scores */
3816 cm->flags |= CMH_BITS;
3817
3818 /* create cm->cmcons, we expect this to be valid if we have valid log odds score */
3819 if(cm->cmcons != NULL) FreeCMConsensus(cm->cmcons);
3820 if((cm->cmcons = CreateCMConsensus(cm, cm->abc)) == NULL) return eslFAIL;
3821
3822 return eslOK;
3823 }
3824
3825 /* Function: SubFCalcAndCopyOptimizedEmitScoresFromMother()
3826 * Date: EPN, Wed Aug 20 15:40:23 2008
3827 *
3828 * Purpose: Allocate, fill and return an optimized emission score vector
3829 * of float scores for fast search/alignment.
3830 * Fill emission scores by copying them when
3831 * possible from a 'mother' CM if the current
3832 * cm is a sub CM of the mother.
3833 *
3834 * Returns: the 2D float emission score vector on success,
3835 * dies immediately on memory allocation error.
3836 */
3837 float **
3838 SubFCalcAndCopyOptimizedEmitScoresFromMother(CM_t *cm, CM_t *mother_cm, CMSubMap_t *mother_map)
3839 {
3840 int status;
3841 float **esc_vAA;
3842 ESL_DSQ a,b;
3843 int v;
3844 int cur_cell;
3845 int npairs = 0;
3846 int nsinglets = 0;
3847 float *ptr_to_start; /* points to block allocated to esc_vAA[0], nec b/c esc_vAA[0] gets set to NULL, because v == 0 is non-emitter */
3848 float **leftAA;
3849 float **rightAA;
3850 int mv;
3851
3852 /* count pairs, singlets */
3853 for(v = 0; v < cm->M; v++) {
3854 switch(cm->sttype[v]) {
3855 case IL_st:
3856 case ML_st:
3857 case IR_st:
3858 case MR_st:
3859 nsinglets++;
3860 break;
3861 case MP_st:
3862 npairs++;
3863 break;
3864 }
3865 }
3866
3867 /* set up our left and right vectors for all possible non-canonical residues,
3868 * these are calc'ed once and passed to FastPairScore*() functions to minimize
3869 * run time.
3870 */
3871 ESL_ALLOC(leftAA, sizeof(float *) * cm->abc->Kp);
3872 ESL_ALLOC(rightAA, sizeof(float *) * cm->abc->Kp);
3873 for(a = 0; a <= cm->abc->K; a++) leftAA[a] = rightAA[a] = NULL; /* canonicals and gap, left/right unnec */
3874 for(a = cm->abc->K+1; a < cm->abc->Kp-1; a++) {
3875 ESL_ALLOC(leftAA[a], sizeof(float) * cm->abc->K);
3876 ESL_ALLOC(rightAA[a], sizeof(float) * cm->abc->K);
3877 esl_vec_FSet(leftAA[a], cm->abc->K, 0.);
3878 esl_vec_FSet(rightAA[a], cm->abc->K, 0.);
3879 esl_abc_FCount(cm->abc, leftAA[a], a, 1.);
3880 esl_abc_FCount(cm->abc, rightAA[a], a, 1.);
3881 }
3882 leftAA[cm->abc->Kp-1] = rightAA[cm->abc->Kp-1] = NULL; /* missing data, left/right unnec */
3883
3884 /* precalculate possible emission scores for each state */
3885 ESL_ALLOC(esc_vAA, sizeof(float *) * (cm->M));
3886 ESL_ALLOC(esc_vAA[0], sizeof(float) * ((cm->abc->Kp * nsinglets) + (cm->abc->Kp * cm->abc->Kp * npairs)));
3887 ptr_to_start = esc_vAA[0];
3888 cur_cell = 0;
3889 for(v = 0; v < cm->M; v++) {
3890 switch(cm->sttype[v]) {
3891 case IL_st:
3892 case ML_st:
3893 case IR_st:
3894 case MR_st:
3895 esc_vAA[v] = ptr_to_start + cur_cell;
3896 cur_cell += cm->abc->Kp;
3897 if (mother_map->s2o_id[v] == TRUE) { /* this state maps identically to a mother_cm state, copy parameters */
3898 mv = mother_map->s2o_smap[v][0];
3899 esl_vec_FCopy(mother_cm->oesc[mv], cm->abc->Kp, esc_vAA[v]);
3900 }
3901 else {
3902 for(a = 0; a < cm->abc->K; a++) /* all canonical residues */
3903 esc_vAA[v][a] = cm->esc[v][a];
3904 esc_vAA[v][cm->abc->K] = IMPOSSIBLE; /* gap symbol is impossible */
3905 for(a = cm->abc->K+1; a < cm->abc->Kp-1; a++) /* all ambiguous residues */
3906 esc_vAA[v][a] = esl_abc_FAvgScore(cm->abc, a, cm->esc[v]);
3907 esc_vAA[v][cm->abc->Kp-1] = IMPOSSIBLE; /* missing data is IMPOSSIBLE */
3908 }
3909 break;
3910 case MP_st:
3911 esc_vAA[v] = ptr_to_start + cur_cell;
3912 esl_vec_FSet(esc_vAA[v], cm->abc->Kp * cm->abc->Kp, IMPOSSIBLE); /* init all cells to IMPOSSIBLE */
3913 cur_cell += cm->abc->Kp * cm->abc->Kp;
3914 if (mother_map->s2o_id[v] == TRUE) { /* this state maps identically to a mother_cm state, copy parameters */
3915 mv = mother_map->s2o_smap[v][0];
3916 esl_vec_FCopy(mother_cm->oesc[mv], cm->abc->Kp * cm->abc->Kp, esc_vAA[v]);
3917 }
3918 else {
3919 /* a is canonical, b is canonical */
3920 for(a = 0; a < cm->abc->K; a++) {
3921 for(b = 0; b < cm->abc->K; b++) {
3922 esc_vAA[v][(a * cm->abc->Kp) + b] = cm->esc[v][(a * cm->abc->K) + b];
3923 }
3924 }
3925 /* a is not canonical, b is canonical */
3926 for(a = cm->abc->K+1; a < cm->abc->Kp-1; a++) {
3927 for(b = 0; b < cm->abc->K; b++) {
3928 esc_vAA[v][(a * cm->abc->Kp) + b] = FastPairScoreLeftOnlyDegenerate(cm->abc->K, cm->esc[v], leftAA[a], b);
3929 }
3930 }
3931 /* a is canonical, b is not canonical */
3932 for(a = 0; a < cm->abc->K; a++) {
3933 for(b = cm->abc->K+1; b < cm->abc->Kp-1; b++) {
3934 esc_vAA[v][(a * cm->abc->Kp) + b] = FastPairScoreRightOnlyDegenerate(cm->abc->K, cm->esc[v], rightAA[b], a);
3935 }
3936 }
3937 /* a is not canonical, b is not canonical */
3938 for(a = cm->abc->K+1; a < cm->abc->Kp-1; a++) {
3939 for(b = cm->abc->K+1; b < cm->abc->Kp-1; b++) {
3940 esc_vAA[v][(a * cm->abc->Kp) + b] = FastPairScoreBothDegenerate(cm->abc->K, cm->esc[v], leftAA[a], rightAA[b]);
3941 }
3942 }
3943 /* everything else, when either a or b is gap or missing data, stays IMPOSSIBLE */
3944 }
3945 break;
3946 default:
3947 esc_vAA[v] = NULL;
3948 break;
3949 }
3950 }
3951 for(a = 0; a < cm->abc->Kp; a++) {
3952 if(leftAA[a] != NULL) free(leftAA[a]);
3953 if(rightAA[a] != NULL) free(rightAA[a]);
3954 }
3955 free(leftAA);
3956 free(rightAA);
3957 return esc_vAA;
3958
3959 ERROR:
3960 cm_Fail("memory allocation error.");
3961 return NULL; /* NEVERREACHED */
3962 }
3963
3964
3965 /* Function: CP9_reconfig2sub()
3966 * EPN 10.16.06
3967 *
3968 * Purpose: Given a CM Plan 9 HMM and a start position
3969 * (spos) and end position (epos) that a sub CM models,
3970 * reconfigure the HMM so that it can only start in the
3971 * node that models spos (spos_nd) end in the node that
3972 * models epos (epos_nd).
3973 *
3974 * If we're reconfiguring a CP9 HMM that ONLY models the
3975 * consensus columns spos to epos, then spos_nd == 1
3976 * and epos_nd == hmm->M, but this is not necessarily true.
3977 * We may be reconfiguring a CP9 HMM that models the
3978 * full alignment including positions before and/or after
3979 * spos and epos. In this case spos_nd == spos and
3980 * epos_nd == epos;
3981 *
3982 * Args: hmm - the CP9 model w/ data-dep prob's valid
3983 * spos - first consensus column modelled by some original
3984 * full length, template CP9 HMM that 'hmm' models.
3985 * epos - final consensus column modelled by some original
3986 * CP9 HMM that 'hmm' models.
3987 * spos_nd - the node of 'hmm' that models spos.
3988 * (1 if 'hmm' only has (epos-spos+1) nodes
3989 * (spos if 'hmm' has a node for each column of original aln)
3990 * epos_nd - the node of the 'hmm' in that models epos.
3991 * (hmm->M if 'hmm' only has (epos-spos+1) nodes
3992 * (epos if 'hmm' has a node for each column of original aln)
3993 * orig_phi - the 2D phi array for the original CP9 HMM.
3994 * Return: (void)
3995 * HMM probabilities are modified.
3996 */
3997 void
3998 CP9_reconfig2sub(CP9_t *hmm, int spos, int epos, int spos_nd,
3999 int epos_nd, double **orig_phi)
4000 {
4001 /* Make the necessary modifications. Since in cmalign --sub mode this
4002 * function will be called potentially once for each sequence, we
4003 * don't want to call CP9Logoddsify(), but rather only logoddsify
4004 * the parameters that are different.
4005 */
4006
4007 /* Configure entry.
4008 * Exactly 3 ways to start, B->M_1 (hmm->begin[1]), B->I_0 (hmm->t[0][CTMI]),
4009 * and B->D_1 (hmm->t[0][CTMD])
4010 */
4011 /* prob of starting in M_spos is (1. - prob of starting in I_spos-1) as there is no D_spos-1 -> M_spos trans */
4012
4013 if(spos > 1)
4014 {
4015 hmm->begin[spos_nd] = 1.-((orig_phi[spos-1][HMMINSERT] * (1. - hmm->t[spos-1][CTII])) +
4016 (orig_phi[spos ][HMMDELETE] - (orig_phi[spos-1][HMMINSERT] * hmm->t[spos-1][CTID])));
4017 hmm->t[spos_nd-1][CTMI] = (orig_phi[spos-1][HMMINSERT] * (1. - hmm->t[spos-1][CTII]));
4018 hmm->t[spos_nd-1][CTMD] = orig_phi[spos ][HMMDELETE] - (orig_phi[spos-1][HMMINSERT] * hmm->t[spos-1][CTID]);
4019 hmm->t[spos_nd-1][CTMM] = 0.; /* probability of going from B(M_0) to M_1 is begin[1] */
4020 hmm->t[spos_nd-1][CTMEL] = 0.; /* can't go to EL from B(M_0) */
4021 hmm->t[spos_nd-1][CTDM] = 0.; /* D_0 doesn't exist */
4022 hmm->t[spos_nd-1][CTDI] = 0.; /* D_0 doesn't exist */
4023 hmm->t[spos_nd-1][CTDD] = 0.; /* D_0 doesn't exist */
4024
4025 hmm->bsc[spos_nd] = Prob2Score(hmm->begin[1], 1.0);
4026
4027 hmm->tsc[CTMM][spos_nd-1] = -INFTY; /* probability of going from B(M_0) to M_1 is begin[1] */
4028 hmm->tsc[CTMEL][spos_nd-1] = -INFTY;
4029 hmm->tsc[CTDM][spos_nd-1] = -INFTY; /* D_0 doesn't exist */
4030 hmm->tsc[CTDI][spos_nd-1] = -INFTY; /* D_0 doesn't exist */
4031 hmm->tsc[CTDD][spos_nd-1] = -INFTY; /* D_0 doesn't exist */
4032
4033 hmm->tsc[CTMI][spos_nd-1] = Prob2Score(hmm->t[spos_nd-1][CTMI], 1.0);
4034 hmm->tsc[CTMD][spos_nd-1] = Prob2Score(hmm->t[spos_nd-1][CTMD], 1.0);
4035 }
4036
4037 if(epos < hmm->M)
4038 {
4039 hmm->end[epos_nd] = hmm->t[epos][CTMM] + hmm->t[epos][CTMD];
4040 hmm->t[epos_nd][CTDM] += hmm->t[epos][CTDD];
4041 hmm->t[epos_nd][CTIM] += hmm->t[epos][CTID];
4042 hmm->t[epos_nd][CTMM] = 0.; /* M->E is actually end[M] */
4043 hmm->t[epos_nd][CTMEL] = 0.;
4044 hmm->t[epos_nd][CTMD] = 0.; /* D_M+1 doesn't exist */
4045 hmm->t[epos_nd][CTDD] = 0.; /* D_M+1 doesn't exist */
4046 hmm->t[epos_nd][CTID] = 0.; /* D_M+1 doesn't exist */
4047
4048 hmm->esc[epos_nd] = Prob2Score(hmm->end[epos_nd], 1.0);
4049 hmm->tsc[CTDM][epos_nd] = Prob2Score(hmm->t[epos_nd][CTDM], 1.0);
4050 hmm->tsc[CTIM][epos_nd] = Prob2Score(hmm->t[epos_nd][CTIM], 1.0);
4051 hmm->tsc[CTMM][epos_nd] = -INFTY; /* M->E is actually end[M] */
4052 hmm->tsc[CTMEL][epos_nd] = -INFTY;
4053 hmm->tsc[CTMD][epos_nd] = -INFTY; /* D_M+1 doesn't exist */
4054 hmm->tsc[CTDD][epos_nd] = -INFTY; /* D_M+1 doesn't exist */
4055 hmm->tsc[CTID][epos_nd] = -INFTY; /* D_M+1 doesn't exist */
4056 }
4057 hmm->flags |= CPLAN9_HASBITS; /* raise the log-odds ready flag */
4058
4059 return;
4060 }
4061
4062 #if 0
4063 /* These two functions are not currently used, but could be useful for debugging
4064 * in the future */
4065 static void debug_print_misc_sub_cm_info(CM_t *orig_cm, CM_t *sub_cm, CMSubMap_t *submap, CP9Map_t *orig_cp9map);
4066 static void debug_sub_cm_check_all_trans(CM_t *orig_cm, CM_t *sub_cm, CMSubMap_t *submap);
4067
4068 /**************************************************************************
4069 * EPN 10.06.06
4070 * Function: debug_print_misc_sub_cm_info()
4071 **************************************************************************/
4072 static void
4073 debug_print_misc_sub_cm_info(CM_t *orig_cm, CM_t *sub_cm, CMSubMap_t *submap, CP9Map_t *orig_cp9map)
4074 {
4075 int status;
4076 int orig_il1;
4077 int orig_il2;
4078 int orig_ir1;
4079 int orig_ir2;
4080 int orig_ss;
4081
4082 char **nodetypes;
4083 char **sttypes;
4084 char **sides;
4085
4086 int orig_n1_type;
4087 int side_idx;
4088
4089 ESL_ALLOC(nodetypes, sizeof(char *) * 8);
4090 nodetypes[0] = "BIF";
4091 nodetypes[1] = "MATP";
4092 nodetypes[2] = "MATL";
4093 nodetypes[3] = "MATR";
4094 nodetypes[4] = "BEGL";
4095 nodetypes[5] = "BEGR";
4096 nodetypes[6] = "ROOT";
4097 nodetypes[7] = "END";
4098
4099 ESL_ALLOC(sttypes, sizeof(char *) * 10);
4100 sttypes[0] = "D";
4101 sttypes[1] = "MP";
4102 sttypes[2] = "ML";
4103 sttypes[3] = "MR";
4104 sttypes[4] = "IL";
4105 sttypes[5] = "IR";
4106 sttypes[6] = "S";
4107 sttypes[7] = "E";
4108 sttypes[8] = "B";
4109 sttypes[9] = "EL";
4110
4111 ESL_ALLOC(sides, sizeof(char *) * 3);
4112 sides[0] = "L";
4113 sides[1] = "R";
4114 sides[2] = "N";
4115
4116
4117 orig_il1 = submap->s2o_smap[1][0]; /* 1st of up to 2 states that maps to sub_cm's ROOT_IL */
4118 orig_il2 = submap->s2o_smap[1][1]; /* 2nd state that maps to sub_cm's ROOT_IL or -1 if only 1 maps*/
4119 orig_ir1 = submap->s2o_smap[2][0]; /* 1st of up to 2 states that maps to sub_cm's ROOT_IR */
4120 orig_ir2 = submap->s2o_smap[2][1]; /* 2nd state that maps to sub_cm's ROOT_IR or -1 if only 1 maps*/
4121
4122 /* We ASSUME that ambiguities have been removed, i.e. if two insert states map to either ROOT_IL
4123 * or ROOT_IR, one of them has been detached. We exploit this knowledge.
4124 */
4125 if(orig_il2 != -1)
4126 {
4127 if(orig_cm->sttype[orig_il1+1] == E_st)
4128 orig_il1 = orig_il2; /* orig_il1 was detached */
4129 else if(orig_cm->sttype[orig_il2+1] == E_st)
4130 {
4131 /* do nothing */
4132 }
4133 else
4134 cm_Fail("ERROR, can't determine which state was detached in debug_print_misc_sub_cm_info\n");
4135 }
4136 if(orig_ir2 != -1)
4137 {
4138 if(orig_cm->sttype[orig_ir1+1] == E_st)
4139 orig_ir1 = orig_ir2; /* orig_ir1 was detached */
4140 else if(orig_cm->sttype[orig_ir2+1] == E_st)
4141 {
4142 /* do nothing */
4143 }
4144 else
4145 cm_Fail("ERROR, can't determine which state was detached in debug_print_misc_sub_cm_info\n");
4146 }
4147
4148 /* Now orig_il1 and orig_ir1 map to the ONLY insert states that map to sub_cm
4149 * ROOT_IL and ROOT_IR respectively.
4150 */
4151 printf("10.16.06 IL1: %3d %4s %2s | start: %3d | end: %3d\n", orig_il1, nodetypes[(int) orig_cm->ndtype[orig_cm->ndidx[orig_il1]]], sttypes[(int) orig_cm->sttype[orig_il1]], submap->spos, submap->epos);
4152
4153 if(sub_cm->ndtype[1] == BIF_nd)
4154 {
4155 orig_n1_type = 0;
4156 }
4157 else
4158 {
4159 orig_n1_type = orig_cm->ndtype[orig_cm->ndidx[(submap->s2o_smap[3][0])]];
4160 }
4161
4162 if(orig_n1_type == MATP_nd && sub_cm->ndtype[1] != MATR_nd)
4163 {
4164 if(orig_cp9map->nd2lpos[orig_cm->ndidx[(submap->s2o_smap[3][0])]] == submap->spos)
4165 side_idx = 0;
4166 else if(orig_cp9map->nd2rpos[orig_cm->ndidx[(submap->s2o_smap[3][0])]] == submap->spos)
4167 side_idx = 1;
4168 else
4169 cm_Fail("ERROR MATP confusion! orig_cm node: %d | left: %d | right: %d | submap->spos: %d\n", (orig_cm->ndidx[(submap->s2o_smap[3][0])]), (orig_cp9map->nd2lpos[orig_cm->ndidx[(submap->s2o_smap[3][0])]]), (orig_cp9map->nd2rpos[orig_cm->ndidx[(submap->s2o_smap[3][0])]]), submap->spos);
4170 }
4171 else if (orig_n1_type == MATP_nd && sub_cm->ndtype[1] == MATR_nd)
4172 {
4173 if(orig_cp9map->nd2lpos[orig_cm->ndidx[(submap->s2o_smap[3][0])]] == submap->epos)
4174 side_idx = 0;
4175 else if(orig_cp9map->nd2rpos[orig_cm->ndidx[(submap->s2o_smap[3][0])]] == submap->epos)
4176 side_idx = 1;
4177 else
4178 cm_Fail("ERROR MATP confusion! orig_cm node: %d | left: %d | right: %d | submap->spos: %d\n", (orig_cm->ndidx[(submap->s2o_smap[3][0])]), (orig_cp9map->nd2lpos[orig_cm->ndidx[(submap->s2o_smap[3][0])]]), (orig_cp9map->nd2rpos[orig_cm->ndidx[(submap->s2o_smap[3][0])]]), submap->spos);
4179 }
4180 else
4181 {
4182 side_idx = 2;
4183 }
4184 printf("10.16.06 IR1: %3d %4s %2s | sub-n1: %4s | orig-n1: %4s%1s | case: ", orig_ir1, nodetypes[(int) orig_cm->ndtype[orig_cm->ndidx[orig_ir1]]], sttypes[(int) orig_cm->sttype[orig_ir1]], nodetypes[(int) sub_cm->ndtype[1]], nodetypes[(int) orig_n1_type], sides[side_idx]);
4185
4186 /* figure out 'case' of ROOT transitions */
4187 orig_ss = submap->s2o_smap[3][0]; /* orig_ss is the 1 (of possibly 2) orig_cm states that map to the first
4188 * state in sub_cm node 1 (sub_cm state 3)
4189 */
4190 if((orig_il1 < orig_ir1) && (orig_ir1 < orig_ss))
4191 printf("1A\n");
4192 if((orig_il1 < orig_ss) && (orig_ss < orig_ir1))
4193 printf("1B\n");
4194 if((orig_ss < orig_il1) && (orig_il1 < orig_ir1))
4195 printf("1C\n");
4196
4197 if((orig_ir1 < orig_il1) && (orig_il1 < orig_ss))
4198 printf("2A\n");
4199 if((orig_ir1 < orig_ss) && (orig_ss < orig_il1))
4200 printf("2B\n");
4201 if((orig_ss < orig_ir1) && (orig_ir1 < orig_il1))
4202 printf("2C\n");
4203
4204 printf("\n");
4205
4206 /* Begin 10.17.06 info */
4207 int ilmap, irmap;
4208 int ildual, irdual;
4209 int iloff, iroff;
4210 int other_insert_il;
4211 int other_insert_ir;
4212
4213 if(orig_cm->sttype[orig_il1] == IL_st)
4214 ilmap = 0; /* sides[0] = "L" */
4215 else
4216 ilmap = 1; /* sides[1] = "R" */
4217 if(orig_cm->sttype[orig_ir1] == IL_st)
4218 irmap = 0; /* sides[0] = "L" */
4219 else
4220 irmap = 1; /* sides[1] = "R" */
4221
4222 if(orig_cm->ndtype[orig_cm->ndidx[orig_il1]] == MATP_nd ||
4223 orig_cm->ndtype[orig_cm->ndidx[orig_il1]] == ROOT_nd)
4224 ildual = TRUE;
4225 else
4226 ildual = FALSE;
4227 if(orig_cm->ndtype[orig_cm->ndidx[orig_ir1]] == MATP_nd ||
4228 orig_cm->ndtype[orig_cm->ndidx[orig_ir1]] == ROOT_nd)
4229 irdual = TRUE;
4230 else
4231 irdual = FALSE;
4232
4233 iloff = -1;
4234 if(ildual == TRUE)
4235 {
4236 /* check if other insert state in orig_cm node that has insert that
4237 * maps to sub_cm ROOT_IL maps to a state in the sub_cm
4238 */
4239 if(ilmap == 0) /* ROOT_IL maps to a IL */
4240 other_insert_il = orig_il1 + 1;
4241 else /* ROOT_IL maps to a IR */
4242 other_insert_il = orig_il1 - 1;
4243 if(submap->o2s_smap[other_insert_il][0] == -1 &&
4244 submap->o2s_smap[other_insert_il][1] == -1)
4245 iloff = TRUE;
4246 else
4247 iloff = FALSE;
4248 }
4249
4250 iroff = -1;
4251 if(irdual == TRUE)
4252 {
4253 if(irmap == 0) /* ROOT_IR maps to a IL */
4254 other_insert_ir = orig_ir1 + 1;
4255 else /* ROOT_IR maps to a IR */
4256 other_insert_ir = orig_ir1 - 1;
4257 if(submap->o2s_smap[other_insert_ir][0] == -1 &&
4258 submap->o2s_smap[other_insert_ir][1] == -1)
4259 iroff = TRUE;
4260 else
4261 iroff = FALSE;
4262 }
4263
4264 CMEmitMap_t *orig_emap; /* consensus emit map for the original, template CM */
4265 int other_cc_il, other_cc_ir;
4266 orig_emap = CreateEmitMap(orig_cm);
4267 other_cc_il = -1;
4268 other_cc_ir = -1;
4269 if(ildual == TRUE)
4270 {
4271 if(orig_cm->sttype[other_insert_il] == IL_st) /* sub ROOT_IL maps to IR, other maps to IL */
4272 {
4273 other_cc_il = orig_emap->lpos[orig_cm->ndidx[other_insert_il]] + 1;
4274 if(other_cc_il > submap->spos)
4275 cm_Fail("ERROR FUNKY\n");
4276 ildual = 4;
4277 }
4278 else /* ROOT_IL maps to IL, other maps to IR */
4279 {
4280 other_cc_il = orig_emap->rpos[orig_cm->ndidx[other_insert_il]] - 1;
4281 if(other_cc_il < submap->epos)
4282 ildual = 1;
4283 if(other_cc_il == submap->epos)
4284 ildual = 2;
4285 if(other_cc_il > submap->epos)
4286 ildual = 3;
4287 }
4288 }
4289 /*printf("10.17.06 other_insert_il: %d other_cc_il: %d submap->spos: %d submap->epos: %d ildual: %d\n", other_insert_il, other_cc_il, submap->spos, submap->epos, ildual);*/
4290 if(irdual == TRUE)
4291 {
4292 if(orig_cm->sttype[other_insert_ir] == IL_st) /* sub ROOT_IR maps to IR, other maps to IL */
4293 {
4294 other_cc_ir = orig_emap->lpos[orig_cm->ndidx[other_insert_ir]] + 1;
4295 if(other_cc_ir > submap->spos)
4296 irdual = 1;
4297 if(other_cc_ir == submap->spos)
4298 irdual = 2;
4299 if(other_cc_ir < submap->spos)
4300 irdual = 3;
4301 }
4302 else /* ROOT_IR maps to IL, other maps to IR */
4303 {
4304 other_cc_ir = orig_emap->rpos[orig_cm->ndidx[other_insert_ir]] - 1;
4305 if(other_cc_ir < submap->epos)
4306 cm_Fail("ERROR FUNKY\n");
4307 irdual = 4;
4308 }
4309 }
4310 /*printf("10.17.06 other_insert_ir: %d other_cc_ir: %d submap->spos: %d submap->epos: %d irdual: %d\n", other_insert_ir, other_cc_ir, submap->spos, submap->epos, irdual);*/
4311
4312 printf("10.17.06 ilmap: %5s ildual: %2d iloff: %2d irmap: %5s irdual: %2d iroff: %2d subn1: %4s orign1: %4s%1s\n", sides[ilmap], ildual, iloff, sides[irmap], irdual, iroff, nodetypes[(int) sub_cm->ndtype[1]], nodetypes[(int) orig_n1_type], sides[side_idx]);
4313
4314 int start_flag;
4315 int v;
4316 int vend;
4317
4318 if(orig_il1 < orig_ir1)
4319 {
4320 v = orig_il1;
4321 vend = orig_ir1;
4322 }
4323 else
4324 {
4325 v = orig_ir1;
4326 vend = orig_il1;
4327 }
4328 start_flag = 0;
4329 for(; v <= vend; v++)
4330 if(orig_cm->sttype[v] == S_st)
4331 start_flag = 1;
4332 return;
4333
4334 ERROR:
4335 cm_Fail("Memory allocation error.");
4336 }
4337
4338 /**************************************************************************
4339 * EPN 11.01.06
4340 * debug_sub_cm_check_all_trans()
4341 */
4342 void
4343 debug_sub_cm_check_all_trans(CM_t *orig_cm, CM_t *sub_cm, CMSubMap_t *submap)
4344 {
4345 int status;
4346 int nd;
4347 int v;
4348 int y, yoffset;
4349 float sum, ndsum;
4350 int orig_nd, orig_v1, orig_v2;
4351
4352 char **nodetypes;
4353 ESL_ALLOC(nodetypes, sizeof(char *) * 8);
4354 nodetypes[0] = "BIF";
4355 nodetypes[1] = "MATP";
4356 nodetypes[2] = "MATL";
4357 nodetypes[3] = "MATR";
4358 nodetypes[4] = "BEGL";
4359 nodetypes[5] = "BEGR";
4360 nodetypes[6] = "ROOT";
4361 nodetypes[7] = "END";
4362
4363 char **sttypes;
4364 ESL_ALLOC(sttypes, sizeof(char *) * 10);
4365 sttypes[0] = "D";
4366 sttypes[1] = "MP";
4367 sttypes[2] = "ML";
4368 sttypes[3] = "MR";
4369 sttypes[4] = "IL";
4370 sttypes[5] = "IR";
4371 sttypes[6] = "S";
4372 sttypes[7] = "E";
4373 sttypes[8] = "B";
4374 sttypes[9] = "EL";
4375
4376 for(nd = 0; nd < sub_cm->nodes; nd++)
4377 {
4378 sum = 0.;
4379 if(sub_cm->ndtype[nd] != END_nd && sub_cm->ndtype[nd] != BIF_nd)
4380 {
4381 ndsum = 0.;
4382 v = sub_cm->nodemap[nd];
4383 while(sub_cm->ndidx[v] == nd && sub_cm->sttype[v] != IL_st && sub_cm->sttype[v] != IR_st)
4384 {
4385 sum = 0.;
4386 for(y = sub_cm->cfirst[v]; y < sub_cm->cfirst[v]+sub_cm->cnum[v]; y++)
4387 {
4388 yoffset = y - sub_cm->cfirst[v];
4389 printf("\t\tsub_cm->t[%3d][%3d]: %f\n", v, yoffset, sub_cm->t[v][yoffset]);
4390 sum += sub_cm->t[v][yoffset];
4391 }
4392 orig_v1 = submap->s2o_smap[v][0];
4393 orig_v2 = submap->s2o_smap[v][1];
4394 orig_nd = orig_cm->ndidx[orig_v1];
4395 if(sub_cm->ndtype[nd+1] != END_nd)
4396 {
4397 if(orig_v2 != -1)
4398 printf("sum t[%4d %4s %2s %2s] nd: %4d: %f\n", v, nodetypes[(int) orig_cm->ndtype[orig_nd]], sttypes[(int) orig_cm->sttype[orig_v1]], sttypes[(int) orig_cm->sttype[orig_v2]], nd, sum);
4399 else
4400 printf("sum t[%4d %4s %2s ] nd: %4d: %f\n", v, nodetypes[(int) orig_cm->ndtype[orig_nd]], sttypes[(int) orig_cm->sttype[orig_v1]], nd, sum);
4401 }
4402 ndsum += sum;
4403 v++;
4404 }
4405 if(sub_cm->ndtype[nd+1] != END_nd)
4406 printf("\tndsum t nd (%4s): %4d: %f\n", nodetypes[(int) orig_cm->ndtype[orig_nd]], nd, ndsum);
4407 }
4408 }
4409 free(nodetypes);
4410 free(sttypes);
4411 return;
4412
4413 ERROR:
4414 cm_Fail("Memory allocation error.");
4415 }
4416 #endif