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