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::getValidParameters(){
40 string Array[] = {"label","calc","abund","size","outputdir","groupmode","inputdir"};
41 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
45 m->errorOut(e, "SummaryCommand", "getValidParameters");
49 //**********************************************************************************************************************
50 SummaryCommand::SummaryCommand(){
52 abort = true; calledHelp = true;
53 vector<string> tempOutNames;
54 outputTypes["summary"] = tempOutNames;
57 m->errorOut(e, "SummaryCommand", "SummaryCommand");
61 //**********************************************************************************************************************
62 vector<string> SummaryCommand::getRequiredParameters(){
64 vector<string> myArray;
68 m->errorOut(e, "SummaryCommand", "getRequiredParameters");
72 //**********************************************************************************************************************
73 vector<string> SummaryCommand::getRequiredFiles(){
75 string AlignArray[] = {"shared","list","rabund","sabund","or"};
76 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
80 m->errorOut(e, "SummaryCommand", "getRequiredFiles");
84 //**********************************************************************************************************************
86 SummaryCommand::SummaryCommand(string option) {
88 globaldata = GlobalData::getInstance();
89 abort = false; calledHelp = false;
94 //allow user to run help
95 if(option == "help") { validCalculator = new ValidCalculators(); help(); delete validCalculator; abort = true; calledHelp = true; }
98 //valid paramters for this command
99 string Array[] = {"label","calc","abund","size","outputdir","groupmode","inputdir"};
100 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
102 OptionParser parser(option);
103 map<string,string> parameters = parser.getParameters();
105 ValidParameters validParameter;
107 //check to make sure all parameters are valid for command
108 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
109 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
112 //initialize outputTypes
113 vector<string> tempOutNames;
114 outputTypes["summary"] = tempOutNames;
116 //make sure the user has already run the read.otu command
117 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 summary.single command."); m->mothurOutEndLine(); abort = true; }
119 //if the user changes the output directory command factory will send this info to us in the output parameter
120 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
122 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
125 //check for optional parameter and set defaults
126 // ...at some point should added some additional type checking...
127 label = validParameter.validFile(parameters, "label", false);
128 if (label == "not found") { label = ""; }
130 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
131 else { allLines = 1; }
134 //if the user has not specified any labels use the ones from read.otu
136 allLines = globaldata->allLines;
137 labels = globaldata->labels;
140 calc = validParameter.validFile(parameters, "calc", false);
141 if (calc == "not found") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
143 if (calc == "default") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
145 m->splitAtDash(calc, Estimators);
148 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
149 convert(temp, abund);
151 temp = validParameter.validFile(parameters, "size", false); if (temp == "not found") { temp = "0"; }
154 temp = validParameter.validFile(parameters, "groupmode", false); if (temp == "not found") { temp = "T"; }
155 groupMode = m->isTrue(temp);
160 catch(exception& e) {
161 m->errorOut(e, "SummaryCommand", "SummaryCommand");
165 //**********************************************************************************************************************
167 void SummaryCommand::help(){
169 m->mothurOut("The summary.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION.\n");
170 m->mothurOut("The summary.single command can be executed after a successful cluster command. It will use the .list file from the output of the cluster.\n");
171 m->mothurOut("The summary.single command parameters are label, calc, abund and groupmode. No parameters are required.\n");
172 m->mothurOut("The summary.single command should be in the following format: \n");
173 m->mothurOut("summary.single(label=yourLabel, calc=yourEstimators).\n");
174 m->mothurOut("Example summary.single(label=unique-.01-.03, calc=sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson).\n");
175 validCalculator->printCalc("summary", cout);
176 m->mothurOut("The default value calc is sobs-chao-ace-jack-shannon-npshannon-simpson\n");
177 m->mothurOut("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");
178 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
179 m->mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabels).\n\n");
181 catch(exception& e) {
182 m->errorOut(e, "SummaryCommand", "help");
187 //**********************************************************************************************************************
189 SummaryCommand::~SummaryCommand(){}
191 //**********************************************************************************************************************
193 int SummaryCommand::execute(){
196 if (abort == true) { if (calledHelp) { return 0; } return 2; }
198 string hadShared = "";
199 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName); }
200 else { hadShared = globaldata->getSharedFile(); inputFileNames = parseSharedFile(globaldata->getSharedFile()); globaldata->setFormat("rabund"); }
202 if (m->control_pressed) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } return 0; }
207 for (int p = 0; p < inputFileNames.size(); p++) {
212 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p])) + "summary";
213 globaldata->inputFileName = inputFileNames[p];
214 outputNames.push_back(fileNameRoot); outputTypes["summary"].push_back(fileNameRoot);
216 if (inputFileNames.size() > 1) {
217 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
220 sumCalculators.clear();
222 validCalculator = new ValidCalculators();
224 for (int i=0; i<Estimators.size(); i++) {
225 if (validCalculator->isValidCalculator("summary", Estimators[i]) == true) {
226 if(Estimators[i] == "sobs"){
227 sumCalculators.push_back(new Sobs());
228 }else if(Estimators[i] == "chao"){
229 sumCalculators.push_back(new Chao1());
230 }else if(Estimators[i] == "coverage"){
231 sumCalculators.push_back(new Coverage());
232 }else if(Estimators[i] == "geometric"){
233 sumCalculators.push_back(new Geom());
234 }else if(Estimators[i] == "logseries"){
235 sumCalculators.push_back(new LogSD());
236 }else if(Estimators[i] == "qstat"){
237 sumCalculators.push_back(new QStat());
238 }else if(Estimators[i] == "bergerparker"){
239 sumCalculators.push_back(new BergerParker());
240 }else if(Estimators[i] == "bstick"){
241 sumCalculators.push_back(new BStick());
242 }else if(Estimators[i] == "ace"){
245 sumCalculators.push_back(new Ace(abund));
246 }else if(Estimators[i] == "jack"){
247 sumCalculators.push_back(new Jackknife());
248 }else if(Estimators[i] == "shannon"){
249 sumCalculators.push_back(new Shannon());
250 }else if(Estimators[i] == "shannoneven"){
251 sumCalculators.push_back(new ShannonEven());
252 }else if(Estimators[i] == "npshannon"){
253 sumCalculators.push_back(new NPShannon());
254 }else if(Estimators[i] == "heip"){
255 sumCalculators.push_back(new Heip());
256 }else if(Estimators[i] == "smithwilson"){
257 sumCalculators.push_back(new SmithWilson());
258 }else if(Estimators[i] == "simpson"){
259 sumCalculators.push_back(new Simpson());
260 }else if(Estimators[i] == "simpsoneven"){
261 sumCalculators.push_back(new SimpsonEven());
262 }else if(Estimators[i] == "invsimpson"){
263 sumCalculators.push_back(new InvSimpson());
264 }else if(Estimators[i] == "bootstrap"){
265 sumCalculators.push_back(new Bootstrap());
266 }else if (Estimators[i] == "nseqs") {
267 sumCalculators.push_back(new NSeqs());
268 }else if (Estimators[i] == "goodscoverage") {
269 sumCalculators.push_back(new GoodsCoverage());
270 }else if (Estimators[i] == "efron") {
271 sumCalculators.push_back(new Efron(size));
272 }else if (Estimators[i] == "boneh") {
273 sumCalculators.push_back(new Boneh(size));
274 }else if (Estimators[i] == "solow") {
275 sumCalculators.push_back(new Solow(size));
276 }else if (Estimators[i] == "shen") {
277 sumCalculators.push_back(new Shen(size, abund));
282 //if the users entered no valid calculators don't execute command
283 if (sumCalculators.size() == 0) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } return 0; }
285 ofstream outputFileHandle;
286 m->openOutputFile(fileNameRoot, outputFileHandle);
287 outputFileHandle << "label";
289 read = new ReadOTUFile(globaldata->inputFileName);
290 read->read(&*globaldata);
292 sabund = globaldata->sabund;
293 string lastLabel = sabund->getLabel();
294 input = globaldata->ginput;
296 for(int i=0;i<sumCalculators.size();i++){
297 if(sumCalculators[i]->getCols() == 1){
298 outputFileHandle << '\t' << sumCalculators[i]->getName();
302 outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
306 outputFileHandle << endl;
308 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
309 set<string> processedLabels;
310 set<string> userLabels = labels;
312 if (m->control_pressed) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } 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 validCalculator; delete read; delete sabund; globaldata->sabund = NULL; delete input; globaldata->ginput = NULL; return 0; }
314 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
316 if (m->control_pressed) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } 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 validCalculator; delete read; delete sabund; globaldata->sabund = NULL; delete input; globaldata->ginput = NULL; return 0; }
318 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
320 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
321 processedLabels.insert(sabund->getLabel());
322 userLabels.erase(sabund->getLabel());
324 outputFileHandle << sabund->getLabel();
325 for(int i=0;i<sumCalculators.size();i++){
326 vector<double> data = sumCalculators[i]->getValues(sabund);
328 if (m->control_pressed) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } 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 validCalculator; delete read; delete sabund; globaldata->sabund = NULL; delete input; globaldata->ginput = NULL; return 0; }
330 outputFileHandle << '\t';
331 sumCalculators[i]->print(outputFileHandle);
333 outputFileHandle << endl;
337 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
338 string saveLabel = sabund->getLabel();
341 sabund = input->getSAbundVector(lastLabel);
343 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
344 processedLabels.insert(sabund->getLabel());
345 userLabels.erase(sabund->getLabel());
347 outputFileHandle << sabund->getLabel();
348 for(int i=0;i<sumCalculators.size();i++){
349 vector<double> data = sumCalculators[i]->getValues(sabund);
351 if (m->control_pressed) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } 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 validCalculator; delete read; delete sabund; globaldata->sabund = NULL; delete input; globaldata->ginput = NULL; return 0; }
353 outputFileHandle << '\t';
354 sumCalculators[i]->print(outputFileHandle);
356 outputFileHandle << endl;
359 //restore real lastlabel to save below
360 sabund->setLabel(saveLabel);
363 lastLabel = sabund->getLabel();
366 sabund = input->getSAbundVector();
369 if (m->control_pressed) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } 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 validCalculator; delete read; delete input; globaldata->ginput = NULL; return 0; }
371 //output error messages about any remaining user labels
372 set<string>::iterator it;
373 bool needToRun = false;
374 for (it = userLabels.begin(); it != userLabels.end(); it++) {
375 m->mothurOut("Your file does not include the label " + *it);
376 if (processedLabels.count(lastLabel) != 1) {
377 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
380 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
384 //run last label if you need to
385 if (needToRun == true) {
386 if (sabund != NULL) { delete sabund; }
387 sabund = input->getSAbundVector(lastLabel);
389 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
390 outputFileHandle << sabund->getLabel();
391 for(int i=0;i<sumCalculators.size();i++){
392 vector<double> data = sumCalculators[i]->getValues(sabund);
394 if (m->control_pressed) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } 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 validCalculator; delete read; delete sabund; globaldata->sabund = NULL; delete input; globaldata->ginput = NULL; return 0; }
396 outputFileHandle << '\t';
397 sumCalculators[i]->print(outputFileHandle);
399 outputFileHandle << endl;
404 outputFileHandle.close();
406 if (m->control_pressed) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } 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 validCalculator; delete read; delete input; globaldata->ginput = NULL; return 0; }
409 delete input; globaldata->ginput = NULL;
411 delete validCalculator;
412 globaldata->sabund = NULL;
413 for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; }
416 if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); }
418 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
420 //create summary file containing all the groups data for each label - this function just combines the info from the files already created.
421 if ((hadShared != "") && (groupMode)) { outputNames.push_back(createGroupSummaryFile(numLines, numCols, outputNames)); }
423 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
425 m->mothurOutEndLine();
426 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
427 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
428 m->mothurOutEndLine();
432 catch(exception& e) {
433 m->errorOut(e, "SummaryCommand", "execute");
437 //**********************************************************************************************************************
438 vector<string> SummaryCommand::parseSharedFile(string filename) {
440 vector<string> filenames;
442 map<string, ofstream*> filehandles;
443 map<string, ofstream*>::iterator it3;
447 read = new ReadOTUFile(filename);
448 read->read(&*globaldata);
450 input = globaldata->ginput;
451 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
453 string sharedFileRoot = m->getRootName(filename);
455 //clears file before we start to write to it below
456 for (int i=0; i<lookup.size(); i++) {
457 remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
458 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
462 for (int i=0; i<lookup.size(); i++) {
464 filehandles[lookup[i]->getGroup()] = temp;
465 groups.push_back(lookup[i]->getGroup());
468 while(lookup[0] != NULL) {
470 for (int i = 0; i < lookup.size(); i++) {
471 RAbundVector rav = lookup[i]->getRAbundVector();
472 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
473 rav.print(*(filehandles[lookup[i]->getGroup()]));
474 (*(filehandles[lookup[i]->getGroup()])).close();
477 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
478 lookup = input->getSharedRAbundVectors();
482 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
487 globaldata->ginput = NULL;
491 catch(exception& e) {
492 m->errorOut(e, "SummaryCommand", "parseSharedFile");
496 //**********************************************************************************************************************
497 string SummaryCommand::createGroupSummaryFile(int numLines, int numCols, vector<string>& outputNames) {
501 string combineFileName = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + "groups.summary";
504 m->openOutputFile(combineFileName, out);
506 //open each groups summary file
507 string newLabel = "";
508 map<string, vector<string> > files;
509 for (int i=0; i<outputNames.size(); i++) {
510 vector<string> thisFilesLines;
513 m->openInputFile(outputNames[i], temp);
515 //read through first line - labels
517 if (i == 0) { //we want to save the labels to output below
518 for (int j = 0; j < numCols+1; j++) {
521 if (j == 1) { newLabel += "group\t" + tempLabel + '\t';
522 }else{ newLabel += tempLabel + '\t'; }
524 }else{ for (int j = 0; j < numCols+1; j++) { temp >> tempLabel; } }
529 for (int k = 0; k < numLines; k++) {
531 string thisLine = "";
534 for (int j = 0; j < numCols+1; j++) {
538 if (j == 1) { thisLine += groups[i] + "\t" + tempLabel + "\t"; }
539 else{ thisLine += tempLabel + "\t"; }
544 thisFilesLines.push_back(thisLine);
549 files[outputNames[i]] = thisFilesLines;
552 remove(outputNames[i].c_str());
555 //output label line to new file
556 out << newLabel << endl;
559 for (int k = 0; k < numLines; k++) {
561 //grab summary data for each group
562 for (int i=0; i<outputNames.size(); i++) {
563 out << files[outputNames[i]][k];
571 //return combine file name
572 return combineFileName;
575 catch(exception& e) {
576 m->errorOut(e, "SummaryCommand", "createGroupSummaryFile");
580 //**********************************************************************************************************************