]> git.donarmstrong.com Git - mothur.git/blob - venncommand.cpp
added smart distance feature and optimized all commands using line by line processing
[mothur.git] / venncommand.cpp
1 /*
2  *  venncommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 3/30/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "venncommand.h"
11 #include "ace.h"
12 #include "sobs.h"
13 #include "chao1.h"
14 //#include "jackknife.h"
15 #include "sharedsobscollectsummary.h"
16 #include "sharedchao1.h"
17 #include "sharedace.h"
18
19
20 //**********************************************************************************************************************
21
22 VennCommand::VennCommand(){
23         try {
24                 globaldata = GlobalData::getInstance();
25                 format = globaldata->getFormat();
26                 validCalculator = new ValidCalculators();
27                 
28                 int i;
29                 
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);
39                                                 if(abund < 5)
40                                                         abund = 10;
41                                                 vennCalculators.push_back(new Ace(abund));
42                                         //}else if (globaldata->Estimators[i] == "jack") {      
43                                                 //vennCalculators.push_back(new Jackknife());
44                                         }
45                                 }
46                         }
47                 }else {
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());
56                                         }
57                                 }
58                         }
59                 }
60                 
61                 venn = new Venn();
62                 
63                 //reset calc for next command
64                 globaldata->setCalc("");
65
66                 
67         }
68         catch(exception& e) {
69                 cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function VennCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
70                 exit(1);
71         }
72         catch(...) {
73                 cout << "An unknown error has occurred in the VennCommand class function VennCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
74                 exit(1);
75         }       
76 }
77 //**********************************************************************************************************************
78
79 VennCommand::~VennCommand(){
80         delete input;
81         delete read;
82         delete venn;
83         for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
84 }
85
86 //**********************************************************************************************************************
87
88 int VennCommand::execute(){
89         try {
90                 int count = 1;
91                 SAbundVector* lastSAbund;
92                 vector<SharedRAbundVector*> lastLookup; 
93                 
94                 //if the users entered no valid calculators don't execute command
95                 if (vennCalculators.size() == 0) { return 0; }
96                 
97                 if (format == "sharedfile") {
98                         //you have groups
99                         read = new ReadOTUFile(globaldata->inputFileName);      
100                         read->read(&*globaldata); 
101                         
102                         input = globaldata->ginput;
103                         lookup = input->getSharedRAbundVectors();
104                         lastLookup = lookup;
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); 
109                 
110                         sabund = globaldata->sabund;
111                         lastSAbund = globaldata->sabund;
112                         input = globaldata->ginput;
113                 }
114                 
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                 
119                 if (format != "list") { 
120                         
121                         //as long as you are not at the end of the file or done wih the lines you want
122                         while((lookup[0] != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
123                 
124                                 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){                   
125                                         cout << lookup[0]->getLabel() << '\t' << count << endl;
126                                         processedLabels.insert(lookup[0]->getLabel());
127                                         userLabels.erase(lookup[0]->getLabel());
128                                         
129                                         if (lookup.size() > 4) {
130                                                 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;
131                                                 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
132                                         }
133                                         venn->getPic(lookup, vennCalculators);
134                                 }
135                                 
136                                 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
137                                         cout << lastLookup[0]->getLabel() << '\t' << count << endl;
138                                         processedLabels.insert(lastLookup[0]->getLabel());
139                                         userLabels.erase(lastLookup[0]->getLabel());
140
141                                         if (lastLookup.size() > 4) {
142                                                 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;
143                                                 for (int i = lastLookup.size(); i > 4; i--) { lastLookup.pop_back(); } //no memmory leak because pop_back calls destructor
144                                         }                               
145                                         venn->getPic(lastLookup, vennCalculators);
146                                 }
147                                 
148                                 //prevent memory leak
149                                 if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  } }
150                                 lastLookup = lookup;    
151                                                 
152                                 //get next line to process
153                                 lookup = input->getSharedRAbundVectors();
154                                 count++;
155                         }
156                         
157                         //output error messages about any remaining user labels
158                         set<string>::iterator it;
159                         bool needToRun = false;
160                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
161                                 cout << "Your file does not include the label "<< *it; 
162                                 if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
163                                         cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
164                                         needToRun = true;
165                                 }else {
166                                         cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
167                                 }
168                         }
169                 
170                         //run last line if you need to
171                         if (needToRun == true)  {
172                                 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
173                                 if (lastLookup.size() > 4) {
174                                         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;
175                                         for (int i = lastLookup.size(); i > 3; i--) { delete lastLookup[i]; lastLookup.pop_back(); }
176                                 }                               
177                                 venn->getPic(lastLookup, vennCalculators);
178                         }
179                 
180                         for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  }
181
182                         //reset groups parameter
183                         globaldata->Groups.clear();  
184                         
185                 }else{
186                 
187                         while((sabund != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
188                 
189                                 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(sabund->getLabel()) == 1){                      
190         
191                                         cout << sabund->getLabel() << '\t' << count << endl;
192                                         venn->getPic(sabund, vennCalculators);
193                                         
194                                         processedLabels.insert(sabund->getLabel());
195                                         userLabels.erase(sabund->getLabel());
196                                 }
197                                 
198                                 if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastSAbund->getLabel()) != 1)) {
199
200                                         cout << lastSAbund->getLabel() << '\t' << count << endl;
201                                         venn->getPic(lastSAbund, vennCalculators);
202                                         
203                                         processedLabels.insert(lastSAbund->getLabel());
204                                         userLabels.erase(lastSAbund->getLabel());
205                                 }               
206                                 
207                                 if (count != 1) { delete lastSAbund; }
208                                 lastSAbund = sabund;                    
209
210                                 sabund = input->getSAbundVector();
211                                 count++;
212                         }
213                         
214                         //output error messages about any remaining user labels
215                         set<string>::iterator it;
216                         bool needToRun = false;
217                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
218                                 cout << "Your file does not include the label "<< *it; 
219                                 if (processedLabels.count(lastSAbund->getLabel()) != 1) {
220                                         cout << ". I will use " << lastSAbund->getLabel() << "." << endl;
221                                         needToRun = true;
222                                 }else {
223                                         cout << ". Please refer to " << lastSAbund->getLabel() << "." << endl;
224                                 }
225                         }
226                 
227                         //run last line if you need to
228                         if (needToRun == true)  {
229                                 cout << lastSAbund->getLabel() << '\t' << count << endl;
230                                 venn->getPic(lastSAbund, vennCalculators);
231                         }
232                         delete lastSAbund;
233                 }
234                 
235                 
236                 globaldata->setGroups("");
237                 return 0;
238         }
239         catch(exception& e) {
240                 cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
241                 exit(1);
242         }
243         catch(...) {
244                 cout << "An unknown error has occurred in the VennCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
245                 exit(1);
246         }               
247 }
248
249 //**********************************************************************************************************************