Debian Codegraph @

src/cp9-test.c

raw symbols SCIP
1 /* cp9-test.c
2 * EPN, Mon Nov 13 17:49:07 2006
3 * Easelification: EPN, Fri Nov 30 10:14:17 2007
4 *
5 * Test the CM -> CP9 HMM construction procedure.
6 */
7
8 #include <esl_config.h>
9 #include <p7_config.h>
10 #include "config.h"
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <math.h>
15 #include <string.h>
16 #include <time.h>
17
18 #include "easel.h"
19 #include "esl_getopts.h"
20 #include "esl_random.h"
21 #include "esl_stopwatch.h"
22 #include "esl_stack.h"
23 #include "esl_vectorops.h"
24
25 #include "hmmer.h"
26
27 #include "infernal.h"
28
29 static ESL_OPTIONS options[] = {
30 /* name type default env range toggles reqs incomp help docgroup*/
31 { "-h", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
32 { "-s", eslARG_INT, NULL, NULL, "n>0", NULL, NULL, NULL, "set random number seed to <n>", 0 },
33 { "-t", eslARG_REAL, "1E-4",NULL, "x>0.",NULL, NULL, NULL, "probability threshold for reporting violations", 0 },
34 { "--psionly", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "only check that psi and phi values match", 1 },
35 { "--nseq", eslARG_INT, "500000",NULL, "n>=1000", NULL, NULL, NULL, "use <n> samples to build CP9 HMM from", 1 },
36 { "--chi", eslARG_REAL, ".01", NULL, "x>0.",NULL, NULL, NULL, "fail sampling check if any chi-square test < <f>", 1},
37 { "--dlev", eslARG_INT, NULL, NULL, "0<n<4",NULL, NULL, NULL, "set verbosity of debugging print statements to <n>", 1},
38 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
39 };
40 static char usage[] = "[-options] <cmfile>";
41 static char banner[] = "test CP9 HMM construction procedure";
42
43 int
44 main(int argc, char **argv)
45 {
46 int status;
47 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 1, argc, argv, banner, usage);
48 char *cmfile = esl_opt_GetArg(go, 1);
49 CM_FILE *cmfp; /* open CM file for reading */
50 CM_t *cm; /* a covariance model */
51 CP9_t *hmm; /* constructed CP9 HMM; written to hmmfile */
52 CP9Map_t *cp9map; /* maps the hmm to the cm and vice versa */
53 double pthresh; /* psi threshold for calling violations */
54 int nsamples; /* Number of samples to build sampled HMM check. */
55 float chi_thresh; /* if any chi-square test during the sampling check
56 * is below this threshold, fail. */
57 int do_psionly; /* don't do a sampling check only compare the expected
58 * number of times each HMM and CM state is entered */
59 int debug_level; /* verbosity of debugging printf statements */
60 ESL_STOPWATCH *w = NULL; /* for timings */
61 ESL_RANDOMNESS *r = NULL; /* source of randomness */
62 ESL_ALPHABET *abc = NULL; /* alphabet, for the CM */
63 char errbuf[eslERRBUFSIZE]; /* for error messages */
64
65 /***********************************************
66 * Parse command line
67 ***********************************************/
68 pthresh = esl_opt_GetReal (go, "-t");
69 do_psionly = esl_opt_GetBoolean(go, "--psionly");
70 nsamples = esl_opt_GetInteger(go, "--nseq");
71 chi_thresh = esl_opt_GetReal (go, "--chi");
72 if(esl_opt_IsOn(go, "--dlev")) debug_level = esl_opt_GetInteger(go, "--dlev");
73 else debug_level = 0;
74
75 /********************************************`***
76 * Preliminaries: get our CM
77 ***********************************************/
78
79 if ((status = cm_file_Open(cmfile, NULL, FALSE, &(cmfp), errbuf)) != eslOK) cm_Fail("Failed to open covariance model save file %s\n", cmfile);
80 if ((cm_file_Read(cmfp, TRUE, &abc, &cm)) != eslOK) cm_Fail("Failed to read CM");
81 cm_file_Close(cmfp);
82 if((status = CMLogoddsify(cm)) != eslOK) cm_Fail("problem logoddisfying the CM");
83
84 w = esl_stopwatch_Create();
85 esl_stopwatch_Start(w);
86 if((status = build_cp9_hmm(cm, errbuf, TRUE, pthresh, debug_level, &hmm, &cp9map)) != eslOK) cm_Fail(errbuf);
87 esl_stopwatch_Stop(w);
88 esl_stopwatch_Display(stdout, w, "CP9 construction CPU time: ");
89
90 esl_stopwatch_Start(w);
91 CP9Logoddsify(hmm);
92 esl_stopwatch_Stop(w);
93 esl_stopwatch_Display(stdout, w, "CP9 score calc CPU time: ");
94
95 if(!do_psionly) {
96 esl_stopwatch_Start(w);
97 if ( esl_opt_IsOn(go, "-s")) r = esl_randomness_Create((long) esl_opt_GetInteger(go, "-s"));
98 else r = esl_randomness_CreateTimeseeded();
99
100 if((status = CP9_check_by_sampling(cm, hmm, errbuf, r, NULL, 1, hmm->M, chi_thresh, nsamples, debug_level)) != eslOK) cm_Fail(errbuf);
101 printf("CP9 HMM passed sampling check.\n");
102
103 esl_stopwatch_Stop(w);
104 esl_stopwatch_Display(stdout, w, "CP9 sampling check CPU time: ");
105 }
106 /* clean up and exit */
107 FreeCP9Map(cp9map);
108 FreeCPlan9(hmm);
109 FreeCM(cm);
110 esl_alphabet_Destroy(abc);
111 esl_randomness_Destroy(r);
112 esl_stopwatch_Destroy(w);
113 esl_getopts_Destroy(go);
114 return 0;
115 }