7 #define USE_PENALTY_EX 0
9 static void OpeningGapCount( float *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++ )
20 for( i=0; i<len; i++ )
23 gc = ( seq[j][i] == '-' );
25 if( !gb * gc ) ogcp[i] += feff;
30 // for( i=0; i<len; i++ )
32 // fprintf( stderr, "ogcp[%d] = %30.20f\n", i, ogcp[i] );
33 // ogcp[i] /= totaleff;
37 static void FinalGapCount( float *fgcp, int clus, char **seq, double *eff, int len )
42 for( i=0; i<len+1; i++ ) fgcp[i] = 0.0;
43 for( j=0; j<clus; j++ )
46 gc = ( seq[j][0] == '-' );
47 for( i=1; i<len+1; i++ )
50 gc = ( seq[j][i] == '-' );
52 if( gb * !gc ) fgcp[i-1] += feff;
55 // totaleff += eff[j];
56 // fprintf( stderr, "eff[%d] = %30.20f\n", j, eff[j] );
58 // for( i=0; i<len; i++ )
60 // fgcp[i] /= totaleff;
61 // fprintf( stderr, "fgcp[%d] = %30.20f\n", i, fgcp[i] );
64 static float **impmtx = NULL;
65 float imp_match_out_sc( int i1, int j1 )
67 // fprintf( stderr, "imp+match = %f\n", impmtx[i1][j1] * fastathreshold );
68 // fprintf( stderr, "val = %f\n", impmtx[i1][j1] );
69 return( impmtx[i1][j1] );
71 static void imp_match_out_vead_gapmap( float *imp, int i1, int lgth2, int *gapmap2 )
74 float *pt = impmtx[i1];
75 for( j=0; j<lgth2; j++ )
76 *imp++ += pt[gapmap2[j]];
78 static void imp_match_out_vead( float *imp, int i1, int lgth2 )
81 float *pt = impmtx[i1];
82 for( j=0; j<lgth2; j++ )
86 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 )
88 int dif, i, j, k1, k2, tmpint, start1, start2, end1, end2;
89 static int impalloclen = 0;
94 static char *nocount1 = NULL;
95 static char *nocount2 = NULL;
98 if( impalloclen < lgth1 + 2 || impalloclen < lgth2 + 2 )
100 if( impmtx ) FreeFloatMtx( impmtx );
101 if( nocount1 ) free( nocount1 );
102 if( nocount2 ) free( nocount2 );
103 impalloclen = MAX( lgth1, lgth2 ) + 2;
104 impmtx = AllocateFloatMtx( impalloclen, impalloclen );
105 nocount1 = AllocateCharVec( impalloclen );
106 nocount2 = AllocateCharVec( impalloclen );
109 for( i=0; i<lgth1; i++ )
111 for( j=0; j<clus1; j++ )
112 if( seq1[j][i] == '-' ) break;
113 if( j != clus1 ) nocount1[i] = 1;
114 else nocount1[i] = 0;
116 for( i=0; i<lgth2; i++ )
118 for( j=0; j<clus2; j++ )
119 if( seq2[j][i] == '-' ) break;
120 if( j != clus2 ) nocount2[i] = 1;
121 else nocount2[i] = 0;
125 fprintf( stderr, "nocount2 =\n" );
126 for( i = 0; i<impalloclen; i++ )
128 fprintf( stderr, "nocount2[%d] = %d (%c)\n", i, nocount2[i], seq2[0][i] );
135 fprintf( stderr, "eff1 in _init_strict = \n" );
136 for( i=0; i<clus1; i++ )
137 fprintf( stderr, "eff1[] = %f\n", eff1[i] );
138 for( i=0; i<clus2; i++ )
139 fprintf( stderr, "eff2[] = %f\n", eff2[i] );
142 for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
144 effijx = fastathreshold;
145 for( i=0; i<clus1; i++ )
147 for( j=0; j<clus2; j++ )
149 effij = (float)( eff1[i] * eff2[j] * effijx );
150 tmpptr = localhom[i][j];
153 // fprintf( stderr, "start1 = %d\n", tmpptr->start1 );
154 // fprintf( stderr, "end1 = %d\n", tmpptr->end1 );
155 // fprintf( stderr, "i = %d, seq1 = \n%s\n", i, seq1[i] );
156 // fprintf( stderr, "j = %d, seq2 = \n%s\n", j, seq2[j] );
161 if( *pt++ != '-' ) tmpint++;
162 if( tmpint == tmpptr->start1 ) break;
164 start1 = pt - seq1[i] - 1;
168 // fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, tmpptr->end1, pt-seq1[i] );
169 if( *pt++ != '-' ) tmpint++;
170 if( tmpint == tmpptr->end1 ) break;
172 end1 = pt - seq1[i] - 1;
178 if( *pt++ != '-' ) tmpint++;
179 if( tmpint == tmpptr->start2 ) break;
181 start2 = pt - seq2[j] - 1;
184 if( *pt++ != '-' ) tmpint++;
185 if( tmpint == tmpptr->end2 ) break;
187 end2 = pt - seq2[j] - 1;
188 // 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] );
189 // fprintf( stderr, "step 0\n" );
190 if( end1 - start1 != end2 - start2 )
192 // fprintf( stderr, "CHUUI!!, start1 = %d, end1 = %d, start2 = %d, end2 = %d\n", start1, end1, start2, end2 );
196 k1 = start1; k2 = start2;
199 while( *pt1 && *pt2 )
201 if( *pt1 != '-' && *pt2 != '-' )
203 // ½Å¤ß¤òÆó½Å¤Ë¤«¤±¤Ê¤¤¤è¤¦¤ËÃí°Õ¤·¤Æ²¼¤µ¤¤¡£
204 // impmtx[k1][k2] += tmpptr->wimportance * fastathreshold;
205 // impmtx[k1][k2] += tmpptr->importance * effij;
206 impmtx[k1][k2] += tmpptr->fimportance * 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 */
840 static float **cpmx1;
841 static float **cpmx2;
842 static int **intwork;
843 static float **floatwork;
844 static int orlgth1 = 0, orlgth2 = 0;
845 float fpenalty = (float)penalty;
849 fprintf( stderr, "eff in SA+++align\n" );
850 for( i=0; i<icyc; i++ ) fprintf( stderr, "eff1[%d] = %f\n", i, eff1[i] );
854 mseq1 = AllocateCharMtx( njob, 0 );
855 mseq2 = AllocateCharMtx( njob, 0 );
859 lgth1 = strlen( seq1[0] );
860 lgth2 = strlen( seq2[0] );
862 if( lgth1 > orlgth1 || lgth2 > orlgth2 )
866 if( orlgth1 > 0 && orlgth2 > 0 )
870 FreeFloatVec( match );
871 FreeFloatVec( initverticalw );
872 FreeFloatVec( lastverticalw );
879 FreeFloatVec( ogcp1 );
880 FreeFloatVec( ogcp2 );
881 FreeFloatVec( fgcp1 );
882 FreeFloatVec( fgcp2 );
885 FreeFloatMtx( cpmx1 );
886 FreeFloatMtx( cpmx2 );
888 FreeFloatMtx( floatwork );
889 FreeIntMtx( intwork );
892 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
893 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
896 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
899 w1 = AllocateFloatVec( ll2+2 );
900 w2 = AllocateFloatVec( ll2+2 );
901 match = AllocateFloatVec( ll2+2 );
903 initverticalw = AllocateFloatVec( ll1+2 );
904 lastverticalw = AllocateFloatVec( ll1+2 );
906 m = AllocateFloatVec( ll2+2 );
907 mp = AllocateIntVec( ll2+2 );
909 mseq = AllocateCharMtx( njob, ll1+ll2 );
911 ogcp1 = AllocateFloatVec( ll1+2 );
912 ogcp2 = AllocateFloatVec( ll2+2 );
913 fgcp1 = AllocateFloatVec( ll1+2 );
914 fgcp2 = AllocateFloatVec( ll2+2 );
916 cpmx1 = AllocateFloatMtx( 26, ll1+2 );
917 cpmx2 = AllocateFloatMtx( 26, ll2+2 );
919 floatwork = AllocateFloatMtx( 26, MAX( ll1, ll2 )+2 );
920 intwork = AllocateIntMtx( 26, MAX( ll1, ll2 )+2 );
923 fprintf( stderr, "succeeded\n" );
931 for( i=0; i<icyc; i++ )
936 for( j=0; j<jcyc; j++ )
938 mseq2[j] = mseq[icyc+j];
943 if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
947 if( commonAlloc1 && commonAlloc2 )
949 FreeShortMtx( commonIP );
952 ll1 = MAX( orlgth1, commonAlloc1 );
953 ll2 = MAX( orlgth2, commonAlloc2 );
956 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
959 commonIP = AllocateShortMtx( ll1+10, ll2+10 );
962 fprintf( stderr, "succeeded\n\n" );
973 for( i=0; i<icyc; i++ )
975 fprintf( stderr, "## totaleff = %f\n", t );
978 cpmx_calc_new( seq1, cpmx1, eff1, lgth1, icyc );
979 cpmx_calc_new( seq2, cpmx2, eff2, lgth2, jcyc );
981 OpeningGapCount( ogcp1, icyc, seq1, eff1, lgth1 );
982 OpeningGapCount( ogcp2, jcyc, seq2, eff2, lgth2 );
983 FinalGapCount( fgcp1, icyc, seq1, eff1, lgth1 );
984 FinalGapCount( fgcp2, jcyc, seq2, eff2, lgth2 );
986 for( i=0; i<lgth1; i++ )
988 ogcp1[i] = 0.5 * ( 1.0 - ogcp1[i] ) * fpenalty;
989 fgcp1[i] = 0.5 * ( 1.0 - fgcp1[i] ) * fpenalty;
991 for( i=0; i<lgth2; i++ )
993 ogcp2[i] = 0.5 * ( 1.0 - ogcp2[i] ) * fpenalty;
994 fgcp2[i] = 0.5 * ( 1.0 - fgcp2[i] ) * fpenalty;
997 for( i=0; i<lgth1; i++ )
998 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
1004 match_calc( initverticalw, cpmx2, cpmx1, 0, lgth1, floatwork, intwork, 1 );
1006 match_calc( currentw, cpmx1, cpmx2, 0, lgth2, floatwork, intwork, 1 );
1007 #if 0 // -> tbfast.c
1009 imp_match_calc( currentw, icyc, jcyc, lgth1, lgth2, seq1, seq2, eff1, eff2, localhom, 1, 0 );
1015 for( i=1; i<lgth1+1; i++ )
1017 initverticalw[i] += ( ogcp1[0] + fgcp1[i-1] ) ;
1019 for( j=1; j<lgth2+1; j++ )
1021 currentw[j] += ( ogcp2[0] + fgcp2[j-1] ) ;
1027 for( j=1; j<lgth2+1; j++ )
1028 currentw[j] -= offset * j / 2.0;
1029 for( i=1; i<lgth1+1; i++ )
1030 initverticalw[i] -= offset * i / 2.0;
1034 for( j=1; j<lgth2+1; ++j )
1036 m[j] = currentw[j-1] + ogcp1[1]; mp[j] = 0;
1039 lastverticalw[0] = currentw[lgth2-1];
1041 if( outgap ) lasti = lgth1+1; else lasti = lgth1;
1044 fprintf( stderr, "currentw = \n" );
1045 for( i=0; i<lgth1+1; i++ )
1047 fprintf( stderr, "%5.2f ", currentw[i] );
1049 fprintf( stderr, "\n" );
1050 fprintf( stderr, "initverticalw = \n" );
1051 for( i=0; i<lgth2+1; i++ )
1053 fprintf( stderr, "%5.2f ", initverticalw[i] );
1055 fprintf( stderr, "\n" );
1056 fprintf( stderr, "fcgp\n" );
1057 for( i=0; i<lgth1; i++ )
1058 fprintf( stderr, "fgcp1[%d]=%f\n", i, ogcp1[i] );
1059 for( i=0; i<lgth2; i++ )
1060 fprintf( stderr, "fgcp2[%d]=%f\n", i, ogcp2[i] );
1063 for( i=1; i<lasti; i++ )
1066 previousw = currentw;
1069 previousw[0] = initverticalw[i-1];
1071 match_calc( currentw, cpmx1, cpmx2, i, lgth2, floatwork, intwork, 0 );
1073 fprintf( stderr, "\n" );
1074 fprintf( stderr, "i=%d\n", i );
1075 fprintf( stderr, "currentw = \n" );
1076 for( j=0; j<lgth2; j++ )
1078 fprintf( stderr, "%5.2f ", currentw[j] );
1080 fprintf( stderr, "\n" );
1084 // fprintf( stderr, "Calling imp_match_calc (o) lgth = %d, i = %d\n", lgth1, i );
1086 imp_match_out_vead( currentw, i, lgth2 );
1088 imp_match_out_vead( currentw, i, lgth2 );
1092 fprintf( stderr, "\n" );
1093 fprintf( stderr, "i=%d\n", i );
1094 fprintf( stderr, "currentw = \n" );
1095 for( j=0; j<lgth2; j++ )
1097 fprintf( stderr, "%5.2f ", currentw[j] );
1099 fprintf( stderr, "\n" );
1101 currentw[0] = initverticalw[i];
1105 mi = previousw[0] + ogcp2[1]; mpi = 0;
1110 curpt = currentw + 1;
1112 for( j=1; j<lgth2+1; j++ )
1118 fprintf( stderr, "%5.0f->", wm );
1120 g = mi + fgcp2[j-1];
1122 fprintf( stderr, "%5.0f?", g );
1127 *ijppt = -( j - mpi );
1129 g = *prept + ogcp2[j];
1139 g = *mjpt + fgcp1[i-1];
1141 fprintf( stderr, "%5.0f?", g );
1146 *ijppt = +( i - *mpjpt );
1148 g = *prept + ogcp1[i];
1155 m[j] += fpenalty_ex;
1159 fprintf( stderr, "%5.0f ", wm );
1168 lastverticalw[i] = currentw[lgth2-1];
1170 floatncpy( previousw, currentw, lgth2+1 );
1171 previousw[0] = initverticalw[i-1];
1174 match_calc( currentw, cpmx1, cpmx2, i, lgth2, floatwork, intwork, 0 );
1175 currentw[0] = initverticalw[i];
1177 mi = previousw[0] + ogcp2[1]; mpi = 0;
1178 for( j=1; j<lgth2+1; j++ )
1180 wm = previousw[j-1];
1187 ijp[i][j] = -( j - mpi );
1190 if( mi <= previousw[j-1] + g )
1192 mi = previousw[j-1] + g;
1200 ijp[i][j] = +( i - mp[j] );
1203 if( m[j] <= previousw[j-1] + g )
1205 m[j] = previousw[j-1] + g ;
1210 lastverticalw[i] = currentw[lgth2-1];
1215 // fprintf( stderr, "wm = %f\n", wm );
1220 for( j=1; j<lgth2+1; j++ )
1221 currentw[j] -= offset * ( lgth2 - j ) / 2.0;
1222 for( i=1; i<lgth1+1; i++ )
1223 lastverticalw[i] -= offset * ( lgth1 - i / 2.0);
1228 fprintf( stderr, "\n" );
1229 for( i=0; i<icyc; i++ ) fprintf( stderr,"%s\n", seq1[i] );
1230 fprintf( stderr, "#####\n" );
1231 for( j=0; j<jcyc; j++ ) fprintf( stderr,"%s\n", seq2[j] );
1232 fprintf( stderr, "====>" );
1233 for( i=0; i<icyc; i++ ) strcpy( mseq1[i], seq1[i] );
1234 for( j=0; j<jcyc; j++ ) strcpy( mseq2[j], seq2[j] );
1238 Atracking_localhom( impmatch, currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
1241 Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
1243 // fprintf( stderr, "### impmatch = %f\n", *impmatch );
1245 resultlen = strlen( mseq1[0] );
1246 if( alloclen < resultlen || resultlen > N )
1248 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
1249 ErrorExit( "LENGTH OVER!\n" );
1253 for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
1254 for( j=0; j<jcyc; j++ ) strcpy( seq2[j], mseq2[j] );
1256 fprintf( stderr, "\n" );
1257 for( i=0; i<icyc; i++ ) fprintf( stderr, "%s\n", mseq1[i] );
1258 fprintf( stderr, "#####\n" );
1259 for( j=0; j<jcyc; j++ ) fprintf( stderr, "%s\n", mseq2[j] );
1262 fprintf( stderr, "wm = %f\n", wm );
1267 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 )
1268 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
1272 int lasti, lastj; /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
1275 float wm; /* int ?????? */
1277 float *currentw, *previousw;
1278 // float fpenalty = (float)penalty;
1279 float fpenalty_ex = (float)penalty_ex;
1283 float *mjpt, *prept, *curpt;
1286 static float mi, *m;
1288 static int mpi, *mp;
1289 static float *w1, *w2;
1290 static float *match;
1291 static float *initverticalw; /* kufuu sureba iranai */
1292 static float *lastverticalw; /* kufuu sureba iranai */
1293 static char **mseq1;
1294 static char **mseq2;
1296 static float *ogcp1;
1297 static float *ogcp2;
1298 static float *fgcp1;
1299 static float *fgcp2;
1300 static float **cpmx1;
1301 static float **cpmx2;
1302 static int **intwork;
1303 static float **floatwork;
1304 static int orlgth1 = 0, orlgth2 = 0;
1308 fprintf( stderr, "eff in SA+++align\n" );
1309 for( i=0; i<icyc; i++ ) fprintf( stderr, "eff1[%d] = %f\n", i, eff1[i] );
1313 mseq1 = AllocateCharMtx( njob, 0 );
1314 mseq2 = AllocateCharMtx( njob, 0 );
1318 lgth1 = strlen( seq1[0] );
1319 lgth2 = strlen( seq2[0] );
1321 if( lgth1 > orlgth1 || lgth2 > orlgth2 )
1325 if( orlgth1 > 0 && orlgth2 > 0 )
1329 FreeFloatVec( match );
1330 FreeFloatVec( initverticalw );
1331 FreeFloatVec( lastverticalw );
1336 FreeCharMtx( mseq );
1338 FreeFloatVec( ogcp1 );
1339 FreeFloatVec( ogcp2 );
1340 FreeFloatVec( fgcp1 );
1341 FreeFloatVec( fgcp2 );
1344 FreeFloatMtx( cpmx1 );
1345 FreeFloatMtx( cpmx2 );
1347 FreeFloatMtx( floatwork );
1348 FreeIntMtx( intwork );
1351 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
1352 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
1355 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
1358 w1 = AllocateFloatVec( ll2+2 );
1359 w2 = AllocateFloatVec( ll2+2 );
1360 match = AllocateFloatVec( ll2+2 );
1362 initverticalw = AllocateFloatVec( ll1+2 );
1363 lastverticalw = AllocateFloatVec( ll1+2 );
1365 m = AllocateFloatVec( ll2+2 );
1366 mp = AllocateIntVec( ll2+2 );
1368 mseq = AllocateCharMtx( njob, ll1+ll2 );
1370 ogcp1 = AllocateFloatVec( ll1+2 );
1371 ogcp2 = AllocateFloatVec( ll2+2 );
1372 fgcp1 = AllocateFloatVec( ll1+2 );
1373 fgcp2 = AllocateFloatVec( ll2+2 );
1375 cpmx1 = AllocateFloatMtx( 26, ll1+2 );
1376 cpmx2 = AllocateFloatMtx( 26, ll2+2 );
1378 floatwork = AllocateFloatMtx( 26, MAX( ll1, ll2 )+2 );
1379 intwork = AllocateIntMtx( 26, MAX( ll1, ll2 )+2 );
1382 fprintf( stderr, "succeeded\n" );
1385 orlgth1 = ll1 - 100;
1386 orlgth2 = ll2 - 100;
1390 for( i=0; i<icyc; i++ )
1395 for( j=0; j<jcyc; j++ )
1397 mseq2[j] = mseq[icyc+j];
1402 if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
1406 if( commonAlloc1 && commonAlloc2 )
1408 FreeShortMtx( commonIP );
1411 ll1 = MAX( orlgth1, commonAlloc1 );
1412 ll2 = MAX( orlgth2, commonAlloc2 );
1415 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
1418 commonIP = AllocateShortMtx( ll1+10, ll2+10 );
1421 fprintf( stderr, "succeeded\n\n" );
1429 cpmx_calc_new( seq1, cpmx1, eff1, strlen( seq1[0] ), icyc );
1430 cpmx_calc_new( seq2, cpmx2, eff2, strlen( seq2[0] ), jcyc );
1432 OpeningGapCount( ogcp1, icyc, seq1, eff1, lgth1 );
1433 OpeningGapCount( ogcp2, jcyc, seq2, eff2, lgth2 );
1434 FinalGapCount( fgcp1, icyc, seq1, eff1, lgth1 );
1435 FinalGapCount( fgcp2, jcyc, seq2, eff2, lgth2 );
1437 for( i=0; i<lgth1; i++ )
1439 ogcp1[i] = 0.5 * ( 1.0 - ogcp1[i] );
1440 fgcp1[i] = 0.5 * ( 1.0 - fgcp1[i] );
1442 for( i=0; i<lgth2; i++ )
1444 ogcp2[i] = 0.5 * ( 1.0 - ogcp2[i] );
1445 fgcp2[i] = 0.5 * ( 1.0 - fgcp2[i] );
1448 for( i=0; i<lgth1; i++ )
1449 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
1456 match_calc( initverticalw, cpmx2, cpmx1, 0, lgth1, floatwork, intwork, 1 );
1459 match_calc( currentw, cpmx1, cpmx2, 0, lgth2, floatwork, intwork, 1 );
1460 #if 0 // -> tbfast.c
1462 imp_match_calc( currentw, icyc, jcyc, lgth1, lgth2, seq1, seq2, eff1, eff2, localhom, 1, 0 );
1468 for( i=1; i<lgth1+1; i++ )
1470 initverticalw[i] += ( ogcp1[0] + fgcp1[i-1] ) ;
1472 for( j=1; j<lgth2+1; j++ )
1474 currentw[j] += ( ogcp2[0] + fgcp2[j-1] ) ;
1480 for( j=1; j<lgth2+1; j++ )
1481 currentw[j] -= offset * j / 2.0;
1482 for( i=1; i<lgth1+1; i++ )
1483 initverticalw[i] -= offset * i / 2.0;
1487 for( j=1; j<lgth2+1; ++j )
1489 m[j] = currentw[j-1] + ogcp1[1]; mp[j] = 0;
1492 lastverticalw[0] = currentw[lgth2-1];
1494 if( outgap ) lasti = lgth1+1; else lasti = lgth1;
1497 fprintf( stderr, "currentw = \n" );
1498 for( i=0; i<lgth1+1; i++ )
1500 fprintf( stderr, "%5.2f ", currentw[i] );
1502 fprintf( stderr, "\n" );
1503 fprintf( stderr, "initverticalw = \n" );
1504 for( i=0; i<lgth2+1; i++ )
1506 fprintf( stderr, "%5.2f ", initverticalw[i] );
1508 fprintf( stderr, "\n" );
1509 fprintf( stderr, "fcgp\n" );
1510 for( i=0; i<lgth1; i++ )
1511 fprintf( stderr, "fgcp1[%d]=%f\n", i, ogcp1[i] );
1512 for( i=0; i<lgth2; i++ )
1513 fprintf( stderr, "fgcp2[%d]=%f\n", i, ogcp2[i] );
1516 for( i=1; i<lasti; i++ )
1519 previousw = currentw;
1522 previousw[0] = initverticalw[i-1];
1524 match_calc( currentw, cpmx1, cpmx2, i, lgth2, floatwork, intwork, 0 );
1526 fprintf( stderr, "\n" );
1527 fprintf( stderr, "i=%d\n", i );
1528 fprintf( stderr, "currentw = \n" );
1529 for( j=0; j<lgth2; j++ )
1531 fprintf( stderr, "%5.2f ", currentw[j] );
1533 fprintf( stderr, "\n" );
1537 // fprintf( stderr, "Calling imp_match_calc (o) lgth = %d, i = %d\n", lgth1, i );
1539 imp_match_out_vead( currentw, i, lgth2 );
1541 imp_match_out_vead_gapmap( currentw, gapmap1[i], lgth2, gapmap2 );
1545 fprintf( stderr, "\n" );
1546 fprintf( stderr, "i=%d\n", i );
1547 fprintf( stderr, "currentw = \n" );
1548 for( j=0; j<lgth2; j++ )
1550 fprintf( stderr, "%5.2f ", currentw[j] );
1552 fprintf( stderr, "\n" );
1554 currentw[0] = initverticalw[i];
1558 mi = previousw[0] + ogcp2[1]; mpi = 0;
1563 curpt = currentw + 1;
1565 for( j=1; j<lgth2+1; j++ )
1571 fprintf( stderr, "%5.0f->", wm );
1573 g = mi + fgcp2[j-1];
1575 fprintf( stderr, "%5.0f?", g );
1580 *ijppt = -( j - mpi );
1582 g = *prept + ogcp2[j];
1592 g = *mjpt + fgcp1[i-1];
1594 fprintf( stderr, "%5.0f?", g );
1599 *ijppt = +( i - *mpjpt );
1601 g = *prept + ogcp1[i];
1608 m[j] += fpenalty_ex;
1612 fprintf( stderr, "%5.0f ", wm );
1621 lastverticalw[i] = currentw[lgth2-1];
1623 floatncpy( previousw, currentw, lgth2+1 );
1624 previousw[0] = initverticalw[i-1];
1627 match_calc( currentw, cpmx1, cpmx2, i, lgth2, floatwork, intwork, 0 );
1628 currentw[0] = initverticalw[i];
1630 mi = previousw[0] + ogcp2[1]; mpi = 0;
1632 for( j=1; j<lastj; j++ )
1634 wm = previousw[j-1];
1641 ijp[i][j] = -( j - mpi );
1644 if( mi <= previousw[j-1] + g )
1646 mi = previousw[j-1] + g;
1654 ijp[i][j] = +( i - mp[j] );
1657 if( m[j] <= previousw[j-1] + g )
1659 m[j] = previousw[j-1] + g ;
1664 lastverticalw[i] = currentw[lgth2-1];
1672 for( j=1; j<lgth2+1; j++ )
1673 currentw[j] -= offset * ( lgth2 - j ) / 2.0;
1674 for( i=1; i<lgth1+1; i++ )
1675 lastverticalw[i] -= offset * ( lgth1 - i / 2.0);
1680 fprintf( stderr, "\n" );
1681 for( i=0; i<icyc; i++ ) fprintf( stderr,"%s\n", seq1[i] );
1682 fprintf( stderr, "#####\n" );
1683 for( j=0; j<jcyc; j++ ) fprintf( stderr,"%s\n", seq2[j] );
1684 fprintf( stderr, "====>" );
1685 for( i=0; i<icyc; i++ ) strcpy( mseq1[i], seq1[i] );
1686 for( j=0; j<jcyc; j++ ) strcpy( mseq2[j], seq2[j] );
1690 Atracking_localhom_gapmap( impmatch, currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc, gapmap1, gapmap2 );
1693 Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
1695 // fprintf( stderr, "### impmatch = %f\n", *impmatch );
1697 resultlen = strlen( mseq1[0] );
1698 if( alloclen < resultlen || resultlen > N )
1700 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
1701 ErrorExit( "LENGTH OVER!\n" );
1705 for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
1706 for( j=0; j<jcyc; j++ ) strcpy( seq2[j], mseq2[j] );
1708 fprintf( stderr, "\n" );
1709 for( i=0; i<icyc; i++ ) fprintf( stderr, "%s\n", mseq1[i] );
1710 fprintf( stderr, "#####\n" );
1711 for( j=0; j<jcyc; j++ ) fprintf( stderr, "%s\n", mseq2[j] );
1714 fprintf( stderr, "wm = %f\n", wm );
1719 float translate_and_Calign( char **mseq1, char **mseq2, double *effarr1, double *effarr2, int clus1, int clus2, int alloclen )
1729 if ( clus1 == 1 && clus2 != 1 )
1731 seq = mseq1[0]; aseq = mseq2; effarr = effarr2; nseq = clus2+1;
1733 printf( "effarr in transl... = \n" );
1734 for( i=0; i<clus2; i++ ) printf( "%f ", effarr2[i] );
1737 else if( clus1 != 1 && clus2 == 1 )
1739 seq = mseq2[0]; aseq = mseq1; effarr = effarr1; nseq = clus1+1;
1741 else ErrorExit( "ERROR in translate_and_Calign" );
1743 result = Calignm1( &wm, aseq, seq, effarr, nseq-2, 0 );
1745 resultlen = strlen( result[0] );
1746 if( alloclen < resultlen || resultlen > N )
1748 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
1749 ErrorExit( "LENGTH OVER!\n" );
1751 for( i=0; i<nseq-1; i++ ) strcpy( aseq[i], result[i] );
1752 strcpy( seq, result[nseq-1] );