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