Next version of JABA
[jabaws.git] / binaries / src / mafft / core / MSalign11.c.m
1 #include "mltaln.h"
2 #include "dp.h"
3
4 #define MEMSAVE 1
5
6 #define DEBUG 1
7 #define USE_PENALTY_EX  0
8 #define STOREWM 1
9
10 #define DPTANNI 10
11
12
13 static int reccycle = 0;
14
15
16 static void match_calc( float *match, float **cpmx1, float **cpmx2, int i1, int lgth2, float **floatwork, int **intwork, int initialize )
17 {
18         int j, k, l;
19         float scarr[26];
20         float **cpmxpd = floatwork;
21         int **cpmxpdn = intwork;
22         int count = 0;
23         float *matchpt;
24         float **cpmxpdpt;
25         int **cpmxpdnpt;
26         int cpkd;
27
28         if( initialize )
29         {
30                 for( j=0; j<lgth2; j++ )
31                 {
32                         count = 0;
33                         for( l=0; l<26; l++ )
34                         {
35                                 if( cpmx2[j][l] )
36                                 {
37                                         cpmxpd[j][count] = cpmx2[j][l];
38                                         cpmxpdn[j][count] = l;
39                                         count++;
40                                 }
41                         }
42                         cpmxpdn[j][count] = -1;
43                 }
44         }
45
46         for( l=0; l<26; l++ )
47         {
48                 scarr[l] = 0.0;
49                 for( k=0; k<26; k++ )
50                 {
51                         scarr[l] += n_dis[k][l] * cpmx1[i1][k];
52                 }
53         }
54 #if 0 /* ¤³¤ì¤ò»È¤¦¤È¤\ad¤Ïfloatwork¤Î¥¢¥í¥±¡¼¥È¤òµÕ¤Ë¤¹¤ë */
55         {
56                 float *fpt, **fptpt, *fpt2;
57                 int *ipt, **iptpt;
58                 fpt2 = match;
59                 iptpt = cpmxpdn;
60                 fptpt = cpmxpd;
61                 while( lgth2-- )
62                 {
63                         *fpt2 = 0.0;
64                         ipt=*iptpt,fpt=*fptpt;
65                         while( *ipt > -1 )
66                                 *fpt2 += scarr[*ipt++] * *fpt++;
67                         fpt2++,iptpt++,fptpt++;
68                 } 
69         }
70         for( j=0; j<lgth2; j++ )
71         {
72                 match[j] = 0.0;
73                 for( k=0; cpmxpdn[j][k]>-1; k++ )
74                         match[j] += scarr[cpmxpdn[j][k]] * cpmxpd[j][k];
75         } 
76 #else
77         matchpt = match;
78         cpmxpdnpt = cpmxpdn;
79         cpmxpdpt = cpmxpd;
80         while( lgth2-- )
81         {
82                 *matchpt = 0.0;
83                 for( k=0; (cpkd=(*cpmxpdnpt)[k])>-1; k++ )
84                         *matchpt += scarr[cpkd] * (*cpmxpdpt)[k];
85                 matchpt++;
86                 cpmxpdnpt++;
87                 cpmxpdpt++;
88         }
89 #endif
90 }
91
92 static float Atracking( 
93                                                 char **seq1, char **seq2, 
94                         char **mseq1, char **mseq2, 
95                         int **ijp, int icyc, int jcyc,
96                                                 int ist, int ien, int jst, int jen )
97 {
98         int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k, klim;
99         char gap = '-';
100         char *gaptable1, *gt1bk;
101         char *gaptable2, *gt2bk;
102         lgth1 = ien-ist+1;
103         lgth2 = jen-jst+1;
104
105         gt1bk = AllocateCharVec( lgth1+lgth2+1 );
106         gt2bk = AllocateCharVec( lgth1+lgth2+1 );
107
108 #if 0
109         for( i=0; i<lgth1; i++ ) 
110         {
111                 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
112         }
113 #endif
114
115
116 //      fprintf( stderr, "in Atracking, lgth1=%d, lgth2=%d\n", lgth1, lgth2 );
117  
118     for( i=0; i<lgth1+1; i++ ) 
119     {
120         ijp[i][0] = i + 1;
121     }
122     for( j=0; j<lgth2+1; j++ ) 
123     {
124         ijp[0][j] = -( j + 1 );
125     }
126
127
128         gaptable1 = gt1bk + lgth1+lgth2;
129         *gaptable1 = 0;
130         gaptable2 = gt2bk + lgth1+lgth2;
131         *gaptable2 = 0;
132
133 //      if( lgth2 == 1 ) fprintf( stderr, "in Atracking, mseq1 = %s, mseq2 = %s\n", mseq1[0], mseq2[0] );
134
135         iin = lgth1; jin = lgth2;
136         klim = lgth1+lgth2;
137         for( k=0; k<=klim; k++ ) 
138         {
139                 if( ijp[iin][jin] < 0 ) 
140                 {
141                         ifi = iin-1; jfi = jin+ijp[iin][jin];
142                 }
143                 else if( ijp[iin][jin] > 0 )
144                 {
145                         ifi = iin-ijp[iin][jin]; jfi = jin-1;
146                 }
147                 else
148                 {
149                         ifi = iin-1; jfi = jin-1;
150                 }
151                 l = iin - ifi;
152                 while( --l ) 
153                 {
154                         *--gaptable1 = 'o';
155                         *--gaptable2 = '-';
156                         k++;
157                 }
158                 l= jin - jfi;
159                 while( --l )
160                 {
161                         *--gaptable1 = '-';
162                         *--gaptable2 = 'o';
163                         k++;
164                 }
165                 if( iin <= 0 || jin <= 0 ) break;
166                 *--gaptable1 = 'o';
167                 *--gaptable2 = 'o';
168                 k++;
169                 iin = ifi; jin = jfi;
170
171         }
172
173         for( i=0; i<icyc; i++ ) gapireru( mseq1[i], seq1[i]+ist, gaptable1 );
174         for( j=0; j<jcyc; j++ ) gapireru( mseq2[j], seq2[j]+jst, gaptable2 );
175
176         free( gt1bk );
177         free( gt2bk );
178
179 //      fprintf( stderr, "in Atracking (owari), mseq1 = %s\n", mseq1[0] );
180 //      fprintf( stderr, "in Atracking (owari), mseq2 = %s\n", mseq2[0] );
181         return( 0.0 );
182 }
183
184 static float MSalign11_tanni( int icyc, int jcyc, double *eff1, double *eff2, char **seq1, char **seq2, float **cpmx1, float **cpmx2, int ist, int ien, int jst, int jen, int alloclen, char **mseq1, char **mseq2, float **gapinfo )
185 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
186 {
187 //      int k;
188         register int i, j;
189         int ll1, ll2;
190         int lasti, lastj;
191         int resultlen;
192         float wm = 0.0;   /* int ?????? */
193         float g;
194         float *currentw, *previousw;
195         float fpenalty_ex = (float)penalty_ex;
196 #if 1
197         float *wtmp;
198         int *ijppt;
199         float *mjpt, *prept, *curpt;
200         int *mpjpt;
201 #endif
202         float mi, *m;
203         int **ijp;
204         int mpi, *mp;
205         float *w1, *w2;
206         float *initverticalw;    /* kufuu sureba iranai */
207         float *lastverticalw;    /* kufuu sureba iranai */
208         int **intwork;
209         float **floatwork;
210         int **intmtx;
211         float dumfl;
212         int lgth1, lgth2;
213         float *ogcp1;
214         float *fgcp1;
215         float *ogcp2;
216         float *fgcp2;
217         char **aseq1;
218         char **aseq2;
219         char **aseq1bk, **aseq2bk;
220
221         ogcp1 = gapinfo[0] + ist;
222         fgcp1 = gapinfo[1] + ist;
223         ogcp2 = gapinfo[2] + jst;
224         fgcp2 = gapinfo[3] + jst;
225
226 #if STOREWM
227         char ttt1[10000], ttt2[10000];
228 #endif
229
230
231         lgth1 = ien-ist+1;
232         lgth2 = jen-jst+1;
233
234 #if STOREWM
235         strncpy( ttt1, seq1[0]+ist, lgth1 ); ttt1[lgth1] = 0;
236         strncpy( ttt2, seq2[0]+jst, lgth2 ); ttt2[lgth2] = 0;
237
238         fprintf( stderr, "in _tanni ist,ien = %d,%d, lgth1=%d\n", ist, ien, lgth1 );
239         fprintf( stderr, "in _tanni jst,jen = %d,%d, lgth2=%d\n", jst, jen, lgth2 );
240         fprintf( stderr, "ttt1 = %s\n", ttt1 );
241         fprintf( stderr, "ttt2 = %s\n", ttt2 );
242 #endif
243
244
245         ll1 = ( (int)(lgth1) ) + 100;
246         ll2 = ( (int)(lgth2) ) + 100;
247
248 //      aseq1 = AllocateCharMtx( icyc, 0 );
249 //      aseq2 = AllocateCharMtx( jcyc, 0 );
250 //      aseq1bk = AllocateCharMtx( icyc, lgth1+lgth2+100 );
251 //      aseq2bk = AllocateCharMtx( jcyc, lgth1+lgth2+100 );
252 //      for( i=0; i<icyc; i++ ) aseq1[i] = aseq1bk[i];
253 //      for( i=0; i<jcyc; i++ ) aseq2[i] = aseq2bk[i];
254
255         w1 = AllocateFloatVec( ll2+2 );
256         w2 = AllocateFloatVec( ll2+2 );
257
258         initverticalw = AllocateFloatVec( ll1+2 );
259         lastverticalw = AllocateFloatVec( ll1+2 );
260
261         m = AllocateFloatVec( ll2+2 );
262         mp = AllocateIntVec( ll2+2 );
263
264         floatwork = AllocateFloatMtx( MAX( ll1, ll2 )+2, 27 ); 
265         intwork = AllocateIntMtx( MAX( ll1, ll2 )+2, 27 ); 
266
267
268         intmtx = AllocateIntMtx( ll1+1, ll2+1 );
269
270         ijp = intmtx;
271
272         currentw = w1;
273         previousw = w2;
274
275         match_calc( initverticalw, cpmx2+jst, cpmx1+ist, 0, lgth1, floatwork, intwork, 1 );
276
277         match_calc( currentw, cpmx1+ist, cpmx2+jst, 0, lgth2, floatwork, intwork, 1 );
278
279         for( i=1; i<lgth1+1; i++ )
280         {
281                 initverticalw[i] += ( ogcp1[0] + fgcp1[i-1] );
282         }
283         for( j=1; j<lgth2+1; j++ )
284         {
285                 currentw[j] += ( ogcp2[0] + fgcp2[j-1] );
286         }
287
288         for( j=1; j<lgth2+1; ++j ) 
289         {
290                 m[j] = currentw[j-1] + ogcp1[1]; mp[j] = 0;;
291         }
292
293         lastverticalw[0] = currentw[lgth2-1];
294
295
296
297         lasti = lgth1+1;
298         for( i=1; i<lasti; i++ )
299         {
300
301                 wtmp = previousw; 
302                 previousw = currentw;
303                 currentw = wtmp;
304
305                 previousw[0] = initverticalw[i-1];
306
307                 match_calc( currentw, cpmx1+ist, cpmx2+jst, i, lgth2, floatwork, intwork, 0 );
308                 currentw[0] = initverticalw[i];
309
310                 mi = previousw[0] + ogcp2[1]; 
311                 mpi = 0;
312
313                 ijppt = ijp[i] + 1;
314                 mjpt = m + 1;
315                 prept = previousw;
316                 curpt = currentw + 1;
317                 mpjpt = mp + 1;
318                 lastj = lgth2+1;
319                 for( j=1; j<lastj; j++ )
320                 {
321                         wm = *prept;
322                         *ijppt = 0;
323
324 #if 0
325                         fprintf( stderr, "%5.0f->", wm );
326 #endif
327                         g = mi + fgcp2[j-1];
328 #if 0
329                         fprintf( stderr, "%5.0f?", g );
330 #endif
331                         if( g > wm )
332                         {
333                                 wm = g;
334                                 *ijppt = -( j - mpi );
335                         }
336                         g = *prept + ogcp2[j];
337                         if( g >= mi )
338                         {
339                                 mi = g;
340                                 mpi = j-1;
341                         }
342 #if USE_PENALTY_EX
343                         mi += fpenalty_ex;
344 #endif
345
346                         g = *mjpt + fgcp1[i-1];
347 #if 0 
348                         fprintf( stderr, "%5.0f?", g );
349 #endif
350                         if( g > wm )
351                         {
352                                 wm = g;
353                                 *ijppt = +( i - *mpjpt );
354                         }
355                         g = *prept + ogcp1[i];
356                         if( g >= *mjpt )
357                         {
358                                 *mjpt = g;
359                                 *mpjpt = i-1;
360                         }
361 #if USE_PENALTY_EX
362                         m[j] += fpenalty_ex;
363 #endif
364
365 #if 0
366                         fprintf( stderr, "%5.0f ", wm );
367 #endif
368                         *curpt += wm;
369
370
371                         ijppt++;
372                         mjpt++;
373                         prept++;
374                         mpjpt++;
375                         curpt++;
376                 }
377                 lastverticalw[i] = currentw[lgth2-1];
378         }
379
380 //      fprintf( stderr, "wm = %f\n", wm );
381
382         Atracking( seq1, seq2, mseq1, mseq2, ijp, icyc, jcyc, ist, ien, jst, jen );
383
384 //      for( i=0; i<icyc; i++ ) strcpy( mseq1[i], aseq1[i] );
385 //      for( i=0; i<jcyc; i++ ) strcpy( mseq2[i], aseq2[i] );
386
387 //      fprintf( stderr, "in _tanni, aseq1 = %s\n", aseq1[0] );
388 //      fprintf( stderr, "in _tanni, mseq1 = %s\n", mseq1[0] );
389
390         FreeFloatVec( w1 );
391         FreeFloatVec( w2 );
392         FreeFloatVec( initverticalw );
393         FreeFloatVec( lastverticalw );
394
395         FreeFloatVec( m );
396         FreeIntVec( mp );
397
398
399         FreeFloatMtx( floatwork );
400         FreeIntMtx( intwork );
401
402         FreeIntMtx( intmtx );
403
404 //      FreeCharMtx( aseq1bk );
405 //      FreeCharMtx( aseq2bk );
406
407 //      free( aseq1 );
408 //      free( aseq2 );
409
410         return( wm );
411
412 }
413
414 static float MSalign11_rec( int icyc, int jcyc, double *eff1, double *eff2, char **seq1, char **seq2, float **cpmx1, float **cpmx2, int ist, int ien, int jst, int jen, int alloclen, char **mseq1, char **mseq2, int depth, float **gapinfo )
415 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
416 {
417 //      int k;
418         int alnlen;
419         float value = 0.0;
420         register int i, j;
421         char **aseq1, **aseq2;
422         int ll1, ll2, l, len;
423         int lasti, lastj, imid, jmid;
424         int resultlen;
425         float wm = 0.0;   /* int ?????? */
426         float g;
427         float *currentw, *previousw;
428         float fpenalty_ex = (float)penalty_ex;
429         float *wtmp;
430 //      short *ijppt;
431         int *mpjpt;
432 //      short **ijp;
433         int *mp;
434         int mpi;
435         float *mjpt, *prept, *curpt;
436         float mi;
437         float *m;
438         float *w1, *w2;
439 //      float *match;
440         float *initverticalw;    /* kufuu sureba iranai */
441         float *lastverticalw;    /* kufuu sureba iranai */
442         int **intwork;
443         float **floatwork;
444 //      short **shortmtx;
445 #if STOREWM
446         float **WMMTX;
447         float **WMMTX2;
448 #endif
449         float *midw;
450         float *midm;
451         float *midn;
452         float dumfl;
453         int lgth1, lgth2;
454         float maxwm;
455         int *jumpforwi;
456         int *jumpforwj;
457         int *jumpbacki;
458         int *jumpbackj;
459         int *jumpdummi; //muda
460         int *jumpdummj; //muda
461         int jumpi, jumpj;
462         char *gaps;
463         int ijpi, ijpj;
464         float *ogcp1;
465         float *fgcp1;
466         float *ogcp2;
467         float *fgcp2;
468         float firstm;
469         int firstmp;
470 #if STOREWM
471         static char ttt1[50000];
472         static char ttt2[50000];
473 #endif
474
475         ogcp1 = gapinfo[0] + ist;
476         fgcp1 = gapinfo[1] + ist;
477         ogcp2 = gapinfo[2] + jst;
478         fgcp2 = gapinfo[3] + jst;
479
480         depth++;
481         reccycle++;
482
483         lgth1 = ien-ist+1;
484         lgth2 = jen-jst+1;
485
486 //      if( lgth1 < 5 )
487 //              fprintf( stderr, "\nWARNING: lgth1 = %d\n", lgth1 );
488 //      if( lgth2 < 5 )
489 //              fprintf( stderr, "\nWARNING: lgth2 = %d\n", lgth2 );
490 //
491
492 #if STOREWM
493         fprintf( stderr, "==== MSalign (depth=%d, reccycle=%d), ist=%d, ien=%d, jst=%d, jen=%d\n", depth, reccycle, ist, ien, jst, jen );
494         strncpy( ttt1, seq1[0]+ist, lgth1 );
495         strncpy( ttt2, seq2[0]+jst, lgth2 );
496         ttt1[lgth1] = 0;
497         ttt2[lgth2] = 0;
498         fprintf( stderr, "seq1 = %s\n", ttt1 );
499         fprintf( stderr, "seq2 = %s\n", ttt2 );
500 #endif
501         if( lgth2 <= 0 ) // lgth1 <= 0 ha?
502         {
503 //              fprintf( stderr, "\n\n==== jimei\n\n" );
504 //              exit( 1 );
505                 for( i=0; i<icyc; i++ ) 
506                 {
507                         strncpy( mseq1[i], seq1[i]+ist, lgth1 );
508                         mseq1[i][lgth1] = 0;
509                 }
510                 for( i=0; i<jcyc; i++ ) 
511                 {
512                         mseq2[i][0] = 0;
513                         for( j=0; j<lgth1; j++ )
514                                 strcat( mseq2[i], "-" );
515                 }
516
517 //              fprintf( stderr, "==== mseq1[0] (%d) = %s\n", depth, mseq1[0] );
518 //              fprintf( stderr, "==== mseq2[0] (%d) = %s\n", depth, mseq2[0] );
519
520                 return( 0.0 );
521         }
522
523 #if MEMSAVE
524         aseq1 = AllocateCharMtx( icyc, 0 );
525         aseq2 = AllocateCharMtx( jcyc, 0 );
526         for( i=0; i<icyc; i++ ) aseq1[i] = mseq1[i];
527         for( i=0; i<jcyc; i++ ) aseq2[i] = mseq2[i];
528 #else
529         aseq1 = AllocateCharMtx( icyc, lgth1+lgth2+100 );
530         aseq2 = AllocateCharMtx( jcyc, lgth1+lgth2+100 );
531 #endif
532
533 //  if( lgth1 < DPTANNI && lgth2 < DPTANNI ) // & dato lgth ==1 no kanousei ga arunode yokunai 
534 //    if( lgth1 < DPTANNI ) // kore mo lgth2 ga mijikasugiru kanousei ari
535     if( lgth1 < DPTANNI || lgth2 < DPTANNI ) // zettai ni anzen ka?
536         {
537 //              fprintf( stderr, "==== Going to _tanni\n" );
538
539                 value = MSalign11_tanni( icyc, jcyc, eff1, eff2, seq1, seq2, cpmx1, cpmx2, ist, ien, jst, jen, alloclen, aseq1, aseq2, gapinfo );       
540
541
542 #if MEMSAVE
543                 free( aseq1 );
544                 free( aseq2 );
545 #else
546                 for( i=0; i<icyc; i++ ) strcpy( mseq1[i], aseq1[i] );
547                 for( i=0; i<jcyc; i++ ) strcpy( mseq2[i], aseq2[i] );
548
549                 FreeCharMtx( aseq1 );
550                 FreeCharMtx( aseq2 );
551 #endif
552
553 //              fprintf( stderr, "value = %f\n", value );
554
555                 return( value );
556         }
557 //      fprintf( stderr, "Trying to divide the mtx\n" );
558
559         ll1 = ( (int)(lgth1) ) + 100;
560         ll2 = ( (int)(lgth2) ) + 100;
561
562 //      fprintf( stderr, "ll1,ll2=%d,%d\n", ll1, ll2 );
563
564         w1 = AllocateFloatVec( ll2+2 );
565         w2 = AllocateFloatVec( ll2+2 );
566 //      match = AllocateFloatVec( ll2+2 );
567         midw = AllocateFloatVec( ll2+2 );
568         midn = AllocateFloatVec( ll2+2 );
569         midm = AllocateFloatVec( ll2+2 );
570         jumpbacki = AllocateIntVec( ll2+2 );
571         jumpbackj = AllocateIntVec( ll2+2 );
572         jumpforwi = AllocateIntVec( ll2+2 );
573         jumpforwj = AllocateIntVec( ll2+2 );
574         jumpdummi = AllocateIntVec( ll2+2 );
575         jumpdummj = AllocateIntVec( ll2+2 );
576
577         initverticalw = AllocateFloatVec( ll1+2 );
578         lastverticalw = AllocateFloatVec( ll1+2 );
579
580         m = AllocateFloatVec( ll2+2 );
581         mp = AllocateIntVec( ll2+2 );
582         gaps = AllocateCharVec( MAX( ll1, ll2 ) + 2 );
583
584         floatwork = AllocateFloatMtx( MAX( ll1, ll2 )+2, 26 ); 
585         intwork = AllocateIntMtx( MAX( ll1, ll2 )+2, 26 ); 
586
587 #if DEBUG
588         fprintf( stderr, "succeeded\n" );
589 #endif
590
591 #if STOREWM
592         WMMTX = AllocateFloatMtx( ll1, ll2 );
593         WMMTX2 = AllocateFloatMtx( ll1, ll2 );
594 #endif
595 #if 0
596         shortmtx = AllocateShortMtx( ll1, ll2 );
597
598 #if DEBUG
599         fprintf( stderr, "succeeded\n\n" );
600 #endif
601
602         ijp = shortmtx;
603 #endif
604
605         currentw = w1;
606         previousw = w2;
607
608         match_calc( initverticalw, cpmx2+jst, cpmx1+ist, 0, lgth1, floatwork, intwork, 1 );
609
610         match_calc( currentw, cpmx1+ist, cpmx2+jst, 0, lgth2, floatwork, intwork, 1 );
611
612         for( i=1; i<lgth1+1; i++ )
613         {
614                 initverticalw[i] += ( ogcp1[0] + fgcp1[i-1] );
615         }
616         for( j=1; j<lgth2+1; j++ )
617         {
618                 currentw[j] += ( ogcp2[0] + fgcp2[j-1] );
619         }
620
621 #if STOREWM
622         WMMTX[0][0] = initverticalw[0];
623         for( i=1; i<lgth1+1; i++ )
624         {
625                 WMMTX[i][0] = initverticalw[i];
626         }
627         for( j=1; j<lgth2+1; j++ )
628         {
629                 WMMTX[0][j] = currentw[j];
630         }
631 #endif
632
633
634         for( j=1; j<lgth2+1; ++j ) 
635         {
636                 m[j] = currentw[j-1] + ogcp1[1];
637 //              m[j] = currentw[j-1];
638                 mp[j] = 0;
639         }
640
641         lastverticalw[0] = currentw[lgth2-1];
642
643         imid = lgth1 * 0.5;
644
645         jumpi = 0; // atode kawaru.
646         lasti = lgth1+1;
647 #if STOREWM
648         for( i=1; i<lasti; i++ )
649 #else
650         for( i=1; i<=imid; i++ )
651 #endif
652         {
653                 wtmp = previousw; 
654                 previousw = currentw;
655                 currentw = wtmp;
656
657                 previousw[0] = initverticalw[i-1];
658
659                 match_calc( currentw, cpmx1+ist, cpmx2+jst, i, lgth2, floatwork, intwork, 0 );
660                 currentw[0] = initverticalw[i];
661
662                 m[0] = ogcp1[i];
663 #if STOREM
664                 WMMTX2[i][0] = m[0];
665 #endif
666                 if( i == imid ) midm[0] = m[0];
667
668                 mi = previousw[0] + ogcp2[1]; 
669 //              mi = previousw[0];
670                 mpi = 0;
671
672
673 //              ijppt = ijp[i] + 1;
674                 mjpt = m + 1;
675                 prept = previousw;
676                 curpt = currentw + 1;
677                 mpjpt = mp + 1;
678
679
680                 lastj = lgth2+1;
681                 for( j=1; j<lastj; j++ )
682                 {
683
684                         wm = *prept;
685
686 #if 0
687                         fprintf( stderr, "%5.0f->", wm );
688 #endif
689                         g = mi + fgcp2[j-1];
690 //                      g = mi + fpenalty;
691 #if 0
692                         fprintf( stderr, "%5.0f?", g );
693 #endif
694                         if( g > wm )
695                         {
696                                 wm = g;
697 //                              *ijppt = -( j - mpi );
698                         }
699                         g = *prept + ogcp2[j];
700 //                      g = *prept;
701                         if( g >= mi )
702                         {
703                                 mi = g;
704                                 mpi = j-1;
705                         }
706 #if USE_PENALTY_EX
707                         mi += fpenalty_ex;
708 #endif
709
710                         g = *mjpt + fgcp1[i-1];
711 //                      g = *mjpt + fpenalty;
712 #if 0 
713                         fprintf( stderr, "%5.0f?", g );
714 #endif
715                         if( g > wm )
716                         {
717                                 wm = g;
718 //                              *ijppt = +( i - *mpjpt );
719                         }
720
721
722                         g = *prept + ogcp1[i];
723 //                      g = *prept;
724                         if( g >= *mjpt )
725                         {
726                                 *mjpt = g;
727                                 *mpjpt = i-1;
728                         }
729 #if USE_PENALTY_EX
730                         m[j] += fpenalty_ex;
731 #endif
732
733 #if 0
734                         fprintf( stderr, "%5.0f ", wm );
735 #endif
736                         *curpt += wm;
737
738 #if STOREWM
739                         WMMTX[i][j] = *curpt;
740                         WMMTX2[i][j] = *mjpt;
741 #endif
742
743                         if( i == imid ) //muda
744                         {       
745                                 jumpbackj[j] = *mpjpt; // muda atode matomeru
746                                 jumpbacki[j] = mpi; // muda atode matomeru
747 //                              fprintf( stderr, "jumpbackj[%d] in forward dp is %d\n", j, *mpjpt );
748 //                              fprintf( stderr, "jumpbacki[%d] in forward dp is %d\n", j, mpi );
749                                 midw[j] = *curpt;
750                                 midm[j] = *mjpt;
751                                 midn[j] = mi;
752                         }
753
754 //                      fprintf( stderr, "m[%d] = %f\n", j, m[j] );
755                         mjpt++;
756                         prept++;
757                         mpjpt++;
758                         curpt++;
759
760                 }
761                 lastverticalw[i] = currentw[lgth2-1];
762
763 #if STOREWM
764                 WMMTX2[i][lgth2] = m[lgth2-1];
765 #endif
766
767 #if 0  // ue
768                 if( i == imid )
769                 {
770                         for( j=0; j<lgth2; j++ ) midw[j] = currentw[j];
771                         for( j=0; j<lgth2; j++ ) midm[j] = m[j];
772                 }
773 #endif
774         }
775 //      for( j=0; j<lgth2; j++ ) midw[j] = WMMTX[imid][j];
776 //      for( j=0; j<lgth2; j++ ) midm[j] = WMMTX2[imid][j];
777
778 #if 1
779         fprintf( stderr, "WMMTX (1) = \n" );
780     for( i=0; i<lgth1; i++ )
781     {
782         for( j=0; j<lgth2; j++ )
783         {
784             fprintf( stderr, "% 10.2f ", WMMTX[i][j] );
785         }
786         fprintf( stderr, "\n" );
787     }
788         fprintf( stderr, "\n" );
789         fprintf( stderr, "WMMTX2 (1) = \n" );
790     for( i=0; i<lgth1; i++ )
791     {
792         for( j=0; j<lgth2; j++ )
793         {
794             fprintf( stderr, "% 10.2f ", WMMTX2[i][j] );
795         }
796         fprintf( stderr, "\n" );
797     }
798         fprintf( stderr, "\n" );
799 #endif
800
801 // gyakudp
802
803         match_calc( initverticalw, cpmx2+jst, cpmx1+ist, lgth2-1, lgth1, floatwork, intwork, 1 );
804         match_calc( currentw, cpmx1+ist, cpmx2+jst, lgth1-1, lgth2, floatwork, intwork, 1 );
805
806         for( i=0; i<lgth1-1; i++ )
807         {
808                 initverticalw[i] += ( fgcp1[lgth1-1] + ogcp1[i+1] );
809 //              initverticalw[i] += fpenalty;
810         }
811         for( j=0; j<lgth2-1; j++ )
812         {
813                 currentw[j] += ( fgcp2[lgth2-1] + ogcp2[j+1] );
814 //              currentw[j] += fpenalty;
815         }
816
817 #if STOREWM
818         for( i=0; i<lgth1-1; i++ )
819         {
820                 WMMTX[i][lgth2-1] += ( fgcp1[lgth1-1] + ogcp1[i+1] );
821                 fprintf( stderr, "fgcp1[lgth1-1] + ogcp1[i+1] = %f\n", fgcp1[lgth1-1] + ogcp1[i+1] );
822         }
823         for( j=0; j<lgth2-1; j++ )
824         {
825                 WMMTX[lgth1-1][j] += ( fgcp2[lgth2-1] + ogcp2[j+1] );
826                 fprintf( stderr, "fgcp2[lgth2-1] + ogcp2[j+1] = %f\n", fgcp2[lgth2-1] + ogcp2[j+1] );
827         }
828 #endif
829
830
831
832
833
834
835         for( j=lgth2-1; j>0; --j )
836         {
837                 m[j-1] = currentw[j] + fgcp2[lgth2-2];
838 //              m[j-1] = currentw[j];
839                 mp[j] = lgth1-1;
840         }
841
842 //      for( j=0; j<lgth2; j++ ) m[j] = 0.0;
843         // m[lgth2-1] ha irunoka?
844
845
846 //      for( i=lgth1-2; i>=imid; i-- )
847         firstm = -9999999.9;
848         firstmp = lgth1-1;
849         for( i=lgth1-2; i>-1; i-- )
850         {
851                 wtmp = previousw;
852                 previousw = currentw;
853                 currentw = wtmp;
854                 previousw[lgth2-1] = initverticalw[i+1];
855 //              match_calc( currentw, seq1, seq2, i, lgth2 );
856                 match_calc( currentw, cpmx1+ist, cpmx2+jst, i, lgth2, floatwork, intwork, 0 );
857
858                 currentw[lgth2-1] = initverticalw[i];
859
860 //              m[lgth2] = fgcp1[i];
861 //              WMMTX2[i][lgth2] += m[lgth2];
862 //              fprintf( stderr, "m[] = %f\n", m[lgth2] );
863
864                 mi = previousw[lgth2-1] + fgcp2[lgth2-2];
865 //              mi = previousw[lgth2-1];
866                 mpi = lgth2 - 1;
867
868                 mjpt = m + lgth2 - 2;
869                 prept = previousw + lgth2 - 1;
870                 curpt = currentw + lgth2 - 2;
871                 mpjpt = mp + lgth2 - 2;
872
873
874                 for( j=lgth2-2; j>-1; j-- )
875                 {
876                         wm = *prept;
877                         ijpi = i+1;
878                         ijpj = j+1;
879
880                         g = mi + ogcp2[j+1];
881 //                      g = mi + fpenalty;
882                         if( g > wm )
883                         {
884                                 wm = g;
885                                 ijpj = mpi;
886                                 ijpi = i+1;
887                         }
888
889                         g = *prept + fgcp2[j];
890 //                      g = *prept;
891                         if( g >= mi )
892                         {
893 //                              fprintf( stderr, "i,j=%d,%d - renewed! mpi = %d\n", i, j, j+1 );
894                                 mi = g;
895                                 mpi = j + 1;
896                         }
897
898 #if USE_PENALTY_EX
899                         mi += fpenalty_ex;
900 #endif
901
902 //                      fprintf( stderr, "i,j=%d,%d *mpjpt = %d\n", i, j, *mpjpt );
903                         g = *mjpt + ogcp1[i+1];
904 //                      g = *mjpt + fpenalty;
905                         if( g > wm )
906                         {
907                                 wm = g;
908                                 ijpi = *mpjpt;
909                                 ijpj = j+1;
910                         }
911
912 //                      if( i == imid )fprintf( stderr, "i,j=%d,%d \n", i, j );
913                         g = *prept + fgcp1[i];
914 //                      g = *prept;
915                         if( g >= *mjpt )
916                         {
917                                 *mjpt = g;
918                                 *mpjpt = i + 1;
919                         }
920
921 #if USE_PENALTY_EX
922                         m[j] += fpenalty_ex;
923 #endif
924
925                         if( i == jumpi || i == imid - 1 )
926                         {
927                                 jumpforwi[j] = ijpi; //muda
928                                 jumpforwj[j] = ijpj; //muda
929 //                              fprintf( stderr, "jumpfori[%d] = %d\n", j, ijpi );
930 //                              fprintf( stderr, "jumpforj[%d] = %d\n", j, ijpj );
931                         }
932                         if( i == imid ) // muda
933                         {
934                                 midw[j] += wm;
935 //                              midm[j+1] += *mjpt + fpenalty; //??????
936                                 midm[j+1] += *mjpt; //??????
937                         }
938                         if( i == imid - 1 )
939                         {
940 //                              midn[j] += mi + fpenalty;  //????
941                                 midn[j] += mi;  //????
942                         }
943 #if STOREWM
944                         WMMTX[i][j] += wm;
945 //                      WMMTX2[i][j+1] += *mjpt + fpenalty;
946                         WMMTX2[i][j+1] += *mjpt;
947 #endif
948                         *curpt += wm;
949
950                         mjpt--;
951                         prept--;
952                         mpjpt--;
953                         curpt--;
954                 }
955 //              fprintf( stderr, "adding *mjpt (=%f) to WMMTX2[%d][%d]\n", *mjpt, i, j+1 );
956                 g = *prept + fgcp1[i];
957                 if( firstm < g ) 
958                 {
959                         firstm = g;
960                         firstmp = i + 1;
961                 }
962 #if STOREWM
963                 WMMTX2[i][j+1] += firstm;
964 #endif
965                 if( i == imid ) midm[j+1] += firstm;
966
967                 if( i == imid - 1 )     
968                 {
969                         maxwm = midw[1];
970                         jmid = 0;
971 //                      if( depth == 1 ) fprintf( stderr, "maxwm!! = %f\n", maxwm );
972                         for( j=2; j<lgth2-1; j++ )
973                         {
974                                 wm = midw[j];
975                                 if( wm > maxwm )
976                                 {
977                                         jmid = j;
978                                         maxwm = wm;
979                                 }
980 //                              if( depth == 1 ) fprintf( stderr, "maxwm!! = %f\n", maxwm );
981                         }
982                         for( j=0; j<lgth2+1; j++ )
983                         {
984                                 wm = midm[j];
985                                 if( wm > maxwm )
986                                 {
987                                         jmid = j;
988                                         maxwm = wm;
989                                 }
990 //                              if( depth == 1 ) fprintf( stderr, "maxwm!! = %f\n", maxwm );
991                         }
992
993 //                      if( depth == 1 ) fprintf( stderr, "maxwm!! = %f\n", maxwm );
994
995
996 //                      fprintf( stderr, "### imid=%d, jmid=%d\n", imid, jmid );
997                         wm = midw[jmid];
998                         jumpi = imid-1;
999                         jumpj = jmid-1;
1000                         if( jmid > 0 && midn[jmid-1] > wm ) //060413
1001                         {
1002                                 jumpi = imid-1;
1003                                 jumpj = jumpbacki[jmid];
1004                                 wm = midn[jmid-1];
1005 //                              fprintf( stderr, "rejump (n)\n" );
1006                         }
1007                         if( midm[jmid] > wm )
1008                         {
1009                                 jumpi = jumpbackj[jmid];
1010                                 jumpj = jmid-1;
1011                                 wm = midm[jmid];
1012 //                              fprintf( stderr, "rejump (m) jumpi=%d\n", jumpi );
1013                         }
1014
1015
1016 //                      fprintf( stderr, "--> imid=%d, jmid=%d\n", imid, jmid );
1017 //                      fprintf( stderr, "--> jumpi=%d, jumpj=%d\n", jumpi, jumpj );
1018 #if STOREWM
1019                         fprintf( stderr, "imid = %d\n", imid );
1020                         fprintf( stderr, "midn = \n" );
1021                         for( j=0; j<lgth2; j++ )
1022                         {
1023                                 fprintf( stderr, "% 7.1f ", midn[j] );
1024                         }
1025                         fprintf( stderr, "\n" );
1026                         fprintf( stderr, "midw = \n" );
1027                         for( j=0; j<lgth2; j++ )
1028                         {
1029                                 fprintf( stderr, "% 7.1f ", midw[j] );
1030                         }
1031                         fprintf( stderr, "\n" );
1032                         fprintf( stderr, "midm = \n" );
1033                         for( j=0; j<lgth2; j++ )
1034                         {
1035                                 fprintf( stderr, "% 7.1f ", midm[j] );
1036                         }
1037                         fprintf( stderr, "\n" );
1038 #endif
1039 //                      fprintf( stderr, "maxwm = %f\n", maxwm );
1040                 }
1041                 if( i == jumpi ) //saki?
1042                 {
1043 //                      fprintf( stderr, "imid, jumpi = %d,%d\n", imid, jumpi );
1044 //                      fprintf( stderr, "jmid, jumpj = %d,%d\n", jmid, jumpj );
1045                         if( jmid == 0 )
1046                         {
1047 //                              fprintf( stderr, "CHUI2!\n" );
1048                                 jumpj = 0; jmid = 1;
1049                                 jumpi = firstmp - 1;
1050                                 imid = firstmp;
1051                         }
1052
1053 #if 0
1054                         else if( jmid == lgth2 ) 
1055                         {
1056                                 fprintf( stderr, "CHUI1!\n" );
1057                                 jumpi=0; jumpj=0;
1058                                 imid=jumpforwi[0]; jmid=lgth2-1;
1059                         }
1060 #else // 060414
1061                         else if( jmid >= lgth2 ) 
1062                         {
1063 //                              fprintf( stderr, "CHUI1!\n" );
1064                                 jumpi=imid-1; jmid=lgth2;
1065                                 jumpj = lgth2-1;
1066                         }
1067 #endif
1068                         else
1069                         {
1070                                 imid = jumpforwi[jumpj];
1071                                 jmid = jumpforwj[jumpj];
1072                         }
1073 #if 0
1074                         fprintf( stderr, "jumpi -> %d\n", jumpi );
1075                         fprintf( stderr, "jumpj -> %d\n", jumpj );
1076                         fprintf( stderr, "imid -> %d\n", imid );
1077                         fprintf( stderr, "jmid -> %d\n", jmid );
1078 #endif
1079
1080 #if STOREWM
1081 //                      break;
1082 #else
1083                         break;
1084 #endif
1085                 }
1086         }
1087 #if 0
1088                 jumpi=0; jumpj=0;
1089                 imid=lgth1-1; jmid=lgth2-1;
1090         }
1091 #endif
1092
1093 //      fprintf( stderr, "imid = %d, but jumpi = %d\n", imid, jumpi );
1094 //      fprintf( stderr, "jmid = %d, but jumpj = %d\n", jmid, jumpj );
1095
1096 //      for( j=0; j<lgth2; j++ ) midw[j] += currentw[j];
1097 //      for( j=0; j<lgth2; j++ ) midm[j] += m[j+1];
1098 //      for( j=0; j<lgth2; j++ ) midw[j] += WMMTX[imid][j];
1099 //      for( j=0; j<lgth2; j++ ) midw[j] += WMMTX[imid][j];
1100
1101
1102 #if STOREWM
1103         fprintf( stderr, "WMMTX = \n" );
1104     for( i=0; i<lgth1; i++ )
1105     {
1106 //        fprintf( stderr, "%d ", i );
1107         for( j=0; j<lgth2; j++ )
1108         {
1109             fprintf( stderr, "%d %d % 10.2f\n", i, j, WMMTX[i][j] );
1110         }
1111         fprintf( stderr, "\n" );
1112     }
1113         fprintf( stderr, "WMMTX2 = \n" );
1114     for( i=0; i<lgth1; i++ )
1115     {
1116         fprintf( stderr, "%d ", i );
1117         for( j=0; j<lgth2+1; j++ )
1118         {
1119             fprintf( stderr, "% 10.2f", WMMTX2[i][j] );
1120         }
1121         fprintf( stderr, "\n" );
1122     }
1123
1124         fprintf( stderr, "jumpbacki = \n" );
1125         for( j=0; j<lgth2; j++ )
1126         {
1127                 fprintf( stderr, "% 7d ", jumpbacki[j] );
1128         }
1129         fprintf( stderr, "\n" );
1130         fprintf( stderr, "jumpbackj = \n" );
1131         for( j=0; j<lgth2; j++ )
1132         {
1133                 fprintf( stderr, "% 7d ", jumpbackj[j] );
1134         }
1135         fprintf( stderr, "\n" );
1136         fprintf( stderr, "jumpforwi = \n" );
1137         for( j=0; j<lgth2; j++ )
1138         {
1139                 fprintf( stderr, "% 7d ", jumpforwi[j] );
1140         }
1141         fprintf( stderr, "\n" );
1142         fprintf( stderr, "jumpforwj = \n" );
1143         for( j=0; j<lgth2; j++ )
1144         {
1145                 fprintf( stderr, "% 7d ", jumpforwj[j] );
1146         }
1147         fprintf( stderr, "\n" );
1148
1149
1150 #endif
1151
1152
1153 //      Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
1154
1155 #if 0 // irukamo
1156         resultlen = strlen( mseq1[0] );
1157         if( alloclen < resultlen || resultlen > N )
1158         {
1159                 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
1160                 ErrorExit( "LENGTH OVER!\n" );
1161         }
1162 #endif
1163
1164
1165
1166 #if 0
1167         fprintf( stderr, "jumpi = %d, imid = %d\n", jumpi, imid );
1168         fprintf( stderr, "jumpj = %d, jmid = %d\n", jumpj, jmid );
1169
1170         fprintf( stderr, "imid = %d\n", imid );
1171         fprintf( stderr, "jmid = %d\n", jmid );
1172 #endif
1173
1174
1175         FreeFloatVec( w1 );
1176         FreeFloatVec( w2 );
1177         FreeFloatVec( initverticalw );
1178         FreeFloatVec( lastverticalw );
1179         FreeFloatVec( midw );
1180         FreeFloatVec( midm );
1181         FreeFloatVec( midn );
1182
1183         FreeIntVec( jumpbacki );
1184         FreeIntVec( jumpbackj );
1185         FreeIntVec( jumpforwi );
1186         FreeIntVec( jumpforwj );
1187         FreeIntVec( jumpdummi );
1188         FreeIntVec( jumpdummj );
1189
1190         FreeFloatVec( m );
1191         FreeIntVec( mp );
1192
1193         FreeFloatMtx( floatwork );
1194         FreeIntMtx( intwork );
1195
1196 #if STOREWM
1197         FreeFloatMtx( WMMTX );
1198         FreeFloatMtx( WMMTX2 );
1199 #endif
1200
1201 //      fprintf( stderr, "==== calling myself (first)\n" );
1202
1203 #if 0
1204                 fprintf( stderr, "seq1[0] = %s\n", seq1[0] );
1205                 fprintf( stderr, "seq2[0] = %s\n", seq2[0] );
1206 #endif
1207         value = MSalign11_rec( icyc, jcyc, eff1, eff2, seq1, seq2, cpmx1, cpmx2, ist, ist+jumpi, jst, jst+jumpj, alloclen, aseq1, aseq2, depth, gapinfo );      
1208 #if 0
1209                 fprintf( stderr, "aseq1[0] = %s\n", aseq1[0] );
1210                 fprintf( stderr, "aseq2[0] = %s\n", aseq2[0] );
1211 #endif
1212 #if MEMSAVE
1213 #else
1214         for( i=0; i<icyc; i++ ) strcpy( mseq1[i], aseq1[i] );
1215         for( i=0; i<jcyc; i++ ) strcpy( mseq2[i], aseq2[i] );
1216 #endif
1217
1218 //      fprintf( stderr, "====(f) aseq1[0] (%d) = %s (%d-%d)\n", depth, aseq1[0], ist, ien );
1219 //      fprintf( stderr, "====(f) aseq2[0] (%d) = %s (%d-%d)\n", depth, aseq2[0], jst, jen );
1220
1221         len = strlen( mseq1[0] );
1222 //      fprintf( stderr, "len = %d\n", len );
1223         l = jmid - jumpj - 1;
1224 //      fprintf( stderr, "l=%d\n", l );
1225         if( l > 0 )
1226         {
1227                 for( i=0; i<l; i++ ) gaps[i] = '-'; gaps[i] = 0;
1228                 for( i=0; i<icyc; i++ ) 
1229                 {
1230                         strcat( mseq1[i], gaps );
1231                         mseq1[i][len+l] = 0;
1232                 }
1233                 for( j=0; j<jcyc; j++ )
1234                 {
1235                         strncat( mseq2[j], seq2[j]+jst+jumpj+1, l );
1236                         mseq2[j][len+l] = 0;
1237                 }
1238 //              fprintf( stderr, "penalizing (2) .. %f(%d), %f(%d)\n", ogcp2[jumpj+1], jumpj+1, fgcp2[jmid-1], jmid-1 );
1239                 value +=  ( ogcp2[jumpj+1] + fgcp2[jmid-1] );
1240 //              value += fpenalty;
1241         }
1242         len = strlen( mseq1[0] );
1243         l = imid - jumpi - 1;
1244 //      fprintf( stderr, "l=%d\n", l );
1245         if( l > 0 )
1246         {
1247                 for( i=0; i<l; i++ ) gaps[i] = '-'; gaps[i] = 0;
1248                 for( i=0; i<icyc; i++ )
1249                 {
1250                         strncat( mseq1[i], seq1[i]+ist+jumpi+1, l );
1251                         mseq1[i][len+l] = 0;
1252                 }
1253                 for( j=0; j<jcyc; j++ ) 
1254                 {
1255                         strcat( mseq2[j], gaps );
1256                         mseq2[j][len+l] = 0;
1257                 }
1258
1259 //              for( i=0; i<lgth1; i++ ) fprintf( stderr, "ogcp1[%d] = %f\n", i, ogcp1[i] );
1260 //              for( i=0; i<lgth1; i++ ) fprintf( stderr, "fgcp1[%d] = %f\n", i, fgcp1[i] );
1261
1262
1263 //              fprintf( stderr, "penalizing (1) .. ogcp1[%d] = %f, fgcp1[%d] = %f\n", jumpi+1, ogcp1[jumpi+1], imid-1, fgcp1[imid-1] );
1264                 value += ( ogcp1[jumpi+1] + fgcp1[imid-1] );
1265 //              value += fpenalty;
1266         }
1267 #if 0
1268         for( i=0; i<icyc; i++ ) fprintf( stderr, "after gapfill mseq1[%d]=%s\n", i, mseq1[i] );
1269         for( i=0; i<jcyc; i++ ) fprintf( stderr, "after gapfill mseq2[%d]=%s\n", i, mseq2[i] );
1270 #endif
1271
1272 //      fprintf( stderr, "==== calling myself (second)\n" );
1273
1274 #if MEMSAVE
1275         alnlen = strlen( aseq1[0] );
1276         for( i=0; i<icyc; i++ ) aseq1[i] += alnlen;
1277         for( i=0; i<jcyc; i++ ) aseq2[i] += alnlen;
1278 #endif
1279
1280 #if 0
1281                 fprintf( stderr, "seq1[0] = %s\n", seq1[0] );
1282                 fprintf( stderr, "seq2[0] = %s\n", seq2[0] );
1283 #endif
1284         value += MSalign11_rec( icyc, jcyc, eff1, eff2, seq1, seq2, cpmx1, cpmx2, ist+imid, ien, jst+jmid, jen, alloclen, aseq1, aseq2, depth, gapinfo );       
1285 #if 0
1286                 fprintf( stderr, "aseq1[0] = %s\n", aseq1[0] );
1287                 fprintf( stderr, "aseq2[0] = %s\n", aseq2[0] );
1288 #endif
1289
1290
1291
1292 #if DEBUG
1293         if( value - maxwm > 1 || maxwm - value > 1 )
1294         {
1295                 fprintf( stderr, "WARNING value  = %f, but maxwm = %f\n", value, maxwm );
1296                 for( i=0; i<icyc; i++ )
1297                 {
1298                         fprintf( stderr, ">1-%d\n%s\n", i, mseq1[i] );
1299                         fprintf( stderr, "%s\n", aseq1[i] );
1300                 }
1301                 for( i=0; i<jcyc; i++ )
1302                 {
1303                         fprintf( stderr, ">2-%d\n%s\n", i, mseq2[i] );
1304                         fprintf( stderr, "%s\n", aseq2[i] );
1305                 }
1306
1307 //              exit( 1 );
1308         }
1309         else
1310         {
1311                 fprintf( stderr, "value = %.0f, maxwm = %.0f -> ok\n", value, maxwm );
1312         }
1313 #endif
1314
1315 #if MEMSAVE
1316 #else
1317         for( i=0; i<icyc; i++ ) strcat( mseq1[i], aseq1[i] );
1318         for( i=0; i<jcyc; i++ ) strcat( mseq2[i], aseq2[i] );
1319 #endif
1320
1321 //      fprintf( stderr, "====(s) aseq1[0] (%d) = %s (%d-%d)\n", depth, aseq1[0], ist, ien );
1322 //      fprintf( stderr, "====(s) aseq2[0] (%d) = %s (%d-%d)\n", depth, aseq2[0], jst, jen );
1323
1324         free( gaps );
1325 #if MEMSAVE
1326         free( aseq1 );
1327         free( aseq2 );
1328 #else
1329         FreeCharMtx( aseq1 );
1330         FreeCharMtx( aseq2 );
1331 #endif
1332         
1333         return( value );
1334 }
1335
1336
1337
1338 float MSalign11( char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen, char *sgap1, char *sgap2, char *egap1, char *egap2 )
1339 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
1340 {
1341 //      int k;
1342         int i, j;
1343         int ll1, ll2;
1344         int lgth1, lgth2;
1345         float wm = 0.0;   /* int ?????? */
1346         char **mseq1;
1347         char **mseq2;
1348         char **mseq;
1349         float *ogcp1;
1350         float *ogcp2;
1351         float *fgcp1;
1352         float *fgcp2;
1353         float **cpmx1;
1354         float **cpmx2;
1355         float **gapinfo;
1356         float fpenalty = (float)penalty;
1357         int nglen1, nglen2;
1358
1359 #if 0
1360         fprintf( stderr, "eff in SA+++align\n" );
1361         for( i=0; i<icyc; i++ ) fprintf( stderr, "eff1[%d] = %f\n", i, eff1[i] );
1362 #endif
1363
1364         nglen1 = seqlen( seq1[0] );
1365         nglen2 = seqlen( seq2[0] );
1366
1367         lgth1 = strlen( seq1[0] );
1368         lgth2 = strlen( seq2[0] );
1369
1370         ll1 = ( (int)(lgth1) ) + 100;
1371         ll2 = ( (int)(lgth2) ) + 100;
1372
1373         mseq1 = AllocateCharMtx( icyc, ll1+ll2 );
1374         mseq2 = AllocateCharMtx( jcyc, ll1+ll2 );
1375
1376         gapinfo = AllocateFloatMtx( 4, 0 );
1377         ogcp1 = AllocateFloatVec( ll1+2 );
1378         ogcp2 = AllocateFloatVec( ll2+2 );
1379         fgcp1 = AllocateFloatVec( ll1+2 );
1380         fgcp2 = AllocateFloatVec( ll2+2 );
1381
1382
1383         cpmx1 = AllocateFloatMtx( ll1+2, 27 );
1384         cpmx2 = AllocateFloatMtx( ll2+2, 27 );
1385
1386         for( i=0; i<icyc; i++ ) 
1387         {
1388                 if( strlen( seq1[i] ) != lgth1 )
1389                 {
1390                         fprintf( stderr, "i = %d / %d\n", i, icyc );
1391                         fprintf( stderr, "bug! hairetsu ga kowareta!\n" );
1392                         exit( 1 );
1393                 }
1394         }
1395         for( j=0; j<jcyc; j++ )
1396         {
1397                 if( strlen( seq2[j] ) != lgth2 )
1398                 {
1399                         fprintf( stderr, "j = %d / %d\n", j, jcyc );
1400                         fprintf( stderr, "bug! hairetsu ga kowareta!\n" );
1401                         exit( 1 );
1402                 }
1403         }
1404
1405         MScpmx_calc_new( seq1, cpmx1, eff1, lgth1, icyc );
1406         MScpmx_calc_new( seq2, cpmx2, eff2, lgth2, jcyc );
1407
1408
1409 #if 1
1410
1411         if( sgap1 )
1412         {
1413                 new_OpeningGapCount( ogcp1, icyc, seq1, eff1, lgth1, sgap1 );
1414                 new_OpeningGapCount( ogcp2, jcyc, seq2, eff2, lgth2, sgap2 );
1415                 new_FinalGapCount( fgcp1, icyc, seq1, eff1, lgth1, egap2 );
1416                 new_FinalGapCount( fgcp2, jcyc, seq2, eff2, lgth2, egap2 );
1417         }
1418         else
1419         {
1420                 st_OpeningGapCount( ogcp1, icyc, seq1, eff1, lgth1 );
1421                 st_OpeningGapCount( ogcp2, jcyc, seq2, eff2, lgth2 );
1422                 st_FinalGapCount( fgcp1, icyc, seq1, eff1, lgth1 );
1423                 st_FinalGapCount( fgcp2, jcyc, seq2, eff2, lgth2 );
1424         }
1425
1426 #if 1
1427         for( i=0; i<lgth1; i++ ) 
1428         {
1429                 ogcp1[i] = 0.5 * ( 1.0 - ogcp1[i] ) * fpenalty;
1430                 fgcp1[i] = 0.5 * ( 1.0 - fgcp1[i] ) * fpenalty;
1431 //              fprintf( stderr, "fgcp1[%d] = %f\n", i, fgcp1[i] );
1432         }
1433         for( i=0; i<lgth2; i++ ) 
1434         {
1435                 ogcp2[i] = 0.5 * ( 1.0 - ogcp2[i] ) * fpenalty;
1436                 fgcp2[i] = 0.5 * ( 1.0 - fgcp2[i] ) * fpenalty;
1437 //              fprintf( stderr, "fgcp2[%d] = %f\n", i, fgcp2[i] );
1438         }
1439 #else
1440         for( i=0; i<lgth1; i++ ) 
1441         {
1442                 ogcp1[i] = 0.5 * fpenalty;
1443                 fgcp1[i] = 0.5 * fpenalty;
1444         }
1445         for( i=0; i<lgth2; i++ ) 
1446         {
1447                 ogcp2[i] = 0.5 * fpenalty;
1448                 fgcp2[i] = 0.5 * fpenalty;
1449         }
1450 #endif
1451
1452         gapinfo[0] = ogcp1;
1453         gapinfo[1] = fgcp1;
1454         gapinfo[2] = ogcp2;
1455         gapinfo[3] = fgcp2;
1456 #endif
1457
1458 #if 0
1459         fprintf( stdout, "in MSalign11.c\n" );
1460         for( i=0; i<icyc; i++ )
1461         {
1462                 fprintf( stdout, ">%d of GROUP1\n", i );
1463                 fprintf( stdout, "%s\n", seq1[i] );
1464         }
1465         for( i=0; i<jcyc; i++ )
1466         {
1467                 fprintf( stdout, ">%d of GROUP2\n", i );
1468                 fprintf( stdout, "%s\n", seq2[i] );
1469         }
1470         fflush( stdout );
1471 #endif
1472
1473         wm = MSalign11_rec( icyc, jcyc, eff1, eff2, seq1, seq2, cpmx1, cpmx2, 0, lgth1-1, 0, lgth2-1, alloclen, mseq1, mseq2, 0, gapinfo );
1474 #if DEBUG
1475                 fprintf( stderr, " seq1[0] = %s\n", seq1[0] );
1476                 fprintf( stderr, " seq2[0] = %s\n", seq2[0] );
1477                 fprintf( stderr, "mseq1[0] = %s\n", mseq1[0] );
1478                 fprintf( stderr, "mseq2[0] = %s\n", mseq2[0] );
1479 #endif
1480
1481 //      fprintf( stderr, "wm = %f\n", wm );
1482
1483
1484         for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
1485         for( i=0; i<jcyc; i++ ) strcpy( seq2[i], mseq2[i] );
1486
1487         if( seqlen( seq1[0] ) != nglen1 )
1488         {
1489                 fprintf( stderr, "bug! hairetsu ga kowareta! (nglen1) seqlen(seq1[0])=%d but nglen1=%d\n", seqlen( seq1[0] ), nglen1 );
1490                 fprintf( stderr, "seq1[0] = %s\n", seq1[0] );
1491                 exit( 1 );
1492         }
1493         if( seqlen( seq2[0] ) != nglen2 )
1494         {
1495                 fprintf( stderr, "bug! hairetsu ga kowareta! (nglen2) seqlen(seq2[0])=%d but nglen2=%d\n", seqlen( seq2[0] ), nglen2 );
1496                 exit( 1 );
1497         }
1498
1499         FreeFloatVec( ogcp1 );
1500         FreeFloatVec( ogcp2 );
1501         FreeFloatVec( fgcp1 );
1502         FreeFloatVec( fgcp2 );
1503         FreeFloatMtx( cpmx1 );
1504         FreeFloatMtx( cpmx2 );
1505         free( (void *)gapinfo );
1506
1507         FreeCharMtx( mseq1 );
1508         FreeCharMtx( mseq2 );
1509
1510         lgth1 = strlen( seq1[0] );
1511         lgth2 = strlen( seq2[0] );
1512         for( i=0; i<icyc; i++ ) 
1513         {
1514                 if( strlen( seq1[i] ) != lgth1 )
1515                 {
1516                         fprintf( stderr, "i = %d / %d\n", i, icyc );
1517                         fprintf( stderr, "hairetsu ga kowareta (end of MSalign11) !\n" );
1518                         exit( 1 );
1519                 }
1520         }
1521         for( j=0; j<jcyc; j++ )
1522         {
1523                 if( strlen( seq2[j] ) != lgth2 )
1524                 {
1525                         fprintf( stderr, "j = %d / %d\n", j, jcyc );
1526                         fprintf( stderr, "hairetsu ga kowareta (end of MSalign11) !\n" );
1527                         exit( 1 );
1528                 }
1529         }
1530
1531         return( wm );
1532 }