]> git.donarmstrong.com Git - mothur.git/blobdiff - raredisplay.cpp
fixes while testing 1.33.0
[mothur.git] / raredisplay.cpp
index b86312bc4411e8cdd2faca9934aab3abf5c96c52..da22e74e90731d5294c6250d4c19f1a84045c06d 100644 (file)
 void RareDisplay::init(string label){
        try {
                this->label = label;
-               if(nIters != 1){
-                       tempInFile.clear();
-                       openOutputFile(tempOutName, tempOutFile);
-                       openInputFile(tempInName, tempInFile);
-               }
-               else{
-                       openOutputFile(tempOutName, tempOutFile);
-               }
        }
        catch(exception& e) {
-               errorOut(e, "RareDisplay", "init");
+               m->errorOut(e, "RareDisplay", "init");
                exit(1);
        }
 }
@@ -36,26 +28,17 @@ 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) {
-               errorOut(e, "RareDisplay", "update");
+               m->errorOut(e, "RareDisplay", "update");
                exit(1);
        }
 }
@@ -64,27 +47,18 @@ void RareDisplay::update(SAbundVector* rank){
 void RareDisplay::update(vector<SharedRAbundVector*> shared, int numSeqs, int numGroupComb) {
        try {
                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 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) {
-               errorOut(e, "RareDisplay", "update");
+               m->errorOut(e, "RareDisplay", "update");
                exit(1);
        }
 }
@@ -93,24 +67,10 @@ void RareDisplay::update(vector<SharedRAbundVector*> shared, int numSeqs, int nu
 
 void RareDisplay::reset(){
        try {
-               if(nIters != 1){
-                       tempOutFile.close();
-                       tempInFile.close();
-               }
-               else{
-                       tempOutFile.close();
-               }
-       
                nIters++;
-       
-               remove(tempInName.c_str());
-               renameOk = rename(tempOutName.c_str(), tempInName.c_str());     
-               
-               //checks to make sure user was able to rename and remove successfully
-               if (renameOk != 0) { mothurOut("Unable to rename the necessary temp files."); mothurOutEndLine(); }
        }
        catch(exception& e) {
-               errorOut(e, "RareDisplay", "reset");
+               m->errorOut(e, "RareDisplay", "reset");
                exit(1);
        }
 }
@@ -119,42 +79,101 @@ void RareDisplay::reset(){
 
 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->output(nSeqs, data);
+               output->resetFile();
+       }
+       catch(exception& e) {
+               m->errorOut(e, "RareDisplay", "close");
+               exit(1);
+       }
+}
+/***********************************************************************/
+
+void RareDisplay::inputTempFiles(string filename){
+       try {
+               ifstream in;
+               m->openInputFile(filename, in);
                
-                       gobble(tempInFile);
+               int thisIters, size;
+               in >> thisIters >> size; m->gobble(in);
+        nIters += thisIters;
+               
+               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) {
+               m->errorOut(e, "RareDisplay", "inputTempFiles");
+               exit(1);
+       }
+}
+
+/***********************************************************************/
+
+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) {
-               errorOut(e, "RareDisplay", "close");
+               m->errorOut(e, "RareDisplay", "outputTempFiles");
                exit(1);
        }
 }
 
+
 /***********************************************************************/