void updateHomopolymers(int** aanHA, int** aanHB, int** aanMoves, int nI, int nJ, const char* acA, const char *acB) { int nMove = aanMoves[nI][nJ]; switch(nMove){ case DIAG: if(acA[nI - 1] == acB[nJ - 1]){ if(getLastMatch(aanMoves[nI - 1][nJ - 1], nI - 1, nJ - 1, acA, acB) == acA[nI - 1]){ aanHA[nI][nJ] = aanHA[nI - 1][nJ - 1] + 1; aanHB[nI][nJ] = aanHB[nI - 1][nJ - 1] + 1; } else{ aanHA[nI][nJ] = 1; aanHB[nI][nJ] = 1; } } else{ aanHA[nI][nJ] = 0; aanHB[nI][nJ] = 0; } break; case LEFT: if(acB[nJ - 1] == getLastMatch(aanMoves[nI][nJ - 1], nI, nJ - 1, acA, acB)){ aanHB[nI][nJ] = aanHB[nI][nJ - 1] + 1; aanHA[nI][nJ] = aanHA[nI][nJ - 1] + 1; } else{ aanHA[nI][nJ] = 0; aanHB[nI][nJ] = 0; } break; case UP: /*GAP in B*/ if(acA[nI - 1] == getLastMatch(aanMoves[nI - 1][nJ], nI - 1, nJ, acA, acB)){ aanHA[nI][nJ] = aanHA[nI - 1][nJ] + 1; aanHB[nI][nJ] = aanHB[nI - 1][nJ] + 1; } else{ aanHA[nI][nJ] = 0; aanHB[nI][nJ] = 0; } break; } }
int returnHomopolymerA(int nMove, int** aanHA, int** aanMoves, int nI, int nJ, const char* acA, const char *acB) { int retA = 0; switch(nMove){ case DIAG: if(acA[nI - 1] == acB[nJ - 1]){ if(getLastMatch(aanMoves[nI - 1][nJ - 1], nI - 1, nJ - 1, acA, acB) == acA[nI - 1]){ retA = aanHA[nI - 1][nJ - 1] + 1; } else{ retA = 1; } } else{ retA = 0; } break; case LEFT: if(acB[nJ - 1] == getLastMatch(aanMoves[nI][nJ - 1], nI, nJ - 1, acA, acB)){ retA = aanHA[nI][nJ - 1] + 1; } else{ retA = 0; } break; case UP: /*GAP in B*/ if(acA[nI - 1] == getLastMatch(aanMoves[nI - 1][nJ], nI - 1, nJ, acA, acB)){ retA = aanHA[nI - 1][nJ] + 1; } else{ retA = 0; } break; } return retA; }
double Perseus::modeledPairwiseAlignSeqs(string query, string reference, string& qAlign, string& rAlign, vector<vector<double> >& correctMatrix){ try { int queryLength = query.size(); int refLength = reference.size(); vector<vector<double> > alignMatrix(queryLength + 1); vector<vector<char> > alignMoves(queryLength + 1); for(int i=0;i<=queryLength;i++){ if (m->getControl_pressed()) { return 0; } alignMatrix[i].resize(refLength + 1, 0); alignMoves[i].resize(refLength + 1, 'x'); } for(int i=0;i<=queryLength;i++){ if (m->getControl_pressed()) { return 0; } alignMatrix[i][0] = 15.0 * i; alignMoves[i][0] = 'u'; } for(int i=0;i<=refLength;i++){ if (m->getControl_pressed()) { return 0; } alignMatrix[0][i] = 15.0 * i; alignMoves[0][i] = 'l'; } for(int i=1;i<=queryLength;i++){ if (m->getControl_pressed()) { return 0; } for(int j=1;j<=refLength;j++){ double nogap; nogap = alignMatrix[i-1][j-1] + correctMatrix[toInt(query[i-1])][toInt(reference[j-1])]; double gap; double left; if(i == queryLength){ //terminal gap left = alignMatrix[i][j-1]; } else{ if(reference[j-1] == getLastMatch('l', alignMoves, i, j, query, reference)){ gap = 4.0; } else{ gap = 15.0; } left = alignMatrix[i][j-1] + gap; } double up; if(j == refLength){ //terminal gap up = alignMatrix[i-1][j]; } else{ if(query[i-1] == getLastMatch('u', alignMoves, i, j, query, reference)){ gap = 4.0; } else{ gap = 15.0; } up = alignMatrix[i-1][j] + gap; } if(nogap < left){ if(nogap < up){ alignMoves[i][j] = 'd'; alignMatrix[i][j] = nogap; } else{ alignMoves[i][j] = 'u'; alignMatrix[i][j] = up; } } else{ if(left < up){ alignMoves[i][j] = 'l'; alignMatrix[i][j] = left; } else{ alignMoves[i][j] = 'u'; alignMatrix[i][j] = up; } } } } int i = queryLength; int j = refLength; int alignLength = 0; while(i > 0 && j > 0){ if (m->getControl_pressed()) { return 0; } if(alignMoves[i][j] == 'd'){ qAlign = query[i-1] + qAlign; rAlign = reference[j-1] + rAlign; alignLength++; i--; j--; } else if(alignMoves[i][j] == 'u'){ if(j != refLength){ qAlign = query[i-1] + qAlign; rAlign = '-' + rAlign; alignLength++; } i--; } else if(alignMoves[i][j] == 'l'){ if(i != queryLength){ qAlign = '-' + qAlign; rAlign = reference[j-1] + rAlign; alignLength++; } j--; } } return alignMatrix[queryLength][refLength] / (double)alignLength; } catch(exception& e) { m->errorOut(e, "Perseus", "modeledPairwiseAlignSeqs"); exit(1); } }
int seqNoise::countDiffs(vector<int> query, vector<int> ref){ try { //double MATCH = 5.0; //double MISMATCH = -2.0; //double GAP = -2.0; vector<vector<double> > correctMatrix(4); for(int i=0;i<4;i++){ correctMatrix[i].resize(4); } correctMatrix[0][0] = 0.000000; //AA correctMatrix[1][0] = 11.619259; //CA correctMatrix[2][0] = 11.694004; //TA correctMatrix[3][0] = 7.748623; //GA correctMatrix[1][1] = 0.000000; //CC correctMatrix[2][1] = 7.619657; //TC correctMatrix[3][1] = 12.852562; //GC correctMatrix[2][2] = 0.000000; //TT correctMatrix[3][2] = 10.964048; //TG correctMatrix[3][3] = 0.000000; //GG for(int i=0;i<4;i++){ for(int j=0;j<i;j++){ correctMatrix[j][i] = correctMatrix[i][j]; } } int queryLength = query.size(); int refLength = ref.size(); vector<vector<double> > alignMatrix(queryLength + 1); vector<vector<char> > alignMoves(queryLength + 1); for(int i=0;i<=queryLength;i++){ if (m->control_pressed) { return 0; } alignMatrix[i].resize(refLength + 1, 0); alignMoves[i].resize(refLength + 1, 'x'); } for(int i=0;i<=queryLength;i++){ if (m->control_pressed) { return 0; } alignMatrix[i][0] = 15.0 * i; alignMoves[i][0] = 'u'; } for(int i=0;i<=refLength;i++){ if (m->control_pressed) { return 0; } alignMatrix[0][i] = 15.0 * i; alignMoves[0][i] = 'l'; } for(int i=1;i<=queryLength;i++){ if (m->control_pressed) { return 0; } for(int j=1;j<=refLength;j++){ double nogap; nogap = alignMatrix[i-1][j-1] + correctMatrix[query[i-1]][ref[j-1]]; double gap; double left; if(i == queryLength){ //terminal gap left = alignMatrix[i][j-1]; } else{ if(ref[j-1] == getLastMatch('l', alignMoves, i, j, query, ref)){ gap = 4.0; } else{ gap = 15.0; } left = alignMatrix[i][j-1] + gap; } double up; if(j == refLength){ //terminal gap up = alignMatrix[i-1][j]; } else{ if(query[i-1] == getLastMatch('u', alignMoves, i, j, query, ref)){ gap = 4.0; } else{ gap = 15.0; } up = alignMatrix[i-1][j] + gap; } if(nogap < left){ if(nogap < up){ alignMoves[i][j] = 'd'; alignMatrix[i][j] = nogap; } else{ alignMoves[i][j] = 'u'; alignMatrix[i][j] = up; } } else{ if(left < up){ alignMoves[i][j] = 'l'; alignMatrix[i][j] = left; } else{ alignMoves[i][j] = 'u'; alignMatrix[i][j] = up; } } } } int i = queryLength; int j = refLength; int diffs = 0; // string alignA = ""; // string alignB = ""; // string bases = "ACTG"; while(i > 0 && j > 0){ if (m->control_pressed) { return 0; } if(alignMoves[i][j] == 'd'){ // alignA = bases[query[i-1]] + alignA; // alignB = bases[ref[j-1]] + alignB; if(query[i-1] != ref[j-1]) { diffs++; } i--; j--; } else if(alignMoves[i][j] == 'u'){ if(j != refLength){ // alignA = bases[query[i-1]] + alignA; // alignB = '-' + alignB; diffs++; } i--; } else if(alignMoves[i][j] == 'l'){ if(i != queryLength){ // alignA = '-' + alignA; // alignB = bases[ref[j-1]] + alignB; diffs++; } j--; } } // cout << diffs << endl; // cout << alignA << endl; // cout << alignB << endl; // cout << endl; return diffs; } catch(exception& e) { m->errorOut(e, "seqNoise", "countDiffs"); exit(1); } }
double correctDist::getDist(vector<int>& seqA, vector<int>& seqB){ try { int lengthA = seqA.size(); int lengthB = seqB.size(); vector<vector<double> > alignMatrix(lengthA+1); vector<vector<char> > alignMoves(lengthA+1); for(int i=0;i<=lengthA;i++){ alignMatrix[i].resize(lengthB+1, 0); alignMoves[i].resize(lengthB+1, 'x'); } for(int i=0;i<=lengthA;i++){ alignMatrix[i][0] = 15.0 * i; alignMoves[i][0] = 'u'; } for(int i=0;i<=lengthB;i++){ alignMatrix[0][i] = 15.0 * i; alignMoves[0][i] = 'l'; } for(int i=1;i<=lengthA;i++){ for(int j=1;j<=lengthB;j++){ if (m->control_pressed) { return 0; } double nogap; nogap = alignMatrix[i-1][j-1] + correctMatrix[seqA[i-1]][seqB[j-1]]; double gap; double left; if(i == lengthA){ //terminal gap left = alignMatrix[i][j-1]; } else{ if(seqB[j-1] == getLastMatch('l', alignMoves, i, j, seqA, seqB)){ gap = 4.0; } else{ gap = 15.0; } left = alignMatrix[i][j-1] + gap; } double up; if(j == lengthB){ //terminal gap up = alignMatrix[i-1][j]; } else{ if(seqA[i-1] == getLastMatch('u', alignMoves, i, j, seqA, seqB)){ gap = 4.0; } else{ gap = 15.0; } up = alignMatrix[i-1][j] + gap; } if(nogap < left){ if(nogap < up){ alignMoves[i][j] = 'd'; alignMatrix[i][j] = nogap; } else{ alignMoves[i][j] = 'u'; alignMatrix[i][j] = up; } } else{ if(left < up){ alignMoves[i][j] = 'l'; alignMatrix[i][j] = left; } else{ alignMoves[i][j] = 'u'; alignMatrix[i][j] = up; } } } } int i = lengthA; int j = lengthB; int count = 0; // string alignA = ""; // string alignB = ""; // string bases = "ACTG"; // // for(int i=0;i<lengthA;i++){ // cout << bases[seqA[i]]; // }cout << endl; // // for(int i=0;i<lengthB;i++){ // cout << bases[seqB[i]]; // }cout << endl; while(i > 0 && j > 0){ if (m->control_pressed) { return 0; } if(alignMoves[i][j] == 'd'){ // alignA = bases[seqA[i-1]] + alignA; // alignB = bases[seqB[j-1]] + alignB; count++; i--; j--; } else if(alignMoves[i][j] == 'u'){ if(j != lengthB){ // alignA = bases[seqA[i-1]] + alignA; // alignB = '-' + alignB; count++; } i--; } else if(alignMoves[i][j] == 'l'){ if(i != lengthA){ // alignA = '-' + alignA; // alignB = bases[seqB[j-1]] + alignB; count++; } j--; } } // cout << alignA << endl << alignB << endl; return alignMatrix[lengthA][lengthB] / (double)count; } catch(exception& e) { m->errorOut(e, "correctDist", "getDist"); exit(1); } }