107 #define MAX(a,b) ((a) > (b) ? (a) : (b))
110 #define DATAENV "DATADIR"
124 static int **sMDMScore;
125 static int sMDMSize = 0;
147 static int NumericSearchForBest(
int **matrix,
int length1,
int length2,
148 int *BestI,
int *BestJ,
int *seq1,
int *seq2,
149 int *align1,
int *align2);
150 static int NumericTraceBack(
int **matrix,
XY **dirn,
int length1,
151 int length2,
int *seq1,
int *seq2,
152 int *align1,
int *align2,
183 static int NumericSearchForBest(
int **matrix,
200 for(i = 1; i < length1; i++)
202 if(matrix[i][0] > matrix[besti][0]) besti = i;
205 for(j = 1; j < length2; j++)
207 if(matrix[0][j] > matrix[0][bestj]) bestj = j;
209 if(matrix[besti][0] > matrix[0][bestj])
213 for(i=0; i<*BestI; i++)
215 align1[ai] = seq1[i];
223 for(j=0; j<*BestJ; j++)
226 align2[ai++] = seq2[j];
273 while(fgets(buffer,
MAXBUFF,mdm))
277 if(strlen(p) && p[0] !=
'!')
282 for(p = buffer, sMDMSize = 0; p!=
NULL; sMDMSize++)
287 if((sMDMScore = (
int **)
blArray2D(
sizeof(
int),sMDMSize,sMDMSize))==
NULL)
291 for(i=0; i<sMDMSize; i++)
293 for(j=0; j<sMDMSize; j++)
307 if(sscanf(word,
"%d",&j))
309 for(p = buffer, j = 0; p!=
NULL && j<sMDMSize; j++)
312 sscanf(word,
"%d",&(sMDMScore[i][j]));
317 }
while(fgets(buffer,
MAXBUFF,mdm));
345 static int NWarn = 0;
354 printf(
"Token %d not found in matrix\n",resa);
356 printf(
"More token not found in matrix...\n");
362 printf(
"Token %d not found in matrix\n",resb);
364 printf(
"More tokens not found in matrix...\n");
374 return(sMDMScore[i][j]);
430 rcell, dcell, maxoff,
436 maxdim =
MAX(length1, length2);
439 if((matrix = (
int **)
blArray2D(
sizeof(
int), maxdim, maxdim))==
NULL)
444 for(i=0;i<maxdim;i++)
446 for(j=0;j<maxdim;j++)
455 for(j=0; j<length2; j++)
459 if(seq1[length1-1] == seq2[j]) matrix[length1-1][j] = match;
469 for(i=0; i<length1; i++)
473 if(seq1[i] == seq2[length2-1]) matrix[i][length2-1] = match;
486 while(i > 0 && j > 0)
492 for(i1 = i; i1 > -1; i1--)
494 dia = matrix[i1+1][j+1];
498 if(i1+2 >= length1) right = 0;
499 else right = matrix[i1+2][j+1] - penalty;
502 for(k = i1+3; k<length1; k++, gapext++)
504 thisscore = matrix[k][j+1] - (penalty + gapext*penext);
506 if(thisscore > right)
515 if(j+2 >= length2) down = 0;
516 else down = matrix[i1+1][j+2] - penalty;
519 for(l = j+3; l<length2; l++, gapext++)
521 thisscore = matrix[i1+1][l] - (penalty + gapext*penext);
531 maxoff =
MAX(right, down);
535 dirn[i1][j].
x = i1+1;
542 matrix[i1][j] = right;
543 dirn[i1][j].
x = rcell;
548 matrix[i1][j] = down;
549 dirn[i1][j].
x = i1+1;
550 dirn[i1][j].
y = dcell;
557 if(seq1[i1] == seq2[j]) matrix[i1][j] += match;
566 for(j1 = j; j1 > -1; j1--)
568 dia = matrix[i+1][j1+1];
572 if(i+2 >= length1) right = 0;
573 else right = matrix[i+2][j1+1] - penalty;
576 for(k = i+3; k<length1; k++, gapext++)
578 thisscore = matrix[k][j1+1] - (penalty + gapext*penext);
580 if(thisscore > right)
589 if(j1+2 >= length2) down = 0;
590 else down = matrix[i+1][j1+2] - penalty;
593 for(l = j1+3; l<length2; l++, gapext++)
595 thisscore = matrix[i+1][l] - (penalty + gapext*penext);
605 maxoff =
MAX(right, down);
610 dirn[i][j1].
y = j1+1;
616 matrix[i][j1] = right;
617 dirn[i][j1].
x = rcell;
618 dirn[i][j1].
y = j1+1;
622 matrix[i][j1] = down;
624 dirn[i][j1].
y = dcell;
631 if(seq1[i] == seq2[j1]) matrix[i][j1] += match;
640 score = NumericTraceBack(matrix, dirn, length1, length2,
641 seq1, seq2, align1, align2, align_len);
645 printf(
"Matrix:\n-------\n");
646 for(j=0; j<length2;j++)
648 for(i=0; i<length1; i++)
650 printf(
"%3d ",matrix[i][j]);
655 printf(
"Path:\n-----\n");
656 for(j=0; j<length2;j++)
658 for(i=0; i<length1; i++)
660 printf(
"(%3d,%3d) ",dirn[i][j].x,dirn[i][j].y);
698 static int NumericTraceBack(
int **matrix,
713 ai = NumericSearchForBest(matrix, length1, length2, &BestI, &BestJ,
714 seq1, seq2, align1, align2);
719 align1[ai] = seq1[i];
720 align2[ai++] = seq2[j];
722 while(i < length1-1 && j < length2-1)
724 nextCell.
x = dirn[i][j].
x;
725 nextCell.
y = dirn[i][j].
y;
726 if((nextCell.
x == i+1) && (nextCell.
y == j+1))
732 else if(nextCell.
y == j+1)
739 while((i < nextCell.
x) && (i < length1-1))
741 align1[ai] = seq1[i++];
745 else if(nextCell.
x == i+1)
752 while((j < nextCell.
y) && (j < length2-1))
755 align2[ai++] = seq2[j++];
761 fprintf(stderr,
"align.c/TraceBack() internal error\n");
764 align1[ai] = seq1[i];
765 align2[ai++] = seq2[j];
771 for(j=i+1; j<length1; j++)
773 align1[ai] = seq1[j];
777 else if(j < length2-1)
779 for(i=j+1; i<length2; i++)
782 align2[ai++] = seq2[i];
788 return(matrix[BestI][BestJ]);
797 int main(
int argc,
char **argv)
799 int seq1[] = {1, 3, 1, 3, 7, 9, 5, 6},
800 seq2[] = {1, 3, 1, 3, 5, 6},
803 int score, al_len, i;
806 NumericReadMDM(
"numtopmat.mat");
808 score = NumericAffineAlign(seq1, 8, seq2, 6,
810 5, 0, align1, align2, &al_len);
812 align1[al_len] =
'\0';
813 align2[al_len] =
'\0';
815 for(i=0;i<al_len;i++)
816 printf(
"%2d=", align1[i]);
819 for(i=0;i<al_len;i++)
820 printf(
"%2d=", align2[i]);
int main(int argc, char **argv)
int blNumericAffineAlign(int *seq1, int length1, int *seq2, int length2, BOOL verbose, BOOL identity, int penalty, int penext, int *align1, int *align2, int *align_len)
char ** blArray2D(int size, int dim1, int dim2)
BOOL blNumericReadMDM(char *mdmfile)
Include file for 2D/3D array functions.
FILE * blOpenFile(char *filename, char *envvar, char *mode, BOOL *noenv)
Header file for sequence handling.
void blFreeArray2D(char **array, int dim1, int dim2)
char * blGetWord(char *buffer, char *word, int maxsize)
Header file for general purpose routines.
#define KILLLEADSPACES(y, x)
System-type variable type definitions.
int blNumericCalcMDMScore(int resa, int resb)