]> git.donarmstrong.com Git - mothur.git/blob - sparsedistancematrix.cpp
sffinfo bug with flow grams right index when clipQualRight=0
[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++) {  
43             if (seqVec[vrow][i].index == row) { vcol = i;  break; }  
44         }
45        
46         seqVec[vrow][vcol].dist = seqVec[row][col].dist;
47         
48         return 0;
49     }
50         catch(exception& e) {
51                 m->errorOut(e, "SparseDistanceMatrix", "updateCellCompliment");
52                 exit(1);
53         }
54 }
55 /***********************************************************************/
56
57 int SparseDistanceMatrix::rmCell(ull row, ull col){
58         try {
59         numNodes-=2;
60  
61         ull vrow = seqVec[row][col].index;
62         ull vcol = 0;
63         
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; }  }
66         
67         seqVec[vrow].erase(seqVec[vrow].begin()+vcol);
68         seqVec[row].erase(seqVec[row].begin()+col);
69  
70                 return(0);
71     }
72         catch(exception& e) {
73                 m->errorOut(e, "SparseDistanceMatrix", "rmCell");
74                 exit(1);
75         }
76 }
77 /***********************************************************************/
78 void SparseDistanceMatrix::addCell(ull row, PDistCell cell){
79         try {
80                 numNodes+=2;
81                 if(cell.dist < smallDist){ smallDist = cell.dist; }
82         
83         seqVec[row].push_back(cell);
84         PDistCell temp(row, cell.dist);
85         seqVec[cell.index].push_back(temp);
86         }
87         catch(exception& e) {
88                 m->errorOut(e, "SparseDistanceMatrix", "addCell");
89                 exit(1);
90         }
91 }
92 /***********************************************************************/
93
94 ull SparseDistanceMatrix::getSmallestCell(ull& row){
95         try {
96         if (!sorted) { sortSeqVec(); sorted = true; }
97         
98         vector<PDistCellMin> mins;
99         smallDist = 1e6;
100        
101         for (int i = 0; i < seqVec.size(); i++) {
102             for (int j = 0; j < seqVec[i].size(); j++) {
103                 
104                 if (m->control_pressed) { return smallDist; }
105                 
106                 //already checked everyone else in row
107                 if (i < seqVec[i][j].index) {  
108                             
109                     float dist = seqVec[i][j].dist;
110                   
111                     if(dist < smallDist){  //found a new smallest distance
112                         mins.clear();
113                         smallDist = dist;
114                         PDistCellMin temp(i, seqVec[i][j].index);
115                         mins.push_back(temp);  
116                     }
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); 
120                     }
121                 }else { j+=seqVec[i].size(); } //stop looking 
122                         }
123                 }
124         
125                 //random_shuffle(mins.begin(), mins.end());  //randomize the order of the iterators in the mins vector
126         
127         row = mins[0].row;
128         ull col = mins[0].col;
129
130                 return col;
131         }
132         catch(exception& e) {
133                 m->errorOut(e, "SparseDistanceMatrix", "getSmallestCell");
134                 exit(1);
135         }
136 }
137 /***********************************************************************/
138
139 int SparseDistanceMatrix::sortSeqVec(){
140         try {
141         
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); }
144     
145         return 0;
146     }
147         catch(exception& e) {
148                 m->errorOut(e, "SparseDistanceMatrix", "sortSeqVec");
149                 exit(1);
150         }
151 }
152 /***********************************************************************/
153