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","outputdir","inputdir"};
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 //if the user changes the output directory command factory will send this info to us in the output parameter
40 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
42 //error checking to make sure they read a distance file
43 if ((globaldata->gSparseMatrix == NULL) || (globaldata->gListVector == NULL)) {
44 mothurOut("Before you use the cluster command, you first need to read in a distance matrix."); mothurOutEndLine();
48 //check for optional parameter and set defaults
49 // ...at some point should added some additional type checking...
50 //get user cutoff and precision or use defaults
52 temp = validParameter.validFile(parameters, "precision", false);
53 if (temp == "not found") { temp = "100"; }
54 //saves precision legnth for formatting below
55 length = temp.length();
56 convert(temp, precision);
58 temp = validParameter.validFile(parameters, "cutoff", false);
59 if (temp == "not found") { temp = "10"; }
60 convert(temp, cutoff);
61 cutoff += (5 / (precision * 10.0));
63 method = validParameter.validFile(parameters, "method", false);
64 if (method == "not found") { method = "furthest"; }
66 if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
67 else { mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average."); mothurOutEndLine(); abort = true; }
69 showabund = validParameter.validFile(parameters, "showabund", false);
70 if (showabund == "not found") { showabund = "T"; }
72 timing = validParameter.validFile(parameters, "timing", false);
73 if (timing == "not found") { timing = "F"; }
78 //get matrix, list and rabund for execute
79 if(globaldata->gSparseMatrix != NULL) { matrix = globaldata->gSparseMatrix; }
81 if(globaldata->gListVector != NULL){
82 list = globaldata->gListVector;
83 rabund = new RAbundVector(list->getRAbundVector());
87 if (method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix, cutoff, method); }
88 else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix, cutoff, method); }
89 else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix, cutoff, method); }
90 tag = cluster->getTag();
92 if (outputDir == "") { outputDir += hasPath(globaldata->inputFileName); }
93 fileroot = outputDir + getRootName(getSimpleName(globaldata->inputFileName));
95 openOutputFile(fileroot+ tag + ".sabund", sabundFile);
96 openOutputFile(fileroot+ tag + ".rabund", rabundFile);
97 openOutputFile(fileroot+ tag + ".list", listFile);
101 catch(exception& e) {
102 errorOut(e, "ClusterCommand", "ClusterCommand");
107 //**********************************************************************************************************************
109 void ClusterCommand::help(){
111 mothurOut("The cluster command can only be executed after a successful read.dist command.\n");
112 mothurOut("The cluster command parameter options are method, cuttoff, precision, showabund and timing. No parameters are required.\n");
113 mothurOut("The cluster command should be in the following format: \n");
114 mothurOut("cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
115 mothurOut("The acceptable cluster methods are furthest, nearest and average. If no method is provided then furthest is assumed.\n\n");
117 catch(exception& e) {
118 errorOut(e, "ClusterCommand", "help");
123 //**********************************************************************************************************************
125 ClusterCommand::~ClusterCommand(){
126 if (abort == false) {
132 //**********************************************************************************************************************
134 int ClusterCommand::execute(){
137 if (abort == true) { return 0; }
139 time_t estart = time(NULL);
140 //int ndist = matrix->getNNodes();
141 float previousDist = 0.00000;
142 float rndPreviousDist = 0.00000;
149 double saveCutoff = cutoff;
151 while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
152 if (print_start && isTrue(timing)) {
153 mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/"
154 + toString(roundDist(matrix->getSmallDist(), precision))
155 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
162 cluster->update(cutoff);
163 float dist = matrix->getSmallDist();
164 float rndDist = roundDist(dist, precision);
166 if(previousDist <= 0.0000 && dist != previousDist){
169 else if(rndDist != rndPreviousDist){
170 printData(toString(rndPreviousDist, length-1));
174 rndPreviousDist = rndDist;
179 if (print_start && isTrue(timing)) {
180 mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist)
181 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
186 if(previousDist <= 0.0000){
189 else if(rndPreviousDist<cutoff){
190 printData(toString(rndPreviousDist, length-1));
193 //delete globaldata's copy of the sparsematrix and listvector to free up memory
194 delete globaldata->gSparseMatrix; globaldata->gSparseMatrix = NULL;
195 delete globaldata->gListVector; globaldata->gListVector = NULL;
197 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
198 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
199 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
201 globaldata->setListFile(fileroot+ tag + ".list");
202 globaldata->setNameFile("");
203 globaldata->setFormat("list");
209 if (saveCutoff != cutoff) { mothurOut("changed cutoff to " + toString(cutoff)); mothurOutEndLine(); }
211 //if (isTrue(timing)) {
212 mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); mothurOutEndLine();
218 catch(exception& e) {
219 errorOut(e, "ClusterCommand", "execute");
224 //**********************************************************************************************************************
226 void ClusterCommand::printData(string label){
228 if (isTrue(timing)) {
229 mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
230 + "\tclusters. Updates: " + toString(loops)); mothurOutEndLine();
236 oldRAbund.setLabel(label);
237 if (isTrue(showabund)) {
238 oldRAbund.getSAbundVector().print(cout);
240 oldRAbund.print(rabundFile);
241 oldRAbund.getSAbundVector().print(sabundFile);
243 oldList.setLabel(label);
244 oldList.print(listFile);
246 catch(exception& e) {
247 errorOut(e, "ClusterCommand", "printData");
253 //**********************************************************************************************************************