]> git.donarmstrong.com Git - mothur.git/blob - venncommand.cpp
fixed some issues while testing 1.6
[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                         OptionParser parser(option);
39                         map<string,string> parameters = parser.getParameters();
40                         
41                         ValidParameters validParameter;
42                         
43                         //check to make sure all parameters are valid for command
44                         for (map<string,string>::iterator 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                                 mothurOut("You must read a list, or a list and a group, or a shared before you can use the venn command."); mothurOutEndLine(); 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 != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); 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                         if (abort == false) {
104                                 validCalculator = new ValidCalculators();
105                 
106                                 int i;
107                                 
108                                 if (format == "list") {
109                                         for (i=0; i<Estimators.size(); i++) {
110                                                 if (validCalculator->isValidCalculator("vennsingle", Estimators[i]) == true) { 
111                                                         if (Estimators[i] == "sobs") { 
112                                                                 vennCalculators.push_back(new Sobs());
113                                                         }else if (Estimators[i] == "chao") { 
114                                                                 vennCalculators.push_back(new Chao1());
115                                                         }else if (Estimators[i] == "ace") {
116                                                                 if(abund < 5)
117                                                                         abund = 10;
118                                                                 vennCalculators.push_back(new Ace(abund));
119                                                         }
120                                                 }
121                                         }
122                                 }else {
123                                         for (i=0; i<Estimators.size(); i++) {
124                                                 if (validCalculator->isValidCalculator("vennshared", Estimators[i]) == true) { 
125                                                         if (Estimators[i] == "sharedsobs") { 
126                                                                 vennCalculators.push_back(new SharedSobsCS());
127                                                         }else if (Estimators[i] == "sharedchao") { 
128                                                                 vennCalculators.push_back(new SharedChao1());
129                                                         }else if (Estimators[i] == "sharedace") { 
130                                                                 vennCalculators.push_back(new SharedAce());
131                                                         }
132                                                 }
133                                         }
134                                 }
135                                 
136                                 venn = new Venn();
137                         }
138                 }
139
140                 
141                                 
142         }
143         catch(exception& e) {
144                 errorOut(e, "VennCommand", "VennCommand");
145                 exit(1);
146         }
147 }
148
149 //**********************************************************************************************************************
150
151 void VennCommand::help(){
152         try {
153                 mothurOut("The venn command can only be executed after a successful read.otu command.\n");
154                 mothurOut("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");
155                 mothurOut("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");
156                 mothurOut("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");
157                 mothurOut("The venn command should be in the following format: venn(groups=yourGroups, calc=yourCalcs, line=yourLines, label=yourLabels, abund=yourAbund).\n");
158                 mothurOut("Example venn(groups=A-B-C, line=1-3-5, calc=sharedsobs-sharedchao, abund=20).\n");
159                 mothurOut("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");
160                 mothurOut("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");
161                 mothurOut("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");
162                 mothurOut("The only estmiator available four 4 groups is sharedsobs.\n");
163                 mothurOut("The venn command outputs a .svg file for each calculator you specify at each distance you choose.\n");
164                 mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
165         }
166         catch(exception& e) {
167                 errorOut(e, "VennCommand", "help");
168                 exit(1);
169         }
170 }
171
172
173 //**********************************************************************************************************************
174
175 VennCommand::~VennCommand(){
176         if (abort == false) {
177                 delete input; globaldata->ginput = NULL;
178                 delete read;
179                 delete venn;
180                 globaldata->sabund = NULL;
181         }
182         
183 }
184
185 //**********************************************************************************************************************
186
187 int VennCommand::execute(){
188         try {
189         
190                 if (abort == true) { return 0; }
191                 
192                 int count = 1;
193                 string lastLabel;
194                 
195                 //if the users entered no valid calculators don't execute command
196                 if (vennCalculators.size() == 0) { return 0; }
197                 
198                 if (format == "sharedfile") {
199                         //you have groups
200                         read = new ReadOTUFile(globaldata->inputFileName);      
201                         read->read(&*globaldata); 
202                         
203                         input = globaldata->ginput;
204                         lookup = input->getSharedRAbundVectors();
205                         lastLabel = lookup[0]->getLabel();
206                 }else if (format == "list") {
207                         //you are using just a list file and have only one group
208                         read = new ReadOTUFile(globaldata->inputFileName);      
209                         read->read(&*globaldata); 
210                 
211                         sabund = globaldata->sabund;
212                         lastLabel = sabund->getLabel();
213                         input = globaldata->ginput;
214                 }
215                 
216                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
217                 set<string> processedLabels;
218                 set<string> userLabels = labels;
219                 set<int> userLines = lines;
220                 
221                 if (format != "list") { 
222                         
223                         //as long as you are not at the end of the file or done wih the lines you want
224                         while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
225
226                                 if(allLines == 1 || lines.count(count) == 1 || labels.count(lookup[0]->getLabel()) == 1){                       
227                                         mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
228                                         processedLabels.insert(lookup[0]->getLabel());
229                                         userLabels.erase(lookup[0]->getLabel());
230                                         userLines.erase(count);
231                                         
232                                         if (lookup.size() > 4) {
233                                                 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."); mothurOutEndLine();
234                                                 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
235                                         }
236                                         venn->getPic(lookup, vennCalculators);
237                                 }
238                                 
239                                 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
240                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
241                                         lookup = input->getSharedRAbundVectors(lastLabel);
242
243                                         mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
244                                         processedLabels.insert(lookup[0]->getLabel());
245                                         userLabels.erase(lookup[0]->getLabel());
246
247                                         if (lookup.size() > 4) {
248                                                 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."); mothurOutEndLine();
249                                                 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
250                                         }                               
251                                         venn->getPic(lookup, vennCalculators);
252                                 }
253                                 
254                                 
255                                 lastLabel = lookup[0]->getLabel();      
256                                                 
257                                 //get next line to process
258                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
259                                 lookup = input->getSharedRAbundVectors();
260                                 count++;
261                         }
262                         
263                         //output error messages about any remaining user labels
264                         set<string>::iterator it;
265                         bool needToRun = false;
266                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
267                                 mothurOut("Your file does not include the label " + *it); 
268                                 if (processedLabels.count(lastLabel) != 1) {
269                                         mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
270                                         needToRun = true;
271                                 }else {
272                                         mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
273                                 }
274                         }
275                 
276                         //run last line if you need to
277                         if (needToRun == true)  {
278                                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {     delete lookup[i]; }  } 
279                                         lookup = input->getSharedRAbundVectors(lastLabel);
280
281                                         mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
282                                         processedLabels.insert(lookup[0]->getLabel());
283                                         userLabels.erase(lookup[0]->getLabel());
284
285                                         if (lookup.size() > 4) {
286                                                 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."); mothurOutEndLine();
287                                                 for (int i = lookup.size(); i > 4; i--) { lookup.pop_back(); } //no memmory leak because pop_back calls destructor
288                                         }                               
289                                         venn->getPic(lookup, vennCalculators);
290                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
291                         }
292                 
293
294                         //reset groups parameter
295                         globaldata->Groups.clear();  
296                         
297                 }else{
298                 
299                         while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
300                 
301                                 if(allLines == 1 || lines.count(count) == 1 || labels.count(sabund->getLabel()) == 1){                  
302         
303                                         mothurOut(sabund->getLabel()); mothurOutEndLine();
304                                         venn->getPic(sabund, vennCalculators);
305                                         
306                                         processedLabels.insert(sabund->getLabel());
307                                         userLabels.erase(sabund->getLabel());
308                                         userLines.erase(count);
309                                 }
310                                 
311                                 if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
312                                         delete sabund;
313                                         sabund = input->getSAbundVector(lastLabel);
314                                         
315                                         mothurOut(sabund->getLabel()); mothurOutEndLine();
316                                         venn->getPic(sabund, vennCalculators);
317                                         
318                                         processedLabels.insert(sabund->getLabel());
319                                         userLabels.erase(sabund->getLabel());
320                                 }               
321                                 
322                                 lastLabel = sabund->getLabel();         
323                                 
324                                 delete sabund;
325                                 sabund = input->getSAbundVector();
326                                 count++;
327                         }
328                         
329                         //output error messages about any remaining user labels
330                         set<string>::iterator it;
331                         bool needToRun = false;
332                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
333                                 mothurOut("Your file does not include the label " + *it); 
334                                 if (processedLabels.count(lastLabel) != 1) {
335                                         mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
336                                         needToRun = true;
337                                 }else {
338                                         mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
339                                 }
340                         }
341                 
342                         //run last line if you need to
343                         if (needToRun == true)  {
344                                 if (sabund != NULL) {   delete sabund;  }
345                                 sabund = input->getSAbundVector(lastLabel);
346                                         
347                                 mothurOut(sabund->getLabel()); mothurOutEndLine();
348                                 venn->getPic(sabund, vennCalculators);
349                                 delete sabund;
350                                         
351                         }
352                         
353                 }
354                 
355                 for (int i = 0; i < vennCalculators.size(); i++) {      delete vennCalculators[i];      }
356                 return 0;
357         }
358         catch(exception& e) {
359                 errorOut(e, "VennCommand", "execute");
360                 exit(1);
361         }
362 }
363
364 //**********************************************************************************************************************