5 * Created by Sarah Westcott on 1/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "clustercommand.h"
12 //**********************************************************************************************************************
13 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
14 ClusterCommand::ClusterCommand(string option){
16 globaldata = GlobalData::getInstance();
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
23 //valid paramters for this command
24 string Array[] = {"cutoff","precision","method","showabund","timing","outputdir","inputdir"};
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string,string> parameters = parser.getParameters();
30 ValidParameters validParameter;
32 //check to make sure all parameters are valid for command
33 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
34 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
39 //if the user changes the output directory command factory will send this info to us in the output parameter
40 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
42 //error checking to make sure they read a distance file
43 if ((globaldata->gSparseMatrix == NULL) || (globaldata->gListVector == NULL)) {
44 mothurOut("Before you use the cluster command, you first need to read in a distance matrix."); mothurOutEndLine();
48 //check for optional parameter and set defaults
49 // ...at some point should added some additional type checking...
50 //get user cutoff and precision or use defaults
52 temp = validParameter.validFile(parameters, "precision", false);
53 if (temp == "not found") { temp = "100"; }
54 //saves precision legnth for formatting below
55 length = temp.length();
56 convert(temp, precision);
58 temp = validParameter.validFile(parameters, "cutoff", false);
59 if (temp == "not found") { temp = "10"; }
60 convert(temp, cutoff);
61 cutoff += (5 / (precision * 10.0));
63 method = validParameter.validFile(parameters, "method", false);
64 if (method == "not found") { method = "furthest"; }
66 if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
67 else { mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average."); mothurOutEndLine(); abort = true; }
69 showabund = validParameter.validFile(parameters, "showabund", false);
70 if (showabund == "not found") { showabund = "T"; }
72 timing = validParameter.validFile(parameters, "timing", false);
73 if (timing == "not found") { timing = "F"; }
78 //get matrix, list and rabund for execute
79 if(globaldata->gSparseMatrix != NULL) { matrix = globaldata->gSparseMatrix; }
81 if(globaldata->gListVector != NULL){
82 list = globaldata->gListVector;
83 rabund = new RAbundVector(list->getRAbundVector());
87 if (method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix, cutoff, method); }
88 else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix, cutoff, method); }
89 else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix, cutoff, method); }
90 tag = cluster->getTag();
92 if (outputDir == "") { outputDir += hasPath(globaldata->inputFileName); }
93 fileroot = outputDir + getRootName(getSimpleName(globaldata->inputFileName));
95 openOutputFile(fileroot+ tag + ".sabund", sabundFile);
96 openOutputFile(fileroot+ tag + ".rabund", rabundFile);
97 openOutputFile(fileroot+ tag + ".list", listFile);
101 catch(exception& e) {
102 errorOut(e, "ClusterCommand", "ClusterCommand");
107 //**********************************************************************************************************************
109 void ClusterCommand::help(){
111 mothurOut("The cluster command can only be executed after a successful read.dist command.\n");
112 mothurOut("The cluster command parameter options are method, cuttoff, precision, showabund and timing. No parameters are required.\n");
113 mothurOut("The cluster command should be in the following format: \n");
114 mothurOut("cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
115 mothurOut("The acceptable cluster methods are furthest, nearest and average. If no method is provided then furthest is assumed.\n\n");
117 catch(exception& e) {
118 errorOut(e, "ClusterCommand", "help");
123 //**********************************************************************************************************************
125 ClusterCommand::~ClusterCommand(){
126 if (abort == false) {
132 //**********************************************************************************************************************
134 int ClusterCommand::execute(){
137 if (abort == true) { return 0; }
139 time_t estart = time(NULL);
140 //int ndist = matrix->getNNodes();
141 float previousDist = 0.00000;
142 float rndPreviousDist = 0.00000;
150 while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
151 if (print_start && isTrue(timing)) {
152 mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/"
153 + toString(roundDist(matrix->getSmallDist(), precision))
154 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
161 cluster->update(cutoff);
162 float dist = matrix->getSmallDist();
163 float rndDist = roundDist(dist, precision);
165 if(previousDist <= 0.0000 && dist != previousDist){
168 else if(rndDist != rndPreviousDist){
169 printData(toString(rndPreviousDist, length-1));
173 rndPreviousDist = rndDist;
178 if (print_start && isTrue(timing)) {
179 mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist)
180 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
185 if(previousDist <= 0.0000){
188 else if(rndPreviousDist<cutoff){
189 printData(toString(rndPreviousDist, length-1));
192 //delete globaldata's copy of the sparsematrix and listvector to free up memory
193 delete globaldata->gSparseMatrix; globaldata->gSparseMatrix = NULL;
194 delete globaldata->gListVector; globaldata->gListVector = NULL;
196 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
197 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
198 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
200 globaldata->setListFile(fileroot+ tag + ".list");
201 globaldata->setNameFile("");
202 globaldata->setFormat("list");
207 //if (isTrue(timing)) {
208 mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); mothurOutEndLine();
212 catch(exception& e) {
213 errorOut(e, "ClusterCommand", "execute");
218 //**********************************************************************************************************************
220 void ClusterCommand::printData(string label){
222 if (isTrue(timing)) {
223 mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
224 + "\tclusters. Updates: " + toString(loops)); mothurOutEndLine();
230 oldRAbund.setLabel(label);
231 if (isTrue(showabund)) {
232 oldRAbund.getSAbundVector().print(cout);
234 oldRAbund.print(rabundFile);
235 oldRAbund.getSAbundVector().print(sabundFile);
237 oldList.setLabel(label);
238 oldList.print(listFile);
240 catch(exception& e) {
241 errorOut(e, "ClusterCommand", "printData");
247 //**********************************************************************************************************************