/* tree-dependent iteration algorithm A+ when group-to-group, C when group-to-singleSeqence OR algorithm A+ */ #include "mltaln.h" #define FULLSCORE 0 #define DEBUG 0 #define RECORD 0 extern char **seq_g; extern char **res_g; static int nwa; #ifdef enablemultithread typedef struct _threadarg { int thread_no; int *jobposintpt; int *ndonept; int *ntrypt; int *collectingpt; int njob; int nbranch; int maxiter; int nkozo; int *subgenerationpt; double *basegainpt; double *gainlist; double *tscorelist; int *generationofinput; char *kozoarivec; char **mastercopy; char ***candidates; int *generationofmastercopypt; int *branchtable; RNApair ***singlerna; LocalHom **localhomtable; int alloclen; Node *stopol; int ***topol; double **len; double **tscorehistory_detail; int *finishpt; int **skipthisbranch; double **distmtx; int ntarget; int *targetmap; pthread_mutex_t *mutex; pthread_cond_t *collection_end; pthread_cond_t *collection_start; } threadarg_t; #endif #if 1 static void shuffle( int *arr, int n ) { int i; int x; int b; for( i=1; i= ndistclass ) c = ndistclass-1; if( c >= maxdistclass ) c = maxdistclass-1; fprintf( stderr, "pair %d-%d (%f), dist=%f -> c=%d\n", i, j, eff1[i] * eff2[j], smalldistmtx[i][j], c ); eff1s[c][i] += 1.0; eff2s[c][j] += 1.0; matnum[i][j] = c; } for( c=0; c= ndistclass ) c = ndistclass-1; if( c >= maxdistclass ) c = maxdistclass-1; // fprintf( stderr, "pair %d-%d (%f), dist=%f -> c=%d\n", i, j, eff1[i] * eff2[j], smalldistmtx[i][j], c ); eff1s[c][i] = eff1[i]; eff2s[c][j] = eff2[j]; matnum[i][j] = c; } #endif #if 0 double totaleff; for( i=0; ithread_no; int njob = targ->njob; int nbranch = targ->nbranch; int maxiter = targ->maxiter; int *ndonept = targ->ndonept; int *ntrypt = targ->ntrypt; int *collectingpt = targ->collectingpt; int *jobposintpt = targ->jobposintpt; int nkozo = targ->nkozo; double *gainlist = targ->gainlist; double *tscorelist = targ->tscorelist; int *generationofinput = targ->generationofinput; int *subgenerationpt = targ->subgenerationpt; double *basegainpt = targ->basegainpt; char *kozoarivec = targ->kozoarivec; char **mastercopy = targ->mastercopy; char ***candidates = targ->candidates; int *generationofmastercopypt = targ->generationofmastercopypt; int *branchtable = targ->branchtable; RNApair ***singlerna = targ->singlerna; LocalHom **localhomtable = targ->localhomtable; int alloclen = targ->alloclen; Node * stopol = targ->stopol; int ***topol = targ->topol; double **len = targ->len; double **tscorehistory_detail = targ->tscorehistory_detail; int *finishpt = targ->finishpt; int **skipthisbranch = targ->skipthisbranch; double **distmtx = targ->distmtx; int ntarget = targ->ntarget; int *targetmap = targ->targetmap; int i, k, l, ii; double gain; int iterate; int **memlist; char *pairbuf; int locnjob; int s1, s2; int clus1, clus2; char **localcopy; char **mseq1, **mseq2; double *distarr; // re-calc double *effarr, *effarr_kozo; // re-calc double *effarr1, *effarr2, *effarr1_kozo, *effarr2_kozo; char *indication1, *indication2; int length; RNApair ***grouprna1, ***grouprna2; RNApair *rnapairboth; LocalHom ***localhomshrink; char *swaplist; int *gapmap1, *gapmap2; double tscore, mscore; double oimpmatchdouble; double impmatchdouble; int identity; double tmpdouble; // double naivescore0 = 0, naivescore1; double *effarrforlocalhom; double *tscorehistory; int intdum; #if 0 int oscillating; int lin, ldf; #endif double maxgain; int bestthread; int branchpos; int subgenerationatfirst; double unweightedspscore; int myjob; int converged2 = 0; int chudanres; double **smalldistmtx; double ***scoringmatrices; double **eff1s, **eff2s; int **whichmtx; locnjob = njob; if( utree == 0 ) { fprintf( stderr, "Dynamic tree is not supported in the multithread version.\n" ); exit( 1 ); } if( score_check == 2 ) { fprintf( stderr, "Score_check 2 is not supported in the multithread version.\n" ); exit( 1 ); } if( weight == 2 ) { fprintf( stderr, "Weight 2 is not supported in the multithread version.\n" ); exit( 1 ); } if( cooling && cut > 0.0 ) { fprintf( stderr, "Cooling is not supported in the multithread version.\n" ); exit( 1 ); } tscorehistory = calloc( maxiter, sizeof( double ) ); if( rnakozo && rnaprediction == 'm' ) { grouprna1 = (RNApair ***)calloc( njob, sizeof( RNApair ** ) ); grouprna2 = (RNApair ***)calloc( njob, sizeof( RNApair ** ) ); } else { grouprna1 = grouprna2 = NULL; } indication1 = AllocateCharVec( 150 ); indication2 = AllocateCharVec( 150 ); distarr = AllocateDoubleVec( locnjob ); effarr = AllocateDoubleVec( locnjob ); effarrforlocalhom = AllocateDoubleVec( locnjob ); effarr1 = AllocateDoubleVec( locnjob ); effarr2 = AllocateDoubleVec( locnjob ); mseq1 = AllocateCharMtx( locnjob, 0 ); mseq2 = AllocateCharMtx( locnjob, 0 ); localcopy = AllocateCharMtx( locnjob, alloclen ); gapmap1 = AllocateIntVec( alloclen ); gapmap2 = AllocateIntVec( alloclen ); if( specificityconsideration != 0 ) { smalldistmtx = AllocateDoubleMtx( locnjob, locnjob ); // ookii? scoringmatrices = AllocateDoubleCub( maxdistclass, nalphabets, nalphabets ); makescoringmatrices( scoringmatrices, n_dis_consweight_multi ); eff1s = AllocateDoubleMtx( maxdistclass, locnjob ); eff2s = AllocateDoubleMtx( maxdistclass, locnjob ); whichmtx = AllocateIntMtx( locnjob, locnjob ); } else { smalldistmtx = NULL; scoringmatrices = NULL; eff1s = eff2s = NULL; whichmtx = NULL; } effarr1_kozo = AllocateDoubleVec( locnjob ); // tsuneni allocate suru. effarr2_kozo = AllocateDoubleVec( locnjob ); // tsuneni allocate suru. effarr_kozo = AllocateDoubleVec( locnjob ); for( i=0; imutex ); if( *collectingpt == 1 ) { *collectingpt = 0; *generationofmastercopypt = iterate; *subgenerationpt = 0; *basegainpt = 0.0; *ndonept = 0; *jobposintpt = 0; for( i=0; icollection_end ); pthread_mutex_unlock( targ->mutex ); } else { pthread_cond_broadcast( targ->collection_end ); pthread_mutex_unlock( targ->mutex ); freelocalarrays ( tscorehistory, grouprna1, grouprna2, rnapairboth, indication1, indication2, distarr, effarr, effarrforlocalhom, effarr1, effarr2, mseq1, mseq2, localcopy, gapmap1, gapmap2, effarr1_kozo, effarr2_kozo, effarr_kozo, memlist, pairbuf, localhomshrink, swaplist, smalldistmtx, scoringmatrices, eff1s, eff2s, whichmtx ); // return( NULL ); pthread_exit( NULL ); } pthread_mutex_lock( targ->mutex ); while( *ndonept < nbranch ) pthread_cond_wait( targ->collection_start, targ->mutex ); pthread_mutex_unlock( targ->mutex ); // fprintf( stderr, "Thread 0 got a signal, *collectionpt = %d\n", *collectingpt ); /* Hoka no thread ga keisan */ pthread_mutex_lock( targ->mutex ); *collectingpt = 1; // chofuku #if 0 for( i=0; i maxgain ) { maxgain = gainlist[i]; bestthread = i; } } if( maxgain > 0.0 ) { // fprintf( stderr, "\nGain = %f\n", maxgain ); // fprintf( stderr, "best gain = %f by thread %d\n", gainlist[bestthread], bestthread ); // fprintf( stderr, "tscorelist[best] = %f by thread %d\n", tscorelist[bestthread], bestthread ); if( parallelizationstrategy == BESTFIRST ) { for( i=0; i0; i-- ) { // if( iterate-i < 15 ) fprintf( stderr, "hist[%d] = %f\n", i, tscorehistory[i] ); if( tscorehistory[i] == tscorelist[bestthread] ) { fprintf( stderr, "\nOscillating? %f == %f\n", tscorehistory[i], tscorelist[bestthread] ); *collectingpt = -1; break; } } tscorehistory[iterate] = tscorelist[bestthread]; #endif } else { fprintf( stderr, "\nConverged.\n" ); *collectingpt = -1; // pthread_cond_broadcast( targ->collection_end ); // pthread_mutex_unlock( targ->mutex ); // freelocalarrays(); // return( NULL ); // pthread_exit( NULL ); } #if 1 if( *finishpt ) { fprintf( stderr, "\nConverged2.\n" ); *collectingpt = -1; } #endif pthread_mutex_unlock( targ->mutex ); } pthread_mutex_lock( targ->mutex ); fprintf( stderr, "\nReached %d\n", maxiter ); *collectingpt = -1; pthread_cond_broadcast( targ->collection_end ); pthread_mutex_unlock( targ->mutex ); freelocalarrays ( tscorehistory, grouprna1, grouprna2, rnapairboth, indication1, indication2, distarr, effarr, effarrforlocalhom, effarr1, effarr2, mseq1, mseq2, localcopy, gapmap1, gapmap2, effarr1_kozo, effarr2_kozo, effarr_kozo, memlist, pairbuf, localhomshrink, swaplist, smalldistmtx, scoringmatrices, eff1s, eff2s, whichmtx ); return( NULL ); pthread_exit( NULL ); } else { while( 1 ) { #if 0 if( iterate % 2 == 0 ) { lin = 0; ldf = +1; } else { lin = locnjob - 2; ldf = -1; } for( l=lin; l < locnjob-1 && l >= 0 ; l+=ldf ) for( k=0; k<2; k++ ) #endif pthread_mutex_lock( targ->mutex ); while( *collectingpt > 0 ) pthread_cond_wait( targ->collection_end, targ->mutex ); if( *collectingpt == -1 ) { pthread_mutex_unlock( targ->mutex ); freelocalarrays ( tscorehistory, grouprna1, grouprna2, rnapairboth, indication1, indication2, distarr, effarr, effarrforlocalhom, effarr1, effarr2, mseq1, mseq2, localcopy, gapmap1, gapmap2, effarr1_kozo, effarr2_kozo, effarr_kozo, memlist, pairbuf, localhomshrink, swaplist, smalldistmtx, scoringmatrices, eff1s, eff2s, whichmtx ); return( NULL ); pthread_exit( NULL ); } // pthread_mutex_unlock( targ->mutex ); // pthread_mutex_lock( targ->mutex ); if( *jobposintpt == nbranch ) { if( *collectingpt != -1 ) *collectingpt = 1; // chofuku pthread_mutex_unlock( targ->mutex ); continue; } // fprintf( stderr, "JOB jobposintpt=%d\n", *jobposintpt ); myjob = branchtable[*jobposintpt]; l = myjob / 2; if( l == locnjob-2 ) k = 1; else k = myjob - l * 2; // fprintf( stderr, "JOB l=%d, k=%d\n", l, k ); branchpos = myjob; (*jobposintpt)++; iterate = *generationofmastercopypt; (*ntrypt)++; pthread_mutex_unlock( targ->mutex ); // fprintf( stderr, "\n IRANAI IRANAI *jobposintpt=%d, nbranch = %d\n", *jobposintpt, nbranch ); // fprintf( stderr, "branchpos = %d (thread %d)\n", branchpos, thread_no ); // fprintf( stderr, "iterate=%d, l=%d, k=%d (thread %d)\n", iterate, l, k, thread_no ); #if 0 fprintf( stderr, "STEP %03d-%03d-%d (Thread %d) ", iterate+1, l+1, k, thread_no ); fprintf( stderr, "STEP %03d-%03d-%d (thread %d) %s ", iterate+1, l+1, k, thread_no, use_fft?"\n":"\n" ); #endif // for( i=0; i<2; i++ ) for( j=0; jmutex ); for( i=0; imutex ); length = strlen( localcopy[0] ); if( nkozo ) { // double tmptmptmp; // tmptmptmp = 0.0; // clus1 = conjuctionfortbfast_kozo( &tmptmptmp, pair[0], s1, localcopy, mseq1, effarr1, effarr, effarr1_kozo, effarr_kozo, indication1 ); clus1 = fastconjuction_noname_kozo( memlist[0], localcopy, mseq1, effarr1, effarr, effarr1_kozo, effarr_kozo, indication1 ); for( i=0; i=0; i-- ) { oimpmatchdouble += (double)imp_match_out_scD( i, i ); // fprintf( stderr, "#### i=%d, initial impmatch = %f seq1 = %c, seq2 = %c\n", i, oimpmatch, mseq1[0][i], mseq2[0][i] ); } } else { part_imp_match_init_strict( NULL, clus1, clus2, length, length, mseq1, mseq2, effarr1, effarr2, effarr1_kozo, effarr2_kozo, localhomshrink, swaplist, 1, memlist[0], memlist[1] ); if( rnakozo ) part_imp_rna( clus1, clus2, mseq1, mseq2, effarr1, effarr2, grouprna1, grouprna2, gapmap1, gapmap2, NULL ); for( i=length-1; i>=0; i-- ) { oimpmatchdouble += (double)part_imp_match_out_sc( i, i ); // fprintf( stderr, "#### i=%d, initial impmatch = %f seq1 = %c, seq2 = %c\n", i, oimpmatch, mseq1[0][i], mseq2[0][i] ); } } // fprintf( stderr, "otmpmatch = %f\n", oimpmatch ); } else { if( alg == 'Q' ) { fprintf( stderr, "'Q' is no longer supported\n" ); exit( 1 ); } else { imp_match_init_strict( NULL, clus1, clus2, length, length, mseq1, mseq2, effarr1, effarr2, effarr1_kozo, effarr2_kozo, localhomshrink, swaplist, 1, memlist[0], memlist[1], NULL, NULL, NULL, -1, 0 ); fprintf( stderr, "not supported\n" ); exit( 1 ); for( i=length-1; i>=0; i-- ) { oimpmatchdouble += (double)imp_match_out_sc( i, i ); // fprintf( stderr, "#### i=%d, initial impmatch = %f seq1 = %c, seq2 = %c\n", i, oimpmatch, mseq1[0][i], mseq2[0][i] ); } } // fprintf( stderr, "otmpmatch = %f\n", oimpmatch ); } // fprintf( stderr, "#### initial impmatch = %f\n", oimpmatch ); } else { if( RNAscoremtx == 'r' ) intergroup_score_gapnomi( mseq1, mseq2, effarr1, effarr2, clus1, clus2, length, &tmpdouble ); // gappick mae denaito dame else { if( smalldistmtx ) #if 1 intergroup_score_multimtx( whichmtx, scoringmatrices, mseq1, mseq2, effarr1, effarr2, clus1, clus2, length, &tmpdouble ); #else intergroup_score_dynmtx( smalldistmtx, amino_dis, mseq1, mseq2, effarr1, effarr2, clus1, clus2, length, &tmpdouble ); // gappick mae denaito dame #endif else intergroup_score( mseq1, mseq2, effarr1, effarr2, clus1, clus2, length, &tmpdouble ); // gappick mae denaito dame } oimpmatchdouble = 0.0; } // fprintf( stderr, "#### tmpdouble = %f\n", tmpdouble ); mscore = oimpmatchdouble + tmpdouble; } else { fprintf( stderr, "score_check = %d\n", score_check ); fprintf( stderr, "Not supported. Please add --threadit 0 to disable the multithreading in the iterative refinement calculation.\n" ); exit( 1 ); } // if( rnakozo ) foldalignedrna( clus1, clus2, mseq1, mseq2, effarr1, effarr2, rnapairboth ); // if( !use_fft && !rnakozo ) // if( !use_fft ) if( !use_fft ) { commongappick_record( clus1, mseq1, gapmap1 ); commongappick_record( clus2, mseq2, gapmap2 ); } #if 0 fprintf( stderr, "##### mscore = %f\n", mscore ); #endif #if DEBUG if( !devide ) { fprintf( trap_g, "\n%d-%d-%d\n", iterate+1, l+1, k ); fprintf( trap_g, "group1 = %s\n", indication1 ); fprintf( trap_g, "group2 = %s\n", indication2 ); fflush( trap_g ); } #endif #if 0 printf( "STEP %d-%d-%d\n", iterate, l, k ); for( i=0; i %f, tscore = %f\n", tmpdouble, oimpmatch, impmatch, tscore ); } else { if( smalldistmtx ) #if 1 intergroup_score_multimtx( whichmtx, scoringmatrices, mseq1, mseq2, effarr1, effarr2, clus1, clus2, length, &tmpdouble ); #else intergroup_score_dynmtx( smalldistmtx, amino_dis, mseq1, mseq2, effarr1, effarr2, clus1, clus2, length, &tmpdouble ); #endif else intergroup_score( mseq1, mseq2, effarr1, effarr2, clus1, clus2, length, &tmpdouble ); tscore = tmpdouble; } // fprintf( stderr, "#######ii=%d, iterate=%d score = %f -> %f \n", ii, iterate , mscore, tscore ); #if 0 for( i=0; i<1; i++ ) fprintf( stderr, "%s\n", mseq1[i] ); fprintf( stderr, "+++++++\n" ); for( i=0; i<1; i++ ) fprintf( stderr, "%s\n", mseq2[i] ); #endif } else { tscore = mscore + 1.0; // tscore = 0.0; // fprintf( stderr, "in line 705, tscore=%f\n", tscore ); // for( i=0; i 0 ) { if( parallelizationstrategy == BESTFIRST ) { if( gain > gainlist[thread_no] ) { gainlist[thread_no] = gain; for( i=0; imutex ); for( i=0; imutex ); tscorelist[thread_no] = tscore; } #if 0 fprintf( stderr, "tscore = %f mscore = %f accepted.\n", tscore, mscore ); fprintf( stderr, "\nbetter! gain = %f (thread %d)\r", gain, thread_no ); #else fprintf( stderr, "%03d-%04d-%d (thread %4d) better \r", iterate+1, *ndonept, k, thread_no ); #endif } else { #if 0 fprintf( stderr, "tscore = %f mscore = %f rejected.\r", tscore, mscore ); fprintf( stderr, "worse! gain = %f", gain ); #else fprintf( stderr, "%03d-%04d-%d (thread %4d) worse \r", iterate+1, *ndonept, k, thread_no ); #endif tscore = mscore; } } #if FULLSCORE { int j; double fullscore = 0.0; for( i=1; i=0; ii-=1 ) { // fprintf( stderr, "Checking tscorehistory %f ?= %f\n", tscore, tscorehistory_detail[ii][branchpos] ); if( tscore == tscorehistory_detail[ii][branchpos] ) { converged2 = 1; break; } } if( parallelizationstrategy != BESTFIRST && converged2 ) { // fprintf( stderr, "\nFINISH!\n" ); pthread_mutex_lock( targ->mutex ); *finishpt = 1; pthread_mutex_unlock( targ->mutex ); } tscorehistory_detail[iterate][branchpos] = tscore; fprintf( stderr, "\r" ); pthread_mutex_lock( targ->mutex ); (*ndonept)++; // fprintf( stderr, "*ndonept = %d, nbranch = %d (thread %d) iterate=%d\n", *ndonept, nbranch, thread_no, iterate ); generationofinput[branchpos] = iterate; if( *ndonept == nbranch ) { if( *collectingpt != -1 ) *collectingpt = 1; // chofuku // fprintf( stderr, "Thread %d sends a signal, *ndonept = %d\n", thread_no, *ndonept ); pthread_cond_signal( targ->collection_start ); } pthread_mutex_unlock( targ->mutex ); } /* while( 1 ) */ } /* for( iterate ) */ // return( NULL ); } #endif int TreeDependentIteration( int locnjob, char **name, int nlen[M], char **aseq, char **bseq, int ***topol, double **len, double **distmtx, int **skipthisbranch, int alloclen, LocalHom **localhomtable, RNApair ***singlerna, int nkozo, char *kozoarivec, int ntarget, int *targetmap, int *targetmapr ) { int i, j, k, l, iterate, ii, iu, ju; int lin, ldf, length; int clus1, clus2; int s1, s2; static double **imanoten; static Node *stopol; static double *distarr = NULL; static double *effarrforlocalhom = NULL; static double *effarr = NULL; static double *effarr1 = NULL; static double *effarr2 = NULL; static double *effarr_kozo = NULL; static double *effarr1_kozo = NULL; static double *effarr2_kozo = NULL; static double **mtx = NULL; static int **node = NULL; static int *branchnode = NULL; static double **branchWeight = NULL; static char **mseq1, **mseq2; static double ***history; FILE *trap; double tscore, mscore; int identity; int converged; int oscillating; // double naivescore0 = 0.0; // by D.Mathog, a guess // double naivescore1; #if 0 char pair[njob][njob]; #else static int **memlist; static char *pairbuf; #endif #if DEBUG + RECORD double score_for_check0, score_for_check1; static double **effmtx = NULL; extern double score_calc0(); #endif static char *indication1, *indication2; static LocalHom ***localhomshrink = NULL; static char *swaplist = NULL; double impmatchdouble = 0.0; double oimpmatchdouble = 0.0; static int *gapmap1; static int *gapmap2; double tmpdouble; int intdum; static RNApair *rnapairboth; RNApair ***grouprna1, ***grouprna2; double unweightedspscore; static double **smalldistmtx; static double ***scoringmatrices; static double **eff1s, **eff2s; static int **whichmtx; int value; if( rnakozo && rnaprediction == 'm' ) { grouprna1 = (RNApair ***)calloc( njob, sizeof( RNApair ** ) ); grouprna2 = (RNApair ***)calloc( njob, sizeof( RNApair ** ) ); } else { grouprna1 = grouprna2 = NULL; } Writeoptions( trap_g ); fflush( trap_g ); if( 1 || effarr == NULL ) /* locnjob == njob ni kagiru */ { indication1 = AllocateCharVec( 150 ); indication2 = AllocateCharVec( 150 ); effarr = AllocateDoubleVec( locnjob ); distarr = AllocateDoubleVec( locnjob ); effarrforlocalhom = AllocateDoubleVec( locnjob ); effarr1 = AllocateDoubleVec( locnjob ); effarr2 = AllocateDoubleVec( locnjob ); mseq1 = AllocateCharMtx( locnjob, 0 ); mseq2 = AllocateCharMtx( locnjob, 0 ); mtx = AllocateDoubleMtx( locnjob, locnjob ); node = AllocateIntMtx( locnjob, locnjob ); branchnode = AllocateIntVec( locnjob ); branchWeight = AllocateDoubleMtx( locnjob, 2 ); history = AllocateFloatCub( niter, locnjob, 2 ); stopol = (Node *)calloc( locnjob * 2, sizeof( Node ) ); gapmap1 = AllocateIntVec( alloclen ); gapmap2 = AllocateIntVec( alloclen ); if( score_check == 2 ) imanoten = AllocateDoubleMtx( njob, njob ); if( specificityconsideration != 0 ) { smalldistmtx = AllocateDoubleMtx( locnjob, locnjob ); // ookii? scoringmatrices = AllocateDoubleCub( maxdistclass, nalphabets, nalphabets ); makescoringmatrices( scoringmatrices, n_dis_consweight_multi ); eff1s = AllocateDoubleMtx( maxdistclass, locnjob ); eff2s = AllocateDoubleMtx( maxdistclass, locnjob ); whichmtx = AllocateIntMtx( locnjob, locnjob ); } else { smalldistmtx = NULL; scoringmatrices = NULL; eff1s = eff2s = NULL; whichmtx = NULL; } effarr1_kozo = AllocateDoubleVec( locnjob ); // tsuneni allocate suru. effarr2_kozo = AllocateDoubleVec( locnjob ); // tsuneni allocate suru. effarr_kozo = AllocateDoubleVec( locnjob ); for( i=0; i 2 && ( weight == 4 || weight == 0 ) ) { treeCnv( stopol, locnjob, topol, len, branchWeight ); calcBranchWeight( branchWeight, locnjob, stopol, topol, len ); // IRU!!! } } #ifdef enablemultithread if( nthread > 0 ) { threadarg_t *targ; pthread_t *handle; pthread_mutex_t mutex; pthread_cond_t collection_end; pthread_cond_t collection_start; int jobposint; int generationofmastercopy; int subgeneration; double basegain; int *generationofinput; double *gainlist; double *tscorelist; int ndone; int ntry; int collecting; int nbranch; int maxiter; char ***candidates; int *branchtable; double **tscorehistory_detail; int finish; nwa = nthread + 1; nbranch = (njob-1) * 2 - 1; maxiter = niter; targ = calloc( nwa, sizeof( threadarg_t ) ); handle = calloc( nwa, sizeof( pthread_t ) ); pthread_mutex_init( &mutex, NULL ); pthread_cond_init( &collection_end, NULL ); pthread_cond_init( &collection_start, NULL ); gainlist = calloc( nwa, sizeof( double ) ); tscorelist = calloc( nwa, sizeof( double ) ); branchtable = calloc( nbranch, sizeof( int ) ); generationofinput = calloc( nbranch, sizeof( int ) ); if( parallelizationstrategy == BESTFIRST ) candidates = AllocateCharCub( nwa, locnjob, alloclen ); for( i=0; i 2 && ( weight == 4 || weight == 0 ) ) { treeCnv( stopol, locnjob, topol, len, branchWeight ); calcBranchWeight( branchWeight, locnjob, stopol, topol, len ); // IRU!!! } trap = fopen( "hat2", "w" ); if( !trap ) ErrorExit( "Cannot open hat2." ); WriteHat2_pointer( trap, locnjob, name, mtx ); fclose( trap ); if( constraint ) { counteff_simple( locnjob, topol, len, effarrforlocalhom ); if( ntarget < locnjob ) calcimportance_target( locnjob, ntarget, effarrforlocalhom, aseq, localhomtable, targetmap, targetmapr, alloclen ); else calcimportance_half( locnjob, effarrforlocalhom, aseq, localhomtable, alloclen ); } } if( iterate % 2 == 0 ) { lin = 0; ldf = +1; } else { lin = locnjob - 2; ldf = -1; } if( score_check == 2 ) { effarr1[0] = 1.0; effarr2[0] = 1.0; length = strlen( bseq[0] ); for( i=0; i= 0 ; l+=ldf ) { for( k=0; k<2; k++ ) { if( l == locnjob-2 ) k = 1; #else for( jobpos=0; jobpos=0; i-- ) oimpmatchdouble += (double)part_imp_match_out_sc( i, i ); } } else { if( alg == 'Q' ) { fprintf( stderr, "'Q' is no longer supported\n" ); exit( 1 ); } else { imp_match_init_strict( NULL, clus1, clus2, length, length, mseq1, mseq2, effarr1, effarr2, effarr1_kozo, effarr2_kozo, localhomshrink, swaplist, 1, memlist[0], memlist[1], NULL, NULL, NULL, -1, 0 ); fprintf( stderr, "not supported\n" ); exit( 1 ); } } // fprintf( stderr, "### oimpmatch = %f\n", oimpmatch ); } else { oimpmatchdouble = 0.0; } #if 0 tmpdouble = 0.0; iu=0; for( i=s1; i>> oimpmatchdouble = 0.0; if( use_fft ) { if( alg == 'Q' ) { fprintf( stderr, "'Q' is no longer supported\n" ); exit( 1 ); } else if( alg == 'd' ) { imp_match_init_strictD( NULL, clus1, clus2, length, length, mseq1, mseq2, effarr1, effarr2, effarr1_kozo, effarr2_kozo, localhomshrink, swaplist, 1, memlist[0], memlist[1], NULL, NULL, NULL, -1, 0 ); for( i=length-1; i>=0; i-- ) { oimpmatchdouble += (double)imp_match_out_scD( i, i ); // fprintf( stderr, "#### i=%d, initial impmatch = %f seq1 = %c, seq2 = %c\n", i, oimpmatch, mseq1[0][i], mseq2[0][i] ); } } else { part_imp_match_init_strict( NULL, clus1, clus2, length, length, mseq1, mseq2, effarr1, effarr2, effarr1_kozo, effarr2_kozo, localhomshrink, swaplist, 1, memlist[0], memlist[1] ); if( rnakozo ) part_imp_rna( clus1, clus2, mseq1, mseq2, effarr1, effarr2, grouprna1, grouprna2, gapmap1, gapmap2, NULL ); for( i=length-1; i>=0; i-- ) { oimpmatchdouble += (double)part_imp_match_out_sc( i, i ); // fprintf( stderr, "#### i=%d, initial impmatch = %f seq1 = %c, seq2 = %c\n", i, oimpmatch, mseq1[0][i], mseq2[0][i] ); } } // fprintf( stderr, "otmpmatch = %f\n", oimpmatch ); } else { if( alg == 'Q' ) { fprintf( stderr, "'Q' is no longer supported\n" ); exit( 1 ); } else { imp_match_init_strict( NULL, clus1, clus2, length, length, mseq1, mseq2, effarr1, effarr2, effarr1_kozo, effarr2_kozo, localhomshrink, swaplist, 1, memlist[0], memlist[1], NULL, NULL, NULL, -1, 0 ); fprintf( stderr, "not supported\n" ); exit( 1 ); for( i=length-1; i>=0; i-- ) { oimpmatchdouble += (double)imp_match_out_sc( i, i ); // fprintf( stderr, "#### i=%d, initial impmatch = %f seq1 = %c, seq2 = %c\n", i, oimpmatch, mseq1[0][i], mseq2[0][i] ); } } // fprintf( stderr, "otmpmatch = %f\n", oimpmatch ); } // fprintf( stderr, "#### initial impmatch = %f\n", oimpmatch ); } else { if( RNAscoremtx == 'r' ) intergroup_score_gapnomi( mseq1, mseq2, effarr1, effarr2, clus1, clus2, length, &tmpdouble ); // gappick mae denaito dame else { if( smalldistmtx ) #if 1 intergroup_score_multimtx( whichmtx, scoringmatrices, mseq1, mseq2, effarr1, effarr2, clus1, clus2, length, &tmpdouble ); #else intergroup_score_dynmtx( offsetmtx, amino_dis, mseq1, mseq2, effarr1, effarr2, clus1, clus2, length, &tmpdouble ); // n_dis ha machigai #endif else intergroup_score( mseq1, mseq2, effarr1, effarr2, clus1, clus2, length, &tmpdouble ); // gappick mae denaito dame } oimpmatchdouble = 0.0; } // fprintf( stderr, "#### tmpdouble = %f\n", tmpdouble ); mscore = oimpmatchdouble + tmpdouble; } else { // fprintf( stderr, "score_check = %d\n" ); #if 1 /* Oscilation check no tame hitsuyou! atode kousokuka */ intergroup_score( mseq1, mseq2, effarr1, effarr2, clus1, clus2, length, &tmpdouble ); mscore = tmpdouble; /* atode kousokuka */ #else mscore = 0.0; #endif if( constraint ) { oimpmatchdouble = 0.0; // shrinklocalhom( pair, s1, s2, localhomtable, localhomshrink ); if( ntarget < locnjob ) msshrinklocalhom_fast_target( memlist[0], memlist[1], localhomtable, localhomshrink, swaplist, targetmap ); else msshrinklocalhom_fast_half( memlist[0], memlist[1], localhomtable, localhomshrink ); if( use_fft ) { if( alg == 'Q' ) { fprintf( stderr, "'Q' is no longer supported\n" ); exit( 1 ); } else if( alg == 'd' ) { imp_match_init_strictD( NULL, clus1, clus2, length, length, mseq1, mseq2, effarr1, effarr2, effarr1_kozo, effarr2_kozo, localhomshrink, swaplist, 1, memlist[0], memlist[1], NULL, NULL, NULL, -1, 0 ); if( rnakozo ) imp_rnaD( clus1, clus2, mseq1, mseq2, effarr1, effarr2, grouprna1, grouprna2, gapmap1, gapmap2, NULL ); } else { part_imp_match_init_strict( NULL, clus1, clus2, length, length, mseq1, mseq2, effarr1, effarr2, effarr1_kozo, effarr2_kozo, localhomshrink, swaplist, 1, memlist[0], memlist[1] ); if( rnakozo ) part_imp_rna( clus1, clus2, mseq1, mseq2, effarr1, effarr2, grouprna1, grouprna2, gapmap1, gapmap2, NULL ); } } else { if( alg == 'Q' ) { fprintf( stderr, "'Q' is no longer supported\n" ); exit( 1 ); } else { imp_match_init_strict( NULL, clus1, clus2, length, length, mseq1, mseq2, effarr1, effarr2, effarr1_kozo, effarr2_kozo, localhomshrink, swaplist, 1, memlist[0], memlist[1], NULL, NULL, NULL, -1, 0 ); fprintf( stderr, "Not supported\n" ); exit( 1 ); } } } } // oimpmatch = 0.0; if( constraint ) { #if 0 // iranai if( alg == 'Q' ) { imp_match_init_strictQ( NULL, clus1, clus2, length, length, mseq1, mseq2, effarr1, effarr2, localhomshrink, 1 ); for( i=length-1; i>=0; i-- ) { oimpmatch += imp_match_out_scQ( i, i ); // fprintf( stderr, "#### i=%d, initial impmatch = %f seq1 = %c, seq2 = %c\n", i, oimpmatch, mseq1[0][i], mseq2[0][i] ); } } else { imp_match_init_strict( NULL, clus1, clus2, length, length, mseq1, mseq2, effarr1, effarr2, effarr1_kozo, effarr2_kozo, localhomshrink, 1 ); for( i=length-1; i>=0; i-- ) { oimpmatch += imp_match_out_sc( i, i ); // fprintf( stderr, "#### i=%d, initial impmatch = %f seq1 = %c, seq2 = %c\n", i, oimpmatch, mseq1[0][i], mseq2[0][i] ); } } #endif } #if 0 if( alg == 'H' ) naivescore0 = naivepairscore( clus1, clus2, mseq1, mseq2, effarr1, effarr2, penalty ) + oimpmatch; else if( alg == 'Q' ) naivescore0 = naiveQpairscore( clus1, clus2, mseq1, mseq2, effarr1, effarr2, penalty ) + oimpmatch; else if( alg == 'R' ) naivescore0 = naiveRpairscore( clus1, clus2, mseq1, mseq2, effarr1, effarr2, penalty ) + oimpmatch; #endif // if( rnakozo ) foldalignedrna( clus1, clus2, mseq1, mseq2, effarr1, effarr2, rnapairboth ); // if( !use_fft && !rnakozo ) // if( !use_fft ) if( !use_fft ) { commongappick_record( clus1, mseq1, gapmap1 ); commongappick_record( clus2, mseq2, gapmap2 ); } #if 0 fprintf( stderr, "##### mscore = %f\n", mscore ); #endif #if DEBUG if( !devide ) { fprintf( trap_g, "\nSTEP%d-%d-%d\n", iterate+1, l+1, k ); fprintf( trap_g, "group1 = %s\n", indication1 ); fprintf( trap_g, "group2 = %s\n", indication2 ); fflush( trap_g ); } #endif #if 0 printf( "STEP %d-%d-%d\n", iterate, l, k ); for( i=0; i fullscore ) { for( i=0; igroup1\n%s\n", mseq1[i] ); for( i=0; igroup2\n%s\n", mseq2[i] ); for( i=0; ibetter alignment\n%s\n", bseq[i] ); exit( 1 ); } } #endif length = strlen( mseq1[0] ); if( identity ) { tscore = mscore; if( !devide ) fprintf( trap_g, "tscore = %f identical.\n", tscore ); fprintf( stderr, " identical. " ); converged++; } else { if( score_check ) { if( constraint == 2 ) { #if 1 if( RNAscoremtx == 'r' ) intergroup_score_gapnomi( mseq1, mseq2, effarr1, effarr2, clus1, clus2, length, &tmpdouble ); else intergroup_score( mseq1, mseq2, effarr1, effarr2, clus1, clus2, length, &tmpdouble ); #else intergroup_score( mseq1, mseq2, effarr1, effarr2, clus1, clus2, length, &tmpdouble ); #endif tscore = impmatchdouble + tmpdouble; // fprintf( stderr, "tmpdouble=%f, impmatch = %f -> %f, tscore = %f\n", tmpdouble, oimpmatch, impmatch, tscore ); } else { if( smalldistmtx ) #if 1 intergroup_score_multimtx( whichmtx, scoringmatrices, mseq1, mseq2, effarr1, effarr2, clus1, clus2, length, &tmpdouble ); #else intergroup_score_dynmtx( offsetmtx, amino_dis, mseq1, mseq2, effarr1, effarr2, clus1, clus2, length, &tmpdouble ); // n_dis ha machigai #endif else intergroup_score( mseq1, mseq2, effarr1, effarr2, clus1, clus2, length, &tmpdouble ); tscore = tmpdouble; } // fprintf( stderr, "#######ii=%d, iterate=%d score = %f -> %f \n", ii, iterate , mscore, tscore ); #if 0 for( i=0; i<1; i++ ) fprintf( stderr, "%s\n", mseq1[i] ); fprintf( stderr, "+++++++\n" ); for( i=0; i<1; i++ ) fprintf( stderr, "%s\n", mseq2[i] ); #endif } else { tscore = mscore + 1.0; // tscore = 0.0; // fprintf( stderr, "in line 705, tscore=%f\n", tscore ); // for( i=0; i mscore - cut/100.0*mscore ) { writePre( locnjob, name, nlen, aseq, 0 ); for( i=0; i= locnjob * 2 ) { fprintf( trap_g, "Converged.\n\n" ); fprintf( stderr, "\nConverged.\n\n" ); if( scoreout ) { unweightedspscore = plainscore( njob, bseq ); fprintf( stderr, "\nSCORE %d = %.0f, ", iterate * ( (njob-1)*2-1 ), unweightedspscore ); fprintf( stderr, "SCORE / residue = %f", unweightedspscore / ( njob * strlen( bseq[0] ) ) ); if( weight || constraint ) fprintf( stderr, " (differs from the objective score)" ); fprintf( stderr, "\n\n" ); } value = 0; goto end; } if( iterate >= 1 ) { /* oscillation check */ oscillating = 0; for( ii=iterate-2; ii>=0; ii-=2 ) { if( (double)tscore == history[ii][l][k] ) { oscillating = 1; break; } } if( ( oscillating && !cooling ) || ( oscillating && cut < 0.001 && cooling ) ) { fprintf( trap_g, "Oscillating.\n" ); fprintf( stderr, "\nOscillating.\n\n" ); if( scoreout ) { unweightedspscore = plainscore( njob, bseq ); fprintf( stderr, "\nSCORE %d = %.0f, ", iterate * ( (njob-1)*2-1 ), unweightedspscore ); fprintf( stderr, "SCORE / residue = %f", unweightedspscore / ( njob * strlen( bseq[0] ) ) ); if( weight || constraint ) fprintf( stderr, " (differs from the objective score)" ); fprintf( stderr, "\n\n" ); } #if 1 /* hujuubun */ value = -1; goto end; #endif } } /* if( iterate ) */ } /* for( k ) */ } /* for( l ) */ if( scoreout ) { unweightedspscore = plainscore( njob, bseq ); fprintf( stderr, "\nSCORE %d = %.0f, ", iterate * ( (njob-1)*2-1 ), unweightedspscore ); fprintf( stderr, "SCORE / residue = %f", unweightedspscore / ( njob * strlen( bseq[0] ) ) ); if( weight || constraint ) fprintf( stderr, " (differs from the objective score)" ); fprintf( stderr, "\n\n" ); } } /* for( iterate ) */ } value = 2; end: // if( grouprna1 ) free( grouprna1 ); // if( grouprna2 ) free( grouprna2 ); #if 1 freelocalarrays ( NULL, grouprna1, grouprna2, rnapairboth, indication1, indication2, distarr, effarr, effarrforlocalhom, effarr1, effarr2, mseq1, mseq2, NULL, gapmap1, gapmap2, effarr1_kozo, effarr2_kozo, effarr_kozo, memlist, pairbuf, localhomshrink, swaplist, smalldistmtx, scoringmatrices, eff1s, eff2s, whichmtx ); if( branchnode ) free( branchnode ); if( stopol) free( stopol ); if( mtx ) FreeDoubleMtx( mtx ); if( node ) FreeIntMtx( node ); if( branchWeight ) FreeDoubleMtx( branchWeight ); if( history) FreeFloatCub( history ); treeCnv( NULL, 0, 0, NULL, NULL ); // 2021/Sep #endif // freelocalarrays // ( // NULL, // grouprna1, grouprna2, // rnapairboth, // indication1, indication2, // distarr, // effarr, effarrforlocalhom, effarr1, effarr2, // mseq1, mseq2, // NULL, // gapmap1, gapmap2, // effarr1_kozo, effarr2_kozo, effarr_kozo, // memlist, pairbuf, // localhomshrink, // smalldistmtx, // scoringmatrices, // eff1s, eff2s, // whichmtx // ); // free( branchnode ); // free( stopol ); // return( value ); } /* int Tree... */