Next version of JABA
[jabaws.git] / binaries / src / mafft / core / Salignmm.c.fast
1 #include "mltaln.h"
2 #include "dp.h"
3
4 #define OUTGAP0TRY 1
5 #define DEBUG 0
6 #define XXXXXXX    0
7 #define USE_PENALTY_EX  0
8
9 static void OpeningGapCount( float *ogcp, int clus, char **seq, double *eff, int len )
10 {
11         int i, j, gc, gb; 
12         float feff;
13
14         
15         for( i=0; i<len; i++ ) ogcp[i] = 0.0;
16         for( j=0; j<clus; j++ ) 
17         {
18                 feff = (float)eff[j];
19                 gc = 0;
20                 for( i=0; i<len; i++ ) 
21                 {
22                         gb = gc;
23                         gc = ( seq[j][i] == '-' );
24                         {
25                                 if( !gb *  gc ) ogcp[i] += feff;
26                         }
27                 }
28 //              totaleff+= eff[j];
29         }
30 //      for( i=0; i<len; i++ ) 
31 //      {
32 //              fprintf( stderr, "ogcp[%d] = %30.20f\n", i, ogcp[i] );
33 //              ogcp[i] /= totaleff;
34 //      }
35 }
36
37 static void FinalGapCount( float *fgcp, int clus, char **seq, double *eff, int len )
38 {
39         int i, j, gc, gb; 
40         float feff;
41         
42         for( i=0; i<len+1; i++ ) fgcp[i] = 0.0;
43         for( j=0; j<clus; j++ ) 
44         {
45                 feff = (float)eff[j];
46                 gc = ( seq[j][0] == '-' );
47                 for( i=1; i<len+1; i++ ) 
48                 {
49                         gb = gc;
50                         gc = ( seq[j][i] == '-' );
51                         {
52                                 if( gb * !gc ) fgcp[i-1] += feff;
53                         }
54                 }
55 //              totaleff += eff[j];
56 //              fprintf( stderr, "eff[%d] = %30.20f\n", j, eff[j] );
57         }
58 //      for( i=0; i<len; i++ ) 
59 //      {
60 //              fgcp[i] /= totaleff;
61 //              fprintf( stderr, "fgcp[%d] = %30.20f\n", i, fgcp[i] );
62 //      }
63 }
64 static float **impmtx = NULL;
65 float imp_match_out_sc( int i1, int j1 )
66 {
67 //      fprintf( stderr, "imp+match = %f\n", impmtx[i1][j1] * fastathreshold );
68 //      fprintf( stderr, "val = %f\n", impmtx[i1][j1] );
69         return( impmtx[i1][j1] );
70 }
71 static void imp_match_out_vead_gapmap( float *imp, int i1, int lgth2, int *gapmap2 )
72 {
73         int j;
74         float *pt = impmtx[i1];
75         for( j=0; j<lgth2; j++ )
76                 *imp++ += pt[gapmap2[j]];
77 }
78 static void imp_match_out_vead( float *imp, int i1, int lgth2 )
79 {
80         int j;
81         float *pt = impmtx[i1];
82         for( j=0; j<lgth2; j++ )
83                 *imp++ += pt[j];
84 }
85
86 void imp_match_init_strict( float *imp, int clus1, int clus2, int lgth1, int lgth2, char **seq1, char **seq2, double *eff1, double *eff2, LocalHom ***localhom, int forscore )
87 {
88         int dif, i, j, k1, k2, tmpint, start1, start2, end1, end2;
89         static int impalloclen = 0;
90         float effij;
91         double effijx;
92         char *pt, *pt1, *pt2;
93         int allgap;
94         static char *nocount1 = NULL;
95         static char *nocount2 = NULL;
96         LocalHom *tmpptr;
97
98         if( impalloclen < lgth1 + 2 || impalloclen < lgth2 + 2 )
99         {
100                 if( impmtx ) FreeFloatMtx( impmtx );
101                 if( nocount1 ) free( nocount1 );
102                 if( nocount2 ) free( nocount2 );
103                 impalloclen = MAX( lgth1, lgth2 ) + 2;
104                 impmtx = AllocateFloatMtx( impalloclen, impalloclen );
105                 nocount1 = AllocateCharVec( impalloclen );
106                 nocount2 = AllocateCharVec( impalloclen );
107         }
108
109         for( i=0; i<lgth1; i++ )
110         {
111                 for( j=0; j<clus1; j++ )
112                         if( seq1[j][i] == '-' ) break;
113                 if( j != clus1 ) nocount1[i] = 1; 
114                 else                     nocount1[i] = 0;
115         }
116         for( i=0; i<lgth2; i++ )
117         {
118                 for( j=0; j<clus2; j++ )
119                         if( seq2[j][i] == '-' ) break;
120                 if( j != clus2 ) nocount2[i] = 1;
121                 else                     nocount2[i] = 0;
122         }
123
124 #if 0
125 fprintf( stderr, "nocount2 =\n" );
126 for( i = 0; i<impalloclen; i++ )
127 {
128         fprintf( stderr, "nocount2[%d] = %d (%c)\n", i, nocount2[i], seq2[0][i] );
129 }
130 #endif
131
132
133
134 #if 0
135         fprintf( stderr, "eff1 in _init_strict = \n" );
136         for( i=0; i<clus1; i++ )
137                 fprintf( stderr, "eff1[] = %f\n", eff1[i] );
138         for( i=0; i<clus2; i++ )
139                 fprintf( stderr, "eff2[] = %f\n", eff2[i] );
140 #endif
141
142         for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
143                 impmtx[i][j] = 0.0;
144         effijx =  fastathreshold;
145         for( i=0; i<clus1; i++ )
146         {
147                 for( j=0; j<clus2; j++ )
148                 {
149                         effij = (float)( eff1[i] * eff2[j] * effijx );
150                         tmpptr = localhom[i][j];
151                         while( tmpptr )
152                         {
153 //                              fprintf( stderr, "start1 = %d\n", tmpptr->start1 );
154 //                              fprintf( stderr, "end1   = %d\n", tmpptr->end1   );
155 //                              fprintf( stderr, "i = %d, seq1 = \n%s\n", i, seq1[i] );
156 //                              fprintf( stderr, "j = %d, seq2 = \n%s\n", j, seq2[j] );
157                                 pt = seq1[i];
158                                 tmpint = -1;
159                                 while( *pt != 0 )
160                                 {
161                                         if( *pt++ != '-' ) tmpint++;
162                                         if( tmpint == tmpptr->start1 ) break;
163                                 }
164                                 start1 = pt - seq1[i] - 1;
165         
166                                 while( *pt != 0 )
167                                 {
168 //                                      fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, tmpptr->end1, pt-seq1[i] );
169                                         if( *pt++ != '-' ) tmpint++;
170                                         if( tmpint == tmpptr->end1 ) break;
171                                 }
172                                 end1 = pt - seq1[i] - 1;
173         
174                                 pt = seq2[j];
175                                 tmpint = -1;
176                                 while( *pt != 0 )
177                                 {
178                                         if( *pt++ != '-' ) tmpint++;
179                                         if( tmpint == tmpptr->start2 ) break;
180                                 }
181                                 start2 = pt - seq2[j] - 1;
182                                 while( *pt != 0 )
183                                 {
184                                         if( *pt++ != '-' ) tmpint++;
185                                         if( tmpint == tmpptr->end2 ) break;
186                                 }
187                                 end2 = pt - seq2[j] - 1;
188 //                              fprintf( stderr, "start1 = %d (%c), end1 = %d (%c), start2 = %d (%c), end2 = %d (%c)\n", start1, seq1[i][start1], end1, seq1[i][end1], start2, seq2[j][start2], end2, seq2[j][end2] );
189 //                              fprintf( stderr, "step 0\n" );
190                                 if( end1 - start1 != end2 - start2 )
191                                 {
192 //                                      fprintf( stderr, "CHUUI!!, start1 = %d, end1 = %d, start2 = %d, end2 = %d\n", start1, end1, start2, end2 );
193                                 }
194
195 #if 1
196                                 k1 = start1; k2 = start2;
197                                 pt1 = seq1[i] + k1;
198                                 pt2 = seq2[j] + k2;
199                                 while( *pt1 && *pt2 )
200                                 {
201                                         if( *pt1 != '-' && *pt2 != '-' )
202                                         {
203 // ½Å¤ß¤òÆó½Å¤Ë¤«¤±¤Ê¤¤¤è¤¦¤ËÃí°Õ¤·¤Æ²¼¤µ¤¤¡£
204 //                                              impmtx[k1][k2] += tmpptr->wimportance * fastathreshold;
205 //                                              impmtx[k1][k2] += tmpptr->importance * effij;
206                                                 impmtx[k1][k2] += tmpptr->fimportance * effij;
207 //                                              fprintf( stderr, "mark, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
208                                                 k1++; k2++;
209                                                 pt1++; pt2++;
210                                         }
211                                         else if( *pt1 != '-' && *pt2 == '-' )
212                                         {
213 //                                              fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
214                                                 k2++; pt2++;
215                                         }
216                                         else if( *pt1 == '-' && *pt2 != '-' )
217                                         {
218 //                                              fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
219                                                 k1++; pt1++;
220                                         }
221                                         else if( *pt1 == '-' && *pt2 == '-' )
222                                         {
223 //                                              fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
224                                                 k1++; pt1++;
225                                                 k2++; pt2++;
226                                         }
227                                         if( k1 > end1 || k2 > end2 ) break;
228                                 }
229 #else
230                                 while( k1 <= end1 && k2 <= end2 )
231                                 {
232                                         fprintf( stderr, "k1,k2=%d,%d - ", k1, k2 );
233                                         if( !nocount1[k1] && !nocount2[k2] )
234                                         {
235                                                 impmtx[k1][k2] += tmpptr->wimportance * eff1[i] * eff2[j]  * fastathreshold;
236                                                 fprintf( stderr, "marked\n" );
237                                         }
238                                         else
239                                                 fprintf( stderr, "no count\n" );
240                                         k1++; k2++;
241                                 }
242 #endif
243                                 tmpptr = tmpptr->next;
244                         }
245                 }
246         }
247 #if 0
248         fprintf( stderr, "impmtx = \n" );
249         for( k2=0; k2<lgth2; k2++ )
250                 fprintf( stderr, "%6.3f ", (double)k2 );
251         fprintf( stderr, "\n" );
252         for( k1=0; k1<lgth1; k1++ )
253         {
254                 fprintf( stderr, "%d", k1 );
255                 for( k2=0; k2<lgth2; k2++ )
256                         fprintf( stderr, "%2.1f ", impmtx[k1][k2] );
257                 fprintf( stderr, "\n" );
258         }
259 #endif
260 }
261
262 void imp_match_init( float *imp, int clus1, int clus2, int lgth1, int lgth2, char **seq1, char **seq2, double *eff1, double *eff2, LocalHom ***localhom )
263 {
264         int dif, i, j, k1, k2, tmpint, start1, start2, end1, end2;
265         static int impalloclen = 0;
266         char *pt;
267         int allgap;
268         static char *nocount1 = NULL;
269         static char *nocount2 = NULL;
270
271         if( impalloclen < lgth1 + 2 || impalloclen < lgth2 + 2 )
272         {
273                 if( impmtx ) FreeFloatMtx( impmtx );
274                 if( nocount1 ) free( nocount1 );
275                 if( nocount2 ) free( nocount2 );
276                 impalloclen = MAX( lgth1, lgth2 ) + 2;
277                 impmtx = AllocateFloatMtx( impalloclen, impalloclen );
278                 nocount1 = AllocateCharVec( impalloclen );
279                 nocount2 = AllocateCharVec( impalloclen );
280         }
281
282         for( i=0; i<lgth1; i++ )
283         {
284                 for( j=0; j<clus1; j++ )
285                         if( seq1[j][i] == '-' ) break;
286                 if( j != clus1 ) nocount1[i] = 1; 
287                 else                     nocount1[i] = 0;
288         }
289         for( i=0; i<lgth2; i++ )
290         {
291                 for( j=0; j<clus2; j++ )
292                         if( seq2[j][i] == '-' ) break;
293                 if( j != clus2 ) nocount2[i] = 1;
294                 else                     nocount2[i] = 0;
295         }
296
297 #if 0
298 fprintf( stderr, "nocount2 =\n" );
299 for( i = 0; i<impalloclen; i++ )
300 {
301         fprintf( stderr, "nocount2[%d] = %d (%c)\n", i, nocount2[i], seq2[0][i] );
302 }
303 #endif
304
305         for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
306                 impmtx[i][j] = 0;
307         for( i=0; i<clus1; i++ )
308         {
309                 fprintf( stderr, "i = %d, seq1 = %s\n", i, seq1[i] );
310                 for( j=0; j<clus2; j++ )
311                 {
312                         fprintf( stderr, "start1 = %d\n", localhom[i][j]->start1 );
313                         fprintf( stderr, "end1   = %d\n", localhom[i][j]->end1   );
314                         fprintf( stderr, "j = %d, seq2 = %s\n", j, seq2[j] );
315                         pt = seq1[i];
316                         tmpint = -1;
317                         while( *pt != 0 )
318                         {
319                                 if( *pt++ != '-' ) tmpint++;
320                                 if( tmpint == localhom[i][j]->start1 ) break;
321                         }
322                         start1 = pt - seq1[i] - 1;
323
324                         while( *pt != 0 )
325                         {
326 //                              fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, localhom[i][j].end1, pt-seq1[i] );
327                                 if( *pt++ != '-' ) tmpint++;
328                                 if( tmpint == localhom[i][j]->end1 ) break;
329                         }
330                         end1 = pt - seq1[i] - 1;
331
332                         pt = seq2[j];
333                         tmpint = -1;
334                         while( *pt != 0 )
335                         {
336                                 if( *pt++ != '-' ) tmpint++;
337                                 if( tmpint == localhom[i][j]->start2 ) break;
338                         }
339                         start2 = pt - seq2[j] - 1;
340                         while( *pt != 0 )
341                         {
342                                 if( *pt++ != '-' ) tmpint++;
343                                 if( tmpint == localhom[i][j]->end2 ) break;
344                         }
345                         end2 = pt - seq2[j] - 1;
346 //                      fprintf( stderr, "start1 = %d, end1 = %d, start2 = %d, end2 = %d\n", start1, end1, start2, end2 );
347                         k1 = start1;
348                         k2 = start2;
349                         fprintf( stderr, "step 0\n" );
350                         while( k1 <= end1 && k2 <= end2 )
351                         {
352 #if 0
353                                 if( !nocount1[k1] && !nocount2[k2] )
354                                         impmtx[k1][k2] += localhom[i][j].wimportance * eff1[i] * eff2[j];
355                                 k1++; k2++;
356 #else
357                                 if( !nocount1[k1] && !nocount2[k2] )
358                                         impmtx[k1][k2] += localhom[i][j]->wimportance * eff1[i] * eff2[j];
359                                 k1++; k2++;
360 #endif
361                         }
362
363                         dif = ( end1 - start1 ) - ( end2 - start2 );
364                         fprintf( stderr, "dif = %d\n", dif );
365                         if( dif > 0 )
366                         {
367                                 do
368                                 {
369                                         fprintf( stderr, "dif = %d\n", dif );
370                                         k1 = start1;
371                                         k2 = start2 - dif;
372                                         while( k1 <= end1 && k2 <= end2 )
373                                         {
374                                                 if( 0 <= k2 && start2 <= k2 && !nocount1[k1] && !nocount2[k2] )
375                                                         impmtx[k1][k2] = localhom[i][j]->wimportance * eff1[i] * eff2[j];
376                                                 k1++; k2++;
377                                         }
378                                 }
379                                 while( dif-- );
380                         }
381                         else
382                         {
383                                 do
384                                 {
385                                         k1 = start1 + dif;
386                                         k2 = start2;
387                                         while( k1 <= end1 )
388                                         {
389                                                 if( k1 >= 0 && k1 >= start1 && !nocount1[k1] && !nocount2[k2] )
390                                                         impmtx[k1][k2] = localhom[i][j]->wimportance * eff1[i] * eff2[j];
391                                                 k1++; k2++;
392                                         }
393                                 }
394                                 while( dif++ );
395                         }
396                 }
397         }
398 #if 0
399         fprintf( stderr, "impmtx = \n" );
400         for( k2=0; k2<lgth2; k2++ )
401                 fprintf( stderr, "%6.3f ", (double)k2 );
402         fprintf( stderr, "\n" );
403         for( k1=0; k1<lgth1; k1++ )
404         {
405                 fprintf( stderr, "%d", k1 );
406                 for( k2=0; k2<lgth2; k2++ )
407                         fprintf( stderr, "%6.3f ", impmtx[k1][k2] );
408                 fprintf( stderr, "\n" );
409         }
410 #endif
411 }
412
413 static void match_calc( float *match, float **cpmx1, float **cpmx2, int i1, int lgth2, float **floatwork, int **intwork, int initialize )
414 {
415         int j, k, l;
416         float scarr[26];
417         float **cpmxpd = floatwork;
418         int **cpmxpdn = intwork;
419         int count = 0;
420
421         if( initialize )
422         {
423                 for( j=0; j<lgth2; j++ )
424                 {
425                         count = 0;
426                         for( l=0; l<26; l++ )
427                         {
428                                 if( cpmx2[l][j] )
429                                 {
430                                         cpmxpd[count][j] = cpmx2[l][j];
431                                         cpmxpdn[count][j] = l;
432                                         count++;
433                                 }
434                         }
435                         cpmxpdn[count][j] = -1;
436                 }
437         }
438
439         for( l=0; l<26; l++ )
440         {
441                 scarr[l] = 0.0;
442                 for( k=0; k<26; k++ )
443                         scarr[l] += n_dis[k][l] * cpmx1[k][i1];
444         }
445 #if 0 /* ¤³¤ì¤ò»È¤¦¤È¤\ad¤Ïfloatwork¤Î¥¢¥í¥±¡¼¥È¤òµÕ¤Ë¤¹¤ë */
446         {
447                 float *fpt, **fptpt, *fpt2;
448                 int *ipt, **iptpt;
449                 fpt2 = match;
450                 iptpt = cpmxpdn;
451                 fptpt = cpmxpd;
452                 while( lgth2-- )
453                 {
454                         *fpt2 = 0.0;
455                         ipt=*iptpt,fpt=*fptpt;
456                         while( *ipt > -1 )
457                                 *fpt2 += scarr[*ipt++] * *fpt++;
458                         fpt2++,iptpt++,fptpt++;
459                 } 
460         }
461 #else
462         for( j=0; j<lgth2; j++ )
463         {
464                 match[j] = 0.0;
465                 for( k=0; cpmxpdn[k][j]>-1; k++ )
466                         match[j] += scarr[cpmxpdn[k][j]] * cpmxpd[k][j];
467         } 
468 #endif
469 }
470
471 static void Atracking_localhom( float *impwmpt, float *lasthorizontalw, float *lastverticalw, 
472                                                 char **seq1, char **seq2, 
473                         char **mseq1, char **mseq2, 
474                         float **cpmx1, float **cpmx2, 
475                         short **ijp, int icyc, int jcyc )
476 {
477         int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k;
478         char gap[] = "-";
479         float wm;
480         lgth1 = strlen( seq1[0] );
481         lgth2 = strlen( seq2[0] );
482
483 #if 0
484         for( i=0; i<lgth1; i++ ) 
485         {
486                 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
487         }
488 #endif
489  
490         if( outgap == 1 )
491                 ;
492         else
493         {
494                 wm = lastverticalw[0];
495                 for( i=0; i<lgth1; i++ )
496                 {
497                         if( lastverticalw[i] >= wm )
498                         {
499                                 wm = lastverticalw[i];
500                                 iin = i; jin = lgth2-1;
501                                 ijp[lgth1][lgth2] = +( lgth1 - i );
502                         }
503                 }
504                 for( j=0; j<lgth2; j++ )
505                 {
506                         if( lasthorizontalw[j] >= wm )
507                         {
508                                 wm = lasthorizontalw[j];
509                                 iin = lgth1-1; jin = j;
510                                 ijp[lgth1][lgth2] = -( lgth2 - j );
511                         }
512                 }
513         }
514
515     for( i=0; i<lgth1+1; i++ ) 
516     {
517         ijp[i][0] = i + 1;
518     }
519     for( j=0; j<lgth2+1; j++ ) 
520     {
521         ijp[0][j] = -( j + 1 );
522     }
523
524         for( i=0; i<icyc; i++ )
525         {
526                 mseq1[i] += lgth1+lgth2;
527                 *mseq1[i] = 0;
528         }
529         for( j=0; j<jcyc; j++ )
530         {
531                 mseq2[j] += lgth1+lgth2;
532                 *mseq2[j] = 0;
533         }
534         iin = lgth1; jin = lgth2;
535         *impwmpt = 0.0;
536         for( k=0; k<=lgth1+lgth2; k++ ) 
537         {
538                 if( ijp[iin][jin] < 0 ) 
539                 {
540                         ifi = iin-1; jfi = jin+ijp[iin][jin];
541                 }
542                 else if( ijp[iin][jin] > 0 )
543                 {
544                         ifi = iin-ijp[iin][jin]; jfi = jin-1;
545                 }
546                 else
547                 {
548                         ifi = iin-1; jfi = jin-1;
549                 }
550                 l = iin - ifi;
551                 while( --l ) 
552                 {
553                         for( i=0; i<icyc; i++ )
554                                 *--mseq1[i] = seq1[i][ifi+l];
555                         for( j=0; j<jcyc; j++ ) 
556                                 *--mseq2[j] = *gap;
557                         k++;
558                 }
559                 l= jin - jfi;
560                 while( --l )
561                 {
562                         for( i=0; i<icyc; i++ ) 
563                                 *--mseq1[i] = *gap;
564                         for( j=0; j<jcyc; j++ ) 
565                                 *--mseq2[j] = seq2[j][jfi+l];
566                         k++;
567                 }
568                 if( iin == lgth1 || jin == lgth2 )
569                         ;
570                 else
571                 {
572                         *impwmpt += imp_match_out_sc( iin, jin );
573
574 //              fprintf( stderr, "impwm = %f (iin=%d, jin=%d) seq1=%c, seq2=%c\n", *impwmpt, iin, jin, seq1[0][iin], seq2[0][jin] );
575                 }
576                 if( iin <= 0 || jin <= 0 ) break;
577                 for( i=0; i<icyc; i++ ) 
578                         *--mseq1[i] = seq1[i][ifi];
579                 for( j=0; j<jcyc; j++ ) 
580                         *--mseq2[j] = seq2[j][jfi];
581                 k++;
582                 iin = ifi; jin = jfi;
583         }
584 }
585 static void Atracking_localhom_gapmap( float *impwmpt, float *lasthorizontalw, float *lastverticalw, 
586                                                 char **seq1, char **seq2, 
587                         char **mseq1, char **mseq2, 
588                         float **cpmx1, float **cpmx2, 
589                         short **ijp, int icyc, int jcyc,
590                                                 int *gapmap1, int *gapmap2 )
591 {
592         int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k;
593         char gap[] = "-";
594         float wm;
595         lgth1 = strlen( seq1[0] );
596         lgth2 = strlen( seq2[0] );
597
598 #if 0
599         for( i=0; i<lgth1; i++ ) 
600         {
601                 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
602         }
603 #endif
604  
605         if( outgap == 1 )
606                 ;
607         else
608         {
609                 wm = lastverticalw[0];
610                 for( i=0; i<lgth1; i++ )
611                 {
612                         if( lastverticalw[i] >= wm )
613                         {
614                                 wm = lastverticalw[i];
615                                 iin = i; jin = lgth2-1;
616                                 ijp[lgth1][lgth2] = +( lgth1 - i );
617                         }
618                 }
619                 for( j=0; j<lgth2; j++ )
620                 {
621                         if( lasthorizontalw[j] >= wm )
622                         {
623                                 wm = lasthorizontalw[j];
624                                 iin = lgth1-1; jin = j;
625                                 ijp[lgth1][lgth2] = -( lgth2 - j );
626                         }
627                 }
628         }
629
630     for( i=0; i<lgth1+1; i++ ) 
631     {
632         ijp[i][0] = i + 1;
633     }
634     for( j=0; j<lgth2+1; j++ ) 
635     {
636         ijp[0][j] = -( j + 1 );
637     }
638
639         for( i=0; i<icyc; i++ )
640         {
641                 mseq1[i] += lgth1+lgth2;
642                 *mseq1[i] = 0;
643         }
644         for( j=0; j<jcyc; j++ )
645         {
646                 mseq2[j] += lgth1+lgth2;
647                 *mseq2[j] = 0;
648         }
649         iin = lgth1; jin = lgth2;
650         *impwmpt = 0.0;
651         for( k=0; k<=lgth1+lgth2; k++ ) 
652         {
653                 if( ijp[iin][jin] < 0 ) 
654                 {
655                         ifi = iin-1; jfi = jin+ijp[iin][jin];
656                 }
657                 else if( ijp[iin][jin] > 0 )
658                 {
659                         ifi = iin-ijp[iin][jin]; jfi = jin-1;
660                 }
661                 else
662                 {
663                         ifi = iin-1; jfi = jin-1;
664                 }
665                 l = iin - ifi;
666                 while( --l ) 
667                 {
668                         for( i=0; i<icyc; i++ )
669                                 *--mseq1[i] = seq1[i][ifi+l];
670                         for( j=0; j<jcyc; j++ ) 
671                                 *--mseq2[j] = *gap;
672                         k++;
673                 }
674                 l= jin - jfi;
675                 while( --l )
676                 {
677                         for( i=0; i<icyc; i++ ) 
678                                 *--mseq1[i] = *gap;
679                         for( j=0; j<jcyc; j++ ) 
680                                 *--mseq2[j] = seq2[j][jfi+l];
681                         k++;
682                 }
683                 if( iin == lgth1 || jin == lgth2 )
684                         ;
685                 else
686                 {
687                         *impwmpt += imp_match_out_sc( gapmap1[iin], gapmap2[jin] );
688
689 //              fprintf( stderr, "impwm = %f (iin=%d, jin=%d) seq1=%c, seq2=%c\n", *impwmpt, iin, jin, seq1[0][iin], seq2[0][jin] );
690                 }
691                 if( iin <= 0 || jin <= 0 ) break;
692                 for( i=0; i<icyc; i++ ) 
693                         *--mseq1[i] = seq1[i][ifi];
694                 for( j=0; j<jcyc; j++ ) 
695                         *--mseq2[j] = seq2[j][jfi];
696                 k++;
697                 iin = ifi; jin = jfi;
698         }
699 }
700 static float Atracking( float *lasthorizontalw, float *lastverticalw, 
701                                                 char **seq1, char **seq2, 
702                         char **mseq1, char **mseq2, 
703                         float **cpmx1, float **cpmx2, 
704                         short **ijp, int icyc, int jcyc )
705 {
706         int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k;
707         char gap[] = "-";
708         float wm;
709         lgth1 = strlen( seq1[0] );
710         lgth2 = strlen( seq2[0] );
711
712 #if 0
713         for( i=0; i<lgth1; i++ ) 
714         {
715                 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
716         }
717 #endif
718  
719         if( outgap == 1 )
720                 ;
721         else
722         {
723                 wm = lastverticalw[0];
724                 for( i=0; i<lgth1; i++ )
725                 {
726                         if( lastverticalw[i] >= wm )
727                         {
728                                 wm = lastverticalw[i];
729                                 iin = i; jin = lgth2-1;
730                                 ijp[lgth1][lgth2] = +( lgth1 - i );
731                         }
732                 }
733                 for( j=0; j<lgth2; j++ )
734                 {
735                         if( lasthorizontalw[j] >= wm )
736                         {
737                                 wm = lasthorizontalw[j];
738                                 iin = lgth1-1; jin = j;
739                                 ijp[lgth1][lgth2] = -( lgth2 - j );
740                         }
741                 }
742         }
743
744     for( i=0; i<lgth1+1; i++ ) 
745     {
746         ijp[i][0] = i + 1;
747     }
748     for( j=0; j<lgth2+1; j++ ) 
749     {
750         ijp[0][j] = -( j + 1 );
751     }
752
753         for( i=0; i<icyc; i++ )
754         {
755                 mseq1[i] += lgth1+lgth2;
756                 *mseq1[i] = 0;
757         }
758         for( j=0; j<jcyc; j++ )
759         {
760                 mseq2[j] += lgth1+lgth2;
761                 *mseq2[j] = 0;
762         }
763         iin = lgth1; jin = lgth2;
764         for( k=0; k<=lgth1+lgth2; k++ ) 
765         {
766                 if( ijp[iin][jin] < 0 ) 
767                 {
768                         ifi = iin-1; jfi = jin+ijp[iin][jin];
769                 }
770                 else if( ijp[iin][jin] > 0 )
771                 {
772                         ifi = iin-ijp[iin][jin]; jfi = jin-1;
773                 }
774                 else
775                 {
776                         ifi = iin-1; jfi = jin-1;
777                 }
778                 l = iin - ifi;
779                 while( --l ) 
780                 {
781                         for( i=0; i<icyc; i++ )
782                                 *--mseq1[i] = seq1[i][ifi+l];
783                         for( j=0; j<jcyc; j++ ) 
784                                 *--mseq2[j] = *gap;
785                         k++;
786                 }
787                 l= jin - jfi;
788                 while( --l )
789                 {
790                         for( i=0; i<icyc; i++ ) 
791                                 *--mseq1[i] = *gap;
792                         for( j=0; j<jcyc; j++ ) 
793                                 *--mseq2[j] = seq2[j][jfi+l];
794                         k++;
795                 }
796                 if( iin <= 0 || jin <= 0 ) break;
797                 for( i=0; i<icyc; i++ ) 
798                         *--mseq1[i] = seq1[i][ifi];
799                 for( j=0; j<jcyc; j++ ) 
800                         *--mseq2[j] = seq2[j][jfi];
801                 k++;
802                 iin = ifi; jin = jfi;
803         }
804         return( 0.0 );
805 }
806
807 float A__align( char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen, LocalHom ***localhom, float *impmatch )
808 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
809 {
810 //      int k;
811         register int i, j;
812         int lasti;                      /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
813         int lgth1, lgth2;
814         int resultlen;
815         float wm;   /* int ?????? */
816         float g;
817         float *currentw, *previousw;
818 //      float fpenalty = (float)penalty;
819         float fpenalty_ex = (float)penalty_ex;
820 #if 1
821         float *wtmp;
822         short *ijppt;
823         float *mjpt, *prept, *curpt;
824         int *mpjpt;
825 #endif
826         static float mi, *m;
827         static short **ijp;
828         static int mpi, *mp;
829         static float *w1, *w2;
830         static float *match;
831         static float *initverticalw;    /* kufuu sureba iranai */
832         static float *lastverticalw;    /* kufuu sureba iranai */
833         static char **mseq1;
834         static char **mseq2;
835         static char **mseq;
836         static float *ogcp1;
837         static float *ogcp2;
838         static float *fgcp1;
839         static float *fgcp2;
840         static float **cpmx1;
841         static float **cpmx2;
842         static int **intwork;
843         static float **floatwork;
844         static int orlgth1 = 0, orlgth2 = 0;
845         float fpenalty = (float)penalty;
846
847
848 #if 0
849         fprintf( stderr, "eff in SA+++align\n" );
850         for( i=0; i<icyc; i++ ) fprintf( stderr, "eff1[%d] = %f\n", i, eff1[i] );
851 #endif
852         if( orlgth1 == 0 )
853         {
854                 mseq1 = AllocateCharMtx( njob, 0 );
855                 mseq2 = AllocateCharMtx( njob, 0 );
856         }
857
858
859         lgth1 = strlen( seq1[0] );
860         lgth2 = strlen( seq2[0] );
861
862         if( lgth1 > orlgth1 || lgth2 > orlgth2 )
863         {
864                 int ll1, ll2;
865
866                 if( orlgth1 > 0 && orlgth2 > 0 )
867                 {
868                         FreeFloatVec( w1 );
869                         FreeFloatVec( w2 );
870                         FreeFloatVec( match );
871                         FreeFloatVec( initverticalw );
872                         FreeFloatVec( lastverticalw );
873
874                         FreeFloatVec( m );
875                         FreeIntVec( mp );
876
877                         FreeCharMtx( mseq );
878
879                         FreeFloatVec( ogcp1 );
880                         FreeFloatVec( ogcp2 );
881                         FreeFloatVec( fgcp1 );
882                         FreeFloatVec( fgcp2 );
883
884
885                         FreeFloatMtx( cpmx1 );
886                         FreeFloatMtx( cpmx2 );
887
888                         FreeFloatMtx( floatwork );
889                         FreeIntMtx( intwork );
890                 }
891
892                 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
893                 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
894
895 #if DEBUG
896                 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
897 #endif
898
899                 w1 = AllocateFloatVec( ll2+2 );
900                 w2 = AllocateFloatVec( ll2+2 );
901                 match = AllocateFloatVec( ll2+2 );
902
903                 initverticalw = AllocateFloatVec( ll1+2 );
904                 lastverticalw = AllocateFloatVec( ll1+2 );
905
906                 m = AllocateFloatVec( ll2+2 );
907                 mp = AllocateIntVec( ll2+2 );
908
909                 mseq = AllocateCharMtx( njob, ll1+ll2 );
910
911                 ogcp1 = AllocateFloatVec( ll1+2 );
912                 ogcp2 = AllocateFloatVec( ll2+2 );
913                 fgcp1 = AllocateFloatVec( ll1+2 );
914                 fgcp2 = AllocateFloatVec( ll2+2 );
915
916                 cpmx1 = AllocateFloatMtx( 26, ll1+2 );
917                 cpmx2 = AllocateFloatMtx( 26, ll2+2 );
918
919                 floatwork = AllocateFloatMtx( 26, MAX( ll1, ll2 )+2 ); 
920                 intwork = AllocateIntMtx( 26, MAX( ll1, ll2 )+2 ); 
921
922 #if DEBUG
923                 fprintf( stderr, "succeeded\n" );
924 #endif
925
926                 orlgth1 = ll1 - 100;
927                 orlgth2 = ll2 - 100;
928         }
929
930
931         for( i=0; i<icyc; i++ )
932         {
933                 mseq1[i] = mseq[i];
934                 seq1[i][lgth1] = 0;
935         }
936         for( j=0; j<jcyc; j++ )
937         {
938                 mseq2[j] = mseq[icyc+j];
939                 seq2[j][lgth2] = 0;
940         }
941
942
943         if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
944         {
945                 int ll1, ll2;
946
947                 if( commonAlloc1 && commonAlloc2 )
948                 {
949                         FreeShortMtx( commonIP );
950                 }
951
952                 ll1 = MAX( orlgth1, commonAlloc1 );
953                 ll2 = MAX( orlgth2, commonAlloc2 );
954
955 #if DEBUG
956                 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
957 #endif
958
959                 commonIP = AllocateShortMtx( ll1+10, ll2+10 );
960
961 #if DEBUG
962                 fprintf( stderr, "succeeded\n\n" );
963 #endif
964
965                 commonAlloc1 = ll1;
966                 commonAlloc2 = ll2;
967         }
968         ijp = commonIP;
969
970 #if 0
971         {
972                 float t = 0.0;
973                 for( i=0; i<icyc; i++ )
974                         t += eff1[i];
975         fprintf( stderr, "## totaleff = %f\n", t );
976         }
977 #endif
978         cpmx_calc_new( seq1, cpmx1, eff1, lgth1, icyc );
979         cpmx_calc_new( seq2, cpmx2, eff2, lgth2, jcyc );
980
981         OpeningGapCount( ogcp1, icyc, seq1, eff1, lgth1 );
982         OpeningGapCount( ogcp2, jcyc, seq2, eff2, lgth2 );
983         FinalGapCount( fgcp1, icyc, seq1, eff1, lgth1 );
984         FinalGapCount( fgcp2, jcyc, seq2, eff2, lgth2 );
985
986         for( i=0; i<lgth1; i++ ) 
987         {
988                 ogcp1[i] = 0.5 * ( 1.0 - ogcp1[i] ) * fpenalty;
989                 fgcp1[i] = 0.5 * ( 1.0 - fgcp1[i] ) * fpenalty;
990         }
991         for( i=0; i<lgth2; i++ ) 
992         {
993                 ogcp2[i] = 0.5 * ( 1.0 - ogcp2[i] ) * fpenalty;
994                 fgcp2[i] = 0.5 * ( 1.0 - fgcp2[i] ) * fpenalty;
995         }
996 #if 0
997         for( i=0; i<lgth1; i++ ) 
998                 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
999 #endif
1000
1001         currentw = w1;
1002         previousw = w2;
1003
1004         match_calc( initverticalw, cpmx2, cpmx1, 0, lgth1, floatwork, intwork, 1 );
1005
1006         match_calc( currentw, cpmx1, cpmx2, 0, lgth2, floatwork, intwork, 1 );
1007 #if 0 // -> tbfast.c
1008         if( localhom )
1009                 imp_match_calc( currentw, icyc, jcyc, lgth1, lgth2, seq1, seq2, eff1, eff2, localhom, 1, 0 );
1010
1011 #endif
1012
1013         if( outgap == 1 )
1014         {
1015                 for( i=1; i<lgth1+1; i++ )
1016                 {
1017                         initverticalw[i] += ( ogcp1[0] + fgcp1[i-1] ) ;
1018                 }
1019                 for( j=1; j<lgth2+1; j++ )
1020                 {
1021                         currentw[j] += ( ogcp2[0] + fgcp2[j-1] ) ;
1022                 }
1023         }
1024 #if OUTGAP0TRY
1025         else
1026         {
1027                 for( j=1; j<lgth2+1; j++ )
1028                         currentw[j] -= offset * j / 2.0;
1029                 for( i=1; i<lgth1+1; i++ )
1030                         initverticalw[i] -= offset * i / 2.0;
1031         }
1032 #endif
1033
1034         for( j=1; j<lgth2+1; ++j ) 
1035         {
1036                 m[j] = currentw[j-1] + ogcp1[1]; mp[j] = 0;
1037         }
1038
1039         lastverticalw[0] = currentw[lgth2-1];
1040
1041         if( outgap ) lasti = lgth1+1; else lasti = lgth1;
1042
1043 #if XXXXXXX
1044 fprintf( stderr, "currentw = \n" );
1045 for( i=0; i<lgth1+1; i++ )
1046 {
1047         fprintf( stderr, "%5.2f ", currentw[i] );
1048 }
1049 fprintf( stderr, "\n" );
1050 fprintf( stderr, "initverticalw = \n" );
1051 for( i=0; i<lgth2+1; i++ )
1052 {
1053         fprintf( stderr, "%5.2f ", initverticalw[i] );
1054 }
1055 fprintf( stderr, "\n" );
1056 fprintf( stderr, "fcgp\n" );
1057 for( i=0; i<lgth1; i++ ) 
1058         fprintf( stderr, "fgcp1[%d]=%f\n", i, ogcp1[i] );
1059 for( i=0; i<lgth2; i++ ) 
1060         fprintf( stderr, "fgcp2[%d]=%f\n", i, ogcp2[i] );
1061 #endif
1062
1063         for( i=1; i<lasti; i++ )
1064         {
1065                 wtmp = previousw; 
1066                 previousw = currentw;
1067                 currentw = wtmp;
1068
1069                 previousw[0] = initverticalw[i-1];
1070
1071                 match_calc( currentw, cpmx1, cpmx2, i, lgth2, floatwork, intwork, 0 );
1072 #if XXXXXXX
1073 fprintf( stderr, "\n" );
1074 fprintf( stderr, "i=%d\n", i );
1075 fprintf( stderr, "currentw = \n" );
1076 for( j=0; j<lgth2; j++ )
1077 {
1078         fprintf( stderr, "%5.2f ", currentw[j] );
1079 }
1080 fprintf( stderr, "\n" );
1081 #endif
1082                 if( localhom )
1083                 {
1084 //                      fprintf( stderr, "Calling imp_match_calc (o) lgth = %d, i = %d\n", lgth1, i );
1085 #if  0
1086                         imp_match_out_vead( currentw, i, lgth2 );
1087 #else
1088                         imp_match_out_vead( currentw, i, lgth2 );
1089 #endif
1090                 }
1091 #if XXXXXXX
1092 fprintf( stderr, "\n" );
1093 fprintf( stderr, "i=%d\n", i );
1094 fprintf( stderr, "currentw = \n" );
1095 for( j=0; j<lgth2; j++ )
1096 {
1097         fprintf( stderr, "%5.2f ", currentw[j] );
1098 }
1099 fprintf( stderr, "\n" );
1100 #endif
1101                 currentw[0] = initverticalw[i];
1102
1103 #if 1
1104
1105                 mi = previousw[0] + ogcp2[1]; mpi = 0;
1106
1107                 ijppt = ijp[i] + 1;
1108                 mjpt = m + 1;
1109                 prept = previousw;
1110                 curpt = currentw + 1;
1111                 mpjpt = mp + 1;
1112                 for( j=1; j<lgth2+1; j++ )
1113                 {
1114                         wm = *prept;
1115                         *ijppt = 0;
1116
1117 #if 0
1118                         fprintf( stderr, "%5.0f->", wm );
1119 #endif
1120                         g = mi + fgcp2[j-1];
1121 #if 0
1122                         fprintf( stderr, "%5.0f?", g );
1123 #endif
1124                         if( g > wm )
1125                         {
1126                                 wm = g;
1127                                 *ijppt = -( j - mpi );
1128                         }
1129                         g = *prept + ogcp2[j];
1130                         if( g >= mi )
1131                         {
1132                                 mi = g;
1133                                 mpi = j-1;
1134                         }
1135 #if USE_PENALTY_EX
1136                         mi += fpenalty_ex;
1137 #endif
1138
1139                         g = *mjpt + fgcp1[i-1];
1140 #if 0 
1141                         fprintf( stderr, "%5.0f?", g );
1142 #endif
1143                         if( g > wm )
1144                         {
1145                                 wm = g;
1146                                 *ijppt = +( i - *mpjpt );
1147                         }
1148                         g = *prept + ogcp1[i];
1149                         if( g >= *mjpt )
1150                         {
1151                                 *mjpt = g;
1152                                 *mpjpt = i-1;
1153                         }
1154 #if USE_PENALTY_EX
1155                         m[j] += fpenalty_ex;
1156 #endif
1157
1158 #if 0
1159                         fprintf( stderr, "%5.0f ", wm );
1160 #endif
1161                         *curpt += wm;
1162                         ijppt++;
1163                         mjpt++;
1164                         prept++;
1165                         mpjpt++;
1166                         curpt++;
1167                 }
1168                 lastverticalw[i] = currentw[lgth2-1];
1169 #else
1170         floatncpy( previousw, currentw, lgth2+1 );
1171         previousw[0] = initverticalw[i-1];
1172
1173
1174         match_calc( currentw, cpmx1, cpmx2, i, lgth2, floatwork, intwork, 0 );
1175         currentw[0] = initverticalw[i];
1176
1177         mi = previousw[0] + ogcp2[1]; mpi = 0;
1178         for( j=1; j<lgth2+1; j++ )
1179         {
1180             wm = previousw[j-1];
1181             ijp[i][j] = 0;
1182
1183             g = fgcp2[j-1];
1184             if( mi + g > wm )
1185             {
1186                 wm = mi + g; 
1187                 ijp[i][j] = -( j - mpi );
1188             }
1189             g = ogcp2[j];
1190             if( mi <= previousw[j-1] + g )
1191             {
1192                 mi = previousw[j-1] + g;
1193                 mpi = j-1;
1194             }
1195
1196             g = fgcp1[i-1];
1197             if( m[j] + g > wm )
1198             {
1199                 wm = m[j] + g;
1200                 ijp[i][j] = +( i - mp[j] );
1201             }
1202             g = ogcp1[i];
1203             if( m[j] <= previousw[j-1] + g )
1204             {
1205                 m[j] = previousw[j-1] + g ;
1206                 mp[j] = i-1;
1207             }
1208             currentw[j] += wm;
1209         }
1210         lastverticalw[i] = currentw[lgth2-1];
1211
1212 #endif
1213         }
1214
1215 //      fprintf( stderr, "wm = %f\n", wm );
1216
1217 #if OUTGAP0TRY
1218         if( !outgap )
1219         {
1220                 for( j=1; j<lgth2+1; j++ )
1221                         currentw[j] -= offset * ( lgth2 - j ) / 2.0;
1222                 for( i=1; i<lgth1+1; i++ )
1223                         lastverticalw[i] -= offset * ( lgth1 - i  / 2.0);
1224         }
1225 #endif
1226                 
1227         /*
1228         fprintf( stderr, "\n" );
1229         for( i=0; i<icyc; i++ ) fprintf( stderr,"%s\n", seq1[i] );
1230         fprintf( stderr, "#####\n" );
1231         for( j=0; j<jcyc; j++ ) fprintf( stderr,"%s\n", seq2[j] );
1232         fprintf( stderr, "====>" );
1233         for( i=0; i<icyc; i++ ) strcpy( mseq1[i], seq1[i] );
1234         for( j=0; j<jcyc; j++ ) strcpy( mseq2[j], seq2[j] );
1235         */
1236         if( localhom )
1237         {
1238                 Atracking_localhom( impmatch, currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
1239         }
1240         else
1241                 Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
1242
1243 //      fprintf( stderr, "### impmatch = %f\n", *impmatch );
1244
1245         resultlen = strlen( mseq1[0] );
1246         if( alloclen < resultlen || resultlen > N )
1247         {
1248                 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
1249                 ErrorExit( "LENGTH OVER!\n" );
1250         }
1251
1252
1253         for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
1254         for( j=0; j<jcyc; j++ ) strcpy( seq2[j], mseq2[j] );
1255         /*
1256         fprintf( stderr, "\n" );
1257         for( i=0; i<icyc; i++ ) fprintf( stderr, "%s\n", mseq1[i] );
1258         fprintf( stderr, "#####\n" );
1259         for( j=0; j<jcyc; j++ ) fprintf( stderr, "%s\n", mseq2[j] );
1260         */
1261
1262         fprintf( stderr, "wm = %f\n", wm );
1263
1264         return( wm );
1265 }
1266
1267 float A__align_gapmap( char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen, LocalHom ***localhom, float *impmatch, int *gapmap1, int *gapmap2 )
1268 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
1269 {
1270 //      int k;
1271         register int i, j;
1272         int lasti, lastj;                      /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
1273         int lgth1, lgth2;
1274         int resultlen;
1275         float wm;   /* int ?????? */
1276         float g;
1277         float *currentw, *previousw;
1278 //      float fpenalty = (float)penalty;
1279         float fpenalty_ex = (float)penalty_ex;
1280 #if 1
1281         float *wtmp;
1282         short *ijppt;
1283         float *mjpt, *prept, *curpt;
1284         int *mpjpt;
1285 #endif
1286         static float mi, *m;
1287         static short **ijp;
1288         static int mpi, *mp;
1289         static float *w1, *w2;
1290         static float *match;
1291         static float *initverticalw;    /* kufuu sureba iranai */
1292         static float *lastverticalw;    /* kufuu sureba iranai */
1293         static char **mseq1;
1294         static char **mseq2;
1295         static char **mseq;
1296         static float *ogcp1;
1297         static float *ogcp2;
1298         static float *fgcp1;
1299         static float *fgcp2;
1300         static float **cpmx1;
1301         static float **cpmx2;
1302         static int **intwork;
1303         static float **floatwork;
1304         static int orlgth1 = 0, orlgth2 = 0;
1305
1306
1307 #if 0
1308         fprintf( stderr, "eff in SA+++align\n" );
1309         for( i=0; i<icyc; i++ ) fprintf( stderr, "eff1[%d] = %f\n", i, eff1[i] );
1310 #endif
1311         if( orlgth1 == 0 )
1312         {
1313                 mseq1 = AllocateCharMtx( njob, 0 );
1314                 mseq2 = AllocateCharMtx( njob, 0 );
1315         }
1316
1317
1318         lgth1 = strlen( seq1[0] );
1319         lgth2 = strlen( seq2[0] );
1320
1321         if( lgth1 > orlgth1 || lgth2 > orlgth2 )
1322         {
1323                 int ll1, ll2;
1324
1325                 if( orlgth1 > 0 && orlgth2 > 0 )
1326                 {
1327                         FreeFloatVec( w1 );
1328                         FreeFloatVec( w2 );
1329                         FreeFloatVec( match );
1330                         FreeFloatVec( initverticalw );
1331                         FreeFloatVec( lastverticalw );
1332
1333                         FreeFloatVec( m );
1334                         FreeIntVec( mp );
1335
1336                         FreeCharMtx( mseq );
1337
1338                         FreeFloatVec( ogcp1 );
1339                         FreeFloatVec( ogcp2 );
1340                         FreeFloatVec( fgcp1 );
1341                         FreeFloatVec( fgcp2 );
1342
1343
1344                         FreeFloatMtx( cpmx1 );
1345                         FreeFloatMtx( cpmx2 );
1346
1347                         FreeFloatMtx( floatwork );
1348                         FreeIntMtx( intwork );
1349                 }
1350
1351                 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
1352                 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
1353
1354 #if DEBUG
1355                 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
1356 #endif
1357
1358                 w1 = AllocateFloatVec( ll2+2 );
1359                 w2 = AllocateFloatVec( ll2+2 );
1360                 match = AllocateFloatVec( ll2+2 );
1361
1362                 initverticalw = AllocateFloatVec( ll1+2 );
1363                 lastverticalw = AllocateFloatVec( ll1+2 );
1364
1365                 m = AllocateFloatVec( ll2+2 );
1366                 mp = AllocateIntVec( ll2+2 );
1367
1368                 mseq = AllocateCharMtx( njob, ll1+ll2 );
1369
1370                 ogcp1 = AllocateFloatVec( ll1+2 );
1371                 ogcp2 = AllocateFloatVec( ll2+2 );
1372                 fgcp1 = AllocateFloatVec( ll1+2 );
1373                 fgcp2 = AllocateFloatVec( ll2+2 );
1374
1375                 cpmx1 = AllocateFloatMtx( 26, ll1+2 );
1376                 cpmx2 = AllocateFloatMtx( 26, ll2+2 );
1377
1378                 floatwork = AllocateFloatMtx( 26, MAX( ll1, ll2 )+2 ); 
1379                 intwork = AllocateIntMtx( 26, MAX( ll1, ll2 )+2 ); 
1380
1381 #if DEBUG
1382                 fprintf( stderr, "succeeded\n" );
1383 #endif
1384
1385                 orlgth1 = ll1 - 100;
1386                 orlgth2 = ll2 - 100;
1387         }
1388
1389
1390         for( i=0; i<icyc; i++ )
1391         {
1392                 mseq1[i] = mseq[i];
1393                 seq1[i][lgth1] = 0;
1394         }
1395         for( j=0; j<jcyc; j++ )
1396         {
1397                 mseq2[j] = mseq[icyc+j];
1398                 seq2[j][lgth2] = 0;
1399         }
1400
1401
1402         if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
1403         {
1404                 int ll1, ll2;
1405
1406                 if( commonAlloc1 && commonAlloc2 )
1407                 {
1408                         FreeShortMtx( commonIP );
1409                 }
1410
1411                 ll1 = MAX( orlgth1, commonAlloc1 );
1412                 ll2 = MAX( orlgth2, commonAlloc2 );
1413
1414 #if DEBUG
1415                 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
1416 #endif
1417
1418                 commonIP = AllocateShortMtx( ll1+10, ll2+10 );
1419
1420 #if DEBUG
1421                 fprintf( stderr, "succeeded\n\n" );
1422 #endif
1423
1424                 commonAlloc1 = ll1;
1425                 commonAlloc2 = ll2;
1426         }
1427         ijp = commonIP;
1428
1429         cpmx_calc_new( seq1, cpmx1, eff1, strlen( seq1[0] ), icyc );
1430         cpmx_calc_new( seq2, cpmx2, eff2, strlen( seq2[0] ), jcyc );
1431
1432         OpeningGapCount( ogcp1, icyc, seq1, eff1, lgth1  );
1433         OpeningGapCount( ogcp2, jcyc, seq2, eff2, lgth2 );
1434         FinalGapCount( fgcp1, icyc, seq1, eff1, lgth1 );
1435         FinalGapCount( fgcp2, jcyc, seq2, eff2, lgth2 );
1436
1437         for( i=0; i<lgth1; i++ ) 
1438         {
1439                 ogcp1[i] = 0.5 * ( 1.0 - ogcp1[i] );
1440                 fgcp1[i] = 0.5 * ( 1.0 - fgcp1[i] );
1441         }
1442         for( i=0; i<lgth2; i++ ) 
1443         {
1444                 ogcp2[i] = 0.5 * ( 1.0 - ogcp2[i] );
1445                 fgcp2[i] = 0.5 * ( 1.0 - fgcp2[i] );
1446         }
1447 #if 0
1448         for( i=0; i<lgth1; i++ ) 
1449                 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
1450 #endif
1451
1452         currentw = w1;
1453         previousw = w2;
1454
1455
1456         match_calc( initverticalw, cpmx2, cpmx1, 0, lgth1, floatwork, intwork, 1 );
1457
1458
1459         match_calc( currentw, cpmx1, cpmx2, 0, lgth2, floatwork, intwork, 1 );
1460 #if 0 // -> tbfast.c
1461         if( localhom )
1462                 imp_match_calc( currentw, icyc, jcyc, lgth1, lgth2, seq1, seq2, eff1, eff2, localhom, 1, 0 );
1463
1464 #endif
1465
1466         if( outgap == 1 )
1467         {
1468                 for( i=1; i<lgth1+1; i++ )
1469                 {
1470                         initverticalw[i] += ( ogcp1[0] + fgcp1[i-1] ) ;
1471                 }
1472                 for( j=1; j<lgth2+1; j++ )
1473                 {
1474                         currentw[j] += ( ogcp2[0] + fgcp2[j-1] ) ;
1475                 }
1476         }
1477 #if OUTGAP0TRY
1478         else
1479         {
1480                 for( j=1; j<lgth2+1; j++ )
1481                         currentw[j] -= offset * j / 2.0;
1482                 for( i=1; i<lgth1+1; i++ )
1483                         initverticalw[i] -= offset * i / 2.0;
1484         }
1485 #endif
1486
1487         for( j=1; j<lgth2+1; ++j ) 
1488         {
1489                 m[j] = currentw[j-1] + ogcp1[1]; mp[j] = 0;
1490         }
1491
1492         lastverticalw[0] = currentw[lgth2-1];
1493
1494         if( outgap ) lasti = lgth1+1; else lasti = lgth1;
1495
1496 #if XXXXXXX
1497 fprintf( stderr, "currentw = \n" );
1498 for( i=0; i<lgth1+1; i++ )
1499 {
1500         fprintf( stderr, "%5.2f ", currentw[i] );
1501 }
1502 fprintf( stderr, "\n" );
1503 fprintf( stderr, "initverticalw = \n" );
1504 for( i=0; i<lgth2+1; i++ )
1505 {
1506         fprintf( stderr, "%5.2f ", initverticalw[i] );
1507 }
1508 fprintf( stderr, "\n" );
1509 fprintf( stderr, "fcgp\n" );
1510 for( i=0; i<lgth1; i++ ) 
1511         fprintf( stderr, "fgcp1[%d]=%f\n", i, ogcp1[i] );
1512 for( i=0; i<lgth2; i++ ) 
1513         fprintf( stderr, "fgcp2[%d]=%f\n", i, ogcp2[i] );
1514 #endif
1515
1516         for( i=1; i<lasti; i++ )
1517         {
1518                 wtmp = previousw; 
1519                 previousw = currentw;
1520                 currentw = wtmp;
1521
1522                 previousw[0] = initverticalw[i-1];
1523
1524                 match_calc( currentw, cpmx1, cpmx2, i, lgth2, floatwork, intwork, 0 );
1525 #if XXXXXXX
1526 fprintf( stderr, "\n" );
1527 fprintf( stderr, "i=%d\n", i );
1528 fprintf( stderr, "currentw = \n" );
1529 for( j=0; j<lgth2; j++ )
1530 {
1531         fprintf( stderr, "%5.2f ", currentw[j] );
1532 }
1533 fprintf( stderr, "\n" );
1534 #endif
1535                 if( localhom )
1536                 {
1537 //                      fprintf( stderr, "Calling imp_match_calc (o) lgth = %d, i = %d\n", lgth1, i );
1538 #if  0
1539                         imp_match_out_vead( currentw, i, lgth2 );
1540 #else
1541                         imp_match_out_vead_gapmap( currentw, gapmap1[i], lgth2, gapmap2 );
1542 #endif
1543                 }
1544 #if XXXXXXX
1545 fprintf( stderr, "\n" );
1546 fprintf( stderr, "i=%d\n", i );
1547 fprintf( stderr, "currentw = \n" );
1548 for( j=0; j<lgth2; j++ )
1549 {
1550         fprintf( stderr, "%5.2f ", currentw[j] );
1551 }
1552 fprintf( stderr, "\n" );
1553 #endif
1554                 currentw[0] = initverticalw[i];
1555
1556 #if 1
1557
1558                 mi = previousw[0] + ogcp2[1]; mpi = 0;
1559
1560                 ijppt = ijp[i] + 1;
1561                 mjpt = m + 1;
1562                 prept = previousw;
1563                 curpt = currentw + 1;
1564                 mpjpt = mp + 1;
1565                 for( j=1; j<lgth2+1; j++ )
1566                 {
1567                         wm = *prept;
1568                         *ijppt = 0;
1569
1570 #if 0
1571                         fprintf( stderr, "%5.0f->", wm );
1572 #endif
1573                         g = mi + fgcp2[j-1];
1574 #if 0
1575                         fprintf( stderr, "%5.0f?", g );
1576 #endif
1577                         if( g > wm )
1578                         {
1579                                 wm = g;
1580                                 *ijppt = -( j - mpi );
1581                         }
1582                         g = *prept + ogcp2[j];
1583                         if( g >= mi )
1584                         {
1585                                 mi = g;
1586                                 mpi = j-1;
1587                         }
1588 #if USE_PENALTY_EX
1589                         mi += fpenalty_ex;
1590 #endif
1591
1592                         g = *mjpt + fgcp1[i-1];
1593 #if 0 
1594                         fprintf( stderr, "%5.0f?", g );
1595 #endif
1596                         if( g > wm )
1597                         {
1598                                 wm = g;
1599                                 *ijppt = +( i - *mpjpt );
1600                         }
1601                         g = *prept + ogcp1[i];
1602                         if( g >= *mjpt )
1603                         {
1604                                 *mjpt = g;
1605                                 *mpjpt = i-1;
1606                         }
1607 #if USE_PENALTY_EX
1608                         m[j] += fpenalty_ex;
1609 #endif
1610
1611 #if 0
1612                         fprintf( stderr, "%5.0f ", wm );
1613 #endif
1614                         *curpt += wm;
1615                         ijppt++;
1616                         mjpt++;
1617                         prept++;
1618                         mpjpt++;
1619                         curpt++;
1620                 }
1621                 lastverticalw[i] = currentw[lgth2-1];
1622 #else
1623         floatncpy( previousw, currentw, lgth2+1 );
1624         previousw[0] = initverticalw[i-1];
1625
1626
1627         match_calc( currentw, cpmx1, cpmx2, i, lgth2, floatwork, intwork, 0 );
1628         currentw[0] = initverticalw[i];
1629
1630         mi = previousw[0] + ogcp2[1]; mpi = 0;
1631                 lastj = lgth2+1;
1632         for( j=1; j<lastj; j++ )
1633         {
1634             wm = previousw[j-1];
1635             ijp[i][j] = 0;
1636
1637             g = fgcp2[j-1];
1638             if( mi + g > wm )
1639             {
1640                 wm = mi + g; 
1641                 ijp[i][j] = -( j - mpi );
1642             }
1643             g = ogcp2[j];
1644             if( mi <= previousw[j-1] + g )
1645             {
1646                 mi = previousw[j-1] + g;
1647                 mpi = j-1;
1648             }
1649
1650             g = fgcp1[i-1];
1651             if( m[j] + g > wm )
1652             {
1653                 wm = m[j] + g;
1654                 ijp[i][j] = +( i - mp[j] );
1655             }
1656             g = ogcp1[i];
1657             if( m[j] <= previousw[j-1] + g )
1658             {
1659                 m[j] = previousw[j-1] + g ;
1660                 mp[j] = i-1;
1661             }
1662             currentw[j] += wm;
1663         }
1664         lastverticalw[i] = currentw[lgth2-1];
1665
1666 #endif
1667         }
1668
1669 #if OUTGAP0TRY
1670         if( !outgap )
1671         {
1672                 for( j=1; j<lgth2+1; j++ )
1673                         currentw[j] -= offset * ( lgth2 - j ) / 2.0;
1674                 for( i=1; i<lgth1+1; i++ )
1675                         lastverticalw[i] -= offset * ( lgth1 - i  / 2.0);
1676         }
1677 #endif
1678                 
1679         /*
1680         fprintf( stderr, "\n" );
1681         for( i=0; i<icyc; i++ ) fprintf( stderr,"%s\n", seq1[i] );
1682         fprintf( stderr, "#####\n" );
1683         for( j=0; j<jcyc; j++ ) fprintf( stderr,"%s\n", seq2[j] );
1684         fprintf( stderr, "====>" );
1685         for( i=0; i<icyc; i++ ) strcpy( mseq1[i], seq1[i] );
1686         for( j=0; j<jcyc; j++ ) strcpy( mseq2[j], seq2[j] );
1687         */
1688         if( localhom )
1689         {
1690                 Atracking_localhom_gapmap( impmatch, currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc, gapmap1, gapmap2 );
1691         }
1692         else
1693                 Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
1694
1695 //      fprintf( stderr, "### impmatch = %f\n", *impmatch );
1696
1697         resultlen = strlen( mseq1[0] );
1698         if( alloclen < resultlen || resultlen > N )
1699         {
1700                 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
1701                 ErrorExit( "LENGTH OVER!\n" );
1702         }
1703
1704
1705         for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
1706         for( j=0; j<jcyc; j++ ) strcpy( seq2[j], mseq2[j] );
1707         /*
1708         fprintf( stderr, "\n" );
1709         for( i=0; i<icyc; i++ ) fprintf( stderr, "%s\n", mseq1[i] );
1710         fprintf( stderr, "#####\n" );
1711         for( j=0; j<jcyc; j++ ) fprintf( stderr, "%s\n", mseq2[j] );
1712         */
1713
1714         fprintf( stderr, "wm = %f\n", wm );
1715
1716         return( wm );
1717 }
1718
1719 float translate_and_Calign( char **mseq1, char **mseq2, double *effarr1, double *effarr2, int clus1, int clus2, int alloclen )
1720 {
1721     int i;
1722     float wm;
1723     char **result;
1724     char *seq, **aseq;
1725     double *effarr;
1726     int nseq;
1727         int resultlen;
1728
1729     if     ( clus1 == 1 && clus2 != 1 ) 
1730     {
1731         seq = mseq1[0]; aseq = mseq2; effarr = effarr2; nseq = clus2+1;
1732 #if 0
1733                 printf( "effarr in transl... = \n" );
1734                 for( i=0; i<clus2; i++ ) printf( "%f ", effarr2[i] );
1735 #endif
1736     }
1737     else if( clus1 != 1 && clus2 == 1 ) 
1738     {
1739         seq = mseq2[0]; aseq = mseq1; effarr = effarr1; nseq = clus1+1;
1740     }
1741     else ErrorExit( "ERROR in translate_and_Calign" );
1742
1743     result = Calignm1( &wm, aseq, seq, effarr, nseq-2, 0 );
1744
1745         resultlen = strlen( result[0] );
1746         if( alloclen < resultlen || resultlen > N )
1747         {
1748                 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
1749                 ErrorExit( "LENGTH OVER!\n" );
1750         }
1751     for( i=0; i<nseq-1; i++ ) strcpy( aseq[i], result[i] );
1752     strcpy( seq, result[nseq-1] );
1753
1754     return( 0.0 );
1755 }