]> git.donarmstrong.com Git - mothur.git/blob - clustersplitcommand.cpp
finished cluster.split adding classify method.
[mothur.git] / clustersplitcommand.cpp
1 /*
2  *  clustersplitcommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 5/19/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "clustersplitcommand.h"
11 #include "readcluster.h"
12 #include "splitmatrix.h"
13 #include "readphylip.h"
14 #include "readcolumn.h"
15 #include "readmatrix.hpp"
16 #include "inputdata.h"
17
18 //**********************************************************************************************************************
19 //This function checks to make sure the cluster command has no errors and then clusters based on the method chosen.
20 ClusterSplitCommand::ClusterSplitCommand(string option)  {
21         try{
22                 globaldata = GlobalData::getInstance();
23                 abort = false;
24                 
25                 //allow user to run help
26                 if(option == "help") { help(); abort = true; }
27                 
28                 else {
29                         //valid paramters for this command
30                         string Array[] =  {"phylip","column","name","cutoff","precision","method","splitmethod","taxonomy","taxlevel","showabund","timing","hard","processors","outputdir","inputdir"};
31                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
32                         
33                         OptionParser parser(option);
34                         map<string,string> parameters = parser.getParameters();
35                         
36                         ValidParameters validParameter;
37                 
38                         //check to make sure all parameters are valid for command
39                         map<string,string>::iterator it;
40                         for (it = parameters.begin(); it != parameters.end(); it++) { 
41                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
42                                         abort = true;
43                                 }
44                         }
45                         
46                         globaldata->newRead();
47                         
48                         //if the user changes the output directory command factory will send this info to us in the output parameter 
49                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
50                         
51                                 //if the user changes the input directory command factory will send this info to us in the output parameter 
52                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
53                         if (inputDir == "not found"){   inputDir = "";          }
54                         else {
55                                 string path;
56                                 it = parameters.find("phylip");
57                                 //user has given a template file
58                                 if(it != parameters.end()){ 
59                                         path = hasPath(it->second);
60                                         //if the user has not given a path then, add inputdir. else leave path alone.
61                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
62                                 }
63                                 
64                                 it = parameters.find("column");
65                                 //user has given a template file
66                                 if(it != parameters.end()){ 
67                                         path = hasPath(it->second);
68                                         //if the user has not given a path then, add inputdir. else leave path alone.
69                                         if (path == "") {       parameters["column"] = inputDir + it->second;           }
70                                 }
71                                 
72                                 it = parameters.find("name");
73                                 //user has given a template file
74                                 if(it != parameters.end()){ 
75                                         path = hasPath(it->second);
76                                         //if the user has not given a path then, add inputdir. else leave path alone.
77                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
78                                 }
79                                 
80                                 it = parameters.find("taxonomy");
81                                 //user has given a template file
82                                 if(it != parameters.end()){ 
83                                         path = hasPath(it->second);
84                                         //if the user has not given a path then, add inputdir. else leave path alone.
85                                         if (path == "") {       parameters["taxonomy"] = inputDir + it->second;         }
86                                 }
87                         }
88                         
89                         //check for required parameters
90                         phylipfile = validParameter.validFile(parameters, "phylip", true);
91                         if (phylipfile == "not open") { abort = true; }
92                         else if (phylipfile == "not found") { phylipfile = ""; }        
93                         else {  distfile = phylipfile;  format = "phylip";      }
94                         
95                         columnfile = validParameter.validFile(parameters, "column", true);
96                         if (columnfile == "not open") { abort = true; } 
97                         else if (columnfile == "not found") { columnfile = ""; }
98                         else {  distfile = columnfile; format = "column";       }
99                         
100                         namefile = validParameter.validFile(parameters, "name", true);
101                         if (namefile == "not open") { abort = true; }   
102                         else if (namefile == "not found") { namefile = ""; }
103                         
104                         taxFile = validParameter.validFile(parameters, "taxonomy", true);
105                         if (taxFile == "not open") { abort = true; }    
106                         else if (taxFile == "not found") { taxFile = ""; }
107                         
108                         if ((phylipfile == "") && (columnfile == "")) { m->mothurOut("When executing a cluster.split command you must enter a phylip or a column."); m->mothurOutEndLine(); abort = true; }
109                         else if ((phylipfile != "") && (columnfile != "")) { m->mothurOut("When executing a cluster.split command you must enter ONLY ONE of the following: phylip or column."); m->mothurOutEndLine(); abort = true; }
110                 
111                         if (columnfile != "") {
112                                 if (namefile == "") { m->mothurOut("You need to provide a namefile if you are going to use the column format."); m->mothurOutEndLine(); abort = true; }
113                         }
114                                         
115                         //check for optional parameter and set defaults
116                         // ...at some point should added some additional type checking...
117                         //get user cutoff and precision or use defaults
118                         string temp;
119                         temp = validParameter.validFile(parameters, "precision", false);
120                         if (temp == "not found") { temp = "100"; }
121                         //saves precision legnth for formatting below
122                         length = temp.length();
123                         convert(temp, precision); 
124                         
125                         temp = validParameter.validFile(parameters, "hard", false);                     if (temp == "not found") { temp = "F"; }
126                         hard = isTrue(temp);
127                         
128                         temp = validParameter.validFile(parameters, "processors", false);       if (temp == "not found"){       temp = "1";                             }
129                         convert(temp, processors); 
130                         
131                         splitmethod = validParameter.validFile(parameters, "splitmethod", false);               if (splitmethod == "not found") { splitmethod = "distance"; }
132                         
133                         temp = validParameter.validFile(parameters, "cutoff", false);           if (temp == "not found")  { temp = "10"; }
134                         convert(temp, cutoff); 
135                         cutoff += (5 / (precision * 10.0));  
136                         
137                         temp = validParameter.validFile(parameters, "taxlevel", false);         if (temp == "not found")  { temp = "1"; }
138                         convert(temp, taxLevelCutoff); 
139                         
140                         method = validParameter.validFile(parameters, "method", false);         if (method == "not found") { method = "furthest"; }
141                         
142                         if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
143                         else { m->mothurOut("Not a valid clustering method.  Valid clustering algorithms are furthest, nearest or average."); m->mothurOutEndLine(); abort = true; }
144                         
145                         if ((splitmethod == "distance") || (splitmethod == "classify")) { }
146                         else { m->mothurOut("Not a valid splitting method.  Valid splitting algorithms are distance or classify."); m->mothurOutEndLine(); abort = true; }
147                         
148                         if ((splitmethod == "classify") && (taxFile == "")) {  m->mothurOut("You need to provide a taxonomy file if you are going to use the classify splitmethod."); m->mothurOutEndLine(); abort = true;  }
149
150                         showabund = validParameter.validFile(parameters, "showabund", false);
151                         if (showabund == "not found") { showabund = "T"; }
152
153                         timing = validParameter.validFile(parameters, "timing", false);
154                         if (timing == "not found") { timing = "F"; }
155                         
156                 }
157         }
158         catch(exception& e) {
159                 m->errorOut(e, "ClusterSplitCommand", "ClusterSplitCommand");
160                 exit(1);
161         }
162 }
163
164 //**********************************************************************************************************************
165
166 void ClusterSplitCommand::help(){
167         try {
168                 m->mothurOut("The cluster.split command parameter options are phylip, column, name, cutoff, precision, method, splitmethod, taxonomy, taxlevel, showabund, timing, hard, processors. Phylip or column and name are required.\n");
169                 m->mothurOut("The phylip and column parameter allow you to enter your distance file. \n");
170                 m->mothurOut("The name parameter allows you to enter your name file and is required if your distance file is in column format. \n");
171                 m->mothurOut("The cutoff parameter allow you to set the distance you want to cluster to, default is 10.0. \n");
172                 m->mothurOut("The precision parameter allows you specify the precision of the precision of the distances outputted, default=100, meaning 2 decimal places. \n");
173                 m->mothurOut("The method allows you to specify what clustering algorythm you want to use, default=furthest, option furthest, nearest, or average. \n");
174                 m->mothurOut("The splitmethod parameter allows you to specify how you want to split your distance file before you cluster, default=distance, options distance or classify. \n");
175                 m->mothurOut("The taxonomy parameter allows you to enter the taxonomy file for your sequences, this is only valid if you are using splitmethod=classify. Be sure your taxonomy file does not include the probability scores. \n");
176                 m->mothurOut("The taxlevel parameter allows you to specify the taxonomy level you want to use to split the distance file, default=1. \n");
177                 m->mothurOut("The cluster.split command should be in the following format: \n");
178                 m->mothurOut("cluster.split(column=youDistanceFile, name=yourNameFile, method=yourMethod, cutoff=yourCutoff, precision=yourPrecision, splitmethod=yourSplitmethod, taxonomy=yourTaxonomyfile, taxlevel=yourtaxlevel) \n");
179                 m->mothurOut("Example: cluster.split(column=abrecovery.dist, name=abrecovery.names, method=furthest, cutoff=0.10, precision=1000, splitmethod=classify, taxonomy=abrecovery.silva.slv.taxonomy, taxlevel=5) \n");       
180
181         }
182         catch(exception& e) {
183                 m->errorOut(e, "ClusterSplitCommand", "help");
184                 exit(1);
185         }
186 }
187
188 //**********************************************************************************************************************
189
190 ClusterSplitCommand::~ClusterSplitCommand(){}
191
192 //**********************************************************************************************************************
193
194 int ClusterSplitCommand::execute(){
195         try {
196         
197                 if (abort == true) {    return 0;       }
198                 
199                 //****************** file prep work ******************************//
200                 
201                 //if user gave a phylip file convert to column file
202                 if (format == "phylip") {
203         
204                         ReadCluster* convert = new ReadCluster(distfile, cutoff, outputDir, false);
205                         
206                         NameAssignment* nameMap = NULL;
207                         convert->setFormat("phylip");
208                         convert->read(nameMap);
209                         
210                         if (m->control_pressed) {  delete convert;  return 0;  }
211                         
212                         distfile = convert->getOutputFile();
213                 
214                         //if no names file given with phylip file, create it
215                         ListVector* listToMakeNameFile =  convert->getListVector();
216                         if (namefile == "") {  //you need to make a namefile for split matrix
217                                 ofstream out;
218                                 namefile = phylipfile + ".names";
219                                 openOutputFile(namefile, out);
220                                 for (int i = 0; i < listToMakeNameFile->getNumBins(); i++) {
221                                         string bin = listToMakeNameFile->get(i);
222                                         out << bin << '\t' << bin << endl;
223                                 }
224                                 out.close();
225                         }
226                         delete listToMakeNameFile;
227                         delete convert;
228                 }
229                 if (m->control_pressed) { return 0; }
230                 
231                 time_t estart = time(NULL);
232                 
233                 //split matrix into non-overlapping groups
234                 SplitMatrix* split;
235                 if (splitmethod == "distance")  {       split = new SplitMatrix(distfile, namefile, taxFile, cutoff, splitmethod);                      }
236                 else                                                    {       split = new SplitMatrix(distfile, namefile, taxFile, taxLevelCutoff, splitmethod);  }
237                 
238                 split->split();
239                 
240                 if (m->control_pressed) { delete split; return 0; }
241                 
242                 string singletonName = split->getSingletonNames();
243                 vector< map<string, string> > distName = split->getDistanceFiles();  //returns map of distance files -> namefile sorted by distance file size
244                 delete split;
245                 
246                 if (m->control_pressed) { return 0; }
247                 
248                 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to split the distance file."); m->mothurOutEndLine();
249                 estart = time(NULL);
250                 
251                 //****************** break up files between processes and cluster each file set ******************************//
252                 vector<string> listFileNames;
253                 set<string> labels;
254                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
255                                 if(processors == 1){
256                                         listFileNames = cluster(distName, labels); //clusters individual files and returns names of list files
257                                 }else{
258                                         vector < vector < map<string, string> > > dividedNames; //distNames[1] = vector of filenames for process 1...
259                                         dividedNames.resize(processors);
260                                         
261                                         //for each file group figure out which process will complete it
262                                         //want to divide the load intelligently so the big files are spread between processes
263                                         int count = 1;
264                                         for (int i = 0; i < distName.size(); i++) { 
265                                                 int processToAssign = (i+1) % processors; 
266                                                 if (processToAssign == 0) { processToAssign = processors; }
267                                                 
268                                                 dividedNames[(processToAssign-1)].push_back(distName[i]);
269                                         }
270                                         
271                                         //not lets reverse the order of ever other process, so we balance big files running with little ones
272                                         for (int i = 0; i < processors; i++) {
273                                                 int remainder = ((i+1) % processors);
274                                                 if (remainder) {  reverse(dividedNames[i].begin(), dividedNames[i].end());  }
275                                         }
276                                         
277                                         createProcesses(dividedNames);
278                                                         
279                                         if (m->control_pressed) { return 0; }
280
281                                         //get list of list file names from each process
282                                         for(int i=0;i<processors;i++){
283                                                 string filename = toString(processIDS[i]) + ".temp";
284                                                 ifstream in;
285                                                 openInputFile(filename, in);
286                                                 
287                                                 while(!in.eof()) {
288                                                         string tempName;
289                                                         in >> tempName; gobble(in);
290                                                         listFileNames.push_back(tempName);
291                                                 }
292                                                 in.close();
293                                                 remove((toString(processIDS[i]) + ".temp").c_str());
294                                                 
295                                                 //get labels
296                                                 filename = toString(processIDS[i]) + ".temp.labels";
297                                                 ifstream in2;
298                                                 openInputFile(filename, in2);
299                                                 
300                                                 while(!in2.eof()) {
301                                                         string tempName;
302                                                         in2 >> tempName; gobble(in);
303                                                         if (labels.count(tempName) == 0) { labels.insert(tempName); }
304                                                 }
305                                                 in2.close();
306                                                 remove((toString(processIDS[i]) + ".temp.labels").c_str());
307                                         }
308                                 }
309                 #else
310                                 listFileNames = cluster(distName, labels); //clusters individual files and returns names of list files
311                 #endif
312                 
313                 if (m->control_pressed) { for (int i = 0; i < listFileNames.size(); i++) { remove(listFileNames[i].c_str()); } return 0; }
314                 
315                 //****************** merge list file and create rabund and sabund files ******************************//
316                                 
317                 mergeLists(listFileNames, singletonName, labels);
318
319                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
320                 
321                 m->mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster"); m->mothurOutEndLine();
322                 
323                 m->mothurOutEndLine();
324                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
325                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
326                 m->mothurOutEndLine();
327
328                 return 0;
329         }
330         catch(exception& e) {
331                 m->errorOut(e, "ClusterSplitCommand", "execute");
332                 exit(1);
333         }
334 }
335 //**********************************************************************************************************************
336 int ClusterSplitCommand::mergeLists(vector<string> listNames, string singleton, set<string> userLabels){
337         try {
338                 if (outputDir == "") { outputDir += hasPath(distfile); }
339                 fileroot = outputDir + getRootName(getSimpleName(distfile));
340                 
341                 openOutputFile(fileroot+ tag + ".sabund",       outSabund);
342                 openOutputFile(fileroot+ tag + ".rabund",       outRabund);
343                 openOutputFile(fileroot+ tag + ".list",         outList);
344                                 
345                 outputNames.push_back(fileroot+ tag + ".sabund");
346                 outputNames.push_back(fileroot+ tag + ".rabund");
347                 outputNames.push_back(fileroot+ tag + ".list");
348                 
349                 //read in singletons
350                 ListVector* listSingle = NULL;
351                 if (singleton != "none") {
352                         ifstream in;
353                         openInputFile(singleton, in);
354                                 
355                         string firstCol, secondCol;
356                         listSingle = new ListVector();
357                         while (!in.eof()) {
358                                 in >> firstCol >> secondCol; gobble(in);
359                                 listSingle->push_back(secondCol);
360                         }
361                         in.close();
362                 }
363                 
364                 vector<float> orderFloat;
365         
366                 //go through users set and make them floats so we can sort them 
367                 for(set<string>::iterator it = userLabels.begin(); it != userLabels.end(); ++it) {
368                         float temp;
369
370                         if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
371                                 convert(*it, temp);
372                                 orderFloat.push_back(temp);
373                         }else if (*it == "unique") { orderFloat.push_back(-1.0); }
374                         else {
375                                 userLabels.erase(*it); 
376                                 it--;
377                         }
378                 }
379         
380                 //sort order
381                 sort(orderFloat.begin(), orderFloat.end());
382
383                 vector<InputData*> inputs;
384                 vector<string> lastLabels;
385                 for (int i = 0; i < listNames.size(); i++) {
386                         InputData* input = new InputData(listNames[i], "list");
387                         inputs.push_back(input);
388                         
389                         ifstream in;
390                         openInputFile(listNames[i], in);
391                         ListVector tempList(in);
392                         lastLabels.push_back(tempList.getLabel());
393                         in.close();
394                 }
395         
396                 ListVector* merged = NULL;
397                                 
398                 //for each label needed
399                 for(int l = 0; l < orderFloat.size(); l++){
400                         
401                         string thisLabel;
402                         if (orderFloat[l] == -1) { thisLabel = "unique"; }
403                         else { thisLabel = toString(orderFloat[l],  length-1);  } 
404         
405                         //get the list info from each file
406                         for (int k = 0; k < listNames.size(); k++) {
407         
408                                 if (m->control_pressed) {  
409                                         if (listSingle != NULL) { delete listSingle; remove(singleton.c_str());  }
410                                         for (int i = 0; i < listNames.size(); i++) {  delete inputs[i];  remove(listNames[i].c_str());  }
411                                         delete merged; merged = NULL;
412                                         return 0;
413                                 }
414                                 
415                                 ListVector* list = inputs[k]->getListVector();
416                                 
417                                 //this file has reached the end
418                                 if (list == NULL) { list = inputs[k]->getListVector(lastLabels[k], true); }     
419                                                 
420                                 float labelFloat;
421                                 if (list->getLabel() == "unique") {  labelFloat = -1.0;  }
422                                 else { convert(list->getLabel(), labelFloat); }
423
424                                 //check for missing labels
425                                 if (labelFloat > orderFloat[l]) { //you are missing the label, get the next smallest one
426                                         //if its bigger get last label, otherwise keep it
427                                         delete list;
428                                         list = inputs[k]->getListVector(lastLabels[k], true); //get last list vector to use, you actually want to move back in the file
429                                 }
430                                 lastLabels[k] = list->getLabel();
431
432                                 //is this the first file
433                                 if (merged == NULL) {  merged = new ListVector();  merged->setLabel(thisLabel); }
434                                 
435                                 for (int j = 0; j < list->getNumBins(); j++) {
436                                         merged->push_back(list->get(j));
437                                 }
438                                 
439                                 delete list;
440                         }
441                         
442                         //add in singletons
443                         if (listSingle != NULL) {
444                                 for (int j = 0; j < listSingle->getNumBins(); j++) {
445                                         merged->push_back(listSingle->get(j));
446                                 }
447                         }
448                         
449                         //print to files
450                         printData(merged);
451                         
452                         delete merged; merged = NULL;
453                 }
454                 
455                 if (listSingle != NULL) { delete listSingle; remove(singleton.c_str());  }
456                 
457                 for (int i = 0; i < listNames.size(); i++) {  delete inputs[i];  remove(listNames[i].c_str());  }
458                 
459                 return 0;
460         }
461         catch(exception& e) {
462                 m->errorOut(e, "ClusterSplitCommand", "mergeLists");
463                 exit(1);
464         }
465 }
466 //**********************************************************************************************************************
467
468 void ClusterSplitCommand::printData(ListVector* oldList){
469         try {
470                 string label = oldList->getLabel();
471                 RAbundVector oldRAbund = oldList->getRAbundVector();
472                 
473                 oldRAbund.setLabel(label);
474                 if (isTrue(showabund)) {
475                         oldRAbund.getSAbundVector().print(cout);
476                 }
477                 oldRAbund.print(outRabund);
478                 oldRAbund.getSAbundVector().print(outSabund);
479         
480                 oldList->print(outList);
481         }
482         catch(exception& e) {
483                 m->errorOut(e, "ClusterSplitCommand", "printData");
484                 exit(1);
485         }
486 }
487 //**********************************************************************************************************************
488 int ClusterSplitCommand::createProcesses(vector < vector < map<string, string> > > dividedNames){
489         try {
490         
491         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
492                 int process = 0;
493                 int exitCommand = 1;
494                 processIDS.clear();
495                 
496                 //loop through and create all the processes you want
497                 while (process != processors) {
498                         int pid = fork();
499                         
500                         if (pid > 0) {
501                                 processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
502                                 process++;
503                         }else if (pid == 0){
504                                 set<string> labels;
505                                 vector<string> listFileNames = cluster(dividedNames[process], labels);
506                                 
507                                 //write out names to file
508                                 string filename = toString(getpid()) + ".temp";
509                                 ofstream out;
510                                 openOutputFile(filename, out);
511                                 for (int j = 0; j < listFileNames.size(); j++) { out << listFileNames[j] << endl;  }
512                                 out.close();
513                                 
514                                 //print out labels
515                                 ofstream outLabels;
516                                 filename = toString(getpid()) + ".temp.labels";
517                                 openOutputFile(filename, outLabels);
518                 
519                                 for (set<string>::iterator it = labels.begin(); it != labels.end(); it++) {
520                                         outLabels << (*it) << endl;
521                                 }
522                                 outLabels.close();
523
524                                 exit(0);
525                         }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
526                 }
527                 
528                 //force parent to wait until all the processes are done
529                 for (int i=0;i<processors;i++) { 
530                         int temp = processIDS[i];
531                         wait(&temp);
532                 }
533                 
534                 return exitCommand;
535         #endif          
536         
537         }
538         catch(exception& e) {
539                 m->errorOut(e, "ClusterSplitCommand", "createProcesses");
540                 exit(1);
541         }
542 }
543 //**********************************************************************************************************************
544
545 vector<string> ClusterSplitCommand::cluster(vector< map<string, string> > distNames, set<string>& labels){
546         try {
547                 Cluster* cluster;
548                 SparseMatrix* matrix;
549                 ListVector* list;
550                 ListVector oldList;
551                 RAbundVector* rabund;
552                 
553                 vector<string> listFileNames;
554                 
555                 //cluster each distance file
556                 for (int i = 0; i < distNames.size(); i++) {
557                         
558                         string thisNamefile = distNames[i].begin()->second;
559                         string thisDistFile = distNames[i].begin()->first;
560                         
561                         //read in distance file
562                         globaldata->setNameFile(thisNamefile);
563                         globaldata->setColumnFile(thisDistFile); globaldata->setFormat("column");
564                         
565                         ReadMatrix* read = new ReadColumnMatrix(thisDistFile);  
566                         read->setCutoff(cutoff);
567
568                         NameAssignment* nameMap = new NameAssignment(thisNamefile);
569                         nameMap->readMap();
570                         read->read(nameMap);
571                         
572                         if (m->control_pressed) {  delete read; delete nameMap; return listFileNames; }
573                         
574                         list = read->getListVector();
575                         oldList = *list;
576                         matrix = read->getMatrix();
577                         
578                         delete read; 
579                         delete nameMap; 
580                         
581                         m->mothurOutEndLine(); m->mothurOut("Clustering " + thisDistFile); m->mothurOutEndLine();
582                 
583                         rabund = new RAbundVector(list->getRAbundVector());
584                         
585                         //create cluster
586                         if (method == "furthest")       {       cluster = new CompleteLinkage(rabund, list, matrix, cutoff, method); }
587                         else if(method == "nearest"){   cluster = new SingleLinkage(rabund, list, matrix, cutoff, method); }
588                         else if(method == "average"){   cluster = new AverageLinkage(rabund, list, matrix, cutoff, method);     }
589                         tag = cluster->getTag();
590                 
591                         if (outputDir == "") { outputDir += hasPath(thisDistFile); }
592                         fileroot = outputDir + getRootName(getSimpleName(thisDistFile));
593                         
594                         ofstream listFile;
595                         openOutputFile(fileroot+ tag + ".list", listFile);
596                 
597                         listFileNames.push_back(fileroot+ tag + ".list");
598                 
599                         time_t estart = time(NULL);
600                         
601                         float previousDist = 0.00000;
602                         float rndPreviousDist = 0.00000;
603                         
604                         oldList = *list;
605
606                         print_start = true;
607                         start = time(NULL);
608                         double saveCutoff = cutoff;
609                 
610                         while (matrix->getSmallDist() < cutoff && matrix->getNNodes() > 0){
611                 
612                                 if (m->control_pressed) { //clean up
613                                         delete matrix; delete list;     delete cluster; delete rabund;
614                                         listFile.close();
615                                         for (int i = 0; i < listFileNames.size(); i++) {        remove(listFileNames[i].c_str());       }
616                                         listFileNames.clear(); return listFileNames;
617                                 }
618                 
619                                 cluster->update(cutoff);
620         
621                                 float dist = matrix->getSmallDist();
622                                 float rndDist;
623                                 if (hard) {
624                                         rndDist = ceilDist(dist, precision); 
625                                 }else{
626                                         rndDist = roundDist(dist, precision); 
627                                 }
628
629                                 if(previousDist <= 0.0000 && dist != previousDist){
630                                         oldList.setLabel("unique");
631                                         oldList.print(listFile);
632                                         if (labels.count("unique") == 0) {  labels.insert("unique");  }
633                                 }
634                                 else if(rndDist != rndPreviousDist){
635                                         oldList.setLabel(toString(rndPreviousDist,  length-1));
636                                         oldList.print(listFile);
637                                         if (labels.count(toString(rndPreviousDist,  length-1)) == 0) { labels.insert(toString(rndPreviousDist,  length-1)); }
638                                 }
639                 
640                                 previousDist = dist;
641                                 rndPreviousDist = rndDist;
642                                 oldList = *list;
643                         }
644
645                 
646                         if(previousDist <= 0.0000){
647                                 oldList.setLabel("unique");
648                                 oldList.print(listFile);
649                                 if (labels.count("unique") == 0) { labels.insert("unique"); }
650                         }
651                         else if(rndPreviousDist<cutoff){
652                                 oldList.setLabel(toString(rndPreviousDist,  length-1));
653                                 oldList.print(listFile);
654                                 if (labels.count(toString(rndPreviousDist,  length-1)) == 0) { labels.insert(toString(rndPreviousDist,  length-1)); }
655                         }
656         
657                         delete matrix; delete list;     delete cluster; delete rabund; 
658                         listFile.close();
659                         
660                         if (m->control_pressed) { //clean up
661                                 for (int i = 0; i < listFileNames.size(); i++) {        remove(listFileNames[i].c_str());       }
662                                 listFileNames.clear(); return listFileNames;
663                         }
664                         
665                         remove(thisDistFile.c_str());
666                         remove(thisNamefile.c_str());
667                 }
668                 
669                                         
670                 return listFileNames;
671         
672         }
673         catch(exception& e) {
674                 m->errorOut(e, "ClusterSplitCommand", "cluster");
675                 exit(1);
676         }
677
678
679 }
680
681 //**********************************************************************************************************************