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"; }
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 //require at least 4 SNPs on each side of the break
50 if ((numSNPSLeft >= 4) && (numSNPSRight >= 4)) {
53 bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B, iters);
55 if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
59 divs[k].bsMax = max(BS_A, BS_B);
60 divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra);
63 //are we within 10 points of the bootstrap cutoff?
64 if ((divs[k].bsMax >= (minBS-10)) && (iters < 1000)) {
65 bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B, 1000);
67 if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
71 divs[k].bsMax = max(BS_A, BS_B);
72 divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra);
75 //so results reflect orignal alignment
76 divs[k].winLStart = spots[divs[k].winLStart];
77 divs[k].winLEnd = spots[divs[k].winLEnd];
78 divs[k].winRStart = spots[divs[k].winRStart];
79 divs[k].winREnd = spots[divs[k].winREnd];
81 selectedDivs.push_back(divs[k]);
86 for (int mi = 0; mi < selectedDivs.size(); mi++) { all.push_back(selectedDivs[mi]); }
95 // compute bootstrap support
98 sort(all.begin(), all.end(), compareDataStruct);
99 reverse(all.begin(), all.end());
108 catch(exception& e) {
109 m->errorOut(e, "Slayer", "getResults");
113 /***********************************************************************/
114 vector<data_struct> Slayer::runBellerophon(Sequence* q, Sequence* pA, Sequence* pB, map<int, int>& spots) {
117 vector<data_struct> data;
120 vector<Sequence*> temp;
121 temp.push_back(q); temp.push_back(pA); temp.push_back(pB);
123 //maps spot in new alignment to spot in alignment before filter
124 spots = verticalFilter(temp); //fills baseSpots
126 //get these to avoid numerous function calls
127 string query = q->getAligned();
128 string parentA = pA->getAligned();
129 string parentB = pB->getAligned();
130 int length = query.length();
131 //cout << q->getName() << endl << q->getAligned() << endl << endl;
132 //cout << pA->getName() << endl << pA->getUnaligned() << endl << endl;
133 //cout << pB->getName() << endl << pB->getUnaligned() << endl << endl;
134 //cout << " length = " << length << endl;
137 if (length < (2*windowSize+windowStep)) {
138 // 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();
139 windowSize = length / 4;
142 for (int i = windowSize-1; i <= (length - windowSize); i += windowStep) {
144 if (m->control_pressed) { return data; }
147 int leftLength = breakpoint + 1;
148 int rightLength = length - leftLength;
150 float QLA = computePercentID(query, parentA, 0, breakpoint);
151 float QRB = computePercentID(query, parentB, breakpoint+1, length);
153 float QLB = computePercentID(query, parentB, 0, breakpoint);
154 float QRA = computePercentID(query, parentA, breakpoint+1, length);
156 float LAB = computePercentID(parentA, parentB, 0, breakpoint);
157 float RAB = computePercentID(parentA, parentB, breakpoint+1, length);
159 float AB = ((LAB*leftLength) + (RAB*rightLength)) / (float) length;
160 float QA = ((QLA*leftLength) + (QRA*rightLength)) / (float) length;
161 float QB = ((QLB*leftLength) + (QRB*rightLength)) / (float) length;
163 float QLA_QRB = ((QLA*leftLength) + (QRB*rightLength)) / (float) length;
164 float QLB_QRA = ((QLB*leftLength) + (QRA*rightLength)) / (float) length;
166 //in original and not used
167 //float avgQA_QB = ((QA*leftLength) + (QB*rightLength)) / (float) length;
169 float divR_QLA_QRB = min((QLA_QRB/QA), (QLA_QRB/QB));
170 float divR_QLB_QRA = min((QLB_QRA/QA), (QLB_QRA/QB));
173 //cout << q->getName() << '\t';
174 //cout << pA->getName() << '\t';
175 //cout << pB->getName() << '\t';
176 // cout << "bp: " << breakpoint << " CHIM_TYPE_A\t" << divR_QLA_QRB << "\tQLA: " << QLA << "\tQRB: " << QRB << "\tQLA_QRB: " << QLA_QRB;
177 //cout << "\tCHIM_TYPE_B\t" << divR_QLB_QRA << "\tQLB: " << QLB << "\tQRA: " << QRA << "\tQLB_QRA: " << QLB_QRA << endl;
178 //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;
180 //cout << divRThreshold << endl;
181 //cout << breakpoint << '\t' << divR_QLA_QRB << '\t' << divR_QLB_QRA << endl;
182 //is one of them above the
183 if (divR_QLA_QRB >= divRThreshold || divR_QLB_QRA >= divRThreshold) {
185 if (((QLA_QRB > QA) && (QLA_QRB > QB) && (QLA >= parentFragmentThreshold) && (QRB >= parentFragmentThreshold)) ||
186 ((QLB_QRA > QA) && (QLB_QRA > QB) && (QLB >=parentFragmentThreshold) && (QRA >= parentFragmentThreshold))) {
190 member.divr_qla_qrb = divR_QLA_QRB;
191 member.divr_qlb_qra = divR_QLB_QRA;
192 member.qla_qrb = QLA_QRB;
193 member.qlb_qra = QLB_QRA;
203 member.winLStart = 0;
204 member.winLEnd = breakpoint;
205 member.winRStart = breakpoint+1;
206 member.winREnd = length-1;
207 member.querySeq = *(q);
208 member.parentA = *(pA);
209 member.parentB = *(pB);
213 member.chimeraMax = 0;
215 data.push_back(member);
225 catch(exception& e) {
226 m->errorOut(e, "Slayer", "runBellerophon");
230 /***********************************************************************/
231 vector<snps> Slayer::getSNPS(string parentA, string query, string parentB, int left, int right) {
236 for (int i = left; i <= right; i++) {
242 if ((A != Q) || (B != Q)) {
244 //ensure not neighboring a gap. change to 12/09 release of chimeraSlayer - not sure what this adds, but it eliminates alot of SNPS
248 //did query loose a base here during filter??
249 ( i == 0 || abs (baseSpots[0][i] - baseSpots[0][i-1]) == 1) &&
250 ( i == query.length()-1 || abs (baseSpots[0][i] - baseSpots[0][i+1]) == 1)
252 //did parentA loose a base here during filter??
253 ( i == 0 || abs (baseSpots[1][i] - baseSpots[1][i-1]) == 1) &&
254 ( i == parentA.length()-1 || abs (baseSpots[1][i] - baseSpots[1][i+1]) == 1)
256 //did parentB loose a base here during filter??
257 ( i == 0 || abs (baseSpots[2][i] - baseSpots[2][i-1]) == 1) &&
258 ( i == parentB.length()-1 || abs (baseSpots[2][i] - baseSpots[2][i+1]) == 1)
262 member.queryChar = Q;
263 member.parentAChar = A;
264 member.parentBChar = B;
265 data.push_back(member);
273 catch(exception& e) {
274 m->errorOut(e, "Slayer", "getSNPS");
278 /***********************************************************************/
279 int Slayer::bootstrapSNPS(vector<snps> left, vector<snps> right, float& BSA, float& BSB, int numIters) {
282 srand((unsigned)time( NULL ));
284 int count_A = 0; // sceneario QLA,QRB supported
285 int count_B = 0; // sceneario QLB,QRA supported
287 int numLeft = max(1, int(left.size() * percentSNPSample/(float)100 + 0.5));
288 int numRight = max(1, int(right.size() * percentSNPSample/(float)100 + 0.5));
290 for (int i = 0; i < numIters; i++) {
291 //random sampling with replacement.
293 if (m->control_pressed) { return 0; }
295 vector<snps> selectedLeft;
297 for (int j = 0; j < numLeft; j++) {
298 int index = int(rand() % left.size());
299 selectedLeft.push_back(left[index]);
302 vector<snps> selectedRight;
303 for (int j = 0; j < numRight; j++) {
304 int index = int(rand() % right.size());
305 selectedRight.push_back(right[index]);
308 /* A ------------------------------------------
310 # Q ------------------------------------------
313 # Q ------------------------------------------
315 # B ------------------------------------------ */
318 float QLA = snpQA(selectedLeft);
319 float QRA = snpQA(selectedRight);
321 float QLB = snpQB(selectedLeft);
322 float QRB = snpQB(selectedRight);
324 //in original - not used - not sure why?
325 //float ALB = snpAB(selectedLeft);
326 //float ARB = snpAB(selectedRight);
328 if ((QLA > QLB) && (QRB > QRA)) {
332 if ((QLB > QLA) && (QRA > QRB)) {
336 //cout << "selected left snp: \n";
337 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentAChar; }
339 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].queryChar; }
341 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentBChar; }
343 //cout << "selected right snp: \n";
344 //for (int j = 0; j < selectedRight.size(); j++) { cout << selectedRight[j].parentAChar; }
346 //for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].queryChar; }
348 //for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].parentBChar; }
353 //cout << count_A << '\t' << count_B << endl;
355 BSA = (float) count_A / (float) numIters * 100;
356 BSB = (float) count_B / (float) numIters * 100;
357 //cout << "bsa = " << BSA << " bsb = " << BSB << endl;
362 catch(exception& e) {
363 m->errorOut(e, "Slayer", "bootstrapSNPS");
367 /***********************************************************************/
368 float Slayer::snpQA(vector<snps> data) {
371 int numIdentical = 0;
373 for (int i = 0; i < data.size(); i++) {
374 if (data[i].parentAChar == data[i].queryChar) {
379 float percentID = (numIdentical / (float) data.size()) * 100;
383 catch(exception& e) {
384 m->errorOut(e, "Slayer", "snpQA");
388 /***********************************************************************/
389 float Slayer::snpQB(vector<snps> data) {
392 int numIdentical = 0;
394 for (int i = 0; i < data.size(); i++) {
395 if (data[i].parentBChar == data[i].queryChar) {
400 float percentID = (numIdentical / (float) data.size()) * 100;
405 catch(exception& e) {
406 m->errorOut(e, "Slayer", "snpQB");
410 /***********************************************************************/
411 float Slayer::snpAB(vector<snps> data) {
413 int numIdentical = 0;
415 for (int i = 0; i < data.size(); i++) {
416 if (data[i].parentAChar == data[i].parentBChar) {
421 float percentID = (numIdentical / (float) data.size()) * 100;
426 catch(exception& e) {
427 m->errorOut(e, "Slayer", "snpAB");
431 /***********************************************************************/
432 float Slayer::computePercentID(string queryAlign, string chimera, int left, int right) {
435 int numIdentical = 0;
438 for (int i = left; i <= right; i++) {
439 if (((queryAlign[i] != 'G') && (queryAlign[i] != 'T') && (queryAlign[i] != 'A') && (queryAlign[i] != 'C')&& (queryAlign[i] != '.') && (queryAlign[i] != '-')) ||
440 ((chimera[i] != 'G') && (chimera[i] != 'T') && (chimera[i] != 'A') && (chimera[i] != 'C')&& (chimera[i] != '.') && (chimera[i] != '-'))) {}
443 bool charA = false; bool charB = false;
444 if ((queryAlign[i] == 'G') || (queryAlign[i] == 'T') || (queryAlign[i] == 'A') || (queryAlign[i] == 'C')) { charA = true; }
445 if ((chimera[i] == 'G') || (chimera[i] == 'T') || (chimera[i] == 'A') || (chimera[i] == 'C')) { charB = true; }
447 if (charA || charB) {
449 if (charA) { countA++; }
450 if (charB) { countB++; }
452 if (queryAlign[i] == chimera[i]) {
460 float numBases = (countA + countB) /(float) 2;
462 if (numBases == 0) { return 0; }
464 float percentIdentical = (numIdentical/(float)numBases) * 100;
466 return percentIdentical;
469 catch(exception& e) {
470 m->errorOut(e, "Slayer", "computePercentID");
474 /***********************************************************************/
475 //remove columns that contain any gaps
476 map<int, int> Slayer::verticalFilter(vector<Sequence*> seqs) {
480 baseSpots.resize(3); //query, parentA, parentB
482 vector<int> gaps; gaps.resize(seqs[0]->getAligned().length(), 0);
484 string filterString = (string(seqs[0]->getAligned().length(), '1'));
487 for (int i = 0; i < seqs.size(); i++) {
489 string seqAligned = seqs[i]->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]++; }
497 //zero out spot where any sequences have blanks
498 int numColRemoved = 0;
500 map<int, int> maskMap; maskMap.clear();
502 for(int i = 0; i < seqs[0]->getAligned().length(); i++){
503 if(gaps[i] != 0) { filterString[i] = '0'; numColRemoved++; }
511 for (int i = 0; i < seqs.size(); i++) {
513 string seqAligned = seqs[i]->getAligned();
514 string newAligned = "";
518 for (int j = 0; j < seqAligned.length(); j++) {
520 if ((seqAligned[j] != '-') && (seqAligned[j] != '.') && (toupper(seqAligned[j]) != 'N')) { baseCount++; }
522 //if this spot is not a gap
523 if (filterString[j] == '1') {
524 newAligned += seqAligned[j];
525 baseSpots[i][count] = baseCount;
530 seqs[i]->setAligned(newAligned);
535 catch(exception& e) {
536 m->errorOut(e, "Slayer", "verticalFilter");
540 /***********************************************************************/