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","phylip","column","name","sorted","showabund","timing","outputdir","inputdir"};
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string,string> parameters = parser.getParameters();
30 ValidParameters validParameter;
31 map<string,string>::iterator it;
33 //check to make sure all parameters are valid for command
34 for (it = parameters.begin(); it != parameters.end(); it++) {
35 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
40 globaldata->newRead();
42 //if the user changes the input directory command factory will send this info to us in the output parameter
43 string inputDir = validParameter.validFile(parameters, "inputdir", false);
44 if (inputDir == "not found"){ inputDir = ""; }
47 it = parameters.find("phylip");
48 //user has given a template file
49 if(it != parameters.end()){
50 path = hasPath(it->second);
51 //if the user has not given a path then, add inputdir. else leave path alone.
52 if (path == "") { parameters["phylip"] = inputDir + it->second; }
55 it = parameters.find("column");
56 //user has given a template file
57 if(it != parameters.end()){
58 path = hasPath(it->second);
59 //if the user has not given a path then, add inputdir. else leave path alone.
60 if (path == "") { parameters["column"] = inputDir + it->second; }
63 it = parameters.find("name");
64 //user has given a template file
65 if(it != parameters.end()){
66 path = hasPath(it->second);
67 //if the user has not given a path then, add inputdir. else leave path alone.
68 if (path == "") { parameters["name"] = inputDir + it->second; }
72 //if the user changes the output directory command factory will send this info to us in the output parameter
73 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
75 //check for required parameters
76 phylipfile = validParameter.validFile(parameters, "phylip", true);
77 if (phylipfile == "not open") { abort = true; }
78 else if (phylipfile == "not found") { phylipfile = ""; }
79 else { distfile = phylipfile; format = "phylip"; }
81 columnfile = validParameter.validFile(parameters, "column", true);
82 if (columnfile == "not open") { abort = true; }
83 else if (columnfile == "not found") { columnfile = ""; }
84 else { distfile = columnfile; format = "column"; }
86 namefile = validParameter.validFile(parameters, "name", true);
87 if (namefile == "not open") { abort = true; }
88 else if (namefile == "not found") { namefile = ""; }
90 if ((phylipfile == "") && (columnfile == "")) { mothurOut("When executing a hcluster command you must enter a phylip or a column."); mothurOutEndLine(); abort = true; }
91 else if ((phylipfile != "") && (columnfile != "")) { mothurOut("When executing a hcluster command you must enter ONLY ONE of the following: phylip or column."); mothurOutEndLine(); abort = true; }
93 if (columnfile != "") {
94 if (namefile == "") { cout << "You need to provide a namefile if you are going to use the column format." << endl; abort = true; }
97 //check for optional parameter and set defaults
98 // ...at some point should added some additional type checking...
99 //get user cutoff and precision or use defaults
101 temp = validParameter.validFile(parameters, "precision", false);
102 if (temp == "not found") { temp = "100"; }
103 //saves precision legnth for formatting below
104 length = temp.length();
105 convert(temp, precision);
107 temp = validParameter.validFile(parameters, "cutoff", false);
108 if (temp == "not found") { temp = "10"; }
109 convert(temp, cutoff);
110 cutoff += (5 / (precision * 10.0));
112 method = validParameter.validFile(parameters, "method", false);
113 if (method == "not found") { method = "furthest"; }
115 if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
116 else { mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average."); mothurOutEndLine(); abort = true; }
118 showabund = validParameter.validFile(parameters, "showabund", false);
119 if (showabund == "not found") { showabund = "T"; }
121 sort = validParameter.validFile(parameters, "sorted", false);
122 if (sort == "not found") { sort = "F"; }
123 sorted = isTrue(sort);
125 timing = validParameter.validFile(parameters, "timing", false);
126 if (timing == "not found") { timing = "F"; }
129 if (abort == false) {
131 if (outputDir == "") { outputDir += hasPath(distfile); }
132 fileroot = outputDir + getRootName(getSimpleName(distfile));
134 if (method == "furthest") { tag = "fn"; }
135 else if (method == "nearest") { tag = "nn"; }
138 openOutputFile(fileroot+ tag + ".sabund", sabundFile);
139 openOutputFile(fileroot+ tag + ".rabund", rabundFile);
140 openOutputFile(fileroot+ tag + ".list", listFile);
144 catch(exception& e) {
145 errorOut(e, "HClusterCommand", "HClusterCommand");
150 //**********************************************************************************************************************
152 void HClusterCommand::help(){
154 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");
155 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");
156 mothurOut("The name parameter allows you to enter your name file and is required if your distance file is in column format. \n");
157 mothurOut("The hcluster command should be in the following format: \n");
158 mothurOut("hcluster(column=youDistanceFile, name=yourNameFile, method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
159 mothurOut("The acceptable hcluster methods are furthest and nearest, but we hope to add average in the future.\n\n");
161 catch(exception& e) {
162 errorOut(e, "HClusterCommand", "help");
167 //**********************************************************************************************************************
169 HClusterCommand::~HClusterCommand(){}
171 //**********************************************************************************************************************
173 int HClusterCommand::execute(){
176 if (abort == true) { return 0; }
179 globaldata->nameMap = new NameAssignment(namefile);
180 globaldata->nameMap->readMap();
182 globaldata->nameMap = NULL;
185 time_t estart = time(NULL);
188 read = new ReadCluster(distfile, cutoff);
189 read->setFormat(format);
190 read->read(globaldata->nameMap);
191 distfile = read->getOutputFile();
193 list = read->getListVector();
196 list = new ListVector(globaldata->nameMap->getListVector());
199 mothurOut("It took " + toString(time(NULL) - estart) + " seconds to sort. "); mothurOutEndLine();
202 //list vector made by read contains all sequence names
204 rabund = new RAbundVector(list->getRAbundVector());
206 mothurOut("Error: no list vector!"); mothurOutEndLine(); return 0;
209 float previousDist = 0.00000;
210 float rndPreviousDist = 0.00000;
217 cluster = new HCluster(rabund, list, method, distfile, globaldata->nameMap, cutoff);
218 vector<seqDist> seqs; seqs.resize(1); // to start loop
220 while (seqs.size() != 0){
222 seqs = cluster->getSeqs();
224 for (int i = 0; i < seqs.size(); i++) { //-1 means skip me
226 if (seqs[i].seq1 != seqs[i].seq2) {
227 cluster->update(seqs[i].seq1, seqs[i].seq2, seqs[i].dist);
229 float rndDist = roundDist(seqs[i].dist, precision);
231 if((previousDist <= 0.0000) && (seqs[i].dist != previousDist)){
234 else if((rndDist != rndPreviousDist)){
235 printData(toString(rndPreviousDist, length-1));
238 previousDist = seqs[i].dist;
239 rndPreviousDist = rndDist;
246 if(previousDist <= 0.0000){
249 else if(rndPreviousDist<cutoff){
250 printData(toString(rndPreviousDist, length-1));
253 //delete globaldata's copy of the sparsematrix and listvector to free up memory
254 delete globaldata->gListVector; globaldata->gListVector = NULL;
256 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
257 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
258 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
260 globaldata->setListFile(fileroot+ tag + ".list");
261 globaldata->setNameFile("");
262 globaldata->setFormat("list");
269 mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster. "); mothurOutEndLine();
273 catch(exception& e) {
274 errorOut(e, "HClusterCommand", "execute");
279 //**********************************************************************************************************************
281 void HClusterCommand::printData(string label){
283 if (isTrue(timing)) {
284 mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins())
285 + "\tclusters. Updates: " + toString(loops)); mothurOutEndLine();
291 oldRAbund.setLabel(label);
292 if (isTrue(showabund)) {
293 oldRAbund.getSAbundVector().print(cout);
295 oldRAbund.print(rabundFile);
296 oldRAbund.getSAbundVector().print(sabundFile);
298 oldList.setLabel(label);
299 oldList.print(listFile);
301 catch(exception& e) {
302 errorOut(e, "HClusterCommand", "printData");
308 //**********************************************************************************************************************