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