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)) && (iters < 1000)) {
70 bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B, 1000);
72 if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
76 divs[k].bsMax = max(BS_A, BS_B);
77 divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra);
80 //so results reflect orignal alignment
81 divs[k].winLStart = spots[divs[k].winLStart];
82 divs[k].winLEnd = spots[divs[k].winLEnd];
83 divs[k].winRStart = spots[divs[k].winRStart];
84 divs[k].winREnd = spots[divs[k].winREnd];
86 selectedDivs.push_back(divs[k]);
91 for (int mi = 0; mi < selectedDivs.size(); mi++) { all.push_back(selectedDivs[mi]); }
99 // compute bootstrap support
100 if (all.size() > 0) {
102 sort(all.begin(), all.end(), compareDataStruct);
103 reverse(all.begin(), all.end());
112 catch(exception& e) {
113 m->errorOut(e, "Slayer", "getResults");
117 /***********************************************************************/
118 vector<data_struct> Slayer::runBellerophon(Sequence* q, Sequence* pA, Sequence* pB, map<int, int>& spots) {
121 vector<data_struct> data;
124 vector<Sequence*> temp;
125 temp.push_back(q); temp.push_back(pA); temp.push_back(pB);
127 //maps spot in new alignment to spot in alignment before filter
128 spots = verticalFilter(temp); //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));
175 //cout << q->getName() << '\t';
176 //cout << pA->getName() << '\t';
177 //cout << pB->getName() << '\t';
178 // cout << "bp: " << breakpoint << " CHIM_TYPE_A\t" << divR_QLA_QRB << "\tQLA: " << QLA << "\tQRB: " << QRB << "\tQLA_QRB: " << QLA_QRB;
179 //cout << "\tCHIM_TYPE_B\t" << divR_QLB_QRA << "\tQLB: " << QLB << "\tQRA: " << QRA << "\tQLB_QRA: " << QLB_QRA << endl;
180 //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;
182 //cout << divRThreshold << endl;
183 //cout << breakpoint << '\t' << divR_QLA_QRB << '\t' << divR_QLB_QRA << endl;
184 //is one of them above the
185 if (divR_QLA_QRB >= divRThreshold || divR_QLB_QRA >= divRThreshold) {
187 if (((QLA_QRB > QA) && (QLA_QRB > QB) && (QLA >= parentFragmentThreshold) && (QRB >= parentFragmentThreshold)) ||
188 ((QLB_QRA > QA) && (QLB_QRA > QB) && (QLB >=parentFragmentThreshold) && (QRA >= parentFragmentThreshold))) {
192 member.divr_qla_qrb = divR_QLA_QRB;
193 member.divr_qlb_qra = divR_QLB_QRA;
194 member.qla_qrb = QLA_QRB;
195 member.qlb_qra = QLB_QRA;
205 member.winLStart = 0;
206 member.winLEnd = breakpoint;
207 member.winRStart = breakpoint+1;
208 member.winREnd = length-1;
209 member.querySeq = *(q);
210 member.parentA = *(pA);
211 member.parentB = *(pB);
215 member.chimeraMax = 0;
217 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) {
236 //cout << left << '\t' << right << endl;
237 for (int i = left; i <= right; i++) {
243 if ((A != Q) || (B != Q)) {
244 //cout << "not equal " << Q << '\t' << A << '\t' << B << endl;
246 //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() || 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() || 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() || abs (baseSpots[2][i] - baseSpots[2][i+1]) == 1)
263 member.queryChar = Q;
264 member.parentAChar = A;
265 member.parentBChar = B;
266 //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;
267 data.push_back(member);
270 // cout << i << '\t' << data.size() << endl;
276 catch(exception& e) {
277 m->errorOut(e, "Slayer", "getSNPS");
281 /***********************************************************************/
282 int Slayer::bootstrapSNPS(vector<snps> left, vector<snps> right, float& BSA, float& BSB, int numIters) {
285 srand((unsigned)time( NULL ));
287 int count_A = 0; // sceneario QLA,QRB supported
288 int count_B = 0; // sceneario QLB,QRA supported
290 int numLeft = max(1, int(left.size() * percentSNPSample/(float)100 + 0.5));
291 int numRight = max(1, int(right.size() * percentSNPSample/(float)100 + 0.5));
292 //cout << numLeft << '\t' << numRight << endl;
293 for (int i = 0; i < numIters; i++) {
294 //random sampling with replacement.
296 if (m->control_pressed) { return 0; }
298 vector<snps> selectedLeft;
300 for (int j = 0; j < numLeft; j++) {
301 int index = int(rand() % left.size());
302 selectedLeft.push_back(left[index]);
305 vector<snps> selectedRight;
306 for (int j = 0; j < numRight; j++) {
307 int index = int(rand() % right.size());
308 selectedRight.push_back(right[index]);
311 /* A ------------------------------------------
313 # Q ------------------------------------------
316 # Q ------------------------------------------
318 # B ------------------------------------------ */
321 float QLA = snpQA(selectedLeft);
322 float QRA = snpQA(selectedRight);
324 float QLB = snpQB(selectedLeft);
325 float QRB = snpQB(selectedRight);
327 //in original - not used - not sure why?
328 //float ALB = snpAB(selectedLeft);
329 //float ARB = snpAB(selectedRight);
331 if ((QLA > QLB) && (QRB > QRA)) {
335 if ((QLB > QLA) && (QRA > QRB)) {
339 //cout << "selected left snp: \n";
340 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentAChar; }
342 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].queryChar; }
344 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentBChar; }
346 //cout << "selected right snp: \n";
347 //for (int j = 0; j < selectedRight.size(); j++) { cout << selectedRight[j].parentAChar; }
349 //for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].queryChar; }
351 //for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].parentBChar; }
356 //cout << count_A << '\t' << count_B << endl;
358 BSA = (float) count_A / (float) numIters * 100;
359 BSB = (float) count_B / (float) numIters * 100;
360 //cout << "bsa = " << BSA << " bsb = " << BSB << endl;
365 catch(exception& e) {
366 m->errorOut(e, "Slayer", "bootstrapSNPS");
370 /***********************************************************************/
371 float Slayer::snpQA(vector<snps> data) {
374 int numIdentical = 0;
376 for (int i = 0; i < data.size(); i++) {
377 if (data[i].parentAChar == data[i].queryChar) {
382 float percentID = (numIdentical / (float) data.size()) * 100;
386 catch(exception& e) {
387 m->errorOut(e, "Slayer", "snpQA");
391 /***********************************************************************/
392 float Slayer::snpQB(vector<snps> data) {
395 int numIdentical = 0;
397 for (int i = 0; i < data.size(); i++) {
398 if (data[i].parentBChar == data[i].queryChar) {
403 float percentID = (numIdentical / (float) data.size()) * 100;
408 catch(exception& e) {
409 m->errorOut(e, "Slayer", "snpQB");
413 /***********************************************************************/
414 float Slayer::snpAB(vector<snps> data) {
416 int numIdentical = 0;
418 for (int i = 0; i < data.size(); i++) {
419 if (data[i].parentAChar == data[i].parentBChar) {
424 float percentID = (numIdentical / (float) data.size()) * 100;
429 catch(exception& e) {
430 m->errorOut(e, "Slayer", "snpAB");
434 /***********************************************************************/
435 float Slayer::computePercentID(string queryFrag, string parent, int left, int right) {
440 for (int i = left; i <= right; i++) {
442 if (queryFrag[i] == parent[i]) {
447 float percentID =( matches/(float)total) * 100;
451 catch(exception& e) {
452 m->errorOut(e, "Slayer", "computePercentID");
456 /***********************************************************************/
457 //remove columns that contain any gaps
458 map<int, int> Slayer::verticalFilter(vector<Sequence*> seqs) {
462 baseSpots.resize(3); //query, parentA, parentB
464 vector<int> gaps; gaps.resize(seqs[0]->getAligned().length(), 0);
466 string filterString = (string(seqs[0]->getAligned().length(), '1'));
469 for (int i = 0; i < seqs.size(); i++) {
471 string seqAligned = seqs[i]->getAligned();
473 for (int j = 0; j < seqAligned.length(); j++) {
474 //if this spot is a gap
475 if ((seqAligned[j] == '-') || (seqAligned[j] == '.') || (toupper(seqAligned[j]) == 'N')) { gaps[j]++; }
479 //zero out spot where any sequences have blanks
480 int numColRemoved = 0;
482 map<int, int> maskMap; maskMap.clear();
484 for(int i = 0; i < seqs[0]->getAligned().length(); i++){
485 if(gaps[i] != 0) { filterString[i] = '0'; numColRemoved++; }
493 for (int i = 0; i < seqs.size(); i++) {
495 string seqAligned = seqs[i]->getAligned();
496 string newAligned = "";
500 for (int j = 0; j < seqAligned.length(); j++) {
502 if ((seqAligned[j] != '-') && (seqAligned[j] != '.') && (toupper(seqAligned[j]) != 'N')) { baseCount++; }
504 //if this spot is not a gap
505 if (filterString[j] == '1') {
506 newAligned += seqAligned[j];
507 baseSpots[i][count] = baseCount;
512 seqs[i]->setAligned(newAligned);
517 catch(exception& e) {
518 m->errorOut(e, "Slayer", "verticalFilter");
522 /***********************************************************************/