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