-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_tools.cpp
183 lines (161 loc) · 5.34 KB
/
test_tools.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
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
#include <cassert>
#include <set>
#include <unordered_map>
#include <map>
#include "globals.hh"
#include "stdlib_printing.hh"
#include "throwing_streams.hh"
#include "test_tools.hh"
#include <cassert>
using namespace std;
typedef int64_t int64_t;
set<char> get_alphabet(string S){
set<char> ans;
for(char c: S) ans.insert(c);
return ans;
}
set<string> get_all_distinct_kmers(string S, int64_t k){
set<string> kmers;
for(int64_t i = 0; i < (int64_t)S.size()-k+1; i++){
kmers.insert(S.substr(i,k));
}
return kmers;
}
set<string> get_all_distinct_cyclic_kmers(string S, int64_t k){
set<string> kmers;
for(int64_t i = 0; i < S.size(); i++){
string kmer;
for(int64_t j = 0; j < k; j++){
kmer += S[(i+j) % S.size()];
}
kmers.insert(kmer);
}
return kmers;
}
set<string> get_all_distinct_cyclic_kmers(vector<string>& A, int64_t k){
throw std::runtime_error("ERROR: OLD CODE");
/*
string concat;
for(string read : A){
concat += read_separator + read;
}
concat += '\x01'; // bibwt end sentinel
return get_all_distinct_cyclic_kmers(concat,k);
*/
}
vector<string> get_all_kmers(string& S, int64_t k){
vector<string> kmers;
for(int64_t i = 0; i < (int64_t)S.size()-k+1; i++){
kmers.push_back(S.substr(i,k));
}
return kmers;
}
vector<string> all_binary_strings_up_to(int64_t k){ // For testing
vector<string> ans;
for(int64_t length = 1; length <= k; length++){
for(int64_t mask = 0; mask < (1 << length); mask++){
string s = "";
for(int64_t i = 0; i < length; i++){
if(mask & (1 << i)) s += 'A';
else s += 'C';
}
ans.push_back(s);
}
}
return ans;
}
string get_random_dna_string(int64_t length, int64_t alphabet_size){ // For testing
string s;
assert(alphabet_size >= 1 && alphabet_size <= 4);
char alphabet[4] = {'A','T','G','C'};
for(int64_t i = 0; i < length; i++){
s.push_back(alphabet[rand() % alphabet_size]);
}
return s;
}
string get_random_string(int64_t length, int64_t alphabet_size){ // For testing
string s;
for(int64_t i = 0; i < length; i++){
int64_t r = rand() % alphabet_size;
s += 'a' + r;
}
return s;
}
vector<string> get_sorted_suffixes(string S){
vector<string> suffixes;
for(int64_t i = 0; i < S.size(); i++){
suffixes.push_back(S.substr(i));
}
sort(suffixes.begin(), suffixes.end());
return suffixes;
}
void write_as_fasta(const vector<string>& seqs, string fasta_filename){
sbwt::throwing_ofstream out(fasta_filename);
for(const string& S : seqs) out << ">\n" << S << "\n";
}
void write_as_fastq(const vector<string>& seqs, string fastq_filename){
sbwt::throwing_ofstream out(fastq_filename);
for(const string& S : seqs){
out << "@\n" << S << "\n+\n" << string(S.size(), 'I') << "\n";
}
}
static char incoming_label(const sbwt::plain_matrix_sbwt_t& SBWT, int64_t node){
if(node < SBWT.get_C_array()[0]) return '$';
else if(node < SBWT.get_C_array()[1]) return 'A';
else if(node < SBWT.get_C_array()[2]) return 'C';
else if(node < SBWT.get_C_array()[3]) return 'G';
else return 'T';
}
// Include dummy nodes
vector<string> dump_node_labels(sbwt::plain_matrix_sbwt_t& SBWT){
vector<sdsl::select_support_mcl<>> select_supports(4);
sdsl::util::init_support(select_supports[0], &SBWT.get_subset_rank_structure().A_bits);
sdsl::util::init_support(select_supports[1], &SBWT.get_subset_rank_structure().C_bits);
sdsl::util::init_support(select_supports[2], &SBWT.get_subset_rank_structure().G_bits);
sdsl::util::init_support(select_supports[3], &SBWT.get_subset_rank_structure().T_bits);
vector<string> labels;
int64_t k = SBWT.get_k();
for(int64_t i = 0; i < SBWT.number_of_subsets(); i++){
string label;
int64_t node = i;
for(int64_t j = 0; j < k; j++){
char c = incoming_label(SBWT, node);
label.push_back(c);
if(c == '$')
continue;
if(c == 'A')
node = select_supports[0].select(node - SBWT.get_C_array()[0] + 1);
if(c == 'C')
node = select_supports[1].select(node - SBWT.get_C_array()[1] + 1);
if(c == 'G')
node = select_supports[2].select(node - SBWT.get_C_array()[2] + 1);
if(c == 'T')
node = select_supports[3].select(node - SBWT.get_C_array()[3] + 1);
}
std::reverse(label.begin(), label.end());
labels.push_back(label);
}
return labels;
}
vector<string> split_seqs_to_separate_files(string infile){
seq_io::Reader<> in(infile);
vector<string> outfiles;
while(true){ // Read unitigs
string S = in.get_next_read();
if(S == "") break;
string outfile = sbwt::get_temp_file_manager().create_filename("", ".fna");
sbwt::throwing_ofstream out(outfile);
out.stream << ">\n" << S << "\n";
outfiles.push_back(outfile);
}
return outfiles;
}
void write_lines(const vector<string>& lines, const string& filename){
sbwt::throwing_ofstream out(filename);
for(const string& line : lines) out.stream << line << "\n";
}