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