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();
22 for (int i = 0; i < refSeqs.size(); i++) {
24 for (int j = i+1; j < refSeqs.size(); j++) {
26 if (m->control_pressed) { return "no"; }
28 //make copies of query and each parent because runBellerophon removes gaps and messes them up
29 Sequence* q = new Sequence(query->getName(), query->getAligned());
30 Sequence* leftParent = new Sequence(refSeqs[i]->getName(), refSeqs[i]->getAligned());
31 Sequence* rightParent = new Sequence(refSeqs[j]->getName(), refSeqs[j]->getAligned());
33 map<int, int> spots; //map from spot in original sequence to spot in filtered sequence for query and both parents
34 vector<data_struct> divs = runBellerophon(q, leftParent, rightParent, spots);
36 if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
37 // cout << "examining:\t" << refSeqs[i]->getName() << '\t' << refSeqs[j]->getName() << endl;
38 vector<data_struct> selectedDivs;
39 for (int k = 0; k < divs.size(); k++) {
41 vector<snps> snpsLeft = getSNPS(divs[k].parentA.getAligned(), divs[k].querySeq.getAligned(), divs[k].parentB.getAligned(), divs[k].winLStart, divs[k].winLEnd);
42 vector<snps> snpsRight = getSNPS(divs[k].parentA.getAligned(), divs[k].querySeq.getAligned(), divs[k].parentB.getAligned(), divs[k].winRStart, divs[k].winREnd);
44 if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
46 int numSNPSLeft = snpsLeft.size();
47 int numSNPSRight = snpsRight.size();
49 // cout << numSNPSLeft << '\t' << numSNPSRight << endl;
50 //require at least 4 SNPs on each side of the break
51 if ((numSNPSLeft >= 4) && (numSNPSRight >= 4)) {
54 bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B, iters);
56 if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
60 divs[k].bsMax = max(BS_A, BS_B);
61 divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra);
64 //are we within 10 points of the bootstrap cutoff?
65 // if ((divs[k].bsMax >= (minBS-10)) && (iters < 1000)) {
66 // bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B, 1000);
68 // if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
70 // divs[k].bsa = BS_A;
71 // divs[k].bsb = BS_B;
72 // divs[k].bsMax = max(BS_A, BS_B);
73 // divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra);
76 //so results reflect orignal alignment
77 divs[k].winLStart = spots[divs[k].winLStart];
78 divs[k].winLEnd = spots[divs[k].winLEnd];
79 divs[k].winRStart = spots[divs[k].winRStart];
80 divs[k].winREnd = spots[divs[k].winREnd];
82 selectedDivs.push_back(divs[k]);
87 for (int mi = 0; mi < selectedDivs.size(); mi++) { all.push_back(selectedDivs[mi]); }
96 // compute bootstrap support
99 sort(all.begin(), all.end(), compareDataStruct);
100 reverse(all.begin(), all.end());
109 catch(exception& e) {
110 m->errorOut(e, "Slayer", "getResults");
114 /***********************************************************************/
115 vector<data_struct> Slayer::runBellerophon(Sequence* q, Sequence* pA, Sequence* pB, map<int, int>& spots) {
118 vector<data_struct> data;
121 vector<Sequence*> temp;
122 temp.push_back(q); temp.push_back(pA); temp.push_back(pB);
124 //maps spot in new alignment to spot in alignment before filter
125 spots = verticalFilter(temp); //fills baseSpots
127 //get these to avoid numerous function calls
128 string query = q->getAligned();
129 string parentA = pA->getAligned();
130 string parentB = pB->getAligned();
131 int length = query.length();
132 //cout << q->getName() << endl << q->getAligned() << endl << endl;
133 //cout << pA->getName() << endl << pA->getUnaligned() << endl << endl;
134 //cout << pB->getName() << endl << pB->getUnaligned() << endl << endl;
135 //cout << " length = " << length << endl;
138 if (length < (2*windowSize+windowStep)) {
139 // 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();
140 windowSize = length / 4;
143 for (int i = windowSize-1; i <= (length - windowSize); i += windowStep) {
145 if (m->control_pressed) { return data; }
148 int leftLength = breakpoint + 1;
149 int rightLength = length - leftLength;
151 float QLA = computePercentID(query, parentA, 0, breakpoint);
152 float QRB = computePercentID(query, parentB, breakpoint+1, length);
154 float QLB = computePercentID(query, parentB, 0, breakpoint);
155 float QRA = computePercentID(query, parentA, breakpoint+1, length);
157 float LAB = computePercentID(parentA, parentB, 0, breakpoint);
158 float RAB = computePercentID(parentA, parentB, breakpoint+1, length);
160 float AB = ((LAB*leftLength) + (RAB*rightLength)) / (float) length;
161 float QA = ((QLA*leftLength) + (QRA*rightLength)) / (float) length;
162 float QB = ((QLB*leftLength) + (QRB*rightLength)) / (float) length;
164 float QLA_QRB = ((QLA*leftLength) + (QRB*rightLength)) / (float) length;
165 float QLB_QRA = ((QLB*leftLength) + (QRA*rightLength)) / (float) length;
167 //in original and not used
168 //float avgQA_QB = ((QA*leftLength) + (QB*rightLength)) / (float) length;
170 float divR_QLA_QRB = min((QLA_QRB/QA), (QLA_QRB/QB));
171 float divR_QLB_QRA = min((QLB_QRA/QA), (QLB_QRA/QB));
174 //cout << q->getName() << '\t';
175 //cout << pA->getName() << '\t';
176 //cout << pB->getName() << '\t';
177 // cout << "bp: " << breakpoint << " CHIM_TYPE_A\t" << divR_QLA_QRB << "\tQLA: " << QLA << "\tQRB: " << QRB << "\tQLA_QRB: " << QLA_QRB;
178 //cout << "\tCHIM_TYPE_B\t" << divR_QLB_QRA << "\tQLB: " << QLB << "\tQRA: " << QRA << "\tQLB_QRA: " << QLB_QRA << endl;
179 //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;
181 //cout << divRThreshold << endl;
182 //cout << breakpoint << '\t' << divR_QLA_QRB << '\t' << divR_QLB_QRA << endl;
183 //is one of them above the
184 if (divR_QLA_QRB >= divRThreshold || divR_QLB_QRA >= divRThreshold) {
186 if (((QLA_QRB > QA) && (QLA_QRB > QB) && (QLA >= parentFragmentThreshold) && (QRB >= parentFragmentThreshold)) ||
187 ((QLB_QRA > QA) && (QLB_QRA > QB) && (QLB >=parentFragmentThreshold) && (QRA >= parentFragmentThreshold))) {
191 member.divr_qla_qrb = divR_QLA_QRB;
192 member.divr_qlb_qra = divR_QLB_QRA;
193 member.qla_qrb = QLA_QRB;
194 member.qlb_qra = QLB_QRA;
204 member.winLStart = 0;
205 member.winLEnd = breakpoint;
206 member.winRStart = breakpoint+1;
207 member.winREnd = length-1;
208 member.querySeq = *(q);
209 member.parentA = *(pA);
210 member.parentB = *(pB);
214 member.chimeraMax = 0;
216 data.push_back(member);
226 catch(exception& e) {
227 m->errorOut(e, "Slayer", "runBellerophon");
231 /***********************************************************************/
232 vector<snps> Slayer::getSNPS(string parentA, string query, string parentB, int left, int right) {
237 for (int i = left; i <= right; i++) {
243 if ((A != Q) || (B != Q)) {
245 //ensure not neighboring a gap. change to 12/09 release of chimeraSlayer - not sure what this adds, but it eliminates alot of SNPS
249 //did query loose a base here during filter??
250 ( i == 0 || abs (baseSpots[0][i] - baseSpots[0][i-1]) == 1) &&
251 ( i == query.length()-1 || abs (baseSpots[0][i] - baseSpots[0][i+1]) == 1)
253 //did parentA loose a base here during filter??
254 ( i == 0 || abs (baseSpots[1][i] - baseSpots[1][i-1]) == 1) &&
255 ( i == parentA.length()-1 || abs (baseSpots[1][i] - baseSpots[1][i+1]) == 1)
257 //did parentB loose a base here during filter??
258 ( i == 0 || abs (baseSpots[2][i] - baseSpots[2][i-1]) == 1) &&
259 ( i == parentB.length()-1 || abs (baseSpots[2][i] - baseSpots[2][i+1]) == 1)
263 member.queryChar = Q;
264 member.parentAChar = A;
265 member.parentBChar = B;
266 data.push_back(member);
274 catch(exception& e) {
275 m->errorOut(e, "Slayer", "getSNPS");
279 /***********************************************************************/
280 int Slayer::bootstrapSNPS(vector<snps> left, vector<snps> right, float& BSA, float& BSB, int numIters) {
283 srand((unsigned)time( NULL ));
285 int count_A = 0; // sceneario QLA,QRB supported
286 int count_B = 0; // sceneario QLB,QRA supported
288 int numLeft = max(1, int(left.size() * percentSNPSample/(float)100 + 0.5));
289 int numRight = max(1, int(right.size() * percentSNPSample/(float)100 + 0.5));
291 for (int i = 0; i < numIters; i++) {
292 //random sampling with replacement.
294 if (m->control_pressed) { return 0; }
296 vector<snps> selectedLeft;
298 for (int j = 0; j < numLeft; j++) {
299 int index = int(rand() % left.size());
300 selectedLeft.push_back(left[index]);
303 vector<snps> selectedRight;
304 for (int j = 0; j < numRight; j++) {
305 int index = int(rand() % right.size());
306 selectedRight.push_back(right[index]);
309 /* A ------------------------------------------
311 # Q ------------------------------------------
314 # Q ------------------------------------------
316 # B ------------------------------------------ */
319 float QLA = snpQA(selectedLeft);
320 float QRA = snpQA(selectedRight);
322 float QLB = snpQB(selectedLeft);
323 float QRB = snpQB(selectedRight);
325 //in original - not used - not sure why?
326 //float ALB = snpAB(selectedLeft);
327 //float ARB = snpAB(selectedRight);
329 if ((QLA > QLB) && (QRB > QRA)) {
333 if ((QLB > QLA) && (QRA > QRB)) {
337 //cout << "selected left snp: \n";
338 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentAChar; }
340 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].queryChar; }
342 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentBChar; }
344 //cout << "selected right snp: \n";
345 //for (int j = 0; j < selectedRight.size(); j++) { cout << selectedRight[j].parentAChar; }
347 //for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].queryChar; }
349 //for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].parentBChar; }
354 //cout << count_A << '\t' << count_B << endl;
356 BSA = (float) count_A / (float) numIters * 100;
357 BSB = (float) count_B / (float) numIters * 100;
358 //cout << "bsa = " << BSA << " bsb = " << BSB << endl;
363 catch(exception& e) {
364 m->errorOut(e, "Slayer", "bootstrapSNPS");
368 /***********************************************************************/
369 float Slayer::snpQA(vector<snps> data) {
372 int numIdentical = 0;
374 for (int i = 0; i < data.size(); i++) {
375 if (data[i].parentAChar == data[i].queryChar) {
380 float percentID = (numIdentical / (float) data.size()) * 100;
384 catch(exception& e) {
385 m->errorOut(e, "Slayer", "snpQA");
389 /***********************************************************************/
390 float Slayer::snpQB(vector<snps> data) {
393 int numIdentical = 0;
395 for (int i = 0; i < data.size(); i++) {
396 if (data[i].parentBChar == data[i].queryChar) {
401 float percentID = (numIdentical / (float) data.size()) * 100;
406 catch(exception& e) {
407 m->errorOut(e, "Slayer", "snpQB");
411 /***********************************************************************/
412 float Slayer::snpAB(vector<snps> data) {
414 int numIdentical = 0;
416 for (int i = 0; i < data.size(); i++) {
417 if (data[i].parentAChar == data[i].parentBChar) {
422 float percentID = (numIdentical / (float) data.size()) * 100;
427 catch(exception& e) {
428 m->errorOut(e, "Slayer", "snpAB");
432 /***********************************************************************/
433 float Slayer::computePercentID(string queryAlign, string chimera, int left, int right) {
436 int numIdentical = 0;
439 for (int i = left; i <= right; i++) {
440 if (((queryAlign[i] != 'G') && (queryAlign[i] != 'T') && (queryAlign[i] != 'A') && (queryAlign[i] != 'C')&& (queryAlign[i] != '.') && (queryAlign[i] != '-')) ||
441 ((chimera[i] != 'G') && (chimera[i] != 'T') && (chimera[i] != 'A') && (chimera[i] != 'C')&& (chimera[i] != '.') && (chimera[i] != '-'))) {}
444 bool charA = false; bool charB = false;
445 if ((queryAlign[i] == 'G') || (queryAlign[i] == 'T') || (queryAlign[i] == 'A') || (queryAlign[i] == 'C')) { charA = true; }
446 if ((chimera[i] == 'G') || (chimera[i] == 'T') || (chimera[i] == 'A') || (chimera[i] == 'C')) { charB = true; }
448 if (charA || charB) {
450 if (charA) { countA++; }
451 if (charB) { countB++; }
453 if (queryAlign[i] == chimera[i]) {
461 float numBases = (countA + countB) /(float) 2;
463 if (numBases == 0) { return 0; }
465 float percentIdentical = (numIdentical/(float)numBases) * 100;
467 return percentIdentical;
470 catch(exception& e) {
471 m->errorOut(e, "Slayer", "computePercentID");
475 /***********************************************************************/
476 //remove columns that contain any gaps
477 map<int, int> Slayer::verticalFilter(vector<Sequence*> seqs) {
481 baseSpots.resize(3); //query, parentA, parentB
483 vector<int> gaps; gaps.resize(seqs[0]->getAligned().length(), 0);
485 string filterString = (string(seqs[0]->getAligned().length(), '1'));
488 for (int i = 0; i < seqs.size(); i++) {
490 string seqAligned = seqs[i]->getAligned();
492 for (int j = 0; j < seqAligned.length(); j++) {
493 //if this spot is a gap
494 if ((seqAligned[j] == '-') || (seqAligned[j] == '.') || (toupper(seqAligned[j]) == 'N')) { gaps[j]++; }
498 //zero out spot where any sequences have blanks
499 int numColRemoved = 0;
501 map<int, int> maskMap; maskMap.clear();
503 for(int i = 0; i < seqs[0]->getAligned().length(); i++){
504 if(gaps[i] != 0) { filterString[i] = '0'; numColRemoved++; }
512 for (int i = 0; i < seqs.size(); i++) {
514 string seqAligned = seqs[i]->getAligned();
515 string newAligned = "";
519 for (int j = 0; j < seqAligned.length(); j++) {
521 if ((seqAligned[j] != '-') && (seqAligned[j] != '.') && (toupper(seqAligned[j]) != 'N')) { baseCount++; }
523 //if this spot is not a gap
524 if (filterString[j] == '1') {
525 newAligned += seqAligned[j];
526 baseSpots[i][count] = baseCount;
531 seqs[i]->setAligned(newAligned);
536 catch(exception& e) {
537 m->errorOut(e, "Slayer", "verticalFilter");
541 /***********************************************************************/