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(){
28 //initialize outputTypes
29 vector<string> tempOutNames;
30 outputTypes["list"] = tempOutNames;
31 outputTypes["rabund"] = tempOutNames;
32 outputTypes["sabund"] = tempOutNames;
35 m->errorOut(e, "HClusterCommand", "HClusterCommand");
39 //**********************************************************************************************************************
40 vector<string> HClusterCommand::getRequiredParameters(){
42 string Array[] = {"phylip","column","or"};
43 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
47 m->errorOut(e, "HClusterCommand", "getRequiredParameters");
51 //**********************************************************************************************************************
52 vector<string> HClusterCommand::getRequiredFiles(){
54 vector<string> myArray;
58 m->errorOut(e, "HClusterCommand", "getRequiredFiles");
62 //**********************************************************************************************************************
63 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
64 HClusterCommand::HClusterCommand(string option) {
66 globaldata = GlobalData::getInstance();
69 //allow user to run help
70 if(option == "help") { help(); abort = true; }
73 //valid paramters for this command
74 string Array[] = {"cutoff","hard","precision","method","phylip","column","name","sorted","showabund","timing","outputdir","inputdir"};
75 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
77 OptionParser parser(option);
78 map<string,string> parameters = parser.getParameters();
80 ValidParameters validParameter;
81 map<string,string>::iterator it;
83 //check to make sure all parameters are valid for command
84 for (it = parameters.begin(); it != parameters.end(); it++) {
85 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
90 //initialize outputTypes
91 vector<string> tempOutNames;
92 outputTypes["list"] = tempOutNames;
93 outputTypes["rabund"] = tempOutNames;
94 outputTypes["sabund"] = tempOutNames;
96 globaldata->newRead();
98 //if the user changes the input directory command factory will send this info to us in the output parameter
99 string inputDir = validParameter.validFile(parameters, "inputdir", false);
100 if (inputDir == "not found"){ inputDir = ""; }
103 it = parameters.find("phylip");
104 //user has given a template file
105 if(it != parameters.end()){
106 path = m->hasPath(it->second);
107 //if the user has not given a path then, add inputdir. else leave path alone.
108 if (path == "") { parameters["phylip"] = inputDir + it->second; }
111 it = parameters.find("column");
112 //user has given a template file
113 if(it != parameters.end()){
114 path = m->hasPath(it->second);
115 //if the user has not given a path then, add inputdir. else leave path alone.
116 if (path == "") { parameters["column"] = inputDir + it->second; }
119 it = parameters.find("name");
120 //user has given a template file
121 if(it != parameters.end()){
122 path = m->hasPath(it->second);
123 //if the user has not given a path then, add inputdir. else leave path alone.
124 if (path == "") { parameters["name"] = inputDir + it->second; }
128 //if the user changes the output directory command factory will send this info to us in the output parameter
129 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
131 //check for required parameters
132 phylipfile = validParameter.validFile(parameters, "phylip", true);
133 if (phylipfile == "not open") { abort = true; }
134 else if (phylipfile == "not found") { phylipfile = ""; }
135 else { distfile = phylipfile; format = "phylip"; }
137 columnfile = validParameter.validFile(parameters, "column", true);
138 if (columnfile == "not open") { abort = true; }
139 else if (columnfile == "not found") { columnfile = ""; }
140 else { distfile = columnfile; format = "column"; }
142 namefile = validParameter.validFile(parameters, "name", true);
143 if (namefile == "not open") { abort = true; }
144 else if (namefile == "not found") { namefile = ""; }
146 if ((phylipfile == "") && (columnfile == "")) { m->mothurOut("When executing a hcluster command you must enter a phylip or a column."); m->mothurOutEndLine(); abort = true; }
147 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; }
149 if (columnfile != "") {
150 if (namefile == "") { cout << "You need to provide a namefile if you are going to use the column format." << endl; abort = true; }
153 //check for optional parameter and set defaults
154 // ...at some point should added some additional type checking...
155 //get user cutoff and precision or use defaults
157 temp = validParameter.validFile(parameters, "precision", false);
158 if (temp == "not found") { temp = "100"; }
159 //saves precision legnth for formatting below
160 length = temp.length();
161 convert(temp, precision);
163 temp = validParameter.validFile(parameters, "hard", false); if (temp == "not found") { temp = "F"; }
164 hard = m->isTrue(temp);
166 temp = validParameter.validFile(parameters, "cutoff", false);
167 if (temp == "not found") { temp = "10"; }
168 convert(temp, cutoff);
169 cutoff += (5 / (precision * 10.0));
171 method = validParameter.validFile(parameters, "method", false);
172 if (method == "not found") { method = "furthest"; }
174 if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { }
175 else { m->mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest, average or weighted."); m->mothurOutEndLine(); abort = true; }
177 showabund = validParameter.validFile(parameters, "showabund", false);
178 if (showabund == "not found") { showabund = "T"; }
180 sort = validParameter.validFile(parameters, "sorted", false);
181 if (sort == "not found") { sort = "F"; }
182 sorted = m->isTrue(sort);
184 timing = validParameter.validFile(parameters, "timing", false);
185 if (timing == "not found") { timing = "F"; }
188 if (abort == false) {
190 if (outputDir == "") { outputDir += m->hasPath(distfile); }
191 fileroot = outputDir + m->getRootName(m->getSimpleName(distfile));
193 if (method == "furthest") { tag = "fn"; }
194 else if (method == "nearest") { tag = "nn"; }
195 else if (method == "weighted") { tag = "wn"; }
198 m->openOutputFile(fileroot+ tag + ".sabund", sabundFile);
199 m->openOutputFile(fileroot+ tag + ".rabund", rabundFile);
200 m->openOutputFile(fileroot+ tag + ".list", listFile);
202 outputNames.push_back(fileroot+ tag + ".sabund"); outputTypes["sabund"].push_back(fileroot+ tag + ".sabund");
203 outputNames.push_back(fileroot+ tag + ".rabund"); outputTypes["rabund"].push_back(fileroot+ tag + ".rabund");
204 outputNames.push_back(fileroot+ tag + ".list"); outputTypes["list"].push_back(fileroot+ tag + ".list");
208 catch(exception& e) {
209 m->errorOut(e, "HClusterCommand", "HClusterCommand");
214 //**********************************************************************************************************************
216 void HClusterCommand::help(){
218 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");
219 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");
220 m->mothurOut("The name parameter allows you to enter your name file and is required if your distance file is in column format. \n");
221 m->mothurOut("The hcluster command should be in the following format: \n");
222 m->mothurOut("hcluster(column=youDistanceFile, name=yourNameFile, method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
223 m->mothurOut("The acceptable hcluster methods are furthest, nearest, weighted and average.\n\n");
225 catch(exception& e) {
226 m->errorOut(e, "HClusterCommand", "help");
231 //**********************************************************************************************************************
233 HClusterCommand::~HClusterCommand(){}
235 //**********************************************************************************************************************
237 int HClusterCommand::execute(){
240 if (abort == true) { return 0; }
243 globaldata->nameMap = new NameAssignment(namefile);
244 globaldata->nameMap->readMap();
246 globaldata->nameMap = NULL;
249 time_t estart = time(NULL);
252 read = new ReadCluster(distfile, cutoff, outputDir, true);
253 read->setFormat(format);
254 read->read(globaldata->nameMap);
256 if (m->control_pressed) {
261 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
265 distfile = read->getOutputFile();
267 list = read->getListVector();
270 list = new ListVector(globaldata->nameMap->getListVector());
273 if (m->control_pressed) {
277 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
281 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to sort. "); m->mothurOutEndLine();
284 //list vector made by read contains all sequence names
286 rabund = new RAbundVector(list->getRAbundVector());
288 m->mothurOut("Error: no list vector!"); m->mothurOutEndLine(); return 0;
291 float previousDist = 0.00000;
292 float rndPreviousDist = 0.00000;
299 cluster = new HCluster(rabund, list, method, distfile, globaldata->nameMap, cutoff);
300 vector<seqDist> seqs; seqs.resize(1); // to start loop
302 if (m->control_pressed) {
307 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
312 while (seqs.size() != 0){
314 seqs = cluster->getSeqs();
316 if (m->control_pressed) {
321 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
325 for (int i = 0; i < seqs.size(); i++) { //-1 means skip me
327 if (seqs[i].seq1 != seqs[i].seq2) {
328 cluster->update(seqs[i].seq1, seqs[i].seq2, seqs[i].dist);
330 if (m->control_pressed) {
335 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
342 rndDist = m->ceilDist(seqs[i].dist, precision);
344 rndDist = m->roundDist(seqs[i].dist, precision);
348 if((previousDist <= 0.0000) && (seqs[i].dist != previousDist)){
351 else if((rndDist != rndPreviousDist)){
352 printData(toString(rndPreviousDist, length-1));
355 previousDist = seqs[i].dist;
356 rndPreviousDist = rndDist;
363 if (m->control_pressed) {
368 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
372 if(previousDist <= 0.0000){
375 else if(rndPreviousDist<cutoff){
376 printData(toString(rndPreviousDist, length-1));
379 //delete globaldata's copy of the sparsematrix and listvector to free up memory
380 delete globaldata->gListVector; globaldata->gListVector = NULL;
382 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
383 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
384 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
386 globaldata->setListFile(fileroot+ tag + ".list");
387 globaldata->setNameFile("");
388 globaldata->setFormat("list");
395 if (m->control_pressed) {
396 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
401 m->mothurOutEndLine();
402 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
403 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
404 m->mothurOutEndLine();
406 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster. "); m->mothurOutEndLine();
410 catch(exception& e) {
411 m->errorOut(e, "HClusterCommand", "execute");
416 //**********************************************************************************************************************
418 void HClusterCommand::printData(string label){
420 if (m->isTrue(timing)) {
421 m->mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
422 + "\tclusters. Updates: " + toString(loops)); m->mothurOutEndLine();
428 oldRAbund.setLabel(label);
429 if (m->isTrue(showabund)) {
430 oldRAbund.getSAbundVector().print(cout);
432 oldRAbund.print(rabundFile);
433 oldRAbund.getSAbundVector().print(sabundFile);
435 oldList.setLabel(label);
436 oldList.print(listFile);
438 catch(exception& e) {
439 m->errorOut(e, "HClusterCommand", "printData");
445 //**********************************************************************************************************************