-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.cpp
218 lines (181 loc) · 7.09 KB
/
test.cpp
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
/* tandem.hpp
Copyright (C) 2017-2018 University of Oxford.
Author: Daniel Cooke <[email protected]>
Use of this source code is governed by the MIT license that can be found in the LICENSE file. */
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <fstream>
#include "tandem.hpp"
template <typename Container1, typename Container2>
bool are_equal(const Container1& lhs, const Container2& rhs)
{
return std::equal(std::cbegin(lhs), std::cend(lhs), std::cbegin(rhs), std::cend(rhs));
}
auto get_substrings(const std::string& str, const std::vector<tandem::LZBlock>& lz_blocks)
{
std::vector<std::string> result {};
result.reserve(lz_blocks.size());
std::transform(std::cbegin(lz_blocks), std::cend(lz_blocks), std::back_inserter(result),
[&str] (const auto& block) { return str.substr(block.pos, block.length); });
return result;
}
auto lz_decomposition(std::string str)
{
str.push_back('$'); // sentinel
auto result = get_substrings(str, tandem::lempel_ziv_factorisation(str));
assert(!result.empty() && result.back() == "$");
result.pop_back();
return result;
}
int test_lz_decomposition()
{
int num_failed_tests {0};
const std::string seq1 {"abcdbabdcadbcbdbabcabcb"};
const auto lz_factorization1 = lz_decomposition(seq1);
const std::vector<std::string> true_lz_factorization1 {"a", "b", "c", "d", "b", "ab", "d", "c", "a", "db", "c", "bd", "bab", "ca", "bcb"};
if (!are_equal(lz_factorization1, true_lz_factorization1)) {
++num_failed_tests;
}
const std::string seq2 {"aaaaa"};
const auto lz_factorization2 = lz_decomposition(seq2);
const std::vector<std::string> true_lz_factorization2 {"a", "aaaa"};
if (!are_equal(lz_factorization2, true_lz_factorization2)) {
++num_failed_tests;
}
const std::string seq3 {"abcbcbbcbaba"};
const auto lz_factorization3 = lz_decomposition(seq3);
const std::vector<std::string> true_lz_factorization3 {"a", "b", "c", "bcb", "bcb", "ab", "a"};
if (!are_equal(lz_factorization3, true_lz_factorization3)) {
++num_failed_tests;
}
const std::string seq4 {"AAATGAATT"};
const auto lz_factorization4 = lz_decomposition(seq4);
const std::vector<std::string> true_lz_factorization4 {"A", "AA", "T", "G", "AAT", "T"};
if (!are_equal(lz_factorization4, true_lz_factorization4)) {
++num_failed_tests;
}
return num_failed_tests;
}
auto lz_decomposition(std::string str, std::vector<std::uint32_t>& prev_occs)
{
str.push_back('$'); // sentinel
std::vector<tandem::LZBlock> lz_blocks;
std::tie(lz_blocks, prev_occs) = tandem::lempel_ziv_factorisation_with_prev_block_occurences(str);
auto result = get_substrings(str, lz_blocks);
assert(!result.empty() && result.back() == "$");
result.pop_back();
return result;
}
int test_lz_decomposition_with_prev_blocks()
{
int num_failed_tests {0};
std::vector<std::uint32_t> prev_occs;
const std::string seq1 {"abcdbabdcadbcbdbabcabcb"};
const auto lz_factorization1 = lz_decomposition(seq1, prev_occs);
const std::vector<std::string> true_lz_factorization1 {"a", "b", "c", "d", "b", "ab", "d", "c", "a", "db", "c", "bd", "bab", "ca", "bcb"};
if (!are_equal(lz_factorization1, true_lz_factorization1)) {
++num_failed_tests;
}
const std::string seq2 {"aaaaa"};
const auto lz_factorization2 = lz_decomposition(seq2, prev_occs);
const std::vector<std::string> true_lz_factorization2 {"a", "aaaa"};
if (!are_equal(lz_factorization2, true_lz_factorization2)) {
++num_failed_tests;
}
const std::string seq3 {"abcbcbbcbaba"};
const auto lz_factorization3 = lz_decomposition(seq3, prev_occs);
const std::vector<std::string> true_lz_factorization3 {"a", "b", "c", "bcb", "bcb", "ab", "a"};
if (!are_equal(lz_factorization3, true_lz_factorization3)) {
++num_failed_tests;
}
const std::string seq4 {"AAATGAAT"};
const auto lz_factorization4 = lz_decomposition(seq4, prev_occs);
const std::vector<std::string> true_lz_factorization4 {"A", "AA", "T", "G", "AAT"};
if (!are_equal(lz_factorization4, true_lz_factorization4)) {
++num_failed_tests;
}
return num_failed_tests;
}
auto get_mains_leftmost_maximal_periodiciites(std::string str)
{
str.push_back('$');
const auto lz_blocks = tandem::lempel_ziv_factorisation(str);
auto lmrs = tandem::detail::find_leftmost_maximal_repetitions(str, lz_blocks);
assert(!lmrs.empty());
std::vector<std::string> result {};
result.reserve(lmrs.size());
for (const auto& lmr : lmrs) {
result.push_back(str.substr(lmr.pos, lmr.length));
}
std::sort(std::begin(result), std::end(result));
result.erase(std::unique(std::begin(result), std::end(result)), std::end(result));
return result;
}
int test_mains_algorithm()
{
int num_failed_tests {0};
const std::string seq1 {"abcbcbbcbaba"};
const auto lmrs1 = get_mains_leftmost_maximal_periodiciites(seq1);
const std::vector<std::string> true_lmrs1 {"baba", "bb", "bcbbcb", "bcbcb"};
if (!are_equal(lmrs1, true_lmrs1)) {
++num_failed_tests;
}
const std::string seq2 {"AAATGAAT"};
const auto lmrs2 = get_mains_leftmost_maximal_periodiciites(seq2);
const std::vector<std::string> true_lmrs2 {"AA", "AAA"};
if (!are_equal(lmrs2, true_lmrs2)) {
++num_failed_tests;
}
return num_failed_tests;
}
auto get_repeat_strings(std::string str)
{
str.push_back('$');
auto repeats = tandem::extract_exact_tandem_repeats(str);
std::vector<std::string> result {};
result.reserve(repeats.size());
for (const auto& repeat : repeats) {
result.push_back(str.substr(repeat.pos, repeat.length));
}
return result;
}
int test_tandem_repeat_finder()
{
int num_failed_tests {0};
const std::string seq1 {"AAATGAAT$"};
const auto repeats1 = get_repeat_strings(seq1);
const std::vector<std::string> true_repeats1 {"AAA", "AA"};
if (are_equal(repeats1, true_repeats1)) {
++num_failed_tests;
}
std::ifstream file {"../test_dna.txt"};
assert(file.good());
std::string seq2;
std::getline(file, seq2);
seq2.push_back('$');
const auto repeats2 = tandem::extract_exact_tandem_repeats(seq2);
for (const auto& repeat : repeats2) {
const auto substr = seq2.substr(repeat.pos, repeat.length);
if (!std::equal(substr.cbegin(), substr.cbegin() + repeat.period, substr.cbegin() + repeat.period)) {
++num_failed_tests;
break;
}
}
return num_failed_tests;
}
int main()
{
int num_failed_tests {0};
num_failed_tests += test_lz_decomposition();
num_failed_tests += test_lz_decomposition_with_prev_blocks();
num_failed_tests += test_mains_algorithm();
num_failed_tests += test_tandem_repeat_finder();
if (num_failed_tests > 0) {
std::cout << num_failed_tests << " tests FAILED!" << std::endl;
} else {
std::cout << "All tests PASSED!" << std::endl;
}
}