]> git.donarmstrong.com Git - mothur.git/blobdiff - readotucommand.cpp
adding more error checking for list and group files. outputs missing.names or missin...
[mothur.git] / readotucommand.cpp
index 1f1ad971738596854650363ef01672aa19e0748b..999d8de95ecdda62b411e3d5fab11092d3105636 100644 (file)
@@ -24,46 +24,47 @@ ReadOtuCommand::ReadOtuCommand(string option){
                        string Array[] =  {"list","order","shared", "line", "label","group","sabund", "rabund"};
                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                        
-                       parser = new OptionParser();
-                       parser->parse(option, parameters);  delete parser;
+                       OptionParser parser(option);
+                       map<string, string> parameters = parser.getParameters();
                        
-                       ValidParameters* validParameter = new ValidParameters();
+                       ValidParameters validParameter;
                
                        //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;  }
+                       for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
+                               if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
                        globaldata->newRead();
                        
                        //check for required parameters
-                       listfile = validParameter->validFile(parameters, "list", true);
+                       listfile = validParameter.validFile(parameters, "list", true);
                        if (listfile == "not open") { abort = true; }
                        else if (listfile == "not found") { listfile = ""; }    
                        else {  globaldata->setListFile(listfile);  globaldata->setFormat("list");      }
                        
-                       sabundfile = validParameter->validFile(parameters, "sabund", true);
+                       sabundfile = validParameter.validFile(parameters, "sabund", true);
                        if (sabundfile == "not open") { abort = true; } 
                        else if (sabundfile == "not found") { sabundfile = ""; }
                        else {  globaldata->setSabundFile(sabundfile); globaldata->setFormat("sabund"); }
 
-                       rabundfile = validParameter->validFile(parameters, "rabund", true);
+                       rabundfile = validParameter.validFile(parameters, "rabund", true);
                        if (rabundfile == "not open") { abort = true; } 
                        else if (rabundfile == "not found") { rabundfile = ""; }
                        else {  globaldata->setRabundFile(rabundfile);  globaldata->setFormat("rabund");}
                        
-                       sharedfile = validParameter->validFile(parameters, "shared", true);
+                       sharedfile = validParameter.validFile(parameters, "shared", true);
                        if (sharedfile == "not open") { abort = true; } 
                        else if (sharedfile == "not found") { sharedfile = ""; }
                        else {  globaldata->setSharedFile(sharedfile); globaldata->setFormat("sharedfile");     }
                        
-                       groupfile = validParameter->validFile(parameters, "group", true);
+                       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();
+                               globaldata->gGroupmap = groupMap;
                        }
 
                        //you are doing a list and group shared
@@ -71,12 +72,12 @@ ReadOtuCommand::ReadOtuCommand(string option){
                        
                        //you have not given a file
                        if ((listfile == "") && (sharedfile == "") && (rabundfile == "") && (sabundfile == "")) {
-                               cout << "You must enter either a listfile, rabundfile, sabundfile or a sharedfile with the read.otu command. " << endl; abort = true; 
+                               mothurOut("You must enter either a listfile, rabundfile, sabundfile or a sharedfile with the read.otu command. "); mothurOutEndLine(); abort = true; 
                        }
                
                        //check for optional parameter and set defaults
                        // ...at some point should added some additional type checking...
-                       line = validParameter->validFile(parameters, "line", false);                            
+                       line = validParameter.validFile(parameters, "line", false);                             
                        if (line == "not found") { line = ""; }
                        else { 
                                if(line != "all") {  splitAtDash(line, lines);  allLines = 0;  }
@@ -84,7 +85,7 @@ ReadOtuCommand::ReadOtuCommand(string option){
                                globaldata->lines = lines;
                        }
                        
-                       label = validParameter->validFile(parameters, "label", false);                  
+                       label = validParameter.validFile(parameters, "label", false);                   
                        if (label == "not found") { label = ""; }
                        else { 
                                if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
@@ -95,30 +96,24 @@ ReadOtuCommand::ReadOtuCommand(string option){
                        globaldata->allLines = allLines;
                        
                        //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 ((line != "") && (label != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); abort = true; }
                        
-                       orderfile = validParameter->validFile(parameters, "order", true);
+                       orderfile = validParameter.validFile(parameters, "order", true);
                        if (orderfile == "not open") { abort = true; }  
                        else if (orderfile == "not found") { orderfile = ""; }
                        else {  globaldata->setOrderFile(orderfile);    }
                        
-                       delete validParameter;
                        
                        if (abort == false) {
                                //gets whichever one of the above is set
                                filename = globaldata->inputFileName;
-                               read = new ReadOTUFile(filename);
                        }
 
                }
 
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the ReadOtuCommand class Function ReadOtuCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-       catch(...) {
-               cout << "An unknown error has occurred in the ReadOtuCommand class function ReadOtuCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "ReadOtuCommand", "ReadOtuCommand");
                exit(1);
        }
 }
@@ -126,29 +121,25 @@ ReadOtuCommand::ReadOtuCommand(string option){
 
 void ReadOtuCommand::help(){
        try {
-               cout << "The read.otu command must be run before you execute a collect.single, rarefaction.single, summary.single, " << "\n";
-               cout << "collect.shared, rarefaction.shared or summary.shared command.   Mothur will generate a .list, .rabund and .sabund upon completion of the cluster command " << "\n";
-               cout << "or you may use your own. The read.otu command parameter options are list, rabund, sabund, shared, group, order, line and label." << "\n";
-               cout << "The read.otu command can be used in two ways.  The first is to read a list, rabund or sabund and run the collect.single, rarefaction.single or summary.single." << "\n";
-               cout << "For this use the read.otu command should be in the following format: read.otu(list=yourListFile, order=yourOrderFile, label=yourLabels)." << "\n";
-               cout << "The list, rabund or sabund parameter is required, but you may only use one of them." << "\n";
-               cout << "The line and label parameters are optional but you may not use both the line and label parameters at the same time." << "\n";
-               cout << "The label and line parameters are used to read specific lines in your input." << "\n";
-               cout << "The second way to use the read.otu command is to read a list and a group, or a shared so you can use the collect.shared, rarefaction.shared or summary.shared commands." << "\n";
-               cout << "In this case the read.otu command should be in the following format: read.otu(list=yourListFile, group=yourGroupFile, line=yourLines) or read.otu(shared=yourSharedFile).  " << "\n";
-               cout << "The list parameter and group paramaters or the shared paremeter is required. When using the command the second way with a list and group file read.otu command parses the .list file" << "\n";
-               cout << "and separates it into groups.  It outputs a .shared file containing the OTU information for each group. The read.otu command also outputs a .list file for each group. " << "\n";
-               cout << "Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile)." << "\n" << "\n";
+               mothurOut("The read.otu command must be run before you execute a collect.single, rarefaction.single, summary.single, \n");
+               mothurOut("collect.shared, rarefaction.shared or summary.shared command.   Mothur will generate a .list, .rabund and .sabund upon completion of the cluster command \n");
+               mothurOut("or you may use your own. The read.otu command parameter options are list, rabund, sabund, shared, group, order, line and label.\n");
+               mothurOut("The read.otu command can be used in two ways.  The first is to read a list, rabund or sabund and run the collect.single, rarefaction.single or summary.single.\n");
+               mothurOut("For this use the read.otu command should be in the following format: read.otu(list=yourListFile, order=yourOrderFile, label=yourLabels).\n");
+               mothurOut("The list, rabund or sabund parameter is required, but you may only use one of them.\n");
+               mothurOut("The line and label parameters are optional but you may not use both the line and label parameters at the same time.\n");
+               mothurOut("The label and line parameters are used to read specific lines in your input.\n");
+               mothurOut("The second way to use the read.otu command is to read a list and a group, or a shared so you can use the collect.shared, rarefaction.shared or summary.shared commands.\n");
+               mothurOut("In this case the read.otu command should be in the following format: read.otu(list=yourListFile, group=yourGroupFile, line=yourLines) or read.otu(shared=yourSharedFile).  \n");
+               mothurOut("The list parameter and group paramaters or the shared paremeter is required. When using the command the second way with a list and group file read.otu command parses the .list file\n");
+               mothurOut("and separates it into groups.  It outputs a .shared file containing the OTU information for each group. The read.otu command also outputs a .list file for each group. \n");
+               mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile).\n\n");
 
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the ReadOtuCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "ReadOtuCommand", "help");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the ReadOtuCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 }
 
 
@@ -156,8 +147,7 @@ void ReadOtuCommand::help(){
 //**********************************************************************************************************************
 
 ReadOtuCommand::~ReadOtuCommand(){
-       if (abort == false) {  delete read;  }
-}
+       }
 
 //**********************************************************************************************************************
 
@@ -166,32 +156,30 @@ int ReadOtuCommand::execute(){
        
                if (abort == true) {    return 0;       }
                
-               read->read(&*globaldata); 
                if (globaldata->getFormat() == "shared") {
-                       groupMap->readMap();
                        
-                       //if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap;  }
-                       globaldata->gGroupmap = groupMap;               
                        shared = new SharedCommand();
-                       shared->execute();
-
-                       parselist = new ParseListCommand();
-                       parselist->execute();
-                       
-                       //change format to shared  to speed up commands
-                       globaldata->setFormat("sharedfile");
-                       globaldata->setListFile("");
-                       globaldata->setGroupFile("");
-                       globaldata->setSharedFile(getRootName(filename) + "shared");
+                       int okay = shared->execute();
+                       delete shared;
+                       
+                       //problem with shared
+                       if (okay == 1) {
+                               globaldata->setListFile("");
+                               globaldata->setGroupFile("");
+                               globaldata->setSharedFile("");
+                       }else{
+                               
+                               //change format to shared  to speed up commands
+                               globaldata->setFormat("sharedfile");
+                               globaldata->setListFile("");
+                               globaldata->setGroupFile("");
+                               globaldata->setSharedFile(getRootName(filename) + "shared");
+                       }
                }
                return 0;
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the ReadOtuCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-       catch(...) {
-               cout << "An unknown error has occurred in the ReadOtuCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "ReadOtuCommand", "execute");
                exit(1);
        }
 }