Debian Codegraph @

hmmer/src/itest_brute.c

raw symbols SCIP
1 /* The "brute" integration test.
2 *
3 * Create an entirely hand-specified profile HMM from given
4 * parameters; enumerate all paths and calculate either the sum
5 * (Forward score) or max (Viterbi score). Compare this to
6 * <p7_GForward()> and <p7_GViterbi()> results.
7 *
8 * The test exercises all possible transitions in a model. The model
9 * is M=3 because this is the minimum model size that can use a D->D
10 * transition in the search profile (D2->D3; D1 isn't in a profile).
11 * The target sequences go up to L=4 because this is the minimum
12 * length that uses an I->I transition.
13 *
14 * To see the models and paths drawn out by hand, xref J1/106-109.
15 *
16 * Besides the hand-specified model, the integration test samples many
17 * more "brute" HMMs randomly, peppering them with zero probability
18 * transitions where possible.
19 *
20 * Viterbi scores (hand enumerated vs. GViterbi()) should match
21 * exactly (within machine precision). Forward scores should match
22 * "closely", with some error introduced by the discretization in
23 * FLogsum()'s table lookup.
24 *
25 * This is an important test of correctness for the generic Viterbi
26 * and Forward implementations. Optimized implementations (impl_sse,
27 * etc) are then verified against the generic implementations.
28 *
29 * SRE, Tue Jul 17 08:17:36 2007 [Janelia]
30 * xref J1/106-109: original implementation
31 * xref J5/118: revival; brought up to date with H3's assumptions of zero insert scores.
32 */
33
34 /* gcc -std=c99 -g -Wall -I. -I../easel -L. -L../easel -o itest_brute itest_brute.c -lhmmer -leasel -lm
35 */
36 #include <p7_config.h>
37
38 #include "easel.h"
39 #include "esl_alphabet.h"
40 #include "esl_dirichlet.h"
41 #include "esl_getopts.h"
42 #include "esl_vectorops.h"
43
44 #include "hmmer.h"
45
46 static ESL_OPTIONS options[] = {
47 /* name type default env range toggles reqs incomp help docgroup*/
48 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
49 { "-o", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "save each tested HMM to file <f>", 0 },
50 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
51 { "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be verbose", 0 },
52 { "-N", eslARG_INT, "100", NULL, NULL, NULL, NULL, NULL, "number of randomly sampled HMMs", 0 },
53 { "--vv", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be very verbose", 0 },
54 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
55 };
56 static char usage[] = "[-options] <hmmfile>";
57 static char banner[] = "the brute force HMM integration test";
58
59 struct p7_bruteparam_s {
60 double a; /* hmm->t[0][p7H_MM] */
61 double b; /* hmm->t[1][p7H_MM] */
62 double c; /* hmm->t[2][p7H_MM] */
63 double d; /* hmm->t[3][p7H_MM] */
64 double e; /* hmm->t[0][p7H_MI] */
65 double f; /* hmm->t[1][p7H_MI] */
66 double g; /* hmm->t[2][p7H_MI] */
67 double h; /* hmm->t[3][p7H_MI] */
68 double i; /* hmm->t[1][p7H_IM] */
69 double j; /* hmm->t[2][p7H_IM] */
70 double k; /* hmm->t[3][p7H_IM] */
71 double l; /* hmm->t[1][p7H_DD] */
72 double m; /* hmm->t[2][p7H_DD] */
73
74 double n; /* N->B exp(gm->xsc[p7P_N][p7P_MOVE]) */
75 double p; /* E->C exp(gm->xsc[p7P_E][p7P_MOVE]) */
76 double q; /* C->T exp(gm->xsc[p7P_C][p7P_MOVE]) */
77 double r; /* J->B exp(gm->xsc[p7P_J][p7P_MOVE]) */
78
79 double alpha; /* hmm->mat[k][A] emission for all match states */
80 double beta; /* hmm->ins[k][A] emission for all insert states */
81
82 double begin[4]; /* constructed from transitions when brute profile is configured. */
83 double end; /* internal ends, set when profile is configured */
84 };
85
86 static void set_bruteparams(struct p7_bruteparam_s *prm);
87 static void sample_zeropeppered_probvector(ESL_RANDOMNESS *r, double *p, int n);
88 static void sample_bruteparams(ESL_RANDOMNESS *r, struct p7_bruteparam_s *prm);
89 static P7_HMM *create_brute_hmm(ESL_ALPHABET *abc, struct p7_bruteparam_s *prm);
90 static P7_PROFILE *create_brute_profile(struct p7_bruteparam_s *prm, P7_HMM *hmm, P7_BG *bg, int do_local);
91 static double score_brute_profile(struct p7_bruteparam_s *prm, P7_BG *bg, int do_viterbi, double sc[5]);
92
93 int
94 main(int argc, char **argv)
95 {
96 struct p7_bruteparam_s prm;
97 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage);
98 ESL_ALPHABET *abc = esl_alphabet_Create(eslDNA);
99 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
100 P7_BG *bg = p7_bg_Create(abc);
101 P7_GMX *gx = p7_gmx_Create(3, 4); /* M=3, L up to 4. */
102 P7_HMM *hmm = NULL;
103 P7_PROFILE *gm = NULL;
104 char *hmmfile = esl_opt_GetString (go, "-o");
105 int N = esl_opt_GetInteger(go, "-N");
106 int do_local;
107 double brute_fwd[5]; /* lod Forward scores for seqs L=0..4 calculated by brute force path enumeration */
108 double brute_vit[5]; /* lod Viterbi scores for seqs L=0..4 calculated by brute force path enumeration */
109 float fsc[5]; /* lod scores for seqs L=0..4 calculated by GForward() DP */
110 float vsc[5]; /* lod scores for seqs L=0..4 calculated by GViterbi() DP */
111 ESL_DSQ dsq[6];
112 int L;
113 int i,j;
114 float vprecision, fprecision; /* expected bound on absolute accuracy for viterbi, forward */
115
116 p7_FLogsumInit();
117
118 for (do_local = 0; do_local <= 1; do_local++) /* run tests in both glocal and local mode */
119 for (j = 0; j <= N; j++) /* #0 = fixed params; #1..N = sampled params */
120 {
121 if (esl_opt_GetBoolean(go, "-v"))
122 printf("%s\n", do_local ? "Local mode (implicit model)" : "Glocal mode (wing retracted)");
123
124 if (j == 0) set_bruteparams(&prm);
125 else sample_bruteparams(r, &prm);
126
127 hmm = create_brute_hmm(abc, &prm);
128 gm = create_brute_profile(&prm, hmm, bg, do_local);
129 score_brute_profile(&prm, bg, TRUE, brute_vit);
130 score_brute_profile(&prm, bg, FALSE, brute_fwd);
131
132 if (hmmfile)
133 {
134 FILE *ofp = fopen(hmmfile, "w");
135 p7_hmmfile_WriteASCII(ofp, -1, hmm);
136 fclose(ofp);
137 }
138
139 for (L = 0; L <= 4; L++)
140 {
141 p7_gmx_GrowTo(gx, 3, L);
142
143 dsq[0] = dsq[L+1] = eslDSQ_SENTINEL; /* Initialize dsq of length L at 0000... (all A) */
144 for (i = 1; i <= L; i++) dsq[i] = 0;
145
146 if (p7_GViterbi(dsq, L, gm, gx, &(vsc[L])) != eslOK) esl_fatal("viterbi failed");
147
148 if (esl_opt_GetBoolean(go, "--vv"))
149 p7_gmx_Dump(stdout, gx, p7_DEFAULT);
150 p7_gmx_Reuse(gx);
151
152 p7_gmx_GrowTo(gx, 3, L);
153 if (p7_GForward(dsq, L, gm, gx, &(fsc[L])) != eslOK) esl_fatal("forward failed");
154 if (esl_opt_GetBoolean(go, "--vv"))
155 p7_gmx_Dump(stdout, gx, p7_DEFAULT);
156 p7_gmx_Reuse(gx);
157
158 vprecision = 1e-4; /* default impl uses fp, should be accurate within machine precision */
159 fprecision = 0.01; /* default impl uses FLogsum, tolerate e^0.1 ~= 1% error in Forward probs */
160
161
162 if (esl_opt_GetBoolean(go, "-v"))
163 printf("%d %-6s %6s %1d %8.4f %8.4f %8.4f %8.4f\n",
164 j,
165 do_local ? "local" : "glocal",
166 (j > 0) ? "random": "fixed",
167 L,
168 brute_fwd[L], fsc[L],
169 brute_vit[L], vsc[L]);
170
171
172 if (fabs(vsc[L] - brute_vit[L]) > vprecision)
173 esl_fatal("Viterbi scores mismatched: %-6s %s L=%1d brute=%8.4f GViterbi()=%8.4f (difference %g)",
174 do_local ? "local" : "glocal",
175 (j > 0) ? "random": "fixed",
176 L,
177 brute_vit[L], vsc[L], fabs(brute_vit[L] - vsc[L]));
178
179 /* verify that Forward scores match closely (within error introduced by FLogsum() */
180 if (fabs(fsc[L] - brute_fwd[L]) > fprecision)
181 esl_fatal("Forward scores mismatched: %-6s %s L=%1d brute=%8.4f GForward()=%8.4f",
182 do_local ? "local" : "glocal",
183 (j > 0) ? "random": "fixed",
184 L,
185 brute_fwd[L], fsc[L]);
186
187
188 }
189 p7_profile_Destroy(gm);
190 p7_hmm_Destroy(hmm);
191 }
192
193 p7_gmx_Destroy(gx);
194 p7_bg_Destroy(bg);
195 esl_alphabet_Destroy(abc);
196 esl_randomness_Destroy(r);
197 esl_getopts_Destroy(go);
198
199 printf("ok\n");
200 return 0;
201 }
202
203
204 static void
205 set_bruteparams(struct p7_bruteparam_s *prm)
206 {
207 prm->a = 0.8; /* hmm->t[0][p7H_MM] */
208 prm->b = 0.7; /* hmm->t[1][p7H_MM] */
209 prm->c = 0.1; /* hmm->t[2][p7H_MM] */
210 prm->d = 0.6; /* hmm->t[3][p7H_MM] */
211 prm->e = 0.05; /* hmm->t[0][p7H_MI] */
212 prm->f = 0.2; /* hmm->t[1][p7H_MI] */
213 prm->g = 0.88; /* hmm->t[2][p7H_MI] */
214 prm->h = 0.90; /* hmm->t[0][p7H_IM] */
215 prm->i = 0.92; /* hmm->t[1][p7H_IM] */
216 prm->j = 0.94; /* hmm->t[2][p7H_IM] */
217 prm->k = 0.96; /* hmm->t[3][p7H_IM] */
218 prm->l = 0.57; /* hmm->t[1][p7H_DD] */
219 prm->m = 0.59; /* hmm->t[2][p7H_DD] */
220
221 #if 0
222 /* Setting n,p,q,r to 1.0 makes the core model account
223 * for the entire target seq: <= 19 paths are possible,
224 * SNB->core->ECT.
225 */
226 prm->n = 1.0; /* N->B exp(gm->xsc[p7P_N][p7P_MOVE]) */
227 prm->p = 1.0; /* E->C exp(gm->xsc[p7P_E][p7P_MOVE]) */
228 prm->q = 1.0; /* C->T exp(gm->xsc[p7P_C][p7P_MOVE]) */
229 prm->r = 1.0; /* J->B exp(gm->xsc[p7P_J][p7P_MOVE]) */
230 #endif
231
232 prm->n = 0.41; /* N->B exp(gm->xsc[p7P_N][p7P_MOVE]) */
233 prm->p = 0.43; /* E->C exp(gm->xsc[p7P_E][p7P_MOVE]) */
234 prm->q = 0.45; /* C->T exp(gm->xsc[p7P_C][p7P_MOVE]) */
235 prm->r = 0.47; /* J->B exp(gm->xsc[p7P_J][p7P_MOVE]) */
236
237 prm->alpha = 0.7; /* hmm->mat[k][A] for all k */
238 prm->beta = 0.25; /* hmm->ins[k][A] for all k [MUST be 0.25, equal to background; H3 assumes insert score 0; xref J5/118 */
239 return;
240 }
241
242
243 static void
244 sample_zeropeppered_probvector(ESL_RANDOMNESS *r, double *p, int n)
245 {
246 esl_dirichlet_DSampleUniform(r, n, p);
247 if (esl_rnd_Roll(r, 2)) /* coin flip */
248 {
249 p[esl_rnd_Roll(r, n)] = 0.0;
250 esl_vec_DNorm(p, n);
251 }
252 }
253
254
255 static void
256 sample_bruteparams(ESL_RANDOMNESS *r, struct p7_bruteparam_s *prm)
257 {
258 double tmp[3];
259
260 /* make sure we can get M->M w/ nonzero prob, but pepper zeros elsewhere */
261 do { sample_zeropeppered_probvector(r, tmp, 3); prm->a = tmp[0]; prm->e = tmp[1]; } while (prm->a == 0.0);
262 do { sample_zeropeppered_probvector(r, tmp, 3); prm->b = tmp[0]; prm->f = tmp[1]; } while (prm->b == 0.0);
263 do { sample_zeropeppered_probvector(r, tmp, 3); prm->c = tmp[0]; prm->g = tmp[1]; } while (prm->c == 0.0);
264 do { sample_zeropeppered_probvector(r, tmp, 2); prm->d = tmp[0]; } while (prm->d == 0.0);
265
266 /* pepper any D, I transition. [3][II] cannot be 1.0 (k param)*/
267 sample_zeropeppered_probvector(r, tmp, 2); prm->h = tmp[0];
268 sample_zeropeppered_probvector(r, tmp, 2); prm->i = tmp[0];
269 sample_zeropeppered_probvector(r, tmp, 2); prm->j = tmp[0];
270 do { sample_zeropeppered_probvector(r, tmp, 2); prm->k = tmp[0]; } while (prm->k == 1.0);
271 sample_zeropeppered_probvector(r, tmp, 2); prm->l = tmp[0];
272 sample_zeropeppered_probvector(r, tmp, 2); prm->m = tmp[0];
273
274 /* make sure N,E,C move probs are nonzero, pepper otherwise */
275 do { sample_zeropeppered_probvector(r, tmp, 2); prm->n = tmp[0]; } while (prm->n == 0.0);
276 do { sample_zeropeppered_probvector(r, tmp, 2); prm->p = tmp[0]; } while (prm->p == 0.0);
277 do { sample_zeropeppered_probvector(r, tmp, 2); prm->q = tmp[0]; } while (prm->q == 0.0);
278
279 /* J can be peppered */
280 sample_zeropeppered_probvector(r, tmp, 2); prm->r = tmp[0];
281
282 /* make sure x=A emissions for match, insert are nonzero */
283 prm->alpha = esl_rnd_UniformPositive(r);
284 prm->beta = 0.25; /* MUST match background; H3 assumes isc=0; xref J5/118 */
285 return;
286 }
287
288 /* 1.0-a-b operations below can result in -epsilon or +epsilon. Round these to 0. */
289 static float zerofy(float p) { return (p < 1e-6) ? 0.0 : p; }
290
291 static P7_HMM *
292 create_brute_hmm(ESL_ALPHABET *abc, struct p7_bruteparam_s *prm)
293 {
294 P7_HMM *hmm = NULL;
295 int M = 3;
296 int k;
297 char *logmsg = "[test created by create_brute_hmm()]";
298 if (abc->type != eslDNA) esl_fatal("brute hmm uses DNA alphabet");
299
300
301 hmm = p7_hmm_Create(M, abc);
302 hmm->t[0][p7H_MM] = prm->a;
303 hmm->t[0][p7H_MI] = prm->e;
304 hmm->t[0][p7H_MD] = zerofy(1.0 - (prm->a+prm->e));
305 hmm->t[0][p7H_IM] = prm->h;
306 hmm->t[0][p7H_II] = zerofy(1.0 - prm->h);
307 hmm->t[0][p7H_DM] = 1.0; /* D0 doesn't exist; 1.0 is a convention */
308 hmm->t[0][p7H_DD] = 0.0; /* D0 doesn't exist; 0.0 is a convention */
309 hmm->t[1][p7H_MM] = prm->b;
310 hmm->t[1][p7H_MI] = prm->f;
311 hmm->t[1][p7H_MD] = zerofy(1.0 - (prm->b+prm->f));
312 hmm->t[1][p7H_IM] = prm->i;
313 hmm->t[1][p7H_II] = zerofy(1.0 - prm->i);
314 hmm->t[1][p7H_DM] = zerofy(1.0 - prm->l);
315 hmm->t[1][p7H_DD] = prm->l;
316 hmm->t[2][p7H_MM] = prm->c;
317 hmm->t[2][p7H_MI] = prm->g;
318 hmm->t[2][p7H_MD] = zerofy(1.0 - (prm->c+prm->g));
319 hmm->t[2][p7H_IM] = prm->j;
320 hmm->t[2][p7H_II] = zerofy(1.0 - prm->j);
321 hmm->t[2][p7H_DM] = zerofy(1.0 - prm->m);
322 hmm->t[2][p7H_DD] = prm->m;
323 hmm->t[3][p7H_MM] = prm->d; /* M3->E */
324 hmm->t[3][p7H_MI] = zerofy(1.0 - prm->d);
325 hmm->t[3][p7H_MD] = 0.0; /* no D_M+1 state to move to */
326 hmm->t[3][p7H_IM] = prm->k;
327 hmm->t[3][p7H_II] = zerofy(1.0 - prm->k);
328 hmm->t[3][p7H_DM] = 1.0; /* forced transition to E */
329 hmm->t[3][p7H_DD] = 0.0;
330
331 for (k = 1; k <= M; k++) {
332 esl_vec_FSet(hmm->mat[k], abc->K, (1.0-prm->alpha)/(float)(abc->K-1));
333 hmm->mat[k][0] = prm->alpha;
334 }
335 for (k = 0; k <= M; k++) {
336 esl_vec_FSet(hmm->ins[k], abc->K, (1.0-prm->beta)/(float)(abc->K-1));
337 hmm->ins[k][0] = prm->beta;
338 }
339
340 /* Add mandatory annotation */
341 p7_hmm_SetName(hmm, "itest-brute");
342 p7_hmm_AppendComlog(hmm, 1, &logmsg);
343 hmm->nseq = 0;
344 hmm->eff_nseq = 0;
345 p7_hmm_SetCtime(hmm);
346 p7_hmm_SetConsensus(hmm, NULL);
347 hmm->checksum = 0;
348
349 return hmm;
350 }
351
352
353 static P7_PROFILE *
354 create_brute_profile(struct p7_bruteparam_s *prm, P7_HMM *hmm, P7_BG *bg, int do_local)
355 {
356 P7_PROFILE *gm = NULL;
357 double occ[4], Z;
358
359 gm = p7_profile_Create(hmm->M, hmm->abc);
360
361 if (do_local) p7_ProfileConfig(hmm, bg, gm, 100, p7_UNILOCAL); /* only local vs. glocal matters... */
362 else p7_ProfileConfig(hmm, bg, gm, 100, p7_UNIGLOCAL); /* all else will be replaced. */
363
364 if (do_local)
365 { /* local modes: uniform but weighted by match occupancy;
366 * occ[k] / \sum_i occ[i] * (M-k+1), which
367 * reduces to uniform 2/(M(M+1)) for uniform match occupancy
368 */
369 occ[1] = prm->a+prm->e;
370 occ[2] = occ[1] * (prm->b+prm->f) + zerofy(1.0-occ[1]) * zerofy(1.0-prm->l);
371 occ[3] = occ[2] * (prm->c+prm->g) + zerofy(1.0-occ[2]) * zerofy(1.0-prm->m);
372 Z = occ[1] * 3.0 + occ[2] * 2.0 + occ[3];
373 prm->begin[1] = occ[1] / Z;
374 prm->begin[2] = occ[2] / Z;
375 prm->begin[3] = occ[3] / Z;
376 prm->end = 1.0;
377 }
378 else
379 { /* glocal modes: right wing retraction and no internal exit */
380 prm->begin[1] = (prm->a + prm->e);
381 prm->begin[2] = zerofy(1. - (prm->a+prm->e)) * zerofy(1.-prm->l);
382 prm->begin[3] = zerofy(1. - (prm->a+prm->e)) * prm->l * zerofy(1.-prm->m);
383 prm->end = 0.0;
384 }
385
386 /* Replace profile's configured length and multihit modeling. */
387 gm->xsc[p7P_N][p7P_MOVE] = log(prm->n);
388 gm->xsc[p7P_N][p7P_LOOP] = log(zerofy(1. - prm->n));
389 gm->xsc[p7P_E][p7P_MOVE] = log(prm->p);
390 gm->xsc[p7P_E][p7P_LOOP] = log(zerofy(1. - prm->p));
391 gm->xsc[p7P_C][p7P_MOVE] = log(prm->q);
392 gm->xsc[p7P_C][p7P_LOOP] = log(zerofy(1. - prm->q));
393 gm->xsc[p7P_J][p7P_MOVE] = log(prm->r);
394 gm->xsc[p7P_J][p7P_LOOP] = log(zerofy(1. - prm->r));
395
396 return gm;
397 }
398
399
400 /* score_brute_profile() enumerates all paths combinatorially, and
401 * calculates their Forward or Viterbi probabilities either by summing
402 * or by max, for A* (polyA) sequences of lengths 0..4.
403 */
404 static double
405 score_brute_profile(struct p7_bruteparam_s *prm, P7_BG *bg, int do_viterbi, double sc[5])
406 {
407 double b = prm->b;
408 double c = prm->c;
409 double f = prm->f;
410 double g = prm->g;
411 double i = prm->i;
412 double j = prm->j;
413 double m = prm->m;
414 double n = prm->n;
415 double p = prm->p;
416 double q = prm->q;
417 double r = prm->r;
418 double msc = prm->alpha / (double) bg->f[0];
419 double isc = prm->beta / (double) bg->f[0];
420
421 double cp[19]; /* odds of 19 possible paths through core model */
422 double cL[5]; /* summed odds of core model accounting for seq of length 0..4 */
423 double jp[21]; /* odds of 21 possible paths using core model and J state */
424 double jL[5]; /* summed odds of core+J accounting for seq of length 0..4 */
425 double ap[10]; /* odds of 10 possible paths through flanking states, accounting for 0..3 residues */
426 double aL[4]; /* summed odds of flanks accounting for 0..3 residues */
427
428
429 /* 1. There are 19 possible paths that up to L=4 residues can align
430 to the core model.
431 */
432 cp[0] = msc * prm->begin[1] * prm->end; /* B M1 E (L=1) */
433 cp[1] = msc * prm->begin[2] * prm->end; /* B M2 E (L=1) */
434 cp[2] = msc * prm->begin[3]; /* B M3 E (L=1) */
435 cp[3] = msc * prm->begin[1] * zerofy(1. - (b+f)) * prm->end;/* B M1 D2 E (L=1) */
436 cp[4] = msc * prm->begin[2] * zerofy(1. - (c+g)); /* B M2 D3 E (L=1) */
437 cp[5] = msc * prm->begin[1] * zerofy(1. - (b+f)) * m; /* B M1 D2 D3 E (L=1) */
438
439 cp[6] = msc * msc * prm->begin[1] * b * prm->end; /* B M1 M2 E (L=2) */
440 cp[7] = msc * msc * prm->begin[2] * c; /* B M2 M3 E (L=2) */
441 cp[8] = msc * msc * prm->begin[1] * b * zerofy(1.-(c+g)); /* B M1 M2 D3 E (L=2) */
442 cp[9] = msc * msc * prm->begin[1] * zerofy(1.-(b+f)) * zerofy(1.-m); /* B M1 D2 M3 E (L=2) */
443
444 cp[10]= msc * msc * msc * prm->begin[1] * b * c; /* B M1 M2 M3 E (L=3) */
445 cp[11]= msc * isc * msc * prm->begin[1] * f * i * zerofy(1.-(c+g)); /* B M1 I1 M2 D3 E (L=3) */
446 cp[12]= msc * isc * msc * prm->begin[1] * f * i * prm->end; /* B M1 I1 M2 E (L=3) */
447 cp[13]= msc * isc * msc * prm->begin[2] * g * j; /* B M2 I2 M3 E (L=3) */
448
449 cp[14] = msc * isc * msc * msc * prm->begin[1] * f * i * c; /* B M1 I1 M2 M3 E (L=4) */
450 cp[15] = msc * isc * isc * msc * prm->begin[1] * f * zerofy(1.-i) * i * zerofy(1.-(c+g)); /* B M1 I1 I1 M2 D3 E (L=4) */
451 cp[16] = msc * msc * isc * msc * prm->begin[1] * b * g * j; /* B M1 M2 I2 M3 E (L=4) */
452 cp[17] = msc * isc * isc * msc * prm->begin[1] * f * zerofy(1.-i) * i * prm->end; /* B M1 I1 I1 M2 E (L=4) */
453 cp[18] = msc * isc * isc * msc * prm->begin[2] * g * zerofy(1.-j) * j; /* B M2 I2 I2 M3 E (L=4) */
454
455 /* 2. Sum or max the total probability of L={1..4} aligned to one pass
456 through the core model
457 */
458 if (do_viterbi)
459 {
460 cL[0] = 0.0;
461 cL[1] = esl_vec_DMax(cp, 6);
462 cL[2] = esl_vec_DMax(cp+6, 4);
463 cL[3] = esl_vec_DMax(cp+10, 4);
464 cL[4] = esl_vec_DMax(cp+14, 5);
465 }
466 else
467 {
468 cL[0] = 0.0;
469 cL[1] = esl_vec_DSum(cp, 6);
470 cL[2] = esl_vec_DSum(cp+6, 4);
471 cL[3] = esl_vec_DSum(cp+10, 4);
472 cL[4] = esl_vec_DSum(cp+14, 5);
473 }
474
475 /* 3. J state introduces a combiexplosion of paths accounting for
476 * jL={1..4} total residues in one or more passes through the
477 * core model: 21 such paths.
478 */
479 jp[0] = cL[4]; /* B [4] E (jL=4, 0 in J) */
480 jp[1] = cL[3] * zerofy(1.-p) * r * cL[1]; /* B [3] J [1] E (jL=4, 0 in J) */
481 jp[2] = cL[2] * zerofy(1.-p) * r * cL[2]; /* B [2] J [2] E (jL=4, 0 in J) */
482 jp[3] = cL[2] * zerofy(1.-p) * r * cL[1] * zerofy(1.-p) * r * cL[1]; /* B [2] J [1] J [1] E (jL=4, 0 in J) */
483 jp[4] = cL[1] * zerofy(1.-p) * r * cL[3]; /* B [1] J [3] E (jL=4, 0 in J) */
484 jp[5] = cL[1] * zerofy(1.-p) * r * cL[2] * zerofy(1.-p) * r * cL[1]; /* B [1] J [2] J [1] E (jL=4, 0 in J) */
485 jp[6] = cL[1] * zerofy(1.-p) * r * cL[1] * zerofy(1.-p) * r * cL[2]; /* B [1] J [1] J [2] E (jL=4, 0 in J) */
486 jp[7] = cL[1] * zerofy(1.-p) * r * cL[1] * zerofy(1.-p) * r * cL[1] * zerofy(1.-p) * r * cL[1]; /* B [1] J [1] J [1] J [1] E (jL=4, 0 in J) */
487 jp[8] = cL[2] * zerofy(1.-p) * zerofy(1.-r) * r * cL[1]; /* B [2] JJ [1] E (jL=4, 1 in J) */
488 jp[9] = cL[1] * zerofy(1.-p) * zerofy(1.-r) * r * cL[2]; /* B [1] JJ [2] E (jL=4, 1 in J) */
489 jp[10] = cL[1] * zerofy(1.-p) * zerofy(1.-r) * r * cL[1] * zerofy(1.-p) * r * cL[1]; /* B [1] JJ [1] J [1] E (jL=4, 1 in J) */
490 jp[11] = cL[1] * zerofy(1.-p) * r * cL[1] * zerofy(1.-p) * zerofy(1.-r) * r * cL[1]; /* B [1] J [1] JJ [1] E (jL=4, 1 in J) */
491 jp[12] = cL[1] * zerofy(1.-p) * zerofy(1.-r) * zerofy(1.-r) * r * cL[1]; /* B [1] JJJ [1] E (jL=4, 2 in J) */
492
493 jp[13] = cL[3]; /* B [3] E (jL=3, 0 in J) */
494 jp[14] = cL[2] * zerofy(1.-p) * r * cL[1]; /* B [2] J [1] E (jL=3, 0 in J) */
495 jp[15] = cL[1] * zerofy(1.-p) * r * cL[2]; /* B [1] J [2] E (jL=3, 0 in J) */
496 jp[16] = cL[1] * zerofy(1.-p) * r * cL[1] * zerofy(1.-p) * r * cL[1]; /* B [1] J [1] J [1] E (jL=3, 0 in J) */
497 jp[17] = cL[1] * zerofy(1.-p) * zerofy(1.-r) * r * cL[1]; /* B [1] JJ [1] E (jL=3, 1 in J) */
498
499 jp[18] = cL[2]; /* B [2] E (jL=2, 0 in J) */
500 jp[19] = cL[1] * zerofy(1.-p) * r * cL[1]; /* B [1] J [1] E (jL=2, 0 in J) */
501
502 jp[20] = cL[1]; /* B [1] E (jL=1, 0 in J) */
503
504 /* 4. Sum or max the total path probability of jL={1..4}
505 */
506 if (do_viterbi)
507 {
508 jL[0] = 0.;
509 jL[1] = jp[20];
510 jL[2] = esl_vec_DMax(jp + 18, 2);
511 jL[3] = esl_vec_DMax(jp + 13, 5);
512 jL[4] = esl_vec_DMax(jp, 13);
513 }
514 else
515 {
516 jL[0] = 0.;
517 jL[1] = jp[20];
518 jL[2] = esl_vec_DSum(jp + 18, 2);
519 jL[3] = esl_vec_DSum(jp + 13, 5);
520 jL[4] = esl_vec_DSum(jp, 13);
521 }
522
523 /* 5. The total probability, including SNB...ECJ flanks, accounts for
524 * 10 possible paths accounting for 0..3 residues in the flanks.
525 */
526 ap[0] = n * p * q;
527 ap[1] = zerofy(1.-n) * n * p * q;
528 ap[2] = n * p * zerofy(1.-q) * q;
529 ap[3] = zerofy(1.-n) * zerofy(1.-n) * n * p * q;
530 ap[4] = zerofy(1.-n) * n * p * zerofy(1.-q) * q;
531 ap[5] = n * p * zerofy(1.-q) * zerofy(1.-q) * q;
532 ap[6] = zerofy(1.-n) * zerofy(1.-n) * zerofy(1.-n) * n * p * q;
533 ap[7] = zerofy(1.-n) * zerofy(1.-n) * n * p * zerofy(1.-q) * q;
534 ap[8] = zerofy(1.-n) * n * p * zerofy(1.-q) * zerofy(1.-q) * q;
535 ap[9] = n * p * zerofy(1.-q) * zerofy(1.-q) * zerofy(1.-q) * q;
536
537 /* 6. Sum or max the total path probability for the flanks generating
538 * 0..3 residues
539 */
540 if (do_viterbi)
541 {
542 aL[0] = ap[0];
543 aL[1] = esl_vec_DMax(ap+1, 2);
544 aL[2] = esl_vec_DMax(ap+3, 3);
545 aL[3] = esl_vec_DMax(ap+6, 4);
546 }
547 else
548 {
549 aL[0] = ap[0];
550 aL[1] = esl_vec_DSum(ap+1, 2);
551 aL[2] = esl_vec_DSum(ap+3, 3);
552 aL[3] = esl_vec_DSum(ap+6, 4);
553 }
554
555 /* 6. The total lod score is then the possible combinations
556 * of flank + (core+J)
557 */
558 if (do_viterbi)
559 {
560 sc[0] = -eslINFINITY;
561 sc[1] = jL[1] * aL[0];
562 sc[2] = ESL_MAX(jL[2] * aL[0],
563 jL[1] * aL[1]);
564
565 sc[3] = ESL_MAX(ESL_MAX(jL[3] * aL[0],
566 jL[2] * aL[1]),
567 jL[1] * aL[2]);
568 sc[4] = ESL_MAX(ESL_MAX(jL[4] * aL[0], jL[3] * aL[1]),
569 ESL_MAX(jL[2] * aL[2], jL[1] * aL[3]));
570 sc[1] = log(sc[1]);
571 sc[2] = log(sc[2]);
572 sc[3] = log(sc[3]);
573 sc[4] = log(sc[4]);
574 }
575 else
576 {
577 sc[0] = -eslINFINITY;
578 sc[1] = log(jL[1] * aL[0]);
579 sc[2] = log(jL[2] * aL[0] + jL[1] * aL[1]);
580 sc[3] = log(jL[3] * aL[0] + jL[2] * aL[1] + jL[1] * aL[2]);
581 sc[4] = log(jL[4] * aL[0] + jL[3] * aL[1] + jL[2] * aL[2] + jL[1] * aL[3]);
582 }
583
584 return eslOK;
585 }
586
587
588
589
590
591