]> git.donarmstrong.com Git - mothur.git/blob - clustercommand.cpp
fixed memory issue with cluster command
[mothur.git] / clustercommand.cpp
1 /*
2  *  clustercommand.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "clustercommand.h"
11
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(){
15         try{
16                 globaldata = GlobalData::getInstance();
17         
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();         }
22         
23                 if(globaldata->gListVector != NULL){
24                         list = new ListVector(*globaldata->gListVector);
25                         rabund = new RAbundVector(list->getRAbundVector());
26                         //rabund->print(cout);
27                 }
28         
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;                                                                                        }
35         
36                 if(globaldata->getPrecision() != ""){
37                         convert(globaldata->getPrecision(), precision); 
38                 }
39                 
40                 //saves precision legnth for formatting below
41                 length = globaldata->getPrecision().length();
42                 
43                 if(globaldata->getCutOff() != ""){
44                         convert(globaldata->getCutOff(), cutoff);       
45                         cutoff += (5 / (precision * 10.0));
46                 }
47         
48                 fileroot = getRootName(globaldata->getFileRoot());
49                 
50                 openOutputFile(fileroot+ tag + ".sabund",       sabundFile);
51                 openOutputFile(fileroot+ tag + ".rabund",       rabundFile);
52                 openOutputFile(fileroot+ tag + ".list",         listFile);
53         }
54         catch(exception& e) {
55                 cout << "Standard Error: " << e.what() << " has occurred in the ClusterCommand class Function ClusterCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
56                 exit(1);
57         }
58         catch(...) {
59                 cout << "An unknown error has occurred in the ClusterCommand class function ClusterCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
60                 exit(1);
61         }
62 }
63 //**********************************************************************************************************************
64
65 ClusterCommand::~ClusterCommand(){
66         delete cluster;
67         delete matrix;
68         delete list;
69         delete rabund;
70 }
71
72 //**********************************************************************************************************************
73
74 int ClusterCommand::execute(){
75         try {
76                 float previousDist = 0.00000;
77                 float rndPreviousDist = 0.00000;
78                 oldRAbund = *rabund;
79                 oldList = *list;
80                 
81                 float x;
82                 x=0.1;
83                 toString(x, 2);
84         
85                 while(matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
86                         cluster->update();
87                         float dist = matrix->getSmallDist();
88                         float rndDist = roundDist(dist, precision);
89
90                         if(previousDist <= 0.0000 && dist != previousDist){
91                                 printData("unique");
92                         }
93                         else if(rndDist != rndPreviousDist){
94                                 printData(toString(rndPreviousDist,  length-1));
95                         }
96                 
97                         previousDist = dist;
98                         rndPreviousDist = rndDist;
99                         oldRAbund = *rabund;
100                         oldList = *list;
101                 }
102         
103                 if(previousDist <= 0.0000){
104                         printData("unique");
105                 }
106                 else if(rndPreviousDist<cutoff){
107                         printData(toString(rndPreviousDist, length-1));
108                 }
109                 
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;
113                 
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(""); }
117                 
118                 globaldata->setListFile(fileroot+ tag + ".list");
119                 globaldata->setNameFile("");
120                 globaldata->setFormat("list");
121                 
122                 return 0;
123         }
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";
126                 exit(1);
127         }
128         catch(...) {
129                 cout << "An unknown error has occurred in the ClusterCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
130                 exit(1);
131         }
132
133 }
134
135 //**********************************************************************************************************************
136
137 void ClusterCommand::printData(string label){
138         try {
139                 oldRAbund.setLabel(label);
140                 oldRAbund.getSAbundVector().print(cout);
141                 oldRAbund.print(rabundFile);
142                 oldRAbund.getSAbundVector().print(sabundFile);
143         
144                 oldList.setLabel(label);
145                 oldList.print(listFile);
146         }
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";
149                 exit(1);
150         }
151         catch(...) {
152                 cout << "An unknown error has occurred in the ClusterCommand class function printData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
153                 exit(1);
154         }
155
156 }
157 //**********************************************************************************************************************