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 vector<string> ClusterCommand::getValidParameters(){
15 string AlignArray[] = {"cutoff","precision","method","showabund","timing","hard","outputdir","inputdir"};
16 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
20 m->errorOut(e, "ClusterCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 ClusterCommand::ClusterCommand(){
28 //initialize outputTypes
29 vector<string> tempOutNames;
30 outputTypes["list"] = tempOutNames;
31 outputTypes["rabund"] = tempOutNames;
32 outputTypes["sabund"] = tempOutNames;
35 m->errorOut(e, "ClusterCommand", "ClusterCommand");
39 //**********************************************************************************************************************
40 vector<string> ClusterCommand::getRequiredParameters(){
42 vector<string> myArray;
46 m->errorOut(e, "ClusterCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> ClusterCommand::getRequiredFiles(){
53 string Array[] = {"phylip","column","or"};
54 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
58 m->errorOut(e, "ClusterCommand", "getRequiredFiles");
62 //**********************************************************************************************************************
63 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
64 ClusterCommand::ClusterCommand(string option) {
66 globaldata = GlobalData::getInstance();
70 //allow user to run help
71 if(option == "help") { help(); abort = true; }
74 //valid paramters for this command
75 string Array[] = {"cutoff","precision","method","showabund","timing","hard","outputdir","inputdir"};
76 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
78 OptionParser parser(option);
79 map<string,string> parameters = parser.getParameters();
81 ValidParameters validParameter;
83 //check to make sure all parameters are valid for command
84 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
85 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
90 //initialize outputTypes
91 vector<string> tempOutNames;
92 outputTypes["list"] = tempOutNames;
93 outputTypes["rabund"] = tempOutNames;
94 outputTypes["sabund"] = tempOutNames;
96 //if the user changes the output directory command factory will send this info to us in the output parameter
97 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
99 //error checking to make sure they read a distance file
100 if ((globaldata->gSparseMatrix == NULL) || (globaldata->gListVector == NULL)) {
101 m->mothurOut("Before you use the cluster command, you first need to read in a distance matrix."); m->mothurOutEndLine();
105 //check for optional parameter and set defaults
106 // ...at some point should added some additional type checking...
107 //get user cutoff and precision or use defaults
109 temp = validParameter.validFile(parameters, "precision", false);
110 if (temp == "not found") { temp = "100"; }
111 //saves precision legnth for formatting below
112 length = temp.length();
113 convert(temp, precision);
115 temp = validParameter.validFile(parameters, "hard", false); if (temp == "not found") { temp = "F"; }
116 hard = m->isTrue(temp);
118 temp = validParameter.validFile(parameters, "cutoff", false);
119 if (temp == "not found") { temp = "10"; }
120 convert(temp, cutoff);
121 cutoff += (5 / (precision * 10.0));
123 method = validParameter.validFile(parameters, "method", false);
124 if (method == "not found") { method = "furthest"; }
126 if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { }
127 else { m->mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest, average, and weighted."); m->mothurOutEndLine(); abort = true; }
129 showabund = validParameter.validFile(parameters, "showabund", false);
130 if (showabund == "not found") { showabund = "T"; }
132 timing = validParameter.validFile(parameters, "timing", false);
133 if (timing == "not found") { timing = "F"; }
135 if (abort == false) {
138 //get matrix, list and rabund for execute
139 if(globaldata->gSparseMatrix != NULL) { matrix = globaldata->gSparseMatrix; }
141 if(globaldata->gListVector != NULL){
142 list = globaldata->gListVector;
143 rabund = new RAbundVector(list->getRAbundVector());
147 if (method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix, cutoff, method); }
148 else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix, cutoff, method); }
149 else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix, cutoff, method); }
150 else if(method == "weighted"){ cluster = new WeightedLinkage(rabund, list, matrix, cutoff, method); }
151 tag = cluster->getTag();
153 if (outputDir == "") { outputDir += m->hasPath(globaldata->inputFileName); }
154 fileroot = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName));
156 m->openOutputFile(fileroot+ tag + ".sabund", sabundFile);
157 m->openOutputFile(fileroot+ tag + ".rabund", rabundFile);
158 m->openOutputFile(fileroot+ tag + ".list", listFile);
160 outputNames.push_back(fileroot+ tag + ".sabund"); outputTypes["sabund"].push_back(fileroot+ tag + ".sabund");
161 outputNames.push_back(fileroot+ tag + ".rabund"); outputTypes["rabund"].push_back(fileroot+ tag + ".rabund");
162 outputNames.push_back(fileroot+ tag + ".list"); outputTypes["list"].push_back(fileroot+ tag + ".list");
166 catch(exception& e) {
167 m->errorOut(e, "ClusterCommand", "ClusterCommand");
172 //**********************************************************************************************************************
174 void ClusterCommand::help(){
176 m->mothurOut("The cluster command can only be executed after a successful read.dist command.\n");
177 m->mothurOut("The cluster command parameter options are method, cuttoff, hard, precision, showabund and timing. No parameters are required.\n");
178 m->mothurOut("The cluster command should be in the following format: \n");
179 m->mothurOut("cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
180 m->mothurOut("The acceptable cluster methods are furthest, nearest and average. If no method is provided then furthest is assumed.\n\n");
182 catch(exception& e) {
183 m->errorOut(e, "ClusterCommand", "help");
188 //**********************************************************************************************************************
190 ClusterCommand::~ClusterCommand(){
191 if (abort == false) {
197 //**********************************************************************************************************************
199 int ClusterCommand::execute(){
202 if (abort == true) { return 0; }
204 time_t estart = time(NULL);
205 //int ndist = matrix->getNNodes();
206 float previousDist = 0.00000;
207 float rndPreviousDist = 0.00000;
214 double saveCutoff = cutoff;
216 while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
218 if (m->control_pressed) { //clean up
219 delete globaldata->gSparseMatrix; globaldata->gSparseMatrix = NULL;
220 delete globaldata->gListVector; globaldata->gListVector = NULL;
221 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
222 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
223 sabundFile.close();rabundFile.close();listFile.close();
224 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
228 if (print_start && m->isTrue(timing)) {
229 m->mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/"
230 + toString(m->roundDist(matrix->getSmallDist(), precision))
231 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
238 cluster->update(cutoff);
240 float dist = matrix->getSmallDist();
243 rndDist = m->ceilDist(dist, precision);
245 rndDist = m->roundDist(dist, precision);
248 if(previousDist <= 0.0000 && dist != previousDist){
251 else if(rndDist != rndPreviousDist){
252 printData(toString(rndPreviousDist, length-1));
256 rndPreviousDist = rndDist;
261 if (print_start && m->isTrue(timing)) {
262 m->mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist)
263 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
268 if(previousDist <= 0.0000){
271 else if(rndPreviousDist<cutoff){
272 printData(toString(rndPreviousDist, length-1));
275 //delete globaldata's copy of the sparsematrix and listvector to free up memory
276 delete globaldata->gSparseMatrix; globaldata->gSparseMatrix = NULL;
277 delete globaldata->gListVector; globaldata->gListVector = NULL;
279 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
280 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
281 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
283 globaldata->setListFile(fileroot+ tag + ".list");
284 globaldata->setNameFile("");
285 globaldata->setFormat("list");
291 if (saveCutoff != cutoff) {
292 if (hard) { saveCutoff = m->ceilDist(saveCutoff, precision); }
293 else { saveCutoff = m->roundDist(saveCutoff, precision); }
295 m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine();
298 m->mothurOutEndLine();
299 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
300 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
301 m->mothurOutEndLine();
304 //if (m->isTrue(timing)) {
305 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
311 catch(exception& e) {
312 m->errorOut(e, "ClusterCommand", "execute");
317 //**********************************************************************************************************************
319 void ClusterCommand::printData(string label){
321 if (m->isTrue(timing)) {
322 m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
323 + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
329 oldRAbund.setLabel(label);
330 if (m->isTrue(showabund)) {
331 oldRAbund.getSAbundVector().print(cout);
333 oldRAbund.print(rabundFile);
334 oldRAbund.getSAbundVector().print(sabundFile);
336 oldList.setLabel(label);
337 oldList.print(listFile);
339 catch(exception& e) {
340 m->errorOut(e, "ClusterCommand", "printData");
346 //**********************************************************************************************************************