2 // sparsedistancematrix.cpp
5 // Created by Sarah Westcott on 7/16/12.
6 // Copyright (c) 2012 Schloss Lab. All rights reserved.
9 #include "sparsedistancematrix.h"
12 /***********************************************************************/
14 SparseDistanceMatrix::SparseDistanceMatrix() : numNodes(0), smallDist(1e6){ m = MothurOut::getInstance(); sorted=false; aboveCutoff = 1e6; }
16 /***********************************************************************/
18 int SparseDistanceMatrix::getNNodes(){
21 /***********************************************************************/
23 void SparseDistanceMatrix::clear(){
24 for (int i = 0; i < seqVec.size(); i++) { seqVec[i].clear(); }
28 /***********************************************************************/
30 float SparseDistanceMatrix::getSmallDist(){
33 /***********************************************************************/
35 int SparseDistanceMatrix::updateCellCompliment(ull row, ull col){
38 ull vrow = seqVec[row][col].index;
41 //find the columns entry for this cell as well
42 for (int i = 0; i < seqVec[vrow].size(); i++) {
43 if (seqVec[vrow][i].index == row) { vcol = i; break; }
46 seqVec[vrow][vcol].dist = seqVec[row][col].dist;
51 m->errorOut(e, "SparseDistanceMatrix", "updateCellCompliment");
55 /***********************************************************************/
57 int SparseDistanceMatrix::rmCell(ull row, ull col){
61 ull vrow = seqVec[row][col].index;
64 //find the columns entry for this cell as well
65 for (int i = 0; i < seqVec[vrow].size(); i++) { if (seqVec[vrow][i].index == row) { vcol = i; break; } }
67 seqVec[vrow].erase(seqVec[vrow].begin()+vcol);
68 seqVec[row].erase(seqVec[row].begin()+col);
73 m->errorOut(e, "SparseDistanceMatrix", "rmCell");
77 /***********************************************************************/
78 void SparseDistanceMatrix::addCell(ull row, PDistCell cell){
81 if(cell.dist < smallDist){ smallDist = cell.dist; }
83 seqVec[row].push_back(cell);
84 PDistCell temp(row, cell.dist);
85 seqVec[cell.index].push_back(temp);
88 m->errorOut(e, "SparseDistanceMatrix", "addCell");
92 /***********************************************************************/
93 int SparseDistanceMatrix::addCellSorted(ull row, PDistCell cell){
96 if(cell.dist < smallDist){ smallDist = cell.dist; }
98 seqVec[row].push_back(cell);
99 PDistCell temp(row, cell.dist);
100 seqVec[cell.index].push_back(temp);
103 sortSeqVec(cell.index);
105 int location = -1; //find location of new cell when sorted
106 for (int i = 0; i < seqVec[row].size(); i++) { if (seqVec[row][i].index == cell.index) { location = i; break; } }
110 catch(exception& e) {
111 m->errorOut(e, "SparseDistanceMatrix", "addCellSorted");
116 /***********************************************************************/
118 ull SparseDistanceMatrix::getSmallestCell(ull& row){
120 if (!sorted) { sortSeqVec(); sorted = true; }
122 vector<PDistCellMin> mins;
125 for (int i = 0; i < seqVec.size(); i++) {
126 for (int j = 0; j < seqVec[i].size(); j++) {
128 if (m->control_pressed) { return smallDist; }
130 //already checked everyone else in row
131 if (i < seqVec[i][j].index) {
133 float dist = seqVec[i][j].dist;
135 if(dist < smallDist){ //found a new smallest distance
138 PDistCellMin temp(i, seqVec[i][j].index);
139 mins.push_back(temp);
141 else if(dist == smallDist){ //if a subsequent distance is the same as mins distance add the new iterator to the mins vector
142 PDistCellMin temp(i, seqVec[i][j].index);
143 mins.push_back(temp);
145 }else { j+=seqVec[i].size(); } //stop looking
149 //random_shuffle(mins.begin(), mins.end()); //randomize the order of the iterators in the mins vector
152 ull col = mins[0].col;
156 catch(exception& e) {
157 m->errorOut(e, "SparseDistanceMatrix", "getSmallestCell");
161 /***********************************************************************/
163 int SparseDistanceMatrix::sortSeqVec(){
166 //saves time in getSmallestCell, by making it so you dont search the repeats
167 for (int i = 0; i < seqVec.size(); i++) { sort(seqVec[i].begin(), seqVec[i].end(), compareIndexes); }
171 catch(exception& e) {
172 m->errorOut(e, "SparseDistanceMatrix", "sortSeqVec");
176 /***********************************************************************/
178 int SparseDistanceMatrix::sortSeqVec(int index){
181 //saves time in getSmallestCell, by making it so you dont search the repeats
182 sort(seqVec[index].begin(), seqVec[index].end(), compareIndexes);
186 catch(exception& e) {
187 m->errorOut(e, "SparseDistanceMatrix", "sortSeqVec");
191 /***********************************************************************/