--- /dev/null
+#include "mltaln.h"
+#include "dp.h"
+
+#define DEBUG 0
+#define XXXXXXX 0
+#define USE_PENALTY_EX 0
+#define STOREWM 0
+
+#define DPTANNI 50
+
+
+static reccycle = 0;
+
+static void OpeningGapCount( float *ogcp, int clus, char **seq, double *eff )
+{
+ int i, j, gc, gb;
+ int len = strlen( seq[0] );
+ float totaleff = 0.0;
+
+ for( i=0; i<len; i++ ) ogcp[i] = 0.0;
+ for( j=0; j<clus; j++ )
+ {
+ gc = 0;
+ for( i=0; i<len; i++ )
+ {
+ gb = gc;
+ gc = ( seq[j][i] == '-' );
+ {
+ if( !gb * gc ) ogcp[i] += eff[j];
+ }
+ }
+ totaleff+= eff[j];
+ }
+ for( i=0; i<len; i++ )
+ ogcp[i] /= totaleff;
+}
+
+static void FinalGapCount( float *fgcp, int clus, char **seq, double *eff )
+{
+ int i, j, gc, gb;
+ int len = strlen( seq[0] );
+ float totaleff = 0.0;
+
+ for( i=0; i<len; i++ ) fgcp[i] = 0.0;
+ for( j=0; j<clus; j++ )
+ {
+ gc = ( seq[j][0] == '-' );
+ for( i=1; i<len+1; i++ )
+ {
+ gb = gc;
+ gc = ( seq[j][i] == '-' );
+ {
+ if( gb * !gc ) fgcp[i-1] += eff[j];
+ }
+ }
+ totaleff += eff[j];
+ }
+ for( i=0; i<len; i++ )
+ fgcp[i] /= totaleff;
+}
+
+static void match_calc( float *match, float **cpmx1, float **cpmx2, int i1, int lgth2, float **floatwork, int **intwork, int initialize )
+{
+ int j, k, l;
+ float scarr[26];
+ float **cpmxpd = floatwork;
+ int **cpmxpdn = intwork;
+ int count = 0;
+
+ if( initialize )
+ {
+ for( j=0; j<lgth2; j++ )
+ {
+ count = 0;
+ for( l=0; l<26; l++ )
+ {
+ if( cpmx2[j][l] )
+ {
+ cpmxpd[j][count] = cpmx2[j][l];
+ cpmxpdn[j][count] = l;
+ count++;
+ }
+ }
+ cpmxpdn[j][count] = -1;
+ }
+ }
+
+ for( l=0; l<26; l++ )
+ {
+ scarr[l] = 0.0;
+ for( k=0; k<26; k++ )
+ {
+ scarr[l] += n_dis[k][l] * cpmx1[i1][k];
+ }
+ }
+#if 0 /* ¤³¤ì¤ò»È¤¦¤È¤\ad¤Ïfloatwork¤Î¥¢¥í¥±¡¼¥È¤òµÕ¤Ë¤¹¤ë */
+ {
+ float *fpt, **fptpt, *fpt2;
+ int *ipt, **iptpt;
+ fpt2 = match;
+ iptpt = cpmxpdn;
+ fptpt = cpmxpd;
+ while( lgth2-- )
+ {
+ *fpt2 = 0.0;
+ ipt=*iptpt,fpt=*fptpt;
+ while( *ipt > -1 )
+ *fpt2 += scarr[*ipt++] * *fpt++;
+ fpt2++,iptpt++,fptpt++;
+ }
+ }
+#else
+ for( j=0; j<lgth2; j++ )
+ {
+ match[j] = 0.0;
+ for( k=0; cpmxpdn[j][k]>-1; k++ )
+ match[j] += scarr[cpmxpdn[j][k]] * cpmxpd[j][k];
+ }
+#endif
+}
+
+static float Atracking( float *lasthorizontalw, float *lastverticalw,
+ char **seq1, char **seq2,
+ char **mseq1, char **mseq2,
+ float **cpmx1, float **cpmx2,
+ short **ijp, int icyc, int jcyc,
+ int ist, int ien, int jst, int jen )
+{
+ int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k, klim;
+ char gap[] = "-";
+ float wm;
+ lgth1 = ien-ist+1;
+ lgth2 = jen-jst+1;
+
+#if 0
+ for( i=0; i<lgth1; i++ )
+ {
+ fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
+ }
+#endif
+
+
+// fprintf( stderr, "in Atracking, lgth1=%d, lgth2=%d\n", lgth1, lgth2 );
+
+ for( i=0; i<lgth1+1; i++ )
+ {
+ ijp[i][0] = i + 1;
+ }
+ for( j=0; j<lgth2+1; j++ )
+ {
+ ijp[0][j] = -( j + 1 );
+ }
+
+
+ for( i=0; i<icyc; i++ )
+ {
+ mseq1[i] += lgth1+lgth2;
+ *mseq1[i] = 0;
+ }
+ for( j=0; j<jcyc; j++ )
+ {
+ mseq2[j] += lgth1+lgth2;
+ *mseq2[j] = 0;
+ }
+
+// if( lgth2 == 1 ) fprintf( stderr, "in Atracking, mseq1 = %s, mseq2 = %s\n", mseq1[0], mseq2[0] );
+
+ iin = lgth1; jin = lgth2;
+ klim = lgth1+lgth2;
+ for( k=0; k<=klim; k++ )
+ {
+ if( ijp[iin][jin] < 0 )
+ {
+ ifi = iin-1; jfi = jin+ijp[iin][jin];
+ }
+ else if( ijp[iin][jin] > 0 )
+ {
+ ifi = iin-ijp[iin][jin]; jfi = jin-1;
+ }
+ else
+ {
+ ifi = iin-1; jfi = jin-1;
+ }
+ l = iin - ifi;
+ while( --l )
+ {
+ for( i=0; i<icyc; i++ )
+ *--mseq1[i] = seq1[i][ist+ifi+l];
+ for( j=0; j<jcyc; j++ )
+ *--mseq2[j] = *gap;
+ k++;
+ }
+ l= jin - jfi;
+ while( --l )
+ {
+ for( i=0; i<icyc; i++ )
+ *--mseq1[i] = *gap;
+ for( j=0; j<jcyc; j++ )
+ *--mseq2[j] = seq2[j][jst+jfi+l];
+ k++;
+ }
+ if( iin <= 0 || jin <= 0 ) break;
+ for( i=0; i<icyc; i++ )
+ *--mseq1[i] = seq1[i][ist+ifi];
+ for( j=0; j<jcyc; j++ )
+ *--mseq2[j] = seq2[j][jst+jfi];
+ k++;
+ iin = ifi; jin = jfi;
+
+// if( lgth2 == 1 ) fprintf( stderr, "in Atracking, mseq1 = %s, mseq2 = %s\n", mseq1[0], mseq2[0] );
+ }
+// fprintf( stderr, "in Atracking (owari), mseq1 = %s\n", mseq1[0] );
+// fprintf( stderr, "in Atracking (owari), mseq2 = %s\n", mseq2[0] );
+ return( 0.0 );
+}
+
+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[4] )
+/* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
+{
+// int k;
+ register int i, j;
+ int ll1, ll2;
+ int lasti, lastj;
+ int resultlen;
+ float wm; /* int ?????? */
+ float g;
+ float *currentw, *previousw;
+ float fpenalty = (float)penalty;
+ float fpenalty_ex = (float)penalty_ex;
+#if 1
+ float *wtmp;
+ short *ijppt;
+ float *mjpt, *prept, *curpt;
+ int *mpjpt;
+#endif
+ float mi, *m;
+ short **ijp;
+ int mpi, *mp;
+ float *w1, *w2;
+ float *initverticalw; /* kufuu sureba iranai */
+ float *lastverticalw; /* kufuu sureba iranai */
+ int **intwork;
+ float **floatwork;
+ short **shortmtx;
+ float dumfl;
+ int lgth1, lgth2;
+ float *ogcp1 = gapinfo[0];
+ float *fgcp1 = gapinfo[1];
+ float *ogcp2 = gapinfo[2];
+ float *fgcp2 = gapinfo[3];
+ static char **aseq1 = NULL;
+ static char **aseq2 = NULL;
+ static char **aseq1bk, **aseq2bk;
+
+// char ttt1[10000], ttt2[10000];
+
+ if( !aseq1 )
+ {
+ aseq1 = AllocateCharMtx( njob, 0 );
+ aseq2 = AllocateCharMtx( njob, 0 );
+ }
+
+ lgth1 = ien-ist+1;
+ lgth2 = jen-jst+1;
+
+// strncpy( ttt1, seq1[0]+ist, lgth1 ); ttt1[lgth1] = 0;
+// strncpy( ttt2, seq2[0]+jst, lgth2 ); ttt2[lgth2] = 0;
+//
+// fprintf( stderr, "in _tanni ist,ien = %d,%d, lgth1=%d\n", ist, ien, lgth1 );
+// fprintf( stderr, "in _tanni jst,jen = %d,%d, lgth2=%d\n", jst, jen, lgth2 );
+// fprintf( stderr, "ttt1 = %s\n", ttt1 );
+// fprintf( stderr, "ttt2 = %s\n", ttt2 );
+
+
+ ll1 = ( (int)(1.3*lgth1) ) + 100;
+ ll2 = ( (int)(1.3*lgth2) ) + 100;
+
+ aseq1bk = AllocateCharMtx( icyc, lgth1+lgth2+100 );
+ aseq2bk = AllocateCharMtx( jcyc, lgth1+lgth2+100 );
+ for( i=0; i<icyc; i++ ) aseq1[i] = aseq1bk[i];
+ for( i=0; i<jcyc; i++ ) aseq2[i] = aseq2bk[i];
+
+ w1 = AllocateFloatVec( ll2+2 );
+ w2 = AllocateFloatVec( ll2+2 );
+
+ initverticalw = AllocateFloatVec( ll1+2 );
+ lastverticalw = AllocateFloatVec( ll1+2 );
+
+ m = AllocateFloatVec( ll2+2 );
+ mp = AllocateIntVec( ll2+2 );
+
+ floatwork = AllocateFloatMtx( MAX( ll1, ll2 )+2, 26 );
+ intwork = AllocateIntMtx( MAX( ll1, ll2 )+2, 26 );
+
+
+ shortmtx = AllocateShortMtx( ll1+1, ll2+1 );
+
+ ijp = shortmtx;
+
+ currentw = w1;
+ previousw = w2;
+
+ match_calc( initverticalw, cpmx2+jst, cpmx1+ist, 0, lgth1, floatwork, intwork, 1 );
+
+ match_calc( currentw, cpmx1+ist, cpmx2+jst, 0, lgth2, floatwork, intwork, 1 );
+
+ for( i=1; i<lgth1+1; i++ )
+ {
+ initverticalw[i] += fpenalty * ( ogcp1[0] + fgcp1[i-1] );
+ }
+ for( j=1; j<lgth2+1; j++ )
+ {
+ currentw[j] += fpenalty * ( ogcp2[0] + fgcp2[j-1] );
+ }
+
+ for( j=1; j<lgth2+1; ++j )
+ {
+ m[j] = currentw[j-1] + fpenalty * ogcp1[1]; mp[j] = 0;;
+ }
+
+ lastverticalw[0] = currentw[lgth2-1];
+
+
+ lasti = lgth1+1;
+ for( i=1; i<lasti; i++ )
+ {
+
+ wtmp = previousw;
+ previousw = currentw;
+ currentw = wtmp;
+
+ previousw[0] = initverticalw[i-1];
+
+ match_calc( currentw, cpmx1+ist, cpmx2+jst, i, lgth2, floatwork, intwork, 0 );
+ currentw[0] = initverticalw[i];
+
+ mi = previousw[0] + fpenalty * ogcp2[1];
+ mpi = 0;
+
+ ijppt = ijp[i] + 1;
+ mjpt = m + 1;
+ prept = previousw;
+ curpt = currentw + 1;
+ mpjpt = mp + 1;
+ lastj = lgth2+1;
+ for( j=1; j<lastj; j++ )
+ {
+ wm = *prept;
+ *ijppt = 0;
+
+#if 0
+ fprintf( stderr, "%5.0f->", wm );
+#endif
+ g = mi + fpenalty * fgcp2[j-1];
+#if 0
+ fprintf( stderr, "%5.0f?", g );
+#endif
+ if( g > wm )
+ {
+ wm = g;
+ *ijppt = -( j - mpi );
+ }
+ g = *prept + fpenalty * ogcp2[j];
+ if( g >= mi )
+ {
+ mi = g;
+ mpi = j-1;
+ }
+#if USE_PENALTY_EX
+ mi += fpenalty_ex;
+#endif
+
+ g = *mjpt + fpenalty * fgcp1[i-1];
+#if 0
+ fprintf( stderr, "%5.0f?", g );
+#endif
+ if( g > wm )
+ {
+ wm = g;
+ *ijppt = +( i - *mpjpt );
+ }
+ g = *prept + fpenalty * ogcp1[i];
+ if( g >= *mjpt )
+ {
+ *mjpt = g;
+ *mpjpt = i-1;
+ }
+#if USE_PENALTY_EX
+ m[j] += fpenalty_ex;
+#endif
+
+#if 0
+ fprintf( stderr, "%5.0f ", wm );
+#endif
+ *curpt += wm;
+
+
+ ijppt++;
+ mjpt++;
+ prept++;
+ mpjpt++;
+ curpt++;
+ }
+ lastverticalw[i] = currentw[lgth2-1];
+ }
+
+
+ Atracking( currentw, lastverticalw, seq1, seq2, aseq1, aseq2, cpmx1+ist, cpmx2+jst, ijp, icyc, jcyc, ist, ien, jst, jen );
+
+ for( i=0; i<icyc; i++ ) strcpy( mseq1[i], aseq1[i] );
+ for( i=0; i<jcyc; i++ ) strcpy( mseq2[i], aseq2[i] );
+
+// fprintf( stderr, "in _tanni, aseq1 = %s\n", aseq1[0] );
+// fprintf( stderr, "in _tanni, mseq1 = %s\n", mseq1[0] );
+
+ FreeFloatVec( w1 );
+ FreeFloatVec( w2 );
+ FreeFloatVec( initverticalw );
+ FreeFloatVec( lastverticalw );
+
+ FreeFloatVec( m );
+ FreeIntVec( mp );
+
+
+ FreeFloatMtx( floatwork );
+ FreeIntMtx( intwork );
+
+ FreeShortMtx( shortmtx );
+
+ FreeCharMtx( aseq1bk );
+ FreeCharMtx( aseq2bk );
+
+ return( wm );
+}
+
+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[4] )
+/* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
+{
+// int k;
+ register int i, j;
+ char **aseq1, **aseq2;
+ int ll1, ll2, l, len;
+ int lasti, lastj, imid, jmid;
+ int resultlen;
+ float wm; /* int ?????? */
+ float g;
+ float *currentw, *previousw;
+ float fpenalty = (float)penalty;
+ float fpenalty_ex = (float)penalty_ex;
+ float *wtmp;
+// short *ijppt;
+ int *mpjpt;
+// short **ijp;
+ int *mp;
+ int mpi;
+ float *mjpt, *prept, *curpt;
+ float mi;
+ float *m;
+ float *w1, *w2;
+// float *match;
+ float *initverticalw; /* kufuu sureba iranai */
+ float *lastverticalw; /* kufuu sureba iranai */
+ int **intwork;
+ float **floatwork;
+// short **shortmtx;
+#if STOREWM
+// float **WMMTX;
+// float **WMMTX2;
+#endif
+ float *midw;
+ float *midm;
+ float *midn;
+ float dumfl;
+ int lgth1, lgth2;
+ float maxwm;
+ int *jumpforwi;
+ int *jumpforwj;
+ int *jumpbacki;
+ int *jumpbackj;
+ int *jumpdummi; //muda
+ int *jumpdummj; //muda
+ int jumpi, jumpj;
+ char *gaps;
+ int ijpi, ijpj;
+// float *ogcp1 = gapinfo[0];
+// float *fgcp1 = gapinfo[1];
+// float *ogcp2 = gapinfo[2];
+// float *fgcp2 = gapinfo[3];
+// static char ttt1[50000];
+// static char ttt2[50000];
+
+ depth++;
+ reccycle++;
+
+ lgth1 = ien-ist+1;
+ lgth2 = jen-jst+1;
+
+// fprintf( stderr, "==== MSalign (depth=%d, reccycle=%d), ist=%d, ien=%d, jst=%d, jen=%d\n", depth, reccycle, ist, ien, jst, jen );
+// strncpy( ttt1, seq1[0]+ist, lgth1 );
+// strncpy( ttt2, seq2[0]+jst, lgth2 );
+// ttt1[lgth1] = 0;
+// ttt2[lgth2] = 0;
+// fprintf( stderr, "seq1 = %s\n", ttt1 );
+// fprintf( stderr, "seq2 = %s\n", ttt2 );
+ if( lgth2 <= 0 )
+ {
+ fprintf( stderr, "==== jimei\n" );
+ for( i=0; i<icyc; i++ )
+ {
+ strncpy( mseq1[i], seq1[i]+ist, lgth1 );
+ mseq1[i][lgth1] = 0;
+ }
+ for( i=0; i<jcyc; i++ )
+ {
+ mseq2[i][0] = 0;
+ for( j=0; j<lgth1; j++ )
+ strcat( mseq2[i], "-" );
+ }
+
+// fprintf( stderr, "==== mseq1[0] (%d) = %s\n", depth, mseq1[0] );
+// fprintf( stderr, "==== mseq2[0] (%d) = %s\n", depth, mseq2[0] );
+
+ return( (float)offset * lgth1 );
+ }
+
+ aseq1 = AllocateCharMtx( icyc, lgth1+lgth2+100 );
+ aseq2 = AllocateCharMtx( jcyc, lgth1+lgth2+100 );
+
+ if( lgth1 < DPTANNI || lgth2 < DPTANNI )
+ {
+// fprintf( stderr, "==== Going to _tanni\n" );
+
+ wm = MSalignmm_tanni( icyc, jcyc, eff1, eff2, seq1, seq2, cpmx1, cpmx2, ist, ien, jst, jen, alloclen, aseq1, aseq2, gapinfo );
+
+
+ for( i=0; i<icyc; i++ ) strcpy( mseq1[i], aseq1[i] );
+ for( i=0; i<jcyc; i++ ) strcpy( mseq2[i], aseq2[i] );
+
+// fprintf( stderr, "==== mseq1[0] (%d) = %s\n", depth, mseq1[0] );
+// fprintf( stderr, "==== mseq2[0] (%d) = %s\n", depth, mseq2[0] );
+
+// fprintf( stderr, "freeing aseq\n" );
+ FreeCharMtx( aseq1 );
+ FreeCharMtx( aseq2 );
+
+ return( wm );
+ }
+// fprintf( stderr, "Trying to divide the mtx\n" );
+
+ ll1 = ( (int)(1.3*lgth1) ) + 100;
+ ll2 = ( (int)(1.3*lgth2) ) + 100;
+
+// fprintf( stderr, "ll1,ll2=%d,%d\n", ll1, ll2 );
+
+ w1 = AllocateFloatVec( ll2+2 );
+ w2 = AllocateFloatVec( ll2+2 );
+// match = AllocateFloatVec( ll2+2 );
+ midw = AllocateFloatVec( ll2+2 );
+ midn = AllocateFloatVec( ll2+2 );
+ midm = AllocateFloatVec( ll2+2 );
+ jumpbacki = AllocateIntVec( ll2+2 );
+ jumpbackj = AllocateIntVec( ll2+2 );
+ jumpforwi = AllocateIntVec( ll2+2 );
+ jumpforwj = AllocateIntVec( ll2+2 );
+ jumpdummi = AllocateIntVec( ll2+2 );
+ jumpdummj = AllocateIntVec( ll2+2 );
+
+ initverticalw = AllocateFloatVec( ll1+2 );
+ lastverticalw = AllocateFloatVec( ll1+2 );
+
+ m = AllocateFloatVec( ll2+2 );
+ mp = AllocateIntVec( ll2+2 );
+ gaps = AllocateCharVec( MAX( ll1, ll2 ) + 2 );
+
+ floatwork = AllocateFloatMtx( MAX( ll1, ll2 )+2, 26 );
+ intwork = AllocateIntMtx( MAX( ll1, ll2 )+2, 26 );
+
+#if DEBUG
+ fprintf( stderr, "succeeded\n" );
+#endif
+
+#if STOREWM
+ WMMTX = AllocateFloatMtx( ll1, ll2 );
+ WMMTX2 = AllocateFloatMtx( ll1, ll2 );
+#endif
+#if 0
+ shortmtx = AllocateShortMtx( ll1, ll2 );
+
+#if DEBUG
+ fprintf( stderr, "succeeded\n\n" );
+#endif
+
+ ijp = shortmtx;
+#endif
+
+ currentw = w1;
+ previousw = w2;
+
+ match_calc( initverticalw, cpmx2+jst, cpmx1+ist, 0, lgth1, floatwork, intwork, 1 );
+
+ match_calc( currentw, cpmx1+ist, cpmx2+jst, 0, lgth2, floatwork, intwork, 1 );
+
+ for( i=1; i<lgth1+1; i++ )
+ {
+// initverticalw[i] += fpenalty * ( ogcp1[0] + fgcp1[i-1] );
+ initverticalw[i] += fpenalty;
+ }
+ for( j=1; j<lgth2+1; j++ )
+ {
+// currentw[j] += fpenalty * ( ogcp2[0] + fgcp2[j-1] );
+ currentw[j] += fpenalty;
+ }
+
+#if STOREWM
+ WMMTX[0][0] = initverticalw[0];
+ for( i=1; i<lgth1+1; i++ )
+ {
+ WMMTX[i][0] = initverticalw[i];
+ }
+ for( j=1; j<lgth2+1; j++ )
+ {
+ WMMTX[0][j] = currentw[j];
+ }
+#endif
+
+ for( j=1; j<lgth2+1; ++j )
+ {
+// m[j] = currentw[j-1] + fpenalty * ogcp1[1];
+ m[j] = currentw[j-1];
+ mp[j] = 0;
+ }
+
+ lastverticalw[0] = currentw[lgth2-1];
+
+ imid = lgth1 / 2;
+
+ jumpi = 0; // atode kawaru.
+ lasti = lgth1+1;
+// for( i=1; i<lasti; i++ )
+ for( i=1; i<=imid; i++ )
+ {
+ wtmp = previousw;
+ previousw = currentw;
+ currentw = wtmp;
+
+ previousw[0] = initverticalw[i-1];
+
+ match_calc( currentw, cpmx1+ist, cpmx2+jst, i, lgth2, floatwork, intwork, 0 );
+ currentw[0] = initverticalw[i];
+
+
+// mi = previousw[0] + fpenalty * ogcp2[1];
+ mi = previousw[0];
+ mpi = 0;
+
+// ijppt = ijp[i] + 1;
+ mjpt = m + 1;
+ prept = previousw;
+ curpt = currentw + 1;
+ mpjpt = mp + 1;
+
+
+ lastj = lgth2+1;
+ for( j=1; j<lastj; j++ )
+ {
+
+ wm = *prept;
+// *ijppt = 0;
+
+#if 0
+ fprintf( stderr, "%5.0f->", wm );
+#endif
+ g = mi + fpenalty;
+#if 0
+ fprintf( stderr, "%5.0f?", g );
+#endif
+ if( g > wm )
+ {
+ wm = g;
+// *ijppt = -( j - mpi );
+ }
+// g = *prept + fpenalty * ogcp2[j];
+ g = *prept;
+ if( g >= mi )
+ {
+ mi = g;
+ mpi = j-1;
+ }
+#if USE_PENALTY_EX
+ mi += fpenalty_ex;
+#endif
+
+// g = *mjpt + fpenalty * fgcp1[i-1];
+ g = *mjpt + fpenalty;
+#if 0
+ fprintf( stderr, "%5.0f?", g );
+#endif
+ if( g > wm )
+ {
+ wm = g;
+// *ijppt = +( i - *mpjpt );
+ }
+
+
+// g = *prept + fpenalty * ogcp1[i];
+ g = *prept;
+ if( g >= *mjpt )
+ {
+ *mjpt = g;
+ *mpjpt = i-1;
+ }
+#if USE_PENALTY_EX
+ m[j] += fpenalty_ex;
+#endif
+
+#if 0
+ fprintf( stderr, "%5.0f ", wm );
+#endif
+ *curpt += wm;
+
+#if STOREWM
+ WMMTX[i][j] = *curpt;
+ WMMTX2[i][j] = *mjpt;
+#endif
+
+ if( i == imid ) //muda
+ {
+ jumpbackj[j] = *mpjpt; // muda atode matomeru
+ jumpbacki[j] = mpi; // muda atode matomeru
+// fprintf( stderr, "jumpbackj[%d] in forward dp is %d\n", j, *mpjpt );
+// fprintf( stderr, "jumpbacki[%d] in forward dp is %d\n", j, mpi );
+ midw[j] = *curpt;
+ midm[j] = *mjpt;
+ midn[j] = mi;
+ }
+
+ mjpt++;
+ prept++;
+ mpjpt++;
+ curpt++;
+
+ }
+ lastverticalw[i] = currentw[lgth2-1];
+#if 0 // ue
+ if( i == imid )
+ {
+ for( j=0; j<lgth2; j++ ) midw[j] = currentw[j];
+ for( j=0; j<lgth2; j++ ) midm[j] = m[j];
+ }
+#endif
+ }
+// for( j=0; j<lgth2; j++ ) midw[j] = WMMTX[imid][j];
+// for( j=0; j<lgth2; j++ ) midm[j] = WMMTX2[imid][j];
+
+#if 0
+ for( i=0; i<lgth1; i++ )
+ {
+ for( j=0; j<lgth2; j++ )
+ {
+ fprintf( stderr, "% 10.2f ", WMMTX[i][j] );
+ }
+ fprintf( stderr, "\n" );
+ }
+ fprintf( stderr, "\n" );
+ fprintf( stderr, "WMMTX2 = \n" );
+ for( i=0; i<lgth1; i++ )
+ {
+ for( j=0; j<lgth2; j++ )
+ {
+ fprintf( stderr, "% 10.2f ", WMMTX2[i][j] );
+ }
+ fprintf( stderr, "\n" );
+ }
+ fprintf( stderr, "\n" );
+#endif
+
+// gyakudp
+
+ match_calc( initverticalw, cpmx2+jst, cpmx1+ist, lgth2-1, lgth1, floatwork, intwork, 1 );
+ match_calc( currentw, cpmx1+ist, cpmx2+jst, lgth1-1, lgth2, floatwork, intwork, 1 );
+
+ for( i=0; i<lgth1-1; i++ )
+ {
+// initverticalw[i] += fpenalty * ( fgcp1[lgth1-1] + ogcp1[i+1] );
+ initverticalw[i] += fpenalty;
+ }
+ for( j=0; j<lgth2-1; j++ )
+ {
+// currentw[j] += fpenalty * ( fgcp2[lgth1-1] + ogcp2[j+1] );
+ currentw[j] += fpenalty;
+ }
+
+#if STOREWM
+ for( i=0; i<lgth1-1; i++ )
+ {
+ WMMTX[i][lgth2-1] += fpenalty;
+ }
+ for( j=0; j<lgth2-1; j++ )
+ {
+ WMMTX[lgth1-1][j] += fpenalty;
+ }
+#endif
+
+ for( j=lgth2-1; j>0; --j )
+ {
+// m[j-1] = currentw[j] + fpenalty * fgcp2[lgth2-2];
+ m[j-1] = currentw[j];
+ mp[j] = lgth1-1;
+ }
+
+// for( j=0; j<lgth2; j++ ) m[j] = 0.0;
+ // m[lgth2-1] ha irunoka?
+
+// for( i=lgth1-2; i>=imid; i-- )
+ for( i=lgth1-2; i>-1; i-- )
+ {
+ wtmp = previousw;
+ previousw = currentw;
+ currentw = wtmp;
+ previousw[lgth2-1] = initverticalw[i+1];
+// match_calc( currentw, seq1, seq2, i, lgth2 );
+ match_calc( currentw, cpmx1+ist, cpmx2+jst, i, lgth2, floatwork, intwork, 0 );
+
+ currentw[lgth2-1] = initverticalw[i];
+
+// mi = previousw[lgth2-1] + fpenalty * fgcp2[lgth2-2];
+ mi = previousw[lgth2-1];
+ mpi = lgth2 - 1;
+
+ mjpt = m + lgth2 - 2;
+ prept = previousw + lgth2 - 1;
+ curpt = currentw + lgth2 - 2;
+ mpjpt = mp + lgth2 - 2;
+
+
+ for( j=lgth2-2; j>-1; j-- )
+ {
+ wm = *prept;
+ ijpi = i+1;
+ ijpj = j+1;
+
+// g = mi + fpenalty * ogcp2[j+1];
+ g = mi + fpenalty;
+ if( g > wm )
+ {
+ wm = g;
+ ijpj = mpi;
+ }
+
+// g = *prept + fpenalty + fgcp2[j];
+ g = *prept;
+ if( g > mi )
+ {
+// fprintf( stderr, "i,j=%d,%d - renewed! mpi = %d\n", i, j, j+1 );
+ mi = g;
+ mpi = j + 1;
+ }
+
+#if USE_PENALTY_EX
+ mi += fpenalty_ex;
+#endif
+
+// fprintf( stderr, "i,j=%d,%d *mpjpt = %d\n", i, j, *mpjpt );
+// g = *mjpt + fpenalty * ogcp1[i+1];
+ g = *mjpt + fpenalty;
+ if( g > wm )
+ {
+ wm = g;
+ ijpi = *mpjpt;
+ }
+
+// if( i == imid )fprintf( stderr, "i,j=%d,%d \n", i, j );
+// g = *prept + fpenalty * fgcp1[i];
+ g = *prept;
+ if( g > *mjpt )
+ {
+ *mjpt = g;
+ *mpjpt = i + 1;
+ }
+
+#if USE_PENALTY_EX
+ m[j] += fpenalty_ex;
+#endif
+
+ if( i == jumpi || i == imid - 1 )
+ {
+ jumpforwi[j] = ijpi; //muda
+ jumpforwj[j] = ijpj; //muda
+// fprintf( stderr, "jumpfori[%d] = %d\n", j, ijpi );
+// fprintf( stderr, "jumpforj[%d] = %d\n", j, ijpj );
+ }
+ if( i == imid ) // muda
+ {
+ midw[j] += wm;
+ midm[j+1] += *mjpt + fpenalty; //??????
+ }
+ if( i == imid - 1 )
+ {
+ midn[j] += mi + fpenalty; //????
+ }
+#if STOREWM
+ WMMTX[i][j] += wm;
+ WMMTX2[i][j+1] += *mjpt + fpenalty;
+#endif
+ *curpt += wm;
+
+ mjpt--;
+ prept--;
+ mpjpt--;
+ curpt--;
+ }
+
+ if( i == imid - 1 )
+ {
+ maxwm = midw[1];
+ jmid = 1;
+ for( j=2; j<lgth2; j++ )
+ {
+ wm = midw[j];
+ if( wm > maxwm )
+ {
+ jmid = j;
+ maxwm = wm;
+ }
+ wm = midm[j];
+ if( wm > maxwm )
+ {
+ jmid = j;
+ maxwm = wm;
+ }
+ }
+// fprintf( stderr, "imid=%d, jmid=%d\n", imid, jmid );
+ wm = midw[jmid];
+ {
+ jumpi = imid-1;
+ jumpj = jmid-1;
+ }
+ if( midm[jmid] > wm )
+ {
+ jumpi = jumpbackj[jmid];
+ jumpj = jmid-1;
+ }
+ if( midn[jmid-1] >= wm )
+ {
+ jumpi = imid-1;
+ jumpj = jumpbacki[jmid];
+ }
+#if 0
+ fprintf( stderr, "imid = %d\n", imid );
+ fprintf( stderr, "midn = \n" );
+ for( j=0; j<lgth2; j++ )
+ {
+ fprintf( stderr, "% 10.2f ", midn[j] );
+ }
+ fprintf( stderr, "\n" );
+ fprintf( stderr, "midw = \n" );
+ for( j=0; j<lgth2; j++ )
+ {
+ fprintf( stderr, "% 10.2f ", midw[j] );
+ }
+ fprintf( stderr, "\n" );
+ fprintf( stderr, "midm = \n" );
+ for( j=0; j<lgth2; j++ )
+ {
+ fprintf( stderr, "% 10.2f ", midm[j] );
+ }
+ fprintf( stderr, "\n" );
+#endif
+ }
+ if( i == jumpi ) //saki?
+ {
+// fprintf( stderr, "imid, jumpi = %d,%d\n", imid, jumpi );
+// fprintf( stderr, "jmid, jumpj = %d,%d\n", jmid, jumpj );
+ imid = jumpforwi[jumpj];
+ jmid = jumpforwj[jumpj];
+// fprintf( stderr, "imid -> %d\n", imid );
+// fprintf( stderr, "jmid -> %d\n", jmid );
+ break;
+ }
+ }
+// fprintf( stderr, "imid = %d, but jumpi = %d\n", imid, jumpi );
+// fprintf( stderr, "jmid = %d, but jumpj = %d\n", jmid, jumpj );
+
+// for( j=0; j<lgth2; j++ ) midw[j] += currentw[j];
+// for( j=0; j<lgth2; j++ ) midm[j] += m[j+1];
+// for( j=0; j<lgth2; j++ ) midw[j] += WMMTX[imid][j];
+// for( j=0; j<lgth2; j++ ) midw[j] += WMMTX[imid][j];
+
+
+#if 0
+ fprintf( stderr, "WMMTX = \n" );
+ for( i=0; i<lgth1; i++ )
+ {
+ fprintf( stderr, "%d ", i );
+ for( j=0; j<lgth2; j++ )
+ {
+ fprintf( stderr, "% 10.2f ", WMMTX[i][j] );
+ }
+ fprintf( stderr, "\n" );
+ }
+ fprintf( stderr, "WMMTX2 = (p = %f)\n", fpenalty );
+ for( i=0; i<lgth1; i++ )
+ {
+ fprintf( stderr, "%d ", i );
+ for( j=0; j<lgth2; j++ )
+ {
+ fprintf( stderr, "% 10.2f ", WMMTX2[i][j] );
+ }
+ fprintf( stderr, "\n" );
+ }
+
+ fprintf( stderr, "jumpbacki = \n" );
+ for( j=0; j<lgth2; j++ )
+ {
+ fprintf( stderr, "% 10d ", jumpbacki[j] );
+ }
+ fprintf( stderr, "\n" );
+ fprintf( stderr, "jumpbackj = \n" );
+ for( j=0; j<lgth2; j++ )
+ {
+ fprintf( stderr, "% 10d ", jumpbackj[j] );
+ }
+ fprintf( stderr, "\n" );
+ fprintf( stderr, "jumpforwi = \n" );
+ for( j=0; j<lgth2; j++ )
+ {
+ fprintf( stderr, "% 10d ", jumpforwi[j] );
+ }
+ fprintf( stderr, "\n" );
+ fprintf( stderr, "jumpforwj = \n" );
+ for( j=0; j<lgth2; j++ )
+ {
+ fprintf( stderr, "% 10d ", jumpforwj[j] );
+ }
+ fprintf( stderr, "\n" );
+
+
+#endif
+
+// Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
+
+#if 0 // irukamo
+ resultlen = strlen( mseq1[0] );
+ if( alloclen < resultlen || resultlen > N )
+ {
+ fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
+ ErrorExit( "LENGTH OVER!\n" );
+ }
+#endif
+
+
+
+#if 0
+ fprintf( stderr, "jumpi = %d, imid = %d\n", jumpi, imid );
+ fprintf( stderr, "jumpj = %d, jmid = %d\n", jumpj, jmid );
+
+ fprintf( stderr, "imid = %d\n", imid );
+ fprintf( stderr, "jmid = %d\n", jmid );
+#endif
+
+
+ FreeFloatVec( w1 );
+ FreeFloatVec( w2 );
+ FreeFloatVec( initverticalw );
+ FreeFloatVec( lastverticalw );
+ FreeFloatVec( midw );
+ FreeFloatVec( midm );
+ FreeFloatVec( midn );
+
+ FreeIntVec( jumpbacki );
+ FreeIntVec( jumpbackj );
+ FreeIntVec( jumpforwi );
+ FreeIntVec( jumpforwj );
+ FreeIntVec( jumpdummi );
+ FreeIntVec( jumpdummj );
+
+ FreeFloatVec( m );
+ FreeIntVec( mp );
+
+ FreeFloatMtx( floatwork );
+ FreeIntMtx( intwork );
+
+#if STOREWM
+ FreeFloatMtx( WMMTX );
+ FreeFloatMtx( WMMTX2 );
+#endif
+
+// fprintf( stderr, "==== calling myself (first)\n" );
+
+ MSalignmm_rec( icyc, jcyc, eff1, eff2, seq1, seq2, cpmx1, cpmx2, ist, ist+jumpi, jst, jst+jumpj, alloclen, aseq1, aseq2, depth, gapinfo );
+ for( i=0; i<icyc; i++ ) strcpy( mseq1[i], aseq1[i] );
+ for( i=0; i<jcyc; i++ ) strcpy( mseq2[i], aseq2[i] );
+
+// fprintf( stderr, "====(f) aseq1[0] (%d) = %s (%d-%d)\n", depth, aseq1[0], ist, ien );
+// fprintf( stderr, "====(f) aseq2[0] (%d) = %s (%d-%d)\n", depth, aseq2[0], jst, jen );
+
+ len = strlen( mseq1[0] );
+// fprintf( stderr, "len = %d\n", len );
+ l = jmid - jumpj - 1;
+// fprintf( stderr, "l=%d\n", l );
+ if( l > 0 )
+ {
+ for( i=0; i<l; i++ ) gaps[i] = '-'; gaps[i] = 0;
+ for( i=0; i<icyc; i++ )
+ {
+ strcat( mseq1[i], gaps );
+ mseq1[i][len+l] = 0;
+ }
+ for( j=0; j<jcyc; j++ )
+ {
+ strncat( mseq2[j], seq2[j]+jst+jumpj+1, l );
+ mseq2[j][len+l] = 0;
+ }
+ }
+ len = strlen( mseq1[0] );
+ l = imid - jumpi - 1;
+// fprintf( stderr, "l=%d\n", l );
+ if( l > 0 )
+ {
+ for( i=0; i<l; i++ ) gaps[i] = '-'; gaps[i] = 0;
+ for( i=0; i<icyc; i++ )
+ {
+ strncat( mseq1[i], seq1[i]+ist+jumpi+1, l );
+ mseq1[i][len+l] = 0;
+ }
+ for( j=0; j<jcyc; j++ )
+ {
+ strcat( mseq2[j], gaps );
+ mseq2[j][len+l] = 0;
+ }
+ }
+#if DEBUG
+ fprintf( stderr, "after gapfill mseq1[0]=%s\n", mseq1[0] );
+ fprintf( stderr, "after gapfill mseq2[0]=%s\n", mseq2[0] );
+#endif
+
+// fprintf( stderr, "==== calling myself (second)\n" );
+
+ MSalignmm_rec( icyc, jcyc, eff1, eff2, seq1, seq2, cpmx1, cpmx2, ist+imid, ien, jst+jmid, jen, alloclen, aseq1, aseq2, depth, gapinfo );
+
+ for( i=0; i<icyc; i++ ) strcat( mseq1[i], aseq1[i] );
+ for( i=0; i<jcyc; i++ ) strcat( mseq2[i], aseq2[i] );
+
+// fprintf( stderr, "====(s) aseq1[0] (%d) = %s (%d-%d)\n", depth, aseq1[0], ist, ien );
+// fprintf( stderr, "====(s) aseq2[0] (%d) = %s (%d-%d)\n", depth, aseq2[0], jst, jen );
+
+// fprintf( stderr, "==== mseq1[0] (%d) = %s\n", depth, mseq1[0] );
+// fprintf( stderr, "==== mseq2[0] (%d) = %s\n", depth, mseq2[0] );
+
+ free( gaps );
+ FreeCharMtx( aseq1 );
+ FreeCharMtx( aseq2 );
+
+ return( wm );
+}
+
+
+
+float MSalignmm( char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen )
+/* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
+{
+// int k;
+ int i, j;
+ int ll1, ll2;
+ int lgth1, lgth2;
+ float wm; /* int ?????? */
+ static char **mseq1 = NULL;
+ static char **mseq2 = NULL;
+ char **mseq;
+ float *ogcp1;
+ float *ogcp2;
+ float *fgcp1;
+ float *fgcp2;
+ float **cpmx1;
+ float **cpmx2;
+ float *gapinfo[4];
+
+#if 0
+ fprintf( stderr, "eff in SA+++align\n" );
+ for( i=0; i<icyc; i++ ) fprintf( stderr, "eff1[%d] = %f\n", i, eff1[i] );
+#endif
+ if( mseq1 == NULL )
+ {
+ mseq1 = AllocateCharMtx( njob, 0 );
+ mseq2 = AllocateCharMtx( njob, 0 );
+ }
+
+ lgth1 = strlen( seq1[0] );
+ lgth2 = strlen( seq2[0] );
+
+ ll1 = ( (int)(1.3*lgth1) ) + 100;
+ ll2 = ( (int)(1.3*lgth2) ) + 100;
+
+ mseq = AllocateCharMtx( njob, ll1+ll2 );
+
+ ogcp1 = AllocateFloatVec( ll1+2 );
+ ogcp2 = AllocateFloatVec( ll2+2 );
+ fgcp1 = AllocateFloatVec( ll1+2 );
+ fgcp2 = AllocateFloatVec( ll2+2 );
+
+ cpmx1 = AllocateFloatMtx( ll1+2, 26 );
+ cpmx2 = AllocateFloatMtx( ll2+2, 26 );
+
+ for( i=0; i<icyc; i++ ) mseq1[i] = mseq[i];
+ for( j=0; j<jcyc; j++ ) mseq2[j] = mseq[icyc+j];
+
+ MScpmx_calc_new( seq1, cpmx1, eff1, lgth1, icyc );
+ MScpmx_calc_new( seq2, cpmx2, eff2, lgth2, jcyc );
+
+#if 1
+ OpeningGapCount( ogcp1, icyc, seq1, eff1 );
+ OpeningGapCount( ogcp2, jcyc, seq2, eff2 );
+ FinalGapCount( fgcp1, icyc, seq1, eff1 );
+ FinalGapCount( fgcp2, jcyc, seq2, eff2 );
+
+ for( i=0; i<lgth1; i++ )
+ {
+ ogcp1[i] = 0.5 * ( 1.0 - ogcp1[i] );
+ fgcp1[i] = 0.5 * ( 1.0 - fgcp1[i] );
+ }
+ for( i=0; i<lgth2; i++ )
+ {
+ ogcp2[i] = 0.5 * ( 1.0 - ogcp2[i] );
+ fgcp2[i] = 0.5 * ( 1.0 - fgcp2[i] );
+ }
+
+ gapinfo[0] = ogcp1;
+ gapinfo[1] = fgcp1;
+ gapinfo[2] = ogcp2;
+ gapinfo[3] = fgcp2;
+#endif
+
+#if 0
+ fprintf( stdout, "in MSalignmm.c\n" );
+ for( i=0; i<icyc; i++ )
+ {
+ fprintf( stdout, ">%d of GROUP1\n", i );
+ fprintf( stdout, "%s\n", seq1[i] );
+ }
+ for( i=0; i<jcyc; i++ )
+ {
+ fprintf( stdout, ">%d of GROUP2\n", i );
+ fprintf( stdout, "%s\n", seq2[i] );
+ }
+ fflush( stdout );
+#endif
+
+ wm = MSalignmm_rec( icyc, jcyc, eff1, eff2, seq1, seq2, cpmx1, cpmx2, 0, lgth1-1, 0, lgth2-1, alloclen, mseq1, mseq2, 0, gapinfo );
+
+ for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
+ for( i=0; i<jcyc; i++ ) strcpy( seq2[i], mseq2[i] );
+
+
+ FreeFloatVec( ogcp1 );
+ FreeFloatVec( ogcp2 );
+ FreeFloatVec( fgcp1 );
+ FreeFloatVec( fgcp2 );
+ FreeFloatMtx( cpmx1 );
+ FreeFloatMtx( cpmx2 );
+
+ FreeCharMtx( mseq );
+
+ return( wm );
+}