8 #define USE_PENALTY_EX 0
9 #define FASTMATCHCALC 1
12 static void st_OpeningGapCount( double *ogcp, int clus, char **seq, double *eff, int len )
17 for( i=0; i<len; i++ ) ogcp[i] = 0.0;
18 for( j=0; j<clus; j++ )
20 feff = (double)eff[j];
22 for( i=0; i<len; i++ )
25 gc = ( seq[j][i] == '-' );
27 if( !gb * gc ) ogcp[i] += feff;
33 static void st_FinalGapCount( double *fgcp, int clus, char **seq, double *eff, int len )
38 for( i=0; i<len; i++ ) fgcp[i] = 0.0;
39 for( j=0; j<clus; j++ )
41 feff = (double)eff[j];
42 gc = ( seq[j][0] == '-' );
43 for( i=1; i<len+1; i++ )
46 gc = ( seq[j][i] == '-' );
48 if( gb * !gc ) fgcp[i-1] += feff;
59 static TLS int impalloclen = 0;
60 static TLS double **impmtx = NULL;
61 double part_imp_match_out_sc( int i1, int j1 )
63 // fprintf( stderr, "impalloclen = %d\n", impalloclen );
64 // fprintf( stderr, "i1,j1=%d,%d -> impmtx=%f\n", i1, j1, impmtx[i1][j1] );
65 return( impmtx[i1][j1] );
67 if( i1 == l1 || j1 == l2 ) return( 0.0 );
68 return( impmtx[i1+start1][j1+start2] );
71 static void part_imp_match_out_vead_gapmap( double *imp, int i1, int lgth2, int start2, int *gapmap2 )
75 int *gapmappt = gapmap2;
77 *pt++ += impmtx[i1][start2+*gapmappt++];
80 for( j=0; j<lgth2; j++ )
82 imp[j] += impmtx[i1][start2+gapmap2[j]];
87 static void part_imp_match_out_vead_tate_gapmap( double *imp, int j1, int lgth1, int start1, int *gapmap1 )
91 int *gapmappt = gapmap1;
93 *pt++ = impmtx[start1+*gapmappt++][j1];
96 for( i=0; i<lgth1; i++ )
98 imp[i] += impmtx[start1+gapmap1[i]][j1];
104 void part_imp_match_init_strict( double *imp, int clus1, int clus2, int lgth1, int lgth2, char **seq1, char **seq2, double *eff1, double *eff2, double *eff1_kozo, double *eff2_kozo, LocalHom ***localhom, char *swaplist, int forscore, int *orinum1, int *orinum2 )
106 // int i, j, k1, k2, tmpint, start1, start2, end1, end2;
108 // double effij_kozo;
110 // char *pt, *pt1, *pt2;
111 // static TLS char *nocount1 = NULL;
112 // static TLS char *nocount2 = NULL;
117 if( impmtx ) FreeFloatMtx( impmtx );
119 // if( nocount1 ) free( nocount1 );
121 // if( nocount2 ) free( nocount2 );
127 if( impalloclen < lgth1 + 2 || impalloclen < lgth2 + 2 )
129 if( impmtx ) FreeFloatMtx( impmtx );
130 // if( nocount1 ) free( nocount1 );
131 // if( nocount2 ) free( nocount2 );
132 impalloclen = MAX( lgth1, lgth2 ) + 2;
133 impmtx = AllocateFloatMtx( impalloclen, impalloclen );
134 // nocount1 = AllocateCharVec( impalloclen );
135 // nocount2 = AllocateCharVec( impalloclen );
138 fillimp( impmtx, imp, clus1, clus2, lgth1, lgth2, seq1, seq2, eff1, eff2, eff1_kozo, eff2_kozo, localhom, swaplist, forscore, orinum1, orinum2 );
144 void part_imp_rna( int nseq1, int nseq2, char **seq1, char **seq2, double *eff1, double *eff2, RNApair ***grouprna1, RNApair ***grouprna2, int *gapmap1, int *gapmap2, RNApair *additionalpair )
146 foldrna( nseq1, nseq2, seq1, seq2, eff1, eff2, grouprna1, grouprna2, impmtx, gapmap1, gapmap2, additionalpair );
152 static void match_calc( double *match, double **cpmx1, double **cpmx2, int i1, int lgth2, double **doublework, int **intwork, int initialize )
157 double **cpmxpd = doublework;
158 int **cpmxpdn = intwork;
159 double *matchpt, *cpmxpdpt, **cpmxpdptpt;
160 int *cpmxpdnpt, **cpmxpdnptpt;
162 scarr = calloc( nalphabets, sizeof( double ) );
166 for( j=0; j<lgth2; j++ )
169 for( l=0; l<nalphabets; l++ )
173 cpmxpd[j][count] = cpmx2[l][j];
174 cpmxpdn[j][count] = l;
178 cpmxpdn[j][count] = -1;
183 for( l=0; l<nalphabets; l++ )
186 for( j=0; j<nalphabets; j++ )
187 scarr[l] += n_dis_consweight_multi[j][l] * cpmx1[j][i1];
188 // scarr[l] += n_dis[j][l] * cpmx1[j][i1];
191 cpmxpdnptpt = cpmxpdn;
196 cpmxpdnpt = *cpmxpdnptpt++;
197 cpmxpdpt = *cpmxpdptpt++;
198 while( *cpmxpdnpt>-1 )
199 *matchpt += scarr[*cpmxpdnpt++] * *cpmxpdpt++;
207 double **cpmxpd = doublework;
208 int **cpmxpdn = intwork;
210 scarr = calloc( nalphabets, sizeof( double ) );
215 for( j=0; j<lgth2; j++ )
218 for( l=0; l<nalphabets; l++ )
222 cpmxpd[count][j] = cpmx2[l][j];
223 cpmxpdn[count][j] = l;
227 cpmxpdn[count][j] = -1;
230 for( l=0; l<nalphabets; l++ )
233 for( k=0; k<nalphabets; k++ )
234 scarr[l] += n_dis_consweight_multi[k][l] * cpmx1[k][i1];
235 // scarr[l] += n_dis[k][l] * cpmx1[k][i1];
237 for( j=0; j<lgth2; j++ )
240 for( k=0; cpmxpdn[k][j]>-1; k++ )
241 match[j] += scarr[cpmxpdn[k][j]] * cpmxpd[k][j];
247 static void fillzero( double *s, int l )
249 while( l-- ) *s++ = 0.0;
253 static void match_calc_del( int **which, double ***matrices, double *match, int n1, char **seq1, double *eff1, int n2, char **seq2, double *eff2, int i1, int lgth2, int mid, int nmask, int *mask1, int *mask2 )
258 // fprintf( stderr, "\nmatch_calc_dynamicmtx... %d", i1 );
259 // fprintf( stderr, "\nseq1[0]=%s\n", seq1[0] );
260 // fprintf( stderr, "\nseq2[0]=%s\n", seq2[0] );
261 // for( i=0; i<n1; i++ ) for( j=0; j<n2; j++ )
263 // if( flip ) reporterr( "in match_calc_slow, which[%d][%d] = %d\n", j, i, which[j][i] );
264 // else reporterr( "in match_calc_slow, which[%d][%d] = %d\n", i, j, which[i][j] );
266 for( k=0; k<lgth2; k++ )
268 for( m=0; m<nmask; m++ )
272 // reporterr( "Deleting %d-%d (c=%d)\n", i, j, mid );
273 // if( k==0 ) fprintf( stderr, "pairoffset[%d][%d] = %f\n", i, j, po );
274 c1 = amino_n[(int)seq1[i][i1]];
275 c2 = amino_n[(int)seq2[j][k]];
276 // reporterr( "k=%d, c1=%d, c2=%d, seq1[i][i1]=%c, seq2[%d][%d]=%c\n", k, c1, c2, seq1[i][i1], j, k, seq2[j][k] );
277 if( seq1[i][i1] == '-' || seq2[j][k] == '-' ) continue;
278 if( c1 < 0 || c2 < 0 ) continue;
279 // fprintf( stderr, "c1=%d, c2=%d\n", c1, c2 );
280 // fprintf( stderr, "match[k] = %f -> ", match[k], mid );
281 match[k] -= matrices[mid][c1][c2] * eff1[i] * eff2[j];
282 // fprintf( stderr, "match[k] = %f (mid=%d)\n", match[k], mid );
285 // fprintf( stderr, "done\n" );
289 static void match_calc_add( double **scoreingmtx, double *match, double **cpmx1, double **cpmx2, int i1, int lgth2, double **doublework, int **intwork, int initialize )
292 // fprintf( stderr, "\nmatch_calc... %d", i1 );
295 double **cpmxpd = doublework;
296 int **cpmxpdn = intwork;
297 double *matchpt, *cpmxpdpt, **cpmxpdptpt;
298 int *cpmxpdnpt, **cpmxpdnptpt;
300 scarr = calloc( nalphabets, sizeof( double ) );
304 for( j=0; j<lgth2; j++ )
307 for( l=0; l<nalphabets; l++ )
311 cpmxpd[j][count] = cpmx2[l][j];
312 cpmxpdn[j][count] = l;
316 cpmxpdn[j][count] = -1;
321 for( l=0; l<nalphabets; l++ )
324 for( j=0; j<nalphabets; j++ )
325 // scarr[l] += n_dis[j][l] * cpmx1[j][i1];
326 // scarr[l] += n_dis_consweight_multi[j][l] * cpmx1[j][i1];
327 scarr[l] += scoreingmtx[j][l] * cpmx1[j][i1];
330 cpmxpdnptpt = cpmxpdn;
335 cpmxpdnpt = *cpmxpdnptpt++;
336 cpmxpdpt = *cpmxpdptpt++;
337 while( *cpmxpdnpt>-1 )
338 *matchpt += scarr[*cpmxpdnpt++] * *cpmxpdpt++;
343 // fprintf( stderr, "done\n" );
347 double **cpmxpd = doublework;
348 int **cpmxpdn = intwork;
350 scarr = calloc( nalphabets, sizeof( double ) );
355 for( j=0; j<lgth2; j++ )
358 for( l=0; l<nalphabets; l++ )
362 cpmxpd[count][j] = cpmx2[l][j];
363 cpmxpdn[count][j] = l;
367 cpmxpdn[count][j] = -1;
370 for( l=0; l<nalphabets; l++ )
373 for( k=0; k<nalphabets; k++ )
374 // scarr[l] += n_dis[k][l] * cpmx1[k][i1];
375 // scarr[l] += n_dis_consweight_multi[k][l] * cpmx1[k][i1];
376 scarr[l] += scoreingmtx[k][l] * cpmx1[k][i1];
378 for( j=0; j<lgth2; j++ )
381 for( k=0; cpmxpdn[k][j]>-1; k++ )
382 match[j] += scarr[cpmxpdn[k][j]] * cpmxpd[k][j];
388 static void Atracking_localhom( double *impwmpt, double *lasthorizontalw, double *lastverticalw,
389 char **seq1, char **seq2,
390 char **mseq1, char **mseq2,
391 int **ijp, int icyc, int jcyc,
392 int start1, int end1, int start2, int end2,
393 int *gapmap1, int *gapmap2,
394 int *warpis, int *warpjs, int warpbase )
396 int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k, limk;
401 lgth1 = strlen( seq1[0] );
402 lgth2 = strlen( seq2[0] );
405 for( i=0; i<lgth1; i++ )
407 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
415 wm = lastverticalw[0];
416 for( i=0; i<lgth1; i++ )
418 if( lastverticalw[i] >= wm )
420 wm = lastverticalw[i];
421 iin = i; jin = lgth2-1;
422 ijp[lgth1][lgth2] = +( lgth1 - i );
425 for( j=0; j<lgth2; j++ )
427 if( lasthorizontalw[j] >= wm )
429 wm = lasthorizontalw[j];
430 iin = lgth1-1; jin = j;
431 ijp[lgth1][lgth2] = -( lgth2 - j );
436 for( i=0; i<lgth1+1; i++ )
440 for( j=0; j<lgth2+1; j++ )
442 ijp[0][j] = -( j + 1 );
445 for( i=0; i<icyc; i++ )
447 mseq1[i] += lgth1+lgth2;
450 for( j=0; j<jcyc; j++ )
452 mseq2[j] += lgth1+lgth2;
455 iin = lgth1; jin = lgth2;
457 limk = lgth1+lgth2+1;
458 for( k=0; k<limk; k++ )
461 if( ijp[iin][jin] >= warpbase )
463 ifi = warpis[ijp[iin][jin]-warpbase];
464 jfi = warpjs[ijp[iin][jin]-warpbase];
466 else if( ijp[iin][jin] < 0 )
468 ifi = iin-1; jfi = jin+ijp[iin][jin];
470 else if( ijp[iin][jin] > 0 )
472 ifi = iin-ijp[iin][jin]; jfi = jin-1;
476 ifi = iin-1; jfi = jin-1;
478 if( ifi == -warpbase && jfi == -warpbase )
483 for( i=0; i<icyc; i++ )
484 *--mseq1[i] = seq1[i][l];
485 for( j=0; j<jcyc; j++ )
492 for( i=0; i<icyc; i++ )
494 for( j=0; j<jcyc; j++ )
495 *--mseq2[j] = seq2[j][l];
505 for( i=0; i<icyc; i++ )
506 *--mseq1[i] = seq1[i][ifi+l];
507 for( j=0; j<jcyc; j++ )
514 for( i=0; i<icyc; i++ )
516 for( j=0; j<jcyc; j++ )
517 *--mseq2[j] = seq2[j][jfi+l];
521 if( iin != lgth1 && jin != lgth2 ) // ??
523 *impwmpt += (double)part_imp_match_out_sc( gapmap1[iin]+start1, gapmap2[jin]+start2 );
524 // fprintf( stderr, "impwm = %f (iin=%d, jin=%d) seq1=%c, seq2=%c\n", *impwmpt, iin, jin, seq1[0][iin], seq2[0][jin] );
526 if( iin <= 0 || jin <= 0 ) break;
527 for( i=0; i<icyc; i++ )
528 *--mseq1[i] = seq1[i][ifi];
529 for( j=0; j<jcyc; j++ )
530 *--mseq2[j] = seq2[j][jfi];
532 iin = ifi; jin = jfi;
536 static double Atracking( double *lasthorizontalw, double *lastverticalw,
537 char **seq1, char **seq2,
538 char **mseq1, char **mseq2,
539 int **ijp, int icyc, int jcyc,
540 int *warpis, int *warpjs, int warpbase )
542 int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k, lastk, limk;
547 lgth1 = strlen( seq1[0] );
548 lgth2 = strlen( seq2[0] );
551 for( i=0; i<lgth1; i++ )
553 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
561 wm = lastverticalw[0];
562 for( i=0; i<lgth1; i++ )
564 if( lastverticalw[i] >= wm )
566 wm = lastverticalw[i];
567 iin = i; jin = lgth2-1;
568 ijp[lgth1][lgth2] = +( lgth1 - i );
571 for( j=0; j<lgth2; j++ )
573 if( lasthorizontalw[j] >= wm )
575 wm = lasthorizontalw[j];
576 iin = lgth1-1; jin = j;
577 ijp[lgth1][lgth2] = -( lgth2 - j );
582 for( i=0; i<lgth1+1; i++ )
586 for( j=0; j<lgth2+1; j++ )
588 ijp[0][j] = -( j + 1 );
591 for( i=0; i<icyc; i++ )
593 mseq1[i] += lgth1+lgth2;
596 for( j=0; j<jcyc; j++ )
598 mseq2[j] += lgth1+lgth2;
601 iin = lgth1; jin = lgth2;
603 limk = lgth1+lgth2+1;
604 for( k=0; k<limk; k++ )
606 if( ijp[iin][jin] >= warpbase )
608 ifi = warpis[ijp[iin][jin]-warpbase];
609 jfi = warpjs[ijp[iin][jin]-warpbase];
611 else if( ijp[iin][jin] < 0 )
613 ifi = iin-1; jfi = jin+ijp[iin][jin];
615 else if( ijp[iin][jin] > 0 )
617 ifi = iin-ijp[iin][jin]; jfi = jin-1;
621 ifi = iin-1; jfi = jin-1;
623 if( ifi == -warpbase && jfi == -warpbase )
628 for( i=0; i<icyc; i++ )
629 *--mseq1[i] = seq1[i][l];
630 for( j=0; j<jcyc; j++ )
637 for( i=0; i<icyc; i++ )
639 for( j=0; j<jcyc; j++ )
640 *--mseq2[j] = seq2[j][l];
650 for( i=0; i<icyc; i++ )
651 *--mseq1[i] = seq1[i][ifi+l];
652 for( j=0; j<jcyc; j++ )
659 for( i=0; i<icyc; i++ )
661 for( j=0; j<jcyc; j++ )
662 *--mseq2[j] = seq2[j][jfi+l];
667 if( iin <= 0 || jin <= 0 ) break;
668 for( i=0; i<icyc; i++ )
669 *--mseq1[i] = seq1[i][ifi];
670 for( j=0; j<jcyc; j++ )
671 *--mseq2[j] = seq2[j][jfi];
673 iin = ifi; jin = jfi;
678 double partA__align( char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen, LocalHom ***localhom, double *impmatch, int start1, int end1, int start2, int end2, int *gapmap1, int *gapmap2, char *sgap1, char *sgap2, char *egap1, char *egap2, int *chudanpt, int chudanref, int *chudanres )
679 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
683 int lasti, lastj; /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
686 double wm = 0.0; /* int ?????? */
688 double *currentw, *previousw;
692 double *mjpt, *prept, *curpt;
695 static TLS double mi, *m;
696 static TLS int **ijp;
697 static TLS int mpi, *mp;
698 static TLS double *w1, *w2;
699 static TLS double *match;
700 static TLS double *initverticalw; /* kufuu sureba iranai */
701 static TLS double *lastverticalw; /* kufuu sureba iranai */
702 static TLS char **mseq1;
703 static TLS char **mseq2;
704 static TLS char **mseq;
705 static TLS double *ogcp1;
706 static TLS double *ogcp2;
707 static TLS double *fgcp1;
708 static TLS double *fgcp2;
709 static TLS double **cpmx1;
710 static TLS double **cpmx2;
711 static TLS double *gapfreq1;
712 static TLS double *gapfreq2;
713 static TLS int **intwork;
714 static TLS double **doublework;
715 static TLS int orlgth1 = 0, orlgth2 = 0;
716 double fpenalty = (double)penalty;
717 double fpenalty_shift = (double)penalty_shift;
719 double fpenalty_ex = (double)penalty_ex;
736 int *prevwarpi = NULL;
737 int *prevwarpj = NULL;
738 double *wmrecords = NULL;
739 double *prevwmrecords = NULL;
743 double *wmrecordspt, *wmrecords1pt, *prevwmrecordspt;
744 int *warpipt, *warpjpt;
751 // fprintf( stderr, "## Freeing local arrays in A__align\n" );
755 part_imp_match_init_strict( NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL );
761 FreeFloatVec( match );
762 FreeFloatVec( initverticalw );
763 FreeFloatVec( lastverticalw );
770 FreeFloatVec( ogcp1 );
771 FreeFloatVec( ogcp2 );
772 FreeFloatVec( fgcp1 );
773 FreeFloatVec( fgcp2 );
776 FreeFloatMtx( cpmx1 );
777 FreeFloatMtx( cpmx2 );
779 FreeFloatVec( gapfreq1 );
780 FreeFloatVec( gapfreq2 );
782 FreeFloatMtx( doublework );
783 FreeIntMtx( intwork );
788 // fprintf( stderr, "## Not allocated\n" );
792 // fprintf( stderr, "IN partA__align\n" );
795 lgth1 = strlen( seq1[0] );
796 lgth2 = strlen( seq2[0] );
798 // if( lgth1 == 0 ) fprintf( stderr, "WARNING: lgth1=0 in partA__align\n" );
799 // if( lgth2 == 0 ) fprintf( stderr, "WARNING: lgth2=0 in partA__align\n" );
801 if( lgth1 == 0 && lgth2 == 0 )
806 for( i=0; i<icyc; i++ )
810 while( j ) seq1[i][--j] = *newgapstr;
811 // fprintf( stderr, "seq1[i] = %s\n", seq1[i] );
818 for( i=0; i<jcyc; i++ )
822 while( j ) seq2[i][--j] = *newgapstr;
823 // fprintf( stderr, "seq2[i] = %s\n", seq2[i] );
829 warpbase = lgth1 + lgth2;
841 fprintf( stderr, "At present, outgap must be 1.\n" );
844 wmrecords = AllocateFloatVec( lgth2+1 );
845 warpi = AllocateIntVec( lgth2+1 );
846 warpj = AllocateIntVec( lgth2+1 );
847 prevwmrecords = AllocateFloatVec( lgth2+1 );
848 prevwarpi = AllocateIntVec( lgth2+1 );
849 prevwarpj = AllocateIntVec( lgth2+1 );
850 for( i=0; i<lgth2+1; i++ ) wmrecords[i] = 0.0;
851 for( i=0; i<lgth2+1; i++ ) prevwmrecords[i] = 0.0;
852 for( i=0; i<lgth2+1; i++ ) prevwarpi[i] = -warpbase;
853 for( i=0; i<lgth2+1; i++ ) prevwarpj[i] = -warpbase;
854 for( i=0; i<lgth2+1; i++ ) warpi[i] = -warpbase;
855 for( i=0; i<lgth2+1; i++ ) warpj[i] = -warpbase;
859 fprintf( stderr, "eff in SA+++align\n" );
860 for( i=0; i<icyc; i++ ) fprintf( stderr, "eff1[%d] = %f\n", i, eff1[i] );
864 mseq1 = AllocateCharMtx( njob, 0 );
865 mseq2 = AllocateCharMtx( njob, 0 );
871 if( lgth1 > orlgth1 || lgth2 > orlgth2 )
875 if( orlgth1 > 0 && orlgth2 > 0 )
879 FreeFloatVec( match );
880 FreeFloatVec( initverticalw );
881 FreeFloatVec( lastverticalw );
888 FreeFloatVec( ogcp1 );
889 FreeFloatVec( ogcp2 );
890 FreeFloatVec( fgcp1 );
891 FreeFloatVec( fgcp2 );
894 FreeFloatMtx( cpmx1 );
895 FreeFloatMtx( cpmx2 );
897 FreeFloatVec( gapfreq1 );
898 FreeFloatVec( gapfreq2 );
900 FreeFloatMtx( doublework );
901 FreeIntMtx( intwork );
904 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
905 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
908 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
911 w1 = AllocateFloatVec( ll2+2 );
912 w2 = AllocateFloatVec( ll2+2 );
913 match = AllocateFloatVec( ll2+2 );
915 initverticalw = AllocateFloatVec( ll1+2 );
916 lastverticalw = AllocateFloatVec( ll1+2 );
918 m = AllocateFloatVec( ll2+2 );
919 mp = AllocateIntVec( ll2+2 );
921 mseq = AllocateCharMtx( njob, ll1+ll2 );
923 ogcp1 = AllocateFloatVec( ll1+2 );
924 ogcp2 = AllocateFloatVec( ll2+2 );
925 fgcp1 = AllocateFloatVec( ll1+2 );
926 fgcp2 = AllocateFloatVec( ll2+2 );
928 cpmx1 = AllocateFloatMtx( nalphabets, ll1+2 );
929 cpmx2 = AllocateFloatMtx( nalphabets, ll2+2 );
931 gapfreq1 = AllocateFloatVec( ll1+2 );
932 gapfreq2 = AllocateFloatVec( ll2+2 );
935 doublework = AllocateFloatMtx( MAX( ll1, ll2 )+2, nalphabets );
936 intwork = AllocateIntMtx( MAX( ll1, ll2 )+2, nalphabets );
938 doublework = AllocateFloatMtx( nalphabets, MAX( ll1, ll2 )+2 );
939 intwork = AllocateIntMtx( nalphabets, MAX( ll1, ll2 )+2 );
943 fprintf( stderr, "succeeded\n" );
951 for( i=0; i<icyc; i++ ) mseq1[i] = mseq[i];
952 for( j=0; j<jcyc; j++ ) mseq2[j] = mseq[icyc+j];
955 if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
959 if( commonAlloc1 && commonAlloc2 )
961 FreeIntMtx( commonIP );
964 ll1 = MAX( orlgth1, commonAlloc1 );
965 ll2 = MAX( orlgth2, commonAlloc2 );
968 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
971 commonIP = AllocateIntMtx( ll1+10, ll2+10 );
974 fprintf( stderr, "succeeded\n\n" );
982 cpmx_calc_new( seq1, cpmx1, eff1, lgth1, icyc );
983 cpmx_calc_new( seq2, cpmx2, eff2, lgth2, jcyc );
987 new_OpeningGapCount( ogcp1, icyc, seq1, eff1, lgth1, sgap1 );
988 new_OpeningGapCount( ogcp2, jcyc, seq2, eff2, lgth2, sgap2 );
989 new_FinalGapCount( fgcp1, icyc, seq1, eff1, lgth1, egap1 );
990 new_FinalGapCount( fgcp2, jcyc, seq2, eff2, lgth2, egap2 );
991 outgapcount( &headgapfreq1, icyc, sgap1, eff1 );
992 outgapcount( &headgapfreq2, jcyc, sgap2, eff2 );
993 outgapcount( gapfreq1+lgth1, icyc, egap1, eff1 );
994 outgapcount( gapfreq2+lgth2, jcyc, egap2, eff2 );
998 st_OpeningGapCount( ogcp1, icyc, seq1, eff1, lgth1 );
999 st_OpeningGapCount( ogcp2, jcyc, seq2, eff2, lgth2 );
1000 st_FinalGapCount( fgcp1, icyc, seq1, eff1, lgth1 );
1001 st_FinalGapCount( fgcp2, jcyc, seq2, eff2, lgth2 );
1004 gapfreq1[lgth1] = 0.0;
1005 gapfreq2[lgth2] = 0.0;
1008 if( legacygapcost == 0 )
1010 gapcountf( gapfreq1, seq1, icyc, eff1, lgth1 );
1011 gapcountf( gapfreq2, seq2, jcyc, eff2, lgth2 );
1012 for( i=0; i<lgth1+1; i++ ) gapfreq1[i] = 1.0 - gapfreq1[i];
1013 for( i=0; i<lgth2+1; i++ ) gapfreq2[i] = 1.0 - gapfreq2[i];
1014 headgapfreq1 = 1.0 - headgapfreq1;
1015 headgapfreq2 = 1.0 - headgapfreq2;
1019 for( i=0; i<lgth1+1; i++ ) gapfreq1[i] = 1.0;
1020 for( i=0; i<lgth2+1; i++ ) gapfreq2[i] = 1.0;
1025 for( i=0; i<lgth1; i++ )
1027 ogcp1[i] = 0.5 * ( 1.0 - ogcp1[i] ) * fpenalty * ( gapfreq1[i] );
1028 fgcp1[i] = 0.5 * ( 1.0 - fgcp1[i] ) * fpenalty * ( gapfreq1[i] );
1030 for( i=0; i<lgth2; i++ )
1032 ogcp2[i] = 0.5 * ( 1.0 - ogcp2[i] ) * fpenalty * ( gapfreq2[i] );
1033 fgcp2[i] = 0.5 * ( 1.0 - fgcp2[i] ) * fpenalty * ( gapfreq2[i] );
1036 for( i=0; i<lgth1; i++ )
1037 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
1044 match_calc( initverticalw, cpmx2, cpmx1, 0, lgth1, doublework, intwork, 1 );
1046 part_imp_match_out_vead_tate_gapmap( initverticalw, gapmap2[0]+start2, lgth1, start1, gapmap1 );
1049 match_calc( currentw, cpmx1, cpmx2, 0, lgth2, doublework, intwork, 1 );
1051 part_imp_match_out_vead_gapmap( currentw, gapmap1[0]+start1, lgth2, start2, gapmap2 );
1052 #if 0 // -> tbfast.c
1054 imp_match_calc( currentw, icyc, jcyc, lgth1, lgth2, seq1, seq2, eff1, eff2, localhom, 1, 0 );
1060 for( i=1; i<lgth1+1; i++ )
1062 // initverticalw[i] += ( ogcp1[0] + fgcp1[i-1] ) ;
1063 initverticalw[i] += ( ogcp1[0] * headgapfreq2 + fgcp1[i-1] * gapfreq2[0] ) ;
1065 for( j=1; j<lgth2+1; j++ )
1067 // currentw[j] += ( ogcp2[0] + fgcp2[j-1] ) ;
1068 currentw[j] += ( ogcp2[0] * headgapfreq1 + fgcp2[j-1] * gapfreq1[0] ) ;
1074 for( j=1; j<lgth2+1; j++ )
1075 currentw[j] -= offset * j / 2.0;
1076 for( i=1; i<lgth1+1; i++ )
1077 initverticalw[i] -= offset * i / 2.0;
1081 for( j=1; j<lgth2+1; ++j )
1083 // m[j] = currentw[j-1] + ogcp1[1]; mp[j] = 0;
1084 m[j] = currentw[j-1] + ogcp1[1] * gapfreq2[j-1]; mp[j] = 0;;
1087 lastverticalw[0] = currentw[lgth2-1];
1089 if( outgap ) lasti = lgth1+1; else lasti = lgth1;
1093 fprintf( stderr, "currentw = \n" );
1094 for( i=0; i<lgth1+1; i++ )
1096 fprintf( stderr, "%5.2f ", currentw[i] );
1098 fprintf( stderr, "\n" );
1099 fprintf( stderr, "initverticalw = \n" );
1100 for( i=0; i<lgth2+1; i++ )
1102 fprintf( stderr, "%5.2f ", initverticalw[i] );
1104 fprintf( stderr, "\n" );
1105 fprintf( stderr, "fcgp\n" );
1106 for( i=0; i<lgth1; i++ )
1107 fprintf( stderr, "fgcp1[%d]=%f\n", i, ogcp1[i] );
1108 for( i=0; i<lgth2; i++ )
1109 fprintf( stderr, "fgcp2[%d]=%f\n", i, ogcp2[i] );
1112 for( i=1; i<lasti; i++ )
1115 #ifdef enablemultithread
1116 // fprintf( stderr, "chudan = %d, %d\n", *chudanpt, chudanref );
1117 if( chudanpt && *chudanpt != chudanref )
1119 // fprintf( stderr, "\n\n## CHUUDAN!!! i\n" );
1126 previousw = currentw;
1129 previousw[0] = initverticalw[i-1];
1131 match_calc( currentw, cpmx1, cpmx2, i, lgth2, doublework, intwork, 0 );
1133 fprintf( stderr, "\n" );
1134 fprintf( stderr, "i=%d\n", i );
1135 fprintf( stderr, "currentw before imp = \n" );
1136 for( j=0; j<lgth2; j++ )
1138 fprintf( stderr, "%5.2f ", currentw[j] );
1140 fprintf( stderr, "\n" );
1144 // fprintf( stderr, "Calling imp_match_calc (o) lgth = %d, i = %d\n", lgth1, i );
1145 // imp_match_out_vead( currentw, i, lgth2 );
1146 part_imp_match_out_vead_gapmap( currentw, gapmap1[i]+start1, lgth2, start2, gapmap2 );
1149 fprintf( stderr, "specificity = 0\n" );
1150 fprintf( stderr, "i=%d\n", i );
1151 fprintf( stderr, "currentw = \n" );
1152 for( j=0; j<lgth2; j++ )
1154 fprintf( stderr, "%5.2f ", currentw[j] );
1156 fprintf( stderr, "\n" );
1158 currentw[0] = initverticalw[i];
1161 // mi = previousw[0] + ogcp2[1]; mpi = 0;
1162 mi = previousw[0] + ogcp2[1] * gapfreq1[i-1]; mpi=0;
1167 curpt = currentw + 1;
1171 fgcp1va = fgcp1[i-1];
1173 gf1va = gapfreq1[i];
1174 gf1vapre = gapfreq1[i-1];
1176 gf2ptpre = gapfreq2;
1180 prevwmrecordspt = prevwmrecords;
1181 wmrecordspt = wmrecords+1;
1182 wmrecords1pt = wmrecords;
1183 warpipt = warpi + 1;
1184 warpjpt = warpj + 1;
1187 for( j=1; j<lastj; j++ )
1189 #ifdef xxxenablemultithread
1190 // fprintf( stderr, "chudan = %d, %d\n", *chudanpt, chudanref );
1191 if( chudanpt && *chudanpt != chudanref )
1193 // fprintf( stderr, "\n\n## CHUUDAN!!! j\n" );
1202 fprintf( stderr, "%5.0f->", wm );
1204 // g = mi + *fgcp2pt * gapfreq1[i];
1205 if( (g = mi + *fgcp2pt * gf1va) > wm )
1208 *ijppt = -( j - mpi );
1210 // g = *prept + *ogcp2pt * gapfreq1[i-1];
1211 if( (g = *prept + *ogcp2pt * gf1vapre) >= mi )
1220 // g = *mjpt + fgcp1va * gapfreq2[j];
1221 if( (g = *mjpt + fgcp1va * *gf2pt) > wm )
1224 *ijppt = +( i - *mpjpt );
1226 // g = *prept + ogcp1va * gapfreq2[j-1];
1227 if( (g = *prept + ogcp1va * *gf2ptpre) >= *mjpt )
1233 m[j] += fpenalty_ex;
1238 if( ( g=*prevwmrecordspt++ + fpenalty_shift + fpenalty_ex * ( i - prevwarpi[j-1] + j - prevwarpj[j-1] ) ) > wm ) // naka ha osokute kamawanai
1240 if( ( g=*prevwmrecordspt++ + fpenalty_shift ) > wm ) // naka ha osokute kamawanai
1243 // fprintf( stderr, "WARP in partA__align\n" );
1244 if( warpn && prevwarpi[j-1] == warpis[warpn-1] && prevwarpj[j-1] == warpjs[warpn-1] )
1246 *ijppt = warpbase + warpn - 1;
1250 *ijppt = warpbase + warpn;
1251 warpis = realloc( warpis, sizeof(int) * ( warpn+1 ) );
1252 warpjs = realloc( warpjs, sizeof(int) * ( warpn+1 ) );
1253 warpis[warpn] = prevwarpi[j-1];
1254 warpjs[warpn] = prevwarpj[j-1];
1260 if( *wmrecords1pt > *wmrecordspt )
1262 *wmrecordspt = *wmrecords1pt;
1263 *warpipt = *(warpipt-1);
1264 *warpjpt = *(warpjpt-1);
1266 if( curm > *wmrecordspt )
1268 *wmrecordspt = curm;
1279 fprintf( stderr, "%5.0f ", wm );
1293 lastverticalw[i] = currentw[lgth2-1];
1297 fltncpy( prevwmrecords, wmrecords, lastj );
1298 intncpy( prevwarpi, warpi, lastj );
1299 intncpy( prevwarpj, warpj, lastj );
1304 // fprintf( stderr, "wm = %f\n", wm );
1305 // fprintf( stderr, "warpn = %d\n", warpn );
1307 free( prevwmrecords );
1317 for( j=1; j<lgth2+1; j++ )
1318 currentw[j] -= offset * ( lgth2 - j ) / 2.0;
1319 for( i=1; i<lgth1+1; i++ )
1320 lastverticalw[i] -= offset * ( lgth1 - i / 2.0);
1325 fprintf( stderr, "\n" );
1326 for( i=0; i<icyc; i++ ) fprintf( stderr,"%s\n", seq1[i] );
1327 fprintf( stderr, "#####\n" );
1328 for( j=0; j<jcyc; j++ ) fprintf( stderr,"%s\n", seq2[j] );
1329 fprintf( stderr, "====>" );
1330 for( i=0; i<icyc; i++ ) strcpy( mseq1[i], seq1[i] );
1331 for( j=0; j<jcyc; j++ ) strcpy( mseq2[j], seq2[j] );
1335 Atracking_localhom( impmatch, currentw, lastverticalw, seq1, seq2, mseq1, mseq2, ijp, icyc, jcyc, start1, end1, start2, end2, gapmap1, gapmap2, warpis, warpjs, warpbase );
1338 Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, ijp, icyc, jcyc, warpis, warpjs, warpbase );
1340 if( warpis ) free( warpis );
1341 if( warpjs ) free( warpjs );
1343 // fprintf( stderr, "### impmatch = %f\n", *impmatch );
1345 resultlen = strlen( mseq1[0] );
1346 if( alloclen < resultlen || resultlen > N )
1348 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
1349 ErrorExit( "LENGTH OVER!\n" );
1353 for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
1354 for( j=0; j<jcyc; j++ ) strcpy( seq2[j], mseq2[j] );
1356 fprintf( stderr, "\n" );
1357 for( i=0; i<icyc; i++ ) fprintf( stderr, "%s\n", mseq1[i] );
1358 fprintf( stderr, "#####\n" );
1359 for( j=0; j<jcyc; j++ ) fprintf( stderr, "%s\n", mseq2[j] );
1366 double partA__align_variousdist( int **which, double ***matrices, double **n_dynamicmtx, char **seq1, char **seq2, double *eff1, double *eff2, double **eff1s, double **eff2s, int icyc, int jcyc, int alloclen, LocalHom ***localhom, double *impmatch, int start1, int end1, int start2, int end2, int *gapmap1, int *gapmap2, char *sgap1, char *sgap2, char *egap1, char *egap2, int *chudanpt, int chudanref, int *chudanres )
1367 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
1370 register int i, j, c;
1371 int lasti, lastj; /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
1374 double wm = 0.0; /* int ?????? */
1376 double *currentw, *previousw;
1380 double *mjpt, *prept, *curpt;
1383 static TLS double mi, *m;
1384 static TLS int **ijp;
1385 static TLS int mpi, *mp;
1386 static TLS double *w1, *w2;
1387 static TLS double *match;
1388 static TLS double *initverticalw; /* kufuu sureba iranai */
1389 static TLS double *lastverticalw; /* kufuu sureba iranai */
1390 static TLS char **mseq1;
1391 static TLS char **mseq2;
1392 static TLS char **mseq;
1393 static TLS double *ogcp1;
1394 static TLS double *ogcp2;
1395 static TLS double *fgcp1;
1396 static TLS double *fgcp2;
1397 static TLS double ***cpmx1s;
1398 static TLS double ***cpmx2s;
1399 static TLS double *gapfreq1;
1400 static TLS double *gapfreq2;
1401 static TLS int ***intwork;
1402 static TLS double ***doublework;
1403 static TLS int orlgth1 = 0, orlgth2 = 0;
1404 double fpenalty = (double)penalty;
1405 double fpenalty_shift = (double)penalty_shift;
1407 double fpenalty_ex = (double)penalty_ex;
1417 double headgapfreq1;
1418 double headgapfreq2;
1424 int *prevwarpi = NULL;
1425 int *prevwarpj = NULL;
1426 double *wmrecords = NULL;
1427 double *prevwmrecords = NULL;
1431 double *wmrecordspt, *wmrecords1pt, *prevwmrecordspt;
1432 int *warpipt, *warpjpt;
1433 int *nmask, **masklist1, **masklist2;
1440 // fprintf( stderr, "## Freeing local arrays in A__align\n" );
1444 part_imp_match_init_strict( NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL );
1450 FreeFloatVec( match );
1451 FreeFloatVec( initverticalw );
1452 FreeFloatVec( lastverticalw );
1457 FreeCharMtx( mseq );
1459 FreeFloatVec( ogcp1 );
1460 FreeFloatVec( ogcp2 );
1461 FreeFloatVec( fgcp1 );
1462 FreeFloatVec( fgcp2 );
1465 FreeFloatCub( cpmx1s );
1466 FreeFloatCub( cpmx2s );
1468 FreeFloatVec( gapfreq1 );
1469 FreeFloatVec( gapfreq2 );
1471 FreeFloatCub( doublework );
1472 FreeIntCub( intwork );
1477 // fprintf( stderr, "## Not allocated\n" );
1482 masklist1 = AllocateIntMtx( maxdistclass, 0 );
1483 masklist2 = AllocateIntMtx( maxdistclass, 0 );
1484 nmask = calloc( maxdistclass, sizeof( int ) );
1486 for( c=0; c<maxdistclass; c++ )
1488 for( i=0; i<icyc; i++ ) for( j=0; j<jcyc; j++ )
1490 if( eff1s[c][i] * eff2s[c][j] != 0.0 )
1493 if( c != which[i][j] )
1495 masklist1[c] = realloc( masklist1[c], sizeof( int ) * (nmask[c]+1) );
1496 masklist2[c] = realloc( masklist2[c], sizeof( int ) * (nmask[c]+1) );
1498 masklist1[c][nmask[c]] = i;
1499 masklist2[c][nmask[c]] = j;
1505 for( c=0; c<maxdistclass; c++ ) if( nmask[c] ) break;
1506 if( c<maxdistclass ) reporterr( "Found a complex grouping. This step may be a bit slow.\n" );
1508 lgth1 = strlen( seq1[0] );
1509 lgth2 = strlen( seq2[0] );
1511 // if( lgth1 == 0 ) fprintf( stderr, "WARNING: lgth1=0 in partA__align\n" );
1512 // if( lgth2 == 0 ) fprintf( stderr, "WARNING: lgth2=0 in partA__align\n" );
1514 if( lgth1 == 0 && lgth2 == 0 )
1519 for( i=0; i<icyc; i++ )
1523 while( j ) seq1[i][--j] = *newgapstr;
1524 // fprintf( stderr, "seq1[i] = %s\n", seq1[i] );
1531 for( i=0; i<jcyc; i++ )
1535 while( j ) seq2[i][--j] = *newgapstr;
1536 // fprintf( stderr, "seq2[i] = %s\n", seq2[i] );
1542 warpbase = lgth1 + lgth2;
1550 // fprintf( stderr, "IN partA__align_variousdist\n" );
1553 fprintf( stderr, "At present, outgap must be 1 to allow shift.\n" );
1556 wmrecords = AllocateFloatVec( lgth2+1 );
1557 warpi = AllocateIntVec( lgth2+1 );
1558 warpj = AllocateIntVec( lgth2+1 );
1559 prevwmrecords = AllocateFloatVec( lgth2+1 );
1560 prevwarpi = AllocateIntVec( lgth2+1 );
1561 prevwarpj = AllocateIntVec( lgth2+1 );
1562 for( i=0; i<lgth2+1; i++ ) wmrecords[i] = 0.0;
1563 for( i=0; i<lgth2+1; i++ ) prevwmrecords[i] = 0.0;
1564 for( i=0; i<lgth2+1; i++ ) prevwarpi[i] = -warpbase;
1565 for( i=0; i<lgth2+1; i++ ) prevwarpj[i] = -warpbase;
1566 for( i=0; i<lgth2+1; i++ ) warpi[i] = -warpbase;
1567 for( i=0; i<lgth2+1; i++ ) warpj[i] = -warpbase;
1571 fprintf( stderr, "eff in SA+++align\n" );
1572 for( i=0; i<icyc; i++ ) fprintf( stderr, "eff1[%d] = %f\n", i, eff1[i] );
1576 mseq1 = AllocateCharMtx( njob, 0 );
1577 mseq2 = AllocateCharMtx( njob, 0 );
1583 if( lgth1 > orlgth1 || lgth2 > orlgth2 )
1587 if( orlgth1 > 0 && orlgth2 > 0 )
1591 FreeFloatVec( match );
1592 FreeFloatVec( initverticalw );
1593 FreeFloatVec( lastverticalw );
1598 FreeCharMtx( mseq );
1600 FreeFloatVec( ogcp1 );
1601 FreeFloatVec( ogcp2 );
1602 FreeFloatVec( fgcp1 );
1603 FreeFloatVec( fgcp2 );
1606 FreeFloatCub( cpmx1s );
1607 FreeFloatCub( cpmx2s );
1609 FreeFloatVec( gapfreq1 );
1610 FreeFloatVec( gapfreq2 );
1612 FreeFloatCub( doublework );
1613 FreeIntCub( intwork );
1616 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
1617 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
1620 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
1623 w1 = AllocateFloatVec( ll2+2 );
1624 w2 = AllocateFloatVec( ll2+2 );
1625 match = AllocateFloatVec( ll2+2 );
1627 initverticalw = AllocateFloatVec( ll1+2 );
1628 lastverticalw = AllocateFloatVec( ll1+2 );
1630 m = AllocateFloatVec( ll2+2 );
1631 mp = AllocateIntVec( ll2+2 );
1633 mseq = AllocateCharMtx( njob, ll1+ll2 );
1635 ogcp1 = AllocateFloatVec( ll1+2 );
1636 ogcp2 = AllocateFloatVec( ll2+2 );
1637 fgcp1 = AllocateFloatVec( ll1+2 );
1638 fgcp2 = AllocateFloatVec( ll2+2 );
1640 cpmx1s = AllocateFloatCub( maxdistclass, nalphabets, ll1+2 );
1641 cpmx2s = AllocateFloatCub( maxdistclass, nalphabets, ll2+2 );
1643 gapfreq1 = AllocateFloatVec( ll1+2 );
1644 gapfreq2 = AllocateFloatVec( ll2+2 );
1646 doublework = AllocateFloatCub( maxdistclass, MAX( ll1, ll2 )+2, nalphabets );
1647 intwork = AllocateIntCub( maxdistclass, MAX( ll1, ll2 )+2, nalphabets );
1650 fprintf( stderr, "succeeded\n" );
1653 orlgth1 = ll1 - 100;
1654 orlgth2 = ll2 - 100;
1658 for( i=0; i<icyc; i++ ) mseq1[i] = mseq[i];
1659 for( j=0; j<jcyc; j++ ) mseq2[j] = mseq[icyc+j];
1662 if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
1666 if( commonAlloc1 && commonAlloc2 )
1668 FreeIntMtx( commonIP );
1671 ll1 = MAX( orlgth1, commonAlloc1 );
1672 ll2 = MAX( orlgth2, commonAlloc2 );
1675 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
1678 commonIP = AllocateIntMtx( ll1+10, ll2+10 );
1681 fprintf( stderr, "succeeded\n\n" );
1689 // cpmx_calc_new( seq1, cpmx1, eff1, lgth1, icyc );
1690 // cpmx_calc_new( seq2, cpmx2, eff2, lgth2, jcyc );
1691 for( c=0; c<maxdistclass; c++ )
1693 cpmx_calc_new( seq1, cpmx1s[c], eff1s[c], lgth1, icyc );
1694 cpmx_calc_new( seq2, cpmx2s[c], eff2s[c], lgth2, jcyc );
1699 new_OpeningGapCount( ogcp1, icyc, seq1, eff1, lgth1, sgap1 );
1700 new_OpeningGapCount( ogcp2, jcyc, seq2, eff2, lgth2, sgap2 );
1701 new_FinalGapCount( fgcp1, icyc, seq1, eff1, lgth1, egap1 );
1702 new_FinalGapCount( fgcp2, jcyc, seq2, eff2, lgth2, egap2 );
1703 outgapcount( &headgapfreq1, icyc, sgap1, eff1 );
1704 outgapcount( &headgapfreq2, jcyc, sgap2, eff2 );
1705 outgapcount( gapfreq1+lgth1, icyc, egap1, eff1 );
1706 outgapcount( gapfreq2+lgth2, jcyc, egap2, eff2 );
1710 st_OpeningGapCount( ogcp1, icyc, seq1, eff1, lgth1 );
1711 st_OpeningGapCount( ogcp2, jcyc, seq2, eff2, lgth2 );
1712 st_FinalGapCount( fgcp1, icyc, seq1, eff1, lgth1 );
1713 st_FinalGapCount( fgcp2, jcyc, seq2, eff2, lgth2 );
1716 gapfreq1[lgth1] = 0.0;
1717 gapfreq2[lgth2] = 0.0;
1720 if( legacygapcost == 0 )
1722 gapcountf( gapfreq1, seq1, icyc, eff1, lgth1 );
1723 gapcountf( gapfreq2, seq2, jcyc, eff2, lgth2 );
1724 for( i=0; i<lgth1+1; i++ ) gapfreq1[i] = 1.0 - gapfreq1[i];
1725 for( i=0; i<lgth2+1; i++ ) gapfreq2[i] = 1.0 - gapfreq2[i];
1726 headgapfreq1 = 1.0 - headgapfreq1;
1727 headgapfreq2 = 1.0 - headgapfreq2;
1731 for( i=0; i<lgth1+1; i++ ) gapfreq1[i] = 1.0;
1732 for( i=0; i<lgth2+1; i++ ) gapfreq2[i] = 1.0;
1737 for( i=0; i<lgth1; i++ )
1739 ogcp1[i] = 0.5 * ( 1.0 - ogcp1[i] ) * fpenalty * ( gapfreq1[i] );
1740 fgcp1[i] = 0.5 * ( 1.0 - fgcp1[i] ) * fpenalty * ( gapfreq1[i] );
1742 for( i=0; i<lgth2; i++ )
1744 ogcp2[i] = 0.5 * ( 1.0 - ogcp2[i] ) * fpenalty * ( gapfreq2[i] );
1745 fgcp2[i] = 0.5 * ( 1.0 - fgcp2[i] ) * fpenalty * ( gapfreq2[i] );
1748 for( i=0; i<lgth1; i++ )
1749 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
1756 // match_calc( initverticalw, cpmx2, cpmx1, 0, lgth1, doublework, intwork, 1 );
1757 fillzero( initverticalw, lgth1 );
1758 for( c=0; c<maxdistclass; c++ )
1760 match_calc_add( matrices[c], initverticalw, cpmx2s[c], cpmx1s[c], 0, lgth1, doublework[c], intwork[c], 1 );
1761 if( nmask[c] ) match_calc_del( which, matrices, initverticalw, jcyc, seq2, eff2, icyc, seq1, eff1, 0, lgth1, c, nmask[c], masklist2[c], masklist1[c] );
1765 part_imp_match_out_vead_tate_gapmap( initverticalw, gapmap2[0]+start2, lgth1, start1, gapmap1 );
1768 // match_calc( currentw, cpmx1, cpmx2, 0, lgth2, doublework, intwork, 1 );
1769 fillzero( currentw, lgth2 );
1770 for( c=0; c<maxdistclass; c++ )
1772 match_calc_add( matrices[c], currentw, cpmx1s[c], cpmx2s[c], 0, lgth2, doublework[c], intwork[c], 1 );
1773 if( nmask[c] ) match_calc_del( which, matrices, currentw, icyc, seq1, eff1, jcyc, seq2, eff2, 0, lgth2, c, nmask[c], masklist1[c], masklist2[c] );
1776 part_imp_match_out_vead_gapmap( currentw, gapmap1[0]+start1, lgth2, start2, gapmap2 );
1777 #if 0 // -> tbfast.c
1779 imp_match_calc( currentw, icyc, jcyc, lgth1, lgth2, seq1, seq2, eff1, eff2, localhom, 1, 0 );
1785 for( i=1; i<lgth1+1; i++ )
1787 // initverticalw[i] += ( ogcp1[0] + fgcp1[i-1] ) ;
1788 initverticalw[i] += ( ogcp1[0] * headgapfreq2 + fgcp1[i-1] * gapfreq2[0] ) ;
1790 for( j=1; j<lgth2+1; j++ )
1792 // currentw[j] += ( ogcp2[0] + fgcp2[j-1] ) ;
1793 currentw[j] += ( ogcp2[0] * headgapfreq1 + fgcp2[j-1] * gapfreq1[0] ) ;
1799 for( j=1; j<lgth2+1; j++ )
1800 currentw[j] -= offset * j / 2.0;
1801 for( i=1; i<lgth1+1; i++ )
1802 initverticalw[i] -= offset * i / 2.0;
1806 for( j=1; j<lgth2+1; ++j )
1808 // m[j] = currentw[j-1] + ogcp1[1]; mp[j] = 0;
1809 m[j] = currentw[j-1] + ogcp1[1] * gapfreq2[j-1]; mp[j] = 0;;
1812 lastverticalw[0] = currentw[lgth2-1];
1814 if( outgap ) lasti = lgth1+1; else lasti = lgth1;
1818 fprintf( stderr, "currentw = \n" );
1819 for( i=0; i<lgth1+1; i++ )
1821 fprintf( stderr, "%5.2f ", currentw[i] );
1823 fprintf( stderr, "\n" );
1824 fprintf( stderr, "initverticalw = \n" );
1825 for( i=0; i<lgth2+1; i++ )
1827 fprintf( stderr, "%5.2f ", initverticalw[i] );
1829 fprintf( stderr, "\n" );
1830 fprintf( stderr, "fcgp\n" );
1831 for( i=0; i<lgth1; i++ )
1832 fprintf( stderr, "fgcp1[%d]=%f\n", i, ogcp1[i] );
1833 for( i=0; i<lgth2; i++ )
1834 fprintf( stderr, "fgcp2[%d]=%f\n", i, ogcp2[i] );
1837 for( i=1; i<lasti; i++ )
1840 #ifdef enablemultithread
1841 // fprintf( stderr, "chudan = %d, %d\n", *chudanpt, chudanref );
1842 if( chudanpt && *chudanpt != chudanref )
1844 // fprintf( stderr, "\n\n## CHUUDAN!!! i\n" );
1846 if( masklist1 ) freeintmtx( masklist1, maxdistclass ); masklist1 = NULL;
1847 if( masklist2 ) freeintmtx( masklist2, maxdistclass ); masklist2 = NULL;
1848 if( nmask ) free( nmask ); nmask = NULL;
1854 previousw = currentw;
1857 previousw[0] = initverticalw[i-1];
1859 // match_calc( currentw, cpmx1, cpmx2, i, lgth2, doublework, intwork, 0 );
1860 fillzero( currentw, lgth2 );
1861 for( c=0; c<maxdistclass; c++ )
1863 match_calc_add( matrices[c], currentw, cpmx1s[c], cpmx2s[c], i, lgth2, doublework[c], intwork[c], 0 );
1864 if( nmask[c] ) match_calc_del( which, matrices, currentw, icyc, seq1, eff1, jcyc, seq2, eff2, i, lgth2, c, nmask[c], masklist1[c], masklist2[c] );
1867 fprintf( stderr, "\n" );
1868 fprintf( stderr, "i=%d\n", i );
1869 fprintf( stderr, "currentw before imp = \n" );
1870 for( j=0; j<lgth2; j++ )
1872 fprintf( stderr, "%5.2f ", currentw[j] );
1874 fprintf( stderr, "\n" );
1878 // fprintf( stderr, "Calling imp_match_calc (o) lgth = %d, i = %d\n", lgth1, i );
1879 // imp_match_out_vead( currentw, i, lgth2 );
1880 part_imp_match_out_vead_gapmap( currentw, gapmap1[i]+start1, lgth2, start2, gapmap2 );
1883 fprintf( stderr, "specificity = %f\n", specificityconsideration );
1884 fprintf( stderr, "i=%d\n", i );
1885 fprintf( stderr, "currentw = \n" );
1886 for( j=0; j<lgth2; j++ )
1888 fprintf( stderr, "%5.2f ", currentw[j] );
1890 fprintf( stderr, "\n" );
1892 currentw[0] = initverticalw[i];
1895 // mi = previousw[0] + ogcp2[1]; mpi = 0;
1896 mi = previousw[0] + ogcp2[1] * gapfreq1[i-1]; mpi=0;
1901 curpt = currentw + 1;
1905 fgcp1va = fgcp1[i-1];
1907 gf1va = gapfreq1[i];
1908 gf1vapre = gapfreq1[i-1];
1910 gf2ptpre = gapfreq2;
1914 prevwmrecordspt = prevwmrecords;
1915 wmrecordspt = wmrecords+1;
1916 wmrecords1pt = wmrecords;
1917 warpipt = warpi + 1;
1918 warpjpt = warpj + 1;
1921 for( j=1; j<lastj; j++ )
1923 #ifdef xxxenablemultithread
1924 // fprintf( stderr, "chudan = %d, %d\n", *chudanpt, chudanref );
1925 if( chudanpt && *chudanpt != chudanref )
1927 // fprintf( stderr, "\n\n## CHUUDAN!!! j\n" );
1929 if( masklist1 ) freeintmtx( masklist1, maxdistclass ); masklist1 = NULL;
1930 if( masklist2 ) freeintmtx( masklist2, maxdistclass ); masklist2 = NULL;
1931 if( nmask ) free( nmask ); nmask = NULL;
1939 fprintf( stderr, "%5.0f->", wm );
1941 // g = mi + *fgcp2pt * gapfreq1[i];
1942 if( (g = mi + *fgcp2pt * gf1va) > wm )
1945 *ijppt = -( j - mpi );
1947 // g = *prept + *ogcp2pt * gapfreq1[i-1];
1948 if( (g = *prept + *ogcp2pt * gf1vapre) >= mi )
1957 // g = *mjpt + fgcp1va * gapfreq2[j];
1958 if( (g = *mjpt + fgcp1va * *gf2pt) > wm )
1961 *ijppt = +( i - *mpjpt );
1963 // g = *prept + ogcp1va * gapfreq2[j-1];
1964 if( (g = *prept + ogcp1va * *gf2ptpre) >= *mjpt )
1970 m[j] += fpenalty_ex;
1975 if( ( g=*prevwmrecordspt++ + fpenalty_shift + fpenalty_ex * ( i - prevwarpi[j-1] + j - prevwarpj[j-1] ) ) > wm ) // naka ha osokute kamawanai
1977 if( ( g=*prevwmrecordspt++ + fpenalty_shift ) > wm ) // naka ha osokute kamawanai
1980 if( warpn && prevwarpi[j-1] == warpis[warpn-1] && prevwarpj[j-1] == warpjs[warpn-1] )
1982 *ijppt = warpbase + warpn - 1;
1986 *ijppt = warpbase + warpn;
1987 warpis = realloc( warpis, sizeof(int) * ( warpn+1 ) );
1988 warpjs = realloc( warpjs, sizeof(int) * ( warpn+1 ) );
1989 warpis[warpn] = prevwarpi[j-1];
1990 warpjs[warpn] = prevwarpj[j-1];
1997 if( *wmrecords1pt > *wmrecordspt )
1999 *wmrecordspt = *wmrecords1pt;
2000 *warpipt = *(warpipt-1);
2001 *warpjpt = *(warpjpt-1);
2003 if( curm > *wmrecordspt )
2005 *wmrecordspt = curm;
2016 fprintf( stderr, "%5.0f ", wm );
2030 lastverticalw[i] = currentw[lgth2-1];
2034 fltncpy( prevwmrecords, wmrecords, lastj );
2035 intncpy( prevwarpi, warpi, lastj );
2036 intncpy( prevwarpj, warpj, lastj );
2041 // fprintf( stderr, "wm = %f\n", wm );
2042 // fprintf( stderr, "warpn = %d\n", warpn );
2044 free( prevwmrecords );
2054 for( j=1; j<lgth2+1; j++ )
2055 currentw[j] -= offset * ( lgth2 - j ) / 2.0;
2056 for( i=1; i<lgth1+1; i++ )
2057 lastverticalw[i] -= offset * ( lgth1 - i / 2.0);
2062 fprintf( stderr, "\n" );
2063 for( i=0; i<icyc; i++ ) fprintf( stderr,"%s\n", seq1[i] );
2064 fprintf( stderr, "#####\n" );
2065 for( j=0; j<jcyc; j++ ) fprintf( stderr,"%s\n", seq2[j] );
2066 fprintf( stderr, "====>" );
2067 for( i=0; i<icyc; i++ ) strcpy( mseq1[i], seq1[i] );
2068 for( j=0; j<jcyc; j++ ) strcpy( mseq2[j], seq2[j] );
2072 Atracking_localhom( impmatch, currentw, lastverticalw, seq1, seq2, mseq1, mseq2, ijp, icyc, jcyc, start1, end1, start2, end2, gapmap1, gapmap2, warpis, warpjs, warpbase );
2075 Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, ijp, icyc, jcyc, warpis, warpjs, warpbase );
2077 if( warpis ) free( warpis );
2078 if( warpjs ) free( warpjs );
2080 // fprintf( stderr, "### impmatch = %f\n", *impmatch );
2082 resultlen = strlen( mseq1[0] );
2083 if( alloclen < resultlen || resultlen > N )
2085 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
2086 ErrorExit( "LENGTH OVER!\n" );
2090 for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
2091 for( j=0; j<jcyc; j++ ) strcpy( seq2[j], mseq2[j] );
2093 fprintf( stderr, "\n" );
2094 for( i=0; i<icyc; i++ ) fprintf( stderr, "%s\n", mseq1[i] );
2095 fprintf( stderr, "#####\n" );
2096 for( j=0; j<jcyc; j++ ) fprintf( stderr, "%s\n", mseq2[j] );
2099 if( masklist1 ) freeintmtx( masklist1, maxdistclass ); masklist1 = NULL;
2100 if( masklist2 ) freeintmtx( masklist2, maxdistclass ); masklist2 = NULL;
2101 if( nmask ) free( nmask ); nmask = NULL;