]> git.donarmstrong.com Git - mothur.git/blob - clusterdoturcommand.cpp
rewrote metastats command in c++, added mothurRemove function to handle ~ error....
[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 ClusterDoturCommand::ClusterDoturCommand(){     
53         try {
54                 abort = true; calledHelp = true;
55                 setParameters();
56                 vector<string> tempOutNames;
57                 outputTypes["list"] = tempOutNames;
58                 outputTypes["rabund"] = tempOutNames;
59                 outputTypes["sabund"] = tempOutNames;
60         }
61         catch(exception& e) {
62                 m->errorOut(e, "ClusterDoturCommand", "ClusterCommand");
63                 exit(1);
64         }
65 }
66 //**********************************************************************************************************************
67 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
68 ClusterDoturCommand::ClusterDoturCommand(string option)  {
69         try{
70                 
71                 abort = false; calledHelp = false;   
72                 
73                 //allow user to run help
74                 if(option == "help") { help(); abort = true; calledHelp = true; }
75                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
76                 
77                 else {
78                         vector<string> myArray = setParameters();
79                         
80                         OptionParser parser(option);
81                         map<string,string> parameters = parser.getParameters();
82                         
83                         ValidParameters validParameter;
84                 
85                         //check to make sure all parameters are valid for command
86                         map<string,string>::iterator it;
87                         for (it = parameters.begin(); it != parameters.end(); it++) { 
88                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
89                                         abort = true;
90                                 }
91                         }
92                         
93                         //if the user changes the input directory command factory will send this info to us in the output parameter 
94                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
95                         if (inputDir == "not found"){   inputDir = "";          }
96                         else {
97                                 string path;
98                                 it = parameters.find("phylip");
99                                 //user has given a template file
100                                 if(it != parameters.end()){ 
101                                         path = m->hasPath(it->second);
102                                         //if the user has not given a path then, add inputdir. else leave path alone.
103                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
104                                 }
105                                 
106                                 it = parameters.find("name");
107                                 //user has given a template file
108                                 if(it != parameters.end()){ 
109                                         path = m->hasPath(it->second);
110                                         //if the user has not given a path then, add inputdir. else leave path alone.
111                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
112                                 }
113
114                         }
115                         
116                         //initialize outputTypes
117                         vector<string> tempOutNames;
118                         outputTypes["list"] = tempOutNames;
119                         outputTypes["rabund"] = tempOutNames;
120                         outputTypes["sabund"] = tempOutNames;
121                 
122                         //if the user changes the output directory command factory will send this info to us in the output parameter 
123                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
124                         
125                         //check for required parameters
126                         phylipfile = validParameter.validFile(parameters, "phylip", true);
127                         if (phylipfile == "not open") { abort = true; }
128                         else if (phylipfile == "not found") { 
129                                 phylipfile = m->getPhylipFile(); 
130                                 if (phylipfile != "") {  m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
131                                 else { 
132                                         m->mothurOut("You need to provide a phylip file with the cluster.classic command."); m->mothurOutEndLine(); 
133                                         abort = true; 
134                                 }       
135                         }else { m->setPhylipFile(phylipfile); } 
136
137                 
138                         //check for optional parameter and set defaults
139                         namefile = validParameter.validFile(parameters, "name", true);
140                         if (namefile == "not open") { abort = true; }   
141                         else if (namefile == "not found") { namefile = ""; }
142                         else { m->setNameFile(namefile); }
143                         
144                         string temp;
145                         temp = validParameter.validFile(parameters, "precision", false);
146                         if (temp == "not found") { temp = "100"; }
147                         //saves precision legnth for formatting below
148                         length = temp.length();
149                         convert(temp, precision); 
150                         
151                         temp = validParameter.validFile(parameters, "cutoff", false);
152                         if (temp == "not found") { temp = "10"; }
153                         convert(temp, cutoff); 
154                         cutoff += (5 / (precision * 10.0));  
155                         
156                         temp = validParameter.validFile(parameters, "hard", false);                     if (temp == "not found") { temp = "T"; }
157                         hard = m->isTrue(temp);
158                         
159                         temp = validParameter.validFile(parameters, "sim", false);                              if (temp == "not found") { temp = "F"; }
160                         sim = m->isTrue(temp); 
161                         
162                         method = validParameter.validFile(parameters, "method", false);
163                         if (method == "not found") { method = "average"; }
164                         
165                         if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { 
166                                 if (method == "furthest") { tag = "fn"; }
167                                 else if (method == "nearest") { tag = "nn"; }
168                                 else if (method == "average") { tag = "an"; }
169                                 else if (method == "weighted") { tag = "wn"; }
170                         }else { m->mothurOut("Not a valid clustering method.  Valid clustering algorithms are furthest, nearest, average, weighted."); m->mothurOutEndLine(); abort = true; }
171                 }
172         }
173         catch(exception& e) {
174                 m->errorOut(e, "ClusterDoturCommand", "ClusterCommand");
175                 exit(1);
176         }
177 }
178 //**********************************************************************************************************************
179
180 int ClusterDoturCommand::execute(){
181         try {
182         
183                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
184                 
185                 if(namefile != ""){     
186                         nameMap = new NameAssignment(namefile);
187                         nameMap->readMap();
188                 }else{
189                         nameMap = NULL;
190                 }
191                 
192                 //reads phylip file storing data in 2D vector, also fills list and rabund
193                 ClusterClassic* cluster = new ClusterClassic(cutoff, method, sim);
194                 cluster->readPhylipFile(phylipfile, nameMap);
195                 
196                 if (m->control_pressed) { delete cluster; delete list; delete rabund; return 0; }
197                 
198                 list = cluster->getListVector();
199                 rabund = cluster->getRAbundVector();
200                                                 
201                 if (outputDir == "") { outputDir += m->hasPath(phylipfile); }
202                 fileroot = outputDir + m->getRootName(m->getSimpleName(phylipfile));
203                         
204                 m->openOutputFile(fileroot+ tag + ".sabund",    sabundFile);
205                 m->openOutputFile(fileroot+ tag + ".rabund",    rabundFile);
206                 m->openOutputFile(fileroot+ tag + ".list",              listFile);
207                                 
208                 outputNames.push_back(fileroot+ tag + ".sabund"); outputTypes["sabund"].push_back(fileroot+ tag + ".sabund");
209                 outputNames.push_back(fileroot+ tag + ".rabund"); outputTypes["rabund"].push_back(fileroot+ tag + ".rabund");
210                 outputNames.push_back(fileroot+ tag + ".list"); outputTypes["list"].push_back(fileroot+ tag + ".list");
211                 
212                 float previousDist = 0.00000;
213                 float rndPreviousDist = 0.00000;
214                 oldRAbund = *rabund;
215                 oldList = *list;
216
217                 //double saveCutoff = cutoff;
218                 
219                 int estart = time(NULL);
220         
221                 while ((cluster->getSmallDist() < cutoff) && (cluster->getNSeqs() > 1)){
222                         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;  }
223                 
224                         cluster->update(cutoff);
225         
226                         float dist = cluster->getSmallDist();
227                         float rndDist;
228                         if (hard) {
229                                 rndDist = m->ceilDist(dist, precision); 
230                         }else{
231                                 rndDist = m->roundDist(dist, precision); 
232                         }
233
234                         if(previousDist <= 0.0000 && dist != previousDist){
235                                 printData("unique");
236                         }
237                         else if(rndDist != rndPreviousDist){
238                                 printData(toString(rndPreviousDist,  length-1));
239                         }
240                 
241                         previousDist = dist;
242                         rndPreviousDist = rndDist;
243                         oldRAbund = *rabund;
244                         oldList = *list;
245                 }
246         
247                 if(previousDist <= 0.0000){
248                         printData("unique");
249                 }
250                 else if(rndPreviousDist<cutoff){
251                         printData(toString(rndPreviousDist, length-1));
252                 }
253                                         
254                 sabundFile.close();
255                 rabundFile.close();
256                 listFile.close();
257                 
258                 delete cluster; delete nameMap; delete list; delete rabund;
259         
260                 //if (saveCutoff != cutoff) { 
261                 //      if (hard)       {  saveCutoff = m->ceilDist(saveCutoff, precision);     }
262                 //      else            {       saveCutoff = m->roundDist(saveCutoff, precision);  }
263                 //      m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine(); 
264                 //}
265                 
266                 //set list file as new current listfile
267                 string current = "";
268                 itTypes = outputTypes.find("list");
269                 if (itTypes != outputTypes.end()) {
270                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
271                 }
272                 
273                 //set rabund file as new current rabundfile
274                 itTypes = outputTypes.find("rabund");
275                 if (itTypes != outputTypes.end()) {
276                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
277                 }
278                 
279                 //set sabund file as new current sabundfile
280                 itTypes = outputTypes.find("sabund");
281                 if (itTypes != outputTypes.end()) {
282                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
283                 }
284                 
285                 m->mothurOutEndLine();
286                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
287                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
288                 m->mothurOutEndLine();
289
290                 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
291
292                 return 0;
293         }
294         catch(exception& e) {
295                 m->errorOut(e, "ClusterDoturCommand", "execute");
296                 exit(1);
297         }
298 }
299
300 //**********************************************************************************************************************
301
302 void ClusterDoturCommand::printData(string label){
303         try {
304         
305                 oldRAbund.setLabel(label);
306                 oldRAbund.print(rabundFile);
307                 oldRAbund.getSAbundVector().print(sabundFile);
308                 
309                 oldRAbund.getSAbundVector().print(cout);
310                 
311                 oldList.setLabel(label);
312                 oldList.print(listFile);
313         }
314         catch(exception& e) {
315                 m->errorOut(e, "ClusterDoturCommand", "printData");
316                 exit(1);
317         }
318 }
319 //**********************************************************************************************************************