Debian Codegraph @

src/eweight.c

raw symbols SCIP
1 /* eweight.c [EPN 11.07.05]
2 * based on: HMMER 2.4devl's lsj_eweight.c
3 * Most original comments from lsj_eweight.c untouched.
4 *
5 * LSJ, Wed Feb 4 15:03:58 CST 2004
6 *
7 * entropy targeting:
8 * Code for setting effective sequence number (in cmbuild) by
9 * achieving a certain target entropy loss, relative to background
10 * null distribution.
11 */
12
13 #include <esl_config.h>
14 #include <p7_config.h>
15 #include "config.h"
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <math.h>
20
21 #include "easel.h"
22 #include "esl_rootfinder.h"
23 #include "esl_vectorops.h"
24
25 #include "hmmer.h"
26
27 #include "infernal.h"
28
29 struct ew_param_s {
30 CM_t *cm; /* ptr to the original count-based CM, cm->t and cm->e be changed, but we have a copy of the original data in t_orig and e_orig */
31 const Prior_t *pri; /* Dirichlet prior used to parameterize from counts */
32 float **t_orig; /* copy of initial (when this object was created) CM transitions in counts form */
33 float **e_orig; /* copy of initial (when this object was created) CM emissions in counts form */
34 float *begin_orig; /* copy of initial (when this object was created) CM begin transitions in counts form */
35 float *end_orig; /* copy of initial (when this object was created) CM end transitions in counts form */
36 double etarget; /* information content target, in bits */
37 };
38
39
40 /* Evaluate fx = cm rel entropy - etarget, which we want to be = 0,
41 * for effective sequence number <Neff>.
42 */
43 static int
44 cm_eweight_target_f(double Neff, void *params, double *ret_fx)
45 {
46 struct ew_param_s *p = (struct ew_param_s *) params;
47 int v, i;
48
49 /* copy parameters from to p->*_orig to CM arrays */
50 for (v = 0; v < p->cm->M; v++) {
51 for (i = 0; i < MAXCONNECT; i++) p->cm->t[v][i] = p->t_orig[v][i];
52 for (i = 0; i < p->cm->abc->K * p->cm->abc->K; i++) p->cm->e[v][i] = p->e_orig[v][i];
53 p->cm->begin[v] = p->begin_orig[v];
54 p->cm->end[v] = p->end_orig[v];
55 }
56 cm_Rescale(p->cm, Neff / (double) p->cm->nseq);
57 PriorifyCM(p->cm, p->pri);
58 *ret_fx = cm_MeanMatchRelativeEntropy(p->cm) - p->etarget; /* only diff with hmm_eweight_target_f */
59 return eslOK;
60 }
61
62 /* Evaluate fx = hmm rel entropy - etarget, which we want to be = 0,
63 * for effective sequence number <x>. Differs from cm_eweight_target_f
64 * in that emissions from MATP_MP pair emitting states are marginalized
65 * out, effectively treating the CM like an HMM. This is done with
66 * a cm_MeanMatchRelativeEntropyHMM() instead of cm_MeanMatchRelativeEntropy().
67 *
68 */
69 static int
70 hmm_eweight_target_f(double Neff, void *params, double *ret_fx)
71 {
72 struct ew_param_s *p = (struct ew_param_s *) params;
73 int v, i;
74 /*printf("hmm_eweight_target_f() Neff: %f\n", Neff); */
75
76 /* copy parameters from CM to p->*_orig arrays */
77 for (v = 0; v < p->cm->M; v++) {
78 for (i = 0; i < MAXCONNECT; i++) p->cm->t[v][i] = p->t_orig[v][i];
79 for (i = 0; i < p->cm->abc->K * p->cm->abc->K; i++) p->cm->e[v][i] = p->e_orig[v][i];
80 p->cm->begin[v] = p->begin_orig[v];
81 p->cm->end[v] = p->end_orig[v];
82 }
83 cm_Rescale(p->cm, Neff / (double) p->cm->nseq);
84 PriorifyCM(p->cm, p->pri);
85 *ret_fx = cm_MeanMatchRelativeEntropyHMM(p->cm) - p->etarget; /* only diff with cm_eweight_target_f */
86 return eslOK;
87 }
88
89 /* Function: cm_EntropyWeight()
90 * Incept: EPN, Mon Jan 7 07:19:46 2008
91 * based on HMMER3's p7_EntropyWeight()
92 * SRE, Fri May 4 15:32:59 2007 [Janelia]
93 *
94 * Purpose: Use the "entropy weighting" algorithm to determine
95 * what effective sequence number we should use, and
96 * return it in <ret_Neff>.
97 *
98 * Caller provides a count-based <cm>, and the
99 * Dirichlet prior <pri> that's to be used to parameterize
100 * models; neither of these will be modified.
101 * Caller also provides the relative entropy
102 * target in bits in <etarget>.
103 *
104 * <ret_Neff> will range from min_Neff to the true number of
105 * sequences counted into the model, <hmm->nseq>.
106 *
107 * If <pretend_cm_is_hmm> is TRUE the CM's MATP_MP pair
108 * emissions are marginalized, treating pair emitting states
109 * effectively as a pair of singlet emitting states.
110 *
111 * Minimum allowed Neff is <min_Neff>. Maximum allowed Neff
112 * is <max_Neff> (new as of v1.1.3, controllable via
113 * cmbuild cmdline option --emaxseq).)
114 *
115 * Returns: <eslOK> on success.
116 *
117 * Throws: <eslEMEM> on allocation failure.
118 */
119 int
120 cm_EntropyWeight(CM_t *cm, const Prior_t *pri, double etarget, double min_Neff, double max_Neff, int pretend_cm_is_hmm, double *ret_hmm_re, double *ret_Neff)
121 {
122 int status;
123 ESL_ROOTFINDER *R = NULL;
124 struct ew_param_s p;
125 double Neff;
126 double fx;
127 double hmm_re;
128 int v, i;
129
130 /* Store parameters in the structure we'll pass to the rootfinder
131 */
132 p.cm = cm;
133 /* copy parameters of the CM that will be changed by cm_Rescale() */
134 ESL_ALLOC(p.t_orig, (cm->M) * sizeof(float *));
135 ESL_ALLOC(p.e_orig, (cm->M) * sizeof(float *));
136 ESL_ALLOC(p.begin_orig, (cm->M) * sizeof(float));
137 ESL_ALLOC(p.end_orig, (cm->M) * sizeof(float));
138
139 p.t_orig[0] = NULL;
140 p.e_orig[0] = NULL;
141 ESL_ALLOC(p.t_orig[0], MAXCONNECT * cm->M * sizeof(float));
142 ESL_ALLOC(p.e_orig[0], cm->abc->K * cm->abc->K * cm->M * sizeof(float));
143 for (v = 0; v < cm->M; v++) {
144 p.t_orig[v] = p.t_orig[0] + v * MAXCONNECT;
145 p.e_orig[v] = p.e_orig[0] + v * (cm->abc->K * cm->abc->K);
146 }
147 for (v = 0; v < p.cm->M; v++) {
148 for (i = 0; i < MAXCONNECT; i++) p.t_orig[v][i] = cm->t[v][i];
149 for (i = 0; i < cm->abc->K * cm->abc->K; i++) p.e_orig[v][i] = cm->e[v][i];
150 p.begin_orig[v] = cm->begin[v];
151 p.end_orig[v] = cm->end[v];
152 }
153 p.pri = pri;
154 p.etarget = etarget;
155
156 /* First, check if min_Neff gives a rel entropy >= e.target, if so
157 * set Neff to min_Neff. In this case its impossible to get a Neff
158 * < min_Neff such that a relent == etarget, so we use min_Neff;
159 */
160 Neff = min_Neff;
161 if(pretend_cm_is_hmm) { if ((status = hmm_eweight_target_f(Neff, &p, &fx)) != eslOK) goto ERROR; }
162 else { if ((status = cm_eweight_target_f(Neff, &p, &fx)) != eslOK) goto ERROR; }
163
164 if(fx < 0.) { /* an Neff > min_Neff that gives a rel entropy of p.etarget is achievable, find it */
165
166 /* check if max_Neff gives a rel_entropy < e.target, if so set
167 * Neff to max_Neff. In this case its impossible to get a Neff >
168 * max_Neff such that relent == etarget, so we use max_Neff;
169 * (max_Neff is typically cm->nseq (for cmbuild, this it's
170 * cm->nseq unless --emaxseq is used on cmdline). Using
171 * max_Neff in this way is new in v1.1.3, in versions 1.1.2
172 * and earlier max_Neff was fixed at cm->nseq.
173 */
174 Neff = max_Neff;
175 if(pretend_cm_is_hmm) { if ((status = hmm_eweight_target_f(Neff, &p, &fx)) != eslOK) goto ERROR; }
176 else { if ((status = cm_eweight_target_f(Neff, &p, &fx)) != eslOK) goto ERROR; }
177
178 if (fx > 0.) {
179 if(pretend_cm_is_hmm) { if ((R = esl_rootfinder_Create(hmm_eweight_target_f, &p)) == NULL) {status = eslEMEM; goto ERROR;} }
180 else { if ((R = esl_rootfinder_Create(cm_eweight_target_f, &p)) == NULL) {status = eslEMEM; goto ERROR;} }
181 esl_rootfinder_SetAbsoluteTolerance(R, 0.01); /* getting Neff to ~2 sig digits is fine */
182 if ((status = esl_root_Bisection(R, 0., max_Neff, &Neff)) != eslOK) goto ERROR;
183
184 esl_rootfinder_Destroy(R);
185 }
186 }
187 /* we've found Neff, determine the relative entropy of the CM if we marginalize the MP pair emissions,
188 * this is (ALMOST) the relative entropy of the CP9 HMM we'll eventually construct from it,
189 * (it's only ALMOST b/c the CP9 will have marginalized emissions from the MATP_MP PLUS the MATP_ML
190 * state weighted by the expected number of times each state is visited).
191 */
192 hmm_re = cm_MeanMatchRelativeEntropyHMM(p.cm);
193
194 /* reset CM params to their original values */
195 for (v = 0; v < cm->M; v++) {
196 for (i = 0; i < MAXCONNECT; i++) cm->t[v][i] = p.t_orig[v][i];
197 for (i = 0; i < cm->abc->K * cm->abc->K; i++) cm->e[v][i] = p.e_orig[v][i];
198 cm->begin[v] = p.begin_orig[v];
199 cm->end[v] = p.end_orig[v];
200 }
201 /* free params p */
202 free(p.t_orig[0]);
203 free(p.t_orig);
204 free(p.e_orig[0]);
205 free(p.e_orig);
206 free(p.begin_orig);
207 free(p.end_orig);
208
209 *ret_hmm_re = hmm_re;
210 *ret_Neff = Neff;
211 return eslOK;
212
213 ERROR:
214 if (R != NULL) esl_rootfinder_Destroy(R);
215 *ret_Neff = (double) cm->nseq;
216 return status;
217 }
218
219 /* Function: cm_Rescale()
220 *
221 * Incept: EPN 11.07.05
222 * based on: HMMER's plan7.c's Plan7Rescale() (Steve Johnson)
223 *
224 * Purpose: Scale a counts-based CM by some factor, for
225 * adjusting counts to a new effective sequence number.
226 *
227 * Args: cm - counts based CM.
228 * scale - scaling factor (e.g. eff_nseq/nseq); 1.0= no scaling.
229 *
230 * Returns: (void)
231 */
232 void
233 cm_Rescale(CM_t *cm, float scale)
234 {
235 int v;
236
237 for (v = 0; v < cm->M; v++)
238 {
239 /* Scale transition counts vector if not a BIF or E state */
240 if (cm->sttype[v] != B_st && cm->sttype[v] != E_st)
241 {
242 /* Number of transitions is cm->cnum[v] */
243 esl_vec_FScale(cm->t[v], cm->cnum[v], scale);
244 }
245 /* Scale emission counts vectors */
246 if (cm->sttype[v] == MP_st)
247 { /* Consensus base pairs */
248 esl_vec_FScale(cm->e[v], (cm->abc->K*cm->abc->K), scale);
249 }
250 else if ((cm->sttype[v] == ML_st) ||
251 (cm->sttype[v] == MR_st) ||
252 (cm->sttype[v] == IL_st) ||
253 (cm->sttype[v] == IR_st))
254 { /* singlets (some consensus, some not)*/
255 esl_vec_FScale(cm->e[v], cm->abc->K, scale);
256 }
257 }/* end loop over states v */
258
259 /* begin, end transitions; only valid [0..M-1] */
260 esl_vec_FScale(cm->begin, cm->M, scale);
261 esl_vec_FScale(cm->end, cm->M, scale);
262
263 return;
264 }
265
266 /* Function: cp9_Rescale()
267 * EPN based on Steve Johnsons plan 7 version
268 *
269 * Purpose: Scale a counts-based HMM by some factor, for
270 * adjusting counts to a new effective sequence number.
271 *
272 * Args: hmm - counts based HMM.
273 * scale - scaling factor (e.g. eff_nseq/nseq); 1.0= no scaling.
274 *
275 * Returns: (void)
276 */
277 void
278 cp9_Rescale(CP9_t *hmm, float scale)
279 {
280 int k;
281
282 /* emissions and transitions in the main model.
283 * Note that match states are 1..M, insert states are 0..M,
284 * and deletes are 0..M-1
285 */
286 for(k = 1; k <= hmm->M; k++)
287 esl_vec_FScale(hmm->mat[k], hmm->abc->K, scale);
288 for(k = 0; k <= hmm->M; k++)
289 esl_vec_FScale(hmm->ins[k], hmm->abc->K, scale);
290 for(k = 0; k < hmm->M; k++)
291 esl_vec_FScale(hmm->t[k], cp9_NTRANS, scale);
292
293 /* begin, end transitions; only valid [1..M] */
294 esl_vec_FScale(hmm->begin+1, hmm->M, scale);
295 esl_vec_FScale(hmm->end+1, hmm->M, scale);
296
297 return;
298 }
299
300 /* Function: cm_MeanMatchInfo()
301 * Incept: SRE, Fri May 4 11:43:56 2007 [Janelia]
302 *
303 * Purpose: Calculate the mean information content per match state
304 * emission distribution, in bits:
305 *
306 * \[
307 * \frac{1}{M} \sum_{k=1}^{M}
308 * \left[
309 * - \sum_x p_k(x) \log_2 p_k(x)
310 * + \sum_x f(x) \log_2 f(x)
311 * \right]
312 * \]
313 *
314 * where $p_k(x)$ is emission probability for symbol $x$
315 * from match state $k$, and $f(x)$ is the null model's
316 * background emission probability for $x$.
317 *
318 */
319 double
320 cm_MeanMatchInfo(const CM_t *cm)
321 {
322 return esl_vec_FEntropy(cm->null, cm->abc->K) - cm_MeanMatchEntropy(cm);
323 }
324
325 /*
326 * Function: cm_MeanMatchEntropy
327 * Incept: EPN, Tue May 1 14:06:37 2007
328 * Updated to match Sean's analogous p7_MeanMatchEntropy() in
329 * HMMER3's hmmstat.c, EPN, Sat Jan 5 14:48:27 2008.
330 *
331 * Purpose: Calculate the mean entropy per match state emission
332 * distribution, in bits:
333 *
334 * \[
335 * \frac{1}{clen} \sum_{v=0}^{M-1} -\sum_x p_v(x) \log_2 p_v(x)
336 * \]
337 *
338 * where $p_v(x)$ is emission probability for symbol $x$
339 * from MATL\_ML, MATR\_MR or MATP\_MP state $v$. For MATP\_MP
340 * states symbols $x$ are base pairs.
341 */
342 double
343 cm_MeanMatchEntropy(const CM_t *cm)
344 {
345 int v;
346 double H = 0.;
347
348 for (v = 0; v < cm->M; v++)
349 {
350 if(cm->stid[v] == MATP_MP)
351 H += esl_vec_FEntropy(cm->e[v], (cm->abc->K * cm->abc->K));
352 else if(cm->stid[v] == MATL_ML ||
353 cm->stid[v] == MATR_MR)
354 H += esl_vec_FEntropy(cm->e[v], cm->abc->K);
355 }
356 H /= (double) cm->clen;
357 return H;
358 }
359
360
361 /* Function: cm_MeanMatchRelativeEntropy()
362 * Incept: SRE, Fri May 11 09:25:01 2007 [Janelia]
363 *
364 * Purpose: Calculate the mean relative entropy per match state emission
365 * distribution, in bits:
366 *
367 * \[
368 * \frac{1}{M} \sum_{v=0}^{M-1} \sum_x p_v(x) \log_2 \frac{p_v(x)}{f(x)}
369 * \]
370 *
371 * where $p_v(x)$ is emission probability for symbol $x$
372 * from MATL\_ML, MATR\_MR, or MATP\_MP state state $v$,
373 * and $f(x)$ is the null model's background emission
374 * probability for $x$. For MATP\_MP states, $x$ is a
375 * base pair.
376 */
377 double
378 cm_MeanMatchRelativeEntropy(const CM_t *cm)
379 {
380 int status;
381 int v;
382 double KL = 0.;
383 float *pair_null;
384 int i,j;
385 float left_e[cm->abc->K];
386 float right_e[cm->abc->K];
387 /* variables only needed if we uncomment debug print statements at end of function
388 * int KL_pair_denom = 0;
389 * int KL_singlet_denom = 0;
390 * double KL_pair = 0.;
391 * double KL_pair_marg = 0.;
392 * double KL_singlet = 0.;
393 */
394
395 ESL_ALLOC(pair_null, (sizeof(float) * cm->abc->K * cm->abc->K));
396 for(i = 0; i < cm->abc->K; i++)
397 for(j = 0; j < cm->abc->K; j++)
398 pair_null[(i * cm->abc->K) + j] = cm->null[i] * cm->null[j];
399
400 for (v = 0; v < cm->M; v++) {
401 if(cm->stid[v] == MATP_MP) {
402 KL += esl_vec_FRelEntropy(cm->e[v], pair_null, (cm->abc->K * cm->abc->K));
403 // KL_pair += esl_vec_FRelEntropy(cm->e[v], pair_null, (cm->abc->K * cm->abc->K));
404 // KL_pair_denom += 2;
405 /*printf("MP (%5d) %6.3f\n", v, esl_vec_FRelEntropy(cm->e[v], pair_null, (cm->abc->K * cm->abc->K)));*/
406
407 /* calculate marginals */
408 /* left half */
409 esl_vec_FSet(left_e, cm->abc->K, 0.);
410 for(i = 0; i < cm->abc->K; i++) {
411 for(j = (i*cm->abc->K); j < ((i+1)*cm->abc->K); j++) {
412 left_e[i] += cm->e[v][j];
413 }
414 }
415 esl_vec_FNorm(left_e, cm->abc->K);
416 // KL_pair_marg += esl_vec_FRelEntropy(left_e, cm->null, cm->abc->K);
417 /*printf("cm L %4d (%4s) v: %5d KL: %10.5f (added: %10.5f)\n", cm->ndidx[v], "MATP", v, KL, esl_vec_FRelEntropy(left_e, cm->null, cm->abc->K));*/
418 /* right half */
419 esl_vec_FSet(right_e, cm->abc->K, 0.);
420 for(i = 0; i < cm->abc->K; i++) {
421 for(j = i; j < cm->abc->K * cm->abc->K; j += cm->abc->K) {
422 right_e[i] += cm->e[v][j];
423 }
424 }
425 // KL_pair_marg += esl_vec_FRelEntropy(right_e, cm->null, cm->abc->K);
426 }
427 else if(cm->stid[v] == MATL_ML ||
428 cm->stid[v] == MATR_MR) {
429 KL += esl_vec_FRelEntropy(cm->e[v], cm->null, cm->abc->K);
430 // KL_singlet += esl_vec_FRelEntropy(cm->e[v], cm->null, cm->abc->K);
431 // KL_singlet_denom += 1;
432 /*printf("ML/MR (%5d) %6.3f\n", v, esl_vec_FRelEntropy(cm->e[v], cm->null, cm->abc->K));*/
433 }
434 }
435 free(pair_null);
436
437 /*printf("\n%s KL total %8.3f %8.3f per cpos\n", cm->name, KL, KL / (double) cm->clen);
438 printf("%s KL pair %8.3f %8.3f per paired cpos\n", cm->name, KL_pair, KL_pair / (double) KL_pair_denom);
439 printf("%s KL pair(m) %8.3f %8.3f per paired cpos\n", cm->name, KL_pair_marg, KL_pair_marg / (double) KL_pair_denom);
440 printf("%s KL singlet %8.3f %8.3f per unpaired cpos\n", cm->name, KL_singlet, KL_singlet / (double) KL_singlet_denom);*/
441
442 KL /= (double) cm->clen;
443 return KL;
444
445 ERROR:
446 cm_Fail("Memory allocation error.");
447 return 0.; /* NOTREACHED */
448 }
449
450
451 /* Function: cm_MeanMatchInfoHMM()
452 * Incept: EPN, Mon Feb 18 07:43:01 2008
453 *
454 * Purpose: Calculate the mean information content per match state
455 * emission distribution, in bits:
456 *
457 * \[
458 * \frac{1}{M} \sum_{k=1}^{M}
459 * \left[
460 * - \sum_x p_k(x) \log_2 p_k(x)
461 * + \sum_x f(x) \log_2 f(x)
462 * \right]
463 * \]
464 *
465 * where $p_k(x)$ is emission probability for symbol $x$
466 * from match state $k$, and $f(x)$ is the null model's
467 * background emission probability for $x$.
468 *
469 * Differs from cm_MeanMatchInfo() in that base pair emissions
470 * are marginalized, in effect treating the CM like an HMM that
471 * can only emit 1 residue at a time.
472 *
473 */
474 double
475 cm_MeanMatchInfoHMM(const CM_t *cm)
476 {
477 return esl_vec_FEntropy(cm->null, cm->abc->K) - cm_MeanMatchEntropyHMM(cm);
478 }
479
480 /* Function: cm_MeanMatchEntropyHMM
481 * Incept: EPN, Mon Feb 18 08:06:20 2008
482 * Updated to match Sean's analogous p7_MeanMatchEntropy() in
483 * HMMER3's hmmstat.c, EPN, Sat Jan 5 14:48:27 2008.
484 *
485 * Purpose: Calculate the mean entropy per match state emission
486 * distribution, in bits:
487 *
488 * \[
489 * \frac{1}{clen} \sum_{v=0}^{M-1} -\sum_x p_v(x) \log_2 p_v(x)
490 * \]
491 *
492 * where $p_v(x)$ is emission probability for symbol $x$
493 * from MATL\_ML, MATR\_MR or MATP\_MP state $v$. For MATP\_MP
494 * states symbols $x$ are base pairs.
495 *
496 * Differs from cm_MeanMatchEntropy() in that base pair emissions
497 * are marginalized, in effect treating the CM like an HMM that
498 * can only emit 1 residue at a time.
499 */
500 double
501 cm_MeanMatchEntropyHMM(const CM_t *cm)
502 {
503 int v;
504 double H = 0.;
505 float left_e[cm->abc->K];
506 float right_e[cm->abc->K];
507 int i,j;
508
509 for (v = 0; v < cm->M; v++) {
510 if(cm->stid[v] == MATP_MP) {
511 /* calculate marginals */
512 /* left half */
513 esl_vec_FSet(left_e, cm->abc->K, 0.);
514 for(i = 0; i < cm->abc->K; i++) {
515 for(j = (i*cm->abc->K); j < ((i+1)*cm->abc->K); j++) {
516 left_e[i] += cm->e[v][j];
517 }
518 H += esl_vec_FEntropy(left_e, cm->abc->K);
519 }
520 /* right half */
521 esl_vec_FSet(right_e, cm->abc->K, 0.);
522 for(i = 0; i < cm->abc->K; i++) {
523 for(j = i; j < cm->abc->K * cm->abc->K; j += cm->abc->K) {
524 right_e[i] += cm->e[v][j];
525 }
526 H += esl_vec_FEntropy(right_e, cm->abc->K);
527 }
528 }
529 else if(cm->stid[v] == MATL_ML ||
530 cm->stid[v] == MATR_MR) {
531 H += esl_vec_FEntropy(cm->e[v], cm->abc->K);
532 }
533 }
534 H /= (double) cm->clen;
535 return H;
536 }
537
538 /* Function: cm_MeanMatchRelativeEntropyHMM()
539 * Incept: EPN, Mon Feb 18 08:06:24 2008
540 *
541 * Purpose: Calculate the mean relative entropy per match state emission
542 * distribution, in bits:
543 *
544 * \[
545 * \frac{1}{M} \sum_{v=0}^{M-1} \sum_x p_v(x) \log_2 \frac{p_v(x)}{f(x)}
546 * \]
547 *
548 * where $p_v(x)$ is emission probability for symbol $x$
549 * from MATL\_ML, MATR\_MR, or MATP\_MP state state $v$,
550 * and $f(x)$ is the null model's background emission
551 * probability for $x$. For MATP\_MP states, $x$ is a
552 * base pair.
553 *
554 * Differs from cm_MeanMatchRelativeEntropy() in that base pair
555 * emissions are marginalized, in effect treating the CM like an
556 * HMM that can only emit 1 residue at a time.
557 */
558 double
559 cm_MeanMatchRelativeEntropyHMM(const CM_t *cm)
560 {
561 int v;
562 double KL = 0.;
563 float left_e[cm->abc->K];
564 float right_e[cm->abc->K];
565 int i,j;
566
567 for (v = 0; v < cm->M; v++) {
568 if(cm->stid[v] == MATP_MP) {
569 /* calculate marginals */
570 /* left half */
571 esl_vec_FSet(left_e, cm->abc->K, 0.);
572 for(i = 0; i < cm->abc->K; i++) {
573 for(j = (i*cm->abc->K); j < ((i+1)*cm->abc->K); j++) {
574 left_e[i] += cm->e[v][j];
575 }
576 }
577 esl_vec_FNorm(left_e, cm->abc->K);
578 KL += esl_vec_FRelEntropy(left_e, cm->null, cm->abc->K);
579 /*printf("cm L %4d (%4s) v: %5d KL: %10.5f (added: %10.5f)\n", cm->ndidx[v], "MATP", v, KL, esl_vec_FRelEntropy(left_e, cm->null, cm->abc->K));*/
580 /* right half */
581 esl_vec_FSet(right_e, cm->abc->K, 0.);
582 for(i = 0; i < cm->abc->K; i++) {
583 for(j = i; j < cm->abc->K * cm->abc->K; j += cm->abc->K) {
584 right_e[i] += cm->e[v][j];
585 }
586 }
587 KL += esl_vec_FRelEntropy(right_e, cm->null, cm->abc->K);
588 /*printf("cm R %4d (%4s) v: %5d KL: %10.5f (added: %10.5f)\n", cm->ndidx[v], "MATP", v, KL, esl_vec_FRelEntropy(right_e, cm->null, cm->abc->K));*/
589 }
590 else if(cm->stid[v] == MATL_ML ||
591 cm->stid[v] == MATR_MR) {
592 KL += esl_vec_FRelEntropy(cm->e[v], cm->null, cm->abc->K);
593 /*printf("cm %4d (%4s) v: %5d KL: %10.5f (added %10.5f)\n", cm->ndidx[v], Nodetype(cm->ndtype[cm->ndidx[v]]), v, KL, esl_vec_FRelEntropy(cm->e[v], cm->null, cm->abc->K));*/
594 }
595 }
596
597 KL /= (double) cm->clen;
598 return KL;
599 }
600
601 /* Function: cp9_MeanMatchInfo()
602 * Incept: SRE, Fri May 4 11:43:56 2007 [Janelia]
603 *
604 * Purpose: Calculate the mean information content per match state
605 * emission distribution, in bits:
606 *
607 * \[
608 * \frac{1}{M} \sum_{k=1}^{M}
609 * \left[
610 * - \sum_x p_k(x) \log_2 p_k(x)
611 * + \sum_x f(x) \log_2 f(x)
612 * \right]
613 * \]
614 *
615 * where $p_k(x)$ is emission probability for symbol $x$
616 * from match state $k$, and $f(x)$ is the null model's
617 * background emission probability for $x$.
618 *
619 * This statistic is used in "entropy weighting" to set the
620 * total sequence weight when model building.
621 */
622 double
623 cp9_MeanMatchInfo(const CP9_t *cp9)
624 {
625 return esl_vec_FEntropy(cp9->null, cp9->abc->K) - cp9_MeanMatchEntropy(cp9);
626 }
627
628 /* Function: cp9_MeanMatchEntropy()
629 * Incept: SRE, Fri May 4 13:37:15 2007 [Janelia]
630 *
631 * Purpose: Calculate the mean entropy per match state emission
632 * distribution, in bits:
633 *
634 * \[
635 * \frac{1}{M} \sum_{k=1}^{M} -\sum_x p_k(x) \log_2 p_k(x)
636 * \]
637 *
638 * where $p_k(x)$ is emission probability for symbol $x$
639 * from match state $k$.
640 */
641 double
642 cp9_MeanMatchEntropy(const CP9_t *cp9)
643 {
644 int k;
645 double H = 0.;
646
647 for (k = 1; k <= cp9->M; k++)
648 H += esl_vec_FEntropy(cp9->mat[k], cp9->abc->K);
649 H /= (double) cp9->M;
650 return H;
651 }
652
653
654 /* Function: cp9_MeanMatchRelativeEntropy()
655 * Incept: SRE, Fri May 11 09:25:01 2007 [Janelia]
656 *
657 * Purpose: Calculate the mean relative entropy per match state emission
658 * distribution, in bits:
659 *
660 * \[
661 * \frac{1}{M} \sum_{k=1}^{M} \sum_x p_k(x) \log_2 \frac{p_k(x)}{f(x)}
662 * \]
663 *
664 * where $p_k(x)$ is emission probability for symbol $x$
665 * from match state $k$, and $f(x)$ is the null model's
666 * background emission probability for $x$.
667 */
668 double
669 cp9_MeanMatchRelativeEntropy(const CP9_t *cp9)
670 {
671 int k;
672 double KL = 0.;
673
674 for (k = 1; k <= cp9->M; k++) {
675 KL += esl_vec_FRelEntropy(cp9->mat[k], cp9->null, cp9->abc->K);
676 }
677 KL /= (double) cp9->M;
678 return KL;
679 }
680