]> git.donarmstrong.com Git - mothur.git/blobdiff - getoturepcommand.cpp
get.oturep ifstream
[mothur.git] / getoturepcommand.cpp
index 91990811173cea55be293e643647ff8a1352db77..9cc99732e715d40f0ef06743bb85870a147f7036 100644 (file)
@@ -12,7 +12,7 @@
 #include "readcolumn.h"
 #include "formatphylip.h"
 #include "formatcolumn.h"
-
+#include "sharedutilities.h"
 
 
 //********************************************************************************************************************
@@ -36,6 +36,55 @@ inline bool compareGroup(repStruct left, repStruct right){
        return (left.group < right.group);      
 }
 //**********************************************************************************************************************
+GetOTURepCommand::GetOTURepCommand(){  
+       try {
+               abort = true;
+               //initialize outputTypes
+               vector<string> tempOutNames;
+               outputTypes["fasta"] = tempOutNames;
+               outputTypes["name"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "GetOTURepCommand", "GetOTURepCommand");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> GetOTURepCommand::getValidParameters(){ 
+       try {
+               string Array[] =  {"fasta","list","label","name", "group", "weighted","sorted", "phylip","column","large","cutoff","precision","groups","outputdir","inputdir"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "GetOTURepCommand", "getValidParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> GetOTURepCommand::getRequiredParameters(){      
+       try {
+               string Array[] =  {"fasta","list"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "GetOTURepCommand", "getRequiredParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> GetOTURepCommand::getRequiredFiles(){   
+       try {
+               vector<string> myArray;
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "GetOTURepCommand", "getRequiredFiles");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
 GetOTURepCommand::GetOTURepCommand(string option)  {
        try{
                globaldata = GlobalData::getInstance();
@@ -48,7 +97,7 @@ GetOTURepCommand::GetOTURepCommand(string option)  {
                        help(); abort = true;
                } else {
                        //valid paramters for this command
-                       string Array[] =  {"fasta","list","label","name", "group", "sorted", "phylip","column","large","cutoff","precision","outputdir","inputdir"};
+                       string Array[] =  {"fasta","list","label","name","weighted", "group", "sorted", "phylip","column","large","cutoff","precision","groups","outputdir","inputdir"};
                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                        
                        OptionParser parser(option);
@@ -62,6 +111,11 @@ GetOTURepCommand::GetOTURepCommand(string option)  {
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["fasta"] = tempOutNames;
+                       outputTypes["name"] = tempOutNames;
+                       
                        //if the user changes the input directory command factory will send this info to us in the output parameter 
                        string inputDir = validParameter.validFile(parameters, "inputdir", false);              
                        if (inputDir == "not found"){   inputDir = "";          }
@@ -70,7 +124,7 @@ GetOTURepCommand::GetOTURepCommand(string option)  {
                                it = parameters.find("list");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       path = m->hasPath(it->second);
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["list"] = inputDir + it->second;             }
                                }
@@ -78,7 +132,7 @@ GetOTURepCommand::GetOTURepCommand(string option)  {
                                it = parameters.find("fasta");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       path = m->hasPath(it->second);
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
                                }
@@ -86,7 +140,7 @@ GetOTURepCommand::GetOTURepCommand(string option)  {
                                it = parameters.find("phylip");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       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;           }
                                }
@@ -94,7 +148,7 @@ GetOTURepCommand::GetOTURepCommand(string option)  {
                                it = parameters.find("column");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       path = m->hasPath(it->second);
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["column"] = inputDir + it->second;           }
                                }
@@ -102,7 +156,7 @@ GetOTURepCommand::GetOTURepCommand(string option)  {
                                it = parameters.find("name");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       path = m->hasPath(it->second);
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["name"] = inputDir + it->second;             }
                                }
@@ -110,7 +164,7 @@ GetOTURepCommand::GetOTURepCommand(string option)  {
                                it = parameters.find("group");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       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;            }
                                }
@@ -153,7 +207,7 @@ GetOTURepCommand::GetOTURepCommand(string option)  {
                        label = validParameter.validFile(parameters, "label", false);                   
                        if (label == "not found") { label = ""; allLines = 1;  }
                        else { 
-                               if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
+                               if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
                                else { allLines = 1;  }
                        }
                        
@@ -172,8 +226,25 @@ GetOTURepCommand::GetOTURepCommand(string option)  {
                                sorted = "";
                        }
                        
+                       groups = validParameter.validFile(parameters, "groups", false);                 
+                       if (groups == "not found") { groups = ""; }
+                       else { 
+                               if (groupfile == "") {
+                                       m->mothurOut("You must provide a groupfile to use groups."); m->mothurOutEndLine();
+                                       abort = true;
+                               }else { 
+                                       m->splitAtDash(groups, Groups);
+                               }
+                       }
+                       globaldata->Groups = Groups;
+                       
                        string temp = validParameter.validFile(parameters, "large", false);             if (temp == "not found") {      temp = "F";     }
-                       large = isTrue(temp);
+                       large = m->isTrue(temp);
+                       
+                       temp = validParameter.validFile(parameters, "weighted", false);         if (temp == "not found") {      if (namefile == "") { temp = "F"; } else { temp = "t"; }        }
+                       weighted = m->isTrue(temp);
+                       
+                       if ((weighted) && (namefile == "")) { m->mothurOut("You cannot set weighted to true unless you provide a namesfile."); m->mothurOutEndLine(); abort = true; }
                        
                        temp = validParameter.validFile(parameters, "precision", false);                        if (temp == "not found") { temp = "100"; }
                        convert(temp, precision); 
@@ -193,7 +264,7 @@ GetOTURepCommand::GetOTURepCommand(string option)  {
 
 void GetOTURepCommand::help(){
        try {
-               m->mothurOut("The get.oturep command parameters are phylip, column, list, fasta, name, group, large, cutoff, precision, sorted and label.  The fasta and list parameters are required, as well as phylip or column and name.\n");
+               m->mothurOut("The get.oturep command parameters are phylip, column, list, fasta, name, group, large, weighted, cutoff, precision, groups, sorted and label.  The fasta and list parameters are required, as well as phylip or column and name.\n");
                m->mothurOut("The label parameter allows you to select what distance levels you would like a output files created for, and is separated by dashes.\n");
                m->mothurOut("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");
                m->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");
@@ -202,6 +273,13 @@ void GetOTURepCommand::help(){
                m->mothurOut("The default value for label is all labels in your inputfile.\n");
                m->mothurOut("The sorted parameter allows you to indicate you want the output sorted. You can sort by sequence name, bin number, bin size or group. The default is no sorting, but your options are name, number, size, or group.\n");
                m->mothurOut("The large parameter allows you to indicate that your distance matrix is too large to fit in RAM.  The default value is false.\n");
+               m->mothurOut("The weighted parameter allows you to indicate that want to find the weighted representative. You must provide a namesfile to set weighted to true.  The default value is false with no namesfile and true when a name file is provided.\n");
+               m->mothurOut("The representative is found by selecting the sequence that has the smallest total distance to all other sequences in the OTU. If a tie occurs the smallest average distance is used.\n");
+               m->mothurOut("For weighted = false, mothur assumes the distance file contains only unique sequences, the list file may contain all sequences, but only the uniques are considered to become the representative. If your distance file contains all the sequences it would become weighted=true.\n");
+               m->mothurOut("For weighted = true, mothur assumes the distance file contains only unique sequences, the list file must contain all sequences, all sequences are considered to become the representative, but unique name will be used in the output for consistency.\n");
+               m->mothurOut("If your distance file contains all the sequence and you do not provide a name file, the weighted representative will be given, unless your listfile is unique. If you provide a namefile, then you can select weighted or unweighted.\n");
+               m->mothurOut("The group parameter allows you provide a group file.\n");
+               m->mothurOut("The groups parameter allows you to indicate that you want representative sequences for each group specified for each OTU, group name should be separated by dashes. ex. groups=A-B-C.\n");
                m->mothurOut("The get.oturep command outputs a .fastarep and .rep.names file for each distance you specify, selecting one OTU representative for each bin.\n");
                m->mothurOut("If you provide a groupfile, then it also appends the names of the groups present in that bin.\n");
                m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
@@ -256,6 +334,9 @@ int GetOTURepCommand::execute(){
                                if (m->control_pressed) { delete readMatrix; return 0; }
                                seqVec[currentCell->row][currentCell->column] = currentCell->dist;
                        }
+                       //add dummy map for unweighted calc
+                       SeqMap dummy;
+                       seqVec.push_back(dummy);
                        
                        delete matrix;
                        delete readMatrix;
@@ -288,12 +369,13 @@ int GetOTURepCommand::execute(){
                        //positions in file where the distances for each sequence begin
                        //rowPositions[1] = position in file where distance related to sequence 1 start.
                        rowPositions = formatMatrix->getRowPositions();
+                       rowPositions.push_back(-1); //dummy row for unweighted calc
                        
                        delete formatMatrix;
                        delete nameMap;
                        
                        //openfile for getMap to use
-                       openInputFile(distFile, inRow);
+                       m->openInputFile(distFile, inRow);
                        
                        if (m->control_pressed) { inRow.close(); remove(distFile.c_str()); return 0; }
                }
@@ -308,7 +390,7 @@ int GetOTURepCommand::execute(){
                                names.clear();
                                binnames = globaldata->gListVector->get(i);
                                
-                               splitAtComma(binnames, names);
+                               m->splitAtComma(binnames, names);
                                
                                for (int j = 0; j < names.size(); j++) {
                                        nameToIndex[names[j]] = i;
@@ -321,10 +403,23 @@ int GetOTURepCommand::execute(){
                        if (large) {  inRow.close(); remove(distFile.c_str());  }
                        return 0; 
                }
-                               
+               
+               if (groupfile != "") {
+                       //read in group map info.
+                       groupMap = new GroupMap(groupfile);
+                       int error = groupMap->readMap();
+                       if (error == 1) { delete groupMap; m->mothurOut("Error reading your groupfile. Proceeding without groupfile."); m->mothurOutEndLine(); groupfile = "";  }
+                       
+                       if (Groups.size() != 0) {
+                               SharedUtil* util = new SharedUtil();
+                               util->setGroups(Groups, groupMap->namesOfGroups, "getoturep");
+                               delete util;
+                       }
+               }
+                                                               
                //set format to list so input can get listvector
                globaldata->setFormat("list");
-
+       
                //read list file
                read = new ReadOTUFile(listfile);
                read->read(&*globaldata); 
@@ -332,7 +427,7 @@ int GetOTURepCommand::execute(){
                input = globaldata->ginput;
                list = globaldata->gListVector;
                string lastLabel = list->getLabel();
-               
+
                //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
                set<string> processedLabels;
                set<string> userLabels = labels;
@@ -341,7 +436,9 @@ int GetOTURepCommand::execute(){
                        if (large) {  inRow.close(); remove(distFile.c_str());  }
                        delete read; delete input; delete list; globaldata->gListVector = NULL; return 0; 
                }
-       
+               
+               if ((!weighted) && (namefile != "")) { readNamesFile(weighted); }
+               
                while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
                        
                        if (allLines == 1 || labels.count(list->getLabel()) == 1){
@@ -351,7 +448,7 @@ int GetOTURepCommand::execute(){
                                        
                                        if (m->control_pressed) { 
                                                if (large) {  inRow.close(); remove(distFile.c_str());  }
-                                               for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  }
+                                               for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  } outputTypes.clear();
                                                delete read; delete input; delete list; globaldata->gListVector = NULL; return 0; 
                                        }
                                        
@@ -359,7 +456,7 @@ int GetOTURepCommand::execute(){
                                        userLabels.erase(list->getLabel());
                        }
                        
-                       if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
+                       if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
                                        string saveLabel = list->getLabel();
                                        
                                        delete list;
@@ -370,7 +467,7 @@ int GetOTURepCommand::execute(){
                                        
                                        if (m->control_pressed) { 
                                                if (large) {  inRow.close(); remove(distFile.c_str());  }
-                                               for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  }
+                                               for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  } outputTypes.clear();
                                                delete read; delete input; delete list; globaldata->gListVector = NULL; return 0; 
                                        }
                                        
@@ -382,7 +479,7 @@ int GetOTURepCommand::execute(){
                        }
                        
                        lastLabel = list->getLabel();
-                       
+       
                        delete list;
                        list = input->getListVector();
                }
@@ -390,8 +487,8 @@ int GetOTURepCommand::execute(){
                //output error messages about any remaining user labels
                bool needToRun = false;
                for (set<string>::iterator it = userLabels.begin(); it != userLabels.end(); it++) {  
-                       m->mothurOut("Your file does not include the label " + *it); 
-                       if (processedLabels.count(list->getLabel()) != 1) {
+                       m->mothurOut("Your file does not include the label " + (*it)); 
+                       if (processedLabels.count(lastLabel) != 1) {
                                m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
                                needToRun = true;
                        }else {
@@ -410,7 +507,7 @@ int GetOTURepCommand::execute(){
                        
                        if (m->control_pressed) { 
                                        if (large) {  inRow.close(); remove(distFile.c_str());  }
-                                       for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  }
+                                       for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  } outputTypes.clear();
                                        delete read; delete input; delete list; globaldata->gListVector = NULL; return 0; 
                        }
                }
@@ -425,13 +522,8 @@ int GetOTURepCommand::execute(){
                delete input;  globaldata->ginput = NULL;
                delete read;
                
-               if (groupfile != "") {
-                       //read in group map info.
-                       groupMap = new GroupMap(groupfile);
-                       int error = groupMap->readMap();
-                       if (error == 1) { delete groupMap; m->mothurOut("Error reading your groupfile. Proceeding without groupfile."); m->mothurOutEndLine(); groupfile = "";  }
-               }
-
+               if (!weighted) { nameFileMap.clear(); }
+               
                //read fastafile
                fasta = new FastaMap();
                fasta->readFastaFile(fastafile);
@@ -468,19 +560,20 @@ int GetOTURepCommand::execute(){
 //**********************************************************************************************************************
 void GetOTURepCommand::readNamesFile() {
        try {
+               ifstream in;
                vector<string> dupNames;
-               openInputFile(namefile, inNames);
+               m->openInputFile(namefile, in);
                
                string name, names, sequence;
        
-               while(inNames){
-                       inNames >> name;                        //read from first column  A
-                       inNames >> names;               //read from second column  A,B,C,D
+               while(!in.eof()){
+                       in >> name;                     //read from first column  A
+                       in >> names;            //read from second column  A,B,C,D
                        
                        dupNames.clear();
                        
                        //parse names into vector
-                       splitAtComma(names, dupNames);
+                       m->splitAtComma(names, dupNames);
                        
                        //store names in fasta map
                        sequence = fasta->getSequence(name);
@@ -488,9 +581,9 @@ void GetOTURepCommand::readNamesFile() {
                                fasta->push_back(dupNames[i], sequence);
                        }
                
-                       gobble(inNames);
+                       m->gobble(in);
                }
-               inNames.close();
+               in.close();
 
        }
        catch(exception& e) {
@@ -499,16 +592,41 @@ void GetOTURepCommand::readNamesFile() {
        }
 }
 //**********************************************************************************************************************
-string GetOTURepCommand::findRep(int bin, ListVector* thisList) {
+//read names file to find the weighted rep for each bin
+void GetOTURepCommand::readNamesFile(bool w) {
+       try {
+               ifstream in;
+               vector<string> dupNames;
+               m->openInputFile(namefile, in);
+               
+               string name, names, sequence;
+               
+               while(!in.eof()){
+                       in >> name;     m->gobble(in);          //read from first column  A
+                       in >> names;                                                    //read from second column  A,B,C,D
+                       
+                       dupNames.clear();
+                       
+                       //parse names into vector
+                       m->splitAtComma(names, dupNames);
+                       
+                       for (int i = 0; i < dupNames.size(); i++) {
+                               nameFileMap[dupNames[i]] = name;
+                       }
+                       
+                       m->gobble(in);
+               }
+               in.close();
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "GetOTURepCommand", "readNamesFile");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+string GetOTURepCommand::findRep(vector<string> names) {
        try{
-               vector<string> names;
-               map<string, string> groups;
-               map<string, string>::iterator groupIt;
-
-               //parse names into vector
-               string binnames = thisList->get(bin);
-               splitAtComma(binnames, names);
-
                // if only 1 sequence in bin or processing the "unique" label, then 
                // the first sequence of the OTU is the representative one
                if ((names.size() == 1) || (list->getLabel() == "unique")) {
@@ -517,10 +635,42 @@ string GetOTURepCommand::findRep(int bin, ListVector* thisList) {
                        vector<int> seqIndex(names.size());
                        vector<float> max_dist(names.size());
                        vector<float> total_dist(names.size());
+                       map<string, string>::iterator itNameFile;
+                       map<string, int>::iterator itNameIndex;
 
                        //fill seqIndex and initialize sums
                        for (size_t i = 0; i < names.size(); i++) {
-                               seqIndex[i] = nameToIndex[names[i]];
+                               if (weighted) {
+                                       seqIndex[i] = nameToIndex[names[i]];
+                               }else { 
+                                       if (namefile == "") {
+                                               itNameIndex = nameToIndex.find(names[i]);
+                                               
+                                               if (itNameIndex == nameToIndex.end()) { // you are not in the distance file and no namesfile, then assume you are not unique
+                                                       if (large) {  seqIndex[i] = (rowPositions.size()-1); }
+                                                       else {  seqIndex[i] = (seqVec.size()-1); }
+                                               }else {
+                                                       seqIndex[i] = itNameIndex->second;
+                                               }
+                                               
+                                       }else {
+                                               itNameFile = nameFileMap.find(names[i]);
+                                               
+                                               if (itNameFile == nameFileMap.end()) {
+                                                       m->mothurOut("[ERROR]: " + names[i] + " is not in your namefile, please correct."); m->mothurOutEndLine(); m->control_pressed = true; 
+                                               }else{
+                                                       string name1 = itNameFile->first;
+                                                       string name2 = itNameFile->second;
+                                                       
+                                                       if (name1 == name2) { //then you are unique so add your real dists
+                                                               seqIndex[i] = nameToIndex[names[i]];
+                                                       }else { //add dummy
+                                                               if (large) {  seqIndex[i] = (rowPositions.size()-1); }
+                                                               else {  seqIndex[i] = (seqVec.size()-1); }
+                                                       }
+                                               }
+                                       }
+                               }
                                max_dist[i] = 0.0;
                                total_dist[i] = 0.0;
                        }
@@ -569,7 +719,7 @@ string GetOTURepCommand::findRep(int bin, ListVector* thisList) {
                                        }
                                }
                        }
-
+                       
                        return(names[minIndex]);
                }
        }
@@ -582,28 +732,100 @@ string GetOTURepCommand::findRep(int bin, ListVector* thisList) {
 //**********************************************************************************************************************
 int GetOTURepCommand::process(ListVector* processList) {
        try{
-               string nameRep, name, sequence;
+               string name, sequence;
+               string nameRep;
 
                //create output file
-               if (outputDir == "") { outputDir += hasPath(listfile); }
+               if (outputDir == "") { outputDir += m->hasPath(listfile); }
                                
                ofstream newNamesOutput;
-               string outputNamesFile = outputDir + getRootName(getSimpleName(listfile)) + processList->getLabel() + ".rep.names";
-               openOutputFile(outputNamesFile, newNamesOutput);
-               outputNames.push_back(outputNamesFile);
-               outputNameFiles[outputNamesFile] = processList->getLabel();
+               string outputNamesFile;
+               map<string, ofstream*> filehandles;
+               
+               if (Groups.size() == 0) { //you don't want to use groups
+                       outputNamesFile  = outputDir + m->getRootName(m->getSimpleName(listfile)) + processList->getLabel() + ".rep.names";
+                       m->openOutputFile(outputNamesFile, newNamesOutput);
+                       outputNames.push_back(outputNamesFile); outputTypes["name"].push_back(outputNamesFile); 
+                       outputNameFiles[outputNamesFile] = processList->getLabel();
+               }else{ //you want to use groups
+                       ofstream* temp;
+                       for (int i=0; i<Groups.size(); i++) {
+                               temp = new ofstream;
+                               filehandles[Groups[i]] = temp;
+                               outputNamesFile = outputDir + m->getRootName(m->getSimpleName(listfile)) + processList->getLabel() + "." + Groups[i] + ".rep.names";
+                               
+                               m->openOutputFile(outputNamesFile, *(temp));
+                               outputNames.push_back(outputNamesFile); outputTypes["name"].push_back(outputNamesFile);
+                               outputNameFiles[outputNamesFile] = processList->getLabel() + "." + Groups[i];
+                       }
+               }
                
                //for each bin in the list vector
                for (int i = 0; i < processList->size(); i++) {
-                       nameRep = findRep(i, processList);
-                       
-                       if (m->control_pressed) { out.close();  newNamesOutput.close(); return 0; }
+                       if (m->control_pressed) { 
+                               out.close();  
+                               if (Groups.size() == 0) { //you don't want to use groups
+                                       newNamesOutput.close();
+                               }else{
+                                       for (int j=0; j<Groups.size(); j++) {
+                                               (*(filehandles[Groups[j]])).close();
+                                               delete filehandles[Groups[j]];
+                                       }
+                               }
+                               return 0; 
+                       }
                        
-                       //output to new names file
-                       newNamesOutput << nameRep << '\t' << processList->get(i) << endl;
+                       string temp = processList->get(i);
+                       vector<string> namesInBin;
+                       m->splitAtComma(temp, namesInBin);
+                       
+                       if (Groups.size() == 0) {
+                               nameRep = findRep(namesInBin);
+                               newNamesOutput << i << '\t' << nameRep << '\t' << processList->get(i) << endl;
+                       }else{
+                               map<string, vector<string> > NamesInGroup;
+                               for (int j=0; j<Groups.size(); j++) { //initialize groups
+                                       NamesInGroup[Groups[j]].resize(0);
+                               }
+                               
+                               for (int j=0; j<namesInBin.size(); j++) {
+                                       string thisgroup = groupMap->getGroup(namesInBin[j]);
+                                       
+                                       if (thisgroup == "not found") { m->mothurOut(namesInBin[j] + " is not in your groupfile, please correct."); m->mothurOutEndLine(); m->control_pressed = true; }
+                                       
+                                       if (m->inUsersGroups(thisgroup, Groups)) { //add this name to correct group
+                                               NamesInGroup[thisgroup].push_back(namesInBin[j]);
+                                       }
+                               }
+                               
+                               //get rep for each group in otu
+                               for (int j=0; j<Groups.size(); j++) {
+                                       if (NamesInGroup[Groups[j]].size() != 0) { //are there members from this group in this otu?
+                                               //get rep for each group
+                                               nameRep = findRep(NamesInGroup[Groups[j]]);
+                                               
+                                               //output group rep and other members of this group
+                                               (*(filehandles[Groups[j]])) << i << '\t' << nameRep << '\t';
+                                               
+                                               for (int k=0; k<NamesInGroup[Groups[j]].size()-1; k++) {//output list of names in this otu from this group
+                                                       (*(filehandles[Groups[j]])) << NamesInGroup[Groups[j]][k] << ",";
+                                               }
+                                               //output last name
+                                               (*(filehandles[Groups[j]])) << NamesInGroup[Groups[j]][NamesInGroup[Groups[j]].size()-1] << endl;
+                                       }
+                               }
+                       }
                }
-
-               newNamesOutput.close();
+               
+               if (Groups.size() == 0) { //you don't want to use groups
+                       newNamesOutput.close();
+               }else{
+                       for (int i=0; i<Groups.size(); i++) {
+                               (*(filehandles[Groups[i]])).close();
+                               delete filehandles[Groups[i]];
+                       }
+               }
+               
                return 0;
 
        }
@@ -617,22 +839,27 @@ int GetOTURepCommand::processNames(string filename, string label) {
        try{
 
                //create output file
-               if (outputDir == "") { outputDir += hasPath(listfile); }
-               string outputFileName = outputDir + getRootName(getSimpleName(listfile)) + label + ".rep.fasta";
-               openOutputFile(outputFileName, out);
+               if (outputDir == "") { outputDir += m->hasPath(listfile); }
+               string outputFileName = outputDir + m->getRootName(m->getSimpleName(listfile)) + label + ".rep.fasta";
+               m->openOutputFile(outputFileName, out);
                vector<repStruct> reps;
-               outputNames.push_back(outputFileName);
+               outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
+               
+               ofstream out2;
+               string tempNameFile = filename + ".temp";
+               m->openOutputFile(tempNameFile, out2);
                
                ifstream in;
-               openInputFile(filename, in);
+               m->openInputFile(filename, in);
                
                int i = 0;
                while (!in.eof()) {
                        string rep, binnames;
-                       in >> rep >> binnames; gobble(in);
+                       in >> i >> rep >> binnames; m->gobble(in);
+                       out2 << rep << '\t' << binnames << endl;
                        
                        vector<string> names;
-                       splitAtComma(binnames, names);
+                       m->splitAtComma(binnames, names);
                        int binsize = names.size();
                        
                        //if you have a groupfile
@@ -666,7 +893,7 @@ int GetOTURepCommand::processNames(string filename, string label) {
 
                        if (sequence != "not found") {
                                if (sorted == "") { //print them out
-                                       rep = rep + "|" + toString(i+1);
+                                       rep = rep + "\t" + toString(i+1);
                                        rep = rep + "|" + toString(binsize);
                                        if (groupfile != "") {
                                                rep = rep + "|" + group;
@@ -680,7 +907,6 @@ int GetOTURepCommand::processNames(string filename, string label) {
                        }else { 
                                m->mothurOut(rep + " is missing from your fasta or name file, ignoring. Please correct."); m->mothurOutEndLine(); 
                        }
-                       i++;
                }
                
                        
@@ -693,7 +919,7 @@ int GetOTURepCommand::processNames(string filename, string label) {
                        //print them
                        for (int i = 0; i < reps.size(); i++) {
                                string sequence = fasta->getSequence(reps[i].name);
-                               string outputName = reps[i].name + "|" + toString(reps[i].bin);
+                               string outputName = reps[i].name + "\t" + toString(reps[i].bin);
                                outputName = outputName + "|" + toString(reps[i].size);
                                if (groupfile != "") {
                                        outputName = outputName + "|" + reps[i].group;
@@ -702,8 +928,13 @@ int GetOTURepCommand::processNames(string filename, string label) {
                                out << sequence << endl;
                        }
                }
-                       
+               
+               in.close();
                out.close();
+               out2.close();
+               
+               remove(filename.c_str());
+               rename(tempNameFile.c_str(), filename.c_str());
                
                return 0;