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