X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=clustercommand.cpp;h=d14af108d69c36f98617fc2e133c3dcfb537c553;hb=2397df97b12cd5d21ea10dc4248c18a5803ddd41;hp=36fff2242ee65d3cdb1e72868d173c69ed5bc3ff;hpb=0470f6d037aacb3563c3f7010708120a4a67d4e6;p=mothur.git diff --git a/clustercommand.cpp b/clustercommand.cpp index 36fff22..d14af10 100644 --- a/clustercommand.cpp +++ b/clustercommand.cpp @@ -21,79 +21,85 @@ ClusterCommand::ClusterCommand(string option){ else { //valid paramters for this command - string Array[] = {"cutoff","precision","method"}; + string Array[] = {"cutoff","precision","method","showabund","timing","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); - parser = new OptionParser(); - parser->parse(option, parameters); delete parser; + OptionParser parser(option); + map parameters = parser.getParameters(); - ValidParameters* validParameter = new ValidParameters(); + ValidParameters validParameter; //check to make sure all parameters are valid for command - for (it = parameters.begin(); it != parameters.end(); it++) { - if (validParameter->isValidParameter(it->first, myArray, it->second) != true) { abort = true; } + for (map::iterator it = parameters.begin(); it != parameters.end(); it++) { + if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { + abort = true; + } } + //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)) { - cout << "Before you use the cluster command, you first need to read in a distance matrix." << endl; abort = true; + mothurOut("Before you use the cluster command, you first need to read in a distance matrix."); 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 string temp; - temp = validParameter->validFile(parameters, "precision", false); if (temp == "not found") { temp = "100"; } + 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, "cutoff", false); if (temp == "not found") { temp = "10"; } + 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"; } - - delete validParameter; + method = validParameter.validFile(parameters, "method", false); + if (method == "not found") { method = "furthest"; } if ((method == "furthest") || (method == "nearest") || (method == "average")) { } - else {cout << "Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average." << endl; abort = true; } + else { mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average."); 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 = new SparseMatrix(*globaldata->gSparseMatrix); } + + //get matrix, list and rabund for execute + if(globaldata->gSparseMatrix != NULL) { matrix = globaldata->gSparseMatrix; } if(globaldata->gListVector != NULL){ - list = new ListVector(*globaldata->gListVector); + list = globaldata->gListVector; rabund = new RAbundVector(list->getRAbundVector()); } //create cluster - 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; abort = true; } + 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); } + tag = cluster->getTag(); - fileroot = getRootName(globaldata->inputFileName); + if (outputDir == "") { outputDir += hasPath(globaldata->inputFileName); } + fileroot = outputDir + getRootName(getSimpleName(globaldata->inputFileName)); openOutputFile(fileroot+ tag + ".sabund", sabundFile); openOutputFile(fileroot+ tag + ".rabund", rabundFile); openOutputFile(fileroot+ tag + ".list", listFile); - - } - } - } 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"; - 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"; + errorOut(e, "ClusterCommand", "ClusterCommand"); exit(1); } } @@ -102,29 +108,25 @@ ClusterCommand::ClusterCommand(string option){ void ClusterCommand::help(){ try { - cout << "The cluster command can only be executed after a successful read.dist command." << "\n"; - cout << "The cluster command parameter options are method, cuttoff and precision. No parameters are required." << "\n"; - cout << "The cluster command should be in the following format: " << "\n"; - cout << "cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) " << "\n"; - cout << "The acceptable cluster methods are furthest, nearest and average. If no method is provided then furthest is assumed." << "\n" << "\n"; + 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"); } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the ClusterCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + errorOut(e, "ClusterCommand", "help"); exit(1); } - catch(...) { - cout << "An unknown error has occurred in the ClusterCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } } //********************************************************************************************************************** ClusterCommand::~ClusterCommand(){ - delete cluster; - delete matrix; - delete list; - delete rabund; + if (abort == false) { + delete cluster; + delete rabund; + } } //********************************************************************************************************************** @@ -134,17 +136,29 @@ int ClusterCommand::execute(){ 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; - float x; - x=0.1; - toString(x, 2); - - while(matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){ - cluster->update(); + while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){ + if (print_start && isTrue(timing)) { + mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/" + + toString(roundDist(matrix->getSmallDist(), precision)) + + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")"); + cout.flush(); + print_start = false; + } + + loops++; + + cluster->update(cutoff); float dist = matrix->getSmallDist(); float rndDist = roundDist(dist, precision); @@ -160,6 +174,13 @@ int ClusterCommand::execute(){ oldRAbund = *rabund; oldList = *list; } + + if (print_start && isTrue(timing)) { + 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"); @@ -180,25 +201,36 @@ int ClusterCommand::execute(){ globaldata->setNameFile(""); globaldata->setFormat("list"); + sabundFile.close(); + rabundFile.close(); + listFile.close(); + //if (isTrue(timing)) { + mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); 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"; + errorOut(e, "ClusterCommand", "execute"); exit(1); } - } //********************************************************************************************************************** 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(); + } + print_start = true; + loops = 0; + start = time(NULL); + oldRAbund.setLabel(label); - oldRAbund.getSAbundVector().print(cout); + if (isTrue(showabund)) { + oldRAbund.getSAbundVector().print(cout); + } oldRAbund.print(rabundFile); oldRAbund.getSAbundVector().print(sabundFile); @@ -206,13 +238,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"; + errorOut(e, "ClusterCommand", "printData"); exit(1); } + } //**********************************************************************************************************************