File: cluster.cpp

package info (click to toggle)
mothur 1.33.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,248 kB
  • ctags: 12,231
  • sloc: cpp: 152,046; fortran: 665; makefile: 74; sh: 34
file content (203 lines) | stat: -rw-r--r-- 6,792 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 *  cluster.cpp
 *  
 *
 *  Created by Pat Schloss on 8/14/08.
 *  Copyright 2008 Patrick D. Schloss. All rights reserved.
 *
 */

#include "cluster.hpp"
#include "rabundvector.hpp"
#include "listvector.hpp"

/***********************************************************************/

Cluster::Cluster(RAbundVector* rav, ListVector* lv, SparseDistanceMatrix* dm, float c, string f, float cs) :
rabund(rav), list(lv), dMatrix(dm), method(f), adjust(cs)
{
	try {
        
        mapWanted = false;  //set to true by mgcluster to speed up overlap merge
        
        //save so you can modify as it changes in average neighbor
        cutoff = c;
        m = MothurOut::getInstance();
	}
	catch(exception& e) {
		m->errorOut(e, "Cluster", "Cluster");
		exit(1);
	}
}
/***********************************************************************/
void Cluster::clusterBins(){
	try {
 		rabund->set(smallCol, rabund->get(smallRow)+rabund->get(smallCol));	
		rabund->set(smallRow, 0);	
		rabund->setLabel(toString(smallDist));
	}
	catch(exception& e) {
		m->errorOut(e, "Cluster", "clusterBins");
		exit(1);
	}
}
/***********************************************************************/

void Cluster::clusterNames(){
	try {
		if (mapWanted) {  updateMap();  }
		
		list->set(smallCol, list->get(smallRow)+','+list->get(smallCol));
		list->set(smallRow, "");	
		list->setLabel(toString(smallDist));
    }
	catch(exception& e) {
		m->errorOut(e, "Cluster", "clusterNames");
		exit(1);
	}
}
/***********************************************************************/
void Cluster::update(double& cutOFF){
	try {
        smallCol = dMatrix->getSmallestCell(smallRow);
        nColCells = dMatrix->seqVec[smallCol].size();
        nRowCells = dMatrix->seqVec[smallRow].size();
        
		vector<int> foundCol(nColCells, 0);
        //cout << dMatrix->getNNodes() << " small cell: " << smallRow << '\t' << smallCol << endl;
		int search;
		bool changed;
        
		for (int i=nRowCells-1;i>=0;i--) {
            if (m->control_pressed) { break; }
             
			//if you are not the smallCell
			if (dMatrix->seqVec[smallRow][i].index != smallCol) { 
                search = dMatrix->seqVec[smallRow][i].index;
                
				bool merged = false;
				for (int j=0;j<nColCells;j++) {
                    
					if (dMatrix->seqVec[smallCol][j].index != smallRow) {  //if you are not the smallest distance
						if (dMatrix->seqVec[smallCol][j].index == search) {
							foundCol[j] = 1;
							merged = true;
							changed = updateDistance(dMatrix->seqVec[smallCol][j], dMatrix->seqVec[smallRow][i]);
                            dMatrix->updateCellCompliment(smallCol, j);
							break;
						}else if (dMatrix->seqVec[smallCol][j].index < search) { //we don't have a distance for this cell
                            if (adjust != -1.0) { //adjust
                                merged = true;
                                PDistCell value(search, adjust); //create a distance for the missing value
                                int location = dMatrix->addCellSorted(smallCol, value);
                                changed = updateDistance(dMatrix->seqVec[smallCol][location], dMatrix->seqVec[smallRow][i]);
                                dMatrix->updateCellCompliment(smallCol, location);
                                nColCells++;
                                foundCol.push_back(0); //add a new found column
                                //adjust value
                                for (int k = foundCol.size()-1; k > location; k--) { foundCol[k] = foundCol[k-1]; }
                                foundCol[location] = 1;
                            }
                            j+=nColCells;
                        } 
					}	
				}
				//if not merged it you need it for warning 
				if ((!merged) && (method == "average" || method == "weighted")) {  
					if (cutOFF > dMatrix->seqVec[smallRow][i].dist) {  
						cutOFF = dMatrix->seqVec[smallRow][i].dist;
                        //cout << "changing cutoff to " << cutOFF << endl;
					}
                    
				}
				dMatrix->rmCell(smallRow, i);
			}
		}
		clusterBins();
		clusterNames();
        
		// Special handling for singlelinkage case, not sure whether this
		// could be avoided
		for (int i=nColCells-1;i>=0;i--) {
			if (foundCol[i] == 0) {
                if (adjust != -1.0) { //adjust
                    PDistCell value(smallCol, adjust); //create a distance for the missing value
                    changed = updateDistance(dMatrix->seqVec[smallCol][i], value);
                    dMatrix->updateCellCompliment(smallCol, i);
                }else {
                    if (method == "average" || method == "weighted") {
                        if (dMatrix->seqVec[smallCol][i].index != smallRow) { //if you are not hte smallest distance 
                            if (cutOFF > dMatrix->seqVec[smallCol][i].dist) {  
                                cutOFF = dMatrix->seqVec[smallCol][i].dist;  
                            }
                        }
                    }
                }
                dMatrix->rmCell(smallCol, i);
			}
		}
        
	}
	catch(exception& e) {
		m->errorOut(e, "Cluster", "update");
		exit(1);
	}
}
/***********************************************************************/
void Cluster::setMapWanted(bool f)  {  
	try {
		mapWanted = f;
		
        //initialize map
		for (int k = 0; k < list->getNumBins(); k++) {
            
            string names = list->get(k);
            
            //parse bin
            string individual = "";
            int binNameslength = names.size();
            for(int j=0;j<binNameslength;j++){
                if(names[j] == ','){
                    seq2Bin[individual] = k;
                    individual = "";				
                }
                else{  individual += names[j];  }
            }
            //get last name
            seq2Bin[individual] = k;
		}
		
	}
	catch(exception& e) {
		m->errorOut(e, "Cluster", "setMapWanted");
		exit(1);
	}
}
/***********************************************************************/
void Cluster::updateMap() {
    try {
		//update location of seqs in smallRow since they move to smallCol now
		string names = list->get(smallRow);
		
        string individual = "";
        int binNameslength = names.size();
        for(int j=0;j<binNameslength;j++){
            if(names[j] == ','){
                seq2Bin[individual] = smallCol;
                individual = "";				
            }
            else{  individual += names[j];  }
        }
        //get last name
        seq2Bin[individual] = smallCol;		
	
	}
	catch(exception& e) {
		m->errorOut(e, "Cluster", "updateMap");
		exit(1);
	}
}
/***********************************************************************/