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