forked from pkrusche/vt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ahmm.h
584 lines (481 loc) · 12.5 KB
/
ahmm.h
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/* The MIT License
Copyright (c) 2014 Adrian Tan <[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.
*/
#ifndef AHMM_H
#define AHMM_H
#include <iomanip>
#include "htslib/kstring.h"
#include "log_tool.h"
#define MAXLEN 1024
#define MAXLEN_NBITS 10
#define S 0
#define M 1
#define D 2
#define I 3
#define E 4
#define N 5
#define TBD 6
#define NSTATES 6
#define LFLANK 0
#define MOTIF 1
#define RFLANK 2
#define UNMODELED 3
#define UNCERTAIN 4
//match type
#define MODEL 0
#define READ 1
#define MATCH 2
/*for indexing single array*/
#define index(i,j) (((i)<<MAXLEN_NBITS)+(j))
/*functions for getting trace back information*/
#define track_get_u(t) (((t)&0xF8000000)>>27)
#define track_get_d(t) (((t)&0x07000000)>>24)
#define track_get_c(t) (((t)&0x00FFF000)>>12)
#define track_get_p(t) (((t)&0x00000FFF))
#define track_get_base(t) (model[track_get_d(t)][track_get_p(t)-1])
#define track_valid(t) ((track_get_d(t)==RFLANK||track_get_d(t)==MOTIF||track_get_d(t)==LFLANK)&&track_get_p(t)!=0)
#define track_set_u(t,u) (((t)&0x07FFFFFF)|((u)<<27))
#define track_set_d(t,d) (((t)&0xF8FFFFFF)|((d)<<24))
#define track_set_c(t,c) (((t)&0xFF000FFF)|((c)<<12))
#define track_set_p(t,p) (((t)&0xFFFFF000)|(p))
#define make_track(u,d,c,p) (((u)<<27)|((d)<<24)|((c)<<12)|(p))
//[N|!|0|0]
#define NULL_TRACK 0x7C000000
//[N|l|0|0]
#define START_TRACK 0x78000000
class AHMMParameters
{
public:
float delta;
float epsilon;
float tau;
float eta;
float mismatch_penalty;
AHMMParameters()
{
delta = 0.0001;
epsilon = 0.001;
tau = 0.01;
eta = 0.01;
mismatch_penalty = 1;
};
};
class AHMM
{
public:
const char* read;
const char* qual;
//model variables
//array indexed by LFLANK, MOTIF
char **model;
//length of read, probe and components in the model
int32_t rlen, plen, lflen, mlen;
/*result variables*/
int32_t lflank_start[2], lflank_end[2], motif_start[2], motif_end[2], rflank_start[2], rflank_end[2];
int32_t motif_count, exact_motif_count, motif_m, motif_xid;
int32_t* motif_discordance;
float motif_concordance, maxLogOdds;
AHMMParameters par;
//for track intermediate scores during Viterbi algorithm
float max_score;
int32_t max_track;
//for optimal alignment
bool optimal_path_traced;
float optimal_score;
int32_t optimal_state;
int32_t optimal_track;
int32_t optimal_probe_len;
int32_t *optimal_path; // for storage
int32_t *optimal_path_ptr; //just a pointer
int32_t optimal_path_len;
float T[NSTATES][NSTATES];
float **V;
int32_t **U;
LogTool *lt;
typedef int32_t (AHMM::*move) (int32_t t, int32_t j);
move **moves;
bool debug;
/**
* Constructor.
*/
AHMM(bool debug=false);
/**
* Constructor.
*/
AHMM(LogTool *lt, bool debug=false);
/**
* Destructor.
*/
~AHMM();
/**
* Initializes object, helper function for constructor.
*/
void initialize();
/**
* Initializes objects for constructor.
*/
void initialize_structures();
/**
* Initialize transition matrix based on parameters.
*/
void initialize_T();
/**
* Initializes U and V.
*/
void initialize_UV();
/**
* Sets a model.
*/
void set_model(const char* motif);
/**
* Sets delta.
*/
void set_delta(float delta);
/**
* Sets epsilon.
*/
void set_epsilon(float epsilon);
/**
* Sets tau.
*/
void set_tau(float tau);
/**
* Sets eta.
*/
void set_eta(float eta);
/**
* Sets mismatch penalty.
*/
void set_mismatch_penalty(float mismatch_penalty);
/**
* Sets debug.
*/
void set_debug(bool debug);
/**
* Get left flank start position for model.
*/
int32_t get_lflank_model_spos1();
/**
* Get left flank end position for model.
*/
int32_t get_lflank_model_epos1();
/**
* Get motif start position for model.
*/
int32_t get_motif_model_spos1();
/**
* Get motif end position for model.
*/
int32_t get_motif_model_epos1();
/**
* Get right flank start position for model.
*/
int32_t get_rflank_model_spos1();
/**
* Get right flank end position for model.
*/
int32_t get_rflank_model_epos1();
/**
* Get left flank start position for read.
*/
int32_t get_lflank_read_spos1();
/**
* Get left flank end position for read.
*/
int32_t get_lflank_read_epos1();
/**
* Get motif start position for read.
*/
int32_t get_motif_read_spos1();
/**
* Get motif end position for read.
*/
int32_t get_motif_read_epos1();
/**
* Get right flank start position for read.
*/
int32_t get_rflank_read_spos1();
/**
* Get right flank end position for read.
*/
int32_t get_rflank_read_epos1();
/**
* Computes the score associated with the move from A to B
* Updates the max_score and associated max_track.
*
* @A - start state
* @B - end state
* @index1 - flattened index of the one dimensional array of start state
* @j - 1 based position of read of start state
* @m - base match required (MATCH, MODEL_ONLY, READ_ONLY)
*/
void proc_comp(int32_t A, int32_t B, int32_t i, int32_t j, int32_t match_type);
/**
* Align and compute genotype likelihood.
*/
void align(const char* y, const char* qual=NULL);
/**
* Trace path after alignment.
*/
void trace_path();
/**
* Compute log10 emission odds based on equal error probability distribution contrasted against log10(1/16).
*/
float log10_emission_odds(char read_base, char probe_base, uint32_t pl, float mismatch_penalty);
/**
* Compute log10 emission odds based on equal error probability distribution contrasted against log10(1/16).
*/
float log10_emission_odds(char read_base, char probe_base, uint32_t pl);
/**
* Converts state to string representation.
*/
std::string state2string(int32_t state);
/**
* Converts state to cigar string representation.
*/
std::string state2cigarstring(int32_t state);
/**
* Converts track to cigar string representation.
*/
std::string track2cigarstring1(int32_t t, int32_t j);
/**
* Converts state to cigar string representation.
*/
std::string track2cigarstring2(int32_t t);
/**
* Converts model component to string representation.
*/
std::string component2string(int32_t component);
/**
* Prints an alignment.
*/
void print_alignment();
/**
* Prints an alignment with padding.
*/
void print_alignment(std::string& pad);
/**
* Prints a float matrix.
*/
void print(float *v, size_t plen, size_t rlen);
/**
* Prints a int32_t matrix.
*/
void print(int32_t *v, size_t plen, size_t rlen);
/**
* Prints the transition matrix.
*/
void print_T();
/**
* Prints U.
*/
void print_U(int32_t *U, size_t plen, size_t rlen);
/**
* Prints U and V.
*/
void print_trace(int32_t state, size_t plen, size_t rlen);
/**
* Collect alignment summary statistics.
*/
void collect_statistics(int32_t t1, int32_t t2, int32_t j);
/**
* Clear alignment statistics.
*/
void clear_statistics();
/**
* Update alignment statistics after collection.
*/
void update_statistics();
/**
* Returns true if flanks are mapped.
*/
bool flanks_are_mapped();
/**
* Returns a string representation of track.
*/
std::string track2string(int32_t t);
/**
* Prints track.
*/
void print_track(int32_t t);
/**
* move_A_B(int32_t t, int32_t j)
*
* A - source state
* B - destination state
* t - source track (model position)
* j - source read position
*/
///////////////////
//Repeating Motif//
///////////////////
int32_t move_S_M(int32_t t, int32_t j)
{
if (t==START_TRACK)
{
return make_track(S,MOTIF,1,1);
}
return NULL_TRACK;
}
int32_t move_M_M(int32_t t, int32_t j)
{
int32_t p;
if ((p=track_get_p(t))<=mlen && j<rlen)
{
if (p==mlen)
{
return make_track(M,MOTIF,track_get_c(t)+1,1);
}
else
{
return make_track(M,MOTIF,track_get_c(t),p+1);
}
}
return NULL_TRACK;
}
int32_t move_D_M(int32_t t, int32_t j)
{
int32_t p;
if ((p=track_get_p(t))<=mlen && j<rlen)
{
if (p==mlen)
{
return make_track(D,MOTIF,track_get_c(t)+1,1);
}
else
{
return make_track(D,MOTIF,track_get_c(t),p+1);
}
}
return NULL_TRACK;
}
int32_t move_I_M(int32_t t, int32_t j)
{
int32_t p;
if ((p=track_get_p(t))<=mlen)
{
if (p==mlen)
{
return make_track(I,MOTIF,track_get_c(t)+1,1);
}
else
{
return make_track(I,MOTIF,track_get_c(t),p+1);
}
}
return NULL_TRACK;
}
int32_t move_S_D(int32_t t, int32_t j)
{
if (t==START_TRACK)
{
return make_track(S,MOTIF,1,1);
}
return NULL_TRACK;
}
int32_t move_M_D(int32_t t, int32_t j)
{
int32_t p;
if ((p=track_get_p(t))<=mlen)
{
if (p==mlen)
{
return make_track(M,MOTIF,track_get_c(t)+1,1);
}
else
{
return make_track(M,MOTIF,track_get_c(t),p+1);
}
}
return NULL_TRACK;
}
int32_t move_D_D(int32_t t, int32_t j)
{
int32_t p;
if ((p=track_get_p(t))<=mlen)
{
if (p==mlen)
{
return make_track(D,MOTIF,track_get_c(t)+1,1);
}
else
{
return make_track(D,MOTIF,track_get_c(t),p+1);
}
}
return NULL_TRACK;
}
int32_t move_S_I(int32_t t, int32_t j)
{
if (t==START_TRACK)
{
return make_track(S,MOTIF,1,1);
}
return NULL_TRACK;
}
int32_t move_M_I(int32_t t, int32_t j)
{
if (t!=NULL_TRACK && j<rlen)
{
return track_set_u(t,M);
}
return NULL_TRACK;
}
int32_t move_I_I(int32_t t, int32_t j)
{
if (t!=NULL_TRACK && j<rlen)
{
return track_set_u(t,I);
}
return NULL_TRACK;
}
};
#undef MAXLEN
#undef MAXLEN_NBITS
#undef S
#undef M
#undef I
#undef D
#undef E
#undef N
#undef TBD
#undef NSTATES
#undef LFLANK
#undef MOTIF
#undef RFLANK
#undef UNMODELED
#undef UNCERTAIN
#undef READ
#undef MODEL
#undef MATCH
#undef index
#undef track_get_u
#undef track_get_d
#undef track_get_d
#undef track_get_c
#undef track_get_p
#undef track_get_base
#undef track_valid
#undef track_set_u
#undef track_set_d
#undef track_set_c
#undef track_set_p
#undef make_track
#undef NULL_TRACK
#undef START_TRACK
#endif