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)) {
61 //removed in 12/09 version of chimeraSlayer
62 //int winSizeLeft = divs[k].winLEnd - divs[k].winLStart + 1;
63 //int winSizeRight = divs[k].winREnd - divs[k].winRStart + 1;
65 //float snpRateLeft = numSNPSLeft / (float) winSizeLeft;
66 //float snpRateRight = numSNPSRight / (float) winSizeRight;
67 //float logR = log(snpRateLeft / snpRateRight) / log(2.0);
69 // do not accept excess snp ratio on either side of the break
70 //if (abs(logR) < 1 ) {
73 bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B);
75 if (m->control_pressed) {
85 divs[k].bsMax = max(BS_A, BS_B);
87 divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra);
89 //so results reflect orignal alignment
90 divs[k].winLStart = spots[divs[k].winLStart];
91 divs[k].winLEnd = spots[divs[k].winLEnd];
92 divs[k].winRStart = spots[divs[k].winRStart];
93 divs[k].winREnd = spots[divs[k].winREnd];
95 selectedDivs.push_back(divs[k]);
101 for (int m = 0; m < selectedDivs.size(); m++) { all.push_back(selectedDivs[m]); }
109 // compute bootstrap support
110 if (all.size() > 0) {
112 sort(all.begin(), all.end(), compareDataStruct);
113 reverse(all.begin(), all.end());
123 catch(exception& e) {
124 m->errorOut(e, "Slayer", "getResults");
128 /***********************************************************************/
129 vector<data_struct> Slayer::runBellerophon(Sequence* q, Sequence* pA, Sequence* pB, map<int, int>& spots) {
132 vector<data_struct> data;
135 vector<Sequence*> temp;
136 temp.push_back(q); temp.push_back(pA); temp.push_back(pB);
138 //maps spot in new alignment to spot in alignment before filter
139 spots = verticalFilter(temp); //fills baseSpots
141 //get these to avoid numerous function calls
142 string query = q->getAligned();
143 string parentA = pA->getAligned();
144 string parentB = pB->getAligned();
145 int length = query.length();
146 /*cout << q->getName() << endl << q->getAligned() << endl << endl;
147 cout << pA->getName() << endl << pA->getAligned() << endl << endl;
148 cout << pB->getName() << endl << pB->getAligned() << endl << endl;
149 cout << " length = " << length << endl;
150 cout << q->getName() << endl;
151 cout << pA->getName() << '\t';
152 cout << pB->getName() << endl;*/
155 if (length < (2*windowSize+windowStep)) {
156 // 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();
157 windowSize = length / 4;
160 for (int i = windowSize-1; i <= (length - windowSize); i += windowStep) {
162 if (m->control_pressed) { return data; }
165 int leftLength = breakpoint + 1;
166 int rightLength = length - leftLength;
168 float QLA = computePercentID(query, parentA, 0, breakpoint);
169 float QRB = computePercentID(query, parentB, breakpoint+1, length - 1);
171 float QLB = computePercentID(query, parentB, 0, breakpoint);
172 float QRA = computePercentID(query, parentA, breakpoint+1, length - 1);
174 float LAB = computePercentID(parentA, parentB, 0, breakpoint);
175 float RAB = computePercentID(parentA, parentB, breakpoint+1, length - 1);
177 float AB = ((LAB*leftLength) + (RAB*rightLength)) / (float) length;
178 float QA = ((QLA*leftLength) + (QRA*rightLength)) / (float) length;
179 float QB = ((QLB*leftLength) + (QRB*rightLength)) / (float) length;
181 float QLA_QRB = ((QLA*leftLength) + (QRB*rightLength)) / (float) length;
182 float QLB_QRA = ((QLB*leftLength) + (QRA*rightLength)) / (float) length;
184 //in original and not used
185 //float avgQA_QB = ((QA*leftLength) + (QB*rightLength)) / (float) length;
187 float divR_QLA_QRB = min((QLA_QRB/QA), (QLA_QRB/QB));
188 float divR_QLB_QRA = min((QLB_QRA/QA), (QLB_QRA/QB));
189 //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;
191 //cout << divRThreshold << endl;
192 //cout << breakpoint << '\t' << divR_QLA_QRB << '\t' << divR_QLB_QRA << endl;
193 //is one of them above the
194 if (divR_QLA_QRB >= divRThreshold || divR_QLB_QRA >= divRThreshold) {
196 if (((QLA_QRB > QA) && (QLA_QRB > QB) && (QLA >= parentFragmentThreshold) && (QRB >= parentFragmentThreshold)) ||
197 ((QLB_QRA > QA) && (QLB_QRA > QB) && (QLB >=parentFragmentThreshold) && (QRA >= parentFragmentThreshold))) {
201 member.divr_qla_qrb = divR_QLA_QRB;
202 member.divr_qlb_qra = divR_QLB_QRA;
203 member.qla_qrb = QLA_QRB;
204 member.qlb_qra = QLB_QRA;
214 member.winLStart = 0;
215 member.winLEnd = breakpoint;
216 member.winRStart = breakpoint+1;
217 member.winREnd = length-1;
218 member.querySeq = *(q);
219 member.parentA = *(pA);
220 member.parentB = *(pB);
224 member.chimeraMax = 0;
226 data.push_back(member);
235 catch(exception& e) {
236 m->errorOut(e, "Slayer", "runBellerophon");
240 /***********************************************************************/
241 vector<snps> Slayer::getSNPS(string parentA, string query, string parentB, int left, int right) {
245 //cout << left << '\t' << right << endl;
246 for (int i = left; i <= right; i++) {
252 if ((A != Q) || (B != Q)) {
253 //cout << "not equal " << Q << '\t' << A << '\t' << B << endl;
255 //ensure not neighboring a gap. change to 12/09 release of chimeraSlayer - not sure what this adds, but it eliminates alot of SNPS
257 //did query loose a base here during filter??
258 ( i == 0 || abs (baseSpots[0][i] - baseSpots[0][i-1]) == 1) &&
259 ( i == query.length() || abs (baseSpots[0][i] - baseSpots[0][i+1]) == 1)
261 //did parentA loose a base here during filter??
262 ( i == 0 || abs (baseSpots[1][i] - baseSpots[1][i-1]) == 1) &&
263 ( i == parentA.length() || abs (baseSpots[1][i] - baseSpots[1][i+1]) == 1)
265 //did parentB loose a base here during filter??
266 ( i == 0 || abs (baseSpots[2][i] - baseSpots[2][i-1]) == 1) &&
267 ( i == parentB.length() || abs (baseSpots[2][i] - baseSpots[2][i+1]) == 1)
272 member.queryChar = Q;
273 member.parentAChar = A;
274 member.parentBChar = B;
275 //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;
276 data.push_back(member);
279 // cout << i << '\t' << data.size() << endl;
285 catch(exception& e) {
286 m->errorOut(e, "Slayer", "getSNPS");
290 /***********************************************************************/
291 int Slayer::bootstrapSNPS(vector<snps> left, vector<snps> right, float& BSA, float& BSB) {
294 srand((unsigned)time( NULL ));
296 int count_A = 0; // sceneario QLA,QRB supported
297 int count_B = 0; // sceneario QLB,QRA supported
299 int numLeft = max(1, int(left.size() * (percentSNPSample/(float)100) + 0.5));
300 int numRight = max(1, int(right.size() * (percentSNPSample/(float)100) + 0.5));
302 for (int i = 0; i < iters; i++) {
303 //random sampling with replacement.
305 if (m->control_pressed) { return 0; }
307 vector<snps> selectedLeft;
309 for (int j = 0; j < numLeft; j++) {
310 int index = int(rand() % left.size());
311 selectedLeft.push_back(left[index]);
314 vector<snps> selectedRight;
315 for (int j = 0; j < numRight; j++) {
316 int index = int(rand() % right.size());
317 selectedRight.push_back(right[index]);
320 /* A ------------------------------------------
322 # Q ------------------------------------------
325 # Q ------------------------------------------
327 # B ------------------------------------------ */
330 float QLA = snpQA(selectedLeft);
331 float QRA = snpQA(selectedRight);
333 float QLB = snpQB(selectedLeft);
334 float QRB = snpQB(selectedRight);
336 //in original - not used - not sure why?
337 //float ALB = snpAB(selectedLeft);
338 //float ARB = snpAB(selectedRight);
340 if ((QLA > QLB) && (QRB > QRA)) {
344 if ((QLB > QLA) && (QRA > QRB)) {
348 //cout << "selected left snp: \n";
349 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentAChar; }
351 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].queryChar; }
353 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentBChar; }
355 //cout << "selected right snp: \n";
356 //for (int j = 0; j < selectedRight.size(); j++) { cout << selectedRight[j].parentAChar; }
358 //for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].queryChar; }
360 //for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].parentBChar; }
367 BSA = ((float) count_A / (float) iters) * 100;
368 BSB = ((float) count_B / (float) iters) * 100;
369 //cout << "bsa = " << BSA << " bsb = " << BSB << endl;
374 catch(exception& e) {
375 m->errorOut(e, "Slayer", "bootstrapSNPS");
379 /***********************************************************************/
380 float Slayer::snpQA(vector<snps> data) {
383 int numIdentical = 0;
385 for (int i = 0; i < data.size(); i++) {
386 if (data[i].parentAChar == data[i].queryChar) {
391 float percentID = (numIdentical / (float) data.size()) * 100;
395 catch(exception& e) {
396 m->errorOut(e, "Slayer", "snpQA");
400 /***********************************************************************/
401 float Slayer::snpQB(vector<snps> data) {
404 int numIdentical = 0;
406 for (int i = 0; i < data.size(); i++) {
407 if (data[i].parentBChar == data[i].queryChar) {
412 float percentID = (numIdentical / (float) data.size()) * 100;
417 catch(exception& e) {
418 m->errorOut(e, "Slayer", "snpQB");
422 /***********************************************************************/
423 float Slayer::snpAB(vector<snps> data) {
425 int numIdentical = 0;
427 for (int i = 0; i < data.size(); i++) {
428 if (data[i].parentAChar == data[i].parentBChar) {
433 float percentID = (numIdentical / (float) data.size()) * 100;
438 catch(exception& e) {
439 m->errorOut(e, "Slayer", "snpAB");
443 /***********************************************************************/
444 float Slayer::computePercentID(string queryFrag, string parent, int left, int right) {
449 for (int i = left; i <= right; i++) {
451 if (queryFrag[i] == parent[i]) {
456 float percentID =( matches/(float)total) * 100;
460 catch(exception& e) {
461 m->errorOut(e, "Slayer", "computePercentID");
465 /***********************************************************************/
466 //remove columns that contain any gaps
467 map<int, int> Slayer::verticalFilter(vector<Sequence*> seqs) {
471 baseSpots.resize(3); //query, parentA, parentB
473 vector<int> gaps; gaps.resize(seqs[0]->getAligned().length(), 0);
475 string filterString = (string(seqs[0]->getAligned().length(), '1'));
478 for (int i = 0; i < seqs.size(); i++) {
480 string seqAligned = seqs[i]->getAligned();
482 for (int j = 0; j < seqAligned.length(); j++) {
483 //if this spot is a gap
484 if ((seqAligned[j] == '-') || (seqAligned[j] == '.') || (toupper(seqAligned[j]) == 'N')) { gaps[j]++; }
488 //zero out spot where any sequences have blanks
489 int numColRemoved = 0;
491 map<int, int> maskMap; maskMap.clear();
493 for(int i = 0; i < seqs[0]->getAligned().length(); i++){
494 if(gaps[i] != 0) { filterString[i] = '0'; numColRemoved++; }
502 for (int i = 0; i < seqs.size(); i++) {
504 string seqAligned = seqs[i]->getAligned();
505 string newAligned = "";
509 for (int j = 0; j < seqAligned.length(); j++) {
511 if ((seqAligned[j] != '-') && (seqAligned[j] != '.') && (toupper(seqAligned[j]) != 'N')) { baseCount++; }
513 //if this spot is not a gap
514 if (filterString[j] == '1') {
515 newAligned += seqAligned[j];
516 baseSpots[i][count] = baseCount;
521 seqs[i]->setAligned(newAligned);
526 catch(exception& e) {
527 m->errorOut(e, "Slayer", "verticalFilter");
531 /***********************************************************************/