1
/*****************************************************************
2
* Unipro UGENE - Integrated Bioinformatics Suite
3
* Copyright (C) 2008 Unipro, Russia (http://ugene.unipro.ru)
6
* This source code is distributed under the terms of the
7
* GNU General Public License. See the files COPYING and LICENSE
9
*****************************************************************/
11
#ifndef _GB2_DYN_TABLE_H_
12
#define _GB2_DYN_TABLE_H_
14
#include "RollingMatrix.h"
18
struct GB2_COREAPI_EXPORT MatchScores
26
class GB2_COREAPI_EXPORT DynTable : public RollingMatrix {
33
DynTable(int n, int m, bool _allowInsDel)
34
: RollingMatrix(n, m), allowInsDel(_allowInsDel)
37
scores.match = DEFAULT_SCORE_MATCH;
38
scores.ins = DEFAULT_SCORE_INS;
39
scores.del = DEFAULT_SCORE_DEL;
40
scores.mismatch = DEFAULT_SCORE_MISMATCH;
41
strategy = Strategy_Min;
43
DynTable(): RollingMatrix(0,0), allowInsDel(false)
46
DynTable( int n, int m, bool _allowInsDel, MatchScores _scores, FillStrategy _strategy = Strategy_Min )
47
: RollingMatrix(n, m), allowInsDel(_allowInsDel), scores(_scores), strategy(_strategy)
53
return get_value(n-1, m-1);
56
int getLastLen() const {
57
return getLen(n-1, m-1);
60
void match(int y, bool ok) {
64
virtual int get(int x, int y) const {
66
if (x<0) {return y+1;}
67
return RollingMatrix::get(x, y);
70
void match( int x, int y, bool ok )
72
int d = get_value( x-1, y-1 );
73
int res = d + (ok ? scores.match : scores.mismatch);
75
int u = get_value(x, y-1);
76
int l = get_value(x-1, y);
81
insdelRes = qMin( l+scores.ins, u+scores.del );
82
res = qMin( insdelRes, res );
88
set_pair( x, y, res, ok );
91
int getLen(int x, int y) const {
98
return 1 + getLen(x-1, y-1);
100
int v = get_value(x, y);
101
bool match = get_flag( x, y );
102
int d = get_value(x-1, y-1);
103
int l = get_value(x-1, y);
104
int u = get_value(x, y-1);
106
if( match && v == d + scores.match ) {
107
return 1 + getLen( x-1, y-1 );
109
if ( v == u + scores.del ) { //prefer deletion in X sequence to minimize result len
110
return getLen(x, y-1);
112
if ( !match && v == d + scores.mismatch ) { // prefer mismatch instead of insertion into X sequence
113
return 1 + getLen(x-1, y-1);
115
assert( v == l + scores.ins );
116
return 1 + getLen(x-1, y); // this is insertion into X sequence
121
for (int i=0; i<n; i++) {
122
for (int j=0; j<m; j++) {
123
set_pair(i, j, j+1, false);
127
void set_value( int x, int y, int val )
129
assert( !(val & MASK_FLAG_MISMATCH) );
130
int oldval = get( x, y );
131
set( x, y, (oldval & MASK_FLAG_MISMATCH) | val );
133
void set_mm_flag( int x, int y, bool flag )
135
int oldval = get( x, y );
136
set( x, y, (oldval & MASK_VALUE)|(flag ? Flag_Match : Flag_Mismatch) );
138
void set_pair( int x, int y, int val, bool flag )
140
assert( !(val & MASK_FLAG_MISMATCH) );
141
set( x, y, val | (flag ? Flag_Match : Flag_Mismatch) );
143
int get_value( int x, int y ) const
145
return get( x, y ) & MASK_VALUE;
147
bool get_flag( int x, int y ) const
149
return get( x, y ) & MASK_FLAG_MISMATCH;
154
const static int DEFAULT_SCORE_MATCH = 0;
155
const static int DEFAULT_SCORE_MISMATCH = 1;
156
const static int DEFAULT_SCORE_DEL = 1;
157
const static int DEFAULT_SCORE_INS = 1;
159
const static int MASK_VALUE = 0x7FFFFFFF;
160
const static int MASK_FLAG_MISMATCH = 0x80000000;
163
Flag_Mismatch = 0x00000000,
164
Flag_Match = 0x80000000
168
FillStrategy strategy;