]> git.donarmstrong.com Git - mothur.git/blob - cluster.cpp
sffinfo bug with flow grams right index when clipQualRight=0
[mothur.git] / cluster.cpp
1 /*
2  *  cluster.cpp
3  *  
4  *
5  *  Created by Pat Schloss on 8/14/08.
6  *  Copyright 2008 Patrick D. Schloss. All rights reserved.
7  *
8  */
9
10 #include "cluster.hpp"
11 #include "rabundvector.hpp"
12 #include "listvector.hpp"
13
14 /***********************************************************************/
15
16 Cluster::Cluster(RAbundVector* rav, ListVector* lv, SparseDistanceMatrix* dm, float c, string f) :
17 rabund(rav), list(lv), dMatrix(dm), method(f)
18 {
19         try {
20         
21         mapWanted = false;  //set to true by mgcluster to speed up overlap merge
22         
23         //save so you can modify as it changes in average neighbor
24         cutoff = c;
25         m = MothurOut::getInstance();
26         }
27         catch(exception& e) {
28                 m->errorOut(e, "Cluster", "Cluster");
29                 exit(1);
30         }
31 }
32 /***********************************************************************/
33 void Cluster::clusterBins(){
34         try {
35                 rabund->set(smallCol, rabund->get(smallRow)+rabund->get(smallCol));     
36                 rabund->set(smallRow, 0);       
37                 rabund->setLabel(toString(smallDist));
38         }
39         catch(exception& e) {
40                 m->errorOut(e, "Cluster", "clusterBins");
41                 exit(1);
42         }
43 }
44 /***********************************************************************/
45
46 void Cluster::clusterNames(){
47         try {
48                 if (mapWanted) {  updateMap();  }
49                 
50                 list->set(smallCol, list->get(smallRow)+','+list->get(smallCol));
51                 list->set(smallRow, "");        
52                 list->setLabel(toString(smallDist));
53     }
54         catch(exception& e) {
55                 m->errorOut(e, "Cluster", "clusterNames");
56                 exit(1);
57         }
58 }
59 /***********************************************************************/
60 void Cluster::update(double& cutOFF){
61         try {
62         smallCol = dMatrix->getSmallestCell(smallRow);
63         nColCells = dMatrix->seqVec[smallCol].size();
64         nRowCells = dMatrix->seqVec[smallRow].size();
65         
66                 vector<int> foundCol(nColCells, 0);
67         //cout << dMatrix->getNNodes() << " small cell: " << smallRow << '\t' << smallCol << endl;
68                 int search;
69                 bool changed;
70         
71                 for (int i=nRowCells-1;i>=0;i--) {
72             if (m->control_pressed) { break; }
73              
74                         //if you are not the smallCell
75                         if (dMatrix->seqVec[smallRow][i].index != smallCol) { 
76                 search = dMatrix->seqVec[smallRow][i].index;
77                 
78                                 bool merged = false;
79                                 for (int j=0;j<nColCells;j++) {
80                     
81                                         if (dMatrix->seqVec[smallCol][j].index != smallRow) {  //if you are not the smallest distance
82                                                 if (dMatrix->seqVec[smallCol][j].index == search) {
83                                                         foundCol[j] = 1;
84                                                         merged = true;
85                                                         changed = updateDistance(dMatrix->seqVec[smallCol][j], dMatrix->seqVec[smallRow][i]);
86                             dMatrix->updateCellCompliment(smallCol, j);
87                                                         break;
88                                                 }else if (dMatrix->seqVec[smallCol][j].index < search) { j+=nColCells; } //we don't have a distance for this cell 
89                                         }       
90                                 }
91                                 //if not merged it you need it for warning 
92                                 if ((!merged) && (method == "average" || method == "weighted")) {  
93                                         if (cutOFF > dMatrix->seqVec[smallRow][i].dist) {  
94                                                 cutOFF = dMatrix->seqVec[smallRow][i].dist;
95                         //cout << "changing cutoff to " << cutOFF << endl;
96                                         }
97                     
98                                 }
99                                 dMatrix->rmCell(smallRow, i);
100                         }
101                 }
102                 clusterBins();
103                 clusterNames();
104         
105                 // Special handling for singlelinkage case, not sure whether this
106                 // could be avoided
107                 for (int i=nColCells-1;i>=0;i--) {
108                         if (foundCol[i] == 0) { 
109                                 if (method == "average" || method == "weighted") {
110                                         if (dMatrix->seqVec[smallCol][i].index != smallRow) { //if you are not hte smallest distance 
111                                                 if (cutOFF > dMatrix->seqVec[smallCol][i].dist) {  
112                                                         cutOFF = dMatrix->seqVec[smallCol][i].dist;  
113                                                 }
114                                         }
115                                 }
116                 dMatrix->rmCell(smallCol, i);
117                         }
118                 }
119         
120         }
121         catch(exception& e) {
122                 m->errorOut(e, "Cluster", "update");
123                 exit(1);
124         }
125 }
126 /***********************************************************************/
127 void Cluster::setMapWanted(bool f)  {  
128         try {
129                 mapWanted = f;
130                 
131         //initialize map
132                 for (int k = 0; k < list->getNumBins(); k++) {
133             
134             string names = list->get(k);
135             
136             //parse bin
137             string individual = "";
138             int binNameslength = names.size();
139             for(int j=0;j<binNameslength;j++){
140                 if(names[j] == ','){
141                     seq2Bin[individual] = k;
142                     individual = "";                            
143                 }
144                 else{  individual += names[j];  }
145             }
146             //get last name
147             seq2Bin[individual] = k;
148                 }
149                 
150         }
151         catch(exception& e) {
152                 m->errorOut(e, "Cluster", "setMapWanted");
153                 exit(1);
154         }
155 }
156 /***********************************************************************/
157 void Cluster::updateMap() {
158     try {
159                 //update location of seqs in smallRow since they move to smallCol now
160                 string names = list->get(smallRow);
161                 
162         string individual = "";
163         int binNameslength = names.size();
164         for(int j=0;j<binNameslength;j++){
165             if(names[j] == ','){
166                 seq2Bin[individual] = smallCol;
167                 individual = "";                                
168             }
169             else{  individual += names[j];  }
170         }
171         //get last name
172         seq2Bin[individual] = smallCol;         
173         
174         }
175         catch(exception& e) {
176                 m->errorOut(e, "Cluster", "updateMap");
177                 exit(1);
178         }
179 }
180 /***********************************************************************/
181
182
183