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