Debian Codegraph @

hmmer/src/p7_bg.c

raw symbols SCIP
1 /* P7_BG: the null (background) model
2 *
3 * Contents:
4 * 1. P7_BG object: allocation, initialization, destruction.
5 * 2. Reading/writing residue backgrounds from files.
6 * 3. Standard iid null model ("null1").
7 * 4. Filter null model.
8 * 5. Benchmark driver.
9 * 6. Unit tests.
10 * 7. Test driver.
11 * 8. Examples.
12 */
13 #include <p7_config.h>
14
15 #include <string.h>
16
17 #include "easel.h"
18 #include "esl_alphabet.h"
19 #include "esl_hmm.h"
20 #include "esl_vectorops.h"
21
22 #include "hmmer.h"
23
24
25 /*****************************************************************
26 * 1. The P7_BG object: allocation, initialization, destruction.
27 *****************************************************************/
28
29 /* Function: p7_bg_Create()
30 * Synopsis: Create a <P7_BG> null model object.
31 *
32 * Purpose: Allocate a <P7_BG> object for digital alphabet <abc>,
33 * initializes it to appropriate default values, and
34 * returns a pointer to it.
35 *
36 * For protein models, default iid background frequencies
37 * are set (by <p7_AminoFrequencies()>) to average
38 * Swiss-Prot residue composition. For DNA, RNA and other
39 * alphabets, default frequencies are set to a uniform
40 * distribution.
41 *
42 * The model composition <bg->mcomp[]> is not initialized
43 * here; neither is the filter null model <bg->fhmm>. To
44 * use the filter null model, caller will want to
45 * initialize these fields by calling
46 * <p7_bg_SetFilter()>.
47 *
48 * Throws: <NULL> on allocation failure.
49 *
50 * Xref: STL11/125.
51 */
52 P7_BG *
53 p7_bg_Create(const ESL_ALPHABET *abc)
54 {
55 P7_BG *bg = NULL;
56 int status;
57
58 ESL_ALLOC(bg, sizeof(P7_BG));
59 bg->f = NULL;
60 bg->fhmm = NULL;
61
62 ESL_ALLOC(bg->f, sizeof(float) * abc->K);
63 if ((bg->fhmm = esl_hmm_Create(abc, 2)) == NULL) goto ERROR;
64
65 if (abc->type == eslAMINO)
66 {
67 if (p7_AminoFrequencies(bg->f) != eslOK) goto ERROR;
68 }
69 else
70 esl_vec_FSet(bg->f, abc->K, 1. / (float) abc->K);
71
72 bg->p1 = 350./351.;
73 bg->omega = 1./256.;
74 bg->abc = abc;
75 return bg;
76
77 ERROR:
78 p7_bg_Destroy(bg);
79 return NULL;
80 }
81
82
83 /* Function: p7_bg_CreateUniform()
84 * Synopsis: Creates background model with uniform freqs.
85 *
86 * Purpose: Creates a background model for alphabet <abc>
87 * with uniform residue frequencies.
88 */
89 P7_BG *
90 p7_bg_CreateUniform(const ESL_ALPHABET *abc)
91 {
92 P7_BG *bg = NULL;
93 int status;
94
95 ESL_ALLOC(bg, sizeof(P7_BG));
96 bg->f = NULL;
97 bg->fhmm = NULL;
98
99 ESL_ALLOC(bg->f, sizeof(float) * abc->K);
100 if ((bg->fhmm = esl_hmm_Create(abc, 2)) == NULL) goto ERROR;
101
102 esl_vec_FSet(bg->f, abc->K, 1. / (float) abc->K);
103 bg->p1 = 350./351.;
104 bg->omega = 1./256.;
105 bg->abc = (ESL_ALPHABET *) abc; /* safe: we're just keeping a reference */
106 return bg;
107
108 ERROR:
109 p7_bg_Destroy(bg);
110 return NULL;
111 }
112
113
114 /* Function: p7_bg_Clone()
115 * Synopsis: Create a duplicate of an existing <P7_BG> object.
116 *
117 * Purpose: Creates a duplicate of the existing <P7_BG> object <bg>.
118 *
119 * Returns: ptr to the duplicate <P7_BG> object.
120 *
121 * Throws: <NULL> on allocation failure.
122 */
123 P7_BG *
124 p7_bg_Clone(const P7_BG *bg)
125 {
126 P7_BG *dup = NULL;
127 int status;
128
129 ESL_ALLOC(dup, sizeof(P7_BG));
130 dup->f = NULL;
131 dup->fhmm = NULL;
132 dup->abc = bg->abc; /* by reference only */
133
134 ESL_ALLOC(dup->f, sizeof(float) * bg->abc->K);
135 memcpy(dup->f, bg->f, sizeof(float) * bg->abc->K);
136 if ((dup->fhmm = esl_hmm_Clone(bg->fhmm)) == NULL) goto ERROR;
137
138 dup->p1 = bg->p1;
139 dup->omega = bg->omega;
140 return dup;
141
142 ERROR:
143 p7_bg_Destroy(dup);
144 return NULL;
145 }
146
147
148 /* Function: p7_bg_Dump()
149 * Synopsis: Outputs <P7_BG> object as text, for diagnostics.
150 *
151 * Purpose: Given a null model <bg>, dump it as text to stream <fp>.
152 */
153 int
154 p7_bg_Dump(FILE *ofp, const P7_BG *bg)
155 {
156 esl_vec_FDump(ofp, bg->f, bg->abc->K, bg->abc->sym);
157 return eslOK;
158 }
159
160
161
162 /* Function: p7_bg_Destroy()
163 *
164 * Purpose: Frees a <P7_BG> object.
165 *
166 * Returns: (void)
167 *
168 * Xref: SRE:STL11/125.
169 */
170 void
171 p7_bg_Destroy(P7_BG *bg)
172 {
173 if (bg != NULL) {
174 if (bg->f != NULL) free(bg->f);
175 if (bg->fhmm != NULL) esl_hmm_Destroy(bg->fhmm);
176 free(bg);
177 }
178 return;
179 }
180
181
182 /* Function: p7_bg_SetLength()
183 * Synopsis: Set the null model length distribution.
184 *
185 * Purpose: Sets the geometric null model length
186 * distribution in <bg> to a mean of <L> residues.
187 */
188 int
189 p7_bg_SetLength(P7_BG *bg, int L)
190 {
191 bg->p1 = (float) L / (float) (L+1);
192
193 bg->fhmm->t[0][0] = bg->p1;
194 bg->fhmm->t[0][1] = 1.0f - bg->p1;
195
196 return eslOK;
197 }
198
199
200
201 /*****************************************************************
202 * 2. Reading/writing residue backgrounds from files
203 *****************************************************************/
204
205 /* Function: p7_bg_Read()
206 * Synopsis: Read background frequencies from a file.
207 *
208 * Purpose: Read new background frequencies from file <bgfile>,
209 * overwriting the frequencies previously in the
210 * <P7_BG> object <bg>.
211 *
212 * Note that <bg> is already created by the caller, not
213 * created here. Also note that <p7_bg_Read()> only reads
214 * residue background frequencies used for the "null
215 * model", whereas a <P7_BG> object contains additional
216 * information for the bias filter and for the biased
217 * composition correction.
218 *
219 * Args: bgfile - file to read.
220 * bg - existing <P7_BG> object provided by the caller.
221 * errbuf - OPTIONAL: space for an error message, upon parse errors; or NULL.
222 *
223 * Returns: <eslOK> on success, and background frequencies in <bg>
224 * are overwritten.
225 *
226 * <eslENOTFOUND> if <bgfile> can't be opened for reading.
227 * <eslEFORMAT> if parsing of <bgfile> fails for some
228 * reason. In both cases, <errbuf> contains a
229 * user-directed error message upon return, including (if
230 * relevant) the file name <bgfile> and the line number on
231 * which an error was detected. <bg> is unmodified.
232 *
233 * Throws: <eslEMEM> on allocation failure; <bg> is unmodified,
234 * and <errbuf> is empty.
235 */
236 int
237 p7_bg_Read(char *bgfile, P7_BG *bg, char *errbuf)
238 {
239 ESL_FILEPARSER *efp = NULL;
240 float *fq = NULL;
241 int n = 0;
242 char *tok;
243 int toklen;
244 int alphatype;
245 ESL_DSQ x;
246 int status;
247
248 if (errbuf) errbuf[0] = '\0';
249
250 status = esl_fileparser_Open(bgfile, NULL, &efp);
251 if (status == eslENOTFOUND) ESL_XFAIL(eslENOTFOUND, errbuf, "couldn't open bg file %s for reading", bgfile);
252 else if (status != eslOK) goto ERROR;
253
254 esl_fileparser_SetCommentChar(efp, '#');
255
256 /* First token is alphabet type: amino | DNA | RNA */
257 status = esl_fileparser_GetToken(efp, &tok, &toklen);
258 if (status == eslEOF) ESL_XFAIL(eslEFORMAT, errbuf, "premature end of file [line %d of bgfile %s]", efp->linenumber, bgfile);
259 else if (status != eslOK) goto ERROR;
260
261 alphatype = esl_abc_EncodeType(tok);
262 if (alphatype == eslUNKNOWN) ESL_XFAIL(eslEFORMAT, errbuf, "expected alphabet type but saw \"%s\" [line %d of bgfile %s]", tok, efp->linenumber, bgfile);
263 else if (alphatype != bg->abc->type) ESL_XFAIL(eslEFORMAT, errbuf, "bg file's alphabet is %s; expected %s [line %d, %s]", tok, esl_abc_DecodeType(bg->abc->type), efp->linenumber, bgfile);
264
265 ESL_ALLOC(fq, sizeof(float) * bg->abc->K);
266 esl_vec_FSet(fq, bg->abc->K, -1.0);
267
268 while ((status = esl_fileparser_NextLine(efp)) == eslOK)
269 {
270 status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen);
271 if (status == eslEOL) ESL_XFAIL(eslEFORMAT, errbuf, "premature end of file [line %d of bgfile %s", efp->linenumber, bgfile);
272 else if (status != eslOK) goto ERROR;
273
274 if (toklen != 1 || ! esl_abc_CIsCanonical(bg->abc, *tok))
275 ESL_XFAIL(eslEFORMAT, errbuf, "expected to parse a residue letter; saw %s [line %d of bgfile %s]", tok, efp->linenumber, bgfile);
276
277 x = esl_abc_DigitizeSymbol(bg->abc, *tok);
278 if (fq[x] != -1.0) ESL_XFAIL(eslEFORMAT, errbuf, "already parsed probability of %c [line %d of bgfile %s]", bg->abc->sym[x], efp->linenumber, bgfile);
279 n++;
280
281 status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen);
282 if (status == eslEOL) ESL_XFAIL(eslEFORMAT, errbuf, "premature end of file, expected a probability [line %d of bgfile %s]", efp->linenumber, bgfile);
283 else if (status != eslOK) goto ERROR;
284 if (! esl_str_IsReal(tok)) ESL_XFAIL(eslEFORMAT, errbuf, "expected a probability, saw %s [line %d of bgfile %s]", tok, efp->linenumber, bgfile);
285
286 fq[x] = atof(tok);
287
288 status = esl_fileparser_GetTokenOnLine(efp, &tok, &toklen);
289 if (status == eslOK) ESL_XFAIL(eslEFORMAT, errbuf, "extra unexpected data found [line %d of bgfile %s]", efp->linenumber, bgfile);
290 else if (status != eslEOL) goto ERROR;
291 }
292 if (status != eslEOF) goto ERROR;
293
294 if ( n != bg->abc->K)
295 ESL_XFAIL(eslEFORMAT, errbuf, "expected %d residue frequencies, but found %d in bgfile %s", bg->abc->K, n, bgfile);
296 if ( esl_FCompare_old(esl_vec_FSum(fq, bg->abc->K), 1.0, 0.001) != eslOK)
297 ESL_XFAIL(eslEFORMAT, errbuf, "residue frequencies do not sum to 1.0 in bgfile %s", bgfile);
298
299 /* all checking complete. no more error cases. overwrite bg with the new frequencies */
300 esl_vec_FNorm(fq, bg->abc->K);
301 esl_vec_FCopy(fq, bg->abc->K, bg->f);
302
303 free(fq);
304 esl_fileparser_Close(efp);
305 return eslOK;
306
307 ERROR:
308 if (fq) free(fq);
309 if (efp) esl_fileparser_Close(efp);
310 return status;
311 }
312
313
314 /* Function: p7_bg_Write()
315 * Synopsis: Write a <P7_BG> object to a stream in its save file format.
316 *
317 * Purpose: Write the residue frequencies of <P7_BG> object <bg> to
318 * stream <fp> in save file format. Only the residue
319 * frequencies are written (there are other parts of a
320 * <P7_BG> object, having to do with the bias filter and
321 * biased composition score correction.)
322 *
323 * Returns: <eslOK> on success.
324 *
325 * Throws: <eslEWRITE> on any write error, such as filling the disk.
326 */
327 int
328 p7_bg_Write(FILE *fp, P7_BG *bg)
329 {
330 int x;
331 if (fprintf(fp, "%s\n", esl_abc_DecodeType(bg->abc->type)) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "bg model write failed");
332 for (x = 0; x < bg->abc->K; x++)
333 { if (fprintf(fp, "%c %.5f\n", bg->abc->sym[x], bg->f[x]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "bg model write failed"); }
334 return eslOK;
335 }
336 /*---------------- end, i/o of P7_BG object ---------------------*/
337
338
339 /*****************************************************************
340 * 3. Standard iid null model ("null1")
341 *****************************************************************/
342
343 /* Function: p7_bg_NullOne()
344 *
345 * Purpose: Calculate the null1 lod score, for sequence <dsq>
346 * of length <L> "aligned" to the base null model <bg>.
347 *
348 * Note: Because the residue composition in null1 <bg> is the
349 * same as the background used to calculate residue
350 * scores in profiles and null models, all we have to
351 * do here is score null model transitions.
352 *
353 * Can accept a NULL for *dsq, in which case the returned
354 * value will be (float) L * log(bg->p1) + log(1.-bg->p1);
355 */
356 int
357 p7_bg_NullOne(const P7_BG *bg, const ESL_DSQ *dsq, int L, float *ret_sc)
358 {
359 *ret_sc = (float) L * log(bg->p1) + log(1.-bg->p1);
360 return eslOK;
361 }
362
363
364
365
366
367 /*****************************************************************
368 * 4. Filter null model
369 *****************************************************************/
370
371 /* Function: p7_bg_SetFilter()
372 * Synopsis: Configure filter HMM with new model composition.
373 *
374 * Purpose: The "filter HMM" is an experimental filter in the
375 * acceleration pipeline for avoiding biased composition
376 * sequences. It has no effect on final scoring, if a
377 * sequence passes all steps of the pipeline; it is only
378 * used to eliminate biased sequences from further
379 * consideration early in the pipeline, before the big guns
380 * of domain postprocessing are applied.
381 *
382 * At least at present, it doesn't actually work as well as
383 * one would hope. This will be an area of future work.
384 * What we really want to do is make a better null model of
385 * real protein sequences (and their biases), and incorporate
386 * that model into the flanks (NCJ states) of the profile.
387 *
388 * <compo> is the average model residue composition, from
389 * either the HMM or the copy in a profile or optimized
390 * profile. <M> is the length of the model in nodes.
391 *
392 * The expected length of the filter HMM's generated
393 * sequence is set to a default (about 400). You need a
394 * subsequent call to <p7_bg_SetLength()> to set it to the
395 * target sequence length. In hmmscan, this requires a
396 * call after every new model is read and <p7_pli_NewModel()>
397 * is called, because <NewModel()> is calling <p7_bg_SetFilter()>
398 * to copy the new model's composition <compo>. [Failure to
399 * do this properly was bug #h85, 14 Dec 2010.]
400 *
401 * Returns: <eslOK> on success.
402 *
403 * Throws: (no abnormal error conditions)
404 *
405 * Xref: SRE:J4/25: generalized to use composition vector, not
406 * specifically an HMM.
407 *
408 * Note: This looks like a two-state HMM, but if you start thinking
409 * about its length distribution ("oh my god, L0 assumes a
410 * fixed L=400 expectation, it's all wrong, it's not
411 * conditional on the target sequence length and length
412 * modeling's messed up!"), don't panic. It's set up as a
413 * conditional-on-L model that generates according to P(x |
414 * model, L) P(L); the P(L) term is added in
415 * p7_bg_FilterScore() below.
416 *
417 * Additionally, and not to confuse you further, but the
418 * t[0][0] transition is dependent on L. The initial
419 * setting here is just a dummy. When p7_bg_SetLength()
420 * sets p1 for the null1 model length distribution, it sets
421 * t[0][0] to the same thing. This is controlling the
422 * relative expected balance of background sequence to
423 * biased sequence, not the overall length distribution.
424 *
425 * All of this is ad hoc, and little of it has been
426 * optimized against data.
427 */
428 int
429 p7_bg_SetFilter(P7_BG *bg, int M, const float *compo)
430 {
431 float L0 = 400.0; /* mean length in state 0 of filter HMM (normal background) */
432 float L1 = (float) M / 8.0; /* mean length in state 1 of filter HMM (biased segment) */
433
434 /* State 0 is the normal iid model. */
435 bg->fhmm->t[0][0] = L0 / (L0+1.0f);
436 bg->fhmm->t[0][1] = 1.0f / (L0+1.0f);
437 bg->fhmm->t[0][2] = 1.0f; /* 1.0 transition to E means we'll set length distribution externally. */
438 esl_vec_FCopy(bg->f, bg->abc->K, bg->fhmm->e[0]);
439
440 /* State 1 is the potentially biased model composition. */
441 bg->fhmm->t[1][0] = 1.0f / (L1+1.0f);
442 bg->fhmm->t[1][1] = L1 / (L1+1.0f);
443 bg->fhmm->t[1][2] = 1.0f; /* 1.0 transition to E means we'll set length distribution externally. */
444 esl_vec_FCopy(compo, bg->abc->K, bg->fhmm->e[1]);
445
446 bg->fhmm->pi[0] = 0.999;
447 bg->fhmm->pi[1] = 0.001;
448
449 esl_hmm_Configure(bg->fhmm, bg->f);
450 return eslOK;
451 }
452
453
454 /* Function: p7_bg_FilterScore()
455 * Synopsis: Calculates the filter null model score.
456 *
457 * Purpose: Calculates the filter null model <bg> score for sequence
458 * <dsq> of length <L>, and return it in
459 * <*ret_sc>.
460 *
461 * The score is calculated as an HMM Forward score using
462 * the two-state filter null model. It is a log-odds ratio,
463 * relative to the iid background frequencies, in nats:
464 * same as main model Forward scores.
465 *
466 * The filter null model has no length distribution of its
467 * own; the same geometric length distribution (controlled
468 * by <bg->p1>) that the null1 model uses is imposed.
469 */
470 int
471 p7_bg_FilterScore(P7_BG *bg, const ESL_DSQ *dsq, int L, float *ret_sc)
472 {
473 ESL_HMX *hmx = esl_hmx_Create(L, bg->fhmm->M); /* optimization target: this can be a 2-row matrix, and it can be stored in <bg>. */
474 float nullsc; /* (or it could be passed in as an arg, but for sure it shouldn't be alloc'ed here */
475
476 esl_hmm_Forward(dsq, L, bg->fhmm, hmx, &nullsc);
477
478 /* impose the length distribution */
479 *ret_sc = nullsc + (float) L * logf(bg->p1) + logf(1.-bg->p1);
480 esl_hmx_Destroy(hmx);
481 return eslOK;
482 }
483
484
485
486
487 /*****************************************************************
488 * 5. Benchmark driver
489 *****************************************************************/
490 #ifdef p7BG_BENCHMARK
491 /*
492 gcc -O2 -Wall -msse2 -std=gnu99 -o p7_bg_benchmark -I. -L. -I../easel -L../easel -Dp7BG_BENCHMARK p7_bg.c -lhmmer -leasel -lm
493 ./p7_bg_benchmark <hmmfile>
494 */
495 #include <p7_config.h>
496
497 #include "easel.h"
498 #include "esl_alphabet.h"
499 #include "esl_getopts.h"
500 #include "esl_sq.h"
501 #include "esl_stopwatch.h"
502
503 #include "hmmer.h"
504
505 static ESL_OPTIONS options[] = {
506 /* name type default env range toggles reqs incomp help docgroup*/
507 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
508 { "-L", eslARG_INT, "400", NULL, "n>0", NULL, NULL, NULL, "length of random target seqs", 0 },
509 { "-N", eslARG_INT, "100", NULL, "n>0", NULL, NULL, NULL, "number of random target seqs", 0 },
510 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
511 };
512 static char usage[] = "[-options] <hmmfile>";
513 static char banner[] = "benchmark timing for calculating null model scores";
514
515 int
516 main(int argc, char **argv)
517 {
518 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
519 ESL_STOPWATCH *w = esl_stopwatch_Create();
520 char *hmmfile = esl_opt_GetArg(go, 1);
521 ESL_ALPHABET *abc = NULL;
522 P7_HMMFILE *hfp = NULL;
523 P7_HMM *hmm = NULL;
524 P7_BG *bg = NULL;
525 int L = esl_opt_GetInteger(go, "-L");
526 int N = esl_opt_GetInteger(go, "-N");
527 int i;
528
529 /* Read one HMM from <hmmfile> */
530 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
531 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
532 p7_hmmfile_Close(hfp);
533
534 bg = p7_bg_Create(abc);
535
536 esl_stopwatch_Start(w);
537 for (i = 0; i < N; i++)
538 p7_bg_SetFilterByHMM(bg, hmm);
539 esl_stopwatch_Stop(w);
540 esl_stopwatch_Display(stdout, w, "# CPU time: ");
541
542 p7_bg_Destroy(bg);
543 p7_hmm_Destroy(hmm);
544 esl_alphabet_Destroy(abc);
545 esl_stopwatch_Destroy(w);
546 esl_getopts_Destroy(go);
547 return 0;
548 }
549 #endif /*p7BG_BENCHMARK*/
550
551
552 /*****************************************************************
553 * 6. Unit tests
554 *****************************************************************/
555 #ifdef p7BG_TESTDRIVE
556 #include "esl_dirichlet.h"
557 #include "esl_random.h"
558
559 static void
560 utest_ReadWrite(ESL_RANDOMNESS *rng)
561 {
562 char msg[] = "bg Read/Write unit test failed";
563 char tmpfile[32] = "esltmpXXXXXX";
564 FILE *fp = NULL;
565 ESL_ALPHABET *abc = NULL; /* random alphabet choice eslRNA..eslDICE */
566 float *fq = NULL;
567 P7_BG *bg = NULL;
568
569 if ((abc = esl_alphabet_Create(esl_rnd_Roll(rng, 5) + 1)) == NULL) esl_fatal(msg);
570 if (( bg = p7_bg_Create(abc)) == NULL) esl_fatal(msg);
571 if (( fq = malloc(sizeof(float) * abc->K)) == NULL) esl_fatal(msg);
572 do {
573 if (esl_dirichlet_FSampleUniform(rng, abc->K, fq) != eslOK) esl_fatal(msg);
574 } while (esl_vec_FMin(fq, abc->K) < 0.001); /* small p's will get rounded off and fail FCompare() */
575 esl_vec_FCopy(fq, abc->K, bg->f);
576
577 if (esl_tmpfile_named(tmpfile, &fp) != eslOK) esl_fatal(msg);
578 if ( p7_bg_Write(fp, bg) != eslOK) esl_fatal(msg);
579 fclose(fp);
580
581 esl_vec_FSet(bg->f, bg->abc->K, 0.0);
582 if ( p7_bg_Read(tmpfile, bg, NULL) != eslOK) esl_fatal(msg);
583 if ( esl_vec_FCompare(fq, bg->f, bg->abc->K, 0.01) != eslOK) esl_fatal(msg);
584
585 p7_bg_Destroy(bg);
586 esl_alphabet_Destroy(abc);
587 free(fq);
588 remove(tmpfile);
589 }
590 #endif /*p7BG_TESTDRIVE*/
591
592
593 /*****************************************************************
594 * 7. Test driver
595 *****************************************************************/
596
597 #ifdef p7BG_TESTDRIVE
598 #include <esl_config.h>
599
600 #include <stdio.h>
601
602 #include "easel.h"
603 #include "esl_getopts.h"
604 #include "esl_random.h"
605
606 #include "hmmer.h"
607
608 static ESL_OPTIONS options[] = {
609 /* name type default env range togs reqs incomp help docgrp */
610 {"-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0},
611 {"-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0},
612 {"-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show verbose commentary/output", 0},
613 { 0,0,0,0,0,0,0,0,0,0},
614 };
615 static char usage[] = "[-options]";
616 static char banner[] = "test driver for p7_bg";
617
618 int
619 main(int argc, char **argv)
620 {
621 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage);
622 ESL_RANDOMNESS *rng = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
623 int be_verbose = esl_opt_GetBoolean(go, "-v");
624
625 if (be_verbose) printf("p7_bg unit test: rng seed %" PRIu32 "\n", esl_randomness_GetSeed(rng));
626
627 utest_ReadWrite(rng);
628
629 esl_randomness_Destroy(rng);
630 esl_getopts_Destroy(go);
631 return 0;
632 }
633 #endif /* p7BG_TESTDRIVE */
634
635
636 /*****************************************************************
637 * 8. Examples
638 *****************************************************************/
639 #ifdef p7BG_EXAMPLE
640 /*
641 gcc -O2 -Wall -msse2 -std=gnu99 -o p7_bg_example -I. -L. -I../easel -L../easel -Dp7BG_EXAMPLE p7_bg.c -lhmmer -leasel -lm
642 ./p7_bg_example <hmmfile> <seqfile>
643 */
644 #include <p7_config.h>
645
646 #include "easel.h"
647 #include "esl_alphabet.h"
648 #include "esl_getopts.h"
649 #include "esl_sq.h"
650 #include "esl_sqio.h"
651
652 #include "hmmer.h"
653
654 static ESL_OPTIONS options[] = {
655 /* name type default env range toggles reqs incomp help docgroup*/
656 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
657 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
658 };
659 static char usage[] = "[-options] <hmmfile> <seqfile>";
660 static char banner[] = "example of calculating null model scores";
661
662 int
663 main(int argc, char **argv)
664 {
665 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 2, argc, argv, banner, usage);
666 char *hmmfile = esl_opt_GetArg(go, 1);
667 char *seqfile = esl_opt_GetArg(go, 2);
668 ESL_ALPHABET *abc = NULL;
669 P7_HMMFILE *hfp = NULL;
670 P7_HMM *hmm = NULL;
671 P7_BG *bg = NULL;
672 ESL_SQFILE *sqfp = NULL;
673 int format = eslSQFILE_UNKNOWN;
674 ESL_SQ *sq = NULL;
675 float nullsc, filtersc, H;
676 int status;
677
678 /* Read one HMM from <hmmfile> */
679 if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile);
680 if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM");
681 p7_hmmfile_Close(hfp);
682
683 /* Open <seqfile> for reading */
684 status = esl_sqfile_OpenDigital(abc, seqfile, format, NULL, &sqfp);
685 if (status == eslENOTFOUND) esl_fatal("No such file.");
686 else if (status == eslEFORMAT) esl_fatal("Format unrecognized.");
687 else if (status != eslOK) esl_fatal("Open failed, code %d.", status);
688
689 sq = esl_sq_CreateDigital(abc);
690 bg = p7_bg_Create(abc);
691
692 p7_bg_SetFilter(bg, hmm->M, hmm->compo);
693
694 H = esl_vec_FEntropy(bg->f, bg->abc->K);
695 printf("bg iid H = %.4f\n", H);
696
697 H = esl_vec_FEntropy(hmm->compo, bg->abc->K);
698 printf("modelcomp H = %.4f\n", H);
699
700 while ((status = esl_sqio_Read(sqfp, sq)) == eslOK)
701 {
702 p7_bg_SetLength(bg, sq->n);
703
704 p7_bg_NullOne (bg, sq->dsq, sq->n, &nullsc);
705 p7_bg_FilterScore(bg, sq->dsq, sq->n, &filtersc);
706
707 printf("%-20s %5d %8.5f %8.5f %8.5f\n", sq->name, (int) sq->n, nullsc, filtersc, filtersc-nullsc);
708
709 esl_sq_Reuse(sq);
710 }
711 if (status == eslEFORMAT) esl_fatal("Parse failed (sequence file %s)\n%s\n",
712 sqfp->filename, sqfp->get_error(sqfp));
713 else if (status != eslEOF) esl_fatal("Unexpected error %d reading sequence file %s",
714 status, sqfp->filename);
715
716 esl_sqfile_Close(sqfp);
717 esl_sq_Destroy(sq);
718 p7_bg_Destroy(bg);
719 p7_hmm_Destroy(hmm);
720 esl_alphabet_Destroy(abc);
721 esl_getopts_Destroy(go);
722 return 0;
723 }
724 #endif /*p7BG_EXAMPLE*/
725
726 #ifdef p7BG_EXAMPLE2
727 #include <stdio.h>
728 #include "easel.h"
729 #include "esl_alphabet.h"
730 #include "hmmer.h"
731
732 int
733 main(int argc, char **argv)
734 {
735 char *bgfile = argv[1];
736 char *alphabet = argv[2];
737 ESL_ALPHABET *abc = esl_alphabet_Create(esl_abc_EncodeType(alphabet));
738 P7_BG *bg = p7_bg_Create(abc);
739 char errbuf[eslERRBUFSIZE];
740 int status;
741
742 status = p7_bg_Read(bgfile, bg, errbuf);
743 if (status == eslENOTFOUND) esl_fatal("open failed: %s", errbuf);
744 else if (status == eslEFORMAT) esl_fatal("parse failed: %s", errbuf);
745 else if (status != eslOK) esl_fatal("failed to read bg file %s (error %d)\n", bgfile, status);
746
747 p7_bg_Write(stdout, bg);
748 return 0;
749 }
750 #endif /*p7BG_EXAMPLE2*/
751
752
753
754
755