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"
14 #include "clusterdoturcommand.h"
16 //**********************************************************************************************************************
17 vector<string> ClusterCommand::setParameters(){
19 CommandParameter pphylip("phylip", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "none",false,false); parameters.push_back(pphylip);
20 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "ColumnName",false,false); parameters.push_back(pname);
21 CommandParameter pcolumn("column", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "ColumnName",false,false); parameters.push_back(pcolumn);
22 CommandParameter pcutoff("cutoff", "Number", "", "10", "", "", "",false,false); parameters.push_back(pcutoff);
23 CommandParameter pprecision("precision", "Number", "", "100", "", "", "",false,false); parameters.push_back(pprecision);
24 CommandParameter pmethod("method", "Multiple", "furthest-nearest-average-weighted", "average", "", "", "",false,false); parameters.push_back(pmethod);
25 CommandParameter pshowabund("showabund", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pshowabund);
26 CommandParameter ptiming("timing", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(ptiming);
27 CommandParameter psim("sim", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(psim);
28 CommandParameter phard("hard", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(phard);
29 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
30 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
32 vector<string> myArray;
33 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
37 m->errorOut(e, "ClusterCommand", "setParameters");
41 //**********************************************************************************************************************
42 string ClusterCommand::getHelpString(){
44 string helpString = "";
45 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";
46 helpString += "The cluster command should be in the following format: \n";
47 helpString += "cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n";
48 helpString += "The acceptable cluster methods are furthest, nearest, average and weighted. If no method is provided then average is assumed.\n";
52 m->errorOut(e, "ClusterCommand", "getHelpString");
56 //**********************************************************************************************************************
57 string ClusterCommand::getOutputFileNameTag(string type, string inputName=""){
59 string outputFileName = "";
60 map<string, vector<string> >::iterator it;
62 //is this a type this command creates
63 it = outputTypes.find(type);
64 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
66 if (type == "list") { outputFileName = "list"; }
67 else if (type == "rabund") { outputFileName = "rabund"; }
68 else if (type == "sabund") { outputFileName = "sabund"; }
69 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
71 return outputFileName;
74 m->errorOut(e, "ClusterCommand", "getOutputFileNameTag");
78 //**********************************************************************************************************************
79 ClusterCommand::ClusterCommand(){
81 abort = true; calledHelp = true;
83 vector<string> tempOutNames;
84 outputTypes["list"] = tempOutNames;
85 outputTypes["rabund"] = tempOutNames;
86 outputTypes["sabund"] = tempOutNames;
89 m->errorOut(e, "ClusterCommand", "ClusterCommand");
93 //**********************************************************************************************************************
94 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
95 ClusterCommand::ClusterCommand(string option) {
97 abort = false; calledHelp = false;
99 //allow user to run help
100 if(option == "help") { help(); abort = true; calledHelp = true; }
101 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
104 vector<string> myArray = setParameters();
106 OptionParser parser(option);
107 map<string,string> parameters = parser.getParameters();
108 map<string,string>::iterator it;
110 ValidParameters validParameter;
112 //check to make sure all parameters are valid for command
113 for (it = parameters.begin(); it != parameters.end(); it++) {
114 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
119 //initialize outputTypes
120 vector<string> tempOutNames;
121 outputTypes["list"] = tempOutNames;
122 outputTypes["rabund"] = tempOutNames;
123 outputTypes["sabund"] = tempOutNames;
125 //if the user changes the output directory command factory will send this info to us in the output parameter
126 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
128 string inputDir = validParameter.validFile(parameters, "inputdir", false);
129 if (inputDir == "not found"){ inputDir = ""; }
132 it = parameters.find("phylip");
133 //user has given a template file
134 if(it != parameters.end()){
135 path = m->hasPath(it->second);
136 //if the user has not given a path then, add inputdir. else leave path alone.
137 if (path == "") { parameters["phylip"] = inputDir + it->second; }
140 it = parameters.find("column");
141 //user has given a template file
142 if(it != parameters.end()){
143 path = m->hasPath(it->second);
144 //if the user has not given a path then, add inputdir. else leave path alone.
145 if (path == "") { parameters["column"] = inputDir + it->second; }
148 it = parameters.find("name");
149 //user has given a template file
150 if(it != parameters.end()){
151 path = m->hasPath(it->second);
152 //if the user has not given a path then, add inputdir. else leave path alone.
153 if (path == "") { parameters["name"] = inputDir + it->second; }
157 //check for required parameters
158 phylipfile = validParameter.validFile(parameters, "phylip", true);
159 if (phylipfile == "not open") { phylipfile = ""; abort = true; }
160 else if (phylipfile == "not found") { phylipfile = ""; }
161 else { distfile = phylipfile; format = "phylip"; m->setPhylipFile(phylipfile); }
163 columnfile = validParameter.validFile(parameters, "column", true);
164 if (columnfile == "not open") { columnfile = ""; abort = true; }
165 else if (columnfile == "not found") { columnfile = ""; }
166 else { distfile = columnfile; format = "column"; m->setColumnFile(columnfile); }
168 namefile = validParameter.validFile(parameters, "name", true);
169 if (namefile == "not open") { abort = true; }
170 else if (namefile == "not found") { namefile = ""; }
171 else { m->setNameFile(namefile); }
173 if ((phylipfile == "") && (columnfile == "")) {
174 //is there are current file available for either of these?
175 //give priority to column, then phylip
176 columnfile = m->getColumnFile();
177 if (columnfile != "") { distfile = columnfile; format = "column"; m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
179 phylipfile = m->getPhylipFile();
180 if (phylipfile != "") { distfile = phylipfile; format = "phylip"; m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
182 m->mothurOut("No valid current files. You must provide a phylip or column file before you can use the cluster command."); m->mothurOutEndLine();
187 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; }
189 if (columnfile != "") {
190 if (namefile == "") {
191 namefile = m->getNameFile();
192 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
194 m->mothurOut("You need to provide a namefile if you are going to use the column format."); m->mothurOutEndLine();
200 //check for optional parameter and set defaults
201 // ...at some point should added some additional type checking...
202 //get user cutoff and precision or use defaults
204 temp = validParameter.validFile(parameters, "precision", false);
205 if (temp == "not found") { temp = "100"; }
206 //saves precision legnth for formatting below
207 length = temp.length();
208 m->mothurConvert(temp, precision);
210 temp = validParameter.validFile(parameters, "hard", false); if (temp == "not found") { temp = "T"; }
211 hard = m->isTrue(temp);
213 temp = validParameter.validFile(parameters, "sim", false); if (temp == "not found") { temp = "F"; }
214 sim = m->isTrue(temp);
216 temp = validParameter.validFile(parameters, "cutoff", false);
217 if (temp == "not found") { temp = "10"; }
218 m->mothurConvert(temp, cutoff);
219 cutoff += (5 / (precision * 10.0));
221 method = validParameter.validFile(parameters, "method", false);
222 if (method == "not found") { method = "average"; }
224 if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { }
225 else { m->mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest, average, and weighted."); m->mothurOutEndLine(); abort = true; }
227 showabund = validParameter.validFile(parameters, "showabund", false);
228 if (showabund == "not found") { showabund = "T"; }
230 timing = validParameter.validFile(parameters, "timing", false);
231 if (timing == "not found") { timing = "F"; }
235 catch(exception& e) {
236 m->errorOut(e, "ClusterCommand", "ClusterCommand");
240 //**********************************************************************************************************************
241 ClusterCommand::~ClusterCommand(){}
242 //**********************************************************************************************************************
244 int ClusterCommand::execute(){
247 if (abort == true) { if (calledHelp) { return 0; } return 2; }
249 //phylip file given and cutoff not given - use cluster.classic because it uses less memory and is faster
250 if ((format == "phylip") && (cutoff > 10.0)) {
251 m->mothurOutEndLine(); m->mothurOut("You are using a phylip file and no cutoff. I will run cluster.classic to save memory and time."); m->mothurOutEndLine();
253 //run unique.seqs for deconvolute results
254 string inputString = "phylip=" + distfile;
255 if (namefile != "") { inputString += ", name=" + namefile; }
256 inputString += ", precision=" + toString(precision);
257 inputString += ", method=" + method;
258 if (hard) { inputString += ", hard=T"; }
259 else { inputString += ", hard=F"; }
260 if (sim) { inputString += ", sim=T"; }
261 else { inputString += ", sim=F"; }
264 m->mothurOutEndLine();
265 m->mothurOut("/------------------------------------------------------------/"); m->mothurOutEndLine();
266 m->mothurOut("Running command: cluster.classic(" + inputString + ")"); m->mothurOutEndLine();
268 Command* clusterClassicCommand = new ClusterDoturCommand(inputString);
269 clusterClassicCommand->execute();
270 delete clusterClassicCommand;
272 m->mothurOut("/------------------------------------------------------------/"); m->mothurOutEndLine();
278 if (format == "column") { read = new ReadColumnMatrix(columnfile, sim); } //sim indicates whether its a similarity matrix
279 else if (format == "phylip") { read = new ReadPhylipMatrix(phylipfile, sim); }
281 read->setCutoff(cutoff);
283 NameAssignment* nameMap = NULL;
285 nameMap = new NameAssignment(namefile);
290 list = read->getListVector();
291 matrix = read->getMatrix();
292 rabund = new RAbundVector(list->getRAbundVector());
295 if (m->control_pressed) { //clean up
296 delete list; delete matrix; delete rabund;
297 sabundFile.close();rabundFile.close();listFile.close();
298 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
303 if (method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix, cutoff, method); }
304 else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix, cutoff, method); }
305 else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix, cutoff, method); }
306 else if(method == "weighted"){ cluster = new WeightedLinkage(rabund, list, matrix, cutoff, method); }
307 tag = cluster->getTag();
309 if (outputDir == "") { outputDir += m->hasPath(distfile); }
310 fileroot = outputDir + m->getRootName(m->getSimpleName(distfile));
312 string sabundFileName = fileroot+ tag + "." + getOutputFileNameTag("sabund");
313 string rabundFileName = fileroot+ tag + "." + getOutputFileNameTag("rabund");
314 string listFileName = fileroot+ tag + "." + getOutputFileNameTag("list");
316 m->openOutputFile(sabundFileName, sabundFile);
317 m->openOutputFile(rabundFileName, rabundFile);
318 m->openOutputFile(listFileName, listFile);
320 outputNames.push_back(sabundFileName); outputTypes["sabund"].push_back(sabundFileName);
321 outputNames.push_back(rabundFileName); outputTypes["rabund"].push_back(rabundFileName);
322 outputNames.push_back(listFileName); outputTypes["list"].push_back(listFileName);
325 time_t estart = time(NULL);
326 float previousDist = 0.00000;
327 float rndPreviousDist = 0.00000;
334 double saveCutoff = cutoff;
336 while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
338 if (m->control_pressed) { //clean up
339 delete list; delete matrix; delete rabund; delete cluster;
340 sabundFile.close();rabundFile.close();listFile.close();
341 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
345 if (print_start && m->isTrue(timing)) {
346 m->mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/"
347 + toString(m->roundDist(matrix->getSmallDist(), precision))
348 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
355 cluster->update(cutoff);
357 float dist = matrix->getSmallDist();
360 rndDist = m->ceilDist(dist, precision);
362 rndDist = m->roundDist(dist, precision);
365 if(previousDist <= 0.0000 && dist != previousDist){
368 else if(rndDist != rndPreviousDist){
369 printData(toString(rndPreviousDist, length-1));
373 rndPreviousDist = rndDist;
378 if (print_start && m->isTrue(timing)) {
379 m->mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist)
380 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
385 if(previousDist <= 0.0000){
388 else if(rndPreviousDist<cutoff){
389 printData(toString(rndPreviousDist, length-1));
401 if (saveCutoff != cutoff) {
402 if (hard) { saveCutoff = m->ceilDist(saveCutoff, precision); }
403 else { saveCutoff = m->roundDist(saveCutoff, precision); }
405 m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine();
408 //set list file as new current listfile
410 itTypes = outputTypes.find("list");
411 if (itTypes != outputTypes.end()) {
412 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
415 //set rabund file as new current rabundfile
416 itTypes = outputTypes.find("rabund");
417 if (itTypes != outputTypes.end()) {
418 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
421 //set sabund file as new current sabundfile
422 itTypes = outputTypes.find("sabund");
423 if (itTypes != outputTypes.end()) {
424 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
427 m->mothurOutEndLine();
428 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
429 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
430 m->mothurOutEndLine();
433 //if (m->isTrue(timing)) {
434 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
440 catch(exception& e) {
441 m->errorOut(e, "ClusterCommand", "execute");
446 //**********************************************************************************************************************
448 void ClusterCommand::printData(string label){
450 if (m->isTrue(timing)) {
451 m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
452 + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
458 oldRAbund.setLabel(label);
459 if (m->isTrue(showabund)) {
460 oldRAbund.getSAbundVector().print(cout);
462 oldRAbund.print(rabundFile);
463 oldRAbund.getSAbundVector().print(sabundFile);
465 oldList.setLabel(label);
466 oldList.print(listFile);
468 catch(exception& e) {
469 m->errorOut(e, "ClusterCommand", "printData");
475 //**********************************************************************************************************************