Skip to content

Commit

Permalink
feat: adding function to get sequence length to faidx mod (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
pblischak committed Dec 9, 2023
1 parent 3008a13 commit ae79eba
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/faidx/mod.rs
Expand Up @@ -126,6 +126,17 @@ impl Reader {

Ok(out)
}

/// Fetches the length of the given sequence name.
///
/// # Arguments
///
/// * `name` - the name of the template sequence (e.g., "chr1")
pub fn fetch_seq_len<N: AsRef<str>>(&self, name: N) -> u64 {
let cname = ffi::CString::new(name.as_ref().as_bytes()).unwrap();
let seq_len = unsafe { htslib::faidx_seq_len(self.inner, cname.as_ptr()) };
seq_len as u64
}
}

impl Drop for Reader {
Expand Down Expand Up @@ -247,6 +258,15 @@ mod tests {
assert_eq!(n, "chr2");
}

#[test]
fn faidx_get_seq_len() {
let r = open_reader();
let chr1_len = r.fetch_seq_len("chr1");
let chr2_len = r.fetch_seq_len("chr2");
assert_eq!(chr1_len, 120u64);
assert_eq!(chr2_len, 120u64);
}

#[test]
fn open_many_readers() {
for _ in 0..500_000 {
Expand Down

0 comments on commit ae79eba

Please sign in to comment.