]> git.donarmstrong.com Git - mothur.git/blob - slayer.cpp
working on chimeraslayer and found bug in shared command.
[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) :
14                 windowSize(win), windowStep(increment), parentFragmentThreshold(parentThreshold), divRThreshold(div) {}
15 /***********************************************************************/
16 string Slayer::getResults(Sequence* query, vector<Sequence*> refSeqs) {
17         try {
18 cout << "refSeqs = " << refSeqs.size() << endl;         
19                 vector<data_struct> all; all.clear();
20                 
21                 for (int i = 0; i < refSeqs.size(); i++) {
22                 
23                         for (int j = i+1; j < refSeqs.size(); j++) {
24                         
25                                 //make copies of query and each parent because runBellerophon removes gaps and messes them up
26                                 Sequence* q = new Sequence(query->getName(), query->getAligned());
27                                 Sequence* leftParent = new Sequence(refSeqs[i]->getName(), refSeqs[i]->getAligned());
28                                 Sequence* rightParent = new Sequence(refSeqs[j]->getName(), refSeqs[j]->getAligned());
29                                 
30                                 vector<data_struct> divs = runBellerophon(q, leftParent, rightParent);
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                                                 int winSizeLeft = divs[k].winLEnd - divs[k].winLStart + 1;
45                                                 int winSizeRight = divs[k].winREnd - divs[k].winRStart + 1;
46                                                 
47                                                 float snpRateLeft = numSNPSLeft / (float) winSizeLeft;
48                                                 float snpRateRight = numSNPSRight / (float) winSizeRight;
49                                                 
50                                                 float logR = log(snpRateLeft / snpRateRight) / log(2);
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                                                         selectedDivs.push_back(divs[k]);
66                                                 }
67                                         }
68                                 }
69                                 
70                                 //save selected
71                                 for (int m = 0; m < selectedDivs.size(); m++) {  all.push_back(selectedDivs[m]);        }
72                                 
73                                 delete q;
74                                 delete leftParent;
75                                 delete rightParent;
76                         }
77                 }
78                 
79                 
80                 // compute bootstrap support
81                 if (all.size() > 0) {
82                         //sort them
83                         sort(all.begin(), all.end(), compareDataStruct);
84                         reverse(all.begin(), all.end());
85                         
86                         outputResults = all;
87                         return "yes"; 
88                 }
89                 else {
90                         outputResults = all;
91                         return "no";
92                 }
93         }
94         catch(exception& e) {
95                 errorOut(e, "Slayer", "getResults");
96                 exit(1);
97         }
98 }
99 /***********************************************************************/
100 vector<data_struct> Slayer::runBellerophon(Sequence* q, Sequence* pA, Sequence* pB) {
101         try{
102                 
103                 vector<data_struct> data;
104 cout << q->getName() << '\t' << q->getAligned().length() << endl;               
105                 //vertical filter
106                 vector<Sequence*> temp;
107                 temp.push_back(q); temp.push_back(pA); temp.push_back(pB);
108                 verticalFilter(temp);
109                 
110                 //get these to avoid numerous function calls
111                 string query = q->getAligned();
112                 string parentA = pA->getAligned();
113                 string parentB = pB->getAligned();
114                 int length = query.length();
115 cout << q->getName() << '\t' << length << endl;
116                 
117                 //check window size
118                 if (length < (2*windowSize+windowStep)) { 
119                         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."); mothurOutEndLine();      
120                         windowSize = length / 4;
121                 }
122                 
123                 for (int i = windowSize-1; i <= (length - windowSize); i += windowStep) {
124                 
125                         int breakpoint = i;
126                         int leftLength = breakpoint + 1;
127                         int rightLength = length - leftLength;
128                         
129                         float QLA = computePercentID(query, parentA, 0, breakpoint);
130                         float QRB = computePercentID(query, parentB, breakpoint+1, length - 1);
131                 
132                         float QLB = computePercentID(query, parentB, 0, breakpoint);
133                         float QRA = computePercentID(query, parentA, breakpoint+1, length - 1);
134                 
135                         float LAB = computePercentID(parentA, parentB, 0, breakpoint);
136                         float RAB = computePercentID(parentA, parentB, breakpoint+1, length - 1);
137                 
138                         float AB = ((LAB*leftLength) + (RAB*rightLength)) / (float) length;
139                         float QA = ((QLA*leftLength) + (QRA*rightLength)) / (float) length;
140                         float QB = ((QLB*leftLength) + (QRB*rightLength)) / (float) length;
141                 
142                         float QLA_QRB = ((QLA*leftLength) + (QRB*rightLength)) / (float) length;
143                         float QLB_QRA = ((QLB*leftLength) + (QRA*rightLength)) / (float) length;
144                 
145                         //in original and not used
146                         //float avgQA_QB = ((QA*leftLength) + (QB*rightLength)) / (float) length;
147                 
148                         float divR_QLA_QRB = min((QLA_QRB/QA), (QLA_QRB/QB));
149                 
150                         float divR_QLB_QRA = min((QLB_QRA/QA), (QLB_QRA/QB));
151
152                         //is one of them above the 
153                         if (divR_QLA_QRB >= divRThreshold || divR_QLB_QRA >= divRThreshold) {
154                                 
155                                 if (((QLA_QRB > QA) && (QLA_QRB > QB) && (QLA >= parentFragmentThreshold) && (QRB >= parentFragmentThreshold))  ||
156                                         ((QLB_QRA > QA) && (QLB_QRA > QB) && (QLB >=parentFragmentThreshold) && (QRA >= parentFragmentThreshold)))  {
157                                         
158                                         data_struct member;
159                                         
160                                         member.divr_qla_qrb = divR_QLA_QRB;
161                                         member.divr_qlb_qra = divR_QLB_QRA;
162                                         member.qla_qrb = QLA_QRB;
163                                         member.qlb_qra = QLB_QRA;
164                                         member.qla = QLA;
165                                         member.qrb = QRB;
166                                         member.ab = AB; 
167                                         member.qa = QA;
168                                         member.qb = QB; 
169                                         member.lab = LAB; 
170                                         member.rab = RAB; 
171                                         member.qra = QRA; 
172                                         member.qlb = QLB; 
173                                         member.winLStart = 0;
174                                         member.winLEnd = breakpoint; 
175                                         member.winRStart = breakpoint+1; 
176                                         member.winREnd = length-1; 
177                                         member.querySeq = *(q); 
178                                         member.parentA = *(pA);
179                                         member.parentB = *(pB);
180                                         member.bsa = 0;
181                                         member.bsb = 0;
182                                         member.bsMax = 0;
183                                         member.chimeraMax = 0;
184                                         
185                                         data.push_back(member);
186                                         
187                                 }//if
188                         }//if
189                 }//for
190                 
191                 return data;
192                 
193         }
194         catch(exception& e) {
195                 errorOut(e, "Slayer", "runBellerophon");
196                 exit(1);
197         }
198 }
199 /***********************************************************************/
200 vector<snps> Slayer::getSNPS(string parentA, string query, string parentB, int left, int right) {
201         try {
202         
203                 vector<snps> data;
204
205                 for (int i = left; i <= right; i++) {
206                         
207                         char A = parentA[i];
208                         char Q = query[i];
209                         char B = parentB[i];
210                         
211                         if ((A != Q) || (B != Q)) {
212                                 snps member;
213                                 member.queryChar = Q;
214                                 member.parentAChar = A;
215                                 member.parentBChar = B;
216                                 
217                                 data.push_back(member);
218                         }
219                 }
220                 
221                 return data;
222                 
223         }
224         catch(exception& e) {
225                 errorOut(e, "Slayer", "getSNPS");
226                 exit(1);
227         }
228 }
229 /***********************************************************************/
230 void Slayer::bootstrapSNPS(vector<snps> left, vector<snps> right, float& BSA, float& BSB) {
231         try {
232
233                 srand((unsigned)time( NULL ));
234
235                 int count_A = 0; // sceneario QLA,QRB supported
236                 int count_B = 0; // sceneario QLB,QRA supported
237         
238                 int numLeft = max(1, int(left.size()/10 +0.5));
239                 int numRight = max(1, int(right.size()/10 + 0.5));
240                 
241                 for (int i = 0; i < 100; i++) {
242                         //random sampling with replacement.
243                 
244                         vector<snps> selectedLeft;
245                         for (int j = 0; j < numLeft; j++) {
246                                 int index = int(rand() % left.size());
247                                 selectedLeft.push_back(left[index]);
248                         }
249                 
250                         vector<snps> selectedRight;
251                         for (int j = 0; j < numRight; j++) {
252                                 int index = int(rand() % right.size());
253                                 selectedRight.push_back(right[index]);
254                         }
255                 
256                         /* A  ------------------------------------------
257                         #       QLA                     QRA
258                         # Q  ------------------------------------------
259                         #                      |
260                         #                      |
261                         # Q  ------------------------------------------
262                         #       QLB                     QRB
263                         # B  ------------------------------------------ */
264                 
265                 
266                         float QLA = snpQA(selectedLeft);
267                         float QRA = snpQA(selectedRight);
268                 
269                         float QLB = snpQB(selectedLeft);
270                         float QRB = snpQB(selectedRight);
271                         
272                         //in original - not used - not sure why?
273                         //float ALB = snpAB(selectedLeft);
274                         //float ARB = snpAB(selectedRight);
275                 
276                         if ((QLA > QLB) && (QRB > QRA)) {
277                                 count_A++;
278                         }
279                 
280                         if ((QLB > QLA) && (QRA > QRB)) {
281                                 count_B++;
282                         }
283                 
284                 }
285
286                 BSA = (float) count_A;
287                 BSB = (float) count_B;
288         
289         }
290         catch(exception& e) {
291                 errorOut(e, "Slayer", "bootstrapSNPS");
292                 exit(1);
293         }
294 }
295 /***********************************************************************/
296 float Slayer::snpQA(vector<snps> data) {
297         try {
298         
299                 int numIdentical = 0;
300         
301                 for (int i = 0; i < data.size(); i++) {
302                         if (data[i].parentAChar == data[i].queryChar) {
303                                 numIdentical++;
304                         }
305                 }
306
307                 float percentID = (numIdentical / data.size()) * 100;
308                 
309                 return percentID;
310         }
311         catch(exception& e) {
312                 errorOut(e, "Slayer", "snpQA");
313                 exit(1);
314         }
315 }
316 /***********************************************************************/
317 float Slayer::snpQB(vector<snps> data) {
318         try {
319         
320                 int numIdentical = 0;
321         
322                 for (int i = 0; i < data.size(); i++) {
323                         if (data[i].parentBChar == data[i].queryChar) {
324                                 numIdentical++;
325                         }
326                 }
327
328                 float percentID = (numIdentical / data.size()) * 100;
329                 
330                 return percentID;
331
332         }
333         catch(exception& e) {
334                 errorOut(e, "Slayer", "snpQB");
335                 exit(1);
336         }
337 }
338 /***********************************************************************/
339 float Slayer::snpAB(vector<snps> data) {
340         try {
341                 int numIdentical = 0;
342         
343                 for (int i = 0; i < data.size(); i++) {
344                         if (data[i].parentAChar == data[i].parentBChar) {
345                                 numIdentical++;
346                         }
347                 }
348
349                 float percentID = (numIdentical / data.size()) * 100;
350                 
351                 return percentID;
352
353         }
354         catch(exception& e) {
355                 errorOut(e, "Slayer", "snpAB");
356                 exit(1);
357         }
358 }
359 /***********************************************************************/
360 float Slayer::computePercentID(string queryFrag, string parent, int left, int right) {
361         try {
362                 int total = 0;
363                 int matches = 0;
364         
365                 for (int i = left; i <= right; i++) {
366                         total++;
367                         if (queryFrag[i] == parent[i]) {
368                                 matches++;
369                         }
370                 }
371
372                 float percentID =( matches/(float)total) * 100;
373                 
374                 return percentID;
375         }
376         catch(exception& e) {
377                 errorOut(e, "Slayer", "computePercentID");
378                 exit(1);
379         }
380 }
381 /***********************************************************************/
382 //remove columns that contain any gaps
383 void Slayer::verticalFilter(vector<Sequence*> seqs) {
384         try {
385                 vector<int> gaps;       gaps.resize(seqs[0]->getAligned().length(), 0);
386                 
387                 string filterString = (string(seqs[0]->getAligned().length(), '1'));
388                 
389                 //for each sequence
390                 for (int i = 0; i < seqs.size(); i++) {
391                 
392                         string seqAligned = seqs[i]->getAligned();
393                         
394                         for (int j = 0; j < seqAligned.length(); j++) {
395                                 //if this spot is a gap
396                                 if ((seqAligned[j] == '-') || (seqAligned[j] == '.') || (toupper(seqAligned[j]) == 'N'))        {       gaps[j]++;      }
397                         }
398                 }
399                 
400                 //zero out spot where all sequences have blanks
401                 int numColRemoved = 0;
402
403                 for(int i = 0; i < seqs[0]->getAligned().length(); i++){
404                         if(gaps[i] != 0)        {       filterString[i] = '0';  numColRemoved++;  }
405                 }
406
407                 //for each sequence
408                 for (int i = 0; i < seqs.size(); i++) {
409                 
410                         string seqAligned = seqs[i]->getAligned();
411                         string newAligned = "";
412                         
413                         for (int j = 0; j < seqAligned.length(); j++) {
414                                 //if this spot is not a gap
415                                 if (filterString[j] == '1') { newAligned += seqAligned[j]; }
416                         }
417                         
418                         seqs[i]->setAligned(newAligned);
419                 }
420         }
421         catch(exception& e) {
422                 errorOut(e, "Slayer", "verticalFilter");
423                 exit(1);
424         }
425 }
426 /***********************************************************************/