Next version of JABA
[jabaws.git] / binaries / src / mafft / core / Lalign11.c
1 #include "mltaln.h"
2 #include "dp.h"
3
4 #define DEBUG 0
5 #define DEBUG2 0
6 #define XXXXXXX    0
7 #define USE_PENALTY_EX  1
8
9 static int localstop; // 060910
10
11 #if 1
12 static void match_calc( float *match, char **s1, char **s2, int i1, int lgth2 ) 
13 {
14         char *seq2 = s2[0];
15         int *intptr;
16
17         intptr = amino_dis[(int)s1[0][i1]];
18         while( lgth2-- )
19                 *match++ = intptr[(int)*seq2++];
20 }
21 #else
22 static void match_calc( float *match, char **s1, char **s2, int i1, int lgth2 )
23 {
24         int j;
25
26         for( j=0; j<lgth2; j++ )
27                 match[j] = amino_dis[(*s1)[i1]][(*s2)[j]];
28 }
29 #endif
30
31 #if 0
32 static void match_calc_bk( float *match, float **cpmx1, float **cpmx2, int i1, int lgth2, float **floatwork, int **intwork, int initialize )
33 {
34         int j, k, l;
35         float scarr[26];
36         float **cpmxpd = floatwork;
37         int **cpmxpdn = intwork;
38         int count = 0;
39
40         if( initialize )
41         {
42                 for( j=0; j<lgth2; j++ )
43                 {
44                         count = 0;
45                         for( l=0; l<26; l++ )
46                         {
47                                 if( cpmx2[l][j] )
48                                 {
49                                         cpmxpd[count][j] = cpmx2[l][j];
50                                         cpmxpdn[count][j] = l;
51                                         count++;
52                                 }
53                         }
54                         cpmxpdn[count][j] = -1;
55                 }
56         }
57
58         for( l=0; l<26; l++ )
59         {
60                 scarr[l] = 0.0;
61                 for( k=0; k<26; k++ )
62                         scarr[l] += n_dis[k][l] * cpmx1[k][i1];
63         }
64 #if 0 /* ¤³¤ì¤ò»È¤¦¤È¤\ad¤Ïfloatwork¤Î¥¢¥í¥±¡¼¥È¤òµÕ¤Ë¤¹¤ë */
65         {
66                 float *fpt, **fptpt, *fpt2;
67                 int *ipt, **iptpt;
68                 fpt2 = match;
69                 iptpt = cpmxpdn;
70                 fptpt = cpmxpd;
71                 while( lgth2-- )
72                 {
73                         *fpt2 = 0.0;
74                         ipt=*iptpt,fpt=*fptpt;
75                         while( *ipt > -1 )
76                                 *fpt2 += scarr[*ipt++] * *fpt++;
77                         fpt2++,iptpt++,fptpt++;
78                 } 
79         }
80 #else
81         for( j=0; j<lgth2; j++ )
82         {
83                 match[j] = 0.0;
84                 for( k=0; cpmxpdn[k][j]>-1; k++ )
85                         match[j] += scarr[cpmxpdn[k][j]] * cpmxpd[k][j];
86         } 
87 #endif
88 }
89 #endif
90
91 static float Ltracking( float *lasthorizontalw, float *lastverticalw, 
92                                                 char **seq1, char **seq2, 
93                         char **mseq1, char **mseq2, 
94                         int **ijp, int *off1pt, int *off2pt, int endi, int endj )
95 {
96         int i, j, l, iin, jin, lgth1, lgth2, k, limk;
97         int ifi=0, jfi=0; // by D.Mathog, a guess
98         char gap[] = "-";
99         lgth1 = strlen( seq1[0] );
100         lgth2 = strlen( seq2[0] );
101
102 #if 0
103         for( i=0; i<lgth1; i++ ) 
104         {
105                 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
106         }
107 #endif
108  
109     for( i=0; i<lgth1+1; i++ ) 
110     {
111         ijp[i][0] = localstop;
112     }
113     for( j=0; j<lgth2+1; j++ ) 
114     {
115         ijp[0][j] = localstop;
116     }
117
118         mseq1[0] += lgth1+lgth2;
119         *mseq1[0] = 0;
120         mseq2[0] += lgth1+lgth2;
121         *mseq2[0] = 0;
122         iin = endi; jin = endj;
123         limk = lgth1+lgth2;
124         for( k=0; k<=limk; k++ ) 
125         {
126                 if( ijp[iin][jin] < 0 ) 
127                 {
128                         ifi = iin-1; jfi = jin+ijp[iin][jin];
129                 }
130                 else if( ijp[iin][jin] > 0 )
131                 {
132                         ifi = iin-ijp[iin][jin]; jfi = jin-1;
133                 }
134                 else
135                 {
136                         ifi = iin-1; jfi = jin-1;
137                 }
138                 l = iin - ifi;
139                 while( --l ) 
140                 {
141                         *--mseq1[0] = seq1[0][ifi+l];
142                         *--mseq2[0] = *gap;
143                         k++;
144                 }
145                 l= jin - jfi;
146                 while( --l )
147                 {
148                         *--mseq1[0] = *gap;
149                         *--mseq2[0] = seq2[0][jfi+l];
150                         k++;
151                 }
152
153                 if( iin <= 0 || jin <= 0 ) break;
154                 *--mseq1[0] = seq1[0][ifi];
155                 *--mseq2[0] = seq2[0][jfi];
156                 if( ijp[ifi][jfi] == localstop ) break;
157                 k++;
158                 iin = ifi; jin = jfi;
159         }
160         if( ifi == -1 ) *off1pt = 0; else *off1pt = ifi;
161         if( jfi == -1 ) *off2pt = 0; else *off2pt = jfi;
162
163 //      fprintf( stderr, "ifn = %d, jfn = %d\n", ifi, jfi );
164
165
166         return( 0.0 );
167 }
168
169
170 float L__align11( char **seq1, char **seq2, int alloclen, int *off1pt, int *off2pt )
171 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
172 {
173 //      int k;
174         register int i, j;
175         int lasti, lastj;                      /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
176         int lgth1, lgth2;
177         int resultlen;
178         float wm = 0.0;   /* int ?????? */
179         float g;
180         float *currentw, *previousw;
181 #if 1
182         float *wtmp;
183         int *ijppt;
184         float *mjpt, *prept, *curpt;
185         int *mpjpt;
186 #endif
187         static float mi, *m;
188         static int **ijp;
189         static int mpi, *mp;
190         static float *w1, *w2;
191         static float *match;
192         static float *initverticalw;    /* kufuu sureba iranai */
193         static float *lastverticalw;    /* kufuu sureba iranai */
194         static char **mseq1;
195         static char **mseq2;
196         static char **mseq;
197 //      static int **intwork;
198 //      static float **floatwork;
199         static int orlgth1 = 0, orlgth2 = 0;
200         float maxwm;
201         int endali = 0, endalj = 0; // by D.Mathog, a guess
202 //      int endali, endalj;
203         float localthr = -offset;
204         float localthr2 = -offset;
205 //      float localthr = 100;
206 //      float localthr2 = 100;
207         float fpenalty = (float)penalty;
208         float fpenalty_ex = (float)penalty_ex;
209
210
211         if( orlgth1 == 0 )
212         {
213                 mseq1 = AllocateCharMtx( njob, 0 );
214                 mseq2 = AllocateCharMtx( njob, 0 );
215         }
216
217
218         lgth1 = strlen( seq1[0] );
219         lgth2 = strlen( seq2[0] );
220
221         if( lgth1 > orlgth1 || lgth2 > orlgth2 )
222         {
223                 int ll1, ll2;
224
225                 if( orlgth1 > 0 && orlgth2 > 0 )
226                 {
227                         FreeFloatVec( w1 );
228                         FreeFloatVec( w2 );
229                         FreeFloatVec( match );
230                         FreeFloatVec( initverticalw );
231                         FreeFloatVec( lastverticalw );
232
233                         FreeFloatVec( m );
234                         FreeIntVec( mp );
235
236                         FreeCharMtx( mseq );
237
238
239
240 //                      FreeFloatMtx( floatwork );
241 //                      FreeIntMtx( intwork );
242                 }
243
244                 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
245                 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
246
247 #if DEBUG
248                 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
249 #endif
250
251                 w1 = AllocateFloatVec( ll2+2 );
252                 w2 = AllocateFloatVec( ll2+2 );
253                 match = AllocateFloatVec( ll2+2 );
254
255                 initverticalw = AllocateFloatVec( ll1+2 );
256                 lastverticalw = AllocateFloatVec( ll1+2 );
257
258                 m = AllocateFloatVec( ll2+2 );
259                 mp = AllocateIntVec( ll2+2 );
260
261                 mseq = AllocateCharMtx( njob, ll1+ll2 );
262
263
264 //              floatwork = AllocateFloatMtx( 26, MAX( ll1, ll2 )+2 ); 
265 //              intwork = AllocateIntMtx( 26, MAX( ll1, ll2 )+2 ); 
266
267 #if DEBUG
268                 fprintf( stderr, "succeeded\n" );
269 #endif
270
271                 orlgth1 = ll1 - 100;
272                 orlgth2 = ll2 - 100;
273         }
274
275
276         mseq1[0] = mseq[0];
277         mseq2[0] = mseq[1];
278
279
280         if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
281         {
282                 int ll1, ll2;
283
284                 if( commonAlloc1 && commonAlloc2 )
285                 {
286                         FreeIntMtx( commonIP );
287                 }
288
289                 ll1 = MAX( orlgth1, commonAlloc1 );
290                 ll2 = MAX( orlgth2, commonAlloc2 );
291
292 #if DEBUG
293                 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
294 #endif
295
296                 commonIP = AllocateIntMtx( ll1+10, ll2+10 );
297
298 #if DEBUG
299                 fprintf( stderr, "succeeded\n\n" );
300 #endif
301
302                 commonAlloc1 = ll1;
303                 commonAlloc2 = ll2;
304         }
305         ijp = commonIP;
306
307
308 #if 0
309         for( i=0; i<lgth1; i++ ) 
310                 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
311 #endif
312
313         currentw = w1;
314         previousw = w2;
315
316         match_calc( initverticalw, seq2, seq1, 0, lgth1 );
317
318         match_calc( currentw, seq1, seq2, 0, lgth2 );
319
320
321         lasti = lgth2+1;
322         for( j=1; j<lasti; ++j ) 
323         {
324                 m[j] = currentw[j-1]; mp[j] = 0;
325 #if 0
326                 if( m[j] < localthr ) m[j] = localthr2;
327 #endif
328         }
329
330         lastverticalw[0] = currentw[lgth2-1];
331
332         lasti = lgth1+1;
333
334 #if 0
335 fprintf( stderr, "currentw = \n" );
336 for( i=0; i<lgth1+1; i++ )
337 {
338         fprintf( stderr, "%5.2f ", currentw[i] );
339 }
340 fprintf( stderr, "\n" );
341 fprintf( stderr, "initverticalw = \n" );
342 for( i=0; i<lgth2+1; i++ )
343 {
344         fprintf( stderr, "%5.2f ", initverticalw[i] );
345 }
346 fprintf( stderr, "\n" );
347 #endif
348 #if DEBUG2
349         fprintf( stderr, "\n" );
350         fprintf( stderr, "       " );
351         for( j=0; j<lgth2; j++ )
352                 fprintf( stderr, "%c     ", seq2[0][j] );
353         fprintf( stderr, "\n" );
354 #endif
355
356         localstop = lgth1+lgth2+1;
357         maxwm = -999999999.9;
358 #if DEBUG2
359         fprintf( stderr, "\n" );
360         fprintf( stderr, "%c   ", seq1[0][0] );
361
362         for( j=0; j<lgth2+1; j++ )
363                 fprintf( stderr, "%5.0f ", currentw[j] );
364         fprintf( stderr, "\n" );
365 #endif
366
367         for( i=1; i<lasti; i++ )
368         {
369                 wtmp = previousw; 
370                 previousw = currentw;
371                 currentw = wtmp;
372
373                 previousw[0] = initverticalw[i-1];
374
375                 match_calc( currentw, seq1, seq2, i, lgth2 );
376 #if DEBUG2
377                 fprintf( stderr, "%c   ", seq1[0][i] );
378                 fprintf( stderr, "%5.0f ", currentw[0] );
379 #endif
380
381 #if XXXXXXX
382 fprintf( stderr, "\n" );
383 fprintf( stderr, "i=%d\n", i );
384 fprintf( stderr, "currentw = \n" );
385 for( j=0; j<lgth2; j++ )
386 {
387         fprintf( stderr, "%5.2f ", currentw[j] );
388 }
389 fprintf( stderr, "\n" );
390 #endif
391 #if XXXXXXX
392 fprintf( stderr, "\n" );
393 fprintf( stderr, "i=%d\n", i );
394 fprintf( stderr, "currentw = \n" );
395 for( j=0; j<lgth2; j++ )
396 {
397         fprintf( stderr, "%5.2f ", currentw[j] );
398 }
399 fprintf( stderr, "\n" );
400 #endif
401                 currentw[0] = initverticalw[i];
402
403                 mi = previousw[0]; mpi = 0;
404
405 #if 0
406                 if( mi < localthr ) mi = localthr2;
407 #endif
408
409                 ijppt = ijp[i] + 1;
410                 mjpt = m + 1;
411                 prept = previousw;
412                 curpt = currentw + 1;
413                 mpjpt = mp + 1;
414                 lastj = lgth2+1;
415                 for( j=1; j<lastj; j++ )
416                 {
417                         wm = *prept;
418                         *ijppt = 0;
419
420 #if 0
421                         fprintf( stderr, "%5.0f->", wm );
422 #endif
423 #if 0
424                         fprintf( stderr, "%5.0f?", g );
425 #endif
426                         if( (g=mi+fpenalty) > wm )
427                         {
428                                 wm = g;
429                                 *ijppt = -( j - mpi );
430                         }
431                         if( *prept > mi )
432                         {
433                                 mi = *prept;
434                                 mpi = j-1;
435                         }
436
437 #if USE_PENALTY_EX
438                         mi += fpenalty_ex;
439 #endif
440
441 #if 0 
442                         fprintf( stderr, "%5.0f?", g );
443 #endif
444                         if( (g=*mjpt+fpenalty) > wm )
445                         {
446                                 wm = g;
447                                 *ijppt = +( i - *mpjpt );
448                         }
449                         if( *prept > *mjpt )
450                         {
451                                 *mjpt = *prept;
452                                 *mpjpt = i-1;
453                         }
454 #if USE_PENALTY_EX
455                         *mjpt += fpenalty_ex;
456 #endif
457
458                         if( maxwm < wm )
459                         {
460                                 maxwm = wm;
461                                 endali = i;
462                                 endalj = j;
463                         }
464 #if 1
465                         if( wm < localthr )
466                         {
467 //                              fprintf( stderr, "stop i=%d, j=%d, curpt=%f\n", i, j, *curpt );
468                                 *ijppt = localstop;
469                                 wm = localthr2;
470                         }
471 #endif
472 #if 0
473                         fprintf( stderr, "%5.0f ", *curpt );
474 #endif
475 #if DEBUG2
476                         fprintf( stderr, "%5.0f ", wm );
477 //                      fprintf( stderr, "%c-%c *ijppt = %d, localstop = %d\n", seq1[0][i], seq2[0][j], *ijppt, localstop );
478 #endif
479
480                         *curpt++ += wm;
481                         ijppt++;
482                         mjpt++;
483                         prept++;
484                         mpjpt++;
485                 }
486 #if DEBUG2
487                 fprintf( stderr, "\n" );
488 #endif
489
490                 lastverticalw[i] = currentw[lgth2-1];
491         }
492
493
494 #if 0
495         fprintf( stderr, "maxwm = %f\n", maxwm );
496         fprintf( stderr, "endali = %d\n", endali );
497         fprintf( stderr, "endalj = %d\n", endalj );
498 #endif
499
500         if( ijp[endali][endalj] == localstop )
501         {
502                 strcpy( seq1[0], "" );
503                 strcpy( seq2[0], "" );
504                 *off1pt = *off2pt = 0;
505                 return( 0.0 );
506         }
507                 
508         Ltracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, ijp, off1pt, off2pt, endali, endalj );
509
510
511         resultlen = strlen( mseq1[0] );
512         if( alloclen < resultlen || resultlen > N )
513         {
514                 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
515                 ErrorExit( "LENGTH OVER!\n" );
516         }
517
518
519         strcpy( seq1[0], mseq1[0] );
520         strcpy( seq2[0], mseq2[0] );
521
522 #if 0
523         fprintf( stderr, "wm=%f\n", wm );
524         fprintf( stderr, ">\n%s\n", mseq1[0] );
525         fprintf( stderr, ">\n%s\n", mseq2[0] );
526
527         fprintf( stderr, "maxwm = %f\n", maxwm );
528         fprintf( stderr, "   wm = %f\n",    wm );
529 #endif
530
531         return( maxwm );
532 }
533