Debian Codegraph @

hmmer/src/emit.c

raw symbols SCIP
1 /* Emitting (sampling) sequences from an HMM, in either core or
2 * profile form.
3 *
4 * 1. Exported API: sequence emission routines.
5 * 2. Private functions.
6 * 3. Stats driver.
7 *
8 * SRE, Tue Jan 9 08:55:53 2007 [Janelia] [The Crystal Method, Vegas]
9 */
10
11 #include <p7_config.h>
12
13 #include "easel.h"
14 #include "esl_random.h"
15 #include "esl_sq.h"
16 #include "esl_vectorops.h"
17
18 #include "hmmer.h"
19
20 static int sample_endpoints(ESL_RANDOMNESS *r, const P7_PROFILE *gm, int *ret_kstart, int *ret_kend);
21
22
23 /*****************************************************************
24 * 1. Exported API: sequence emission routines.
25 *****************************************************************/
26
27 /* Function: p7_CoreEmit()
28 * Incept: SRE, Tue Jan 9 10:20:51 2007 [Janelia]
29 *
30 * Purpose: Generate (sample) a sequence from a core HMM <hmm>.
31 *
32 * Optionally return the sequence and/or its trace in <sq>
33 * and <tr>, respectively, which the caller has
34 * allocated. Having the caller provide these reusable
35 * objects allows re-use of both <sq> and <tr> in repeated
36 * calls, saving malloc/free wastage. Either can be passed
37 * as <NULL> if it isn't needed.
38 *
39 * This does not set any fields in the <sq> except for the
40 * sequence itself. Caller must set the name, and any other
41 * annotation it wants to add.
42 *
43 * Trace is relative to the core model: it may include
44 * I_0 and I_M states, B->DD->M entry is explicit, and a
45 * 0 length generated sequence is possible.
46 *
47 * Args: r - source of randomness
48 * hmm - core HMM to generate from
49 * sq - opt: digital sequence sampled (or NULL)
50 * tr - opt: trace sampled (or NULL)
51 *
52 * Returns: <eslOK> on success;
53 * optionally return the digital sequence through <ret_sq>,
54 * and optionally return its trace in <ret_tr>.
55 *
56 * Throws: <eslECORRUPT> if emission gets us into an illegal state,
57 * probably indicating that a probability that should have
58 * been zero wasn't.
59 *
60 * Throws <eslEMEM> on a reallocation error.
61 *
62 * In these cases, the contents of <sq> and <tr> may be
63 * corrupted. Caller should not trust their data, but may
64 * safely reuse them.
65 *
66 * Xref: STL11/124.
67 */
68 int
69 p7_CoreEmit(ESL_RANDOMNESS *r, const P7_HMM *hmm, ESL_SQ *sq, P7_TRACE *tr)
70 {
71 int k = 0; /* position in model nodes 1..M */
72 int i = 0; /* position in sequence 1..L */
73 char st = p7T_B; /* state type */
74 int x; /* sampled residue */
75 int status;
76
77 if (sq != NULL) esl_sq_Reuse(sq);
78 if (tr != NULL) {
79 if ((status = p7_trace_Reuse(tr)) != eslOK) goto ERROR;
80 if ((status = p7_trace_Append(tr, st, k, i)) != eslOK) goto ERROR;
81 }
82 while (st != p7T_E)
83 {
84 /* Sample next state type, given current state type (and current k) */
85 switch (st) {
86 case p7T_B:
87 case p7T_M:
88 switch (esl_rnd_FChoose(r, hmm->t[k], 3)) {
89 case 0: st = p7T_M; break;
90 case 1: st = p7T_I; break;
91 case 2: st = p7T_D; break;
92 default: ESL_XEXCEPTION(eslEINCONCEIVABLE, "impossible.");
93 }
94 break;
95
96 case p7T_I:
97 switch (esl_rnd_FChoose(r, hmm->t[k]+3, 2)) {
98 case 0: st = p7T_M; break;
99 case 1: st = p7T_I; break;
100 default: ESL_XEXCEPTION(eslEINCONCEIVABLE, "impossible.");
101 }
102 break;
103
104 case p7T_D:
105 switch (esl_rnd_FChoose(r, hmm->t[k]+5, 2)) {
106 case 0: st = p7T_M; break;
107 case 1: st = p7T_D; break;
108 default: ESL_XEXCEPTION(eslEINCONCEIVABLE, "impossible.");
109 }
110 break;
111
112 default: ESL_XEXCEPTION(eslECORRUPT, "impossible state reached during emission");
113 }
114
115 /* Bump k,i if needed, depending on new state type */
116 if (st == p7T_M || st == p7T_D) k++;
117 if (st == p7T_M || st == p7T_I) i++;
118
119 /* a transit to M_M+1 is a transit to the E state */
120 if (k == hmm->M+1) {
121 if (st == p7T_M) { st = p7T_E; k = 0; }
122 else ESL_XEXCEPTION(eslECORRUPT, "failed to reach E state properly");
123 }
124
125 /* Sample new residue x if in match or insert */
126 if (st == p7T_M) x = esl_rnd_FChoose(r, hmm->mat[k], hmm->abc->K);
127 else if (st == p7T_I) x = esl_rnd_FChoose(r, hmm->ins[k], hmm->abc->K);
128 else x = eslDSQ_SENTINEL;
129
130 /* Add state to trace */
131 if (tr != NULL) {
132 if ((status = p7_trace_Append(tr, st, k, i)) != eslOK) goto ERROR;
133 }
134 /* Add x to sequence */
135 if (sq != NULL && x != eslDSQ_SENTINEL)
136 if ((status = esl_sq_XAddResidue(sq, x)) != eslOK) goto ERROR;
137 }
138
139 /* Terminate the trace and sequence (both are optional, remember) */
140 if (tr != NULL) { tr->M = hmm->M; tr->L = i; }
141 if (sq != NULL && (status = esl_sq_XAddResidue(sq, eslDSQ_SENTINEL)) != eslOK) goto ERROR;
142 return eslOK;
143
144 ERROR:
145 return status;
146 }
147
148
149 /* Function: p7_ProfileEmit()
150 * Synopsis: Sample a sequence from the search form of the model.
151 * Incept: SRE, Mon Jan 22 10:23:28 2007 [Janelia]
152 *
153 * Purpose: Sample a sequence from the implicit
154 * probabilistic model of a Plan7 profile <gm>. This
155 * requires also having the core probabilities of
156 * the accompanying <hmm>, and the background
157 * frequencies of null1 model <bg>.
158 *
159 * Optionally return the sequence and/or its trace in <sq>
160 * and <tr>, respectively. Caller has allocated space for
161 * both of these, though they may get reallocated/grown
162 * here. Either can be passed as <NULL> if unneeded.
163 *
164 * Only the sequence field is set in the <sq>. Caller must
165 * set the name, plus any other fields it wants to set. If
166 * the <sq> was created in digital mode, this is the <sq->dsq>;
167 * if the <sq> was created in text mode, this is <sq->seq>.
168 *
169 * <p7_ProfileEmit()> deliberately uses an <ESL_SQ> object
170 * instead of a plain <ESL_DSQ *> or <char *> string, to
171 * take advantage of the object's support for dynamic
172 * reallocation of seq length, and to allow both digital and
173 * text mode generation.
174 *
175 * Args: r - source of randomness
176 * hmm - core probabilities of the profile
177 * gm - configured search profile
178 * sq - optRETURN: sampled sequence
179 * tr - optRETURN: sampled trace
180 *
181 * Throws: (no abnormal error conditions)
182 */
183 int
184 p7_ProfileEmit(ESL_RANDOMNESS *r, const P7_HMM *hmm, const P7_PROFILE *gm, const P7_BG *bg, ESL_SQ *sq, P7_TRACE *tr)
185 {
186 char prv, st; /* prev, current state type */
187 int k = 0; /* position in model nodes 1..M */
188 int i = 0; /* position in sequence 1..L */
189 int x; /* sampled residue */
190 int kend = hmm->M; /* predestined end node */
191 int status;
192 float xt[p7P_NXSTATES][p7P_NXTRANS];
193
194 /* Backcalculate the probabilities in the special states (loop and length model) */
195 for (i = 0; i < p7P_NXSTATES; i++)
196 for (x = 0; x < p7P_NXTRANS; x++)
197 xt[i][x] = exp(gm->xsc[i][x]);
198
199 if (sq != NULL) esl_sq_Reuse(sq);
200 if (tr != NULL) {
201 if ((status = p7_trace_Reuse(tr)) != eslOK) goto ERROR;
202 if ((status = p7_trace_Append(tr, p7T_S, k, i)) != eslOK) goto ERROR;
203 if ((status = p7_trace_Append(tr, p7T_N, k, i)) != eslOK) goto ERROR;
204 }
205 st = p7T_N;
206 i = 0;
207 while (st != p7T_T)
208 {
209 /* Sample a state transition. After this section, prv and st (prev->current state) are set;
210 * k also gets set if we make a B->Mk entry transition.
211 */
212 prv = st;
213 switch (st) {
214 case p7T_B:
215 if (p7_profile_IsLocal(gm))
216 { /* local mode: enter the implicit profile: choose our entry and our predestined exit */
217 if ((status = sample_endpoints(r, gm, &k, &kend)) != eslOK) goto ERROR;
218 st = p7T_M; /* must be, because left wing is retracted */
219 }
220 else
221 { /* glocal mode: treat B as M_0, use its transitions to MID. */
222 /* FIXME: this is wrong. It should sample from B->Mk distribution! */
223 switch (esl_rnd_FChoose(r, P7H_TMAT(hmm, 0), p7H_NTMAT)) {
224 case 0: st = p7T_M; k = 1; break;
225 case 1: st = p7T_I; k = 0; break;
226 case 2: st = p7T_D; k = 1; break;
227 default: ESL_XEXCEPTION(eslEINCONCEIVABLE, "impossible.");
228 }
229 }
230 break;
231
232 case p7T_M:
233 if (k == kend) st = p7T_E; /* check our preordained fate */
234 else {
235 switch (esl_rnd_FChoose(r, P7H_TMAT(hmm, k), p7H_NTMAT)) {
236 case 0: st = p7T_M; break;
237 case 1: st = p7T_I; break;
238 case 2: st = p7T_D; break;
239 default: ESL_XEXCEPTION(eslEINCONCEIVABLE, "impossible.");
240 }
241 }
242 break;
243
244 case p7T_D:
245 if (k == kend) st = p7T_E;
246 else st = (esl_rnd_FChoose(r, P7H_TDEL(hmm, k), p7H_NTDEL) == 0) ? p7T_M : p7T_D;
247 break;
248
249 case p7T_I: st = (esl_rnd_FChoose(r, P7H_TINS(hmm, k), p7H_NTINS) == 0) ? p7T_M : p7T_I; break;
250 case p7T_N: st = (esl_rnd_FChoose(r, xt[p7P_N], p7P_NXTRANS) == p7P_MOVE) ? p7T_B : p7T_N; break;
251 case p7T_E: st = (esl_rnd_FChoose(r, xt[p7P_E], p7P_NXTRANS) == p7P_MOVE) ? p7T_C : p7T_J; break;
252 case p7T_C: st = (esl_rnd_FChoose(r, xt[p7P_C], p7P_NXTRANS) == p7P_MOVE) ? p7T_T : p7T_C; break;
253 case p7T_J: st = (esl_rnd_FChoose(r, xt[p7P_J], p7P_NXTRANS) == p7P_MOVE) ? p7T_B : p7T_J; break;
254 default: ESL_XEXCEPTION(eslECORRUPT, "impossible state reached during emission");
255 }
256
257 /* Based on the transition we just sampled, update k. */
258 if (st == p7T_E) k = 0;
259 else if (st == p7T_M && prv != p7T_B) k++; /* be careful about B->Mk, where we already set k */
260 else if (st == p7T_D) k++;
261
262 /* Based on the transition we just sampled, generate a residue. */
263 if (st == p7T_M) x = esl_rnd_FChoose(r, hmm->mat[k], hmm->abc->K);
264 else if (st == p7T_I) x = esl_rnd_FChoose(r, hmm->ins[k], hmm->abc->K);
265 else if ((st == p7T_N || st == p7T_C || st == p7T_J) && prv==st) x = esl_rnd_FChoose(r, bg->f, hmm->abc->K);
266 else x = eslDSQ_SENTINEL;
267
268 if (x != eslDSQ_SENTINEL) i++;
269
270 /* Add residue (if any) to sequence */
271 if (sq != NULL && x != eslDSQ_SENTINEL && (status = esl_sq_XAddResidue(sq, x)) != eslOK) goto ERROR;
272
273 /* Add state to trace. */
274 if (tr != NULL) {
275 if ((status = p7_trace_Append(tr, st, k, i)) != eslOK) goto ERROR;
276 }
277 }
278 /* Terminate the trace and sequence (both are optional, remember) */
279 if (tr != NULL) { tr->M = hmm->M; tr->L = i; }
280 if (sq != NULL && (status = esl_sq_XAddResidue(sq, eslDSQ_SENTINEL)) != eslOK) goto ERROR;
281 return eslOK;
282
283 ERROR:
284 return status;
285 }
286
287 /* Function: p7_emit_SimpleConsensus()
288 * Synopsis: Generate simple consensus: ML residue in each match state
289 * Incept: SRE, Mon Sep 1 09:10:47 2008 [Janelia]
290 *
291 * Purpose: Generate a simple consensus sequence for model <hmm>
292 * consisting of the maximum probability residue in each
293 * match state; store this consensus in digital <sq>.
294 *
295 * Returns: <eslOK> on success.
296 *
297 * Throws: <eslEINVAL> if the <sq> isn't in digital mode.
298 */
299 int
300 p7_emit_SimpleConsensus(const P7_HMM *hmm, ESL_SQ *sq)
301 {
302 int k;
303 int x;
304 int status;
305
306 if (! esl_sq_IsDigital(sq)) ESL_EXCEPTION(eslEINVAL, "p7_emit_SimpleConsensus() expects a digital-mode <sq>");
307 if ((status = esl_sq_GrowTo(sq, hmm->M)) != eslOK) return status;
308
309 for (k = 1; k <= hmm->M; k++)
310 {
311 if (hmm->mm && hmm->mm[k] == 'm') { //masked position, spit out the degenerate code
312 if ((status = esl_sq_XAddResidue(sq, hmm->abc->Kp-3)) != eslOK) return status;
313 } else {
314 x = esl_vec_FArgMax(hmm->mat[k], hmm->abc->K);
315 if ((status = esl_sq_XAddResidue(sq, x)) != eslOK) return status;
316 }
317 }
318 if ((status = esl_sq_XAddResidue(sq, eslDSQ_SENTINEL)) != eslOK) return status;
319 return eslOK;
320 }
321
322
323 /* Function: p7_emit_FancyConsensus()
324 * Synopsis: Emit a fancier consensus with upper/lower case and N/X's.
325 * Incept: SRE, Fri May 14 09:33:10 2010 [Janelia]
326 *
327 * Purpose: Generate a consensus sequence for model <hmm>, consisting
328 * of the maximum probability residue in each match state;
329 * store this sequence in text-mode <sq> provided by the caller.
330 *
331 * If the probability of the consensus residue is less than
332 * <min_lower>, show an ``any'' residue (N or X) instead.
333 * If the probability of the consensus residue is $\geq$
334 * <min_lower> and less than <min_upper>, show the residue
335 * as lower case; if it is $\geq$ <min_upper>, show it as
336 * upper case.
337 *
338 * Returns: <eslOK> on success.
339 *
340 * Throws: <eslEINVAL> if the <sq> isn't in text mode.
341 *
342 * Xref: SRE:J6/59.
343 */
344 int
345 p7_emit_FancyConsensus(const P7_HMM *hmm, float min_lower, float min_upper, ESL_SQ *sq)
346 {
347 int k, x;
348 float p;
349 char c;
350 int status;
351
352 if (! esl_sq_IsText(sq)) ESL_EXCEPTION(eslEINVAL, "p7_emit_FancyConsensus() expects a text-mode <sq>");
353
354 if ((status = esl_sq_GrowTo(sq, hmm->M)) != eslOK) return status;
355
356 for (k = 1; k <= hmm->M; k++)
357 {
358
359 if (hmm->mm && hmm->mm[k] == 'm') { //masked position, spit out the degenerate code
360 if ((status = esl_sq_CAddResidue(sq, tolower(esl_abc_CGetUnknown(hmm->abc))) ) != eslOK) return status;
361 } else {
362 p = esl_vec_FMax( hmm->mat[k], hmm->abc->K);
363 x = esl_vec_FArgMax(hmm->mat[k], hmm->abc->K);
364
365 if (p < min_lower) c = tolower(esl_abc_CGetUnknown(hmm->abc));
366 else if (p >= min_upper) c = toupper(hmm->abc->sym[x]);
367 else c = tolower(hmm->abc->sym[x]);
368
369 if ((status = esl_sq_CAddResidue(sq, c)) != eslOK) return status;
370 }
371 }
372 if ((status = esl_sq_CAddResidue(sq, '\0')) != eslOK) return status;
373 return eslOK;
374 }
375
376
377
378 /*****************************************************************
379 * 2. Private functions.
380 *****************************************************************/
381
382 /* sample_endpoints()
383 * Incept: SRE, Mon Jan 22 10:43:20 2007 [Janelia]
384 *
385 * Purpose: Given a profile <gm> and random number source <r>, sample
386 * a begin transition from the implicit probabilistic profile
387 * model, yielding a sampled start and end node; return these
388 * via <ret_kstart> and <ret_kend>.
389 *
390 * By construction, the entry at node <kstart> is into a
391 * match state, but the exit from node <kend> might turn
392 * out to be from either a match or delete state.
393 *
394 * We assume that exits j are uniformly distributed for a
395 * particular entry point i: $a_{ij} =$ constant $\forall
396 * j$.
397 *
398 * Returns: <eslOK> on success.
399 *
400 * Throws: <eslEMEM> on allocation error.
401 *
402 * Xref: STL11/138
403 */
404 static int
405 sample_endpoints(ESL_RANDOMNESS *r, const P7_PROFILE *gm, int *ret_kstart, int *ret_kend)
406 {
407 float *pstart = NULL;
408 int k;
409 int kstart, kend;
410 int status;
411
412 /* We have to backcalculate a probability distribution from the
413 * lod B->Mk scores in a local model; this is a little time consuming,
414 * but we don't have to do it often.
415 */
416 ESL_ALLOC(pstart, sizeof(float) * (gm->M+1));
417 pstart[0] = 0.0f;
418 for (k = 1; k <= gm->M; k++)
419 pstart[k] = exp(p7P_TSC(gm, k-1, p7P_BM)) * (gm->M - k + 1); /* multiply p_ij by the number of exits j */
420 kstart = esl_rnd_FChoose(r, pstart, gm->M+1); /* sample the starting position from that distribution */
421 kend = kstart + esl_rnd_Roll(r, gm->M-kstart+1); /* and the exit uniformly from possible exits for it */
422
423 free(pstart);
424 *ret_kstart = kstart;
425 *ret_kend = kend;
426 return eslOK;
427
428 ERROR:
429 if (pstart != NULL) free(pstart);
430 *ret_kstart = 0;
431 *ret_kend = 0;
432 return status;
433 }
434
435
436 /*****************************************************************
437 * 3. Stats driver
438 *****************************************************************/
439
440 /* A small driver providing a testbed for sequence-emission related development testing.
441 *
442 * gcc -g -Wall -o stats -L. -I. -L../easel -I../easel -Dp7EMIT_STATS emit.c -lhmmer -leasel -lm
443 */
444 #ifdef p7EMIT_STATS
445 #include <p7_config.h>
446
447 #include <stdio.h>
448
449 #include "easel.h"
450 #include "esl_alphabet.h"
451 #include "esl_random.h"
452 #include "esl_sq.h"
453 #include "hmmer.h"
454
455 int
456 main(int argc, char **argv)
457 {
458 char *hmmfile = argv[1]; /* name of HMM file to read one HMM from */
459 ESL_ALPHABET *abc = NULL; /* sequence alphabet */
460 ESL_RANDOMNESS *r = NULL; /* source of randomness */
461 P7_HMMFILE *hfp = NULL; /* open hmmfile */
462 P7_HMM *hmm = NULL; /* HMM to emit from */
463 P7_PROFILE *gm = NULL; /* profile HMM (scores) */
464 P7_BG *bg = NULL; /* null model */
465 P7_TRACE *tr = NULL; /* sampled trace */
466 ESL_SQ *sq = NULL; /* sampled digital sequence */
467 int n = 1000;
468 int counts[p7T_NSTATETYPES];
469 int i;
470 float sc;
471 float nullsc;
472 double bitscore;
473
474 r = esl_randomness_CreateFast(0);
475 tr = p7_trace_Create();
476 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("failed to open %s", hmmfile);
477 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("failed to read HMM");
478 sq = esl_sq_CreateDigital(abc);
479 bg = p7_bg_Create(abc);
480 gm = p7_profile_Create(hmm->M, abc);
481
482 p7_ProfileConfig(hmm, bg, gm, sq->n, p7_LOCAL);
483
484 for (i = 0; i < n; i++)
485 {
486 p7_ProfileEmit(r, hmm, gm, bg, sq, tr);
487 p7_trace_GetStateUseCounts(tr, counts);
488
489 p7_ReconfigLength(gm, sq->n);
490 p7_bg_SetLength(bg, sq->n);
491 p7_trace_Score(tr, sq->dsq, gm, &sc);
492 p7_bg_NullOne (bg, sq->dsq, sq->n, &nullsc);
493 bitscore = (sc - nullsc)/ eslCONST_LOG2;
494
495 printf("%d %8.4f\n",
496 counts[p7T_M] + (counts[p7T_I] + counts[p7T_D])/2,
497 bitscore);
498 }
499
500 p7_profile_Destroy(gm);
501 esl_sq_Destroy(sq);
502 p7_trace_Destroy(tr);
503 esl_randomness_Destroy(r);
504 esl_alphabet_Destroy(abc);
505 p7_hmmfile_Close(hfp);
506 p7_hmm_Destroy(hmm);
507 return eslOK;
508 }
509 #endif /*p7EMIT_STATS*/
510 /*-------------------- end, stats driver ------------------------*/
511
512
513 /*****************************************************************
514 * x. Example
515 *****************************************************************/
516 #ifdef p7EMIT_EXAMPLE
517 /*
518 gcc -g -Wall -o emit_example -Dp7EMIT_EXAMPLE -I. -I../easel -L. -L../easel emit.c -lhmmer -leasel -lm
519 */
520 #include <p7_config.h>
521
522 #include "easel.h"
523 #include "esl_alphabet.h"
524 #include "esl_getopts.h"
525 #include "esl_sq.h"
526 #include "esl_sqio.h"
527
528 #include "hmmer.h"
529
530 static ESL_OPTIONS options[] = {
531 /* name type default env range toggles reqs incomp help docgroup*/
532 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
533 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
534 { "-L", eslARG_INT, "100", NULL, NULL, NULL, NULL, NULL, "configured mean seq length for profile", 0 },
535 { "-N", eslARG_INT, "10", NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
536 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
537 };
538 static char usage[] = "[-options] <hmmfile>";
539 static char banner[] = "example of emitting sequences from profile";
540
541 int
542 main(int argc, char **argv)
543 {
544 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
545 ESL_RANDOMNESS *rng = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
546 char *hmmfile = esl_opt_GetArg(go, 1);
547 int L = esl_opt_GetInteger(go, "-L");
548 int N = esl_opt_GetInteger(go, "-N");
549 ESL_ALPHABET *abc = NULL;
550 P7_HMMFILE *hfp = NULL;
551 P7_HMM *hmm = NULL;
552 P7_BG *bg = NULL;
553 P7_PROFILE *gm = NULL;
554 P7_TRACE *tr = p7_trace_Create();
555 ESL_SQ *sq = NULL;
556 char errbuf[eslERRBUFSIZE];
557 int i;
558 int status;
559
560 status = p7_hmmfile_Open(hmmfile, NULL, &hfp, errbuf);
561 if (status == eslENOTFOUND) p7_Fail("File existence/permissions problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf);
562 else if (status == eslEFORMAT) p7_Fail("File format problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf);
563 else if (status != eslOK) p7_Fail("Unexpected error %d in opening HMM file %s.\n%s\n", status, hmmfile, errbuf);
564
565 status = p7_hmmfile_Read(hfp, &abc, &hmm);
566 if (status == eslEFORMAT) p7_Fail("Bad file format in HMM file %s:\n%s\n", hfp->fname, hfp->errbuf);
567 else if (status == eslEINCOMPAT) p7_Fail("HMM in %s is not in the expected %s alphabet\n", hfp->fname, esl_abc_DecodeType(abc->type));
568 else if (status == eslEOF) p7_Fail("Empty HMM file %s? No HMM data found.\n", hfp->fname);
569 else if (status != eslOK) p7_Fail("Unexpected error in reading HMMs from %s\n", hfp->fname);
570
571 p7_hmmfile_Close(hfp);
572
573 bg = p7_bg_Create(abc); p7_bg_SetLength(bg, L);
574 gm = p7_profile_Create(hmm->M, abc); p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL);
575 sq = esl_sq_CreateDigital(abc);
576
577 for (i = 0; i < N; i++)
578 {
579 p7_ProfileEmit(rng, hmm, gm, bg, sq, tr);
580 esl_sq_FormatName(sq, "%s-sample%d", hmm->name, i);
581 esl_sqio_Write(stdout, sq, eslSQFILE_FASTA, FALSE);
582
583 if (p7_trace_Validate(tr, abc, sq->dsq, errbuf) != eslOK) esl_fatal(errbuf);
584
585 esl_sq_Reuse(sq);
586 p7_trace_Reuse(tr);
587 }
588
589 esl_sq_Destroy(sq);
590 p7_trace_Destroy(tr);
591 p7_profile_Destroy(gm);
592 p7_bg_Destroy(bg);
593 p7_hmm_Destroy(hmm);
594 esl_alphabet_Destroy(abc);
595 esl_randomness_Destroy(rng);
596 esl_getopts_Destroy(go);
597 return 0;
598 }
599 #endif /*p7EMIT_EXAMPLE*/
600 /*---------------------- end, example ---------------------------*/
601
602
603
604