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++) { if (seqVec[vrow][i].index == row) { vcol = i; break; } }
44 seqVec[vrow][vcol].dist = seqVec[row][col].dist;
49 m->errorOut(e, "SparseDistanceMatrix", "updateCellCompliment");
53 /***********************************************************************/
55 int SparseDistanceMatrix::rmCell(ull row, ull col){
59 ull vrow = seqVec[row][col].index;
62 //find the columns entry for this cell as well
63 for (int i = 0; i < seqVec[vrow].size(); i++) { if (seqVec[vrow][i].index == row) { vcol = i; break; } }
65 seqVec[vrow].erase(seqVec[vrow].begin()+vcol);
66 seqVec[row].erase(seqVec[row].begin()+col);
71 m->errorOut(e, "SparseDistanceMatrix", "rmCell");
75 /***********************************************************************/
76 void SparseDistanceMatrix::addCell(ull row, PDistCell cell){
79 if(cell.dist < smallDist){ smallDist = cell.dist; }
81 seqVec[row].push_back(cell);
82 PDistCell temp(row, cell.dist);
83 seqVec[cell.index].push_back(temp);
86 m->errorOut(e, "SparseDistanceMatrix", "addCell");
90 /***********************************************************************/
92 ull SparseDistanceMatrix::getSmallestCell(ull& row){
94 if (!sorted) { sortSeqVec(); sorted = true; }
96 vector<PDistCellMin> mins;
99 for (int i = 0; i < seqVec.size(); i++) {
100 for (int j = 0; j < seqVec[i].size(); j++) {
102 //already checked everyone else in row
103 if (i < seqVec[i][j].index) {
105 float dist = seqVec[i][j].dist;
107 if(dist < smallDist){ //found a new smallest distance
110 PDistCellMin temp(i, seqVec[i][j].index);
111 mins.push_back(temp);
113 else if(dist == smallDist){ //if a subsequent distance is the same as mins distance add the new iterator to the mins vector
114 PDistCellMin temp(i, seqVec[i][j].index);
115 mins.push_back(temp);
117 }else { j+=seqVec[i].size(); } //stop looking
121 //random_shuffle(mins.begin(), mins.end()); //randomize the order of the iterators in the mins vector
124 ull col = mins[0].col;
128 catch(exception& e) {
129 m->errorOut(e, "SparseMatrix", "getSmallestCell");
133 /***********************************************************************/
135 int SparseDistanceMatrix::sortSeqVec(){
138 //saves time in getSmallestCell, by making it so you dont search the repeats
139 for (int i = 0; i < seqVec.size(); i++) { sort(seqVec[i].begin(), seqVec[i].end(), compareIndexes); }
143 catch(exception& e) {
144 m->errorOut(e, "SparseMatrix", "getSmallestCell");
148 /***********************************************************************/