]> git.donarmstrong.com Git - mothur.git/blob - sparsematrix.cpp
6eaf3ec7d384786fb38ec98669c9db4612593000
[mothur.git] / sparsematrix.cpp
1 #include <vector>
2 #include <iostream>
3 #include <list>
4 #include "utilities.hpp"
5 #include "sparsematrix.hpp"
6 #include "listvector.hpp"
7 #include <exception>
8
9 using namespace std;
10
11 typedef list<PCell>::iterator MatData;
12
13 /***********************************************************************/
14
15 SparseMatrix::SparseMatrix() : numNodes(0), minsIndex(0), smallDist(1e6){};
16
17 /***********************************************************************/
18
19 int SparseMatrix::getNNodes(){
20         return numNodes; 
21 }
22
23 /***********************************************************************/
24
25 float SparseMatrix::getSmallDist(){
26         return smallDist;
27 }
28
29 /***********************************************************************/
30
31 void SparseMatrix::rmCell(MatData data){
32         try {
33                 if(data->vectorMap != NULL ){
34                         *(data->vectorMap) = NULL;
35                         data->vectorMap = NULL;
36                 }
37                 matrix.erase(data);             
38                 numNodes--;
39         
40         //  seems like i should be updating smallDist here, but the only time we remove cells is when
41         //  clustering and the clustering algorithm updates smallDist
42         }
43         catch(exception& e) {
44                 cout << "Standard Error: " << e.what() << " has occurred in the SparseMatrix class Function rmCell. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
45                 exit(1);
46         }
47         catch(...) {
48                 cout << "An unknown error has occurred in the SparseMatrix class function rmCell. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
49                 exit(1);
50         }       
51 }
52
53 /***********************************************************************/
54
55 void SparseMatrix::addCell(PCell value){
56         try {
57                 matrix.push_back(value);
58                 numNodes++;
59                 if(value.dist < smallDist){
60                         smallDist = value.dist;
61                 }
62         }
63         catch(exception& e) {
64                 cout << "Standard Error: " << e.what() << " has occurred in the SparseMatrix class Function addCell. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
65                 exit(1);
66         }
67         catch(...) {
68                 cout << "An unknown error has occurred in the SparseMatrix class function addCell. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
69                 exit(1);
70         }       
71 }
72
73 /***********************************************************************/
74
75 void SparseMatrix::clear(){
76         try {
77                 matrix.clear();
78                 mins.clear();
79                 numNodes = 0;
80                 minsIndex = 0;
81                 smallDist = 1e6;
82         }
83         catch(exception& e) {
84                 cout << "Standard Error: " << e.what() << " has occurred in the SparseMatrix class Function clear. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
85                 exit(1);
86         }
87         catch(...) {
88                 cout << "An unknown error has occurred in the SparseMatrix class function clear. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
89                 exit(1);
90         }       
91 }
92
93 /***********************************************************************/
94
95 MatData SparseMatrix::begin(){
96         return matrix.begin();  
97 }
98
99 /***********************************************************************/
100
101 MatData SparseMatrix::end(){
102         return matrix.end();    
103 }
104
105 /***********************************************************************/
106
107 void SparseMatrix::print(){
108         try {
109                 int index = 0;
110         
111                 cout << endl << "Index\tRow\tColumn\tDistance" << endl;
112         
113                 for(MatData currentCell=matrix.begin();currentCell!=matrix.end();currentCell++){
114                         cout << index << '\t' << currentCell->row  << '\t' << currentCell->column << '\t' << currentCell->dist << endl;
115                         index++;
116                 }
117         }
118         catch(exception& e) {
119                 cout << "Standard Error: " << e.what() << " has occurred in the SparseMatrix class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
120                 exit(1);
121         }
122         catch(...) {
123                 cout << "An unknown error has occurred in the SparseMatrix class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
124                 exit(1);
125         }       
126 }
127
128 /***********************************************************************/
129
130 void SparseMatrix::print(ListVector* list){
131         try {
132                 int index = 0;
133         
134                 cout << endl << "Index\tRow\tColumn\tDistance" << endl;
135         
136                 for(MatData currentCell=matrix.begin();currentCell!=matrix.end();currentCell++){
137                         cout << index << '\t' << list->get(currentCell->row)  << '\t' << list->get(currentCell->column) << '\t' << currentCell->dist << endl;
138                         index++;
139                 }
140         }
141         catch(exception& e) {
142                 cout << "Standard Error: " << e.what() << " has occurred in the SparseMatrix class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
143                 exit(1);
144         }
145         catch(...) {
146                 cout << "An unknown error has occurred in the SparseMatrix class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
147                 exit(1);
148         }       
149 }
150
151 /***********************************************************************/
152
153 PCell* SparseMatrix::getSmallestCell(){
154         try {
155         //      this is where I check to see if the next small distance has the correct distance
156         //      if it doesn't then I remove the offending Cell -> should also be able to check for
157         //      invalid iterator / pointer -- right???
158         
159                 while(!mins.empty() && mins.back() == NULL){
160                         mins.pop_back();                
161                 }
162         
163         //      if the mins vector is empty go here...
164                 if(mins.empty()){               
165                         mins.clear();
166                 
167                         smallDist = begin()->dist;  //set the first candidate small distance
168                 
169                         for(MatData currentCell=begin();currentCell!=end();currentCell++){
170                         
171                                 float dist = currentCell->dist;
172                         
173                                 if(dist < smallDist){  //found a new smallest distance
174                                         mins.clear();
175                                         smallDist = dist;
176                                         mins.push_back(&*currentCell);  //this is the address of the data in the list being pointed to by the MatData iterator
177                                 }
178                                 else if(dist == smallDist){  //if a subsequent distance is the same as mins distance add the new iterator to the mins vector
179                                         mins.push_back(&*currentCell); //this is the address of the data in the list being pointed to by the MatData iterator
180                                 }
181
182                         }
183                         random_shuffle(mins.begin(), mins.end());  //randomize the order of the iterators in the mins vector
184
185                         for(int i=0;i<mins.size();i++){
186                                 mins[i]->vectorMap = &mins[i];  //assign vectorMap to the address for the container
187                         }
188                         
189                 }
190         
191                 smallCell = mins.back();        //make the smallestCell the last element of the vector
192
193                 mins.pop_back();                        //remove the last element from the vector
194
195                 return smallCell;
196         }
197         catch(exception& e) {
198                 cout << "Standard Error: " << e.what() << " has occurred in the SparseMatrix class Function getSmallestCell. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
199                 exit(1);
200         }
201         catch(...) {
202                 cout << "An unknown error has occurred in the SparseMatrix class function getSmallestCell. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
203                 exit(1);
204         }       
205 }
206
207 /***********************************************************************/
208