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

Commit

Permalink
Address reviewer's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauricio Carneiro committed Aug 20, 2014
1 parent 264a9a0 commit 1ad948f
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 85 deletions.
105 changes: 47 additions & 58 deletions gamgee/variant.cpp
Expand Up @@ -61,14 +61,6 @@ Variant::Variant(const Variant& other) :
m_body {utils::make_shared_variant(utils::variant_deep_copy(other.m_body.get()))}
{}

/**
* @brief moves a variant record and header, transferring ownership of the underlying htslib memory
*/
Variant::Variant(Variant&& other) noexcept :
m_header {move(other.m_header)},
m_body {move(other.m_body)}
{}

/**
* @brief creates a deep copy of a variant record
* @param other the Variant to be copied
Expand All @@ -84,16 +76,6 @@ Variant& Variant::operator=(const Variant& other) {
return *this;
}

/**
* @brief moves a variant record, transferring ownership of the underlying htslib memory
*/
Variant& Variant::operator=(Variant&& other) noexcept {
if ( &other == this )
return *this;
m_body = move(other.m_body);
m_header = move(other.m_header);
return *this;
}

/******************************************************************************
* General record API *
Expand Down Expand Up @@ -127,24 +109,15 @@ bool Variant::has_filter(const std::string& filter) const {
* Individual field API *
******************************************************************************/
IndividualField<IndividualFieldValue<int32_t>> Variant::integer_individual_field(const std::string& tag) const {
const auto index = get_field_index(tag);
if (check_field(BCF_HL_FMT, BCF_HT_INT, index))
return individual_field_as_integer(index);
return IndividualField<IndividualFieldValue<int32_t>>{};
return integer_individual_field(get_field_index(tag));
}

IndividualField<IndividualFieldValue<float>> Variant::float_individual_field(const std::string& tag) const {
const auto index = get_field_index(tag);
if (check_field(BCF_HL_FMT, BCF_HT_REAL, index))
return individual_field_as_float(index);
return IndividualField<IndividualFieldValue<float>>{};
return float_individual_field(get_field_index(tag));
}

IndividualField<IndividualFieldValue<string>> Variant::string_individual_field(const std::string& tag) const {
const auto index = get_field_index(tag);
if (check_field(BCF_HL_FMT, BCF_HT_STR, index))
return individual_field_as_string(index);
return IndividualField<IndividualFieldValue<string>>{};
return string_individual_field(get_field_index(tag));
}

IndividualField<IndividualFieldValue<int32_t>> Variant::individual_field_as_integer(const std::string& tag) const {
Expand All @@ -159,6 +132,24 @@ IndividualField<IndividualFieldValue<std::string>> Variant::individual_field_as_
return individual_field_as<string>(tag);
}

IndividualField<IndividualFieldValue<int32_t>> Variant::integer_individual_field(const int32_t index) const {
if (check_field(BCF_HL_FMT, BCF_HT_INT, index))
return individual_field_as<int32_t>(index);
return IndividualField<IndividualFieldValue<int32_t>>{};
}

IndividualField<IndividualFieldValue<float>> Variant::float_individual_field(const int32_t index) const {
if (check_field(BCF_HL_FMT, BCF_HT_REAL, index))
return individual_field_as<float>(index);
return IndividualField<IndividualFieldValue<float>>{};
}

IndividualField<IndividualFieldValue<string>> Variant::string_individual_field(const int32_t index) const {
if (check_field(BCF_HL_FMT, BCF_HT_STR, index))
return individual_field_as<string>(index);
return IndividualField<IndividualFieldValue<string>>{};
}

IndividualField<IndividualFieldValue<int32_t>> Variant::individual_field_as_integer(const int32_t index) const {
return individual_field_as<int32_t>(index);
}
Expand All @@ -176,55 +167,53 @@ IndividualField<IndividualFieldValue<std::string>> Variant::individual_field_as_
* Shared field API *
******************************************************************************/
bool Variant::boolean_shared_field(const std::string& tag) const {
const auto info = find_shared_field(tag);
return info != nullptr;
return find_shared_field(tag) != nullptr;
}

bool Variant::boolean_shared_field(const int32_t index) const {
const auto info = find_shared_field(index);
return info != nullptr;
return find_shared_field(index) != nullptr;
}

SharedField<int32_t> Variant::integer_shared_field(const std::string& tag) const {
const auto index = get_field_index(tag);
if (check_field(BCF_HL_INFO, BCF_HT_INT, index))
return shared_field_as_integer(index);
return SharedField<int32_t>{};
return integer_shared_field(get_field_index(tag));
}

SharedField<float> Variant::float_shared_field(const std::string& tag) const {
const auto index = get_field_index(tag);
if (check_field(BCF_HL_INFO, BCF_HT_REAL, index))
return shared_field_as_float(index);
return SharedField<float>{};
return float_shared_field(get_field_index(tag));
}

SharedField<string> Variant::string_shared_field(const std::string& tag) const {
const auto index = get_field_index(tag);
if (check_field(BCF_HL_INFO, BCF_HT_STR, index))
return shared_field_as_string(index);
return SharedField<string>{};
return string_shared_field(get_field_index(tag));
}

SharedField<int32_t> Variant::shared_field_as_integer(const std::string& tag) const {
const auto info = find_shared_field(tag);
if (info == nullptr)
return SharedField<int32_t>{};
return SharedField<int32_t>{m_body, info};
return shared_field_as<int32_t>(tag);
}

SharedField<float> Variant::shared_field_as_float(const std::string& tag) const {
const auto info = find_shared_field(tag);
if (info == nullptr)
return SharedField<float>{};
return SharedField<float>{m_body, info};
return shared_field_as<float>(tag);
}

SharedField<string> Variant::shared_field_as_string(const std::string& tag) const {
const auto info = find_shared_field(tag);
if (info == nullptr)
return SharedField<string>{};
return SharedField<string>{m_body, info};
return shared_field_as<string>(tag);
}

SharedField<int32_t> Variant::integer_shared_field(const int32_t index) const {
if (check_field(BCF_HL_INFO, BCF_HT_INT, index))
return shared_field_as<int32_t>(index);
return SharedField<int32_t>{};
}

SharedField<float> Variant::float_shared_field(const int32_t index) const {
if (check_field(BCF_HL_INFO, BCF_HT_REAL, index))
return shared_field_as<float>(index);
return SharedField<float>{};
}

SharedField<string> Variant::string_shared_field(const int32_t index) const {
if (check_field(BCF_HL_INFO, BCF_HT_STR, index))
return shared_field_as<string>(index);
return SharedField<string>{};
}

SharedField<int32_t> Variant::shared_field_as_integer(const int32_t index) const {
Expand Down
28 changes: 17 additions & 11 deletions gamgee/variant.h
Expand Up @@ -29,9 +29,9 @@ class Variant {
Variant() = default; ///< initializes a null Variant @note this is only used internally by the iterators @warning if you need to create a Variant from scratch, use the builder instead
explicit Variant(const std::shared_ptr<bcf_hdr_t>& header, const std::shared_ptr<bcf1_t>& body) noexcept; ///< creates a Variant given htslib objects. @note used by all iterators
Variant(const Variant& other); ///< makes a deep copy of a Variant and it's header. Shared pointers maintain state to all other associated objects correctly.
Variant(Variant&& other) noexcept; ///< moves Variant and it's header accordingly. Shared pointers maintain state to all other associated objects correctly.
Variant& operator=(const Variant& other); ///< deep copy assignment of a Variant and it's header. Shared pointers maintain state to all other associated objects correctly.
Variant& operator=(Variant&& other) noexcept; ///< move assignment of a Variant and it's header. Shared pointers maintain state to all other associated objects correctly.
Variant(Variant&& other) = default; ///< moves Variant and it's header accordingly. Shared pointers maintain state to all other associated objects correctly.
Variant& operator=(Variant&& other) = default; ///< move assignment of a Variant and it's header. Shared pointers maintain state to all other associated objects correctly.

VariantHeader header() const { return VariantHeader{m_header}; }

Expand All @@ -53,15 +53,18 @@ class Variant {

// 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.
IndividualField<IndividualFieldValue<int32_t>> integer_individual_field(const std::string& tag) const; ///< returns a random access object with all the values in a given individual field tag in integer format 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.
IndividualField<IndividualFieldValue<float>> float_individual_field(const std::string& tag) const; ///< returns a random access object with all the values in a given individual field tag in float format 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.
IndividualField<IndividualFieldValue<std::string>> string_individual_field(const std::string& tag) const; ///< returns a random access object with all the values in a given individual field tag in string format 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.
IndividualField<IndividualFieldValue<int32_t>> individual_field_as_integer(const std::string& tag) const; ///< same as integer_format_field but will attempt to convert underlying data to integer if possible. @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.
IndividualField<IndividualFieldValue<float>> individual_field_as_float(const std::string& tag) const; ///< same as float_format_field but will attempt to convert underlying data to float if possible. @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.
IndividualField<IndividualFieldValue<std::string>> individual_field_as_string(const std::string& tag) const; ///< same as string_format_field but will attempt to convert underlying data to string if possible. @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.
IndividualField<IndividualFieldValue<int32_t>> individual_field_as_integer(const int32_t index) const;
IndividualField<IndividualFieldValue<float>> individual_field_as_float(const int32_t index) const;
IndividualField<IndividualFieldValue<std::string>> individual_field_as_string(const int32_t index) const;
IndividualField<IndividualFieldValue<int32_t>> integer_individual_field(const std::string& tag) const; ///< returns a random access object with all the values in a given individual field tag in integer format for all samples contiguous in memory. @warning creates a new object but makes no copies of the underlying values.
IndividualField<IndividualFieldValue<float>> float_individual_field(const std::string& tag) const; ///< returns a random access object with all the values in a given individual field tag in float format for all samples contiguous in memory. @warning creates a new object but makes no copies of the underlying values.
IndividualField<IndividualFieldValue<std::string>> string_individual_field(const std::string& tag) const; ///< returns a random access object with all the values in a given individual field tag in string format for all samples contiguous in memory. @warning creates a new object but makes no copies of the underlying values.
IndividualField<IndividualFieldValue<int32_t>> individual_field_as_integer(const std::string& tag) const; ///< same as integer_individual_field but will attempt to convert underlying data to integer if possible. @warning creates a new object but makes no copies of the underlying values.
IndividualField<IndividualFieldValue<float>> individual_field_as_float(const std::string& tag) const; ///< same as float_individual_field but will attempt to convert underlying data to float if possible. @warning creates a new object but makes no copies of the underlying values.
IndividualField<IndividualFieldValue<std::string>> individual_field_as_string(const std::string& tag) const; ///< same as string_individual_field but will attempt to convert underlying data to string if possible. @warning Only int8_t GT fields have been tested.
IndividualField<IndividualFieldValue<int32_t>> integer_individual_field(const int32_t index) const; ///< returns a random access object with all the values in a given individual field tag index in integer format for all samples contiguous in memory. @warning creates a new object but makes no copies of the underlying values.
IndividualField<IndividualFieldValue<float>> float_individual_field(const int32_t index) const; ///< returns a random access object with all the values in a given individual field tag index in float format for all samples contiguous in memory. @warning creates a new object but makes no copies of the underlying values.
IndividualField<IndividualFieldValue<std::string>> string_individual_field(const int32_t index) const; ///< returns a random access object with all the values in a given individual field tag index in string format for all samples contiguous in memory. @warning creates a new object but makes no copies of the underlying values.
IndividualField<IndividualFieldValue<int32_t>> individual_field_as_integer(const int32_t index) const; ///< same as integer_individual_field but will attempt to convert underlying data to integer if possible. @warning creates a new object but makes no copies of the underlying values.
IndividualField<IndividualFieldValue<float>> individual_field_as_float(const int32_t index) const; ///< same as float_individual_field but will attempt to convert underlying data to float if possible. @warning creates a new object but makes no copies of the underlying values.
IndividualField<IndividualFieldValue<std::string>> individual_field_as_string(const int32_t index) const; ///< same as string_individual_field but will attempt to convert underlying data to string if possible. @warning creates a new object but makes no copies of the underlying values.

// shared field getters (a.k.a "info fields")
bool boolean_shared_field(const std::string& tag) const; ///< whether or not the tag is present @note bools are treated specially as vector<bool> is impossible given the spec
Expand All @@ -72,6 +75,9 @@ class Variant {
SharedField<float> shared_field_as_float(const std::string& tag) const; ///< same as float_shared_field but will attempt to convert underlying data to float if possible. @warning creates a new object but makes no copies of the underlying values.
SharedField<std::string> shared_field_as_string(const std::string& tag) const; ///< same as string_shared_field but will attempt to convert underlying data to string if possible. @warning creates a new object but makes no copies of the underlying values.
bool boolean_shared_field(const int32_t index) const; ///< whether or not the tag with this index is present @note bools are treated specially as vector<bool> is impossible given the spec
SharedField<int32_t> integer_shared_field(const int32_t index) const; ///< same as integer_shared_field but will attempt to convert underlying data to integer if possible. @warning creates a new object but makes no copies of the underlying values.
SharedField<float> float_shared_field(const int32_t index) const; ///< same as float_shared_field but will attempt to convert underlying data to float if possible. @warning creates a new object but makes no copies of the underlying values.
SharedField<std::string> string_shared_field(const int32_t index) const; ///< same as string_shared_field but will attempt to convert underlying data to string if possible. @warning creates a new object but makes no copies of the underlying values.
SharedField<int32_t> shared_field_as_integer(const int32_t index) const; ///< same as integer_shared_field but will attempt to convert underlying data to integer if possible. @warning creates a new object but makes no copies of the underlying values.
SharedField<float> shared_field_as_float(const int32_t index) const; ///< same as float_shared_field but will attempt to convert underlying data to float if possible. @warning creates a new object but makes no copies of the underlying values.
SharedField<std::string> shared_field_as_string(const int32_t index) const; ///< same as string_shared_field but will attempt to convert underlying data to string if possible. @warning creates a new object but makes no copies of the underlying values.
Expand Down
2 changes: 1 addition & 1 deletion gamgee/variant_header.cpp
Expand Up @@ -80,7 +80,7 @@ bool VariantHeader::has_individual_field(const string field) const {
return find(fields.begin(), fields.end(), field) != fields.end();
}

int32_t VariantHeader::shared_field_index(const string& tag) const {
int32_t VariantHeader::field_index(const string& tag) const {
const auto index = bcf_hdr_id2int(m_header.get(), BCF_DT_ID, tag.c_str());
return index >= 0 ? index : missing_values::int32;
}
Expand Down

0 comments on commit 1ad948f

Please sign in to comment.