]> git.donarmstrong.com Git - mothur.git/blob - cluster.cpp
created mothurOut class to handle logfiles
[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 #include "sparsematrix.hpp"
14
15 /***********************************************************************/
16
17 Cluster::Cluster(RAbundVector* rav, ListVector* lv, SparseMatrix* dm, float c, string f) :
18 rabund(rav), list(lv), dMatrix(dm), method(f)
19 {
20 /*
21         cout << "sizeof(MatData): " << sizeof(MatData) << endl;
22         cout << "sizeof(PCell*): " << sizeof(PCell*) << endl;
23
24         int nCells = dMatrix->getNNodes();
25         time_t start = time(NULL);
26
27         MatVec matvec = MatVec(nCells); 
28         int i = 0;
29         for (MatData currentCell = dMatrix->begin(); currentCell != dMatrix->end(); currentCell++) {
30                 matvec[i++] = currentCell;
31         }
32         for (i= matvec.size();i>0;i--) {
33                 dMatrix->rmCell(matvec[i-1]);
34         }
35         MatData it = dMatrix->begin(); 
36         while (it != dMatrix->end()) { 
37                 it = dMatrix->rmCell(it);
38         }
39         cout << "Time to remove " << nCells << " cells: " << time(NULL) - start << " seconds" << endl;
40     exit(0);
41         MatData it = dMatrix->begin();
42         cout << it->row << "/" << it->column << "/" << it->dist << endl;
43         dMatrix->rmCell(dMatrix->begin());
44         cout << it->row << "/" << it->column << "/" << it->dist << endl;
45         exit(0);
46 */
47
48         // Create a data structure to quickly access the PCell information
49         // for a certain sequence. It consists of a vector of lists, where 
50         // a list contains pointers (iterators) to the all distances related
51         // to a certain sequence. The Vector is accessed via the index of a 
52         // sequence in the distance matrix.
53         seqVec = vector<MatVec>(lv->size());
54         for (MatData currentCell = dMatrix->begin(); currentCell != dMatrix->end(); currentCell++) {
55                 seqVec[currentCell->row].push_back(currentCell);
56                 seqVec[currentCell->column].push_back(currentCell);
57         }
58         mapWanted = false;  //set to true by mgcluster to speed up overlap merge
59         
60         //save so you can modify as it changes in average neighbor
61         cutoff = c;
62         m = MothurOut::getInstance();
63 }
64
65 /***********************************************************************/
66
67 void Cluster::getRowColCells() {
68         try {
69                 PCell* smallCell = dMatrix->getSmallestCell();  //find the smallest cell - this routine should probably not be in the SpMat class
70         
71                 smallRow = smallCell->row;              // get its row
72                 smallCol = smallCell->column;   // get its column
73                 smallDist = smallCell->dist;    // get the smallest distance
74         
75                 rowCells = seqVec[smallRow];    // all distances related to the row index
76                 colCells = seqVec[smallCol];    // all distances related to the column index
77                 nRowCells = rowCells.size();
78                 nColCells = colCells.size();
79         }
80         catch(exception& e) {
81                 m->errorOut(e, "Cluster", "getRowColCells");
82                 exit(1);
83         }
84
85 }
86 /***********************************************************************/
87 // Remove the specified cell from the seqVec and from the sparse
88 // matrix
89 void Cluster::removeCell(const MatData& cell, int vrow, int vcol, bool rmMatrix)
90 {
91         ull drow = cell->row;
92         ull dcol = cell->column;
93         if (((vrow >=0) && (drow != smallRow)) ||
94                 ((vcol >=0) && (dcol != smallCol))) {
95                 ull dtemp = drow;
96                 drow = dcol;
97                 dcol = dtemp;
98         }
99
100         ull crow;
101         ull ccol;
102         int nCells;
103         if (vrow < 0) {
104                 nCells = seqVec[drow].size();
105                 for (vrow=0; vrow<nCells;vrow++) {
106                         crow = seqVec[drow][vrow]->row;
107                         ccol = seqVec[drow][vrow]->column;
108                         if (((crow == drow) && (ccol == dcol)) ||
109                                 ((ccol == drow) && (crow == dcol))) {
110                                 break;
111                         }
112                 }
113         }
114         seqVec[drow].erase(seqVec[drow].begin()+vrow);
115         if (vcol < 0) {
116                 nCells = seqVec[dcol].size();
117                 for (vcol=0; vcol<nCells;vcol++) {
118                         crow = seqVec[dcol][vcol]->row;
119                         ccol = seqVec[dcol][vcol]->column;
120                         if (((crow == drow) && (ccol == dcol)) ||
121                                 ((ccol == drow) && (crow == dcol))) {
122                                 break;
123                         }
124                 }
125         }
126         seqVec[dcol].erase(seqVec[dcol].begin()+vcol);
127         if (rmMatrix) {
128                 dMatrix->rmCell(cell);
129         }
130 }
131
132
133 /***********************************************************************/
134
135 void Cluster::clusterBins(){
136         try {
137         //      cout << smallCol << '\t' << smallRow << '\t' << smallDist << '\t' << rabund->get(smallRow) << '\t' << rabund->get(smallCol);
138
139                 rabund->set(smallCol, rabund->get(smallRow)+rabund->get(smallCol));     
140                 rabund->set(smallRow, 0);       
141                 rabund->setLabel(toString(smallDist));
142
143         //      cout << '\t' << rabund->get(smallRow) << '\t' << rabund->get(smallCol) << endl;
144         }
145         catch(exception& e) {
146                 m->errorOut(e, "Cluster", "clusterBins");
147                 exit(1);
148         }
149
150
151 }
152
153 /***********************************************************************/
154
155 void Cluster::clusterNames(){
156         try {
157         //      cout << smallCol << '\t' << smallRow << '\t' << smallDist << '\t' << list->get(smallRow) << '\t' << list->get(smallCol);
158                 if (mapWanted) {  updateMap();  }
159                 
160                 list->set(smallCol, list->get(smallRow)+','+list->get(smallCol));
161                 list->set(smallRow, "");        
162                 list->setLabel(toString(smallDist));
163         
164         //      cout << '\t' << list->get(smallRow) << '\t' << list->get(smallCol) << endl;
165     }
166         catch(exception& e) {
167                 m->errorOut(e, "Cluster", "clusterNames");
168                 exit(1);
169         }
170
171 }
172
173 /***********************************************************************/
174 //This function clusters based on the method of the derived class
175 //At the moment only average and complete linkage are covered, because
176 //single linkage uses a different approach.
177 void Cluster::update(double& cutOFF){
178         try {
179                 getRowColCells();       
180         
181                 vector<int> foundCol(nColCells, 0);
182
183                 int search;
184                 bool changed;
185
186                 // The vector has to be traversed in reverse order to preserve the index
187                 // for faster removal in removeCell()
188                 for (int i=nRowCells-1;i>=0;i--) {
189                         if (!((rowCells[i]->row == smallRow) && (rowCells[i]->column == smallCol))) {
190                                 if (rowCells[i]->row == smallRow) {
191                                         search = rowCells[i]->column;
192                                 } else {
193                                         search = rowCells[i]->row;
194                                 }
195                                 
196                                 bool merged = false;
197                                 for (int j=0;j<nColCells;j++) {
198                                         if (!((colCells[j]->row == smallRow) && (colCells[j]->column == smallCol))) { //if you are not hte smallest distance
199                                                 if (colCells[j]->row == search || colCells[j]->column == search) {
200                                                         foundCol[j] = 1;
201                                                         merged = true;
202                                                         changed = updateDistance(colCells[j], rowCells[i]);
203                                                         // If the cell's distance changed and it had the same distance as 
204                                                         // the smallest distance, invalidate the mins vector in SparseMatrix
205                                                         if (changed) {
206                                                                 if (colCells[j]->vectorMap != NULL) {
207                                                                         *(colCells[j]->vectorMap) = NULL;
208                                                                         colCells[j]->vectorMap = NULL;
209                                                                 }
210                                                         }
211                                                         break;
212                                                 }
213                                         }               
214                                 }
215                                 //if not merged it you need it for warning 
216                                 if ((!merged) && (method == "average")) {  
217                                         //m->mothurOut("Warning: trying to merge cell " + toString(rowCells[i]->row+1) + " " + toString(rowCells[i]->column+1) + " distance " + toString(rowCells[i]->dist) + " with value above cutoff. Results may vary from using cutoff at cluster command instead of read.dist."); m->mothurOutEndLine(); 
218                                         if (cutOFF > rowCells[i]->dist) {  
219                                                 cutOFF = rowCells[i]->dist;  
220                                                 //m->mothurOut("changing cutoff to " + toString(cutOFF));  m->mothurOutEndLine(); 
221                                         }
222
223                                 }
224                                 removeCell(rowCells[i], i , -1);  
225                                 
226                         }
227                 }
228                 clusterBins();
229                 clusterNames();
230
231                 // Special handling for singlelinkage case, not sure whether this
232                 // could be avoided
233                 for (int i=nColCells-1;i>=0;i--) {
234                         if (foundCol[i] == 0) {
235                                 if (method == "average") {
236                                         if (!((colCells[i]->row == smallRow) && (colCells[i]->column == smallCol))) {
237                                                 //m->mothurOut("Warning: merging cell " + toString(colCells[i]->row+1) + " " + toString(colCells[i]->column+1) + " distance " + toString(colCells[i]->dist) + " value above cutoff. Results may vary from using cutoff at cluster command instead of read.dist."); m->mothurOutEndLine();
238                                                 if (cutOFF > colCells[i]->dist) {  
239                                                         cutOFF = colCells[i]->dist;  
240                                                         //m->mothurOut("changing cutoff to " + toString(cutOFF));  m->mothurOutEndLine(); 
241                                                 }
242                                         }
243                                 }
244                                 removeCell(colCells[i], -1, i);
245                         }
246                 }
247         }
248         catch(exception& e) {
249                 m->errorOut(e, "Cluster", "update");
250                 exit(1);
251         }
252 }
253 /***********************************************************************/
254 void Cluster::setMapWanted(bool f)  {  
255         try {
256                 mapWanted = f;
257                 
258                 //initialize map
259                 for (int i = 0; i < list->getNumBins(); i++) {
260                         
261                         //parse bin 
262                         string names = list->get(i);
263                         while (names.find_first_of(',') != -1) { 
264                                 //get name from bin
265                                 string name = names.substr(0,names.find_first_of(','));
266                                 //save name and bin number
267                                 seq2Bin[name] = i;
268                                 names = names.substr(names.find_first_of(',')+1, names.length());
269                         }
270                         
271                         //get last name
272                         seq2Bin[names] = i;
273                 }
274                 
275         }
276         catch(exception& e) {
277                 m->errorOut(e, "Cluster", "setMapWanted");
278                 exit(1);
279         }
280 }
281 /***********************************************************************/
282 void Cluster::updateMap() {
283 try {
284                 //update location of seqs in smallRow since they move to smallCol now
285                 string names = list->get(smallRow);
286                 while (names.find_first_of(',') != -1) { 
287                         //get name from bin
288                         string name = names.substr(0,names.find_first_of(','));
289                         //save name and bin number
290                         seq2Bin[name] = smallCol;
291                         names = names.substr(names.find_first_of(',')+1, names.length());
292                 }
293                         
294                 //get last name
295                 seq2Bin[names] = smallCol;
296                 
297         }
298         catch(exception& e) {
299                 m->errorOut(e, "Cluster", "updateMap");
300                 exit(1);
301         }
302 }
303 /***********************************************************************/
304
305
306