pub struct Header { /* private fields */ }Expand description
A VCF/BCF header with metadata and sample information.
Header owns its underlying bcf_hdr_t* via duplication. It provides
access to sample names, INFO/FORMAT field definitions, and supports
header modification (adding INFO/FORMAT fields).
§Caching
Sample names and tag ID-to-name mappings are cached at construction time for O(1) lookups during variant processing.
Implementations§
Source§impl Header
impl Header
Sourcepub unsafe fn new(inner: *mut bcf_hdr_t) -> Self
pub unsafe fn new(inner: *mut bcf_hdr_t) -> Self
Duplicate the underlying header and take ownership.
This is important for thread safety and correct lifetime management: the
returned Header owns its internal bcf_hdr_t* and frees it on drop.
§Safety
inner must be a valid pointer to a bcf_hdr_t.
Sourcepub fn inner_ptr(&self) -> *mut bcf_hdr_t
pub fn inner_ptr(&self) -> *mut bcf_hdr_t
Get the raw bcf_hdr_t pointer.
§Safety
The returned pointer is valid for the lifetime of this Header.
pub fn translate_view(&self) -> Arc<HeaderView>
Sourcepub fn header_records(&self) -> Vec<HeaderRecord>
pub fn header_records(&self) -> Vec<HeaderRecord>
Get all header records (INFO, FORMAT, FILTER, contig, etc.).
Sourcepub fn sample_id(&self, sample: &[u8]) -> Option<usize>
pub fn sample_id(&self, sample: &[u8]) -> Option<usize>
Get the sample index for a sample name.
Returns None if the sample is not found.
Sourcepub fn id_to_name(&self, id: u32) -> Vec<u8> ⓘ
pub fn id_to_name(&self, id: u32) -> Vec<u8> ⓘ
Get the tag name for a numeric ID (INFO/FORMAT).
This performs a fresh lookup; prefer id_to_name_cached
for repeated calls.
Sourcepub fn id_to_name_cached(&self, id: u32) -> (String, Vec<u8>)
pub fn id_to_name_cached(&self, id: u32) -> (String, Vec<u8>)
Get the cached name for a tag ID, returning both String and bytes.
Falls back to id_to_name if not in cache
(e.g., for dynamically added tags).
Sourcepub fn sample_count(&self) -> usize
pub fn sample_count(&self) -> usize
Get the number of samples in the VCF.
Sourcepub fn sample_names(&self) -> &[String]
pub fn sample_names(&self) -> &[String]
Get all sample names in header order.
Sourcepub fn sample_idx(&self, name: &str) -> Option<usize>
pub fn sample_idx(&self, name: &str) -> Option<usize>
Get the index of a sample by name.
Returns None if the sample is not found.
Sourcepub fn sample_name_to_idx(&self) -> &HashMap<String, usize>
pub fn sample_name_to_idx(&self) -> &HashMap<String, usize>
Get a reference to the sample name-to-index map.
Sourcepub fn info_type(&self, tag: &[u8]) -> Option<(TagType, TagLength)>
pub fn info_type(&self, tag: &[u8]) -> Option<(TagType, TagLength)>
Get the type and length of an INFO field.
Returns None if the tag is not defined in the header.
Sourcepub fn format_type(&self, tag: &[u8]) -> Option<(TagType, TagLength)>
pub fn format_type(&self, tag: &[u8]) -> Option<(TagType, TagLength)>
Get the type and length of a FORMAT field.
Returns None if the tag is not defined in the header.
Sourcepub fn sync(&self)
pub fn sync(&self)
Synchronize header changes to the underlying htslib structure.
Called automatically after modifications; usually not needed directly.
Sourcepub fn push_record(&self, record: &[u8]) -> bool
pub fn push_record(&self, record: &[u8]) -> bool
Append a raw header line (e.g., ##INFO=<...>).
Returns true on success, false on failure.
Sourcepub fn add_info(
&self,
id: &str,
number: &str,
ty: &str,
description: &str,
) -> bool
pub fn add_info( &self, id: &str, number: &str, ty: &str, description: &str, ) -> bool
Add an INFO field definition to the header.
§Arguments
id- Field ID (e.g., “DP”)number- Number field (“1”, “A”, “R”, “G”, “.”)ty- Type (“Integer”, “Float”, “String”, “Flag”)description- Human-readable description
Returns true on success.
Sourcepub fn add_format(
&self,
id: &str,
number: &str,
ty: &str,
description: &str,
) -> bool
pub fn add_format( &self, id: &str, number: &str, ty: &str, description: &str, ) -> bool
Add a FORMAT field definition to the header.
§Arguments
id- Field ID (e.g., “GQ”)number- Number field (“1”, “A”, “R”, “G”, “.”)ty- Type (“Integer”, “Float”, “String”)description- Human-readable description
Returns true on success.
Sourcepub fn to_string(&self) -> Option<String>
pub fn to_string(&self) -> Option<String>
Format the header as a VCF header string.
Returns None if formatting fails.
Sourcepub fn get_field(&self, section: &str, id: &str) -> Option<HeaderField>
pub fn get_field(&self, section: &str, id: &str) -> Option<HeaderField>
Get a specific field definition by section and ID.
§Arguments
section- “INFO” or “FORMAT”id- Field ID (e.g., “DP”)
Returns None if the field is not found.
Sourcepub fn all_fields(&self) -> Vec<(String, HeaderField)>
pub fn all_fields(&self) -> Vec<(String, HeaderField)>
Get all INFO, FORMAT, and FILTER field definitions.
Returns a vector of (section, field) tuples.