| Bsoft 2.0.5 Bernard's software package

seq_align.cpp
Go to the documentation of this file.
1
9 #include "rwmolecule.h"
10 #include "rwresprop.h"
11 #include "dynamic_programming.h"
12 #include "seq_align.h"
13 #include "seq_util.h"
14 #include "linked_list.h"
15 #include "Matrix3.h"
16 #include "utilities.h"
17
18 // Declaration of global variables
19 extern int verbose; // Level of output to the screen
20
34 long seq_pair_align(Bmolecule* mol1, Bmolecule* mol2, double gapopen,
35 double gapextend, Bresidue_matrix* simat)
36 {
37 if ( verbose & VERB_PROCESS ) {
38 cout << "Aligning two sequences:" << endl;
39 cout << mol1->id << " with " << mol1->nres << " residues" << endl;
40 cout << mol2->id << " with " << mol2->nres << " residues" << endl;
41 cout << "Gap opening penalty: " << gapopen << endl;
42 cout << "Gap extension penalty: " << gapextend << endl;
43 cout << endl;
44 }
45
46 Matrix dp = seq_dot_plot(mol1, mol2, simat);
47
48 long imax = dp_matrix_scoring(dp, gapopen, gapextend);
49
50 long i(imax%dp.rows()), j(imax/dp.rows());
51 long alnlen(0);
52 double score = dp[j][i];
53
54 int* indaln = dp_matrix_backtrace(dp, gapopen, gapextend, alnlen);
55
56 // Bstring alnseq1(alnlen, '-');
57 // Bstring alnseq2(alnlen, '-');
58 Bstring alnseq1('-', alnlen);
59 Bstring alnseq2('-', alnlen);
60
61 for ( i=0, j=alnlen; i<alnlen; i++, j++ ) {
62 if ( indaln[i] > -1 ) alnseq1[i] = mol1->seq[indaln[i]];
63 if ( indaln[j] > -1 ) alnseq2[i] = mol2->seq[indaln[j]];
64 }
65
66 delete[] indaln;
67
68 mol1->seq = alnseq1;
69 mol2->seq = alnseq2;
70
71 if ( verbose & VERB_PROCESS ) {
72 cout << "Alignment length: " << alnlen << endl;
73 cout << "Alignment score: " << score << endl << endl;
74 }
75
76 if ( verbose & VERB_DEBUG )
77 cout << "DEBUG seq_pair_align:" << endl << alnseq1 << endl << alnseq2 << endl;
78
79 return alnlen;
80 }
81
94 int seq_find_best_offset(Bmolecule* mol1, Bmolecule* mol2,
95 long& nres, Bresidue_matrix* simat)
96 {
97 nres = 0;
98 if ( mol1->nres < 10 || mol2->nres < 10 ) return 0;
99
100 int offset(0);
101 long k, l;
102 Matrix dot = seq_dot_plot(mol1, mol2, simat);
103 vector<double> diag(mol1->nres+mol2->nres, 0);
104
105 for ( k=0; k<mol1->nres; k++ ) {
106 for ( l=0; l<mol2->nres; l++ ) {
107 if ( dot[l][k] > 0 ) diag[mol2->nres-l-1+k] += 1;
108 }
109 }
110
111 for ( k=0; k<mol1->nres+mol2->nres; k++ ) {
112 if ( nres < diag[k] ) {
113 nres = (long) diag[k];
114 offset = k - mol2->nres + 1;
115 }
116 }
117
118 if ( verbose & VERB_FULL ) {
119 cout << mol1->id << tab << mol2->id << ":\t" << offset << tab << nres << endl;
120 if ( offset > 0 ) cout << &mol1->seq[offset] << endl;
121 else cout << mol1->seq << endl;
122 if ( offset < 0 ) cout << &mol2->seq[-offset] << endl;
123 else cout << mol2->seq << endl;
124 cout << endl;
125 }
126
127 return offset;
128 }
129
137 Matrix seq_dot_plot(Bmolecule* mol1, Bmolecule* mol2, Bresidue_matrix* simat)
138 {
139 if ( verbose & VERB_FULL )
140 cout << "Dot plot between sequences of sizes " << mol1->nres << " and " << mol2->nres << endl;
141
142 long nres = simat->n;
143 Bstring code1 = simat->c;
144 float* sim_mat = simat->m;
145
146 int k, l, ii, jj;
147
148 Matrix dot_plot(mol1->nres, mol2->nres);
149
150 for ( k=0; k<mol1->nres; k++ ) {
151 ii = code1.index(mol1->seq[k]);
152 for ( l=0; l<mol2->nres; l++ ) {
153 jj = code1.index(mol2->seq[l]);
154 if ( ii > -1 && jj > -1 )
155 dot_plot[l][k] = sim_mat[ii*nres+jj];
156 }
157 }
158
159 return dot_plot;
160 }
161
168 Matrix seq_dot_plot_mov_avg(Matrix dot_plot, int window)
169 {
170 int isize = dot_plot.columns(), jsize = dot_plot.rows();
171
172 if ( window < 1 ) window = 1;
173 if ( window > 1000 ) window = 1000;
174 int i, j, w, start, end;
175 int shortest = isize;
176 if ( shortest > jsize ) shortest = jsize;
177 int low_half = window/2;
178 int up_half = window - low_half - 1;
179
180 Matrix mov_avg(isize, jsize);
181
182 for ( j=0; j<jsize; j++ ) { // Diagonals in upper triangle
183 for ( i=0; i<jsize-j && i<shortest; i++ ) {
184 start = i - low_half;
185 if ( start < 0 ) start = 0;
186 end = i + up_half;
187 if ( end >= shortest ) end = shortest - 1;
188 if ( end >= jsize-j ) end = jsize - j - 1;
189 for ( w=start; w<=end; w++ )
190 mov_avg[i+j][i] += dot_plot[w+j][w];
191 }
192 }
193
194 for ( i=1; i<isize; i++ ) { // Diagonals in lower triangle
195 for ( j=0; j<isize-i && j<shortest; j++ ) {
196 start = j - low_half;
197 if ( start < 0 ) start = 0;
198 end = j + up_half;
199 if ( end >= shortest ) end = shortest - 1;
200 if ( end >= isize-i ) end = isize - i - 1;
201 for ( w=start; w<=end; w++ )
202 mov_avg[j][i+j] += dot_plot[w][i+w];
203 }
204 }
205
206 return mov_avg;
207 }
208
214 int seq_dot_plot_interpret(Matrix dot_plot)
215 {
216 int isize = dot_plot.columns(), jsize = dot_plot.rows();
217
218 int i, j, k;
219 double max;
220 int nmask[4] = {0,0,0,0};
221 vector<char> mask(isize*jsize, 0);
222
223 for ( j=0; j<jsize; j++ ) {
224 max = 0;
225 for ( i=0; i<isize; i++ )
226 if ( max < dot_plot[j][i] )
227 max = dot_plot[j][i];
228 for ( i=0; i<isize; i++ )
229 if ( max == dot_plot[j][i] ) mask[j*isize+i] += 1;
230 }
231
232 for ( i=0; i<isize; i++ ) {
233 max = 0;
234 for ( j=0; j<jsize; j++ )
235 if ( max < dot_plot[j][i] )
236 max = dot_plot[j][i];
237 for ( j=0; j<jsize; j++ )
238 if ( max == dot_plot[j][i] ) mask[j*isize+i] += 1;
239 }
240
241 for ( j=1; j<jsize-1; j++ ) {
242 for ( i=1; i<isize-1; i++ ) {
243 if ( mask[j*isize+i] > 1 ) {
244 if ( mask[(j-1)*isize+i-1] ) mask[j*isize+i] = 3;
245 if ( mask[(j+1)*isize+i+1] ) mask[j*isize+i] = 3;
246 }
247 k = mask[j*isize+i];
248 nmask[k] += 1;
249 }
250 }
251
252 cout << "Dot plot mask sums:" << endl << "n\tCount" << endl;
253 for ( i=0; i<4; i++ )
254 cout << i << tab << nmask[i] << endl;
255 cout << endl;
256
257 return nmask[3];
258 }
259
265 double seq_dot_plot_best_segments(Matrix dot_plot)
266 {
267 int isize = dot_plot.columns(), jsize = dot_plot.rows();
268
269 int i, j, n, nr=1, nc=1, nr1, nc1;
270 double threshold, tmin, tmax, max = 0;
271
272 for ( i=0; i<isize; i++ )
273 for ( j=0; j<jsize; j++ )
274 if ( max < dot_plot[i][j] ) max = dot_plot[i][j];
275
276 threshold = max/2;
277 tmin = 0;
278 tmax = max;
279
280 cout << "Thresh\tUnique\tMulti" << endl;
281 while ( nc+nr>0 || ( threshold - tmin > 1e-3 && tmax - threshold > 1e-3 ) ) {
282 for ( j=nr=nr1=0; j<jsize; j++ ) {
283 for ( i=n=0; i<isize; i++ )
284 if ( dot_plot[j][i] > threshold ) n++;
285 if ( n > 1 ) nr++;
286 else if ( n == 1 ) nr1++;
287 }
288
289 for ( i=nc=nc1=0; i<isize; i++ ) {
290 for ( j=n=0; j<jsize; j++ )
291 if ( dot_plot[j][i] > threshold ) n++;
292 if ( n > 1 ) nc++;
293 else if ( n == 1 ) nc1++;
294 }
295 cout << threshold << tab << nc1+nr1 << tab << nc+nr << endl;
296 if ( nc+nr > 0 ) {
297 tmin = threshold;
298 threshold = (threshold + tmax)/2;
299 } else {
300 tmax = threshold;
301 threshold = (threshold + tmin)/2;
302 }
303 }
304
305 cout << "Threshold: " << threshold << endl;
306 cout << "Unique assignments: " << nc1 << " " << nr1 << endl;
307 cout << "Multiple assignments: " << nc << " " << nr << endl << endl;
308
309 return threshold;
310 }
311
324 int seq_show_segments(Bmolecule* mol1, Bmolecule* mol2, double threshold, Matrix dot_plot)
325 {
326 int isize = dot_plot.columns(), jsize = dot_plot.rows();
327
328 int i, j, notfound;
329
330 for ( i=0; i<isize; i++ ) {
331 cout << mol1->seq[i];
332 notfound = 1;
333 for ( j=0; j<jsize && notfound; j++ ) {
334 if ( dot_plot[j][i] > threshold ) {
335 cout << tab << mol2->seq[j] << tab << j;
336 notfound = 0;
337 }
338 }
339 cout << endl;
340 }
341
342 return 0;
343 }
seq_align.h
Header file for functions to generate and analyze dot plots.
Bmolecule::id
Bstring id
Definition: rwmolecule.h:110
rwresprop.h
Header file for reading residue property files.
seq_util.h
Header file for sequence utilities.
seq_find_best_offset
int seq_find_best_offset(Bmolecule *mol1, Bmolecule *mol2, long &nres, Bresidue_matrix *simat)
Finds the best offset for aligning two sequences without gaps.
Definition: seq_align.cpp:94
seq_dot_plot
Matrix seq_dot_plot(Bmolecule *mol1, Bmolecule *mol2, Bresidue_matrix *simat)
Calculates a dot plot for two sequences.
Definition: seq_align.cpp:137
Matrix::rows
long rows()
Definition: Matrix.h:153
Bmolecule::nres
long nres
Definition: rwmolecule.h:111
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bresidue_matrix::m
float * m
Definition: rwresprop.h:47
seq_dot_plot_best_segments
double seq_dot_plot_best_segments(Matrix dot_plot)
Finding the best segments in a dot plot.
Definition: seq_align.cpp:265
Matrix::columns
long columns()
Definition: Matrix.h:154
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
verbose
int verbose
Matrix3.h
3x3 Matrix manipulation functions
Bmolecule::seq
Bstring seq
Definition: rwmolecule.h:114
linked_list.h
Header file for generalized linked list functions.
seq_pair_align
long seq_pair_align(Bmolecule *mol1, Bmolecule *mol2, double gapopen, double gapextend, Bresidue_matrix *simat)
Aligns two sequences.
Definition: seq_align.cpp:34
rwmolecule.h
Header file for reading reflection files.
seq_dot_plot_mov_avg
Matrix seq_dot_plot_mov_avg(Matrix dot_plot, int window)
Calculates a moving average along the diagonals of a dot plot.
Definition: seq_align.cpp:168
seq_show_segments
int seq_show_segments(Bmolecule *mol1, Bmolecule *mol2, double threshold, Matrix dot_plot)
Finds the best offset for aligning two sequences without gaps.
Definition: seq_align.cpp:324
Bresidue_matrix
Definition: rwresprop.h:44
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
Bresidue_matrix::n
int n
Definition: rwresprop.h:45
Bmolecule
Definition: rwmolecule.h:107
Bresidue_matrix::c
Bstring c
Definition: rwresprop.h:46
Matrix
Definition: Matrix.h:21
Bstring
Definition: Bstring.h:29
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
dynamic_programming.h
Library functions for dynamic programming.
seq_dot_plot_interpret
int seq_dot_plot_interpret(Matrix dot_plot)
Finds the row and column maxima in a dot plot.
Definition: seq_align.cpp:214
Bstring::index
long index(const char c)
Definition: Bstring.h:101
VERB_FULL
#define VERB_FULL
Definition: utilities.h:109
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21