Skip to content

Commit 33db8e4

Browse files
committed
I hate computer science
1 parent 2c40d01 commit 33db8e4

File tree

12 files changed

+253
-41
lines changed

12 files changed

+253
-41
lines changed

bin/app

19.3 KB
Binary file not shown.

input/base-sample.fasta

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
>s1
2-
ACATGCTACACGTATCCGATACCCCGTAACCGATAACGATACACAGACCTCGTACGCTTGCTACAACGTACTCTATAACCGAGAACGATTGACATGCCTCGTACACATGCTACACGTACTCCGAT
3-
4-
>s2
5-
ACATGCGACACTACTCCGATACCCCGTAACCGATAACGATACAGAGACCTCGTACGCTTGCTAATAACCGAGAACGATTGACATTCCTCGTACAGCTACACGTACTCCGAT
1+
>s1
2+
ACATGCTACACGTATCCGATACCCCGTAACCGATAACGATACACAGACCTCGTACGCTTG
3+
CTACAACGTACTCTATAACCGAGAACGATTGACATGCCTCGTACACATGCTACACGTACT
4+
CCGAT
5+
>s2
6+
ACATGCGACACTACTCCGATACCCCGTAACCGATAACGATACAGAGACCTCGTACGCTTG
7+
CTAATAACCGAGAACGATTGACATTCCTCGTACAGCTACACGTACT
8+
CCGAT

obj/alignment/alignment.o

30.7 KB
Binary file not shown.

obj/alignment/de_cell.o

10.4 KB
Binary file not shown.

obj/io/io_utils.o

48 Bytes
Binary file not shown.

obj/main.o

304 Bytes
Binary file not shown.

output.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{(s_score-[55], d_score-[40], i_score[40]), (s_match-[105], d_match[104], i_match[104])}

src/alignment/alignment.cpp

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,48 @@
11
#include "./alignment.hpp"
22

33
namespace alignment {
4-
int64_t execute_global_alignment_bad(const std::string& string_one, const std::string& string_two) {
5-
if (string_one.empty() || string_two.empty()) {
6-
return 0;
4+
int64_t execute_global_alignment(const std::string& string_one, const std::string& string_two) {
5+
const size_t row_size = string_one.size() + 1;
6+
const size_t col_size = string_two.size() + 1;
7+
std::vector<dp_cell> cur_dp_row(col_size, dp_cell());
8+
std::vector<dp_cell> prev_dp_row(col_size, dp_cell());
9+
10+
// temp
11+
std::ofstream s("./output.txt");
12+
13+
// prev_dp_row[0].print_scores(s);
14+
// s << " ";
15+
16+
for (size_t i = 1; i < col_size; ++i) {
17+
prev_dp_row[i].set_top_row(i);
18+
// prev_dp_row[i].print_scores(s);
19+
// s << " ";
720
}
8-
size_t max_row = string_one.size();
9-
size_t max_col = string_one.size();
10-
std::vector<std::vector<dp_cell>> dp_table(max_row, std::vector<dp_cell>(max_col, dp_cell()));
1121

12-
for (size_t i = 1; i < max_row; ++i) {
13-
for (size_t j = 1; j < max_col; ++i) {
22+
for (size_t row_idx = 1; row_idx < row_size; ++row_idx) {
23+
cur_dp_row[0].set_left_col(row_idx);
24+
// cur_dp_row[0].print_scores(s);
25+
// s << " ";
26+
for (size_t col_idx = 1; col_idx < col_size; ++col_idx) {
27+
cur_dp_row[col_idx].score_cell(
28+
prev_dp_row[col_idx-1],
29+
prev_dp_row[col_idx],
30+
cur_dp_row[col_idx-1],
31+
string_one[row_idx-1],
32+
string_two[col_idx-1]
33+
);
34+
// cur_dp_row[col_idx].print_scores(s);
35+
// s << " ";
1436
}
37+
// s << std::endl;
38+
std::swap(prev_dp_row, cur_dp_row);
1539
}
40+
41+
s << prev_dp_row.back().to_string() << std::endl;
42+
43+
s.close();
44+
45+
return prev_dp_row.back().get_max_score();
1646
}
1747

1848
// int32_t execute_modified_global_alignment(const std::string& string_one, const std::string& string_two) {

src/alignment/alignment.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "./dp_cell.hpp"
1111

1212
namespace alignment {
13-
int64_t execute_global_alignment_bad(const std::string&, const std::string&);
13+
int64_t execute_global_alignment(const std::string&, const std::string&);
1414
// int32_t execute_modified_global_alignment(const std::string&, const std::string&);
1515
}
1616

src/alignment/de_cell.cpp

Lines changed: 156 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,117 @@ const int64_t mismatch_score = -2;
77
const int64_t opening_gap_score = -5;
88
const int64_t gap_extension_score = -2;
99

10+
// MARK: private methods
11+
12+
void dp_cell::score_s(const dp_cell& cell_s, const char c_one, const char c_two) {
13+
const int64_t s_candidate = cell_s.get_s_score();
14+
const int64_t d_candidate = cell_s.get_d_score();
15+
const int64_t i_candidate = cell_s.get_i_score();
16+
const int64_t max_candidate = std::max(s_candidate, std::max(d_candidate, i_candidate));
17+
if (s_candidate == max_candidate) {
18+
this->m_s_matches = cell_s.get_s_matches();
19+
} else if (d_candidate == max_candidate) {
20+
this->m_s_matches = cell_s.get_d_matches();
21+
} else {
22+
this->m_s_matches = cell_s.get_i_matches();
23+
}
24+
this->m_s_matches += (c_one == c_two ? 1 : 0);
25+
this->m_s_score = std::max(dp_cell::min_score, max_candidate + (c_one == c_two ? match_score : mismatch_score));
26+
}
27+
28+
void dp_cell::score_d(const dp_cell& cell_d) {
29+
const int64_t s_candidate = cell_d.get_s_score() + opening_gap_score + gap_extension_score;
30+
const int64_t d_candidate = cell_d.get_d_score() + gap_extension_score;
31+
const int64_t i_candidate = cell_d.get_i_score() + opening_gap_score + gap_extension_score;
32+
const int64_t max_candidate = std::max(s_candidate, std::max(d_candidate, i_candidate));
33+
if (s_candidate == max_candidate) {
34+
this->m_d_matches = cell_d.get_s_matches();
35+
} else if (d_candidate == max_candidate) {
36+
this->m_d_matches = cell_d.get_d_matches();
37+
} else {
38+
this->m_d_matches = cell_d.get_i_matches();
39+
}
40+
this->m_d_score = std::max(dp_cell::min_score, max_candidate);
41+
}
42+
43+
void dp_cell::score_i(const dp_cell& cell_i) {
44+
const int64_t s_candidate = cell_i.get_s_score() + opening_gap_score + gap_extension_score;
45+
const int64_t d_candidate = cell_i.get_d_score() + opening_gap_score + gap_extension_score;
46+
const int64_t i_candidate = cell_i.get_i_score() + gap_extension_score;
47+
const int64_t max_candidate = std::max(s_candidate, std::max(d_candidate, i_candidate));
48+
if (s_candidate == max_candidate) {
49+
this->m_i_matches = cell_i.get_s_matches();
50+
} else if (d_candidate == max_candidate) {
51+
this->m_i_matches = cell_i.get_d_matches();
52+
} else {
53+
this->m_i_matches = cell_i.get_i_matches();
54+
}
55+
this->m_i_score = std::max(dp_cell::min_score, max_candidate);
56+
}
57+
58+
// MARK: public methods
59+
1060
dp_cell::dp_cell() :
11-
m_i_score(0),
1261
m_s_score(0),
1362
m_d_score(0),
14-
m_i_matches(0),
63+
m_i_score(0),
1564
m_s_matches(0),
16-
m_d_matches(0) {}
65+
m_d_matches(0),
66+
m_i_matches(0) {
67+
}
1768

18-
dp_cell::~dp_cell() {}
69+
dp_cell::~dp_cell() {
70+
}
1971

2072
int64_t dp_cell::get_max_score(void) const {
21-
return std::max(this->m_i_score, std::max(this->m_s_score, this->m_d_score));
73+
return std::max(this->m_s_score, std::max(this->m_d_score, this->m_i_score));
2274
}
2375

24-
// MARK: getters
76+
int32_t dp_cell::get_max_score_matches(void) const {
77+
const int64_t max_score = std::max(this->m_s_score, std::max(this->m_d_score, this->m_i_score));
78+
if (max_score == this->m_s_score) {
79+
return this->m_s_matches;
80+
} else if (max_score == this->m_d_score) {
81+
return this->m_d_matches;
82+
}
83+
return this->m_i_matches;
84+
}
2585

26-
int64_t dp_cell::get_i_score(void) const {
27-
return this->m_i_score;
86+
void dp_cell::score_cell(const dp_cell& cell_s, const dp_cell& cell_d, const dp_cell& cell_i, const char c_one, const char c_two) {
87+
this->score_s(cell_s, c_one, c_two);
88+
this->score_d(cell_d);
89+
this->score_i(cell_i);
90+
}
91+
92+
void dp_cell::reset_cell(void) {
93+
this->m_s_score = 0;
94+
this->m_d_score = 0;
95+
this->m_i_score = 0;
96+
this->m_s_matches = 0;
97+
this->m_d_matches = 0;
98+
this->m_i_matches = 0;
2899
}
29100

101+
void dp_cell::set_top_row(size_t col_idx) {
102+
this->m_s_score = dp_cell::min_score;
103+
this->m_d_score = dp_cell::min_score;
104+
this->m_i_score = opening_gap_score + (static_cast<int64_t>(col_idx) * gap_extension_score);
105+
this->m_s_matches = 0;
106+
this->m_d_matches = 0;
107+
this->m_i_matches = 0;
108+
}
109+
110+
void dp_cell::set_left_col(size_t row_idx) {
111+
this->m_s_score = dp_cell::min_score;
112+
this->m_d_score = opening_gap_score + (static_cast<int64_t>(row_idx) * gap_extension_score);
113+
this->m_i_score = dp_cell::min_score;
114+
this->m_s_matches = 0;
115+
this->m_d_matches = 0;
116+
this->m_i_matches = 0;
117+
}
118+
119+
// MARK: getters
120+
30121
int64_t dp_cell::get_s_score(void) const {
31122
return this->m_s_score;
32123
}
@@ -35,16 +126,68 @@ int64_t dp_cell::get_d_score(void) const {
35126
return this->m_d_score;
36127
}
37128

38-
// MARK: setters
129+
int64_t dp_cell::get_i_score(void) const {
130+
return this->m_i_score;
131+
}
39132

40-
void dp_cell::set_i_score(const int64_t _i_score) {
41-
this->m_i_score = _i_score;
133+
int32_t dp_cell::get_s_matches(void) const {
134+
return this->m_s_matches;
135+
}
136+
137+
int32_t dp_cell::get_d_matches(void) const {
138+
return this->m_d_matches;
42139
}
43140

44-
void dp_cell::set_i_score(const int64_t _s_score) {
141+
int32_t dp_cell::get_i_matches(void) const {
142+
return this->m_i_matches;
143+
}
144+
145+
// MARK: setters
146+
147+
void dp_cell::set_s_score(int64_t _s_score) {
45148
this->m_s_score = _s_score;
46149
}
47150

48-
void dp_cell::set_i_score(const int64_t _d_score) {
151+
void dp_cell::set_d_score(int64_t _d_score) {
49152
this->m_d_score = _d_score;
153+
}
154+
155+
void dp_cell::set_i_score(int64_t _i_score) {
156+
this->m_i_score = _i_score;
157+
}
158+
159+
void dp_cell::set_s_matches(int32_t _s_matches) {
160+
this->m_s_matches = _s_matches;
161+
}
162+
163+
void dp_cell::set_d_matches(int32_t _d_matches) {
164+
this->m_d_matches = _d_matches;
165+
}
166+
167+
void dp_cell::set_i_matches(int32_t _i_matches) {
168+
this->m_i_matches = _i_matches;
169+
}
170+
171+
// MARK: printers
172+
173+
void dp_cell::print_scores(std::ostream& os) const {
174+
os << "(s-[" << this->m_s_score << "], d-[" << this->m_d_score << "], i-[" << this->m_i_score << "])";
175+
}
176+
177+
void dp_cell::print_matches(std::ostream& os) const {
178+
os << "(s-" << this->m_s_matches << ", d-" << this->m_d_matches << ", i-" << this->m_i_matches << ")";
179+
}
180+
181+
void dp_cell::print_all(std::ostream& os) const {
182+
os << "{(s-" << this->m_s_score << ", d-" << this->m_d_score << ", i-" << this->m_i_score << "), (s-"
183+
<< this->m_s_matches << ", d-" << this->m_d_matches << ", i-" << this->m_i_matches << ")}";
184+
}
185+
186+
std::string dp_cell::to_string(void) const {
187+
std::ostringstream string_stream;
188+
string_stream
189+
<< "{(s_score-[" << this->m_s_score << "], d_score-[" << this->m_d_score << "], i_score[" << this->m_i_score
190+
<< "]), (s_match-[" << this->m_s_matches << "], d_match[" << this->m_d_matches << "], i_match[" << this->m_d_matches
191+
<< "])}";
192+
return string_stream.str();
50193
}

0 commit comments

Comments
 (0)