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