2 * summarysharedcommand.cpp
5 * Created by Sarah Westcott on 1/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "summarysharedcommand.h"
11 #include "sharedsobscollectsummary.h"
12 #include "sharedchao1.h"
13 #include "sharedace.h"
14 #include "sharednseqs.h"
15 #include "sharedjabund.h"
16 #include "sharedsorabund.h"
17 #include "sharedjclass.h"
18 #include "sharedsorclass.h"
19 #include "sharedjest.h"
20 #include "sharedsorest.h"
21 #include "sharedthetayc.h"
22 #include "sharedthetan.h"
23 #include "sharedkstest.h"
24 #include "whittaker.h"
25 #include "sharedochiai.h"
26 #include "sharedanderbergs.h"
27 #include "sharedkulczynski.h"
28 #include "sharedkulczynskicody.h"
29 #include "sharedlennon.h"
30 #include "sharedmorisitahorn.h"
31 #include "sharedbraycurtis.h"
32 #include "sharedjackknife.h"
33 #include "whittaker.h"
36 //**********************************************************************************************************************
38 SummarySharedCommand::SummarySharedCommand(string option){
40 globaldata = GlobalData::getInstance();
47 //allow user to run help
48 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
51 //valid paramters for this command
52 string Array[] = {"line","label","calc","groups"};
53 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
55 OptionParser parser(option);
56 map<string, string> parameters = parser.getParameters();
58 ValidParameters validParameter;
60 //check to make sure all parameters are valid for command
61 for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
62 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
65 //make sure the user has already run the read.otu command
66 if (globaldata->getSharedFile() == "") {
67 mothurOut("You must read a list and a group, or a shared before you can use the summary.shared command."); mothurOutEndLine(); abort = true;
70 //check for optional parameter and set defaults
71 // ...at some point should added some additional type checking...
72 line = validParameter.validFile(parameters, "line", false);
73 if (line == "not found") { line = ""; }
75 if(line != "all") { splitAtDash(line, lines); allLines = 0; }
76 else { allLines = 1; }
79 label = validParameter.validFile(parameters, "label", false);
80 if (label == "not found") { label = ""; }
82 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
83 else { allLines = 1; }
86 //make sure user did not use both the line and label parameters
87 if ((line != "") && (label != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); abort = true; }
88 //if the user has not specified any line or labels use the ones from read.otu
89 else if((line == "") && (label == "")) {
90 allLines = globaldata->allLines;
91 labels = globaldata->labels;
92 lines = globaldata->lines;
95 calc = validParameter.validFile(parameters, "calc", false);
96 if (calc == "not found") { calc = "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan"; }
98 if (calc == "default") { calc = "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan"; }
100 splitAtDash(calc, Estimators);
102 groups = validParameter.validFile(parameters, "groups", false);
103 if (groups == "not found") { groups = ""; }
105 splitAtDash(groups, Groups);
106 globaldata->Groups = Groups;
109 if (abort == false) {
111 validCalculator = new ValidCalculators();
114 for (i=0; i<Estimators.size(); i++) {
115 if (validCalculator->isValidCalculator("sharedsummary", Estimators[i]) == true) {
116 if (Estimators[i] == "sharedsobs") {
117 sumCalculators.push_back(new SharedSobsCS());
118 }else if (Estimators[i] == "sharedchao") {
119 sumCalculators.push_back(new SharedChao1());
120 }else if (Estimators[i] == "sharedace") {
121 sumCalculators.push_back(new SharedAce());
122 }else if (Estimators[i] == "jabund") {
123 sumCalculators.push_back(new JAbund());
124 }else if (Estimators[i] == "sorabund") {
125 sumCalculators.push_back(new SorAbund());
126 }else if (Estimators[i] == "jclass") {
127 sumCalculators.push_back(new Jclass());
128 }else if (Estimators[i] == "sorclass") {
129 sumCalculators.push_back(new SorClass());
130 }else if (Estimators[i] == "jest") {
131 sumCalculators.push_back(new Jest());
132 }else if (Estimators[i] == "sorest") {
133 sumCalculators.push_back(new SorEst());
134 }else if (Estimators[i] == "thetayc") {
135 sumCalculators.push_back(new ThetaYC());
136 }else if (Estimators[i] == "thetan") {
137 sumCalculators.push_back(new ThetaN());
138 }else if (Estimators[i] == "kstest") {
139 sumCalculators.push_back(new KSTest());
140 }else if (Estimators[i] == "sharednseqs") {
141 sumCalculators.push_back(new SharedNSeqs());
142 }else if (Estimators[i] == "ochiai") {
143 sumCalculators.push_back(new Ochiai());
144 }else if (Estimators[i] == "anderberg") {
145 sumCalculators.push_back(new Anderberg());
146 }else if (Estimators[i] == "kulczynski") {
147 sumCalculators.push_back(new Kulczynski());
148 }else if (Estimators[i] == "kulczynskicody") {
149 sumCalculators.push_back(new KulczynskiCody());
150 }else if (Estimators[i] == "lennon") {
151 sumCalculators.push_back(new Lennon());
152 }else if (Estimators[i] == "morisitahorn") {
153 sumCalculators.push_back(new MorHorn());
154 }else if (Estimators[i] == "braycurtis") {
155 sumCalculators.push_back(new BrayCurtis());
156 }else if (Estimators[i] == "whittaker") {
157 sumCalculators.push_back(new Whittaker());
162 outputFileName = ((getRootName(globaldata->inputFileName)) + "shared.summary");
163 openOutputFile(outputFileName, outputFileHandle);
168 catch(exception& e) {
169 errorOut(e, "SummarySharedCommand", "SummarySharedCommand");
174 //**********************************************************************************************************************
176 void SummarySharedCommand::help(){
178 mothurOut("The summary.shared command can only be executed after a successful read.otu command.\n");
179 mothurOut("The summary.shared command parameters are label, line and calc. No parameters are required, but you may not use \n");
180 mothurOut("both the line and label parameters at the same time. The summary.shared command should be in the following format: \n");
181 mothurOut("summary.shared(label=yourLabel, line=yourLines, calc=yourEstimators, groups=yourGroups).\n");
182 mothurOut("Example summary.shared(label=unique-.01-.03, line=0,5,10, groups=B-C, calc=sharedchao-sharedace-jabund-sorensonabund-jclass-sorclass-jest-sorest-thetayc-thetan).\n");
183 validCalculator->printCalc("sharedsummary", cout);
184 mothurOut("The default value for calc is sharedsobs-sharedchao-sharedace-jabund-sorensonabund-jclass-sorclass-jest-sorest-thetayc-thetan\n");
185 mothurOut("The default value for groups is all the groups in your groupfile.\n");
186 mothurOut("The label and line parameters are used to analyze specific lines in your input.\n");
187 mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed. You must enter at least 2 valid groups.\n");
188 mothurOut("Note: No spaces between parameter labels (i.e. line), '=' and parameters (i.e.yourLines).\n\n");
190 catch(exception& e) {
191 errorOut(e, "SummarySharedCommand", "help");
196 //**********************************************************************************************************************
198 SummarySharedCommand::~SummarySharedCommand(){
199 if (abort == false) {
201 delete validCalculator;
205 //**********************************************************************************************************************
207 int SummarySharedCommand::execute(){
210 if (abort == true) { return 0; }
214 //if the users entered no valid calculators don't execute command
215 if (sumCalculators.size() == 0) { return 0; }
216 //check if any calcs can do multiples
218 for (int i = 0; i < sumCalculators.size(); i++) {
219 if (sumCalculators[i]->getMultiple() == true) { mult = true; }
224 read = new ReadOTUFile(globaldata->inputFileName);
225 read->read(&*globaldata);
227 input = globaldata->ginput;
228 lookup = input->getSharedRAbundVectors();
229 string lastLabel = lookup[0]->getLabel();
231 //output estimator names as column headers
232 outputFileHandle << "label" <<'\t' << "comparison" << '\t';
233 for(int i=0;i<sumCalculators.size();i++){
234 outputFileHandle << '\t' << sumCalculators[i]->getName();
236 outputFileHandle << endl;
238 //create file and put column headers for multiple groups file
240 outAllFileName = ((getRootName(globaldata->inputFileName)) + "sharedmultiple.summary");
241 openOutputFile(outAllFileName, outAll);
243 outAll << "label" <<'\t' << "comparison" << '\t';
244 for(int i=0;i<sumCalculators.size();i++){
245 if (sumCalculators[i]->getMultiple() == true) {
246 outAll << '\t' << sumCalculators[i]->getName();
252 if (lookup.size() < 2) {
253 mothurOut("I cannot run the command without at least 2 valid groups.");
254 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
256 //close files and clean up
257 outputFileHandle.close(); remove(outputFileName.c_str());
258 if (mult == true) { outAll.close(); remove(outAllFileName.c_str()); }
260 //if you only have 2 groups you don't need a .sharedmultiple file
261 }else if ((lookup.size() == 2) && (mult == true)) {
264 remove(outAllFileName.c_str());
267 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
268 set<string> processedLabels;
269 set<string> userLabels = labels;
270 set<int> userLines = lines;
272 //as long as you are not at the end of the file or done wih the lines you want
273 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
275 if(allLines == 1 || lines.count(count) == 1 || labels.count(lookup[0]->getLabel()) == 1){
276 mothurOut(lookup[0]->getLabel() + "\t" + toString(count)); mothurOutEndLine();
279 processedLabels.insert(lookup[0]->getLabel());
280 userLabels.erase(lookup[0]->getLabel());
281 userLines.erase(count);
284 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
285 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
286 lookup = input->getSharedRAbundVectors(lastLabel);
288 mothurOut(lookup[0]->getLabel() + "\t" + toString(count)); mothurOutEndLine();
291 processedLabels.insert(lookup[0]->getLabel());
292 userLabels.erase(lookup[0]->getLabel());
297 lastLabel = lookup[0]->getLabel();
299 //get next line to process
300 //prevent memory leak
301 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
302 lookup = input->getSharedRAbundVectors();
306 //output error messages about any remaining user labels
307 set<string>::iterator it;
308 bool needToRun = false;
309 for (it = userLabels.begin(); it != userLabels.end(); it++) {
310 mothurOut("Your file does not include the label " + *it);
311 if (processedLabels.count(lastLabel) != 1) {
312 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
315 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
319 //run last line if you need to
320 if (needToRun == true) {
321 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
322 lookup = input->getSharedRAbundVectors(lastLabel);
324 mothurOut(lookup[0]->getLabel() + "\t" + toString(count)); mothurOutEndLine();
326 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
330 //reset groups parameter
331 globaldata->Groups.clear();
334 outputFileHandle.close();
335 if (mult == true) { outAll.close(); }
337 for(int i=0;i<sumCalculators.size();i++){ delete sumCalculators[i]; }
339 delete input; globaldata->ginput = NULL;
343 catch(exception& e) {
344 errorOut(e, "SummarySharedCommand", "execute");
349 /***********************************************************/
350 void SummarySharedCommand::process(vector<SharedRAbundVector*> thisLookup) {
352 //loop through calculators and add to file all for all calcs that can do mutiple groups
355 outAll << thisLookup[0]->getLabel() << '\t';
357 //output groups names
358 string outNames = "";
359 for (int j = 0; j < thisLookup.size(); j++) {
360 outNames += thisLookup[j]->getGroup() + "-";
362 outNames = outNames.substr(0, outNames.length()-1); //rip off extra '-';
363 outAll << outNames << '\t';
365 for(int i=0;i<sumCalculators.size();i++){
366 if (sumCalculators[i]->getMultiple() == true) {
367 sumCalculators[i]->getValues(thisLookup);
369 sumCalculators[i]->print(outAll);
376 vector<SharedRAbundVector*> subset;
377 for (int k = 0; k < (thisLookup.size() - 1); k++) { // pass cdd each set of groups to commpare
378 for (int l = n; l < thisLookup.size(); l++) {
380 outputFileHandle << thisLookup[0]->getLabel() << '\t';
382 subset.clear(); //clear out old pair of sharedrabunds
383 //add new pair of sharedrabunds
384 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]);
386 //sort groups to be alphanumeric
387 if (thisLookup[k]->getGroup() > thisLookup[l]->getGroup()) {
388 outputFileHandle << (thisLookup[l]->getGroup() +'\t' + thisLookup[k]->getGroup()) << '\t'; //print out groups
390 outputFileHandle << (thisLookup[k]->getGroup() +'\t' + thisLookup[l]->getGroup()) << '\t'; //print out groups
393 for(int i=0;i<sumCalculators.size();i++) {
395 sumCalculators[i]->getValues(subset); //saves the calculator outputs
396 outputFileHandle << '\t';
397 sumCalculators[i]->print(outputFileHandle);
399 outputFileHandle << endl;
405 catch(exception& e) {
406 errorOut(e, "SummarySharedCommand", "process");
411 /***********************************************************/