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();
42 //allow user to run help
43 if(option == "help") { validCalculator = new ValidCalculators(); help(); delete validCalculator; abort = true; }
46 //valid paramters for this command
47 string Array[] = {"freq","label","calc","abund","size","outputdir","inputdir"};
48 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
50 OptionParser parser(option);
51 map<string,string> parameters = parser.getParameters();
53 ValidParameters validParameter;
55 //check to make sure all parameters are valid for command
56 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
57 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
60 //if the user changes the output directory command factory will send this info to us in the output parameter
61 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
63 //make sure the user has already run the read.otu command
64 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; }
66 //check for optional parameter and set defaults
67 // ...at some point should added some additional type checking...
68 label = validParameter.validFile(parameters, "label", false);
69 if (label == "not found") { label = ""; }
71 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
72 else { allLines = 1; }
75 //if the user has not specified any labels use the ones from read.otu
77 allLines = globaldata->allLines;
78 labels = globaldata->labels;
81 calc = validParameter.validFile(parameters, "calc", false);
82 if (calc == "not found") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
84 if (calc == "default") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
86 splitAtDash(calc, Estimators);
89 temp = validParameter.validFile(parameters, "freq", false); if (temp == "not found") { temp = "100"; }
92 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
95 temp = validParameter.validFile(parameters, "size", false); if (temp == "not found") { temp = "0"; }
100 catch(exception& e) {
101 m->errorOut(e, "CollectCommand", "CollectCommand");
105 //**********************************************************************************************************************
107 void CollectCommand::help(){
109 m->mothurOut("The collect.single command can only be executed after a successful read.otu command. WITH ONE EXECEPTION. \n");
110 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");
111 m->mothurOut("The collect.single command parameters are label, freq, calc and abund. No parameters are required. \n");
112 m->mothurOut("The collect.single command should be in the following format: \n");
113 m->mothurOut("collect.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n");
114 m->mothurOut("Example collect(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-chao-ace-jack).\n");
115 m->mothurOut("The default values for freq is 100, and calc are sobs-chao-ace-jack-shannon-npshannon-simpson.\n");
116 validCalculator->printCalc("single", cout);
117 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
118 m->mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
120 catch(exception& e) {
121 m->errorOut(e, "CollectCommand", "help");
126 //**********************************************************************************************************************
128 CollectCommand::~CollectCommand(){}
130 //**********************************************************************************************************************
132 int CollectCommand::execute(){
135 if (abort == true) { return 0; }
137 vector<string> outputNames;
139 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName); }
140 else { inputFileNames = parseSharedFile(globaldata->getSharedFile()); globaldata->setFormat("rabund"); }
142 for (int p = 0; p < inputFileNames.size(); p++) {
144 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } globaldata->Groups.clear(); return 0; }
146 if (outputDir == "") { outputDir += hasPath(inputFileNames[p]); }
147 string fileNameRoot = outputDir + getRootName(getSimpleName(inputFileNames[p]));
148 globaldata->inputFileName = inputFileNames[p];
150 if (inputFileNames.size() > 1) {
151 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
154 validCalculator = new ValidCalculators();
156 for (int i=0; i<Estimators.size(); i++) {
157 if (validCalculator->isValidCalculator("single", Estimators[i]) == true) {
158 if (Estimators[i] == "sobs") {
159 cDisplays.push_back(new CollectDisplay(new Sobs(), new OneColumnFile(fileNameRoot+"sobs")));
160 outputNames.push_back(fileNameRoot+"sobs");
161 }else if (Estimators[i] == "chao") {
162 cDisplays.push_back(new CollectDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"chao")));
163 outputNames.push_back(fileNameRoot+"chao");
164 }else if (Estimators[i] == "nseqs") {
165 cDisplays.push_back(new CollectDisplay(new NSeqs(), new OneColumnFile(fileNameRoot+"nseqs")));
166 outputNames.push_back(fileNameRoot+"nseqs");
167 }else if (Estimators[i] == "coverage") {
168 cDisplays.push_back(new CollectDisplay(new Coverage(), new OneColumnFile(fileNameRoot+"coverage")));
169 outputNames.push_back(fileNameRoot+"coverage");
170 }else if (Estimators[i] == "ace") {
171 cDisplays.push_back(new CollectDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"ace")));
172 outputNames.push_back(fileNameRoot+"ace");
173 }else if (Estimators[i] == "jack") {
174 cDisplays.push_back(new CollectDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"jack")));
175 outputNames.push_back(fileNameRoot+"jack");
176 }else if (Estimators[i] == "shannon") {
177 cDisplays.push_back(new CollectDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"shannon")));
178 outputNames.push_back(fileNameRoot+"shannon");
179 }else if (Estimators[i] == "npshannon") {
180 cDisplays.push_back(new CollectDisplay(new NPShannon(), new OneColumnFile(fileNameRoot+"np_shannon")));
181 outputNames.push_back(fileNameRoot+"np_shannon");
182 }else if (Estimators[i] == "simpson") {
183 cDisplays.push_back(new CollectDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"simpson")));
184 outputNames.push_back(fileNameRoot+"simpson");
185 }else if (Estimators[i] == "bootstrap") {
186 cDisplays.push_back(new CollectDisplay(new Bootstrap(), new OneColumnFile(fileNameRoot+"bootstrap")));
187 outputNames.push_back(fileNameRoot+"bootstrap");
188 }else if (Estimators[i] == "geometric") {
189 cDisplays.push_back(new CollectDisplay(new Geom(), new OneColumnFile(fileNameRoot+"geometric")));
190 outputNames.push_back(fileNameRoot+"geometric");
191 }else if (Estimators[i] == "qstat") {
192 cDisplays.push_back(new CollectDisplay(new QStat(), new OneColumnFile(fileNameRoot+"qstat")));
193 outputNames.push_back(fileNameRoot+"qstat");
194 }else if (Estimators[i] == "logseries") {
195 cDisplays.push_back(new CollectDisplay(new LogSD(), new OneColumnFile(fileNameRoot+"logseries")));
196 outputNames.push_back(fileNameRoot+"logseries");
197 }else if (Estimators[i] == "bergerparker") {
198 cDisplays.push_back(new CollectDisplay(new BergerParker(), new OneColumnFile(fileNameRoot+"bergerparker")));
199 outputNames.push_back(fileNameRoot+"bergerparker");
200 }else if (Estimators[i] == "bstick") {
201 cDisplays.push_back(new CollectDisplay(new BStick(), new ThreeColumnFile(fileNameRoot+"bstick")));
202 outputNames.push_back(fileNameRoot+"bstick");
203 }else if (Estimators[i] == "goodscoverage") {
204 cDisplays.push_back(new CollectDisplay(new GoodsCoverage(), new OneColumnFile(fileNameRoot+"goodscoverage")));
205 outputNames.push_back(fileNameRoot+"goodscoverage");
206 }else if (Estimators[i] == "efron") {
207 cDisplays.push_back(new CollectDisplay(new Efron(size), new OneColumnFile(fileNameRoot+"efron")));
208 outputNames.push_back(fileNameRoot+"efron");
209 }else if (Estimators[i] == "boneh") {
210 cDisplays.push_back(new CollectDisplay(new Boneh(size), new OneColumnFile(fileNameRoot+"boneh")));
211 outputNames.push_back(fileNameRoot+"boneh");
212 }else if (Estimators[i] == "solow") {
213 cDisplays.push_back(new CollectDisplay(new Solow(size), new OneColumnFile(fileNameRoot+"solow")));
214 outputNames.push_back(fileNameRoot+"solow");
215 }else if (Estimators[i] == "shen") {
216 cDisplays.push_back(new CollectDisplay(new Shen(size, abund), new OneColumnFile(fileNameRoot+"shen")));
217 outputNames.push_back(fileNameRoot+"shen");
222 //if the users entered no valid calculators don't execute command
223 if (cDisplays.size() == 0) { return 0; }
225 read = new ReadOTUFile(inputFileNames[p]);
226 read->read(&*globaldata);
228 order = globaldata->gorder;
229 string lastLabel = order->getLabel();
230 input = globaldata->ginput;
232 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
233 set<string> processedLabels;
234 set<string> userLabels = labels;
236 if (m->control_pressed) {
237 for(int i=0;i<cDisplays.size();i++){ delete cDisplays[i]; }
238 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
239 delete input; globaldata->ginput = NULL;
241 delete order; globaldata->gorder = NULL;
242 delete validCalculator;
243 globaldata->Groups.clear();
248 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
250 if (m->control_pressed) {
251 for(int i=0;i<cDisplays.size();i++){ delete cDisplays[i]; }
252 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
253 delete input; globaldata->ginput = NULL;
255 delete order; globaldata->gorder = NULL;
256 delete validCalculator;
257 globaldata->Groups.clear();
262 if(allLines == 1 || labels.count(order->getLabel()) == 1){
264 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
265 cCurve = new Collect(order, cDisplays);
266 cCurve->getCurve(freq);
269 processedLabels.insert(order->getLabel());
270 userLabels.erase(order->getLabel());
274 //you have a label the user want that is smaller than this label and the last label has not already been processed
275 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
276 string saveLabel = order->getLabel();
279 order = (input->getOrderVector(lastLabel));
281 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
282 cCurve = new Collect(order, cDisplays);
283 cCurve->getCurve(freq);
287 processedLabels.insert(order->getLabel());
288 userLabels.erase(order->getLabel());
290 //restore real lastlabel to save below
291 order->setLabel(saveLabel);
294 lastLabel = order->getLabel();
297 order = (input->getOrderVector());
301 if (m->control_pressed) {
302 for(int i=0;i<cDisplays.size();i++){ delete cDisplays[i]; }
303 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
304 delete input; globaldata->ginput = NULL;
306 delete validCalculator;
307 globaldata->Groups.clear();
311 //output error messages about any remaining user labels
312 set<string>::iterator it;
313 bool needToRun = false;
314 for (it = userLabels.begin(); it != userLabels.end(); it++) {
315 m->mothurOut("Your file does not include the label " + *it);
316 if (processedLabels.count(lastLabel) != 1) {
317 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
320 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
324 //run last label if you need to
325 if (needToRun == true) {
326 if (order != NULL) { delete order; }
327 order = (input->getOrderVector(lastLabel));
329 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
331 cCurve = new Collect(order, cDisplays);
332 cCurve->getCurve(freq);
335 if (m->control_pressed) {
336 for(int i=0;i<cDisplays.size();i++){ delete cDisplays[i]; }
337 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
338 delete input; globaldata->ginput = NULL;
340 delete order; globaldata->gorder = NULL;
341 delete validCalculator;
342 globaldata->Groups.clear();
348 for(int i=0;i<cDisplays.size();i++){ delete cDisplays[i]; }
350 delete input; globaldata->ginput = NULL;
352 globaldata->gorder = NULL;
353 delete validCalculator;
356 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
358 m->mothurOutEndLine();
359 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
360 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
361 m->mothurOutEndLine();
366 catch(exception& e) {
367 m->errorOut(e, "CollectCommand", "execute");
372 //**********************************************************************************************************************
373 vector<string> CollectCommand::parseSharedFile(string filename) {
375 vector<string> filenames;
377 map<string, ofstream*> filehandles;
378 map<string, ofstream*>::iterator it3;
382 read = new ReadOTUFile(filename);
383 read->read(&*globaldata);
385 input = globaldata->ginput;
386 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
388 string sharedFileRoot = getRootName(filename);
390 //clears file before we start to write to it below
391 for (int i=0; i<lookup.size(); i++) {
392 remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
393 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
397 for (int i=0; i<lookup.size(); i++) {
399 filehandles[lookup[i]->getGroup()] = temp;
400 groups.push_back(lookup[i]->getGroup());
403 while(lookup[0] != NULL) {
405 for (int i = 0; i < lookup.size(); i++) {
406 RAbundVector rav = lookup[i]->getRAbundVector();
407 openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
408 rav.print(*(filehandles[lookup[i]->getGroup()]));
409 (*(filehandles[lookup[i]->getGroup()])).close();
412 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
413 lookup = input->getSharedRAbundVectors();
417 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
422 globaldata->ginput = NULL;
426 catch(exception& e) {
427 m->errorOut(e, "CollectCommand", "parseSharedFile");
431 //**********************************************************************************************************************