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 /***********************************************************************/
94 ull SparseDistanceMatrix::getSmallestCell(ull& row){
96 if (!sorted) { sortSeqVec(); sorted = true; }
98 vector<PDistCellMin> mins;
101 for (int i = 0; i < seqVec.size(); i++) {
102 for (int j = 0; j < seqVec[i].size(); j++) {
104 if (m->control_pressed) { return smallDist; }
106 //already checked everyone else in row
107 if (i < seqVec[i][j].index) {
109 float dist = seqVec[i][j].dist;
111 if(dist < smallDist){ //found a new smallest distance
114 PDistCellMin temp(i, seqVec[i][j].index);
115 mins.push_back(temp);
117 else if(dist == smallDist){ //if a subsequent distance is the same as mins distance add the new iterator to the mins vector
118 PDistCellMin temp(i, seqVec[i][j].index);
119 mins.push_back(temp);
121 }else { j+=seqVec[i].size(); } //stop looking
125 //random_shuffle(mins.begin(), mins.end()); //randomize the order of the iterators in the mins vector
128 ull col = mins[0].col;
132 catch(exception& e) {
133 m->errorOut(e, "SparseDistanceMatrix", "getSmallestCell");
137 /***********************************************************************/
139 int SparseDistanceMatrix::sortSeqVec(){
142 //saves time in getSmallestCell, by making it so you dont search the repeats
143 for (int i = 0; i < seqVec.size(); i++) { sort(seqVec[i].begin(), seqVec[i].end(), compareIndexes); }
147 catch(exception& e) {
148 m->errorOut(e, "SparseDistanceMatrix", "sortSeqVec");
152 /***********************************************************************/