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

Commit

Permalink
Add sample_index to VariantHeader (analogous to field_index)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmthibault79 committed Oct 6, 2014
1 parent e5f0438 commit 01b9b8f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions gamgee/variant_header.cpp
Expand Up @@ -136,4 +136,9 @@ int32_t VariantHeader::field_index(const string& tag) const {
return index >= 0 ? index : missing_values::int32;
}

int32_t VariantHeader::sample_index(const string& tag) const {
const auto index = bcf_hdr_id2int(m_header.get(), BCF_DT_SAMPLE, tag.c_str());
return index >= 0 ? index : missing_values::int32;
}

}
6 changes: 6 additions & 0 deletions gamgee/variant_header.h
Expand Up @@ -63,6 +63,12 @@ class VariantHeader {
* @note if multiple fields (e.g. shared and individual) have the same tag (e.g. "DP"), they will also have the same index internally, so this function will do the right thing. The accessors for individual and shared field will know how to use the index to retrieve the correct field.
*/
int32_t field_index(const std::string& tag) const;
/**
* @brief looks up the index of a particular sample, enabling subsequent O(1) random-access lookups for that sample throughout the iteration.
* @return missing_values::int32_t if the tag is not present in the header (you can use missing() on the return value to check)
* @note prefer this to looking up sample names during the iteration if you are looking for samples multiple times.
*/
int32_t sample_index(const std::string& tag) const;

private:
std::shared_ptr<bcf_hdr_t> m_header;
Expand Down
3 changes: 3 additions & 0 deletions test/variant_header_test.cpp
Expand Up @@ -15,6 +15,7 @@ void check_fields(T actual, T truth) {
}

const auto samples = vector<string>{"S1", "S292", "S30034"};
const auto samples_indices = vector<int32_t>{0, 1, 2};
const auto chromosomes = vector<string>{"chr1", "chr2", "chr3", "chr4"};
const auto filters = vector<string>{"LOW_QUAL", "PASS", "VQSR_FAILED"};
const auto shareds = vector<string>{"DP", "MQ", "RankSum"};
Expand All @@ -41,6 +42,8 @@ void variant_header_builder_checks(const VariantHeader& vh) {
BOOST_CHECK_EQUAL(shareds_indices[i], vh.field_index(shareds[i]));
for (auto i = 0u; i != individuals.size(); ++i)
BOOST_CHECK_EQUAL(individuals_indices[i], vh.field_index(individuals[i]));
for (auto i = 0u; i != samples.size(); ++i)
BOOST_CHECK_EQUAL(samples_indices[i], vh.sample_index(samples[i]));
BOOST_CHECK(missing(vh.field_index("MISSING")));
BOOST_CHECK(missing(vh.field_index("MISSING")));
BOOST_CHECK_EQUAL(vh.field_index("PASS"), 0);
Expand Down

0 comments on commit 01b9b8f

Please sign in to comment.