7 #define USE_PENALTY_EX 0
9 static void OpeningGapCount( double *ogcp, int clus, char **seq, double *eff, int len )
15 for( i=0; i<len; i++ ) ogcp[i] = 0.0;
16 for( j=0; j<clus; j++ )
18 fprintf( stderr, "seq[%d] = %s\n", j, seq[j] );
20 for( i=0; i<len; i++ )
23 gc = ( seq[j][i] == '-' );
25 fprintf( stderr, "gb,gc=%d,%d ", gb,gc );
26 fprintf( stderr, "i=%d, %30.20f", i, ogcp[i] );
27 if( !gb * gc ) ogcp[i] += (float)eff[j];
28 fprintf( stderr, "-> %30.20f\n", ogcp[i] );
33 for( i=0; i<len; i++ )
35 fprintf( stderr, "ogcp[%d] = %30.20f\n", i, ogcp[i] );
40 static void FinalGapCount( double *fgcp, int clus, char **seq, double *eff, int len )
45 for( i=0; i<len+1; i++ ) fgcp[i] = 0.0;
46 for( j=0; j<clus; j++ )
48 gc = ( seq[j][0] == '-' );
49 for( i=1; i<len+1; i++ )
52 gc = ( seq[j][i] == '-' );
54 if( gb * !gc ) fgcp[i-1] += (float)eff[j];
58 fprintf( stderr, "eff[%d] = %30.20f\n", j, eff[j] );
60 for( i=0; i<len; i++ )
62 // fgcp[i] /= totaleff;
63 fprintf( stderr, "fgcp[%d] = %30.20f\n", i, fgcp[i] );
66 static float **impmtx = NULL;
67 float imp_match_out_sc( int i1, int j1 )
69 // fprintf( stderr, "imp+match = %f\n", impmtx[i1][j1] * fastathreshold );
70 // fprintf( stderr, "val = %f\n", impmtx[i1][j1] );
71 return( impmtx[i1][j1] );
73 static void imp_match_out_vead_gapmap( float *imp, int i1, int lgth2, int *gapmap2 )
76 float *pt = impmtx[i1];
77 for( j=0; j<lgth2; j++ )
78 *imp++ += pt[gapmap2[j]];
80 static void imp_match_out_vead( float *imp, int i1, int lgth2 )
83 float *pt = impmtx[i1];
84 for( j=0; j<lgth2; j++ )
88 void imp_match_init_strict( float *imp, int clus1, int clus2, int lgth1, int lgth2, char **seq1, char **seq2, double *eff1, double *eff2, LocalHom ***localhom, int forscore )
90 int dif, i, j, k1, k2, tmpint, start1, start2, end1, end2;
91 static int impalloclen = 0;
95 static char *nocount1 = NULL;
96 static char *nocount2 = NULL;
99 if( impalloclen < lgth1 + 2 || impalloclen < lgth2 + 2 )
101 if( impmtx ) FreeFloatMtx( impmtx );
102 if( nocount1 ) free( nocount1 );
103 if( nocount2 ) free( nocount2 );
104 impalloclen = MAX( lgth1, lgth2 ) + 2;
105 impmtx = AllocateFloatMtx( impalloclen, impalloclen );
106 nocount1 = AllocateCharVec( impalloclen );
107 nocount2 = AllocateCharVec( impalloclen );
110 for( i=0; i<lgth1; i++ )
112 for( j=0; j<clus1; j++ )
113 if( seq1[j][i] == '-' ) break;
114 if( j != clus1 ) nocount1[i] = 1;
115 else nocount1[i] = 0;
117 for( i=0; i<lgth2; i++ )
119 for( j=0; j<clus2; j++ )
120 if( seq2[j][i] == '-' ) break;
121 if( j != clus2 ) nocount2[i] = 1;
122 else nocount2[i] = 0;
126 fprintf( stderr, "nocount2 =\n" );
127 for( i = 0; i<impalloclen; i++ )
129 fprintf( stderr, "nocount2[%d] = %d (%c)\n", i, nocount2[i], seq2[0][i] );
136 fprintf( stderr, "eff1 in _init_strict = \n" );
137 for( i=0; i<clus1; i++ )
138 fprintf( stderr, "eff1[] = %f\n", eff1[i] );
139 for( i=0; i<clus2; i++ )
140 fprintf( stderr, "eff2[] = %f\n", eff2[i] );
143 for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
145 effijx = fastathreshold;
146 for( i=0; i<clus1; i++ )
148 for( j=0; j<clus2; j++ )
150 effij = eff1[i] * eff2[j] * effijx;
151 tmpptr = localhom[i][j];
154 // fprintf( stderr, "start1 = %d\n", tmpptr->start1 );
155 // fprintf( stderr, "end1 = %d\n", tmpptr->end1 );
156 // fprintf( stderr, "i = %d, seq1 = \n%s\n", i, seq1[i] );
157 // fprintf( stderr, "j = %d, seq2 = \n%s\n", j, seq2[j] );
162 if( *pt++ != '-' ) tmpint++;
163 if( tmpint == tmpptr->start1 ) break;
165 start1 = pt - seq1[i] - 1;
169 // fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, tmpptr->end1, pt-seq1[i] );
170 if( *pt++ != '-' ) tmpint++;
171 if( tmpint == tmpptr->end1 ) break;
173 end1 = pt - seq1[i] - 1;
179 if( *pt++ != '-' ) tmpint++;
180 if( tmpint == tmpptr->start2 ) break;
182 start2 = pt - seq2[j] - 1;
185 if( *pt++ != '-' ) tmpint++;
186 if( tmpint == tmpptr->end2 ) break;
188 end2 = pt - seq2[j] - 1;
189 // 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] );
190 // fprintf( stderr, "step 0\n" );
191 if( end1 - start1 != end2 - start2 )
193 // fprintf( stderr, "CHUUI!!, start1 = %d, end1 = %d, start2 = %d, end2 = %d\n", start1, end1, start2, end2 );
197 k1 = start1; k2 = start2;
200 while( *pt1 && *pt2 )
202 if( *pt1 != '-' && *pt2 != '-' )
204 // ½Å¤ß¤òÆó½Å¤Ë¤«¤±¤Ê¤¤¤è¤¦¤ËÃí°Õ¤·¤Æ²¼¤µ¤¤¡£
205 // impmtx[k1][k2] += tmpptr->wimportance * fastathreshold;
206 impmtx[k1][k2] += tmpptr->importance * effij;
207 // fprintf( stderr, "mark, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
211 else if( *pt1 != '-' && *pt2 == '-' )
213 // fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
216 else if( *pt1 == '-' && *pt2 != '-' )
218 // fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
221 else if( *pt1 == '-' && *pt2 == '-' )
223 // fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
227 if( k1 > end1 || k2 > end2 ) break;
230 while( k1 <= end1 && k2 <= end2 )
232 fprintf( stderr, "k1,k2=%d,%d - ", k1, k2 );
233 if( !nocount1[k1] && !nocount2[k2] )
235 impmtx[k1][k2] += tmpptr->wimportance * eff1[i] * eff2[j] * fastathreshold;
236 fprintf( stderr, "marked\n" );
239 fprintf( stderr, "no count\n" );
243 tmpptr = tmpptr->next;
248 fprintf( stderr, "impmtx = \n" );
249 for( k2=0; k2<lgth2; k2++ )
250 fprintf( stderr, "%6.3f ", (double)k2 );
251 fprintf( stderr, "\n" );
252 for( k1=0; k1<lgth1; k1++ )
254 fprintf( stderr, "%d", k1 );
255 for( k2=0; k2<lgth2; k2++ )
256 fprintf( stderr, "%2.1f ", impmtx[k1][k2] );
257 fprintf( stderr, "\n" );
262 void imp_match_init( float *imp, int clus1, int clus2, int lgth1, int lgth2, char **seq1, char **seq2, double *eff1, double *eff2, LocalHom ***localhom )
264 int dif, i, j, k1, k2, tmpint, start1, start2, end1, end2;
265 static int impalloclen = 0;
268 static char *nocount1 = NULL;
269 static char *nocount2 = NULL;
271 if( impalloclen < lgth1 + 2 || impalloclen < lgth2 + 2 )
273 if( impmtx ) FreeFloatMtx( impmtx );
274 if( nocount1 ) free( nocount1 );
275 if( nocount2 ) free( nocount2 );
276 impalloclen = MAX( lgth1, lgth2 ) + 2;
277 impmtx = AllocateFloatMtx( impalloclen, impalloclen );
278 nocount1 = AllocateCharVec( impalloclen );
279 nocount2 = AllocateCharVec( impalloclen );
282 for( i=0; i<lgth1; i++ )
284 for( j=0; j<clus1; j++ )
285 if( seq1[j][i] == '-' ) break;
286 if( j != clus1 ) nocount1[i] = 1;
287 else nocount1[i] = 0;
289 for( i=0; i<lgth2; i++ )
291 for( j=0; j<clus2; j++ )
292 if( seq2[j][i] == '-' ) break;
293 if( j != clus2 ) nocount2[i] = 1;
294 else nocount2[i] = 0;
298 fprintf( stderr, "nocount2 =\n" );
299 for( i = 0; i<impalloclen; i++ )
301 fprintf( stderr, "nocount2[%d] = %d (%c)\n", i, nocount2[i], seq2[0][i] );
305 for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
307 for( i=0; i<clus1; i++ )
309 fprintf( stderr, "i = %d, seq1 = %s\n", i, seq1[i] );
310 for( j=0; j<clus2; j++ )
312 fprintf( stderr, "start1 = %d\n", localhom[i][j]->start1 );
313 fprintf( stderr, "end1 = %d\n", localhom[i][j]->end1 );
314 fprintf( stderr, "j = %d, seq2 = %s\n", j, seq2[j] );
319 if( *pt++ != '-' ) tmpint++;
320 if( tmpint == localhom[i][j]->start1 ) break;
322 start1 = pt - seq1[i] - 1;
326 // fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, localhom[i][j].end1, pt-seq1[i] );
327 if( *pt++ != '-' ) tmpint++;
328 if( tmpint == localhom[i][j]->end1 ) break;
330 end1 = pt - seq1[i] - 1;
336 if( *pt++ != '-' ) tmpint++;
337 if( tmpint == localhom[i][j]->start2 ) break;
339 start2 = pt - seq2[j] - 1;
342 if( *pt++ != '-' ) tmpint++;
343 if( tmpint == localhom[i][j]->end2 ) break;
345 end2 = pt - seq2[j] - 1;
346 // fprintf( stderr, "start1 = %d, end1 = %d, start2 = %d, end2 = %d\n", start1, end1, start2, end2 );
349 fprintf( stderr, "step 0\n" );
350 while( k1 <= end1 && k2 <= end2 )
353 if( !nocount1[k1] && !nocount2[k2] )
354 impmtx[k1][k2] += localhom[i][j].wimportance * eff1[i] * eff2[j];
357 if( !nocount1[k1] && !nocount2[k2] )
358 impmtx[k1][k2] += localhom[i][j]->wimportance * eff1[i] * eff2[j];
363 dif = ( end1 - start1 ) - ( end2 - start2 );
364 fprintf( stderr, "dif = %d\n", dif );
369 fprintf( stderr, "dif = %d\n", dif );
372 while( k1 <= end1 && k2 <= end2 )
374 if( 0 <= k2 && start2 <= k2 && !nocount1[k1] && !nocount2[k2] )
375 impmtx[k1][k2] = localhom[i][j]->wimportance * eff1[i] * eff2[j];
389 if( k1 >= 0 && k1 >= start1 && !nocount1[k1] && !nocount2[k2] )
390 impmtx[k1][k2] = localhom[i][j]->wimportance * eff1[i] * eff2[j];
399 fprintf( stderr, "impmtx = \n" );
400 for( k2=0; k2<lgth2; k2++ )
401 fprintf( stderr, "%6.3f ", (double)k2 );
402 fprintf( stderr, "\n" );
403 for( k1=0; k1<lgth1; k1++ )
405 fprintf( stderr, "%d", k1 );
406 for( k2=0; k2<lgth2; k2++ )
407 fprintf( stderr, "%6.3f ", impmtx[k1][k2] );
408 fprintf( stderr, "\n" );
413 static void match_calc( float *match, float **cpmx1, float **cpmx2, int i1, int lgth2, float **floatwork, int **intwork, int initialize )
417 float **cpmxpd = floatwork;
418 int **cpmxpdn = intwork;
423 for( j=0; j<lgth2; j++ )
426 for( l=0; l<26; l++ )
430 cpmxpd[count][j] = cpmx2[l][j];
431 cpmxpdn[count][j] = l;
435 cpmxpdn[count][j] = -1;
439 for( l=0; l<26; l++ )
442 for( k=0; k<26; k++ )
443 scarr[l] += n_dis[k][l] * cpmx1[k][i1];
445 #if 0 /* ¤³¤ì¤ò»È¤¦¤È¤
\ad¤Ïfloatwork¤Î¥¢¥í¥±¡¼¥È¤òµÕ¤Ë¤¹¤ë */
447 float *fpt, **fptpt, *fpt2;
455 ipt=*iptpt,fpt=*fptpt;
457 *fpt2 += scarr[*ipt++] * *fpt++;
458 fpt2++,iptpt++,fptpt++;
462 for( j=0; j<lgth2; j++ )
465 for( k=0; cpmxpdn[k][j]>-1; k++ )
466 match[j] += scarr[cpmxpdn[k][j]] * cpmxpd[k][j];
471 static void Atracking_localhom( float *impwmpt, float *lasthorizontalw, float *lastverticalw,
472 char **seq1, char **seq2,
473 char **mseq1, char **mseq2,
474 float **cpmx1, float **cpmx2,
475 short **ijp, int icyc, int jcyc )
477 int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k;
480 lgth1 = strlen( seq1[0] );
481 lgth2 = strlen( seq2[0] );
484 for( i=0; i<lgth1; i++ )
486 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
494 wm = lastverticalw[0];
495 for( i=0; i<lgth1; i++ )
497 if( lastverticalw[i] >= wm )
499 wm = lastverticalw[i];
500 iin = i; jin = lgth2-1;
501 ijp[lgth1][lgth2] = +( lgth1 - i );
504 for( j=0; j<lgth2; j++ )
506 if( lasthorizontalw[j] >= wm )
508 wm = lasthorizontalw[j];
509 iin = lgth1-1; jin = j;
510 ijp[lgth1][lgth2] = -( lgth2 - j );
515 for( i=0; i<lgth1+1; i++ )
519 for( j=0; j<lgth2+1; j++ )
521 ijp[0][j] = -( j + 1 );
524 for( i=0; i<icyc; i++ )
526 mseq1[i] += lgth1+lgth2;
529 for( j=0; j<jcyc; j++ )
531 mseq2[j] += lgth1+lgth2;
534 iin = lgth1; jin = lgth2;
536 for( k=0; k<=lgth1+lgth2; k++ )
538 if( ijp[iin][jin] < 0 )
540 ifi = iin-1; jfi = jin+ijp[iin][jin];
542 else if( ijp[iin][jin] > 0 )
544 ifi = iin-ijp[iin][jin]; jfi = jin-1;
548 ifi = iin-1; jfi = jin-1;
553 for( i=0; i<icyc; i++ )
554 *--mseq1[i] = seq1[i][ifi+l];
555 for( j=0; j<jcyc; j++ )
562 for( i=0; i<icyc; i++ )
564 for( j=0; j<jcyc; j++ )
565 *--mseq2[j] = seq2[j][jfi+l];
568 if( iin == lgth1 || jin == lgth2 )
572 *impwmpt += imp_match_out_sc( iin, jin );
574 // fprintf( stderr, "impwm = %f (iin=%d, jin=%d) seq1=%c, seq2=%c\n", *impwmpt, iin, jin, seq1[0][iin], seq2[0][jin] );
576 if( iin <= 0 || jin <= 0 ) break;
577 for( i=0; i<icyc; i++ )
578 *--mseq1[i] = seq1[i][ifi];
579 for( j=0; j<jcyc; j++ )
580 *--mseq2[j] = seq2[j][jfi];
582 iin = ifi; jin = jfi;
585 static void Atracking_localhom_gapmap( float *impwmpt, float *lasthorizontalw, float *lastverticalw,
586 char **seq1, char **seq2,
587 char **mseq1, char **mseq2,
588 float **cpmx1, float **cpmx2,
589 short **ijp, int icyc, int jcyc,
590 int *gapmap1, int *gapmap2 )
592 int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k;
595 lgth1 = strlen( seq1[0] );
596 lgth2 = strlen( seq2[0] );
599 for( i=0; i<lgth1; i++ )
601 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
609 wm = lastverticalw[0];
610 for( i=0; i<lgth1; i++ )
612 if( lastverticalw[i] >= wm )
614 wm = lastverticalw[i];
615 iin = i; jin = lgth2-1;
616 ijp[lgth1][lgth2] = +( lgth1 - i );
619 for( j=0; j<lgth2; j++ )
621 if( lasthorizontalw[j] >= wm )
623 wm = lasthorizontalw[j];
624 iin = lgth1-1; jin = j;
625 ijp[lgth1][lgth2] = -( lgth2 - j );
630 for( i=0; i<lgth1+1; i++ )
634 for( j=0; j<lgth2+1; j++ )
636 ijp[0][j] = -( j + 1 );
639 for( i=0; i<icyc; i++ )
641 mseq1[i] += lgth1+lgth2;
644 for( j=0; j<jcyc; j++ )
646 mseq2[j] += lgth1+lgth2;
649 iin = lgth1; jin = lgth2;
651 for( k=0; k<=lgth1+lgth2; k++ )
653 if( ijp[iin][jin] < 0 )
655 ifi = iin-1; jfi = jin+ijp[iin][jin];
657 else if( ijp[iin][jin] > 0 )
659 ifi = iin-ijp[iin][jin]; jfi = jin-1;
663 ifi = iin-1; jfi = jin-1;
668 for( i=0; i<icyc; i++ )
669 *--mseq1[i] = seq1[i][ifi+l];
670 for( j=0; j<jcyc; j++ )
677 for( i=0; i<icyc; i++ )
679 for( j=0; j<jcyc; j++ )
680 *--mseq2[j] = seq2[j][jfi+l];
683 if( iin == lgth1 || jin == lgth2 )
687 *impwmpt += imp_match_out_sc( gapmap1[iin], gapmap2[jin] );
689 // fprintf( stderr, "impwm = %f (iin=%d, jin=%d) seq1=%c, seq2=%c\n", *impwmpt, iin, jin, seq1[0][iin], seq2[0][jin] );
691 if( iin <= 0 || jin <= 0 ) break;
692 for( i=0; i<icyc; i++ )
693 *--mseq1[i] = seq1[i][ifi];
694 for( j=0; j<jcyc; j++ )
695 *--mseq2[j] = seq2[j][jfi];
697 iin = ifi; jin = jfi;
700 static float Atracking( float *lasthorizontalw, float *lastverticalw,
701 char **seq1, char **seq2,
702 char **mseq1, char **mseq2,
703 float **cpmx1, float **cpmx2,
704 short **ijp, int icyc, int jcyc )
706 int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k;
709 lgth1 = strlen( seq1[0] );
710 lgth2 = strlen( seq2[0] );
713 for( i=0; i<lgth1; i++ )
715 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
723 wm = lastverticalw[0];
724 for( i=0; i<lgth1; i++ )
726 if( lastverticalw[i] >= wm )
728 wm = lastverticalw[i];
729 iin = i; jin = lgth2-1;
730 ijp[lgth1][lgth2] = +( lgth1 - i );
733 for( j=0; j<lgth2; j++ )
735 if( lasthorizontalw[j] >= wm )
737 wm = lasthorizontalw[j];
738 iin = lgth1-1; jin = j;
739 ijp[lgth1][lgth2] = -( lgth2 - j );
744 for( i=0; i<lgth1+1; i++ )
748 for( j=0; j<lgth2+1; j++ )
750 ijp[0][j] = -( j + 1 );
753 for( i=0; i<icyc; i++ )
755 mseq1[i] += lgth1+lgth2;
758 for( j=0; j<jcyc; j++ )
760 mseq2[j] += lgth1+lgth2;
763 iin = lgth1; jin = lgth2;
764 for( k=0; k<=lgth1+lgth2; k++ )
766 if( ijp[iin][jin] < 0 )
768 ifi = iin-1; jfi = jin+ijp[iin][jin];
770 else if( ijp[iin][jin] > 0 )
772 ifi = iin-ijp[iin][jin]; jfi = jin-1;
776 ifi = iin-1; jfi = jin-1;
781 for( i=0; i<icyc; i++ )
782 *--mseq1[i] = seq1[i][ifi+l];
783 for( j=0; j<jcyc; j++ )
790 for( i=0; i<icyc; i++ )
792 for( j=0; j<jcyc; j++ )
793 *--mseq2[j] = seq2[j][jfi+l];
796 if( iin <= 0 || jin <= 0 ) break;
797 for( i=0; i<icyc; i++ )
798 *--mseq1[i] = seq1[i][ifi];
799 for( j=0; j<jcyc; j++ )
800 *--mseq2[j] = seq2[j][jfi];
802 iin = ifi; jin = jfi;
807 float A__align( char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen, LocalHom ***localhom, float *impmatch )
808 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
812 int lasti; /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
815 float wm; /* int ?????? */
817 float *currentw, *previousw;
818 float fpenalty = (float)penalty;
819 float fpenalty_ex = (float)penalty_ex;
823 float *mjpt, *prept, *curpt;
829 static float *w1, *w2;
831 static float *initverticalw; /* kufuu sureba iranai */
832 static float *lastverticalw; /* kufuu sureba iranai */
836 static double *ogcp1;
837 static double *ogcp2;
838 static double *fgcp1;
839 static double *fgcp2;
840 static float **cpmx1;
841 static float **cpmx2;
842 static int **intwork;
843 static float **floatwork;
844 static int orlgth1 = 0, orlgth2 = 0;
848 fprintf( stderr, "eff in SA+++align\n" );
849 for( i=0; i<icyc; i++ ) fprintf( stderr, "eff1[%d] = %f\n", i, eff1[i] );
853 mseq1 = AllocateCharMtx( njob, 0 );
854 mseq2 = AllocateCharMtx( njob, 0 );
858 lgth1 = strlen( seq1[0] );
859 lgth2 = strlen( seq2[0] );
861 if( lgth1 > orlgth1 || lgth2 > orlgth2 )
865 if( orlgth1 > 0 && orlgth2 > 0 )
869 FreeFloatVec( match );
870 FreeFloatVec( initverticalw );
871 FreeFloatVec( lastverticalw );
878 FreeDoubleVec( ogcp1 );
879 FreeDoubleVec( ogcp2 );
880 FreeDoubleVec( fgcp1 );
881 FreeDoubleVec( fgcp2 );
884 FreeFloatMtx( cpmx1 );
885 FreeFloatMtx( cpmx2 );
887 FreeFloatMtx( floatwork );
888 FreeIntMtx( intwork );
891 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
892 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
895 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
898 w1 = AllocateFloatVec( ll2+2 );
899 w2 = AllocateFloatVec( ll2+2 );
900 match = AllocateFloatVec( ll2+2 );
902 initverticalw = AllocateFloatVec( ll1+2 );
903 lastverticalw = AllocateFloatVec( ll1+2 );
905 m = AllocateFloatVec( ll2+2 );
906 mp = AllocateIntVec( ll2+2 );
908 mseq = AllocateCharMtx( njob, ll1+ll2 );
910 ogcp1 = AllocateDoubleVec( ll1+2 );
911 ogcp2 = AllocateDoubleVec( ll2+2 );
912 fgcp1 = AllocateDoubleVec( ll1+2 );
913 fgcp2 = AllocateDoubleVec( ll2+2 );
915 cpmx1 = AllocateFloatMtx( 26, ll1+2 );
916 cpmx2 = AllocateFloatMtx( 26, ll2+2 );
918 floatwork = AllocateFloatMtx( 26, MAX( ll1, ll2 )+2 );
919 intwork = AllocateIntMtx( 26, MAX( ll1, ll2 )+2 );
922 fprintf( stderr, "succeeded\n" );
930 for( i=0; i<icyc; i++ )
935 for( j=0; j<jcyc; j++ )
937 mseq2[j] = mseq[icyc+j];
942 if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
946 if( commonAlloc1 && commonAlloc2 )
948 FreeShortMtx( commonIP );
951 ll1 = MAX( orlgth1, commonAlloc1 );
952 ll2 = MAX( orlgth2, commonAlloc2 );
955 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
958 commonIP = AllocateShortMtx( ll1+10, ll2+10 );
961 fprintf( stderr, "succeeded\n\n" );
972 for( i=0; i<icyc; i++ )
974 fprintf( stderr, "## totaleff = %f\n", t );
977 cpmx_calc_new( seq1, cpmx1, eff1, lgth1, icyc );
978 cpmx_calc_new( seq2, cpmx2, eff2, lgth2, jcyc );
980 OpeningGapCount( ogcp1, icyc, seq1, eff1, lgth1 );
981 OpeningGapCount( ogcp2, jcyc, seq2, eff2, lgth2 );
982 FinalGapCount( fgcp1, icyc, seq1, eff1, lgth1 );
983 FinalGapCount( fgcp2, jcyc, seq2, eff2, lgth2 );
985 for( i=0; i<lgth1; i++ )
987 ogcp1[i] = 0.5 * ( 1.0 - ogcp1[i] );
988 fgcp1[i] = 0.5 * ( 1.0 - fgcp1[i] );
990 for( i=0; i<lgth2; i++ )
992 ogcp2[i] = 0.5 * ( 1.0 - ogcp2[i] );
993 fgcp2[i] = 0.5 * ( 1.0 - fgcp2[i] );
996 for( i=0; i<lgth1; i++ )
997 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
1003 match_calc( initverticalw, cpmx2, cpmx1, 0, lgth1, floatwork, intwork, 1 );
1005 match_calc( currentw, cpmx1, cpmx2, 0, lgth2, floatwork, intwork, 1 );
1006 #if 0 // -> tbfast.c
1008 imp_match_calc( currentw, icyc, jcyc, lgth1, lgth2, seq1, seq2, eff1, eff2, localhom, 1, 0 );
1014 for( i=1; i<lgth1+1; i++ )
1016 initverticalw[i] += fpenalty * ( ogcp1[0] + fgcp1[i-1] ) ;
1018 for( j=1; j<lgth2+1; j++ )
1020 currentw[j] += fpenalty * ( ogcp2[0] + fgcp2[j-1] ) ;
1026 for( j=1; j<lgth2+1; j++ )
1027 currentw[j] -= offset * j / 2.0;
1028 for( i=1; i<lgth1+1; i++ )
1029 initverticalw[i] -= offset * i / 2.0;
1033 for( j=1; j<lgth2+1; ++j )
1035 m[j] = currentw[j-1] + fpenalty * ogcp1[1]; mp[j] = 0;
1038 lastverticalw[0] = currentw[lgth2-1];
1040 if( outgap ) lasti = lgth1+1; else lasti = lgth1;
1043 fprintf( stderr, "currentw = \n" );
1044 for( i=0; i<lgth1+1; i++ )
1046 fprintf( stderr, "%5.2f ", currentw[i] );
1048 fprintf( stderr, "\n" );
1049 fprintf( stderr, "initverticalw = \n" );
1050 for( i=0; i<lgth2+1; i++ )
1052 fprintf( stderr, "%5.2f ", initverticalw[i] );
1054 fprintf( stderr, "\n" );
1055 fprintf( stderr, "fcgp\n" );
1056 for( i=0; i<lgth1; i++ )
1057 fprintf( stderr, "fgcp1[%d]=%f\n", i, ogcp1[i] );
1058 for( i=0; i<lgth2; i++ )
1059 fprintf( stderr, "fgcp2[%d]=%f\n", i, ogcp2[i] );
1062 for( i=1; i<lasti; i++ )
1065 previousw = currentw;
1068 previousw[0] = initverticalw[i-1];
1070 match_calc( currentw, cpmx1, cpmx2, i, lgth2, floatwork, intwork, 0 );
1072 fprintf( stderr, "\n" );
1073 fprintf( stderr, "i=%d\n", i );
1074 fprintf( stderr, "currentw = \n" );
1075 for( j=0; j<lgth2; j++ )
1077 fprintf( stderr, "%5.2f ", currentw[j] );
1079 fprintf( stderr, "\n" );
1083 // fprintf( stderr, "Calling imp_match_calc (o) lgth = %d, i = %d\n", lgth1, i );
1085 imp_match_out_vead( currentw, i, lgth2 );
1087 imp_match_out_vead( currentw, i, lgth2 );
1091 fprintf( stderr, "\n" );
1092 fprintf( stderr, "i=%d\n", i );
1093 fprintf( stderr, "currentw = \n" );
1094 for( j=0; j<lgth2; j++ )
1096 fprintf( stderr, "%5.2f ", currentw[j] );
1098 fprintf( stderr, "\n" );
1100 currentw[0] = initverticalw[i];
1104 mi = previousw[0] + fpenalty * ogcp2[1]; mpi = 0;
1109 curpt = currentw + 1;
1111 for( j=1; j<lgth2+1; j++ )
1117 fprintf( stderr, "%5.0f->", wm );
1119 g = mi + fpenalty * fgcp2[j-1];
1121 fprintf( stderr, "%5.0f?", g );
1126 *ijppt = -( j - mpi );
1128 g = *prept + fpenalty * ogcp2[j];
1138 g = *mjpt + fpenalty * fgcp1[i-1];
1140 fprintf( stderr, "%5.0f?", g );
1145 *ijppt = +( i - *mpjpt );
1147 g = *prept + fpenalty * ogcp1[i];
1154 m[j] += fpenalty_ex;
1158 fprintf( stderr, "%5.0f ", wm );
1167 lastverticalw[i] = currentw[lgth2-1];
1169 floatncpy( previousw, currentw, lgth2+1 );
1170 previousw[0] = initverticalw[i-1];
1173 match_calc( currentw, cpmx1, cpmx2, i, lgth2, floatwork, intwork, 0 );
1174 currentw[0] = initverticalw[i];
1176 mi = previousw[0] + fpenalty * ogcp2[1]; mpi = 0;
1177 for( j=1; j<lgth2+1; j++ )
1179 wm = previousw[j-1];
1182 g = fpenalty * fgcp2[j-1];
1186 ijp[i][j] = -( j - mpi );
1188 g = fpenalty * ogcp2[j];
1189 if( mi <= previousw[j-1] + g )
1191 mi = previousw[j-1] + g;
1195 g = fpenalty * fgcp1[i-1];
1199 ijp[i][j] = +( i - mp[j] );
1201 g = fpenalty * ogcp1[i];
1202 if( m[j] <= previousw[j-1] + g )
1204 m[j] = previousw[j-1] + g ;
1209 lastverticalw[i] = currentw[lgth2-1];
1214 fprintf( stderr, "wm = %f\n", wm );
1219 for( j=1; j<lgth2+1; j++ )
1220 currentw[j] -= offset * ( lgth2 - j ) / 2.0;
1221 for( i=1; i<lgth1+1; i++ )
1222 lastverticalw[i] -= offset * ( lgth1 - i / 2.0);
1227 fprintf( stderr, "\n" );
1228 for( i=0; i<icyc; i++ ) fprintf( stderr,"%s\n", seq1[i] );
1229 fprintf( stderr, "#####\n" );
1230 for( j=0; j<jcyc; j++ ) fprintf( stderr,"%s\n", seq2[j] );
1231 fprintf( stderr, "====>" );
1232 for( i=0; i<icyc; i++ ) strcpy( mseq1[i], seq1[i] );
1233 for( j=0; j<jcyc; j++ ) strcpy( mseq2[j], seq2[j] );
1237 Atracking_localhom( impmatch, currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
1240 Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
1242 // fprintf( stderr, "### impmatch = %f\n", *impmatch );
1244 resultlen = strlen( mseq1[0] );
1245 if( alloclen < resultlen || resultlen > N )
1247 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
1248 ErrorExit( "LENGTH OVER!\n" );
1252 for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
1253 for( j=0; j<jcyc; j++ ) strcpy( seq2[j], mseq2[j] );
1255 fprintf( stderr, "\n" );
1256 for( i=0; i<icyc; i++ ) fprintf( stderr, "%s\n", mseq1[i] );
1257 fprintf( stderr, "#####\n" );
1258 for( j=0; j<jcyc; j++ ) fprintf( stderr, "%s\n", mseq2[j] );
1265 float A__align_gapmap( char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen, LocalHom ***localhom, float *impmatch, int *gapmap1, int *gapmap2 )
1266 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
1270 int lasti, lastj; /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
1273 float wm; /* int ?????? */
1275 float *currentw, *previousw;
1276 float fpenalty = (float)penalty;
1277 float fpenalty_ex = (float)penalty_ex;
1281 float *mjpt, *prept, *curpt;
1284 static float mi, *m;
1286 static int mpi, *mp;
1287 static float *w1, *w2;
1288 static float *match;
1289 static float *initverticalw; /* kufuu sureba iranai */
1290 static float *lastverticalw; /* kufuu sureba iranai */
1291 static char **mseq1;
1292 static char **mseq2;
1294 static double *ogcp1;
1295 static double *ogcp2;
1296 static double *fgcp1;
1297 static double *fgcp2;
1298 static float **cpmx1;
1299 static float **cpmx2;
1300 static int **intwork;
1301 static float **floatwork;
1302 static int orlgth1 = 0, orlgth2 = 0;
1306 fprintf( stderr, "eff in SA+++align\n" );
1307 for( i=0; i<icyc; i++ ) fprintf( stderr, "eff1[%d] = %f\n", i, eff1[i] );
1311 mseq1 = AllocateCharMtx( njob, 0 );
1312 mseq2 = AllocateCharMtx( njob, 0 );
1316 lgth1 = strlen( seq1[0] );
1317 lgth2 = strlen( seq2[0] );
1319 if( lgth1 > orlgth1 || lgth2 > orlgth2 )
1323 if( orlgth1 > 0 && orlgth2 > 0 )
1327 FreeFloatVec( match );
1328 FreeFloatVec( initverticalw );
1329 FreeFloatVec( lastverticalw );
1334 FreeCharMtx( mseq );
1336 FreeDoubleVec( ogcp1 );
1337 FreeDoubleVec( ogcp2 );
1338 FreeDoubleVec( fgcp1 );
1339 FreeDoubleVec( fgcp2 );
1342 FreeFloatMtx( cpmx1 );
1343 FreeFloatMtx( cpmx2 );
1345 FreeFloatMtx( floatwork );
1346 FreeIntMtx( intwork );
1349 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
1350 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
1353 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
1356 w1 = AllocateFloatVec( ll2+2 );
1357 w2 = AllocateFloatVec( ll2+2 );
1358 match = AllocateFloatVec( ll2+2 );
1360 initverticalw = AllocateFloatVec( ll1+2 );
1361 lastverticalw = AllocateFloatVec( ll1+2 );
1363 m = AllocateFloatVec( ll2+2 );
1364 mp = AllocateIntVec( ll2+2 );
1366 mseq = AllocateCharMtx( njob, ll1+ll2 );
1368 ogcp1 = AllocateDoubleVec( ll1+2 );
1369 ogcp2 = AllocateDoubleVec( ll2+2 );
1370 fgcp1 = AllocateDoubleVec( ll1+2 );
1371 fgcp2 = AllocateDoubleVec( ll2+2 );
1373 cpmx1 = AllocateFloatMtx( 26, ll1+2 );
1374 cpmx2 = AllocateFloatMtx( 26, ll2+2 );
1376 floatwork = AllocateFloatMtx( 26, MAX( ll1, ll2 )+2 );
1377 intwork = AllocateIntMtx( 26, MAX( ll1, ll2 )+2 );
1380 fprintf( stderr, "succeeded\n" );
1383 orlgth1 = ll1 - 100;
1384 orlgth2 = ll2 - 100;
1388 for( i=0; i<icyc; i++ )
1393 for( j=0; j<jcyc; j++ )
1395 mseq2[j] = mseq[icyc+j];
1400 if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
1404 if( commonAlloc1 && commonAlloc2 )
1406 FreeShortMtx( commonIP );
1409 ll1 = MAX( orlgth1, commonAlloc1 );
1410 ll2 = MAX( orlgth2, commonAlloc2 );
1413 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
1416 commonIP = AllocateShortMtx( ll1+10, ll2+10 );
1419 fprintf( stderr, "succeeded\n\n" );
1427 cpmx_calc_new( seq1, cpmx1, eff1, strlen( seq1[0] ), icyc );
1428 cpmx_calc_new( seq2, cpmx2, eff2, strlen( seq2[0] ), jcyc );
1430 OpeningGapCount( ogcp1, icyc, seq1, eff1, lgth1 );
1431 OpeningGapCount( ogcp2, jcyc, seq2, eff2, lgth2 );
1432 FinalGapCount( fgcp1, icyc, seq1, eff1, lgth1 );
1433 FinalGapCount( fgcp2, jcyc, seq2, eff2, lgth2 );
1435 for( i=0; i<lgth1; i++ )
1437 ogcp1[i] = 0.5 * ( 1.0 - ogcp1[i] );
1438 fgcp1[i] = 0.5 * ( 1.0 - fgcp1[i] );
1440 for( i=0; i<lgth2; i++ )
1442 ogcp2[i] = 0.5 * ( 1.0 - ogcp2[i] );
1443 fgcp2[i] = 0.5 * ( 1.0 - fgcp2[i] );
1446 for( i=0; i<lgth1; i++ )
1447 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
1454 match_calc( initverticalw, cpmx2, cpmx1, 0, lgth1, floatwork, intwork, 1 );
1457 match_calc( currentw, cpmx1, cpmx2, 0, lgth2, floatwork, intwork, 1 );
1458 #if 0 // -> tbfast.c
1460 imp_match_calc( currentw, icyc, jcyc, lgth1, lgth2, seq1, seq2, eff1, eff2, localhom, 1, 0 );
1466 for( i=1; i<lgth1+1; i++ )
1468 initverticalw[i] += fpenalty * ( ogcp1[0] + fgcp1[i-1] ) ;
1470 for( j=1; j<lgth2+1; j++ )
1472 currentw[j] += fpenalty * ( ogcp2[0] + fgcp2[j-1] ) ;
1478 for( j=1; j<lgth2+1; j++ )
1479 currentw[j] -= offset * j / 2.0;
1480 for( i=1; i<lgth1+1; i++ )
1481 initverticalw[i] -= offset * i / 2.0;
1485 for( j=1; j<lgth2+1; ++j )
1487 m[j] = currentw[j-1] + fpenalty * ogcp1[1]; mp[j] = 0;
1490 lastverticalw[0] = currentw[lgth2-1];
1492 if( outgap ) lasti = lgth1+1; else lasti = lgth1;
1495 fprintf( stderr, "currentw = \n" );
1496 for( i=0; i<lgth1+1; i++ )
1498 fprintf( stderr, "%5.2f ", currentw[i] );
1500 fprintf( stderr, "\n" );
1501 fprintf( stderr, "initverticalw = \n" );
1502 for( i=0; i<lgth2+1; i++ )
1504 fprintf( stderr, "%5.2f ", initverticalw[i] );
1506 fprintf( stderr, "\n" );
1507 fprintf( stderr, "fcgp\n" );
1508 for( i=0; i<lgth1; i++ )
1509 fprintf( stderr, "fgcp1[%d]=%f\n", i, ogcp1[i] );
1510 for( i=0; i<lgth2; i++ )
1511 fprintf( stderr, "fgcp2[%d]=%f\n", i, ogcp2[i] );
1514 for( i=1; i<lasti; i++ )
1517 previousw = currentw;
1520 previousw[0] = initverticalw[i-1];
1522 match_calc( currentw, cpmx1, cpmx2, i, lgth2, floatwork, intwork, 0 );
1524 fprintf( stderr, "\n" );
1525 fprintf( stderr, "i=%d\n", i );
1526 fprintf( stderr, "currentw = \n" );
1527 for( j=0; j<lgth2; j++ )
1529 fprintf( stderr, "%5.2f ", currentw[j] );
1531 fprintf( stderr, "\n" );
1535 // fprintf( stderr, "Calling imp_match_calc (o) lgth = %d, i = %d\n", lgth1, i );
1537 imp_match_out_vead( currentw, i, lgth2 );
1539 imp_match_out_vead_gapmap( currentw, gapmap1[i], lgth2, gapmap2 );
1543 fprintf( stderr, "\n" );
1544 fprintf( stderr, "i=%d\n", i );
1545 fprintf( stderr, "currentw = \n" );
1546 for( j=0; j<lgth2; j++ )
1548 fprintf( stderr, "%5.2f ", currentw[j] );
1550 fprintf( stderr, "\n" );
1552 currentw[0] = initverticalw[i];
1556 mi = previousw[0] + fpenalty * ogcp2[1]; mpi = 0;
1561 curpt = currentw + 1;
1563 for( j=1; j<lgth2+1; j++ )
1569 fprintf( stderr, "%5.0f->", wm );
1571 g = mi + fpenalty * fgcp2[j-1];
1573 fprintf( stderr, "%5.0f?", g );
1578 *ijppt = -( j - mpi );
1580 g = *prept + fpenalty * ogcp2[j];
1590 g = *mjpt + fpenalty * fgcp1[i-1];
1592 fprintf( stderr, "%5.0f?", g );
1597 *ijppt = +( i - *mpjpt );
1599 g = *prept + fpenalty * ogcp1[i];
1606 m[j] += fpenalty_ex;
1610 fprintf( stderr, "%5.0f ", wm );
1619 lastverticalw[i] = currentw[lgth2-1];
1621 floatncpy( previousw, currentw, lgth2+1 );
1622 previousw[0] = initverticalw[i-1];
1625 match_calc( currentw, cpmx1, cpmx2, i, lgth2, floatwork, intwork, 0 );
1626 currentw[0] = initverticalw[i];
1628 mi = previousw[0] + fpenalty * ogcp2[1]; mpi = 0;
1630 for( j=1; j<lastj; j++ )
1632 wm = previousw[j-1];
1635 g = fpenalty * fgcp2[j-1];
1639 ijp[i][j] = -( j - mpi );
1641 g = fpenalty * ogcp2[j];
1642 if( mi <= previousw[j-1] + g )
1644 mi = previousw[j-1] + g;
1648 g = fpenalty * fgcp1[i-1];
1652 ijp[i][j] = +( i - mp[j] );
1654 g = fpenalty * ogcp1[i];
1655 if( m[j] <= previousw[j-1] + g )
1657 m[j] = previousw[j-1] + g ;
1662 lastverticalw[i] = currentw[lgth2-1];
1670 for( j=1; j<lgth2+1; j++ )
1671 currentw[j] -= offset * ( lgth2 - j ) / 2.0;
1672 for( i=1; i<lgth1+1; i++ )
1673 lastverticalw[i] -= offset * ( lgth1 - i / 2.0);
1678 fprintf( stderr, "\n" );
1679 for( i=0; i<icyc; i++ ) fprintf( stderr,"%s\n", seq1[i] );
1680 fprintf( stderr, "#####\n" );
1681 for( j=0; j<jcyc; j++ ) fprintf( stderr,"%s\n", seq2[j] );
1682 fprintf( stderr, "====>" );
1683 for( i=0; i<icyc; i++ ) strcpy( mseq1[i], seq1[i] );
1684 for( j=0; j<jcyc; j++ ) strcpy( mseq2[j], seq2[j] );
1688 Atracking_localhom_gapmap( impmatch, currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc, gapmap1, gapmap2 );
1691 Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
1693 // fprintf( stderr, "### impmatch = %f\n", *impmatch );
1695 resultlen = strlen( mseq1[0] );
1696 if( alloclen < resultlen || resultlen > N )
1698 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
1699 ErrorExit( "LENGTH OVER!\n" );
1703 for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
1704 for( j=0; j<jcyc; j++ ) strcpy( seq2[j], mseq2[j] );
1706 fprintf( stderr, "\n" );
1707 for( i=0; i<icyc; i++ ) fprintf( stderr, "%s\n", mseq1[i] );
1708 fprintf( stderr, "#####\n" );
1709 for( j=0; j<jcyc; j++ ) fprintf( stderr, "%s\n", mseq2[j] );
1716 float translate_and_Calign( char **mseq1, char **mseq2, double *effarr1, double *effarr2, int clus1, int clus2, int alloclen )
1726 if ( clus1 == 1 && clus2 != 1 )
1728 seq = mseq1[0]; aseq = mseq2; effarr = effarr2; nseq = clus2+1;
1730 printf( "effarr in transl... = \n" );
1731 for( i=0; i<clus2; i++ ) printf( "%f ", effarr2[i] );
1734 else if( clus1 != 1 && clus2 == 1 )
1736 seq = mseq2[0]; aseq = mseq1; effarr = effarr1; nseq = clus1+1;
1738 else ErrorExit( "ERROR in translate_and_Calign" );
1740 result = Calignm1( &wm, aseq, seq, effarr, nseq-2, 0 );
1742 resultlen = strlen( result[0] );
1743 if( alloclen < resultlen || resultlen > N )
1745 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
1746 ErrorExit( "LENGTH OVER!\n" );
1748 for( i=0; i<nseq-1; i++ ) strcpy( aseq[i], result[i] );
1749 strcpy( seq, result[nseq-1] );