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();
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
24 //valid paramters for this command
25 string Array[] = {"cutoff","precision","method","showabund","timing","outputdir","inputdir"};
26 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28 OptionParser parser(option);
29 map<string,string> parameters = parser.getParameters();
31 ValidParameters validParameter;
33 //check to make sure all parameters are valid for command
34 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
35 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
40 //if the user changes the output directory command factory will send this info to us in the output parameter
41 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
43 //error checking to make sure they read a distance file
44 if ((globaldata->gSparseMatrix == NULL) || (globaldata->gListVector == NULL)) {
45 m->mothurOut("Before you use the cluster command, you first need to read in a distance matrix."); m->mothurOutEndLine();
49 //check for optional parameter and set defaults
50 // ...at some point should added some additional type checking...
51 //get user cutoff and precision or use defaults
53 temp = validParameter.validFile(parameters, "precision", false);
54 if (temp == "not found") { temp = "100"; }
55 //saves precision legnth for formatting below
56 length = temp.length();
57 convert(temp, precision);
59 temp = validParameter.validFile(parameters, "cutoff", false);
60 if (temp == "not found") { temp = "10"; }
61 convert(temp, cutoff);
62 cutoff += (5 / (precision * 10.0));
64 method = validParameter.validFile(parameters, "method", false);
65 if (method == "not found") { method = "furthest"; }
67 if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
68 else { m->mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average."); m->mothurOutEndLine(); abort = true; }
70 showabund = validParameter.validFile(parameters, "showabund", false);
71 if (showabund == "not found") { showabund = "T"; }
73 timing = validParameter.validFile(parameters, "timing", false);
74 if (timing == "not found") { timing = "F"; }
79 //get matrix, list and rabund for execute
80 if(globaldata->gSparseMatrix != NULL) { matrix = globaldata->gSparseMatrix; }
82 if(globaldata->gListVector != NULL){
83 list = globaldata->gListVector;
84 rabund = new RAbundVector(list->getRAbundVector());
88 if (method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix, cutoff, method); }
89 else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix, cutoff, method); }
90 else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix, cutoff, method); }
91 tag = cluster->getTag();
93 if (outputDir == "") { outputDir += hasPath(globaldata->inputFileName); }
94 fileroot = outputDir + getRootName(getSimpleName(globaldata->inputFileName));
96 openOutputFile(fileroot+ tag + ".sabund", sabundFile);
97 openOutputFile(fileroot+ tag + ".rabund", rabundFile);
98 openOutputFile(fileroot+ tag + ".list", listFile);
100 outputNames.push_back(fileroot+ tag + ".sabund");
101 outputNames.push_back(fileroot+ tag + ".rabund");
102 outputNames.push_back(fileroot+ tag + ".list");
106 catch(exception& e) {
107 m->errorOut(e, "ClusterCommand", "ClusterCommand");
112 //**********************************************************************************************************************
114 void ClusterCommand::help(){
116 m->mothurOut("The cluster command can only be executed after a successful read.dist command.\n");
117 m->mothurOut("The cluster command parameter options are method, cuttoff, precision, showabund and timing. No parameters are required.\n");
118 m->mothurOut("The cluster command should be in the following format: \n");
119 m->mothurOut("cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
120 m->mothurOut("The acceptable cluster methods are furthest, nearest and average. If no method is provided then furthest is assumed.\n\n");
122 catch(exception& e) {
123 m->errorOut(e, "ClusterCommand", "help");
128 //**********************************************************************************************************************
130 ClusterCommand::~ClusterCommand(){
131 if (abort == false) {
137 //**********************************************************************************************************************
139 int ClusterCommand::execute(){
142 if (abort == true) { return 0; }
144 time_t estart = time(NULL);
145 //int ndist = matrix->getNNodes();
146 float previousDist = 0.00000;
147 float rndPreviousDist = 0.00000;
154 double saveCutoff = cutoff;
156 while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
157 if (print_start && isTrue(timing)) {
158 m->mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/"
159 + toString(roundDist(matrix->getSmallDist(), precision))
160 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
167 cluster->update(cutoff);
168 float dist = matrix->getSmallDist();
169 float rndDist = roundDist(dist, precision);
171 if(previousDist <= 0.0000 && dist != previousDist){
174 else if(rndDist != rndPreviousDist){
175 printData(toString(rndPreviousDist, length-1));
179 rndPreviousDist = rndDist;
184 if (print_start && isTrue(timing)) {
185 m->mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist)
186 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
191 if(previousDist <= 0.0000){
194 else if(rndPreviousDist<cutoff){
195 printData(toString(rndPreviousDist, length-1));
198 //delete globaldata's copy of the sparsematrix and listvector to free up memory
199 delete globaldata->gSparseMatrix; globaldata->gSparseMatrix = NULL;
200 delete globaldata->gListVector; globaldata->gListVector = NULL;
202 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
203 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
204 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
206 globaldata->setListFile(fileroot+ tag + ".list");
207 globaldata->setNameFile("");
208 globaldata->setFormat("list");
214 if (saveCutoff != cutoff) { m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine(); }
216 m->mothurOutEndLine();
217 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
218 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
219 m->mothurOutEndLine();
222 //if (isTrue(timing)) {
223 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
229 catch(exception& e) {
230 m->errorOut(e, "ClusterCommand", "execute");
235 //**********************************************************************************************************************
237 void ClusterCommand::printData(string label){
239 if (isTrue(timing)) {
240 m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
241 + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
247 oldRAbund.setLabel(label);
248 if (isTrue(showabund)) {
249 oldRAbund.getSAbundVector().print(cout);
251 oldRAbund.print(rabundFile);
252 oldRAbund.getSAbundVector().print(sabundFile);
254 oldList.setLabel(label);
255 oldList.print(listFile);
257 catch(exception& e) {
258 m->errorOut(e, "ClusterCommand", "printData");
264 //**********************************************************************************************************************