2 * rarefactsharedcommand.cpp
5 * Created by Sarah Westcott on 1/6/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "rarefactsharedcommand.h"
11 #include "sharedsobs.h"
12 #include "sharednseqs.h"
14 //**********************************************************************************************************************
16 RareFactSharedCommand::RareFactSharedCommand(){
18 globaldata = GlobalData::getInstance();
20 fileNameRoot = getRootName(globaldata->inputFileName);
21 format = globaldata->getFormat();
22 convert(globaldata->getFreq(), freq);
23 convert(globaldata->getIters(), nIters);
24 validCalculator = new ValidCalculators();
27 for (i=0; i<globaldata->Estimators.size(); i++) {
28 if (validCalculator->isValidCalculator("sharedrarefaction", globaldata->Estimators[i]) == true) {
29 if (globaldata->Estimators[i] == "sharedobserved") {
30 rDisplays.push_back(new RareDisplay(new SharedSobs(), new SharedThreeColumnFile(fileNameRoot+"shared.rarefaction", "")));
31 }else if (globaldata->Estimators[i] == "sharednseqs") {
32 rDisplays.push_back(new RareDisplay(new SharedNSeqs(), new SharedThreeColumnFile(fileNameRoot+"shared.r_nseqs", "")));
38 //reset calc for next command
39 globaldata->setCalc("");
43 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function RareFactSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
47 cout << "An unknown error has occurred in the RareFactSharedCommand class function RareFactSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
53 //**********************************************************************************************************************
55 RareFactSharedCommand::~RareFactSharedCommand(){
61 //**********************************************************************************************************************
63 int RareFactSharedCommand::execute(){
67 //if the users entered no valid calculators don't execute command
68 if (rDisplays.size() == 0) { return 0; }
70 read = new ReadOTUFile(globaldata->inputFileName);
71 read->read(&*globaldata);
73 input = globaldata->ginput;
74 lookup = input->getSharedRAbundVectors();
75 vector<SharedRAbundVector*> lastLookup = lookup;
77 if (lookup.size() < 2) {
78 cout << "I cannot run the command without at least 2 valid groups.";
79 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
83 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
84 set<string> processedLabels;
85 set<string> userLabels = globaldata->labels;
87 //as long as you are not at the end of the file or done wih the lines you want
88 while((lookup[0] != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
90 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){
92 rCurve = new Rarefact(lookup, rDisplays);
93 rCurve->getSharedCurve(freq, nIters);
96 cout << lookup[0]->getLabel() << '\t' << count << endl;
97 processedLabels.insert(lookup[0]->getLabel());
98 userLabels.erase(lookup[0]->getLabel());
101 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
102 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
103 rCurve = new Rarefact(lastLookup, rDisplays);
104 rCurve->getSharedCurve(freq, nIters);
107 processedLabels.insert(lastLookup[0]->getLabel());
108 userLabels.erase(lastLookup[0]->getLabel());
111 //prevent memory leak
112 if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) { delete lastLookup[i]; } }
115 //get next line to process
116 lookup = input->getSharedRAbundVectors();
120 //output error messages about any remaining user labels
121 set<string>::iterator it;
122 bool needToRun = false;
123 for (it = userLabels.begin(); it != userLabels.end(); it++) {
124 cout << "Your file does not include the label "<< *it;
125 if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
126 cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
129 cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
133 //run last line if you need to
134 if (needToRun == true) {
135 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
136 rCurve = new Rarefact(lastLookup, rDisplays);
137 rCurve->getSharedCurve(freq, nIters);
141 for (int i = 0; i < lastLookup.size(); i++) { delete lastLookup[i]; }
143 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
145 //reset groups parameter
146 globaldata->Groups.clear(); globaldata->setGroups("");
150 catch(exception& e) {
151 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
155 cout << "An unknown error has occurred in the RareFactSharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
161 //**********************************************************************************************************************