]> git.donarmstrong.com Git - mothur.git/blobdiff - clustercommand.cpp
fixes while testing
[mothur.git] / clustercommand.cpp
index d14af108d69c36f98617fc2e133c3dcfb537c553..6043d39be8d559010e156111f41a856b716cda40 100644 (file)
@@ -9,11 +9,62 @@
 
 #include "clustercommand.h"
 
+//**********************************************************************************************************************
+vector<string> ClusterCommand::getValidParameters(){   
+       try {
+               string AlignArray[] =  {"cutoff","precision","method","showabund","timing","hard","outputdir","inputdir"};
+               vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ClusterCommand", "getValidParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+ClusterCommand::ClusterCommand(){      
+       try {
+               abort = true;
+               //initialize outputTypes
+               vector<string> tempOutNames;
+               outputTypes["list"] = tempOutNames;
+               outputTypes["rabund"] = tempOutNames;
+               outputTypes["sabund"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ClusterCommand", "ClusterCommand");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> ClusterCommand::getRequiredParameters(){        
+       try {
+               vector<string> myArray; 
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ClusterCommand", "getRequiredParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> ClusterCommand::getRequiredFiles(){     
+       try {
+               string Array[] =  {"phylip","column","or"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ClusterCommand", "getRequiredFiles");
+               exit(1);
+       }
+}
 //**********************************************************************************************************************
 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
-ClusterCommand::ClusterCommand(string option){
+ClusterCommand::ClusterCommand(string option)  {
        try{
                globaldata = GlobalData::getInstance();
+               
                abort = false;
                
                //allow user to run help
@@ -21,7 +72,7 @@ ClusterCommand::ClusterCommand(string option){
                
                else {
                        //valid paramters for this command
-                       string Array[] =  {"cutoff","precision","method","showabund","timing","outputdir","inputdir"};
+                       string Array[] =  {"cutoff","precision","method","showabund","timing","hard","outputdir","inputdir"};
                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                        
                        OptionParser parser(option);
@@ -36,12 +87,18 @@ ClusterCommand::ClusterCommand(string option){
                                }
                        }
                        
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["list"] = tempOutNames;
+                       outputTypes["rabund"] = tempOutNames;
+                       outputTypes["sabund"] = tempOutNames;
+               
                        //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 = "";         }
                        
                        //error checking to make sure they read a distance file
                        if ((globaldata->gSparseMatrix == NULL) || (globaldata->gListVector == NULL)) {
-                               mothurOut("Before you use the cluster command, you first need to read in a distance matrix."); mothurOutEndLine();
+                               m->mothurOut("Before you use the cluster command, you first need to read in a distance matrix."); m->mothurOutEndLine();
                                abort = true;
                        } 
                
@@ -55,16 +112,19 @@ ClusterCommand::ClusterCommand(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"; }
                        
-                       if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
-                       else { mothurOut("Not a valid clustering method.  Valid clustering algorithms are furthest, nearest or average."); mothurOutEndLine(); abort = true; }
+                       if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { }
+                       else { m->mothurOut("Not a valid clustering method.  Valid clustering algorithms are furthest, nearest, average, and weighted."); m->mothurOutEndLine(); abort = true; }
 
                        showabund = validParameter.validFile(parameters, "showabund", false);
                        if (showabund == "not found") { showabund = "T"; }
@@ -87,19 +147,24 @@ ClusterCommand::ClusterCommand(string option){
                                if (method == "furthest")       {       cluster = new CompleteLinkage(rabund, list, matrix, cutoff, method); }
                                else if(method == "nearest"){   cluster = new SingleLinkage(rabund, list, matrix, cutoff, method); }
                                else if(method == "average"){   cluster = new AverageLinkage(rabund, list, matrix, cutoff, method);     }
+                               else if(method == "weighted"){  cluster = new WeightedLinkage(rabund, list, matrix, cutoff, method);    }
                                tag = cluster->getTag();
                                
-                               if (outputDir == "") { outputDir += hasPath(globaldata->inputFileName); }
-                               fileroot = outputDir + getRootName(getSimpleName(globaldata->inputFileName));
+                               if (outputDir == "") { outputDir += m->hasPath(globaldata->inputFileName); }
+                               fileroot = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName));
                        
-                               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"); 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");
                        }
                }
        }
        catch(exception& e) {
-               errorOut(e, "ClusterCommand", "ClusterCommand");
+               m->errorOut(e, "ClusterCommand", "ClusterCommand");
                exit(1);
        }
 }
@@ -108,14 +173,14 @@ ClusterCommand::ClusterCommand(string option){
 
 void ClusterCommand::help(){
        try {
-               mothurOut("The cluster command can only be executed after a successful read.dist command.\n");
-               mothurOut("The cluster command parameter options are method, cuttoff, precision, showabund and timing. No parameters are required.\n");
-               mothurOut("The cluster command should be in the following format: \n");
-               mothurOut("cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
-               mothurOut("The acceptable cluster methods are furthest, nearest and average.  If no method is provided then furthest is assumed.\n\n"); 
+               m->mothurOut("The cluster command can only be executed after a successful read.dist command.\n");
+               m->mothurOut("The cluster command parameter options are method, cuttoff, hard, precision, showabund and timing. No parameters are required.\n");
+               m->mothurOut("The cluster command should be in the following format: \n");
+               m->mothurOut("cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
+               m->mothurOut("The acceptable cluster methods are furthest, nearest and average.  If no method is provided then furthest is assumed.\n\n");      
        }
        catch(exception& e) {
-               errorOut(e, "ClusterCommand", "help");
+               m->errorOut(e, "ClusterCommand", "help");
                exit(1);
        }
 }
@@ -146,11 +211,23 @@ int ClusterCommand::execute(){
                print_start = true;
                start = time(NULL);
                loops = 0;
+               double saveCutoff = cutoff;
                
                while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
-                       if (print_start && isTrue(timing)) {
-                               mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/" 
-                                       + toString(roundDist(matrix->getSmallDist(), precision)) 
+               
+                       if (m->control_pressed) { //clean up
+                               delete globaldata->gSparseMatrix;  globaldata->gSparseMatrix = NULL;
+                               delete globaldata->gListVector;  globaldata->gListVector = NULL;
+                               if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
+                               else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
+                               sabundFile.close();rabundFile.close();listFile.close();
+                               for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         } outputTypes.clear();
+                               return 0;
+                       }
+               
+                       if (print_start && m->isTrue(timing)) {
+                               m->mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/" 
+                                       + toString(m->roundDist(matrix->getSmallDist(), precision)) 
                                        + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
                                cout.flush();
                                print_start = false;
@@ -159,8 +236,14 @@ int ClusterCommand::execute(){
                        loops++;
 
                        cluster->update(cutoff);
+       
                        float dist = matrix->getSmallDist();
-                       float rndDist = roundDist(dist, precision);
+                       float rndDist;
+                       if (hard) {
+                               rndDist = m->ceilDist(dist, precision); 
+                       }else{
+                               rndDist = m->roundDist(dist, precision); 
+                       }
 
                        if(previousDist <= 0.0000 && dist != previousDist){
                                printData("unique");
@@ -175,8 +258,8 @@ int ClusterCommand::execute(){
                        oldList = *list;
                }
 
-               if (print_start && isTrue(timing)) {
-                       mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist) 
+               if (print_start && m->isTrue(timing)) {
+                       m->mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist) 
                                         + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
                        cout.flush();
                        print_start = false;
@@ -192,7 +275,7 @@ int ClusterCommand::execute(){
                //delete globaldata's copy of the sparsematrix and listvector to free up memory
                delete globaldata->gSparseMatrix;  globaldata->gSparseMatrix = NULL;
                delete globaldata->gListVector;  globaldata->gListVector = NULL;
-               
+       
                //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
                if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
                else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
@@ -204,13 +287,29 @@ int ClusterCommand::execute(){
                sabundFile.close();
                rabundFile.close();
                listFile.close();
-               //if (isTrue(timing)) {
-                       mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); mothurOutEndLine();
+       
+               if (saveCutoff != cutoff) { 
+                       if (hard)       {  saveCutoff = m->ceilDist(saveCutoff, precision);     }
+                       else            {       saveCutoff = m->roundDist(saveCutoff, precision);  }
+
+                       m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine(); 
+               }
+               
+               m->mothurOutEndLine();
+               m->mothurOut("Output File Names: "); m->mothurOutEndLine();
+               for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
+               m->mothurOutEndLine();
+
+               
+               //if (m->isTrue(timing)) {
+                       m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
                //}
+               
+               
                return 0;
        }
        catch(exception& e) {
-               errorOut(e, "ClusterCommand", "execute");
+               m->errorOut(e, "ClusterCommand", "execute");
                exit(1);
        }
 }
@@ -219,16 +318,16 @@ int ClusterCommand::execute(){
 
 void ClusterCommand::printData(string label){
        try {
-               if (isTrue(timing)) {
-                       mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins()) 
-                    + "\tclusters. Updates: " + toString(loops)); mothurOutEndLine();
+               if (m->isTrue(timing)) {
+                       m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins()) 
+                    + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
                }
                print_start = true;
                loops = 0;
                start = time(NULL);
 
                oldRAbund.setLabel(label);
-               if (isTrue(showabund)) {
+               if (m->isTrue(showabund)) {
                        oldRAbund.getSAbundVector().print(cout);
                }
                oldRAbund.print(rabundFile);
@@ -238,7 +337,7 @@ void ClusterCommand::printData(string label){
                oldList.print(listFile);
        }
        catch(exception& e) {
-               errorOut(e, "ClusterCommand", "printData");
+               m->errorOut(e, "ClusterCommand", "printData");
                exit(1);
        }