| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 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; |
| 57 |
CM_t *cm; |
| 58 |
CM_t *sub_cm; |
| 59 |
int nmodels; |
| 60 |
int sstruct; |
| 61 |
int estruct; |
| 62 |
int i; |
| 63 |
int j; |
| 64 |
int temp; |
| 65 |
|
| 66 |
double pthresh; |
| 67 |
int begin_set; |
| 68 |
int end_set; |
| 69 |
int do_atest; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
int do_stest; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
int do_exhaust; |
| 79 |
int ndone; |
| 80 |
int print_flag; |
| 81 |
int *awrong_total_ct; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
int *swrong_total_ct; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
int *apredict_total_ct; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
int *spredict_total_ct; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
float chi_thresh; |
| 98 |
|
| 99 |
|
| 100 |
int nsamples; |
| 101 |
|
| 102 |
|
| 103 |
int npredict_cases; |
| 104 |
|
| 105 |
|
| 106 |
CMSubMap_t *submap; |
| 107 |
CMSubInfo_t *subinfo; |
| 108 |
ESL_RANDOMNESS *r = NULL; |
| 109 |
ESL_ALPHABET *abc = NULL; |
| 110 |
char errbuf[eslERRBUFSIZE]; |
| 111 |
|
| 112 |
|
| 113 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
ndone = 0; |
| 195 |
if(do_exhaust) { |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
| 245 |
{ |
| 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++) { |
| 253 |
if(!(begin_set && end_set)) { |
| 254 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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; |
| 341 |
} |