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"; }
75 //get matrix, list and rabund for execute
76 if(globaldata->gSparseMatrix != NULL) { matrix = globaldata->gSparseMatrix; }
78 if(globaldata->gListVector != NULL){
79 list = globaldata->gListVector;
80 rabund = new RAbundVector(list->getRAbundVector());
84 if (method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix, cutoff); }
85 else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix, cutoff); }
86 else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix, cutoff); }
87 tag = cluster->getTag();
89 fileroot = getRootName(globaldata->inputFileName);
91 openOutputFile(fileroot+ tag + ".sabund", sabundFile);
92 openOutputFile(fileroot+ tag + ".rabund", rabundFile);
93 openOutputFile(fileroot+ tag + ".list", listFile);
98 errorOut(e, "ClusterCommand", "ClusterCommand");
103 //**********************************************************************************************************************
105 void ClusterCommand::help(){
107 mothurOut("The cluster command can only be executed after a successful read.dist command.\n");
108 mothurOut("The cluster command parameter options are method, cuttoff, precision, showabund and timing. No parameters are required.\n");
109 mothurOut("The cluster command should be in the following format: \n");
110 mothurOut("cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
111 mothurOut("The acceptable cluster methods are furthest, nearest and average. If no method is provided then furthest is assumed.\n\n");
113 catch(exception& e) {
114 errorOut(e, "ClusterCommand", "help");
119 //**********************************************************************************************************************
121 ClusterCommand::~ClusterCommand(){
122 if (abort == false) {
128 //**********************************************************************************************************************
130 int ClusterCommand::execute(){
133 if (abort == true) { return 0; }
135 time_t estart = time(NULL);
136 //int ndist = matrix->getNNodes();
137 float previousDist = 0.00000;
138 float rndPreviousDist = 0.00000;
146 while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
147 if (print_start && isTrue(timing)) {
148 mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/"
149 + toString(roundDist(matrix->getSmallDist(), precision))
150 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
158 float dist = matrix->getSmallDist();
159 float rndDist = roundDist(dist, precision);
161 if(previousDist <= 0.0000 && dist != previousDist){
164 else if(rndDist != rndPreviousDist){
165 printData(toString(rndPreviousDist, length-1));
169 rndPreviousDist = rndDist;
174 if (print_start && isTrue(timing)) {
175 mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist)
176 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
181 if(previousDist <= 0.0000){
184 else if(rndPreviousDist<cutoff){
185 printData(toString(rndPreviousDist, length-1));
188 //delete globaldata's copy of the sparsematrix and listvector to free up memory
189 delete globaldata->gSparseMatrix; globaldata->gSparseMatrix = NULL;
190 delete globaldata->gListVector; globaldata->gListVector = NULL;
192 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
193 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
194 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
196 globaldata->setListFile(fileroot+ tag + ".list");
197 globaldata->setNameFile("");
198 globaldata->setFormat("list");
203 //if (isTrue(timing)) {
204 mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); mothurOutEndLine();
208 catch(exception& e) {
209 errorOut(e, "ClusterCommand", "execute");
214 //**********************************************************************************************************************
216 void ClusterCommand::printData(string label){
218 if (isTrue(timing)) {
219 mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
220 + "\tclusters. Updates: " + toString(loops)); mothurOutEndLine();
226 oldRAbund.setLabel(label);
227 if (isTrue(showabund)) {
228 oldRAbund.getSAbundVector().print(cout);
230 oldRAbund.print(rabundFile);
231 oldRAbund.getSAbundVector().print(sabundFile);
233 oldList.setLabel(label);
234 oldList.print(listFile);
236 catch(exception& e) {
237 errorOut(e, "ClusterCommand", "printData");
243 //**********************************************************************************************************************