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 ClusterCommand::ClusterCommand(){
59 abort = true; calledHelp = true;
61 vector<string> tempOutNames;
62 outputTypes["list"] = tempOutNames;
63 outputTypes["rabund"] = tempOutNames;
64 outputTypes["sabund"] = tempOutNames;
67 m->errorOut(e, "ClusterCommand", "ClusterCommand");
71 //**********************************************************************************************************************
72 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
73 ClusterCommand::ClusterCommand(string option) {
75 abort = false; calledHelp = false;
77 //allow user to run help
78 if(option == "help") { help(); abort = true; calledHelp = true; }
79 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
82 vector<string> myArray = setParameters();
84 OptionParser parser(option);
85 map<string,string> parameters = parser.getParameters();
86 map<string,string>::iterator it;
88 ValidParameters validParameter;
90 //check to make sure all parameters are valid for command
91 for (it = parameters.begin(); it != parameters.end(); it++) {
92 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
97 //initialize outputTypes
98 vector<string> tempOutNames;
99 outputTypes["list"] = tempOutNames;
100 outputTypes["rabund"] = tempOutNames;
101 outputTypes["sabund"] = tempOutNames;
103 //if the user changes the output directory command factory will send this info to us in the output parameter
104 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
106 string inputDir = validParameter.validFile(parameters, "inputdir", false);
107 if (inputDir == "not found"){ inputDir = ""; }
110 it = parameters.find("phylip");
111 //user has given a template file
112 if(it != parameters.end()){
113 path = m->hasPath(it->second);
114 //if the user has not given a path then, add inputdir. else leave path alone.
115 if (path == "") { parameters["phylip"] = inputDir + it->second; }
118 it = parameters.find("column");
119 //user has given a template file
120 if(it != parameters.end()){
121 path = m->hasPath(it->second);
122 //if the user has not given a path then, add inputdir. else leave path alone.
123 if (path == "") { parameters["column"] = inputDir + it->second; }
126 it = parameters.find("name");
127 //user has given a template file
128 if(it != parameters.end()){
129 path = m->hasPath(it->second);
130 //if the user has not given a path then, add inputdir. else leave path alone.
131 if (path == "") { parameters["name"] = inputDir + it->second; }
135 //check for required parameters
136 phylipfile = validParameter.validFile(parameters, "phylip", true);
137 if (phylipfile == "not open") { phylipfile = ""; abort = true; }
138 else if (phylipfile == "not found") { phylipfile = ""; }
139 else { distfile = phylipfile; format = "phylip"; m->setPhylipFile(phylipfile); }
141 columnfile = validParameter.validFile(parameters, "column", true);
142 if (columnfile == "not open") { columnfile = ""; abort = true; }
143 else if (columnfile == "not found") { columnfile = ""; }
144 else { distfile = columnfile; format = "column"; m->setColumnFile(columnfile); }
146 namefile = validParameter.validFile(parameters, "name", true);
147 if (namefile == "not open") { abort = true; }
148 else if (namefile == "not found") { namefile = ""; }
149 else { m->setNameFile(namefile); }
151 if ((phylipfile == "") && (columnfile == "")) {
152 //is there are current file available for either of these?
153 //give priority to column, then phylip
154 columnfile = m->getColumnFile();
155 if (columnfile != "") { distfile = columnfile; format = "column"; m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
157 phylipfile = m->getPhylipFile();
158 if (phylipfile != "") { distfile = phylipfile; format = "phylip"; m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
160 m->mothurOut("No valid current files. You must provide a phylip or column file before you can use the cluster command."); m->mothurOutEndLine();
165 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; }
167 if (columnfile != "") {
168 if (namefile == "") {
169 namefile = m->getNameFile();
170 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
172 m->mothurOut("You need to provide a namefile if you are going to use the column format."); m->mothurOutEndLine();
178 //check for optional parameter and set defaults
179 // ...at some point should added some additional type checking...
180 //get user cutoff and precision or use defaults
182 temp = validParameter.validFile(parameters, "precision", false);
183 if (temp == "not found") { temp = "100"; }
184 //saves precision legnth for formatting below
185 length = temp.length();
186 m->mothurConvert(temp, precision);
188 temp = validParameter.validFile(parameters, "hard", false); if (temp == "not found") { temp = "T"; }
189 hard = m->isTrue(temp);
191 temp = validParameter.validFile(parameters, "sim", false); if (temp == "not found") { temp = "F"; }
192 sim = m->isTrue(temp);
194 temp = validParameter.validFile(parameters, "cutoff", false);
195 if (temp == "not found") { temp = "10"; }
196 m->mothurConvert(temp, cutoff);
197 cutoff += (5 / (precision * 10.0));
199 method = validParameter.validFile(parameters, "method", false);
200 if (method == "not found") { method = "average"; }
202 if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { }
203 else { m->mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest, average, and weighted."); m->mothurOutEndLine(); abort = true; }
205 showabund = validParameter.validFile(parameters, "showabund", false);
206 if (showabund == "not found") { showabund = "T"; }
208 timing = validParameter.validFile(parameters, "timing", false);
209 if (timing == "not found") { timing = "F"; }
213 catch(exception& e) {
214 m->errorOut(e, "ClusterCommand", "ClusterCommand");
218 //**********************************************************************************************************************
219 ClusterCommand::~ClusterCommand(){}
220 //**********************************************************************************************************************
222 int ClusterCommand::execute(){
225 if (abort == true) { if (calledHelp) { return 0; } return 2; }
227 //phylip file given and cutoff not given - use cluster.classic because it uses less memory and is faster
228 if ((format == "phylip") && (cutoff > 10.0)) {
229 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();
231 //run unique.seqs for deconvolute results
232 string inputString = "phylip=" + distfile;
233 if (namefile != "") { inputString += ", name=" + namefile; }
234 inputString += ", precision=" + toString(precision);
235 inputString += ", method=" + method;
236 if (hard) { inputString += ", hard=T"; }
237 else { inputString += ", hard=F"; }
238 if (sim) { inputString += ", sim=T"; }
239 else { inputString += ", sim=F"; }
242 m->mothurOutEndLine();
243 m->mothurOut("/------------------------------------------------------------/"); m->mothurOutEndLine();
244 m->mothurOut("Running command: cluster.classic(" + inputString + ")"); m->mothurOutEndLine();
246 Command* clusterClassicCommand = new ClusterDoturCommand(inputString);
247 clusterClassicCommand->execute();
248 delete clusterClassicCommand;
250 m->mothurOut("/------------------------------------------------------------/"); m->mothurOutEndLine();
256 if (format == "column") { read = new ReadColumnMatrix(columnfile, sim); } //sim indicates whether its a similarity matrix
257 else if (format == "phylip") { read = new ReadPhylipMatrix(phylipfile, sim); }
259 read->setCutoff(cutoff);
261 NameAssignment* nameMap = NULL;
263 nameMap = new NameAssignment(namefile);
268 list = read->getListVector();
269 matrix = read->getMatrix();
270 rabund = new RAbundVector(list->getRAbundVector());
273 if (m->control_pressed) { //clean up
274 delete list; delete matrix; delete rabund;
275 sabundFile.close();rabundFile.close();listFile.close();
276 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
281 if (method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix, cutoff, method); }
282 else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix, cutoff, method); }
283 else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix, cutoff, method); }
284 else if(method == "weighted"){ cluster = new WeightedLinkage(rabund, list, matrix, cutoff, method); }
285 tag = cluster->getTag();
287 if (outputDir == "") { outputDir += m->hasPath(distfile); }
288 fileroot = outputDir + m->getRootName(m->getSimpleName(distfile));
290 m->openOutputFile(fileroot+ tag + ".sabund", sabundFile);
291 m->openOutputFile(fileroot+ tag + ".rabund", rabundFile);
292 m->openOutputFile(fileroot+ tag + ".list", listFile);
294 outputNames.push_back(fileroot+ tag + ".sabund"); outputTypes["sabund"].push_back(fileroot+ tag + ".sabund");
295 outputNames.push_back(fileroot+ tag + ".rabund"); outputTypes["rabund"].push_back(fileroot+ tag + ".rabund");
296 outputNames.push_back(fileroot+ tag + ".list"); outputTypes["list"].push_back(fileroot+ tag + ".list");
299 time_t estart = time(NULL);
300 float previousDist = 0.00000;
301 float rndPreviousDist = 0.00000;
308 double saveCutoff = cutoff;
310 while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
312 if (m->control_pressed) { //clean up
313 delete list; delete matrix; delete rabund; delete cluster;
314 sabundFile.close();rabundFile.close();listFile.close();
315 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
319 if (print_start && m->isTrue(timing)) {
320 m->mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/"
321 + toString(m->roundDist(matrix->getSmallDist(), precision))
322 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
329 cluster->update(cutoff);
331 float dist = matrix->getSmallDist();
334 rndDist = m->ceilDist(dist, precision);
336 rndDist = m->roundDist(dist, precision);
339 if(previousDist <= 0.0000 && dist != previousDist){
342 else if(rndDist != rndPreviousDist){
343 printData(toString(rndPreviousDist, length-1));
347 rndPreviousDist = rndDist;
352 if (print_start && m->isTrue(timing)) {
353 m->mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist)
354 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
359 if(previousDist <= 0.0000){
362 else if(rndPreviousDist<cutoff){
363 printData(toString(rndPreviousDist, length-1));
375 if (saveCutoff != cutoff) {
376 if (hard) { saveCutoff = m->ceilDist(saveCutoff, precision); }
377 else { saveCutoff = m->roundDist(saveCutoff, precision); }
379 m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine();
382 //set list file as new current listfile
384 itTypes = outputTypes.find("list");
385 if (itTypes != outputTypes.end()) {
386 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
389 //set rabund file as new current rabundfile
390 itTypes = outputTypes.find("rabund");
391 if (itTypes != outputTypes.end()) {
392 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
395 //set sabund file as new current sabundfile
396 itTypes = outputTypes.find("sabund");
397 if (itTypes != outputTypes.end()) {
398 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
401 m->mothurOutEndLine();
402 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
403 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
404 m->mothurOutEndLine();
407 //if (m->isTrue(timing)) {
408 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
414 catch(exception& e) {
415 m->errorOut(e, "ClusterCommand", "execute");
420 //**********************************************************************************************************************
422 void ClusterCommand::printData(string label){
424 if (m->isTrue(timing)) {
425 m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
426 + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
432 oldRAbund.setLabel(label);
433 if (m->isTrue(showabund)) {
434 oldRAbund.getSAbundVector().print(cout);
436 oldRAbund.print(rabundFile);
437 oldRAbund.getSAbundVector().print(sabundFile);
439 oldList.setLabel(label);
440 oldList.print(listFile);
442 catch(exception& e) {
443 m->errorOut(e, "ClusterCommand", "printData");
449 //**********************************************************************************************************************