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 "simpsoneven.h"
18 #include "invsimpson.h"
19 #include "npshannon.h"
21 #include "smithwilson.h"
23 #include "shannoneven.h"
24 #include "jackknife.h"
28 #include "bergerparker.h"
30 #include "goodscoverage.h"
38 //**********************************************************************************************************************
39 CollectCommand::CollectCommand(string option) {
41 globaldata = GlobalData::getInstance();
47 //allow user to run help
48 if(option == "help") { validCalculator = new ValidCalculators(); help(); delete validCalculator; abort = true; }
51 //valid paramters for this command
52 string Array[] = {"freq","label","calc","abund","size","outputdir","inputdir"};
53 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
55 OptionParser parser(option);
56 map<string,string> parameters = parser.getParameters();
58 ValidParameters validParameter;
60 //check to make sure all parameters are valid for command
61 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
62 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
65 //if the user changes the output directory command factory will send this info to us in the output parameter
66 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
68 //make sure the user has already run the read.otu command
69 if ((globaldata->getSharedFile() == "") && (globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { m->mothurOut("You must read a list, sabund, rabund or shared file before you can use the collect.single command."); m->mothurOutEndLine(); abort = true; }
71 //check for optional parameter and set defaults
72 // ...at some point should added some additional type checking...
73 label = validParameter.validFile(parameters, "label", false);
74 if (label == "not found") { label = ""; }
76 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
77 else { allLines = 1; }
80 //if the user has not specified any labels use the ones from read.otu
82 allLines = globaldata->allLines;
83 labels = globaldata->labels;
86 calc = validParameter.validFile(parameters, "calc", false);
87 if (calc == "not found") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
89 if (calc == "default") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
91 m->splitAtDash(calc, Estimators);
94 temp = validParameter.validFile(parameters, "freq", false); if (temp == "not found") { temp = "100"; }
97 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
100 temp = validParameter.validFile(parameters, "size", false); if (temp == "not found") { temp = "0"; }
105 catch(exception& e) {
106 m->errorOut(e, "CollectCommand", "CollectCommand");
110 //**********************************************************************************************************************
112 void CollectCommand::help(){
114 m->mothurOut("The collect.single command can only be executed after a successful read.otu command. WITH ONE EXECEPTION. \n");
115 m->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");
116 m->mothurOut("The collect.single command parameters are label, freq, calc and abund. No parameters are required. \n");
117 m->mothurOut("The collect.single command should be in the following format: \n");
118 m->mothurOut("The freq parameter is used indicate when to output your data, by default it is set to 100. But you can set it to a percentage of the number of sequence. For example freq=0.10, means 10%. \n");
119 m->mothurOut("collect.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n");
120 m->mothurOut("Example collect(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-chao-ace-jack).\n");
121 m->mothurOut("The default values for freq is 100, and calc are sobs-chao-ace-jack-shannon-npshannon-simpson.\n");
122 validCalculator->printCalc("single", cout);
123 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
124 m->mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
126 catch(exception& e) {
127 m->errorOut(e, "CollectCommand", "help");
132 //**********************************************************************************************************************
134 CollectCommand::~CollectCommand(){}
136 //**********************************************************************************************************************
138 int CollectCommand::execute(){
141 if (abort == true) { return 0; }
143 vector<string> outputNames;
145 string hadShared = "";
146 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName); }
147 else { hadShared = globaldata->getSharedFile(); inputFileNames = parseSharedFile(globaldata->getSharedFile()); globaldata->setFormat("rabund"); }
149 for (int p = 0; p < inputFileNames.size(); p++) {
151 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } globaldata->Groups.clear(); if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } return 0; }
153 if (outputDir == "") { outputDir += m->hasPath(inputFileNames[p]); }
154 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p]));
155 globaldata->inputFileName = inputFileNames[p];
157 if (inputFileNames.size() > 1) {
158 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
161 validCalculator = new ValidCalculators();
163 for (int i=0; i<Estimators.size(); i++) {
164 if (validCalculator->isValidCalculator("single", Estimators[i]) == true) {
165 if (Estimators[i] == "sobs") {
166 cDisplays.push_back(new CollectDisplay(new Sobs(), new OneColumnFile(fileNameRoot+"sobs")));
167 outputNames.push_back(fileNameRoot+"sobs");
168 }else if (Estimators[i] == "chao") {
169 cDisplays.push_back(new CollectDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"chao")));
170 outputNames.push_back(fileNameRoot+"chao");
171 }else if (Estimators[i] == "nseqs") {
172 cDisplays.push_back(new CollectDisplay(new NSeqs(), new OneColumnFile(fileNameRoot+"nseqs")));
173 outputNames.push_back(fileNameRoot+"nseqs");
174 }else if (Estimators[i] == "coverage") {
175 cDisplays.push_back(new CollectDisplay(new Coverage(), new OneColumnFile(fileNameRoot+"coverage")));
176 outputNames.push_back(fileNameRoot+"coverage");
177 }else if (Estimators[i] == "ace") {
178 cDisplays.push_back(new CollectDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"ace")));
179 outputNames.push_back(fileNameRoot+"ace");
180 }else if (Estimators[i] == "jack") {
181 cDisplays.push_back(new CollectDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"jack")));
182 outputNames.push_back(fileNameRoot+"jack");
183 }else if (Estimators[i] == "shannon") {
184 cDisplays.push_back(new CollectDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"shannon")));
185 outputNames.push_back(fileNameRoot+"shannon");
186 }else if (Estimators[i] == "shannoneven") {
187 cDisplays.push_back(new CollectDisplay(new ShannonEven(), new OneColumnFile(fileNameRoot+"shannoneven")));
188 outputNames.push_back(fileNameRoot+"shannoneven");
189 }else if (Estimators[i] == "npshannon") {
190 cDisplays.push_back(new CollectDisplay(new NPShannon(), new OneColumnFile(fileNameRoot+"np_shannon")));
191 outputNames.push_back(fileNameRoot+"np_shannon");
192 }else if (Estimators[i] == "heip") {
193 cDisplays.push_back(new CollectDisplay(new Heip(), new OneColumnFile(fileNameRoot+"heip")));
194 outputNames.push_back(fileNameRoot+"heip");
195 }else if (Estimators[i] == "smithwilson") {
196 cDisplays.push_back(new CollectDisplay(new SmithWilson(), new OneColumnFile(fileNameRoot+"smithwilson")));
197 outputNames.push_back(fileNameRoot+"smithwilson");
198 }else if (Estimators[i] == "simpson") {
199 cDisplays.push_back(new CollectDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"simpson")));
200 outputNames.push_back(fileNameRoot+"simpson");
201 }else if (Estimators[i] == "simpsoneven") {
202 cDisplays.push_back(new CollectDisplay(new SimpsonEven(), new OneColumnFile(fileNameRoot+"simpsoneven")));
203 outputNames.push_back(fileNameRoot+"simpsoneven");
204 }else if (Estimators[i] == "invsimpson") {
205 cDisplays.push_back(new CollectDisplay(new InvSimpson(), new ThreeColumnFile(fileNameRoot+"invsimpson")));
206 outputNames.push_back(fileNameRoot+"invsimpson");
207 }else if (Estimators[i] == "bootstrap") {
208 cDisplays.push_back(new CollectDisplay(new Bootstrap(), new OneColumnFile(fileNameRoot+"bootstrap")));
209 outputNames.push_back(fileNameRoot+"bootstrap");
210 }else if (Estimators[i] == "geometric") {
211 cDisplays.push_back(new CollectDisplay(new Geom(), new OneColumnFile(fileNameRoot+"geometric")));
212 outputNames.push_back(fileNameRoot+"geometric");
213 }else if (Estimators[i] == "qstat") {
214 cDisplays.push_back(new CollectDisplay(new QStat(), new OneColumnFile(fileNameRoot+"qstat")));
215 outputNames.push_back(fileNameRoot+"qstat");
216 }else if (Estimators[i] == "logseries") {
217 cDisplays.push_back(new CollectDisplay(new LogSD(), new OneColumnFile(fileNameRoot+"logseries")));
218 outputNames.push_back(fileNameRoot+"logseries");
219 }else if (Estimators[i] == "bergerparker") {
220 cDisplays.push_back(new CollectDisplay(new BergerParker(), new OneColumnFile(fileNameRoot+"bergerparker")));
221 outputNames.push_back(fileNameRoot+"bergerparker");
222 }else if (Estimators[i] == "bstick") {
223 cDisplays.push_back(new CollectDisplay(new BStick(), new ThreeColumnFile(fileNameRoot+"bstick")));
224 outputNames.push_back(fileNameRoot+"bstick");
225 }else if (Estimators[i] == "goodscoverage") {
226 cDisplays.push_back(new CollectDisplay(new GoodsCoverage(), new OneColumnFile(fileNameRoot+"goodscoverage")));
227 outputNames.push_back(fileNameRoot+"goodscoverage");
228 }else if (Estimators[i] == "efron") {
229 cDisplays.push_back(new CollectDisplay(new Efron(size), new OneColumnFile(fileNameRoot+"efron")));
230 outputNames.push_back(fileNameRoot+"efron");
231 }else if (Estimators[i] == "boneh") {
232 cDisplays.push_back(new CollectDisplay(new Boneh(size), new OneColumnFile(fileNameRoot+"boneh")));
233 outputNames.push_back(fileNameRoot+"boneh");
234 }else if (Estimators[i] == "solow") {
235 cDisplays.push_back(new CollectDisplay(new Solow(size), new OneColumnFile(fileNameRoot+"solow")));
236 outputNames.push_back(fileNameRoot+"solow");
237 }else if (Estimators[i] == "shen") {
238 cDisplays.push_back(new CollectDisplay(new Shen(size, abund), new OneColumnFile(fileNameRoot+"shen")));
239 outputNames.push_back(fileNameRoot+"shen");
244 //if the users entered no valid calculators don't execute command
245 if (cDisplays.size() == 0) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } return 0; }
247 read = new ReadOTUFile(inputFileNames[p]);
248 read->read(&*globaldata);
250 order = globaldata->gorder;
251 string lastLabel = order->getLabel();
252 input = globaldata->ginput;
254 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
255 set<string> processedLabels;
256 set<string> userLabels = labels;
258 if (m->control_pressed) {
259 for(int i=0;i<cDisplays.size();i++){ delete cDisplays[i]; }
260 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
261 delete input; globaldata->ginput = NULL;
263 delete order; globaldata->gorder = NULL;
264 delete validCalculator;
265 globaldata->Groups.clear();
266 if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); }
271 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
273 if (m->control_pressed) {
274 for(int i=0;i<cDisplays.size();i++){ delete cDisplays[i]; }
275 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
276 delete input; globaldata->ginput = NULL;
278 delete order; globaldata->gorder = NULL;
279 delete validCalculator;
280 globaldata->Groups.clear();
281 if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); }
286 if(allLines == 1 || labels.count(order->getLabel()) == 1){
288 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
289 cCurve = new Collect(order, cDisplays);
290 cCurve->getCurve(freq);
293 processedLabels.insert(order->getLabel());
294 userLabels.erase(order->getLabel());
298 //you have a label the user want that is smaller than this label and the last label has not already been processed
299 if ((m->anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
300 string saveLabel = order->getLabel();
303 order = (input->getOrderVector(lastLabel));
305 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
306 cCurve = new Collect(order, cDisplays);
307 cCurve->getCurve(freq);
311 processedLabels.insert(order->getLabel());
312 userLabels.erase(order->getLabel());
314 //restore real lastlabel to save below
315 order->setLabel(saveLabel);
318 lastLabel = order->getLabel();
321 order = (input->getOrderVector());
325 if (m->control_pressed) {
326 for(int i=0;i<cDisplays.size();i++){ delete cDisplays[i]; }
327 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
328 delete input; globaldata->ginput = NULL;
330 delete validCalculator;
331 globaldata->Groups.clear();
332 if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); }
336 //output error messages about any remaining user labels
337 set<string>::iterator it;
338 bool needToRun = false;
339 for (it = userLabels.begin(); it != userLabels.end(); it++) {
340 m->mothurOut("Your file does not include the label " + *it);
341 if (processedLabels.count(lastLabel) != 1) {
342 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
345 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
349 //run last label if you need to
350 if (needToRun == true) {
351 if (order != NULL) { delete order; }
352 order = (input->getOrderVector(lastLabel));
354 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
356 cCurve = new Collect(order, cDisplays);
357 cCurve->getCurve(freq);
360 if (m->control_pressed) {
361 for(int i=0;i<cDisplays.size();i++){ delete cDisplays[i]; }
362 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
363 delete input; globaldata->ginput = NULL;
365 delete order; globaldata->gorder = NULL;
366 delete validCalculator;
367 globaldata->Groups.clear();
368 if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); }
374 for(int i=0;i<cDisplays.size();i++){ delete cDisplays[i]; }
376 delete input; globaldata->ginput = NULL;
378 globaldata->gorder = NULL;
379 delete validCalculator;
382 //return to shared mode if you changed above
383 if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); }
385 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
387 m->mothurOutEndLine();
388 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
389 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
390 m->mothurOutEndLine();
395 catch(exception& e) {
396 m->errorOut(e, "CollectCommand", "execute");
401 //**********************************************************************************************************************
402 vector<string> CollectCommand::parseSharedFile(string filename) {
404 vector<string> filenames;
406 map<string, ofstream*> filehandles;
407 map<string, ofstream*>::iterator it3;
411 read = new ReadOTUFile(filename);
412 read->read(&*globaldata);
414 input = globaldata->ginput;
415 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
417 string sharedFileRoot = m->getRootName(filename);
419 //clears file before we start to write to it below
420 for (int i=0; i<lookup.size(); i++) {
421 remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
422 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
426 for (int i=0; i<lookup.size(); i++) {
428 filehandles[lookup[i]->getGroup()] = temp;
429 groups.push_back(lookup[i]->getGroup());
432 while(lookup[0] != NULL) {
434 for (int i = 0; i < lookup.size(); i++) {
435 RAbundVector rav = lookup[i]->getRAbundVector();
436 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
437 rav.print(*(filehandles[lookup[i]->getGroup()]));
438 (*(filehandles[lookup[i]->getGroup()])).close();
441 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
442 lookup = input->getSharedRAbundVectors();
446 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
451 globaldata->ginput = NULL;
455 catch(exception& e) {
456 m->errorOut(e, "CollectCommand", "parseSharedFile");
460 //**********************************************************************************************************************