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 "npshannon.h"
19 #include "jackknife.h"
23 #include "bergerparker.h"
25 #include "goodscoverage.h"
32 //**********************************************************************************************************************
34 SummaryCommand::SummaryCommand(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[] = {"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 //make sure the user has already run the read.otu command
61 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; }
63 //if the user changes the output directory command factory will send this info to us in the output parameter
64 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
66 outputDir += hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
69 //check for optional parameter and set defaults
70 // ...at some point should added some additional type checking...
71 label = validParameter.validFile(parameters, "label", false);
72 if (label == "not found") { label = ""; }
74 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
75 else { allLines = 1; }
78 //if the user has not specified any labels use the ones from read.otu
80 allLines = globaldata->allLines;
81 labels = globaldata->labels;
84 calc = validParameter.validFile(parameters, "calc", false);
85 if (calc == "not found") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
87 if (calc == "default") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
89 splitAtDash(calc, Estimators);
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, "SummaryCommand", "SummaryCommand");
105 //**********************************************************************************************************************
107 void SummaryCommand::help(){
109 m->mothurOut("The summary.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION.\n");
110 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");
111 m->mothurOut("The summary.single command parameters are label, calc, abund. No parameters are required.\n");
112 m->mothurOut("The summary.single command should be in the following format: \n");
113 m->mothurOut("summary.single(label=yourLabel, calc=yourEstimators).\n");
114 m->mothurOut("Example summary.single(label=unique-.01-.03, calc=sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson).\n");
115 validCalculator->printCalc("summary", cout);
116 m->mothurOut("The default value calc is sobs-chao-ace-jack-shannon-npshannon-simpson\n");
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. label), '=' and parameters (i.e.yourLabels).\n\n");
120 catch(exception& e) {
121 m->errorOut(e, "SummaryCommand", "help");
126 //**********************************************************************************************************************
128 SummaryCommand::~SummaryCommand(){}
130 //**********************************************************************************************************************
132 int SummaryCommand::execute(){
135 if (abort == true) { return 0; }
137 vector<string> outputNames;
139 string hadShared = "";
140 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName); }
141 else { hadShared = globaldata->getSharedFile(); inputFileNames = parseSharedFile(globaldata->getSharedFile()); globaldata->setFormat("rabund"); }
143 if (m->control_pressed) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } return 0; }
145 for (int p = 0; p < inputFileNames.size(); p++) {
147 string fileNameRoot = outputDir + getRootName(getSimpleName(inputFileNames[p])) + "summary";
148 globaldata->inputFileName = inputFileNames[p];
149 outputNames.push_back(fileNameRoot);
151 if (inputFileNames.size() > 1) {
152 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
155 sumCalculators.clear();
157 validCalculator = new ValidCalculators();
159 for (int i=0; i<Estimators.size(); i++) {
160 if (validCalculator->isValidCalculator("summary", Estimators[i]) == true) {
161 if(Estimators[i] == "sobs"){
162 sumCalculators.push_back(new Sobs());
163 }else if(Estimators[i] == "chao"){
164 sumCalculators.push_back(new Chao1());
165 }else if(Estimators[i] == "coverage"){
166 sumCalculators.push_back(new Coverage());
167 }else if(Estimators[i] == "geometric"){
168 sumCalculators.push_back(new Geom());
169 }else if(Estimators[i] == "logseries"){
170 sumCalculators.push_back(new LogSD());
171 }else if(Estimators[i] == "qstat"){
172 sumCalculators.push_back(new QStat());
173 }else if(Estimators[i] == "bergerparker"){
174 sumCalculators.push_back(new BergerParker());
175 }else if(Estimators[i] == "bstick"){
176 sumCalculators.push_back(new BStick());
177 }else if(Estimators[i] == "ace"){
180 sumCalculators.push_back(new Ace(abund));
181 }else if(Estimators[i] == "jack"){
182 sumCalculators.push_back(new Jackknife());
183 }else if(Estimators[i] == "shannon"){
184 sumCalculators.push_back(new Shannon());
185 }else if(Estimators[i] == "npshannon"){
186 sumCalculators.push_back(new NPShannon());
187 }else if(Estimators[i] == "simpson"){
188 sumCalculators.push_back(new Simpson());
189 }else if(Estimators[i] == "bootstrap"){
190 sumCalculators.push_back(new Bootstrap());
191 }else if (Estimators[i] == "nseqs") {
192 sumCalculators.push_back(new NSeqs());
193 }else if (Estimators[i] == "goodscoverage") {
194 sumCalculators.push_back(new GoodsCoverage());
195 }else if (Estimators[i] == "efron") {
196 sumCalculators.push_back(new Efron(size));
197 }else if (Estimators[i] == "boneh") {
198 sumCalculators.push_back(new Boneh(size));
199 }else if (Estimators[i] == "solow") {
200 sumCalculators.push_back(new Solow(size));
201 }else if (Estimators[i] == "shen") {
202 sumCalculators.push_back(new Shen(size, abund));
207 //if the users entered no valid calculators don't execute command
208 if (sumCalculators.size() == 0) { if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); } return 0; }
210 ofstream outputFileHandle;
211 openOutputFile(fileNameRoot, outputFileHandle);
212 outputFileHandle << "label";
214 read = new ReadOTUFile(globaldata->inputFileName);
215 read->read(&*globaldata);
217 sabund = globaldata->sabund;
218 string lastLabel = sabund->getLabel();
219 input = globaldata->ginput;
221 for(int i=0;i<sumCalculators.size();i++){
222 if(sumCalculators[i]->getCols() == 1){
223 outputFileHandle << '\t' << sumCalculators[i]->getName();
226 outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
229 outputFileHandle << endl;
231 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
232 set<string> processedLabels;
233 set<string> userLabels = labels;
235 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; }
237 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
239 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; }
241 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
243 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
244 processedLabels.insert(sabund->getLabel());
245 userLabels.erase(sabund->getLabel());
247 outputFileHandle << sabund->getLabel();
248 for(int i=0;i<sumCalculators.size();i++){
249 vector<double> data = sumCalculators[i]->getValues(sabund);
251 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; }
253 outputFileHandle << '\t';
254 sumCalculators[i]->print(outputFileHandle);
256 outputFileHandle << endl;
259 if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
260 string saveLabel = sabund->getLabel();
263 sabund = input->getSAbundVector(lastLabel);
265 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
266 processedLabels.insert(sabund->getLabel());
267 userLabels.erase(sabund->getLabel());
269 outputFileHandle << sabund->getLabel();
270 for(int i=0;i<sumCalculators.size();i++){
271 vector<double> data = sumCalculators[i]->getValues(sabund);
273 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; }
275 outputFileHandle << '\t';
276 sumCalculators[i]->print(outputFileHandle);
278 outputFileHandle << endl;
280 //restore real lastlabel to save below
281 sabund->setLabel(saveLabel);
284 lastLabel = sabund->getLabel();
287 sabund = input->getSAbundVector();
290 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; }
292 //output error messages about any remaining user labels
293 set<string>::iterator it;
294 bool needToRun = false;
295 for (it = userLabels.begin(); it != userLabels.end(); it++) {
296 m->mothurOut("Your file does not include the label " + *it);
297 if (processedLabels.count(lastLabel) != 1) {
298 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
301 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
305 //run last label if you need to
306 if (needToRun == true) {
307 if (sabund != NULL) { delete sabund; }
308 sabund = input->getSAbundVector(lastLabel);
310 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
311 outputFileHandle << sabund->getLabel();
312 for(int i=0;i<sumCalculators.size();i++){
313 vector<double> data = sumCalculators[i]->getValues(sabund);
315 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; }
317 outputFileHandle << '\t';
318 sumCalculators[i]->print(outputFileHandle);
320 outputFileHandle << endl;
324 outputFileHandle.close();
326 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; }
329 delete input; globaldata->ginput = NULL;
331 delete validCalculator;
332 globaldata->sabund = NULL;
333 for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; }
336 if (hadShared != "") { globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile"); }
338 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
340 m->mothurOutEndLine();
341 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
342 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
343 m->mothurOutEndLine();
347 catch(exception& e) {
348 m->errorOut(e, "SummaryCommand", "execute");
352 //**********************************************************************************************************************
353 vector<string> SummaryCommand::parseSharedFile(string filename) {
355 vector<string> filenames;
357 map<string, ofstream*> filehandles;
358 map<string, ofstream*>::iterator it3;
362 read = new ReadOTUFile(filename);
363 read->read(&*globaldata);
365 input = globaldata->ginput;
366 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
368 string sharedFileRoot = getRootName(filename);
370 //clears file before we start to write to it below
371 for (int i=0; i<lookup.size(); i++) {
372 remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
373 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
377 for (int i=0; i<lookup.size(); i++) {
379 filehandles[lookup[i]->getGroup()] = temp;
380 groups.push_back(lookup[i]->getGroup());
383 while(lookup[0] != NULL) {
385 for (int i = 0; i < lookup.size(); i++) {
386 RAbundVector rav = lookup[i]->getRAbundVector();
387 openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
388 rav.print(*(filehandles[lookup[i]->getGroup()]));
389 (*(filehandles[lookup[i]->getGroup()])).close();
392 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
393 lookup = input->getSharedRAbundVectors();
397 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
402 globaldata->ginput = NULL;
406 catch(exception& e) {
407 m->errorOut(e, "SummaryCommand", "parseSharedFile");
411 //**********************************************************************************************************************