]> git.donarmstrong.com Git - mothur.git/blob - hclustercommand.cpp
moved utilities out of mothur.h and into mothurOut class.
[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","hard","precision","method","phylip","column","name","sorted","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                         map<string,string>::iterator it;
32                 
33                         //check to make sure all parameters are valid for command
34                         for (it = parameters.begin(); it != parameters.end(); it++) { 
35                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
36                                         abort = true;
37                                 }
38                         }
39                         
40                         globaldata->newRead();
41                         
42                         //if the user changes the input directory command factory will send this info to us in the output parameter 
43                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
44                         if (inputDir == "not found"){   inputDir = "";          }
45                         else {
46                                 string path;
47                                 it = parameters.find("phylip");
48                                 //user has given a template file
49                                 if(it != parameters.end()){ 
50                                         path = m->hasPath(it->second);
51                                         //if the user has not given a path then, add inputdir. else leave path alone.
52                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
53                                 }
54                                 
55                                 it = parameters.find("column");
56                                 //user has given a template file
57                                 if(it != parameters.end()){ 
58                                         path = m->hasPath(it->second);
59                                         //if the user has not given a path then, add inputdir. else leave path alone.
60                                         if (path == "") {       parameters["column"] = inputDir + it->second;           }
61                                 }
62                                 
63                                 it = parameters.find("name");
64                                 //user has given a template file
65                                 if(it != parameters.end()){ 
66                                         path = m->hasPath(it->second);
67                                         //if the user has not given a path then, add inputdir. else leave path alone.
68                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
69                                 }
70                         }
71
72                         //if the user changes the output directory command factory will send this info to us in the output parameter 
73                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
74                         
75                         //check for required parameters
76                         phylipfile = validParameter.validFile(parameters, "phylip", true);
77                         if (phylipfile == "not open") { abort = true; }
78                         else if (phylipfile == "not found") { phylipfile = ""; }        
79                         else {  distfile = phylipfile;  format = "phylip";      }
80                         
81                         columnfile = validParameter.validFile(parameters, "column", true);
82                         if (columnfile == "not open") { abort = true; } 
83                         else if (columnfile == "not found") { columnfile = ""; }
84                         else {  distfile = columnfile; format = "column";       }
85                         
86                         namefile = validParameter.validFile(parameters, "name", true);
87                         if (namefile == "not open") { abort = true; }   
88                         else if (namefile == "not found") { namefile = ""; }
89                         
90                         if ((phylipfile == "") && (columnfile == "")) { m->mothurOut("When executing a hcluster command you must enter a phylip or a column."); m->mothurOutEndLine(); abort = true; }
91                         else if ((phylipfile != "") && (columnfile != "")) { m->mothurOut("When executing a hcluster command you must enter ONLY ONE of the following: phylip or column."); m->mothurOutEndLine(); abort = true; }
92                 
93                         if (columnfile != "") {
94                                 if (namefile == "") {  cout << "You need to provide a namefile if you are going to use the column format." << endl; abort = true; }
95                         }
96                         
97                         //check for optional parameter and set defaults
98                         // ...at some point should added some additional type checking...
99                         //get user cutoff and precision or use defaults
100                         string temp;
101                         temp = validParameter.validFile(parameters, "precision", false);
102                         if (temp == "not found") { temp = "100"; }
103                         //saves precision legnth for formatting below
104                         length = temp.length();
105                         convert(temp, precision); 
106                         
107                         temp = validParameter.validFile(parameters, "hard", false);                     if (temp == "not found") { temp = "F"; }
108                         hard = m->isTrue(temp);
109                         
110                         temp = validParameter.validFile(parameters, "cutoff", false);
111                         if (temp == "not found") { temp = "10"; }
112                         convert(temp, cutoff); 
113                         cutoff += (5 / (precision * 10.0)); 
114                         
115                         method = validParameter.validFile(parameters, "method", false);
116                         if (method == "not found") { method = "furthest"; }
117                         
118                         if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
119                         else { m->mothurOut("Not a valid clustering method.  Valid clustering algorithms are furthest, nearest or average."); m->mothurOutEndLine(); abort = true; }
120
121                         showabund = validParameter.validFile(parameters, "showabund", false);
122                         if (showabund == "not found") { showabund = "T"; }
123                         
124                         sort = validParameter.validFile(parameters, "sorted", false);
125                         if (sort == "not found") { sort = "F"; }
126                         sorted = m->isTrue(sort);
127
128                         timing = validParameter.validFile(parameters, "timing", false);
129                         if (timing == "not found") { timing = "F"; }
130                         
131                                 
132                         if (abort == false) {
133                                 
134                                 if (outputDir == "") {  outputDir += m->hasPath(distfile); }
135                                 fileroot = outputDir + m->getRootName(m->getSimpleName(distfile));
136                                 
137                                 if (method == "furthest")               { tag = "fn";  }
138                                 else if (method == "nearest")   { tag = "nn";  }
139                                 else                                                    { tag = "an";  }
140                         
141                                 m->openOutputFile(fileroot+ tag + ".sabund",    sabundFile);
142                                 m->openOutputFile(fileroot+ tag + ".rabund",    rabundFile);
143                                 m->openOutputFile(fileroot+ tag + ".list",              listFile);
144                                 
145                                 outputNames.push_back(fileroot+ tag + ".sabund");
146                                 outputNames.push_back(fileroot+ tag + ".rabund");
147                                 outputNames.push_back(fileroot+ tag + ".list");
148                         }
149                 }
150         }
151         catch(exception& e) {
152                 m->errorOut(e, "HClusterCommand", "HClusterCommand");
153                 exit(1);
154         }
155 }
156
157 //**********************************************************************************************************************
158
159 void HClusterCommand::help(){
160         try {
161                 m->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");
162                 m->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");
163                 m->mothurOut("The name parameter allows you to enter your name file and is required if your distance file is in column format. \n");
164                 m->mothurOut("The hcluster command should be in the following format: \n");
165                 m->mothurOut("hcluster(column=youDistanceFile, name=yourNameFile, method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
166                 m->mothurOut("The acceptable hcluster methods are furthest, nearest and average.\n\n"); 
167         }
168         catch(exception& e) {
169                 m->errorOut(e, "HClusterCommand", "help");
170                 exit(1);
171         }
172 }
173
174 //**********************************************************************************************************************
175
176 HClusterCommand::~HClusterCommand(){}
177
178 //**********************************************************************************************************************
179
180 int HClusterCommand::execute(){
181         try {
182         
183                 if (abort == true) {    return 0;       }
184                 
185                 if(namefile != ""){     
186                         globaldata->nameMap = new NameAssignment(namefile);
187                         globaldata->nameMap->readMap();
188                 }else{
189                         globaldata->nameMap = NULL;
190                 }
191                 
192                 time_t estart = time(NULL);
193                 
194                 if (!sorted) {
195                         read = new ReadCluster(distfile, cutoff, outputDir, true);      
196                         read->setFormat(format);
197                         read->read(globaldata->nameMap);
198                         
199                         if (m->control_pressed) {  
200                                 delete read; 
201                                 sabundFile.close();
202                                 rabundFile.close();
203                                 listFile.close();
204                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  }
205                                 return 0;  
206                         }
207                         
208                         distfile = read->getOutputFile();
209                 
210                         list = read->getListVector();
211                         delete read;
212                 }else {
213                         list = new ListVector(globaldata->nameMap->getListVector());
214                 }
215                 
216                 if (m->control_pressed) {  
217                         sabundFile.close();
218                         rabundFile.close();
219                         listFile.close();
220                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  }
221                         return 0;  
222                 }
223
224                 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to sort. "); m->mothurOutEndLine();
225                 estart = time(NULL);
226         
227                 //list vector made by read contains all sequence names
228                 if(list != NULL){
229                         rabund = new RAbundVector(list->getRAbundVector());
230                 }else{
231                         m->mothurOut("Error: no list vector!"); m->mothurOutEndLine(); return 0;
232                 }
233                 
234                 float previousDist = 0.00000;
235                 float rndPreviousDist = 0.00000;
236                 oldRAbund = *rabund;
237                 oldList = *list;
238                 
239                 print_start = true;
240                 start = time(NULL);
241                                 
242                 cluster = new HCluster(rabund, list, method, distfile, globaldata->nameMap, cutoff);
243                 vector<seqDist> seqs; seqs.resize(1); // to start loop
244                 
245                 if (m->control_pressed) {  
246                                 delete cluster;
247                                 sabundFile.close();
248                                 rabundFile.close();
249                                 listFile.close();
250                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  }
251                                 return 0;  
252                 }
253
254                 
255                 while (seqs.size() != 0){
256                 
257                         seqs = cluster->getSeqs();
258                         
259                         if (m->control_pressed) {  
260                                 delete cluster;
261                                 sabundFile.close();
262                                 rabundFile.close();
263                                 listFile.close();
264                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  }
265                                 return 0;  
266                         }
267
268                         for (int i = 0; i < seqs.size(); i++) {  //-1 means skip me
269                                 
270                                 if (seqs[i].seq1 != seqs[i].seq2) {
271                                         cluster->update(seqs[i].seq1, seqs[i].seq2, seqs[i].dist);
272                                         
273                                         if (m->control_pressed) {  
274                                                 delete cluster;
275                                                 sabundFile.close();
276                                                 rabundFile.close();
277                                                 listFile.close();
278                                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  }
279                                                 return 0;  
280                                         }
281
282                         
283                                         float rndDist;
284                                         if (hard) {
285                                                 rndDist = m->ceilDist(seqs[i].dist, precision); 
286                                         }else{
287                                                 rndDist = m->roundDist(seqs[i].dist, precision); 
288                                         }
289
290                                         
291                                         if((previousDist <= 0.0000) && (seqs[i].dist != previousDist)){
292                                                 printData("unique");
293                                         }
294                                         else if((rndDist != rndPreviousDist)){
295                                                 printData(toString(rndPreviousDist,  length-1));
296                                         }
297                                 
298                                         previousDist = seqs[i].dist;
299                                         rndPreviousDist = rndDist;
300                                         oldRAbund = *rabund;
301                                         oldList = *list;
302                                 }
303                         }
304                 }
305
306                 if (m->control_pressed) {  
307                         delete cluster;
308                         sabundFile.close();
309                         rabundFile.close();
310                         listFile.close();
311                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  }
312                         return 0;  
313                 }
314                                         
315                 if(previousDist <= 0.0000){
316                         printData("unique");
317                 }
318                 else if(rndPreviousDist<cutoff){
319                         printData(toString(rndPreviousDist, length-1));
320                 }
321                 
322                 //delete globaldata's copy of the sparsematrix and listvector to free up memory
323                 delete globaldata->gListVector;  globaldata->gListVector = NULL;
324                 
325                 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
326                 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
327                 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
328                 
329                 globaldata->setListFile(fileroot+ tag + ".list");
330                 globaldata->setNameFile("");
331                 globaldata->setFormat("list");
332                 
333                 sabundFile.close();
334                 rabundFile.close();
335                 listFile.close();
336                 delete cluster;
337                 
338                 if (m->control_pressed) {  
339                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  }
340                         return 0;  
341                 }
342
343                 
344                 m->mothurOutEndLine();
345                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
346                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
347                 m->mothurOutEndLine();
348                 
349                 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster. "); m->mothurOutEndLine();
350                 
351                 return 0;
352         }
353         catch(exception& e) {
354                 m->errorOut(e, "HClusterCommand", "execute");
355                 exit(1);
356         }
357 }
358
359 //**********************************************************************************************************************
360
361 void HClusterCommand::printData(string label){
362         try {
363                 if (m->isTrue(timing)) {
364                         m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins()) 
365                      + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
366                 }
367                 print_start = true;
368                 loops = 0;
369                 start = time(NULL);
370
371                 oldRAbund.setLabel(label);
372                 if (m->isTrue(showabund)) {
373                         oldRAbund.getSAbundVector().print(cout);
374                 }
375                 oldRAbund.print(rabundFile);
376                 oldRAbund.getSAbundVector().print(sabundFile);
377         
378                 oldList.setLabel(label);
379                 oldList.print(listFile);
380         }
381         catch(exception& e) {
382                 m->errorOut(e, "HClusterCommand", "printData");
383                 exit(1);
384         }
385
386
387 }
388 //**********************************************************************************************************************
389