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