Debian Codegraph @

src/bandcyk-montecarlo-test.c

raw symbols SCIP
1 /* bandcyk-montecarlo-test.c
2 * SRE, Tue Oct 21 17:00:44 2003
3 * From notebook/1015-infernal-bands; xref STL7 p.130
4 * Easelfied: EPN, Fri Nov 30 09:48:32 2007
5 *
6 * Produce densities both by Monte Carlo and by calculation engine.
7 * Compare each density distribution by chi-square test.
8 *
9 * Small numbers (close to zero) indicate that the two densities
10 * are significantly different.
11 *
12 * If any chi-square is less than a threshold (0.01), fail.
13 */
14
15 #include <esl_config.h>
16 #include <p7_config.h>
17 #include "config.h"
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <math.h>
22 #include <string.h>
23 #include <time.h>
24
25 #include "easel.h"
26 #include "esl_getopts.h"
27 #include "esl_stats.h"
28 #include "esl_vectorops.h"
29
30 #include "hmmer.h"
31
32 #include "infernal.h"
33
34 static ESL_OPTIONS options[] = {
35 /* name type default env range toggles reqs incomp help docgroup*/
36 { "-h", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
37 { "-n", eslARG_INT, "10000", NULL, NULL, NULL, NULL, NULL, "number of monte carlo samples to do", 0 },
38 { "-s", eslARG_INT, NULL, NULL, "n>0", NULL, NULL, NULL, "set random number seed for Monte Carlo to <n>", 0 },
39 { "-t", eslARG_REAL, "0.01",NULL, "x>0.",NULL, NULL, NULL, "threshold for rejecting hypothesis that distros are identical ", 0 },
40 { "-Z", eslARG_INT, "1000", NULL, "n>0", NULL, NULL, NULL, "set maximum Z (subseq length) to <n>", 0 },
41 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
42 };
43 static char usage[] = "[-options] <cmfile>";
44 static char banner[] = "Monte Carlo sampling test program for query-dependent band calculation";
45
46 static double DChiSquareFit(double *d1, double *d2, int N);
47
48 int
49 main(int argc, char **argv)
50 {
51 int status;
52 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 1, argc, argv, banner, usage);
53 char *cmfile = esl_opt_GetArg(go, 1);
54 CM_FILE *cmfp; /* open CM file for reading */
55 CM_t *cm; /* a covariance model */
56 int v; /* counter over states */
57
58 double **mc_gamma; /* densities from monte carlo */
59 double **gamma; /* densities from calculation engine */
60 double *gamma0_glb; /* gamma[0] if local begins were not used */
61 double p; /* p from chi squared test */
62 double threshold; /* probability threshold for rejecting */
63
64 int maxZ; /* maximum length that densities are calc'ed for */
65 int mc_nsample; /* # of monte carlo samples to do */
66 ESL_RANDOMNESS *r = NULL;
67 ESL_ALPHABET *abc = NULL;
68 char errbuf[eslERRBUFSIZE]; /* for error messages */
69
70 /***********************************************
71 * Parse command line
72 ***********************************************/
73
74 maxZ = esl_opt_GetInteger(go, "-Z");
75 mc_nsample = esl_opt_GetInteger(go, "-n");
76 threshold = esl_opt_GetReal (go, "-t");
77
78 /* create RNG */
79 if ( esl_opt_IsOn(go, "-s")) r = esl_randomness_Create((long) esl_opt_GetInteger(go, "-s"));
80 else r = esl_randomness_CreateTimeseeded();
81
82 /***********************************************
83 * Preliminaries: get our CM
84 ***********************************************/
85
86 if ((status = cm_file_Open(cmfile, NULL, FALSE, &(cmfp), errbuf)) != eslOK) cm_Fail("Failed to open covariance model save file %s\n", cmfile);
87 if ((cm_file_Read(cmfp, TRUE, &abc, &cm)) != eslOK) cm_Fail("Failed to read CM");
88 cm_file_Close(cmfp);
89
90 /*****************************************************************
91 * Do the band calculations
92 ****************************************************************/
93
94 /* BandMonteCarlo() collects "density" as unnormalized counts
95 */
96 if (! BandMonteCarlo(cm, mc_nsample, maxZ, &mc_gamma))
97 cm_Fail("Your maxZ (%d) must be too small, sorry...\n", maxZ);
98
99 /* BandCalculationEngine() calculates a real density for each state v
100 */
101 if ((status = BandCalculationEngine(cm, maxZ, NULL, 0.001, NULL, &gamma, NULL, &gamma0_glb)) != eslOK)
102 cm_Fail("Your maxZ (%d) must be too small, sorry...\n", maxZ);
103
104 for (v = 0; v < cm->M; v++)
105 {
106 if(v == 0) { /* gamma[0] was calc'ed with local begins on, gamma0_glb is what we want to use */
107 esl_vec_DScale(gamma0_glb, maxZ+1, esl_vec_DSum(mc_gamma[v], maxZ+1)); /* convert to #'s */
108 p = DChiSquareFit(gamma0_glb, mc_gamma[v], maxZ+1); /* compare #'s */
109 }
110 else {
111 esl_vec_DScale(gamma[v], maxZ+1, esl_vec_DSum(mc_gamma[v], maxZ+1)); /* convert to #'s */
112 p = DChiSquareFit(gamma[v], mc_gamma[v], maxZ+1); /* compare #'s */
113 }
114
115 if (cm->sttype[v] != E_st
116 && cm->ndtype[cm->ndidx[v]+1] != END_nd /* skip nodes with unreachable inserts */
117 && p < threshold)
118 cm_Fail("Rejected band distribution for state %d: chi-squared p = %f\n", v, p);
119 }
120 FreeBandDensities(cm, mc_gamma);
121 FreeBandDensities(cm, gamma);
122 free(gamma0_glb);
123 FreeCM(cm);
124 esl_alphabet_Destroy(abc);
125 esl_randomness_Destroy(r);
126 esl_getopts_Destroy(go);
127
128 exit(0);
129 }
130
131 static double
132 DChiSquareFit(double *d1, double *d2, int N)
133 {
134 int i;
135 double diff;
136 double chisq = 0.0;
137 int n;
138 double qax;
139
140 n = 0;
141 for (i = 0; i < N; i++)
142 {
143 if (d1[i] == 0. && d2[i] == 0.) continue;
144 diff = d1[i] - d2[i];
145 chisq += diff * diff / (d1[i]+d2[i]);
146 n++;
147 }
148
149 if (n > 1) {
150 if(esl_stats_IncompleteGamma(((double) n-1.)/2., chisq/2., NULL, &qax) != eslOK)
151 cm_Fail("DChiSquareFit() call to esl_stats_IncompleteGamma() failed.");
152 return qax;
153 }
154 else return -1.;
155 }