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]); }
93 // compute bootstrap support
96 sort(all.begin(), all.end(), compareDataStruct);
97 reverse(all.begin(), all.end());
106 catch(exception& e) {
107 m->errorOut(e, "Slayer", "getResults");
111 /***********************************************************************/
112 vector<data_struct> Slayer::runBellerophon(Sequence* q, Sequence* pA, Sequence* pB, map<int, int>& spots) {
115 vector<data_struct> data;
118 vector<Sequence*> temp;
119 temp.push_back(q); temp.push_back(pA); temp.push_back(pB);
121 //maps spot in new alignment to spot in alignment before filter
122 spots = verticalFilter(temp); //fills baseSpots
124 //get these to avoid numerous function calls
125 string query = q->getAligned();
126 string parentA = pA->getAligned();
127 string parentB = pB->getAligned();
128 int length = query.length();
129 //cout << q->getName() << endl << q->getAligned() << endl << endl;
130 //cout << pA->getName() << endl << pA->getUnaligned() << endl << endl;
131 //cout << pB->getName() << endl << pB->getUnaligned() << endl << endl;
132 //cout << " length = " << length << endl;
135 if (length < (2*windowSize+windowStep)) {
136 // 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();
137 windowSize = length / 4;
140 for (int i = windowSize-1; i <= (length - windowSize); i += windowStep) {
142 if (m->control_pressed) { return data; }
145 int leftLength = breakpoint + 1;
146 int rightLength = length - leftLength;
148 float QLA = computePercentID(query, parentA, 0, breakpoint);
149 float QRB = computePercentID(query, parentB, breakpoint+1, length - 1);
151 float QLB = computePercentID(query, parentB, 0, breakpoint);
152 float QRA = computePercentID(query, parentA, breakpoint+1, length - 1);
154 float LAB = computePercentID(parentA, parentB, 0, breakpoint);
155 float RAB = computePercentID(parentA, parentB, breakpoint+1, length - 1);
157 float AB = ((LAB*leftLength) + (RAB*rightLength)) / (float) length;
158 float QA = ((QLA*leftLength) + (QRA*rightLength)) / (float) length;
159 float QB = ((QLB*leftLength) + (QRB*rightLength)) / (float) length;
161 float QLA_QRB = ((QLA*leftLength) + (QRB*rightLength)) / (float) length;
162 float QLB_QRA = ((QLB*leftLength) + (QRA*rightLength)) / (float) length;
164 //in original and not used
165 //float avgQA_QB = ((QA*leftLength) + (QB*rightLength)) / (float) length;
167 float divR_QLA_QRB = min((QLA_QRB/QA), (QLA_QRB/QB));
168 float divR_QLB_QRA = min((QLB_QRA/QA), (QLB_QRA/QB));
169 //cout << q->getName() << '\t';
170 //cout << pA->getName() << '\t';
171 //cout << pB->getName() << '\t';
172 // cout << "bp: " << breakpoint << " CHIM_TYPE_A\t" << divR_QLA_QRB << "\tQLA: " << QLA << "\tQRB: " << QRB << "\tQLA_QRB: " << QLA_QRB;
173 //cout << "\tCHIM_TYPE_B\t" << divR_QLB_QRA << "\tQLB: " << QLB << "\tQRA: " << QRA << "\tQLB_QRA: " << QLB_QRA << endl;
174 //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;
176 //cout << divRThreshold << endl;
177 //cout << breakpoint << '\t' << divR_QLA_QRB << '\t' << divR_QLB_QRA << endl;
178 //is one of them above the
179 if (divR_QLA_QRB >= divRThreshold || divR_QLB_QRA >= divRThreshold) {
181 if (((QLA_QRB > QA) && (QLA_QRB > QB) && (QLA >= parentFragmentThreshold) && (QRB >= parentFragmentThreshold)) ||
182 ((QLB_QRA > QA) && (QLB_QRA > QB) && (QLB >=parentFragmentThreshold) && (QRA >= parentFragmentThreshold))) {
186 member.divr_qla_qrb = divR_QLA_QRB;
187 member.divr_qlb_qra = divR_QLB_QRA;
188 member.qla_qrb = QLA_QRB;
189 member.qlb_qra = QLB_QRA;
199 member.winLStart = 0;
200 member.winLEnd = breakpoint;
201 member.winRStart = breakpoint+1;
202 member.winREnd = length-1;
203 member.querySeq = *(q);
204 member.parentA = *(pA);
205 member.parentB = *(pB);
209 member.chimeraMax = 0;
211 data.push_back(member);
220 catch(exception& e) {
221 m->errorOut(e, "Slayer", "runBellerophon");
225 /***********************************************************************/
226 vector<snps> Slayer::getSNPS(string parentA, string query, string parentB, int left, int right) {
230 //cout << left << '\t' << right << endl;
231 for (int i = left; i <= right; i++) {
237 if ((A != Q) || (B != Q)) {
238 //cout << "not equal " << Q << '\t' << A << '\t' << B << endl;
240 //ensure not neighboring a gap. change to 12/09 release of chimeraSlayer - not sure what this adds, but it eliminates alot of SNPS
242 //did query loose a base here during filter??
243 ( i == 0 || abs (baseSpots[0][i] - baseSpots[0][i-1]) == 1) &&
244 ( i == query.length() || abs (baseSpots[0][i] - baseSpots[0][i+1]) == 1)
246 //did parentA loose a base here during filter??
247 ( i == 0 || abs (baseSpots[1][i] - baseSpots[1][i-1]) == 1) &&
248 ( i == parentA.length() || abs (baseSpots[1][i] - baseSpots[1][i+1]) == 1)
250 //did parentB loose a base here during filter??
251 ( i == 0 || abs (baseSpots[2][i] - baseSpots[2][i-1]) == 1) &&
252 ( i == parentB.length() || abs (baseSpots[2][i] - baseSpots[2][i+1]) == 1)
257 member.queryChar = Q;
258 member.parentAChar = A;
259 member.parentBChar = B;
260 //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;
261 data.push_back(member);
264 // cout << i << '\t' << data.size() << endl;
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));
286 //cout << numLeft << '\t' << numRight << endl;
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 queryFrag, string parent, int left, int right) {
434 for (int i = left; i <= right; i++) {
436 if (queryFrag[i] == parent[i]) {
441 float percentID =( matches/(float)total) * 100;
445 catch(exception& e) {
446 m->errorOut(e, "Slayer", "computePercentID");
450 /***********************************************************************/
451 //remove columns that contain any gaps
452 map<int, int> Slayer::verticalFilter(vector<Sequence*> seqs) {
456 baseSpots.resize(3); //query, parentA, parentB
458 vector<int> gaps; gaps.resize(seqs[0]->getAligned().length(), 0);
460 string filterString = (string(seqs[0]->getAligned().length(), '1'));
463 for (int i = 0; i < seqs.size(); i++) {
465 string seqAligned = seqs[i]->getAligned();
467 for (int j = 0; j < seqAligned.length(); j++) {
468 //if this spot is a gap
469 if ((seqAligned[j] == '-') || (seqAligned[j] == '.') || (toupper(seqAligned[j]) == 'N')) { gaps[j]++; }
473 //zero out spot where any sequences have blanks
474 int numColRemoved = 0;
476 map<int, int> maskMap; maskMap.clear();
478 for(int i = 0; i < seqs[0]->getAligned().length(); i++){
479 if(gaps[i] != 0) { filterString[i] = '0'; numColRemoved++; }
487 for (int i = 0; i < seqs.size(); i++) {
489 string seqAligned = seqs[i]->getAligned();
490 string newAligned = "";
494 for (int j = 0; j < seqAligned.length(); j++) {
496 if ((seqAligned[j] != '-') && (seqAligned[j] != '.') && (toupper(seqAligned[j]) != 'N')) { baseCount++; }
498 //if this spot is not a gap
499 if (filterString[j] == '1') {
500 newAligned += seqAligned[j];
501 baseSpots[i][count] = baseCount;
506 seqs[i]->setAligned(newAligned);
511 catch(exception& e) {
512 m->errorOut(e, "Slayer", "verticalFilter");
516 /***********************************************************************/