Repository navigation
Expand file tree
/
Copy pathtest.cpp
More file actions
265 lines (244 loc) · 11.4 KB
/
Copy pathtest.cpp
File metadata and controls
265 lines (244 loc) · 11.4 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <iostream>
#include <string>
#include "vg/vg.pb.h"
#include <google/protobuf/descriptor.h>
#include "vg/io/alignment_io.hpp"
#include "vg/io/protobuf_emitter.hpp"
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <mutex>
#include <unistd.h>
#include <sstream>
#include <stdexcept>
#include <omp.h>
namespace {
using namespace vg;
using namespace vg::io;
using Groups = std::vector<std::vector<std::string>>;
void require(bool condition, const char* message) {
if (!condition) {
throw std::runtime_error(message);
}
}
void check_groups(Groups observed, Groups expected, size_t count) {
// Callback completion order is unspecified, but order within a run is not.
std::sort(observed.begin(), observed.end());
std::sort(expected.begin(), expected.end());
require(count == expected.size(), "wrong group count");
require(observed == expected, "groups lost, split, combined, or reordered records");
}
}
void test_grouped_input() {
// A repeated key after another key starts a new group; the final A is flushed at EOF.
const std::vector<std::string> input{"a:0", "a:1", "b:2", "a:3"};
size_t cursor = 0;
Groups observed;
auto count = grouped_for_each_parallel<std::string>(
[&](std::string& record) {
if (cursor == input.size()) return false;
record = input[cursor++];
return true;
},
[](const std::string& record) { return record.substr(0, record.find(':')); },
[&](std::vector<std::string>& group) { observed.push_back(group); }, 2);
check_groups(observed, {{"a:0", "a:1"}, {"b:2"}, {"a:3"}}, count);
std::cerr << "Consecutive grouping and EOF tests passed." << std::endl;
}
void test_grouped_gam_input() {
std::stringstream gam;
{
ProtobufEmitter<Alignment> emitter(gam);
for (int score : {10, 20, 30}) {
Alignment aln;
aln.set_name(score == 30 ? "b" : "a");
aln.set_sequence("ACGT");
aln.set_score(score);
emitter.write(std::move(aln));
}
}
std::vector<std::vector<Alignment>> observed;
auto count = gam_grouped_for_each_parallel(gam,
[&](std::vector<Alignment>& group) { observed.push_back(std::move(group)); }, 2);
Groups names_and_scores;
for (const auto& group : observed) {
names_and_scores.emplace_back();
for (const auto& aln : group) {
require(aln.sequence() == "ACGT", "GAM sequence changed");
names_and_scores.back().push_back(aln.name() + ":" + std::to_string(aln.score()));
}
}
check_groups(names_and_scores, {{"a:10", "a:20"}, {"b:30"}}, count);
std::cerr << "Grouped GAM adapter test passed." << std::endl;
}
void test_grouped_gaf_input() {
// Two placements of read a followed by one placement of read b.
const char* gaf = R"(a 4 0 4 + >1 4 0 4 4 4 60 cs:Z::4 AS:i:10 tp:A:P
a 4 0 4 + >1 4 0 4 4 4 60 cs:Z::4 AS:i:20 tp:A:S
b 4 0 4 + >1 4 0 4 4 4 60 cs:Z::4 AS:i:30 tp:A:P
)";
const char* temporary_directory = std::getenv("TMPDIR");
std::string pattern =
temporary_directory != nullptr && temporary_directory[0] != '\0'
? temporary_directory
: ".";
pattern += "/libvgio-grouped-XXXXXX";
std::vector<char> filename(pattern.begin(), pattern.end());
filename.push_back('\0');
int fd = mkstemp(filename.data());
require(fd != -1, "could not create temporary GAF file");
FILE* file = fdopen(fd, "w");
if (file == nullptr) {
close(fd);
std::remove(filename.data());
throw std::runtime_error("could not open temporary GAF file");
}
bool written = std::fputs(gaf, file) >= 0;
bool closed = std::fclose(file) == 0;
if (!written || !closed) {
std::remove(filename.data());
throw std::runtime_error("could not write temporary GAF file");
}
std::vector<std::vector<Alignment>> observed;
size_t count;
try {
count = gaf_grouped_for_each_parallel(
[](nid_t) { return size_t(4); },
[](nid_t, bool) { return std::string("ACGT"); }, filename.data(),
[&](std::vector<Alignment>& group) { observed.push_back(std::move(group)); }, 2);
} catch (...) {
std::remove(filename.data());
throw;
}
std::remove(filename.data());
Groups names_and_scores;
for (const auto& group : observed) {
names_and_scores.emplace_back();
for (const auto& aln : group) {
require(aln.sequence() == "ACGT", "GAF sequence reconstruction failed");
require(aln.path().mapping_size() == 1 && aln.path().mapping(0).position().node_id() == 1,
"GAF graph placement was lost");
require(aln.is_secondary() == (aln.score() == 20), "GAF primary/secondary status was lost");
names_and_scores.back().push_back(aln.name() + ":" + std::to_string(aln.score()));
}
}
check_groups(names_and_scores, {{"a:10", "a:20"}, {"b:30"}}, count);
std::cerr << "Grouped GAF adapter test passed." << std::endl;
}
void test_paired_grouped_input() {
const int previous_threads = omp_get_max_threads();
for (int threads : {1, 4}) {
omp_set_num_threads(threads);
for (bool empty : {false, true}) {
std::stringstream gam;
{
ProtobufEmitter<Alignment> emitter(gam);
if (!empty) {
for (int i = 0; i < 33; ++i) {
for (int score : {10, 20, 30}) {
Alignment first, second;
first.set_name(std::to_string(i) + "/1");
second.set_name(std::to_string(i) + "/2");
first.mutable_fragment_next()->set_name(second.name());
second.mutable_fragment_prev()->set_name(first.name());
first.set_score(score);
second.set_score(score + 1);
emitter.write(std::move(first));
emitter.write(std::move(second));
}
}
}
}
std::mutex mutex;
size_t processed = 0;
bool ready = false;
auto collect = [&](std::vector<std::pair<Alignment, Alignment>>& group) {
std::lock_guard<std::mutex> lock(mutex);
require(group.size() == 3, "paired group was split across batches");
for (size_t i = 0; i < group.size(); ++i) {
require(group[i].first.name() == group.front().first.name(), "fragment names mixed");
require(group[i].first.score() == 10 * (i + 1), "pair order changed");
require(group[i].second.score() == group[i].first.score() + 1, "mates mismatched");
}
++processed;
if (!ready && processed == 3) ready = true;
};
auto count = gam_paired_grouped_for_each_parallel(gam, collect, [&] { return ready; }, 2);
require(count == (empty ? 0 : 33) && processed == count, "unexpected number of paired groups processed");
}
}
omp_set_num_threads(previous_threads);
std::cerr << "Paired grouped input tests passed." << std::endl;
}
void test_diploid_tags() {
auto length = [](nid_t) -> size_t { return 4; };
auto sequence = [](nid_t, bool) { return string("ACGT"); };
for (bool preferred : {false, true}) {
for (int quality : {0, 60, 90, 255}) {
Alignment original;
original.set_name("diploid");
original.set_sequence("ACGT");
original.set_is_secondary(!preferred);
auto* mapping = original.mutable_path()->add_mapping();
mapping->mutable_position()->set_node_id(1);
mapping->add_edit()->set_from_length(4);
mapping->mutable_edit(0)->set_to_length(4);
require(decode_diploid_tag(original, "hp", 'Z', preferred ? "pri_hap" : "sec_hap"), "hp decoding failed");
require(decode_diploid_tag(original, "hq", 'i', "0"), "hq decoding failed");
require(decode_diploid_tag(original, "aq", 'i', to_string(quality)), "aq decoding failed");
(*original.mutable_annotation()->mutable_fields())["tags"].set_string_value("ZZ:Z:keep\thp:Z:obsolete\taq:i:1");
auto gaf = alignment_to_gaf(length, sequence, original);
require(gaf.opt_fields.at("aq").second == to_string(quality), "typed aq did not override raw tag");
Alignment restored;
gaf_to_alignment(length, sequence, gaf, restored);
require(encode_diploid_tags(restored) == encode_diploid_tags(original), "diploid GAF round trip failed");
require(restored.is_secondary() == original.is_secondary(), "GAF primary status was lost");
require(restored.annotation().fields().at("tags").string_value() == "ZZ:Z:keep", "raw tag preservation failed");
}
}
Alignment alignment;
for (const string& value : {"", "-1", "+1", " 1", "1 ", "256", "1.5", "60junk", "999999999999999999999"}) {
require(!decode_diploid_tag(alignment, "aq", 'i', value), "invalid quality accepted");
}
require(!decode_diploid_tag(alignment, "hp", 'i', "1"), "unrelated hp type consumed");
require(!decode_diploid_tag(alignment, "hp", 'Z', "other"), "unrelated hp value consumed");
(*alignment.mutable_annotation()->mutable_fields())["diploid_source_mapping_quality"].set_number_value(1.5);
bool rejected = false;
try { encode_diploid_tags(alignment); } catch (const invalid_argument&) { rejected = true; }
require(rejected, "invalid typed quality accepted");
}
int main (int arcg, char** argv) {
std::cerr << "Testing libvgio..." << std::endl;
std::cerr << "Creating Graph..." << std::endl;
vg::Graph g;
std::cerr << "Graph exists at " << &g << std::endl;
// Check up on Protobuf
std::cerr << "Checking for Protobuf descriptor pool..." << std::endl;
const google::protobuf::DescriptorPool* pool = google::protobuf::DescriptorPool::generated_pool();
if (pool == nullptr) {
throw std::runtime_error("Cound not find Protobuf descriptor pool: is libvgio working?");
}
std::cerr << "Found descriptor pool at " << pool << std::endl;
for (auto message_name : {"vg.Graph", "vg.Alignment", "vg.Position"}) {
std::cerr << "Checking for Protobuf message type " << message_name << "..." << std::endl;
const google::protobuf::Descriptor* descriptor = pool->FindMessageTypeByName(message_name);
if (descriptor == nullptr) {
throw std::runtime_error(std::string("Cound not find Protobuf descriptor for message type ") + message_name + ": is libvgio working?");
}
std::cerr << "Found " << message_name << " as " << descriptor->full_name() << " at " << descriptor << std::endl;
}
// These tests check grouping rather than concurrency. Run with one thread
// because their callbacks append to shared vectors without locking.
const int previous_threads = omp_get_max_threads();
omp_set_num_threads(1);
test_grouped_input();
test_grouped_gam_input();
test_grouped_gaf_input();
omp_set_num_threads(previous_threads);
// Exercise multiple batches with one and four threads, including the
// transition from serial processing.
test_paired_grouped_input();
test_diploid_tags();
std::cerr << "Tests complete!" << std::endl;
return 0;
}