int MergeGroupsCommand::processGroupFile(DesignMap*& designMap){
	try {
		
		string thisOutputDir = outputDir;
		if (outputDir == "") {  thisOutputDir += m->hasPath(groupfile);  }
        map<string, string> variables; 
        variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(groupfile));
        variables["[extension]"] = m->getExtension(groupfile);
		string outputFileName = getOutputFileName("group", variables);
		outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
		
		ofstream out;
		m->openOutputFile(outputFileName, out);
		
		//read groupfile
		GroupMap groupMap(groupfile);
		groupMap.readMap();
		
		//fill Groups - checks for "all" and for any typo groups
		SharedUtil* util = new SharedUtil();
		vector<string> nameGroups = groupMap.getNamesOfGroups();
		util->setGroups(Groups, nameGroups);
		delete util;
		
		vector<string> namesOfSeqs = groupMap.getNamesSeqs();
		bool error = false;
		
		for (int i = 0; i < namesOfSeqs.size(); i++) {
			
			if (m->control_pressed) { break; }
			
			string thisGroup = groupMap.getGroup(namesOfSeqs[i]);
			
			//are you in a group the user wants
			if (m->inUsersGroups(thisGroup, Groups)) {
				string thisGrouping = designMap->get(thisGroup);
				
				if (thisGrouping == "not found") { m->mothurOut("[ERROR]: " + namesOfSeqs[i] + " is from group " + thisGroup + " which is not in your design file, please correct."); m->mothurOutEndLine();  error = true; }
				else {
					out << namesOfSeqs[i] << '\t' << thisGrouping << endl;
				}
			}
		}
		
		if (error) { m->control_pressed = true; }

		out.close();
		
		return 0;
		
	}
	catch(exception& e) {
		m->errorOut(e, "MergeGroupsCommand", "processGroupFile");
		exit(1);
	}
}
int RenameSeqsCommand::execute() {
	try {
		
		if (abort == true) { if (calledHelp) { return 0; }  return 2;	}
        
        GroupMap groupMap(groupfile);
        groupMap.readMap();
                
		//prepare filenames and open files
        string thisOutputDir = outputDir;
		if (outputDir == "") {  thisOutputDir += m->hasPath(fastaFile);  }
		string outFastaFile = thisOutputDir + m->getRootName(m->getSimpleName(fastaFile));
        map<string, string> variables;
        variables["[filename]"] = outFastaFile;
        variables["[extension]"] = m->getExtension(fastaFile);
        outFastaFile = getOutputFileName("fasta", variables);
        outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile);
        
        ofstream outFasta;
		m->openOutputFile(outFastaFile, outFasta);
        
        ifstream in;
        m->openInputFile(fastaFile, in);
        
        while (!in.eof()) {
            if (m->control_pressed) { break; }
            
            Sequence seq(in); m->gobble(in);
            string group = groupMap.getGroup(seq.getName());
            if (group == "not found") {  m->mothurOut("[ERROR]: " + seq.getName() + " is not in your group file, please correct.\n"); m->control_pressed = true; }
            else {
                string newName = seq.getName() + "_" + group;
                seq.setName(newName);
                seq.printSequence(outFasta);
            }
            
        }
        in.close();
        
        if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]);  } return 0; }
        
        bool notDone = true;
        if (nameFile != "") {
            thisOutputDir = outputDir;
            if (outputDir == "") {  thisOutputDir += m->hasPath(nameFile);  }
            string outNameFile = thisOutputDir + m->getRootName(m->getSimpleName(nameFile));
            variables["[filename]"] = outNameFile;
            variables["[extension]"] = m->getExtension(nameFile);
            outNameFile = getOutputFileName("group", variables);
            outputNames.push_back(outNameFile); outputTypes["name"].push_back(outNameFile);
            
            ofstream outName;
            m->openOutputFile(outNameFile, outName);
            
            map<string, vector<string> > nameMap;
            m->readNames(nameFile, nameMap);
            
            //process name file changing names
            for (map<string, vector<string> >::iterator it = nameMap.begin(); it != nameMap.end(); it++) {
                for (int i = 0; i < (it->second).size()-1; i++) {
                    if (m->control_pressed) { break; }
                    string group = groupMap.getGroup((it->second)[i]);
                    if (group == "not found") {  m->mothurOut("[ERROR]: " + (it->second)[i] + " is not in your group file, please correct.\n"); m->control_pressed = true;  }
                    else {
                        string newName = (it->second)[i] + "_" + group;
                        groupMap.renameSeq((it->second)[i], newName); //change in group file
                        (it->second)[i] = newName; //change in namefile
                    }
                    if (i == 0) {  outName << (it->second)[i] << '\t' << (it->second)[i] << ','; }
                    else { outName << (it->second)[i] << ','; }
                }
                
                //print last one
                if ((it->second).size() == 1) {
                    string group = groupMap.getGroup((it->second)[0]);
                    if (group == "not found") {  m->mothurOut("[ERROR]: " + (it->second)[0] + " is not in your group file, please correct.\n"); m->control_pressed = true;  }
                    else {
                        string newName = (it->second)[0] + "_" + group;
                        groupMap.renameSeq((it->second)[0], newName); //change in group file
                        (it->second)[0] = newName; //change in namefile

                        outName << (it->second)[0] << '\t' << (it->second)[0] << endl;
                    }
                }
                else {
                    string group = groupMap.getGroup((it->second)[(it->second).size()-1]);
                    if (group == "not found") {  m->mothurOut("[ERROR]: " + (it->second)[(it->second).size()-1] + " is not in your group file, please correct.\n"); m->control_pressed = true;  }
                    else {
                        string newName = (it->second)[(it->second).size()-1] + "_" + group;
                        groupMap.renameSeq((it->second)[(it->second).size()-1], newName); //change in group file
                        (it->second)[(it->second).size()-1] = newName; //change in namefile

                        outName << (it->second)[(it->second).size()-1] << endl;
                    }
                }
            }
            notDone = false;
            outName.close();
        }
        
        if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]);  } return 0; }
        
        if (notDone) {
            vector<string> seqs = groupMap.getNamesSeqs();
            for (int i = 0; i < seqs.size(); i++) {
                if (m->control_pressed) { break; }
                string group = groupMap.getGroup(seqs[i]);
                string newName = seqs[i] + "_" + group;
                groupMap.renameSeq(seqs[i], newName);
            }
        }
        if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]);  } return 0; }
        
        thisOutputDir = outputDir;
        if (outputDir == "") {  thisOutputDir += m->hasPath(groupfile);  }
		string outGroupFile = thisOutputDir + m->getRootName(m->getSimpleName(groupfile));
        variables["[filename]"] = outGroupFile;
        variables["[extension]"] = m->getExtension(groupfile);
        outGroupFile = getOutputFileName("group", variables);
        outputNames.push_back(outGroupFile); outputTypes["group"].push_back(outGroupFile);
        
        ofstream outGroup;
		m->openOutputFile(outGroupFile, outGroup);
        groupMap.print(outGroup);
        outGroup.close();
        
        if (m->control_pressed) {  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();	}
        m->mothurOutEndLine();
        
        //set fasta file as new current fastafile
        string current = "";
        itTypes = outputTypes.find("fasta");
        if (itTypes != outputTypes.end()) {
            if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
        }
        
        itTypes = outputTypes.find("name");
        if (itTypes != outputTypes.end()) {
            if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
        }
        
        itTypes = outputTypes.find("group");
        if (itTypes != outputTypes.end()) {
            if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
        }
				
		return 0;
	}
	catch(exception& e) {
		m->errorOut(e, "RenameSeqsCommand", "execute");
		exit(1);
	}
}