]> git.donarmstrong.com Git - mothur.git/blobdiff - readdistcommand.cpp
broke up globaldata and moved error checking and help into commands
[mothur.git] / readdistcommand.cpp
index 8de0c34ee039e3c79d539fb7ed531fb2351f3400..31d8308414755601166268f6088dd7142f7dfaad 100644 (file)
 #include "readcolumn.h"
 #include "readmatrix.hpp"
 
-ReadDistCommand::ReadDistCommand(){
+ReadDistCommand::ReadDistCommand(string option){
        try {
                globaldata = GlobalData::getInstance();
+               abort = false;
                
-               filename = globaldata->inputFileName;
-               format = globaldata->getFormat();       
+               //allow user to run help
+               if(option == "help") { help(); abort = true; }
                
-               if (format == "column") { read = new ReadColumnMatrix(filename); }      
-               else if (format == "phylip") { read = new ReadPhylipMatrix(filename); }
-               else if (format == "matrix") { 
-                               groupMap = new GroupMap(globaldata->getGroupFile());
-                               groupMap->readMap();
-                               //if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap;  }
-                               globaldata->gGroupmap = groupMap;
-               }
+               else {
+                       //valid paramters for this command
+                       string Array[] =  {"phylip","column", "name","cutoff","precision", "group"};
+                       vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+                       
+                       parser = new OptionParser();
+                       parser->parse(option, parameters);  delete parser;
+                       
+                       ValidParameters* validParameter = new ValidParameters();
                
-               if (format != "matrix" ) {
-                       if(globaldata->getPrecision() != ""){
-                               convert(globaldata->getPrecision(), precision); 
+                       //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;  }
+                       }
+                       
+                       globaldata->newRead();
+                       
+                       //check for required parameters
+                       phylipfile = validParameter->validFile(parameters, "phylip", true);
+                       if (phylipfile == "not open") { abort = true; }
+                       else if (phylipfile == "not found") { phylipfile = ""; }        
+                       else {  globaldata->setPhylipFile(phylipfile);  globaldata->setFormat("phylip");        }
+                       
+                       columnfile = validParameter->validFile(parameters, "column", true);
+                       if (columnfile == "not open") { abort = true; } 
+                       else if (columnfile == "not found") { columnfile = ""; }
+                       else {  globaldata->setColumnFile(columnfile); globaldata->setFormat("column"); }
+                       
+                       groupfile = validParameter->validFile(parameters, "group", true);
+                       if (groupfile == "not open") { abort = true; }  
+                       else if (groupfile == "not found") { groupfile = ""; }
+                       else {  
+                               globaldata->setGroupFile(groupfile); 
+                               groupMap = new GroupMap(groupfile);
+                               groupMap->readMap();
                        }
+                       
+                       namefile = validParameter->validFile(parameters, "name", true);
+                       if (namefile == "not open") { abort = true; }   
+                       else if (namefile == "not found") { namefile = ""; }
+                       else {  globaldata->setNameFile(namefile);      }
+
+                       
+                       //you are doing a list and group shared
+                       if ((phylipfile != "") && (groupfile != "")) { globaldata->setFormat("matrix"); }
+                       
+                       if ((phylipfile == "") && (columnfile == "")) { cout << "When executing a read.dist command you must enter a phylip or a column." << endl; abort = true; }
+                       else if ((phylipfile != "") && (columnfile != "")) { cout << "When executing a read.dist command you must enter ONLY ONE of the following: phylip or column." << endl; abort = true; }
                
-                       if(globaldata->getCutOff() != ""){
-                               convert(globaldata->getCutOff(), cutoff);       
-                               cutoff += (5 / (precision * 10.0));
+                       if (columnfile != "") {
+                               if (namefile == "") {  cout << "You need to provide a namefile if you are going to use the column format." << endl; abort = true; }
                        }
-                       read->setCutoff(cutoff);
+               
+                       //check for optional parameter and set defaults
+                       // ...at some point should added some additional type checking...
+                       //get user cutoff and precision or use defaults
+                       string temp;
+                       temp = validParameter->validFile(parameters, "precision", false);                       if (temp == "not found") { temp = "100"; }
+                       convert(temp, precision); 
+                       
+                       temp = validParameter->validFile(parameters, "cutoff", false);                  if (temp == "not found") { temp = "10"; }
+                       convert(temp, cutoff); 
+                       cutoff += (5 / (precision * 10.0));
+
+                       delete validParameter;
+                       
+                       if (abort == false) {
+                               filename = globaldata->inputFileName;
+                               format = globaldata->getFormat();       
+               
+                               if (format == "column") { read = new ReadColumnMatrix(filename); }      
+                               else if (format == "phylip") { read = new ReadPhylipMatrix(filename); }
+                               else if (format == "matrix") { 
+                                       groupMap = new GroupMap(groupfile);
+                                       groupMap->readMap();
+                                       if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap;  }
+                                       globaldata->gGroupmap = groupMap;
+                               }
+               
+                               if (format != "matrix" ) {
+                                       read->setCutoff(cutoff);
        
-                       if(globaldata->getNameFile() != ""){    
-                               nameMap = new NameAssignment(globaldata->getNameFile());
-                               nameMap->readMap(1,2);
-                       }
-                       else{
-                               nameMap = NULL;
+                                       if(namefile != ""){     
+                                               nameMap = new NameAssignment(namefile);
+                                               nameMap->readMap(1,2);
+                                       }else{
+                                               nameMap = NULL;
+                                       }
+                               }
                        }
+
                }
-                       
+
        }
        catch(exception& e) {
                cout << "Standard Error: " << e.what() << " has occurred in the ReadDistCommand class Function ReadDistCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
@@ -58,6 +123,30 @@ ReadDistCommand::ReadDistCommand(){
                exit(1);
        }
 }
+//**********************************************************************************************************************
+
+void ReadDistCommand::help(){
+       try {
+               cout << "The read.dist command parameter options are phylip or column, group, name, cutoff and precision" << "\n";
+               cout << "The read.dist command can be used in two ways.  The first is to read a phylip or column and run the cluster command" << "\n";
+               cout << "For this use the read.dist command should be in the following format: " << "\n";
+               cout << "read.dist(phylip=yourDistFile, name=yourNameFile, cutoff=yourCutoff, precision=yourPrecision) " << "\n";
+               cout << "The phylip or column parameter is required, but only one may be used.  If you use a column file the name filename is required. " << "\n";
+               cout << "If you do not provide a cutoff value 10.00 is assumed. If you do not provide a precision value then 100 is assumed." << "\n";
+               cout << "The second way to use the read.dist command is to read a phylip or column and a group, so you can use the libshuff command." << "\n";
+               cout << "For this use the read.dist command should be in the following format: " << "\n";
+               cout << "read.dist(phylip=yourPhylipfile, group=yourGroupFile). The cutoff and precision parameters are not valid with this use.  " << "\n";
+               cout << "Note: No spaces between parameter labels (i.e. phylip), '=' and parameters (i.e.yourPhylipfile)." << "\n" << "\n";
+       }
+       catch(exception& e) {
+               cout << "Standard Error: " << e.what() << " has occurred in the ReadDistCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }
+       catch(...) {
+               cout << "An unknown error has occurred in the ReadDistCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }       
+}
 
 //**********************************************************************************************************************
 ReadDistCommand::~ReadDistCommand(){
@@ -69,6 +158,8 @@ ReadDistCommand::~ReadDistCommand(){
 int ReadDistCommand::execute(){
        try {
                
+               if (abort == true) {    return 0;       }
+               
                if (format == "matrix") {
                        ifstream in;
                        openInputFile(filename, in);