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