Skip to content

Commit

Permalink
Merge branch 'master' into ar/header-remove-unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneskoester authored Nov 19, 2024
2 parents 9c41657 + 7f3cfea commit 15974f2
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 10 deletions.
4 changes: 0 additions & 4 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,3 @@ updates:
directory: "/"
schedule:
interval: "weekly"
commit-message:
# Prefix all commit messages with "deps: ", which should be
# accepted as a conventional commit and trigger release-please
prefix: "deps"
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [0.48.0](https://github.com/rust-bio/rust-htslib/compare/v0.47.1...v0.48.0) (2024-11-12)


### Features

* Add to_vcf_string method for bcf::Record ([#443](https://github.com/rust-bio/rust-htslib/issues/443)) ([489c0d7](https://github.com/rust-bio/rust-htslib/commit/489c0d7677445cfe38580c8d4c843ad4f4e2d827))


### Bug Fixes

* return error when bgzf_open fails to open a file ([#444](https://github.com/rust-bio/rust-htslib/issues/444)) ([9bda5f7](https://github.com/rust-bio/rust-htslib/commit/9bda5f768a5c54767c7e08ef1cafd28ff3f2a3b3))

## [0.47.1](https://github.com/rust-bio/rust-htslib/compare/v0.47.0...v0.47.1) (2024-11-12)


Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "MIT"
name = "rust-htslib"
readme = "README.md"
repository = "https://github.com/rust-bio/rust-htslib.git"
version = "0.47.1"
version = "0.48.0"

[package.metadata.release]
pre-release-commit-message = "release version {{version}}"
Expand All @@ -20,7 +20,7 @@ libz-sys = ">=1.1.15"
bio-types = ">=0.9"
byteorder = "1.3"
custom_derive = "0.1"
derive-new = "0.5"
derive-new = "0.7"
hts-sys = {version = "2.1.4", default-features = false, features = ["bindgen"]}
ieee754 = "0.2"
lazy_static = "1.4"
Expand All @@ -30,7 +30,7 @@ newtype_derive = "0.1"
regex = "1.3"
serde = {version = "^1", optional = true, features = ["derive"]}
serde_bytes = {version = "0.11", optional = true}
thiserror = "1"
thiserror = "2"
url = "2.1"

[features]
Expand All @@ -47,6 +47,6 @@ static = ["hts-sys/static"]

[dev-dependencies]
bincode = "1.2"
pretty_assertions = "0.6"
pretty_assertions = "1.4"
serde_json = "1.0"
tempfile = "3.1.0"
58 changes: 58 additions & 0 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 @@ -1714,4 +1746,30 @@ mod tests {
assert!(!record.has_filter(&bar));
assert!(record.has_filter("PASS".as_bytes()));
}

#[test]
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();
header.push_record(b"##contig=<ID=chr1,length=1000>");
header.push_record(br#"##FILTER=<ID=foo,Description="sample is a foo fighter">"#);
let vcf = Writer::from_path(path, &header, true, Format::Vcf).unwrap();
let mut record = vcf.empty_record();
record.push_filter("foo".as_bytes()).unwrap();
assert_eq!(
record.to_vcf_string().unwrap(),
"chr1\t1\t.\t.\t.\t0\tfoo\t.\n"
);
}
}
16 changes: 14 additions & 2 deletions src/bgzf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ impl Reader {
let mode = ffi::CString::new("r").unwrap();
let cpath = ffi::CString::new(path).unwrap();
let inner = unsafe { htslib::bgzf_open(cpath.as_ptr(), mode.as_ptr()) };
Ok(Self { inner })
if inner != std::ptr::null_mut() {
Ok(Self { inner })
} else {
Err(Error::FileOpen {
path: String::from_utf8(path.to_vec()).unwrap(),
})
}
}

/// Set the thread pool to use for parallel decompression.
Expand Down Expand Up @@ -211,7 +217,13 @@ impl Writer {
let mode = Self::get_open_mode(level)?;
let cpath = ffi::CString::new(path).unwrap();
let inner = unsafe { htslib::bgzf_open(cpath.as_ptr(), mode.as_ptr()) };
Ok(Self { inner, tpool: None })
if inner != std::ptr::null_mut() {
Ok(Self { inner, tpool: None })
} else {
Err(Error::FileOpen {
path: String::from_utf8(path.to_vec()).unwrap(),
})
}
}

/// Internal function to convert compression level to "mode"
Expand Down
4 changes: 4 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub enum Error {
// General errors
#[error("file not found: {path}")]
FileNotFound { path: PathBuf },
#[error("file could not be opened: {path}")]
FileOpen { path: String },
#[error("invalid (non-unicode) characters in path")]
NonUnicodePath,
#[error("failed to fetch region")]
Expand Down Expand Up @@ -131,6 +133,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 15974f2

Please sign in to comment.