gb_io/reader/
mod.rs

1use std::io::Read;
2
3#[macro_use]
4mod errors;
5mod feature_table;
6mod field;
7mod location;
8mod locus;
9mod misc;
10mod stream_parser;
11
12use self::stream_parser::StreamParser;
13use crate::seq::{Location, Seq};
14
15pub use crate::errors::GbParserError;
16
17#[derive(Debug)]
18pub struct SeqReader<T: Read> {
19    parser: StreamParser<T>,
20}
21
22impl<T: Read> Iterator for SeqReader<T> {
23    type Item = Result<Seq, GbParserError>;
24
25    fn next(&mut self) -> Option<Result<Seq, GbParserError>> {
26        match self.parser.read_one_record() {
27            Ok(Some(seq)) => Some(Ok(seq)),
28            Ok(None) => None,
29            Err(e) => Some(Err(e)),
30        }
31    }
32}
33
34const READ_BUF_SIZE: usize = 64 * 1024;
35
36impl<T: Read> SeqReader<T> {
37    /// Parse a stream one `Seq` at a time
38    pub fn new(data: T) -> SeqReader<T> {
39        SeqReader {
40            parser: StreamParser::new(data, READ_BUF_SIZE),
41        }
42    }
43}
44
45/// Convenience method to parse an entire file at once. Uses the streaming parser.
46pub fn parse_file<P: AsRef<::std::path::Path>>(path: P) -> Result<Vec<Seq>, GbParserError> {
47    let file = ::std::fs::File::open(path)?;
48    SeqReader::new(file).collect()
49}
50
51/// Parse an entire genbank file provided as a slice. Might be slightly faster
52/// than the streaming parser used by `parse_file` and `SeqReader::from_stream` 
53/// since less copying of data is required, however not as well tested. I recommend using
54/// `SeqReader` instead. I've mainly left this here for benchmarking purposes.
55pub fn parse_slice(data: &[u8]) -> Result<Vec<Seq>, GbParserError> {
56    let res = misc::gb_records(data);
57    match res {
58        Ok((_, o)) => Ok(o),
59        Err(e) => {
60            Err(GbParserError::SyntaxError(
61                format!("{:?}", e),
62            ))
63        }
64    }
65}
66
67/// used by `Location::from_gb_format`
68pub(crate) fn parse_location(data: &[u8]) -> Result<Location, GbParserError> {
69    let res = location::location(data);
70    match res {
71        Ok((_, o)) => Ok(o),
72        Err(e) => Err(GbParserError::SyntaxError(format!("{:?}", e))),
73    }
74}