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"; m->setPhylipFile(phylipfile); }
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"; m->setColumnFile(columnfile); }
145 namefile = validParameter.validFile(parameters, "name", true);
146 if (namefile == "not open") { abort = true; }
147 else if (namefile == "not found") { namefile = ""; }
148 else { m->setNameFile(namefile); }
150 if ((phylipfile == "") && (columnfile == "")) {
151 //is there are current file available for either of these?
152 //give priority to column, then phylip
153 columnfile = m->getColumnFile();
154 if (columnfile != "") { distfile = columnfile; format = "column"; m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
156 phylipfile = m->getPhylipFile();
157 if (phylipfile != "") { distfile = phylipfile; format = "phylip"; m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
159 m->mothurOut("No valid current files. You must provide a phylip or column file before you can use the cluster command."); m->mothurOutEndLine();
164 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; }
166 if (columnfile != "") {
167 if (namefile == "") {
168 namefile = m->getNameFile();
169 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
171 m->mothurOut("You need to provide a namefile if you are going to use the column format."); m->mothurOutEndLine();
177 //check for optional parameter and set defaults
178 // ...at some point should added some additional type checking...
179 //get user cutoff and precision or use defaults
181 temp = validParameter.validFile(parameters, "precision", false);
182 if (temp == "not found") { temp = "100"; }
183 //saves precision legnth for formatting below
184 length = temp.length();
185 convert(temp, precision);
187 temp = validParameter.validFile(parameters, "hard", false); if (temp == "not found") { temp = "T"; }
188 hard = m->isTrue(temp);
190 temp = validParameter.validFile(parameters, "sim", false); if (temp == "not found") { temp = "F"; }
191 sim = m->isTrue(temp);
193 temp = validParameter.validFile(parameters, "cutoff", false);
194 if (temp == "not found") { temp = "10"; }
195 convert(temp, cutoff);
196 cutoff += (5 / (precision * 10.0));
198 method = validParameter.validFile(parameters, "method", false);
199 if (method == "not found") { method = "average"; }
201 if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { }
202 else { m->mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest, average, and weighted."); m->mothurOutEndLine(); abort = true; }
204 showabund = validParameter.validFile(parameters, "showabund", false);
205 if (showabund == "not found") { showabund = "T"; }
207 timing = validParameter.validFile(parameters, "timing", false);
208 if (timing == "not found") { timing = "F"; }
212 catch(exception& e) {
213 m->errorOut(e, "ClusterCommand", "ClusterCommand");
217 //**********************************************************************************************************************
218 ClusterCommand::~ClusterCommand(){}
219 //**********************************************************************************************************************
221 int ClusterCommand::execute(){
224 if (abort == true) { if (calledHelp) { return 0; } return 2; }
227 if (format == "column") { read = new ReadColumnMatrix(columnfile, sim); } //sim indicates whether its a similarity matrix
228 else if (format == "phylip") { read = new ReadPhylipMatrix(phylipfile, sim); }
230 read->setCutoff(cutoff);
232 NameAssignment* nameMap = NULL;
234 nameMap = new NameAssignment(namefile);
239 list = read->getListVector();
240 matrix = read->getMatrix();
241 rabund = new RAbundVector(list->getRAbundVector());
244 if (m->control_pressed) { //clean up
245 delete list; delete matrix; delete rabund;
246 sabundFile.close();rabundFile.close();listFile.close();
247 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
252 if (method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix, cutoff, method); }
253 else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix, cutoff, method); }
254 else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix, cutoff, method); }
255 else if(method == "weighted"){ cluster = new WeightedLinkage(rabund, list, matrix, cutoff, method); }
256 tag = cluster->getTag();
258 if (outputDir == "") { outputDir += m->hasPath(distfile); }
259 fileroot = outputDir + m->getRootName(m->getSimpleName(distfile));
261 m->openOutputFile(fileroot+ tag + ".sabund", sabundFile);
262 m->openOutputFile(fileroot+ tag + ".rabund", rabundFile);
263 m->openOutputFile(fileroot+ tag + ".list", listFile);
265 outputNames.push_back(fileroot+ tag + ".sabund"); outputTypes["sabund"].push_back(fileroot+ tag + ".sabund");
266 outputNames.push_back(fileroot+ tag + ".rabund"); outputTypes["rabund"].push_back(fileroot+ tag + ".rabund");
267 outputNames.push_back(fileroot+ tag + ".list"); outputTypes["list"].push_back(fileroot+ tag + ".list");
270 time_t estart = time(NULL);
271 float previousDist = 0.00000;
272 float rndPreviousDist = 0.00000;
279 double saveCutoff = cutoff;
281 while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
283 if (m->control_pressed) { //clean up
284 delete list; delete matrix; delete rabund; delete cluster;
285 sabundFile.close();rabundFile.close();listFile.close();
286 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
290 if (print_start && m->isTrue(timing)) {
291 m->mothurOut("Clustering (" + tag + ") dist " + toString(matrix->getSmallDist()) + "/"
292 + toString(m->roundDist(matrix->getSmallDist(), precision))
293 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
300 cluster->update(cutoff);
302 float dist = matrix->getSmallDist();
305 rndDist = m->ceilDist(dist, precision);
307 rndDist = m->roundDist(dist, precision);
310 if(previousDist <= 0.0000 && dist != previousDist){
313 else if(rndDist != rndPreviousDist){
314 printData(toString(rndPreviousDist, length-1));
318 rndPreviousDist = rndDist;
323 if (print_start && m->isTrue(timing)) {
324 m->mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist)
325 + "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
330 if(previousDist <= 0.0000){
333 else if(rndPreviousDist<cutoff){
334 printData(toString(rndPreviousDist, length-1));
346 if (saveCutoff != cutoff) {
347 if (hard) { saveCutoff = m->ceilDist(saveCutoff, precision); }
348 else { saveCutoff = m->roundDist(saveCutoff, precision); }
350 m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine();
353 //set list file as new current listfile
355 itTypes = outputTypes.find("list");
356 if (itTypes != outputTypes.end()) {
357 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
360 //set rabund file as new current rabundfile
361 itTypes = outputTypes.find("rabund");
362 if (itTypes != outputTypes.end()) {
363 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
366 //set sabund file as new current sabundfile
367 itTypes = outputTypes.find("sabund");
368 if (itTypes != outputTypes.end()) {
369 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
372 m->mothurOutEndLine();
373 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
374 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
375 m->mothurOutEndLine();
378 //if (m->isTrue(timing)) {
379 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
385 catch(exception& e) {
386 m->errorOut(e, "ClusterCommand", "execute");
391 //**********************************************************************************************************************
393 void ClusterCommand::printData(string label){
395 if (m->isTrue(timing)) {
396 m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
397 + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
403 oldRAbund.setLabel(label);
404 if (m->isTrue(showabund)) {
405 oldRAbund.getSAbundVector().print(cout);
407 oldRAbund.print(rabundFile);
408 oldRAbund.getSAbundVector().print(sabundFile);
410 oldList.setLabel(label);
411 oldList.print(listFile);
413 catch(exception& e) {
414 m->errorOut(e, "ClusterCommand", "printData");
420 //**********************************************************************************************************************