File: sparsematrix.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 (177 lines) | stat: -rw-r--r-- 4,756 bytes parent folder | download | duplicates (3)
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

#include "sparsematrix.hpp"
#include "listvector.hpp"


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

SparseMatrix::SparseMatrix() : numNodes(0), minsIndex(0), smallDist(1e6){  m = MothurOut::getInstance();  }

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

int SparseMatrix::getNNodes(){
	return numNodes; 
}

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

float SparseMatrix::getSmallDist(){
	return smallDist;
}

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

MatData SparseMatrix::rmCell(MatData data){
	try {
		if(data->vectorMap != NULL ){
			*(data->vectorMap) = NULL;
			data->vectorMap = NULL;
		}
		data = matrix.erase(data);		
		numNodes--;
		return(data);
	//  seems like i should be updating smallDist here, but the only time we remove cells is when
	//  clustering and the clustering algorithm updates smallDist
	}
	catch(exception& e) {
		m->errorOut(e, "SparseMatrix", "rmCell");
		exit(1);
	}
}

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

void SparseMatrix::addCell(PCell value){
	try {
		matrix.push_back(value);
		numNodes++;
		if(value.dist < smallDist){
			smallDist = value.dist;
		}
	}
	catch(exception& e) {
		m->errorOut(e, "SparseMatrix", "addCell");
		exit(1);
	}
}

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

void SparseMatrix::clear(){
	try {
		matrix.clear();
		mins.clear();
		numNodes = 0;
		minsIndex = 0;
		smallDist = 1e6;
	}
	catch(exception& e) {
		m->errorOut(e, "SparseMatrix", "clear");
		exit(1);
	}
}

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

MatData SparseMatrix::begin(){
	return matrix.begin();	
}

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

MatData SparseMatrix::end(){
	return matrix.end();	
}

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

void SparseMatrix::print(){
	try {
		int index = 0;
	
		cout << endl << "Index\tRow\tColumn\tDistance" << endl;
	
		for(MatData currentCell=matrix.begin();currentCell!=matrix.end();currentCell++){
			cout << index << '\t' << currentCell->row  << '\t' << currentCell->column << '\t' << currentCell->dist << endl;
			index++;
		}
	}
	catch(exception& e) {
		m->errorOut(e, "SparseMatrix", "print");
		exit(1);
	}
}

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

void SparseMatrix::print(ListVector* list){
	try {
		int index = 0;
	
		m->mothurOutEndLine(); m->mothurOut("Index\tRow\tColumn\tDistance"); m->mothurOutEndLine();
	
		for(MatData currentCell=matrix.begin();currentCell!=matrix.end();currentCell++){
			m->mothurOut(toString(index) + "\t" + toString(list->get(currentCell->row))  + "\t" + toString(list->get(currentCell->column)) + "\t" + toString(currentCell->dist)); m->mothurOutEndLine();
			index++;
		}
	}
	catch(exception& e) {
		m->errorOut(e, "SparseMatrix", "print");
		exit(1);
	}
}

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

PCell* SparseMatrix::getSmallestCell(){
	try {
	//	this is where I check to see if the next small distance has the correct distance
	//	if it doesn't then I remove the offending Cell -> should also be able to check for
	//	invalid iterator / pointer -- right???
	
		while(!mins.empty() && mins.back() == NULL){
			mins.pop_back();		
		}
	
	//	if the mins vector is empty go here...
		if(mins.empty()){		
			mins.clear();
	
			smallDist = begin()->dist;  //set the first candidate small distance
		
			for(MatData currentCell=begin();currentCell!=end();currentCell++){
			
				float dist = currentCell->dist;
			
				if(dist < smallDist){  //found a new smallest distance
					mins.clear();
					smallDist = dist;
					mins.push_back(&*currentCell);  //this is the address of the data in the list being pointed to by the MatData iterator
				}
				else if(dist == smallDist){  //if a subsequent distance is the same as mins distance add the new iterator to the mins vector
					mins.push_back(&*currentCell); //this is the address of the data in the list being pointed to by the MatData iterator
				}

			}
			random_shuffle(mins.begin(), mins.end());  //randomize the order of the iterators in the mins vector

			for(int i=0;i<mins.size();i++){
				mins[i]->vectorMap = &mins[i];  //assign vectorMap to the address for the container
			}
			
		}
	
		smallCell = mins.back();	//make the smallestCell the last element of the vector

		mins.pop_back();			//remove the last element from the vector

		return smallCell;
	}
	catch(exception& e) {
		m->errorOut(e, "SparseMatrix", "getSmallestCell");
		exit(1);
	}
}

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