]> git.donarmstrong.com Git - mothur.git/blob - collectcommand.cpp
added hcluster command and fixed some bugs, namely one with smart distancing.
[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(string option){
35         try {
36                 globaldata = GlobalData::getInstance();
37                 abort = false;
38                 allLines = 1;
39                 labels.clear();
40                 Estimators.clear();
41                 
42                 //allow user to run help
43                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
44                 
45                 else {
46                         //valid paramters for this command
47                         string Array[] =  {"freq","label","calc","abund","size"};
48                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
49                         
50                         OptionParser parser(option);
51                         map<string,string> parameters = parser.getParameters();
52                         
53                         ValidParameters validParameter;
54                 
55                         //check to make sure all parameters are valid for command
56                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
57                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
58                         }
59                         
60                         //make sure the user has already run the read.otu command
61                         if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { mothurOut("You must read a list, sabund or rabund before you can use the collect.single command."); mothurOutEndLine(); abort = true; }
62                         
63                         //check for optional parameter and set defaults
64                         // ...at some point should added some additional type checking...
65                         label = validParameter.validFile(parameters, "label", false);                   
66                         if (label == "not found") { label = ""; }
67                         else { 
68                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
69                                 else { allLines = 1;  }
70                         }
71                         
72                         //if the user has not specified any labels use the ones from read.otu
73                         if (label == "") {  
74                                 allLines = globaldata->allLines; 
75                                 labels = globaldata->labels; 
76                         }
77                                 
78                         calc = validParameter.validFile(parameters, "calc", false);                     
79                         if (calc == "not found") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson";  }
80                         else { 
81                                  if (calc == "default")  {  calc = "sobs-chao-ace-jack-shannon-npshannon-simpson";  }
82                         }
83                         splitAtDash(calc, Estimators);
84
85                         string temp;
86                         temp = validParameter.validFile(parameters, "freq", false);                     if (temp == "not found") { temp = "100"; }
87                         convert(temp, freq); 
88                         
89                         temp = validParameter.validFile(parameters, "abund", false);            if (temp == "not found") { temp = "10"; }
90                         convert(temp, abund); 
91                         
92                         temp = validParameter.validFile(parameters, "size", false);                     if (temp == "not found") { temp = "0"; }
93                         convert(temp, size); 
94                                 
95                         if (abort == false) {
96                                 string fileNameRoot = getRootName(globaldata->inputFileName);
97                                 int i;
98                                 validCalculator = new ValidCalculators();
99                                 
100                                 for (i=0; i<Estimators.size(); i++) {
101                                         if (validCalculator->isValidCalculator("single", Estimators[i]) == true) { 
102                                                 if (Estimators[i] == "sobs") { 
103                                                         cDisplays.push_back(new CollectDisplay(new Sobs(), new OneColumnFile(fileNameRoot+"sobs")));
104                                                 }else if (Estimators[i] == "chao") { 
105                                                         cDisplays.push_back(new CollectDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"chao")));
106                                                 }else if (Estimators[i] == "nseqs") { 
107                                                         cDisplays.push_back(new CollectDisplay(new NSeqs(), new OneColumnFile(fileNameRoot+"nseqs")));
108                                                 }else if (Estimators[i] == "coverage") { 
109                                                         cDisplays.push_back(new CollectDisplay(new Coverage(), new OneColumnFile(fileNameRoot+"coverage")));
110                                                 }else if (Estimators[i] == "ace") { 
111                                                         cDisplays.push_back(new CollectDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"ace")));
112                                                 }else if (Estimators[i] == "jack") { 
113                                                         cDisplays.push_back(new CollectDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"jack")));
114                                                 }else if (Estimators[i] == "shannon") { 
115                                                         cDisplays.push_back(new CollectDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"shannon")));
116                                                 }else if (Estimators[i] == "npshannon") { 
117                                                         cDisplays.push_back(new CollectDisplay(new NPShannon(), new OneColumnFile(fileNameRoot+"np_shannon")));
118                                                 }else if (Estimators[i] == "simpson") { 
119                                                         cDisplays.push_back(new CollectDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"simpson")));
120                                                 }else if (Estimators[i] == "bootstrap") { 
121                                                         cDisplays.push_back(new CollectDisplay(new Bootstrap(), new OneColumnFile(fileNameRoot+"bootstrap")));
122                                                 }else if (Estimators[i] == "geometric") { 
123                                                         cDisplays.push_back(new CollectDisplay(new Geom(), new OneColumnFile(fileNameRoot+"geometric")));
124                                                 }else if (Estimators[i] == "qstat") { 
125                                                         cDisplays.push_back(new CollectDisplay(new QStat(), new OneColumnFile(fileNameRoot+"qstat")));
126                                                 }else if (Estimators[i] == "logseries") { 
127                                                         cDisplays.push_back(new CollectDisplay(new LogSD(), new OneColumnFile(fileNameRoot+"logseries")));
128                                                 }else if (Estimators[i] == "bergerparker") { 
129                                                         cDisplays.push_back(new CollectDisplay(new BergerParker(), new OneColumnFile(fileNameRoot+"bergerparker")));
130                                                 }else if (Estimators[i] == "bstick") { 
131                                                         cDisplays.push_back(new CollectDisplay(new BStick(), new ThreeColumnFile(fileNameRoot+"bstick")));
132                                                 }else if (Estimators[i] == "goodscoverage") { 
133                                                         cDisplays.push_back(new CollectDisplay(new GoodsCoverage(), new OneColumnFile(fileNameRoot+"goodscoverage")));
134                                                 }else if (Estimators[i] == "efron") {
135                                                         cDisplays.push_back(new CollectDisplay(new Efron(size), new OneColumnFile(fileNameRoot+"efron")));
136                                                 }else if (Estimators[i] == "boneh") {
137                                                         cDisplays.push_back(new CollectDisplay(new Boneh(size), new OneColumnFile(fileNameRoot+"boneh")));
138                                                 }else if (Estimators[i] == "solow") {
139                                                         cDisplays.push_back(new CollectDisplay(new Solow(size), new OneColumnFile(fileNameRoot+"solow")));
140                                                 }else if (Estimators[i] == "shen") {
141                                                         cDisplays.push_back(new CollectDisplay(new Shen(size, abund), new OneColumnFile(fileNameRoot+"shen")));
142                                                 }
143                                         }
144                                 }
145                         }
146                 }
147                 
148         }
149         catch(exception& e) {
150                 errorOut(e, "CollectCommand", "CollectCommand");
151                 exit(1);
152         }                       
153 }
154 //**********************************************************************************************************************
155
156 void CollectCommand::help(){
157         try {
158                 mothurOut("The collect.single command can only be executed after a successful read.otu command. WITH ONE EXECEPTION. \n");
159                 mothurOut("The collect.single command can be executed after a successful cluster command.  It will use the .list file from the output of the cluster.\n");
160                 mothurOut("The collect.single command parameters are label, freq, calc and abund.  No parameters are required. \n");
161                 mothurOut("The collect.single command should be in the following format: \n");
162                 mothurOut("collect.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n");
163                 mothurOut("Example collect(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-chao-ace-jack).\n");
164                 mothurOut("The default values for freq is 100, and calc are sobs-chao-ace-jack-shannon-npshannon-simpson.\n");
165                 validCalculator->printCalc("single", cout);
166                 mothurOut("The label parameter is used to analyze specific labels in your input.\n");
167                 mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
168         }
169         catch(exception& e) {
170                 errorOut(e, "CollectCommand", "help");
171                 exit(1);
172         }
173 }
174
175 //**********************************************************************************************************************
176
177 CollectCommand::~CollectCommand(){
178         if (abort == false) {
179                 //delete order;
180                 delete input;  globaldata->ginput = NULL;
181                 delete read;
182                 delete validCalculator;
183                 globaldata->gorder = NULL;
184         }
185 }
186
187 //**********************************************************************************************************************
188
189 int CollectCommand::execute(){
190         try {
191                 
192                 if (abort == true) { return 0; }
193                 
194                 //if the users entered no valid calculators don't execute command
195                 if (cDisplays.size() == 0) { return 0; }
196
197                 read = new ReadOTUFile(globaldata->inputFileName);      
198                 read->read(&*globaldata); 
199                 
200                 order = globaldata->gorder;
201                 string lastLabel = order->getLabel();
202                 input = globaldata->ginput;
203                 
204                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
205                 set<string> processedLabels;
206                 set<string> userLabels = labels;
207                 
208                 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
209                 
210                         if(allLines == 1 || labels.count(order->getLabel()) == 1){
211                                 
212                                 cCurve = new Collect(order, cDisplays);
213                                 cCurve->getCurve(freq);
214                                 delete cCurve;
215                         
216                                 mothurOut(order->getLabel()); mothurOutEndLine();
217                                 processedLabels.insert(order->getLabel());
218                                 userLabels.erase(order->getLabel());
219                         
220                         
221                         }
222                         //you have a label the user want that is smaller than this label and the last label has not already been processed 
223                         if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
224                                 string saveLabel = order->getLabel();
225                                 
226                                 delete order;
227                                 order = (input->getOrderVector(lastLabel));
228                                 
229                                 cCurve = new Collect(order, cDisplays);
230                                 cCurve->getCurve(freq);
231                                 delete cCurve;
232                         
233                                 mothurOut(order->getLabel()); mothurOutEndLine();
234                                 processedLabels.insert(order->getLabel());
235                                 userLabels.erase(order->getLabel());
236                                 
237                                 //restore real lastlabel to save below
238                                 order->setLabel(saveLabel);
239                         }
240                         
241                         lastLabel = order->getLabel();  
242                         
243                         delete order;           
244                         order = (input->getOrderVector());
245                 }
246                 
247                 //output error messages about any remaining user labels
248                 set<string>::iterator it;
249                 bool needToRun = false;
250                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
251                         mothurOut("Your file does not include the label " + *it); 
252                         if (processedLabels.count(lastLabel) != 1) {
253                                 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
254                                 needToRun = true;
255                         }else {
256                                 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
257                         }
258                 }
259                 
260                 //run last label if you need to
261                 if (needToRun == true)  {
262                         if (order != NULL) {    delete order;   }
263                         order = (input->getOrderVector(lastLabel));
264                         
265                         mothurOut(order->getLabel()); mothurOutEndLine();
266                         
267                         cCurve = new Collect(order, cDisplays);
268                         cCurve->getCurve(freq);
269                         delete cCurve;
270                         delete order;
271                 }
272                 
273                 
274                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
275                 return 0;
276         }
277         catch(exception& e) {
278                 errorOut(e, "CollectCommand", "execute");
279                 exit(1);
280         }
281 }
282
283 //**********************************************************************************************************************