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 HClusterCommand::HClusterCommand(){
57 abort = true; calledHelp = true;
59 vector<string> tempOutNames;
60 outputTypes["list"] = tempOutNames;
61 outputTypes["rabund"] = tempOutNames;
62 outputTypes["sabund"] = tempOutNames;
65 m->errorOut(e, "HClusterCommand", "HClusterCommand");
69 //**********************************************************************************************************************
70 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
71 HClusterCommand::HClusterCommand(string option) {
73 abort = false; calledHelp = false;
75 //allow user to run help
76 if(option == "help") { help(); abort = true; calledHelp = true; }
77 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
80 vector<string> myArray = setParameters();
82 OptionParser parser(option);
83 map<string,string> parameters = parser.getParameters();
85 ValidParameters validParameter;
86 map<string,string>::iterator it;
88 //check to make sure all parameters are valid for command
89 for (it = parameters.begin(); it != parameters.end(); it++) {
90 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
95 //initialize outputTypes
96 vector<string> tempOutNames;
97 outputTypes["list"] = tempOutNames;
98 outputTypes["rabund"] = tempOutNames;
99 outputTypes["sabund"] = tempOutNames;
101 //if the user changes the input directory command factory will send this info to us in the output parameter
102 string inputDir = validParameter.validFile(parameters, "inputdir", false);
103 if (inputDir == "not found"){ inputDir = ""; }
106 it = parameters.find("phylip");
107 //user has given a template file
108 if(it != parameters.end()){
109 path = m->hasPath(it->second);
110 //if the user has not given a path then, add inputdir. else leave path alone.
111 if (path == "") { parameters["phylip"] = inputDir + it->second; }
114 it = parameters.find("column");
115 //user has given a template file
116 if(it != parameters.end()){
117 path = m->hasPath(it->second);
118 //if the user has not given a path then, add inputdir. else leave path alone.
119 if (path == "") { parameters["column"] = inputDir + it->second; }
122 it = parameters.find("name");
123 //user has given a template file
124 if(it != parameters.end()){
125 path = m->hasPath(it->second);
126 //if the user has not given a path then, add inputdir. else leave path alone.
127 if (path == "") { parameters["name"] = inputDir + it->second; }
131 //if the user changes the output directory command factory will send this info to us in the output parameter
132 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
134 //check for required parameters
135 phylipfile = validParameter.validFile(parameters, "phylip", true);
136 if (phylipfile == "not open") { 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") { 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 != "") { m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
156 phylipfile = m->getPhylipFile();
157 if (phylipfile != "") { 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 hcluster command."); m->mothurOutEndLine();
164 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; }
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 m->mothurConvert(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, "cutoff", false);
191 if (temp == "not found") { temp = "10"; }
192 m->mothurConvert(temp, cutoff);
193 cutoff += (5 / (precision * 10.0));
195 method = validParameter.validFile(parameters, "method", false);
196 if (method == "not found") { method = "average"; }
198 if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { }
199 else { m->mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest, average or weighted."); m->mothurOutEndLine(); abort = true; }
201 showabund = validParameter.validFile(parameters, "showabund", false);
202 if (showabund == "not found") { showabund = "T"; }
204 sort = validParameter.validFile(parameters, "sorted", false);
205 if (sort == "not found") { sort = "F"; }
206 sorted = m->isTrue(sort);
208 timing = validParameter.validFile(parameters, "timing", false);
209 if (timing == "not found") { timing = "F"; }
212 if (abort == false) {
214 if (outputDir == "") { outputDir += m->hasPath(distfile); }
215 fileroot = outputDir + m->getRootName(m->getSimpleName(distfile));
217 if (method == "furthest") { tag = "fn"; }
218 else if (method == "nearest") { tag = "nn"; }
219 else if (method == "weighted") { tag = "wn"; }
222 m->openOutputFile(fileroot+ tag + ".sabund", sabundFile);
223 m->openOutputFile(fileroot+ tag + ".rabund", rabundFile);
224 m->openOutputFile(fileroot+ tag + ".list", listFile);
226 outputNames.push_back(fileroot+ tag + ".sabund"); outputTypes["sabund"].push_back(fileroot+ tag + ".sabund");
227 outputNames.push_back(fileroot+ tag + ".rabund"); outputTypes["rabund"].push_back(fileroot+ tag + ".rabund");
228 outputNames.push_back(fileroot+ tag + ".list"); outputTypes["list"].push_back(fileroot+ tag + ".list");
232 catch(exception& e) {
233 m->errorOut(e, "HClusterCommand", "HClusterCommand");
238 //**********************************************************************************************************************
240 int HClusterCommand::execute(){
243 if (abort == true) { if (calledHelp) { return 0; } return 2; }
245 NameAssignment* nameMap = NULL;
247 nameMap = new NameAssignment(namefile);
251 time_t estart = time(NULL);
254 read = new ReadCluster(distfile, cutoff, outputDir, true);
255 read->setFormat(format);
258 if (m->control_pressed) {
263 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
267 distfile = read->getOutputFile();
269 list = read->getListVector();
272 list = new ListVector(nameMap->getListVector());
275 if (m->control_pressed) {
279 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
283 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to sort. "); m->mothurOutEndLine();
286 //list vector made by read contains all sequence names
288 rabund = new RAbundVector(list->getRAbundVector());
290 m->mothurOut("Error: no list vector!"); m->mothurOutEndLine(); return 0;
293 float previousDist = 0.00000;
294 float rndPreviousDist = 0.00000;
301 cluster = new HCluster(rabund, list, method, distfile, nameMap, cutoff);
302 vector<seqDist> seqs; seqs.resize(1); // to start loop
304 if (m->control_pressed) {
309 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
313 float saveCutoff = cutoff;
315 while (seqs.size() != 0){
317 seqs = cluster->getSeqs();
319 //to account for cutoff change in average neighbor
320 if (seqs.size() != 0) {
321 if (seqs[0].dist > cutoff) { break; }
324 if (m->control_pressed) {
329 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
333 for (int i = 0; i < seqs.size(); i++) { //-1 means skip me
335 if (seqs[i].seq1 != seqs[i].seq2) {
336 cutoff = cluster->update(seqs[i].seq1, seqs[i].seq2, seqs[i].dist);
338 if (m->control_pressed) {
343 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
350 rndDist = m->ceilDist(seqs[i].dist, precision);
352 rndDist = m->roundDist(seqs[i].dist, precision);
356 if((previousDist <= 0.0000) && (seqs[i].dist != previousDist)){
359 else if((rndDist != rndPreviousDist)){
360 printData(toString(rndPreviousDist, length-1));
363 previousDist = seqs[i].dist;
364 rndPreviousDist = rndDist;
371 if (m->control_pressed) {
376 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
380 if(previousDist <= 0.0000){
383 else if(rndPreviousDist<cutoff){
384 printData(toString(rndPreviousDist, length-1));
392 if (m->control_pressed) {
393 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
398 if (saveCutoff != cutoff) {
399 if (hard) { saveCutoff = m->ceilDist(saveCutoff, precision); }
400 else { saveCutoff = m->roundDist(saveCutoff, precision); }
402 m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine();
405 //set list file as new current listfile
407 itTypes = outputTypes.find("list");
408 if (itTypes != outputTypes.end()) {
409 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
412 //set rabund file as new current rabundfile
413 itTypes = outputTypes.find("rabund");
414 if (itTypes != outputTypes.end()) {
415 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
418 //set sabund file as new current sabundfile
419 itTypes = outputTypes.find("sabund");
420 if (itTypes != outputTypes.end()) {
421 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
425 m->mothurOutEndLine();
426 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
427 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
428 m->mothurOutEndLine();
430 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster. "); m->mothurOutEndLine();
434 catch(exception& e) {
435 m->errorOut(e, "HClusterCommand", "execute");
440 //**********************************************************************************************************************
442 void HClusterCommand::printData(string label){
444 if (m->isTrue(timing)) {
445 m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
446 + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
452 oldRAbund.setLabel(label);
453 if (m->isTrue(showabund)) {
454 oldRAbund.getSAbundVector().print(cout);
456 oldRAbund.print(rabundFile);
457 oldRAbund.getSAbundVector().print(sabundFile);
459 oldList.setLabel(label);
460 oldList.print(listFile);
462 catch(exception& e) {
463 m->errorOut(e, "HClusterCommand", "printData");
469 //**********************************************************************************************************************