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
impl Header
Sourcepub fn from_string(text: &str) -> Result<Self, ParseHeaderError>
pub fn from_string(text: &str) -> Result<Self, ParseHeaderError>
parse header lines to structured data Header
Sourcepub fn get_idx_from_dictionary_str(
&self,
dictionary: &str,
field: &str,
) -> Option<usize>
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());Sourcepub fn get_chrname(&self, idx: usize) -> &str
pub fn get_chrname(&self, idx: usize) -> &str
Get chromosome name from the contig index
Sourcepub fn get_fmt_gt_id(&self) -> Option<usize>
pub fn get_fmt_gt_id(&self) -> Option<usize>
Get key for FORMAT/GT field.
Sourcepub fn dict_contigs(&self) -> &HashMap<usize, HashMap<String, String>>
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’
Sourcepub fn dict_strings(&self) -> &HashMap<usize, HashMap<String, String>>
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’
Sourcepub fn get_samples(&self) -> &Vec<String>
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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