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