Array2D matrix_multiply(const Array2D &M1,const Array2D &M2) { if (M1.height()!=M2.width()) return Array2D(); Array2D ret(M1.width(),M2.height()); for (int j=0; j<ret.height(); j++) for (int i=0; i<ret.width(); i++) { double val=0; for (int k=0; k<M1.height(); k++) { val+=M1.value(i,k)*M2.value(k,j); } ret.setValue(val,i,j); } return ret; }
void Trajectory::nextframe() { if(fail) return; if(!frameheader(×tep, &n, boxlo.data(), boxhi.data())) { fail = true; return; } if(timestep < 0) { fail = true; return; } for (int i = 0; i < 3; ++i) { prd[i] = boxhi[i] - boxlo[i]; } if (n > 0) { if (last_n_allocated != n) { if (x != nullptr) Free2D(x); if (v != nullptr) Free2D(v); if (f != nullptr) Free2D(f); } x = Array2D(n, 3); v = Array2D(n, 3); f = Array2D(n, 3); last_n_allocated = n; if(!framebody(&n, x[0], v[0], f[0])) { fail = true; return; } } else { fail = true; return; } }
/*>BOOL ReadMDM(char *mdmfile) --------------------------- Input: char *mdmfile Mutation data matrix filename Returns: BOOL Success? Read mutation data matrix into static global arrays. The matrix may have comments at the start introduced with a ! in the first column. The matrix must be complete (i.e. a triangular matrix will not work). A line describing the residue types must appear, and may be placed before or after the matrix itself 07.10.92 Original 18.03.94 getc() -> fgetc() 24.11.94 Automatically looks in DATAENV if not found in current directory 28.02.95 Modified to read any size MDM and allow comments Also allows the list of aa types before or after the actual matrix 26.07.95 Removed unused variables 06.02.03 Fixed for new version of GetWord() 07.04.09 Completely re-written to allow it to read BLAST style matrix files as well as the ones used previously Allow comments introduced with # as well as ! Uses MAXWORD rather than hardcoded 16 */ BOOL ReadMDM(char *mdmfile) { FILE *mdm = NULL; int i, j, k, row, tmpStoreSize; char buffer[MAXBUFF], word[MAXWORD], *p, **tmpStore; BOOL noenv; if((mdm=OpenFile(mdmfile, DATAENV, "r", &noenv))==NULL) { return(FALSE); } /* First read the file to determine the dimensions */ while(fgets(buffer,MAXBUFF,mdm)) { TERMINATE(buffer); KILLLEADSPACES(p,buffer); /* First line which is non-blank and non-comment */ if(strlen(p) && p[0] != '!' && p[0] != '#') { sMDMSize = 0; for(p = buffer; p!=NULL;) { p = GetWord(p, word, MAXWORD); /* Increment counter if this is numeric */ if(isdigit(word[0]) || ((word[0] == '-')&&(isdigit(word[1])))) sMDMSize++; } if(sMDMSize) break; } } /* Allocate memory for the MDM and the AA List */ if((sMDMScore = (int **)Array2D(sizeof(int),sMDMSize,sMDMSize))==NULL) return(FALSE); if((sMDM_AAList = (char *)malloc((sMDMSize+1)*sizeof(char)))==NULL) { FreeArray2D((char **)sMDMScore, sMDMSize, sMDMSize); return(FALSE); } /* Allocate temporary storage for a row from the matrix */ tmpStoreSize = 2*sMDMSize; if((tmpStore = (char **)Array2D(sizeof(char), tmpStoreSize, MAXWORD)) ==NULL) { free(sMDM_AAList); FreeArray2D((char **)sMDMScore, sMDMSize, sMDMSize); return(FALSE); } /* Fill the matrix with zeros */ for(i=0; i<sMDMSize; i++) { for(j=0; j<sMDMSize; j++) { sMDMScore[i][j] = 0; } } /* Rewind the file and read the actual data */ rewind(mdm); row = 0; while(fgets(buffer,MAXBUFF,mdm)) { int Numeric; TERMINATE(buffer); KILLLEADSPACES(p,buffer); /* Check line is non-blank and non-comment */ if(strlen(p) && p[0] != '!' && p[0] != '#') { Numeric = 0; for(p = buffer, i = 0; p!=NULL && i<tmpStoreSize; i++) { p = GetWord(p, tmpStore[i], MAXWORD); /* Incremement Numeric counter if it's a numeric field */ if(isdigit(tmpStore[i][0]) || ((tmpStore[i][0] == '-')&&(isdigit(tmpStore[i][1])))) { Numeric++; } } /* No numeric fields so it is the amino acid names */ if(Numeric == 0) { for(j = 0; j<i && j<sMDMSize; j++) { sMDM_AAList[j] = tmpStore[j][0]; } } else { /* There were numeric fields, so copy them into the matrix, skipping any non-numeric fields j counts the input fields k counts the fields in sMDMScore row counts the row in sMDMScore */ for(j=0, k=0; j<i && k<sMDMSize; j++) { if(isdigit(tmpStore[j][0]) || ((tmpStore[j][0] == '-')&&(isdigit(tmpStore[j][1])))) { sscanf(tmpStore[j],"%d",&(sMDMScore[row][k])); k++; } } row++; } } } fclose(mdm); FreeArray2D((char **)tmpStore, tmpStoreSize, MAXWORD); return(TRUE); }
/*>int affinealign(char *seq1, int length1, char *seq2, int length2, BOOL verbose, BOOL identity, int penalty, int penext, char *align1, char *align2, int *align_len) --------------------------------------------------------------------- Input: char *seq1 First sequence int length1 First sequence length char *seq2 Second sequence int length2 Second sequence length BOOL verbose Display N&W matrix BOOL identity Use identity matrix int penalty Gap insertion penalty value int penext Extension penalty Output: char *align1 Sequence 1 aligned char *align2 Sequence 2 aligned int *align_len Alignment length Returns: int Alignment score (0 on error) Perform simple N&W alignment of seq1 and seq2. No window is used, so will be slow for long sequences. Note that you must allocate sufficient memory for the aligned sequences. The easy way to do this is to ensure that align1 and align2 are of length (length1+length2). 07.10.92 Adapted from original written while at NIMR 08.10.92 Split into separate routines 09.10.92 Changed best structure to simple integers, moved SearchForBest() into TraceBack() 21.08.95 Was only filling in the bottom right cell at initialisation rather than all the right hand column and bottom row 11.07.96 Changed calls to calcscore() to CalcMDMScore() 06.03.00 Changed name to affinealign() (the routine align() is provided as a backwards compatible wrapper). Added penext parameter. Now supports affine gap penalties with separate opening and extension penalties. The code now maintains the path as it goes. 27.02.07 Exactly as affinealign() but upcases characters before comparison ************************************************************************** ****** NOTE AND CHANGES SHOULD BE PROPAGATED TO affinealign() ****** ************************************************************************** */ int affinealignuc(char *seq1, int length1, char *seq2, int length2, BOOL verbose, BOOL identity, int penalty, int penext, char *align1, char *align2, int *align_len) { XY **dirn = NULL; int **matrix = NULL, maxdim, i, j, k, l, i1, j1, dia, right, down, rcell, dcell, maxoff, match = 1, thisscore, gapext, score; maxdim = MAX(length1, length2); /* Initialise the score matrix */ if((matrix = (int **)Array2D(sizeof(int), maxdim, maxdim))==NULL) return(0); if((dirn = (XY **)Array2D(sizeof(XY), maxdim, maxdim))==NULL) return(0); for(i=0;i<maxdim;i++) { for(j=0;j<maxdim;j++) { matrix[i][j] = 0; dirn[i][j].x = -1; dirn[i][j].y = -1; } } /* Fill in scores up the right hand side of the matrix */ for(j=0; j<length2; j++) { if(identity) { if(seq1[length1-1] == seq2[j]) matrix[length1-1][j] = match; } else { matrix[length1-1][j] = CalcMDMScoreUC(seq1[length1-1], seq2[j]); } } /* Fill in scores along the bottom row of the matrix */ for(i=0; i<length1; i++) { if(identity) { if(seq1[i] == seq2[length2-1]) matrix[i][length2-1] = match; } else { matrix[i][length2-1] = CalcMDMScoreUC(seq1[i], seq2[length2-1]); } } i = length1 - 1; j = length2 - 1; /* Move back along the diagonal */ while(i > 0 && j > 0) { i--; j--; /* Fill in the scores along this row */ for(i1 = i; i1 > -1; i1--) { dia = matrix[i1+1][j+1]; /* Find highest score to right of diagonal */ rcell = i1+2; if(i1+2 >= length1) right = 0; else right = matrix[i1+2][j+1] - penalty; gapext = 1; for(k = i1+3; k<length1; k++, gapext++) { thisscore = matrix[k][j+1] - (penalty + gapext*penext); if(thisscore > right) { right = thisscore; rcell = k; } } /* Find highest score below diagonal */ dcell = j+2; if(j+2 >= length2) down = 0; else down = matrix[i1+1][j+2] - penalty; gapext = 1; for(l = j+3; l<length2; l++, gapext++) { thisscore = matrix[i1+1][l] - (penalty + gapext*penext); if(thisscore > down) { down = thisscore; dcell = l; } } /* Set score to best of these */ maxoff = MAX(right, down); if(dia >= maxoff) { matrix[i1][j] = dia; dirn[i1][j].x = i1+1; dirn[i1][j].y = j+1; } else { if(right > down) { matrix[i1][j] = right; dirn[i1][j].x = rcell; dirn[i1][j].y = j+1; } else { matrix[i1][j] = down; dirn[i1][j].x = i1+1; dirn[i1][j].y = dcell; } } /* Add the score for a match */ if(identity) { if(seq1[i1] == seq2[j]) matrix[i1][j] += match; } else { matrix[i1][j] += CalcMDMScoreUC(seq1[i1],seq2[j]); } } /* Fill in the scores in this column */ for(j1 = j; j1 > -1; j1--) { dia = matrix[i+1][j1+1]; /* Find highest score to right of diagonal */ rcell = i+2; if(i+2 >= length1) right = 0; else right = matrix[i+2][j1+1] - penalty; gapext = 1; for(k = i+3; k<length1; k++, gapext++) { thisscore = matrix[k][j1+1] - (penalty + gapext*penext); if(thisscore > right) { right = thisscore; rcell = k; } } /* Find highest score below diagonal */ dcell = j1+2; if(j1+2 >= length2) down = 0; else down = matrix[i+1][j1+2] - penalty; gapext = 1; for(l = j1+3; l<length2; l++, gapext++) { thisscore = matrix[i+1][l] - (penalty + gapext*penext); if(thisscore > down) { down = thisscore; dcell = l; } } /* Set score to best of these */ maxoff = MAX(right, down); if(dia >= maxoff) { matrix[i][j1] = dia; dirn[i][j1].x = i+1; dirn[i][j1].y = j1+1; } else { if(right > down) { matrix[i][j1] = right; dirn[i][j1].x = rcell; dirn[i][j1].y = j1+1; } else { matrix[i][j1] = down; dirn[i][j1].x = i+1; dirn[i][j1].y = dcell; } } /* Add the score for a match */ if(identity) { if(seq1[i] == seq2[j1]) matrix[i][j1] += match; } else { matrix[i][j1] += CalcMDMScoreUC(seq1[i],seq2[j1]); } } } score = TraceBack(matrix, dirn, length1, length2, seq1, seq2, align1, align2, align_len); if(verbose) { printf("Matrix:\n-------\n"); for(j=0; j<length2;j++) { for(i=0; i<length1; i++) { printf("%3d ",matrix[i][j]); } printf("\n"); } printf("Path:\n-----\n"); for(j=0; j<length2;j++) { for(i=0; i<length1; i++) { printf("(%3d,%3d) ",dirn[i][j].x,dirn[i][j].y); } printf("\n"); } } FreeArray2D((char **)matrix, maxdim, maxdim); FreeArray2D((char **)dirn, maxdim, maxdim); return(score); }