]> git.donarmstrong.com Git - mothur.git/blobdiff - libshuffcommand.cpp
working on pam
[mothur.git] / libshuffcommand.cpp
index 773d8d351211f3ab9794770e72aad77235754b53..46f1a53a475767d379059ca431f0d01e8596afa8 100644 (file)
 #include "slibshuff.h"
 #include "dlibshuff.h"
 
+
+
+//**********************************************************************************************************************
+vector<string> LibShuffCommand::setParameters(){       
+       try {
+               CommandParameter pphylip("phylip", "InputTypes", "", "", "none", "none", "none","coverage-libshuffsummary",false,true,true); parameters.push_back(pphylip);
+               CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none","",false,true,true); parameters.push_back(pgroup);
+               CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
+               CommandParameter piters("iters", "Number", "", "10000", "", "", "","",false,false); parameters.push_back(piters);
+               CommandParameter pstep("step", "Number", "", "0.01", "", "", "","",false,false); parameters.push_back(pstep);
+               CommandParameter pcutoff("cutoff", "Number", "", "1.0", "", "", "","",false,false); parameters.push_back(pcutoff);
+               CommandParameter pform("form", "Multiple", "discrete-integral", "integral", "", "", "","",false,false); parameters.push_back(pform);
+               CommandParameter psim("sim", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(psim);
+               CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
+               CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
+               
+               vector<string> myArray;
+               for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "LibShuffCommand", "setParameters");
+               exit(1);
+       }
+}
 //**********************************************************************************************************************
+string LibShuffCommand::getHelpString(){       
+       try {
+               string helpString = "";
+               helpString += "The libshuff command parameters are phylip, group, sim, groups, iters, step, form and cutoff.  phylip and group parameters are required, unless you have valid current files.\n";
+               helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed.  You must enter at least 2 valid groups.\n";
+               helpString += "The group names are separated by dashes.  The iters parameter allows you to specify how many random matrices you would like compared to your matrix.\n";
+               helpString += "The step parameter allows you to specify change in distance you would like between each output if you are using the discrete form.\n";
+               helpString += "The form parameter allows you to specify if you would like to analyze your matrix using the discrete or integral form. Your options are integral or discrete.\n";
+               helpString += "The libshuff command should be in the following format: libshuff(groups=yourGroups, iters=yourIters, cutOff=yourCutOff, form=yourForm, step=yourStep).\n";
+               helpString += "Example libshuff(groups=A-B-C, iters=500, form=discrete, step=0.01, cutOff=2.0).\n";
+               helpString += "The default value for groups is all the groups in your groupfile, iters is 10000, cutoff is 1.0, form is integral and step is 0.01.\n";
+               helpString += "The libshuff command output two files: .coverage and .slsummary their descriptions are in the manual.\n";
+               helpString += "Note: No spaces between parameter labels (i.e. iters), '=' and parameters (i.e.yourIters).\n";
+               return helpString;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "LibShuffCommand", "getHelpString");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+string LibShuffCommand::getOutputPattern(string type) {
+    try {
+        string pattern = "";
+        
+        if (type == "coverage") {  pattern = "[filename],libshuff.coverage"; } 
+        else if (type == "libshuffsummary") {  pattern = "[filename],libshuff.summary"; } 
+        else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
+        
+        return pattern;
+    }
+    catch(exception& e) {
+        m->errorOut(e, "LibShuffCommand", "getOutputPattern");
+        exit(1);
+    }
+}
+//**********************************************************************************************************************
+LibShuffCommand::LibShuffCommand(){    
+       try {
+               abort = true; calledHelp = true; 
+               setParameters();
+               vector<string> tempOutNames;
+               outputTypes["coverage"] = tempOutNames;
+               outputTypes["libshuffsummary"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "LibShuffCommand", "LibShuffCommand");
+               exit(1);
+       }
+}
 
+//**********************************************************************************************************************
 LibShuffCommand::LibShuffCommand(string option)  {
        try {
-               globaldata = GlobalData::getInstance();
-               abort = false;
-               Groups.clear();
+               abort = false; calledHelp = false;   
                
                //allow user to run help
-               if(option == "help") { help(); abort = true; }
+               if(option == "help") { help(); abort = true; calledHelp = true; }
+               else if(option == "citation") { citation(); abort = true; calledHelp = true;}
                
                else {
-                       //valid paramters for this command
-                       string Array[] =  {"iters","groups","step","form","cutoff","outputdir","inputdir"};
-                       vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+                       vector<string> myArray = setParameters();
                        
                        OptionParser parser(option);
                        map<string, string> parameters = parser.getParameters();
+                       map<string,string>::iterator it;
                        
                        ValidParameters validParameter;
                
                        //check to make sure all parameters are valid for command
-                       for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
+                       for (it = parameters.begin(); it != parameters.end(); it++) { 
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["coverage"] = tempOutNames;
+                       outputTypes["libshuffsummary"] = tempOutNames;
+                       
+                       string inputDir = validParameter.validFile(parameters, "inputdir", false);              
+                       if (inputDir == "not found"){   inputDir = "";          }
+                       else {
+                               string path;
+                               it = parameters.find("phylip");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = m->hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
+                               }
+                               
+                               it = parameters.find("group");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = m->hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["group"] = inputDir + it->second;            }
+                               }
+                       }
+                       
+                       //check for required parameters
+                       phylipfile = validParameter.validFile(parameters, "phylip", true);
+                       if (phylipfile == "not open") { phylipfile = ""; abort = true; }
+                       else if (phylipfile == "not found") { 
+                               phylipfile = m->getPhylipFile(); 
+                               if (phylipfile != "") {  m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
+                               else { 
+                                       m->mothurOut("You must provide a phylip file."); m->mothurOutEndLine(); 
+                                       abort = true;
+                               } 
+                       }else { m->setPhylipFile(phylipfile); } 
+                       
+                       //check for required parameters
+                       groupfile = validParameter.validFile(parameters, "group", true);
+                       if (groupfile == "not open") { groupfile = ""; abort = true; }
+                       else if (groupfile == "not found") { 
+                               groupfile = m->getGroupFile(); 
+                               if (groupfile != "") {  m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
+                               else { 
+                                       m->mothurOut("You must provide a group file."); m->mothurOutEndLine(); 
+                                       abort = true;
+                               } 
+                       }else { m->setGroupFile(groupfile); }   
+                       
                        //if the user changes the output directory command factory will send this info to us in the output parameter 
                        outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
                                outputDir = ""; 
-                               outputDir += m->hasPath(globaldata->getPhylipFile()); //if user entered a file with a path then preserve it     
-                       }
-                       
-                       //make sure the user has already run the read.dist command
-                       if ((globaldata->gMatrix == NULL) || (globaldata->gGroupmap == NULL)) {
-                               m->mothurOut("You must read in a matrix and groupfile using the read.dist command, before you use the libshuff command. "); m->mothurOutEndLine(); abort = true;; 
+                               outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it      
                        }
                                                
                        //check for optional parameter and set defaults
@@ -62,43 +181,23 @@ LibShuffCommand::LibShuffCommand(string option)  {
                        else { 
                                savegroups = groups;
                                m->splitAtDash(groups, Groups);
-                               globaldata->Groups = Groups;
+                               m->setGroups(Groups);
                        }
                                
                        string temp;
                        temp = validParameter.validFile(parameters, "iters", false);                            if (temp == "not found") { temp = "10000"; }
-                       convert(temp, iters); 
+                       m->mothurConvert(temp, iters); 
                        
                        temp = validParameter.validFile(parameters, "cutoff", false);                           if (temp == "not found") { temp = "1.0"; }
-                       convert(temp, cutOff); 
+                       m->mothurConvert(temp, cutOff); 
                        
                        temp = validParameter.validFile(parameters, "step", false);                             if (temp == "not found") { temp = "0.01"; }
-                       convert(temp, step); 
-       
-                       userform = validParameter.validFile(parameters, "form", false);                 if (userform == "not found") { userform = "integral"; }
+                       m->mothurConvert(temp, step); 
                        
-                       if (abort == false) {
-               
-                               matrix = globaldata->gMatrix;                           //get the distance matrix
-                               setGroups();                                                            //set the groups to be analyzed and sorts them
-       
-                               /********************************************************************************************/
-                               //this is needed because when we read the matrix we sort it into groups in alphabetical order
-                               //the rest of the command and the classes used in this command assume specific order
-                               /********************************************************************************************/
-                               matrix->setGroups(globaldata->gGroupmap->namesOfGroups);
-                               vector<int> sizes;
-                               for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {   sizes.push_back(globaldata->gGroupmap->getNumSeqs(globaldata->gGroupmap->namesOfGroups[i]));  }
-                               matrix->setSizes(sizes);
+                       temp = validParameter.validFile(parameters, "sim", false);                              if (temp == "not found") { temp = "F"; }
+                       sim = m->isTrue(temp); 
                        
-
-                               if(userform == "discrete"){
-                                       form = new DLibshuff(matrix, iters, step, cutOff);
-                               }
-                               else{
-                                       form = new SLibshuff(matrix, iters, cutOff);
-                               }
-                       }
+                       userform = validParameter.validFile(parameters, "form", false);                 if (userform == "not found") { userform = "integral"; }
                        
                }
                
@@ -110,65 +209,110 @@ LibShuffCommand::LibShuffCommand(string option)  {
 }
 //**********************************************************************************************************************
 
-void LibShuffCommand::help(){
-       try {
-               m->mothurOut("The libshuff command can only be executed after a successful read.dist command including a groupfile.\n");
-               m->mothurOut("The libshuff command parameters are groups, iters, step, form and cutoff.  No parameters are required.\n");
-               m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed.  You must enter at least 2 valid groups.\n");
-               m->mothurOut("The group names are separated by dashes.  The iters parameter allows you to specify how many random matrices you would like compared to your matrix.\n");
-               m->mothurOut("The step parameter allows you to specify change in distance you would like between each output if you are using the discrete form.\n");
-               m->mothurOut("The form parameter allows you to specify if you would like to analyze your matrix using the discrete or integral form. Your options are integral or discrete.\n");
-               m->mothurOut("The libshuff command should be in the following format: libshuff(groups=yourGroups, iters=yourIters, cutOff=yourCutOff, form=yourForm, step=yourStep).\n");
-               m->mothurOut("Example libshuff(groups=A-B-C, iters=500, form=discrete, step=0.01, cutOff=2.0).\n");
-               m->mothurOut("The default value for groups is all the groups in your groupfile, iters is 10000, cutoff is 1.0, form is integral and step is 0.01.\n");
-               m->mothurOut("The libshuff command output two files: .coverage and .slsummary their descriptions are in the manual.\n");
-               m->mothurOut("Note: No spaces between parameter labels (i.e. iters), '=' and parameters (i.e.yourIters).\n\n");
-       }
-       catch(exception& e) {
-               m->errorOut(e, "LibShuffCommand", "help");
-               exit(1);
-       }
-}
-
-//**********************************************************************************************************************
-
 int LibShuffCommand::execute(){
        try {
                
-               if (abort == true) {    return 0;       }
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
+               
+               //read files
+               groupMap = new GroupMap(groupfile);
+               int error = groupMap->readMap();
+               if (error == 1) { delete groupMap; return 0; }
+               
+               ifstream in;
+               m->openInputFile(phylipfile, in);
+               matrix = new FullMatrix(in, groupMap, sim); //reads the matrix file
+               in.close();
+               
+               if (m->control_pressed) { delete groupMap; delete matrix; return 0; }
+               
+               //if files don't match...
+               if (matrix->getNumSeqs() < groupMap->getNumSeqs()) {  
+                       m->mothurOut("Your distance file contains " + toString(matrix->getNumSeqs()) + " sequences, and your group file contains " + toString(groupMap->getNumSeqs()) + " sequences.");  m->mothurOutEndLine();                         
+                       //create new group file
+                       if(outputDir == "") { outputDir += m->hasPath(groupfile); }
+                       
+                       string newGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "editted.groups";
+                       outputNames.push_back(newGroupFile);
+                       ofstream outGroups;
+                       m->openOutputFile(newGroupFile, outGroups);
+                       
+                       for (int i = 0; i < matrix->getNumSeqs(); i++) {
+                               if (m->control_pressed) { delete groupMap; delete matrix; outGroups.close(); m->mothurRemove(newGroupFile); return 0; }
+                               
+                               Names temp = matrix->getRowInfo(i);
+                               outGroups << temp.seqName << '\t' << temp.groupName << endl;
+                       }
+                       outGroups.close();
+                       
+                       m->mothurOut(newGroupFile + " is a new group file containing only the sequence that are in your distance file. I will read this file instead."); m->mothurOutEndLine();
+                       
+                       //read new groupfile
+                       delete groupMap; 
+                       groupfile = newGroupFile;
+                       
+                       groupMap = new GroupMap(groupfile);
+                       groupMap->readMap();
+                       
+                       if (m->control_pressed) { delete groupMap; delete matrix; m->mothurRemove(newGroupFile); return 0; }
+               }
+               
+                       
+               setGroups();                                                            //set the groups to be analyzed and sorts them
+               
+               if (numGroups < 2) { m->mothurOut("[ERROR]: libshuff requires at least 2 groups, you only have " + toString(numGroups) + ", aborting."); m->mothurOutEndLine(); m->control_pressed = true; }
+               
+               if (m->control_pressed) { delete groupMap; delete matrix; return 0; }
+               
+               /********************************************************************************************/
+               //this is needed because when we read the matrix we sort it into groups in alphabetical order
+               //the rest of the command and the classes used in this command assume specific order
+               /********************************************************************************************/
+               matrix->setGroups(groupMap->getNamesOfGroups());
+               vector<int> sizes;
+               for (int i = 0; i < (groupMap->getNamesOfGroups()).size(); i++) {   sizes.push_back(groupMap->getNumSeqs((groupMap->getNamesOfGroups())[i]));  }
+               matrix->setSizes(sizes);
+                       
+                       
+               if(userform == "discrete"){
+                       form = new DLibshuff(matrix, iters, step, cutOff);
+               }
+               else{
+                       form = new SLibshuff(matrix, iters, cutOff);
+               }
        
                savedDXYValues = form->evaluateAll();
                savedMinValues = form->getSavedMins();
                
-               if (m->control_pressed) {  delete form; globaldata->Groups.clear(); delete globaldata->gMatrix;  globaldata->gMatrix = NULL; return 0; }
+               if (m->control_pressed) {  delete form; m->clearGroups(); delete matrix; delete groupMap; return 0; }
        
                pValueCounts.resize(numGroups);
                for(int i=0;i<numGroups;i++){
                        pValueCounts[i].assign(numGroups, 0);
                }
        
-               if (m->control_pressed) {  delete form; globaldata->Groups.clear(); delete globaldata->gMatrix;  globaldata->gMatrix = NULL; return 0; }
+               if (m->control_pressed) {  outputTypes.clear(); delete form; m->clearGroups(); delete matrix; delete groupMap; return 0; }
                                
                Progress* reading = new Progress();
                
                for(int i=0;i<numGroups-1;i++) {
                        for(int j=i+1;j<numGroups;j++) {
                                
-                               if (m->control_pressed) {  delete form; globaldata->Groups.clear(); delete globaldata->gMatrix;  globaldata->gMatrix = NULL; delete reading; return 0; }
+                               if (m->control_pressed) {  outputTypes.clear();  delete form; m->clearGroups(); delete matrix; delete groupMap; delete reading; return 0; }
 
                                reading->newLine(groupNames[i]+'-'+groupNames[j], iters);
-                               int spoti = globaldata->gGroupmap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
-                               int spotj = globaldata->gGroupmap->groupIndex[groupNames[j]];
+                               int spoti = groupMap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
+                               int spotj = groupMap->groupIndex[groupNames[j]];
        
                                for(int p=0;p<iters;p++) {      
                                        
-                                       if (m->control_pressed) {  delete form; globaldata->Groups.clear(); delete globaldata->gMatrix;  globaldata->gMatrix = NULL; delete reading; return 0; }
+                                       if (m->control_pressed) {  outputTypes.clear(); delete form; m->clearGroups(); delete matrix; delete groupMap; delete reading; return 0; }
                                        
                                        form->randomizeGroups(spoti,spotj); 
                                        if(form->evaluatePair(spoti,spotj) >= savedDXYValues[spoti][spotj])     {       pValueCounts[i][j]++;   }
                                        if(form->evaluatePair(spotj,spoti) >= savedDXYValues[spotj][spoti])     {       pValueCounts[j][i]++;   }
                                        
-                                       if (m->control_pressed) {  delete form; globaldata->Groups.clear(); delete globaldata->gMatrix;  globaldata->gMatrix = NULL; delete reading; return 0; }
+                                       if (m->control_pressed) {  outputTypes.clear(); delete form; m->clearGroups(); delete matrix; delete groupMap; delete reading; return 0; }
                                        
                                        reading->update(p);                     
                                }
@@ -177,7 +321,7 @@ int LibShuffCommand::execute(){
                        }
                }
                
-               if (m->control_pressed) {  delete form; globaldata->Groups.clear(); delete globaldata->gMatrix;  globaldata->gMatrix = NULL; delete reading; return 0; }
+               if (m->control_pressed) { outputTypes.clear();  delete form; m->clearGroups(); delete matrix; delete groupMap; delete reading; return 0; }
        
                reading->finish();
                delete reading;
@@ -187,15 +331,13 @@ int LibShuffCommand::execute(){
                printCoverageFile();
                                
                //clear out users groups
-               globaldata->Groups.clear();
+               m->clearGroups();
                delete form;
                
-               //delete globaldata's copy of the gmatrix to free up memory
-               delete globaldata->gMatrix;  globaldata->gMatrix = NULL;
+               delete matrix; delete groupMap;
                
-               if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str()); } return 0; }
+               if (m->control_pressed) {  outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]); } return 0; }
 
-               
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
                for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
@@ -215,9 +357,11 @@ int LibShuffCommand::printCoverageFile() {
        try {
 
                ofstream outCov;
-               summaryFile = outputDir + m->getRootName(m->getSimpleName(globaldata->getPhylipFile())) + "libshuff.coverage";
+        map<string, string> variables; 
+        variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(phylipfile));
+               summaryFile = getOutputFileName("coverage", variables);
                m->openOutputFile(summaryFile, outCov);
-               outputNames.push_back(summaryFile);
+               outputNames.push_back(summaryFile); outputTypes["coverage"].push_back(summaryFile);
                outCov.setf(ios::fixed, ios::floatfield); outCov.setf(ios::showpoint);
                //cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
                
@@ -233,8 +377,8 @@ int LibShuffCommand::printCoverageFile() {
                        for(int j=0;j<numGroups;j++){
                                indices[i][j] = index++;
                                
-                               int spoti = globaldata->gGroupmap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
-                               int spotj = globaldata->gGroupmap->groupIndex[groupNames[j]];
+                               int spoti = groupMap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
+                               int spotj = groupMap->groupIndex[groupNames[j]];
                                
                                for(int k=0;k<savedMinValues[spoti][spotj].size();k++){
                                        
@@ -311,9 +455,11 @@ int LibShuffCommand::printSummaryFile() {
        try {
 
                ofstream outSum;
-               summaryFile = outputDir + m->getRootName(m->getSimpleName(globaldata->getPhylipFile())) + "libshuff.summary";
+        map<string, string> variables; 
+        variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(phylipfile));
+               summaryFile = getOutputFileName("libshuffsummary",variables);
                m->openOutputFile(summaryFile, outSum);
-               outputNames.push_back(summaryFile);
+               outputNames.push_back(summaryFile); outputTypes["libshuffsummary"].push_back(summaryFile);
 
                outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
                cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
@@ -327,8 +473,8 @@ int LibShuffCommand::printSummaryFile() {
                        for(int j=i+1;j<numGroups;j++){
                                if(m->control_pressed)  { outSum.close(); return 0; }
                                
-                               int spoti = globaldata->gGroupmap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
-                               int spotj = globaldata->gGroupmap->groupIndex[groupNames[j]];
+                               int spoti = groupMap->groupIndex[groupNames[i]]; //neccessary in case user selects groups so you know where they are in the matrix
+                               int spotj = groupMap->groupIndex[groupNames[j]];
                                
                                if(pValueCounts[i][j]){
                                        cout << setw(20) << left << groupNames[i]+'-'+groupNames[j] << '\t' << setprecision(8) << savedDXYValues[spoti][spotj] << '\t' << setprecision(precision) << pValueCounts[i][j]/(float)iters << endl;
@@ -366,49 +512,51 @@ int LibShuffCommand::printSummaryFile() {
 
 void LibShuffCommand::setGroups() {
        try {
+               vector<string> myGroups = m->getGroups();
                //if the user has not entered specific groups to analyze then do them all
-               if (globaldata->Groups.size() == 0) {
-                       numGroups = globaldata->gGroupmap->getNumGroups();
+               if (m->getNumGroups() == 0) {
+                       numGroups = groupMap->getNumGroups();
                        for (int i=0; i < numGroups; i++) { 
-                               globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
+                               myGroups.push_back((groupMap->getNamesOfGroups())[i]);
                        }
                } else {
                        if (savegroups != "all") {
                                //check that groups are valid
-                               for (int i = 0; i < globaldata->Groups.size(); i++) {
-                                       if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
-                                               m->mothurOut(globaldata->Groups[i] + " is not a valid group, and will be disregarded."); m->mothurOutEndLine();
+                               for (int i = 0; i < myGroups.size(); i++) {
+                                       if (groupMap->isValidGroup(myGroups[i]) != true) {
+                                               m->mothurOut(myGroups[i] + " is not a valid group, and will be disregarded."); m->mothurOutEndLine();
                                                // erase the invalid group from globaldata->Groups
-                                               globaldata->Groups.erase(globaldata->Groups.begin()+i);
+                                               myGroups.erase(myGroups.begin()+i);
                                        }
                                }
                        
                                //if the user only entered invalid groups
-                               if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) { 
-                                       numGroups = globaldata->gGroupmap->getNumGroups();
+                               if ((myGroups.size() == 0) || (myGroups.size() == 1)) { 
+                                       numGroups = groupMap->getNumGroups();
                                        for (int i=0; i < numGroups; i++) { 
-                                               globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
+                                               myGroups.push_back((groupMap->getNamesOfGroups())[i]);
                                        }
                                        m->mothurOut("When using the groups parameter you must have at least 2 valid groups. I will run the command using all the groups in your groupfile."); m->mothurOutEndLine();
-                               } else { numGroups = globaldata->Groups.size(); }
+                               } else { numGroups = myGroups.size(); }
                        } else { //users wants all groups
-                               numGroups = globaldata->gGroupmap->getNumGroups();
-                               globaldata->Groups.clear();
+                               numGroups = groupMap->getNumGroups();
+                               myGroups.clear();
                                for (int i=0; i < numGroups; i++) { 
-                                       globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
+                                       myGroups.push_back((groupMap->getNamesOfGroups())[i]);
                                }
                        }
                }
 
                //sort so labels match
-               sort(globaldata->Groups.begin(), globaldata->Groups.end());
+               sort(myGroups.begin(), myGroups.end());
                
                //sort
-               sort(globaldata->gGroupmap->namesOfGroups.begin(), globaldata->gGroupmap->namesOfGroups.end());
+               //sort(groupMap->namesOfGroups.begin(), groupMap->namesOfGroups.end());
                
-               for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {  globaldata->gGroupmap->groupIndex[globaldata->gGroupmap->namesOfGroups[i]] = i;  }
+               for (int i = 0; i < (groupMap->getNamesOfGroups()).size(); i++) {  groupMap->groupIndex[(groupMap->getNamesOfGroups())[i]] = i;  }
 
-               groupNames = globaldata->Groups;
+               groupNames = myGroups;
+               m->setGroups(myGroups);
 
        }
        catch(exception& e) {