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