]> git.donarmstrong.com Git - mothur.git/blob - bellerophon.cpp
added sorted parameter to get.oturep, added error checking to chimera classes in...
[mothur.git] / bellerophon.cpp
1 /*
2  *  bellerophon.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 7/9/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "bellerophon.h"
11 #include "eachgapdist.h"
12 #include "ignoregaps.h"
13 #include "onegapdist.h"
14
15
16 //***************************************************************************************************************
17
18 Bellerophon::Bellerophon(string name) {
19         try {
20                 fastafile = name;
21         }
22         catch(exception& e) {
23                 errorOut(e, "Bellerophon", "Bellerophon");
24                 exit(1);
25         }
26 }
27
28 //***************************************************************************************************************
29 void Bellerophon::print(ostream& out) {
30         try {
31                 int above1 = 0;
32                 out << "Name\tScore\tLeft\tRight\t" << endl;
33                 //output prefenence structure to .chimeras file
34                 for (int i = 0; i < pref.size(); i++) {
35                         out << pref[i].name << '\t' << setprecision(3) << pref[i].score[0] << '\t' << pref[i].leftParent[0] << '\t' << pref[i].rightParent[0] << endl;
36                         
37                         //calc # of seqs with preference above 1.0
38                         if (pref[i].score[0] > 1.0) { 
39                                 above1++; 
40                                 mothurOut(pref[i].name + " is a suspected chimera at breakpoint " + toString(pref[i].midpoint)); mothurOutEndLine();
41                                 mothurOut("It's score is " + toString(pref[i].score[0]) + " with suspected left parent " + pref[i].leftParent[0] + " and right parent " + pref[i].rightParent[0]); mothurOutEndLine();
42                         }
43                 }
44                 
45                 //output results to screen
46                 mothurOutEndLine();
47                 mothurOut("Sequence with preference score above 1.0: " + toString(above1)); mothurOutEndLine();
48                 int spot;
49                 spot = pref.size()-1;
50                 mothurOut("Minimum:\t" + toString(pref[spot].score[0])); mothurOutEndLine();
51                 spot = pref.size() * 0.975;
52                 mothurOut("2.5%-tile:\t" + toString(pref[spot].score[0])); mothurOutEndLine();
53                 spot = pref.size() * 0.75;
54                 mothurOut("25%-tile:\t" + toString(pref[spot].score[0])); mothurOutEndLine();
55                 spot = pref.size() * 0.50;
56                 mothurOut("Median: \t" + toString(pref[spot].score[0])); mothurOutEndLine();
57                 spot = pref.size() * 0.25;
58                 mothurOut("75%-tile:\t" + toString(pref[spot].score[0])); mothurOutEndLine();
59                 spot = pref.size() * 0.025;
60                 mothurOut("97.5%-tile:\t" + toString(pref[spot].score[0])); mothurOutEndLine();
61                 spot = 0;
62                 mothurOut("Maximum:\t" + toString(pref[spot].score[0])); mothurOutEndLine();
63
64         }
65         catch(exception& e) {
66                 errorOut(e, "Bellerophon", "print");
67                 exit(1);
68         }
69 }
70
71 //********************************************************************************************************************
72 //sorts highest score to lowest
73 inline bool comparePref(Preference left, Preference right){
74         return (left.score[0] > right.score[0]);        
75 }
76
77 //***************************************************************************************************************
78 int Bellerophon::getChimeras() {
79         try {
80                 
81                 //do soft filter
82                 if (filter)  {
83                         string optionString = "fasta=" + fastafile + ", soft=50";
84                         filterSeqs = new FilterSeqsCommand(optionString);
85                         filterSeqs->execute();
86                         delete filterSeqs;
87                         
88                         //reset fastafile to filtered file
89                         fastafile = getRootName(fastafile) + "filter.fasta";
90                 }
91                 
92                 distCalculator = new eachGapDist();
93                 
94                 //read in sequences
95                 seqs = readSeqs(fastafile);
96                 
97                 if (unaligned) { mothurOut("Your sequences need to be aligned when you use the bellerophon method."); mothurOutEndLine(); return 1;  }
98                 
99                 int numSeqs = seqs.size();
100                 
101                 if (numSeqs == 0) { mothurOut("Error in reading you sequences."); mothurOutEndLine(); exit(1); }
102                 
103                 //set default window to 25% of sequence length
104                 string seq0 = seqs[0]->getAligned();
105                 if (window == 0) { window = seq0.length() / 4;  }
106                 else if (window > (seq0.length() / 2)) {  
107                         mothurOut("Your sequence length is = " + toString(seq0.length()) + ". You have selected a window size greater than the length of half your aligned sequence. I will run it with a window size of " + toString((seq0.length() / 2))); mothurOutEndLine();
108                         window = (seq0.length() / 2);
109                 }
110                 
111                 if (increment > (seqs[0]->getAlignLength() - (2*window))) { 
112                         if (increment != 10) {
113                         
114                                 mothurOut("You have selected a increment that is too large. I will use the default."); mothurOutEndLine();
115                                 increment = 10;
116                                 if (increment > (seqs[0]->getAlignLength() - (2*window))) {  increment = 0;  }
117                                 
118                         }else{ increment = 0; }
119                 }
120                 
121                 if (increment == 0) { iters = 1; }
122                 else { iters = ((seqs[0]->getAlignLength() - (2*window)) / increment); }
123                 
124                 //initialize pref
125                 pref.resize(numSeqs);  
126                 
127                 for (int i = 0; i < numSeqs; i++ ) { 
128                         pref[i].leftParent.resize(2); pref[i].rightParent.resize(2); pref[i].score.resize(2);   pref[i].closestLeft.resize(2); pref[i].closestRight.resize(3);
129                         pref[i].name = seqs[i]->getName();
130                         pref[i].score[0] = 0.0;  pref[i].score[1] = 0.0; 
131                         pref[i].closestLeft[0] = 100000.0;  pref[i].closestLeft[1] = 100000.0;  
132                         pref[i].closestRight[0] = 100000.0;  pref[i].closestRight[1] = 100000.0;  
133                 }
134
135                 int midpoint = window;
136                 int count = 0;
137                 while (count < iters) {
138                                 
139                                 //create 2 vectors of sequences, 1 for left side and one for right side
140                                 vector<Sequence> left;  vector<Sequence> right;
141                                 
142                                 for (int i = 0; i < seqs.size(); i++) {
143 //cout << "midpoint = " << midpoint << "\twindow = " << window << endl;
144 //cout << "whole = " << seqs[i]->getAligned().length() << endl;
145                                         //save left side
146                                         string seqLeft = seqs[i]->getAligned().substr(midpoint-window, window);
147                                         Sequence tempLeft;
148                                         tempLeft.setName(seqs[i]->getName());
149                                         tempLeft.setAligned(seqLeft);
150                                         left.push_back(tempLeft);
151 //cout << "left = " << tempLeft.getAligned().length() << endl;                  
152                                         //save right side
153                                         string seqRight = seqs[i]->getAligned().substr(midpoint, window);
154                                         Sequence tempRight;
155                                         tempRight.setName(seqs[i]->getName());
156                                         tempRight.setAligned(seqRight);
157                                         right.push_back(tempRight);
158 //cout << "right = " << seqRight.length() << endl;      
159                                 }
160                                 
161                                 //adjust midpoint by increment
162                                 midpoint += increment;
163                                 
164                                 
165                                 //this should be parallelized
166                                 //perference = sum of (| distance of my left to sequence j's left - distance of my right to sequence j's right | )
167                                 //create a matrix containing the distance from left to left and right to right
168                                 //calculate distances
169                                 SparseMatrix* SparseLeft = new SparseMatrix();
170                                 SparseMatrix* SparseRight = new SparseMatrix();
171                                 
172                                 createSparseMatrix(0, left.size(), SparseLeft, left);
173                                 createSparseMatrix(0, right.size(), SparseRight, right);
174                                 
175                                 vector<SeqMap> distMapRight;
176                                 vector<SeqMap> distMapLeft;
177                                 
178                                 // Create a data structure to quickly access the distance information.
179                                 //this is from thallingers reimplementation on get.oturep
180                                 // It consists of a vector of distance maps, where each map contains
181                                 // all distances of a certain sequence. Vector and maps are accessed
182                                 // via the index of a sequence in the distance matrix
183                                 distMapRight = vector<SeqMap>(numSeqs); 
184                                 distMapLeft = vector<SeqMap>(numSeqs); 
185                                 //cout << "left" << endl << endl;
186                                 for (MatData currentCell = SparseLeft->begin(); currentCell != SparseLeft->end(); currentCell++) {
187                                         distMapLeft[currentCell->row][currentCell->column] = currentCell->dist;
188                                         //cout << " i = " << currentCell->row << " j = " << currentCell->column << " dist = " << currentCell->dist << endl;
189                                 }
190                                 //cout << "right" << endl << endl;
191                                 for (MatData currentCell = SparseRight->begin(); currentCell != SparseRight->end(); currentCell++) {
192                                         distMapRight[currentCell->row][currentCell->column] = currentCell->dist;
193                                         //cout << " i = " << currentCell->row << " j = " << currentCell->column << " dist = " << currentCell->dist << endl;
194                                 }
195                                 
196                                 delete SparseLeft;
197                                 delete SparseRight;
198                                 
199                                 //fill preference structure
200                                 generatePreferences(distMapLeft, distMapRight, midpoint);
201                                 
202                                 count++;
203                                 
204                 }
205                 
206                 delete distCalculator;
207                 
208                 //rank preference score to eachother
209                 float dme = 0.0;
210                 float expectedPercent = 1 / (float) (pref.size());
211                 
212                 for (int i = 0; i < pref.size(); i++) {  dme += pref[i].score[0];  }
213         
214                 for (int i = 0; i < pref.size(); i++) {
215
216                         //gives the actual percentage of the dme this seq adds
217                         pref[i].score[0] = pref[i].score[0] / dme;
218                         
219                         //how much higher or lower is this than expected
220                         pref[i].score[0] = pref[i].score[0] / expectedPercent;
221                 
222                 }
223                 
224                 //sort Preferences highest to lowest
225                 sort(pref.begin(), pref.end(), comparePref);
226                 
227                 return 0;
228
229         }
230         catch(exception& e) {
231                 errorOut(e, "Bellerophon", "getChimeras");
232                 exit(1);
233         }
234 }
235
236 /***************************************************************************************************************/
237 int Bellerophon::createSparseMatrix(int startSeq, int endSeq, SparseMatrix* sparse, vector<Sequence> s){
238         try {
239
240                 for(int i=startSeq; i<endSeq; i++){
241                         
242                         for(int j=0;j<i;j++){
243                         
244                                 distCalculator->calcDist(s[i], s[j]);
245                                 float dist = distCalculator->getDist();
246                         
247                                 PCell temp(i, j, dist);
248                                 sparse->addCell(temp);
249                                 
250                         }
251                 }
252                 
253                 return 1;
254         }
255         catch(exception& e) {
256                 errorOut(e, "Bellerophon", "createSparseMatrix");
257                 exit(1);
258         }
259 }
260 /***************************************************************************************************************/
261 void Bellerophon::generatePreferences(vector<SeqMap> left, vector<SeqMap> right, int mid){
262         try {
263                 
264                 float dme = 0.0;
265                 SeqMap::iterator itR;
266                 SeqMap::iterator itL;
267                 
268                 //initialize pref[i]
269                 for (int i = 0; i < pref.size(); i++) {
270                         pref[i].score[1] = 0.0;
271                         pref[i].closestLeft[1] = 100000.0; 
272                         pref[i].closestRight[1] = 100000.0; 
273                         pref[i].leftParent[1] = "";
274                         pref[i].rightParent[1] = "";
275                 }
276         
277                 for (int i = 0; i < left.size(); i++) {
278                         
279                         SeqMap currentLeft = left[i];    //example i = 3;   currentLeft is a map of 0 to the distance of sequence 3 to sequence 0,
280                                                                                                 //                                                                              1 to the distance of sequence 3 to sequence 1,
281                                                                                                 //                                                                              2 to the distance of sequence 3 to sequence 2.
282                         SeqMap currentRight = right[i];         // same as left but with distances on the right side.
283                         
284                         for (int j = 0; j < i; j++) {
285                                 
286                                 itL = currentLeft.find(j);
287                                 itR = currentRight.find(j);
288 //cout << " i = " << i << " j = " << j << " distLeft = " << itL->second << endl;
289 //cout << " i = " << i << " j = " << j << " distright = " << itR->second << endl;
290                                 
291                                 //if you can find this entry update the preferences
292                                 if ((itL != currentLeft.end()) && (itR != currentRight.end())) {
293                                 
294                                         if (!correction) {
295                                                 pref[i].score[1] += abs((itL->second - itR->second));
296                                                 pref[j].score[1] += abs((itL->second - itR->second));
297 //cout << "left " << i << " " << j << " = " << itL->second << " right " << i << " " << j << " = " << itR->second << endl;
298 //cout << "abs = " << abs((itL->second - itR->second)) << endl;
299 //cout << i << " score = " << pref[i].score[1] << endl;
300 //cout << j << " score = " << pref[j].score[1] << endl;
301                                         }else {
302                                                 pref[i].score[1] += abs((sqrt(itL->second) - sqrt(itR->second)));
303                                                 pref[j].score[1] += abs((sqrt(itL->second) - sqrt(itR->second)));
304 //cout << "left " << i << " " << j << " = " << itL->second << " right " << i << " " << j << " = " << itR->second << endl;
305 //cout << "abs = " << abs((sqrt(itL->second) - sqrt(itR->second))) << endl;
306 //cout << i << " score = " << pref[i].score[1] << endl;
307 //cout << j << " score = " << pref[j].score[1] << endl;
308                                         }
309 //cout << "pref[" << i << "].closestLeft[1] = " <<      pref[i].closestLeft[1] << " parent = " << pref[i].leftParent[1] << endl;                        
310                                         //are you the closest left sequence
311                                         if (itL->second < pref[i].closestLeft[1]) {  
312
313                                                 pref[i].closestLeft[1] = itL->second;
314                                                 pref[i].leftParent[1] = seqs[j]->getName();
315 //cout << "updating closest left to " << pref[i].leftParent[1] << endl;
316                                         }
317 //cout << "pref[" << j << "].closestLeft[1] = " <<      pref[j].closestLeft[1] << " parent = " << pref[j].leftParent[1] << endl;        
318                                         if (itL->second < pref[j].closestLeft[1]) { 
319                                                 pref[j].closestLeft[1] = itL->second;
320                                                 pref[j].leftParent[1] = seqs[i]->getName();
321 //cout << "updating closest left to " << pref[j].leftParent[1] << endl;
322                                         }
323                                         
324                                         //are you the closest right sequence
325                                         if (itR->second < pref[i].closestRight[1]) {   
326                                                 pref[i].closestRight[1] = itR->second;
327                                                 pref[i].rightParent[1] = seqs[j]->getName();
328                                         }
329                                         if (itR->second < pref[j].closestRight[1]) {   
330                                                 pref[j].closestRight[1] = itR->second;
331                                                 pref[j].rightParent[1] = seqs[i]->getName();
332                                         }
333                                         
334                                 }
335                         }
336                 
337                 }
338                 
339                 
340                   
341                 //calculate the dme
342                 int count0 = 0;
343                 for (int i = 0; i < pref.size(); i++) {  dme += pref[i].score[1];  if (pref[i].score[1] == 0.0) { count0++; }  }
344                 
345                 float expectedPercent = 1 / (float) (pref.size() - count0);
346 //cout << endl << "dme = " << dme << endl;
347                 //recalculate prefernences based on dme
348                 for (int i = 0; i < pref.size(); i++) {
349 //cout << "unadjusted pref " << i << " = " << pref[i].score[1] << endl; 
350                         // gives the actual percentage of the dme this seq adds
351                         pref[i].score[1] = pref[i].score[1] / dme;
352                         
353                         //how much higher or lower is this than expected
354                         pref[i].score[1] = pref[i].score[1] / expectedPercent;
355                         
356                         //pref[i].score[1] = dme / (dme - 2 * pref[i].score[1]);
357                         
358                         //so a non chimeric sequence would be around 1, and a chimeric would be signifigantly higher.
359 //cout << "adjusted pref " << i << " = " << pref[i].score[1] << endl;                                   
360                 }
361                 
362                 //is this score bigger then the last score
363                 for (int i = 0; i < pref.size(); i++) {  
364                         
365                         //update biggest score
366                         if (pref[i].score[1] > pref[i].score[0]) {
367                                 pref[i].score[0] = pref[i].score[1];
368                                 pref[i].leftParent[0] = pref[i].leftParent[1];
369                                 pref[i].rightParent[0] = pref[i].rightParent[1];
370                                 pref[i].closestLeft[0] = pref[i].closestLeft[1];
371                                 pref[i].closestRight[0] = pref[i].closestRight[1];
372                                 pref[i].midpoint = mid;
373                         }
374                         
375                 }
376
377         }
378         catch(exception& e) {
379                 errorOut(e, "Bellerophon", "generatePreferences");
380                 exit(1);
381         }
382 }
383 /**************************************************************************************************/
384