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 cout << "Before you use the cluster command, you first need to read in a distance matrix." << endl; 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 {cout << "Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average." << endl; 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 { cout << "error - not recognized method" << endl; 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 cout << "Standard Error: " << e.what() << " has occurred in the ClusterCommand class Function ClusterCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
95 cout << "An unknown error has occurred in the ClusterCommand class function ClusterCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
100 //**********************************************************************************************************************
102 void ClusterCommand::help(){
104 cout << "The cluster command can only be executed after a successful read.dist command." << "\n";
105 cout << "The cluster command parameter options are method, cuttoff and precision. No parameters are required." << "\n";
106 cout << "The cluster command should be in the following format: " << "\n";
107 cout << "cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) " << "\n";
108 cout << "The acceptable cluster methods are furthest, nearest and average. If no method is provided then furthest is assumed." << "\n" << "\n";
110 catch(exception& e) {
111 cout << "Standard Error: " << e.what() << " has occurred in the ClusterCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
115 cout << "An unknown error has occurred in the ClusterCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
120 //**********************************************************************************************************************
122 ClusterCommand::~ClusterCommand(){
127 //**********************************************************************************************************************
129 int ClusterCommand::execute(){
132 if (abort == true) { return 0; }
134 float previousDist = 0.00000;
135 float rndPreviousDist = 0.00000;
143 while(matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
145 float dist = matrix->getSmallDist();
146 float rndDist = roundDist(dist, precision);
148 if(previousDist <= 0.0000 && dist != previousDist){
151 else if(rndDist != rndPreviousDist){
152 printData(toString(rndPreviousDist, length-1));
156 rndPreviousDist = rndDist;
161 if(previousDist <= 0.0000){
164 else if(rndPreviousDist<cutoff){
165 printData(toString(rndPreviousDist, length-1));
168 //delete globaldata's copy of the sparsematrix and listvector to free up memory
169 delete globaldata->gSparseMatrix; globaldata->gSparseMatrix = NULL;
170 delete globaldata->gListVector; globaldata->gListVector = NULL;
172 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
173 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
174 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
176 globaldata->setListFile(fileroot+ tag + ".list");
177 globaldata->setNameFile("");
178 globaldata->setFormat("list");
182 catch(exception& e) {
183 cout << "Standard Error: " << e.what() << " has occurred in the ClusterCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
187 cout << "An unknown error has occurred in the ClusterCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
193 //**********************************************************************************************************************
195 void ClusterCommand::printData(string label){
197 oldRAbund.setLabel(label);
198 oldRAbund.getSAbundVector().print(cout);
199 oldRAbund.print(rabundFile);
200 oldRAbund.getSAbundVector().print(sabundFile);
202 oldList.setLabel(label);
203 oldList.print(listFile);
205 catch(exception& e) {
206 cout << "Standard Error: " << e.what() << " has occurred in the ClusterCommand class Function printData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
210 cout << "An unknown error has occurred in the ClusterCommand class function printData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
215 //**********************************************************************************************************************