]> git.donarmstrong.com Git - mothur.git/blob - rarefactcommand.cpp
bugs fixes while testing for 1.5 release
[mothur.git] / rarefactcommand.cpp
1 /*
2  *  rarefactcommand.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 "rarefactcommand.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 "coverage.h"
21
22 //**********************************************************************************************************************
23
24
25 RareFactCommand::RareFactCommand(string option){
26         try {
27                 globaldata = GlobalData::getInstance();
28                 abort = false;
29                 allLines = 1;
30                 lines.clear();
31                 labels.clear();
32                 Estimators.clear();
33                 
34                 //allow user to run help
35                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
36                 
37                 else {
38                         //valid paramters for this command
39                         string Array[] =  {"iters","freq","line","label","calc","abund"};
40                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
41                         
42                         OptionParser parser(option);
43                         map<string,string> parameters = parser.getParameters();
44                         
45                         ValidParameters validParameter;
46                 
47                         //check to make sure all parameters are valid for command
48                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
49                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
50                         }
51                         
52                         //make sure the user has already run the read.otu command
53                         if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { mothurOut("You must read a list, sabund or rabund before you can use the rarefaction.single command."); mothurOutEndLine(); abort = true; }
54                         
55                         //check for optional parameter and set defaults
56                         // ...at some point should added some additional type checking...
57                         line = validParameter.validFile(parameters, "line", false);                             
58                         if (line == "not found") { line = "";  }
59                         else { 
60                                 if(line != "all") {  splitAtDash(line, lines);  allLines = 0;  }
61                                 else { allLines = 1;  }
62                         }
63                         
64                         label = validParameter.validFile(parameters, "label", false);                   
65                         if (label == "not found") { label = ""; }
66                         else { 
67                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
68                                 else { allLines = 1;  }
69                         }
70                         
71                         //make sure user did not use both the line and label parameters
72                         if ((line != "") && (label != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); abort = true; }
73                         //if the user has not specified any line or labels use the ones from read.otu
74                         else if((line == "") && (label == "")) {  
75                                 allLines = globaldata->allLines; 
76                                 labels = globaldata->labels; 
77                                 lines = globaldata->lines;
78                         }
79                                 
80                         calc = validParameter.validFile(parameters, "calc", false);                     
81                         if (calc == "not found") { calc = "sobs";  }
82                         else { 
83                                  if (calc == "default")  {  calc = "sobs";  }
84                         }
85                         splitAtDash(calc, Estimators);
86
87                         string temp;
88                         temp = validParameter.validFile(parameters, "freq", false);                     if (temp == "not found") { temp = "100"; }
89                         convert(temp, freq); 
90                         
91                         temp = validParameter.validFile(parameters, "abund", false);                    if (temp == "not found") { temp = "10"; }
92                         convert(temp, abund); 
93                         
94                         temp = validParameter.validFile(parameters, "iters", false);                    if (temp == "not found") { temp = "1000"; }
95                         convert(temp, nIters); 
96                         
97                         if (abort == false) {
98                         
99                                 string fileNameRoot = getRootName(globaldata->inputFileName);
100                                 int i;
101                                 validCalculator = new ValidCalculators();
102                                 
103                                 
104                                 for (i=0; i<Estimators.size(); i++) {
105                                         if (validCalculator->isValidCalculator("rarefaction", Estimators[i]) == true) { 
106                                                 if (Estimators[i] == "sobs") { 
107                                                         rDisplays.push_back(new RareDisplay(new Sobs(), new ThreeColumnFile(fileNameRoot+"rarefaction")));
108                                                 }else if (Estimators[i] == "chao") { 
109                                                         rDisplays.push_back(new RareDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"r_chao")));
110                                                 }else if (Estimators[i] == "ace") { 
111                                                         if(abund < 5)
112                                                                 abund = 10;
113                                                         rDisplays.push_back(new RareDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"r_ace")));
114                                                 }else if (Estimators[i] == "jack") { 
115                                                         rDisplays.push_back(new RareDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"r_jack")));
116                                                 }else if (Estimators[i] == "shannon") { 
117                                                         rDisplays.push_back(new RareDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"r_shannon")));
118                                                 }else if (Estimators[i] == "npshannon") { 
119                                                         rDisplays.push_back(new RareDisplay(new NPShannon(), new ThreeColumnFile(fileNameRoot+"r_npshannon")));
120                                                 }else if (Estimators[i] == "simpson") { 
121                                                         rDisplays.push_back(new RareDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"r_simpson")));
122                                                 }else if (Estimators[i] == "bootstrap") { 
123                                                         rDisplays.push_back(new RareDisplay(new Bootstrap(), new ThreeColumnFile(fileNameRoot+"r_bootstrap")));
124                                                 }else if (Estimators[i] == "coverage") { 
125                                                         rDisplays.push_back(new RareDisplay(new Coverage(), new ThreeColumnFile(fileNameRoot+"r_coverage")));
126                                                 }else if (Estimators[i] == "nseqs") { 
127                                                         rDisplays.push_back(new RareDisplay(new NSeqs(), new ThreeColumnFile(fileNameRoot+"r_nseqs")));
128                                                 }
129                                         }
130                                 }
131                         }
132                                 
133                 }
134                 
135         }
136         catch(exception& e) {
137                 errorOut(e, "RareFactCommand", "RareFactCommand");
138                 exit(1);
139         }
140 }
141 //**********************************************************************************************************************
142
143 void RareFactCommand::help(){
144         try {
145                 mothurOut("The rarefaction.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION.\n");
146                 mothurOut("The rarefaction.single command can be executed after a successful cluster command.  It will use the .list file from the output of the cluster.\n");
147                 mothurOut("The rarefaction.single command parameters are label, line, iters, freq, calc and abund.  No parameters are required, but you may not use \n");
148                 mothurOut("both the line and label parameters at the same time. The rarefaction.single command should be in the following format: \n");
149                 mothurOut("rarefaction.single(label=yourLabel, line=yourLines, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n");
150                 mothurOut("Example rarefaction.single(label=unique-.01-.03, line=0-5-10, iters=10000, freq=10, calc=sobs-rchao-race-rjack-rbootstrap-rshannon-rnpshannon-rsimpson).\n");
151                 mothurOut("The default values for iters is 1000, freq is 100, and calc is rarefaction which calculates the rarefaction curve for the observed richness.\n");
152                 validCalculator->printCalc("rarefaction", cout);
153                 mothurOut("The label and line parameters are used to analyze specific lines in your input.\n");
154                 mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
155         }
156         catch(exception& e) {
157                 errorOut(e, "RareFactCommand", "help");
158                 exit(1);
159         }
160 }
161
162 //**********************************************************************************************************************
163
164 RareFactCommand::~RareFactCommand(){
165         if (abort == false) {
166                 globaldata->gorder = NULL;
167                 delete input;  globaldata->ginput = NULL;
168                 delete read;
169                 delete validCalculator;
170         }
171 }
172
173 //**********************************************************************************************************************
174
175 int RareFactCommand::execute(){
176         try {
177         
178                 if (abort == true) { return 0; }
179                 
180                 int count = 1;
181                 
182                 //if the users entered no valid calculators don't execute command
183                 if (rDisplays.size() == 0) { return 0; }
184
185                 read = new ReadOTUFile(globaldata->inputFileName);      
186                 read->read(&*globaldata); 
187
188                 order = globaldata->gorder;
189                 string lastLabel = order->getLabel();
190                 input = globaldata->ginput;
191                 
192                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
193                 set<string> processedLabels;
194                 set<string> userLabels = labels;
195                 set<int> userLines = lines;
196         
197                 //as long as you are not at the end of the file or done wih the lines you want
198                 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
199                         
200                         if(allLines == 1 || lines.count(count) == 1 || labels.count(order->getLabel()) == 1){
201                         
202                                 rCurve = new Rarefact(order, rDisplays);
203                                 rCurve->getCurve(freq, nIters);
204                                 delete rCurve;
205                         
206                                 mothurOut(order->getLabel()); mothurOutEndLine();
207                                 processedLabels.insert(order->getLabel());
208                                 userLabels.erase(order->getLabel());
209                                 userLines.erase(count);
210                         }
211                         
212                         if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
213                                 delete order;
214                                 order = (input->getOrderVector(lastLabel));
215                                 
216                                 rCurve = new Rarefact(order, rDisplays);
217                                 rCurve->getCurve(freq, nIters);
218                                 delete rCurve;
219                         
220                                 mothurOut(order->getLabel()); mothurOutEndLine();
221                                 processedLabels.insert(order->getLabel());
222                                 userLabels.erase(order->getLabel());
223                         }
224                         
225                         lastLabel = order->getLabel();          
226                         
227                         delete order;
228                         order = (input->getOrderVector());
229                         count++;
230                 }
231                 
232                 //output error messages about any remaining user labels
233                 set<string>::iterator it;
234                 bool needToRun = false;
235                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
236                         mothurOut("Your file does not include the label " + *it);
237                         if (processedLabels.count(lastLabel) != 1) {
238                                 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
239                                 needToRun = true;
240                         }else {
241                                 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
242                         }
243                 }
244                 
245                 //run last line if you need to
246                 if (needToRun == true)  {
247                         delete order;
248                         order = (input->getOrderVector(lastLabel));
249                                 
250                         rCurve = new Rarefact(order, rDisplays);
251                         rCurve->getCurve(freq, nIters);
252                         delete rCurve;
253                         
254                         mothurOut(order->getLabel()); mothurOutEndLine();
255                         delete order;
256                 }
257                 
258
259                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }       
260                 return 0;
261         }
262         catch(exception& e) {
263                 errorOut(e, "RareFactCommand", "execute");
264                 exit(1);
265         }
266 }
267
268 //**********************************************************************************************************************