5 * Created by Sarah Westcott on 1/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "summarycommand.h"
15 #include "bootstrap.h"
17 #include "simpsoneven.h"
18 #include "invsimpson.h"
19 #include "npshannon.h"
22 #include "smithwilson.h"
23 #include "shannoneven.h"
24 #include "jackknife.h"
28 #include "bergerparker.h"
30 #include "goodscoverage.h"
37 //**********************************************************************************************************************
38 vector<string> SummaryCommand::setParameters(){
40 CommandParameter plist("list", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(plist);
41 CommandParameter prabund("rabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(prabund);
42 CommandParameter psabund("sabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(psabund);
43 CommandParameter pshared("shared", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(pshared);
44 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
45 CommandParameter pcalc("calc", "Multiple", "sobs-chao-nseqs-coverage-ace-jack-shannon-shannoneven-np_shannon-heip-smithwilson-simpson-simpsoneven-invsimpson-bootstrap-geometric-qstat-logseries-bergerparker-bstick-goodscoverage-efron-boneh-solow-shen", "sobs-chao-ace-jack-shannon-npshannon-simpson", "", "", "",true,false); parameters.push_back(pcalc);
46 CommandParameter pabund("abund", "Number", "", "10", "", "", "",false,false); parameters.push_back(pabund);
47 CommandParameter psize("size", "Number", "", "0", "", "", "",false,false); parameters.push_back(psize);
48 CommandParameter pgroupmode("groupmode", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pgroupmode);
49 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
50 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
52 vector<string> myArray;
53 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
57 m->errorOut(e, "SummaryCommand", "setParameters");
61 //**********************************************************************************************************************
62 string SummaryCommand::getHelpString(){
64 string helpString = "";
65 ValidCalculators validCalculator;
66 helpString += "The summary.single command parameters are list, sabund, rabund, shared, label, calc, abund and groupmode. list, sabund, rabund or shared is required unless you have a valid current file.\n";
67 helpString += "The summary.single command should be in the following format: \n";
68 helpString += "summary.single(label=yourLabel, calc=yourEstimators).\n";
69 helpString += "Example summary.single(label=unique-.01-.03, calc=sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson).\n";
70 helpString += validCalculator.printCalc("summary");
71 helpString += "The default value calc is sobs-chao-ace-jack-shannon-npshannon-simpson\n";
72 helpString += "If you are running summary.single with a shared file and would like your summary results collated in one file, set groupmode=t. (Default=true).\n";
73 helpString += "The label parameter is used to analyze specific labels in your input.\n";
74 helpString += "Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabels).\n";
78 m->errorOut(e, "SummaryCommand", "getHelpString");
83 //**********************************************************************************************************************
84 SummaryCommand::SummaryCommand(){
86 abort = true; calledHelp = true;
88 vector<string> tempOutNames;
89 outputTypes["summary"] = tempOutNames;
92 m->errorOut(e, "SummaryCommand", "SummaryCommand");
96 //**********************************************************************************************************************
98 SummaryCommand::SummaryCommand(string option) {
100 abort = false; calledHelp = false;
103 //allow user to run help
104 if(option == "help") { help(); abort = true; calledHelp = true; }
107 vector<string> myArray = setParameters();
109 OptionParser parser(option);
110 map<string,string> parameters = parser.getParameters();
111 map<string,string>::iterator it;
113 ValidParameters validParameter;
115 //check to make sure all parameters are valid for command
116 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
117 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
120 //initialize outputTypes
121 vector<string> tempOutNames;
122 outputTypes["summary"] = tempOutNames;
124 //if the user changes the input directory command factory will send this info to us in the output parameter
125 string inputDir = validParameter.validFile(parameters, "inputdir", false);
126 if (inputDir == "not found"){ inputDir = ""; }
129 it = parameters.find("shared");
130 //user has given a template file
131 if(it != parameters.end()){
132 path = m->hasPath(it->second);
133 //if the user has not given a path then, add inputdir. else leave path alone.
134 if (path == "") { parameters["shared"] = inputDir + it->second; }
137 it = parameters.find("rabund");
138 //user has given a template file
139 if(it != parameters.end()){
140 path = m->hasPath(it->second);
141 //if the user has not given a path then, add inputdir. else leave path alone.
142 if (path == "") { parameters["rabund"] = inputDir + it->second; }
145 it = parameters.find("sabund");
146 //user has given a template file
147 if(it != parameters.end()){
148 path = m->hasPath(it->second);
149 //if the user has not given a path then, add inputdir. else leave path alone.
150 if (path == "") { parameters["sabund"] = inputDir + it->second; }
153 it = parameters.find("list");
154 //user has given a template file
155 if(it != parameters.end()){
156 path = m->hasPath(it->second);
157 //if the user has not given a path then, add inputdir. else leave path alone.
158 if (path == "") { parameters["list"] = inputDir + it->second; }
162 //check for required parameters
163 listfile = validParameter.validFile(parameters, "list", true);
164 if (listfile == "not open") { listfile = ""; abort = true; }
165 else if (listfile == "not found") { listfile = ""; }
166 else { format = "list"; inputfile = listfile; }
168 sabundfile = validParameter.validFile(parameters, "sabund", true);
169 if (sabundfile == "not open") { sabundfile = ""; abort = true; }
170 else if (sabundfile == "not found") { sabundfile = ""; }
171 else { format = "sabund"; inputfile = sabundfile; }
173 rabundfile = validParameter.validFile(parameters, "rabund", true);
174 if (rabundfile == "not open") { rabundfile = ""; abort = true; }
175 else if (rabundfile == "not found") { rabundfile = ""; }
176 else { format = "rabund"; inputfile = rabundfile; }
178 sharedfile = validParameter.validFile(parameters, "shared", true);
179 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
180 else if (sharedfile == "not found") { sharedfile = ""; }
181 else { format = "sharedfile"; inputfile = sharedfile; }
183 if ((sharedfile == "") && (listfile == "") && (rabundfile == "") && (sabundfile == "")) {
184 //is there are current file available for any of these?
185 //give priority to shared, then list, then rabund, then sabund
186 //if there is a current shared file, use it
187 sharedfile = m->getSharedFile();
188 if (sharedfile != "") { inputfile = sharedfile; format = "sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
190 listfile = m->getListFile();
191 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
193 rabundfile = m->getRabundFile();
194 if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
196 sabundfile = m->getSabundFile();
197 if (sabundfile != "") { inputfile = sabundfile; format = "sabund"; m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
199 m->mothurOut("No valid current files. You must provide a list, sabund, rabund or shared file before you can use the collect.single command."); m->mothurOutEndLine();
207 //if the user changes the output directory command factory will send this info to us in the output parameter
208 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputfile); }
210 //check for optional parameter and set defaults
211 // ...at some point should added some additional type checking...
212 label = validParameter.validFile(parameters, "label", false);
213 if (label == "not found") { label = ""; }
215 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
216 else { allLines = 1; }
219 calc = validParameter.validFile(parameters, "calc", false);
220 if (calc == "not found") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
222 if (calc == "default") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
224 m->splitAtDash(calc, Estimators);
227 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
228 convert(temp, abund);
230 temp = validParameter.validFile(parameters, "size", false); if (temp == "not found") { temp = "0"; }
233 temp = validParameter.validFile(parameters, "groupmode", false); if (temp == "not found") { temp = "T"; }
234 groupMode = m->isTrue(temp);
239 catch(exception& e) {
240 m->errorOut(e, "SummaryCommand", "SummaryCommand");
244 //**********************************************************************************************************************
246 int SummaryCommand::execute(){
249 if (abort == true) { if (calledHelp) { return 0; } return 2; }
251 if ((format != "sharedfile")) { inputFileNames.push_back(inputfile); }
252 else { inputFileNames = parseSharedFile(sharedfile); format = "rabund"; }
254 if (m->control_pressed) { return 0; }
259 for (int p = 0; p < inputFileNames.size(); p++) {
264 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p])) + "summary";
265 outputNames.push_back(fileNameRoot); outputTypes["summary"].push_back(fileNameRoot);
267 if (inputFileNames.size() > 1) {
268 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
271 sumCalculators.clear();
273 ValidCalculators validCalculator;
275 for (int i=0; i<Estimators.size(); i++) {
276 if (validCalculator.isValidCalculator("summary", Estimators[i]) == true) {
277 if(Estimators[i] == "sobs"){
278 sumCalculators.push_back(new Sobs());
279 }else if(Estimators[i] == "chao"){
280 sumCalculators.push_back(new Chao1());
281 }else if(Estimators[i] == "coverage"){
282 sumCalculators.push_back(new Coverage());
283 }else if(Estimators[i] == "geometric"){
284 sumCalculators.push_back(new Geom());
285 }else if(Estimators[i] == "logseries"){
286 sumCalculators.push_back(new LogSD());
287 }else if(Estimators[i] == "qstat"){
288 sumCalculators.push_back(new QStat());
289 }else if(Estimators[i] == "bergerparker"){
290 sumCalculators.push_back(new BergerParker());
291 }else if(Estimators[i] == "bstick"){
292 sumCalculators.push_back(new BStick());
293 }else if(Estimators[i] == "ace"){
296 sumCalculators.push_back(new Ace(abund));
297 }else if(Estimators[i] == "jack"){
298 sumCalculators.push_back(new Jackknife());
299 }else if(Estimators[i] == "shannon"){
300 sumCalculators.push_back(new Shannon());
301 }else if(Estimators[i] == "shannoneven"){
302 sumCalculators.push_back(new ShannonEven());
303 }else if(Estimators[i] == "npshannon"){
304 sumCalculators.push_back(new NPShannon());
305 }else if(Estimators[i] == "heip"){
306 sumCalculators.push_back(new Heip());
307 }else if(Estimators[i] == "smithwilson"){
308 sumCalculators.push_back(new SmithWilson());
309 }else if(Estimators[i] == "simpson"){
310 sumCalculators.push_back(new Simpson());
311 }else if(Estimators[i] == "simpsoneven"){
312 sumCalculators.push_back(new SimpsonEven());
313 }else if(Estimators[i] == "invsimpson"){
314 sumCalculators.push_back(new InvSimpson());
315 }else if(Estimators[i] == "bootstrap"){
316 sumCalculators.push_back(new Bootstrap());
317 }else if (Estimators[i] == "nseqs") {
318 sumCalculators.push_back(new NSeqs());
319 }else if (Estimators[i] == "goodscoverage") {
320 sumCalculators.push_back(new GoodsCoverage());
321 }else if (Estimators[i] == "efron") {
322 sumCalculators.push_back(new Efron(size));
323 }else if (Estimators[i] == "boneh") {
324 sumCalculators.push_back(new Boneh(size));
325 }else if (Estimators[i] == "solow") {
326 sumCalculators.push_back(new Solow(size));
327 }else if (Estimators[i] == "shen") {
328 sumCalculators.push_back(new Shen(size, abund));
333 //if the users entered no valid calculators don't execute command
334 if (sumCalculators.size() == 0) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
336 ofstream outputFileHandle;
337 m->openOutputFile(fileNameRoot, outputFileHandle);
338 outputFileHandle << "label";
340 input = new InputData(inputFileNames[p], format);
341 sabund = input->getSAbundVector();
342 string lastLabel = sabund->getLabel();
344 for(int i=0;i<sumCalculators.size();i++){
345 if(sumCalculators[i]->getCols() == 1){
346 outputFileHandle << '\t' << sumCalculators[i]->getName();
350 outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
354 outputFileHandle << endl;
356 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
357 set<string> processedLabels;
358 set<string> userLabels = labels;
360 if (m->control_pressed) { outputFileHandle.close(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; } delete sabund; delete input; return 0; }
362 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
364 if (m->control_pressed) { outputFileHandle.close(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; } delete sabund; delete input; return 0; }
366 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
368 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
369 processedLabels.insert(sabund->getLabel());
370 userLabels.erase(sabund->getLabel());
372 outputFileHandle << sabund->getLabel();
373 for(int i=0;i<sumCalculators.size();i++){
374 vector<double> data = sumCalculators[i]->getValues(sabund);
376 if (m->control_pressed) { outputFileHandle.close(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; } delete sabund; delete input; return 0; }
378 outputFileHandle << '\t';
379 sumCalculators[i]->print(outputFileHandle);
381 outputFileHandle << endl;
385 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
386 string saveLabel = sabund->getLabel();
389 sabund = input->getSAbundVector(lastLabel);
391 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
392 processedLabels.insert(sabund->getLabel());
393 userLabels.erase(sabund->getLabel());
395 outputFileHandle << sabund->getLabel();
396 for(int i=0;i<sumCalculators.size();i++){
397 vector<double> data = sumCalculators[i]->getValues(sabund);
399 if (m->control_pressed) { outputFileHandle.close(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; } delete sabund; delete input; return 0; }
401 outputFileHandle << '\t';
402 sumCalculators[i]->print(outputFileHandle);
404 outputFileHandle << endl;
407 //restore real lastlabel to save below
408 sabund->setLabel(saveLabel);
411 lastLabel = sabund->getLabel();
414 sabund = input->getSAbundVector();
417 if (m->control_pressed) { outputFileHandle.close(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; } delete input; return 0; }
419 //output error messages about any remaining user labels
420 set<string>::iterator it;
421 bool needToRun = false;
422 for (it = userLabels.begin(); it != userLabels.end(); it++) {
423 m->mothurOut("Your file does not include the label " + *it);
424 if (processedLabels.count(lastLabel) != 1) {
425 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
428 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
432 //run last label if you need to
433 if (needToRun == true) {
434 if (sabund != NULL) { delete sabund; }
435 sabund = input->getSAbundVector(lastLabel);
437 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
438 outputFileHandle << sabund->getLabel();
439 for(int i=0;i<sumCalculators.size();i++){
440 vector<double> data = sumCalculators[i]->getValues(sabund);
442 if (m->control_pressed) { outputFileHandle.close(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; } delete sabund; delete input; return 0; }
444 outputFileHandle << '\t';
445 sumCalculators[i]->print(outputFileHandle);
447 outputFileHandle << endl;
452 outputFileHandle.close();
454 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; } delete input; return 0; }
458 for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; }
461 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
463 //create summary file containing all the groups data for each label - this function just combines the info from the files already created.
464 if ((sharedfile != "") && (groupMode)) { outputNames.push_back(createGroupSummaryFile(numLines, numCols, outputNames)); }
466 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
468 m->mothurOutEndLine();
469 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
470 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
471 m->mothurOutEndLine();
475 catch(exception& e) {
476 m->errorOut(e, "SummaryCommand", "execute");
480 //**********************************************************************************************************************
481 vector<string> SummaryCommand::parseSharedFile(string filename) {
483 vector<string> filenames;
485 map<string, ofstream*> filehandles;
486 map<string, ofstream*>::iterator it3;
488 input = new InputData(filename, "sharedfile");
489 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
491 string sharedFileRoot = m->getRootName(filename);
493 //clears file before we start to write to it below
494 for (int i=0; i<lookup.size(); i++) {
495 remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
496 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
500 for (int i=0; i<lookup.size(); i++) {
502 filehandles[lookup[i]->getGroup()] = temp;
503 groups.push_back(lookup[i]->getGroup());
506 while(lookup[0] != NULL) {
508 for (int i = 0; i < lookup.size(); i++) {
509 RAbundVector rav = lookup[i]->getRAbundVector();
510 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
511 rav.print(*(filehandles[lookup[i]->getGroup()]));
512 (*(filehandles[lookup[i]->getGroup()])).close();
515 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
516 lookup = input->getSharedRAbundVectors();
520 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
528 catch(exception& e) {
529 m->errorOut(e, "SummaryCommand", "parseSharedFile");
533 //**********************************************************************************************************************
534 string SummaryCommand::createGroupSummaryFile(int numLines, int numCols, vector<string>& outputNames) {
538 string combineFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "groups.summary";
541 m->openOutputFile(combineFileName, out);
543 //open each groups summary file
544 string newLabel = "";
545 map<string, vector<string> > files;
546 for (int i=0; i<outputNames.size(); i++) {
547 vector<string> thisFilesLines;
550 m->openInputFile(outputNames[i], temp);
552 //read through first line - labels
554 if (i == 0) { //we want to save the labels to output below
555 for (int j = 0; j < numCols+1; j++) {
558 if (j == 1) { newLabel += "group\t" + tempLabel + '\t';
559 }else{ newLabel += tempLabel + '\t'; }
561 }else{ for (int j = 0; j < numCols+1; j++) { temp >> tempLabel; } }
566 for (int k = 0; k < numLines; k++) {
568 string thisLine = "";
571 for (int j = 0; j < numCols+1; j++) {
575 if (j == 1) { thisLine += groups[i] + "\t" + tempLabel + "\t"; }
576 else{ thisLine += tempLabel + "\t"; }
581 thisFilesLines.push_back(thisLine);
586 files[outputNames[i]] = thisFilesLines;
589 remove(outputNames[i].c_str());
592 //output label line to new file
593 out << newLabel << endl;
596 for (int k = 0; k < numLines; k++) {
598 //grab summary data for each group
599 for (int i=0; i<outputNames.size(); i++) {
600 out << files[outputNames[i]][k];
608 //return combine file name
609 return combineFileName;
612 catch(exception& e) {
613 m->errorOut(e, "SummaryCommand", "createGroupSummaryFile");
617 //**********************************************************************************************************************