JWS-112 Bumping version of Mafft to version 7.310.
[jabaws.git] / binaries / src / mafft / core / io.c
index 75520d2..81590e6 100644 (file)
@@ -7,10 +7,36 @@ static int upperCase = 0;
 
 char creverse( char f )
 {
-       static char *table = NULL;
+       static TLS char *table = NULL;
+
+       if( f == 0 )
+       {
+               free( table );
+               table = NULL;
+               return( 0 );
+       }
+
        if( table == NULL )
        {
+               int i;
                table = AllocateCharVec(0x80);
+               for( i=0; i<0x80; i++ ) table[i] = i;
+               table['A'] = 'T';
+               table['C'] = 'G';
+               table['G'] = 'C';
+               table['T'] = 'A';
+               table['U'] = 'A';
+               table['M'] = 'K';
+               table['R'] = 'Y';
+               table['W'] = 'W';
+               table['S'] = 'S';
+               table['Y'] = 'R';
+               table['K'] = 'M';
+               table['V'] = 'B';
+               table['H'] = 'D';
+               table['D'] = 'H';
+               table['B'] = 'V';
+               table['N'] = 'N';
                table['a'] = 't';
                table['c'] = 'g';
                table['g'] = 'c';
@@ -27,9 +53,9 @@ char creverse( char f )
                table['d'] = 'h';
                table['b'] = 'v';
                table['n'] = 'n';
-               table['-'] = '-';
-               table['.'] = '.';
-               table['*'] = '*';
+//             table['-'] = '-';
+//             table['.'] = '.';
+//             table['*'] = '*';
        }
        return( table[(int)f] );
 }
@@ -191,7 +217,7 @@ static int addlocalhom2( char *al1, char *al2, LocalHom *localhompt, int off1, i
 
 
 
-static int addlocalhom_r( char *al1, char *al2, LocalHom *localhompt, int off1, int off2, int opt, int overlapaa, int skip )
+static int addlocalhom_r( char *al1, char *al2, LocalHom *localhompt, int off1, int off2, int opt, int overlapaa, int skip, char korh )
 {
        int pos1, pos2, start1, start2, end1, end2;
        char *pt1, *pt2;
@@ -249,6 +275,7 @@ static int addlocalhom_r( char *al1, char *al2, LocalHom *localhompt, int off1,
                        tmppt->start2 = start2;
                        tmppt->end1   = end1  ;
                        tmppt->end2   = end2  ;
+                       tmppt->korh   = korh  ;
 
 #if 1
                        sumscore += score;
@@ -273,7 +300,7 @@ static int addlocalhom_r( char *al1, char *al2, LocalHom *localhompt, int off1,
                                start1 = pos1; start2 = pos2;
                                st = 1;
                        }
-                       score += (double)n_dis[(int)amino_n[(int)*pt1]][(int)amino_n[(int)*pt2]];
+                       score += (double)n_dis[(int)amino_n[(unsigned char)*pt1]][(int)amino_n[(unsigned char)*pt2]];
 //                     fprintf( stderr, "%c-%c, score(0) = %f\n", *pt1, *pt2, score );
                }
                if( *pt1++ != '-' ) pos1++;
@@ -293,6 +320,7 @@ static int addlocalhom_r( char *al1, char *al2, LocalHom *localhompt, int off1,
        tmppt->start2 = start2;
        tmppt->end1   = end1  ;
        tmppt->end2   = end2  ;
+       tmppt->korh   = korh  ;
 
 #if 1
        sumscore += score;
@@ -315,7 +343,7 @@ static int addlocalhom_r( char *al1, char *al2, LocalHom *localhompt, int off1,
        }
        return( nlocalhom );
 }
-void putlocalhom3( char *al1, char *al2, LocalHom *localhompt, int off1, int off2, int opt, int overlapaa )
+void putlocalhom3( char *al1, char *al2, LocalHom *localhompt, int off1, int off2, int opt, int overlapaa, char korh )
 {
        int pos1, pos2, start1, start2, end1, end2;
        char *pt1, *pt2;
@@ -367,6 +395,7 @@ void putlocalhom3( char *al1, char *al2, LocalHom *localhompt, int off1, int off
                        tmppt->start2 = start2;
                        tmppt->end1   = end1  ;
                        tmppt->end2   = end2  ;
+                       tmppt->korh   = korh  ;
 
 #if 1
                        if( divpairscore )
@@ -399,7 +428,7 @@ void putlocalhom3( char *al1, char *al2, LocalHom *localhompt, int off1, int off
                                start1 = pos1; start2 = pos2;
                                st = 1;
                        }
-                       score += (double)n_dis[(int)amino_n[(int)*pt1]][(int)amino_n[(int)*pt2]]; // - offset ¤Ï¤¤¤é¤Ê¤¤¤«¤â
+                       score += (double)n_dis[(int)amino_n[(unsigned char)*pt1]][(int)amino_n[(unsigned char)*pt2]]; // - offset ¤Ï¤¤¤é¤Ê¤¤¤«¤â
 //                     fprintf( stderr, "%c-%c, score(0) = %f\n", *pt1, *pt2, score );
                }
                if( *pt1++ != '-' ) pos1++;
@@ -422,6 +451,7 @@ void putlocalhom3( char *al1, char *al2, LocalHom *localhompt, int off1, int off
                tmppt->start2 = start2;
                tmppt->end1   = end1  ;
                tmppt->end2   = end2  ;
+               tmppt->korh   = korh  ;
 
 
 #if 1
@@ -460,7 +490,7 @@ void putlocalhom3( char *al1, char *al2, LocalHom *localhompt, int off1, int off
                }
        }
 }
-void putlocalhom_ext( char *al1, char *al2, LocalHom *localhompt, int off1, int off2, int opt, int overlapaa )
+void putlocalhom_ext( char *al1, char *al2, LocalHom *localhompt, int off1, int off2, int opt, int overlapaa, char korh )
 {
        int pos1, pos2, start1, start2, end1, end2;
        char *pt1, *pt2;
@@ -501,6 +531,7 @@ void putlocalhom_ext( char *al1, char *al2, LocalHom *localhompt, int off1, int
                        tmppt->start2 = start2;
                        tmppt->end1   = end1  ;
                        tmppt->end2   = end2  ;
+                       tmppt->korh   = korh  ;
 
 #if 1
                        if( divpairscore )
@@ -533,7 +564,7 @@ void putlocalhom_ext( char *al1, char *al2, LocalHom *localhompt, int off1, int
                                start1 = pos1; start2 = pos2;
                                st = 1;
                        }
-                       iscore += n_dis[(int)amino_n[(int)*pt1]][(int)amino_n[(int)*pt2]]; // - offset ¤Ï¤¤¤é¤Ê¤¤¤«¤â
+                       iscore += n_dis[(int)amino_n[(unsigned char)*pt1]][(int)amino_n[(unsigned char)*pt2]]; // - offset ¤Ï¤¤¤é¤Ê¤¤¤«¤â
 //                     fprintf( stderr, "%c-%c, iscore(0) = %d\n", *pt1, *pt2, iscore );
                }
                if( *pt1++ != '-' ) pos1++;
@@ -555,6 +586,7 @@ void putlocalhom_ext( char *al1, char *al2, LocalHom *localhompt, int off1, int
                tmppt->start2 = start2;
                tmppt->end1   = end1  ;
                tmppt->end2   = end2  ;
+               tmppt->korh   = korh  ;
        
 #if 1
                if( divpairscore )
@@ -591,7 +623,7 @@ void putlocalhom_ext( char *al1, char *al2, LocalHom *localhompt, int off1, int
        }
 }
 
-void putlocalhom_str( char *al1, char *al2, double *equiv, double scale, LocalHom *localhompt, int off1, int off2, int opt, int overlapaa )
+void putlocalhom_str( char *al1, char *al2, double *equiv, double scale, LocalHom *localhompt, int off1, int off2, int opt, int overlapaa, char korh )
 {
        int posinaln, pos1, pos2, start1, start2, end1, end2;
        char *pt1, *pt2;
@@ -626,6 +658,7 @@ void putlocalhom_str( char *al1, char *al2, double *equiv, double scale, LocalHo
                        tmppt->start2 = start2;
                        tmppt->end1   = end1  ;
                        tmppt->end2   = end2  ;
+                       tmppt->korh   = korh  ;
 
                        tmppt->overlapaa   = 1;
 //                     tmppt->opt = (double)iscore / tmppt->overlapaa * 5.8 / 600;
@@ -639,7 +672,9 @@ void putlocalhom_str( char *al1, char *al2, double *equiv, double scale, LocalHo
        }
 }
 
-void putlocalhom2( char *al1, char *al2, LocalHom *localhompt, int off1, int off2, int opt, int overlapaa )
+
+
+void putlocalhom2( char *al1, char *al2, LocalHom *localhompt, int off1, int off2, int opt, int overlapaa, char korh )
 {
        int pos1, pos2, start1, start2, end1, end2;
        char *pt1, *pt2;
@@ -680,6 +715,9 @@ void putlocalhom2( char *al1, char *al2, LocalHom *localhompt, int off1, int off
                        tmppt->start2 = start2;
                        tmppt->end1   = end1  ;
                        tmppt->end2   = end2  ;
+                       tmppt->korh   = korh  ;
+                       tmppt->nokori  += 1;
+                       localhompt->last  = tmppt;
 
 #if 1
                        if( divpairscore )
@@ -712,7 +750,7 @@ void putlocalhom2( char *al1, char *al2, LocalHom *localhompt, int off1, int off
                                start1 = pos1; start2 = pos2;
                                st = 1;
                        }
-                       iscore += n_dis[(int)amino_n[(int)*pt1]][(int)amino_n[(int)*pt2]]; // - offset ¤Ï¤¤¤é¤Ê¤¤¤«¤â
+                       iscore += n_dis[(int)amino_n[(unsigned char)*pt1]][(int)amino_n[(unsigned char)*pt2]]; // - offset ¤Ï¤¤¤é¤Ê¤¤¤«¤â
 //                     fprintf( stderr, "%c-%c, iscore(0) = %d\n", *pt1, *pt2, iscore );
                }
                if( *pt1++ != '-' ) pos1++;
@@ -734,6 +772,9 @@ void putlocalhom2( char *al1, char *al2, LocalHom *localhompt, int off1, int off
                tmppt->start2 = start2;
                tmppt->end1   = end1  ;
                tmppt->end2   = end2  ;
+               tmppt->korh   = korh  ;
+               tmppt->nokori  += 1;
+               localhompt->last  = tmppt;
        
 #if 1
                if( divpairscore )
@@ -768,7 +809,9 @@ void putlocalhom2( char *al1, char *al2, LocalHom *localhompt, int off1, int off
                }
        }
 }
-void putlocalhom( char *al1, char *al2, LocalHom *localhompt, int off1, int off2, int opt, int overlapaa )
+
+#if 0
+void putlocalhom( char *al1, char *al2, LocalHom *localhompt, int off1, int off2, int opt, int overlapaa, char korh )
 {
        int pos1, pos2, start1, start2, end1, end2;
        char *pt1, *pt2;
@@ -809,6 +852,7 @@ void putlocalhom( char *al1, char *al2, LocalHom *localhompt, int off1, int off2
                        tmppt->start2 = start2;
                        tmppt->end1   = end1  ;
                        tmppt->end2   = end2  ;
+                       tmppt->korh   = korh  ;
 
 #if 1
                        if( divpairscore )
@@ -841,7 +885,7 @@ void putlocalhom( char *al1, char *al2, LocalHom *localhompt, int off1, int off2
                                start1 = pos1; start2 = pos2;
                                st = 1;
                        }
-                       score += (double)n_dis[(int)amino_n[(int)*pt1]][(int)amino_n[(int)*pt2]]; // - offset ¤Ï¤¤¤é¤Ê¤¤¤«¤â
+                       score += (double)n_dis[(int)amino_n[(unsigned char)*pt1]][(int)amino_n[(unsigned char)*pt2]]; // - offset ¤Ï¤¤¤é¤Ê¤¤¤«¤â
 //                     fprintf( stderr, "%c-%c, score(0) = %f\n", *pt1, *pt2, score );
                }
                if( *pt1++ != '-' ) pos1++;
@@ -861,6 +905,7 @@ void putlocalhom( char *al1, char *al2, LocalHom *localhompt, int off1, int off2
        tmppt->start2 = start2;
        tmppt->end1   = end1  ;
        tmppt->end2   = end2  ;
+       tmppt->korh   = korh  ;
 
 #if 1
        if( divpairscore )
@@ -894,6 +939,7 @@ void putlocalhom( char *al1, char *al2, LocalHom *localhompt, int off1, int off2
                }
        }
 }
+#endif
 
 char *cutal( char *al, int al_display_start, int start, int end )
 {
@@ -929,7 +975,7 @@ void strncpy_caseC( char *str1, char *str2, int len )
        else strncpy( str1, str2, len );
 }
        
-void seqUpper( int nseq, char **seq ) /* not used */
+void seqUpper( int nseq, char **seq )
 {
        int i, j, len;
        for( i=0; i<nseq; i++ ) 
@@ -993,7 +1039,7 @@ char    s[] ; int l ; FILE *fp ;
                return( 0 );
 }
 
-float input_new( FILE *fp, int d )
+double input_new( FILE *fp, int d )
 {
        char mojiretsu[10];
        int i, c;
@@ -1190,6 +1236,7 @@ void searchKUorWA( FILE *fp )
        ungetc( c, fp );
 }
 
+#if 0
 static int onlyGraph( char *str )
 {
        char tmp;
@@ -1200,14 +1247,14 @@ static int onlyGraph( char *str )
        while( (tmp=*str++) ) 
        {
                if( 0x20 < tmp && tmp < 0x7f ) *res++ = tmp;
-               if( tmp == '>' )
+               if( tmp == '>' || tmp == '(' )
                {
                        fprintf( stderr, "========================================================\n" );
                        fprintf( stderr, "========================================================\n" );
                        fprintf( stderr, "=== \n" );
                        fprintf( stderr, "=== ERROR!! \n" );
-                       fprintf( stderr, "=== In the '--anysymbol' and '--preservecase' modes, \n" );
-                       fprintf( stderr, "=== '>' in sequence is unacceptable.\n" );
+//                     fprintf( stderr, "=== In the '--anysymbol' and '--preservecase' modes, \n" );
+                       fprintf( stderr, "=== '>' and '(' are acceptable only in title lines.\n" );
                        fprintf( stderr, "=== \n" );
                        fprintf( stderr, "========================================================\n" );
                        fprintf( stderr, "========================================================\n" );
@@ -1217,6 +1264,37 @@ static int onlyGraph( char *str )
        *res = 0;
        return( res - bk );
 }
+#endif
+
+static int charfilter( unsigned char *str )
+{
+       unsigned char tmp;
+       unsigned char *res = str;
+       unsigned char *bk = str;
+
+       while( (tmp=*str++) )
+       {
+//             if( tmp == '=' || tmp == '*' || tmp == '<' || tmp == '>' || tmp == '(' || tmp == ')' )
+               if( tmp == '=' || tmp == '<' || tmp == '>' )
+               {
+                       fprintf( stderr, "\n" );
+                       fprintf( stderr, "Characters '= < >' are not accepted in the --text mode, \nalthough most printable characters are ok.\n" );
+                       fprintf( stderr, "\n" );
+                       exit( 1 );
+               }
+//             if( 0x20 < tmp && tmp < 0x7f )
+//             if( 0x0 <=tmp && tmp < 0x100 && 
+               if( tmp != 0x0a && tmp != 0x20 && tmp != 0x0d )
+//             if( tmp != '\n' && tmp != ' ' && tmp != '\t' ) // unprintable characters mo ok.
+               {
+                       *res++ = tmp;
+//                     reporterr( "tmp=%d (%c)\n", tmp, tmp );
+               }
+       }
+       *res = 0;
+       return( res - bk );
+}
+
 
 static int onlyAlpha_lower( char *str )
 {
@@ -1262,7 +1340,7 @@ char *load1SeqWithoutName_realloc_casepreserve( FILE *fpp )
 
        b = '\n';
        while( ( c = getc( fpp ) ) != EOF &&           
-          !( ( c == '>' || c == '(' || c == EOF ) && b == '\n' ) )
+          !( ( c == '>' || c == EOF ) && b == '\n' ) )
        {
                *cbuf++ = (char)c;  /* Ä¹¤¹¤®¤Æ¤â¤·¤é¤Ê¤¤ */
                if( cbuf - val == size )
@@ -1282,7 +1360,8 @@ char *load1SeqWithoutName_realloc_casepreserve( FILE *fpp )
        }
        ungetc( c, fpp );
        *cbuf = 0;
-       onlyGraph( val );
+//     onlyGraph( val );
+       charfilter( (unsigned char *) val );
 //     kake2hiku( val );
        return( val );
 }
@@ -1299,7 +1378,7 @@ char *load1SeqWithoutName_realloc( FILE *fpp )
 
        b = '\n';
        while( ( c = getc( fpp ) ) != EOF &&           
-          !( ( c == '>' || c == '(' || c == EOF ) && b == '\n' ) )
+          !( ( c == '>' || c == EOF ) && b == '\n' ) )
        {
                *cbuf++ = (char)c;  /* Ä¹¤¹¤®¤Æ¤â¤·¤é¤Ê¤¤ */
                if( cbuf - val == size )
@@ -1319,11 +1398,19 @@ char *load1SeqWithoutName_realloc( FILE *fpp )
        }
        ungetc( c, fpp );
        *cbuf = 0;
-       if( dorp == 'd' )
-               onlyAlpha_lower( val );
+
+       if( nblosum == -2 )
+       {
+               charfilter( (unsigned char *) val );
+       }
        else
-               onlyAlpha_upper( val );
-       kake2hiku( val );
+       {
+               if( dorp == 'd' )
+                       onlyAlpha_lower( val );
+               else
+                       onlyAlpha_upper( val );
+               kake2hiku( val );
+       }
        return( val );
 }
 
@@ -1334,7 +1421,7 @@ int load1SeqWithoutName_new( FILE *fpp, char *cbuf )
 
        b = '\n';
        while( ( c = getc( fpp ) ) != EOF &&                    /* by T. Nishiyama */
-          !( ( c == '>' || c == '(' || c == EOF ) && b == '\n' ) )
+          !( ( c == '>' || c == EOF ) && b == '\n' ) )
        {
                *cbuf++ = (char)c;  /* Ä¹¤¹¤®¤Æ¤â¤·¤é¤Ê¤¤ */
                b = c;
@@ -1526,6 +1613,53 @@ void readData_pointer_casepreserve( FILE *fp, char **name, int *nlen, char **seq
 }
 
 
+int copydatafromgui( char **namegui, char **seqgui, char **name, int *nlen, char **seq )
+{
+       int i; 
+
+
+       for( i=0; i<njob; i++ )
+       {
+               name[i][0] = '='; 
+               strncpy( name[i]+1, namegui[i], B-2 );
+               name[i][B-1] = 0;
+
+               strcpy( seq[i], seqgui[i] );
+               nlen[i] = strlen( seq[i] );
+       }
+       if( dorp == 'd' ) 
+               seqLower( njob, seq );
+       else if( dorp == 'p' ) 
+               seqUpper( njob, seq );
+       else
+       {
+               reporterr( "DNA or Protein?\n" );
+               return( 1 );
+       }
+#if 0
+       free( tmpseq );
+#endif
+       if( outnumber )
+       {
+               char *namebuf;
+               char *cptr;
+               namebuf = calloc( B+100, sizeof( char ) );
+               for( i=0; i<njob; i++ )
+               {
+                       namebuf[0] = '=';
+                       cptr = strstr( name[i], "_numo_e_" );
+                       if( cptr )
+                               sprintf( namebuf+1, "_numo_s_%08d_numo_e_%s", i+1, cptr+8 );
+                       else
+                               sprintf( namebuf+1, "_numo_s_%08d_numo_e_%s", i+1, name[i]+1 );
+                       strncpy( name[i], namebuf, B );
+                       name[i][B-1] = 0;
+               }
+               free( namebuf );
+       }
+       return( 0 );
+}
+
 void readData_pointer( FILE *fp, char **name, int *nlen, char **seq )
 {
        int i; 
@@ -1747,43 +1881,53 @@ void cutData( FILE *fp, int **regtable, char **revtable, int *outtable )
                        seqlen = strlen( tmpseq );
 
                        if( dorp == 'd' && upperCase != -1 ) seqLower( 1, &tmpseq );
-                       for( j=0; j<5; j++ )
+                       if( outtable[i] == 2 )
                        {
-                               if( regtable[i][j*2] == -1 && regtable[i][j*2+1] == -1 ) continue;
-
-                               startpos = regtable[i][j*2];
-                               endpos   = regtable[i][j*2+1];
-
-                               if( startpos > endpos )
-                               {
-                                       endpos   = regtable[i][j*2];
-                                       startpos = regtable[i][j*2+1];
-                               }
-
-                               if( startpos < 0 ) startpos = 0;
-                               if( endpos   < 0 ) endpos   = 0;
-                               if( endpos   >= seqlen ) endpos   = seqlen-1;
-                               if( startpos >= seqlen ) startpos = seqlen-1;
-
+                               startpos = 0;
+                               endpos   = seqlen-1;
                                outlen = endpos - startpos + 1;
-                               if( revtable[i][j] == 'f' )
-                               {
-                                       fprintf( stderr, "startpos = %d\n", startpos );
-                                       fprintf( stderr, "endpos   = %d\n", endpos );
-                                       fprintf( stderr, "outlen = %d\n", outlen );
-                                       fprintf( stdout, "%.*s\n", outlen, tmpseq+startpos );
-                               }
-                               else
+                               fprintf( stdout, "%.*s\n", outlen, tmpseq+startpos );
+                       }
+                       else
+                       {
+                               for( j=0; j<5; j++ )
                                {
-                                       fs = AllocateCharVec( outlen+1 );
-                                       rs = AllocateCharVec( outlen+1 );
-
-                                       fs[outlen] = 0;
-                                       strncpy( fs, tmpseq+startpos, outlen );
-                                       sreverse( rs, fs );
-                                       fprintf( stdout, "%s\n", rs );
-                                       free( fs );
-                                       free( rs );
+                                       if( regtable[i][j*2] == -1 && regtable[i][j*2+1] == -1 ) continue;
+       
+                                       startpos = regtable[i][j*2];
+                                       endpos   = regtable[i][j*2+1];
+       
+                                       if( startpos > endpos )
+                                       {
+                                               endpos   = regtable[i][j*2];
+                                               startpos = regtable[i][j*2+1];
+                                       }
+       
+                                       if( startpos < 0 ) startpos = 0;
+                                       if( endpos   < 0 ) endpos   = 0;
+                                       if( endpos   >= seqlen ) endpos   = seqlen-1;
+                                       if( startpos >= seqlen ) startpos = seqlen-1;
+       
+                                       outlen = endpos - startpos + 1;
+                                       if( revtable[i][j] == 'f' )
+                                       {
+                                               fprintf( stderr, "startpos = %d\n", startpos );
+                                               fprintf( stderr, "endpos   = %d\n", endpos );
+                                               fprintf( stderr, "outlen = %d\n", outlen );
+                                               fprintf( stdout, "%.*s\n", outlen, tmpseq+startpos );
+                                       }
+                                       else
+                                       {
+                                               fs = AllocateCharVec( outlen+1 );
+                                               rs = AllocateCharVec( outlen+1 );
+       
+                                               fs[outlen] = 0;
+                                               strncpy( fs, tmpseq+startpos, outlen );
+                                               sreverse( rs, fs );
+                                               fprintf( stdout, "%s\n", rs );
+                                               free( fs );
+                                               free( rs );
+                                       }
                                }
                        }
                }
@@ -1826,6 +1970,41 @@ void catData( FILE *fp )
        }
 }
 
+int countATGCandN( char *s, int *countN, int *total )
+{
+       int nATGC;
+       int nChar;
+       int nN;
+       char c;
+       nN = nATGC = nChar = 0;
+
+       if( *s == 0 ) 
+       {
+               *total = 0;
+               return( 0 );
+       }
+
+       do
+       {
+               c = tolower( *s );
+               if( isalpha( c ) )
+               {
+                       nChar++;
+                       if( c == 'a' || c == 't' || c == 'g' || c == 'c' || c == 'u' || c == 'n' )
+                               nATGC++;
+                       if( c == 'n' )
+                               nN++;
+               }
+       }
+       while( *++s );
+
+//     reporterr( "nN = %d", nN );
+
+       *total = nChar;
+       *countN = nN;
+       return( nATGC );
+}
+
 int countATGC( char *s, int *total )
 {
        int nATGC;
@@ -1835,7 +2014,7 @@ int countATGC( char *s, int *total )
 
        if( *s == 0 ) 
        {
-               total = 0;
+               *total = 0;
                return( 0 );
        }
 
@@ -1885,6 +2064,14 @@ int countnogaplen( char *seq )
        return( val );
 }
 
+int countnormalletters( char *seq, char *ref )
+{
+       int val = 0;
+       while( *seq )
+               if( strchr( ref, *seq++ ) ) val++;
+       return( val );
+}
+
 void getnumlen_casepreserve( FILE *fp, int *nlenminpt )
 {
        int total;
@@ -1929,6 +2116,53 @@ void getnumlen_casepreserve( FILE *fp, int *nlenminpt )
        }
 }
 
+void getnumlen_nogap_countn( FILE *fp, int *nlenminpt, double *nfreq )
+{
+       int total;
+       int nsite = 0;
+       int atgcnum, nnum, nN;
+       int i, tmp;
+       char *tmpseq, *tmpname;
+       double atgcfreq;
+       tmpname = AllocateCharVec( N );
+       njob = countKUorWA( fp );
+       searchKUorWA( fp );
+       nlenmax = 0;
+       *nlenminpt = 99999999;
+       atgcnum = 0;
+       total = 0;
+       nnum = 0;
+       for( i=0; i<njob; i++ )
+       {
+               myfgets( tmpname, N-1, fp );
+               tmpseq = load1SeqWithoutName_realloc( fp );
+               tmp = countnogaplen( tmpseq );
+               if( tmp > nlenmax ) nlenmax  = tmp;
+               if( tmp < *nlenminpt ) *nlenminpt  = tmp;
+               atgcnum += countATGCandN( tmpseq, &nN, &nsite );
+               total += nsite;
+               nnum += nN;
+               free( tmpseq );
+       }
+       free( tmpname );
+       atgcfreq = (double)atgcnum / total;
+       *nfreq = (double)nnum / atgcnum;
+//     fprintf( stderr, "##### nnum = %d\n", nnum );
+//     fprintf( stderr, "##### atgcfreq = %f, *nfreq = %f\n", atgcfreq, *nfreq );
+       if( dorp == NOTSPECIFIED )
+       {
+               if( atgcfreq > 0.75 )   
+               {
+                       dorp = 'd';
+                       upperCase = -1;
+               }
+               else                  
+               {
+                       dorp = 'p';
+                       upperCase = 0;
+               }
+       }
+}
 void getnumlen_nogap( FILE *fp, int *nlenminpt )
 {
        int total;
@@ -1957,7 +2191,7 @@ void getnumlen_nogap( FILE *fp, int *nlenminpt )
        }
        free( tmpname );
        atgcfreq = (double)atgcnum / total;
-       fprintf( stderr, "##### atgcfreq = %f\n", atgcfreq );
+//     fprintf( stderr, "##### atgcfreq = %f\n", atgcfreq );
        if( dorp == NOTSPECIFIED )
        {
                if( atgcfreq > 0.75 )   
@@ -2004,7 +2238,7 @@ void getnumlen_nogap_outallreg( FILE *fp, int *nlenminpt )
        }
        free( tmpname );
        atgcfreq = (double)atgcnum / total;
-       fprintf( stderr, "##### atgcfreq = %f\n", atgcfreq );
+//     fprintf( stderr, "##### atgcfreq = %f\n", atgcfreq );
        if( dorp == NOTSPECIFIED )
        {
                if( atgcfreq > 0.75 )   
@@ -2020,16 +2254,59 @@ void getnumlen_nogap_outallreg( FILE *fp, int *nlenminpt )
        }
 }
 
+static void escapehtml( char *res, char *ori, int maxlen )
+{
+       char *res0 = res;
+       while( *ori )
+       {
+               if( *ori == '<' ) 
+               {
+                       strcpy( res, "&lt;" );
+                       res += 3;
+               }
+               else if( *ori == '>' ) 
+               {
+                       strcpy( res, "&gt;" );
+                       res += 3;
+               }
+               else if( *ori == '&' ) 
+               {
+                       strcpy( res, "&amp;" );
+                       res += 4;
+               }
+               else if( *ori == '"' ) 
+               {
+                       strcpy( res, "&quot;" );
+                       res += 5;
+               }
+               else if( *ori == ' ' ) 
+               {
+                       strcpy( res, "&nbsp;" );
+                       res += 5;
+               }
+               else
+               {
+                       *res = *ori;
+               }
+               res++;
+               ori++;
+
+               if( res - res0 -10 > N ) break;
+       }
+       *res = 0;
+}
+
 void getnumlen_nogap_outallreg_web( FILE *fp, FILE *ofp, int *nlenminpt, int *isalignedpt )
 {
        int total;
        int nsite = 0;
        int atgcnum;
        int alnlen = 0, alnlen_prev;
-       int i, tmp;
-       char *tmpseq, *tmpname;
+       int i, tmp, lennormalchar;
+       char *tmpseq, *tmpname, *tmpname2;
        double atgcfreq;
        tmpname = AllocateCharVec( N );
+       tmpname2 = AllocateCharVec( N );
        njob = countKUorWA( fp );
        searchKUorWA( fp );
        nlenmax = 0;
@@ -2041,7 +2318,11 @@ void getnumlen_nogap_outallreg_web( FILE *fp, FILE *ofp, int *nlenminpt, int *is
        for( i=0; i<njob; i++ )
        {
                myfgets( tmpname, N-1, fp );
+               tmpname2[0] = tmpname[0];
+               escapehtml( tmpname2+1, tmpname+1, N );
 //             fprintf( stdout, "%s\n", tmpname );
+//             fprintf( stdout, "%s\n", tmpname2 );
+//             exit(1);
                tmpseq = load1SeqWithoutName_realloc_casepreserve( fp );
                tmp = countnogaplen( tmpseq );
 //             fprintf( stdout, "%d\n", tmp );
@@ -2055,7 +2336,6 @@ void getnumlen_nogap_outallreg_web( FILE *fp, FILE *ofp, int *nlenminpt, int *is
                if( i>0 && alnlen_prev != alnlen ) *isalignedpt = 0;
                alnlen_prev = alnlen;
 
-               free( tmpseq );
                atgcfreq = (double)atgcnum / total;
 //             fprintf( stderr, "##### atgcfreq = %f\n", atgcfreq );
 //             if( dorp == NOTSPECIFIED ) // you kentou
@@ -2072,34 +2352,36 @@ void getnumlen_nogap_outallreg_web( FILE *fp, FILE *ofp, int *nlenminpt, int *is
                        }
                }
        
+               if( dorp == 'd' ) lennormalchar = countnormalletters( tmpseq, "atgcuATGCU" );
+               else              lennormalchar = countnormalletters( tmpseq, "ARNDCQEGHILKMFPSTWYVarndcqeghilkmfpstwyv" );
+               free( tmpseq );
 
-
-
-               fprintf( ofp, " <label for='s%d'><input type='checkbox' id='s%d' name='s%d' checked>%s</label>\n", i, i, i, tmpname );
+               fprintf( ofp, " <label for='s%d'><span id='ss%d'><input type='checkbox' id='s%d' name='s%d' checked></span> <input type='text' class='ll' id='ll%d' style='display:none' size='6' value='%d' readonly='readonly'>%s</label>\n", i, i, i, i, i, lennormalchar, tmpname2 );
                fprintf( ofp, "<span id='t%d-0' style='display:none'>", i );
                fprintf( ofp, " <a href='javascript:void(0)' onclick='ddcycle(this.form,\"t%d\")'>+reg</a>", i );
-               fprintf( ofp, " Begin:<input type='text' name='b%d-0' size='8' value='1'> End:<input type='text' name='e%d-0' size='8' value='%d'>", i, i, tmp );
+               fprintf( ofp, " Begin:<input type='text' name='b%d-0' size='8' value='1' class='ie'> End:<input type='text' name='e%d-0' size='8' value='%d' class='ie'>", i, i, tmp );
                if( dorp == 'd' ) fprintf( ofp, " <label for='r%d-0'><input type='checkbox' name='r%d-0' id='r%d-0'>Reverse</label>", i, i, i );
-               fprintf( ofp, "  Sequence Length:<input type='text' name='l%d' size='8' value='%d' readonly='readonly'>", i, tmp );
+//             fprintf( ofp, "  Sequence Length:<input type='text' name='l%d' size='8' value='%d' readonly='readonly'>", i, tmp );
                fprintf( ofp, "\n</span>" );
                fprintf( ofp, "<span id='t%d-1' style='display:none'>", i );
-               fprintf( ofp, "      Begin:<input type='text' name='b%d-1' size='8' value=''> End:<input type='text' name='e%d-1' size='8' value=''>", i, i );
+               fprintf( ofp, "      Begin:<input type='text' name='b%d-1' size='8' value='' class='ie'> End:<input type='text' name='e%d-1' size='8' value='' class='ie'>", i, i );
                if( dorp == 'd' ) fprintf( ofp, " <label for='r%d-1'><input type='checkbox' name='r%d-1' id='r%d-1'>Reverse</label>", i, i, i );
                fprintf( ofp, "\n</span>" );
                fprintf( ofp, "<span id='t%d-2' style='display:none'>", i );
-               fprintf( ofp, "      Begin:<input type='text' name='b%d-2' size='8' value=''> End:<input type='text' name='e%d-2' size='8' value=''>", i, i );
+               fprintf( ofp, "      Begin:<input type='text' name='b%d-2' size='8' value='' class='ie'> End:<input type='text' name='e%d-2' size='8' value='' class='ie'>", i, i );
                if( dorp == 'd' ) fprintf( ofp, " <label for='r%d-2'><input type='checkbox' name='r%d-2' id='r%d-2'>Reverse</label>", i, i, i );
                fprintf( ofp, "\n</span>" );
                fprintf( ofp, "<span id='t%d-3' style='display:none'>", i );
-               fprintf( ofp, "      Begin:<input type='text' name='b%d-3' size='8' value=''> End:<input type='text' name='e%d-3' size='8' value=''>", i, i );
+               fprintf( ofp, "      Begin:<input type='text' name='b%d-3' size='8' value='' class='ie'> End:<input type='text' name='e%d-3' size='8' value='' class='ie'>", i, i );
                if( dorp == 'd' ) fprintf( ofp, " <label for='r%d-3'><input type='checkbox' name='r%d-3' id='r%d-3'>Reverse</label>", i, i, i );
                fprintf( ofp, "\n</span>" );
                fprintf( ofp, "<span id='t%d-4' style='display:none'>", i );
-               fprintf( ofp, "      Begin:<input type='text' name='b%d-4' size='8' value=''> End:<input type='text' name='e%d-4' size='8' value=''>", i, i );
+               fprintf( ofp, "      Begin:<input type='text' name='b%d-4' size='8' value='' class='ie'> End:<input type='text' name='e%d-4' size='8' value='' class='ie'>", i, i );
                if( dorp == 'd' ) fprintf( ofp, " <label for='r%d-4'><input type='checkbox' name='r%d-4' id='r%d-4'>Reverse</label>", i, i, i );
                fprintf( ofp, "\n</span>" );
        }
        free( tmpname );
+       free( tmpname2 );
        atgcfreq = (double)atgcnum / total;
        fprintf( stderr, "##### atgcfreq = %f\n", atgcfreq );
 //     if( dorp == NOTSPECIFIED ) // you kentou
@@ -2115,9 +2397,9 @@ void getnumlen_nogap_outallreg_web( FILE *fp, FILE *ofp, int *nlenminpt, int *is
                        upperCase = 0;
                }
        }
+       fprintf( ofp, "\n" );
        if( *isalignedpt )
        {
-               fprintf( ofp, "\n" );
                fprintf( ofp, "<span id='tall-0' style='display:none'>" );
                fprintf( ofp, "Cut the alignment\n" );
                fprintf( ofp, " <a href='javascript:void(0)' onclick='ddcycle(this.form,\"tall\")'>+reg</a>" );
@@ -2172,6 +2454,7 @@ void getnumlen( FILE *fp )
                free( tmpseq );
        }
 
+
        atgcfreq = (double)atgcnum / total;
 //     fprintf( stderr, "##### atgcfreq = %f\n", atgcfreq );
        if( dorp == NOTSPECIFIED )
@@ -2318,15 +2601,53 @@ void write1seq( FILE *fp, char *aseq )
                fprintf( fp, "%.*s\n", C, aseq+j );
 }
 
+void readhat2_doublehalf_part_pointer( FILE *fp, int nseq, int nadd, char **name, double **mtx )
+{
+    int i, j, nseq0, norg;
+    char b[B];
 
+    fgets( b, B, fp );
+    fgets( b, B, fp ); b[5] = 0; nseq0 = atoi( b ); if( nseq != nseq0 ) 
+       {
+               fprintf( stderr, "%d != %d\n", nseq, nseq0 );
+               ErrorExit( "hat2 is wrong." );
+       }
+    fgets( b, B, fp );
+    for( i=0; i<nseq; i++ )
+    {
+#if 0
+        getaline_fp_eof( b, B, fp ); 
+#else
+               myfgets( b, B-2, fp );
+#endif
+#if 0
+               j = MIN( strlen( b+6 ), 10 );
+        if( strncmp( name[i], b+6 , j ) ) 
+               {
+                       fprintf( stderr, "Error in hat2\n" );
+                       fprintf( stderr, "%s != %s\n", b, name[i] );
+                       exit( 1 );
+               }
+#endif
+    }
+       norg = nseq-nadd;
+    for( i=0; i<norg; i++ ) for( j=0; j<nadd; j++ )
+    {
+        mtx[i][j] = ( input_new( fp, D ) );
+    }
+}
 
-void readhat2_floathalf_pointer( FILE *fp, int nseq, char **name, float **mtx )
+void readhat2_doublehalf_pointer( FILE *fp, int nseq, char **name, double **mtx )
 {
     int i, j, nseq0;
     char b[B];
 
     fgets( b, B, fp );
-    fgets( b, B, fp ); b[5] = 0; nseq0 = atoi( b ); if( nseq != nseq0 ) ErrorExit( "hat2 is wrong." );
+    fgets( b, B, fp ); b[5] = 0; nseq0 = atoi( b ); if( nseq != nseq0 ) 
+       {
+               fprintf( stderr, "%d != %d\n", nseq, nseq0 );
+               ErrorExit( "hat2 is wrong." );
+       }
     fgets( b, B, fp );
     for( i=0; i<nseq; i++ )
     {
@@ -2350,7 +2671,7 @@ void readhat2_floathalf_pointer( FILE *fp, int nseq, char **name, float **mtx )
         mtx[i][j-i] = ( input_new( fp, D ) );
     }
 }
-void readhat2_floathalf( FILE *fp, int nseq, char name[M][B], float **mtx )
+void readhat2_doublehalf( FILE *fp, int nseq, char name[M][B], double **mtx )
 {
     int i, j, nseq0;
     char b[B];
@@ -2380,7 +2701,7 @@ void readhat2_floathalf( FILE *fp, int nseq, char name[M][B], float **mtx )
         mtx[i][j-i] = ( input_new( fp, D ) );
     }
 }
-void readhat2_float( FILE *fp, int nseq, char name[M][B], float **mtx )
+void readhat2_double( FILE *fp, int nseq, char name[M][B], double **mtx )
 {
     int i, j, nseq0;
     char b[B];
@@ -2502,7 +2823,7 @@ void readhat2( FILE *fp, int nseq, char name[M][B], double **mtx )
     }
 }
 
-void WriteFloatHat2_pointer_halfmtx( FILE *hat2p, int locnjob, char **name, float **mtx )
+void WriteFloatHat2_pointer_halfmtx( FILE *hat2p, int locnjob, char **name, double **mtx )
 {
        int i, j, ijsa;
        double max = 0.0;
@@ -2517,14 +2838,14 @@ void WriteFloatHat2_pointer_halfmtx( FILE *hat2p, int locnjob, char **name, floa
        {
                for( j=i+1; j<njob; j++ )
                {
-                       fprintf( hat2p, "%#6.3f", mtx[i][j-i] );
+                       fprintf( hat2p, DFORMAT, mtx[i][j-i] );
                        ijsa = j-i;
                        if( ijsa % 12 == 0 || ijsa == locnjob-i-1 ) fprintf( hat2p, "\n" );
                }
        }
 }
 
-void WriteFloatHat2_pointer( FILE *hat2p, int locnjob, char **name, float **mtx )
+void WriteFloatHat2_pointer( FILE *hat2p, int locnjob, char **name, double **mtx )
 {
        int i, j;
        double max = 0.0;
@@ -2539,13 +2860,13 @@ void WriteFloatHat2_pointer( FILE *hat2p, int locnjob, char **name, float **mtx
        {
                for( j=1; j<locnjob-i; j++ ) 
                {
-                       fprintf( hat2p, "%#6.3f", mtx[i][j] );
+                       fprintf( hat2p, DFORMAT, mtx[i][j] );
                        if( j % 12 == 0 || j == locnjob-i-1 ) fprintf( hat2p, "\n" );
                }
        }
 }
 
-void WriteFloatHat2( FILE *hat2p, int locnjob, char name[M][B], float **mtx )
+void WriteFloatHat2( FILE *hat2p, int locnjob, char name[M][B], double **mtx )
 {
        int i, j;
        double max = 0.0;
@@ -2560,7 +2881,7 @@ void WriteFloatHat2( FILE *hat2p, int locnjob, char name[M][B], float **mtx )
        {
                for( j=1; j<locnjob-i; j++ ) 
                {
-                       fprintf( hat2p, "%#6.3f", mtx[i][j] );
+                       fprintf( hat2p, DFORMAT, mtx[i][j] );
                        if( j % 12 == 0 || j == locnjob-i-1 ) fprintf( hat2p, "\n" );
                }
        }
@@ -2582,11 +2903,34 @@ void WriteHat2_int( FILE *hat2p, int locnjob, char name[M][B], int **mtx )
        {
                for( j=i+1; j<locnjob; j++ ) 
                {
-                       fprintf( hat2p, "%#6.3f", (float)mtx[i][j] / INTMTXSCALE );
+                       fprintf( hat2p, DFORMAT, (double)mtx[i][j] / INTMTXSCALE );
                        if( (j-i) % 12 == 0 || j == locnjob-1 ) fprintf( hat2p, "\n" );
                }
        }
 }
+
+void WriteHat2_part_pointer( FILE *hat2p, int locnjob, int nadd, char **name, double **mtx )
+{
+       int i, j;
+       int norg = locnjob-nadd;
+       double max = 0.0;
+//     for( i=0; i<locnjob-1; i++ ) for( j=i+1; j<locnjob; j++ ) if( mtx[i][j] > max ) max = mtx[i][j];
+
+       fprintf( hat2p, "%5d\n", 1 );
+       fprintf( hat2p, "%5d\n", locnjob );
+       fprintf( hat2p, " %#6.3f\n", max * 2.5 );
+
+       for( i=0; i<locnjob; i++ ) fprintf( hat2p, "%4d. %s\n", i+1, name[i] );
+       for( i=0; i<norg; i++ )
+       {
+               for( j=0; j<nadd; j++ ) 
+               {
+                       fprintf( hat2p, DFORMAT, mtx[i][j] );
+                       if( (j+1) % 12 == 0 || j == nadd-1 ) fprintf( hat2p, "\n" );
+               }
+       }
+}
+
 void WriteHat2_pointer( FILE *hat2p, int locnjob, char **name, double **mtx )
 {
        int i, j;
@@ -2602,11 +2946,12 @@ void WriteHat2_pointer( FILE *hat2p, int locnjob, char **name, double **mtx )
        {
                for( j=i+1; j<locnjob; j++ ) 
                {
-                       fprintf( hat2p, "%#6.3f", mtx[i][j] );
+                       fprintf( hat2p, DFORMAT, mtx[i][j] );
                        if( (j-i) % 12 == 0 || j == locnjob-1 ) fprintf( hat2p, "\n" );
                }
        }
 }
+
 void WriteHat2( FILE *hat2p, int locnjob, char name[M][B], double **mtx )
 {
        int i, j;
@@ -2622,7 +2967,7 @@ void WriteHat2( FILE *hat2p, int locnjob, char name[M][B], double **mtx )
        {
                for( j=i+1; j<locnjob; j++ ) 
                {
-                       fprintf( hat2p, "%#6.3f", mtx[i][j] );
+                       fprintf( hat2p, DFORMAT, mtx[i][j] );
                        if( (j-i) % 12 == 0 || j == locnjob-1 ) fprintf( hat2p, "\n" );
                }
        }
@@ -2957,7 +3302,7 @@ int ReadBlastm7_scoreonly( FILE *fp, double *dis, int nin )
     return count;
 }
 
-int ReadBlastm7( FILE *fp, double *dis, int qmem, char name[M][B], LocalHom *localhomlist )
+int ReadBlastm7( FILE *fp, double *dis, int qmem, char **name, LocalHom *localhomlist )
 {
     int count=0;
     char b[B];
@@ -3051,7 +3396,7 @@ int ReadBlastm7( FILE *fp, double *dis, int qmem, char name[M][B], LocalHom *loc
 
 //             fprintf( stderr, "t=%d, score = %f, qstart=%d, qend=%d, tstart=%d, tend=%d, overlapaa=%d\n", junban[count], score, qstart, qend, tstart, tend, overlapaa );
 
-               nlocalhom += addlocalhom_r( qal, tal, localhomlist+junban[count], qstart, tstart, score, overlapaa, nlocalhom );
+               nlocalhom += addlocalhom_r( qal, tal, localhomlist+junban[count], qstart, tstart, score, overlapaa, nlocalhom, 'h' );
 
                while( fgets( b, B-1, fp ) )
                        if( !strncmp( "            </Hsp>:", b, 18 ) ) break;
@@ -3072,7 +3417,7 @@ int ReadBlastm7( FILE *fp, double *dis, int qmem, char name[M][B], LocalHom *loc
     return count;
 }
 
-int ReadFasta34noalign( FILE *fp, double *dis, int qmem, char name[M][B], LocalHom *localhomlist )
+int ReadFasta34noalign( FILE *fp, double *dis, int qmem, char **name, LocalHom *localhomlist )
 {
     int count=0;
     char b[B];
@@ -3104,7 +3449,7 @@ int ReadFasta34noalign( FILE *fp, double *dis, int qmem, char name[M][B], LocalH
 
     return count;
 }
-int ReadFasta34m10_nuc( FILE *fp, double *dis, int qmem, char name[M][B], LocalHom *localhomlist )
+int ReadFasta34m10_nuc( FILE *fp, double *dis, int qmem, char **name, LocalHom *localhomlist )
 {
     int count=0;
     char b[B];
@@ -3238,12 +3583,12 @@ int ReadFasta34m10_nuc( FILE *fp, double *dis, int qmem, char name[M][B], LocalH
 //             fprintf( stderr, "tal2 = %s\n", tal2 );
 
 //             fprintf( stderr, "putting   %d - %d, opt = %d\n", qmem, junban[count-1], opt );
-               putlocalhom( qal2, tal2, localhomlist+junban[count-1], qstart, tstart, opt, overlapaa );
+               putlocalhom2( qal2, tal2, localhomlist+junban[count-1], qstart, tstart, opt, overlapaa, 'h' );
        }
 //     fprintf( stderr, "count = %d\n", count );
     return count;
 }
-int ReadFasta34m10( FILE *fp, double *dis, int qmem, char name[M][B], LocalHom *localhomlist )
+int ReadFasta34m10( FILE *fp, double *dis, int qmem, char **name, LocalHom *localhomlist )
 {
     int count=0;
     char b[B];
@@ -3375,7 +3720,7 @@ int ReadFasta34m10( FILE *fp, double *dis, int qmem, char name[M][B], LocalHom *
 //             fprintf( stderr, "tal2 = %s\n", tal2 );
 
 //             fprintf( stderr, "putting   %d - %d, opt = %d\n", qmem, junban[count-1], opt );
-               putlocalhom( qal2, tal2, localhomlist+junban[count-1], qstart, tstart, opt, overlapaa );
+               putlocalhom2( qal2, tal2, localhomlist+junban[count-1], qstart, tstart, opt, overlapaa, 'h' );
        }
 //     fprintf( stderr, "count = %d\n", count );
     return count;
@@ -3865,8 +4210,14 @@ void initFiles( void )
        fflush( trap_g );
 }
 
+void closeFiles( void )
+{
+       fclose( prep_g );
+       fclose( trap_g );
+}
 
-void WriteForFasta( FILE *fp, int locnjob, char name[][B], int nlen[M], char **aseq )
+
+void WriteForFasta( FILE *fp, int locnjob, char **name, int nlen[M], char **aseq )
 {
     static char b[N];
     int i, j;
@@ -3884,7 +4235,157 @@ void WriteForFasta( FILE *fp, int locnjob, char name[][B], int nlen[M], char **a
     }
 }
 
-void readlocalhomtable2( FILE*fp, int njob, LocalHom **localhomtable, char *kozoarivec )
+void readlocalhomtable2_target( FILE*fp, int njob, LocalHom **localhomtable, char *kozoarivec, int *targetmap )
+{
+       double opt;
+       static char buff[B];
+       char infor[100];
+       int i, j, overlapaa, start1, end1, start2, end2, it, jt;
+       LocalHom *tmpptr1, *tmpptr2;
+
+//     for( i=0; i<njob; i++ ) for( j=0; j<njob; j++ ) nlocalhom[i][j] = 0;
+
+       while ( NULL != fgets( buff, B-1, fp ) )
+       {
+//             fprintf( stderr, "\n" );
+               sscanf( buff, "%d %d %d %lf %d %d %d %d %s",  &i, &j, &overlapaa, &opt, &start1, &end1, &start2, &end2, infor );
+               if( *infor == 'k' ) kozoarivec[i] = kozoarivec[j] = 1;
+
+#if 0
+               if( start1 == end1 || start2 == end2 ) continue; //mondai ari
+#endif
+               it = targetmap[i];
+               if( it == -1 )
+               {
+                       reporterr( "hat3 ga okashii.  _target_ \n" );
+                       exit( 1 );
+               }
+               jt = targetmap[j];
+
+
+
+//             if( i < j )
+               {
+                       if( localhomtable[it][j].nokori++ > 0 )
+                       {
+                               tmpptr1 = localhomtable[it][j].last;
+//                             fprintf( stderr, "reallocating, localhomtable[%d][%d].nokori = %d\n", i, j, localhomtable[i][j].nokori );
+                               tmpptr1->next = (LocalHom *)calloc( 1, sizeof( LocalHom ) );
+                               tmpptr1 = tmpptr1->next;
+                               tmpptr1->extended = -1;
+                               tmpptr1->next = NULL;
+                               localhomtable[it][j].last = tmpptr1;
+//                             fprintf( stderr, "### i,j = %d,%d, nokori=%d\n", i, j, localhomtable[i][j].nokori );
+                       }
+                       else
+                       {
+                               tmpptr1 = localhomtable[it]+j;
+//                             fprintf( stderr, "### i,j = %d,%d, nokori=%d\n", i, j, localhomtable[i][j].nokori );
+                       }
+       
+                       tmpptr1->start1 = start1;
+                       tmpptr1->start2 = start2;
+                       tmpptr1->end1 = end1;
+                       tmpptr1->end2 = end2;
+//                     tmpptr1->opt = ( opt / overlapaa + 0.00 ) / 5.8  * 600;
+//                     tmpptr1->opt = opt;
+                       tmpptr1->opt = ( opt + 0.00 ) / 5.8  * 600;
+                       tmpptr1->overlapaa = overlapaa;
+                       tmpptr1->korh = *infor;
+               }
+//             else
+               if( jt != -1 )
+               {
+                       if( localhomtable[jt][i].nokori++ > 0 )
+                       {
+                               tmpptr2 = localhomtable[jt][i].last;
+                               tmpptr2->next = (LocalHom *)calloc( 1, sizeof( LocalHom ) );
+                               tmpptr2 = tmpptr2->next;
+                               tmpptr2->extended = -1;
+                               tmpptr2->next = NULL;
+                               localhomtable[jt][i].last = tmpptr2;
+//                             fprintf( stderr, "### i,j = %d,%d, nokori=%d\n", j, i, localhomtable[j][i].nokori );
+                       }
+                       else
+                       {
+                               tmpptr2 = localhomtable[jt]+i;
+//                             fprintf( stderr, "### i,j = %d,%d, nokori=%d\n", j, i, localhomtable[j][i].nokori );
+                       }
+       
+                       tmpptr2->start2 = start1;
+                       tmpptr2->start1 = start2;
+                       tmpptr2->end2 = end1;
+                       tmpptr2->end1 = end2;
+//                     tmpptr2->opt = ( opt / overlapaa + 0.00 ) / 5.8  * 600;
+//                     tmpptr2->opt = opt;
+                       tmpptr2->opt = ( opt + 0.00 ) / 5.8  * 600;
+                       tmpptr2->overlapaa = overlapaa;
+                       tmpptr2->korh = *infor;
+       
+//                     fprintf( stderr, "i=%d, j=%d, st1=%d, en1=%d, opt = %f\n", i, j, tmpptr1->start1, tmpptr1->end1, opt );
+               }
+
+       }
+}
+
+void readlocalhomtable2_half( FILE*fp, int njob, LocalHom **localhomtable, char *kozoarivec )
+{
+       double opt;
+       static char buff[B];
+       char infor[100];
+       int i, j, overlapaa, start1, end1, start2, end2;
+       LocalHom *tmpptr1;
+
+//     for( i=0; i<njob; i++ ) for( j=0; j<njob; j++ ) nlocalhom[i][j] = 0;
+
+       while ( NULL != fgets( buff, B-1, fp ) )
+       {
+//             fprintf( stderr, "\n" );
+               sscanf( buff, "%d %d %d %lf %d %d %d %d %s",  &i, &j, &overlapaa, &opt, &start1, &end1, &start2, &end2, infor );
+               if( *infor == 'k' ) kozoarivec[i] = kozoarivec[j] = 1;
+
+#if 0
+               if( start1 == end1 || start2 == end2 ) continue; //mondai ari
+#endif
+
+               if( j <= i )
+               {
+                       reporterr( "Check hat3.  The first sequence must be younger than the second one.\n" );
+                       exit( 1 );
+               }
+               {
+                       if( localhomtable[i][j-i].nokori++ > 0 )
+                       {
+                               tmpptr1 = localhomtable[i][j-i].last;
+//                             fprintf( stderr, "reallocating, localhomtable[%d][%d].nokori = %d\n", i, j, localhomtable[i][j].nokori );
+                               tmpptr1->next = (LocalHom *)calloc( 1, sizeof( LocalHom ) );
+                               tmpptr1 = tmpptr1->next;
+                               tmpptr1->extended = -1;
+                               tmpptr1->next = NULL;
+                               localhomtable[i][j-i].last = tmpptr1;
+//                             fprintf( stderr, "### i,j = %d,%d, nokori=%d\n", i, j, localhomtable[i][j-i].nokori );
+                       }
+                       else
+                       {
+                               tmpptr1 = localhomtable[i]+j-i;
+//                             fprintf( stderr, "### i,j = %d,%d, nokori=%d\n", i, j, localhomtable[i][j-i].nokori );
+                       }
+       
+                       tmpptr1->start1 = start1;
+                       tmpptr1->start2 = start2;
+                       tmpptr1->end1 = end1;
+                       tmpptr1->end2 = end2;
+//                     tmpptr1->opt = ( opt / overlapaa + 0.00 ) / 5.8  * 600;
+//                     tmpptr1->opt = opt;
+                       tmpptr1->opt = ( opt + 0.00 ) / 5.8  * 600;
+                       tmpptr1->overlapaa = overlapaa;
+                       tmpptr1->korh = *infor;
+               }
+
+       }
+}
+
+void readlocalhomtable2( FILE*fp, int njob, LocalHom **localhomtable, char *kozoarivec )
 {
        double opt;
        static char buff[B];
@@ -3966,17 +4467,19 @@ void readlocalhomtable2( FILE*fp, int njob, LocalHom **localhomtable, char *kozo
 
        }
 }
-void readlocalhomtable( FILE*fp, int njob, LocalHom **localhomtable, char *kozoarivec )
+
+#if 0
+void readlocalhomtable_target( FILE*fp, int ntarget, int njob, LocalHom **localhomtable, char *kozoarivec, int *targetmap )
 {
        double opt;
        static char buff[B];
        char infor[100];
-       int i, j, overlapaa, start1, end1, start2, end2;
+       int i, j, overlapaa, start1, end1, start2, end2, it, jt;
        int **nlocalhom = NULL;
        LocalHom *tmpptr1=NULL, *tmpptr2=NULL; // by D.Mathog, a guess
 
        nlocalhom = AllocateIntMtx( njob, njob );
-       for( i=0; i<njob; i++ ) for( j=0; j<njob; j++ ) nlocalhom[i][j] = 0;
+       for( i=0; i<ntarget; i++ ) for( j=0; j<njob; j++ ) nlocalhom[i][j] = 0;
 
        while ( NULL != fgets( buff, B-1, fp ) )
        {
@@ -3988,9 +4491,109 @@ void readlocalhomtable( FILE*fp, int njob, LocalHom **localhomtable, char *kozoa
                if( start1 == end1 || start2 == end2 ) continue; //mondai ari
 #endif
 
+               printf( "reading %d-%d\n", i, j );
+
+               it = targetmap[i];
+               if( it == -1 )
+               {
+                       reporterr( "hat3 ga okashii.  _target_ \n" );
+                       exit( 1 );
+               }
+               jt = targetmap[j];
 
 //             if( i < j )
                {
+                       if( nlocalhom[it][j]++ > 0 )
+                       {
+                               printf( "extending %d-%d, ->%d\n", i, j, nlocalhom[it][j] );
+//                             fprintf( stderr, "reallocating, nlocalhom[%d][%d] = %d\n", i, j, nlocalhom[i][j] );
+                               tmpptr1->next = (LocalHom *)calloc( 1, sizeof( LocalHom ) );
+                               tmpptr1 = tmpptr1->next;
+                               tmpptr1->next = NULL;
+                       }
+                       else
+                       {
+                               tmpptr1 = localhomtable[it]+j;
+//                             fprintf( stderr, "nlocalhom[%d][%d] = %d\n", i, j, nlocalhom[i][j] );
+                       }
+       
+                       tmpptr1->start1 = start1; // CHUUI!!!!
+                       tmpptr1->start2 = start2;
+                       tmpptr1->end1 = end1; // CHUUI!!!!
+                       tmpptr1->end2 = end2;
+//                     tmpptr1->opt = ( opt / overlapaa + 0.00 ) / 5.8  * 600;
+//                     tmpptr1->opt = opt;
+                       tmpptr1->opt = ( opt + 0.00 ) / 5.8  * 600;
+                       tmpptr1->overlapaa = overlapaa;
+                       tmpptr1->korh = *infor;
+       
+//                     fprintf( stderr, "i=%d, j=%d, opt = %f\n", i, j, opt );
+
+
+               }
+//             else
+
+               if( jt != -1 )
+               {
+                       if( nlocalhom[jt][i]++ > 0 )
+                       {
+                               printf( "extending %d-%d, ->%d\n", i, j, nlocalhom[jt][i] );
+                               tmpptr2->next = (LocalHom *)calloc( 1, sizeof( LocalHom ) );
+                               tmpptr2 = tmpptr2->next;
+                               tmpptr2->next = NULL;
+                       }
+                       else
+                               tmpptr2 = localhomtable[jt]+i;
+       
+                       tmpptr2->start2 = start1; // CHUUI!!!!
+                       tmpptr2->start1 = start2;
+                       tmpptr2->end2 = end1; // CHUUI!!!!
+                       tmpptr2->end1 = end2;
+//                     tmpptr2->opt = ( opt / overlapaa + 0.00 ) / 5.8  * 600;
+//                     tmpptr2->opt = opt;
+                       tmpptr2->opt = ( opt + 0.00 ) / 5.8  * 600;
+                       tmpptr2->overlapaa = overlapaa;
+                       tmpptr2->korh = *infor;
+
+//                     fprintf( stderr, "j=%d, i=%d, opt = %f\n", j, i, opt );
+               }
+
+       }
+       LocalHom *tmpptr;
+       for( tmpptr = localhomtable[1]+11; tmpptr; tmpptr=tmpptr->next )
+               fprintf( stdout, "reg1=%d-%d, reg2=%d-%d, imp=%f, opt=%f, next=%p\n", tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->importance, tmpptr->opt / 600 * 5.8, tmpptr->next );
+       FreeIntMtx( nlocalhom );
+}
+
+void readlocalhomtable_half( FILE*fp, int njob, LocalHom **localhomtable, char *kozoarivec )
+{
+       double opt;
+       static char buff[B];
+       char infor[100];
+       int i, j, overlapaa, start1, end1, start2, end2;
+       int **nlocalhom = NULL;
+       LocalHom *tmpptr1=NULL; // by D.Mathog, a guess
+
+       nlocalhom = AllocateIntMtx( njob, njob );
+       for( i=0; i<njob; i++ ) for( j=0; j<njob; j++ ) nlocalhom[i][j] = 0;
+
+       while ( NULL != fgets( buff, B-1, fp ) )
+       {
+//             fprintf( stderr, "\n" );
+               sscanf( buff, "%d %d %d %lf %d %d %d %d %s",  &i, &j, &overlapaa, &opt, &start1, &end1, &start2, &end2, infor );
+               if( *infor == 'k' ) kozoarivec[i] = kozoarivec[j] = 1;
+
+#if 0
+               if( start1 == end1 || start2 == end2 ) continue; //mondai ari
+#endif
+
+
+               if( j <= i )
+               {
+                       reporterr( "Check hat3.  The first sequence must be younger than the second one.\n" );
+                       exit( 1 );
+               }
+               {
                        if( nlocalhom[i][j]++ > 0 )
                        {
 //                             fprintf( stderr, "reallocating, nlocalhom[%d][%d] = %d\n", i, j, nlocalhom[i][j] );
@@ -4000,13 +4603,13 @@ void readlocalhomtable( FILE*fp, int njob, LocalHom **localhomtable, char *kozoa
                        }
                        else
                        {
-                               tmpptr1 = localhomtable[i]+j;
+                               tmpptr1 = localhomtable[i]+j-i;
 //                             fprintf( stderr, "nlocalhom[%d][%d] = %d\n", i, j, nlocalhom[i][j] );
                        }
        
-                       tmpptr1->start1 = start1;
+                       tmpptr1->start1 = start1; // CHUUI!!!!
                        tmpptr1->start2 = start2;
-                       tmpptr1->end1 = end1;
+                       tmpptr1->end1 = end1; // CHUUI!!!!
                        tmpptr1->end2 = end2;
 //                     tmpptr1->opt = ( opt / overlapaa + 0.00 ) / 5.8  * 600;
 //                     tmpptr1->opt = opt;
@@ -4016,64 +4619,504 @@ void readlocalhomtable( FILE*fp, int njob, LocalHom **localhomtable, char *kozoa
        
 //                     fprintf( stderr, "i=%d, j=%d, opt = %f\n", i, j, opt );
                }
-//             else
+       }
+       FreeIntMtx( nlocalhom );
+}
+#endif
+
+void readlocalhomtable( FILE*fp, int njob, LocalHom **localhomtable, char *kozoarivec )
+{
+       double opt;
+       static char buff[B];
+       char infor[100];
+       int i, j, overlapaa, start1, end1, start2, end2;
+       int **nlocalhom = NULL;
+       LocalHom *tmpptr1=NULL, *tmpptr2=NULL; // by D.Mathog, a guess
+
+       nlocalhom = AllocateIntMtx( njob, njob );
+       for( i=0; i<njob; i++ ) for( j=0; j<njob; j++ ) nlocalhom[i][j] = 0;
+
+       while ( NULL != fgets( buff, B-1, fp ) )
+       {
+//             fprintf( stderr, "\n" );
+               sscanf( buff, "%d %d %d %lf %d %d %d %d %s",  &i, &j, &overlapaa, &opt, &start1, &end1, &start2, &end2, infor );
+               if( *infor == 'k' ) kozoarivec[i] = kozoarivec[j] = 1;
+
+#if 0
+               if( start1 == end1 || start2 == end2 ) continue; //mondai ari
+#endif
+
+
+               if( j <= i )
+               {
+                       reporterr( "Check hat3.  The first sequence must be younger than the second one.\n" );
+                       exit( 1 );
+               }
+               {
+                       if( nlocalhom[i][j]++ > 0 )
+                       {
+//                             fprintf( stderr, "reallocating, nlocalhom[%d][%d] = %d\n", i, j, nlocalhom[i][j] );
+                               tmpptr1->next = (LocalHom *)calloc( 1, sizeof( LocalHom ) );
+                               tmpptr1 = tmpptr1->next;
+                               tmpptr1->next = NULL;
+                       }
+                       else
+                       {
+                               tmpptr1 = localhomtable[i]+j;
+//                             fprintf( stderr, "nlocalhom[%d][%d] = %d\n", i, j, nlocalhom[i][j] );
+                       }
+       
+                       tmpptr1->start1 = start1; // CHUUI!!!!
+                       tmpptr1->start2 = start2;
+                       tmpptr1->end1 = end1; // CHUUI!!!!
+                       tmpptr1->end2 = end2;
+//                     tmpptr1->opt = ( opt / overlapaa + 0.00 ) / 5.8  * 600;
+//                     tmpptr1->opt = opt;
+                       tmpptr1->opt = ( opt + 0.00 ) / 5.8  * 600;
+                       tmpptr1->overlapaa = overlapaa;
+                       tmpptr1->korh = *infor;
+       
+//                     fprintf( stderr, "i=%d, j=%d, opt = %f\n", i, j, opt );
+               }
+//             else
+               {
+                       if( nlocalhom[j][i]++ > 0 )
+                       {
+                               tmpptr2->next = (LocalHom *)calloc( 1, sizeof( LocalHom ) );
+                               tmpptr2 = tmpptr2->next;
+                               tmpptr2->next = NULL;
+                       }
+                       else
+                               tmpptr2 = localhomtable[j]+i;
+       
+                       tmpptr2->start2 = start1; // CHUUI!!!!
+                       tmpptr2->start1 = start2;
+                       tmpptr2->end2 = end1; // CHUUI!!!!
+                       tmpptr2->end1 = end2;
+//                     tmpptr2->opt = ( opt / overlapaa + 0.00 ) / 5.8  * 600;
+//                     tmpptr2->opt = opt;
+                       tmpptr2->opt = ( opt + 0.00 ) / 5.8  * 600;
+                       tmpptr2->overlapaa = overlapaa;
+                       tmpptr2->korh = *infor;
+
+//                     fprintf( stderr, "j=%d, i=%d, opt = %f\n", j, i, opt );
+               }
+
+       }
+       FreeIntMtx( nlocalhom );
+}
+
+
+void readlocalhomtable_two( FILE*fp, int norg, int nadd, LocalHom **localhomtable, LocalHom **localhomtablex, char *kozoarivec ) // for test only
+{
+       double opt;
+       static char buff[B];
+       char infor[100];
+       int i, j, overlapaa, start1, end1, start2, end2;
+       int **nlocalhom = NULL;
+       int **nlocalhomx = NULL;
+       LocalHom *tmpptr1=NULL, *tmpptr2=NULL; // by D.Mathog, a guess
+
+       nlocalhom = AllocateIntMtx( norg, nadd );
+       for( i=0; i<norg; i++ ) for( j=0; j<nadd; j++ ) nlocalhom[i][j] = 0;
+       nlocalhomx = AllocateIntMtx( nadd, norg );
+       for( i=0; i<nadd; i++ ) for( j=0; j<norg; j++ ) nlocalhomx[i][j] = 0;
+
+       while ( NULL != fgets( buff, B-1, fp ) )
+       {
+//             fprintf( stderr, "\n" );
+               sscanf( buff, "%d %d %d %lf %d %d %d %d %s",  &i, &j, &overlapaa, &opt, &start1, &end1, &start2, &end2, infor );
+               if( *infor == 'k' ) 
+               {
+                       fprintf( stderr, "Not supported!\n" );
+                       exit( 1 );
+               }
+               j -= norg;
+
+#if 0
+               if( start1 == end1 || start2 == end2 ) continue; //mondai ari
+#endif
+
+
+//             if( i < j )
+               {
+                       if( nlocalhom[i][j]++ > 0 )
+                       {
+//                             fprintf( stderr, "reallocating, nlocalhom[%d][%d] = %d\n", i, j, nlocalhom[i][j] );
+                               tmpptr1->next = (LocalHom *)calloc( 1, sizeof( LocalHom ) );
+                               tmpptr1 = tmpptr1->next;
+                               tmpptr1->next = NULL;
+                       }
+                       else
+                       {
+                               tmpptr1 = localhomtable[i]+j;
+//                             fprintf( stderr, "nlocalhom[%d][%d] = %d\n", i, j, nlocalhom[i][j] );
+                       }
+       
+                       tmpptr1->start1 = start1; // CHUUI!!!!
+                       tmpptr1->start2 = start2;
+                       tmpptr1->end1 = end1; // CHUUI!!!!
+                       tmpptr1->end2 = end2;
+//                     tmpptr1->opt = ( opt / overlapaa + 0.00 ) / 5.8  * 600;
+//                     tmpptr1->opt = opt;
+                       tmpptr1->opt = ( opt + 0.00 ) / 5.8  * 600;
+                       tmpptr1->overlapaa = overlapaa;
+                       tmpptr1->korh = *infor;
+       
+//                     fprintf( stderr, "i=%d, j=%d, opt = %f\n", i, j, opt );
+               }
+
+               {
+                       if( nlocalhomx[j][i]++ > 0 )
+                       {
+                               tmpptr2->next = (LocalHom *)calloc( 1, sizeof( LocalHom ) );
+                               tmpptr2 = tmpptr2->next;
+                               tmpptr2->next = NULL;
+                       }
+                       else
+                               tmpptr2 = localhomtablex[j]+i;
+       
+                       tmpptr2->start2 = start1+1; // CHUUI!!!!
+                       tmpptr2->start1 = start2;
+                       tmpptr2->end2 = end1+1; // CHUUI!!!!
+                       tmpptr2->end1 = end2;
+//                     tmpptr2->opt = ( opt / overlapaa + 0.00 ) / 5.8  * 600;
+//                     tmpptr2->opt = opt;
+                       tmpptr2->opt = ( opt + 0.00 ) / 5.8  * 600;
+                       tmpptr2->overlapaa = overlapaa;
+                       tmpptr2->korh = *infor;
+
+//                     fprintf( stderr, "j=%d, i=%d, opt = %f\n", j, i, opt );
+               }
+
+       }
+       FreeIntMtx( nlocalhom );
+       FreeIntMtx( nlocalhomx );
+}
+
+void readlocalhomtable_one( FILE*fp, int norg, int nadd, LocalHom **localhomtable, char *kozoarivec ) // for test only
+{
+       double opt;
+       static char buff[B];
+       char infor[100];
+       int i, j, overlapaa, start1, end1, start2, end2;
+       int **nlocalhom = NULL;
+       LocalHom *tmpptr1=NULL; // by D.Mathog, a guess
+
+       nlocalhom = AllocateIntMtx( norg, nadd );
+       for( i=0; i<norg; i++ ) for( j=0; j<nadd; j++ ) nlocalhom[i][j] = 0;
+
+       while ( NULL != fgets( buff, B-1, fp ) )
+       {
+//             fprintf( stderr, "\n" );
+               sscanf( buff, "%d %d %d %lf %d %d %d %d %s",  &i, &j, &overlapaa, &opt, &start1, &end1, &start2, &end2, infor );
+               if( *infor == 'k' ) 
+               {
+                       fprintf( stderr, "Not supported!\n" );
+                       exit( 1 );
+               }
+               j -= norg;
+
+#if 0
+               if( start1 == end1 || start2 == end2 ) continue; //mondai ari
+#endif
+
+
+//             if( i < j )
+               {
+                       if( nlocalhom[i][j]++ > 0 )
+                       {
+//                             fprintf( stderr, "reallocating, nlocalhom[%d][%d] = %d\n", i, j, nlocalhom[i][j] );
+                               tmpptr1->next = (LocalHom *)calloc( 1, sizeof( LocalHom ) );
+                               tmpptr1 = tmpptr1->next;
+                               tmpptr1->next = NULL;
+                       }
+                       else
+                       {
+                               tmpptr1 = localhomtable[i]+j;
+//                             fprintf( stderr, "nlocalhom[%d][%d] = %d\n", i, j, nlocalhom[i][j] );
+                       }
+       
+                       tmpptr1->start1 = start1; // CHUUI!!!!
+                       tmpptr1->start2 = start2;
+                       tmpptr1->end1 = end1; // CHUUI!!!!
+                       tmpptr1->end2 = end2;
+//                     tmpptr1->opt = ( opt / overlapaa + 0.00 ) / 5.8  * 600;
+//                     tmpptr1->opt = opt;
+                       tmpptr1->opt = ( opt + 0.00 ) / 5.8  * 600;
+                       tmpptr1->overlapaa = overlapaa;
+                       tmpptr1->korh = *infor;
+       
+//                     fprintf( stderr, "i=%d, j=%d, opt = %f\n", i, j, opt );
+               }
+
+       }
+       FreeIntMtx( nlocalhom );
+}
+
+void outlocalhom_part( LocalHom **localhom, int norg, int nadd )
+{
+       int i, j;
+       LocalHom *tmpptr;
+       for( i=0; i<norg; i++ ) for( j=0; j<nadd; j++ )
+       {
+               tmpptr = localhom[i]+j;
+               fprintf( stdout, "%d-%d\n", i, j+norg );
+               do
+               {
+                       fprintf( stdout, "reg1=%d-%d, reg2=%d-%d, imp=%f, opt=%f\n", tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->importance, tmpptr->opt / 600 * 5.8 );
+               }
+               while( (tmpptr=tmpptr->next) );
+       }
+}
+
+void outlocalhom_target( LocalHom **localhom, int norg, int nadd )
+{
+       int i, j;
+       LocalHom *tmpptr;
+       for( i=0; i<norg; i++ ) for( j=0; j<nadd; j++ )
+       {
+               tmpptr = localhom[i]+j;
+               fprintf( stdout, "%d-%d\n", i, j );
+               for( ; tmpptr; tmpptr=tmpptr->next )
+               {
+                       fprintf( stdout, "reg1=%d-%d, reg2=%d-%d, imp=%f, opt=%f, next=%p\n", tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->importance, tmpptr->opt / 600 * 5.8, (void *)tmpptr->next );
+               }
+//             while( (tmpptr=tmpptr->next) );
+       }
+}
+
+void outlocalhom_half( LocalHom **localhom, int nseq )
+{
+       int i, j;
+       LocalHom *tmpptr;
+       for( i=0; i<nseq-1; i++ ) for( j=i+1; j<nseq; j++ )
+       {
+               tmpptr = localhom[i]+j-i;
+               fprintf( stdout, "%d-%d\n", i, j );
+               do
+               {
+                       fprintf( stdout, "reg1=%d-%d, reg2=%d-%d, imp=%f, opt=%f, next=%p\n", tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->importance, tmpptr->opt / 600 * 5.8, (void *)tmpptr->next );
+               }
+               while( (tmpptr=tmpptr->next) );
+       }
+}
+
+void outlocalhom( LocalHom **localhom, int nseq )
+{
+       int i, j;
+       LocalHom *tmpptr;
+       for( i=0; i<nseq; i++ ) for( j=0; j<nseq; j++ )
+       {
+               tmpptr = localhom[i]+j;
+               fprintf( stderr, "%d-%d\n", i, j );
+               do
+               {
+                       fprintf( stderr, "reg1=%d-%d, reg2=%d-%d, imp=%f, opt=%f\n", tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->importance, tmpptr->opt );
+               }
+               while( (tmpptr=tmpptr->next) );
+       }
+}
+
+void outlocalhompt( LocalHom ***localhom, int n1, int n2 )
+{
+       int i, j;
+       LocalHom *tmpptr;
+       for( i=0; i<n1; i++ ) for( j=0; j<n2; j++ )
+       {
+               tmpptr = localhom[i][j];
+//             fprintf( stdout, "%d-%d\n", i, j );
+               do
+               {
+                       fprintf( stdout, "%d-%d, reg1=%d-%d, reg2=%d-%d, imp=%f, opt=%f\n", i, j, tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->importance, tmpptr->opt );
+               }
+               while( (tmpptr=tmpptr->next) );
+       }
+}
+
+void FreeLocalHomTable_part( LocalHom **localhomtable, int n, int m ) 
+{
+       int i, j;
+       LocalHom *ppp, *tmpptr;
+       for( i=0; i<n; i++ ) 
+       {
+               for( j=0; j<m; j++ )
+               {
+                       tmpptr=localhomtable[i]+j;
+                       ppp = tmpptr->next;
+                       for( ; tmpptr; tmpptr=ppp )
+                       {
+#if DEBUG
+                               fprintf( stderr, "i=%d, j=%d\n", i, j ); 
+#endif
+                               ppp = tmpptr->next;
+                               if( tmpptr!=localhomtable[i]+j ) 
+                               {
+#if DEBUG
+                                       fprintf( stderr, "freeing %p\n", tmpptr );
+#endif
+                                       free( tmpptr );
+                               }
+                       }
+               }
+#if DEBUG
+               fprintf( stderr, "freeing localhomtable[%d]\n", i );
+#endif
+               free( localhomtable[i] );
+       }
+#if DEBUG
+       fprintf( stderr, "freeing localhomtable\n" );
+#endif
+       free( localhomtable );
+#if DEBUG
+       fprintf( stderr, "freed\n" );
+#endif
+}
+
+void FreeLocalHomTable_two( LocalHom **localhomtable, int n, int m ) 
+{
+       int i, j;
+       LocalHom *ppp, *tmpptr;
+       for( i=0; i<n; i++ ) 
+       {
+               for( j=0; j<m; j++ )
+               {
+                       tmpptr=localhomtable[i]+j;
+                       ppp = tmpptr->next;
+                       for( ; tmpptr; tmpptr=ppp )
+                       {
+#if DEBUG
+                               fprintf( stderr, "i=%d, j=%d\n", i, j ); 
+#endif
+                               ppp = tmpptr->next;
+                               if( tmpptr!=localhomtable[i]+j ) 
+                               {
+#if DEBUG
+                                       fprintf( stderr, "freeing %p\n", tmpptr );
+#endif
+                                       free( tmpptr );
+                               }
+                       }
+               }
+#if DEBUG
+               fprintf( stderr, "freeing localhomtable[%d]\n", i );
+#endif
+               free( localhomtable[i] );
+       }
+
+       for( i=n; i<n+m; i++ ) 
+       {
+               for( j=0; j<n; j++ )
                {
-                       if( nlocalhom[j][i]++ > 0 )
+                       tmpptr=localhomtable[i]+j;
+                       ppp = tmpptr->next;
+                       for( ; tmpptr; tmpptr=ppp )
                        {
-                               tmpptr2->next = (LocalHom *)calloc( 1, sizeof( LocalHom ) );
-                               tmpptr2 = tmpptr2->next;
-                               tmpptr2->next = NULL;
+#if DEBUG
+                               fprintf( stderr, "i=%d, j=%d\n", i, j ); 
+#endif
+                               ppp = tmpptr->next;
+                               if( tmpptr!=localhomtable[i]+j ) 
+                               {
+#if DEBUG
+                                       fprintf( stderr, "freeing %p\n", tmpptr );
+#endif
+                                       free( tmpptr );
+                               }
                        }
-                       else
-                               tmpptr2 = localhomtable[j]+i;
-       
-                       tmpptr2->start2 = start1;
-                       tmpptr2->start1 = start2;
-                       tmpptr2->end2 = end1;
-                       tmpptr2->end1 = end2;
-//                     tmpptr2->opt = ( opt / overlapaa + 0.00 ) / 5.8  * 600;
-//                     tmpptr2->opt = opt;
-                       tmpptr2->opt = ( opt + 0.00 ) / 5.8  * 600;
-                       tmpptr2->overlapaa = overlapaa;
-                       tmpptr2->korh = *infor;
                }
-
+#if DEBUG
+               fprintf( stderr, "freeing localhomtable[%d]\n", i );
+#endif
+               free( localhomtable[i] );
        }
-       FreeIntMtx( nlocalhom );
+#if DEBUG
+       fprintf( stderr, "freeing localhomtable\n" );
+#endif
+       free( localhomtable );
+#if DEBUG
+       fprintf( stderr, "freed\n" );
+#endif
 }
 
-void outlocalhom( LocalHom **localhom, int nseq )
+void FreeLocalHomTable_one( LocalHom **localhomtable, int n, int m ) 
 {
        int i, j;
-       LocalHom *tmpptr;
-       for( i=0; i<nseq; i++ ) for( j=0; j<nseq; j++ )
+       LocalHom *ppp, *tmpptr;
+       for( i=0; i<n; i++ ) 
        {
-               tmpptr = localhom[i]+j;
-               fprintf( stderr, "%d-%d\n", i, j );
-               do
+               for( j=0; j<m; j++ )
                {
-                       fprintf( stderr, "reg1=%d-%d, reg2=%d-%d, imp=%f, opt=%f\n", tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->importance, tmpptr->opt );
+                       tmpptr=localhomtable[i]+j;
+                       ppp = tmpptr->next;
+                       for( ; tmpptr; tmpptr=ppp )
+                       {
+#if DEBUG
+                               fprintf( stderr, "i=%d, j=%d\n", i, j ); 
+#endif
+                               ppp = tmpptr->next;
+                               if( tmpptr!=localhomtable[i]+j ) 
+                               {
+#if DEBUG
+                                       fprintf( stderr, "freeing %p\n", tmpptr );
+#endif
+                                       free( tmpptr );
+                               }
+                       }
                }
-               while( (tmpptr=tmpptr->next) );
+#if DEBUG
+               fprintf( stderr, "freeing localhomtable[%d]\n", i );
+#endif
+               free( localhomtable[i] );
        }
+
+#if DEBUG
+       fprintf( stderr, "freeing localhomtable\n" );
+#endif
+       free( localhomtable );
+#if DEBUG
+       fprintf( stderr, "freed\n" );
+#endif
 }
 
-void outlocalhompt( LocalHom ***localhom, int n1, int n2 )
+void FreeLocalHomTable_half( LocalHom **localhomtable, int n ) 
 {
        int i, j;
-       LocalHom *tmpptr;
-       for( i=0; i<n1; i++ ) for( j=0; j<n2; j++ )
+       LocalHom *ppp, *tmpptr;
+       for( i=0; i<n; i++ ) 
        {
-               tmpptr = localhom[i][j];
-               fprintf( stderr, "%d-%d\n", i, j );
-               do
+               for( j=0; j<n-i; j++ )
                {
-                       fprintf( stderr, "reg1=%d-%d, reg2=%d-%d, imp=%f, opt=%f, wimp=%f\n", tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->importance, tmpptr->opt, tmpptr->wimportance );
+                       tmpptr=localhomtable[i]+j;
+                       ppp = tmpptr->next;
+                       for( ; tmpptr; tmpptr=ppp )
+                       {
+#if DEBUG
+                               fprintf( stderr, "i=%d, j=%d\n", i, j ); 
+#endif
+                               ppp = tmpptr->next;
+                               if( tmpptr!=localhomtable[i]+j ) 
+                               {
+#if DEBUG
+                                       fprintf( stderr, "freeing %p\n", tmpptr );
+#endif
+                                       free( tmpptr );
+                               }
+                       }
                }
-               while( (tmpptr=tmpptr->next) );
+#if DEBUG
+               fprintf( stderr, "freeing localhomtable[%d]\n", i );
+#endif
+               free( localhomtable[i] );
        }
+#if DEBUG
+       fprintf( stderr, "freeing localhomtable\n" );
+#endif
+       free( localhomtable );
+#if DEBUG
+       fprintf( stderr, "freed\n" );
+#endif
 }
-
 void FreeLocalHomTable( LocalHom **localhomtable, int n ) 
 {
        int i, j;
@@ -4156,9 +5199,10 @@ static char *extractfirstword( char *str )
        return( val );
 }
 
-void phylipout_pointer( FILE *fp, int nseq, int maxlen, char **seq, char **name, int *order )
+void phylipout_pointer( FILE *fp, int nseq, int maxlen, char **seq, char **name, int *order, int namelen )
 {
        int pos, pos2, j;
+       if( namelen == -1 ) namelen = 10;
        pos = 0;
 
        fprintf( fp, " %d %d\n", nseq, maxlen );
@@ -4168,9 +5212,9 @@ void phylipout_pointer( FILE *fp, int nseq, int maxlen, char **seq, char **name,
                for( j=0; j<nseq; j++ )
                {
                        if( pos == 0 )
-                               fprintf( fp, "%-10.10s", extractfirstword( name[order[j]]+1 ) );
+                               fprintf( fp, "%-*.*s", namelen, namelen, extractfirstword( name[order[j]]+1 ) );
                        else
-                               fprintf( fp, "          " );
+                               fprintf( fp, "%-*.*s", namelen, namelen, "" );
 
                        pos2 = pos;
                        while( pos2 < pos+41 && pos2 < maxlen )
@@ -4188,6 +5232,7 @@ void phylipout_pointer( FILE *fp, int nseq, int maxlen, char **seq, char **name,
 void clustalout_pointer( FILE *fp, int nseq, int maxlen, char **seq, char **name, char *mark, char *comment, int *order, int namelen )
 {
        int pos, j;
+       if( namelen == -1 ) namelen = 15;
        pos = 0;
        if( comment == NULL )
                fprintf( fp, "CLUSTAL format alignment by MAFFT (v%s)\n\n", VERSION );
@@ -4413,6 +5458,161 @@ double *loadaamtx( void )
        return( val );
 }
 
+static void tab2space( char *s ) // nen no tame
+{
+       while( *s )
+       {
+               if( *s == '\t' ) *s = ' ';
+               s++;
+       }
+}
+
+static int readasubalignment( char *s, int *t, int *preservegaps )
+{
+       int v = 0;
+       char status = 's';
+       char *pt = s;
+       *preservegaps = 0;
+       tab2space( s );
+       while( *pt )
+       {
+               if( *pt == ' ' )
+               {
+                       status = 's';
+               }
+               else
+               {
+                       if( status == 's' ) 
+                       {
+                               if( *pt == '\n' || *pt == '#' ) break;
+                               status = 'n';
+                               t[v] = atoi( pt );
+                               if( t[v] == 0 )
+                               {
+                                       fprintf( stderr, "Format error? Sequences must be specified as 1, 2, 3...\n" );
+                                       exit( 1 );
+                               }
+                               if( t[v] < 0 ) *preservegaps = 1;
+                               t[v] = abs( t[v] );
+                               t[v] -= 1;
+                               v++;
+                       }
+               }
+               pt++;
+       }
+       t[v] = -1;
+       return( v );
+}
+
+static int countspace( char *s )
+{
+       int v = 0;
+       char status = 's';
+       char *pt = s;
+       tab2space( s );
+       while( *pt )
+       {
+               if( *pt == ' ' )
+               {
+                       status = 's';
+               }
+               else
+               {
+                       if( status == 's' ) 
+                       {
+                               if( *pt == '\n' || *pt == '#' ) break;
+                               v++;
+                               status = 'n';
+                               if( atoi( pt ) == 0 )
+                               {
+                                       fprintf( stderr, "Format error? Sequences should be specified as 1, 2, 3...\n" );
+                                       exit( 1 );
+                               }
+                       }
+               }
+               pt++;
+       }
+       return( v );
+}
+
+
+void readsubalignmentstable( int nseq, int **table, int *preservegaps, int *nsubpt, int *maxmempt )
+{
+       FILE *fp;
+       char *line;
+       int linelen = 1000000;
+       int nmem;
+       int lpos;
+       int i, p;
+       int *tab01;
+
+       line = calloc( linelen, sizeof( char ) );
+       fp = fopen( "_subalignmentstable", "r" );
+       if( !fp )
+       {
+               fprintf( stderr, "Cannot open _subalignmentstable\n" );
+               exit( 1 );
+       }
+       if( table == NULL )
+       {
+               *nsubpt = 0;
+               *maxmempt = 0;
+               while( 1 )
+               {
+                       fgets( line, linelen-1, fp );
+                       if( feof( fp ) ) break;
+                       if( line[strlen(line)-1] != '\n' )
+                       {
+                               fprintf( stderr, "too long line? \n" );
+                               exit( 1 );
+                       }
+                       if( line[0] == '#' ) continue;
+                       if( atoi( line ) == 0 ) continue;
+                       nmem = countspace( line );
+                       if( nmem > *maxmempt ) *maxmempt = nmem;
+                       (*nsubpt)++;
+               }
+       }
+       else
+       {
+               tab01 = calloc( nseq, sizeof( int ) );
+               for( i=0; i<nseq; i++ ) tab01[i] = 0;
+               lpos = 0;
+               while( 1 )
+               {
+                       fgets( line, linelen-1, fp );
+                       if( feof( fp ) ) break;
+                       if( line[strlen(line)-1] != '\n' )
+                       {
+                               fprintf( stderr, "too long line? \n" );
+                               exit( 1 );
+                       }
+                       if( line[0] == '#' ) continue;
+                       if( atoi( line ) == 0 ) continue;
+                       readasubalignment( line, table[lpos], preservegaps+lpos );
+                       for( i=0; (p=table[lpos][i])!=-1; i++ )
+                       {
+                               if( tab01[p] )
+                               {
+                                       fprintf( stderr, "\nSequence %d appears in different groups.\n", p+1 );
+                                       fprintf( stderr, "Hierarchical grouping is not supported.\n\n" );
+                                       exit( 1 );
+                               }
+                               tab01[p] = 1;
+                               if( p > nseq-1 )
+                               {
+                                       fprintf( stderr, "Sequence %d does not exist in the input sequence file.\n", p+1 );
+                                       exit( 1 );
+                               }
+                       }
+                       lpos++;
+               }
+               free( tab01 );
+       }
+       fclose( fp );
+       free( line );
+}
+
 
 void readmccaskill( FILE *fp, RNApair **pairprob, int length )
 {
@@ -4420,7 +5620,7 @@ void readmccaskill( FILE *fp, RNApair **pairprob, int length )
        int *pairnum;
        int i;
        int left, right;
-       float prob;
+       double prob;
        int c;
 
        pairnum = (int *)calloc( length, sizeof( int ) );
@@ -4430,7 +5630,7 @@ void readmccaskill( FILE *fp, RNApair **pairprob, int length )
        {
                if( c != '>' )
                {
-                       fprintf( stderr, "format error in hat4\n" );
+                       fprintf( stderr, "format error in hat4 - 1\n" );
                        exit( 1 );
                }
        }
@@ -4440,17 +5640,17 @@ void readmccaskill( FILE *fp, RNApair **pairprob, int length )
                if( feof( fp ) ) break;
                c = getc( fp );
                ungetc( c, fp );
-               if( c == '>' )
+               if( c == '>' || c == EOF )
                {
                        break;
                }
                fgets( gett, 999, fp );
 //             fprintf( stderr, "gett = %s\n", gett );
-               sscanf( gett, "%d %d %f", &left, &right, &prob );
+               sscanf( gett, "%d %d %lf", &left, &right, &prob );
 
                if( left >= length || right >= length )
                {
-                       fprintf( stderr, "format error in hat4\n" );
+                       fprintf( stderr, "format error in hat4 - 2\n" );
                        exit( 1 );
                }
 
@@ -4598,3 +5798,123 @@ void readpairfoldalign( FILE *fp, char *s1, char *s2, char *aln1, char *aln2, in
        free( maptoseq1 );
        free( maptoseq2 );
 }
+
+int myatoi( char *in )
+{
+       if( in == NULL )
+       {
+               fprintf( stderr, "Error in myatoi()\n" );
+               exit( 1 );
+       }
+       return( atoi( in ) );
+}
+
+double myatof( char *in )
+{
+       if( in == NULL )
+       {
+               fprintf( stderr, "Error in myatof()\n" );
+               exit( 1 );
+       }
+       return( atof( in ) );
+}
+
+void reporterr( const char *str, ... )
+{
+//     static int loglen = 0;
+       va_list args;
+
+       if( gmsg )
+       {
+# if 1  // ato de sakujo
+               static FILE *errtmpfp = NULL;
+               if( errtmpfp == NULL )
+                       errtmpfp = fopen( "maffterr", "w" );
+               else
+                       errtmpfp = fopen( "maffterr", "a" );
+               va_start( args, str );
+               vfprintf( errtmpfp, str, args );
+               va_end( args );
+               fclose( errtmpfp );
+#endif
+
+#if 0
+               char *tmpptr;
+               tmpptr = (char *)realloc( *gmsg, (loglen+10000) * sizeof( char ) );
+               if( tmpptr == NULL )
+               {
+                       fprintf( stderr, "Cannot relloc *gmsg\n" );
+                       exit( 1 );
+               }
+               *gmsg = tmpptr;
+               va_start( args, str );
+               loglen += vsprintf( *gmsg + loglen, str, args );
+               va_end( args );
+
+
+               va_start( args, str );
+               loglen += vsprintf( *gmsg + loglen, str, args );
+               va_end( args );
+               *(*gmsg + loglen) = 0;
+               if( loglen > gmsglen - 100 ) loglen = 0; // tekitou
+#endif
+
+       }
+       else
+       {
+               va_start( args, str );
+               vfprintf( stderr, str, args );
+               va_end( args );
+//             fflush( stderr ); // iru?
+       }
+       return;
+}
+
+
+#ifndef mingw
+void setstacksize(rlim_t kStackSize )
+{
+//     const rlim_t kStackSize = 100 * 1024 * 1024;   // min stack size = 10MB
+       struct rlimit rl;
+       int result;
+       rlim_t originalsize;
+
+       result = getrlimit(RLIMIT_STACK, &rl);
+       if (result == 0)
+       {
+               originalsize = rl.rlim_cur;
+               if (rl.rlim_cur < kStackSize)
+               {
+                       rl.rlim_cur = kStackSize;
+                       reporterr( "stacksize: %d kb->%d kb\n", originalsize/1024, rl.rlim_cur/1024 );
+                       result = setrlimit(RLIMIT_STACK, &rl);
+                       if (result != 0)
+                       {
+                               reporterr( "Warning: Failed to extend stack size. It's ok in most cases but there may be problems in --pileup and --chainedtree.\n" );
+                       }
+               }
+               else
+                       reporterr( "stacksize: %d kb\n", rl.rlim_cur / 1024 );
+       }
+       else
+               reporterr( "Warning: Cannot check stack size.\n" );
+}
+#endif
+
+
+
+
+#if 0
+#include <sys/time.h>
+#include <sys/resource.h>
+
+void use_getrusage(void)
+{
+       struct rusage r;
+       if (getrusage(RUSAGE_SELF, &r) != 0) {
+               /*Failure*/
+       }
+       fprintf(stderr, "\nmaxrss = %ld MB\n", r.ru_maxrss/1000);
+}
+
+#endif