| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <p7_config.h> |
| 7 |
|
| 8 |
#include "easel.h" |
| 9 |
#include "esl_vectorops.h" |
| 10 |
|
| 11 |
#include "hmmer.h" |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
double |
| 34 |
p7_MeanMatchInfo(const P7_HMM *hmm, const P7_BG *bg) |
| 35 |
{ |
| 36 |
return esl_vec_FEntropy(bg->f, hmm->abc->K) - p7_MeanMatchEntropy(hmm); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
double |
| 53 |
p7_MeanMatchEntropy(const P7_HMM *hmm) |
| 54 |
{ |
| 55 |
int k; |
| 56 |
double H = 0.; |
| 57 |
|
| 58 |
for (k = 1; k <= hmm->M; k++) |
| 59 |
H += esl_vec_FEntropy(hmm->mat[k], hmm->abc->K); |
| 60 |
H /= (double) hmm->M; |
| 61 |
return H; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
double |
| 80 |
p7_MeanMatchRelativeEntropy(const P7_HMM *hmm, const P7_BG *bg) |
| 81 |
{ |
| 82 |
int k; |
| 83 |
double KL = 0.; |
| 84 |
|
| 85 |
#if 0 |
| 86 |
p7_bg_Dump(stdout, hmm->bg); |
| 87 |
for (k = 1; k <= hmm->M; k++) |
| 88 |
printf("Match %d : %.2f %.2f\n", k, |
| 89 |
esl_vec_FRelEntropy(hmm->mat[k], hmm->bg->f, hmm->abc->K), |
| 90 |
esl_vec_FEntropy(bg->f, hmm->abc->K) - esl_vec_FEntropy(hmm->mat[k], hmm->abc->K)); |
| 91 |
#endif |
| 92 |
|
| 93 |
for (k = 1; k <= hmm->M; k++) |
| 94 |
KL += esl_vec_FRelEntropy(hmm->mat[k], bg->f, hmm->abc->K); |
| 95 |
KL /= (double) hmm->M; |
| 96 |
return KL; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
double |
| 102 |
p7_MeanForwardScore(const P7_HMM *hmm, const P7_BG *bg) |
| 103 |
{ |
| 104 |
int L = 350; |
| 105 |
int N = 100; |
| 106 |
P7_PROFILE *gm = p7_profile_Create(hmm->M, hmm->abc); |
| 107 |
P7_GMX *gx = p7_gmx_Create(gm->M, L); |
| 108 |
ESL_SQ *sq = esl_sq_CreateDigital(hmm->abc); |
| 109 |
ESL_RANDOMNESS *r = esl_randomness_CreateFast(0); |
| 110 |
float fsc; |
| 111 |
float nullsc; |
| 112 |
double bitscore; |
| 113 |
double sum = 0.; |
| 114 |
int i; |
| 115 |
|
| 116 |
if (p7_ProfileConfig (hmm, bg, gm, L, p7_LOCAL) != eslOK) p7_Die("failed to configure profile"); |
| 117 |
for (i = 0; i < N; i++) |
| 118 |
{ |
| 119 |
if (p7_ReconfigLength(gm, L) != eslOK) p7_Die("failed to reconfig profile length"); |
| 120 |
if (p7_ProfileEmit(r, hmm, gm, bg, sq, NULL) != eslOK) p7_Die("failed to emit sequence"); |
| 121 |
if (p7_ReconfigLength(gm, sq->n) != eslOK) p7_Die("failed to reconfig profile length"); |
| 122 |
if (p7_gmx_GrowTo(gx, gm->M, sq->n) != eslOK) p7_Die("failed to grow the matrix"); |
| 123 |
if (p7_GForward(sq->dsq, sq->n, gm, gx, &fsc) != eslOK) p7_Die("failed to run Forward"); |
| 124 |
if (p7_bg_NullOne(bg, sq->dsq, sq->n, &nullsc) != eslOK) p7_Die("failed to run bg_NullOne()"); |
| 125 |
bitscore = (fsc - nullsc) / eslCONST_LOG2; |
| 126 |
|
| 127 |
sum += bitscore; |
| 128 |
} |
| 129 |
|
| 130 |
esl_randomness_Destroy(r); |
| 131 |
esl_sq_Destroy(sq); |
| 132 |
p7_gmx_Destroy(gx); |
| 133 |
p7_profile_Destroy(gm); |
| 134 |
return (sum / (double) N); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
int |
| 163 |
p7_MeanPositionRelativeEntropy(const P7_HMM *hmm, const P7_BG *bg, double *ret_entropy) |
| 164 |
{ |
| 165 |
int status; |
| 166 |
float *mocc = NULL; |
| 167 |
int k; |
| 168 |
double mre, tre; |
| 169 |
double xm, xi, xd; |
| 170 |
|
| 171 |
ESL_ALLOC(mocc, sizeof(float) * (hmm->M+1)); |
| 172 |
if ((status = p7_hmm_CalculateOccupancy(hmm, mocc, NULL)) != eslOK) goto ERROR; |
| 173 |
|
| 174 |
|
| 175 |
for (mre = 0., k = 1; k <= hmm->M; k++) |
| 176 |
mre += mocc[k] * esl_vec_FRelEntropy(hmm->mat[k], bg->f, hmm->abc->K); |
| 177 |
mre /= esl_vec_FSum(mocc+1, hmm->M); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
for (tre = 0., k = 2; k <= hmm->M; k++) |
| 182 |
{ |
| 183 |
xm = mocc[k-1]*hmm->t[k-1][p7H_MM] * log(hmm->t[k-1][p7H_MM] / bg->p1); |
| 184 |
xi = mocc[k-1]*hmm->t[k-1][p7H_MI] * (log(hmm->t[k-1][p7H_MM] / bg->p1) + log(hmm->t[k-1][p7H_IM] / bg->p1)); |
| 185 |
xd = (1.-mocc[k-1])*hmm->t[k-1][p7H_DM] * log(hmm->t[k-1][p7H_DM] / bg->p1); |
| 186 |
tre += (xm+xi+xd) / eslCONST_LOG2; |
| 187 |
} |
| 188 |
tre /= esl_vec_FSum(mocc+2, hmm->M-1); |
| 189 |
|
| 190 |
free(mocc); |
| 191 |
*ret_entropy = mre+tre; |
| 192 |
return eslOK; |
| 193 |
|
| 194 |
ERROR: |
| 195 |
if (mocc != NULL) free(mocc); |
| 196 |
*ret_entropy = 0.; |
| 197 |
return status; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
int |
| 231 |
p7_hmm_CompositionKLD(const P7_HMM *hmm, const P7_BG *bg, float *ret_KL, float **opt_avp) |
| 232 |
{ |
| 233 |
int K = hmm->abc->K; |
| 234 |
float *occ = NULL; |
| 235 |
float *p = NULL; |
| 236 |
int status; |
| 237 |
int k; |
| 238 |
|
| 239 |
ESL_ALLOC(occ, sizeof(float) * (hmm->M+1)); |
| 240 |
ESL_ALLOC(p, sizeof(float) * K); |
| 241 |
|
| 242 |
p7_hmm_CalculateOccupancy(hmm, occ, NULL); |
| 243 |
|
| 244 |
esl_vec_FSet(p, K, 0.); |
| 245 |
for (k = 1; k <= hmm->M; k++) |
| 246 |
esl_vec_FAddScaled(p, hmm->mat[k], occ[k], K); |
| 247 |
esl_vec_FNorm(p, K); |
| 248 |
|
| 249 |
*ret_KL = esl_vec_FRelEntropy(p, bg->f, K); |
| 250 |
if (opt_avp != NULL) *opt_avp = p; else free(p); |
| 251 |
free(occ); |
| 252 |
return eslOK; |
| 253 |
|
| 254 |
ERROR: |
| 255 |
free(occ); |
| 256 |
free(p); |
| 257 |
if (opt_avp) *opt_avp = NULL; |
| 258 |
*ret_KL = 0.0; |
| 259 |
return status; |
| 260 |
} |
| 261 |
|
| 262 |
|