-
Notifications
You must be signed in to change notification settings - Fork 86
Add basic functionality to read indexed FASTA files #165
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
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
05ee782
Add basic functionality to read indexed FASTA files
landesfeind 4478e1d
Add missing file
landesfeind bf6fd6e
Add basic functionality to read indexed FASTA files
landesfeind ab53edc
Add missing file
landesfeind 917ef40
Merge branch 'master' of github.com:landesfeind/rust-htslib
landesfeind 21a7899
Apply cargo fmt
landesfeind 4c12035
Add test for Faidx loading
landesfeind d10fe5d
Add test to read twice from the same faidx
landesfeind 9c65520
Merge branch 'master' into master
brainstorm 338b712
Merge branch 'master' into master
brainstorm c17cda0
Merge branch 'master' of https://github.com/rust-bio/rust-htslib
landesfeind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use snafu::Snafu; | ||
use std::path::PathBuf; | ||
|
||
pub type Result<T, E = Error> = std::result::Result<T, E>; | ||
|
||
#[derive(Snafu, Debug, PartialEq)] | ||
#[snafu(visibility = "pub")] | ||
pub enum Error { | ||
#[snafu(display("file not found: {}", path.display()))] | ||
FileNotFound { path: PathBuf }, | ||
#[snafu(display("invalid (non-unique) characters in path"))] | ||
NonUnicodePath, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// Copyright 2019 Manuel Landesfeind, Evotec International GmbH | ||
// Licensed under the MIT license (http://opensource.org/licenses/MIT) | ||
// This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
//! | ||
//! Module for working with faidx-indexed FASTA files. | ||
//! | ||
|
||
use std::ffi; | ||
use std::path::Path; | ||
use url::Url; | ||
|
||
use crate::htslib; | ||
|
||
pub mod errors; | ||
pub use errors::{Error, Result}; | ||
|
||
fn path_as_bytes<'a, P: 'a + AsRef<Path>>(path: P, must_exist: bool) -> Result<Vec<u8>> { | ||
if path.as_ref().exists() || !must_exist { | ||
Ok(path | ||
.as_ref() | ||
.to_str() | ||
.ok_or(Error::NonUnicodePath)? | ||
.as_bytes() | ||
.to_owned()) | ||
} else { | ||
Err(Error::FileNotFound { | ||
path: path.as_ref().to_owned(), | ||
}) | ||
} | ||
} | ||
dlaehnemann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// A Fasta reader. | ||
#[derive(Debug)] | ||
pub struct Reader { | ||
inner: *mut htslib::faidx_t, | ||
} | ||
|
||
impl Reader { | ||
/// Create a new Reader from path. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `path` - the path to open. | ||
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, Error> { | ||
Self::new(&path_as_bytes(path, true)?) | ||
} | ||
|
||
/// Create a new Reader from URL. | ||
dlaehnemann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub fn from_url(url: &Url) -> Result<Self, Error> { | ||
Self::new(url.as_str().as_bytes()) | ||
} | ||
|
||
/// Create a new Reader. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `path` - the path to open | ||
fn new(path: &[u8]) -> Result<Self, Error> { | ||
let cpath = ffi::CString::new(path).unwrap(); | ||
let inner = unsafe { htslib::fai_load(cpath.as_ptr()) }; | ||
Ok(Self { inner }) | ||
} | ||
dlaehnemann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// Fetches the sequence and returns it | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `name` - the name of the template sequence (e.g., "chr1") | ||
/// * `begin` - the offset within the template sequence (starting with 0) | ||
/// * `end` - the end position to return | ||
pub fn fetch_seq<N: AsRef<str>>(&self, name: N, begin: usize, end: usize) -> String { | ||
let cname = ffi::CString::new(name.as_ref().as_bytes()).unwrap(); | ||
let len_out: i32 = 0; | ||
let cseq = unsafe { | ||
let ptr = htslib::faidx_fetch_seq( | ||
self.inner, //*const faidx_t, | ||
cname.as_ptr(), // c_name | ||
begin as ::std::os::raw::c_int, // p_beg_i | ||
end as ::std::os::raw::c_int, // p_end_i | ||
&mut (len_out as ::std::os::raw::c_int), //len | ||
); | ||
ffi::CStr::from_ptr(ptr) | ||
}; | ||
|
||
cseq.to_str().unwrap().to_owned() | ||
} | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
fn open_reader() -> Reader { | ||
Reader::from_path(format!("{}/test/test_cram.fa", env!("CARGO_MANIFEST_DIR"))).ok().unwrap() | ||
} | ||
|
||
#[test] | ||
fn faidx_open() { | ||
} | ||
dlaehnemann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#[test] | ||
fn faidx_read_chr_first_base() { | ||
let r = open_reader(); | ||
let seq = r.fetch_seq("chr1", 0 , 0); | ||
assert_eq!(seq.len(), 1); | ||
assert_eq!(seq, "G"); | ||
} | ||
|
||
#[test] | ||
fn faidx_read_chr_start() { | ||
let r = open_reader(); | ||
let seq = r.fetch_seq("chr1", 0 , 9); | ||
assert_eq!(seq.len(), 10); | ||
assert_eq!(seq, "GGGCACAGCC"); | ||
} | ||
|
||
#[test] | ||
fn faidx_read_chr_between() { | ||
let r = open_reader(); | ||
let seq = r.fetch_seq("chr1", 4 , 14); | ||
assert_eq!(seq.len(), 11); | ||
assert_eq!(seq, "ACAGCCTCACC"); | ||
} | ||
|
||
#[test] | ||
fn faidx_read_chr_end() { | ||
let r = open_reader(); | ||
let seq = r.fetch_seq("chr1", 110, 120); | ||
assert_eq!(seq.len(), 10); | ||
assert_eq!(seq, "CCCCTCCGTG"); | ||
} | ||
|
||
#[test] | ||
fn faidx_read_twice() { | ||
let r = open_reader(); | ||
let seq = r.fetch_seq("chr1", 110, 120); | ||
assert_eq!(seq.len(), 10); | ||
assert_eq!(seq, "CCCCTCCGTG"); | ||
|
||
let seq = r.fetch_seq("chr1", 5, 9); | ||
assert_eq!(seq.len(), 5); | ||
assert_eq!(seq, "CAGCC"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.