Skip to content

Commit

Permalink
Add fasta::build function + FaidxBuildError rust_htslib::errors::Erro…
Browse files Browse the repository at this point in the history
…r enum entry.
  • Loading branch information
PB-DB committed Feb 7, 2024
1 parent 4d146c6 commit 0f16319
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub enum Error {
FaidxPositionTooLarge,
#[error("bad conversion of sequence name")]
FaidxBadSeqName,
#[error("failed to build index for fasta file {path:?}")]
FaidxBuildFailed { path: std::path::PathBuf },

// Errors for Tbx
#[error("previous iterator generation failed")]
Expand Down
25 changes: 25 additions & 0 deletions src/faidx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@ pub struct Reader {
inner: *mut htslib::faidx_t,
}

///
/// Build a faidx for input path.
///
/// # Errors
/// If indexing fails. Could be malformatted or file could not be accessible.
///
///```
/// use rust_htslib::faidx::build;
/// let path = std::path::PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"),"/test/test_cram.fa"));
/// build(&path).expect("Failed to build fasta index");
///```
///
pub fn build(
path: impl Into<std::path::PathBuf>,
) -> Result<(), std::boxed::Box<dyn std::error::Error>> {
let path = path.into();
let os_path = std::ffi::CString::new(path.display().to_string())?;
let rc = unsafe { htslib::fai_build(os_path.as_ptr()) };
if rc < 0 {
Err(Error::FaidxBuildFailed { path })?
} else {
Ok(())
}
}

impl Reader {
/// Create a new Reader from a path.
///
Expand Down

0 comments on commit 0f16319

Please sign in to comment.