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