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