]> git.donarmstrong.com Git - mothur.git/blobdiff - venncommand.cpp
broke up globaldata and moved error checking and help into commands
[mothur.git] / venncommand.cpp
index bb3762cc54f741d1e7c0a1fd7bdf9bc6c35f38db..0bed752e08ccb5a7672a5b5e1744569700cf73ab 100644 (file)
 
 //**********************************************************************************************************************
 
-VennCommand::VennCommand(){
+VennCommand::VennCommand(string option){
        try {
                globaldata = GlobalData::getInstance();
-               format = globaldata->getFormat();
-               validCalculator = new ValidCalculators();
-               
-               int i;
-               
-               if (format == "list") {
-                       for (i=0; i<globaldata->Estimators.size(); i++) {
-                               if (validCalculator->isValidCalculator("vennsingle", globaldata->Estimators[i]) == true) { 
-                                       if (globaldata->Estimators[i] == "sobs") { 
-                                               vennCalculators.push_back(new Sobs());
-                                       }else if (globaldata->Estimators[i] == "chao") { 
-                                               vennCalculators.push_back(new Chao1());
-                                       }else if (globaldata->Estimators[i] == "ace") {
-                                               convert(globaldata->getAbund(), abund);
-                                               if(abund < 5)
-                                                       abund = 10;
-                                               vennCalculators.push_back(new Ace(abund));
-                                       //}else if (globaldata->Estimators[i] == "jack") {      
-                                               //vennCalculators.push_back(new Jackknife());
-                                       }
+               abort = false;
+               allLines = 1;
+               lines.clear();
+               labels.clear();
+               
+               //allow user to run help
+               if(option == "help") { help(); abort = true; }
+               
+               else {
+                       //valid paramters for this command
+                       string AlignArray[] =  {"groups","line","label","calc", "abund"};
+                       vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
+                       
+                       parser = new OptionParser();
+                       parser->parse(option, parameters);  delete parser;
+                       
+                       ValidParameters* validParameter = new ValidParameters();
+               
+                       //check to make sure all parameters are valid for command
+                       for (it = parameters.begin(); it != parameters.end(); it++) { 
+                               if (validParameter->isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
+                       }
+                       
+                       //make sure the user has already run the read.otu command
+                       if ((globaldata->getListFile() == "") && (globaldata->getSharedFile() == "")) {
+                                cout << "You must read a list, or a list and a group, or a shared before you can use the venn command." << endl; abort = true; 
+                       }
+
+                       //check for optional parameter and set defaults
+                       // ...at some point should added some additional type checking...
+                       line = validParameter->validFile(parameters, "line", false);                            
+                       if (line == "not found") { line = "";  }
+                       else { 
+                               if(line != "all") {  splitAtDash(line, lines);  allLines = 0;  }
+                               else { allLines = 1;  }
+                       }
+                       
+                       label = validParameter->validFile(parameters, "label", false);                  
+                       if (label == "not found") { label = ""; }
+                       else { 
+                               if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
+                               else { allLines = 1;  }
+                       }
+                       
+                       //make sure user did not use both the line and label parameters
+                       if ((line != "") && (label != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; abort = true; }
+                       //if the user has not specified any line or labels use the ones from read.otu
+                       else if ((line == "") && (label == "")) {  
+                               allLines = globaldata->allLines; 
+                               labels = globaldata->labels; 
+                               lines = globaldata->lines;
+                       }
+                       
+                       groups = validParameter->validFile(parameters, "groups", false);                        
+                       if (groups == "not found") { groups = ""; }
+                       else { 
+                               splitAtDash(groups, Groups);
+                               globaldata->Groups = Groups;
+                       }
+                       
+                       format = globaldata->getFormat();
+                       calc = validParameter->validFile(parameters, "calc", false);                    
+                       if (calc == "not found") { 
+                               if(format == "list") { calc = "sobs"; }
+                               else { calc = "sharedsobs"; }
+                       }
+                       else { 
+                                if (calc == "default")  {  
+                                       if(format == "list") { calc = "sobs"; }
+                                       else { calc = "sharedsobs"; }
                                }
                        }
-               }else {
-                       for (i=0; i<globaldata->Estimators.size(); i++) {
-                               if (validCalculator->isValidCalculator("vennshared", globaldata->Estimators[i]) == true) { 
-                                       if (globaldata->Estimators[i] == "sharedsobs") { 
-                                               vennCalculators.push_back(new SharedSobsCS());
-                                       }else if (globaldata->Estimators[i] == "sharedchao") { 
-                                               vennCalculators.push_back(new SharedChao1());
-                                       }else if (globaldata->Estimators[i] == "sharedace") { 
-                                               vennCalculators.push_back(new SharedAce());
+                       splitAtDash(calc, Estimators);
+                       
+                       string temp;
+                       temp = validParameter->validFile(parameters, "abund", false);           if (temp == "not found") { temp = "10"; }
+                       convert(temp, abund); 
+
+                       delete validParameter;
+                       
+                       if (abort == false) {
+                               validCalculator = new ValidCalculators();
+               
+                               int i;
+                               
+                               if (format == "list") {
+                                       for (i=0; i<Estimators.size(); i++) {
+                                               if (validCalculator->isValidCalculator("vennsingle", Estimators[i]) == true) { 
+                                                       if (Estimators[i] == "sobs") { 
+                                                               vennCalculators.push_back(new Sobs());
+                                                       }else if (Estimators[i] == "chao") { 
+                                                               vennCalculators.push_back(new Chao1());
+                                                       }else if (Estimators[i] == "ace") {
+                                                               if(abund < 5)
+                                                                       abund = 10;
+                                                               vennCalculators.push_back(new Ace(abund));
+                                                       }
+                                               }
+                                       }
+                               }else {
+                                       for (i=0; i<Estimators.size(); i++) {
+                                               if (validCalculator->isValidCalculator("vennshared", Estimators[i]) == true) { 
+                                                       if (Estimators[i] == "sharedsobs") { 
+                                                               vennCalculators.push_back(new SharedSobsCS());
+                                                       }else if (Estimators[i] == "sharedchao") { 
+                                                               vennCalculators.push_back(new SharedChao1());
+                                                       }else if (Estimators[i] == "sharedace") { 
+                                                               vennCalculators.push_back(new SharedAce());
+                                                       }
+                                               }
                                        }
                                }
+                               
+                               venn = new Venn();
                        }
                }
-               
-               venn = new Venn();
-               
-               //reset calc for next command
-               globaldata->setCalc("");
 
                
+                               
        }
        catch(exception& e) {
                cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function VennCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
@@ -74,6 +151,35 @@ VennCommand::VennCommand(){
                exit(1);
        }       
 }
+
+//**********************************************************************************************************************
+
+void VennCommand::help(){
+       try {
+               cout << "The venn command can only be executed after a successful read.otu command." << "\n";
+               cout << "The venn command parameters are groups, calc, abund, line and label.  No parameters are required, but you may not use line and label at the same time." << "\n";
+               cout << "The groups parameter allows you to specify which of the groups in your groupfile you would like included in your venn diagram, you may only use a maximum of 4 groups." << "\n";
+               cout << "The group names are separated by dashes. The line and label allow you to select what distance levels you would like a venn diagram created for, and are also separated by dashes." << "\n";
+               cout << "The venn command should be in the following format: venn(groups=yourGroups, calc=yourCalcs, line=yourLines, label=yourLabels, abund=yourAbund)." << "\n";
+               cout << "Example venn(groups=A-B-C, line=1-3-5, calc=sharedsobs-sharedchao, abund=20)." << "\n";
+               cout << "The default value for groups is all the groups in your groupfile up to 4, and all lines in your inputfile will be used." << "\n";
+               cout << "The default value for calc is sobs if you have only read a list file or if you have selected only one group, and sharedsobs if you have multiple groups." << "\n";
+               cout << "The default available estimators for calc are sobs, chao and ace if you have only read a list file, and sharedsobs, sharedchao and sharedace if you have read a list and group file or a shared file." << "\n";
+               cout << "The only estmiator available four 4 groups is sharedsobs." << "\n";
+               cout << "The venn command outputs a .svg file for each calculator you specify at each distance you choose." << "\n";
+               cout << "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups)." << "\n" << "\n";
+       }
+       catch(exception& e) {
+               cout << "Standard Error: " << e.what() << " has occurred in the VennCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }
+       catch(...) {
+               cout << "An unknown error has occurred in the VennCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }       
+}
+
+
 //**********************************************************************************************************************
 
 VennCommand::~VennCommand(){
@@ -87,10 +193,13 @@ VennCommand::~VennCommand(){
 
 int VennCommand::execute(){
        try {
+       
+               if (abort == true) { return 0; }
+               
                int count = 1;
                SAbundVector* lastSAbund;
                vector<SharedRAbundVector*> lastLookup; 
-               
+
                //if the users entered no valid calculators don't execute command
                if (vennCalculators.size() == 0) { return 0; }
                
@@ -114,15 +223,15 @@ int VennCommand::execute(){
                
                //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
                set<string> processedLabels;
-               set<string> userLabels = globaldata->labels;
-               set<int> userLines = globaldata->lines;
+               set<string> userLabels = labels;
+               set<int> userLines = lines;
                
                if (format != "list") { 
                        
                        //as long as you are not at the end of the file or done wih the lines you want
-                       while((lookup[0] != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
-               
-                               if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){                   
+                       while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
+
+                               if(allLines == 1 || lines.count(count) == 1 || labels.count(lookup[0]->getLabel()) == 1){                       
                                        cout << lookup[0]->getLabel() << '\t' << count << endl;
                                        processedLabels.insert(lookup[0]->getLabel());
                                        userLabels.erase(lookup[0]->getLabel());
@@ -186,9 +295,9 @@ int VennCommand::execute(){
                        
                }else{
                
-                       while((sabund != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
+                       while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
                
-                               if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(sabund->getLabel()) == 1){                      
+                               if(allLines == 1 || lines.count(count) == 1 || labels.count(sabund->getLabel()) == 1){                  
        
                                        cout << sabund->getLabel() << '\t' << count << endl;
                                        venn->getPic(sabund, vennCalculators);
@@ -235,8 +344,6 @@ int VennCommand::execute(){
                        delete lastSAbund;
                }
                
-               
-               globaldata->setGroups("");
                return 0;
        }
        catch(exception& e) {