143 #define MAX(a,b) ((a) > (b) ? (a) : (b))
146 #define MAX3(c,d,e) (MAX(MAX((c),(d)),(e)))
148 #define DATAENV "DATADIR"
163 static int **sMDMScore;
164 static char *sMDM_AAList;
165 static int sMDMSize = 0;
170 static int SearchForBest(
int **matrix,
int length1,
int length2,
171 int *BestI,
int *BestJ,
char *seq1,
char *seq2,
172 char *align1,
char *align2);
173 static int TraceBack(
int **matrix,
XY **dirn,
int length1,
int length2,
174 char *seq1,
char *seq2,
char *align1,
char *align2,
225 return(
blAffinealign(seq1, length1, seq2, length2, verbose, identity,
226 penalty, 0, align1, align2, align_len));
293 rcell, dcell, maxoff,
299 maxdim =
MAX(length1, length2);
302 if((matrix = (
int **)
blArray2D(
sizeof(
int), maxdim, maxdim))==
NULL)
307 for(i=0;i<maxdim;i++)
309 for(j=0;j<maxdim;j++)
318 for(j=0; j<length2; j++)
322 if(seq1[length1-1] == seq2[j]) matrix[length1-1][j] = match;
331 for(i=0; i<length1; i++)
335 if(seq1[i] == seq2[length2-1]) matrix[i][length2-1] = match;
347 while(i > 0 && j > 0)
353 for(i1 = i; i1 > -1; i1--)
355 dia = matrix[i1+1][j+1];
359 if(i1+2 >= length1) right = 0;
360 else right = matrix[i1+2][j+1] - penalty;
363 for(k = i1+3; k<length1; k++, gapext++)
365 thisscore = matrix[k][j+1] - (penalty + gapext*penext);
367 if(thisscore > right)
376 if(j+2 >= length2) down = 0;
377 else down = matrix[i1+1][j+2] - penalty;
380 for(l = j+3; l<length2; l++, gapext++)
382 thisscore = matrix[i1+1][l] - (penalty + gapext*penext);
392 maxoff =
MAX(right, down);
396 dirn[i1][j].
x = i1+1;
403 matrix[i1][j] = right;
404 dirn[i1][j].
x = rcell;
409 matrix[i1][j] = down;
410 dirn[i1][j].
x = i1+1;
411 dirn[i1][j].
y = dcell;
418 if(seq1[i1] == seq2[j]) matrix[i1][j] += match;
427 for(j1 = j; j1 > -1; j1--)
429 dia = matrix[i+1][j1+1];
433 if(i+2 >= length1) right = 0;
434 else right = matrix[i+2][j1+1] - penalty;
437 for(k = i+3; k<length1; k++, gapext++)
439 thisscore = matrix[k][j1+1] - (penalty + gapext*penext);
441 if(thisscore > right)
450 if(j1+2 >= length2) down = 0;
451 else down = matrix[i+1][j1+2] - penalty;
454 for(l = j1+3; l<length2; l++, gapext++)
456 thisscore = matrix[i+1][l] - (penalty + gapext*penext);
466 maxoff =
MAX(right, down);
471 dirn[i][j1].
y = j1+1;
477 matrix[i][j1] = right;
478 dirn[i][j1].
x = rcell;
479 dirn[i][j1].
y = j1+1;
483 matrix[i][j1] = down;
485 dirn[i][j1].
y = dcell;
492 if(seq1[i] == seq2[j1]) matrix[i][j1] += match;
501 score = TraceBack(matrix, dirn, length1, length2,
502 seq1, seq2, align1, align2, align_len);
506 printf(
"Matrix:\n-------\n");
507 for(j=0; j<length2;j++)
509 for(i=0; i<length1; i++)
511 printf(
"%3d ",matrix[i][j]);
516 printf(
"Path:\n-----\n");
517 for(j=0; j<length2;j++)
519 for(i=0; i<length1; i++)
521 printf(
"(%3d,%3d) ",dirn[i][j].x,dirn[i][j].y);
601 rcell, dcell, maxoff,
607 maxdim =
MAX(length1, length2);
610 if((matrix = (
int **)
blArray2D(
sizeof(
int), maxdim, maxdim))==
NULL)
615 for(i=0;i<maxdim;i++)
617 for(j=0;j<maxdim;j++)
626 for(j=0; j<length2; j++)
630 if(seq1[length1-1] == seq2[j]) matrix[length1-1][j] = match;
639 for(i=0; i<length1; i++)
643 if(seq1[i] == seq2[length2-1]) matrix[i][length2-1] = match;
655 while(i > 0 && j > 0)
661 for(i1 = i; i1 > -1; i1--)
663 dia = matrix[i1+1][j+1];
667 if(i1+2 >= length1) right = 0;
668 else right = matrix[i1+2][j+1] - penalty;
671 for(k = i1+3; k<length1; k++, gapext++)
673 thisscore = matrix[k][j+1] - (penalty + gapext*penext);
675 if(thisscore > right)
684 if(j+2 >= length2) down = 0;
685 else down = matrix[i1+1][j+2] - penalty;
688 for(l = j+3; l<length2; l++, gapext++)
690 thisscore = matrix[i1+1][l] - (penalty + gapext*penext);
700 maxoff =
MAX(right, down);
704 dirn[i1][j].
x = i1+1;
711 matrix[i1][j] = right;
712 dirn[i1][j].
x = rcell;
717 matrix[i1][j] = down;
718 dirn[i1][j].
x = i1+1;
719 dirn[i1][j].
y = dcell;
726 if(seq1[i1] == seq2[j]) matrix[i1][j] += match;
735 for(j1 = j; j1 > -1; j1--)
737 dia = matrix[i+1][j1+1];
741 if(i+2 >= length1) right = 0;
742 else right = matrix[i+2][j1+1] - penalty;
745 for(k = i+3; k<length1; k++, gapext++)
747 thisscore = matrix[k][j1+1] - (penalty + gapext*penext);
749 if(thisscore > right)
758 if(j1+2 >= length2) down = 0;
759 else down = matrix[i+1][j1+2] - penalty;
762 for(l = j1+3; l<length2; l++, gapext++)
764 thisscore = matrix[i+1][l] - (penalty + gapext*penext);
774 maxoff =
MAX(right, down);
779 dirn[i][j1].
y = j1+1;
785 matrix[i][j1] = right;
786 dirn[i][j1].
x = rcell;
787 dirn[i][j1].
y = j1+1;
791 matrix[i][j1] = down;
793 dirn[i][j1].
y = dcell;
800 if(seq1[i] == seq2[j1]) matrix[i][j1] += match;
809 score = TraceBack(matrix, dirn, length1, length2,
810 seq1, seq2, align1, align2, align_len);
814 printf(
"Matrix:\n-------\n");
815 for(j=0; j<length2;j++)
817 for(i=0; i<length1; i++)
819 printf(
"%3d ",matrix[i][j]);
824 printf(
"Path:\n-----\n");
825 for(j=0; j<length2;j++)
827 for(i=0; i<length1; i++)
829 printf(
"(%3d,%3d) ",dirn[i][j].x,dirn[i][j].y);
874 int i, j, k, row, tmpStoreSize;
887 while(fgets(buffer,
MAXBUFF,mdm))
893 if(strlen(p) && p[0] !=
'!' && p[0] !=
'#')
896 for(p = buffer; p!=
NULL;)
900 if(isdigit(word[0]) ||
901 ((word[0] ==
'-')&&(isdigit(word[1]))))
910 if((sMDMScore = (
int **)
blArray2D(
sizeof(
int),sMDMSize,sMDMSize))==
NULL)
912 if((sMDM_AAList = (
char *)malloc((sMDMSize+1)*
sizeof(
char)))==
NULL)
919 tmpStoreSize = 2*sMDMSize;
929 for(i=0; i<sMDMSize; i++)
931 for(j=0; j<sMDMSize; j++)
940 while(fgets(buffer,
MAXBUFF,mdm))
948 if(strlen(p) && p[0] !=
'!' && p[0] !=
'#')
951 for(p = buffer, i = 0; p!=
NULL && i<tmpStoreSize; i++)
955 if(isdigit(tmpStore[i][0]) ||
956 ((tmpStore[i][0] ==
'-')&&(isdigit(tmpStore[i][1]))))
965 for(j = 0; j<i && j<sMDMSize; j++)
967 sMDM_AAList[j] = tmpStore[j][0];
978 for(j=0, k=0; j<i && k<sMDMSize; j++)
980 if(isdigit(tmpStore[j][0]) ||
981 ((tmpStore[j][0] ==
'-')&&(isdigit(tmpStore[j][1]))))
983 sscanf(tmpStore[j],
"%d",&(sMDMScore[row][k]));
1022 static int SearchForBest(
int **matrix,
1039 for(i = 1; i < length1; i++)
1041 if(matrix[i][0] > matrix[besti][0]) besti = i;
1044 for(j = 1; j < length2; j++)
1046 if(matrix[0][j] > matrix[0][bestj]) bestj = j;
1048 if(matrix[besti][0] > matrix[0][bestj])
1052 for(i=0; i<*BestI; i++)
1054 align1[ai] = seq1[i];
1062 for(j=0; j<*BestJ; j++)
1065 align2[ai++] = seq2[j];
1103 static int TraceBack(
int **matrix,
1118 ai = SearchForBest(matrix, length1, length2, &BestI, &BestJ,
1119 seq1, seq2, align1, align2);
1124 align1[ai] = seq1[i];
1125 align2[ai++] = seq2[j];
1127 while(i < length1-1 && j < length2-1)
1129 nextCell.
x = dirn[i][j].
x;
1130 nextCell.
y = dirn[i][j].
y;
1131 if((nextCell.
x == i+1) && (nextCell.
y == j+1))
1137 else if(nextCell.
y == j+1)
1144 while((i < nextCell.
x) && (i < length1-1))
1146 align1[ai] = seq1[i++];
1150 else if(nextCell.
x == i+1)
1157 while((j < nextCell.
y) && (j < length2-1))
1160 align2[ai++] = seq2[j++];
1166 fprintf(stderr,
"align.c/TraceBack() internal error\n");
1169 align1[ai] = seq1[i];
1170 align2[ai++] = seq2[j];
1176 for(j=i+1; j<length1; j++)
1178 align1[ai] = seq1[j];
1182 else if(j < length2-1)
1184 for(i=j+1; i<length2; i++)
1187 align2[ai++] = seq2[i];
1193 return(matrix[BestI][BestJ]);
1223 static int NWarn = 0;
1226 if((resa ==
'\0') && (resb ==
'\0'))
1232 for(i=0; i<sMDMSize; i++)
1234 if(resa==sMDM_AAList[i])
break;
1240 fprintf(stderr,
"Residue %c not found in matrix\n", resa);
1241 else if(NWarn == 10)
1242 fprintf(stderr,
"More residues not found in matrix...\n");
1247 for(j=0; j<sMDMSize; j++)
1249 if(resb==sMDM_AAList[j])
break;
1255 fprintf(stderr,
"Residue %c not found in matrix\n", resb);
1256 else if(NWarn == 10)
1257 fprintf(stderr,
"More residues not found in matrix...\n");
1268 return(sMDMScore[i][j]);
1296 static int NWarn = 0;
1299 if((resa ==
'\0') && (resb ==
'\0'))
1305 resa = (islower(resa)?toupper(resa):resa);
1306 resb = (islower(resb)?toupper(resb):resb);
1308 for(i=0; i<sMDMSize; i++)
1310 if(resa==sMDM_AAList[i])
break;
1316 fprintf(stderr,
"Residue %c not found in matrix\n", resa);
1317 else if(NWarn == 10)
1318 fprintf(stderr,
"More residues not found in matrix...\n");
1323 for(j=0; j<sMDMSize; j++)
1325 if(resb==sMDM_AAList[j])
break;
1331 fprintf(stderr,
"Residue %c not found in matrix\n", resb);
1332 else if(NWarn == 10)
1333 fprintf(stderr,
"More residues not found in matrix...\n");
1344 return(sMDMScore[i][j]);
1360 int MinVal = sMDMScore[0][0],
1361 MaxVal = sMDMScore[0][0],
1365 for(i=0; i<sMDMSize; i++)
1367 for(j=0; j<sMDMSize; j++)
1369 if(sMDMScore[i][j] < MinVal)
1371 MinVal = sMDMScore[i][j];
1373 else if(sMDMScore[i][j] > MaxVal)
1375 MaxVal = sMDMScore[i][j];
1383 for(i=0; i<sMDMSize; i++)
1385 for(j=0; j<sMDMSize; j++)
1387 sMDMScore[i][j] -= MinVal;
1392 return(MaxVal-MinVal);
1411 static int NWarn = 0;
1414 resa = (islower(resa)?toupper(resa):resa);
1415 resb = (islower(resb)?toupper(resb):resb);
1417 for(i=0;i<sMDMSize;i++)
1419 if(resa==sMDM_AAList[i])
break;
1424 printf(
"Residue %c not found in matrix\n",resa);
1425 else if(NWarn == 10)
1426 printf(
"More residues not found in matrix...\n");
1429 for(j=0;j<sMDMSize;j++)
1431 if(resb==sMDM_AAList[j])
break;
1436 printf(
"Residue %c not found in matrix\n",resb);
1437 else if(NWarn == 10)
1438 printf(
"More residues not found in matrix...\n");
1448 sMDMScore[i][j] *= weight;
1451 sMDMScore[j][i] *= weight;
1457 int main(
int argc,
char **argv)
1459 char seq1[] =
"ACTCLMCT",
1466 ReadMDM(
"pet91.mat");
1468 for(i=0; i<sMDMSize; i++)
1470 printf(
" %c", sMDM_AAList[i]);
1474 for(i=0; i<sMDMSize; i++)
1476 for(j=0; j<sMDMSize; j++)
1478 printf(
"%3d", sMDMScore[i][j]);
1483 score = affinealign(seq1, strlen(seq1), seq2, strlen(seq2),
1485 10, 1, align1, align2, &al_len);
1487 align1[al_len] =
'\0';
1488 align2[al_len] =
'\0';
1490 printf(
"%s\n", align1);
1491 printf(
"%s\n", align2);
int main(int argc, char **argv)
int blCalcMDMScoreUC(char resa, char resb)
char ** blArray2D(int size, int dim1, int dim2)
void blSetMDMScoreWeight(char resa, char resb, REAL weight)
Include file for 2D/3D array functions.
FILE * blOpenFile(char *filename, char *envvar, char *mode, BOOL *noenv)
Header file for sequence handling.
int blAffinealignuc(char *seq1, int length1, char *seq2, int length2, BOOL verbose, BOOL identity, int penalty, int penext, char *align1, char *align2, int *align_len)
void blFreeArray2D(char **array, int dim1, int dim2)
char * blGetWord(char *buffer, char *word, int maxsize)
BOOL blReadMDM(char *mdmfile)
Header file for general purpose routines.
int blAlign(char *seq1, int length1, char *seq2, int length2, BOOL verbose, BOOL identity, int penalty, char *align1, char *align2, int *align_len)
#define KILLLEADSPACES(y, x)
System-type variable type definitions.
int blCalcMDMScore(char resa, char resb)
int blAffinealign(char *seq1, int length1, char *seq2, int length2, BOOL verbose, BOOL identity, int penalty, int penext, char *align1, char *align2, int *align_len)