]> git.donarmstrong.com Git - mothur.git/blob - hclustercommand.cpp
fixed some bugs and added mgcluster command
[mothur.git] / hclustercommand.cpp
1 /*
2  *  hclustercommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 10/13/09.
6  *  Copyright 2009 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "hclustercommand.h"
11
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){
15         try{
16                 globaldata = GlobalData::getInstance();
17                 abort = false;
18                 
19                 //allow user to run help
20                 if(option == "help") { help(); abort = true; }
21                 
22                 else {
23                         //valid paramters for this command
24                         string Array[] =  {"cutoff","precision","method","phylip","column","name","sorted","showabund","timing"};
25                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26                         
27                         OptionParser parser(option);
28                         map<string,string> parameters = parser.getParameters();
29                         
30                         ValidParameters validParameter;
31                 
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) {
35                                         abort = true;
36                                 }
37                         }
38                         
39                         globaldata->newRead();
40                         
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";      }
46                         
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";       }
51                         
52                         namefile = validParameter.validFile(parameters, "name", true);
53                         if (namefile == "not open") { abort = true; }   
54                         else if (namefile == "not found") { namefile = ""; }
55                         
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; }
58                 
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; }
61                         }
62                         
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
66                         string temp;
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); 
72                         
73                         temp = validParameter.validFile(parameters, "cutoff", false);
74                         if (temp == "not found") { temp = "10"; }
75                         convert(temp, cutoff); 
76                         cutoff += (5 / (precision * 10.0));
77                         
78                         method = validParameter.validFile(parameters, "method", false);
79                         if (method == "not found") { method = "nearest"; }
80                         
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; }
83
84                         showabund = validParameter.validFile(parameters, "showabund", false);
85                         if (showabund == "not found") { showabund = "T"; }
86                         
87                         sort = validParameter.validFile(parameters, "sorted", false);
88                         if (sort == "not found") { sort = "F"; }
89                         sorted = isTrue(sort);
90
91                         timing = validParameter.validFile(parameters, "timing", false);
92                         if (timing == "not found") { timing = "F"; }
93                         
94                                 
95                         if (abort == false) {
96                                                                                         
97                                 fileroot = getRootName(distfile);
98                                 
99                                 tag = "fn";  //until we figure out average and nearest methods
100                         
101                                 openOutputFile(fileroot+ tag + ".sabund",       sabundFile);
102                                 openOutputFile(fileroot+ tag + ".rabund",       rabundFile);
103                                 openOutputFile(fileroot+ tag + ".list",         listFile);
104                         }
105                 }
106         }
107         catch(exception& e) {
108                 errorOut(e, "HClusterCommand", "HClusterCommand");
109                 exit(1);
110         }
111 }
112
113 //**********************************************************************************************************************
114
115 void HClusterCommand::help(){
116         try {
117                 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");
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");    
123         }
124         catch(exception& e) {
125                 errorOut(e, "HClusterCommand", "help");
126                 exit(1);
127         }
128 }
129
130 //**********************************************************************************************************************
131
132 HClusterCommand::~HClusterCommand(){}
133
134 //**********************************************************************************************************************
135
136 int HClusterCommand::execute(){
137         try {
138         
139                 if (abort == true) {    return 0;       }
140                 
141                 if(namefile != ""){     
142                         globaldata->nameMap = new NameAssignment(namefile);
143                         globaldata->nameMap->readMap();
144                 }else{
145                         globaldata->nameMap = NULL;
146                 }
147                 
148                 time_t estart = time(NULL);
149                 
150                 if (!sorted) {
151                         read = new ReadCluster(distfile, cutoff);       
152                         read->setFormat(format);
153                         read->read(globaldata->nameMap);
154                         distfile = read->getOutputFile();
155                 
156                         list = read->getListVector();
157                         delete read;
158                 }else {
159                         list = new ListVector(globaldata->nameMap->getListVector());
160                 }
161         
162                 mothurOut("It took " + toString(time(NULL) - estart) + " seconds to sort. "); mothurOutEndLine();
163                 estart = time(NULL);
164         
165                 //list vector made by read contains all sequence names
166                 if(list != NULL){
167                         rabund = new RAbundVector(list->getRAbundVector());
168                 }else{
169                         mothurOut("Error: no list vector!"); mothurOutEndLine(); return 0;
170                 }
171                 
172                 float previousDist = 0.00000;
173                 float rndPreviousDist = 0.00000;
174                 oldRAbund = *rabund;
175                 oldList = *list;
176                 
177                 print_start = true;
178                 start = time(NULL);
179         
180                 ifstream in;
181                 openInputFile(distfile, in);
182                 string firstName, secondName;
183                 float distance;
184                 
185                 cluster = new HCluster(rabund, list);
186                 vector<seqDist> seqs; seqs.resize(1); // to start loop
187                 exitedBreak = false;  //lets you know if there is a distance stored in next
188         
189                 while (seqs.size() != 0){
190                 
191                         seqs = getSeqs(in);
192                         random_shuffle(seqs.begin(), seqs.end());
193                         
194                         if (seqs.size() == 0) { break; } //there are no more distances
195                 
196                         for (int i = 0; i < seqs.size(); i++) {  //-1 means skip me
197
198                                 if (print_start && isTrue(timing)) {
199                                         mothurOut("Clustering (" + tag + ") dist " + toString(distance) + "/" 
200                                                           + toString(roundDist(distance, precision)) 
201                                                           + "\t(precision: " + toString(precision) + ")");
202                                         cout.flush();
203                                         print_start = false;
204                                 }
205                                 
206         //cout << "before cluster update" << endl;
207                                 if (seqs[i].seq1 != seqs[i].seq2) {
208                                         cluster->update(seqs[i].seq1, seqs[i].seq2, seqs[i].dist);
209                                         
210                                         float rndDist = roundDist(seqs[i].dist, precision);
211                 //cout << "after cluster update clusterSomething = " << clusteredSomething << " rndDist = " << rndDist << " rndPreviousDist = " << rndPreviousDist << endl;                     
212                                         
213                                         
214                                         if((previousDist <= 0.0000) && (seqs[i].dist != previousDist)){
215                                                 printData("unique");
216                                         }
217                                         else if((rndDist != rndPreviousDist)){
218                                                 printData(toString(rndPreviousDist,  length-1));
219                                         }
220                                         
221                                         previousDist = seqs[i].dist;
222                                         rndPreviousDist = rndDist;
223                                         oldRAbund = *rabund;
224                                         oldList = *list;
225                                 }
226                         }
227                 }
228                 
229                 in.close();
230
231                 if (print_start && isTrue(timing)) {
232                         //mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist) 
233                                          //+ "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
234                         cout.flush();
235                         print_start = false;
236                 }
237         
238                 if(previousDist <= 0.0000){
239                         printData("unique");
240                 }
241                 else if(rndPreviousDist<cutoff){
242                         printData(toString(rndPreviousDist, length-1));
243                 }
244                 
245                 //delete globaldata's copy of the sparsematrix and listvector to free up memory
246                 delete globaldata->gListVector;  globaldata->gListVector = NULL;
247                 
248                 //saves .list file so you can do the collect, rarefaction and summary commands without doing a read.list
249                 if (globaldata->getFormat() == "phylip") { globaldata->setPhylipFile(""); }
250                 else if (globaldata->getFormat() == "column") { globaldata->setColumnFile(""); }
251                 
252                 globaldata->setListFile(fileroot+ tag + ".list");
253                 globaldata->setNameFile("");
254                 globaldata->setFormat("list");
255                 
256                 sabundFile.close();
257                 rabundFile.close();
258                 listFile.close();
259                 
260                 delete cluster;
261                 //if (isTrue(timing)) {
262                         mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster. "); mothurOutEndLine();
263                 //}
264                 return 0;
265         }
266         catch(exception& e) {
267                 errorOut(e, "HClusterCommand", "execute");
268                 exit(1);
269         }
270 }
271
272 //**********************************************************************************************************************
273
274 void HClusterCommand::printData(string label){
275         try {
276                 if (isTrue(timing)) {
277                         mothurOut("\tTime: " + toString(time(NULL) - start) + "\tsecs for " + toString(oldRAbund.getNumBins()) 
278                      + "\tclusters. Updates: " + toString(loops)); mothurOutEndLine();
279                 }
280                 print_start = true;
281                 loops = 0;
282                 start = time(NULL);
283
284                 oldRAbund.setLabel(label);
285                 if (isTrue(showabund)) {
286                         oldRAbund.getSAbundVector().print(cout);
287                 }
288                 oldRAbund.print(rabundFile);
289                 oldRAbund.getSAbundVector().print(sabundFile);
290         
291                 oldList.setLabel(label);
292                 oldList.print(listFile);
293         }
294         catch(exception& e) {
295                 errorOut(e, "HClusterCommand", "printData");
296                 exit(1);
297         }
298
299
300 }
301 //**********************************************************************************************************************
302 vector<seqDist> HClusterCommand::getSeqs(ifstream& filehandle){
303         try {
304                 string firstName, secondName;
305                 float distance, prevDistance;
306                 vector<seqDist> sameSeqs;
307                 prevDistance = -1;
308                 
309                 //if you are not at the beginning of the file
310                 if (exitedBreak) { 
311                         sameSeqs.push_back(next);
312                         prevDistance = next.dist;
313                         exitedBreak = false;
314                 }
315         
316                 //get entry
317                 while (filehandle) {
318                         
319                         filehandle >> firstName >> secondName >> distance;  
320 //cout << firstName << '\t' << secondName << '\t' << distance << endl;
321                         gobble(filehandle);
322                         
323                         //save first one
324                         if (prevDistance == -1) { prevDistance = distance; }
325         //cout << prevDistance << endl; 
326 //if (globaldata->nameMap == NULL) { cout << "null" << endl; }
327                         map<string,int>::iterator itA = globaldata->nameMap->find(firstName);
328                         map<string,int>::iterator itB = globaldata->nameMap->find(secondName);
329                         
330                         if(itA == globaldata->nameMap->end()){
331                                 cerr << "AAError: Sequence '" << firstName << "' was not found in the names file, please correct\n"; exit(1);
332                         }
333                         if(itB == globaldata->nameMap->end()){
334                                 cerr << "ABError: Sequence '" << secondName << "' was not found in the names file, please correct\n"; exit(1);
335                         }
336         //cout << "here" << endl;               
337                         //using cutoff
338                         if (distance > cutoff) { break; }
339                         
340                         if (distance != -1) { //-1 means skip me
341                                 
342                                 //are the distances the same
343                                 if (distance == prevDistance) { //save in vector
344                                         seqDist temp;
345                                         temp.seq1 = itA->second;
346                                         temp.seq2 = itB->second;
347                                         temp.dist = distance;
348                                         sameSeqs.push_back(temp);
349                                         exitedBreak = false;
350                                         //what about precision??
351                                         
352                                 }else{ 
353                                         next.seq1 = itA->second;
354                                         next.seq2 = itB->second;
355                                         next.dist = distance;
356                                         exitedBreak = true;
357                                         break;
358                                 }
359                                 
360                         }
361                 }
362
363                 return sameSeqs;
364         }
365         catch(exception& e) {
366                 errorOut(e, "HClusterCommand", "getSeqs");
367                 exit(1);
368         }
369
370
371 }
372
373 //**********************************************************************************************************************
374