X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=slayer.cpp;h=a787f2727dd8290b89bffeb382bae6c15001110d;hp=fab457bc8f3e7948271190a55ac519ef0d1cd06d;hb=df7e3ff9f68ef157b0328a2d353c3258c5d45d89;hpb=2b96bd74f70764a8b51436883afdc871fe826762 diff --git a/slayer.cpp b/slayer.cpp index fab457b..a787f27 100644 --- a/slayer.cpp +++ b/slayer.cpp @@ -10,13 +10,14 @@ #include "slayer.h" /***********************************************************************/ -Slayer::Slayer(int win, int increment, int parentThreshold, float div, int i, int snp) : - windowSize(win), windowStep(increment), parentFragmentThreshold(parentThreshold), divRThreshold(div), iters(i), percentSNPSample(snp){ m = MothurOut::getInstance(); } +Slayer::Slayer(int win, int increment, int parentThreshold, float div, int i, int snp, int mi) : + minBS(mi), windowSize(win), windowStep(increment), parentFragmentThreshold(parentThreshold), divRThreshold(div), iters(i), percentSNPSample(snp){ m = MothurOut::getInstance(); } /***********************************************************************/ -string Slayer::getResults(Sequence* query, vector refSeqs) { +string Slayer::getResults(Sequence query, vector refSeqs) { try { vector all; all.clear(); - + myQuery = query; + for (int i = 0; i < refSeqs.size(); i++) { for (int j = i+1; j < refSeqs.size(); j++) { @@ -24,98 +25,85 @@ string Slayer::getResults(Sequence* query, vector refSeqs) { if (m->control_pressed) { return "no"; } //make copies of query and each parent because runBellerophon removes gaps and messes them up - Sequence* q = new Sequence(query->getName(), query->getAligned()); - Sequence* leftParent = new Sequence(refSeqs[i]->getName(), refSeqs[i]->getAligned()); - Sequence* rightParent = new Sequence(refSeqs[j]->getName(), refSeqs[j]->getAligned()); - + Sequence q(query.getName(), query.getAligned()); + Sequence leftParent(refSeqs[i].getName(), refSeqs[i].getAligned()); + Sequence rightParent(refSeqs[j].getName(), refSeqs[j].getAligned()); + + //cout << q->getName() << endl << q->getAligned() << endl << endl; + //cout << leftParent.getName() << '\t' << leftParent.getAligned().length() << endl << endl; + //cout << rightParent.getName() << '\t' << rightParent.getAligned().length() << endl << endl; + //cout << q.getName() << '\t' << q.getAligned().length() << endl << endl; + //cout << rightParent->getName() << endl << rightParent->getAligned() << endl << endl; + //cout << " length = " << rightParent->getAligned().length() << endl; + map spots; //map from spot in original sequence to spot in filtered sequence for query and both parents vector divs = runBellerophon(q, leftParent, rightParent, spots); - - if (m->control_pressed) { - delete q; - delete leftParent; - delete rightParent; - return "no"; - } - -// cout << divs.size() << endl; + + if (m->control_pressed) { return "no"; } +// cout << "examining:\t" << refSeqs[i]->getName() << '\t' << refSeqs[j]->getName() << endl; vector selectedDivs; for (int k = 0; k < divs.size(); k++) { vector snpsLeft = getSNPS(divs[k].parentA.getAligned(), divs[k].querySeq.getAligned(), divs[k].parentB.getAligned(), divs[k].winLStart, divs[k].winLEnd); vector snpsRight = getSNPS(divs[k].parentA.getAligned(), divs[k].querySeq.getAligned(), divs[k].parentB.getAligned(), divs[k].winRStart, divs[k].winREnd); - - if (m->control_pressed) { - delete q; - delete leftParent; - delete rightParent; - return "no"; - } + + if (m->control_pressed) { return "no"; } int numSNPSLeft = snpsLeft.size(); int numSNPSRight = snpsRight.size(); - //require at least 3 SNPs on each side of the break - if ((numSNPSLeft >= 3) && (numSNPSRight >= 3)) { - - //removed in 12/09 version of chimeraSlayer - //int winSizeLeft = divs[k].winLEnd - divs[k].winLStart + 1; - //int winSizeRight = divs[k].winREnd - divs[k].winRStart + 1; - - //float snpRateLeft = numSNPSLeft / (float) winSizeLeft; - //float snpRateRight = numSNPSRight / (float) winSizeRight; - //float logR = log(snpRateLeft / snpRateRight) / log(2.0); - - // do not accept excess snp ratio on either side of the break - //if (abs(logR) < 1 ) { +// cout << numSNPSLeft << '\t' << numSNPSRight << endl; + //require at least 4 SNPs on each side of the break + if ((numSNPSLeft >= 4) && (numSNPSRight >= 4)) { - float BS_A, BS_B; - bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B); - - if (m->control_pressed) { - delete q; - delete leftParent; - delete rightParent; - return "no"; - } + float BS_A, BS_B; + bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B, iters); + + if (m->control_pressed) { return "no"; } - divs[k].bsa = BS_A; - divs[k].bsb = BS_B; + divs[k].bsa = BS_A; + divs[k].bsb = BS_B; + divs[k].bsMax = max(BS_A, BS_B); + divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra); - divs[k].bsMax = max(BS_A, BS_B); - divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra); - - //so results reflect orignal alignment - divs[k].winLStart = spots[divs[k].winLStart]; - divs[k].winLEnd = spots[divs[k].winLEnd]; - divs[k].winRStart = spots[divs[k].winRStart]; - divs[k].winREnd = spots[divs[k].winREnd]; + //are we within 10 points of the bootstrap cutoff? +// if ((divs[k].bsMax >= (minBS-10)) && (iters < 1000)) { +// bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B, 1000); +// +// if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; } +// +// divs[k].bsa = BS_A; +// divs[k].bsb = BS_B; +// divs[k].bsMax = max(BS_A, BS_B); +// divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra); +// } - selectedDivs.push_back(divs[k]); - //} + //so results reflect orignal alignment + divs[k].winLStart = spots[divs[k].winLStart]; + divs[k].winLEnd = spots[divs[k].winLEnd]; + divs[k].winRStart = spots[divs[k].winRStart]; + divs[k].winREnd = spots[divs[k].winREnd]; + + selectedDivs.push_back(divs[k]); } } - + //save selected - for (int m = 0; m < selectedDivs.size(); m++) { all.push_back(selectedDivs[m]); } - - delete q; - delete leftParent; - delete rightParent; + for (int mi = 0; mi < selectedDivs.size(); mi++) { all.push_back(selectedDivs[mi]); } } } + // compute bootstrap support if (all.size() > 0) { //sort them sort(all.begin(), all.end(), compareDataStruct); reverse(all.begin(), all.end()); - + outputResults = all; return "yes"; - } - else { + }else { outputResults = all; return "no"; } @@ -126,30 +114,28 @@ string Slayer::getResults(Sequence* query, vector refSeqs) { } } /***********************************************************************/ -vector Slayer::runBellerophon(Sequence* q, Sequence* pA, Sequence* pB, map& spots) { +vector Slayer::runBellerophon(Sequence q, Sequence pA, Sequence pB, map& spots) { try{ vector data; //vertical filter - vector temp; - temp.push_back(q); temp.push_back(pA); temp.push_back(pB); + //cout << q.getName() << endl << q.getAligned() << endl << endl; + //cout << pA.getName() << endl << pA.getUnaligned() << endl << endl; + //cout << pB.getName() << endl << pB.getUnaligned() << endl << endl; //maps spot in new alignment to spot in alignment before filter - spots = verticalFilter(temp); //fills baseSpots + spots = verticalFilter(q, pA, pB); //fills baseSpots //get these to avoid numerous function calls - string query = q->getAligned(); - string parentA = pA->getAligned(); - string parentB = pB->getAligned(); + string query = q.getAligned(); + string parentA = pA.getAligned(); + string parentB = pB.getAligned(); int length = query.length(); -/*cout << q->getName() << endl << q->getAligned() << endl << endl; -cout << pA->getName() << endl << pA->getAligned() << endl << endl; -cout << pB->getName() << endl << pB->getAligned() << endl << endl; -cout << " length = " << length << endl; -cout << q->getName() << endl; -cout << pA->getName() << '\t'; -cout << pB->getName() << endl;*/ +//cout << q.getName() << endl << q.getAligned() << endl << endl; +//cout << pA.getName() << endl << pA.getUnaligned() << endl << endl; +//cout << pB.getName() << endl << pB.getUnaligned() << endl << endl; +//cout << " length = " << length << endl; //check window size if (length < (2*windowSize+windowStep)) { @@ -164,16 +150,16 @@ cout << pB->getName() << endl;*/ int breakpoint = i; int leftLength = breakpoint + 1; int rightLength = length - leftLength; - + float QLA = computePercentID(query, parentA, 0, breakpoint); - float QRB = computePercentID(query, parentB, breakpoint+1, length - 1); + float QRB = computePercentID(query, parentB, breakpoint+1, length-1); float QLB = computePercentID(query, parentB, 0, breakpoint); - float QRA = computePercentID(query, parentA, breakpoint+1, length - 1); + float QRA = computePercentID(query, parentA, breakpoint+1, length-1); float LAB = computePercentID(parentA, parentB, 0, breakpoint); - float RAB = computePercentID(parentA, parentB, breakpoint+1, length - 1); - + float RAB = computePercentID(parentA, parentB, breakpoint+1, length-1); + float AB = ((LAB*leftLength) + (RAB*rightLength)) / (float) length; float QA = ((QLA*leftLength) + (QRA*rightLength)) / (float) length; float QB = ((QLB*leftLength) + (QRB*rightLength)) / (float) length; @@ -186,6 +172,13 @@ cout << pB->getName() << endl;*/ float divR_QLA_QRB = min((QLA_QRB/QA), (QLA_QRB/QB)); float divR_QLB_QRA = min((QLB_QRA/QA), (QLB_QRA/QB)); + + + //cout << q->getName() << '\t'; + //cout << pA->getName() << '\t'; + //cout << pB->getName() << '\t'; + //cout << "bp: " << breakpoint << " CHIM_TYPE_A\t" << divR_QLA_QRB << "\tQLA: " << QLA << "\tQRB: " << QRB << "\tQLA_QRB: " << QLA_QRB; + //cout << "\tCHIM_TYPE_B\t" << divR_QLB_QRA << "\tQLB: " << QLB << "\tQRA: " << QRA << "\tQLB_QRA: " << QLB_QRA << endl; //cout << leftLength << '\t' << rightLength << '\t' << QLA << '\t' << QRB << '\t' << QLB << '\t' << QRA << '\t' << LAB << '\t' << RAB << '\t' << AB << '\t' << QA << '\t' << QB << '\t' << QLA_QRB << '\t' << QLB_QRA << endl; //cout << divRThreshold << endl; @@ -215,9 +208,9 @@ cout << pB->getName() << endl;*/ member.winLEnd = breakpoint; member.winRStart = breakpoint+1; member.winREnd = length-1; - member.querySeq = *(q); - member.parentA = *(pA); - member.parentB = *(pB); + member.querySeq = q; + member.parentA = pA; + member.parentB = pB; member.bsa = 0; member.bsb = 0; member.bsMax = 0; @@ -229,6 +222,7 @@ cout << pB->getName() << endl;*/ }//if }//for + return data; } @@ -242,7 +236,7 @@ vector Slayer::getSNPS(string parentA, string query, string parentB, int l try { vector data; -//cout << left << '\t' << right << endl; + for (int i = left; i <= right; i++) { char A = parentA[i]; @@ -250,33 +244,31 @@ vector Slayer::getSNPS(string parentA, string query, string parentB, int l char B = parentB[i]; if ((A != Q) || (B != Q)) { -//cout << "not equal " << Q << '\t' << A << '\t' << B << endl; - + //ensure not neighboring a gap. change to 12/09 release of chimeraSlayer - not sure what this adds, but it eliminates alot of SNPS + + if ( //did query loose a base here during filter?? ( i == 0 || abs (baseSpots[0][i] - baseSpots[0][i-1]) == 1) && - ( i == query.length() || abs (baseSpots[0][i] - baseSpots[0][i+1]) == 1) + ( i == query.length()-1 || abs (baseSpots[0][i] - baseSpots[0][i+1]) == 1) && //did parentA loose a base here during filter?? ( i == 0 || abs (baseSpots[1][i] - baseSpots[1][i-1]) == 1) && - ( i == parentA.length() || abs (baseSpots[1][i] - baseSpots[1][i+1]) == 1) + ( i == parentA.length()-1 || abs (baseSpots[1][i] - baseSpots[1][i+1]) == 1) && //did parentB loose a base here during filter?? ( i == 0 || abs (baseSpots[2][i] - baseSpots[2][i-1]) == 1) && - ( i == parentB.length() || abs (baseSpots[2][i] - baseSpots[2][i+1]) == 1) + ( i == parentB.length()-1 || abs (baseSpots[2][i] - baseSpots[2][i+1]) == 1) ) { - snps member; member.queryChar = Q; member.parentAChar = A; member.parentBChar = B; -//cout << "not neighboring a gap " << Q << '\t' << A << '\t' << B << '\t' << baseSpots[0][i] << '\t' << baseSpots[0][i+1] << '\t' << baseSpots[0][i-1] << '\t' << baseSpots[1][i] << '\t' << baseSpots[1][i+1] << '\t' << baseSpots[1][i-1] << '\t' << baseSpots[2][i] << '\t' << baseSpots[2][i+1] << '\t' << baseSpots[2][i-1] << endl; data.push_back(member); } } -// cout << i << '\t' << data.size() << endl; } return data; @@ -288,7 +280,7 @@ vector Slayer::getSNPS(string parentA, string query, string parentB, int l } } /***********************************************************************/ -int Slayer::bootstrapSNPS(vector left, vector right, float& BSA, float& BSB) { +int Slayer::bootstrapSNPS(vector left, vector right, float& BSA, float& BSB, int numIters) { try { srand((unsigned)time( NULL )); @@ -296,10 +288,10 @@ int Slayer::bootstrapSNPS(vector left, vector right, float& BSA, flo int count_A = 0; // sceneario QLA,QRB supported int count_B = 0; // sceneario QLB,QRA supported - int numLeft = max(1, int(left.size() * (percentSNPSample/(float)100) + 0.5)); - int numRight = max(1, int(right.size() * (percentSNPSample/(float)100) + 0.5)); + int numLeft = max(1, int(left.size() * percentSNPSample/(float)100 + 0.5)); + int numRight = max(1, int(right.size() * percentSNPSample/(float)100 + 0.5)); - for (int i = 0; i < iters; i++) { + for (int i = 0; i < numIters; i++) { //random sampling with replacement. if (m->control_pressed) { return 0; } @@ -362,10 +354,10 @@ int Slayer::bootstrapSNPS(vector left, vector right, float& BSA, flo } + //cout << count_A << '\t' << count_B << endl; - - BSA = ((float) count_A / (float) iters) * 100; - BSB = ((float) count_B / (float) iters) * 100; + BSA = (float) count_A / (float) numIters * 100; + BSB = (float) count_B / (float) numIters * 100; //cout << "bsa = " << BSA << " bsb = " << BSB << endl; return 0; @@ -441,21 +433,42 @@ float Slayer::snpAB(vector data) { } } /***********************************************************************/ -float Slayer::computePercentID(string queryFrag, string parent, int left, int right) { +float Slayer::computePercentID(string queryAlign, string chimera, int left, int right) { try { - int total = 0; - int matches = 0; - + + int numIdentical = 0; + int countA = 0; + int countB = 0; for (int i = left; i <= right; i++) { - total++; - if (queryFrag[i] == parent[i]) { - matches++; + if (((queryAlign[i] != 'G') && (queryAlign[i] != 'T') && (queryAlign[i] != 'A') && (queryAlign[i] != 'C')&& (queryAlign[i] != '.') && (queryAlign[i] != '-')) || + ((chimera[i] != 'G') && (chimera[i] != 'T') && (chimera[i] != 'A') && (chimera[i] != 'C')&& (chimera[i] != '.') && (chimera[i] != '-'))) {} + else { + + bool charA = false; bool charB = false; + if ((queryAlign[i] == 'G') || (queryAlign[i] == 'T') || (queryAlign[i] == 'A') || (queryAlign[i] == 'C')) { charA = true; } + if ((chimera[i] == 'G') || (chimera[i] == 'T') || (chimera[i] == 'A') || (chimera[i] == 'C')) { charB = true; } + + if (charA || charB) { + + if (charA) { countA++; } + if (charB) { countB++; } + + if (queryAlign[i] == chimera[i]) { + numIdentical++; + } + } } + } + + float numBases = (countA + countB) /(float) 2; + + if (numBases == 0) { return 0; } + + float percentIdentical = (numIdentical/(float)numBases) * 100; - float percentID =( matches/(float)total) * 100; + return percentIdentical; - return percentID; } catch(exception& e) { m->errorOut(e, "Slayer", "computePercentID"); @@ -464,33 +477,41 @@ float Slayer::computePercentID(string queryFrag, string parent, int left, int ri } /***********************************************************************/ //remove columns that contain any gaps -map Slayer::verticalFilter(vector seqs) { +map Slayer::verticalFilter(Sequence& q, Sequence& pA, Sequence& pB) { try { //find baseSpots baseSpots.clear(); baseSpots.resize(3); //query, parentA, parentB - vector gaps; gaps.resize(seqs[0]->getAligned().length(), 0); + vector gaps; gaps.resize(q.getAligned().length(), 0); - string filterString = (string(seqs[0]->getAligned().length(), '1')); + string filterString = (string(q.getAligned().length(), '1')); - //for each sequence - for (int i = 0; i < seqs.size(); i++) { + string seqAligned = q.getAligned(); + for (int j = 0; j < seqAligned.length(); j++) { + //if this spot is a gap + if ((seqAligned[j] == '-') || (seqAligned[j] == '.') || (toupper(seqAligned[j]) == 'N')) { gaps[j]++; } + } - string seqAligned = seqs[i]->getAligned(); - - for (int j = 0; j < seqAligned.length(); j++) { - //if this spot is a gap - if ((seqAligned[j] == '-') || (seqAligned[j] == '.') || (toupper(seqAligned[j]) == 'N')) { gaps[j]++; } - } + seqAligned = pA.getAligned(); + for (int j = 0; j < seqAligned.length(); j++) { + //if this spot is a gap + if ((seqAligned[j] == '-') || (seqAligned[j] == '.') || (toupper(seqAligned[j]) == 'N')) { gaps[j]++; } + } + + seqAligned = pB.getAligned(); + for (int j = 0; j < seqAligned.length(); j++) { + //if this spot is a gap + if ((seqAligned[j] == '-') || (seqAligned[j] == '.') || (toupper(seqAligned[j]) == 'N')) { gaps[j]++; } } + //zero out spot where any sequences have blanks int numColRemoved = 0; int count = 0; map maskMap; maskMap.clear(); - for(int i = 0; i < seqs[0]->getAligned().length(); i++){ + for(int i = 0; i < q.getAligned().length(); i++){ if(gaps[i] != 0) { filterString[i] = '0'; numColRemoved++; } else { maskMap[count] = i; @@ -498,29 +519,64 @@ map Slayer::verticalFilter(vector seqs) { } } - //for each sequence - for (int i = 0; i < seqs.size(); i++) { - - string seqAligned = seqs[i]->getAligned(); - string newAligned = ""; + seqAligned = q.getAligned(); + string newAligned = ""; - int baseCount = 0; - int count = 0; - for (int j = 0; j < seqAligned.length(); j++) { - //are you a base - if ((seqAligned[j] != '-') && (seqAligned[j] != '.') && (toupper(seqAligned[j]) != 'N')) { baseCount++; } + int baseCount = 0; + count = 0; + for (int j = 0; j < seqAligned.length(); j++) { + //are you a base + if ((seqAligned[j] != '-') && (seqAligned[j] != '.') && (toupper(seqAligned[j]) != 'N')) { baseCount++; } - //if this spot is not a gap - if (filterString[j] == '1') { - newAligned += seqAligned[j]; - baseSpots[i][count] = baseCount; - count++; - } + //if this spot is not a gap + if (filterString[j] == '1') { + newAligned += seqAligned[j]; + baseSpots[0][count] = baseCount; + count++; } + } - seqs[i]->setAligned(newAligned); + q.setAligned(newAligned); + + seqAligned = pA.getAligned(); + newAligned = ""; + + baseCount = 0; + count = 0; + for (int j = 0; j < seqAligned.length(); j++) { + //are you a base + if ((seqAligned[j] != '-') && (seqAligned[j] != '.') && (toupper(seqAligned[j]) != 'N')) { baseCount++; } + + //if this spot is not a gap + if (filterString[j] == '1') { + newAligned += seqAligned[j]; + baseSpots[1][count] = baseCount; + count++; + } } + pA.setAligned(newAligned); + + seqAligned = pB.getAligned(); + newAligned = ""; + + baseCount = 0; + count = 0; + for (int j = 0; j < seqAligned.length(); j++) { + //are you a base + if ((seqAligned[j] != '-') && (seqAligned[j] != '.') && (toupper(seqAligned[j]) != 'N')) { baseCount++; } + + //if this spot is not a gap + if (filterString[j] == '1') { + newAligned += seqAligned[j]; + baseSpots[2][count] = baseCount; + count++; + } + } + + pB.setAligned(newAligned); + + return maskMap; } catch(exception& e) {