]> git.donarmstrong.com Git - mothur.git/blobdiff - mgclustercommand.cpp
1.18.0
[mothur.git] / mgclustercommand.cpp
index 182e78dc31502fb6c9a75e1703c8f17ffe4ba0fb..945d116c035d663eff0a3bc2317fda9ba643b908 100644 (file)
@@ -9,19 +9,82 @@
 
 #include "mgclustercommand.h"
 
+//**********************************************************************************************************************
+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);
+               
+               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, "MGClusterCommand", "setParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+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 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";
+               helpString += "The precision parameter's default value is 100. \n";
+               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 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";
+               helpString += "The mgcluster command should be in the following format: \n";
+               helpString += "mgcluster(blast=yourBlastfile, name=yourNameFile, cutoff=yourCutOff).\n";
+               helpString += "Note: No spaces between parameter labels (i.e. balst), '=' and parameters (i.e.yourBlastfile).\n";
+               return helpString;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "MGClusterCommand", "getHelpString");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+MGClusterCommand::MGClusterCommand(){  
+       try {
+               abort = true; calledHelp = true; 
+               setParameters();
+               vector<string> tempOutNames;
+               outputTypes["list"] = tempOutNames;
+               outputTypes["rabund"] = tempOutNames;
+               outputTypes["sabund"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "MGClusterCommand", "MGClusterCommand");
+               exit(1);
+       }
+}
 //**********************************************************************************************************************
 MGClusterCommand::MGClusterCommand(string option) {
        try {
-               globaldata = GlobalData::getInstance();
-               abort = false;
+               abort = false; calledHelp = false;   
                
                //allow user to run help
-               if(option == "help") { help(); abort = true; }
+               if(option == "help") { help(); abort = true; calledHelp = true; }
                
                else {
-                       //valid paramters for this command
-                       string Array[] =  {"blast", "method", "name", "hard", "cutoff", "precision", "length", "min", "penalty", "hcluster","merge","outputdir","inputdir"};
-                       vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+                       vector<string> myArray = setParameters();
                        
                        OptionParser parser(option);
                        map<string, string> parameters = parser.getParameters();
@@ -34,6 +97,12 @@ MGClusterCommand::MGClusterCommand(string option) {
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["list"] = tempOutNames;
+                       outputTypes["rabund"] = tempOutNames;
+                       outputTypes["sabund"] = 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 = "";          }
@@ -42,7 +111,7 @@ MGClusterCommand::MGClusterCommand(string option) {
                                it = parameters.find("blast");
                                //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["blast"] = inputDir + it->second;            }
                                }
@@ -50,7 +119,7 @@ MGClusterCommand::MGClusterCommand(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;             }
                                }
@@ -59,13 +128,13 @@ MGClusterCommand::MGClusterCommand(string option) {
                        
                        //check for required parameters
                        blastfile = validParameter.validFile(parameters, "blast", true);
-                       if (blastfile == "not open") { abort = true; }  
+                       if (blastfile == "not open") { blastfile = ""; abort = true; }  
                        else if (blastfile == "not found") { blastfile = ""; }
                        
                        //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 += hasPath(blastfile); //if user entered a file with a path then preserve it  
+                               outputDir += m->hasPath(blastfile); //if user entered a file with a path then preserve it       
                        }
                        
                        namefile = validParameter.validFile(parameters, "name", true);
@@ -85,7 +154,7 @@ MGClusterCommand::MGClusterCommand(string option) {
                        cutoff += (5 / (precision * 10.0));
                        
                        method = validParameter.validFile(parameters, "method", false);
-                       if (method == "not found") { method = "furthest"; }
+                       if (method == "not found") { method = "average"; }
                        
                        if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
                        else { m->mothurOut("Not a valid clustering method.  Valid clustering algorithms are furthest, nearest or average."); m->mothurOutEndLine(); abort = true; }
@@ -97,16 +166,16 @@ MGClusterCommand::MGClusterCommand(string option) {
                        convert(temp, penalty); 
                        
                        temp = validParameter.validFile(parameters, "min", false);                              if (temp == "not found") { temp = "true"; }
-                       minWanted = isTrue(temp); 
+                       minWanted = m->isTrue(temp); 
                        
                        temp = validParameter.validFile(parameters, "merge", false);                    if (temp == "not found") { temp = "true"; }
-                       merge = isTrue(temp); 
+                       merge = m->isTrue(temp); 
                        
                        temp = validParameter.validFile(parameters, "hcluster", false);                 if (temp == "not found") { temp = "false"; }
-                       hclusterWanted = isTrue(temp); 
+                       hclusterWanted = m->isTrue(temp); 
                        
-                       temp = validParameter.validFile(parameters, "hard", false);                     if (temp == "not found") { temp = "F"; }
-                       hard = isTrue(temp);
+                       temp = validParameter.validFile(parameters, "hard", false);                     if (temp == "not found") { temp = "T"; }
+                       hard = m->isTrue(temp);
                }
 
        }
@@ -116,36 +185,10 @@ MGClusterCommand::MGClusterCommand(string option) {
        }
 }
 //**********************************************************************************************************************
-
-void MGClusterCommand::help(){
-       try {
-               m->mothurOut("The mgcluster command parameter options are blast, name, cutoff, precision, method, merge, min, length, penalty and hcluster. The blast parameter is required.\n");
-               m->mothurOut("The mgcluster command reads a blast and name file and clusters the sequences into OPF units similiar to the OTUs.\n");
-               m->mothurOut("This command outputs a .list, .rabund and .sabund file that can be used with mothur other commands to estimate richness.\n");
-               m->mothurOut("The cutoff parameter is used to specify the maximum distance you would like to cluster to. The default is 0.70.\n");
-               m->mothurOut("The precision parameter's default value is 100. \n");
-               m->mothurOut("The acceptable mgcluster methods are furthest, nearest and average.  If no method is provided then furthest is assumed.\n\n");    
-               m->mothurOut("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");
-               m->mothurOut("The length parameter is used to specify the minimum overlap required.  The default is 5.\n");
-               m->mothurOut("The penalty parameter is used to adjust the error rate.  The default is 0.10.\n");
-               m->mothurOut("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");
-               m->mothurOut("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");
-               m->mothurOut("The mgcluster command should be in the following format: \n");
-               m->mothurOut("mgcluster(blast=yourBlastfile, name=yourNameFile, cutoff=yourCutOff).\n");
-               m->mothurOut("Note: No spaces between parameter labels (i.e. balst), '=' and parameters (i.e.yourBlastfile).\n\n");
-       }
-       catch(exception& e) {
-               m->errorOut(e, "MGClusterCommand", "help");
-               exit(1);
-       }
-}
-//**********************************************************************************************************************
-MGClusterCommand::~MGClusterCommand(){}
-//**********************************************************************************************************************
 int MGClusterCommand::execute(){
        try {
                
-               if (abort == true) {    return 0;       }
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
                //read names file
                if (namefile != "") {
@@ -153,7 +196,7 @@ int MGClusterCommand::execute(){
                        nameMap->readMap();
                }else{ nameMap= new NameAssignment(); }
                
-               string fileroot = outputDir + getRootName(getSimpleName(blastfile));
+               string fileroot = outputDir + m->getRootName(m->getSimpleName(blastfile));
                string tag = "";
                time_t start;
                float previousDist = 0.00000;
@@ -166,15 +209,8 @@ int MGClusterCommand::execute(){
                
                list = new ListVector(nameMap->getListVector());
                RAbundVector* rabund = new RAbundVector(list->getRAbundVector());
-for (int i = 0; i < list->getNumBins(); i++) {
-string bin = list->get(i);
-if(bin == "") {
-cout << "bin " << i << " is blank."<< endl;
-}
-}
-cout << "after outputting blank bins." << endl;
                
-               if (m->control_pressed) { delete nameMap; delete read; delete list; delete rabund; return 0; }
+               if (m->control_pressed) { outputTypes.clear(); delete nameMap; delete read; delete list; delete rabund; return 0; }
                
                start = time(NULL);
                oldList = *list;
@@ -186,13 +222,14 @@ cout << "after outputting blank bins." << endl;
                else                                                    { tag = "an";  }
                
                //open output files
-               openOutputFile(fileroot+ tag + ".list",  listFile);
-               openOutputFile(fileroot+ tag + ".rabund",  rabundFile);
-               openOutputFile(fileroot+ tag + ".sabund",  sabundFile);
+               m->openOutputFile(fileroot+ tag + ".list",  listFile);
+               m->openOutputFile(fileroot+ tag + ".rabund",  rabundFile);
+               m->openOutputFile(fileroot+ tag + ".sabund",  sabundFile);
                
                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());
+                       outputTypes.clear();
                        return 0; 
                }
                
@@ -213,9 +250,10 @@ cout << "after outputting blank bins." << endl;
                        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());
+                               outputTypes.clear();
                                return 0; 
                        }
-       int count = 0;          
+               
                        //cluster using cluster classes
                        while (distMatrix->getSmallDist() < cutoff && distMatrix->getNNodes() > 0){
                                
@@ -224,31 +262,30 @@ cout << "after outputting blank bins." << endl;
                                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());
+                                       outputTypes.clear();
                                        return 0; 
                                }
                                
                                float dist = distMatrix->getSmallDist();
                                float rndDist;
                                if (hard) {
-                                       rndDist = ceilDist(dist, precision); 
+                                       rndDist = m->ceilDist(dist, precision); 
                                }else{
-                                       rndDist = roundDist(dist, precision); 
+                                       rndDist = m->roundDist(dist, precision); 
                                }
-cout << "here " << count << '\t' << dist << endl;
                                
                                if(previousDist <= 0.0000 && dist != previousDist){
                                        oldList.setLabel("unique");
                                        printData(&oldList);
-                                       Seq2Bin = cluster->getSeqtoBin();
                                }
                                else if(rndDist != rndPreviousDist){
                                        if (merge) {
-                                               Seq2Bin = cluster->getSeqtoBin();
                                                ListVector* temp = mergeOPFs(oldSeq2Bin, rndPreviousDist);
                                                
                                                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());
+                                                       outputTypes.clear();
                                                        return 0; 
                                                }
                                                
@@ -260,27 +297,26 @@ cout << "here " << count << '\t' << dist << endl;
                                                printData(&oldList);
                                        }
                                }
-       //cout << "after merge " << count << '\t' << dist << endl;      
-       count++;                
+       
                                previousDist = dist;
                                rndPreviousDist = rndDist;
                                oldList = *list;
+                               Seq2Bin = cluster->getSeqtoBin();
                                oldSeq2Bin = Seq2Bin;
                        }
                        
                        if(previousDist <= 0.0000){
                                oldList.setLabel("unique");
                                printData(&oldList);
-                               Seq2Bin = cluster->getSeqtoBin();
                        }
                        else if(rndPreviousDist<cutoff){
                                if (merge) {
-                                       Seq2Bin = cluster->getSeqtoBin();
                                        ListVector* temp = mergeOPFs(oldSeq2Bin, rndPreviousDist);
                                        
                                        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());
+                                                       outputTypes.clear();
                                                        return 0; 
                                        }
                                        
@@ -310,6 +346,7 @@ cout << "here " << count << '\t' << dist << endl;
                        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());
+                               outputTypes.clear();
                                return 0; 
                        }
                
@@ -321,11 +358,12 @@ cout << "here " << count << '\t' << dist << endl;
                        
                        vector<seqDist> seqs; seqs.resize(1); // to start loop
                        //ifstream inHcluster;
-                       //openInputFile(distFile, inHcluster);
+                       //m->openInputFile(distFile, inHcluster);
                        
                        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());
+                               outputTypes.clear();
                                return 0; 
                        }
 
@@ -338,6 +376,7 @@ cout << "here " << count << '\t' << dist << endl;
                                        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());
+                                       outputTypes.clear();
                                        return 0; 
                                }
                                
@@ -352,24 +391,23 @@ cout << "here " << count << '\t' << dist << endl;
                                                        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());
+                                                       outputTypes.clear();
                                                        return 0; 
                                                }
        
                                                float rndDist;
                                                if (hard) {
-                                                       rndDist = ceilDist(seqs[i].dist, precision); 
+                                                       rndDist = m->ceilDist(seqs[i].dist, precision); 
                                                }else{
-                                                       rndDist = roundDist(seqs[i].dist, precision); 
+                                                       rndDist = m->roundDist(seqs[i].dist, precision); 
                                                }
                                                                                                
                                                if((previousDist <= 0.0000) && (seqs[i].dist != previousDist)){
                                                        oldList.setLabel("unique");
                                                        printData(&oldList);
-                                                       Seq2Bin = hcluster->getSeqtoBin();
                                                }
                                                else if((rndDist != rndPreviousDist)){
                                                        if (merge) {
-                                                               Seq2Bin = hcluster->getSeqtoBin();
                                                                ListVector* temp = mergeOPFs(oldSeq2Bin, rndPreviousDist);
                                                                
                                                                if (m->control_pressed) { 
@@ -377,6 +415,7 @@ cout << "here " << count << '\t' << dist << endl;
                                                                        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());
+                                                                       outputTypes.clear();
                                                                        return 0; 
                                                                }
 
@@ -392,6 +431,7 @@ cout << "here " << count << '\t' << dist << endl;
                                                previousDist = seqs[i].dist;
                                                rndPreviousDist = rndDist;
                                                oldList = *list;
+                                               Seq2Bin = cluster->getSeqtoBin();
                                                oldSeq2Bin = Seq2Bin;
                                        }
                                }
@@ -404,7 +444,6 @@ cout << "here " << count << '\t' << dist << endl;
                        }
                        else if(rndPreviousDist<cutoff){
                                if (merge) {
-                                       Seq2Bin = hcluster->getSeqtoBin();
                                        ListVector* temp = mergeOPFs(oldSeq2Bin, rndPreviousDist);
                                        
                                        if (m->control_pressed) { 
@@ -412,6 +451,7 @@ cout << "here " << count << '\t' << dist << endl;
                                                        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());
+                                                       outputTypes.clear();
                                                        return 0; 
                                        }
                                        
@@ -435,24 +475,40 @@ cout << "here " << count << '\t' << dist << endl;
                sabundFile.close();
                rabundFile.close();
        
-               globaldata->setListFile(fileroot+ tag + ".list");
-               globaldata->setFormat("list");
-               
                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());
-                       globaldata->setListFile("");
-                       globaldata->setFormat("");
+                       outputTypes.clear();
                        return 0; 
                }
                
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
-               m->mothurOut(fileroot+ tag + ".list"); m->mothurOutEndLine();   
-               m->mothurOut(fileroot+ tag + ".rabund"); m->mothurOutEndLine(); 
-               m->mothurOut(fileroot+ tag + ".sabund"); 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->mothurOutEndLine();
                
+               //set list file as new current listfile
+               string current = "";
+               itTypes = outputTypes.find("list");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
+               }
+               
+               //set rabund file as new current rabundfile
+               itTypes = outputTypes.find("rabund");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
+               }
+               
+               //set sabund file as new current sabundfile
+               itTypes = outputTypes.find("sabund");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
+               }
+               
+               
                m->mothurOut("It took " + toString(time(NULL) - start) + " seconds to cluster."); m->mothurOutEndLine();
                        
                return 0;
@@ -485,23 +541,13 @@ ListVector* MGClusterCommand::mergeOPFs(map<string, int> binInfo, float dist){
        try {
                //create new listvector so you don't overwrite the clustering
                ListVector* newList = new ListVector(oldList);
-for (int i = 0; i < newList->getNumBins(); i++) {
-string bin = newList->get(i);
-if(bin == "") {
-cout << "bin " << i << " is blank."<< endl;
-for (map<string, int>::iterator itBin = binInfo.begin(); itBin != binInfo.end(); itBin++) {
-       if (itBin->second == i) { cout << itBin->first << " maps to an empty bin." << endl; }
-}
-}
-}
-cout << "after outputting blank bins." << endl;                
 
                bool done = false;
                ifstream inOverlap;
                int count = 0;
                
                if (hclusterWanted) {  
-                       openInputFile(overlapFile, inOverlap);  
+                       m->openInputFile(overlapFile, inOverlap);  
                        if (inOverlap.eof()) {  done = true;  }
                }else { if (overlapMatrix.size() == 0)  {  done = true;  } } 
                
@@ -522,15 +568,18 @@ cout << "after outputting blank bins." << endl;
                                if (!inOverlap.eof()) {
                                        string firstName, secondName;
                                        float overlapDistance;
-                                       inOverlap >> firstName >> secondName >> overlapDistance; gobble(inOverlap);
+                                       inOverlap >> firstName >> secondName >> overlapDistance; m->gobble(inOverlap);
                                        
-                                       map<string,int>::iterator itA = nameMap->find(firstName);
-                                       map<string,int>::iterator itB = nameMap->find(secondName);
-                                       if(itA == nameMap->end()){  cerr << "AAError: Sequence '" << firstName << "' was not found in the names file, please correct\n"; exit(1);  }
-                                       if(itB == nameMap->end()){  cerr << "ABError: Sequence '" << secondName << "' was not found in the names file, please correct\n"; exit(1);  }
+                                       //commented out because we check this in readblast already
+                                       //map<string,int>::iterator itA = nameMap->find(firstName);
+                                       //map<string,int>::iterator itB = nameMap->find(secondName);
+                                       //if(itA == nameMap->end()){  cerr << "AAError: Sequence '" << firstName << "' was not found in the names file, please correct\n"; exit(1);  }
+                                       //if(itB == nameMap->end()){  cerr << "ABError: Sequence '" << secondName << "' was not found in the names file, please correct\n"; exit(1);  }
                                        
-                                       overlapNode.seq1 = itA->second;
-                                       overlapNode.seq2 = itB->second;
+                                       //overlapNode.seq1 = itA->second;
+                                       //overlapNode.seq2 = itB->second;
+                                       overlapNode.seq1 = nameMap->get(firstName);
+                                       overlapNode.seq2 = nameMap->get(secondName);
                                        overlapNode.dist = overlapDistance;
                                }else { inOverlap.close(); break; }
                        } 
@@ -541,22 +590,24 @@ cout << "after outputting blank bins." << endl;
                                string name2 = nameMap->get(overlapNode.seq2);
                        
                                //use binInfo to find out if they are already in the same bin
-                               map<string, int>::iterator itBin1 = binInfo.find(name1);
-                               map<string, int>::iterator itBin2 = binInfo.find(name2);
+                               //map<string, int>::iterator itBin1 = binInfo.find(name1);
+                               //map<string, int>::iterator itBin2 = binInfo.find(name2);
                                
-                               if(itBin1 == binInfo.end()){  cerr << "AAError: Sequence '" << name1 << "' does not have any bin info.\n"; exit(1);  }
-                               if(itBin2 == binInfo.end()){  cerr << "ABError: Sequence '" << name2 << "' does not have any bin info.\n"; exit(1);  }
-cout << overlapNode.dist << '\t' << dist << endl;
-                               int binKeep = itBin1->second;
-                               int binRemove = itBin2->second;
+                               //if(itBin1 == binInfo.end()){  cerr << "AAError: Sequence '" << name1 << "' does not have any bin info.\n"; exit(1);  }
+                               //if(itBin2 == binInfo.end()){  cerr << "ABError: Sequence '" << name2 << "' does not have any bin info.\n"; exit(1);  }
+
+                               //int binKeep = itBin1->second;
+                               //int binRemove = itBin2->second;
+                               
+                               int binKeep = binInfo[name1];
+                               int binRemove = binInfo[name2];
                        
                                //if not merge bins and update binInfo
                                if(binKeep != binRemove) {
-       cout << "bin keep = " << binKeep << " bin remove = " << binRemove << endl;              
+               
                                        //save names in old bin
                                        string names = newList->get(binRemove);
-                       cout << names << endl << endl << endl;  
-                       cout << newList->get(binKeep) << endl << endl << endl;  
+               
                                        //merge bins into name1s bin
                                        newList->set(binKeep, newList->get(binRemove)+','+newList->get(binKeep));
                                        newList->set(binRemove, "");    
@@ -590,12 +641,12 @@ cout << overlapNode.dist << '\t' << dist << endl;
 void MGClusterCommand::sortHclusterFiles(string unsortedDist, string unsortedOverlap) {
        try {
                //sort distFile
-               string sortedDistFile = sortFile(unsortedDist, outputDir);
+               string sortedDistFile = m->sortFile(unsortedDist, outputDir);
                remove(unsortedDist.c_str());  //delete unsorted file
                distFile = sortedDistFile;
                
                //sort overlap file
-               string sortedOverlapFile = sortFile(unsortedOverlap, outputDir);
+               string sortedOverlapFile = m->sortFile(unsortedOverlap, outputDir);
                remove(unsortedOverlap.c_str());  //delete unsorted file
                overlapFile = sortedOverlapFile;
        }