Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
variant_builder.h
Go to the documentation of this file.
1 #ifndef gamgee__variant_builder__guard
2 #define gamgee__variant_builder__guard
3 
4 #include "variant.h"
8 #include "genotype.h"
9 
10 #include <vector>
11 #include <string>
12 
13 namespace gamgee {
14 
21 template<class FIELD_TYPE>
23  public:
24  VariantBuilderCoreField() : m_field_value{}, m_is_set{false} {}
25 
26  void set(const FIELD_TYPE value) { m_field_value = value; m_is_set = true; }
27  FIELD_TYPE field_value() const { return m_field_value; }
28  bool is_set() const { return m_is_set; }
29  void clear() { m_is_set = false; }
30 
31  private:
32  FIELD_TYPE m_field_value;
33  bool m_is_set;
34 };
35 
36 
165  public:
166 
179  explicit VariantBuilder(const VariantHeader& header);
180 
181  // VariantBuilders are moveable but not copyable, and use default destruction
182  VariantBuilder(VariantBuilder&& other) = default;
183  VariantBuilder& operator=(VariantBuilder&& other) = default;
184  VariantBuilder(const VariantBuilder& other) = delete;
185  VariantBuilder& operator=(const VariantBuilder& other) = delete;
186  ~VariantBuilder() = default;
187 
193  VariantHeader header() const { return VariantHeader{m_header}; }
194 
206  VariantBuilder& set_enable_validation(const bool enable_validation);
207 
208 
209  /******************************************************************************
210  *
211  * Functions for setting core site-level fields
212  *
213  ******************************************************************************/
214 
220  VariantBuilder& set_chromosome(const uint32_t chromosome);
221 
227  VariantBuilder& set_chromosome(const std::string& chromosome);
228 
236  VariantBuilder& set_alignment_start(const uint32_t alignment_start);
237 
245  VariantBuilder& set_alignment_stop(const uint32_t alignment_stop);
246 
252  VariantBuilder& set_qual(const float qual);
253 
259  VariantBuilder& set_id(const std::string& id);
260 
266  VariantBuilder& set_ref_allele(const std::string& ref_allele);
267 
277  VariantBuilder& set_alt_allele(const std::string& alt_allele);
278 
288  VariantBuilder& set_alt_alleles(const std::vector<std::string>& alt_alleles);
289 
297  VariantBuilder& set_filters(const std::vector<std::string>& filters);
298 
304  VariantBuilder& set_filters(const std::vector<int32_t>& filters);
305 
306 
307  /******************************************************************************
308  *
309  * Functions for removing core site-level fields
310  * (Note that we don't have removal functions for the required fields)
311  *
312  ******************************************************************************/
313 
318 
323 
328 
333 
338 
339 
340  /******************************************************************************
341  *
342  * Functions for setting shared/INFO fields
343  *
344  ******************************************************************************/
345 
357  VariantBuilder& set_integer_shared_field(const std::string& tag, const int32_t value);
358 
367  VariantBuilder& set_integer_shared_field(const std::string& tag, const std::vector<int32_t>& values);
368 
378  VariantBuilder& set_integer_shared_field(const uint32_t index, const int32_t value);
379 
386  VariantBuilder& set_integer_shared_field(const uint32_t index, const std::vector<int32_t>& values);
387 
399  VariantBuilder& set_float_shared_field(const std::string& tag, const float value);
400 
409  VariantBuilder& set_float_shared_field(const std::string& tag, const std::vector<float>& values);
410 
420  VariantBuilder& set_float_shared_field(const uint32_t index, const float value);
421 
428  VariantBuilder& set_float_shared_field(const uint32_t index, const std::vector<float>& values);
429 
438  VariantBuilder& set_string_shared_field(const std::string& tag, const std::string& value);
439 
446  VariantBuilder& set_string_shared_field(const uint32_t index, const std::string& value);
447 
458  VariantBuilder& set_boolean_shared_field(const std::string& tag);
459 
468  VariantBuilder& set_boolean_shared_field(const uint32_t index);
469 
470 
471  /******************************************************************************
472  *
473  * Functions for removing shared/INFO fields
474  *
475  ******************************************************************************/
476 
484  VariantBuilder& remove_shared_field(const std::string& tag);
485 
491  VariantBuilder& remove_shared_field(const uint32_t field_index);
492 
500  VariantBuilder& remove_shared_fields(const std::vector<std::string>& tags);
501 
507  VariantBuilder& remove_shared_fields(const std::vector<uint32_t>& field_indices);
508 
509 
510  /******************************************************************************
511  *
512  * Functions for setting individual/FORMAT fields in bulk
513  * (ie., setting all values for all samples at once)
514  *
515  ******************************************************************************/
516 
550  VariantBuilder& set_genotypes(const VariantBuilderMultiSampleVector<int32_t>& genotypes_for_all_samples);
551 
585 
604  VariantBuilder& set_genotypes(const std::vector<std::vector<int32_t>>& genotypes_for_all_samples);
605 
623  VariantBuilder& set_genotypes(std::vector<std::vector<int32_t>>&& genotypes_for_all_samples);
624 
653  VariantBuilder& set_integer_individual_field(const std::string& tag, const VariantBuilderMultiSampleVector<int32_t>& values_for_all_samples);
654 
682  VariantBuilder& set_integer_individual_field(const std::string& tag, VariantBuilderMultiSampleVector<int32_t>&& values_for_all_samples);
683 
699  VariantBuilder& set_integer_individual_field(const std::string& tag, const std::vector<std::vector<int32_t>>& values_for_all_samples);
700 
715  VariantBuilder& set_integer_individual_field(const std::string& tag, std::vector<std::vector<int32_t>>&& values_for_all_samples);
716 
744  VariantBuilder& set_integer_individual_field(const uint32_t field_index, const VariantBuilderMultiSampleVector<int32_t>& values_for_all_samples);
745 
771  VariantBuilder& set_integer_individual_field(const uint32_t field_index, VariantBuilderMultiSampleVector<int32_t>&& values_for_all_samples);
772 
787  VariantBuilder& set_integer_individual_field(const uint32_t field_index, const std::vector<std::vector<int32_t>>& values_for_all_samples);
788 
802  VariantBuilder& set_integer_individual_field(const uint32_t field_index, std::vector<std::vector<int32_t>>&& values_for_all_samples);
803 
832  VariantBuilder& set_float_individual_field(const std::string& tag, const VariantBuilderMultiSampleVector<float>& values_for_all_samples);
833 
861  VariantBuilder& set_float_individual_field(const std::string& tag, VariantBuilderMultiSampleVector<float>&& values_for_all_samples);
862 
878  VariantBuilder& set_float_individual_field(const std::string& tag, const std::vector<std::vector<float>>& values_for_all_samples);
879 
894  VariantBuilder& set_float_individual_field(const std::string& tag, std::vector<std::vector<float>>&& values_for_all_samples);
895 
923  VariantBuilder& set_float_individual_field(const uint32_t field_index, const VariantBuilderMultiSampleVector<float>& values_for_all_samples);
924 
950  VariantBuilder& set_float_individual_field(const uint32_t field_index, VariantBuilderMultiSampleVector<float>&& values_for_all_samples);
951 
966  VariantBuilder& set_float_individual_field(const uint32_t field_index, const std::vector<std::vector<float>>& values_for_all_samples);
967 
981  VariantBuilder& set_float_individual_field(const uint32_t field_index, std::vector<std::vector<float>>&& values_for_all_samples);
982 
992  VariantBuilder& set_string_individual_field(const std::string& tag, const std::vector<std::string>& values_for_all_samples);
993 
1002  VariantBuilder& set_string_individual_field(const std::string& tag, std::vector<std::string>&& values_for_all_samples);
1003 
1012  VariantBuilder& set_string_individual_field(const uint32_t field_index, const std::vector<std::string>& values_for_all_samples);
1013 
1020  VariantBuilder& set_string_individual_field(const uint32_t field_index, std::vector<std::string>&& values_for_all_samples);
1021 
1022 
1023  /******************************************************************************
1024  *
1025  * Functions for setting individual/FORMAT fields by sample
1026  *
1027  ******************************************************************************/
1028 
1043  VariantBuilder& set_genotype(const std::string& sample, const std::vector<int32_t>& genotype);
1044 
1058  VariantBuilder& set_genotype(const std::string& sample, std::vector<int32_t>&& genotype);
1059 
1073  VariantBuilder& set_genotype(const uint32_t sample_index, const std::vector<int32_t>& genotype);
1074 
1087  VariantBuilder& set_genotype(const uint32_t sample_index, std::vector<int32_t>&& genotype);
1088 
1100  VariantBuilder& set_integer_individual_field(const std::string& tag, const std::string& sample, const int32_t value);
1101 
1111  VariantBuilder& set_integer_individual_field(const std::string& tag, const std::string& sample, const std::vector<int32_t>& values);
1112 
1123  VariantBuilder& set_integer_individual_field(const uint32_t field_index, const uint32_t sample_index, const int32_t value);
1124 
1132  VariantBuilder& set_integer_individual_field(const uint32_t field_index, const uint32_t sample_index, const std::vector<int32_t>& values);
1133 
1145  VariantBuilder& set_float_individual_field(const std::string& tag, const std::string& sample, const float value);
1146 
1156  VariantBuilder& set_float_individual_field(const std::string& tag, const std::string& sample, const std::vector<float>& values);
1157 
1168  VariantBuilder& set_float_individual_field(const uint32_t field_index, const uint32_t sample_index, const float value);
1169 
1177  VariantBuilder& set_float_individual_field(const uint32_t field_index, const uint32_t sample_index, const std::vector<float>& values);
1178 
1188  VariantBuilder& set_string_individual_field(const std::string& tag, const std::string& sample, const std::string& value);
1189 
1197  VariantBuilder& set_string_individual_field(const uint32_t field_index, const uint32_t sample_index, const std::string& value);
1198 
1199 
1200  /******************************************************************************
1201  *
1202  * Functions for removing individual/FORMAT fields
1203  *
1204  ******************************************************************************/
1205 
1213  VariantBuilder& remove_individual_field(const std::string& tag);
1214 
1220  VariantBuilder& remove_individual_field(const uint32_t field_index);
1221 
1229  VariantBuilder& remove_individual_fields(const std::vector<std::string>& tags);
1230 
1236  VariantBuilder& remove_individual_fields(const std::vector<uint32_t>& field_indices);
1237 
1238  /******************************************************************************
1239  *
1240  * Data preparation functions for working with individual fields
1241  *
1242  ******************************************************************************/
1243 
1255  VariantBuilderMultiSampleVector<int32_t> get_genotype_multi_sample_vector(const uint32_t num_samples, const uint32_t max_values_per_sample) const;
1256 
1268  VariantBuilderMultiSampleVector<int32_t> get_integer_multi_sample_vector(const uint32_t num_samples, const uint32_t max_values_per_sample) const;
1269 
1281  VariantBuilderMultiSampleVector<float> get_float_multi_sample_vector(const uint32_t num_samples, const uint32_t max_values_per_sample) const;
1282 
1283 
1284  /******************************************************************************
1285  *
1286  * Builder operations: build() and clear()
1287  *
1288  ******************************************************************************/
1289 
1299  Variant build() const;
1300 
1307  VariantBuilder& clear();
1308 
1309 
1310  /******************************************************************************
1311  *
1312  * Private functionality
1313  *
1314  ******************************************************************************/
1315 
1316  private:
1317  VariantHeader m_header;
1322  VariantBuilderSharedRegion m_shared_region;
1323  VariantBuilderIndividualRegion m_individual_region;
1324  bool m_enable_validation;
1325 
1326  void build_from_scratch(const std::shared_ptr<bcf1_t>& new_variant_body) const;
1327  void post_build_validation(const std::shared_ptr<bcf1_t>& new_variant_body) const;
1328 };
1329 
1330 }
1331 
1332 #endif /* gamgee__variant_builder__guard */
VariantBuilder & set_integer_individual_field(const std::string &tag, const VariantBuilderMultiSampleVector< int32_t > &values_for_all_samples)
Set an integer individual field for all samples at once by name using an efficient flattened (one-dim...
Definition: variant_builder.cpp:258
VariantBuilder & remove_id()
Clear the ID field (if set)
Definition: variant_builder.cpp:108
VariantBuilder & set_alignment_start(const uint32_t alignment_start)
Set the alignment start position.
Definition: variant_builder.cpp:47
VariantBuilder & set_alt_alleles(const std::vector< std::string > &alt_alleles)
Set the alt alleles.
Definition: variant_builder.cpp:77
void set(const FIELD_TYPE value)
Definition: variant_builder.h:26
VariantBuilder & set_chromosome(const uint32_t chromosome)
Set the chromosome by index.
Definition: variant_builder.cpp:36
VariantBuilder & set_ref_allele(const std::string &ref_allele)
Set the reference allele.
Definition: variant_builder.cpp:67
FIELD_TYPE field_value() const
Definition: variant_builder.h:27
VariantBuilder & set_integer_shared_field(const std::string &tag, const int32_t value)
Set a single-valued integer shared field by field name.
Definition: variant_builder.cpp:129
VariantBuilder & remove_filters()
Clear filters (if set)
Definition: variant_builder.cpp:118
VariantBuilderCoreField()
Definition: variant_builder.h:24
VariantBuilder & remove_alignment_stop()
Clear the alignment stop value (if set)
Definition: variant_builder.cpp:98
VariantBuilder & set_boolean_shared_field(const std::string &tag)
Set a boolean (flag) shared field by field name.
Definition: variant_builder.cpp:179
Helper class for VariantBuilder to manage the fields belonging to the shared region of Variant record...
Definition: variant_builder_shared_region.h:56
VariantBuilderMultiSampleVector< float > get_float_multi_sample_vector(const uint32_t num_samples, const uint32_t max_values_per_sample) const
Get a pre-initialized/padded VariantBuilderMultiSampleVector for use with the more-efficient float in...
Definition: variant_builder.cpp:497
VariantBuilder & remove_shared_field(const std::string &tag)
Remove a shared field by field name.
Definition: variant_builder.cpp:195
VariantBuilder & set_genotypes(const VariantBuilderMultiSampleVector< int32_t > &genotypes_for_all_samples)
Set the genotypes (GT) field for all samples at once using an efficient flattened (one-dimensional) v...
Definition: variant_builder.cpp:222
void clear()
Definition: variant_builder.h:29
Class that allows you to efficiently prepare multi-sample data for setting individual fields in Varia...
Definition: variant_builder_multi_sample_vector.h:40
VariantBuilder: construct Variant records from scratch (and, coming soon, from existing Variant recor...
Definition: variant_builder.h:164
VariantBuilder & set_string_shared_field(const std::string &tag, const std::string &value)
Set a string shared field by field name.
Definition: variant_builder.cpp:169
VariantBuilder & set_alignment_stop(const uint32_t alignment_stop)
Set the alignment stop position.
Definition: variant_builder.cpp:52
VariantBuilder & remove_individual_fields(const std::vector< std::string > &tags)
Remove multiple individual fields by field name.
Definition: variant_builder.cpp:472
VariantBuilder & clear()
Clear all field values in this builder to prepare it for the next build operation.
Definition: variant_builder.cpp:523
Internal VariantBuilder class to represent "core" (non-data-region) fields such as the alignment star...
Definition: variant_builder.h:22
Variant build() const
Create a new Variant record using the current state of the builder.
Definition: variant_builder.cpp:510
VariantBuilder & set_float_shared_field(const std::string &tag, const float value)
Set a single-valued float shared field by field name.
Definition: variant_builder.cpp:149
Definition: exceptions.h:9
Helper class for VariantBuilder to manage the fields belonging to the individual region of Variant re...
Definition: variant_builder_individual_region.h:23
VariantBuilderMultiSampleVector< int32_t > get_integer_multi_sample_vector(const uint32_t num_samples, const uint32_t max_values_per_sample) const
Get a pre-initialized/padded VariantBuilderMultiSampleVector for use with the more-efficient integer ...
Definition: variant_builder.cpp:493
VariantBuilderMultiSampleVector< int32_t > get_genotype_multi_sample_vector(const uint32_t num_samples, const uint32_t max_values_per_sample) const
Get a pre-initialized/padded VariantBuilderMultiSampleVector for use with the more-efficient GT field...
Definition: variant_builder.cpp:489
VariantBuilder & set_genotype(const std::string &sample, const std::vector< int32_t > &genotype)
Set the genotypes (GT) field for a single sample by sample name, copying the genotype before encoding...
Definition: variant_builder.cpp:372
VariantBuilder & remove_individual_field(const std::string &tag)
Remove an individual field by field name.
Definition: variant_builder.cpp:462
VariantBuilder & set_filters(const std::vector< std::string > &filters)
Set the filters using filter names.
Definition: variant_builder.cpp:82
VariantBuilder & set_enable_validation(const bool enable_validation)
Disable or enable all validation checks in this builder.
Definition: variant_builder.cpp:23
Utility class to manipulate a Variant record.
Definition: variant.h:29
VariantBuilder & remove_qual()
Clear the qual field (if set)
Definition: variant_builder.cpp:103
bool is_set() const
Definition: variant_builder.h:28
Utility class to hold a variant header.
Definition: variant_header.h:52
VariantHeader header() const
Return the Variant header for this builder.
Definition: variant_builder.h:193
VariantBuilder & set_qual(const float qual)
Set the Phred-scaled site quality (probability that the site is not reference)
Definition: variant_builder.cpp:57
VariantBuilder(const VariantHeader &header)
Construct a new VariantBuilder given a VariantHeader.
Definition: variant_builder.cpp:12
VariantBuilder & set_id(const std::string &id)
Set the variant ID field.
Definition: variant_builder.cpp:62
VariantBuilder & set_alt_allele(const std::string &alt_allele)
Set the alt allele.
Definition: variant_builder.cpp:72
VariantBuilder & remove_alt_alleles()
Clear alt alleles (if set)
Definition: variant_builder.cpp:113
VariantBuilder & remove_shared_fields(const std::vector< std::string > &tags)
Remove multiple shared fields by field name.
Definition: variant_builder.cpp:205
VariantBuilder & set_float_individual_field(const std::string &tag, const VariantBuilderMultiSampleVector< float > &values_for_all_samples)
Set a float individual field for all samples at once by name using an efficient flattened (one-dimens...
Definition: variant_builder.cpp:302
VariantBuilder & operator=(VariantBuilder &&other)=default
VariantBuilder & set_string_individual_field(const std::string &tag, const std::vector< std::string > &values_for_all_samples)
Set a string individual field for all samples at once by name, copying the provided values...
Definition: variant_builder.cpp:346