7 #define USE_PENALTY_EX 1
9 typedef struct _shuryoten
14 struct _shuryoten *next;
15 struct _shuryoten *prev;
21 static int compshuryo( Shuryoten *s1_arg, Shuryoten *s2_arg )
23 Shuryoten *s1 = (Shuryoten *)s1_arg;
24 Shuryoten *s2 = (Shuryoten *)s2_arg;
25 if ( s1->wm > s2->wm ) return( -1 );
26 else if ( s1->wm < s2->wm ) return( 1 );
30 static void match_calc( float *match, char **s1, char **s2, int i1, int lgth2 )
34 for( j=0; j<lgth2; j++ )
35 match[j] = amino_dis[(int)(*s1)[i1]][(int)(*s2)[j]];
38 static float gentracking( int **used,
39 char **seq1, char **seq2,
40 char **mseq1, char **mseq2,
41 float **cpmx1, float **cpmx2,
42 int **ijpi, int **ijpj, int *off1pt, int *off2pt, int endi, int endj )
44 int l, iin, jin, lgth1, lgth2, k, limk;
47 static char *res1 = NULL, *res2 = NULL;
51 res1 = (char *)calloc( N, sizeof( char ) );
52 res2 = (char *)calloc( N, sizeof( char ) );
55 lgth1 = strlen( seq1[0] );
56 lgth2 = strlen( seq2[0] );
58 mspt1 = res1 + lgth1+lgth2;
60 mspt2 = res2 + lgth1+lgth2;
62 iin = endi; jin = endj;
65 if( used[iin][jin] ) return( -1.0 );
66 for( k=0; k<=limk; k++ )
68 ifi = ( ijpi[iin][jin] );
69 jfi = ( ijpj[iin][jin] );
71 if( used[ifi][jfi] ) return( -1.0 );
76 *--mspt1 = seq1[0][ifi+l];
84 *--mspt2 = seq2[0][jfi+l];
88 if( iin <= 0 || jin <= 0 ) break;
89 *--mspt1 = seq1[0][ifi];
90 *--mspt2 = seq2[0][jfi];
91 if( ijpi[ifi][jfi] == localstop ) break;
92 if( ijpj[ifi][jfi] == localstop ) break;
96 if( ifi == -1 ) *off1pt = 0; else *off1pt = ifi;
97 if( jfi == -1 ) *off2pt = 0; else *off2pt = jfi;
99 // fprintf( stderr, "ifn = %d, jfn = %d\n", ifi, jfi );
101 iin = endi; jin = endj;
103 for( k=0; k<=limk; k++ )
105 ifi = ( ijpi[iin][jin] );
106 jfi = ( ijpj[iin][jin] );
109 if( iin <= 0 || jin <= 0 ) break;
110 if( ijpi[ifi][jfi] == localstop ) break;
111 if( ijpj[ifi][jfi] == localstop ) break;
114 iin = ifi; jin = jfi;
118 strcpy( mseq1[0], mspt1 );
119 strcpy( mseq2[0], mspt2 );
121 fprintf( stderr, "mseq1=%s\nmseq2=%s\n", mspt1, mspt2 );
127 float suboptalign11( char **seq1, char **seq2, int alloclen, int *off1pt, int *off2pt, LocalHom *lhmpt )
128 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
133 int lasti, lastj; /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
136 float wm = 0.0; // by D.Mathog,
138 float *currentw, *previousw;
143 float *mjpt, *Mjpt, *prept, *curpt;
147 static float Mi, *largeM;
152 static float *w1, *w2;
153 // static float *match;
154 static float *initverticalw; /* kufuu sureba iranai */
155 static float *lastverticalw; /* kufuu sureba iranai */
158 static float **cpmx1;
159 static float **cpmx2;
160 static int **intwork;
161 static float **floatwork;
162 static int orlgth1 = 0, orlgth2 = 0;
167 // float localthr = 0.0;
168 // float localthr2 = 0.0;
169 float fpenalty = (float)penalty;
170 float fpenalty_OP = (float)penalty_OP;
171 float fpenalty_ex = (float)penalty_ex;
172 // float fpenalty_EX = (float)penalty_EX;
173 float foffset = (float)offset;
174 float localthr = -foffset;
175 float localthr2 = -foffset;
176 static Shuryoten *shuryo = NULL;
178 float minshuryowm = 0.0; // by D.Mathog
179 int minshuryopos = 0; // by D.Mathog
183 // fprintf( stderr, "@@@@@@@@@@@@@ penalty_OP = %f, penalty_EX = %f, pelanty = %f\n", fpenalty_OP, fpenalty_EX, fpenalty );
185 fprintf( stderr, "in suboptalign11\n" );
188 shuryo = (Shuryoten *)calloc( 100, sizeof( Shuryoten ) );
190 for( i=0; i<100; i++ )
203 lgth1 = strlen( seq1[0] );
204 lgth2 = strlen( seq2[0] );
206 fprintf( stderr, "in suboptalign11 step 1\n" );
208 if( lgth1 > orlgth1 || lgth2 > orlgth2 )
212 fprintf( stderr, "in suboptalign11 step 1.3\n" );
213 if( orlgth1 > 0 && orlgth2 > 0 )
215 fprintf( stderr, "in suboptalign11 step 1.4\n" );
218 // FreeFloatVec( match );
219 FreeFloatVec( initverticalw );
220 FreeFloatVec( lastverticalw );
221 fprintf( stderr, "in suboptalign11 step 1.5\n" );
225 FreeFloatVec( largeM );
227 fprintf( stderr, "in suboptalign11 step 1.6\n" );
230 FreeFloatMtx( cpmx1 );
231 FreeFloatMtx( cpmx2 );
233 fprintf( stderr, "in suboptalign11 step 1.7\n" );
234 FreeFloatMtx( floatwork );
235 FreeIntMtx( intwork );
238 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
239 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
242 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
245 w1 = AllocateFloatVec( ll2+2 );
246 w2 = AllocateFloatVec( ll2+2 );
247 // match = AllocateFloatVec( ll2+2 );
249 initverticalw = AllocateFloatVec( ll1+2 );
250 lastverticalw = AllocateFloatVec( ll1+2 );
252 m = AllocateFloatVec( ll2+2 );
253 mp = AllocateIntVec( ll2+2 );
254 largeM = AllocateFloatVec( ll2+2 );
255 Mp = AllocateIntVec( ll2+2 );
257 cpmx1 = AllocateFloatMtx( 26, ll1+2 );
258 cpmx2 = AllocateFloatMtx( 26, ll2+2 );
260 floatwork = AllocateFloatMtx( 26, MAX( ll1, ll2 )+2 );
261 intwork = AllocateIntMtx( 26, MAX( ll1, ll2 )+2 );
263 mseq1 = AllocateCharMtx( njob, ll1+ll2 );
264 mseq2 = AllocateCharMtx( njob, ll1+ll2 );
267 fprintf( stderr, "succeeded\n" );
273 fprintf( stderr, "in suboptalign11 step 1.6\n" );
277 fprintf( stderr, "in suboptalign11 step 2\n" );
279 if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
283 if( commonAlloc1 && commonAlloc2 )
285 FreeIntMtx( commonIP );
286 FreeIntMtx( commonJP );
290 ll1 = MAX( orlgth1, commonAlloc1 );
291 ll2 = MAX( orlgth2, commonAlloc2 );
294 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
297 used = AllocateIntMtx( ll1+10, ll2+10 );
298 commonIP = AllocateIntMtx( ll1+10, ll2+10 );
299 commonJP = AllocateIntMtx( ll1+10, ll2+10 );
302 fprintf( stderr, "succeeded\n\n" );
313 for( i=0; i<lgth1; i++ )
314 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
317 fprintf( stderr, "in suboptalign11 step 3\n" );
321 match_calc( initverticalw, seq2, seq1, 0, lgth1 );
323 match_calc( currentw, seq1, seq2, 0, lgth2 );
327 for( j=1; j<lasti; ++j )
329 m[j] = currentw[j-1]; mp[j] = 0;
330 largeM[j] = currentw[j-1]; Mp[j] = 0;
333 lastverticalw[0] = currentw[lgth2-1];
337 fprintf( stderr, "currentw = \n" );
338 for( i=0; i<lgth1+1; i++ )
340 fprintf( stderr, "%5.2f ", currentw[i] );
342 fprintf( stderr, "\n" );
343 fprintf( stderr, "initverticalw = \n" );
344 for( i=0; i<lgth2+1; i++ )
346 fprintf( stderr, "%5.2f ", initverticalw[i] );
348 fprintf( stderr, "\n" );
351 fprintf( stderr, "\n" );
352 fprintf( stderr, " " );
353 for( j=0; j<lgth2+1; j++ )
354 fprintf( stderr, "%c ", seq2[0][j] );
355 fprintf( stderr, "\n" );
358 localstop = lgth1+lgth2+1;
362 fprintf( stderr, "\n" );
363 fprintf( stderr, "%c ", seq1[0][0] );
365 for( j=0; j<lgth2+1; j++ )
366 fprintf( stderr, "%5.0f ", currentw[j] );
367 fprintf( stderr, "\n" );
371 for( i=1; i<lasti; i++ )
374 previousw = currentw;
377 previousw[0] = initverticalw[i-1];
379 match_calc( currentw, seq1, seq2, i, lgth2 );
381 fprintf( stderr, "%c ", seq1[0][i] );
382 fprintf( stderr, "%5.0f ", currentw[0] );
386 fprintf( stderr, "\n" );
387 fprintf( stderr, "i=%d\n", i );
388 fprintf( stderr, "currentw = \n" );
389 for( j=0; j<lgth2; j++ )
391 fprintf( stderr, "%5.2f ", currentw[j] );
393 fprintf( stderr, "\n" );
396 fprintf( stderr, "\n" );
397 fprintf( stderr, "i=%d\n", i );
398 fprintf( stderr, "currentw = \n" );
399 for( j=0; j<lgth2; j++ )
401 fprintf( stderr, "%5.2f ", currentw[j] );
403 fprintf( stderr, "\n" );
405 currentw[0] = initverticalw[i];
407 mi = previousw[0]; mpi = 0;
408 Mi = previousw[0]; Mpi = 0;
411 if( mi < localthr ) mi = localthr2;
414 ijpipt = ijpi[i] + 1;
415 ijpjpt = ijpj[i] + 1;
419 curpt = currentw + 1;
426 for( j=1; j<lastj; j++ )
433 // fprintf( stderr, "i,j=%d,%d %c-%c\n", i, j, seq1[0][i], seq2[0][j] );
434 // fprintf( stderr, "wm=%f\n", wm );
436 fprintf( stderr, "%5.0f->", wm );
440 fprintf( stderr, "%5.0f?", g );
460 fprintf( stderr, "%5.0f->", wm );
462 g = *mjpt + fpenalty;
464 fprintf( stderr, "m%5.0f?", g );
479 *mjpt += fpenalty_ex;
483 g = tbk + fpenalty_OP;
490 // fprintf( stderr, "hit! i%d, j%d, ijpi = %d, ijpj = %d\n", i, j, *ijpipt, *ijpjpt );
506 // tbk += fpenalty_EX;// + foffset;
514 // *Mjpt += fpenalty_EX;// + foffset;
522 // Mi += fpenalty_EX;// + foffset;
525 // fprintf( stderr, "wm=%f, tbk=%f(%c-%c), mi=%f, *mjpt=%f\n", wm, tbk, seq1[0][tbki], seq2[0][tbkj], mi, *mjpt );
526 // fprintf( stderr, "ijp = %c,%c\n", seq1[0][abs(*ijpipt)], seq2[0][abs(*ijpjpt)] );
537 if( numshuryo < 100 )
539 shuryo[numshuryo].i = i;
540 shuryo[numshuryo].j = j;
541 shuryo[numshuryo].wm = wm;
543 if( minshuryowm > wm )
546 minshuryopos = numshuryo;
552 if( wm > minshuryowm )
554 shuryo[minshuryopos].i = i;
555 shuryo[minshuryopos].j = j;
556 shuryo[minshuryopos].wm = wm;
558 for( k=0; k<100; k++ ) // muda
560 if( shuryo[k].wm < minshuryowm )
562 minshuryowm = shuryo[k].wm;
573 // fprintf( stderr, "stop i=%d, j=%d, curpt=%f\n", i, j, *curpt );
575 // *ijpjpt = localstop;
580 fprintf( stderr, "%5.0f ", *curpt );
583 fprintf( stderr, "%5.0f ", wm );
584 // fprintf( stderr, "%c-%c *ijppt = %d, localstop = %d\n", seq1[0][i], seq2[0][j], *ijppt, localstop );
598 fprintf( stderr, "\n" );
601 lastverticalw[i] = currentw[lgth2-1];
604 for( k=0; k<100; k++ )
606 fprintf( stderr, "shuryo[%d].i,j,wm = %d,%d,%f\n", k, shuryo[k].i, shuryo[k].j, shuryo[k].wm );
611 fprintf( stderr, "maxwm = %f\n", maxwm );
612 fprintf( stderr, "endali = %d\n", endali );
613 fprintf( stderr, "endalj = %d\n", endalj );
616 qsort( shuryo, 100, sizeof( Shuryoten ), (int (*)())compshuryo );
617 for( k=0; k<100; k++ )
619 fprintf( stderr, "shuryo[%d].i,j,wm = %d,%d,%f\n", k, shuryo[k].i, shuryo[k].j, shuryo[k].wm );
624 for( i=0; i<lasti; i++ )
626 ijpi[i][0] = localstop;
627 ijpj[i][0] = localstop;
630 for( j=0; j<lastj; j++ )
632 ijpi[0][j] = localstop;
633 ijpj[0][j] = localstop;
636 for( i=0; i<lasti; i++ ) for( j=0; j<lastj; j++ ) used[i][j] = 0;
638 for( k=0; k<numshuryo; k++ )
640 if( shuryo[k].wm < shuryo[0].wm * 0.3 ) break;
641 fprintf( stderr, "k=%d, shuryo[k].i,j,wm=%d,%d,%f go\n", k, shuryo[k].i, shuryo[k].j, shuryo[k].wm );
642 resf = gentracking( used, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijpi, ijpj, off1pt, off2pt, shuryo[k].i, shuryo[k].j );
643 if( resf == -1.0 ) continue;
644 putlocalhom3( mseq1[0], mseq2[0], lhmpt, *off1pt, *off2pt, (int)shuryo[k].wm, strlen( mseq1[0] ) );
646 fprintf( stderr, "\n" );
647 fprintf( stderr, ">\n%s\n", mseq1[0] );
648 fprintf( stderr, ">\n%s\n", mseq2[0] );
651 for( i=0; i<20; i++ )
653 for( j=0; j<20; j++ )
655 fprintf( stderr, "%2d ", used[i][j] );
657 fprintf( stderr, "\n" );
661 // fprintf( stderr, "### impmatch = %f\n", *impmatch );
663 resultlen = strlen( mseq1[0] );
664 if( alloclen < resultlen || resultlen > N )
666 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
667 ErrorExit( "LENGTH OVER!\n" );