forked from kuangmeng/DLPAlign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SparseMatrix.h
292 lines (243 loc) · 8.72 KB
/
SparseMatrix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/////////////////////////////////////////////////////////////////
// SparseMatrix.h
//
// Sparse matrix computations
/////////////////////////////////////////////////////////////////
#ifndef SPARSEMATRIX_H
#define SPARSEMATRIX_H
#include <iostream>
using namespace std;
const float POSTERIOR_CUTOFF = 0.01; // minimum posterior probability
// value that is maintained in the
// sparse matrix representation
typedef pair<int, float> PIF; // Sparse matrix entry type
// first --> column
// second --> value
/////////////////////////////////////////////////////////////////
// SparseMatrix
//
// Class for sparse matrix computations
/////////////////////////////////////////////////////////////////
class SparseMatrix
{
int seq1Length, seq2Length; // dimensions of matrix
VI rowSize; // rowSize[i] = # of cells in row i
SafeVector<PIF> data; // data values
SafeVector<SafeVector<PIF>::iterator> rowPtrs; // pointers to the beginning of each row
/////////////////////////////////////////////////////////////////
// SparseMatrix::SparseMatrix()
//
// Private constructor.
/////////////////////////////////////////////////////////////////
SparseMatrix()
{
}
public:
/////////////////////////////////////////////////////////////////
// SparseMatrix::SparseMatrix()
//
// Constructor. Builds a sparse matrix from a posterior matrix.
// Note that the expected format for the posterior matrix is as
// a (seq1Length+1) x (seq2Length+1) matrix where the 0th row
// and 0th column are ignored (they should contain all zeroes).
/////////////////////////////////////////////////////////////////
SparseMatrix(int seq1Length, int seq2Length, const VF &posterior) :
seq1Length(seq1Length), seq2Length(seq2Length)
{
int numCells = 0;
assert(seq1Length > 0);
assert(seq2Length > 0);
// calculate memory required; count the number of cells in the
// posterior matrix above the threshold
VF::const_iterator postPtr = posterior.begin();
for (int i = 0; i <= seq1Length; i++)
{
for (int j = 0; j <= seq2Length; j++)
{
if (*(postPtr++) >= POSTERIOR_CUTOFF)
{
assert(i != 0 && j != 0);
numCells++;
}
}
}
// allocate memory
data.resize(numCells);
rowSize.resize(seq1Length + 1);
rowSize[0] = -1;
rowPtrs.resize(seq1Length + 1);
rowPtrs[0] = data.end();
// build sparse matrix
postPtr = posterior.begin() + seq2Length + 1; // note that we're skipping the first row here
SafeVector<PIF>::iterator dataPtr = data.begin();
for (int i = 1; i <= seq1Length; i++)
{
postPtr++; // and skipping the first column of each row
rowPtrs[i] = dataPtr;
for (int j = 1; j <= seq2Length; j++)
{
if (*postPtr >= POSTERIOR_CUTOFF)
{
dataPtr->first = j;
dataPtr->second = *postPtr;
dataPtr++;
}
postPtr++;
}
rowSize[i] = dataPtr - rowPtrs[i];
}
}
/////////////////////////////////////////////////////////////////
// SparseMatrix::GetRowPtr()
//
// Returns the pointer to a particular row in the sparse matrix.
/////////////////////////////////////////////////////////////////
SafeVector<PIF>::iterator GetRowPtr(int row) const
{
assert(row >= 1 && row <= seq1Length);
return rowPtrs[row];
}
/////////////////////////////////////////////////////////////////
// SparseMatrix::GetValue()
//
// Returns value at a particular row, column.
/////////////////////////////////////////////////////////////////
float GetValue(int row, int col)
{
assert(row >= 1 && row <= seq1Length);
assert(col >= 1 && col <= seq2Length);
for (int i = 0; i < rowSize[row]; i++)
{
if (rowPtrs[row][i].first == col)
return rowPtrs[row][i].second;
}
return 0;
}
/////////////////////////////////////////////////////////////////
// SparseMatrix::GetRowSize()
//
// Returns the number of entries in a particular row.
/////////////////////////////////////////////////////////////////
int GetRowSize(int row) const
{
assert(row >= 1 && row <= seq1Length);
return rowSize[row];
}
/////////////////////////////////////////////////////////////////
// SparseMatrix::GetSeq1Length()
//
// Returns the first dimension of the matrix.
/////////////////////////////////////////////////////////////////
int GetSeq1Length() const
{
return seq1Length;
}
/////////////////////////////////////////////////////////////////
// SparseMatrix::GetSeq2Length()
//
// Returns the second dimension of the matrix.
/////////////////////////////////////////////////////////////////
int GetSeq2Length() const
{
return seq2Length;
}
/////////////////////////////////////////////////////////////////
// SparseMatrix::GetRowPtr
//
// Returns the pointer to a particular row in the sparse matrix.
/////////////////////////////////////////////////////////////////
int GetNumCells() const
{
return data.size();
}
/////////////////////////////////////////////////////////////////
// SparseMatrix::Print()
//
// Prints out a sparse matrix.
/////////////////////////////////////////////////////////////////
void Print(ostream &outfile) const
{
outfile << "Sparse Matrix:" << endl;
for (int i = 1; i <= seq1Length; i++)
{
outfile << " " << i << ":";
for (int j = 0; j < rowSize[i]; j++)
{
outfile << " (" << rowPtrs[i][j].first << ","
<< rowPtrs[i][j].second << ")";
}
outfile << endl;
}
}
/////////////////////////////////////////////////////////////////
// SparseMatrix::ComputeTranspose()
//
// Returns a new sparse matrix containing the transpose of the
// current matrix.
/////////////////////////////////////////////////////////////////
SparseMatrix *ComputeTranspose() const
{
// create a new sparse matrix
SparseMatrix *ret = new SparseMatrix();
int numCells = data.size();
ret->seq1Length = seq2Length;
ret->seq2Length = seq1Length;
// allocate memory
ret->data.resize(numCells);
ret->rowSize.resize(seq2Length + 1);
ret->rowSize[0] = -1;
ret->rowPtrs.resize(seq2Length + 1);
ret->rowPtrs[0] = ret->data.end();
// compute row sizes
for (int i = 1; i <= seq2Length; i++)
ret->rowSize[i] = 0;
for (int i = 0; i < numCells; i++)
ret->rowSize[data[i].first]++;
// compute row ptrs
for (int i = 1; i <= seq2Length; i++)
{
ret->rowPtrs[i] =
(i == 1) ?
ret->data.begin() :
ret->rowPtrs[i - 1] + ret->rowSize[i - 1];
}
// now fill in data
SafeVector<SafeVector<PIF>::iterator> currPtrs = ret->rowPtrs;
for (int i = 1; i <= seq1Length; i++)
{
SafeVector<PIF>::iterator row = rowPtrs[i];
for (int j = 0; j < rowSize[i]; j++)
{
currPtrs[row[j].first]->first = i;
currPtrs[row[j].first]->second = row[j].second;
currPtrs[row[j].first]++;
}
}
return ret;
}
/////////////////////////////////////////////////////////////////
// SparseMatrix::GetPosterior()
//
// Return the posterior representation of the sparse matrix.
/////////////////////////////////////////////////////////////////
VF *GetPosterior() const
{
// create a new posterior matrix
VF *posteriorPtr = new VF((seq1Length + 1) * (seq2Length + 1));
assert(posteriorPtr);
VF &posterior = *posteriorPtr;
// build the posterior matrix
for (int i = 0; i < (seq1Length + 1) * (seq2Length + 1); i++)
posterior[i] = 0;
for (int i = 1; i <= seq1Length; i++)
{
VF::iterator postPtr = posterior.begin() + i * (seq2Length + 1);
for (int j = 0; j < rowSize[i]; j++)
{
postPtr[rowPtrs[i][j].first] = rowPtrs[i][j].second;
}
}
return posteriorPtr;
}
};
#endif