]> git.donarmstrong.com Git - mothur.git/blob - clusterdoturcommand.cpp
added citation function to commands
[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                         }       
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                         
143                         string temp;
144                         temp = validParameter.validFile(parameters, "precision", false);
145                         if (temp == "not found") { temp = "100"; }
146                         //saves precision legnth for formatting below
147                         length = temp.length();
148                         convert(temp, precision); 
149                         
150                         temp = validParameter.validFile(parameters, "cutoff", false);
151                         if (temp == "not found") { temp = "10"; }
152                         convert(temp, cutoff); 
153                         cutoff += (5 / (precision * 10.0));  
154                         
155                         temp = validParameter.validFile(parameters, "hard", false);                     if (temp == "not found") { temp = "T"; }
156                         hard = m->isTrue(temp);
157                         
158                         temp = validParameter.validFile(parameters, "sim", false);                              if (temp == "not found") { temp = "F"; }
159                         sim = m->isTrue(temp); 
160                         
161                         method = validParameter.validFile(parameters, "method", false);
162                         if (method == "not found") { method = "average"; }
163                         
164                         if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { 
165                                 if (method == "furthest") { tag = "fn"; }
166                                 else if (method == "nearest") { tag = "nn"; }
167                                 else if (method == "average") { tag = "an"; }
168                                 else if (method == "weighted") { tag = "wn"; }
169                         }else { m->mothurOut("Not a valid clustering method.  Valid clustering algorithms are furthest, nearest, average, weighted."); m->mothurOutEndLine(); abort = true; }
170                 }
171         }
172         catch(exception& e) {
173                 m->errorOut(e, "ClusterDoturCommand", "ClusterCommand");
174                 exit(1);
175         }
176 }
177 //**********************************************************************************************************************
178
179 int ClusterDoturCommand::execute(){
180         try {
181         
182                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
183                 
184                 if(namefile != ""){     
185                         nameMap = new NameAssignment(namefile);
186                         nameMap->readMap();
187                 }else{
188                         nameMap = NULL;
189                 }
190                 
191                 //reads phylip file storing data in 2D vector, also fills list and rabund
192                 ClusterClassic* cluster = new ClusterClassic(cutoff, method, sim);
193                 cluster->readPhylipFile(phylipfile, nameMap);
194                 
195                 if (m->control_pressed) { delete cluster; delete list; delete rabund; return 0; }
196                 
197                 list = cluster->getListVector();
198                 rabund = cluster->getRAbundVector();
199                                                 
200                 if (outputDir == "") { outputDir += m->hasPath(phylipfile); }
201                 fileroot = outputDir + m->getRootName(m->getSimpleName(phylipfile));
202                         
203                 m->openOutputFile(fileroot+ tag + ".sabund",    sabundFile);
204                 m->openOutputFile(fileroot+ tag + ".rabund",    rabundFile);
205                 m->openOutputFile(fileroot+ tag + ".list",              listFile);
206                                 
207                 outputNames.push_back(fileroot+ tag + ".sabund"); outputTypes["sabund"].push_back(fileroot+ tag + ".sabund");
208                 outputNames.push_back(fileroot+ tag + ".rabund"); outputTypes["rabund"].push_back(fileroot+ tag + ".rabund");
209                 outputNames.push_back(fileroot+ tag + ".list"); outputTypes["list"].push_back(fileroot+ tag + ".list");
210                 
211                 float previousDist = 0.00000;
212                 float rndPreviousDist = 0.00000;
213                 oldRAbund = *rabund;
214                 oldList = *list;
215
216                 //double saveCutoff = cutoff;
217                 
218                 int estart = time(NULL);
219         
220                 while ((cluster->getSmallDist() < cutoff) && (cluster->getNSeqs() > 1)){
221                         if (m->control_pressed) { delete cluster; delete list; delete rabund; sabundFile.close();rabundFile.close();listFile.close();  for (int i = 0; i < outputNames.size(); i++) {   remove(outputNames[i].c_str());         } outputTypes.clear();  return 0;  }
222                 
223                         cluster->update(cutoff);
224         
225                         float dist = cluster->getSmallDist();
226                         float rndDist;
227                         if (hard) {
228                                 rndDist = m->ceilDist(dist, precision); 
229                         }else{
230                                 rndDist = m->roundDist(dist, precision); 
231                         }
232
233                         if(previousDist <= 0.0000 && dist != previousDist){
234                                 printData("unique");
235                         }
236                         else if(rndDist != rndPreviousDist){
237                                 printData(toString(rndPreviousDist,  length-1));
238                         }
239                 
240                         previousDist = dist;
241                         rndPreviousDist = rndDist;
242                         oldRAbund = *rabund;
243                         oldList = *list;
244                 }
245         
246                 if(previousDist <= 0.0000){
247                         printData("unique");
248                 }
249                 else if(rndPreviousDist<cutoff){
250                         printData(toString(rndPreviousDist, length-1));
251                 }
252                                         
253                 sabundFile.close();
254                 rabundFile.close();
255                 listFile.close();
256                 
257                 delete cluster; delete nameMap; delete list; delete rabund;
258         
259                 //if (saveCutoff != cutoff) { 
260                 //      if (hard)       {  saveCutoff = m->ceilDist(saveCutoff, precision);     }
261                 //      else            {       saveCutoff = m->roundDist(saveCutoff, precision);  }
262                 //      m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine(); 
263                 //}
264                 
265                 //set list file as new current listfile
266                 string current = "";
267                 itTypes = outputTypes.find("list");
268                 if (itTypes != outputTypes.end()) {
269                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
270                 }
271                 
272                 //set rabund file as new current rabundfile
273                 itTypes = outputTypes.find("rabund");
274                 if (itTypes != outputTypes.end()) {
275                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
276                 }
277                 
278                 //set sabund file as new current sabundfile
279                 itTypes = outputTypes.find("sabund");
280                 if (itTypes != outputTypes.end()) {
281                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
282                 }
283                 
284                 m->mothurOutEndLine();
285                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
286                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
287                 m->mothurOutEndLine();
288
289                 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
290
291                 return 0;
292         }
293         catch(exception& e) {
294                 m->errorOut(e, "ClusterDoturCommand", "execute");
295                 exit(1);
296         }
297 }
298
299 //**********************************************************************************************************************
300
301 void ClusterDoturCommand::printData(string label){
302         try {
303         
304                 oldRAbund.setLabel(label);
305                 oldRAbund.print(rabundFile);
306                 oldRAbund.getSAbundVector().print(sabundFile);
307                 
308                 oldRAbund.getSAbundVector().print(cout);
309                 
310                 oldList.setLabel(label);
311                 oldList.print(listFile);
312         }
313         catch(exception& e) {
314                 m->errorOut(e, "ClusterDoturCommand", "printData");
315                 exit(1);
316         }
317 }
318 //**********************************************************************************************************************