Debian Codegraph @

src/stats.c

raw symbols SCIP
1 /*
2 * EPN, Wed Nov 22 12:00:20 2006
3 * Ported from RSEARCH-1.1
4 *
5 * stats.c
6 *
7 * Routines for calculating statistics in Infernal.
8 *
9 * Robert J. Klein
10 * Separate file started May 17, 2002
11 */
12 #include <esl_config.h>
13 #include <p7_config.h>
14 #include "config.h"
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <float.h>
19 #include <ctype.h>
20 #include <string.h>
21
22 #include "easel.h"
23 #include "esl_exponential.h"
24 #include "esl_histogram.h"
25 #include "esl_random.h"
26 #include "esl_sq.h"
27 #include "esl_sqio.h"
28 #include "esl_ssi.h"
29 #include "esl_vectorops.h"
30
31 #include "hmmer.h"
32
33 #include "infernal.h"
34
35
36 int debug_print_expinfo_array(CM_t *cm, char *errbuf, ExpInfo_t **expA)
37 {
38 int status;
39 char *namedashes;
40 int ni;
41 int namewidth = strlen(cm->name);
42 ESL_ALLOC(namedashes, sizeof(char) * namewidth+1);
43 namedashes[namewidth] = '\0';
44 for(ni = 0; ni < namewidth; ni++) namedashes[ni] = '-';
45
46 if(expA != NULL) {
47 printf("cm_lc exp tail:\t");
48 debug_print_expinfo(expA[EXP_CM_LC]);
49 printf("cm_gc exp tail:\t");
50 debug_print_expinfo(expA[EXP_CM_GC]);
51 printf("cm_li exp tail:\t");
52 debug_print_expinfo(expA[EXP_CM_LI]);
53 printf("cm_gi exp tail:\t");
54 debug_print_expinfo(expA[EXP_CM_GI]);
55 printf("\n\n");
56 }
57 free(namedashes);
58 return eslOK;
59
60 ERROR:
61 ESL_FAIL(status, errbuf, "Memory allocation error in debug_print_expinfo_array().");
62 return status; /* NEVERREACHED */
63 }
64
65 /* Function: debug_print_expinfo
66 */
67 int debug_print_expinfo(ExpInfo_t *exp)
68 {
69 if(exp->is_valid) printf("cur_eff_dbsize: %f lambda: %f mu_extrap: %f mu_orig: %f dbsize: %.0f nrandhits: %d tailp: %f (valid)\n", exp->cur_eff_dbsize, exp->lambda, exp->mu_extrap, exp->mu_orig, exp->dbsize, exp->nrandhits, exp->tailp);
70 else printf("invalid (not yet set)\n");
71 fflush(stdout);
72 return eslOK;
73 }
74
75 /* Function: get_gc_comp
76 * Date: EPN, Tue Aug 7 08:51:06 2007
77 * Purpose: Given a sequence and start and stop coordinates, returns
78 * integerized GC composition of the region. This Easelfied
79 * version replaces RSEARCH's version.
80 */
81 int get_gc_comp(const ESL_ALPHABET *abc, ESL_DSQ *dsq, int start, int stop)
82 {
83 int status;
84 int i;
85 float *ct = NULL;
86 float gc;
87
88 /* contract check */
89 if(abc == NULL) cm_Fail("get_gc_comp alphabet is NULL.");
90 if(dsq == NULL) cm_Fail("get_gc_comp dsq is NULL.");
91 if(abc->type != eslRNA && abc->type != eslDNA) cm_Fail("get_gc_comp expects alphabet of RNA or DNA");
92
93 if (start > stop) {
94 i = start;
95 start = stop;
96 stop = i;
97 }
98 ESL_ALLOC(ct, sizeof(float) * abc->K);
99 esl_vec_FSet(ct, abc->K, 0.);
100 for (i = start; i <= stop; i++)
101 {
102 if(esl_abc_XIsGap(abc, dsq[i])) cm_Fail("in get_gc_comp, res %d is a gap!\n", i);
103 esl_abc_FCount(abc, ct, dsq[i], 1.);
104 }
105 gc = ct[abc->inmap[(int) 'G']] + ct[abc->inmap[(int) 'C']];
106 gc /= ((float) (stop-start+1));
107 gc *= 100.;
108 free(ct);
109 return (int) (gc + 0.5);
110
111 ERROR:
112 cm_Fail("Memory allocation error.");
113 return 0; /* never reached */
114 }
115
116
117 /* Function: get_alphabet_comp
118 * Date: EPN, Wed May 7 18:39:28 2008
119 * Purpose: Given a sequence and start and stop coordinates, allocate and fill
120 * a abc->K sized vector with frequency of each residue.
121 */
122 int get_alphabet_comp(const ESL_ALPHABET *abc, ESL_DSQ *dsq, int start, int stop, float **ret_freq)
123 {
124 int status;
125 int i;
126 float *freq;
127
128 ESL_ALLOC(freq, sizeof(float) * abc->K);
129 esl_vec_FSet(freq, abc->K, 0.0);
130
131 /* contract check */
132 if(abc == NULL) cm_Fail("get_alphabet_comp alphabet is NULL.");
133 if(dsq == NULL) cm_Fail("get_alphabet_comp dsq is NULL.");
134 if(abc->type != eslRNA && abc->type != eslDNA) cm_Fail("get_alphabet_comp expects alphabet of RNA or DNA");
135 if(ret_freq == NULL) cm_Fail("get_alphabet_comp ret_freq is NULL.");
136
137 if (start > stop) {
138 i = start;
139 start = stop;
140 stop = i;
141 }
142 for (i = start; i <= stop; i++)
143 {
144 if(esl_abc_XIsGap(abc, dsq[i])) cm_Fail("in get_alphabet_comp, res %d is a gap!\n", i);
145 esl_abc_FCount(abc, freq, dsq[i], 1.);
146 }
147 esl_vec_FNorm(freq, abc->K);
148 *ret_freq = freq;
149 return eslOK;
150
151 ERROR:
152 cm_Fail("get_alphabet_comp() memory allocation error.");
153 return 0; /* never reached */
154 }
155
156 /* Function: GetDBSize()
157 *
158 * Date: EPN, Wed Apr 2 16:33:19 2008
159 *
160 * Purpose: Given a sequence file ptr, determine the number, and summed
161 * size of the seqs in the file.
162 *
163 * Args: sqfp - open sequence file
164 * errbuf - for writing error messages
165 * ret_N - RETURN: total length (residues) or all seqs in sqfp
166 * ret_nseq - RETURN: number of seqs in sqfp
167 * ret_namewidth - RETURN: max length of name in sqfp
168 *
169 * Returns: eslOK on success, other status on failure, errbuf filled with error message.
170 */
171 int GetDBSize (ESL_SQFILE *sqfp, char *errbuf, long *ret_N, int *ret_nseq, int *ret_namewidth)
172 {
173 int status;
174 ESL_SQ *sq;
175 long N = 0;
176 int namewidth = 11; /* length of "target name" */
177 int nseq = 0;
178
179 sq = esl_sq_Create();
180
181 while ((status = esl_sqio_ReadInfo(sqfp, sq)) == eslOK) {
182 N += sq->L;
183 namewidth = ESL_MAX(namewidth, strlen(sq->name));
184 esl_sq_Reuse(sq);
185 nseq++;
186 }
187 if (status != eslEOF) {
188 ESL_FAIL(status, errbuf, "Parse failed, file %s:\n%s",
189 sqfp->filename, esl_sqfile_GetErrorBuf(sqfp));
190 }
191 esl_sq_Destroy(sq);
192 esl_sqfile_Position(sqfp, (off_t) 0); /* rewind sequence file to beginning */
193
194 if(ret_N != NULL) *ret_N = N;
195 if(ret_nseq != NULL) *ret_nseq = nseq;
196 if(ret_namewidth != NULL) *ret_namewidth = namewidth;
197 return eslOK;
198 }
199
200
201 /* Function: GetDBInfo()
202 *
203 * Date: Easelification: EPN, Thu Dec 7 06:07:58 2006
204 * (initial - RSEARCH::get_dbinfo()) RJK, Thu Apr 11, 2002 [St. Louis]
205 *
206 * Purpose: Given a sequence file name, determine the total size of the
207 * seqs in the file (DB) and GC content information.
208 *
209 * Args: abc - alphabet for sq file
210 * sqfp - open sequence file
211 * errbuf - for error messages
212 * ret_N - RETURN: total length (residues) or all seqs in seqfile
213 * ret_nseq - RETURN: number of seqs in seqfile
214 * ret_gc_ct - RETURN: gc_ct[x] observed 100-nt segments with GC% of x [0..100]
215 *
216 * Returns: eslOK on success, other status on failure, errbuf filled with error message.
217 */
218 int GetDBInfo (const ESL_ALPHABET *abc, ESL_SQFILE *sqfp, char *errbuf, long *ret_N, int *ret_nseq, double **ret_gc_ct)
219 {
220 int status;
221 ESL_SQ *sq;
222 int i, j, jp;
223 long N = 0;
224 double *gc_ct;
225 int gc;
226 int nseq = 0;
227 int all_ambig_flag; /* used to check if curr DB chunk is all ambiguous characters
228 * usually Ns, if it is, we don't count it towards the GC content info */
229 if (abc == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "GetDBInfo(), abc is NULL\n");
230 if (ret_gc_ct == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "GetDBInfo(), ret_gc_ct is NULL\n");
231
232 ESL_ALLOC(gc_ct, sizeof(double) * GC_SEGMENTS);
233 for (i=0; i<GC_SEGMENTS; i++) gc_ct[i] = 0.;
234
235 sq = esl_sq_CreateDigital(abc);
236 esl_sqfile_SetDigital(sqfp, abc);
237
238 while ((status = esl_sqio_Read(sqfp, sq)) == eslOK) {
239 nseq++;
240 N += sq->n;
241 /*printf("new N: %d\n", N);*/
242 for(i = 1; i <= sq->n; i += 100) {
243 j = (i+99 <= sq->n) ? i+99 : sq->n;
244 gc = get_gc_comp(abc, sq->dsq, i, j);
245 all_ambig_flag = TRUE;
246 for(jp = 0; jp < 100 && (jp+i) < sq->n; jp++) {
247 if(sq->dsq[i+jp] < abc->K) {
248 all_ambig_flag = FALSE;
249 break;
250 }
251 }
252 /*printf("N: %d i: %d gc: %d\n", N, i, gc);*/
253 /* don't count GC content of chunks < 20 nt, very hacky;
254 * don't count GC content of chunks that are all N, this
255 * will be common in RepeatMasked genomes where poly-Ns could
256 * skew the base composition stats of the genome */
257 if(j > 20 && !all_ambig_flag)
258 {
259 /*printf("j: %d i: %d adding 1 to gc_ct[%d]\n", j, i, ((int) gc));*/
260 gc_ct[(int) gc] += 1.;
261 }
262 }
263 esl_sq_Reuse(sq);
264 }
265 if (status != eslEOF)
266 ESL_FAIL(status, errbuf, "Parse failed, file %s:\n%s",
267 sqfp->filename, esl_sqfile_GetErrorBuf(sqfp));
268 esl_sq_Destroy(sq);
269 esl_sqfile_Position(sqfp, (off_t) 0);
270
271 if(ret_N != NULL) *ret_N = N;
272 if(ret_nseq != NULL) *ret_nseq = nseq;
273 *ret_gc_ct = gc_ct;
274
275 return eslOK;
276
277 ERROR:
278 ESL_FAIL(status, errbuf, "GetDBInfo(): memory allocation error.");
279 }
280
281 /* Function: E2ScoreGivenExpInfo()
282 * Date: EPN, Thu Apr 3 15:57:34 2008
283 *
284 * Purpose: Given an E-value <E> and a exp tail stat structure
285 * determine the bit score that will give an E-value
286 * of <E>.
287 *
288 * Returns: eslOK on success, <ret_sc> filled with bit score
289 * error code on failure, errbuf filled with message
290 */
291 int E2ScoreGivenExpInfo(ExpInfo_t *exp, char *errbuf, double E, float *ret_sc)
292 {
293 float sc;
294 /* contract checks */
295 if(ret_sc == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "E2ScoreGivenExpInfo, ret_sc is NULL");
296 sc = exp->mu_extrap + ((log(E/exp->cur_eff_dbsize)) / (-1 * exp->lambda));
297 *ret_sc = sc;
298 return eslOK;
299 }
300
301 /* Function: P2ScoreGivenExpInfo()
302 * Date: EPN, Sun Mar 18 10:58:17 2012
303 *
304 * Purpose: Given a P-value <P> <E> and a exp tail stat structure
305 * determine the bit score that will give a P-value
306 * of <P>.
307 *
308 * Returns: eslOK on success, <ret_sc> filled with bit score
309 * error code on failure, errbuf filled with message
310 */
311 int P2ScoreGivenExpInfo(ExpInfo_t *exp, char *errbuf, double P, float *ret_sc)
312 {
313 float sc;
314 /* contract checks */
315 if(ret_sc == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "P2ScoreGivenExpInfo, ret_sc is NULL");
316 sc = exp->mu_extrap + (log(P) / (-1 * exp->lambda));
317 *ret_sc = sc;
318 return eslOK;
319 }
320
321 /* Function: Score2E()
322 * Date: EPN, Fri Feb 15 12:40:21 2008
323 *
324 * Purpose: Given a bit score <x>, a mu and lambda that describe
325 * an exponential tail distribution, and an effective
326 * database size, return the E-value of <sc>.
327 *
328 * Returns: E value of <x>
329 */
330 double Score2E (float x, double mu, double lambda, double eff_dbsize)
331 {
332 return esl_exp_surv(x, mu, lambda) * eff_dbsize;
333 }
334
335 /* Function: cm_p7_E2Score()
336 * Date: EPN, Tue Apr 17 14:46:51 2012
337 *
338 * Purpose: Given an E-value <E>, a database size in residues <Z>, a
339 * fixed hit length <hitlen>, and a mu and lambda for a
340 * P7_HMM, determine the bit score that will give an E-value
341 * of <E>. This function computes the E-value the same way
342 * nhmmer does, as of svn r3990.
343 *
344 * Returns: bit score corresponding to E-value <E>.
345 */
346 float cm_p7_E2Score(double E, double Z, int hitlen, float mu, float lambda)
347 {
348 return mu + ((log(E/(Z / (float) hitlen))) / (-1 * lambda));
349 }
350
351 /* Function: cm_p7_P2Score()
352 * Date: EPN, Tue Apr 17 14:46:51 2012
353 *
354 * Purpose: Given a P value and a mu and lambda for a
355 * P7_HMM, determine the bit score that corresponds with
356 * that P value.
357 *
358 * Returns: bit score corresponding to P-value <P>.
359 */
360 float cm_p7_P2Score(double P, float mu, float lambda)
361 {
362 return mu + (log(P) / (-1 * lambda));
363 }
364
365 /* Function: ExpModeIsLocal()
366 * Date: EPN, Mon Dec 10 09:07:59 2007
367 * Purpose: Given a exp tail mode, return TRUE if it corresponds to
368 * a local model configuration.
369 *
370 * Args: exp_mode - the mode 0..EXP_NMODES-1
371 */
372 int
373 ExpModeIsLocal(int exp_mode)
374 {
375 ESL_DASSERT1((exp_mode >= 0 && exp_mode < EXP_NMODES));
376
377 switch (exp_mode) {
378 case EXP_CM_LC:
379 case EXP_CM_LI:
380 return TRUE;
381 break;
382 case EXP_CM_GC:
383 case EXP_CM_GI:
384 return FALSE;
385 break;
386 default:
387 cm_Fail("ExpModeIsLocal(): bogus exp_mode: %d\n", exp_mode);
388 }
389 return FALSE; /* never reached */
390 }
391
392 /* Function: ExpModeIsInside()
393 * Date: EPN, Mon Dec 10 09:11:55 2007
394 * Purpose: Given a exp tail mode, return TRUE if it corresponds to
395 * Inside, false if it corresponds to Inside.
396 *
397 * Args: exp_mode - the mode 0..EXP_NMODES-1
398 */
399 int
400 ExpModeIsInside(int exp_mode)
401 {
402 ESL_DASSERT1((exp_mode >= 0 && exp_mode < EXP_NMODES));
403
404 switch (exp_mode) {
405 case EXP_CM_LI:
406 case EXP_CM_GI:
407 return TRUE;
408 break;
409
410 case EXP_CM_LC:
411 case EXP_CM_GC:
412 return FALSE;
413 break;
414
415 default:
416 cm_Fail("ExpModeIsInside(): bogus exp_mode: %d\n", exp_mode);
417 }
418 return FALSE; /* never reached */
419 }
420
421 /* Function: CreateExpInfo()
422 * Date: EPN, Tue Dec 11 05:25:06 2007
423 *
424 * Purpose: Allocate and minimally initialize a exp tail info object.
425 *
426 * Returns: Newly allocated ExpInfo_t object on success, NULL if some error occurs
427 */
428 ExpInfo_t *
429 CreateExpInfo()
430 {
431 int status;
432
433 ExpInfo_t *exp = NULL;
434 ESL_ALLOC(exp, sizeof(ExpInfo_t));
435
436 exp->cur_eff_dbsize = 0.;
437 exp->lambda = 0.;
438 exp->mu_extrap = 0.;
439 exp->mu_orig = 0.;
440 exp->dbsize = 0;
441 exp->nrandhits = 0;
442 exp->tailp = 0.;
443
444 exp->is_valid = FALSE;
445 return exp;
446
447 ERROR:
448 return NULL; /* reached if memory error */
449 }
450
451
452 /* Function: SetExpInfo()
453 * Date: EPN, Wed Dec 12 13:43:36 2007
454 *
455 * Purpose: Set parameters of a exp tail info object and raise it's is_valid 'flag'.
456 *
457 * Returns: void
458 */
459 void
460 SetExpInfo(ExpInfo_t *exp, double lambda, double mu_orig, double dbsize, int nrandhits, double tailp)
461 {
462 exp->lambda = lambda;
463 exp->mu_orig = mu_orig;
464 exp->dbsize = dbsize;
465 exp->nrandhits = nrandhits;
466 exp->tailp = tailp;
467
468 exp->mu_extrap = exp->mu_orig - log(1./exp->tailp) / exp->lambda;
469
470 /* initialize exp->cur_eff_dbsize as effective database size as exp->nrandhits, this is
471 * effective database size if we searched a database of size <exp->dbsize>, which we
472 * just calibrated for. we'll update this in cmsearch for the target database size. */
473 exp->cur_eff_dbsize = (double) exp->nrandhits;
474
475 exp->is_valid = TRUE; /* we can now write Exp Info to a cm file */
476 return;
477 }
478
479 /* Function: DuplicateExpInfo()
480 * Date: EPN, Tue Dec 11 05:28:13 2007
481 *
482 * Purpose: Duplicate a exp tail info object.
483 *
484 * Returns: Newly allocated ExpInfo_t object on success, NULL if some error occurs
485 */
486 ExpInfo_t *
487 DuplicateExpInfo(ExpInfo_t *src)
488 {
489 int status;
490
491 ExpInfo_t *dest = NULL;
492 ESL_ALLOC(dest, sizeof(ExpInfo_t));
493
494 dest->cur_eff_dbsize = src->cur_eff_dbsize;
495 dest->lambda = src->lambda;
496 dest->mu_orig = src->mu_orig;
497 dest->mu_extrap = src->mu_extrap;
498 dest->dbsize = src->dbsize;
499 dest->nrandhits = src->nrandhits;
500 dest->tailp = src->tailp;
501 dest->is_valid = src->is_valid;
502 return dest;
503
504 ERROR:
505 return NULL; /* reached if memory error */
506 }
507
508
509 /* Function: DescribeExpMode()
510 * Incept: EPN, Mon Jan 7 18:04:31 2008
511 *
512 * Purpose: Returns the Exp Tail mode in text.
513 * For example, <DescribeExpMode(EXP_CM_GC)>
514 * returns "glocal CM CYK".
515 */
516 char *
517 DescribeExpMode(int exp_mode)
518 {
519 switch (exp_mode) {
520 case EXP_CM_GC: return " cm glc cyk";
521 case EXP_CM_GI: return " cm glc ins";
522 case EXP_CM_LC: return " cm loc cyk";
523 case EXP_CM_LI: return " cm loc ins";
524 default: return "?";
525 }
526 }
527
528 /* Function: UpdateExpsForDBSize()
529 * Date: EPN, Thu Jan 17 09:38:06 2008
530 *
531 * Purpose: Update the <cur_eff_dbsize> parameter of the
532 * ExpInfo_t objects in a CM's cm->expA object
533 * to reflect a database of size <dbsize>.
534 *
535 * Returns: eslOK on success, other Easel status code on contract
536 * violation with informative error message in errbuf.
537 */
538 int
539 UpdateExpsForDBSize(CM_t *cm, char *errbuf, double dbsize)
540 {
541 int i;
542 /* contract checks */
543 if(! (cm->flags & CMH_EXPTAIL_STATS)) ESL_FAIL(eslEINCOMPAT, errbuf, "UpdateExpsForDBSize(), cm does not have Exp stats\nYou may need to run cmcalibrate.");
544
545 for(i = 0; i < EXP_NMODES; i++) {
546 cm->expA[i]->cur_eff_dbsize = (dbsize / cm->expA[i]->dbsize) * ((double) cm->expA[i]->nrandhits);
547 }
548
549 return eslOK;
550 }
551
552
553
554 /* Function: CreateGenomicHMM()
555 * Date: EPN, Tue May 20 17:40:54 2008
556 *
557 * Purpose: Create the three arrays that make up the parameters of the
558 * fully connected 5 state HMM that emits 'realistic' genomic
559 * sequence for calculating E-value statistics.
560 *
561 * The HMM was trained by EM from 30 Mb of 100 Kb chunks of
562 * real genomes of hand selected GC contents (10 Mb each from
563 * Archaea, Bacteria, Eukarya genomes). See
564 * ~nawrockie/notebook/8_0326_inf_default_gc/ for more info.
565 *
566 * There were larger HMMs that 'performed' better, but this 5
567 * state guy was a good balance b/t performance and number of
568 * parameters. Performance was judged by how similar the
569 * generated sequence was to the training 30 Mb genomic
570 * sequence.
571 *
572 * abc - alphabet, must be eslRNA | eslDNA
573 * errbuf - for error messages
574 * ret_sA - RETURN: start probabilities [0..nstates-1]
575 * ret_tAA - RETURN: transition probabilities [0..nstates-1][0..nstates-1]
576 * ret_eAA - RETURN: emission probabilities [0..nstates-1][0..abc->K-1]
577 * ret_nstates - RETURN: number of states (5)
578 */
579 int
580 CreateGenomicHMM(const ESL_ALPHABET *abc, char *errbuf, double **ret_sA, double ***ret_tAA, double ***ret_eAA, int *ret_nstates)
581 {
582 int status;
583 int nstates = 5;
584 int i;
585
586 /* contract check, make sure we're in a valid mode */
587 if(abc->type != eslRNA && abc->type != eslDNA) ESL_FAIL(eslEINCOMPAT, errbuf, "get_genomic_sequence_from_hmm(), abc is not eslRNA nor eslDNA");
588
589 /* start probabilities */
590 double *sA;
591 ESL_ALLOC(sA, sizeof(double) * nstates);
592
593 sA[0] = 0.157377049180328;
594 sA[1] = 0.39344262295082;
595 sA[2] = 0.265573770491803;
596 sA[3] = 0.00327868852459016;
597 sA[4] = 0.180327868852459;
598 esl_vec_DNorm(sA, nstates);
599
600 /* transition probabilities */
601 double **tAA;
602 ESL_ALLOC(tAA, sizeof(double *) * nstates);
603 for(i = 0; i < nstates; i ++) ESL_ALLOC(tAA[i], sizeof(double) * nstates);
604
605 tAA[0][0] = 0.999483637183643;
606 tAA[0][1] = 0.000317942006440604;
607 tAA[0][2] = 0.000185401071732768;
608 tAA[0][3] = 2.60394763669618e-07;
609 tAA[0][4] = 1.27593434198113e-05;
610 esl_vec_DNorm(tAA[0], nstates);
611
612 tAA[1][0] = 9.76333640771184e-05;
613 tAA[1][1] = 0.99980020511745;
614 tAA[1][2] = 9.191359010352e-05;
615 tAA[1][3] = 7.94413051888677e-08;
616 tAA[1][4] = 1.01684870641751e-05;
617 esl_vec_DNorm(tAA[1], nstates);
618
619 tAA[2][0] = 1.3223694798182e-07;
620 tAA[2][1] = 0.000155642887774602;
621 tAA[2][2] = 0.999700615549769;
622 tAA[2][3] = 9.15079680034191e-05;
623 tAA[2][4] = 5.21013575048369e-05;
624 esl_vec_DNorm(tAA[2], nstates);
625
626 tAA[3][0] = 0.994252873563218;
627 tAA[3][1] = 0.0014367816091954;
628 tAA[3][2] = 0.0014367816091954;
629 tAA[3][3] = 0.0014367816091954;
630 tAA[3][4] = 0.0014367816091954;
631 esl_vec_DNorm(tAA[3], nstates);
632
633 tAA[4][0] = 8.32138798088677e-06;
634 tAA[4][1] = 2.16356087503056e-05;
635 tAA[4][2] = 6.42411152124459e-05;
636 tAA[4][3] = 1.66427759617735e-07;
637 tAA[4][4] = 0.999905635460297;
638 esl_vec_DNorm(tAA[4], nstates);
639
640 /* emission probabilities */
641 double **eAA;
642 ESL_ALLOC(eAA, sizeof(double *) * nstates);
643 for(i = 0; i < nstates; i ++) ESL_ALLOC(eAA[i], sizeof(double) * abc->K);
644
645 eAA[0][0] = 0.370906566523225;
646 eAA[0][1] = 0.129213995153577;
647 eAA[0][2] = 0.130511270043053;
648 eAA[0][3] = 0.369368168280145;
649 esl_vec_DNorm(eAA[0], abc->K);
650
651 eAA[1][0] = 0.305194882571888;
652 eAA[1][1] = 0.194580936415687;
653 eAA[1][2] = 0.192343972160245;
654 eAA[1][3] = 0.307880208852179;
655 esl_vec_DNorm(eAA[1], abc->K);
656
657 eAA[2][0] = 0.238484980800698;
658 eAA[2][1] = 0.261262845707113;
659 eAA[2][2] = 0.261810301531792;
660 eAA[2][3] = 0.238441871960397;
661 esl_vec_DNorm(eAA[2], abc->K);
662
663 eAA[3][0] = 0.699280575539568;
664 eAA[3][1] = 0.00143884892086331;
665 eAA[3][2] = 0.00143884892086331;
666 eAA[3][3] = 0.297841726618705;
667 esl_vec_DNorm(eAA[3], abc->K);
668
669 eAA[4][0] = 0.169064007664923;
670 eAA[4][1] = 0.331718611320207;
671 eAA[4][2] = 0.33045427183482;
672 eAA[4][3] = 0.16876310918005;
673 esl_vec_DNorm(eAA[4], abc->K);
674
675 *ret_sA = sA;
676 *ret_tAA = tAA;
677 *ret_eAA = eAA;
678 *ret_nstates = nstates;
679
680 return eslOK;
681
682 ERROR:
683 return status;
684 }
685
686 /* Function: SampleGenomicSequenceFromHMM()
687 * Date: EPN, Tue May 20 17:40:54 2008
688 *
689 * Purpose: Sample a sequence of length L from an HMM. The HMM defined
690 * by three arrays:
691 *
692 * sA - start probabilities [0..nstates-1]
693 * tAA - transition probabilities [0..nstates-1][0..nstates-1]
694 * eAA - emission probabilities [0..nstates-1][0..abc->K-1]
695 *
696 * ret_dsq - the sampled sequence
697 */
698 int
699 SampleGenomicSequenceFromHMM(ESL_RANDOMNESS *r, const ESL_ALPHABET *abc, char *errbuf, double *sA, double **tAA, double **eAA, int nstates, int L, ESL_DSQ **ret_dsq)
700 {
701 int status;
702 ESL_DSQ *dsq = NULL;
703 int si, x;
704
705 ESL_ALLOC(dsq, sizeof(ESL_DSQ) * (L+2));
706 dsq[0] = dsq[L+1] = eslDSQ_SENTINEL;
707
708 /* pick initial state to emit from */
709 si = esl_rnd_DChoose(r, sA, nstates);
710 for (x = 1; x <= L; x++) {
711 dsq[x] = esl_rnd_DChoose(r, eAA[si], abc->K); /* emit residue */
712 si = esl_rnd_DChoose(r, tAA[si], nstates); /* make transition */
713 }
714
715 *ret_dsq = dsq;
716 return eslOK;
717
718 ERROR:
719 return status;
720 }
721
722 /* Function: CopyExpInfo()
723 * Date: EPN, Tue May 24 09:59:14 2011
724 *
725 * Purpose: Copy an ExpInfo_t object.
726 *
727 * Returns: eslOK on success.
728 */
729 int
730 CopyExpInfo(ExpInfo_t *src, ExpInfo_t *dest)
731 {
732 dest->cur_eff_dbsize = src->cur_eff_dbsize;
733 dest->lambda = src->lambda;
734 dest->mu_extrap = src->mu_extrap;
735 dest->mu_orig = src->mu_orig;
736 dest->dbsize = src->dbsize;
737 dest->nrandhits = src->nrandhits;
738 dest->tailp = src->tailp;
739 dest->is_valid = src->is_valid;
740
741 return eslOK;
742 }
743
744