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();
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
23 //valid paramters for this command
24 string Array[] = {"cutoff","precision","method","showabund","timing"};
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string,string> parameters = parser.getParameters();
30 ValidParameters validParameter;
32 //check to make sure all parameters are valid for command
33 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
34 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
39 //error checking to make sure they read a distance file
40 if ((globaldata->gSparseMatrix == NULL) || (globaldata->gListVector == NULL)) {
41 mothurOut("Before you use the cluster command, you first need to read in a distance matrix."); mothurOutEndLine();
45 //check for optional parameter and set defaults
46 // ...at some point should added some additional type checking...
47 //get user cutoff and precision or use defaults
49 temp = validParameter.validFile(parameters, "precision", false);
50 if (temp == "not found") { temp = "100"; }
51 //saves precision legnth for formatting below
52 length = temp.length();
53 convert(temp, precision);
55 temp = validParameter.validFile(parameters, "cutoff", false);
56 if (temp == "not found") { temp = "10"; }
57 convert(temp, cutoff);
58 cutoff += (5 / (precision * 10.0));
60 method = validParameter.validFile(parameters, "method", false);
61 if (method == "not found") { method = "furthest"; }
63 if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
64 else { mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average."); mothurOutEndLine(); abort = true; }
66 showabund = validParameter.validFile(parameters, "showabund", false);
67 if (showabund == "not found") { showabund = "T"; }
69 timing = validParameter.validFile(parameters, "timing", false);
70 if (timing == "not found") { timing = "F"; }
74 //get matrix, list and rabund for execute
75 if(globaldata->gSparseMatrix != NULL) { matrix = globaldata->gSparseMatrix; }
77 if(globaldata->gListVector != NULL){
78 list = globaldata->gListVector;
79 rabund = new RAbundVector(list->getRAbundVector());
83 if (method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix); }
84 else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix); }
85 else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix); }
86 tag = cluster->getTag();
88 fileroot = getRootName(globaldata->inputFileName);
90 openOutputFile(fileroot+ tag + ".sabund", sabundFile);
91 openOutputFile(fileroot+ tag + ".rabund", rabundFile);
92 openOutputFile(fileroot+ tag + ".list", listFile);
97 errorOut(e, "ClusterCommand", "ClusterCommand");
102 //**********************************************************************************************************************
104 void ClusterCommand::help(){
106 mothurOut("The cluster command can only be executed after a successful read.dist command.\n");
107 mothurOut("The cluster command parameter options are method, cuttoff, precision, showabund and timing. No parameters are required.\n");
108 mothurOut("The cluster command should be in the following format: \n");
109 mothurOut("cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
110 mothurOut("The acceptable cluster methods are furthest, nearest and average. If no method is provided then furthest is assumed.\n\n");
112 catch(exception& e) {
113 errorOut(e, "ClusterCommand", "help");
118 //**********************************************************************************************************************
120 ClusterCommand::~ClusterCommand(){
121 if (abort == false) {
127 //**********************************************************************************************************************
129 int ClusterCommand::execute(){
132 if (abort == true) { return 0; }
134 time_t estart = time(NULL);
135 int ndist = matrix->getNNodes();
136 float previousDist = 0.00000;
137 float rndPreviousDist = 0.00000;
145 while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
146 if (print_start && isTrue(timing)) {
147 mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/"
148 + toString(roundDist(matrix->getSmallDist(), precision))
149 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
157 float dist = matrix->getSmallDist();
158 float rndDist = roundDist(dist, precision);
160 if(previousDist <= 0.0000 && dist != previousDist){
163 else if(rndDist != rndPreviousDist){
164 printData(toString(rndPreviousDist, length-1));
168 rndPreviousDist = rndDist;
173 if (print_start && isTrue(timing)) {
174 mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist)
175 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
180 if(previousDist <= 0.0000){
183 else if(rndPreviousDist<cutoff){
184 printData(toString(rndPreviousDist, length-1));
187 //delete globaldata's copy of the sparsematrix and listvector to free up memory
188 delete globaldata->gSparseMatrix; globaldata->gSparseMatrix = NULL;
189 delete globaldata->gListVector; globaldata->gListVector = NULL;
191 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
192 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
193 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
195 globaldata->setListFile(fileroot+ tag + ".list");
196 globaldata->setNameFile("");
197 globaldata->setFormat("list");
202 if (isTrue(timing)) {
203 mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster " + toString(ndist) + " distances"); mothurOutEndLine();
207 catch(exception& e) {
208 errorOut(e, "ClusterCommand", "execute");
213 //**********************************************************************************************************************
215 void ClusterCommand::printData(string label){
217 if (isTrue(timing)) {
218 mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
219 + "\tclusters. Updates: " + toString(loops)); mothurOutEndLine();
225 oldRAbund.setLabel(label);
226 if (isTrue(showabund)) {
227 oldRAbund.getSAbundVector().print(cout);
229 oldRAbund.print(rabundFile);
230 oldRAbund.getSAbundVector().print(sabundFile);
232 oldList.setLabel(label);
233 oldList.print(listFile);
235 catch(exception& e) {
236 errorOut(e, "ClusterCommand", "printData");
242 //**********************************************************************************************************************