Debian Codegraph @

hmmer/src/generic_fwdback_rescaled.c

raw symbols SCIP
1 /*
2 * gcc -o generic_fwdback_rescaled_example -mssse3 -I. -I../easel -L../easel -L. generic_fwdback_rescaled.c -lhmmer -leasel
3 */
4
5 #include <p7_config.h>
6
7 #include <string.h>
8
9 #include "easel.h"
10 #include "esl_alphabet.h"
11 #include "esl_getopts.h"
12 #include "esl_sq.h"
13 #include "esl_sqio.h"
14 #include "esl_vectorops.h"
15
16 #include "hmmer.h"
17
18 extern int p7_GForwardOdds(const ESL_DSQ *dsq, int L, const P7_PROFILE *gm, P7_GMX *gx, float *opt_sc);
19 extern int p7_profile_CopyInfoFromHMM(P7_PROFILE *gm, const P7_HMM *hmm);
20 extern int p7_profile_ReconfigLengthInOdds(P7_PROFILE *gm, int L);
21 extern int p7_profile_ConfigInOdds (const P7_HMM *hmm, const P7_BG *bg, P7_PROFILE *gm, int L, int mode);
22 extern int p7_profile_ConfigInOdds_DDScaled(const P7_HMM *hmm, const P7_BG *bg, P7_PROFILE *gm, int L, float *ret_ddscale);
23 extern float p7_gmx_Max(P7_GMX *gx);
24 extern float p7_gmx_Min(P7_GMX *gx);
25
26
27 #define STYLES "--dd,--fs,--sw,--ls,--s" /* Exclusive choice for alignment mode */
28
29 static ESL_OPTIONS options[] = {
30 /* name type default env range toggles reqs incomp help docgroup*/
31 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
32 { "--dd", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "DD-scaled unihit glocal alignment", 0 },
33 { "--fs", eslARG_NONE,"default",NULL, NULL, STYLES, NULL, NULL, "multihit local alignment", 0 },
34 { "--sw", eslARG_NONE, FALSE, NULL, NULL, STYLES, NULL, NULL, "unihit local alignment", 0 },
35 { "--ls", eslARG_NONE, FALSE, NULL, NULL, STYLES, NULL, NULL, "multihit glocal alignment", 0 },
36 { "--s", eslARG_NONE, FALSE, NULL, NULL, STYLES, NULL, NULL, "unihit glocal alignment", 0 },
37 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
38 };
39 static char usage[] = "[-options] <hmmfile> <seqfile>";
40 static char banner[] = "testbed for Farrar DD-scaled Forward, generic implementation";
41
42 int
43 main(int argc, char **argv)
44 {
45 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 2, argc, argv, banner, usage);
46 char *hmmfile = esl_opt_GetArg(go, 1);
47 char *seqfile = esl_opt_GetArg(go, 2);
48 ESL_ALPHABET *abc = NULL;
49 P7_HMMFILE *hfp = NULL;
50 P7_HMM *hmm = NULL;
51 P7_BG *bg = NULL;
52 P7_PROFILE *gm = NULL;
53 P7_PROFILE *gmref = NULL;
54 P7_GMX *fwd = NULL;
55 P7_GMX *fwdref = NULL;
56 ESL_SQ *sq = NULL;
57 ESL_SQFILE *sqfp = NULL;
58 int format = eslSQFILE_UNKNOWN;
59 float fsc, fscref;
60 float nullsc;
61 float ddscale = 0.0;
62 float min, max;
63 int status;
64
65 printf("%-30s %-30s %-10s %-10s\n", "# profile name", "# seq name", "fwd (test)", "fwd (ref) ");
66 printf("%-30s %-30s %10s %10s\n", "#--------------", "#--------------", "----------", "----------");
67
68
69 /* Open the HMM file */
70 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
71
72 while ( (status = p7_hmmfile_Read(hfp, &abc, &hmm)) != eslEOF)
73 {
74 if (status == eslEOD) p7_Fail("read failed, HMM file %s may be truncated?", hmmfile);
75 else if (status == eslEFORMAT) p7_Fail("bad file format in HMM file %s", hmmfile);
76 else if (status == eslEINCOMPAT) p7_Fail("HMM file %s contains different alphabets", hmmfile);
77 else if (status != eslOK) p7_Fail("Unexpected error in reading HMMs from %s", hmmfile);
78
79 /* Open the sqfile */
80 sq = esl_sq_CreateDigital(abc);
81 status = esl_sqfile_Open(seqfile, format, NULL, &sqfp);
82 if (status == eslENOTFOUND) p7_Fail("No such file.");
83 else if (status == eslEFORMAT) p7_Fail("Format unrecognized.");
84 else if (status == eslEINVAL) p7_Fail("Can't autodetect stdin or .gz.");
85 else if (status != eslOK) p7_Fail("Open failed, code %d.", status);
86
87 bg = p7_bg_Create(abc);
88
89 /* Config the test model however we were asked to */
90 gm = p7_profile_Create(hmm->M, abc);
91 if (esl_opt_GetBoolean(go, "--dd")) status = p7_profile_ConfigInOdds_DDScaled(hmm, bg, gm, sq->n, &ddscale);
92 else if (esl_opt_GetBoolean(go, "--fs")) status = p7_profile_ConfigInOdds (hmm, bg, gm, sq->n, p7_LOCAL);
93 else if (esl_opt_GetBoolean(go, "--sw")) status = p7_profile_ConfigInOdds (hmm, bg, gm, sq->n, p7_UNILOCAL);
94 else if (esl_opt_GetBoolean(go, "--ls")) status = p7_profile_ConfigInOdds (hmm, bg, gm, sq->n, p7_GLOCAL);
95 else if (esl_opt_GetBoolean(go, "--s")) status = p7_profile_ConfigInOdds (hmm, bg, gm, sq->n, p7_UNIGLOCAL);
96 if (status == eslERANGE) printf("# MODEL ENTRY UNDERFLOW: %s\n", hmm->name);
97
98 /* Config the reference model the same way */
99 gmref = p7_profile_Create(hmm->M, abc);
100 if (esl_opt_GetBoolean(go, "--dd")) status = p7_ProfileConfig(hmm, bg, gmref, sq->n, p7_UNIGLOCAL);
101 else if (esl_opt_GetBoolean(go, "--fs")) status = p7_ProfileConfig(hmm, bg, gmref, sq->n, p7_LOCAL);
102 else if (esl_opt_GetBoolean(go, "--sw")) status = p7_ProfileConfig(hmm, bg, gmref, sq->n, p7_UNILOCAL);
103 else if (esl_opt_GetBoolean(go, "--ls")) status = p7_ProfileConfig(hmm, bg, gmref, sq->n, p7_GLOCAL);
104 else if (esl_opt_GetBoolean(go, "--s")) status = p7_ProfileConfig(hmm, bg, gmref, sq->n, p7_UNIGLOCAL);
105
106 fwd = p7_gmx_Create(gm->M, 400);
107 fwdref = p7_gmx_Create(gmref->M, 400);
108
109 while ( (status = esl_sqio_Read(sqfp, sq)) != eslEOF)
110 {
111 if (status == eslEFORMAT) p7_Fail("Parse failed (sequence file %s)\n%s\n", sqfp->filename, sqfp->get_error(sqfp));
112 else if (status != eslOK) p7_Fail("Unexpected error %d reading sequence file %s", status, sqfp->filename);
113
114 /* Resize DP matrix if necessary */
115 p7_gmx_GrowTo(fwd, gm->M, sq->n);
116 p7_gmx_GrowTo(fwdref, gmref->M, sq->n);
117
118 /* Set the profile and null model's target length models */
119 p7_bg_SetLength(bg, sq->n);
120 p7_profile_ReconfigLengthInOdds(gm, sq->n);
121 p7_ReconfigLength (gmref, sq->n);
122
123 /* Run Forward test version*/
124 p7_GForwardOdds (sq->dsq, sq->n, gm, fwd, &fsc);
125 fsc += ddscale;
126
127 /* Run Forward reference version */
128 p7_GForward( sq->dsq, sq->n, gmref, fwdref, &fscref);
129
130 //p7_gmx_Dump(stdout, fwd, p7_SHOW_LOG);
131
132 /* Those scores are partial log-odds likelihoods in nats.
133 * Subtract off the rest of the null model, convert to bits.
134 */
135 p7_bg_NullOne(bg, sq->dsq, sq->n, &nullsc);
136
137 min = p7_gmx_Min(fwd);
138 max = p7_gmx_Max(fwd);
139 fsc = (fsc - nullsc) / eslCONST_LOG2;
140 fscref = (fscref - nullsc) / eslCONST_LOG2;
141 printf("%-30s %-30s %10.4f %10.4f %10.4g %10.4g %4s %9s %9s\n",
142 gm->name, sq->name, fsc, fscref,
143 min, max,
144 (fabs(fsc - fscref) >= 0.5 ? "FAIL" : "pass"),
145 (min == 0.0 ? "UNDERFLOW" : "ok"),
146 ((max > 1e37 || isinf(max))? "OVERFLOW" : "ok"));
147
148
149 p7_gmx_Reuse(fwd);
150 p7_gmx_Reuse(fwdref);
151 esl_sq_Reuse(sq);
152 }
153
154 p7_gmx_Destroy(fwd);
155 p7_gmx_Destroy(fwdref);
156 p7_profile_Destroy(gm);
157 p7_profile_Destroy(gmref);
158 p7_hmm_Destroy(hmm);
159 p7_bg_Destroy(bg);
160 esl_sq_Destroy(sq);
161 esl_sqfile_Close(sqfp);
162 }
163
164 /* Cleanup */
165 p7_hmmfile_Close(hfp);
166 esl_alphabet_Destroy(abc);
167 esl_getopts_Destroy(go);
168 return 0;
169 }
170
171 int
172 p7_GForwardOdds(const ESL_DSQ *dsq, int L, const P7_PROFILE *gm, P7_GMX *gx, float *opt_sc)
173 {
174 float const *tsc = gm->tsc;
175 float **dp = gx->dp;
176 float *xmx = gx->xmx;
177 int M = gm->M;
178 int i,k;
179 float esc = p7_profile_IsLocal(gm) ? 1.0f : 0.0f;
180 float totscale = 0.0f;
181 float rescale;
182
183 /* Initialization of the zero row */
184 XMX(0,p7G_N) = 1.0f; /* S->N, p=1 */
185 XMX(0,p7G_B) = gm->xsc[p7P_N][p7P_MOVE]; /* S->N->B, 1*t_NB */
186 XMX(0,p7G_E) = XMX(0,p7G_C) = XMX(0,p7G_J) = 0.0f; /* need seq to get here */
187 for (k = 0; k <= M; k++)
188 MMX(0,k) = IMX(0,k) = DMX(0,k) = 0.0f;
189
190 /* Recursion
191 * boundary conditions: tsc[0][] = impossible for all eight transitions (no node 0)
192 * D_1 wastefully calculated (doesn't exist)
193 */
194 for (i = 1; i <= L; i++)
195 {
196 float const *rsc = gm->rsc[dsq[i]];
197 float sc;
198
199 MMX(i,0) = IMX(i,0) = DMX(i,0) = 0.0f;
200 XMX(i, p7G_E) = 0.0f;
201
202 for (k = 1; k < M; k++)
203 {
204 /* match state */
205 sc = MMX(i-1,k-1) * TSC(p7P_MM,k-1)
206 + IMX(i-1,k-1) * TSC(p7P_IM,k-1)
207 + XMX(i-1,p7G_B) * TSC(p7P_BM,k-1)
208 + DMX(i-1,k-1) * TSC(p7P_DM,k-1);
209 MMX(i,k) = sc * MSC(k);
210
211 /* insert state */
212 sc = MMX(i-1,k) * TSC(p7P_MI,k)
213 + IMX(i-1,k) * TSC(p7P_II,k);
214 IMX(i,k) = sc * ISC(k);
215
216 /* delete state */
217 DMX(i,k) = MMX(i,k-1) * TSC(p7P_MD,k-1)
218 + DMX(i,k-1) * TSC(p7P_DD,k-1);
219
220 /* E state update */
221 XMX(i,p7G_E) += MMX(i,k) * esc
222 + DMX(i,k) * esc;
223 }
224 /* unrolled match state M_M */
225 sc = MMX(i-1,M-1) * TSC(p7P_MM,M-1)
226 + IMX(i-1,M-1) * TSC(p7P_IM,M-1)
227 + XMX(i-1,p7G_B) * TSC(p7P_BM,M-1)
228 + DMX(i-1,M-1) * TSC(p7P_DM,M-1);
229 MMX(i,M) = sc * MSC(M);
230 IMX(i,M) = 0.0f;
231
232 /* unrolled delete state D_M */
233 DMX(i,M) = MMX(i,M-1) * TSC(p7P_MD,M-1)
234 + DMX(i,M-1) * TSC(p7P_DD,M-1);
235
236 /* unrolled E state update */
237 XMX(i,p7G_E) += MMX(i,M) + DMX(i,M);
238
239 /* J state */
240 XMX(i,p7G_J) = XMX(i-1,p7G_J) * gm->xsc[p7P_J][p7P_LOOP]
241 + XMX(i, p7G_E) * gm->xsc[p7P_E][p7P_LOOP];
242 /* C state */
243 XMX(i,p7G_C) = XMX(i-1,p7G_C) * gm->xsc[p7P_C][p7P_LOOP]
244 + XMX(i, p7G_E) * gm->xsc[p7P_E][p7P_MOVE];
245 /* N state */
246 XMX(i,p7G_N) = XMX(i-1,p7G_N) * gm->xsc[p7P_N][p7P_LOOP];
247
248 /* B state */
249 XMX(i,p7G_B) = XMX(i, p7G_N) * gm->xsc[p7P_N][p7P_MOVE]
250 + XMX(i, p7G_J) * gm->xsc[p7P_J][p7P_MOVE];
251
252 /* sparse rescaling */
253 if (XMX(i,p7G_E) > 1.0e4)
254 {
255 rescale = 1.0 / XMX(i,p7G_E);
256 totscale += log(XMX(i,p7G_E));
257
258 XMX(i,p7G_N) *= rescale;
259 XMX(i,p7G_B) *= rescale;
260 XMX(i,p7G_E) = 1.0;
261 XMX(i,p7G_J) *= rescale;
262 XMX(i,p7G_C) *= rescale;
263 for (k = 1; k <= M; k++)
264 {
265 MMX(i,k) *= rescale;
266 DMX(i,k) *= rescale;
267 IMX(i,k) *= rescale;
268 }
269 }
270 }
271
272 if (opt_sc != NULL) *opt_sc = log(XMX(L,p7G_C) * gm->xsc[p7P_C][p7P_MOVE]) + totscale;
273 gx->M = M;
274 gx->L = L;
275 return eslOK;
276 }
277
278
279 /* this is a copy of a block of modelconfig.c::p7_ProfileConfig()
280 * ...and could replace it
281 */
282 int
283 p7_profile_CopyInfoFromHMM(P7_PROFILE *gm, const P7_HMM *hmm)
284 {
285 int z;
286 int status;
287
288 /* Contract checks */
289 if (gm->abc->type != hmm->abc->type) ESL_XEXCEPTION(eslEINVAL, "HMM and profile alphabet don't match");
290 if (hmm->M > gm->allocM) ESL_XEXCEPTION(eslEINVAL, "profile too small to hold HMM");
291 if (! (hmm->flags & p7H_CONS)) ESL_XEXCEPTION(eslEINVAL, "HMM must have a consensus to transfer to the profile");
292
293 /* Copy some pointer references and other info across from HMM */
294 gm->M = hmm->M;
295 gm->max_length = hmm->max_length;
296 gm->mode = p7_NO_MODE;
297 gm->roff = -1;
298 gm->eoff = -1;
299 gm->offs[p7_MOFFSET] = -1;
300 gm->offs[p7_FOFFSET] = -1;
301 gm->offs[p7_POFFSET] = -1;
302 if (gm->name != NULL) free(gm->name);
303 if (gm->acc != NULL) free(gm->acc);
304 if (gm->desc != NULL) free(gm->desc);
305 if ((status = esl_strdup(hmm->name, -1, &(gm->name))) != eslOK) goto ERROR;
306 if ((status = esl_strdup(hmm->acc, -1, &(gm->acc))) != eslOK) goto ERROR;
307 if ((status = esl_strdup(hmm->desc, -1, &(gm->desc))) != eslOK) goto ERROR;
308 if (hmm->flags & p7H_RF) strcpy(gm->rf, hmm->rf);
309 if (hmm->flags & p7H_MMASK) strcpy(gm->mm, hmm->mm);
310 if (hmm->flags & p7H_CONS) strcpy(gm->consensus, hmm->consensus); /* must be present, actually, so the flag test is just for symmetry w/ other optional HMM fields */
311 if (hmm->flags & p7H_CS) strcpy(gm->cs, hmm->cs);
312 for (z = 0; z < p7_NEVPARAM; z++) gm->evparam[z] = hmm->evparam[z];
313 for (z = 0; z < p7_NCUTOFFS; z++) gm->cutoff[z] = hmm->cutoff[z];
314 for (z = 0; z < p7_MAXABET; z++) gm->compo[z] = hmm->compo[z];
315
316 return eslOK;
317
318 ERROR:
319 return status;
320 }
321
322
323 int
324 p7_profile_ReconfigLengthInOdds(P7_PROFILE *gm, int L)
325 {
326 float ploop, pmove;
327
328 /* Configure N,J,C transitions so they bear L/(2+nj) of the total
329 * unannotated sequence length L.
330 */
331 pmove = (2.0f + gm->nj) / ((float) L + 2.0f + gm->nj); /* 2/(L+2) for sw; 3/(L+3) for fs */
332 ploop = 1.0f - pmove;
333 gm->xsc[p7P_N][p7P_LOOP] = gm->xsc[p7P_C][p7P_LOOP] = gm->xsc[p7P_J][p7P_LOOP] = ploop;
334 gm->xsc[p7P_N][p7P_MOVE] = gm->xsc[p7P_C][p7P_MOVE] = gm->xsc[p7P_J][p7P_MOVE] = pmove;
335 gm->L = L;
336 return eslOK;
337 }
338
339 /* like p7_ProfileConfig(), but in odds ratios rather than log.
340 * This is the "normal" odds ratio version.
341 * See _DDScaled for an experimental version for glocal/global.
342 *
343 * In glocal, because we left-wing-retract the BMk transition,
344 * that transition can underflow. If it does, return <eslERANGE>.
345 */
346 int
347 p7_profile_ConfigInOdds(const P7_HMM *hmm, const P7_BG *bg, P7_PROFILE *gm, int L, int mode)
348 {
349 int k,x;
350 float *occ = NULL;
351 float *tp, *rp;
352 float sc[p7_MAXCODE];
353 float Z;
354 int status;
355 int did_underflow = FALSE;
356
357 if ( (status = p7_profile_CopyInfoFromHMM(gm, hmm)) != eslOK) return status;
358 gm->mode = mode;
359
360 /* Some initializations assumed log space. Change them to probspace. */
361 esl_vec_FSet(gm->tsc, p7P_NTRANS, 0.0f); /* node 0 nonexistent, has no transitions */
362 if (gm->M > 1) {
363 p7P_TSC(gm, 1, p7P_DM) = 0.0f; /* delete state D_1 is wing-retracted */
364 p7P_TSC(gm, 1, p7P_DD) = 0.0f;
365 }
366 for (x = 0; x < gm->abc->Kp; x++) {
367 p7P_MSC(gm, 0, x) = 0.0f; /* no emissions from nonexistent M_0... */
368 p7P_ISC(gm, 0, x) = 0.0f; /* or I_0... */
369 /* I_M is initialized in profile config, when we know actual M, not just allocated max M */
370 }
371 x = esl_abc_XGetGap(gm->abc); /* no emission can emit/score gap characters */
372 esl_vec_FSet(gm->rsc[x], (gm->M+1)*p7P_NR, 0.0f);
373 x = esl_abc_XGetMissing(gm->abc); /* no emission can emit/score missing data characters */
374 esl_vec_FSet(gm->rsc[x], (gm->M+1)*p7P_NR, 0.0f);
375
376
377 /* Entry scores. Recall k-1,k off-by-one storage issue here.
378 * p7P_TSC(gm, k-1, p7P_BM) is the t_BMk transition
379 */
380 if (p7_profile_IsLocal(gm))
381 {
382 /* Local mode entry: occ[k] /( \sum_i occ[i] * (M-i+1))
383 * (Reduces to uniform 2/(M(M+1)) for occupancies of 1.0) */
384 Z = 0.;
385 ESL_ALLOC(occ, sizeof(float) * (hmm->M+1));
386
387 if ((status = p7_hmm_CalculateOccupancy(hmm, occ, NULL)) != eslOK) goto ERROR;
388 for (k = 1; k <= hmm->M; k++)
389 Z += occ[k] * (float) (hmm->M-k+1);
390 for (k = 1; k <= hmm->M; k++)
391 p7P_TSC(gm, k-1, p7P_BM) = occ[k] / Z; /* note off-by-one: entry at Mk stored as [k-1][BM] */
392 free(occ);
393 }
394 else /* glocal modes: left wing retraction. Check for underflow. */
395 {
396 Z = hmm->t[0][p7H_MD];
397 p7P_TSC(gm, 0, p7P_BM) = 1.0 - Z;
398 for (k = 1; k < hmm->M; k++)
399 {
400 p7P_TSC(gm, k, p7P_BM) = Z * hmm->t[k][p7H_DM];
401 Z *= hmm->t[k][p7H_DD];
402 }
403 if (p7P_TSC(gm, hmm->M-1, p7P_BM) == 0.0f) did_underflow = TRUE;
404 }
405
406 /* E state loop/move probabilities: nonzero for MOVE allows loops/multihits
407 * N,C,J transitions are set later by target length model config
408 */
409 if (p7_profile_IsMultihit(gm)) {
410 gm->xsc[p7P_E][p7P_MOVE] = 0.5f;
411 gm->xsc[p7P_E][p7P_LOOP] = 0.5f;
412 gm->nj = 1.0f;
413 } else {
414 gm->xsc[p7P_E][p7P_MOVE] = 1.0f;
415 gm->xsc[p7P_E][p7P_LOOP] = 0.0f;
416 gm->nj = 0.0f;
417 }
418
419 /* main profile transition scores */
420 for (k = 1; k < gm->M; k++)
421 {
422 tp = gm->tsc + k * p7P_NTRANS;
423 tp[p7P_MM] = hmm->t[k][p7H_MM];
424 tp[p7P_MI] = hmm->t[k][p7H_MI];
425 tp[p7P_MD] = hmm->t[k][p7H_MD];
426 tp[p7P_IM] = hmm->t[k][p7H_IM];
427 tp[p7P_II] = hmm->t[k][p7H_II];
428 tp[p7P_DM] = hmm->t[k][p7H_DM];
429 tp[p7P_DD] = hmm->t[k][p7H_DD];
430 }
431
432 /* match residue scores */
433 /* we still compute this in log space, then exp() it back,
434 * because degenerate residue scores are avg log odds.
435 */
436 sc[hmm->abc->K] = -eslINFINITY; /* gap character */
437 sc[hmm->abc->Kp-2] = -eslINFINITY; /* nonresidue character */
438 sc[hmm->abc->Kp-1] = -eslINFINITY; /* missing data character */
439 for (k = 1; k <= hmm->M; k++) {
440 for (x = 0; x < hmm->abc->K; x++)
441 sc[x] = log(hmm->mat[k][x] / bg->f[x]);
442 esl_abc_FExpectScVec(hmm->abc, sc, bg->f);
443 for (x = 0; x < hmm->abc->Kp; x++) {
444 rp = gm->rsc[x] + k * p7P_NR;
445 rp[p7P_MSC] = exp(sc[x]);
446 }
447 }
448
449 /* insert residue scores, hardwired to odds ratio of 1.0 */
450 for (x = 0; x < gm->abc->Kp; x++)
451 {
452 for (k = 1; k < hmm->M; k++) p7P_ISC(gm, k, x) = 1.0f;
453 p7P_ISC(gm, hmm->M, x) = 0.0f; /* init I_M to impossible. */
454 }
455 for (k = 1; k <= hmm->M; k++) p7P_ISC(gm, k, gm->abc->K) = 0.0f; /* gap symbol */
456 for (k = 1; k <= hmm->M; k++) p7P_ISC(gm, k, gm->abc->Kp-2) = 0.0f; /* nonresidue symbol */
457 for (k = 1; k <= hmm->M; k++) p7P_ISC(gm, k, gm->abc->Kp-1) = 0.0f; /* missing data symbol */
458
459
460 /* Remaining specials, [NCJ][MOVE | LOOP] are set by ReconfigLengthInOdds() */
461 gm->L = 0; /* force ReconfigLengthInOdds to reconfig */
462 if ((status = p7_profile_ReconfigLengthInOdds(gm, L)) != eslOK) goto ERROR;
463
464 return (did_underflow ? eslERANGE : eslOK);
465
466 ERROR:
467 if (occ) free(occ);
468 return status;
469 }
470
471
472 /* Experimental version that scales by DD transitions
473 * Model must be unihit glocal (p7_UNILOCAL)
474 */
475 int
476 p7_profile_ConfigInOdds_DDScaled(const P7_HMM *hmm, const P7_BG *bg, P7_PROFILE *gm, int L, float *ret_ddscale)
477 {
478 int k,x;
479 float *occ = NULL;
480 float ddscale;
481 float *tp, *rp;
482 float sc[p7_MAXCODE];
483 int status;
484
485 if ( (status = p7_profile_CopyInfoFromHMM(gm, hmm)) != eslOK) return status;
486 gm->mode = p7_UNIGLOCAL;
487
488 /* Some initializations assumed log space.
489 * Change them
490 */
491 esl_vec_FSet(gm->tsc, p7P_NTRANS, 0.0f); /* node 0 nonexistent, has no transitions */
492 if (gm->M > 1) {
493 p7P_TSC(gm, 1, p7P_DM) = 0.0f; /* delete state D_1 is wing-retracted */
494 p7P_TSC(gm, 1, p7P_DD) = 0.0f;
495 }
496 for (x = 0; x < gm->abc->Kp; x++) {
497 p7P_MSC(gm, 0, x) = 0.0f; /* no emissions from nonexistent M_0... */
498 p7P_ISC(gm, 0, x) = 0.0f; /* or I_0... */
499 /* I_M is initialized in profile config, when we know actual M, not just allocated max M */
500 }
501 x = esl_abc_XGetGap(gm->abc); /* no emission can emit/score gap characters */
502 esl_vec_FSet(gm->rsc[x], (gm->M+1)*p7P_NR, 0.0f);
503 x = esl_abc_XGetMissing(gm->abc); /* no emission can emit/score missing data characters */
504 esl_vec_FSet(gm->rsc[x], (gm->M+1)*p7P_NR, 0.0f);
505
506
507 /* glocal mode BMk entries, DD-scaled */
508 p7P_TSC(gm, 0, p7P_BM) = 1.0 - hmm->t[0][p7H_MD];
509 for (k = 1; k < hmm->M; k++)
510 p7P_TSC(gm, k, p7P_BM) = hmm->t[0][p7H_MD] * hmm->t[k][p7H_DM] / hmm->t[k][p7H_DD];
511
512 /* unihit E state loop/move probabilities */
513 gm->xsc[p7P_E][p7P_MOVE] = 1.0f;
514 gm->xsc[p7P_E][p7P_LOOP] = 0.0f;
515 gm->nj = 0.0f;
516
517 /* main profile transition scores, DD-scaled */
518 for (k = 1; k < gm->M; k++)
519 {
520 tp = gm->tsc + k * p7P_NTRANS;
521 ddscale = 1.0f/hmm->t[k][p7H_DD];
522
523 tp[p7P_MM] = hmm->t[k][p7H_MM] * ddscale;
524 tp[p7P_MI] = hmm->t[k][p7H_MI];
525 tp[p7P_MD] = hmm->t[k][p7H_MD] * ddscale;
526 tp[p7P_IM] = hmm->t[k][p7H_IM] * ddscale;
527 tp[p7P_II] = hmm->t[k][p7H_II];
528 tp[p7P_DM] = hmm->t[k][p7H_DM] * ddscale;
529 tp[p7P_DD] = 1.0;
530 }
531
532 /* compute log(\prod_k=1..M-1 t_k(DD), total rescale factor */
533 ddscale = 0.0f;
534 if (! p7_profile_IsLocal(gm)) /* glocal mode: nonzero ddscale */
535 {
536 for (k = 1; k < gm->M; k++)
537 ddscale += log(hmm->t[k][p7H_DD]);
538 }
539
540 /* match residue scores */
541 /* we still compute this in log space, then exp() it back,
542 * because degenerate residue scores are avg log odds.
543 */
544 sc[hmm->abc->K] = -eslINFINITY; /* gap character */
545 sc[hmm->abc->Kp-2] = -eslINFINITY; /* nonresidue character */
546 sc[hmm->abc->Kp-1] = -eslINFINITY; /* missing data character */
547 for (k = 1; k <= hmm->M; k++) {
548 for (x = 0; x < hmm->abc->K; x++)
549 sc[x] = log(hmm->mat[k][x] / bg->f[x]);
550 esl_abc_FExpectScVec(hmm->abc, sc, bg->f);
551 for (x = 0; x < hmm->abc->Kp; x++) {
552 rp = gm->rsc[x] + k * p7P_NR;
553 rp[p7P_MSC] = exp(sc[x]);
554 }
555 }
556
557 /* insert residue scores, hardwired to odds ratio of 1.0 */
558 for (x = 0; x < gm->abc->Kp; x++)
559 {
560 for (k = 1; k < hmm->M; k++) p7P_ISC(gm, k, x) = 1.0f;
561 p7P_ISC(gm, hmm->M, x) = 0.0f; /* init I_M to impossible. */
562 }
563 for (k = 1; k <= hmm->M; k++) p7P_ISC(gm, k, gm->abc->K) = 0.0f; /* gap symbol */
564 for (k = 1; k <= hmm->M; k++) p7P_ISC(gm, k, gm->abc->Kp-2) = 0.0f; /* nonresidue symbol */
565 for (k = 1; k <= hmm->M; k++) p7P_ISC(gm, k, gm->abc->Kp-1) = 0.0f; /* missing data symbol */
566
567
568 /* Remaining specials, [NCJ][MOVE | LOOP] are set by ReconfigLengthInOdds() */
569 gm->L = 0; /* force ReconfigLengthInOdds to reconfig */
570 if ((status = p7_profile_ReconfigLengthInOdds(gm, L)) != eslOK) goto ERROR;
571
572 *ret_ddscale = ddscale;
573 return eslOK;
574
575 ERROR:
576 if (occ) free(occ);
577 return status;
578 }
579
580
581
582
583 float
584 p7_gmx_Max(P7_GMX *gx)
585 {
586 float **dp = gx->dp;
587 float *xmx = gx->xmx;
588 float max;
589 int i,k;
590
591 /* all k=0 are invalid (-inf or 0 as boundary condition) */
592 /* all D_1 and I_M are invalid (don't exist in search profile) */
593 /* on i=0 row, only B, N are valid */
594 /* on i=1 row, all I_k are invalid */
595 /* in unihit mode, all J are invalid */
596 max = ESL_MAX( XMX(0, p7G_N), XMX(0, p7G_B));
597 for (i = 1; i <= gx->L; i++)
598 {
599 for (k = 1; k <= gx->M; k++) max = ESL_MAX( max, MMX(i,k));
600 for (k = 2; k <= gx->M; k++) max = ESL_MAX( max, DMX(i,k));
601 if (i > 1)
602 for (k = 1; k < gx->M; k++) max = ESL_MAX( max, IMX(i,k));
603
604 /* we don't have to check J; it's equal to C in multihit mode, for our current multihit parameterization of EJ=EC=0.5 */
605 max = ESL_MAX(max, XMX(i, p7G_E));
606 max = ESL_MAX(max, XMX(i, p7G_N));
607 max = ESL_MAX(max, XMX(i, p7G_B));
608 max = ESL_MAX(max, XMX(i, p7G_C));
609 }
610 return max;
611 }
612
613 float
614 p7_gmx_Min(P7_GMX *gx)
615 {
616 float **dp = gx->dp;
617 float *xmx = gx->xmx;
618 float min;
619 int i,k;
620
621 /* all k=0 are invalid (-inf or 0 as boundary condition) */
622 /* all D_1 and I_M are invalid (don't exist in search profile) */
623 /* on i=0 row, only B, N are valid */
624 /* on i=1 row, all I_k are invalid */
625 /* in unihit mode, all J are invalid */
626 min = ESL_MIN( XMX(0, p7G_N), XMX(0, p7G_B));
627 for (i = 1; i <= gx->L; i++)
628 {
629 for (k = 1; k <= gx->M; k++) min = ESL_MIN( min, MMX(i,k));
630 for (k = 2; k <= gx->M; k++) min = ESL_MIN( min, DMX(i,k));
631 if (i > 1)
632 for (k = 1; k < gx->M; k++) min = ESL_MIN( min, IMX(i,k));
633
634 /* we don't have to check J; it's equal to C in multihit mode, for our current multihit parameterization of EJ=EC=0.5 */
635 min = ESL_MIN(min, XMX(i, p7G_E));
636 min = ESL_MIN(min, XMX(i, p7G_N));
637 min = ESL_MIN(min, XMX(i, p7G_B));
638 min = ESL_MIN(min, XMX(i, p7G_C));
639 }
640 return min;
641 }
642