#include "mltaln.h" #define DEBUG 0 #define EF_THREEWAY 1.0 #define MAXBW 1.0 #define MINBW 0.01 #define MINLEN 0.001 #if DEBUG Node *stopol_g; #endif void checkMinusLength( int nseq, double **len ) { int i, j; for( i=0; i= 0 ) { free( tmpintvec[numintvec] ); } free( tmpintvec ); numintvec = 0; #endif } void treeCnv( Node *stopol, int locnseq, int ***topol, double **len, double **bw ) { int i; NodeInCub parent; int *count; int ccount; int rep; int tmpint; static int **tmpintvec = NULL; static int numintvec = 0; count = AllocateIntVec( 2 * locnseq ); /* oome */ if( !count ) ErrorExit( "Cannot allocate count.\n" ); checkMinusLength( locnseq, len ); /* uwagaki */ stopolInit( locnseq * 2, stopol ); for( i=0; ilength[0] ); } for( i=0, count=0; i<3; i++ ) { #if DEBUG fprintf( stderr, "ob->tmpChildren[%d] = %d\n", i, ob->tmpChildren[i] ); #endif if( oppositeNode != ob->children[i] ) dir_ch[count++] = i; else dir_pa = i; } #if DEBUG fprintf( stderr, "\n" ); #endif if( count != 2 ) { #if DEBUG fprintf( stderr, "Node No.%d has no child like No.%d \n", ob-stopol_g, oppositeNode-stopol_g ); #endif ErrorExit( "Invalid call\n" ); } tmpvalue0 = syntheticLength( ob->children[dir_ch[0]], ob ); tmpvalue1 = syntheticLength( ob->children[dir_ch[1]], ob ); #if DEBUG fprintf( stderr, "tmpvalue0 = %f\n", tmpvalue0 ); fprintf( stderr, "tmpvalue1 = %f\n", tmpvalue1 ); #endif if( tmpvalue0 ) tmpvalue0 = 1.0 / tmpvalue0; else nanflag = 1; if( tmpvalue1 ) tmpvalue1 = 1.0 / tmpvalue1; else nanflag = 1; if( nanflag ) value = 0.0; else { value = tmpvalue0 + tmpvalue1; value = 1.0 / value; } value += ob->length[dir_pa]; #if DEBUG fprintf( stderr, "value = %f\n", value ); #endif return( value ); } double calcW( Node *ob, Node *op ) { int i, count; int dir_ch[3]; int dir_pa = -10; // by katoh double a, b, c, f, s; double value; if( isLeaf( *ob ) ) return( 1.0 ); for( i=0, count=0; i<3; i++ ) { if( op != ob->children[i] ) dir_ch[count++] = i; else dir_pa = i; } if( count != 2 ) ErrorExit( "Invalid call of calcW\n" ); #if DEBUG fprintf( stderr, "In calcW\n" ); fprintf( stderr, "ob = %d\n", ob - stopol_g ); fprintf( stderr, "op = %d\n", op - stopol_g ); fprintf( stderr, "ob->children[c1] = %d\n", ob->children[dir_ch[0]] - stopol_g ); fprintf( stderr, "ob->children[c2] = %d\n", ob->children[dir_ch[1]] - stopol_g ); fprintf( stderr, "ob->children[pa] = %d\n", ob->children[dir_pa] - stopol_g ); fprintf( stderr, "\n" ); #endif a = syntheticLength( ob->children[dir_ch[0]], ob ); b = syntheticLength( ob->children[dir_ch[1]], ob ); c = syntheticLength( ob->children[dir_pa], ob ); #if DEBUG fprintf( stderr, "a = %f\n", a ); fprintf( stderr, "b = %f\n", b ); fprintf( stderr, "c = %f\n", c ); #endif if( !c ) return( MAXBW ); if ( !a || !b ) return( MINBW ); /* ? */ f = EF_THREEWAY; s = ( b*c + c*a + a*b ); value = a*b*(c+a)*(c+b) / ( c*(a+b) * f * s ); value = sqrt( value ); return( value ); } void calcBranchWeight( double **bw, int locnseq, Node *stopol, int ***topol, double **len ) { NodeInCub parent; int i; int rep; Node *topNode, *btmNode; double topW, btmW; for( i=locnseq; ichildren[i] != op ) dir_ch[count++] = i; else dir_pa = i; } if( count != 2 ) { #if DEBUG fprintf( stderr, "Node No.%d has no child like No.%d \n", ob-stopol_g, op-stopol_g ); #endif ErrorExit( "Incorrect call of weightFromABranch_rec" ); } for( i=0; (n=ob->members[dir_ch[0]][i])!=-1; i++ ) result[n] *= *ob->weightptr[dir_ch[0]]; weightFromABranch_rec( result, ob->children[dir_ch[0]], ob ); for( i=0; (n=ob->members[dir_ch[1]][i])!=-1; i++ ) result[n] *= *ob->weightptr[dir_ch[1]]; weightFromABranch_rec( result, ob->children[dir_ch[1]], ob ); } void weightFromABranch( int nseq, double *result, Node *stopol, int ***topol, int step, int LorR ) { Node *topNode, *btmNode; int i; if( step == nseq - 2 ) { topNode = stopol[nseq-2].children[0]; btmNode = stopol + nseq-3; #if DEBUG fprintf( stderr, "Now step == nseq-3, topNode = %d, btmNode = %d\n", topNode - stopol_g, btmNode-stopol_g ); #endif } else { for( i=0; i<3; i++ ) { if( stopol[step].members[i][0] == topol[step][LorR][0] ) break; } if( i== 3 ) ErrorExit( "Incorrect call of weightFromABranch." ); btmNode = stopol[step].children[i]; topNode = stopol+step; } for( i=0; ichildren[i] != op ) dir_ch[count++] = i; else dir_pa = i; } if( count != 2 ) { #if DEBUG fprintf( stderr, "Node No.%d has no child like No.%d \n", ob-stopol_g, op-stopol_g ); #endif ErrorExit( "Incorrect call of weightFromABranch_rec" ); } // fprintf( stderr, "\n" ); sumweight = 0.0; count = 0; lastkozo = -1; for( i=0; (n=ob->members[dir_ch[0]][i])!=-1; i++ ) { // fprintf( stderr, "member1! n=%d\n", n ); sumweight += seqweight[n]; if( kozoari[n] ) { count++; lastkozo = n; } } for( i=0; (n=ob->members[dir_ch[1]][i])!=-1; i++ ) { // fprintf( stderr, "member2! n=%d\n", n ); sumweight += seqweight[n]; if( kozoari[n] ) { count++; lastkozo = n; } } // fprintf( stderr, "count = %d\n", count ); if( count == 1 ) strweight[lastkozo] = sumweight; else if( count > 1 ) { assignstrweight_rec( strweight, ob->children[dir_ch[0]], ob, kozoari, seqweight ); assignstrweight_rec( strweight, ob->children[dir_ch[1]], ob, kozoari, seqweight ); } } void assignstrweight( int nseq, double *strweight, Node *stopol, int ***topol, int step, int LorR, char *kozoari, double *seqweight ) { Node *topNode, *btmNode; int i; if( step == nseq - 2 ) { topNode = stopol[nseq-2].children[0]; btmNode = stopol + nseq-3; #if DEBUG fprintf( stderr, "Now step == nseq-3, topNode = %d, btmNode = %d\n", topNode - stopol_g, btmNode-stopol_g ); #endif } else { for( i=0; i<3; i++ ) { if( stopol[step].members[i][0] == topol[step][LorR][0] ) break; } if( i== 3 ) ErrorExit( "Incorrect call of weightFromABranch." ); btmNode = stopol[step].children[i]; topNode = stopol+step; } for( i=0; i-1; i++ ) fprintf( stderr, "%3d ", topol[step][0][i] ); fprintf( stderr, "\n" ); for( i=0; topol[step][1][i]>-1; i++ ) fprintf( stderr, "%3d ", topol[step][1][i] ); fprintf( stderr, "\n" ); for( i=0; i