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"
17 //**********************************************************************************************************************
18 vector<string> ClusterCommand::setParameters(){
20 CommandParameter pphylip("phylip", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "none",false,false); parameters.push_back(pphylip);
21 CommandParameter pname("name", "InputTypes", "", "", "NameCount", "none", "ColumnName",false,false); parameters.push_back(pname);
22 CommandParameter pcount("count", "InputTypes", "", "", "NameCount", "none", "none",false,false); parameters.push_back(pcount);
23 CommandParameter pcolumn("column", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "ColumnName",false,false); parameters.push_back(pcolumn);
24 CommandParameter pcutoff("cutoff", "Number", "", "10", "", "", "",false,false); parameters.push_back(pcutoff);
25 CommandParameter pprecision("precision", "Number", "", "100", "", "", "",false,false); parameters.push_back(pprecision);
26 CommandParameter pmethod("method", "Multiple", "furthest-nearest-average-weighted", "average", "", "", "",false,false); parameters.push_back(pmethod);
27 CommandParameter pshowabund("showabund", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pshowabund);
28 CommandParameter ptiming("timing", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(ptiming);
29 CommandParameter psim("sim", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(psim);
30 CommandParameter phard("hard", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(phard);
31 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
32 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
34 vector<string> myArray;
35 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
39 m->errorOut(e, "ClusterCommand", "setParameters");
43 //**********************************************************************************************************************
44 string ClusterCommand::getHelpString(){
46 string helpString = "";
47 helpString += "The cluster command parameter options are phylip, column, name, count, method, cuttoff, hard, precision, sim, showabund and timing. Phylip or column and name are required, unless you have a valid current file.\n";
48 helpString += "The cluster command should be in the following format: \n";
49 helpString += "cluster(method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n";
50 helpString += "The acceptable cluster methods are furthest, nearest, average and weighted. If no method is provided then average is assumed.\n";
54 m->errorOut(e, "ClusterCommand", "getHelpString");
58 //**********************************************************************************************************************
59 string ClusterCommand::getOutputFileNameTag(string type, string inputName=""){
61 string outputFileName = "";
62 map<string, vector<string> >::iterator it;
64 //is this a type this command creates
65 it = outputTypes.find(type);
66 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
68 if (type == "list") { outputFileName = "list"; }
69 else if (type == "rabund") { outputFileName = "rabund"; }
70 else if (type == "sabund") { outputFileName = "sabund"; }
71 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
73 return outputFileName;
76 m->errorOut(e, "ClusterCommand", "getOutputFileNameTag");
80 //**********************************************************************************************************************
81 ClusterCommand::ClusterCommand(){
83 abort = true; calledHelp = true;
85 vector<string> tempOutNames;
86 outputTypes["list"] = tempOutNames;
87 outputTypes["rabund"] = tempOutNames;
88 outputTypes["sabund"] = tempOutNames;
91 m->errorOut(e, "ClusterCommand", "ClusterCommand");
95 //**********************************************************************************************************************
96 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
97 ClusterCommand::ClusterCommand(string option) {
99 abort = false; calledHelp = false;
101 //allow user to run help
102 if(option == "help") { help(); abort = true; calledHelp = true; }
103 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
106 vector<string> myArray = setParameters();
108 OptionParser parser(option);
109 map<string,string> parameters = parser.getParameters();
110 map<string,string>::iterator it;
112 ValidParameters validParameter;
114 //check to make sure all parameters are valid for command
115 for (it = parameters.begin(); it != parameters.end(); it++) {
116 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
121 //initialize outputTypes
122 vector<string> tempOutNames;
123 outputTypes["list"] = tempOutNames;
124 outputTypes["rabund"] = tempOutNames;
125 outputTypes["sabund"] = tempOutNames;
127 //if the user changes the output directory command factory will send this info to us in the output parameter
128 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
130 string inputDir = validParameter.validFile(parameters, "inputdir", false);
131 if (inputDir == "not found"){ inputDir = ""; }
134 it = parameters.find("phylip");
135 //user has given a template file
136 if(it != parameters.end()){
137 path = m->hasPath(it->second);
138 //if the user has not given a path then, add inputdir. else leave path alone.
139 if (path == "") { parameters["phylip"] = inputDir + it->second; }
142 it = parameters.find("column");
143 //user has given a template file
144 if(it != parameters.end()){
145 path = m->hasPath(it->second);
146 //if the user has not given a path then, add inputdir. else leave path alone.
147 if (path == "") { parameters["column"] = inputDir + it->second; }
150 it = parameters.find("name");
151 //user has given a template file
152 if(it != parameters.end()){
153 path = m->hasPath(it->second);
154 //if the user has not given a path then, add inputdir. else leave path alone.
155 if (path == "") { parameters["name"] = inputDir + it->second; }
159 //check for required parameters
160 phylipfile = validParameter.validFile(parameters, "phylip", true);
161 if (phylipfile == "not open") { phylipfile = ""; abort = true; }
162 else if (phylipfile == "not found") { phylipfile = ""; }
163 else { distfile = phylipfile; format = "phylip"; m->setPhylipFile(phylipfile); }
165 columnfile = validParameter.validFile(parameters, "column", true);
166 if (columnfile == "not open") { columnfile = ""; abort = true; }
167 else if (columnfile == "not found") { columnfile = ""; }
168 else { distfile = columnfile; format = "column"; m->setColumnFile(columnfile); }
170 namefile = validParameter.validFile(parameters, "name", true);
171 if (namefile == "not open") { abort = true; }
172 else if (namefile == "not found") { namefile = ""; }
173 else { m->setNameFile(namefile); }
175 countfile = validParameter.validFile(parameters, "count", true);
176 if (countfile == "not open") { abort = true; countfile = ""; }
177 else if (countfile == "not found") { countfile = ""; }
178 else { m->setCountTableFile(countfile); }
180 if ((phylipfile == "") && (columnfile == "")) {
181 //is there are current file available for either of these?
182 //give priority to column, then phylip
183 columnfile = m->getColumnFile();
184 if (columnfile != "") { distfile = columnfile; format = "column"; m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
186 phylipfile = m->getPhylipFile();
187 if (phylipfile != "") { distfile = phylipfile; format = "phylip"; m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
189 m->mothurOut("No valid current files. You must provide a phylip or column file before you can use the cluster command."); m->mothurOutEndLine();
194 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; }
196 if (columnfile != "") {
197 if ((namefile == "") && (countfile == "")){
198 namefile = m->getNameFile();
199 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
201 countfile = m->getCountTableFile();
202 if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
204 m->mothurOut("You need to provide a namefile or countfile if you are going to use the column format."); m->mothurOutEndLine();
211 if ((countfile != "") && (namefile != "")) { m->mothurOut("When executing a cluster command you must enter ONLY ONE of the following: count or name."); m->mothurOutEndLine(); abort = true; }
213 //check for optional parameter and set defaults
214 // ...at some point should added some additional type checking...
215 //get user cutoff and precision or use defaults
217 temp = validParameter.validFile(parameters, "precision", false);
218 if (temp == "not found") { temp = "100"; }
219 //saves precision legnth for formatting below
220 length = temp.length();
221 m->mothurConvert(temp, precision);
223 temp = validParameter.validFile(parameters, "hard", false); if (temp == "not found") { temp = "T"; }
224 hard = m->isTrue(temp);
226 temp = validParameter.validFile(parameters, "sim", false); if (temp == "not found") { temp = "F"; }
227 sim = m->isTrue(temp);
229 temp = validParameter.validFile(parameters, "cutoff", false);
230 if (temp == "not found") { temp = "10"; }
231 m->mothurConvert(temp, cutoff);
232 cutoff += (5 / (precision * 10.0));
234 method = validParameter.validFile(parameters, "method", false);
235 if (method == "not found") { method = "average"; }
237 if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { }
238 else { m->mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest, average, and weighted."); m->mothurOutEndLine(); abort = true; }
240 showabund = validParameter.validFile(parameters, "showabund", false);
241 if (showabund == "not found") { showabund = "T"; }
243 timing = validParameter.validFile(parameters, "timing", false);
244 if (timing == "not found") { timing = "F"; }
248 catch(exception& e) {
249 m->errorOut(e, "ClusterCommand", "ClusterCommand");
253 //**********************************************************************************************************************
254 ClusterCommand::~ClusterCommand(){}
255 //**********************************************************************************************************************
257 int ClusterCommand::execute(){
260 if (abort == true) { if (calledHelp) { return 0; } return 2; }
262 //phylip file given and cutoff not given - use cluster.classic because it uses less memory and is faster
263 if ((format == "phylip") && (cutoff > 10.0)) {
264 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();
266 //run unique.seqs for deconvolute results
267 string inputString = "phylip=" + distfile;
268 if (namefile != "") { inputString += ", name=" + namefile; }
269 else if (countfile != "") { inputString += ", count=" + countfile; }
270 inputString += ", precision=" + toString(precision);
271 inputString += ", method=" + method;
272 if (hard) { inputString += ", hard=T"; }
273 else { inputString += ", hard=F"; }
274 if (sim) { inputString += ", sim=T"; }
275 else { inputString += ", sim=F"; }
278 m->mothurOutEndLine();
279 m->mothurOut("/------------------------------------------------------------/"); m->mothurOutEndLine();
280 m->mothurOut("Running command: cluster.classic(" + inputString + ")"); m->mothurOutEndLine();
282 Command* clusterClassicCommand = new ClusterDoturCommand(inputString);
283 clusterClassicCommand->execute();
284 delete clusterClassicCommand;
286 m->mothurOut("/------------------------------------------------------------/"); m->mothurOutEndLine();
292 if (format == "column") { read = new ReadColumnMatrix(columnfile, sim); } //sim indicates whether its a similarity matrix
293 else if (format == "phylip") { read = new ReadPhylipMatrix(phylipfile, sim); }
295 read->setCutoff(cutoff);
297 NameAssignment* nameMap = NULL;
298 CountTable* ct = NULL;
300 nameMap = new NameAssignment(namefile);
303 }else if (countfile != "") {
304 ct = new CountTable();
305 ct->readTable(countfile);
309 list = read->getListVector();
310 matrix = read->getDMatrix();
312 if(countfile != "") {
313 rabund = new RAbundVector();
314 createRabund(ct, list, rabund); //creates an rabund that includes the counts for the unique list
316 }else { rabund = new RAbundVector(list->getRAbundVector()); }
319 if (m->control_pressed) { //clean up
320 delete list; delete matrix; delete rabund; if(countfile == ""){rabundFile.close(); sabundFile.close(); m->mothurRemove((fileroot+ tag + ".rabund")); m->mothurRemove((fileroot+ tag + ".sabund")); }
321 listFile.close(); m->mothurRemove((fileroot+ tag + ".list")); outputTypes.clear(); return 0;
325 if (method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix, cutoff, method); }
326 else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix, cutoff, method); }
327 else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix, cutoff, method); }
328 else if(method == "weighted"){ cluster = new WeightedLinkage(rabund, list, matrix, cutoff, method); }
329 tag = cluster->getTag();
331 if (outputDir == "") { outputDir += m->hasPath(distfile); }
332 fileroot = outputDir + m->getRootName(m->getSimpleName(distfile));
334 string sabundFileName = fileroot+ tag + "." + getOutputFileNameTag("sabund");
335 string rabundFileName = fileroot+ tag + "." + getOutputFileNameTag("rabund");
336 string listFileName = fileroot+ tag + ".";
337 if (countfile != "") { listFileName += "unique_"; }
338 listFileName += getOutputFileNameTag("list");
340 if (countfile == "") {
341 m->openOutputFile(sabundFileName, sabundFile);
342 m->openOutputFile(rabundFileName, rabundFile);
343 outputNames.push_back(sabundFileName); outputTypes["sabund"].push_back(sabundFileName);
344 outputNames.push_back(rabundFileName); outputTypes["rabund"].push_back(rabundFileName);
347 m->openOutputFile(listFileName, listFile);
348 outputNames.push_back(listFileName); outputTypes["list"].push_back(listFileName);
351 time_t estart = time(NULL);
352 float previousDist = 0.00000;
353 float rndPreviousDist = 0.00000;
360 double saveCutoff = cutoff;
362 while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
364 if (m->control_pressed) { //clean up
365 delete list; delete matrix; delete rabund; delete cluster;
366 if(countfile == "") {rabundFile.close(); sabundFile.close(); m->mothurRemove((fileroot+ tag + ".rabund")); m->mothurRemove((fileroot+ tag + ".sabund")); }
367 listFile.close(); m->mothurRemove((fileroot+ tag + ".list")); outputTypes.clear(); return 0;
370 if (print_start && m->isTrue(timing)) {
371 m->mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/"
372 + toString(m->roundDist(matrix->getSmallDist(), precision))
373 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
380 cluster->update(cutoff);
382 float dist = matrix->getSmallDist();
385 rndDist = m->ceilDist(dist, precision);
387 rndDist = m->roundDist(dist, precision);
390 if(previousDist <= 0.0000 && dist != previousDist){
393 else if(rndDist != rndPreviousDist){
394 printData(toString(rndPreviousDist, length-1));
398 rndPreviousDist = rndDist;
403 if (print_start && m->isTrue(timing)) {
404 m->mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist)
405 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
410 if(previousDist <= 0.0000){
413 else if(rndPreviousDist<cutoff){
414 printData(toString(rndPreviousDist, length-1));
421 if (countfile == "") {
427 if (saveCutoff != cutoff) {
428 if (hard) { saveCutoff = m->ceilDist(saveCutoff, precision); }
429 else { saveCutoff = m->roundDist(saveCutoff, precision); }
431 m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine();
434 //set list file as new current listfile
436 itTypes = outputTypes.find("list");
437 if (itTypes != outputTypes.end()) {
438 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
441 //set rabund file as new current rabundfile
442 itTypes = outputTypes.find("rabund");
443 if (itTypes != outputTypes.end()) {
444 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
447 //set sabund file as new current sabundfile
448 itTypes = outputTypes.find("sabund");
449 if (itTypes != outputTypes.end()) {
450 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
453 m->mothurOutEndLine();
454 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
455 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
456 m->mothurOutEndLine();
459 //if (m->isTrue(timing)) {
460 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
466 catch(exception& e) {
467 m->errorOut(e, "ClusterCommand", "execute");
472 //**********************************************************************************************************************
474 void ClusterCommand::printData(string label){
476 if (m->isTrue(timing)) {
477 m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
478 + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
484 if (countfile == "") {
485 oldRAbund.print(rabundFile);
486 oldRAbund.getSAbundVector().print(sabundFile);
489 oldRAbund.setLabel(label);
490 if (m->isTrue(showabund)) {
491 oldRAbund.getSAbundVector().print(cout);
494 oldList.setLabel(label);
495 oldList.print(listFile);
497 catch(exception& e) {
498 m->errorOut(e, "ClusterCommand", "printData");
504 //**********************************************************************************************************************
506 int ClusterCommand::createRabund(CountTable*& ct, ListVector*& list, RAbundVector*& rabund){
508 rabund->setLabel(list->getLabel());
509 for(int i = 0; i < list->getNumBins(); i++) {
510 if (m->control_pressed) { break; }
511 vector<string> binNames;
512 string bin = list->get(i);
513 m->splitAtComma(bin, binNames);
515 for (int j = 0; j < binNames.size(); j++) { total += ct->getNumSeqs(binNames[j]); }
516 rabund->push_back(total);
520 catch(exception& e) {
521 m->errorOut(e, "ClusterCommand", "createRabund");
526 //**********************************************************************************************************************