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