1 #include "mltaln.h"
2 #include "dp.h"
3
4 #define DEBUG 0
5
match_calc(double * match,double ** cpmx1,double ** cpmx2,int i1,int lgth2,double ** doublework,int ** intwork,int initialize)6 static void match_calc( double *match, double **cpmx1, double **cpmx2, int i1, int lgth2, double **doublework, int **intwork, int initialize )
7 {
8 int j, k, l;
9 // double scarr[26];
10 double **cpmxpd = doublework;
11 int **cpmxpdn = intwork;
12 int count = 0;
13 double *scarr;
14 scarr = calloc( nalphabets, sizeof( double ) );
15
16 if( initialize )
17 {
18 for( j=0; j<lgth2; j++ )
19 {
20 count = 0;
21 for( l=0; l<nalphabets; l++ )
22 {
23 if( cpmx2[l][j] )
24 {
25 cpmxpd[count][j] = cpmx2[l][j];
26 cpmxpdn[count][j] = l;
27 count++;
28 }
29 }
30 cpmxpdn[count][j] = -1;
31 }
32 }
33
34 for( l=0; l<nalphabets; l++ )
35 {
36 scarr[l] = 0.0;
37 for( k=0; k<nalphabets; k++ )
38 scarr[l] += n_dis[k][l] * cpmx1[k][i1];
39 }
40 for( j=0; j<lgth2; j++ )
41 {
42 match[j] = 0;
43 for( k=0; cpmxpdn[k][j] > -1; k++ )
44 match[j] += scarr[cpmxpdn[k][j]] * cpmxpd[k][j];
45 }
46 free( scarr );
47 }
48
Atracking(double * lasthorizontalw,double * lastverticalw,char ** seq1,char ** seq2,char ** mseq1,char ** mseq2,double ** cpmx1,double ** cpmx2,int ** ijp,int icyc,int jcyc)49 static double Atracking( double *lasthorizontalw, double *lastverticalw,
50 char **seq1, char **seq2,
51 char **mseq1, char **mseq2,
52 double **cpmx1, double **cpmx2,
53 int **ijp, int icyc, int jcyc )
54 {
55 int i, j, k, l, iin, jin, ifi, jfi, lgth1, lgth2;
56 // char gap[] = "-";
57 char *gap;
58 double wm;
59 gap = newgapstr;
60 lgth1 = strlen( seq1[0] );
61 lgth2 = strlen( seq2[0] );
62
63 #if DEBUG
64 for( i=0; i<lgth1; i++ )
65 {
66 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
67 }
68 #endif
69
70 if( outgap == 1 )
71 ;
72 else
73 {
74 wm = lastverticalw[0];
75 for( i=0; i<lgth1; i++ )
76 {
77 if( lastverticalw[i] >= wm )
78 {
79 wm = lastverticalw[i];
80 iin = i; jin = lgth2-1;
81 ijp[lgth1][lgth2] = +( lgth1 - i );
82 }
83 }
84 for( j=0; j<lgth2; j++ )
85 {
86 if( lasthorizontalw[j] >= wm )
87 {
88 wm = lasthorizontalw[j];
89 iin = lgth1-1; jin = j;
90 ijp[lgth1][lgth2] = -( lgth2 - j );
91 }
92 }
93 }
94
95 for( i=0; i<lgth1+1; i++ )
96 {
97 ijp[i][0] = i + 1;
98 }
99 for( j=0; j<lgth2+1; j++ )
100 {
101 ijp[0][j] = -( j + 1 );
102 }
103
104 for( i=0; i<icyc; i++ )
105 {
106 mseq1[i] += lgth1+lgth2;
107 *mseq1[i] = 0;
108 }
109 for( j=0; j<jcyc; j++ )
110 {
111 mseq2[j] += lgth1+lgth2;
112 *mseq2[j] = 0;
113 }
114 iin = lgth1; jin = lgth2;
115 for( k=0; k<=lgth1+lgth2; k++ )
116 {
117 if( ijp[iin][jin] < 0 )
118 {
119 ifi = iin-1; jfi = jin+ijp[iin][jin];
120 }
121 else if( ijp[iin][jin] > 0 )
122 {
123 ifi = iin-ijp[iin][jin]; jfi = jin-1;
124 }
125 else
126 {
127 ifi = iin-1; jfi = jin-1;
128 }
129 l = iin - ifi;
130 while( --l )
131 {
132 for( i=0; i<icyc; i++ )
133 *--mseq1[i] = seq1[i][ifi+l];
134 for( j=0; j<jcyc; j++ )
135 *--mseq2[j] = *gap;
136 k++;
137 }
138 l= jin - jfi;
139 while( --l )
140 {
141 for( i=0; i<icyc; i++ )
142 *--mseq1[i] = *gap;
143 for( j=0; j<jcyc; j++ )
144 *--mseq2[j] = seq2[j][jfi+l];
145 k++;
146 }
147 if( iin <= 0 || jin <= 0 ) break;
148 for( i=0; i<icyc; i++ )
149 *--mseq1[i] = seq1[i][ifi];
150 for( j=0; j<jcyc; j++ )
151 *--mseq2[j] = seq2[j][jfi];
152 k++;
153 iin = ifi; jin = jfi;
154 }
155 return( 0.0 );
156 }
157
158
Aalign(char ** seq1,char ** seq2,double * eff1,double * eff2,int icyc,int jcyc,int alloclen)159 double Aalign( char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen )
160 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
161 {
162 register int i, j;
163 int lasti; /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
164 int lgth1, lgth2;
165 int resultlen;
166 double wm = 0.0; /* int ?????? */
167 double g;
168 double x;
169 static TLS double mi, *m;
170 static TLS int **ijp;
171 static TLS int mpi, *mp;
172 static TLS double *currentw;
173 static TLS double *previousw;
174 static TLS double *match;
175 static TLS double *initverticalw; /* kufuu sureba iranai */
176 static TLS double *lastverticalw; /* kufuu sureba iranai */
177 static TLS char **mseq1;
178 static TLS char **mseq2;
179 static TLS char **mseq;
180 static TLS double **cpmx1;
181 static TLS double **cpmx2;
182 static TLS int **intwork;
183 static TLS double **doublework;
184 static TLS int orlgth1 = 0, orlgth2 = 0;
185
186 #if DEBUG
187 fprintf( stderr, "eff in SA+++align\n" );
188 for( i=0; i<icyc; i++ ) fprintf( stderr, "eff1[%d] = %f\n", i, eff1[i] );
189 #endif
190 if( orlgth1 == 0 )
191 {
192 mseq1 = AllocateCharMtx( njob, 1 );
193 mseq2 = AllocateCharMtx( njob, 1 ); /* by J. Thompson */
194 }
195
196 lgth1 = strlen( seq1[0] );
197 lgth2 = strlen( seq2[0] );
198
199 if( lgth1 > orlgth1 || lgth2 > orlgth2 )
200 {
201 int ll1, ll2;
202
203 if( orlgth1 > 0 && orlgth2 > 0 )
204 {
205 FreeFloatVec( currentw );
206 FreeFloatVec( previousw );
207 FreeFloatVec( match );
208 FreeFloatVec( initverticalw );
209 FreeFloatVec( lastverticalw );
210
211 FreeFloatVec( m );
212 FreeIntVec( mp );
213
214 FreeCharMtx( mseq );
215
216 FreeFloatMtx( cpmx1 );
217 FreeFloatMtx( cpmx2 );
218
219 FreeFloatMtx( doublework );
220 FreeIntMtx( intwork );
221 }
222
223 ll1 = MAX( (int)(1.1*lgth1), orlgth1 ) + 100;
224 ll2 = MAX( (int)(1.1*lgth2), orlgth2 ) + 100;
225
226 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
227
228 currentw = AllocateFloatVec( ll2+2 );
229 previousw = AllocateFloatVec( ll2+2 );
230 match = AllocateFloatVec( ll2+2 );
231
232 initverticalw = AllocateFloatVec( ll1+2 );
233 lastverticalw = AllocateFloatVec( ll1+2 );
234
235 m = AllocateFloatVec( ll2+2 );
236 mp = AllocateIntVec( ll2+2 );
237
238 mseq = AllocateCharMtx( njob, ll1+ll2 );
239
240 cpmx1 = AllocateFloatMtx( nalphabets, ll1+2 );
241 cpmx2 = AllocateFloatMtx( nalphabets, ll2+2 );
242
243 doublework = AllocateFloatMtx( nalphabets, MAX( ll1, ll2 )+2 );
244 intwork = AllocateIntMtx( nalphabets, MAX( ll1, ll2 )+2 );
245
246 fprintf( stderr, "succeeded\n" );
247
248 orlgth1 = ll1;
249 orlgth2 = ll2;
250 }
251
252 for( i=0; i<icyc; i++ ) mseq1[i] = mseq[i];
253 for( j=0; j<jcyc; j++ ) mseq2[j] = mseq[icyc+j];
254
255
256 if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
257 {
258 int ll1, ll2;
259
260 if( commonAlloc1 && commonAlloc2 )
261 {
262 FreeIntMtx( commonIP );
263 }
264
265 ll1 = MAX( orlgth1, commonAlloc1 );
266 ll2 = MAX( orlgth2, commonAlloc2 );
267
268 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
269
270 commonIP = AllocateIntMtx( ll1+10, ll2+10 );
271
272 fprintf( stderr, "succeeded\n\n" );
273
274 commonAlloc1 = ll1;
275 commonAlloc2 = ll2;
276 }
277 ijp = commonIP;
278
279 cpmx_calc( seq1, cpmx1, eff1, strlen( seq1[0] ), icyc );
280 cpmx_calc( seq2, cpmx2, eff2, strlen( seq2[0] ), jcyc );
281
282 match_calc( initverticalw, cpmx2, cpmx1, 0, lgth1, doublework, intwork, 1 );
283 match_calc( currentw, cpmx1, cpmx2, 0, lgth2, doublework, intwork, 1 );
284
285 if( outgap == 1 )
286 {
287 for( i=1; i<lgth1+1; i++ )
288 {
289 initverticalw[i] += penalty * 0.5;
290 }
291 for( j=1; j<lgth2+1; j++ )
292 {
293 currentw[j] += penalty * 0.5;
294 }
295 }
296
297 for( j=0; j<lgth2+1; ++j )
298 {
299 m[j] = currentw[j-1] + penalty * 0.5; mp[j] = 0;
300 }
301
302 lastverticalw[0] = currentw[lgth2-1];
303
304 if( outgap ) lasti = lgth1+1; else lasti = lgth1;
305
306 for( i=1; i<lasti; i++ )
307 {
308
309 doublencpy( previousw, currentw, lgth2+1 );
310 previousw[0] = initverticalw[i-1];
311
312 match_calc( currentw, cpmx1, cpmx2, i, lgth2, doublework, intwork, 0 );
313 currentw[0] = initverticalw[i];
314
315 mi = previousw[0] + penalty * 0.5; mpi = 0;
316 for( j=1; j<lgth2+1; j++ )
317 {
318 wm = previousw[j-1];
319 ijp[i][j] = 0;
320
321 g = penalty * 0.5;
322 x = mi + g;
323 if( x > wm )
324 {
325 wm = x;
326 ijp[i][j] = -( j - mpi );
327 }
328 g = penalty * 0.5;
329 x = previousw[j-1] + g;
330 if( mi <= x )
331 {
332 mi = x;
333 mpi = j-1;
334 }
335
336 g = penalty * 0.5;
337 x = m[j] + g;
338 if( x > wm )
339 {
340 wm = x;
341 ijp[i][j] = +( i - mp[j] );
342 }
343 g = penalty * 0.5;
344 x = previousw[j-1] + g;
345 if( m[j] <= x )
346 {
347 m[j] = x;
348 mp[j] = i-1;
349 }
350 currentw[j] += wm;
351 }
352 lastverticalw[i] = currentw[lgth2-1];
353 }
354 /*
355 fprintf( stderr, "\n" );
356 for( i=0; i<icyc; i++ ) fprintf( stderr,"%s\n", seq1[i] );
357 fprintf( stderr, "#####\n" );
358 for( j=0; j<jcyc; j++ ) fprintf( stderr,"%s\n", seq2[j] );
359 fprintf( stderr, "====>" );
360 for( i=0; i<icyc; i++ ) strcpy( mseq1[i], seq1[i] );
361 for( j=0; j<jcyc; j++ ) strcpy( mseq2[j], seq2[j] );
362 */
363 Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
364
365 resultlen = strlen( mseq1[0] );
366 if( alloclen < resultlen || resultlen > N )
367 {
368 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
369 ErrorExit( "LENGTH OVER!\n" );
370 }
371
372 for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
373 for( j=0; j<jcyc; j++ ) strcpy( seq2[j], mseq2[j] );
374 /*
375 fprintf( stderr, "\n" );
376 for( i=0; i<icyc; i++ ) fprintf( stderr, "%s\n", mseq1[i] );
377 fprintf( stderr, "#####\n" );
378 for( j=0; j<jcyc; j++ ) fprintf( stderr, "%s\n", mseq2[j] );
379 */
380 return( wm );
381 }
382