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();
34 //allow user to run help
35 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
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)));
42 OptionParser parser(option);
43 map<string,string> parameters = parser.getParameters();
45 ValidParameters validParameter;
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; }
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; }
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 = ""; }
60 if(line != "all") { splitAtDash(line, lines); allLines = 0; }
61 else { allLines = 1; }
64 label = validParameter.validFile(parameters, "label", false);
65 if (label == "not found") { label = ""; }
67 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
68 else { allLines = 1; }
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;
80 calc = validParameter.validFile(parameters, "calc", false);
81 if (calc == "not found") { calc = "sobs"; }
83 if (calc == "default") { calc = "sobs"; }
85 splitAtDash(calc, Estimators);
88 temp = validParameter.validFile(parameters, "freq", false); if (temp == "not found") { temp = "100"; }
91 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
94 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; }
95 convert(temp, nIters);
99 string fileNameRoot = getRootName(globaldata->inputFileName);
101 validCalculator = new ValidCalculators();
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") {
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")));
136 catch(exception& e) {
137 errorOut(e, "RareFactCommand", "RareFactCommand");
141 //**********************************************************************************************************************
143 void RareFactCommand::help(){
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");
156 catch(exception& e) {
157 errorOut(e, "RareFactCommand", "help");
162 //**********************************************************************************************************************
164 RareFactCommand::~RareFactCommand(){
165 if (abort == false) {
166 globaldata->gorder = NULL;
167 delete input; globaldata->ginput = NULL;
169 delete validCalculator;
173 //**********************************************************************************************************************
175 int RareFactCommand::execute(){
178 if (abort == true) { return 0; }
182 //if the users entered no valid calculators don't execute command
183 if (rDisplays.size() == 0) { return 0; }
185 read = new ReadOTUFile(globaldata->inputFileName);
186 read->read(&*globaldata);
188 order = globaldata->gorder;
189 string lastLabel = order->getLabel();
190 input = globaldata->ginput;
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;
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))) {
200 if(allLines == 1 || lines.count(count) == 1 || labels.count(order->getLabel()) == 1){
202 rCurve = new Rarefact(order, rDisplays);
203 rCurve->getCurve(freq, nIters);
206 mothurOut(order->getLabel() + "\t" + toString(count)); mothurOutEndLine();
207 processedLabels.insert(order->getLabel());
208 userLabels.erase(order->getLabel());
209 userLines.erase(count);
212 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
214 order = (input->getOrderVector(lastLabel));
216 rCurve = new Rarefact(order, rDisplays);
217 rCurve->getCurve(freq, nIters);
220 mothurOut(order->getLabel() + "\t" + toString(count)); mothurOutEndLine();
221 processedLabels.insert(order->getLabel());
222 userLabels.erase(order->getLabel());
225 lastLabel = order->getLabel();
228 order = (input->getOrderVector());
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();
241 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
245 //run last line if you need to
246 if (needToRun == true) {
248 order = (input->getOrderVector(lastLabel));
250 rCurve = new Rarefact(order, rDisplays);
251 rCurve->getCurve(freq, nIters);
254 mothurOut(order->getLabel() + "\t" + toString(count)); mothurOutEndLine();
259 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
262 catch(exception& e) {
263 errorOut(e, "RareFactCommand", "execute");
268 //**********************************************************************************************************************