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;
49 static char *res1 = NULL, *res2 = NULL;
53 res1 = (char *)calloc( N, sizeof( char ) );
54 res2 = (char *)calloc( N, sizeof( char ) );
57 lgth1 = strlen( seq1[0] );
58 lgth2 = strlen( seq2[0] );
60 mspt1 = res1 + lgth1+lgth2;
62 mspt2 = res2 + lgth1+lgth2;
64 iin = endi; jin = endj;
67 if( used[iin][jin] ) return( -1.0 );
68 for( k=0; k<=limk; k++ )
70 ifi = ( ijpi[iin][jin] );
71 jfi = ( ijpj[iin][jin] );
73 if( used[ifi][jfi] ) return( -1.0 );
78 *--mspt1 = seq1[0][ifi+l];
86 *--mspt2 = seq2[0][jfi+l];
90 if( iin <= 0 || jin <= 0 ) break;
91 *--mspt1 = seq1[0][ifi];
92 *--mspt2 = seq2[0][jfi];
93 if( ijpi[ifi][jfi] == localstop ) break;
94 if( ijpj[ifi][jfi] == localstop ) break;
98 if( ifi == -1 ) *off1pt = 0; else *off1pt = ifi;
99 if( jfi == -1 ) *off2pt = 0; else *off2pt = jfi;
101 // fprintf( stderr, "ifn = %d, jfn = %d\n", ifi, jfi );
103 iin = endi; jin = endj;
105 for( k=0; k<=limk; k++ )
107 ifi = ( ijpi[iin][jin] );
108 jfi = ( ijpj[iin][jin] );
111 if( iin <= 0 || jin <= 0 ) break;
112 if( ijpi[ifi][jfi] == localstop ) break;
113 if( ijpj[ifi][jfi] == localstop ) break;
116 iin = ifi; jin = jfi;
120 strcpy( mseq1[0], mspt1 );
121 strcpy( mseq2[0], mspt2 );
123 fprintf( stderr, "mseq1=%s\nmseq2=%s\n", mspt1, mspt2 );
129 float suboptalign11( char **seq1, char **seq2, int alloclen, int *off1pt, int *off2pt, LocalHom *lhmpt )
130 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
135 int lasti, lastj; /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
138 float wm = 0.0; // by D.Mathog,
140 float *currentw, *previousw;
145 float *mjpt, *Mjpt, *prept, *curpt;
149 static float Mi, *largeM;
154 static float *w1, *w2;
155 // static float *match;
156 static float *initverticalw; /* kufuu sureba iranai */
157 static float *lastverticalw; /* kufuu sureba iranai */
160 static float **cpmx1;
161 static float **cpmx2;
162 static int **intwork;
163 static float **floatwork;
164 static int orlgth1 = 0, orlgth2 = 0;
169 // float localthr = 0.0;
170 // float localthr2 = 0.0;
171 float fpenalty = (float)penalty;
172 float fpenalty_OP = (float)penalty_OP;
173 float fpenalty_ex = (float)penalty_ex;
174 // float fpenalty_EX = (float)penalty_EX;
175 float foffset = (float)offset;
176 float localthr = -foffset;
177 float localthr2 = -foffset;
178 static Shuryoten *shuryo = NULL;
180 float minshuryowm = 0.0; // by D.Mathog
181 int minshuryopos = 0; // by D.Mathog
185 // fprintf( stderr, "@@@@@@@@@@@@@ penalty_OP = %f, penalty_EX = %f, pelanty = %f\n", fpenalty_OP, fpenalty_EX, fpenalty );
187 fprintf( stderr, "in suboptalign11\n" );
190 shuryo = (Shuryoten *)calloc( 100, sizeof( Shuryoten ) );
192 for( i=0; i<100; i++ )
205 lgth1 = strlen( seq1[0] );
206 lgth2 = strlen( seq2[0] );
208 fprintf( stderr, "in suboptalign11 step 1\n" );
210 if( lgth1 > orlgth1 || lgth2 > orlgth2 )
214 fprintf( stderr, "in suboptalign11 step 1.3\n" );
215 if( orlgth1 > 0 && orlgth2 > 0 )
217 fprintf( stderr, "in suboptalign11 step 1.4\n" );
220 // FreeFloatVec( match );
221 FreeFloatVec( initverticalw );
222 FreeFloatVec( lastverticalw );
223 fprintf( stderr, "in suboptalign11 step 1.5\n" );
227 FreeFloatVec( largeM );
229 fprintf( stderr, "in suboptalign11 step 1.6\n" );
232 FreeFloatMtx( cpmx1 );
233 FreeFloatMtx( cpmx2 );
235 fprintf( stderr, "in suboptalign11 step 1.7\n" );
236 FreeFloatMtx( floatwork );
237 FreeIntMtx( intwork );
240 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
241 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
244 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
247 w1 = AllocateFloatVec( ll2+2 );
248 w2 = AllocateFloatVec( ll2+2 );
249 // match = AllocateFloatVec( ll2+2 );
251 initverticalw = AllocateFloatVec( ll1+2 );
252 lastverticalw = AllocateFloatVec( ll1+2 );
254 m = AllocateFloatVec( ll2+2 );
255 mp = AllocateIntVec( ll2+2 );
256 largeM = AllocateFloatVec( ll2+2 );
257 Mp = AllocateIntVec( ll2+2 );
259 cpmx1 = AllocateFloatMtx( 26, ll1+2 );
260 cpmx2 = AllocateFloatMtx( 26, ll2+2 );
262 floatwork = AllocateFloatMtx( 26, MAX( ll1, ll2 )+2 );
263 intwork = AllocateIntMtx( 26, MAX( ll1, ll2 )+2 );
265 mseq1 = AllocateCharMtx( njob, ll1+ll2 );
266 mseq2 = AllocateCharMtx( njob, ll1+ll2 );
269 fprintf( stderr, "succeeded\n" );
275 fprintf( stderr, "in suboptalign11 step 1.6\n" );
279 fprintf( stderr, "in suboptalign11 step 2\n" );
281 if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
285 if( commonAlloc1 && commonAlloc2 )
287 FreeIntMtx( commonIP );
288 FreeIntMtx( commonJP );
292 ll1 = MAX( orlgth1, commonAlloc1 );
293 ll2 = MAX( orlgth2, commonAlloc2 );
296 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
299 used = AllocateIntMtx( ll1+10, ll2+10 );
300 commonIP = AllocateIntMtx( ll1+10, ll2+10 );
301 commonJP = AllocateIntMtx( ll1+10, ll2+10 );
304 fprintf( stderr, "succeeded\n\n" );
315 for( i=0; i<lgth1; i++ )
316 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
319 fprintf( stderr, "in suboptalign11 step 3\n" );
323 match_calc( initverticalw, seq2, seq1, 0, lgth1 );
325 match_calc( currentw, seq1, seq2, 0, lgth2 );
329 for( j=1; j<lasti; ++j )
331 m[j] = currentw[j-1]; mp[j] = 0;
332 largeM[j] = currentw[j-1]; Mp[j] = 0;
335 lastverticalw[0] = currentw[lgth2-1];
339 fprintf( stderr, "currentw = \n" );
340 for( i=0; i<lgth1+1; i++ )
342 fprintf( stderr, "%5.2f ", currentw[i] );
344 fprintf( stderr, "\n" );
345 fprintf( stderr, "initverticalw = \n" );
346 for( i=0; i<lgth2+1; i++ )
348 fprintf( stderr, "%5.2f ", initverticalw[i] );
350 fprintf( stderr, "\n" );
353 fprintf( stderr, "\n" );
354 fprintf( stderr, " " );
355 for( j=0; j<lgth2+1; j++ )
356 fprintf( stderr, "%c ", seq2[0][j] );
357 fprintf( stderr, "\n" );
360 localstop = lgth1+lgth2+1;
364 fprintf( stderr, "\n" );
365 fprintf( stderr, "%c ", seq1[0][0] );
367 for( j=0; j<lgth2+1; j++ )
368 fprintf( stderr, "%5.0f ", currentw[j] );
369 fprintf( stderr, "\n" );
373 for( i=1; i<lasti; i++ )
376 previousw = currentw;
379 previousw[0] = initverticalw[i-1];
381 match_calc( currentw, seq1, seq2, i, lgth2 );
383 fprintf( stderr, "%c ", seq1[0][i] );
384 fprintf( stderr, "%5.0f ", currentw[0] );
388 fprintf( stderr, "\n" );
389 fprintf( stderr, "i=%d\n", i );
390 fprintf( stderr, "currentw = \n" );
391 for( j=0; j<lgth2; j++ )
393 fprintf( stderr, "%5.2f ", currentw[j] );
395 fprintf( stderr, "\n" );
398 fprintf( stderr, "\n" );
399 fprintf( stderr, "i=%d\n", i );
400 fprintf( stderr, "currentw = \n" );
401 for( j=0; j<lgth2; j++ )
403 fprintf( stderr, "%5.2f ", currentw[j] );
405 fprintf( stderr, "\n" );
407 currentw[0] = initverticalw[i];
409 mi = previousw[0]; mpi = 0;
410 Mi = previousw[0]; Mpi = 0;
413 if( mi < localthr ) mi = localthr2;
416 ijpipt = ijpi[i] + 1;
417 ijpjpt = ijpj[i] + 1;
421 curpt = currentw + 1;
428 for( j=1; j<lastj; j++ )
435 // fprintf( stderr, "i,j=%d,%d %c-%c\n", i, j, seq1[0][i], seq2[0][j] );
436 // fprintf( stderr, "wm=%f\n", wm );
438 fprintf( stderr, "%5.0f->", wm );
442 fprintf( stderr, "%5.0f?", g );
462 fprintf( stderr, "%5.0f->", wm );
464 g = *mjpt + fpenalty;
466 fprintf( stderr, "m%5.0f?", g );
481 *mjpt += fpenalty_ex;
485 g = tbk + fpenalty_OP;
492 // fprintf( stderr, "hit! i%d, j%d, ijpi = %d, ijpj = %d\n", i, j, *ijpipt, *ijpjpt );
508 // tbk += fpenalty_EX;// + foffset;
516 // *Mjpt += fpenalty_EX;// + foffset;
524 // Mi += fpenalty_EX;// + foffset;
527 // fprintf( stderr, "wm=%f, tbk=%f(%c-%c), mi=%f, *mjpt=%f\n", wm, tbk, seq1[0][tbki], seq2[0][tbkj], mi, *mjpt );
528 // fprintf( stderr, "ijp = %c,%c\n", seq1[0][abs(*ijpipt)], seq2[0][abs(*ijpjpt)] );
539 if( numshuryo < 100 )
541 shuryo[numshuryo].i = i;
542 shuryo[numshuryo].j = j;
543 shuryo[numshuryo].wm = wm;
545 if( minshuryowm > wm )
548 minshuryopos = numshuryo;
554 if( wm > minshuryowm )
556 shuryo[minshuryopos].i = i;
557 shuryo[minshuryopos].j = j;
558 shuryo[minshuryopos].wm = wm;
560 for( k=0; k<100; k++ ) // muda
562 if( shuryo[k].wm < minshuryowm )
564 minshuryowm = shuryo[k].wm;
575 // fprintf( stderr, "stop i=%d, j=%d, curpt=%f\n", i, j, *curpt );
577 // *ijpjpt = localstop;
582 fprintf( stderr, "%5.0f ", *curpt );
585 fprintf( stderr, "%5.0f ", wm );
586 // fprintf( stderr, "%c-%c *ijppt = %d, localstop = %d\n", seq1[0][i], seq2[0][j], *ijppt, localstop );
600 fprintf( stderr, "\n" );
603 lastverticalw[i] = currentw[lgth2-1];
606 for( k=0; k<100; k++ )
608 fprintf( stderr, "shuryo[%d].i,j,wm = %d,%d,%f\n", k, shuryo[k].i, shuryo[k].j, shuryo[k].wm );
613 fprintf( stderr, "maxwm = %f\n", maxwm );
614 fprintf( stderr, "endali = %d\n", endali );
615 fprintf( stderr, "endalj = %d\n", endalj );
618 qsort( shuryo, 100, sizeof( Shuryoten ), (int (*)())compshuryo );
619 for( k=0; k<100; k++ )
621 fprintf( stderr, "shuryo[%d].i,j,wm = %d,%d,%f\n", k, shuryo[k].i, shuryo[k].j, shuryo[k].wm );
626 for( i=0; i<lasti; i++ )
628 ijpi[i][0] = localstop;
629 ijpj[i][0] = localstop;
632 for( j=0; j<lastj; j++ )
634 ijpi[0][j] = localstop;
635 ijpj[0][j] = localstop;
638 for( i=0; i<lasti; i++ ) for( j=0; j<lastj; j++ ) used[i][j] = 0;
640 for( k=0; k<numshuryo; k++ )
642 if( shuryo[k].wm < shuryo[0].wm * 0.3 ) break;
643 fprintf( stderr, "k=%d, shuryo[k].i,j,wm=%d,%d,%f go\n", k, shuryo[k].i, shuryo[k].j, shuryo[k].wm );
644 resf = gentracking( used, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijpi, ijpj, off1pt, off2pt, shuryo[k].i, shuryo[k].j );
645 if( resf == -1.0 ) continue;
646 putlocalhom3( mseq1[0], mseq2[0], lhmpt, *off1pt, *off2pt, (int)shuryo[k].wm, strlen( mseq1[0] ) );
648 fprintf( stderr, "\n" );
649 fprintf( stderr, ">\n%s\n", mseq1[0] );
650 fprintf( stderr, ">\n%s\n", mseq2[0] );
653 for( i=0; i<20; i++ )
655 for( j=0; j<20; j++ )
657 fprintf( stderr, "%2d ", used[i][j] );
659 fprintf( stderr, "\n" );
663 // fprintf( stderr, "### impmatch = %f\n", *impmatch );
665 resultlen = strlen( mseq1[0] );
666 if( alloclen < resultlen || resultlen > N )
668 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
669 ErrorExit( "LENGTH OVER!\n" );