]> git.donarmstrong.com Git - mothur.git/blobdiff - hclustercommand.cpp
fixes while testing
[mothur.git] / hclustercommand.cpp
index d9a14d9b5979636dba434733a132d35c25de8568..5c08320bbed66718d9fa3fb335a36af6cfaf3a44 100644 (file)
@@ -9,6 +9,56 @@
 
 #include "hclustercommand.h"
 
+//**********************************************************************************************************************
+vector<string> HClusterCommand::getValidParameters(){  
+       try {
+               string Array[] =  {"cutoff","hard","precision","method","phylip","column","name","sorted","showabund","timing","outputdir","inputdir"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "HClusterCommand", "getValidParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+HClusterCommand::HClusterCommand(){    
+       try {
+               abort = true;
+               //initialize outputTypes
+               vector<string> tempOutNames;
+               outputTypes["list"] = tempOutNames;
+               outputTypes["rabund"] = tempOutNames;
+               outputTypes["sabund"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "HClusterCommand", "HClusterCommand");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> HClusterCommand::getRequiredParameters(){       
+       try {
+               string Array[] =  {"phylip","column","or"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "HClusterCommand", "getRequiredParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> HClusterCommand::getRequiredFiles(){    
+       try {
+               vector<string> myArray;
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "HClusterCommand", "getRequiredFiles");
+               exit(1);
+       }
+}
 //**********************************************************************************************************************
 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
 HClusterCommand::HClusterCommand(string option)  {
@@ -21,7 +71,7 @@ HClusterCommand::HClusterCommand(string option)  {
                
                else {
                        //valid paramters for this command
-                       string Array[] =  {"cutoff","precision","method","phylip","column","name","sorted","showabund","timing","outputdir","inputdir"};
+                       string Array[] =  {"cutoff","hard","precision","method","phylip","column","name","sorted","showabund","timing","outputdir","inputdir"};
                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                        
                        OptionParser parser(option);
@@ -37,6 +87,12 @@ HClusterCommand::HClusterCommand(string option)  {
                                }
                        }
                        
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["list"] = tempOutNames;
+                       outputTypes["rabund"] = tempOutNames;
+                       outputTypes["sabund"] = tempOutNames;
+               
                        globaldata->newRead();
                        
                        //if the user changes the input directory command factory will send this info to us in the output parameter 
@@ -47,7 +103,7 @@ HClusterCommand::HClusterCommand(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;           }
                                }
@@ -55,7 +111,7 @@ HClusterCommand::HClusterCommand(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;           }
                                }
@@ -63,7 +119,7 @@ HClusterCommand::HClusterCommand(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;             }
                                }
@@ -104,10 +160,13 @@ HClusterCommand::HClusterCommand(string option)  {
                        length = temp.length();
                        convert(temp, precision); 
                        
+                       temp = validParameter.validFile(parameters, "hard", false);                     if (temp == "not found") { temp = "F"; }
+                       hard = m->isTrue(temp);
+                       
                        temp = validParameter.validFile(parameters, "cutoff", false);
                        if (temp == "not found") { temp = "10"; }
                        convert(temp, cutoff); 
-                       cutoff += (5 / (precision * 10.0));
+                       cutoff += (5 / (precision * 10.0)); 
                        
                        method = validParameter.validFile(parameters, "method", false);
                        if (method == "not found") { method = "furthest"; }
@@ -120,7 +179,7 @@ HClusterCommand::HClusterCommand(string option)  {
                        
                        sort = validParameter.validFile(parameters, "sorted", false);
                        if (sort == "not found") { sort = "F"; }
-                       sorted = isTrue(sort);
+                       sorted = m->isTrue(sort);
 
                        timing = validParameter.validFile(parameters, "timing", false);
                        if (timing == "not found") { timing = "F"; }
@@ -128,20 +187,20 @@ HClusterCommand::HClusterCommand(string option)  {
                                
                        if (abort == false) {
                                
-                               if (outputDir == "") {  outputDir += hasPath(distfile); }
-                               fileroot = outputDir + getRootName(getSimpleName(distfile));
+                               if (outputDir == "") {  outputDir += m->hasPath(distfile); }
+                               fileroot = outputDir + m->getRootName(m->getSimpleName(distfile));
                                
                                if (method == "furthest")               { tag = "fn";  }
                                else if (method == "nearest")   { tag = "nn";  }
                                else                                                    { tag = "an";  }
                        
-                               openOutputFile(fileroot+ tag + ".sabund",       sabundFile);
-                               openOutputFile(fileroot+ tag + ".rabund",       rabundFile);
-                               openOutputFile(fileroot+ tag + ".list",         listFile);
+                               m->openOutputFile(fileroot+ tag + ".sabund",    sabundFile);
+                               m->openOutputFile(fileroot+ tag + ".rabund",    rabundFile);
+                               m->openOutputFile(fileroot+ tag + ".list",              listFile);
                                
-                               outputNames.push_back(fileroot+ tag + ".sabund");
-                               outputNames.push_back(fileroot+ tag + ".rabund");
-                               outputNames.push_back(fileroot+ tag + ".list");
+                               outputNames.push_back(fileroot+ tag + ".sabund"); outputTypes["sabund"].push_back(fileroot+ tag + ".sabund");
+                               outputNames.push_back(fileroot+ tag + ".rabund"); outputTypes["rabund"].push_back(fileroot+ tag + ".rabund");
+                               outputNames.push_back(fileroot+ tag + ".list"); outputTypes["list"].push_back(fileroot+ tag + ".list");
                        }
                }
        }
@@ -160,7 +219,7 @@ void HClusterCommand::help(){
                m->mothurOut("The name parameter allows you to enter your name file and is required if your distance file is in column format. \n");
                m->mothurOut("The hcluster command should be in the following format: \n");
                m->mothurOut("hcluster(column=youDistanceFile, name=yourNameFile, method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
-               m->mothurOut("The acceptable hcluster methods are furthest and nearest, but we hope to add average in the future.\n\n");        
+               m->mothurOut("The acceptable hcluster methods are furthest, nearest and average.\n\n"); 
        }
        catch(exception& e) {
                m->errorOut(e, "HClusterCommand", "help");
@@ -189,7 +248,7 @@ int HClusterCommand::execute(){
                time_t estart = time(NULL);
                
                if (!sorted) {
-                       read = new ReadCluster(distfile, cutoff);       
+                       read = new ReadCluster(distfile, cutoff, outputDir, true);      
                        read->setFormat(format);
                        read->read(globaldata->nameMap);
                        
@@ -198,7 +257,7 @@ int HClusterCommand::execute(){
                                sabundFile.close();
                                rabundFile.close();
                                listFile.close();
-                               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();
                                return 0;  
                        }
                        
@@ -214,7 +273,7 @@ int HClusterCommand::execute(){
                        sabundFile.close();
                        rabundFile.close();
                        listFile.close();
-                       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();
                        return 0;  
                }
 
@@ -244,7 +303,7 @@ int HClusterCommand::execute(){
                                sabundFile.close();
                                rabundFile.close();
                                listFile.close();
-                               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();
                                return 0;  
                }
 
@@ -258,7 +317,7 @@ int HClusterCommand::execute(){
                                sabundFile.close();
                                rabundFile.close();
                                listFile.close();
-                               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();
                                return 0;  
                        }
 
@@ -272,12 +331,18 @@ int HClusterCommand::execute(){
                                                sabundFile.close();
                                                rabundFile.close();
                                                listFile.close();
-                                               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();
                                                return 0;  
                                        }
 
-                                       
-                                       float rndDist = roundDist(seqs[i].dist, precision);
+                       
+                                       float rndDist;
+                                       if (hard) {
+                                               rndDist = m->ceilDist(seqs[i].dist, precision); 
+                                       }else{
+                                               rndDist = m->roundDist(seqs[i].dist, precision); 
+                                       }
+
                                        
                                        if((previousDist <= 0.0000) && (seqs[i].dist != previousDist)){
                                                printData("unique");
@@ -299,7 +364,7 @@ int HClusterCommand::execute(){
                        sabundFile.close();
                        rabundFile.close();
                        listFile.close();
-                       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();
                        return 0;  
                }
                                        
@@ -327,7 +392,7 @@ int HClusterCommand::execute(){
                delete cluster;
                
                if (m->control_pressed) {  
-                       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();
                        return 0;  
                }
 
@@ -351,7 +416,7 @@ int HClusterCommand::execute(){
 
 void HClusterCommand::printData(string label){
        try {
-               if (isTrue(timing)) {
+               if (m->isTrue(timing)) {
                        m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins()) 
                     + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
                }
@@ -360,7 +425,7 @@ void HClusterCommand::printData(string label){
                start = time(NULL);
 
                oldRAbund.setLabel(label);
-               if (isTrue(showabund)) {
+               if (m->isTrue(showabund)) {
                        oldRAbund.getSAbundVector().print(cout);
                }
                oldRAbund.print(rabundFile);