Next version of JABA
[jabaws.git] / binaries / src / mafft / core / MSalignmm.c
1 #include "mltaln.h"
2 #include "dp.h"
3
4 #define MEMSAVE 1
5
6 #define DEBUG 0
7 #define USE_PENALTY_EX  0
8 #define STOREWM 0
9
10 #define DPTANNI 100
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 *gaptable1, *gt1bk;
100         char *gaptable2, *gt2bk;
101         lgth1 = ien-ist+1;
102         lgth2 = jen-jst+1;
103
104         gt1bk = AllocateCharVec( lgth1+lgth2+1 );
105         gt2bk = AllocateCharVec( lgth1+lgth2+1 );
106
107 #if 0
108         for( i=0; i<lgth1; i++ ) 
109         {
110                 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
111         }
112 #endif
113
114
115 //      fprintf( stderr, "in Atracking, lgth1=%d, lgth2=%d\n", lgth1, lgth2 );
116  
117     for( i=0; i<lgth1+1; i++ ) 
118     {
119         ijp[i][0] = i + 1;
120     }
121     for( j=0; j<lgth2+1; j++ ) 
122     {
123         ijp[0][j] = -( j + 1 );
124     }
125
126
127         gaptable1 = gt1bk + lgth1+lgth2;
128         *gaptable1 = 0;
129         gaptable2 = gt2bk + lgth1+lgth2;
130         *gaptable2 = 0;
131
132 //      if( lgth2 == 1 ) fprintf( stderr, "in Atracking, mseq1 = %s, mseq2 = %s\n", mseq1[0], mseq2[0] );
133
134         iin = lgth1; jin = lgth2;
135         klim = lgth1+lgth2;
136         for( k=0; k<=klim; k++ ) 
137         {
138                 if( ijp[iin][jin] < 0 ) 
139                 {
140                         ifi = iin-1; jfi = jin+ijp[iin][jin];
141                 }
142                 else if( ijp[iin][jin] > 0 )
143                 {
144                         ifi = iin-ijp[iin][jin]; jfi = jin-1;
145                 }
146                 else
147                 {
148                         ifi = iin-1; jfi = jin-1;
149                 }
150                 l = iin - ifi;
151                 while( --l ) 
152                 {
153                         *--gaptable1 = 'o';
154                         *--gaptable2 = '-';
155                         k++;
156                 }
157                 l= jin - jfi;
158                 while( --l )
159                 {
160                         *--gaptable1 = '-';
161                         *--gaptable2 = 'o';
162                         k++;
163                 }
164                 if( iin <= 0 || jin <= 0 ) break;
165                 *--gaptable1 = 'o';
166                 *--gaptable2 = 'o';
167                 k++;
168                 iin = ifi; jin = jfi;
169
170         }
171
172         for( i=0; i<icyc; i++ ) gapireru( mseq1[i], seq1[i]+ist, gaptable1 );
173         for( j=0; j<jcyc; j++ ) gapireru( mseq2[j], seq2[j]+jst, gaptable2 );
174
175         free( gt1bk );
176         free( gt2bk );
177
178 //      fprintf( stderr, "in Atracking (owari), mseq1 = %s\n", mseq1[0] );
179 //      fprintf( stderr, "in Atracking (owari), mseq2 = %s\n", mseq2[0] );
180         return( 0.0 );
181 }
182
183 static float MSalignmm_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 )
184 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
185 {
186 //      int k;
187         register int i, j;
188         int ll1, ll2;
189         int lasti, lastj;
190         float wm = 0.0;   /* int ?????? */
191         float g;
192         float *currentw, *previousw;
193 #if USE_PENALTY_EX
194         float fpenalty_ex = (float)penalty_ex;
195 #endif
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         int lgth1, lgth2;
212         float *ogcp1;
213         float *fgcp1;
214         float *ogcp2;
215         float *fgcp2;
216 //      char **aseq1;
217 //      char **aseq2;
218 //      char **aseq1bk, **aseq2bk;
219
220         ogcp1 = gapinfo[0] + ist;
221         fgcp1 = gapinfo[1] + ist;
222         ogcp2 = gapinfo[2] + jst;
223         fgcp2 = gapinfo[3] + jst;
224
225 #if STOREWM
226         char ttt1[10000], ttt2[10000];
227 #endif
228
229
230         lgth1 = ien-ist+1;
231         lgth2 = jen-jst+1;
232
233 #if STOREWM
234         strncpy( ttt1, seq1[0]+ist, lgth1 ); ttt1[lgth1] = 0;
235         strncpy( ttt2, seq2[0]+jst, lgth2 ); ttt2[lgth2] = 0;
236
237         fprintf( stderr, "in _tanni ist,ien = %d,%d, lgth1=%d\n", ist, ien, lgth1 );
238         fprintf( stderr, "in _tanni jst,jen = %d,%d, lgth2=%d\n", jst, jen, lgth2 );
239         fprintf( stderr, "ttt1 = %s\n", ttt1 );
240         fprintf( stderr, "ttt2 = %s\n", ttt2 );
241 #endif
242
243
244         ll1 = ( (int)(lgth1) ) + 100;
245         ll2 = ( (int)(lgth2) ) + 100;
246
247 //      aseq1 = AllocateCharMtx( icyc, 0 );
248 //      aseq2 = AllocateCharMtx( jcyc, 0 );
249 //      aseq1bk = AllocateCharMtx( icyc, lgth1+lgth2+100 );
250 //      aseq2bk = AllocateCharMtx( jcyc, lgth1+lgth2+100 );
251 //      for( i=0; i<icyc; i++ ) aseq1[i] = aseq1bk[i];
252 //      for( i=0; i<jcyc; i++ ) aseq2[i] = aseq2bk[i];
253
254         w1 = AllocateFloatVec( ll2+2 );
255         w2 = AllocateFloatVec( ll2+2 );
256
257         initverticalw = AllocateFloatVec( ll1+2 );
258         lastverticalw = AllocateFloatVec( ll1+2 );
259
260         m = AllocateFloatVec( ll2+2 );
261         mp = AllocateIntVec( ll2+2 );
262
263         floatwork = AllocateFloatMtx( MAX( ll1, ll2 )+2, 27 ); 
264         intwork = AllocateIntMtx( MAX( ll1, ll2 )+2, 27 ); 
265
266
267         intmtx = AllocateIntMtx( ll1+1, ll2+1 );
268
269         ijp = intmtx;
270
271         currentw = w1;
272         previousw = w2;
273
274         match_calc( initverticalw, cpmx2+jst, cpmx1+ist, 0, lgth1, floatwork, intwork, 1 );
275
276         match_calc( currentw, cpmx1+ist, cpmx2+jst, 0, lgth2, floatwork, intwork, 1 );
277
278         for( i=1; i<lgth1+1; i++ )
279         {
280                 initverticalw[i] += ( ogcp1[0] + fgcp1[i-1] );
281         }
282         for( j=1; j<lgth2+1; j++ )
283         {
284                 currentw[j] += ( ogcp2[0] + fgcp2[j-1] );
285         }
286
287         for( j=1; j<lgth2+1; ++j ) 
288         {
289                 m[j] = currentw[j-1] + ogcp1[1]; mp[j] = 0;;
290         }
291
292         lastverticalw[0] = currentw[lgth2-1];
293
294
295
296         lasti = lgth1+1;
297         for( i=1; i<lasti; i++ )
298         {
299
300                 wtmp = previousw; 
301                 previousw = currentw;
302                 currentw = wtmp;
303
304                 previousw[0] = initverticalw[i-1];
305
306                 match_calc( currentw, cpmx1+ist, cpmx2+jst, i, lgth2, floatwork, intwork, 0 );
307                 currentw[0] = initverticalw[i];
308
309                 mi = previousw[0] + ogcp2[1]; 
310                 mpi = 0;
311
312                 ijppt = ijp[i] + 1;
313                 mjpt = m + 1;
314                 prept = previousw;
315                 curpt = currentw + 1;
316                 mpjpt = mp + 1;
317                 lastj = lgth2+1;
318                 for( j=1; j<lastj; j++ )
319                 {
320                         wm = *prept;
321                         *ijppt = 0;
322
323 #if 0
324                         fprintf( stderr, "%5.0f->", wm );
325 #endif
326                         g = mi + fgcp2[j-1];
327 #if 0
328                         fprintf( stderr, "%5.0f?", g );
329 #endif
330                         if( g > wm )
331                         {
332                                 wm = g;
333                                 *ijppt = -( j - mpi );
334                         }
335                         g = *prept + ogcp2[j];
336                         if( g >= mi )
337                         {
338                                 mi = g;
339                                 mpi = j-1;
340                         }
341 #if USE_PENALTY_EX
342                         mi += fpenalty_ex;
343 #endif
344
345                         g = *mjpt + fgcp1[i-1];
346 #if 0 
347                         fprintf( stderr, "%5.0f?", g );
348 #endif
349                         if( g > wm )
350                         {
351                                 wm = g;
352                                 *ijppt = +( i - *mpjpt );
353                         }
354                         g = *prept + ogcp1[i];
355                         if( g >= *mjpt )
356                         {
357                                 *mjpt = g;
358                                 *mpjpt = i-1;
359                         }
360 #if USE_PENALTY_EX
361                         m[j] += fpenalty_ex;
362 #endif
363
364 #if 0
365                         fprintf( stderr, "%5.0f ", wm );
366 #endif
367                         *curpt += wm;
368
369
370                         ijppt++;
371                         mjpt++;
372                         prept++;
373                         mpjpt++;
374                         curpt++;
375                 }
376                 lastverticalw[i] = currentw[lgth2-1];
377         }
378
379 //      fprintf( stderr, "wm = %f\n", wm );
380
381         Atracking( seq1, seq2, mseq1, mseq2, ijp, icyc, jcyc, ist, ien, jst, jen );
382
383 //      for( i=0; i<icyc; i++ ) strcpy( mseq1[i], aseq1[i] );
384 //      for( i=0; i<jcyc; i++ ) strcpy( mseq2[i], aseq2[i] );
385
386 //      fprintf( stderr, "in _tanni, aseq1 = %s\n", aseq1[0] );
387 //      fprintf( stderr, "in _tanni, mseq1 = %s\n", mseq1[0] );
388
389         FreeFloatVec( w1 );
390         FreeFloatVec( w2 );
391         FreeFloatVec( initverticalw );
392         FreeFloatVec( lastverticalw );
393
394         FreeFloatVec( m );
395         FreeIntVec( mp );
396
397
398         FreeFloatMtx( floatwork );
399         FreeIntMtx( intwork );
400
401         FreeIntMtx( intmtx );
402
403 //      FreeCharMtx( aseq1bk );
404 //      FreeCharMtx( aseq2bk );
405
406 //      free( aseq1 );
407 //      free( aseq2 );
408
409         return( wm );
410
411 }
412
413 static float MSalignmm_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 )
414 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
415 {
416 //      int k;
417         int alnlen;
418         float value = 0.0;
419         register int i, j;
420         char **aseq1, **aseq2;
421         int ll1, ll2, l, len;
422         int lasti, lastj, imid;
423         int jmid = 0; // by D.Mathog, a guess
424         float wm = 0.0;   /* int ?????? */
425         float g;
426         float *currentw, *previousw;
427 #if USE_PENALTY_EX
428         float fpenalty_ex = (float)penalty_ex;
429 #endif
430         float *wtmp;
431 //      short *ijppt;
432         int *mpjpt;
433 //      short **ijp;
434         int *mp;
435         int mpi;
436         float *mjpt, *prept, *curpt;
437         float mi;
438         float *m;
439         float *w1, *w2;
440 //      float *match;
441         float *initverticalw;    /* kufuu sureba iranai */
442         float *lastverticalw;    /* kufuu sureba iranai */
443         int **intwork;
444         float **floatwork;
445 //      short **shortmtx;
446 #if STOREWM
447         float **WMMTX;
448         float **WMMTX2;
449 #endif
450         float *midw;
451         float *midm;
452         float *midn;
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 = NULL; // by D.Mathog, a guess
461         int jumpi, jumpj = 0; // by D.Mathog, a guess
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 = MSalignmm_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 0
779     for( i=0; i<lgth1; i++ )
780     {
781         for( j=0; j<lgth2; j++ )
782         {
783             fprintf( stderr, "% 10.2f ", WMMTX[i][j] );
784         }
785         fprintf( stderr, "\n" );
786     }
787         fprintf( stderr, "\n" );
788         fprintf( stderr, "WMMTX2 = \n" );
789     for( i=0; i<lgth1; i++ )
790     {
791         for( j=0; j<lgth2; j++ )
792         {
793             fprintf( stderr, "% 10.2f ", WMMTX2[i][j] );
794         }
795         fprintf( stderr, "\n" );
796     }
797         fprintf( stderr, "\n" );
798 #endif
799
800 // gyakudp
801
802         match_calc( initverticalw, cpmx2+jst, cpmx1+ist, lgth2-1, lgth1, floatwork, intwork, 1 );
803         match_calc( currentw, cpmx1+ist, cpmx2+jst, lgth1-1, lgth2, floatwork, intwork, 1 );
804
805         for( i=0; i<lgth1-1; i++ )
806         {
807                 initverticalw[i] += ( fgcp1[lgth1-1] + ogcp1[i+1] );
808 //              initverticalw[i] += fpenalty;
809         }
810         for( j=0; j<lgth2-1; j++ )
811         {
812                 currentw[j] += ( fgcp2[lgth2-1] + ogcp2[j+1] );
813 //              currentw[j] += fpenalty;
814         }
815
816 #if STOREWM
817         for( i=0; i<lgth1-1; i++ )
818         {
819                 WMMTX[i][lgth2-1] += ( fgcp1[lgth1-1] + ogcp1[i+1] );
820                 fprintf( stderr, "fgcp1[lgth1-1] + ogcp1[i+1] = %f\n", fgcp1[lgth1-1] + ogcp1[i+1] );
821         }
822         for( j=0; j<lgth2-1; j++ )
823         {
824                 WMMTX[lgth1-1][j] += ( fgcp2[lgth2-1] + ogcp2[j+1] );
825                 fprintf( stderr, "fgcp2[lgth2-1] + ogcp2[j+1] = %f\n", fgcp2[lgth2-1] + ogcp2[j+1] );
826         }
827 #endif
828
829
830
831
832
833
834         for( j=lgth2-1; j>0; --j )
835         {
836                 m[j-1] = currentw[j] + fgcp2[lgth2-2];
837 //              m[j-1] = currentw[j];
838                 mp[j] = lgth1-1;
839         }
840
841 //      for( j=0; j<lgth2; j++ ) m[j] = 0.0;
842         // m[lgth2-1] ha irunoka?
843
844
845 //      for( i=lgth1-2; i>=imid; i-- )
846         firstm = -9999999.9;
847         firstmp = lgth1-1;
848         for( i=lgth1-2; i>-1; i-- )
849         {
850                 wtmp = previousw;
851                 previousw = currentw;
852                 currentw = wtmp;
853                 previousw[lgth2-1] = initverticalw[i+1];
854 //              match_calc( currentw, seq1, seq2, i, lgth2 );
855                 match_calc( currentw, cpmx1+ist, cpmx2+jst, i, lgth2, floatwork, intwork, 0 );
856
857                 currentw[lgth2-1] = initverticalw[i];
858
859 //              m[lgth2] = fgcp1[i];
860 //              WMMTX2[i][lgth2] += m[lgth2];
861 //              fprintf( stderr, "m[] = %f\n", m[lgth2] );
862
863                 mi = previousw[lgth2-1] + fgcp2[lgth2-2];
864 //              mi = previousw[lgth2-1];
865                 mpi = lgth2 - 1;
866
867                 mjpt = m + lgth2 - 2;
868                 prept = previousw + lgth2 - 1;
869                 curpt = currentw + lgth2 - 2;
870                 mpjpt = mp + lgth2 - 2;
871
872
873                 for( j=lgth2-2; j>-1; j-- )
874                 {
875                         wm = *prept;
876                         ijpi = i+1;
877                         ijpj = j+1;
878
879                         g = mi + ogcp2[j+1];
880 //                      g = mi + fpenalty;
881                         if( g > wm )
882                         {
883                                 wm = g;
884                                 ijpj = mpi;
885                                 ijpi = i+1;
886                         }
887
888                         g = *prept + fgcp2[j];
889 //                      g = *prept;
890                         if( g >= mi )
891                         {
892 //                              fprintf( stderr, "i,j=%d,%d - renewed! mpi = %d\n", i, j, j+1 );
893                                 mi = g;
894                                 mpi = j + 1;
895                         }
896
897 #if USE_PENALTY_EX
898                         mi += fpenalty_ex;
899 #endif
900
901 //                      fprintf( stderr, "i,j=%d,%d *mpjpt = %d\n", i, j, *mpjpt );
902                         g = *mjpt + ogcp1[i+1];
903 //                      g = *mjpt + fpenalty;
904                         if( g > wm )
905                         {
906                                 wm = g;
907                                 ijpi = *mpjpt;
908                                 ijpj = j+1;
909                         }
910
911 //                      if( i == imid )fprintf( stderr, "i,j=%d,%d \n", i, j );
912                         g = *prept + fgcp1[i];
913 //                      g = *prept;
914                         if( g >= *mjpt )
915                         {
916                                 *mjpt = g;
917                                 *mpjpt = i + 1;
918                         }
919
920 #if USE_PENALTY_EX
921                         m[j] += fpenalty_ex;
922 #endif
923
924                         if( i == jumpi || i == imid - 1 )
925                         {
926                                 jumpforwi[j] = ijpi; //muda
927                                 jumpforwj[j] = ijpj; //muda
928 //                              fprintf( stderr, "jumpfori[%d] = %d\n", j, ijpi );
929 //                              fprintf( stderr, "jumpforj[%d] = %d\n", j, ijpj );
930                         }
931                         if( i == imid ) // muda
932                         {
933                                 midw[j] += wm;
934 //                              midm[j+1] += *mjpt + fpenalty; //??????
935                                 midm[j+1] += *mjpt; //??????
936                         }
937                         if( i == imid - 1 )
938                         {
939 //                              midn[j] += mi + fpenalty;  //????
940                                 midn[j] += mi;  //????
941                         }
942 #if STOREWM
943                         WMMTX[i][j] += wm;
944 //                      WMMTX2[i][j+1] += *mjpt + fpenalty;
945                         WMMTX2[i][j+1] += *mjpt;
946 #endif
947                         *curpt += wm;
948
949                         mjpt--;
950                         prept--;
951                         mpjpt--;
952                         curpt--;
953                 }
954 //              fprintf( stderr, "adding *mjpt (=%f) to WMMTX2[%d][%d]\n", *mjpt, i, j+1 );
955                 g = *prept + fgcp1[i];
956                 if( firstm < g ) 
957                 {
958                         firstm = g;
959                         firstmp = i + 1;
960                 }
961 #if STOREWM
962                 WMMTX2[i][j+1] += firstm;
963 #endif
964                 if( i == imid ) midm[j+1] += firstm;
965
966                 if( i == imid - 1 )     
967                 {
968                         maxwm = midw[1];
969                         jmid = 0;
970 //                      if( depth == 1 ) fprintf( stderr, "maxwm!! = %f\n", maxwm );
971                         for( j=2; j<lgth2-1; j++ )
972                         {
973                                 wm = midw[j];
974                                 if( wm > maxwm )
975                                 {
976                                         jmid = j;
977                                         maxwm = wm;
978                                 }
979 //                              if( depth == 1 ) fprintf( stderr, "maxwm!! = %f\n", maxwm );
980                         }
981                         for( j=0; j<lgth2+1; j++ )
982                         {
983                                 wm = midm[j];
984                                 if( wm > maxwm )
985                                 {
986                                         jmid = j;
987                                         maxwm = wm;
988                                 }
989 //                              if( depth == 1 ) fprintf( stderr, "maxwm!! = %f\n", maxwm );
990                         }
991
992 //                      if( depth == 1 ) fprintf( stderr, "maxwm!! = %f\n", maxwm );
993
994
995 //                      fprintf( stderr, "### imid=%d, jmid=%d\n", imid, jmid );
996                         wm = midw[jmid];
997                         jumpi = imid-1;
998                         jumpj = jmid-1;
999                         if( jmid > 0 && midn[jmid-1] > wm ) //060413
1000                         {
1001                                 jumpi = imid-1;
1002                                 jumpj = jumpbacki[jmid];
1003                                 wm = midn[jmid-1];
1004 //                              fprintf( stderr, "rejump (n)\n" );
1005                         }
1006                         if( midm[jmid] > wm )
1007                         {
1008                                 jumpi = jumpbackj[jmid];
1009                                 jumpj = jmid-1;
1010                                 wm = midm[jmid];
1011 //                              fprintf( stderr, "rejump (m) jumpi=%d\n", jumpi );
1012                         }
1013
1014
1015 //                      fprintf( stderr, "--> imid=%d, jmid=%d\n", imid, jmid );
1016 //                      fprintf( stderr, "--> jumpi=%d, jumpj=%d\n", jumpi, jumpj );
1017 #if STOREWM
1018                         fprintf( stderr, "imid = %d\n", imid );
1019                         fprintf( stderr, "midn = \n" );
1020                         for( j=0; j<lgth2; j++ )
1021                         {
1022                                 fprintf( stderr, "% 7.1f ", midn[j] );
1023                         }
1024                         fprintf( stderr, "\n" );
1025                         fprintf( stderr, "midw = \n" );
1026                         for( j=0; j<lgth2; j++ )
1027                         {
1028                                 fprintf( stderr, "% 7.1f ", midw[j] );
1029                         }
1030                         fprintf( stderr, "\n" );
1031                         fprintf( stderr, "midm = \n" );
1032                         for( j=0; j<lgth2; j++ )
1033                         {
1034                                 fprintf( stderr, "% 7.1f ", midm[j] );
1035                         }
1036                         fprintf( stderr, "\n" );
1037 #endif
1038 //                      fprintf( stderr, "maxwm = %f\n", maxwm );
1039                 }
1040                 if( i == jumpi ) //saki?
1041                 {
1042 //                      fprintf( stderr, "imid, jumpi = %d,%d\n", imid, jumpi );
1043 //                      fprintf( stderr, "jmid, jumpj = %d,%d\n", jmid, jumpj );
1044                         if( jmid == 0 )
1045                         {
1046 //                              fprintf( stderr, "CHUI2!\n" );
1047                                 jumpj = 0; jmid = 1;
1048                                 jumpi = firstmp - 1;
1049                                 imid = firstmp;
1050                         }
1051
1052 #if 0
1053                         else if( jmid == lgth2 ) 
1054                         {
1055                                 fprintf( stderr, "CHUI1!\n" );
1056                                 jumpi=0; jumpj=0;
1057                                 imid=jumpforwi[0]; jmid=lgth2-1;
1058                         }
1059 #else // 060414
1060                         else if( jmid >= lgth2 ) 
1061                         {
1062 //                              fprintf( stderr, "CHUI1!\n" );
1063                                 jumpi=imid-1; jmid=lgth2;
1064                                 jumpj = lgth2-1;
1065                         }
1066 #endif
1067                         else
1068                         {
1069                                 imid = jumpforwi[jumpj];
1070                                 jmid = jumpforwj[jumpj];
1071                         }
1072 #if 0
1073                         fprintf( stderr, "jumpi -> %d\n", jumpi );
1074                         fprintf( stderr, "jumpj -> %d\n", jumpj );
1075                         fprintf( stderr, "imid -> %d\n", imid );
1076                         fprintf( stderr, "jmid -> %d\n", jmid );
1077 #endif
1078
1079 #if STOREWM
1080                         break;
1081 #else
1082                         break;
1083 #endif
1084                 }
1085         }
1086 #if 0
1087                 jumpi=0; jumpj=0;
1088                 imid=lgth1-1; jmid=lgth2-1;
1089         }
1090 #endif
1091
1092 //      fprintf( stderr, "imid = %d, but jumpi = %d\n", imid, jumpi );
1093 //      fprintf( stderr, "jmid = %d, but jumpj = %d\n", jmid, jumpj );
1094
1095 //      for( j=0; j<lgth2; j++ ) midw[j] += currentw[j];
1096 //      for( j=0; j<lgth2; j++ ) midm[j] += m[j+1];
1097 //      for( j=0; j<lgth2; j++ ) midw[j] += WMMTX[imid][j];
1098 //      for( j=0; j<lgth2; j++ ) midw[j] += WMMTX[imid][j];
1099
1100
1101 #if STOREWM
1102         fprintf( stderr, "WMMTX = \n" );
1103     for( i=0; i<lgth1; i++ )
1104     {
1105         fprintf( stderr, "%d ", i );
1106         for( j=0; j<lgth2; j++ )
1107         {
1108             fprintf( stderr, "% 7.2f ", WMMTX[i][j] );
1109         }
1110         fprintf( stderr, "\n" );
1111     }
1112 //      fprintf( stderr, "WMMTX2 = (p = %f)\n", fpenalty );
1113     for( i=0; i<lgth1; i++ )
1114     {
1115         fprintf( stderr, "%d ", i );
1116         for( j=0; j<lgth2+1; j++ )
1117         {
1118             fprintf( stderr, "% 7.2f ", WMMTX2[i][j] );
1119         }
1120         fprintf( stderr, "\n" );
1121     }
1122
1123         fprintf( stderr, "jumpbacki = \n" );
1124         for( j=0; j<lgth2; j++ )
1125         {
1126                 fprintf( stderr, "% 7d ", jumpbacki[j] );
1127         }
1128         fprintf( stderr, "\n" );
1129         fprintf( stderr, "jumpbackj = \n" );
1130         for( j=0; j<lgth2; j++ )
1131         {
1132                 fprintf( stderr, "% 7d ", jumpbackj[j] );
1133         }
1134         fprintf( stderr, "\n" );
1135         fprintf( stderr, "jumpforwi = \n" );
1136         for( j=0; j<lgth2; j++ )
1137         {
1138                 fprintf( stderr, "% 7d ", jumpforwi[j] );
1139         }
1140         fprintf( stderr, "\n" );
1141         fprintf( stderr, "jumpforwj = \n" );
1142         for( j=0; j<lgth2; j++ )
1143         {
1144                 fprintf( stderr, "% 7d ", jumpforwj[j] );
1145         }
1146         fprintf( stderr, "\n" );
1147
1148
1149 #endif
1150
1151
1152 //      Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
1153
1154 #if 0 // irukamo
1155         resultlen = strlen( mseq1[0] );
1156         if( alloclen < resultlen || resultlen > N )
1157         {
1158                 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
1159                 ErrorExit( "LENGTH OVER!\n" );
1160         }
1161 #endif
1162
1163
1164
1165 #if 0
1166         fprintf( stderr, "jumpi = %d, imid = %d\n", jumpi, imid );
1167         fprintf( stderr, "jumpj = %d, jmid = %d\n", jumpj, jmid );
1168
1169         fprintf( stderr, "imid = %d\n", imid );
1170         fprintf( stderr, "jmid = %d\n", jmid );
1171 #endif
1172
1173
1174         FreeFloatVec( w1 );
1175         FreeFloatVec( w2 );
1176         FreeFloatVec( initverticalw );
1177         FreeFloatVec( lastverticalw );
1178         FreeFloatVec( midw );
1179         FreeFloatVec( midm );
1180         FreeFloatVec( midn );
1181
1182         FreeIntVec( jumpbacki );
1183         FreeIntVec( jumpbackj );
1184         FreeIntVec( jumpforwi );
1185         FreeIntVec( jumpforwj );
1186         FreeIntVec( jumpdummi );
1187         FreeIntVec( jumpdummj );
1188
1189         FreeFloatVec( m );
1190         FreeIntVec( mp );
1191
1192         FreeFloatMtx( floatwork );
1193         FreeIntMtx( intwork );
1194
1195 #if STOREWM
1196         FreeFloatMtx( WMMTX );
1197         FreeFloatMtx( WMMTX2 );
1198 #endif
1199
1200 //      fprintf( stderr, "==== calling myself (first)\n" );
1201
1202 #if 0
1203                 fprintf( stderr, "seq1[0] = %s\n", seq1[0] );
1204                 fprintf( stderr, "seq2[0] = %s\n", seq2[0] );
1205 #endif
1206         value = MSalignmm_rec( icyc, jcyc, eff1, eff2, seq1, seq2, cpmx1, cpmx2, ist, ist+jumpi, jst, jst+jumpj, alloclen, aseq1, aseq2, depth, gapinfo );      
1207 #if 0
1208                 fprintf( stderr, "aseq1[0] = %s\n", aseq1[0] );
1209                 fprintf( stderr, "aseq2[0] = %s\n", aseq2[0] );
1210 #endif
1211 #if MEMSAVE
1212 #else
1213         for( i=0; i<icyc; i++ ) strcpy( mseq1[i], aseq1[i] );
1214         for( i=0; i<jcyc; i++ ) strcpy( mseq2[i], aseq2[i] );
1215 #endif
1216
1217 //      fprintf( stderr, "====(f) aseq1[0] (%d) = %s (%d-%d)\n", depth, aseq1[0], ist, ien );
1218 //      fprintf( stderr, "====(f) aseq2[0] (%d) = %s (%d-%d)\n", depth, aseq2[0], jst, jen );
1219
1220         len = strlen( mseq1[0] );
1221 //      fprintf( stderr, "len = %d\n", len );
1222         l = jmid - jumpj - 1;
1223 //      fprintf( stderr, "l=%d\n", l );
1224         if( l > 0 )
1225         {
1226                 for( i=0; i<l; i++ ) gaps[i] = '-'; gaps[i] = 0;
1227                 for( i=0; i<icyc; i++ ) 
1228                 {
1229                         strcat( mseq1[i], gaps );
1230                         mseq1[i][len+l] = 0;
1231                 }
1232                 for( j=0; j<jcyc; j++ )
1233                 {
1234                         strncat( mseq2[j], seq2[j]+jst+jumpj+1, l );
1235                         mseq2[j][len+l] = 0;
1236                 }
1237 //              fprintf( stderr, "penalizing (2) .. %f(%d), %f(%d)\n", ogcp2[jumpj+1], jumpj+1, fgcp2[jmid-1], jmid-1 );
1238                 value +=  ( ogcp2[jumpj+1] + fgcp2[jmid-1] );
1239 //              value += fpenalty;
1240         }
1241         len = strlen( mseq1[0] );
1242         l = imid - jumpi - 1;
1243 //      fprintf( stderr, "l=%d\n", l );
1244         if( l > 0 )
1245         {
1246                 for( i=0; i<l; i++ ) gaps[i] = '-'; gaps[i] = 0;
1247                 for( i=0; i<icyc; i++ )
1248                 {
1249                         strncat( mseq1[i], seq1[i]+ist+jumpi+1, l );
1250                         mseq1[i][len+l] = 0;
1251                 }
1252                 for( j=0; j<jcyc; j++ ) 
1253                 {
1254                         strcat( mseq2[j], gaps );
1255                         mseq2[j][len+l] = 0;
1256                 }
1257
1258 //              for( i=0; i<lgth1; i++ ) fprintf( stderr, "ogcp1[%d] = %f\n", i, ogcp1[i] );
1259 //              for( i=0; i<lgth1; i++ ) fprintf( stderr, "fgcp1[%d] = %f\n", i, fgcp1[i] );
1260
1261
1262 //              fprintf( stderr, "penalizing (1) .. ogcp1[%d] = %f, fgcp1[%d] = %f\n", jumpi+1, ogcp1[jumpi+1], imid-1, fgcp1[imid-1] );
1263                 value += ( ogcp1[jumpi+1] + fgcp1[imid-1] );
1264 //              value += fpenalty;
1265         }
1266 #if 0
1267         for( i=0; i<icyc; i++ ) fprintf( stderr, "after gapfill mseq1[%d]=%s\n", i, mseq1[i] );
1268         for( i=0; i<jcyc; i++ ) fprintf( stderr, "after gapfill mseq2[%d]=%s\n", i, mseq2[i] );
1269 #endif
1270
1271 //      fprintf( stderr, "==== calling myself (second)\n" );
1272
1273 #if MEMSAVE
1274         alnlen = strlen( aseq1[0] );
1275         for( i=0; i<icyc; i++ ) aseq1[i] += alnlen;
1276         for( i=0; i<jcyc; i++ ) aseq2[i] += alnlen;
1277 #endif
1278
1279 #if 0
1280                 fprintf( stderr, "seq1[0] = %s\n", seq1[0] );
1281                 fprintf( stderr, "seq2[0] = %s\n", seq2[0] );
1282 #endif
1283         value += MSalignmm_rec( icyc, jcyc, eff1, eff2, seq1, seq2, cpmx1, cpmx2, ist+imid, ien, jst+jmid, jen, alloclen, aseq1, aseq2, depth, gapinfo );       
1284 #if 0
1285                 fprintf( stderr, "aseq1[0] = %s\n", aseq1[0] );
1286                 fprintf( stderr, "aseq2[0] = %s\n", aseq2[0] );
1287 #endif
1288
1289
1290
1291 #if DEBUG
1292         if( value - maxwm > 1 || maxwm - value > 1 )
1293         {
1294                 fprintf( stderr, "WARNING value  = %f, but maxwm = %f\n", value, maxwm );
1295                 for( i=0; i<icyc; i++ )
1296                 {
1297                         fprintf( stderr, ">1-%d\n%s\n", i, mseq1[i] );
1298                         fprintf( stderr, "%s\n", aseq1[i] );
1299                 }
1300                 for( i=0; i<jcyc; i++ )
1301                 {
1302                         fprintf( stderr, ">2-%d\n%s\n", i, mseq2[i] );
1303                         fprintf( stderr, "%s\n", aseq2[i] );
1304                 }
1305
1306 //              exit( 1 );
1307         }
1308         else
1309         {
1310                 fprintf( stderr, "value = %.0f, maxwm = %.0f -> ok\n", value, maxwm );
1311         }
1312 #endif
1313
1314 #if MEMSAVE
1315 #else
1316         for( i=0; i<icyc; i++ ) strcat( mseq1[i], aseq1[i] );
1317         for( i=0; i<jcyc; i++ ) strcat( mseq2[i], aseq2[i] );
1318 #endif
1319
1320 //      fprintf( stderr, "====(s) aseq1[0] (%d) = %s (%d-%d)\n", depth, aseq1[0], ist, ien );
1321 //      fprintf( stderr, "====(s) aseq2[0] (%d) = %s (%d-%d)\n", depth, aseq2[0], jst, jen );
1322
1323         free( gaps );
1324 #if MEMSAVE
1325         free( aseq1 );
1326         free( aseq2 );
1327 #else
1328         FreeCharMtx( aseq1 );
1329         FreeCharMtx( aseq2 );
1330 #endif
1331         
1332         return( value );
1333 }
1334
1335
1336
1337 float MSalignmm( char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen, char *sgap1, char *sgap2, char *egap1, char *egap2 )
1338 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
1339 {
1340 //      int k;
1341         int i, j;
1342         int ll1, ll2;
1343         int lgth1, lgth2;
1344         float wm = 0.0;   /* int ?????? */
1345         char **mseq1;
1346         char **mseq2;
1347         float *ogcp1;
1348         float *ogcp2;
1349         float *fgcp1;
1350         float *fgcp2;
1351         float **cpmx1;
1352         float **cpmx2;
1353         float **gapinfo;
1354         float fpenalty = (float)penalty;
1355         int nglen1, nglen2;
1356
1357 #if 0
1358         fprintf( stderr, "eff in SA+++align\n" );
1359         for( i=0; i<icyc; i++ ) fprintf( stderr, "eff1[%d] = %f\n", i, eff1[i] );
1360 #endif
1361
1362         nglen1 = seqlen( seq1[0] );
1363         nglen2 = seqlen( seq2[0] );
1364
1365         lgth1 = strlen( seq1[0] );
1366         lgth2 = strlen( seq2[0] );
1367
1368         ll1 = ( (int)(lgth1) ) + 100;
1369         ll2 = ( (int)(lgth2) ) + 100;
1370
1371         mseq1 = AllocateCharMtx( icyc, ll1+ll2 );
1372         mseq2 = AllocateCharMtx( jcyc, ll1+ll2 );
1373
1374         gapinfo = AllocateFloatMtx( 4, 0 );
1375         ogcp1 = AllocateFloatVec( ll1+2 );
1376         ogcp2 = AllocateFloatVec( ll2+2 );
1377         fgcp1 = AllocateFloatVec( ll1+2 );
1378         fgcp2 = AllocateFloatVec( ll2+2 );
1379
1380
1381         cpmx1 = AllocateFloatMtx( ll1+2, 27 );
1382         cpmx2 = AllocateFloatMtx( ll2+2, 27 );
1383
1384         for( i=0; i<icyc; i++ ) 
1385         {
1386                 if( strlen( seq1[i] ) != lgth1 )
1387                 {
1388                         fprintf( stderr, "i = %d / %d\n", i, icyc );
1389                         fprintf( stderr, "bug! hairetsu ga kowareta!\n" );
1390                         exit( 1 );
1391                 }
1392         }
1393         for( j=0; j<jcyc; j++ )
1394         {
1395                 if( strlen( seq2[j] ) != lgth2 )
1396                 {
1397                         fprintf( stderr, "j = %d / %d\n", j, jcyc );
1398                         fprintf( stderr, "bug! hairetsu ga kowareta!\n" );
1399                         exit( 1 );
1400                 }
1401         }
1402
1403         MScpmx_calc_new( seq1, cpmx1, eff1, lgth1, icyc );
1404         MScpmx_calc_new( seq2, cpmx2, eff2, lgth2, jcyc );
1405
1406
1407 #if 1
1408
1409         if( sgap1 )
1410         {
1411                 new_OpeningGapCount( ogcp1, icyc, seq1, eff1, lgth1, sgap1 );
1412                 new_OpeningGapCount( ogcp2, jcyc, seq2, eff2, lgth2, sgap2 );
1413                 new_FinalGapCount( fgcp1, icyc, seq1, eff1, lgth1, egap2 );
1414                 new_FinalGapCount( fgcp2, jcyc, seq2, eff2, lgth2, egap2 );
1415         }
1416         else
1417         {
1418                 st_OpeningGapCount( ogcp1, icyc, seq1, eff1, lgth1 );
1419                 st_OpeningGapCount( ogcp2, jcyc, seq2, eff2, lgth2 );
1420                 st_FinalGapCount( fgcp1, icyc, seq1, eff1, lgth1 );
1421                 st_FinalGapCount( fgcp2, jcyc, seq2, eff2, lgth2 );
1422         }
1423
1424 #if 1
1425         for( i=0; i<lgth1; i++ ) 
1426         {
1427                 ogcp1[i] = 0.5 * ( 1.0 - ogcp1[i] ) * fpenalty;
1428                 fgcp1[i] = 0.5 * ( 1.0 - fgcp1[i] ) * fpenalty;
1429 //              fprintf( stderr, "fgcp1[%d] = %f\n", i, fgcp1[i] );
1430         }
1431         for( i=0; i<lgth2; i++ ) 
1432         {
1433                 ogcp2[i] = 0.5 * ( 1.0 - ogcp2[i] ) * fpenalty;
1434                 fgcp2[i] = 0.5 * ( 1.0 - fgcp2[i] ) * fpenalty;
1435 //              fprintf( stderr, "fgcp2[%d] = %f\n", i, fgcp2[i] );
1436         }
1437 #else
1438         for( i=0; i<lgth1; i++ ) 
1439         {
1440                 ogcp1[i] = 0.5 * fpenalty;
1441                 fgcp1[i] = 0.5 * fpenalty;
1442         }
1443         for( i=0; i<lgth2; i++ ) 
1444         {
1445                 ogcp2[i] = 0.5 * fpenalty;
1446                 fgcp2[i] = 0.5 * fpenalty;
1447         }
1448 #endif
1449
1450         gapinfo[0] = ogcp1;
1451         gapinfo[1] = fgcp1;
1452         gapinfo[2] = ogcp2;
1453         gapinfo[3] = fgcp2;
1454 #endif
1455
1456 #if 0
1457         fprintf( stdout, "in MSalignmm.c\n" );
1458         for( i=0; i<icyc; i++ )
1459         {
1460                 fprintf( stdout, ">%d of GROUP1\n", i );
1461                 fprintf( stdout, "%s\n", seq1[i] );
1462         }
1463         for( i=0; i<jcyc; i++ )
1464         {
1465                 fprintf( stdout, ">%d of GROUP2\n", i );
1466                 fprintf( stdout, "%s\n", seq2[i] );
1467         }
1468         fflush( stdout );
1469 #endif
1470
1471         wm = MSalignmm_rec( icyc, jcyc, eff1, eff2, seq1, seq2, cpmx1, cpmx2, 0, lgth1-1, 0, lgth2-1, alloclen, mseq1, mseq2, 0, gapinfo );
1472 #if DEBUG
1473                 fprintf( stderr, " seq1[0] = %s\n", seq1[0] );
1474                 fprintf( stderr, " seq2[0] = %s\n", seq2[0] );
1475                 fprintf( stderr, "mseq1[0] = %s\n", mseq1[0] );
1476                 fprintf( stderr, "mseq2[0] = %s\n", mseq2[0] );
1477 #endif
1478
1479 //      fprintf( stderr, "wm = %f\n", wm );
1480
1481
1482         for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
1483         for( i=0; i<jcyc; i++ ) strcpy( seq2[i], mseq2[i] );
1484
1485         if( seqlen( seq1[0] ) != nglen1 )
1486         {
1487                 fprintf( stderr, "bug! hairetsu ga kowareta! (nglen1) seqlen(seq1[0])=%d but nglen1=%d\n", seqlen( seq1[0] ), nglen1 );
1488                 fprintf( stderr, "seq1[0] = %s\n", seq1[0] );
1489                 exit( 1 );
1490         }
1491         if( seqlen( seq2[0] ) != nglen2 )
1492         {
1493                 fprintf( stderr, "bug! hairetsu ga kowareta! (nglen2) seqlen(seq2[0])=%d but nglen2=%d\n", seqlen( seq2[0] ), nglen2 );
1494                 exit( 1 );
1495         }
1496
1497         FreeFloatVec( ogcp1 );
1498         FreeFloatVec( ogcp2 );
1499         FreeFloatVec( fgcp1 );
1500         FreeFloatVec( fgcp2 );
1501         FreeFloatMtx( cpmx1 );
1502         FreeFloatMtx( cpmx2 );
1503         free( (void *)gapinfo );
1504
1505         FreeCharMtx( mseq1 );
1506         FreeCharMtx( mseq2 );
1507
1508         lgth1 = strlen( seq1[0] );
1509         lgth2 = strlen( seq2[0] );
1510         for( i=0; i<icyc; i++ ) 
1511         {
1512                 if( strlen( seq1[i] ) != lgth1 )
1513                 {
1514                         fprintf( stderr, "i = %d / %d\n", i, icyc );
1515                         fprintf( stderr, "hairetsu ga kowareta (end of MSalignmm) !\n" );
1516                         exit( 1 );
1517                 }
1518         }
1519         for( j=0; j<jcyc; j++ )
1520         {
1521                 if( strlen( seq2[j] ) != lgth2 )
1522                 {
1523                         fprintf( stderr, "j = %d / %d\n", j, jcyc );
1524                         fprintf( stderr, "hairetsu ga kowareta (end of MSalignmm) !\n" );
1525                         exit( 1 );
1526                 }
1527         }
1528
1529         return( wm );
1530 }