Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add faidx_seq_len function #410

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/faidx/mod.rs
Original file line number Diff line number Diff line change
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
Loading