8 #define USE_PENALTY_EX 0
9 #define FASTMATCHCALC 1
12 static float **impmtx = NULL;
14 float imp_match_out_sc( int i1, int j1 )
16 // fprintf( stderr, "imp+match = %f\n", impmtx[i1][j1] * fastathreshold );
17 // fprintf( stderr, "val = %f\n", impmtx[i1][j1] );
18 return( impmtx[i1][j1] );
21 static void imp_match_out_vead_gapmap( float *imp, int i1, int lgth2, int *gapmap2 )
24 float *pt = impmtx[i1];
25 int *gapmappt = gapmap2;
27 *imp++ += pt[*gapmappt++];
30 float *pt = impmtx[i1];
31 for( j=0; j<lgth2; j++ )
32 *imp++ += pt[gapmap2[j]];
36 static void imp_match_out_vead_tate_gapmap( float *imp, int j1, int lgth1, int *gapmap1 )
39 int *gapmappt = gapmap1;
41 *imp++ += impmtx[*gapmappt++][j1];
44 for( i=0; i<lgth1; i++ )
45 *imp++ += impmtx[gapmap1[i]][j1];
49 static void imp_match_out_vead( float *imp, int i1, int lgth2 )
52 float *pt = impmtx[i1];
57 float *pt = impmtx[i1];
58 for( j=0; j<lgth2; j++ )
62 static void imp_match_out_vead_tate( float *imp, int j1, int lgth1 )
65 for( i=0; i<lgth1; i++ )
66 *imp++ += impmtx[i][j1];
69 #if 0 // -> Salignmm.c
70 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 )
72 int i, j, k1, k2, tmpint, start1, start2, end1, end2;
73 static int impalloclen = 0;
77 static char *nocount1 = NULL;
78 static char *nocount2 = NULL;
81 if( impalloclen < lgth1 + 2 || impalloclen < lgth2 + 2 )
83 if( impmtx ) FreeFloatMtx( impmtx );
84 if( nocount1 ) free( nocount1 );
85 if( nocount2 ) free( nocount2 );
86 impalloclen = MAX( lgth1, lgth2 ) + 2;
87 impmtx = AllocateFloatMtx( impalloclen, impalloclen );
88 nocount1 = AllocateCharVec( impalloclen );
89 nocount2 = AllocateCharVec( impalloclen );
92 for( i=0; i<lgth1; i++ )
94 for( j=0; j<clus1; j++ )
95 if( seq1[j][i] == '-' ) break;
96 if( j != clus1 ) nocount1[i] = 1;
99 for( i=0; i<lgth2; i++ )
101 for( j=0; j<clus2; j++ )
102 if( seq2[j][i] == '-' ) break;
103 if( j != clus2 ) nocount2[i] = 1;
104 else nocount2[i] = 0;
108 fprintf( stderr, "nocount2 =\n" );
109 for( i = 0; i<impalloclen; i++ )
111 fprintf( stderr, "nocount2[%d] = %d (%c)\n", i, nocount2[i], seq2[0][i] );
118 fprintf( stderr, "eff1 in _init_strict = \n" );
119 for( i=0; i<clus1; i++ )
120 fprintf( stderr, "eff1[] = %f\n", eff1[i] );
121 for( i=0; i<clus2; i++ )
122 fprintf( stderr, "eff2[] = %f\n", eff2[i] );
125 for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
127 effijx = fastathreshold;
128 for( i=0; i<clus1; i++ )
130 for( j=0; j<clus2; j++ )
132 effij = (float)( eff1[i] * eff2[j] * effijx );
133 tmpptr = localhom[i][j];
136 // fprintf( stderr, "start1 = %d\n", tmpptr->start1 );
137 // fprintf( stderr, "end1 = %d\n", tmpptr->end1 );
138 // fprintf( stderr, "i = %d, seq1 = \n%s\n", i, seq1[i] );
139 // fprintf( stderr, "j = %d, seq2 = \n%s\n", j, seq2[j] );
144 if( *pt++ != '-' ) tmpint++;
145 if( tmpint == tmpptr->start1 ) break;
147 start1 = pt - seq1[i] - 1;
149 if( tmpptr->start1 == tmpptr->end1 ) end1 = start1;
155 // fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, tmpptr->end1, pt-seq1[i] );
156 if( tmpint == tmpptr->end1 ) break;
157 if( *pt++ != '-' ) tmpint++;
159 end1 = pt - seq1[i] - 0;
163 // fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, tmpptr->end1, pt-seq1[i] );
164 if( *pt++ != '-' ) tmpint++;
165 if( tmpint == tmpptr->end1 ) break;
167 end1 = pt - seq1[i] - 1;
175 if( *pt++ != '-' ) tmpint++;
176 if( tmpint == tmpptr->start2 ) break;
178 start2 = pt - seq2[j] - 1;
179 if( tmpptr->start2 == tmpptr->end2 ) end2 = start2;
185 if( tmpint == tmpptr->end2 ) break;
186 if( *pt++ != '-' ) tmpint++;
188 end2 = pt - seq2[j] - 0;
192 if( *pt++ != '-' ) tmpint++;
193 if( tmpint == tmpptr->end2 ) break;
195 end2 = pt - seq2[j] - 1;
198 // 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] );
199 // fprintf( stderr, "step 0\n" );
200 if( end1 - start1 != end2 - start2 )
202 // fprintf( stderr, "CHUUI!!, start1 = %d, end1 = %d, start2 = %d, end2 = %d\n", start1, end1, start2, end2 );
206 k1 = start1; k2 = start2;
209 while( *pt1 && *pt2 )
211 if( *pt1 != '-' && *pt2 != '-' )
213 // ½Å¤ß¤òÆó½Å¤Ë¤«¤±¤Ê¤¤¤è¤¦¤ËÃðդ·¤Æ²¼¤µ¤¤¡£
214 // impmtx[k1][k2] += tmpptr->wimportance * fastathreshold;
215 // impmtx[k1][k2] += tmpptr->importance * effij;
216 impmtx[k1][k2] += tmpptr->fimportance * effij;
217 // fprintf( stderr, "#### impmtx[k1][k2] = %f, tmpptr->fimportance=%f, effij=%f\n", impmtx[k1][k2], tmpptr->fimportance, effij );
218 // fprintf( stderr, "mark, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
219 // fprintf( stderr, "%d (%c) - %d (%c) - %f\n", k1, *pt1, k2, *pt2, tmpptr->fimportance * effij );
223 else if( *pt1 != '-' && *pt2 == '-' )
225 // fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
228 else if( *pt1 == '-' && *pt2 != '-' )
230 // fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
233 else if( *pt1 == '-' && *pt2 == '-' )
235 // fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
239 if( k1 > end1 || k2 > end2 ) break;
242 while( k1 <= end1 && k2 <= end2 )
244 fprintf( stderr, "k1,k2=%d,%d - ", k1, k2 );
245 if( !nocount1[k1] && !nocount2[k2] )
247 impmtx[k1][k2] += tmpptr->wimportance * eff1[i] * eff2[j] * fastathreshold;
248 fprintf( stderr, "marked\n" );
251 fprintf( stderr, "no count\n" );
255 tmpptr = tmpptr->next;
260 if( clus1 == 1 && clus2 == 6 )
262 fprintf( stderr, "\n" );
263 fprintf( stderr, "seq1[0] = %s\n", seq1[0] );
264 fprintf( stderr, "seq2[0] = %s\n", seq2[0] );
265 fprintf( stderr, "impmtx = \n" );
266 for( k2=0; k2<lgth2; k2++ )
267 fprintf( stderr, "%6.3f ", (double)k2 );
268 fprintf( stderr, "\n" );
269 for( k1=0; k1<lgth1; k1++ )
271 fprintf( stderr, "%d ", k1 );
272 for( k2=0; k2<3; k2++ )
273 fprintf( stderr, "%2.1f ", impmtx[k1][k2] );
274 fprintf( stderr, "\n" );
283 void imp_match_init( float *imp, int clus1, int clus2, int lgth1, int lgth2, char **seq1, char **seq2, double *eff1, double *eff2, LocalHom ***localhom )
285 int dif, i, j, k1, k2, tmpint, start1, start2, end1, end2;
286 static int impalloclen = 0;
289 static char *nocount1 = NULL;
290 static char *nocount2 = NULL;
292 if( impalloclen < lgth1 + 2 || impalloclen < lgth2 + 2 )
294 if( impmtx ) FreeFloatMtx( impmtx );
295 if( nocount1 ) free( nocount1 );
296 if( nocount2 ) free( nocount2 );
297 impalloclen = MAX( lgth1, lgth2 ) + 2;
298 impmtx = AllocateFloatMtx( impalloclen, impalloclen );
299 nocount1 = AllocateCharVec( impalloclen );
300 nocount2 = AllocateCharVec( impalloclen );
303 for( i=0; i<lgth1; i++ )
305 for( j=0; j<clus1; j++ )
306 if( seq1[j][i] == '-' ) break;
307 if( j != clus1 ) nocount1[i] = 1;
308 else nocount1[i] = 0;
310 for( i=0; i<lgth2; i++ )
312 for( j=0; j<clus2; j++ )
313 if( seq2[j][i] == '-' ) break;
314 if( j != clus2 ) nocount2[i] = 1;
315 else nocount2[i] = 0;
319 fprintf( stderr, "nocount2 =\n" );
320 for( i = 0; i<impalloclen; i++ )
322 fprintf( stderr, "nocount2[%d] = %d (%c)\n", i, nocount2[i], seq2[0][i] );
326 for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
328 for( i=0; i<clus1; i++ )
330 fprintf( stderr, "i = %d, seq1 = %s\n", i, seq1[i] );
331 for( j=0; j<clus2; j++ )
333 fprintf( stderr, "start1 = %d\n", localhom[i][j]->start1 );
334 fprintf( stderr, "end1 = %d\n", localhom[i][j]->end1 );
335 fprintf( stderr, "j = %d, seq2 = %s\n", j, seq2[j] );
340 if( *pt++ != '-' ) tmpint++;
341 if( tmpint == localhom[i][j]->start1 ) break;
343 start1 = pt - seq1[i] - 1;
347 // fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, localhom[i][j].end1, pt-seq1[i] );
348 if( *pt++ != '-' ) tmpint++;
349 if( tmpint == localhom[i][j]->end1 ) break;
351 end1 = pt - seq1[i] - 1;
357 if( *pt++ != '-' ) tmpint++;
358 if( tmpint == localhom[i][j]->start2 ) break;
360 start2 = pt - seq2[j] - 1;
363 if( *pt++ != '-' ) tmpint++;
364 if( tmpint == localhom[i][j]->end2 ) break;
366 end2 = pt - seq2[j] - 1;
367 // fprintf( stderr, "start1 = %d, end1 = %d, start2 = %d, end2 = %d\n", start1, end1, start2, end2 );
370 fprintf( stderr, "step 0\n" );
371 while( k1 <= end1 && k2 <= end2 )
374 if( !nocount1[k1] && !nocount2[k2] )
375 impmtx[k1][k2] += localhom[i][j].wimportance * eff1[i] * eff2[j];
378 if( !nocount1[k1] && !nocount2[k2] )
379 impmtx[k1][k2] += localhom[i][j]->wimportance * eff1[i] * eff2[j];
384 dif = ( end1 - start1 ) - ( end2 - start2 );
385 fprintf( stderr, "dif = %d\n", dif );
390 fprintf( stderr, "dif = %d\n", dif );
393 while( k1 <= end1 && k2 <= end2 )
395 if( 0 <= k2 && start2 <= k2 && !nocount1[k1] && !nocount2[k2] )
396 impmtx[k1][k2] = localhom[i][j]->wimportance * eff1[i] * eff2[j];
410 if( k1 >= 0 && k1 >= start1 && !nocount1[k1] && !nocount2[k2] )
411 impmtx[k1][k2] = localhom[i][j]->wimportance * eff1[i] * eff2[j];
420 fprintf( stderr, "impmtx = \n" );
421 for( k2=0; k2<lgth2; k2++ )
422 fprintf( stderr, "%6.3f ", (double)k2 );
423 fprintf( stderr, "\n" );
424 for( k1=0; k1<lgth1; k1++ )
426 fprintf( stderr, "%d", k1 );
427 for( k2=0; k2<lgth2; k2++ )
428 fprintf( stderr, "%6.3f ", impmtx[k1][k2] );
429 fprintf( stderr, "\n" );
435 static void match_calc( float *match, float **cpmx1, float **cpmx2, int i1, int lgth2, float **floatwork, int **intwork, int initialize )
440 float **cpmxpd = floatwork;
441 int **cpmxpdn = intwork;
442 float *matchpt, *cpmxpdpt, **cpmxpdptpt;
443 int *cpmxpdnpt, **cpmxpdnptpt;
447 for( j=0; j<lgth2; j++ )
450 for( l=0; l<26; l++ )
454 cpmxpd[j][count] = cpmx2[l][j];
455 cpmxpdn[j][count] = l;
459 cpmxpdn[j][count] = -1;
464 for( l=0; l<26; l++ )
467 for( j=0; j<26; j++ )
468 scarr[l] += n_dis[j][l] * cpmx1[j][i1];
471 cpmxpdnptpt = cpmxpdn;
476 cpmxpdnpt = *cpmxpdnptpt++;
477 cpmxpdpt = *cpmxpdptpt++;
478 while( *cpmxpdnpt>-1 )
479 *matchpt += scarr[*cpmxpdnpt++] * *cpmxpdpt++;
486 float **cpmxpd = floatwork;
487 int **cpmxpdn = intwork;
492 for( j=0; j<lgth2; j++ )
495 for( l=0; l<26; l++ )
499 cpmxpd[count][j] = cpmx2[l][j];
500 cpmxpdn[count][j] = l;
504 cpmxpdn[count][j] = -1;
507 for( l=0; l<26; l++ )
510 for( k=0; k<26; k++ )
511 scarr[l] += n_dis[k][l] * cpmx1[k][i1];
513 for( j=0; j<lgth2; j++ )
516 for( k=0; cpmxpdn[k][j]>-1; k++ )
517 match[j] += scarr[cpmxpdn[k][j]] * cpmxpd[k][j];
522 static void Atracking_localhom( float *impwmpt, float *lasthorizontalw, float *lastverticalw,
523 char **seq1, char **seq2,
524 char **mseq1, char **mseq2,
525 float **cpmx1, float **cpmx2,
526 short **ijp, int icyc, int jcyc )
528 int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k;
530 char *gaptable1, *gt1bk;
531 char *gaptable2, *gt2bk;
532 lgth1 = strlen( seq1[0] );
533 lgth2 = strlen( seq2[0] );
534 gt1bk = AllocateCharVec( lgth1+lgth2+1 );
535 gt2bk = AllocateCharVec( lgth1+lgth2+1 );
538 for( i=0; i<lgth1; i++ )
540 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
548 wm = lastverticalw[0];
549 for( i=0; i<lgth1; i++ )
551 if( lastverticalw[i] >= wm )
553 wm = lastverticalw[i];
554 iin = i; jin = lgth2-1;
555 ijp[lgth1][lgth2] = +( lgth1 - i );
558 for( j=0; j<lgth2; j++ )
560 if( lasthorizontalw[j] >= wm )
562 wm = lasthorizontalw[j];
563 iin = lgth1-1; jin = j;
564 ijp[lgth1][lgth2] = -( lgth2 - j );
569 for( i=0; i<lgth1+1; i++ )
573 for( j=0; j<lgth2+1; j++ )
575 ijp[0][j] = -( j + 1 );
578 gaptable1 = gt1bk + lgth1+lgth2;
580 gaptable2 = gt2bk + lgth1+lgth2;
583 iin = lgth1; jin = lgth2;
585 for( k=0; k<=lgth1+lgth2; k++ )
587 if( ijp[iin][jin] < 0 )
589 ifi = iin-1; jfi = jin+ijp[iin][jin];
591 else if( ijp[iin][jin] > 0 )
593 ifi = iin-ijp[iin][jin]; jfi = jin-1;
597 ifi = iin-1; jfi = jin-1;
613 if( iin == lgth1 || jin == lgth2 )
617 *impwmpt += imp_match_out_sc( iin, jin );
619 // fprintf( stderr, "impwm = %f (iin=%d, jin=%d) seq1=%c, seq2=%c\n", *impwmpt, iin, jin, seq1[0][iin], seq2[0][jin] );
621 if( iin <= 0 || jin <= 0 ) break;
625 iin = ifi; jin = jfi;
628 for( i=0; i<icyc; i++ ) gapireru( mseq1[i], seq1[i], gaptable1 );
629 for( j=0; j<jcyc; j++ ) gapireru( mseq2[j], seq2[j], gaptable2 );
634 static void Atracking_localhom_gapmap( float *impwmpt, float *lasthorizontalw, float *lastverticalw,
635 char **seq1, char **seq2,
636 char **mseq1, char **mseq2,
637 float **cpmx1, float **cpmx2,
638 short **ijp, int icyc, int jcyc,
639 int *gapmap1, int *gapmap2 )
641 int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k;
643 char *gaptable1, *gt1bk;
644 char *gaptable2, *gt2bk;
645 lgth1 = strlen( seq1[0] );
646 lgth2 = strlen( seq2[0] );
647 gt1bk = AllocateCharVec( lgth1+lgth2+1 );
648 gt2bk = AllocateCharVec( lgth1+lgth2+1 );
651 for( i=0; i<lgth1; i++ )
653 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
661 wm = lastverticalw[0];
662 for( i=0; i<lgth1; i++ )
664 if( lastverticalw[i] >= wm )
666 wm = lastverticalw[i];
667 iin = i; jin = lgth2-1;
668 ijp[lgth1][lgth2] = +( lgth1 - i );
671 for( j=0; j<lgth2; j++ )
673 if( lasthorizontalw[j] >= wm )
675 wm = lasthorizontalw[j];
676 iin = lgth1-1; jin = j;
677 ijp[lgth1][lgth2] = -( lgth2 - j );
682 for( i=0; i<lgth1+1; i++ )
686 for( j=0; j<lgth2+1; j++ )
688 ijp[0][j] = -( j + 1 );
691 gaptable1 = gt1bk + lgth1+lgth2;
693 gaptable2 = gt2bk + lgth1+lgth2;
696 iin = lgth1; jin = lgth2;
698 for( k=0; k<=lgth1+lgth2; k++ )
700 if( ijp[iin][jin] < 0 )
702 ifi = iin-1; jfi = jin+ijp[iin][jin];
704 else if( ijp[iin][jin] > 0 )
706 ifi = iin-ijp[iin][jin]; jfi = jin-1;
710 ifi = iin-1; jfi = jin-1;
726 if( iin == lgth1 || jin == lgth2 )
730 *impwmpt += imp_match_out_sc( gapmap1[iin], gapmap2[jin] );
732 // fprintf( stderr, "impwm = %f (iin=%d, jin=%d) seq1=%c, seq2=%c\n", *impwmpt, iin, jin, seq1[0][iin], seq2[0][jin] );
734 if( iin <= 0 || jin <= 0 ) break;
738 iin = ifi; jin = jfi;
740 for( i=0; i<icyc; i++ ) gapireru( mseq1[i], seq1[i], gaptable1 );
741 for( j=0; j<jcyc; j++ ) gapireru( mseq2[j], seq2[j], gaptable2 );
746 static float Atracking( float *lasthorizontalw, float *lastverticalw,
747 char **seq1, char **seq2,
748 char **mseq1, char **mseq2,
749 float **cpmx1, float **cpmx2,
750 short **ijp, int icyc, int jcyc )
752 int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k;
754 char *gaptable1, *gt1bk;
755 char *gaptable2, *gt2bk;
756 lgth1 = strlen( seq1[0] );
757 lgth2 = strlen( seq2[0] );
759 gt1bk = AllocateCharVec( lgth1+lgth2+1 );
760 gt2bk = AllocateCharVec( lgth1+lgth2+1 );
763 for( i=0; i<lgth1; i++ )
765 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
773 wm = lastverticalw[0];
774 for( i=0; i<lgth1; i++ )
776 if( lastverticalw[i] >= wm )
778 wm = lastverticalw[i];
779 iin = i; jin = lgth2-1;
780 ijp[lgth1][lgth2] = +( lgth1 - i );
783 for( j=0; j<lgth2; j++ )
785 if( lasthorizontalw[j] >= wm )
787 wm = lasthorizontalw[j];
788 iin = lgth1-1; jin = j;
789 ijp[lgth1][lgth2] = -( lgth2 - j );
794 for( i=0; i<lgth1+1; i++ )
798 for( j=0; j<lgth2+1; j++ )
800 ijp[0][j] = -( j + 1 );
803 gaptable1 = gt1bk + lgth1+lgth2;
805 gaptable2 = gt2bk + lgth1+lgth2;
808 iin = lgth1; jin = lgth2;
809 for( k=0; k<=lgth1+lgth2; k++ )
811 if( ijp[iin][jin] < 0 )
813 ifi = iin-1; jfi = jin+ijp[iin][jin];
815 else if( ijp[iin][jin] > 0 )
817 ifi = iin-ijp[iin][jin]; jfi = jin-1;
821 ifi = iin-1; jfi = jin-1;
837 if( iin <= 0 || jin <= 0 ) break;
841 iin = ifi; jin = jfi;
844 for( i=0; i<icyc; i++ ) gapireru( mseq1[i], seq1[i], gaptable1 );
845 for( j=0; j<jcyc; j++ ) gapireru( mseq2[j], seq2[j], gaptable2 );
853 float H__align( char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen, LocalHom ***localhom, float *impmatch, char *sgap1, char *sgap2, char *egap1, char *egap2 )
854 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
858 int lasti, lastj; /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
861 float wm = 0.0; /* int ?????? */
863 float *currentw, *previousw;
864 // float fpenalty = (float)penalty;
866 float fpenalty_ex = (float)penalty_ex;
871 float *mjpt, *prept, *curpt;
877 static float *w1, *w2;
879 static float *initverticalw; /* kufuu sureba iranai */
880 static float *lastverticalw; /* kufuu sureba iranai */
888 static float *ogcp1g;
889 static float *ogcp2g;
890 static float *fgcp1g;
891 static float *fgcp2g;
896 static float **cpmx1;
897 static float **cpmx2;
898 static int **intwork;
899 static float **floatwork;
900 static int orlgth1 = 0, orlgth2 = 0;
901 float fpenalty = (float)penalty;
911 fprintf( stderr, "#### eff in SA+++align\n" );
912 fprintf( stderr, "#### seq1[0] = %s\n", seq1[0] );
913 fprintf( stderr, "#### strlen( seq1[0] ) = %d\n", strlen( seq1[0] ) );
914 for( i=0; i<icyc; i++ ) fprintf( stderr, "eff1[%d] = %f\n", i, eff1[i] );
918 mseq1 = AllocateCharMtx( njob, 0 );
919 mseq2 = AllocateCharMtx( njob, 0 );
923 lgth1 = strlen( seq1[0] );
924 lgth2 = strlen( seq2[0] );
926 if( lgth1 == 0 || lgth2 == 0 )
928 fprintf( stderr, "WARNING (Aalignmm): lgth1=%d, lgth2=%d\n", lgth1, lgth2 );
932 if( lgth1 > orlgth1 || lgth2 > orlgth2 )
936 if( orlgth1 > 0 && orlgth2 > 0 )
940 FreeFloatVec( match );
941 FreeFloatVec( initverticalw );
942 FreeFloatVec( lastverticalw );
949 FreeFloatVec( diaf1 );
950 FreeFloatVec( diaf2 );
951 FreeFloatVec( gapf1 );
952 FreeFloatVec( gapf2 );
953 FreeFloatVec( ogcp1 );
954 FreeFloatVec( ogcp2 );
955 FreeFloatVec( fgcp1 );
956 FreeFloatVec( fgcp2 );
957 FreeFloatVec( ogcp1g );
958 FreeFloatVec( ogcp2g );
959 FreeFloatVec( fgcp1g );
960 FreeFloatVec( fgcp2g );
963 FreeFloatMtx( cpmx1 );
964 FreeFloatMtx( cpmx2 );
966 FreeFloatMtx( floatwork );
967 FreeIntMtx( intwork );
970 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
971 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
974 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
977 w1 = AllocateFloatVec( ll2+2 );
978 w2 = AllocateFloatVec( ll2+2 );
979 match = AllocateFloatVec( ll2+2 );
981 initverticalw = AllocateFloatVec( ll1+2 );
982 lastverticalw = AllocateFloatVec( ll1+2 );
984 m = AllocateFloatVec( ll2+2 );
985 mp = AllocateIntVec( ll2+2 );
987 mseq = AllocateCharMtx( njob, ll1+ll2 );
989 diaf1 = AllocateFloatVec( ll1+2 );
990 diaf2 = AllocateFloatVec( ll2+2 );
991 gapf1 = AllocateFloatVec( ll1+2 );
992 gapf2 = AllocateFloatVec( ll2+2 );
993 ogcp1 = AllocateFloatVec( ll1+2 );
994 ogcp2 = AllocateFloatVec( ll2+2 );
995 fgcp1 = AllocateFloatVec( ll1+2 );
996 fgcp2 = AllocateFloatVec( ll2+2 );
997 ogcp1g = AllocateFloatVec( ll1+2 );
998 ogcp2g = AllocateFloatVec( ll2+2 );
999 fgcp1g = AllocateFloatVec( ll1+2 );
1000 fgcp2g = AllocateFloatVec( ll2+2 );
1002 cpmx1 = AllocateFloatMtx( 26, ll1+2 );
1003 cpmx2 = AllocateFloatMtx( 26, ll2+2 );
1006 floatwork = AllocateFloatMtx( MAX( ll1, ll2 )+2, 26 );
1007 intwork = AllocateIntMtx( MAX( ll1, ll2 )+2, 27 );
1009 floatwork = AllocateFloatMtx( 26, MAX( ll1, ll2 )+2 );
1010 intwork = AllocateIntMtx( 26, MAX( ll1, ll2 )+2 );
1014 fprintf( stderr, "succeeded\n" );
1017 orlgth1 = ll1 - 100;
1018 orlgth2 = ll2 - 100;
1022 for( i=0; i<icyc; i++ )
1027 for( j=0; j<jcyc; j++ )
1029 mseq2[j] = mseq[icyc+j];
1034 if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
1038 if( commonAlloc1 && commonAlloc2 )
1040 FreeShortMtx( commonIP );
1043 ll1 = MAX( orlgth1, commonAlloc1 );
1044 ll2 = MAX( orlgth2, commonAlloc2 );
1047 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
1050 commonIP = AllocateShortMtx( ll1+10, ll2+10 );
1053 fprintf( stderr, "succeeded\n\n" );
1064 for( i=0; i<icyc; i++ )
1066 fprintf( stderr, "## totaleff = %f\n", t );
1070 cpmx_calc_new( seq1, cpmx1, eff1, lgth1, icyc );
1071 cpmx_calc_new( seq2, cpmx2, eff2, lgth2, jcyc );
1075 new_OpeningGapCount( ogcp1g, icyc, seq1, eff1, lgth1, sgap1 );
1076 new_OpeningGapCount( ogcp2g, jcyc, seq2, eff2, lgth2, sgap2 );
1077 new_FinalGapCount( fgcp1g, icyc, seq1, eff1, lgth1, egap1 );
1078 new_FinalGapCount( fgcp2g, jcyc, seq2, eff2, lgth2, egap2 );
1079 getdiaminofreq_st( diaf1, icyc, seq1, eff1, lgth1 ); // atode
1080 getdiaminofreq_st( diaf2, jcyc, seq2, eff2, lgth2 ); // atode
1081 getgapfreq( gapf1, icyc, seq1, eff1, lgth1 ); // atode
1082 getgapfreq( gapf2, jcyc, seq2, eff2, lgth2 ); // atode
1086 st_OpeningGapCount( ogcp1g, icyc, seq1, eff1, lgth1 );
1087 st_OpeningGapCount( ogcp2g, jcyc, seq2, eff2, lgth2 );
1088 st_FinalGapCount( fgcp1g, icyc, seq1, eff1, lgth1 );
1089 st_FinalGapCount( fgcp2g, jcyc, seq2, eff2, lgth2 );
1090 getdiaminofreq_st( diaf1, icyc, seq1, eff1, lgth1 ); // atode
1091 getdiaminofreq_st( diaf2, jcyc, seq2, eff2, lgth2 ); // atode
1092 getgapfreq( gapf1, icyc, seq1, eff1, lgth1 );
1093 getgapfreq( gapf2, jcyc, seq2, eff2, lgth2 );
1096 for( i=0; i<lgth1; i++ )
1098 ogcp1[i] = 0.5 * ( 1.0 - ogcp1g[i] ) * fpenalty;
1099 fgcp1[i] = 0.5 * ( 1.0 - fgcp1g[i] ) * fpenalty;
1101 for( i=0; i<lgth2; i++ )
1103 ogcp2[i] = 0.5 * ( 1.0 - ogcp2g[i] ) * fpenalty;
1104 fgcp2[i] = 0.5 * ( 1.0 - fgcp2g[i] ) * fpenalty;
1107 for( i=0; i<lgth1; i++ )
1108 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
1114 match_calc( initverticalw, cpmx2, cpmx1, 0, lgth1, floatwork, intwork, 1 );
1116 imp_match_out_vead_tate( initverticalw, 0, lgth1 ); // 060306
1118 match_calc( currentw, cpmx1, cpmx2, 0, lgth2, floatwork, intwork, 1 );
1120 imp_match_out_vead( currentw, 0, lgth2 ); // 060306
1121 #if 0 // -> tbfast.c
1123 imp_match_calc( currentw, icyc, jcyc, lgth1, lgth2, seq1, seq2, eff1, eff2, localhom, 1, 0 );
1129 g = ogcp1g[0] * ( diaf2[0] ) * fpenalty * 0.5;
1130 // if( g ) fprintf( stderr, "match penal1=%f, %c-%c\n", g, seq1[0][i], seq2[0][j] );
1131 initverticalw[0] += g;
1134 g = ogcp2g[0] * ( diaf1[0] ) * fpenalty * 0.5;
1135 // if( g ) fprintf( stderr, "match penal2=%f, %c-%c\n", g, seq1[0][i], seq2[0][j] );
1136 initverticalw[0] += g;
1140 // g = fgcp1g[-1] * ( diaf2[0] ) * fpenalty * 0.5;
1141 // if( g ) fprintf( stderr, "match penal1=%f, %c-%c\n", g, seq1[0][i], seq2[0][j] );
1142 initverticalw[0] += g;
1146 // g = fgcp2g[-1] * ( diaf1[0] ) * fpenalty * 0.5;
1147 // if( g ) fprintf( stderr, "match penal2=%f, %c-%c\n", g, seq1[0][i], seq2[0][j] );
1148 initverticalw[0] += g;
1151 for( i=1; i<lgth1+1; i++ )
1153 // initverticalw[i] += ( ogcp1[0] + fgcp1[i-1] ) ;
1154 tmppenal = 0.5 * fpenalty;
1155 tmppenal -= ( (1.0-0.0) * (1.0-diaf1[0]) + 0.0 ) * 0.5 * fpenalty; // 0.
1156 // tmppenal -= ( (1.0-gapf2[j-1]) * ogcp1g[i] + gapf2[j-1] ) * 0.5 * fpenalty;
1157 // fprintf( stderr, "0,0<-%d,%d, tmppenal 1 = %f\n", i, 0, tmppenal );
1158 initverticalw[i] += tmppenal;
1159 tmppenal = 0.5 * fpenalty;
1160 tmppenal -= ( (1.0-gapf2[0]) * (1.0-diaf1[i]) + gapf2[0] ) * 0.5 * fpenalty;
1161 // tmppenal -= ( (1.0-gapf2[j]) * fgcp1g[i-1] + gapf2[j] ) * 0.5 * fpenalty;
1162 // fprintf( stderr, "0,0<-%d,%d, tmppenal 2 = %f\n", i, 0, tmppenal );
1163 initverticalw[i] += tmppenal;
1166 for( j=1; j<lgth2+1; j++ )
1168 // currentw[j] += ( ogcp2[0] + fgcp2[j-1] ) ;
1169 tmppenal = 0.5 * fpenalty;
1170 tmppenal -= ( (1.0-0.0) * (1.0-diaf2[0]) + 0.0 ) * 0.5 * fpenalty; // 0.
1171 // tmppenal -= ( (1.0-gapf1[0]) * fgcp2g[j-1] + gapf1[0] ) * 0.5 * fpenalty;
1172 // fprintf( stderr, "0,0<-%d,%d, tmppenal 3 = %f\n", 0, j, tmppenal );
1173 currentw[j] += tmppenal;
1174 tmppenal = 0.5 * fpenalty;
1175 tmppenal -= ( (1.0-gapf1[0]) * (1.0-diaf2[j]) + gapf1[0] ) * 0.5 * fpenalty;
1176 // tmppenal -= ( (1.0-gapf1[0]) * ogcp2g[j] + gapf1[i-1] ) * 0.5 * fpenalty;
1177 // fprintf( stderr, "0,0<-%d,%d, tmppenal 4 = %f\n", 0, j, tmppenal );
1178 currentw[j] += tmppenal;
1184 for( j=1; j<lgth2+1; j++ )
1185 currentw[j] -= offset * j / 2.0;
1186 for( i=1; i<lgth1+1; i++ )
1187 initverticalw[i] -= offset * i / 2.0;
1191 for( j=1; j<lgth2+1; ++j )
1193 // m[j] = currentw[j-1] + ogcp1[1];
1196 tmppenal = 0.5 * fpenalty;
1197 tmppenal -= ( (1.0-0.0) * (1.0-0.0) + 0.0 ) * 0.5 * fpenalty;
1198 // tmppenal -= ( (1.0-gapf2[-1]) * (1.0-diaf1[0]) + gapf2[-1] ) * 0.5 * fpenalty;
1199 // m[j] = currentw[j-1] + tmppenal;
1200 m[j] = currentw[j-1] + ogcp1[1];
1203 lastverticalw[0] = 0.0; // Falign kara yobaretatoki kounarukanousei ari
1205 lastverticalw[0] = currentw[lgth2-1];
1207 if( outgap ) lasti = lgth1+1; else lasti = lgth1;
1210 fprintf( stderr, "currentw = \n" );
1211 for( i=0; i<lgth1+1; i++ )
1213 fprintf( stderr, "%5.2f ", currentw[i] );
1215 fprintf( stderr, "\n" );
1216 fprintf( stderr, "initverticalw = \n" );
1217 for( i=0; i<lgth2+1; i++ )
1219 fprintf( stderr, "%5.2f ", initverticalw[i] );
1221 fprintf( stderr, "\n" );
1222 fprintf( stderr, "fcgp\n" );
1223 for( i=0; i<lgth1; i++ )
1224 fprintf( stderr, "fgcp1[%d]=%f\n", i, ogcp1[i] );
1225 for( i=0; i<lgth2; i++ )
1226 fprintf( stderr, "fgcp2[%d]=%f\n", i, ogcp2[i] );
1229 for( i=1; i<lasti; i++ )
1232 previousw = currentw;
1235 previousw[0] = initverticalw[i-1];
1237 match_calc( currentw, cpmx1, cpmx2, i, lgth2, floatwork, intwork, 0 );
1239 fprintf( stderr, "\n" );
1240 fprintf( stderr, "i=%d\n", i );
1241 fprintf( stderr, "currentw = \n" );
1242 for( j=0; j<lgth2; j++ )
1244 fprintf( stderr, "%5.2f ", currentw[j] );
1246 fprintf( stderr, "\n" );
1250 // fprintf( stderr, "Calling imp_match_calc (o) lgth = %d, i = %d\n", lgth1, i );
1252 imp_match_out_vead( currentw, i, lgth2 );
1254 imp_match_out_vead( currentw, i, lgth2 );
1258 fprintf( stderr, "\n" );
1259 fprintf( stderr, "i=%d\n", i );
1260 fprintf( stderr, "currentw = \n" );
1261 for( j=0; j<lgth2; j++ )
1263 fprintf( stderr, "%5.2f ", currentw[j] );
1265 fprintf( stderr, "\n" );
1267 currentw[0] = initverticalw[i];
1270 mi = previousw[0] + ogcp2[1]; mpi = 0;
1274 curpt = currentw + 1;
1277 ogcp2pt = ogcp2 + 1;
1278 fgcp1va = fgcp1[i-1];
1281 for( j=1; j<lastj; j++ )
1285 g = ogcp1g[i] * ( diaf2[j] ) * fpenalty * 0.5;
1286 // if( g ) fprintf( stderr, "match penal1=%f, %c-%c\n", g, seq1[0][i], seq2[0][j] );
1289 g = ogcp2g[j] * ( diaf1[i] ) * fpenalty * 0.5;
1290 // if( g ) fprintf( stderr, "match penal2=%f, %c-%c\n", g, seq1[0][i], seq2[0][j] );
1293 g = fgcp1g[i-1] * ( diaf2[j] ) * fpenalty * 0.5;
1294 // if( g ) fprintf( stderr, "match penal3=%f, %c-%c\n", g, seq1[0][i], seq2[0][j] );
1297 g = fgcp2g[j-1] * ( diaf1[i] ) * fpenalty * 0.5;
1298 // if( g ) fprintf( stderr, "match penal4=%f, %c-%c\n", g, seq1[0][i], seq2[0][j] );
1304 fprintf( stderr, "%5.0f->", wm );
1307 fprintf( stderr, "%5.0f?", g );
1309 tmppenal = 0.5 * fpenalty;
1310 tmppenal -= ( (1.0-gapf1[i]) * (1.0-diaf2[j]) + gapf1[i] ) * 0.5 * fpenalty;
1311 // tmppenal -= ( (1.0-gapf1[i]) * fgcp2g[j-1] + gapf1[i] ) * 0.5 * fpenalty;
1312 // tmppenal = *fgcp2pt-fpenalty*0.5*(gapf2[j]+gapf1[i]);
1313 // tmppenal = *fgcp2pt-fpenalty*0.5*gapf1[i];
1314 // tmppenal = *fgcp2pt;
1315 if( (g=mi+tmppenal) > wm )
1317 // fprintf( stderr, "jump i start=%f, %c-%c\n", g-mi, seq1[0][i], seq2[0][j] );
1319 *ijppt = -( j - mpi );
1321 // tmppenal = *ogcp2pt-fpenalty*0.5*(gapf2[j-1]+gapf1[i-1]);
1322 tmppenal = 0.5 * fpenalty;
1323 tmppenal -= ( (1.0-gapf1[i-1]) * (1.0-diaf2[j]) + gapf1[i-1] ) * 0.5 * fpenalty;
1324 // tmppenal -= ( (1.0-gapf1[i-1]) * ogcp2g[j] + gapf1[i-1] ) * 0.5 * fpenalty;
1325 // tmppenal = *ogcp2pt-fpenalty*0.5*gapf1[i-1];
1326 // tmppenal = *prept+*ogcp2pt;
1327 if( (g=*prept+tmppenal) >= mi )
1329 // fprintf( stderr, "jump i end=%f, %c-%c\n", g-*prept, seq1[0][i-1], seq2[0][j-1] );
1338 fprintf( stderr, "%5.0f?", g );
1340 tmppenal = 0.5 * fpenalty;
1341 // tmppenal -= ( (1.0-gapf2[j]) * fgcp1g[i-1] + gapf2[j] ) * 0.5 * fpenalty;
1342 tmppenal -= ( (1.0-gapf2[j]) * (1.0-diaf1[i]) + gapf2[j] ) * 0.5 * fpenalty;
1343 // tmppenal = fgcp1va-fpenalty*0.5*(gapf1[i]+gapf2[j]);
1344 // tmppenal = fgcp1va-fpenalty*0.5*gapf2[j];
1345 // tmppenal = fgcp1va;
1346 if( (g=*mjpt+tmppenal) > wm )
1348 // fprintf( stderr, "jump j start=%f, %c-%c\n", g-*mjpt, seq1[0][i], seq2[0][j] );
1350 *ijppt = +( i - *mpjpt );
1352 // tmppenal = ogcp1va-fpenalty*0.5*(gapf1[i-1]+gapf2[j-1]);
1353 tmppenal = 0.5 * fpenalty;
1354 // tmppenal -= ( (1.0-gapf2[j-1]) * ogcp1g[i] + gapf2[j-1] ) * 0.5 * fpenalty;
1355 tmppenal -= ( (1.0-gapf2[j-1]) * (1.0-diaf1[i]) + gapf2[j-1] ) * 0.5 * fpenalty;
1356 // tmppenal = 0.5 * fpenalty - ( (1.0-gapf2[j-1]) * (ogcp1g[i]) + gapf2[j-1] ) * ( 0.5 * fpenalty );
1357 // tmppenal = ogcp1va-fpenalty*0.5*gapf2[j-1];
1358 // tmppenal = ogcp1va;
1359 if( (g=*prept+tmppenal) >= *mjpt )
1361 // fprintf( stderr, "jump j end=%f, %c-%c\n", g-*prept, seq1[0][i-1], seq2[0][j-1] );
1366 m[j] += fpenalty_ex;
1370 fprintf( stderr, "%5.0f ", wm );
1380 lastverticalw[i] = currentw[lgth2-1];
1383 // fprintf( stderr, "wm = %f\n", wm );
1388 for( j=1; j<lgth2+1; j++ )
1389 currentw[j] -= offset * ( lgth2 - j ) / 2.0;
1390 for( i=1; i<lgth1+1; i++ )
1391 lastverticalw[i] -= offset * ( lgth1 - i / 2.0);
1396 fprintf( stderr, "\n" );
1397 for( i=0; i<icyc; i++ ) fprintf( stderr,"%s\n", seq1[i] );
1398 fprintf( stderr, "#####\n" );
1399 for( j=0; j<jcyc; j++ ) fprintf( stderr,"%s\n", seq2[j] );
1400 fprintf( stderr, "====>" );
1401 for( i=0; i<icyc; i++ ) strcpy( mseq1[i], seq1[i] );
1402 for( j=0; j<jcyc; j++ ) strcpy( mseq2[j], seq2[j] );
1406 Atracking_localhom( impmatch, currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
1409 Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, cpmx1, cpmx2, ijp, icyc, jcyc );
1411 // fprintf( stderr, "### impmatch = %f\n", *impmatch );
1413 resultlen = strlen( mseq1[0] );
1414 if( alloclen < resultlen || resultlen > N )
1416 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
1417 ErrorExit( "LENGTH OVER!\n" );
1421 for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
1422 for( j=0; j<jcyc; j++ ) strcpy( seq2[j], mseq2[j] );
1424 fprintf( stderr, "\n" );
1425 for( i=0; i<icyc; i++ ) fprintf( stderr, "%s\n", mseq1[i] );
1426 fprintf( stderr, "#####\n" );
1427 for( j=0; j<jcyc; j++ ) fprintf( stderr, "%s\n", mseq2[j] );
1430 fprintf( stderr, "wm = %f\n", wm );