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