]> git.donarmstrong.com Git - mothur.git/blobdiff - raredisplay.cpp
sffinfo bug with flow grams right index when clipQualRight=0
[mothur.git] / raredisplay.cpp
index e2c75e9d0d78e11de49ad96e72d0455dc4f90a5c..b82127c51e6a64b50480a0f9b359cda3cd8e75f0 100644 (file)
@@ -3,7 +3,7 @@
  *  Dotur
  *
  *  Created by Sarah Westcott on 11/18/08.
- *  Copyright 2008 __MyCompanyName__. All rights reserved.
+ *  Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
  *
  */
 
 void RareDisplay::init(string label){
        try {
                this->label = label;
-               if(nIters != 1){
-                       openOutputFile(tempOutName, tempOutFile);
-                       openInputFile(tempInName, tempInFile);
-               }
-               else{
-                       openOutputFile(tempOutName, tempOutFile);
-               }
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the RareDisplay class Function init. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-       catch(...) {
-               cout << "An unknown error has occurred in the RareDisplay class function init. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "RareDisplay", "init");
                exit(1);
        }
 }
@@ -41,88 +30,72 @@ void RareDisplay::update(SAbundVector* rank){
 
                if(nIters != 1){
 
-                       double oldNSeqs, oldMean, oldVar;
-               
-                       tempInFile >> oldNSeqs >> oldMean >> oldVar;
-               
-                       double oldS = oldVar * ( nIters - 2 );
-                       double delta = data[0] - oldMean;
-                       double newMean = oldMean + delta / nIters;
+                       double oldS = var[index] * ( nIters - 2 );
+                       double delta = data[0] - results[index];
+                       double newMean = results[index] + delta / nIters;
                        double newS = oldS + delta * ( data[0] - newMean );
                        double newVar = newS / ( nIters - 1 );
 
-                       tempOutFile << newNSeqs << '\t' << newMean << '\t' << newVar << endl;
+                       seqs[index] = newNSeqs;
+                       results[index] = newMean; 
+                       var[index] = newVar;
+                       
+                       index++;  
                }
                else{
-                       tempOutFile << newNSeqs << '\t' << data[0] << '\t' << 0 << endl;
+                       seqs.push_back(newNSeqs); 
+                       results.push_back(data[0]);
+                       var.push_back(0.0);
                }
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the RareDisplay class Function update. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "RareDisplay", "update");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the RareDisplay class function update. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-};
+}
 
 /***********************************************************************/
-void RareDisplay::update(SharedRAbundVector* shared1, SharedRAbundVector* shared2, int numSeqs, int numGroupComb) {
+void RareDisplay::update(vector<SharedRAbundVector*> shared, int numSeqs, int numGroupComb) {
        try {
-               vector<double> data = estimate->getValues(shared1, shared2); 
+               vector<double> data = estimate->getValues(shared); 
                double newNSeqs = data[0];
                
                if(nIters != 1){
-                       double oldNSeqs, oldMean, oldVar;
-               
-                       tempInFile >> oldNSeqs >> oldMean >> oldVar;
                
-                       double oldS = oldVar * ( nIters - 2 );
-                       double delta = data[0] - oldMean;
-                       double newMean = oldMean + delta / nIters;
+                       double oldS = var[index] * ( nIters - 2 );
+                       double delta = data[0] - results[index];
+                       double newMean = results[index] + delta / nIters;
                        double newS = oldS + delta * ( data[0] - newMean );
                        double newVar = newS / ( nIters - 1 );
-
-                       tempOutFile << newNSeqs << '\t' << newMean << '\t' << newVar << endl;
+                       seqs[index] = newNSeqs;
+                       results[index] = newMean; 
+                       var[index] = newVar;
+                       
+                       index++;  
                }
                else{
-                       tempOutFile << newNSeqs << '\t' << data[0] << '\t' << 0 << endl;
+                       
+                       seqs.push_back(newNSeqs); 
+                       results.push_back(data[0]);
+                       var.push_back(0.0);
+
                }
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the RareDisplay class Function update. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-       catch(...) {
-               cout << "An unknown error has occurred in the RareDisplay class function update. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "RareDisplay", "update");
                exit(1);
        }
-};
+}
 
 /***********************************************************************/
 
 void RareDisplay::reset(){
        try {
-               if(nIters != 1){
-                       tempOutFile.close();
-                       tempInFile.close();
-               }
-               else{
-                       tempOutFile.close();
-               }
-       
                nIters++;
-       
-               remove(tempInName.c_str());
-               rename(tempOutName.c_str(), tempInName.c_str());        
+               index = 0;
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the RareDisplay class Function reset. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-       catch(...) {
-               cout << "An unknown error has occurred in the RareDisplay class function reset. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "RareDisplay", "reset");
                exit(1);
        }
 }
@@ -131,45 +104,86 @@ void RareDisplay::reset(){
 
 void RareDisplay::close(){
        try {
+               
                output->initFile(label);
        
-               openInputFile(tempInName, tempInFile);
-       
-               while(!tempInFile.eof()){
-                       int nSeqs;
-                       tempInFile >> nSeqs;
+               for (int i = 0; i < seqs.size(); i++) {
                
                        vector<double> data(3,0);
-                       double variance = 0;
-               
-                       tempInFile >> data[0];
-                       tempInFile >> variance;
-               
+                       double variance = var[i];
+                       
+                       data[0] = results[i];
+                       
                        double ci = 1.96 * pow(variance, 0.5);
                        data[1] = data[0] - ci;
                        data[2] = data[0] + ci;
                
-                       output->output(nSeqs, data);
-               
-                       gobble(tempInFile);
+                       output->output(seqs[i], data);
                }
-               tempInFile.close();
-       
-               remove(tempInName.c_str());
-               remove(tempOutName.c_str());
-       
+               
                nIters = 1;
+               index = 0;
+               
+               seqs.clear();
+               results.clear();
+               var.clear();
+               
                output->resetFile();
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the RareDisplay class Function close. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "RareDisplay", "close");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the RareDisplay class function close. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+}
+/***********************************************************************/
+
+void RareDisplay::inputTempFiles(string filename){
+       try {
+               ifstream in;
+               m->openInputFile(filename, in);
+               
+               int thisIters;
+               in >> thisIters; m->gobble(in);
+               
+               for (int i = 0; i < seqs.size(); i++) {
+                       double tempresult, tempvar;
+                       in >> tempresult >> tempvar; m->gobble(in);
+                       
+                       //find weighted result
+                       results[i] = ((nIters * results[i]) + (thisIters * tempresult)) / (float)(nIters + thisIters);
+                       
+                       var[i] = ((nIters * var[i]) + (thisIters * tempvar)) / (float)(nIters + thisIters);
+               }
+               
+               in.close();
+       }
+       catch(exception& e) {
+               m->errorOut(e, "RareDisplay", "inputTempFiles");
                exit(1);
        }
 }
 
 /***********************************************************************/
 
+void RareDisplay::outputTempFiles(string filename){
+       try {
+               ofstream out;
+               m->openOutputFile(filename, out);
+               
+               out << nIters << endl;
+               
+               for (int i = 0; i < seqs.size(); i++) {
+                       out << results[i] << '\t' << var[i] << endl;
+               }
+               
+               out.close();
+       }
+       catch(exception& e) {
+               m->errorOut(e, "RareDisplay", "outputTempFiles");
+               exit(1);
+       }
+}
+
+
+/***********************************************************************/
+