8 #define USE_PENALTY_EX 0
9 #define FASTMATCHCALC 1
12 static float **impmtx = NULL;
13 static int impalloclen = 0;
15 #if 1 // tditeration to naiveQscore_imp de tsukawareru.
16 float part_imp_match_out_scQ( int i1, int j1 )
18 // fprintf( stderr, "imp+match = %f\n", impmtx[i1][j1] * fastathreshold );
19 // fprintf( stderr, "val = %f\n", impmtx[i1][j1] );
20 return( impmtx[i1][j1] );
24 static void part_imp_match_out_veadQ_gapmap( float *imp, int i1, int lgth2, int start2, int *gapmap2 )
27 float *pt = impmtx[i1];
32 float *pt = impmtx[i1];
33 for( j=0; j<lgth2; j++ )
34 *imp++ += pt[start2+gapmap2[j]];
38 static void part_imp_match_out_vead_tateQ_gapmap( float *imp, int j1, int lgth1, int start1, int *gapmap1 )
41 for( i=0; i<lgth1; i++ )
42 *imp++ += impmtx[start1+gapmap1[i]][j1];
45 #if 0 // nocount wo tamesu
46 void part_imp_match_init_strictQ( float *imp, int clus1, int clus2, int lgth1, int lgth2, char **seq1, char **seq2, double *eff1, double *eff2, LocalHom ***localhom, int forscore )
48 int i, j, k1, k2, tmpint, start1, start2, end1, end2;
49 static int impalloclen = 0;
53 static char *nocount1 = NULL;
54 static char *nocount2 = NULL;
57 if( impalloclen < lgth1 + 2 || impalloclen < lgth2 + 2 )
59 if( impmtx ) FreeFloatMtx( impmtx );
60 if( nocount1 ) free( nocount1 );
61 if( nocount2 ) free( nocount2 );
62 impalloclen = MAX( lgth1, lgth2 ) + 2;
63 impmtx = AllocateFloatMtx( impalloclen, impalloclen );
64 nocount1 = AllocateCharVec( impalloclen );
65 nocount2 = AllocateCharVec( impalloclen );
68 for( i=0; i<lgth1; i++ )
70 for( j=0; j<clus1; j++ )
71 if( seq1[j][i] == '-' ) break;
72 if( j != clus1 ) nocount1[i] = 1;
75 for( i=0; i<lgth2; i++ )
77 for( j=0; j<clus2; j++ )
78 if( seq2[j][i] == '-' ) break;
79 if( j != clus2 ) nocount2[i] = 1;
84 fprintf( stderr, "nocount2 =\n" );
85 for( i = 0; i<impalloclen; i++ )
87 fprintf( stderr, "nocount2[%d] = %d (%c)\n", i, nocount2[i], seq2[0][i] );
94 fprintf( stderr, "eff1 in _init_strict = \n" );
95 for( i=0; i<clus1; i++ )
96 fprintf( stderr, "eff1[] = %f\n", eff1[i] );
97 for( i=0; i<clus2; i++ )
98 fprintf( stderr, "eff2[] = %f\n", eff2[i] );
101 for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
103 effijx = fastathreshold;
104 for( i=0; i<clus1; i++ )
106 for( j=0; j<clus2; j++ )
108 effij = (float)( eff1[i] * eff2[j] * effijx );
109 tmpptr = localhom[i][j];
112 // fprintf( stderr, "start1 = %d\n", tmpptr->start1 );
113 // fprintf( stderr, "end1 = %d\n", tmpptr->end1 );
114 // fprintf( stderr, "i = %d, seq1 = \n%s\n", i, seq1[i] );
115 // fprintf( stderr, "j = %d, seq2 = \n%s\n", j, seq2[j] );
120 if( *pt++ != '-' ) tmpint++;
121 if( tmpint == tmpptr->start1 ) break;
123 start1 = pt - seq1[i] - 1;
125 if( tmpptr->start1 == tmpptr->end1 ) end1 = start1;
131 // fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, tmpptr->end1, pt-seq1[i] );
132 if( tmpint == tmpptr->end1 ) break;
133 if( *pt++ != '-' ) tmpint++;
135 end1 = pt - seq1[i] - 0;
139 // fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, tmpptr->end1, pt-seq1[i] );
140 if( *pt++ != '-' ) tmpint++;
141 if( tmpint == tmpptr->end1 ) break;
143 end1 = pt - seq1[i] - 1;
151 if( *pt++ != '-' ) tmpint++;
152 if( tmpint == tmpptr->start2 ) break;
154 start2 = pt - seq2[j] - 1;
155 if( tmpptr->start2 == tmpptr->end2 ) end2 = start2;
161 if( tmpint == tmpptr->end2 ) break;
162 if( *pt++ != '-' ) tmpint++;
164 end2 = pt - seq2[j] - 0;
168 if( *pt++ != '-' ) tmpint++;
169 if( tmpint == tmpptr->end2 ) break;
171 end2 = pt - seq2[j] - 1;
174 // fprintf( stderr, "start1 = %d (%c), end1 = %d (%c), start2 = %d (%c), end2 = %d (%c)\n", start1, seq1[i][start1], end1, seq1[i][end1], start2, seq2[j][start2], end2, seq2[j][end2] );
175 // fprintf( stderr, "step 0\n" );
176 if( end1 - start1 != end2 - start2 )
178 // fprintf( stderr, "CHUUI!!, start1 = %d, end1 = %d, start2 = %d, end2 = %d\n", start1, end1, start2, end2 );
182 k1 = start1; k2 = start2;
185 while( *pt1 && *pt2 )
187 if( *pt1 != '-' && *pt2 != '-' )
189 // ½Å¤ß¤òÆó½Å¤Ë¤«¤±¤Ê¤¤¤è¤¦¤ËÃðդ·¤Æ²¼¤µ¤¤¡£
190 // impmtx[k1][k2] += tmpptr->wimportance * fastathreshold;
191 // impmtx[k1][k2] += tmpptr->importance * effij;
192 impmtx[k1][k2] += tmpptr->fimportance * effij;
193 // fprintf( stderr, "#### impmtx[k1][k2] = %f, tmpptr->fimportance=%f, effij=%f\n", impmtx[k1][k2], tmpptr->fimportance, effij );
194 // fprintf( stderr, "mark, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
195 // fprintf( stderr, "%d (%c) - %d (%c) - %f\n", k1, *pt1, k2, *pt2, tmpptr->fimportance * effij );
199 else if( *pt1 != '-' && *pt2 == '-' )
201 // fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
204 else if( *pt1 == '-' && *pt2 != '-' )
206 // fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
209 else if( *pt1 == '-' && *pt2 == '-' )
211 // fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
215 if( k1 > end1 || k2 > end2 ) break;
218 while( k1 <= end1 && k2 <= end2 )
220 fprintf( stderr, "k1,k2=%d,%d - ", k1, k2 );
221 if( !nocount1[k1] && !nocount2[k2] )
223 impmtx[k1][k2] += tmpptr->wimportance * eff1[i] * eff2[j] * fastathreshold;
224 fprintf( stderr, "marked\n" );
227 fprintf( stderr, "no count\n" );
231 tmpptr = tmpptr->next;
236 if( clus1 == 1 && clus2 == 6 )
238 fprintf( stderr, "\n" );
239 fprintf( stderr, "seq1[0] = %s\n", seq1[0] );
240 fprintf( stderr, "seq2[0] = %s\n", seq2[0] );
241 fprintf( stderr, "impmtx = \n" );
242 for( k2=0; k2<lgth2; k2++ )
243 fprintf( stderr, "%6.3f ", (double)k2 );
244 fprintf( stderr, "\n" );
245 for( k1=0; k1<lgth1; k1++ )
247 fprintf( stderr, "%d ", k1 );
248 for( k2=0; k2<3; k2++ )
249 fprintf( stderr, "%2.1f ", impmtx[k1][k2] );
250 fprintf( stderr, "\n" );
257 void part_imp_match_init_strictQ( float *imp, int clus1, int clus2, int lgth1, int lgth2, char **seq1, char **seq2, double *eff1, double *eff2, LocalHom ***localhom, int forscore )
259 int i, j, k1, k2, tmpint, start1, start2, end1, end2;
260 double effij, effijx;
261 char *pt, *pt1, *pt2;
264 if( impalloclen <= lgth1 + 2 || impalloclen <= lgth2 + 2 )
266 if( impmtx ) FreeFloatMtx( impmtx );
267 impalloclen = MAX( lgth1, lgth2 ) + 2;
268 impmtx = AllocateFloatMtx( impalloclen+100, impalloclen+100 );
273 fprintf( stderr, "eff1 in _init_strict = \n" );
274 for( i=0; i<clus1; i++ )
275 fprintf( stderr, "eff1[] = %f\n", eff1[i] );
276 for( i=0; i<clus2; i++ )
277 fprintf( stderr, "eff2[] = %f\n", eff2[i] );
280 for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
282 effijx = 1.0 * fastathreshold;
283 for( i=0; i<clus1; i++ )
285 for( j=0; j<clus2; j++ )
287 effij = eff1[i] * eff2[j] * effijx;
288 tmpptr = localhom[i][j];
291 // fprintf( stderr, "start1 = %d\n", tmpptr->start1 );
292 // fprintf( stderr, "end1 = %d\n", tmpptr->end1 );
293 // fprintf( stderr, "i = %d, seq1 = \n%s\n", i, seq1[i] );
294 // fprintf( stderr, "j = %d, seq2 = \n%s\n", j, seq2[j] );
299 if( *pt++ != '-' ) tmpint++;
300 if( tmpint == tmpptr->start1 ) break;
302 start1 = (int)( pt - seq1[i] ) - 1;
304 if( tmpptr->start1 == tmpptr->end1 ) end1 = start1;
310 if( tmpint == tmpptr->end1 ) break;
311 if( *pt++ != '-' ) tmpint++;
313 end1 = (int)( pt - seq1[i] ) - 1;
317 // fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, tmpptr->end1, pt-seq1[i] );
318 if( *pt++ != '-' ) tmpint++;
319 if( tmpint == tmpptr->end1 ) break;
321 end1 = (int)( pt - seq1[i] ) - 1;
329 if( *pt++ != '-' ) tmpint++;
330 if( tmpint == tmpptr->start2 ) break;
332 start2 = (int)( pt - seq2[j] ) - 1;
333 if( tmpptr->start2 == tmpptr->end2 ) end2 = start2;
339 if( tmpint == tmpptr->end2 ) break;
340 if( *pt++ != '-' ) tmpint++;
342 end2 = (int)( pt - seq2[j] ) - 1;
346 if( *pt++ != '-' ) tmpint++;
347 if( tmpint == tmpptr->end2 ) break;
349 end2 = (int)( pt - seq2[j] ) - 1;
352 // fprintf( stderr, "start1 = %d (%c), end1 = %d (%c), start2 = %d (%c), end2 = %d (%c)\n", start1, seq1[i][start1], end1, seq1[i][end1], start2, seq2[j][start2], end2, seq2[j][end2] );
353 // fprintf( stderr, "step 0\n" );
354 if( end1 - start1 != end2 - start2 )
356 // fprintf( stderr, "CHUUI!!, start1 = %d, end1 = %d, start2 = %d, end2 = %d\n", start1, end1, start2, end2 );
359 k1 = start1; k2 = start2;
362 while( *pt1 && *pt2 )
364 if( *pt1 != '-' && *pt2 != '-' )
366 // ½Å¤ß¤òÆó½Å¤Ë¤«¤±¤Ê¤¤¤è¤¦¤ËÃðդ·¤Æ²¼¤µ¤¤¡£
367 // impmtx[k1][k2] += tmpptr->wimportance * fastathreshold;
368 // impmtx[k1][k2] += tmpptr->importance * effij;
369 impmtx[k1][k2] += tmpptr->fimportance * effij;
370 // fprintf( stderr, "k1=%d, k2=%d, impalloclen=%d\n", k1, k2, impalloclen );
371 // fprintf( stderr, "mark, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
375 else if( *pt1 != '-' && *pt2 == '-' )
377 // fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
380 else if( *pt1 == '-' && *pt2 != '-' )
382 // fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
385 else if( *pt1 == '-' && *pt2 == '-' )
387 // fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
391 if( k1 > end1 || k2 > end2 ) break;
393 tmpptr = tmpptr->next;
398 fprintf( stderr, "impmtx = \n" );
399 for( k2=0; k2<lgth2; k2++ )
400 fprintf( stderr, "%6.3f ", (double)k2 );
401 fprintf( stderr, "\n" );
402 for( k1=0; k1<lgth1; k1++ )
404 fprintf( stderr, "%d", k1 );
405 for( k2=0; k2<lgth2; k2++ )
406 fprintf( stderr, "%2.1f ", impmtx[k1][k2] );
407 fprintf( stderr, "\n" );
415 static void match_calc( float *match, float **cpmx1, float **cpmx2, int i1, int lgth2, float **floatwork, int **intwork, int initialize )
420 float **cpmxpd = floatwork;
421 int **cpmxpdn = intwork;
422 float *matchpt, *cpmxpdpt, **cpmxpdptpt;
423 int *cpmxpdnpt, **cpmxpdnptpt;
427 for( j=0; j<lgth2; j++ )
430 for( l=0; l<26; l++ )
434 cpmxpd[j][count] = cpmx2[l][j];
435 cpmxpdn[j][count] = l;
439 cpmxpdn[j][count] = -1;
444 for( l=0; l<26; l++ )
447 for( j=0; j<26; j++ )
448 scarr[l] += n_dis[j][l] * cpmx1[j][i1];
451 cpmxpdnptpt = cpmxpdn;
456 cpmxpdnpt = *cpmxpdnptpt++;
457 cpmxpdpt = *cpmxpdptpt++;
458 while( *cpmxpdnpt>-1 )
459 *matchpt += scarr[*cpmxpdnpt++] * *cpmxpdpt++;
466 float **cpmxpd = floatwork;
467 int **cpmxpdn = intwork;
472 for( j=0; j<lgth2; j++ )
475 for( l=0; l<26; l++ )
479 cpmxpd[count][j] = cpmx2[l][j];
480 cpmxpdn[count][j] = l;
484 cpmxpdn[count][j] = -1;
487 for( l=0; l<26; l++ )
490 for( k=0; k<26; k++ )
491 scarr[l] += n_dis[k][l] * cpmx1[k][i1];
493 for( j=0; j<lgth2; j++ )
496 for( k=0; cpmxpdn[k][j]>-1; k++ )
497 match[j] += scarr[cpmxpdn[k][j]] * cpmxpd[k][j];
502 static void Atracking_localhom( float *impwmpt, float *lasthorizontalw, float *lastverticalw,
503 char **seq1, char **seq2,
504 char **mseq1, char **mseq2,
505 float **cpmx1, float **cpmx2,
506 short **ijp, int icyc, int jcyc,
507 int start1, int end1, int start2, int end2,
508 int *gapmap1, int *gapmap2 )
510 int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k;
512 char *gaptable1, *gt1bk;
513 char *gaptable2, *gt2bk;
514 lgth1 = strlen( seq1[0] );
515 lgth2 = strlen( seq2[0] );
516 gt1bk = AllocateCharVec( lgth1+lgth2+1 );
517 gt2bk = AllocateCharVec( lgth1+lgth2+1 );
520 for( i=0; i<lgth1; i++ )
522 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
530 wm = lastverticalw[0];
531 for( i=0; i<lgth1; i++ )
533 if( lastverticalw[i] >= wm )
535 wm = lastverticalw[i];
536 iin = i; jin = lgth2-1;
537 ijp[lgth1][lgth2] = +( lgth1 - i );
540 for( j=0; j<lgth2; j++ )
542 if( lasthorizontalw[j] >= wm )
544 wm = lasthorizontalw[j];
545 iin = lgth1-1; jin = j;
546 ijp[lgth1][lgth2] = -( lgth2 - j );
551 for( i=0; i<lgth1+1; i++ )
555 for( j=0; j<lgth2+1; j++ )
557 ijp[0][j] = -( j + 1 );
560 gaptable1 = gt1bk + lgth1+lgth2;
562 gaptable2 = gt2bk + lgth1+lgth2;
565 iin = lgth1; jin = lgth2;
567 for( k=0; k<=lgth1+lgth2; k++ )
569 if( ijp[iin][jin] < 0 )
571 ifi = iin-1; jfi = jin+ijp[iin][jin];
573 else if( ijp[iin][jin] > 0 )
575 ifi = iin-ijp[iin][jin]; jfi = jin-1;
579 ifi = iin-1; jfi = jin-1;
595 if( iin == lgth1 || jin == lgth2 )
599 *impwmpt += part_imp_match_out_scQ( gapmap1[iin]+start1, gapmap2[jin]+start2 );
601 // fprintf( stderr, "impwm = %f (iin=%d, jin=%d) seq1=%c, seq2=%c\n", *impwmpt, iin, jin, seq1[0][iin], seq2[0][jin] );
603 if( iin <= 0 || jin <= 0 ) break;
607 iin = ifi; jin = jfi;
610 for( i=0; i<icyc; i++ ) gapireru( mseq1[i], seq1[i], gaptable1 );
611 for( j=0; j<jcyc; j++ ) gapireru( mseq2[j], seq2[j], gaptable2 );
617 static float Atracking( float *lasthorizontalw, float *lastverticalw,
618 char **seq1, char **seq2,
619 char **mseq1, char **mseq2,
620 float **cpmx1, float **cpmx2,
621 short **ijp, int icyc, int jcyc )
623 int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k;
625 char *gaptable1, *gt1bk;
626 char *gaptable2, *gt2bk;
627 lgth1 = strlen( seq1[0] );
628 lgth2 = strlen( seq2[0] );
630 gt1bk = AllocateCharVec( lgth1+lgth2+1 );
631 gt2bk = AllocateCharVec( lgth1+lgth2+1 );
634 for( i=0; i<lgth1; i++ )
636 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
644 wm = lastverticalw[0];
645 for( i=0; i<lgth1; i++ )
647 if( lastverticalw[i] >= wm )
649 wm = lastverticalw[i];
650 iin = i; jin = lgth2-1;
651 ijp[lgth1][lgth2] = +( lgth1 - i );
654 for( j=0; j<lgth2; j++ )
656 if( lasthorizontalw[j] >= wm )
658 wm = lasthorizontalw[j];
659 iin = lgth1-1; jin = j;
660 ijp[lgth1][lgth2] = -( lgth2 - j );
665 for( i=0; i<lgth1+1; i++ )
669 for( j=0; j<lgth2+1; j++ )
671 ijp[0][j] = -( j + 1 );
674 gaptable1 = gt1bk + lgth1+lgth2;
676 gaptable2 = gt2bk + lgth1+lgth2;
679 iin = lgth1; jin = lgth2;
680 for( k=0; k<=lgth1+lgth2; k++ )
682 if( ijp[iin][jin] < 0 )
684 ifi = iin-1; jfi = jin+ijp[iin][jin];
686 else if( ijp[iin][jin] > 0 )
688 ifi = iin-ijp[iin][jin]; jfi = jin-1;
692 ifi = iin-1; jfi = jin-1;
708 if( iin <= 0 || jin <= 0 ) break;
712 iin = ifi; jin = jfi;
715 for( i=0; i<icyc; i++ ) gapireru( mseq1[i], seq1[i], gaptable1 );
716 for( j=0; j<jcyc; j++ ) gapireru( mseq2[j], seq2[j], gaptable2 );
724 float partQ__align( char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen, LocalHom ***localhom, float *impmatch, int start1, int end1, int start2, int end2, int *gapmap1, int *gapmap2, char *sgap1, char *sgap2, char *egap1, char *egap2 )
725 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
729 int lasti, lastj; /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
732 float wm = 0.0; /* int ?????? */
734 float *currentw, *previousw;
735 // float fpenalty = (float)penalty;
737 float fpenalty_ex = (float)penalty_ex;
742 float *mjpt, *prept, *curpt;
748 static float *w1, *w2;
750 static float *initverticalw; /* kufuu sureba iranai */
751 static float *lastverticalw; /* kufuu sureba iranai */
763 static float *ogcp1g;
764 static float *ogcp2g;
765 static float *fgcp1g;
766 static float *fgcp2g;
767 static float *og_h_dg_n1_p;
768 static float *og_h_dg_n2_p;
769 static float *fg_h_dg_n1_p;
770 static float *fg_h_dg_n2_p;
771 static float *og_t_fg_h_dg_n1_p;
772 static float *og_t_fg_h_dg_n2_p;
773 static float *fg_t_og_h_dg_n1_p;
774 static float *fg_t_og_h_dg_n2_p;
775 static float *gapz_n1;
776 static float *gapz_n2;
777 static float **cpmx1;
778 static float **cpmx2;
779 static int **intwork;
780 static float **floatwork;
781 static int orlgth1 = 0, orlgth2 = 0;
782 float fpenalty = (float)penalty;
784 float *fg_t_og_h_dg_n2_p_pt;
785 float *og_t_fg_h_dg_n2_p_pt;
786 float *og_h_dg_n2_p_pt;
787 float *fg_h_dg_n2_p_pt;
792 float fg_t_og_h_dg_n1_p_va;
793 float og_t_fg_h_dg_n1_p_va;
794 float og_h_dg_n1_p_va;
795 float fg_h_dg_n1_p_va;
805 fprintf( stderr, "#### seq1[0] = %s\n", seq1[0] );
806 fprintf( stderr, "#### seq2[0] = %s\n", seq2[0] );
810 mseq1 = AllocateCharMtx( njob, 0 );
811 mseq2 = AllocateCharMtx( njob, 0 );
815 lgth1 = strlen( seq1[0] );
816 lgth2 = strlen( seq2[0] );
818 if( lgth1 == 0 || lgth2 == 0 )
820 fprintf( stderr, "WARNING (Aalignmm): lgth1=%d, lgth2=%d\n", lgth1, lgth2 );
824 if( lgth1 > orlgth1 || lgth2 > orlgth2 )
828 if( orlgth1 > 0 && orlgth2 > 0 )
832 FreeFloatVec( match );
833 FreeFloatVec( initverticalw );
834 FreeFloatVec( lastverticalw );
841 FreeFloatVec( digf1 );
842 FreeFloatVec( digf2 );
843 FreeFloatVec( diaf1 );
844 FreeFloatVec( diaf2 );
845 FreeFloatVec( gapz1 );
846 FreeFloatVec( gapz2 );
847 FreeFloatVec( gapf1 );
848 FreeFloatVec( gapf2 );
849 FreeFloatVec( ogcp1g );
850 FreeFloatVec( ogcp2g );
851 FreeFloatVec( fgcp1g );
852 FreeFloatVec( fgcp2g );
853 FreeFloatVec( og_h_dg_n1_p );
854 FreeFloatVec( og_h_dg_n2_p );
855 FreeFloatVec( fg_h_dg_n1_p );
856 FreeFloatVec( fg_h_dg_n2_p );
857 FreeFloatVec( og_t_fg_h_dg_n1_p );
858 FreeFloatVec( og_t_fg_h_dg_n2_p );
859 FreeFloatVec( fg_t_og_h_dg_n1_p );
860 FreeFloatVec( fg_t_og_h_dg_n2_p );
861 FreeFloatVec( gapz_n1 );
862 FreeFloatVec( gapz_n2 );
864 FreeFloatMtx( cpmx1 );
865 FreeFloatMtx( cpmx2 );
867 FreeFloatMtx( floatwork );
868 FreeIntMtx( intwork );
871 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
872 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
875 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
878 w1 = AllocateFloatVec( ll2+2 );
879 w2 = AllocateFloatVec( ll2+2 );
880 match = AllocateFloatVec( ll2+2 );
882 initverticalw = AllocateFloatVec( ll1+2 );
883 lastverticalw = AllocateFloatVec( ll1+2 );
885 m = AllocateFloatVec( ll2+2 );
886 mp = AllocateIntVec( ll2+2 );
888 mseq = AllocateCharMtx( njob, ll1+ll2 );
890 digf1 = AllocateFloatVec( ll1+2 );
891 digf2 = AllocateFloatVec( ll2+2 );
892 diaf1 = AllocateFloatVec( ll1+2 );
893 diaf2 = AllocateFloatVec( ll2+2 );
894 gapz1 = AllocateFloatVec( ll1+2 );
895 gapz2 = AllocateFloatVec( ll2+2 );
896 gapf1 = AllocateFloatVec( ll1+2 );
897 gapf2 = AllocateFloatVec( ll2+2 );
898 ogcp1g = AllocateFloatVec( ll1+2 );
899 ogcp2g = AllocateFloatVec( ll2+2 );
900 fgcp1g = AllocateFloatVec( ll1+2 );
901 fgcp2g = AllocateFloatVec( ll2+2 );
902 og_h_dg_n1_p = AllocateFloatVec( ll1 + 2 );
903 og_h_dg_n2_p = AllocateFloatVec( ll2 + 2 );
904 fg_h_dg_n1_p = AllocateFloatVec( ll1 + 2 );
905 fg_h_dg_n2_p = AllocateFloatVec( ll2 + 2 );
906 og_t_fg_h_dg_n1_p = AllocateFloatVec( ll1 + 2 );
907 og_t_fg_h_dg_n2_p = AllocateFloatVec( ll2 + 2 );
908 fg_t_og_h_dg_n1_p = AllocateFloatVec( ll1 + 2 );
909 fg_t_og_h_dg_n2_p = AllocateFloatVec( ll2 + 2 );
910 gapz_n1 = AllocateFloatVec( ll1+2 );
911 gapz_n2 = AllocateFloatVec( ll2+2 );
913 cpmx1 = AllocateFloatMtx( 26, ll1+2 );
914 cpmx2 = AllocateFloatMtx( 26, ll2+2 );
917 floatwork = AllocateFloatMtx( MAX( ll1, ll2 )+2, 26 );
918 intwork = AllocateIntMtx( MAX( ll1, ll2 )+2, 27 );
920 floatwork = AllocateFloatMtx( 26, MAX( ll1, ll2 )+2 );
921 intwork = AllocateIntMtx( 26, MAX( ll1, ll2 )+2 );
925 fprintf( stderr, "succeeded\n" );
933 for( i=0; i<icyc; i++ )
938 for( j=0; j<jcyc; j++ )
940 mseq2[j] = mseq[icyc+j];
945 if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
949 if( commonAlloc1 && commonAlloc2 )
951 FreeShortMtx( commonIP );
954 ll1 = MAX( orlgth1, commonAlloc1 );
955 ll2 = MAX( orlgth2, commonAlloc2 );
958 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
961 commonIP = AllocateShortMtx( ll1+10, ll2+10 );
964 fprintf( stderr, "succeeded\n\n" );
975 for( i=0; i<icyc; i++ )
977 fprintf( stderr, "## totaleff = %f\n", t );
981 cpmx_calc_new( seq1, cpmx1, eff1, lgth1, icyc );
982 cpmx_calc_new( seq2, cpmx2, eff2, lgth2, jcyc );
986 new_OpeningGapCount_zure( ogcp1g, icyc, seq1, eff1, lgth1, sgap1, egap1 );
987 new_OpeningGapCount_zure( ogcp2g, jcyc, seq2, eff2, lgth2, sgap2, egap2 );
988 new_FinalGapCount_zure( fgcp1g, icyc, seq1, eff1, lgth1, sgap1, egap1 );
989 new_FinalGapCount_zure( fgcp2g, jcyc, seq2, eff2, lgth2, sgap2, egap2 );
990 getdigapfreq_part( digf1, icyc, seq1, eff1, lgth1, sgap1, egap1 );
991 getdigapfreq_part( digf2, jcyc, seq2, eff2, lgth2, sgap2, egap2 );
992 getdiaminofreq_part( diaf1, icyc, seq1, eff1, lgth1, sgap1, egap1 );
993 getdiaminofreq_part( diaf2, jcyc, seq2, eff2, lgth2, sgap2, egap2 );
994 getgapfreq( gapf1, icyc, seq1, eff1, lgth1 );
995 getgapfreq( gapf2, jcyc, seq2, eff2, lgth2 );
996 getgapfreq_zure_part( gapz1, icyc, seq1, eff1, lgth1, sgap1 );
997 getgapfreq_zure_part( gapz2, jcyc, seq2, eff2, lgth2, sgap1 );
1001 st_OpeningGapCount( ogcp1g, icyc, seq1, eff1, lgth1 );
1002 st_OpeningGapCount( ogcp2g, jcyc, seq2, eff2, lgth2 );
1003 st_FinalGapCount_zure( fgcp1g, icyc, seq1, eff1, lgth1 );
1004 st_FinalGapCount_zure( fgcp2g, jcyc, seq2, eff2, lgth2 );
1005 getdigapfreq_st( digf1, icyc, seq1, eff1, lgth1 );
1006 getdigapfreq_st( digf2, jcyc, seq2, eff2, lgth2 );
1007 getdiaminofreq_x( diaf1, icyc, seq1, eff1, lgth1 );
1008 getdiaminofreq_x( diaf2, jcyc, seq2, eff2, lgth2 );
1009 getgapfreq( gapf1, icyc, seq1, eff1, lgth1 );
1010 getgapfreq( gapf2, jcyc, seq2, eff2, lgth2 );
1011 getgapfreq_zure( gapz1, icyc, seq1, eff1, lgth1 );
1012 getgapfreq_zure( gapz2, jcyc, seq2, eff2, lgth2 );
1017 for( i=0; i<lastj; i++ )
1019 og_h_dg_n2_p[i] = ( 1.0-ogcp2g[i]-digf2[i] ) * fpenalty * 0.5;
1020 fg_h_dg_n2_p[i] = ( 1.0-fgcp2g[i]-digf2[i] ) * fpenalty * 0.5;
1021 og_t_fg_h_dg_n2_p[i] = (1.0-ogcp2g[i]+fgcp2g[i]-digf2[i]) * 0.5 * fpenalty;
1022 fg_t_og_h_dg_n2_p[i] = (1.0-fgcp2g[i]+ogcp2g[i]-digf2[i]) * 0.5 * fpenalty;
1023 gapz_n2[i] = (1.0-gapz2[i]);
1026 for( i=0; i<lastj; i++ )
1028 og_h_dg_n1_p[i] = ( 1.0-ogcp1g[i]-digf1[i] ) * fpenalty * 0.5;
1029 fg_h_dg_n1_p[i] = ( 1.0-fgcp1g[i]-digf1[i] ) * fpenalty * 0.5;
1030 og_t_fg_h_dg_n1_p[i] = (1.0-ogcp1g[i]+fgcp1g[i]-digf1[i]) * 0.5 * fpenalty;
1031 fg_t_og_h_dg_n1_p[i] = (1.0-fgcp1g[i]+ogcp1g[i]-digf1[i]) * 0.5 * fpenalty;
1032 gapz_n1[i] = (1.0-gapz1[i]);
1038 for( i=0; i<lgth1; i++ )
1039 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
1045 match_calc( initverticalw, cpmx2, cpmx1, 0, lgth1, floatwork, intwork, 1 );
1047 part_imp_match_out_vead_tateQ_gapmap( initverticalw, gapmap2[0]+start2, lgth1, start1, gapmap1 );
1049 match_calc( currentw, cpmx1, cpmx2, 0, lgth2, floatwork, intwork, 1 );
1051 part_imp_match_out_veadQ_gapmap( currentw, gapmap1[0], lgth2, start2, gapmap2 );
1054 #if 0 // -> tbfast.c
1056 imp_match_calc( currentw, icyc, jcyc, lgth1, lgth2, seq1, seq2, eff1, eff2, localhom, 1, 0 );
1067 g += ogcp1g[0] * og_h_dg_n2_p[0];
1068 // g += ogcp1g[0] * ( 1.0-ogcp2g[0]-digf2[0] ) * fpenalty * 0.5;
1069 // if( g ) fprintf( stderr, "init-match penal1=%f, %c-%c\n", g, seq1[0][0], seq2[0][0] );
1071 g += ogcp2g[0] * og_h_dg_n1_p[0];
1072 // g += ogcp2g[0] * ( 1.0-ogcp1g[0]-digf1[0] ) * fpenalty * 0.5;
1073 // if( g ) fprintf( stderr, "init-match penal2=%f, %c-%c\n", g, seq1[0][0], seq2[0][0] );
1075 g += fgcp1g[0] * fg_h_dg_n2_p[0];
1076 // g += fgcp1g[0] * ( 1.0-fgcp2g[0]-digf2[0] ) * fpenalty * 0.5;
1077 // if( g ) fprintf( stderr, "match penal1=%f, %c-%c\n", g, seq1[0][i], seq2[0][j] );
1079 g += fgcp2g[0] * fg_h_dg_n1_p[0];
1080 // g += fgcp2g[0] * ( 1.0-fgcp1g[0]-digf1[0] ) * fpenalty * 0.5;
1081 // if( g ) fprintf( stderr, "match penal2=%f, %c-%c\n", g, seq1[0][i], seq2[0][j] );
1084 initverticalw[0] += g;
1087 for( i=1; i<lgth1+1; i++ )
1089 tmppenal = gapz_n2[0]*og_t_fg_h_dg_n1_p[0];
1090 // tmppenal = ( (1.0-gapz2[0])*(1.0-ogcp1g[0]+fgcp1g[0]-digf1[0]) ) * 0.5 * fpenalty; // mada
1091 initverticalw[i] += tmppenal;
1093 tmppenal = gapz_n2[1]*fg_t_og_h_dg_n1_p[i];
1094 // tmppenal = ( (1.0-gapz2[1])*(1.0-fgcp1g[i]+ogcp1g[i]-digf1[i]) ) * 0.5 * fpenalty; // mada
1095 initverticalw[i] += tmppenal;
1098 for( j=1; j<lgth2+1; j++ )
1100 tmppenal = gapz_n1[0]*og_t_fg_h_dg_n2_p[0];
1101 // tmppenal = ( (1.0-gapz1[0])*(1.0-ogcp2g[0]+fgcp2g[0]-digf2[0]) ) * 0.5 * fpenalty; // mada
1102 currentw[j] += tmppenal;
1104 tmppenal = gapz_n1[1]*fg_t_og_h_dg_n2_p[j];
1105 // tmppenal = ( (1.0-gapz1[1])*(1.0-fgcp2g[j]+ogcp2g[j]-digf2[j]) ) * 0.5 * fpenalty; // mada
1106 currentw[j] += tmppenal;
1112 for( j=1; j<lgth2+1; j++ )
1113 currentw[j] -= offset * j / 2.0;
1114 for( i=1; i<lgth1+1; i++ )
1115 initverticalw[i] -= offset * i / 2.0;
1119 m[0] = 0.0; // iranai
1120 for( j=1; j<lgth2+1; ++j )
1123 m[j] = currentw[j-1] + 10000 * fpenalty; //iinoka?
1126 lastverticalw[0] = 0.0; // Falign kara yobaretatoki kounarukanousei ari
1128 lastverticalw[0] = currentw[lgth2-1];
1130 if( outgap ) lasti = lgth1+1; else lasti = lgth1;
1133 fprintf( stderr, "currentw = \n" );
1134 for( i=0; i<lgth1+1; i++ )
1136 fprintf( stderr, "%5.2f ", currentw[i] );
1138 fprintf( stderr, "\n" );
1139 fprintf( stderr, "initverticalw = \n" );
1140 for( i=0; i<lgth2+1; i++ )
1142 fprintf( stderr, "%5.2f ", initverticalw[i] );
1144 fprintf( stderr, "\n" );
1145 fprintf( stderr, "fcgp\n" );
1146 for( i=0; i<lgth1; i++ )
1147 fprintf( stderr, "fgcp1[%d]=%f\n", i, ogcp1[i] );
1148 for( i=0; i<lgth2; i++ )
1149 fprintf( stderr, "fgcp2[%d]=%f\n", i, ogcp2[i] );
1152 for( i=1; i<lasti; i++ )
1155 previousw = currentw;
1158 previousw[0] = initverticalw[i-1];
1160 match_calc( currentw, cpmx1, cpmx2, i, lgth2, floatwork, intwork, 0 );
1162 fprintf( stderr, "\n" );
1163 fprintf( stderr, "i=%d\n", i );
1164 fprintf( stderr, "currentw = \n" );
1165 for( j=0; j<lgth2; j++ )
1167 fprintf( stderr, "%5.2f ", currentw[j] );
1169 fprintf( stderr, "\n" );
1173 // fprintf( stderr, "Calling imp_match_calc (o) lgth = %d, i = %d\n", lgth1, i );
1175 imp_match_out_veadQ( currentw, i, lgth2 );
1177 part_imp_match_out_veadQ_gapmap( currentw, gapmap1[i]+start1, lgth2, start2, gapmap2 );
1181 fprintf( stderr, "\n" );
1182 fprintf( stderr, "i=%d\n", i );
1183 fprintf( stderr, "currentw = \n" );
1184 for( j=0; j<lgth2; j++ )
1186 fprintf( stderr, "%5.2f ", currentw[j] );
1188 fprintf( stderr, "\n" );
1190 currentw[0] = initverticalw[i];
1193 mi = previousw[0] + 10000 * fpenalty;
1198 curpt = currentw + 1;
1200 fg_t_og_h_dg_n2_p_pt = fg_t_og_h_dg_n2_p + 1;
1201 og_t_fg_h_dg_n2_p_pt = og_t_fg_h_dg_n2_p + 1;
1202 og_h_dg_n2_p_pt = og_h_dg_n2_p + 1;
1203 fg_h_dg_n2_p_pt = fg_h_dg_n2_p + 1;
1204 gapz_n2_pt0 = gapz_n2 + 1;
1205 gapz_n2_pt1 = gapz_n2 + 2;
1206 fgcp2pt = fgcp2g + 1;
1207 ogcp2pt = ogcp2g + 1;
1209 fg_t_og_h_dg_n1_p_va = fg_t_og_h_dg_n1_p[i];
1210 og_t_fg_h_dg_n1_p_va = og_t_fg_h_dg_n1_p[i];
1211 og_h_dg_n1_p_va = og_h_dg_n1_p[i];
1212 fg_h_dg_n1_p_va = fg_h_dg_n1_p[i];
1213 gapz_n1_va0 = gapz_n1[i];
1214 gapz_n1_va1 = gapz_n1[i+1];
1215 fgcp1va = fgcp1g[i];
1216 ogcp1va = ogcp1g[i];
1219 for( j=1; j<lastj; j++ )
1223 g = ogcp1va * *og_h_dg_n2_p_pt;
1224 // g = ogcp1g[i] * og_h_dg_n2_p[j];
1225 // g = ogcp1g[i] * ( 1.0-ogcp2g[j]-digf2[j] ) * fpenalty * 0.5;
1226 // if( g && i==j ) fprintf( stderr, "match penal1=%f, %c-%c\n", g, seq1[0][i], seq2[0][j] );
1229 g = *ogcp2pt * og_h_dg_n1_p_va;
1230 // g = ogcp2g[j] * og_h_dg_n1_p[i];
1231 // g = ogcp2g[j] * ( 1.0-ogcp1g[i]-digf1[i] ) * fpenalty * 0.5;
1232 // if( g && i==j ) fprintf( stderr, "match penal2=%f, %c-%c\n", g, seq1[0][i], seq2[0][j] );
1235 g = fgcp1va * *fg_h_dg_n2_p_pt;
1236 // g = fgcp1g[i] * fg_h_dg_n2_p[j];
1237 // g = fgcp1g[i] * ( 1.0-fgcp2g[j]-digf2[j] ) * fpenalty * 0.5;
1238 // if( g && i==j ) fprintf( stderr, "match penal3=%f, %c-%c\n", g, seq1[0][i], seq2[0][j] );
1241 g = *fgcp2pt * fg_h_dg_n1_p_va;
1242 // g = fgcp2g[j] * fg_h_dg_n1_p[i];
1243 // g = fgcp2g[j] * ( 1.0-fgcp1g[i]-digf1[i] ) * fpenalty * 0.5;
1244 // if( g && i==j ) fprintf( stderr, "match penal4=%f, %c-%c\n", g, seq1[0][i], seq2[0][j] );
1250 fprintf( stderr, "%5.0f->", wm );
1253 fprintf( stderr, "%5.0f?", g );
1255 tmppenal = gapz_n1_va1 * *fg_t_og_h_dg_n2_p_pt;
1256 // tmppenal = gapz_n1[i+1] * fg_t_og_h_dg_n2_p[j];
1257 // tmppenal = ( (1.0-gapz1[i+1])*(1.0-fgcp2g[j]+ogcp2g[j]-digf2[j]) ) * 0.5 * fpenalty; // mada
1258 if( (g=mi+tmppenal) > wm )
1260 // fprintf( stderr, "jump i start=%f (j=%d, fgcp2g[j]=%f, digf2[j]=%f, diaf2[j]=%f), %c-%c\n", g-mi, j, fgcp2g[j], digf2[j], diaf2[j], seq1[0][i], seq2[0][j] );
1262 *ijppt = -( j - mpi );
1264 tmppenal = gapz_n1_va0 * *og_t_fg_h_dg_n2_p_pt;
1265 // tmppenal = gapz_n1[i] * og_t_fg_h_dg_n2_p[j];
1266 // tmppenal = ( (1.0-gapz1[i])*(1.0-ogcp2g[j]+fgcp2g[j]-digf2[j]) ) * 0.5 * fpenalty; // mada
1267 if( (g=*prept+tmppenal) >= mi )
1269 // fprintf( stderr, "jump i end=%f, %c-%c\n", g-*prept, seq1[0][i-1], seq2[0][j-1] );
1275 fprintf( stderr, "%5.0f?", g );
1277 tmppenal = *gapz_n2_pt1 * fg_t_og_h_dg_n1_p_va;
1278 // tmppenal = gapz_n2[j+1] * fg_t_og_h_dg_n1_p[i];
1279 // tmppenal = ( (1.0-gapz2[j+1])*(1.0-fgcp1g[i]+ogcp1g[i]-digf1[i]) ) * 0.5 * fpenalty; // mada
1280 if( (g=*mjpt+tmppenal) > wm )
1283 *ijppt = +( i - *mpjpt );
1285 tmppenal = *gapz_n2_pt0 * og_t_fg_h_dg_n1_p_va;
1286 // tmppenal = gapz_n2[j] * og_t_fg_h_dg_n1_p[i];
1287 // tmppenal = ( (1.0-gapz2[j])*(1.0-ogcp1g[i]+fgcp1g[i]-digf1[i]) ) * 0.5 * fpenalty; // mada
1288 if( (g=*prept+tmppenal) >= *mjpt )
1294 fprintf( stderr, "%5.0f ", wm );
1301 fg_t_og_h_dg_n2_p_pt++;
1302 og_t_fg_h_dg_n2_p_pt++;
1310 lastverticalw[i] = currentw[lgth2-1];
1313 // fprintf( stderr, "wm = %f\n", wm );
1318 for( j=1; j<lgth2+1; j++ )
1319 currentw[j] -= offset * ( lgth2 - j ) / 2.0;
1320 for( i=1; i<lgth1+1; i++ )
1321 lastverticalw[i] -= offset * ( lgth1 - i / 2.0);
1326 fprintf( stderr, "\n" );
1327 for( i=0; i<icyc; i++ ) fprintf( stderr,"%s\n", seq1[i] );
1328 fprintf( stderr, "#####\n" );
1329 for( j=0; j<jcyc; j++ ) fprintf( stderr,"%s\n", seq2[j] );
1330 fprintf( stderr, "====>" );
1331 for( i=0; i<icyc; i++ ) strcpy( mseq1[i], seq1[i] );
1332 for( j=0; j<jcyc; j++ ) strcpy( mseq2[j], seq2[j] );
1336 Atracking_localhom( impmatch, currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc, start1, end1, start2, end2, gapmap1, gapmap2 );
1339 Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
1341 // fprintf( stderr, "### impmatch = %f\n", *impmatch );
1343 resultlen = strlen( mseq1[0] );
1344 if( alloclen < resultlen || resultlen > N )
1346 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
1347 ErrorExit( "LENGTH OVER!\n" );
1351 for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
1352 for( j=0; j<jcyc; j++ ) strcpy( seq2[j], mseq2[j] );
1354 fprintf( stderr, "\n" );
1355 for( i=0; i<icyc; i++ ) fprintf( stderr, "%s\n", mseq1[i] );
1356 fprintf( stderr, "#####\n" );
1357 for( j=0; j<jcyc; j++ ) fprintf( stderr, "%s\n", mseq2[j] );
1360 // fprintf( stderr, "wm = %f\n", wm );