5 * Created by westcott on 10/13/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
10 #include "hclustercommand.h"
12 //**********************************************************************************************************************
13 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
14 HClusterCommand::HClusterCommand(string option){
16 globaldata = GlobalData::getInstance();
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
23 //valid paramters for this command
24 string Array[] = {"cutoff","precision","method","showabund","timing","phylip","column","name","sorted"};
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string,string> parameters = parser.getParameters();
30 ValidParameters validParameter;
32 //check to make sure all parameters are valid for command
33 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
34 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
39 globaldata->newRead();
41 //check for required parameters
42 phylipfile = validParameter.validFile(parameters, "phylip", true);
43 if (phylipfile == "not open") { abort = true; }
44 else if (phylipfile == "not found") { phylipfile = ""; }
45 else { distfile = phylipfile; format = "phylip"; }
47 columnfile = validParameter.validFile(parameters, "column", true);
48 if (columnfile == "not open") { abort = true; }
49 else if (columnfile == "not found") { columnfile = ""; }
50 else { distfile = columnfile; format = "column"; }
52 namefile = validParameter.validFile(parameters, "name", true);
53 if (namefile == "not open") { abort = true; }
54 else if (namefile == "not found") { namefile = ""; }
56 if ((phylipfile == "") && (columnfile == "")) { mothurOut("When executing a hcluster command you must enter a phylip or a column."); mothurOutEndLine(); abort = true; }
57 else if ((phylipfile != "") && (columnfile != "")) { mothurOut("When executing a hcluster command you must enter ONLY ONE of the following: phylip or column."); mothurOutEndLine(); abort = true; }
59 if (columnfile != "") {
60 if (namefile == "") { cout << "You need to provide a namefile if you are going to use the column format." << endl; abort = true; }
63 //check for optional parameter and set defaults
64 // ...at some point should added some additional type checking...
65 //get user cutoff and precision or use defaults
67 temp = validParameter.validFile(parameters, "precision", false);
68 if (temp == "not found") { temp = "100"; }
69 //saves precision legnth for formatting below
70 length = temp.length();
71 convert(temp, precision);
73 temp = validParameter.validFile(parameters, "cutoff", false);
74 if (temp == "not found") { temp = "10"; }
75 convert(temp, cutoff);
76 cutoff += (5 / (precision * 10.0));
78 method = validParameter.validFile(parameters, "method", false);
79 if (method == "not found") { method = "nearest"; }
81 if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
82 else { mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average."); mothurOutEndLine(); abort = true; }
84 showabund = validParameter.validFile(parameters, "showabund", false);
85 if (showabund == "not found") { showabund = "T"; }
87 sort = validParameter.validFile(parameters, "sorted", false);
88 if (sort == "not found") { sort = "F"; }
89 sorted = isTrue(sort);
91 timing = validParameter.validFile(parameters, "timing", false);
92 if (timing == "not found") { timing = "F"; }
97 fileroot = getRootName(distfile);
99 tag = "fn"; //until we figure out average and nearest methods
101 openOutputFile(fileroot+ tag + ".sabund", sabundFile);
102 openOutputFile(fileroot+ tag + ".rabund", rabundFile);
103 openOutputFile(fileroot+ tag + ".list", listFile);
107 catch(exception& e) {
108 errorOut(e, "HClusterCommand", "HClusterCommand");
113 //**********************************************************************************************************************
115 void HClusterCommand::help(){
117 mothurOut("The hcluster command parameter options are cutoff, precision, method, showabund, timing, phylip, column, name and sorted. Phylip or column and name are required.\n");
118 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");
119 mothurOut("The name parameter allows you to enter your name file and is required if your distance file is in column format. \n");
120 mothurOut("The hcluster command should be in the following format: \n");
121 mothurOut("hcluster(column=youDistanceFile, name=yourNameFile, method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
122 mothurOut("The acceptable hcluster methods is furthest, but we hope to add nearest and average in the future.\n\n");
124 catch(exception& e) {
125 errorOut(e, "HClusterCommand", "help");
130 //**********************************************************************************************************************
132 HClusterCommand::~HClusterCommand(){}
134 //**********************************************************************************************************************
136 int HClusterCommand::execute(){
139 if (abort == true) { return 0; }
142 globaldata->nameMap = new NameAssignment(namefile);
143 globaldata->nameMap->readMap();
145 globaldata->nameMap = NULL;
148 time_t estart = time(NULL);
151 read = new ReadCluster(distfile, cutoff);
152 read->setFormat(format);
153 read->read(globaldata->nameMap);
154 distfile = read->getOutputFile();
156 list = read->getListVector();
159 list = new ListVector(globaldata->nameMap->getListVector());
162 mothurOut("It took " + toString(time(NULL) - estart) + " seconds to sort. "); mothurOutEndLine();
165 //list vector made by read contains all sequence names
167 rabund = new RAbundVector(list->getRAbundVector());
169 mothurOut("Error: no list vector!"); mothurOutEndLine(); return 0;
172 float previousDist = 0.00000;
173 float rndPreviousDist = 0.00000;
180 //cout << "here" << endl;
182 openInputFile(distfile, in);
183 string firstName, secondName;
186 cluster = new HCluster(rabund, list);
187 bool clusteredSomething;
188 vector<seqDist> seqs; seqs.resize(1); // to start loop
189 exitedBreak = false; //lets you know if there is a distance stored in next
191 while (seqs.size() != 0){
194 random_shuffle(seqs.begin(), seqs.end());
196 if (seqs.size() == 0) { break; } //there are no more distances
198 for (int i = 0; i < seqs.size(); i++) { //-1 means skip me
200 if (print_start && isTrue(timing)) {
201 mothurOut("Clustering (" + tag + ") dist " + toString(distance) + "/"
202 + toString(roundDist(distance, precision))
203 + "\t(precision: " + toString(precision) + ")");
208 ///cout << "before cluster update" << endl;
209 if (seqs[i].seq1 != seqs[i].seq2) {
210 clusteredSomething = cluster->update(seqs[i].seq1, seqs[i].seq2, seqs[i].dist);
212 float rndDist = roundDist(seqs[i].dist, precision);
213 //cout << "after cluster update clusterSomething = " << clusteredSomething << " rndDist = " << rndDist << " rndPreviousDist = " << rndPreviousDist << endl;
216 if((previousDist <= 0.0000) && (seqs[i].dist != previousDist)){
219 else if((rndDist != rndPreviousDist)){
220 printData(toString(rndPreviousDist, length-1));
223 previousDist = seqs[i].dist;
224 rndPreviousDist = rndDist;
233 if (print_start && isTrue(timing)) {
234 //mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist)
235 //+ "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
240 if(previousDist <= 0.0000){
243 else if(rndPreviousDist<cutoff){
244 printData(toString(rndPreviousDist, length-1));
247 //delete globaldata's copy of the sparsematrix and listvector to free up memory
248 delete globaldata->gListVector; globaldata->gListVector = NULL;
250 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
251 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
252 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
254 globaldata->setListFile(fileroot+ tag + ".list");
255 globaldata->setNameFile("");
256 globaldata->setFormat("list");
263 //if (isTrue(timing)) {
264 mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster. "); mothurOutEndLine();
268 catch(exception& e) {
269 errorOut(e, "HClusterCommand", "execute");
274 //**********************************************************************************************************************
276 void HClusterCommand::printData(string label){
278 if (isTrue(timing)) {
279 mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
280 + "\tclusters. Updates: " + toString(loops)); mothurOutEndLine();
286 oldRAbund.setLabel(label);
287 if (isTrue(showabund)) {
288 oldRAbund.getSAbundVector().print(cout);
290 oldRAbund.print(rabundFile);
291 oldRAbund.getSAbundVector().print(sabundFile);
293 oldList.setLabel(label);
294 oldList.print(listFile);
296 catch(exception& e) {
297 errorOut(e, "HClusterCommand", "printData");
303 //**********************************************************************************************************************
305 vector<seqDist> HClusterCommand::getSeqs(ifstream& filehandle){
307 string firstName, secondName;
308 float distance, prevDistance;
309 vector<seqDist> sameSeqs;
312 //if you are not at the beginning of the file
314 sameSeqs.push_back(next);
315 prevDistance = next.dist;
322 filehandle >> firstName >> secondName >> distance;
323 //cout << firstName << '\t' << secondName << '\t' << distance << endl;
327 if (prevDistance == -1) { prevDistance = distance; }
329 map<string,int>::iterator itA = globaldata->nameMap->find(firstName);
330 map<string,int>::iterator itB = globaldata->nameMap->find(secondName);
332 if(itA == globaldata->nameMap->end()){
333 cerr << "AAError: Sequence '" << firstName << "' was not found in the names file, please correct\n"; exit(1);
335 if(itB == globaldata->nameMap->end()){
336 cerr << "ABError: Sequence '" << secondName << "' was not found in the names file, please correct\n"; exit(1);
340 if (distance > cutoff) { break; }
342 if (distance != -1) { //-1 means skip me
344 //are the distances the same
345 if (distance == prevDistance) { //save in vector
347 temp.seq1 = itA->second;
348 temp.seq2 = itB->second;
349 temp.dist = distance;
350 sameSeqs.push_back(temp);
352 //what about precision??
355 next.seq1 = itA->second;
356 next.seq2 = itB->second;
357 next.dist = distance;
367 catch(exception& e) {
368 errorOut(e, "HClusterCommand", "getSeqs");
375 //**********************************************************************************************************************