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 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName); }
140 else { inputFileNames = parseSharedFile(globaldata->getSharedFile()); globaldata->setFormat("rabund"); }
142 for (int p = 0; p < inputFileNames.size(); p++) {
144 string fileNameRoot = outputDir + getRootName(getSimpleName(inputFileNames[p])) + "summary";
145 globaldata->inputFileName = inputFileNames[p];
146 outputNames.push_back(fileNameRoot);
148 if (inputFileNames.size() > 1) {
149 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
152 sumCalculators.clear();
154 validCalculator = new ValidCalculators();
156 for (int i=0; i<Estimators.size(); i++) {
157 if (validCalculator->isValidCalculator("summary", Estimators[i]) == true) {
158 if(Estimators[i] == "sobs"){
159 sumCalculators.push_back(new Sobs());
160 }else if(Estimators[i] == "chao"){
161 sumCalculators.push_back(new Chao1());
162 }else if(Estimators[i] == "coverage"){
163 sumCalculators.push_back(new Coverage());
164 }else if(Estimators[i] == "geometric"){
165 sumCalculators.push_back(new Geom());
166 }else if(Estimators[i] == "logseries"){
167 sumCalculators.push_back(new LogSD());
168 }else if(Estimators[i] == "qstat"){
169 sumCalculators.push_back(new QStat());
170 }else if(Estimators[i] == "bergerparker"){
171 sumCalculators.push_back(new BergerParker());
172 }else if(Estimators[i] == "bstick"){
173 sumCalculators.push_back(new BStick());
174 }else if(Estimators[i] == "ace"){
177 sumCalculators.push_back(new Ace(abund));
178 }else if(Estimators[i] == "jack"){
179 sumCalculators.push_back(new Jackknife());
180 }else if(Estimators[i] == "shannon"){
181 sumCalculators.push_back(new Shannon());
182 }else if(Estimators[i] == "npshannon"){
183 sumCalculators.push_back(new NPShannon());
184 }else if(Estimators[i] == "simpson"){
185 sumCalculators.push_back(new Simpson());
186 }else if(Estimators[i] == "bootstrap"){
187 sumCalculators.push_back(new Bootstrap());
188 }else if (Estimators[i] == "nseqs") {
189 sumCalculators.push_back(new NSeqs());
190 }else if (Estimators[i] == "goodscoverage") {
191 sumCalculators.push_back(new GoodsCoverage());
192 }else if (Estimators[i] == "efron") {
193 sumCalculators.push_back(new Efron(size));
194 }else if (Estimators[i] == "boneh") {
195 sumCalculators.push_back(new Boneh(size));
196 }else if (Estimators[i] == "solow") {
197 sumCalculators.push_back(new Solow(size));
198 }else if (Estimators[i] == "shen") {
199 sumCalculators.push_back(new Shen(size, abund));
204 //if the users entered no valid calculators don't execute command
205 if (sumCalculators.size() == 0) { return 0; }
207 ofstream outputFileHandle;
208 openOutputFile(fileNameRoot, outputFileHandle);
209 outputFileHandle << "label";
211 read = new ReadOTUFile(globaldata->inputFileName);
212 read->read(&*globaldata);
214 sabund = globaldata->sabund;
215 string lastLabel = sabund->getLabel();
216 input = globaldata->ginput;
218 for(int i=0;i<sumCalculators.size();i++){
219 if(sumCalculators[i]->getCols() == 1){
220 outputFileHandle << '\t' << sumCalculators[i]->getName();
223 outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
226 outputFileHandle << endl;
228 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
229 set<string> processedLabels;
230 set<string> userLabels = labels;
232 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
234 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
236 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
237 processedLabels.insert(sabund->getLabel());
238 userLabels.erase(sabund->getLabel());
240 outputFileHandle << sabund->getLabel();
241 for(int i=0;i<sumCalculators.size();i++){
242 vector<double> data = sumCalculators[i]->getValues(sabund);
243 outputFileHandle << '\t';
244 sumCalculators[i]->print(outputFileHandle);
246 outputFileHandle << endl;
249 if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
250 string saveLabel = sabund->getLabel();
253 sabund = input->getSAbundVector(lastLabel);
255 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
256 processedLabels.insert(sabund->getLabel());
257 userLabels.erase(sabund->getLabel());
259 outputFileHandle << sabund->getLabel();
260 for(int i=0;i<sumCalculators.size();i++){
261 vector<double> data = sumCalculators[i]->getValues(sabund);
262 outputFileHandle << '\t';
263 sumCalculators[i]->print(outputFileHandle);
265 outputFileHandle << endl;
267 //restore real lastlabel to save below
268 sabund->setLabel(saveLabel);
271 lastLabel = sabund->getLabel();
274 sabund = input->getSAbundVector();
277 //output error messages about any remaining user labels
278 set<string>::iterator it;
279 bool needToRun = false;
280 for (it = userLabels.begin(); it != userLabels.end(); it++) {
281 m->mothurOut("Your file does not include the label " + *it);
282 if (processedLabels.count(lastLabel) != 1) {
283 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
286 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
290 //run last label if you need to
291 if (needToRun == true) {
292 if (sabund != NULL) { delete sabund; }
293 sabund = input->getSAbundVector(lastLabel);
295 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
296 outputFileHandle << sabund->getLabel();
297 for(int i=0;i<sumCalculators.size();i++){
298 vector<double> data = sumCalculators[i]->getValues(sabund);
299 outputFileHandle << '\t';
300 sumCalculators[i]->print(outputFileHandle);
302 outputFileHandle << endl;
306 outputFileHandle.close();
308 delete input; globaldata->ginput = NULL;
310 delete validCalculator;
311 globaldata->sabund = NULL;
314 m->mothurOutEndLine();
315 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
316 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
317 m->mothurOutEndLine();
321 catch(exception& e) {
322 m->errorOut(e, "SummaryCommand", "execute");
326 //**********************************************************************************************************************
327 vector<string> SummaryCommand::parseSharedFile(string filename) {
329 vector<string> filenames;
331 map<string, ofstream*> filehandles;
332 map<string, ofstream*>::iterator it3;
336 read = new ReadOTUFile(filename);
337 read->read(&*globaldata);
339 input = globaldata->ginput;
340 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
342 string sharedFileRoot = getRootName(filename);
344 //clears file before we start to write to it below
345 for (int i=0; i<lookup.size(); i++) {
346 remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
347 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
351 for (int i=0; i<lookup.size(); i++) {
353 filehandles[lookup[i]->getGroup()] = temp;
354 groups.push_back(lookup[i]->getGroup());
357 while(lookup[0] != NULL) {
359 for (int i = 0; i < lookup.size(); i++) {
360 RAbundVector rav = lookup[i]->getRAbundVector();
361 openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
362 rav.print(*(filehandles[lookup[i]->getGroup()]));
363 (*(filehandles[lookup[i]->getGroup()])).close();
366 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
367 lookup = input->getSharedRAbundVectors();
371 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
376 globaldata->ginput = NULL;
380 catch(exception& e) {
381 m->errorOut(e, "SummaryCommand", "parseSharedFile");
385 //**********************************************************************************************************************