5 * Created by westcott on 10/13/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
10 #include "hclustercommand.h"
12 //**********************************************************************************************************************
13 vector<string> HClusterCommand::setParameters(){
15 CommandParameter pphylip("phylip", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "none",false,false); parameters.push_back(pphylip);
16 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "ColumnName",false,false); parameters.push_back(pname);
17 CommandParameter pcolumn("column", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "ColumnName",false,false); parameters.push_back(pcolumn);
18 CommandParameter pcutoff("cutoff", "Number", "", "10", "", "", "",false,false); parameters.push_back(pcutoff);
19 CommandParameter pprecision("precision", "Number", "", "100", "", "", "",false,false); parameters.push_back(pprecision);
20 CommandParameter pmethod("method", "Multiple", "furthest-nearest-average-weighted", "average", "", "", "",false,false); parameters.push_back(pmethod);
21 CommandParameter phard("hard", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(phard);
22 CommandParameter psorted("sorted", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(psorted);
23 CommandParameter pshowabund("showabund", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pshowabund);
24 CommandParameter ptiming("timing", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(ptiming);
25 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
26 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
28 vector<string> myArray;
29 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
33 m->errorOut(e, "HClusterCommand", "setParameters");
37 //**********************************************************************************************************************
38 string HClusterCommand::getHelpString(){
40 string helpString = "";
41 helpString += "The hcluster command parameter options are cutoff, precision, method, phylip, column, name, showabund, timing and sorted. Phylip or column and name are required, unless you have valid current files.\n";
42 helpString += "The phylip and column parameter allow you to enter your distance file, and sorted indicates whether your column distance file is already sorted. \n";
43 helpString += "The name parameter allows you to enter your name file and is required if your distance file is in column format. \n";
44 helpString += "The hcluster command should be in the following format: \n";
45 helpString += "hcluster(column=youDistanceFile, name=yourNameFile, method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n";
46 helpString += "The acceptable hcluster methods are furthest, nearest, weighted and average.\n";
50 m->errorOut(e, "HClusterCommand", "getHelpString");
54 //**********************************************************************************************************************
55 string HClusterCommand::getOutputFileNameTag(string type, string inputName=""){
57 string outputFileName = "";
58 map<string, vector<string> >::iterator it;
60 //is this a type this command creates
61 it = outputTypes.find(type);
62 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
64 if (type == "list") { outputFileName = "list"; }
65 else if (type == "rabund") { outputFileName = "rabund"; }
66 else if (type == "sabund") { outputFileName = "sabund"; }
67 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
69 return outputFileName;
72 m->errorOut(e, "HClusterCommand", "getOutputFileNameTag");
76 //**********************************************************************************************************************
77 HClusterCommand::HClusterCommand(){
79 abort = true; calledHelp = true;
81 vector<string> tempOutNames;
82 outputTypes["list"] = tempOutNames;
83 outputTypes["rabund"] = tempOutNames;
84 outputTypes["sabund"] = tempOutNames;
87 m->errorOut(e, "HClusterCommand", "HClusterCommand");
91 //**********************************************************************************************************************
92 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
93 HClusterCommand::HClusterCommand(string option) {
95 abort = false; calledHelp = false;
97 //allow user to run help
98 if(option == "help") { help(); abort = true; calledHelp = true; }
99 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
102 vector<string> myArray = setParameters();
104 OptionParser parser(option);
105 map<string,string> parameters = parser.getParameters();
107 ValidParameters validParameter;
108 map<string,string>::iterator it;
110 //check to make sure all parameters are valid for command
111 for (it = parameters.begin(); it != parameters.end(); it++) {
112 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
117 //initialize outputTypes
118 vector<string> tempOutNames;
119 outputTypes["list"] = tempOutNames;
120 outputTypes["rabund"] = tempOutNames;
121 outputTypes["sabund"] = tempOutNames;
123 //if the user changes the input directory command factory will send this info to us in the output parameter
124 string inputDir = validParameter.validFile(parameters, "inputdir", false);
125 if (inputDir == "not found"){ inputDir = ""; }
128 it = parameters.find("phylip");
129 //user has given a template file
130 if(it != parameters.end()){
131 path = m->hasPath(it->second);
132 //if the user has not given a path then, add inputdir. else leave path alone.
133 if (path == "") { parameters["phylip"] = inputDir + it->second; }
136 it = parameters.find("column");
137 //user has given a template file
138 if(it != parameters.end()){
139 path = m->hasPath(it->second);
140 //if the user has not given a path then, add inputdir. else leave path alone.
141 if (path == "") { parameters["column"] = inputDir + it->second; }
144 it = parameters.find("name");
145 //user has given a template file
146 if(it != parameters.end()){
147 path = m->hasPath(it->second);
148 //if the user has not given a path then, add inputdir. else leave path alone.
149 if (path == "") { parameters["name"] = inputDir + it->second; }
153 //if the user changes the output directory command factory will send this info to us in the output parameter
154 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
156 //check for required parameters
157 phylipfile = validParameter.validFile(parameters, "phylip", true);
158 if (phylipfile == "not open") { abort = true; }
159 else if (phylipfile == "not found") { phylipfile = ""; }
160 else { distfile = phylipfile; format = "phylip"; m->setPhylipFile(phylipfile); }
162 columnfile = validParameter.validFile(parameters, "column", true);
163 if (columnfile == "not open") { abort = true; }
164 else if (columnfile == "not found") { columnfile = ""; }
165 else { distfile = columnfile; format = "column"; m->setColumnFile(columnfile); }
167 namefile = validParameter.validFile(parameters, "name", true);
168 if (namefile == "not open") { abort = true; }
169 else if (namefile == "not found") { namefile = ""; }
170 else { m->setNameFile(namefile); }
172 if ((phylipfile == "") && (columnfile == "")) {
173 //is there are current file available for either of these?
174 //give priority to column, then phylip
175 columnfile = m->getColumnFile();
176 if (columnfile != "") { m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
178 phylipfile = m->getPhylipFile();
179 if (phylipfile != "") { m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
181 m->mothurOut("No valid current files. You must provide a phylip or column file before you can use the hcluster command."); m->mothurOutEndLine();
186 else if ((phylipfile != "") && (columnfile != "")) { m->mothurOut("When executing a hcluster command you must enter ONLY ONE of the following: phylip or column."); m->mothurOutEndLine(); abort = true; }
188 if (columnfile != "") {
189 if (namefile == "") {
190 namefile = m->getNameFile();
191 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
193 m->mothurOut("You need to provide a namefile if you are going to use the column format."); m->mothurOutEndLine();
199 //check for optional parameter and set defaults
200 // ...at some point should added some additional type checking...
201 //get user cutoff and precision or use defaults
203 temp = validParameter.validFile(parameters, "precision", false);
204 if (temp == "not found") { temp = "100"; }
205 //saves precision legnth for formatting below
206 length = temp.length();
207 m->mothurConvert(temp, precision);
209 temp = validParameter.validFile(parameters, "hard", false); if (temp == "not found") { temp = "T"; }
210 hard = m->isTrue(temp);
212 temp = validParameter.validFile(parameters, "cutoff", false);
213 if (temp == "not found") { temp = "10"; }
214 m->mothurConvert(temp, cutoff);
215 cutoff += (5 / (precision * 10.0));
217 method = validParameter.validFile(parameters, "method", false);
218 if (method == "not found") { method = "average"; }
220 if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { }
221 else { m->mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest, average or weighted."); m->mothurOutEndLine(); abort = true; }
223 showabund = validParameter.validFile(parameters, "showabund", false);
224 if (showabund == "not found") { showabund = "T"; }
226 sort = validParameter.validFile(parameters, "sorted", false);
227 if (sort == "not found") { sort = "F"; }
228 sorted = m->isTrue(sort);
230 timing = validParameter.validFile(parameters, "timing", false);
231 if (timing == "not found") { timing = "F"; }
234 if (abort == false) {
236 if (outputDir == "") { outputDir += m->hasPath(distfile); }
237 fileroot = outputDir + m->getRootName(m->getSimpleName(distfile));
239 if (method == "furthest") { tag = "fn"; }
240 else if (method == "nearest") { tag = "nn"; }
241 else if (method == "weighted") { tag = "wn"; }
244 string sabundFileName = fileroot+ tag + "." + getOutputFileNameTag("sabund");
245 string rabundFileName = fileroot+ tag + "." + getOutputFileNameTag("rabund");
246 string listFileName = fileroot+ tag + "." + getOutputFileNameTag("list");
248 m->openOutputFile(sabundFileName, sabundFile);
249 m->openOutputFile(rabundFileName, rabundFile);
250 m->openOutputFile(listFileName, listFile);
252 outputNames.push_back(sabundFileName); outputTypes["sabund"].push_back(sabundFileName);
253 outputNames.push_back(rabundFileName); outputTypes["rabund"].push_back(rabundFileName);
254 outputNames.push_back(listFileName); outputTypes["list"].push_back(listFileName);
258 catch(exception& e) {
259 m->errorOut(e, "HClusterCommand", "HClusterCommand");
264 //**********************************************************************************************************************
266 int HClusterCommand::execute(){
269 if (abort == true) { if (calledHelp) { return 0; } return 2; }
271 NameAssignment* nameMap = NULL;
273 nameMap = new NameAssignment(namefile);
277 time_t estart = time(NULL);
280 read = new ReadCluster(distfile, cutoff, outputDir, true);
281 read->setFormat(format);
284 if (m->control_pressed) {
289 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
293 distfile = read->getOutputFile();
295 list = read->getListVector();
298 list = new ListVector(nameMap->getListVector());
301 if (m->control_pressed) {
305 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
309 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to sort. "); m->mothurOutEndLine();
312 //list vector made by read contains all sequence names
314 rabund = new RAbundVector(list->getRAbundVector());
316 m->mothurOut("Error: no list vector!"); m->mothurOutEndLine(); return 0;
319 float previousDist = 0.00000;
320 float rndPreviousDist = 0.00000;
327 cluster = new HCluster(rabund, list, method, distfile, nameMap, cutoff);
328 vector<seqDist> seqs; seqs.resize(1); // to start loop
330 if (m->control_pressed) {
335 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
339 float saveCutoff = cutoff;
341 while (seqs.size() != 0){
343 seqs = cluster->getSeqs();
345 //to account for cutoff change in average neighbor
346 if (seqs.size() != 0) {
347 if (seqs[0].dist > cutoff) { break; }
350 if (m->control_pressed) {
355 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
359 for (int i = 0; i < seqs.size(); i++) { //-1 means skip me
361 if (seqs[i].seq1 != seqs[i].seq2) {
362 cutoff = cluster->update(seqs[i].seq1, seqs[i].seq2, seqs[i].dist);
364 if (m->control_pressed) {
369 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
376 rndDist = m->ceilDist(seqs[i].dist, precision);
378 rndDist = m->roundDist(seqs[i].dist, precision);
382 if((previousDist <= 0.0000) && (seqs[i].dist != previousDist)){
385 else if((rndDist != rndPreviousDist)){
386 printData(toString(rndPreviousDist, length-1));
389 previousDist = seqs[i].dist;
390 rndPreviousDist = rndDist;
397 if (m->control_pressed) {
402 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
406 if(previousDist <= 0.0000){
409 else if(rndPreviousDist<cutoff){
410 printData(toString(rndPreviousDist, length-1));
418 if (m->control_pressed) {
419 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
424 if (saveCutoff != cutoff) {
425 if (hard) { saveCutoff = m->ceilDist(saveCutoff, precision); }
426 else { saveCutoff = m->roundDist(saveCutoff, precision); }
428 m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine();
431 //set list file as new current listfile
433 itTypes = outputTypes.find("list");
434 if (itTypes != outputTypes.end()) {
435 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
438 //set rabund file as new current rabundfile
439 itTypes = outputTypes.find("rabund");
440 if (itTypes != outputTypes.end()) {
441 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
444 //set sabund file as new current sabundfile
445 itTypes = outputTypes.find("sabund");
446 if (itTypes != outputTypes.end()) {
447 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
451 m->mothurOutEndLine();
452 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
453 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
454 m->mothurOutEndLine();
456 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster. "); m->mothurOutEndLine();
460 catch(exception& e) {
461 m->errorOut(e, "HClusterCommand", "execute");
466 //**********************************************************************************************************************
468 void HClusterCommand::printData(string label){
470 if (m->isTrue(timing)) {
471 m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
472 + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
478 oldRAbund.setLabel(label);
479 if (m->isTrue(showabund)) {
480 oldRAbund.getSAbundVector().print(cout);
482 oldRAbund.print(rabundFile);
483 oldRAbund.getSAbundVector().print(sabundFile);
485 oldList.setLabel(label);
486 oldList.print(listFile);
488 catch(exception& e) {
489 m->errorOut(e, "HClusterCommand", "printData");
495 //**********************************************************************************************************************