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(){
27 abort = true; calledHelp = true;
28 vector<string> tempOutNames;
29 outputTypes["list"] = tempOutNames;
30 outputTypes["rabund"] = tempOutNames;
31 outputTypes["sabund"] = tempOutNames;
34 m->errorOut(e, "ClusterCommand", "ClusterCommand");
38 //**********************************************************************************************************************
39 vector<string> ClusterCommand::getRequiredParameters(){
41 vector<string> myArray;
45 m->errorOut(e, "ClusterCommand", "getRequiredParameters");
49 //**********************************************************************************************************************
50 vector<string> ClusterCommand::getRequiredFiles(){
52 string Array[] = {"phylip","column","or"};
53 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
57 m->errorOut(e, "ClusterCommand", "getRequiredFiles");
61 //**********************************************************************************************************************
62 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
63 ClusterCommand::ClusterCommand(string option) {
65 globaldata = GlobalData::getInstance();
67 abort = false; calledHelp = false;
69 //allow user to run help
70 if(option == "help") { help(); abort = true; calledHelp = true; }
73 //valid paramters for this command
74 string Array[] = {"cutoff","precision","method","showabund","timing","hard","outputdir","inputdir"};
75 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
77 OptionParser parser(option);
78 map<string,string> parameters = parser.getParameters();
80 ValidParameters validParameter;
82 //check to make sure all parameters are valid for command
83 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
84 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
89 //initialize outputTypes
90 vector<string> tempOutNames;
91 outputTypes["list"] = tempOutNames;
92 outputTypes["rabund"] = tempOutNames;
93 outputTypes["sabund"] = tempOutNames;
95 //if the user changes the output directory command factory will send this info to us in the output parameter
96 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
98 //error checking to make sure they read a distance file
99 if ((globaldata->gSparseMatrix == NULL) || (globaldata->gListVector == NULL)) {
100 m->mothurOut("Before you use the cluster command, you first need to read in a distance matrix."); m->mothurOutEndLine();
104 //check for optional parameter and set defaults
105 // ...at some point should added some additional type checking...
106 //get user cutoff and precision or use defaults
108 temp = validParameter.validFile(parameters, "precision", false);
109 if (temp == "not found") { temp = "100"; }
110 //saves precision legnth for formatting below
111 length = temp.length();
112 convert(temp, precision);
114 temp = validParameter.validFile(parameters, "hard", false); if (temp == "not found") { temp = "F"; }
115 hard = m->isTrue(temp);
117 temp = validParameter.validFile(parameters, "cutoff", false);
118 if (temp == "not found") { temp = "10"; }
119 convert(temp, cutoff);
120 cutoff += (5 / (precision * 10.0));
122 method = validParameter.validFile(parameters, "method", false);
123 if (method == "not found") { method = "furthest"; }
125 if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { }
126 else { m->mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest, average, and weighted."); m->mothurOutEndLine(); abort = true; }
128 showabund = validParameter.validFile(parameters, "showabund", false);
129 if (showabund == "not found") { showabund = "T"; }
131 timing = validParameter.validFile(parameters, "timing", false);
132 if (timing == "not found") { timing = "F"; }
134 if (abort == false) {
137 //get matrix, list and rabund for execute
138 if(globaldata->gSparseMatrix != NULL) { matrix = globaldata->gSparseMatrix; }
140 if(globaldata->gListVector != NULL){
141 list = globaldata->gListVector;
142 rabund = new RAbundVector(list->getRAbundVector());
146 if (method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix, cutoff, method); }
147 else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix, cutoff, method); }
148 else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix, cutoff, method); }
149 else if(method == "weighted"){ cluster = new WeightedLinkage(rabund, list, matrix, cutoff, method); }
150 tag = cluster->getTag();
152 if (outputDir == "") { outputDir += m->hasPath(globaldata->inputFileName); }
153 fileroot = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName));
155 m->openOutputFile(fileroot+ tag + ".sabund", sabundFile);
156 m->openOutputFile(fileroot+ tag + ".rabund", rabundFile);
157 m->openOutputFile(fileroot+ tag + ".list", listFile);
159 outputNames.push_back(fileroot+ tag + ".sabund"); outputTypes["sabund"].push_back(fileroot+ tag + ".sabund");
160 outputNames.push_back(fileroot+ tag + ".rabund"); outputTypes["rabund"].push_back(fileroot+ tag + ".rabund");
161 outputNames.push_back(fileroot+ tag + ".list"); outputTypes["list"].push_back(fileroot+ tag + ".list");
165 catch(exception& e) {
166 m->errorOut(e, "ClusterCommand", "ClusterCommand");
171 //**********************************************************************************************************************
173 void ClusterCommand::help(){
175 m->mothurOut("The cluster command can only be executed after a successful read.dist command.\n");
176 m->mothurOut("The cluster command parameter options are method, cuttoff, hard, precision, showabund and timing. No parameters are required.\n");
177 m->mothurOut("The cluster command should be in the following format: \n");
178 m->mothurOut("cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
179 m->mothurOut("The acceptable cluster methods are furthest, nearest and average. If no method is provided then furthest is assumed.\n\n");
181 catch(exception& e) {
182 m->errorOut(e, "ClusterCommand", "help");
187 //**********************************************************************************************************************
189 ClusterCommand::~ClusterCommand(){
190 if (abort == false) {
196 //**********************************************************************************************************************
198 int ClusterCommand::execute(){
201 if (abort == true) { if (calledHelp) { return 0; } return 2; }
203 time_t estart = time(NULL);
204 //int ndist = matrix->getNNodes();
205 float previousDist = 0.00000;
206 float rndPreviousDist = 0.00000;
213 double saveCutoff = cutoff;
215 while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
217 if (m->control_pressed) { //clean up
218 delete globaldata->gSparseMatrix; globaldata->gSparseMatrix = NULL;
219 delete globaldata->gListVector; globaldata->gListVector = NULL;
220 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
221 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
222 sabundFile.close();rabundFile.close();listFile.close();
223 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
227 if (print_start && m->isTrue(timing)) {
228 m->mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/"
229 + toString(m->roundDist(matrix->getSmallDist(), precision))
230 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
237 cluster->update(cutoff);
239 float dist = matrix->getSmallDist();
242 rndDist = m->ceilDist(dist, precision);
244 rndDist = m->roundDist(dist, precision);
247 if(previousDist <= 0.0000 && dist != previousDist){
250 else if(rndDist != rndPreviousDist){
251 printData(toString(rndPreviousDist, length-1));
255 rndPreviousDist = rndDist;
260 if (print_start && m->isTrue(timing)) {
261 m->mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist)
262 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
267 if(previousDist <= 0.0000){
270 else if(rndPreviousDist<cutoff){
271 printData(toString(rndPreviousDist, length-1));
274 //delete globaldata's copy of the sparsematrix and listvector to free up memory
275 delete globaldata->gSparseMatrix; globaldata->gSparseMatrix = NULL;
276 delete globaldata->gListVector; globaldata->gListVector = NULL;
278 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
279 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
280 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
282 globaldata->setListFile(fileroot+ tag + ".list");
283 globaldata->setNameFile("");
284 globaldata->setFormat("list");
290 if (saveCutoff != cutoff) {
291 if (hard) { saveCutoff = m->ceilDist(saveCutoff, precision); }
292 else { saveCutoff = m->roundDist(saveCutoff, precision); }
294 m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine();
297 m->mothurOutEndLine();
298 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
299 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
300 m->mothurOutEndLine();
303 //if (m->isTrue(timing)) {
304 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
310 catch(exception& e) {
311 m->errorOut(e, "ClusterCommand", "execute");
316 //**********************************************************************************************************************
318 void ClusterCommand::printData(string label){
320 if (m->isTrue(timing)) {
321 m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
322 + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
328 oldRAbund.setLabel(label);
329 if (m->isTrue(showabund)) {
330 oldRAbund.getSAbundVector().print(cout);
332 oldRAbund.print(rabundFile);
333 oldRAbund.getSAbundVector().print(sabundFile);
335 oldList.setLabel(label);
336 oldList.print(listFile);
338 catch(exception& e) {
339 m->errorOut(e, "ClusterCommand", "printData");
345 //**********************************************************************************************************************