5 * Created by Sarah Westcott on 1/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "collectcommand.h"
15 #include "bootstrap.h"
17 #include "npshannon.h"
19 #include "jackknife.h"
23 #include "bergerparker.h"
25 #include "goodscoverage.h"
33 //**********************************************************************************************************************
34 CollectCommand::CollectCommand(string option){
36 globaldata = GlobalData::getInstance();
43 //allow user to run help
44 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
47 //valid paramters for this command
48 string Array[] = {"freq","line","label","calc","abund","size"};
49 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
51 OptionParser parser(option);
52 map<string,string> parameters = parser.getParameters();
54 ValidParameters validParameter;
56 //check to make sure all parameters are valid for command
57 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
58 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
61 //make sure the user has already run the read.otu command
62 if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { cout << "You must read a list, sabund or rabund before you can use the collect.single command." << endl; abort = true; }
64 //check for optional parameter and set defaults
65 // ...at some point should added some additional type checking...
66 line = validParameter.validFile(parameters, "line", false);
67 if (line == "not found") { line = ""; }
69 if(line != "all") { splitAtDash(line, lines); allLines = 0; }
70 else { allLines = 1; }
73 label = validParameter.validFile(parameters, "label", false);
74 if (label == "not found") { label = ""; }
76 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
77 else { allLines = 1; }
80 //make sure user did not use both the line and label parameters
81 if ((line != "") && (label != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; abort = true; }
82 //if the user has not specified any line or labels use the ones from read.otu
83 else if((line == "") && (label == "")) {
84 allLines = globaldata->allLines;
85 labels = globaldata->labels;
86 lines = globaldata->lines;
89 calc = validParameter.validFile(parameters, "calc", false);
90 if (calc == "not found") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
92 if (calc == "default") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
94 splitAtDash(calc, Estimators);
97 temp = validParameter.validFile(parameters, "freq", false); if (temp == "not found") { temp = "100"; }
100 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
101 convert(temp, abund);
103 temp = validParameter.validFile(parameters, "size", false); if (temp == "not found") { temp = "0"; }
106 if (abort == false) {
107 string fileNameRoot = getRootName(globaldata->inputFileName);
109 validCalculator = new ValidCalculators();
111 for (i=0; i<Estimators.size(); i++) {
112 if (validCalculator->isValidCalculator("single", Estimators[i]) == true) {
113 if (Estimators[i] == "sobs") {
114 cDisplays.push_back(new CollectDisplay(new Sobs(), new OneColumnFile(fileNameRoot+"sobs")));
115 }else if (Estimators[i] == "chao") {
116 cDisplays.push_back(new CollectDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"chao")));
117 }else if (Estimators[i] == "nseqs") {
118 cDisplays.push_back(new CollectDisplay(new NSeqs(), new OneColumnFile(fileNameRoot+"nseqs")));
119 }else if (Estimators[i] == "coverage") {
120 cDisplays.push_back(new CollectDisplay(new Coverage(), new OneColumnFile(fileNameRoot+"coverage")));
121 }else if (Estimators[i] == "ace") {
122 cDisplays.push_back(new CollectDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"ace")));
123 }else if (Estimators[i] == "jack") {
124 cDisplays.push_back(new CollectDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"jack")));
125 }else if (Estimators[i] == "shannon") {
126 cDisplays.push_back(new CollectDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"shannon")));
127 }else if (Estimators[i] == "npshannon") {
128 cDisplays.push_back(new CollectDisplay(new NPShannon(), new OneColumnFile(fileNameRoot+"np_shannon")));
129 }else if (Estimators[i] == "simpson") {
130 cDisplays.push_back(new CollectDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"simpson")));
131 }else if (Estimators[i] == "bootstrap") {
132 cDisplays.push_back(new CollectDisplay(new Bootstrap(), new OneColumnFile(fileNameRoot+"bootstrap")));
133 }else if (Estimators[i] == "geometric") {
134 cDisplays.push_back(new CollectDisplay(new Geom(), new OneColumnFile(fileNameRoot+"geometric")));
135 }else if (Estimators[i] == "qstat") {
136 cDisplays.push_back(new CollectDisplay(new QStat(), new OneColumnFile(fileNameRoot+"qstat")));
137 }else if (Estimators[i] == "logseries") {
138 cDisplays.push_back(new CollectDisplay(new LogSD(), new OneColumnFile(fileNameRoot+"logseries")));
139 }else if (Estimators[i] == "bergerparker") {
140 cDisplays.push_back(new CollectDisplay(new BergerParker(), new OneColumnFile(fileNameRoot+"bergerparker")));
141 }else if (Estimators[i] == "bstick") {
142 cDisplays.push_back(new CollectDisplay(new BStick(), new ThreeColumnFile(fileNameRoot+"bstick")));
143 }else if (Estimators[i] == "goodscoverage") {
144 cDisplays.push_back(new CollectDisplay(new GoodsCoverage(), new OneColumnFile(fileNameRoot+"goodscoverage")));
145 }else if (Estimators[i] == "efron") {
146 cDisplays.push_back(new CollectDisplay(new Efron(size), new OneColumnFile(fileNameRoot+"efron")));
147 }else if (Estimators[i] == "boneh") {
148 cDisplays.push_back(new CollectDisplay(new Boneh(size), new OneColumnFile(fileNameRoot+"boneh")));
149 }else if (Estimators[i] == "solow") {
150 cDisplays.push_back(new CollectDisplay(new Solow(size), new OneColumnFile(fileNameRoot+"solow")));
151 }else if (Estimators[i] == "shen") {
152 cDisplays.push_back(new CollectDisplay(new Shen(size, abund), new OneColumnFile(fileNameRoot+"shen")));
160 catch(exception& e) {
161 cout << "Standard Error: " << e.what() << " has occurred in the CollectCommand class Function CollectCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
165 cout << "An unknown error has occurred in the CollectCommand class function CollectCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
170 //**********************************************************************************************************************
172 void CollectCommand::help(){
174 cout << "The collect.single command can only be executed after a successful read.otu command. WITH ONE EXECEPTION. " << "\n";
175 cout << "The collect.single command can be executed after a successful cluster command. It will use the .list file from the output of the cluster." << "\n";
176 cout << "The collect.single command parameters are label, line, freq, calc and abund. No parameters are required, but you may not use " << "\n";
177 cout << "both the line and label parameters at the same time. The collect.single command should be in the following format: " << "\n";
178 cout << "collect.single(label=yourLabel, line=yourLines, iters=yourIters, freq=yourFreq, calc=yourEstimators)." << "\n";
179 cout << "Example collect(label=unique-.01-.03, line=0-5-10, iters=10000, freq=10, calc=sobs-chao-ace-jack)." << "\n";
180 cout << "The default values for freq is 100, and calc are sobs-chao-ace-jack-shannon-npshannon-simpson." << "\n";
181 validCalculator->printCalc("single", cout);
182 cout << "The label and line parameters are used to analyze specific lines in your input." << "\n";
183 cout << "Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq)." << "\n" << "\n";
185 catch(exception& e) {
186 cout << "Standard Error: " << e.what() << " has occurred in the CollectCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
190 cout << "An unknown error has occurred in the CollectCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
195 //**********************************************************************************************************************
197 CollectCommand::~CollectCommand(){
198 if (abort == false) {
200 delete input; globaldata->ginput = NULL;
202 delete validCalculator;
203 globaldata->gorder = NULL;
207 //**********************************************************************************************************************
209 int CollectCommand::execute(){
212 if (abort == true) { return 0; }
216 //if the users entered no valid calculators don't execute command
217 if (cDisplays.size() == 0) { return 0; }
219 read = new ReadOTUFile(globaldata->inputFileName);
220 read->read(&*globaldata);
222 order = globaldata->gorder;
223 string lastLabel = order->getLabel();
224 input = globaldata->ginput;
226 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
227 set<string> processedLabels;
228 set<string> userLabels = labels;
229 set<int> userLines = lines;
231 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
233 if(allLines == 1 || lines.count(count) == 1 || labels.count(order->getLabel()) == 1){
235 cCurve = new Collect(order, cDisplays);
236 cCurve->getCurve(freq);
239 cout << order->getLabel() << '\t' << count << endl;
240 processedLabels.insert(order->getLabel());
241 userLabels.erase(order->getLabel());
242 userLines.erase(count);
244 //you have a label the user want that is smaller than this line and the last line has not already been processed
247 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
250 order = (input->getOrderVector(lastLabel));
252 cCurve = new Collect(order, cDisplays);
253 cCurve->getCurve(freq);
256 cout << order->getLabel() << '\t' << count << endl;
257 processedLabels.insert(order->getLabel());
258 userLabels.erase(order->getLabel());
261 lastLabel = order->getLabel();
264 order = (input->getOrderVector());
268 //output error messages about any remaining user labels
269 set<string>::iterator it;
270 bool needToRun = false;
271 for (it = userLabels.begin(); it != userLabels.end(); it++) {
272 cout << "Your file does not include the label "<< *it;
273 if (processedLabels.count(lastLabel) != 1) {
274 cout << ". I will use " << lastLabel << "." << endl;
277 cout << ". Please refer to " << lastLabel << "." << endl;
281 //run last line if you need to
282 if (needToRun == true) {
284 order = (input->getOrderVector(lastLabel));
286 cout << order->getLabel() << '\t' << count << endl;
288 cCurve = new Collect(order, cDisplays);
289 cCurve->getCurve(freq);
295 for(int i=0;i<cDisplays.size();i++){ delete cDisplays[i]; }
298 catch(exception& e) {
299 cout << "Standard Error: " << e.what() << " has occurred in the CollectCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
303 cout << "An unknown error has occurred in the CollectCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
308 //**********************************************************************************************************************