]> git.donarmstrong.com Git - mothur.git/blob - clusterdoturcommand.cpp
Merge remote-tracking branch 'mothur/master'
[mothur.git] / clusterdoturcommand.cpp
1 /*
2  *  clusterdoturcommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 10/27/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "clusterdoturcommand.h"
11 #include "clusterclassic.h"
12
13 //**********************************************************************************************************************
14 vector<string> ClusterDoturCommand::setParameters(){    
15         try {
16                 CommandParameter pphylip("phylip", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pphylip);
17                 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
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", "average", "", "", "",false,false); parameters.push_back(pmethod);
21                 CommandParameter phard("hard", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(phard);
22                 CommandParameter psim("sim", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(psim);
23                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
24                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
25                 
26                 vector<string> myArray;
27                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
28                 return myArray;
29         }
30         catch(exception& e) {
31                 m->errorOut(e, "ClusterDoturCommand", "setParameters");
32                 exit(1);
33         }
34 }
35 //**********************************************************************************************************************
36 string ClusterDoturCommand::getHelpString(){    
37         try {
38                 string helpString = "";
39                 helpString += "The cluster.classic command clusters using the algorithm from dotur. \n";
40                 helpString += "The cluster.classic command parameter options are phylip, name, method, cuttoff, hard, sim, precision. Phylip is required, unless you have a valid current file.\n";
41                 helpString += "The cluster.classic command should be in the following format: \n";
42                 helpString += "cluster.classic(phylip=yourDistanceMatrix, method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n";
43                 helpString += "The acceptable cluster methods are furthest, nearest, weighted and average.  If no method is provided then average is assumed.\n";       
44                 return helpString;
45         }
46         catch(exception& e) {
47                 m->errorOut(e, "ClusterDoturCommand", "getHelpString");
48                 exit(1);
49         }
50 }
51 //**********************************************************************************************************************
52 string ClusterDoturCommand::getOutputFileNameTag(string type, string inputName=""){     
53         try {
54         string outputFileName = "";
55                 map<string, vector<string> >::iterator it;
56         
57         //is this a type this command creates
58         it = outputTypes.find(type);
59         if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
60         else {
61             if (type == "list") {  outputFileName =  "list"; }
62             else if (type == "rabund") {  outputFileName =  "rabund"; }
63             else if (type == "sabund") {  outputFileName =  "sabund"; }
64             else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
65         }
66         return outputFileName;
67         }
68         catch(exception& e) {
69                 m->errorOut(e, "ClusterDoturCommand", "getOutputFileNameTag");
70                 exit(1);
71         }
72 }
73 //**********************************************************************************************************************
74 ClusterDoturCommand::ClusterDoturCommand(){     
75         try {
76                 abort = true; calledHelp = true;
77                 setParameters();
78                 vector<string> tempOutNames;
79                 outputTypes["list"] = tempOutNames;
80                 outputTypes["rabund"] = tempOutNames;
81                 outputTypes["sabund"] = tempOutNames;
82         }
83         catch(exception& e) {
84                 m->errorOut(e, "ClusterDoturCommand", "ClusterCommand");
85                 exit(1);
86         }
87 }
88 //**********************************************************************************************************************
89 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
90 ClusterDoturCommand::ClusterDoturCommand(string option)  {
91         try{
92                 
93                 abort = false; calledHelp = false;   
94                 
95                 //allow user to run help
96                 if(option == "help") { help(); abort = true; calledHelp = true; }
97                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
98                 
99                 else {
100                         vector<string> myArray = setParameters();
101                         
102                         OptionParser parser(option);
103                         map<string,string> parameters = parser.getParameters();
104                         
105                         ValidParameters validParameter;
106                 
107                         //check to make sure all parameters are valid for command
108                         map<string,string>::iterator it;
109                         for (it = parameters.begin(); it != parameters.end(); it++) { 
110                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
111                                         abort = true;
112                                 }
113                         }
114                         
115                         //if the user changes the input directory command factory will send this info to us in the output parameter 
116                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
117                         if (inputDir == "not found"){   inputDir = "";          }
118                         else {
119                                 string path;
120                                 it = parameters.find("phylip");
121                                 //user has given a template file
122                                 if(it != parameters.end()){ 
123                                         path = m->hasPath(it->second);
124                                         //if the user has not given a path then, add inputdir. else leave path alone.
125                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
126                                 }
127                                 
128                                 it = parameters.find("name");
129                                 //user has given a template file
130                                 if(it != parameters.end()){ 
131                                         path = m->hasPath(it->second);
132                                         //if the user has not given a path then, add inputdir. else leave path alone.
133                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
134                                 }
135
136                         }
137                         
138                         //initialize outputTypes
139                         vector<string> tempOutNames;
140                         outputTypes["list"] = tempOutNames;
141                         outputTypes["rabund"] = tempOutNames;
142                         outputTypes["sabund"] = tempOutNames;
143                 
144                         //if the user changes the output directory command factory will send this info to us in the output parameter 
145                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
146                         
147                         //check for required parameters
148                         phylipfile = validParameter.validFile(parameters, "phylip", true);
149                         if (phylipfile == "not open") { abort = true; }
150                         else if (phylipfile == "not found") { 
151                                 phylipfile = m->getPhylipFile(); 
152                                 if (phylipfile != "") {  m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
153                                 else { 
154                                         m->mothurOut("You need to provide a phylip file with the cluster.classic command."); m->mothurOutEndLine(); 
155                                         abort = true; 
156                                 }       
157                         }else { m->setPhylipFile(phylipfile); } 
158
159                 
160                         //check for optional parameter and set defaults
161                         namefile = validParameter.validFile(parameters, "name", true);
162                         if (namefile == "not open") { abort = true; }   
163                         else if (namefile == "not found") { namefile = ""; }
164                         else { m->setNameFile(namefile); }
165                         
166                         string temp;
167                         temp = validParameter.validFile(parameters, "precision", false);
168                         if (temp == "not found") { temp = "100"; }
169                         //saves precision legnth for formatting below
170                         length = temp.length();
171                         m->mothurConvert(temp, precision); 
172                         
173                         temp = validParameter.validFile(parameters, "cutoff", false);
174                         if (temp == "not found") { temp = "10"; }
175                         m->mothurConvert(temp, cutoff); 
176                         cutoff += (5 / (precision * 10.0));  
177                         
178                         temp = validParameter.validFile(parameters, "hard", false);                     if (temp == "not found") { temp = "T"; }
179                         hard = m->isTrue(temp);
180                         
181                         temp = validParameter.validFile(parameters, "sim", false);                              if (temp == "not found") { temp = "F"; }
182                         sim = m->isTrue(temp); 
183                         
184                         method = validParameter.validFile(parameters, "method", false);
185                         if (method == "not found") { method = "average"; }
186                         
187                         if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { 
188                                 if (method == "furthest") { tag = "fn"; }
189                                 else if (method == "nearest") { tag = "nn"; }
190                                 else if (method == "average") { tag = "an"; }
191                                 else if (method == "weighted") { tag = "wn"; }
192                         }else { m->mothurOut("Not a valid clustering method.  Valid clustering algorithms are furthest, nearest, average, weighted."); m->mothurOutEndLine(); abort = true; }
193                 }
194         }
195         catch(exception& e) {
196                 m->errorOut(e, "ClusterDoturCommand", "ClusterCommand");
197                 exit(1);
198         }
199 }
200 //**********************************************************************************************************************
201
202 int ClusterDoturCommand::execute(){
203         try {
204         
205                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
206                 
207                 if(namefile != ""){     
208                         nameMap = new NameAssignment(namefile);
209                         nameMap->readMap();
210                 }else{
211                         nameMap = NULL;
212                 }
213                 
214                 //reads phylip file storing data in 2D vector, also fills list and rabund
215                 ClusterClassic* cluster = new ClusterClassic(cutoff, method, sim);
216                 cluster->readPhylipFile(phylipfile, nameMap);
217                 
218                 if (m->control_pressed) { delete cluster; delete list; delete rabund; return 0; }
219                 
220                 list = cluster->getListVector();
221                 rabund = cluster->getRAbundVector();
222                                                 
223                 if (outputDir == "") { outputDir += m->hasPath(phylipfile); }
224                 fileroot = outputDir + m->getRootName(m->getSimpleName(phylipfile));
225                         
226         string sabundFileName = fileroot+ tag + "." + getOutputFileNameTag("sabund");
227         string rabundFileName = fileroot+ tag + "." + getOutputFileNameTag("rabund");
228         string listFileName = fileroot+ tag + "." + getOutputFileNameTag("list");
229         
230                 m->openOutputFile(sabundFileName,       sabundFile);
231                 m->openOutputFile(rabundFileName,       rabundFile);
232                 m->openOutputFile(listFileName, listFile);
233                 
234                 outputNames.push_back(sabundFileName); outputTypes["sabund"].push_back(sabundFileName);
235                 outputNames.push_back(rabundFileName); outputTypes["rabund"].push_back(rabundFileName);
236                 outputNames.push_back(listFileName); outputTypes["list"].push_back(listFileName);
237                 
238                 float previousDist = 0.00000;
239                 float rndPreviousDist = 0.00000;
240                 oldRAbund = *rabund;
241                 oldList = *list;
242
243                 //double saveCutoff = cutoff;
244                 
245                 int estart = time(NULL);
246         
247                 while ((cluster->getSmallDist() < cutoff) && (cluster->getNSeqs() > 1)){
248                         if (m->control_pressed) { delete cluster; delete list; delete rabund; sabundFile.close();rabundFile.close();listFile.close();  for (int i = 0; i < outputNames.size(); i++) {   m->mothurRemove(outputNames[i]);        } outputTypes.clear();  return 0;  }
249                 
250                         cluster->update(cutoff);
251         
252                         float dist = cluster->getSmallDist();
253                         float rndDist;
254                         if (hard) {
255                                 rndDist = m->ceilDist(dist, precision); 
256                         }else{
257                                 rndDist = m->roundDist(dist, precision); 
258                         }
259
260                         if(previousDist <= 0.0000 && dist != previousDist){
261                                 printData("unique");
262                         }
263                         else if(rndDist != rndPreviousDist){
264                                 printData(toString(rndPreviousDist,  length-1));
265                         }
266                 
267                         previousDist = dist;
268                         rndPreviousDist = rndDist;
269                         oldRAbund = *rabund;
270                         oldList = *list;
271                 }
272         
273                 if(previousDist <= 0.0000){
274                         printData("unique");
275                 }
276                 else if(rndPreviousDist<cutoff){
277                         printData(toString(rndPreviousDist, length-1));
278                 }
279                                         
280                 sabundFile.close();
281                 rabundFile.close();
282                 listFile.close();
283                 
284                 delete cluster; delete nameMap; delete list; delete rabund;
285         
286                 //if (saveCutoff != cutoff) { 
287                 //      if (hard)       {  saveCutoff = m->ceilDist(saveCutoff, precision);     }
288                 //      else            {       saveCutoff = m->roundDist(saveCutoff, precision);  }
289                 //      m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine(); 
290                 //}
291                 
292                 //set list file as new current listfile
293                 string current = "";
294                 itTypes = outputTypes.find("list");
295                 if (itTypes != outputTypes.end()) {
296                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
297                 }
298                 
299                 //set rabund file as new current rabundfile
300                 itTypes = outputTypes.find("rabund");
301                 if (itTypes != outputTypes.end()) {
302                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
303                 }
304                 
305                 //set sabund file as new current sabundfile
306                 itTypes = outputTypes.find("sabund");
307                 if (itTypes != outputTypes.end()) {
308                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
309                 }
310                 
311                 m->mothurOutEndLine();
312                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
313                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
314                 m->mothurOutEndLine();
315
316                 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
317
318                 return 0;
319         }
320         catch(exception& e) {
321                 m->errorOut(e, "ClusterDoturCommand", "execute");
322                 exit(1);
323         }
324 }
325
326 //**********************************************************************************************************************
327
328 void ClusterDoturCommand::printData(string label){
329         try {
330         
331                 oldRAbund.setLabel(label);
332                 oldRAbund.print(rabundFile);
333                 oldRAbund.getSAbundVector().print(sabundFile);
334                 
335                 oldRAbund.getSAbundVector().print(cout);
336                 
337                 oldList.setLabel(label);
338                 oldList.print(listFile);
339         }
340         catch(exception& e) {
341                 m->errorOut(e, "ClusterDoturCommand", "printData");
342                 exit(1);
343         }
344 }
345 //**********************************************************************************************************************