]> git.donarmstrong.com Git - mothur.git/blob - hclustercommand.cpp
precluster command finished
[mothur.git] / hclustercommand.cpp
1 /*
2  *  hclustercommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 10/13/09.
6  *  Copyright 2009 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "hclustercommand.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 HClusterCommand::HClusterCommand(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","phylip","column","name","sorted","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                         globaldata->newRead();
40                         
41                         //check for required parameters
42                         phylipfile = validParameter.validFile(parameters, "phylip", true);
43                         if (phylipfile == "not open") { abort = true; }
44                         else if (phylipfile == "not found") { phylipfile = ""; }        
45                         else {  distfile = phylipfile;  format = "phylip";      }
46                         
47                         columnfile = validParameter.validFile(parameters, "column", true);
48                         if (columnfile == "not open") { abort = true; } 
49                         else if (columnfile == "not found") { columnfile = ""; }
50                         else {  distfile = columnfile; format = "column";       }
51                         
52                         namefile = validParameter.validFile(parameters, "name", true);
53                         if (namefile == "not open") { abort = true; }   
54                         else if (namefile == "not found") { namefile = ""; }
55                         
56                         if ((phylipfile == "") && (columnfile == "")) { mothurOut("When executing a hcluster command you must enter a phylip or a column."); mothurOutEndLine(); abort = true; }
57                         else if ((phylipfile != "") && (columnfile != "")) { mothurOut("When executing a hcluster command you must enter ONLY ONE of the following: phylip or column."); mothurOutEndLine(); abort = true; }
58                 
59                         if (columnfile != "") {
60                                 if (namefile == "") {  cout << "You need to provide a namefile if you are going to use the column format." << endl; abort = true; }
61                         }
62                         
63                         //check for optional parameter and set defaults
64                         // ...at some point should added some additional type checking...
65                         //get user cutoff and precision or use defaults
66                         string temp;
67                         temp = validParameter.validFile(parameters, "precision", false);
68                         if (temp == "not found") { temp = "100"; }
69                         //saves precision legnth for formatting below
70                         length = temp.length();
71                         convert(temp, precision); 
72                         
73                         temp = validParameter.validFile(parameters, "cutoff", false);
74                         if (temp == "not found") { temp = "10"; }
75                         convert(temp, cutoff); 
76                         cutoff += (5 / (precision * 10.0));
77                         
78                         method = validParameter.validFile(parameters, "method", false);
79                         if (method == "not found") { method = "furthest"; }
80                         
81                         if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
82                         else { mothurOut("Not a valid clustering method.  Valid clustering algorithms are furthest, nearest or average."); mothurOutEndLine(); abort = true; }
83
84                         showabund = validParameter.validFile(parameters, "showabund", false);
85                         if (showabund == "not found") { showabund = "T"; }
86                         
87                         sort = validParameter.validFile(parameters, "sorted", false);
88                         if (sort == "not found") { sort = "F"; }
89                         sorted = isTrue(sort);
90
91                         timing = validParameter.validFile(parameters, "timing", false);
92                         if (timing == "not found") { timing = "F"; }
93                         
94                                 
95                         if (abort == false) {
96                                                                                         
97                                 fileroot = getRootName(distfile);
98                                 
99                                 if (method == "furthest")               { tag = "fn";  }
100                                 else if (method == "nearest")   { tag = "nn";  }
101                                 else                                                    { tag = "an";  }
102                         
103                                 openOutputFile(fileroot+ tag + ".sabund",       sabundFile);
104                                 openOutputFile(fileroot+ tag + ".rabund",       rabundFile);
105                                 openOutputFile(fileroot+ tag + ".list",         listFile);
106                         }
107                 }
108         }
109         catch(exception& e) {
110                 errorOut(e, "HClusterCommand", "HClusterCommand");
111                 exit(1);
112         }
113 }
114
115 //**********************************************************************************************************************
116
117 void HClusterCommand::help(){
118         try {
119                 mothurOut("The hcluster command parameter options are cutoff, precision, method, phylip, column, name, showabund, timing and sorted. Phylip or column and name are required.\n");
120                 mothurOut("The phylip and column parameter allow you to enter your distance file, and sorted indicates whether your column distance file is already sorted. \n");
121                 mothurOut("The name parameter allows you to enter your name file and is required if your distance file is in column format. \n");
122                 mothurOut("The hcluster command should be in the following format: \n");
123                 mothurOut("hcluster(column=youDistanceFile, name=yourNameFile, method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
124                 mothurOut("The acceptable hcluster methods are furthest and nearest, but we hope to add average in the future.\n\n");   
125         }
126         catch(exception& e) {
127                 errorOut(e, "HClusterCommand", "help");
128                 exit(1);
129         }
130 }
131
132 //**********************************************************************************************************************
133
134 HClusterCommand::~HClusterCommand(){}
135
136 //**********************************************************************************************************************
137
138 int HClusterCommand::execute(){
139         try {
140         
141                 if (abort == true) {    return 0;       }
142                 
143                 if(namefile != ""){     
144                         globaldata->nameMap = new NameAssignment(namefile);
145                         globaldata->nameMap->readMap();
146                 }else{
147                         globaldata->nameMap = NULL;
148                 }
149                 
150                 time_t estart = time(NULL);
151                 
152                 if (!sorted) {
153                         read = new ReadCluster(distfile, cutoff);       
154                         read->setFormat(format);
155                         read->read(globaldata->nameMap);
156                         distfile = read->getOutputFile();
157                 
158                         list = read->getListVector();
159                         delete read;
160                 }else {
161                         list = new ListVector(globaldata->nameMap->getListVector());
162                 }
163         
164                 mothurOut("It took " + toString(time(NULL) - estart) + " seconds to sort. "); mothurOutEndLine();
165                 estart = time(NULL);
166         
167                 //list vector made by read contains all sequence names
168                 if(list != NULL){
169                         rabund = new RAbundVector(list->getRAbundVector());
170                 }else{
171                         mothurOut("Error: no list vector!"); mothurOutEndLine(); return 0;
172                 }
173                 
174                 float previousDist = 0.00000;
175                 float rndPreviousDist = 0.00000;
176                 oldRAbund = *rabund;
177                 oldList = *list;
178                 
179                 print_start = true;
180                 start = time(NULL);
181         
182                 ifstream in;
183                 openInputFile(distfile, in);
184                 string firstName, secondName;
185                 float distance;
186                 
187                 cluster = new HCluster(rabund, list, method);
188                 vector<seqDist> seqs; seqs.resize(1); // to start loop
189                 
190                 while (seqs.size() != 0){
191                 
192                         seqs = cluster->getSeqs(in, globaldata->nameMap, cutoff);
193                                 
194                         for (int i = 0; i < seqs.size(); i++) {  //-1 means skip me
195
196                                 if (print_start && isTrue(timing)) {
197                                         mothurOut("Clustering (" + tag + ") dist " + toString(distance) + "/" 
198                                                           + toString(roundDist(distance, precision)) 
199                                                           + "\t(precision: " + toString(precision) + ")");
200                                         cout.flush();
201                                         print_start = false;
202                                 }
203                                 
204         
205                                 if (seqs[i].seq1 != seqs[i].seq2) {
206                                         cluster->update(seqs[i].seq1, seqs[i].seq2, seqs[i].dist);
207                                         
208                                         float rndDist = roundDist(seqs[i].dist, precision);
209                                                         
210                                         if((previousDist <= 0.0000) && (seqs[i].dist != previousDist)){
211                                                 printData("unique");
212                                         }
213                                         else if((rndDist != rndPreviousDist)){
214                                                 printData(toString(rndPreviousDist,  length-1));
215                                         }
216                                         
217                                         previousDist = seqs[i].dist;
218                                         rndPreviousDist = rndDist;
219                                         oldRAbund = *rabund;
220                                         oldList = *list;
221                                 }
222                         }
223                 }
224                 
225                 in.close();
226
227                 if (print_start && isTrue(timing)) {
228                         //mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist) 
229                                          //+ "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
230                         cout.flush();
231                         print_start = false;
232                 }
233         
234                 if(previousDist <= 0.0000){
235                         printData("unique");
236                 }
237                 else if(rndPreviousDist<cutoff){
238                         printData(toString(rndPreviousDist, length-1));
239                 }
240                 
241                 //delete globaldata's copy of the sparsematrix and listvector to free up memory
242                 delete globaldata->gListVector;  globaldata->gListVector = NULL;
243                 
244                 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
245                 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
246                 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
247                 
248                 globaldata->setListFile(fileroot+ tag + ".list");
249                 globaldata->setNameFile("");
250                 globaldata->setFormat("list");
251                 
252                 sabundFile.close();
253                 rabundFile.close();
254                 listFile.close();
255                 delete cluster;
256         
257                 mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster. "); mothurOutEndLine();
258                 
259                 return 0;
260         }
261         catch(exception& e) {
262                 errorOut(e, "HClusterCommand", "execute");
263                 exit(1);
264         }
265 }
266
267 //**********************************************************************************************************************
268
269 void HClusterCommand::printData(string label){
270         try {
271                 if (isTrue(timing)) {
272                         mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins()) 
273                      + "\tclusters. Updates: " + toString(loops)); mothurOutEndLine();
274                 }
275                 print_start = true;
276                 loops = 0;
277                 start = time(NULL);
278
279                 oldRAbund.setLabel(label);
280                 if (isTrue(showabund)) {
281                         oldRAbund.getSAbundVector().print(cout);
282                 }
283                 oldRAbund.print(rabundFile);
284                 oldRAbund.getSAbundVector().print(sabundFile);
285         
286                 oldList.setLabel(label);
287                 oldList.print(listFile);
288         }
289         catch(exception& e) {
290                 errorOut(e, "HClusterCommand", "printData");
291                 exit(1);
292         }
293
294
295 }
296 //**********************************************************************************************************************
297