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(){
27 globaldata = GlobalData::getInstance();
29 fileNameRoot = getRootName(globaldata->inputFileName);
30 convert(globaldata->getFreq(), freq);
31 convert(globaldata->getIters(), nIters);
32 validCalculator = new ValidCalculators();
35 for (i=0; i<globaldata->Estimators.size(); i++) {
36 if (validCalculator->isValidCalculator("rarefaction", globaldata->Estimators[i]) == true) {
37 if (globaldata->Estimators[i] == "sobs") {
38 rDisplays.push_back(new RareDisplay(new Sobs(), new ThreeColumnFile(fileNameRoot+"rarefaction")));
39 }else if (globaldata->Estimators[i] == "chao") {
40 rDisplays.push_back(new RareDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"r_chao")));
41 }else if (globaldata->Estimators[i] == "ace") {
42 convert(globaldata->getAbund(), abund);
45 rDisplays.push_back(new RareDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"r_ace")));
46 }else if (globaldata->Estimators[i] == "jack") {
47 rDisplays.push_back(new RareDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"r_jack")));
48 }else if (globaldata->Estimators[i] == "shannon") {
49 rDisplays.push_back(new RareDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"r_shannon")));
50 }else if (globaldata->Estimators[i] == "npshannon") {
51 rDisplays.push_back(new RareDisplay(new NPShannon(), new ThreeColumnFile(fileNameRoot+"r_npshannon")));
52 }else if (globaldata->Estimators[i] == "simpson") {
53 rDisplays.push_back(new RareDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"r_simpson")));
54 }else if (globaldata->Estimators[i] == "bootstrap") {
55 rDisplays.push_back(new RareDisplay(new Bootstrap(), new ThreeColumnFile(fileNameRoot+"r_bootstrap")));
56 }else if (globaldata->Estimators[i] == "coverage") {
57 rDisplays.push_back(new RareDisplay(new Coverage(), new ThreeColumnFile(fileNameRoot+"r_coverage")));
58 }else if (globaldata->Estimators[i] == "nseqs") {
59 rDisplays.push_back(new RareDisplay(new NSeqs(), new ThreeColumnFile(fileNameRoot+"r_nseqs")));
64 //reset calc for next command
65 globaldata->setCalc("");
69 cout << "Standard Error: " << e.what() << " has occurred in the RareFactCommand class Function RareFactCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
73 cout << "An unknown error has occurred in the RareFactCommand class function RareFactCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
79 //**********************************************************************************************************************
81 RareFactCommand::~RareFactCommand(){
88 //**********************************************************************************************************************
90 int RareFactCommand::execute(){
94 //if the users entered no valid calculators don't execute command
95 if (rDisplays.size() == 0) { return 0; }
97 read = new ReadOTUFile(globaldata->inputFileName);
98 read->read(&*globaldata);
100 order = globaldata->gorder;
101 OrderVector* lastOrder = order;
102 input = globaldata->ginput;
104 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
105 set<string> processedLabels;
106 set<string> userLabels = globaldata->labels;
107 set<int> userLines = globaldata->lines;
109 //as long as you are not at the end of the file or done wih the lines you want
110 while((order != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
112 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){
114 rCurve = new Rarefact(order, rDisplays);
115 rCurve->getCurve(freq, nIters);
118 cout << order->getLabel() << '\t' << count << endl;
119 processedLabels.insert(order->getLabel());
120 userLabels.erase(order->getLabel());
121 userLines.erase(count);
124 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastOrder->getLabel()) != 1)) {
125 rCurve = new Rarefact(lastOrder, rDisplays);
126 rCurve->getCurve(freq, nIters);
129 cout << lastOrder->getLabel() << '\t' << count << endl;
130 processedLabels.insert(lastOrder->getLabel());
131 userLabels.erase(lastOrder->getLabel());
134 if (count != 1) { delete lastOrder; }
137 order = (input->getOrderVector());
141 //output error messages about any remaining user labels
142 set<string>::iterator it;
143 bool needToRun = false;
144 for (it = userLabels.begin(); it != userLabels.end(); it++) {
145 cout << "Your file does not include the label "<< *it;
146 if (processedLabels.count(lastOrder->getLabel()) != 1) {
147 cout << ". I will use " << lastOrder->getLabel() << "." << endl;
150 cout << ". Please refer to " << lastOrder->getLabel() << "." << endl;
154 //run last line if you need to
155 if (needToRun == true) {
156 rCurve = new Rarefact(lastOrder, rDisplays);
157 rCurve->getCurve(freq, nIters);
160 cout << lastOrder->getLabel() << '\t' << count << endl;
165 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
168 catch(exception& e) {
169 cout << "Standard Error: " << e.what() << " has occurred in the RareFactCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
173 cout << "An unknown error has occurred in the RareFactCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
178 //**********************************************************************************************************************