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) :
14 windowSize(win), windowStep(increment), parentFragmentThreshold(parentThreshold), divRThreshold(div) {}
15 /***********************************************************************/
16 string Slayer::getResults(Sequence* query, vector<Sequence*> refSeqs) {
18 cout << "refSeqs = " << refSeqs.size() << endl;
19 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 //make copies of query and each parent because runBellerophon removes gaps and messes them up
26 Sequence* q = new Sequence(query->getName(), query->getAligned());
27 Sequence* leftParent = new Sequence(refSeqs[i]->getName(), refSeqs[i]->getAligned());
28 Sequence* rightParent = new Sequence(refSeqs[j]->getName(), refSeqs[j]->getAligned());
30 vector<data_struct> divs = runBellerophon(q, leftParent, rightParent);
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 int winSizeLeft = divs[k].winLEnd - divs[k].winLStart + 1;
45 int winSizeRight = divs[k].winREnd - divs[k].winRStart + 1;
47 float snpRateLeft = numSNPSLeft / (float) winSizeLeft;
48 float snpRateRight = numSNPSRight / (float) winSizeRight;
50 float logR = log(snpRateLeft / snpRateRight) / log(2);
52 // do not accept excess snp ratio on either side of the break
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 selectedDivs.push_back(divs[k]);
71 for (int m = 0; m < selectedDivs.size(); m++) { all.push_back(selectedDivs[m]); }
80 // compute bootstrap support
83 sort(all.begin(), all.end(), compareDataStruct);
84 reverse(all.begin(), all.end());
95 errorOut(e, "Slayer", "getResults");
99 /***********************************************************************/
100 vector<data_struct> Slayer::runBellerophon(Sequence* q, Sequence* pA, Sequence* pB) {
103 vector<data_struct> data;
104 cout << q->getName() << '\t' << q->getAligned().length() << endl;
106 vector<Sequence*> temp;
107 temp.push_back(q); temp.push_back(pA); temp.push_back(pB);
108 verticalFilter(temp);
110 //get these to avoid numerous function calls
111 string query = q->getAligned();
112 string parentA = pA->getAligned();
113 string parentB = pB->getAligned();
114 int length = query.length();
115 cout << q->getName() << '\t' << length << endl;
118 if (length < (2*windowSize+windowStep)) {
119 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();
120 windowSize = length / 4;
123 for (int i = windowSize-1; i <= (length - windowSize); i += windowStep) {
126 int leftLength = breakpoint + 1;
127 int rightLength = length - leftLength;
129 float QLA = computePercentID(query, parentA, 0, breakpoint);
130 float QRB = computePercentID(query, parentB, breakpoint+1, length - 1);
132 float QLB = computePercentID(query, parentB, 0, breakpoint);
133 float QRA = computePercentID(query, parentA, breakpoint+1, length - 1);
135 float LAB = computePercentID(parentA, parentB, 0, breakpoint);
136 float RAB = computePercentID(parentA, parentB, breakpoint+1, length - 1);
138 float AB = ((LAB*leftLength) + (RAB*rightLength)) / (float) length;
139 float QA = ((QLA*leftLength) + (QRA*rightLength)) / (float) length;
140 float QB = ((QLB*leftLength) + (QRB*rightLength)) / (float) length;
142 float QLA_QRB = ((QLA*leftLength) + (QRB*rightLength)) / (float) length;
143 float QLB_QRA = ((QLB*leftLength) + (QRA*rightLength)) / (float) length;
145 //in original and not used
146 //float avgQA_QB = ((QA*leftLength) + (QB*rightLength)) / (float) length;
148 float divR_QLA_QRB = min((QLA_QRB/QA), (QLA_QRB/QB));
150 float divR_QLB_QRA = min((QLB_QRA/QA), (QLB_QRA/QB));
152 //is one of them above the
153 if (divR_QLA_QRB >= divRThreshold || divR_QLB_QRA >= divRThreshold) {
155 if (((QLA_QRB > QA) && (QLA_QRB > QB) && (QLA >= parentFragmentThreshold) && (QRB >= parentFragmentThreshold)) ||
156 ((QLB_QRA > QA) && (QLB_QRA > QB) && (QLB >=parentFragmentThreshold) && (QRA >= parentFragmentThreshold))) {
160 member.divr_qla_qrb = divR_QLA_QRB;
161 member.divr_qlb_qra = divR_QLB_QRA;
162 member.qla_qrb = QLA_QRB;
163 member.qlb_qra = QLB_QRA;
173 member.winLStart = 0;
174 member.winLEnd = breakpoint;
175 member.winRStart = breakpoint+1;
176 member.winREnd = length-1;
177 member.querySeq = *(q);
178 member.parentA = *(pA);
179 member.parentB = *(pB);
183 member.chimeraMax = 0;
185 data.push_back(member);
194 catch(exception& e) {
195 errorOut(e, "Slayer", "runBellerophon");
199 /***********************************************************************/
200 vector<snps> Slayer::getSNPS(string parentA, string query, string parentB, int left, int right) {
205 for (int i = left; i <= right; i++) {
211 if ((A != Q) || (B != Q)) {
213 member.queryChar = Q;
214 member.parentAChar = A;
215 member.parentBChar = B;
217 data.push_back(member);
224 catch(exception& e) {
225 errorOut(e, "Slayer", "getSNPS");
229 /***********************************************************************/
230 void Slayer::bootstrapSNPS(vector<snps> left, vector<snps> right, float& BSA, float& BSB) {
233 srand((unsigned)time( NULL ));
235 int count_A = 0; // sceneario QLA,QRB supported
236 int count_B = 0; // sceneario QLB,QRA supported
238 int numLeft = max(1, int(left.size()/10 +0.5));
239 int numRight = max(1, int(right.size()/10 + 0.5));
241 for (int i = 0; i < 100; i++) {
242 //random sampling with replacement.
244 vector<snps> selectedLeft;
245 for (int j = 0; j < numLeft; j++) {
246 int index = int(rand() % left.size());
247 selectedLeft.push_back(left[index]);
250 vector<snps> selectedRight;
251 for (int j = 0; j < numRight; j++) {
252 int index = int(rand() % right.size());
253 selectedRight.push_back(right[index]);
256 /* A ------------------------------------------
258 # Q ------------------------------------------
261 # Q ------------------------------------------
263 # B ------------------------------------------ */
266 float QLA = snpQA(selectedLeft);
267 float QRA = snpQA(selectedRight);
269 float QLB = snpQB(selectedLeft);
270 float QRB = snpQB(selectedRight);
272 //in original - not used - not sure why?
273 //float ALB = snpAB(selectedLeft);
274 //float ARB = snpAB(selectedRight);
276 if ((QLA > QLB) && (QRB > QRA)) {
280 if ((QLB > QLA) && (QRA > QRB)) {
286 BSA = (float) count_A;
287 BSB = (float) count_B;
290 catch(exception& e) {
291 errorOut(e, "Slayer", "bootstrapSNPS");
295 /***********************************************************************/
296 float Slayer::snpQA(vector<snps> data) {
299 int numIdentical = 0;
301 for (int i = 0; i < data.size(); i++) {
302 if (data[i].parentAChar == data[i].queryChar) {
307 float percentID = (numIdentical / data.size()) * 100;
311 catch(exception& e) {
312 errorOut(e, "Slayer", "snpQA");
316 /***********************************************************************/
317 float Slayer::snpQB(vector<snps> data) {
320 int numIdentical = 0;
322 for (int i = 0; i < data.size(); i++) {
323 if (data[i].parentBChar == data[i].queryChar) {
328 float percentID = (numIdentical / data.size()) * 100;
333 catch(exception& e) {
334 errorOut(e, "Slayer", "snpQB");
338 /***********************************************************************/
339 float Slayer::snpAB(vector<snps> data) {
341 int numIdentical = 0;
343 for (int i = 0; i < data.size(); i++) {
344 if (data[i].parentAChar == data[i].parentBChar) {
349 float percentID = (numIdentical / data.size()) * 100;
354 catch(exception& e) {
355 errorOut(e, "Slayer", "snpAB");
359 /***********************************************************************/
360 float Slayer::computePercentID(string queryFrag, string parent, int left, int right) {
365 for (int i = left; i <= right; i++) {
367 if (queryFrag[i] == parent[i]) {
372 float percentID =( matches/(float)total) * 100;
376 catch(exception& e) {
377 errorOut(e, "Slayer", "computePercentID");
381 /***********************************************************************/
382 //remove columns that contain any gaps
383 void Slayer::verticalFilter(vector<Sequence*> seqs) {
385 vector<int> gaps; gaps.resize(seqs[0]->getAligned().length(), 0);
387 string filterString = (string(seqs[0]->getAligned().length(), '1'));
390 for (int i = 0; i < seqs.size(); i++) {
392 string seqAligned = seqs[i]->getAligned();
394 for (int j = 0; j < seqAligned.length(); j++) {
395 //if this spot is a gap
396 if ((seqAligned[j] == '-') || (seqAligned[j] == '.') || (toupper(seqAligned[j]) == 'N')) { gaps[j]++; }
400 //zero out spot where all sequences have blanks
401 int numColRemoved = 0;
403 for(int i = 0; i < seqs[0]->getAligned().length(); i++){
404 if(gaps[i] != 0) { filterString[i] = '0'; numColRemoved++; }
408 for (int i = 0; i < seqs.size(); i++) {
410 string seqAligned = seqs[i]->getAligned();
411 string newAligned = "";
413 for (int j = 0; j < seqAligned.length(); j++) {
414 //if this spot is not a gap
415 if (filterString[j] == '1') { newAligned += seqAligned[j]; }
418 seqs[i]->setAligned(newAligned);
421 catch(exception& e) {
422 errorOut(e, "Slayer", "verticalFilter");
426 /***********************************************************************/