Skip to content

Commit

Permalink
Revert to Record::to_vcf_string method
Browse files Browse the repository at this point in the history
Added return value checking on vcf_format, which also suggests that the
methods should return a Result and shound't be implemented directly as
a Display trait.
  • Loading branch information
ctsa committed Oct 21, 2024
1 parent 7ee96e2 commit bdea7de
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
69 changes: 47 additions & 22 deletions src/bcf/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,38 @@ impl Record {
}
"".to_owned()
}

/// Convert to VCF String
///
/// Intended for debug only. Use Writer for efficient VCF output.
///
pub fn to_vcf_string(&self) -> Result<String> {
let mut buf = htslib::kstring_t {
l: 0,
m: 0,
s: ptr::null_mut(),
};
let ret = unsafe { htslib::vcf_format(self.header().inner, self.inner, &mut buf) };

if ret < 0 {
if !buf.s.is_null() {
unsafe {
libc::free(buf.s as *mut libc::c_void);
}
}
return Err(Error::BcfToString);
}

let vcf_str = unsafe {
let vcf_str = String::from(ffi::CStr::from_ptr(buf.s).to_str().unwrap());
if !buf.s.is_null() {
libc::free(buf.s as *mut libc::c_void);
}
vcf_str
};

Ok(vcf_str)
}
}

impl Clone for Record {
Expand Down Expand Up @@ -1237,25 +1269,6 @@ unsafe impl Send for Record {}

unsafe impl Sync for Record {}

impl fmt::Display for Record {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut buf = htslib::kstring_t {
l: 0,
m: 0,
s: ptr::null_mut(),
};
let vcf_str = unsafe {
htslib::vcf_format(self.header().inner, self.inner, &mut buf);
let vcf_str = String::from(ffi::CStr::from_ptr(buf.s).to_str().unwrap());
if !buf.s.is_null() {
libc::free(buf.s as *mut libc::c_void);
}
vcf_str
};
write!(f, "{vcf_str}")
}
}

/// Info tag representation.
#[derive(Debug)]
pub struct Info<'a, B: BorrowMut<Buffer> + Borrow<Buffer>> {
Expand Down Expand Up @@ -1735,7 +1748,17 @@ mod tests {
}

#[test]
fn test_record_display_trait() {
fn test_record_to_vcf_string_err() {
let tmp = NamedTempFile::new().unwrap();
let path = tmp.path();
let header = Header::new();
let vcf = Writer::from_path(path, &header, true, Format::Vcf).unwrap();
let record = vcf.empty_record();
assert!(record.to_vcf_string().is_err());
}

#[test]
fn test_record_to_vcf_string() {
let tmp = NamedTempFile::new().unwrap();
let path = tmp.path();
let mut header = Header::new();
Expand All @@ -1744,7 +1767,9 @@ mod tests {
let vcf = Writer::from_path(path, &header, true, Format::Vcf).unwrap();
let mut record = vcf.empty_record();
record.push_filter("foo".as_bytes()).unwrap();
let s = format!("{record}");
assert_eq!(s, "chr1\t1\t.\t.\t.\t0\tfoo\t.\n");
assert_eq!(
record.to_vcf_string().unwrap(),
"chr1\t1\t.\t.\t.\t0\tfoo\t.\n"
);
}
}
2 changes: 2 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ pub enum Error {
BcfSetValues,
#[error("failed to remove alleles in BCF/VCF record")]
BcfRemoveAlleles,
#[error("failed to render BCF record as string")]
BcfToString,

#[error("invalid compression level {level}")]
BgzfInvalidCompressionLevel { level: i8 },
Expand Down

0 comments on commit bdea7de

Please sign in to comment.