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(){
16 globaldata = GlobalData::getInstance();
18 if(globaldata->gSparseMatrix != NULL) { matrix = new SparseMatrix(*globaldata->gSparseMatrix); }
19 // Not sure if we want the address or an entire new memory allocation. Might be nice to have new memory so data
20 // doesn't need to be re-read, but then again, it could suck up a ton of memory. Dunno.
21 // if(globaldata->getSparseMatrix() != NULL) { matrix = globaldata->getSparseMatrix(); }
23 if(globaldata->gListVector != NULL){
24 list = new ListVector(*globaldata->gListVector);
25 rabund = new RAbundVector(list->getRAbundVector());
26 //rabund->print(cout);
29 if(globaldata->getMethod() != "") { method = globaldata->getMethod(); }
30 //if no method given use furthest, initialized in globaldata
31 if(method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix); tag = "fn"; }
32 else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix); tag = "nn"; }
33 else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix); tag = "an"; }
34 else { cout << "error - not recognized method" << endl; }
36 if(globaldata->getPrecision() != ""){
37 convert(globaldata->getPrecision(), precision);
40 //saves precision legnth for formatting below
41 length = globaldata->getPrecision().length();
43 if(globaldata->getCutOff() != ""){
44 convert(globaldata->getCutOff(), cutoff);
45 cutoff += (5 / (precision * 10.0));
48 fileroot = getRootName(globaldata->getFileRoot());
50 openOutputFile(fileroot+ tag + ".sabund", sabundFile);
51 openOutputFile(fileroot+ tag + ".rabund", rabundFile);
52 openOutputFile(fileroot+ tag + ".list", listFile);
55 cout << "Standard Error: " << e.what() << " has occurred in the ClusterCommand class Function ClusterCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
59 cout << "An unknown error has occurred in the ClusterCommand class function ClusterCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
63 //**********************************************************************************************************************
65 ClusterCommand::~ClusterCommand(){
72 //**********************************************************************************************************************
74 int ClusterCommand::execute(){
76 float previousDist = 0.00000;
77 float rndPreviousDist = 0.00000;
85 while(matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
87 float dist = matrix->getSmallDist();
88 float rndDist = roundDist(dist, precision);
90 if(previousDist <= 0.0000 && dist != previousDist){
93 else if(rndDist != rndPreviousDist){
94 printData(toString(rndPreviousDist, length-1));
98 rndPreviousDist = rndDist;
103 if(previousDist <= 0.0000){
106 else if(rndPreviousDist<cutoff){
107 printData(toString(rndPreviousDist, length-1));
110 //delete globaldata's copy of the sparsematrix and listvector to free up memory
111 delete globaldata->gSparseMatrix; globaldata->gSparseMatrix = NULL;
112 delete globaldata->gListVector; globaldata->gListVector = NULL;
114 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
115 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
116 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
118 globaldata->setListFile(fileroot+ tag + ".list");
119 globaldata->setNameFile("");
120 globaldata->setFormat("list");
124 catch(exception& e) {
125 cout << "Standard Error: " << e.what() << " has occurred in the ClusterCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
129 cout << "An unknown error has occurred in the ClusterCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
135 //**********************************************************************************************************************
137 void ClusterCommand::printData(string label){
139 oldRAbund.setLabel(label);
140 oldRAbund.getSAbundVector().print(cout);
141 oldRAbund.print(rabundFile);
142 oldRAbund.getSAbundVector().print(sabundFile);
144 oldList.setLabel(label);
145 oldList.print(listFile);
147 catch(exception& e) {
148 cout << "Standard Error: " << e.what() << " has occurred in the ClusterCommand class Function printData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
152 cout << "An unknown error has occurred in the ClusterCommand class function printData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
157 //**********************************************************************************************************************