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() == "")) { cout << "You must read a list, sabund or rabund before you can use the rarefaction.single command." << endl; 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 != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; 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 cout << "Standard Error: " << e.what() << " has occurred in the RareFactCommand class Function RareFactCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
141 cout << "An unknown error has occurred in the RareFactCommand class function RareFactCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
146 //**********************************************************************************************************************
148 void RareFactCommand::help(){
150 cout << "The rarefaction.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION." << "\n";
151 cout << "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";
152 cout << "The rarefaction.single command parameters are label, line, iters, freq, calc and abund. No parameters are required, but you may not use " << "\n";
153 cout << "both the line and label parameters at the same time. The rarefaction.single command should be in the following format: " << "\n";
154 cout << "rarefaction.single(label=yourLabel, line=yourLines, iters=yourIters, freq=yourFreq, calc=yourEstimators)." << "\n";
155 cout << "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";
156 cout << "The default values for iters is 1000, freq is 100, and calc is rarefaction which calculates the rarefaction curve for the observed richness." << "\n";
157 validCalculator->printCalc("rarefaction", cout);
158 cout << "The label and line parameters are used to analyze specific lines in your input." << "\n";
159 cout << "Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq)." << "\n" << "\n";
161 catch(exception& e) {
162 cout << "Standard Error: " << e.what() << " has occurred in the RareFactCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
166 cout << "An unknown error has occurred in the RareFactCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
171 //**********************************************************************************************************************
173 RareFactCommand::~RareFactCommand(){
178 delete validCalculator;
181 //**********************************************************************************************************************
183 int RareFactCommand::execute(){
186 if (abort == true) { return 0; }
190 //if the users entered no valid calculators don't execute command
191 if (rDisplays.size() == 0) { return 0; }
193 read = new ReadOTUFile(globaldata->inputFileName);
194 read->read(&*globaldata);
196 order = globaldata->gorder;
197 OrderVector* lastOrder = order;
198 input = globaldata->ginput;
200 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
201 set<string> processedLabels;
202 set<string> userLabels = labels;
203 set<int> userLines = lines;
205 //as long as you are not at the end of the file or done wih the lines you want
206 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
208 if(allLines == 1 || lines.count(count) == 1 || labels.count(order->getLabel()) == 1){
210 rCurve = new Rarefact(order, rDisplays);
211 rCurve->getCurve(freq, nIters);
214 cout << order->getLabel() << '\t' << count << endl;
215 processedLabels.insert(order->getLabel());
216 userLabels.erase(order->getLabel());
217 userLines.erase(count);
220 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastOrder->getLabel()) != 1)) {
221 rCurve = new Rarefact(lastOrder, rDisplays);
222 rCurve->getCurve(freq, nIters);
225 cout << lastOrder->getLabel() << '\t' << count << endl;
226 processedLabels.insert(lastOrder->getLabel());
227 userLabels.erase(lastOrder->getLabel());
230 if (count != 1) { delete lastOrder; }
233 order = (input->getOrderVector());
237 //output error messages about any remaining user labels
238 set<string>::iterator it;
239 bool needToRun = false;
240 for (it = userLabels.begin(); it != userLabels.end(); it++) {
241 cout << "Your file does not include the label "<< *it;
242 if (processedLabels.count(lastOrder->getLabel()) != 1) {
243 cout << ". I will use " << lastOrder->getLabel() << "." << endl;
246 cout << ". Please refer to " << lastOrder->getLabel() << "." << endl;
250 //run last line if you need to
251 if (needToRun == true) {
252 rCurve = new Rarefact(lastOrder, rDisplays);
253 rCurve->getCurve(freq, nIters);
256 cout << lastOrder->getLabel() << '\t' << count << endl;
261 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
264 catch(exception& e) {
265 cout << "Standard Error: " << e.what() << " has occurred in the RareFactCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
269 cout << "An unknown error has occurred in the RareFactCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
274 //**********************************************************************************************************************