5 * Created by westcott on 9/25/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
12 /***********************************************************************/
13 Slayer::Slayer(int win, int increment, int parentThreshold, float div, int i, int snp, int mi) :
14 minBS(mi), windowSize(win), windowStep(increment), parentFragmentThreshold(parentThreshold), divRThreshold(div), iters(i), percentSNPSample(snp){ m = MothurOut::getInstance(); }
15 /***********************************************************************/
16 string Slayer::getResults(Sequence* query, vector<Sequence*> refSeqs) {
18 vector<data_struct> all; all.clear();
21 for (int i = 0; i < refSeqs.size(); i++) {
23 for (int j = i+1; j < refSeqs.size(); j++) {
25 if (m->control_pressed) { return "no"; }
27 //make copies of query and each parent because runBellerophon removes gaps and messes them up
28 Sequence* q = new Sequence(query->getName(), query->getAligned());
29 Sequence* leftParent = new Sequence(refSeqs[i]->getName(), refSeqs[i]->getAligned());
30 Sequence* rightParent = new Sequence(refSeqs[j]->getName(), refSeqs[j]->getAligned());
32 map<int, int> spots; //map from spot in original sequence to spot in filtered sequence for query and both parents
33 vector<data_struct> divs = runBellerophon(q, leftParent, rightParent, spots);
35 if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
37 vector<data_struct> selectedDivs;
38 for (int k = 0; k < divs.size(); k++) {
40 vector<snps> snpsLeft = getSNPS(divs[k].parentA.getAligned(), divs[k].querySeq.getAligned(), divs[k].parentB.getAligned(), divs[k].winLStart, divs[k].winLEnd);
41 vector<snps> snpsRight = getSNPS(divs[k].parentA.getAligned(), divs[k].querySeq.getAligned(), divs[k].parentB.getAligned(), divs[k].winRStart, divs[k].winREnd);
43 if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
45 int numSNPSLeft = snpsLeft.size();
46 int numSNPSRight = snpsRight.size();
48 //require at least 4 SNPs on each side of the break
49 if ((numSNPSLeft >= 4) && (numSNPSRight >= 4)) {
52 bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B, iters);
54 if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
58 divs[k].bsMax = max(BS_A, BS_B);
59 divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra);
62 //are we within 10 points of the bootstrap cutoff?
63 if ((divs[k].bsMax >= (minBS-10)) && (iters < 1000)) {
64 bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B, 1000);
66 if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
70 divs[k].bsMax = max(BS_A, BS_B);
71 divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra);
74 //so results reflect orignal alignment
75 divs[k].winLStart = spots[divs[k].winLStart];
76 divs[k].winLEnd = spots[divs[k].winLEnd];
77 divs[k].winRStart = spots[divs[k].winRStart];
78 divs[k].winREnd = spots[divs[k].winREnd];
80 selectedDivs.push_back(divs[k]);
85 for (int mi = 0; mi < selectedDivs.size(); mi++) { all.push_back(selectedDivs[mi]); }
94 // compute bootstrap support
97 sort(all.begin(), all.end(), compareDataStruct);
98 reverse(all.begin(), all.end());
107 catch(exception& e) {
108 m->errorOut(e, "Slayer", "getResults");
112 /***********************************************************************/
113 vector<data_struct> Slayer::runBellerophon(Sequence* q, Sequence* pA, Sequence* pB, map<int, int>& spots) {
116 vector<data_struct> data;
119 vector<Sequence*> temp;
120 temp.push_back(q); temp.push_back(pA); temp.push_back(pB);
122 //maps spot in new alignment to spot in alignment before filter
123 spots = verticalFilter(temp); //fills baseSpots
125 //get these to avoid numerous function calls
126 string query = q->getAligned();
127 string parentA = pA->getAligned();
128 string parentB = pB->getAligned();
129 int length = query.length();
130 //cout << q->getName() << endl << q->getAligned() << endl << endl;
131 //cout << pA->getName() << endl << pA->getUnaligned() << endl << endl;
132 //cout << pB->getName() << endl << pB->getUnaligned() << endl << endl;
133 //cout << " length = " << length << endl;
136 if (length < (2*windowSize+windowStep)) {
137 // 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();
138 windowSize = length / 4;
141 for (int i = windowSize-1; i <= (length - windowSize); i += windowStep) {
143 if (m->control_pressed) { return data; }
146 int leftLength = breakpoint + 1;
147 int rightLength = length - leftLength;
149 float QLA = computePercentID(query, parentA, 0, breakpoint);
150 float QRB = computePercentID(query, parentB, breakpoint+1, length);
152 float QLB = computePercentID(query, parentB, 0, breakpoint);
153 float QRA = computePercentID(query, parentA, breakpoint+1, length);
155 float LAB = computePercentID(parentA, parentB, 0, breakpoint);
156 float RAB = computePercentID(parentA, parentB, breakpoint+1, length);
158 float AB = ((LAB*leftLength) + (RAB*rightLength)) / (float) length;
159 float QA = ((QLA*leftLength) + (QRA*rightLength)) / (float) length;
160 float QB = ((QLB*leftLength) + (QRB*rightLength)) / (float) length;
162 float QLA_QRB = ((QLA*leftLength) + (QRB*rightLength)) / (float) length;
163 float QLB_QRA = ((QLB*leftLength) + (QRA*rightLength)) / (float) length;
165 //in original and not used
166 //float avgQA_QB = ((QA*leftLength) + (QB*rightLength)) / (float) length;
168 float divR_QLA_QRB = min((QLA_QRB/QA), (QLA_QRB/QB));
169 float divR_QLB_QRA = min((QLB_QRA/QA), (QLB_QRA/QB));
171 //cout << q->getName() << '\t';
172 //cout << pA->getName() << '\t';
173 //cout << pB->getName() << '\t';
174 // cout << "bp: " << breakpoint << " CHIM_TYPE_A\t" << divR_QLA_QRB << "\tQLA: " << QLA << "\tQRB: " << QRB << "\tQLA_QRB: " << QLA_QRB;
175 //cout << "\tCHIM_TYPE_B\t" << divR_QLB_QRA << "\tQLB: " << QLB << "\tQRA: " << QRA << "\tQLB_QRA: " << QLB_QRA << endl;
176 //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;
178 //cout << divRThreshold << endl;
179 //cout << breakpoint << '\t' << divR_QLA_QRB << '\t' << divR_QLB_QRA << endl;
180 //is one of them above the
181 if (divR_QLA_QRB >= divRThreshold || divR_QLB_QRA >= divRThreshold) {
183 if (((QLA_QRB > QA) && (QLA_QRB > QB) && (QLA >= parentFragmentThreshold) && (QRB >= parentFragmentThreshold)) ||
184 ((QLB_QRA > QA) && (QLB_QRA > QB) && (QLB >=parentFragmentThreshold) && (QRA >= parentFragmentThreshold))) {
188 member.divr_qla_qrb = divR_QLA_QRB;
189 member.divr_qlb_qra = divR_QLB_QRA;
190 member.qla_qrb = QLA_QRB;
191 member.qlb_qra = QLB_QRA;
201 member.winLStart = 0;
202 member.winLEnd = breakpoint;
203 member.winRStart = breakpoint+1;
204 member.winREnd = length-1;
205 member.querySeq = *(q);
206 member.parentA = *(pA);
207 member.parentB = *(pB);
211 member.chimeraMax = 0;
213 data.push_back(member);
222 catch(exception& e) {
223 m->errorOut(e, "Slayer", "runBellerophon");
227 /***********************************************************************/
228 vector<snps> Slayer::getSNPS(string parentA, string query, string parentB, int left, int right) {
233 for (int i = left; i <= right; i++) {
239 if ((A != Q) || (B != Q)) {
241 //ensure not neighboring a gap. change to 12/09 release of chimeraSlayer - not sure what this adds, but it eliminates alot of SNPS
245 //did query loose a base here during filter??
246 ( i == 0 || abs (baseSpots[0][i] - baseSpots[0][i-1]) == 1) &&
247 ( i == query.length()-1 || abs (baseSpots[0][i] - baseSpots[0][i+1]) == 1)
249 //did parentA loose a base here during filter??
250 ( i == 0 || abs (baseSpots[1][i] - baseSpots[1][i-1]) == 1) &&
251 ( i == parentA.length()-1 || abs (baseSpots[1][i] - baseSpots[1][i+1]) == 1)
253 //did parentB loose a base here during filter??
254 ( i == 0 || abs (baseSpots[2][i] - baseSpots[2][i-1]) == 1) &&
255 ( i == parentB.length()-1 || abs (baseSpots[2][i] - baseSpots[2][i+1]) == 1)
259 member.queryChar = Q;
260 member.parentAChar = A;
261 member.parentBChar = B;
262 data.push_back(member);
270 catch(exception& e) {
271 m->errorOut(e, "Slayer", "getSNPS");
275 /***********************************************************************/
276 int Slayer::bootstrapSNPS(vector<snps> left, vector<snps> right, float& BSA, float& BSB, int numIters) {
279 srand((unsigned)time( NULL ));
281 int count_A = 0; // sceneario QLA,QRB supported
282 int count_B = 0; // sceneario QLB,QRA supported
284 int numLeft = max(1, int(left.size() * percentSNPSample/(float)100 + 0.5));
285 int numRight = max(1, int(right.size() * percentSNPSample/(float)100 + 0.5));
287 for (int i = 0; i < numIters; i++) {
288 //random sampling with replacement.
290 if (m->control_pressed) { return 0; }
292 vector<snps> selectedLeft;
294 for (int j = 0; j < numLeft; j++) {
295 int index = int(rand() % left.size());
296 selectedLeft.push_back(left[index]);
299 vector<snps> selectedRight;
300 for (int j = 0; j < numRight; j++) {
301 int index = int(rand() % right.size());
302 selectedRight.push_back(right[index]);
305 /* A ------------------------------------------
307 # Q ------------------------------------------
310 # Q ------------------------------------------
312 # B ------------------------------------------ */
315 float QLA = snpQA(selectedLeft);
316 float QRA = snpQA(selectedRight);
318 float QLB = snpQB(selectedLeft);
319 float QRB = snpQB(selectedRight);
321 //in original - not used - not sure why?
322 //float ALB = snpAB(selectedLeft);
323 //float ARB = snpAB(selectedRight);
325 if ((QLA > QLB) && (QRB > QRA)) {
329 if ((QLB > QLA) && (QRA > QRB)) {
333 //cout << "selected left snp: \n";
334 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentAChar; }
336 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].queryChar; }
338 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentBChar; }
340 //cout << "selected right snp: \n";
341 //for (int j = 0; j < selectedRight.size(); j++) { cout << selectedRight[j].parentAChar; }
343 //for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].queryChar; }
345 //for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].parentBChar; }
350 //cout << count_A << '\t' << count_B << endl;
352 BSA = (float) count_A / (float) numIters * 100;
353 BSB = (float) count_B / (float) numIters * 100;
354 //cout << "bsa = " << BSA << " bsb = " << BSB << endl;
359 catch(exception& e) {
360 m->errorOut(e, "Slayer", "bootstrapSNPS");
364 /***********************************************************************/
365 float Slayer::snpQA(vector<snps> data) {
368 int numIdentical = 0;
370 for (int i = 0; i < data.size(); i++) {
371 if (data[i].parentAChar == data[i].queryChar) {
376 float percentID = (numIdentical / (float) data.size()) * 100;
380 catch(exception& e) {
381 m->errorOut(e, "Slayer", "snpQA");
385 /***********************************************************************/
386 float Slayer::snpQB(vector<snps> data) {
389 int numIdentical = 0;
391 for (int i = 0; i < data.size(); i++) {
392 if (data[i].parentBChar == data[i].queryChar) {
397 float percentID = (numIdentical / (float) data.size()) * 100;
402 catch(exception& e) {
403 m->errorOut(e, "Slayer", "snpQB");
407 /***********************************************************************/
408 float Slayer::snpAB(vector<snps> data) {
410 int numIdentical = 0;
412 for (int i = 0; i < data.size(); i++) {
413 if (data[i].parentAChar == data[i].parentBChar) {
418 float percentID = (numIdentical / (float) data.size()) * 100;
423 catch(exception& e) {
424 m->errorOut(e, "Slayer", "snpAB");
428 /***********************************************************************/
429 float Slayer::computePercentID(string queryAlign, string chimera, int left, int right) {
432 int numIdentical = 0;
435 for (int i = left; i <= right; i++) {
436 if (((queryAlign[i] != 'G') && (queryAlign[i] != 'T') && (queryAlign[i] != 'A') && (queryAlign[i] != 'C')&& (queryAlign[i] != '.') && (queryAlign[i] != '-')) ||
437 ((chimera[i] != 'G') && (chimera[i] != 'T') && (chimera[i] != 'A') && (chimera[i] != 'C')&& (chimera[i] != '.') && (chimera[i] != '-'))) {}
440 bool charA = false; bool charB = false;
441 if ((queryAlign[i] == 'G') || (queryAlign[i] == 'T') || (queryAlign[i] == 'A') || (queryAlign[i] == 'C')) { charA = true; }
442 if ((chimera[i] == 'G') || (chimera[i] == 'T') || (chimera[i] == 'A') || (chimera[i] == 'C')) { charB = true; }
444 if (charA || charB) {
446 if (charA) { countA++; }
447 if (charB) { countB++; }
449 if (queryAlign[i] == chimera[i]) {
457 float numBases = (countA + countB) /(float) 2;
459 if (numBases == 0) { return 0; }
461 float percentIdentical = (numIdentical/(float)numBases) * 100;
463 return percentIdentical;
466 catch(exception& e) {
467 m->errorOut(e, "Slayer", "computePercentID");
471 /***********************************************************************/
472 //remove columns that contain any gaps
473 map<int, int> Slayer::verticalFilter(vector<Sequence*> seqs) {
477 baseSpots.resize(3); //query, parentA, parentB
479 vector<int> gaps; gaps.resize(seqs[0]->getAligned().length(), 0);
481 string filterString = (string(seqs[0]->getAligned().length(), '1'));
484 for (int i = 0; i < seqs.size(); i++) {
486 string seqAligned = seqs[i]->getAligned();
488 for (int j = 0; j < seqAligned.length(); j++) {
489 //if this spot is a gap
490 if ((seqAligned[j] == '-') || (seqAligned[j] == '.') || (toupper(seqAligned[j]) == 'N')) { gaps[j]++; }
494 //zero out spot where any sequences have blanks
495 int numColRemoved = 0;
497 map<int, int> maskMap; maskMap.clear();
499 for(int i = 0; i < seqs[0]->getAligned().length(); i++){
500 if(gaps[i] != 0) { filterString[i] = '0'; numColRemoved++; }
508 for (int i = 0; i < seqs.size(); i++) {
510 string seqAligned = seqs[i]->getAligned();
511 string newAligned = "";
515 for (int j = 0; j < seqAligned.length(); j++) {
517 if ((seqAligned[j] != '-') && (seqAligned[j] != '.') && (toupper(seqAligned[j]) != 'N')) { baseCount++; }
519 //if this spot is not a gap
520 if (filterString[j] == '1') {
521 newAligned += seqAligned[j];
522 baseSpots[i][count] = baseCount;
527 seqs[i]->setAligned(newAligned);
532 catch(exception& e) {
533 m->errorOut(e, "Slayer", "verticalFilter");
537 /***********************************************************************/