Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
Enable type checking of shared and individual fields
Browse files Browse the repository at this point in the history
- Fixes #257
  • Loading branch information
jmthibault79 committed Sep 19, 2014
1 parent 526008e commit 1143ab2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
16 changes: 16 additions & 0 deletions gamgee/variant.cpp
Expand Up @@ -108,6 +108,14 @@ bool Variant::has_filter(const std::string& filter) const {
/******************************************************************************
* Individual field API *
******************************************************************************/
uint8_t Variant::individual_field_type(const std::string& tag) const {
return individual_field_type(get_field_index(tag));
}

uint8_t Variant::individual_field_type(const int32_t index) const {
return bcf_hdr_id2type(m_header.get(), BCF_HL_FMT, index);
}

IndividualField<IndividualFieldValue<int32_t>> Variant::integer_individual_field(const std::string& tag) const {
return integer_individual_field(get_field_index(tag));
}
Expand Down Expand Up @@ -166,6 +174,14 @@ IndividualField<IndividualFieldValue<std::string>> Variant::individual_field_as_
/******************************************************************************
* Shared field API *
******************************************************************************/
uint8_t Variant::shared_field_type(const std::string& tag) const {
return shared_field_type(get_field_index(tag));
}

uint8_t Variant::shared_field_type(const int32_t index) const {
return bcf_hdr_id2type(m_header.get(), BCF_HL_INFO, index);
}

bool Variant::boolean_shared_field(const std::string& tag) const {
return find_shared_field(tag) != nullptr;
}
Expand Down
5 changes: 5 additions & 0 deletions gamgee/variant.h
Expand Up @@ -50,6 +50,11 @@ class Variant {
VariantFilters filters() const; ///< returns a vector-like object with all the filters for this record
bool has_filter(const std::string& filter) const; ///< checks for the existence of a filter in this record

// type checking functions: returns BCF_HT_FLAG, BCF_HT_INT, BCF_HT_REAL, BCF_HT_STR from htslib/vcf.h
uint8_t shared_field_type(const std::string& tag) const; ///< returns the type of this shared (info) field
uint8_t shared_field_type(const int32_t index) const; ///< returns the type of this shared (info) field
uint8_t individual_field_type(const std::string& tag) const; ///< returns the type of this individual (format) field
uint8_t individual_field_type(const int32_t index) const; ///< returns the type of this individual (format) field

// individual field getters (a.k.a "format fields")
IndividualField<Genotype> genotypes() const; ///< special getter for the Genotype (GT) field. Returns a random access object with all the values in a given GT tag for all samples contiguous in memory. @warning Only int8_t GT fields have been tested. @warning Missing GT fields are untested. @warning creates a new object but makes no copies of the underlying values.
Expand Down
17 changes: 17 additions & 0 deletions test/variant_reader_test.cpp
Expand Up @@ -179,6 +179,14 @@ void check_individual_field_api(const Variant& record, const uint32_t truth_inde
BOOST_CHECK(pl_float != record.individual_field_as_float("GQ"));
BOOST_CHECK(as_string != record.individual_field_as_string("PL"));

// check types
BOOST_CHECK_EQUAL(record.individual_field_type("GQ"), BCF_HT_INT);
BOOST_CHECK_EQUAL(record.individual_field_type("AF"), BCF_HT_REAL);
BOOST_CHECK_EQUAL(record.individual_field_type("AS"), BCF_HT_STR);
BOOST_CHECK_EQUAL(record.individual_field_type(header.field_index("GQ")), BCF_HT_INT);
BOOST_CHECK_EQUAL(record.individual_field_type(header.field_index("AF")), BCF_HT_REAL);
BOOST_CHECK_EQUAL(record.individual_field_type(header.field_index("AS")), BCF_HT_STR);

for(auto i=0u; i != record.n_samples(); ++i) {
BOOST_CHECK_EQUAL(gq_int[i][0], truth_gq[truth_index][i]);
BOOST_CHECK_EQUAL(gq_int_idx[i][0], truth_gq[truth_index][i]);
Expand Down Expand Up @@ -258,6 +266,15 @@ void check_shared_field_api(const Variant& record, const uint32_t truth_index) {
BOOST_CHECK(missing(record.integer_shared_field(-1)));
BOOST_CHECK(missing(record.float_shared_field(-1)));
BOOST_CHECK(missing(record.string_shared_field(-1)));
// type checking
BOOST_CHECK_EQUAL(record.shared_field_type("VALIDATED"), BCF_HT_FLAG);
BOOST_CHECK_EQUAL(record.shared_field_type("AN"), BCF_HT_INT);
BOOST_CHECK_EQUAL(record.shared_field_type("AF"), BCF_HT_REAL);
BOOST_CHECK_EQUAL(record.shared_field_type("DESC"), BCF_HT_STR);
BOOST_CHECK_EQUAL(record.shared_field_type(header.field_index("VALIDATED")), BCF_HT_FLAG);
BOOST_CHECK_EQUAL(record.shared_field_type(header.field_index("AN")), BCF_HT_INT);
BOOST_CHECK_EQUAL(record.shared_field_type(header.field_index("AF")), BCF_HT_REAL);
BOOST_CHECK_EQUAL(record.shared_field_type(header.field_index("DESC")), BCF_HT_STR);
// check type conversions in the unforgiving API
BOOST_CHECK_THROW(record.float_shared_field("AN"), runtime_error);
BOOST_CHECK_THROW(record.string_shared_field("AN"), runtime_error);
Expand Down

0 comments on commit 1143ab2

Please sign in to comment.