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){}
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 //make copies of query and each parent because runBellerophon removes gaps and messes them up
25 Sequence* q = new Sequence(query->getName(), query->getAligned());
26 Sequence* leftParent = new Sequence(refSeqs[i]->getName(), refSeqs[i]->getAligned());
27 Sequence* rightParent = new Sequence(refSeqs[j]->getName(), refSeqs[j]->getAligned());
29 map<int, int> spots; //map from spot in original sequence to spot in filtered sequence for query and both parents
30 vector<data_struct> divs = runBellerophon(q, leftParent, rightParent, spots);
32 vector<data_struct> selectedDivs;
33 for (int k = 0; k < divs.size(); k++) {
35 vector<snps> snpsLeft = getSNPS(divs[k].parentA.getAligned(), divs[k].querySeq.getAligned(), divs[k].parentB.getAligned(), divs[k].winLStart, divs[k].winLEnd);
36 vector<snps> snpsRight = getSNPS(divs[k].parentA.getAligned(), divs[k].querySeq.getAligned(), divs[k].parentB.getAligned(), divs[k].winRStart, divs[k].winREnd);
38 int numSNPSLeft = snpsLeft.size();
39 int numSNPSRight = snpsRight.size();
41 //require at least 3 SNPs on each side of the break
42 if ((numSNPSLeft >= 3) && (numSNPSRight >= 3)) {
44 //removed in 12/09 version of chimeraSlayer
45 //int winSizeLeft = divs[k].winLEnd - divs[k].winLStart + 1;
46 //int winSizeRight = divs[k].winREnd - divs[k].winRStart + 1;
48 //float snpRateLeft = numSNPSLeft / (float) winSizeLeft;
49 //float snpRateRight = numSNPSRight / (float) winSizeRight;
50 //float logR = log(snpRateLeft / snpRateRight) / log(2.0);
52 // do not accept excess snp ratio on either side of the break
53 //if (abs(logR) < 1 ) {
56 bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B);
61 divs[k].bsMax = max(BS_A, BS_B);
63 divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra);
65 //so results reflect orignal alignment
66 divs[k].winLStart = spots[divs[k].winLStart];
67 divs[k].winLEnd = spots[divs[k].winLEnd];
68 divs[k].winRStart = spots[divs[k].winRStart];
69 divs[k].winREnd = spots[divs[k].winREnd];
71 selectedDivs.push_back(divs[k]);
77 for (int m = 0; m < selectedDivs.size(); m++) { all.push_back(selectedDivs[m]); }
86 // compute bootstrap support
89 sort(all.begin(), all.end(), compareDataStruct);
90 reverse(all.begin(), all.end());
100 catch(exception& e) {
101 errorOut(e, "Slayer", "getResults");
105 /***********************************************************************/
106 vector<data_struct> Slayer::runBellerophon(Sequence* q, Sequence* pA, Sequence* pB, map<int, int>& spots) {
109 vector<data_struct> data;
112 vector<Sequence*> temp;
113 temp.push_back(q); temp.push_back(pA); temp.push_back(pB);
115 //maps spot in new alignment to spot in alignment before filter
116 spots = verticalFilter(temp); //fills baseSpots
118 //get these to avoid numerous function calls
119 string query = q->getAligned();
120 string parentA = pA->getAligned();
121 string parentB = pB->getAligned();
122 int length = query.length();
123 //cout << q->getName() << endl << q->getAligned() << endl << endl;
124 //cout << pA->getName() << endl << pA->getAligned() << endl << endl;
125 //cout << pB->getName() << endl << pB->getAligned() << endl << endl;
126 //cout << " length = " << length << endl;
127 //cout << q->getName() << endl;
128 //cout << pA->getName() << '\t';
129 //cout << pB->getName() << endl;
132 if (length < (2*windowSize+windowStep)) {
133 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."); mothurOutEndLine();
134 windowSize = length / 4;
137 for (int i = windowSize-1; i <= (length - windowSize); i += windowStep) {
140 int leftLength = breakpoint + 1;
141 int rightLength = length - leftLength;
143 float QLA = computePercentID(query, parentA, 0, breakpoint);
144 float QRB = computePercentID(query, parentB, breakpoint+1, length - 1);
146 float QLB = computePercentID(query, parentB, 0, breakpoint);
147 float QRA = computePercentID(query, parentA, breakpoint+1, length - 1);
149 float LAB = computePercentID(parentA, parentB, 0, breakpoint);
150 float RAB = computePercentID(parentA, parentB, breakpoint+1, length - 1);
152 float AB = ((LAB*leftLength) + (RAB*rightLength)) / (float) length;
153 float QA = ((QLA*leftLength) + (QRA*rightLength)) / (float) length;
154 float QB = ((QLB*leftLength) + (QRB*rightLength)) / (float) length;
156 float QLA_QRB = ((QLA*leftLength) + (QRB*rightLength)) / (float) length;
157 float QLB_QRA = ((QLB*leftLength) + (QRA*rightLength)) / (float) length;
159 //in original and not used
160 //float avgQA_QB = ((QA*leftLength) + (QB*rightLength)) / (float) length;
162 float divR_QLA_QRB = min((QLA_QRB/QA), (QLA_QRB/QB));
163 float divR_QLB_QRA = min((QLB_QRA/QA), (QLB_QRA/QB));
164 //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;
166 //cout << divRThreshold << endl;
167 //cout << breakpoint << '\t' << divR_QLA_QRB << '\t' << divR_QLB_QRA << endl;
168 //is one of them above the
169 if (divR_QLA_QRB >= divRThreshold || divR_QLB_QRA >= divRThreshold) {
171 if (((QLA_QRB > QA) && (QLA_QRB > QB) && (QLA >= parentFragmentThreshold) && (QRB >= parentFragmentThreshold)) ||
172 ((QLB_QRA > QA) && (QLB_QRA > QB) && (QLB >=parentFragmentThreshold) && (QRA >= parentFragmentThreshold))) {
176 member.divr_qla_qrb = divR_QLA_QRB;
177 member.divr_qlb_qra = divR_QLB_QRA;
178 member.qla_qrb = QLA_QRB;
179 member.qlb_qra = QLB_QRA;
189 member.winLStart = 0;
190 member.winLEnd = breakpoint;
191 member.winRStart = breakpoint+1;
192 member.winREnd = length-1;
193 member.querySeq = *(q);
194 member.parentA = *(pA);
195 member.parentB = *(pB);
199 member.chimeraMax = 0;
201 data.push_back(member);
210 catch(exception& e) {
211 errorOut(e, "Slayer", "runBellerophon");
215 /***********************************************************************/
216 vector<snps> Slayer::getSNPS(string parentA, string query, string parentB, int left, int right) {
220 //cout << left << '\t' << right << endl;
221 for (int i = left; i <= right; i++) {
227 if ((A != Q) || (B != Q)) {
228 //cout << "not equal " << Q << '\t' << A << '\t' << B << endl;
230 //ensure not neighboring a gap. change to 12/09 release of chimeraSlayer - not sure what this adds, but it eliminates alot of SNPS
232 //did query loose a base here during filter??
233 ( i == 0 || abs (baseSpots[0][i] - baseSpots[0][i-1]) == 1) &&
234 ( i == query.length() || abs (baseSpots[0][i] - baseSpots[0][i+1]) == 1)
236 //did parentA loose a base here during filter??
237 ( i == 0 || abs (baseSpots[1][i] - baseSpots[1][i-1]) == 1) &&
238 ( i == parentA.length() || abs (baseSpots[1][i] - baseSpots[1][i+1]) == 1)
240 //did parentB loose a base here during filter??
241 ( i == 0 || abs (baseSpots[2][i] - baseSpots[2][i-1]) == 1) &&
242 ( i == parentB.length() || abs (baseSpots[2][i] - baseSpots[2][i+1]) == 1)
247 member.queryChar = Q;
248 member.parentAChar = A;
249 member.parentBChar = B;
250 //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;
251 data.push_back(member);
259 catch(exception& e) {
260 errorOut(e, "Slayer", "getSNPS");
264 /***********************************************************************/
265 void Slayer::bootstrapSNPS(vector<snps> left, vector<snps> right, float& BSA, float& BSB) {
268 srand((unsigned)time( NULL ));
270 int count_A = 0; // sceneario QLA,QRB supported
271 int count_B = 0; // sceneario QLB,QRA supported
273 int numLeft = max(1, int(left.size() * (percentSNPSample/(float)100) + 0.5));
274 int numRight = max(1, int(right.size() * (percentSNPSample/(float)100) + 0.5));
276 for (int i = 0; i < iters; i++) {
277 //random sampling with replacement.
279 vector<snps> selectedLeft;
281 for (int j = 0; j < numLeft; j++) {
282 int index = int(rand() % left.size());
283 selectedLeft.push_back(left[index]);
286 vector<snps> selectedRight;
287 for (int j = 0; j < numRight; j++) {
288 int index = int(rand() % right.size());
289 selectedRight.push_back(right[index]);
292 /* A ------------------------------------------
294 # Q ------------------------------------------
297 # Q ------------------------------------------
299 # B ------------------------------------------ */
302 float QLA = snpQA(selectedLeft);
303 float QRA = snpQA(selectedRight);
305 float QLB = snpQB(selectedLeft);
306 float QRB = snpQB(selectedRight);
308 //in original - not used - not sure why?
309 //float ALB = snpAB(selectedLeft);
310 //float ARB = snpAB(selectedRight);
312 if ((QLA > QLB) && (QRB > QRA)) {
316 if ((QLB > QLA) && (QRA > QRB)) {
320 //cout << "selected left snp: \n";
321 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentAChar; }
323 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].queryChar; }
325 //for (int j = 0; j < selectedLeft.size(); j++) { cout << selectedLeft[j].parentBChar; }
327 //cout << "selected right snp: \n";
328 //for (int j = 0; j < selectedRight.size(); j++) { cout << selectedRight[j].parentAChar; }
330 //for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].queryChar; }
332 //for (int i = 0; i < selectedRight.size(); i++) { cout << selectedRight[i].parentBChar; }
339 BSA = ((float) count_A / (float) iters) * 100;
340 BSB = ((float) count_B / (float) iters) * 100;
341 //cout << "bsa = " << BSA << " bsb = " << BSB << endl;
344 catch(exception& e) {
345 errorOut(e, "Slayer", "bootstrapSNPS");
349 /***********************************************************************/
350 float Slayer::snpQA(vector<snps> data) {
353 int numIdentical = 0;
355 for (int i = 0; i < data.size(); i++) {
356 if (data[i].parentAChar == data[i].queryChar) {
361 float percentID = (numIdentical / (float) data.size()) * 100;
365 catch(exception& e) {
366 errorOut(e, "Slayer", "snpQA");
370 /***********************************************************************/
371 float Slayer::snpQB(vector<snps> data) {
374 int numIdentical = 0;
376 for (int i = 0; i < data.size(); i++) {
377 if (data[i].parentBChar == data[i].queryChar) {
382 float percentID = (numIdentical / (float) data.size()) * 100;
387 catch(exception& e) {
388 errorOut(e, "Slayer", "snpQB");
392 /***********************************************************************/
393 float Slayer::snpAB(vector<snps> data) {
395 int numIdentical = 0;
397 for (int i = 0; i < data.size(); i++) {
398 if (data[i].parentAChar == data[i].parentBChar) {
403 float percentID = (numIdentical / (float) data.size()) * 100;
408 catch(exception& e) {
409 errorOut(e, "Slayer", "snpAB");
413 /***********************************************************************/
414 float Slayer::computePercentID(string queryFrag, string parent, int left, int right) {
419 for (int i = left; i <= right; i++) {
421 if (queryFrag[i] == parent[i]) {
426 float percentID =( matches/(float)total) * 100;
430 catch(exception& e) {
431 errorOut(e, "Slayer", "computePercentID");
435 /***********************************************************************/
436 //remove columns that contain any gaps
437 map<int, int> Slayer::verticalFilter(vector<Sequence*> seqs) {
441 baseSpots.resize(3); //query, parentA, parentB
443 vector<int> gaps; gaps.resize(seqs[0]->getAligned().length(), 0);
445 string filterString = (string(seqs[0]->getAligned().length(), '1'));
448 for (int i = 0; i < seqs.size(); i++) {
450 string seqAligned = seqs[i]->getAligned();
452 for (int j = 0; j < seqAligned.length(); j++) {
453 //if this spot is a gap
454 if ((seqAligned[j] == '-') || (seqAligned[j] == '.') || (toupper(seqAligned[j]) == 'N')) { gaps[j]++; }
458 //zero out spot where any sequences have blanks
459 int numColRemoved = 0;
461 map<int, int> maskMap; maskMap.clear();
463 for(int i = 0; i < seqs[0]->getAligned().length(); i++){
464 if(gaps[i] != 0) { filterString[i] = '0'; numColRemoved++; }
472 for (int i = 0; i < seqs.size(); i++) {
474 string seqAligned = seqs[i]->getAligned();
475 string newAligned = "";
479 for (int j = 0; j < seqAligned.length(); j++) {
481 if ((seqAligned[j] != '-') && (seqAligned[j] != '.') && (toupper(seqAligned[j]) != 'N')) { baseCount++; }
483 //if this spot is not a gap
484 if (filterString[j] == '1') {
485 newAligned += seqAligned[j];
486 baseSpots[i][count] = baseCount;
491 seqs[i]->setAligned(newAligned);
496 catch(exception& e) {
497 errorOut(e, "Slayer", "verticalFilter");
501 /***********************************************************************/