Skip to content

Commit 4530892

Browse files
committed
Matrix
1 parent 63b774d commit 4530892

File tree

6 files changed

+169
-33
lines changed

6 files changed

+169
-33
lines changed

General/General.vcxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,15 @@
116116
</ItemDefinitionGroup>
117117
<ItemGroup>
118118
<ClInclude Include="Knapsack.h" />
119+
<ClInclude Include="Matrix.h" />
119120
<ClInclude Include="MaxWIS.h" />
120121
<ClInclude Include="SequenceAlignment.h" />
121122
</ItemGroup>
122123
<ItemGroup>
123124
<ClCompile Include="Knapsack.cpp" />
125+
<ClInclude Include="Matrix.hpp">
126+
<FileType>CppCode</FileType>
127+
</ClInclude>
124128
<ClCompile Include="MaxWIS.cpp" />
125129
<ClCompile Include="SequenceAlignment.cpp" />
126130
</ItemGroup>

General/General.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
<ClInclude Include="SequenceAlignment.h">
2525
<Filter>Header Files</Filter>
2626
</ClInclude>
27+
<ClInclude Include="Matrix.h">
28+
<Filter>Header Files</Filter>
29+
</ClInclude>
2730
</ItemGroup>
2831
<ItemGroup>
2932
<ClCompile Include="MaxWIS.cpp">
@@ -35,5 +38,8 @@
3538
<ClCompile Include="SequenceAlignment.cpp">
3639
<Filter>Source Files</Filter>
3740
</ClCompile>
41+
<ClCompile Include="Matrix.hpp">
42+
<Filter>Header Files</Filter>
43+
</ClCompile>
3844
</ItemGroup>
3945
</Project>

General/Matrix.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef MATRIX_H
2+
#define MATRIX_H
3+
4+
#include <memory>
5+
#include <cstdint>
6+
#include <exception>
7+
#include <algorithm>
8+
9+
template<typename T>
10+
class Matrix
11+
{
12+
public:
13+
typedef T ValueType;
14+
private:
15+
uint64_t m_rowsCount;
16+
uint64_t m_colsCount;
17+
18+
std::unique_ptr<ValueType> m_matrix;
19+
20+
public:
21+
Matrix();
22+
Matrix(uint64_t rowsCount, uint64_t colsCount) throw (std::bad_alloc);
23+
Matrix(const Matrix<ValueType>& matrix) throw (std::bad_alloc);
24+
Matrix(Matrix<ValueType>&& matrix);
25+
~Matrix();
26+
27+
ValueType* operator[] (uint64_t i);
28+
29+
void reset();
30+
void resize(uint64_t rowsCount, uint64_t colsCount) throw (std::bad_alloc);
31+
32+
const uint64_t getRowsCount() const;
33+
const uint64_t getColsCount() const;
34+
};
35+
36+
template<typename T>
37+
inline const uint64_t Matrix<T>::getRowsCount() const
38+
{
39+
return m_rowsCount;
40+
}
41+
42+
template<typename T>
43+
inline const uint64_t Matrix<T>::getColsCount() const
44+
{
45+
return m_colsCount;
46+
}
47+
48+
#include "Matrix.hpp"
49+
50+
#endif

General/Matrix.hpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
template<typename T>
2+
Matrix<T>::Matrix():
3+
m_rowsCount(0),
4+
m_colsCount(0),
5+
m_matrix()
6+
{
7+
}
8+
9+
template<typename T>
10+
Matrix<T>::Matrix(uint64_t rowsCount, uint64_t colsCount) throw (std::bad_alloc) :
11+
m_rowsCount(rowsCount),
12+
m_colsCount(colsCount),
13+
m_matrix()
14+
{
15+
m_matrix.reset(new ValueType[m_rowsCount * m_colsCount]);
16+
}
17+
18+
template<typename T>
19+
Matrix<T>::Matrix(const Matrix<T>& matrix) throw (std::bad_alloc) :
20+
m_rowsCount(matrix.m_rowsCount),
21+
m_colsCount(matrix.m_colsCount),
22+
m_matrix()
23+
{
24+
m_matrix.reset(new ValueType[m_rowsCount * m_colsCount]);
25+
memcpy(m_matrix.get(), matrix.m_matrix.get(), sizeof(ValueType) * m_rowsCount * m_colsCount);
26+
}
27+
28+
template<typename T>
29+
Matrix<T>::Matrix(Matrix&& matrix):
30+
m_rowsCount(matrix.m_rowsCount),
31+
m_colsCount(matrix.m_colsCount)
32+
{
33+
m_matrix = std::move(matrix.m_matrix);
34+
matrix.m_rowsCount = 0;
35+
matrix.m_colsCount = 0;
36+
}
37+
38+
template<typename T>
39+
Matrix<T>::~Matrix()
40+
{
41+
m_matrix.reset();
42+
}
43+
44+
template<typename T>
45+
T* Matrix<T>::operator[] (uint64_t i)
46+
{
47+
return &m_matrix.get()[i * m_colsCount];
48+
}
49+
50+
template<typename T>
51+
void Matrix<T>::reset()
52+
{
53+
m_matrix.reset();
54+
m_rowsCount = 0;
55+
m_colsCount = 0;
56+
}
57+
58+
template<typename T>
59+
void Matrix<T>::resize(uint64_t rowsCount, uint64_t colsCount) throw (std::bad_alloc)
60+
{
61+
std::unique_ptr<ValueType> matrix(new ValueType[rowsCount * colsCount]);
62+
uint64_t minColsCount = std::min(m_colsCount, colsCount);
63+
uint64_t minRowsCount = std::min(m_rowsCount, rowsCount);
64+
for (uint64_t i = 0; i < minRowsCount; ++i)
65+
{
66+
memcpy(&matrix.get()[i * colsCount], &m_matrix.get()[i * m_colsCount], sizeof(ValueType) * minColsCount);
67+
}
68+
m_matrix.reset();
69+
m_rowsCount = rowsCount;
70+
m_colsCount = colsCount;
71+
m_matrix = std::move(matrix);
72+
}

General/SequenceAlignment.cpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void SequenceAlignment::clear()
1717
m_charToIndexMap.clear();
1818
m_numScores = 0;
1919
m_gapScore = 0;
20-
m_scores.clear();
20+
m_scores.reset();
2121
}
2222

2323
bool SequenceAlignment::read(std::istream& in)
@@ -28,20 +28,20 @@ bool SequenceAlignment::read(std::istream& in)
2828
std::string alphabet;
2929
in >> m_alphabet;
3030

31-
uint32_t idx = 0;
31+
uint64_t idx = 0;
3232
for (auto c : m_alphabet)
3333
{
3434
m_charToIndexMap.insert(std::make_pair(c, idx++));
3535
}
3636

3737
m_numScores = m_alphabet.size();
38-
m_scores.resize(m_numScores * m_numScores, 0);
38+
m_scores.resize(m_numScores, m_numScores);
3939

40-
for (uint32_t i = 0; i < m_numScores; ++i)
40+
for (uint64_t i = 0; i < m_numScores; ++i)
4141
{
42-
for (uint32_t j = 0; j < m_numScores; ++j)
42+
for (uint64_t j = 0; j < m_numScores; ++j)
4343
{
44-
in >> m_scores[i * m_numScores + j];
44+
in >> m_scores[i][j];
4545
}
4646
}
4747
in >> m_gapScore;
@@ -55,53 +55,53 @@ bool SequenceAlignment::run(
5555
uint32_t &scoreNeedlemanWunsch,
5656
std::string(&seq)[2])
5757
{
58-
uint32_t size[2] = { m_sequnces[0].size() + 1, m_sequnces[1].size() + 1 };
59-
std::vector<char> nwScore(size[0] * size[1], 0);
60-
for (uint32_t i = 0; i < size[1]; ++i)
58+
uint64_t size[2] = { m_sequnces[0].size() + 1, m_sequnces[1].size() + 1 };
59+
Matrix<uint32_t> nwScore(size[0], size[1]);
60+
for (uint64_t i = 0; i < size[0]; ++i)
6161
{
62-
nwScore[i] = (i + 1) * m_gapScore;
62+
nwScore[i][0] = (i + 1) * m_gapScore;
6363
}
64-
for (uint32_t i = 0; i < size[0]; ++i)
64+
for (uint64_t i = 0; i < size[1]; ++i)
6565
{
66-
nwScore[i * size[1]] = (i + 1) * m_gapScore;
66+
nwScore[0][i] = (i + 1) * m_gapScore;
6767
}
6868

6969
uint32_t tmpScore[3];
70-
for (uint32_t i = 1; i < size[0]; ++i)
70+
for (uint64_t i = 1; i < size[0]; ++i)
7171
{
72-
for (uint32_t j = 1; j < size[1]; ++j)
72+
for (uint64_t j = 1; j < size[1]; ++j)
7373
{
74-
uint32_t idx1 = m_charToIndexMap[m_sequnces[0][i - 1]];
75-
uint32_t idx2 = m_charToIndexMap[m_sequnces[1][j - 1]];
76-
tmpScore[0] = nwScore[(i - 1) * size[1] + (j - 1)] + m_scores[idx1 * m_numScores + idx2];
77-
tmpScore[1] = nwScore[i * size[1] + (j - 1)] + m_gapScore;
78-
tmpScore[2] = nwScore[(i - 1) * size[1] + j] + m_gapScore;
79-
nwScore[i * size[1] + j] = std::min(tmpScore[0], std::min(tmpScore[1], tmpScore[2]));
74+
uint64_t idx1 = m_charToIndexMap[m_sequnces[0][i - 1]];
75+
uint64_t idx2 = m_charToIndexMap[m_sequnces[1][j - 1]];
76+
tmpScore[0] = nwScore[i - 1][j - 1] + m_scores[idx1][idx2];
77+
tmpScore[1] = nwScore[i][j - 1] + m_gapScore;
78+
tmpScore[2] = nwScore[i - 1][j] + m_gapScore;
79+
nwScore[i][j] = std::min(tmpScore[0], std::min(tmpScore[1], tmpScore[2]));
8080
}
8181
}
8282

83-
scoreNeedlemanWunsch = nwScore[size[0] * size[1] - 1];
83+
scoreNeedlemanWunsch = nwScore[size[0] - 1][size[1] - 1];
8484

85-
for (uint32_t i = size[0] - 1, j = size[1] - 1; (i > 0) && (j > 0);)
85+
for (uint64_t i = size[0] - 1, j = size[1] - 1; (i > 0) && (j > 0);)
8686
{
87-
uint32_t idx1 = m_charToIndexMap[m_sequnces[0][i - 1]];
88-
uint32_t idx2 = m_charToIndexMap[m_sequnces[1][j - 1]];
89-
tmpScore[0] = nwScore[(i - 1) * size[1] + (j - 1)] + m_scores[idx1 * m_numScores + idx2];
90-
tmpScore[1] = nwScore[i * size[1] + (j - 1)] + m_gapScore;
91-
tmpScore[2] = nwScore[(i - 1) * size[1] + j] + m_gapScore;
92-
if (nwScore[i * size[1] + j] == tmpScore[0])
87+
uint64_t idx1 = m_charToIndexMap[m_sequnces[0][i - 1]];
88+
uint64_t idx2 = m_charToIndexMap[m_sequnces[1][j - 1]];
89+
tmpScore[0] = nwScore[i - 1][j - 1] + m_scores[idx1][idx2];
90+
tmpScore[1] = nwScore[i][j - 1] + m_gapScore;
91+
tmpScore[2] = nwScore[i - 1][j] + m_gapScore;
92+
if (nwScore[i][j] == tmpScore[0])
9393
{
9494
seq[0].push_back(m_sequnces[0][i - 1]);
9595
seq[1].push_back(m_sequnces[1][j - 1]);
9696
--i, --j;
9797
}
98-
else if (nwScore[i * size[1] + j] == tmpScore[1])
98+
else if (nwScore[i][j] == tmpScore[1])
9999
{
100100
seq[0].push_back(' ');
101101
seq[1].push_back(m_sequnces[1][j - 1]);
102102
--j;
103103
}
104-
else if (nwScore[i * size[1] + j] == tmpScore[2])
104+
else if (nwScore[i][j] == tmpScore[2])
105105
{
106106
seq[0].push_back(m_sequnces[0][i - 1]);
107107
seq[1].push_back(' ');

General/SequenceAlignment.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
#include <string>
77
#include <cstdint>
88

9+
#include "Matrix.h"
10+
911
class SequenceAlignment
1012
{
1113
private:
12-
typedef std::unordered_map<char, uint32_t> CharToIndexMap;
14+
typedef std::unordered_map<char, uint64_t> CharToIndexMap;
1315
CharToIndexMap m_charToIndexMap;
1416
std::string m_alphabet;
15-
uint32_t m_numScores;
16-
std::vector<uint32_t> m_scores;
17+
uint64_t m_numScores;
18+
//std::vector<uint32_t> m_scores;
19+
Matrix<uint32_t> m_scores;
20+
1721
uint32_t m_gapScore;
1822
std::string m_sequnces[2];
1923

0 commit comments

Comments
 (0)