From 37519fc35c0eb1523e84227517694c4015847c6d Mon Sep 17 00:00:00 2001 From: westcott Date: Fri, 15 May 2009 13:55:53 +0000 Subject: [PATCH] speed up rarefaction.shared --- heatmapcommand.cpp | 6 +++++- rarefact.cpp | 36 ++++-------------------------------- rarefact.h | 6 ++---- rarefactsharedcommand.cpp | 29 ++++++++++++++++------------- rarefactsharedcommand.h | 7 +------ sharedlistvector.cpp | 2 ++ sharedordervector.cpp | 32 +++++++++++++------------------- sharedordervector.h | 9 +++++---- sharedrabundvector.cpp | 2 ++ sharedsabundvector.cpp | 2 ++ venncommand.cpp | 7 ++++++- 11 files changed, 58 insertions(+), 80 deletions(-) diff --git a/heatmapcommand.cpp b/heatmapcommand.cpp index fd7a827..09dfb5a 100644 --- a/heatmapcommand.cpp +++ b/heatmapcommand.cpp @@ -66,7 +66,10 @@ int HeatMapCommand::execute(){ cout << lookup[0]->getLabel() << '\t' << count << endl; heatmap->getPic(lookup); } - + + //prevent memory leak + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + //get next line to process lookup = input->getSharedRAbundVectors(); count++; @@ -85,6 +88,7 @@ int HeatMapCommand::execute(){ heatmap->getPic(rabund); } + delete rabund; rabund = input->getRAbundVector(); count++; } diff --git a/rarefact.cpp b/rarefact.cpp index cb4bbbc..5c1ce74 100644 --- a/rarefact.cpp +++ b/rarefact.cpp @@ -76,46 +76,21 @@ void Rarefact::getCurve(int increment = 1, int nIters = 1000){ void Rarefact::getSharedCurve(int increment = 1, int nIters = 1000){ try { - globaldata = GlobalData::getInstance(); SharedRarefactionCurveData* rcd = new SharedRarefactionCurveData(); + label = lookup[0]->getLabel(); + //register the displays for(int i=0;iregisterDisplay(displays[i]); } - + for(int iter=0;iterGroups.size(); i++) { - SharedRAbundVector* temp = new SharedRAbundVector(sharedorder->getNumBins()); - temp->setLabel(sharedorder->getLabel()); - temp->setGroup(globaldata->Groups[i]); - lookup.push_back(temp); - } - for(int i=0;iinit(label); } - - //sample all the members - for(int i=0;iget(i); - int abundance; - - //set info for sharedvector in chosens group - for (int j = 0; j < lookup.size(); j++) { - if (chosen.group == lookup[j]->getGroup()) { - abundance = lookup[j]->getAbundance(chosen.bin); - lookup[j]->set(chosen.bin, (abundance + 1), chosen.group); - break; - } - } - } - + //randomize the groups random_shuffle(lookup.begin(), lookup.end()); @@ -135,9 +110,6 @@ try { displays[i]->reset(); } - for (int i = 0; i < lookup.size(); i++) { - delete lookup[i]; - } } for(int i=0;i disp) : numSeqs(o->getNumSeqs()), order(o), displays(disp), label(o->getLabel()) {}; - Rarefact(SharedOrderVector* sharedorder, vector disp) : - numSeqs(sharedorder->getNumSeqs()), sharedorder(sharedorder), displays(disp), label(sharedorder->getLabel()) {}; + Rarefact(vector shared, vector disp) : + lookup(shared), displays(disp) {}; ~Rarefact(){}; void getCurve(int, int); void getSharedCurve(int, int); private: - SharedOrderVector* sharedorder; - GlobalData* globaldata; OrderVector* order; vector displays; int numSeqs, numGroupComb; diff --git a/rarefactsharedcommand.cpp b/rarefactsharedcommand.cpp index 4a25cb3..0928d2e 100644 --- a/rarefactsharedcommand.cpp +++ b/rarefactsharedcommand.cpp @@ -20,7 +20,6 @@ RareFactSharedCommand::RareFactSharedCommand(){ fileNameRoot = getRootName(globaldata->inputFileName); format = globaldata->getFormat(); validCalculator = new ValidCalculators(); - util = new SharedUtil(); int i; for (i=0; iEstimators.size(); i++) { @@ -52,11 +51,9 @@ RareFactSharedCommand::RareFactSharedCommand(){ //********************************************************************************************************************** RareFactSharedCommand::~RareFactSharedCommand(){ - delete order; delete input; delete rCurve; delete read; - delete util; } //********************************************************************************************************************** @@ -72,28 +69,34 @@ int RareFactSharedCommand::execute(){ read->read(&*globaldata); input = globaldata->ginput; - order = input->getSharedOrderVector(); - - //set users groups - util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "rarefact"); + lookup = input->getSharedRAbundVectors(); + + if (lookup.size() < 2) { + cout << "I cannot run the command without at least 2 valid groups."; + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + return 0; + } + - while(order != NULL){ + while(lookup[0] != NULL){ - if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){ + if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){ //create collectors curve - rCurve = new Rarefact(order, rDisplays); + rCurve = new Rarefact(lookup, rDisplays); convert(globaldata->getFreq(), freq); convert(globaldata->getIters(), nIters); rCurve->getSharedCurve(freq, nIters); delete rCurve; - cout << order->getLabel() << '\t' << count << endl; + cout << lookup[0]->getLabel() << '\t' << count << endl; } + //prevent memory leak + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + //get next line to process - delete order; - order = input->getSharedOrderVector(); + lookup = input->getSharedRAbundVectors(); count++; } diff --git a/rarefactsharedcommand.h b/rarefactsharedcommand.h index a16fdb9..1935398 100644 --- a/rarefactsharedcommand.h +++ b/rarefactsharedcommand.h @@ -10,14 +10,11 @@ */ #include "command.hpp" -#include "sharedordervector.h" -#include "sharedlistvector.h" #include "inputdata.h" #include "rarefact.h" #include "display.h" #include "readotu.h" #include "validcalculator.h" -#include "sharedutilities.h" /* The rarefaction.shared() command: The rarefaction command generates a rarefaction curve from a given file representing several groups. @@ -42,10 +39,8 @@ public: private: GlobalData* globaldata; - SharedUtil* util; - SharedListVector* SharedList; ReadOTUFile* read; - SharedOrderVector* order; + vector lookup; InputData* input; ValidCalculators* validCalculator; Rarefact* rCurve; diff --git a/sharedlistvector.cpp b/sharedlistvector.cpp index 02c012e..2dc4502 100644 --- a/sharedlistvector.cpp +++ b/sharedlistvector.cpp @@ -236,6 +236,8 @@ SharedOrderVector* SharedListVector::getSharedOrderVector(){ } random_shuffle(order->begin(), order->end()); + order->updateStats(); + return order; } catch(exception& e) { diff --git a/sharedordervector.cpp b/sharedordervector.cpp index 973f695..08692de 100644 --- a/sharedordervector.cpp +++ b/sharedordervector.cpp @@ -149,7 +149,7 @@ individual SharedOrderVector::get(int index){ /***********************************************************************/ - +//commented updateStats out to improve speed, but whoever calls this must remember to update when they are done with all the pushbacks they are doing void SharedOrderVector::push_back(int binNumber, int abund, string groupName){ individual newGuy; newGuy.group = groupName; @@ -159,7 +159,8 @@ void SharedOrderVector::push_back(int binNumber, int abund, string groupName){ //numSeqs++; //numBins++; //if (abund > maxRank) { maxRank = abund; } - updateStats(); + + //updateStats(); } /***********************************************************************/ @@ -354,25 +355,18 @@ void SharedOrderVector::updateStats(){ numBins = 0; maxRank = 0; - for(int i=0;i hold(numSeqs, 0); - - //for(int i=0;i hold(numSeqs, 0); for(int i=0;i numBins) { numBins = data[i].bin; } - if(data[i].abundance > maxRank) { maxRank = data[i].abundance; } + hold[data[i].bin] = hold[data[i].bin]+1; + } + + for(int i=0;i 0) { numBins++; } + if(hold[i] > maxRank) { maxRank = hold[i]; } } - numBins++; //if you have 10 bins largest .bin is 9 since we start at 0. + } catch(exception& e) { cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; diff --git a/sharedordervector.h b/sharedordervector.h index e941c7e..30d655f 100644 --- a/sharedordervector.h +++ b/sharedordervector.h @@ -49,15 +49,15 @@ public: SharedOrderVector(ifstream&); ~SharedOrderVector(){}; - void set(int, int, int, string); //index, OTU, abundance, group + individual get(int); - void push_back(int, int, string); //OTU, abundance, group void resize(int); int size(); void print(ostream&); vector::iterator begin(); vector::iterator end(); - + void push_back(int, int, string); //OTU, abundance, group MUST CALL UPDATE STATS AFTER PUSHBACK!!! + void updateStats(); int getNumBins(); int getNumSeqs(); @@ -80,7 +80,8 @@ private: int numBins; int numSeqs; bool needToUpdate; - void updateStats(); + void set(int, int, int, string); //index, OTU, abundance, group + }; #endif diff --git a/sharedrabundvector.cpp b/sharedrabundvector.cpp index a6ade30..1711012 100644 --- a/sharedrabundvector.cpp +++ b/sharedrabundvector.cpp @@ -514,6 +514,8 @@ SharedOrderVector SharedRAbundVector::getSharedOrderVector() { random_shuffle(ov.begin(), ov.end()); ov.setLabel(label); + ov.updateStats(); + return ov; } catch(exception& e) { diff --git a/sharedsabundvector.cpp b/sharedsabundvector.cpp index 3cc80f7..0e86977 100644 --- a/sharedsabundvector.cpp +++ b/sharedsabundvector.cpp @@ -253,6 +253,8 @@ SharedOrderVector SharedSAbundVector::getSharedOrderVector() { rav = this->getSharedRAbundVector(); ov = rav.getSharedOrderVector(); + ov.updateStats(); + return ov; } catch(exception& e) { diff --git a/venncommand.cpp b/venncommand.cpp index b2dfc89..a934126 100644 --- a/venncommand.cpp +++ b/venncommand.cpp @@ -80,6 +80,7 @@ VennCommand::~VennCommand(){ delete input; delete read; delete venn; + for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; } } //********************************************************************************************************************** @@ -124,7 +125,10 @@ int VennCommand::execute(){ //util->getSharedVectors(globaldata->Groups, lookup, order); //fills group vectors from order vector. venn->getPic(lookup, vennCalculators); } - + + //prevent memory leak + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + //get next line to process lookup = input->getSharedRAbundVectors(); count++; @@ -143,6 +147,7 @@ int VennCommand::execute(){ venn->getPic(sabund, vennCalculators); } + delete sabund; sabund = input->getSAbundVector(); count++; } -- 2.39.2