/*****************************************************************************/ /* score.c /* Scores a 200x200 matrix in 3 seconds */ /*****************************************************************************/ #include "score.h" #include #include #include static char *Unshift(char *pszSource, int c); Matrix *New(char *pszA, char *pszB, Payoff payoff) { int row, col; Matrix *pMatrix = (Matrix *) malloc(sizeof (Matrix)); fprintf(stderr, "New"); pMatrix->payoff = payoff; pMatrix->pszA = Unshift(pszA, ' '); pMatrix->pszB = Unshift(pszB, ' '); pMatrix->nRows = strlen(pMatrix->pszA); pMatrix->nCols = strlen(pMatrix->pszB); pMatrix->ppCell = (Cell**) malloc(pMatrix->nRows * sizeof (Cell *)); for (row=0; rownRows; row++) { fprintf(stderr, "."); pMatrix->ppCell[row] = (Cell *) malloc(pMatrix->nCols * sizeof(Cell)); for (col=0; colnCols; col++) { Cell *pCell = &pMatrix->ppCell[row][col]; pCell->row = row; pCell->col = col; pCell->score = 0; pCell->prev = 0; } } fprintf(stderr, "\n"); return pMatrix; } void Delete(Matrix *pMatrix) { int row; free(pMatrix->pszA); free(pMatrix->pszB); for (row=0; rownRows; row++) free(pMatrix->ppCell[row]); free(pMatrix); } void Score(Matrix *pMatrix) { int row, col; Payoff payoff = pMatrix->payoff; fprintf(stderr, "Score"); for (row=1; rownRows; row++) { int cA = pMatrix->pszA[row]; fprintf(stderr, "."); for (col=1; colnCols; col++) { int cB = pMatrix->pszB[col]; int pay = cA==cB ? payoff.match : payoff. mismatch; int ulScore = pMatrix->ppCell[row-1][col-1].score; int maxScore = ulScore + pay; Cell* pCell = &pMatrix->ppCell[row-1][col-1]; int r, c; for (r=0; rppCell[r][col].score; int gapScore = uScore + payoff.open + payoff.extend * (row-r); if (gapScore < maxScore) continue; maxScore = gapScore; pCell = &pMatrix->ppCell[r][col]; } for (c=0; cppCell[row][c].score; int gapScore = lScore + payoff.open + payoff.extend * (col-c); if (gapScore < maxScore) continue; maxScore = gapScore; pCell = &pMatrix->ppCell[row][c]; } pMatrix->ppCell[row][col].score = maxScore; pMatrix->ppCell[row][col].prev = pCell; } } fprintf(stderr, "\n"); } void DumpScore(Matrix *pMatrix) { int row, col; printf(" "); for (col=1; colnCols; col++) printf("%3c", pMatrix->pszB[col]); printf("\n"); for (row=1; rownRows; row++) { printf("%3c", pMatrix->pszA[row]); for (col=1; colnCols; col++) printf("%3d", pMatrix->ppCell[row][col].score); printf("\n"); } } char *Unshift(char *pszSource, int c) { int n = strlen(pszSource); char *pszDest = (char *) malloc(n+2); assert(pszDest); pszDest[0] = c; strcpy(pszDest+1, pszSource); return pszDest; }