]> git.donarmstrong.com Git - mothur.git/blobdiff - mgclustercommand.cpp
changes while testing
[mothur.git] / mgclustercommand.cpp
index b982b8d1deb3cbc51716cf74339b8dcaea5d136d..97f0afdb366949d39e5af37a96c0211d9df0ca17 100644 (file)
 //**********************************************************************************************************************
 vector<string> MGClusterCommand::setParameters(){      
        try {
-               CommandParameter pblast("blast", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pblast);
-               CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
-               CommandParameter plength("length", "Number", "", "5", "", "", "",false,false); parameters.push_back(plength);
-               CommandParameter ppenalty("penalty", "Number", "", "0.10", "", "", "",false,false); parameters.push_back(ppenalty);
-               CommandParameter pcutoff("cutoff", "Number", "", "0.70", "", "", "",false,false); parameters.push_back(pcutoff);
-               CommandParameter pprecision("precision", "Number", "", "100", "", "", "",false,false); parameters.push_back(pprecision);
-               CommandParameter pmethod("method", "Multiple", "furthest-nearest-average", "average", "", "", "",false,false); parameters.push_back(pmethod);
-               CommandParameter phard("hard", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(phard);
-               CommandParameter pmin("min", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pmin);
-               CommandParameter pmerge("merge", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pmerge);
-               CommandParameter phcluster("hcluster", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(phcluster);
-               CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
-               CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
+               CommandParameter pblast("blast", "InputTypes", "", "", "none", "none", "none","list",false,true,true); parameters.push_back(pblast);
+               CommandParameter pname("name", "InputTypes", "", "", "NameCount", "none", "ColumnName","rabund-sabund",false,false,true); parameters.push_back(pname);
+               CommandParameter pcount("count", "InputTypes", "", "", "NameCount", "none", "none","",false,false,true); parameters.push_back(pcount);
+               CommandParameter plength("length", "Number", "", "5", "", "", "","",false,false); parameters.push_back(plength);
+               CommandParameter ppenalty("penalty", "Number", "", "0.10", "", "", "","",false,false); parameters.push_back(ppenalty);
+               CommandParameter pcutoff("cutoff", "Number", "", "0.70", "", "", "","",false,false,true); parameters.push_back(pcutoff);
+               CommandParameter pprecision("precision", "Number", "", "100", "", "", "","",false,false); parameters.push_back(pprecision);
+               CommandParameter pmethod("method", "Multiple", "furthest-nearest-average", "average", "", "", "","",false,false); parameters.push_back(pmethod);
+               CommandParameter phard("hard", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(phard);
+               CommandParameter pmin("min", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(pmin);
+               CommandParameter pmerge("merge", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(pmerge);
+        CommandParameter padjust("adjust", "String", "", "F", "", "", "","",false,false); parameters.push_back(padjust);
+               CommandParameter phcluster("hcluster", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(phcluster);
+               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);          }
@@ -39,7 +41,7 @@ vector<string> MGClusterCommand::setParameters(){
 string MGClusterCommand::getHelpString(){      
        try {
                string helpString = "";
-               helpString += "The mgcluster command parameter options are blast, name, cutoff, precision, hard,  method, merge, min, length, penalty and hcluster. The blast parameter is required.\n";
+               helpString += "The mgcluster command parameter options are blast, name, cutoff, precision, hard,  method, merge, min, length, penalty, adjust and hcluster. The blast parameter is required.\n";
                helpString += "The mgcluster command reads a blast and name file and clusters the sequences into OPF units similiar to the OTUs.\n";
                helpString += "This command outputs a .list, .rabund and .sabund file that can be used with mothur other commands to estimate richness.\n";
                helpString += "The cutoff parameter is used to specify the maximum distance you would like to cluster to. The default is 0.70.\n";
@@ -47,6 +49,7 @@ string MGClusterCommand::getHelpString(){
                helpString += "The acceptable mgcluster methods are furthest, nearest and average.  If no method is provided then average is assumed.\n";       
                helpString += "The min parameter allows you to specify is you want the minimum or maximum blast score ratio used in calculating the distance. The default is true, meaning you want the minimum.\n";
                helpString += "The length parameter is used to specify the minimum overlap required.  The default is 5.\n";
+        helpString += "The adjust parameter is used to handle missing distances.  If you set a cutoff, adjust=f by default.  If not, adjust=t by default. Adjust=f, means ignore missing distances and adjust cutoff as needed with the average neighbor method.  Adjust=t, will treat missing distances as 1.0. You can also set the value the missing distances should be set to, adjust=0.5 would give missing distances a value of 0.5.\n";
                helpString += "The penalty parameter is used to adjust the error rate.  The default is 0.10.\n";
                helpString += "The merge parameter allows you to shut off merging based on overlaps and just cluster.  By default merge is true, meaning you want to merge.\n";
                helpString += "The hcluster parameter allows you to use the hcluster algorithm when clustering.  This may be neccessary if your file is too large to fit into RAM. The default is false.\n";
@@ -61,6 +64,23 @@ string MGClusterCommand::getHelpString(){
        }
 }
 //**********************************************************************************************************************
+string MGClusterCommand::getOutputPattern(string type) {
+    try {
+        string pattern = "";
+        
+        if (type == "list") {  pattern = "[filename],[clustertag],list-[filename],[clustertag],[tag2],list"; } 
+        else if (type == "rabund") {  pattern = "[filename],[clustertag],rabund"; } 
+        else if (type == "sabund") {  pattern = "[filename],[clustertag],sabund"; }
+        else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
+        
+        return pattern;
+    }
+    catch(exception& e) {
+        m->errorOut(e, "MGClusterCommand", "getOutputPattern");
+        exit(1);
+    }
+}
+//*******************************************************************************************************************
 MGClusterCommand::MGClusterCommand(){  
        try {
                abort = true; calledHelp = true; 
@@ -124,6 +144,14 @@ MGClusterCommand::MGClusterCommand(string option) {
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["name"] = inputDir + it->second;             }
                                }
+                
+                it = parameters.find("count");
+                               //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["count"] = inputDir + it->second;            }
+                               }
                        }
 
                        
@@ -142,17 +170,27 @@ MGClusterCommand::MGClusterCommand(string option) {
                        if (namefile == "not open") { abort = true; }   
                        else if (namefile == "not found") { namefile = ""; }
                        else { m->setNameFile(namefile); }
+            
+            countfile = validParameter.validFile(parameters, "count", true);
+                       if (countfile == "not open") { abort = true; }  
+                       else if (countfile == "not found") { countfile = ""; }
+            else { m->setCountTableFile(countfile); }
+            
+            if (countfile != "" && namefile != "") { m->mothurOut("[ERROR]: Cannot have both a name file and count file. Please use one or the other."); m->mothurOutEndLine(); abort = true; }
                        
                        if ((blastfile == "")) { m->mothurOut("When executing a mgcluster command you must provide a blastfile."); m->mothurOutEndLine(); abort = true; }
                        
                        //check for optional parameter and set defaults
                        string temp;
-                       temp = validParameter.validFile(parameters, "precision", false);                if (temp == "not found") { temp = "100"; }
+            temp = validParameter.validFile(parameters, "precision", false);           if (temp == "not found") { temp = "100"; }
                        precisionLength = temp.length();
-                       convert(temp, precision); 
+                       m->mothurConvert(temp, precision); 
                        
-                       temp = validParameter.validFile(parameters, "cutoff", false);                   if (temp == "not found") { temp = "0.70"; }
-                       convert(temp, cutoff); 
+            cutoffSet = false;
+                       temp = validParameter.validFile(parameters, "cutoff", false);
+            if (temp == "not found") { temp = "0.70"; }
+            else { cutoffSet = true;  }
+                       m->mothurConvert(temp, cutoff); 
                        cutoff += (5 / (precision * 10.0));
                        
                        method = validParameter.validFile(parameters, "method", false);
@@ -162,10 +200,10 @@ MGClusterCommand::MGClusterCommand(string option) {
                        else { m->mothurOut("Not a valid clustering method.  Valid clustering algorithms are furthest, nearest or average."); m->mothurOutEndLine(); abort = true; }
 
                        temp = validParameter.validFile(parameters, "length", false);                   if (temp == "not found") { temp = "5"; }
-                       convert(temp, length); 
+                       m->mothurConvert(temp, length); 
                        
                        temp = validParameter.validFile(parameters, "penalty", false);                  if (temp == "not found") { temp = "0.10"; }
-                       convert(temp, penalty); 
+                       m->mothurConvert(temp, penalty); 
                        
                        temp = validParameter.validFile(parameters, "min", false);                              if (temp == "not found") { temp = "true"; }
                        minWanted = m->isTrue(temp); 
@@ -178,6 +216,11 @@ MGClusterCommand::MGClusterCommand(string option) {
                        
                        temp = validParameter.validFile(parameters, "hard", false);                     if (temp == "not found") { temp = "T"; }
                        hard = m->isTrue(temp);
+            
+            temp = validParameter.validFile(parameters, "adjust", false);                              if (temp == "not found") { if (cutoffSet) { temp = "F"; }else { temp="T"; } }
+            if (m->isNumeric1(temp))    { m->mothurConvert(temp, adjust);   }
+            else if (m->isTrue(temp))   { adjust = 1.0;                     }
+            else                        { adjust = -1.0;                    }
                }
 
        }
@@ -189,28 +232,44 @@ MGClusterCommand::MGClusterCommand(string option) {
 //**********************************************************************************************************************
 int MGClusterCommand::execute(){
        try {
-               
                if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
                //read names file
                if (namefile != "") {
                        nameMap = new NameAssignment(namefile);
                        nameMap->readMap();
-               }else{ nameMap= new NameAssignment(); }
+               }else if (countfile != "") {
+            ct = new CountTable();
+            ct->readTable(countfile, false);
+            nameMap= new NameAssignment();
+            vector<string> tempNames = ct->getNamesOfSeqs();
+            for (int i = 0; i < tempNames.size(); i++) {  nameMap->push_back(tempNames[i]);  }
+        }else{ nameMap= new NameAssignment(); }
                
                string fileroot = outputDir + m->getRootName(m->getSimpleName(blastfile));
                string tag = "";
                time_t start;
                float previousDist = 0.00000;
-               float rndPreviousDist = 0.00000;
-               
+               float rndPreviousDist = 0.00000; 
+        
                //read blastfile - creates sparsematrices for the distances and overlaps as well as a listvector
                //must remember to delete those objects here since readBlast does not
                read = new ReadBlast(blastfile, cutoff, penalty, length, minWanted, hclusterWanted);
                read->read(nameMap);
-               
-               list = new ListVector(nameMap->getListVector());
-               RAbundVector* rabund = new RAbundVector(list->getRAbundVector());
+        
+        list = new ListVector(nameMap->getListVector());
+        RAbundVector* rabund = NULL;
+        
+        if(countfile != "") {
+            rabund = new RAbundVector();
+            createRabund(ct, list, rabund);
+        }else {
+            rabund = new RAbundVector(list->getRAbundVector());
+        }
+        
+                
+               //list = new ListVector(nameMap->getListVector());
+               //rabund = new RAbundVector(list->getRAbundVector());
                
                if (m->control_pressed) { outputTypes.clear(); delete nameMap; delete read; delete list; delete rabund; return 0; }
                
@@ -223,14 +282,23 @@ int MGClusterCommand::execute(){
                else if (method == "nearest")   { tag = "nn";  }
                else                                                    { tag = "an";  }
                
-               //open output files
-               m->openOutputFile(fileroot+ tag + ".list",  listFile);
-               m->openOutputFile(fileroot+ tag + ".rabund",  rabundFile);
-               m->openOutputFile(fileroot+ tag + ".sabund",  sabundFile);
+        map<string, string> variables; 
+        variables["[filename]"] = fileroot;
+        variables["[clustertag]"] = tag;
+        string sabundFileName = getOutputFileName("sabund", variables);
+        string rabundFileName = getOutputFileName("rabund", variables);
+        if (countfile != "") { variables["[tag2]"] = "unique_list"; }
+        string listFileName = getOutputFileName("list", variables);
+        
+        if (countfile == "") {
+            m->openOutputFile(sabundFileName,  sabundFile);
+            m->openOutputFile(rabundFileName,  rabundFile);
+        }
+               m->openOutputFile(listFileName, listFile);
                
                if (m->control_pressed) { 
                        delete nameMap; delete read; delete list; delete rabund; 
-                       listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
+                       listFile.close(); if (countfile == "") { rabundFile.close(); sabundFile.close();  m->mothurRemove((fileroot+ tag + ".rabund")); m->mothurRemove((fileroot+ tag + ".sabund")); } m->mothurRemove((fileroot+ tag + ".list"));
                        outputTypes.clear();
                        return 0; 
                }
@@ -239,33 +307,36 @@ int MGClusterCommand::execute(){
                
                if (!hclusterWanted) {
                        //get distmatrix and overlap
-                       SparseMatrix* distMatrix = read->getDistMatrix();
+                       SparseDistanceMatrix* distMatrix = read->getDistMatrix();
                        overlapMatrix = read->getOverlapMatrix(); //already sorted by read 
                        delete read;
                
                        //create cluster
-                       if (method == "furthest")       {       cluster = new CompleteLinkage(rabund, list, distMatrix, cutoff, method); }
-                       else if(method == "nearest"){   cluster = new SingleLinkage(rabund, list, distMatrix, cutoff, method); }
-                       else if(method == "average"){   cluster = new AverageLinkage(rabund, list, distMatrix, cutoff, method); }
+                       if (method == "furthest")       {       cluster = new CompleteLinkage(rabund, list, distMatrix, cutoff, method, adjust); }
+                       else if(method == "nearest"){   cluster = new SingleLinkage(rabund, list, distMatrix, cutoff, method, adjust); }
+                       else if(method == "average"){   cluster = new AverageLinkage(rabund, list, distMatrix, cutoff, method, adjust); }
                        cluster->setMapWanted(true);
                        Seq2Bin = cluster->getSeqtoBin();
                        oldSeq2Bin = Seq2Bin;
                        
                        if (m->control_pressed) { 
                                delete nameMap; delete distMatrix; delete list; delete rabund; delete cluster;
-                               listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
+                               listFile.close(); if (countfile == "") { rabundFile.close(); sabundFile.close();  m->mothurRemove((fileroot+ tag + ".rabund")); m->mothurRemove((fileroot+ tag + ".sabund")); } m->mothurRemove((fileroot+ tag + ".list"));
                                outputTypes.clear();
                                return 0; 
                        }
-               
+            
+            
                        //cluster using cluster classes
                        while (distMatrix->getSmallDist() < cutoff && distMatrix->getNNodes() > 0){
                                
+                if (m->debug) {  cout << "numNodes=" << distMatrix->getNNodes() << " smallDist = " << distMatrix->getSmallDist() << endl; }
+                
                                cluster->update(cutoff);
                                
                                if (m->control_pressed) { 
                                        delete nameMap; delete distMatrix; delete list; delete rabund; delete cluster;
-                                       listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
+                                       listFile.close(); if (countfile == "") { rabundFile.close(); sabundFile.close();  m->mothurRemove((fileroot+ tag + ".rabund")); m->mothurRemove((fileroot+ tag + ".sabund")); } m->mothurRemove((fileroot+ tag + ".list"));
                                        outputTypes.clear();
                                        return 0; 
                                }
@@ -288,7 +359,7 @@ int MGClusterCommand::execute(){
                                                
                                                if (m->control_pressed) { 
                                                        delete nameMap; delete distMatrix; delete list; delete rabund; delete cluster; delete temp;
-                                                       listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
+                                                       listFile.close(); if (countfile == "") { rabundFile.close(); sabundFile.close();  m->mothurRemove((fileroot+ tag + ".rabund")); m->mothurRemove((fileroot+ tag + ".sabund")); } m->mothurRemove((fileroot+ tag + ".list"));
                                                        outputTypes.clear();
                                                        return 0; 
                                                }
@@ -319,7 +390,7 @@ int MGClusterCommand::execute(){
                                        
                                        if (m->control_pressed) { 
                                                        delete nameMap; delete distMatrix; delete list; delete rabund; delete cluster; delete temp;
-                                                       listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
+                                                       listFile.close(); if (countfile == "") { rabundFile.close(); sabundFile.close();  m->mothurRemove((fileroot+ tag + ".rabund")); m->mothurRemove((fileroot+ tag + ".sabund")); } m->mothurRemove((fileroot+ tag + ".list"));
                                                        outputTypes.clear();
                                                        return 0; 
                                        }
@@ -349,7 +420,7 @@ int MGClusterCommand::execute(){
                        
                        if (m->control_pressed) { 
                                delete nameMap;  delete list; delete rabund; 
-                               listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
+                               listFile.close(); if (countfile == "") { rabundFile.close(); sabundFile.close();  m->mothurRemove((fileroot+ tag + ".rabund")); m->mothurRemove((fileroot+ tag + ".sabund")); } m->mothurRemove((fileroot+ tag + ".list"));
                                outputTypes.clear();
                                return 0; 
                        }
@@ -366,7 +437,7 @@ int MGClusterCommand::execute(){
                        
                        if (m->control_pressed) { 
                                delete nameMap;  delete list; delete rabund; delete hcluster;
-                               listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
+                               listFile.close(); if (countfile == "") { rabundFile.close(); sabundFile.close();  m->mothurRemove((fileroot+ tag + ".rabund")); m->mothurRemove((fileroot+ tag + ".sabund")); } m->mothurRemove((fileroot+ tag + ".list"));
                                outputTypes.clear();
                                return 0; 
                        }
@@ -382,9 +453,9 @@ int MGClusterCommand::execute(){
                                
                                if (m->control_pressed) { 
                                        delete nameMap;  delete list; delete rabund; delete hcluster;
-                                       listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
-                                       remove(distFile.c_str());
-                                       remove(overlapFile.c_str());
+                                       listFile.close(); if (countfile == "") { rabundFile.close(); sabundFile.close();  m->mothurRemove((fileroot+ tag + ".rabund")); m->mothurRemove((fileroot+ tag + ".sabund")); } m->mothurRemove((fileroot+ tag + ".list"));
+                                       m->mothurRemove(distFile);
+                                       m->mothurRemove(overlapFile);
                                        outputTypes.clear();
                                        return 0; 
                                }
@@ -397,9 +468,9 @@ int MGClusterCommand::execute(){
                                                
                                                if (m->control_pressed) { 
                                                        delete nameMap;  delete list; delete rabund; delete hcluster;
-                                                       listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
-                                                       remove(distFile.c_str());
-                                                       remove(overlapFile.c_str());
+                                                       listFile.close(); if (countfile == "") { rabundFile.close(); sabundFile.close();  m->mothurRemove((fileroot+ tag + ".rabund")); m->mothurRemove((fileroot+ tag + ".sabund")); } m->mothurRemove((fileroot+ tag + ".list"));
+                                                       m->mothurRemove(distFile);
+                                                       m->mothurRemove(overlapFile);
                                                        outputTypes.clear();
                                                        return 0; 
                                                }
@@ -421,9 +492,9 @@ int MGClusterCommand::execute(){
                                                                
                                                                if (m->control_pressed) { 
                                                                        delete nameMap;  delete list; delete rabund; delete hcluster; delete temp;
-                                                                       listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
-                                                                       remove(distFile.c_str());
-                                                                       remove(overlapFile.c_str());
+                                                                       listFile.close(); if (countfile == "") { rabundFile.close(); sabundFile.close();  m->mothurRemove((fileroot+ tag + ".rabund")); m->mothurRemove((fileroot+ tag + ".sabund")); } m->mothurRemove((fileroot+ tag + ".list"));
+                                                                       m->mothurRemove(distFile);
+                                                                       m->mothurRemove(overlapFile);
                                                                        outputTypes.clear();
                                                                        return 0; 
                                                                }
@@ -457,9 +528,9 @@ int MGClusterCommand::execute(){
                                        
                                        if (m->control_pressed) { 
                                                        delete nameMap; delete list; delete rabund; delete hcluster; delete temp;
-                                                       listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
-                                                       remove(distFile.c_str());
-                                                       remove(overlapFile.c_str());
+                                                       listFile.close(); if (countfile == "") { rabundFile.close(); sabundFile.close();  m->mothurRemove((fileroot+ tag + ".rabund")); m->mothurRemove((fileroot+ tag + ".sabund")); } m->mothurRemove((fileroot+ tag + ".list"));
+                                                       m->mothurRemove(distFile);
+                                                       m->mothurRemove(overlapFile);
                                                        outputTypes.clear();
                                                        return 0; 
                                        }
@@ -474,28 +545,31 @@ int MGClusterCommand::execute(){
                        }
                        
                        delete hcluster;
-                       remove(distFile.c_str());
-                       remove(overlapFile.c_str());
+                       m->mothurRemove(distFile);
+                       m->mothurRemove(overlapFile);
                }
                
-               delete list; 
+               delete list;
                delete rabund;
                listFile.close();
-               sabundFile.close();
-               rabundFile.close();
-       
+        if (countfile == "") {
+            sabundFile.close();
+            rabundFile.close();
+        }
                if (m->control_pressed) { 
                        delete nameMap; 
-                       listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
+                       listFile.close(); if (countfile == "") { rabundFile.close(); sabundFile.close();  m->mothurRemove((fileroot+ tag + ".rabund")); m->mothurRemove((fileroot+ tag + ".sabund")); } m->mothurRemove((fileroot+ tag + ".list"));
                        outputTypes.clear();
                        return 0; 
                }
                
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
-               m->mothurOut(fileroot+ tag + ".list"); m->mothurOutEndLine();   outputNames.push_back(fileroot+ tag + ".list"); outputTypes["list"].push_back(fileroot+ tag + ".list");
-               m->mothurOut(fileroot+ tag + ".rabund"); m->mothurOutEndLine(); outputNames.push_back(fileroot+ tag + ".rabund"); outputTypes["rabund"].push_back(fileroot+ tag + ".rabund");
-               m->mothurOut(fileroot+ tag + ".sabund"); m->mothurOutEndLine(); outputNames.push_back(fileroot+ tag + ".sabund"); outputTypes["sabund"].push_back(fileroot+ tag + ".sabund");
+               m->mothurOut(listFileName); m->mothurOutEndLine();      outputNames.push_back(listFileName); outputTypes["list"].push_back(listFileName);
+               if (countfile == "") {
+            m->mothurOut(rabundFileName); m->mothurOutEndLine();       outputNames.push_back(rabundFileName); outputTypes["rabund"].push_back(rabundFileName);
+            m->mothurOut(sabundFileName); m->mothurOutEndLine();       outputNames.push_back(sabundFileName); outputTypes["sabund"].push_back(sabundFileName);
+        }
                m->mothurOutEndLine();
                
                if (saveCutoff != cutoff) { 
@@ -538,12 +612,14 @@ int MGClusterCommand::execute(){
 void MGClusterCommand::printData(ListVector* mergedList){
        try {
                mergedList->print(listFile);
-               mergedList->getRAbundVector().print(rabundFile);
-               
-               SAbundVector sabund = mergedList->getSAbundVector();
+        SAbundVector sabund = mergedList->getSAbundVector();
+        
+        if (countfile == "") {
+            mergedList->getRAbundVector().print(rabundFile);
+            sabund.print(sabundFile);
+        }
 
                sabund.print(cout);
-               sabund.print(sabundFile);
        }
        catch(exception& e) {
                m->errorOut(e, "MGClusterCommand", "printData");
@@ -658,12 +734,12 @@ void MGClusterCommand::sortHclusterFiles(string unsortedDist, string unsortedOve
        try {
                //sort distFile
                string sortedDistFile = m->sortFile(unsortedDist, outputDir);
-               remove(unsortedDist.c_str());  //delete unsorted file
+               m->mothurRemove(unsortedDist);  //delete unsorted file
                distFile = sortedDistFile;
                
                //sort overlap file
                string sortedOverlapFile = m->sortFile(unsortedOverlap, outputDir);
-               remove(unsortedOverlap.c_str());  //delete unsorted file
+               m->mothurRemove(unsortedOverlap);  //delete unsorted file
                overlapFile = sortedOverlapFile;
        }
        catch(exception& e) {
@@ -674,7 +750,33 @@ void MGClusterCommand::sortHclusterFiles(string unsortedDist, string unsortedOve
 
 //**********************************************************************************************************************
 
+void MGClusterCommand::createRabund(CountTable*& ct, ListVector*& list, RAbundVector*& rabund){
+    try {
+        //vector<string> names = ct.getNamesOfSeqs();
 
+        //for ( int i; i < ct.getNumGroups(); i++ ) {    rav.push_back( ct.getNumSeqs(names[i]) );    }
+        //return rav;
+        
+        for(int i = 0; i < list->getNumBins(); i++) { 
+           vector<string> binNames;
+           string bin = list->get(i);
+           m->splitAtComma(bin, binNames);
+           int total = 0;
+           for (int j = 0; j < binNames.size(); j++) { 
+               total += ct->getNumSeqs(binNames[j]);
+           }
+           rabund->push_back(total);   
+       }
+        
+        
+    }
+    catch(exception& e) {
+               m->errorOut(e, "MGClusterCommand", "createRabund");
+               exit(1);
+       }
+    
+}
 
+//**********************************************************************************************************************