b4d200b1f2932dcd0315b9b8b1ad6c4ec7d684cb
[jabaws.git] / binaries / src / mafft / core / partSalignmm.c.comment
1 #include "mltaln.h"
2 #include "dp.h"
3
4 #define MACHIGAI 0
5 #define OUTGAP0TRY 1
6 #define DEBUG 0
7 #define XXXXXXX    0
8 #define USE_PENALTY_EX  0
9
10 static void OpeningGapCount( float *ogcp, int clus, char **seq, double *eff, int len )
11 {
12         int i, j, gc, gb; 
13         float feff;
14         
15         for( i=0; i<len; i++ ) ogcp[i] = 0.0;
16         for( j=0; j<clus; j++ ) 
17         {
18                 feff = (float)eff[j];
19                 gc = 0;
20                 for( i=0; i<len; i++ ) 
21                 {
22                         gb = gc;
23                         gc = ( seq[j][i] == '-' );
24                         {
25                                 if( !gb *  gc ) ogcp[i] += feff;
26                         }
27                 }
28         }
29 }
30
31 static void FinalGapCount( float *fgcp, int clus, char **seq, double *eff, int len )
32 {
33         int i, j, gc, gb; 
34         float feff;
35         
36         for( i=0; i<len; i++ ) fgcp[i] = 0.0;
37         for( j=0; j<clus; j++ ) 
38         {
39                 feff = (float)eff[j];
40                 gc = ( seq[j][0] == '-' );
41                 for( i=1; i<len+1; i++ ) 
42                 {
43                         gb = gc;
44                         gc = ( seq[j][i] == '-' );
45                         {
46                                 if( gb * !gc ) fgcp[i-1] += feff;
47                         }
48                 }
49         }
50 }
51
52
53                         
54                 
55
56 static int impalloclen = 0;
57 static float **impmtx = NULL;
58 float part_imp_match_out_sc( int i1, int j1 )
59 {
60 //      fprintf( stderr, "impalloclen = %d\n", impalloclen );
61 //      fprintf( stderr, "i1,j1=%d,%d -> impmtx=%f\n", i1, j1, impmtx[i1][j1] );
62         return( impmtx[i1][j1] );
63 #if 0
64         if( i1 == l1 || j1 == l2 ) return( 0.0 );
65         return( impmtx[i1+start1][j1+start2] );
66 #endif
67 }
68 static void part_imp_match_out_vead_gapmap( float *imp, int i1, int lgth2, int start2, int *gapmap2 )
69 {
70         int j;
71         for( j=0; j<lgth2; j++ )
72         {
73                 imp[j] += impmtx[i1][start2+gapmap2[j]];
74         }
75 }
76
77 void part_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 )
78 {
79         int dif, i, j, k1, k2, tmpint, start1, start2, end1, end2;
80         double effij, effijx; 
81         char *pt, *pt1, *pt2;
82         int allgap;
83         LocalHom *tmpptr;
84
85         if( impalloclen <= lgth1 + 2 || impalloclen <= lgth2 + 2 )
86         {
87                 if( impmtx ) FreeFloatMtx( impmtx );
88                 impalloclen = MAX( lgth1, lgth2 ) + 2;
89                 impmtx = AllocateFloatMtx( impalloclen+100, impalloclen+100 );
90         }
91
92
93 #if 0
94         fprintf( stderr, "eff1 in _init_strict = \n" );
95         for( i=0; i<clus1; i++ )
96                 fprintf( stderr, "eff1[] = %f\n", eff1[i] );
97         for( i=0; i<clus2; i++ )
98                 fprintf( stderr, "eff2[] = %f\n", eff2[i] );
99 #endif
100
101         for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
102                 impmtx[i][j] = 0.0;
103         effijx = 1.0 * fastathreshold;
104         for( i=0; i<clus1; i++ )
105         {
106                 for( j=0; j<clus2; j++ )
107                 {
108                         effij = eff1[i] * eff2[j] * effijx;
109                         tmpptr = localhom[i][j];
110                         while( tmpptr )
111                         {
112 //                              fprintf( stderr, "start1 = %d\n", tmpptr->start1 );
113 //                              fprintf( stderr, "end1   = %d\n", tmpptr->end1   );
114 //                              fprintf( stderr, "i = %d, seq1 = \n%s\n", i, seq1[i] );
115 //                              fprintf( stderr, "j = %d, seq2 = \n%s\n", j, seq2[j] );
116                                 pt = seq1[i];
117                                 tmpint = -1;
118                                 while( *pt != 0 )
119                                 {
120                                         if( *pt++ != '-' ) tmpint++;
121                                         if( tmpint == tmpptr->start1 ) break;
122                                 }
123                                 start1 = (int)( pt - seq1[i] ) - 1;
124         
125                                 if( tmpptr->start1 == tmpptr->end1 ) end1 = start1;
126                                 else
127                                 {
128 #if MACHIGAI
129                                         while( *pt != 0 )
130                                         {
131                                                 if( tmpint == tmpptr->end1 ) break;
132                                                 if( *pt++ != '-' ) tmpint++;
133                                         }
134                                         end1 = (int)( pt - seq1[i] ) - 1;
135 #else
136                                         while( *pt != 0 )
137                                         {
138 //                                              fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, tmpptr->end1, pt-seq1[i] );
139                                                 if( *pt++ != '-' ) tmpint++;
140                                                 if( tmpint == tmpptr->end1 ) break;
141                                         }
142                                         end1 = (int)( pt - seq1[i] ) - 1;
143 #endif
144                                 }
145         
146                                 pt = seq2[j];
147                                 tmpint = -1;
148                                 while( *pt != 0 )
149                                 {
150                                         if( *pt++ != '-' ) tmpint++;
151                                         if( tmpint == tmpptr->start2 ) break;
152                                 }
153                                 start2 = (int)( pt - seq2[j] ) - 1;
154                                 if( tmpptr->start2 == tmpptr->end2 ) end2 = start2;
155                                 else
156                                 {
157 #if MACHIGAI
158                                         while( *pt != 0 )
159                                         {
160                                                 if( tmpint == tmpptr->end2 ) break;
161                                                 if( *pt++ != '-' ) tmpint++;
162                                         }
163                                         end2 = (int)( pt - seq2[j] ) - 1;
164 #else
165                                         while( *pt != 0 )
166                                         {
167                                                 if( *pt++ != '-' ) tmpint++;
168                                                 if( tmpint == tmpptr->end2 ) break;
169                                         }
170                                         end2 = (int)( pt - seq2[j] ) - 1;
171 #endif
172                                 }
173 //                              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] );
174 //                              fprintf( stderr, "step 0\n" );
175                                 if( end1 - start1 != end2 - start2 )
176                                 {
177 //                                      fprintf( stderr, "CHUUI!!, start1 = %d, end1 = %d, start2 = %d, end2 = %d\n", start1, end1, start2, end2 );
178                                 }
179
180                                 k1 = start1; k2 = start2;
181                                 pt1 = seq1[i] + k1;
182                                 pt2 = seq2[j] + k2;
183                                 while( *pt1 && *pt2 )
184                                 {
185                                         if( *pt1 != '-' && *pt2 != '-' )
186                                         {
187 // ½Å¤ß¤òÆó½Å¤Ë¤«¤±¤Ê¤¤¤è¤¦¤ËÃí°Õ¤·¤Æ²¼¤µ¤¤¡£
188 //                                              impmtx[k1][k2] += tmpptr->wimportance * fastathreshold;
189 //                                              impmtx[k1][k2] += tmpptr->importance * effij;
190                                                 impmtx[k1][k2] += tmpptr->fimportance * effij;
191 //                                              fprintf( stderr, "k1=%d, k2=%d, impalloclen=%d\n", k1, k2, impalloclen );
192 //                                              fprintf( stderr, "mark, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
193                                                 k1++; k2++;
194                                                 pt1++; pt2++;
195                                         }
196                                         else if( *pt1 != '-' && *pt2 == '-' )
197                                         {
198 //                                              fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
199                                                 k2++; pt2++;
200                                         }
201                                         else if( *pt1 == '-' && *pt2 != '-' )
202                                         {
203 //                                              fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
204                                                 k1++; pt1++;
205                                         }
206                                         else if( *pt1 == '-' && *pt2 == '-' )
207                                         {
208 //                                              fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
209                                                 k1++; pt1++;
210                                                 k2++; pt2++;
211                                         }
212                                         if( k1 > end1 || k2 > end2 ) break;
213                                 }
214                                 tmpptr = tmpptr->next;
215                         }
216                 }
217         }
218 #if 1
219         0printf( stderr, "impmtx = \n" );
220         for( k2=0; k2<lgth2; k2++ )
221                 fprintf( stderr, "%6.3f ", (double)k2 );
222         fprintf( stderr, "\n" );
223         for( k1=0; k1<lgth1; k1++ )
224         {
225                 fprintf( stderr, "%d", k1 );
226                 for( k2=0; k2<lgth2; k2++ )
227                         fprintf( stderr, "%2.1f ", impmtx[k1][k2] );
228                 fprintf( stderr, "\n" );
229         }
230         exit( 1 );
231 #endif
232 }
233
234 void part_imp_match_init( float *imp, int clus1, int clus2, int lgth1, int lgth2, char **seq1, char **seq2, double *eff1, double *eff2, LocalHom ***localhom )
235 {
236         int dif, i, j, k1, k2, tmpint, start1, start2, end1, end2;
237         static int impalloclen = 0;
238         char *pt;
239         int allgap;
240         static char *nocount1 = NULL;
241         static char *nocount2 = NULL;
242
243         if( impalloclen < lgth1 || impalloclen < lgth2 )
244         {
245                 if( impmtx ) FreeFloatMtx( impmtx );
246                 if( nocount1 ) free( nocount1 );
247                 if( nocount2 ) free( nocount2 );
248                 impalloclen = MAX( lgth1, lgth2 ) + 2;
249                 impmtx = AllocateFloatMtx( impalloclen, impalloclen );
250                 nocount1 = AllocateCharVec( impalloclen );
251                 nocount2 = AllocateCharVec( impalloclen );
252                 impalloclen -= 2;
253         }
254
255         for( i=0; i<lgth1; i++ )
256         {
257                 for( j=0; j<clus1; j++ )
258                         if( seq1[j][i] == '-' ) break;
259                 if( j != clus1 ) nocount1[i] = 1; 
260                 else                     nocount1[i] = 0;
261         }
262         for( i=0; i<lgth2; i++ )
263         {
264                 for( j=0; j<clus2; j++ )
265                         if( seq2[j][i] == '-' ) break;
266                 if( j != clus2 ) nocount2[i] = 1;
267                 else                     nocount2[i] = 0;
268         }
269
270 #if 0
271 fprintf( stderr, "nocount2 =\n" );
272 for( i = 0; i<impalloclen; i++ )
273 {
274         fprintf( stderr, "nocount2[%d] = %d (%c)\n", i, nocount2[i], seq2[0][i] );
275 }
276 #endif
277
278         for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
279                 impmtx[i][j] = 0.0;
280         for( i=0; i<clus1; i++ )
281         {
282                 fprintf( stderr, "i = %d, seq1 = %s\n", i, seq1[i] );
283                 for( j=0; j<clus2; j++ )
284                 {
285                         fprintf( stderr, "start1 = %d\n", localhom[i][j]->start1 );
286                         fprintf( stderr, "end1   = %d\n", localhom[i][j]->end1   );
287                         fprintf( stderr, "j = %d, seq2 = %s\n", j, seq2[j] );
288                         pt = seq1[i];
289                         tmpint = -1;
290                         while( *pt != 0 )
291                         {
292                                 if( *pt++ != '-' ) tmpint++;
293                                 if( tmpint == localhom[i][j]->start1 ) break;
294                         }
295                         start1 = pt - seq1[i] - 1;
296
297                         while( *pt != 0 )
298                         {
299 //                              fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, localhom[i][j].end1, pt-seq1[i] );
300                                 if( *pt++ != '-' ) tmpint++;
301                                 if( tmpint == localhom[i][j]->end1 ) break;
302                         }
303                         end1 = pt - seq1[i] - 1;
304
305                         pt = seq2[j];
306                         tmpint = -1;
307                         while( *pt != 0 )
308                         {
309                                 if( *pt++ != '-' ) tmpint++;
310                                 if( tmpint == localhom[i][j]->start2 ) break;
311                         }
312                         start2 = pt - seq2[j] - 1;
313                         while( *pt != 0 )
314                         {
315                                 if( *pt++ != '-' ) tmpint++;
316                                 if( tmpint == localhom[i][j]->end2 ) break;
317                         }
318                         end2 = pt - seq2[j] - 1;
319 //                      fprintf( stderr, "start1 = %d, end1 = %d, start2 = %d, end2 = %d\n", start1, end1, start2, end2 );
320                         k1 = start1;
321                         k2 = start2;
322                         fprintf( stderr, "step 0\n" );
323                         while( k1 <= end1 && k2 <= end2 )
324                         {
325 #if 0
326                                 if( !nocount1[k1] && !nocount2[k2] )
327                                         impmtx[k1][k2] += localhom[i][j].wimportance * eff1[i] * eff2[j];
328                                 k1++; k2++;
329 #else
330                                 if( !nocount1[k1] && !nocount2[k2] )
331                                         impmtx[k1][k2] += localhom[i][j]->wimportance * eff1[i] * eff2[j];
332                                 k1++; k2++;
333 #endif
334                         }
335
336                         dif = ( end1 - start1 ) - ( end2 - start2 );
337                         fprintf( stderr, "dif = %d\n", dif );
338                         if( dif > 0 )
339                         {
340                                 do
341                                 {
342                                         fprintf( stderr, "dif = %d\n", dif );
343                                         k1 = start1;
344                                         k2 = start2 - dif;
345                                         while( k1 <= end1 && k2 <= end2 )
346                                         {
347                                                 if( 0 <= k2 && start2 <= k2 && !nocount1[k1] && !nocount2[k2] )
348                                                         impmtx[k1][k2] = localhom[i][j]->wimportance * eff1[i] * eff2[j];
349                                                 k1++; k2++;
350                                         }
351                                 }
352                                 while( dif-- );
353                         }
354                         else
355                         {
356                                 do
357                                 {
358                                         k1 = start1 + dif;
359                                         k2 = start2;
360                                         while( k1 <= end1 )
361                                         {
362                                                 if( k1 >= 0 && k1 >= start1 && !nocount1[k1] && !nocount2[k2] )
363                                                         impmtx[k1][k2] = localhom[i][j]->wimportance * eff1[i] * eff2[j];
364                                                 k1++; k2++;
365                                         }
366                                 }
367                                 while( dif++ );
368                         }
369                 }
370         }
371 #if 1
372         fprintf( stderr, "impmtx = \n" );
373         for( k2=0; k2<lgth2; k2++ )
374                 fprintf( stderr, "%6.3f ", (double)k2 );
375         fprintf( stderr, "\n" );
376         for( k1=0; k1<lgth1; k1++ )
377         {
378                 fprintf( stderr, "%d", k1 );
379                 for( k2=0; k2<lgth2; k2++ )
380                         fprintf( stderr, "%6.3f ", impmtx[k1][k2] );
381                 fprintf( stderr, "\n" );
382         }
383         exit( 1 );
384 #endif
385 }
386
387 static void match_calc( float *match, float **cpmx1, float **cpmx2, int i1, int lgth2, float **floatwork, int **intwork, int initialize )
388 {
389         int j, k, l;
390         float scarr[26];
391         float **cpmxpd = floatwork;
392         int **cpmxpdn = intwork;
393         int count = 0;
394
395         if( initialize )
396         {
397                 for( j=0; j<lgth2; j++ )
398                 {
399                         count = 0;
400                         for( l=0; l<26; l++ )
401                         {
402                                 if( cpmx2[l][j] )
403                                 {
404                                         cpmxpd[count][j] = cpmx2[l][j];
405                                         cpmxpdn[count][j] = l;
406                                         count++;
407                                 }
408                         }
409                         cpmxpdn[count][j] = -1;
410                 }
411         }
412
413         for( l=0; l<26; l++ )
414         {
415                 scarr[l] = 0.0;
416                 for( k=0; k<26; k++ )
417                         scarr[l] += n_dis[k][l] * cpmx1[k][i1];
418         }
419 #if 0 /* ¤³¤ì¤ò»È¤¦¤È¤\ad¤Ïfloatwork¤Î¥¢¥í¥±¡¼¥È¤òµÕ¤Ë¤¹¤ë */
420         {
421                 float *fpt, **fptpt, *fpt2;
422                 int *ipt, **iptpt;
423                 fpt2 = match;
424                 iptpt = cpmxpdn;
425                 fptpt = cpmxpd;
426                 while( lgth2-- )
427                 {
428                         *fpt2 = 0.0;
429                         ipt=*iptpt,fpt=*fptpt;
430                         while( *ipt > -1 )
431                                 *fpt2 += scarr[*ipt++] * *fpt++;
432                         fpt2++,iptpt++,fptpt++;
433                 } 
434         }
435 #else
436         for( j=0; j<lgth2; j++ )
437         {
438                 match[j] = 0.0;
439                 for( k=0; cpmxpdn[k][j]>-1; k++ )
440                         match[j] += scarr[cpmxpdn[k][j]] * cpmxpd[k][j];
441         } 
442 #endif
443 }
444
445 static void Atracking_localhom( float *impwmpt, float *lasthorizontalw, float *lastverticalw, 
446                                                 char **seq1, char **seq2, 
447                         char **mseq1, char **mseq2, 
448                         float **cpmx1, float **cpmx2, 
449                         short **ijp, int icyc, int jcyc,
450                                                 int start1, int end1, int start2, int end2,
451                                                 int *gapmap1, int *gapmap2 )
452 {
453         int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k;
454         char gap[] = "-";
455         float wm;
456         lgth1 = strlen( seq1[0] );
457         lgth2 = strlen( seq2[0] );
458
459 #if 0
460         for( i=0; i<lgth1; i++ ) 
461         {
462                 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
463         }
464 #endif
465  
466         if( outgap == 1 )
467                 ;
468         else
469         {
470                 wm = lastverticalw[0];
471                 for( i=0; i<lgth1; i++ )
472                 {
473                         if( lastverticalw[i] >= wm )
474                         {
475                                 wm = lastverticalw[i];
476                                 iin = i; jin = lgth2-1;
477                                 ijp[lgth1][lgth2] = +( lgth1 - i );
478                         }
479                 }
480                 for( j=0; j<lgth2; j++ )
481                 {
482                         if( lasthorizontalw[j] >= wm )
483                         {
484                                 wm = lasthorizontalw[j];
485                                 iin = lgth1-1; jin = j;
486                                 ijp[lgth1][lgth2] = -( lgth2 - j );
487                         }
488                 }
489         }
490
491     for( i=0; i<lgth1+1; i++ ) 
492     {
493         ijp[i][0] = i + 1;
494     }
495     for( j=0; j<lgth2+1; j++ ) 
496     {
497         ijp[0][j] = -( j + 1 );
498     }
499
500         for( i=0; i<icyc; i++ )
501         {
502                 mseq1[i] += lgth1+lgth2;
503                 *mseq1[i] = 0;
504         }
505         for( j=0; j<jcyc; j++ )
506         {
507                 mseq2[j] += lgth1+lgth2;
508                 *mseq2[j] = 0;
509         }
510         iin = lgth1; jin = lgth2;
511         *impwmpt = 0.0;
512         for( k=0; k<=lgth1+lgth2; k++ ) 
513         {
514                 if( ijp[iin][jin] < 0 ) 
515                 {
516                         ifi = iin-1; jfi = jin+ijp[iin][jin];
517                 }
518                 else if( ijp[iin][jin] > 0 )
519                 {
520                         ifi = iin-ijp[iin][jin]; jfi = jin-1;
521                 }
522                 else
523                 {
524                         ifi = iin-1; jfi = jin-1;
525                 }
526                 l = iin - ifi;
527                 while( --l ) 
528                 {
529                         for( i=0; i<icyc; i++ )
530                                 *--mseq1[i] = seq1[i][ifi+l];
531                         for( j=0; j<jcyc; j++ ) 
532                                 *--mseq2[j] = *gap;
533                         k++;
534                 }
535                 l= jin - jfi;
536                 while( --l )
537                 {
538                         for( i=0; i<icyc; i++ ) 
539                                 *--mseq1[i] = *gap;
540                         for( j=0; j<jcyc; j++ ) 
541                                 *--mseq2[j] = seq2[j][jfi+l];
542                         k++;
543                 }
544                 if( iin != lgth1 && jin != lgth2 ) // ??
545                 {
546                         *impwmpt += part_imp_match_out_sc( gapmap1[iin]+start1, gapmap2[jin]+start2 );
547 //                      fprintf( stderr, "impwm = %f (iin=%d, jin=%d) seq1=%c, seq2=%c\n", *impwmpt, iin, jin, seq1[0][iin], seq2[0][jin] );
548                 }
549                 if( iin <= 0 || jin <= 0 ) break;
550                 for( i=0; i<icyc; i++ ) 
551                         *--mseq1[i] = seq1[i][ifi];
552                 for( j=0; j<jcyc; j++ ) 
553                         *--mseq2[j] = seq2[j][jfi];
554                 k++;
555                 iin = ifi; jin = jfi;
556         }
557 }
558 static float Atracking( float *lasthorizontalw, float *lastverticalw, 
559                                                 char **seq1, char **seq2, 
560                         char **mseq1, char **mseq2, 
561                         float **cpmx1, float **cpmx2, 
562                         short **ijp, int icyc, int jcyc )
563 {
564         int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k, lastk;
565         char gap[] = "-";
566         float wm = 0.0;
567         lgth1 = strlen( seq1[0] );
568         lgth2 = strlen( seq2[0] );
569
570 #if 0
571         for( i=0; i<lgth1; i++ ) 
572         {
573                 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
574         }
575 #endif
576  
577         if( outgap == 1 )
578                 ;
579         else
580         {
581                 wm = lastverticalw[0];
582                 for( i=0; i<lgth1; i++ )
583                 {
584                         if( lastverticalw[i] >= wm )
585                         {
586                                 wm = lastverticalw[i];
587                                 iin = i; jin = lgth2-1;
588                                 ijp[lgth1][lgth2] = +( lgth1 - i );
589                         }
590                 }
591                 for( j=0; j<lgth2; j++ )
592                 {
593                         if( lasthorizontalw[j] >= wm )
594                         {
595                                 wm = lasthorizontalw[j];
596                                 iin = lgth1-1; jin = j;
597                                 ijp[lgth1][lgth2] = -( lgth2 - j );
598                         }
599                 }
600         }
601
602     for( i=0; i<lgth1+1; i++ ) 
603     {
604         ijp[i][0] = i + 1;
605     }
606     for( j=0; j<lgth2+1; j++ ) 
607     {
608         ijp[0][j] = -( j + 1 );
609     }
610
611         for( i=0; i<icyc; i++ )
612         {
613                 mseq1[i] += lgth1+lgth2;
614                 *mseq1[i] = 0;
615         }
616         for( j=0; j<jcyc; j++ )
617         {
618                 mseq2[j] += lgth1+lgth2;
619                 *mseq2[j] = 0;
620         }
621         iin = lgth1; jin = lgth2;
622         lastk = lgth1+lgth2;
623         for( k=0; k<=lastk; k++ ) 
624         {
625                 if( ijp[iin][jin] < 0 ) 
626                 {
627                         ifi = iin-1; jfi = jin+ijp[iin][jin];
628                 }
629                 else if( ijp[iin][jin] > 0 )
630                 {
631                         ifi = iin-ijp[iin][jin]; jfi = jin-1;
632                 }
633                 else
634                 {
635                         ifi = iin-1; jfi = jin-1;
636                 }
637                 l = iin - ifi;
638                 while( --l ) 
639                 {
640                         for( i=0; i<icyc; i++ )
641                                 *--mseq1[i] = seq1[i][ifi+l];
642                         for( j=0; j<jcyc; j++ ) 
643                                 *--mseq2[j] = *gap;
644                         k++;
645                 }
646                 l= jin - jfi;
647                 while( --l )
648                 {
649                         for( i=0; i<icyc; i++ ) 
650                                 *--mseq1[i] = *gap;
651                         for( j=0; j<jcyc; j++ ) 
652                                 *--mseq2[j] = seq2[j][jfi+l];
653                         k++;
654                 }
655                 if( iin <= 0 || jin <= 0 ) break;
656                 for( i=0; i<icyc; i++ ) 
657                         *--mseq1[i] = seq1[i][ifi];
658                 for( j=0; j<jcyc; j++ ) 
659                         *--mseq2[j] = seq2[j][jfi];
660                 k++;
661                 iin = ifi; jin = jfi;
662         }
663         return( 0.0 );
664 }
665
666 float partA__align( char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen, LocalHom ***localhom, float *impmatch, int start1, int end1, int start2, int end2, int *gapmap1, int *gapmap2 )
667 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
668 {
669 //      int k;
670         register int i, j;
671         int lasti;                      /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
672         int lgth1, lgth2;
673         int resultlen;
674         float wm = 0.0;   /* int ?????? */
675         float g;
676         float *currentw, *previousw;
677 #if 1
678         float *wtmp;
679         short *ijppt;
680         float *mjpt, *prept, *curpt;
681         int *mpjpt;
682 #endif
683         static float mi, *m;
684         static short **ijp;
685         static int mpi, *mp;
686         static float *w1, *w2;
687         static float *match;
688         static float *initverticalw;    /* kufuu sureba iranai */
689         static float *lastverticalw;    /* kufuu sureba iranai */
690         static char **mseq1;
691         static char **mseq2;
692         static char **mseq;
693         static float *ogcp1;
694         static float *ogcp2;
695         static float *fgcp1;
696         static float *fgcp2;
697         static float **cpmx1;
698         static float **cpmx2;
699         static int **intwork;
700         static float **floatwork;
701         static int orlgth1 = 0, orlgth2 = 0;
702         float fpenalty = (float)penalty;
703         float fpenalty_ex = (float)penalty_ex;
704         float *fgcp2pt;
705         float *ogcp2pt;
706         float fgcp1va;
707         float ogcp1va;
708
709
710
711 #if 0
712         fprintf( stderr, "eff in SA+++align\n" );
713         for( i=0; i<icyc; i++ ) fprintf( stderr, "eff1[%d] = %f\n", i, eff1[i] );
714 #endif
715         if( orlgth1 == 0 )
716         {
717                 mseq1 = AllocateCharMtx( njob, 0 );
718                 mseq2 = AllocateCharMtx( njob, 0 );
719         }
720
721
722         lgth1 = strlen( seq1[0] );
723         lgth2 = strlen( seq2[0] );
724
725         if( lgth1 > orlgth1 || lgth2 > orlgth2 )
726         {
727                 int ll1, ll2;
728
729                 if( orlgth1 > 0 && orlgth2 > 0 )
730                 {
731                         FreeFloatVec( w1 );
732                         FreeFloatVec( w2 );
733                         FreeFloatVec( match );
734                         FreeFloatVec( initverticalw );
735                         FreeFloatVec( lastverticalw );
736
737                         FreeFloatVec( m );
738                         FreeIntVec( mp );
739
740                         FreeCharMtx( mseq );
741
742                         FreeFloatVec( ogcp1 );
743                         FreeFloatVec( ogcp2 );
744                         FreeFloatVec( fgcp1 );
745                         FreeFloatVec( fgcp2 );
746
747
748                         FreeFloatMtx( cpmx1 );
749                         FreeFloatMtx( cpmx2 );
750
751                         FreeFloatMtx( floatwork );
752                         FreeIntMtx( intwork );
753                 }
754
755                 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
756                 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
757
758 #if DEBUG
759                 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
760 #endif
761
762                 w1 = AllocateFloatVec( ll2+2 );
763                 w2 = AllocateFloatVec( ll2+2 );
764                 match = AllocateFloatVec( ll2+2 );
765
766                 initverticalw = AllocateFloatVec( ll1+2 );
767                 lastverticalw = AllocateFloatVec( ll1+2 );
768
769                 m = AllocateFloatVec( ll2+2 );
770                 mp = AllocateIntVec( ll2+2 );
771
772                 mseq = AllocateCharMtx( njob, ll1+ll2 );
773
774                 ogcp1 = AllocateFloatVec( ll1+2 );
775                 ogcp2 = AllocateFloatVec( ll2+2 );
776                 fgcp1 = AllocateFloatVec( ll1+2 );
777                 fgcp2 = AllocateFloatVec( ll2+2 );
778
779                 cpmx1 = AllocateFloatMtx( 26, ll1+2 );
780                 cpmx2 = AllocateFloatMtx( 26, ll2+2 );
781
782                 floatwork = AllocateFloatMtx( 26, MAX( ll1, ll2 )+2 ); 
783                 intwork = AllocateIntMtx( 26, MAX( ll1, ll2 )+2 ); 
784
785 #if DEBUG
786                 fprintf( stderr, "succeeded\n" );
787 #endif
788
789                 orlgth1 = ll1 - 100;
790                 orlgth2 = ll2 - 100;
791         }
792
793
794         for( i=0; i<icyc; i++ ) mseq1[i] = mseq[i];
795         for( j=0; j<jcyc; j++ ) mseq2[j] = mseq[icyc+j];
796
797
798         if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
799         {
800                 int ll1, ll2;
801
802                 if( commonAlloc1 && commonAlloc2 )
803                 {
804                         FreeShortMtx( commonIP );
805                 }
806
807                 ll1 = MAX( orlgth1, commonAlloc1 );
808                 ll2 = MAX( orlgth2, commonAlloc2 );
809
810 #if DEBUG
811                 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
812 #endif
813
814                 commonIP = AllocateShortMtx( ll1+10, ll2+10 );
815
816 #if DEBUG
817                 fprintf( stderr, "succeeded\n\n" );
818 #endif
819
820                 commonAlloc1 = ll1;
821                 commonAlloc2 = ll2;
822         }
823         ijp = commonIP;
824
825         cpmx_calc_new( seq1, cpmx1, eff1, lgth1, icyc );
826         cpmx_calc_new( seq2, cpmx2, eff2, lgth2, jcyc );
827
828         OpeningGapCount( ogcp1, icyc, seq1, eff1, lgth1 );
829         OpeningGapCount( ogcp2, jcyc, seq2, eff2, lgth2 );
830         FinalGapCount( fgcp1, icyc, seq1, eff1, lgth1 );
831         FinalGapCount( fgcp2, jcyc, seq2, eff2, lgth2 );
832
833         for( i=0; i<lgth1; i++ ) 
834         {
835                 ogcp1[i] = 0.5 * ( 1.0 - ogcp1[i] ) * fpenalty;
836                 fgcp1[i] = 0.5 * ( 1.0 - fgcp1[i] ) * fpenalty;
837         }
838         for( i=0; i<lgth2; i++ ) 
839         {
840                 ogcp2[i] = 0.5 * ( 1.0 - ogcp2[i] ) * fpenalty;
841                 fgcp2[i] = 0.5 * ( 1.0 - fgcp2[i] ) * fpenalty;
842         }
843 #if 0
844         for( i=0; i<lgth1; i++ ) 
845                 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
846 #endif
847
848         currentw = w1;
849         previousw = w2;
850
851
852         match_calc( initverticalw, cpmx2, cpmx1, 0, lgth1, floatwork, intwork, 1 );
853
854
855         match_calc( currentw, cpmx1, cpmx2, 0, lgth2, floatwork, intwork, 1 );
856 #if 0 // -> tbfast.c
857         if( localhom )
858                 imp_match_calc( currentw, icyc, jcyc, lgth1, lgth2, seq1, seq2, eff1, eff2, localhom, 1, 0 );
859
860 #endif
861
862         if( outgap == 1 )
863         {
864                 for( i=1; i<lgth1+1; i++ )
865                 {
866                         initverticalw[i] += ( ogcp1[0] + fgcp1[i-1] ) ;
867                 }
868                 for( j=1; j<lgth2+1; j++ )
869                 {
870                         currentw[j] += ( ogcp2[0] + fgcp2[j-1] ) ;
871                 }
872         }
873 #if OUTGAP0TRY
874         else
875         {
876                 for( j=1; j<lgth2+1; j++ )
877                         currentw[j] -= offset * j / 2.0;
878                 for( i=1; i<lgth1+1; i++ )
879                         initverticalw[i] -= offset * i / 2.0;
880         }
881 #endif
882
883         for( j=1; j<lgth2+1; ++j ) 
884         {
885                 m[j] = currentw[j-1] + ogcp1[1]; mp[j] = 0;
886         }
887
888         lastverticalw[0] = currentw[lgth2-1];
889
890         if( outgap ) lasti = lgth1+1; else lasti = lgth1;
891
892 #if XXXXXXX
893 fprintf( stderr, "currentw = \n" );
894 for( i=0; i<lgth1+1; i++ )
895 {
896         fprintf( stderr, "%5.2f ", currentw[i] );
897 }
898 fprintf( stderr, "\n" );
899 fprintf( stderr, "initverticalw = \n" );
900 for( i=0; i<lgth2+1; i++ )
901 {
902         fprintf( stderr, "%5.2f ", initverticalw[i] );
903 }
904 fprintf( stderr, "\n" );
905 fprintf( stderr, "fcgp\n" );
906 for( i=0; i<lgth1; i++ ) 
907         fprintf( stderr, "fgcp1[%d]=%f\n", i, ogcp1[i] );
908 for( i=0; i<lgth2; i++ ) 
909         fprintf( stderr, "fgcp2[%d]=%f\n", i, ogcp2[i] );
910 #endif
911
912         for( i=1; i<lasti; i++ )
913         {
914                 wtmp = previousw; 
915                 previousw = currentw;
916                 currentw = wtmp;
917
918                 previousw[0] = initverticalw[i-1];
919
920                 match_calc( currentw, cpmx1, cpmx2, i, lgth2, floatwork, intwork, 0 );
921 #if XXXXXXX
922 fprintf( stderr, "\n" );
923 fprintf( stderr, "i=%d\n", i );
924 fprintf( stderr, "currentw = \n" );
925 for( j=0; j<lgth2; j++ )
926 {
927         fprintf( stderr, "%5.2f ", currentw[j] );
928 }
929 fprintf( stderr, "\n" );
930 #endif
931                 if( localhom )
932                 {
933 //                      fprintf( stderr, "Calling imp_match_calc (o) lgth = %d, i = %d\n", lgth1, i );
934 //                      imp_match_out_vead( currentw, i, lgth2 );
935                         part_imp_match_out_vead_gapmap( currentw, gapmap1[i]+start1, lgth2, start2, gapmap2 );
936                 }
937 #if XXXXXXX
938 fprintf( stderr, "\n" );
939 fprintf( stderr, "i=%d\n", i );
940 fprintf( stderr, "currentw = \n" );
941 for( j=0; j<lgth2; j++ )
942 {
943         fprintf( stderr, "%5.2f ", currentw[j] );
944 }
945 fprintf( stderr, "\n" );
946 #endif
947                 currentw[0] = initverticalw[i];
948
949
950                 mi = previousw[0] + ogcp2[1]; mpi = 0;
951
952                 ijppt = ijp[i] + 1;
953                 mjpt = m + 1;
954                 prept = previousw;
955                 curpt = currentw + 1;
956                 mpjpt = mp + 1;
957                 fgcp2pt = fgcp2;
958                 ogcp2pt = ogcp2+1;
959                 fgcp1va = fgcp1[i-1];
960                 ogcp1va = ogcp1[i];
961                 for( j=1; j<lgth2+1; j++ )
962                 {
963                         wm = *prept;
964                         *ijppt = 0;
965
966 #if 0
967                         fprintf( stderr, "%5.0f->", wm );
968 #endif
969                         g = mi + *fgcp2pt;
970 #if 0
971                         fprintf( stderr, "%5.0f?", g );
972 #endif
973                         if( g > wm )
974                         {
975                                 wm = g;
976                                 *ijppt = -( j - mpi );
977                         }
978                         g = *prept + *ogcp2pt;
979                         if( g >= mi )
980                         {
981                                 mi = g;
982                                 mpi = j-1;
983                         }
984 #if USE_PENALTY_EX
985                         mi += fpenalty_ex;
986 #endif
987
988                         g = *mjpt + fgcp1va;
989 #if 0 
990                         fprintf( stderr, "%5.0f?", g );
991 #endif
992                         if( g > wm )
993                         {
994                                 wm = g;
995                                 *ijppt = +( i - *mpjpt );
996                         }
997                         g = *prept + ogcp1va;
998                         if( g >= *mjpt )
999                         {
1000                                 *mjpt = g;
1001                                 *mpjpt = i-1;
1002                         }
1003 #if USE_PENALTY_EX
1004                         m[j] += fpenalty_ex;
1005 #endif
1006
1007 #if 0
1008                         fprintf( stderr, "%5.0f ", wm );
1009 #endif
1010                         *curpt += wm;
1011                         ijppt++;
1012                         mjpt++;
1013                         prept++;
1014                         mpjpt++;
1015                         curpt++;
1016                         fgcp2pt++;
1017                         ogcp2pt++;
1018                 }
1019                 lastverticalw[i] = currentw[lgth2-1];
1020         }
1021
1022 #if OUTGAP0TRY
1023         if( !outgap )
1024         {
1025                 for( j=1; j<lgth2+1; j++ )
1026                         currentw[j] -= offset * ( lgth2 - j ) / 2.0;
1027                 for( i=1; i<lgth1+1; i++ )
1028                         lastverticalw[i] -= offset * ( lgth1 - i  / 2.0);
1029         }
1030 #endif
1031                 
1032         /*
1033         fprintf( stderr, "\n" );
1034         for( i=0; i<icyc; i++ ) fprintf( stderr,"%s\n", seq1[i] );
1035         fprintf( stderr, "#####\n" );
1036         for( j=0; j<jcyc; j++ ) fprintf( stderr,"%s\n", seq2[j] );
1037         fprintf( stderr, "====>" );
1038         for( i=0; i<icyc; i++ ) strcpy( mseq1[i], seq1[i] );
1039         for( j=0; j<jcyc; j++ ) strcpy( mseq2[j], seq2[j] );
1040         */
1041         if( localhom )
1042         {
1043                 Atracking_localhom( impmatch, currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc, start1, end1, start2, end2, gapmap1, gapmap2 );
1044         }
1045         else
1046                 Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
1047
1048 //      fprintf( stderr, "### impmatch = %f\n", *impmatch );
1049
1050         resultlen = strlen( mseq1[0] );
1051         if( alloclen < resultlen || resultlen > N )
1052         {
1053                 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
1054                 ErrorExit( "LENGTH OVER!\n" );
1055         }
1056
1057
1058         for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
1059         for( j=0; j<jcyc; j++ ) strcpy( seq2[j], mseq2[j] );
1060         /*
1061         fprintf( stderr, "\n" );
1062         for( i=0; i<icyc; i++ ) fprintf( stderr, "%s\n", mseq1[i] );
1063         fprintf( stderr, "#####\n" );
1064         for( j=0; j<jcyc; j++ ) fprintf( stderr, "%s\n", mseq2[j] );
1065         */
1066
1067
1068         return( wm );
1069 }
1070