]> git.donarmstrong.com Git - mothur.git/blob - sparsedistancematrix.cpp
Merge remote-tracking branch 'mothur/master'
[mothur.git] / sparsedistancematrix.cpp
1 //
2 //  sparsedistancematrix.cpp
3 //  Mothur
4 //
5 //  Created by Sarah Westcott on 7/16/12.
6 //  Copyright (c) 2012 Schloss Lab. All rights reserved.
7 //
8
9 #include "sparsedistancematrix.h"
10
11
12 /***********************************************************************/
13
14 SparseDistanceMatrix::SparseDistanceMatrix() : numNodes(0), smallDist(1e6){  m = MothurOut::getInstance(); sorted=false; aboveCutoff = 1e6; }
15
16 /***********************************************************************/
17
18 int SparseDistanceMatrix::getNNodes(){
19         return numNodes; 
20 }
21 /***********************************************************************/
22
23 void SparseDistanceMatrix::clear(){
24     for (int i = 0; i < seqVec.size(); i++) {  seqVec[i].clear();  }
25     seqVec.clear();
26 }
27
28 /***********************************************************************/
29
30 float SparseDistanceMatrix::getSmallDist(){
31         return smallDist;
32 }
33 /***********************************************************************/
34
35 int SparseDistanceMatrix::updateCellCompliment(ull row, ull col){
36     try {
37         
38         ull vrow = seqVec[row][col].index;
39         ull vcol = 0;
40         
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; }  }
43
44         seqVec[vrow][vcol].dist = seqVec[row][col].dist;
45         
46         return 0;
47     }
48         catch(exception& e) {
49                 m->errorOut(e, "SparseDistanceMatrix", "updateCellCompliment");
50                 exit(1);
51         }
52 }
53 /***********************************************************************/
54
55 int SparseDistanceMatrix::rmCell(ull row, ull col){
56         try {
57         numNodes-=2;
58  
59         ull vrow = seqVec[row][col].index;
60         ull vcol = 0;
61         
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; }  }
64         
65         seqVec[vrow].erase(seqVec[vrow].begin()+vcol);
66         seqVec[row].erase(seqVec[row].begin()+col);
67  
68                 return(0);
69     }
70         catch(exception& e) {
71                 m->errorOut(e, "SparseDistanceMatrix", "rmCell");
72                 exit(1);
73         }
74 }
75 /***********************************************************************/
76 void SparseDistanceMatrix::addCell(ull row, PDistCell cell){
77         try {
78                 numNodes+=2;
79                 if(cell.dist < smallDist){ smallDist = cell.dist; }
80         
81         seqVec[row].push_back(cell);
82         PDistCell temp(row, cell.dist);
83         seqVec[cell.index].push_back(temp);
84         }
85         catch(exception& e) {
86                 m->errorOut(e, "SparseDistanceMatrix", "addCell");
87                 exit(1);
88         }
89 }
90 /***********************************************************************/
91
92 ull SparseDistanceMatrix::getSmallestCell(ull& row){
93         try {
94         if (!sorted) { sortSeqVec(); sorted = true; }
95         
96         vector<PDistCellMin> mins;
97         smallDist = 1e6;
98        
99         for (int i = 0; i < seqVec.size(); i++) {
100             for (int j = 0; j < seqVec[i].size(); j++) {
101  
102                 //already checked everyone else in row
103                 if (i < seqVec[i][j].index) {  
104                             
105                     float dist = seqVec[i][j].dist;
106                   
107                     if(dist < smallDist){  //found a new smallest distance
108                         mins.clear();
109                         smallDist = dist;
110                         PDistCellMin temp(i, seqVec[i][j].index);
111                         mins.push_back(temp);  
112                     }
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); 
116                     }
117                 }else { j+=seqVec[i].size(); } //stop looking 
118                         }
119                 }
120         
121                 //random_shuffle(mins.begin(), mins.end());  //randomize the order of the iterators in the mins vector
122         
123         row = mins[0].row;
124         ull col = mins[0].col;
125
126                 return col;
127         }
128         catch(exception& e) {
129                 m->errorOut(e, "SparseDistanceMatrix", "getSmallestCell");
130                 exit(1);
131         }
132 }
133 /***********************************************************************/
134
135 int SparseDistanceMatrix::sortSeqVec(){
136         try {
137         
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); }
140     
141         return 0;
142     }
143         catch(exception& e) {
144                 m->errorOut(e, "SparseDistanceMatrix", "sortSeqVec");
145                 exit(1);
146         }
147 }
148 /***********************************************************************/
149