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 parser = new OptionParser();
28 parser->parse(option, parameters); delete parser;
30 ValidParameters* validParameter = new ValidParameters();
32 //check to make sure all parameters are valid for command
33 for (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"; }
57 delete validParameter;
59 if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
60 else {cout << "Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average." << endl; abort = true; }
65 //get matrix, list and rabund for execute
66 if(globaldata->gSparseMatrix != NULL) { matrix = new SparseMatrix(*globaldata->gSparseMatrix); }
68 if(globaldata->gListVector != NULL){
69 list = new ListVector(*globaldata->gListVector);
70 rabund = new RAbundVector(list->getRAbundVector());
74 if(method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix); tag = "fn"; }
75 else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix); tag = "nn"; }
76 else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix); tag = "an"; }
77 else { cout << "error - not recognized method" << endl; abort = true; }
79 fileroot = getRootName(globaldata->inputFileName);
81 openOutputFile(fileroot+ tag + ".sabund", sabundFile);
82 openOutputFile(fileroot+ tag + ".rabund", rabundFile);
83 openOutputFile(fileroot+ tag + ".list", listFile);
92 cout << "Standard Error: " << e.what() << " has occurred in the ClusterCommand class Function ClusterCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
96 cout << "An unknown error has occurred in the ClusterCommand class function ClusterCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
101 //**********************************************************************************************************************
103 void ClusterCommand::help(){
105 cout << "The cluster command can only be executed after a successful read.dist command." << "\n";
106 cout << "The cluster command parameter options are method, cuttoff and precision. No parameters are required." << "\n";
107 cout << "The cluster command should be in the following format: " << "\n";
108 cout << "cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) " << "\n";
109 cout << "The acceptable cluster methods are furthest, nearest and average. If no method is provided then furthest is assumed." << "\n" << "\n";
111 catch(exception& e) {
112 cout << "Standard Error: " << e.what() << " has occurred in the ClusterCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
116 cout << "An unknown error has occurred in the ClusterCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
121 //**********************************************************************************************************************
123 ClusterCommand::~ClusterCommand(){
130 //**********************************************************************************************************************
132 int ClusterCommand::execute(){
135 if (abort == true) { return 0; }
137 float previousDist = 0.00000;
138 float rndPreviousDist = 0.00000;
146 while(matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
148 float dist = matrix->getSmallDist();
149 float rndDist = roundDist(dist, precision);
151 if(previousDist <= 0.0000 && dist != previousDist){
154 else if(rndDist != rndPreviousDist){
155 printData(toString(rndPreviousDist, length-1));
159 rndPreviousDist = rndDist;
164 if(previousDist <= 0.0000){
167 else if(rndPreviousDist<cutoff){
168 printData(toString(rndPreviousDist, length-1));
171 //delete globaldata's copy of the sparsematrix and listvector to free up memory
172 delete globaldata->gSparseMatrix; globaldata->gSparseMatrix = NULL;
173 delete globaldata->gListVector; globaldata->gListVector = NULL;
175 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
176 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
177 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
179 globaldata->setListFile(fileroot+ tag + ".list");
180 globaldata->setNameFile("");
181 globaldata->setFormat("list");
185 catch(exception& e) {
186 cout << "Standard Error: " << e.what() << " has occurred in the ClusterCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
190 cout << "An unknown error has occurred in the ClusterCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
196 //**********************************************************************************************************************
198 void ClusterCommand::printData(string label){
200 oldRAbund.setLabel(label);
201 oldRAbund.getSAbundVector().print(cout);
202 oldRAbund.print(rabundFile);
203 oldRAbund.getSAbundVector().print(sabundFile);
205 oldList.setLabel(label);
206 oldList.print(listFile);
208 catch(exception& e) {
209 cout << "Standard Error: " << e.what() << " has occurred in the ClusterCommand class Function printData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
213 cout << "An unknown error has occurred in the ClusterCommand class function printData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
218 //**********************************************************************************************************************