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