Next version of JABA
[jabaws.git] / binaries / src / mafft / core / Halignmm.c.juyo
1 #include "mltaln.h"
2 #include "dp.h"
3
4 #define MACHIGAI 0
5 #define OUTGAP0TRY 1
6 #define DEBUG 0
7 #define XXXXXXX    0
8 #define USE_PENALTY_EX  0
9 #define FASTMATCHCALC 1
10
11
12 static float countnocountmatch( Gappat *pat1, Gappat *pat2, int r )
13 {
14 //      return( 0.0 );
15         Gappat *pat1bk = pat1;
16         Gappat *pat2bk = pat2;
17         float val = 0.0;
18         // pat1[][0] ha total gap.
19         for( pat1=pat1bk+1; pat1->len != 0; pat1++ )
20         {
21                 if( r ) fprintf( stderr, "b %d-%d, len=%d,%d\n", pat1-pat1bk, pat2-pat2bk, pat1->len, pat2->len );
22                 for( pat2=pat2bk+1; pat2->len != 0; pat2++ )
23                 {
24                         if( pat1->len == pat2->len )
25                         {
26                                 if( r ) fprintf( stderr, " x%d-%d, len=%d,%d\n", pat1-pat1bk, pat2-pat2bk, pat1->len, pat2->len );
27                                 val += pat1->freq * pat2->freq;
28                                 if( r ) fprintf( stderr, "y %d-%d, val = %f\n", pat1-pat1bk, pat2-pat2bk,val );
29                                 if( r ) fprintf( stderr, "z tsugi, %d-%d, len=%d,%d\n", pat1-pat1bk+1, pat2-pat2bk+1, (pat1+1)->len, (pat2+1)->len );
30                         }
31                         if( r ) fprintf( stderr, "a %d-%d, len=%d,%d\n", pat1-pat1bk, pat2-pat2bk, pat1->len, pat2->len );
32                 } 
33         }
34 //      fprintf( stderr, "nocountmatch=%f\n", val );
35         return( val );
36 }
37
38 static float countnocountx( Gappat *pat1, float diaf1, Gappat *pat2, int offset1, int r )
39 {
40 //      return( 0.0 );
41         float gclose;
42         float gmatch;
43         Gappat *pat1bk = pat1;
44         Gappat *pat2bk = pat2;
45
46         gmatch = 0.0;
47         for( pat2=pat2bk+1; pat2->len != 0; pat2++ ) // excl. len=0
48         {
49                 if( pat2->len == offset1 )
50                 {
51                         gmatch = diaf1 * pat2->freq;
52 //                      if( r ) fprintf( stderr, "match0!!, len=%d, gmatch=%f * %f\n", pat2->len, diaf1, pat2->freq );
53                 }
54         }
55         for( pat1=pat1bk+1; pat1->len != 0; pat1++ ) // excl. len=0
56         {
57                 for( pat2=pat2bk+1; pat2->len != 0; pat2++ ) // excl. len=0
58                 {
59                         if( pat1->len + offset1 == pat2->len )
60                         {
61                                 gmatch += pat1->freq * pat2->freq;
62 //                              if( r ) fprintf( stderr, "match1!!, len=%d, gmatch=%f * %f\n", pat2->len, pat1->freq, pat2->freq );
63                         }
64                 }
65         }
66         return( gmatch );
67 }
68
69 static float countnocount( Gappat *pat1, Gappat *pat2, int offset1, int offset2 ) //osoi
70 {
71 //      return( 0.0 );
72         Gappat *pat1bk = pat1;
73         Gappat *pat2bk = pat2;
74         float val = 0.0;
75         // pat1[][0] ha total gap.
76         for( pat1=pat1bk+1; pat1->len != -1; pat1++ )
77         {
78                 for( pat2=pat2bk+1; pat2->len != -1; pat2++ )
79                 {
80                         if( pat1->len+offset1 == pat2->len+offset2 )
81                         {
82                                 val += pat1->freq * pat2->freq;
83                         }
84                 } 
85         }
86 //      fprintf( stderr, "nocount=%f\n", val );
87         return( val );
88 }
89
90
91 static float **impmtx = NULL;
92 #if 1 // tditeration to naiveHscore_imp de tsukawareru.
93 float imp_match_out_scH( int i1, int j1 )
94 {
95 //      fprintf( stderr, "imp+match = %f\n", impmtx[i1][j1] * fastathreshold );
96 //      fprintf( stderr, "val = %f\n", impmtx[i1][j1] );
97         return( impmtx[i1][j1] );
98 }
99 #endif
100
101 static void imp_match_out_veadH( float *imp, int i1, int lgth2 )
102 {
103 #if FASTMATCHCALC 
104         float *pt = impmtx[i1];
105         while( lgth2-- )
106                 *imp++ += *pt++;
107 #else
108         int j;
109         float *pt = impmtx[i1];
110         for( j=0; j<lgth2; j++ )
111                 *imp++ += pt[j];
112 #endif
113 }
114 static void imp_match_out_vead_tateH( float *imp, int j1, int lgth1 )
115 {
116         int i;
117         for( i=0; i<lgth1; i++ )
118                 *imp++ += impmtx[i][j1];
119 }
120
121 #if 1 // tbfast.c kara yobareru.
122 void imp_match_init_strictH( float *imp, int clus1, int clus2, int lgth1, int lgth2, char **seq1, char **seq2, double *eff1, double *eff2, LocalHom ***localhom, int forscore )
123 {
124         int i, j, k1, k2, tmpint, start1, start2, end1, end2;
125         static int impalloclen = 0;
126         float effij;
127         double effijx;
128         char *pt, *pt1, *pt2;
129         static char *nocount1 = NULL;
130         static char *nocount2 = NULL;
131         LocalHom *tmpptr;
132
133         if( impalloclen < lgth1 + 2 || impalloclen < lgth2 + 2 )
134         {
135                 if( impmtx ) FreeFloatMtx( impmtx );
136                 if( nocount1 ) free( nocount1 );
137                 if( nocount2 ) free( nocount2 );
138                 impalloclen = MAX( lgth1, lgth2 ) + 2;
139                 impmtx = AllocateFloatMtx( impalloclen, impalloclen );
140                 nocount1 = AllocateCharVec( impalloclen );
141                 nocount2 = AllocateCharVec( impalloclen );
142         }
143
144         for( i=0; i<lgth1; i++ )
145         {
146                 for( j=0; j<clus1; j++ )
147                         if( seq1[j][i] == '-' ) break;
148                 if( j != clus1 ) nocount1[i] = 1; 
149                 else                     nocount1[i] = 0;
150         }
151         for( i=0; i<lgth2; i++ )
152         {
153                 for( j=0; j<clus2; j++ )
154                         if( seq2[j][i] == '-' ) break;
155                 if( j != clus2 ) nocount2[i] = 1;
156                 else                     nocount2[i] = 0;
157         }
158
159 #if 0
160 fprintf( stderr, "nocount2 =\n" );
161 for( i = 0; i<impalloclen; i++ )
162 {
163         fprintf( stderr, "nocount2[%d] = %d (%c)\n", i, nocount2[i], seq2[0][i] );
164 }
165 #endif
166
167
168
169 #if 0
170         fprintf( stderr, "eff1 in _init_strict = \n" );
171         for( i=0; i<clus1; i++ )
172                 fprintf( stderr, "eff1[] = %f\n", eff1[i] );
173         for( i=0; i<clus2; i++ )
174                 fprintf( stderr, "eff2[] = %f\n", eff2[i] );
175 #endif
176
177         for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
178                 impmtx[i][j] = 0.0;
179         effijx =  fastathreshold;
180         for( i=0; i<clus1; i++ )
181         {
182                 for( j=0; j<clus2; j++ )
183                 {
184                         effij = (float)( eff1[i] * eff2[j] * effijx );
185                         tmpptr = localhom[i][j];
186                         while( tmpptr )
187                         {
188 //                              fprintf( stderr, "start1 = %d\n", tmpptr->start1 );
189 //                              fprintf( stderr, "end1   = %d\n", tmpptr->end1   );
190 //                              fprintf( stderr, "i = %d, seq1 = \n%s\n", i, seq1[i] );
191 //                              fprintf( stderr, "j = %d, seq2 = \n%s\n", j, seq2[j] );
192                                 pt = seq1[i];
193                                 tmpint = -1;
194                                 while( *pt != 0 )
195                                 {
196                                         if( *pt++ != '-' ) tmpint++;
197                                         if( tmpint == tmpptr->start1 ) break;
198                                 }
199                                 start1 = pt - seq1[i] - 1;
200         
201                                 if( tmpptr->start1 == tmpptr->end1 ) end1 = start1;
202                                 else
203                                 {
204 #if MACHIGAI
205                                         while( *pt != 0 )
206                                         {
207 //                                              fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, tmpptr->end1, pt-seq1[i] );
208                                                 if( tmpint == tmpptr->end1 ) break;
209                                                 if( *pt++ != '-' ) tmpint++;
210                                         }
211                                         end1 = pt - seq1[i] - 0;
212 #else
213                                         while( *pt != 0 )
214                                         {
215 //                                              fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, tmpptr->end1, pt-seq1[i] );
216                                                 if( *pt++ != '-' ) tmpint++;
217                                                 if( tmpint == tmpptr->end1 ) break;
218                                         }
219                                         end1 = pt - seq1[i] - 1;
220 #endif
221                                 }
222         
223                                 pt = seq2[j];
224                                 tmpint = -1;
225                                 while( *pt != 0 )
226                                 {
227                                         if( *pt++ != '-' ) tmpint++;
228                                         if( tmpint == tmpptr->start2 ) break;
229                                 }
230                                 start2 = pt - seq2[j] - 1;
231                                 if( tmpptr->start2 == tmpptr->end2 ) end2 = start2;
232                                 else
233                                 {
234 #if MACHIGAI
235                                         while( *pt != 0 )
236                                         {
237                                                 if( tmpint == tmpptr->end2 ) break;
238                                                 if( *pt++ != '-' ) tmpint++;
239                                         }
240                                         end2 = pt - seq2[j] - 0;
241 #else
242                                         while( *pt != 0 )
243                                         {
244                                                 if( *pt++ != '-' ) tmpint++;
245                                                 if( tmpint == tmpptr->end2 ) break;
246                                         }
247                                         end2 = pt - seq2[j] - 1;
248 #endif
249                                 }
250 //                              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] );
251 //                              fprintf( stderr, "step 0\n" );
252                                 if( end1 - start1 != end2 - start2 )
253                                 {
254 //                                      fprintf( stderr, "CHUUI!!, start1 = %d, end1 = %d, start2 = %d, end2 = %d\n", start1, end1, start2, end2 );
255                                 }
256
257 #if 1
258                                 k1 = start1; k2 = start2;
259                                 pt1 = seq1[i] + k1;
260                                 pt2 = seq2[j] + k2;
261                                 while( *pt1 && *pt2 )
262                                 {
263                                         if( *pt1 != '-' && *pt2 != '-' )
264                                         {
265 // Â½Ã…¤ß¤òÆó½Å¤Ë¤«¤±¤Ê¤¤¤è¤¦¤ËÃí°Õ¤·¤Æ²¼¤µ¤¤¡£
266 //                                              impmtx[k1][k2] += tmpptr->wimportance * fastathreshold;
267 //                                              impmtx[k1][k2] += tmpptr->importance * effij;
268                                                 impmtx[k1][k2] += tmpptr->fimportance * effij;
269 //                                              fprintf( stderr, "#### impmtx[k1][k2] = %f, tmpptr->fimportance=%f, effij=%f\n", impmtx[k1][k2], tmpptr->fimportance, effij );
270 //                                              fprintf( stderr, "mark, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
271 //                                              fprintf( stderr, "%d (%c) - %d (%c)  - %f\n", k1, *pt1, k2, *pt2, tmpptr->fimportance * effij );
272                                                 k1++; k2++;
273                                                 pt1++; pt2++;
274                                         }
275                                         else if( *pt1 != '-' && *pt2 == '-' )
276                                         {
277 //                                              fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
278                                                 k2++; pt2++;
279                                         }
280                                         else if( *pt1 == '-' && *pt2 != '-' )
281                                         {
282 //                                              fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
283                                                 k1++; pt1++;
284                                         }
285                                         else if( *pt1 == '-' && *pt2 == '-' )
286                                         {
287 //                                              fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
288                                                 k1++; pt1++;
289                                                 k2++; pt2++;
290                                         }
291                                         if( k1 > end1 || k2 > end2 ) break;
292                                 }
293 #else
294                                 while( k1 <= end1 && k2 <= end2 )
295                                 {
296                                         fprintf( stderr, "k1,k2=%d,%d - ", k1, k2 );
297                                         if( !nocount1[k1] && !nocount2[k2] )
298                                         {
299                                                 impmtx[k1][k2] += tmpptr->wimportance * eff1[i] * eff2[j]  * fastathreshold;
300                                                 fprintf( stderr, "marked\n" );
301                                         }
302                                         else
303                                                 fprintf( stderr, "no count\n" );
304                                         k1++; k2++;
305                                 }
306 #endif
307                                 tmpptr = tmpptr->next;
308                         }
309                 }
310         }
311 #if 0
312         if( clus1 == 1 && clus2 == 6 )
313         {
314                 fprintf( stderr, "\n" );
315                 fprintf( stderr, "seq1[0] =  %s\n", seq1[0] );
316                 fprintf( stderr, "seq2[0] =  %s\n", seq2[0] );
317                 fprintf( stderr, "impmtx = \n" );
318                 for( k2=0; k2<lgth2; k2++ )
319                         fprintf( stderr, "%6.3f ", (double)k2 );
320                 fprintf( stderr, "\n" );
321                 for( k1=0; k1<lgth1; k1++ )
322                 {
323                         fprintf( stderr, "%d ", k1 );
324                         for( k2=0; k2<3; k2++ )
325                                 fprintf( stderr, "%2.1f ", impmtx[k1][k2] );
326                         fprintf( stderr, "\n" );
327                 }
328                 exit( 1 );
329         }
330 #endif
331 }
332 #endif
333
334
335 static void match_calc( float *match, float **cpmx1, float **cpmx2, int i1, int lgth2, float **floatwork, int **intwork, int initialize )
336 {
337 #if FASTMATCHCALC
338         int j, l;
339         float scarr[26];
340         float **cpmxpd = floatwork;
341         int **cpmxpdn = intwork;
342         float *matchpt, *cpmxpdpt, **cpmxpdptpt;
343         int *cpmxpdnpt, **cpmxpdnptpt;
344         if( initialize )
345         {
346                 int count = 0;
347                 for( j=0; j<lgth2; j++ )
348                 {
349                         count = 0;
350                         for( l=0; l<26; l++ )
351                         {
352                                 if( cpmx2[l][j] )
353                                 {
354                                         cpmxpd[j][count] = cpmx2[l][j];
355                                         cpmxpdn[j][count] = l;
356                                         count++;
357                                 }
358                         }
359                         cpmxpdn[j][count] = -1;
360                 }
361         }
362
363         {
364                 for( l=0; l<26; l++ )
365                 {
366                         scarr[l] = 0.0;
367                         for( j=0; j<26; j++ )
368                                 scarr[l] += n_dis[j][l] * cpmx1[j][i1];
369                 }
370                 matchpt = match;
371                 cpmxpdnptpt = cpmxpdn;
372                 cpmxpdptpt = cpmxpd;
373                 while( lgth2-- )
374                 {
375                         *matchpt = 0.0;
376                         cpmxpdnpt = *cpmxpdnptpt++;
377                         cpmxpdpt = *cpmxpdptpt++;
378                         while( *cpmxpdnpt>-1 )
379                                 *matchpt += scarr[*cpmxpdnpt++] * *cpmxpdpt++;
380                         matchpt++;
381                 } 
382         }
383 #else
384         int j, k, l;
385         float scarr[26];
386         float **cpmxpd = floatwork;
387         int **cpmxpdn = intwork;
388 // simple
389         if( initialize )
390         {
391                 int count = 0;
392                 for( j=0; j<lgth2; j++ )
393                 {
394                         count = 0;
395                         for( l=0; l<26; l++ )
396                         {
397                                 if( cpmx2[l][j] )
398                                 {
399                                         cpmxpd[count][j] = cpmx2[l][j];
400                                         cpmxpdn[count][j] = l;
401                                         count++;
402                                 }
403                         }
404                         cpmxpdn[count][j] = -1;
405                 }
406         }
407         for( l=0; l<26; l++ )
408         {
409                 scarr[l] = 0.0;
410                 for( k=0; k<26; k++ )
411                         scarr[l] += n_dis[k][l] * cpmx1[k][i1];
412         }
413         for( j=0; j<lgth2; j++ )
414         {
415                 match[j] = 0.0;
416                 for( k=0; cpmxpdn[k][j]>-1; k++ )
417                         match[j] += scarr[cpmxpdn[k][j]] * cpmxpd[k][j];
418         } 
419 #endif
420 }
421
422 static void Atracking_localhom( float *impwmpt, float *lasthorizontalw, float *lastverticalw, 
423                                                 char **seq1, char **seq2, 
424                         char **mseq1, char **mseq2, 
425                         float **cpmx1, float **cpmx2, 
426                         short **ijp, int icyc, int jcyc )
427 {
428         int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k;
429         float wm;
430         char *gaptable1, *gt1bk;
431         char *gaptable2, *gt2bk;
432         lgth1 = strlen( seq1[0] );
433         lgth2 = strlen( seq2[0] );
434         gt1bk = AllocateCharVec( lgth1+lgth2+1 );
435         gt2bk = AllocateCharVec( lgth1+lgth2+1 );
436
437 #if 0
438         for( i=0; i<lgth1; i++ ) 
439         {
440                 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
441         }
442 #endif
443  
444         if( outgap == 1 )
445                 ;
446         else
447         {
448                 wm = lastverticalw[0];
449                 for( i=0; i<lgth1; i++ )
450                 {
451                         if( lastverticalw[i] >= wm )
452                         {
453                                 wm = lastverticalw[i];
454                                 iin = i; jin = lgth2-1;
455                                 ijp[lgth1][lgth2] = +( lgth1 - i );
456                         }
457                 }
458                 for( j=0; j<lgth2; j++ )
459                 {
460                         if( lasthorizontalw[j] >= wm )
461                         {
462                                 wm = lasthorizontalw[j];
463                                 iin = lgth1-1; jin = j;
464                                 ijp[lgth1][lgth2] = -( lgth2 - j );
465                         }
466                 }
467         }
468
469     for( i=0; i<lgth1+1; i++ ) 
470     {
471         ijp[i][0] = i + 1;
472     }
473     for( j=0; j<lgth2+1; j++ ) 
474     {
475         ijp[0][j] = -( j + 1 );
476     }
477
478         gaptable1 = gt1bk + lgth1+lgth2;
479         *gaptable1 = 0;
480         gaptable2 = gt2bk + lgth1+lgth2;
481         *gaptable2 = 0;
482
483         iin = lgth1; jin = lgth2;
484         *impwmpt = 0.0;
485         for( k=0; k<=lgth1+lgth2; k++ ) 
486         {
487                 if( ijp[iin][jin] < 0 ) 
488                 {
489                         ifi = iin-1; jfi = jin+ijp[iin][jin];
490                 }
491                 else if( ijp[iin][jin] > 0 )
492                 {
493                         ifi = iin-ijp[iin][jin]; jfi = jin-1;
494                 }
495                 else
496                 {
497                         ifi = iin-1; jfi = jin-1;
498                 }
499                 l = iin - ifi;
500                 while( --l ) 
501                 {
502                         *--gaptable1 = 'o';
503                         *--gaptable2 = '-';
504                         k++;
505                 }
506                 l= jin - jfi;
507                 while( --l )
508                 {
509                         *--gaptable1 = '-';
510                         *--gaptable2 = 'o';
511                         k++;
512                 }
513                 if( iin == lgth1 || jin == lgth2 )
514                         ;
515                 else
516                 {
517                         *impwmpt += imp_match_out_scH( iin, jin );
518
519 //              fprintf( stderr, "impwm = %f (iin=%d, jin=%d) seq1=%c, seq2=%c\n", *impwmpt, iin, jin, seq1[0][iin], seq2[0][jin] );
520                 }
521                 if( iin <= 0 || jin <= 0 ) break;
522                 *--gaptable1 = 'o';
523                 *--gaptable2 = 'o';
524                 k++;
525                 iin = ifi; jin = jfi;
526         }
527
528         for( i=0; i<icyc; i++ ) gapireru( mseq1[i], seq1[i], gaptable1 );
529         for( j=0; j<jcyc; j++ ) gapireru( mseq2[j], seq2[j], gaptable2 );
530
531         free( gt1bk );
532         free( gt2bk );
533 }
534
535 static float Atracking( float *lasthorizontalw, float *lastverticalw, 
536                                                 char **seq1, char **seq2, 
537                         char **mseq1, char **mseq2, 
538                         float **cpmx1, float **cpmx2, 
539                         short **ijp, int icyc, int jcyc )
540 {
541         int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k;
542         float wm;
543         char *gaptable1, *gt1bk;
544         char *gaptable2, *gt2bk;
545         lgth1 = strlen( seq1[0] );
546         lgth2 = strlen( seq2[0] );
547
548         gt1bk = AllocateCharVec( lgth1+lgth2+1 );
549         gt2bk = AllocateCharVec( lgth1+lgth2+1 );
550
551 #if 0
552         for( i=0; i<lgth1; i++ ) 
553         {
554                 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
555         }
556 #endif
557  
558         if( outgap == 1 )
559                 ;
560         else
561         {
562                 wm = lastverticalw[0];
563                 for( i=0; i<lgth1; i++ )
564                 {
565                         if( lastverticalw[i] >= wm )
566                         {
567                                 wm = lastverticalw[i];
568                                 iin = i; jin = lgth2-1;
569                                 ijp[lgth1][lgth2] = +( lgth1 - i );
570                         }
571                 }
572                 for( j=0; j<lgth2; j++ )
573                 {
574                         if( lasthorizontalw[j] >= wm )
575                         {
576                                 wm = lasthorizontalw[j];
577                                 iin = lgth1-1; jin = j;
578                                 ijp[lgth1][lgth2] = -( lgth2 - j );
579                         }
580                 }
581         }
582
583     for( i=0; i<lgth1+1; i++ ) 
584     {
585         ijp[i][0] = i + 1;
586     }
587     for( j=0; j<lgth2+1; j++ ) 
588     {
589         ijp[0][j] = -( j + 1 );
590     }
591
592         gaptable1 = gt1bk + lgth1+lgth2;
593         *gaptable1 = 0;
594         gaptable2 = gt2bk + lgth1+lgth2;
595         *gaptable2 = 0;
596
597         iin = lgth1; jin = lgth2;
598         for( k=0; k<=lgth1+lgth2; k++ ) 
599         {
600                 if( ijp[iin][jin] < 0 ) 
601                 {
602                         ifi = iin-1; jfi = jin+ijp[iin][jin];
603                 }
604                 else if( ijp[iin][jin] > 0 )
605                 {
606                         ifi = iin-ijp[iin][jin]; jfi = jin-1;
607                 }
608                 else
609                 {
610                         ifi = iin-1; jfi = jin-1;
611                 }
612                 l = iin - ifi;
613                 while( --l ) 
614                 {
615                         *--gaptable1 = 'o';
616                         *--gaptable2 = '-';
617                         k++;
618                 }
619                 l= jin - jfi;
620                 while( --l )
621                 {
622                         *--gaptable1 = '-';
623                         *--gaptable2 = 'o';
624                         k++;
625                 }
626                 if( iin <= 0 || jin <= 0 ) break;
627                 *--gaptable1 = 'o';
628                 *--gaptable2 = 'o';
629                 k++;
630                 iin = ifi; jin = jfi;
631         }
632
633         for( i=0; i<icyc; i++ ) gapireru( mseq1[i], seq1[i], gaptable1 );
634         for( j=0; j<jcyc; j++ ) gapireru( mseq2[j], seq2[j], gaptable2 );
635
636         free( gt1bk );
637         free( gt2bk );
638
639         return( 0.0 );
640 }
641
642 float H__align( char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen, LocalHom ***localhom, float *impmatch, char *sgap1, char *sgap2, char *egap1, char *egap2 )
643 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
644 {
645 //      int k;
646         register int i, j;
647         int lasti, lastj;      /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
648         int lgth1, lgth2;
649         int resultlen;
650         float wm = 0.0;   /* int ?????? */
651         float g;
652         float *currentw, *previousw;
653 //      float fpenalty = (float)penalty;
654 #if USE_PENALTY_EX
655         float fpenalty_ex = (float)penalty_ex;
656 #endif
657 #if 1
658         float *wtmp;
659         short *ijppt;
660         float *mjpt, *prept, *curpt;
661         int *mpjpt;
662 #endif
663         static float mi, *m;
664         static short **ijp;
665         static int mpi, *mp;
666         static float *w1, *w2;
667         static float *match;
668         static float *initverticalw;    /* kufuu sureba iranai */
669         static float *lastverticalw;    /* kufuu sureba iranai */
670         static char **mseq1;
671         static char **mseq2;
672         static char **mseq;
673         static Gappat **gappat1;
674         static Gappat **gappat2;
675         static float *digf1;
676         static float *digf2;
677         static float *diaf1;
678         static float *diaf2;
679         static float *gapz1;
680         static float *gapz2;
681         static float *gapf1;
682         static float *gapf2;
683         static float *ogcp1g;
684         static float *ogcp2g;
685         static float *fgcp1g;
686         static float *fgcp2g;
687         static float *ogcp1;
688         static float *ogcp2;
689         static float *fgcp1;
690         static float *fgcp2;
691         static float **cpmx1;
692         static float **cpmx2;
693         static int **intwork;
694         static float **floatwork;
695         static int orlgth1 = 0, orlgth2 = 0;
696         float fpenalty = (float)penalty;
697         float tmppenal;
698         float cumpenal;
699         float *fgcp2pt;
700         float *ogcp2pt;
701         float fgcp1va;
702         float ogcp1va;
703
704
705
706 #if 0
707         fprintf( stderr, "####  eff in SA+++align\n" );
708         fprintf( stderr, "####  seq1[0] = %s\n", seq1[0] );
709         fprintf( stderr, "####  strlen( seq1[0] ) = %d\n", strlen( seq1[0] ) );
710         for( i=0; i<icyc; i++ ) fprintf( stderr, "eff1[%d] = %f\n", i, eff1[i] );
711 #endif
712         if( orlgth1 == 0 )
713         {
714                 mseq1 = AllocateCharMtx( njob, 0 );
715                 mseq2 = AllocateCharMtx( njob, 0 );
716         }
717
718
719         lgth1 = strlen( seq1[0] );
720         lgth2 = strlen( seq2[0] );
721 #if 0
722         if( lgth1 == 0 || lgth2 == 0 )
723         {
724                 fprintf( stderr, "WARNING (Aalignmm): lgth1=%d, lgth2=%d\n", lgth1, lgth2 );
725         }
726 #endif
727
728         if( lgth1 > orlgth1 || lgth2 > orlgth2 )
729         {
730                 int ll1, ll2;
731
732                 if( orlgth1 > 0 && orlgth2 > 0 )
733                 {
734                         FreeFloatVec( w1 );
735                         FreeFloatVec( w2 );
736                         FreeFloatVec( match );
737                         FreeFloatVec( initverticalw );
738                         FreeFloatVec( lastverticalw );
739
740                         FreeFloatVec( m );
741                         FreeIntVec( mp );
742
743                         FreeCharMtx( mseq );
744
745                         free( gappat1 );
746                         free( gappat2 );
747                         FreeFloatVec( digf1 );
748                         FreeFloatVec( digf2 );
749                         FreeFloatVec( diaf1 );
750                         FreeFloatVec( diaf2 );
751                         FreeFloatVec( gapz1 );
752                         FreeFloatVec( gapz2 );
753                         FreeFloatVec( gapf1 );
754                         FreeFloatVec( gapf2 );
755                         FreeFloatVec( ogcp1 );
756                         FreeFloatVec( ogcp2 );
757                         FreeFloatVec( fgcp1 );
758                         FreeFloatVec( fgcp2 );
759                         FreeFloatVec( ogcp1g );
760                         FreeFloatVec( ogcp2g );
761                         FreeFloatVec( fgcp1g );
762                         FreeFloatVec( fgcp2g );
763
764
765                         FreeFloatMtx( cpmx1 );
766                         FreeFloatMtx( cpmx2 );
767
768                         FreeFloatMtx( floatwork );
769                         FreeIntMtx( intwork );
770                 }
771
772                 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
773                 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
774
775 #if DEBUG
776                 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
777 #endif
778
779                 w1 = AllocateFloatVec( ll2+2 );
780                 w2 = AllocateFloatVec( ll2+2 );
781                 match = AllocateFloatVec( ll2+2 );
782
783                 initverticalw = AllocateFloatVec( ll1+2 );
784                 lastverticalw = AllocateFloatVec( ll1+2 );
785
786                 m = AllocateFloatVec( ll2+2 );
787                 mp = AllocateIntVec( ll2+2 );
788
789                 mseq = AllocateCharMtx( njob, ll1+ll2 );
790
791                 gappat1 = calloc( ll1+2, sizeof( Gappat *) );
792                 gappat2 = calloc( ll2+2, sizeof( Gappat *) );
793                 digf1 = AllocateFloatVec( ll1+2 );
794                 digf2 = AllocateFloatVec( ll2+2 );
795                 diaf1 = AllocateFloatVec( ll1+2 );
796                 diaf2 = AllocateFloatVec( ll2+2 );
797                 gapz1 = AllocateFloatVec( ll1+2 );
798                 gapz2 = AllocateFloatVec( ll2+2 );
799                 gapf1 = AllocateFloatVec( ll1+2 );
800                 gapf2 = AllocateFloatVec( ll2+2 );
801                 ogcp1 = AllocateFloatVec( ll1+2 );
802                 ogcp2 = AllocateFloatVec( ll2+2 );
803                 fgcp1 = AllocateFloatVec( ll1+2 );
804                 fgcp2 = AllocateFloatVec( ll2+2 );
805                 ogcp1g = AllocateFloatVec( ll1+2 );
806                 ogcp2g = AllocateFloatVec( ll2+2 );
807                 fgcp1g = AllocateFloatVec( ll1+2 );
808                 fgcp2g = AllocateFloatVec( ll2+2 );
809
810                 cpmx1 = AllocateFloatMtx( 26, ll1+2 );
811                 cpmx2 = AllocateFloatMtx( 26, ll2+2 );
812
813 #if FASTMATCHCALC
814                 floatwork = AllocateFloatMtx( MAX( ll1, ll2 )+2, 26 ); 
815                 intwork = AllocateIntMtx( MAX( ll1, ll2 )+2, 27 ); 
816 #else
817                 floatwork = AllocateFloatMtx( 26, MAX( ll1, ll2 )+2 ); 
818                 intwork = AllocateIntMtx( 26, MAX( ll1, ll2 )+2 ); 
819 #endif
820
821 #if DEBUG
822                 fprintf( stderr, "succeeded\n" );
823 #endif
824
825                 orlgth1 = ll1 - 100;
826                 orlgth2 = ll2 - 100;
827         }
828
829
830         for( i=0; i<icyc; i++ )
831         {
832                 mseq1[i] = mseq[i];
833                 seq1[i][lgth1] = 0;
834         }
835         for( j=0; j<jcyc; j++ )
836         {
837                 mseq2[j] = mseq[icyc+j];
838                 seq2[j][lgth2] = 0;
839         }
840
841
842         if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
843         {
844                 int ll1, ll2;
845
846                 if( commonAlloc1 && commonAlloc2 )
847                 {
848                         FreeShortMtx( commonIP );
849                 }
850
851                 ll1 = MAX( orlgth1, commonAlloc1 );
852                 ll2 = MAX( orlgth2, commonAlloc2 );
853
854 #if DEBUG
855                 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
856 #endif
857
858                 commonIP = AllocateShortMtx( ll1+10, ll2+10 );
859
860 #if DEBUG
861                 fprintf( stderr, "succeeded\n\n" );
862 #endif
863
864                 commonAlloc1 = ll1;
865                 commonAlloc2 = ll2;
866         }
867         ijp = commonIP;
868
869 #if 0
870         {
871                 float t = 0.0;
872                 for( i=0; i<icyc; i++ )
873                         t += eff1[i];
874         fprintf( stderr, "## totaleff = %f\n", t );
875         }
876 #endif
877
878         cpmx_calc_new( seq1, cpmx1, eff1, lgth1, icyc );
879         cpmx_calc_new( seq2, cpmx2, eff2, lgth2, jcyc );
880
881         if( sgap1 )
882         {
883                 new_OpeningGapCount_zure( ogcp1g, icyc, seq1, eff1, lgth1, sgap1, egap1 );
884                 new_OpeningGapCount_zure( ogcp2g, jcyc, seq2, eff2, lgth2, sgap2, egap1 );
885                 new_FinalGapCount_zure( fgcp1g, icyc, seq1, eff1, lgth1, sgap1, egap1 );
886                 new_FinalGapCount_zure( fgcp2g, jcyc, seq2, eff2, lgth2, sgap1, egap2 );
887                 getdigapfreq_part( digf1, icyc, seq1, eff1, lgth1, sgap1, egap1 ); // sgap1 ha iranai ?
888                 getdigapfreq_part( digf2, jcyc, seq2, eff2, lgth2, sgap2, egap2 ); // sgap1 ha iranai ?
889                 getdiaminofreq_part( diaf1, icyc, seq1, eff1, lgth1, sgap1, egap1 ); // sgap1 ha iranai ?
890                 getdiaminofreq_part( diaf2, jcyc, seq2, eff2, lgth2, sgap1, egap2 ); // sgap1 ha iranai ?
891                 getgapfreq( gapf1, icyc, seq1, eff1, lgth1 ); // atode
892                 getgapfreq( gapf2, jcyc, seq2, eff2, lgth2 ); // atode
893                 getgapfreq_zure( gapz1, icyc, seq1, eff1, lgth1 ); // atode
894                 getgapfreq_zure( gapz2, jcyc, seq2, eff2, lgth2 ); // atode
895         }
896         else
897         {
898                 st_OpeningGapCount( ogcp1g, icyc, seq1, eff1, lgth1 );
899                 st_OpeningGapCount( ogcp2g, jcyc, seq2, eff2, lgth2 );
900                 st_FinalGapCount_zure( fgcp1g, icyc, seq1, eff1, lgth1 );
901                 st_FinalGapCount_zure( fgcp2g, jcyc, seq2, eff2, lgth2 );
902                 st_getGapPattern( gappat1, icyc, seq1, eff1, lgth1 );
903                 st_getGapPattern( gappat2, jcyc, seq2, eff2, lgth2 );
904                 getdigapfreq_st( digf1, icyc, seq1, eff1, lgth1 );
905                 getdigapfreq_st( digf2, jcyc, seq2, eff2, lgth2 );
906                 getdiaminofreq_x( diaf1, icyc, seq1, eff1, lgth1 );
907                 getdiaminofreq_x( diaf2, jcyc, seq2, eff2, lgth2 );
908                 getgapfreq( gapf1, icyc, seq1, eff1, lgth1 );
909                 getgapfreq( gapf2, jcyc, seq2, eff2, lgth2 );
910                 getgapfreq_zure( gapz1, icyc, seq1, eff1, lgth1 );
911                 getgapfreq_zure( gapz2, jcyc, seq2, eff2, lgth2 );
912         }
913
914 #if 1
915         for( i=0; i<lgth1+1; i++ ) 
916                 if( gappat1[i] )
917                 {
918                         for( j=1; gappat1[i][j].len != -1; j++ )
919                         fprintf( stderr, "i=%d, j=%d, gap end, freq=%f, len=%d\n", i, j, gappat1[i][j].freq, gappat1[i][j].len );
920                 }
921 //      exit( 1 );
922 #endif
923
924         currentw = w1;
925         previousw = w2;
926
927         match_calc( initverticalw, cpmx2, cpmx1, 0, lgth1, floatwork, intwork, 1 );
928         if( localhom )
929                 imp_match_out_vead_tateH( initverticalw, 0, lgth1 ); // 060306
930
931         match_calc( currentw, cpmx1, cpmx2, 0, lgth2, floatwork, intwork, 1 );
932         if( localhom )
933                 imp_match_out_veadH( currentw, 0, lgth2 ); // 060306
934
935
936 #if 0 // -> tbfast.c
937         if( localhom )
938                 imp_match_calc( currentw, icyc, jcyc, lgth1, lgth2, seq1, seq2, eff1, eff2, localhom, 1, 0 );
939
940 #endif
941
942         if( outgap == 1 )
943         {
944                 g = diaf1[0] * gappat2[0][0].freq * fpenalty;
945 //              g = ogcp1g[0] * ( 1.0-ogcp2g[0] ) * fpenalty * 0.5;
946 //              fprintf( stderr, "init-match penal1=%f, %c-%c, diaf1[0] = %f\n", g/fpenalty, seq1[0][0], seq2[0][0], diaf1[0] );
947                 initverticalw[0] += g;
948                 currentw[0] += g;
949
950                 g = diaf2[0] * gappat1[0][0].freq * fpenalty;
951 //              g = ogcp2g[0] * ( 1.0-ogcp1g[0] ) * fpenalty * 0.5;
952 //              fprintf( stderr, "init-match penal2=%f, %c-%c, diaf2[0] = %f\n", g/fpenalty, seq1[0][0], seq2[0][0], diaf2[0] );
953                 initverticalw[0] += g;
954                 currentw[0] += g;
955
956                 g = ( (gappat1[0][0].freq) * (gappat2[0][0].freq) - countnocountmatch( gappat1[0], gappat2[0], i==j ) ) * fpenalty;
957 //              fprintf( stderr, "init-match penal2=%f, %c-%c\n", g/fpenalty, seq1[0][0], seq2[0][0] );
958                 initverticalw[0] += g;
959                 currentw[0] += g;
960
961                 cumpenal = 0.0;
962                 for( i=1; i<lgth1+1; i++ )
963                 {
964 //                      initverticalw[i] += ( ogcp1[0] + fgcp1[i-1] ) ;
965                         tmppenal = 0.0;
966
967 //                      tmppenal = ( (1.0-gapf2[j-1])*(1.0-ogcp1g[i]+fgcp1g[i]) + gapf2[j-1]*(1.0-digf1[i]-diaf1[i]) ) * 0.5 * fpenalty; // mada
968 //                      tmppenal = ( (1.0-gapz2[0])*(1.0-ogcp1g[0]+0.0) + gapz2[0]*(1.0-digf1[0] - diaf1[0]) ) * 0.5 * fpenalty; // mada
969 //                      tmppenal = ( (1.0-0.0)*(1.0-ogcp1g[0]+0.0) + 0.0*(1.0-0.0-0.0) ) * 0.5 * fpenalty; // mada
970 //                      tmppenal = 0.5 * fpenalty;
971 //                      tmppenal -= ( (1.0-0.0) * (1.0-diaf1[0]) + 0.0 ) * 0.5 * fpenalty; // 0.
972 //                      tmppenal -= ( (1.0-gapf2[j-1]) * ogcp1g[i] + gapf2[j-1] ) * 0.5 * fpenalty;
973 //                      fprintf( stderr, "0,0<-%d,%d, tmppenal 1 = %f\n", i, 0, tmppenal );
974                         initverticalw[i] += tmppenal;
975
976                         tmppenal = (1.0-gapf2[0]) * ( diaf1[i] + gappat1[i][0].freq - countnocount( gappat1[i], gappat2[0], 0, i ) ) * fpenalty;
977 //                      tmppenal = ( (1.0-gapf2[j])*(1.0-fgcp1g[i]+ogcp1g[i]) + gapf2[j]*(1.0-digf1[i]-diaf1[i]) ) * 0.5 * fpenalty; // mada
978 //                      tmppenal = ( (1.0-gapz2[1])*(1.0-fgcp1g[i]+ogcp1g[i]) + gapz2[1]*(1.0-digf1[i]-diaf1[i]) ) * 0.5 * fpenalty; // mada
979 //                      tmppenal = ( (1.0-gapf2[0])*(1.0-fgcp1g[i]+ogcp1g[i]) + gapf2[0]*(1.0-digf1[i]-diaf1[i]) ) * 0.5 * fpenalty; // mada
980 //                      tmppenal = 0.5 * fpenalty;
981 //                      tmppenal -= ( (1.0-gapf2[0]) * (1.0-diaf1[i]) + gapf2[0] ) * 0.5 * fpenalty;
982 //                      tmppenal -= ( (1.0-gapf2[j]) * fgcp1g[i] + gapf2[j] ) * 0.5 * fpenalty;
983                         initverticalw[i] += tmppenal;
984                         cumpenal += ( ogcp1g[i-0] + fgcp1g[i] ) * fpenalty * 0.5;
985 //                      initverticalw[i] += cumpenal - fgcp1g[i] * fpenalty * 0.5;
986 //                      fprintf( stderr, "0,0<-%d,%d, tmppenal2 = %f, cumpenal=%f, fgcp1g[i]=%f, ogcp1g[i]=%f\n", i, 0, tmppenal, cumpenal, fgcp1g[i], ogcp1g[i] );
987
988                 }
989                 cumpenal = 0.0;
990                 for( j=1; j<lgth2+1; j++ )
991                 {
992 //                      currentw[j] += ( ogcp2[0] + fgcp2[j-1] ) ;
993                         tmppenal = 0.0;
994
995 //                      tmppenal = ( (1.0-gapf1[i-1])*(1.0-ogcp2g[j]+fgcp2g[j]) + gapf1[i-1]*(1.0-digf2[j]-diaf2[j]) ) * 0.5 * fpenalty; // mada
996 //                      tmppenal = ( (1.0-gapz1[0])*(1.0-ogcp2g[0]+0.0) + gapz1[0]*(1.0-digf2[j]-diaf2[j]) ) * 0.5 * fpenalty; // mada
997 //                      tmppenal = ( (1.0-0.0)*(1.0-ogcp2g[0]+0.0) + 0.0*(1.0-0.0-0.0) ) * 0.5 * fpenalty; // mada
998 //                      tmppenal = 0.5 * fpenalty;
999 //                      tmppenal -= ( (1.0-0.0) * (1.0-diaf2[0]) + 0.0 ) * 0.5 * fpenalty; // 0.
1000 //                      tmppenal -= ( (1.0-gapf1[0]) * fgcp2g[j] + gapf1[0] ) * 0.5 * fpenalty;
1001 //                      fprintf( stderr, "0,0<-%d,%d, tmppenal 3 = %f\n", 0, j, tmppenal );
1002                         currentw[j] += tmppenal;
1003
1004                         tmppenal = (1.0-gapf1[0]) * ( diaf2[j] + gappat2[j][0].freq - countnocount( gappat1[0], gappat2[j], j, 0 ) ) * fpenalty;
1005 //                      tmppenal = ( 1.0-gapf1[i] ) * ( diaf2[j] + gappat2[j][0].freq - countnocount( gappat1[i], gappat2[j], j-mpi-1, 0 ) ) * fpenalty;
1006 //                      tmppenal = ( (1.0-gapf1[i])*(1.0-fgcp2g[j]+ogcp2g[j]) + gapf1[i]*(1.0-digf2[j]-diaf2[j]) ) * 0.5 * fpenalty; // mada
1007 //                      tmppenal = ( (1.0-gapz1[1])*(1.0-fgcp2g[j]+ogcp2g[j]) + gapz1[1]*(1.0-digf2[j]-diaf2[j]) ) * 0.5 * fpenalty; // mada
1008 //                      tmppenal = ( (1.0-gapf1[0])*(1.0-fgcp2g[j]+ogcp2g[j]) + gapf1[0]*(1.0-digf2[j]-diaf2[j]) ) * 0.5 * fpenalty; // mada
1009 //                      tmppenal = 0.5 * fpenalty;
1010 //                      tmppenal -= ( (1.0-gapf1[0]) * (1.0-diaf2[j]) + gapf1[0] ) * 0.5 * fpenalty;
1011 //                      tmppenal -= ( (1.0-gapf1[0]) * ogcp2g[j] + gapf1[i-1] ) * 0.5 * fpenalty;
1012 //                      fprintf( stderr, "-1,-1<-%d,%d, tmppenal4 = %f\n", 0, j, tmppenal );
1013                         currentw[j] += tmppenal;
1014                         cumpenal += ( ogcp2g[j-0] + fgcp2g[j] ) * fpenalty * 0.5;
1015 //                      currentw[j] += cumpenal - fgcp2g[j] * fpenalty * 0.5;
1016                 }
1017         }
1018 #if OUTGAP0TRY
1019         else
1020         {
1021                 for( j=1; j<lgth2+1; j++ )
1022                         currentw[j] -= offset * j / 2.0;
1023                 for( i=1; i<lgth1+1; i++ )
1024                         initverticalw[i] -= offset * i / 2.0;
1025         }
1026 #endif
1027
1028         m[0] = 0.0; // iranai
1029         for( j=1; j<lgth2+1; ++j ) 
1030         {
1031 //              m[j] = currentw[j-1] + ogcp1[1]; 
1032                 mp[j] = 0;
1033
1034                 tmppenal = 0.0;
1035 //              tmppenal = ( (1.0-gapz2[j])*(1.0-ogcp1g[1]+fgcp1g[1]) + gapz2[j]*(1.0-digf1[1]-diaf1[1]) ) * 0.5 * fpenalty; // mada
1036 //              tmppenal = ( (1.0-gapf2[j-1])*(1.0-ogcp1g[1]+fgcp1g[1]) + gapf2[j-1]*(1.0-digf1[1]-diaf1[1]) ) * 0.5 * fpenalty; // mada
1037 //              tmppenal = ( (1.0-gapf2[j-1])*(1.0-ogcp1g[i]+fgcp1g[i]) + gapf2[j-1]*(1.0-digf1[i]-diaf1[i]) ) * 0.5 * fpenalty; // mada
1038 //              tmppenal = 0.5 * fpenalty;
1039 //              tmppenal -= ( (1.0-0.0) * (1.0-0.0) + 0.0 ) * 0.5 * fpenalty;
1040 //              tmppenal -= ( (1.0-gapf2[-1]) * (1.0-diaf1[0]) + gapf2[-1] ) * 0.5 * fpenalty;
1041 //              if( tmppenal ) fprintf( stderr, "%c=%c, end j tmppenal=%f\n", seq1[0][0], seq2[0][j-1], tmppenal );
1042                 m[j] = currentw[j-1] + tmppenal + fpenalty * 10000; // iinoka??????? 
1043 //              m[j] = currentw[j-1] + tmppenal;
1044 //              m[j] = currentw[j-1] + ogcp1[1]; 
1045         }
1046         if( lgth2 == 0 )
1047                 lastverticalw[0] = 0.0; // Falign kara yobaretatoki kounarukanousei ari
1048         else
1049                 lastverticalw[0] = currentw[lgth2-1];
1050
1051         if( outgap ) lasti = lgth1+1; else lasti = lgth1;
1052
1053 #if 0
1054 fprintf( stderr, "currentw after penal= \n" );
1055 for( i=0; i<lgth2+1; i++ )
1056 {
1057         fprintf( stderr, "%5.2f ", currentw[i] );
1058 }
1059 fprintf( stderr, "\n" );
1060 fprintf( stderr, "initverticalw after penal= \n" );
1061 for( i=0; i<lgth1+1; i++ )
1062 {
1063         fprintf( stderr, "%5.2f ", initverticalw[i] );
1064 }
1065 fprintf( stderr, "\n" );
1066 fprintf( stderr, "fcgp\n" );
1067 for( i=0; i<lgth1; i++ ) 
1068         fprintf( stderr, "fgcp1[%d]=%f\n", i, ogcp1[i] );
1069 for( i=0; i<lgth2; i++ ) 
1070         fprintf( stderr, "fgcp2[%d]=%f\n", i, ogcp2[i] );
1071 #endif
1072
1073         for( i=1; i<lasti; i++ )
1074         {
1075                 wtmp = previousw; 
1076                 previousw = currentw;
1077                 currentw = wtmp;
1078
1079                 previousw[0] = initverticalw[i-1];
1080
1081                 match_calc( currentw, cpmx1, cpmx2, i, lgth2, floatwork, intwork, 0 );
1082 #if XXXXXXX
1083 fprintf( stderr, "\n" );
1084 fprintf( stderr, "i=%d\n", i );
1085 fprintf( stderr, "currentw = \n" );
1086 for( j=0; j<lgth2; j++ )
1087 {
1088         fprintf( stderr, "%5.2f ", currentw[j] );
1089 }
1090 fprintf( stderr, "\n" );
1091 #endif
1092                 if( localhom )
1093                 {
1094 //                      fprintf( stderr, "Calling imp_match_calc (o) lgth = %d, i = %d\n", lgth1, i );
1095 #if  0
1096                         imp_match_out_veadH( currentw, i, lgth2 );
1097 #else
1098                         imp_match_out_veadH( currentw, i, lgth2 );
1099 #endif
1100                 }
1101 #if 1
1102 fprintf( stderr, "\n" );
1103 fprintf( stderr, "i=%d\n", i );
1104 fprintf( stderr, "currentw = \n" );
1105 for( j=0; j<lgth2; j++ )
1106 {
1107         fprintf( stderr, "%5.2f ", currentw[j] );
1108 }
1109 fprintf( stderr, "\n" );
1110 #endif
1111                 currentw[0] = initverticalw[i];
1112
1113
1114 //              mi = previousw[0] + ogcp2[1];  // machigai
1115                 tmppenal = 0.0;
1116 //              tmppenal = ( (1.0-gapz1[i])*(1.0-ogcp2g[1]+fgcp2g[1]) + gapz1[i]*(1.0-digf2[1]-diaf2[1]) ) * 0.5 * fpenalty; // mada
1117 //              tmppenal = ( (1.0-gapf1[i-1])*(1.0-ogcp2g[1]+fgcp2g[1]) + gapf1[i-1]*(1.0-digf2[1]-diaf2[1]) ) * 0.5 * fpenalty; // mada
1118 //              fprintf( stderr, "%c=%c, end i tmppenal=%f, ogcp2g[1]=%f\n", seq1[0][i-1], seq2[0][0], tmppenal, ogcp2g[1] );
1119                 mi = previousw[0] + tmppenal + fpenalty * 10000; // iinoka???????
1120 //              mi = previousw[0] + tmppenal;
1121
1122                 mpi = 0;
1123                 ijppt = ijp[i] + 1;
1124                 mjpt = m + 1;
1125                 prept = previousw;
1126                 curpt = currentw + 1;
1127                 mpjpt = mp + 1;
1128                 fgcp2pt = fgcp2;
1129                 ogcp2pt = ogcp2 + 1;
1130                 fgcp1va = fgcp1[i-1];
1131                 ogcp1va = ogcp1[i];
1132                 lastj = lgth2+1;
1133                 for( j=1; j<lastj; j++ )
1134                 {
1135                         wm = *prept;
1136
1137                         if( i==j ) fprintf( stderr, "%c-%c, wm1=%f\n", seq1[0][i], seq2[0][j], wm );
1138
1139                         if( gappat2[j] ) 
1140                         {
1141                                 g = diaf1[i] * gappat2[j][0].freq * fpenalty;
1142                                 if( g ) fprintf( stderr, "match penal1=%f, %c-%c\n", g/fpenalty, seq1[0][i], seq2[0][j] );
1143 //                              wm += g;
1144                         }
1145
1146                         if( gappat1[i] )
1147                         {
1148                                 g = diaf2[j] * gappat1[i][0].freq * fpenalty;
1149                                 if( g ) fprintf( stderr, "match penal2=%f, %c-%c\n", g/fpenalty, seq1[0][i], seq2[0][j] );
1150 //                              wm += g;
1151                         }
1152
1153                         {
1154                                 g = ( (gappat1[i][0].freq) * (gappat2[j][0].freq) - countnocountmatch( gappat1[i], gappat2[j], i==j ) ) * fpenalty;
1155                                 if( g ) fprintf( stderr, "match penal3=%f*%f-%f=%f, %c-%c\n", gappat1[i][0].freq, gappat2[j][0].freq, countnocountmatch( gappat1[i], gappat2[j], 0 ), g/fpenalty, seq1[0][i], seq2[0][j] );
1156 //                              if( g ) fprintf( stderr, "match penal3=%f-%f, %c-%c\n", gappat1[i][0].freq * gappat2[j][0].freq * fpenalty, countnocountmatch( gappat1[i], gappat2[j], 0 ) * fpenalty, seq1[0][i], seq2[0][j] );
1157 //                              wm += g;
1158                         }
1159 //                      if( i==j+1 ) fprintf( stderr, "%c-%c, wm2=%f\n", seq1[0][i], seq2[0][j], wm );
1160
1161                         *ijppt = 0;
1162
1163
1164 #if 0
1165                         fprintf( stderr, "%5.0f->", wm );
1166 #endif
1167 #if 0
1168                         fprintf( stderr, "%5.0f?", g );
1169 #endif
1170 //                      tmppenal = fpenalty;
1171                         tmppenal = diaf2[j] * ( 1.0 - gapf1[i] ) * fpenalty;
1172                         if( gappat2[j][0].freq )
1173                         {
1174 //                              fprintf( stderr, "gap pat wo shiraberu. %c-%c\n", seq1[0][i], seq2[0][j] );
1175                                 tmppenal += ( gappat2[j][0].freq ) * ( 1.0 - gapf1[i] ) * fpenalty;
1176                                 tmppenal -= ( countnocountx( gappat1[i], diaf1[i], gappat2[j], j-mpi-1, 1 ) ) * fpenalty;
1177                         }
1178 //                      tmppenal = ( 1.0-gapf1[i] ) * ( diaf2[j] + gappat2[j][0].freq ) * fpenalty;
1179 //                      tmppenal -= ( diaf1[i] * gappat2[j][j-mpi-1].freq + countnocountx( gappat1[i], gappat2[j], j-mpi-1, (seq1[0][i] == 'L' && seq2[0][j] == 'F') ) ) * fpenalty;
1180 //                      if( gappat2[j][0].freq )
1181 //                              tmppenal -= ( diaf1[i] * gappat2[j][j-mpi-1].freq + countnocountx( gappat1[i], gappat2[j], j-mpi-1, 0 ) ) * fpenalty;
1182 //                      tmppenal = ( (1.0-gapf1[i])*(1.0-fgcp2g[j]) + gapf1[i]*(1.0-digf2[j]-diaf2[j]) ) * 0.5 * fpenalty; // mada
1183 //                      tmppenal = ( (1.0-gapz1[i+1])*(1.0-fgcp2g[j]+ogcp2g[j]) + gapz1[i+1]*(1.0-digf2[j]-diaf2[j]) ) * 0.5 * fpenalty; // mada
1184 //                      tmppenal = ( (1.0-gapf1[i])*(1.0-fgcp2g[j]+ogcp2g[j]) + gapf1[i]*(1.0-digf2[j]-diaf2[j]) ) * 0.5 * fpenalty; // mada
1185 //                      tmppenal = 0.5 * fpenalty;
1186 //                      tmppenal -= ( (1.0-gapf1[i])*(1.0-diaf2[j]) + gapf1[i] ) * 0.5 * fpenalty;
1187 //                      tmppenal -= ( (1.0-gapf1[i])*fgcp2g[j] + gapf1[i] ) * 0.5 * fpenalty;
1188 //                      tmppenal = *fgcp2pt-fpenalty*0.5*gapf1[i];
1189 //                      tmppenal = *fgcp2pt;
1190                         if( (g=mi+tmppenal) > wm )
1191                         {
1192                                 fprintf( stderr, "jump i start=%f (i=%d, j=%d, gapf1[i]=%f, digf2[j]=%f), %c-%c, gaplen=%d\n", (g-mi)/fpenalty, i, j, gapf1[i], diaf2[j], seq1[0][i], seq2[0][j], j-mpi-1 );
1193 //                              fprintf( stderr, "jump i start, zenhan=%f\n", ( 1.0-gapf1[i] ) * ( diaf2[j] + gappat2[j][0].freq ) );
1194                                 if( gappat2[j][0].freq )
1195 //                                      fprintf( stderr, "jump i start, kanwa(len=%d)=%f\n", j-mpi-1, diaf1[i] * gappat2[j][j-mpi-1].freq );
1196                                 wm = g;
1197 //                              if( i==j+1 ) fprintf( stderr, "%c-%c, wm2.1=%f\n", seq1[0][i], seq2[0][j], wm );
1198                                 *ijppt = -( j - mpi );
1199                         }
1200
1201                         if( (g=*prept) > mi )
1202                         {
1203                                 fprintf( stderr, "jump i end=%f, %c-%c\n", g-*prept, seq1[0][i-1], seq2[0][j-1] );
1204                                 mi = g;
1205                                 mpi = j-1;
1206                         }
1207                         else if( j != 1 )
1208                         {
1209 //                              mi += ( ogcp2g[j-0] + fgcp2g[j] ) * fpenalty * 0.5;
1210 //                              fprintf( stderr, "%c%c/%c%c exp, og=%f,fg=%f\n", '=', '=', seq2[0][j-1], seq2[0][j], ogcp2g[j-0] * fpenalty*0.5, fgcp2g[j] * fpenalty*0.5 );
1211                         }
1212 #if USE_PENALTY_EX
1213                                 mi += fpenalty_ex;
1214 #endif
1215
1216 #if 0 
1217                         fprintf( stderr, "%5.0f?", g );
1218 #endif
1219 //                      tmppenal = fpenalty;
1220                         tmppenal = diaf1[i] * ( 1.0 - gapf2[j] ) * fpenalty;
1221                         if( gappat1[i][0].freq )
1222                         {
1223 //                              fprintf( stderr, "gap pat wo shiraberu. %c-%c\n", seq1[0][i], seq2[0][j] );
1224                                 tmppenal += ( gappat1[i][0].freq ) * ( 1.0 - gapf2[j] ) * fpenalty;
1225 //                              fprintf( stderr, "penalx = %f\n", tmppenal/fpenalty );
1226                                 tmppenal -= ( countnocountx( gappat2[j], diaf2[j], gappat1[i], i-*mpjpt-1, 1 ) ) * fpenalty;
1227 //                              fprintf( stderr, "penaly = %f\n", tmppenal/fpenalty );
1228                         }
1229 //                      tmppenal += gappat1[i][0].freq * ( 1.0 - gapf2[j] ) * fpenalty;
1230 //                      tmppenal = (1.0-gapf2[j]) * ( diaf1[i] + gappat1[i][0].freq ) * fpenalty;
1231 //                      if( gappat1[i][0].freq )
1232 //                              tmppenal -= ( diaf2[j] * gappat1[i][i-*mpjpt-1].freq + countnocountx( gappat2[j], gappat1[i], i-*mpjpt-1, 0 ) ) * fpenalty;
1233 //                      tmppenal = ( (1.0-gapz2[j+1])*(1.0-fgcp1g[i]+ogcp1g[i]) + gapz2[j+1]*(1.0-digf1[i]-diaf1[i]) ) * 0.5 * fpenalty; // mada
1234 //                      tmppenal = ( (1.0-gapf2[j])*(1.0-fgcp1g[i]+ogcp1g[i]) + gapf2[j]*(1.0-digf1[i]-diaf1[i]) ) * 0.5 * fpenalty; // mada
1235 //                      tmppenal = ( (1.0-gapf2[j])*(1.0-fgcp1g[i]) + gapf2[j]*(1.0-digf1[i]-diaf1[i]) ) * 0.5 * fpenalty; // mada
1236 //                      tmppenal = 0.5 * fpenalty;
1237 //                      tmppenal -= ( (1.0-gapf2[j])*(1.0-diaf1[i]) + gapf2[j] ) * 0.5 * fpenalty;
1238 //                      tmppenal -= ( (1.0-gapf2[j])*fgcp1g[i] + gapf2[j] ) * 0.5 * fpenalty;
1239 //                      tmppenal = fgcp1va-fpenalty*0.5*gapf2[j];
1240 //                      tmppenal = fgcp1va;
1241                         if( (g=*mjpt+tmppenal) > wm )
1242                         {
1243                                 fprintf( stderr, "jump j start=%f, %c-%c, gaplen=%d\n", (g-*mjpt)/fpenalty, seq1[0][i], seq2[0][j], i-*mpjpt-1 );
1244                                 wm = g;
1245 //                              if( i==j+1 ) fprintf( stderr, "%c-%c, wm2.2=%f\n", seq1[0][i], seq2[0][j], wm );
1246                                 *ijppt = +( i - *mpjpt );
1247                         }
1248                         if( (g=*prept) > *mjpt )
1249                         {
1250                                 fprintf( stderr, "jump j end=%f, %c-%c\n", g-*prept, seq1[0][i-1], seq2[0][j-1] );
1251                                 *mjpt = g;
1252                                 *mpjpt = i-1;
1253                         }
1254                         else if( i != 1 )
1255                         {
1256 //                              m[j] += ( ogcp1g[i-0] + fgcp1g[i] ) * fpenalty * 0.5;
1257 //                              fprintf( stderr, "%c%c/%c%c exp, og=%f,fg=%f\n", seq1[0][i-1], seq1[0][i], '=', '=', ogcp1g[i-0] * fpenalty*0.5, fgcp1g[i] * fpenalty*0.5 );
1258                         }
1259 #if USE_PENALTY_EX
1260                         m[j] += fpenalty_ex;
1261 #endif
1262
1263 #if 0
1264                         fprintf( stderr, "%5.0f ", wm );
1265 #endif
1266                         *curpt++ += wm;
1267                         ijppt++;
1268                         mjpt++;
1269                         prept++;
1270                         mpjpt++;
1271                         fgcp2pt++;
1272                         ogcp2pt++;
1273 //                      if( i==j+1 ) fprintf( stderr, "%c-%c, wm3=%f\n", seq1[0][i], seq2[0][j], wm );
1274                 }
1275                 lastverticalw[i] = currentw[lgth2-1];
1276         }
1277
1278 //      fprintf( stderr, "wm = %f\n", wm );
1279
1280 #if OUTGAP0TRY
1281         if( !outgap )
1282         {
1283                 for( j=1; j<lgth2+1; j++ )
1284                         currentw[j] -= offset * ( lgth2 - j ) / 2.0;
1285                 for( i=1; i<lgth1+1; i++ )
1286                         lastverticalw[i] -= offset * ( lgth1 - i  / 2.0);
1287         }
1288 #endif
1289                 
1290         /*
1291         fprintf( stderr, "\n" );
1292         for( i=0; i<icyc; i++ ) fprintf( stderr,"%s\n", seq1[i] );
1293         fprintf( stderr, "#####\n" );
1294         for( j=0; j<jcyc; j++ ) fprintf( stderr,"%s\n", seq2[j] );
1295         fprintf( stderr, "====>" );
1296         for( i=0; i<icyc; i++ ) strcpy( mseq1[i], seq1[i] );
1297         for( j=0; j<jcyc; j++ ) strcpy( mseq2[j], seq2[j] );
1298         */
1299         if( localhom )
1300         {
1301                 Atracking_localhom( impmatch, currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
1302         }
1303         else
1304                 Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
1305
1306 //      fprintf( stderr, "### impmatch = %f\n", *impmatch );
1307
1308         resultlen = strlen( mseq1[0] );
1309         if( alloclen < resultlen || resultlen > N )
1310         {
1311                 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
1312                 ErrorExit( "LENGTH OVER!\n" );
1313         }
1314
1315
1316         for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
1317         for( j=0; j<jcyc; j++ ) strcpy( seq2[j], mseq2[j] );
1318         for( i=0; i<lgth1+1; i++ ) 
1319         {
1320                 free( gappat1[i] );
1321                 gappat1[i] = NULL; 
1322         }
1323         for( i=0; i<lgth2+1; i++ ) 
1324         {
1325                 free( gappat2[i] );
1326                 gappat2[i] = NULL; 
1327         }
1328         /*
1329         fprintf( stderr, "\n" );
1330         for( i=0; i<icyc; i++ ) fprintf( stderr, "%s\n", mseq1[i] );
1331         fprintf( stderr, "#####\n" );
1332         for( j=0; j<jcyc; j++ ) fprintf( stderr, "%s\n", mseq2[j] );
1333         */
1334
1335         fprintf( stderr, "wm = %f\n", wm );
1336
1337         return( wm );
1338 }
1339
1340