-
Notifications
You must be signed in to change notification settings - Fork 37
/
vcf_header.cpp
425 lines (332 loc) · 14.5 KB
/
vcf_header.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Copyright (c) 2015-2019 Daniel Cooke
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
#include "vcf_header.hpp"
#include <algorithm>
#include <utility>
#include <iterator>
#include "vcf_spec.hpp"
namespace octopus {
bool is_valid_line(const std::string& line);
bool is_valid_field(const std::string& str);
bool is_format_line(const std::string& line);
std::unordered_map<std::string, std::string> parse_fields(const std::string& fields);
// public methods
VcfHeader::VcfHeader() : VcfHeader {vcfspec::version} {}
VcfHeader::VcfHeader(std::string file_format)
: file_format_ {std::move(file_format)}
{}
const VcfHeader::Value& VcfHeader::file_format() const noexcept
{
return file_format_;
}
unsigned VcfHeader::num_samples() const noexcept
{
return static_cast<unsigned>(samples_.size());
}
const std::vector<std::string>& VcfHeader::samples() const noexcept
{
return samples_;
}
bool VcfHeader::has(const BasicKey& k) const noexcept
{
return basic_fields_.count(k) == 1;
}
bool VcfHeader::has(const Tag& t) const noexcept
{
return structured_fields_.count(t) > 0;
}
bool VcfHeader::has(const Tag& t, const StructuredKey& k) const noexcept
{
return structured_fields_.count(t) > 0; // TODO: complete this
}
const VcfHeader::Value& VcfHeader::at(const BasicKey& k) const
{
return basic_fields_.at(k);
}
const VcfHeader::Value& VcfHeader::find(const Tag& search_tag, const StructuredKey& search_key,
const StructuredKey& id_key, const Value& id_value) const
{
const auto er = structured_fields_.equal_range(search_tag);
return std::find_if(er.first, er.second,
[&] (const auto& p) {
return p.second.at(id_key) == id_value;
})->second.at(search_key);
}
std::vector<VcfHeader::BasicKey> VcfHeader::basic_keys() const
{
std::vector<BasicKey> result {};
result.reserve(basic_fields_.size());
std::transform(std::cbegin(basic_fields_), std::cend(basic_fields_),
std::back_inserter(result), [] (const auto& p) { return p.first; });
return result;
}
std::vector<VcfHeader::Tag> VcfHeader::tags() const
{
std::vector<Tag> result {};
result.reserve(structured_fields_.size());
std::transform(std::cbegin(structured_fields_), std::cend(structured_fields_),
std::back_inserter(result), [] (const auto& p) { return p.first; });
std::sort(std::begin(result), std::end(result));
result.erase(std::unique(std::begin(result), std::end(result)), std::end(result));
return result;
}
std::vector<VcfHeader::StructuredKey> VcfHeader::keys(const Tag& t) const
{
std::vector<StructuredKey> result {};
// TODO
return result;
}
const VcfHeader::BasicFieldMap& VcfHeader::basic_fields() const noexcept
{
return basic_fields_;
}
std::vector<VcfHeader::StructuredField> VcfHeader::structured_fields(const Tag& tag) const
{
std::vector<StructuredField> result {};
result.reserve(structured_fields_.count(tag));
const auto er = structured_fields_.equal_range(tag);
std::transform(er.first, er.second, std::back_inserter(result),
[] (const auto& p) { return p.second; });
return result;
}
const VcfHeader::StructuredFieldMap& VcfHeader::structured_fields() const noexcept
{
return structured_fields_;
}
// non-member methods
const VcfHeader::Value& get_id_field_value(const VcfHeader& header, const VcfHeader::Tag& tag,
const VcfHeader::Value& id_value,
const VcfHeader::StructuredKey& lookup_key)
{
return header.find(tag, vcfspec::header::meta::struc::id, id_value, lookup_key);
}
const VcfHeader::Value& get_id_field_type(const VcfHeader& header, const VcfHeader::Tag& tag,
const VcfHeader::Value& id_value)
{
using namespace vcfspec::header::meta;
return header.find(tag, struc::id, id_value, struc::type);
}
VcfType get_typed_value(const VcfHeader& header, const VcfHeader::Tag& tag,
const VcfHeader::StructuredKey& key, const VcfHeader::Value& value)
{
return make_vcf_type(get_id_field_type(header, tag, key), value);
}
VcfType get_typed_info_value(const VcfHeader& header, const VcfHeader::StructuredKey& key,
const VcfHeader::Value& value)
{
return get_typed_value(header, vcfspec::header::meta::tag::info, key, value);
}
VcfType get_typed_format_value(const VcfHeader& header, const VcfHeader::StructuredKey& key,
const VcfHeader::Value& value)
{
return get_typed_value(header, vcfspec::header::meta::tag::format, key, value);
}
std::vector<VcfType> get_typed_values(const VcfHeader& header, const VcfHeader::StructuredKey& format_key,
const VcfHeader::StructuredKey& field_key,
const std::vector<VcfHeader::Value>& values)
{
std::vector<VcfType> result {};
result.reserve(values.size());
std::transform(values.cbegin(), values.cend(), std::back_inserter(result),
[&header, &format_key, &field_key] (const auto& value) {
return get_typed_value(header, format_key, field_key, value);
});
return result;
}
std::vector<VcfType> get_typed_info_values(const VcfHeader& header, const VcfHeader::StructuredKey& field_key,
const std::vector<VcfHeader::Value>& values)
{
return get_typed_values(header, vcfspec::header::meta::tag::info, field_key, values);
}
std::vector<VcfType> get_typed_format_values(const VcfHeader& header, const VcfHeader::StructuredKey& field_key,
const std::vector<VcfHeader::Value>& values)
{
return get_typed_values(header, vcfspec::header::meta::tag::format, field_key, values);
}
bool contig_line_exists(const VcfHeader& header, const std::string& contig)
{
return header.has(vcfspec::header::meta::tag::contig, contig);
}
bool operator==(const VcfHeader& lhs, const VcfHeader& rhs)
{
return lhs.file_format() == rhs.file_format()
&& lhs.samples() == rhs.samples()
&& lhs.basic_fields() == rhs.basic_fields()
&& lhs.structured_fields() == rhs.structured_fields();
}
bool operator!=(const VcfHeader& lhs, const VcfHeader& rhs)
{
return !operator==(lhs, rhs);
}
std::ostream& operator<<(std::ostream& os, const VcfHeader::StructuredField& fields)
{
os << "<";
for (const auto& p : fields) {
os << p.first.value << "=" << p.second << ",";
}
os << ">";
return os;
}
std::ostream& operator<<(std::ostream& os, const VcfHeader& header)
{
using vcfspec::header::lineOpener;
os << lineOpener << vcfspec::header::meta::vcfVersion;
os << header.file_format_ << std::endl;
for (const auto& field : header.basic_fields_) {
os << lineOpener << field.first.value << "=" << field.second << std::endl;
}
for (const auto& format : header.structured_fields_) {
os << lineOpener << format.first.value << "=" << format.second << std::endl;
}
// TODO
return os;
}
// VcfHeader::Builder
VcfHeader::Builder::Builder(const VcfHeader& header)
: file_format_ {header.file_format_}
, samples_ {header.samples_}
, basic_fields_ {header.basic_fields_}
, structured_fields_ {header.structured_fields_}
{}
VcfHeader::Builder& VcfHeader::Builder::set_file_format(std::string file_format)
{
file_format_ = std::move(file_format);
return *this;
}
VcfHeader::Builder& VcfHeader::Builder::add_sample(std::string sample)
{
samples_.push_back(std::move(sample));
return *this;
}
VcfHeader::Builder& VcfHeader::Builder::set_samples(std::vector<std::string> samples)
{
samples_ = std::move(samples);
return *this;
}
VcfHeader::Builder& VcfHeader::Builder::add_basic_field(std::string key, std::string value)
{
if (key != vcfspec::header::meta::vcfVersion) {
basic_fields_.emplace(std::move(key), std::move(value));
}
return *this;
}
VcfHeader::Builder& VcfHeader::Builder::add_structured_field(std::string tag,
std::unordered_map<std::string, std::string> values)
{
VcfHeader::StructuredField tmp {};
tmp.reserve(values.size());
for (auto&& p : values) tmp.emplace(p.first, std::move(p.second));
structured_fields_.emplace(std::move(tag), std::move(tmp));
return *this;
}
std::string add_quotes(const std::string& str)
{
std::string result {};
if (str.front() != '"') result = '"' + str;
if (str.back() != '"') result.push_back('"');
return result;
}
VcfHeader::Builder& VcfHeader::Builder::add_info(std::string id, std::string number,
std::string type, std::string description,
std::unordered_map<std::string, std::string> other_values)
{
using namespace vcfspec::header::meta;
other_values.reserve(other_values.size() + 4);
other_values.emplace(struc::id, std::move(id));
other_values.emplace(struc::number, std::move(number));
other_values.emplace(struc::type, std::move(type));
other_values.emplace(struc::description, add_quotes(description));
add_structured_field(tag::info, std::move(other_values));
return *this;
}
VcfHeader::Builder& VcfHeader::Builder::add_filter(std::string id, std::string description,
std::unordered_map<std::string, std::string> other_values)
{
using namespace vcfspec::header::meta;
other_values.reserve(other_values.size() + 2);
other_values.emplace(struc::id, std::move(id));
other_values.emplace(struc::description, add_quotes(description));
add_structured_field(tag::filter, std::move(other_values));
return *this;
}
VcfHeader::Builder& VcfHeader::Builder::add_format(std::string id, std::string number,
std::string type, std::string description,
std::unordered_map<std::string, std::string> other_values)
{
using namespace vcfspec::header::meta;
other_values.reserve(other_values.size() + 4);
other_values.emplace(struc::id, std::move(id));
other_values.emplace(struc::number, std::move(number));
other_values.emplace(struc::type, std::move(type));
other_values.emplace(struc::description, add_quotes(description));
add_structured_field(tag::format, std::move(other_values));
return *this;
}
VcfHeader::Builder& VcfHeader::Builder::add_contig(std::string id,
std::unordered_map<std::string, std::string> other_values)
{
using namespace vcfspec::header::meta;
other_values.emplace(struc::id, std::move(id));
add_structured_field(tag::contig, std::move(other_values));
return *this;
}
VcfHeader::Builder& VcfHeader::Builder::clear_info() noexcept
{
using namespace vcfspec::header::meta;
structured_fields_.erase(tag::info);
return *this;
}
VcfHeader::Builder& VcfHeader::Builder::clear_format() noexcept
{
using namespace vcfspec::header::meta;
structured_fields_.erase(tag::format);
samples_.clear();
return *this;
}
VcfHeader VcfHeader::Builder::build() const
{
return VcfHeader {file_format_, samples_, basic_fields_, structured_fields_};
}
VcfHeader VcfHeader::Builder::build_once() noexcept
{
return VcfHeader {std::move(file_format_), std::move(samples_), std::move(basic_fields_), std::move(structured_fields_)};
}
VcfHeader::Builder get_default_header_builder()
{
VcfHeader::Builder result {};
result.add_info("AA", "1", "String", "Ancestral allele");
result.add_info("AC", "1", "Integer", "Allele count in genotypes, for each ALT allele, in the same order as listed");
result.add_info("AF", "A", "Float", "Allele Frequency, for each ALT allele, in the same order as listed");
result.add_info("AN", "1", "Integer", "Total number of alleles in called genotypes");
result.add_info("BQ", "1", "Integer", "RMS base quality at this position");
result.add_info("CIGAR", "A", "String", "Cigar string describing how to align an alternate allele to the reference allele");
result.add_info("DB", "0", "Flag", "dbSNP membership");
result.add_info("DP", "1", "Integer", "Combined depth across samples");
result.add_info("END", "1", "Integer", "End position of the variant described in this record");
result.add_info("H2", "0", "Flag", "Membership in hapmap2");
result.add_info("H3", "0", "Flag", "Membership in hapmap3");
result.add_info("MQ", "1", "Integer", "RMS mapping quality");
result.add_info("MQ0", "1", "Integer", "Number of MAPQ == 0 reads covering this record");
result.add_info("NS", "1", "Integer", "Number of samples with data");
result.add_info("SB", "1", "Float", "Strand bias at this position");
result.add_info("SOMATIC", "0", "Flag", "Indicates that the record is a somatic mutation, for cancer genomics");
result.add_info("VALIDATED", "0", "Flag", "Validated by follow-up experiment");
result.add_info("1000G", "0", "Flag", "Membership in 1000 Genomes");
result.add_format("GT", "1", "String", "Genotype");
result.add_format("DP", "1", "Integer", "Read depth at this position for this sample");
result.add_format("FT", "1", "String", "Sample genotype filter indicating if this genotype was “called”");
result.add_format("GL", "G", "Float", "log10-scaled genotype likelihoods");
result.add_format("GLE", "1", "Integer", "Genotype likelihoods of heterogeneous ploidy");
result.add_format("PL", "G", "Integer", "Phred-scaled genotype likelihoods");
result.add_format("GP", "G", "Float", "Phred-scaled genotype posterior probabilities");
result.add_format("GQ", "1", "Integer", "Conditional genotype quality (phred-scaled)");
result.add_format("HQ", "1", "Integer", "Haplotype qualities");
result.add_format("PS", "1", "String", "Phase set");
result.add_format("PQ", "1", "Integer", "Phasing quality");
result.add_format("EC", "1", "Integer", "Expected alternate allele counts");
result.add_format("MQ", "1", "Integer", "RMS mapping quality");
result.add_format("BQ", "1", "Integer", "RMS base quality at this position");
result.add_filter("PASS", "All filters passed");
return result;
}
} // namespace octopus