X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=clustercommand.cpp;h=6043d39be8d559010e156111f41a856b716cda40;hb=191ae1be0679d5cf4eda950b3b1bf26fb7dd503d;hp=f0ef51f05b846797a42c8e71cacce326db06f4bb;hpb=8f92612d5a69f5245e63a20657e7d93519c0769e;p=mothur.git diff --git a/clustercommand.cpp b/clustercommand.cpp index f0ef51f..6043d39 100644 --- a/clustercommand.cpp +++ b/clustercommand.cpp @@ -9,83 +9,241 @@ #include "clustercommand.h" +//********************************************************************************************************************** +vector ClusterCommand::getValidParameters(){ + try { + string AlignArray[] = {"cutoff","precision","method","showabund","timing","hard","outputdir","inputdir"}; + vector 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 tempOutNames; + outputTypes["list"] = tempOutNames; + outputTypes["rabund"] = tempOutNames; + outputTypes["sabund"] = tempOutNames; + } + catch(exception& e) { + m->errorOut(e, "ClusterCommand", "ClusterCommand"); + exit(1); + } +} +//********************************************************************************************************************** +vector ClusterCommand::getRequiredParameters(){ + try { + vector myArray; + return myArray; + } + catch(exception& e) { + m->errorOut(e, "ClusterCommand", "getRequiredParameters"); + exit(1); + } +} +//********************************************************************************************************************** +vector ClusterCommand::getRequiredFiles(){ + try { + string Array[] = {"phylip","column","or"}; + vector 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(){ +ClusterCommand::ClusterCommand(string option) { try{ globaldata = GlobalData::getInstance(); - - if(globaldata->getSparseMatrix() != NULL) { matrix = new SparseMatrix(*globaldata->getSparseMatrix()); } - // Not sure if we want the address or an entire new memory allocation. Might be nice to have new memory so data - // doesn't need to be re-read, but then again, it could suck up a ton of memory. Dunno. - // if(globaldata->getSparseMatrix() != NULL) { matrix = globaldata->getSparseMatrix(); } - - if(globaldata->getListVector() != NULL){ - list = new ListVector(*globaldata->getListVector()); - rabund = new RAbundVector(list->getRAbundVector()); - //rabund->print(cout); - } - - if(globaldata->getMethod() != "") { method = globaldata->getMethod(); } - //if no method given use furthest, initialized in globaldata - if(method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix); tag = "fn"; } - else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix); tag = "nn"; } - else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix); tag = "an"; } - else { cout << "error - not recognized method" << endl; } - - if(globaldata->getPrecision() != ""){ - convert(globaldata->getPrecision(), precision); - } - //saves precision legnth for formatting below - length = globaldata->getPrecision().length(); + abort = false; - if(globaldata->getCutOff() != ""){ - convert(globaldata->getCutOff(), cutoff); - cutoff += (5 / (precision * 10.0)); - } - - fileroot = getRootName(globaldata->getFileRoot()); + //allow user to run help + if(option == "help") { help(); abort = true; } + + else { + //valid paramters for this command + string Array[] = {"cutoff","precision","method","showabund","timing","hard","outputdir","inputdir"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + + OptionParser parser(option); + map parameters = parser.getParameters(); + + ValidParameters validParameter; + + //check to make sure all parameters are valid for command + for (map::iterator it = parameters.begin(); it != parameters.end(); it++) { + if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { + abort = true; + } + } + + //initialize outputTypes + vector 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)) { + m->mothurOut("Before you use the cluster command, you first need to read in a distance matrix."); m->mothurOutEndLine(); + abort = true; + } - openOutputFile(fileroot+ tag + ".sabund", sabundFile); - openOutputFile(fileroot+ tag + ".rabund", rabundFile); - openOutputFile(fileroot+ tag + ".list", listFile); + //check for optional parameter and set defaults + // ...at some point should added some additional type checking... + //get user cutoff and precision or use defaults + string temp; + temp = validParameter.validFile(parameters, "precision", false); + if (temp == "not found") { temp = "100"; } + //saves precision legnth for formatting below + 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)); + + method = validParameter.validFile(parameters, "method", false); + if (method == "not found") { method = "furthest"; } + + 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"; } + + 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) { - cout << "Standard Error: " << e.what() << " has occurred in the ClusterCommand class Function ClusterCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "ClusterCommand", "ClusterCommand"); exit(1); } - catch(...) { - cout << "An unknown error has occurred in the ClusterCommand class function ClusterCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; +} + +//********************************************************************************************************************** + +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(){ - delete cluster; - delete matrix; - delete list; - delete rabund; + if (abort == false) { + delete cluster; + delete rabund; + } } //********************************************************************************************************************** int ClusterCommand::execute(){ try { + + if (abort == true) { return 0; } + + time_t estart = time(NULL); + //int ndist = matrix->getNNodes(); float previousDist = 0.00000; float rndPreviousDist = 0.00000; oldRAbund = *rabund; oldList = *list; + + print_start = true; + start = time(NULL); + loops = 0; + double saveCutoff = cutoff; + + 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(""); } + sabundFile.close();rabundFile.close();listFile.close(); + for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear(); + return 0; + } - float x; - x=0.1; - toString(x, 2); + 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; + } + + loops++; + + cluster->update(cutoff); - while(matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){ - cluster->update(); 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"); @@ -99,6 +257,13 @@ int ClusterCommand::execute(){ oldRAbund = *rabund; oldList = *list; } + + 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; + } if(previousDist <= 0.0000){ printData("unique"); @@ -108,12 +273,9 @@ int ClusterCommand::execute(){ } //delete globaldata's copy of the sparsematrix and listvector to free up memory - SparseMatrix* clearM = NULL; - globaldata->setSparseMatrix(clearM); - ListVector* clearL = NULL; - globaldata->setListVector(clearL); - - + 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(""); } @@ -122,25 +284,52 @@ int ClusterCommand::execute(){ globaldata->setNameFile(""); globaldata->setFormat("list"); + sabundFile.close(); + rabundFile.close(); + listFile.close(); + + 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) { - cout << "Standard Error: " << e.what() << " has occurred in the ClusterCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the ClusterCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "ClusterCommand", "execute"); exit(1); } - } //********************************************************************************************************************** void ClusterCommand::printData(string label){ try { + 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); - oldRAbund.getSAbundVector().print(cout); + if (m->isTrue(showabund)) { + oldRAbund.getSAbundVector().print(cout); + } oldRAbund.print(rabundFile); oldRAbund.getSAbundVector().print(sabundFile); @@ -148,13 +337,10 @@ void ClusterCommand::printData(string label){ oldList.print(listFile); } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the ClusterCommand class Function printData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the ClusterCommand class function printData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "ClusterCommand", "printData"); exit(1); } + } //**********************************************************************************************************************