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; }
105 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
108 vector<string> myArray = setParameters();
110 OptionParser parser(option);
111 map<string,string> parameters = parser.getParameters();
112 map<string,string>::iterator it;
114 ValidParameters validParameter;
116 //check to make sure all parameters are valid for command
117 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
118 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
121 //initialize outputTypes
122 vector<string> tempOutNames;
123 outputTypes["summary"] = tempOutNames;
125 //if the user changes the input directory command factory will send this info to us in the output parameter
126 string inputDir = validParameter.validFile(parameters, "inputdir", false);
127 if (inputDir == "not found"){ inputDir = ""; }
130 it = parameters.find("shared");
131 //user has given a template file
132 if(it != parameters.end()){
133 path = m->hasPath(it->second);
134 //if the user has not given a path then, add inputdir. else leave path alone.
135 if (path == "") { parameters["shared"] = inputDir + it->second; }
138 it = parameters.find("rabund");
139 //user has given a template file
140 if(it != parameters.end()){
141 path = m->hasPath(it->second);
142 //if the user has not given a path then, add inputdir. else leave path alone.
143 if (path == "") { parameters["rabund"] = inputDir + it->second; }
146 it = parameters.find("sabund");
147 //user has given a template file
148 if(it != parameters.end()){
149 path = m->hasPath(it->second);
150 //if the user has not given a path then, add inputdir. else leave path alone.
151 if (path == "") { parameters["sabund"] = inputDir + it->second; }
154 it = parameters.find("list");
155 //user has given a template file
156 if(it != parameters.end()){
157 path = m->hasPath(it->second);
158 //if the user has not given a path then, add inputdir. else leave path alone.
159 if (path == "") { parameters["list"] = inputDir + it->second; }
163 //check for required parameters
164 listfile = validParameter.validFile(parameters, "list", true);
165 if (listfile == "not open") { listfile = ""; abort = true; }
166 else if (listfile == "not found") { listfile = ""; }
167 else { format = "list"; inputfile = listfile; }
169 sabundfile = validParameter.validFile(parameters, "sabund", true);
170 if (sabundfile == "not open") { sabundfile = ""; abort = true; }
171 else if (sabundfile == "not found") { sabundfile = ""; }
172 else { format = "sabund"; inputfile = sabundfile; }
174 rabundfile = validParameter.validFile(parameters, "rabund", true);
175 if (rabundfile == "not open") { rabundfile = ""; abort = true; }
176 else if (rabundfile == "not found") { rabundfile = ""; }
177 else { format = "rabund"; inputfile = rabundfile; }
179 sharedfile = validParameter.validFile(parameters, "shared", true);
180 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
181 else if (sharedfile == "not found") { sharedfile = ""; }
182 else { format = "sharedfile"; inputfile = sharedfile; }
184 if ((sharedfile == "") && (listfile == "") && (rabundfile == "") && (sabundfile == "")) {
185 //is there are current file available for any of these?
186 //give priority to shared, then list, then rabund, then sabund
187 //if there is a current shared file, use it
188 sharedfile = m->getSharedFile();
189 if (sharedfile != "") { inputfile = sharedfile; format = "sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
191 listfile = m->getListFile();
192 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
194 rabundfile = m->getRabundFile();
195 if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
197 sabundfile = m->getSabundFile();
198 if (sabundfile != "") { inputfile = sabundfile; format = "sabund"; m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
200 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();
208 //if the user changes the output directory command factory will send this info to us in the output parameter
209 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputfile); }
211 //check for optional parameter and set defaults
212 // ...at some point should added some additional type checking...
213 label = validParameter.validFile(parameters, "label", false);
214 if (label == "not found") { label = ""; }
216 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
217 else { allLines = 1; }
220 calc = validParameter.validFile(parameters, "calc", false);
221 if (calc == "not found") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
223 if (calc == "default") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
225 m->splitAtDash(calc, Estimators);
228 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
229 convert(temp, abund);
231 temp = validParameter.validFile(parameters, "size", false); if (temp == "not found") { temp = "0"; }
234 temp = validParameter.validFile(parameters, "groupmode", false); if (temp == "not found") { temp = "T"; }
235 groupMode = m->isTrue(temp);
240 catch(exception& e) {
241 m->errorOut(e, "SummaryCommand", "SummaryCommand");
245 //**********************************************************************************************************************
247 int SummaryCommand::execute(){
250 if (abort == true) { if (calledHelp) { return 0; } return 2; }
252 if ((format != "sharedfile")) { inputFileNames.push_back(inputfile); }
253 else { inputFileNames = parseSharedFile(sharedfile); format = "rabund"; }
255 if (m->control_pressed) { return 0; }
260 for (int p = 0; p < inputFileNames.size(); p++) {
265 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p])) + "summary";
266 outputNames.push_back(fileNameRoot); outputTypes["summary"].push_back(fileNameRoot);
268 if (inputFileNames.size() > 1) {
269 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
272 sumCalculators.clear();
274 ValidCalculators validCalculator;
276 for (int i=0; i<Estimators.size(); i++) {
277 if (validCalculator.isValidCalculator("summary", Estimators[i]) == true) {
278 if(Estimators[i] == "sobs"){
279 sumCalculators.push_back(new Sobs());
280 }else if(Estimators[i] == "chao"){
281 sumCalculators.push_back(new Chao1());
282 }else if(Estimators[i] == "coverage"){
283 sumCalculators.push_back(new Coverage());
284 }else if(Estimators[i] == "geometric"){
285 sumCalculators.push_back(new Geom());
286 }else if(Estimators[i] == "logseries"){
287 sumCalculators.push_back(new LogSD());
288 }else if(Estimators[i] == "qstat"){
289 sumCalculators.push_back(new QStat());
290 }else if(Estimators[i] == "bergerparker"){
291 sumCalculators.push_back(new BergerParker());
292 }else if(Estimators[i] == "bstick"){
293 sumCalculators.push_back(new BStick());
294 }else if(Estimators[i] == "ace"){
297 sumCalculators.push_back(new Ace(abund));
298 }else if(Estimators[i] == "jack"){
299 sumCalculators.push_back(new Jackknife());
300 }else if(Estimators[i] == "shannon"){
301 sumCalculators.push_back(new Shannon());
302 }else if(Estimators[i] == "shannoneven"){
303 sumCalculators.push_back(new ShannonEven());
304 }else if(Estimators[i] == "npshannon"){
305 sumCalculators.push_back(new NPShannon());
306 }else if(Estimators[i] == "heip"){
307 sumCalculators.push_back(new Heip());
308 }else if(Estimators[i] == "smithwilson"){
309 sumCalculators.push_back(new SmithWilson());
310 }else if(Estimators[i] == "simpson"){
311 sumCalculators.push_back(new Simpson());
312 }else if(Estimators[i] == "simpsoneven"){
313 sumCalculators.push_back(new SimpsonEven());
314 }else if(Estimators[i] == "invsimpson"){
315 sumCalculators.push_back(new InvSimpson());
316 }else if(Estimators[i] == "bootstrap"){
317 sumCalculators.push_back(new Bootstrap());
318 }else if (Estimators[i] == "nseqs") {
319 sumCalculators.push_back(new NSeqs());
320 }else if (Estimators[i] == "goodscoverage") {
321 sumCalculators.push_back(new GoodsCoverage());
322 }else if (Estimators[i] == "efron") {
323 sumCalculators.push_back(new Efron(size));
324 }else if (Estimators[i] == "boneh") {
325 sumCalculators.push_back(new Boneh(size));
326 }else if (Estimators[i] == "solow") {
327 sumCalculators.push_back(new Solow(size));
328 }else if (Estimators[i] == "shen") {
329 sumCalculators.push_back(new Shen(size, abund));
334 //if the users entered no valid calculators don't execute command
335 if (sumCalculators.size() == 0) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
337 ofstream outputFileHandle;
338 m->openOutputFile(fileNameRoot, outputFileHandle);
339 outputFileHandle << "label";
341 input = new InputData(inputFileNames[p], format);
342 sabund = input->getSAbundVector();
343 string lastLabel = sabund->getLabel();
345 for(int i=0;i<sumCalculators.size();i++){
346 if(sumCalculators[i]->getCols() == 1){
347 outputFileHandle << '\t' << sumCalculators[i]->getName();
351 outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
355 outputFileHandle << endl;
357 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
358 set<string> processedLabels;
359 set<string> userLabels = labels;
361 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; }
363 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
365 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; }
367 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
369 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
370 processedLabels.insert(sabund->getLabel());
371 userLabels.erase(sabund->getLabel());
373 outputFileHandle << sabund->getLabel();
374 for(int i=0;i<sumCalculators.size();i++){
375 vector<double> data = sumCalculators[i]->getValues(sabund);
377 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; }
379 outputFileHandle << '\t';
380 sumCalculators[i]->print(outputFileHandle);
382 outputFileHandle << endl;
386 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
387 string saveLabel = sabund->getLabel();
390 sabund = input->getSAbundVector(lastLabel);
392 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
393 processedLabels.insert(sabund->getLabel());
394 userLabels.erase(sabund->getLabel());
396 outputFileHandle << sabund->getLabel();
397 for(int i=0;i<sumCalculators.size();i++){
398 vector<double> data = sumCalculators[i]->getValues(sabund);
400 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; }
402 outputFileHandle << '\t';
403 sumCalculators[i]->print(outputFileHandle);
405 outputFileHandle << endl;
408 //restore real lastlabel to save below
409 sabund->setLabel(saveLabel);
412 lastLabel = sabund->getLabel();
415 sabund = input->getSAbundVector();
418 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; }
420 //output error messages about any remaining user labels
421 set<string>::iterator it;
422 bool needToRun = false;
423 for (it = userLabels.begin(); it != userLabels.end(); it++) {
424 m->mothurOut("Your file does not include the label " + *it);
425 if (processedLabels.count(lastLabel) != 1) {
426 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
429 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
433 //run last label if you need to
434 if (needToRun == true) {
435 if (sabund != NULL) { delete sabund; }
436 sabund = input->getSAbundVector(lastLabel);
438 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
439 outputFileHandle << sabund->getLabel();
440 for(int i=0;i<sumCalculators.size();i++){
441 vector<double> data = sumCalculators[i]->getValues(sabund);
443 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; }
445 outputFileHandle << '\t';
446 sumCalculators[i]->print(outputFileHandle);
448 outputFileHandle << endl;
453 outputFileHandle.close();
455 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; }
459 for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; }
462 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
464 //create summary file containing all the groups data for each label - this function just combines the info from the files already created.
465 if ((sharedfile != "") && (groupMode)) { outputNames.push_back(createGroupSummaryFile(numLines, numCols, outputNames)); }
467 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
469 m->mothurOutEndLine();
470 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
471 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
472 m->mothurOutEndLine();
476 catch(exception& e) {
477 m->errorOut(e, "SummaryCommand", "execute");
481 //**********************************************************************************************************************
482 vector<string> SummaryCommand::parseSharedFile(string filename) {
484 vector<string> filenames;
486 map<string, ofstream*> filehandles;
487 map<string, ofstream*>::iterator it3;
489 input = new InputData(filename, "sharedfile");
490 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
492 string sharedFileRoot = m->getRootName(filename);
494 //clears file before we start to write to it below
495 for (int i=0; i<lookup.size(); i++) {
496 remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
497 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
501 for (int i=0; i<lookup.size(); i++) {
503 filehandles[lookup[i]->getGroup()] = temp;
504 groups.push_back(lookup[i]->getGroup());
507 while(lookup[0] != NULL) {
509 for (int i = 0; i < lookup.size(); i++) {
510 RAbundVector rav = lookup[i]->getRAbundVector();
511 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
512 rav.print(*(filehandles[lookup[i]->getGroup()]));
513 (*(filehandles[lookup[i]->getGroup()])).close();
516 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
517 lookup = input->getSharedRAbundVectors();
521 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
529 catch(exception& e) {
530 m->errorOut(e, "SummaryCommand", "parseSharedFile");
534 //**********************************************************************************************************************
535 string SummaryCommand::createGroupSummaryFile(int numLines, int numCols, vector<string>& outputNames) {
539 string combineFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "groups.summary";
542 m->openOutputFile(combineFileName, out);
544 //open each groups summary file
545 string newLabel = "";
546 map<string, vector<string> > files;
547 for (int i=0; i<outputNames.size(); i++) {
548 vector<string> thisFilesLines;
551 m->openInputFile(outputNames[i], temp);
553 //read through first line - labels
555 if (i == 0) { //we want to save the labels to output below
556 for (int j = 0; j < numCols+1; j++) {
559 if (j == 1) { newLabel += "group\t" + tempLabel + '\t';
560 }else{ newLabel += tempLabel + '\t'; }
562 }else{ for (int j = 0; j < numCols+1; j++) { temp >> tempLabel; } }
567 for (int k = 0; k < numLines; k++) {
569 string thisLine = "";
572 for (int j = 0; j < numCols+1; j++) {
576 if (j == 1) { thisLine += groups[i] + "\t" + tempLabel + "\t"; }
577 else{ thisLine += tempLabel + "\t"; }
582 thisFilesLines.push_back(thisLine);
587 files[outputNames[i]] = thisFilesLines;
590 remove(outputNames[i].c_str());
593 //output label line to new file
594 out << newLabel << endl;
597 for (int k = 0; k < numLines; k++) {
599 //grab summary data for each group
600 for (int i=0; i<outputNames.size(); i++) {
601 out << files[outputNames[i]][k];
609 //return combine file name
610 return combineFileName;
613 catch(exception& e) {
614 m->errorOut(e, "SummaryCommand", "createGroupSummaryFile");
618 //**********************************************************************************************************************