#include "mltaln.h" #include "dp.h" #define DEBUG 0 #define WMCHECK 1 #define ALGZSTRAIGHT 0 #define ALGZGAP 0 #define USEGAPLENMTX 0 #define USEGAPLENHALF 0 #define FREEFREQUENTLY 1 #define IDATEND 0 #define MACHIGAI 0 #define OUTGAP0TRY 0 #define XXXXXXX 0 #define USE_PENALTY_EX 0 #define FASTMATCHCALC 1 #define SLOW 0 #define zero 0 #define one 1 #if USEGAPLENHALF #define USEGAPLENHALFORMTX 1 #endif #if USEGAPLENMTX #define USEGAPLENHALFORMTX 1 #endif #if WMCHECK static int PFACERROR = 0; #endif static TLS double **impmtx = NULL; static TLS int impalloclen = 0; double imp_match_out_scD( int i1, int j1 ) { // fprintf( stderr, "imp+match = %f\n", impmtx[i1][j1] * fastathreshold ); // fprintf( stderr, "val = %f\n", impmtx[i1][j1] ); return( impmtx[i1][j1] ); } typedef struct _gaplenvec { int relend; #if USEGAPLENHALFORMTX int idatend; #endif int idatnext; int idatprev; int npat; int len; // sukoshi muda. double freq; } Gaplen; #if 0 static void imp_match_out_vead_gapmap( double *imp, int i1, int lgth2, int *gapmap2 ) { #if FASTMATCHCALC double *pt = impmtx[i1]; int *gapmappt = gapmap2; while( lgth2-- ) *imp++ += pt[*gapmappt++]; #else int j; double *pt = impmtx[i1]; for( j=0; j ", match[k], mid ); match[k] -= matrices[mid][c1][c2] * eff1[i] * eff2[j]; // fprintf( stderr, "match[k] = %f (mid=%d)\n", match[k], mid ); } } // fprintf( stderr, "done\n" ); return; } #if SLOW static void match_calc_slow( int **which, double ***matrices, double *match, int n1, char **seq1, double *eff1, int n2, char **seq2, double *eff2, int i1, int lgth2, double **doublework, int **intwork, int initialize, int flip ) { // osoi! int i, j, k; int c1, c2; int mid; // fprintf( stderr, "\nmatch_calc_dynamicmtx... %d", i1 ); // fprintf( stderr, "\nseq1[0]=%s\n", seq1[0] ); // fprintf( stderr, "\nseq2[0]=%s\n", seq2[0] ); // for( i=0; i-1 ) *matchpt += scarr[*cpmxpdnpt++] * *cpmxpdpt++; matchpt++; } } free( scarr ); // fprintf( stderr, "done\n" ); #else int j, k, l; // double scarr[26]; double **cpmxpd = doublework; int **cpmxpdn = intwork; double *scarr; scarr = calloc( nalphabets, sizeof( double ) ); // simple if( initialize ) { int count = 0; for( j=0; j-1; k++ ) match[j] += scarr[cpmxpdn[k][j]] * cpmxpd[k][j]; } free( scarr ); #endif } static void match_calc( double **n_dynamicmtx, double *match, double **cpmx1, double **cpmx2, int i1, int lgth2, double **doublework, int **intwork, int initialize ) { #if FASTMATCHCALC // fprintf( stderr, "\nmatch_calc... %d", i1 ); int j, l; // double scarr[26]; double **cpmxpd = doublework; int **cpmxpdn = intwork; double *matchpt, *cpmxpdpt, **cpmxpdptpt; int *cpmxpdnpt, **cpmxpdnptpt; double *scarr; scarr = calloc( nalphabets, sizeof( double ) ); if( initialize ) { int count = 0; for( j=0; j-1 ) *matchpt += scarr[*cpmxpdnpt++] * *cpmxpdpt++; matchpt++; } } free( scarr ); // fprintf( stderr, "done\n" ); #else int j, k, l; // double scarr[26]; double **cpmxpd = doublework; int **cpmxpdn = intwork; double *scarr; scarr = calloc( nalphabets, sizeof( double ) ); // simple if( initialize ) { int count = 0; for( j=0; j-1; k++ ) match[j] += scarr[cpmxpdn[k][j]] * cpmxpd[k][j]; } free( scarr ); #endif } static void Atracking_localhom( double *impwmpt, double *lasthorizontalw, double *lastverticalw, char **seq1, char **seq2, char **mseq1, char **mseq2, int **ijp, int icyc, int jcyc, int *warpis, int *warpjs, int warpbase ) { int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k, limk; double wm; char *gaptable1, *gt1bk; char *gaptable2, *gt2bk; lgth1 = strlen( seq1[0] ); lgth2 = strlen( seq2[0] ); gt1bk = AllocateCharVec( lgth1+lgth2+1 ); gt2bk = AllocateCharVec( lgth1+lgth2+1 ); #if 0 for( i=0; i= wm ) { wm = lastverticalw[i]; iin = i; jin = lgth2-1; ijp[lgth1][lgth2] = +( lgth1 - i ); } } for( j=0; j= wm ) { wm = lasthorizontalw[j]; iin = lgth1-1; jin = j; ijp[lgth1][lgth2] = -( lgth2 - j ); } } } for( i=0; i= warpbase ) { ifi = warpis[ijp[iin][jin]-warpbase]; jfi = warpjs[ijp[iin][jin]-warpbase]; } else if( ijp[iin][jin] < 0 ) { ifi = iin-1; jfi = jin+ijp[iin][jin]; } else if( ijp[iin][jin] > 0 ) { ifi = iin-ijp[iin][jin]; jfi = jin-1; } else { ifi = iin-1; jfi = jin-1; } if( ifi == -warpbase && jfi == -warpbase ) { l = iin; while( --l >= 0 ) { *--gaptable1 = 'o'; *--gaptable2 = '-'; k++; } l= jin; while( --l >= 0 ) { *--gaptable1 = '-'; *--gaptable2 = 'o'; } break; } else { l = iin - ifi; while( --l ) { *--gaptable1 = 'o'; *--gaptable2 = '-'; k++; } l= jin - jfi; while( --l ) { *--gaptable1 = '-'; *--gaptable2 = 'o'; k++; } } if( iin == lgth1 || jin == lgth2 ) ; else { *impwmpt += (double)imp_match_out_scD( iin, jin ); // fprintf( stderr, "impwm = %f (iin=%d, jin=%d) seq1=%c, seq2=%c\n", *impwmpt, iin, jin, seq1[0][iin], seq2[0][jin] ); } if( iin <= 0 || jin <= 0 ) break; *--gaptable1 = 'o'; *--gaptable2 = 'o'; k++; iin = ifi; jin = jfi; } for( i=0; i= wm ) { wm = lastverticalw[i]; iin = i; jin = lgth2-1; ijp[lgth1][lgth2] = +( lgth1 - i ); } } for( j=0; j= wm ) { wm = lasthorizontalw[j]; iin = lgth1-1; jin = j; ijp[lgth1][lgth2] = -( lgth2 - j ); } } } for( i=0; i= warpbase ) { ifi = warpis[ijp[iin][jin]-warpbase]; jfi = warpjs[ijp[iin][jin]-warpbase]; } else if( ijp[iin][jin] < 0 ) { ifi = iin-1; jfi = jin+ijp[iin][jin]; } else if( ijp[iin][jin] > 0 ) { ifi = iin-ijp[iin][jin]; jfi = jin-1; } else { ifi = iin-1; jfi = jin-1; } if( ifi == -warpbase && jfi == -warpbase ) { l = iin; while( --l >= 0 ) { *--gaptable1 = 'o'; *--gaptable2 = '-'; k++; } l= jin; while( --l >= 0 ) { *--gaptable1 = '-'; *--gaptable2 = 'o'; } break; } else { l = iin - ifi; while( --l ) { *--gaptable1 = 'o'; *--gaptable2 = '-'; k++; } l= jin - jfi; while( --l ) { *--gaptable1 = '-'; *--gaptable2 = 'o'; k++; } } if( iin <= 0 || jin <= 0 ) break; *--gaptable1 = 'o'; *--gaptable2 = 'o'; k++; iin = ifi; jin = jfi; } for( i=0; i= first ) { if( *s-- != '-' ) return( v ); v++; } if( s == first-1 ) return( v ); return( -1 ); } static void fillgaplen( Gaplen **mtx, int l ) { int i, j, n, k, len, pos, idatnext; double freq; for( i=0; i<=l; i++ ) { // reporterr( "i=%d\n", i ); if( mtx[i] == NULL ) continue; for( n=0; (len=mtx[i][n].len)!=-1; n++ ) { freq = mtx[i][n].freq; idatnext = n; for( j=0; j 0 ) { if( known[gl] ) { ; } else { nknown++; } } } val += nknown; } free( known ); return( val ); } static void gaplencount( int n, int l, Gaplen **mtx, char **seq, double *eff ) { int i, j, k, gl, *known, nknown; known = calloc( l+1, sizeof( int ) ); // for( i=0; i 0 ) { if( known[gl] ) { // reporterr( "gl=%d, Known!\n", gl ); for( k=0; mtx[j][k].len!=-1; k++ ) if( mtx[j][k].len == gl ) break; if( mtx[j][k].len == -1 ) { reporterr( "Unexpected error!\n" ); exit( 1 ); } mtx[j][k].freq += eff[i]; } else { // reporterr( "gl=%d, First!\n", gl ); mtx[j] = realloc( mtx[j], sizeof( Gaplen ) * (nknown+2) ); mtx[j][nknown].len = gl; mtx[j][nknown].relend = 0; mtx[j][nknown].freq = eff[i]; mtx[j][nknown].idatnext = -2; mtx[j][nknown+1].len = -1; mtx[j][nknown+1].idatnext = -1; mtx[j][nknown+1].relend = -1; mtx[j][nknown+1].freq = 0.0; mtx[j][nknown+1].npat = -1; #if USEGAPLENHALFORMTX mtx[j][nknown].idatend = nknown; mtx[j][nknown+1].idatend = -1; #endif known[gl]++; nknown++; mtx[j][0].npat = nknown; } } } } fillgaplen( mtx, l ); #if 0 reporterr( "Gaplen:\n" ); for( i=0; i<=l; i++ ) { // reporterr( "i=%d, gaplen[i] = %p\n", i, mtx[i] ); if( mtx[i] ) { for( j=0; mtx[i][j].len!=-1; j++ ) reporterr( "i=%d, len = %d, relend = %d, freq = %f\n", i, mtx[i][j].len, mtx[i][j].relend, mtx[i][j].freq ); } } #endif free( known ); } #if DEBUG static void showgaplen( Gaplen **mtx, int seqlen ) { int i, l; #if USEGAPLENHALFORMTX int id, pos; #endif // for( i=0; i<=seqlen; i++ ) for( i=0; ; i++ ) { // reporterr( "chain[%d] = %d\n", i, chain[i] ); if( mtx[i] == NULL ) continue; if( mtx[i] == (Gaplen *)1 ) break; for( l=0; mtx[i][l].idatnext!=-1; l++ ) { #if USEGAPLENHALFORMTX reporterr( "i=%d, l=%d, len=%d, relend=%d, idatend=%d, idatnext=%d, idatprev=%d, freq=%f\n", i, l, mtx[i][l].len, mtx[i][l].relend, mtx[i][l].idatend, mtx[i][l].idatnext, mtx[i][l].idatprev, mtx[i][l].freq ); pos = mtx[i][l].relend; id = mtx[i][l].idatend; if( mtx[i+pos] == NULL ) { // reporterr( "Error in SOURCE\n" ); reporterr( ".len and .freq were lost when i=%d!\n", i ); // exit( 1 ); } #else reporterr( "i=%d, l=%d, len=%d, relend=%d, idatnext=%d, idatprev=%d, freq=%f\n", i, l, mtx[i][l].len, mtx[i][l].relend, mtx[i][l].idatnext, mtx[i][l].idatprev, mtx[i][l].freq ); #endif } } } #endif #if WMCHECK static int pairgapcount( char *s1, char *s2 ) { char **tmpseq; int i, len, st, k; int v = 0; len = strlen( s1 ); tmpseq = calloc( sizeof( char * ), 2 ); tmpseq[0] = malloc( sizeof( char ) * ( len + 1 ) ); tmpseq[1] = malloc( sizeof( char ) * ( len + 1 ) ); strcpy( tmpseq[0], s1 ); strcpy( tmpseq[1], s2 ); commongappick( 2, tmpseq ); len = strlen( tmpseq[0] ); for( k=0; k<2; k++ ) { st = 0; for( i=0; iidatnext!=-1; k++ ) { pos1 = g1->relend; if( pos1 != 0 ) { pfac2 = 0.0; if( gaplen2j ) for( l=0; (g2=gaplen2j+l)->idatnext!=-1; l++ ) { pos2 = g2->relend; if( pos2 == 0 && g2->len >g1->len - (pos1) + newgaplen ) { pfac2 += g2->freq; // reporterr( "hit! pfac2=%f, .freq=%f\n", pfac2, gaplen2[j][l].freq ); } // else // reporterr( "does not hit! pfac2=%f, gaplen1[i][k].len=%d, gaplen[i][k].relend=%d, newgaplen=%d\n", pfac2, gaplen1[i][k].len, gaplen1[i][k].relend, newgaplen ); } pfac += pfac2 * g1->freq; pfac1 += g1->freq; } else if( pos1 == 0 ) { pfac2 = 1.0; if( gaplen2j ) for( l=0; (g2=gaplen2j+l)->idatnext!=-1; l++ ) { pos2 = g2->relend; if( pos2 == 0 && g2->len == g1->len+newgaplen ) pfac2 -= g2->freq;// kokode shuryou suru gap, gaplen1 ha kangaenai. if( pos2 != 0 && g2->len - (pos2-1) > g1->len+newgaplen ) pfac2 -= g2->freq;// keizoku suru gap, gaplen1 ha kangaenai. } // reporterr( "pfac2 in line 1056 = %f\n", pfac2 ); pfac += pfac2 * g1->freq; pfac10 += g1->freq; } } #if DEBUG reporterr( "pfac1 (step2) = %f\n", pfac1 ); reporterr( "pfac10 (step2) = %f\n", pfac10 ); reporterr( "pfac (step2) = %f\n", pfac ); #endif pfac1 = 1.0 - pfac1 - pfac10; pfac2 = 1.0; if( gaplen2j ) for( l=0; (g2=gaplen2j+l)->idatnext!=-1; l++ ) { pos2 = g2->relend; if( pos2 == 0 && g2->len == newgaplen ) pfac2 -= g2->freq;// kokode shuryou suru gap, gaplen1 ha kangaenai. if( pos2 != 0 && g2->len - (pos2-1) > newgaplen ) pfac2 -= g2->freq;// keizoku suru gap, gaplen1 ha kangaenai. } #if DEBUG reporterr( "pfac1 (type3) = %f\n", pfac1 ); reporterr( "pfac2 (type3) = %f\n", pfac2 ); reporterr( "pfac (step3) = %f\n", pfac ); #endif pfac += pfac1 * pfac2; #if DEBUG reporterr( "incomplete pfac = %f, pfac1,pfac2 (%c%d,%c%d) = %f, %f\n", pfac, seq1[i], i, seq2[j], j, pfac1, pfac2 ); #endif return( pfac ); #else double pfac, pfac1, pfac10, pfac2; int k, l, pos1, pos2, id1, id2; Gaplen *gaplen1i, *gaplen2j; #if 0 // .len no shouryaku ni taiou shiteinai int gl; if( disp ) { reporterr( "calcpfac_gap_noidatend, %c (%d) - %c (%d)\n", seq1[i], i, seq2[j], j ); reporterr( "newgaplen = %d\n", newgaplen ); reporterr( "In calcpfac_gap, gaplen1[%d(%c)] = \n", i, seq1[i] ); for( k=0; gaplen1[i]&&(id1=gaplen1[i][k].idatend)!=-1; k++ ) { pos1 = gaplen1[i][k].relend; reporterr( ".len=%d, .relend=%d, .freq=%f\n", gaplen1[i+pos1][id1].len, gaplen1[i][k].relend, gaplen1[i+pos1][id1].freq[0] ); } reporterr( "In calcpfac_gap, gaplen2[%d(%c)] = \n", j, seq2[j] ); showgaplen( gaplen2, strlen(seq2) ); for( k=0; gaplen2[j]&&(id2=gaplen2[j][k].idatend)!=-1; k++ ) { pos2 = gaplen2[j][k].relend; reporterr( ".len=%d, .relend=%d, .freq=%f\n", gaplen2[j+pos2][id2].len, gaplen2[j][k].relend, gaplen2[j+pos2][id2].freq[0] ); } } #endif gaplen2j = gaplen2[j]; gaplen1i = gaplen1[i]; pfac = 0.0; pfac1 = 0.0; pfac10 = 0.0; if( gaplen1i ) for( k=0; (gaplen1i[k].idatnext)!=-1; k++ ) { pos1 = gaplen1i[k].relend; if( pos1 != 0 ) { pfac2 = 0.0; if( gaplen2j ) for( l=0; (gaplen2j[l].idatnext)!=-1; l++ ) { pos2 = gaplen2j[l].relend; if( pos2 == 0 && gaplen2j[l].len > gaplen1i[k].len - (pos1) + newgaplen ) { pfac2 += gaplen2j[l].freq; // reporterr( "hit! pfac2=%f, .freq=%f\n", pfac2, gaplen2[j][l].freq ); } // else // reporterr( "does not hit! pfac2=%f, gaplen1[i][k].len=%d, gaplen[i][k].relend=%d, newgaplen=%d\n", pfac2, gaplen1[i][k].len, gaplen1[i][k].relend, newgaplen ); } pfac += pfac2 * gaplen1i[k].freq; pfac1 += gaplen1i[k].freq; } else if( pos1 == 0 ) { pfac2 = 1.0; if( gaplen2j ) for( l=0; (gaplen2j[l].idatnext)!=-1; l++ ) { pos2 = gaplen2j[l].relend; if( pos2 == 0 && gaplen2j[l].len == gaplen1i[k].len+newgaplen ) pfac2 -= gaplen2j[l].freq;// kokode shuryou suru gap, gaplen1 ha kangaenai. if( pos2 != 0 && gaplen2j[l].len - (pos2-1) > gaplen1i[k].len+newgaplen ) pfac2 -= gaplen2j[l].freq;// keizoku suru gap, gaplen1 ha kangaenai. } // reporterr( "pfac2 in line 1056 = %f\n", pfac2 ); pfac += pfac2 * gaplen1i[k].freq; pfac10 += gaplen1i[k].freq; } } #if DEBUG reporterr( "pfac1 (step2) = %f\n", pfac1 ); reporterr( "pfac10 (step2) = %f\n", pfac10 ); reporterr( "pfac (step2) = %f\n", pfac ); #endif pfac1 = 1.0 - pfac1 - pfac10; pfac2 = 1.0; if( gaplen2j ) for( l=0; (gaplen2j[l].idatnext)!=-1; l++ ) { pos2 = gaplen2j[l].relend; if( pos2 == 0 && gaplen2j[l].len == newgaplen ) pfac2 -= gaplen2j[l].freq;// kokode shuryou suru gap, gaplen1 ha kangaenai. if( pos2 != 0 && gaplen2j[l].len - (pos2-1) > newgaplen ) pfac2 -= gaplen2j[l].freq;// keizoku suru gap, gaplen1 ha kangaenai. } #if DEBUG reporterr( "pfac1 (type3) = %f\n", pfac1 ); reporterr( "pfac2 (type3) = %f\n", pfac2 ); reporterr( "pfac (step3) = %f\n", pfac ); #endif pfac += pfac1 * pfac2; #if DEBUG reporterr( "incomplete pfac = %f, pfac1,pfac2 (%c%d,%c%d) = %f, %f\n", pfac, seq1[i], i, seq2[j], j, pfac1, pfac2 ); #endif return( pfac ); #endif } #if USEGAPLENHALFORMTX static double calcpfac_gap_incomplete( Gaplen **gaplen1, Gaplen **gaplen2, int newgaplen, int i, int j, char *seq1, char *seq2, int disp ) // seq1 to seq2 ha debug you { double pfac, pfac1, pfac10, pfac2; int k, l, pos1, pos2, id1, id2; Gaplen *gapend1, *gapend2; Gaplen *gaplen1i, *gaplen2j; #if 0 // .len no shouryaku ni taiou shiteinai int gl; if( disp ) { reporterr( "calcpfac_gap_incomplete, %c (%d) - %c (%d)\n", seq1[i], i, seq2[j], j ); reporterr( "newgaplen = %d\n", newgaplen ); reporterr( "In calcpfac_gap, gaplen1[%d(%c)] = \n", i, seq1[i] ); for( k=0; gaplen1[i]&&(id1=gaplen1[i][k].idatend)!=-1; k++ ) { pos1 = gaplen1[i][k].relend; reporterr( ".len=%d, .relend=%d, .freq=%f\n", gaplen1[i+pos1][id1].len, gaplen1[i][k].relend, gaplen1[i+pos1][id1].freq[0] ); } reporterr( "In calcpfac_gap, gaplen2[%d(%c)] = \n", j, seq2[j] ); showgaplen( gaplen2, strlen(seq2) ); for( k=0; gaplen2[j]&&(id2=gaplen2[j][k].idatend)!=-1; k++ ) { pos2 = gaplen2[j][k].relend; reporterr( ".len=%d, .relend=%d, .freq=%f\n", gaplen2[j+pos2][id2].len, gaplen2[j][k].relend, gaplen2[j+pos2][id2].freq[0] ); } } #endif gaplen2j = gaplen2[j]; gaplen1i = gaplen1[i]; pfac = 0.0; pfac1 = 0.0; pfac10 = 0.0; if( gaplen1i ) for( k=0; (id1=gaplen1i[k].idatend)!=-1; k++ ) { pos1 = gaplen1i[k].relend; gapend1 = gaplen1[i+pos1]+id1; if( pos1 != 0 ) { pfac2 = 0.0; if( gaplen2j ) for( l=0; (id2=gaplen2j[l].idatend)!=-1; l++ ) { pos2 = gaplen2j[l].relend; gapend2 = gaplen2[j+pos2]+id2; // if( pos2 == 0 && gapend2->len + 1 > gapend1->len - (pos1-1) + newgaplen ) if( pos2 == 0 && gapend2->len > gapend1->len - (pos1) + newgaplen ) { pfac2 += gapend2->freq; // reporterr( "hit! pfac2=%f, .freq=%f\n", pfac2, gaplen2[j][l].freq ); } // else // reporterr( "does not hit! pfac2=%f, gaplen1[i][k].len=%d, gaplen[i][k].relend=%d, newgaplen=%d\n", pfac2, gaplen1[i][k].len, gaplen1[i][k].relend, newgaplen ); } pfac += pfac2 * gapend1->freq; pfac1 += gapend1->freq; } else if( pos1 == 0 ) { pfac2 = 1.0; if( gaplen2j ) for( l=0; (id2=gaplen2j[l].idatend)!=-1; l++ ) { pos2 = gaplen2j[l].relend; gapend2 = gaplen2[j+pos2]+id2; if( pos2 == 0 && gapend2->len == gapend1->len+newgaplen ) pfac2 -= gapend2->freq;// kokode shuryou suru gap, gaplen1 ha kangaenai. if( pos2 != 0 && gapend2->len - (pos2-1) > gapend1->len+newgaplen ) pfac2 -= gapend2->freq;// keizoku suru gap, gaplen1 ha kangaenai. } // reporterr( "pfac2 in line 1056 = %f\n", pfac2 ); pfac += pfac2 * gapend1->freq; pfac10 += gapend1->freq; } } #if DEBUG reporterr( "pfac1 (step2) = %f\n", pfac1 ); reporterr( "pfac10 (step2) = %f\n", pfac10 ); reporterr( "pfac (step2) = %f\n", pfac ); #endif pfac1 = 1.0 - pfac1 - pfac10; pfac2 = 1.0; if( gaplen2j ) for( l=0; (id2=gaplen2j[l].idatend)!=-1; l++ ) { pos2 = gaplen2j[l].relend; gapend2 = gaplen2[j+pos2]+id2; if( pos2 == 0 && gapend2->len == newgaplen ) pfac2 -= gapend2->freq;// kokode shuryou suru gap, gaplen1 ha kangaenai. if( pos2 != 0 && gapend2->len - (pos2-1) > newgaplen ) pfac2 -= gapend2->freq;// keizoku suru gap, gaplen1 ha kangaenai. } #if DEBUG reporterr( "pfac1 (type3) = %f\n", pfac1 ); reporterr( "pfac2 (type3) = %f\n", pfac2 ); reporterr( "pfac (step3) = %f\n", pfac ); #endif pfac += pfac1 * pfac2; #if DEBUG reporterr( "incomplete pfac = %f, pfac1,pfac2 (%c%d,%c%d) = %f, %f\n", pfac, seq1[i], i, seq2[j], j, pfac1, pfac2 ); #endif return( pfac ); } static double calcpfac_gapex( Gaplen **gaplen1, Gaplen **gaplen2, int i, int j, int newgaplen, char *seq1, char *seq2, int disp ) { double pfac, pfac1, pfac2, pfac10; int k, l, id1, id2, pos1, pos2; Gaplen *gapend1, *gapend2; Gaplen *gaplen1i, *gaplen2j; gaplen1i = gaplen1[i]; gaplen2j = gaplen2[j]; pfac = 0.0; pfac2 = 0.0; // for( k=0; gaplen2[j]&&(gl=gaplen2[j][k].len)!=-1; k++ ) // ososugi! hash ni atode henkou if( gaplen2j ) for( k=0; (id2=gaplen2j[k].idatend)!=-1; k++ ) // ososugi! hash ni atode henkou { #if DEBUG int gl; pos2 = gaplen2j[k].relend; id2 = gaplen2j[k].idatend; gl = gaplen2[j+pos2][id2].len; if( disp ) reporterr( "gaplen2[][].len=%d, .relend=%d, .freq=%f\n", gaplen2[j+pos2][id2].len, gaplen2[j][k].relend, gaplen2[j+pos2][id2].freq ); if( disp ) reporterr( "gl = %d, newgaplen=%d\n", gl, newgaplen ); #endif if( (pos2=gaplen2[j][k].relend) != 0 ) continue; gapend2 = gaplen2[j+pos2]+id2; pfac1 = 1.0; pfac10 = 1.0; if( gaplen1i ) for( l=0; (id1=gaplen1i[l].idatend)!=-1; l++ ) // ososugi! hash ni atode henkou { pos1 = gaplen1i[l].relend; gapend1 = gaplen1[i+pos1]+id1; pfac10 -= gapend1->freq; #if DEBUG if( disp ) reporterr( "gaplen1[][].len=%d, .relend=%d, .freq=%f\n", gaplen1[i+pos1][id1].len, gaplen1[i][l].relend, gaplen1[i+pos1][id1].freq ); #endif if( newgaplen + gapend1->len - (pos1) > gapend2->len - (pos2) ) pfac1 -= gapend1->freq; // reporterr( "pfac1 = %f\n", pfac1 ); } pfac += pfac1 * gapend2->freq; /* ???? */ if( newgaplen >= gapend2->len - (pos2-1) ) // >= or >?? { pfac -= pfac10 * gapend2->freq; // reporterr( "Hit! pfac1 = %f\n", pfac1 ); } /* ???? */ // if( gaplen2[j][k].relend == -1 ) pfac += gaplen2[j][k].freq; } return( pfac ); } static double calcpfac( Gaplen **gaplen1, Gaplen **gaplen2, int i, int j, char *seq1, char *seq2, int disp ) // seq1 to seq2 ha debug you { double pfac, pfac1, pfac2; int k, l, pos1, pos2, id1, id2; Gaplen *gapend1, *gapend2; Gaplen *gaplen1i, *gaplen2j; gaplen1i = gaplen1[i]; gaplen2j = gaplen2[j]; #if DEBUG if( disp ) { reporterr( "seq1[0] = %s\n", seq1 ); reporterr( "seq2[0] = %s\n", seq2 ); reporterr( "i,j=%d,%d\n", i, j ); reporterr( "In calcpfac(), gaplen1[%d(%c)] = \n", i, seq1[i] ); // showgaplen( gaplen1, seqlen( seq1 ) ); for( k=0; gaplen1[i]&&(id1=gaplen1[i][k].idatend)!=-1; k++ ) { pos1 = gaplen1[i][k].relend; reporterr( "pos1=%d, id1=%d\n", pos1, id1 ); reporterr( ".len=%d, .relend=%d, .freq=%f\n", gaplen1[i+pos1][id1].len, gaplen1[i][k].relend, gaplen1[i+pos1][id1].freq ); } reporterr( "In calcpfac(), gaplen2[%d(%c)] = \n", j, seq2[j] ); // showgaplen( gaplen2, seqlen( seq2 ) ); for( k=0; gaplen2[j]&&(id2=gaplen2[j][k].idatend)!=-1; k++ ) { pos2 = gaplen2[j][k].relend; reporterr( "j=%d, k=%d, id2=%d, pos2=%d\n", j, k, id2, pos2 ); reporterr( ".len=%d, .relend=%d\n", gaplen2[j+pos2][id2].len, gaplen2[j][k].relend ); reporterr( ".freq=%f\n", gaplen2[j+pos2][id2].freq ); } } #endif pfac1 = pfac2 = 0.0; if( gaplen1i ) for( k=0; (id1=gaplen1i[k].idatend)!=-1; k++ ) { if( (pos1=gaplen1i[k].relend) == 0 ) pfac1 += gaplen1[i+pos1][id1].freq; } if( gaplen2j ) for( l=0; (id2=gaplen2j[l].idatend)!=-1; l++ ) // ososugi! hash ni atode henkou { if( (pos2=gaplen2j[l].relend) == 0 ) pfac2 += gaplen2[j+pos2][id2].freq; } #if DEBUG reporterr( "\n\nInitial pfac1,pfac2 (%c%d,%c%d) = %f, %f\n", seq1[i], i, seq2[j], j, pfac1, pfac2 ); #endif pfac = pfac1 * pfac2 + pfac1 * (1-pfac2) + pfac2 * (1-pfac1); #if DEBUG reporterr( "\n\nInitial pfac (%d,%d) = %f\n", i, j, pfac ); #endif #if 1 // if( pfac ) reporterr( "i,j=%d,%d, Cancel (eq len)? pfac = %f -> ", i, j, pfac ); if( gaplen1i ) for( k=0; (id1=gaplen1i[k].idatend)!=-1; k++ ) // ososugi! hash ni atode henkou { pos1=gaplen1i[k].relend; gapend1 = gaplen1[i+pos1]+id1; if( gaplen2j ) for( l=0; (id2=gaplen2j[l].idatend)!=-1; l++ ) // ososugi! hash ni atode henkou { pos2 = gaplen2j[l].relend; gapend2 = gaplen2[j+pos2]+id2; if ( pos1 == 0 && pos2 == 0 && gapend1->len == gapend2->len ) pfac -= gapend1->freq * gapend2->freq; else if( pos1 == 0 && pos2 != 0 && gapend2->len - (pos2-1) > gapend1->len ) pfac -= gapend1->freq * gapend2->freq; else if( pos1 != 0 && pos2 == 0 && gapend1->len - (pos1-1) > gapend2->len ) pfac -= gapend1->freq * gapend2->freq; } } #if DEBUG reporterr( "\n\nFinal pfac1,pfac2 (%c%d,%c%d, straight) = %f\n\n", seq1[i], i, seq2[j], j, pfac ); #endif #else #endif return( pfac ); } #endif static double calcpfac_gapex_noidatend( Gaplen **gaplen1, Gaplen **gaplen2, int i, int j, int newgaplen, char *seq1, char *seq2, int disp ) { #if 1 double pfac, pfac1, pfac2, pfac10; int k, l, pos1, pos2; Gaplen *gaplen1i, *gaplen2j, *g1, *g2; gaplen1i = gaplen1[i]; gaplen2j = gaplen2[j]; pfac = 0.0; pfac2 = 0.0; if( gaplen2j ) for( k=0; (g2=gaplen2j+k)->idatnext!=-1; k++ ) { #if DEBUG int gl; pos2 = gaplen2j[k].relend; gl = gaplen2j[k].len; if( disp ) reporterr( "gaplen2[][].len=%d, .relend=%d, .freq=%f\n", gaplen2[j][k].len, gaplen2[j][k].relend, gaplen2[j][k].freq ); if( disp ) reporterr( "gl = %d, newgaplen=%d\n", gl, newgaplen ); #endif if( (pos2=g2->relend) != 0 ) continue; pfac1 = 1.0; pfac10 = 1.0; if( gaplen1i ) for( l=0; (g1=gaplen1i+l)->idatnext!=-1; l++ ) { pos1 = g1->relend; pfac10 -= g1->freq; #if DEBUG if( disp ) reporterr( "gaplen1[][].len=%d, .relend=%d, .freq=%f\n", gaplen1[i][l].len, gaplen1[i][l].relend, gaplen1[i][l].freq ); #endif if( newgaplen + g1->len - (pos1) > g2->len - (pos2) ) pfac1 -= g1->freq; // reporterr( "pfac1 = %f\n", pfac1 ); } pfac += pfac1 * g2->freq; /* ???? */ if( newgaplen >= g2->len - (pos2-1) ) // >= or >?? { pfac -= pfac10 * g2->freq; // reporterr( "Hit! pfac1 = %f\n", pfac1 ); } /* ???? */ // if( gaplen2[j][k].relend == -1 ) pfac += gaplen2[j][k].freq; } return( pfac ); #else double pfac, pfac1, pfac2, pfac10; int k, l, id1, id2, pos1, pos2; Gaplen *gaplen1i, *gaplen2j; gaplen1i = gaplen1[i]; gaplen2j = gaplen2[j]; pfac = 0.0; pfac2 = 0.0; // for( k=0; gaplen2[j]&&(gl=gaplen2[j][k].len)!=-1; k++ ) // ososugi! hash ni atode henkou if( gaplen2j ) for( k=0; (gaplen2j[k].idatnext)!=-1; k++ ) // ososugi! hash ni atode henkou { #if DEBUG int gl; pos2 = gaplen2j[k].relend; gl = gaplen2j[k].len; if( disp ) reporterr( "gaplen2[][].len=%d, .relend=%d, .freq=%f\n", gaplen2[j][k].len, gaplen2[j][k].relend, gaplen2[j][k].freq ); if( disp ) reporterr( "gl = %d, newgaplen=%d\n", gl, newgaplen ); #endif if( (pos2=gaplen2[j][k].relend) != 0 ) continue; pfac1 = 1.0; pfac10 = 1.0; if( gaplen1i ) for( l=0; (gaplen1i[l].idatnext)!=-1; l++ ) // ososugi! hash ni atode henkou { pos1 = gaplen1i[l].relend; pfac10 -= gaplen1i[l].freq; #if DEBUG if( disp ) reporterr( "gaplen1[][].len=%d, .relend=%d, .freq=%f\n", gaplen1[i][l].len, gaplen1[i][l].relend, gaplen1[i][l].freq ); #endif if( newgaplen + gaplen1i[l].len - (pos1) > gaplen2j[k].len - (pos2) ) pfac1 -= gaplen1i[l].freq; // reporterr( "pfac1 = %f\n", pfac1 ); } pfac += pfac1 * gaplen2j[k].freq; /* ???? */ if( newgaplen >= gaplen2j[k].len - (pos2-1) ) // >= or >?? { pfac -= pfac10 * gaplen2j[k].freq; // reporterr( "Hit! pfac1 = %f\n", pfac1 ); } /* ???? */ // if( gaplen2[j][k].relend == -1 ) pfac += gaplen2[j][k].freq; } return( pfac ); #endif } static double calcpfacnoidatend( Gaplen **gaplen1, Gaplen **gaplen2, int i, int j, char *seq1, char *seq2, int disp ) // seq1 to seq2 ha debug you { double pfac, pfac1, pfac2; int k, l, pos1, pos2; Gaplen *gaplen1i, *gaplen2j, *g1, *g2; gaplen1i = gaplen1[i]; gaplen2j = gaplen2[j]; #if DEBUG if( disp ) { reporterr( "seq1[0] = %s\n", seq1 ); reporterr( "seq2[0] = %s\n", seq2 ); reporterr( "i,j=%d,%d\n", i, j ); reporterr( "In calcpfacnoidatend(), gaplen1[%d(%c)] = \n", i, seq1[i] ); showgaplen( gaplen1, seqlen( seq1 ) ); for( k=0; gaplen1[i]&&gaplen1[i][k].idatnext!=-1; k++ ) { pos1 = gaplen1[i][k].relend; reporterr( ".len=%d, .relend=%d, .freq=%f (i=%d)\n", gaplen1[i][k].len, gaplen1[i][k].relend, gaplen1[i][k].freq, i ); } reporterr( "In calcpfacnoidatend(), gaplen2[%d(%c)] = \n", j, seq2[j] ); showgaplen( gaplen2, seqlen( seq2 ) ); for( k=0; gaplen2[j]&&gaplen2[j][k].idatnext!=-1; k++ ) { pos2 = gaplen2[j][k].relend; reporterr( ".len=%d, .relend=%d (j=%d)\n", gaplen2[j][k].len, gaplen2[j][k].relend, j ); reporterr( ".freq=%f\n", gaplen2[j][k].freq ); } } #endif #if 1 pfac1 = pfac2 = 0.0; if( gaplen1i ) for( k=0; (g1=gaplen1i+k)->idatnext!=-1; k++ ) { if( (pos1=g1->relend) == 0 ) pfac1 += g1->freq; } if( gaplen2j ) for( l=0; (g2=gaplen2j+l)->idatnext!=-1; l++ ) // ososugi! hash ni atode henkou { if( (pos2=g2->relend) == 0 ) pfac2 += g2->freq; } #if DEBUG reporterr( "\n\nInitial pfac1,pfac2 (%c%d,%c%d) = %f, %f\n", seq1[i], i, seq2[j], j, pfac1, pfac2 ); #endif pfac = pfac1 * pfac2 + pfac1 * (1-pfac2) + pfac2 * (1-pfac1); #if DEBUG reporterr( "\n\nInitial pfac (%d,%d) = %f\n", i, j, pfac ); #endif // if( pfac ) reporterr( "i,j=%d,%d, Cancel (eq len)? pfac = %f -> ", i, j, pfac ); if( gaplen1i ) for( k=0; (g1=gaplen1i+k)->idatnext!=-1; k++ ) // ososugi! hash ni atode henkou { pos1=g1->relend; if( gaplen2j ) for( l=0; (g2=gaplen2j+l)->idatnext!=-1; l++ ) // ososugi! hash ni atode henkou { pos2 = gaplen2j[l].relend; if ( pos1 == 0 && pos2 == 0 && g1->len == g2->len ) pfac -= g1->freq * g2->freq; else if( pos1 == 0 && pos2 != 0 && g2->len - (pos2-1) > g1->len ) pfac -= g1->freq * g2->freq; else if( pos1 != 0 && pos2 == 0 && g1->len - (pos1-1) > g2->len ) pfac -= g1->freq * g2->freq; } } #else pfac1 = pfac2 = 0.0; if( gaplen1i ) for( k=0; (gaplen1i[k].idatnext)!=-1; k++ ) { if( gaplen1i[k].relend == 0 ) pfac1 += gaplen1[i][k].freq; } if( gaplen2j ) for( l=0; (gaplen2j[l].idatnext)!=-1; l++ ) // ososugi! hash ni atode henkou { if( gaplen2j[l].relend == 0 ) pfac2 += gaplen2[j][l].freq; } #if DEBUG reporterr( "\n\nInitial pfac1,pfac2 (%c%d,%c%d) = %f, %f\n", seq1[i], i, seq2[j], j, pfac1, pfac2 ); #endif pfac = pfac1 * pfac2 + pfac1 * (1-pfac2) + pfac2 * (1-pfac1); #if DEBUG reporterr( "\n\nInitial pfac (%d,%d) = %f\n", i, j, pfac ); #endif #if 1 // if( pfac ) reporterr( "i,j=%d,%d, Cancel (eq len)? pfac = %f -> ", i, j, pfac ); if( gaplen1i ) for( k=0; (gaplen1i[k].idatnext)!=-1; k++ ) // ososugi! hash ni atode henkou { pos1=gaplen1i[k].relend; if( gaplen2j ) for( l=0; (gaplen2j[l].idatnext)!=-1; l++ ) // ososugi! hash ni atode henkou { pos2 = gaplen2j[l].relend; if ( pos1 == 0 && pos2 == 0 && gaplen1i[k].len == gaplen2j[l].len ) pfac -= gaplen1i[k].freq * gaplen2j[l].freq; else if( pos1 == 0 && pos2 != 0 && gaplen2j[l].len - (pos2-1) > gaplen1i[k].len ) pfac -= gaplen1i[k].freq * gaplen2j[l].freq; else if( pos1 != 0 && pos2 == 0 && gaplen1i[k].len - (pos1-1) > gaplen2j[l].len ) pfac -= gaplen1i[k].freq * gaplen2j[l].freq; } } #endif #endif #if DEBUG reporterr( "\n\nFinal pfac1,pfac2 (%c%d,%c%d, straight) = %f\n\n", seq1[i], i, seq2[j], j, pfac ); #endif return( pfac ); } static void extendgaplencompactx( Gaplen **cpy, Gaplen **orig, int start ) { Gaplen *opt, *cpt; int l, id; #if DEBUG Gaplen cpybk; #endif // if( start < 0 ) start = 0; if( orig[start] == NULL ) { if( cpy[start] ) { free( cpy[start] ); cpy[start] = NULL; } return; } #if DEBUG reporterr( "At first, cpy -> \n" ); showgaplen( cpy, 100 ); reporterr( "Look at %d \n", start ); #endif if( cpy[start] == NULL ) { l = orig[start][0].npat; cpy[start] = realloc( cpy[start], (l+2) * sizeof( Gaplen ) ); #if 0 for( l=0; (gl=orig[start][l].idatend)!=-1; l++ ) cpy[start][l] = orig[start][l]; // freq ha pointer de copy cpy[start][l] = orig[start][l]; // dekiru? #else for( opt = orig[start],cpt = cpy[start]; opt->idatnext!=-1; ) *cpt++ = *opt++; *cpt = *opt; #endif } #if DEBUG cpybk = cpy[start][0]; #endif #if 0 for( l=0; (opt=orig[start]+l)->idatend!=-1; l++ ) { if( (pos=opt->relend) == 0 ) continue; if( cpy[posplus=start+pos] != NULL ) { id = opt->idatend; // reporterr( "cpy[%d][%d].len: %d -> %d (relend=%d)\n", start, l, cpy[start][l].len, cpy[posplus][id].len, pos ); cpy[start][l].len = cpy[posplus][id].len; // Ato de posplus wo tsukawanaiyouni henkou. continue; // HITSUYOU!!! } else { // reporterr( "cpy[%d][%d].len: %d (relend=%d)\n", start, l, cpy[start][l].len, pos ); } #if 0 for( k=0; orig[start+pos][k].idatend!=-1; k++ ) ; #else optplus = orig[posplus]; k = optplus->npat; #endif cptplus = cpy[posplus] = realloc( cpy[posplus], (k+2) * sizeof( Gaplen ) ); // cptplus = realloc( cptplus, (k+2) * sizeof( Gaplen ) ); #if 0 for( k=0; optplus[k].idatend!=-1; k++ ) { cptplus[k] = optplus[k]; // dekiru? } cptplus[k] = optplus[k]; // dekiru? #else while( optplus->idatend!=-1 ) *cptplus++ = *optplus++; *cptplus = *optplus; #endif } #endif if( start == 0 ) return; if( cpy[start-1] == NULL ) return; #if DEBUG reporterr( "cpy -> \n" ); showgaplen( cpy, 100 ); reporterr( "Look at %d \n", start ); #endif for( l=0; orig[start][l].idatnext!=-1; l++ ) { if( (id=orig[start][l].idatprev) == -1 ) continue; // if( cpy[start][l].relend != 0 ) cpy[start][l].len = cpy[start-1][id].len; // Shinchou ni cpy[start][l].len = cpy[start-1][id].len; // Shinchou ni // if( cpy[start][l].len != cpy[start-1][id].len ) #if DEBUG if( 1 || cpy[start][l].len != cpy[start-1][id].len ) { reporterr( "Check!! cpy[%d][%d].len=%d, but [start-1][].len=%d, relend=%d\n", start, l, cpy[start][l].len, cpy[start-1][id].len, cpy[start][l].relend ); reporterr( "orig[%d][%d].len=%d, relend=%d\n", start, l, orig[start][l].len, orig[start][l].relend ); reporterr( "cpybk.len=%d, relend=%d\n", cpybk.len, cpybk.relend ); } else { // reporterr( "OK, cpy[%d][%d].len=%d, relend=%d\n", start, l, cpy[start][l].len, cpy[start][l].relend ); } #endif } } #if USEGAPLENHALFORMTX static void extendgaplenpartly( Gaplen **cpy, Gaplen **orig, int start, int end ) { int i, l, gl, extrascope; Gaplen *pt; if( start < 0 ) start = 0; // for( i=start; i<=end; i++ ) // { // if( cpy[i] == (Gaplen *)1 ) // { // end = i-1; // break; //// reporterr( "Okashii! i=%d\n", i ); //// exit( 1 ); // } // if( cpy[i] ) free( cpy[i] ); // cpy[i] = NULL; // } extrascope = 0; #if 0 for( i=start; i<=end; i++ ) if( orig[i] ) { for( pt=orig[i]; (pt->idatend)!=-1; ) { if( (gl=pt++->relend) > extrascope ) extrascope = i+gl-end+1; } // extrascope = 10; // Kinji } #else if( orig[end] ) { for( pt=orig[end]; (pt->idatend)!=-1; ) { if( (gl=pt++->relend) > extrascope ) extrascope = gl; } // extrascope = 10; // Kinji } #endif end += extrascope; for( i=start; i<=end; i++ ) { if( cpy[i] != NULL ) continue; if( orig[i] == NULL ) { if( cpy[i] ) free( cpy[i] ); // muda dakedo cpy[i] = NULL; continue; } for( l=0; (gl=orig[i][l].idatend)!=-1; l++ ) ; cpy[i] = realloc( cpy[i], (l+2) * sizeof( Gaplen ) ); // cpy[i] = calloc( sizeof( Gaplen ), l+2 ); for( l=0; (gl=orig[i][l].idatend)!=-1; l++ ) { #if 1 cpy[i][l] = orig[i][l]; // freq ha pointer de copy #else cpy[i][l].len = gl; cpy[i][l].relend = orig[i][l].relend; cpy[i][l].freq = orig[i][l].freq; cpy[i][l].gapidatend = orig[i][l].gapidatend; #endif // reporterr( "i=%d, l=%d, len=%d, freq=%f, relend=%d\n", i, l, cpy[i][l].len, cpy[i][l].freq, cpy[i][l].relend ); } cpy[i][l] = orig[i][l]; // dekiru? // cpy[i][l].relend = -1; // cpy[i][l].len = -1; } } #endif static void duplicategaplencompactx( Gaplen **cpy, Gaplen **orig, int maxlen, int start, int end ) { int i, l; if( start < 0 ) start = 0; for( i=start; i<=end; i++ ) { // reporterr( "i=%d / %d\n", i, maxlen ); if( cpy[i] == (Gaplen *)1 ) { end = i-1; break; // reporterr( "Okashii! i=%d\n", i ); // exit( 1 ); } if( cpy[i] ) free( cpy[i] ); cpy[i] = NULL; } for( i=start; i<=end; i++ ) { if( orig[i] == NULL ) { if( cpy[i] ) free( cpy[i] ); // muda dakedo cpy[i] = NULL; continue; } #if 0 for( l=0; (gl=orig[i][l].idatend)!=-1; l++ ) ; #else l = orig[i][0].npat; #endif cpy[i] = realloc( cpy[i], (l+2) * sizeof( Gaplen ) ); // cpy[i] = calloc( sizeof( Gaplen ), l+2 ); for( l=0; orig[i][l].idatnext!=-1; l++ ) { cpy[i][l] = orig[i][l]; // freq ha pointer de copy // reporterr( "i=%d, l=%d, len=%d, freq=%f, relend=%d\n", i, l, cpy[i][l].len, cpy[i][l].freq, cpy[i][l].relend ); } cpy[i][l] = orig[i][l]; // dekiru? // cpy[i][l].relend = -1; // cpy[i][l].len = -1; } return; } #if USEGAPLENHALFORMTX static void duplicategaplenpartly( Gaplen **cpy, Gaplen **orig, int start, int end ) { int i, l, gl, extrascope; Gaplen *pt; if( start < 0 ) start = 0; for( i=start; i<=end; i++ ) { if( cpy[i] == (Gaplen *)1 ) { end = i-1; break; // reporterr( "Okashii! i=%d\n", i ); // exit( 1 ); } if( cpy[i] ) free( cpy[i] ); cpy[i] = NULL; } extrascope = 0; #if 0 for( i=start; i<=end; i++ ) if( orig[i] ) { for( pt=orig[i]; (pt->idatend)!=-1; ) { if( (gl=pt++->relend) > extrascope ) extrascope = i+gl-end+1; } // extrascope = 10; // Kinji } #else if( orig[end] ) { for( pt=orig[end]; (pt->idatend)!=-1; ) { if( (gl=pt++->relend) > extrascope ) extrascope = gl; } // extrascope = 10; // Kinji } #endif end += extrascope; for( i=start; i<=end; i++ ) { if( orig[i] == NULL ) { if( cpy[i] ) free( cpy[i] ); // muda dakedo cpy[i] = NULL; continue; } for( l=0; (gl=orig[i][l].idatend)!=-1; l++ ) ; cpy[i] = realloc( cpy[i], (l+2) * sizeof( Gaplen ) ); // cpy[i] = calloc( sizeof( Gaplen ), l+2 ); for( l=0; (gl=orig[i][l].idatend)!=-1; l++ ) { #if 1 cpy[i][l] = orig[i][l]; // freq ha pointer de copy #else cpy[i][l].len = gl; cpy[i][l].relend = orig[i][l].relend; cpy[i][l].freq = orig[i][l].freq; cpy[i][l].gapidatend = orig[i][l].gapidatend; #endif // reporterr( "i=%d, l=%d, len=%d, freq=%f, relend=%d\n", i, l, cpy[i][l].len, cpy[i][l].freq, cpy[i][l].relend ); } cpy[i][l] = orig[i][l]; // dekiru? // cpy[i][l].relend = -1; // cpy[i][l].len = -1; } } #endif static void gaplenextendnoidatend( Gaplen **cpy, int gapstartpos, int insertionlen ) { int l, id, idn, pos, len; #if 0 // reporterr( "inserting %d gaps at position %d\n", insertionlen, gapstartpos ); for( l=0; cpy[gapstartpos] && (id=cpy[gapstartpos][l].idatend) !=-1; l++ ) { pos = cpy[gapstartpos][l].relend; cpy[gapstartpos+pos][id].len += insertionlen; } #endif #if 1 for( l=0; cpy[gapstartpos] && (id=cpy[gapstartpos][l].idatnext) !=-1; l++ ) { len = cpy[gapstartpos][l].len + insertionlen; // reporterr( "ext\n" ); for( pos=gapstartpos, idn=l; cpy[pos] != NULL && cpy[pos][idn].relend != 0; pos++ ) { // reporterr( "%d, plus %d %d->%d\n", pos, insertionlen, cpy[pos][idn].len, cpy[pos][idn].len+insertionlen ); cpy[pos][idn].len = len; idn = cpy[pos][idn].idatnext; // if( pos == gapstartpos + 1 ) break; break; } // reporterr( "end\n" ); idn = cpy[gapstartpos][l].idatprev; if( gapstartpos != 0 && idn != -1 && cpy[gapstartpos-1] ) cpy[gapstartpos-1][idn].len = len; } #endif } #if USEGAPLENHALFORMTX static void gaplenextend( Gaplen **cpy, int gapstartpos, int insertionlen ) { int l, id, idn, pos, len; #if 1 // reporterr( "inserting %d gaps at position %d\n", insertionlen, gapstartpos ); for( l=0; cpy[gapstartpos] && (id=cpy[gapstartpos][l].idatend) !=-1; l++ ) { pos = cpy[gapstartpos][l].relend; cpy[gapstartpos+pos][id].len += insertionlen; } #endif #if 1 for( l=0; cpy[gapstartpos] && (id=cpy[gapstartpos][l].idatend) !=-1; l++ ) { len = cpy[gapstartpos][l].len + insertionlen; // reporterr( "ext\n" ); for( pos=gapstartpos, idn=l; cpy[pos] != NULL && cpy[pos][idn].relend != 0; pos++ ) { // reporterr( "%d, plus %d %d->%d\n", pos, insertionlen, cpy[pos][idn].len, cpy[pos][idn].len+insertionlen ); cpy[pos][idn].len = len; idn = cpy[pos][idn].idatnext; // if( pos == gapstartpos + 1 ) break; // break; } // reporterr( "end\n" ); idn = cpy[gapstartpos][l].idatprev; if( gapstartpos != 0 && idn != -1 && cpy[gapstartpos-1] ) cpy[gapstartpos-1][idn].len = len; } #endif } #endif static void copygaplencompactx( Gaplen **cpy, Gaplen **orig, int seqlen, int gapstartpos, int insertionlen, int posincopy, int posinori ) { Gaplen *pt, *cpt; #if DEBUG reporterr( "At the head of copygaplencompactx, cpy=\n" ); showgaplen( cpy+posincopy, 100 ); reporterr( "At the head of copygaplencompactx, orig=\n" ); showgaplen( orig+posinori, 100 ); reporterr( "posinori=%d\n", posinori ); #endif if( orig[posinori] == NULL ) return; // for( pt=orig[posinori],cpt=cpy[posincopy]; pt->relend==0; ) // zenhan ni relend=0 ga matomatteirukara. for( pt=orig[posinori],cpt=cpy[posincopy]; pt->idatnext!=-1; ) // kouhan mo copy { cpt++->len = pt++->len; } #if 0 for( l=0; (id=orig[posinori][l].idatend)!=-1; l++ ) { pos = orig[posinori][l].relend; if( pos == 0 ) continue; if( orig[posinori+pos] == NULL ) { reporterr( "Okashii\n" ); PFACERROR = 1; continue; } #if 0 for( k=0; orig[posinori+pos][k].relend==0; k++ ) // zenhan dake { cpy[posincopy+pos][k].len = orig[posinori+pos][k].len; // dekiru? } #else cpy[posincopy+pos][id].len = orig[posinori+pos][id].len; // dekiru? #endif } #endif if( gapstartpos == -1 ) gapstartpos = posincopy; gaplenextendnoidatend( cpy, gapstartpos, insertionlen ); #if DEBUG reporterr( "At the end of copygaplencompactx, cpy=\n" ); showgaplen( cpy+posincopy, 100 ); #endif } #if USEGAPLENHALF static void copygaplenrestricted_zurasu( Gaplen **cpy, Gaplen **orig, int seqlen, int gapstartpos, int insertionlen, int startincopy, int endincopy, int startinori, int endinori ) { int i, extrascope, gl, j; int zure, newend; Gaplen *pt, *cpt; // int ncopied = 0; #if 0 // mae houkou nimo renzoku gap de enchou suru hitsuyou ga aru to omou. for( i=startinori-1; 0<=i&&i<=seqlen; i-- ) { // reporterr( "i=%d\n", i ); if( orig[i] == NULL ) break; for( pt=orig[i],cpt=cpy[i]; (gl=pt++->len)!=-1; ) cpt++->len = gl; } #endif zure = startincopy - startinori; // end ha check shinai // int ncopied = 0; if( orig[endinori] ) { extrascope = 0; for( pt=orig[endinori]; (pt->idatend)!=-1; ) { if( (gl=pt++->relend) > extrascope ) extrascope = gl; } // extrascope = 10; // Kinji newend = endinori + extrascope; } else newend = endinori; // reporterr( "ncopy = %d\n", newend - startinori ); // #if 0 // extra end wo shizen ni kimereba iranai if( newend > seqlen ) newend = seqlen; // if( startinori < 0 ) startinori = 0; #endif for( i=startinori, j=startincopy; i<=newend; i++, j++ ) { if( orig[i] == NULL ) continue; // ncopied += 1; #if 0 for( pt=orig[i],cpt=cpy[i]; (gl=pt++->len)!=-1; ) cpt++->len = gl; #else for( pt=orig[i],cpt=cpy[j]; pt->relend==0; ) // zenhan ni relend=0 ga matomatteirukara. // int k; // for( k=0; orig[i][k].relend==0; k++ ) // zenhan ni relend=0 ga matomatteirukara. { cpt++->len = pt++->len; // reporterr( "i=%d, k=%d\n", i, k ); // cpy[i][k].len = orig[i][k].len; } #endif } #if 0 for( i=0; i<=seqlen; i++ ) { for( l=0; cpy[i]&&(gl=cpy[i][l].len)!=-1; l++ ) reporterr( "after copy, i=%d, l=%d, len=%d, freq=%f, relend=%d\n", i, l, cpy[i][l].len, cpy[i][l].freq, cpy[i][l].relend ); } #endif if( gapstartpos < 0 ) return; gaplenextend( cpy, gapstartpos, insertionlen ); // return; // TEST // for( i=endinori+1; i<=newend; i++ ) for( i=endincopy+1; i<=newend+zure; i++ ) { if( cpy[i] == NULL ) continue; for( j=0; cpy[i][j].idatend!=-1; j++ ) { if( cpy[i][j].relend == 0 ) { break; } } if( cpy[i][j].idatend == -1 ) { free( cpy[i] ); cpy[i] = NULL; } } #if 0 reporterr( "\n" ); for( i=0; i<=seqlen; i++ ) { for( l=0; cpy[i]&&(gl=cpy[i][l].len)!=-1; l++ ) reporterr( "after add, i=%d, l=%d, len=%d, freq=%f, relend=%d\n", i, l, cpy[i][l].len, cpy[i][l].freq, cpy[i][l].relend ); } #endif } #endif #if USEGAPLENHALFORMTX static void copygaplenrestricted( Gaplen **cpy, Gaplen **orig, int seqlen, int gapstartpos, int insertionlen, int scopestart, int scopeend ) { int i, j, extrascope, gl, endinori, newend; Gaplen *pt, *cpt; // int ncopied = 0; #if 0 // mae houkou nimo renzoku gap de enchou suru hitsuyou ga aru to omou. for( i=scopestart-1; 0<=i&&i<=seqlen; i-- ) { // reporterr( "i=%d\n", i ); if( orig[i] == NULL ) break; for( pt=orig[i],cpt=cpy[i]; (gl=pt++->len)!=-1; ) cpt++->len = gl; } #endif // int ncopied = 0; endinori = scopeend; if( orig[scopeend] ) { extrascope = 0; for( pt=orig[scopeend]; (pt->idatend)!=-1; ) { if( (gl=pt++->relend) > extrascope ) extrascope = gl; } // extrascope = 10; // Kinji scopeend += extrascope; } newend = scopeend; // reporterr( "ncopy = %d\n", scopeend - scopestart ); // #if 0 // extra end wo shizen ni kimereba iranai if( scopeend > seqlen ) scopeend = seqlen; // if( scopestart < 0 ) scopestart = 0; #endif if( scopestart < 0 ) scopestart = 0; for( i=scopestart; i<=scopeend; i++ ) { if( orig[i] == NULL ) continue; // ncopied += 1; #if 0 for( pt=orig[i],cpt=cpy[i]; (gl=pt++->len)!=-1; ) cpt++->len = gl; #else for( pt=orig[i],cpt=cpy[i]; pt->relend==0; ) // zenhan ni relend=0 ga matomatteirukara. // int k; // for( k=0; orig[i][k].relend==0; k++ ) // zenhan ni relend=0 ga matomatteirukara. { cpt++->len = pt++->len; // reporterr( "i=%d, k=%d\n", i, k ); // cpy[i][k].len = orig[i][k].len; } #endif } #if 0 for( i=0; i<=seqlen; i++ ) { for( l=0; cpy[i]&&(gl=cpy[i][l].len)!=-1; l++ ) reporterr( "after copy, i=%d, l=%d, len=%d, freq=%f, relend=%d\n", i, l, cpy[i][l].len, cpy[i][l].freq, cpy[i][l].relend ); } #endif if( gapstartpos < 0 ) return; gaplenextend( cpy, gapstartpos, insertionlen ); return; // TEST extra scope de tsukaunoha end dake? for( i=endinori+1; i<=newend; i++ ) { if( cpy[i] == NULL ) continue; for( j=0; cpy[i][j].idatend!=-1; j++ ) { if( cpy[i][j].relend == 0 ) break; } if( cpy[i][j].idatend == -1 ) { free( cpy[i] ); cpy[i] = NULL; } } #if 0 reporterr( "\n" ); for( i=0; i<=seqlen; i++ ) { for( l=0; cpy[i]&&(gl=cpy[i][l].len)!=-1; l++ ) reporterr( "after add, i=%d, l=%d, len=%d, freq=%f, relend=%d\n", i, l, cpy[i][l].len, cpy[i][l].freq, cpy[i][l].relend ); } #endif } #endif #if 1 static void freegaplenpartly( Gaplen **mtx, int startpos, int endpos ) { int i; Gaplen **pt; if( startpos < 0 ) startpos = 0; for( i=startpos; i<=endpos; i++ ) { if( *(pt=mtx+i) == (Gaplen *)1 ) break; if( *pt ) free( *pt ); *pt = NULL; } } #else static void freegaplenpartly( Gaplen **mtx, int startpos, int endpos ) { int i; if( startpos < 0 ) startpos = 0; for( i=startpos; i<=endpos; i++ ) { if( mtx[i] == (Gaplen *)1 ) break; if( mtx[i] ) free( mtx[i] ); mtx[i] = NULL; } } #endif double D__align( double **n_dynamicmtx, char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen, int constraint, double *impmatch, char *sgap1, char *sgap2, char *egap1, char *egap2, int *chudanpt, int chudanref, int *chudanres, int headgp, int tailgp ) /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */ { // int k; register int i, j; int lasti, lastj; /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */ int lgth1, lgth2; int resultlen; double wm = 0.0; /* int ?????? */ double g; double *currentw, *previousw; // double fpenalty = (double)penalty; #if USE_PENALTY_EX double fpenalty_ex = (double)penalty_ex; #endif #if 1 double *wtmp; int *ijppt; double *mjpt, *prept, *curpt; int *mpjpt; #endif static TLS double mi, *m; static TLS int **ijp; static TLS int mpi, *mp; static TLS double *w1, *w2; static TLS double *match; static TLS double *initverticalw; /* kufuu sureba iranai */ static TLS double *lastverticalw; /* kufuu sureba iranai */ static TLS char **mseq1; static TLS char **mseq2; static TLS char **mseq; static TLS double **cpmx1; static TLS double **cpmx2; static TLS int **intwork; static TLS double **doublework; static TLS int orlgth1 = 0, orlgth2 = 0; #if USEGAPLENHALF Gaplen ****gaplen1half = NULL; // NULL ga iru to omou. Gaplen ****gaplen2half = NULL; // NULL ga iru to omou. #endif #if USEGAPLENMTX Gaplen ****gaplen1mtx = NULL; // NULL ga iru to omou. Gaplen ****gaplen2mtx = NULL; // NULL ga iru to omou. #endif static TLS Gaplen **gaplen1 = NULL; // NULL ga iru to omou. static TLS Gaplen **gaplen2 = NULL; // NULL ga iru to omou. static TLS Gaplen ***gaplen1jprev = NULL; static TLS Gaplen ***gaplen2jprev = NULL; static TLS Gaplen ***gaplen1jcurr = NULL; static TLS Gaplen ***gaplen2jcurr = NULL; static TLS Gaplen ***gaplen1icurr = NULL; static TLS Gaplen ***gaplen2icurr = NULL; static TLS Gaplen ***gaplen1jbestkamo = NULL; static TLS Gaplen ***gaplen2jbestkamo = NULL; static TLS Gaplen ***gaplen1ibestkamo = NULL; static TLS Gaplen ***gaplen2ibestkamo = NULL; static TLS Gaplen ***gaplen1jbest = NULL; static TLS Gaplen ***gaplen2jbest = NULL; double fpenalty = (double)penalty; double fpenalty_shift = (double)penalty_shift; static TLS Gaplen ****gaplens = NULL; Gaplen ***gaplentmp = NULL; int *warpis = NULL; int *warpjs = NULL; int *warpi = NULL; int *warpj = NULL; int *prevwarpi = NULL; int *prevwarpj = NULL; double *wmrecords = NULL; double *prevwmrecords = NULL; int warpn = 0; int warpbase; double curm = 0.0; double *wmrecordspt, *wmrecords1pt, *prevwmrecordspt; int *warpipt, *warpjpt; int k; double pfac, pfactmp; int newgaplen; // for( i=0; i orlgth1 || lgth2 > orlgth2 ) { int ll1, ll2; if( orlgth1 > 0 && orlgth2 > 0 ) { FreeFloatVec( w1 ); FreeFloatVec( w2 ); FreeFloatVec( match ); FreeFloatVec( initverticalw ); FreeFloatVec( lastverticalw ); FreeFloatVec( m ); FreeIntVec( mp ); FreeCharMtx( mseq ); FreeFloatMtx( cpmx1 ); FreeFloatMtx( cpmx2 ); FreeFloatMtx( doublework ); FreeIntMtx( intwork ); free( gaplens ); if( gaplen1ibestkamo ) FreeGaplenCub( gaplen1ibestkamo ); gaplen1ibestkamo = NULL; if( gaplen2ibestkamo ) FreeGaplenCub( gaplen2ibestkamo ); gaplen2ibestkamo = NULL; if( gaplen1icurr ) FreeGaplenCub( gaplen1icurr ); gaplen1icurr = NULL; if( gaplen2icurr ) FreeGaplenCub( gaplen2icurr ); gaplen2icurr = NULL; if( gaplen1jcurr ) FreeGaplenCub( gaplen1jcurr ); gaplen1jcurr = NULL; if( gaplen1jprev ) FreeGaplenCub( gaplen1jprev ); gaplen1jprev = NULL; if( gaplen2jcurr ) FreeGaplenCub( gaplen2jcurr ); gaplen2jcurr = NULL; if( gaplen2jprev ) FreeGaplenCub( gaplen2jprev ); gaplen2jprev = NULL; if( gaplen1jbestkamo ) FreeGaplenCub( gaplen1jbestkamo ); gaplen1jbestkamo = NULL; if( gaplen2jbestkamo ) FreeGaplenCub( gaplen2jbestkamo ); gaplen2jbestkamo = NULL; if( gaplen1jbest ) FreeGaplenCub( gaplen1jbest ); gaplen1jbest = NULL; if( gaplen2jbest ) FreeGaplenCub( gaplen2jbest ); gaplen2jbest = NULL; if( gaplen1 ) FreeGaplenMtx( gaplen1, 1 ); gaplen1 = NULL; if( gaplen2 ) FreeGaplenMtx( gaplen2, 1 ); gaplen2 = NULL; } ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100; ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100; #if DEBUG fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 ); #endif w1 = AllocateFloatVec( ll2+2 ); w2 = AllocateFloatVec( ll2+2 ); match = AllocateFloatVec( ll2+2 ); initverticalw = AllocateFloatVec( ll1+2 ); lastverticalw = AllocateFloatVec( ll1+2 ); m = AllocateFloatVec( ll2+2 ); mp = AllocateIntVec( ll2+2 ); mseq = AllocateCharMtx( njob, ll1+ll2 ); cpmx1 = AllocateFloatMtx( nalphabets, ll1+2 ); cpmx2 = AllocateFloatMtx( nalphabets, ll2+2 ); #if FASTMATCHCALC doublework = AllocateFloatMtx( MAX( ll1, ll2 )+2, nalphabets ); intwork = AllocateIntMtx( MAX( ll1, ll2 )+2, nalphabets+1 ); #else doublework = AllocateFloatMtx( nalphabets, MAX( ll1, ll2 )+2 ); intwork = AllocateIntMtx( nalphabets, MAX( ll1, ll2 )+2 ); #endif #if DEBUG fprintf( stderr, "succeeded\n" ); #endif orlgth1 = ll1 - 100; orlgth2 = ll2 - 100; // reporterr( "Allocating gaplen1 and gaplen2\n" ); gaplen1 = (Gaplen ** )calloc( ll1+2, sizeof( Gaplen * ) ); gaplen1[ll1+1] = (Gaplen *)1; gaplen2 = (Gaplen ** )calloc( ll2+2, sizeof( Gaplen * ) ); gaplen2[ll2+1] = (Gaplen *)1; // reporterr( "Allocating gaplen*\n" ); gaplen1ibestkamo = (Gaplen ***)calloc( (ll1+2), sizeof( Gaplen **) ); gaplen2ibestkamo = (Gaplen ***)calloc( (ll1+2), sizeof( Gaplen **) ); gaplen1icurr = (Gaplen ***)calloc( (ll1+2), sizeof( Gaplen **) ); gaplen2icurr = (Gaplen ***)calloc( (ll1+2), sizeof( Gaplen **) ); gaplen1jbestkamo = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) ); gaplen2jbestkamo = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) ); gaplen1jbest = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) ); gaplen2jbest = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) ); gaplen1jcurr = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) ); gaplen2jcurr = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) ); gaplen1jprev = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) ); gaplen2jprev = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) ); gaplens = calloc( sizeof( Gaplen ***), 12 ); gaplens[0] = gaplen1ibestkamo; gaplens[1] = gaplen2ibestkamo; gaplens[2] = gaplen1icurr; gaplens[3] = gaplen2icurr; gaplens[4] = gaplen1jbestkamo; gaplens[5] = gaplen2jbestkamo; gaplens[6] = gaplen1jbest; gaplens[7] = gaplen2jbest; gaplens[8] = gaplen1jcurr; gaplens[9] = gaplen2jcurr; gaplens[10] = gaplen1jprev; gaplens[11] = gaplen2jprev; // reporterr( "Allocation end\n" ); } { int ll1 = lgth1; int ll2 = lgth2; // reporterr( "Allocating gaplen*i\n" ); for(i=0; i commonAlloc1 || orlgth2 > commonAlloc2 ) { int ll1, ll2; if( commonAlloc1 && commonAlloc2 ) { FreeIntMtx( commonIP ); } ll1 = MAX( orlgth1, commonAlloc1 ); ll2 = MAX( orlgth2, commonAlloc2 ); #if DEBUG fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 ); #endif commonIP = AllocateIntMtx( ll1+10, ll2+10 ); #if DEBUG fprintf( stderr, "succeeded\n\n" ); #endif commonAlloc1 = ll1; commonAlloc2 = ll2; } ijp = commonIP; #if 0 { double t = 0.0; for( i=0; i zurasu -> error? // duplicategaplencompactx( gaplen1ibestkamo[i], gaplen1, lgth1, 0, 1 ); // duplicategaplencompactx( gaplen1ibestkamo[i], gaplen1+i, lgth1-i, 0, 1 ); // half duplicategaplencompactx( gaplen1ibestkamo[i], gaplen1+i, lgth1-i, 0, 0 ); // half // duplicategaplencompactx( gaplen2ibestkamo[i], gaplen2, lgth2, 0, lgth2 ); duplicategaplencompactx( gaplen2ibestkamo[i], gaplen2, lgth2, 0, 0 ); // copygaplenrestricted( gaplen2ibestkamo[i], gaplen2, lgth2, 0, i, 0, 0 ); // -> zurasu -> error? // copygaplenrestricted_zurasu( gaplen2ibestkamo[i], gaplen2, lgth2, 0, i, 0, lgth2, 0, lgth2 ); // -> zurasu -> error? copygaplencompactx( gaplen2ibestkamo[i], gaplen2, lgth2, 0, i, 0, 0 ); // -> zurasu -> error? } // reporterr( "Duplicating gaplen*j*curr \n" ); // int nduplicated = 0; for( j=0; j tbfast.c // impossible if( localhom ) imp_match_calc( n_dynamicmtx, currentw, icyc, jcyc, lgth1, lgth2, seq1, seq2, eff1, eff2, localhom, 1, 0 ); #endif for( j=1; j ", seq1[0][0], seq2[0][j], j, currentw[j] ); currentw[j] += fpenalty * pfac; // tekitou // reporterr( " %f\n", currentw[j] ); } for( i=1; ifull, 2j->half #if USEGAPLENMTX //reporterr( "#### FULL, i,j=%d,%d\n", i, j ); pfactmp = calcpfac( gaplen1mtx[i-1][j-1], gaplen2mtx[i-1][j-1], i, j, seq1[0], seq2[0], one ); #endif #if USEGAPLENHALF //reporterr( "#### HALF, i,j=%d/%d,%d/%d\n", i, lgth1, j, lgth2 ); // showgaplen( gaplen2half[i-1][j-1], lgth2-j ); pfactmp = calcpfac( gaplen1half[i-1][j-1], gaplen2half[i-1][j-1], 1, 1, seq1[0]+i, seq2[0]+j, zero ); #endif #if USEGAPLENMTX + USEGAPLENHALF if( pfac != pfactmp ) { reporterr( "(straight) pfac=%f, but pfactmp=%f (i,j=%d,%d)\n", pfac, pfactmp, i, j ); PFACERROR = 1; exit( 1 ); } #endif //if( i==50 && j==135 ) exit( 1 ); // reporterr( "i,j=%d,%d, *prept = %f\n", i, j, *prept ); #if ALGZSTRAIGHT wm = *prept; // Machigai!! #else wm = *prept + fpenalty * pfac; #endif *ijppt = 0; #if DEBUG if( i == j ) { fprintf( stderr, "\n i=%d, j=%d %c, %c ", i, j, seq1[0][i], seq2[0][j] ); fprintf( stderr, "%5.0f, pfac for straight =%f\n", wm, pfac ); } #endif newgaplen = j-mpi-1; //if( i == 53 && j == 93 ) exit( 1 ); // pfac = calcpfac_gap_incomplete( gaplen1ibestkamo[i-1], gaplen2ibestkamo[i-1], newgaplen, i, j, seq1[0], seq2[0], 0 ); // i-1 pfac = calcpfac_gap_noidatend( gaplen1ibestkamo[i-1], gaplen2ibestkamo[i-1], newgaplen, 1, j, seq1[0]+i-1, seq2[0], 0 ); // i-1 #if USEGAPLENMTX pfactmp = calcpfac_gap_incomplete( gaplen1mtx[i-1][mpi], gaplen2mtx[i-1][mpi], newgaplen, i, j, seq1[0], seq2[0], 1 ); #endif #if USEGAPLENHALF pfactmp = calcpfac_gap_incomplete( gaplen1half[i-1][mpi], gaplen2half[i-1][mpi], newgaplen, 1, j-mpi, seq1[0]+i-1, seq2[0]+mpi, 1 ); #endif #if USEGAPLENMTX || USEGAPLENHALF if( pfac != pfactmp ) { reporterr( "(igap) pfac=%f, but pfactmp=%f (i,j=%d,%d)\n", pfac, pfactmp, i, j ); PFACERROR = 1; } #endif #if DEBUG reporterr( "%c-%c pfac for igap end incomplete = %f\n", seq1[0][i], seq2[0][j], pfac ); reporterr( "mi when igap end checking = %f\n", mi ); reporterr( "wm = %f, mi+fpenalty*pfac=%f\n", wm, mi+fpenalty*pfac ); #endif #if ALGZGAP if( (g=mi+*fgcp2pt*gf1va) > wm ) #else if( (g=mi+fpenalty*pfac) > wm ) #endif { wm = g; *ijppt = -( j - mpi ); #if DEBUG80 reporterr( "Selected as igap end! wm = %f, mi = %f\n", wm, mi ); fprintf( stderr, "Jump from %d-%d (%c-%c) to %d (%c-%c)!\n", i, j, seq1[0][i], seq2[0][j], mpi, seq1[0][i-1], seq2[0][mpi] ); #endif } #if 0 fprintf( stderr, "%5.0f->", wm ); #endif // if( (g=*mjpt+ fgcp1va* *gf2pt) > wm ) #if 0 // reporterr( "Checking %c, (current pos = %c)\n", seq2[0][j+1], seq2[0][j] ); sfac = 1.0; for( k=0; gaplen2[j+1]&&(gl=gaplen2[j+1][k].len)!=-1; k++ ) // ososugi! hash ni atode henkou { // reporterr( ".len = %d, .relend = %d\n", gaplen2[j+1][k].len, gaplen2[j+1][k].relend ); if( gl - 1 == gaplen2[j+1][k].relend ) { sfac -= gaplen2[j+1][k].freq; // reporterr( "Hit! sfac = %f\n", sfac ); } } sfac2 = 1.0; for( k=0; gaplen1[i+1]&&(gl=gaplen1[i+1][k].len)!=-1; k++ ) // ososugi! hash ni atode henkou if( gaplen1[i+1][k].relend != -1 ) sfac2 -= gaplen1[i+1][k].freq; sfac *= sfac2; #else // sfac = 0.0; #endif #if ALGZGAP if( (g=*prept+*ogcp2pt*gf1vapre) >= mi ) #else // if( (g=*prept + fpenalty * sfac ) >= mi ) if( (g=*prept ) >= mi ) #endif { // mpibk = mpi; // mi = g - fpenalty * sfac; mi = g; mpi = j-1; #if DEBUG80 reporterr( "Selected as igap start! %c%d-%c%d, mi=%f, g=%f\n", seq1[0][i-1], i-1, seq2[0][mpi], mpi, mi, g ); #endif #if FREEFREQUENTLY // freegaplenpartly( gaplen1ibestkamo[i-1], 0, i-1 ); freegaplenpartly( gaplen2ibestkamo[i-1], j-3, j-2 ); #endif // freegaplenpartly( gaplen1jprev[mpibk], 0, lgth2 ); // full // freegaplenpartly( gaplen2jprev[mpibk], 0, lgth2-mpibk ); // half // if( gaplen1jprev[mpibk] ) FreeGaplenMtx( gaplen1jprev[mpibk], 0 ); // gaplen1jprev[mpibk] = NULL; // if( gaplen2jprev[mpibk] ) FreeGaplenMtx( gaplen2jprev[mpibk], 0 ); // gaplen2jprev[mpibk] = NULL; // addnewgaplen( gaplen1ibestkamo[i-1], gaplen1jprev[j-1], gaplen1, lgth1, -1, 0 ); // addnewgaplen( gaplen2ibestkamo[i-1], gaplen2jprev[j-1], gaplen2, lgth2, -1, 0 ); // copygaplenrestricted( gaplen1ibestkamo[i-1], gaplen1jprev[j-1], lgth1, -1, 0, i, i ); // i-1, i copygaplencompactx( gaplen1ibestkamo[i-1], gaplen1jprev[j-1], lgth1, -1, 0, 1, i ); // half // copygaplenrestricted( gaplen2ibestkamo[i-1], gaplen2jprev[j-1], lgth2, -1, 0, j, j ); // mpi, j copygaplencompactx( gaplen2ibestkamo[i-1], gaplen2jprev[j-1], lgth2, -1, 0, j, 1 ); //half } // reporterr( "g=%f, *prept=%f, mi=%f\n", g, *prept, mi ); #if USE_PENALTY_EX mi += fpenalty_ex; #endif #if ALGZGAP pfac = 0.0; // CHUUI! #else // pfac = calcpfac_gapex( gaplen1ibestkamo[i-1], gaplen2ibestkamo[i-1], i, j, j-mpi, seq1[0], seq2[0], 1 ); // i-1 pfac = calcpfac_gapex_noidatend( gaplen1ibestkamo[i-1], gaplen2ibestkamo[i-1], 1, j, j-mpi, seq1[0]+i, seq2[0], 1 ); // 1ibest->half, 2ibest->full #if USEGAPLENMTX pfactmp = calcpfac_gapex( gaplen1mtx[i-1][mpi], gaplen2mtx[i-1][mpi], i, j, j-mpi, seq1[0], seq2[0], 1 ); #endif #if USEGAPLENHALF pfactmp = calcpfac_gapex( gaplen1half[i-1][mpi], gaplen2half[i-1][mpi], 1, j-mpi, j-mpi, seq1[0]+i, seq2[0]+mpi, 1 ); #endif #if USEGAPLENMTX || USEGAPLENHALF if( pfac != pfactmp ) { reporterr( "(igapex) pfac=%f, but pfactmp=%f (i,j=%d,%d)\n", pfac, pfactmp, i, j ); PFACERROR = 1; } #endif #if DEBUG reporterr( "%c-%c, igap extension check, pfac = %f\n\n", '=', seq2[0][j], pfac ); #endif #endif // reporterr( "mi = %f -> ", mi ); mi += fpenalty * pfac; // reporterr( "mi = %f\n", mi ); // reporterr( "using %d-%d, %d, %d\n", *mpjpt, j-1, i, j ); newgaplen = i-*mpjpt-1; // pfac = calcpfac_gap_incomplete( gaplen2jbestkamo[j-1], gaplen1jbestkamo[j-1], newgaplen, j, i, seq2[0], seq1[0], 0 ); // j-1 deha??? pfac = calcpfac_gap_noidatend( gaplen2jbestkamo[j-1], gaplen1jbestkamo[j-1], newgaplen, 1, i, seq2[0]+j-1, seq1[0], 1 ); // 2jbestkamo->half, 1jbestkamo->full #if USEGAPLENMTX pfactmp = calcpfac_gap_incomplete( gaplen2mtx[*mpjpt][j-1], gaplen1mtx[*mpjpt][j-1], newgaplen, j, i, seq2[0], seq1[0], 1 ); #endif #if USEGAPLENHALF pfactmp = calcpfac_gap_incomplete( gaplen2half[*mpjpt][j-1], gaplen1half[*mpjpt][j-1], newgaplen, 1, i-*mpjpt, seq2[0]+j-1, seq1[0]+*mpjpt, 1 ); #endif #if USEGAPLENMTX || USEGAPLENHALF if( pfac != pfactmp ) { reporterr( "(jgap) pfac=%f, but pfactmp=%f (i,j=%d,%d)\n", pfac, pfactmp, i, j ); // exit( 1 ); PFACERROR = 1; } #endif #if ALGZGAP if( (g=*mjpt+ fgcp1va* *gf2pt) > wm ) #else if( (g=*mjpt + fpenalty*pfac) > wm ) #endif { wm = g; *ijppt = +( i - *mpjpt ); #if FREEFREQUENTLY freegaplenpartly( gaplen1jbest[j-1], i-3, i-2 ); // freegaplenpartly( gaplen2jbest[j-1], j-3, j-2 ); #endif #if DEBUG reporterr( "Selected as jgap end!, pfac = %f\n", pfac ); fprintf( stderr, "Jump from %d (%c) to %d (%c)!\n", j, seq1[0][j], *mpjpt, seq1[0][*mpjpt] ); #endif // addnewgaplen( gaplen1jbest[j-1], gaplen1jbestkamo[j-1], gaplen1, lgth1, -1, 0 ); // addnewgaplen( gaplen2jbest[j-1], gaplen2jbestkamo[j-1], gaplen2, lgth2, -1, 0 ); copygaplencompactx( gaplen1jbest[j-1], gaplen1jbestkamo[j-1], lgth1, -1, 0, i, i );// *mpjpt, i // copygaplenrestricted( gaplen2jbest[j-1], gaplen2jbestkamo[j-1], lgth2, -1, 0, j, j ); // j-1, j copygaplencompactx( gaplen2jbest[j-1], gaplen2jbestkamo[j-1], lgth2, -1, 0, 1, 1 ); // half! } // extendgaplenpartly( gaplen1jbest[j-1], gaplen1, i, i ); // tmptmptmp // extendgaplenpartly( gaplen2jbest[j-1], gaplen2, 0, 0 ); // tmptmptmp #if 0 sfac = 1.0; for( l=0; gaplen1[i+1]&&(gl=gaplen1[i+1][l].len)!=-1; l++ ) // ososugi! hash ni atode henkou if( gl - 1 == gaplen1[i+1][l].relend ) sfac -= gaplen1[i+1][l].freq; sfac2 = 1.0; for( k=0; gaplen2[j+1]&&(gl=gaplen2[j+1][k].len)!=-1; k++ ) // ososugi! hash ni atode henkou if( gaplen2[j+1][k].relend != -1 ) sfac2 -= gaplen2[j+1][k].freq; sfac *= sfac2; #else // sfac = 0.0; #endif #if DEBUG reporterr( " (jgap start check i=%d) -> *prept=%f, *mjpt=%f\n", i, seq1[0][i], seq2[0][j], *prept, *mjpt ); #endif #if ALGZGAP if( (g=*prept+ ogcp1va* *gf2ptpre) >= *mjpt ) #else // if( (g=*prept + fpenalty * sfac ) >= *mjpt ) if( (g=*prept ) >= *mjpt ) #endif { // *mjpt = g - fpenalty * sfac; *mjpt = g; *mpjpt = i-1; #if DEBUG reporterr( "Selected as jgap start!\n" ); #endif #if FREEFREQUENTLY freegaplenpartly( gaplen1jbestkamo[j-1], i-3, i-2 ); // freegaplenpartly( gaplen2jbestkamo[j-1], j-3, j-2 ); #endif // addnewgaplen( gaplen1jbestkamo[j-1], gaplen1jprev[j-1], gaplen1, lgth1, -1, 0 ); // addnewgaplen( gaplen2jbestkamo[j-1], gaplen2jprev[j-1], gaplen2, lgth2, -1, 0 ); // reporterr( "copying gaplen1jbestkamo[%d-1] from galpen1jprev, j=%d, i=%d\n", j, j, i ); copygaplencompactx( gaplen1jbestkamo[j-1], gaplen1jprev[j-1], lgth1, -1, 0, i, i ); // *mpjpt, i // copygaplenrestricted( gaplen2jbestkamo[j-1], gaplen2jprev[j-1], lgth2, -1, 0, j, j ); // j-1, j // copygaplencompactx( gaplen2jbestkamo[j-1], gaplen2jprev[j-1], lgth2, -1, 0, j, 1 ); // half! // reporterr( "copying gaplen2jbestkamo[%d-1] from galpen2jprev\n", j ); copygaplencompactx( gaplen2jbestkamo[j-1], gaplen2jprev[j-1], lgth2-j, -1, 0, 1, 1 ); // ryouhou half! // if( j==2 && i==1 ) exit( 1 ); } // extendgaplenpartly( gaplen1ibestkamo[i-1], gaplen1, 0, 0 ); // tmptmptmp // extendgaplenpartly( gaplen2ibestkamo[i-1], gaplen2, j, j ); // tmptmptmp // extendgaplenpartly( gaplen1jbestkamo[j-1], gaplen1, i, i ); // tmptmptmp // extendgaplenpartly( gaplen2jbestkamo[j-1], gaplen2, 0, 0 ); // tmptmptmp #if USE_PENALTY_EX m[j] += fpenalty_ex; #endif #if ALGZGAP pfac = 0.0; #else // pfactmp = calcpfac_gapex( gaplen2jbestkamo[j-1], gaplen1jbestkamo[j-1], j, i, i-*mpjpt, seq2[0], seq1[0], 0 ); // j-1 pfactmp = calcpfac_gapex_noidatend( gaplen2jbestkamo[j-1], gaplen1jbestkamo[j-1], 1, i, i-*mpjpt, seq2[0]+j, seq1[0], 0 ); // 2jbestkamo->half, 1jbestkamo->full #if USEGAPLENMTX pfac = calcpfac_gapex( gaplen2mtx[*mpjpt][j-1], gaplen1mtx[*mpjpt][j-1], j, i, i-*mpjpt, seq2[0], seq1[0], 0 ); #endif #if USEGAPLENHALF pfac = calcpfac_gapex( gaplen2half[*mpjpt][j-1], gaplen1half[*mpjpt][j-1], 1, i-*mpjpt, i-*mpjpt, seq2[0]+j, seq1[0]+*mpjpt, 0 ); #endif #if USEGAPLENMTX || USEGAPLENHALF if( pfac != pfactmp ) { reporterr( "(jgapex) pfac=%f, but pfactmp=%f (i,j=%d,%d) diff=%f\n", pfac, pfactmp, i, j, pfac-pfactmp ); // exit( 1 ); PFACERROR = 1; } #endif pfac = pfactmp; #if DEBUG reporterr( "%c-%c, jgap extension check (j=%d), pfac = %f\n", seq1[0][i], '=', j, pfac ); #endif #endif m[j] += fpenalty * pfac; if( trywarp ) { #if USE_PENALTY_EX if( ( g=*prevwmrecordspt++ + fpenalty_shift + fpenalty_ex * ( i - prevwarpi[j-1] + j - prevwarpj[j-1] ) ) > wm ) // naka ha osokute kamawanai #else if( ( g=*prevwmrecordspt++ + fpenalty_shift ) > wm ) // naka ha osokute kamawanai #endif { // fprintf( stderr, "WARP!!\n" ); if( warpn && prevwarpi[j-1] == warpis[warpn-1] && prevwarpj[j-1] == warpjs[warpn-1] ) { *ijppt = warpbase + warpn - 1; } else { *ijppt = warpbase + warpn; warpis = realloc( warpis, sizeof(int) * ( warpn+1 ) ); warpjs = realloc( warpjs, sizeof(int) * ( warpn+1 ) ); warpis[warpn] = prevwarpi[j-1]; warpjs[warpn] = prevwarpj[j-1]; warpn++; } wm = g; } #if 0 fprintf( stderr, "%5.0f ", wm ); #endif curm = *curpt + wm; if( *wmrecords1pt > *wmrecordspt ) { *wmrecordspt = *wmrecords1pt; *warpipt = *(warpipt-1); *warpjpt = *(warpjpt-1); } if( curm > *wmrecordspt ) { *wmrecordspt = curm; *warpipt = i; *warpjpt = j; } wmrecordspt++; wmrecords1pt++; warpipt++; warpjpt++; } #if DEBUG reporterr( "extention-x 1j???, before extention-x, j=%d\n", j ); showgaplen( gaplen1jcurr[j], 100 ); #endif extendgaplencompactx( gaplen1jcurr[j], gaplen1, i ); #if DEBUG reporterr( "after extention-x\n" ); showgaplen( gaplen1jcurr[j], 100 ); reporterr( "extention-x 2j???\n" ); #endif extendgaplencompactx( gaplen2jcurr[j], gaplen2+j, 0 ); #if 1 if( *ijppt < 0 ) { #if DEBUG reporterr( "Path: %d-%d->%d-%d, i=%d (%c), j=%d (%c), *ijppt=%d\n", i, j, i-1, j+*ijppt, i, seq1[0][i], j, seq2[0][j], *ijppt ); reporterr( "Inserting %d gaps to gaplen1 and copying gaplen2 (%c%d-%c%d)\n", -*ijppt-1, seq1[0][i], i, seq2[0][j], j ); #endif #if USEGAPLENMTX // addnewgaplen( gaplen1mtx[i][j], gaplen1mtx[i-1][j+*ijppt], gaplen1, lgth1, i, -*ijppt-1 ); // addnewgaplen( gaplen2mtx[i][j], gaplen2mtx[i-1][j+*ijppt], gaplen2, lgth2, -1, 0 ); copygaplenrestricted( gaplen1mtx[i][j], gaplen1mtx[i-1][j+*ijppt], lgth1, i, -*ijppt-1, i, i ); // 0, lgth1 copygaplenrestricted( gaplen2mtx[i][j], gaplen2mtx[i-1][j+*ijppt], lgth2, -1, 0, j, j ); // 0, lgth2 #endif #if USEGAPLENHALF copygaplenrestricted_zurasu( gaplen1half[i][j], gaplen1half[i-1][j+*ijppt], lgth1, 0, -*ijppt-1, 0, 0, 1, 1 ); // 0, lgth1 copygaplenrestricted_zurasu( gaplen2half[i][j], gaplen2half[i-1][j+*ijppt], lgth2, -1, 0, 0, 0, -*ijppt, -*ijppt ); // 0, lgth2 #endif // addnewgaplen( gaplen1jcurr[j], gaplen1jprev[j+*ijppt], gaplen1, lgth1, i, -*ijppt-1 ); // addnewgaplen( gaplen2jcurr[j], gaplen2jprev[j+*ijppt], gaplen2, lgth2, -1, 0 ); // reporterr( "copying gaplen1jcurr from gaplen1jbest, with a %d insertion\n", -*ijppt-1 ); copygaplencompactx( gaplen1jcurr[j], gaplen1jprev[j+*ijppt], lgth1, i, -*ijppt-1, i, i ); // scope: i+*ijppt+1, i ? // reporterr( "copy end\n" ); // copygaplenrestricted( gaplen2jcurr[j], gaplen2jprev[j+*ijppt], lgth2, -1, 0, j, j ); copygaplencompactx( gaplen2jcurr[j], gaplen2jprev[j+*ijppt], lgth2, -1, 0, 0, -*ijppt ); // half! ryouho zureteru } else if( *ijppt > 0 ) { #if DEBUG reporterr( "Path: %d-%d->%d-%d, i=%d (%c), j=%d (%c), *ijppt=%d\n", i, j, i-*ijppt, j-1, i, seq1[0][i], j, seq2[0][j], *ijppt ); reporterr( "Copying gaplen1 inserting %d gaps to gaplen2 (%c%d-%c%d)\n", *ijppt-1, seq1[0][i], i, seq2[0][j], j ); #endif #if USEGAPLENMTX // addnewgaplen( gaplen1mtx[i][j], gaplen1mtx[i-*ijppt][j-1], gaplen1, lgth1, -1, 0 ); // addnewgaplen( gaplen2mtx[i][j], gaplen2mtx[i-*ijppt][j-1], gaplen2, lgth2, j, *ijppt-1 ); copygaplenrestricted( gaplen1mtx[i][j], gaplen1mtx[i-*ijppt][j-1], lgth1, -1, 0, i, i ); // 0, lgth1 copygaplenrestricted( gaplen2mtx[i][j], gaplen2mtx[i-*ijppt][j-1], lgth2, j, *ijppt-1, j, j ); // 0, lgth2 #endif #if USEGAPLENHALF copygaplenrestricted_zurasu( gaplen1half[i][j], gaplen1half[i-*ijppt][j-1], lgth1, -1, 0, 0, 0, *ijppt, *ijppt ); // 0, lgth1 copygaplenrestricted_zurasu( gaplen2half[i][j], gaplen2half[i-*ijppt][j-1], lgth2, 0, *ijppt-1, 0, 0, 1, 1 ); // 0, lgth2 #endif // addnewgaplen( gaplen1jcurr[j], gaplen1jbest[j-1], gaplen1, lgth1, -1, 0 ); // addnewgaplen( gaplen2jcurr[j], gaplen2jbest[j-1], gaplen2, lgth2, j, *ijppt-1 ); copygaplencompactx( gaplen1jcurr[j], gaplen1jbest[j-1], lgth1, -1, 0, i, i ); // copygaplenrestricted( gaplen2jcurr[j], gaplen2jbest[j-1], lgth2, j, *ijppt-1, j, j ); // j-*ijppt+1? // copygaplenrestricted_zurasu( gaplen2jcurr[j], gaplen2jbest[j-1], lgth2, 0, *ijppt-1, 0, 0, j, j ); // 2jcurr->half, but 2jbest->full, imanotokoro copygaplencompactx( gaplen2jcurr[j], gaplen2jbest[j-1], lgth2, 0, *ijppt-1, 0, 1 ); //ryouhou half } else #endif { #if DEBUG reporterr( "Path: %d-%d->%d-%d, i=%d (%c), j=%d (%c), *ijppt=%d\n", i, j, i-1, j-1, i, seq1[0][i], j, seq2[0][j], *ijppt ); reporterr( "Copying gaplen1 and gaplen2 (%c%d-%c%d)\n", seq1[0][i], i, seq2[0][j], j ); #endif #if USEGAPLENMTX // addnewgaplen( gaplen1mtx[i][j], gaplen1mtx[i-1][j-1], gaplen1, lgth1, -1, 0 ); // addnewgaplen( gaplen2mtx[i][j], gaplen2mtx[i-1][j-1], gaplen2, lgth2, -1, 0 ); copygaplenrestricted( gaplen1mtx[i][j], gaplen1mtx[i-1][j-1], lgth1, -1, 0, i, i ); // 0, lgth1 copygaplenrestricted( gaplen2mtx[i][j], gaplen2mtx[i-1][j-1], lgth2, -1, 0, j, j ); // 0, lgth2 #endif #if USEGAPLENHALF copygaplenrestricted_zurasu( gaplen1half[i][j], gaplen1half[i-1][j-1], lgth1, -1, 0, 0, 0, 1, 1 ); // 0, lgth1 copygaplenrestricted_zurasu( gaplen2half[i][j], gaplen2half[i-1][j-1], lgth2, -1, 0, 0, 0, 1, 1 ); // 0, lgth2 #endif // addnewgaplen( gaplen1jcurr[j], gaplen1jprev[j-1], gaplen1, lgth1, -1, 0 ); // addnewgaplen( gaplen2jcurr[j], gaplen2jprev[j-1], gaplen2, lgth2, -1, 0 ); copygaplencompactx( gaplen1jcurr[j], gaplen1jprev[j-1], lgth1, -1, 0, i, i ); // copygaplenrestricted( gaplen2jcurr[j], gaplen2jprev[j-1], lgth2, -1, 0, j, j ); copygaplencompactx( gaplen2jcurr[j], gaplen2jprev[j-1], lgth2, -1, 0, 0, 1 ); // half } #if DEBUG reporterr( "at the end of j loop, gaplen1jcurr[%d] = \n", j ); showgaplen( gaplen1jcurr[j], 100 ); reporterr( "at the end of j loop, gaplen1prev[%d] = \n", j ); showgaplen( gaplen1jprev[j], 100 ); #endif #if 1 freegaplenpartly( gaplen1jcurr[j-1], i-3, i-2 ); // -1 dehanaku -2?? // freegaplenpartly( gaplen2jcurr[j-1], j-3, j-2 ); // -1 dehanaku -2?? // freegaplenpartly( gaplen2jcurr[j-1], j-3, j-2 ); // half! freegaplenpartly( gaplen1jbestkamo[j-1], i-3, i-2 ); // -1 dehanaku -2?? // freegaplenpartly( gaplen2jbestkamo[j-1], j-3, j-2 ); // -1 dehanaku -2?? freegaplenpartly( gaplen1jbest[j-1], i-3, i-2 ); // -1 dehanaku -2?? // freegaplenpartly( gaplen2jbest[j-1], j-3, j-2 ); // -1 dehanaku -2?? #else freegaplenpartly( gaplen1jprev[j-1], 0, i-2 ); // -1 dehanaku -2?? freegaplenpartly( gaplen1jcurr[j-1], 0, i-2 ); // -1 dehanaku -2?? // freegaplenpartly( gaplen2jcurr[j-1], j-3, j-2 ); // -1 dehanaku -2?? // freegaplenpartly( gaplen2jcurr[j-1], j-3, j-2 ); // half! freegaplenpartly( gaplen1jbestkamo[j-1], 0, i-2 ); // -1 dehanaku -2?? // freegaplenpartly( gaplen2jbestkamo[j-1], j-3, j-2 ); // -1 dehanaku -2?? freegaplenpartly( gaplen1jbest[j-1], 0, i-2 ); // -1 dehanaku -2?? // freegaplenpartly( gaplen2jbest[j-1], j-3, j-2 ); // -1 dehanaku -2?? #endif #if USEGAPLENMTX // freegaplenpartly( gaplen1mtx[i-1][j-1], 0, i-2 ); // freegaplenpartly( gaplen2mtx[i-1][j-1], 0, j-2 ); #endif *curpt++ += wm; ijppt++; mjpt++; prept++; mpjpt++; } lastverticalw[i] = currentw[lgth2-1]; #if 1 // freegaplenpartly( gaplen1icurr[i-1], i-1, i-1 ); freegaplenpartly( gaplen1icurr[i-1], 0, lgth1-i ); freegaplenpartly( gaplen2icurr[i-1], 0, lgth2 ); // freegaplenpartly( gaplen1ibestkamo[i-1], i-1, i-1 ); freegaplenpartly( gaplen1ibestkamo[i-1], 0, lgth1-i ); freegaplenpartly( gaplen2ibestkamo[i-1], 0, lgth2 ); #endif if( trywarp ) { fltncpy( prevwmrecords, wmrecords, lastj ); intncpy( prevwarpi, warpi, lastj ); intncpy( prevwarpj, warpj, lastj ); } #if 0 fprintf( stderr, "i=%d, %15.5f \n", i, wm ); #endif //if( i == 2 ) exit( 1 ); } if( trywarp ) { // fprintf( stderr, "wm = %f\n", wm ); // fprintf( stderr, "warpn = %d\n", warpn ); free( wmrecords ); free( prevwmrecords ); free( warpi ); free( warpj ); free( prevwarpi ); free( prevwarpj ); } #if OUTGAP0TRY if( !outgap ) { for( j=1; j" ); for( i=0; i N ) { fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N ); ErrorExit( "LENGTH OVER!\n" ); } for( i=0; i %f\n", kenzan, (double)kenzan /( icyc*jcyc ) ); double pairscore, nogappairscore; char **pseq; pseq = AllocateCharMtx( 2, strlen( seq1[0] ) + 1 ); pairscore = nogappairscore = 0.0; for( i=0; i 0.01 ) || PFACERROR ) // abs() -> fabs(), 2019/Jan/25 // if( abs( pairscore - wm +*impmatch ) > 0.01 ) #else if( abs( pairscore - wm +*impmatch ) > 0.01 ) #endif // if( abs( pairscore - wm +*impmatch ) > 0.01 ) { for( i=0; igroup1\n%s\n", seq1[i] ); for( j=0; jgroup2\n%s\n", seq2[j] ); exit( 1 ); } #else reporterr( "\n" ); #endif #if 0 // if( strlen( seq1[0] ) - lgth1 > 100 && icyc > 1 || strlen( seq2[0] ) - lgth2 > 100 & jcyc > 1 ) if( strstr( seq1[0], "LNDDAT" ) && icyc == 1 || strstr( seq2[0], "LNDDAT" ) && jcyc==1) { for( i=0; igroup1\n%s\n", seq1[i] ); for( j=0; jgroup2\n%s\n", seq2[j] ); exit( 1 ); } #endif return( wm ); } double D__align_ls( double **n_dynamicmtx, char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen, int constraint, double *impmatch, char *sgap1, char *sgap2, char *egap1, char *egap2, int *chudanpt, int chudanref, int *chudanres, int headgp, int tailgp ) { int v1, v2; double val; #if 1 v1 = gapvariety( icyc, strlen( seq1[0] ), seq1 ); v2 = gapvariety( jcyc, strlen( seq2[0] ), seq2 ); #else v1 = icyc; v2 = jcyc; #endif // reporterr( "\nicyc,jcyc = %d,%d\n", icyc, jcyc ); reporterr( " v1,v2 = %d,%d\n", v1, v2 ); if( v1 >= v2 ) { val = D__align( n_dynamicmtx, seq1, seq2, eff1, eff2, icyc, jcyc, alloclen, constraint, impmatch, sgap1, sgap2, egap1, egap2, chudanpt, chudanref, chudanres, headgp, tailgp ); } else { val = D__align( n_dynamicmtx, seq2, seq1, eff2, eff1, jcyc, icyc, alloclen, constraint, impmatch, sgap2, sgap1, egap2, egap1, chudanpt, chudanref, chudanres, headgp, tailgp ); } return val; } double D__align_gapmap( char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen, int constraint, double *impmatch, int *gapmap1, int *gapmap2 ) /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */ { fprintf( stderr, "Unexpected error. Please contact katoh@ifrec.osaka-u.ac.jp\n" ); exit( 1 ); } double D__align_variousdist( int **which, double ***matrices, double **n_dynamicmtx, char **seq1, char **seq2, double *eff1, double *eff2, double **eff1s, double **eff2s, int icyc, int jcyc, int alloclen, int constraint, double *impmatch, char *sgap1, char *sgap2, char *egap1, char *egap2, int *chudanpt, int chudanref, int *chudanres, int headgp, int tailgp ) /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */ { // int k; register int i, j, c; int lasti, lastj; /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */ int lgth1, lgth2; int resultlen; double wm = 0.0; /* int ?????? */ double g; double *currentw, *previousw; // double fpenalty = (double)penalty; #if USE_PENALTY_EX double fpenalty_ex = (double)penalty_ex; #endif #if 1 double *wtmp; int *ijppt; double *mjpt, *prept, *curpt; int *mpjpt; #endif static TLS double mi, *m; static TLS int **ijp; static TLS int mpi, *mp; static TLS double *w1, *w2; static TLS double *match; static TLS double *initverticalw; /* kufuu sureba iranai */ static TLS double *lastverticalw; /* kufuu sureba iranai */ static TLS char **mseq1; static TLS char **mseq2; static TLS char **mseq; static TLS double ***cpmx1s; static TLS double ***cpmx2s; static TLS int ***intwork; static TLS double ***doublework; static TLS int orlgth1 = 0, orlgth2 = 0; #if USEGAPLENHALF Gaplen ****gaplen1half = NULL; // NULL ga iru to omou. Gaplen ****gaplen2half = NULL; // NULL ga iru to omou. #endif #if USEGAPLENMTX Gaplen ****gaplen1mtx = NULL; // NULL ga iru to omou. Gaplen ****gaplen2mtx = NULL; // NULL ga iru to omou. #endif static TLS Gaplen **gaplen1 = NULL; // NULL ga iru to omou. static TLS Gaplen **gaplen2 = NULL; // NULL ga iru to omou. static TLS Gaplen ***gaplen1jprev = NULL; static TLS Gaplen ***gaplen2jprev = NULL; static TLS Gaplen ***gaplen1jcurr = NULL; static TLS Gaplen ***gaplen2jcurr = NULL; static TLS Gaplen ***gaplen1icurr = NULL; static TLS Gaplen ***gaplen2icurr = NULL; static TLS Gaplen ***gaplen1jbestkamo = NULL; static TLS Gaplen ***gaplen2jbestkamo = NULL; static TLS Gaplen ***gaplen1ibestkamo = NULL; static TLS Gaplen ***gaplen2ibestkamo = NULL; static TLS Gaplen ***gaplen1jbest = NULL; static TLS Gaplen ***gaplen2jbest = NULL; double fpenalty = (double)penalty; double fpenalty_shift = (double)penalty_shift; static TLS Gaplen ****gaplens = NULL; Gaplen ***gaplentmp = NULL; int *warpis = NULL; int *warpjs = NULL; int *warpi = NULL; int *warpj = NULL; int *prevwarpi = NULL; int *prevwarpj = NULL; double *wmrecords = NULL; double *prevwmrecords = NULL; int warpn = 0; int warpbase; double curm = 0.0; double *wmrecordspt, *wmrecords1pt, *prevwmrecordspt; int *warpipt, *warpjpt; int k; double pfac, pfactmp; int newgaplen; int **masklist1 = NULL, **masklist2 = NULL; int *nmask; // for( i=0; i orlgth1 || lgth2 > orlgth2 ) { int ll1, ll2; if( orlgth1 > 0 && orlgth2 > 0 ) { FreeFloatVec( w1 ); FreeFloatVec( w2 ); FreeFloatVec( match ); FreeFloatVec( initverticalw ); FreeFloatVec( lastverticalw ); FreeFloatVec( m ); FreeIntVec( mp ); FreeCharMtx( mseq ); FreeFloatCub( cpmx1s ); FreeFloatCub( cpmx2s ); FreeFloatCub( doublework ); FreeIntCub( intwork ); free( gaplens ); if( gaplen1ibestkamo ) FreeGaplenCub( gaplen1ibestkamo ); gaplen1ibestkamo = NULL; if( gaplen2ibestkamo ) FreeGaplenCub( gaplen2ibestkamo ); gaplen2ibestkamo = NULL; if( gaplen1icurr ) FreeGaplenCub( gaplen1icurr ); gaplen1icurr = NULL; if( gaplen2icurr ) FreeGaplenCub( gaplen2icurr ); gaplen2icurr = NULL; if( gaplen1jcurr ) FreeGaplenCub( gaplen1jcurr ); gaplen1jcurr = NULL; if( gaplen1jprev ) FreeGaplenCub( gaplen1jprev ); gaplen1jprev = NULL; if( gaplen2jcurr ) FreeGaplenCub( gaplen2jcurr ); gaplen2jcurr = NULL; if( gaplen2jprev ) FreeGaplenCub( gaplen2jprev ); gaplen2jprev = NULL; if( gaplen1jbestkamo ) FreeGaplenCub( gaplen1jbestkamo ); gaplen1jbestkamo = NULL; if( gaplen2jbestkamo ) FreeGaplenCub( gaplen2jbestkamo ); gaplen2jbestkamo = NULL; if( gaplen1jbest ) FreeGaplenCub( gaplen1jbest ); gaplen1jbest = NULL; if( gaplen2jbest ) FreeGaplenCub( gaplen2jbest ); gaplen2jbest = NULL; if( gaplen1 ) FreeGaplenMtx( gaplen1, 1 ); gaplen1 = NULL; if( gaplen2 ) FreeGaplenMtx( gaplen2, 1 ); gaplen2 = NULL; } ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100; ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100; #if DEBUG fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 ); #endif w1 = AllocateFloatVec( ll2+2 ); w2 = AllocateFloatVec( ll2+2 ); match = AllocateFloatVec( ll2+2 ); initverticalw = AllocateFloatVec( ll1+2 ); lastverticalw = AllocateFloatVec( ll1+2 ); m = AllocateFloatVec( ll2+2 ); mp = AllocateIntVec( ll2+2 ); mseq = AllocateCharMtx( njob, ll1+ll2 ); cpmx1s = AllocateFloatCub( maxdistclass, nalphabets, ll1+2 ); cpmx2s = AllocateFloatCub( maxdistclass, nalphabets, ll2+2 ); doublework = AllocateFloatCub( maxdistclass, MAX( ll1, ll2 )+2, nalphabets ); intwork = AllocateIntCub( maxdistclass, MAX( ll1, ll2 )+2, nalphabets+1 ); #if DEBUG fprintf( stderr, "succeeded\n" ); #endif orlgth1 = ll1 - 100; orlgth2 = ll2 - 100; // reporterr( "Allocating gaplen1 and gaplen2\n" ); gaplen1 = (Gaplen ** )calloc( ll1+2, sizeof( Gaplen * ) ); gaplen1[ll1+1] = (Gaplen *)1; gaplen2 = (Gaplen ** )calloc( ll2+2, sizeof( Gaplen * ) ); gaplen2[ll2+1] = (Gaplen *)1; // reporterr( "Allocating gaplen*\n" ); gaplen1ibestkamo = (Gaplen ***)calloc( (ll1+2), sizeof( Gaplen **) ); gaplen2ibestkamo = (Gaplen ***)calloc( (ll1+2), sizeof( Gaplen **) ); gaplen1icurr = (Gaplen ***)calloc( (ll1+2), sizeof( Gaplen **) ); gaplen2icurr = (Gaplen ***)calloc( (ll1+2), sizeof( Gaplen **) ); gaplen1jbestkamo = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) ); gaplen2jbestkamo = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) ); gaplen1jbest = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) ); gaplen2jbest = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) ); gaplen1jcurr = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) ); gaplen2jcurr = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) ); gaplen1jprev = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) ); gaplen2jprev = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) ); gaplens = calloc( sizeof( Gaplen ***), 12 ); gaplens[0] = gaplen1ibestkamo; gaplens[1] = gaplen2ibestkamo; gaplens[2] = gaplen1icurr; gaplens[3] = gaplen2icurr; gaplens[4] = gaplen1jbestkamo; gaplens[5] = gaplen2jbestkamo; gaplens[6] = gaplen1jbest; gaplens[7] = gaplen2jbest; gaplens[8] = gaplen1jcurr; gaplens[9] = gaplen2jcurr; gaplens[10] = gaplen1jprev; gaplens[11] = gaplen2jprev; // reporterr( "Allocation end\n" ); } { int ll1 = lgth1; int ll2 = lgth2; // reporterr( "Allocating gaplen*i\n" ); for(i=0; i commonAlloc1 || orlgth2 > commonAlloc2 ) { int ll1, ll2; if( commonAlloc1 && commonAlloc2 ) { FreeIntMtx( commonIP ); } ll1 = MAX( orlgth1, commonAlloc1 ); ll2 = MAX( orlgth2, commonAlloc2 ); #if DEBUG fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 ); #endif commonIP = AllocateIntMtx( ll1+10, ll2+10 ); #if DEBUG fprintf( stderr, "succeeded\n\n" ); #endif commonAlloc1 = ll1; commonAlloc2 = ll2; } ijp = commonIP; #if 0 { double t = 0.0; for( i=0; i zurasu -> error? // duplicategaplencompactx( gaplen1ibestkamo[i], gaplen1, lgth1, 0, 1 ); // duplicategaplencompactx( gaplen1ibestkamo[i], gaplen1+i, lgth1-i, 0, 1 ); // half duplicategaplencompactx( gaplen1ibestkamo[i], gaplen1+i, lgth1-i, 0, 0 ); // half // duplicategaplencompactx( gaplen2ibestkamo[i], gaplen2, lgth2, 0, lgth2 ); duplicategaplencompactx( gaplen2ibestkamo[i], gaplen2, lgth2, 0, 0 ); // copygaplenrestricted( gaplen2ibestkamo[i], gaplen2, lgth2, 0, i, 0, 0 ); // -> zurasu -> error? // copygaplenrestricted_zurasu( gaplen2ibestkamo[i], gaplen2, lgth2, 0, i, 0, lgth2, 0, lgth2 ); // -> zurasu -> error? copygaplencompactx( gaplen2ibestkamo[i], gaplen2, lgth2, 0, i, 0, 0 ); // -> zurasu -> error? } // reporterr( "Duplicating gaplen*j*curr \n" ); // int nduplicated = 0; for( j=0; j tbfast.c // impossible if( localhom ) imp_match_calc( n_dynamicmtx, currentw, icyc, jcyc, lgth1, lgth2, seq1, seq2, eff1, eff2, localhom, 1, 0 ); #endif for( j=1; j ", seq1[0][0], seq2[0][j], j, currentw[j] ); currentw[j] += fpenalty * pfac; // tekitou // reporterr( " %f\n", currentw[j] ); } for( i=1; ifull, 2j->half #if USEGAPLENMTX //reporterr( "#### FULL, i,j=%d,%d\n", i, j ); pfactmp = calcpfac( gaplen1mtx[i-1][j-1], gaplen2mtx[i-1][j-1], i, j, seq1[0], seq2[0], one ); #endif #if USEGAPLENHALF //reporterr( "#### HALF, i,j=%d/%d,%d/%d\n", i, lgth1, j, lgth2 ); // showgaplen( gaplen2half[i-1][j-1], lgth2-j ); pfactmp = calcpfac( gaplen1half[i-1][j-1], gaplen2half[i-1][j-1], 1, 1, seq1[0]+i, seq2[0]+j, zero ); #endif #if USEGAPLENMTX + USEGAPLENHALF if( pfac != pfactmp ) { reporterr( "(straight) pfac=%f, but pfactmp=%f (i,j=%d,%d)\n", pfac, pfactmp, i, j ); PFACERROR = 1; exit( 1 ); } #endif //if( i==50 && j==135 ) exit( 1 ); // reporterr( "i,j=%d,%d, *prept = %f\n", i, j, *prept ); #if ALGZSTRAIGHT wm = *prept; // Machigai!! #else wm = *prept + fpenalty * pfac; #endif *ijppt = 0; #if DEBUG if( i == j ) { fprintf( stderr, "\n i=%d, j=%d %c, %c ", i, j, seq1[0][i], seq2[0][j] ); fprintf( stderr, "%5.0f, pfac for straight =%f\n", wm, pfac ); } #endif newgaplen = j-mpi-1; //if( i == 53 && j == 93 ) exit( 1 ); // pfac = calcpfac_gap_incomplete( gaplen1ibestkamo[i-1], gaplen2ibestkamo[i-1], newgaplen, i, j, seq1[0], seq2[0], 0 ); // i-1 pfac = calcpfac_gap_noidatend( gaplen1ibestkamo[i-1], gaplen2ibestkamo[i-1], newgaplen, 1, j, seq1[0]+i-1, seq2[0], 0 ); // i-1 #if USEGAPLENMTX pfactmp = calcpfac_gap_incomplete( gaplen1mtx[i-1][mpi], gaplen2mtx[i-1][mpi], newgaplen, i, j, seq1[0], seq2[0], 1 ); #endif #if USEGAPLENHALF pfactmp = calcpfac_gap_incomplete( gaplen1half[i-1][mpi], gaplen2half[i-1][mpi], newgaplen, 1, j-mpi, seq1[0]+i-1, seq2[0]+mpi, 1 ); #endif #if USEGAPLENMTX || USEGAPLENHALF if( pfac != pfactmp ) { reporterr( "(igap) pfac=%f, but pfactmp=%f (i,j=%d,%d)\n", pfac, pfactmp, i, j ); PFACERROR = 1; } #endif #if DEBUG reporterr( "%c-%c pfac for igap end incomplete = %f\n", seq1[0][i], seq2[0][j], pfac ); reporterr( "mi when igap end checking = %f\n", mi ); reporterr( "wm = %f, mi+fpenalty*pfac=%f\n", wm, mi+fpenalty*pfac ); #endif #if ALGZGAP if( (g=mi+*fgcp2pt*gf1va) > wm ) #else if( (g=mi+fpenalty*pfac) > wm ) #endif { wm = g; *ijppt = -( j - mpi ); #if DEBUG80 reporterr( "Selected as igap end! wm = %f, mi = %f\n", wm, mi ); fprintf( stderr, "Jump from %d-%d (%c-%c) to %d (%c-%c)!\n", i, j, seq1[0][i], seq2[0][j], mpi, seq1[0][i-1], seq2[0][mpi] ); #endif } #if 0 fprintf( stderr, "%5.0f->", wm ); #endif // if( (g=*mjpt+ fgcp1va* *gf2pt) > wm ) #if 0 // reporterr( "Checking %c, (current pos = %c)\n", seq2[0][j+1], seq2[0][j] ); sfac = 1.0; for( k=0; gaplen2[j+1]&&(gl=gaplen2[j+1][k].len)!=-1; k++ ) // ososugi! hash ni atode henkou { // reporterr( ".len = %d, .relend = %d\n", gaplen2[j+1][k].len, gaplen2[j+1][k].relend ); if( gl - 1 == gaplen2[j+1][k].relend ) { sfac -= gaplen2[j+1][k].freq; // reporterr( "Hit! sfac = %f\n", sfac ); } } sfac2 = 1.0; for( k=0; gaplen1[i+1]&&(gl=gaplen1[i+1][k].len)!=-1; k++ ) // ososugi! hash ni atode henkou if( gaplen1[i+1][k].relend != -1 ) sfac2 -= gaplen1[i+1][k].freq; sfac *= sfac2; #else // sfac = 0.0; #endif #if ALGZGAP if( (g=*prept+*ogcp2pt*gf1vapre) >= mi ) #else // if( (g=*prept + fpenalty * sfac ) >= mi ) if( (g=*prept ) >= mi ) #endif { // mpibk = mpi; // mi = g - fpenalty * sfac; mi = g; mpi = j-1; #if DEBUG80 reporterr( "Selected as igap start! %c%d-%c%d, mi=%f, g=%f\n", seq1[0][i-1], i-1, seq2[0][mpi], mpi, mi, g ); #endif #if FREEFREQUENTLY // freegaplenpartly( gaplen1ibestkamo[i-1], 0, i-1 ); freegaplenpartly( gaplen2ibestkamo[i-1], j-3, j-2 ); #endif // freegaplenpartly( gaplen1jprev[mpibk], 0, lgth2 ); // full // freegaplenpartly( gaplen2jprev[mpibk], 0, lgth2-mpibk ); // half // if( gaplen1jprev[mpibk] ) FreeGaplenMtx( gaplen1jprev[mpibk], 0 ); // gaplen1jprev[mpibk] = NULL; // if( gaplen2jprev[mpibk] ) FreeGaplenMtx( gaplen2jprev[mpibk], 0 ); // gaplen2jprev[mpibk] = NULL; // addnewgaplen( gaplen1ibestkamo[i-1], gaplen1jprev[j-1], gaplen1, lgth1, -1, 0 ); // addnewgaplen( gaplen2ibestkamo[i-1], gaplen2jprev[j-1], gaplen2, lgth2, -1, 0 ); // copygaplenrestricted( gaplen1ibestkamo[i-1], gaplen1jprev[j-1], lgth1, -1, 0, i, i ); // i-1, i copygaplencompactx( gaplen1ibestkamo[i-1], gaplen1jprev[j-1], lgth1, -1, 0, 1, i ); // half // copygaplenrestricted( gaplen2ibestkamo[i-1], gaplen2jprev[j-1], lgth2, -1, 0, j, j ); // mpi, j copygaplencompactx( gaplen2ibestkamo[i-1], gaplen2jprev[j-1], lgth2, -1, 0, j, 1 ); //half } // reporterr( "g=%f, *prept=%f, mi=%f\n", g, *prept, mi ); #if USE_PENALTY_EX mi += fpenalty_ex; #endif #if ALGZGAP pfac = 0.0; // CHUUI! #else // pfac = calcpfac_gapex( gaplen1ibestkamo[i-1], gaplen2ibestkamo[i-1], i, j, j-mpi, seq1[0], seq2[0], 1 ); // i-1 pfac = calcpfac_gapex_noidatend( gaplen1ibestkamo[i-1], gaplen2ibestkamo[i-1], 1, j, j-mpi, seq1[0]+i, seq2[0], 1 ); // 1ibest->half, 2ibest->full #if USEGAPLENMTX pfactmp = calcpfac_gapex( gaplen1mtx[i-1][mpi], gaplen2mtx[i-1][mpi], i, j, j-mpi, seq1[0], seq2[0], 1 ); #endif #if USEGAPLENHALF pfactmp = calcpfac_gapex( gaplen1half[i-1][mpi], gaplen2half[i-1][mpi], 1, j-mpi, j-mpi, seq1[0]+i, seq2[0]+mpi, 1 ); #endif #if USEGAPLENMTX || USEGAPLENHALF if( pfac != pfactmp ) { reporterr( "(igapex) pfac=%f, but pfactmp=%f (i,j=%d,%d)\n", pfac, pfactmp, i, j ); PFACERROR = 1; } #endif #if DEBUG reporterr( "%c-%c, igap extension check, pfac = %f\n\n", '=', seq2[0][j], pfac ); #endif #endif // reporterr( "mi = %f -> ", mi ); mi += fpenalty * pfac; // reporterr( "mi = %f\n", mi ); // reporterr( "using %d-%d, %d, %d\n", *mpjpt, j-1, i, j ); newgaplen = i-*mpjpt-1; // pfac = calcpfac_gap_incomplete( gaplen2jbestkamo[j-1], gaplen1jbestkamo[j-1], newgaplen, j, i, seq2[0], seq1[0], 0 ); // j-1 deha??? pfac = calcpfac_gap_noidatend( gaplen2jbestkamo[j-1], gaplen1jbestkamo[j-1], newgaplen, 1, i, seq2[0]+j-1, seq1[0], 1 ); // 2jbestkamo->half, 1jbestkamo->full #if USEGAPLENMTX pfactmp = calcpfac_gap_incomplete( gaplen2mtx[*mpjpt][j-1], gaplen1mtx[*mpjpt][j-1], newgaplen, j, i, seq2[0], seq1[0], 1 ); #endif #if USEGAPLENHALF pfactmp = calcpfac_gap_incomplete( gaplen2half[*mpjpt][j-1], gaplen1half[*mpjpt][j-1], newgaplen, 1, i-*mpjpt, seq2[0]+j-1, seq1[0]+*mpjpt, 1 ); #endif #if USEGAPLENMTX || USEGAPLENHALF if( pfac != pfactmp ) { reporterr( "(jgap) pfac=%f, but pfactmp=%f (i,j=%d,%d)\n", pfac, pfactmp, i, j ); // exit( 1 ); PFACERROR = 1; } #endif #if ALGZGAP if( (g=*mjpt+ fgcp1va* *gf2pt) > wm ) #else if( (g=*mjpt + fpenalty*pfac) > wm ) #endif { wm = g; *ijppt = +( i - *mpjpt ); #if FREEFREQUENTLY freegaplenpartly( gaplen1jbest[j-1], i-3, i-2 ); // freegaplenpartly( gaplen2jbest[j-1], j-3, j-2 ); #endif #if DEBUG reporterr( "Selected as jgap end!, pfac = %f\n", pfac ); fprintf( stderr, "Jump from %d (%c) to %d (%c)!\n", j, seq1[0][j], *mpjpt, seq1[0][*mpjpt] ); #endif // addnewgaplen( gaplen1jbest[j-1], gaplen1jbestkamo[j-1], gaplen1, lgth1, -1, 0 ); // addnewgaplen( gaplen2jbest[j-1], gaplen2jbestkamo[j-1], gaplen2, lgth2, -1, 0 ); copygaplencompactx( gaplen1jbest[j-1], gaplen1jbestkamo[j-1], lgth1, -1, 0, i, i );// *mpjpt, i // copygaplenrestricted( gaplen2jbest[j-1], gaplen2jbestkamo[j-1], lgth2, -1, 0, j, j ); // j-1, j copygaplencompactx( gaplen2jbest[j-1], gaplen2jbestkamo[j-1], lgth2, -1, 0, 1, 1 ); // half! } // extendgaplenpartly( gaplen1jbest[j-1], gaplen1, i, i ); // tmptmptmp // extendgaplenpartly( gaplen2jbest[j-1], gaplen2, 0, 0 ); // tmptmptmp #if 0 sfac = 1.0; for( l=0; gaplen1[i+1]&&(gl=gaplen1[i+1][l].len)!=-1; l++ ) // ososugi! hash ni atode henkou if( gl - 1 == gaplen1[i+1][l].relend ) sfac -= gaplen1[i+1][l].freq; sfac2 = 1.0; for( k=0; gaplen2[j+1]&&(gl=gaplen2[j+1][k].len)!=-1; k++ ) // ososugi! hash ni atode henkou if( gaplen2[j+1][k].relend != -1 ) sfac2 -= gaplen2[j+1][k].freq; sfac *= sfac2; #else // sfac = 0.0; #endif #if DEBUG reporterr( " (jgap start check i=%d) -> *prept=%f, *mjpt=%f\n", i, seq1[0][i], seq2[0][j], *prept, *mjpt ); #endif #if ALGZGAP if( (g=*prept+ ogcp1va* *gf2ptpre) >= *mjpt ) #else // if( (g=*prept + fpenalty * sfac ) >= *mjpt ) if( (g=*prept ) >= *mjpt ) #endif { // *mjpt = g - fpenalty * sfac; *mjpt = g; *mpjpt = i-1; #if DEBUG reporterr( "Selected as jgap start!\n" ); #endif #if FREEFREQUENTLY freegaplenpartly( gaplen1jbestkamo[j-1], i-3, i-2 ); // freegaplenpartly( gaplen2jbestkamo[j-1], j-3, j-2 ); #endif // addnewgaplen( gaplen1jbestkamo[j-1], gaplen1jprev[j-1], gaplen1, lgth1, -1, 0 ); // addnewgaplen( gaplen2jbestkamo[j-1], gaplen2jprev[j-1], gaplen2, lgth2, -1, 0 ); // reporterr( "copying gaplen1jbestkamo[%d-1] from galpen1jprev, j=%d, i=%d\n", j, j, i ); copygaplencompactx( gaplen1jbestkamo[j-1], gaplen1jprev[j-1], lgth1, -1, 0, i, i ); // *mpjpt, i // copygaplenrestricted( gaplen2jbestkamo[j-1], gaplen2jprev[j-1], lgth2, -1, 0, j, j ); // j-1, j // copygaplencompactx( gaplen2jbestkamo[j-1], gaplen2jprev[j-1], lgth2, -1, 0, j, 1 ); // half! // reporterr( "copying gaplen2jbestkamo[%d-1] from galpen2jprev\n", j ); copygaplencompactx( gaplen2jbestkamo[j-1], gaplen2jprev[j-1], lgth2-j, -1, 0, 1, 1 ); // ryouhou half! // if( j==2 && i==1 ) exit( 1 ); } // extendgaplenpartly( gaplen1ibestkamo[i-1], gaplen1, 0, 0 ); // tmptmptmp // extendgaplenpartly( gaplen2ibestkamo[i-1], gaplen2, j, j ); // tmptmptmp // extendgaplenpartly( gaplen1jbestkamo[j-1], gaplen1, i, i ); // tmptmptmp // extendgaplenpartly( gaplen2jbestkamo[j-1], gaplen2, 0, 0 ); // tmptmptmp #if USE_PENALTY_EX m[j] += fpenalty_ex; #endif #if ALGZGAP pfac = 0.0; #else // pfactmp = calcpfac_gapex( gaplen2jbestkamo[j-1], gaplen1jbestkamo[j-1], j, i, i-*mpjpt, seq2[0], seq1[0], 0 ); // j-1 pfactmp = calcpfac_gapex_noidatend( gaplen2jbestkamo[j-1], gaplen1jbestkamo[j-1], 1, i, i-*mpjpt, seq2[0]+j, seq1[0], 0 ); // 2jbestkamo->half, 1jbestkamo->full #if USEGAPLENMTX pfac = calcpfac_gapex( gaplen2mtx[*mpjpt][j-1], gaplen1mtx[*mpjpt][j-1], j, i, i-*mpjpt, seq2[0], seq1[0], 0 ); #endif #if USEGAPLENHALF pfac = calcpfac_gapex( gaplen2half[*mpjpt][j-1], gaplen1half[*mpjpt][j-1], 1, i-*mpjpt, i-*mpjpt, seq2[0]+j, seq1[0]+*mpjpt, 0 ); #endif #if USEGAPLENMTX || USEGAPLENHALF if( pfac != pfactmp ) { reporterr( "(jgapex) pfac=%f, but pfactmp=%f (i,j=%d,%d) diff=%f\n", pfac, pfactmp, i, j, pfac-pfactmp ); // exit( 1 ); PFACERROR = 1; } #endif pfac = pfactmp; #if DEBUG reporterr( "%c-%c, jgap extension check (j=%d), pfac = %f\n", seq1[0][i], '=', j, pfac ); #endif #endif m[j] += fpenalty * pfac; if( trywarp ) { #if USE_PENALTY_EX if( ( g=*prevwmrecordspt++ + fpenalty_shift + fpenalty_ex * ( i - prevwarpi[j-1] + j - prevwarpj[j-1] ) ) > wm ) // naka ha osokute kamawanai #else if( ( g=*prevwmrecordspt++ + fpenalty_shift ) > wm ) // naka ha osokute kamawanai #endif { // fprintf( stderr, "WARP!!\n" ); if( warpn && prevwarpi[j-1] == warpis[warpn-1] && prevwarpj[j-1] == warpjs[warpn-1] ) { *ijppt = warpbase + warpn - 1; } else { *ijppt = warpbase + warpn; warpis = realloc( warpis, sizeof(int) * ( warpn+1 ) ); warpjs = realloc( warpjs, sizeof(int) * ( warpn+1 ) ); warpis[warpn] = prevwarpi[j-1]; warpjs[warpn] = prevwarpj[j-1]; warpn++; } wm = g; } #if 0 fprintf( stderr, "%5.0f ", wm ); #endif curm = *curpt + wm; if( *wmrecords1pt > *wmrecordspt ) { *wmrecordspt = *wmrecords1pt; *warpipt = *(warpipt-1); *warpjpt = *(warpjpt-1); } if( curm > *wmrecordspt ) { *wmrecordspt = curm; *warpipt = i; *warpjpt = j; } wmrecordspt++; wmrecords1pt++; warpipt++; warpjpt++; } #if DEBUG reporterr( "extention-x 1j???, before extention-x, j=%d\n", j ); showgaplen( gaplen1jcurr[j], 100 ); #endif extendgaplencompactx( gaplen1jcurr[j], gaplen1, i ); #if DEBUG reporterr( "after extention-x\n" ); showgaplen( gaplen1jcurr[j], 100 ); reporterr( "extention-x 2j???\n" ); #endif extendgaplencompactx( gaplen2jcurr[j], gaplen2+j, 0 ); #if 1 if( *ijppt < 0 ) { #if DEBUG reporterr( "Path: %d-%d->%d-%d, i=%d (%c), j=%d (%c), *ijppt=%d\n", i, j, i-1, j+*ijppt, i, seq1[0][i], j, seq2[0][j], *ijppt ); reporterr( "Inserting %d gaps to gaplen1 and copying gaplen2 (%c%d-%c%d)\n", -*ijppt-1, seq1[0][i], i, seq2[0][j], j ); #endif #if USEGAPLENMTX // addnewgaplen( gaplen1mtx[i][j], gaplen1mtx[i-1][j+*ijppt], gaplen1, lgth1, i, -*ijppt-1 ); // addnewgaplen( gaplen2mtx[i][j], gaplen2mtx[i-1][j+*ijppt], gaplen2, lgth2, -1, 0 ); copygaplenrestricted( gaplen1mtx[i][j], gaplen1mtx[i-1][j+*ijppt], lgth1, i, -*ijppt-1, i, i ); // 0, lgth1 copygaplenrestricted( gaplen2mtx[i][j], gaplen2mtx[i-1][j+*ijppt], lgth2, -1, 0, j, j ); // 0, lgth2 #endif #if USEGAPLENHALF copygaplenrestricted_zurasu( gaplen1half[i][j], gaplen1half[i-1][j+*ijppt], lgth1, 0, -*ijppt-1, 0, 0, 1, 1 ); // 0, lgth1 copygaplenrestricted_zurasu( gaplen2half[i][j], gaplen2half[i-1][j+*ijppt], lgth2, -1, 0, 0, 0, -*ijppt, -*ijppt ); // 0, lgth2 #endif // addnewgaplen( gaplen1jcurr[j], gaplen1jprev[j+*ijppt], gaplen1, lgth1, i, -*ijppt-1 ); // addnewgaplen( gaplen2jcurr[j], gaplen2jprev[j+*ijppt], gaplen2, lgth2, -1, 0 ); // reporterr( "copying gaplen1jcurr from gaplen1jbest, with a %d insertion\n", -*ijppt-1 ); copygaplencompactx( gaplen1jcurr[j], gaplen1jprev[j+*ijppt], lgth1, i, -*ijppt-1, i, i ); // scope: i+*ijppt+1, i ? // reporterr( "copy end\n" ); // copygaplenrestricted( gaplen2jcurr[j], gaplen2jprev[j+*ijppt], lgth2, -1, 0, j, j ); copygaplencompactx( gaplen2jcurr[j], gaplen2jprev[j+*ijppt], lgth2, -1, 0, 0, -*ijppt ); // half! ryouho zureteru } else if( *ijppt > 0 ) { #if DEBUG reporterr( "Path: %d-%d->%d-%d, i=%d (%c), j=%d (%c), *ijppt=%d\n", i, j, i-*ijppt, j-1, i, seq1[0][i], j, seq2[0][j], *ijppt ); reporterr( "Copying gaplen1 inserting %d gaps to gaplen2 (%c%d-%c%d)\n", *ijppt-1, seq1[0][i], i, seq2[0][j], j ); #endif #if USEGAPLENMTX // addnewgaplen( gaplen1mtx[i][j], gaplen1mtx[i-*ijppt][j-1], gaplen1, lgth1, -1, 0 ); // addnewgaplen( gaplen2mtx[i][j], gaplen2mtx[i-*ijppt][j-1], gaplen2, lgth2, j, *ijppt-1 ); copygaplenrestricted( gaplen1mtx[i][j], gaplen1mtx[i-*ijppt][j-1], lgth1, -1, 0, i, i ); // 0, lgth1 copygaplenrestricted( gaplen2mtx[i][j], gaplen2mtx[i-*ijppt][j-1], lgth2, j, *ijppt-1, j, j ); // 0, lgth2 #endif #if USEGAPLENHALF copygaplenrestricted_zurasu( gaplen1half[i][j], gaplen1half[i-*ijppt][j-1], lgth1, -1, 0, 0, 0, *ijppt, *ijppt ); // 0, lgth1 copygaplenrestricted_zurasu( gaplen2half[i][j], gaplen2half[i-*ijppt][j-1], lgth2, 0, *ijppt-1, 0, 0, 1, 1 ); // 0, lgth2 #endif // addnewgaplen( gaplen1jcurr[j], gaplen1jbest[j-1], gaplen1, lgth1, -1, 0 ); // addnewgaplen( gaplen2jcurr[j], gaplen2jbest[j-1], gaplen2, lgth2, j, *ijppt-1 ); copygaplencompactx( gaplen1jcurr[j], gaplen1jbest[j-1], lgth1, -1, 0, i, i ); // copygaplenrestricted( gaplen2jcurr[j], gaplen2jbest[j-1], lgth2, j, *ijppt-1, j, j ); // j-*ijppt+1? // copygaplenrestricted_zurasu( gaplen2jcurr[j], gaplen2jbest[j-1], lgth2, 0, *ijppt-1, 0, 0, j, j ); // 2jcurr->half, but 2jbest->full, imanotokoro copygaplencompactx( gaplen2jcurr[j], gaplen2jbest[j-1], lgth2, 0, *ijppt-1, 0, 1 ); //ryouhou half } else #endif { #if DEBUG reporterr( "Path: %d-%d->%d-%d, i=%d (%c), j=%d (%c), *ijppt=%d\n", i, j, i-1, j-1, i, seq1[0][i], j, seq2[0][j], *ijppt ); reporterr( "Copying gaplen1 and gaplen2 (%c%d-%c%d)\n", seq1[0][i], i, seq2[0][j], j ); #endif #if USEGAPLENMTX // addnewgaplen( gaplen1mtx[i][j], gaplen1mtx[i-1][j-1], gaplen1, lgth1, -1, 0 ); // addnewgaplen( gaplen2mtx[i][j], gaplen2mtx[i-1][j-1], gaplen2, lgth2, -1, 0 ); copygaplenrestricted( gaplen1mtx[i][j], gaplen1mtx[i-1][j-1], lgth1, -1, 0, i, i ); // 0, lgth1 copygaplenrestricted( gaplen2mtx[i][j], gaplen2mtx[i-1][j-1], lgth2, -1, 0, j, j ); // 0, lgth2 #endif #if USEGAPLENHALF copygaplenrestricted_zurasu( gaplen1half[i][j], gaplen1half[i-1][j-1], lgth1, -1, 0, 0, 0, 1, 1 ); // 0, lgth1 copygaplenrestricted_zurasu( gaplen2half[i][j], gaplen2half[i-1][j-1], lgth2, -1, 0, 0, 0, 1, 1 ); // 0, lgth2 #endif // addnewgaplen( gaplen1jcurr[j], gaplen1jprev[j-1], gaplen1, lgth1, -1, 0 ); // addnewgaplen( gaplen2jcurr[j], gaplen2jprev[j-1], gaplen2, lgth2, -1, 0 ); copygaplencompactx( gaplen1jcurr[j], gaplen1jprev[j-1], lgth1, -1, 0, i, i ); // copygaplenrestricted( gaplen2jcurr[j], gaplen2jprev[j-1], lgth2, -1, 0, j, j ); copygaplencompactx( gaplen2jcurr[j], gaplen2jprev[j-1], lgth2, -1, 0, 0, 1 ); // half } #if DEBUG reporterr( "at the end of j loop, gaplen1jcurr[%d] = \n", j ); showgaplen( gaplen1jcurr[j], 100 ); reporterr( "at the end of j loop, gaplen1prev[%d] = \n", j ); showgaplen( gaplen1jprev[j], 100 ); #endif #if 1 freegaplenpartly( gaplen1jcurr[j-1], i-3, i-2 ); // -1 dehanaku -2?? // freegaplenpartly( gaplen2jcurr[j-1], j-3, j-2 ); // -1 dehanaku -2?? // freegaplenpartly( gaplen2jcurr[j-1], j-3, j-2 ); // half! freegaplenpartly( gaplen1jbestkamo[j-1], i-3, i-2 ); // -1 dehanaku -2?? // freegaplenpartly( gaplen2jbestkamo[j-1], j-3, j-2 ); // -1 dehanaku -2?? freegaplenpartly( gaplen1jbest[j-1], i-3, i-2 ); // -1 dehanaku -2?? // freegaplenpartly( gaplen2jbest[j-1], j-3, j-2 ); // -1 dehanaku -2?? #else freegaplenpartly( gaplen1jprev[j-1], 0, i-2 ); // -1 dehanaku -2?? freegaplenpartly( gaplen1jcurr[j-1], 0, i-2 ); // -1 dehanaku -2?? // freegaplenpartly( gaplen2jcurr[j-1], j-3, j-2 ); // -1 dehanaku -2?? // freegaplenpartly( gaplen2jcurr[j-1], j-3, j-2 ); // half! freegaplenpartly( gaplen1jbestkamo[j-1], 0, i-2 ); // -1 dehanaku -2?? // freegaplenpartly( gaplen2jbestkamo[j-1], j-3, j-2 ); // -1 dehanaku -2?? freegaplenpartly( gaplen1jbest[j-1], 0, i-2 ); // -1 dehanaku -2?? // freegaplenpartly( gaplen2jbest[j-1], j-3, j-2 ); // -1 dehanaku -2?? #endif #if USEGAPLENMTX // freegaplenpartly( gaplen1mtx[i-1][j-1], 0, i-2 ); // freegaplenpartly( gaplen2mtx[i-1][j-1], 0, j-2 ); #endif *curpt++ += wm; ijppt++; mjpt++; prept++; mpjpt++; } lastverticalw[i] = currentw[lgth2-1]; #if 1 // freegaplenpartly( gaplen1icurr[i-1], i-1, i-1 ); freegaplenpartly( gaplen1icurr[i-1], 0, lgth1-i ); freegaplenpartly( gaplen2icurr[i-1], 0, lgth2 ); // freegaplenpartly( gaplen1ibestkamo[i-1], i-1, i-1 ); freegaplenpartly( gaplen1ibestkamo[i-1], 0, lgth1-i ); freegaplenpartly( gaplen2ibestkamo[i-1], 0, lgth2 ); #endif if( trywarp ) { fltncpy( prevwmrecords, wmrecords, lastj ); intncpy( prevwarpi, warpi, lastj ); intncpy( prevwarpj, warpj, lastj ); } #if 0 fprintf( stderr, "i=%d, %15.5f \n", i, wm ); #endif //if( i == 2 ) exit( 1 ); } if( trywarp ) { // fprintf( stderr, "wm = %f\n", wm ); // fprintf( stderr, "warpn = %d\n", warpn ); free( wmrecords ); free( prevwmrecords ); free( warpi ); free( warpj ); free( prevwarpi ); free( prevwarpj ); } #if OUTGAP0TRY if( !outgap ) { for( j=1; j" ); for( i=0; i N ) { fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N ); ErrorExit( "LENGTH OVER!\n" ); } for( i=0; i %f\n", kenzan, (double)kenzan /( icyc*jcyc ) ); double pairscore, nogappairscore, diff; char **pseq; pseq = AllocateCharMtx( 2, strlen( seq1[0] ) + 1 ); pairscore = nogappairscore = 0.0; #if 1 for( i=0; i 0.01 ) || PFACERROR ) // if( abs( pairscore - wm +*impmatch ) > 0.01 ) #else if( abs( pairscore - wm +*impmatch ) > 0.01 ) #endif // if( abs( pairscore - wm +*impmatch ) > 0.01 ) { for( i=0; igroup1\n%s\n", seq1[i] ); for( j=0; jgroup2\n%s\n", seq2[j] ); exit( 1 ); } #else reporterr( "\n" ); #endif #if 0 // if( strlen( seq1[0] ) - lgth1 > 100 && icyc > 1 || strlen( seq2[0] ) - lgth2 > 100 & jcyc > 1 ) if( strstr( seq1[0], "LNDDAT" ) && icyc == 1 || strstr( seq2[0], "LNDDAT" ) && jcyc==1) { for( i=0; igroup1\n%s\n", seq1[i] ); for( j=0; jgroup2\n%s\n", seq2[j] ); exit( 1 ); } #endif return( wm ); }