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"};
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) { abort = true; }
37 //error checking to make sure they read a distance file
38 if ((globaldata->gSparseMatrix == NULL) || (globaldata->gListVector == NULL)) {
39 mothurOut("Before you use the cluster command, you first need to read in a distance matrix."); mothurOutEndLine(); abort = true;
42 //check for optional parameter and set defaults
43 // ...at some point should added some additional type checking...
44 //get user cutoff and precision or use defaults
46 temp = validParameter.validFile(parameters, "precision", false); if (temp == "not found") { temp = "100"; }
47 //saves precision legnth for formatting below
48 length = temp.length();
49 convert(temp, precision);
51 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "10"; }
52 convert(temp, cutoff);
53 cutoff += (5 / (precision * 10.0));
55 method = validParameter.validFile(parameters, "method", false); if (method == "not found") { method = "furthest"; }
58 if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
59 else { mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average."); mothurOutEndLine(); abort = true; }
64 //get matrix, list and rabund for execute
65 if(globaldata->gSparseMatrix != NULL) { matrix = globaldata->gSparseMatrix; }
67 if(globaldata->gListVector != NULL){
68 list = globaldata->gListVector;
69 rabund = new RAbundVector(list->getRAbundVector());
73 if(method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix); tag = "fn"; }
74 else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix); tag = "nn"; }
75 else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix); tag = "an"; }
76 else { mothurOut("error - not recognized method"); mothurOutEndLine(); abort = true; }
78 fileroot = getRootName(globaldata->inputFileName);
80 openOutputFile(fileroot+ tag + ".sabund", sabundFile);
81 openOutputFile(fileroot+ tag + ".rabund", rabundFile);
82 openOutputFile(fileroot+ tag + ".list", listFile);
91 errorOut(e, "ClusterCommand", "ClusterCommand");
96 //**********************************************************************************************************************
98 void ClusterCommand::help(){
100 mothurOut("The cluster command can only be executed after a successful read.dist command.\n");
101 mothurOut("The cluster command parameter options are method, cuttoff and precision. No parameters are required.\n");
102 mothurOut("The cluster command should be in the following format: \n");
103 mothurOut("cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
104 mothurOut("The acceptable cluster methods are furthest, nearest and average. If no method is provided then furthest is assumed.\n\n");
106 catch(exception& e) {
107 errorOut(e, "ClusterCommand", "help");
112 //**********************************************************************************************************************
114 ClusterCommand::~ClusterCommand(){
115 if (abort == false) {
121 //**********************************************************************************************************************
123 int ClusterCommand::execute(){
126 if (abort == true) { return 0; }
128 float previousDist = 0.00000;
129 float rndPreviousDist = 0.00000;
137 while(matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
139 float dist = matrix->getSmallDist();
140 float rndDist = roundDist(dist, precision);
142 if(previousDist <= 0.0000 && dist != previousDist){
145 else if(rndDist != rndPreviousDist){
146 printData(toString(rndPreviousDist, length-1));
150 rndPreviousDist = rndDist;
155 if(previousDist <= 0.0000){
158 else if(rndPreviousDist<cutoff){
159 printData(toString(rndPreviousDist, length-1));
162 //delete globaldata's copy of the sparsematrix and listvector to free up memory
163 delete globaldata->gSparseMatrix; globaldata->gSparseMatrix = NULL;
164 delete globaldata->gListVector; globaldata->gListVector = NULL;
166 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
167 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
168 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
170 globaldata->setListFile(fileroot+ tag + ".list");
171 globaldata->setNameFile("");
172 globaldata->setFormat("list");
180 catch(exception& e) {
181 errorOut(e, "ClusterCommand", "execute");
186 //**********************************************************************************************************************
188 void ClusterCommand::printData(string label){
190 oldRAbund.setLabel(label);
191 oldRAbund.getSAbundVector().print(cout);
192 oldRAbund.print(rabundFile);
193 oldRAbund.getSAbundVector().print(sabundFile);
195 oldList.setLabel(label);
196 oldList.print(listFile);
198 catch(exception& e) {
199 errorOut(e, "ClusterCommand", "printData");
203 //**********************************************************************************************************************