5 * Created by Sarah Westcott on 3/30/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "venncommand.h"
14 //#include "jackknife.h"
15 #include "sharedsobscollectsummary.h"
16 #include "sharedchao1.h"
17 #include "sharedace.h"
20 //**********************************************************************************************************************
22 VennCommand::VennCommand(){
24 globaldata = GlobalData::getInstance();
25 format = globaldata->getFormat();
26 validCalculator = new ValidCalculators();
30 if (format == "list") {
31 for (i=0; i<globaldata->Estimators.size(); i++) {
32 if (validCalculator->isValidCalculator("vennsingle", globaldata->Estimators[i]) == true) {
33 if (globaldata->Estimators[i] == "sobs") {
34 vennCalculators.push_back(new Sobs());
35 }else if (globaldata->Estimators[i] == "chao") {
36 vennCalculators.push_back(new Chao1());
37 }else if (globaldata->Estimators[i] == "ace") {
38 convert(globaldata->getAbund(), abund);
41 vennCalculators.push_back(new Ace(abund));
42 //}else if (globaldata->Estimators[i] == "jack") {
43 //vennCalculators.push_back(new Jackknife());
48 for (i=0; i<globaldata->Estimators.size(); i++) {
49 if (validCalculator->isValidCalculator("vennshared", globaldata->Estimators[i]) == true) {
50 if (globaldata->Estimators[i] == "sharedsobs") {
51 vennCalculators.push_back(new SharedSobsCS());
52 }else if (globaldata->Estimators[i] == "sharedchao") {
53 vennCalculators.push_back(new SharedChao1());
54 }else if (globaldata->Estimators[i] == "sharedace") {
55 vennCalculators.push_back(new SharedAce());
63 //reset calc for next command
64 globaldata->setCalc("");
69 cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function VennCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
73 cout << "An unknown error has occurred in the VennCommand class function VennCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
77 //**********************************************************************************************************************
79 VennCommand::~VennCommand(){
83 for (int i = 0; i < vennCalculators.size(); i++) { delete vennCalculators[i]; }
86 //**********************************************************************************************************************
88 int VennCommand::execute(){
91 SAbundVector* lastSAbund;
92 vector<SharedRAbundVector*> lastLookup;
94 //if the users entered no valid calculators don't execute command
95 if (vennCalculators.size() == 0) { return 0; }
97 if (format == "sharedfile") {
99 read = new ReadOTUFile(globaldata->inputFileName);
100 read->read(&*globaldata);
102 input = globaldata->ginput;
103 lookup = input->getSharedRAbundVectors();
105 }else if (format == "list") {
106 //you are using just a list file and have only one group
107 read = new ReadOTUFile(globaldata->inputFileName);
108 read->read(&*globaldata);
110 sabund = globaldata->sabund;
111 lastSAbund = globaldata->sabund;
112 input = globaldata->ginput;
115 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
116 set<string> processedLabels;
117 set<string> userLabels = globaldata->labels;
118 set<int> userLines = globaldata->lines;
120 if (format != "list") {
122 //as long as you are not at the end of the file or done wih the lines you want
123 while((lookup[0] != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
125 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){
126 cout << lookup[0]->getLabel() << '\t' << count << endl;
127 processedLabels.insert(lookup[0]->getLabel());
128 userLabels.erase(lookup[0]->getLabel());
129 userLines.erase(count);
131 if (lookup.size() > 4) {
132 cout << "Error: Too many groups chosen. You may use up to 4 groups with the venn command. I will use the first four groups in your groupfile." << endl;
133 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
135 venn->getPic(lookup, vennCalculators);
138 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
139 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
140 processedLabels.insert(lastLookup[0]->getLabel());
141 userLabels.erase(lastLookup[0]->getLabel());
143 if (lastLookup.size() > 4) {
144 cout << "Error: Too many groups chosen. You may use up to 4 groups with the venn command. I will use the first four groups in your groupfile." << endl;
145 for (int i = lastLookup.size(); i > 4; i--) { lastLookup.pop_back(); } //no memmory leak because pop_back calls destructor
147 venn->getPic(lastLookup, vennCalculators);
150 //prevent memory leak
151 if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) { delete lastLookup[i]; } }
154 //get next line to process
155 lookup = input->getSharedRAbundVectors();
159 //output error messages about any remaining user labels
160 set<string>::iterator it;
161 bool needToRun = false;
162 for (it = userLabels.begin(); it != userLabels.end(); it++) {
163 cout << "Your file does not include the label "<< *it;
164 if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
165 cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
168 cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
172 //run last line if you need to
173 if (needToRun == true) {
174 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
175 if (lastLookup.size() > 4) {
176 cout << "Error: Too many groups chosen. You may use up to 4 groups with the venn command. I will use the first four groups in your groupfile." << endl;
177 for (int i = lastLookup.size(); i > 3; i--) { delete lastLookup[i]; lastLookup.pop_back(); }
179 venn->getPic(lastLookup, vennCalculators);
182 for (int i = 0; i < lastLookup.size(); i++) { delete lastLookup[i]; }
184 //reset groups parameter
185 globaldata->Groups.clear();
189 while((sabund != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
191 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(sabund->getLabel()) == 1){
193 cout << sabund->getLabel() << '\t' << count << endl;
194 venn->getPic(sabund, vennCalculators);
196 processedLabels.insert(sabund->getLabel());
197 userLabels.erase(sabund->getLabel());
198 userLines.erase(count);
201 if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastSAbund->getLabel()) != 1)) {
203 cout << lastSAbund->getLabel() << '\t' << count << endl;
204 venn->getPic(lastSAbund, vennCalculators);
206 processedLabels.insert(lastSAbund->getLabel());
207 userLabels.erase(lastSAbund->getLabel());
210 if (count != 1) { delete lastSAbund; }
213 sabund = input->getSAbundVector();
217 //output error messages about any remaining user labels
218 set<string>::iterator it;
219 bool needToRun = false;
220 for (it = userLabels.begin(); it != userLabels.end(); it++) {
221 cout << "Your file does not include the label "<< *it;
222 if (processedLabels.count(lastSAbund->getLabel()) != 1) {
223 cout << ". I will use " << lastSAbund->getLabel() << "." << endl;
226 cout << ". Please refer to " << lastSAbund->getLabel() << "." << endl;
230 //run last line if you need to
231 if (needToRun == true) {
232 cout << lastSAbund->getLabel() << '\t' << count << endl;
233 venn->getPic(lastSAbund, vennCalculators);
239 globaldata->setGroups("");
242 catch(exception& e) {
243 cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
247 cout << "An unknown error has occurred in the VennCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
252 //**********************************************************************************************************************