From 3a79ceb6e06a5d7377adf6fda5b37f58eb2c06bc Mon Sep 17 00:00:00 2001 From: Karthik Gururaj Date: Mon, 1 Dec 2014 17:27:05 -0800 Subject: [PATCH] Added functions in VariantHeader to query field length descriptors and field lengths. Will be useful in CombineGVCFs --- gamgee/variant/variant_header.h | 25 ++++++++++++++++++++++++- test/variant_test.cpp | 16 ++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/gamgee/variant/variant_header.h b/gamgee/variant/variant_header.h index aca333643..50037f513 100644 --- a/gamgee/variant/variant_header.h +++ b/gamgee/variant/variant_header.h @@ -123,7 +123,30 @@ class VariantHeader { * @note must check whether the field exists before calling this function, as it doesn't check for you */ uint8_t field_type(const int32_t index, const int32_t field_category) const { return bcf_hdr_id2type(m_header.get(), field_category, index); } - + /* + * returns one of BCF_VL_* values for field with the specified name and category (one of BCF_HL_FMT, BCF_HL_INFO, or BCF_HL_FLT) + * + * @note must check whether the field exists before calling this function, as it doesn't check for you + */ + uint32_t field_length_descriptor(const std::string& tag, const int32_t field_category) const { return field_length_descriptor(field_index(tag), field_category); } + /** + * returns one of BCF_VL_* values for the field with the specified index and category (one of BCF_HL_FMT, BCF_HL_INFO, or BCF_HL_FLT) + * + * @note must check whether the field exists before calling this function, as it doesn't check for you + */ + uint32_t field_length_descriptor(const int32_t index, const int32_t field_category) const { return bcf_hdr_id2length(m_header.get(), field_category, index); } + /** + * returns number of values for the field with the specified name and category (one of BCF_HL_FMT, BCF_HL_INFO, or BCF_HL_FLT), 0xfffff for variable length fields + * + * @note must check whether the field exists before calling this function, as it doesn't check for you + */ + uint32_t field_length(const std::string& tag, const int32_t field_category) const { return field_length(field_index(tag), field_category); } + /** + * returns number of values for the field with the specified index and category (one of BCF_HL_FMT, BCF_HL_INFO, or BCF_HL_FLT), 0xfffff for variable length fields + * + * @note must check whether the field exists before calling this function, as it doesn't check for you + */ + uint32_t field_length(const int32_t index, const int32_t field_category) const { return bcf_hdr_id2number(m_header.get(), field_category, index); } /** * @brief checks whether the given filter is present given the filter name */ diff --git a/test/variant_test.cpp b/test/variant_test.cpp index 458213768..082e53528 100644 --- a/test/variant_test.cpp +++ b/test/variant_test.cpp @@ -112,3 +112,19 @@ BOOST_AUTO_TEST_CASE( test_missing_string_individual_field_values ) { BOOST_CHECK(missing(variant.string_individual_field("AS")[1])); BOOST_CHECK(! missing(variant.string_individual_field("AS")[2])); } + +BOOST_AUTO_TEST_CASE( test_field_lengths ) { + auto header = SingleVariantReader{"testdata/test_variants.vcf"}.header(); + BOOST_CHECK_EQUAL(header.field_length_descriptor("AF", BCF_HL_INFO), static_cast(BCF_VL_A)); + BOOST_CHECK_EQUAL(header.field_length_descriptor("AF", BCF_HL_FMT), static_cast(BCF_VL_FIXED)); + BOOST_CHECK_EQUAL(header.field_length_descriptor("PL", BCF_HL_FMT), static_cast(BCF_VL_G)); + BOOST_CHECK_EQUAL(header.field_length_descriptor("AS", BCF_HL_FMT), static_cast(BCF_VL_FIXED)); + BOOST_CHECK_EQUAL(header.field_length_descriptor("VLINT", BCF_HL_FMT), static_cast(BCF_VL_VAR)); + + BOOST_CHECK_EQUAL(header.field_length("AF", BCF_HL_INFO), 0xfffffu); + BOOST_CHECK_EQUAL(header.field_length("AF", BCF_HL_FMT), 2u); + BOOST_CHECK_EQUAL(header.field_length("PL", BCF_HL_FMT), 0xfffffu); + BOOST_CHECK_EQUAL(header.field_length("AS", BCF_HL_FMT), 1u); + BOOST_CHECK_EQUAL(header.field_length("VLINT", BCF_HL_FMT), 0xfffffu); +} +