Debian Codegraph @

src/cm_p7_modelmaker.c

raw symbols SCIP
1 /* cm_p7_modelmaker.c
2 * EPN, Tue Aug 5 15:32:34 2008
3 *
4 * Construct a p7 model from CM and its CP9 HMM.
5 */
6 #include <esl_config.h>
7 #include <p7_config.h>
8 #include "config.h"
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <ctype.h>
14
15 #include "easel.h"
16 #include "esl_exponential.h"
17 #include "esl_msa.h"
18 #include "esl_gumbel.h"
19 #include "esl_random.h"
20 #include "esl_randomseq.h"
21 #include "esl_stack.h"
22 #include "esl_vectorops.h"
23 #include "esl_wuss.h"
24
25 #include "hmmer.h"
26
27 #include "infernal.h"
28
29 /* Function: BuildP7HMM_MatchEmitsOnly()
30 * Incept: EPN, Tue Aug 5 15:33:00 2008
31 *
32 * Purpose: Create and fill a P7_HMM object from a CM and its CP9 HMM.
33 * Copy only the match emissions of the CP9 HMM, the rest of
34 * the p7 model parameters are irrelevant.
35 *
36 * Args: cm - the cm
37 * cp9 - the cp9 HMM to build the p7 HMM from (usually cm->cp9loc)
38 * ret_p7 - RETURN: new p7 model
39 *
40 * Return: eslOK on success
41 *
42 * Throws: eslEINCOMPAT on contract violation
43 * eslEMEM on memory error
44 */
45 int
46 BuildP7HMM_MatchEmitsOnly(CM_t *cm, CP9_t *cp9, P7_HMM **ret_p7)
47 {
48 int status;
49 P7_HMM *hmm = NULL; /* RETURN: new hmm */
50 int k;
51
52 if(cp9 == NULL) return eslEINCOMPAT;
53 if(cp9->M != cm->clen) return eslEINCOMPAT;
54
55 if ((hmm = p7_hmm_Create(cm->clen, cm->abc)) == NULL) return eslEMEM;
56 if ((status = p7_hmm_Zero(hmm)) != eslOK) return status;
57
58 /* copy only match emissions */
59 for (k = 1; k <= cm->clen; k++) esl_vec_FCopy(cp9->mat[k], cm->abc->K, hmm->mat[k]);
60
61 /* parameterize, hacked from hmmer/p7_prior.c::p7_ParameterEstimation() */
62 /* match transitions */
63 for (k = 1; k <= hmm->M; k++) esl_vec_FNorm(hmm->t[k], 3);
64
65 /* insert transitions */
66 for (k = 1; k <= hmm->M; k++) esl_vec_FNorm(hmm->t[k]+3, 2);
67
68 /* delete transitions */
69 for (k = 1; k < hmm->M; k++) esl_vec_FNorm(hmm->t[k]+5, 2);
70 /* For k=0, which is unused; convention sets TMM=1.0, TMD=0.0
71 * For k=M, TMM = 1.0 (to the E state) and TMD=0.0 (no next D; must go to E).
72 */
73 hmm->t[0][p7H_DM] = hmm->t[hmm->M][p7H_DM] = 1.0;
74 hmm->t[0][p7H_DD] = hmm->t[hmm->M][p7H_DD] = 0.0;
75
76 /* insert emissions */
77 for (k = 0; k <= hmm->M; k++) esl_vec_FNorm(hmm->ins[k], hmm->abc->K); /* normalize inserts (0.25 each) */
78
79 p7_hmm_SetName(hmm, cm->name);
80 p7_hmm_SetAccession(hmm, cm->acc);
81 p7_hmm_SetDescription(hmm, cm->desc);
82 p7_hmm_SetCtime(hmm);
83 if((status = p7_hmm_SetConsensus(hmm, NULL)) != eslOK) goto ERROR;
84 if(cm->comlog != NULL) {
85 if((status = esl_strdup(cm->comlog, -1, &(hmm->comlog))) != eslOK) goto ERROR;
86 }
87 else {
88 hmm->comlog = NULL;
89 }
90
91 hmm->eff_nseq = cm->eff_nseq;
92 hmm->nseq = cm->nseq;
93 hmm->checksum = 0;
94
95 *ret_p7 = hmm;
96
97 return eslOK;
98
99 ERROR:
100 if(hmm != NULL) p7_hmm_Destroy(hmm);
101 return status;
102 }
103
104 /* Function: cm_cp9_to_p7()
105 * Incept: EPN, Fri Sep 24 13:46:37 2010
106 *
107 * Purpose: Create and fill a P7_HMM object from a CM and a CP9 HMM.
108 *
109 * Args: cm - the cm, must have a cp9 model in it.
110 * cp9 - the CP9 HMM, usually cm->cp9loc
111 * errbuf - for error messages
112 *
113 * Return: eslOK on success
114 * Throws: eslEINCOMPAT on contract violation, errbuf filled.
115 * eslEMEM on memory error, errbuf filled.
116 */
117 int
118 cm_cp9_to_p7(CM_t *cm, CP9_t *cp9, char *errbuf)
119 {
120 int status;
121 int k;
122
123 /* contract check */
124 if(cp9 == NULL) ESL_XFAIL(eslEINCOMPAT, errbuf, "trying to create a p7 from cp9 HMM, but cp9 is NULL");
125 if(cm->mlp7 != NULL) ESL_XFAIL(eslEINCOMPAT, errbuf, "trying to create ml p7, but it already exists");
126 if(cm->W == 0) ESL_XFAIL(eslEINCOMPAT, errbuf, "trying to create ml p7, cm->W is 0");
127 if(cp9->M != cm->clen) ESL_XFAIL(eslEINCOMPAT, errbuf, "trying to create ml p7, cm->clen != cp9->M");
128 if(cm->cmcons == NULL) ESL_XFAIL(eslEINCOMPAT, errbuf, "trying to create ml p7, cm->cmcons is NULL, we need it's structure string");
129
130 if ((cm->mlp7 = p7_hmm_Create(cm->clen, cm->abc)) == NULL) ESL_XFAIL(eslEMEM, errbuf, "out of memory");
131 p7_hmm_Zero(cm->mlp7);
132
133 /* copy transitions */
134 for (k = 0; k <= cm->mlp7->M; k++) {
135 cm->mlp7->t[k][p7H_MM] = cp9->t[k][CTMM];
136 cm->mlp7->t[k][p7H_MI] = cp9->t[k][CTMI];
137 cm->mlp7->t[k][p7H_MD] = cp9->t[k][CTMD];
138 cm->mlp7->t[k][p7H_IM] = cp9->t[k][CTIM];
139 cm->mlp7->t[k][p7H_II] = cp9->t[k][CTII];
140 cm->mlp7->t[k][p7H_DM] = cp9->t[k][CTDM];
141 cm->mlp7->t[k][p7H_DD] = cp9->t[k][CTDD];
142 /* note: the cp9 CTDI and CTID transitions do not exist the p7 model */
143 }
144 /* normalize match transitions */
145 for (k = 1; k <= cm->mlp7->M; k++) esl_vec_FNorm(cm->mlp7->t[k], 3);
146 /* normalize insert transitions */
147 for (k = 0; k < cm->mlp7->M; k++) esl_vec_FNorm(cm->mlp7->t[k]+3, 2);
148 /* normalize delete transitions */
149 for (k = 1; k < cm->mlp7->M; k++) esl_vec_FNorm(cm->mlp7->t[k]+5, 2);
150
151 /* enforce HMMER conventions */
152 cm->mlp7->t[cm->mlp7->M][p7H_MD] = 0.0;
153 esl_vec_FNorm(cm->mlp7->t[cm->mlp7->M], 3);
154 cm->mlp7->t[0][p7H_DM] = cm->mlp7->t[cm->mlp7->M][p7H_DM] = 1.0;
155 cm->mlp7->t[0][p7H_DD] = cm->mlp7->t[cm->mlp7->M][p7H_DD] = 0.0;
156
157 /* enforce INFERNAL CP9 convention, the 0'th node's MM transition is really begin[0] */
158 cm->mlp7->t[0][p7H_MM] = cp9->begin[1];
159 esl_vec_FNorm(cm->mlp7->t[0], 3);
160
161 /* match emissions: copy, then normalize (should be unnec actually) */
162 for (k = 1; k <= cm->clen; k++) esl_vec_FCopy(cp9->mat[k], cm->abc->K, cm->mlp7->mat[k]);
163 for (k = 1; k <= cm->clen; k++) esl_vec_FNorm(cm->mlp7->mat[k], cm->abc->K);
164 /* special case */
165 esl_vec_FSet(cm->mlp7->mat[0], cm->mlp7->abc->K, 0.);
166 cm->mlp7->mat[0][0] = 1.0;
167
168 /* insert emissions: copy, then normalize (should be unnec actually) */
169 for (k = 0; k <= cm->clen; k++) esl_vec_FCopy(cp9->ins[k], cm->abc->K, cm->mlp7->ins[k]);
170 for (k = 0; k <= cm->clen; k++) esl_vec_FNorm(cm->mlp7->ins[k], cm->abc->K);
171
172 /* copy cm->W as max_length */
173 cm->mlp7->max_length = cm->W;
174
175 p7_hmm_SetName (cm->mlp7, cm->name);
176 p7_hmm_SetAccession (cm->mlp7, cm->acc);
177 p7_hmm_SetDescription(cm->mlp7, cm->desc);
178 p7_hmm_SetCtime (cm->mlp7);
179 if((status = p7_hmm_SetConsensus(cm->mlp7, NULL)) != eslOK) ESL_XFAIL(status, errbuf, "out of memory");
180 if(cm->comlog != NULL) {
181 if((status = esl_strdup(cm->comlog, -1, &(cm->mlp7->comlog))) != eslOK) goto ERROR;
182 }
183 else {
184 cm->mlp7->comlog = NULL;
185 }
186
187 /* copy CM's RF annotation to mlp7 */
188 if(cm->flags & CMH_RF && cm->rf != NULL) {
189 ESL_ALLOC(cm->mlp7->rf, sizeof(char) * (cm->clen+2));
190 strcpy(cm->mlp7->rf, cm->rf);
191 cm->mlp7->flags |= p7H_RF;
192 }
193
194 /* copy CM's consensus structure annotation to mlp7 */
195 if(cm->cmcons != NULL) {
196 ESL_ALLOC(cm->mlp7->cs, sizeof(char) * (cm->clen+2));
197 cm->mlp7->cs[0] = ' ';
198 for(k = 1; k <= cm->clen; k++) {
199 cm->mlp7->cs[k] = cm->cmcons->cstr[k-1]; /* cmcons->cstr is 0..cm->clen-1, mlp7->cs is 1..cm->clen */
200 }
201 cm->mlp7->cs[cm->clen+1] = '\0';
202 cm->mlp7->flags |= p7H_CS;
203 }
204
205 cm->mlp7->eff_nseq = cm->eff_nseq;
206 cm->mlp7->nseq = cm->nseq;
207 if(cm->flags & CMH_CHKSUM) {
208 cm->mlp7->checksum = cm->checksum;
209 cm->mlp7->flags |= p7H_CHKSUM;
210 }
211 else {
212 cm->mlp7->checksum = 0;
213 }
214
215 /* set the model composition */
216 if ((status = p7_hmm_SetComposition(cm->mlp7)) != eslOK) ESL_XFAIL(status, errbuf, "out of memory");
217
218 cm->flags |= CMH_MLP7; /* raise the P7 flag */
219
220 return eslOK;
221
222 ERROR:
223 if(cm->mlp7 != NULL) { p7_hmm_Destroy(cm->mlp7); cm->mlp7 = NULL; }
224 return status;
225 }
226
227 /* Function: cm_p7_Calibrate()
228 * Incept: EPN, Tue Nov 9 06:16:57 2010
229 *
230 * Purpose: Calibrate a p7 HMM for local MSV, Viterbi, Forward and
231 * also glocal Forward.
232 *
233 * Args: hmm - the hmm
234 * errbuf - for error messages
235 * ElmL - length of sequences to sample for local MSV
236 * ElvL - length of sequences to sample for local Vit
237 * ElfL - length of sequences to sample for local Fwd
238 * EgfL - length of sequences to sample for glocal Fwd
239 * ElmN - number of sequences to sample for local MSV
240 * ElvN - number of sequences to sample for local Vit
241 * ElfN - number of sequences to sample for local Fwd
242 * EgfN - number of sequences to sample for glocal Fwd
243 * ElfT - fraction of tail mass to fit for local Fwd (usually (HMMER3 is) 0.04)
244 * EgfT - fraction of tail mass to fit for glocal Fwd
245 * ret_gfmu - RETURN: mu for glocal forward
246 * ret_gflambda - RETURN: lambda for glocal forward
247 *
248 * Return: eslOK on success
249 *
250 * Throws: eslEINCOMPAT on contract violation
251 * eslEMEM on memory error
252 */
253 int
254 cm_p7_Calibrate(P7_HMM *hmm, char *errbuf,
255 int ElmL, int ElvL, int ElfL, int EgfL,
256 int ElmN, int ElvN, int ElfN, int EgfN,
257 double ElfT, double EgfT,
258 double *ret_gfmu, double *ret_gflambda)
259 {
260 int status;
261 P7_OPROFILE *om = NULL;
262 P7_BG *bg = NULL;
263 P7_PROFILE *gm = NULL;
264 ESL_RANDOMNESS *r = NULL;
265 double lmmu, lvmu, lftau, gfmu;
266 double lmlam, lvlam, lflam, gflambda, lambda;
267
268 /*printf("cm_p7_Calibrate:\n\tElmL: %d\n\tElvL: %d\n\tElfL: %d\n\tEgfL: %d\n\tElmN: %d\n\tElvN: %d\n\tElfN: %d\n\tEgfN: %d\n\tElfT: %f\n\tEgfT: %f\n\n", ElmL, ElvL, ElfL, EgfL, ElmN, ElvN, ElfN, EgfN, ElfT, EgfT, do_real, do_null3, do_fitlam, do_bias);*/
269
270 /* most of this code stolen from hmmer's evalues.c::p7_Calibrate() */
271 if ((r = esl_randomness_CreateFast(42)) == NULL) ESL_XFAIL(eslEMEM, errbuf, "cm_p7_Calibrate(): failed to create RNG");
272 if ((bg = p7_bg_Create(hmm->abc)) == NULL) ESL_XFAIL(eslEMEM, errbuf, "cm_p7_Calibrate(): failed to allocate background");
273 if ((gm = p7_profile_Create(hmm->M, hmm->abc)) == NULL) ESL_XFAIL(eslEMEM, errbuf, "cm_p7_Calibrate(): failed to allocate profile");
274 if ((status = p7_ProfileConfig(hmm, bg, gm, ElmL, p7_LOCAL)) != eslOK) ESL_XFAIL(status, errbuf, "cm_p7_Calibrate(): failed to configure profile");
275 if ((om = p7_oprofile_Create(hmm->M, hmm->abc)) == NULL) ESL_XFAIL(eslEMEM, errbuf, "cm_p7_Calibrate(): failed to create optimized profile");
276 if ((status = p7_oprofile_Convert(gm, om)) != eslOK) ESL_XFAIL(status, errbuf, "cm_p7_Calibrate(): failed to convert to optimized profile");
277
278 /* The calibration steps themselves */
279 lambda = lmlam = lvlam = lflam = 0.;
280 if ((status = p7_Lambda (hmm, bg, &lambda)) != eslOK) ESL_XFAIL(status, errbuf, "failed to determine lambda");
281 if ((status = p7_MSVMu (r, om, bg, ElmL, ElmN, lambda, &lmmu)) != eslOK) ESL_XFAIL(status, errbuf, "failed to determine msv mu");
282 if ((status = p7_ViterbiMu(r, om, bg, ElvL, ElvN, lambda, &lvmu)) != eslOK) ESL_XFAIL(status, errbuf, "failed to determine vit mu");
283 if ((status = p7_Tau (r, om, bg, ElfL, ElfN, lambda, ElfT, &lftau)) != eslOK) ESL_XFAIL(status, errbuf, "failed to determine fwd tau");
284
285 /* set the p7's evparam[] */
286 hmm->evparam[p7_MMU] = lmmu;
287 hmm->evparam[p7_MLAMBDA] = lambda;
288 hmm->evparam[p7_VMU] = lvmu;
289 hmm->evparam[p7_VLAMBDA] = lambda;
290 hmm->evparam[p7_FTAU] = lftau;
291 hmm->evparam[p7_FLAMBDA] = lambda;
292 hmm->flags |= p7H_STATS;
293
294 /* finally, determine Glocal Forward stats */
295 if ((status = p7_ProfileConfig(hmm, bg, gm, EgfL, p7_GLOCAL)) != eslOK) goto ERROR;
296 if ((status = cm_p7_Tau(r, errbuf, NULL, gm, bg, EgfL, EgfN, lambda, EgfT, &gfmu)) != eslOK) ESL_XFAIL(status, errbuf, "failed to determine fwd tau");
297 gflambda = lambda;
298
299 esl_randomness_Destroy(r);
300 p7_bg_Destroy(bg);
301 p7_oprofile_Destroy(om);
302 p7_profile_Destroy(gm);
303
304 if(ret_gfmu != NULL) *ret_gfmu = gfmu;
305 if(ret_gflambda != NULL) *ret_gflambda = gflambda;
306
307 return eslOK;
308
309 ERROR:
310 if(r != NULL) esl_randomness_Destroy(r);
311 if(bg != NULL) p7_bg_Destroy(bg);
312 if(om != NULL) p7_oprofile_Destroy(om);
313 if(gm != NULL) p7_profile_Destroy(gm);
314 if(ret_gfmu != NULL) *ret_gfmu = 0.;
315 if(ret_gflambda != NULL) *ret_gflambda = 0.;
316 return status;
317 }
318
319 /* Function: cm_p7_Tau()
320 * Synopsis: Determine Forward tau by brief simulation.
321 * Incept: SRE, Thu Aug 9 15:08:39 2007 [Janelia] (p7_Tau())
322 *
323 * Purpose: Identical to p7_Tau() except that it can handle
324 * either an optimized profile or a generic profile,
325 * the latter of which is used for glocal Forward.
326 * See hmmer/evalues.c::cm_p7_Tau for additional information.
327 *
328 * Args: r : source of randomness
329 * om : configured profile (optimized), if non-NULL, <gm> must be NULL
330 * gm : configured profile (generic), if non-NULL, <om> must be NULL
331 * bg : null model (for background residue frequencies)
332 * L : mean length model for seq emission from profile
333 * N : number of sequences to generate
334 * lambda : expected slope of the exponential tail (from p7_Lambda())
335 * tailp : tail mass from which we will extrapolate mu
336 * ret_tau : RETURN: estimate for the Forward tau (base of exponential tail)
337 *
338 * Returns: <eslOK> on success, and <*ret_fv> is the score difference
339 * in bits.
340 *
341 * Throws: <eslEMEM> on allocation error, and <*ret_fv> is 0.
342 */
343 int
344 cm_p7_Tau(ESL_RANDOMNESS *r, char *errbuf, P7_OPROFILE *om, P7_PROFILE *gm, P7_BG *bg, int L, int N, double lambda, double tailp, double *ret_tau)
345 {
346 P7_OMX *ox = NULL;
347 P7_GMX *gx = NULL;
348
349 ESL_DSQ *dsq = NULL;
350 double *xv = NULL;
351 float sc, fsc, nullsc;
352 double gmu, glam;
353 int status;
354 int i;
355 int do_generic;
356
357 if(om == NULL && gm == NULL) { status = eslEINVAL; goto ERROR; }
358 if(om != NULL && gm != NULL) { status = eslEINVAL; goto ERROR; }
359 do_generic = (gm != NULL) ? TRUE : FALSE;
360
361 if(do_generic) {
362 gx = p7_gmx_Create(gm->M, L); /* DP matrix: for ForwardParser, L rows */
363 if (gx == NULL) { status = eslEMEM; goto ERROR; }
364 }
365 else {
366 ox = p7_omx_Create(om->M, 0, L); /* DP matrix: for ForwardParser, L rows */
367 if (ox == NULL) { status = eslEMEM; goto ERROR; }
368 }
369
370 ESL_ALLOC(xv, sizeof(double) * N);
371 ESL_ALLOC(dsq, sizeof(ESL_DSQ) * (L+2));
372
373 if(do_generic) p7_ReconfigLength(gm, L);
374 else p7_oprofile_ReconfigLength(om, L);
375 p7_bg_SetLength(bg, L);
376
377 for (i = 0; i < N; i++)
378 {
379 if((status = esl_rsq_xfIID(r, bg->f, bg->abc->K, L, dsq)) != eslOK) goto ERROR;
380 if(do_generic) {
381 if ((status = p7_GForward(dsq, L, gm, gx, &fsc)) != eslOK) goto ERROR;
382 }
383 else {
384 if ((status = p7_ForwardParser(dsq, L, om, ox, &fsc)) != eslOK) goto ERROR;
385 }
386 if((status = p7_bg_NullOne(bg, dsq, L, &nullsc)) != eslOK) goto ERROR;
387 sc = (fsc - nullsc) / eslCONST_LOG2;
388 xv[i] = sc;
389 }
390
391 if ((status = esl_gumbel_FitComplete(xv, N, &gmu, &glam)) != eslOK) goto ERROR;
392 /* Explanation of the eqn below: first find the x at which the Gumbel tail
393 * mass is predicted to be equal to tailp. Then back up from that x
394 * by log(tailp)/lambda to set the origin of the exponential tail to 1.0
395 * instead of tailp.
396 */
397 *ret_tau = esl_gumbel_invcdf(1.0-tailp, gmu, glam) + (log(tailp) / lambda);
398
399 free(xv);
400 free(dsq);
401 if (ox != NULL) p7_omx_Destroy(ox);
402 if (gx != NULL) p7_gmx_Destroy(gx);
403 return eslOK;
404
405 ERROR:
406 *ret_tau = 0.;
407 if (xv != NULL) free(xv);
408 if (dsq != NULL) free(dsq);
409 if (ox != NULL) p7_omx_Destroy(ox);
410 if (gx != NULL) p7_gmx_Destroy(gx);
411 return status;
412 }
413
414 /* Function: cm_SetFilterHMM()
415 * Incept: EPN, Mon Dec 27 07:59:47 2010
416 *
417 * Purpose: Assign a p7 HMM as a CM's filter hmm (cm->fp7)
418 *
419 * Args: cm - the CM
420 * hmm - the HMM to add
421 * gfmu - glocal forward mu parameter
422 * gflambda - glocal forward lambda parameter
423 * errbuf - for error messages
424 *
425 * Return: eslOK on success
426 *
427 * Throws: eslEINCOMPAT on contract violation
428 * eslEMEM on memory error
429 */
430 int
431 cm_SetFilterHMM(CM_t *cm, P7_HMM *hmm, double gfmu, double gflambda)
432 {
433 if(cm->fp7 != NULL) {
434 p7_hmm_Destroy(cm->fp7);
435 }
436 cm->fp7 = hmm;
437
438 if(hmm->flags & p7H_STATS) {
439 cm->fp7_evparam[CM_p7_LMMU] = hmm->evparam[p7_MMU];
440 cm->fp7_evparam[CM_p7_LMLAMBDA] = hmm->evparam[p7_MLAMBDA];
441 cm->fp7_evparam[CM_p7_LVMU] = hmm->evparam[p7_VMU];
442 cm->fp7_evparam[CM_p7_LVLAMBDA] = hmm->evparam[p7_VLAMBDA];
443 cm->fp7_evparam[CM_p7_LFTAU] = hmm->evparam[p7_FTAU];
444 cm->fp7_evparam[CM_p7_LFLAMBDA] = hmm->evparam[p7_FLAMBDA];
445 cm->fp7_evparam[CM_p7_GFMU] = gfmu;
446 cm->fp7_evparam[CM_p7_GFLAMBDA] = gflambda;
447 }
448 else { /* this should never happen */
449 cm->fp7_evparam[CM_p7_LMMU] = 0.;
450 cm->fp7_evparam[CM_p7_LMLAMBDA] = 0.;
451 cm->fp7_evparam[CM_p7_LVMU] = 0.;
452 cm->fp7_evparam[CM_p7_LVLAMBDA] = 0.;
453 cm->fp7_evparam[CM_p7_LFTAU] = 0.;
454 cm->fp7_evparam[CM_p7_LFLAMBDA] = 0.;
455 cm->fp7_evparam[CM_p7_GFMU] = 0.;
456 cm->fp7_evparam[CM_p7_GFLAMBDA] = 0.;
457 }
458 cm->flags |= CMH_FP7; /* raise the FP7 flag */
459
460 return eslOK;
461 }
462
463 /* Function: dump_p7()
464 * Incept: EPN, Fri Sep 24 14:22:49 2010
465 *
466 * Purpose: Dump parameters of a p7 HMM to a file.
467 *
468 * Args: hmm - the p7 HMM
469 * fp - the file to print to
470 *
471 * Return: eslOK on success
472 *
473 * Throws: eslEINCOMPAT on contract violation
474 * eslEMEM on memory error
475 */
476 int
477 dump_p7(P7_HMM *hmm, FILE *fp)
478 {
479 return p7_hmmfile_WriteASCII(fp, -1, hmm);
480 }
481
482
483 /* Function: cm_p7_hmm_Sizeof()
484 * Incept: EPN, Wed Jan 18 10:10:10 2012
485 *
486 * Purpose: Calculate and return size of a P7_HMM
487 * in Mb.
488 *
489 * Args: hmm - the p7 HMM
490 *
491 * Return: size of hmm in Mb
492 */
493 float
494 cm_p7_hmm_Sizeof(P7_HMM *hmm)
495 {
496 float bytes = 0.;
497
498 bytes = sizeof(P7_HMM);
499
500 if(hmm->M > 0 && hmm->abc != NULL)
501 /* following from p7_hmm_CreateBody() */
502 bytes += sizeof(float *) * (hmm->M+1); /* t */
503 bytes += sizeof(float *) * (hmm->M+1); /* mat */
504 bytes += sizeof(float *) * (hmm->M+1); /* ins */
505
506 bytes += sizeof(float) * p7H_NTRANSITIONS*(hmm->M+1); /* t */
507 bytes += sizeof(float *) * hmm->abc->K * (hmm->M+1); /* mat */
508 bytes += sizeof(float *) * hmm->abc->K * (hmm->M+1); /* ins */
509
510 if(hmm->rf != NULL) bytes += sizeof(char) * (hmm->M+2);
511 if(hmm->consensus != NULL) bytes += sizeof(char) * (hmm->M+2);
512 if(hmm->cs != NULL) bytes += sizeof(char) * (hmm->M+2);
513 if(hmm->ca != NULL) bytes += sizeof(char) * (hmm->M+2);
514 if(hmm->map != NULL) bytes += sizeof(int) * (hmm->M+1);
515
516 return bytes / 1000000.;
517 }
518
519 /* Function: cm_p7_hmm_SetConsensus()
520 * Incept: EPN, Wed May 9 14:13:37 2012
521 * Synopsis: Set the consensus residue line of the HMM.
522 *
523 * Purpose: Sets the consensus annotation line of the model <hmm>.
524 *
525 * Based on p7_hmm_SetConsensus() which is flexible to
526 * setting the consensus as a single sequence or a
527 * consensus from a multiple sequence alignment. Here,
528 * only the latter case is handled, i.e. but in the future,
529 * we should relax this to allow for single sequence
530 * models.
531 *
532 * This function only exists because p7_hmm_SetConsensus()
533 * uses a threshold probability of 0.9 for setting
534 * a consensus residue as uppercase, while we want
535 * to be able to use 0.5 since that's what we use with
536 * single stranded CM positions. (Actually we use 1.0
537 * bits, which equates to a 0.5 probability for a default
538 * null1 model (so using 0.5 will be wrong for non-standard
539 * null models...)).
540 *
541 * The most likely (highest emission probability) residue
542 * is the consensus at each position. If the emission
543 * probability is $\geq$ certain threshold (0.5), the
544 * residue is upper cased.
545 *
546 * Args: hmm - model with valid probability parameters mat[1..M][x]
547 *
548 * Returns: <eslOK> on success. The <p7H_CONS> flag on the <hmm> is raised
549 * if it wasn't already. The <hmm->consensus> line is set.
550 *
551 * Throws: <eslEMEM> on allocation error. The <p7H_CONS> is dropped, even
552 * if it was up to begin with, and the <hmm->consensus> is <NULL>,
553 * even if we had one to begin with.
554 *
555 */
556 int
557 cm_p7_hmm_SetConsensus(P7_HMM *hmm)
558 {
559 int k, x;
560 float mthresh = 0.5;
561 int status;
562
563 /* allocation, if needed */
564 if (! hmm->consensus) ESL_ALLOC(hmm->consensus, sizeof(char) * (hmm->M+2));
565
566 /* set our arbitrary threshold for upper/lower casing */
567
568 hmm->consensus[0] = ' ';
569 for (k = 1; k <= hmm->M; k++)
570 {
571 x = esl_vec_FArgMax(hmm->mat[k], hmm->abc->K);
572 hmm->consensus[k] = ((hmm->mat[k][x] >= mthresh) ? toupper(hmm->abc->sym[x]) : tolower(hmm->abc->sym[x]));
573 }
574 hmm->consensus[hmm->M+1] = '\0';
575 hmm->flags |= p7H_CONS;
576 return eslOK;
577
578 ERROR:
579 if (hmm->consensus) free(hmm->consensus);
580 hmm->consensus = NULL;
581 hmm->flags &= (~p7H_CONS);
582 return status;
583 }