2 * clusterdoturcommand.cpp
5 * Created by westcott on 10/27/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "clusterdoturcommand.h"
11 #include "clusterclassic.h"
13 //**********************************************************************************************************************
14 vector<string> ClusterDoturCommand::getValidParameters(){
16 string AlignArray[] = {"phylip","name","hard","cutoff","precision","method","outputdir","inputdir"};
17 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
21 m->errorOut(e, "ClusterDoturCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 ClusterDoturCommand::ClusterDoturCommand(){
29 //initialize outputTypes
30 vector<string> tempOutNames;
31 outputTypes["list"] = tempOutNames;
32 outputTypes["rabund"] = tempOutNames;
33 outputTypes["sabund"] = tempOutNames;
36 m->errorOut(e, "ClusterDoturCommand", "ClusterCommand");
40 //**********************************************************************************************************************
41 vector<string> ClusterDoturCommand::getRequiredParameters(){
43 string Array[] = {"phylip"};
44 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
48 m->errorOut(e, "ClusterDoturCommand", "getRequiredParameters");
52 //**********************************************************************************************************************
53 vector<string> ClusterDoturCommand::getRequiredFiles(){
55 vector<string> myArray;
59 m->errorOut(e, "ClusterDoturCommand", "getRequiredFiles");
63 //**********************************************************************************************************************
64 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
65 ClusterDoturCommand::ClusterDoturCommand(string option) {
70 //allow user to run help
71 if(option == "help") { help(); abort = true; }
74 //valid paramters for this command
75 string Array[] = {"phylip","name","cutoff","hard","precision","method","outputdir","inputdir"};
76 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
78 OptionParser parser(option);
79 map<string,string> parameters = parser.getParameters();
81 ValidParameters validParameter;
83 //check to make sure all parameters are valid for command
84 map<string,string>::iterator it;
85 for (it = parameters.begin(); it != parameters.end(); it++) {
86 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
91 //if the user changes the input directory command factory will send this info to us in the output parameter
92 string inputDir = validParameter.validFile(parameters, "inputdir", false);
93 if (inputDir == "not found"){ inputDir = ""; }
96 it = parameters.find("phylip");
97 //user has given a template file
98 if(it != parameters.end()){
99 path = m->hasPath(it->second);
100 //if the user has not given a path then, add inputdir. else leave path alone.
101 if (path == "") { parameters["phylip"] = inputDir + it->second; }
104 it = parameters.find("name");
105 //user has given a template file
106 if(it != parameters.end()){
107 path = m->hasPath(it->second);
108 //if the user has not given a path then, add inputdir. else leave path alone.
109 if (path == "") { parameters["name"] = inputDir + it->second; }
114 //initialize outputTypes
115 vector<string> tempOutNames;
116 outputTypes["list"] = tempOutNames;
117 outputTypes["rabund"] = tempOutNames;
118 outputTypes["sabund"] = tempOutNames;
120 //if the user changes the output directory command factory will send this info to us in the output parameter
121 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
123 //check for required parameters
124 phylipfile = validParameter.validFile(parameters, "phylip", true);
125 if (phylipfile == "not open") { abort = true; }
126 else if (phylipfile == "not found") { phylipfile = ""; m->mothurOut("When executing the cluster.dotur command you must enter a phylip file."); m->mothurOutEndLine(); abort = true; }
129 //check for optional parameter and set defaults
130 namefile = validParameter.validFile(parameters, "name", true);
131 if (namefile == "not open") { abort = true; }
132 else if (namefile == "not found") { namefile = ""; }
135 temp = validParameter.validFile(parameters, "precision", false);
136 if (temp == "not found") { temp = "100"; }
137 //saves precision legnth for formatting below
138 length = temp.length();
139 convert(temp, precision);
141 temp = validParameter.validFile(parameters, "cutoff", false);
142 if (temp == "not found") { temp = "10"; }
143 convert(temp, cutoff);
144 cutoff += (5 / (precision * 10.0));
146 temp = validParameter.validFile(parameters, "hard", false); if (temp == "not found") { temp = "F"; }
147 hard = m->isTrue(temp);
149 method = validParameter.validFile(parameters, "method", false);
150 if (method == "not found") { method = "furthest"; }
152 if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) {
153 if (method == "furthest") { tag = "fn"; }
154 else if (method == "nearest") { tag = "nn"; }
155 else if (method == "average") { tag = "an"; }
156 else if (method == "weighted") { tag = "wn"; }
157 }else { m->mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest, average, weighted."); m->mothurOutEndLine(); abort = true; }
160 catch(exception& e) {
161 m->errorOut(e, "ClusterDoturCommand", "ClusterCommand");
166 //**********************************************************************************************************************
168 void ClusterDoturCommand::help(){
170 m->mothurOut("The cluster.classic command clusters using the algorithm from dotur. \n");
171 m->mothurOut("The cluster.classic command parameter options are phylip, name, method, cuttoff, hard, precision. Phylip is required.\n");
172 m->mothurOut("The cluster.classic command should be in the following format: \n");
173 m->mothurOut("cluster.classic(phylip=yourDistanceMatrix, method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
174 m->mothurOut("The acceptable cluster methods are furthest, nearest, weighted and average. If no method is provided then furthest is assumed.\n\n");
177 catch(exception& e) {
178 m->errorOut(e, "ClusterDoturCommand", "help");
183 //**********************************************************************************************************************
185 ClusterDoturCommand::~ClusterDoturCommand(){}
187 //**********************************************************************************************************************
189 int ClusterDoturCommand::execute(){
192 if (abort == true) { return 0; }
195 nameMap = new NameAssignment(namefile);
201 //reads phylip file storing data in 2D vector, also fills list and rabund
202 ClusterClassic* cluster = new ClusterClassic(cutoff, method);
203 cluster->readPhylipFile(phylipfile, nameMap);
205 if (m->control_pressed) { delete cluster; delete list; delete rabund; return 0; }
207 list = cluster->getListVector();
208 rabund = cluster->getRAbundVector();
210 if (outputDir == "") { outputDir += m->hasPath(phylipfile); }
211 fileroot = outputDir + m->getRootName(m->getSimpleName(phylipfile));
213 m->openOutputFile(fileroot+ tag + ".sabund", sabundFile);
214 m->openOutputFile(fileroot+ tag + ".rabund", rabundFile);
215 m->openOutputFile(fileroot+ tag + ".list", listFile);
217 outputNames.push_back(fileroot+ tag + ".sabund"); outputTypes["sabund"].push_back(fileroot+ tag + ".sabund");
218 outputNames.push_back(fileroot+ tag + ".rabund"); outputTypes["rabund"].push_back(fileroot+ tag + ".rabund");
219 outputNames.push_back(fileroot+ tag + ".list"); outputTypes["list"].push_back(fileroot+ tag + ".list");
221 float previousDist = 0.00000;
222 float rndPreviousDist = 0.00000;
226 //double saveCutoff = cutoff;
228 int estart = time(NULL);
230 while ((cluster->getSmallDist() < cutoff) && (cluster->getNSeqs() > 1)){
231 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; }
233 cluster->update(cutoff);
235 float dist = cluster->getSmallDist();
238 rndDist = m->ceilDist(dist, precision);
240 rndDist = m->roundDist(dist, precision);
243 if(previousDist <= 0.0000 && dist != previousDist){
246 else if(rndDist != rndPreviousDist){
247 printData(toString(rndPreviousDist, length-1));
251 rndPreviousDist = rndDist;
256 if(previousDist <= 0.0000){
259 else if(rndPreviousDist<cutoff){
260 printData(toString(rndPreviousDist, length-1));
267 delete cluster; delete nameMap; delete list; delete rabund;
269 //if (saveCutoff != cutoff) {
270 // if (hard) { saveCutoff = m->ceilDist(saveCutoff, precision); }
271 // else { saveCutoff = m->roundDist(saveCutoff, precision); }
272 // m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine();
275 m->mothurOutEndLine();
276 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
277 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
278 m->mothurOutEndLine();
280 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
284 catch(exception& e) {
285 m->errorOut(e, "ClusterDoturCommand", "execute");
290 //**********************************************************************************************************************
292 void ClusterDoturCommand::printData(string label){
295 oldRAbund.setLabel(label);
296 oldRAbund.print(rabundFile);
297 oldRAbund.getSAbundVector().print(sabundFile);
299 oldRAbund.getSAbundVector().print(cout);
301 oldList.setLabel(label);
302 oldList.print(listFile);
304 catch(exception& e) {
305 m->errorOut(e, "ClusterDoturCommand", "printData");
309 //**********************************************************************************************************************