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());
31 //cout << "parents: (" << refSeqs[i]->getName() << ", " << refSeqs[j]->getName() << ")\n";
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);
34 //cout << divs.size() << endl;
35 if (m->control_pressed) {
42 // cout << divs.size() << endl;
43 vector<data_struct> selectedDivs;
44 for (int k = 0; k < divs.size(); k++) {
46 vector<snps> snpsLeft = getSNPS(divs[k].parentA.getAligned(), divs[k].querySeq.getAligned(), divs[k].parentB.getAligned(), divs[k].winLStart, divs[k].winLEnd);
47 vector<snps> snpsRight = getSNPS(divs[k].parentA.getAligned(), divs[k].querySeq.getAligned(), divs[k].parentB.getAligned(), divs[k].winRStart, divs[k].winREnd);
48 //cout << refSeqs[i]->getName() << '\t' << refSeqs[j]->getName() << '\t' << k << divs[k].parentA.getAligned() << endl << divs[k].parentB.getAligned() << endl;
49 if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
51 int numSNPSLeft = snpsLeft.size();
52 int numSNPSRight = snpsRight.size();
54 //require at least 4 SNPs on each side of the break
55 if ((numSNPSLeft >= 4) && (numSNPSRight >= 4)) {
58 bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B, iters);
60 if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
64 divs[k].bsMax = max(BS_A, BS_B);
65 divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra);
68 //are we within 10 points of the bootstrap cutoff?
69 if (divs[k].bsMax >= (minBS-10)) {
70 snpsLeft = getSNPS(divs[k].parentA.getAligned(), divs[k].querySeq.getAligned(), divs[k].parentB.getAligned(), divs[k].winLStart, divs[k].winLEnd);
71 snpsRight = getSNPS(divs[k].parentA.getAligned(), divs[k].querySeq.getAligned(), divs[k].parentB.getAligned(), divs[k].winRStart, divs[k].winREnd);
73 if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
75 numSNPSLeft = snpsLeft.size();
76 numSNPSRight = snpsRight.size();
78 //require at least 4 SNPs on each side of the break
79 if ((numSNPSLeft >= 4) && (numSNPSRight >= 4)) {
81 bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B, 1000);
83 if (m->control_pressed) { return "no"; }
87 divs[k].bsMax = max(BS_A, BS_B);
88 divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra);
92 //so results reflect orignal alignment
93 divs[k].winLStart = spots[divs[k].winLStart];
94 divs[k].winLEnd = spots[divs[k].winLEnd];
95 divs[k].winRStart = spots[divs[k].winRStart];
96 divs[k].winREnd = spots[divs[k].winREnd];
98 selectedDivs.push_back(divs[k]);
103 for (int mi = 0; mi < selectedDivs.size(); mi++) { all.push_back(selectedDivs[mi]); }
111 // compute bootstrap support
112 if (all.size() > 0) {
114 sort(all.begin(), all.end(), compareDataStruct);
115 reverse(all.begin(), all.end());
124 catch(exception& e) {
125 m->errorOut(e, "Slayer", "getResults");
129 /***********************************************************************/
130 vector<data_struct> Slayer::runBellerophon(Sequence* q, Sequence* pA, Sequence* pB, map<int, int>& spots) {
133 vector<data_struct> data;
136 vector<Sequence*> temp;
137 temp.push_back(q); temp.push_back(pA); temp.push_back(pB);
139 //maps spot in new alignment to spot in alignment before filter
140 spots = verticalFilter(temp); //fills baseSpots
142 //get these to avoid numerous function calls
143 string query = q->getAligned();
144 string parentA = pA->getAligned();
145 string parentB = pB->getAligned();
146 int length = query.length();
147 //cout << q->getName() << endl << q->getAligned() << endl << endl;
148 //cout << pA->getName() << endl << pA->getUnaligned() << endl << endl;
149 //cout << pB->getName() << endl << pB->getUnaligned() << endl << endl;
150 //cout << " length = " << length << endl;
153 if (length < (2*windowSize+windowStep)) {
154 // 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();
155 windowSize = length / 4;
158 for (int i = windowSize-1; i <= (length - windowSize); i += windowStep) {
160 if (m->control_pressed) { return data; }
163 int leftLength = breakpoint + 1;
164 int rightLength = length - leftLength;
166 float QLA = computePercentID(query, parentA, 0, breakpoint);
167 float QRB = computePercentID(query, parentB, breakpoint+1, length - 1);
169 float QLB = computePercentID(query, parentB, 0, breakpoint);
170 float QRA = computePercentID(query, parentA, breakpoint+1, length - 1);
172 float LAB = computePercentID(parentA, parentB, 0, breakpoint);
173 float RAB = computePercentID(parentA, parentB, breakpoint+1, length - 1);
175 float AB = ((LAB*leftLength) + (RAB*rightLength)) / (float) length;
176 float QA = ((QLA*leftLength) + (QRA*rightLength)) / (float) length;
177 float QB = ((QLB*leftLength) + (QRB*rightLength)) / (float) length;
179 float QLA_QRB = ((QLA*leftLength) + (QRB*rightLength)) / (float) length;
180 float QLB_QRA = ((QLB*leftLength) + (QRA*rightLength)) / (float) length;
182 //in original and not used
183 //float avgQA_QB = ((QA*leftLength) + (QB*rightLength)) / (float) length;
185 float divR_QLA_QRB = min((QLA_QRB/QA), (QLA_QRB/QB));
186 float divR_QLB_QRA = min((QLB_QRA/QA), (QLB_QRA/QB));
187 //cout << q->getName() << '\t';
188 //cout << pA->getName() << '\t';
189 //cout << pB->getName() << '\t';
190 // cout << "bp: " << breakpoint << " CHIM_TYPE_A\t" << divR_QLA_QRB << "\tQLA: " << QLA << "\tQRB: " << QRB << "\tQLA_QRB: " << QLA_QRB;
191 //cout << "\tCHIM_TYPE_B\t" << divR_QLB_QRA << "\tQLB: " << QLB << "\tQRA: " << QRA << "\tQLB_QRA: " << QLB_QRA << endl;
192 //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;
194 //cout << divRThreshold << endl;
195 //cout << breakpoint << '\t' << divR_QLA_QRB << '\t' << divR_QLB_QRA << endl;
196 //is one of them above the
197 if (divR_QLA_QRB >= divRThreshold || divR_QLB_QRA >= divRThreshold) {
199 if (((QLA_QRB > QA) && (QLA_QRB > QB) && (QLA >= parentFragmentThreshold) && (QRB >= parentFragmentThreshold)) ||
200 ((QLB_QRA > QA) && (QLB_QRA > QB) && (QLB >=parentFragmentThreshold) && (QRA >= parentFragmentThreshold))) {
204 member.divr_qla_qrb = divR_QLA_QRB;
205 member.divr_qlb_qra = divR_QLB_QRA;
206 member.qla_qrb = QLA_QRB;
207 member.qlb_qra = QLB_QRA;
217 member.winLStart = 0;
218 member.winLEnd = breakpoint;
219 member.winRStart = breakpoint+1;
220 member.winREnd = length-1;
221 member.querySeq = *(q);
222 member.parentA = *(pA);
223 member.parentB = *(pB);
227 member.chimeraMax = 0;
229 data.push_back(member);
238 catch(exception& e) {
239 m->errorOut(e, "Slayer", "runBellerophon");
243 /***********************************************************************/
244 vector<snps> Slayer::getSNPS(string parentA, string query, string parentB, int left, int right) {
248 //cout << left << '\t' << right << endl;
249 for (int i = left; i <= right; i++) {
255 if ((A != Q) || (B != Q)) {
256 //cout << "not equal " << Q << '\t' << A << '\t' << B << endl;
258 //ensure not neighboring a gap. change to 12/09 release of chimeraSlayer - not sure what this adds, but it eliminates alot of SNPS
260 //did query loose a base here during filter??
261 ( i == 0 || abs (baseSpots[0][i] - baseSpots[0][i-1]) == 1) &&
262 ( i == query.length() || abs (baseSpots[0][i] - baseSpots[0][i+1]) == 1)
264 //did parentA loose a base here during filter??
265 ( i == 0 || abs (baseSpots[1][i] - baseSpots[1][i-1]) == 1) &&
266 ( i == parentA.length() || abs (baseSpots[1][i] - baseSpots[1][i+1]) == 1)
268 //did parentB loose a base here during filter??
269 ( i == 0 || abs (baseSpots[2][i] - baseSpots[2][i-1]) == 1) &&
270 ( i == parentB.length() || abs (baseSpots[2][i] - baseSpots[2][i+1]) == 1)
275 member.queryChar = Q;
276 member.parentAChar = A;
277 member.parentBChar = B;
278 //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;
279 data.push_back(member);
282 // cout << i << '\t' << data.size() << endl;
288 catch(exception& e) {
289 m->errorOut(e, "Slayer", "getSNPS");
293 /***********************************************************************/
294 int Slayer::bootstrapSNPS(vector<snps> left, vector<snps> right, float& BSA, float& BSB, int numIters) {
297 srand((unsigned)time( NULL ));
299 int count_A = 0; // sceneario QLA,QRB supported
300 int count_B = 0; // sceneario QLB,QRA supported
302 int numLeft = max(1, int(left.size() * percentSNPSample/(float)100 + 0.5));
303 int numRight = max(1, int(right.size() * percentSNPSample/(float)100 + 0.5));
304 //cout << numLeft << '\t' << numRight << endl;
305 for (int i = 0; i < numIters; i++) {
306 //random sampling with replacement.
308 if (m->control_pressed) { return 0; }
310 vector<snps> selectedLeft;
312 for (int j = 0; j < numLeft; j++) {
313 int index = int(rand() % left.size());
314 selectedLeft.push_back(left[index]);
317 vector<snps> selectedRight;
318 for (int j = 0; j < numRight; j++) {
319 int index = int(rand() % right.size());
320 selectedRight.push_back(right[index]);
323 /* A ------------------------------------------
325 # Q ------------------------------------------
328 # Q ------------------------------------------
330 # B ------------------------------------------ */
333 float QLA = snpQA(selectedLeft);
334 float QRA = snpQA(selectedRight);
336 float QLB = snpQB(selectedLeft);
337 float QRB = snpQB(selectedRight);
339 //in original - not used - not sure why?
340 //float ALB = snpAB(selectedLeft);
341 //float ARB = snpAB(selectedRight);
343 if ((QLA > QLB) && (QRB > QRA)) {
347 if ((QLB > QLA) && (QRA > QRB)) {
351 //cout << "selected left snp: \n";
352 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentAChar; }
354 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].queryChar; }
356 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentBChar; }
358 //cout << "selected right snp: \n";
359 //for (int j = 0; j < selectedRight.size(); j++) { cout << selectedRight[j].parentAChar; }
361 //for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].queryChar; }
363 //for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].parentBChar; }
368 //cout << count_A << '\t' << count_B << endl;
370 BSA = (float) count_A / (float) numIters * 100;
371 BSB = (float) count_B / (float) numIters * 100;
372 //cout << "bsa = " << BSA << " bsb = " << BSB << endl;
377 catch(exception& e) {
378 m->errorOut(e, "Slayer", "bootstrapSNPS");
382 /***********************************************************************/
383 float Slayer::snpQA(vector<snps> data) {
386 int numIdentical = 0;
388 for (int i = 0; i < data.size(); i++) {
389 if (data[i].parentAChar == data[i].queryChar) {
394 float percentID = (numIdentical / (float) data.size()) * 100;
398 catch(exception& e) {
399 m->errorOut(e, "Slayer", "snpQA");
403 /***********************************************************************/
404 float Slayer::snpQB(vector<snps> data) {
407 int numIdentical = 0;
409 for (int i = 0; i < data.size(); i++) {
410 if (data[i].parentBChar == data[i].queryChar) {
415 float percentID = (numIdentical / (float) data.size()) * 100;
420 catch(exception& e) {
421 m->errorOut(e, "Slayer", "snpQB");
425 /***********************************************************************/
426 float Slayer::snpAB(vector<snps> data) {
428 int numIdentical = 0;
430 for (int i = 0; i < data.size(); i++) {
431 if (data[i].parentAChar == data[i].parentBChar) {
436 float percentID = (numIdentical / (float) data.size()) * 100;
441 catch(exception& e) {
442 m->errorOut(e, "Slayer", "snpAB");
446 /***********************************************************************/
447 float Slayer::computePercentID(string queryFrag, string parent, int left, int right) {
452 for (int i = left; i <= right; i++) {
454 if (queryFrag[i] == parent[i]) {
459 float percentID =( matches/(float)total) * 100;
463 catch(exception& e) {
464 m->errorOut(e, "Slayer", "computePercentID");
468 /***********************************************************************/
469 //remove columns that contain any gaps
470 map<int, int> Slayer::verticalFilter(vector<Sequence*> seqs) {
474 baseSpots.resize(3); //query, parentA, parentB
476 vector<int> gaps; gaps.resize(seqs[0]->getAligned().length(), 0);
478 string filterString = (string(seqs[0]->getAligned().length(), '1'));
481 for (int i = 0; i < seqs.size(); i++) {
483 string seqAligned = seqs[i]->getAligned();
485 for (int j = 0; j < seqAligned.length(); j++) {
486 //if this spot is a gap
487 if ((seqAligned[j] == '-') || (seqAligned[j] == '.') || (toupper(seqAligned[j]) == 'N')) { gaps[j]++; }
491 //zero out spot where any sequences have blanks
492 int numColRemoved = 0;
494 map<int, int> maskMap; maskMap.clear();
496 for(int i = 0; i < seqs[0]->getAligned().length(); i++){
497 if(gaps[i] != 0) { filterString[i] = '0'; numColRemoved++; }
505 for (int i = 0; i < seqs.size(); i++) {
507 string seqAligned = seqs[i]->getAligned();
508 string newAligned = "";
512 for (int j = 0; j < seqAligned.length(); j++) {
514 if ((seqAligned[j] != '-') && (seqAligned[j] != '.') && (toupper(seqAligned[j]) != 'N')) { baseCount++; }
516 //if this spot is not a gap
517 if (filterString[j] == '1') {
518 newAligned += seqAligned[j];
519 baseSpots[i][count] = baseCount;
524 seqs[i]->setAligned(newAligned);
529 catch(exception& e) {
530 m->errorOut(e, "Slayer", "verticalFilter");
534 /***********************************************************************/