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