| 1 | /* Model configuration: |
| 2 | * Converting a core model to a fully configured Plan7 search profile. |
| 3 | * |
| 4 | * Contents: |
| 5 | * 1. Routines in the exposed API. |
| 6 | * 2. Unit tests. |
| 7 | * 3. Test driver. |
| 8 | * 4. Statistics collection driver. |
| 9 | * |
| 10 | * Revised May 2005: xref STL9/77-81. (Uniform fragment distribution) |
| 11 | * Again, Sept 2005: xref STL10/24-26. (Inherent target length dependency) |
| 12 | * Again, Jan 2007: xref STL11/125,136-137 (HMMER3) |
| 13 | * Again, Jul 2007: xref J1/103 (floating point ops) |
| 14 | */ |
| 15 | #include <p7_config.h> |
| 16 | |
| 17 | #include <math.h> |
| 18 | #include <float.h> |
| 19 | #include <string.h> |
| 20 | #include <ctype.h> |
| 21 | |
| 22 | #include "easel.h" |
| 23 | #include "esl_vectorops.h" |
| 24 | |
| 25 | #include "hmmer.h" |
| 26 | |
| 27 | |
| 28 | /***************************************************************** |
| 29 | * 1. Routines in the exposed API. |
| 30 | *****************************************************************/ |
| 31 | |
| 32 | /* Function: p7_ProfileConfig() |
| 33 | * Synopsis: Configure a search profile. |
| 34 | * |
| 35 | * Purpose: Given a model <hmm> with core probabilities, the null1 |
| 36 | * model <bg>, a desired search <mode> (one of <p7_LOCAL>, |
| 37 | * <p7_GLOCAL>, <p7_UNILOCAL>, or <p7_UNIGLOCAL>), and an |
| 38 | * expected target sequence length <L>; configure the |
| 39 | * search model in <gm> with lod scores relative to the |
| 40 | * background frequencies in <bg>. |
| 41 | * |
| 42 | * Returns: <eslOK> on success; the profile <gm> now contains |
| 43 | * scores and is ready for searching target sequences. |
| 44 | * |
| 45 | * Throws: <eslEMEM> on allocation error. |
| 46 | */ |
| 47 | int |
| 48 | p7_ProfileConfig(const P7_HMM *hmm, const P7_BG *bg, P7_PROFILE *gm, int L, int mode) |
| 49 | { |
| 50 | int k, x, z; /* counters over states, residues, annotation */ |
| 51 | int status; |
| 52 | float *occ = NULL; |
| 53 | float *tp, *rp; |
| 54 | float sc[p7_MAXCODE]; |
| 55 | float Z; |
| 56 | |
| 57 | /* Contract checks */ |
| 58 | if (gm->abc->type != hmm->abc->type) ESL_XEXCEPTION(eslEINVAL, "HMM and profile alphabet don't match"); |
| 59 | if (hmm->M > gm->allocM) ESL_XEXCEPTION(eslEINVAL, "profile too small to hold HMM"); |
| 60 | if (! (hmm->flags & p7H_CONS)) ESL_XEXCEPTION(eslEINVAL, "HMM must have a consensus to transfer to the profile"); |
| 61 | |
| 62 | /* Copy some pointer references and other info across from HMM */ |
| 63 | gm->M = hmm->M; |
| 64 | gm->max_length = hmm->max_length; |
| 65 | gm->mode = mode; |
| 66 | gm->roff = -1; |
| 67 | gm->eoff = -1; |
| 68 | gm->offs[p7_MOFFSET] = -1; |
| 69 | gm->offs[p7_FOFFSET] = -1; |
| 70 | gm->offs[p7_POFFSET] = -1; |
| 71 | if (gm->name != NULL) free(gm->name); |
| 72 | if (gm->acc != NULL) free(gm->acc); |
| 73 | if (gm->desc != NULL) free(gm->desc); |
| 74 | if ((status = esl_strdup(hmm->name, -1, &(gm->name))) != eslOK) goto ERROR; |
| 75 | if ((status = esl_strdup(hmm->acc, -1, &(gm->acc))) != eslOK) goto ERROR; |
| 76 | if ((status = esl_strdup(hmm->desc, -1, &(gm->desc))) != eslOK) goto ERROR; |
| 77 | if (hmm->flags & p7H_RF) strcpy(gm->rf, hmm->rf); |
| 78 | if (hmm->flags & p7H_MMASK) strcpy(gm->mm, hmm->mm); |
| 79 | if (hmm->flags & p7H_CONS) strcpy(gm->consensus, hmm->consensus); /* must be present, actually, so the flag test is just for symmetry w/ other optional HMM fields */ |
| 80 | if (hmm->flags & p7H_CS) strcpy(gm->cs, hmm->cs); |
| 81 | for (z = 0; z < p7_NEVPARAM; z++) gm->evparam[z] = hmm->evparam[z]; |
| 82 | for (z = 0; z < p7_NCUTOFFS; z++) gm->cutoff[z] = hmm->cutoff[z]; |
| 83 | for (z = 0; z < p7_MAXABET; z++) gm->compo[z] = hmm->compo[z]; |
| 84 | |
| 85 | /* Entry scores. */ |
| 86 | if (p7_profile_IsLocal(gm)) |
| 87 | { |
| 88 | /* Local mode entry: occ[k] /( \sum_i occ[i] * (M-i+1)) |
| 89 | * (Reduces to uniform 2/(M(M+1)) for occupancies of 1.0) */ |
| 90 | Z = 0.; |
| 91 | ESL_ALLOC(occ, sizeof(float) * (hmm->M+1)); |
| 92 | |
| 93 | if ((status = p7_hmm_CalculateOccupancy(hmm, occ, NULL)) != eslOK) goto ERROR; |
| 94 | for (k = 1; k <= hmm->M; k++) |
| 95 | Z += occ[k] * (float) (hmm->M-k+1); |
| 96 | for (k = 1; k <= hmm->M; k++) |
| 97 | p7P_TSC(gm, k-1, p7P_BM) = log(occ[k] / Z); /* note off-by-one: entry at Mk stored as [k-1][BM] */ |
| 98 | |
| 99 | free(occ); |
| 100 | } |
| 101 | else /* glocal modes: left wing retraction; must be in log space for precision */ |
| 102 | { |
| 103 | Z = log(hmm->t[0][p7H_MD]); |
| 104 | p7P_TSC(gm, 0, p7P_BM) = log(1.0 - hmm->t[0][p7H_MD]); |
| 105 | for (k = 1; k < hmm->M; k++) |
| 106 | { |
| 107 | p7P_TSC(gm, k, p7P_BM) = Z + log(hmm->t[k][p7H_DM]); |
| 108 | Z += log(hmm->t[k][p7H_DD]); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /* E state loop/move probabilities: nonzero for MOVE allows loops/multihits |
| 113 | * N,C,J transitions are set later by length config |
| 114 | */ |
| 115 | if (p7_profile_IsMultihit(gm)) { |
| 116 | gm->xsc[p7P_E][p7P_MOVE] = -eslCONST_LOG2; |
| 117 | gm->xsc[p7P_E][p7P_LOOP] = -eslCONST_LOG2; |
| 118 | gm->nj = 1.0f; |
| 119 | } else { |
| 120 | gm->xsc[p7P_E][p7P_MOVE] = 0.0f; |
| 121 | gm->xsc[p7P_E][p7P_LOOP] = -eslINFINITY; |
| 122 | gm->nj = 0.0f; |
| 123 | } |
| 124 | |
| 125 | /* Transition scores. */ |
| 126 | for (k = 1; k < gm->M; k++) { |
| 127 | tp = gm->tsc + k * p7P_NTRANS; |
| 128 | tp[p7P_MM] = log(hmm->t[k][p7H_MM]); |
| 129 | tp[p7P_MI] = log(hmm->t[k][p7H_MI]); |
| 130 | tp[p7P_MD] = log(hmm->t[k][p7H_MD]); |
| 131 | tp[p7P_IM] = log(hmm->t[k][p7H_IM]); |
| 132 | tp[p7P_II] = log(hmm->t[k][p7H_II]); |
| 133 | tp[p7P_DM] = log(hmm->t[k][p7H_DM]); |
| 134 | tp[p7P_DD] = log(hmm->t[k][p7H_DD]); |
| 135 | } |
| 136 | |
| 137 | /* Match emission scores. */ |
| 138 | sc[hmm->abc->K] = -eslINFINITY; /* gap character */ |
| 139 | sc[hmm->abc->Kp-2] = -eslINFINITY; /* nonresidue character */ |
| 140 | sc[hmm->abc->Kp-1] = -eslINFINITY; /* missing data character */ |
| 141 | for (k = 1; k <= hmm->M; k++) { |
| 142 | for (x = 0; x < hmm->abc->K; x++) |
| 143 | sc[x] = log((double)hmm->mat[k][x] / bg->f[x]); |
| 144 | |
| 145 | esl_abc_FExpectScVec(hmm->abc, sc, bg->f); |
| 146 | |
| 147 | for (x = 0; x < hmm->abc->Kp; x++) { |
| 148 | rp = gm->rsc[x] + k * p7P_NR; |
| 149 | rp[p7P_MSC] = sc[x]; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /* Insert emission scores */ |
| 154 | /* SRE, Fri Dec 5 08:41:08 2008: We currently hardwire insert scores |
| 155 | * to 0, i.e. corresponding to the insertion emission probabilities |
| 156 | * being equal to the background probabilities. Benchmarking shows |
| 157 | * that setting inserts to informative emission distributions causes |
| 158 | * more problems than it's worth: polar biased composition hits |
| 159 | * driven by stretches of "insertion" occur, and are difficult to |
| 160 | * correct for. |
| 161 | */ |
| 162 | for (x = 0; x < gm->abc->Kp; x++) |
| 163 | { |
| 164 | for (k = 1; k < hmm->M; k++) p7P_ISC(gm, k, x) = 0.0f; |
| 165 | p7P_ISC(gm, hmm->M, x) = -eslINFINITY; /* init I_M to impossible. */ |
| 166 | } |
| 167 | for (k = 1; k <= hmm->M; k++) p7P_ISC(gm, k, gm->abc->K) = -eslINFINITY; /* gap symbol */ |
| 168 | for (k = 1; k <= hmm->M; k++) p7P_ISC(gm, k, gm->abc->Kp-2) = -eslINFINITY; /* nonresidue symbol */ |
| 169 | for (k = 1; k <= hmm->M; k++) p7P_ISC(gm, k, gm->abc->Kp-1) = -eslINFINITY; /* missing data symbol */ |
| 170 | |
| 171 | |
| 172 | #if 0 |
| 173 | /* original (informative) insert setting: relies on sc[K, Kp-1] initialization to -inf above */ |
| 174 | for (k = 1; k < hmm->M; k++) { |
| 175 | for (x = 0; x < hmm->abc->K; x++) |
| 176 | sc[x] = log(hmm->ins[k][x] / bg->f[x]); |
| 177 | esl_abc_FExpectScVec(hmm->abc, sc, bg->f); |
| 178 | for (x = 0; x < hmm->abc->Kp; x++) { |
| 179 | rp = gm->rsc[x] + k*p7P_NR; |
| 180 | rp[p7P_ISC] = sc[x]; |
| 181 | } |
| 182 | } |
| 183 | for (x = 0; x < hmm->abc->Kp; x++) |
| 184 | p7P_ISC(gm, hmm->M, x) = -eslINFINITY; /* init I_M to impossible. */ |
| 185 | #endif |
| 186 | |
| 187 | /* Remaining specials, [NCJ][MOVE | LOOP] are set by ReconfigLength() |
| 188 | */ |
| 189 | gm->L = 0; /* force ReconfigLength to reconfig */ |
| 190 | if ((status = p7_ReconfigLength(gm, L)) != eslOK) goto ERROR; |
| 191 | return eslOK; |
| 192 | |
| 193 | ERROR: |
| 194 | if (occ != NULL) free(occ); |
| 195 | return status; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /* Function: p7_ReconfigLength() |
| 200 | * Synopsis: Set the target sequence length of a model. |
| 201 | * |
| 202 | * Purpose: Given a model already configured for scoring, in some |
| 203 | * particular algorithm mode; reset the expected length |
| 204 | * distribution of the profile for a new mean of <L>. |
| 205 | * |
| 206 | * This doesn't affect the length distribution of the null |
| 207 | * model. That must also be reset, using <p7_bg_SetLength()>. |
| 208 | * |
| 209 | * We want this routine to run as fast as possible, because |
| 210 | * the caller needs to dynamically reconfigure the model |
| 211 | * for the length of each target sequence in a database |
| 212 | * search. The profile has precalculated <gm->nj>, |
| 213 | * the number of times the J state is expected to be used, |
| 214 | * based on the E state loop transition in the current |
| 215 | * configuration. |
| 216 | * |
| 217 | * Returns: <eslOK> on success; xsc[NCJ] scores are set here. These |
| 218 | * control the target length dependence of the model. |
| 219 | */ |
| 220 | int |
| 221 | p7_ReconfigLength(P7_PROFILE *gm, int L) |
| 222 | { |
| 223 | float ploop, pmove; |
| 224 | |
| 225 | /* Configure N,J,C transitions so they bear L/(2+nj) of the total |
| 226 | * unannotated sequence length L. |
| 227 | */ |
| 228 | pmove = (2.0f + gm->nj) / ((float) L + 2.0f + gm->nj); /* 2/(L+2) for sw; 3/(L+3) for fs */ |
| 229 | ploop = 1.0f - pmove; |
| 230 | gm->xsc[p7P_N][p7P_LOOP] = gm->xsc[p7P_C][p7P_LOOP] = gm->xsc[p7P_J][p7P_LOOP] = log(ploop); |
| 231 | gm->xsc[p7P_N][p7P_MOVE] = gm->xsc[p7P_C][p7P_MOVE] = gm->xsc[p7P_J][p7P_MOVE] = log(pmove); |
| 232 | gm->L = L; |
| 233 | return eslOK; |
| 234 | } |
| 235 | |
| 236 | /* Function: p7_ReconfigMultihit() |
| 237 | * Synopsis: Quickly reconfig model into multihit mode for target length <L>. |
| 238 | * |
| 239 | * Purpose: Given a profile <gm> that's already been configured once, |
| 240 | * quickly reconfigure it into a multihit mode for target |
| 241 | * length <L>. |
| 242 | * |
| 243 | * This gets called in domain definition, when we need to |
| 244 | * flip the model in and out of unihit <L=0> mode to |
| 245 | * process individual domains. |
| 246 | * |
| 247 | * Note: You can't just flip uni/multi mode alone, because that |
| 248 | * parameterization also affects target length |
| 249 | * modeling. You need to make sure uni vs. multi choice is |
| 250 | * made before the length model is set, and you need to |
| 251 | * make sure the length model is recalculated if you change |
| 252 | * the uni/multi mode. Hence, these functions call |
| 253 | * <p7_ReconfigLength()>. |
| 254 | */ |
| 255 | int |
| 256 | p7_ReconfigMultihit(P7_PROFILE *gm, int L) |
| 257 | { |
| 258 | gm->xsc[p7P_E][p7P_MOVE] = -eslCONST_LOG2; |
| 259 | gm->xsc[p7P_E][p7P_LOOP] = -eslCONST_LOG2; |
| 260 | gm->nj = 1.0f; |
| 261 | return p7_ReconfigLength(gm, L); |
| 262 | } |
| 263 | |
| 264 | /* Function: p7_ReconfigUnihit() |
| 265 | * Synopsis: Quickly reconfig model into unihit mode for target length <L>. |
| 266 | * |
| 267 | * Purpose: Given a profile <gm> that's already been configured once, |
| 268 | * quickly reconfigure it into a unihit mode for target |
| 269 | * length <L>. |
| 270 | * |
| 271 | * This gets called in domain definition, when we need to |
| 272 | * flip the model in and out of unihit <L=0> mode to |
| 273 | * process individual domains. |
| 274 | */ |
| 275 | int |
| 276 | p7_ReconfigUnihit(P7_PROFILE *gm, int L) |
| 277 | { |
| 278 | gm->xsc[p7P_E][p7P_MOVE] = 0.0f; |
| 279 | gm->xsc[p7P_E][p7P_LOOP] = -eslINFINITY; |
| 280 | gm->nj = 0.0f; |
| 281 | return p7_ReconfigLength(gm, L); |
| 282 | } |
| 283 | |
| 284 | |
| 285 | /***************************************************************** |
| 286 | * 2. Unit tests |
| 287 | *****************************************************************/ |
| 288 | #ifdef p7MODELCONFIG_TESTDRIVE |
| 289 | |
| 290 | /* The Config test simply makes sure a random profile passes |
| 291 | * a Validate() check. |
| 292 | */ |
| 293 | static void |
| 294 | utest_Config(P7_HMM *hmm, P7_BG *bg) |
| 295 | { |
| 296 | char *msg = "modelconfig.c::p7_ProfileConfig() unit test failed"; |
| 297 | P7_PROFILE *gm = NULL; |
| 298 | |
| 299 | if ((gm = p7_profile_Create(hmm->M, hmm->abc)) == NULL) esl_fatal(msg); |
| 300 | if (p7_ProfileConfig(hmm, bg, gm, 350, p7_LOCAL) != eslOK) esl_fatal(msg); |
| 301 | if (p7_profile_Validate(gm, NULL, 0.0001) != eslOK) esl_fatal(msg); |
| 302 | |
| 303 | p7_profile_Destroy(gm); |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | /* Note that calculate_occupancy has moved to p7_hmm.c, but |
| 308 | * unit tests over there aren't hooked up yet; so leave a copy of the unit test |
| 309 | * here for now. |
| 310 | */ |
| 311 | static void |
| 312 | utest_occupancy(P7_HMM *hmm) |
| 313 | { |
| 314 | char *msg = "modelconfig.c::calculate_occupancy() unit test failed"; |
| 315 | float *occ; |
| 316 | float x; |
| 317 | |
| 318 | occ = malloc(sizeof(float) * (hmm->M+1)); |
| 319 | p7_hmm_CalculateOccupancy(hmm, occ, NULL); |
| 320 | x = esl_vec_FSum(occ+1, hmm->M) / (float) hmm->M; |
| 321 | if (esl_FCompare_old(x, 0.6, 0.1) != eslOK) esl_fatal(msg); |
| 322 | free(occ); |
| 323 | return; |
| 324 | } |
| 325 | #endif /*p7MODELCONFIG_TESTDRIVE*/ |
| 326 | |
| 327 | |
| 328 | |
| 329 | /***************************************************************** |
| 330 | * 3. Test driver |
| 331 | *****************************************************************/ |
| 332 | #ifdef p7MODELCONFIG_TESTDRIVE |
| 333 | |
| 334 | /* gcc -g -Wall -Dp7MODELCONFIG_TESTDRIVE -I. -I../easel -L. -L../easel -o modelconfig_utest modelconfig.c -lhmmer -leasel -lm |
| 335 | * ./modelconfig_utest |
| 336 | */ |
| 337 | #include "easel.h" |
| 338 | |
| 339 | #include <p7_config.h> |
| 340 | #include "hmmer.h" |
| 341 | |
| 342 | |
| 343 | int |
| 344 | main(int argc, char **argv) |
| 345 | { |
| 346 | ESL_ALPHABET *abc = NULL; |
| 347 | ESL_RANDOMNESS *r = NULL; |
| 348 | P7_HMM *hmm = NULL; |
| 349 | P7_BG *bg = NULL; |
| 350 | int M = 10000; |
| 351 | |
| 352 | if ((abc = esl_alphabet_Create(eslAMINO)) == NULL) esl_fatal("failed to create amino alphabet"); |
| 353 | if ((r = esl_randomness_CreateFast(0)) == NULL) esl_fatal("failed to create randomness"); |
| 354 | if (p7_hmm_Sample(r, M, abc, &hmm) != eslOK) esl_fatal("failed to sample random HMM"); |
| 355 | if ((bg = p7_bg_Create(abc)) == NULL) esl_fatal("failed to created null model"); |
| 356 | |
| 357 | utest_Config(hmm, bg); |
| 358 | utest_occupancy(hmm); |
| 359 | |
| 360 | p7_hmm_Destroy(hmm); |
| 361 | p7_bg_Destroy(bg); |
| 362 | esl_alphabet_Destroy(abc); |
| 363 | esl_randomness_Destroy(r); |
| 364 | return eslOK; |
| 365 | } |
| 366 | #endif /*p7MODELCONFIG_TESTDRIVE*/ |
| 367 | |
| 368 | |
| 369 | |
| 370 | /***************************************************************** |
| 371 | * 4. Statistics collection driver. |
| 372 | *****************************************************************/ |
| 373 | #ifdef p7MODELCONFIG_STATS |
| 374 | /* gcc -g -Wall -Dp7MODELCONFIG_STATS -I. -I../easel -L. -L../easel -o statprog modelconfig.c -lhmmer -leasel -lm |
| 375 | * ./statprog |
| 376 | */ |
| 377 | #include <p7_config.h> |
| 378 | |
| 379 | #include <stdio.h> |
| 380 | #include <stdlib.h> |
| 381 | |
| 382 | #include "easel.h" |
| 383 | #include "esl_alphabet.h" |
| 384 | #include "esl_dmatrix.h" |
| 385 | #include "esl_getopts.h" |
| 386 | #include "esl_random.h" |
| 387 | #include "esl_sq.h" |
| 388 | |
| 389 | #include "hmmer.h" |
| 390 | |
| 391 | static ESL_OPTIONS options[] = { |
| 392 | /* name type default env range toggles reqs incomp help docgroup*/ |
| 393 | { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 394 | { "-i", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "sample by two-step ideal rule, not from profile", 0}, |
| 395 | { "-m", eslARG_INFILE, NULL, NULL, NULL, NULL, NULL, "-u,-M", "input HMM from file <f> instead of sampling",0 }, |
| 396 | { "-n", eslARG_INT, "100000", NULL, "n>0", NULL, NULL, NULL, "number of seqs to sample", 0 }, |
| 397 | { "-s", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-m,-u", "make sampled HMM uniform transitions, as S/W", 0}, |
| 398 | { "-u", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-m,-s", "make sampled HMM ungapped", 0 }, |
| 399 | { "-L", eslARG_INT, "400", NULL,"n>=0", NULL, NULL, NULL, "set expected length from profile to <n>", 0 }, |
| 400 | { "-M", eslARG_INT, "50", NULL, "n>0", NULL, NULL, "-m", "set sampled model length to <n>", 0 }, |
| 401 | { "-2", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "emulate HMMER2 configuration", 0 }, |
| 402 | { "--ips", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "output PostScript mx of i endpoints to <f>", 0 }, |
| 403 | { "--kps", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "output PostScript mx of k endpoints to <f>", 0 }, |
| 404 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 405 | }; |
| 406 | |
| 407 | static char usage[] = "./statprog [options]"; |
| 408 | |
| 409 | static int ideal_local_endpoints(ESL_RANDOMNESS *r, P7_HMM *hmm, ESL_SQ *sq, P7_TRACE *tr, int Lbins, |
| 410 | int *ret_i1, int *ret_i2, int *ret_k1, int *ret_k2); |
| 411 | static int profile_local_endpoints(ESL_RANDOMNESS *r, P7_HMM *core, P7_PROFILE *gm, ESL_SQ *sq, P7_TRACE *tr, int Lbins, |
| 412 | int *ret_i1, int *ret_i2, int *ret_k1, int *ret_k2); |
| 413 | |
| 414 | int |
| 415 | main(int argc, char **argv) |
| 416 | { |
| 417 | ESL_ALPHABET *abc = NULL; /* sequence alphabet */ |
| 418 | ESL_GETOPTS *go = NULL; /* command line processing */ |
| 419 | ESL_RANDOMNESS *r = NULL; /* source of randomness */ |
| 420 | P7_HMM *hmm = NULL; /* sampled HMM to emit from */ |
| 421 | P7_HMM *core = NULL; /* safe copy of the HMM, before config */ |
| 422 | P7_BG *bg = NULL; /* null model */ |
| 423 | ESL_SQ *sq = NULL; /* sampled sequence */ |
| 424 | P7_TRACE *tr = NULL; /* sampled trace */ |
| 425 | P7_PROFILE *gm = NULL; /* profile */ |
| 426 | int i,j; |
| 427 | int i1,i2; |
| 428 | int k1,k2; |
| 429 | int iseq; |
| 430 | FILE *fp = NULL; |
| 431 | double expected; |
| 432 | |
| 433 | int do_ilocal; |
| 434 | char *hmmfile = NULL; |
| 435 | int nseq; |
| 436 | int do_swlike; |
| 437 | int do_ungapped; |
| 438 | int L; |
| 439 | int M; |
| 440 | int do_h2; |
| 441 | char *ipsfile = NULL; |
| 442 | char *kpsfile = NULL; |
| 443 | ESL_DMATRIX *imx = NULL; |
| 444 | ESL_DMATRIX *kmx = NULL; |
| 445 | ESL_DMATRIX *iref = NULL; /* reference matrix: expected i distribution under ideality */ |
| 446 | int Lbins; |
| 447 | int status; |
| 448 | char errbuf[eslERRBUFSIZE]; |
| 449 | |
| 450 | /***************************************************************** |
| 451 | * Parse the command line |
| 452 | *****************************************************************/ |
| 453 | go = esl_getopts_Create(options); |
| 454 | if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) esl_fatal("Failed to parse command line: %s\n", go->errbuf); |
| 455 | if (esl_opt_VerifyConfig(go) != eslOK) esl_fatal("Failed to parse command line: %s\n", go->errbuf); |
| 456 | if (esl_opt_GetBoolean(go, "-h") == TRUE) { |
| 457 | puts(usage); |
| 458 | puts("\n where options are:\n"); |
| 459 | esl_opt_DisplayHelp(stdout, go, 0, 2, 80); /* 0=all docgroups; 2 = indentation; 80=textwidth*/ |
| 460 | return eslOK; |
| 461 | } |
| 462 | do_ilocal = esl_opt_GetBoolean(go, "-i"); |
| 463 | hmmfile = esl_opt_GetString (go, "-m"); |
| 464 | nseq = esl_opt_GetInteger(go, "-n"); |
| 465 | do_swlike = esl_opt_GetBoolean(go, "-s"); |
| 466 | do_ungapped = esl_opt_GetBoolean(go, "-u"); |
| 467 | L = esl_opt_GetInteger(go, "-L"); |
| 468 | M = esl_opt_GetInteger(go, "-M"); |
| 469 | do_h2 = esl_opt_GetBoolean(go, "-2"); |
| 470 | ipsfile = esl_opt_GetString (go, "--ips"); |
| 471 | kpsfile = esl_opt_GetString (go, "--kps"); |
| 472 | |
| 473 | if (esl_opt_ArgNumber(go) != 0) { |
| 474 | puts("Incorrect number of command line arguments."); |
| 475 | printf("Usage: %s [options]\n", argv[0]); |
| 476 | return eslFAIL; |
| 477 | } |
| 478 | |
| 479 | r = esl_randomness_CreateFast(0); |
| 480 | |
| 481 | if (hmmfile != NULL) |
| 482 | { /* Read the HMM (and get alphabet from it) */ |
| 483 | P7_HMMFILE *hfp = NULL; |
| 484 | |
| 485 | status = p7_hmmfile_Open(hmmfile, NULL, &hfp, errbuf); |
| 486 | if (status == eslENOTFOUND) p7_Fail("File existence/permissions problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf); |
| 487 | else if (status == eslEFORMAT) p7_Fail("File format problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf); |
| 488 | else if (status != eslOK) p7_Fail("Unexpected error %d in opening HMM file %s.\n%s\n", status, hmmfile, errbuf); |
| 489 | |
| 490 | if ((status = p7_hmmfile_Read(hfp, &abc, &hmm)) != eslOK) { |
| 491 | if (status == eslEOD) esl_fatal("read failed, HMM file %s may be truncated?", hmmfile); |
| 492 | else if (status == eslEFORMAT) esl_fatal("bad file format in HMM file %s", hmmfile); |
| 493 | else if (status == eslEINCOMPAT) esl_fatal("HMM file %s contains different alphabets", hmmfile); |
| 494 | else esl_fatal("Unexpected error in reading HMMs"); |
| 495 | } |
| 496 | M = hmm->M; |
| 497 | p7_hmmfile_Close(hfp); |
| 498 | } |
| 499 | else |
| 500 | { /* Or sample the HMM (create alphabet first) */ |
| 501 | abc = esl_alphabet_Create(eslAMINO); |
| 502 | if (do_ungapped) p7_hmm_SampleUngapped(r, M, abc, &hmm); |
| 503 | else if (do_swlike) p7_hmm_SampleUniform (r, M, abc, 0.05, 0.5, 0.05, 0.2, &hmm); /* tmi, tii, tmd, tdd */ |
| 504 | else p7_hmm_Sample (r, M, abc, &hmm); |
| 505 | } |
| 506 | |
| 507 | Lbins = M; |
| 508 | imx = esl_dmatrix_Create(Lbins, Lbins); |
| 509 | iref = esl_dmatrix_Create(Lbins, Lbins); |
| 510 | kmx = esl_dmatrix_Create(M, M); |
| 511 | esl_dmatrix_SetZero(imx); |
| 512 | esl_dmatrix_SetZero(iref); |
| 513 | esl_dmatrix_SetZero(kmx); |
| 514 | tr = p7_trace_Create(); |
| 515 | sq = esl_sq_CreateDigital(abc); |
| 516 | bg = p7_bg_Create(abc); |
| 517 | core = p7_hmm_Clone(hmm); |
| 518 | |
| 519 | if (do_h2) { |
| 520 | gm = p7_profile_Create(hmm->M, abc); |
| 521 | p7_H2_ProfileConfig(hmm, bg, gm, p7_UNILOCAL); |
| 522 | } else { |
| 523 | gm = p7_profile_Create(hmm->M, abc); |
| 524 | p7_ProfileConfig(hmm, bg, gm, L, p7_UNILOCAL); |
| 525 | if (p7_hmm_Validate (hmm, NULL, 0.0001) != eslOK) esl_fatal("whoops, HMM is bad!"); |
| 526 | if (p7_profile_Validate(gm, NULL, 0.0001) != eslOK) esl_fatal("whoops, profile is bad!"); |
| 527 | } |
| 528 | |
| 529 | /* Sample endpoints. |
| 530 | * Also sample an ideal reference distribution for i endpoints. i |
| 531 | * endpoints are prone to discretization artifacts, when emitted |
| 532 | * sequences have varying lengths. Taking log odds w.r.t. an ideal |
| 533 | * reference that is subject to the same discretization artifacts |
| 534 | * cancels out the effect. |
| 535 | */ |
| 536 | for (iseq = 0; iseq < nseq; iseq++) |
| 537 | { |
| 538 | if (do_ilocal) ideal_local_endpoints (r, core, sq, tr, Lbins, &i1, &i2, &k1, &k2); |
| 539 | else profile_local_endpoints(r, core, gm, sq, tr, Lbins, &i1, &i2, &k1, &k2); |
| 540 | |
| 541 | imx->mx[i1-1][i2-1] += 1.; |
| 542 | kmx->mx[k1-1][k2-1] += 1.; |
| 543 | |
| 544 | /* reference distribution for i */ |
| 545 | ideal_local_endpoints (r, core, sq, tr, Lbins, &i1, &i2, &k1, &k2); |
| 546 | iref->mx[i1-1][i2-1] += 1.; |
| 547 | } |
| 548 | |
| 549 | |
| 550 | /* Adjust both mx's to log_2(obs/exp) ratio */ |
| 551 | printf("Before normalization/log-odds:\n"); |
| 552 | printf(" i matrix values range from %f to %f\n", dmx_upper_min(imx), dmx_upper_max(imx)); |
| 553 | printf(" k matrix values range from %f to %f\n", dmx_upper_min(kmx), dmx_upper_max(kmx)); |
| 554 | printf("iref matrix values range from %f to %f\n", dmx_upper_min(iref), dmx_upper_max(iref)); |
| 555 | |
| 556 | expected = (double) nseq * 2. / (double) (M*(M+1)); |
| 557 | for (i = 0; i < kmx->m; i++) |
| 558 | for (j = i; j < kmx->n; j++) |
| 559 | kmx->mx[i][j] = log(kmx->mx[i][j] / expected) / log(2.0); |
| 560 | |
| 561 | for (i = 0; i < imx->m; i++) |
| 562 | for (j = i; j < imx->m; j++) |
| 563 | if (iref->mx[i][j] == 0. && imx->mx[i][j] == 0.) |
| 564 | imx->mx[i][j] = 0.; |
| 565 | else if (iref->mx[i][j] == 0.) |
| 566 | imx->mx[i][j] = eslINFINITY; |
| 567 | else if (imx->mx[i][j] == 0.) |
| 568 | imx->mx[i][j] = -eslINFINITY; |
| 569 | else |
| 570 | imx->mx[i][j] = log(imx->mx[i][j] / iref->mx[i][j]) / log(2.0); |
| 571 | |
| 572 | /* Print ps files */ |
| 573 | if (kpsfile != NULL) { |
| 574 | if ((fp = fopen(kpsfile, "w")) == NULL) esl_fatal("Failed to open output postscript file %s", kpsfile); |
| 575 | dmx_Visualize(fp, kmx, -4., 5.); |
| 576 | fclose(fp); |
| 577 | } |
| 578 | if (ipsfile != NULL) { |
| 579 | if ((fp = fopen(ipsfile, "w")) == NULL) esl_fatal("Failed to open output postscript file %s", ipsfile); |
| 580 | dmx_Visualize(fp, imx, -4., 5.); |
| 581 | /* dmx_Visualize(fp, imx, dmx_upper_min(imx), dmx_upper_max(imx)); */ |
| 582 | fclose(fp); |
| 583 | } |
| 584 | |
| 585 | printf("After normalization/log-odds:\n"); |
| 586 | printf("i matrix values range from %f to %f\n", dmx_upper_min(imx), dmx_upper_max(imx)); |
| 587 | printf("k matrix values range from %f to %f\n", dmx_upper_min(kmx), dmx_upper_max(kmx)); |
| 588 | |
| 589 | |
| 590 | p7_profile_Destroy(gm); |
| 591 | p7_bg_Destroy(bg); |
| 592 | p7_hmm_Destroy(core); |
| 593 | p7_hmm_Destroy(hmm); |
| 594 | p7_trace_Destroy(tr); |
| 595 | esl_sq_Destroy(sq); |
| 596 | esl_dmatrix_Destroy(imx); |
| 597 | esl_dmatrix_Destroy(kmx); |
| 598 | esl_alphabet_Destroy(abc); |
| 599 | esl_randomness_Destroy(r); |
| 600 | esl_getopts_Destroy(go); |
| 601 | return eslOK; |
| 602 | } |
| 603 | |
| 604 | /* ideal_local_endpoints() |
| 605 | * |
| 606 | * Purpose: Implementation of the "two-step" fragment sampling |
| 607 | * algorithm, sampling a uniform local fragment w.r.t. |
| 608 | * sequence coords, by first sampling a complete |
| 609 | * sequence of length L from <hmm>; then choosing |
| 610 | * a random fragment <i1..i2> uniformly from all |
| 611 | * possible $\frac{L(L+1)/2}$ fragments; then finding |
| 612 | * local alignment coordinates wrt model and sequence, |
| 613 | * using convention that local alignment starts/stops |
| 614 | * with match states. (Thus, if the initially selected |
| 615 | * i1 or i2 were generated by insert states, bounds |
| 616 | * are moved to reach first/last match state.) |
| 617 | * |
| 618 | * The caller also provides an allocated sequence <sq> and |
| 619 | * traceback <tr>, as storage to be provided to |
| 620 | * <p7_CoreEmit()>. They contain the generated global |
| 621 | * sequence and trace upon return (not a local trace, note). |
| 622 | * |
| 623 | * i endpoints are normalized/discretized to 1..<Lbins>, so |
| 624 | * we can collate i statistics from sampled sequences of |
| 625 | * varying L. Note this causes discretization artifacts, |
| 626 | * leading to underrepresentation of j=M and |
| 627 | * overrepresentation of i=1. |
| 628 | * |
| 629 | * This routine is only intended for collecting endpoint |
| 630 | * statistics (i1,i2,k1,k2); it does not generate a local |
| 631 | * alignment trace. (xref milestone 2, STL11/115). |
| 632 | * |
| 633 | * Returns: <eslOK> on success; returns normalized/binned sequence |
| 634 | * coords in <*ret_i1> and <*ret_i2> in range <1..Lbins> and |
| 635 | * the model entry/exit coords in <*ret_k1> and <*ret_k2> in |
| 636 | * range <1..M>. By internal def'n of local alignment endpoints, |
| 637 | * M_k1 emits residue x_i1, M_k2 emits residue x_i2. |
| 638 | * |
| 639 | * Xref: STL11/142-143 |
| 640 | */ |
| 641 | static int |
| 642 | ideal_local_endpoints(ESL_RANDOMNESS *r, P7_HMM *hmm, ESL_SQ *sq, P7_TRACE *tr, int Lbins, |
| 643 | int *ret_i1, int *ret_i2, int *ret_k1, int *ret_k2) |
| 644 | { |
| 645 | int status; |
| 646 | int tpos; |
| 647 | int i1, i2, k1,k2, t1,t2; |
| 648 | int all_insert; |
| 649 | int failsafe = 0; /* a failsafe timer for rejection sampling */ |
| 650 | |
| 651 | do { |
| 652 | if (failsafe++ == 1000) ESL_XEXCEPTION(eslENOHALT, "failed to obtain local alignment that wasn't all inserts"); |
| 653 | |
| 654 | if ((status = p7_CoreEmit(r, hmm, sq, tr)) != eslOK) goto ERROR; |
| 655 | |
| 656 | /* a simple way to sample uniformly from upper triangle is by rejection |
| 657 | * this do/while cannot infinite loop, doesn't need failsafe |
| 658 | */ |
| 659 | do { |
| 660 | i1 = 1 + esl_rnd_Roll(r, sq->n); |
| 661 | i2 = 1 + esl_rnd_Roll(r, sq->n); |
| 662 | } while (i1 > i2); |
| 663 | |
| 664 | /* Get initial k1,k2 coords: this step must work in a core model, |
| 665 | * i1/i2 were generated by an M or I. Also record t1,t2 endpoints |
| 666 | * on core's trace. |
| 667 | */ |
| 668 | for (tpos = 0; tpos < tr->N; tpos++) |
| 669 | if (tr->i[tpos] == i1) { t1 = tpos; k1 = tr->k[tpos]; break; } |
| 670 | for (tpos = tr->N-1; tpos >= 0; tpos--) |
| 671 | if (tr->i[tpos] == i2) { t2 = tpos; k2 = tr->k[tpos]; break; } |
| 672 | |
| 673 | /* Enforce the definition of local alignment endpoints being |
| 674 | * match-delimited - roll up any leading/trailing I states. |
| 675 | * Watch out for pathological case of a local fragment that |
| 676 | * includes no M state at all. |
| 677 | */ |
| 678 | all_insert = FALSE; |
| 679 | for (; t1 <= t2; t1++) if (tr->st[t1] == p7T_M) break; |
| 680 | for (; t2 >= t1; t2--) if (tr->st[t2] == p7T_M) break; |
| 681 | if (t2 < t1) all_insert = TRUE; /* sufficient to check both. */ |
| 682 | i1 = tr->i[t1]; i2 = tr->i[t2]; |
| 683 | k1 = tr->k[t1]; k2 = tr->k[t2]; |
| 684 | } while (all_insert); |
| 685 | |
| 686 | /* Normalize sequence coords. |
| 687 | * They're 1..L now; make them 1..Lbins |
| 688 | */ |
| 689 | *ret_i1 = ((i1-1) * Lbins / sq->n) + 1; |
| 690 | *ret_i2 = ((i2-1) * Lbins / sq->n) + 1; |
| 691 | *ret_k1 = k1; |
| 692 | *ret_k2 = k2; |
| 693 | return eslOK; |
| 694 | |
| 695 | ERROR: |
| 696 | *ret_i1 = 0.; |
| 697 | *ret_i2 = 0.; |
| 698 | *ret_k1 = 0; |
| 699 | *ret_k2 = 0; |
| 700 | return status; |
| 701 | } |
| 702 | |
| 703 | /* profile_local_endpoints() |
| 704 | * |
| 705 | * Purpose: Wrapper around <p7_ProfileEmit()>, sampling a local |
| 706 | * alignment fragment from the profile's probabilistic model |
| 707 | * (which may be the implicit model of HMMER3, or the |
| 708 | * Plan7 model of HMMER2), and reporting coordinates |
| 709 | * of the fragment w.r.t. both model and sequence. |
| 710 | * |
| 711 | * To simplify the implementation, the profile must be in |
| 712 | * <p7_UNILOCAL> mode, not <p7_LOCAL> mode, so we know we |
| 713 | * only have to deal with a single hit per sampled |
| 714 | * sequence. |
| 715 | * |
| 716 | * We want <i1..i2> to be relative to the sequence coords |
| 717 | * of a complete (global) sampled sequence that we could |
| 718 | * have sampled this local alignment from; but the <i1..i2> |
| 719 | * we initially get are relative to our profile-sampled |
| 720 | * trace, so they are offset both by N-generated residues |
| 721 | * that occur in the profile and by residues that the |
| 722 | * profile's local entry skipped. To translate from |
| 723 | * profile/sequence coords to core model/sequence coords, |
| 724 | * we use rejection sampling: sample traces from the core |
| 725 | * model until we find one that uses the same statetypes |
| 726 | * at *initial* entry/exit points <k1>,<k2>, then use |
| 727 | * that sample's sequence to determine offsets and correct |
| 728 | * <i1..i2> reference frame. |
| 729 | * |
| 730 | * Local alignment endpoints are defined to be |
| 731 | * match-delimited. However, an H3 model allows exit on |
| 732 | * either a D or M state. Thus, the initially sampled end |
| 733 | * point k2 may need to be rolled back to last M state, to |
| 734 | * satisfy local alignment endpoint definition. Entries are |
| 735 | * not a problem; both H2 and H3 profiles can only enter on |
| 736 | * a M state. (This rollback has to occur after we've |
| 737 | * matched a core trace to the profile trace to determine |
| 738 | * i offsets.) |
| 739 | * |
| 740 | * Then, sampling from both the core model and the profile |
| 741 | * in the same routine introduces a complication: |
| 742 | * conceivably, profile configuration alters the transition |
| 743 | * probabilities in the core model (by adding <M->E> |
| 744 | * transitions and renormalizing the M transition |
| 745 | * distributions, for example; H2 configuration does this, |
| 746 | * though H3 does not). So you can't <CoreSample()> the |
| 747 | * <gm->hmm> safely. To avoid such things, the caller |
| 748 | * provides a clean copy of the core model in <core>. |
| 749 | * |
| 750 | * i endpoints are normalized/discretized to 1..<Lbins>, so |
| 751 | * we can collate i statistics from sampled sequences of |
| 752 | * varying L. Note this causes discretization artifacts, |
| 753 | * leading to underrepresentation of j=M and |
| 754 | * overrepresentation of i=1. |
| 755 | * |
| 756 | * Returns: <eslOK> on success; returns normalized sequence coords in |
| 757 | * <*ret_i1> and <*ret_i2>, and the model entry/exit coords |
| 758 | * in <*ret_k1> and <*ret_k2>. |
| 759 | * |
| 760 | * Xref: STL11/142-143 |
| 761 | */ |
| 762 | static int |
| 763 | profile_local_endpoints(ESL_RANDOMNESS *r, P7_HMM *core, P7_PROFILE *gm, ESL_SQ *sq, P7_TRACE *tr, int Lbins, |
| 764 | int *ret_i1, int *ret_i2, int *ret_k1, int *ret_k2) |
| 765 | { |
| 766 | int status; |
| 767 | int i1,i2; |
| 768 | int k1,k2; |
| 769 | int t1,t2; /* entry/exit positions in local trace, tr */ |
| 770 | int tg1, tg2; /* entry/exit positions in global trace, tr2 */ |
| 771 | int tpos; |
| 772 | int nterm, cterm; /* offsets at N, C terminus. */ |
| 773 | int L; /* inferred length from 3-part patching */ |
| 774 | ESL_SQ *sq2 = NULL; |
| 775 | P7_TRACE *tr2 = NULL; |
| 776 | int failsafe = 0; |
| 777 | |
| 778 | if (gm->mode != p7_UNILOCAL) ESL_XEXCEPTION(eslEINVAL, "profile must be unilocal"); |
| 779 | if ((sq2 = esl_sq_CreateDigital(gm->abc)) == NULL) { status = eslEMEM; goto ERROR; } |
| 780 | if ((tr = p7_trace_Create()) == NULL) { status = eslEMEM; goto ERROR; } |
| 781 | |
| 782 | /* sample local alignment from the implicit model */ |
| 783 | if (gm->h2_mode) { |
| 784 | if ((status = p7_H2_ProfileEmit(r, gm, sq, tr)) != eslOK) goto ERROR; |
| 785 | } else { |
| 786 | if ((status = p7_ProfileEmit(r, gm, sq, tr)) != eslOK) goto ERROR; |
| 787 | } |
| 788 | |
| 789 | /* Get initial trace coords */ |
| 790 | for (tpos = 0; tpos < tr->N; tpos++) if (tr->st[tpos] == p7T_B) { t1 = tpos+1; break; } |
| 791 | for (tpos = tr->N-1; tpos >= 0; tpos--) if (tr->st[tpos] == p7T_E) { t2 = tpos-1; break; } |
| 792 | |
| 793 | /* Match a core trace to this local trace by rejection sampling; |
| 794 | * this is to let us calculate sequence offsets; see comments above in preamble |
| 795 | */ |
| 796 | do { |
| 797 | if (failsafe++ == 100000) ESL_XEXCEPTION(eslENOHALT, "failed to match core,local traces in %d tries\n", failsafe); |
| 798 | |
| 799 | if ((status = p7_CoreEmit(r, core, sq2, tr2)) != eslOK) goto ERROR; |
| 800 | for (tpos = 0; tpos < tr2->N; tpos++) |
| 801 | if (tr2->k[tpos] == tr->k[t1]) { tg1 = tpos; break; } |
| 802 | for (tpos = tr2->N-1; tpos >= 0; tpos--) |
| 803 | if (tr2->k[tpos] == tr->k[t2]) { tg2 = tpos; break; } |
| 804 | } while (tr2->st[tg1] != tr->st[t1] && tr2->st[tg2] != tr->st[t2]); |
| 805 | |
| 806 | /* tg1..tg2 in core trace is now matched to t1..t2 in the profile trace. |
| 807 | * Calculate # of residues preceding tg1 and following tg2 in the core trace. |
| 808 | * A core trace can only generate residues from M or I states. |
| 809 | */ |
| 810 | for (nterm = 0, tpos = 0; tpos < tg1; tpos++) |
| 811 | if (tr2->st[tpos] == p7T_M || tr2->st[tpos] == p7T_I) nterm++; |
| 812 | for (cterm = 0, tpos = tr2->N-1; tpos > tg2; tpos--) |
| 813 | if (tr2->st[tpos] == p7T_M || tr2->st[tpos] == p7T_I) cterm++; |
| 814 | |
| 815 | /* rectify the t2 endpoint, rolling back any trailing D path |
| 816 | */ |
| 817 | for (; t2 >= 0; t2--) if (tr->st[t2] == p7T_M) break; |
| 818 | if (t2 < t1) ESL_XEXCEPTION(eslEINCONCEIVABLE, "this only happens on an all-D path through profile"); |
| 819 | |
| 820 | /* determine initial endpoint coords from t1 and t2 */ |
| 821 | i1 = tr->i[t1]; i2 = tr->i[t2]; |
| 822 | k1 = tr->k[t1]; k2 = tr->k[t2]; |
| 823 | |
| 824 | /* offset the i coords. */ |
| 825 | L = (i2-i1+1) + nterm + cterm; |
| 826 | i2 = (i2-i1+1) + nterm; |
| 827 | i1 = nterm+1; |
| 828 | |
| 829 | /* normalize the i coords into range 1..Lbins, instead of 1..L */ |
| 830 | i1 = ((i1-1) * Lbins / L) + 1; |
| 831 | i2 = ((i2-1) * Lbins / L) + 1; |
| 832 | |
| 833 | *ret_i1 = i1; |
| 834 | *ret_i2 = i2; |
| 835 | *ret_k1 = k1; |
| 836 | *ret_k2 = k2; |
| 837 | p7_trace_Destroy(tr2); |
| 838 | esl_sq_Destroy(sq2); |
| 839 | return eslOK; |
| 840 | |
| 841 | ERROR: |
| 842 | if (sq2 != NULL) esl_sq_Destroy(sq2); |
| 843 | if (tr2 != NULL) p7_trace_Destroy(tr2); |
| 844 | *ret_i1 = 0.; |
| 845 | *ret_i2 = 0.; |
| 846 | *ret_k1 = 0; |
| 847 | *ret_k2 = 0; |
| 848 | return status; |
| 849 | } |
| 850 | |
| 851 | |
| 852 | |
| 853 | #endif /*p7MODELCONFIG_STATS*/ |
| 854 | |
| 855 | |
| 856 | /* All the commentary below here is archaic and obsolete. |
| 857 | * It is only a shadow of the current truth, and may mislead. |
| 858 | * It is of archaeological interest only; needs to be whipped back |
| 859 | * into shape in real documentation. |
| 860 | */ |
| 861 | |
| 862 | /*---------------------------------------------------------------------- |
| 863 | * Preamble. |
| 864 | * |
| 865 | * There are four search modes: |
| 866 | * single-hit multi-hit |
| 867 | * -------------------- ------------------------ |
| 868 | * local | sw (p7_UNILOCAL) fs (p7_LOCAL) |
| 869 | * glocal | s (p7_UNIGLOCAL) ls (p7_GLOCAL) |
| 870 | * |
| 871 | * Additionally, each search mode is configured for a particular |
| 872 | * target length. Thus "LS/400" means a model configured for glocal, |
| 873 | * multihit alignment of a target sequence of length 400. |
| 874 | * |
| 875 | *----------------------------------------------------------------------- |
| 876 | * Exegesis. |
| 877 | * |
| 878 | * When you enter this module, you've got an HMM (P7_HMM) in "core" |
| 879 | * probability form: t[], mat[], ins[] are all valid, normalized |
| 880 | * probabilities. The routines here are used to create the "profile" |
| 881 | * form (P7_PROFILE) of the model: tsc[], msc[], isc[], bsc[], esc[], |
| 882 | * and xsc[] fields as integer log-odds scores. |
| 883 | * |
| 884 | * Also in the process, xt[] are set to their algorithm-dependent |
| 885 | * probabilities, though these probabilities are only for reference. |
| 886 | * |
| 887 | * The configuration process breaks down into distinct conceptual steps: |
| 888 | * |
| 889 | * 1. Algorithm configuration. |
| 890 | * An "algorithm mode" is chosen. This determines whether |
| 891 | * alignments will allow local entry/exit in the model, and sets |
| 892 | * the probabilities in xt[XTE], which determine |
| 893 | * multi-hit/single-hit behavior. The "nj" value of the HMM is |
| 894 | * also set here (the expected # of times the J state will be used; |
| 895 | * 0 for single-hit mode and 1 for the default parameterization of |
| 896 | * multihit modes). |
| 897 | * |
| 898 | * 2. Wing retraction. |
| 899 | * In a profile, the D_1 and D_M states of the core model are |
| 900 | * removed. The probability of the paths B->D1...->Mk ("BMk") that |
| 901 | * enter D1 and use all D's before reaching M_k is treated instead |
| 902 | * as an additional dollop of B->Mk entry probability, and the |
| 903 | * probability of paths Mk->Dk+1...D_M->E ("MkE") is treated |
| 904 | * instead as an additional dollop of Mk->E exit probability. The |
| 905 | * MkE path probability is subtracted from the Mk->Dk+1 transition. |
| 906 | * |
| 907 | * In local algorithm modes, these extra dollops are ignored, and |
| 908 | * the model is renormalized appropriately. That is, the algorithm |
| 909 | * overrides all B->DDDD->M and/or M->DDDD->E path probabilities |
| 910 | * with its own internal entry/exit probabilities. |
| 911 | * |
| 912 | * If the algorithm mode is "global" at either entry or exit, then |
| 913 | * the internal entries are set to BMk and internal exits are set |
| 914 | * to MkE, and the model is renormalized appropriately. That is, |
| 915 | * the algorithm treats B->DDDD->M and/or M->DDDD->E path |
| 916 | * probabilities as internal entries/exits, instead of allowing |
| 917 | * dynamic programming algorithms to use the D_1 or D_M states. |
| 918 | * |
| 919 | * These two alternatives are represented differently in traces, |
| 920 | * where an X state is used to signal 'missing data' in a local |
| 921 | * alignment. Thus B->X->Mk indicates local entry, whereas B->Mk in |
| 922 | * a trace indicates a wing-retracted B->DDD->Mk entry with respect |
| 923 | * to the core HMM; similarly Mk->X->E indicates local exit, and |
| 924 | * Mk->E indicates a Mk->DDDD->E path in the core HMM. |
| 925 | * |
| 926 | * Wing retraction is a compulsive detail with two purposes. First, |
| 927 | * it removes a mute cycle from the model, B->D1 ...D_M->E, which |
| 928 | * cannot be correctly and efficiently dealt with by DP |
| 929 | * recursions. (A DP algorithm could just *ignore* that path |
| 930 | * though, and ignore the negligible amount of probability in it.) |
| 931 | * Second, wing retraction reconciles the algorithm-dependent |
| 932 | * entry/exit probabilities with the core model. For algorithms |
| 933 | * that impose local internal entry/exit, we don't want there to be |
| 934 | * any additional probability coming from "internal" B->DDD->M and |
| 935 | * M->DDD->E paths, so wing retraction takes it away. |
| 936 | * |
| 937 | * 3. Local alignment D-path leveling. |
| 938 | * For fully local alignments, we want every fragment ij (starting |
| 939 | * at match i, ending from match j) to be equiprobable. There are |
| 940 | * M(M+1)/2 possible such fragments, so the probability of each |
| 941 | * one is 2/M(M+1). |
| 942 | * |
| 943 | * Notionally, we imagine a "model" consisting of the M(M+1)/2 |
| 944 | * possible fragments, with entry probability of 2/M(M+1) for each. |
| 945 | * |
| 946 | * Operationally, we achieve this by a trick inspired by a |
| 947 | * suggestion from Bill Bruno. Bill suggested that for a model with |
| 948 | * no delete states, if we set begin[k] = 1/(M-k+1) and end[k] = |
| 949 | * (M-k+1) / [M(M+1)/2], all fragments are equiprobable: the prob |
| 950 | * of any given fragment is |
| 951 | * b_i * e_j * \prod_{k=i}^{j-1} (1-e_k); |
| 952 | * that is, the fragment also includes (j-i) penalizing terms for |
| 953 | * *not* ending at i..j-1. Remarkably, this gives the result we |
| 954 | * want: this product is always 2/M(M+1), for any ij. |
| 955 | * |
| 956 | * However, D->D transitions throw a wrench into this trick, |
| 957 | * though. A local alignment that goes M_i->D...D->M_j, for |
| 958 | * example, only gets hit with one not-end penalty (for the |
| 959 | * M_i->D). This means that paths including deletions will be |
| 960 | * artifactually favored. |
| 961 | * |
| 962 | * A solution is to subtract log(1-e_k) from the deletion |
| 963 | * transition scores as well as the match transition scores. Thus |
| 964 | * one log(1-e_k) penalty is always exacted upon transitioning from |
| 965 | * any node k->k+1. This is *not* part of the probabilistic model: |
| 966 | * it is a score accounting trick that forces the DP algorithms to |
| 967 | * associate a log(1-e_k) penalty for each node k->k+1 transition, |
| 968 | * which makes the DP calculations give the result desired for our |
| 969 | * *notional* probabilistic model with a single 2/M(M+1) transition |
| 970 | * for each possible fragment. (A similar accounting trick is the |
| 971 | * use of log-odds scoring, where we associate null model |
| 972 | * transitions and emissions with appropriate terms in the HMM, to |
| 973 | * assure that the final score of any path accounts for all the |
| 974 | * desired probability terms in an overall log-odds score). The |
| 975 | * overall score of any fragment can be rearranged such that there |
| 976 | * is one term consisting of a product of all these penalties * b_i |
| 977 | * * e_j = 2/M(M+1), and another term consisting of the actual |
| 978 | * model transition path score between i,j. |
| 979 | * |
| 980 | * 4. Target length dependence. |
| 981 | * Given a particular target sequence of length L, we want our HMM score |
| 982 | * to be as independent as possible of L. Otherwise, long sequences will |
| 983 | * give higher scores, even if they are nonhomologous. |
| 984 | * |
| 985 | * The traditional solution to this is Karlin/Altschul statistics, |
| 986 | * which tells us that E(s=x) = KMNe^-{\lambda x}, so we expect to |
| 987 | * have to make a -1 bit score correction for every 2x increase in |
| 988 | * target sequence length (ignoring edge correction effects). K/A |
| 989 | * statistics have been proven for local Viterbi single-hit |
| 990 | * ungapped alignments. There is abundant literature showing they |
| 991 | * hold empirically for local Viterbi single-hit gapped |
| 992 | * alignments. In my hands the length dependence (though not the |
| 993 | * form of the distribution) holds for any single-hit alignment |
| 994 | * (local or glocal, Viterbi or forward) but it does not |
| 995 | * hold for multihit alignment modes. |
| 996 | * |
| 997 | * HMMER's solution is to build the length dependence right into |
| 998 | * the probabilistic model, so that we have a full probabilistic |
| 999 | * model of the target sequence. We match the expected lengths of |
| 1000 | * the model M and the null model R by setting the p1, N, C, and J |
| 1001 | * transitions appropriately. R has to emit the whole sequence, so |
| 1002 | * it has a self-transition of L/(L+1). N, C, and J have to emit |
| 1003 | * (L-(k+1)x) residues of the sequence, where x is the expected |
| 1004 | * length of an alignment to the core model, and k is the expected |
| 1005 | * number of times that we cycle through the J state. k=0 in sw |
| 1006 | * mode, and k=1 in fs/ls mode w/ the standard [XTE][LOOP] |
| 1007 | * probability of 0.5. |
| 1008 | * |
| 1009 | * 5. Conversion of probabilities to integer log-odds scores. |
| 1010 | * This step incorporates the contribution of the null model, |
| 1011 | * and converts floating-point probs to the scaled integer log-odds |
| 1012 | * score values that are used by the DP alignment routines. |
| 1013 | * |
| 1014 | * Step 1 is done by the main p7_ProfileConfig() function, which takes |
| 1015 | * a choice of algorithm mode as an argument. |
| 1016 | * |
| 1017 | * Step 2 is done by the *wing_retraction*() functions, which also |
| 1018 | * go ahead and convert the affected transitions to log-odds scores; |
| 1019 | * left wing retraction sets bsc[], right wing retraction sets |
| 1020 | * esc[] and tsc[TM*]. |
| 1021 | * |
| 1022 | * Step 3 is carried out by one of two delete path accounting routines, |
| 1023 | * which go ahead and set tsc[TD*]. |
| 1024 | * |
| 1025 | * Step 4 is carried out by the p7_ReconfigLength() routine. |
| 1026 | * |
| 1027 | * Step 5 is carried out for all remaining scores by logoddsify_the_rest(). |
| 1028 | * |
| 1029 | * Note that the profile never exists in a configured probability |
| 1030 | * form. The probability model for the search profile is implicit, not |
| 1031 | * explicit, because of the handling of local entry/exit transitions. |
| 1032 | * You can see this in more detail in emit.c:p7_ProfileEmit() |
| 1033 | * function, which samples sequences from the profile's probabilistic |
| 1034 | * model. |
| 1035 | * |
| 1036 | * So, overall, to find where the various scores and probs are set: |
| 1037 | * bsc : wing retraction (section 2) |
| 1038 | * esc : wing retraction (section 2) |
| 1039 | * tsc[TM*] : wing retraction (section 2) |
| 1040 | * tsc[TI*] : logoddsify_the_rest() (section 4) |
| 1041 | * tsc[TD*] : dpath leveling (section 3) |
| 1042 | * p1 : target_ldependence() (section 4) |
| 1043 | * xt[NCJ] : target_ldependence() (section 4) |
| 1044 | * xsc (all): logoddsify_the_rest() (section 4) |
| 1045 | * msc : logoddsify_the_rest() (section 5) |
| 1046 | * isc : logoddsify_the_rest() (section 5) |
| 1047 | */ |
| 1048 | |
| 1049 | |
| 1050 | /***************************************************************** |
| 1051 | * 2. The four config_*() functions for specific algorithm modes. |
| 1052 | *****************************************************************/ |
| 1053 | |
| 1054 | /***************************************************************** |
| 1055 | * Exegesis. |
| 1056 | * |
| 1057 | * The following functions are the Plan7 equivalent of choosing |
| 1058 | * different alignment styles (fully local, fully global, |
| 1059 | * global/local, multihit, etc.) |
| 1060 | * |
| 1061 | * When you come into a configuration routine, the following |
| 1062 | * probabilities are valid in the model: |
| 1063 | * 1. t[1..M-1][0..6]: all the state transitions. |
| 1064 | * (Node M is special: it has only a match and a delete state, |
| 1065 | * no insert state, and M_M->E = 1.0 and D_M->E = 1.0 by def'n.) |
| 1066 | * 2. mat[1..M][]: all the match emissions. |
| 1067 | * 3. ins[1..M-1][]: all the insert emissions. Note that there is |
| 1068 | * no insert state in node M. |
| 1069 | * 4. tbd1: the B->D1 probability. The B->M1 probability is 1-tbd1. |
| 1070 | * These are the "data-dependent" probabilities in the model. |
| 1071 | * |
| 1072 | * The configuration routine gets to set the "algorithm-dependent" |
| 1073 | * probabilities: |
| 1074 | * 1. xt[XTN][MOVE,LOOP] dist controls unaligned N-terminal seq. |
| 1075 | * The higher xt[XTN][LOOP] is, the more unaligned seq we allow. |
| 1076 | * Similarly, xt[XTC][MOVE,LOOP] dist controls unaligned C-terminal |
| 1077 | * seq, and xt[XTJ][MOVE,LOOP] dist controls length of unaligned sequence |
| 1078 | * between multiple copies of a domain. Normally, if these are nonzero, |
| 1079 | * they are all set to be equal to hmm->p1, the loop probability |
| 1080 | * for the null hypothesis (see below). |
| 1081 | * 2. xt[XTE][MOVE,LOOP] distribution controls multihits. |
| 1082 | * Setting xt[XTE][LOOP] to 0.0 forces one hit per model. |
| 1083 | * 3. begin[1..M] controls entry probabilities. An algorithm |
| 1084 | * mode either imposes internal begin probabilities, or leaves begin[1] |
| 1085 | * as 1.0 and begin[k] = 0.0 for k>1. |
| 1086 | * 4. end[1..M] controls exit probabilities. An algorithm mode either |
| 1087 | * imposes internal exit probabilities, or leaves end[M] = 1.0 |
| 1088 | * and end[k] = 0.0 for k<M. |
| 1089 | * |
| 1090 | * The configuration routine then calls routines as appropriate to set |
| 1091 | * up all the model's scores, given these configured probabilities. When |
| 1092 | * the config routine returns, all scores are ready for alignment: |
| 1093 | * bsc, esc, tsc, msc, isc, and xsc. |
| 1094 | * |
| 1095 | ***************************************************************** |
| 1096 | * |
| 1097 | * SRE: REVISIT THE ISSUE BELOW. THE CONDITIONS ARE NO LONGER MET! |
| 1098 | * |
| 1099 | * There is (at least) one more issue worth noting. |
| 1100 | * If you want per-domain scores to sum up to per-sequence scores, which is |
| 1101 | * generally desirable if you don't want "bug" reports from vigilant users, |
| 1102 | * then one of the following two sets of conditions must be met: |
| 1103 | * |
| 1104 | * 1) t(E->J) = 0 |
| 1105 | * e.g. no multidomain hits |
| 1106 | * |
| 1107 | * 2) t(N->N) = t(C->C) = t(J->J) = hmm->p1 |
| 1108 | * e.g. unmatching sequence scores zero, and |
| 1109 | * N->B first-model score is equal to J->B another-model score. |
| 1110 | * |
| 1111 | * These constraints are obeyed in the default Config() functions below, |
| 1112 | * but in the future (say, when HMM editing may be allowed) we'll have |
| 1113 | * to remember this. Non-equality of the summed domain scores and |
| 1114 | * the total sequence score is a really easy "red flag" for people to |
| 1115 | * notice and report as a bug, even if it may make probabilistic |
| 1116 | * sense not to meet either constraint for certain modeling problems. |
| 1117 | ***************************************************************** |
| 1118 | */ |
| 1119 | |
| 1120 | |
| 1121 |