X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=slayer.cpp;h=a244ea42634fc9b31218aceaafd3d76f249d92e8;hb=1cf188b912d6da8f2cd03dd71cecef664a699c1a;hp=66db22b6a36ae9a0f7f9dd383201cd3852a120b2;hpb=5a1e62397b91f57d0d3aff635891df04b8999a88;p=mothur.git diff --git a/slayer.cpp b/slayer.cpp index 66db22b..a244ea4 100644 --- a/slayer.cpp +++ b/slayer.cpp @@ -10,71 +10,79 @@ #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){} +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) { try { 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()); - + 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"; } + 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"; } 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 ) { + //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) { 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); + + + //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); - - //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]; + } + + //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]); } + for (int mi = 0; mi < selectedDivs.size(); mi++) { all.push_back(selectedDivs[mi]); } delete q; delete leftParent; @@ -82,23 +90,21 @@ string Slayer::getResults(Sequence* query, vector refSeqs) { } } - // 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); } } @@ -121,21 +127,20 @@ vector Slayer::runBellerophon(Sequence* q, Sequence* pA, Sequence* 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 << pA->getName() << endl << pA->getUnaligned() << endl << endl; +//cout << pB->getName() << endl << pB->getUnaligned() << endl << endl; //cout << " length = " << length << endl; -//cout << q->getName() << endl; -//cout << pA->getName() << '\t'; -//cout << pB->getName() << 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; @@ -161,6 +166,11 @@ vector Slayer::runBellerophon(Sequence* q, Sequence* pA, Sequence* 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; @@ -208,7 +218,7 @@ vector Slayer::runBellerophon(Sequence* q, Sequence* pA, Sequence* } catch(exception& e) { - errorOut(e, "Slayer", "runBellerophon"); + m->errorOut(e, "Slayer", "runBellerophon"); exit(1); } } @@ -251,18 +261,19 @@ vector Slayer::getSNPS(string parentA, string query, string parentB, int l data.push_back(member); } } +// cout << i << '\t' << data.size() << endl; } return data; } 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 )); @@ -270,12 +281,14 @@ 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() * (percentSNPSample/(float)100) + 0.5)); - int numRight = max(1, int(right.size() * (percentSNPSample/(float)100) + 0.5)); - - for (int i = 0; i < iters; 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)); + //cout << numLeft << '\t' << numRight << endl; + 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++) { @@ -334,15 +347,17 @@ void Slayer::bootstrapSNPS(vector left, vector right, float& BSA, fl } + //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; } catch(exception& e) { - errorOut(e, "Slayer", "bootstrapSNPS"); + m->errorOut(e, "Slayer", "bootstrapSNPS"); exit(1); } } @@ -363,7 +378,7 @@ float Slayer::snpQA(vector data) { return percentID; } catch(exception& e) { - errorOut(e, "Slayer", "snpQA"); + m->errorOut(e, "Slayer", "snpQA"); exit(1); } } @@ -385,7 +400,7 @@ float Slayer::snpQB(vector data) { } catch(exception& e) { - errorOut(e, "Slayer", "snpQB"); + m->errorOut(e, "Slayer", "snpQB"); exit(1); } } @@ -406,29 +421,49 @@ float Slayer::snpAB(vector data) { } 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 percentID =( matches/(float)total) * 100; - return percentID; + float numBases = (countA + countB) /(float) 2; + + if (numBases == 0) { return 0; } + + float percentIdentical = (numIdentical/(float)numBases) * 100; + + return percentIdentical; + } catch(exception& e) { - errorOut(e, "Slayer", "computePercentID"); + m->errorOut(e, "Slayer", "computePercentID"); exit(1); } } @@ -494,7 +529,7 @@ map Slayer::verticalFilter(vector seqs) { return maskMap; } catch(exception& e) { - errorOut(e, "Slayer", "verticalFilter"); + m->errorOut(e, "Slayer", "verticalFilter"); exit(1); } }