5 * Created by Sarah Westcott on 1/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "clustercommand.h"
11 #include "readphylip.h"
12 #include "readcolumn.h"
13 #include "readmatrix.hpp"
15 //**********************************************************************************************************************
16 vector<string> ClusterCommand::setParameters(){
18 CommandParameter pphylip("phylip", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "none",false,false); parameters.push_back(pphylip);
19 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "ColumnName",false,false); parameters.push_back(pname);
20 CommandParameter pcolumn("column", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "ColumnName",false,false); parameters.push_back(pcolumn);
21 CommandParameter pcutoff("cutoff", "Number", "", "10", "", "", "",false,false); parameters.push_back(pcutoff);
22 CommandParameter pprecision("precision", "Number", "", "100", "", "", "",false,false); parameters.push_back(pprecision);
23 CommandParameter pmethod("method", "Multiple", "furthest-nearest-average-weighted", "average", "", "", "",false,false); parameters.push_back(pmethod);
24 CommandParameter pshowabund("showabund", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pshowabund);
25 CommandParameter ptiming("timing", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(ptiming);
26 CommandParameter psim("sim", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(psim);
27 CommandParameter phard("hard", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(phard);
28 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
29 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
31 vector<string> myArray;
32 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
36 m->errorOut(e, "ClusterCommand", "setParameters");
40 //**********************************************************************************************************************
41 string ClusterCommand::getHelpString(){
43 string helpString = "";
44 helpString += "The cluster command parameter options are phylip, column, name, method, cuttoff, hard, precision, sim, showabund and timing. Phylip or column and name are required, unless you have a valid current file.\n";
45 helpString += "The cluster command should be in the following format: \n";
46 helpString += "cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n";
47 helpString += "The acceptable cluster methods are furthest, nearest, average and weighted. If no method is provided then average is assumed.\n";
51 m->errorOut(e, "ClusterCommand", "getHelpString");
55 //**********************************************************************************************************************
56 ClusterCommand::ClusterCommand(){
58 abort = true; calledHelp = true;
60 vector<string> tempOutNames;
61 outputTypes["list"] = tempOutNames;
62 outputTypes["rabund"] = tempOutNames;
63 outputTypes["sabund"] = tempOutNames;
66 m->errorOut(e, "ClusterCommand", "ClusterCommand");
70 //**********************************************************************************************************************
71 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
72 ClusterCommand::ClusterCommand(string option) {
74 abort = false; calledHelp = false;
76 //allow user to run help
77 if(option == "help") { help(); abort = true; calledHelp = true; }
78 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
81 vector<string> myArray = setParameters();
83 OptionParser parser(option);
84 map<string,string> parameters = parser.getParameters();
85 map<string,string>::iterator it;
87 ValidParameters validParameter;
89 //check to make sure all parameters are valid for command
90 for (it = parameters.begin(); it != parameters.end(); it++) {
91 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
96 //initialize outputTypes
97 vector<string> tempOutNames;
98 outputTypes["list"] = tempOutNames;
99 outputTypes["rabund"] = tempOutNames;
100 outputTypes["sabund"] = tempOutNames;
102 //if the user changes the output directory command factory will send this info to us in the output parameter
103 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
105 string inputDir = validParameter.validFile(parameters, "inputdir", false);
106 if (inputDir == "not found"){ inputDir = ""; }
109 it = parameters.find("phylip");
110 //user has given a template file
111 if(it != parameters.end()){
112 path = m->hasPath(it->second);
113 //if the user has not given a path then, add inputdir. else leave path alone.
114 if (path == "") { parameters["phylip"] = inputDir + it->second; }
117 it = parameters.find("column");
118 //user has given a template file
119 if(it != parameters.end()){
120 path = m->hasPath(it->second);
121 //if the user has not given a path then, add inputdir. else leave path alone.
122 if (path == "") { parameters["column"] = inputDir + it->second; }
125 it = parameters.find("name");
126 //user has given a template file
127 if(it != parameters.end()){
128 path = m->hasPath(it->second);
129 //if the user has not given a path then, add inputdir. else leave path alone.
130 if (path == "") { parameters["name"] = inputDir + it->second; }
134 //check for required parameters
135 phylipfile = validParameter.validFile(parameters, "phylip", true);
136 if (phylipfile == "not open") { phylipfile = ""; abort = true; }
137 else if (phylipfile == "not found") { phylipfile = ""; }
138 else { distfile = phylipfile; format = "phylip"; }
140 columnfile = validParameter.validFile(parameters, "column", true);
141 if (columnfile == "not open") { columnfile = ""; abort = true; }
142 else if (columnfile == "not found") { columnfile = ""; }
143 else { distfile = columnfile; format = "column"; }
145 namefile = validParameter.validFile(parameters, "name", true);
146 if (namefile == "not open") { abort = true; }
147 else if (namefile == "not found") { namefile = ""; }
149 if ((phylipfile == "") && (columnfile == "")) {
150 //is there are current file available for either of these?
151 //give priority to column, then phylip
152 columnfile = m->getColumnFile();
153 if (columnfile != "") { distfile = columnfile; format = "column"; m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
155 phylipfile = m->getPhylipFile();
156 if (phylipfile != "") { distfile = phylipfile; format = "phylip"; m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
158 m->mothurOut("No valid current files. You must provide a phylip or column file before you can use the cluster command."); m->mothurOutEndLine();
163 else if ((phylipfile != "") && (columnfile != "")) { m->mothurOut("When executing a cluster command you must enter ONLY ONE of the following: phylip or column."); m->mothurOutEndLine(); abort = true; }
165 if (columnfile != "") {
166 if (namefile == "") {
167 namefile = m->getNameFile();
168 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
170 m->mothurOut("You need to provide a namefile if you are going to use the column format."); m->mothurOutEndLine();
176 //check for optional parameter and set defaults
177 // ...at some point should added some additional type checking...
178 //get user cutoff and precision or use defaults
180 temp = validParameter.validFile(parameters, "precision", false);
181 if (temp == "not found") { temp = "100"; }
182 //saves precision legnth for formatting below
183 length = temp.length();
184 convert(temp, precision);
186 temp = validParameter.validFile(parameters, "hard", false); if (temp == "not found") { temp = "T"; }
187 hard = m->isTrue(temp);
189 temp = validParameter.validFile(parameters, "sim", false); if (temp == "not found") { temp = "F"; }
190 sim = m->isTrue(temp);
192 temp = validParameter.validFile(parameters, "cutoff", false);
193 if (temp == "not found") { temp = "10"; }
194 convert(temp, cutoff);
195 cutoff += (5 / (precision * 10.0));
197 method = validParameter.validFile(parameters, "method", false);
198 if (method == "not found") { method = "average"; }
200 if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { }
201 else { m->mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest, average, and weighted."); m->mothurOutEndLine(); abort = true; }
203 showabund = validParameter.validFile(parameters, "showabund", false);
204 if (showabund == "not found") { showabund = "T"; }
206 timing = validParameter.validFile(parameters, "timing", false);
207 if (timing == "not found") { timing = "F"; }
211 catch(exception& e) {
212 m->errorOut(e, "ClusterCommand", "ClusterCommand");
216 //**********************************************************************************************************************
217 ClusterCommand::~ClusterCommand(){}
218 //**********************************************************************************************************************
220 int ClusterCommand::execute(){
223 if (abort == true) { if (calledHelp) { return 0; } return 2; }
226 if (format == "column") { read = new ReadColumnMatrix(columnfile, sim); } //sim indicates whether its a similarity matrix
227 else if (format == "phylip") { read = new ReadPhylipMatrix(phylipfile, sim); }
229 read->setCutoff(cutoff);
231 NameAssignment* nameMap = NULL;
233 nameMap = new NameAssignment(namefile);
238 list = read->getListVector();
239 matrix = read->getMatrix();
240 rabund = new RAbundVector(list->getRAbundVector());
243 if (m->control_pressed) { //clean up
244 delete list; delete matrix; delete rabund;
245 sabundFile.close();rabundFile.close();listFile.close();
246 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
251 if (method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix, cutoff, method); }
252 else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix, cutoff, method); }
253 else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix, cutoff, method); }
254 else if(method == "weighted"){ cluster = new WeightedLinkage(rabund, list, matrix, cutoff, method); }
255 tag = cluster->getTag();
257 if (outputDir == "") { outputDir += m->hasPath(distfile); }
258 fileroot = outputDir + m->getRootName(m->getSimpleName(distfile));
260 m->openOutputFile(fileroot+ tag + ".sabund", sabundFile);
261 m->openOutputFile(fileroot+ tag + ".rabund", rabundFile);
262 m->openOutputFile(fileroot+ tag + ".list", listFile);
264 outputNames.push_back(fileroot+ tag + ".sabund"); outputTypes["sabund"].push_back(fileroot+ tag + ".sabund");
265 outputNames.push_back(fileroot+ tag + ".rabund"); outputTypes["rabund"].push_back(fileroot+ tag + ".rabund");
266 outputNames.push_back(fileroot+ tag + ".list"); outputTypes["list"].push_back(fileroot+ tag + ".list");
269 time_t estart = time(NULL);
270 float previousDist = 0.00000;
271 float rndPreviousDist = 0.00000;
278 double saveCutoff = cutoff;
280 while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
282 if (m->control_pressed) { //clean up
283 delete list; delete matrix; delete rabund; delete cluster;
284 sabundFile.close();rabundFile.close();listFile.close();
285 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
289 if (print_start && m->isTrue(timing)) {
290 m->mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/"
291 + toString(m->roundDist(matrix->getSmallDist(), precision))
292 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
299 cluster->update(cutoff);
301 float dist = matrix->getSmallDist();
304 rndDist = m->ceilDist(dist, precision);
306 rndDist = m->roundDist(dist, precision);
309 if(previousDist <= 0.0000 && dist != previousDist){
312 else if(rndDist != rndPreviousDist){
313 printData(toString(rndPreviousDist, length-1));
317 rndPreviousDist = rndDist;
322 if (print_start && m->isTrue(timing)) {
323 m->mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist)
324 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
329 if(previousDist <= 0.0000){
332 else if(rndPreviousDist<cutoff){
333 printData(toString(rndPreviousDist, length-1));
345 if (saveCutoff != cutoff) {
346 if (hard) { saveCutoff = m->ceilDist(saveCutoff, precision); }
347 else { saveCutoff = m->roundDist(saveCutoff, precision); }
349 m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine();
352 //set list file as new current listfile
354 itTypes = outputTypes.find("list");
355 if (itTypes != outputTypes.end()) {
356 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
359 //set rabund file as new current rabundfile
360 itTypes = outputTypes.find("rabund");
361 if (itTypes != outputTypes.end()) {
362 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
365 //set sabund file as new current sabundfile
366 itTypes = outputTypes.find("sabund");
367 if (itTypes != outputTypes.end()) {
368 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
371 m->mothurOutEndLine();
372 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
373 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
374 m->mothurOutEndLine();
377 //if (m->isTrue(timing)) {
378 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
384 catch(exception& e) {
385 m->errorOut(e, "ClusterCommand", "execute");
390 //**********************************************************************************************************************
392 void ClusterCommand::printData(string label){
394 if (m->isTrue(timing)) {
395 m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
396 + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
402 oldRAbund.setLabel(label);
403 if (m->isTrue(showabund)) {
404 oldRAbund.getSAbundVector().print(cout);
406 oldRAbund.print(rabundFile);
407 oldRAbund.getSAbundVector().print(sabundFile);
409 oldList.setLabel(label);
410 oldList.print(listFile);
412 catch(exception& e) {
413 m->errorOut(e, "ClusterCommand", "printData");
419 //**********************************************************************************************************************