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){
158 if (m->control_pressed) { //clean up
159 delete globaldata->gSparseMatrix; globaldata->gSparseMatrix = NULL;
160 delete globaldata->gListVector; globaldata->gListVector = NULL;
161 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
162 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
163 sabundFile.close();rabundFile.close();listFile.close();
164 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
168 if (print_start && isTrue(timing)) {
169 m->mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/"
170 + toString(roundDist(matrix->getSmallDist(), precision))
171 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
178 cluster->update(cutoff);
180 float dist = matrix->getSmallDist();
181 float rndDist = roundDist(dist, precision);
183 if(previousDist <= 0.0000 && dist != previousDist){
186 else if(rndDist != rndPreviousDist){
187 printData(toString(rndPreviousDist, length-1));
191 rndPreviousDist = rndDist;
196 if (print_start && isTrue(timing)) {
197 m->mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist)
198 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
203 if(previousDist <= 0.0000){
206 else if(rndPreviousDist<cutoff){
207 printData(toString(rndPreviousDist, length-1));
210 //delete globaldata's copy of the sparsematrix and listvector to free up memory
211 delete globaldata->gSparseMatrix; globaldata->gSparseMatrix = NULL;
212 delete globaldata->gListVector; globaldata->gListVector = NULL;
214 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
215 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
216 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
218 globaldata->setListFile(fileroot+ tag + ".list");
219 globaldata->setNameFile("");
220 globaldata->setFormat("list");
226 if (saveCutoff != cutoff) { m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine(); }
228 m->mothurOutEndLine();
229 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
230 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
231 m->mothurOutEndLine();
234 //if (isTrue(timing)) {
235 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
241 catch(exception& e) {
242 m->errorOut(e, "ClusterCommand", "execute");
247 //**********************************************************************************************************************
249 void ClusterCommand::printData(string label){
251 if (isTrue(timing)) {
252 m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
253 + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
259 oldRAbund.setLabel(label);
260 if (isTrue(showabund)) {
261 oldRAbund.getSAbundVector().print(cout);
263 oldRAbund.print(rabundFile);
264 oldRAbund.getSAbundVector().print(sabundFile);
266 oldList.setLabel(label);
267 oldList.print(listFile);
269 catch(exception& e) {
270 m->errorOut(e, "ClusterCommand", "printData");
276 //**********************************************************************************************************************