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() == "")) { mothurOut("You must read a list, sabund or rabund before you can use the collect.single command."); mothurOutEndLine(); 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 != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); 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 errorOut(e, "CollectCommand", "CollectCommand");
165 //**********************************************************************************************************************
167 void CollectCommand::help(){
169 mothurOut("The collect.single command can only be executed after a successful read.otu command. WITH ONE EXECEPTION. \n");
170 mothurOut("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");
171 mothurOut("The collect.single command parameters are label, line, freq, calc and abund. No parameters are required, but you may not use \n");
172 mothurOut("both the line and label parameters at the same time. The collect.single command should be in the following format: \n");
173 mothurOut("collect.single(label=yourLabel, line=yourLines, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n");
174 mothurOut("Example collect(label=unique-.01-.03, line=0-5-10, iters=10000, freq=10, calc=sobs-chao-ace-jack).\n");
175 mothurOut("The default values for freq is 100, and calc are sobs-chao-ace-jack-shannon-npshannon-simpson.\n");
176 validCalculator->printCalc("single", cout);
177 mothurOut("The label and line parameters are used to analyze specific lines in your input.\n");
178 mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
180 catch(exception& e) {
181 errorOut(e, "CollectCommand", "help");
186 //**********************************************************************************************************************
188 CollectCommand::~CollectCommand(){
189 if (abort == false) {
191 delete input; globaldata->ginput = NULL;
193 delete validCalculator;
194 globaldata->gorder = NULL;
198 //**********************************************************************************************************************
200 int CollectCommand::execute(){
203 if (abort == true) { return 0; }
207 //if the users entered no valid calculators don't execute command
208 if (cDisplays.size() == 0) { return 0; }
210 read = new ReadOTUFile(globaldata->inputFileName);
211 read->read(&*globaldata);
213 order = globaldata->gorder;
214 string lastLabel = order->getLabel();
215 input = globaldata->ginput;
217 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
218 set<string> processedLabels;
219 set<string> userLabels = labels;
220 set<int> userLines = lines;
222 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
224 if(allLines == 1 || lines.count(count) == 1 || labels.count(order->getLabel()) == 1){
226 cCurve = new Collect(order, cDisplays);
227 cCurve->getCurve(freq);
230 mothurOut(order->getLabel() + "\t" + toString(count)); mothurOutEndLine();
231 processedLabels.insert(order->getLabel());
232 userLabels.erase(order->getLabel());
233 userLines.erase(count);
235 //you have a label the user want that is smaller than this line and the last line has not already been processed
238 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
241 order = (input->getOrderVector(lastLabel));
243 cCurve = new Collect(order, cDisplays);
244 cCurve->getCurve(freq);
247 mothurOut(order->getLabel() + "\t" + toString(count)); mothurOutEndLine();
248 processedLabels.insert(order->getLabel());
249 userLabels.erase(order->getLabel());
252 lastLabel = order->getLabel();
255 order = (input->getOrderVector());
259 //output error messages about any remaining user labels
260 set<string>::iterator it;
261 bool needToRun = false;
262 for (it = userLabels.begin(); it != userLabels.end(); it++) {
263 mothurOut("Your file does not include the label " + *it);
264 if (processedLabels.count(lastLabel) != 1) {
265 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
268 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
272 //run last line if you need to
273 if (needToRun == true) {
275 order = (input->getOrderVector(lastLabel));
277 mothurOut(order->getLabel() + "\t" + toString(count)); mothurOutEndLine();
279 cCurve = new Collect(order, cDisplays);
280 cCurve->getCurve(freq);
286 for(int i=0;i<cDisplays.size();i++){ delete cDisplays[i]; }
289 catch(exception& e) {
290 errorOut(e, "CollectCommand", "execute");
295 //**********************************************************************************************************************