]> git.donarmstrong.com Git - mothur.git/blob - clustercommand.cpp
changed hard parameter in cluster commands
[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")) { }
71                         else { m->mothurOut("Not a valid clustering method.  Valid clustering algorithms are furthest, nearest or average."); 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                                 tag = cluster->getTag();
95                                 
96                                 if (outputDir == "") { outputDir += hasPath(globaldata->inputFileName); }
97                                 fileroot = outputDir + getRootName(getSimpleName(globaldata->inputFileName));
98                         
99                                 openOutputFile(fileroot+ tag + ".sabund",       sabundFile);
100                                 openOutputFile(fileroot+ tag + ".rabund",       rabundFile);
101                                 openOutputFile(fileroot+ tag + ".list",         listFile);
102                                 
103                                 outputNames.push_back(fileroot+ tag + ".sabund");
104                                 outputNames.push_back(fileroot+ tag + ".rabund");
105                                 outputNames.push_back(fileroot+ tag + ".list");
106                         }
107                 }
108         }
109         catch(exception& e) {
110                 m->errorOut(e, "ClusterCommand", "ClusterCommand");
111                 exit(1);
112         }
113 }
114
115 //**********************************************************************************************************************
116
117 void ClusterCommand::help(){
118         try {
119                 m->mothurOut("The cluster command can only be executed after a successful read.dist command.\n");
120                 m->mothurOut("The cluster command parameter options are method, cuttoff, hard, precision, showabund and timing. No parameters are required.\n");
121                 m->mothurOut("The cluster command should be in the following format: \n");
122                 m->mothurOut("cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
123                 m->mothurOut("The acceptable cluster methods are furthest, nearest and average.  If no method is provided then furthest is assumed.\n\n");      
124         }
125         catch(exception& e) {
126                 m->errorOut(e, "ClusterCommand", "help");
127                 exit(1);
128         }
129 }
130
131 //**********************************************************************************************************************
132
133 ClusterCommand::~ClusterCommand(){
134         if (abort == false) {
135                 delete cluster;
136                 delete rabund;
137         }
138 }
139
140 //**********************************************************************************************************************
141
142 int ClusterCommand::execute(){
143         try {
144         
145                 if (abort == true) {    return 0;       }
146                 
147                 time_t estart = time(NULL);
148                 //int ndist = matrix->getNNodes();
149                 float previousDist = 0.00000;
150                 float rndPreviousDist = 0.00000;
151                 oldRAbund = *rabund;
152                 oldList = *list;
153
154                 print_start = true;
155                 start = time(NULL);
156                 loops = 0;
157                 double saveCutoff = cutoff;
158                 
159                 while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
160                 
161                         if (m->control_pressed) { //clean up
162                                 delete globaldata->gSparseMatrix;  globaldata->gSparseMatrix = NULL;
163                                 delete globaldata->gListVector;  globaldata->gListVector = NULL;
164                                 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
165                                 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
166                                 sabundFile.close();rabundFile.close();listFile.close();
167                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
168                                 return 0;
169                         }
170                 
171                         if (print_start && isTrue(timing)) {
172                                 m->mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/" 
173                                         + toString(roundDist(matrix->getSmallDist(), precision)) 
174                                         + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
175                                 cout.flush();
176                                 print_start = false;
177                         }
178
179                         loops++;
180
181                         cluster->update(cutoff);
182         
183                         float dist = matrix->getSmallDist();
184                         float rndDist;
185                         if (hard) {
186                                 rndDist = ceilDist(dist, precision); 
187                         }else{
188                                 rndDist = roundDist(dist, precision); 
189                         }
190
191                         if(previousDist <= 0.0000 && dist != previousDist){
192                                 printData("unique");
193                         }
194                         else if(rndDist != rndPreviousDist){
195                                 printData(toString(rndPreviousDist,  length-1));
196                         }
197                 
198                         previousDist = dist;
199                         rndPreviousDist = rndDist;
200                         oldRAbund = *rabund;
201                         oldList = *list;
202                 }
203
204                 if (print_start && isTrue(timing)) {
205                         m->mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist) 
206                                          + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
207                         cout.flush();
208                         print_start = false;
209                 }
210         
211                 if(previousDist <= 0.0000){
212                         printData("unique");
213                 }
214                 else if(rndPreviousDist<cutoff){
215                         printData(toString(rndPreviousDist, length-1));
216                 }
217                 
218                 //delete globaldata's copy of the sparsematrix and listvector to free up memory
219                 delete globaldata->gSparseMatrix;  globaldata->gSparseMatrix = NULL;
220                 delete globaldata->gListVector;  globaldata->gListVector = NULL;
221         
222                 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
223                 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
224                 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
225                 
226                 globaldata->setListFile(fileroot+ tag + ".list");
227                 globaldata->setNameFile("");
228                 globaldata->setFormat("list");
229                 
230                 sabundFile.close();
231                 rabundFile.close();
232                 listFile.close();
233         
234                 if (saveCutoff != cutoff) { m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine();  }
235                 
236                 m->mothurOutEndLine();
237                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
238                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
239                 m->mothurOutEndLine();
240
241                 
242                 //if (isTrue(timing)) {
243                         m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
244                 //}
245                 
246                 
247                 return 0;
248         }
249         catch(exception& e) {
250                 m->errorOut(e, "ClusterCommand", "execute");
251                 exit(1);
252         }
253 }
254
255 //**********************************************************************************************************************
256
257 void ClusterCommand::printData(string label){
258         try {
259                 if (isTrue(timing)) {
260                         m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins()) 
261                      + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
262                 }
263                 print_start = true;
264                 loops = 0;
265                 start = time(NULL);
266
267                 oldRAbund.setLabel(label);
268                 if (isTrue(showabund)) {
269                         oldRAbund.getSAbundVector().print(cout);
270                 }
271                 oldRAbund.print(rabundFile);
272                 oldRAbund.getSAbundVector().print(sabundFile);
273         
274                 oldList.setLabel(label);
275                 oldList.print(listFile);
276         }
277         catch(exception& e) {
278                 m->errorOut(e, "ClusterCommand", "printData");
279                 exit(1);
280         }
281
282
283 }
284 //**********************************************************************************************************************