Header

Struct Header 

Source
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

Source

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.

Source

pub fn empty() -> Self

Create an empty header (for writing new VCFs).

Source

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.

Source

pub fn translate_view(&self) -> Arc<HeaderView>

Source

pub fn header_records(&self) -> Vec<HeaderRecord>

Get all header records (INFO, FORMAT, FILTER, contig, etc.).

Source

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.

Source

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.

Source

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).

Source

pub fn sample_count(&self) -> usize

Get the number of samples in the VCF.

Source

pub fn sample_names(&self) -> &[String]

Get all sample names in header order.

Source

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.

Source

pub fn sample_name_to_idx(&self) -> &HashMap<String, usize>

Get a reference to the sample name-to-index map.

Source

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.

Source

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.

Source

pub fn sync(&self)

Synchronize header changes to the underlying htslib structure.

Called automatically after modifications; usually not needed directly.

Source

pub fn push_record(&self, record: &[u8]) -> bool

Append a raw header line (e.g., ##INFO=<...>).

Returns true on success, false on failure.

Source

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.

Source

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.

Source

pub fn to_string(&self) -> Option<String>

Format the header as a VCF header string.

Returns None if formatting fails.

Source

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.

Source

pub fn all_fields(&self) -> Vec<(String, HeaderField)>

Get all INFO, FORMAT, and FILTER field definitions.

Returns a vector of (section, field) tuples.

Trait Implementations§

Source§

impl Debug for Header

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Header

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for Header

Source§

impl Sync for Header

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> ErasedDestructor for T
where T: 'static,