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