Debian Codegraph @

hmmer/src/generic_vtrace.c

raw symbols SCIP
1 /* Viterbi traceback; generic (non-SIMD) version.
2 *
3 * Contents:
4 * 1. Viterbi traceback
5 * 2. Example.
6 *
7 * SRE, Fri Aug 15 09:17:11 2008 [Janelia]
8 */
9 #include <p7_config.h>
10
11 #include "easel.h"
12 #include "esl_alphabet.h"
13
14 #include "hmmer.h"
15
16 /*****************************************************************
17 * 1. Viterbi traceback, generic version
18 *****************************************************************/
19
20 /* Function: p7_GTrace()
21 * Incept: SRE, Thu Feb 1 10:25:56 2007 [UA 8018 St. Louis to Dulles]
22 *
23 * Purpose: Traceback of a Viterbi matrix: retrieval
24 * of optimum alignment.
25 *
26 * This function is currently implemented as a
27 * reconstruction traceback, rather than using a shadow
28 * matrix. Because H3 uses floating point scores, and we
29 * can't compare floats for equality, we have to compare
30 * floats for near-equality and therefore, formally, we can
31 * only guarantee a near-optimal traceback. However, even in
32 * the unlikely event that a suboptimal is returned, the
33 * score difference from true optimal will be negligible.
34 *
35 * Args: dsq - digital sequence aligned to, 1..L
36 * L - length of <dsq>
37 * gm - profile
38 * mx - Viterbi matrix to trace, L x M
39 * tr - storage for the recovered traceback.
40 *
41 * Return: <eslOK> on success.
42 * <eslFAIL> if even the optimal path has zero probability;
43 * in this case, the trace is set blank (<tr->N = 0>).
44 *
45 * Note: Care is taken to evaluate the prev+tsc+emission
46 * calculations in exactly the same order that Viterbi did
47 * them, lest you get numerical problems with
48 * a+b+c = d; d-c != a+b because d,c are nearly equal.
49 * (This bug appeared in dev: xref J1/121.)
50 */
51 int
52 p7_GTrace(const ESL_DSQ *dsq, int L, const P7_PROFILE *gm, const P7_GMX *gx, P7_TRACE *tr)
53 {
54 int i = L; /* position in seq (1..L) */
55 int k = 0; /* position in model (1..M) */
56 int M = gm->M;
57 float **dp = gx->dp; /* so {MDI}MX() macros work */
58 float *xmx = gx->xmx; /* so XMX() macro works */
59 float tol = 1e-5; /* floating point "equality" test */
60 float const *tsc = gm->tsc;
61 int sprv, scur; /* previous, current state in trace */
62 int status;
63
64 #if eslDEBUGLEVEL > 0
65 if (tr->N != 0) ESL_EXCEPTION(eslEINVAL, "trace isn't empty: forgot to Reuse()?");
66 #endif
67
68 if ((status = p7_trace_Append(tr, p7T_T, k, i)) != eslOK) return status;
69 if ((status = p7_trace_Append(tr, p7T_C, k, i)) != eslOK) return status;
70 sprv = p7T_C;
71 while (sprv != p7T_S) {
72 float const *rsc = (i>0 ? gm->rsc[dsq[i]] : NULL);
73
74 switch (sprv) {
75 case p7T_C: /* C(i) comes from C(i-1) or E(i) */
76 if (XMX(i,p7G_C) == -eslINFINITY) ESL_EXCEPTION(eslFAIL, "impossible C reached at i=%d", i);
77
78 if (esl_FCompare_old(XMX(i, p7G_C), XMX(i-1, p7G_C) + gm->xsc[p7P_C][p7P_LOOP], tol) == eslOK) scur = p7T_C;
79 else if (esl_FCompare_old(XMX(i, p7G_C), XMX(i, p7G_E) + gm->xsc[p7P_E][p7P_MOVE], tol) == eslOK) scur = p7T_E;
80 else ESL_EXCEPTION(eslFAIL, "C at i=%d couldn't be traced", i);
81 break;
82
83 case p7T_E: /* E connects from any M state. k set here */
84 if (XMX(i, p7G_E) == -eslINFINITY) ESL_EXCEPTION(eslFAIL, "impossible E reached at i=%d", i);
85
86 if (p7_profile_IsLocal(gm))
87 {
88 scur = p7T_M; /* can't come from D, in a *local* Viterbi trace. */
89 for (k = M; k >= 1; k--) if (esl_FCompare_old(XMX(i, p7G_E), MMX(i,k), tol) == eslOK) break;
90 if (k == 0) ESL_EXCEPTION(eslFAIL, "E at i=%d couldn't be traced", i);
91 }
92 else /* glocal mode: we either come from D_M or M_M */
93 {
94 if (esl_FCompare_old(XMX(i, p7G_E), MMX(i,M), tol) == eslOK) { scur = p7T_M; k = M; }
95 else if (esl_FCompare_old(XMX(i, p7G_E), DMX(i,M), tol) == eslOK) { scur = p7T_D; k = M; }
96 else ESL_EXCEPTION(eslFAIL, "E at i=%d couldn't be traced", i);
97 }
98 break;
99
100 case p7T_M: /* M connects from i-1,k-1, or B */
101 if (MMX(i,k) == -eslINFINITY) ESL_EXCEPTION(eslFAIL, "impossible M reached at k=%d,i=%d", k,i);
102
103 if (esl_FCompare_old(MMX(i,k), XMX(i-1,p7G_B) + TSC(p7P_BM, k-1) + MSC(k), tol) == eslOK) scur = p7T_B;
104 else if (esl_FCompare_old(MMX(i,k), MMX(i-1,k-1) + TSC(p7P_MM, k-1) + MSC(k), tol) == eslOK) scur = p7T_M;
105 else if (esl_FCompare_old(MMX(i,k), IMX(i-1,k-1) + TSC(p7P_IM, k-1) + MSC(k), tol) == eslOK) scur = p7T_I;
106 else if (esl_FCompare_old(MMX(i,k), DMX(i-1,k-1) + TSC(p7P_DM, k-1) + MSC(k), tol) == eslOK) scur = p7T_D;
107 else ESL_EXCEPTION(eslFAIL, "M at k=%d,i=%d couldn't be traced", k,i);
108 k--; i--;
109 break;
110
111 case p7T_D: /* D connects from M,D at i,k-1 */
112 if (DMX(i, k) == -eslINFINITY) ESL_EXCEPTION(eslFAIL, "impossible D reached at k=%d,i=%d", k,i);
113
114 if (esl_FCompare_old(DMX(i,k), MMX(i, k-1) + TSC(p7P_MD, k-1), tol) == eslOK) scur = p7T_M;
115 else if (esl_FCompare_old(DMX(i,k), DMX(i, k-1) + TSC(p7P_DD, k-1), tol) == eslOK) scur = p7T_D;
116 else ESL_EXCEPTION(eslFAIL, "D at k=%d,i=%d couldn't be traced", k,i);
117 k--;
118 break;
119
120 case p7T_I: /* I connects from M,I at i-1,k*/
121 if (IMX(i,k) == -eslINFINITY) ESL_EXCEPTION(eslFAIL, "impossible I reached at k=%d,i=%d", k,i);
122
123 if (esl_FCompare_old(IMX(i,k), MMX(i-1,k) + TSC(p7P_MI, k) + ISC(k), tol) == eslOK) scur = p7T_M;
124 else if (esl_FCompare_old(IMX(i,k), IMX(i-1,k) + TSC(p7P_II, k) + ISC(k), tol) == eslOK) scur = p7T_I;
125 else ESL_EXCEPTION(eslFAIL, "I at k=%d,i=%d couldn't be traced", k,i);
126 i--;
127 break;
128
129 case p7T_N: /* N connects from S, N */
130 if (XMX(i, p7G_N) == -eslINFINITY) ESL_EXCEPTION(eslFAIL, "impossible N reached at i=%d", i);
131 scur = ( (i == 0) ? p7T_S : p7T_N);
132 break;
133
134 case p7T_B: /* B connects from N, J */
135 if (XMX(i,p7G_B) == -eslINFINITY) ESL_EXCEPTION(eslFAIL, "impossible B reached at i=%d", i);
136
137 if (esl_FCompare_old(XMX(i,p7G_B), XMX(i, p7G_N) + gm->xsc[p7P_N][p7P_MOVE], tol) == eslOK) scur = p7T_N;
138 else if (esl_FCompare_old(XMX(i,p7G_B), XMX(i, p7G_J) + gm->xsc[p7P_J][p7P_MOVE], tol) == eslOK) scur = p7T_J;
139 else ESL_EXCEPTION(eslFAIL, "B at i=%d couldn't be traced", i);
140 break;
141
142 case p7T_J: /* J connects from E(i) or J(i-1) */
143 if (XMX(i,p7G_J) == -eslINFINITY) ESL_EXCEPTION(eslFAIL, "impossible J reached at i=%d", i);
144
145 if (esl_FCompare_old(XMX(i,p7G_J), XMX(i-1,p7G_J) + gm->xsc[p7P_J][p7P_LOOP], tol) == eslOK) scur = p7T_J;
146 else if (esl_FCompare_old(XMX(i,p7G_J), XMX(i, p7G_E) + gm->xsc[p7P_E][p7P_LOOP], tol) == eslOK) scur = p7T_E;
147 else ESL_EXCEPTION(eslFAIL, "J at i=%d couldn't be traced", i);
148 break;
149
150 default: ESL_EXCEPTION(eslFAIL, "bogus state in traceback");
151 } /* end switch over statetype[tpos-1] */
152
153 /* Append this state and the current i,k to be explained to the growing trace */
154 if ((status = p7_trace_Append(tr, scur, k, i)) != eslOK) return status;
155
156 /* For NCJ, we had to defer i decrement. */
157 if ( (scur == p7T_N || scur == p7T_J || scur == p7T_C) && scur == sprv) i--;
158
159 sprv = scur;
160 } /* end traceback, at S state */
161
162 tr->M = gm->M;
163 tr->L = L;
164 return p7_trace_Reverse(tr);
165 }
166
167 /*****************************************************************
168 * 2. Example
169 *****************************************************************/
170 #ifdef p7GENERIC_VTRACE_EXAMPLE
171 /*
172 gcc -g -O2 -Dp7GENERIC_VTRACE_EXAMPLE -I. -I../easel -L. -L../easel -o generic_vtrace_example generic_vtrace.c -lhmmer -leasel -lm
173 */
174 #include <p7_config.h>
175
176 #include "easel.h"
177 #include "esl_alphabet.h"
178 #include "esl_getopts.h"
179 #include "esl_sq.h"
180 #include "esl_sqio.h"
181
182 #include "hmmer.h"
183
184 static ESL_OPTIONS options[] = {
185 /* name type default env range toggles reqs incomp help docgroup*/
186 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
187 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
188 };
189 static char usage[] = "[-options] <hmmfile> <seqfile>";
190 static char banner[] = "example of generic Viterbi tracebacks";
191
192
193 int
194 main(int argc, char **argv)
195 {
196 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 2, argc, argv, banner, usage);
197 char *hmmfile = esl_opt_GetArg(go, 1);
198 char *seqfile = esl_opt_GetArg(go, 2);
199 ESL_ALPHABET *abc = NULL;
200 P7_HMMFILE *hfp = NULL;
201 P7_HMM *hmm = NULL;
202 P7_BG *bg = NULL;
203 P7_PROFILE *gm = NULL;
204 P7_GMX *fwd = NULL;
205 ESL_SQ *sq = NULL;
206 ESL_SQFILE *sqfp = NULL;
207 P7_TRACE *tr = NULL;
208 int format = eslSQFILE_UNKNOWN;
209 char errbuf[eslERRBUFSIZE];
210 float sc;
211 int d;
212 int status;
213
214 /* Read in one HMM */
215 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
216 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
217 p7_hmmfile_Close(hfp);
218
219 /* Read in one sequence */
220 sq = esl_sq_CreateDigital(abc);
221 status = esl_sqfile_Open(seqfile, format, NULL, &sqfp);
222 if (status == eslENOTFOUND) p7_Fail("No such file.");
223 else if (status == eslEFORMAT) p7_Fail("Format unrecognized.");
224 else if (status == eslEINVAL) p7_Fail("Can't autodetect stdin or .gz.");
225 else if (status != eslOK) p7_Fail("Open failed, code %d.", status);
226 if (esl_sqio_Read(sqfp, sq) != eslOK) p7_Fail("Failed to read sequence");
227 esl_sqfile_Close(sqfp);
228
229 /* Configure a profile from the HMM */
230 bg = p7_bg_Create(abc);
231 p7_bg_SetLength(bg, sq->n);
232 gm = p7_profile_Create(hmm->M, abc);
233 p7_ProfileConfig(hmm, bg, gm, sq->n, p7_LOCAL);
234
235 /* Allocate matrix and a trace */
236 fwd = p7_gmx_Create(gm->M, sq->n);
237 tr = p7_trace_Create();
238
239 /* Run Viterbi; do traceback */
240 p7_GViterbi (sq->dsq, sq->n, gm, fwd, &sc);
241 p7_GTrace (sq->dsq, sq->n, gm, fwd, tr);
242
243 /* Dump and validate the trace. */
244 p7_trace_Dump(stdout, tr, gm, sq->dsq);
245 if (p7_trace_Validate(tr, abc, sq->dsq, errbuf) != eslOK)
246 p7_Die("trace fails validation:\n%s\n", errbuf);
247
248 /* Domain info in the trace. */
249 p7_trace_Index(tr);
250 printf("# Viterbi: %d domains :\n", tr->ndom);
251 for (d = 0; d < tr->ndom; d++)
252 printf("%6d %6d %6d %6d\n", tr->sqfrom[d], tr->sqto[d], tr->hmmfrom[d], tr->hmmto[d]);
253
254 /* Cleanup */
255 p7_trace_Destroy(tr);
256 p7_gmx_Destroy(fwd);
257 p7_profile_Destroy(gm);
258 p7_bg_Destroy(bg);
259 p7_hmm_Destroy(hmm);
260 esl_alphabet_Destroy(abc);
261 esl_getopts_Destroy(go);
262 return 0;
263 }
264 #endif /*p7GENERIC_VTRACE_EXAMPLE*/
265 /*-------------------- end, example -----------------------------*/
266
267
268
269