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) :
14 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();
20 for (int i = 0; i < refSeqs.size(); i++) {
22 for (int j = i+1; j < refSeqs.size(); j++) {
24 if (m->control_pressed) { return "no"; }
26 //make copies of query and each parent because runBellerophon removes gaps and messes them up
27 Sequence* q = new Sequence(query->getName(), query->getAligned());
28 Sequence* leftParent = new Sequence(refSeqs[i]->getName(), refSeqs[i]->getAligned());
29 Sequence* rightParent = new Sequence(refSeqs[j]->getName(), refSeqs[j]->getAligned());
31 map<int, int> spots; //map from spot in original sequence to spot in filtered sequence for query and both parents
32 vector<data_struct> divs = runBellerophon(q, leftParent, rightParent, spots);
34 if (m->control_pressed) {
41 // cout << divs.size() << endl;
42 vector<data_struct> selectedDivs;
43 for (int k = 0; k < divs.size(); k++) {
45 vector<snps> snpsLeft = getSNPS(divs[k].parentA.getAligned(), divs[k].querySeq.getAligned(), divs[k].parentB.getAligned(), divs[k].winLStart, divs[k].winLEnd);
46 vector<snps> snpsRight = getSNPS(divs[k].parentA.getAligned(), divs[k].querySeq.getAligned(), divs[k].parentB.getAligned(), divs[k].winRStart, divs[k].winREnd);
48 if (m->control_pressed) {
55 int numSNPSLeft = snpsLeft.size();
56 int numSNPSRight = snpsRight.size();
58 //require at least 3 SNPs on each side of the break
59 // if ((numSNPSLeft >= 3) && (numSNPSRight >= 3)) {
60 if ((numSNPSLeft >= 4) && (numSNPSRight >= 4)) {
62 //removed in 12/09 version of chimeraSlayer
63 //int winSizeLeft = divs[k].winLEnd - divs[k].winLStart + 1;
64 //int winSizeRight = divs[k].winREnd - divs[k].winRStart + 1;
66 //float snpRateLeft = numSNPSLeft / (float) winSizeLeft;
67 //float snpRateRight = numSNPSRight / (float) winSizeRight;
68 //float logR = log(snpRateLeft / snpRateRight) / log(2.0);
70 // do not accept excess snp ratio on either side of the break
71 //if (abs(logR) < 1 ) {
74 bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B);
76 if (m->control_pressed) {
86 divs[k].bsMax = max(BS_A, BS_B);
88 divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra);
90 //so results reflect orignal alignment
91 divs[k].winLStart = spots[divs[k].winLStart];
92 divs[k].winLEnd = spots[divs[k].winLEnd];
93 divs[k].winRStart = spots[divs[k].winRStart];
94 divs[k].winREnd = spots[divs[k].winREnd];
96 selectedDivs.push_back(divs[k]);
102 for (int m = 0; m < selectedDivs.size(); m++) { all.push_back(selectedDivs[m]); }
110 // compute bootstrap support
111 if (all.size() > 0) {
113 sort(all.begin(), all.end(), compareDataStruct);
114 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->getAligned() << endl << endl;
149 cout << pB->getName() << endl << pB->getAligned() << endl << endl;
150 cout << " length = " << length << endl;
151 cout << q->getName() << endl;
152 cout << pA->getName() << '\t';
153 cout << pB->getName() << endl;*/
156 if (length < (2*windowSize+windowStep)) {
157 // 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();
158 windowSize = length / 4;
161 for (int i = windowSize-1; i <= (length - windowSize); i += windowStep) {
163 if (m->control_pressed) { return data; }
166 int leftLength = breakpoint + 1;
167 int rightLength = length - leftLength;
169 float QLA = computePercentID(query, parentA, 0, breakpoint);
170 float QRB = computePercentID(query, parentB, breakpoint+1, length - 1);
172 float QLB = computePercentID(query, parentB, 0, breakpoint);
173 float QRA = computePercentID(query, parentA, breakpoint+1, length - 1);
175 float LAB = computePercentID(parentA, parentB, 0, breakpoint);
176 float RAB = computePercentID(parentA, parentB, breakpoint+1, length - 1);
178 float AB = ((LAB*leftLength) + (RAB*rightLength)) / (float) length;
179 float QA = ((QLA*leftLength) + (QRA*rightLength)) / (float) length;
180 float QB = ((QLB*leftLength) + (QRB*rightLength)) / (float) length;
182 float QLA_QRB = ((QLA*leftLength) + (QRB*rightLength)) / (float) length;
183 float QLB_QRA = ((QLB*leftLength) + (QRA*rightLength)) / (float) length;
185 //in original and not used
186 //float avgQA_QB = ((QA*leftLength) + (QB*rightLength)) / (float) length;
188 float divR_QLA_QRB = min((QLA_QRB/QA), (QLA_QRB/QB));
189 float divR_QLB_QRA = min((QLB_QRA/QA), (QLB_QRA/QB));
190 //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;
192 //cout << divRThreshold << endl;
193 //cout << breakpoint << '\t' << divR_QLA_QRB << '\t' << divR_QLB_QRA << endl;
194 //is one of them above the
195 if (divR_QLA_QRB >= divRThreshold || divR_QLB_QRA >= divRThreshold) {
197 if (((QLA_QRB > QA) && (QLA_QRB > QB) && (QLA >= parentFragmentThreshold) && (QRB >= parentFragmentThreshold)) ||
198 ((QLB_QRA > QA) && (QLB_QRA > QB) && (QLB >=parentFragmentThreshold) && (QRA >= parentFragmentThreshold))) {
202 member.divr_qla_qrb = divR_QLA_QRB;
203 member.divr_qlb_qra = divR_QLB_QRA;
204 member.qla_qrb = QLA_QRB;
205 member.qlb_qra = QLB_QRA;
215 member.winLStart = 0;
216 member.winLEnd = breakpoint;
217 member.winRStart = breakpoint+1;
218 member.winREnd = length-1;
219 member.querySeq = *(q);
220 member.parentA = *(pA);
221 member.parentB = *(pB);
225 member.chimeraMax = 0;
227 data.push_back(member);
236 catch(exception& e) {
237 m->errorOut(e, "Slayer", "runBellerophon");
241 /***********************************************************************/
242 vector<snps> Slayer::getSNPS(string parentA, string query, string parentB, int left, int right) {
246 //cout << left << '\t' << right << endl;
247 for (int i = left; i <= right; i++) {
253 if ((A != Q) || (B != Q)) {
254 //cout << "not equal " << Q << '\t' << A << '\t' << B << endl;
256 //ensure not neighboring a gap. change to 12/09 release of chimeraSlayer - not sure what this adds, but it eliminates alot of SNPS
258 //did query loose a base here during filter??
259 ( i == 0 || abs (baseSpots[0][i] - baseSpots[0][i-1]) == 1) &&
260 ( i == query.length() || abs (baseSpots[0][i] - baseSpots[0][i+1]) == 1)
262 //did parentA loose a base here during filter??
263 ( i == 0 || abs (baseSpots[1][i] - baseSpots[1][i-1]) == 1) &&
264 ( i == parentA.length() || abs (baseSpots[1][i] - baseSpots[1][i+1]) == 1)
266 //did parentB loose a base here during filter??
267 ( i == 0 || abs (baseSpots[2][i] - baseSpots[2][i-1]) == 1) &&
268 ( i == parentB.length() || abs (baseSpots[2][i] - baseSpots[2][i+1]) == 1)
273 member.queryChar = Q;
274 member.parentAChar = A;
275 member.parentBChar = B;
276 //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;
277 data.push_back(member);
280 // cout << i << '\t' << data.size() << endl;
286 catch(exception& e) {
287 m->errorOut(e, "Slayer", "getSNPS");
291 /***********************************************************************/
292 int Slayer::bootstrapSNPS(vector<snps> left, vector<snps> right, float& BSA, float& BSB) {
295 srand((unsigned)time( NULL ));
297 int count_A = 0; // sceneario QLA,QRB supported
298 int count_B = 0; // sceneario QLB,QRA supported
300 int numLeft = max(1, int(left.size() * percentSNPSample/(float)100 + 0.5));
301 int numRight = max(1, int(right.size() * percentSNPSample/(float)100 + 0.5));
303 for (int i = 0; i < iters; i++) {
304 //random sampling with replacement.
306 if (m->control_pressed) { return 0; }
308 vector<snps> selectedLeft;
310 for (int j = 0; j < numLeft; j++) {
311 int index = int(rand() % left.size());
312 selectedLeft.push_back(left[index]);
315 vector<snps> selectedRight;
316 for (int j = 0; j < numRight; j++) {
317 int index = int(rand() % right.size());
318 selectedRight.push_back(right[index]);
321 /* A ------------------------------------------
323 # Q ------------------------------------------
326 # Q ------------------------------------------
328 # B ------------------------------------------ */
331 float QLA = snpQA(selectedLeft);
332 float QRA = snpQA(selectedRight);
334 float QLB = snpQB(selectedLeft);
335 float QRB = snpQB(selectedRight);
337 //in original - not used - not sure why?
338 //float ALB = snpAB(selectedLeft);
339 //float ARB = snpAB(selectedRight);
341 if ((QLA > QLB) && (QRB > QRA)) {
345 if ((QLB > QLA) && (QRA > QRB)) {
349 //cout << "selected left snp: \n";
350 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentAChar; }
352 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].queryChar; }
354 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentBChar; }
356 //cout << "selected right snp: \n";
357 //for (int j = 0; j < selectedRight.size(); j++) { cout << selectedRight[j].parentAChar; }
359 //for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].queryChar; }
361 //for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].parentBChar; }
368 BSA = (float) count_A / (float) iters * 100;
369 BSB = (float) count_B / (float) iters * 100;
370 //cout << "bsa = " << BSA << " bsb = " << BSB << endl;
375 catch(exception& e) {
376 m->errorOut(e, "Slayer", "bootstrapSNPS");
380 /***********************************************************************/
381 float Slayer::snpQA(vector<snps> data) {
384 int numIdentical = 0;
386 for (int i = 0; i < data.size(); i++) {
387 if (data[i].parentAChar == data[i].queryChar) {
392 float percentID = (numIdentical / (float) data.size()) * 100;
396 catch(exception& e) {
397 m->errorOut(e, "Slayer", "snpQA");
401 /***********************************************************************/
402 float Slayer::snpQB(vector<snps> data) {
405 int numIdentical = 0;
407 for (int i = 0; i < data.size(); i++) {
408 if (data[i].parentBChar == data[i].queryChar) {
413 float percentID = (numIdentical / (float) data.size()) * 100;
418 catch(exception& e) {
419 m->errorOut(e, "Slayer", "snpQB");
423 /***********************************************************************/
424 float Slayer::snpAB(vector<snps> data) {
426 int numIdentical = 0;
428 for (int i = 0; i < data.size(); i++) {
429 if (data[i].parentAChar == data[i].parentBChar) {
434 float percentID = (numIdentical / (float) data.size()) * 100;
439 catch(exception& e) {
440 m->errorOut(e, "Slayer", "snpAB");
444 /***********************************************************************/
445 float Slayer::computePercentID(string queryFrag, string parent, int left, int right) {
450 for (int i = left; i <= right; i++) {
452 if (queryFrag[i] == parent[i]) {
457 float percentID =( matches/(float)total) * 100;
461 catch(exception& e) {
462 m->errorOut(e, "Slayer", "computePercentID");
466 /***********************************************************************/
467 //remove columns that contain any gaps
468 map<int, int> Slayer::verticalFilter(vector<Sequence*> seqs) {
472 baseSpots.resize(3); //query, parentA, parentB
474 vector<int> gaps; gaps.resize(seqs[0]->getAligned().length(), 0);
476 string filterString = (string(seqs[0]->getAligned().length(), '1'));
479 for (int i = 0; i < seqs.size(); i++) {
481 string seqAligned = seqs[i]->getAligned();
483 for (int j = 0; j < seqAligned.length(); j++) {
484 //if this spot is a gap
485 if ((seqAligned[j] == '-') || (seqAligned[j] == '.') || (toupper(seqAligned[j]) == 'N')) { gaps[j]++; }
489 //zero out spot where any sequences have blanks
490 int numColRemoved = 0;
492 map<int, int> maskMap; maskMap.clear();
494 for(int i = 0; i < seqs[0]->getAligned().length(); i++){
495 if(gaps[i] != 0) { filterString[i] = '0'; numColRemoved++; }
503 for (int i = 0; i < seqs.size(); i++) {
505 string seqAligned = seqs[i]->getAligned();
506 string newAligned = "";
510 for (int j = 0; j < seqAligned.length(); j++) {
512 if ((seqAligned[j] != '-') && (seqAligned[j] != '.') && (toupper(seqAligned[j]) != 'N')) { baseCount++; }
514 //if this spot is not a gap
515 if (filterString[j] == '1') {
516 newAligned += seqAligned[j];
517 baseSpots[i][count] = baseCount;
522 seqs[i]->setAligned(newAligned);
527 catch(exception& e) {
528 m->errorOut(e, "Slayer", "verticalFilter");
532 /***********************************************************************/