Debian Codegraph @

src/sub_cm-test.c

raw symbols SCIP
1 /* sub_cm-test.c
2 * EPN, 09.18.06
3 * Easeled: EPN, Fri Nov 30 13:35:14 2007
4 *
5 * Build many submodels from a template CM by choosing
6 * random model start and end positions.
7 * Compare the submodels to the corresponding stretch of
8 * main model by determining the expected number of
9 * times each state is entered.
10 *
11 */
12
13 #include <esl_config.h>
14 #include <p7_config.h>
15 #include "config.h"
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <math.h>
20 #include <string.h>
21 #include <time.h>
22
23 #include "easel.h"
24 #include "esl_getopts.h"
25 #include "esl_vectorops.h"
26
27 #include "hmmer.h"
28
29 #include "infernal.h"
30
31 static ESL_OPTIONS options[] = {
32 /* name type default env range toggles reqs incomp help docgroup*/
33 { "-h", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
34 { "-n", eslARG_INT, "100", NULL, "n>0", NULL, NULL, NULL, "number of sub CMs to build and test", 0 },
35 { "-s", eslARG_INT, NULL, NULL, "n>0", NULL, NULL, NULL, "set random number seed to <n>", 0 },
36 { "-b", eslARG_INT, NULL, NULL, "n>0", NULL, NULL, "--exhaust", "set sub CM begin consensus (match) column as <n>", 0 },
37 { "-e", eslARG_INT, NULL, NULL, "n>0", NULL, NULL, "--exhaust", "set sub CM end consensus (match) column as <n>", 0 },
38 { "-t", eslARG_REAL, "1E-5",NULL, "x>0.",NULL, NULL, NULL, "probability threshold for reporting violations", 0 },
39 { "--psionly", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "only check that psi values match (don't build HMMs)", 1 },
40 { "--sample", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "build and check two CP9 HMMs (one an ML HMM via sampling)", 1 },
41 { "--nseq", eslARG_INT, "50000", NULL,"n>=10000",NULL,"--sample", NULL, "use <n> samples to build ML HMM for --sample", 1 },
42 { "--chi", eslARG_REAL, ".01", NULL, "x>0.",NULL, NULL, NULL, "fail sampling check if any chi-square test < <f>", 1 },
43 { "--exhaust", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "build and check every possible sub CM (all (N^2+N)/2)", 1 },
44 { "--debug", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "turn debugging print statements ON", 1 },
45 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
46 };
47 static char usage[] = "[-options] <cmfile>";
48 static char banner[] = "test sub CM construction procedure";
49
50 int
51 main(int argc, char **argv)
52 {
53 int status;
54 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 1, argc, argv, banner, usage);
55 char *cmfile = esl_opt_GetArg(go, 1);
56 CM_FILE *cmfp; /* open CM file for reading */
57 CM_t *cm; /* a covariance model */
58 CM_t *sub_cm; /* sub covariance model */
59 int nmodels; /* number of sub CMs to build */
60 int sstruct; /* start position for sub CM */
61 int estruct; /* end position for sub CM */
62 int i; /* counter over sub CMs */
63 int j; /* counter */
64 int temp;
65
66 double pthresh; /* psi threshold for calling violations */
67 int begin_set; /* TRUE if -b entered at command line */
68 int end_set; /* TRUE if -e entered at command line */
69 int do_atest; /* TRUE to build 2 ML HMMs, one from the CM and one from
70 * the sub_cm, analytically, and check to make sure
71 * the corresponding parameters of these two HMMS
72 * are within 'threshold' of each other.
73 */
74 int do_stest; /* TRUE to build an ML HMM from a truncated MSA emitted from the
75 * original CM and test it via chi-squared tests against
76 * the CP9 analytically built from the sub_cm.
77 */
78 int do_exhaust; /* TRUE to build every possible sub_cm */
79 int ndone; /* number of models built so far */
80 int print_flag; /* TRUE to print debug statements */
81 int *awrong_total_ct; /* For ALL 'atest's: the number of times we predict we'll
82 * fail the test for an HMM node and we get it right, for
83 * each of 6 cases - 6 different reasons we predict we'll fail.
84 */
85 int *swrong_total_ct; /* For ALL sampling tests: the number of times we predict we'll
86 * fail the test for an HMM node and we get it right, for
87 * each of 6 cases - 6 different reasons we predict we'll fail.
88 */
89 int *apredict_total_ct; /* For ALL 'atest's: the number of times we predict we'll
90 * fail the test for an HMM node for
91 * each of 6 cases - 6 different reasons we predict we'll fail.
92 */
93 int *spredict_total_ct; /* For ALL sampling tests: the number of times we predict we'll
94 * fail the test for an HMM node for
95 * each of 6 cases - 6 different reasons we predict we'll fail.
96 */
97 float chi_thresh; /* if any chi-square test (which we haven't deemed 'impossible'
98 * during the sampling check is below this threshold, fail.
99 */
100 int nsamples; /* Number of samples to build the ML HMM with during a sampling
101 * check.
102 */
103 int npredict_cases; /* Number of different cases for predicting a node's transitions
104 * will be impossible to match b/t the two HMMs.
105 */
106 CMSubMap_t *submap;
107 CMSubInfo_t *subinfo;
108 ESL_RANDOMNESS *r = NULL; /* source of randomness */
109 ESL_ALPHABET *abc = NULL; /* alphabet, for the CM */
110 char errbuf[eslERRBUFSIZE];
111
112 /***********************************************
113 * Parse command line
114 ***********************************************/
115
116 nmodels = esl_opt_GetInteger(go, "-n");
117 pthresh = esl_opt_GetReal (go, "-t");
118 if( esl_opt_IsOn (go, "-b")) {
119 begin_set = TRUE;
120 sstruct = esl_opt_GetInteger(go, "-b");
121 nmodels = 1;
122 }
123 else begin_set = FALSE;
124 if( esl_opt_IsOn (go, "-e")) {
125 end_set = TRUE;
126 estruct = esl_opt_GetInteger(go, "-e");
127 nmodels = 1;
128 }
129 else end_set = FALSE;
130 do_atest = (! esl_opt_GetBoolean(go, "--psionly"));
131 do_stest = esl_opt_GetBoolean(go, "--sample");
132 nsamples = esl_opt_GetInteger(go, "--nseq");
133 chi_thresh = esl_opt_GetReal (go, "--chi");
134 do_exhaust = esl_opt_GetBoolean(go, "--exhaust");
135 print_flag = esl_opt_GetBoolean(go, "--debug");
136
137 if(begin_set && esl_opt_IsOn(go, "-n")) cm_Fail("-n does not make sense with -b and -e.\n");
138 if(begin_set && sstruct > estruct) cm_Fail("For -b <x> and -e <y> y must be >= x.\n");
139 if(begin_set && sstruct > estruct) cm_Fail("For -b <x> and -e <y> y must be >= x.\n");
140
141 if(do_exhaust && do_stest) printf("--exhaust and --sample might take a long time...\n");
142 npredict_cases = 6;
143
144 /********************************************`***
145 * Preliminaries: get our CM
146 ***********************************************/
147
148 if ((status = cm_file_Open(cmfile, NULL, FALSE, &(cmfp), errbuf)) != eslOK) cm_Fail("Failed to open covariance model save file %s\n", cmfile);
149 if ((cm_file_Read(cmfp, TRUE, &abc, &cm)) != eslOK) cm_Fail("Failed to read CM");
150 cm_file_Close(cmfp);
151
152 /* configure our model */
153 cm->config_opts |= CM_CONFIG_SUB;
154 if((status = cm_Configure(cm, errbuf, -1)) != eslOK) cm_Fail("Problem configuring CM");
155
156 if (esl_opt_IsOn(go, "-s")) r = esl_randomness_Create((long) esl_opt_GetInteger(go, "-s"));
157 else r = esl_randomness_CreateTimeseeded();
158
159 /* Allocate and initialize our *wrong_total_ct arrays */
160 ESL_ALLOC(apredict_total_ct, (sizeof(int) * (npredict_cases+1)));
161 ESL_ALLOC(spredict_total_ct, (sizeof(int) * (npredict_cases+1)));
162 ESL_ALLOC(awrong_total_ct, (sizeof(int) * (npredict_cases+1)));
163 ESL_ALLOC(swrong_total_ct, (sizeof(int) * (npredict_cases+1)));
164 esl_vec_ISet(apredict_total_ct, (npredict_cases+1), 0);
165 esl_vec_ISet(spredict_total_ct, (npredict_cases+1), 0);
166 esl_vec_ISet(awrong_total_ct, (npredict_cases+1), 0);
167 esl_vec_ISet(swrong_total_ct, (npredict_cases+1), 0);
168
169 /***********************************************************
170 * Stategy:
171 * If do_atest (default), we do:
172 * 1. Build a CP9 HMM (cp9_1) from the sub_cm (this is done).
173 * 2. Build a CP9 HMM (cp9_2) from the full cm.
174 * 3. Reconfig cp9_2 so start node is sstruct and end node is estruct.
175 * 4. Check corresponding parameters of cp9_1 and cp9_2 to make
176 * sure they're within the threshold.
177 *
178 * If do_stest, we also do:
179 * 1. Build a CP9 HMM (cp9_1) from the sub_cm (this is done.)
180 * 2. Sample a deep MSA from the CM.
181 * 3. Truncate the MSA between sstruct and estruct.
182 * 4. Perform chi-squared tests to see if sample from
183 * (3) could have come from cp9_1. We do this by first
184 * building a ML CP9 HMM (cp9_2) from the counts in
185 * the truncated MSA.
186 *
187 * No matter what, we always check to make sure that the
188 * corresponding psi values of the orig_cm and sub_cm
189 * are within 'threshold' of each other.
190 *
191 * This is all done within the build_sub_cm() function.
192 *********************************************************/
193
194 ndone = 0;
195 if(do_exhaust) { /* Build every possible sub CM. */
196 nmodels = (cm->clen * cm->clen + cm->clen) / 2;
197 printf("Building and checking all possible sub CM (%5d different start/end positions):\n", cm->clen);
198
199 for(sstruct = 1; sstruct <= cm->clen; sstruct++) {
200 printf("\tBuilding models with start pos: %5d (%5d / %5d completed)\n", sstruct, ndone, nmodels);
201 for(estruct = sstruct+2; estruct <= cm->clen; estruct++) {
202 if((status = build_sub_cm(cm, errbuf, &sub_cm, sstruct, estruct, &submap, print_flag)) != eslOK)
203 cm_Fail("Couldn't build a sub_cm from CM with sstruct: %d estruct: %d\n", sstruct, estruct);
204 /* Do the psi test */
205 if((status = check_orig_psi_vs_sub_psi(cm, sub_cm, errbuf, submap, pthresh, print_flag)) != eslOK) {
206 printf("\nSub CM construction for sstruct: %4d estruct: %4d failed psi test.\n", sstruct, estruct);
207 cm_Fail(errbuf);
208 }
209 /* Do analytical and/or sampling HMM tests */
210 if(do_atest || do_stest) {
211 subinfo = AllocSubInfo(submap->epos-submap->spos+1);
212 if((status = cm_ConfigureSub(sub_cm, errbuf, -1, cm, submap)) != eslOK) cm_Fail(errbuf);
213 if(do_atest) {
214 if((status = check_sub_cm(cm, sub_cm, errbuf, submap, subinfo, pthresh, print_flag)) != eslOK) {
215 printf("\nSub CM construction for sstruct: %4d estruct: %4d failed analytical HMM test.\n", sstruct, estruct);
216 cm_Fail(errbuf);
217 }
218 }
219 if(do_stest) {
220 if((status = check_sub_cm_by_sampling(cm, sub_cm, errbuf, r, submap, subinfo, chi_thresh, nsamples, print_flag)) != eslOK) {
221 printf("\nSub CM construction for sstruct: %4d estruct: %4d failed sampling HMM test.\n", sstruct, estruct);
222 cm_Fail(errbuf);
223 }
224 }
225 /* keep track of number of each case of wrong prediction */
226 for(j = 1; j <= npredict_cases; j++) {
227 apredict_total_ct[j] += subinfo->apredict_ct[j];
228 spredict_total_ct[j] += subinfo->spredict_ct[j];
229 awrong_total_ct[j] += subinfo->awrong_ct[j];
230 swrong_total_ct[j] += subinfo->swrong_ct[j];
231 }
232 FreeSubInfo(subinfo);
233 }
234 FreeCM(sub_cm);
235 FreeSubMap(submap);
236 ndone++;
237 }
238 }
239 printf("\nDone. %5d sub CM(s) were constructed and passed the following tests:\n", ndone);
240 printf("\tpsi test\n");
241 if(do_atest) printf("\tanalytical HMM test\n");
242 if(do_stest) printf("\tsampling HMM test\n");
243 }
244 else /* Build models with either preset begin point (sstruct) and end points (estruct)
245 * or randomly chosen ones */ {
246 if(begin_set && end_set) {
247 if(sstruct < 1) sstruct = 1;
248 if(estruct > cm->clen) estruct = cm->clen;
249 printf("Building a single sub CM with sstruct: %4d and estruct: %4d ... ", sstruct, estruct);
250 }
251 else printf("\tBuilding models with random start and end positions:\n");
252 for(i = 0; i < nmodels; i++) { /* if begin_set && end_set, nmodels is 1 */
253 if(!(begin_set && end_set)) {
254 /* Randomly pick a start and end between 1 and cm->clen, inclusive */
255 sstruct = esl_rnd_Roll(r, (cm->clen)) + 1;
256 estruct = esl_rnd_Roll(r, (cm->clen)) + 1;
257 ESL_DASSERT1((sstruct <= cm->clen));
258 ESL_DASSERT1((estruct <= cm->clen));
259 ESL_DASSERT1((sstruct >= 1));
260 ESL_DASSERT1((estruct >= 1));
261 if(sstruct > estruct) {
262 temp = sstruct;
263 sstruct = estruct;
264 estruct = temp;
265 }
266 if((estruct - sstruct) < 2) {
267 estruct = sstruct + 2;
268 if(estruct > cm->clen) {
269 estruct = cm->clen;
270 sstruct = estruct - 2;
271 }
272 }
273 }
274 if((status = build_sub_cm(cm, errbuf, &sub_cm, sstruct, estruct, &submap, print_flag)) != eslOK) {
275 printf("Couldn't build a sub_cm from CM with sstruct: %d estruct: %d\n", sstruct, estruct);
276 cm_Fail(errbuf);
277 }
278 /* Do the psi test */
279 if((status = check_orig_psi_vs_sub_psi(cm, sub_cm, errbuf, submap, pthresh, print_flag)) != eslOK) {
280 printf("\nSub CM construction for sstruct: %4d estruct: %4d failed psi test.\n", sstruct, estruct);
281 cm_Fail(errbuf);
282 }
283 /* Do analytical and/or sampling HMM tests */
284 if(do_atest || do_stest) {
285 subinfo = AllocSubInfo(submap->epos-submap->spos+1);
286 if((status = cm_ConfigureSub(sub_cm, errbuf, -1, cm, submap)) != eslOK) cm_Fail(errbuf);
287 if(do_atest) {
288 if((status = check_sub_cm(cm, sub_cm, errbuf, submap, subinfo, pthresh, print_flag)) != eslOK) {
289 printf("\nSub CM construction for sstruct: %4d estruct: %4d failed analytical HMM test.\n", sstruct, estruct);
290 cm_Fail(errbuf);
291 }
292 }
293 if(do_stest) {
294 if((status = check_sub_cm_by_sampling(cm, sub_cm, errbuf, r, submap, subinfo, chi_thresh, nsamples, print_flag)) != eslOK) {
295 printf("\nSub CM construction for sstruct: %4d estruct: %4d failed sampling HMM test.\n", sstruct, estruct);
296 cm_Fail(errbuf);
297 }
298 }
299 /* keep track of number of each case of wrong prediction */
300 for(j = 1; j <= npredict_cases; j++) {
301 apredict_total_ct[j] += subinfo->apredict_ct[j];
302 spredict_total_ct[j] += subinfo->spredict_ct[j];
303 awrong_total_ct[j] += subinfo->awrong_ct[j];
304 swrong_total_ct[j] += subinfo->swrong_ct[j];
305 }
306 FreeSubInfo(subinfo);
307 }
308 FreeCM(sub_cm);
309 FreeSubMap(submap);
310 ndone++;
311 }
312 printf("done.\n%5d sub CMs were constructed and passed the following tests:\n", ndone);
313 printf("\tpsi test\n");
314 if(do_atest) printf("\tanalytical HMM test\n");
315 if(do_stest) printf("\tsampling HMM test\n");
316 }
317 if(do_atest) {
318 printf("\nPrinting summary of HMM nodes predicted to fail the analytical test:\n");
319 for(j = 1; j <= npredict_cases; j++)
320 printf("\tcase %d: %6d (%6d passed)\n", j, apredict_total_ct[j], awrong_total_ct[j]);
321 }
322 if(do_stest) {
323 printf("\nPrinting summary of HMM nodes predicted to fail the sampling test:\n");
324 for(j = 1; j <= npredict_cases; j++)
325 printf("\tcase %d: %6d (%6d passed)\n", j, spredict_total_ct[j], swrong_total_ct[j]);
326 }
327 printf("\n");
328 free(apredict_total_ct);
329 free(spredict_total_ct);
330 free(awrong_total_ct);
331 free(swrong_total_ct);
332 FreeCM(cm);
333 esl_alphabet_Destroy(abc);
334 esl_randomness_Destroy(r);
335 esl_getopts_Destroy(go);
336 return 0;
337
338 ERROR:
339 cm_Fail("main(), memory allocation error.");
340 return 1; /* NEVERREACHED */
341 }