-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBandedAlignmentMatrix.H
163 lines (128 loc) · 3.19 KB
/
BandedAlignmentMatrix.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
/***********************************************************************
BandedAlignmentMatrix.H
BOOM : Bioinformatics Object Oriented Modules
Copyright (C)2012 William H. Majoros ([email protected]).
This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
License (GPL) version 3, as described at www.opensource.org.
***********************************************************************/
#ifndef INCL_BandedAlignmentMatrix_H
#define INCL_BandedAlignmentMatrix_H
#include "Array1D.H"
namespace BOOM {
template<class T>
class BandedAlignmentMatrix
{
Array1D<T> matrix;
Array1D<int> lowestY, highestY;
Array1D<int> xToOffset;
Array1D<T*> xToCell;
int xDim, yDim, bandWidth;
void initMatrix();
public:
BandedAlignmentMatrix(int xDim=0,int yDim=0,int bandWidth=0);
int getFirstDim() const; // xDim
int getSecondDim() const;// yDim
bool getCell(int x,int y,T*&); // returns false if not present in matrix
int getMatrixSize() const; // in bytes
void resize(int xDim,int yDim,int bandWidth);
int numAllocatedCells();
T &getIthCell(int i);
static int recommendBandWidth(int maxBytes,int xDim);
};
}
using namespace BOOM;
template<class T>
BandedAlignmentMatrix<T>::BandedAlignmentMatrix(int xDim,int yDim,
int bandWidth)
: xDim(xDim),
yDim(yDim),
bandWidth(bandWidth),
highestY(xDim),
lowestY(xDim),
xToOffset(xDim),
xToCell(xDim)
{
// ctor
initMatrix();
}
template<class T>
void BandedAlignmentMatrix<T>::resize(int xDim,int yDim,int bandWidth)
{
this->xDim=xDim;
this->yDim=yDim;
this->bandWidth=bandWidth;
highestY.resize(xDim);
lowestY.resize(xDim);
xToOffset.resize(xDim);
xToCell.resize(xDim);
initMatrix();
}
template<class T>
int BandedAlignmentMatrix<T>::getFirstDim() const
{
return xDim;
}
template<class T>
int BandedAlignmentMatrix<T>::getSecondDim() const
{
return yDim;
}
template<class T>
void BandedAlignmentMatrix<T>::initMatrix()
{
if(xDim==0 || yDim==0 || bandWidth==0) {
matrix.resize(0);
return;
}
int nextOffset=0;
float m=yDim/float(xDim);
if(bandWidth<m) bandWidth=int(m+1);
for(int x=0 ; x<xDim ; ++x)
{
int y=int(m*x);
int beginY=y-bandWidth;
int endY=y+bandWidth;
if(beginY<0) beginY=0;
if(endY>=yDim) endY=yDim;
int columnHeight=endY-beginY+1;
highestY[x]=endY;
lowestY[x]=beginY;
xToOffset[x]=nextOffset;
nextOffset+=columnHeight;
}
matrix.resize(nextOffset);
for(int x=0 ; x<xDim ; ++x)
xToCell[x]=&matrix[xToOffset[x]];
}
template<class T>
bool BandedAlignmentMatrix<T>::getCell(int x,int y,T *&cellPtr)
{
int beginY=lowestY[x];
if(y<beginY) return false;
int endY=highestY[x];
if(y>endY) return false;
cellPtr=xToCell[x]+(y-beginY);
return true;
}
template<class T>
int BandedAlignmentMatrix<T>::getMatrixSize() const
{
return matrix.size()*sizeof(T);
}
template<class T>
int BandedAlignmentMatrix<T>::recommendBandWidth(int maxBytes,
int xDim)
{
return maxBytes/(2*xDim*sizeof(T));
}
template<class T>
int BandedAlignmentMatrix<T>::numAllocatedCells()
{
return matrix.size();
}
template<class T>
T &BandedAlignmentMatrix<T>::getIthCell(int i)
{
return matrix[i];
}
#endif