5 * Created by Sarah Westcott on 1/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "rarefactcommand.h"
15 #include "bootstrap.h"
17 #include "npshannon.h"
19 #include "jackknife.h"
22 //**********************************************************************************************************************
25 RareFactCommand::RareFactCommand(string option){
27 globaldata = GlobalData::getInstance();
33 //allow user to run help
34 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
37 //valid paramters for this command
38 string Array[] = {"iters","freq","label","calc","abund"};
39 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
41 OptionParser parser(option);
42 map<string,string> parameters = parser.getParameters();
44 ValidParameters validParameter;
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; }
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; }
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 = ""; }
59 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
60 else { allLines = 1; }
63 //if the user has not specified any labels use the ones from read.otu
65 allLines = globaldata->allLines;
66 labels = globaldata->labels;
69 calc = validParameter.validFile(parameters, "calc", false);
70 if (calc == "not found") { calc = "sobs"; }
72 if (calc == "default") { calc = "sobs"; }
74 splitAtDash(calc, Estimators);
77 temp = validParameter.validFile(parameters, "freq", false); if (temp == "not found") { temp = "100"; }
80 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
83 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; }
84 convert(temp, nIters);
88 string fileNameRoot = getRootName(globaldata->inputFileName);
90 validCalculator = new ValidCalculators();
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") {
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")));
125 catch(exception& e) {
126 errorOut(e, "RareFactCommand", "RareFactCommand");
130 //**********************************************************************************************************************
132 void RareFactCommand::help(){
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");
145 catch(exception& e) {
146 errorOut(e, "RareFactCommand", "help");
151 //**********************************************************************************************************************
153 RareFactCommand::~RareFactCommand(){
154 if (abort == false) {
155 globaldata->gorder = NULL;
156 delete input; globaldata->ginput = NULL;
158 delete validCalculator;
162 //**********************************************************************************************************************
164 int RareFactCommand::execute(){
167 if (abort == true) { return 0; }
169 //if the users entered no valid calculators don't execute command
170 if (rDisplays.size() == 0) { return 0; }
172 read = new ReadOTUFile(globaldata->inputFileName);
173 read->read(&*globaldata);
175 order = globaldata->gorder;
176 string lastLabel = order->getLabel();
177 input = globaldata->ginput;
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;
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))) {
186 if(allLines == 1 || labels.count(order->getLabel()) == 1){
188 rCurve = new Rarefact(order, rDisplays);
189 rCurve->getCurve(freq, nIters);
192 mothurOut(order->getLabel()); mothurOutEndLine();
193 processedLabels.insert(order->getLabel());
194 userLabels.erase(order->getLabel());
197 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
198 string saveLabel = order->getLabel();
201 order = (input->getOrderVector(lastLabel));
203 rCurve = new Rarefact(order, rDisplays);
204 rCurve->getCurve(freq, nIters);
207 mothurOut(order->getLabel()); mothurOutEndLine();
208 processedLabels.insert(order->getLabel());
209 userLabels.erase(order->getLabel());
211 //restore real lastlabel to save below
212 order->setLabel(saveLabel);
215 lastLabel = order->getLabel();
218 order = (input->getOrderVector());
221 //output error messages about any remaining user labels
222 set<string>::iterator it;
223 bool needToRun = false;
224 for (it = userLabels.begin(); it != userLabels.end(); it++) {
225 mothurOut("Your file does not include the label " + *it);
226 if (processedLabels.count(lastLabel) != 1) {
227 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
230 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
234 //run last label if you need to
235 if (needToRun == true) {
236 if (order != NULL) { delete order; }
237 order = (input->getOrderVector(lastLabel));
239 rCurve = new Rarefact(order, rDisplays);
240 rCurve->getCurve(freq, nIters);
243 mothurOut(order->getLabel()); mothurOutEndLine();
248 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
251 catch(exception& e) {
252 errorOut(e, "RareFactCommand", "execute");
257 //**********************************************************************************************************************