]> git.donarmstrong.com Git - mothur.git/commitdiff
speed up rarefaction.shared
authorwestcott <westcott>
Fri, 15 May 2009 13:55:53 +0000 (13:55 +0000)
committerwestcott <westcott>
Fri, 15 May 2009 13:55:53 +0000 (13:55 +0000)
heatmapcommand.cpp
rarefact.cpp
rarefact.h
rarefactsharedcommand.cpp
rarefactsharedcommand.h
sharedlistvector.cpp
sharedordervector.cpp
sharedordervector.h
sharedrabundvector.cpp
sharedsabundvector.cpp
venncommand.cpp

index fd7a8279e689b8226b2939ac8a07656dd290d530..09dfb5a15bde8e52a967c1ef9cdae0e4130a0b3a 100644 (file)
@@ -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++;
                        }
index cb4bbbce9a4047ae08b5f37b807be3f78c255d89..5c1ce741c951a620fb68529b834a3fd44e4b1840 100644 (file)
@@ -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;i<displays.size();i++){
                        rcd->registerDisplay(displays[i]);
                }
-
+               
                for(int iter=0;iter<nIters;iter++){
-                       //clear out old values for new iter
-                       lookup.clear();
                
-                       //create and initialize vector of sharedvectors, one for each group
-                       for (int i = 0; i < globaldata->Groups.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;i<displays.size();i++){
                                displays[i]->init(label);                 
                        }
-               
-                       //sample all the members
-                       for(int i=0;i<numSeqs;i++){
-                               //get first sample
-                               individual chosen = sharedorder->get(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<displays.size();i++){
index fc1e8d13e4b289851c65e72a1ebd1804edfbd44d..6eed91214905f3e8f50b89043c45ea8203b2b808 100644 (file)
@@ -14,16 +14,14 @@ class Rarefact {
 public:
        Rarefact(OrderVector* o, vector<Display*> disp) :
                        numSeqs(o->getNumSeqs()), order(o), displays(disp), label(o->getLabel())  {};
-       Rarefact(SharedOrderVector* sharedorder, vector<Display*> disp) :
-                                       numSeqs(sharedorder->getNumSeqs()), sharedorder(sharedorder), displays(disp), label(sharedorder->getLabel())  {};
+       Rarefact(vector<SharedRAbundVector*> shared, vector<Display*> disp) :
+                                        lookup(shared), displays(disp) {};
 
        ~Rarefact(){};
        void getCurve(int, int);
        void getSharedCurve(int, int);
        
 private:
-       SharedOrderVector* sharedorder;
-       GlobalData* globaldata;
        OrderVector* order;
        vector<Display*> displays;
        int numSeqs, numGroupComb;
index 4a25cb3208f2695fc50308a8ff7df83deda163a8..0928d2e6b439b9775e62b5abd8c91b4254216e67 100644 (file)
@@ -20,7 +20,6 @@ RareFactSharedCommand::RareFactSharedCommand(){
                fileNameRoot = getRootName(globaldata->inputFileName);
                format = globaldata->getFormat();
                validCalculator = new ValidCalculators();
-               util = new SharedUtil();
                                
                int i;
                for (i=0; i<globaldata->Estimators.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++;
                }
        
index a16fdb9d8f1ff7166c6b78ca1815368a3c45e287..1935398ac1c312020e03edc32b30fc7ce10045fb 100644 (file)
  */
 
 #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<SharedRAbundVector*> lookup;
        InputData* input;
        ValidCalculators* validCalculator;
        Rarefact* rCurve;
index 02c012e797e47efe6e509c7bdd62bd79fa3e390d..2dc4502cd4523a3b679abcc3d3e3156ea60b0ab9 100644 (file)
@@ -236,6 +236,8 @@ SharedOrderVector* SharedListVector::getSharedOrderVector(){
                }
 
                random_shuffle(order->begin(), order->end());
+               order->updateStats();
+               
                return order;
        }
        catch(exception& e) {
index 973f69553a476c4e67f72c4b9f10cecae0bc32b6..08692de4ca3fec388ff31407aeee1c0a4d04dd61 100644 (file)
@@ -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<data.size();i++){
-
-                       if(data[i].bin != -1){
-                               numSeqs++;
-                       }
-               }
-               //vector<individual> hold(numSeqs, 0);
-       
-               //for(int i=0;i<numSeqs;i++){
-                       //if(data[i].bin != -1){
-                               //hold[data[i].bin].bin = hold[data[i].bin].bin+1;
-                       //}
-               //}     
-
+               numSeqs = data.size();
+                               
+               vector<int> hold(numSeqs, 0);
                for(int i=0;i<numSeqs;i++){
-                       if(data[i].bin > 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<numSeqs;i++){
+                       if(hold[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";
index e941c7e5f860b770c5a766027d3cc658c94e3259..30d655f7347c07fcb9a887ac5330959702e09382 100644 (file)
@@ -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<individual>::iterator begin();
        vector<individual>::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
index a6ade30725644ef9da4cfd303a17530e26eff3d8..1711012d4a0e06945492252d0c9bdaca4d9bac90 100644 (file)
@@ -514,6 +514,8 @@ SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
                random_shuffle(ov.begin(), ov.end());
 
                ov.setLabel(label);     
+               ov.updateStats();
+               
                return ov;
        }
        catch(exception& e) {
index 3cc80f75fbf08728ca793f0f5147b7e043c85f41..0e86977e498a49255f037d2080ef5226a1c3bd6b 100644 (file)
@@ -253,6 +253,8 @@ SharedOrderVector SharedSAbundVector::getSharedOrderVector() {
                rav = this->getSharedRAbundVector();
                ov = rav.getSharedOrderVector();
                
+               ov.updateStats();
+               
                return ov;
        }
        catch(exception& e) {
index b2dfc89bc6ad09c283e31b15523c8a868ad01711..a934126a944513a427f1c202a9a81bec93053a21 100644 (file)
@@ -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++;
                        }