]> git.donarmstrong.com Git - mothur.git/blob - clusterclassic.cpp
Merge remote-tracking branch 'mothur/master'
[mothur.git] / clusterclassic.cpp
1 /*
2  *  clusterclassic.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 10/29/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "clusterclassic.h"
11 #include "progress.hpp"
12
13 /***********************************************************************/
14 ClusterClassic::ClusterClassic(float c, string f, bool s) : method(f), smallDist(1e6), nseqs(0), sim(s) {
15         try {
16                 mapWanted = false;  //set to true by mgcluster to speed up overlap merge
17         
18                 //save so you can modify as it changes in average neighbor
19                 cutoff = c;
20                 aboveCutoff = cutoff + 10000.0;
21                 m = MothurOut::getInstance();
22         }
23         catch(exception& e) {
24                 m->errorOut(e, "ClusterClassic", "ClusterClassic");
25                 exit(1);
26         }
27 }
28 /***********************************************************************/
29 int ClusterClassic::readPhylipFile(string filename, NameAssignment* nameMap) {
30         try {
31                 double distance;
32                 int square;
33                 string name;
34                 vector<string> matrixNames;
35                 
36                 ifstream fileHandle;
37                 m->openInputFile(filename, fileHandle);
38                 
39                 fileHandle >> nseqs >> name;
40
41                 matrixNames.push_back(name);
42
43                 if(nameMap == NULL){
44                                 list = new ListVector(nseqs);
45                                 list->set(0, name);
46                 }
47                 else{
48                                 list = new ListVector(nameMap->getListVector());
49                                 if(nameMap->count(name)==0){        m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
50                 }
51                 
52                 //initialize distance matrix to cutoff
53                 dMatrix.resize(nseqs);
54                 //colDist temp(0, 0, aboveCutoff);
55                 //rowSmallDists.resize(nseqs, temp);
56                 for (int i = 1; i < nseqs; i++) {                       
57                         dMatrix[i].resize(i, aboveCutoff);              
58                 }                                                                                               
59                                                                                                                 
60                 
61                 char d;
62                 while((d=fileHandle.get()) != EOF){
63
64                                 if(isalnum(d)){
65                                                 square = 1;
66                                                 fileHandle.putback(d);
67                                                 for(int i=0;i<nseqs;i++){
68                                                                 fileHandle >> distance;
69                                                 }
70                                                 break;
71                                 }
72                                 if(d == '\n'){
73                                                 square = 0;
74                                                 break;
75                                 }
76                 }
77
78                 Progress* reading;
79
80                 if(square == 0){
81
82                                 reading = new Progress("Reading matrix:     ", nseqs * (nseqs - 1) / 2);
83
84                                 int        index = 0;
85
86                                 for(int i=1;i<nseqs;i++){
87                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
88                                                 
89                                                 fileHandle >> name;
90                                                 matrixNames.push_back(name);
91                 
92
93                                                 //there's A LOT of repeated code throughout this method...
94                                                 if(nameMap == NULL){
95                                                                 list->set(i, name);
96                                                 
97                                                                 for(int j=0;j<i;j++){
98                                                                 
99                                                                                 if (m->control_pressed) { delete reading; fileHandle.close(); return 0;  }
100                                                                                 
101                                                                                 fileHandle >> distance;
102                                                         
103                                                                                 if (distance == -1) { distance = 1000000; }
104                                                                                 else if (sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
105                                                                 
106                                                                                 //if(distance < cutoff){
107                                                                                         dMatrix[i][j] = distance;
108                                                                                         if (distance < smallDist) { smallDist = distance; }
109                                                                                         //if (rowSmallDists[i].dist > distance) {  rowSmallDists[i].dist = distance; rowSmallDists[i].col = j; rowSmallDists[i].row = i; }
110                                                                                         //if (rowSmallDists[j].dist > distance) {  rowSmallDists[j].dist = distance; rowSmallDists[j].col = i; rowSmallDists[j].row = j; }
111                                                                                 //}
112                                                                                 index++;
113                                                                                 reading->update(index);
114                                                                 }
115                                 
116                                                 }
117                                                 else{
118                                                                 if(nameMap->count(name)==0){        m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
119                                 
120                                                                 for(int j=0;j<i;j++){
121                                                                                 fileHandle >> distance;
122                                                                                 
123                                                                                 if (m->control_pressed) { delete reading; fileHandle.close(); return 0;  }
124                                 
125                                                                                 if (distance == -1) { distance = 1000000; }
126                                                                                 else if (sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
127                                                                                 
128                                                                                 //if(distance < cutoff){
129                                                                                         if (distance < smallDist) { smallDist = distance; }
130                                                                                         
131                                                                                         int row = nameMap->get(matrixNames[i]);
132                                                                                         int col = nameMap->get(matrixNames[j]);
133                                                                                         
134                                                                                         if (row < col) {  dMatrix[col][row] = distance; }
135                                                                                         else { dMatrix[row][col] = distance; }
136                                                                                         
137                                                                                         //if (rowSmallDists[row].dist > distance) {  rowSmallDists[row].dist = distance; rowSmallDists[row].col = col; rowSmallDists[row].row = row; }
138                                                                                         //if (rowSmallDists[col].dist > distance) {  rowSmallDists[col].dist = distance; rowSmallDists[col].col = row; rowSmallDists[col].row = col; }
139                                                                                 //}
140                                                                                 index++;
141                                                                                 reading->update(index);
142                                                                 }
143                                                 }
144                                 }
145                 }
146                 else{
147
148                                 reading = new Progress("Reading matrix:     ", nseqs * nseqs);
149                 
150                                 int index = nseqs;
151
152                                 for(int i=1;i<nseqs;i++){
153                                                 fileHandle >> name;                
154                                                 matrixNames.push_back(name);
155                                                 
156                                                 if(nameMap == NULL){
157                                                                 list->set(i, name);
158                                                                 for(int j=0;j<nseqs;j++){
159                                                                                 fileHandle >> distance;
160                                                                                 
161                                                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
162                                                                                 
163                                                                                 if (distance == -1) { distance = 1000000; }
164                                                                                 else if (sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
165                                                                                 
166                                                                                 if(j < i){
167                                                                                         if (distance < smallDist) { smallDist = distance; }
168                                                                                         
169                                                                                         dMatrix[i][j] = distance;
170                                                                                         //if (rowSmallDists[i].dist > distance) {  rowSmallDists[i].dist = distance; rowSmallDists[i].col = j; rowSmallDists[i].row = i; }
171                                                                                         //if (rowSmallDists[j].dist > distance) {  rowSmallDists[j].dist = distance; rowSmallDists[j].col = i; rowSmallDists[j].row = j; }
172                                                                                 }
173                                                                                 index++;
174                                                                                 reading->update(index);
175                                                                 }
176                                                 
177                                                 }
178                                                 else{
179                                                                 if(nameMap->count(name)==0){        m->mothurOut("Error: Sequence '" + name + "' was not found in the names file, please correct"); m->mothurOutEndLine(); }
180                                 
181                                                                 for(int j=0;j<nseqs;j++){
182                                                                                 fileHandle >> distance;
183                                                                                 
184                                                                                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
185                                                                                 
186                                                                            if (distance == -1) { distance = 1000000; }
187                                                                                 else if (sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.                                                        
188                                                                                 
189                                                                                 if(j < i){
190                                                                                         if (distance < smallDist) { smallDist = distance; }
191                                                                                         
192                                                                                         int row = nameMap->get(matrixNames[i]);
193                                                                                         int col = nameMap->get(matrixNames[j]);
194                                                                                         
195                                                                                         if (row < col) {  dMatrix[col][row] = distance; }
196                                                                                         else { dMatrix[row][col] = distance; }
197                                                                                         
198                                                                                         //if (rowSmallDists[row].dist > distance) {  rowSmallDists[row].dist = distance; rowSmallDists[row].col = col; rowSmallDists[row].row = row; }
199                                                                                         //if (rowSmallDists[col].dist > distance) {  rowSmallDists[col].dist = distance; rowSmallDists[col].col = row; rowSmallDists[col].row = col; }
200                                                                                 }
201                                                                                 index++;
202                                                                                 reading->update(index);
203                                                                 }
204                                                 }
205                                 }
206                 }
207                 
208                 if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
209                 
210                 reading->finish();
211                 delete reading;
212
213                 list->setLabel("0");
214                 rabund = new RAbundVector(list->getRAbundVector());
215                 
216                 fileHandle.close();
217                 
218                 return 0;
219         }
220         catch(exception& e) {
221                 m->errorOut(e, "ClusterClassic", "readPhylipFile");
222                 exit(1);
223         }
224
225 }
226 /***********************************************************************/
227 //sets smallCol and smallRow, returns distance
228 double ClusterClassic::getSmallCell() {
229         try {
230                         
231                 smallDist = aboveCutoff;
232                 smallRow = 1;
233                 smallCol = 0;
234                 
235                 vector<colDist> mins;
236                 
237                 for(int i=1;i<nseqs;i++){
238                         for(int j=0;j<i;j++){ 
239                                 if (dMatrix[i][j] < smallDist) {
240                                         mins.clear();
241                                         colDist temp(i, j, dMatrix[i][j]);
242                                         mins.push_back(temp); 
243                                         smallDist = dMatrix[i][j];
244                                 }else if (dMatrix[i][j] == smallDist) {
245                                         colDist temp(i, j, dMatrix[i][j]);
246                                         mins.push_back(temp); 
247
248                                 }
249                         }
250                 }
251                 
252                 if (mins.size() > 0) {
253                         int zrand = 0;
254                         if (mins.size() > 1) {
255                                 //pick random number between 0 and mins.size()
256                                 zrand = (int)((float)(rand()) / (RAND_MAX / (mins.size()-1) + 1));
257                         }
258                         
259                         smallRow = mins[zrand].row;
260                         smallCol = mins[zrand].col;
261                 
262                 }
263         //cout << smallRow << '\t' << smallCol << '\t' << smallDist << endl;
264                 //eliminate smallCell
265                 if (smallRow < smallCol) { dMatrix[smallCol][smallRow] = aboveCutoff; }
266                 else { dMatrix[smallRow][smallCol] = aboveCutoff; }
267                 
268                 return smallDist;
269                 
270         }
271         catch(exception& e) {
272                 m->errorOut(e, "ClusterClassic", "getSmallCell");
273                 exit(1);
274         }
275 }
276 /***********************************************************************/
277 void ClusterClassic::clusterBins(){
278         try {
279         //      cout << smallCol << '\t' << smallRow << '\t' << smallDist << '\t' << rabund->get(smallRow) << '\t' << rabund->get(smallCol);
280
281                 rabund->set(smallRow, rabund->get(smallRow)+rabund->get(smallCol));     
282                 rabund->set(smallCol, 0);       
283                 /*for (int i = smallCol+1; i < rabund->size(); i++) {
284                         rabund->set((i-1), rabund->get(i));
285                 }
286                 rabund->resize((rabund->size()-1));*/
287                 rabund->setLabel(toString(smallDist));
288
289         //      cout << '\t' << rabund->get(smallRow) << '\t' << rabund->get(smallCol) << endl;
290         }
291         catch(exception& e) {
292                 m->errorOut(e, "ClusterClassic", "clusterBins");
293                 exit(1);
294         }
295 }
296 /***********************************************************************/
297 void ClusterClassic::clusterNames(){
298         try {
299         //      cout << smallCol << '\t' << smallRow << '\t' << smallDist << '\t' << list->get(smallRow) << '\t' << list->get(smallCol);
300                 if (mapWanted) {  updateMap();  }
301                 
302                 list->set(smallRow, list->get(smallRow)+','+list->get(smallCol));
303                 list->set(smallCol, "");        
304                 /*for (int i = smallCol+1; i < list->size(); i++) {
305                         list->set((i-1), list->get(i));
306                 }
307                 list->resize((list->size()-1));*/
308                 list->setLabel(toString(smallDist));
309         
310         //      cout << '\t' << list->get(smallRow) << '\t' << list->get(smallCol) << endl;
311     }
312         catch(exception& e) {
313                 m->errorOut(e, "ClusterClassic", "clusterNames");
314                 exit(1);
315         }
316 }
317 /***********************************************************************/
318 void ClusterClassic::update(double& cutOFF){
319         try {
320 //print();              
321                 getSmallCell();
322                 
323                 int r, c;
324                 r = smallRow; c = smallCol;
325                                 
326                 for(int i=0;i<nseqs;i++){
327                         if(i != r && i != c){
328                                 double distRow, distCol, newDist;
329                                 if (i > r) { distRow = dMatrix[i][r]; }
330                                 else { distRow =  dMatrix[r][i]; }
331
332                                 if (i > c) { distCol = dMatrix[i][c]; dMatrix[i][c] = aboveCutoff; } //like removeCell
333                                 else { distCol =  dMatrix[c][i]; dMatrix[c][i] = aboveCutoff; }
334                                 
335                                 if(method == "furthest"){
336                                         newDist = max(distRow, distCol);
337                                 }
338                                 else if (method == "average"){
339                                         int rowBin = rabund->get(r);
340                                         int colBin = rabund->get(c);
341                                         newDist = (colBin * distCol + rowBin * distRow) / (rowBin + colBin);
342                                 }
343                                 else if (method == "weighted"){
344                                         newDist = (distCol + distRow) / 2.0;
345                                 }
346                                 else if (method == "nearest"){
347                                         newDist = min(distRow, distCol);
348                                 }
349                                 //cout << "newDist = " << newDist << endl;      
350                                 if (i > r) { dMatrix[i][r] = newDist; }
351                                 else { dMatrix[r][i] = newDist; }
352                                 
353                         }
354                 }
355                         
356                 clusterBins();
357                 clusterNames();
358                 
359                 //resize each row
360                 /*for(int i=0;i<nseqs;i++){
361                         for(int j=c+1;j<dMatrix[i].size();j++){
362                                 dMatrix[i][j-1]=dMatrix[i][j];
363                         }
364                 }                       
365                 
366                 //resize each col
367                 for(int i=c+1;i<nseqs;i++){
368                         for(int j=0;j < dMatrix[i-1].size();j++){
369                                 dMatrix[i-1][j]=dMatrix[i][j];
370                         }
371                 }       
372                 
373                 nseqs--;
374                 dMatrix.pop_back();*/
375
376         }
377         catch(exception& e) {
378                 m->errorOut(e, "ClusterClassic", "update");
379                 exit(1);
380         }
381 }
382 /***********************************************************************/
383 void ClusterClassic::setMapWanted(bool f)  {  
384         try {
385                 mapWanted = f;
386                 
387                 //initialize map
388                 for (int i = 0; i < list->getNumBins(); i++) {
389                         
390                         //parse bin 
391                         string names = list->get(i);
392                         while (names.find_first_of(',') != -1) { 
393                                 //get name from bin
394                                 string name = names.substr(0,names.find_first_of(','));
395                                 //save name and bin number
396                                 seq2Bin[name] = i;
397                                 names = names.substr(names.find_first_of(',')+1, names.length());
398                         }
399                         
400                         //get last name
401                         seq2Bin[names] = i;
402                 }
403                 
404         }
405         catch(exception& e) {
406                 m->errorOut(e, "ClusterClassic", "setMapWanted");
407                 exit(1);
408         }
409 }
410 /***********************************************************************/
411 void ClusterClassic::updateMap() {
412 try {
413                 //update location of seqs in smallRow since they move to smallCol now
414                 string names = list->get(smallRow);
415                 while (names.find_first_of(',') != -1) { 
416                         //get name from bin
417                         string name = names.substr(0,names.find_first_of(','));
418                         //save name and bin number
419                         seq2Bin[name] = smallCol;
420                         names = names.substr(names.find_first_of(',')+1, names.length());
421                 }
422                         
423                 //get last name
424                 seq2Bin[names] = smallCol;
425                 
426         }
427         catch(exception& e) {
428                 m->errorOut(e, "ClusterClassic", "updateMap");
429                 exit(1);
430         }
431 }
432 /***********************************************************************/
433 void ClusterClassic::print() {
434 try {
435                 //update location of seqs in smallRow since they move to smallCol now
436                 for (int i = 0; i < dMatrix.size(); i++) {
437                         m->mothurOut("row = " + toString(i) + "\t");
438                         for (int j = 0; j < dMatrix[i].size(); j++) {
439                                 m->mothurOut(toString(dMatrix[i][j]) + "\t");
440                         }
441                         m->mothurOutEndLine();
442                 }
443         }
444         catch(exception& e) {
445                 m->errorOut(e, "ClusterClassic", "updateMap");
446                 exit(1);
447         }
448 }
449 /***********************************************************************/
450