Debian Codegraph @

src/cm.c

raw symbols SCIP
1 /* cm.c
2 * SRE, Sat Jul 29 09:01:20 2000 [St. Louis]
3 *
4 * Routines for dealing with the CM data structure.
5 */
6
7 #include <esl_config.h>
8 #include <p7_config.h>
9 #include "config.h"
10
11 #include <limits.h>
12 #include <math.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <time.h>
17
18 #include "easel.h"
19 #include "esl_vectorops.h"
20 #include "esl_alphabet.h"
21 #include "esl_stack.h"
22
23 #include "hmmer.h"
24
25 #include "infernal.h"
26
27 /* Function: CreateCM(); CreateCMShell(); CreateCMBody()
28 * Date: SRE, Sat Jul 29 09:02:16 2000 [St. Louis]
29 *
30 * Purpose: Create a covariance model, given the number of states
31 * and nodes that should be in it.
32 *
33 * Allocation is usually one step: CreateCM(N, M).
34 *
35 * In cmio.c, allocation is two-step: CreateCMShell()
36 * then CreateCMBody(cm, N, M). This way we can create
37 * a model, start storing header info in it (including M
38 * and N themselves), and only after we read M and N
39 * do we allocate the bulk of the model.
40 *
41 * Args: nnodes = number of nodes in the model
42 * nstates = number of states in the model
43 *
44 * Returns: ptr to allocated cm.
45 * Caller is responsible for free'ing the cm.
46 */
47 CM_t *
48 CreateCM(int nnodes, int nstates, int clen, const ESL_ALPHABET *abc)
49 {
50 CM_t *cm;
51
52 cm = CreateCMShell();
53 if (cm != NULL) CreateCMBody(cm, nnodes, nstates, clen, abc);
54 return cm;
55 }
56 CM_t *
57 CreateCMShell(void)
58 {
59 int status, z;
60 CM_t *cm;
61
62 ESL_ALLOC(cm, sizeof(CM_t));
63 /* general information: added later */
64 cm->abc = NULL;
65
66 cm->name = NULL;
67 cm->acc = NULL;
68 cm->desc = NULL;
69 cm->rf = NULL;
70 cm->consensus = NULL;
71 cm->map = NULL;
72 cm->checksum = 0;
73 /* null model information */
74 cm->null = NULL;
75
76 /* structural information */
77 cm->M = 0;
78 cm->clen = 0;
79 cm->W = 0;
80 cm->W_setby = CM_W_SETBY_INIT;
81 cm->sttype = NULL;
82 cm->ndidx = NULL;
83 cm->stid = NULL;
84 cm->cfirst = NULL;
85 cm->cnum = NULL;
86 cm->plast = NULL;
87 cm->pnum = NULL;
88 /* node->state map information */
89 cm->nodes = 0;
90 cm->nodemap = NULL;
91 cm->ndtype = NULL;
92 /* parameter information */
93 cm->t = NULL;
94 cm->e = NULL;
95 cm->begin = NULL;
96 cm->end = NULL;
97 cm->tsc = NULL;
98 cm->esc = NULL;
99 cm->oesc = NULL;
100 cm->beginsc = NULL;
101 cm->endsc = NULL;
102 cm->itsc = NULL;
103 cm->iesc = NULL;
104 cm->ioesc = NULL;
105 cm->ibeginsc = NULL;
106 cm->iendsc = NULL;
107
108 cm->lmesc = NULL;
109 cm->rmesc = NULL;
110 cm->ilmesc = NULL;
111 cm->irmesc = NULL;
112
113 cm->flags = 0;
114 cm->offset = 0;
115
116 cm->el_selfsc = sreLOG2(DEFAULT_EL_SELFPROB);
117
118 cm->qdbinfo = NULL;
119 cm->beta_W = DEFAULT_BETA_W; /* will be set when beta_W is read from cmfile */
120 cm->tau = DEFAULT_TAU; /* 1E-7 the default tau (tail loss for HMM banding) */
121 cm->maxtau = DEFAULT_MAXTAU; /* 0.1 the default max tau during HMM band tightening */
122 cm->null2_omega = V1P0_NULL2_OMEGA; /* will be redefined upon reading cmfile (if CM was created by Infernal version later than 1.0.2) */
123 cm->null3_omega = V1P0_NULL3_OMEGA; /* will be redefined upon reading cmfile (if CM was created by Infernal version later than 1.0.2) */
124 cm->cp9 = NULL;
125 cm->Lcp9 = NULL;
126 cm->Rcp9 = NULL;
127 cm->Tcp9 = NULL;
128 cm->cp9b = NULL;
129 cm->cp9map = NULL;
130 cm->root_trans = NULL;
131 cm->expA = NULL;
132 cm->smx = NULL;
133 cm->trsmx = NULL;
134 cm->hb_mx = NULL;
135 cm->hb_omx = NULL;
136 cm->hb_emx = NULL;
137 cm->hb_shmx = NULL;
138 cm->trhb_mx = NULL;
139 cm->trhb_omx = NULL;
140 cm->trhb_emx = NULL;
141 cm->trhb_shmx = NULL;
142 cm->nb_mx = NULL;
143 cm->nb_omx = NULL;
144 cm->nb_emx = NULL;
145 cm->nb_shmx = NULL;
146 cm->trnb_mx = NULL;
147 cm->trnb_omx = NULL;
148 cm->trnb_emx = NULL;
149 cm->trnb_shmx = NULL;
150 cm->cp9_mx = NULL;
151 cm->cp9_bmx = NULL;
152 cm->pbegin = DEFAULT_PBEGIN; /* summed probability of internal local begin */
153 cm->pend = DEFAULT_PEND; /* summed probability of internal local end */
154 cm->mlp7 = NULL;
155 cm->fp7 = NULL;
156
157 for (z = 0; z < CM_p7_NEVPARAM; z++) cm->fp7_evparam[z] = CM_p7_EVPARAM_UNSET;
158
159 cm->ga = 0.; /* only valid if cm->flags & CMH_GA */
160 cm->tc = 0.; /* only valid if cm->flags & CMH_TC */
161 cm->nc = 0.; /* only valid if cm->flags & CMH_NC */
162 cm->eff_nseq = 0.;
163 cm->nseq = 0;
164 cm->clen = 0;
165 cm->ctime = NULL;
166 cm->comlog = NULL;
167 cm->emap = NULL;
168 cm->cmcons = NULL;
169 cm->trp = NULL;
170
171 return cm;
172
173 ERROR:
174 cm_Fail("CreateCMShell() Memory allocation error.\n");
175 return NULL; /* never reached */
176 }
177
178 void
179 CreateCMBody(CM_t *cm, int nnodes, int nstates, int clen, const ESL_ALPHABET *abc)
180 {
181 int status;
182 int v;
183 /* alphabet, only a reference */
184 cm->abc = abc;
185 /* structural information */
186 cm->M = nstates;
187 cm->nodes = nnodes;
188 cm->clen = clen;
189
190 CMAllocNullModel(cm);
191
192 ESL_ALLOC(cm->sttype, (nstates+1) * sizeof(char));
193 ESL_ALLOC(cm->ndidx, nstates * sizeof(int));
194 ESL_ALLOC(cm->stid, (nstates+1) * sizeof(char));
195 ESL_ALLOC(cm->cfirst, nstates * sizeof(int));
196 ESL_ALLOC(cm->cnum, nstates * sizeof(int));
197 ESL_ALLOC(cm->plast, nstates * sizeof(int));
198 ESL_ALLOC(cm->pnum, nstates * sizeof(int));
199 /* node->state map information */
200 ESL_ALLOC(cm->nodemap, nnodes * sizeof(int));
201 ESL_ALLOC(cm->ndtype, nnodes * sizeof(char));
202
203 if((cm->qdbinfo = CreateCMQDBInfo(nstates, clen)) == NULL) goto ERROR;
204
205 /* parameter information */
206 /* level 1 */
207 ESL_ALLOC(cm->t, (nstates) * sizeof(float *));
208 ESL_ALLOC(cm->e, (nstates) * sizeof(float *));
209 ESL_ALLOC(cm->tsc, (nstates) * sizeof(float *));
210 ESL_ALLOC(cm->esc, (nstates) * sizeof(float *));
211 ESL_ALLOC(cm->lmesc, (nstates) * sizeof(float *));
212 ESL_ALLOC(cm->rmesc, (nstates) * sizeof(float *));
213 ESL_ALLOC(cm->itsc, (nstates) * sizeof(int *));
214 ESL_ALLOC(cm->iesc, (nstates) * sizeof(int *));
215 ESL_ALLOC(cm->ilmesc, (nstates) * sizeof(int *));
216 ESL_ALLOC(cm->irmesc, (nstates) * sizeof(int *));
217 cm->t[0] = NULL;
218 cm->e[0] = NULL;
219 cm->tsc[0] = NULL;
220 cm->esc[0] = NULL;
221 cm->lmesc[0] = NULL;
222 cm->rmesc[0] = NULL;
223 cm->itsc[0] = NULL;
224 cm->iesc[0] = NULL;
225 cm->ilmesc[0] = NULL;
226 cm->irmesc[0] = NULL;
227 ESL_ALLOC(cm->begin, (nstates) * sizeof(float));
228 ESL_ALLOC(cm->end, (nstates) * sizeof(float));
229 ESL_ALLOC(cm->beginsc, (nstates) * sizeof(float));
230 ESL_ALLOC(cm->endsc, (nstates) * sizeof(float));
231 ESL_ALLOC(cm->ibeginsc, (nstates) * sizeof(int));
232 ESL_ALLOC(cm->iendsc, (nstates) * sizeof(int));
233 /* don't allocate for cm->oesc and cm->ioesc yet, they're
234 * alloc'ed and filled by CalcOptimizedEmitScores() called
235 * in CMLogoddsify().
236 */
237
238 /* level 2 */
239 ESL_ALLOC(cm->t[0], MAXCONNECT * nstates * sizeof(float));
240 ESL_ALLOC(cm->e[0], cm->abc->K * cm->abc->K * nstates * sizeof(float));
241 ESL_ALLOC(cm->tsc[0], MAXCONNECT * nstates * sizeof(float));
242 ESL_ALLOC(cm->esc[0], cm->abc->K * cm->abc->K * nstates * sizeof(float));
243 ESL_ALLOC(cm->lmesc[0], cm->abc->Kp * nstates * sizeof(float));
244 ESL_ALLOC(cm->rmesc[0], cm->abc->Kp * nstates * sizeof(float));
245 ESL_ALLOC(cm->itsc[0], MAXCONNECT * nstates * sizeof(int));
246 ESL_ALLOC(cm->iesc[0], cm->abc->K * cm->abc->K * nstates * sizeof(int));
247 ESL_ALLOC(cm->ilmesc[0], cm->abc->Kp * nstates * sizeof(int));
248 ESL_ALLOC(cm->irmesc[0], cm->abc->Kp * nstates * sizeof(int));
249 for (v = 0; v < nstates; v++)
250 {
251 cm->e[v] = cm->e[0] + v * (cm->abc->K * cm->abc->K);
252 cm->t[v] = cm->t[0] + v * MAXCONNECT;
253 cm->tsc[v] = cm->tsc[0] + v * MAXCONNECT;
254 cm->esc[v] = cm->esc[0] + v * (cm->abc->K * cm->abc->K);
255 cm->lmesc[v] = cm->lmesc[0] + v * cm->abc->Kp;
256 cm->rmesc[v] = cm->rmesc[0] + v * cm->abc->Kp;
257 cm->iesc[v] = cm->iesc[0] + v * (cm->abc->K * cm->abc->K);
258 cm->itsc[v] = cm->itsc[0] + v * MAXCONNECT;
259 cm->ilmesc[v] = cm->ilmesc[0] + v * cm->abc->Kp;
260 cm->irmesc[v] = cm->irmesc[0] + v * cm->abc->Kp;
261 }
262
263 /* Zero model */
264
265 CMZero(cm);
266
267 /* the EL state at M is special: we only need state
268 * type info recorded, so functions looking at parsetrees
269 * can interpret what an "M" index means.
270 */
271 cm->sttype[cm->M] = EL_st;
272 cm->stid[cm->M] = END_EL;
273
274 /* don't modify cm->flags nor cm->offset, they may have been updated prior to this
275 * call, i.e. when reading a CM from a file */
276 cm->config_opts = 0;
277 cm->align_opts = 0;
278 cm->search_opts = 0;
279 cm->cp9 = NULL;
280 cm->Lcp9 = NULL;
281 cm->Rcp9 = NULL;
282 cm->Tcp9 = NULL;
283 cm->cp9b = NULL;
284 cm->cp9map = NULL;
285
286 /* we'll allocate all matrices and all cp9 related data structuers
287 * inside cm_Configure(), we need some more info about the
288 * CM besides M and nnodes to build those
289 */
290
291 /* Optional allocation, status flag dependent */
292 if (cm->flags & CMH_RF) ESL_ALLOC(cm->rf, (cm->clen+2) * sizeof(char));
293 if (cm->flags & CMH_CONS) ESL_ALLOC(cm->consensus, (cm->clen+2) * sizeof(char));
294 if (cm->flags & CMH_MAP) ESL_ALLOC(cm->map, (cm->clen+1) * sizeof(int));
295
296 return;
297
298 ERROR:
299 cm_Fail("CreateCMBody(), memory allocation error.");
300 return; /* never reached */
301 }
302
303 /* Function: CMZero()
304 * Date: SRE, Mon Jul 31 19:14:31 2000 [St. Louis]
305 *
306 * Purpose: Initialize the probability parameters and scores of a CM to zero.
307 *
308 * Returns: (void)
309 */
310 void
311 CMZero(CM_t *cm)
312 {
313 int v; /* counter over states */
314
315 for (v = 0; v < cm->M; v++) {
316 esl_vec_FSet(cm->e[v], (cm->abc->K * cm->abc->K), 0.);
317 esl_vec_FSet(cm->t[v], MAXCONNECT, 0.);
318 esl_vec_FSet(cm->tsc[v], MAXCONNECT, 0.);
319 esl_vec_FSet(cm->esc[v], (cm->abc->K * cm->abc->K), 0.);
320 esl_vec_FSet(cm->lmesc[v], cm->abc->Kp, 0.);
321 esl_vec_FSet(cm->rmesc[v], cm->abc->Kp, 0.);
322 esl_vec_ISet(cm->itsc[v], MAXCONNECT, 0);
323 esl_vec_ISet(cm->iesc[v], (cm->abc->K * cm->abc->K), 0);
324 esl_vec_ISet(cm->ilmesc[v], cm->abc->Kp, 0);
325 esl_vec_ISet(cm->irmesc[v], cm->abc->Kp, 0);
326 }
327 esl_vec_FSet(cm->begin, cm->M, 0.);
328 esl_vec_FSet(cm->end, cm->M, 0.);
329 esl_vec_FSet(cm->beginsc, cm->M, 0.);
330 esl_vec_FSet(cm->endsc, cm->M, 0.);
331 esl_vec_ISet(cm->ibeginsc, cm->M, 0);
332 esl_vec_ISet(cm->iendsc, cm->M, 0);
333 }
334
335 /* Function: CMRenormalize()
336 * Incept: SRE, Wed Aug 14 14:16:55 2002 [St. Louis]
337 *
338 * Purpose: Renormalize all the probability distributions in a CM.
339 * Used by cmio.c's flatfile parser, for example.
340 *
341 * Xref: STL6 p.108
342 */
343 void
344 CMRenormalize(CM_t *cm)
345 {
346 int v;
347
348 esl_vec_FNorm(cm->null, cm->abc->K);
349 for (v = 0; v < cm->M; v++)
350 {
351 if (cm->cnum[v] > 0 && cm->sttype[v] != B_st)
352 esl_vec_FNorm(cm->t[v], cm->cnum[v]);
353
354 if (cm->sttype[v] == ML_st || cm->sttype[v] == MR_st || cm->sttype[v] == IL_st || cm->sttype[v] == IR_st)
355 esl_vec_FNorm(cm->e[v], cm->abc->K);
356 if (cm->sttype[v] == MP_st)
357 esl_vec_FNorm(cm->e[v], cm->abc->K * cm->abc->K);
358 }
359 if (cm->flags & CMH_LOCAL_BEGIN) esl_vec_FNorm(cm->begin, cm->M);
360 if (cm->flags & CMH_LOCAL_END) cm_Fail("Renormalization of models in local end mode not supported yet");
361 }
362
363
364 /* Function: FreeCM()
365 * Date: SRE, Sat Jul 29 11:22:32 2000 [St. Louis]
366 *
367 * Purpose: Free a CM data structure.
368 *
369 * Args: cm - the model to free. (duh).
370 *
371 * Note: Remember, leave reference pointer to abc alone.
372 * This is under the application's control not ours.
373 *
374 * Returns: (void)
375 */
376 void
377 FreeCM(CM_t *cm)
378 {
379 int i;
380
381 if (! cm) return;
382
383 if (cm->smx != NULL) cm_scan_mx_Destroy (cm, cm->smx); /* free this early, it needs some info from cm->stid */
384 if (cm->trsmx != NULL) cm_tr_scan_mx_Destroy(cm, cm->trsmx); /* ditto */
385
386 if (cm->name != NULL) free(cm->name);
387 if (cm->acc != NULL) free(cm->acc);
388 if (cm->desc != NULL) free(cm->desc);
389 if (cm->rf != NULL) free(cm->rf);
390 if (cm->consensus != NULL) free(cm->consensus);
391 if (cm->map != NULL) free(cm->map);
392 if (cm->null != NULL) free(cm->null);
393
394 free(cm->sttype);
395 free(cm->ndidx);
396 free(cm->stid);
397 free(cm->cfirst);
398 free(cm->cnum);
399 free(cm->plast);
400 free(cm->pnum);
401 free(cm->nodemap);
402 free(cm->ndtype);
403
404 if (cm->lmesc != NULL) { free(cm->lmesc[0]); free(cm->lmesc); }
405 if (cm->rmesc != NULL) { free(cm->rmesc[0]); free(cm->rmesc); }
406 if (cm->ilmesc != NULL) { free(cm->ilmesc[0]); free(cm->ilmesc); }
407 if (cm->irmesc != NULL) { free(cm->irmesc[0]); free(cm->irmesc); }
408
409 if(cm->t != NULL) {
410 if(cm->t[0] != NULL) free(cm->t[0]);
411 free(cm->t);
412 }
413 if(cm->e != NULL) {
414 if(cm->e[0] != NULL) free(cm->e[0]);
415 free(cm->e);
416 }
417 if(cm->tsc != NULL) {
418 if(cm->tsc[0] != NULL) free(cm->tsc[0]);
419 free(cm->tsc);
420 }
421 if(cm->esc != NULL) {
422 if(cm->esc[0] != NULL) free(cm->esc[0]);
423 free(cm->esc);
424 }
425 if(cm->itsc != NULL) {
426 if(cm->itsc[0] != NULL) free(cm->itsc[0]);
427 free(cm->itsc);
428 }
429 if(cm->iesc != NULL) {
430 if(cm->iesc[0] != NULL) free(cm->iesc[0]);
431 free(cm->iesc);
432 }
433 if(cm->begin != NULL) free(cm->begin);
434 if(cm->end != NULL) free(cm->end);
435 if(cm->beginsc != NULL) free(cm->beginsc);
436 if(cm->endsc != NULL) free(cm->endsc);
437 if(cm->ibeginsc != NULL) free(cm->ibeginsc);
438 if(cm->iendsc != NULL) free(cm->iendsc);
439
440 if(cm->ctime != NULL) free(cm->ctime);
441 if(cm->comlog != NULL) free(cm->comlog);
442 if(cm->qdbinfo != NULL) FreeCMQDBInfo(cm->qdbinfo);
443 if(cm->cp9map != NULL) FreeCP9Map(cm->cp9map);
444 if(cm->cp9b != NULL) FreeCP9Bands(cm->cp9b);
445 if(cm->cp9 != NULL) FreeCPlan9(cm->cp9);
446 if(cm->Lcp9 != NULL) FreeCPlan9(cm->Lcp9);
447 if(cm->Rcp9 != NULL) FreeCPlan9(cm->Rcp9);
448 if(cm->Tcp9 != NULL) FreeCPlan9(cm->Tcp9);
449 if(cm->root_trans != NULL) free(cm->root_trans);
450 if(cm->hb_mx != NULL) cm_hb_mx_Destroy(cm->hb_mx);
451 if(cm->hb_omx != NULL) cm_hb_mx_Destroy(cm->hb_omx);
452 if(cm->hb_emx != NULL) cm_hb_emit_mx_Destroy(cm->hb_emx);
453 if(cm->hb_shmx != NULL) cm_hb_shadow_mx_Destroy(cm->hb_shmx);
454 if(cm->trhb_mx != NULL) cm_tr_hb_mx_Destroy(cm->trhb_mx);
455 if(cm->trhb_omx != NULL) cm_tr_hb_mx_Destroy(cm->trhb_omx);
456 if(cm->trhb_emx != NULL) cm_tr_hb_emit_mx_Destroy(cm->trhb_emx);
457 if(cm->trhb_shmx != NULL) cm_tr_hb_shadow_mx_Destroy(cm->trhb_shmx);
458 if(cm->nb_mx != NULL) cm_mx_Destroy(cm->nb_mx);
459 if(cm->nb_omx != NULL) cm_mx_Destroy(cm->nb_omx);
460 if(cm->nb_emx != NULL) cm_emit_mx_Destroy(cm->nb_emx);
461 if(cm->nb_shmx != NULL) cm_shadow_mx_Destroy(cm->nb_shmx);
462 if(cm->trnb_mx != NULL) cm_tr_mx_Destroy(cm->trnb_mx);
463 if(cm->trnb_omx != NULL) cm_tr_mx_Destroy(cm->trnb_omx);
464 if(cm->trnb_emx != NULL) cm_tr_emit_mx_Destroy(cm->trnb_emx);
465 if(cm->trnb_shmx != NULL) cm_tr_shadow_mx_Destroy(cm->trnb_shmx);
466 if(cm->cp9_mx != NULL) FreeCP9Matrix(cm->cp9_mx);
467 if(cm->cp9_bmx != NULL) FreeCP9Matrix(cm->cp9_bmx);
468 if(cm->oesc != NULL || cm->ioesc != NULL) FreeOptimizedEmitScores(cm->oesc, cm->ioesc, cm->M);
469
470 if(cm->expA != NULL) {
471 for(i = 0; i < EXP_NMODES; i++) {
472 free(cm->expA[i]);
473 }
474 free(cm->expA);
475 }
476
477 if(cm->mlp7 != NULL) {
478 p7_hmm_Destroy(cm->mlp7);
479 if(cm->fp7 == cm->mlp7) cm->fp7 = NULL;
480 cm->mlp7 = NULL;
481 }
482 if(cm->fp7 != NULL) {
483 p7_hmm_Destroy(cm->fp7);
484 cm->fp7 = NULL;
485 }
486 if(cm->emap != NULL) FreeEmitMap(cm->emap);
487 if(cm->cmcons != NULL) FreeCMConsensus(cm->cmcons);
488 if(cm->trp != NULL) cm_tr_penalties_Destroy(cm->trp);
489
490 free(cm);
491 }
492
493 /* Function: DefaultNullModel()
494 * Date: SRE, Tue Aug 1 15:31:52 2000 [St. Louis]
495 *
496 * Purpose: Allocate and initialize a float vector
497 * that will be a template null model to
498 * equiprobable (e.g. 0.25)
499 */
500 int
501 DefaultNullModel(const ESL_ALPHABET *abc, float **ret_null)
502 {
503 /* Contract check */
504 if(abc == NULL) cm_Fail("ERROR in CMCreateNullModel, cm->abc is NULL.\n");
505
506 int status;
507 float *null = NULL;
508 ESL_ALLOC(null, sizeof(float) * abc->K);
509 int x;
510 for (x = 0; x < abc->K; x++)
511 null[x] = 1./(float) abc->K;
512 esl_vec_FNorm(null, abc->K); /* completely unnecessary */
513 *ret_null = null;
514 return eslOK;
515
516 ERROR:
517 if(null != NULL) free(null);
518 return status;
519 }
520
521 /* Function: CMAllocNullModel()
522 *
523 * Purpose: Allocate the null model section of a CM
524 * and fill it with default, equiprobable
525 * null distro.
526 */
527 int
528 CMAllocNullModel(CM_t *cm)
529 {
530 int status;
531
532 /* Contract check */
533 if(cm->abc == NULL) cm_Fail("ERROR in CMAllocNullModel, cm->abc is NULL.\n");
534 if(cm->null != NULL) cm_Fail("ERROR in CMAllocNullModel, cm->null is not NULL.\n");
535
536 status = DefaultNullModel(cm->abc, &(cm->null));
537 return status;
538 }
539
540 /* Function: CMSetNullModel()
541 *
542 * Purpose: Set the null model section of a CM.
543 */
544 void
545 CMSetNullModel(CM_t *cm, float *null)
546 {
547 /* Contract check */
548 if(cm->abc == NULL) cm_Fail("ERROR in CMCreateNullModel, cm->abc is NULL.\n");
549 if(cm->null == NULL) cm_Fail("ERROR in CMSetNullModel, cm->null is NULL.\n");
550
551 int x;
552 for (x = 0; x < cm->abc->K; x++)
553 cm->null[x] = null[x];
554 esl_vec_FNorm(cm->null, cm->abc->K);
555 return;
556 }
557
558
559 /* Function: CMReadNullModel()
560 * EPN 10.19.05
561 * based on SRE's HMMER's cm.c's P7ReadNullModel()
562 *
563 * Purpose: Read a CM null model from a file.
564 * ret_null is filled with a newly allocated
565 * float vector that is the null model.
566 *
567 * Returns: eslOK on success.
568 */
569 int
570 CMReadNullModel(const ESL_ALPHABET *abc, char *nullfile, float **ret_null)
571 {
572 FILE *fp = NULL;
573 float *null = NULL;
574 char *buf = NULL;
575 char *s;
576 int n; /* length of buf */
577 int x;
578 char *tok;
579 float sum;
580 int status;
581
582 /* Contract check */
583 if (!abc) cm_Fail("ERROR in CMReadNullModel, abc is NULL.\n");
584
585 ESL_ALLOC(null, sizeof(float) * abc->K);
586 n = 0;
587 sum = 0.;
588 /* Expects a file with cm->abc->K lines that don't begin with "# ".
589 * The first token of each of these 4 lines is read as
590 * the background probability of A, C, G, and U (in that order)
591 * Then does a check to make sure the 4 read in values
592 * sum to 1.0 exactly.
593 */
594 if ((fp = fopen(nullfile, "r")) == NULL)
595 cm_Fail("Failed to open null model file %s\n", nullfile);
596
597 /* parse the file */
598 x = 0;
599 while(x < abc->K) {
600 if((status = esl_fgets(&buf, &n, fp)) != eslOK) goto ERROR;
601 s = buf;
602 if((status = esl_strtok(&s, " \t\n", &tok)) != eslOK) goto ERROR;
603 if(strcmp(tok, "#") != 0)
604 {
605 null[x] = atof(tok);
606 sum += null[x];
607 x++;
608 }
609 }
610 /*fragile*/
611 if(sum > 1.00001 || sum < 0.99999)
612 cm_Fail("%s is not in CM null model file format.\nThere are not %d background probabilities that sum to exactly 1.0", nullfile, abc->K);
613 esl_vec_FNorm(null, abc->K);
614
615 *ret_null = null;
616 free(buf);
617 fclose(fp);
618 return eslOK;
619
620 ERROR:
621 *ret_null = NULL; // no pun intended
622 fclose(fp);
623 free(buf);
624 free(null);
625 return status;
626 }
627
628 /* Function: CMSimpleProbify()
629 * Date: SRE, Tue Aug 1 11:07:17 2000 [St. Louis]
630 *
631 * Purpose: Convert a counts-based CM to probability form, using
632 * a plus-one Laplace prior.
633 */
634 void
635 CMSimpleProbify(CM_t *cm)
636 {
637 int v,x;
638
639 for (v = 0; v < cm->M; v++)
640 {
641 /* Transitions. B, E have no transition probabilities.
642 */
643 if (cm->sttype[v] != B_st && cm->sttype[v] != E_st)
644 {
645 for (x = 0; x < cm->cnum[v]; x++) cm->t[v][x] += 1.0; /* Laplace prior */
646 esl_vec_FNorm(cm->t[v], cm->cnum[v]); /* normalize to a probability */
647 }
648
649 /* Emissions.
650 */
651 if (cm->sttype[v] == MP_st)
652 {
653 for (x = 0; x < cm->abc->K*cm->abc->K; x++) cm->e[v][x] += 1.0;
654 esl_vec_FNorm(cm->e[v], cm->abc->K*cm->abc->K);
655 }
656 else if (cm->sttype[v] == ML_st || cm->sttype[v] == MR_st ||
657 cm->sttype[v] == IL_st || cm->sttype[v] == IR_st)
658 {
659 for (x = 0; x < cm->abc->K; x++) cm->e[v][x] += 1.0;
660 esl_vec_FNorm(cm->e[v], cm->abc->K);
661 }
662 }
663 }
664
665 /* Function: rsearch_CMProbifyEmissions()
666 * Date: EPN, Wed Mar 14 06:14:51 2007
667 *
668 * Purpose: Convert emissions in a counts-based CM built from a single sequence,
669 * we expect 1 count in each vector to be 1.0, all others 0.0,
670 * to probability form, using a RIBOSUM matrix with background
671 * and target frequencies.
672 *
673 * The code that does this in RSEARCH is buildcm.c::SingleSequenceLogoddsify(),
674 * but that's different in that it fills in log odds scores, here we
675 * fill in probabilities, derived from the RIBOSUM log odds scores.
676 *
677 * Returns: <eslOK> on success.
678 *
679 * Throws: <eslEINVAL> if an emission vector does not have exactly 1 non-zero
680 * count that is exactly 1.0 (or within 0.000001 of it)
681 */
682 int
683 rsearch_CMProbifyEmissions(CM_t *cm, fullmat_t *fullmat)
684 {
685 int v,x,y;
686 int cur_emission;
687 float thresh;
688 int found_ct_flag;
689 thresh = 0.000001;
690
691
692
693 /* Check the contract. */
694 if(fullmat->scores_flag) ESL_EXCEPTION(eslEINVAL, "in rsearch_CMProbifyEmissions(), matrix is in log odds mode, it should be in probs mode");
695 if(!(cm->flags & CM_RSEARCHEMIT)) ESL_EXCEPTION(eslEINVAL, "in rsearch_CMProbifyEmissions(), CM_RSEARCHEMIT flag is down");
696
697 for (v = 0; v < cm->M; v++)
698 {
699 found_ct_flag = FALSE;
700 if (cm->stid[v] == MATP_MP)
701 {
702 /* First, figure out which letter was in the query */
703
704 for (x=0; x<cm->abc->K; x++)
705 for (y=0; y<cm->abc->K; y++)
706 if (fabs(cm->e[v][x*cm->abc->K+y] - 0.) > thresh)
707 {
708 if(found_ct_flag)
709 {
710 for (x=0; x<cm->abc->K; x++)
711 for (y=0; y<cm->abc->K; y++)
712 printf("cm->e[v:%d][%d]: %f\n", v, (x*cm->abc->K+y), cm->e[v][(x*cm->abc->K+y)]);
713 ESL_EXCEPTION(eslEINVAL, "cm->e[v:%d] a MATP_MP has > 1 non-zero count", v);
714 }
715 cur_emission = numbered_basepair(cm->abc->sym[x], cm->abc->sym[y]);
716 found_ct_flag = TRUE;
717 }
718 /* Now, set emission probs as target probs in correct cells of score matrix */
719 for (x=0; x<cm->abc->K*cm->abc->K; x++)
720 cm->e[v][x] = fullmat->paired->matrix[matrix_index(cur_emission, x)];
721 esl_vec_FNorm(cm->e[v], cm->abc->K*cm->abc->K);
722 }
723 else if (cm->stid[v] == MATL_ML || cm->stid[v] == MATR_MR)
724 {
725 for (x=0; x<cm->abc->K; x++)
726 if (fabs(cm->e[v][x] - 0.) > thresh)
727 {
728 if(found_ct_flag) {
729 for (y=0; y<cm->abc->K; y++) printf("cm->e[v:%d][%d]: %f\n", v, x, cm->e[v][x]);
730 ESL_EXCEPTION(eslEINVAL, "cm->e[v:%d] a MAT{L,R}_M{L,R} has > 1 non-zero count", v);
731 }
732 cur_emission = numbered_nucleotide(cm->abc->sym[x]);
733 found_ct_flag = TRUE;
734 }
735 /* Now, set emission probs as target probs in correct cells of score matrix */
736 for (x=0; x<cm->abc->K; x++)
737 cm->e[v][x] = fullmat->unpaired->matrix[matrix_index(cur_emission, x)];
738 esl_vec_FNorm(cm->e[v], cm->abc->K);
739 }
740 else if (cm->stid[v] == MATP_ML || cm->stid[v] == MATP_MR)
741 {
742 /* RSEARCH technique: determine residue emitted to left and right,
743 * use target freqs from unpaired matrix for this residue.
744 * Alternative technique: determine residue emitted to left and right,
745 * marginalize target freqs from paired matrix for this residue.
746 * RSEARCH technique currently implemented */
747 for (x=0; x<cm->abc->K; x++)
748 for (y=0; y<cm->abc->K; y++)
749 {
750 if (cm->stid[v] == MATP_ML && (fabs(cm->e[(v-1)][x*cm->abc->K + y] - 0.) > thresh))
751 cur_emission = numbered_nucleotide(cm->abc->sym[x]);
752 else if (cm->stid[v] == MATP_MR && (fabs(cm->e[(v-2)][x*cm->abc->K + y] - 0.) > thresh))
753 cur_emission = numbered_nucleotide(cm->abc->sym[y]);
754 /* We don't have to check we have only 1 non-zero count, we've already
755 * done so when we filled e for the MATP_MP in the same node as v */
756 }
757 /* fill emission probs */
758 for (x=0; x<cm->abc->K; x++)
759 cm->e[v][x] = fullmat->unpaired->matrix[matrix_index(cur_emission, x)];
760 esl_vec_FNorm(cm->e[v], cm->abc->K);
761 }
762 else if (cm->sttype[v] == IL_st || cm->sttype[v] == IR_st)
763 {
764 /* Don't give any score for emissions matching to an Insert state,
765 * but make sure we don't have any counts in any of these guys */
766 for (x = 0; x < cm->abc->K; x++)
767 if(fabs(cm->e[v][x] - 0.) > thresh) ESL_EXCEPTION(eslEINVAL, "cm->e[v:%d] an I{L,R} has > 0 non-zero count", v);
768 esl_vec_FNorm(cm->e[v], cm->abc->K); /* these will have all been zero */
769 }
770 }
771 return eslOK;
772 }
773
774 /* Function: CMLogoddsify()
775 * Date: SRE, Tue Aug 1 15:18:26 2000 [St. Louis]
776 *
777 * Purpose: Convert the probabilities in a CM to log-odds.
778 * Then create consensus data in cm->cmcons.
779 *
780 * Returns: eslOK on success; eslFAIL if we can't create
781 * cmcons.
782 */
783 int
784 CMLogoddsify(CM_t *cm)
785 {
786 /*printf("in CMLogoddsify()\n");*/
787 int v, x, y;
788
789 /* zero lmesc, rmesc, we'll sum up probs then convert to scores */
790 esl_vec_FSet(cm->lmesc[0], cm->M * cm->abc->Kp, 0.);
791 esl_vec_FSet(cm->rmesc[0], cm->M * cm->abc->Kp, 0.);
792
793 for (v = 0; v < cm->M; v++) {
794 /* fill in unused marginal scores */
795 cm->lmesc[v][cm->abc->K] = cm->rmesc[v][cm->abc->K] = IMPOSSIBLE; /* gap */
796 cm->lmesc[v][cm->abc->Kp-2] = cm->rmesc[v][cm->abc->Kp-2] = IMPOSSIBLE; /* no-residue '*' */
797 cm->lmesc[v][cm->abc->Kp-1] = cm->rmesc[v][cm->abc->Kp-1] = IMPOSSIBLE; /* missing data */
798 cm->ilmesc[v][cm->abc->K] = cm->irmesc[v][cm->abc->K] = -INFTY; ; /* gap */
799 cm->ilmesc[v][cm->abc->Kp-2] = cm->irmesc[v][cm->abc->Kp-2] = -INFTY; ; /* no-residue '*' */
800 cm->ilmesc[v][cm->abc->Kp-1] = cm->irmesc[v][cm->abc->Kp-1] = -INFTY; ; /* missing data */
801
802 if (cm->sttype[v] != B_st && cm->sttype[v] != E_st) {
803 for (x = 0; x < cm->cnum[v]; x++) {
804 cm->tsc[v][x] = sreLOG2(cm->t[v][x]);
805 cm->itsc[v][x] = Prob2Score(cm->t[v][x], 1.0);
806 /*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]);*/
807 }
808 }
809 if (cm->sttype[v] == MP_st) {
810 for (x = 0; x < cm->abc->K; x++) {
811 for (y = 0; y < cm->abc->K; y++) {
812 cm->esc[v][x*cm->abc->K+y] = sreLOG2 (cm->e[v][x*cm->abc->K+y] / (cm->null[x]*cm->null[y]));
813 cm->iesc[v][x*cm->abc->K+y] = Prob2Score(cm->e[v][x*cm->abc->K+y], (cm->null[x]*cm->null[y]));
814 /* 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)]); */
815 cm->lmesc[v][x] += cm->e[v][x*cm->abc->K+y];
816 cm->rmesc[v][y] += cm->e[v][x*cm->abc->K+y];
817 }
818 }
819 for (x = 0; x < cm->abc->K; x++) {
820 cm->ilmesc[v][x] = Prob2Score(cm->lmesc[v][x], cm->null[x]);
821 cm->irmesc[v][x] = Prob2Score(cm->rmesc[v][x], cm->null[x]);
822 cm->lmesc[v][x] = sreLOG2 (cm->lmesc[v][x] / cm->null[x]);
823 cm->rmesc[v][x] = sreLOG2 (cm->rmesc[v][x] / cm->null[x]);
824 }
825 /* handle degeneracies */
826 esl_abc_FExpectScVec(cm->abc, cm->lmesc[v], cm->null);
827 esl_abc_FExpectScVec(cm->abc, cm->rmesc[v], cm->null);
828 esl_abc_IExpectScVec(cm->abc, cm->ilmesc[v], cm->null);
829 esl_abc_IExpectScVec(cm->abc, cm->irmesc[v], cm->null);
830 }
831 else if (cm->sttype[v] == ML_st || cm->sttype[v] == MR_st || cm->sttype[v] == IL_st || cm->sttype[v] == IR_st) {
832 for (x = 0; x < cm->abc->K; x++) {
833 cm->esc[v][x] = sreLOG2(cm->e[v][x] / cm->null[x]);
834 cm->iesc[v][x] = Prob2Score(cm->e[v][x], cm->null[x]);
835 /*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]);*/
836 /*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]);*/
837 }
838 /* handle marginals, differently for L and R states */
839 if(cm->sttype[v] == ML_st || cm->sttype[v] == IL_st) {
840 for (x = 0; x < cm->abc->K; x++) {
841 cm->lmesc[v][x] = cm->esc[v][x];
842 cm->ilmesc[v][x] = cm->iesc[v][x];
843 cm->rmesc[v][x] = 0.;
844 cm->irmesc[v][x] = 0;
845 }
846 esl_abc_FExpectScVec(cm->abc, cm->lmesc[v], cm->null);
847 esl_abc_IExpectScVec(cm->abc, cm->ilmesc[v], cm->null);
848 esl_vec_FSet(cm->rmesc[v] + cm->abc->K+1, ((cm->abc->Kp-3) - cm->abc->K), 0.);
849 esl_vec_ISet(cm->irmesc[v] + cm->abc->K+1, ((cm->abc->Kp-3) - cm->abc->K), 0);
850 }
851 else if(cm->sttype[v] == MR_st || cm->sttype[v] == IR_st) {
852 for (x = 0; x < cm->abc->K; x++) {
853 cm->lmesc[v][x] = 0.;
854 cm->ilmesc[v][x] = 0;
855 cm->rmesc[v][x] = cm->esc[v][x];
856 cm->irmesc[v][x] = cm->iesc[v][x];
857 }
858 esl_vec_FSet(cm->lmesc[v] + cm->abc->K+1, ((cm->abc->Kp-3) - cm->abc->K), 0.);
859 esl_vec_ISet(cm->ilmesc[v] + cm->abc->K+1, ((cm->abc->Kp-3) - cm->abc->K), 0);
860 esl_abc_FExpectScVec(cm->abc, cm->rmesc[v], cm->null);
861 esl_abc_IExpectScVec(cm->abc, cm->irmesc[v], cm->null);
862 }
863 }
864
865 /* These work even if begin/end distributions are inactive 0's,
866 * sreLOG2 will set beginsc, endsc to -infinity.
867 */
868 cm->beginsc[v] = sreLOG2(cm->begin[v]);
869 cm->ibeginsc[v] = Prob2Score(cm->begin[v], 1.0);
870 /*printf("cm->begin[%4d]: %f ibeginsc->e: %f ibeginsc: %d\n", v, cm->begin[v], Score2Prob(cm->ibeginsc[v], 1.0), cm->ibeginsc[v]);*/
871
872 cm->endsc[v] = sreLOG2(cm->end[v]);
873 cm->iendsc[v] = Prob2Score(cm->end[v], 1.0);
874 /*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]);*/
875 }
876
877 cm->iel_selfsc = Prob2Score(sreEXP2(cm->el_selfsc), 1.0);
878 /*printf("cm->el_selfsc: %f prob: %f cm->iel_selfsc: %d prob: %f\n", cm->el_selfsc,
879 (sreEXP2(cm->el_selfsc)), cm->iel_selfsc, (Score2Prob(cm->iel_selfsc, 1.0)));
880 printf("-INFTY: %d prob: %f 2^: %f\n", -INFTY, (Score2Prob(-INFTY, 1.0)), sreEXP2(-INFTY));*/
881
882 /* Allocate and fill optimized emission scores for this CM.
883 * If they already exist, free them and recalculate them, slightly wasteful, oh well.
884 */
885 if(cm->oesc != NULL || cm->ioesc != NULL) FreeOptimizedEmitScores(cm->oesc, cm->ioesc, cm->M);
886 cm->oesc = FCalcOptimizedEmitScores(cm);
887 /* EPN, Wed Aug 20 15:26:01 2008
888 * old, slow way:
889 * cm->ioesc = ICalcOptimizedEmitScores(cm);
890 */
891 cm->ioesc = ICopyOptimizedEmitScoresFromFloats(cm, cm->oesc);
892
893 /* Potentially, overwrite transitions with non-probabilistic
894 * RSEARCH transitions. Currently only default transition
895 * parameters are allowed, these are defined as DEFAULT_R*
896 * in infernal.h */
897 if(cm->flags & CM_RSEARCHTRANS)
898 {
899 float alpha = DEFAULT_RALPHA;
900 float beta = DEFAULT_RBETA;
901 float alphap = DEFAULT_RALPHAP;
902 float betap = DEFAULT_RBETAP;
903 float beginsc = DEFAULT_RBEGINSC;
904 float endsc = DEFAULT_RENDSC;
905 int nd;
906 /* First do the normal transitions */
907 for (v=0; v<cm->M; v++)
908 {
909 if (cm->sttype[v] != B_st && cm->sttype[v] != E_st)
910 {
911 for (x=0; x<cm->cnum[v]; x++)
912 {
913 cm->tsc[v][x] = -1. * rsearch_calculate_gap_penalty
914 (cm->stid[v], cm->stid[cm->cfirst[v]+x],
915 cm->ndtype[cm->ndidx[v]], cm->ndtype[cm->ndidx[cm->cfirst[v]+x]],
916 alpha, beta, alphap, betap);
917 /* alphas and rbetas were positive -- gap score is a penalty, so
918 multiply by -1 */
919 cm->itsc[v][x] = INTSCALE * cm->tsc[v][x];
920 }
921 }
922 }
923 /* Overwrite local begin and end scores */
924 for (v=cm->M - 1; v>=0; v--) {
925 cm->beginsc[v] = IMPOSSIBLE;
926 cm->endsc[v] = IMPOSSIBLE;
927 }
928
929 /* beginsc states */
930 for (nd = 2; nd < cm->nodes; nd++) {
931 if (cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATL_nd ||
932 cm->ndtype[nd] == MATR_nd || cm->ndtype[nd] == BIF_nd)
933 {
934 cm->beginsc[cm->nodemap[nd]] = beginsc;
935 cm->ibeginsc[cm->nodemap[nd]] = INTSCALE * beginsc;
936 }
937 }
938
939 /* endsc states */
940 for (nd = 1; nd < cm->nodes; nd++) {
941 if ((cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATL_nd ||
942 cm->ndtype[nd] == MATR_nd || cm->ndtype[nd] == BEGL_nd ||
943 cm->ndtype[nd] == BEGR_nd) &&
944 cm->ndtype[nd+1] != END_nd)
945 {
946 cm->endsc[cm->nodemap[nd]] = endsc;
947 cm->iendsc[cm->nodemap[nd]] = INTSCALE * endsc;
948 }
949 }
950
951 cm->flags |= CMH_LOCAL_BEGIN;
952 cm->flags |= CMH_LOCAL_END;
953 }
954
955 /* raise flag saying we have valid log odds scores */
956 cm->flags |= CMH_BITS;
957
958 /* create cm->cmcons, we expect this to be valid if we have valid log odds score */
959 if(cm->cmcons != NULL) FreeCMConsensus(cm->cmcons);
960 if((cm->cmcons = CreateCMConsensus(cm, cm->abc)) == NULL) return eslFAIL;
961
962 return eslOK;
963 }
964
965
966
967 /* Function: CountStatetype(), CMSubtreeCountStatetype(), CMSegmentCountStatetype
968 * Date: SRE, Wed Aug 2 09:15:00 2000 [St. Louis]
969 *
970 * Purpose: Conveniences for counting the # of occurrences
971 * of a particular state type in a CM. Useful for
972 * "how many bifurcations does this model have", etc.
973 *
974 * CMSubtreeCountStatetype() only counts underneath
975 * a particular subtree rooted at state v
976 *
977 * Args: cm - the model
978 * r - the root of the subtree to start from (inclusive)
979 * z - end of the subtree to stop at (inclusive)
980 * type - a state type (e.g. E_st or MP_st)
981 *
982 * Returns: how many states of that type are in the model
983 */
984 int
985 CMSegmentCountStatetype(CM_t *cm, int r, int z, char type)
986 {
987 int count = 0;
988 int v;
989 for (v = r; v <= z; v++)
990 if (cm->sttype[v] == type) count++;
991 return count;
992 }
993 int
994 CMSubtreeCountStatetype(CM_t *cm, int v, char type)
995 {
996 int unsatisfied_starts = 1;
997 int count = 0;
998
999 while (unsatisfied_starts) {
1000 if (cm->sttype[v] == B_st) unsatisfied_starts++;
1001 if (cm->sttype[v] == E_st) unsatisfied_starts--;
1002 if (cm->sttype[v] == type) count++;
1003 v++;
1004 }
1005 return count;
1006 }
1007 int
1008 CMCountStatetype(CM_t *cm, char type)
1009 {
1010 return CMSubtreeCountStatetype(cm, 0, type);
1011 }
1012 int
1013 CMSubtreeFindEnd(CM_t *cm, int r)
1014 {
1015 int unsatisfied_starts = 1;
1016
1017 while (unsatisfied_starts) {
1018 if (cm->sttype[r] == B_st) unsatisfied_starts++;
1019 if (cm->sttype[r] == E_st) unsatisfied_starts--;
1020 r++;
1021 }
1022 return (r-1);
1023 }
1024 int
1025 CMCountNodetype(CM_t *cm, char type)
1026 {
1027 int nd;
1028 int count = 0;
1029 for(nd = 0; nd < cm->nodes; nd++) {
1030 if(cm->ndtype[nd] == type) count++;
1031 }
1032 return count;
1033 }
1034 int
1035 CMSubtreeCountNodetype(CM_t *cm, int v, char type)
1036 {
1037 int unsatisfied_starts = 1;
1038 int count = 0;
1039
1040 while (unsatisfied_starts) {
1041 if (cm->sttype[v] == B_st) unsatisfied_starts++;
1042 if (cm->sttype[v] == E_st) unsatisfied_starts--;
1043 if (cm->stid[v] == type) count++;
1044 v++;
1045 }
1046 return count;
1047 }
1048
1049 /* Function: CalculateStateIndex()
1050 * Date: SRE, Mon Jul 31 15:37:55 2000 [St. Louis]
1051 *
1052 * Purpose: Given a node index and a unique state type, use the CM's
1053 * nodemap to calculate and return a state index in the CM.
1054 *
1055 * Doesn't check that the node type matches what's implied
1056 * by the utype! (e.g., if you pass utype==MATP_MP, the node
1057 * had better be a MATP.)
1058 *
1059 * Args: cm - the covariance model
1060 * node - node index, 0..cm->nodes-1
1061 * utype - unique statetype, e.g. MATP_MP
1062 *
1063 * Returns: a state index, 0..cm->M-1
1064 *
1065 * Used in: cm_modelmaker.c:transmogrify()
1066 */
1067 int
1068 CalculateStateIndex(CM_t *cm, int node, char utype)
1069 {
1070 int base;
1071
1072 base = cm->nodemap[node];
1073 switch (utype) {
1074 case ROOT_S: return base;
1075 case ROOT_IL: return base+1;
1076 case ROOT_IR: return base+2;
1077 case BEGL_S: return base;
1078 case BEGR_S: return base;
1079 case BEGR_IL: return base+1;
1080 case MATP_MP: return base;
1081 case MATP_ML: return base+1;
1082 case MATP_MR: return base+2;
1083 case MATP_D: return base+3;
1084 case MATP_IL: return base+4;
1085 case MATP_IR: return base+5;
1086 case MATL_ML: return base;
1087 case MATL_D: return base+1;
1088 case MATL_IL: return base+2;
1089 case MATR_MR: return base;
1090 case MATR_D: return base+1;
1091 case MATR_IR: return base+2;
1092 case END_E: return base;
1093 case BIF_B: return base;
1094 default: cm_Fail("bogus utype %d in CalculateStateIndex()", utype);
1095 }
1096 return base; /* not used */
1097 }
1098
1099 /* Function: TotalStatesInNode(), SplitStatesInNode(), InsertStatesInNode()
1100 * Incept: SRE, Thu Aug 8 09:57:59 2002 [St. Louis]
1101 *
1102 * Purpose: Returns the number of states in a node type.
1103 *
1104 * Args: ndtype - type of node (cm->ndtype[])
1105 */
1106 int
1107 TotalStatesInNode(int ndtype)
1108 {
1109 switch (ndtype) {
1110 case BIF_nd: return 1;
1111 case MATP_nd: return 6;
1112 case MATL_nd: return 3;
1113 case MATR_nd: return 3;
1114 case BEGL_nd: return 1;
1115 case BEGR_nd: return 2;
1116 case ROOT_nd: return 3;
1117 case END_nd: return 1;
1118 default: cm_Fail("Bogus node type %d", ndtype);
1119 }
1120 return 0;/*NOTREACHED*/
1121 }
1122 int
1123 SplitStatesInNode(int ndtype)
1124 {
1125 switch (ndtype) {
1126 case BIF_nd: return 1;
1127 case MATP_nd: return 4;
1128 case MATL_nd: return 2;
1129 case MATR_nd: return 2;
1130 case BEGL_nd: return 1;
1131 case BEGR_nd: return 1;
1132 case ROOT_nd: return 1;
1133 case END_nd: return 1;
1134 default: cm_Fail("Bogus node type %d", ndtype);
1135 }
1136 return 0;/*NOTREACHED*/
1137 }
1138 int
1139 InsertStatesInNode(int ndtype)
1140 {
1141 switch (ndtype) {
1142 case BIF_nd: return 0;
1143 case MATP_nd: return 2;
1144 case MATL_nd: return 1;
1145 case MATR_nd: return 1;
1146 case BEGL_nd: return 0;
1147 case BEGR_nd: return 1;
1148 case ROOT_nd: return 2;
1149 case END_nd: return 0;
1150 default: cm_Fail("Bogus node type %d", ndtype);
1151 }
1152 return 0;/*NOTREACHED*/
1153 }
1154
1155
1156
1157
1158 /* Function: StateDelta(), StateLeftDelta(), StateRightDelta()
1159 * Incept: SRE, Thu Oct 9 11:23:13 2003 [St. Louis]
1160 *
1161 * Purpose: Convenience functions, mirroring some notation in Durbin et al.
1162 * and elsewhere. \Delta notation simplifies some expositions
1163 * of dynamic programming code.
1164 *
1165 * \Delta^R_v = 1 if the state emits right; else 0
1166 * \Delta^L_v = 1 if the state emits left; else 0
1167 * \Delta_v = 2 for pairwise, 1 for singlet, 0 for mute states.
1168 *
1169 * B_st, EL_st are special cases - Delta is returned as zero,
1170 * but can't be used the same way.
1171 *
1172 * Args: sttype - state type code, e.g. MP_st
1173 *
1174 * Returns: (see above)
1175 */
1176 int
1177 StateDelta(int sttype)
1178 {
1179 switch (sttype) {
1180 case D_st: return 0;
1181 case MP_st: return 2;
1182 case ML_st: return 1;
1183 case MR_st: return 1;
1184 case IL_st: return 1;
1185 case IR_st: return 1;
1186 case S_st: return 0;
1187 case E_st: return 0;
1188 case B_st: return 0;
1189 case EL_st: return 0;
1190 default: cm_Fail("bogus state type %d\n", sttype);
1191 }
1192 /*NOTREACHED*/
1193 return 0;
1194 }
1195 int
1196 StateLeftDelta(int sttype)
1197 {
1198 switch (sttype) {
1199 case D_st: return 0;
1200 case MP_st: return 1;
1201 case ML_st: return 1;
1202 case MR_st: return 0;
1203 case IL_st: return 1;
1204 case IR_st: return 0;
1205 case S_st: return 0;
1206 case E_st: return 0;
1207 case B_st: return 0;
1208 case EL_st: return 0;
1209 default: cm_Fail("bogus state type %d\n", sttype);
1210 }
1211 /*NOTREACHED*/
1212 return 0;
1213 }
1214 int
1215 StateRightDelta(int sttype)
1216 {
1217 switch (sttype) {
1218 case D_st: return 0;
1219 case MP_st: return 1;
1220 case ML_st: return 0;
1221 case MR_st: return 1;
1222 case IL_st: return 0;
1223 case IR_st: return 1;
1224 case S_st: return 0;
1225 case E_st: return 0;
1226 case B_st: return 0;
1227 case EL_st: return 0;
1228 default: cm_Fail("bogus state type %d\n", sttype);
1229 }
1230 /*NOTREACHED*/
1231 return 0;
1232 }
1233 /* Function: Emitmode()
1234 * Incept: EPN, Fri Nov 9 09:03:10 2007
1235 *
1236 * Purpose: Convenience function, return emitmode of a sttype
1237 * EMITLEFT, EMITRIGHT, EMITNONE, or EMITPAIR
1238 *
1239 * Args: sttype - state type code, e.g. MP_st
1240 *
1241 * Returns: (see above)
1242 */
1243 int
1244 Emitmode(int sttype)
1245 {
1246 switch (sttype) {
1247 case IL_st: return EMITLEFT;
1248 case IR_st: return EMITRIGHT;
1249 case D_st: return EMITNONE;
1250 case ML_st: return EMITLEFT;
1251 case MR_st: return EMITRIGHT;
1252 case MP_st: return EMITPAIR;
1253 case S_st: return EMITNONE;
1254 case E_st: return EMITNONE;
1255 case B_st: return EMITNONE;
1256 case EL_st: return EMITNONE;
1257 default: cm_Fail("bogus state type %d\n", sttype);
1258 }
1259 /*NOTREACHED*/
1260 return 0;
1261 }
1262 /* Function: NumReachableInserts()
1263 * Incept: EPN, Mon Oct 24 22:41:00 2011
1264 *
1265 * Purpose: Returns number of insert states reachable
1266 * from the current state, depends only on
1267 * stid (node type and state type).
1268 *
1269 * Args: stid - state id code, e.g. MATP_MP
1270 *
1271 * Returns: (see above)
1272 */
1273 int
1274 NumReachableInserts(int stid)
1275 {
1276 switch (stid) {
1277 case MATL_ML: return 1;
1278 case MATL_D: return 1;
1279 case MATL_IL: return 1;
1280 case MATP_MP: return 2;
1281 case MATP_ML: return 2;
1282 case MATP_MR: return 2;
1283 case MATP_D: return 2;
1284 case MATP_IL: return 2;
1285 case MATP_IR: return 1;
1286 case MATR_MR: return 1;
1287 case MATR_D: return 1;
1288 case MATR_IR: return 1;
1289 case BIF_B: return 0;
1290 case BEGL_S: return 0;
1291 case BEGR_S: return 1;
1292 case BEGR_IL: return 1;
1293 case END_E: return 0;
1294 case ROOT_S: return 2;
1295 case ROOT_IL: return 2;
1296 case ROOT_IR: return 1;
1297 case END_EL: return 0;
1298 default: cm_Fail("bogus state id %d\n", stid);
1299 }
1300 /*NOTREACHED*/
1301 return 0;
1302 }
1303
1304 /* Function: PrintCM()
1305 * Date: SRE, Sat Jul 29 10:55:16 2000 [St. Louis]
1306 *
1307 * Purpose: Debugging: show a tabular representation of a CM structure.
1308 *
1309 * Args: fp - output stream (e.g. stdout)
1310 * cm - the CM to show
1311 *
1312 * Returns: (void)
1313 */
1314 void
1315 PrintCM(FILE *fp, CM_t *cm)
1316 {
1317 int x;
1318
1319 fprintf(fp, "%5s %6s %5s %6s %7s %6s %5s %5s %5s\n",
1320 " idx ","sttype", "ndidx", "ndtype", " stid ", "cfirst", " cnum", "plast", " pnum");
1321 fprintf(fp, "%5s %6s %5s %6s %7s %5s %5s %5s %5s\n",
1322 "-----", "------", "-----", "------","-------","------","-----", "-----", "-----");
1323
1324 for (x = 0; x < cm->M; x++)
1325 {
1326 fprintf(fp, "%5d %-6s %5d %6s %-7s %6d %5d %5d %5d\n",
1327 x, Statetype(cm->sttype[x]), cm->ndidx[x],
1328 Nodetype(cm->ndtype[cm->ndidx[x]]), UniqueStatetype(cm->stid[x]),
1329 cm->cfirst[x], cm->cnum[x],
1330 cm->plast[x], cm->pnum[x]);
1331 }
1332 }
1333
1334 /* Function: SummarizeCM()
1335 * Date: SRE, Sat Jul 29 12:19:31 2000 [St. Louis]
1336 *
1337 * Purpose: Print some summary information about a new CM;
1338 * called by cmbuild after each new model construction.
1339 *
1340 * Args: fp - output stream (e.g. stdout)
1341 * cm - cm to summarize
1342 *
1343 * Returns: (void)
1344 */
1345 void
1346 SummarizeCM(FILE *fp, CM_t *cm)
1347 {
1348 int x;
1349 int count[UNIQUESTATES];
1350
1351 for (x = 0; x < UNIQUESTATES; x++) count[x] = 0;
1352
1353 for (x = 0; x < cm->M; x++)
1354 count[(int) cm->stid[x]]++;
1355
1356 fprintf(fp, "Summary report for CM structure:\n");
1357 fprintf(fp, "--------------------------------------\n");
1358 fprintf(fp, "Total states: %4d\n", cm->M);
1359 fprintf(fp, "Total nodes: %4d\n", cm->nodes);
1360 fprintf(fp, "Bifurcations: %4d\n", count[BIF_B]);
1361 fprintf(fp, "MATP nodes: %4d\n", count[MATP_MP]);
1362 fprintf(fp, "MATL nodes: %4d\n", count[MATL_ML]);
1363 fprintf(fp, "MATR nodes: %4d\n", count[MATR_MR]);
1364 fprintf(fp, "Consensus columns: %4d (2*MATP+MATL+MATR)\n",
1365 count[MATP_MP]*2+count[MATL_ML]+count[MATR_MR]);
1366 fprintf(fp, "Base pairs: %4d (MATP)\n", count[MATP_MP]);
1367 fprintf(fp, "Single stranded: %4d (MATL+MATR)\n", count[MATL_ML]+count[MATR_MR]);
1368 /*fprintf(fp, "W: max hit size: %d\n", cm->W);*/
1369
1370 }
1371
1372 /* Functions: Statetype(), Nodetype(), UniqueStatetype();
1373 * StateCode(), NodeCode(), UniqueStateCode()
1374 * Date: SRE, Sat Jul 29 11:07:47 2000 [St. Louis]
1375 *
1376 * Purpose: Translate internal flags into human-readable strings,
1377 * for clearer debugging output (*type functions);
1378 * or vice versa (*Code functions)
1379 *
1380 * Args: type - a state type, node type, or unique statetype code
1381 * s - string representing a code
1382 *
1383 * Returns: the appropriate string; or the appropriate code.
1384 */
1385 char *
1386 Statetype(int type)
1387 {
1388 switch (type) {
1389 case D_st: return "D";
1390 case MP_st: return "MP";
1391 case ML_st: return "ML";
1392 case MR_st: return "MR";
1393 case IL_st: return "IL";
1394 case IR_st: return "IR";
1395 case S_st: return "S";
1396 case E_st: return "E";
1397 case B_st: return "B";
1398 case EL_st: return "EL";
1399 default: cm_Fail("bogus state type %d\n", type);
1400 }
1401 return ""; /*NOTREACHED*/
1402 }
1403 int
1404 StateCode(char *s)
1405 {
1406 if (strcmp(s, "D") == 0) return D_st;
1407 else if (strcmp(s, "MP") == 0) return MP_st;
1408 else if (strcmp(s, "ML") == 0) return ML_st;
1409 else if (strcmp(s, "MR") == 0) return MR_st;
1410 else if (strcmp(s, "IL") == 0) return IL_st;
1411 else if (strcmp(s, "IR") == 0) return IR_st;
1412 else if (strcmp(s, "S") == 0) return S_st;
1413 else if (strcmp(s, "E") == 0) return E_st;
1414 else if (strcmp(s, "B") == 0) return B_st;
1415 else if (strcmp(s, "EL") == 0) return EL_st;
1416 return -1;
1417 }
1418 char *
1419 Nodetype(int type)
1420 {
1421 switch (type) {
1422 case DUMMY_nd: return "-";
1423 case BIF_nd: return "BIF";
1424 case MATP_nd: return "MATP";
1425 case MATL_nd: return "MATL";
1426 case MATR_nd: return "MATR";
1427 case BEGL_nd: return "BEGL";
1428 case BEGR_nd: return "BEGR";
1429 case ROOT_nd: return "ROOT";
1430 case END_nd: return "END";
1431 default: cm_Fail("bogus node type %d\n", type);
1432 }
1433 return "";
1434 }
1435 int
1436 NodeCode(char *s)
1437 {
1438 if (strcmp(s, "BIF") == 0) return BIF_nd;
1439 else if (strcmp(s, "MATP") == 0) return MATP_nd;
1440 else if (strcmp(s, "MATL") == 0) return MATL_nd;
1441 else if (strcmp(s, "MATR") == 0) return MATR_nd;
1442 else if (strcmp(s, "BEGL") == 0) return BEGL_nd;
1443 else if (strcmp(s, "BEGR") == 0) return BEGR_nd;
1444 else if (strcmp(s, "ROOT") == 0) return ROOT_nd;
1445 else if (strcmp(s, "END") == 0) return END_nd;
1446 return -1;
1447 }
1448 char *
1449 UniqueStatetype(int type)
1450 {
1451 switch (type) {
1452 case DUMMY: return "DUMMY";
1453 case ROOT_S: return "ROOT_S";
1454 case ROOT_IL: return "ROOT_IL";
1455 case ROOT_IR: return "ROOT_IR";
1456 case BEGL_S : return "BEGL_S";
1457 case BEGR_S : return "BEGR_S";
1458 case BEGR_IL: return "BEGR_IL";
1459 case MATP_MP: return "MATP_MP";
1460 case MATP_ML: return "MATP_ML";
1461 case MATP_MR: return "MATP_MR";
1462 case MATP_D : return "MATP_D";
1463 case MATP_IL: return "MATP_IL";
1464 case MATP_IR: return "MATP_IR";
1465 case MATL_ML: return "MATL_ML";
1466 case MATL_D : return "MATL_D";
1467 case MATL_IL: return "MATL_IL";
1468 case MATR_MR: return "MATR_MR";
1469 case MATR_D : return "MATR_D";
1470 case MATR_IR: return "MATR_IR";
1471 case END_E : return "END_E";
1472 case BIF_B : return "BIF_B";
1473 case END_EL : return "END_EL";
1474 default: cm_Fail("bogus unique state type %d\n", type);
1475 }
1476 return "";
1477 }
1478 int
1479 UniqueStateCode(char *s)
1480 {
1481 if (strcmp(s, "ROOT_S") == 0) return ROOT_S;
1482 else if (strcmp(s, "ROOT_IL") == 0) return ROOT_IL;
1483 else if (strcmp(s, "ROOT_IR") == 0) return ROOT_IR;
1484 else if (strcmp(s, "BEGL_S") == 0) return BEGL_S;
1485 else if (strcmp(s, "BEGR_S") == 0) return BEGR_S;
1486 else if (strcmp(s, "BEGR_IL") == 0) return BEGR_IL;
1487 else if (strcmp(s, "MATP_MP") == 0) return MATP_MP;
1488 else if (strcmp(s, "MATP_ML") == 0) return MATP_ML;
1489 else if (strcmp(s, "MATP_MR") == 0) return MATP_MR;
1490 else if (strcmp(s, "MATP_D") == 0) return MATP_D;
1491 else if (strcmp(s, "MATP_IL") == 0) return MATP_IL;
1492 else if (strcmp(s, "MATP_IR") == 0) return MATP_IR;
1493 else if (strcmp(s, "MATL_ML") == 0) return MATL_ML;
1494 else if (strcmp(s, "MATL_D") == 0) return MATL_D;
1495 else if (strcmp(s, "MATL_IL") == 0) return MATL_IL;
1496 else if (strcmp(s, "MATR_MR") == 0) return MATR_MR;
1497 else if (strcmp(s, "MATR_D") == 0) return MATR_D;
1498 else if (strcmp(s, "MATR_IR") == 0) return MATR_IR;
1499 else if (strcmp(s, "BIF_B") == 0) return BIF_B;
1500 else if (strcmp(s, "END_E") == 0) return END_E;
1501 else if (strcmp(s, "END_EL") == 0) return END_EL;
1502 else cm_Fail("bogus unique statetype %s\n", s);
1503 return 0; /*NOTREACHED*/
1504 }
1505 int
1506 DeriveUniqueStateCode(int ndtype, int sttype)
1507 {
1508 switch (ndtype) {
1509 case BIF_nd:
1510 switch (sttype) {
1511 case B_st: return BIF_B;
1512 default: return -1;
1513 }
1514 case MATP_nd:
1515 switch (sttype) {
1516 case D_st: return MATP_D;
1517 case MP_st: return MATP_MP;
1518 case ML_st: return MATP_ML;
1519 case MR_st: return MATP_MR;
1520 case IL_st: return MATP_IL;
1521 case IR_st: return MATP_IR;
1522 default: return -1;
1523 }
1524 case MATL_nd:
1525 switch (sttype) {
1526 case D_st: return MATL_D;
1527 case ML_st: return MATL_ML;
1528 case IL_st: return MATL_IL;
1529 default: return -1;
1530 }
1531 case MATR_nd:
1532 switch (sttype) {
1533 case D_st: return MATR_D;
1534 case MR_st: return MATR_MR;
1535 case IR_st: return MATR_IR;
1536 default: return -1;
1537 }
1538 case BEGL_nd:
1539 switch (sttype) {
1540 case S_st: return BEGL_S;
1541 default: return -1;
1542 }
1543 case BEGR_nd:
1544 switch (sttype) {
1545 case S_st: return BEGR_S;
1546 case IL_st: return BEGR_IL;
1547 default: return -1;
1548 }
1549 case ROOT_nd:
1550 switch (sttype) {
1551 case S_st: return ROOT_S;
1552 case IL_st: return ROOT_IL;
1553 case IR_st: return ROOT_IR;
1554 default: return -1;
1555 }
1556 case END_nd:
1557 switch (sttype) {
1558 case E_st: return END_E;
1559 default: return -1;
1560 }
1561 default:
1562 return -1;
1563 }
1564 }
1565
1566 /* Function: MarginalMode()
1567 * Date: EPN, Sat Oct 8 06:52:21 2011
1568 *
1569 * Purpose: Translate internal flags for truncation mode
1570 * into human-readable strings, for clearer output.
1571 *
1572 * Args: mode - a marginal mode
1573 * TRMODE_J, TRMODE_L, TRMODE_R, TRMODE_T or TRMODE_UNKNOWN
1574 *
1575 * Returns: the appropriate string
1576 */
1577 char *
1578 MarginalMode(char mode)
1579 {
1580 switch (mode) {
1581 case TRMODE_J: return "Joint";
1582 case TRMODE_L: return "Left";
1583 case TRMODE_R: return "Right";
1584 case TRMODE_T: return "Term";
1585 case TRMODE_UNKNOWN: return "Unkwn";
1586 default: cm_Fail("bogus marginal mode type %d\n", mode);
1587 }
1588 return "";
1589 }
1590
1591 /* Function: ModeEmitsLeft()
1592 * Date: EPN, Thu Jan 5 09:31:18 2012
1593 *
1594 * Purpose: Returns TRUE if mode emits left, i.e.
1595 * mode is TRMODE_J or TRMODE_L.
1596 *
1597 */
1598 int
1599 ModeEmitsLeft(char mode)
1600 {
1601 if(mode == TRMODE_J || mode == TRMODE_L) return TRUE;
1602 return FALSE;
1603 }
1604
1605 /* Function: ModeEmitsRight()
1606 * Date: EPN, Thu Jan 5 09:32:31 2012
1607 *
1608 * Purpose: Returns TRUE if mode emits right, i.e.
1609 * mode is TRMODE_J or TRMODE_R.
1610 *
1611 */
1612 int
1613 ModeEmitsRight(char mode)
1614 {
1615 if(mode == TRMODE_J || mode == TRMODE_R) return TRUE;
1616 return FALSE;
1617 }
1618
1619 /* Function: StateMapsLeft()
1620 *
1621 * Purpose: Returns TRUE if cm unique states type <stid> is
1622 * a state type that maps emits or deletes on the left.
1623 */
1624 int
1625 StateMapsLeft(char stid)
1626 {
1627 switch (stid) {
1628 case MATP_MP: /* match left, match right */
1629 case MATP_ML: /* match left, delete right */
1630 case MATP_MR: /* delete left, match right */
1631 case MATP_D: /* delete left, delete right */
1632 case MATP_IL:
1633 case BEGR_IL:
1634 case MATL_ML:
1635 case MATL_D:
1636 case MATL_IL:
1637 case ROOT_IL:
1638 return TRUE;
1639 default:
1640 return FALSE;
1641 }
1642 }
1643
1644 /* Function: StateMapsRight()
1645 *
1646 * Purpose: Returns TRUE if cm unique states type <stid> is
1647 * a state type that maps emits or deletes on the right.
1648 */
1649 int
1650 StateMapsRight(char stid)
1651 {
1652 switch (stid) {
1653 case MATP_MP: /* match left, match right */
1654 case MATP_ML: /* match left, delete right */
1655 case MATP_MR: /* delete left, match right */
1656 case MATP_D: /* delete left, delete right */
1657 case MATP_IR:
1658 case MATR_MR:
1659 case MATR_D:
1660 case MATR_IR:
1661 case ROOT_IR:
1662 return TRUE;
1663 default:
1664 return FALSE;
1665 }
1666 }
1667
1668 /* Function: StateMapsMatch()
1669 *
1670 * Purpose: Returns TRUE if cm unique states type <stid> maps
1671 * to an HMM match state type.
1672 */
1673 int
1674 StateMapsMatch(char stid)
1675 {
1676 switch (stid) {
1677 case MATP_MP:
1678 case MATL_ML:
1679 case MATR_MR:
1680 return TRUE;
1681 default:
1682 return FALSE;
1683 }
1684 }
1685
1686 /* Function: StateMapsInsert()
1687 *
1688 * Purpose: Returns TRUE if cm unique states type <stid> maps
1689 * to an HMM insert state type.
1690 */
1691 int
1692 StateMapsInsert(char stid)
1693 {
1694 switch (stid) {
1695 case MATP_IL:
1696 case MATP_IR:
1697 case MATL_IL:
1698 case MATR_IR:
1699 case BEGR_IL:
1700 case ROOT_IL:
1701 case ROOT_IR:
1702 return TRUE;
1703 default:
1704 return FALSE;
1705 }
1706 }
1707
1708 /* Function: StateMapsDelete()
1709 *
1710 * Purpose: Returns TRUE if cm unique states type <stid> maps
1711 * to an HMM delete state type.
1712 */
1713 int
1714 StateMapsDelete(char stid)
1715 {
1716 switch (stid) {
1717 case MATP_ML: /* delete right */
1718 case MATP_MR: /* delete left */
1719 case MATP_D: /* delete pair */
1720 case MATL_D:
1721 case MATR_D:
1722 return TRUE;
1723 default:
1724 return FALSE;
1725 }
1726 }
1727
1728 /* Function: NodeMapsLeft()
1729 *
1730 * Purpose: Returns TRUE if cm node type is a type with
1731 * at least one left emitting (possibly insert)
1732 * state within it.
1733 */
1734 int
1735 NodeMapsLeft(char ndtype)
1736 {
1737 switch (ndtype) {
1738 case MATP_nd:
1739 case MATL_nd:
1740 case ROOT_nd:
1741 case BEGR_nd:
1742 return TRUE;
1743 default:
1744 return FALSE;
1745 }
1746 }
1747
1748 /* Function: NodeMapsRight()
1749 *
1750 * Purpose: Returns TRUE if cm node type is a type with
1751 * at least one right emitting (possibly insert)
1752 * state within it.
1753 */
1754 int
1755 NodeMapsRight(char ndtype)
1756 {
1757 switch (ndtype) {
1758 case MATP_nd:
1759 case MATR_nd:
1760 case ROOT_nd:
1761 return TRUE;
1762 default:
1763 return FALSE;
1764 }
1765 }
1766
1767 /* Function: StateIsDetached()
1768 *
1769 * Purpose: Returns TRUE if state v of cm is a detached
1770 * insert. This should be true IFF type of next
1771 * state is an END_E, meaning state v is an
1772 * IL_st or (rarely) a MATP_IR state.
1773 */
1774 int
1775 StateIsDetached(CM_t *cm, int v)
1776 {
1777 if(cm->stid[(v+1)] == END_E) {
1778 #if eslDEBUGLEVEL >= 1
1779 /* check to make sure the state is actually detached */
1780 int y, x, x_offset;
1781 /* Determine if b is an IL_st, or the rare case of a MATP_IR st */
1782 if(cm->sttype[v] == IL_st) x_offset = 0;
1783 else {
1784 ESL_DASSERT1((cm->stid[v] == MATP_IR)); /* if assertion fails, v is a non-IL, non-MATP_IR state, should'nt be detached */
1785 x_offset = 1; /* MATP_y -> MATP_IR is second possible transition for MATP_*,
1786 * unless MATP_y == MATP_IR, but we don't get there in for loop below. */
1787 }
1788 for (y = cm->pnum[v]-1; y >= 1; y--) { /* y >= 1 means we never get to v->v prob, which is irrelevant. */
1789 x = cm->plast[v] - y;
1790 ESL_DASSERT1((fabs(cm->t[x][x_offset] - 0.0) < eslSMALLX1));
1791 }
1792 #endif
1793 return TRUE;
1794 }
1795 return FALSE;
1796 }
1797
1798 /* Function: CMRebalance()
1799 * Date: SRE, Mon Apr 8 11:40:46 2002 [St. Louis]
1800 *
1801 * Purpose: Rebalance a CM tree to guarantee O(N^2 log N) memory in
1802 * smallcyk.c's divide and conquer algorithm.
1803 *
1804 * Input: a non-configured CM that's numbered in preorder
1805 * traversal: visit root, visit left, visit right. (e.g.,
1806 * left child S always visited before right child S,
1807 * cfirst[w] < cnum[y], as produced by cm_modelmaker.c).
1808 *
1809 * Output: a renumbered CM, in a modified preorder traversal:
1810 * visit root, visit min weight child, visit max weight child,
1811 * where weight is the # of extra CYK decks that'll need to
1812 * be held in memory to calculate this subgraph.
1813 *
1814 * Args: cm - the old CM
1815 *
1816 * Returns: eslOK on success, ret_new_cm is valid rebalanced CM
1817 * eslFAIL if source (old) CM has been configured, ret_new_cm set as NULL
1818 * eslEMEM if we run out of memory
1819 */
1820 int
1821 CMRebalance(CM_t *cm, char *errbuf, CM_t **ret_new_cm)
1822 {
1823 int status;
1824 ESL_STACK *pda = NULL; /* stack used for traversing old CM */
1825 CM_t *new = NULL; /* new CM we're creating */
1826 int *wgt = NULL; /* # of extra CYK decks required to calc subgraphs */
1827 int *newidx = NULL; /* newidx[v] = old CM state v's new index in new CM */
1828 int v, w, y,z; /* state indices in old CM */
1829 int nv; /* state index in new CM */
1830 int x; /* counter over transitions, residues, nodes */
1831
1832 if((status = cm_nonconfigured_Verify(cm, errbuf)) != eslOK) goto ERROR;
1833
1834 /* Create the new model. Copy information that's unchanged by
1835 * renumbering the CM.
1836 */
1837 new = CreateCM(cm->nodes, cm->M, cm->clen, cm->abc);
1838 if((status = esl_strdup(cm->name, -1, &(new->name))) != eslOK) goto ERROR;
1839 if((status = esl_strdup(cm->acc, -1, &(new->acc))) != eslOK) goto ERROR;
1840 if((status = esl_strdup(cm->desc, -1, &(new->desc))) != eslOK) goto ERROR;
1841 if((status = esl_strdup(cm->rf, -1, &(new->rf))) != eslOK) goto ERROR;
1842 if((status = esl_strdup(cm->consensus, -1, &(new->consensus))) != eslOK) goto ERROR;
1843 if(cm->map != NULL) {
1844 ESL_ALLOC(new->map, sizeof(int) * (cm->clen+1));
1845 esl_vec_ICopy(cm->map, cm->clen+1, new->map);
1846 }
1847
1848 new->flags = cm->flags;
1849 new->offset = cm->offset;
1850 new->clen = cm->clen;
1851 new->nseq = cm->nseq;
1852 new->eff_nseq = cm->eff_nseq;
1853 if(cm->flags & CMH_GA) new->ga = cm->ga;
1854 if(cm->flags & CMH_TC) new->tc = cm->tc;
1855 if(cm->flags & CMH_NC) new->nc = cm->nc;
1856
1857 for (x = 0; x < cm->abc->K; x++) new->null[x] = cm->null[x];
1858
1859 /* Calculate "weights" (# of required extra decks) on every B and S state.
1860 * Recursive rule here is: 1 + min(wgt[left], wgt[right]).
1861 */
1862 ESL_ALLOC(wgt, sizeof(int) * cm->M);
1863 for (v = cm->M-1; v >= 0; v--)
1864 {
1865 if (cm->sttype[v] == E_st) /* initialize unbifurcated segments with 1 */
1866 wgt[v] = 1;
1867 else if (cm->sttype[v] == B_st) /* "cfirst"=left S child. "cnum"=right S child. */
1868 wgt[v] = 1 + ESL_MIN(wgt[cm->cfirst[v]], wgt[cm->cnum[v]]);
1869 else
1870 wgt[v] = wgt[v+1]; /* all other states propagate up to S */
1871 }
1872
1873 /* Now, preorder traverse the new CM. At each bifurcation, we want
1874 * to visit the S with minimum weight first. v is an index on the
1875 * old CM, and we hop it around using this traversal order and a
1876 * pushdown stack. nv is an index on the new CM, which just moves
1877 * in preorder traversal 0..cm->M-1.
1878 *
1879 */
1880 v = 0;
1881 z = cm->M-1;
1882 if((pda = esl_stack_ICreate()) == NULL) goto ERROR;
1883 ESL_ALLOC(newidx, sizeof(int) * cm->M);
1884 for (nv = 0; nv < cm->M; nv++)
1885 {
1886 /* Keep a map of where the old states are going in new CM
1887 * old state v becomes newidx[v] in the new model.
1888 * This is guaranteed to be a one to one map.
1889 */
1890 newidx[v] = nv;
1891
1892 /* Copy old v to new nv.
1893 * First, the easy stuff, that's unaffected by renumbering.
1894 */
1895 new->sttype[nv] = cm->sttype[v];
1896 new->ndidx[nv] = cm->ndidx[v];
1897 new->stid[nv] = cm->stid[v];
1898 new->pnum[nv] = cm->pnum[v];
1899 for (x = 0; x < MAXCONNECT; x++) {
1900 new->t[nv][x] = cm->t[v][x];
1901 new->tsc[nv][x] = cm->t[v][x];
1902 }
1903 for (x = 0; x < cm->abc->K*cm->abc->K; x++) {
1904 new->e[nv][x] = cm->e[v][x];
1905 new->esc[nv][x] = cm->esc[v][x];
1906 }
1907
1908 /* Slightly harder - the plast connection for nv, to the last
1909 * of 1-6 parent states. We use the newidx map to get it from plast[v].
1910 */
1911 if (nv != 0) new->plast[nv] = newidx[cm->plast[v]];
1912 else new->plast[nv] = -1; /* ROOT. */
1913
1914 /* Now, figure out next v, and make cfirst, cnum connections.
1915 *
1916 * If we're a B, then traverse to the lighter child S state first.
1917 * Remember the overload in CM struct: cfirst = idx of left child;
1918 * cnum = idx of right child. So if we visit left w first, cfirst=nv+1;
1919 * if we visit right y first, cnum=nv+1. Getting the second child
1920 * index is a little tricky: we rely on knowing that
1921 * the # of states in the first subgraph we visit is y-w,
1922 * so we know the second child index is nv+y-w+1.
1923 *
1924 * If we're an E, pop the next v off the stack. cfirst=-1,cnum=0, because
1925 * it has no children.
1926 *
1927 * Else, the next v is just v++. cfirst for new nv can be calculated by using the
1928 * offset in the old model: e.g. nv + (cfirst[v] - v). cnum is unchanged.
1929 *
1930 */
1931 if (cm->sttype[v] == B_st)
1932 {
1933 w = cm->cfirst[v]; /* left child of v*/
1934 y = cm->cnum[v]; /* right child of v*/
1935
1936 if (wgt[w] <= wgt[y]) /* left (w) lighter or same weight? visit w first, defer y */
1937 {
1938 if((status = esl_stack_IPush(pda, y)) != eslOK) goto ERROR;
1939 if((status = esl_stack_IPush(pda, z)) != eslOK) goto ERROR;
1940 v = w;
1941 z = y-1;
1942 new->cfirst[nv] = nv+1; /* left child is nv+1 */
1943 new->cnum[nv] = nv+y-w+1;
1944 }
1945 else /* right (y) lighter? visit y first, defer w */
1946 {
1947 if((status = esl_stack_IPush(pda, w)) != eslOK) goto ERROR;
1948 if((status = esl_stack_IPush(pda, y-1)) != eslOK) goto ERROR;
1949 v = y; /* z unchanged. */
1950 new->cfirst[nv] = nv+z-y+2;
1951 new->cnum[nv] = nv+1; /* right child is nv+1 */
1952 }
1953 }
1954 else if (cm->sttype[v] == E_st)
1955 {
1956 new->cfirst[nv] = -1;
1957 new->cnum[nv] = 0;
1958 esl_stack_IPop(pda, &z);
1959 esl_stack_IPop(pda, &v);
1960 }
1961 else
1962 {
1963 new->cfirst[nv] = nv + (cm->cfirst[v]-v); /* use offset in old model */
1964 new->cnum[nv] = cm->cnum[v]; /* cnum unchanged. */
1965 v++;
1966 }
1967 }
1968
1969 /* Deal with the renumbered begin and end transition distributions,
1970 * using the newidx[v] map.
1971 */
1972 for (v = 0; v < cm->M; v++)
1973 {
1974 new->begin[newidx[v]] = cm->begin[v];
1975 new->beginsc[newidx[v]] = cm->beginsc[v];
1976 new->ibeginsc[newidx[v]] = cm->ibeginsc[v];
1977 new->end[newidx[v]] = cm->end[v];
1978 new->endsc[newidx[v]] = cm->endsc[v];
1979 new->iendsc[newidx[v]] = cm->iendsc[v];
1980 }
1981 if(cm->qdbinfo != NULL) {
1982 if((status = CopyCMQDBInfo(cm->qdbinfo, new->qdbinfo, errbuf)) != eslOK) goto ERROR;
1983 for (v = 0; v < cm->M; v++) new->qdbinfo->dmin1[newidx[v]] = cm->qdbinfo->dmin1[v];
1984 for (v = 0; v < cm->M; v++) new->qdbinfo->dmax1[newidx[v]] = cm->qdbinfo->dmax1[v];
1985 for (v = 0; v < cm->M; v++) new->qdbinfo->dmin2[newidx[v]] = cm->qdbinfo->dmin2[v];
1986 for (v = 0; v < cm->M; v++) new->qdbinfo->dmax2[newidx[v]] = cm->qdbinfo->dmax2[v];
1987 }
1988
1989 /* Guide tree numbering is unchanged - still in preorder.
1990 * Associate nodes with new state numbering.
1991 */
1992 for (x = 0; x < new->nodes; x++)
1993 {
1994 new->nodemap[x] = newidx[cm->nodemap[x]];
1995 new->ndtype[x] = cm->ndtype[x];
1996 }
1997
1998 if(wgt != NULL) free(wgt);
1999 if(newidx != NULL) free(newidx);
2000 if(pda != NULL) esl_stack_Destroy(pda);
2001 *ret_new_cm = new;
2002 return eslOK;
2003
2004 ERROR:
2005 if(wgt != NULL) free(wgt);
2006 if(newidx != NULL) free(newidx);
2007 if(pda != NULL) esl_stack_Destroy(pda);
2008 if(new != NULL) FreeCM(new);
2009 *ret_new_cm = NULL;
2010 if(status == eslEMEM) ESL_FAIL(status, errbuf, "out of memory");
2011 else return status; /* status is eslFAIL, errbuf was filled by cm_nonconfigured_Verify() */
2012 }
2013
2014 /*
2015 * EPN, Wed Mar 21 09:29:55 2007
2016 *
2017 * rsearch_calculate_gap_penalty (FROM RSEARCH::buildcm.c)
2018 *
2019 * Given the from state, the to state, and the gap parameters, returns
2020 * the gap penalty.
2021 */
2022 float rsearch_calculate_gap_penalty (char from_state, char to_state,
2023 int from_node, int to_node,
2024 float input_alpha, float input_beta,
2025 float input_alphap, float input_betap) {
2026 int from_class, to_class;
2027 double alpha, beta; /* Alpha or beta values to use */
2028
2029 /* There are potentially 400 different combinations of state pairs here.
2030 To make it manageable, break down into the 6 classes on p. 8 of lab
2031 book 7, numbered as follows
2032 0 M ROOT_S, BEGL_S, BEGR_S, MATP_MP, MATL_ML, MATR_MR, END_E, BIF_B
2033 1 IL ROOT_IL, BEGR_IL, MATP_IL, MATL_IL
2034 2 DL MATP_MR, MATL_D
2035 3 IR ROOT_IR, MATP_IR, MATR_IR
2036 4 DR MATP_ML, MATR_D
2037 5 DB MATP_D
2038 */
2039 switch (from_state) {
2040 case MATP_D:
2041 from_class = DB_cl;
2042 break;
2043 case MATP_ML:
2044 case MATR_D:
2045 from_class = DR_cl;
2046 break;
2047 case ROOT_IR:
2048 case MATP_IR:
2049 case MATR_IR:
2050 from_class = IR_cl;
2051 break;
2052 case MATP_MR:
2053 case MATL_D:
2054 from_class = DL_cl;
2055 break;
2056 case ROOT_IL:
2057 case BEGR_IL:
2058 case MATP_IL:
2059 case MATL_IL:
2060 from_class = IL_cl;
2061 break;
2062 default:
2063 from_class = M_cl;
2064 }
2065
2066 switch (to_state) {
2067 case MATP_D:
2068 to_class = DB_cl;
2069 break;
2070 case MATP_ML:
2071 case MATR_D:
2072 to_class = DR_cl;
2073 break;
2074 case ROOT_IR:
2075 case MATP_IR:
2076 case MATR_IR:
2077 to_class = IR_cl;
2078 break;
2079 case MATP_MR:
2080 case MATL_D:
2081 to_class = DL_cl;
2082 break;
2083 case ROOT_IL:
2084 case BEGR_IL:
2085 case MATP_IL:
2086 case MATL_IL:
2087 to_class = IL_cl;
2088 break;
2089 default:
2090 to_class = M_cl;
2091 }
2092
2093 /* Now set alpha and beta according to state classes and nodes */
2094 /* Alpha is alpha' for MATP->MATP, alpha otherwise */
2095 if (from_node == MATP_nd && to_node == MATP_nd)
2096 alpha = input_alphap;
2097 else
2098 alpha = input_alpha;
2099 /* Beta is beta' iff from_cl is DB and MATP->MATP */
2100 if (from_class == DB_cl && from_node == MATP_nd && to_node == MATP_nd)
2101 beta = input_betap;
2102 else
2103 beta = input_beta;
2104
2105 /* Now that we have the proper class, return the appropriate gap penalty */
2106 if (from_class == M_cl) {
2107 if (to_class == M_cl) {
2108 return (0.);
2109 } else if (to_class == DB_cl) {
2110 return (alpha);
2111 } else {
2112 return (0.5*alpha);
2113 }
2114 } else if (from_class == IL_cl) {
2115 if (to_class == M_cl) {
2116 return (beta + 0.5*alpha);
2117 } else if (to_class == IL_cl) {
2118 return (beta);
2119 } else if (to_class == DB_cl) {
2120 return (beta+1.5*alpha);
2121 } else {
2122 return (beta + alpha);
2123 }
2124 } else if (from_class == DL_cl) {
2125 if (to_class == M_cl) {
2126 return (beta + 0.5*alpha);
2127 } else if (to_class == DL_cl) {
2128 return (beta);
2129 } else if (to_class == DB_cl) {
2130 return (beta + 0.5*alpha);
2131 } else {
2132 return (beta + alpha);
2133 }
2134 } else if (from_class == IR_cl) {
2135 if (to_class == M_cl) {
2136 return (beta + 0.5*alpha);
2137 } else if (to_class == IR_cl) {
2138 return (beta);
2139 } else if (to_class == DB_cl) {
2140 return (beta+1.5*alpha);
2141 } else {
2142 return (beta + alpha);
2143 }
2144 } else if (from_class == DR_cl) {
2145 if (to_class == M_cl) {
2146 return (beta + 0.5*alpha);
2147 } else if (to_class == DR_cl) {
2148 return (beta);
2149 } else if (to_class == DB_cl) {
2150 return (beta + 0.5*alpha);
2151 } else {
2152 return (beta + alpha);
2153 }
2154 } else { /* DB_cl */
2155 if (to_class == IL_cl || to_class == IR_cl) {
2156 return (2*beta + 1.5*alpha);
2157 } else if (to_class == M_cl) {
2158 return (2*beta + alpha);
2159 } else if (to_class == DB_cl) {
2160 return (2*beta);
2161 } else {
2162 return (2*beta + 0.5*alpha);
2163 }
2164 }
2165 return (0);
2166 }
2167
2168 /* Function: cm_Exponentiate
2169 * Date: EPN, Sun May 20 13:10:06 2007
2170 *
2171 * Purpose: Exponentiate the emission and transition probabilities
2172 * of a CM by z. We can only do this if a CM is in global
2173 * mode. Otherwise, the cm->end probabilities would change,
2174 * and we don't want that.
2175 *
2176 * Args:
2177 * CM - the covariance model
2178 * z - factor to exponentiate by
2179 */
2180 int
2181 cm_Exponentiate(CM_t *cm, double z)
2182 {
2183 int v;
2184 int x,y;
2185
2186 /* If in local mode, fail */
2187 if(cm->flags & CMH_LOCAL_BEGIN || cm->flags & CMH_LOCAL_END) {
2188 cm_Fail("cm_Exponentiate() model is not in global configuration");
2189 }
2190
2191 for(v = 0; v < cm->M; v++)
2192 {
2193 if (cm->sttype[v] != B_st && cm->sttype[v] != E_st)
2194 for (x = 0; x < cm->cnum[v]; x++)
2195 cm->t[v][x] = pow(cm->t[v][x], z);
2196 if (cm->sttype[v] == MP_st)
2197 {
2198 for (x = 0; x < cm->abc->K; x++)
2199 for (y = 0; y < cm->abc->K; y++)
2200 cm->e[v][x*cm->abc->K+y] = pow(cm->e[v][x*cm->abc->K+y], z);
2201 }
2202 if (cm->sttype[v] == ML_st || cm->sttype[v] == MR_st ||
2203 cm->sttype[v] == IL_st || cm->sttype[v] == IR_st)
2204 {
2205 for (x = 0; x < cm->abc->K; x++)
2206 cm->e[v][x] = pow(cm->e[v][x], z);
2207 }
2208 }
2209 CMRenormalize(cm);
2210
2211 /* new probs invalidate log odds scores */
2212 cm->flags &= ~CMH_BITS;
2213 return eslOK;
2214 }
2215
2216 /* Function: cm_p7_Exponentiate
2217 * Date: EPN, Wed Apr 18 05:22:25 2012
2218 *
2219 * Purpose: Exponentiate the emission and transition probabilities
2220 * of a P7_HMM by z. This function complements
2221 * ExponentiateCM().
2222 * Args:
2223 * hmm - the covariance model
2224 * z - factor to exponentiate by
2225 */
2226 int
2227 cm_p7_Exponentiate(P7_HMM *hmm, double z)
2228 {
2229 int k, i;
2230
2231 for(k = 0; k <= hmm->M; k++) {
2232 for(i = 0; i < p7H_NTRANSITIONS; i++) { /* transitions out of match */
2233 hmm->t[k][i] = pow(hmm->t[k][i], z);
2234 }
2235 }
2236 for(k = 1; k <= hmm->M; k++) {
2237 for(i = 0; i < hmm->abc->K; i++) { /* transitions out of match */
2238 hmm->mat[k][i] = pow(hmm->mat[k][i], z);
2239 hmm->ins[k][i] = pow(hmm->ins[k][i], z);
2240 }
2241 }
2242 p7_hmm_Renormalize(hmm);
2243
2244 return eslOK;
2245 }
2246
2247 /* Function: cm_banner()
2248 * Synopsis: print standard INFERNAL application output header
2249 * Based on p7_banner from HMMER3 dev code.
2250 * Incept: EPN, Fri May 25 15:05:42 2007
2251 *
2252 * Purpose: Print the standard INFERNAL command line application banner
2253 * to <fp>, constructing it from <progname> (the name of the
2254 * program) and a short one-line description <banner>.
2255 *
2256 * <progname> would typically be an application's
2257 * <argv[0]>, rather than a fixed string. This allows the
2258 * program to be renamed, or called under different names
2259 * via symlinks. Any path in the <progname> is discarded;
2260 * for instance, if <progname> is "/usr/local/bin/cmcalibrate",
2261 * "cmcalibrate" is used as the program name.
2262 *
2263 * Note:
2264 * Needs to pick up preprocessor #define's from config.h,
2265 * as set by ./configure.
2266 *
2267 * Returns: (void)
2268 */
2269 void
2270 cm_banner(FILE *fp, char *progname, char *banner)
2271 {
2272 char *appname = NULL;
2273
2274 if (esl_FileTail(progname, FALSE, &appname) != eslOK) appname = progname;
2275
2276 fprintf(fp, "# %s :: %s\n", appname, banner);
2277 fprintf(fp, "# INFERNAL %s (%s)\n", INFERNAL_VERSION, INFERNAL_DATE);
2278 fprintf(fp, "# %s\n", INFERNAL_COPYRIGHT);
2279 fprintf(fp, "# %s\n", INFERNAL_LICENSE);
2280 fprintf(fp, "# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
2281
2282 if (appname != NULL) free(appname);
2283 return;
2284 }
2285
2286 /* Function: cm_CalcExpSc()
2287 * Incept: EPN, Wed Aug 1 16:36:52 2007
2288 *
2289 * Purpose: Calculate the expected score for each state of a CM.
2290 * For state v, this should be the average score of an
2291 * emitted parse subtree rooted at v.
2292 *
2293 * Args:
2294 * cm - the covariance model
2295 * ret_expsc - expected score at each state, alloc'ed here
2296 * ret_expsc_noss - expected score at each state if we ignored structure
2297 * same as ret_expsc, but marginal emission probs used
2298 * for MP states, alloc'ed here
2299 *
2300 * Returns:
2301 */
2302 void
2303 cm_CalcExpSc(CM_t *cm, float **ret_expsc, float **ret_expsc_noss)
2304 {
2305 int status;
2306 float *expsc;
2307 float *expsc_noss;
2308 int v,x,y,yoffset;
2309 float *left_e, *left_esc;
2310 float *right_e, *right_esc;
2311 int i,j;
2312
2313 /* contract check */
2314 if(cm->flags & CMH_LOCAL_BEGIN) cm_Fail("cm_CalcExpSc() CMH_LOCAL_BEGIN flag up.\n");
2315 if(cm->flags & CMH_LOCAL_END) cm_Fail("cm_CalcExpSc() CMH_LOCAL_END flag up.\n");
2316 if(ret_expsc == NULL) cm_Fail("cm_CalcExpSc() ret_expsc is NULL.\n");
2317
2318 ESL_ALLOC(left_e, sizeof(float) * cm->abc->K);
2319 ESL_ALLOC(right_e, sizeof(float) * cm->abc->K);
2320 ESL_ALLOC(left_esc, sizeof(float) * cm->abc->K);
2321 ESL_ALLOC(right_esc, sizeof(float) * cm->abc->K);
2322
2323 ESL_ALLOC(expsc, sizeof(float) * cm->M);
2324 ESL_ALLOC(expsc_noss, sizeof(float) * cm->M);
2325 esl_vec_FSet(expsc, cm->M, 0.);
2326 esl_vec_FSet(expsc_noss, cm->M, 0.);
2327
2328 for(v = cm->M-1; v >= 0; v--)
2329 {
2330 switch (cm->sttype[v]) {
2331 case E_st:
2332 break;
2333
2334 case B_st:
2335 expsc[v] = expsc[cm->cfirst[v]] + expsc[cm->cnum[v]]; /* prob of this transition is 1.0 */
2336 expsc_noss[v] = expsc_noss[cm->cfirst[v]] + expsc_noss[cm->cnum[v]]; /* prob of this transition is 1.0 */
2337 break;
2338
2339 case MP_st:
2340 /* calculate marginals for expsc_noss calculation */
2341 /* left half */
2342 esl_vec_FSet(left_e, cm->abc->K, 0.);
2343 for(i = 0; i < cm->abc->K; i++)
2344 for(j = (i*cm->abc->K); j < ((i+1)*cm->abc->K); j++)
2345 left_e[i] += cm->e[v][j];
2346 /* printf("sum should be 1.0: %f\n", esl_vec_FSum(left_e, cm->abc->K)); */
2347 esl_vec_FNorm(left_e, cm->abc->K);
2348 for(i = 0; i < cm->abc->K; i++) left_esc[i] = sreLOG2(left_e[i] / cm->null[i]);
2349
2350 /* right half */
2351 esl_vec_FSet(right_e, cm->abc->K, 0.);
2352 for(i = 0; i < cm->abc->K; i++)
2353 for(j = i; j < cm->abc->K * cm->abc->K; j += cm->abc->K)
2354 right_e[i] += cm->e[v][j];
2355 /* printf("sum should be 1.0: %f\n", esl_vec_FSum(right_e, cm->abc->K)); */
2356 esl_vec_FNorm(right_e, cm->abc->K);
2357 for(i = 0; i < cm->abc->K; i++) right_esc[i] = sreLOG2(right_e[i] / cm->null[i]);
2358
2359 /* expsc uses joint emission probs */
2360 for(x = 0; x < cm->abc->K * cm->abc->K; x++)
2361 expsc[v] += cm->e[v][x] * cm->esc[v][x];
2362
2363 /* expsc_noss uses marginalized emission probs */
2364 for(x = 0; x < cm->abc->K; x++)
2365 expsc_noss[v] += left_e[x] * left_esc[x];
2366 for(x = 0; x < cm->abc->K; x++)
2367 expsc_noss[v] += right_e[x] * right_esc[x];
2368
2369 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++) {
2370 y = cm->cfirst[v] + yoffset;
2371 expsc[v] += cm->t[v][yoffset] * (expsc[y] + cm->tsc[v][yoffset]);
2372 expsc_noss[v] += cm->t[v][yoffset] * (expsc_noss[y] + cm->tsc[v][yoffset]);
2373 }
2374 break;
2375
2376 case ML_st:
2377 case MR_st:
2378 case IL_st:
2379 case IR_st:
2380 for(x = 0; x < cm->abc->K; x++) {
2381 expsc[v] += cm->e[v][x] * cm->esc[v][x];
2382 expsc_noss[v] += cm->e[v][x] * cm->esc[v][x];
2383 }
2384 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++) {
2385 y = cm->cfirst[v] + yoffset;
2386 expsc[v] += cm->t[v][yoffset] * (expsc[y] + cm->tsc[v][yoffset]);
2387 expsc_noss[v] += cm->t[v][yoffset] * (expsc_noss[y] + cm->tsc[v][yoffset]);
2388 }
2389 break;
2390
2391 case S_st:
2392 case D_st:
2393 for (yoffset = 0; yoffset < cm->cnum[v]; yoffset++) {
2394 y = cm->cfirst[v] + yoffset;
2395 expsc[v] += cm->t[v][yoffset] * (expsc[y] + cm->tsc[v][yoffset]);
2396 expsc_noss[v] += cm->t[v][yoffset] * (expsc_noss[y] + cm->tsc[v][yoffset]);
2397 }
2398 break;
2399 }
2400 }
2401 /* must return ret_expsc */
2402 *ret_expsc = expsc;
2403 /* optionally return ret_expsc_noss */
2404 if(ret_expsc_noss != NULL) *ret_expsc_noss = expsc_noss;
2405 else free(expsc_noss);
2406
2407 for(v = 0; v < cm->M; v++)
2408 printf("EXPSC[%4d]: %10.6f NOSS: %10.6f (%10.6f)\n", v, expsc[v], expsc_noss[v], (expsc[v] - expsc_noss[v]));
2409
2410 return;
2411 ERROR:
2412 cm_Fail("ERROR in cm_CalcExpSc().\n");
2413 }
2414
2415 /* Function: cm_Validate()
2416 * Incept: EPN, Fri Jul 27 14:59:55 2007 [Janelia]
2417 *
2418 * Purpose: Validates some internals of the CM structure <cm>.
2419 *
2420 * Probability vectors are validated to sum up to
2421 * within a fractional tolerance <tol> of 1.0.
2422 *
2423 * Probably only useful for debugging and development,
2424 * not production code.
2425 *
2426 * Returns: <eslOK> if <cm> internals look fine.
2427 * Returns <eslFAIL> if something is wrong.
2428 */
2429 int
2430 cm_Validate(CM_t *cm, float tol, char *errbuf)
2431 {
2432 int status;
2433 int v;
2434 int clen = 0;
2435 float pvec[MAXCONNECT+1];
2436 int y;
2437
2438 if (cm == NULL) ESL_XFAIL(eslFAIL, errbuf, "CM is a null pointer");
2439 if (cm->M < 1) ESL_XFAIL(eslFAIL, errbuf, "CM has M < 1");
2440 if (cm->abc == NULL) ESL_XFAIL(eslFAIL, errbuf, "CM has no alphabet reference");
2441 if (cm->abc->type == eslUNKNOWN) ESL_XFAIL(eslFAIL, errbuf, "CM's alphabet is set to unknown");
2442 if (cm->qdbinfo == NULL) ESL_XFAIL(eslFAIL, errbuf, "CM's qdbinfo is NULL");
2443
2444 esl_vec_FSet(pvec, MAXCONNECT+1, 0.);
2445 for (v = 0; v < cm->M; v++)
2446 {
2447 if(StateDelta(cm->sttype[v]) == 2)
2448 {
2449 if (esl_vec_FValidate(cm->e[v], (cm->abc->K * cm->abc->K), tol, NULL) != eslOK)
2450 ESL_XFAIL(eslFAIL, errbuf, "e[%d] fails pvector validation", v);
2451 }
2452 else if(StateDelta(cm->sttype[v]) > 0)
2453 {
2454 if (esl_vec_FValidate(cm->e[v], cm->abc->K, tol, NULL) != eslOK)
2455 ESL_XFAIL(eslFAIL, errbuf, "e[%d] fails pvector validation", v);
2456 }
2457 if (cm->sttype[v] != B_st && cm->sttype[v] != E_st)
2458 {
2459 if ((! (cm->flags & CMH_LOCAL_BEGIN)) && (! (cm->flags & CMH_LOCAL_END)))
2460 {
2461 if(esl_vec_FValidate(cm->t[v], cm->cnum[v], tol, NULL) != eslOK)
2462 ESL_XFAIL(eslFAIL, errbuf, "t[%d] fails pvector validation", v);
2463 }
2464 else if (v > 0 && (cm->flags & CMH_LOCAL_END))
2465 {
2466 esl_vec_FSet(pvec, MAXCONNECT+1, 0.); /* not really nec */
2467 for(y = 0; y < cm->cnum[v]; y++) pvec[y] = cm->t[v][y];
2468 pvec[cm->cnum[v]] = cm->end[v];
2469 if(esl_vec_FValidate(pvec, (cm->cnum[v]+1), tol, NULL) != eslOK)
2470 ESL_XFAIL(eslFAIL, errbuf, "t[%d] (with local end) fails pvector validation", v);
2471 }
2472 }
2473 if(cm->stid[v] == MATL_ML) clen++;
2474 if(cm->stid[v] == MATR_MR) clen++;
2475 if(cm->stid[v] == MATP_MP) clen+=2;
2476 }
2477 if(cm->flags & CMH_LOCAL_BEGIN)
2478 if(esl_vec_FValidate(cm->begin, cm->M, tol, NULL) != eslOK)
2479 if(cm->clen != clen) ESL_XFAIL(eslFAIL, errbuf, "consensus length %d not correctly stored in CM, should be %d", cm->clen, clen);
2480
2481 return eslOK;
2482
2483 ERROR:
2484 return status;
2485 }
2486
2487 /* Function: CMStatetype()
2488 *
2489 * Purpose: Returns the CM state type in text.
2490 * Example: CP9Statetype(MP_st) = "MP"
2491 */
2492 char *
2493 CMStatetype(char st)
2494 {
2495 switch (st) {
2496 case D_st: return "D";
2497 case MP_st: return "MP";
2498 case ML_st: return "ML";
2499 case MR_st: return "MR";
2500 case IL_st: return "IL";
2501 case IR_st: return "IR";
2502 case S_st: return "S";
2503 case E_st: return "E";
2504 case B_st: return "B";
2505 case EL_st: return "EL";
2506 default: return "BOGUS";
2507 }
2508 }
2509
2510 /* Function: CMNodetype()
2511 *
2512 * Purpose: Returns the CM state type in text.
2513 * Example: CP9Statetype(MATP_nd) = "MATP"
2514 */
2515 char *
2516 CMNodetype(char nd)
2517 {
2518 switch (nd) {
2519 /*case DUMMY_nd: return "DUMMY";*/
2520 case BIF_nd: return "BIF";
2521 case MATP_nd: return "MATP";
2522 case MATL_nd: return "MATL";
2523 case MATR_nd: return "MATR";
2524 case BEGL_nd: return "BEGL";
2525 case BEGR_nd: return "BEGR";
2526 case ROOT_nd: return "ROOT";
2527 case END_nd: return "END";
2528 default: return "BOGUS";
2529 }
2530 }
2531
2532 /* Function: CMStateid()
2533 *
2534 * Purpose: Returns the CM state id in text.
2535 * Example: CP9Statetype(MATP_MP) = "MATP_MP"
2536 */
2537 char *
2538 CMStateid(char st)
2539 {
2540 switch (st) {
2541 /*case DUMMY: return "DUMMY";*/
2542 case ROOT_S: return "ROOT_S";
2543 case ROOT_IL: return "ROOT_IL";
2544 case ROOT_IR: return "ROOT_IR";
2545 case BEGL_S: return "BEGL_S";
2546 case BEGR_S: return "BEGR_S";
2547 case BEGR_IL: return "BEGR_IL";
2548 case MATP_MP: return "MATP_MP";
2549 case MATP_ML: return "MATP_ML";
2550 case MATP_MR: return "MATP_MR";
2551 case MATP_D: return "MATP_D";
2552 case MATP_IL: return "MATP_IL";
2553 case MATP_IR: return "MATP_IR";
2554 case MATL_ML: return "MATL_ML";
2555 case MATL_D: return "MATL_D";
2556 case MATL_IL: return "MATL_IL";
2557 case MATR_MR: return "MATR_MR";
2558 case MATR_D: return "MATR_D";
2559 case MATR_IR: return "MATR_IR";
2560 case END_E: return "END_E";
2561 case BIF_B: return "BIF_B";
2562 case END_EL: return "END_EL";
2563 default: return "BOGUS";
2564 }
2565 }
2566
2567
2568 /*****************************************************************
2569 * Convenience routines for setting fields in an CM. (from p7_hmm.c)
2570 *****************************************************************/
2571 /* Function: cm_SetName()
2572 * Incept: EPN, Fri Jul 27 16:49:49 2007 [Janelia]
2573 *
2574 * Purpose: Set or change the name of a CM to <name>.
2575 * Any trailing whitespace (including newline) is chopped off.
2576 *
2577 * Returns: <eslOK> on success.
2578 *
2579 * Throws: <eslEMEM> on allocation error, and original name (if any)
2580 * remains.
2581 */
2582 int
2583 cm_SetName(CM_t *cm, char *name)
2584 {
2585 int status;
2586 void *tmp;
2587 int n;
2588
2589 if (name == NULL) {
2590 if (cm->name != NULL) free(cm->name);
2591 cm->name = NULL;
2592 } else {
2593 n = strlen(name);
2594 ESL_RALLOC(cm->name, tmp, sizeof(char)*(n+1));
2595 strcpy(cm->name, name);
2596 if ((status = esl_strchop(cm->name, n)) != eslOK) goto ERROR;
2597 }
2598 return eslOK;
2599
2600 ERROR:
2601 return status;
2602 }
2603
2604 /* Function: cm_SetAccession()
2605 * Incept: SRE, Mon Jan 1 16:53:53 2007 [Casa de Gatos]
2606 *
2607 * Purpose: Set or change the accession number of a CM to <acc>,
2608 * and raise the <CMH_ACC> flag. Trailing whitespace (including newline)
2609 * is chopped.
2610 *
2611 * If <acc> is <NULL>, unset the CM's accession (if any) and drop
2612 * the <CMH_ACC> flag.
2613 *
2614 * Returns: <eslOK> on success.
2615 *
2616 * Throws: <eslEMEM> on allocation error, and original name (if any)
2617 * remains.
2618 */
2619 int
2620 cm_SetAccession(CM_t *cm, char *acc)
2621 {
2622 int status;
2623 void *tmp;
2624 int n;
2625
2626 if (acc == NULL) {
2627 if (cm->acc != NULL) free(cm->acc);
2628 cm->acc = NULL;
2629 cm->flags &= ~CMH_ACC;
2630 } else {
2631 n = strlen(acc);
2632 ESL_RALLOC(cm->acc, tmp, sizeof(char)*(n+1));
2633 strcpy(cm->acc, acc);
2634 if ((status = esl_strchop(cm->acc, n)) != eslOK) goto ERROR;
2635 cm->flags |= CMH_ACC;
2636 }
2637 return eslOK;
2638
2639 ERROR:
2640 return status;
2641 }
2642
2643 /* Function: cm_SetDescription()
2644 * Incept: SRE, Mon Jan 1 16:59:28 2007 [Casa de Gatos]
2645 *
2646 * Purpose: Set or change the description line of a Plan7 CM.
2647 * Trailing whitespace (including newline) is chopped.
2648 */
2649 int
2650 cm_SetDescription(CM_t *cm, char *desc)
2651 {
2652 int status;
2653 void *tmp;
2654 int n;
2655
2656 if (desc == NULL)
2657 {
2658 if (cm->desc != NULL) free(cm->desc);
2659 cm->desc = NULL;
2660 cm->flags &= ~CMH_DESC;
2661 }
2662 else
2663 {
2664 n = strlen(desc);
2665 ESL_RALLOC(cm->desc, tmp, sizeof(char)*(n+1));
2666 strcpy(cm->desc, desc);
2667 if ((status = esl_strchop(cm->desc, n)) != eslOK) goto ERROR;
2668 cm->flags |= CMH_DESC;
2669 }
2670 return eslOK;
2671
2672 ERROR:
2673 return status;
2674 }
2675
2676 /* Function: cm_SetConsensus()
2677 * Incept: EPN, Wed Jun 22 06:11:35 2011
2678 * (SRE p7_hmm_SetConsensus())
2679 *
2680 * Synopsis: Set the consensus residue line of the CM.
2681 *
2682 * Purpose: Sets the consensus annotation line of the model <cm>.
2683 *
2684 * Based on p7_hmm_SetConsensus() which is flexible to
2685 * setting the consensus as a single sequence or
2686 * a consensus from a multiple sequence alignment.
2687 * Here, only the latter case is handled, i.e.
2688 * <sq> should always be passed as NULL. But in the
2689 * future, we should relax this to allow for single
2690 * sequence models.
2691 *
2692 * The consensus sequence isn't even calculated here,
2693 * it must be passed in as part of the <cons> CMConsensus_t
2694 * object, which was created by CreateCMConsensus().
2695 *
2696 * So, currently, <sq> must null and <cons> must be non-null.
2697 *
2698 * Args: cm - model with valid probability parameters
2699 * cons - consensus information for the CM
2700 * sq - NULL if a standard model;
2701 * or the query sequence for a single-sequence model (NOT YET IMPLEMENTED)
2702 *
2703 * Returns: <eslOK> on success. The <CMH_CONS> flag on the <cm> is raised
2704 * if it wasn't already. The <cm->consensus> line is set.
2705 *
2706 * Throws: <eslEMEM> on allocation error. <eslEINVAL> if contract is violated.
2707 * In both cases, the <CMH_CONS> is dropped, even if it was up to
2708 * begin with, and the <cm->consensus> is <NULL>,
2709 * even if we had one to begin with.
2710 *
2711 * Xref: SRE:J8/26.
2712 */
2713 int
2714 cm_SetConsensus(CM_t *cm, CMConsensus_t *cons, ESL_SQ *sq)
2715 {
2716 int status;
2717 int cpos;
2718
2719 if(cons == NULL || sq != NULL) { status = eslEINVAL; goto ERROR; }
2720
2721 /* allocation, if needed */
2722 if (! cm->consensus) ESL_ALLOC(cm->consensus, sizeof(char) * (cm->clen+2));
2723
2724 /* copy cons->cseq, careful for off-by-one */
2725 cm->consensus[0] = ' ';
2726 for (cpos = 1; cpos <= cm->clen; cpos++) cm->consensus[cpos] = cons->cseq[cpos-1];
2727 cm->consensus[cm->clen+1] = '\0';
2728 cm->flags |= CMH_CONS;
2729 return eslOK;
2730
2731 ERROR:
2732 if (cm->consensus) free(cm->consensus);
2733 cm->consensus = NULL;
2734 cm->flags &= (~CMH_CONS);
2735 return status;
2736 }
2737
2738 /* Function: cm_AppendComlog()
2739 * Synopsis: Concatenate and append command line to the command line log.
2740 *
2741 * Purpose: Concatenate command line options and append as a new line in the
2742 * command line log. Command line log is multiline, with each line
2743 * ending in newline char, except for last line.
2744 *
2745 * Based on and nearly identical to HMMER's
2746 * p7_hmm_AppendComlog(). One difference is the <add_seed>
2747 * and <seed> parameters, if <use_seed> is TRUE we append
2748 * "--seed <seed>" to the command line string. This is
2749 * necessary to allow a user to reproduce 'cmbuild --refine
2750 * --gibbs' CM files, which use a random (and not recorded)
2751 * seed by default.
2752 *
2753 * Returns: <eslOK> on success.
2754 *
2755 * Throws: <eslEMEM> on allocation failure.
2756 */
2757 int
2758 cm_AppendComlog(CM_t *cm, int argc, char **argv, int add_seed, uint32_t seed)
2759 {
2760 int status;
2761 void *tmp;
2762 int n;
2763 int i;
2764 int seedlen;
2765 uint32_t temp;
2766 char *seedstr;
2767
2768 /* figure out length of added command line, and (re)allocate comlog */
2769 n = argc-1; /* account for 1 space per arg, except last one */
2770 for (i = 0; i < argc; i++) {
2771 n += strlen(argv[i]);
2772 }
2773 /* we may need to add '--seed <seed>' */
2774 if(add_seed) {
2775 temp = seed;
2776 seedlen = 1;
2777 while(temp > 0) { temp/=10; seedlen++; } /* determine length of stringized version of seed */
2778 seedlen += 8; /* strlen(' --seed ') */
2779 n += seedlen;
2780 }
2781
2782 if (cm->comlog != NULL) {
2783 n += strlen(cm->comlog) + 1; /* +1 for the \n we're going to add to the old comlog */
2784 ESL_RALLOC(cm->comlog, tmp, sizeof(char)* (n+1));
2785 strcat(cm->comlog, "\n");
2786 } else {
2787 ESL_ALLOC(cm->comlog, sizeof(char)* (n+1));
2788 *(cm->comlog) = '\0'; /* need this to make strcat work */
2789 }
2790
2791 for (i = 0; i < argc-1; i++)
2792 {
2793 strcat(cm->comlog, argv[i]);
2794 strcat(cm->comlog, " ");
2795 }
2796 strcat(cm->comlog, argv[argc-1]);
2797
2798 if(add_seed) {
2799 ESL_ALLOC(seedstr, sizeof(char) * (seedlen+1));
2800 snprintf(seedstr, seedlen+10, " --seed %" PRIu32 " ", seed); /* buffer length is seedlen + length(" --seed ") + 1 for null terminating char */
2801 strcat(cm->comlog, seedstr);
2802 free(seedstr);
2803 }
2804
2805 return eslOK;
2806
2807 ERROR:
2808 return status;
2809 }
2810
2811 /* Function: cm_SetCtime()
2812 * Synopsis: Timestamp a CM.
2813 *
2814 * Purpose: Set the <ctime> field in a new CM to the current time.
2815 *
2816 * Returns: <eslOK> on success.
2817 *
2818 * Throws: <eslEMEM> on allocation failure.
2819 * <eslESYS> if system calls fail to obtain (or format) the time.
2820 *
2821 * Notes: This function is based on hmmer's p7_hmm_SetCtime(),
2822 * All of the following notes are copied from there.
2823 *
2824 * This function calls <ctime_r()>, supposedly a part of the
2825 * ISO/IEC 9945-1:1996 (POSIX.1) standard, but not ANSI
2826 * C99, so we have potential portability problems here.
2827 *
2828 * A known one: <ctime_r()> is by default a three-argument
2829 * call on Solaris 10 systems. Our autoconf script sets
2830 * -D_POSIX_PTHREAD_SEMANTICS on Solaris systems to fix this
2831 * issue, requesting Solaris to use a compliant version of
2832 * ctime_r().
2833 *
2834 * We might want to use strftime() instead; that's what
2835 * POSIX 2008 recommends; but we'd still need localtime_r() or
2836 * its equivalent, and that has its own portability issues.
2837 *
2838 * Note to porters: it really doesn't matter what this
2839 * timestamp is. INFERNAL doesn't look at it, it's for human
2840 * notetaking. If you have to, set it to an empty string.
2841 *
2842 * TODO: Oi. Time is complicated. Easel should give us an
2843 * easy and portable call to generate time stamps like this;
2844 * an esl_time module, perhaps?
2845 */
2846 int
2847 cm_SetCtime(CM_t *cm)
2848 {
2849 char *s = NULL;
2850 time_t date;
2851 int status;
2852 const char *sde = getenv("SOURCE_DATE_EPOCH");
2853
2854 ESL_ALLOC(s, 32);
2855 if (sde) {
2856 date = strtoul(sde, NULL, 0);
2857 } else {
2858 if ((date = time(NULL)) == -1) { status = eslESYS; goto ERROR; }
2859 }
2860 if (ctime_r(&date, s) == NULL) { status = eslESYS; goto ERROR; }
2861 if ((status = esl_strchop(s, -1)) != eslOK) { goto ERROR; }
2862
2863 if (cm->ctime != NULL) free(cm->ctime);
2864 cm->ctime = s;
2865 return eslOK;
2866
2867 ERROR:
2868 if (s) free(s);
2869 return status;
2870 }
2871
2872 /*---------------- end, internal-setting routines ---------------*/
2873
2874 /* Function: IntMaxDigits()
2875 * Date: EPN, Fri Nov 30 14:51:12 2007
2876 *
2877 * Returns: The number of digits in INT_MAX.
2878 * Originally written to inform how big a
2879 * string must be if it wants to hold any
2880 * possible positive integer.
2881 */
2882 int
2883 IntMaxDigits()
2884 {
2885 int big = INT_MAX;
2886 return IntDigits(big);
2887 }
2888
2889
2890 /* Function: IntDigits()
2891 * Date: EPN, Fri May 23 06:02:22 2008
2892 *
2893 * Returns: The number of digits in <i>.
2894 */
2895 int
2896 IntDigits(int i)
2897 {
2898 int n = 0;
2899 while(i > 0) { i/=10; n++; }
2900 return n;
2901 }
2902
2903 /* Function: cm_GetAvgHitLen()
2904 * Synopsis: Calculate the average local and global hit length for a CM
2905 * Date: EPN, Thu Jan 17 05:52:00 2008
2906 *
2907 * Returns: eslOK on success, <ret_avgL_loc> and <ret_avgL_glb> set as average local/global hit length
2908 * eslEMEM if we're out of memory
2909 */
2910 int
2911 cm_GetAvgHitLen(CM_t *cm, char *errbuf, float *ret_avgL_loc, float *ret_avgL_glb)
2912 {
2913 int status;
2914 int Z;
2915 float avgL_loc;
2916 float avgL_glb;
2917 double *gamma0_loc;
2918 double *gamma0_glb;
2919 int n;
2920
2921 if((status = CalculateQueryDependentBands(cm, errbuf, NULL, DEFAULT_BETA_W, NULL, &gamma0_loc, &gamma0_glb, &Z)) != eslOK) return status;
2922 avgL_loc = avgL_glb = 0.;
2923 for(n = 0; n <= Z; n++) avgL_loc += gamma0_loc[n] * (float) n;
2924 for(n = 0; n <= Z; n++) avgL_glb += gamma0_glb[n] * (float) n;
2925 free(gamma0_loc);
2926 free(gamma0_glb);
2927
2928 if(ret_avgL_loc != NULL) *ret_avgL_loc = avgL_loc;
2929 if(ret_avgL_glb != NULL) *ret_avgL_glb = avgL_glb;
2930
2931 return eslOK;
2932 }
2933
2934 /* Function: CompareCMGuideTrees()
2935 * EPN, Tue Mar 6 08:32:12 2007
2936 *
2937 * Purpose: Given two CMs, cm1 and cm2, compare them, returning TRUE
2938 * iff they have the same guide tree (same node architecture).
2939 *
2940 * Args: cm1 - covariance model number 1
2941 * cm2 - covariance model number 2
2942 *
2943 * Returns: TRUE if CMs have same guide tree, FALSE otherwise
2944 */
2945 int
2946 CompareCMGuideTrees(CM_t *cm1, CM_t *cm2)
2947 {
2948 int nd;
2949 if(cm1->nodes != cm2->nodes) return FALSE;
2950 for(nd = 0; nd < cm1->nodes; nd++) {
2951 if(cm1->ndtype[nd] != cm2->ndtype[nd]) return FALSE;
2952 }
2953 return TRUE;
2954 }
2955
2956 /* Function: cm_nonconfigured_Verify()
2957 * EPN, Fri Dec 9 14:20:03 2011
2958 *
2959 * Purpose: Verify that a cm is non-configured by checking its
2960 * flags and internal variables. A design goal of Infernal
2961 * is that the only way to configure a CM is with the
2962 * cm_Configure() function (but that's difficult to
2963 * be absolutely sure of). The motivation for this
2964 * goal is so there's only 1 execution path through
2965 * all the configuration functions. If there are many
2966 * possible paths, dictated by combinations of options
2967 * to an application for example, it's very possible that
2968 * some of those paths screw something up.
2969 *
2970 * Args: cm - CM to check
2971 * errbuf - string explaining evidence CM is configured, if it is
2972 *
2973 * Returns: eslOK if CM does not seem to be configured
2974 * eslFAIL if the CM has been configured in some way.
2975 */
2976 int
2977 cm_nonconfigured_Verify(CM_t *cm, char *errbuf)
2978 {
2979 /* Check for flags that indicate the CM has been configured in
2980 * any way. A CM has been configured if it has been manipulated
2981 * after being read from a file.
2982 */
2983 if(cm->flags & CM_IS_CONFIGURED) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): CM_IS_CONFIGURED flag is up (should be down in a non-configured CM)");
2984 if(cm->flags & CMH_BITS) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): CMH_BITS flag is up (should be down in a non-configured CM)");
2985 if(cm->flags & CMH_LOCAL_BEGIN) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): CMH_LOCAL_BEGIN flag is up (should be down in a non-configured CM)");
2986 if(cm->flags & CMH_LOCAL_END) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): CMH_LOCAL_END flag is up (should be down in a non-configured CM)");
2987 if(cm->flags & CMH_CP9) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): CMH_CP9 flag is up (should be down in a non-configured CM)");
2988 if(cm->flags & CMH_CP9_TRUNC) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): CMH_CP9_TRUNC flag is up (should be down in a non-configured CM)");
2989 if(cm->flags & CM_EMIT_NO_LOCAL_BEGINS) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): CM_EMIT_NO_LOCAL_BEGINS flag is up (should be down in a non-configured CM)");
2990 if(cm->flags & CM_EMIT_NO_LOCAL_ENDS) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): CM_EMIT_NO_LOCAL_ENDS flag is up (should be down in a non-configured CM)");
2991 if(cm->flags & CMH_MLP7) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): CMH_MLP7 flag is up (should be down in a non-configured CM)");
2992
2993 /* verify variables that should be NULL are NULL */
2994 /* cp9-related variables */
2995 if(cm->cp9 != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): cp9 is non-NULL (it should be NULL in a non-configured CM)");
2996 if(cm->Lcp9 != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): Lcp9 is non-NULL (it should be NULL in a non-configured CM)");
2997 if(cm->Rcp9 != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): Rcp9 is non-NULL (it should be NULL in a non-configured CM)");
2998 if(cm->Tcp9 != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): Tcp9 is non-NULL (it should be NULL in a non-configured CM)");
2999 if(cm->cp9map != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): cp9map is non-NULL (it should be NULL in a non-configured CM)");
3000 if(cm->cp9b != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): cp9b is non-NULL (it should be NULL in a non-configured CM)");
3001 /* matrices */
3002 if(cm->hb_mx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): hb_mx is non-NULL (it should be NULL in a non-configured CM)");
3003 if(cm->hb_omx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): hb_omx is non-NULL (it should be NULL in a non-configured CM)");
3004 if(cm->hb_emx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): hb_emx is non-NULL (it should be NULL in a non-configured CM)");
3005 if(cm->hb_shmx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): hb_shmx is non-NULL (it should be NULL in a non-configured CM)");
3006 if(cm->trhb_mx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): trhb_mx is non-NULL (it should be NULL in a non-configured CM)");
3007 if(cm->trhb_omx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): trhb_omx is non-NULL (it should be NULL in a non-configured CM)");
3008 if(cm->trhb_emx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): trhb_emx is non-NULL (it should be NULL in a non-configured CM)");
3009 if(cm->trhb_shmx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): trhb_shmx is non-NULL (it should be NULL in a non-configured CM)");
3010 if(cm->nb_mx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): nb_mx is non-NULL (it should be NULL in a non-configured CM)");
3011 if(cm->nb_omx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): nb_omx is non-NULL (it should be NULL in a non-configured CM)");
3012 if(cm->nb_emx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): nb_emx is non-NULL (it should be NULL in a non-configured CM)");
3013 if(cm->nb_shmx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): nb_shmx is non-NULL (it should be NULL in a non-configured CM)");
3014 if(cm->trnb_mx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): trnb_mx is non-NULL (it should be NULL in a non-configured CM)");
3015 if(cm->trnb_omx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): trnb_omx is non-NULL (it should be NULL in a non-configured CM)");
3016 if(cm->trnb_emx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): trnb_emx is non-NULL (it should be NULL in a non-configured CM)");
3017 if(cm->trnb_shmx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): trnb_shmx is non-NULL (it should be NULL in a non-configured CM)");
3018 if(cm->smx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): smx is non-NULL (it should be NULL in a non-configured CM)");
3019 if(cm->trsmx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): trsmx is non-NULL (it should be NULL in a non-configured CM)");
3020 if(cm->cp9_mx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): cp9_mx is non-NULL (it should be NULL in a non-configured CM)");
3021 if(cm->cp9_bmx != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): cp9_bmx is non-NULL (it should be NULL in a non-configured CM)");
3022 /* other variables */
3023 if(cm->mlp7 != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): mlp7 is non-NULL (it should be NULL in a non-configured CM)");
3024 if(cm->root_trans != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): root_trans is non-NULL (it should be NULL in a non-configured CM)");
3025 if(cm->trp != NULL) ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): trp is non-NULL (it should be NULL in a non-configured CM)");
3026 if(cm->qdbinfo != NULL && cm->qdbinfo->setby == CM_QDBINFO_SETBY_BANDCALC) {
3027 ESL_FAIL(eslFAIL, errbuf, "cm_nonconfigured_Verify(): qdbinfo is not in its initial state");
3028 }
3029 return eslOK;
3030 }
3031
3032 /* Function: cm_Clone()
3033 * Incept: EPN, Thu Dec 15 05:43:25 2011
3034 *
3035 * Purpose: Duplicates a CM.
3036 *
3037 * For objects the CM refers to, new ones are created
3038 * either by cloning (e.g. cm->cp9, cm->mlp7) or by
3039 * creating a new one (e.g. cm->hb_mx, cm->smx).
3040 *
3041 * Args: cm - CM to clone
3042 * ret_cm - copy of CM, allocated here, must be free'd by caller.
3043 *
3044 * Returns: eslOK on success.
3045 * eslEMEM on memory error, errbuf filled.
3046 * eslEINCOMPAT on contract exception.
3047 * if(! eslOK) *ret_cm set to NULL, errbuf is filled.
3048 */
3049 int
3050 cm_Clone(CM_t *cm, char *errbuf, CM_t **ret_cm)
3051 {
3052 int status;
3053 CM_t *new = NULL;
3054 int i, v;
3055
3056 if ((new = CreateCM(cm->nodes, cm->M, cm->clen, cm->abc)) == NULL) { status = eslEMEM; goto ERROR;}
3057 /* CM is zeroed within CreateCMBody() which is called by CreateCM() */
3058
3059 if (esl_strdup(cm->name, -1, &(new->name)) != eslOK) { status = eslEMEM; goto ERROR;}
3060 if (cm->acc != NULL) { if (esl_strdup(cm->acc, -1, &(new->acc)) != eslOK) { status = eslEMEM; goto ERROR;} }
3061 if (cm->desc != NULL) { if (esl_strdup(cm->desc, -1, &(new->desc)) != eslOK) { status = eslEMEM; goto ERROR;} }
3062 if (cm->rf != NULL) { if (esl_strdup(cm->rf, -1, &(new->rf)) != eslOK) { status = eslEMEM; goto ERROR;} }
3063 if (cm->consensus != NULL) { if (esl_strdup(cm->consensus, -1, &(new->consensus)) != eslOK) { status = eslEMEM; goto ERROR;} }
3064 if(cm->map != NULL) {
3065 ESL_ALLOC(new->map, sizeof(int) * (new->clen+1));
3066 esl_vec_ICopy(cm->map, (new->clen+1), new->map);
3067 }
3068
3069 if ((cm->comlog != NULL) && (status = esl_strdup(cm->comlog, -1, &(new->comlog))) != eslOK) goto ERROR;
3070 if ((cm->ctime != NULL) && (status = esl_strdup(cm->ctime, -1, &(new->ctime))) != eslOK) goto ERROR;
3071
3072 new->flags = cm->flags;
3073 new->checksum = cm->checksum;
3074 new->nseq = cm->nseq;
3075 new->eff_nseq = cm->eff_nseq;
3076 new->ga = cm->ga;
3077 new->tc = cm->tc;
3078 new->nc = cm->nc;
3079 new->offset = cm->offset;
3080 new->clen = cm->clen;
3081 new->W = cm->W;
3082 new->W_setby = cm->W_setby;
3083 new->beta_W = cm->beta_W;
3084 new->pbegin = cm->pbegin;
3085 new->pend = cm->pend;
3086 new->null2_omega = cm->null2_omega;
3087 new->null3_omega = cm->null3_omega;
3088 new->el_selfsc = cm->el_selfsc;
3089 new->iel_selfsc = cm->iel_selfsc;
3090 new->tau = cm->tau;
3091 new->maxtau = cm->maxtau;
3092 new->config_opts = cm->config_opts;
3093 new->align_opts = cm->align_opts;
3094 new->search_opts = cm->search_opts;
3095
3096 esl_vec_FCopy(cm->null, cm->abc->K, new->null);
3097 esl_vec_ICopy(cm->ndidx, cm->M, new->ndidx);
3098 esl_vec_ICopy(cm->cfirst, cm->M, new->cfirst);
3099 esl_vec_ICopy(cm->cnum, cm->M, new->cnum);
3100 esl_vec_ICopy(cm->plast, cm->M, new->plast);
3101 esl_vec_ICopy(cm->pnum, cm->M, new->pnum);
3102 for(i = 0; i < cm->M+1; i++) new->sttype[i] = cm->sttype[i];
3103 for(i = 0; i < cm->M+1; i++) new->stid[i] = cm->stid[i];
3104
3105 esl_vec_ICopy(cm->nodemap, cm->nodes, new->nodemap);
3106 for(i = 0; i < cm->nodes; i++) new->ndtype[i] = cm->ndtype[i];
3107
3108 if(cm->root_trans != NULL) {
3109 ESL_ALLOC(new->root_trans, sizeof(float) * cm->cnum[0]);
3110 esl_vec_FCopy(cm->root_trans, cm->cnum[0], new->root_trans);
3111 }
3112
3113 /* emission and transition probabilities and scores (should be consistent with CMZero()) */
3114 for (v = 0; v < cm->M; v++) {
3115 esl_vec_FCopy(cm->e[v], (cm->abc->K * cm->abc->K), new->e[v]);
3116 esl_vec_FCopy(cm->t[v], MAXCONNECT, new->t[v]);
3117 esl_vec_FCopy(cm->tsc[v], MAXCONNECT, new->tsc[v]);
3118 esl_vec_FCopy(cm->esc[v], (cm->abc->K * cm->abc->K), new->esc[v]);
3119 esl_vec_FCopy(cm->lmesc[v], cm->abc->Kp, new->lmesc[v]);
3120 esl_vec_FCopy(cm->rmesc[v], cm->abc->Kp, new->rmesc[v]);
3121 esl_vec_ICopy(cm->itsc[v], MAXCONNECT, new->itsc[v]);
3122 esl_vec_ICopy(cm->iesc[v], (cm->abc->K * cm->abc->K), new->iesc[v]);
3123 esl_vec_ICopy(cm->ilmesc[v], cm->abc->Kp, new->ilmesc[v]);
3124 esl_vec_ICopy(cm->irmesc[v], cm->abc->Kp, new->irmesc[v]);
3125 }
3126 esl_vec_FCopy(cm->begin, cm->M, new->begin);
3127 esl_vec_FCopy(cm->end, cm->M, new->end);
3128 esl_vec_FCopy(cm->beginsc, cm->M, new->beginsc);
3129 esl_vec_FCopy(cm->endsc, cm->M, new->endsc);
3130 esl_vec_ICopy(cm->ibeginsc, cm->M, new->ibeginsc);
3131 esl_vec_ICopy(cm->iendsc, cm->M, new->iendsc);
3132
3133 /* oesc and ioesc (not yet allocated for in new) */
3134 if(cm->oesc != NULL && cm->ioesc == NULL) ESL_XFAIL(eslEINCOMPAT, errbuf, "cloning cm, cm->oesc != NULL and cm->ioesc == NULL");
3135 if(cm->oesc == NULL && cm->ioesc != NULL) ESL_XFAIL(eslEINCOMPAT, errbuf, "cloning cm, cm->oesc == NULL and cm->ioesc != NULL");
3136 if(cm->oesc != NULL && cm->ioesc != NULL) {
3137 if((status = CloneOptimizedEmitScores(cm, new, errbuf)) != eslOK) goto ERROR;
3138 }
3139
3140 /* create the emit map (just as easy as copying) */
3141 if(cm->emap != NULL) if((new->emap = CreateEmitMap(new)) == NULL) ESL_XFAIL(eslFAIL, errbuf, "Cloning CM, unable to create new emit map");
3142
3143 /* create the CM consensus data (just as easy as copying) */
3144 if(cm->cmcons != NULL && (new->flags & CMH_BITS)) if((new->cmcons = CreateCMConsensus(cm, cm->abc)) == NULL) ESL_XFAIL(eslFAIL, errbuf, "Cloning CM, unable to create a new CMConsensus_t");
3145
3146 /* create the truncation penalties (just as easy as copying) */
3147 if(cm->trp != NULL) if((new->trp = cm_tr_penalties_Create(new, cm->trp->ignored_inserts, errbuf)) == NULL) ESL_XFAIL(eslFAIL, errbuf, "Cloning CM, unable to create new truncation penalties");
3148
3149 /* QDBInfo */
3150 if(cm->qdbinfo != NULL) {
3151 if((status = CopyCMQDBInfo(cm->qdbinfo, new->qdbinfo, errbuf)) != eslOK) return status;
3152 }
3153
3154 /* cp9 HMMs */
3155 if(cm->cp9 != NULL) { if((new->cp9 = cp9_Clone(cm->cp9)) == NULL) ESL_XFAIL(eslFAIL, errbuf, "Cloning CM, couldn't clone cp9"); }
3156 if(cm->Lcp9 != NULL) { if((new->Lcp9 = cp9_Clone(cm->Lcp9)) == NULL) ESL_XFAIL(eslFAIL, errbuf, "Cloning CM, couldn't clone Lcp9"); }
3157 if(cm->Rcp9 != NULL) { if((new->Rcp9 = cp9_Clone(cm->Rcp9)) == NULL) ESL_XFAIL(eslFAIL, errbuf, "Cloning CM, couldn't clone Rcp9"); }
3158 if(cm->Tcp9 != NULL) { if((new->Tcp9 = cp9_Clone(cm->Tcp9)) == NULL) ESL_XFAIL(eslFAIL, errbuf, "Cloning CM, couldn't clone Tcp9"); }
3159 /* cp9map, don't clone, just make a new one */
3160 if(cm->cp9map != NULL) {
3161 new->cp9map = AllocCP9Map(new);
3162 CP9_map_cm2hmm(new, new->cp9map, 0); /* 0 is debug_level, for debugging output */
3163 /* cp9 bands and cp9 matrices, don't clone, just make new ones (these grow to fit a target sequence) */
3164 }
3165 if(cm->cp9b != NULL) new->cp9b = AllocCP9Bands(new->M, new->cp9->M);
3166 if(cm->cp9_mx != NULL) new->cp9_mx = CreateCP9Matrix(1, new->cp9->M);
3167 if(cm->cp9_bmx != NULL) new->cp9_bmx = CreateCP9Matrix(1, new->cp9->M);
3168
3169 /* p7 HMMs */
3170 if(cm->mlp7 != NULL) {
3171 if((new->mlp7 = p7_hmm_Clone(cm->mlp7)) == NULL) { status = eslEMEM; goto ERROR; }
3172 }
3173 if(cm->fp7 != NULL) {
3174 if((new->fp7 = p7_hmm_Clone(cm->fp7)) == NULL) { status = eslEMEM; goto ERROR; }
3175 esl_vec_FCopy(cm->fp7_evparam, CM_p7_NEVPARAM, new->fp7_evparam);
3176 }
3177
3178 /* CM HMM banded DP matrices, don't clone these, just make new ones (these grow to fit a target sequence) */
3179 if(cm->hb_mx != NULL) new->hb_mx = cm_hb_mx_Create(new->M);
3180 if(cm->hb_omx != NULL) new->hb_omx = cm_hb_mx_Create(new->M);
3181 if(cm->hb_emx != NULL) new->hb_emx = cm_hb_emit_mx_Create(new);
3182 if(cm->hb_shmx != NULL) new->hb_shmx = cm_hb_shadow_mx_Create(new);
3183 if(cm->trhb_mx != NULL) new->trhb_mx = cm_tr_hb_mx_Create(new);
3184 if(cm->trhb_omx != NULL) new->trhb_omx = cm_tr_hb_mx_Create(new);
3185 if(cm->trhb_emx != NULL) new->trhb_emx = cm_tr_hb_emit_mx_Create(new);
3186 if(cm->trhb_shmx != NULL) new->trhb_shmx = cm_tr_hb_shadow_mx_Create(new);
3187
3188 /* CM non-banded DP matrices, don't clone these, just make new ones (these grow to fit a target sequence) */
3189 if(cm->nb_mx != NULL) new->nb_mx = cm_mx_Create(new->M);
3190 if(cm->nb_omx != NULL) new->nb_omx = cm_mx_Create(new->M);
3191 if(cm->nb_emx != NULL) new->nb_emx = cm_emit_mx_Create(new);
3192 if(cm->nb_shmx != NULL) new->nb_shmx = cm_shadow_mx_Create(new);
3193 if(cm->trnb_mx != NULL) new->trnb_mx = cm_tr_mx_Create(new);
3194 if(cm->trnb_omx != NULL) new->trnb_omx = cm_tr_mx_Create(new);
3195 if(cm->trnb_emx != NULL) new->trnb_emx = cm_tr_emit_mx_Create(new);
3196 if(cm->trnb_shmx != NULL) new->trnb_shmx = cm_tr_shadow_mx_Create(new);
3197
3198 /* CM scan matrices, don't clone these either, just make new ones.
3199 * Importantly we've already copied cm->qdbinfo into new->qdbinfo.
3200 */
3201 if(cm->smx != NULL) { if((status = cm_scan_mx_Create (new, errbuf, cm->smx->floats_valid, cm->smx->ints_valid, &(new->smx))) != eslOK) goto ERROR; }
3202 if(cm->trsmx != NULL) { if((status = cm_tr_scan_mx_Create(new, errbuf, cm->trsmx->floats_valid, cm->trsmx->ints_valid, &(new->trsmx))) != eslOK) goto ERROR; }
3203
3204 /* expA */
3205 if(cm->expA != NULL) {
3206 ESL_ALLOC(new->expA, sizeof(ExpInfo_t *) * EXP_NMODES);
3207 for(i = 0; i < EXP_NMODES; i++) {
3208 new->expA[i] = CreateExpInfo();
3209 CopyExpInfo(cm->expA[i], new->expA[i]);
3210 }
3211 }
3212
3213 *ret_cm = new;
3214
3215 return eslOK;
3216
3217 ERROR:
3218 if(new != NULL) {
3219 FreeCM(new);
3220 *ret_cm = NULL;
3221 }
3222 if(status == eslEMEM) ESL_FAIL(status, errbuf, "Cloning CM, out of memory");
3223 return status; /* reached if status != eslEMEM, errbuf was filled earlier */
3224 }
3225
3226 /* Function: cm_Sizeof()
3227 * Incept: EPN, Wed Jan 18 04:53:29 2012
3228 *
3229 * Purpose: Calculate and return size of a CM_t object in Mb.
3230 *
3231 * Size will include all objects the CM refers to,
3232 * such as HMM banded matrices and scan matrices.
3233 *
3234 * Args: cm - CM to get size of
3235 *
3236 * Returns: size of CM_t in Mg (megabytes)
3237 */
3238 float
3239 cm_Sizeof(CM_t *cm)
3240 {
3241 float bytes = 0.;
3242
3243 bytes = sizeof(CM_t);
3244 /* from CreateCMBody() */
3245 if(cm->M > 0 && cm->abc != NULL) {
3246 bytes += sizeof(char) * (cm->M+1); /* cm->sttype */
3247 bytes += sizeof(int) * cm->M; /* cm->ndidx */
3248 bytes += sizeof(char) * (cm->M+1); /* cm->stid */
3249 bytes += sizeof(int) * cm->M; /* cm->cfirst */
3250 bytes += sizeof(int) * cm->M; /* cm->cnum */
3251 bytes += sizeof(int) * cm->M; /* cm->plast */
3252 bytes += sizeof(int) * cm->M; /* cm->pnum */
3253 bytes += sizeof(int) * cm->nodes; /* cm->nodemap */
3254 bytes += sizeof(int) * cm->nodes; /* cm->ndtype */
3255
3256 if(cm->comlog != NULL) bytes += sizeof(char) * strlen(cm->comlog);
3257 if(cm->ctime != NULL) bytes += sizeof(char) * strlen(cm->ctime);
3258 if(cm->qdbinfo != NULL) bytes += (1000000. * SizeofCMQDBInfo(cm->qdbinfo));
3259 if(cm->null != NULL) bytes += sizeof(float) * (cm->abc->K);
3260
3261 bytes += sizeof(float *) * cm->M; /* cm->t level 1 ptrs */
3262 bytes += sizeof(float *) * cm->M; /* cm->e level 1 ptrs */
3263 bytes += sizeof(float *) * cm->M; /* cm->tsc level 1 ptrs */
3264 bytes += sizeof(float *) * cm->M; /* cm->esc level 1 ptrs */
3265 bytes += sizeof(float *) * cm->M; /* cm->lmesc level 1 ptrs */
3266 bytes += sizeof(float *) * cm->M; /* cm->rmesc level 1 ptrs */
3267 bytes += sizeof(int *) * cm->M; /* cm->itsc level 1 ptrs */
3268 bytes += sizeof(int *) * cm->M; /* cm->iesc level 1 ptrs */
3269 bytes += sizeof(int *) * cm->M; /* cm->ilmesc level 1 ptrs */
3270 bytes += sizeof(int *) * cm->M; /* cm->irmesc level 1 ptrs */
3271
3272 bytes += sizeof(float) * cm->M; /* cm->begin */
3273 bytes += sizeof(float) * cm->M; /* cm->end */
3274 bytes += sizeof(float) * cm->M; /* cm->beginsc */
3275 bytes += sizeof(float) * cm->M; /* cm->endsc */
3276 bytes += sizeof(int) * cm->M; /* cm->ibeginsc */
3277 bytes += sizeof(int) * cm->M; /* cm->iendsc */
3278
3279 bytes += sizeof(float) * MAXCONNECT * cm->M; /* cm->t level 2 */
3280 bytes += sizeof(float) * cm->abc->K * cm->abc->K * cm->M; /* cm->e level 2 */
3281 bytes += sizeof(float) * MAXCONNECT * cm->M; /* cm->tsc level 2 */
3282 bytes += sizeof(float) * cm->abc->K * cm->abc->K * cm->M; /* cm->esc level 2 */
3283 bytes += sizeof(float) * cm->abc->Kp * cm->M; /* cm->lmesc level 2 */
3284 bytes += sizeof(float) * cm->abc->Kp * cm->M; /* cm->rmesc level 2 */
3285 bytes += sizeof(int) * MAXCONNECT * cm->M; /* cm->itsc level 2 */
3286 bytes += sizeof(int) * cm->abc->K * cm->abc->K * cm->M; /* cm->iesc level 2 */
3287 bytes += sizeof(int) * cm->abc->Kp * cm->M; /* cm->ilmesc level 2 */
3288 bytes += sizeof(int) * cm->abc->Kp * cm->M; /* cm->irmesc level 2 */
3289 }
3290
3291 if(cm->name != NULL) bytes += sizeof(char) * (strlen(cm->name) + 2);
3292 if(cm->acc != NULL) bytes += sizeof(char) * (strlen(cm->acc) + 2);
3293 if(cm->desc != NULL) bytes += sizeof(char) * (strlen(cm->desc) + 2);
3294 if(cm->rf != NULL) bytes += sizeof(char) * (strlen(cm->rf) + 2);
3295 if(cm->consensus != NULL) bytes += sizeof(char) * (strlen(cm->consensus) + 2);
3296 if(cm->map != NULL) bytes += sizeof(int) * (cm->clen+1);
3297 if(cm->root_trans != NULL) bytes += sizeof(float) * (cm->cnum[0]);
3298
3299 if(cm->oesc != NULL || cm->ioesc != NULL) {
3300 int nsinglets = 0;
3301 int npairs = 0;
3302 int v;
3303 for(v = 0; v < cm->M; v++) {
3304 if(cm->sttype[v] == IL_st || cm->sttype[v] == ML_st || cm->sttype[v] == IR_st || cm->sttype[v] == MR_st) nsinglets++;
3305 if(cm->sttype[v] == MP_st) npairs++;
3306 }
3307 if(cm->oesc != NULL) {
3308 bytes += sizeof(float *) * cm->M;
3309 bytes += sizeof(float) * ((cm->abc->Kp * nsinglets) + (cm->abc->Kp * cm->abc->Kp * npairs));
3310 }
3311 if(cm->ioesc != NULL) {
3312 bytes += sizeof(int *) * cm->M;
3313 bytes += sizeof(int) * ((cm->abc->Kp * nsinglets) + (cm->abc->Kp * cm->abc->Kp * npairs));
3314 }
3315 }
3316
3317 /* cp9 HMMs */
3318 if(cm->cp9 != NULL) bytes += (1000000. * cp9_Sizeof(cm->cp9));
3319 if(cm->Lcp9 != NULL) bytes += (1000000. * cp9_Sizeof(cm->Lcp9));
3320 if(cm->Rcp9 != NULL) bytes += (1000000. * cp9_Sizeof(cm->Rcp9));
3321 if(cm->Tcp9 != NULL) bytes += (1000000. * cp9_Sizeof(cm->Tcp9));
3322
3323 if(cm->cp9map != NULL) bytes += (1000000. * SizeofCP9Map(cm->cp9map));
3324 if(cm->cp9b != NULL) bytes += (1000000. * SizeofCP9Bands(cm->cp9b));
3325 if(cm->cp9_mx != NULL) bytes += cm->cp9_mx->size_Mb;
3326 if(cm->cp9_bmx != NULL) bytes += cm->cp9_bmx->size_Mb;
3327
3328 /* p7 HMMs */
3329 if(cm->mlp7 != NULL) bytes += (1000000. * cm_p7_hmm_Sizeof(cm->mlp7));
3330 if(cm->fp7 != NULL) bytes += (1000000. * cm_p7_hmm_Sizeof(cm->fp7));
3331
3332 /* CM HMM banded DP matrices */
3333 if(cm->hb_mx != NULL) bytes += (1000000. * cm->hb_mx->size_Mb);
3334 if(cm->hb_omx != NULL) bytes += (1000000. * cm->hb_omx->size_Mb);
3335 if(cm->hb_emx != NULL) bytes += (1000000. * cm->hb_emx->size_Mb);
3336 if(cm->hb_shmx != NULL) bytes += (1000000. * cm->hb_shmx->size_Mb);
3337 if(cm->trhb_mx != NULL) bytes += (1000000. * cm->trhb_mx->size_Mb);
3338 if(cm->trhb_omx != NULL) bytes += (1000000. * cm->trhb_omx->size_Mb);
3339 if(cm->trhb_emx != NULL) bytes += (1000000. * cm->trhb_emx->size_Mb);
3340 if(cm->trhb_shmx != NULL) bytes += (1000000. * cm->trhb_shmx->size_Mb);
3341
3342 /* CM non-banded DP matrices */
3343 if(cm->nb_mx != NULL) bytes += (1000000. * cm->nb_mx->size_Mb);
3344 if(cm->nb_omx != NULL) bytes += (1000000. * cm->nb_omx->size_Mb);
3345 if(cm->nb_emx != NULL) bytes += (1000000. * cm->nb_emx->size_Mb);
3346 if(cm->nb_shmx != NULL) bytes += (1000000. * cm->nb_shmx->size_Mb);
3347 if(cm->trnb_mx != NULL) bytes += (1000000. * cm->trnb_mx->size_Mb);
3348 if(cm->trnb_omx != NULL) bytes += (1000000. * cm->trnb_omx->size_Mb);
3349 if(cm->trnb_emx != NULL) bytes += (1000000. * cm->trnb_emx->size_Mb);
3350 if(cm->trnb_shmx != NULL) bytes += (1000000. * cm->trnb_shmx->size_Mb);
3351
3352 /* CM scan matrices */
3353 if(cm->smx != NULL) bytes += (1000000. * cm->smx->size_Mb);
3354 if(cm->trsmx != NULL) bytes += (1000000. * cm->trsmx->size_Mb);
3355
3356 /* expA */
3357 if(cm->expA != NULL) {
3358 bytes += sizeof(ExpInfo_t *) * EXP_NMODES;
3359 bytes += sizeof(ExpInfo_t) * EXP_NMODES;
3360 }
3361
3362 /* the emit map */
3363 if(cm->emap != NULL) bytes += (1000000. * SizeofEmitMap(cm, cm->emap));
3364
3365 /* CM_TR_PENALTIES */
3366 if(cm->trp != NULL) bytes += (1000000. * cm_tr_penalties_Sizeof(cm->trp));
3367
3368 return (bytes / 1000000.);
3369 }
3370
3371 /* Function: DumpCMFlags()
3372 * Date: EPN, Wed Jun 22 19:24:54 2011
3373 *
3374 * Purpose: Print flags that are raised in a CM.
3375 *
3376 * Returns: void.
3377 */
3378 void
3379 DumpCMFlags(FILE *fp, CM_t *cm)
3380 {
3381 fprintf(fp, "Dumping CM flags:\n");
3382 if(cm->flags & CMH_BITS) fprintf(fp, "\tCMH_BITS\n");
3383 if(cm->flags & CMH_ACC) fprintf(fp, "\tCMH_ACC\n");
3384 if(cm->flags & CMH_DESC) fprintf(fp, "\tCMH_DESC\n");
3385 if(cm->flags & CMH_RF) fprintf(fp, "\tCMH_RF\n");
3386 if(cm->flags & CMH_GA) fprintf(fp, "\tCMH_GA\n");
3387 if(cm->flags & CMH_TC) fprintf(fp, "\tCMH_TC\n");
3388 if(cm->flags & CMH_NC) fprintf(fp, "\tCMH_NC\n");
3389 if(cm->flags & CMH_CHKSUM) fprintf(fp, "\tCMH_CHKSUM\n");
3390 if(cm->flags & CMH_MAP) fprintf(fp, "\tCMH_MAP\n");
3391 if(cm->flags & CMH_CONS) fprintf(fp, "\tCMH_CONS\n");
3392 if(cm->flags & CMH_LOCAL_BEGIN) fprintf(fp, "\tCMH_LOCAL_BEGIN\n");
3393 if(cm->flags & CMH_LOCAL_END) fprintf(fp, "\tCMH_LOCAL_END\n");
3394 if(cm->flags & CMH_EXPTAIL_STATS) fprintf(fp, "\tCMH_EXPTAIL_STATS\n");
3395 if(cm->flags & CMH_CP9) fprintf(fp, "\tCMH_CP9\n");
3396 if(cm->flags & CMH_CP9_TRUNC) fprintf(fp, "\tCMH_CP9_TRUNC\n");
3397 if(cm->flags & CMH_MLP7) fprintf(fp, "\tCMH_MLP7\n");
3398 if(cm->flags & CMH_FP7) fprintf(fp, "\tCMH_FP7\n");
3399
3400 if(cm->flags & CM_IS_SUB) fprintf(fp, "\tCM_IS_SUB\n");
3401 if(cm->flags & CM_IS_RSEARCH) fprintf(fp, "\tCM_IS_RSEARCH\n");
3402 if(cm->flags & CM_RSEARCHTRANS) fprintf(fp, "\tCM_RSEARCHTRANS\n");
3403 if(cm->flags & CM_RSEARCHEMIT) fprintf(fp, "\tCM_RSEARCHEMIT\n");
3404 if(cm->flags & CM_EMIT_NO_LOCAL_BEGINS) fprintf(fp, "\tCM_EMIT_NO_LOCAL_BEGINS\n");
3405 if(cm->flags & CM_EMIT_NO_LOCAL_ENDS) fprintf(fp, "\tCM_EMIT_NO_LOCAL_ENDS\n");
3406
3407 fprintf(fp, "Finished dumping CM flags.\n");
3408 return;
3409 }
3410
3411
3412 /* Function: cm_CreateDefaultApp()
3413 * Synopsis: Initialize a small/simple/standard INFERNAL application
3414 * Incept: EPN, Fri Jul 1 05:18:21 2011
3415 * SRE, Thu Oct 28 15:03:21 2010 [Janelia] (p7_CreateDefaultApp())
3416 *
3417 * Purpose: Identical to <esl_getopts_CreateDefaultApp()>, but
3418 * specialized for INFERNAL. See documentation in
3419 * <easel/esl_getopts.c>.
3420 *
3421 * Args: options - array of <ESL_OPTIONS> structures for getopts
3422 * nargs - number of cmd line arguments expected (excl. of cmdname)
3423 * argc - <argc> from main()
3424 * argv - <argv> from main()
3425 * banner - optional one-line description of program (or NULL)
3426 * usage - optional one-line usage hint (or NULL)
3427 *
3428 * Returns: ptr to new <ESL_GETOPTS> object.
3429 *
3430 * On command line errors, this routine prints an error
3431 * message to <stderr> then calls <exit(1)> to halt
3432 * execution with abnormal (1) status.
3433 *
3434 * If the standard <-h> option is seen, the routine prints
3435 * the help page (using the data in the <options> structure),
3436 * then calls <exit(0)> to exit with normal (0) status.
3437 *
3438 * Xref: J7/3
3439 *
3440 * Note: The only difference between this and esl_getopts_CreateDefaultApp()
3441 * is to call cm_banner() instead of esl_banner(), to get INFERNAL
3442 * versioning info into the header. There ought to be a better way
3443 * (perhaps using PACKAGE_* define's instead of INFERNAL_* vs. EASEL_*
3444 * define's in esl_banner(), thus removing the need for cm_banner).
3445 */
3446 ESL_GETOPTS *
3447 cm_CreateDefaultApp(ESL_OPTIONS *options, int nargs, int argc, char **argv, char *banner, char *usage)
3448 {
3449 ESL_GETOPTS *go = NULL;
3450
3451 go = esl_getopts_Create(options);
3452 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK ||
3453 esl_opt_VerifyConfig(go) != eslOK)
3454 {
3455 printf("Failed to parse command line: %s\n", go->errbuf);
3456 if (usage != NULL) esl_usage(stdout, argv[0], usage);
3457 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
3458 exit(1);
3459 }
3460 if (esl_opt_GetBoolean(go, "-h") == TRUE)
3461 {
3462 if (banner != NULL) cm_banner(stdout, argv[0], banner);
3463 if (usage != NULL) esl_usage (stdout, argv[0], usage);
3464 puts("\nOptions:");
3465 esl_opt_DisplayHelp(stdout, go, 0, 2, 80);
3466 exit(0);
3467 }
3468 if (esl_opt_ArgNumber(go) != nargs)
3469 {
3470 puts("Incorrect number of command line arguments.");
3471 esl_usage(stdout, argv[0], usage);
3472 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
3473 exit(1);
3474 }
3475 return go;
3476 }
3477
3478 /* Function: cm_p7_oprofile_CreateBlock()
3479 * Synopsis: Create a new block of empty <CM_P7_OM_BLOCK>.
3480 * Incept: EPN, Wed Jul 6 11:39:20 2011
3481 *
3482 * Purpose: Creates a block of empty <CM_P7_OM_BLOCK> CM objects.
3483 *
3484 * Returns: a pointer to the new <CM_P7_OM_BLOCK>. Caller frees this
3485 * with <cm_p7_oprofile_DestroyBlock()>.
3486 *
3487 * Throws: <NULL> if allocation fails.
3488 */
3489 CM_P7_OM_BLOCK *
3490 cm_p7_oprofile_CreateBlock(int count)
3491 {
3492 int i = 0;
3493
3494 CM_P7_OM_BLOCK *block = NULL;
3495 int status = eslOK;
3496
3497 ESL_ALLOC(block, sizeof(*block));
3498
3499 block->count = 0;
3500 block->idx0 = 0;
3501 block->listSize = 0;
3502 block->list = NULL;
3503 block->msvdataA = NULL;
3504 block->cm_offsetA = NULL;
3505 block->cm_clenA = NULL;
3506 block->cm_WA = NULL;
3507 block->cm_nbpA = NULL;
3508 block->gfmuA = NULL;
3509 block->gflambdaA = NULL;
3510 block->clan_idxA = NULL;
3511
3512 ESL_ALLOC(block->list, sizeof(P7_OPROFILE *) * count);
3513 ESL_ALLOC(block->msvdataA, sizeof(P7_SCOREDATA *)* count);
3514 ESL_ALLOC(block->cm_offsetA, sizeof(off_t) * count);
3515 ESL_ALLOC(block->cm_clenA, sizeof(int) * count);
3516 ESL_ALLOC(block->cm_WA, sizeof(int) * count);
3517 ESL_ALLOC(block->cm_nbpA, sizeof(int) * count);
3518 ESL_ALLOC(block->gfmuA, sizeof(float) * count);
3519 ESL_ALLOC(block->gflambdaA, sizeof(float) * count);
3520 ESL_ALLOC(block->clan_idxA, sizeof(int) * count);
3521 block->listSize = count;
3522
3523 for (i = 0; i < count; ++i)
3524 {
3525 block->list[i] = NULL;
3526 block->msvdataA[i] = NULL;
3527 block->cm_offsetA[i] = 0;
3528 block->cm_clenA[i] = 0;
3529 block->cm_WA[i] = 0;
3530 block->cm_nbpA[i] = 0;
3531 block->gfmuA[i] = 0.;
3532 block->gflambdaA[i] = 0.;
3533 block->clan_idxA[i] = -1;
3534 }
3535
3536 return block;
3537
3538 ERROR:
3539 if (block != NULL)
3540 {
3541 if (block->list != NULL) free(block->list);
3542 if (block->msvdataA != NULL) free(block->msvdataA);
3543 if (block->cm_offsetA != NULL) free(block->cm_offsetA);
3544 if (block->cm_clenA != NULL) free(block->cm_clenA);
3545 if (block->cm_WA != NULL) free(block->cm_WA);
3546 if (block->cm_nbpA != NULL) free(block->cm_clenA);
3547 if (block->gfmuA != NULL) free(block->gfmuA);
3548 if (block->gflambdaA != NULL) free(block->gflambdaA);
3549 if (block->clan_idxA != NULL) free(block->clan_idxA);
3550 free(block);
3551 }
3552
3553 return NULL;
3554 }
3555
3556 /* Function: cm_p7_oprofile_DestroyBlock()
3557 * Synopsis: Frees an <CM_P7_OPROFILE_BLOCK>.
3558 * Incept:
3559 *
3560 * Purpose: Free a Create()'d block of profiles.
3561 */
3562 void
3563 cm_p7_oprofile_DestroyBlock(CM_P7_OM_BLOCK *block)
3564 {
3565 int i;
3566
3567 if (block == NULL) return;
3568
3569 if (block->list != NULL) {
3570 for (i = 0; i < block->listSize; ++i) {
3571 if (block->list[i] != NULL) p7_oprofile_Destroy(block->list[i]);
3572 }
3573 free(block->list);
3574 }
3575 if (block->msvdataA != NULL) {
3576 for (i = 0; i < block->listSize; ++i) {
3577 if (block->msvdataA[i] != NULL) p7_hmm_ScoreDataDestroy(block->msvdataA[i]);
3578 }
3579 free(block->msvdataA);
3580 }
3581 if (block->cm_offsetA != NULL) free(block->cm_offsetA);
3582 if (block->cm_clenA != NULL) free(block->cm_clenA);
3583 if (block->cm_WA != NULL) free(block->cm_WA);
3584 if (block->cm_nbpA != NULL) free(block->cm_nbpA);
3585 if (block->gfmuA != NULL) free(block->gfmuA);
3586 if (block->gflambdaA != NULL) free(block->gflambdaA);
3587 if (block->clan_idxA != NULL) free(block->clan_idxA);
3588
3589 free(block);
3590 return;
3591 }
3592
3593
3594 /* Function: FCalcOptimizedEmitScores()
3595 * Date: EPN, Tue Nov 6 17:24:45 2007
3596 *
3597 * Purpose: Allocate, fill and return an optimized emission score vector
3598 * of float scores for fast search/alignment.
3599 *
3600 * Returns: the 2D float emission score vector on success,
3601 * dies immediately on memory allocation error.
3602 */
3603 float **
3604 FCalcOptimizedEmitScores(CM_t *cm)
3605 {
3606 int status;
3607 float **esc_vAA;
3608 ESL_DSQ a,b;
3609 int v;
3610 int cur_cell;
3611 int npairs = 0;
3612 int nsinglets = 0;
3613 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 */
3614 float **leftAA;
3615 float **rightAA;
3616
3617 /* count pairs, singlets */
3618 for(v = 0; v < cm->M; v++) {
3619 switch(cm->sttype[v]) {
3620 case IL_st:
3621 case ML_st:
3622 case IR_st:
3623 case MR_st:
3624 nsinglets++;
3625 break;
3626 case MP_st:
3627 npairs++;
3628 break;
3629 }
3630 }
3631
3632 /* set up our left and right vectors for all possible non-canonical residues,
3633 * these are calc'ed once and passed to FastPairScore*() functions to minimize
3634 * run time.
3635 */
3636 ESL_ALLOC(leftAA, sizeof(float *) * cm->abc->Kp);
3637 ESL_ALLOC(rightAA, sizeof(float *) * cm->abc->Kp);
3638 for(a = 0; a <= cm->abc->K; a++) leftAA[a] = rightAA[a] = NULL; /* canonicals and gap, left/right unnec */
3639 for(a = cm->abc->K+1; a < cm->abc->Kp-1; a++) {
3640 ESL_ALLOC(leftAA[a], sizeof(float) * cm->abc->K);
3641 ESL_ALLOC(rightAA[a], sizeof(float) * cm->abc->K);
3642 esl_vec_FSet(leftAA[a], cm->abc->K, 0.);
3643 esl_vec_FSet(rightAA[a], cm->abc->K, 0.);
3644 esl_abc_FCount(cm->abc, leftAA[a], a, 1.);
3645 esl_abc_FCount(cm->abc, rightAA[a], a, 1.);
3646 }
3647 leftAA[cm->abc->Kp-1] = rightAA[cm->abc->Kp-1] = NULL; /* missing data, left/right unnec */
3648
3649 /* precalculate possible emission scores for each state */
3650 ESL_ALLOC(esc_vAA, sizeof(float *) * (cm->M));
3651 ESL_ALLOC(esc_vAA[0], sizeof(float) * ((cm->abc->Kp * nsinglets) + (cm->abc->Kp * cm->abc->Kp * npairs)));
3652 ptr_to_start = esc_vAA[0];
3653 cur_cell = 0;
3654 for(v = 0; v < cm->M; v++) {
3655 switch(cm->sttype[v]) {
3656 case IL_st:
3657 case ML_st:
3658 case IR_st:
3659 case MR_st:
3660 esc_vAA[v] = ptr_to_start + cur_cell;
3661 cur_cell += cm->abc->Kp;
3662 for(a = 0; a < cm->abc->K; a++) /* all canonical residues */
3663 esc_vAA[v][a] = cm->esc[v][a];
3664 esc_vAA[v][cm->abc->K] = IMPOSSIBLE; /* gap symbol is impossible */
3665 for(a = cm->abc->K+1; a < cm->abc->Kp-1; a++) /* all ambiguous residues */
3666 esc_vAA[v][a] = esl_abc_FAvgScore(cm->abc, a, cm->esc[v]);
3667 esc_vAA[v][cm->abc->Kp-1] = IMPOSSIBLE; /* missing data is IMPOSSIBLE */
3668 break;
3669 case MP_st:
3670 esc_vAA[v] = ptr_to_start + cur_cell;
3671 esl_vec_FSet(esc_vAA[v], cm->abc->Kp * cm->abc->Kp, IMPOSSIBLE); /* init all cells to IMPOSSIBLE */
3672 cur_cell += cm->abc->Kp * cm->abc->Kp;
3673 /* a is canonical, b is canonical */
3674 for(a = 0; a < cm->abc->K; a++) {
3675 for(b = 0; b < cm->abc->K; b++) {
3676 esc_vAA[v][(a * cm->abc->Kp) + b] = cm->esc[v][(a * cm->abc->K) + b];
3677 }
3678 }
3679 /* a is not canonical, b is canonical */
3680 for(a = cm->abc->K+1; a < cm->abc->Kp-1; a++) {
3681 for(b = 0; b < cm->abc->K; b++) {
3682 esc_vAA[v][(a * cm->abc->Kp) + b] = FastPairScoreLeftOnlyDegenerate(cm->abc->K, cm->esc[v], leftAA[a], b);
3683 }
3684 }
3685 /* a is canonical, b is not canonical */
3686 for(a = 0; a < cm->abc->K; a++) {
3687 for(b = cm->abc->K+1; b < cm->abc->Kp-1; b++) {
3688 esc_vAA[v][(a * cm->abc->Kp) + b] = FastPairScoreRightOnlyDegenerate(cm->abc->K, cm->esc[v], rightAA[b], a);
3689 }
3690 }
3691 /* a is not canonical, b is not canonical */
3692 for(a = cm->abc->K+1; a < cm->abc->Kp-1; a++) {
3693 for(b = cm->abc->K+1; b < cm->abc->Kp-1; b++) {
3694 esc_vAA[v][(a * cm->abc->Kp) + b] = FastPairScoreBothDegenerate(cm->abc->K, cm->esc[v], leftAA[a], rightAA[b]);
3695 }
3696 }
3697 /* everything else, when either a or b is gap or missing data, stays IMPOSSIBLE */
3698 break;
3699 default:
3700 esc_vAA[v] = NULL;
3701 break;
3702 }
3703 }
3704 for(a = 0; a < cm->abc->Kp; a++) {
3705 if(leftAA[a] != NULL) free(leftAA[a]);
3706 if(rightAA[a] != NULL) free(rightAA[a]);
3707 }
3708 free(leftAA);
3709 free(rightAA);
3710 return esc_vAA;
3711
3712 ERROR:
3713 cm_Fail("memory allocation error.");
3714 return NULL; /* NEVERREACHED */
3715 }
3716
3717 /* Function: ICalcOptimizedEmitScores()
3718 * Date: EPN, Tue Nov 6 17:27:34 2007
3719 *
3720 * Purpose: Allocate, fill and return an optimized emission score vector
3721 * of integer scores for fast search/alignment.
3722 *
3723 * Returns: the 2D integer emission score vector on success,
3724 * dies immediately on memory allocation error.
3725 */
3726 int **
3727 ICalcOptimizedEmitScores(CM_t *cm)
3728 {
3729 int status;
3730 int **iesc_vAA;
3731 ESL_DSQ a,b;
3732 int v;
3733 int cur_cell;
3734 int npairs = 0;
3735 int nsinglets = 0;
3736 int *ptr_to_start; /* points to block allocated to iesc_vAA[0], nec b/c esc_vAA[0] gets set to NULL, because v == 0 is non-emitter */
3737 float **leftAA;
3738 float **rightAA;
3739
3740 /* count pairs, singlets */
3741 for(v = 0; v < cm->M; v++) {
3742 switch(cm->sttype[v]) {
3743 case IL_st:
3744 case ML_st:
3745 case IR_st:
3746 case MR_st:
3747 nsinglets++;
3748 break;
3749 case MP_st:
3750 npairs++;
3751 break;
3752 }
3753 }
3754
3755 /* set up our left and right vectors for all possible non-canonical residues,
3756 * these are calc'ed once and passed to FastPairScore*() functions to minimize
3757 * run time.
3758 */
3759 ESL_ALLOC(leftAA, sizeof(float *) * cm->abc->Kp);
3760 ESL_ALLOC(rightAA, sizeof(float *) * cm->abc->Kp);
3761 for(a = 0; a <= cm->abc->K; a++) leftAA[a] = rightAA[a] = NULL; /* canonicals and gap, left/right unnec */
3762 for(a = cm->abc->K+1; a < cm->abc->Kp-1; a++) {
3763 ESL_ALLOC(leftAA[a], sizeof(float) * cm->abc->K);
3764 ESL_ALLOC(rightAA[a], sizeof(float) * cm->abc->K);
3765 esl_vec_FSet(leftAA[a], cm->abc->K, 0.);
3766 esl_vec_FSet(rightAA[a], cm->abc->K, 0.);
3767 esl_abc_FCount(cm->abc, leftAA[a], a, 1.);
3768 esl_abc_FCount(cm->abc, rightAA[a], a, 1.);
3769 }
3770 leftAA[cm->abc->Kp-1] = rightAA[cm->abc->Kp-1] = NULL; /* missing data, left/right unnec */
3771
3772 /* precalculate possible emission scores for each state */
3773 ESL_ALLOC(iesc_vAA, sizeof(int *) * (cm->M));
3774 ESL_ALLOC(iesc_vAA[0], sizeof(int) * ((cm->abc->Kp * nsinglets) + (cm->abc->Kp * cm->abc->Kp * npairs)));
3775 ptr_to_start = iesc_vAA[0];
3776 cur_cell = 0;
3777 for(v = 0; v < cm->M; v++) {
3778 switch(cm->sttype[v]) {
3779 case IL_st:
3780 case ML_st:
3781 case IR_st:
3782 case MR_st:
3783 iesc_vAA[v] = ptr_to_start + cur_cell;
3784 cur_cell += cm->abc->Kp;
3785 for(a = 0; a < cm->abc->K; a++) /* all canonical residues */
3786 iesc_vAA[v][a] = cm->iesc[v][a];
3787 iesc_vAA[v][cm->abc->K] = -INFTY; /* gap symbol is impossible */
3788 for(a = cm->abc->K+1; a < cm->abc->Kp-1; a++) /* all ambiguous residues */
3789 iesc_vAA[v][a] = esl_abc_IAvgScore(cm->abc, a, cm->iesc[v]);
3790 iesc_vAA[v][cm->abc->Kp-1] = -INFTY; /* missing data is IMPOSSIBLE */
3791 break;
3792 case MP_st:
3793 iesc_vAA[v] = ptr_to_start + cur_cell;
3794 esl_vec_ISet(iesc_vAA[v], cm->abc->Kp * cm->abc->Kp, -INFTY); /* init all cells to -INFTY */
3795 cur_cell += cm->abc->Kp * cm->abc->Kp;
3796 /* a is canonical, b is canonical */
3797 for(a = 0; a < cm->abc->K; a++) {
3798 for(b = 0; b < cm->abc->K; b++) {
3799 iesc_vAA[v][(a * cm->abc->Kp) + b] = cm->iesc[v][(a * cm->abc->K) + b];
3800 }
3801 }
3802 /* a is not canonical, b is canonical */
3803 for(a = cm->abc->K+1; a < cm->abc->Kp-1; a++) {
3804 for(b = 0; b < cm->abc->K; b++) {
3805 iesc_vAA[v][(a * cm->abc->Kp) + b] = iFastPairScoreLeftOnlyDegenerate(cm->abc->K, cm->iesc[v], leftAA[a], b);
3806 }
3807 }
3808 /* a is canonical, b is not canonical */
3809 for(a = 0; a < cm->abc->K; a++) {
3810 for(b = cm->abc->K+1; b < cm->abc->Kp-1; b++) {
3811 iesc_vAA[v][(a * cm->abc->Kp) + b] = iFastPairScoreRightOnlyDegenerate(cm->abc->K, cm->iesc[v], rightAA[b], a);
3812 }
3813 }
3814 /* a is not canonical, b is not canonical */
3815 for(a = cm->abc->K+1; a < cm->abc->Kp-1; a++) {
3816 for(b = cm->abc->K+1; b < cm->abc->Kp-1; b++) {
3817 iesc_vAA[v][(a * cm->abc->Kp) + b] = iFastPairScoreBothDegenerate(cm->abc->K, cm->iesc[v], leftAA[a], rightAA[b]);
3818 }
3819 }
3820 /* everything else, when either a or b is gap or missing data, stays -INFTY */
3821 break;
3822 default:
3823 iesc_vAA[v] = NULL;
3824 break;
3825 }
3826 }
3827 for(a = 0; a < cm->abc->Kp; a++) {
3828 if(leftAA[a] != NULL) free(leftAA[a]);
3829 if(rightAA[a] != NULL) free(rightAA[a]);
3830 }
3831 free(leftAA);
3832 free(rightAA);
3833 return iesc_vAA;
3834
3835 ERROR:
3836 cm_Fail("memory allocation error.");
3837 return NULL; /* NEVERREACHED */
3838 }
3839
3840 /* Function: ICopyOptimizedEmitScoresFromFloats()
3841 * Date: EPN, Wed Aug 20 14:47:13 2008
3842 *
3843 * Purpose: Allocate and fill an optimized emission score
3844 * vector of integer scores. For degenerate
3845 * residues calculating the scores is somewhat
3846 * compute-intensive, so don't calc them, but copy/integerize
3847 * scores from the CM's pre-calculated float
3848 * optimized emission score vector.
3849 * This is done only because is fast.
3850 *
3851 * Returns: the 2D integer emission score vector on success,
3852 * dies immediately on memory allocation error.
3853 */
3854 int **
3855 ICopyOptimizedEmitScoresFromFloats(CM_t *cm, float **oesc)
3856 {
3857 int status;
3858 int **iesc_vAA;
3859 ESL_DSQ a,b;
3860 int v;
3861 int cur_cell;
3862 int npairs = 0;
3863 int nsinglets = 0;
3864 int *ptr_to_start; /* points to block allocated to iesc_vAA[0], nec b/c esc_vAA[0] gets set to NULL, because v == 0 is non-emitter */
3865
3866 /* count pairs, singlets */
3867 for(v = 0; v < cm->M; v++) {
3868 switch(cm->sttype[v]) {
3869 case IL_st:
3870 case ML_st:
3871 case IR_st:
3872 case MR_st:
3873 nsinglets++;
3874 break;
3875 case MP_st:
3876 npairs++;
3877 break;
3878 }
3879 }
3880
3881 /* fill emission scores for each state */
3882 ESL_ALLOC(iesc_vAA, sizeof(int *) * (cm->M));
3883 ESL_ALLOC(iesc_vAA[0], sizeof(int) * ((cm->abc->Kp * nsinglets) + (cm->abc->Kp * cm->abc->Kp * npairs)));
3884 ptr_to_start = iesc_vAA[0];
3885 cur_cell = 0;
3886 for(v = 0; v < cm->M; v++) {
3887 switch(cm->sttype[v]) {
3888 case IL_st:
3889 case ML_st:
3890 case IR_st:
3891 case MR_st:
3892 iesc_vAA[v] = ptr_to_start + cur_cell;
3893 cur_cell += cm->abc->Kp;
3894 for(a = 0; a < cm->abc->K; a++) /* all canonical residues */
3895 iesc_vAA[v][a] = cm->iesc[v][a];
3896 iesc_vAA[v][cm->abc->K] = -INFTY; /* gap symbol is impossible */
3897 for(a = cm->abc->K+1; a < cm->abc->Kp-1; a++) /* all ambiguous residues */
3898 iesc_vAA[v][a] = (int) floor(0.5 + INTSCALE * oesc[v][a]); /* COPY, don't calc */
3899 iesc_vAA[v][cm->abc->Kp-1] = -INFTY; /* missing data is IMPOSSIBLE */
3900 break;
3901 case MP_st:
3902 iesc_vAA[v] = ptr_to_start + cur_cell;
3903 esl_vec_ISet(iesc_vAA[v], cm->abc->Kp * cm->abc->Kp, -INFTY); /* init all cells to -INFTY */
3904 cur_cell += cm->abc->Kp * cm->abc->Kp;
3905 /* a is canonical, b is canonical */
3906 for(a = 0; a < cm->abc->K; a++) {
3907 for(b = 0; b < cm->abc->K; b++) {
3908 iesc_vAA[v][(a * cm->abc->Kp) + b] = cm->iesc[v][(a * cm->abc->K) + b];
3909 }
3910 }
3911 /* a is not canonical, b is canonical */
3912 for(a = cm->abc->K+1; a < cm->abc->Kp-1; a++) {
3913 for(b = 0; b < cm->abc->K; b++) {
3914 iesc_vAA[v][(a * cm->abc->Kp) + b] = (int) floor(0.5 + INTSCALE * oesc[v][(a * cm->abc->Kp)+b]); /* COPY, don't calc */
3915 }
3916 }
3917 /* a is canonical, b is not canonical */
3918 for(a = 0; a < cm->abc->K; a++) {
3919 for(b = cm->abc->K+1; b < cm->abc->Kp-1; b++) {
3920 iesc_vAA[v][(a * cm->abc->Kp) + b] = (int) floor(0.5 + INTSCALE * oesc[v][(a * cm->abc->Kp)+b]); /* COPY, don't calc */
3921 }
3922 }
3923 /* a is not canonical, b is not canonical */
3924 for(a = cm->abc->K+1; a < cm->abc->Kp-1; a++) {
3925 for(b = cm->abc->K+1; b < cm->abc->Kp-1; b++) {
3926 iesc_vAA[v][(a * cm->abc->Kp) + b] = (int) floor(0.5 + INTSCALE * oesc[v][(a * cm->abc->Kp)+b]); /* COPY, don't calc */
3927 }
3928 }
3929 /* everything else, when either a or b is gap or missing data, stays -INFTY */
3930 break;
3931 default:
3932 iesc_vAA[v] = NULL;
3933 break;
3934 }
3935 }
3936 return iesc_vAA;
3937
3938 ERROR:
3939 cm_Fail("memory allocation error.");
3940 return NULL; /* NEVERREACHED */
3941 }
3942
3943 /* Function: CloneOptimizedEmitScores()
3944 * Date: EPN, Thu Dec 15 09:27:06 2011
3945 *
3946 * Purpose: Allocate <oesc> and <ioesc> in <dest> and
3947 * copy <src->oesc> and <src->ioesc> into it.
3948 *
3949 * Returns: eslOK on success.
3950 * eslEMEM if out of memory, errbuf filled.
3951 * eslEINCOMPAT if <dest> is not the same size as <src>, or <dest->oesc> != NULL or <dest->ioesc>, errbuf filled
3952 */
3953 int
3954 CloneOptimizedEmitScores(const CM_t *src, CM_t *dest, char *errbuf)
3955 {
3956 int status;
3957 int v;
3958 int nsinglets = 0;
3959 int npairs = 0;
3960 float *fptr_to_start; /* points to block allocated to cm->oesc[0], nec b/c it gets set to NULL, because v == 0 is non-emitter */
3961 int *iptr_to_start; /* points to block allocated to cm->ioesc[0], nec b/c it gets set to NULL, because v == 0 is non-emitter */
3962 int cur_cell;
3963
3964 if(src->M != dest->M) ESL_FAIL(eslEINCOMPAT, errbuf, "cloning optimized emit scores, src->M != dest->M");
3965 if(src->nodes != dest->nodes) ESL_FAIL(eslEINCOMPAT, errbuf, "cloning optimized emit scores, src->nodes != dest->nodes");
3966 if(src->clen != dest->clen) ESL_FAIL(eslEINCOMPAT, errbuf, "cloning optimized emit scores, src->clen != dest->clen");
3967 if(src->abc->type != dest->abc->type) ESL_FAIL(eslEINCOMPAT, errbuf, "cloning optimized emit scores, src->abc->type != dest->abc->type");
3968 if(dest->oesc != NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cloning optimized emit scores, dest->oesc != NULL");
3969 if(dest->ioesc != NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cloning optimized emit scores, dest->ioesc != NULL");
3970 if(src->oesc == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cloning optimized emit scores, src->oesc == NULL");
3971 if(src->ioesc == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "cloning optimized emit scores, src->ioesc == NULL");
3972
3973 /* count pairs, singlets */
3974 for(v = 0; v < src->M; v++) {
3975 if(src->sttype[v] == IL_st || src->sttype[v] == ML_st || src->sttype[v] == IR_st || src->sttype[v] == MR_st) nsinglets++;
3976 if(src->sttype[v] == MP_st) npairs++;
3977 }
3978 /* allocate and fill (should be consistent with CalcFOptimizedEmitScores()) */
3979 ESL_ALLOC(dest->oesc, sizeof(float *) * (src->M));
3980 ESL_ALLOC(dest->oesc[0], sizeof(float) * ((src->abc->Kp * nsinglets) + (src->abc->Kp * src->abc->Kp * npairs)));
3981 ESL_ALLOC(dest->ioesc, sizeof(int *) * (src->M));
3982 ESL_ALLOC(dest->ioesc[0], sizeof(int) * ((src->abc->Kp * nsinglets) + (src->abc->Kp * src->abc->Kp * npairs)));
3983 fptr_to_start = dest->oesc[0];
3984 iptr_to_start = dest->ioesc[0];
3985 cur_cell = 0;
3986 for(v = 0; v < src->M; v++) {
3987 if(src->sttype[v] == IL_st || src->sttype[v] == ML_st || src->sttype[v] == IR_st || src->sttype[v] == MR_st) {
3988 dest->oesc[v] = fptr_to_start + cur_cell;
3989 dest->ioesc[v] = iptr_to_start + cur_cell;
3990 esl_vec_FCopy(src->oesc[v], src->abc->Kp, dest->oesc[v]);
3991 esl_vec_ICopy(src->ioesc[v], src->abc->Kp, dest->ioesc[v]);
3992 cur_cell += src->abc->Kp;
3993 }
3994 else if(src->sttype[v] == MP_st) {
3995 dest->oesc[v] = fptr_to_start + cur_cell;
3996 dest->ioesc[v] = iptr_to_start + cur_cell;
3997 esl_vec_FCopy(src->oesc[v], src->abc->Kp * src->abc->Kp, dest->oesc[v]);
3998 esl_vec_ICopy(src->ioesc[v], src->abc->Kp * src->abc->Kp, dest->ioesc[v]);
3999 cur_cell += src->abc->Kp * src->abc->Kp;
4000 }
4001 else {
4002 dest->oesc[v] = NULL;
4003 dest->ioesc[v] = NULL;
4004 }
4005 }
4006 return eslOK;
4007
4008 ERROR:
4009 ESL_FAIL(status, errbuf, "out of memory");
4010 return status; /* NEVERREACHED */
4011 }
4012
4013 /* Function: DumpOptimizedEmitScores()
4014 */
4015 void
4016 DumpOptimizedEmitScores(CM_t *cm, FILE *fp)
4017 {
4018 int v;
4019
4020 for(v = 0; v < cm->M; v++) {
4021 switch(cm->sttype[v]) {
4022 case IL_st:
4023 case ML_st:
4024 case IR_st:
4025 case MR_st:
4026 if(cm->oesc != NULL) {
4027 fprintf(fp, "SF v: %5d\t", v);
4028 esl_vec_FDump(fp, cm->oesc[v], cm->abc->Kp, NULL);
4029 }
4030 if(cm->ioesc != NULL) {
4031 fprintf(fp, "SI v: %5d\t", v);
4032 esl_vec_IDump(fp, cm->ioesc[v], cm->abc->Kp, NULL);
4033 }
4034 break;
4035 case MP_st:
4036 if(cm->oesc != NULL) {
4037 fprintf(fp, "PF v: %5d\t", v);
4038 esl_vec_FDump(fp, cm->oesc[v], cm->abc->Kp * cm->abc->Kp, NULL);
4039 }
4040 if(cm->ioesc != NULL) {
4041 esl_vec_IDump(fp, cm->ioesc[v], cm->abc->Kp * cm->abc->Kp, NULL);
4042 fprintf(fp, "PI v: %5d\t", v);
4043 }
4044 break;
4045 }
4046 }
4047 }
4048
4049
4050 /* Function: FreeOptimizedEmitScores()
4051 * Date: EPN, Fri Nov 9 08:44:06 2007
4052 *
4053 * Purpose: Free 2D vectors of optimized emissions scores.
4054 * Either fesc_vAA or iesc_vAA (or both) must be non-NULL.
4055 *
4056 * Returns: void
4057 */
4058 void
4059 FreeOptimizedEmitScores(float **fesc_vAA, int **iesc_vAA, int M)
4060 {
4061 if(fesc_vAA == NULL && iesc_vAA == NULL) cm_Fail("FreeOptimizedEmitScores() but fesc and iesc are NULL.\n");
4062
4063 if(fesc_vAA != NULL) {
4064 if(fesc_vAA[1] != NULL) {
4065 free(fesc_vAA[1]); /* note: we free [1], but we alloc'ed to [0], why? b/c fesc_vAA[0] is set to NULL after it's
4066 * used for allocation b/c it's the ROOT_S state, a non-emitter, then fesc_vAA[1] is set
4067 * to point where it used to point (it's the ROOT_IL state, an emitter).
4068 */
4069 }
4070 free(fesc_vAA);
4071 fesc_vAA = NULL;
4072 }
4073
4074 if(iesc_vAA != NULL) {
4075 if(iesc_vAA[1] != NULL) {
4076 free(iesc_vAA[1]); /* note: we free [1], but we alloc'ed to [0], why? b/c iesc_vAA[0] is set to NULL after it's
4077 * used for allocation b/c it's the ROOT_S state, a non-emitter, then iesc_vAA[1] is set
4078 * to point where it used to point (it's the ROOT_IL state, an emitter).
4079 */
4080 }
4081 free(iesc_vAA);
4082 iesc_vAA = NULL;
4083 }
4084 return;
4085 }
4086
4087 /* Function: FCalcInitDPScores()
4088 * Date: EPN, Fri Nov 9 09:18:07 2007
4089 *
4090 * Purpose: Allocate, fill and return the initial float scores
4091 * for a scanning DP matrix for CM <cm> as it's
4092 * currently configured. All [0..v..M-1][0..d..W]
4093 * cells are allocated and filled, it's up to
4094 * the DP function to ignore cells outside bands.
4095 *
4096 * Returns: the 2D float init sc vector on success,
4097 * dies immediately on memory error.
4098 */
4099 float **
4100 FCalcInitDPScores(CM_t *cm)
4101 {
4102 int status;
4103 float *el_scA;
4104 float **init_scAA;
4105 int v, d;
4106
4107 /* precalcuate all possible local end scores, for local end emits of 1..W residues */
4108 ESL_ALLOC(el_scA, sizeof(float) * (cm->W+1));
4109 for(d = 0; d <= cm->W; d++) el_scA[d] = cm->el_selfsc * d;
4110 /* precalculate the initial score for all alpha[v][j][d] cells, it's independent of j
4111 * these scores ignore bands, (that is cells outside bands still have initsc's calc'ed)
4112 * it's up to the DP function to skip these cells. */
4113 ESL_ALLOC(init_scAA, sizeof(float *) * (cm->M));
4114 ESL_ALLOC(init_scAA[0], sizeof(float) * (cm->M) * (cm->W+1));
4115 for (v = 0; v < cm->M; v++) {
4116 init_scAA[v] = init_scAA[0] + (v * (cm->W+1));
4117 if(NOT_IMPOSSIBLE(cm->endsc[v])) {
4118 for(d = 0; d <= cm->W; d++) init_scAA[v][d] = el_scA[d] + cm->endsc[v];
4119 }
4120 else {
4121 for(d = 0; d <= cm->W; d++) init_scAA[v][d] = IMPOSSIBLE;
4122 }
4123 }
4124 free(el_scA);
4125 return init_scAA;
4126
4127 ERROR:
4128 cm_Fail("memory allocation error.");
4129 return NULL; /* NEVERREACHED */
4130 }
4131
4132
4133 /* Function: ICalcInitDPScores()
4134 * Date: EPN, Fri Nov 9 09:10:33 2007
4135 *
4136 * Purpose: Allocate, fill and return the initial int scores
4137 * for a scanning DP matrix for CM <cm> as it's
4138 * currently configured. All [0..v..M-1][0..d..W]
4139 * cells are allocated and filled, it's up to
4140 * the DP function to ignore cells outside bands.
4141 *
4142 * Returns: the 2D integer init sc vector on success,
4143 * dies immediately on memory error.
4144 */
4145 int **
4146 ICalcInitDPScores(CM_t *cm)
4147 {
4148 int status;
4149 int *el_scA;
4150 int **init_scAA;
4151 int v, d;
4152
4153 /* precalcuate all possible local end scores, for local end emits of 1..W residues */
4154 ESL_ALLOC(el_scA, sizeof(int) * (cm->W+1));
4155 for(d = 0; d <= cm->W; d++) el_scA[d] = cm->iel_selfsc * d;
4156 /* precalculate the initial score for all ialpha[v][j][d] cells, it's independent of j
4157 * these scores ignore bands, (that is cells outside bands still have initsc's calc'ed)
4158 * it's up to the DP function to skip these cells. */
4159 ESL_ALLOC(init_scAA, sizeof(int *) * (cm->M));
4160 ESL_ALLOC(init_scAA[0], sizeof(int) * (cm->M) * (cm->W+1));
4161 for (v = 0; v < cm->M; v++) {
4162 init_scAA[v] = init_scAA[0] + (v * (cm->W+1));
4163 if(cm->iendsc[v] != -INFTY) {
4164 for(d = 0; d <= cm->W; d++) init_scAA[v][d] = el_scA[d] + cm->iendsc[v];
4165 }
4166 else {
4167 for(d = 0; d <= cm->W; d++) init_scAA[v][d] = -INFTY;
4168 }
4169 }
4170
4171
4172 free(el_scA);
4173 return init_scAA;
4174
4175 ERROR:
4176 cm_Fail("memory allocation error.");
4177 return NULL; /* NEVERREACHED */
4178 }
4179
4180
4181 /************************************************************************
4182 * Functions stolen from HMMER 2.4 for use with CM plan 9 HMMs.
4183 * Eventually, these should go away, replaced with Easel funcs.
4184 * These first 3 were stolen from HMMER:mathsupport.c
4185 *
4186 * Score2Prob()
4187 * Prob2Score()
4188 * Scorify()
4189 *
4190 * NOTE: ILogSum() (and auxiliary funcs associated with it) used to be here
4191 * but moved to logsum.c (EPN, Sat Sep 8 15:49:47 2007)
4192 <*/
4193
4194 /* Function: Prob2Score()
4195 *
4196 * Purpose: Convert a probability to a scaled integer log_2 odds score.
4197 * Round to nearest integer (i.e. note use of +0.5 and floor())
4198 * Return the score.
4199 */
4200 int
4201 Prob2Score(float p, float null)
4202 {
4203 if(p == 0.0) return -INFTY;
4204 else return (int) floor(0.5 + INTSCALE * sreLOG2(p/null));
4205 }
4206
4207 /* Function: Score2Prob()
4208 *
4209 * Purpose: Convert an integer log_2 odds score back to a probability;
4210 * needs the null model probability, if any, to do the conversion.
4211 */
4212 float
4213 Score2Prob(int sc, float null)
4214 {
4215 if (sc == -INFTY) return 0.;
4216 else return (null * sreEXP2((float) sc / INTSCALE));
4217 }
4218
4219 /* Function: Scorify()
4220 *
4221 * Purpose: Convert a scaled integer log-odds score to a floating
4222 * point score for output. (could be a macro but who cares.)
4223 */
4224 float
4225 Scorify(int sc)
4226 {
4227 if (sc == -INFTY) return IMPOSSIBLE;
4228 else return ((float) sc / INTSCALE);
4229 }
4230
4231 /* Function: cm_ExpectedStateOccupancy()
4232 * Date: EPN 12.02.05
4233 * EPN, Fri Feb 3 09:07:43 2012 [Updated]
4234 *
4235 * Purpose: Fill psi vector. psi[v] is the expected number of times
4236 * state v is entered in a globally configured version of
4237 * the CM. If the model is locally configured upon entry, we
4238 * deal with that by making a copy of the transitions and
4239 * redefine transitions out of state 0.
4240 *
4241 * Note: this function was renamed and updated from
4242 * 'fill_psi()' between versions 1.0.2 and 1.1.
4243 *
4244 * Args: cm - the model
4245
4246 * Returns: void, dies on an error, which should never happen if the CM is normalized.
4247 *
4248 */
4249 double *
4250 cm_ExpectedStateOccupancy(CM_t *cm)
4251 {
4252 int status;
4253 int v; /* first state in cm node nd */
4254 int y;
4255 int x;
4256 char tmap_val;
4257 int nd;
4258 double summed_psi;
4259 int nstates;
4260 int is_insert;
4261 int final_y;
4262 char ***tmap = NULL; /* transition map */
4263 float **t_copy = NULL; /* copy of transition probabilities */
4264 double *psi = NULL;
4265 double tol = 0.001;
4266 /* tol is difference from 1.0 we allow for summed psi of split set states
4267 * it is larger for really big models,
4268 * tolerance is (clen/25000) * 0.001, with minimum of 0.001
4269 * so it's 0.001 unless clen > 25000
4270 * (from v1.1 to v1.1.4 it was 0.001 for all models)
4271 */
4272 if(cm->clen > 25000) {
4273 tol = ((float) cm->clen / 25000.) * 0.001;
4274 }
4275
4276 /* make a copy of the CM transitions */
4277 ESL_ALLOC(t_copy, cm->M * sizeof(float *));
4278 ESL_ALLOC(t_copy[0], cm->M * MAXCONNECT * sizeof(float));
4279 for (v = 0; v < cm->M; v++) {
4280 t_copy[v] = t_copy[0] + v * MAXCONNECT;
4281 }
4282 esl_vec_FCopy(cm->t[0], cm->M * MAXCONNECT, t_copy[0]);
4283 /* deal with possibility that we have local begins on: redefine transitions out state 0 to cm->root_trans in this case */
4284 if(cm->root_trans != NULL) {
4285 esl_vec_FCopy(cm->root_trans, cm->cnum[0], t_copy[0]);
4286 }
4287 /* deal with possibility that we have local ends on: renormalize transitions out of each state (this will discount
4288 * the possibility that we transition to a local end)
4289 */
4290 if(cm->flags & CMH_LOCAL_END) {
4291 for (nd = 1; nd < cm->nodes; nd++) {
4292 if ((cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATL_nd ||
4293 cm->ndtype[nd] == MATR_nd || cm->ndtype[nd] == BEGL_nd ||
4294 cm->ndtype[nd] == BEGR_nd) &&
4295 cm->ndtype[nd+1] != END_nd)
4296 {
4297 v = cm->nodemap[nd];
4298 esl_vec_FNorm(t_copy[v], cm->cnum[v]);
4299 }
4300 }
4301 }
4302
4303 /* allocate and initialize psi */
4304 ESL_ALLOC(psi, sizeof(double) * cm->M);
4305 esl_vec_DSet(psi, cm->M, 0.);
4306
4307 /* create the transition map */
4308 tmap = cm_CreateTransitionMap();
4309
4310 /*psi[v] is the 'expected number of times state v is entered'.*/
4311 for (v = 0; v <= cm->M-1; v++) {
4312 is_insert = (cm->sttype[v] == IL_st || cm->sttype[v] == IR_st) ? TRUE : FALSE;
4313
4314 if(cm->sttype[v] == S_st) {
4315 psi[v] = 1.0; /* no transitions into start states - they're necessarily visited in every parse. */
4316 }
4317 else { /* not a S_st */
4318 final_y = (is_insert) ? 1 : 0; /* insert self loops are handled differently */
4319 for (y = cm->pnum[v]-1; y >= final_y; y--) {
4320 x = cm->plast[v] - y; /* x is a parent of v, add contribution from x to v */
4321 tmap_val = tmap[(int) cm->stid[x]][(int) cm->ndtype[cm->ndidx[v]+is_insert]][(int) cm->stid[v]];
4322 psi[v] += psi[x] * t_copy[x][(int) tmap_val];
4323 }
4324 if(is_insert) {
4325 psi[v] += psi[v] * (t_copy[v][0] / (1.-t_copy[v][0])); /* the contribution of the self insertion loops */
4326 }
4327 }
4328 }
4329 /* Sanity check. For any node the sum of psi values over
4330 * all split set states should be 1.0 (or close to it, see definition of tol above)
4331 */
4332 for(nd = 0; nd < cm->nodes; nd++) {
4333 summed_psi = 0.;
4334 nstates = TotalStatesInNode(cm->ndtype[nd]);
4335 for(v = cm->nodemap[nd]; v < cm->nodemap[nd] + nstates; v++) {
4336 if(cm->sttype[v] != IL_st && cm->sttype[v] != IR_st) summed_psi += psi[v];
4337 }
4338 if((summed_psi < (1. - tol)) || (summed_psi > (1.0 + tol))) {
4339 cm_Fail("summed psi of split states in node %d not 1.0 but : %f\n", nd, summed_psi);
4340 }
4341 /* printf("split summed psi[%d]: %f\n", nd, summed_psi); */
4342 }
4343 /* Another sanity check, the only states that can have psi equal to 0
4344 * are detached insert states (states immediately prior to END_Es) */
4345 for(v = 0; v < cm->M; v++) {
4346 if(psi[v] == 0. && (! StateIsDetached(cm, v))) {
4347 cm_Fail("psi of state v:%d is 0.0 and this state is not a detached insert! HMM banding would have failed...\n", v);
4348 }
4349 }
4350
4351 cm_FreeTransitionMap(tmap);
4352 if(t_copy != NULL) {
4353 if(t_copy[0] != NULL) free(t_copy[0]);
4354 free(t_copy);
4355 }
4356 return psi;
4357
4358 ERROR:
4359 cm_FreeTransitionMap(tmap);
4360 if(t_copy != NULL) {
4361 if(t_copy[0] != NULL) free(t_copy[0]);
4362 free(t_copy);
4363 }
4364 if(psi != NULL) free(psi);
4365 return NULL;
4366 }
4367
4368
4369 /* Function: cm_ExpectedPositionOccupancy()
4370 * Date: EPN, Fri Feb 3 10:20:19 2012
4371 *
4372 * Purpose: Determine the expected occupancy of each consensus
4373 * position 1..clen and return it in <ret_mexpocc> and
4374 * determine the expected occupancy of each insert position
4375 * (after each position 1..clen) and return it in
4376 * <ret_iexpocc>. Most of the work is done by a call to
4377 * cm_ExpectedStateOccupancy().
4378 *
4379 * Caller can also pass in non-NULL values for <opt_psi>
4380 * <opt_m2v_1>, <opt_m2v_2> and <opt_i2v>. <opt_psi> holds
4381 * expected occupancy of each state. The other arrays hold
4382 * the state index that emits at each match position
4383 * <opt_m2v_1> and <opt_m2v_2> or inserts after each match
4384 * position <opt_i2v>. For <opt_m2v_1> only consensus match
4385 * states are used (MATP_MP, MATL_ML and MATR_MR). For
4386 * <opt_m2v_2> only non-consensus match states are used:
4387 * MATP_ML and MATP_MR, both m2v_1 and m2v_2 are necessary
4388 * because 2 states can emit at each position modeled by a
4389 * MATP.
4390 *
4391 * Args: cm - the model
4392 * ret_mexpocc - RETURN: [0..clen] expected occupancy of match positions
4393 * ret_iexpocc - RETURN: [0..clen] expected occupancy of inserts (after cpos)
4394 * opt_psi - OPTIONAL RETURN: [0..v..M-1] expected occupancy of state v
4395 * opt_m2v_1 - OPTIONAL RETURN: [0..clen] consensus match state that emits at position cpos
4396 * (must be MATP_MP, MATL_ML or MATR_MR)
4397 * opt_m2v_2 - OPTIONAL RETURN: [0..clen] non-consensus match state that emits at position cpos or -1
4398 * (if ! -1, must be MATP_ML, MATP_MR)
4399 * opt_i2v - OPTIONAL RETURN: [0..clen] insert state that emits after each position
4400 *
4401 * Returns: void, dies on an error, which should never happen if the CM is normalized.
4402 *
4403 */
4404 int
4405 cm_ExpectedPositionOccupancy(CM_t *cm, float **ret_mexpocc, float **ret_iexpocc, double **opt_psi, int **opt_m2v_1, int **opt_m2v_2, int **opt_i2v)
4406 {
4407 int status;
4408 double *psi = NULL;
4409 float *mexpocc = NULL;
4410 float *iexpocc = NULL;
4411 int *m2v_1 = NULL;
4412 int *m2v_2 = NULL;
4413 int *i2v = NULL;
4414 int lpos, rpos;
4415 int v, nd;
4416
4417 if((psi = cm_ExpectedStateOccupancy(cm)) == NULL) goto ERROR;
4418
4419 ESL_ALLOC(mexpocc, sizeof(float) * (cm->clen+1));
4420 ESL_ALLOC(iexpocc, sizeof(float) * (cm->clen+1));
4421 ESL_ALLOC(m2v_1, sizeof(int) * (cm->clen+1));
4422 ESL_ALLOC(m2v_2, sizeof(int) * (cm->clen+1));
4423 ESL_ALLOC(i2v, sizeof(int) * (cm->clen+1));
4424 esl_vec_FSet(mexpocc, (cm->clen+1), 0.);
4425 esl_vec_FSet(iexpocc, (cm->clen+1), 0.);
4426 esl_vec_ISet(m2v_1, (cm->clen+1), -1);
4427 esl_vec_ISet(m2v_2, (cm->clen+1), -1);
4428 esl_vec_ISet(i2v, (cm->clen+1), -1);
4429
4430 mexpocc[0] = 0.; /* no consensus position 0, this is redundant, but left here to emphasize that it will stay 0. */
4431 /* DumpEmitMap(stdout, cm->emap, cm); */
4432 for(v = 0; v < cm->M; v++) {
4433 if(! StateIsDetached(cm, v)) {
4434 nd = cm->ndidx[v];
4435 lpos = cm->emap->lpos[nd];
4436 rpos = cm->emap->rpos[nd];
4437 /* printf("v: %4d nd: %4d %4s %2s lpos: %4d rpos: %4d psi: %.6f\n", v, nd, Nodetype(cm->ndtype[nd]), Statetype(cm->sttype[v]), lpos, rpos, psi[v]); */
4438 if(cm->sttype[v] == MP_st || cm->sttype[v] == ML_st) mexpocc[lpos] += psi[v]; /* we do += so MATP_MP and MATP_ML both contribute */
4439 if(cm->sttype[v] == MP_st || cm->sttype[v] == MR_st) mexpocc[rpos] += psi[v]; /* we do += so MATP_MP and MATP_MR both contribute */
4440 if(cm->sttype[v] == IL_st) { iexpocc[lpos] = psi[v]; i2v[lpos] = v; }
4441 if(cm->sttype[v] == IR_st) { iexpocc[rpos-1] = psi[v]; i2v[rpos-1] = v; }
4442
4443 if(cm->stid[v] == MATP_MP) { m2v_1[lpos] = v; m2v_1[rpos] = v; }
4444 if(cm->stid[v] == MATL_ML) { m2v_1[lpos] = v; }
4445 if(cm->stid[v] == MATR_MR) { m2v_1[rpos] = v; }
4446 if(cm->stid[v] == MATP_ML) { m2v_2[lpos] = v; }
4447 if(cm->stid[v] == MATP_MR) { m2v_2[rpos] = v; }
4448 }
4449 }
4450
4451 /* int cpos; for(cpos = 0; cpos <= cm->clen; cpos++) printf("cpos: %3d mexpocc: %.6f iexpocc: %.6f m2v_1: %6d m2v_2: %6d i2v: %6d\n", cpos, mexpocc[cpos], iexpocc[cpos], m2v_1[cpos], m2v_2[cpos], i2v[cpos]); */
4452
4453 *ret_mexpocc = mexpocc;
4454 *ret_iexpocc = iexpocc;
4455 if(opt_psi != NULL) *opt_psi = psi; else free(psi);
4456 if(opt_m2v_1 != NULL) *opt_m2v_1 = m2v_1; else free(m2v_1);
4457 if(opt_m2v_2 != NULL) *opt_m2v_2 = m2v_2; else free(m2v_2);
4458 if(opt_i2v != NULL) *opt_i2v = i2v; else free(i2v);
4459 return eslOK;
4460
4461 ERROR:
4462 if(psi != NULL) free(psi);
4463 if(mexpocc != NULL) free(mexpocc);
4464 if(iexpocc != NULL) free(iexpocc);
4465 *ret_mexpocc = NULL;
4466 *ret_iexpocc = NULL;
4467 if(opt_psi != NULL) *opt_psi = NULL;
4468 if(opt_m2v_1 != NULL) *opt_m2v_1 = NULL;
4469 if(opt_m2v_2 != NULL) *opt_m2v_2 = NULL;
4470 if(opt_i2v != NULL) *opt_i2v = NULL;
4471 return eslEMEM;
4472 }
4473
4474 /* Function: cm_CreateTransitionMap()
4475 * Date: EPN 12.02.05
4476 * EPN, Fri Feb 3 09:07:43 2012 [Updated]
4477 *
4478 * Purpose: Make the predefined transition map which tells you
4479 * the index of a given transition from any of the 74
4480 * transition sets.
4481 *
4482 * Note: this function was renamed and updated from
4483 * 'make_tmap()' between versions 1.0.2 and 1.1.
4484 *
4485 * Args: void
4486 *
4487 * Returns: transition map, a 3 dimensional character array:
4488 * 1st D: statetype of v
4489 * 2nd D: type of downstream node.
4490 * 3rd D: statetype of y, that we're transitioning to.
4491 * value: the index of v->y in cm->t[v]
4492 * Caller must free with cm_FreeTransitionMap().
4493 */
4494 char ***
4495 cm_CreateTransitionMap()
4496 {
4497 int status;
4498 int i,j,k;
4499 char ***tmap;
4500
4501 ESL_ALLOC(tmap, sizeof(char **) * UNIQUESTATES);
4502 for(i = 0; i < UNIQUESTATES; i++) {
4503 ESL_ALLOC(tmap[i], sizeof(char *) * NODETYPES);
4504 for(j = 0; j < NODETYPES; j++) {
4505 ESL_ALLOC(tmap[i][j], sizeof(char) * UNIQUESTATES);
4506 for(k = 0; k < UNIQUESTATES; k++) {
4507 tmap[i][j][k] = -1;
4508 }
4509 }
4510 }
4511
4512 /*following code block generated by:
4513 *perl ~nawrocki/notebook/5_1128_hmnl_ml_hmm/scripts/gen_tmap.pl
4514 */
4515 tmap[ROOT_S][BIF_nd][ROOT_IL] = 0;
4516 tmap[ROOT_S][BIF_nd][ROOT_IR] = 1;
4517 tmap[ROOT_S][BIF_nd][BIF_B] = 2;
4518
4519 tmap[ROOT_S][MATP_nd][ROOT_IL] = 0;
4520 tmap[ROOT_S][MATP_nd][ROOT_IR] = 1;
4521 tmap[ROOT_S][MATP_nd][MATP_MP] = 2;
4522 tmap[ROOT_S][MATP_nd][MATP_ML] = 3;
4523 tmap[ROOT_S][MATP_nd][MATP_MR] = 4;
4524 tmap[ROOT_S][MATP_nd][MATP_D] = 5;
4525
4526 tmap[ROOT_S][MATL_nd][ROOT_IL] = 0;
4527 tmap[ROOT_S][MATL_nd][ROOT_IR] = 1;
4528 tmap[ROOT_S][MATL_nd][MATL_ML] = 2;
4529 tmap[ROOT_S][MATL_nd][MATL_D] = 3;
4530
4531 tmap[ROOT_S][MATR_nd][ROOT_IL] = 0;
4532 tmap[ROOT_S][MATR_nd][ROOT_IR] = 1;
4533 tmap[ROOT_S][MATR_nd][MATR_MR] = 2;
4534 tmap[ROOT_S][MATR_nd][MATR_D] = 3;
4535
4536 tmap[ROOT_IL][BIF_nd][ROOT_IL] = 0;
4537 tmap[ROOT_IL][BIF_nd][ROOT_IR] = 1;
4538 tmap[ROOT_IL][BIF_nd][BIF_B] = 2;
4539
4540 tmap[ROOT_IL][MATP_nd][ROOT_IL] = 0;
4541 tmap[ROOT_IL][MATP_nd][ROOT_IR] = 1;
4542 tmap[ROOT_IL][MATP_nd][MATP_MP] = 2;
4543 tmap[ROOT_IL][MATP_nd][MATP_ML] = 3;
4544 tmap[ROOT_IL][MATP_nd][MATP_MR] = 4;
4545 tmap[ROOT_IL][MATP_nd][MATP_D] = 5;
4546
4547 tmap[ROOT_IL][MATL_nd][ROOT_IL] = 0;
4548 tmap[ROOT_IL][MATL_nd][ROOT_IR] = 1;
4549 tmap[ROOT_IL][MATL_nd][MATL_ML] = 2;
4550 tmap[ROOT_IL][MATL_nd][MATL_D] = 3;
4551
4552 tmap[ROOT_IL][MATR_nd][ROOT_IL] = 0;
4553 tmap[ROOT_IL][MATR_nd][ROOT_IR] = 1;
4554 tmap[ROOT_IL][MATR_nd][MATR_MR] = 2;
4555 tmap[ROOT_IL][MATR_nd][MATR_D] = 3;
4556
4557 tmap[ROOT_IR][BIF_nd][ROOT_IR] = 0;
4558 tmap[ROOT_IR][BIF_nd][BIF_B] = 1;
4559
4560 tmap[ROOT_IR][MATP_nd][ROOT_IR] = 0;
4561 tmap[ROOT_IR][MATP_nd][MATP_MP] = 1;
4562 tmap[ROOT_IR][MATP_nd][MATP_ML] = 2;
4563 tmap[ROOT_IR][MATP_nd][MATP_MR] = 3;
4564 tmap[ROOT_IR][MATP_nd][MATP_D] = 4;
4565
4566 tmap[ROOT_IR][MATL_nd][ROOT_IR] = 0;
4567 tmap[ROOT_IR][MATL_nd][MATL_ML] = 1;
4568 tmap[ROOT_IR][MATL_nd][MATL_D] = 2;
4569
4570 tmap[ROOT_IR][MATR_nd][ROOT_IR] = 0;
4571 tmap[ROOT_IR][MATR_nd][MATR_MR] = 1;
4572 tmap[ROOT_IR][MATR_nd][MATR_D] = 2;
4573
4574 tmap[BEGL_S][BIF_nd][BIF_B] = 0;
4575
4576 tmap[BEGL_S][MATP_nd][MATP_MP] = 0;
4577 tmap[BEGL_S][MATP_nd][MATP_ML] = 1;
4578 tmap[BEGL_S][MATP_nd][MATP_MR] = 2;
4579 tmap[BEGL_S][MATP_nd][MATP_D] = 3;
4580
4581 tmap[BEGR_S][BIF_nd][BEGR_IL] = 0;
4582 tmap[BEGR_S][BIF_nd][BIF_B] = 1;
4583
4584 tmap[BEGR_S][MATP_nd][BEGR_IL] = 0;
4585 tmap[BEGR_S][MATP_nd][MATP_MP] = 1;
4586 tmap[BEGR_S][MATP_nd][MATP_ML] = 2;
4587 tmap[BEGR_S][MATP_nd][MATP_MR] = 3;
4588 tmap[BEGR_S][MATP_nd][MATP_D] = 4;
4589
4590 tmap[BEGR_S][MATL_nd][BEGR_IL] = 0;
4591 tmap[BEGR_S][MATL_nd][MATL_ML] = 1;
4592 tmap[BEGR_S][MATL_nd][MATL_D] = 2;
4593
4594 tmap[BEGR_IL][BIF_nd][BEGR_IL] = 0;
4595 tmap[BEGR_IL][BIF_nd][BIF_B] = 1;
4596
4597 tmap[BEGR_IL][MATP_nd][BEGR_IL] = 0;
4598 tmap[BEGR_IL][MATP_nd][MATP_MP] = 1;
4599 tmap[BEGR_IL][MATP_nd][MATP_ML] = 2;
4600 tmap[BEGR_IL][MATP_nd][MATP_MR] = 3;
4601 tmap[BEGR_IL][MATP_nd][MATP_D] = 4;
4602
4603 tmap[BEGR_IL][MATL_nd][BEGR_IL] = 0;
4604 tmap[BEGR_IL][MATL_nd][MATL_ML] = 1;
4605 tmap[BEGR_IL][MATL_nd][MATL_D] = 2;
4606
4607 tmap[MATP_MP][BIF_nd][MATP_IL] = 0;
4608 tmap[MATP_MP][BIF_nd][MATP_IR] = 1;
4609 tmap[MATP_MP][BIF_nd][BIF_B] = 2;
4610
4611 tmap[MATP_MP][MATP_nd][MATP_IL] = 0;
4612 tmap[MATP_MP][MATP_nd][MATP_IR] = 1;
4613 tmap[MATP_MP][MATP_nd][MATP_MP] = 2;
4614 tmap[MATP_MP][MATP_nd][MATP_ML] = 3;
4615 tmap[MATP_MP][MATP_nd][MATP_MR] = 4;
4616 tmap[MATP_MP][MATP_nd][MATP_D] = 5;
4617
4618 tmap[MATP_MP][MATL_nd][MATP_IL] = 0;
4619 tmap[MATP_MP][MATL_nd][MATP_IR] = 1;
4620 tmap[MATP_MP][MATL_nd][MATL_ML] = 2;
4621 tmap[MATP_MP][MATL_nd][MATL_D] = 3;
4622
4623 tmap[MATP_MP][MATR_nd][MATP_IL] = 0;
4624 tmap[MATP_MP][MATR_nd][MATP_IR] = 1;
4625 tmap[MATP_MP][MATR_nd][MATR_MR] = 2;
4626 tmap[MATP_MP][MATR_nd][MATR_D] = 3;
4627
4628 tmap[MATP_MP][END_nd][MATP_IL] = 0;
4629 tmap[MATP_MP][END_nd][MATP_IR] = 1;
4630 tmap[MATP_MP][END_nd][END_E] = 2;
4631
4632 tmap[MATP_ML][BIF_nd][MATP_IL] = 0;
4633 tmap[MATP_ML][BIF_nd][MATP_IR] = 1;
4634 tmap[MATP_ML][BIF_nd][BIF_B] = 2;
4635
4636 tmap[MATP_ML][MATP_nd][MATP_IL] = 0;
4637 tmap[MATP_ML][MATP_nd][MATP_IR] = 1;
4638 tmap[MATP_ML][MATP_nd][MATP_MP] = 2;
4639 tmap[MATP_ML][MATP_nd][MATP_ML] = 3;
4640 tmap[MATP_ML][MATP_nd][MATP_MR] = 4;
4641 tmap[MATP_ML][MATP_nd][MATP_D] = 5;
4642
4643 tmap[MATP_ML][MATL_nd][MATP_IL] = 0;
4644 tmap[MATP_ML][MATL_nd][MATP_IR] = 1;
4645 tmap[MATP_ML][MATL_nd][MATL_ML] = 2;
4646 tmap[MATP_ML][MATL_nd][MATL_D] = 3;
4647
4648 tmap[MATP_ML][MATR_nd][MATP_IL] = 0;
4649 tmap[MATP_ML][MATR_nd][MATP_IR] = 1;
4650 tmap[MATP_ML][MATR_nd][MATR_MR] = 2;
4651 tmap[MATP_ML][MATR_nd][MATR_D] = 3;
4652
4653 tmap[MATP_ML][END_nd][MATP_IL] = 0;
4654 tmap[MATP_ML][END_nd][MATP_IR] = 1;
4655 tmap[MATP_ML][END_nd][END_E] = 2;
4656
4657 tmap[MATP_MR][BIF_nd][MATP_IL] = 0;
4658 tmap[MATP_MR][BIF_nd][MATP_IR] = 1;
4659 tmap[MATP_MR][BIF_nd][BIF_B] = 2;
4660
4661 tmap[MATP_MR][MATP_nd][MATP_IL] = 0;
4662 tmap[MATP_MR][MATP_nd][MATP_IR] = 1;
4663 tmap[MATP_MR][MATP_nd][MATP_MP] = 2;
4664 tmap[MATP_MR][MATP_nd][MATP_ML] = 3;
4665 tmap[MATP_MR][MATP_nd][MATP_MR] = 4;
4666 tmap[MATP_MR][MATP_nd][MATP_D] = 5;
4667
4668 tmap[MATP_MR][MATL_nd][MATP_IL] = 0;
4669 tmap[MATP_MR][MATL_nd][MATP_IR] = 1;
4670 tmap[MATP_MR][MATL_nd][MATL_ML] = 2;
4671 tmap[MATP_MR][MATL_nd][MATL_D] = 3;
4672
4673 tmap[MATP_MR][MATR_nd][MATP_IL] = 0;
4674 tmap[MATP_MR][MATR_nd][MATP_IR] = 1;
4675 tmap[MATP_MR][MATR_nd][MATR_MR] = 2;
4676 tmap[MATP_MR][MATR_nd][MATR_D] = 3;
4677
4678 tmap[MATP_MR][END_nd][MATP_IL] = 0;
4679 tmap[MATP_MR][END_nd][MATP_IR] = 1;
4680 tmap[MATP_MR][END_nd][END_E] = 2;
4681
4682 tmap[MATP_D][BIF_nd][MATP_IL] = 0;
4683 tmap[MATP_D][BIF_nd][MATP_IR] = 1;
4684 tmap[MATP_D][BIF_nd][BIF_B] = 2;
4685
4686 tmap[MATP_D][MATP_nd][MATP_IL] = 0;
4687 tmap[MATP_D][MATP_nd][MATP_IR] = 1;
4688 tmap[MATP_D][MATP_nd][MATP_MP] = 2;
4689 tmap[MATP_D][MATP_nd][MATP_ML] = 3;
4690 tmap[MATP_D][MATP_nd][MATP_MR] = 4;
4691 tmap[MATP_D][MATP_nd][MATP_D] = 5;
4692
4693 tmap[MATP_D][MATL_nd][MATP_IL] = 0;
4694 tmap[MATP_D][MATL_nd][MATP_IR] = 1;
4695 tmap[MATP_D][MATL_nd][MATL_ML] = 2;
4696 tmap[MATP_D][MATL_nd][MATL_D] = 3;
4697
4698 tmap[MATP_D][MATR_nd][MATP_IL] = 0;
4699 tmap[MATP_D][MATR_nd][MATP_IR] = 1;
4700 tmap[MATP_D][MATR_nd][MATR_MR] = 2;
4701 tmap[MATP_D][MATR_nd][MATR_D] = 3;
4702
4703 tmap[MATP_D][END_nd][MATP_IL] = 0;
4704 tmap[MATP_D][END_nd][MATP_IR] = 1;
4705 tmap[MATP_D][END_nd][END_E] = 2;
4706
4707 tmap[MATP_IL][BIF_nd][MATP_IL] = 0;
4708 tmap[MATP_IL][BIF_nd][MATP_IR] = 1;
4709 tmap[MATP_IL][BIF_nd][BIF_B] = 2;
4710
4711 tmap[MATP_IL][MATP_nd][MATP_IL] = 0;
4712 tmap[MATP_IL][MATP_nd][MATP_IR] = 1;
4713 tmap[MATP_IL][MATP_nd][MATP_MP] = 2;
4714 tmap[MATP_IL][MATP_nd][MATP_ML] = 3;
4715 tmap[MATP_IL][MATP_nd][MATP_MR] = 4;
4716 tmap[MATP_IL][MATP_nd][MATP_D] = 5;
4717
4718 tmap[MATP_IL][MATL_nd][MATP_IL] = 0;
4719 tmap[MATP_IL][MATL_nd][MATP_IR] = 1;
4720 tmap[MATP_IL][MATL_nd][MATL_ML] = 2;
4721 tmap[MATP_IL][MATL_nd][MATL_D] = 3;
4722
4723 tmap[MATP_IL][MATR_nd][MATP_IL] = 0;
4724 tmap[MATP_IL][MATR_nd][MATP_IR] = 1;
4725 tmap[MATP_IL][MATR_nd][MATR_MR] = 2;
4726 tmap[MATP_IL][MATR_nd][MATR_D] = 3;
4727
4728 tmap[MATP_IL][END_nd][MATP_IL] = 0;
4729 tmap[MATP_IL][END_nd][MATP_IR] = 1;
4730 tmap[MATP_IL][END_nd][END_E] = 2;
4731
4732 tmap[MATP_IR][BIF_nd][MATP_IR] = 0;
4733 tmap[MATP_IR][BIF_nd][BIF_B] = 1;
4734
4735 tmap[MATP_IR][MATP_nd][MATP_IR] = 0;
4736 tmap[MATP_IR][MATP_nd][MATP_MP] = 1;
4737 tmap[MATP_IR][MATP_nd][MATP_ML] = 2;
4738 tmap[MATP_IR][MATP_nd][MATP_MR] = 3;
4739 tmap[MATP_IR][MATP_nd][MATP_D] = 4;
4740
4741 tmap[MATP_IR][MATL_nd][MATP_IR] = 0;
4742 tmap[MATP_IR][MATL_nd][MATL_ML] = 1;
4743 tmap[MATP_IR][MATL_nd][MATL_D] = 2;
4744
4745 tmap[MATP_IR][MATR_nd][MATP_IR] = 0;
4746 tmap[MATP_IR][MATR_nd][MATR_MR] = 1;
4747 tmap[MATP_IR][MATR_nd][MATR_D] = 2;
4748
4749 tmap[MATP_IR][END_nd][MATP_IR] = 0;
4750 tmap[MATP_IR][END_nd][END_E] = 1;
4751
4752 tmap[MATL_ML][BIF_nd][MATL_IL] = 0;
4753 tmap[MATL_ML][BIF_nd][BIF_B] = 1;
4754
4755 tmap[MATL_ML][MATP_nd][MATL_IL] = 0;
4756 tmap[MATL_ML][MATP_nd][MATP_MP] = 1;
4757 tmap[MATL_ML][MATP_nd][MATP_ML] = 2;
4758 tmap[MATL_ML][MATP_nd][MATP_MR] = 3;
4759 tmap[MATL_ML][MATP_nd][MATP_D] = 4;
4760
4761 tmap[MATL_ML][MATL_nd][MATL_IL] = 0;
4762 tmap[MATL_ML][MATL_nd][MATL_ML] = 1;
4763 tmap[MATL_ML][MATL_nd][MATL_D] = 2;
4764
4765 tmap[MATL_ML][MATR_nd][MATL_IL] = 0;
4766 tmap[MATL_ML][MATR_nd][MATR_MR] = 1;
4767 tmap[MATL_ML][MATR_nd][MATR_D] = 2;
4768
4769 tmap[MATL_ML][END_nd][MATL_IL] = 0;
4770 tmap[MATL_ML][END_nd][END_E] = 1;
4771
4772 tmap[MATL_D][BIF_nd][MATL_IL] = 0;
4773 tmap[MATL_D][BIF_nd][BIF_B] = 1;
4774
4775 tmap[MATL_D][MATP_nd][MATL_IL] = 0;
4776 tmap[MATL_D][MATP_nd][MATP_MP] = 1;
4777 tmap[MATL_D][MATP_nd][MATP_ML] = 2;
4778 tmap[MATL_D][MATP_nd][MATP_MR] = 3;
4779 tmap[MATL_D][MATP_nd][MATP_D] = 4;
4780
4781 tmap[MATL_D][MATL_nd][MATL_IL] = 0;
4782 tmap[MATL_D][MATL_nd][MATL_ML] = 1;
4783 tmap[MATL_D][MATL_nd][MATL_D] = 2;
4784
4785 tmap[MATL_D][MATR_nd][MATL_IL] = 0;
4786 tmap[MATL_D][MATR_nd][MATR_MR] = 1;
4787 tmap[MATL_D][MATR_nd][MATR_D] = 2;
4788
4789 tmap[MATL_D][END_nd][MATL_IL] = 0;
4790 tmap[MATL_D][END_nd][END_E] = 1;
4791
4792 tmap[MATL_IL][BIF_nd][MATL_IL] = 0;
4793 tmap[MATL_IL][BIF_nd][BIF_B] = 1;
4794
4795 tmap[MATL_IL][MATP_nd][MATL_IL] = 0;
4796 tmap[MATL_IL][MATP_nd][MATP_MP] = 1;
4797 tmap[MATL_IL][MATP_nd][MATP_ML] = 2;
4798 tmap[MATL_IL][MATP_nd][MATP_MR] = 3;
4799 tmap[MATL_IL][MATP_nd][MATP_D] = 4;
4800
4801 tmap[MATL_IL][MATL_nd][MATL_IL] = 0;
4802 tmap[MATL_IL][MATL_nd][MATL_ML] = 1;
4803 tmap[MATL_IL][MATL_nd][MATL_D] = 2;
4804
4805 tmap[MATL_IL][MATR_nd][MATL_IL] = 0;
4806 tmap[MATL_IL][MATR_nd][MATR_MR] = 1;
4807 tmap[MATL_IL][MATR_nd][MATR_D] = 2;
4808
4809 tmap[MATL_IL][END_nd][MATL_IL] = 0;
4810 tmap[MATL_IL][END_nd][END_E] = 1;
4811
4812 tmap[MATR_MR][BIF_nd][MATR_IR] = 0;
4813 tmap[MATR_MR][BIF_nd][BIF_B] = 1;
4814
4815 tmap[MATR_MR][MATP_nd][MATR_IR] = 0;
4816 tmap[MATR_MR][MATP_nd][MATP_MP] = 1;
4817 tmap[MATR_MR][MATP_nd][MATP_ML] = 2;
4818 tmap[MATR_MR][MATP_nd][MATP_MR] = 3;
4819 tmap[MATR_MR][MATP_nd][MATP_D] = 4;
4820
4821 tmap[MATR_MR][MATR_nd][MATR_IR] = 0;
4822 tmap[MATR_MR][MATR_nd][MATR_MR] = 1;
4823 tmap[MATR_MR][MATR_nd][MATR_D] = 2;
4824
4825 tmap[MATR_D][BIF_nd][MATR_IR] = 0;
4826 tmap[MATR_D][BIF_nd][BIF_B] = 1;
4827
4828 tmap[MATR_D][MATP_nd][MATR_IR] = 0;
4829 tmap[MATR_D][MATP_nd][MATP_MP] = 1;
4830 tmap[MATR_D][MATP_nd][MATP_ML] = 2;
4831 tmap[MATR_D][MATP_nd][MATP_MR] = 3;
4832 tmap[MATR_D][MATP_nd][MATP_D] = 4;
4833
4834 tmap[MATR_D][MATR_nd][MATR_IR] = 0;
4835 tmap[MATR_D][MATR_nd][MATR_MR] = 1;
4836 tmap[MATR_D][MATR_nd][MATR_D] = 2;
4837
4838 tmap[MATR_IR][BIF_nd][MATR_IR] = 0;
4839 tmap[MATR_IR][BIF_nd][BIF_B] = 1;
4840
4841 tmap[MATR_IR][MATP_nd][MATR_IR] = 0;
4842 tmap[MATR_IR][MATP_nd][MATP_MP] = 1;
4843 tmap[MATR_IR][MATP_nd][MATP_ML] = 2;
4844 tmap[MATR_IR][MATP_nd][MATP_MR] = 3;
4845 tmap[MATR_IR][MATP_nd][MATP_D] = 4;
4846
4847 tmap[MATR_IR][MATR_nd][MATR_IR] = 0;
4848 tmap[MATR_IR][MATR_nd][MATR_MR] = 1;
4849 tmap[MATR_IR][MATR_nd][MATR_D] = 2;
4850
4851 tmap[BIF_B][BEGL_nd][BEGL_S] = 0;
4852
4853 tmap[BIF_B][BEGR_nd][BEGR_S] = 0;
4854
4855 return tmap;
4856
4857 ERROR:
4858 cm_Fail("Memory allocation error.");
4859 return NULL; /* not reached */
4860 }
4861
4862 /* Function: cm_FreeTransitionMap()
4863 *
4864 * Purpose: Free a transition map.
4865 *
4866 * Returns: (void)
4867 */
4868 void
4869 cm_FreeTransitionMap(char ***tmap)
4870 {
4871 int i, j;
4872
4873 if(tmap != NULL) {
4874 for(i = 0; i < UNIQUESTATES; i++) {
4875 for(j = 0; j < NODETYPES; j++) {
4876 free(tmap[i][j]);
4877 }
4878 free(tmap[i]);
4879 }
4880 free(tmap);
4881 }
4882 return;
4883 }
4884
4885
4886 /* Function: InsertsGivenNodeIndex()
4887 * Incept: EPN, Tue Feb 28 08:50:19 2012
4888 *
4889 * Purpose: Given a CM and a node index, return the insert state(s)
4890 * in that node in <ret_i1> and <ret_i2>. If less than 2
4891 * inserts exist, <ret_i2> will be -1, if no inserts exist,
4892 * <ret_i1> will also be set as -1.
4893 *
4894 * Returns: insert state indices in <i1> and <i2>.
4895 */
4896 void
4897 InsertsGivenNodeIndex(CM_t *cm, int nd, int *ret_i1, int *ret_i2)
4898 {
4899 int i1 = -1;
4900 int i2 = -1;
4901 int v = cm->nodemap[nd];
4902
4903 switch (cm->ndtype[nd]) {
4904 case MATP_nd: i1 = v+4; i2 = v+5; break;
4905 case MATL_nd: i1 = v+2; break;
4906 case MATR_nd: i1 = v+2; break;
4907 case BEGR_nd: i1 = v+1; break;
4908 case ROOT_nd: i1 = v+1; i2 = v+2; break;
4909 }
4910 /* other node types don't have any inserts */
4911
4912 *ret_i1 = i1;
4913 *ret_i2 = i2;
4914 return;
4915 }
4916
4917
4918 /* Function: cm_GuideTree()
4919 * Incept: EPN, Thu Oct 25 06:24:06 2012
4920 *
4921 * Purpose: Given a CM and a MSA with aligned sequences to that
4922 * CM construct a 'guide tree' (a Parsetree_t object)
4923 * that corresponds to the CM with MSA coordinates.
4924 *
4925 * The MSA must have RF annotation indicating which
4926 * positions are consensus (nongap) and which are
4927 * inserts (gap) and which are missing (EL: ~).
4928 *
4929 * This guidetree is useful for backconverting the
4930 * aligned residues to parsetrees themselves.
4931 *
4932 * This function is similar to HandModelmaker()
4933 * in that it generates a guide tree, but is
4934 * different in that we already have the CM,
4935 * whereas in HandModelmaker() we do not have
4936 * the CM (in fact, in HandModelMaker we use
4937 * the guide tree to construct the CM -- the
4938 * inverse of what we do here.)
4939 *
4940 * Guide tree for <cm> is returned in <ret_gtr>.
4941 *
4942 * Returns: eslOK on success.
4943 * eslEINVAL if msa->rf is NULL or
4944 * msa->rf nongap length != cm->clen
4945 */
4946 int
4947 cm_Guidetree(CM_t *cm, char *errbuf, ESL_MSA *msa, Parsetree_t **ret_gtr)
4948 {
4949 Parsetree_t *gtr = NULL;
4950 ESL_STACK *pda = NULL;
4951 int *matassign = NULL; /* 1..alen array; 0=insert col, 1=match col */
4952 int *elassign = NULL; /* 1..alen array; 0=match/ins col, 1=EL col */
4953 int *c2a_map = NULL; /* [1..cm->clen] map from consensus (match) positions to alignment positions */
4954 int nd, apos, v, cpos;
4955 int i, j, k, el_i, el_j; /* position counters */
4956 int status;
4957
4958 if (msa->rf == NULL) ESL_FAIL(eslEINVAL, errbuf, "msa->rf is NULL in cm_Guidetree()");
4959
4960 /* create a map from consensus positions to alignment positions,
4961 * and fill in matassign and elassign, all for convenience later
4962 */
4963 ESL_ALLOC(c2a_map, sizeof(int) * (cm->clen+1));
4964 ESL_ALLOC(matassign, sizeof(int) * (msa->alen+1));
4965 ESL_ALLOC(elassign, sizeof(int) * (msa->alen+1));
4966 c2a_map[0] = 0; /* invalid */
4967 cpos = 0;
4968
4969 for (apos = 1; apos <= msa->alen; apos++) {
4970 matassign[apos] = ((esl_abc_CIsGap (msa->abc, msa->rf[apos-1])) || /* CIsGap returns true for '.', '_' and '-' only (they're equivalent, see create_rna() in esl_alphabet.c()) */
4971 (esl_abc_CIsMissing(msa->abc, msa->rf[apos-1]))) /* CIsMissing returns true for '~' only */
4972 ? FALSE : TRUE;
4973 elassign[apos] = (esl_abc_CIsMissing(msa->abc, msa->rf[apos-1])) ? TRUE : FALSE;
4974 if(! (esl_abc_CIsGap(cm->abc, msa->rf[apos-1]) || esl_abc_CIsMissing(cm->abc, msa->rf[apos-1]))) {
4975 cpos++;
4976 c2a_map[cpos] = apos;
4977 }
4978 }
4979 if(cpos != cm->clen) ESL_FAIL(eslEINVAL, errbuf, "msa->rf nongap length (%d) != clen (%d)\n", cpos, cm->clen);
4980
4981 gtr = CreateParsetree(cm->nodes); /* the guide tree we'll grow */
4982
4983 /* Now create the guide tree using the same procedure used by
4984 * cm_modelmaker.c::HandModelmaker(), except that now it is simpler
4985 * because we already know the node architecture of the CM. We push
4986 * and pop node index and i..j alignment coordinates rooted at the
4987 * node's subtree to a stack to properly deal with the branching
4988 * structure of the tree. It's important we use this specific
4989 * strategy (as opposed to the strategy used for constructing an
4990 * emitmap in display.c:CreateEmitMap()) because we have to deal
4991 * with inserts and ELs in the MSA.
4992 */
4993 if ((pda = esl_stack_ICreate()) == NULL) { status = eslEMEM; goto ERROR; }
4994 if((status = esl_stack_IPush(pda, 1)) != eslOK) goto ERROR; /* emitl */
4995 if((status = esl_stack_IPush(pda, msa->alen)) != eslOK) goto ERROR; /* emitr */
4996 if((status = esl_stack_IPush(pda, 0)) != eslOK) goto ERROR; /* node index */
4997 while (esl_stack_IPop(pda, &nd) != eslEOD) /* pop a node to attach */
4998 {
4999 esl_stack_IPop(pda, &j);
5000 esl_stack_IPop(pda, &i); /* i..j == subseq we're responsible for */
5001
5002 /* We'll skip EL columns but need to remember what i and j would
5003 * be if we didn't: <el_i> and <el_j>. Then, we can set emitr
5004 * for MATL and emitl for MATR as <el_j> and <el_i> respectively.
5005 */
5006 el_i = i;
5007 el_j = j;
5008 while(i <= msa->alen && elassign[i]) i++;
5009 while(j >= 1 && elassign[j]) j--;
5010 if(i > (msa->alen+1)) ESL_XFAIL(eslEINVAL, errbuf, "cm_GuideTree(): problem with local ends (RF='~') during guide tree construction");
5011 if(j < 0) ESL_XFAIL(eslEINVAL, errbuf, "cm_GuideTree(): problem with local ends (RF='~') during guide tree construction");
5012
5013 /*printf("G nd: %2d (%4s) i: %2d j: %2d el_i: %2d el_j: %2d\n", nd, Nodetype(cm->ndtype[nd]), i, j, el_i, el_j);*/
5014
5015 v = cm->nodemap[nd];
5016 gtr->state[nd] = cm->ndtype[nd];
5017 gtr->mode[nd] = TRMODE_J;
5018 gtr->prv[nd] = (cm->ndtype[nd] == ROOT_nd) ? -1 : cm->ndidx[cm->plast[v]];
5019 gtr->n++;
5020
5021 if (cm->ndtype[nd] == END_nd) {
5022 gtr->nxtl[nd] = -1;
5023 gtr->nxtr[nd] = -1;
5024 gtr->emitl[nd] = i;
5025 gtr->emitr[nd] = j;
5026 }
5027
5028 else if (cm->ndtype[nd] == ROOT_nd) { /* try to push i,j; but deal with IL and IR */
5029 gtr->nxtl[nd] = nd+1;
5030 gtr->nxtr[nd] = -1;
5031 gtr->emitl[nd] = i;
5032 gtr->emitr[nd] = j;
5033 for (; i <= j; i++) if (matassign[i] || elassign[i]) break;
5034 for (; j >= i; j--) if (matassign[j] || elassign[j]) break;
5035 if((status = esl_stack_IPush(pda, i)) != eslOK) goto ERROR;
5036 if((status = esl_stack_IPush(pda, j)) != eslOK) goto ERROR;
5037 if((status = esl_stack_IPush(pda, nd+1)) != eslOK) goto ERROR;
5038 }
5039
5040 else if (cm->ndtype[nd] == BEGL_nd) { /* no inserts */
5041 gtr->nxtl[nd] = nd+1;
5042 gtr->nxtr[nd] = -1;
5043 gtr->emitl[nd] = i;
5044 gtr->emitr[nd] = j;
5045 if((status = esl_stack_IPush(pda, i)) != eslOK) goto ERROR;
5046 if((status = esl_stack_IPush(pda, j)) != eslOK) goto ERROR;
5047 if((status = esl_stack_IPush(pda, nd+1)) != eslOK) goto ERROR; /* we don't know yet what the next node will be */
5048 }
5049
5050 else if (cm->ndtype[nd] == BEGR_nd) { /* look for INSL */
5051 gtr->nxtl[nd] = nd+1;
5052 gtr->nxtr[nd] = -1;
5053 gtr->emitl[nd] = i;
5054 gtr->emitr[nd] = j;
5055 for (; i <= j; i++) if (matassign[i] || elassign[i]) break;
5056 if((status = esl_stack_IPush(pda, i)) != eslOK) goto ERROR;
5057 if((status = esl_stack_IPush(pda, j)) != eslOK) goto ERROR;
5058 if((status = esl_stack_IPush(pda, nd+1)) != eslOK) goto ERROR; /* we don't know yet what the next node will be */
5059 }
5060
5061 else if (cm->ndtype[nd] == MATL_nd) {
5062 /* i unpaired. This is a MATL node; allow INSL */
5063 gtr->nxtl[nd] = nd+1;
5064 gtr->nxtr[nd] = -1;
5065 gtr->emitl[nd] = i;
5066 gtr->emitr[nd] = el_j;
5067 for (i = i+1; i <= j; i++) if (matassign[i] || elassign[i]) break;
5068 if((status = esl_stack_IPush(pda, i)) != eslOK) goto ERROR;
5069 if((status = esl_stack_IPush(pda, el_j)) != eslOK) goto ERROR;
5070 if((status = esl_stack_IPush(pda, nd+1)) != eslOK) goto ERROR; /* we don't know yet what the next node will be */
5071 }
5072
5073 else if (cm->ndtype[nd] == MATR_nd) {
5074 gtr->nxtl[nd] = nd+1;
5075 gtr->nxtr[nd] = -1;
5076 gtr->emitl[nd] = el_i;
5077 gtr->emitr[nd] = j;
5078 for (j = j-1; j >= i; j--) if (matassign[j] || elassign[j]) break;
5079 if((status = esl_stack_IPush(pda, el_i)) != eslOK) goto ERROR;
5080 if((status = esl_stack_IPush(pda, j)) != eslOK) goto ERROR;
5081 if((status = esl_stack_IPush(pda, nd+1)) != eslOK) goto ERROR; /* we don't know yet what the next node will be */
5082 }
5083
5084 else if (cm->ndtype[nd] == MATP_nd) {
5085 gtr->nxtl[nd] = nd+1;
5086 gtr->nxtr[nd] = -1;
5087 gtr->emitl[nd] = i;
5088 gtr->emitr[nd] = j;
5089 for (i = i+1; i <= j; i++) if (matassign[i] || elassign[i]) break;
5090 for (j = j-1; j >= i; j--) if (matassign[j] || elassign[j]) break;
5091 if((status = esl_stack_IPush(pda, i)) != eslOK) goto ERROR;
5092 if((status = esl_stack_IPush(pda, j)) != eslOK) goto ERROR;
5093 if((status = esl_stack_IPush(pda, nd+1)) != eslOK) goto ERROR; /* we don't know yet what the next node will be */
5094 }
5095
5096 else if (cm->ndtype[nd] == BIF_nd) {
5097 gtr->nxtl[nd] = cm->ndidx[cm->cfirst[v]];
5098 gtr->nxtr[nd] = cm->ndidx[cm->cnum[v]];
5099 gtr->emitl[nd] = i;
5100 gtr->emitr[nd] = j;
5101
5102 k = c2a_map[cm->emap->rpos[nd+1]-1];
5103
5104 /* push the right BEGIN node first */
5105 if((status = esl_stack_IPush(pda, k+1)) != eslOK) goto ERROR;
5106 if((status = esl_stack_IPush(pda, j)) != eslOK) goto ERROR;
5107 if((status = esl_stack_IPush(pda, cm->ndidx[cm->cnum[cm->nodemap[nd]]])) != eslOK) goto ERROR;
5108 /* then push the left BEGIN node */
5109 if((status = esl_stack_IPush(pda, i)) != eslOK) goto ERROR;
5110 if((status = esl_stack_IPush(pda, k)) != eslOK) goto ERROR;
5111 if((status = esl_stack_IPush(pda, cm->ndidx[cm->cfirst[cm->nodemap[nd]]])) != eslOK) goto ERROR;
5112 }
5113 } /* while something's on the stack */
5114
5115
5116
5117 esl_stack_Destroy(pda);
5118 free(elassign);
5119 free(matassign);
5120 free(c2a_map);
5121
5122 *ret_gtr = gtr;
5123 return eslOK;
5124
5125 ERROR:
5126 esl_stack_Destroy(pda);
5127 free(elassign);
5128 free(matassign);
5129 free(c2a_map);
5130
5131 *ret_gtr = NULL;
5132 FreeParsetree(gtr);
5133 return status;
5134 }
5135