Next version of JABA
[jabaws.git] / binaries / src / mafft / core / Salignmm.c.double
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( double *ogcp, int clus, char **seq, double *eff, int len )
10 {
11         int i, j, gc, gb; 
12         float totaleff = 0.0;
13
14         
15         for( i=0; i<len; i++ ) ogcp[i] = 0.0;
16         for( j=0; j<clus; j++ ) 
17         {
18                 fprintf( stderr, "seq[%d] = %s\n", j, seq[j] );
19                 gc = 0;
20                 for( i=0; i<len; i++ ) 
21                 {
22                         gb = gc;
23                         gc = ( seq[j][i] == '-' );
24                         {
25                                 fprintf( stderr, "gb,gc=%d,%d ", gb,gc );
26                                 fprintf( stderr, "i=%d, %30.20f", i, ogcp[i] );
27                                 if( !gb *  gc ) ogcp[i] += (float)eff[j];
28                                 fprintf( stderr, "-> %30.20f\n", ogcp[i] );
29                         }
30                 }
31                 totaleff+= eff[j];
32         }
33         for( i=0; i<len; i++ ) 
34         {
35                 fprintf( stderr, "ogcp[%d] = %30.20f\n", i, ogcp[i] );
36                 ogcp[i] /= totaleff;
37         }
38 }
39
40 static void FinalGapCount( double *fgcp, int clus, char **seq, double *eff, int len )
41 {
42         int i, j, gc, gb; 
43         float totaleff = 0.0;
44         
45         for( i=0; i<len+1; i++ ) fgcp[i] = 0.0;
46         for( j=0; j<clus; j++ ) 
47         {
48                 gc = ( seq[j][0] == '-' );
49                 for( i=1; i<len+1; i++ ) 
50                 {
51                         gb = gc;
52                         gc = ( seq[j][i] == '-' );
53                         {
54                                 if( gb * !gc ) fgcp[i-1] += (float)eff[j];
55                         }
56                 }
57                 totaleff += eff[j];
58                 fprintf( stderr, "eff[%d] = %30.20f\n", j, eff[j] );
59         }
60         for( i=0; i<len; i++ ) 
61         {
62 //              fgcp[i] /= totaleff;
63                 fprintf( stderr, "fgcp[%d] = %30.20f\n", i, fgcp[i] );
64         }
65 }
66 static float **impmtx = NULL;
67 float imp_match_out_sc( int i1, int j1 )
68 {
69 //      fprintf( stderr, "imp+match = %f\n", impmtx[i1][j1] * fastathreshold );
70 //      fprintf( stderr, "val = %f\n", impmtx[i1][j1] );
71         return( impmtx[i1][j1] );
72 }
73 static void imp_match_out_vead_gapmap( float *imp, int i1, int lgth2, int *gapmap2 )
74 {
75         int j;
76         float *pt = impmtx[i1];
77         for( j=0; j<lgth2; j++ )
78                 *imp++ += pt[gapmap2[j]];
79 }
80 static void imp_match_out_vead( float *imp, int i1, int lgth2 )
81 {
82         int j;
83         float *pt = impmtx[i1];
84         for( j=0; j<lgth2; j++ )
85                 *imp++ += pt[j];
86 }
87
88 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 )
89 {
90         int dif, i, j, k1, k2, tmpint, start1, start2, end1, end2;
91         static int impalloclen = 0;
92         double effij, effijx;
93         char *pt, *pt1, *pt2;
94         int allgap;
95         static char *nocount1 = NULL;
96         static char *nocount2 = NULL;
97         LocalHom *tmpptr;
98
99         if( impalloclen < lgth1 + 2 || impalloclen < lgth2 + 2 )
100         {
101                 if( impmtx ) FreeFloatMtx( impmtx );
102                 if( nocount1 ) free( nocount1 );
103                 if( nocount2 ) free( nocount2 );
104                 impalloclen = MAX( lgth1, lgth2 ) + 2;
105                 impmtx = AllocateFloatMtx( impalloclen, impalloclen );
106                 nocount1 = AllocateCharVec( impalloclen );
107                 nocount2 = AllocateCharVec( impalloclen );
108         }
109
110         for( i=0; i<lgth1; i++ )
111         {
112                 for( j=0; j<clus1; j++ )
113                         if( seq1[j][i] == '-' ) break;
114                 if( j != clus1 ) nocount1[i] = 1; 
115                 else                     nocount1[i] = 0;
116         }
117         for( i=0; i<lgth2; i++ )
118         {
119                 for( j=0; j<clus2; j++ )
120                         if( seq2[j][i] == '-' ) break;
121                 if( j != clus2 ) nocount2[i] = 1;
122                 else                     nocount2[i] = 0;
123         }
124
125 #if 0
126 fprintf( stderr, "nocount2 =\n" );
127 for( i = 0; i<impalloclen; i++ )
128 {
129         fprintf( stderr, "nocount2[%d] = %d (%c)\n", i, nocount2[i], seq2[0][i] );
130 }
131 #endif
132
133
134
135 #if 0
136         fprintf( stderr, "eff1 in _init_strict = \n" );
137         for( i=0; i<clus1; i++ )
138                 fprintf( stderr, "eff1[] = %f\n", eff1[i] );
139         for( i=0; i<clus2; i++ )
140                 fprintf( stderr, "eff2[] = %f\n", eff2[i] );
141 #endif
142
143         for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
144                 impmtx[i][j] = 0.0;
145         effijx =  fastathreshold;
146         for( i=0; i<clus1; i++ )
147         {
148                 for( j=0; j<clus2; j++ )
149                 {
150                         effij = eff1[i] * eff2[j] * effijx;
151                         tmpptr = localhom[i][j];
152                         while( tmpptr )
153                         {
154 //                              fprintf( stderr, "start1 = %d\n", tmpptr->start1 );
155 //                              fprintf( stderr, "end1   = %d\n", tmpptr->end1   );
156 //                              fprintf( stderr, "i = %d, seq1 = \n%s\n", i, seq1[i] );
157 //                              fprintf( stderr, "j = %d, seq2 = \n%s\n", j, seq2[j] );
158                                 pt = seq1[i];
159                                 tmpint = -1;
160                                 while( *pt != 0 )
161                                 {
162                                         if( *pt++ != '-' ) tmpint++;
163                                         if( tmpint == tmpptr->start1 ) break;
164                                 }
165                                 start1 = pt - seq1[i] - 1;
166         
167                                 while( *pt != 0 )
168                                 {
169 //                                      fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, tmpptr->end1, pt-seq1[i] );
170                                         if( *pt++ != '-' ) tmpint++;
171                                         if( tmpint == tmpptr->end1 ) break;
172                                 }
173                                 end1 = pt - seq1[i] - 1;
174         
175                                 pt = seq2[j];
176                                 tmpint = -1;
177                                 while( *pt != 0 )
178                                 {
179                                         if( *pt++ != '-' ) tmpint++;
180                                         if( tmpint == tmpptr->start2 ) break;
181                                 }
182                                 start2 = pt - seq2[j] - 1;
183                                 while( *pt != 0 )
184                                 {
185                                         if( *pt++ != '-' ) tmpint++;
186                                         if( tmpint == tmpptr->end2 ) break;
187                                 }
188                                 end2 = pt - seq2[j] - 1;
189 //                              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] );
190 //                              fprintf( stderr, "step 0\n" );
191                                 if( end1 - start1 != end2 - start2 )
192                                 {
193 //                                      fprintf( stderr, "CHUUI!!, start1 = %d, end1 = %d, start2 = %d, end2 = %d\n", start1, end1, start2, end2 );
194                                 }
195
196 #if 1
197                                 k1 = start1; k2 = start2;
198                                 pt1 = seq1[i] + k1;
199                                 pt2 = seq2[j] + k2;
200                                 while( *pt1 && *pt2 )
201                                 {
202                                         if( *pt1 != '-' && *pt2 != '-' )
203                                         {
204 // ½Å¤ß¤òÆó½Å¤Ë¤«¤±¤Ê¤¤¤è¤¦¤ËÃí°Õ¤·¤Æ²¼¤µ¤¤¡£
205 //                                              impmtx[k1][k2] += tmpptr->wimportance * fastathreshold;
206                                                 impmtx[k1][k2] += tmpptr->importance * 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 double *ogcp1;
837         static double *ogcp2;
838         static double *fgcp1;
839         static double *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                         FreeDoubleVec( ogcp1 );
879                         FreeDoubleVec( ogcp2 );
880                         FreeDoubleVec( fgcp1 );
881                         FreeDoubleVec( 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 = AllocateDoubleVec( ll1+2 );
911                 ogcp2 = AllocateDoubleVec( ll2+2 );
912                 fgcp1 = AllocateDoubleVec( ll1+2 );
913                 fgcp2 = AllocateDoubleVec( 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
1262         return( wm );
1263 }
1264
1265 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 )
1266 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
1267 {
1268 //      int k;
1269         register int i, j;
1270         int lasti, lastj;                      /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
1271         int lgth1, lgth2;
1272         int resultlen;
1273         float wm;   /* int ?????? */
1274         float g;
1275         float *currentw, *previousw;
1276         float fpenalty = (float)penalty;
1277         float fpenalty_ex = (float)penalty_ex;
1278 #if 1
1279         float *wtmp;
1280         short *ijppt;
1281         float *mjpt, *prept, *curpt;
1282         int *mpjpt;
1283 #endif
1284         static float mi, *m;
1285         static short **ijp;
1286         static int mpi, *mp;
1287         static float *w1, *w2;
1288         static float *match;
1289         static float *initverticalw;    /* kufuu sureba iranai */
1290         static float *lastverticalw;    /* kufuu sureba iranai */
1291         static char **mseq1;
1292         static char **mseq2;
1293         static char **mseq;
1294         static double *ogcp1;
1295         static double *ogcp2;
1296         static double *fgcp1;
1297         static double *fgcp2;
1298         static float **cpmx1;
1299         static float **cpmx2;
1300         static int **intwork;
1301         static float **floatwork;
1302         static int orlgth1 = 0, orlgth2 = 0;
1303
1304
1305 #if 0
1306         fprintf( stderr, "eff in SA+++align\n" );
1307         for( i=0; i<icyc; i++ ) fprintf( stderr, "eff1[%d] = %f\n", i, eff1[i] );
1308 #endif
1309         if( orlgth1 == 0 )
1310         {
1311                 mseq1 = AllocateCharMtx( njob, 0 );
1312                 mseq2 = AllocateCharMtx( njob, 0 );
1313         }
1314
1315
1316         lgth1 = strlen( seq1[0] );
1317         lgth2 = strlen( seq2[0] );
1318
1319         if( lgth1 > orlgth1 || lgth2 > orlgth2 )
1320         {
1321                 int ll1, ll2;
1322
1323                 if( orlgth1 > 0 && orlgth2 > 0 )
1324                 {
1325                         FreeFloatVec( w1 );
1326                         FreeFloatVec( w2 );
1327                         FreeFloatVec( match );
1328                         FreeFloatVec( initverticalw );
1329                         FreeFloatVec( lastverticalw );
1330
1331                         FreeFloatVec( m );
1332                         FreeIntVec( mp );
1333
1334                         FreeCharMtx( mseq );
1335
1336                         FreeDoubleVec( ogcp1 );
1337                         FreeDoubleVec( ogcp2 );
1338                         FreeDoubleVec( fgcp1 );
1339                         FreeDoubleVec( fgcp2 );
1340
1341
1342                         FreeFloatMtx( cpmx1 );
1343                         FreeFloatMtx( cpmx2 );
1344
1345                         FreeFloatMtx( floatwork );
1346                         FreeIntMtx( intwork );
1347                 }
1348
1349                 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
1350                 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
1351
1352 #if DEBUG
1353                 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
1354 #endif
1355
1356                 w1 = AllocateFloatVec( ll2+2 );
1357                 w2 = AllocateFloatVec( ll2+2 );
1358                 match = AllocateFloatVec( ll2+2 );
1359
1360                 initverticalw = AllocateFloatVec( ll1+2 );
1361                 lastverticalw = AllocateFloatVec( ll1+2 );
1362
1363                 m = AllocateFloatVec( ll2+2 );
1364                 mp = AllocateIntVec( ll2+2 );
1365
1366                 mseq = AllocateCharMtx( njob, ll1+ll2 );
1367
1368                 ogcp1 = AllocateDoubleVec( ll1+2 );
1369                 ogcp2 = AllocateDoubleVec( ll2+2 );
1370                 fgcp1 = AllocateDoubleVec( ll1+2 );
1371                 fgcp2 = AllocateDoubleVec( ll2+2 );
1372
1373                 cpmx1 = AllocateFloatMtx( 26, ll1+2 );
1374                 cpmx2 = AllocateFloatMtx( 26, ll2+2 );
1375
1376                 floatwork = AllocateFloatMtx( 26, MAX( ll1, ll2 )+2 ); 
1377                 intwork = AllocateIntMtx( 26, MAX( ll1, ll2 )+2 ); 
1378
1379 #if DEBUG
1380                 fprintf( stderr, "succeeded\n" );
1381 #endif
1382
1383                 orlgth1 = ll1 - 100;
1384                 orlgth2 = ll2 - 100;
1385         }
1386
1387
1388         for( i=0; i<icyc; i++ )
1389         {
1390                 mseq1[i] = mseq[i];
1391                 seq1[i][lgth1] = 0;
1392         }
1393         for( j=0; j<jcyc; j++ )
1394         {
1395                 mseq2[j] = mseq[icyc+j];
1396                 seq2[j][lgth2] = 0;
1397         }
1398
1399
1400         if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
1401         {
1402                 int ll1, ll2;
1403
1404                 if( commonAlloc1 && commonAlloc2 )
1405                 {
1406                         FreeShortMtx( commonIP );
1407                 }
1408
1409                 ll1 = MAX( orlgth1, commonAlloc1 );
1410                 ll2 = MAX( orlgth2, commonAlloc2 );
1411
1412 #if DEBUG
1413                 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
1414 #endif
1415
1416                 commonIP = AllocateShortMtx( ll1+10, ll2+10 );
1417
1418 #if DEBUG
1419                 fprintf( stderr, "succeeded\n\n" );
1420 #endif
1421
1422                 commonAlloc1 = ll1;
1423                 commonAlloc2 = ll2;
1424         }
1425         ijp = commonIP;
1426
1427         cpmx_calc_new( seq1, cpmx1, eff1, strlen( seq1[0] ), icyc );
1428         cpmx_calc_new( seq2, cpmx2, eff2, strlen( seq2[0] ), jcyc );
1429
1430         OpeningGapCount( ogcp1, icyc, seq1, eff1, lgth1  );
1431         OpeningGapCount( ogcp2, jcyc, seq2, eff2, lgth2 );
1432         FinalGapCount( fgcp1, icyc, seq1, eff1, lgth1 );
1433         FinalGapCount( fgcp2, jcyc, seq2, eff2, lgth2 );
1434
1435         for( i=0; i<lgth1; i++ ) 
1436         {
1437                 ogcp1[i] = 0.5 * ( 1.0 - ogcp1[i] );
1438                 fgcp1[i] = 0.5 * ( 1.0 - fgcp1[i] );
1439         }
1440         for( i=0; i<lgth2; i++ ) 
1441         {
1442                 ogcp2[i] = 0.5 * ( 1.0 - ogcp2[i] );
1443                 fgcp2[i] = 0.5 * ( 1.0 - fgcp2[i] );
1444         }
1445 #if 0
1446         for( i=0; i<lgth1; i++ ) 
1447                 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
1448 #endif
1449
1450         currentw = w1;
1451         previousw = w2;
1452
1453
1454         match_calc( initverticalw, cpmx2, cpmx1, 0, lgth1, floatwork, intwork, 1 );
1455
1456
1457         match_calc( currentw, cpmx1, cpmx2, 0, lgth2, floatwork, intwork, 1 );
1458 #if 0 // -> tbfast.c
1459         if( localhom )
1460                 imp_match_calc( currentw, icyc, jcyc, lgth1, lgth2, seq1, seq2, eff1, eff2, localhom, 1, 0 );
1461
1462 #endif
1463
1464         if( outgap == 1 )
1465         {
1466                 for( i=1; i<lgth1+1; i++ )
1467                 {
1468                         initverticalw[i] += fpenalty * ( ogcp1[0] + fgcp1[i-1] ) ;
1469                 }
1470                 for( j=1; j<lgth2+1; j++ )
1471                 {
1472                         currentw[j] += fpenalty * ( ogcp2[0] + fgcp2[j-1] ) ;
1473                 }
1474         }
1475 #if OUTGAP0TRY
1476         else
1477         {
1478                 for( j=1; j<lgth2+1; j++ )
1479                         currentw[j] -= offset * j / 2.0;
1480                 for( i=1; i<lgth1+1; i++ )
1481                         initverticalw[i] -= offset * i / 2.0;
1482         }
1483 #endif
1484
1485         for( j=1; j<lgth2+1; ++j ) 
1486         {
1487                 m[j] = currentw[j-1] + fpenalty * ogcp1[1]; mp[j] = 0;
1488         }
1489
1490         lastverticalw[0] = currentw[lgth2-1];
1491
1492         if( outgap ) lasti = lgth1+1; else lasti = lgth1;
1493
1494 #if XXXXXXX
1495 fprintf( stderr, "currentw = \n" );
1496 for( i=0; i<lgth1+1; i++ )
1497 {
1498         fprintf( stderr, "%5.2f ", currentw[i] );
1499 }
1500 fprintf( stderr, "\n" );
1501 fprintf( stderr, "initverticalw = \n" );
1502 for( i=0; i<lgth2+1; i++ )
1503 {
1504         fprintf( stderr, "%5.2f ", initverticalw[i] );
1505 }
1506 fprintf( stderr, "\n" );
1507 fprintf( stderr, "fcgp\n" );
1508 for( i=0; i<lgth1; i++ ) 
1509         fprintf( stderr, "fgcp1[%d]=%f\n", i, ogcp1[i] );
1510 for( i=0; i<lgth2; i++ ) 
1511         fprintf( stderr, "fgcp2[%d]=%f\n", i, ogcp2[i] );
1512 #endif
1513
1514         for( i=1; i<lasti; i++ )
1515         {
1516                 wtmp = previousw; 
1517                 previousw = currentw;
1518                 currentw = wtmp;
1519
1520                 previousw[0] = initverticalw[i-1];
1521
1522                 match_calc( currentw, cpmx1, cpmx2, i, lgth2, floatwork, intwork, 0 );
1523 #if XXXXXXX
1524 fprintf( stderr, "\n" );
1525 fprintf( stderr, "i=%d\n", i );
1526 fprintf( stderr, "currentw = \n" );
1527 for( j=0; j<lgth2; j++ )
1528 {
1529         fprintf( stderr, "%5.2f ", currentw[j] );
1530 }
1531 fprintf( stderr, "\n" );
1532 #endif
1533                 if( localhom )
1534                 {
1535 //                      fprintf( stderr, "Calling imp_match_calc (o) lgth = %d, i = %d\n", lgth1, i );
1536 #if  0
1537                         imp_match_out_vead( currentw, i, lgth2 );
1538 #else
1539                         imp_match_out_vead_gapmap( currentw, gapmap1[i], lgth2, gapmap2 );
1540 #endif
1541                 }
1542 #if XXXXXXX
1543 fprintf( stderr, "\n" );
1544 fprintf( stderr, "i=%d\n", i );
1545 fprintf( stderr, "currentw = \n" );
1546 for( j=0; j<lgth2; j++ )
1547 {
1548         fprintf( stderr, "%5.2f ", currentw[j] );
1549 }
1550 fprintf( stderr, "\n" );
1551 #endif
1552                 currentw[0] = initverticalw[i];
1553
1554 #if 1
1555
1556                 mi = previousw[0] + fpenalty * ogcp2[1]; mpi = 0;
1557
1558                 ijppt = ijp[i] + 1;
1559                 mjpt = m + 1;
1560                 prept = previousw;
1561                 curpt = currentw + 1;
1562                 mpjpt = mp + 1;
1563                 for( j=1; j<lgth2+1; j++ )
1564                 {
1565                         wm = *prept;
1566                         *ijppt = 0;
1567
1568 #if 0
1569                         fprintf( stderr, "%5.0f->", wm );
1570 #endif
1571                         g = mi + fpenalty * fgcp2[j-1];
1572 #if 0
1573                         fprintf( stderr, "%5.0f?", g );
1574 #endif
1575                         if( g > wm )
1576                         {
1577                                 wm = g;
1578                                 *ijppt = -( j - mpi );
1579                         }
1580                         g = *prept + fpenalty * ogcp2[j];
1581                         if( g >= mi )
1582                         {
1583                                 mi = g;
1584                                 mpi = j-1;
1585                         }
1586 #if USE_PENALTY_EX
1587                         mi += fpenalty_ex;
1588 #endif
1589
1590                         g = *mjpt + fpenalty * fgcp1[i-1];
1591 #if 0 
1592                         fprintf( stderr, "%5.0f?", g );
1593 #endif
1594                         if( g > wm )
1595                         {
1596                                 wm = g;
1597                                 *ijppt = +( i - *mpjpt );
1598                         }
1599                         g = *prept + fpenalty * ogcp1[i];
1600                         if( g >= *mjpt )
1601                         {
1602                                 *mjpt = g;
1603                                 *mpjpt = i-1;
1604                         }
1605 #if USE_PENALTY_EX
1606                         m[j] += fpenalty_ex;
1607 #endif
1608
1609 #if 0
1610                         fprintf( stderr, "%5.0f ", wm );
1611 #endif
1612                         *curpt += wm;
1613                         ijppt++;
1614                         mjpt++;
1615                         prept++;
1616                         mpjpt++;
1617                         curpt++;
1618                 }
1619                 lastverticalw[i] = currentw[lgth2-1];
1620 #else
1621         floatncpy( previousw, currentw, lgth2+1 );
1622         previousw[0] = initverticalw[i-1];
1623
1624
1625         match_calc( currentw, cpmx1, cpmx2, i, lgth2, floatwork, intwork, 0 );
1626         currentw[0] = initverticalw[i];
1627
1628         mi = previousw[0] + fpenalty * ogcp2[1]; mpi = 0;
1629                 lastj = lgth2+1;
1630         for( j=1; j<lastj; j++ )
1631         {
1632             wm = previousw[j-1];
1633             ijp[i][j] = 0;
1634
1635             g = fpenalty * fgcp2[j-1];
1636             if( mi + g > wm )
1637             {
1638                 wm = mi + g; 
1639                 ijp[i][j] = -( j - mpi );
1640             }
1641             g = fpenalty * ogcp2[j];
1642             if( mi <= previousw[j-1] + g )
1643             {
1644                 mi = previousw[j-1] + g;
1645                 mpi = j-1;
1646             }
1647
1648             g = fpenalty * fgcp1[i-1];
1649             if( m[j] + g > wm )
1650             {
1651                 wm = m[j] + g;
1652                 ijp[i][j] = +( i - mp[j] );
1653             }
1654             g = fpenalty * ogcp1[i];
1655             if( m[j] <= previousw[j-1] + g )
1656             {
1657                 m[j] = previousw[j-1] + g ;
1658                 mp[j] = i-1;
1659             }
1660             currentw[j] += wm;
1661         }
1662         lastverticalw[i] = currentw[lgth2-1];
1663
1664 #endif
1665         }
1666
1667 #if OUTGAP0TRY
1668         if( !outgap )
1669         {
1670                 for( j=1; j<lgth2+1; j++ )
1671                         currentw[j] -= offset * ( lgth2 - j ) / 2.0;
1672                 for( i=1; i<lgth1+1; i++ )
1673                         lastverticalw[i] -= offset * ( lgth1 - i  / 2.0);
1674         }
1675 #endif
1676                 
1677         /*
1678         fprintf( stderr, "\n" );
1679         for( i=0; i<icyc; i++ ) fprintf( stderr,"%s\n", seq1[i] );
1680         fprintf( stderr, "#####\n" );
1681         for( j=0; j<jcyc; j++ ) fprintf( stderr,"%s\n", seq2[j] );
1682         fprintf( stderr, "====>" );
1683         for( i=0; i<icyc; i++ ) strcpy( mseq1[i], seq1[i] );
1684         for( j=0; j<jcyc; j++ ) strcpy( mseq2[j], seq2[j] );
1685         */
1686         if( localhom )
1687         {
1688                 Atracking_localhom_gapmap( impmatch, currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc, gapmap1, gapmap2 );
1689         }
1690         else
1691                 Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
1692
1693 //      fprintf( stderr, "### impmatch = %f\n", *impmatch );
1694
1695         resultlen = strlen( mseq1[0] );
1696         if( alloclen < resultlen || resultlen > N )
1697         {
1698                 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
1699                 ErrorExit( "LENGTH OVER!\n" );
1700         }
1701
1702
1703         for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
1704         for( j=0; j<jcyc; j++ ) strcpy( seq2[j], mseq2[j] );
1705         /*
1706         fprintf( stderr, "\n" );
1707         for( i=0; i<icyc; i++ ) fprintf( stderr, "%s\n", mseq1[i] );
1708         fprintf( stderr, "#####\n" );
1709         for( j=0; j<jcyc; j++ ) fprintf( stderr, "%s\n", mseq2[j] );
1710         */
1711
1712
1713         return( wm );
1714 }
1715
1716 float translate_and_Calign( char **mseq1, char **mseq2, double *effarr1, double *effarr2, int clus1, int clus2, int alloclen )
1717 {
1718     int i;
1719     float wm;
1720     char **result;
1721     char *seq, **aseq;
1722     double *effarr;
1723     int nseq;
1724         int resultlen;
1725
1726     if     ( clus1 == 1 && clus2 != 1 ) 
1727     {
1728         seq = mseq1[0]; aseq = mseq2; effarr = effarr2; nseq = clus2+1;
1729 #if 0
1730                 printf( "effarr in transl... = \n" );
1731                 for( i=0; i<clus2; i++ ) printf( "%f ", effarr2[i] );
1732 #endif
1733     }
1734     else if( clus1 != 1 && clus2 == 1 ) 
1735     {
1736         seq = mseq2[0]; aseq = mseq1; effarr = effarr1; nseq = clus1+1;
1737     }
1738     else ErrorExit( "ERROR in translate_and_Calign" );
1739
1740     result = Calignm1( &wm, aseq, seq, effarr, nseq-2, 0 );
1741
1742         resultlen = strlen( result[0] );
1743         if( alloclen < resultlen || resultlen > N )
1744         {
1745                 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
1746                 ErrorExit( "LENGTH OVER!\n" );
1747         }
1748     for( i=0; i<nseq-1; i++ ) strcpy( aseq[i], result[i] );
1749     strcpy( seq, result[nseq-1] );
1750
1751     return( 0.0 );
1752 }