]> git.donarmstrong.com Git - mothur.git/blob - clustercommand.cpp
modified calculators to use doubles, added numotu and fontsize parameters to heatmap...
[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(string option)  {
15         try{
16                 globaldata = GlobalData::getInstance();
17                 
18                 abort = false;
19                 
20                 //allow user to run help
21                 if(option == "help") { help(); abort = true; }
22                 
23                 else {
24                         //valid paramters for this command
25                         string Array[] =  {"cutoff","precision","method","showabund","timing","hard","outputdir","inputdir"};
26                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27                         
28                         OptionParser parser(option);
29                         map<string,string> parameters = parser.getParameters();
30                         
31                         ValidParameters validParameter;
32                 
33                         //check to make sure all parameters are valid for command
34                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
35                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
36                                         abort = true;
37                                 }
38                         }
39                         
40                         //if the user changes the output directory command factory will send this info to us in the output parameter 
41                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
42                         
43                         //error checking to make sure they read a distance file
44                         if ((globaldata->gSparseMatrix == NULL) || (globaldata->gListVector == NULL)) {
45                                 m->mothurOut("Before you use the cluster command, you first need to read in a distance matrix."); m->mothurOutEndLine();
46                                 abort = true;
47                         } 
48                 
49                         //check for optional parameter and set defaults
50                         // ...at some point should added some additional type checking...
51                         //get user cutoff and precision or use defaults
52                         string temp;
53                         temp = validParameter.validFile(parameters, "precision", false);
54                         if (temp == "not found") { temp = "100"; }
55                         //saves precision legnth for formatting below
56                         length = temp.length();
57                         convert(temp, precision); 
58                         
59                         temp = validParameter.validFile(parameters, "hard", false);                     if (temp == "not found") { temp = "F"; }
60                         hard = isTrue(temp);
61                         
62                         temp = validParameter.validFile(parameters, "cutoff", false);
63                         if (temp == "not found") { temp = "10"; }
64                         convert(temp, cutoff); 
65                         cutoff += (5 / (precision * 10.0));  
66                         
67                         method = validParameter.validFile(parameters, "method", false);
68                         if (method == "not found") { method = "furthest"; }
69                         
70                         if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { }
71                         else { m->mothurOut("Not a valid clustering method.  Valid clustering algorithms are furthest, nearest, average, and weighted."); m->mothurOutEndLine(); abort = true; }
72
73                         showabund = validParameter.validFile(parameters, "showabund", false);
74                         if (showabund == "not found") { showabund = "T"; }
75
76                         timing = validParameter.validFile(parameters, "timing", false);
77                         if (timing == "not found") { timing = "F"; }
78                         
79                         if (abort == false) {
80                         
81         
82                                                         //get matrix, list and rabund for execute
83                                 if(globaldata->gSparseMatrix != NULL)   {       matrix = globaldata->gSparseMatrix;             }
84                         
85                                 if(globaldata->gListVector != NULL){
86                                         list = globaldata->gListVector;
87                                         rabund = new RAbundVector(list->getRAbundVector());
88                                 }
89                                 
90                                 //create cluster
91                                 if (method == "furthest")       {       cluster = new CompleteLinkage(rabund, list, matrix, cutoff, method); }
92                                 else if(method == "nearest"){   cluster = new SingleLinkage(rabund, list, matrix, cutoff, method); }
93                                 else if(method == "average"){   cluster = new AverageLinkage(rabund, list, matrix, cutoff, method);     }
94                                 else if(method == "weighted"){  cluster = new WeightedLinkage(rabund, list, matrix, cutoff, method);    }
95                                 tag = cluster->getTag();
96                                 
97                                 if (outputDir == "") { outputDir += hasPath(globaldata->inputFileName); }
98                                 fileroot = outputDir + getRootName(getSimpleName(globaldata->inputFileName));
99                         
100                                 openOutputFile(fileroot+ tag + ".sabund",       sabundFile);
101                                 openOutputFile(fileroot+ tag + ".rabund",       rabundFile);
102                                 openOutputFile(fileroot+ tag + ".list",         listFile);
103                                 
104                                 outputNames.push_back(fileroot+ tag + ".sabund");
105                                 outputNames.push_back(fileroot+ tag + ".rabund");
106                                 outputNames.push_back(fileroot+ tag + ".list");
107                         }
108                 }
109         }
110         catch(exception& e) {
111                 m->errorOut(e, "ClusterCommand", "ClusterCommand");
112                 exit(1);
113         }
114 }
115
116 //**********************************************************************************************************************
117
118 void ClusterCommand::help(){
119         try {
120                 m->mothurOut("The cluster command can only be executed after a successful read.dist command.\n");
121                 m->mothurOut("The cluster command parameter options are method, cuttoff, hard, precision, showabund and timing. No parameters are required.\n");
122                 m->mothurOut("The cluster command should be in the following format: \n");
123                 m->mothurOut("cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
124                 m->mothurOut("The acceptable cluster methods are furthest, nearest and average.  If no method is provided then furthest is assumed.\n\n");      
125         }
126         catch(exception& e) {
127                 m->errorOut(e, "ClusterCommand", "help");
128                 exit(1);
129         }
130 }
131
132 //**********************************************************************************************************************
133
134 ClusterCommand::~ClusterCommand(){
135         if (abort == false) {
136                 delete cluster;
137                 delete rabund;
138         }
139 }
140
141 //**********************************************************************************************************************
142
143 int ClusterCommand::execute(){
144         try {
145         
146                 if (abort == true) {    return 0;       }
147                 
148                 time_t estart = time(NULL);
149                 //int ndist = matrix->getNNodes();
150                 float previousDist = 0.00000;
151                 float rndPreviousDist = 0.00000;
152                 oldRAbund = *rabund;
153                 oldList = *list;
154
155                 print_start = true;
156                 start = time(NULL);
157                 loops = 0;
158                 double saveCutoff = cutoff;
159                 
160                 while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
161                 
162                         if (m->control_pressed) { //clean up
163                                 delete globaldata->gSparseMatrix;  globaldata->gSparseMatrix = NULL;
164                                 delete globaldata->gListVector;  globaldata->gListVector = NULL;
165                                 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
166                                 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
167                                 sabundFile.close();rabundFile.close();listFile.close();
168                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
169                                 return 0;
170                         }
171                 
172                         if (print_start && isTrue(timing)) {
173                                 m->mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/" 
174                                         + toString(roundDist(matrix->getSmallDist(), precision)) 
175                                         + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
176                                 cout.flush();
177                                 print_start = false;
178                         }
179
180                         loops++;
181
182                         cluster->update(cutoff);
183         
184                         float dist = matrix->getSmallDist();
185                         float rndDist;
186                         if (hard) {
187                                 rndDist = ceilDist(dist, precision); 
188                         }else{
189                                 rndDist = roundDist(dist, precision); 
190                         }
191
192                         if(previousDist <= 0.0000 && dist != previousDist){
193                                 printData("unique");
194                         }
195                         else if(rndDist != rndPreviousDist){
196                                 printData(toString(rndPreviousDist,  length-1));
197                         }
198                 
199                         previousDist = dist;
200                         rndPreviousDist = rndDist;
201                         oldRAbund = *rabund;
202                         oldList = *list;
203                 }
204
205                 if (print_start && isTrue(timing)) {
206                         m->mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist) 
207                                          + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
208                         cout.flush();
209                         print_start = false;
210                 }
211         
212                 if(previousDist <= 0.0000){
213                         printData("unique");
214                 }
215                 else if(rndPreviousDist<cutoff){
216                         printData(toString(rndPreviousDist, length-1));
217                 }
218                 
219                 //delete globaldata's copy of the sparsematrix and listvector to free up memory
220                 delete globaldata->gSparseMatrix;  globaldata->gSparseMatrix = NULL;
221                 delete globaldata->gListVector;  globaldata->gListVector = NULL;
222         
223                 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
224                 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
225                 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
226                 
227                 globaldata->setListFile(fileroot+ tag + ".list");
228                 globaldata->setNameFile("");
229                 globaldata->setFormat("list");
230                 
231                 sabundFile.close();
232                 rabundFile.close();
233                 listFile.close();
234         
235                 if (saveCutoff != cutoff) { 
236                         if (hard)       {  saveCutoff = ceilDist(saveCutoff, precision);        }
237                         else            {       saveCutoff = roundDist(saveCutoff, precision);  }
238
239                         m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine(); 
240                 }
241                 
242                 m->mothurOutEndLine();
243                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
244                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
245                 m->mothurOutEndLine();
246
247                 
248                 //if (isTrue(timing)) {
249                         m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
250                 //}
251                 
252                 
253                 return 0;
254         }
255         catch(exception& e) {
256                 m->errorOut(e, "ClusterCommand", "execute");
257                 exit(1);
258         }
259 }
260
261 //**********************************************************************************************************************
262
263 void ClusterCommand::printData(string label){
264         try {
265                 if (isTrue(timing)) {
266                         m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins()) 
267                      + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
268                 }
269                 print_start = true;
270                 loops = 0;
271                 start = time(NULL);
272
273                 oldRAbund.setLabel(label);
274                 if (isTrue(showabund)) {
275                         oldRAbund.getSAbundVector().print(cout);
276                 }
277                 oldRAbund.print(rabundFile);
278                 oldRAbund.getSAbundVector().print(sabundFile);
279         
280                 oldList.setLabel(label);
281                 oldList.print(listFile);
282         }
283         catch(exception& e) {
284                 m->errorOut(e, "ClusterCommand", "printData");
285                 exit(1);
286         }
287
288
289 }
290 //**********************************************************************************************************************