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::setParameters(){
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);
26 vector<string> myArray;
27 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
31 m->errorOut(e, "ClusterDoturCommand", "setParameters");
35 //**********************************************************************************************************************
36 string ClusterDoturCommand::getHelpString(){
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";
47 m->errorOut(e, "ClusterDoturCommand", "getHelpString");
51 //**********************************************************************************************************************
52 ClusterDoturCommand::ClusterDoturCommand(){
54 abort = true; calledHelp = true;
56 vector<string> tempOutNames;
57 outputTypes["list"] = tempOutNames;
58 outputTypes["rabund"] = tempOutNames;
59 outputTypes["sabund"] = tempOutNames;
62 m->errorOut(e, "ClusterDoturCommand", "ClusterCommand");
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) {
71 abort = false; calledHelp = false;
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;}
78 vector<string> myArray = setParameters();
80 OptionParser parser(option);
81 map<string,string> parameters = parser.getParameters();
83 ValidParameters validParameter;
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) {
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 = ""; }
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; }
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; }
116 //initialize outputTypes
117 vector<string> tempOutNames;
118 outputTypes["list"] = tempOutNames;
119 outputTypes["rabund"] = tempOutNames;
120 outputTypes["sabund"] = tempOutNames;
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 = ""; }
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(); }
132 m->mothurOut("You need to provide a phylip file with the cluster.classic command."); m->mothurOutEndLine();
135 }else { m->setPhylipFile(phylipfile); }
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); }
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 m->mothurConvert(temp, precision);
151 temp = validParameter.validFile(parameters, "cutoff", false);
152 if (temp == "not found") { temp = "10"; }
153 m->mothurConvert(temp, cutoff);
154 cutoff += (5 / (precision * 10.0));
156 temp = validParameter.validFile(parameters, "hard", false); if (temp == "not found") { temp = "T"; }
157 hard = m->isTrue(temp);
159 temp = validParameter.validFile(parameters, "sim", false); if (temp == "not found") { temp = "F"; }
160 sim = m->isTrue(temp);
162 method = validParameter.validFile(parameters, "method", false);
163 if (method == "not found") { method = "average"; }
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; }
173 catch(exception& e) {
174 m->errorOut(e, "ClusterDoturCommand", "ClusterCommand");
178 //**********************************************************************************************************************
180 int ClusterDoturCommand::execute(){
183 if (abort == true) { if (calledHelp) { return 0; } return 2; }
186 nameMap = new NameAssignment(namefile);
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);
196 if (m->control_pressed) { delete cluster; delete list; delete rabund; return 0; }
198 list = cluster->getListVector();
199 rabund = cluster->getRAbundVector();
201 if (outputDir == "") { outputDir += m->hasPath(phylipfile); }
202 fileroot = outputDir + m->getRootName(m->getSimpleName(phylipfile));
204 m->openOutputFile(fileroot+ tag + ".sabund", sabundFile);
205 m->openOutputFile(fileroot+ tag + ".rabund", rabundFile);
206 m->openOutputFile(fileroot+ tag + ".list", listFile);
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");
212 float previousDist = 0.00000;
213 float rndPreviousDist = 0.00000;
217 //double saveCutoff = cutoff;
219 int estart = time(NULL);
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; }
224 cluster->update(cutoff);
226 float dist = cluster->getSmallDist();
229 rndDist = m->ceilDist(dist, precision);
231 rndDist = m->roundDist(dist, precision);
234 if(previousDist <= 0.0000 && dist != previousDist){
237 else if(rndDist != rndPreviousDist){
238 printData(toString(rndPreviousDist, length-1));
242 rndPreviousDist = rndDist;
247 if(previousDist <= 0.0000){
250 else if(rndPreviousDist<cutoff){
251 printData(toString(rndPreviousDist, length-1));
258 delete cluster; delete nameMap; delete list; delete rabund;
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();
266 //set list file as new current listfile
268 itTypes = outputTypes.find("list");
269 if (itTypes != outputTypes.end()) {
270 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
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); }
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); }
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();
290 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
294 catch(exception& e) {
295 m->errorOut(e, "ClusterDoturCommand", "execute");
300 //**********************************************************************************************************************
302 void ClusterDoturCommand::printData(string label){
305 oldRAbund.setLabel(label);
306 oldRAbund.print(rabundFile);
307 oldRAbund.getSAbundVector().print(sabundFile);
309 oldRAbund.getSAbundVector().print(cout);
311 oldList.setLabel(label);
312 oldList.print(listFile);
314 catch(exception& e) {
315 m->errorOut(e, "ClusterDoturCommand", "printData");
319 //**********************************************************************************************************************