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() == "")) { mothurOut("You must read a list, sabund, rabund or shared file before you can use the summary.single command."); 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 errorOut(e, "SummaryCommand", "SummaryCommand");
105 //**********************************************************************************************************************
107 void SummaryCommand::help(){
109 mothurOut("The summary.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION.\n");
110 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 mothurOut("The summary.single command parameters are label, calc, abund. No parameters are required.\n");
112 mothurOut("The summary.single command should be in the following format: \n");
113 mothurOut("summary.single(label=yourLabel, calc=yourEstimators).\n");
114 mothurOut("Example summary.single(label=unique-.01-.03, calc=sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson).\n");
115 validCalculator->printCalc("summary", cout);
116 mothurOut("The default value calc is sobs-chao-ace-jack-shannon-npshannon-simpson\n");
117 mothurOut("The label parameter is used to analyze specific labels in your input.\n");
118 mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabels).\n\n");
120 catch(exception& e) {
121 errorOut(e, "SummaryCommand", "help");
126 //**********************************************************************************************************************
128 SummaryCommand::~SummaryCommand(){}
130 //**********************************************************************************************************************
132 int SummaryCommand::execute(){
135 if (abort == true) { return 0; }
137 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName); }
138 else { inputFileNames = parseSharedFile(globaldata->getSharedFile()); globaldata->setFormat("rabund"); }
140 for (int p = 0; p < inputFileNames.size(); p++) {
142 string fileNameRoot = outputDir + getRootName(getSimpleName(inputFileNames[p])) + "summary";
143 globaldata->inputFileName = inputFileNames[p];
145 if (inputFileNames.size() > 1) {
146 mothurOutEndLine(); mothurOut("Processing group " + groups[p]); mothurOutEndLine(); mothurOutEndLine();
149 sumCalculators.clear();
151 validCalculator = new ValidCalculators();
153 for (int i=0; i<Estimators.size(); i++) {
154 if (validCalculator->isValidCalculator("summary", Estimators[i]) == true) {
155 if(Estimators[i] == "sobs"){
156 sumCalculators.push_back(new Sobs());
157 }else if(Estimators[i] == "chao"){
158 sumCalculators.push_back(new Chao1());
159 }else if(Estimators[i] == "coverage"){
160 sumCalculators.push_back(new Coverage());
161 }else if(Estimators[i] == "geometric"){
162 sumCalculators.push_back(new Geom());
163 }else if(Estimators[i] == "logseries"){
164 sumCalculators.push_back(new LogSD());
165 }else if(Estimators[i] == "qstat"){
166 sumCalculators.push_back(new QStat());
167 }else if(Estimators[i] == "bergerparker"){
168 sumCalculators.push_back(new BergerParker());
169 }else if(Estimators[i] == "bstick"){
170 sumCalculators.push_back(new BStick());
171 }else if(Estimators[i] == "ace"){
174 sumCalculators.push_back(new Ace(abund));
175 }else if(Estimators[i] == "jack"){
176 sumCalculators.push_back(new Jackknife());
177 }else if(Estimators[i] == "shannon"){
178 sumCalculators.push_back(new Shannon());
179 }else if(Estimators[i] == "npshannon"){
180 sumCalculators.push_back(new NPShannon());
181 }else if(Estimators[i] == "simpson"){
182 sumCalculators.push_back(new Simpson());
183 }else if(Estimators[i] == "bootstrap"){
184 sumCalculators.push_back(new Bootstrap());
185 }else if (Estimators[i] == "nseqs") {
186 sumCalculators.push_back(new NSeqs());
187 }else if (Estimators[i] == "goodscoverage") {
188 sumCalculators.push_back(new GoodsCoverage());
189 }else if (Estimators[i] == "efron") {
190 sumCalculators.push_back(new Efron(size));
191 }else if (Estimators[i] == "boneh") {
192 sumCalculators.push_back(new Boneh(size));
193 }else if (Estimators[i] == "solow") {
194 sumCalculators.push_back(new Solow(size));
195 }else if (Estimators[i] == "shen") {
196 sumCalculators.push_back(new Shen(size, abund));
201 //if the users entered no valid calculators don't execute command
202 if (sumCalculators.size() == 0) { return 0; }
204 ofstream outputFileHandle;
205 openOutputFile(fileNameRoot, outputFileHandle);
206 outputFileHandle << "label";
208 read = new ReadOTUFile(globaldata->inputFileName);
209 read->read(&*globaldata);
211 sabund = globaldata->sabund;
212 string lastLabel = sabund->getLabel();
213 input = globaldata->ginput;
215 for(int i=0;i<sumCalculators.size();i++){
216 if(sumCalculators[i]->getCols() == 1){
217 outputFileHandle << '\t' << sumCalculators[i]->getName();
220 outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
223 outputFileHandle << endl;
225 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
226 set<string> processedLabels;
227 set<string> userLabels = labels;
229 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
231 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
233 mothurOut(sabund->getLabel()); mothurOutEndLine();
234 processedLabels.insert(sabund->getLabel());
235 userLabels.erase(sabund->getLabel());
237 outputFileHandle << sabund->getLabel();
238 for(int i=0;i<sumCalculators.size();i++){
239 vector<double> data = sumCalculators[i]->getValues(sabund);
240 outputFileHandle << '\t';
241 sumCalculators[i]->print(outputFileHandle);
243 outputFileHandle << endl;
246 if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
247 string saveLabel = sabund->getLabel();
250 sabund = input->getSAbundVector(lastLabel);
252 mothurOut(sabund->getLabel()); mothurOutEndLine();
253 processedLabels.insert(sabund->getLabel());
254 userLabels.erase(sabund->getLabel());
256 outputFileHandle << sabund->getLabel();
257 for(int i=0;i<sumCalculators.size();i++){
258 vector<double> data = sumCalculators[i]->getValues(sabund);
259 outputFileHandle << '\t';
260 sumCalculators[i]->print(outputFileHandle);
262 outputFileHandle << endl;
264 //restore real lastlabel to save below
265 sabund->setLabel(saveLabel);
268 lastLabel = sabund->getLabel();
271 sabund = input->getSAbundVector();
274 //output error messages about any remaining user labels
275 set<string>::iterator it;
276 bool needToRun = false;
277 for (it = userLabels.begin(); it != userLabels.end(); it++) {
278 mothurOut("Your file does not include the label " + *it);
279 if (processedLabels.count(lastLabel) != 1) {
280 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
283 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
287 //run last label if you need to
288 if (needToRun == true) {
289 if (sabund != NULL) { delete sabund; }
290 sabund = input->getSAbundVector(lastLabel);
292 mothurOut(sabund->getLabel()); mothurOutEndLine();
293 outputFileHandle << sabund->getLabel();
294 for(int i=0;i<sumCalculators.size();i++){
295 vector<double> data = sumCalculators[i]->getValues(sabund);
296 outputFileHandle << '\t';
297 sumCalculators[i]->print(outputFileHandle);
299 outputFileHandle << endl;
303 outputFileHandle.close();
305 delete input; globaldata->ginput = NULL;
307 delete validCalculator;
308 globaldata->sabund = NULL;
313 catch(exception& e) {
314 errorOut(e, "SummaryCommand", "execute");
318 //**********************************************************************************************************************
319 vector<string> SummaryCommand::parseSharedFile(string filename) {
321 vector<string> filenames;
323 map<string, ofstream*> filehandles;
324 map<string, ofstream*>::iterator it3;
328 read = new ReadOTUFile(filename);
329 read->read(&*globaldata);
331 input = globaldata->ginput;
332 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
334 string sharedFileRoot = getRootName(filename);
336 //clears file before we start to write to it below
337 for (int i=0; i<lookup.size(); i++) {
338 remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
339 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
343 for (int i=0; i<lookup.size(); i++) {
345 filehandles[lookup[i]->getGroup()] = temp;
346 groups.push_back(lookup[i]->getGroup());
349 while(lookup[0] != NULL) {
351 for (int i = 0; i < lookup.size(); i++) {
352 RAbundVector rav = lookup[i]->getRAbundVector();
353 openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
354 rav.print(*(filehandles[lookup[i]->getGroup()]));
355 (*(filehandles[lookup[i]->getGroup()])).close();
358 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
359 lookup = input->getSharedRAbundVectors();
363 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
368 globaldata->ginput = NULL;
372 catch(exception& e) {
373 errorOut(e, "SummaryCommand", "parseSharedFile");
377 //**********************************************************************************************************************