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