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(){
40 globaldata = GlobalData::getInstance();
41 outputFileName = ((getRootName(globaldata->inputFileName)) + "shared.summary");
42 openOutputFile(outputFileName, outputFileHandle);
43 format = globaldata->getFormat();
44 validCalculator = new ValidCalculators();
48 for (i=0; i<globaldata->Estimators.size(); i++) {
49 if (validCalculator->isValidCalculator("sharedsummary", globaldata->Estimators[i]) == true) {
50 if (globaldata->Estimators[i] == "sharedsobs") {
51 sumCalculators.push_back(new SharedSobsCS());
52 }else if (globaldata->Estimators[i] == "sharedchao") {
53 sumCalculators.push_back(new SharedChao1());
54 }else if (globaldata->Estimators[i] == "sharedace") {
55 sumCalculators.push_back(new SharedAce());
56 }else if (globaldata->Estimators[i] == "jabund") {
57 sumCalculators.push_back(new JAbund());
58 }else if (globaldata->Estimators[i] == "sorabund") {
59 sumCalculators.push_back(new SorAbund());
60 }else if (globaldata->Estimators[i] == "jclass") {
61 sumCalculators.push_back(new Jclass());
62 }else if (globaldata->Estimators[i] == "sorclass") {
63 sumCalculators.push_back(new SorClass());
64 }else if (globaldata->Estimators[i] == "jest") {
65 sumCalculators.push_back(new Jest());
66 }else if (globaldata->Estimators[i] == "sorest") {
67 sumCalculators.push_back(new SorEst());
68 }else if (globaldata->Estimators[i] == "thetayc") {
69 sumCalculators.push_back(new ThetaYC());
70 }else if (globaldata->Estimators[i] == "thetan") {
71 sumCalculators.push_back(new ThetaN());
72 }else if (globaldata->Estimators[i] == "kstest") {
73 sumCalculators.push_back(new KSTest());
74 }else if (globaldata->Estimators[i] == "sharednseqs") {
75 sumCalculators.push_back(new SharedNSeqs());
76 }else if (globaldata->Estimators[i] == "ochiai") {
77 sumCalculators.push_back(new Ochiai());
78 }else if (globaldata->Estimators[i] == "anderberg") {
79 sumCalculators.push_back(new Anderberg());
80 }else if (globaldata->Estimators[i] == "kulczynski") {
81 sumCalculators.push_back(new Kulczynski());
82 }else if (globaldata->Estimators[i] == "kulczynskicody") {
83 sumCalculators.push_back(new KulczynskiCody());
84 }else if (globaldata->Estimators[i] == "lennon") {
85 sumCalculators.push_back(new Lennon());
86 }else if (globaldata->Estimators[i] == "morisitahorn") {
87 sumCalculators.push_back(new MorHorn());
88 }else if (globaldata->Estimators[i] == "braycurtis") {
89 sumCalculators.push_back(new BrayCurtis());
90 }else if (globaldata->Estimators[i] == "whittaker") {
91 sumCalculators.push_back(new Whittaker());
95 //reset calc for next command
96 globaldata->setCalc("");
100 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
104 cout << "An unknown error has occurred in the SummarySharedCommand class function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
108 //**********************************************************************************************************************
110 SummarySharedCommand::~SummarySharedCommand(){
115 //**********************************************************************************************************************
117 int SummarySharedCommand::execute(){
121 //if the users entered no valid calculators don't execute command
122 if (sumCalculators.size() == 0) { return 0; }
123 //check if any calcs can do multiples
125 for (int i = 0; i < sumCalculators.size(); i++) {
126 if (sumCalculators[i]->getMultiple() == true) { mult = true; }
131 read = new ReadOTUFile(globaldata->inputFileName);
132 read->read(&*globaldata);
134 input = globaldata->ginput;
135 lookup = input->getSharedRAbundVectors();
136 vector<SharedRAbundVector*> lastLookup = lookup;
138 //output estimator names as column headers
139 outputFileHandle << "label" <<'\t' << "comparison" << '\t';
140 for(int i=0;i<sumCalculators.size();i++){
141 outputFileHandle << '\t' << sumCalculators[i]->getName();
143 outputFileHandle << endl;
145 //create file and put column headers for multiple groups file
147 outAllFileName = ((getRootName(globaldata->inputFileName)) + "sharedmultiple.summary");
148 openOutputFile(outAllFileName, outAll);
150 outAll << "label" <<'\t' << "comparison" << '\t';
151 for(int i=0;i<sumCalculators.size();i++){
152 if (sumCalculators[i]->getMultiple() == true) {
153 outAll << '\t' << sumCalculators[i]->getName();
159 if (lookup.size() < 2) {
160 cout << "I cannot run the command without at least 2 valid groups.";
161 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
163 //close files and clean up
164 outputFileHandle.close(); remove(outputFileName.c_str());
165 if (mult == true) { outAll.close(); remove(outAllFileName.c_str()); }
167 //if you only have 2 groups you don't need a .sharedmultiple file
168 }else if ((lookup.size() == 2) && (mult == true)) {
171 remove(outAllFileName.c_str());
174 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
175 set<string> processedLabels;
176 set<string> userLabels = globaldata->labels;
177 set<int> userLines = globaldata->lines;
179 //as long as you are not at the end of the file or done wih the lines you want
180 while((lookup[0] != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
182 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){
183 cout << lookup[0]->getLabel() << '\t' << count << endl;
186 processedLabels.insert(lookup[0]->getLabel());
187 userLabels.erase(lookup[0]->getLabel());
188 userLines.erase(count);
191 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
192 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
195 processedLabels.insert(lastLookup[0]->getLabel());
196 userLabels.erase(lastLookup[0]->getLabel());
200 //prevent memory leak
201 if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) { delete lastLookup[i]; } }
204 //get next line to process
205 lookup = input->getSharedRAbundVectors();
209 //output error messages about any remaining user labels
210 set<string>::iterator it;
211 bool needToRun = false;
212 for (it = userLabels.begin(); it != userLabels.end(); it++) {
213 cout << "Your file does not include the label "<< *it;
214 if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
215 cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
218 cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
222 //run last line if you need to
223 if (needToRun == true) {
224 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
228 for (int i = 0; i < lastLookup.size(); i++) { delete lastLookup[i]; }
230 //reset groups parameter
231 globaldata->Groups.clear(); globaldata->setGroups("");
234 outputFileHandle.close();
235 if (mult == true) { outAll.close(); }
239 catch(exception& e) {
240 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
244 cout << "An unknown error has occurred in the SummarySharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
249 /***********************************************************/
250 void SummarySharedCommand::process(vector<SharedRAbundVector*> thisLookup) {
252 //loop through calculators and add to file all for all calcs that can do mutiple groups
255 outAll << thisLookup[0]->getLabel() << '\t';
257 //output groups names
258 string outNames = "";
259 for (int j = 0; j < thisLookup.size(); j++) {
260 outNames += thisLookup[j]->getGroup() + "-";
262 outNames = outNames.substr(0, outNames.length()-1); //rip off extra '-';
263 outAll << outNames << '\t';
265 for(int i=0;i<sumCalculators.size();i++){
266 if (sumCalculators[i]->getMultiple() == true) {
267 sumCalculators[i]->getValues(thisLookup);
269 sumCalculators[i]->print(outAll);
276 vector<SharedRAbundVector*> subset;
277 for (int k = 0; k < (thisLookup.size() - 1); k++) { // pass cdd each set of groups to commpare
278 for (int l = n; l < thisLookup.size(); l++) {
280 outputFileHandle << thisLookup[0]->getLabel() << '\t';
282 subset.clear(); //clear out old pair of sharedrabunds
283 //add new pair of sharedrabunds
284 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]);
286 //sort groups to be alphanumeric
287 if (thisLookup[k]->getGroup() > thisLookup[l]->getGroup()) {
288 outputFileHandle << (thisLookup[l]->getGroup() +'\t' + thisLookup[k]->getGroup()) << '\t'; //print out groups
290 outputFileHandle << (thisLookup[k]->getGroup() +'\t' + thisLookup[l]->getGroup()) << '\t'; //print out groups
293 for(int i=0;i<sumCalculators.size();i++) {
295 sumCalculators[i]->getValues(subset); //saves the calculator outputs
296 outputFileHandle << '\t';
297 sumCalculators[i]->print(outputFileHandle);
299 outputFileHandle << endl;
305 catch(exception& e) {
306 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
310 cout << "An unknown error has occurred in the SummarySharedCommand class function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
315 /***********************************************************/