khmm.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef AC_SCHMM_H_
  2. #define AC_SCHMM_H_
  3. /*
  4. * Last Modified: 2008-03-10
  5. * Version: 0.1.0-8
  6. *
  7. * 2008-03-10, 0.1.0-8: make icc report two more "VECTORIZED"
  8. * 2008-03-10, 0.1.0-7: accelerate for some CPU
  9. * 2008-02-07, 0.1.0-6: simulate sequences
  10. * 2008-01-15, 0.1.0-5: goodness of fit
  11. * 2007-11-20, 0.1.0-4: add function declaration of hmm_post_decode()
  12. * 2007-11-09: fix a memory leak
  13. */
  14. #include <stdlib.h>
  15. #define HMM_VERSION "0.1.0-7"
  16. #define HMM_FORWARD 0x02
  17. #define HMM_BACKWARD 0x04
  18. #define HMM_VITERBI 0x40
  19. #define HMM_POSTDEC 0x80
  20. #ifndef FLOAT
  21. #define FLOAT double
  22. #endif
  23. #define HMM_TINY 1e-25
  24. #define HMM_INF 1e300
  25. typedef struct
  26. {
  27. int m, n; // number of symbols, number of states
  28. FLOAT **a, **e; // transition matrix and emitting probilities
  29. FLOAT **ae; // auxiliary array for acceleration, should be calculated by hmm_pre_backward()
  30. FLOAT *a0; // trasition matrix from the start state
  31. } hmm_par_t;
  32. typedef struct
  33. {
  34. int L;
  35. unsigned status;
  36. char *seq;
  37. FLOAT **f, **b, *s;
  38. int *v; // Viterbi path
  39. int *p; // posterior decoding
  40. } hmm_data_t;
  41. typedef struct
  42. {
  43. int m, n;
  44. FLOAT Q0, **A, **E, *A0;
  45. } hmm_exp_t;
  46. typedef struct
  47. {
  48. int l, *obs;
  49. FLOAT *thr;
  50. } hmm_gof_t;
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. /* initialize and destroy hmm_par_t */
  55. hmm_par_t *hmm_new_par(int m, int n);
  56. void hmm_delete_par(hmm_par_t *hp);
  57. /* initialize and destroy hmm_data_t */
  58. hmm_data_t *hmm_new_data(int L, const char *seq, const hmm_par_t *hp);
  59. void hmm_delete_data(hmm_data_t *hd);
  60. /* initialize and destroy hmm_exp_t */
  61. hmm_exp_t *hmm_new_exp(const hmm_par_t *hp);
  62. void hmm_delete_exp(hmm_exp_t *he);
  63. /* Viterbi, forward and backward algorithms */
  64. FLOAT hmm_Viterbi(const hmm_par_t *hp, hmm_data_t *hd);
  65. void hmm_pre_backward(hmm_par_t *hp);
  66. void hmm_forward(const hmm_par_t *hp, hmm_data_t *hd);
  67. void hmm_backward(const hmm_par_t *hp, hmm_data_t *hd);
  68. /* log-likelihood of the observations (natural based) */
  69. FLOAT hmm_lk(const hmm_data_t *hd);
  70. /* posterior probability at the position on the sequence */
  71. FLOAT hmm_post_state(const hmm_par_t *hp, const hmm_data_t *hd, int u, FLOAT *prob);
  72. /* posterior decoding */
  73. void hmm_post_decode(const hmm_par_t *hp, hmm_data_t *hd);
  74. /* expected counts of transitions and emissions */
  75. hmm_exp_t *hmm_expect(const hmm_par_t *hp, const hmm_data_t *hd);
  76. /* add he0 counts to he1 counts*/
  77. void hmm_add_expect(const hmm_exp_t *he0, hmm_exp_t *he1);
  78. /* the Q function that should be maximized in EM */
  79. FLOAT hmm_Q(const hmm_par_t *hp, const hmm_exp_t *he);
  80. FLOAT hmm_Q0(const hmm_par_t *hp, hmm_exp_t *he);
  81. /* simulate sequences */
  82. char *hmm_simulate(const hmm_par_t *hp, int L);
  83. #ifdef __cplusplus
  84. }
  85. #endif
  86. static inline void **calloc2(int n_row, int n_col, int size)
  87. {
  88. char **p;
  89. int k;
  90. p = (char**)malloc(sizeof(char*) * n_row);
  91. for (k = 0; k != n_row; ++k)
  92. p[k] = (char*)calloc(n_col, size);
  93. return (void**)p;
  94. }
  95. #endif