Debian Codegraph @

src/bandcyk-truncation-test.c

raw symbols SCIP
1 /* bandcyk-truncation-test.c
2 * SRE, Tue Oct 21 17:07:56 2003 [St. Louis]
3 * Adapted from check-bandtruncation.c; xref notebook/1015-infernal-bands, STL7 p. 130.
4 * Easelfied: EPN, Fri Nov 30 10:04:16 2007
5 *
6 * Implements two tests of the truncation error:
7 * 1. Calculate bands for two different sizes of Z; verify that bands
8 * are identical.
9 *
10 * 2. Verifies that the geometric decay assumption used to estimate
11 * the truncation error was indeed an upper bound, by comparing
12 * the predicted density from a shorter length Z1 to the observed
13 * density for a longer length Z2, in the interval n=Z1+1..Z2.
14 *
15 * Band calculation is supposed to be guaranteed to be independent
16 * of input Z -- the BandCalculationEngine() returns an error whenever
17 * the input Z is too short to guarantee this. If truncation error calculation
18 * in BandCalculationEngine() is wrong, bands may differ.
19 *
20 * Example tests;
21 * using models built w/ --rf from alignments in intro/ subdirectory:
22 * ./bandcyk-truncation-test trna.cm 160 1000
23 * ./bandcyk-truncation-test rp.cm 525 2000
24 * ./bandcyk-truncation-test ssu.cm 1687 3000
25 *
26 * xref STL7 p.130.
27 */
28
29 #include <esl_config.h>
30 #include <p7_config.h>
31 #include "config.h"
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <math.h>
36 #include <string.h>
37
38 #include "easel.h"
39 #include "esl_getopts.h"
40 #include "esl_vectorops.h"
41
42 #include "hmmer.h"
43
44 #include "infernal.h"
45
46 static ESL_OPTIONS options[] = {
47 /* name type default env range toggles reqs incomp help docgroup*/
48 { "-h", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
49 { "--betaW", eslARG_REAL, "1E-5", NULL, "x>0.",NULL, NULL, NULL, "set tail probability thresh for W calculation to <x>", 0 },
50 { "--beta1", eslARG_REAL, "1E-5", NULL, "x>0.",NULL, NULL, NULL, "set tail probability thresh for dmin1/dmax1 to <x>", 0 },
51 { "--beta2", eslARG_REAL, "1E-6", NULL, "x>0.",NULL, NULL, NULL, "set tail probability thresh for dmin2/dmax2 to <x>", 0 },
52 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
53 };
54 static char usage[] = "[-options] <cmfile> <Z1> <Z2> where width Z1 < Z2.";
55 static char banner[] = "Check of truncation error calculation in BandCalculationEngine()";
56
57 int
58 main(int argc, char **argv)
59 {
60 int status;
61 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 3, argc, argv, banner, usage);
62
63 char *cmfile = esl_opt_GetArg(go, 1);
64 int Z1 = atoi(esl_opt_GetArg(go, 2));
65 int Z2 = atoi(esl_opt_GetArg(go, 3));
66 CM_FILE *cmfp; /* open CM file for reading */
67 CM_t *cm; /* a covariance model */
68 int v; /* counter over states */
69 int n; /* counter over lengths 0..Z */
70
71 double betaW; /* tail probability threshold for calc'ing W */
72 double beta1; /* tail probability threshold for dmin1/dmax1 */
73 double beta2; /* tail probability threshold for dmin2/dmax2 */
74 double **gamma1, **gamma2; /* probability densities from pass 1, 2 */
75 double gbeta1; /* geometric decay constant, truncation error calc */
76 double gbeta2; /* geometric decay constant, truncation error calc */
77 double g; /* log of an estimated gamma[n] */
78 ESL_ALPHABET *abc = NULL;
79 char errbuf[eslERRBUFSIZE]; /* for error messages */
80 CM_QDBINFO *qdbinfo1 = NULL; /* holds dmin1/dmax1 and dmin2/dmax2 for first choice of Z */
81 CM_QDBINFO *qdbinfo2 = NULL; /* holds dmin1/dmax1 and dmin2/dmax2 for second choice of Z */
82 int W1; /* W calculated with qdbinfo1 */
83 int W2; /* W calculated with qdbinfo2 */
84
85 /***********************************************
86 * Parse command line
87 ***********************************************/
88
89 betaW = esl_opt_GetReal (go, "--betaW");
90 beta1 = esl_opt_GetReal (go, "--beta1");
91 beta2 = esl_opt_GetReal (go, "--beta2");
92
93 if (Z1 >= Z2) cm_Fail("Please set a width Z1 < width Z2, else the check won't work right.");
94
95 /* Get our CM
96 */
97
98 if ((status = cm_file_Open(cmfile, NULL, FALSE, &(cmfp), errbuf)) != eslOK) cm_Fail("Failed to open covariance model save file %s\n", cmfile);
99 if ((cm_file_Read(cmfp, TRUE, &abc, &cm)) != eslOK) cm_Fail("Failed to read CM");
100 cm_file_Close(cmfp);
101
102 qdbinfo1 = CreateCMQDBInfo(cm->M, cm->clen);
103 qdbinfo2 = CreateCMQDBInfo(cm->M, cm->clen);
104 qdbinfo1->beta1 = beta1;
105 qdbinfo1->beta2 = beta2;
106 qdbinfo2->beta1 = beta1;
107 qdbinfo2->beta2 = beta2;
108
109 /* Do two band calculations with the different Z's.
110 * Save the gamma_0 densities for root state 0.
111 * Each band calculation calculates two sets of bounds:
112 * qdbinfo->dmin1 and qdbinfo->dmax1 using beta = qdbinfo->beta1
113 * qdbinfo->dmin2 and qdbinfo->dmax2 using beta = qdbinfo->beta2
114 */
115 if ((status = BandCalculationEngine(cm, Z1, qdbinfo1, betaW, &W1, &gamma1, NULL, NULL)) != eslOK)
116 cm_Fail("Your Z1 (%d) must be too small, sorry.\n", Z1);
117 if ((status = BandCalculationEngine(cm, Z2, qdbinfo2, betaW, &W2, &gamma2, NULL, NULL)) != eslOK)
118 cm_Fail("Your Z2 (%d) must be too small, sorry.\n", Z1);
119
120 if (W1 != W2) cm_Fail("failed, W1 != W2 %d != %d\n", W1, W2);
121
122 /* Verify that the bands are all identical, regardless of choice of Z.
123 */
124 for (v = 0; v < cm->M; v++) {
125 if (qdbinfo1->dmin1[v] != qdbinfo2->dmin1[v] || qdbinfo1->dmax1[v] != qdbinfo2->dmax1[v]) {
126 cm_Fail("failed at v=%d: dmin1/dmax1 Band for Z1=%d is %d..%d; for Z2=%d is %d..%d\n",
127 v, Z1, qdbinfo1->dmin1[v], qdbinfo1->dmax1[v], Z2, qdbinfo2->dmin1[v], qdbinfo2->dmax1[v]);
128 }
129 if (qdbinfo1->dmin2[v] != qdbinfo2->dmin2[v] || qdbinfo1->dmax2[v] != qdbinfo2->dmax2[v]) {
130 cm_Fail("failed at v=%d: dmin2/dmax2 Band for Z1=%d is %d..%d; for Z2=%d is %d..%d\n",
131 v, Z1, qdbinfo1->dmin2[v], qdbinfo1->dmax2[v], Z2, qdbinfo2->dmin2[v], qdbinfo2->dmax2[v]);
132 }
133 }
134
135 /* Verify that the geometric decay predicted for the shorter width Z1
136 * was indeed an upper bound for what we saw in the longer width Z2,
137 * in the interval Z1+1..Z2.
138 */
139 if (! BandTruncationNegligible(gamma1[0], qdbinfo1->dmax1[0], Z1, &gbeta1))
140 cm_Fail("shouldn't happen, because we already checked this in the Engine().");
141 if (! BandTruncationNegligible(gamma1[0], qdbinfo1->dmax2[0], Z1, &gbeta2))
142 cm_Fail("shouldn't happen, because we already checked this in the Engine().");
143 for (n = Z1+1; n <= Z2; n++)
144 {
145 g = log(gamma1[0][Z1]) + (n-Z1) * log(gbeta1); /* g = log(estimated gamma[n]); should be upper bound */
146 if (g < log(gamma2[0][n]))
147 cm_Fail("truncation error test failed: geometric is not an upper bound on tail of gamma[n]");
148 g = log(gamma1[0][Z1]) + (n-Z1) * log(gbeta2); /* g = log(estimated gamma[n]); should be upper bound */
149 if (g < log(gamma2[0][n]))
150 cm_Fail("truncation error test failed: geometric is not an upper bound on tail of gamma[n]");
151 }
152
153 /* Clean up and exit.
154 */
155 FreeBandDensities(cm, gamma1);
156 FreeBandDensities(cm, gamma2);
157 FreeCMQDBInfo(qdbinfo1);
158 FreeCMQDBInfo(qdbinfo2);
159 FreeCM(cm);
160 esl_alphabet_Destroy(abc);
161 esl_getopts_Destroy(go);
162 exit(0);
163 }