1 /* iteration ( algorithm C ) */
6 static void Writeoptions( FILE *fp )
9 fprintf( fp, "Dayhoff( ... )\n" );
10 else if( scoremtx == -1 )
11 fprintf( fp, "DNA\n" );
12 else if( scoremtx == 2 )
13 fprintf( fp, "Miyata-Yasunaga\n" );
15 fprintf( fp, "JTT %dPAM\n", pamN );
18 fprintf( fp, "Gap Penalty = %+d, %+d\n", penalty, offset );
20 fprintf( fp, "Gap Penalty = %+d\n", penalty );
22 fprintf( fp, "marginal score to search : best - %f\n", cut );
24 fprintf( fp, "score of rnd or sco\n" );
26 fprintf( fp, "score = sigma( score for a pair of homologous amino acids ) / ( number of amino acids pairs )\n" );
28 fprintf( fp, "score : SP\n" );
34 fprintf( fp, "weighted, geta2 = %f\n", geta2 );
35 else if( weight == 3 )
38 fprintf( fp, "reversely weighted in function 'align', unweighted in function 'score_calc'\n" );
40 fprintf( fp, "weighted like ClustalW," );
43 fprintf( fp, "unweighted\n" );
47 fprintf( fp, "using tree defined by the file hat2 with simplified UPG method\n" );
49 if( weight && !utree )
50 fprintf( fp, "using temporary tree by simplified UPG method\n" );
51 fprintf( fp, "Algorithm %c\n", alg );
57 char **align0( double *wm, char **aseq, char *seq, double effarr[M], int icyc, int ex )
63 ErrorExit( "Sorry!" );
67 result = alignm1_o( wm, aseq, seq, scmx, effarr, icyc, ex );
71 result = alignm1( wm, aseq, seq, scmx, effarr, icyc, ex );
77 result = Calignm1( wm, aseq, seq, effarr, icyc, ex );
83 double score_m_1_0( char **aseq, int locnjob, int s, double **eff, double effarr[M] )
89 ErrorExit( "Sorry!" );
93 x = Cscore_m_1( aseq, locnjob, s, eff );
95 fprintf( stderr, "in score_m_1_0 %f\n", x );
99 int iteration( int locnjob, char name[M][B], int nlen[M], char **aseq, char **bseq, int ***topol, double **len, double **eff )
101 double tscore, mscore;
103 static char *mseq1, **mseq2 = NULL;
104 static char **result;
106 static double effarr[M];
112 int nlenmax0 = nlenmax;
118 double his[2][M][MAXITERATION/locnjob+1];
127 for( i=0; i<locnjob; i++ )
133 sai[locnjob] = sai1[locnjob] = sai2[locnjob] = 0;
136 Writeoptions( trap_g );
138 his = AllocateDoubleCub( 2, M, MAXITERATION/locnjob+1 );
142 alloclen = nlenmax * 2.0;
143 AllocateTmpSeqs( &mseq2, &mseq1, alloclen );
146 if( !tbitr && !tbweight )
148 writePre( locnjob, name, nlen, aseq, 0 );
151 prep = fopen( "best", "w" );
152 Write( prep, locnjob, name, nlen, aseq );
160 treeconstruction( aseq, locnjob, topol, len, eff );
161 tscore = score_calc0( aseq, locnjob, eff, 0 );
164 fprintf( stderr, "eff mtx in iteration\n" );
165 for( i=0; i<locnjob; i++ )
167 for( j=0; j<locnjob; j++ )
169 fprintf( stderr, "%5.3f ", eff[i][j] );
171 fprintf( stderr, "\n" );
175 fprintf( stderr, "\n" );
178 fprintf( stderr, "aseq(initial)\n" );
179 display( aseq, locnjob );
181 fprintf( stderr, "initial score = %f \n", tscore );
182 fprintf( stderr, "\n" );
183 for( i=0; i<locnjob; i++ ) strcpy( bseq[i], aseq[i] );
190 sss[0] = (int)( (double)locnjob/2.0 );
193 cyc[0] = 0; cyc[1] = 0;
195 for( s=-1, l=0; l<MAXITERATION; l++ )
198 double tmpscore, tmpscore1;
200 if( strlen( aseq[0] ) > nlenmax )
201 nlenmax = strlen( aseq[0] );
204 s = ( int )( rnd() * locnjob );
206 if( s == locnjob ) s = 0;
215 if( sss[0] == locnjob )
233 if( sss[1] == locnjob )
239 fprintf( trap_g, "%d ", weight );
242 for( i=0, count=0; i<strlen( aseq[s] ); i++ )
244 if( aseq[s][i] != '-' )
246 mseq1[count] = aseq[s][i];
252 gappick0( mseq1, aseq[s] );
255 tmpscore = score_m_1_0( aseq, locnjob, s, eff, effarr );
257 gappick( locnjob, s, aseq, mseq2, eff, effarr );
259 result = align0( &wm, mseq2, mseq1, effarr, locnjob-2, s );
260 resultlen = strlen( result[0] );
261 if( resultlen > alloclen )
263 if( resultlen > nlenmax0*3 || resultlen > N )
265 fprintf(stderr, "Error in main1\n");
268 FreeTmpSeqs( mseq2, mseq1 );
269 alloclen = strlen( result[0] ) * 2.0;
270 fprintf( stderr, "\n\ntrying to allocate TmpSeqs\n\n" );
271 AllocateTmpSeqs( &mseq2, &mseq1, alloclen );
273 for( i=0; i<locnjob; i++ ) strcpy( mseq2[i], result[i] );
276 fprintf( stderr, "wm in iteration == %f\n", wm );
278 strcpy( mseq1, mseq2[locnjob-1] );
280 Write( stdout, locnjob, name, nlen, mseq2 );
282 for( i=locnjob-2; i>=s; i-- ) strcpy( mseq2[i+1], mseq2[i] );
283 strcpy( mseq2[s], mseq1 );
286 tmpscore1= score_m_1_0( mseq2, locnjob, s, eff, effarr );
287 fprintf( stderr, "pick up %d, before ALIGNM1 score_m_1_0 = %f\n", s+1, tmpscore );
288 fprintf( stderr, "pick up %d, after ALIGNM1 score_m_1_0 = %f\n", s+1, tmpscore1 );
289 if( tmpscore1 < tmpscore )
291 fprintf( stderr, "\7" );
292 fprintf( trap_g, ">>>>>>>n\n" );
294 if( fabs( wm - tmpscore1 ) / wm > 0.001 )
296 fprintf( stderr, "\7sorry\n" );
301 identity = !strcmp( mseq2[s], aseq[s] );
302 if( s == locnjob - 1 ) ss = 0; else ss=s+1;
304 identity *= !strcmp( mseq2[ss], aseq[ss] );
308 tmpscore = score_calc0( mseq2, locnjob, eff, s );
310 else tmpscore = tscore;
314 fprintf( stderr, "% 3d % 3d / the rest \n", l+1, s+1 );
315 display( mseq2, locnjob );
317 fprintf( stderr, "% 3d % 3d / the rest \n", l+1, s+1 );
318 fprintf( stderr, "score = %f mscore = %f ", tmpscore, mscore );
320 fprintf( trap_g, "%#4d %#4d / the rest ", l+1, s+1 );
321 fprintf( trap_g, "score = %f mscore = %f ", tmpscore, mscore );
325 fprintf( stderr, "( identical )\n" );
326 fprintf( trap_g, "( identical )\n" );
330 else if( tmpscore > mscore - cut )
332 fprintf( stderr, "accepted\n" );
333 fprintf( trap_g, "accepted\n" );
334 for( i=0; i<locnjob; i++ ) strcpy( aseq[i], mseq2[i] );
335 strcpy( sai, sai1 ); /* kokoka ? */
336 if( !tbitr && !tbweight )
338 writePre( locnjob, name, nlen, aseq, 0 );
343 tscore = tmpscore = score_calc0( aseq, locnjob, eff, s ); * ? *
345 if( tmpscore > mscore )
347 for( i=0; i<locnjob; i++ ) strcpy( bseq[i], mseq2[i] );
348 treeconstruction( bseq, locnjob, topol, len, eff );
349 tscore = mscore = score_calc0( bseq, locnjob, eff, s );
350 fprintf( trap_g, " -> %f\n", mscore );
351 strcpy( sai, sai1 ); /* kokoka ? */
353 if( !tbitr && !tbweight )
354 { prep = fopen( "best", "w" );
355 Write( prep, locnjob, name, nlen, bseq );
364 if( tmpscore == tscore )
366 fprintf( stderr, "occational coincidence \n" );
367 fprintf( trap_g, "occational coincidence\n" );
371 fprintf( stderr, "rejected\n" );
372 fprintf( trap_g, "rejected\n" );
374 for( i=0; i<locnjob; i++ ) strcpy( aseq[i], bseq[i] );
380 prep = fopen( "cur", "w" );
381 Write( prep, locnjob, name, nlen, mseq2 );
385 his[ou][s][cyc[ou]] = tmpscore;
386 if( !strcmp( sai, sai2 ) )
389 fprintf( trap_g, "converged\n" );
392 for( i=cyc[ou]-1; i>0; i-- )
394 if( tmpscore == his[ou][s][i] )
400 fprintf( stderr, "\n" );
404 fprintf( trap_g, "oscillating\n" );
408 if( l == MAXITERATION ) returnvalue = -2;
409 FreeDoubleCub( his );
410 return( returnvalue );