Esempio n. 1
0
static float recalllara( char **mseq1, char **mseq2, int alloclen )
{
	static FILE *fp = NULL;
	static char *ungap1;
	static char *ungap2;
	static char *ori1;
	static char *ori2;
	int res;
	static char com[10000];
	float value;


	if( fp == NULL )
	{
		fp = fopen( "_laraout", "r" );
		if( fp == NULL )
		{
			fprintf( stderr, "Cannot open _laraout\n" );
			exit( 1 );
		}
		ungap1 = AllocateCharVec( alloclen );
		ungap2 = AllocateCharVec( alloclen );
		ori1 = AllocateCharVec( alloclen );
		ori2 = AllocateCharVec( alloclen );
	}


	strcpy( ori1, *mseq1 );
	strcpy( ori2, *mseq2 );

	fgets( com, 999, fp );
	myfgets( com, 9999, fp );
	strcpy( *mseq1, com );
	myfgets( com, 9999, fp );
	strcpy( *mseq2, com );

	gappick0( ungap1, *mseq1 );
	gappick0( ungap2, *mseq2 );
	t2u( ungap1 );
	t2u( ungap2 );

	if( strcmp( ungap1, ori1 ) || strcmp( ungap2, ori2 ) )
	{
		fprintf( stderr, "SEQUENCE CHANGED!!\n" );
		fprintf( stderr, "*mseq1  = %s\n", *mseq1 );
		fprintf( stderr, "ungap1  = %s\n", ungap1 );
		fprintf( stderr, "ori1    = %s\n", ori1 );
		fprintf( stderr, "*mseq2  = %s\n", *mseq2 );
		fprintf( stderr, "ungap2  = %s\n", ungap2 );
		fprintf( stderr, "ori2    = %s\n", ori2 );
		exit( 1 );
	}

	value = (float)naivepairscore11( *mseq1, *mseq2, penalty );

//	fclose( fp ); // saigo dake yatta houga yoi.

	return( value );
}
Esempio n. 2
0
File: rna.c Progetto: Unode/ext_apps
void foldrna( int nseq1, int nseq2, char **seq1, char **seq2, double *eff1, double *eff2, RNApair ***grouprna1, RNApair ***grouprna2, float **impmtx, int *gapmap1, int *gapmap2, RNApair *additionalpair )
{
    int i, j;
//	int ui, uj;
//	int uiup, ujup;
    int uido, ujdo;
    static TLS char **useq1, **useq2;
    static TLS char **oseq1, **oseq2, **oseq1r, **oseq2r, *odir1, *odir2;
    static TLS RNApair **pairprob1, **pairprob2;
    static TLS RNApair *pairpt1, *pairpt2;
    int lgth1 = strlen( seq1[0] );
    int lgth2 = strlen( seq2[0] );
    static TLS float **impmtx2;
    static TLS float **map;
//	double lenfac;
    float prob;
    int **sgapmap1, **sgapmap2;
    char *nogapdum;
    float **tbppmtx;


//	fprintf( stderr, "nseq1=%d, lgth1=%d\n", nseq1, lgth1 );
    useq1 = AllocateCharMtx( nseq1, lgth1+10 );
    useq2 = AllocateCharMtx( nseq2, lgth2+10 );
    oseq1 = AllocateCharMtx( nseq1, lgth1+10 );
    oseq2 = AllocateCharMtx( nseq2, lgth2+10 );
    oseq1r = AllocateCharMtx( nseq1, lgth1+10 );
    oseq2r = AllocateCharMtx( nseq2, lgth2+10 );
    odir1 = AllocateCharVec( lgth1+10 );
    odir2 = AllocateCharVec( lgth2+10 );
    sgapmap1 = AllocateIntMtx( nseq1, lgth1+1 );
    sgapmap2 = AllocateIntMtx( nseq2, lgth2+1 );
    nogapdum = AllocateCharVec( MAX( lgth1, lgth2 ) );
    pairprob1 = (RNApair **)calloc( lgth1, sizeof( RNApair *) );
    pairprob2 = (RNApair **)calloc( lgth2, sizeof( RNApair *) );
    map = AllocateFloatMtx( lgth1, lgth2 );
    impmtx2 = AllocateFloatMtx( lgth1, lgth2 );
    tbppmtx = AllocateFloatMtx( lgth1, lgth2 );

    for( i=0; i<nseq1; i++ ) strcpy( useq1[i], seq1[i] );
    for( i=0; i<nseq2; i++ ) strcpy( useq2[i], seq2[i] );
    for( i=0; i<nseq1; i++ ) strcpy( oseq1[i], seq1[i] );
    for( i=0; i<nseq2; i++ ) strcpy( oseq2[i], seq2[i] );

    for( i=0; i<nseq1; i++ ) commongappick_record( 1, useq1+i, sgapmap1[i] );
    for( i=0; i<nseq2; i++ ) commongappick_record( 1, useq2+i, sgapmap2[i] );

    for( i=0; i<lgth1; i++ )
    {
        pairprob1[i] = (RNApair *)calloc( 1, sizeof( RNApair ) );
        pairprob1[i][0].bestpos = -1;
        pairprob1[i][0].bestscore = -1;
    }
    for( i=0; i<lgth2; i++ )
    {
        pairprob2[i] = (RNApair *)calloc( 1, sizeof( RNApair ) );
        pairprob2[i][0].bestpos = -1;
        pairprob2[i][0].bestscore = -1;
    }

    utot( nseq1, lgth1, oseq1 );
    utot( nseq2, lgth2, oseq2 );

//	fprintf( stderr, "folding group1\n" );
//	rnalocal( oseq1, useq1, eff1, eff1, nseq1, nseq1, lgth1+10, pair1 );

    /* base-pairing probability of group 1 */
    if( rnaprediction == 'r' )
        rnaalifoldcall( oseq1, nseq1, pairprob1 );
    else
        mccaskillextract( oseq1, useq1, nseq1, pairprob1, grouprna1, sgapmap1, eff1 );


//	fprintf( stderr, "folding group2\n" );
//	rnalocal( oseq2, useq2, eff2, eff2, nseq2, nseq2, lgth2+10, pair2 );

    /* base-pairing probability of group 2 */
    if( rnaprediction == 'r' )
        rnaalifoldcall( oseq2, nseq2, pairprob2 );
    else
        mccaskillextract( oseq2, useq2, nseq2, pairprob2, grouprna2, sgapmap2, eff2 );



#if 0
    makerseq( oseq1, oseq1r, odir1, pairprob1, nseq1, lgth1 );
    makerseq( oseq2, oseq2r, odir2, pairprob2, nseq2, lgth2 );

    fprintf( stderr, "%s\n", odir2 );

    for( i=0; i<nseq1; i++ )
    {
        fprintf( stdout, ">ori\n%s\n", oseq1[0] );
        fprintf( stdout, ">rev\n%s\n", oseq1r[0] );
    }
#endif

    /* similarity score */
    Lalignmm_hmout( oseq1, oseq2, eff1, eff2, nseq1, nseq2, 10000, NULL, NULL, NULL, NULL, map );

    if( 1 )
    {
        if( RNAscoremtx == 'n' )
        {
            for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
                {
//				impmtx2[i][j] = osoiaveragescore( nseq1, nseq2, oseq1, oseq2, eff1, eff2, i, j ) * consweight_multi;
                    impmtx2[i][j] = 0.0;
                }
        }
        else if( RNAscoremtx == 'r' )
        {
            for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
                {
                    tbppmtx[i][j] = 1.0;
                    impmtx2[i][j] = 0.0;
                }
            for( i=0; i<lgth1; i++ ) for( pairpt1=pairprob1[i]; pairpt1->bestpos!=-1; pairpt1++ )
                {
                    for( j=0; j<lgth2; j++ ) for( pairpt2=pairprob2[j]; pairpt2->bestpos!=-1; pairpt2++ )
                        {
                            uido = pairpt1->bestpos;
                            ujdo = pairpt2->bestpos;
                            prob = pairpt1->bestscore * pairpt2->bestscore;
                            if( uido > -1  && ujdo > -1 )
                            {
                                if( uido > i && j > ujdo )
                                {
                                    impmtx2[i][j] += prob * pairedribosumscore53( nseq1, nseq2, oseq1, oseq2, eff1, eff2, i, j, uido, ujdo ) * consweight_multi;
                                    tbppmtx[i][j] -= prob;
                                }
                                else if( i < uido && j < ujdo )
                                {
                                    impmtx2[i][j] += prob * pairedribosumscore35( nseq1, nseq2, oseq1, oseq2, eff1, eff2, i, j, uido, ujdo ) * consweight_multi;
                                    tbppmtx[i][j] -= prob;
                                }
                            }
                        }
                }


            for( i=0; i<lgth1; i++ )
            {
                for( j=0; j<lgth2; j++ )
                {
                    impmtx2[i][j] += tbppmtx[i][j] * singleribosumscore( nseq1, nseq2, oseq1, oseq2, eff1, eff2, i, j ) * consweight_multi;
                }
            }
        }


        /* four-way consistency */

        for( i=0; i<lgth1; i++ ) for( pairpt1=pairprob1[i]; pairpt1->bestpos!=-1; pairpt1++ )
            {

//			if( pairprob1[i] == NULL ) continue;

                for( j=0; j<lgth2; j++ ) for( pairpt2=pairprob2[j]; pairpt2->bestpos!=-1; pairpt2++ )
                    {
//				fprintf( stderr, "i=%d, j=%d, pn1=%d, pn2=%d\n", i, j, pairpt1-pairprob1[i], pairpt2-pairprob2[j] );
//				if( pairprob2[j] == NULL ) continue;

                        uido = pairpt1->bestpos;
                        ujdo = pairpt2->bestpos;
                        prob = pairpt1->bestscore * pairpt2->bestscore;
//				prob = 1.0;
//				fprintf( stderr, "i=%d->uido=%d, j=%d->ujdo=%d\n", i, uido, j, ujdo );

//				fprintf( stderr, "impmtx2[%d][%d] = %f\n", i, j, impmtx2[i][j] );

//				if( i < uido && j > ujdo ) continue;
//				if( i > uido && j < ujdo ) continue;


//				posdistj = abs( ujdo-j );

//				if( uido > -1 && ujdo > -1 )
                        if( uido > -1 && ujdo > -1 && ( ( i > uido && j > ujdo ) || ( i < uido && j < ujdo ) ) )
                        {
                            {
                                impmtx2[i][j] += MAX( 0, map[uido][ujdo] ) * consweight_rna * 600 * prob; // osoi
                            }
                        }

                    }
            }
        for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
            {
                impmtx[i][j] += impmtx2[i][j];
//			fprintf( stderr, "fastathreshold=%f, consweight_multi=%f, consweight_rna=%f\n", fastathreshold, consweight_multi, consweight_rna );
//			impmtx[i][j] *= 0.5;
            }

//		impmtx[0][0] += 10000.0;
//		impmtx[lgth1-1][lgth2-1] += 10000.0;



#if 0
        fprintf( stdout, "#impmtx2 = \n" );
        for( i=0; i<lgth1; i++ )
        {
            for( j=0; j<lgth2; j++ )
            {
                fprintf( stdout, "%d %d %f\n", i, j, impmtx2[i][j] );
            }
            fprintf( stdout, "\n" );
        }
        exit( 1 );
#endif
    }

    FreeCharMtx( useq1 );
    FreeCharMtx( useq2 );
    FreeCharMtx( oseq1 );
    FreeCharMtx( oseq2 );
    FreeCharMtx( oseq1r );
    FreeCharMtx( oseq2r );
    free( odir1 );
    free( odir2 );
    FreeFloatMtx( impmtx2 );
    FreeFloatMtx( map );
    FreeIntMtx( sgapmap1 );
    FreeIntMtx( sgapmap2 );
    FreeFloatMtx( tbppmtx );

    for( i=0; i<lgth1; i++ ) free( pairprob1[i] );
    for( i=0; i<lgth2; i++ ) free( pairprob2[i] );
    free( pairprob1 );
    free( pairprob2 );
}
Esempio n. 3
0
int main( int argc, char **argv )
{
	int i, j;
	FILE *fp, *infp;
	char **seq;
	int *grpseq;
	char *tmpseq;
	int  **pointt;
	static char name[M][B];
	static int nlen[M];
	double **mtx;
	double **mtx2;
	double score, score0;
	static short *table1;
	char b[B];

	arguments( argc, argv );

	if( inputfile )
	{
		infp = fopen( inputfile, "r" );
		if( !infp )
		{
			fprintf( stderr, "Cannot open %s\n", inputfile );
			exit( 1 );
		}
	}
	else
		infp = stdin;

#if 0
	PreRead( stdin, &njob, &nlenmax );
#else
	getnumlen( infp );
#endif
	rewind( infp );
	if( njob < 2 )
	{
		fprintf( stderr, "At least 2 sequences should be input!\n"
						 "Only %d sequence found.\n", njob );
		exit( 1 );
	}

	tmpseq = AllocateCharVec( nlenmax+1 );
	seq = AllocateCharMtx( njob, nlenmax+1 );
	grpseq = AllocateIntVec( nlenmax+1 );
	pointt = AllocateIntMtx( njob, nlenmax+1 );
	mtx = AllocateDoubleMtx( njob, njob );
	mtx2 = AllocateDoubleMtx( njob, njob );
	pamN = NOTSPECIFIED;

#if 0
	FRead( infp, name, nlen, seq );
#else
	readData( infp, name, nlen, seq );
#endif

	fclose( infp );

	constants( njob, seq );

	if( dorp == 'd' ) tsize = (int)pow( 4, 6 );
	else              tsize = (int)pow( 6, 6 );

	maxl = 0;
	for( i=0; i<njob; i++ ) 
	{
		gappick0( tmpseq, seq[i] );
		nlen[i] = strlen( tmpseq );
		if( nlen[i] < 6 )
		{
			fprintf( stderr, "Seq %d, too short, %d characters\n", i+1, nlen[i] );
			exit( 1 );
		}
		if( nlen[i] > maxl ) maxl = nlen[i];
		if( dorp == 'd' ) /* nuc */
		{
			seq_grp_nuc( grpseq, tmpseq );
			makepointtable_nuc( pointt[i], grpseq );
		}
		else                 /* amino */
		{
			seq_grp( grpseq, tmpseq );
			makepointtable( pointt[i], grpseq );
		}
	}
	for( i=0; i<njob; i++ )
	{
		table1 = (short *)calloc( tsize, sizeof( short ) );
		if( !table1 ) ErrorExit( "Cannot allocate table1\n" );
		if( i % 10 == 0 )
		{
			fprintf( stderr, "%4d / %4d\r", i+1, njob );
		}
		makecompositiontable_p( table1, pointt[i] );

		for( j=i; j<njob; j++ ) 
		{
			score = (double)commonsextet_p( table1, pointt[j] );
			mtx[i][j] = score;
		} 
		free( table1 );
	}
	for( i=0; i<njob; i++ )
	{
		score0 = mtx[i][i];
		for( j=0; j<njob; j++ ) 
			mtx2[i][j] = ( score0 - mtx[MIN(i,j)][MAX(i,j)] ) / score0 * 3.0;
	}
	for( i=0; i<njob-1; i++ ) for( j=i+1; j<njob; j++ ) 
	{
#if TEST
                double jscore;
                jscore = mtx[i][j] / ( MIN( strlen( seq[i] ), strlen( seq[j] ) ) - 2 );
                fprintf( stdout, "jscore = %f\n", jscore );

		fprintf( stdout, "mtx2[%d][%d] = %f, mtx2[%d][%d] = %f\n", i, j, mtx2[i][j], j, i, mtx2[j][i] );
#endif
		mtx2[i][j] = MIN( mtx2[i][j], mtx2[j][i] );
#if TEST
		fprintf( stdout, "sonokekka mtx2[%d][%d] %f\n", i, j, mtx2[i][j] );
#endif
	}

	if( disopt )
	{
		for( i=0; i<njob; i++ ) 
		{
			sprintf( b, "=lgth = %04d", nlen[i] );
			strins( b, name[i] );
		}
	}
		
	fp = fopen( "hat2", "w" );
	if( !fp ) ErrorExit( "Cannot open hat2." );
	WriteHat2( fp, njob, name, mtx2 );
	fclose( fp );

	fprintf( stderr, "\n" );
	SHOWVERSION;
	exit( 0 );
}
Esempio n. 4
0
int main( int argc, char *argv[] )
{
	FILE *infp;
	FILE *difp;
	int nlenmin;
	char **name;
	char **seq;
	char *tmpseq;
	char line[100];
	int *nlen;
	int i;

	arguments( argc, argv );

	if( inputfile )
	{
		infp = fopen( inputfile, "r" );
		if( !infp )
		{
			fprintf( stderr, "Cannot open %s\n", inputfile );
			exit( 1 );
		}
	}
	else
		infp = stdin;

	if( directionfile )
	{
		difp = fopen( directionfile, "r" );
		if( !difp )
		{
			fprintf( stderr, "Cannot open %s\n", directionfile );
			exit( 1 );
		}
	}
	else
	{
		fprintf( stderr, "Give directionfile!\n" );
	}


	dorp = NOTSPECIFIED;
	getnumlen_casepreserve( infp, &nlenmin );

	fprintf( stderr, "%d x %d - %d %c\n", njob, nlenmax, nlenmin, dorp );

	seq = AllocateCharMtx( njob, nlenmax+1 );
	tmpseq = AllocateCharVec( MAX( B, nlenmax )+1 );
	name = AllocateCharMtx( njob, B+1 );
	nlen = AllocateIntVec( njob );

	readData_pointer_casepreserve( infp, name, nlen, seq );

	for( i=0; i<njob; i++ )
	{
		fgets( line, 99, difp );
		if( line[0] != '_' )
		{
			fprintf( stderr, "Format error!\n" );
			exit( 1 );
		}
		if( line[1] == 'R' )
		{
			sreverse( tmpseq, seq[i] );
			strcpy( seq[i], tmpseq );

			strncpy( tmpseq, name[i]+1, B-3 );
			tmpseq[B-3] = 0;
			strcpy( name[i]+1, "_R_" );
			strcpy( name[i]+4, tmpseq );
		}
		else if( line[1] == 'F' )
		{
			;
		}
		else
		{
			fprintf( stderr, "Format error!\n" );
			exit( 1 );
		}
	}


	for( i=0; i<njob; i++ )
	{
		fprintf( stdout, ">%s\n", name[i]+1 );
		fprintf( stdout, "%s\n", seq[i] );
	}

	free( nlen );
	FreeCharMtx( seq );
	FreeCharMtx( name );
	free( tmpseq );

	return( 0 );
}
Esempio n. 5
0
static void pairalign( char **name, int nlen[M], char **seq, double *effarr, int alloclen )
{
	FILE *tmpfp;
	static char dumm1[B], dumm0[B];
	int i, j;
	char *res;
	FILE *hat3p;
	static double *effarr1 = NULL;
	static double *effarr2 = NULL;
	static char **pseq;
	LocalHom **localhomtable, *tmpptr;
	float pscore = 0.0; // by D.Mathog, aguess
	char *aseq = NULL; // by D.Mathog
	char **usedseqs = NULL; // by D.Mathog
	char **usednames = NULL; // by D.Mathog
	int nused;
	double tsuyosa;

	tsuyosa = (double)nhomologs * (nhomologs-1) / njob * TSUYOSAFACTOR;
	fprintf( stderr, "tsuyosa = %f\n", tsuyosa );
	localhomtable = (LocalHom **)calloc( njob, sizeof( LocalHom *) );
	for( i=0; i<njob; i++)
	{
		localhomtable[i] = (LocalHom *)calloc( njob, sizeof( LocalHom ) );
		for( j=0; j<njob; j++)
		{
			localhomtable[i][j].start1 = -1;
			localhomtable[i][j].end1 = -1;
			localhomtable[i][j].start2 = -1; 
			localhomtable[i][j].end2 = -1; 
			localhomtable[i][j].opt = -1.0;
			localhomtable[i][j].next = NULL;
		}
	}

	if( effarr1 == NULL ) 
	{
		effarr1 = AllocateDoubleVec( njob );
		effarr2 = AllocateDoubleVec( njob );
		pseq = AllocateCharMtx( 2, nlenmax*9+1 );
		aseq = AllocateCharVec( nlenmax*9+1 );
		usedseqs = AllocateCharMtx( njob, nlenmax*9+1 );
		usednames = AllocateCharMtx( njob, B );
#if 0
#else
#endif
	}

#if 0
	fprintf( stderr, "##### fftwinsize = %d, fftthreshold = %d\n", fftWinSize, fftThreshold );
#endif

#if 0
	for( i=0; i<njob; i++ )
		fprintf( stderr, "TBFAST effarr[%d] = %f\n", i, effarr[i] );
#endif


//	writePre( njob, name, nlen, aseq, 0 );

	fprintf( stderr, "opening %s\n", pairfile  );
	tmpfp = fopen( pairfile, "r" );
	if( !tmpfp )
	{
		fprintf( stderr, "Cannot open %s\n", pairfile );
		exit( 1 );
	}
	searchKUorWA( tmpfp );
	hat3p = fopen( "hat3", "w" );
	if( !hat3p ) ErrorExit( "Cannot open hat3." );
	nused = 0;
	while( 1 )
	{
		res = fgets( dumm0, B-1, tmpfp );
		strip( dumm0 );
		if( res == NULL )
		{
			break;
		}
		load1SeqWithoutName_new( tmpfp, pseq[0] );
		gappick0( aseq, pseq[0] );
		i =  searchused( aseq, usedseqs, nused );
		if( i == -1 )
		{
			strcpy( usednames[nused], dumm0+1 );
			strcpy( usedseqs[nused], aseq );
			i = nused;
			nused++;
		}
		fprintf( stderr, "i = %d\n", i );

		res = fgets( dumm1, B-1, tmpfp );
		strip( dumm1 );
		if( res == NULL )
		{
			fprintf( stderr, "ERROR: The number of sequences in %s must be even.\n", pairfile );
			exit( 1 );
		}
		load1SeqWithoutName_new( tmpfp, pseq[1] );
		gappick0( aseq, pseq[1] );
		j =  searchused( aseq, usedseqs, nused );
		if( j == -1 )
		{
			strcpy( usednames[nused], dumm1+1 );
			strcpy( usedseqs[nused], aseq );
			j = nused;
			nused++;
		}
		fprintf( stderr, "j = %d\n", j );

		if( strlen( pseq[0] ) != strlen( pseq[1] ) )
		{
			fprintf( stderr, "Not aligned,  %s - %s\n", dumm0, dumm1 );
			exit( 1 );
		}


		fprintf( stderr, "adding %d-%d\n", i, j );
		putlocalhom2( pseq[0], pseq[1], localhomtable[i]+j, 0, 0, (int)pscore, strlen( pseq[0] ) );
		for( tmpptr=localhomtable[i]+j; tmpptr; tmpptr=tmpptr->next )
		{
			if( tmpptr->opt == -1.0 ) continue;
			fprintf( hat3p, "%d %d %d %6.3f %d %d %d %d %p\n", i, j, tmpptr->overlapaa, tmpptr->opt * tsuyosa, tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, (void *)tmpptr->next ); 
		}
	}
	fclose( tmpfp );
	fclose( hat3p );

	for( i=0; i<nused; i++ )
		fprintf( stdout, ">%s\n%s\n", usednames[i], usedseqs[i] );


#if 0
	fprintf( stderr, "##### writing hat3\n" );
	hat3p = fopen( "hat3", "w" );
	if( !hat3p ) ErrorExit( "Cannot open hat3." );
	ilim = njob-1;	
	for( i=0; i<ilim; i++ ) 
	{
		for( j=i+1; j<njob; j++ )
		{
			for( tmpptr=localhomtable[i]+j; tmpptr; tmpptr=tmpptr->next )
			{
				if( tmpptr->opt == -1.0 ) continue;
				fprintf( hat3p, "%d %d %d %6.3f %d %d %d %d %p\n", i, j, tmpptr->overlapaa, tmpptr->opt * tsuyosa, tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->next ); 
			}
		}
	}
	fclose( hat3p );
#endif
#if DEBUG
	fprintf( stderr, "calling FreeLocalHomTable\n" );
#endif
	FreeLocalHomTable( localhomtable, njob );
#if DEBUG
	fprintf( stderr, "done. FreeLocalHomTable\n" );
#endif
}
Esempio n. 6
0
static void pairalign( char name[M][B], int nlen[M], char **seq, char **aseq, char **mseq1, char **mseq2, double *effarr, int alloclen )
{
	int i, j, ilim;
	int clus1, clus2;
	int off1, off2;
	float pscore = 0.0; // by D.Mathog
	static char *indication1, *indication2;
	FILE *hat2p, *hat3p;
	static double **distancemtx;
	static double *effarr1 = NULL;
	static double *effarr2 = NULL;
	char *pt;
	char *hat2file = "hat2";
	LocalHom **localhomtable, *tmpptr;
	static char **pair;
	int intdum;
	double bunbo;

	localhomtable = (LocalHom **)calloc( njob, sizeof( LocalHom *) );
	for( i=0; i<njob; i++)
	{
		localhomtable[i] = (LocalHom *)calloc( njob, sizeof( LocalHom ) );
		for( j=0; j<njob; j++)
		{
			localhomtable[i][j].start1 = -1;
			localhomtable[i][j].end1 = -1;
			localhomtable[i][j].start2 = -1; 
			localhomtable[i][j].end2 = -1; 
			localhomtable[i][j].opt = -1.0;
			localhomtable[i][j].next = NULL;
			localhomtable[i][j].nokori = 0;
		}
	}

	if( effarr1 == NULL ) 
	{
		distancemtx = AllocateDoubleMtx( njob, njob );
		effarr1 = AllocateDoubleVec( njob );
		effarr2 = AllocateDoubleVec( njob );
		indication1 = AllocateCharVec( 150 );
		indication2 = AllocateCharVec( 150 );
#if 0
#else
		pair = AllocateCharMtx( njob, njob );
#endif
	}

#if 0
	fprintf( stderr, "##### fftwinsize = %d, fftthreshold = %d\n", fftWinSize, fftThreshold );
#endif

#if 0
	for( i=0; i<njob; i++ )
		fprintf( stderr, "TBFAST effarr[%d] = %f\n", i, effarr[i] );
#endif


//	writePre( njob, name, nlen, aseq, 0 );

	for( i=0; i<njob; i++ ) for( j=0; j<njob; j++ ) pair[i][j] = 0;
	for( i=0; i<njob; i++ ) pair[i][i] = 1;

	if( alg == 'H' )
	{
		fprintf( stderr, "Calling FOLDALIGN with option '%s'\n", foldalignopt );
		callfoldalign( njob, seq );
		fprintf( stderr, "done.\n" );
	}
	if( alg == 'B' )
	{
		fprintf( stderr, "Calling LARA\n" );
		calllara( njob, seq, "" );
		fprintf( stderr, "done.\n" );
	}
	if( alg == 'T' )
	{
		fprintf( stderr, "Calling SLARA\n" );
		calllara( njob, seq, "-s" );
		fprintf( stderr, "done.\n" );
	}

	ilim = njob - 1;
	for( i=0; i<ilim; i++ ) 
	{
		fprintf( stderr, "% 5d / %d\r", i, njob );
		for( j=i+1; j<njob; j++ )
		{

			if( strlen( seq[i] ) == 0 || strlen( seq[j] ) == 0 )
			{
				distancemtx[i][j] = pscore;
				continue;
			}

			strcpy( aseq[i], seq[i] );
			strcpy( aseq[j], seq[j] );
			clus1 = conjuctionfortbfast( pair, i, aseq, mseq1, effarr1, effarr, indication1 );
			clus2 = conjuctionfortbfast( pair, j, aseq, mseq2, effarr2, effarr, indication2 );
	//		fprintf( stderr, "mseq1 = %s\n", mseq1[0] );
	//		fprintf( stderr, "mseq2 = %s\n", mseq2[0] );
	
#if 0
			fprintf( stderr, "group1 = %.66s", indication1 );
			fprintf( stderr, "\n" );
			fprintf( stderr, "group2 = %.66s", indication2 );
			fprintf( stderr, "\n" );
#endif
	//		for( l=0; l<clus1; l++ ) fprintf( stderr, "## STEP-eff for mseq1-%d %f\n", l, effarr1[l] );
	
#if 1
			if( use_fft )
			{
				pscore = Falign( mseq1, mseq2, effarr1, effarr2, clus1, clus2, alloclen, &intdum, NULL, 0, NULL );
				off1 = off2 = 0;
			}
			else
#endif
			{
				switch( alg )
				{
					case( 'a' ):
						pscore = Aalign( mseq1, mseq2, effarr1, effarr2, clus1, clus2, alloclen );
						off1 = off2 = 0;
						break;
					case( 'A' ):
						pscore = G__align11( mseq1, mseq2, alloclen, NULL, 0, NULL );
						off1 = off2 = 0;
						break;
#if 0
					case( 'V' ):
						pscore = VAalign11( mseq1, mseq2, alloclen, &off1, &off2, localhomtable[i]+j );
						fprintf( stderr, "i,j = %d,%d, score = %f\n", i,j, pscore );
						break;
					case( 'S' ):
						fprintf( stderr, "aligning %d-%d\n", i, j );
						pscore = suboptalign11( mseq1, mseq2, alloclen, &off1, &off2, localhomtable[i]+j );
						fprintf( stderr, "i,j = %d,%d, score = %f\n", i,j, pscore );
						break;
#endif
					case( 'N' ):
						pscore = genL__align11( mseq1, mseq2, alloclen, &off1, &off2 );
//						fprintf( stderr, "pscore = %f\n", pscore );
						break;
					case( 'L' ):
						pscore = L__align11( mseq1, mseq2, alloclen, &off1, &off2 );
//						fprintf( stderr, "pscore (1) = %f\n", pscore );
//						pscore = (float)naivepairscore11( *mseq1, *mseq2, penalty ); // nennnotame
//						fprintf( stderr, "pscore (2) = %f\n\n", pscore );
						break;
					case( 'H' ):
						pscore = recallpairfoldalign( mseq1, mseq2, i, j, &off1, &off2, alloclen );
						break;
					case( 'B' ):
					case( 'T' ):
						pscore = recalllara( mseq1, mseq2, alloclen );
						off1 = off2 = 0;
//						fprintf( stderr, "lara, pscore = %f\n", pscore );
						break;
					case( 's' ):
						pscore = callmxscarna( mseq1, mseq2, alloclen );
						off1 = off2 = 0;
//						fprintf( stderr, "scarna, pscore = %f\n", pscore );
						break;
					case( 'M' ):
//						pscore = MSalign11( mseq1, mseq2, effarr1, effarr2, clus1, clus2, alloclen, NULL, NULL, NULL, NULL );
						pscore = MSalign11( mseq1, mseq2, alloclen );
//						fprintf( stderr, "pscore = %f\n", pscore );
						break;
						ErrorExit( "ERROR IN SOURCE FILE" );
				}
			}
			distancemtx[i][j] = pscore;
#if SCOREOUT
			fprintf( stderr, "score = %10.2f (%d,%d)\n", pscore, i, j );
#endif
//			fprintf( stderr, "pslocal = %d\n", pslocal );
//			offset = makelocal( *mseq1, *mseq2, pslocal );
#if 0
			fprintf( stderr, "off1 = %d, off2 = %d\n", off1, off2 );
			fprintf( stderr, ">%d\n%s\n>%d\n%s\n>\n", i, mseq1[0], j, mseq2[0] );
#endif

//			putlocalhom2( mseq1[0], mseq2[0], localhomtable[i]+j, countamino( *mseq1, off1 ), countamino( *mseq2, off2 ), pscore, strlen( mseq1[0] ) );
//			fprintf( stderr, "pscore = %f\n", pscore );
			if( alg == 'H' )
//			if( alg == 'H' || alg == 's' || alg == 'B' ) // next version
				putlocalhom_ext( mseq1[0], mseq2[0], localhomtable[i]+j, off1, off2, (int)pscore, strlen( mseq1[0] ) );
			else if( alg != 'S' && alg != 'V' )
				putlocalhom2( mseq1[0], mseq2[0], localhomtable[i]+j, off1, off2, (int)pscore, strlen( mseq1[0] ) );
		}
	}
	for( i=0; i<njob; i++ )
	{
		pscore = 0.0;
		for( pt=seq[i]; *pt; pt++ )
			pscore += amino_dis[(int)*pt][(int)*pt];
		distancemtx[i][i] = pscore;

	}

	ilim = njob-1;	
	for( i=0; i<ilim; i++ )
	{
		for( j=i+1; j<njob; j++ )
		{
			bunbo = MIN( distancemtx[i][i], distancemtx[j][j] );
			if( bunbo == 0.0 )
				distancemtx[i][j] = 2.0;
			else
				distancemtx[i][j] = ( 1.0 - distancemtx[i][j] / bunbo ) * 2.0;
		}
	}

	hat2p = fopen( hat2file, "w" );
	if( !hat2p ) ErrorExit( "Cannot open hat2." );
	WriteHat2( hat2p, njob, name, distancemtx );
	fclose( hat2p );

	fprintf( stderr, "##### writing hat3\n" );
	hat3p = fopen( "hat3", "w" );
	if( !hat3p ) ErrorExit( "Cannot open hat3." );
	ilim = njob-1;	
	for( i=0; i<ilim; i++ ) 
	{
		for( j=i+1; j<njob; j++ )
		{
			for( tmpptr=localhomtable[i]+j; tmpptr; tmpptr=tmpptr->next )
			{
				if( tmpptr->opt == -1.0 ) continue;
				fprintf( hat3p, "%d %d %d %7.5f %d %d %d %d %p\n", i, j, tmpptr->overlapaa, tmpptr->opt, tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, (void *)tmpptr->next ); 
			}
		}
	}
	fclose( hat3p );
#if DEBUG
	fprintf( stderr, "calling FreeLocalHomTable\n" );
#endif
	FreeLocalHomTable( localhomtable, njob );
#if DEBUG
	fprintf( stderr, "done. FreeLocalHomTable\n" );
#endif
}
Esempio n. 7
0
void GroupAlign( int nseq1, int nseq2, char **name, int *nlen, char **seq, char **aseq, char **mseq1, char **mseq2, int ***topol, double **len, double *eff, int alloclen )
{
	int i;
	int clus1, clus2;
	int s1, s2;
	float pscore;
	static char **name1, **name2;
	double *effarr = eff;
	double *effarr1 = NULL;
	double *effarr2 = NULL;
	static char *indication1, *indication2;
	float dumfl = 0.0;
	int intdum;
#if DEBUG
	double time1, time2;
#endif


//	fprintf( stderr, "in GroupAlign fftWinSize   = %d\n", fftWinSize );
//	fprintf( stderr, "in GroupAlign fftThreshold = %d\n", fftThreshold );

	if( effarr1 == NULL ) 
	{
		name1 = AllocateCharMtx( nseq1, B );
		name2 = AllocateCharMtx( nseq2, B );
		indication1 = AllocateCharVec( 150 );
		indication2 = AllocateCharVec( 150 );
		effarr1 = AllocateDoubleVec( njob );
		effarr2 = AllocateDoubleVec( njob );
#if 0
#else
#endif
	}

	for( i=0; i<njob; i++ ) strcpy( aseq[i], seq[i] );


		
	s1 = 0;
	s2 = nseq1;
//	fprintf( stdout, "nseq1 = %d\n", nseq1 );



	clus1 = conjuctionforgaln( 0, nseq1, aseq, mseq1, effarr1, effarr, name, name1, indication1 );
	clus2 = conjuctionforgaln( nseq1, njob, aseq, mseq2, effarr2, effarr, name, name2, indication2 );
/*
	fprintf( stderr, "before align all\n" );
	display( aseq, njob );
	fprintf( stderr, "\n" );
	fprintf( stderr, "before align 1 %s \n", indication1 );
	display( mseq1, clus1 );
	fprintf( stderr, "\n" );
	fprintf( stderr, "before align 2 %s \n", indication2 );
	display( mseq2, clus2 );
	fprintf( stderr, "\n" );
*/

	commongappick( nseq1, mseq1 );
	commongappick( nseq2, mseq2 );


#if DEBUG
	time1 = getrusage_sec();
	fprintf( stderr, "entering Falign\n" );
#endif
	if( use_fft )
	{
		if( alg == 'M' )
			pscore = Falign_udpari_long( NULL, NULL, n_dis_consweight_multi, mseq1, mseq2, effarr1, effarr2, NULL, NULL, clus1, clus2, alloclen, &intdum );
		else
			pscore = Falign( NULL, NULL, n_dis_consweight_multi, mseq1, mseq2, effarr1, effarr2, NULL, NULL, clus1, clus2, alloclen, &intdum, NULL, 0, NULL );
	}
	else
	{
		if( alg == 'M' )
			pscore = MSalignmm( n_dis_consweight_multi, mseq1, mseq2, effarr1, effarr2, clus1, clus2, alloclen, NULL, NULL, NULL, NULL, NULL, 0, NULL, outgap, outgap );
		else
			pscore = A__align( n_dis_consweight_multi, mseq1, mseq2, effarr1, effarr2, clus1, clus2, alloclen, NULL, &dumfl, NULL, NULL, NULL, NULL, NULL, 0, NULL, outgap, outgap );
	}
#if DEBUG
		time2 = getrusage_sec();
		fprintf( stdout, "### %d - %d,  %f\n", clus1, clus2, time2-time1 );
		fflush( stdout );
#endif

	
/*
	fprintf( stderr, "after align 1 %s \n", indication1 );
	display( mseq1, clus1 );
	fprintf( stderr, "\n" );
	fprintf( stderr, "after align 2 %s \n", indication2 );
	display( mseq2, clus2 );
	fprintf( stderr, "\n" );
*/

	fprintf( stderr, "group-to-group %s /%s     %f\n", indication1, indication2, pscore );
	if( disp ) display( aseq, njob );
	fprintf( stderr, "\n" );

/*
	trap = fopen( "pre", "r+" );
	if( !trap ) ErrorExit( 1 );
	WriteGapFill( trap, njob, name, nlen, aseq );
	fclose( trap );
	fprintf( stdout, "nseq1 = %d\n", nseq1 );
*/
}
Esempio n. 8
0
void part_imp_match_init( float *imp, int clus1, int clus2, int lgth1, int lgth2, char **seq1, char **seq2, double *eff1, double *eff2, LocalHom ***localhom )
{
	int dif, i, j, k1, k2, tmpint, start1, start2, end1, end2;
	static int impalloclen = 0;
	char *pt;
	static char *nocount1 = NULL;
	static char *nocount2 = NULL;

	if( impalloclen < lgth1 || impalloclen < lgth2 )
	{
		if( impmtx ) FreeFloatMtx( impmtx );
		if( nocount1 ) free( nocount1 );
		if( nocount2 ) free( nocount2 );
		impalloclen = MAX( lgth1, lgth2 ) + 2;
		impmtx = AllocateFloatMtx( impalloclen, impalloclen );
		nocount1 = AllocateCharVec( impalloclen );
		nocount2 = AllocateCharVec( impalloclen );
		impalloclen -= 2;
	}

	for( i=0; i<lgth1; i++ )
	{
		for( j=0; j<clus1; j++ )
			if( seq1[j][i] == '-' ) break;
		if( j != clus1 ) nocount1[i] = 1; 
		else			 nocount1[i] = 0;
	}
	for( i=0; i<lgth2; i++ )
	{
		for( j=0; j<clus2; j++ )
			if( seq2[j][i] == '-' ) break;
		if( j != clus2 ) nocount2[i] = 1;
		else			 nocount2[i] = 0;
	}

#if 0
fprintf( stderr, "nocount2 =\n" );
for( i = 0; i<impalloclen; i++ )
{
	fprintf( stderr, "nocount2[%d] = %d (%c)\n", i, nocount2[i], seq2[0][i] );
}
#endif

	for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
		impmtx[i][j] = 0.0;
	for( i=0; i<clus1; i++ )
	{
		fprintf( stderr, "i = %d, seq1 = %s\n", i, seq1[i] );
		for( j=0; j<clus2; j++ )
		{
			fprintf( stderr, "start1 = %d\n", localhom[i][j]->start1 );
			fprintf( stderr, "end1   = %d\n", localhom[i][j]->end1   );
			fprintf( stderr, "j = %d, seq2 = %s\n", j, seq2[j] );
			pt = seq1[i];
			tmpint = -1;
			while( *pt != 0 )
			{
				if( *pt++ != '-' ) tmpint++;
				if( tmpint == localhom[i][j]->start1 ) break;
			}
			start1 = pt - seq1[i] - 1;

			while( *pt != 0 )
			{
//				fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, localhom[i][j].end1, pt-seq1[i] );
				if( *pt++ != '-' ) tmpint++;
				if( tmpint == localhom[i][j]->end1 ) break;
			}
			end1 = pt - seq1[i] - 1;

			pt = seq2[j];
			tmpint = -1;
			while( *pt != 0 )
			{
				if( *pt++ != '-' ) tmpint++;
				if( tmpint == localhom[i][j]->start2 ) break;
			}
			start2 = pt - seq2[j] - 1;
			while( *pt != 0 )
			{
				if( *pt++ != '-' ) tmpint++;
				if( tmpint == localhom[i][j]->end2 ) break;
			}
			end2 = pt - seq2[j] - 1;
//			fprintf( stderr, "start1 = %d, end1 = %d, start2 = %d, end2 = %d\n", start1, end1, start2, end2 );
			k1 = start1;
			k2 = start2;
			fprintf( stderr, "step 0\n" );
			while( k1 <= end1 && k2 <= end2 )
			{
#if 0
				if( !nocount1[k1] && !nocount2[k2] )
					impmtx[k1][k2] += localhom[i][j].wimportance * eff1[i] * eff2[j];
				k1++; k2++;
#else
				if( !nocount1[k1] && !nocount2[k2] )
					impmtx[k1][k2] += localhom[i][j]->wimportance * eff1[i] * eff2[j];
				k1++; k2++;
#endif
			}

			dif = ( end1 - start1 ) - ( end2 - start2 );
			fprintf( stderr, "dif = %d\n", dif );
			if( dif > 0 )
			{
				do
				{
					fprintf( stderr, "dif = %d\n", dif );
					k1 = start1;
					k2 = start2 - dif;
					while( k1 <= end1 && k2 <= end2 )
					{
						if( 0 <= k2 && start2 <= k2 && !nocount1[k1] && !nocount2[k2] )
							impmtx[k1][k2] = localhom[i][j]->wimportance * eff1[i] * eff2[j];
						k1++; k2++;
					}
				}
				while( dif-- );
			}
			else
			{
				do
				{
					k1 = start1 + dif;
					k2 = start2;
					while( k1 <= end1 )
					{
						if( k1 >= 0 && k1 >= start1 && !nocount1[k1] && !nocount2[k2] )
							impmtx[k1][k2] = localhom[i][j]->wimportance * eff1[i] * eff2[j];
						k1++; k2++;
					}
				}
				while( dif++ );
			}
		}
	}
#if 0
	fprintf( stderr, "impmtx = \n" );
	for( k2=0; k2<lgth2; k2++ )
		fprintf( stderr, "%6.3f ", (double)k2 );
	fprintf( stderr, "\n" );
	for( k1=0; k1<lgth1; k1++ )
	{
		fprintf( stderr, "%d", k1 );
		for( k2=0; k2<lgth2; k2++ )
			fprintf( stderr, "%6.3f ", impmtx[k1][k2] );
		fprintf( stderr, "\n" );
	}
	exit( 1 );
#endif
}
Esempio n. 9
0
static void pairalign( char name[M][B], int nlen[M], char **seq, char **aseq, char **mseq1, char **mseq2, double *equiv, double *effarr, char **strfiles, char **chainids, int alloclen )
{
	int i, j, ilim;
	int clus1, clus2;
	int off1, off2;
	float pscore = 0.0; // by D.Mathog
	static char *indication1, *indication2;
	FILE *hat2p, *hat3p;
	static double **distancemtx;
	static double *effarr1 = NULL;
	static double *effarr2 = NULL;
	char *pt;
	char *hat2file = "hat2";
	LocalHom **localhomtable, *tmpptr;
	static char **pair;
//	int intdum;
	double bunbo;
	char **checkseq;


	localhomtable = (LocalHom **)calloc( njob, sizeof( LocalHom *) );
	for( i=0; i<njob; i++)
	{

		localhomtable[i] = (LocalHom *)calloc( njob, sizeof( LocalHom ) );
		for( j=0; j<njob; j++)
		{
			localhomtable[i][j].start1 = -1;
			localhomtable[i][j].end1 = -1;
			localhomtable[i][j].start2 = -1; 
			localhomtable[i][j].end2 = -1; 
			localhomtable[i][j].opt = -1.0;
			localhomtable[i][j].next = NULL;
			localhomtable[i][j].nokori = 0;
		}
	}

	if( effarr1 == NULL ) 
	{
		distancemtx = AllocateDoubleMtx( njob, njob );
		effarr1 = AllocateDoubleVec( njob );
		effarr2 = AllocateDoubleVec( njob );
		indication1 = AllocateCharVec( 150 );
		indication2 = AllocateCharVec( 150 );
		checkseq = AllocateCharMtx( njob, alloclen );
#if 0
#else
		pair = AllocateCharMtx( njob, njob );
#endif
	}

#if 0
	fprintf( stderr, "##### fftwinsize = %d, fftthreshold = %d\n", fftWinSize, fftThreshold );
#endif

#if 0
	for( i=0; i<njob; i++ )
		fprintf( stderr, "TBFAST effarr[%d] = %f\n", i, effarr[i] );
#endif


//	writePre( njob, name, nlen, aseq, 0 );

	for( i=0; i<njob; i++ ) for( j=0; j<njob; j++ ) pair[i][j] = 0;
	for( i=0; i<njob; i++ ) pair[i][i] = 1;

	for( i=0; i<njob; i++ )
	{
		strcpy( checkseq[i], seq[i] );
//		fprintf( stderr, "checkseq[%d] = %s\n", i, checkseq[i] );
	}


	ilim = njob - 1;
	for( i=0; i<ilim; i++ ) 
	{
		fprintf( stderr, "% 5d / %d\r", i, njob );


		for( j=i+1; j<njob; j++ )
		{


#if 0
			if( strlen( seq[i] ) == 0 || strlen( seq[j] ) == 0 )
			{
				distancemtx[i][j] = pscore;
				continue;
			}
#endif

			strcpy( aseq[i], seq[i] );
			strcpy( aseq[j], seq[j] );
			clus1 = conjuctionfortbfast( pair, i, aseq, mseq1, effarr1, effarr, indication1 );
			clus2 = conjuctionfortbfast( pair, j, aseq, mseq2, effarr2, effarr, indication2 );
	//		fprintf( stderr, "mseq1 = %s\n", mseq1[0] );
	//		fprintf( stderr, "mseq2 = %s\n", mseq2[0] );
	
#if 0
			fprintf( stderr, "group1 = %.66s", indication1 );
			fprintf( stderr, "\n" );
			fprintf( stderr, "group2 = %.66s", indication2 );
			fprintf( stderr, "\n" );
#endif
//			for( l=0; l<clus1; l++ ) fprintf( stderr, "## STEP-eff for mseq1-%d %f\n", l, effarr1[l] );
	
#if 1
			{
				switch( alg )
				{
					case( 'T' ):
						fprintf( stderr, "  Calling tmalign %d-%d/%d    \r", i+1, j+1, njob );
						pscore = calltmalign( mseq1, mseq2, equiv, strfiles[i], chainids[i], strfiles[j], chainids[j], alloclen );
						off1 = off2 = 0;
						break;
					case( 'R' ):
						fprintf( stderr, "  Calling PDP_ASH.pl %d-%d/%d    \r", i+1, j+1, njob );
						pscore = callrash( i, j, mseq1, mseq2, equiv, strfiles[i], chainids[i], strfiles[j], chainids[j], alloclen );
						off1 = off2 = 0;
						break;
					ErrorExit( "ERROR IN SOURCE FILE" );
				}
			}
#endif
			distancemtx[i][j] = pscore;
#if SCOREOUT
			fprintf( stderr, "score = %10.2f (%d,%d)\n", pscore, i, j );
#endif

			putlocalhom_str( mseq1[0], mseq2[0], equiv, scale, localhomtable[i]+j, off1, off2, (int)pscore, strlen( mseq1[0] ) );
#if 1

			if( alreadyoutput[i] == 0 )
			{
				alreadyoutput[i] = 1;
				gappick0( seq[i], mseq1[0] );
				fprintf( stdout, ">%d_%s-%s\n%s\n", i+1, strfiles[i], chainids[i], seq[i] );
				strcpy( checkseq[i], seq[i] );
			}
			else
			{
				gappick0( seq[i], mseq1[0] );
				fprintf( stderr, "checking seq%d\n", i );

//				fprintf( stderr, "     seq=%s\n", seq[i] );
//				fprintf( stderr, "checkseq=%s\n", checkseq[i] );

				if( strcmp( checkseq[i], seq[i] ) )
				{
					fprintf( stderr, "\n\nWARNING: Sequence changed!!\n" );
					fprintf( stderr, "i=%d\n", i );
					fprintf( stderr, "     seq=%s\n", seq[i] );
					fprintf( stderr, "checkseq=%s\n", checkseq[i] );
					exit( 1 );
				}
			}
			if( alreadyoutput[j] == 0 )
			{
				alreadyoutput[j] = 1;
				gappick0( seq[j], mseq2[0] );
				fprintf( stdout, ">%d_%s-%s\n%s\n", j+1, strfiles[j], chainids[j], seq[j] );
				strcpy( checkseq[j], seq[j] );
			}
			else
			{
				gappick0( seq[j], mseq2[0] );
				fprintf( stderr, "checking seq%d\n", j );
				if( strcmp( checkseq[j], seq[j] ) )
				{
					fprintf( stderr, "\n\nWARNING: Sequence changed!!\n" );
					fprintf( stderr, "j=%d\n", j );
					fprintf( stderr, "     seq=%s\n", seq[j] );
					fprintf( stderr, "checkseq=%s\n", checkseq[j] );
					exit( 1 );
				}
			}
#endif
		}
	}
	for( i=0; i<njob; i++ )
	{
		pscore = 0.0;
		for( pt=seq[i]; *pt; pt++ )
			pscore += amino_dis[(int)*pt][(int)*pt];
		distancemtx[i][i] = pscore;

	}

	ilim = njob-1;	
	for( i=0; i<ilim; i++ )
	{
		for( j=i+1; j<njob; j++ )
		{
			bunbo = MIN( distancemtx[i][i], distancemtx[j][j] );
			if( bunbo == 0.0 )
				distancemtx[i][j] = 2.0;
			else
				distancemtx[i][j] = ( 1.0 - distancemtx[i][j] / bunbo ) * 2.0;
		}
	}

	hat2p = fopen( hat2file, "w" );
	if( !hat2p ) ErrorExit( "Cannot open hat2." );
	WriteHat2( hat2p, njob, name, distancemtx );
	fclose( hat2p );

	fprintf( stderr, "##### writing hat3\n" );
	hat3p = fopen( "hat3", "w" );
	if( !hat3p ) ErrorExit( "Cannot open hat3." );
	ilim = njob-1;	
	for( i=0; i<ilim; i++ ) 
	{
		for( j=i+1; j<njob; j++ )
		{
			for( tmpptr=localhomtable[i]+j; tmpptr; tmpptr=tmpptr->next )
			{
				if( tmpptr->opt == -1.0 ) continue;
				fprintf( hat3p, "%d %d %d %7.5f %d %d %d %d k\n", i, j, tmpptr->overlapaa, tmpptr->opt, tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2 ); 
			}
		}
	}
	fclose( hat3p );
#if DEBUG
	fprintf( stderr, "calling FreeLocalHomTable\n" );
#endif
	FreeLocalHomTable( localhomtable, njob );
#if DEBUG
	fprintf( stderr, "done. FreeLocalHomTable\n" );
#endif
}
Esempio n. 10
0
static void prepareash( FILE *fp, char ***strfiles, char ***chainids, char ***seqpt, char ***mseq1pt, char ***mseq2pt, double **equivpt, int *alloclenpt )
{
	int i, res;
	char *dumseq;
	char line[1000];
	char fname[1000];
	char command[1000];
	int linenum, istr, nstr;
	FILE *checkfp;
	char *sline; 
	int use[1000];
	linenum = 0;
	nstr = 0;
	while( 1 )
	{
		fgets( line, 999, fp );
		if( feof( fp ) ) break;
		sline = strip( line );
		use[linenum] = 1;
		if( sline[0] == '#' || strlen( sline ) < 2 )
		{
			use[linenum] = 0;
			linenum++;
			continue;
		}
		extractfirstword( sline );
		checkfp = fopen( sline, "r" );
		if( checkfp == NULL )
		{
			fprintf( stderr, "Cannot open %s.\n", sline );
			exit( 1 );
		}
#if 0
		fgets( linec, 999, checkfp );
		if( strncmp( "HEADER ", linec, 7 ) )
		{
			fprintf( stderr, "Check the format of %s.\n", sline );
			exit( 1 );
		}
#endif
		if( checkcbeta( checkfp ) ) 
		{
			fprintf( stderr, "%s has no C-beta atoms.\n", sline );
			exit( 1 );
		}
		else
			nstr++;
		fclose( checkfp );
		linenum++;
	}
	njob = nstr;
	fprintf( stderr, "nstr = %d\n", nstr );

	*strfiles = AllocateCharMtx( nstr, 1000 );
	*chainids = AllocateCharMtx( nstr, 2 );

	rewind( fp );
	istr = 0;
	linenum = 0;
	while( 1 )
	{
		fgets( line, 999, fp );
		if( feof( fp ) ) break;
		sline = strip( line );
		if( use[linenum++] ) 
		{
			(*chainids)[istr][0] = getchainid( sline );
			(*chainids)[istr][1] = 0;
			extractfirstword( sline );
			sprintf( fname, "%s", sline );
			cutpath( fname );
			sprintf( command, "cp %s %s.pdb", sline, fname );
			system( command );
			sprintf( command, "perl \"%s/clean.pl\" %s.pdb", whereispairalign, fname );
			res = system( command );
			if( res )
			{
				fprintf( stderr, "error: Install clean.pl\n" );
				exit( 1 );
			}
			strcpy( (*strfiles)[istr++], fname );
		}
	}

	*seqpt = AllocateCharMtx( njob, nlenmax*2+1 );
	*mseq1pt = AllocateCharMtx( njob, 0 );
	*mseq2pt = AllocateCharMtx( njob, 0 );
	*equivpt = AllocateDoubleVec( nlenmax*2+1 );
	*alloclenpt = nlenmax*2;
	dumseq = AllocateCharVec( nlenmax*2+1 );
	alreadyoutput = AllocateIntVec( njob );
	for( i=0; i<njob; i++ ) alreadyoutput[i] = 0;

	for( i=0; i<istr; i++ )
	{
		fprintf( stderr, "i=%d\n", i );
		(*seqpt)[i][0] = 0;

		(*mseq1pt)[0] = (*seqpt)[i];
		(*mseq2pt)[0] = dumseq;

		callrash( i, i, *mseq1pt, *mseq2pt, *equivpt, (*strfiles)[i], (*chainids)[i], (*strfiles)[i], (*chainids)[i], *alloclenpt );
		fprintf( stdout, ">%d_%s-%s\n%s\n", i+1, (*strfiles)[i], (*chainids)[i], (*seqpt)[i] );
		alreadyoutput[i] = 1;
	}
}
Esempio n. 11
0
static void pairalign( char name[M][B], int nlen[M], char **seq, double *effarr, int alloclen )
{
	int i, j;
	FILE *hat3p;
	float pscore = 0.0; // by D.Mathog
	static double *effarr1 = NULL;
	static double *effarr2 = NULL;
	char *aseq;
	static char **pseq;
	LocalHom **localhomtable, *tmpptr;
	double tsuyosa;

	if( nhomologs < 1 ) nhomologs = 1; // tsuyosa=0.0 wo sakeru
	tsuyosa = (double)nhomologs * nhomologs * TSUYOSAFACTOR;
	fprintf( stderr, "tsuyosa = %f\n", tsuyosa );
	localhomtable = (LocalHom **)calloc( njob, sizeof( LocalHom *) );
	for( i=0; i<njob; i++)
	{
		localhomtable[i] = (LocalHom *)calloc( njob, sizeof( LocalHom ) );
		for( j=0; j<njob; j++)
		{
			localhomtable[i][j].start1 = -1;
			localhomtable[i][j].end1 = -1;
			localhomtable[i][j].start2 = -1; 
			localhomtable[i][j].end2 = -1; 
			localhomtable[i][j].opt = -1.0;
			localhomtable[i][j].next = NULL;
		}
	}

	if( effarr1 == NULL ) 
	{
		effarr1 = AllocateDoubleVec( njob );
		effarr2 = AllocateDoubleVec( njob );
		pseq = AllocateCharMtx( 2, 0 );
		aseq = AllocateCharVec( nlenmax*9+1 );
#if 0
#else
#endif
	}

#if 0
	fprintf( stderr, "##### fftwinsize = %d, fftthreshold = %d\n", fftWinSize, fftThreshold );
#endif

#if 0
	for( i=0; i<njob; i++ )
		fprintf( stderr, "TBFAST effarr[%d] = %f\n", i, effarr[i] );
#endif


//	writePre( njob, name, nlen, aseq, 0 );

	hat3p = fopen( "hat3", "w" );
	if( !hat3p ) ErrorExit( "Cannot open hat3." );
	fprintf( stderr, "\n" );
	for( i=0; i<njob-1; i++ )
	{
		for( j=i+1; j<njob; j++ )
		{
			pseq[0] = seq[i];
			pseq[1] = seq[j];

			if( strlen( pseq[0] ) != strlen( pseq[1] ) )
			{
				fprintf( stderr, "## ERROR  ###\n" );
				fprintf( stderr, "Not aligned,  %s - %s\n", name[i], name[j] );
				fprintf( stderr, "## ERROR  ###\n" );
				exit( 1 );
			}


			fprintf( stderr, "adding %d-%d\r", i, j );
			putlocalhom2( pseq[0], pseq[1], localhomtable[i]+j, 0, 0, (int)pscore, strlen( pseq[0] ) );
			for( tmpptr=localhomtable[i]+j; tmpptr; tmpptr=tmpptr->next )
			{
				if( tmpptr->opt == -1.0 ) continue;
				if( tmpptr->start1 == -1 ) continue;
				fprintf( hat3p, "%d %d %d %6.3f %d %d %d %d k\n", i+seedoffset, j+seedoffset, tmpptr->overlapaa, tmpptr->opt * tsuyosa, tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2 ); 
			}
		}
	}
	fprintf( stderr, "\n" );
	fclose( hat3p );

#if DEBUG
	fprintf( stderr, "calling FreeLocalHomTable\n" );
#endif
	FreeLocalHomTable( localhomtable, njob );
#if DEBUG
	fprintf( stderr, "done. FreeLocalHomTable\n" );
#endif
}
Esempio n. 12
0
float Falign_localhom( char  **seq1, char  **seq2, 
			  double *eff1, double *eff2, 
			  int    clus1, int    clus2,
			  int alloclen, 
			   LocalHom ***localhom, float *totalimpmatch,
			   int *gapmap1, int *gapmap2,
				int *chudanpt, int chudanref, int *chudanres )
{
   // tditeration.c deha alloclen ha huhen nanode
   // prevalloclen ha iranai.
	int i, j, k, l, m, maxk;
	int nlen, nlen2, nlen4;
	static TLS int crossscoresize = 0;
	static TLS char **tmpseq1 = NULL;
	static TLS char **tmpseq2 = NULL;
	static TLS char **tmpptr1 = NULL;
	static TLS char **tmpptr2 = NULL;
	static TLS char **tmpres1 = NULL;
	static TLS char **tmpres2 = NULL;
	static TLS char **result1 = NULL;
	static TLS char **result2 = NULL;
#if RND
	static TLS char **rndseq1 = NULL;
	static TLS char **rndseq2 = NULL;
#endif
	static TLS Fukusosuu **seqVector1 = NULL;
	static TLS Fukusosuu **seqVector2 = NULL;
	static TLS Fukusosuu **naiseki = NULL;   
	static TLS Fukusosuu *naisekiNoWa = NULL; 
	static TLS double *soukan = NULL;
	static TLS double **crossscore = NULL;
	int nlentmp;
	static TLS int *kouho = NULL;
	static TLS Segment *segment = NULL;
	static TLS Segment *segment1 = NULL;
	static TLS Segment *segment2 = NULL;
	static TLS Segment **sortedseg1 = NULL;
	static TLS Segment **sortedseg2 = NULL;
	static TLS int *cut1 = NULL;
	static TLS int *cut2 = NULL;
	static TLS char *sgap1, *egap1, *sgap2, *egap2;
	static TLS int localalloclen = 0;
	int lag;
	int tmpint;
	int count, count0;
	int len1, len2;
	int totallen;
	float totalscore;
	float impmatch;

	extern Fukusosuu   *AllocateFukusosuuVec();
	extern Fukusosuu  **AllocateFukusosuuMtx();

	if( seq1 == NULL )
	{
		if( result1 ) 
		{
//			fprintf( stderr, "Freeing localarrays in Falign\n" );
			localalloclen = 0;
			mymergesort( 0, 0, NULL );
			alignableReagion( 0, 0, NULL, NULL, NULL, NULL, NULL );
			fft( 0, NULL, 1 );
			A__align( NULL, NULL, NULL, NULL, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0, 0 );
			G__align11( NULL, NULL, 0, 0, 0 );
			partA__align( NULL, NULL, NULL, NULL, 0, 0, 0, NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL );
			blockAlign2( NULL, NULL, NULL, NULL, NULL, NULL );
			if( crossscore ) FreeDoubleMtx( crossscore );
			FreeCharMtx( result1 );
			FreeCharMtx( result2 );
			FreeCharMtx( tmpres1 );
			FreeCharMtx( tmpres2 );
			FreeCharMtx( tmpseq1 );
			FreeCharMtx( tmpseq2 );
			free( sgap1 );
			free( egap1 );
			free( sgap2 );
			free( egap2 );
			free( kouho );
			free( cut1 );
			free( cut2 );
			free( tmpptr1 );
			free( tmpptr2 );
			free( segment );
			free( segment1 );
			free( segment2 );
			free( sortedseg1 );
			free( sortedseg2 );
			if( !kobetsubunkatsu )
			{
				FreeFukusosuuMtx ( seqVector1 );
				FreeFukusosuuMtx ( seqVector2 );
				FreeFukusosuuVec( naisekiNoWa );
				FreeFukusosuuMtx( naiseki );
				FreeDoubleVec( soukan );
			}
		}
		else
		{
//			fprintf( stderr, "Did not allocate localarrays in Falign\n" );
		}

		return( 0.0 );
	}

	len1 = strlen( seq1[0] );
	len2 = strlen( seq2[0] );
	nlentmp = MAX( len1, len2 );

	nlen = 1;
	while( nlentmp >= nlen ) nlen <<= 1;
#if 0
	fprintf( stderr, "###   nlen    = %d\n", nlen );
#endif

	nlen2 = nlen/2; nlen4 = nlen2 / 2;

#if DEBUG
	fprintf( stderr, "len1 = %d, len2 = %d\n", len1, len2 );
	fprintf( stderr, "nlentmp = %d, nlen = %d\n", nlentmp, nlen );
#endif

	if( !localalloclen )
	{
		sgap1 = AllocateCharVec( njob ); 
		egap1 = AllocateCharVec( njob ); 
		sgap2 = AllocateCharVec( njob ); 
		egap2 = AllocateCharVec( njob ); 
		kouho = AllocateIntVec( NKOUHO );
		cut1 = AllocateIntVec( MAXSEG );
		cut2 = AllocateIntVec( MAXSEG );
		tmpptr1 = AllocateCharMtx( njob, 0 );
		tmpptr2 = AllocateCharMtx( njob, 0 );
		result1 = AllocateCharMtx( njob, alloclen );
		result2 = AllocateCharMtx( njob, alloclen );
		tmpres1 = AllocateCharMtx( njob, alloclen );
		tmpres2 = AllocateCharMtx( njob, alloclen );
//		crossscore = AllocateDoubleMtx( MAXSEG, MAXSEG );
		segment = (Segment *)calloc( MAXSEG, sizeof( Segment ) );
		segment1 = (Segment *)calloc( MAXSEG, sizeof( Segment ) );
		segment2 = (Segment *)calloc( MAXSEG, sizeof( Segment ) );
		sortedseg1 = (Segment **)calloc( MAXSEG, sizeof( Segment * ) );
		sortedseg2 = (Segment **)calloc( MAXSEG, sizeof( Segment * ) );
		if( !( segment && segment1 && segment2 && sortedseg1 && sortedseg2 ) )
			ErrorExit( "Allocation error\n" );

		if     ( scoremtx == -1 ) n20or4or2 = 4;
		else if( fftscore == 1  ) n20or4or2 = 2;
		else                      n20or4or2 = 20;
	}
	if( localalloclen < nlen )
	{
		if( localalloclen )
		{
#if 1
			if( !kobetsubunkatsu )
			{
				FreeFukusosuuMtx ( seqVector1 );
				FreeFukusosuuMtx ( seqVector2 );
				FreeFukusosuuVec( naisekiNoWa );
				FreeFukusosuuMtx( naiseki );
				FreeDoubleVec( soukan );
			}
			FreeCharMtx( tmpseq1 );
			FreeCharMtx( tmpseq2 );
#endif
#if RND
			FreeCharMtx( rndseq1 );
			FreeCharMtx( rndseq2 );
#endif
		}

		tmpseq1 = AllocateCharMtx( njob, nlen );
		tmpseq2 = AllocateCharMtx( njob, nlen );
		if( !kobetsubunkatsu )
		{
			naisekiNoWa = AllocateFukusosuuVec( nlen );
			naiseki = AllocateFukusosuuMtx( n20or4or2, nlen );
			seqVector1 = AllocateFukusosuuMtx( n20or4or2+1, nlen+1 );
			seqVector2 = AllocateFukusosuuMtx( n20or4or2+1, nlen+1 );
			soukan = AllocateDoubleVec( nlen+1 );
		}
#if RND
		rndseq1 = AllocateCharMtx( njob, nlen );
		rndseq2 = AllocateCharMtx( njob, nlen );
		for( i=0; i<njob; i++ )
		{
			generateRndSeq( rndseq1[i], nlen );
			generateRndSeq( rndseq2[i], nlen );
		}
#endif
		localalloclen = nlen;
	}
	
	for( j=0; j<clus1; j++ ) strcpy( tmpseq1[j], seq1[j] );
	for( j=0; j<clus2; j++ ) strcpy( tmpseq2[j], seq2[j] );

#if 0
fftfp = fopen( "input_of_Falign", "w" );
fprintf( fftfp, "nlen = %d\n", nlen );
fprintf( fftfp, "seq1: ( %d sequences ) \n", clus1 );
for( i=0; i<clus1; i++ )
	fprintf( fftfp, "%s\n", seq1[i] );
fprintf( fftfp, "seq2: ( %d sequences ) \n", clus2 );
for( i=0; i<clus2; i++ )
	fprintf( fftfp, "%s\n", seq2[i] );
fclose( fftfp );
system( "less input_of_Falign < /dev/tty > /dev/tty" );
#endif
	if( !kobetsubunkatsu )
	{
		fprintf( stderr,  "FFT ... " );

		for( j=0; j<n20or4or2; j++ ) vec_init( seqVector1[j], nlen );
		if( fftscore && scoremtx != -1 )
		{
			for( i=0; i<clus1; i++ )
			{
				seq_vec_2( seqVector1[0], polarity, eff1[i], tmpseq1[i] );
				seq_vec_2( seqVector1[1], volume,   eff1[i], tmpseq1[i] );
			}
		}
		else
		{
#if 0
			for( i=0; i<clus1; i++ ) for( j=0; j<n20or4or2; j++ ) 
				seq_vec( seqVector1[j], amino[j], eff1[i], tmpseq1[i] );
#else
			for( i=0; i<clus1; i++ )
				seq_vec_3( seqVector1, eff1[i], tmpseq1[i] );
#endif
		}
#if RND
		for( i=0; i<clus1; i++ )
		{
			vec_init2( seqVector1, rndseq1[i], eff1[i], len1, nlen );
		}
#endif
#if 0
fftfp = fopen( "seqVec", "w" );
fprintf( fftfp, "before transform\n" );
for( k=0; k<n20or4or2; k++ ) 
{
   fprintf( fftfp, "nlen=%d\n", nlen );
   fprintf( fftfp, "%c\n", amino[k] );
   for( l=0; l<nlen; l++ )
   fprintf( fftfp, "%f %f\n", seqVector1[k][l].R, seqVector1[k][l].I );
}
fclose( fftfp );
system( "less seqVec < /dev/tty > /dev/tty" );
#endif

		for( j=0; j<n20or4or2; j++ ) vec_init( seqVector2[j], nlen );
		if( fftscore && scoremtx != -1 )
		{
			for( i=0; i<clus2; i++ )
			{
				seq_vec_2( seqVector2[0], polarity, eff2[i], tmpseq2[i] );
				seq_vec_2( seqVector2[1], volume,   eff2[i], tmpseq2[i] );
			}
		}
		else
		{
#if 0
			for( i=0; i<clus2; i++ ) for( j=0; j<n20or4or2; j++ ) 
				seq_vec( seqVector2[j], amino[j], eff2[i], tmpseq2[i] );
#else
			for( i=0; i<clus2; i++ )
				seq_vec_3( seqVector2, eff2[i], tmpseq2[i] );
#endif
		}
#if RND
		for( i=0; i<clus2; i++ )
		{
			vec_init2( seqVector2, rndseq2[i], eff2[i], len2, nlen );
		}
#endif

#if 0
fftfp = fopen( "seqVec2", "w" );
fprintf( fftfp, "before fft\n" );
for( k=0; k<n20or4or2; k++ ) 
{
   fprintf( fftfp, "%c\n", amino[k] );
   for( l=0; l<nlen; l++ )
   fprintf( fftfp, "%f %f\n", seqVector2[k][l].R, seqVector2[k][l].I );
}
fclose( fftfp );
system( "less seqVec2 < /dev/tty > /dev/tty" );
#endif

		for( j=0; j<n20or4or2; j++ )
		{
			fft( nlen, seqVector2[j], (j==0) );
			fft( nlen, seqVector1[j], 0 );
		}
#if 0
fftfp = fopen( "seqVec2", "w" );
fprintf( fftfp, "#after fft\n" );
for( k=0; k<n20or4or2; k++ ) 
{
   fprintf( fftfp, "#%c\n", amino[k] );
   for( l=0; l<nlen; l++ )
	   fprintf( fftfp, "%f %f\n", seqVector2[k][l].R, seqVector2[k][l].I );
}
fclose( fftfp );
system( "less seqVec2 < /dev/tty > /dev/tty" );
#endif

		for( k=0; k<n20or4or2; k++ ) 
		{
			for( l=0; l<nlen; l++ ) 
				calcNaiseki( naiseki[k]+l, seqVector1[k]+l, seqVector2[k]+l );
		}
		for( l=0; l<nlen; l++ ) 
		{
			naisekiNoWa[l].R = 0.0;
			naisekiNoWa[l].I = 0.0;
			for( k=0; k<n20or4or2; k++ ) 
			{
				naisekiNoWa[l].R += naiseki[k][l].R;
				naisekiNoWa[l].I += naiseki[k][l].I;
			}
		}
	
#if 0
	fftfp = fopen( "naisekiNoWa", "w" );
	fprintf( fftfp, "#Before fft\n" );
	for( l=0; l<nlen; l++ )
		fprintf( fftfp, "%d  %f %f\n", l, naisekiNoWa[l].R, naisekiNoWa[l].I ); 
	fclose( fftfp );
	system( "less naisekiNoWa < /dev/tty > /dev/tty " );
#endif

		fft( -nlen, naisekiNoWa, 0 );
	
		for( m=0; m<=nlen2; m++ ) 
			soukan[m] = naisekiNoWa[nlen2-m].R;
		for( m=nlen2+1; m<nlen; m++ ) 
			soukan[m] = naisekiNoWa[nlen+nlen2-m].R;

#if 0
	fftfp = fopen( "naisekiNoWa", "w" );
	fprintf( fftfp, "#After fft\n" );
	for( l=0; l<nlen; l++ )
		fprintf( fftfp, "%d  %f\n", l, naisekiNoWa[l].R ); 
	fclose( fftfp );
	fftfp = fopen( "list.plot", "w"  );
	fprintf( fftfp, "plot 'naisekiNoWa'\npause -1" );
	fclose( fftfp );
	system( "/usr/bin/gnuplot list.plot &" );
#endif
#if 0
	fprintf( stderr, "frt write start\n" );
	fftfp = fopen( "frt", "w" );
	for( l=0; l<nlen; l++ )
		fprintf( fftfp, "%d  %f\n", l-nlen2, soukan[l] ); 
	fclose( fftfp );
	system( "less frt < /dev/tty > /dev/tty" );
#if 0
	fftfp = fopen( "list.plot", "w"  );
	fprintf( fftfp, "plot 'frt'\n pause +1" );
	fclose( fftfp );
	system( "/usr/bin/gnuplot list.plot" );
#endif
#endif


		getKouho( kouho, NKOUHO, soukan, nlen );

#if 0
		for( i=0; i<NKOUHO; i++ )
		{
			fprintf( stderr, "kouho[%d] = %d\n", i, kouho[i] );
		}
#endif
	}

#if KEIKA
	fprintf( stderr, "Searching anchors ... " );
#endif
	count = 0;



#define CAND 0
#if CAND
	fftfp = fopen( "cand", "w" );
	fclose( fftfp );
#endif
	if( kobetsubunkatsu )
	{
		maxk = 1;
		kouho[0] = 0;
	}
	else
	{
		maxk = NKOUHO;
	}

	for( k=0; k<maxk; k++ ) 
	{
		lag = kouho[k];
		zurasu2( lag, clus1, clus2, seq1, seq2, tmpptr1, tmpptr2 );
#if CAND
		fftfp = fopen( "cand", "a" );
		fprintf( fftfp, "Candidate No.%d lag = %d\n", k+1, lag );
		fprintf( fftfp, "%s\n", tmpptr1[0] );
		fprintf( fftfp, "%s\n", tmpptr2[0] );
		fclose( fftfp );
#endif
		tmpint = alignableReagion( clus1, clus2, tmpptr1, tmpptr2, eff1, eff2, segment+count );
		
		if( count+tmpint > MAXSEG -3 ) ErrorExit( "TOO MANY SEGMENTS.\n" );


		while( tmpint-- > 0 )
		{
			if( lag > 0 )
			{
				segment1[count].start  = segment[count].start ;
				segment1[count].end    = segment[count].end   ;
				segment1[count].center = segment[count].center;
				segment1[count].score  = segment[count].score;

				segment2[count].start  = segment[count].start  + lag;
				segment2[count].end    = segment[count].end    + lag;
				segment2[count].center = segment[count].center + lag;
				segment2[count].score  = segment[count].score       ;
			}
			else
			{
				segment1[count].start  = segment[count].start  - lag;
				segment1[count].end    = segment[count].end    - lag;
				segment1[count].center = segment[count].center - lag;
				segment1[count].score  = segment[count].score       ;

				segment2[count].start  = segment[count].start ;
				segment2[count].end    = segment[count].end   ;
				segment2[count].center = segment[count].center;
				segment2[count].score  = segment[count].score ;
			}
#if 0
			fftfp = fopen( "cand", "a" );
			fprintf( fftfp, "Goukaku=%dko\n", tmpint ); 
			fprintf( fftfp, "in 1 %d\n", segment1[count].center );
			fprintf( fftfp, "in 2 %d\n", segment2[count].center );
			fclose( fftfp );
#endif
			segment1[count].pair = &segment2[count];
			segment2[count].pair = &segment1[count];
			count++;
#if 0
			fprintf( stderr, "count=%d\n", count );
#endif
		}
	}
#if 1
	if( !kobetsubunkatsu )
		fprintf( stderr, "%d segments found\n", count );
#endif
	if( !count && fftNoAnchStop )
		ErrorExit( "Cannot detect anchor!" );
#if 0
	fftfp = fopen( "fft", "a" );
	fprintf( fftfp, "RESULT before sort:\n" );
	for( l=0; l<count; l++ )
	{
		fprintf( fftfp, "cut[%d]=%d, ", l, segment1[l].center );
		fprintf( fftfp, "%d score = %f\n", segment2[l].center, segment1[l].score );
	}
	fclose( fftfp );
#endif

#if KEIKA
	fprintf( stderr, "Aligning anchors ... " );
#endif
	for( i=0; i<count; i++ )
	{
		sortedseg1[i] = &segment1[i];
		sortedseg2[i] = &segment2[i];
	}
#if 0
	tmpsort( count, sortedseg1 ); 
	tmpsort( count, sortedseg2 ); 
	qsort( sortedseg1, count, sizeof( Segment * ), segcmp );
	qsort( sortedseg2, count, sizeof( Segment * ), segcmp );
#else
	mymergesort( 0, count-1, sortedseg1 ); 
	mymergesort( 0, count-1, sortedseg2 ); 
#endif
	for( i=0; i<count; i++ ) sortedseg1[i]->number = i;
	for( i=0; i<count; i++ ) sortedseg2[i]->number = i;


	if( kobetsubunkatsu )
	{
		for( i=0; i<count; i++ )
	    {
			cut1[i+1] = sortedseg1[i]->center;
			cut2[i+1] = sortedseg2[i]->center;
		}
		cut1[0] = 0;
		cut2[0] = 0;
		cut1[count+1] = len1;
		cut2[count+1] = len2;
		count += 2;
	}
	else
	{
		if( crossscoresize < count+2 )
		{
			crossscoresize = count+2;
#if 1
			fprintf( stderr, "######allocating crossscore, size = %d\n", crossscoresize );
#endif
			if( crossscore ) FreeDoubleMtx( crossscore );
			crossscore = AllocateDoubleMtx( crossscoresize, crossscoresize );
		}
		for( i=0; i<count+2; i++ ) for( j=0; j<count+2; j++ )
			crossscore[i][j] = 0.0;
		for( i=0; i<count; i++ )
		{
			crossscore[segment1[i].number+1][segment1[i].pair->number+1] = segment1[i].score;
			cut1[i+1] = sortedseg1[i]->center;
			cut2[i+1] = sortedseg2[i]->center;
		}

#if DEBUG
		fprintf( stderr, "AFTER SORT\n" );
		for( i=0; i<count; i++ ) fprintf( stderr, "%d, %d\n", segment1[i].start, segment2[i].start );
#endif

		crossscore[0][0] = 10000000.0;
		cut1[0] = 0; 
		cut2[0] = 0;
		crossscore[count+1][count+1] = 10000000.0;
		cut1[count+1] = len1;
		cut2[count+1] = len2;
		count += 2;
		count0 = count;
	
		blockAlign2( cut1, cut2, sortedseg1, sortedseg2, crossscore, &count );
		if( count0 > count )
		{
#if 0
			fprintf( stderr, "\7 REPEAT!? \n" ); 
#else
			fprintf( stderr, "REPEAT!? \n" ); 
#endif
			if( fftRepeatStop ) exit( 1 );
		}
#if KEIKA
		else fprintf( stderr, "done\n" );
#endif
	}

#if 0
	fftfp = fopen( "fft", "a" );
	fprintf( fftfp, "RESULT after sort:\n" );
	for( l=0; l<count; l++ )
	{
		fprintf( fftfp, "cut[%d]=%d, ", l, segment1[l].center );
		fprintf( fftfp, "%d\n", segment2[l].center );
	}
	fclose( fftfp );
#endif

#if 0
	fftfp = fopen( "fft", "a" );
	fprintf( fftfp, "RESULT after sort:\n" );
	for( l=0; l<count; l++ )
	{
		fprintf( fftfp, "cut : %d %d\n", cut1[l], cut2[l] );
	}
	fclose( fftfp );
#endif

#if KEIKA
	fprintf( trap_g, "Devided to %d segments\n", count-1 );
	fprintf( trap_g, "%d  %d forg\n", MIN( clus1, clus2 ), count-1 );
#endif

	totallen = 0;
	for( j=0; j<clus1; j++ ) result1[j][0] = 0;
	for( j=0; j<clus2; j++ ) result2[j][0] = 0;
	totalscore = 0.0;
	*totalimpmatch = 0.0;
	for( i=0; i<count-1; i++ )
	{
#if DEBUG
		fprintf( stderr, "DP %03d / %03d %4d to ", i+1, count-1, totallen );
#else
#if KEIKA
		fprintf( stderr, "DP %03d / %03d\r", i+1, count-1 );
#endif
#endif

		if( cut1[i] ) 
		{
			getkyokaigap( sgap1, seq1, cut1[i]-1, clus1 );
			getkyokaigap( sgap2, seq2, cut2[i]-1, clus2 );
		}
		else
		{
			for( j=0; j<clus1; j++ ) sgap1[j] = 'o';
			for( j=0; j<clus2; j++ ) sgap2[j] = 'o';
		}
		if( cut1[i+1] != len1 )
		{
			getkyokaigap( egap1, seq1, cut1[i+1], clus1 );
			getkyokaigap( egap2, seq2, cut2[i+1], clus2 );
		}
		else
		{
			for( j=0; j<clus1; j++ ) egap1[j] = 'o';
			for( j=0; j<clus2; j++ ) egap2[j] = 'o';
		}

		for( j=0; j<clus1; j++ )
		{
			strncpy( tmpres1[j], seq1[j]+cut1[i], cut1[i+1]-cut1[i] );
			tmpres1[j][cut1[i+1]-cut1[i]] = 0;
		}
		if( kobetsubunkatsu ) commongappick_record( clus1, tmpres1, gapmap1 );
		for( j=0; j<clus2; j++ )
		{
			strncpy( tmpres2[j], seq2[j]+cut2[i], cut2[i+1]-cut2[i] );
			tmpres2[j][cut2[i+1]-cut2[i]] = 0;
		}
		if( kobetsubunkatsu ) commongappick_record( clus2, tmpres2, gapmap2 );

#if 0
		fprintf( stderr, "count = %d\n", count );
		fprintf( stderr, "### reg1 = %d-%d\n", cut1[i], cut1[i+1]-1 );
		fprintf( stderr, "### reg2 = %d-%d\n", cut2[i], cut2[i+1]-1 );
#endif

		switch( alg )
		{
			case( 'a' ):
				totalscore += Aalign( tmpres1, tmpres2, eff1, eff2, clus1, clus2, alloclen );
				break;
			case( 'Q' ):
				totalscore += partQ__align( tmpres1, tmpres2, eff1, eff2, clus1, clus2, alloclen, localhom, &impmatch, cut1[i], cut1[i+1]-1, cut2[i], cut2[i+1]-1, gapmap1, gapmap2, sgap1, sgap2, egap1, egap2 );
				*totalimpmatch += impmatch;
//				fprintf( stderr, "*totalimpmatch in Falign_localhom = %f\n", *totalimpmatch );
				break;
			case( 'A' ):
				totalscore += partA__align( tmpres1, tmpres2, eff1, eff2, clus1, clus2, alloclen, localhom, &impmatch, cut1[i], cut1[i+1]-1, cut2[i], cut2[i+1]-1, gapmap1, gapmap2, sgap1, sgap2, egap1, egap2, chudanpt, chudanref, chudanres );
				*totalimpmatch += impmatch;
//				fprintf( stderr, "*totalimpmatch in Falign_localhom = %f\n", *totalimpmatch );


				break;
			default:
				fprintf( stderr, "alg = %c\n", alg );
				ErrorExit( "ERROR IN SOURCE FILE Falign.c" );
				break;
		}
#ifdef enablemultithread
		if( chudanres && *chudanres )
		{
//			fprintf( stderr, "\n\n## CHUUDAN!!! at Falign_localhom\n" );
			return( -1.0 );
		}
#endif

		nlen = strlen( tmpres1[0] );
		if( totallen + nlen > alloclen )
		{
			fprintf( stderr, "totallen=%d +  nlen=%d > alloclen = %d\n", totallen, nlen, alloclen );
			ErrorExit( "LENGTH OVER in Falign\n " );
		}
		for( j=0; j<clus1; j++ ) strcat( result1[j], tmpres1[j] );
		for( j=0; j<clus2; j++ ) strcat( result2[j], tmpres2[j] );
		totallen += nlen;
#if 0
		fprintf( stderr, "%4d\r", totallen );
		fprintf( stderr, "\n\n" );
		for( j=0; j<clus1; j++ ) 
		{
			fprintf( stderr, "%s\n", tmpres1[j] );
		}
		fprintf( stderr, "-------\n" );
		for( j=0; j<clus2; j++ ) 
		{
			fprintf( stderr, "%s\n", tmpres2[j] );
		}
#endif
	}
#if KEIKA
	fprintf( stderr, "DP ... done   \n" );
#endif

	for( j=0; j<clus1; j++ ) strcpy( seq1[j], result1[j] );
	for( j=0; j<clus2; j++ ) strcpy( seq2[j], result2[j] );
#if 0
	for( j=0; j<clus1; j++ ) 
	{
		fprintf( stderr, "%s\n", result1[j] );
	}
	fprintf( stderr, "- - - - - - - - - - -\n" );
	for( j=0; j<clus2; j++ ) 
	{
		fprintf( stderr, "%s\n", result2[j] );
	}
#endif
	return( totalscore );
}