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::getValidParameters(){
15 string Array[] = {"cutoff","hard","precision","method","phylip","column","name","sorted","showabund","timing","outputdir","inputdir"};
16 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
20 m->errorOut(e, "HClusterCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 HClusterCommand::HClusterCommand(){
27 abort = true; calledHelp = true;
28 vector<string> tempOutNames;
29 outputTypes["list"] = tempOutNames;
30 outputTypes["rabund"] = tempOutNames;
31 outputTypes["sabund"] = tempOutNames;
34 m->errorOut(e, "HClusterCommand", "HClusterCommand");
38 //**********************************************************************************************************************
39 vector<string> HClusterCommand::getRequiredParameters(){
41 string Array[] = {"phylip","column","or"};
42 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
46 m->errorOut(e, "HClusterCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> HClusterCommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "HClusterCommand", "getRequiredFiles");
61 //**********************************************************************************************************************
62 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
63 HClusterCommand::HClusterCommand(string option) {
65 globaldata = GlobalData::getInstance();
66 abort = false; calledHelp = false;
68 //allow user to run help
69 if(option == "help") { help(); abort = true; calledHelp = true; }
72 //valid paramters for this command
73 string Array[] = {"cutoff","hard","precision","method","phylip","column","name","sorted","showabund","timing","outputdir","inputdir"};
74 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
76 OptionParser parser(option);
77 map<string,string> parameters = parser.getParameters();
79 ValidParameters validParameter;
80 map<string,string>::iterator it;
82 //check to make sure all parameters are valid for command
83 for (it = parameters.begin(); it != parameters.end(); it++) {
84 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
89 //initialize outputTypes
90 vector<string> tempOutNames;
91 outputTypes["list"] = tempOutNames;
92 outputTypes["rabund"] = tempOutNames;
93 outputTypes["sabund"] = tempOutNames;
95 globaldata->newRead();
97 //if the user changes the input directory command factory will send this info to us in the output parameter
98 string inputDir = validParameter.validFile(parameters, "inputdir", false);
99 if (inputDir == "not found"){ inputDir = ""; }
102 it = parameters.find("phylip");
103 //user has given a template file
104 if(it != parameters.end()){
105 path = m->hasPath(it->second);
106 //if the user has not given a path then, add inputdir. else leave path alone.
107 if (path == "") { parameters["phylip"] = inputDir + it->second; }
110 it = parameters.find("column");
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["column"] = inputDir + it->second; }
118 it = parameters.find("name");
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["name"] = inputDir + it->second; }
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 //check for required parameters
131 phylipfile = validParameter.validFile(parameters, "phylip", true);
132 if (phylipfile == "not open") { abort = true; }
133 else if (phylipfile == "not found") { phylipfile = ""; }
134 else { distfile = phylipfile; format = "phylip"; }
136 columnfile = validParameter.validFile(parameters, "column", true);
137 if (columnfile == "not open") { abort = true; }
138 else if (columnfile == "not found") { columnfile = ""; }
139 else { distfile = columnfile; format = "column"; }
141 namefile = validParameter.validFile(parameters, "name", true);
142 if (namefile == "not open") { abort = true; }
143 else if (namefile == "not found") { namefile = ""; }
145 if ((phylipfile == "") && (columnfile == "")) { m->mothurOut("When executing a hcluster command you must enter a phylip or a column."); m->mothurOutEndLine(); abort = true; }
146 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; }
148 if (columnfile != "") {
149 if (namefile == "") { cout << "You need to provide a namefile if you are going to use the column format." << endl; abort = true; }
152 //check for optional parameter and set defaults
153 // ...at some point should added some additional type checking...
154 //get user cutoff and precision or use defaults
156 temp = validParameter.validFile(parameters, "precision", false);
157 if (temp == "not found") { temp = "100"; }
158 //saves precision legnth for formatting below
159 length = temp.length();
160 convert(temp, precision);
162 temp = validParameter.validFile(parameters, "hard", false); if (temp == "not found") { temp = "F"; }
163 hard = m->isTrue(temp);
165 temp = validParameter.validFile(parameters, "cutoff", false);
166 if (temp == "not found") { temp = "10"; }
167 convert(temp, cutoff);
168 cutoff += (5 / (precision * 10.0));
170 method = validParameter.validFile(parameters, "method", false);
171 if (method == "not found") { method = "furthest"; }
173 if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { }
174 else { m->mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest, average or weighted."); m->mothurOutEndLine(); abort = true; }
176 showabund = validParameter.validFile(parameters, "showabund", false);
177 if (showabund == "not found") { showabund = "T"; }
179 sort = validParameter.validFile(parameters, "sorted", false);
180 if (sort == "not found") { sort = "F"; }
181 sorted = m->isTrue(sort);
183 timing = validParameter.validFile(parameters, "timing", false);
184 if (timing == "not found") { timing = "F"; }
187 if (abort == false) {
189 if (outputDir == "") { outputDir += m->hasPath(distfile); }
190 fileroot = outputDir + m->getRootName(m->getSimpleName(distfile));
192 if (method == "furthest") { tag = "fn"; }
193 else if (method == "nearest") { tag = "nn"; }
194 else if (method == "weighted") { tag = "wn"; }
197 m->openOutputFile(fileroot+ tag + ".sabund", sabundFile);
198 m->openOutputFile(fileroot+ tag + ".rabund", rabundFile);
199 m->openOutputFile(fileroot+ tag + ".list", listFile);
201 outputNames.push_back(fileroot+ tag + ".sabund"); outputTypes["sabund"].push_back(fileroot+ tag + ".sabund");
202 outputNames.push_back(fileroot+ tag + ".rabund"); outputTypes["rabund"].push_back(fileroot+ tag + ".rabund");
203 outputNames.push_back(fileroot+ tag + ".list"); outputTypes["list"].push_back(fileroot+ tag + ".list");
207 catch(exception& e) {
208 m->errorOut(e, "HClusterCommand", "HClusterCommand");
213 //**********************************************************************************************************************
215 void HClusterCommand::help(){
217 m->mothurOut("The hcluster command parameter options are cutoff, precision, method, phylip, column, name, showabund, timing and sorted. Phylip or column and name are required.\n");
218 m->mothurOut("The phylip and column parameter allow you to enter your distance file, and sorted indicates whether your column distance file is already sorted. \n");
219 m->mothurOut("The name parameter allows you to enter your name file and is required if your distance file is in column format. \n");
220 m->mothurOut("The hcluster command should be in the following format: \n");
221 m->mothurOut("hcluster(column=youDistanceFile, name=yourNameFile, method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
222 m->mothurOut("The acceptable hcluster methods are furthest, nearest, weighted and average.\n\n");
224 catch(exception& e) {
225 m->errorOut(e, "HClusterCommand", "help");
230 //**********************************************************************************************************************
232 HClusterCommand::~HClusterCommand(){}
234 //**********************************************************************************************************************
236 int HClusterCommand::execute(){
239 if (abort == true) { if (calledHelp) { return 0; } return 2; }
242 globaldata->nameMap = new NameAssignment(namefile);
243 globaldata->nameMap->readMap();
245 globaldata->nameMap = NULL;
248 time_t estart = time(NULL);
251 read = new ReadCluster(distfile, cutoff, outputDir, true);
252 read->setFormat(format);
253 read->read(globaldata->nameMap);
255 if (m->control_pressed) {
260 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
264 distfile = read->getOutputFile();
266 list = read->getListVector();
269 list = new ListVector(globaldata->nameMap->getListVector());
272 if (m->control_pressed) {
276 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
280 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to sort. "); m->mothurOutEndLine();
283 //list vector made by read contains all sequence names
285 rabund = new RAbundVector(list->getRAbundVector());
287 m->mothurOut("Error: no list vector!"); m->mothurOutEndLine(); return 0;
290 float previousDist = 0.00000;
291 float rndPreviousDist = 0.00000;
298 cluster = new HCluster(rabund, list, method, distfile, globaldata->nameMap, cutoff);
299 vector<seqDist> seqs; seqs.resize(1); // to start loop
301 if (m->control_pressed) {
306 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
311 while (seqs.size() != 0){
313 seqs = cluster->getSeqs();
315 if (m->control_pressed) {
320 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
324 for (int i = 0; i < seqs.size(); i++) { //-1 means skip me
326 if (seqs[i].seq1 != seqs[i].seq2) {
327 cluster->update(seqs[i].seq1, seqs[i].seq2, seqs[i].dist);
329 if (m->control_pressed) {
334 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
341 rndDist = m->ceilDist(seqs[i].dist, precision);
343 rndDist = m->roundDist(seqs[i].dist, precision);
347 if((previousDist <= 0.0000) && (seqs[i].dist != previousDist)){
350 else if((rndDist != rndPreviousDist)){
351 printData(toString(rndPreviousDist, length-1));
354 previousDist = seqs[i].dist;
355 rndPreviousDist = rndDist;
362 if (m->control_pressed) {
367 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
371 if(previousDist <= 0.0000){
374 else if(rndPreviousDist<cutoff){
375 printData(toString(rndPreviousDist, length-1));
378 //delete globaldata's copy of the sparsematrix and listvector to free up memory
379 delete globaldata->gListVector; globaldata->gListVector = NULL;
381 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
382 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
383 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
385 globaldata->setListFile(fileroot+ tag + ".list");
386 globaldata->setNameFile("");
387 globaldata->setFormat("list");
394 if (m->control_pressed) {
395 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
400 m->mothurOutEndLine();
401 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
402 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
403 m->mothurOutEndLine();
405 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster. "); m->mothurOutEndLine();
409 catch(exception& e) {
410 m->errorOut(e, "HClusterCommand", "execute");
415 //**********************************************************************************************************************
417 void HClusterCommand::printData(string label){
419 if (m->isTrue(timing)) {
420 m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
421 + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
427 oldRAbund.setLabel(label);
428 if (m->isTrue(showabund)) {
429 oldRAbund.getSAbundVector().print(cout);
431 oldRAbund.print(rabundFile);
432 oldRAbund.getSAbundVector().print(sabundFile);
434 oldList.setLabel(label);
435 oldList.print(listFile);
437 catch(exception& e) {
438 m->errorOut(e, "HClusterCommand", "printData");
444 //**********************************************************************************************************************