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);
226 if (m->inUsersGroups("citation", Estimators)) {
227 ValidCalculators validCalc; validCalc.printCitations(Estimators);
228 //remove citation from list of calcs
229 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") { Estimators.erase(Estimators.begin()+i); break; } }
233 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
234 convert(temp, abund);
236 temp = validParameter.validFile(parameters, "size", false); if (temp == "not found") { temp = "0"; }
239 temp = validParameter.validFile(parameters, "groupmode", false); if (temp == "not found") { temp = "T"; }
240 groupMode = m->isTrue(temp);
245 catch(exception& e) {
246 m->errorOut(e, "SummaryCommand", "SummaryCommand");
250 //**********************************************************************************************************************
252 int SummaryCommand::execute(){
255 if (abort == true) { if (calledHelp) { return 0; } return 2; }
257 if ((format != "sharedfile")) { inputFileNames.push_back(inputfile); }
258 else { inputFileNames = parseSharedFile(sharedfile); format = "rabund"; }
260 if (m->control_pressed) { return 0; }
265 for (int p = 0; p < inputFileNames.size(); p++) {
270 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p])) + "summary";
271 outputNames.push_back(fileNameRoot); outputTypes["summary"].push_back(fileNameRoot);
273 if (inputFileNames.size() > 1) {
274 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
277 sumCalculators.clear();
279 ValidCalculators validCalculator;
281 for (int i=0; i<Estimators.size(); i++) {
282 if (validCalculator.isValidCalculator("summary", Estimators[i]) == true) {
283 if(Estimators[i] == "sobs"){
284 sumCalculators.push_back(new Sobs());
285 }else if(Estimators[i] == "chao"){
286 sumCalculators.push_back(new Chao1());
287 }else if(Estimators[i] == "coverage"){
288 sumCalculators.push_back(new Coverage());
289 }else if(Estimators[i] == "geometric"){
290 sumCalculators.push_back(new Geom());
291 }else if(Estimators[i] == "logseries"){
292 sumCalculators.push_back(new LogSD());
293 }else if(Estimators[i] == "qstat"){
294 sumCalculators.push_back(new QStat());
295 }else if(Estimators[i] == "bergerparker"){
296 sumCalculators.push_back(new BergerParker());
297 }else if(Estimators[i] == "bstick"){
298 sumCalculators.push_back(new BStick());
299 }else if(Estimators[i] == "ace"){
302 sumCalculators.push_back(new Ace(abund));
303 }else if(Estimators[i] == "jack"){
304 sumCalculators.push_back(new Jackknife());
305 }else if(Estimators[i] == "shannon"){
306 sumCalculators.push_back(new Shannon());
307 }else if(Estimators[i] == "shannoneven"){
308 sumCalculators.push_back(new ShannonEven());
309 }else if(Estimators[i] == "npshannon"){
310 sumCalculators.push_back(new NPShannon());
311 }else if(Estimators[i] == "heip"){
312 sumCalculators.push_back(new Heip());
313 }else if(Estimators[i] == "smithwilson"){
314 sumCalculators.push_back(new SmithWilson());
315 }else if(Estimators[i] == "simpson"){
316 sumCalculators.push_back(new Simpson());
317 }else if(Estimators[i] == "simpsoneven"){
318 sumCalculators.push_back(new SimpsonEven());
319 }else if(Estimators[i] == "invsimpson"){
320 sumCalculators.push_back(new InvSimpson());
321 }else if(Estimators[i] == "bootstrap"){
322 sumCalculators.push_back(new Bootstrap());
323 }else if (Estimators[i] == "nseqs") {
324 sumCalculators.push_back(new NSeqs());
325 }else if (Estimators[i] == "goodscoverage") {
326 sumCalculators.push_back(new GoodsCoverage());
327 }else if (Estimators[i] == "efron") {
328 sumCalculators.push_back(new Efron(size));
329 }else if (Estimators[i] == "boneh") {
330 sumCalculators.push_back(new Boneh(size));
331 }else if (Estimators[i] == "solow") {
332 sumCalculators.push_back(new Solow(size));
333 }else if (Estimators[i] == "shen") {
334 sumCalculators.push_back(new Shen(size, abund));
339 //if the users entered no valid calculators don't execute command
340 if (sumCalculators.size() == 0) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
342 ofstream outputFileHandle;
343 m->openOutputFile(fileNameRoot, outputFileHandle);
344 outputFileHandle << "label";
346 input = new InputData(inputFileNames[p], format);
347 sabund = input->getSAbundVector();
348 string lastLabel = sabund->getLabel();
350 for(int i=0;i<sumCalculators.size();i++){
351 if(sumCalculators[i]->getCols() == 1){
352 outputFileHandle << '\t' << sumCalculators[i]->getName();
356 outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
360 outputFileHandle << endl;
362 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
363 set<string> processedLabels;
364 set<string> userLabels = labels;
366 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; }
368 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
370 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; }
372 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
374 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
375 processedLabels.insert(sabund->getLabel());
376 userLabels.erase(sabund->getLabel());
378 outputFileHandle << sabund->getLabel();
379 for(int i=0;i<sumCalculators.size();i++){
380 vector<double> data = sumCalculators[i]->getValues(sabund);
382 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; }
384 outputFileHandle << '\t';
385 sumCalculators[i]->print(outputFileHandle);
387 outputFileHandle << endl;
391 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
392 string saveLabel = sabund->getLabel();
395 sabund = input->getSAbundVector(lastLabel);
397 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
398 processedLabels.insert(sabund->getLabel());
399 userLabels.erase(sabund->getLabel());
401 outputFileHandle << sabund->getLabel();
402 for(int i=0;i<sumCalculators.size();i++){
403 vector<double> data = sumCalculators[i]->getValues(sabund);
405 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; }
407 outputFileHandle << '\t';
408 sumCalculators[i]->print(outputFileHandle);
410 outputFileHandle << endl;
413 //restore real lastlabel to save below
414 sabund->setLabel(saveLabel);
417 lastLabel = sabund->getLabel();
420 sabund = input->getSAbundVector();
423 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; }
425 //output error messages about any remaining user labels
426 set<string>::iterator it;
427 bool needToRun = false;
428 for (it = userLabels.begin(); it != userLabels.end(); it++) {
429 m->mothurOut("Your file does not include the label " + *it);
430 if (processedLabels.count(lastLabel) != 1) {
431 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
434 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
438 //run last label if you need to
439 if (needToRun == true) {
440 if (sabund != NULL) { delete sabund; }
441 sabund = input->getSAbundVector(lastLabel);
443 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
444 outputFileHandle << sabund->getLabel();
445 for(int i=0;i<sumCalculators.size();i++){
446 vector<double> data = sumCalculators[i]->getValues(sabund);
448 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; }
450 outputFileHandle << '\t';
451 sumCalculators[i]->print(outputFileHandle);
453 outputFileHandle << endl;
458 outputFileHandle.close();
460 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; }
464 for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; }
467 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
469 //create summary file containing all the groups data for each label - this function just combines the info from the files already created.
470 if ((sharedfile != "") && (groupMode)) { outputNames.push_back(createGroupSummaryFile(numLines, numCols, outputNames)); }
472 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
474 m->mothurOutEndLine();
475 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
476 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
477 m->mothurOutEndLine();
481 catch(exception& e) {
482 m->errorOut(e, "SummaryCommand", "execute");
486 //**********************************************************************************************************************
487 vector<string> SummaryCommand::parseSharedFile(string filename) {
489 vector<string> filenames;
491 map<string, ofstream*> filehandles;
492 map<string, ofstream*>::iterator it3;
494 input = new InputData(filename, "sharedfile");
495 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
497 string sharedFileRoot = m->getRootName(filename);
499 //clears file before we start to write to it below
500 for (int i=0; i<lookup.size(); i++) {
501 remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
502 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
506 for (int i=0; i<lookup.size(); i++) {
508 filehandles[lookup[i]->getGroup()] = temp;
509 groups.push_back(lookup[i]->getGroup());
512 while(lookup[0] != NULL) {
514 for (int i = 0; i < lookup.size(); i++) {
515 RAbundVector rav = lookup[i]->getRAbundVector();
516 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
517 rav.print(*(filehandles[lookup[i]->getGroup()]));
518 (*(filehandles[lookup[i]->getGroup()])).close();
521 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
522 lookup = input->getSharedRAbundVectors();
526 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
534 catch(exception& e) {
535 m->errorOut(e, "SummaryCommand", "parseSharedFile");
539 //**********************************************************************************************************************
540 string SummaryCommand::createGroupSummaryFile(int numLines, int numCols, vector<string>& outputNames) {
544 string combineFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "groups.summary";
547 m->openOutputFile(combineFileName, out);
549 //open each groups summary file
550 string newLabel = "";
551 map<string, vector<string> > files;
552 for (int i=0; i<outputNames.size(); i++) {
553 vector<string> thisFilesLines;
556 m->openInputFile(outputNames[i], temp);
558 //read through first line - labels
560 if (i == 0) { //we want to save the labels to output below
561 for (int j = 0; j < numCols+1; j++) {
564 if (j == 1) { newLabel += "group\t" + tempLabel + '\t';
565 }else{ newLabel += tempLabel + '\t'; }
567 }else{ for (int j = 0; j < numCols+1; j++) { temp >> tempLabel; } }
572 for (int k = 0; k < numLines; k++) {
574 string thisLine = "";
577 for (int j = 0; j < numCols+1; j++) {
581 if (j == 1) { thisLine += groups[i] + "\t" + tempLabel + "\t"; }
582 else{ thisLine += tempLabel + "\t"; }
587 thisFilesLines.push_back(thisLine);
592 files[outputNames[i]] = thisFilesLines;
595 remove(outputNames[i].c_str());
598 //output label line to new file
599 out << newLabel << endl;
602 for (int k = 0; k < numLines; k++) {
604 //grab summary data for each group
605 for (int i=0; i<outputNames.size(); i++) {
606 out << files[outputNames[i]][k];
614 //return combine file name
615 return combineFileName;
618 catch(exception& e) {
619 m->errorOut(e, "SummaryCommand", "createGroupSummaryFile");
623 //**********************************************************************************************************************