]> git.donarmstrong.com Git - mothur.git/blob - venncommand.cpp
sffinfo bug with flow grams right index when clipQualRight=0
[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 #include "nseqs.h"
19
20 //**********************************************************************************************************************
21 vector<string> VennCommand::setParameters(){    
22         try {
23                 CommandParameter plist("list", "InputTypes", "", "", "LRSS", "LRSS", "none","svg",false,false,true); parameters.push_back(plist);
24                 CommandParameter pshared("shared", "InputTypes", "", "", "LRSS", "LRSS", "none","svg",false,false,true); parameters.push_back(pshared); 
25                 CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
26                 CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
27                 CommandParameter pcalc("calc", "String", "", "", "", "", "","",false,false); parameters.push_back(pcalc);
28                 CommandParameter pabund("abund", "Number", "", "10", "", "", "","",false,false); parameters.push_back(pabund);
29                 CommandParameter pnseqs("nseqs", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(pnseqs);
30         CommandParameter psharedotus("sharedotus", "Boolean", "", "t", "", "", "","",false,false); parameters.push_back(psharedotus);
31                 CommandParameter pfontsize("fontsize", "Number", "", "24", "", "", "","",false,false); parameters.push_back(pfontsize);
32                 CommandParameter ppermute("permute", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(ppermute);
33                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
34                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
35                 
36                 vector<string> myArray;
37                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
38                 return myArray;
39         }
40         catch(exception& e) {
41                 m->errorOut(e, "VennCommand", "setParameters");
42                 exit(1);
43         }
44 }
45 //**********************************************************************************************************************
46 string VennCommand::getHelpString(){    
47         try {
48                 string helpString = "";
49                 helpString += "The venn command parameters are list, shared, groups, calc, abund, nseqs, permute, sharedotus, fontsize and label.   shared, relabund, list, rabund or sabund is required unless you have a valid current file.\n";
50                 helpString += "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";
51                 helpString += "The group names are separated by dashes. The label allows you to select what distance levels you would like a venn diagram created for, and are also separated by dashes.\n";
52                 helpString += "The fontsize parameter allows you to adjust the font size of the picture created, default=24.\n";
53                 helpString += "The venn command should be in the following format: venn(groups=yourGroups, calc=yourCalcs, label=yourLabels, abund=yourAbund).\n";
54                 helpString += "Example venn(groups=A-B-C, calc=sharedsobs-sharedchao, abund=20).\n";
55                 helpString += "The default value for groups is all the groups in your groupfile up to 4, and all labels in your inputfile will be used.\n";
56                 helpString += "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";
57                 helpString += "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 shared file.\n";
58                 helpString += "The nseqs parameter will output the number of sequences represented by the otus in the picture, default=F.\n";
59                 helpString += "If you have more than 4 groups, the permute parameter will find all possible combos of 4 of your groups and create pictures for them, default=F.\n";
60                 helpString += "The only estimators available four 4 groups are sharedsobs and sharedchao.\n";
61         helpString += "The sharedotus parameter can be used with the sharedsobs calculator to get the names of the OTUs in each section of the venn diagram. Default=t.\n";
62                 helpString += "The venn command outputs a .svg file for each calculator you specify at each distance you choose.\n";
63                 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n";
64                 return helpString;
65         }
66         catch(exception& e) {
67                 m->errorOut(e, "VennCommand", "getHelpString");
68                 exit(1);
69         }
70 }
71 //**********************************************************************************************************************
72 string VennCommand::getOutputPattern(string type) {
73     try {
74         string pattern = "";
75         
76         if (type == "svg") {  pattern = "[filename],svg"; } 
77         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
78         
79         return pattern;
80     }
81     catch(exception& e) {
82         m->errorOut(e, "VennCommand", "getOutputPattern");
83         exit(1);
84     }
85 }
86
87 //**********************************************************************************************************************
88 VennCommand::VennCommand(){     
89         try {
90                 abort = true; calledHelp = true; 
91                 setParameters();
92                 vector<string> tempOutNames;
93                 outputTypes["svg"] = tempOutNames;
94         }
95         catch(exception& e) {
96                 m->errorOut(e, "VennCommand", "VennCommand");
97                 exit(1);
98         }
99 }
100 //**********************************************************************************************************************
101
102 VennCommand::VennCommand(string option)  {
103         try {
104                 abort = false; calledHelp = false;   
105                 allLines = 1;
106                         
107                 //allow user to run help
108                 if(option == "help") { help(); abort = true; calledHelp = true; }
109                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
110                 
111                 else {
112                         vector<string> myArray = setParameters();
113                         
114                         OptionParser parser(option);
115                         map<string,string> parameters = parser.getParameters();
116                         map<string,string>::iterator it;
117                         
118                         ValidParameters validParameter;
119                         
120                         //check to make sure all parameters are valid for command
121                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
122                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
123                         }
124                         
125                         //if the user changes the input directory command factory will send this info to us in the output parameter 
126                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
127                         if (inputDir == "not found"){   inputDir = "";          }
128                         else {
129                                 string path;
130                                 it = parameters.find("shared");
131                                 //user has given a template file
132                                 if(it != parameters.end()){ 
133                                         path = m->hasPath(it->second);
134                                         //if the user has not given a path then, add inputdir. else leave path alone.
135                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
136                                 }
137                                 
138                                 it = parameters.find("list");
139                                 //user has given a template file
140                                 if(it != parameters.end()){ 
141                                         path = m->hasPath(it->second);
142                                         //if the user has not given a path then, add inputdir. else leave path alone.
143                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
144                                 }
145                         }
146                         
147                         //check for required parameters
148                         listfile = validParameter.validFile(parameters, "list", true);
149                         if (listfile == "not open") { listfile = ""; abort = true; }
150                         else if (listfile == "not found") { listfile = ""; }
151                         else {  format = "list"; inputfile = listfile; m->setListFile(listfile); }
152                         
153                         sharedfile = validParameter.validFile(parameters, "shared", true);
154                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }        
155                         else if (sharedfile == "not found") { sharedfile = ""; }
156                         else {  format = "sharedfile"; inputfile = sharedfile; m->setSharedFile(sharedfile); }
157                         
158                         if ((sharedfile == "") && (listfile == "")) { 
159                                 //is there are current file available for any of these?
160                                 //give priority to shared, then list, then rabund, then sabund
161                                 //if there is a current shared file, use it
162                                 sharedfile = m->getSharedFile(); 
163                                 if (sharedfile != "") { inputfile = sharedfile; format = "sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
164                                 else { 
165                                         listfile = m->getListFile(); 
166                                         if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
167                                         else { 
168                                                 m->mothurOut("No valid current files. You must provide a list or shared file."); m->mothurOutEndLine(); 
169                                                 abort = true;
170                                         }
171                                 }
172                         }
173                         
174                         //if the user changes the output directory command factory will send this info to us in the output parameter 
175                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(inputfile);              }
176
177                         //check for optional parameter and set defaults
178                         // ...at some point should added some additional type checking...
179                         label = validParameter.validFile(parameters, "label", false);                   
180                         if (label == "not found") { label = ""; }
181                         else { 
182                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
183                                 else { allLines = 1;  }
184                         }
185                         
186                         groups = validParameter.validFile(parameters, "groups", false);                 
187                         if (groups == "not found") { groups = ""; }
188                         else { 
189                                 m->splitAtDash(groups, Groups);
190                                 m->setGroups(Groups);
191                         }
192                         
193                         calc = validParameter.validFile(parameters, "calc", false);                     
194                         if (calc == "not found") { 
195                                 if(format == "list") { calc = "sobs"; }
196                                 else { calc = "sharedsobs"; }
197                         }
198                         else { 
199                                  if (calc == "default")  {  
200                                         if(format == "list") { calc = "sobs"; }
201                                         else { calc = "sharedsobs"; }
202                                 }
203                         }
204                         m->splitAtDash(calc, Estimators);
205                         if (m->inUsersGroups("citation", Estimators)) { 
206                                 ValidCalculators validCalc; validCalc.printCitations(Estimators); 
207                                 //remove citation from list of calcs
208                                 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") {  Estimators.erase(Estimators.begin()+i); break; } }
209                         }
210                         
211                         string temp;
212                         temp = validParameter.validFile(parameters, "abund", false);            if (temp == "not found") { temp = "10"; }
213                         m->mothurConvert(temp, abund); 
214                         
215                         temp = validParameter.validFile(parameters, "nseqs", false);            if (temp == "not found"){       temp = "f";                             }
216                         nseqs = m->isTrue(temp); 
217
218                         temp = validParameter.validFile(parameters, "permute", false);          if (temp == "not found"){       temp = "f";                             }
219                         perm = m->isTrue(temp); 
220             
221             temp = validParameter.validFile(parameters, "sharedotus", false);           if (temp == "not found"){       temp = "t";                             }
222                         sharedOtus = m->isTrue(temp); 
223                         
224                         temp = validParameter.validFile(parameters, "fontsize", false);         if (temp == "not found") { temp = "24"; }
225                         m->mothurConvert(temp, fontsize);
226
227                 }
228                                 
229         }
230         catch(exception& e) {
231                 m->errorOut(e, "VennCommand", "VennCommand");
232                 exit(1);
233         }
234 }
235 //**********************************************************************************************************************
236
237 int VennCommand::execute(){
238         try {
239         
240                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
241         
242                 ValidCalculators validCalculator;
243                                         
244                 if (format == "list") {
245                         for (int i=0; i<Estimators.size(); i++) {
246                                 if (validCalculator.isValidCalculator("vennsingle", Estimators[i]) == true) { 
247                                         if (Estimators[i] == "sobs") { 
248                                                 vennCalculators.push_back(new Sobs());
249                                         }else if (Estimators[i] == "chao") { 
250                                                 vennCalculators.push_back(new Chao1());
251                                         }else if (Estimators[i] == "ace") {
252                                                 if(abund < 5) { abund = 10; }
253                                                 vennCalculators.push_back(new Ace(abund));
254                                         }
255                                 }
256                         }
257                 }else {
258                         for (int i=0; i<Estimators.size(); i++) {
259                                 if (validCalculator.isValidCalculator("vennshared", Estimators[i]) == true) { 
260                                         if (Estimators[i] == "sharedsobs") { 
261                                                 vennCalculators.push_back(new SharedSobsCS());
262                                         }else if (Estimators[i] == "sharedchao") { 
263                                                 vennCalculators.push_back(new SharedChao1());
264                                         }else if (Estimators[i] == "sharedace") { 
265                                                 vennCalculators.push_back(new SharedAce());
266                                         }
267                                 }
268                         }
269                 }
270                         
271                 //if the users entered no valid calculators don't execute command
272                 if (vennCalculators.size() == 0) { m->mothurOut("No valid calculators given, please correct."); m->mothurOutEndLine(); return 0;  }
273                 
274                 venn = new Venn(outputDir, nseqs, inputfile, fontsize, sharedOtus); 
275                 input = new InputData(inputfile, format);
276                 
277                 string lastLabel;
278                 
279                 if (format == "sharedfile") {
280                         lookup = input->getSharedRAbundVectors();
281                         lastLabel = lookup[0]->getLabel();
282                         
283                         if ((lookup.size() > 4) && (perm)) { combosOfFour = findCombinations(lookup.size()); }
284                 }else if (format == "list") {
285                         sabund = input->getSAbundVector();
286                         lastLabel = sabund->getLabel();
287                 }
288                 
289                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
290                 set<string> processedLabels;
291                 set<string> userLabels = labels;
292
293                 if (format != "list") { 
294                         
295                         //as long as you are not at the end of the file or done wih the lines you want
296                         while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
297                         
298                                 if (m->control_pressed) {
299                                         for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
300                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
301                                         m->clearGroups(); delete venn; delete input;
302                                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  }
303                                         return 0;
304                                 }
305
306                                 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
307                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
308                                         processedLabels.insert(lookup[0]->getLabel());
309                                         userLabels.erase(lookup[0]->getLabel());
310                                         
311                                         if ((lookup.size() > 4) && (!perm)){
312                                                 m->mothurOut("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. If you set perm=t, I will find all possible combos of 4 groups."); m->mothurOutEndLine();
313                                                 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
314                                         
315                                                 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
316                                                 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]);  } }
317
318                                         }else if ((lookup.size() > 4) && (perm)) {
319                                                 set< set<int> >::iterator it3;
320                                                 set<int>::iterator it2;
321                                                 for (it3 = combosOfFour.begin(); it3 != combosOfFour.end(); it3++) {  
322                         
323                                                         set<int> poss = *it3;
324                                                         vector<SharedRAbundVector*> subset;
325                                                         for (it2 = poss.begin(); it2 != poss.end(); it2++) {   subset.push_back(lookup[*it2]);   }
326                                                         
327                                                         vector<string> outfilenames = venn->getPic(subset, vennCalculators);
328                                                         for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]); }  }
329                                                 }               
330                                         }else {
331                                                 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
332                                                 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]);  }  }
333                                         }                                       
334                                 }
335                                 
336                                 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
337                                         string saveLabel = lookup[0]->getLabel();
338                                         
339                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
340                                         lookup = input->getSharedRAbundVectors(lastLabel);
341
342                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
343                                         processedLabels.insert(lookup[0]->getLabel());
344                                         userLabels.erase(lookup[0]->getLabel());
345
346                                         if ((lookup.size() > 4) && (!perm)){
347                                                 m->mothurOut("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. If you set perm=t, I will find all possible combos of 4 groups."); m->mothurOutEndLine();
348                                                 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
349                                         
350                                                 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
351                                                 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]);  }  }
352
353                                         }else if ((lookup.size() > 4) && (perm)) {
354                                                 set< set<int> >::iterator it3;
355                                                 set<int>::iterator it2;
356                                                 for (it3 = combosOfFour.begin(); it3 != combosOfFour.end(); it3++) {  
357                         
358                                                         set<int> poss = *it3;
359                                                         vector<SharedRAbundVector*> subset;
360                                                         for (it2 = poss.begin(); it2 != poss.end(); it2++) {   subset.push_back(lookup[*it2]);   }
361                                                         
362                                                         vector<string> outfilenames = venn->getPic(subset, vennCalculators);
363                                                         for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]);  }  }
364                                                 }               
365                                         }else {
366                                                 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
367                                                 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]);  }  }
368                                         }
369                                                                                 
370                                         //restore real lastlabel to save below
371                                         lookup[0]->setLabel(saveLabel);
372                                 }
373                                 
374                                 
375                                 lastLabel = lookup[0]->getLabel();      
376                                                 
377                                 //get next line to process
378                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
379                                 lookup = input->getSharedRAbundVectors();
380                         }
381                         
382                         if (m->control_pressed) {
383                                         for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
384                                         m->clearGroups(); delete venn; delete input; 
385                                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  }
386                                         return 0;
387                         }
388
389                         
390                         //output error messages about any remaining user labels
391                         set<string>::iterator it;
392                         bool needToRun = false;
393                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
394                                 m->mothurOut("Your file does not include the label " + *it); 
395                                 if (processedLabels.count(lastLabel) != 1) {
396                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
397                                         needToRun = true;
398                                 }else {
399                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
400                                 }
401                         }
402                 
403                         //run last label if you need to
404                         if (needToRun == true)  {
405                                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {     delete lookup[i]; }  } 
406                                         lookup = input->getSharedRAbundVectors(lastLabel);
407
408                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
409                                         processedLabels.insert(lookup[0]->getLabel());
410                                         userLabels.erase(lookup[0]->getLabel());
411
412                                         if ((lookup.size() > 4) && (!perm)){
413                                                 m->mothurOut("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. If you set perm=t, I will find all possible combos of 4 groups."); m->mothurOutEndLine();
414                                                 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
415                                         
416                                                 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
417                                                 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]); }  }
418
419                                         }else if ((lookup.size() > 4) && (perm)) {
420                                                 set< set<int> >::iterator it3;
421                                                 set<int>::iterator it2;
422                                                 for (it3 = combosOfFour.begin(); it3 != combosOfFour.end(); it3++) {  
423                         
424                                                         set<int> poss = *it3;
425                                                         vector<SharedRAbundVector*> subset;
426                                                         for (it2 = poss.begin(); it2 != poss.end(); it2++) {   subset.push_back(lookup[*it2]);   }
427                                                         
428                                                         vector<string> outfilenames = venn->getPic(subset, vennCalculators);
429                                                         for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]); }  }
430                                                 }               
431                                         }else {
432                                                 vector<string> outfilenames = venn->getPic(lookup, vennCalculators);
433                                                 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]);  }  }
434                                         }
435                                         
436                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
437                         }
438                 
439
440                         //reset groups parameter
441                         m->clearGroups();  
442                         
443                         if (m->control_pressed) {
444                                         m->clearGroups(); delete venn; delete input;
445                                         for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
446                                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  }
447                                         return 0;
448                         }
449
450                         
451                 }else{
452                 
453                         while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
454                         
455                                 if (m->control_pressed) {
456                                         for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
457                                         delete sabund; delete venn; delete input;
458                                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  }
459                                         return 0;
460                                 }
461                 
462                                 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){                     
463         
464                                         m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
465                                         vector<string> outfilenames = venn->getPic(sabund, vennCalculators);
466                                         for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]);  }  }
467
468                                         
469                                         processedLabels.insert(sabund->getLabel());
470                                         userLabels.erase(sabund->getLabel());
471                                 }
472                                 
473                                 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
474                                         string saveLabel = sabund->getLabel();
475                                 
476                                         delete sabund;
477                                         sabund = input->getSAbundVector(lastLabel);
478                                         
479                                         m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
480                                         vector<string> outfilenames = venn->getPic(sabund, vennCalculators);
481                                         for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]);  }  }
482
483                                         
484                                         processedLabels.insert(sabund->getLabel());
485                                         userLabels.erase(sabund->getLabel());
486                                         
487                                         //restore real lastlabel to save below
488                                         sabund->setLabel(saveLabel);
489                                 }               
490                                 
491                                 lastLabel = sabund->getLabel();         
492                                 
493                                 delete sabund;
494                                 sabund = input->getSAbundVector();
495                         }
496                         
497                         if (m->control_pressed) {
498                                         for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
499                                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  }
500                                         delete venn; delete input;
501                                         return 0;
502                         }
503                         
504                         //output error messages about any remaining user labels
505                         set<string>::iterator it;
506                         bool needToRun = false;
507                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
508                                 m->mothurOut("Your file does not include the label " + *it); 
509                                 if (processedLabels.count(lastLabel) != 1) {
510                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
511                                         needToRun = true;
512                                 }else {
513                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
514                                 }
515                         }
516                 
517                         //run last label if you need to
518                         if (needToRun == true)  {
519                                 if (sabund != NULL) {   delete sabund;  }
520                                 sabund = input->getSAbundVector(lastLabel);
521                                         
522                                 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
523                                 vector<string> outfilenames = venn->getPic(sabund, vennCalculators);
524                                 for(int i = 0; i < outfilenames.size(); i++) { if (outfilenames[i] != "control" ) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]);  }  }
525
526                                 delete sabund;
527                                         
528                         }
529                         
530                         if (m->control_pressed) {
531                                         delete venn; delete input;
532                                         for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
533                                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  }
534                                         return 0;
535                         }
536                 }
537                 
538                 for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
539                 delete venn; delete input;
540                 
541                 m->mothurOutEndLine();
542                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
543                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
544                 m->mothurOutEndLine();
545
546                 
547                 return 0;
548         }
549         catch(exception& e) {
550                 m->errorOut(e, "VennCommand", "execute");
551                 exit(1);
552         }
553 }
554 //**********************************************************************************************************************
555 //returns a vector of sets containing the 4 group combinations
556 set< set<int> > VennCommand::findCombinations(int lookupSize){
557         try {
558                 set< set<int> > combos;
559                 
560                 set<int> possibles;
561                 for (int i = 0; i < lookupSize; i++) {  possibles.insert(i);  }
562                 
563                 getCombos(possibles, combos);
564                 
565                 return combos;
566                 
567         }
568         catch(exception& e) {
569                 m->errorOut(e, "VennCommand", "findCombinations");
570                 exit(1);
571         }
572 }
573 //**********************************************************************************************************************
574 //recusively finds combos of 4
575 int VennCommand::getCombos(set<int> possibles, set< set<int> >& combos){
576         try {
577                 
578                 if (possibles.size() == 4) { //done
579                         if (combos.count(possibles) == 0) { //no dups
580                                 combos.insert(possibles);
581                         }
582                 }else { //we still have work to do
583                         set<int>::iterator it;
584                         set<int>::iterator it2;
585                         for (it = possibles.begin(); it != possibles.end(); it++) {  
586                                 
587                                 set<int> newPossibles;
588                                 for (it2 = possibles.begin(); it2 != possibles.end(); it2++) {  //all possible combos of one length smaller
589                                         if (*it != *it2) { 
590                                                 newPossibles.insert(*it2);
591                                         }
592                                 }
593                                 getCombos(newPossibles, combos);
594                         }
595                 }
596                 
597                 return 0;
598         }
599         catch(exception& e) {
600                 m->errorOut(e, "VennCommand", "getCombos");
601                 exit(1);
602         }
603 }
604
605 //**********************************************************************************************************************