]> git.donarmstrong.com Git - mothur.git/blobdiff - treegroupscommand.cpp
added list parameter to get.seqs and remove.seqs and added readline library for inter...
[mothur.git] / treegroupscommand.cpp
index 484b5536ec668fbf60b94cba6ae5244570d2f111..c115c54ce1bcb39786ec7c54fcd4ba11df3115ab 100644 (file)
@@ -27,7 +27,6 @@ TreeGroupCommand::TreeGroupCommand(string option){
                globaldata = GlobalData::getInstance();
                abort = false;
                allLines = 1;
-               lines.clear();
                labels.clear();
                Groups.clear();
                Estimators.clear();
@@ -37,7 +36,7 @@ TreeGroupCommand::TreeGroupCommand(string option){
                
                else {
                        //valid paramters for this command
-                       string Array[] =  {"line","label","calc","groups", "phylip", "column", "name", "precision","cutoff"};
+                       string Array[] =  {"label","calc","groups", "phylip", "column", "name", "precision","cutoff"};
                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                        
                        OptionParser parser(option);
@@ -66,25 +65,18 @@ TreeGroupCommand::TreeGroupCommand(string option){
                        else if (namefile == "not found") { namefile = ""; }
                        else {  globaldata->setNameFile(namefile);      }
                        
-//                     format = globaldata->getFormat();
+                       format = globaldata->getFormat();
                        
                        //error checking on files                       
-                       if ((globaldata->getSharedFile() == "") && ((phylipfile == "") && (columnfile == "")))  { cout << "You must run the read.otu command or provide a distance file before running the tree.shared command." << endl; abort = true; }
-                       else if ((phylipfile != "") && (columnfile != "")) { cout << "When running the tree.shared command with a distance file you may not use both the column and the phylip parameters." << endl; abort = true; }
+                       if ((globaldata->getSharedFile() == "") && ((phylipfile == "") && (columnfile == "")))  { mothurOut("You must run the read.otu command or provide a distance file before running the tree.shared command."); mothurOutEndLine(); abort = true; }
+                       else if ((phylipfile != "") && (columnfile != "")) { mothurOut("When running the tree.shared command with a distance file you may not use both the column and the phylip parameters."); mothurOutEndLine(); abort = true; }
                        
                        if (columnfile != "") {
-                               if (namefile == "") {  cout << "You need to provide a namefile if you are going to use the column format." << endl; abort = true; }
+                               if (namefile == "") {  mothurOut("You need to provide a namefile if you are going to use the column format."); 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);                             
-                       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 { 
@@ -92,13 +84,10 @@ TreeGroupCommand::TreeGroupCommand(string option){
                                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 == "")) {  
+                       //if the user has not specified any labels use the ones from read.otu
+                       if(label == "") {  
                                allLines = globaldata->allLines; 
                                labels = globaldata->labels; 
-                               lines = globaldata->lines;
                        }
                                
                        groups = validParameter.validFile(parameters, "groups", false);                 
@@ -161,55 +150,49 @@ TreeGroupCommand::TreeGroupCommand(string option){
 
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function TreeGroupCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "TreeGroupCommand", "TreeGroupCommand");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the TreeGroupCommand class function TreeGroupCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 }
 
 //**********************************************************************************************************************
 
 void TreeGroupCommand::help(){
        try {
-               cout << "The tree.shared command creates a .tre to represent the similiarity between groups or sequences." << "\n";
-               cout << "The tree.shared command can only be executed after a successful read.otu command or by providing a distance file." << "\n";
-               cout << "The tree.shared command parameters are groups, calc, phylip, column, name, cutoff, precision, line and label.  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 used." << "\n";
-               cout << "The group names are separated by dashes. The line and label allow you to select what distance levels you would like trees created for, and are also separated by dashes." << "\n";
-               cout << "The phylip or column parameter are required if you do not run the read.otu command first, and 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 tree.shared command should be in the following format: tree.shared(groups=yourGroups, calc=yourCalcs, line=yourLines, label=yourLabels)." << "\n";
-               cout << "Example tree.shared(groups=A-B-C, line=1-3-5, calc=jabund-sorabund)." << "\n";
-               cout << "The default value for groups is all the groups in your groupfile." << "\n";
-               cout << "The default value for calc is jclass-thetayc." << "\n";
-               cout << "The tree.shared command outputs a .tre file for each calculator you specify at each distance you choose." << "\n";
+               mothurOut("The tree.shared command creates a .tre to represent the similiarity between groups or sequences.\n");
+               mothurOut("The tree.shared command can only be executed after a successful read.otu command or by providing a distance file.\n");
+               mothurOut("The tree.shared command parameters are groups, calc, phylip, column, name, cutoff, precision and label.\n");
+               mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included used.\n");
+               mothurOut("The group names are separated by dashes. The label allow you to select what distance levels you would like trees created for, and are also separated by dashes.\n");
+               mothurOut("The phylip or column parameter are required if you do not run the read.otu command first, and only one may be used.  If you use a column file the name filename is required. \n");
+               mothurOut("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");
+               mothurOut("The tree.shared command should be in the following format: tree.shared(groups=yourGroups, calc=yourCalcs, label=yourLabels).\n");
+               mothurOut("Example tree.shared(groups=A-B-C, calc=jabund-sorabund).\n");
+               mothurOut("The default value for groups is all the groups in your groupfile.\n");
+               mothurOut("The default value for calc is jclass-thetayc.\n");
+               mothurOut("The tree.shared command outputs a .tre file for each calculator you specify at each distance you choose.\n");
                validCalculator->printCalc("treegroup", cout);
-               cout << "Or the tree.shared command can be in the following format: tree.shared(phylip=yourPhylipFile)." << "\n";
-               cout << "Example tree.shared(phylip=abrecovery.dist)." << "\n";
-               cout << "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups)." << "\n" << "\n";
+               mothurOut("Or the tree.shared command can be in the following format: tree.shared(phylip=yourPhylipFile).\n");
+               mothurOut("Example tree.shared(phylip=abrecovery.dist).\n");
+               mothurOut("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 TreeGroupCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "TreeGroupCommand", "help");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the TreeGroupCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 }
 
 
 //**********************************************************************************************************************
 
 TreeGroupCommand::~TreeGroupCommand(){
-       delete input;
-       if (format == "sharedfile") {delete read;}
-       else { delete readMatrix;  delete matrix; delete list; }
-       delete tmap;
-       delete validCalculator;
+       if (abort == false) {
+               
+               if (format == "sharedfile") { delete read;  delete input; globaldata->ginput = NULL; }
+               else { delete readMatrix;  delete matrix; delete list; }
+               delete tmap;  globaldata->gTreemap = NULL;
+               delete validCalculator;
+       }
        
 }
 
@@ -222,7 +205,7 @@ int TreeGroupCommand::execute(){
                
                if (format == "sharedfile") {
                        //if the users entered no valid calculators don't execute command
-                       if (treeCalculators.size() == 0) { cout << "You have given no valid calculators." << endl; return 0; }
+                       if (treeCalculators.size() == 0) { mothurOut("You have given no valid calculators."); mothurOutEndLine(); return 0; }
 
                        //you have groups
                        read = new ReadOTUFile(globaldata->inputFileName);      
@@ -230,10 +213,13 @@ int TreeGroupCommand::execute(){
                        
                        input = globaldata->ginput;
                        lookup = input->getSharedRAbundVectors();
-                       lastLookup = lookup;
+                       lastLabel = lookup[0]->getLabel();
+                       
+                       if (lookup.size() < 2) { mothurOut("You have not provided enough valid groups.  I cannot run the command."); mothurOutEndLine(); return 0; }
+                       
+                       //used in tree constructor 
+                       globaldata->runParse = false;
                        
-                       if (lookup.size() < 2) { cout << "You have not provided enough valid groups.  I cannot run the command." << endl; return 0; }
-               
                        //create tree file
                        makeSimsShared();
                }else{
@@ -247,7 +233,7 @@ int TreeGroupCommand::execute(){
        
                        if(namefile != ""){     
                                nameMap = new NameAssignment(namefile);
-                               nameMap->readMap(1,2);
+                               nameMap->readMap();
                        }
                        else{
                                nameMap = NULL;
@@ -269,14 +255,17 @@ int TreeGroupCommand::execute(){
                
                        //fills globaldatas tree names
                        globaldata->Treenames = globaldata->Groups;
-
+                       
+                       //used in tree constructor 
+                       globaldata->runParse = false;
+                       
                        makeSimsDist();
 
                        //create a new filename
                        outputFile = getRootName(globaldata->inputFileName) + "tre";    
                                
                        createTree();
-                       cout << "Tree complete. " << endl;
+                       mothurOut("Tree complete. "); mothurOutEndLine();
                }
                                
                //reset groups parameter
@@ -285,13 +274,9 @@ int TreeGroupCommand::execute(){
                return 0;
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "TreeGroupCommand", "execute");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the TreeGroupCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }               
 }
 //**********************************************************************************************************************
 
@@ -366,11 +351,7 @@ void TreeGroupCommand::createTree(){
        
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-       catch(...) {
-               cout << "An unknown error has occurred in the TreeGroupCommand class function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "TreeGroupCommand", "createTree");
                exit(1);
        }
 }
@@ -394,13 +375,9 @@ void TreeGroupCommand::printSims(ostream& out) {
 
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "TreeGroupCommand", "printSims");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the TreeGroupCommand class function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }               
 }
 /***********************************************************/
 void TreeGroupCommand::makeSimsDist() {
@@ -431,19 +408,14 @@ void TreeGroupCommand::makeSimsDist() {
 
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function makeSimsDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "TreeGroupCommand", "makeSimsDist");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the TreeGroupCommand class function makeSimsDist. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }               
 }
 
 /***********************************************************/
 void TreeGroupCommand::makeSimsShared() {
        try {
-               int count = 1;  
        
                //clear globaldatas old tree names if any
                globaldata->Treenames.clear();
@@ -458,67 +430,70 @@ void TreeGroupCommand::makeSimsShared() {
                
                set<string> processedLabels;
                set<string> userLabels = labels;
-               set<int> userLines = lines;
-
+               
                //as long as you are not at the end of the file or done wih the lines you want
-               while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
+               while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
                
-                       if(allLines == 1 || lines.count(count) == 1 || labels.count(lookup[0]->getLabel()) == 1){                       
-                               cout << lookup[0]->getLabel() << '\t' << count << endl;
+                       if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
+                               mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
                                process(lookup);
                                
                                processedLabels.insert(lookup[0]->getLabel());
                                userLabels.erase(lookup[0]->getLabel());
-                               userLines.erase(count);
                        }
                        
-                       if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
-                               cout << lastLookup[0]->getLabel() << '\t' << count << endl;
-                               process(lastLookup);
+                       if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
+                               string saveLabel = lookup[0]->getLabel();
+                       
+                               for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
+                               lookup = input->getSharedRAbundVectors(lastLabel);
+
+                               mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
+                               process(lookup);
                                        
-                               processedLabels.insert(lastLookup[0]->getLabel());
-                               userLabels.erase(lastLookup[0]->getLabel());
+                               processedLabels.insert(lookup[0]->getLabel());
+                               userLabels.erase(lookup[0]->getLabel());
+                               
+                               //restore real lastlabel to save below
+                               lookup[0]->setLabel(saveLabel);
                        }
 
-                       //prevent memory leak
-                       if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  } }
-                       lastLookup = lookup;                    
+                       lastLabel = lookup[0]->getLabel();                      
                        
                        //get next line to process
+                       for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
                        lookup = input->getSharedRAbundVectors();
-                       count++;
                }
                
                //output error messages about any remaining user labels
                set<string>::iterator it;
                bool needToRun = false;
                for (it = userLabels.begin(); it != userLabels.end(); it++) {  
-                       cout << "Your file does not include the label "<< *it
-                       if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
-                               cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
+                       mothurOut("Your file does not include the label " + *it)
+                       if (processedLabels.count(lastLabel) != 1) {
+                               mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
                                needToRun = true;
                        }else {
-                               cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
+                               mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
                        }
                }
                
-               //run last line if you need to
+               //run last label if you need to
                if (needToRun == true)  {
-                       cout << lastLookup[0]->getLabel() << '\t' << count << endl;
-                       process(lastLookup);
+                       for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {             delete lookup[i]; }             } 
+                       lookup = input->getSharedRAbundVectors(lastLabel);
+
+                       mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
+                       process(lookup);
+                       for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }         
                }
                
-               for (int i = 0; i < lastLookup.size(); i++) {  delete lastLookup[i];  }
                for(int i = 0 ; i < treeCalculators.size(); i++) {  delete treeCalculators[i]; }
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function makeSimsShared. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "TreeGroupCommand", "makeSimsShared");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the TreeGroupCommand class function makeSimsShared. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }               
 }
 
 /***********************************************************/
@@ -562,20 +537,16 @@ void TreeGroupCommand::process(vector<SharedRAbundVector*> thisLookup) {
                                                        }
                                                }
                                        }
-                                       
+                               
                                        //creates tree from similarity matrix and write out file
                                        createTree();
                                }
 
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "TreeGroupCommand", "process");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the TreeGroupCommand class function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }               
 }
 /***********************************************************/