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