| Bsoft 2.0.5 Bernard's software package

dynamic_programming.cpp
Go to the documentation of this file.
1
9 #include "dynamic_programming.h"
10 #include "utilities.h"
11
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14
27 long dp_matrix_scoring(Matrix mat, double gapopen, double gapextend)
28 {
29 int m = mat.columns(), n = mat.rows();
30 int t, pt(0);
31 long i, j, k, kmax(0);
32 double value, max, maxall(0);
33
34 for ( k=j=0; j<n; j++ ) {
35 for ( i=0; i<m; i++, k++ ) {
36 if ( i==0 || j==0 ) max = mat[j][i];
37 else max = mat[j][i] + mat[j-1][i-1];
38 value = 0;
39 t = 0;
40 if ( i > 0 ) {
41 if ( pt == 1 ) value = mat[j][i-1] - gapextend;
42 else value = mat[j][i-1] - gapopen;
43 }
44 if ( max < value ) {
45 max = value;
46 t = 1;
47 }
48 if ( j > 0 ) {
49 if ( pt == 2 ) value = mat[j-1][i] - gapextend;
50 else value = mat[j-1][i] - gapopen;
51 }
52 if ( max < value ) {
53 max = value;
54 t = 2;
55 }
56 mat[j][i] = max;
57 if ( maxall < max ) {
58 maxall = max;
59 kmax = k;
60 }
61 pt = t;
62 }
63 }
64
65 return kmax;
66 }
67
84 int* dp_matrix_backtrace(Matrix mat, double gapopen, double gapextend, long& length)
85 {
86 int m = mat.columns(), n = mat.rows();
87 int t, pt(0);
88 long i, j, k, h, hbeg(0), hend, imax(0), jmax(0);
89 long tlen = m + n;
90 double max(0), tmax(0), value;
91 int* aln = new int[2*tlen];
92
93 for ( h=0; h<tlen*2; h++ ) aln[h] = -1;
94
95 for ( k=j=0; j<n; j++ ) {
96 for ( i=0; i<m; i++, k++ ) {
97 if ( tmax < mat[j][i] ) {
98 tmax = mat[j][i];
99 imax = i;
100 jmax = j;
101 }
102 }
103 }
104
105 // Fill in the trailing pieces
106 hend = m - imax;
107 if ( n - jmax > hend ) hend = n - jmax;
108 hend = tlen - hend;
109 for ( h=hend, i=imax; h<tlen && i<m; h++, i++ ) aln[h] = i;
110 for ( h=hend + tlen, j=jmax; h<2*tlen && j<n; h++, j++ ) aln[h] = j;
111
112 // Trace back to get the central aligned pieces
113 for ( h=hend, i=imax, j=jmax; i>0 && j>0; h-- ) {
114 k = m*j + i;
115 mat[j][i] = 2*tmax;
116 t = 0;
117 max = mat[j-1][i-1];
118 if ( pt == 1 ) value = mat[j][i-1] - gapextend;
119 else value = mat[j][i-1] - gapopen;
120 if ( max < value ) {
121 max = value;
122 t = 1;
123 }
124 if ( pt == 2 ) value = mat[j-1][i] - gapextend;
125 else value = mat[j-1][i] - gapopen;
126 if ( max < value ) {
127 max = value;
128 t = 2;
129 }
130 if ( t < 2 ) {
131 aln[h] = i;
132 i--;
133 }
134 if ( t%2 == 0 ) {
135 aln[h+tlen] = j;
136 j--;
137 }
138 pt = t;
139 }
140 k = m*j + i;
141 mat[j][i] = 2*tmax;
142
143 // Fill in the leading pieces
144 for ( ; h >= 0; i--, j--, h-- ) {
145 if ( i >= 0 ) aln[h] = i;
146 if ( j >= 0 ) aln[h+tlen] = j;
147 if ( i >= 0 || j >= 0 ) hbeg = h;
148 }
149
150 // Generate the return array
151 length = tlen - hbeg;
152 int* align = new int[2*length];
153
154 for ( i=0, h=hbeg; i<length; i++, h++ ) {
155 align[i] = aln[h];
156 align[i+length] = aln[h+tlen];
157 }
158
159 delete[] aln;
160
161 return align;
162 }
Matrix::rows
long rows()
Definition: Matrix.h:153
utilities.h
Header file for general utilities.
dp_matrix_scoring
long dp_matrix_scoring(Matrix mat, double gapopen, double gapextend)
Calculates the scoring matrix in dynamic programming.
Definition: dynamic_programming.cpp:27
Matrix::columns
long columns()
Definition: Matrix.h:154
dp_matrix_backtrace
int * dp_matrix_backtrace(Matrix mat, double gapopen, double gapextend, long &length)
Backtraces the scoring matrix in dynamic programming.
Definition: dynamic_programming.cpp:84
verbose
int verbose
length
double length(vector< T > v)
Definition: Vector3.h:528
Matrix
Definition: Matrix.h:21
kmax
long kmax(long i, long km, long m)
Definition: Bimage_filter.cpp:22
dynamic_programming.h
Library functions for dynamic programming.