]> git.donarmstrong.com Git - mothur.git/blob - rarefactsharedcommand.cpp
speed up rarefaction.shared
[mothur.git] / rarefactsharedcommand.cpp
1 /*
2  *  rarefactsharedcommand.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/6/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "rarefactsharedcommand.h"
11 #include "sharedsobs.h"
12 #include "sharednseqs.h"
13
14 //**********************************************************************************************************************
15
16 RareFactSharedCommand::RareFactSharedCommand(){
17         try {
18                 globaldata = GlobalData::getInstance();
19                 string fileNameRoot;
20                 fileNameRoot = getRootName(globaldata->inputFileName);
21                 format = globaldata->getFormat();
22                 validCalculator = new ValidCalculators();
23                                 
24                 int i;
25                 for (i=0; i<globaldata->Estimators.size(); i++) {
26                         if (validCalculator->isValidCalculator("sharedrarefaction", globaldata->Estimators[i]) == true) { 
27                                 if (globaldata->Estimators[i] == "sharedobserved") { 
28                                         rDisplays.push_back(new RareDisplay(new SharedSobs(), new SharedThreeColumnFile(fileNameRoot+"shared.rarefaction", "")));
29                                 }else if (globaldata->Estimators[i] == "sharednseqs") { 
30                                         rDisplays.push_back(new RareDisplay(new SharedNSeqs(), new SharedThreeColumnFile(fileNameRoot+"shared.r_nseqs", "")));
31                                 }
32
33                         }
34                 }
35                 
36                 //reset calc for next command
37                 globaldata->setCalc("");
38
39         }
40         catch(exception& e) {
41                 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function RareFactSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
42                 exit(1);
43         }
44         catch(...) {
45                 cout << "An unknown error has occurred in the RareFactSharedCommand class function RareFactSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
46                 exit(1);
47         }       
48                         
49 }
50
51 //**********************************************************************************************************************
52
53 RareFactSharedCommand::~RareFactSharedCommand(){
54         delete input;
55         delete rCurve;
56         delete read;
57 }
58
59 //**********************************************************************************************************************
60
61 int RareFactSharedCommand::execute(){
62         try {
63                 int count = 1;
64                 
65                 //if the users entered no valid calculators don't execute command
66                 if (rDisplays.size() == 0) { return 0; }
67
68                 read = new ReadOTUFile(globaldata->inputFileName);      
69                 read->read(&*globaldata); 
70                         
71                 input = globaldata->ginput;
72                 lookup = input->getSharedRAbundVectors();
73                 
74                 if (lookup.size() < 2) { 
75                         cout << "I cannot run the command without at least 2 valid groups."; 
76                         for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
77                         return 0;
78                 }
79                                         
80                 
81                 while(lookup[0] != NULL){
82                 
83                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){
84                                 //create collectors curve
85                                 rCurve = new Rarefact(lookup, rDisplays);
86                                 convert(globaldata->getFreq(), freq);
87                                 convert(globaldata->getIters(), nIters);
88                                 rCurve->getSharedCurve(freq, nIters);
89                         
90                                 delete rCurve;
91                         
92                                 cout << lookup[0]->getLabel() << '\t' << count << endl;
93                         }
94                         
95                         //prevent memory leak
96                         for (int i = 0; i < lookup.size(); i++) {       delete lookup[i];       }
97                                 
98                         //get next line to process
99                         lookup = input->getSharedRAbundVectors();
100                         count++;
101                 }
102         
103                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }       
104                 
105                 //reset groups parameter
106                 globaldata->Groups.clear();  globaldata->setGroups("");
107
108                 return 0;
109         }
110         catch(exception& e) {
111                 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
112                 exit(1);
113         }
114         catch(...) {
115                 cout << "An unknown error has occurred in the RareFactSharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
116                 exit(1);
117         }       
118 }
119
120
121 //**********************************************************************************************************************