X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=raredisplay.cpp;h=da22e74e90731d5294c6250d4c19f1a84045c06d;hp=e2c75e9d0d78e11de49ad96e72d0455dc4f90a5c;hb=b206f634aae1b4ce13978d203247fb64757d5482;hpb=20a2d0350a737a434c89f303662d64a8eeea7b05 diff --git a/raredisplay.cpp b/raredisplay.cpp index e2c75e9..da22e74 100644 --- a/raredisplay.cpp +++ b/raredisplay.cpp @@ -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. * */ @@ -14,20 +14,9 @@ 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 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 >::iterator it = results.find(newNSeqs); + if (it == results.end()) { //first iter for this count + vector 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 shared, int numSeqs, int numGroupComb) { try { - vector data = estimate->getValues(shared1, shared2); - double newNSeqs = data[0]; - - if(nIters != 1){ - double oldNSeqs, oldMean, oldVar; - - tempInFile >> oldNSeqs >> oldMean >> oldVar; + vector 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 >::iterator it = results.find(numSeqs); + if (it == results.end()) { //first iter for this count + vector 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 >::iterator it = results.begin(); it != results.end(); it++) { vector 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 >::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 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 >::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); } } + /***********************************************************************/