forked from samtools/bcftools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HMM.c
435 lines (372 loc) · 13.9 KB
/
HMM.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* The MIT License
Copyright (c) 2014-2015 Genome Research Ltd.
Author: Petr Danecek <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <htslib/hts.h>
#include "HMM.h"
struct _hmm_t
{
int nstates; // number of states
double *vprob, *vprob_tmp; // viterbi probs [nstates]
uint8_t *vpath; // viterbi path [nstates*nvpath]
double *bwd, *bwd_tmp; // bwd probs [nstates]
double *fwd; // fwd probs [nstates*(nfwd+1)]
int nvpath, nfwd;
int ntprob_arr; // number of pre-calculated tprob matrices
double *curr_tprob, *tmp; // Temporary arrays; curr_tprob is short lived, valid only for
// one site (that is, one step of Viterbi algorithm)
double *tprob_arr; // Array of transition matrices, precalculated to ntprob_arr
// positions. The first matrix is the initial tprob matrix
// set by hmm_init() or hmm_set_tprob()
set_tprob_f set_tprob; // Optional user function to set / modify transition probabilities
// at each site (one step of Viterbi algorithm)
void *set_tprob_data;
double *init_probs; // Initial state probabilities, NULL for uniform probs
};
uint8_t *hmm_get_viterbi_path(hmm_t *hmm) { return hmm->vpath; }
double *hmm_get_tprob(hmm_t *hmm) { return hmm->tprob_arr; }
int hmm_get_nstates(hmm_t *hmm) { return hmm->nstates; }
double *hmm_get_fwd_bwd_prob(hmm_t *hmm) { return hmm->fwd; }
static inline void multiply_matrix(int n, double *a, double *b, double *dst, double *tmp)
{
double *out = dst;
if ( a==dst || b==dst )
out = tmp;
int i,j,k;
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
double val = 0;
for (k=0; k<n; k++) val += MAT(a,n,i,k)*MAT(b,n,k,j);
MAT(out,n,i,j) = val;
}
}
if ( out!=dst )
memcpy(dst,out,sizeof(double)*n*n);
}
hmm_t *hmm_init(int nstates, double *tprob, int ntprob)
{
hmm_t *hmm = (hmm_t*) calloc(1,sizeof(hmm_t));
hmm->nstates = nstates;
hmm->curr_tprob = (double*) malloc(sizeof(double)*nstates*nstates);
hmm->tmp = (double*) malloc(sizeof(double)*nstates*nstates);
hmm_set_tprob(hmm, tprob, ntprob);
return hmm;
}
void hmm_init_states(hmm_t *hmm, double *probs)
{
if ( !probs )
{
free(hmm->init_probs);
hmm->init_probs = NULL;
}
if ( !hmm->init_probs ) hmm->init_probs = (double*) malloc(sizeof(double)*hmm->nstates);
memcpy(hmm->init_probs,probs,sizeof(double)*hmm->nstates);
}
void hmm_set_tprob(hmm_t *hmm, double *tprob, int ntprob)
{
hmm->ntprob_arr = ntprob;
if ( ntprob<=0 ) ntprob = 1;
if ( !hmm->tprob_arr )
hmm->tprob_arr = (double*) malloc(sizeof(double)*hmm->nstates*hmm->nstates*ntprob);
memcpy(hmm->tprob_arr,tprob,sizeof(double)*hmm->nstates*hmm->nstates);
int i;
for (i=1; i<ntprob; i++)
multiply_matrix(hmm->nstates, hmm->tprob_arr, hmm->tprob_arr+(i-1)*hmm->nstates*hmm->nstates, hmm->tprob_arr+i*hmm->nstates*hmm->nstates, hmm->tmp);
}
void hmm_set_tprob_func(hmm_t *hmm, set_tprob_f set_tprob, void *data)
{
hmm->set_tprob = set_tprob;
hmm->set_tprob_data = data;
}
static void _set_tprob(hmm_t *hmm, int pos_diff)
{
assert( pos_diff>=0 );
int i, n;
n = hmm->ntprob_arr ? pos_diff % hmm->ntprob_arr : 0; // n-th precalculated matrix
memcpy(hmm->curr_tprob, hmm->tprob_arr+n*hmm->nstates*hmm->nstates, sizeof(*hmm->curr_tprob)*hmm->nstates*hmm->nstates);
if ( hmm->ntprob_arr > 0 )
{
n = pos_diff / hmm->ntprob_arr; // number of full blocks to jump
for (i=0; i<n; i++)
multiply_matrix(hmm->nstates, hmm->tprob_arr+(hmm->ntprob_arr-1)*hmm->nstates*hmm->nstates, hmm->curr_tprob, hmm->curr_tprob, hmm->tmp);
}
}
void hmm_run_viterbi(hmm_t *hmm, int n, double *eprobs, uint32_t *sites)
{
// Init arrays when run for the first time
if ( hmm->nvpath < n )
{
hmm->nvpath = n;
hmm->vpath = (uint8_t*) realloc(hmm->vpath, sizeof(uint8_t)*hmm->nvpath*hmm->nstates);
}
if ( !hmm->vprob )
{
hmm->vprob = (double*) malloc(sizeof(double)*hmm->nstates);
hmm->vprob_tmp = (double*) malloc(sizeof(double)*hmm->nstates);
}
// Init all states with equal likelihood
int i,j, nstates = hmm->nstates;
if ( hmm->init_probs )
for (i=0; i<nstates; i++) hmm->vprob[i] = hmm->init_probs[i];
else
for (i=0; i<nstates; i++) hmm->vprob[i] = 1./nstates;
// Run Viterbi
uint32_t prev_pos = sites[0];
for (i=0; i<n; i++)
{
uint8_t *vpath = &hmm->vpath[i*nstates];
double *eprob = &eprobs[i*nstates];
int pos_diff = sites[i] == prev_pos ? 0 : sites[i] - prev_pos - 1;
_set_tprob(hmm, pos_diff);
if ( hmm->set_tprob ) hmm->set_tprob(hmm, prev_pos, sites[i], hmm->set_tprob_data, hmm->curr_tprob);
prev_pos = sites[i];
double vnorm = 0;
for (j=0; j<nstates; j++)
{
double vmax = 0;
int k, k_vmax = 0;
for (k=0; k<nstates; k++)
{
double pval = hmm->vprob[k] * MAT(hmm->curr_tprob,hmm->nstates,j,k);
if ( vmax < pval ) { vmax = pval; k_vmax = k; }
}
vpath[j] = k_vmax;
hmm->vprob_tmp[j] = vmax * eprob[j];
vnorm += hmm->vprob_tmp[j];
}
for (j=0; j<nstates; j++) hmm->vprob_tmp[j] /= vnorm;
double *tmp = hmm->vprob; hmm->vprob = hmm->vprob_tmp; hmm->vprob_tmp = tmp;
}
// Find the most likely state
int iptr = 0;
for (i=1; i<nstates; i++)
if ( hmm->vprob[iptr] < hmm->vprob[i] ) iptr = i;
// Trace back the Viterbi path, we are reusing vpath for storing the states (vpath[i*nstates])
for (i=n-1; i>=0; i--)
{
assert( iptr<nstates && hmm->vpath[i*nstates + iptr]<nstates );
iptr = hmm->vpath[i*nstates + iptr];
hmm->vpath[i*nstates] = iptr; // reusing the array for different purpose here
}
}
void hmm_run_fwd_bwd(hmm_t *hmm, int n, double *eprobs, uint32_t *sites)
{
// Init arrays when run for the first time
if ( hmm->nfwd < n )
{
hmm->nfwd = n;
hmm->fwd = (double*) realloc(hmm->fwd, sizeof(double)*(hmm->nfwd+1)*hmm->nstates);
}
if ( !hmm->bwd )
{
hmm->bwd = (double*) malloc(sizeof(double)*hmm->nstates);
hmm->bwd_tmp = (double*) malloc(sizeof(double)*hmm->nstates);
}
// Init all states with equal likelihood
int i,j,k, nstates = hmm->nstates;
if ( hmm->init_probs )
{
for (i=0; i<nstates; i++) hmm->fwd[i] = hmm->init_probs[i];
for (i=0; i<nstates; i++) hmm->bwd[i] = hmm->init_probs[i];
}
else
{
for (i=0; i<nstates; i++) hmm->fwd[i] = 1./hmm->nstates;
for (i=0; i<nstates; i++) hmm->bwd[i] = 1./hmm->nstates;
}
// Run fwd
uint32_t prev_pos = sites[0];
for (i=0; i<n; i++)
{
double *fwd_prev = &hmm->fwd[i*nstates];
double *fwd = &hmm->fwd[(i+1)*nstates];
double *eprob = &eprobs[i*nstates];
int pos_diff = sites[i] == prev_pos ? 0 : sites[i] - prev_pos - 1;
_set_tprob(hmm, pos_diff);
if ( hmm->set_tprob ) hmm->set_tprob(hmm, prev_pos, sites[i], hmm->set_tprob_data, hmm->curr_tprob);
prev_pos = sites[i];
double norm = 0;
for (j=0; j<nstates; j++)
{
double pval = 0;
for (k=0; k<nstates; k++)
pval += fwd_prev[k] * MAT(hmm->curr_tprob,hmm->nstates,j,k);
fwd[j] = pval * eprob[j];
norm += fwd[j];
}
for (j=0; j<nstates; j++) fwd[j] /= norm;
}
// Run bwd
double *bwd = hmm->bwd, *bwd_tmp = hmm->bwd_tmp;
prev_pos = sites[n-1];
for (i=0; i<n; i++)
{
double *fwd = &hmm->fwd[(n-i)*nstates];
double *eprob = &eprobs[(n-i-1)*nstates];
int pos_diff = sites[n-i-1] == prev_pos ? 0 : prev_pos - sites[n-i-1] - 1;
_set_tprob(hmm, pos_diff);
if ( hmm->set_tprob ) hmm->set_tprob(hmm, sites[n-i-1], prev_pos, hmm->set_tprob_data, hmm->curr_tprob);
prev_pos = sites[n-i-1];
double bwd_norm = 0;
for (j=0; j<nstates; j++)
{
double pval = 0;
for (k=0; k<nstates; k++)
pval += bwd[k] * eprob[k] * MAT(hmm->curr_tprob,hmm->nstates,k,j);
bwd_tmp[j] = pval;
bwd_norm += pval;
}
double norm = 0;
for (j=0; j<nstates; j++)
{
bwd_tmp[j] /= bwd_norm;
fwd[j] *= bwd_tmp[j]; // fwd now stores fwd*bwd
norm += fwd[j];
}
for (j=0; j<nstates; j++) fwd[j] /= norm;
double *tmp = bwd_tmp; bwd_tmp = bwd; bwd = tmp;
}
}
void hmm_run_baum_welch(hmm_t *hmm, int n, double *eprobs, uint32_t *sites)
{
// Init arrays when run for the first time
if ( hmm->nfwd < n )
{
hmm->nfwd = n;
hmm->fwd = (double*) realloc(hmm->fwd, sizeof(double)*(hmm->nfwd+1)*hmm->nstates);
}
if ( !hmm->bwd )
{
hmm->bwd = (double*) malloc(sizeof(double)*hmm->nstates);
hmm->bwd_tmp = (double*) malloc(sizeof(double)*hmm->nstates);
}
// Init all states with equal likelihood
int i,j,k, nstates = hmm->nstates;
if ( hmm->init_probs )
{
for (i=0; i<nstates; i++) hmm->fwd[i] = hmm->init_probs[i];
for (i=0; i<nstates; i++) hmm->bwd[i] = hmm->init_probs[i];
}
else
{
for (i=0; i<nstates; i++) hmm->fwd[i] = 1./hmm->nstates;
for (i=0; i<nstates; i++) hmm->bwd[i] = 1./hmm->nstates;
}
// New transition matrix: temporary values
double *tmp_xi = (double*) calloc(nstates*nstates,sizeof(double));
double *tmp_gamma = (double*) calloc(nstates,sizeof(double));
double *fwd_bwd = (double*) malloc(sizeof(double)*nstates);
// Run fwd
uint32_t prev_pos = sites[0];
for (i=0; i<n; i++)
{
double *fwd_prev = &hmm->fwd[i*nstates];
double *fwd = &hmm->fwd[(i+1)*nstates];
double *eprob = &eprobs[i*nstates];
int pos_diff = sites[i] == prev_pos ? 0 : sites[i] - prev_pos - 1;
_set_tprob(hmm, pos_diff);
if ( hmm->set_tprob ) hmm->set_tprob(hmm, prev_pos, sites[i], hmm->set_tprob_data, hmm->curr_tprob);
prev_pos = sites[i];
double norm = 0;
for (j=0; j<nstates; j++)
{
double pval = 0;
for (k=0; k<nstates; k++)
pval += fwd_prev[k] * MAT(hmm->curr_tprob,hmm->nstates,j,k);
fwd[j] = pval * eprob[j];
norm += fwd[j];
}
for (j=0; j<nstates; j++) fwd[j] /= norm;
}
// Run bwd
double *bwd = hmm->bwd, *bwd_tmp = hmm->bwd_tmp;
prev_pos = sites[n-1];
for (i=0; i<n; i++)
{
double *fwd = &hmm->fwd[(n-i)*nstates];
double *eprob = &eprobs[(n-i-1)*nstates];
int pos_diff = sites[n-i-1] == prev_pos ? 0 : prev_pos - sites[n-i-1] - 1;
_set_tprob(hmm, pos_diff);
if ( hmm->set_tprob ) hmm->set_tprob(hmm, sites[n-i-1], prev_pos, hmm->set_tprob_data, hmm->curr_tprob);
prev_pos = sites[n-i-1];
double bwd_norm = 0;
for (j=0; j<nstates; j++)
{
double pval = 0;
for (k=0; k<nstates; k++)
pval += bwd[k] * eprob[k] * MAT(hmm->curr_tprob,hmm->nstates,k,j);
bwd_tmp[j] = pval;
bwd_norm += pval;
}
double norm = 0;
for (j=0; j<nstates; j++)
{
bwd_tmp[j] /= bwd_norm;
fwd_bwd[j] = fwd[j]*bwd_tmp[j];
norm += fwd_bwd[j];
}
for (j=0; j<nstates; j++)
{
fwd_bwd[j] /= norm;
tmp_gamma[j] += fwd_bwd[j];
}
for (j=0; j<nstates; j++)
{
for (k=0; k<nstates; k++)
{
MAT(tmp_xi,nstates,k,j) += fwd[j]*bwd[k]*MAT(hmm->tprob_arr,hmm->nstates,k,j)*eprob[k] / norm;
}
}
for (j=0; j<nstates; j++) fwd[j] = fwd_bwd[j]; // fwd now stores fwd*bwd
double *tmp = bwd_tmp; bwd_tmp = bwd; bwd = tmp;
}
for (j=0; j<nstates; j++)
{
double norm = 0;
for (k=0; k<nstates; k++)
{
MAT(hmm->curr_tprob,nstates,k,j) = MAT(tmp_xi,nstates,k,j) / tmp_gamma[j];
norm += MAT(hmm->curr_tprob,nstates,k,j);
}
for (k=0; k<nstates; k++)
MAT(hmm->curr_tprob,nstates,k,j) /= norm;
}
free(tmp_gamma);
free(tmp_xi);
free(fwd_bwd);
}
void hmm_destroy(hmm_t *hmm)
{
free(hmm->init_probs);
free(hmm->vprob);
free(hmm->vprob_tmp);
free(hmm->vpath);
free(hmm->curr_tprob);
free(hmm->tmp);
free(hmm->tprob_arr);
free(hmm->fwd);
free(hmm->bwd);
free(hmm->bwd_tmp);
free(hmm);
}