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"};
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 //check for optional parameter and set defaults
64 // ...at some point should added some additional type checking...
65 label = validParameter.validFile(parameters, "label", false);
66 if (label == "not found") { label = ""; }
68 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
69 else { allLines = 1; }
72 //if the user has not specified any labels use the ones from read.otu
74 allLines = globaldata->allLines;
75 labels = globaldata->labels;
78 calc = validParameter.validFile(parameters, "calc", false);
79 if (calc == "not found") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
81 if (calc == "default") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson"; }
83 splitAtDash(calc, Estimators);
86 temp = validParameter.validFile(parameters, "abund", false); if (temp == "not found") { temp = "10"; }
89 temp = validParameter.validFile(parameters, "size", false); if (temp == "not found") { temp = "0"; }
95 errorOut(e, "SummaryCommand", "SummaryCommand");
99 //**********************************************************************************************************************
101 void SummaryCommand::help(){
103 mothurOut("The summary.single command can only be executed after a successful read.otu WTIH ONE EXECEPTION.\n");
104 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");
105 mothurOut("The summary.single command parameters are label, calc, abund. No parameters are required.\n");
106 mothurOut("The summary.single command should be in the following format: \n");
107 mothurOut("summary.single(label=yourLabel, calc=yourEstimators).\n");
108 mothurOut("Example summary.single(label=unique-.01-.03, calc=sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson).\n");
109 validCalculator->printCalc("summary", cout);
110 mothurOut("The default value calc is sobs-chao-ace-jack-shannon-npshannon-simpson\n");
111 mothurOut("The label parameter is used to analyze specific labels in your input.\n");
112 mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabels).\n\n");
114 catch(exception& e) {
115 errorOut(e, "SummaryCommand", "help");
120 //**********************************************************************************************************************
122 SummaryCommand::~SummaryCommand(){}
124 //**********************************************************************************************************************
126 int SummaryCommand::execute(){
129 if (abort == true) { return 0; }
131 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName); }
132 else { inputFileNames = parseSharedFile(globaldata->getSharedFile()); globaldata->setFormat("rabund"); }
134 for (int p = 0; p < inputFileNames.size(); p++) {
136 string fileNameRoot = getRootName(inputFileNames[p]) + "summary";
137 globaldata->inputFileName = inputFileNames[p];
139 if (inputFileNames.size() > 1) {
140 mothurOutEndLine(); mothurOut("Processing group " + groups[p]); mothurOutEndLine(); mothurOutEndLine();
144 validCalculator = new ValidCalculators();
146 for (int i=0; i<Estimators.size(); i++) {
147 if (validCalculator->isValidCalculator("summary", Estimators[i]) == true) {
148 if(Estimators[i] == "sobs"){
149 sumCalculators.push_back(new Sobs());
150 }else if(Estimators[i] == "chao"){
151 sumCalculators.push_back(new Chao1());
152 }else if(Estimators[i] == "coverage"){
153 sumCalculators.push_back(new Coverage());
154 }else if(Estimators[i] == "geometric"){
155 sumCalculators.push_back(new Geom());
156 }else if(Estimators[i] == "logseries"){
157 sumCalculators.push_back(new LogSD());
158 }else if(Estimators[i] == "qstat"){
159 sumCalculators.push_back(new QStat());
160 }else if(Estimators[i] == "bergerparker"){
161 sumCalculators.push_back(new BergerParker());
162 }else if(Estimators[i] == "bstick"){
163 sumCalculators.push_back(new BStick());
164 }else if(Estimators[i] == "ace"){
167 sumCalculators.push_back(new Ace(abund));
168 }else if(Estimators[i] == "jack"){
169 sumCalculators.push_back(new Jackknife());
170 }else if(Estimators[i] == "shannon"){
171 sumCalculators.push_back(new Shannon());
172 }else if(Estimators[i] == "npshannon"){
173 sumCalculators.push_back(new NPShannon());
174 }else if(Estimators[i] == "simpson"){
175 sumCalculators.push_back(new Simpson());
176 }else if(Estimators[i] == "bootstrap"){
177 sumCalculators.push_back(new Bootstrap());
178 }else if (Estimators[i] == "nseqs") {
179 sumCalculators.push_back(new NSeqs());
180 }else if (Estimators[i] == "goodscoverage") {
181 sumCalculators.push_back(new GoodsCoverage());
182 }else if (Estimators[i] == "efron") {
183 sumCalculators.push_back(new Efron(size));
184 }else if (Estimators[i] == "boneh") {
185 sumCalculators.push_back(new Boneh(size));
186 }else if (Estimators[i] == "solow") {
187 sumCalculators.push_back(new Solow(size));
188 }else if (Estimators[i] == "shen") {
189 sumCalculators.push_back(new Shen(size, abund));
194 //if the users entered no valid calculators don't execute command
195 if (sumCalculators.size() == 0) { return 0; }
197 ofstream outputFileHandle;
198 openOutputFile(fileNameRoot, outputFileHandle);
199 outputFileHandle << "label";
201 read = new ReadOTUFile(globaldata->inputFileName);
202 read->read(&*globaldata);
204 sabund = globaldata->sabund;
205 string lastLabel = sabund->getLabel();
206 input = globaldata->ginput;
208 for(int i=0;i<sumCalculators.size();i++){
209 if(sumCalculators[i]->getCols() == 1){
210 outputFileHandle << '\t' << sumCalculators[i]->getName();
213 outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
216 outputFileHandle << endl;
218 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
219 set<string> processedLabels;
220 set<string> userLabels = labels;
222 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
224 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
226 mothurOut(sabund->getLabel()); mothurOutEndLine();
227 processedLabels.insert(sabund->getLabel());
228 userLabels.erase(sabund->getLabel());
230 outputFileHandle << sabund->getLabel();
231 for(int i=0;i<sumCalculators.size();i++){
232 vector<double> data = sumCalculators[i]->getValues(sabund);
233 outputFileHandle << '\t';
234 sumCalculators[i]->print(outputFileHandle);
236 outputFileHandle << endl;
239 if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
240 string saveLabel = sabund->getLabel();
243 sabund = input->getSAbundVector(lastLabel);
245 mothurOut(sabund->getLabel()); mothurOutEndLine();
246 processedLabels.insert(sabund->getLabel());
247 userLabels.erase(sabund->getLabel());
249 outputFileHandle << sabund->getLabel();
250 for(int i=0;i<sumCalculators.size();i++){
251 vector<double> data = sumCalculators[i]->getValues(sabund);
252 outputFileHandle << '\t';
253 sumCalculators[i]->print(outputFileHandle);
255 outputFileHandle << endl;
257 //restore real lastlabel to save below
258 sabund->setLabel(saveLabel);
261 lastLabel = sabund->getLabel();
264 sabund = input->getSAbundVector();
267 //output error messages about any remaining user labels
268 set<string>::iterator it;
269 bool needToRun = false;
270 for (it = userLabels.begin(); it != userLabels.end(); it++) {
271 mothurOut("Your file does not include the label " + *it);
272 if (processedLabels.count(lastLabel) != 1) {
273 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
276 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
280 //run last label if you need to
281 if (needToRun == true) {
282 if (sabund != NULL) { delete sabund; }
283 sabund = input->getSAbundVector(lastLabel);
285 mothurOut(sabund->getLabel()); mothurOutEndLine();
286 outputFileHandle << sabund->getLabel();
287 for(int i=0;i<sumCalculators.size();i++){
288 vector<double> data = sumCalculators[i]->getValues(sabund);
289 outputFileHandle << '\t';
290 sumCalculators[i]->print(outputFileHandle);
292 outputFileHandle << endl;
296 outputFileHandle.close();
298 delete input; globaldata->ginput = NULL;
300 delete validCalculator;
301 globaldata->sabund = NULL;
306 catch(exception& e) {
307 errorOut(e, "SummaryCommand", "execute");
311 //**********************************************************************************************************************
312 vector<string> SummaryCommand::parseSharedFile(string filename) {
314 vector<string> filenames;
316 map<string, ofstream*> filehandles;
317 map<string, ofstream*>::iterator it3;
321 read = new ReadOTUFile(filename);
322 read->read(&*globaldata);
324 input = globaldata->ginput;
325 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
327 string sharedFileRoot = getRootName(filename);
329 //clears file before we start to write to it below
330 for (int i=0; i<lookup.size(); i++) {
331 remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
332 filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
336 for (int i=0; i<lookup.size(); i++) {
338 filehandles[lookup[i]->getGroup()] = temp;
339 groups.push_back(lookup[i]->getGroup());
342 while(lookup[0] != NULL) {
344 for (int i = 0; i < lookup.size(); i++) {
345 RAbundVector rav = lookup[i]->getRAbundVector();
346 openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
347 rav.print(*(filehandles[lookup[i]->getGroup()]));
348 (*(filehandles[lookup[i]->getGroup()])).close();
351 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
352 lookup = input->getSharedRAbundVectors();
356 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
361 globaldata->ginput = NULL;
365 catch(exception& e) {
366 errorOut(e, "SummaryCommand", "parseSharedFile");
370 //**********************************************************************************************************************