Skip to content

Commit

Permalink
Merge pull request #4 from jancrichter/patch-1
Browse files Browse the repository at this point in the history
add bcf_reader::Record.id()
  • Loading branch information
bguo068 authored Jul 1, 2024
2 parents aec2cfa + d173c6a commit 9c406a1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,41 @@ impl Record {
self.pos
}

/// get variant ID as &str
/// Example:
/// ```
/// use bcf_reader::*;
/// // read data generated by bcftools
/// // bcftools query -f '%ID\n' test4.bcf | bgzip -c > test_id.gz
/// let mut id_str = String::new();
///
/// smart_reader("testdata/test_id.gz")
/// .read_to_string(&mut id_str)
/// .unwrap();
///
/// // read data via bcf-reader
/// let mut f = smart_reader("testdata/test.bcf");
/// let _s = read_header(&mut f);
/// let mut record = Record::default();
/// let mut id_str2 = Vec::<u8>::new();
/// use std::io::Write;
/// while let Ok(_) = record.read(&mut f) {
/// let id = if record.id().is_empty() {
/// "."
/// } else {
/// record.id()
/// };
/// write!(id_str2, "{}\n", id).unwrap();
/// }
/// let id_str2 = String::from_utf8(id_str2).unwrap();
/// // compare bcftools results and bcf-reader results
/// assert_eq!(id_str, id_str2);
///
/// ```
pub fn id(&self) -> &str {
std::str::from_utf8(&self.buf_shared[self.id.start..self.id.end]).unwrap()
}

/// Returns the ranges of bytes in buf_shared for all alleles in the record.
/// Example:
/// ```
Expand Down
Binary file modified testdata/test.bcf
Binary file not shown.
Binary file added testdata/test_id.gz
Binary file not shown.

0 comments on commit 9c406a1

Please sign in to comment.