| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include <p7_config.h> |
| 11 |
|
| 12 |
#include "easel.h" |
| 13 |
#include "esl_rootfinder.h" |
| 14 |
|
| 15 |
#include "hmmer.h" |
| 16 |
|
| 17 |
struct ew_param_s { |
| 18 |
const P7_HMM *hmm; |
| 19 |
const P7_BG *bg; |
| 20 |
const P7_PRIOR *pri; |
| 21 |
P7_HMM *h2; |
| 22 |
double etarget; |
| 23 |
}; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
static int |
| 29 |
eweight_target_f(double Neff, void *params, double *ret_fx) |
| 30 |
{ |
| 31 |
struct ew_param_s *p = (struct ew_param_s *) params; |
| 32 |
|
| 33 |
p7_hmm_CopyParameters(p->hmm, p->h2); |
| 34 |
p7_hmm_Scale(p->h2, Neff / (double) p->h2->nseq); |
| 35 |
p7_ParameterEstimation(p->h2, p->pri); |
| 36 |
*ret_fx = p7_MeanMatchRelativeEntropy(p->h2, p->bg) - p->etarget; |
| 37 |
return eslOK; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
int |
| 61 |
p7_EntropyWeight(const P7_HMM *hmm, const P7_BG *bg, const P7_PRIOR *pri, double etarget, double *ret_Neff) |
| 62 |
{ |
| 63 |
int status; |
| 64 |
ESL_ROOTFINDER *R = NULL; |
| 65 |
struct ew_param_s p; |
| 66 |
double Neff; |
| 67 |
double fx; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
p.hmm = hmm; |
| 72 |
p.bg = bg; |
| 73 |
p.pri = pri; |
| 74 |
if ((p.h2 = p7_hmm_Clone(hmm)) == NULL) return eslEMEM; |
| 75 |
p.etarget = etarget; |
| 76 |
|
| 77 |
Neff = (double) hmm->nseq; |
| 78 |
if ((status = eweight_target_f(Neff, &p, &fx)) != eslOK) goto ERROR; |
| 79 |
if (fx > 0.) |
| 80 |
{ |
| 81 |
if ((R = esl_rootfinder_Create(eweight_target_f, &p)) == NULL) {status = eslEMEM; goto ERROR;} |
| 82 |
esl_rootfinder_SetAbsoluteTolerance(R, 0.01); |
| 83 |
if ((status = esl_root_Bisection(R, 0., (double) hmm->nseq, &Neff)) != eslOK) goto ERROR; |
| 84 |
|
| 85 |
esl_rootfinder_Destroy(R); |
| 86 |
} |
| 87 |
|
| 88 |
p7_hmm_Destroy(p.h2); |
| 89 |
*ret_Neff = Neff; |
| 90 |
return eslOK; |
| 91 |
|
| 92 |
ERROR: |
| 93 |
if (p.h2 != NULL) p7_hmm_Destroy(p.h2); |
| 94 |
if (R != NULL) esl_rootfinder_Destroy(R); |
| 95 |
*ret_Neff = (double) hmm->nseq; |
| 96 |
return status; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
static int |
| 104 |
eweight_target_exp_f(double exp, void *params, double *ret_fx) |
| 105 |
{ |
| 106 |
struct ew_param_s *p = (struct ew_param_s *) params; |
| 107 |
|
| 108 |
p7_hmm_CopyParameters(p->hmm, p->h2); |
| 109 |
p7_hmm_ScaleExponential(p->h2, exp); |
| 110 |
p7_ParameterEstimation(p->h2, p->pri); |
| 111 |
*ret_fx = p7_MeanMatchRelativeEntropy(p->h2, p->bg) - p->etarget; |
| 112 |
return eslOK; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
int |
| 142 |
p7_EntropyWeight_exp(const P7_HMM *hmm, const P7_BG *bg, const P7_PRIOR *pri, double etarget, double *ret_exp) |
| 143 |
{ |
| 144 |
|
| 145 |
int status; |
| 146 |
ESL_ROOTFINDER *R = NULL; |
| 147 |
struct ew_param_s p; |
| 148 |
double exp = 1.; |
| 149 |
double fx; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
p.hmm = hmm; |
| 154 |
p.bg = bg; |
| 155 |
p.pri = pri; |
| 156 |
if ((p.h2 = p7_hmm_Clone(hmm)) == NULL) return eslEMEM; |
| 157 |
p.etarget = etarget; |
| 158 |
|
| 159 |
|
| 160 |
if ((status = eweight_target_exp_f(1.0, &p, &fx)) != eslOK) goto ERROR; |
| 161 |
if (fx > 0.) |
| 162 |
{ |
| 163 |
if ((R = esl_rootfinder_Create(eweight_target_exp_f, &p)) == NULL) {status = eslEMEM; goto ERROR;} |
| 164 |
esl_rootfinder_SetAbsoluteTolerance(R, 0.001); |
| 165 |
if ((status = esl_root_Bisection(R, 0., 1.0, &exp)) != eslOK) goto ERROR; |
| 166 |
|
| 167 |
esl_rootfinder_Destroy(R); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
p7_hmm_Destroy(p.h2); |
| 172 |
|
| 173 |
*ret_exp = exp; |
| 174 |
return eslOK; |
| 175 |
|
| 176 |
ERROR: |
| 177 |
if (p.h2 != NULL) p7_hmm_Destroy(p.h2); |
| 178 |
if (R != NULL) esl_rootfinder_Destroy(R); |
| 179 |
|
| 180 |
return status; |
| 181 |
} |
| 182 |
|