| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 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 |
|
| 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; |
| 55 |
CM_t *cm; |
| 56 |
int v; |
| 57 |
|
| 58 |
double **mc_gamma; |
| 59 |
double **gamma; |
| 60 |
double *gamma0_glb; |
| 61 |
double p; |
| 62 |
double threshold; |
| 63 |
|
| 64 |
int maxZ; |
| 65 |
int mc_nsample; |
| 66 |
ESL_RANDOMNESS *r = NULL; |
| 67 |
ESL_ALPHABET *abc = NULL; |
| 68 |
char errbuf[eslERRBUFSIZE]; |
| 69 |
|
| 70 |
|
| 71 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 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 |
|
| 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) { |
| 107 |
esl_vec_DScale(gamma0_glb, maxZ+1, esl_vec_DSum(mc_gamma[v], maxZ+1)); |
| 108 |
p = DChiSquareFit(gamma0_glb, mc_gamma[v], maxZ+1); |
| 109 |
} |
| 110 |
else { |
| 111 |
esl_vec_DScale(gamma[v], maxZ+1, esl_vec_DSum(mc_gamma[v], maxZ+1)); |
| 112 |
p = DChiSquareFit(gamma[v], mc_gamma[v], maxZ+1); |
| 113 |
} |
| 114 |
|
| 115 |
if (cm->sttype[v] != E_st |
| 116 |
&& cm->ndtype[cm->ndidx[v]+1] != END_nd |
| 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 |
} |