]> git.donarmstrong.com Git - mothur.git/blobdiff - hclustercommand.cpp
added getCommandInfoCommand for gui
[mothur.git] / hclustercommand.cpp
index dc14e1670c5221599dd14929dd6d08fde6c6b766..503bc48273069f829103788ce2711bc12d56ea42 100644 (file)
@@ -9,15 +9,71 @@
 
 #include "hclustercommand.h"
 
+//**********************************************************************************************************************
+vector<string> HClusterCommand::setParameters(){       
+       try {
+               CommandParameter pphylip("phylip", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "none",false,false); parameters.push_back(pphylip);
+               CommandParameter pname("name", "InputTypes", "", "", "none", "none", "ColumnName",false,false); parameters.push_back(pname);
+               CommandParameter pcolumn("column", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "ColumnName",false,false); parameters.push_back(pcolumn);
+               CommandParameter pcutoff("cutoff", "Number", "", "10", "", "", "",false,false); parameters.push_back(pcutoff);
+               CommandParameter pprecision("precision", "Number", "", "100", "", "", "",false,false); parameters.push_back(pprecision);
+               CommandParameter pmethod("method", "Multiple", "furthest-nearest-average-weighted", "furthest", "", "", "",false,false); parameters.push_back(pmethod);
+               CommandParameter phard("hard", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(phard);
+               CommandParameter psorted("sorted", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(psorted);
+               CommandParameter pshowabund("showabund", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pshowabund);
+               CommandParameter ptiming("timing", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(ptiming);          
+               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, "HClusterCommand", "setParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+string HClusterCommand::getHelpString(){       
+       try {
+               string helpString = "";
+               helpString += "The hcluster command parameter options are cutoff, precision, method, phylip, column, name, showabund, timing and sorted. Phylip or column and name are required, unless you have valid current files.\n";
+               helpString += "The phylip and column parameter allow you to enter your distance file, and sorted indicates whether your column distance file is already sorted. \n";
+               helpString += "The name parameter allows you to enter your name file and is required if your distance file is in column format. \n";
+               helpString += "The hcluster command should be in the following format: \n";
+               helpString += "hcluster(column=youDistanceFile, name=yourNameFile, method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n";
+               helpString += "The acceptable hcluster methods are furthest, nearest, weighted and average.\n"; 
+               return helpString;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "HClusterCommand", "getHelpString");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+HClusterCommand::HClusterCommand(){    
+       try {
+               abort = true; calledHelp = true; 
+               setParameters();
+               vector<string> tempOutNames;
+               outputTypes["list"] = tempOutNames;
+               outputTypes["rabund"] = tempOutNames;
+               outputTypes["sabund"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "HClusterCommand", "HClusterCommand");
+               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)  {
        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
@@ -37,8 +93,12 @@ HClusterCommand::HClusterCommand(string option)  {
                                }
                        }
                        
-                       globaldata->newRead();
-                       
+                       //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 = "";          }
@@ -47,7 +107,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 +115,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 +123,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;             }
                                }
@@ -87,11 +147,31 @@ HClusterCommand::HClusterCommand(string option)  {
                        if (namefile == "not open") { abort = true; }   
                        else if (namefile == "not found") { namefile = ""; }
                        
-                       if ((phylipfile == "") && (columnfile == "")) { m->mothurOut("When executing a hcluster command you must enter a phylip or a column."); m->mothurOutEndLine(); abort = true; }
+                       if ((phylipfile == "") && (columnfile == "")) { 
+                               //is there are current file available for either of these?
+                               //give priority to column, then phylip
+                               columnfile = m->getColumnFile(); 
+                               if (columnfile != "") {  m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
+                               else { 
+                                       phylipfile = m->getPhylipFile(); 
+                                       if (phylipfile != "") {  m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
+                                       else { 
+                                               m->mothurOut("No valid current files. You must provide a phylip or column file before you can use the hcluster command."); m->mothurOutEndLine(); 
+                                               abort = true;
+                                       }
+                               }
+                       }
                        else if ((phylipfile != "") && (columnfile != "")) { m->mothurOut("When executing a hcluster command you must enter ONLY ONE of the following: phylip or column."); m->mothurOutEndLine(); abort = true; }
                
                        if (columnfile != "") {
-                               if (namefile == "") {  cout << "You need to provide a namefile if you are going to use the column format." << endl; abort = true; }
+                               if (namefile == "") { 
+                                       namefile = m->getNameFile(); 
+                                       if (namefile != "") {  m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
+                                       else { 
+                                               m->mothurOut("You need to provide a namefile if you are going to use the column format."); m->mothurOutEndLine(); 
+                                               abort = true; 
+                                       }       
+                               }
                        }
                        
                        //check for optional parameter and set defaults
@@ -105,25 +185,25 @@ HClusterCommand::HClusterCommand(string option)  {
                        convert(temp, precision); 
                        
                        temp = validParameter.validFile(parameters, "hard", false);                     if (temp == "not found") { temp = "F"; }
-                       hard = isTrue(temp);
+                       hard = m->isTrue(temp);
                        
                        temp = validParameter.validFile(parameters, "cutoff", false);
                        if (temp == "not found") { temp = "10"; }
                        convert(temp, cutoff); 
-                       if (!hard) {  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 { m->mothurOut("Not a valid clustering method.  Valid clustering algorithms are furthest, nearest or average."); m->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 or weighted."); m->mothurOutEndLine(); abort = true; }
 
                        showabund = validParameter.validFile(parameters, "showabund", false);
                        if (showabund == "not found") { showabund = "T"; }
                        
                        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"; }
@@ -131,20 +211,21 @@ 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 if (method == "weighted")  { tag = "wn";  }
                                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");
                        }
                }
        }
@@ -156,52 +237,30 @@ HClusterCommand::HClusterCommand(string option)  {
 
 //**********************************************************************************************************************
 
-void HClusterCommand::help(){
-       try {
-               m->mothurOut("The hcluster command parameter options are cutoff, precision, method, phylip, column, name, showabund, timing and sorted. Phylip or column and name are required.\n");
-               m->mothurOut("The phylip and column parameter allow you to enter your distance file, and sorted indicates whether your column distance file is already sorted. \n");
-               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");        
-       }
-       catch(exception& e) {
-               m->errorOut(e, "HClusterCommand", "help");
-               exit(1);
-       }
-}
-
-//**********************************************************************************************************************
-
-HClusterCommand::~HClusterCommand(){}
-
-//**********************************************************************************************************************
-
 int HClusterCommand::execute(){
        try {
        
-               if (abort == true) {    return 0;       }
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
+               NameAssignment* nameMap = NULL;
                if(namefile != ""){     
-                       globaldata->nameMap = new NameAssignment(namefile);
-                       globaldata->nameMap->readMap();
-               }else{
-                       globaldata->nameMap = NULL;
-               }
+                       nameMap = new NameAssignment(namefile);
+                       nameMap->readMap();
+               }               
                
                time_t estart = time(NULL);
                
                if (!sorted) {
                        read = new ReadCluster(distfile, cutoff, outputDir, true);      
                        read->setFormat(format);
-                       read->read(globaldata->nameMap);
+                       read->read(nameMap);
                        
                        if (m->control_pressed) {  
                                delete read; 
                                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;  
                        }
                        
@@ -210,14 +269,14 @@ int HClusterCommand::execute(){
                        list = read->getListVector();
                        delete read;
                }else {
-                       list = new ListVector(globaldata->nameMap->getListVector());
+                       list = new ListVector(nameMap->getListVector());
                }
                
                if (m->control_pressed) {  
                        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;  
                }
 
@@ -239,7 +298,7 @@ int HClusterCommand::execute(){
                print_start = true;
                start = time(NULL);
                                
-               cluster = new HCluster(rabund, list, method, distfile, globaldata->nameMap, cutoff);
+               cluster = new HCluster(rabund, list, method, distfile, nameMap, cutoff);
                vector<seqDist> seqs; seqs.resize(1); // to start loop
                
                if (m->control_pressed) {  
@@ -247,7 +306,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;  
                }
 
@@ -261,7 +320,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;  
                        }
 
@@ -275,12 +334,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");
@@ -302,7 +367,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;  
                }
                                        
@@ -312,28 +377,36 @@ int HClusterCommand::execute(){
                else if(rndPreviousDist<cutoff){
                        printData(toString(rndPreviousDist, length-1));
                }
-               
-               //delete globaldata's copy of the sparsematrix and listvector to free up memory
-               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(""); }
-               
-               globaldata->setListFile(fileroot+ tag + ".list");
-               globaldata->setNameFile("");
-               globaldata->setFormat("list");
-               
+                               
                sabundFile.close();
                rabundFile.close();
                listFile.close();
                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;  
                }
-
+               
+               //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->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
@@ -354,7 +427,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();
                }
@@ -363,7 +436,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);