7 #define USE_PENALTY_EX 1
9 static TLS int localstop;
12 static void match_calc( float *match, char **s1, char **s2, int i1, int lgth2 )
14 char tmpc = s1[0][i1];
18 *match++ = amino_dis[(int)tmpc][(int)*seq2++];
21 static void match_calc( float *match, char **s1, char **s2, int i1, int lgth2 )
25 for( j=0; j<lgth2; j++ )
26 match[j] = amino_dis[(*s1)[i1]][(*s2)[j]];
31 static void match_calc_bk( float *match, float **cpmx1, float **cpmx2, int i1, int lgth2, float **floatwork, int **intwork, int initialize )
35 float **cpmxpd = floatwork;
36 int **cpmxpdn = intwork;
41 for( j=0; j<lgth2; j++ )
48 cpmxpd[count][j] = cpmx2[l][j];
49 cpmxpdn[count][j] = l;
53 cpmxpdn[count][j] = -1;
61 scarr[l] += n_dis[k][l] * cpmx1[k][i1];
65 float *fpt, **fptpt, *fpt2;
73 ipt=*iptpt,fpt=*fptpt;
75 *fpt2 += scarr[*ipt++] * *fpt++;
76 fpt2++,iptpt++,fptpt++;
80 for( j=0; j<lgth2; j++ )
83 for( k=0; cpmxpdn[k][j]>-1; k++ )
84 match[j] += scarr[cpmxpdn[k][j]] * cpmxpd[k][j];
90 static float gentracking( float *lasthorizontalw, float *lastverticalw,
91 char **seq1, char **seq2,
92 char **mseq1, char **mseq2,
93 float **cpmx1, float **cpmx2,
94 int **ijpi, int **ijpj, int *off1pt, int *off2pt, int endi, int endj )
96 int i, j, l, iin, jin, lgth1, lgth2, k, limk;
97 int ifi=0, jfi=0; // by D.Mathog
101 lgth1 = strlen( seq1[0] );
102 lgth2 = strlen( seq2[0] );
105 for( i=0; i<lgth1; i++ )
107 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
111 for( i=0; i<lgth1+1; i++ )
113 ijpi[i][0] = localstop;
114 ijpj[i][0] = localstop;
116 for( j=0; j<lgth2+1; j++ )
118 ijpi[0][j] = localstop;
119 ijpj[0][j] = localstop;
122 mseq1[0] += lgth1+lgth2;
124 mseq2[0] += lgth1+lgth2;
126 iin = endi; jin = endj;
128 for( k=0; k<=limk; k++ )
131 ifi = ( ijpi[iin][jin] );
132 jfi = ( ijpj[iin][jin] );
134 // if( ijpi[iin][jin] < 0 || ijpj[iin][jin] < 0 )
136 // fprintf( stderr, "skip! %d-%d\n", ijpi[iin][jin], ijpj[iin][jin] );
137 // fprintf( stderr, "1: %c-%c\n", seq1[0][iin], seq1[0][ifi] );
138 // fprintf( stderr, "2: %c-%c\n", seq2[0][jin], seq2[0][jfi] );
142 *--mseq1[0] = seq1[0][ifi+l];
150 *--mseq2[0] = seq2[0][jfi+l];
154 if( iin <= 0 || jin <= 0 ) break;
155 *--mseq1[0] = seq1[0][ifi];
156 *--mseq2[0] = seq2[0][jfi];
158 if( ijpi[ifi][jfi] == localstop ) break;
159 if( ijpj[ifi][jfi] == localstop ) break;
161 iin = ifi; jin = jfi;
163 if( ifi == -1 ) *off1pt = 0; else *off1pt = ifi;
164 if( jfi == -1 ) *off2pt = 0; else *off2pt = jfi;
166 // fprintf( stderr, "ifn = %d, jfn = %d\n", ifi, jfi );
173 float genL__align11( char **seq1, char **seq2, int alloclen, int *off1pt, int *off2pt )
174 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
181 float wm = 0.0; /* int ?????? */
183 float *currentw, *previousw;
188 float *mjpt, *Mjpt, *prept, *curpt;
191 static TLS float mi, *m;
192 static TLS float Mi, *largeM;
193 static TLS int **ijpi;
194 static TLS int **ijpj;
195 static TLS int mpi, *mp;
196 static TLS int Mpi, *Mp;
197 static TLS float *w1, *w2;
198 static TLS float *match;
199 static TLS float *initverticalw; /* kufuu sureba iranai */
200 static TLS float *lastverticalw; /* kufuu sureba iranai */
201 static TLS char **mseq1;
202 static TLS char **mseq2;
203 static TLS char **mseq;
204 static TLS float **cpmx1;
205 static TLS float **cpmx2;
206 static TLS int **intwork;
207 static TLS float **floatwork;
208 static TLS int orlgth1 = 0, orlgth2 = 0;
213 // float localthr = 0.0;
214 // float localthr2 = 0.0;
215 float fpenalty = (float)penalty;
216 float fpenalty_OP = (float)penalty_OP;
217 float fpenalty_ex = (float)penalty_ex;
218 // float fpenalty_EX = (float)penalty_EX;
219 float foffset = (float)offset;
220 float localthr = -foffset;
221 float localthr2 = -foffset;
225 if( orlgth1 > 0 && orlgth2 > 0 )
233 FreeFloatVec( match );
234 FreeFloatVec( initverticalw );
235 FreeFloatVec( lastverticalw );
244 FreeFloatMtx( cpmx1 );
245 FreeFloatMtx( cpmx2 );
247 FreeFloatMtx( floatwork );
248 FreeIntMtx( intwork );
256 // fprintf( stderr, "@@@@@@@@@@@@@ penalty_OP = %f, penalty_EX = %f, pelanty = %f\n", fpenalty_OP, fpenalty_EX, fpenalty );
260 mseq1 = AllocateCharMtx( njob, 0 );
261 mseq2 = AllocateCharMtx( njob, 0 );
265 lgth1 = strlen( seq1[0] );
266 lgth2 = strlen( seq2[0] );
268 if( lgth1 > orlgth1 || lgth2 > orlgth2 )
272 if( orlgth1 > 0 && orlgth2 > 0 )
276 FreeFloatVec( match );
277 FreeFloatVec( initverticalw );
278 FreeFloatVec( lastverticalw );
282 FreeFloatVec( largeM );
287 FreeFloatMtx( cpmx1 );
288 FreeFloatMtx( cpmx2 );
290 FreeFloatMtx( floatwork );
291 FreeIntMtx( intwork );
294 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
295 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
298 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
301 w1 = AllocateFloatVec( ll2+2 );
302 w2 = AllocateFloatVec( ll2+2 );
303 match = AllocateFloatVec( ll2+2 );
305 initverticalw = AllocateFloatVec( ll1+2 );
306 lastverticalw = AllocateFloatVec( ll1+2 );
308 m = AllocateFloatVec( ll2+2 );
309 mp = AllocateIntVec( ll2+2 );
310 largeM = AllocateFloatVec( ll2+2 );
311 Mp = AllocateIntVec( ll2+2 );
313 mseq = AllocateCharMtx( njob, ll1+ll2 );
315 cpmx1 = AllocateFloatMtx( 26, ll1+2 );
316 cpmx2 = AllocateFloatMtx( 26, ll2+2 );
318 floatwork = AllocateFloatMtx( 26, MAX( ll1, ll2 )+2 );
319 intwork = AllocateIntMtx( 26, MAX( ll1, ll2 )+2 );
322 fprintf( stderr, "succeeded\n" );
334 if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
338 if( commonAlloc1 && commonAlloc2 )
340 FreeIntMtx( commonIP );
341 FreeIntMtx( commonJP );
344 ll1 = MAX( orlgth1, commonAlloc1 );
345 ll2 = MAX( orlgth2, commonAlloc2 );
348 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
351 commonIP = AllocateIntMtx( ll1+10, ll2+10 );
352 commonJP = AllocateIntMtx( ll1+10, ll2+10 );
355 fprintf( stderr, "succeeded\n\n" );
366 for( i=0; i<lgth1; i++ )
367 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
373 match_calc( initverticalw, seq2, seq1, 0, lgth1 );
375 match_calc( currentw, seq1, seq2, 0, lgth2 );
379 for( j=1; j<lasti; ++j )
381 m[j] = currentw[j-1]; mp[j] = 0;
382 largeM[j] = currentw[j-1]; Mp[j] = 0;
385 lastverticalw[0] = currentw[lgth2-1];
389 fprintf( stderr, "currentw = \n" );
390 for( i=0; i<lgth1+1; i++ )
392 fprintf( stderr, "%5.2f ", currentw[i] );
394 fprintf( stderr, "\n" );
395 fprintf( stderr, "initverticalw = \n" );
396 for( i=0; i<lgth2+1; i++ )
398 fprintf( stderr, "%5.2f ", initverticalw[i] );
400 fprintf( stderr, "\n" );
403 fprintf( stderr, "\n" );
404 fprintf( stderr, " " );
405 for( j=0; j<lgth2+1; j++ )
406 fprintf( stderr, "%c ", seq2[0][j] );
407 fprintf( stderr, "\n" );
410 localstop = lgth1+lgth2+1;
411 maxwm = -999999999.9;
414 fprintf( stderr, "\n" );
415 fprintf( stderr, "%c ", seq1[0][0] );
417 for( j=0; j<lgth2+1; j++ )
418 fprintf( stderr, "%5.0f ", currentw[j] );
419 fprintf( stderr, "\n" );
423 for( i=1; i<lasti; i++ )
426 previousw = currentw;
429 previousw[0] = initverticalw[i-1];
431 match_calc( currentw, seq1, seq2, i, lgth2 );
433 fprintf( stderr, "%c ", seq1[0][i] );
434 fprintf( stderr, "%5.0f ", currentw[0] );
438 fprintf( stderr, "\n" );
439 fprintf( stderr, "i=%d\n", i );
440 fprintf( stderr, "currentw = \n" );
441 for( j=0; j<lgth2; j++ )
443 fprintf( stderr, "%5.2f ", currentw[j] );
445 fprintf( stderr, "\n" );
448 fprintf( stderr, "\n" );
449 fprintf( stderr, "i=%d\n", i );
450 fprintf( stderr, "currentw = \n" );
451 for( j=0; j<lgth2; j++ )
453 fprintf( stderr, "%5.2f ", currentw[j] );
455 fprintf( stderr, "\n" );
457 currentw[0] = initverticalw[i];
459 mi = previousw[0]; mpi = 0;
460 Mi = previousw[0]; Mpi = 0;
463 if( mi < localthr ) mi = localthr2;
466 ijpipt = ijpi[i] + 1;
467 ijpjpt = ijpj[i] + 1;
471 curpt = currentw + 1;
478 for( j=1; j<lastj; j++ )
485 // fprintf( stderr, "i,j=%d,%d %c-%c\n", i, j, seq1[0][i], seq2[0][j] );
486 // fprintf( stderr, "wm=%f\n", wm );
488 fprintf( stderr, "%5.0f->", wm );
492 fprintf( stderr, "%5.0f?", g );
512 fprintf( stderr, "%5.0f->", wm );
514 g = *mjpt + fpenalty;
516 fprintf( stderr, "m%5.0f?", g );
522 *ijpjpt = j - 1; //IRU!
531 *mjpt += fpenalty_ex;
535 g = tbk + fpenalty_OP;
542 // fprintf( stderr, "hit! i%d, j%d, ijpi = %d, ijpj = %d\n", i, j, *ijpipt, *ijpjpt );
547 tbk = Mi; //error desu.
558 // tbk += fpenalty_EX;// + foffset;
566 // *Mjpt += fpenalty_EX;// + foffset;
574 // Mi += fpenalty_EX;// + foffset;
577 // fprintf( stderr, "wm=%f, tbk=%f(%c-%c), mi=%f, *mjpt=%f\n", wm, tbk, seq1[0][tbki], seq2[0][tbkj], mi, *mjpt );
578 // fprintf( stderr, "ijp = %c,%c\n", seq1[0][abs(*ijpipt)], seq2[0][abs(*ijpjpt)] );
590 // fprintf( stderr, "stop i=%d, j=%d, curpt=%f\n", i, j, *curpt );
592 // *ijpjpt = localstop;
597 fprintf( stderr, "%5.0f ", *curpt );
600 fprintf( stderr, "%5.0f ", wm );
601 // fprintf( stderr, "%c-%c *ijppt = %d, localstop = %d\n", seq1[0][i], seq2[0][j], *ijppt, localstop );
615 fprintf( stderr, "\n" );
618 lastverticalw[i] = currentw[lgth2-1];
623 fprintf( stderr, "maxwm = %f\n", maxwm );
624 fprintf( stderr, "endali = %d\n", endali );
625 fprintf( stderr, "endalj = %d\n", endalj );
628 if( ijpi[endali][endalj] == localstop ) // && ijpj[endali][endalj] == localstop )
630 strcpy( seq1[0], "" );
631 strcpy( seq2[0], "" );
632 *off1pt = *off2pt = 0;
637 gentracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijpi, ijpj, off1pt, off2pt, endali, endalj );
639 // fprintf( stderr, "### impmatch = %f\n", *impmatch );
641 resultlen = strlen( mseq1[0] );
642 if( alloclen < resultlen || resultlen > N )
644 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
645 ErrorExit( "LENGTH OVER!\n" );
649 strcpy( seq1[0], mseq1[0] );
650 strcpy( seq2[0], mseq2[0] );
653 fprintf( stderr, "\n" );
654 fprintf( stderr, ">\n%s\n", mseq1[0] );
655 fprintf( stderr, ">\n%s\n", mseq2[0] );