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(query.getName(), query.getAligned());
29 Sequence leftParent(refSeqs[i].getName(), refSeqs[i].getAligned());
30 Sequence rightParent(refSeqs[j].getName(), refSeqs[j].getAligned());
32 //cout << q->getName() << endl << q->getAligned() << endl << endl;
33 //cout << leftParent.getName() << '\t' << leftParent.getAligned().length() << endl << endl;
34 //cout << rightParent.getName() << '\t' << rightParent.getAligned().length() << endl << endl;
35 //cout << q.getName() << '\t' << q.getAligned().length() << endl << endl;
36 //cout << rightParent->getName() << endl << rightParent->getAligned() << endl << endl;
37 //cout << " length = " << rightParent->getAligned().length() << endl;
39 map<int, int> spots; //map from spot in original sequence to spot in filtered sequence for query and both parents
40 vector<data_struct> divs = runBellerophon(q, leftParent, rightParent, spots);
42 if (m->control_pressed) { return "no"; }
43 // cout << "examining:\t" << refSeqs[i]->getName() << '\t' << refSeqs[j]->getName() << endl;
44 vector<data_struct> selectedDivs;
45 for (int k = 0; k < divs.size(); k++) {
47 vector<snps> snpsLeft = getSNPS(divs[k].parentA.getAligned(), divs[k].querySeq.getAligned(), divs[k].parentB.getAligned(), divs[k].winLStart, divs[k].winLEnd);
48 vector<snps> snpsRight = getSNPS(divs[k].parentA.getAligned(), divs[k].querySeq.getAligned(), divs[k].parentB.getAligned(), divs[k].winRStart, divs[k].winREnd);
50 if (m->control_pressed) { return "no"; }
52 int numSNPSLeft = snpsLeft.size();
53 int numSNPSRight = snpsRight.size();
55 // cout << numSNPSLeft << '\t' << numSNPSRight << endl;
56 //require at least 4 SNPs on each side of the break
57 if ((numSNPSLeft >= 4) && (numSNPSRight >= 4)) {
60 bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B, iters);
62 if (m->control_pressed) { return "no"; }
66 divs[k].bsMax = max(BS_A, BS_B);
67 divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra);
70 //are we within 10 points of the bootstrap cutoff?
71 // if ((divs[k].bsMax >= (minBS-10)) && (iters < 1000)) {
72 // bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B, 1000);
74 // if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
76 // divs[k].bsa = BS_A;
77 // divs[k].bsb = BS_B;
78 // divs[k].bsMax = max(BS_A, BS_B);
79 // divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra);
82 //so results reflect orignal alignment
83 divs[k].winLStart = spots[divs[k].winLStart];
84 divs[k].winLEnd = spots[divs[k].winLEnd];
85 divs[k].winRStart = spots[divs[k].winRStart];
86 divs[k].winREnd = spots[divs[k].winREnd];
88 selectedDivs.push_back(divs[k]);
93 for (int mi = 0; mi < selectedDivs.size(); mi++) { all.push_back(selectedDivs[mi]); }
98 // compute bootstrap support
101 sort(all.begin(), all.end(), compareDataStruct);
102 reverse(all.begin(), all.end());
111 catch(exception& e) {
112 m->errorOut(e, "Slayer", "getResults");
116 /***********************************************************************/
117 vector<data_struct> Slayer::runBellerophon(Sequence q, Sequence pA, Sequence pB, map<int, int>& spots) {
120 vector<data_struct> data;
123 //cout << q.getName() << endl << q.getAligned() << endl << endl;
124 //cout << pA.getName() << endl << pA.getUnaligned() << endl << endl;
125 //cout << pB.getName() << endl << pB.getUnaligned() << endl << endl;
127 //maps spot in new alignment to spot in alignment before filter
128 spots = verticalFilter(q, pA, pB); //fills baseSpots
130 //get these to avoid numerous function calls
131 string query = q.getAligned();
132 string parentA = pA.getAligned();
133 string parentB = pB.getAligned();
134 int length = query.length();
135 //cout << q.getName() << endl << q.getAligned() << endl << endl;
136 //cout << pA.getName() << endl << pA.getUnaligned() << endl << endl;
137 //cout << pB.getName() << endl << pB.getUnaligned() << endl << endl;
138 //cout << " length = " << length << endl;
141 if (length < (2*windowSize+windowStep)) {
142 // 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();
143 windowSize = length / 4;
146 for (int i = windowSize-1; i <= (length - windowSize); i += windowStep) {
148 if (m->control_pressed) { return data; }
151 int leftLength = breakpoint + 1;
152 int rightLength = length - leftLength;
154 float QLA = computePercentID(query, parentA, 0, breakpoint);
155 float QRB = computePercentID(query, parentB, breakpoint+1, length-1);
157 float QLB = computePercentID(query, parentB, 0, breakpoint);
158 float QRA = computePercentID(query, parentA, breakpoint+1, length-1);
160 float LAB = computePercentID(parentA, parentB, 0, breakpoint);
161 float RAB = computePercentID(parentA, parentB, breakpoint+1, length-1);
163 float AB = ((LAB*leftLength) + (RAB*rightLength)) / (float) length;
164 float QA = ((QLA*leftLength) + (QRA*rightLength)) / (float) length;
165 float QB = ((QLB*leftLength) + (QRB*rightLength)) / (float) length;
167 float QLA_QRB = ((QLA*leftLength) + (QRB*rightLength)) / (float) length;
168 float QLB_QRA = ((QLB*leftLength) + (QRA*rightLength)) / (float) length;
170 //in original and not used
171 //float avgQA_QB = ((QA*leftLength) + (QB*rightLength)) / (float) length;
173 float divR_QLA_QRB = min((QLA_QRB/QA), (QLA_QRB/QB));
174 float divR_QLB_QRA = min((QLB_QRA/QA), (QLB_QRA/QB));
177 //cout << q->getName() << '\t';
178 //cout << pA->getName() << '\t';
179 //cout << pB->getName() << '\t';
180 //cout << "bp: " << breakpoint << " CHIM_TYPE_A\t" << divR_QLA_QRB << "\tQLA: " << QLA << "\tQRB: " << QRB << "\tQLA_QRB: " << QLA_QRB;
181 //cout << "\tCHIM_TYPE_B\t" << divR_QLB_QRA << "\tQLB: " << QLB << "\tQRA: " << QRA << "\tQLB_QRA: " << QLB_QRA << endl;
182 //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;
184 //cout << divRThreshold << endl;
185 //cout << breakpoint << '\t' << divR_QLA_QRB << '\t' << divR_QLB_QRA << endl;
186 //is one of them above the
187 if (divR_QLA_QRB >= divRThreshold || divR_QLB_QRA >= divRThreshold) {
189 if (((QLA_QRB > QA) && (QLA_QRB > QB) && (QLA >= parentFragmentThreshold) && (QRB >= parentFragmentThreshold)) ||
190 ((QLB_QRA > QA) && (QLB_QRA > QB) && (QLB >=parentFragmentThreshold) && (QRA >= parentFragmentThreshold))) {
194 member.divr_qla_qrb = divR_QLA_QRB;
195 member.divr_qlb_qra = divR_QLB_QRA;
196 member.qla_qrb = QLA_QRB;
197 member.qlb_qra = QLB_QRA;
207 member.winLStart = 0;
208 member.winLEnd = breakpoint;
209 member.winRStart = breakpoint+1;
210 member.winREnd = length-1;
217 member.chimeraMax = 0;
219 data.push_back(member);
229 catch(exception& e) {
230 m->errorOut(e, "Slayer", "runBellerophon");
234 /***********************************************************************/
235 vector<snps> Slayer::getSNPS(string parentA, string query, string parentB, int left, int right) {
240 for (int i = left; i <= right; i++) {
246 if ((A != Q) || (B != Q)) {
248 //ensure not neighboring a gap. change to 12/09 release of chimeraSlayer - not sure what this adds, but it eliminates alot of SNPS
252 //did query loose a base here during filter??
253 ( i == 0 || abs (baseSpots[0][i] - baseSpots[0][i-1]) == 1) &&
254 ( i == query.length()-1 || abs (baseSpots[0][i] - baseSpots[0][i+1]) == 1)
256 //did parentA loose a base here during filter??
257 ( i == 0 || abs (baseSpots[1][i] - baseSpots[1][i-1]) == 1) &&
258 ( i == parentA.length()-1 || abs (baseSpots[1][i] - baseSpots[1][i+1]) == 1)
260 //did parentB loose a base here during filter??
261 ( i == 0 || abs (baseSpots[2][i] - baseSpots[2][i-1]) == 1) &&
262 ( i == parentB.length()-1 || abs (baseSpots[2][i] - baseSpots[2][i+1]) == 1)
266 member.queryChar = Q;
267 member.parentAChar = A;
268 member.parentBChar = B;
269 data.push_back(member);
277 catch(exception& e) {
278 m->errorOut(e, "Slayer", "getSNPS");
282 /***********************************************************************/
283 int Slayer::bootstrapSNPS(vector<snps> left, vector<snps> right, float& BSA, float& BSB, int numIters) {
286 srand((unsigned)time( NULL ));
288 int count_A = 0; // sceneario QLA,QRB supported
289 int count_B = 0; // sceneario QLB,QRA supported
291 int numLeft = max(1, int(left.size() * percentSNPSample/(float)100 + 0.5));
292 int numRight = max(1, int(right.size() * percentSNPSample/(float)100 + 0.5));
294 for (int i = 0; i < numIters; i++) {
295 //random sampling with replacement.
297 if (m->control_pressed) { return 0; }
299 vector<snps> selectedLeft;
301 for (int j = 0; j < numLeft; j++) {
302 int index = int(rand() % left.size());
303 selectedLeft.push_back(left[index]);
306 vector<snps> selectedRight;
307 for (int j = 0; j < numRight; j++) {
308 int index = int(rand() % right.size());
309 selectedRight.push_back(right[index]);
312 /* A ------------------------------------------
314 # Q ------------------------------------------
317 # Q ------------------------------------------
319 # B ------------------------------------------ */
322 float QLA = snpQA(selectedLeft);
323 float QRA = snpQA(selectedRight);
325 float QLB = snpQB(selectedLeft);
326 float QRB = snpQB(selectedRight);
328 //in original - not used - not sure why?
329 //float ALB = snpAB(selectedLeft);
330 //float ARB = snpAB(selectedRight);
332 if ((QLA > QLB) && (QRB > QRA)) {
336 if ((QLB > QLA) && (QRA > QRB)) {
340 //cout << "selected left snp: \n";
341 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentAChar; }
343 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].queryChar; }
345 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentBChar; }
347 //cout << "selected right snp: \n";
348 //for (int j = 0; j < selectedRight.size(); j++) { cout << selectedRight[j].parentAChar; }
350 //for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].queryChar; }
352 //for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].parentBChar; }
357 //cout << count_A << '\t' << count_B << endl;
359 BSA = (float) count_A / (float) numIters * 100;
360 BSB = (float) count_B / (float) numIters * 100;
361 //cout << "bsa = " << BSA << " bsb = " << BSB << endl;
366 catch(exception& e) {
367 m->errorOut(e, "Slayer", "bootstrapSNPS");
371 /***********************************************************************/
372 float Slayer::snpQA(vector<snps> data) {
375 int numIdentical = 0;
377 for (int i = 0; i < data.size(); i++) {
378 if (data[i].parentAChar == data[i].queryChar) {
383 float percentID = (numIdentical / (float) data.size()) * 100;
387 catch(exception& e) {
388 m->errorOut(e, "Slayer", "snpQA");
392 /***********************************************************************/
393 float Slayer::snpQB(vector<snps> data) {
396 int numIdentical = 0;
398 for (int i = 0; i < data.size(); i++) {
399 if (data[i].parentBChar == data[i].queryChar) {
404 float percentID = (numIdentical / (float) data.size()) * 100;
409 catch(exception& e) {
410 m->errorOut(e, "Slayer", "snpQB");
414 /***********************************************************************/
415 float Slayer::snpAB(vector<snps> data) {
417 int numIdentical = 0;
419 for (int i = 0; i < data.size(); i++) {
420 if (data[i].parentAChar == data[i].parentBChar) {
425 float percentID = (numIdentical / (float) data.size()) * 100;
430 catch(exception& e) {
431 m->errorOut(e, "Slayer", "snpAB");
435 /***********************************************************************/
436 float Slayer::computePercentID(string queryAlign, string chimera, int left, int right) {
439 int numIdentical = 0;
442 for (int i = left; i <= right; i++) {
443 if (((queryAlign[i] != 'G') && (queryAlign[i] != 'T') && (queryAlign[i] != 'A') && (queryAlign[i] != 'C')&& (queryAlign[i] != '.') && (queryAlign[i] != '-')) ||
444 ((chimera[i] != 'G') && (chimera[i] != 'T') && (chimera[i] != 'A') && (chimera[i] != 'C')&& (chimera[i] != '.') && (chimera[i] != '-'))) {}
447 bool charA = false; bool charB = false;
448 if ((queryAlign[i] == 'G') || (queryAlign[i] == 'T') || (queryAlign[i] == 'A') || (queryAlign[i] == 'C')) { charA = true; }
449 if ((chimera[i] == 'G') || (chimera[i] == 'T') || (chimera[i] == 'A') || (chimera[i] == 'C')) { charB = true; }
451 if (charA || charB) {
453 if (charA) { countA++; }
454 if (charB) { countB++; }
456 if (queryAlign[i] == chimera[i]) {
464 float numBases = (countA + countB) /(float) 2;
466 if (numBases == 0) { return 0; }
468 float percentIdentical = (numIdentical/(float)numBases) * 100;
470 return percentIdentical;
473 catch(exception& e) {
474 m->errorOut(e, "Slayer", "computePercentID");
478 /***********************************************************************/
479 //remove columns that contain any gaps
480 map<int, int> Slayer::verticalFilter(Sequence& q, Sequence& pA, Sequence& pB) {
484 baseSpots.resize(3); //query, parentA, parentB
486 vector<int> gaps; gaps.resize(q.getAligned().length(), 0);
488 string filterString = (string(q.getAligned().length(), '1'));
490 string seqAligned = q.getAligned();
491 for (int j = 0; j < seqAligned.length(); j++) {
492 //if this spot is a gap
493 if ((seqAligned[j] == '-') || (seqAligned[j] == '.') || (toupper(seqAligned[j]) == 'N')) { gaps[j]++; }
496 seqAligned = pA.getAligned();
497 for (int j = 0; j < seqAligned.length(); j++) {
498 //if this spot is a gap
499 if ((seqAligned[j] == '-') || (seqAligned[j] == '.') || (toupper(seqAligned[j]) == 'N')) { gaps[j]++; }
502 seqAligned = pB.getAligned();
503 for (int j = 0; j < seqAligned.length(); j++) {
504 //if this spot is a gap
505 if ((seqAligned[j] == '-') || (seqAligned[j] == '.') || (toupper(seqAligned[j]) == 'N')) { gaps[j]++; }
509 //zero out spot where any sequences have blanks
510 int numColRemoved = 0;
512 map<int, int> maskMap; maskMap.clear();
514 for(int i = 0; i < q.getAligned().length(); i++){
515 if(gaps[i] != 0) { filterString[i] = '0'; numColRemoved++; }
522 seqAligned = q.getAligned();
523 string newAligned = "";
527 for (int j = 0; j < seqAligned.length(); j++) {
529 if ((seqAligned[j] != '-') && (seqAligned[j] != '.') && (toupper(seqAligned[j]) != 'N')) { baseCount++; }
531 //if this spot is not a gap
532 if (filterString[j] == '1') {
533 newAligned += seqAligned[j];
534 baseSpots[0][count] = baseCount;
539 q.setAligned(newAligned);
541 seqAligned = pA.getAligned();
546 for (int j = 0; j < seqAligned.length(); j++) {
548 if ((seqAligned[j] != '-') && (seqAligned[j] != '.') && (toupper(seqAligned[j]) != 'N')) { baseCount++; }
550 //if this spot is not a gap
551 if (filterString[j] == '1') {
552 newAligned += seqAligned[j];
553 baseSpots[1][count] = baseCount;
558 pA.setAligned(newAligned);
560 seqAligned = pB.getAligned();
565 for (int j = 0; j < seqAligned.length(); j++) {
567 if ((seqAligned[j] != '-') && (seqAligned[j] != '.') && (toupper(seqAligned[j]) != 'N')) { baseCount++; }
569 //if this spot is not a gap
570 if (filterString[j] == '1') {
571 newAligned += seqAligned[j];
572 baseSpots[2][count] = baseCount;
577 pB.setAligned(newAligned);
582 catch(exception& e) {
583 m->errorOut(e, "Slayer", "verticalFilter");
587 /***********************************************************************/