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","hard","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, "hard", false); if (temp == "not found") { temp = "F"; }
62 temp = validParameter.validFile(parameters, "cutoff", false);
63 if (temp == "not found") { temp = "10"; }
64 convert(temp, cutoff);
65 cutoff += (5 / (precision * 10.0));
67 method = validParameter.validFile(parameters, "method", false);
68 if (method == "not found") { method = "furthest"; }
70 if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { }
71 else { m->mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest, average, and weighted."); m->mothurOutEndLine(); abort = true; }
73 showabund = validParameter.validFile(parameters, "showabund", false);
74 if (showabund == "not found") { showabund = "T"; }
76 timing = validParameter.validFile(parameters, "timing", false);
77 if (timing == "not found") { timing = "F"; }
82 //get matrix, list and rabund for execute
83 if(globaldata->gSparseMatrix != NULL) { matrix = globaldata->gSparseMatrix; }
85 if(globaldata->gListVector != NULL){
86 list = globaldata->gListVector;
87 rabund = new RAbundVector(list->getRAbundVector());
91 if (method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix, cutoff, method); }
92 else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix, cutoff, method); }
93 else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix, cutoff, method); }
94 else if(method == "weighted"){ cluster = new WeightedLinkage(rabund, list, matrix, cutoff, method); }
95 tag = cluster->getTag();
97 if (outputDir == "") { outputDir += hasPath(globaldata->inputFileName); }
98 fileroot = outputDir + getRootName(getSimpleName(globaldata->inputFileName));
100 openOutputFile(fileroot+ tag + ".sabund", sabundFile);
101 openOutputFile(fileroot+ tag + ".rabund", rabundFile);
102 openOutputFile(fileroot+ tag + ".list", listFile);
104 outputNames.push_back(fileroot+ tag + ".sabund");
105 outputNames.push_back(fileroot+ tag + ".rabund");
106 outputNames.push_back(fileroot+ tag + ".list");
110 catch(exception& e) {
111 m->errorOut(e, "ClusterCommand", "ClusterCommand");
116 //**********************************************************************************************************************
118 void ClusterCommand::help(){
120 m->mothurOut("The cluster command can only be executed after a successful read.dist command.\n");
121 m->mothurOut("The cluster command parameter options are method, cuttoff, hard, precision, showabund and timing. No parameters are required.\n");
122 m->mothurOut("The cluster command should be in the following format: \n");
123 m->mothurOut("cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
124 m->mothurOut("The acceptable cluster methods are furthest, nearest and average. If no method is provided then furthest is assumed.\n\n");
126 catch(exception& e) {
127 m->errorOut(e, "ClusterCommand", "help");
132 //**********************************************************************************************************************
134 ClusterCommand::~ClusterCommand(){
135 if (abort == false) {
141 //**********************************************************************************************************************
143 int ClusterCommand::execute(){
146 if (abort == true) { return 0; }
148 time_t estart = time(NULL);
149 //int ndist = matrix->getNNodes();
150 float previousDist = 0.00000;
151 float rndPreviousDist = 0.00000;
158 double saveCutoff = cutoff;
160 while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
162 if (m->control_pressed) { //clean up
163 delete globaldata->gSparseMatrix; globaldata->gSparseMatrix = NULL;
164 delete globaldata->gListVector; globaldata->gListVector = NULL;
165 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
166 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
167 sabundFile.close();rabundFile.close();listFile.close();
168 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
172 if (print_start && isTrue(timing)) {
173 m->mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/"
174 + toString(roundDist(matrix->getSmallDist(), precision))
175 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
182 cluster->update(cutoff);
184 float dist = matrix->getSmallDist();
187 rndDist = ceilDist(dist, precision);
189 rndDist = roundDist(dist, precision);
192 if(previousDist <= 0.0000 && dist != previousDist){
195 else if(rndDist != rndPreviousDist){
196 printData(toString(rndPreviousDist, length-1));
200 rndPreviousDist = rndDist;
205 if (print_start && isTrue(timing)) {
206 m->mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist)
207 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
212 if(previousDist <= 0.0000){
215 else if(rndPreviousDist<cutoff){
216 printData(toString(rndPreviousDist, length-1));
219 //delete globaldata's copy of the sparsematrix and listvector to free up memory
220 delete globaldata->gSparseMatrix; globaldata->gSparseMatrix = NULL;
221 delete globaldata->gListVector; globaldata->gListVector = NULL;
223 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
224 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
225 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
227 globaldata->setListFile(fileroot+ tag + ".list");
228 globaldata->setNameFile("");
229 globaldata->setFormat("list");
235 if (saveCutoff != cutoff) {
236 if (hard) { saveCutoff = ceilDist(saveCutoff, precision); }
237 else { saveCutoff = roundDist(saveCutoff, precision); }
239 m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine();
242 m->mothurOutEndLine();
243 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
244 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
245 m->mothurOutEndLine();
248 //if (isTrue(timing)) {
249 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
255 catch(exception& e) {
256 m->errorOut(e, "ClusterCommand", "execute");
261 //**********************************************************************************************************************
263 void ClusterCommand::printData(string label){
265 if (isTrue(timing)) {
266 m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
267 + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
273 oldRAbund.setLabel(label);
274 if (isTrue(showabund)) {
275 oldRAbund.getSAbundVector().print(cout);
277 oldRAbund.print(rabundFile);
278 oldRAbund.getSAbundVector().print(sabundFile);
280 oldList.setLabel(label);
281 oldList.print(listFile);
283 catch(exception& e) {
284 m->errorOut(e, "ClusterCommand", "printData");
290 //**********************************************************************************************************************