]> git.donarmstrong.com Git - mothur.git/blobdiff - clustercommand.cpp
removed read.dist, read.otu, read.tree and globaldata. added current to defaults...
[mothur.git] / clustercommand.cpp
index 391b20a9de857755854401f96869c774508bd0af..9b0fcfd928cb5db6a6df12fb77a0583cc64cc4fb 100644 (file)
@@ -8,53 +8,62 @@
  */
 
 #include "clustercommand.h"
+#include "readphylip.h"
+#include "readcolumn.h"
+#include "readmatrix.hpp"
 
 //**********************************************************************************************************************
-vector<string> ClusterCommand::getValidParameters(){   
+vector<string> ClusterCommand::setParameters(){        
        try {
-               string AlignArray[] =  {"cutoff","precision","method","showabund","timing","hard","outputdir","inputdir"};
-               vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
+               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 pshowabund("showabund", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pshowabund);
+               CommandParameter ptiming("timing", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(ptiming);
+               CommandParameter psim("sim", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(psim);
+               CommandParameter phard("hard", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(phard);
+               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, "ClusterCommand", "getValidParameters");
+               m->errorOut(e, "ClusterCommand", "setParameters");
                exit(1);
        }
 }
 //**********************************************************************************************************************
-ClusterCommand::ClusterCommand(){      
+string ClusterCommand::getHelpString(){        
        try {
-               abort = true; calledHelp = true; 
-               vector<string> tempOutNames;
-               outputTypes["list"] = tempOutNames;
-               outputTypes["rabund"] = tempOutNames;
-               outputTypes["sabund"] = tempOutNames;
+               string helpString = "";
+               helpString += "The cluster command parameter options are phylip, column, name, method, cuttoff, hard, precision, sim, showabund and timing. Phylip or column and name are required, unless you have a valid current file.\n";
+               helpString += "The cluster command should be in the following format: \n";
+               helpString += "cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n";
+               helpString += "The acceptable cluster methods are furthest, nearest, average and weighted.  If no method is provided then furthest is assumed.\n\n";    
+               return helpString;
        }
        catch(exception& e) {
-               m->errorOut(e, "ClusterCommand", "ClusterCommand");
+               m->errorOut(e, "ClusterCommand", "getHelpString");
                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(){     
+ClusterCommand::ClusterCommand(){      
        try {
-               string Array[] =  {"phylip","column","or"};
-               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
-               return myArray;
+               abort = true; calledHelp = true; 
+               setParameters();
+               vector<string> tempOutNames;
+               outputTypes["list"] = tempOutNames;
+               outputTypes["rabund"] = tempOutNames;
+               outputTypes["sabund"] = tempOutNames;
        }
        catch(exception& e) {
-               m->errorOut(e, "ClusterCommand", "getRequiredFiles");
+               m->errorOut(e, "ClusterCommand", "ClusterCommand");
                exit(1);
        }
 }
@@ -62,25 +71,22 @@ vector<string> ClusterCommand::getRequiredFiles(){
 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
 ClusterCommand::ClusterCommand(string option)  {
        try{
-               globaldata = GlobalData::getInstance();
-               
                abort = false; calledHelp = false;   
                
                //allow user to run help
                if(option == "help") { help(); abort = true; calledHelp = true; }
                
                else {
-                       //valid paramters for this command
-                       string Array[] =  {"cutoff","precision","method","showabund","timing","hard","outputdir","inputdir"};
-                       vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+                       vector<string> myArray = setParameters();
                        
                        OptionParser parser(option);
                        map<string,string> parameters = parser.getParameters();
+                       map<string,string>::iterator it;
                        
                        ValidParameters validParameter;
                
                        //check to make sure all parameters are valid for command
-                       for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
+                       for (it = parameters.begin(); it != parameters.end(); it++) { 
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
                                        abort = true;
                                }
@@ -95,12 +101,77 @@ ClusterCommand::ClusterCommand(string option)  {
                        //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)) {
-                               m->mothurOut("Before you use the cluster command, you first need to read in a distance matrix."); m->mothurOutEndLine();
-                               abort = true;
-                       } 
-               
+                       string inputDir = validParameter.validFile(parameters, "inputdir", false);              
+                       if (inputDir == "not found"){   inputDir = "";          }
+                       else {
+                               string path;
+                               it = parameters.find("phylip");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = m->hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
+                               }
+                               
+                               it = parameters.find("column");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = m->hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["column"] = inputDir + it->second;           }
+                               }
+                               
+                               it = parameters.find("name");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = m->hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["name"] = inputDir + it->second;             }
+                               }
+                       }
+                       
+                       //check for required parameters
+                       phylipfile = validParameter.validFile(parameters, "phylip", true);
+                       if (phylipfile == "not open") { phylipfile = ""; abort = true; }
+                       else if (phylipfile == "not found") { phylipfile = ""; }        
+                       else {  distfile = phylipfile;  format = "phylip";      }
+                       
+                       columnfile = validParameter.validFile(parameters, "column", true);
+                       if (columnfile == "not open") { columnfile = ""; abort = true; }        
+                       else if (columnfile == "not found") { columnfile = ""; }
+                       else {  distfile = columnfile; format = "column";       }
+                       
+                       namefile = validParameter.validFile(parameters, "name", true);
+                       if (namefile == "not open") { abort = true; }   
+                       else if (namefile == "not found") { namefile = ""; }
+                       
+                       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 cluster command."); m->mothurOutEndLine(); 
+                                               abort = true;
+                                       }
+                               }
+                       }
+                       else if ((phylipfile != "") && (columnfile != "")) { m->mothurOut("When executing a cluster command you must enter ONLY ONE of the following: phylip or column."); m->mothurOutEndLine(); abort = true; }
+                       
+                       if (columnfile != "") {
+                               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
                        // ...at some point should added some additional type checking...
                        //get user cutoff and precision or use defaults
@@ -114,6 +185,9 @@ ClusterCommand::ClusterCommand(string option)  {
                        temp = validParameter.validFile(parameters, "hard", false);                     if (temp == "not found") { temp = "F"; }
                        hard = m->isTrue(temp);
                        
+                       temp = validParameter.validFile(parameters, "sim", false);                              if (temp == "not found") { temp = "F"; }
+                       sim = m->isTrue(temp); 
+                       
                        temp = validParameter.validFile(parameters, "cutoff", false);
                        if (temp == "not found") { temp = "10"; }
                        convert(temp, cutoff); 
@@ -131,68 +205,15 @@ ClusterCommand::ClusterCommand(string option)  {
                        timing = validParameter.validFile(parameters, "timing", false);
                        if (timing == "not found") { timing = "F"; }
                        
-                       if (abort == false) {
-                       
-       
-                                                       //get matrix, list and rabund for execute
-                               if(globaldata->gSparseMatrix != NULL)   {       matrix = globaldata->gSparseMatrix;             }
-                       
-                               if(globaldata->gListVector != NULL){
-                                       list = globaldata->gListVector;
-                                       rabund = new RAbundVector(list->getRAbundVector());
-                               }
-                               
-                               //create cluster
-                               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 += m->hasPath(globaldata->inputFileName); }
-                               fileroot = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName));
-                       
-                               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) {
                m->errorOut(e, "ClusterCommand", "ClusterCommand");
                exit(1);
        }
 }
-
 //**********************************************************************************************************************
-
-void ClusterCommand::help(){
-       try {
-               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) {
-               m->errorOut(e, "ClusterCommand", "help");
-               exit(1);
-       }
-}
-
-//**********************************************************************************************************************
-
-ClusterCommand::~ClusterCommand(){
-       if (abort == false) {
-               delete cluster;
-               delete rabund;
-       }
-}
-
+ClusterCommand::~ClusterCommand(){}
 //**********************************************************************************************************************
 
 int ClusterCommand::execute(){
@@ -200,8 +221,51 @@ int ClusterCommand::execute(){
        
                if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
+               ReadMatrix* read;
+               if (format == "column") { read = new ReadColumnMatrix(columnfile, sim); }       //sim indicates whether its a similarity matrix
+               else if (format == "phylip") { read = new ReadPhylipMatrix(phylipfile, sim); }
+               
+               read->setCutoff(cutoff);
+               
+               NameAssignment* nameMap = NULL;
+               if(namefile != ""){     
+                       nameMap = new NameAssignment(namefile);
+                       nameMap->readMap();
+               }
+               
+               read->read(nameMap);
+               list = read->getListVector();
+               matrix = read->getMatrix();
+               rabund = new RAbundVector(list->getRAbundVector());
+               delete read;
+               
+               if (m->control_pressed) { //clean up
+                       delete list; delete matrix; delete rabund;
+                       sabundFile.close();rabundFile.close();listFile.close();
+                       for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         } outputTypes.clear();
+                       return 0;
+               }
+               
+               //create cluster
+               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 += m->hasPath(distfile); }
+               fileroot = outputDir + m->getRootName(m->getSimpleName(distfile));
+               
+               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");
+               
+               
                time_t estart = time(NULL);
-               //int ndist = matrix->getNNodes();
                float previousDist = 0.00000;
                float rndPreviousDist = 0.00000;
                oldRAbund = *rabund;
@@ -215,10 +279,7 @@ int ClusterCommand::execute(){
                while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
                
                        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(""); }
+                               delete list; delete matrix; delete rabund; delete cluster;
                                sabundFile.close();rabundFile.close();listFile.close();
                                for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         } outputTypes.clear();
                                return 0;
@@ -271,17 +332,10 @@ int ClusterCommand::execute(){
                        printData(toString(rndPreviousDist, length-1));
                }
                
-               //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(""); }
-               
-               globaldata->setListFile(fileroot+ tag + ".list");
-               globaldata->setNameFile("");
-               globaldata->setFormat("list");
+               delete matrix;
+               delete list;
+               delete rabund;
+               delete cluster;
                
                sabundFile.close();
                rabundFile.close();