Header

Struct Header 

Source
pub struct Header { /* private fields */ }
Expand description

Represents a header of a BCF file.

The Header struct contains information about the dictionar of strings and contigs, samples, and the index of the FORMAT field “GT”. It provides methods to parse a header from a string, retrieve the index of a dictionary string, retrieve the chromosome name by index, retrieve the index of the FORMAT field “GT”, and access the dictionary strings, contigs, and samples.

§Examples

use bcf_reader::Header;

let header_text = concat!(
    r#"##fileformat=VCFv4.3"#,
    "\n",
    r#"##FILTER=<ID=PASS,Description="All filters passed">"#,
    "\n",
    r#"##FILTER=<ID=FAILED1,Description="failed due to something">"#,
    "\n",
    r#"##contig=<ID=chr1,length=123123123>"#,
    "\n",
    r#"##INFO=<ID=DP,Number=1,Type=Integer,Description="Total Depth">"#,
    "\n",
    r#"##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">"#,
    "\n",
    "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\tsample1\tsample2",
    "\n",
);

let header = Header::from_string(&header_text).unwrap();

assert_eq!(header.get_idx_from_dictionary_str("INFO", "DP"), Some(2));
assert_eq!(header.get_chrname(0), "chr1");
assert_eq!(header.get_fmt_gt_id(), Some(3));
assert_eq!(header.dict_contigs().len(), 1);
assert_eq!(header.dict_strings().len(), 4);
assert_eq!(header.get_samples().len(), 2);

Implementations§

Source§

impl Header

Source

pub fn from_string(text: &str) -> Result<Self, ParseHeaderError>

parse header lines to structured data Header

Source

pub fn get_idx_from_dictionary_str( &self, dictionary: &str, field: &str, ) -> Option<usize>

Find the key (offset in header line) for a given INFO/xx or FILTER/xx or FORMAT/xx field.

Example:

use bcf_reader::*;
let mut f = smart_reader("testdata/test.bcf").unwrap();
let s = read_header(&mut f).unwrap();
let header = Header::from_string(&s).unwrap();
let key_found = header.get_idx_from_dictionary_str("FORMAT", "GT").unwrap();
assert_eq!(key_found, header.get_fmt_gt_id().unwrap());
Source

pub fn get_chrname(&self, idx: usize) -> &str

Get chromosome name from the contig index

Source

pub fn get_fmt_gt_id(&self) -> Option<usize>

Get key for FORMAT/GT field.

Source

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

Get hashmap of hashmap of dictionary of contigs outer key: contig_idx inner key: is the key of the dictionary of contig, such as ‘ID’, ‘Description’

Source

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

Get hashmap of hashmap of dictionary of strings outer key: item_idx, for FILTER/xx, FORMAT/xx, INFO/xx, inner key: is the key of the dictionary of string, such as ‘ID’, ‘Description’

Source

pub fn get_samples(&self) -> &Vec<String>

Get samples names from sample idx Example:

use bcf_reader::*;
// read data generated by bcftools
// bcftools query -l test.bcf | bgzip -c > test_samples.gz
let mut samples_str = String::new();
smart_reader("./testdata/test_samples.gz")
    .unwrap()
    .read_to_string(&mut samples_str)
    .unwrap();
// read data via bcf-reader
let mut f = smart_reader("testdata/test.bcf").unwrap();
let s = read_header(&mut f).unwrap();
let header = Header::from_string(&s).unwrap();
let samples_str2 = header.get_samples().join("\n");
// compare bcftools results and bcf-reader results
assert_eq!(samples_str.trim(), samples_str2.trim());

Trait Implementations§

Source§

impl Debug for Header

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Header

§

impl RefUnwindSafe for Header

§

impl Send for Header

§

impl Sync for Header

§

impl Unpin for Header

§

impl UnwindSafe for Header

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.