]> git.donarmstrong.com Git - mothur.git/blob - venncommand.cpp
broke up globaldata and moved error checking and help into commands
[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(string option){
23         try {
24                 globaldata = GlobalData::getInstance();
25                 abort = false;
26                 allLines = 1;
27                 lines.clear();
28                 labels.clear();
29                 
30                 //allow user to run help
31                 if(option == "help") { help(); abort = true; }
32                 
33                 else {
34                         //valid paramters for this command
35                         string AlignArray[] =  {"groups","line","label","calc", "abund"};
36                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
37                         
38                         parser = new OptionParser();
39                         parser->parse(option, parameters);  delete parser;
40                         
41                         ValidParameters* validParameter = new ValidParameters();
42                 
43                         //check to make sure all parameters are valid for command
44                         for (it = parameters.begin(); it != parameters.end(); it++) { 
45                                 if (validParameter->isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
46                         }
47                         
48                         //make sure the user has already run the read.otu command
49                         if ((globaldata->getListFile() == "") && (globaldata->getSharedFile() == "")) {
50                                  cout << "You must read a list, or a list and a group, or a shared before you can use the venn command." << endl; abort = true; 
51                         }
52
53                         //check for optional parameter and set defaults
54                         // ...at some point should added some additional type checking...
55                         line = validParameter->validFile(parameters, "line", false);                            
56                         if (line == "not found") { line = "";  }
57                         else { 
58                                 if(line != "all") {  splitAtDash(line, lines);  allLines = 0;  }
59                                 else { allLines = 1;  }
60                         }
61                         
62                         label = validParameter->validFile(parameters, "label", false);                  
63                         if (label == "not found") { label = ""; }
64                         else { 
65                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
66                                 else { allLines = 1;  }
67                         }
68                         
69                         //make sure user did not use both the line and label parameters
70                         if ((line != "") && (label != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; abort = true; }
71                         //if the user has not specified any line or labels use the ones from read.otu
72                         else if ((line == "") && (label == "")) {  
73                                 allLines = globaldata->allLines; 
74                                 labels = globaldata->labels; 
75                                 lines = globaldata->lines;
76                         }
77                         
78                         groups = validParameter->validFile(parameters, "groups", false);                        
79                         if (groups == "not found") { groups = ""; }
80                         else { 
81                                 splitAtDash(groups, Groups);
82                                 globaldata->Groups = Groups;
83                         }
84                         
85                         format = globaldata->getFormat();
86                         calc = validParameter->validFile(parameters, "calc", false);                    
87                         if (calc == "not found") { 
88                                 if(format == "list") { calc = "sobs"; }
89                                 else { calc = "sharedsobs"; }
90                         }
91                         else { 
92                                  if (calc == "default")  {  
93                                         if(format == "list") { calc = "sobs"; }
94                                         else { calc = "sharedsobs"; }
95                                 }
96                         }
97                         splitAtDash(calc, Estimators);
98                         
99                         string temp;
100                         temp = validParameter->validFile(parameters, "abund", false);           if (temp == "not found") { temp = "10"; }
101                         convert(temp, abund); 
102
103                         delete validParameter;
104                         
105                         if (abort == false) {
106                                 validCalculator = new ValidCalculators();
107                 
108                                 int i;
109                                 
110                                 if (format == "list") {
111                                         for (i=0; i<Estimators.size(); i++) {
112                                                 if (validCalculator->isValidCalculator("vennsingle", Estimators[i]) == true) { 
113                                                         if (Estimators[i] == "sobs") { 
114                                                                 vennCalculators.push_back(new Sobs());
115                                                         }else if (Estimators[i] == "chao") { 
116                                                                 vennCalculators.push_back(new Chao1());
117                                                         }else if (Estimators[i] == "ace") {
118                                                                 if(abund < 5)
119                                                                         abund = 10;
120                                                                 vennCalculators.push_back(new Ace(abund));
121                                                         }
122                                                 }
123                                         }
124                                 }else {
125                                         for (i=0; i<Estimators.size(); i++) {
126                                                 if (validCalculator->isValidCalculator("vennshared", Estimators[i]) == true) { 
127                                                         if (Estimators[i] == "sharedsobs") { 
128                                                                 vennCalculators.push_back(new SharedSobsCS());
129                                                         }else if (Estimators[i] == "sharedchao") { 
130                                                                 vennCalculators.push_back(new SharedChao1());
131                                                         }else if (Estimators[i] == "sharedace") { 
132                                                                 vennCalculators.push_back(new SharedAce());
133                                                         }
134                                                 }
135                                         }
136                                 }
137                                 
138                                 venn = new Venn();
139                         }
140                 }
141
142                 
143                                 
144         }
145         catch(exception& e) {
146                 cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function VennCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
147                 exit(1);
148         }
149         catch(...) {
150                 cout << "An unknown error has occurred in the VennCommand class function VennCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
151                 exit(1);
152         }       
153 }
154
155 //**********************************************************************************************************************
156
157 void VennCommand::help(){
158         try {
159                 cout << "The venn command can only be executed after a successful read.otu command." << "\n";
160                 cout << "The venn command parameters are groups, calc, abund, line and label.  No parameters are required, but you may not use line and label at the same time." << "\n";
161                 cout << "The groups parameter allows you to specify which of the groups in your groupfile you would like included in your venn diagram, you may only use a maximum of 4 groups." << "\n";
162                 cout << "The group names are separated by dashes. The line and label allow you to select what distance levels you would like a venn diagram created for, and are also separated by dashes." << "\n";
163                 cout << "The venn command should be in the following format: venn(groups=yourGroups, calc=yourCalcs, line=yourLines, label=yourLabels, abund=yourAbund)." << "\n";
164                 cout << "Example venn(groups=A-B-C, line=1-3-5, calc=sharedsobs-sharedchao, abund=20)." << "\n";
165                 cout << "The default value for groups is all the groups in your groupfile up to 4, and all lines in your inputfile will be used." << "\n";
166                 cout << "The default value for calc is sobs if you have only read a list file or if you have selected only one group, and sharedsobs if you have multiple groups." << "\n";
167                 cout << "The default available estimators for calc are sobs, chao and ace if you have only read a list file, and sharedsobs, sharedchao and sharedace if you have read a list and group file or a shared file." << "\n";
168                 cout << "The only estmiator available four 4 groups is sharedsobs." << "\n";
169                 cout << "The venn command outputs a .svg file for each calculator you specify at each distance you choose." << "\n";
170                 cout << "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups)." << "\n" << "\n";
171         }
172         catch(exception& e) {
173                 cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
174                 exit(1);
175         }
176         catch(...) {
177                 cout << "An unknown error has occurred in the VennCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
178                 exit(1);
179         }       
180 }
181
182
183 //**********************************************************************************************************************
184
185 VennCommand::~VennCommand(){
186         delete input;
187         delete read;
188         delete venn;
189         for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
190 }
191
192 //**********************************************************************************************************************
193
194 int VennCommand::execute(){
195         try {
196         
197                 if (abort == true) { return 0; }
198                 
199                 int count = 1;
200                 SAbundVector* lastSAbund;
201                 vector<SharedRAbundVector*> lastLookup; 
202
203                 //if the users entered no valid calculators don't execute command
204                 if (vennCalculators.size() == 0) { return 0; }
205                 
206                 if (format == "sharedfile") {
207                         //you have groups
208                         read = new ReadOTUFile(globaldata->inputFileName);      
209                         read->read(&*globaldata); 
210                         
211                         input = globaldata->ginput;
212                         lookup = input->getSharedRAbundVectors();
213                         lastLookup = lookup;
214                 }else if (format == "list") {
215                         //you are using just a list file and have only one group
216                         read = new ReadOTUFile(globaldata->inputFileName);      
217                         read->read(&*globaldata); 
218                 
219                         sabund = globaldata->sabund;
220                         lastSAbund = globaldata->sabund;
221                         input = globaldata->ginput;
222                 }
223                 
224                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
225                 set<string> processedLabels;
226                 set<string> userLabels = labels;
227                 set<int> userLines = lines;
228                 
229                 if (format != "list") { 
230                         
231                         //as long as you are not at the end of the file or done wih the lines you want
232                         while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
233
234                                 if(allLines == 1 || lines.count(count) == 1 || labels.count(lookup[0]->getLabel()) == 1){                       
235                                         cout << lookup[0]->getLabel() << '\t' << count << endl;
236                                         processedLabels.insert(lookup[0]->getLabel());
237                                         userLabels.erase(lookup[0]->getLabel());
238                                         userLines.erase(count);
239                                         
240                                         if (lookup.size() > 4) {
241                                                 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;
242                                                 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
243                                         }
244                                         venn->getPic(lookup, vennCalculators);
245                                 }
246                                 
247                                 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
248                                         cout << lastLookup[0]->getLabel() << '\t' << count << endl;
249                                         processedLabels.insert(lastLookup[0]->getLabel());
250                                         userLabels.erase(lastLookup[0]->getLabel());
251
252                                         if (lastLookup.size() > 4) {
253                                                 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;
254                                                 for (int i = lastLookup.size(); i > 4; i--) { lastLookup.pop_back(); } //no memmory leak because pop_back calls destructor
255                                         }                               
256                                         venn->getPic(lastLookup, vennCalculators);
257                                 }
258                                 
259                                 //prevent memory leak
260                                 if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  } }
261                                 lastLookup = lookup;    
262                                                 
263                                 //get next line to process
264                                 lookup = input->getSharedRAbundVectors();
265                                 count++;
266                         }
267                         
268                         //output error messages about any remaining user labels
269                         set<string>::iterator it;
270                         bool needToRun = false;
271                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
272                                 cout << "Your file does not include the label "<< *it; 
273                                 if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
274                                         cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
275                                         needToRun = true;
276                                 }else {
277                                         cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
278                                 }
279                         }
280                 
281                         //run last line if you need to
282                         if (needToRun == true)  {
283                                 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
284                                 if (lastLookup.size() > 4) {
285                                         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;
286                                         for (int i = lastLookup.size(); i > 3; i--) { delete lastLookup[i]; lastLookup.pop_back(); }
287                                 }                               
288                                 venn->getPic(lastLookup, vennCalculators);
289                         }
290                 
291                         for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  }
292
293                         //reset groups parameter
294                         globaldata->Groups.clear();  
295                         
296                 }else{
297                 
298                         while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
299                 
300                                 if(allLines == 1 || lines.count(count) == 1 || labels.count(sabund->getLabel()) == 1){                  
301         
302                                         cout << sabund->getLabel() << '\t' << count << endl;
303                                         venn->getPic(sabund, vennCalculators);
304                                         
305                                         processedLabels.insert(sabund->getLabel());
306                                         userLabels.erase(sabund->getLabel());
307                                         userLines.erase(count);
308                                 }
309                                 
310                                 if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastSAbund->getLabel()) != 1)) {
311
312                                         cout << lastSAbund->getLabel() << '\t' << count << endl;
313                                         venn->getPic(lastSAbund, vennCalculators);
314                                         
315                                         processedLabels.insert(lastSAbund->getLabel());
316                                         userLabels.erase(lastSAbund->getLabel());
317                                 }               
318                                 
319                                 if (count != 1) { delete lastSAbund; }
320                                 lastSAbund = sabund;                    
321
322                                 sabund = input->getSAbundVector();
323                                 count++;
324                         }
325                         
326                         //output error messages about any remaining user labels
327                         set<string>::iterator it;
328                         bool needToRun = false;
329                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
330                                 cout << "Your file does not include the label "<< *it; 
331                                 if (processedLabels.count(lastSAbund->getLabel()) != 1) {
332                                         cout << ". I will use " << lastSAbund->getLabel() << "." << endl;
333                                         needToRun = true;
334                                 }else {
335                                         cout << ". Please refer to " << lastSAbund->getLabel() << "." << endl;
336                                 }
337                         }
338                 
339                         //run last line if you need to
340                         if (needToRun == true)  {
341                                 cout << lastSAbund->getLabel() << '\t' << count << endl;
342                                 venn->getPic(lastSAbund, vennCalculators);
343                         }
344                         delete lastSAbund;
345                 }
346                 
347                 return 0;
348         }
349         catch(exception& e) {
350                 cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
351                 exit(1);
352         }
353         catch(...) {
354                 cout << "An unknown error has occurred in the VennCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
355                 exit(1);
356         }               
357 }
358
359 //**********************************************************************************************************************