-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use cargo features to select reader
- Loading branch information
1 parent
a6a1845
commit 1e21228
Showing
6 changed files
with
64 additions
and
82 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,43 +1,28 @@ | ||
use std::{error::Error, fmt::Debug, path::Path}; | ||
|
||
use bio::io::fasta::Reader; | ||
use bytes::Bytes; | ||
use needletail::parse_fastx_file; | ||
use rayon::{ | ||
prelude::IntoParallelIterator, | ||
vec::IntoIter, | ||
}; | ||
use rayon::{prelude::IntoParallelIterator, vec::IntoIter}; | ||
|
||
pub(crate) trait SequenceReader { | ||
fn sequence_reader<P: AsRef<Path> + Debug>(path: P) -> Result<IntoIter<Bytes>, Box<dyn Error>>; | ||
#[cfg(not(feature = "needletail"))] | ||
pub(crate) fn read<P: AsRef<Path> + Debug>(path: P) -> Result<IntoIter<Bytes>, Box<dyn Error>> { | ||
Ok(bio::io::fasta::Reader::from_file(path)? | ||
.records() | ||
.into_iter() | ||
.map(|read| read.expect("Error reading fasta record.")) | ||
.map(|record| Bytes::copy_from_slice(record.seq())) | ||
.collect::<Vec<Bytes>>() | ||
.into_par_iter()) | ||
} | ||
|
||
pub(crate) struct RustBio; | ||
|
||
impl SequenceReader for RustBio { | ||
fn sequence_reader<P: AsRef<Path> + Debug>(path: P) -> Result<IntoIter<Bytes>, Box<dyn Error>> { | ||
Ok(Reader::from_file(path)? | ||
.records() | ||
.into_iter() | ||
.map(|read| read.expect("Error reading fasta record.")) | ||
.map(|record| Bytes::copy_from_slice(record.seq())) | ||
.collect::<Vec<Bytes>>() | ||
.into_par_iter()) | ||
} | ||
} | ||
|
||
pub(crate) struct Needletail; | ||
|
||
impl SequenceReader for Needletail { | ||
fn sequence_reader<P: AsRef<Path> + Debug>(path: P) -> Result<IntoIter<Bytes>, Box<dyn Error>> { | ||
let mut reader = parse_fastx_file(path)?; | ||
let mut v = Vec::new(); | ||
while let Some(record) = reader.next() { | ||
let record = record.expect("invalid record"); | ||
let seq = record.seq(); | ||
let seq = Bytes::copy_from_slice(&seq); | ||
v.push(seq); | ||
} | ||
Ok(v.into_par_iter()) | ||
#[cfg(feature = "needletail")] | ||
pub(crate) fn read<P: AsRef<Path> + Debug>(path: P) -> Result<IntoIter<Bytes>, Box<dyn Error>> { | ||
let mut reader = needletail::parse_fastx_file(path)?; | ||
let mut v = Vec::new(); | ||
while let Some(record) = reader.next() { | ||
let record = record.expect("invalid record"); | ||
let seq = record.seq(); | ||
let seq = Bytes::copy_from_slice(&seq); | ||
v.push(seq); | ||
} | ||
Ok(v.into_par_iter()) | ||
} |
This file contains 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