]> git.donarmstrong.com Git - mothur.git/blob - slayer.cpp
chimera.slayer debugging
[mothur.git] / slayer.cpp
1 /*
2  *  slayer.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 9/25/09.
6  *  Copyright 2009 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "slayer.h"
11
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) {
17         try {
18                 vector<data_struct> all; all.clear();
19                 myQuery = *query;
20                 
21                                 
22                 for (int i = 0; i < refSeqs.size(); i++) {
23                 
24                         for (int j = i+1; j < refSeqs.size(); j++) {
25                         
26                                 if (m->control_pressed) { return "no";  }
27         
28                                 //make copies of query and each parent because runBellerophon removes gaps and messes them up
29                                 Sequence* q = new Sequence(query->getName(), query->getAligned());
30                                 Sequence* leftParent = new Sequence(refSeqs[i]->getName(), refSeqs[i]->getAligned());
31                                 Sequence* rightParent = new Sequence(refSeqs[j]->getName(), refSeqs[j]->getAligned());
32         
33                                 map<int, int> spots;  //map from spot in original sequence to spot in filtered sequence for query and both parents
34                                 vector<data_struct> divs = runBellerophon(q, leftParent, rightParent, spots);
35         
36                                 if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
37                                         
38                                 vector<data_struct> selectedDivs;
39                                 for (int k = 0; k < divs.size(); k++) {
40                                         
41                                         vector<snps> snpsLeft = getSNPS(divs[k].parentA.getAligned(), divs[k].querySeq.getAligned(), divs[k].parentB.getAligned(), divs[k].winLStart, divs[k].winLEnd);
42                                         vector<snps> snpsRight = getSNPS(divs[k].parentA.getAligned(), divs[k].querySeq.getAligned(), divs[k].parentB.getAligned(), divs[k].winRStart, divs[k].winREnd);
43         
44                                         if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
45                                         
46                                         int numSNPSLeft = snpsLeft.size();
47                                         int numSNPSRight = snpsRight.size();
48                                         
49                                         //require at least 4 SNPs on each side of the break
50                                         if ((numSNPSLeft >= 4) && (numSNPSRight >= 4)) {
51                                                         
52                                                 float BS_A, BS_B;
53                                                 bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B, iters);
54                                                 
55                                                 if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
56
57                                                 divs[k].bsa = BS_A;
58                                                 divs[k].bsb = BS_B;
59                                                 divs[k].bsMax = max(BS_A, BS_B);
60                                                 divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra);
61                                                 
62                                                 
63                                                 //are we within 10 points of the bootstrap cutoff?
64                                                 if ((divs[k].bsMax >= (minBS-10)) && (iters < 1000)) {
65                                                         bootstrapSNPS(snpsLeft, snpsRight, BS_A, BS_B, 1000);
66                                                                 
67                                                         if (m->control_pressed) { delete q; delete leftParent; delete rightParent; return "no"; }
68                                                                 
69                                                         divs[k].bsa = BS_A;
70                                                         divs[k].bsb = BS_B;
71                                                         divs[k].bsMax = max(BS_A, BS_B);
72                                                         divs[k].chimeraMax = max(divs[k].qla_qrb, divs[k].qlb_qra);
73                                                 }
74                                                 
75                                                 //so results reflect orignal alignment
76                                                 divs[k].winLStart = spots[divs[k].winLStart];
77                                                 divs[k].winLEnd = spots[divs[k].winLEnd];  
78                                                 divs[k].winRStart = spots[divs[k].winRStart]; 
79                                                 divs[k].winREnd = spots[divs[k].winREnd]; 
80                                                 
81                                                 selectedDivs.push_back(divs[k]);
82                                         }
83                                 }
84
85                                 //save selected
86                                 for (int mi = 0; mi < selectedDivs.size(); mi++) {  all.push_back(selectedDivs[mi]);    }
87                                 
88                                 delete q;
89                                 delete leftParent;
90                                 delete rightParent;
91                         }
92                 }
93                 
94
95                 // compute bootstrap support
96                 if (all.size() > 0) {
97                         //sort them
98                         sort(all.begin(), all.end(), compareDataStruct);
99                         reverse(all.begin(), all.end());
100                                                 
101                         outputResults = all;
102                         return "yes"; 
103                 }else {
104                         outputResults = all;
105                         return "no";
106                 }
107         }
108         catch(exception& e) {
109                 m->errorOut(e, "Slayer", "getResults");
110                 exit(1);
111         }
112 }
113 /***********************************************************************/
114 vector<data_struct> Slayer::runBellerophon(Sequence* q, Sequence* pA, Sequence* pB, map<int, int>& spots) {
115         try{
116                 
117                 vector<data_struct> data;
118                 
119                 //vertical filter
120                 vector<Sequence*> temp;
121                 temp.push_back(q); temp.push_back(pA); temp.push_back(pB);
122                 
123                 //maps spot in new alignment to spot in alignment before filter
124                 spots = verticalFilter(temp);  //fills baseSpots
125                 
126                 //get these to avoid numerous function calls
127                 string query = q->getAligned();
128                 string parentA = pA->getAligned();
129                 string parentB = pB->getAligned();
130                 int length = query.length();
131 //cout << q->getName() << endl << q->getAligned() << endl << endl;      
132 //cout << pA->getName() << endl << pA->getUnaligned() << endl << endl;          
133 //cout << pB->getName() << endl << pB->getUnaligned() << endl << endl;  
134 //cout << " length = " << length << endl;
135         
136                 //check window size
137                 if (length < (2*windowSize+windowStep)) { 
138 //                      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();        
139                         windowSize = length / 4;
140                 }
141                 
142                 for (int i = windowSize-1; i <= (length - windowSize); i += windowStep) {
143                 
144                         if (m->control_pressed) { return data; }
145                 
146                         int breakpoint = i;
147                         int leftLength = breakpoint + 1;
148                         int rightLength = length - leftLength;
149                                 
150                         float QLA = computePercentID(query, parentA, 0, breakpoint);
151                         float QRB = computePercentID(query, parentB, breakpoint+1, length);
152                 
153                         float QLB = computePercentID(query, parentB, 0, breakpoint);
154                         float QRA = computePercentID(query, parentA, breakpoint+1, length);
155                 
156                         float LAB = computePercentID(parentA, parentB, 0, breakpoint);
157                         float RAB = computePercentID(parentA, parentB, breakpoint+1, length);   
158                         
159                         float AB = ((LAB*leftLength) + (RAB*rightLength)) / (float) length;
160                         float QA = ((QLA*leftLength) + (QRA*rightLength)) / (float) length;
161                         float QB = ((QLB*leftLength) + (QRB*rightLength)) / (float) length;
162                 
163                         float QLA_QRB = ((QLA*leftLength) + (QRB*rightLength)) / (float) length;
164                         float QLB_QRA = ((QLB*leftLength) + (QRA*rightLength)) / (float) length;
165                 
166                         //in original and not used
167                         //float avgQA_QB = ((QA*leftLength) + (QB*rightLength)) / (float) length;
168                 
169                         float divR_QLA_QRB = min((QLA_QRB/QA), (QLA_QRB/QB));
170                         float divR_QLB_QRA = min((QLB_QRA/QA), (QLB_QRA/QB));
171                         
172                         
173                         //cout << q->getName() << '\t';
174                         //cout << pA->getName() << '\t';
175                         //cout << pB->getName() << '\t';
176                    // cout << "bp: " << breakpoint << " CHIM_TYPE_A\t" << divR_QLA_QRB << "\tQLA: " << QLA << "\tQRB: " << QRB << "\tQLA_QRB: " << QLA_QRB;
177                         //cout << "\tCHIM_TYPE_B\t" << divR_QLB_QRA << "\tQLB: " << QLB << "\tQRA: " << QRA << "\tQLB_QRA: " << QLB_QRA << endl;
178 //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;             
179
180 //cout << divRThreshold << endl;
181 //cout << breakpoint << '\t' << divR_QLA_QRB << '\t' << divR_QLB_QRA << endl;
182                         //is one of them above the 
183                         if (divR_QLA_QRB >= divRThreshold || divR_QLB_QRA >= divRThreshold) {
184                                 
185                                 if (((QLA_QRB > QA) && (QLA_QRB > QB) && (QLA >= parentFragmentThreshold) && (QRB >= parentFragmentThreshold))  ||
186                                         ((QLB_QRA > QA) && (QLB_QRA > QB) && (QLB >=parentFragmentThreshold) && (QRA >= parentFragmentThreshold)))  {
187                                         
188                                         data_struct member;
189                                         
190                                         member.divr_qla_qrb = divR_QLA_QRB;
191                                         member.divr_qlb_qra = divR_QLB_QRA;
192                                         member.qla_qrb = QLA_QRB;
193                                         member.qlb_qra = QLB_QRA;
194                                         member.qla = QLA;
195                                         member.qrb = QRB;
196                                         member.ab = AB; 
197                                         member.qa = QA;
198                                         member.qb = QB; 
199                                         member.lab = LAB; 
200                                         member.rab = RAB; 
201                                         member.qra = QRA; 
202                                         member.qlb = QLB; 
203                                         member.winLStart = 0;
204                                         member.winLEnd = breakpoint;  
205                                         member.winRStart = breakpoint+1; 
206                                         member.winREnd = length-1; 
207                                         member.querySeq = *(q);
208                                         member.parentA = *(pA);
209                                         member.parentB = *(pB);
210                                         member.bsa = 0;
211                                         member.bsb = 0;
212                                         member.bsMax = 0;
213                                         member.chimeraMax = 0;
214                                         
215                                         data.push_back(member);
216                                         
217                                 }//if
218                         }//if
219                 }//for
220                 
221                 
222                 return data;
223                 
224         }
225         catch(exception& e) {
226                 m->errorOut(e, "Slayer", "runBellerophon");
227                 exit(1);
228         }
229 }
230 /***********************************************************************/
231 vector<snps> Slayer::getSNPS(string parentA, string query, string parentB, int left, int right) {
232         try {
233         
234                 vector<snps> data;
235
236                 for (int i = left; i <= right; i++) {
237                         
238                         char A = parentA[i];
239                         char Q = query[i];
240                         char B = parentB[i];
241                         
242                         if ((A != Q) || (B != Q)) {
243
244                                 //ensure not neighboring a gap. change to 12/09 release of chimeraSlayer - not sure what this adds, but it eliminates alot of SNPS
245
246                                 
247                                 if (
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()-1 || abs (baseSpots[0][i] - baseSpots[0][i+1]) == 1)
251                                         &&
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()-1 || abs (baseSpots[1][i] - baseSpots[1][i+1]) == 1) 
255                                         &&
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()-1 || abs (baseSpots[2][i] - baseSpots[2][i+1]) == 1)
259                                         ) 
260                                 { 
261                                         snps member;
262                                         member.queryChar = Q;
263                                         member.parentAChar = A;
264                                         member.parentBChar = B;
265                                         data.push_back(member);
266                                 }
267                         }
268                 }
269                 
270                 return data;
271                 
272         }
273         catch(exception& e) {
274                 m->errorOut(e, "Slayer", "getSNPS");
275                 exit(1);
276         }
277 }
278 /***********************************************************************/
279 int Slayer::bootstrapSNPS(vector<snps> left, vector<snps> right, float& BSA, float& BSB, int numIters) {
280         try {
281
282                 srand((unsigned)time( NULL ));
283
284                 int count_A = 0; // sceneario QLA,QRB supported
285                 int count_B = 0; // sceneario QLB,QRA supported
286         
287                 int numLeft = max(1, int(left.size() * percentSNPSample/(float)100 + 0.5));
288                 int numRight = max(1, int(right.size() * percentSNPSample/(float)100 + 0.5));
289
290                 for (int i = 0; i < numIters; i++) {
291                         //random sampling with replacement.
292                 
293                         if (m->control_pressed) { return 0;  }
294                         
295                         vector<snps> selectedLeft;
296
297                         for (int j = 0; j < numLeft; j++) {
298                                 int index = int(rand() % left.size());
299                                 selectedLeft.push_back(left[index]);
300                         }
301
302                         vector<snps> selectedRight;
303                         for (int j = 0; j < numRight; j++) {
304                                 int index = int(rand() % right.size());
305                                 selectedRight.push_back(right[index]);
306                         }
307                 
308                         /* A  ------------------------------------------
309                         #       QLA                     QRA
310                         # Q  ------------------------------------------
311                         #                      |
312                         #                      |
313                         # Q  ------------------------------------------
314                         #       QLB                     QRB
315                         # B  ------------------------------------------ */
316                 
317                 
318                         float QLA = snpQA(selectedLeft);
319                         float QRA = snpQA(selectedRight);
320                 
321                         float QLB = snpQB(selectedLeft);
322                         float QRB = snpQB(selectedRight);
323         
324                         //in original - not used - not sure why?
325                         //float ALB = snpAB(selectedLeft);
326                         //float ARB = snpAB(selectedRight);
327                 
328                         if ((QLA > QLB) && (QRB > QRA)) {
329                                 count_A++;
330                         }
331                 
332                         if ((QLB > QLA) && (QRA > QRB)) {
333                                 count_B++;
334                         }
335                         
336 //cout << "selected left snp: \n";
337 //for (int j = 0; j < selectedLeft.size(); j++) {  cout << selectedLeft[j].parentAChar;  } 
338 //cout << endl;
339 //for (int j = 0; j < selectedLeft.size(); j++) {  cout << selectedLeft[j].queryChar;  }
340 //cout << endl;
341 //for (int j = 0; j < selectedLeft.size(); j++) {  cout << selectedLeft[j].parentBChar;  }
342 //cout << endl;
343 //cout << "selected right snp: \n";
344 //for (int j = 0; j < selectedRight.size(); j++) {  cout << selectedRight[j].parentAChar;  } 
345 //cout << endl;
346 //for (int i = 0; i < selectedRight.size(); i++) {  cout << selectedRight[i].queryChar;  }
347 //cout << endl;
348 //for (int i = 0; i < selectedRight.size(); i++) {  cout << selectedRight[i].parentBChar;  }
349 //cout << endl;         
350                 }
351
352
353                 //cout << count_A << '\t' << count_B << endl;
354
355                 BSA = (float) count_A / (float) numIters * 100;
356                 BSB = (float) count_B / (float) numIters * 100;
357 //cout << "bsa = " << BSA << " bsb = " << BSB << endl;
358
359                 return 0;
360         
361         }
362         catch(exception& e) {
363                 m->errorOut(e, "Slayer", "bootstrapSNPS");
364                 exit(1);
365         }
366 }
367 /***********************************************************************/
368 float Slayer::snpQA(vector<snps> data) {
369         try {
370         
371                 int numIdentical = 0;
372         
373                 for (int i = 0; i < data.size(); i++) {
374                         if (data[i].parentAChar == data[i].queryChar) {
375                                 numIdentical++;
376                         }
377                 }
378
379                 float percentID = (numIdentical / (float) data.size()) * 100;
380                 
381                 return percentID;
382         }
383         catch(exception& e) {
384                 m->errorOut(e, "Slayer", "snpQA");
385                 exit(1);
386         }
387 }
388 /***********************************************************************/
389 float Slayer::snpQB(vector<snps> data) {
390         try {
391         
392                 int numIdentical = 0;
393         
394                 for (int i = 0; i < data.size(); i++) {
395                         if (data[i].parentBChar == data[i].queryChar) {
396                                 numIdentical++;
397                         }
398                 }
399
400                 float percentID = (numIdentical / (float) data.size()) * 100;
401                 
402                 return percentID;
403
404         }
405         catch(exception& e) {
406                 m->errorOut(e, "Slayer", "snpQB");
407                 exit(1);
408         }
409 }
410 /***********************************************************************/
411 float Slayer::snpAB(vector<snps> data) {
412         try {
413                 int numIdentical = 0;
414         
415                 for (int i = 0; i < data.size(); i++) {
416                         if (data[i].parentAChar == data[i].parentBChar) {
417                                 numIdentical++;
418                         }
419                 }
420
421                 float percentID = (numIdentical / (float) data.size()) * 100;
422                 
423                 return percentID;
424
425         }
426         catch(exception& e) {
427                 m->errorOut(e, "Slayer", "snpAB");
428                 exit(1);
429         }
430 }
431 /***********************************************************************/
432 float Slayer::computePercentID(string queryAlign, string chimera, int left, int right) {
433         try {
434                                 
435                 int numIdentical = 0;
436                 int countA = 0;
437                 int countB = 0;
438                 for (int i = left; i <= right; i++) {
439                         if (((queryAlign[i] != 'G') && (queryAlign[i] != 'T') && (queryAlign[i] != 'A') && (queryAlign[i] != 'C')&& (queryAlign[i] != '.') && (queryAlign[i] != '-')) ||
440                                 ((chimera[i] != 'G') && (chimera[i] != 'T') && (chimera[i] != 'A') && (chimera[i] != 'C')&& (chimera[i] != '.') && (chimera[i] != '-'))) {}
441                         else {
442                                 
443                                 bool charA = false; bool charB = false;
444                                 if ((queryAlign[i] == 'G') || (queryAlign[i] == 'T') || (queryAlign[i] == 'A') || (queryAlign[i] == 'C')) { charA = true; }
445                                 if ((chimera[i] == 'G') || (chimera[i] == 'T') || (chimera[i] == 'A') || (chimera[i] == 'C')) { charB = true; }
446                                 
447                                 if (charA || charB) {
448                                         
449                                         if (charA) { countA++; }
450                                         if (charB) { countB++; }
451                                         
452                                         if (queryAlign[i] == chimera[i]) {
453                                                 numIdentical++;
454                                         }
455                                 }
456                         }
457                         
458                 }
459                 
460                 float numBases = (countA + countB) /(float) 2;
461                 
462                 if (numBases == 0) { return 0; }
463                 
464                 float percentIdentical = (numIdentical/(float)numBases) * 100;
465
466                 return percentIdentical;
467                 
468         }
469         catch(exception& e) {
470                 m->errorOut(e, "Slayer", "computePercentID");
471                 exit(1);
472         }
473 }
474 /***********************************************************************/
475 //remove columns that contain any gaps
476 map<int, int> Slayer::verticalFilter(vector<Sequence*> seqs) {
477         try {
478                 //find baseSpots
479                 baseSpots.clear(); 
480                 baseSpots.resize(3);  //query, parentA, parentB
481         
482                 vector<int> gaps;       gaps.resize(seqs[0]->getAligned().length(), 0);
483                 
484                 string filterString = (string(seqs[0]->getAligned().length(), '1'));
485                 
486                 //for each sequence
487                 for (int i = 0; i < seqs.size(); i++) {
488                 
489                         string seqAligned = seqs[i]->getAligned();
490                         
491                         for (int j = 0; j < seqAligned.length(); j++) {
492                                 //if this spot is a gap
493                                 if ((seqAligned[j] == '-') || (seqAligned[j] == '.') || (toupper(seqAligned[j]) == 'N'))        {   gaps[j]++;  }
494                         }
495                 }
496                 
497                 //zero out spot where any sequences have blanks
498                 int numColRemoved = 0;
499                 int count = 0;
500                 map<int, int> maskMap; maskMap.clear();
501
502                 for(int i = 0; i < seqs[0]->getAligned().length(); i++){
503                         if(gaps[i] != 0)        {       filterString[i] = '0';  numColRemoved++;  }
504                         else {
505                                 maskMap[count] = i;
506                                 count++;
507                         }
508                 }
509
510                 //for each sequence
511                 for (int i = 0; i < seqs.size(); i++) {
512                 
513                         string seqAligned = seqs[i]->getAligned();
514                         string newAligned = "";
515                         
516                         int baseCount = 0;
517                         int count = 0;
518                         for (int j = 0; j < seqAligned.length(); j++) {
519                                 //are you a base
520                                 if ((seqAligned[j] != '-') && (seqAligned[j] != '.') && (toupper(seqAligned[j]) != 'N'))        { baseCount++; }
521                         
522                                 //if this spot is not a gap
523                                 if (filterString[j] == '1') { 
524                                         newAligned += seqAligned[j]; 
525                                         baseSpots[i][count] = baseCount;
526                                         count++;
527                                 }
528                         }
529                         
530                         seqs[i]->setAligned(newAligned);
531                 }
532                 
533                 return maskMap;
534         }
535         catch(exception& e) {
536                 m->errorOut(e, "Slayer", "verticalFilter");
537                 exit(1);
538         }
539 }
540 /***********************************************************************/