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