| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include <p7_config.h> |
| 18 |
|
| 19 |
#include "easel.h" |
| 20 |
#include "esl_vectorops.h" |
| 21 |
|
| 22 |
#include "hmmer.h" |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
void |
| 60 |
p7_null3_score(const ESL_ALPHABET *abc, const ESL_DSQ *dsq, P7_TRACE *tr, int start, int stop, P7_BG *bg, float *ret_sc) |
| 61 |
{ |
| 62 |
float score = 0.; |
| 63 |
int status; |
| 64 |
int i; |
| 65 |
float *freq; |
| 66 |
int dir; |
| 67 |
int tr_pos; |
| 68 |
|
| 69 |
ESL_ALLOC(freq, sizeof(float) * abc->K); |
| 70 |
esl_vec_FSet(freq, abc->K, 0.0); |
| 71 |
|
| 72 |
|
| 73 |
if(abc == NULL) esl_exception(eslEINVAL, FALSE, __FILE__, __LINE__, "p7_null3_score() alphabet is NULL.%s\n", ""); |
| 74 |
if(dsq == NULL) esl_exception(eslEINVAL, FALSE, __FILE__, __LINE__, "p7_null3_score() dsq alphabet is NULL.%s\n", ""); |
| 75 |
if(abc->type != eslRNA && abc->type != eslDNA) esl_exception(eslEINVAL, FALSE, __FILE__, __LINE__, "p7_null3_score() expects alphabet of RNA or DNA.%s\n", ""); |
| 76 |
|
| 77 |
dir = start < stop ? 1 : -1; |
| 78 |
|
| 79 |
if (tr != NULL) { |
| 80 |
|
| 81 |
tr_pos = 2; |
| 82 |
i = start; |
| 83 |
while (tr->st[tr_pos] != p7T_M) { |
| 84 |
if (tr->st[tr_pos] == p7T_N) |
| 85 |
i += dir; |
| 86 |
tr_pos++; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
while (tr->st[tr_pos] != p7T_E) { |
| 91 |
if (tr->st[tr_pos] == p7T_M) { |
| 92 |
if(esl_abc_XIsGap(abc, dsq[i])) esl_exception(eslEINVAL, FALSE, __FILE__, __LINE__, "in p7_null3_score(), res %d is a gap!%s\n", ""); |
| 93 |
esl_abc_FCount(abc, freq, dsq[i], 1.); |
| 94 |
} |
| 95 |
if (tr->st[tr_pos] != p7T_D ) |
| 96 |
i += dir; |
| 97 |
tr_pos++; |
| 98 |
} |
| 99 |
} else { |
| 100 |
|
| 101 |
for (i=ESL_MIN(start,stop); i <= ESL_MAX(start,stop); i++) |
| 102 |
{ |
| 103 |
if(esl_abc_XIsGap(abc, dsq[i])) esl_exception(eslEINVAL, FALSE, __FILE__, __LINE__, "in p7_null3_score(), res %d is a gap!%s\n", ""); |
| 104 |
esl_abc_FCount(abc, freq, dsq[i], 1.); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
esl_vec_FNorm(freq, abc->K); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
for (i = 0; i < abc->K; i++) |
| 113 |
score += freq[i]==0 ? 0.0 : esl_logf( freq[i]/bg->f[i] ) * freq[i] * ( (stop-start)*dir +1) ; |
| 114 |
|
| 115 |
|
| 116 |
score = p7_FLogsum(0., score); |
| 117 |
*ret_sc = score; |
| 118 |
|
| 119 |
return; |
| 120 |
|
| 121 |
ERROR: |
| 122 |
esl_exception(eslEINVAL, FALSE, __FILE__, __LINE__, "p7_null3_score() memory allocation error.%s\n", ""); |
| 123 |
return; |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
#ifdef p7_NULL3_BENCHMARK |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
#include <p7_config.h> |
| 141 |
|
| 142 |
#include "easel.h" |
| 143 |
#include "esl_alphabet.h" |
| 144 |
#include "esl_getopts.h" |
| 145 |
#include "esl_random.h" |
| 146 |
#include "esl_randomseq.h" |
| 147 |
#include "esl_stopwatch.h" |
| 148 |
|
| 149 |
#include "hmmer.h" |
| 150 |
|
| 151 |
static ESL_OPTIONS options[] = { |
| 152 |
|
| 153 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 154 |
{ "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 155 |
{ "-L", eslARG_INT, "400", NULL, "n>0", NULL, NULL, NULL, "length of random target seqs", 0 }, |
| 156 |
{ "-N", eslARG_INT, "50000", NULL, "n>0", NULL, NULL, NULL, "number of random target seqs", 0 }, |
| 157 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 158 |
}; |
| 159 |
static char usage[] = "[-options] <hmmfile>"; |
| 160 |
static char banner[] = "benchmark driver for posterior residue null2, generic version"; |
| 161 |
|
| 162 |
int |
| 163 |
main(int argc, char **argv) |
| 164 |
{ |
| 165 |
ESL_GETOPTS *go = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage); |
| 166 |
char *hmmfile = esl_opt_GetArg(go, 1); |
| 167 |
ESL_STOPWATCH *w = esl_stopwatch_Create(); |
| 168 |
ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s")); |
| 169 |
ESL_ALPHABET *abc = NULL; |
| 170 |
P7_HMMFILE *hfp = NULL; |
| 171 |
P7_HMM *hmm = NULL; |
| 172 |
P7_BG *bg = NULL; |
| 173 |
P7_PROFILE *gm = NULL; |
| 174 |
P7_GMX *gx1 = NULL; |
| 175 |
P7_GMX *gx2 = NULL; |
| 176 |
int L = esl_opt_GetInteger(go, "-L"); |
| 177 |
int N = esl_opt_GetInteger(go, "-N"); |
| 178 |
ESL_DSQ *dsq = malloc(sizeof(ESL_DSQ) * (L+2)); |
| 179 |
float null2[p7_MAXCODE]; |
| 180 |
int i; |
| 181 |
float fsc, bsc; |
| 182 |
double Mcs; |
| 183 |
|
| 184 |
if (p7_hmmfile_Open(hmmfile, NULL, &hfp, NULL) != eslOK) p7_Fail("Failed to open HMM file %s", hmmfile); |
| 185 |
if (p7_hmmfile_Read(hfp, &abc, &hmm) != eslOK) p7_Fail("Failed to read HMM"); |
| 186 |
|
| 187 |
bg = p7_bg_Create(abc); p7_bg_SetLength(bg, L); |
| 188 |
gm = p7_profile_Create(hmm->M, abc); p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL); |
| 189 |
gx1 = p7_gmx_Create(gm->M, L); |
| 190 |
gx2 = p7_gmx_Create(gm->M, L); |
| 191 |
|
| 192 |
esl_rsq_xfIID(r, bg->f, abc->K, L, dsq); |
| 193 |
p7_GForward (dsq, L, gm, gx1, &fsc); |
| 194 |
p7_GBackward(dsq, L, gm, gx2, &bsc); |
| 195 |
p7_GDecoding(gm, gx1, gx2, gx2); |
| 196 |
|
| 197 |
esl_stopwatch_Start(w); |
| 198 |
for (i = 0; i < N; i++) |
| 199 |
p7_GNull2_ByExpectation(gm, gx2, null2); |
| 200 |
esl_stopwatch_Stop(w); |
| 201 |
|
| 202 |
Mcs = (double) N * (double) L * (double) gm->M * 1e-6 / w->user; |
| 203 |
esl_stopwatch_Display(stdout, w, "# CPU time: "); |
| 204 |
printf("# M = %d\n", gm->M); |
| 205 |
printf("# %.1f Mc/s\n", Mcs); |
| 206 |
|
| 207 |
free(dsq); |
| 208 |
p7_gmx_Destroy(gx1); |
| 209 |
p7_gmx_Destroy(gx2); |
| 210 |
p7_profile_Destroy(gm); |
| 211 |
p7_bg_Destroy(bg); |
| 212 |
p7_hmm_Destroy(hmm); |
| 213 |
p7_hmmfile_Close(hfp); |
| 214 |
esl_alphabet_Destroy(abc); |
| 215 |
esl_stopwatch_Destroy(w); |
| 216 |
esl_randomness_Destroy(r); |
| 217 |
esl_getopts_Destroy(go); |
| 218 |
return 0; |
| 219 |
} |
| 220 |
#endif |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
#ifdef p7_NULL3_TESTDRIVE |
| 230 |
|
| 231 |
static void |
| 232 |
utest_correct_normalization(ESL_RANDOMNESS *r, P7_PROFILE *gm, P7_BG *bg, ESL_DSQ *dsq, int L, P7_GMX *fwd, P7_GMX *bck) |
| 233 |
{ |
| 234 |
char *msg = "normalization unit test failed"; |
| 235 |
float null2[p7_MAXABET]; |
| 236 |
float sum; |
| 237 |
int x; |
| 238 |
|
| 239 |
esl_rsq_xfIID(r, bg->f, gm->abc->K, L, dsq); |
| 240 |
|
| 241 |
p7_GForward (dsq, L, gm, fwd, NULL); |
| 242 |
p7_GBackward(dsq, L, gm, bck, NULL); |
| 243 |
p7_PosteriorNull2(L, gm, fwd, bck, bck); |
| 244 |
p7_Null2Corrections(gm, dsq, L, 0, bck, fwd, null2, NULL, NULL); |
| 245 |
|
| 246 |
|
| 247 |
for (x = 0; x < gm->abc->K; x++) |
| 248 |
null2[x] = exp(null2[x]) * bg->f[x]; |
| 249 |
|
| 250 |
sum = esl_vec_FSum(null2, gm->abc->K); |
| 251 |
if (sum < 0.99 || sum > 1.01) esl_fatal(msg); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
#endif |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
#ifdef p7_NULL3_TESTDRIVE |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
#include <p7_config.h> |
| 269 |
|
| 270 |
#include <stdio.h> |
| 271 |
#include <stdlib.h> |
| 272 |
|
| 273 |
#include "easel.h" |
| 274 |
#include "esl_getopts.h" |
| 275 |
#include "esl_random.h" |
| 276 |
#include "esl_alphabet.h" |
| 277 |
#include "hmmer.h" |
| 278 |
|
| 279 |
static ESL_OPTIONS options[] = { |
| 280 |
|
| 281 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 282 |
{ "-L", eslARG_INT, "200", NULL, NULL, NULL, NULL, NULL, "length of sampled sequences", 0 }, |
| 283 |
{ "-M", eslARG_INT, "100", NULL, NULL, NULL, NULL, NULL, "length of sampled HMM", 0 }, |
| 284 |
{ "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 285 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 286 |
}; |
| 287 |
|
| 288 |
static char usage[] = "[-options]"; |
| 289 |
static char banner[] = "unit test driver for the null2 correction calculation"; |
| 290 |
|
| 291 |
int |
| 292 |
main(int argc, char **argv) |
| 293 |
{ |
| 294 |
ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 295 |
ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s")); |
| 296 |
ESL_ALPHABET *abc = esl_alphabet_Create(eslAMINO); |
| 297 |
P7_HMM *hmm = NULL; |
| 298 |
P7_BG *bg = NULL; |
| 299 |
P7_PROFILE *gm = NULL; |
| 300 |
P7_GMX *fwd = NULL; |
| 301 |
P7_GMX *bck = NULL; |
| 302 |
ESL_DSQ *dsq = NULL; |
| 303 |
int M = esl_opt_GetInteger(go, "-M"); |
| 304 |
int L = esl_opt_GetInteger(go, "-L"); |
| 305 |
|
| 306 |
|
| 307 |
p7_hmm_Sample(r, M, abc, &hmm); |
| 308 |
|
| 309 |
|
| 310 |
bg = p7_bg_Create(abc); |
| 311 |
p7_bg_SetLength(bg, L); |
| 312 |
gm = p7_profile_Create(hmm->M, abc); |
| 313 |
p7_ProfileConfig(hmm, bg, gm, L, p7_LOCAL); |
| 314 |
|
| 315 |
|
| 316 |
dsq = malloc(sizeof(ESL_DSQ) * (L+2)); |
| 317 |
fwd = p7_gmx_Create(gm->M, L); |
| 318 |
bck = p7_gmx_Create(gm->M, L); |
| 319 |
p7_FLogsumInit(); |
| 320 |
|
| 321 |
utest_correct_normalization(r, gm, bg, dsq, L, fwd, bck); |
| 322 |
|
| 323 |
free(dsq); |
| 324 |
p7_gmx_Destroy(fwd); |
| 325 |
p7_gmx_Destroy(bck); |
| 326 |
p7_profile_Destroy(gm); |
| 327 |
p7_bg_Destroy(bg); |
| 328 |
p7_hmm_Destroy(hmm); |
| 329 |
esl_alphabet_Destroy(abc); |
| 330 |
esl_randomness_Destroy(r); |
| 331 |
esl_getopts_Destroy(go); |
| 332 |
return 0; |
| 333 |
} |
| 334 |
#endif |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|