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