JWS-112 Bumping version of Mafft to version 7.310.
[jabaws.git] / binaries / src / mafft / core / dvtditr.c
index 54b44c6..a258902 100644 (file)
@@ -5,9 +5,31 @@
 
 extern char **seq_g;
 extern char **res_g;
+static int subalignment;
+static int subalignmentoffset;
+static int specifictarget;
 
 static int intop;
 static int intree;
+static double autosubalignment;
+
+
+static void calcmaxdistclass( void )
+{
+       int c;
+       double rep;
+       for( c=0; c<ndistclass; c++ )
+       {
+               rep = (double) 2 * c / ndistclass; // dist:0-2 for dist2offset 
+//             fprintf( stderr, "c=%d, rep=%f, offset=%f\n", c, rep, dist2offset( rep )  );
+               if( dist2offset( rep ) == 0.0 )
+                       break;
+       }
+       fprintf( stderr, "ndistclass = %d, maxdistclass = %d\n", ndistclass, c+1 );
+       maxdistclass = c + 1;
+//     maxdistclass = ndistclass; // CHUUI!!!!
+       return;
+}
 
 void arguments( int argc, char *argv[] )
 {
@@ -18,6 +40,7 @@ void arguments( int argc, char *argv[] )
        nthread = 1;
        randomseed = 0;
        scoreout = 0;
+       spscoreout = 0;
        parallelizationstrategy = BAATARI1;
        intop = 0;
        intree = 0;
@@ -56,9 +79,11 @@ void arguments( int argc, char *argv[] )
        checkC = 0;
        tbitr = 0;
        treemethod = 'X';
+       sueff_global = 0.1;
        scoremtx = 1;
        dorp = NOTSPECIFIED;
        ppenalty = NOTSPECIFIED;
+       penalty_shift_factor = 1000.0;
        ppenalty_ex = NOTSPECIFIED;
        poffset = NOTSPECIFIED;
        kimuraR = NOTSPECIFIED;
@@ -72,6 +97,13 @@ void arguments( int argc, char *argv[] )
        TMorJTT = JTT;
        consweight_multi = 1.0;
        consweight_rna = 0.0;
+       subalignment = 0;
+       subalignmentoffset = 0;
+       legacygapcost = 0;
+       specificityconsideration = 0.0;
+       autosubalignment = 0.0;
+       specifictarget = 0;
+       nwildcard = 0;
 
        while( --argc > 0 && (*++argv)[0] == '-' )
        {
@@ -85,7 +117,7 @@ void arguments( int argc, char *argv[] )
                                        --argc;
                                        goto nextoption;
                                case 'I':
-                                       niter = atoi( *++argv );
+                                       niter = myatoi( *++argv );
                                        fprintf( stderr, "niter = %d\n", niter );
                                        --argc;
                                        goto nextoption;
@@ -102,6 +134,15 @@ void arguments( int argc, char *argv[] )
 //                                     fprintf( stderr, "ppenalty = %d\n", ppenalty );
                                        --argc;
                                        goto nextoption;
+                               case 'Q':
+                                       penalty_shift_factor = atof( *++argv );
+                                       if( penalty_shift_factor < 100.0 && penalty_shift_factor != 2.0 )
+                                       {
+                                               fprintf( stderr, "%f, penalty_shift is fixed to penalty x 2 in the iterative refinement phase.\n", penalty_shift_factor );
+                                               penalty_shift_factor = 2.0;
+                                       }
+                                       --argc;
+                                       goto nextoption;
                                case 'g':
                                        ppenalty_ex = (int)( atof( *++argv ) * 1000 - 0.5 );
 //                                     fprintf( stderr, "ppenalty_ex = %d\n", ppenalty_ex );
@@ -113,25 +154,25 @@ void arguments( int argc, char *argv[] )
                                        --argc;
                                        goto nextoption;
                                case 'k':
-                                       kimuraR = atoi( *++argv );
+                                       kimuraR = myatoi( *++argv );
                                        fprintf( stderr, "kappa = %d\n", kimuraR );
                                        --argc; 
                                        goto nextoption;
                                case 'b':
-                                       nblosum = atoi( *++argv );
+                                       nblosum = myatoi( *++argv );
                                        scoremtx = 1;
                                        fprintf( stderr, "blosum %d / kimura 200\n", nblosum );
                                        --argc; 
                                        goto nextoption;
                                case 'j':
-                                       pamN = atoi( *++argv );
+                                       pamN = myatoi( *++argv );
                                        scoremtx = 0;
                                        TMorJTT = JTT;
                                        fprintf( stderr, "jtt/kimura %d\n", pamN );
                                        --argc; 
                                        goto nextoption;
                                case 'm':
-                                       pamN = atoi( *++argv );
+                                       pamN = myatoi( *++argv );
                                        scoremtx = 0;
                                        TMorJTT = TM;
                                        fprintf( stderr, "tm %d\n", pamN );
@@ -152,12 +193,17 @@ void arguments( int argc, char *argv[] )
                                        --argc;
                                        goto nextoption;
                                case 'C':
-                                       nthread = atoi( *++argv );
+                                       nthread = myatoi( *++argv );
                                        fprintf( stderr, "nthread = %d\n", nthread );
                                        --argc; 
                                        goto nextoption;
+                               case 'H':
+                                       subalignment = 1;
+                                       subalignmentoffset = myatoi( *++argv );
+                                       --argc;
+                                       goto nextoption;
                                case 't':
-                                       randomseed = atoi( *++argv );
+                                       randomseed = myatoi( *++argv );
                                        fprintf( stderr, "randomseed = %d\n", randomseed );
                                        --argc; 
                                        goto nextoption;
@@ -175,12 +221,25 @@ void arguments( int argc, char *argv[] )
 //                                     exit( 1 );
                                        --argc; 
                                        goto nextoption;
+                               case 's':
+                                       specificityconsideration = (double)myatof( *++argv );
+//                                     fprintf( stderr, "specificityconsideration = %f\n", specificityconsideration );
+                                       --argc; 
+                                       goto nextoption;
+#if 0
+                               case 'S' :
+                                       scoreout = 1; // for checking parallel calculation
+                                       break;
+#else
                                case 'S' :
-                                       scoreout = 1;
+                                       spscoreout = 1; // 2014/Dec/30, sp score
                                        break;
+#endif
+#if 0
                                case 's' :
                                        RNAscoremtx = 'r';
                                        break;
+#endif
 #if 1
                                case 'a':
                                        fmodel = 1;
@@ -195,9 +254,11 @@ void arguments( int argc, char *argv[] )
                                case 'P':
                                        dorp = 'p';
                                        break;
+#if 0
                                case 'Q':
                                        alg = 'Q';
                                        break;
+#endif
                                case 'R':
                                        rnaprediction = 'r';
                                        break;
@@ -231,16 +292,19 @@ void arguments( int argc, char *argv[] )
                                case 's' :
                                        treemethod = 's';
                                        break;
-#endif
                                case 'H':
                                        alg = 'H';
                                        break;
+#endif
                                case 'A':
                                        alg = 'A';
                                        break;
                                case 'M':
                                        alg = 'M';
                                        break;
+                               case '@':
+                                       alg = 'd';
+                                       break;
                                case 'F':
                                        use_fft = 1;
                                        break;
@@ -261,15 +325,20 @@ void arguments( int argc, char *argv[] )
                                case 'J':
                                        utree = 0;
                                        break;
+#if 0
                                case 'd':
                                        disp = 1;
                                        break;
+#endif
                                case 'Z':
                                        score_check = 0;
                                        break;
                                case 'Y':
                                        score_check = 2;
                                        break;
+                               case 'L':
+                                       legacygapcost = 1;
+                                       break;
 #if 0
                                case 'n' :
                                        treemethod = 'n';
@@ -278,23 +347,44 @@ void arguments( int argc, char *argv[] )
                                case 'n' :
                                        outnumber = 1;
                                        break;
-                               case 'X' :
+                               case 'X':
                                        treemethod = 'X';
-                                       break;
+                                       sueff_global = atof( *++argv );
+                                       fprintf( stderr, "sueff_global = %f\n", sueff_global );
+                                       --argc;
+                                       goto nextoption;
+#if 0
                                case 'E' :
                                        treemethod = 'E';
                                        break;
                                case 'q' :
                                        treemethod = 'q';
                                        break;
+#endif
+                               case 'E':
+                                       autosubalignment = atof( *++argv );
+                                       fprintf( stderr, "autosubalignment = %f\n", autosubalignment );
+                                       --argc;
+                                       goto nextoption;
+                               case 'W':
+                                       minimumweight = atof( *++argv );
+                                       fprintf( stderr, "minimumweight = %f\n", minimumweight );
+                                       --argc;
+                                       goto nextoption;
                                case 'z':
-                                       fftThreshold = atoi( *++argv );
+                                       fftThreshold = myatoi( *++argv );
                                        --argc;
                                        goto nextoption;
                                case 'w':
-                                       fftWinSize = atoi( *++argv );
+                                       fftWinSize = myatoi( *++argv );
                                        --argc;
                                        goto nextoption;
+                               case '=':
+                                       specifictarget = 1;
+                                       break;
+                               case ':':
+                                       nwildcard = 1;
+                                       break;
                                default:
                                        fprintf( stderr, "illegal option %c\n", c );
                                        argc = 0;
@@ -335,6 +425,7 @@ int main( int argc, char *argv[] )
        double **eff;
        FILE *prep;
        FILE *infp;
+       FILE *orderfp;
        int alloclen;
        int returnvalue;
        char c;
@@ -346,11 +437,22 @@ int main( int argc, char *argv[] )
        static char **nogap1seq;
        static char *kozoarivec;
        int nkozo;
+       int alignmentlength;
+       int **skipthisbranch;
+       int foundthebranch;
+       int nsubalignments, maxmem;
+       int **subtable;
+       int *insubtable;
+       int *preservegaps;
+       char ***subalnpt;
+       int ntarget, *targetmap, *targetmapr;
+       int ilim;
 
        arguments( argc, argv );
 #ifndef enablemultithread
        nthread = 0;
 #endif
+       if( fastathreshold < 0.0001 ) constraint = 0;
 
        if( inputfile )
        {
@@ -380,6 +482,29 @@ int main( int argc, char *argv[] )
                exit( 1 );
        }
 
+
+       if( subalignment )
+       {
+               readsubalignmentstable( njob, NULL, NULL, &nsubalignments, &maxmem );
+               fprintf( stderr, "nsubalignments = %d\n", nsubalignments );
+               fprintf( stderr, "maxmem = %d\n", maxmem );
+               subtable = AllocateIntMtx( nsubalignments, maxmem+1 );
+               insubtable = AllocateIntVec( njob );
+               preservegaps = AllocateIntVec( njob );
+               for( i=0; i<njob; i++ ) insubtable[i] = 0;
+               for( i=0; i<njob; i++ ) preservegaps[i] = 0;
+               subalnpt = AllocateCharCub( nsubalignments, maxmem, 0 );
+               readsubalignmentstable( njob, subtable, preservegaps, NULL, NULL );
+               for( i=0; i<nsubalignments; i++ ) for( j=0; j<insubtable[i]; j++ )
+               {
+                       if( subtable[i][j] < 0 )
+                       {
+                               fprintf( stderr, "Not supported in the iterative refinmenment mode.\n" );
+                               fprintf( stderr, "Please use a positive number to represent a sequence.\n" );
+                       }
+               }
+       }
+
        ocut = cut;
 
        segment = (Segment *)calloc( MAXSEG, sizeof( Segment ) );
@@ -398,14 +523,48 @@ int main( int argc, char *argv[] )
        seq_g_bk = AllocateCharMtx( njob, 0 );
        for( i=0; i<njob; i++ ) seq_g_bk[i] = seq_g[i];
        kozoarivec = AllocateCharVec( njob );
+       skipthisbranch = AllocateIntMtx( njob, 2 );
+       for( i=0; i<njob; i++ ) skipthisbranch[i][0] = skipthisbranch[i][1] = 0;
+
+
+#if 0
+       Read( name, nlen, seq_g );
+#else
+       readData_pointer( infp, name, nlen, seq_g );
+#endif
+       fclose( infp );
+
+       if( specifictarget )
+       {
+               targetmap = calloc( njob, sizeof( int ) );
+               ntarget = 0;
+               for( i=0; i<njob; i++ )
+               {
+                       targetmap[i] = -1;
+                       if( !strncmp( name[i]+1, "_focus_", 7 ) )
+                               targetmap[i] = ntarget++;
+               }
+               targetmapr = calloc( ntarget, sizeof( int ) );
+               for( i=0; i<njob; i++ )
+                       if( targetmap[i] != -1 ) targetmapr[targetmap[i]] = i;
+       }
+       else
+       {
+               ntarget = njob;
+               targetmap = calloc( njob, sizeof( int ) );
+               targetmapr = calloc( njob, sizeof( int ) );
+               for( i=0; i<njob; i++ )
+                       targetmap[i] = targetmapr[i] = i;
+       }
 
        if( constraint )
        {
-               localhomtable = (LocalHom **)calloc( njob, sizeof( LocalHom *) );
-               for( i=0; i<njob; i++)
+               ilim = njob;
+               localhomtable = (LocalHom **)calloc( ntarget, sizeof( LocalHom *) );
+               for( i=0; i<ntarget; i++)
                {
-                       localhomtable[i] = (LocalHom *)calloc( njob, sizeof( LocalHom ) );
-                       for( j=0; j<njob; j++)
+                       localhomtable[i] = (LocalHom *)calloc( ilim, sizeof( LocalHom ) );
+                       for( j=0; j<ilim; j++)
                        {
                                localhomtable[i][j].start1 = -1;
                                localhomtable[i][j].end1 = -1;
@@ -420,12 +579,14 @@ int main( int argc, char *argv[] )
                                localhomtable[i][j].last = localhomtable[i]+j;
                                localhomtable[i][j].korh = 'h';
                        }
+                       if( !specifictarget ) ilim--;
                }
                fprintf( stderr, "Loading 'hat3' ... " );
                fflush( stderr );
                prep = fopen( "hat3", "r" );
                if( prep == NULL ) ErrorExit( "Make hat3." );
-               readlocalhomtable2( prep, njob, localhomtable, kozoarivec );
+               if( specifictarget ) readlocalhomtable2_target( prep, njob, localhomtable, kozoarivec, targetmap );
+               else readlocalhomtable2_half( prep, njob, localhomtable, kozoarivec );
                fclose( prep ); 
 //             for( i=0; i<njob-1; i++ ) for( j=i+1; j<njob; j++ )
 //                     fprintf( stdout, "%d %d %d %d %d %d %d\n", i, j, localhomtable[i][j].opt, localhomtable[i][j].start1, localhomtable[i][j].end1, localhomtable[i][j].start2, localhomtable[i][j].end2 );
@@ -463,13 +624,8 @@ int main( int argc, char *argv[] )
                        }
                }
 
-#if 0
-       Read( name, nlen, seq_g );
-#else
-       readData_pointer( infp, name, nlen, seq_g );
-#endif
-       fclose( infp );
 
+       if( specificityconsideration ) calcmaxdistclass();
 
        for( i=0; i<njob; i++ )
        {
@@ -532,13 +688,14 @@ int main( int argc, char *argv[] )
                {
                        gappick0( nogap1seq[0], seq_g[i] );
                        nogaplen = strlen( nogap1seq[0] );
-                       singlerna[i] = (RNApair **)calloc( nogaplen, sizeof( RNApair * ) );
+                       singlerna[i] = (RNApair **)calloc( nogaplen+1, sizeof( RNApair * ) );
                        for( j=0; j<nogaplen; j++ )
                        {
                                singlerna[i][j] = (RNApair *)calloc( 1, sizeof( RNApair ) );
                                singlerna[i][j][0].bestpos = -1;
                                singlerna[i][j][0].bestscore = -1.0;
                        }
+                       singlerna[i][nogaplen] = NULL;
                        readmccaskill( prep, singlerna[i], nogaplen );
                }
                fclose( prep );
@@ -556,20 +713,41 @@ int main( int argc, char *argv[] )
                if( !prep ) ErrorExit( "Make hat2." );
                readhat2_pointer( prep, njob, name, eff );
                fclose( prep );
-#if DEBUG
+#if 0
+               fprintf( stderr, "eff = \n" );
                for( i=0; i<njob-1; i++ ) 
                {
                        for( j=i+1; j<njob; j++ ) 
                        {
-                               printf( " %f", eff[i][j] );
+                               fprintf( stderr, "%d-%d,  %f\n", i, j, eff[i][j] );
                        }
-                       printf( "\n" );
+                       fprintf( stderr, "\n" );
                }
 #endif
                if( intree )
-                       veryfastsupg_double_loadtree( njob, eff, topol, len );
+               {
+                       veryfastsupg_double_loadtree( njob, eff, topol, len, name );
+#if 0
+                       fprintf( stderr, "eff = \n" );
+                       for( i=0; i<njob-1; i++ ) 
+                       {
+                               for( j=i+1; j<njob; j++ ) 
+                               {
+                                       fprintf( stderr, "%d-%d,  %f\n", i, j, eff[i][j] );
+                               }
+                               fprintf( stderr, "\n" );
+                       }
+exit( 1 );
+#endif
+               }
                else if( intop ) // v6.528 deha if( intop ) dattanode intree ga mukou datta.
-                       veryfastsupg_double_loadtop( njob, eff, topol, len );
+               {
+                       fprintf( stderr, "--topin has been disabled\n" );
+                       exit( 1 );
+//                     veryfastsupg_double_loadtop( njob, eff, topol, len );
+               }
+               else if( subalignment )
+                       fixed_supg_double_treeout_constrained( njob, eff, topol, len, name, nsubalignments, subtable );
                else if( treemethod == 'X' || treemethod == 'E' || treemethod == 'q' ) 
 //                     veryfastsupg_double_outtree( njob, eff, topol, len, name );
                        fixed_musclesupg_double_treeout( njob, eff, topol, len, name );
@@ -585,6 +763,80 @@ int main( int argc, char *argv[] )
        printf( "utree = %d\n", utree );
 #endif
 
+       if( autosubalignment > 0.0 && subalignment == 0 )
+       {
+//             reporterr( "Computing skipthisbranch..\n" );
+               insubtable = AllocateIntVec( njob );
+               preservegaps = AllocateIntVec( njob );
+               subtable = calloc( 1, sizeof( char * ) );
+               subtable[0] = NULL; // for FreeIntMtx
+               for( i=0; i<njob; i++ ) insubtable[i] = 0;
+               for( i=0; i<njob; i++ ) preservegaps[i] = 0; // tsukawanaikamo
+               if( generatesubalignmentstable( njob, &subtable, &nsubalignments, &maxmem, topol, len, autosubalignment ) ) // subtable ha allocate sareru.
+               {
+                       reporterr( "################################################################################################ \n" );
+                       reporterr( "#\n" );
+                       reporterr( "# WARNING: Iterative refinment was not done because you gave a large --fix value (%6.3f).\n", autosubalignment );
+                       reporterr( "#\n" );
+                       reporterr( "################################################################################################ \n" );
+                       writePre( njob, name, nlen, seq_g, 1 );
+
+                       FreeCharMtx( seq_g_bk );
+                       FreeIntCub( topol );
+                       FreeDoubleMtx( len );
+                       FreeDoubleMtx( eff );
+                       FreeIntMtx( skipthisbranch );
+                       FreeIntMtx( subtable );
+                       free( preservegaps );
+                       free( insubtable );
+                       SHOWVERSION;
+                       return( 0 );
+               }
+//             subtable = AllocateIntMtx( nsubalignments, maxmem+1 );
+               fprintf( stderr, "nsubalignments = %d, maxmem = %d\n", nsubalignments, maxmem );
+               subalnpt = AllocateCharCub( nsubalignments, maxmem, 0 );
+#if 0
+               for( i=0; i<nsubalignments; i++ )
+               {
+                       reporterr( "subalignment %d\n", i );
+                       for( j=0; subtable[i][j]!=-1; j++ )
+                       {
+                               reporterr( "%5d", subtable[i][j] );
+                       }
+                       reporterr( "\n" );
+               }
+#endif
+#if 0 // wakaran
+               for( i=0; i<nsubalignments; i++ ) for( j=0; j<insubtable[i]; j++ )
+               {
+                       if( subtable[i][j] < 0 )
+                       {
+                               fprintf( stderr, "Not supported in the iterative refinmenment mode.\n" );
+                               fprintf( stderr, "Please use a positive number to represent a sequence.\n" );
+                       }
+               }
+#endif
+//             reporterr( "done.\n" );
+       }
+
+
+       orderfp = fopen( "order", "w" );
+       if( !orderfp )
+       {
+               fprintf( stderr, "Cannot open 'order'\n" );
+               exit( 1 );
+       }
+       for( i=0; (j=topol[njob-2][0][i])!=-1; i++ )
+       {
+               fprintf( orderfp, "%d\n", j );
+       }
+       for( i=0; (j=topol[njob-2][1][i])!=-1; i++ )
+       {
+               fprintf( orderfp, "%d\n", j );
+       }
+       fclose( orderfp );
+
+
 
        fprintf( stderr, "\n" );
        if( ( !utree && kobetsubunkatsu ) || constraint || !bunkatsu )
@@ -616,6 +868,148 @@ int main( int argc, char *argv[] )
 #endif
        }
 
+
+//--------------- kokokara ----
+       if( subalignment || autosubalignment )
+       {
+               for( i=0; i<nsubalignments; i++ )
+               {
+                       fprintf( stderr, "\nChecking subalignment %d:\n", i+1 );
+                       alignmentlength = strlen( seq[subtable[i][0]] );
+                       for( j=0; subtable[i][j]!=-1; j++ )
+                               fprintf( stderr, " %d ", subtable[i][j]+1 );
+//                             fprintf( stderr, " ##### %d-%d. %-30.30s\n", i, subtable[i][j]+1, name[subtable[i][j]]+1 );
+                       fprintf( stderr, "\n" );
+                       for( j=0; subtable[i][j]!=-1; j++ )
+                       {
+                               if( subtable[i][j] >= njob )
+                               {
+                                       fprintf( stderr, "No such sequence, %d.\n", subtable[i][j]+1 );
+                                       exit( 1 );
+                               }
+                               if( alignmentlength != strlen( seq[subtable[i][j]] ) )
+                               {
+                                       fprintf( stderr, "\n" );
+                                       fprintf( stderr, "###############################################################################\n" );
+                                       fprintf( stderr, "# ERROR!\n" );
+                                       fprintf( stderr, "# Subalignment %d must be aligned.\n", i+1 );
+                                       fprintf( stderr, "# Please check the alignment lengths of following sequences.\n" );
+                                       fprintf( stderr, "#\n" );
+                                       fprintf( stderr, "# %d. %-10.10s -> %d letters (including gaps)\n", subtable[i][0]+1, name[subtable[i][0]]+1, alignmentlength );
+                                       fprintf( stderr, "# %d. %-10.10s -> %d letters (including gaps)\n", subtable[i][j]+1, name[subtable[i][j]]+1, (int)strlen( seq[subtable[i][j]] ) );
+                                       fprintf( stderr, "#\n" );
+                                       fprintf( stderr, "# See http://mafft.cbrc.jp/alignment/software/merge.html for details.\n" );
+                                       if( subalignmentoffset )
+                                       {
+                                               fprintf( stderr, "#\n" );
+                                               fprintf( stderr, "# You specified seed alignment(s) consisting of %d sequences.\n", subalignmentoffset );
+                                               fprintf( stderr, "# In this case, the rule of numbering is:\n" );
+                                               fprintf( stderr, "#   The aligned seed sequences are numbered as 1 .. %d\n", subalignmentoffset );
+                                               fprintf( stderr, "#   The input sequences to be aligned are numbered as %d .. %d\n", subalignmentoffset+1, subalignmentoffset+njob );
+                                       }
+                                       fprintf( stderr, "###############################################################################\n" );
+                                       fprintf( stderr, "\n" );
+                                       exit( 1 );
+                               }
+                               insubtable[subtable[i][j]] = 1;
+                       }
+                       for( j=0; j<njob-1; j++ )
+                       {
+#if 0
+                               int k;
+                               reporterr( "####  STEP%d\n", j );
+                               for( k=0; topol[j][0][k]!=-1; k++ ) reporterr( "%3d ", topol[j][0][k] );
+                               reporterr( "len=%f\n", len[j][0] );
+                               for( k=0; topol[j][1][k]!=-1; k++ ) reporterr( "%3d ", topol[j][1][k] );
+                               reporterr( "len=%f\n", len[j][1] );
+                               reporterr( "\n" );
+#endif
+                               if( includemember( topol[j][0], subtable[i] ) && !samemember( topol[j][0], subtable[i] ) )
+                               {
+                                       skipthisbranch[j][0] = 1;
+//                                     reporterr( "SKIP 0 !!!!!!\n" );
+                               }
+                               if( includemember( topol[j][1], subtable[i] ) && !samemember( topol[j][1], subtable[i] ) )
+                               {
+                                       skipthisbranch[j][1] = 1;
+//                                     reporterr( "SKIP 1 !!!!!!\n" );
+                               }
+                       }
+                       foundthebranch = 0;
+                       for( j=0; j<njob-1; j++ )
+                       {
+                               if( samemember( topol[j][0], subtable[i] ) || samemember( topol[j][1], subtable[i] ) )
+                               {
+                                       foundthebranch = 1;
+                                       fprintf( stderr, " -> OK\n" );
+                                       break;
+                               }
+                       }
+                       if( !foundthebranch )
+                       {
+                               system( "cp infile.tree GuideTree" ); // tekitou
+                               fprintf( stderr, "\n" );
+                               fprintf( stderr, "###############################################################################\n" );
+                               fprintf( stderr, "# ERROR!\n" );
+                               fprintf( stderr, "# Subalignment %d does not seem to form a monophyletic cluster\n", i+1 );
+                               fprintf( stderr, "# in the guide tree ('GuideTree' in this directory) internally computed.\n" );
+                               fprintf( stderr, "# If you really want to use this subalignment, pelase give a tree with --treein \n" );
+                               fprintf( stderr, "# http://mafft.cbrc.jp/alignment/software/treein.html\n" );
+                               fprintf( stderr, "# http://mafft.cbrc.jp/alignment/software/merge.html\n" );
+                               if( subalignmentoffset )
+                               {
+                                       fprintf( stderr, "#\n" );
+                                       fprintf( stderr, "# You specified seed alignment(s) consisting of %d sequences.\n", subalignmentoffset );
+                                       fprintf( stderr, "# In this case, the rule of numbering is:\n" );
+                                       fprintf( stderr, "#   The aligned seed sequences are numbered as 1 .. %d\n", subalignmentoffset );
+                                       fprintf( stderr, "#   The input sequences to be aligned are numbered as %d .. %d\n", subalignmentoffset+1, subalignmentoffset+njob );
+                               }
+                               fprintf( stderr, "############################################################################### \n" );
+                               fprintf( stderr, "\n" );
+                               exit( 1 );
+                       }
+//                     commongappick( seq[subtable[i]], subalignment[i] ); // irukamo
+               }
+#if 0
+               for( i=0; i<njob-1; i++ )
+               {
+                       fprintf( stderr, "STEP %d\n", i+1 );
+                       fprintf( stderr, "group1 = " );
+                       for( j=0; topol[i][0][j] != -1; j++ )
+                               fprintf( stderr, "%d ", topol[i][0][j]+1 );
+                       fprintf( stderr, "\n" );
+                       fprintf( stderr, "SKIP -> %d\n\n", skipthisbranch[i][0] );
+                       fprintf( stderr, "group2 = " );
+                       for( j=0; topol[i][1][j] != -1; j++ )
+                               fprintf( stderr, "%d ", topol[i][1][j]+1 );
+                       fprintf( stderr, "\n" );
+                       fprintf( stderr, "SKIP -> %d\n\n", skipthisbranch[i][1] );
+               }
+#endif
+
+               for( i=0; i<njob; i++ ) 
+               {
+                       if( insubtable[i] ) strcpy( bseq[i], seq[i] );
+                       else gappick0( bseq[i], seq[i] );
+               }
+
+               for( i=0; i<nsubalignments; i++ ) 
+               {
+                       for( j=0; subtable[i][j]!=-1; j++ ) subalnpt[i][j] = bseq[subtable[i][j]];
+                       commongappick( j, subalnpt[i] );
+               }
+
+               FreeIntMtx( subtable );
+               free( insubtable );
+               for( i=0; i<nsubalignments; i++ ) free( subalnpt[i] );
+               free( subalnpt );
+               free( preservegaps );
+       }
+//--------------- kokomade ----
+
+
+
+
        for( i=0; i<njob; i++ ) res_g[i][0] = 0;
 
        for( iseg=0; iseg<nseg-1; iseg++ )
@@ -634,7 +1028,7 @@ int main( int argc, char *argv[] )
                fprintf( trap_g, "Segment %3d/%3d %4d-%4d\n", iseg+1, nseg-1, pos+1, pos+1+tmplen );
        
                cut = ocut;
-               returnvalue = TreeDependentIteration( njob, name, nlen, seq, bseq, topol, len, alloclen, localhomtable, singlerna, nkozo, kozoarivec );
+               returnvalue = TreeDependentIteration( njob, name, nlen, seq, bseq, topol, len, eff, skipthisbranch, alloclen, localhomtable, singlerna, nkozo, kozoarivec, ntarget, targetmap, targetmapr );
 
                for( i=0; i<njob; i++ )
                        strcat( res_g[i], bseq[i] );
@@ -643,7 +1037,31 @@ int main( int argc, char *argv[] )
        FreeIntCub( topol );
        FreeDoubleMtx( len );
        FreeDoubleMtx( eff );
-       if( constraint ) FreeLocalHomTable( localhomtable, njob );
+       FreeIntMtx( skipthisbranch );
+       free( kozoarivec );
+       if( constraint ) 
+       {
+               if( specifictarget ) FreeLocalHomTable_part( localhomtable, ntarget, njob );
+               else FreeLocalHomTable_half( localhomtable, njob );
+       }
+       free( targetmap );
+       free( targetmapr );
+       if( rnakozo && rnaprediction == 'm' ) 
+       {
+               if( singlerna ) // nen no tame
+               {
+                       for( i=0; i<njob; i++ ) 
+                       {
+                               for( j=0; singlerna[i][j]!=NULL; j++ )
+                               {
+                                       if( singlerna[i][j] ) free( singlerna[i][j] );
+                               }
+                               if( singlerna[i] ) free( singlerna[i] );
+                       }
+                       free( singlerna );
+                       singlerna = NULL;
+               }
+       }
 
 #if 0
        Write( stdout, njob, name, nlen, bseq );
@@ -652,6 +1070,7 @@ int main( int argc, char *argv[] )
        fprintf( stderr, "done\n" );
        fprintf( trap_g, "done\n" );
        fclose( trap_g );
+       freeconstants();
 
 
        devide = 0; 
@@ -661,6 +1080,8 @@ int main( int argc, char *argv[] )
 #endif
 
 
+       if( spscoreout ) reporterr( "Unweighted sum-of-pairs score = %10.5f\n", sumofpairsscore( njob, res_g ) );
+
        SHOWVERSION;
        return( 0 );
 }