X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=slayer.cpp;h=a787f2727dd8290b89bffeb382bae6c15001110d;hp=6e7f0e3b96eb60773d1bd0fdd2e81d84767160b1;hb=1a20e24ee786195ab0e1cccd4f5aede7a88f3f4e;hpb=58cc09a375d1e1afceef3b036574ff21394ccc4d diff --git a/slayer.cpp b/slayer.cpp index 6e7f0e3..a787f27 100644 --- a/slayer.cpp +++ b/slayer.cpp @@ -10,131 +10,156 @@ #include "slayer.h" /***********************************************************************/ -Slayer::Slayer(int win, int increment, int parentThreshold, float div) : - windowSize(win), windowStep(increment), parentFragmentThreshold(parentThreshold), divRThreshold(div) {} +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 { -cout << "refSeqs = " << refSeqs.size() << endl; vector all; all.clear(); - + myQuery = query; + for (int i = 0; i < refSeqs.size(); i++) { for (int j = i+1; j < refSeqs.size(); j++) { + 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()); - - vector divs = runBellerophon(q, leftParent, rightParent); + 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) { 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) { 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)) { - - 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); - - // 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); + 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].bsMax = max(BS_A, BS_B); + divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra); - divs[k].bsa = BS_A; - divs[k].bsb = BS_B; - divs[k].bsMax = max(BS_A, BS_B); + //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); +// } - 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]; - selectedDivs.push_back(divs[k]); - } + 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"; } } catch(exception& e) { - errorOut(e, "Slayer", "getResults"); + m->errorOut(e, "Slayer", "getResults"); exit(1); } } /***********************************************************************/ -vector Slayer::runBellerophon(Sequence* q, Sequence* pA, Sequence* pB) { +vector Slayer::runBellerophon(Sequence q, Sequence pA, Sequence pB, map& spots) { try{ vector data; -cout << q->getName() << '\t' << q->getAligned().length() << endl; + //vertical filter - vector temp; - temp.push_back(q); temp.push_back(pA); temp.push_back(pB); - verticalFilter(temp); + //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(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() << '\t' << length << 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)) { - mothurOut("Your window size is too large for " + q->getName() + ". I will make the window size " + toString(length/4) + " which is 1/4 the filtered length."); mothurOutEndLine(); +// m->mothurOut("Your window size is too large for " + q->getName() + ". I will make the window size " + toString(length/4) + " which is 1/4 the filtered length."); m->mothurOutEndLine(); windowSize = length / 4; } for (int i = windowSize-1; i <= (length - windowSize); i += windowStep) { + if (m->control_pressed) { return data; } + 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; @@ -146,9 +171,18 @@ cout << q->getName() << '\t' << length << endl; //float avgQA_QB = ((QA*leftLength) + (QB*rightLength)) / (float) length; 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; +//cout << breakpoint << '\t' << divR_QLA_QRB << '\t' << divR_QLB_QRA << endl; //is one of them above the if (divR_QLA_QRB >= divRThreshold || divR_QLB_QRA >= divRThreshold) { @@ -171,12 +205,12 @@ cout << q->getName() << '\t' << length << endl; member.qra = QRA; member.qlb = QLB; member.winLStart = 0; - member.winLEnd = breakpoint; + 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; @@ -188,11 +222,12 @@ cout << q->getName() << '\t' << length << endl; }//if }//for + return data; } catch(exception& e) { - errorOut(e, "Slayer", "runBellerophon"); + m->errorOut(e, "Slayer", "runBellerophon"); exit(1); } } @@ -209,12 +244,30 @@ vector Slayer::getSNPS(string parentA, string query, string parentB, int l char B = parentB[i]; if ((A != Q) || (B != Q)) { - snps member; - member.queryChar = Q; - member.parentAChar = A; - member.parentBChar = B; + + //ensure not neighboring a gap. change to 12/09 release of chimeraSlayer - not sure what this adds, but it eliminates alot of SNPS + - data.push_back(member); + if ( + //did query loose a base here during filter?? + ( i == 0 || 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()-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()-1 || abs (baseSpots[2][i] - baseSpots[2][i+1]) == 1) + ) + { + snps member; + member.queryChar = Q; + member.parentAChar = A; + member.parentBChar = B; + data.push_back(member); + } } } @@ -222,12 +275,12 @@ vector Slayer::getSNPS(string parentA, string query, string parentB, int l } catch(exception& e) { - errorOut(e, "Slayer", "getSNPS"); + m->errorOut(e, "Slayer", "getSNPS"); exit(1); } } /***********************************************************************/ -void 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 )); @@ -235,18 +288,21 @@ void Slayer::bootstrapSNPS(vector left, vector right, float& BSA, fl int count_A = 0; // sceneario QLA,QRB supported int count_B = 0; // sceneario QLB,QRA supported - int numLeft = max(1, int(left.size()/10 +0.5)); - int numRight = max(1, int(right.size()/10 + 0.5)); - - for (int i = 0; i < 100; i++) { + 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 < numIters; i++) { //random sampling with replacement. + if (m->control_pressed) { return 0; } + vector selectedLeft; + for (int j = 0; j < numLeft; j++) { int index = int(rand() % left.size()); selectedLeft.push_back(left[index]); } - + vector selectedRight; for (int j = 0; j < numRight; j++) { int index = int(rand() % right.size()); @@ -268,7 +324,7 @@ void Slayer::bootstrapSNPS(vector left, vector right, float& BSA, fl float QLB = snpQB(selectedLeft); float QRB = snpQB(selectedRight); - + //in original - not used - not sure why? //float ALB = snpAB(selectedLeft); //float ARB = snpAB(selectedRight); @@ -280,15 +336,35 @@ void Slayer::bootstrapSNPS(vector left, vector right, float& BSA, fl if ((QLB > QLA) && (QRA > QRB)) { count_B++; } - + +//cout << "selected left snp: \n"; +//for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentAChar; } +//cout << endl; +//for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].queryChar; } +//cout << endl; +//for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentBChar; } +//cout << endl; +//cout << "selected right snp: \n"; +//for (int j = 0; j < selectedRight.size(); j++) { cout << selectedRight[j].parentAChar; } +//cout << endl; +//for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].queryChar; } +//cout << endl; +//for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].parentBChar; } +//cout << endl; } - BSA = (float) count_A; - BSB = (float) count_B; + + //cout << count_A << '\t' << count_B << endl; + + BSA = (float) count_A / (float) numIters * 100; + BSB = (float) count_B / (float) numIters * 100; +//cout << "bsa = " << BSA << " bsb = " << BSB << endl; + + return 0; } catch(exception& e) { - errorOut(e, "Slayer", "bootstrapSNPS"); + m->errorOut(e, "Slayer", "bootstrapSNPS"); exit(1); } } @@ -304,12 +380,12 @@ float Slayer::snpQA(vector data) { } } - float percentID = (numIdentical / data.size()) * 100; + float percentID = (numIdentical / (float) data.size()) * 100; return percentID; } catch(exception& e) { - errorOut(e, "Slayer", "snpQA"); + m->errorOut(e, "Slayer", "snpQA"); exit(1); } } @@ -325,13 +401,13 @@ float Slayer::snpQB(vector data) { } } - float percentID = (numIdentical / data.size()) * 100; + float percentID = (numIdentical / (float) data.size()) * 100; return percentID; } catch(exception& e) { - errorOut(e, "Slayer", "snpQB"); + m->errorOut(e, "Slayer", "snpQB"); exit(1); } } @@ -346,80 +422,165 @@ float Slayer::snpAB(vector data) { } } - float percentID = (numIdentical / data.size()) * 100; + float percentID = (numIdentical / (float) data.size()) * 100; return percentID; } catch(exception& e) { - errorOut(e, "Slayer", "snpAB"); + m->errorOut(e, "Slayer", "snpAB"); exit(1); } } /***********************************************************************/ -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) { - errorOut(e, "Slayer", "computePercentID"); + m->errorOut(e, "Slayer", "computePercentID"); exit(1); } } /***********************************************************************/ //remove columns that contain any gaps -void Slayer::verticalFilter(vector seqs) { +map Slayer::verticalFilter(Sequence& q, Sequence& pA, Sequence& pB) { try { - vector gaps; gaps.resize(seqs[0]->getAligned().length(), 0); + //find baseSpots + baseSpots.clear(); + baseSpots.resize(3); //query, parentA, parentB + + 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 all sequences have blanks + + //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; + count++; + } } - //for each sequence - for (int i = 0; i < seqs.size(); i++) { + seqAligned = q.getAligned(); + string newAligned = ""; + + 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[0][count] = baseCount; + count++; + } + } + + q.setAligned(newAligned); + + seqAligned = pA.getAligned(); + newAligned = ""; - string seqAligned = seqs[i]->getAligned(); - string 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++; } - for (int j = 0; j < seqAligned.length(); j++) { - //if this spot is not a gap - if (filterString[j] == '1') { newAligned += seqAligned[j]; } + //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++; } - seqs[i]->setAligned(newAligned); + //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) { - errorOut(e, "Slayer", "verticalFilter"); + m->errorOut(e, "Slayer", "verticalFilter"); exit(1); } }