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 parser = new OptionParser();
52 parser->parse(option, parameters); delete parser;
54 ValidParameters* validParameter = new ValidParameters();
56 //check to make sure all parameters are valid for command
57 for (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 delete validParameter;
109 if (abort == false) {
110 string fileNameRoot = getRootName(globaldata->inputFileName);
112 validCalculator = new ValidCalculators();
114 for (i=0; i<Estimators.size(); i++) {
115 if (validCalculator->isValidCalculator("single", Estimators[i]) == true) {
116 if (Estimators[i] == "sobs") {
117 cDisplays.push_back(new CollectDisplay(new Sobs(), new OneColumnFile(fileNameRoot+"sobs")));
118 }else if (Estimators[i] == "chao") {
119 cDisplays.push_back(new CollectDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"chao")));
120 }else if (Estimators[i] == "nseqs") {
121 cDisplays.push_back(new CollectDisplay(new NSeqs(), new OneColumnFile(fileNameRoot+"nseqs")));
122 }else if (Estimators[i] == "coverage") {
123 cDisplays.push_back(new CollectDisplay(new Coverage(), new OneColumnFile(fileNameRoot+"coverage")));
124 }else if (Estimators[i] == "ace") {
125 cDisplays.push_back(new CollectDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"ace")));
126 }else if (Estimators[i] == "jack") {
127 cDisplays.push_back(new CollectDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"jack")));
128 }else if (Estimators[i] == "shannon") {
129 cDisplays.push_back(new CollectDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"shannon")));
130 }else if (Estimators[i] == "npshannon") {
131 cDisplays.push_back(new CollectDisplay(new NPShannon(), new OneColumnFile(fileNameRoot+"np_shannon")));
132 }else if (Estimators[i] == "simpson") {
133 cDisplays.push_back(new CollectDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"simpson")));
134 }else if (Estimators[i] == "bootstrap") {
135 cDisplays.push_back(new CollectDisplay(new Bootstrap(), new OneColumnFile(fileNameRoot+"bootstrap")));
136 }else if (Estimators[i] == "geometric") {
137 cDisplays.push_back(new CollectDisplay(new Geom(), new OneColumnFile(fileNameRoot+"geometric")));
138 }else if (Estimators[i] == "qstat") {
139 cDisplays.push_back(new CollectDisplay(new QStat(), new OneColumnFile(fileNameRoot+"qstat")));
140 }else if (Estimators[i] == "logseries") {
141 cDisplays.push_back(new CollectDisplay(new LogSD(), new OneColumnFile(fileNameRoot+"logseries")));
142 }else if (Estimators[i] == "bergerparker") {
143 cDisplays.push_back(new CollectDisplay(new BergerParker(), new OneColumnFile(fileNameRoot+"bergerparker")));
144 }else if (Estimators[i] == "bstick") {
145 cDisplays.push_back(new CollectDisplay(new BStick(), new ThreeColumnFile(fileNameRoot+"bstick")));
146 }else if (Estimators[i] == "goodscoverage") {
147 cDisplays.push_back(new CollectDisplay(new GoodsCoverage(), new OneColumnFile(fileNameRoot+"goodscoverage")));
148 }else if (Estimators[i] == "efron") {
149 cDisplays.push_back(new CollectDisplay(new Efron(size), new OneColumnFile(fileNameRoot+"efron")));
150 }else if (Estimators[i] == "boneh") {
151 cDisplays.push_back(new CollectDisplay(new Boneh(size), new OneColumnFile(fileNameRoot+"boneh")));
152 }else if (Estimators[i] == "solow") {
153 cDisplays.push_back(new CollectDisplay(new Solow(size), new OneColumnFile(fileNameRoot+"solow")));
154 }else if (Estimators[i] == "shen") {
155 cDisplays.push_back(new CollectDisplay(new Shen(size, abund), new OneColumnFile(fileNameRoot+"shen")));
163 catch(exception& e) {
164 cout << "Standard Error: " << e.what() << " has occurred in the CollectCommand class Function CollectCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
168 cout << "An unknown error has occurred in the CollectCommand class function CollectCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
173 //**********************************************************************************************************************
175 void CollectCommand::help(){
177 cout << "The collect.single command can only be executed after a successful read.otu command. WITH ONE EXECEPTION. " << "\n";
178 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";
179 cout << "The collect.single command parameters are label, line, freq, calc and abund. No parameters are required, but you may not use " << "\n";
180 cout << "both the line and label parameters at the same time. The collect.single command should be in the following format: " << "\n";
181 cout << "collect.single(label=yourLabel, line=yourLines, iters=yourIters, freq=yourFreq, calc=yourEstimators)." << "\n";
182 cout << "Example collect(label=unique-.01-.03, line=0-5-10, iters=10000, freq=10, calc=sobs-chao-ace-jack)." << "\n";
183 cout << "The default values for freq is 100, and calc are sobs-chao-ace-jack-shannon-npshannon-simpson." << "\n";
184 validCalculator->printCalc("single", cout);
185 cout << "The label and line parameters are used to analyze specific lines in your input." << "\n";
186 cout << "Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq)." << "\n" << "\n";
188 catch(exception& e) {
189 cout << "Standard Error: " << e.what() << " has occurred in the CollectCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
193 cout << "An unknown error has occurred in the CollectCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
198 //**********************************************************************************************************************
200 CollectCommand::~CollectCommand(){
205 delete validCalculator;
208 //**********************************************************************************************************************
210 int CollectCommand::execute(){
213 if (abort == true) { return 0; }
217 //if the users entered no valid calculators don't execute command
218 if (cDisplays.size() == 0) { return 0; }
220 read = new ReadOTUFile(globaldata->inputFileName);
221 read->read(&*globaldata);
223 order = globaldata->gorder;
225 input = globaldata->ginput;
227 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
228 set<string> processedLabels;
229 set<string> userLabels = labels;
230 set<int> userLines = lines;
232 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
234 if(allLines == 1 || lines.count(count) == 1 || labels.count(order->getLabel()) == 1){
236 cCurve = new Collect(order, cDisplays);
237 cCurve->getCurve(freq);
240 cout << order->getLabel() << '\t' << count << endl;
241 processedLabels.insert(order->getLabel());
242 userLabels.erase(order->getLabel());
243 userLines.erase(count);
245 //you have a label the user want that is smaller than this line and the last line has not already been processed
248 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastOrder->getLabel()) != 1)) {
249 cCurve = new Collect(lastOrder, cDisplays);
250 cCurve->getCurve(freq);
253 cout << lastOrder->getLabel() << '\t' << count << endl;
254 processedLabels.insert(lastOrder->getLabel());
255 userLabels.erase(lastOrder->getLabel());
258 if (count != 1) { delete lastOrder; }
260 order = (input->getOrderVector());
264 //output error messages about any remaining user labels
265 set<string>::iterator it;
266 bool needToRun = false;
267 for (it = userLabels.begin(); it != userLabels.end(); it++) {
268 cout << "Your file does not include the label "<< *it;
269 if (processedLabels.count(lastOrder->getLabel()) != 1) {
270 cout << ". I will use " << lastOrder->getLabel() << "." << endl;
273 cout << ". Please refer to " << lastOrder->getLabel() << "." << endl;
277 //run last line if you need to
278 if (needToRun == true) {
279 cCurve = new Collect(lastOrder, cDisplays);
280 cCurve->getCurve(freq);
283 cout << lastOrder->getLabel() << '\t' << count << endl;
287 for(int i=0;i<cDisplays.size();i++){ delete cDisplays[i]; }
290 catch(exception& e) {
291 cout << "Standard Error: " << e.what() << " has occurred in the CollectCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
295 cout << "An unknown error has occurred in the CollectCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
300 //**********************************************************************************************************************