]> git.donarmstrong.com Git - mothur.git/blob - collectcommand.cpp
added smart distance feature and optimized all commands using line by line processing
[mothur.git] / collectcommand.cpp
1 /*
2  *  collectcommand.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "collectcommand.h"
11 #include "ace.h"
12 #include "sobs.h"
13 #include "nseqs.h"
14 #include "chao1.h"
15 #include "bootstrap.h"
16 #include "simpson.h"
17 #include "npshannon.h"
18 #include "shannon.h"
19 #include "jackknife.h"
20 #include "geom.h"
21 #include "qstat.h"
22 #include "logsd.h"
23 #include "bergerparker.h"
24 #include "bstick.h"
25 #include "goodscoverage.h"
26 #include "efron.h"
27 #include "boneh.h"
28 #include "solow.h"
29 #include "shen.h"
30 #include "coverage.h"
31
32
33 //**********************************************************************************************************************
34 CollectCommand::CollectCommand(){
35         try {
36                 globaldata = GlobalData::getInstance();
37                 string fileNameRoot;
38                 fileNameRoot = getRootName(globaldata->inputFileName);
39                 convert(globaldata->getFreq(), freq);
40                 int i;
41                 validCalculator = new ValidCalculators();
42                 
43                 for (i=0; i<globaldata->Estimators.size(); i++) {
44                         if (validCalculator->isValidCalculator("single", globaldata->Estimators[i]) == true) { 
45                                 if (globaldata->Estimators[i] == "sobs") { 
46                                         cDisplays.push_back(new CollectDisplay(new Sobs(), new OneColumnFile(fileNameRoot+"sobs")));
47                                 }else if (globaldata->Estimators[i] == "chao") { 
48                                         cDisplays.push_back(new CollectDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"chao")));
49                                 }else if (globaldata->Estimators[i] == "nseqs") { 
50                                         cDisplays.push_back(new CollectDisplay(new NSeqs(), new OneColumnFile(fileNameRoot+"nseqs")));
51                                 }else if (globaldata->Estimators[i] == "coverage") { 
52                                         cDisplays.push_back(new CollectDisplay(new Coverage(), new OneColumnFile(fileNameRoot+"coverage")));
53                                 }else if (globaldata->Estimators[i] == "ace") { 
54                                         convert(globaldata->getAbund(), abund);
55                                         cDisplays.push_back(new CollectDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"ace")));
56                                 }else if (globaldata->Estimators[i] == "jack") { 
57                                         cDisplays.push_back(new CollectDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"jack")));
58                                 }else if (globaldata->Estimators[i] == "shannon") { 
59                                         cDisplays.push_back(new CollectDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"shannon")));
60                                 }else if (globaldata->Estimators[i] == "npshannon") { 
61                                         cDisplays.push_back(new CollectDisplay(new NPShannon(), new OneColumnFile(fileNameRoot+"np_shannon")));
62                                 }else if (globaldata->Estimators[i] == "simpson") { 
63                                         cDisplays.push_back(new CollectDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"simpson")));
64                                 }else if (globaldata->Estimators[i] == "bootstrap") { 
65                                         cDisplays.push_back(new CollectDisplay(new Bootstrap(), new OneColumnFile(fileNameRoot+"bootstrap")));
66                                 }else if (globaldata->Estimators[i] == "geometric") { 
67                                         cDisplays.push_back(new CollectDisplay(new Geom(), new OneColumnFile(fileNameRoot+"geometric")));
68                                 }else if (globaldata->Estimators[i] == "qstat") { 
69                                         cDisplays.push_back(new CollectDisplay(new QStat(), new OneColumnFile(fileNameRoot+"qstat")));
70                                 }else if (globaldata->Estimators[i] == "logseries") { 
71                                         cDisplays.push_back(new CollectDisplay(new LogSD(), new OneColumnFile(fileNameRoot+"logseries")));
72                                 }else if (globaldata->Estimators[i] == "bergerparker") { 
73                                         cDisplays.push_back(new CollectDisplay(new BergerParker(), new OneColumnFile(fileNameRoot+"bergerparker")));
74                                 }else if (globaldata->Estimators[i] == "bstick") { 
75                                         cDisplays.push_back(new CollectDisplay(new BStick(), new ThreeColumnFile(fileNameRoot+"bstick")));
76                                 }else if (globaldata->Estimators[i] == "goodscoverage") { 
77                                         cDisplays.push_back(new CollectDisplay(new GoodsCoverage(), new OneColumnFile(fileNameRoot+"goodscoverage")));
78                                 }else if (globaldata->Estimators[i] == "efron") {
79                                         convert(globaldata->getSize(), size); 
80                                         cDisplays.push_back(new CollectDisplay(new Efron(size), new OneColumnFile(fileNameRoot+"efron")));
81                                 }else if (globaldata->Estimators[i] == "boneh") {
82                                         convert(globaldata->getSize(), size); 
83                                         cDisplays.push_back(new CollectDisplay(new Boneh(size), new OneColumnFile(fileNameRoot+"boneh")));
84                                 }else if (globaldata->Estimators[i] == "solow") {
85                                         convert(globaldata->getSize(), size); 
86                                         cDisplays.push_back(new CollectDisplay(new Solow(size), new OneColumnFile(fileNameRoot+"solow")));
87                                 }else if (globaldata->Estimators[i] == "shen") {
88                                         convert(globaldata->getSize(), size); 
89                                         cDisplays.push_back(new CollectDisplay(new Shen(size), new OneColumnFile(fileNameRoot+"shen")));
90                                 }
91                         }
92                 }
93                 
94                 //reset calc for next command
95                 globaldata->setCalc("");
96         }
97         catch(exception& e) {
98                 cout << "Standard Error: " << e.what() << " has occurred in the CollectCommand class Function CollectCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
99                 exit(1);
100         }
101         catch(...) {
102                 cout << "An unknown error has occurred in the CollectCommand class function CollectCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
103                 exit(1);
104         }       
105                         
106 }
107
108 //**********************************************************************************************************************
109
110 CollectCommand::~CollectCommand(){
111         delete order;
112         delete input;
113         delete cCurve;
114         delete read;
115 }
116
117 //**********************************************************************************************************************
118
119 int CollectCommand::execute(){
120         try {
121                 int count = 1;
122                 
123                 //if the users entered no valid calculators don't execute command
124                 if (cDisplays.size() == 0) { return 0; }
125
126                 read = new ReadOTUFile(globaldata->inputFileName);      
127                 read->read(&*globaldata); 
128                 
129                 order = globaldata->gorder;
130                 lastOrder = order;
131                 input = globaldata->ginput;
132                 
133                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
134                 set<string> processedLabels;
135                 set<string> userLabels = globaldata->labels;
136                 
137                 while((order != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
138                 
139                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){
140                                 
141                                 cCurve = new Collect(order, cDisplays);
142                                 cCurve->getCurve(freq);
143                                 delete cCurve;
144                         
145                                 cout << order->getLabel() << '\t' << count << endl;
146                                 processedLabels.insert(order->getLabel());
147                                 userLabels.erase(order->getLabel());
148                         
149                         //you have a label the user want that is smaller than this line and the last line has not already been processed 
150                         }
151                         
152                         if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastOrder->getLabel()) != 1)) {
153                                 cCurve = new Collect(lastOrder, cDisplays);
154                                 cCurve->getCurve(freq);
155                                 delete cCurve;
156                         
157                                 cout << lastOrder->getLabel() << '\t' << count << endl;
158                                 processedLabels.insert(lastOrder->getLabel());
159                                 userLabels.erase(lastOrder->getLabel());
160                         }
161                         
162                         if (count != 1) { delete lastOrder; }
163                         lastOrder = order;                      
164                         order = (input->getOrderVector());
165                         count++;
166                 }
167                 
168                 //output error messages about any remaining user labels
169                 set<string>::iterator it;
170                 bool needToRun = false;
171                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
172                         cout << "Your file does not include the label "<< *it; 
173                         if (processedLabels.count(lastOrder->getLabel()) != 1) {
174                                 cout << ". I will use " << lastOrder->getLabel() << "." << endl;
175                                 needToRun = true;
176                         }else {
177                                 cout << ". Please refer to " << lastOrder->getLabel() << "." << endl;
178                         }
179                 }
180                 
181                 //run last line if you need to
182                 if (needToRun == true)  {
183                         cCurve = new Collect(lastOrder, cDisplays);
184                         cCurve->getCurve(freq);
185                         delete cCurve;
186                         
187                         cout << lastOrder->getLabel() << '\t' << count << endl;
188                 }
189                 
190                 delete lastOrder;
191                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
192                 return 0;
193         }
194         catch(exception& e) {
195                 cout << "Standard Error: " << e.what() << " has occurred in the CollectCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
196                 exit(1);
197         }
198         catch(...) {
199                 cout << "An unknown error has occurred in the CollectCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
200                 exit(1);
201         }       
202 }
203
204 //**********************************************************************************************************************