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