]> git.donarmstrong.com Git - mothur.git/blob - clustercommand.cpp
added phylo.diversity command. added hard parameter to cluster, hcluster and read...
[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                         if (!hard) {    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 = roundDist(dist, precision);
185
186                         if(previousDist <= 0.0000 && dist != previousDist){
187                                 printData("unique");
188                         }
189                         else if(rndDist != rndPreviousDist){
190                                 printData(toString(rndPreviousDist,  length-1));
191                         }
192                 
193                         previousDist = dist;
194                         rndPreviousDist = rndDist;
195                         oldRAbund = *rabund;
196                         oldList = *list;
197                 }
198
199                 if (print_start && isTrue(timing)) {
200                         m->mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist) 
201                                          + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
202                         cout.flush();
203                         print_start = false;
204                 }
205         
206                 if(previousDist <= 0.0000){
207                         printData("unique");
208                 }
209                 else if(rndPreviousDist<cutoff){
210                         printData(toString(rndPreviousDist, length-1));
211                 }
212                 
213                 //delete globaldata's copy of the sparsematrix and listvector to free up memory
214                 delete globaldata->gSparseMatrix;  globaldata->gSparseMatrix = NULL;
215                 delete globaldata->gListVector;  globaldata->gListVector = NULL;
216         
217                 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
218                 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
219                 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
220                 
221                 globaldata->setListFile(fileroot+ tag + ".list");
222                 globaldata->setNameFile("");
223                 globaldata->setFormat("list");
224                 
225                 sabundFile.close();
226                 rabundFile.close();
227                 listFile.close();
228         
229                 if (saveCutoff != cutoff) { m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine();  }
230                 
231                 m->mothurOutEndLine();
232                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
233                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
234                 m->mothurOutEndLine();
235
236                 
237                 //if (isTrue(timing)) {
238                         m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
239                 //}
240                 
241                 
242                 return 0;
243         }
244         catch(exception& e) {
245                 m->errorOut(e, "ClusterCommand", "execute");
246                 exit(1);
247         }
248 }
249
250 //**********************************************************************************************************************
251
252 void ClusterCommand::printData(string label){
253         try {
254                 if (isTrue(timing)) {
255                         m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins()) 
256                      + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
257                 }
258                 print_start = true;
259                 loops = 0;
260                 start = time(NULL);
261
262                 oldRAbund.setLabel(label);
263                 if (isTrue(showabund)) {
264                         oldRAbund.getSAbundVector().print(cout);
265                 }
266                 oldRAbund.print(rabundFile);
267                 oldRAbund.getSAbundVector().print(sabundFile);
268         
269                 oldList.setLabel(label);
270                 oldList.print(listFile);
271         }
272         catch(exception& e) {
273                 m->errorOut(e, "ClusterCommand", "printData");
274                 exit(1);
275         }
276
277
278 }
279 //**********************************************************************************************************************