]> git.donarmstrong.com Git - mothur.git/blobdiff - raredisplay.cpp
fixes while testing 1.33.0
[mothur.git] / raredisplay.cpp
index e2c75e9d0d78e11de49ad96e72d0455dc4f90a5c..da22e74e90731d5294c6250d4c19f1a84045c06d 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);
        }
 }
@@ -39,90 +28,49 @@ void RareDisplay::update(SAbundVector* rank){
                int newNSeqs = rank->getNumSeqs();
                vector<double> data = estimate->getValues(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 newS = oldS + delta * ( data[0] - newMean );
-                       double newVar = newS / ( nIters - 1 );
-
-                       tempOutFile << newNSeqs << '\t' << newMean << '\t' << newVar << endl;
-               }
-               else{
-                       tempOutFile << newNSeqs << '\t' << data[0] << '\t' << 0 << endl;
-               }
+               map<int, vector<double> >::iterator it = results.find(newNSeqs);
+        if (it == results.end()) { //first iter for this count
+            vector<double> temp;
+            temp.push_back(data[0]);
+            results[newNSeqs] = temp;
+        }else {
+            it->second.push_back(data[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::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); 
-               double newNSeqs = data[0];
-               
-               if(nIters != 1){
-                       double oldNSeqs, oldMean, oldVar;
-               
-                       tempInFile >> oldNSeqs >> oldMean >> oldVar;
+               vector<double> data = estimate->getValues(shared); 
                
-                       double oldS = oldVar * ( nIters - 2 );
-                       double delta = data[0] - oldMean;
-                       double newMean = oldMean + delta / nIters;
-                       double newS = oldS + delta * ( data[0] - newMean );
-                       double newVar = newS / ( nIters - 1 );
-
-                       tempOutFile << newNSeqs << '\t' << newMean << '\t' << newVar << endl;
-               }
-               else{
-                       tempOutFile << newNSeqs << '\t' << data[0] << '\t' << 0 << endl;
-               }
+               map<int, vector<double> >::iterator it = results.find(numSeqs);
+        if (it == results.end()) { //first iter for this count
+            vector<double> temp;
+            temp.push_back(data[0]);
+            results[numSeqs] = temp;
+        }else {
+            it->second.push_back(data[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::reset(){
        try {
-               if(nIters != 1){
-                       tempOutFile.close();
-                       tempInFile.close();
-               }
-               else{
-                       tempOutFile.close();
-               }
-       
                nIters++;
-       
-               remove(tempInName.c_str());
-               rename(tempOutName.c_str(), tempInName.c_str());        
        }
        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);
        }
 }
@@ -133,43 +81,99 @@ void RareDisplay::close(){
        try {
                output->initFile(label);
        
-               openInputFile(tempInName, tempInFile);
-       
-               while(!tempInFile.eof()){
-                       int nSeqs;
-                       tempInFile >> nSeqs;
+               for (map<int, vector<double> >::iterator it = results.begin(); it != results.end(); it++) {
                
                        vector<double> data(3,0);
-                       double variance = 0;
+            
+            sort((it->second).begin(), (it->second).end());
+            
+            //lci=results[int(0.025*iter)] and hci=results[int(0.975*iter)]
+                       data[0] = (it->second)[(int)(0.50*(nIters-1))];
+            //data[0] = m->getAverage(it->second);
+                       data[1] = (it->second)[(int)(0.025*(nIters-1))];
+                       data[2] = (it->second)[(int)(0.975*(nIters-1))];
+            //cout << nIters << '\t' << (int)(0.025*(nIters-1)) << '\t' << (int)(0.975*(nIters-1)) << endl;
+            
+            //cout << it->first << '\t' << data[1] << '\t' << data[2] << endl;
                
-                       tempInFile >> data[0];
-                       tempInFile >> variance;
+                       output->output(it->first, data);
+               }
                
-                       double ci = 1.96 * pow(variance, 0.5);
-                       data[1] = data[0] - ci;
-                       data[2] = data[0] + ci;
+               nIters = 1;
+        results.clear();
+               
+               output->resetFile();
+       }
+       catch(exception& e) {
+               m->errorOut(e, "RareDisplay", "close");
+               exit(1);
+       }
+}
+/***********************************************************************/
+
+void RareDisplay::inputTempFiles(string filename){
+       try {
+               ifstream in;
+               m->openInputFile(filename, in);
                
-                       output->output(nSeqs, data);
+               int thisIters, size;
+               in >> thisIters >> size; m->gobble(in);
+        nIters += thisIters;
                
-                       gobble(tempInFile);
+               for (int i = 0; i < size; i++) {
+                       int tempCount;
+            in >> tempCount; m->gobble(in);
+            map<int, vector<double> >::iterator it = results.find(tempCount);
+            if (it != results.end()) {
+                for (int j = 0; j < thisIters; j++) {
+                    double value;
+                    in >> value; m->gobble(in);
+                    (it->second).push_back(value);
+                }
+            }else {
+                vector<double> tempValues;
+                for (int j = 0; j < thisIters; j++) {
+                    double value;
+                    in >> value; m->gobble(in);
+                    tempValues.push_back(value);
+                }
+                results[tempCount] = tempValues;
+            }
                }
-               tempInFile.close();
-       
-               remove(tempInName.c_str());
-               remove(tempOutName.c_str());
-       
-               nIters = 1;
-               output->resetFile();
+               
+               in.close();
        }
        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", "inputTempFiles");
                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::outputTempFiles(string filename){
+       try {
+               ofstream out;
+               m->openOutputFile(filename, out);
+               
+               out << nIters-1 << '\t' << results.size() << endl;
+               
+               for (map<int, vector<double> >::iterator it = results.begin(); it != results.end(); it++) {
+            out << it->first << '\t';
+            for(int i = 0; i < (it->second).size(); i++) {
+                out << (it->second)[i] << '\t';
+            }
+            out << endl;
+               }
+               
+               out.close();
+       }
+       catch(exception& e) {
+               m->errorOut(e, "RareDisplay", "outputTempFiles");
                exit(1);
        }
 }
 
+
 /***********************************************************************/