]> git.donarmstrong.com Git - mothur.git/blob - sparsematrix.cpp
added logfile feature
[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                 errorOut(e, "SparseMatrix", "rmCell");
39                 exit(1);
40         }
41 }
42
43 /***********************************************************************/
44
45 void SparseMatrix::addCell(PCell value){
46         try {
47                 matrix.push_back(value);
48                 numNodes++;
49                 if(value.dist < smallDist){
50                         smallDist = value.dist;
51                 }
52         }
53         catch(exception& e) {
54                 errorOut(e, "SparseMatrix", "addCell");
55                 exit(1);
56         }
57 }
58
59 /***********************************************************************/
60
61 void SparseMatrix::clear(){
62         try {
63                 matrix.clear();
64                 mins.clear();
65                 numNodes = 0;
66                 minsIndex = 0;
67                 smallDist = 1e6;
68         }
69         catch(exception& e) {
70                 errorOut(e, "SparseMatrix", "clear");
71                 exit(1);
72         }
73 }
74
75 /***********************************************************************/
76
77 MatData SparseMatrix::begin(){
78         return matrix.begin();  
79 }
80
81 /***********************************************************************/
82
83 MatData SparseMatrix::end(){
84         return matrix.end();    
85 }
86
87 /***********************************************************************/
88
89 void SparseMatrix::print(){
90         try {
91                 int index = 0;
92                 
93                 mothurOutEndLine();
94                 mothurOut("Index\tRow\tColumn\tDistance");
95                 mothurOutEndLine();
96         
97                 for(MatData currentCell=matrix.begin();currentCell!=matrix.end();currentCell++){
98                         mothurOut(toString(index) + "\t" + toString(currentCell->row)  + "\t" + toString(currentCell->column) + "\t" + toString(currentCell->dist)); mothurOutEndLine();
99                         index++;
100                 }
101         }
102         catch(exception& e) {
103                 errorOut(e, "SparseMatrix", "print");
104                 exit(1);
105         }
106 }
107
108 /***********************************************************************/
109
110 void SparseMatrix::print(ListVector* list){
111         try {
112                 int index = 0;
113         
114                 mothurOutEndLine();
115                 mothurOut("Index\tRow\tColumn\tDistance");
116                 mothurOutEndLine();
117
118         
119                 for(MatData currentCell=matrix.begin();currentCell!=matrix.end();currentCell++){
120                         mothurOut(toString(index) + "\t" + toString(list->get(currentCell->row))  + "\t" + toString(list->get(currentCell->column)) + "\t" + toString(currentCell->dist)); mothurOutEndLine();
121                         index++;
122                 }
123         }
124         catch(exception& e) {
125                 errorOut(e, "SparseMatrix", "print");
126                 exit(1);
127         }
128 }
129
130 /***********************************************************************/
131
132 PCell* SparseMatrix::getSmallestCell(){
133         try {
134         //      this is where I check to see if the next small distance has the correct distance
135         //      if it doesn't then I remove the offending Cell -> should also be able to check for
136         //      invalid iterator / pointer -- right???
137         
138                 while(!mins.empty() && mins.back() == NULL){
139                         mins.pop_back();                
140                 }
141         
142         //      if the mins vector is empty go here...
143                 if(mins.empty()){               
144                         mins.clear();
145                 
146                         smallDist = begin()->dist;  //set the first candidate small distance
147                 
148                         for(MatData currentCell=begin();currentCell!=end();currentCell++){
149                         
150                                 float dist = currentCell->dist;
151                         
152                                 if(dist < smallDist){  //found a new smallest distance
153                                         mins.clear();
154                                         smallDist = dist;
155                                         mins.push_back(&*currentCell);  //this is the address of the data in the list being pointed to by the MatData iterator
156                                 }
157                                 else if(dist == smallDist){  //if a subsequent distance is the same as mins distance add the new iterator to the mins vector
158                                         mins.push_back(&*currentCell); //this is the address of the data in the list being pointed to by the MatData iterator
159                                 }
160
161                         }
162                         random_shuffle(mins.begin(), mins.end());  //randomize the order of the iterators in the mins vector
163
164                         for(int i=0;i<mins.size();i++){
165                                 mins[i]->vectorMap = &mins[i];  //assign vectorMap to the address for the container
166                         }
167                         
168                 }
169         
170                 smallCell = mins.back();        //make the smallestCell the last element of the vector
171
172                 mins.pop_back();                        //remove the last element from the vector
173
174                 return smallCell;
175         }
176         catch(exception& e) {
177                 errorOut(e, "SparseMatrix", "getSmallestCell");
178                 exit(1);
179         }
180 }
181
182 /***********************************************************************/
183