Struct noodles_gff::reader::Reader
source · pub struct Reader<R> { /* private fields */ }
Expand description
A GFF reader.
Implementations§
source§impl<R> Reader<R>where
R: BufRead,
impl<R> Reader<R>where
R: BufRead,
sourcepub fn new(inner: R) -> Self
pub fn new(inner: R) -> Self
Creates a GFF reader.
§Examples
use noodles_gff as gff;
let data = b"##gff-version 3\n";
let mut reader = gff::Reader::new(&data[..]);
sourcepub fn get_ref(&self) -> &R
pub fn get_ref(&self) -> &R
Returns a reference to the underlying reader.
§Examples
use noodles_gff as gff;
let data = b"##gff-version 3\n";
let reader = gff::Reader::new(&data[..]);
let _ = reader.get_ref();
sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Unwraps and returns the underlying reader.
§Examples
use noodles_gff as gff;
let data = b"##gff-version 3
#format: gff3
";
let mut reader = gff::Reader::new(&data[..]);
reader.read_line(&mut String::new())?;
assert_eq!(reader.into_inner(), b"#format: gff3\n");
sourcepub fn read_line(&mut self, buf: &mut String) -> Result<usize>
pub fn read_line(&mut self, buf: &mut String) -> Result<usize>
Reads a raw GFF line.
This reads from the underlying stream until a newline is reached and appends it to the
given buffer, sans the final newline character. The buffer can subsequently be parsed as a
crate::Line
.
It is more ergonomic to read records using an iterator (see Self::lines
), but using
this method allows control of the line buffer and whether the raw line should be parsed.
If successful, the number of bytes read is returned. If the number of bytes read is 0, the stream reached EOF.
§Examples
use noodles_gff as gff;
let data = b"##gff-version 3
sq0\tNOODLES\tgene\t8\t13\t.\t+\t.\tgene_id=ndls0;gene_name=gene0
";
let mut reader = gff::Reader::new(&data[..]);
let mut buf = String::new();
reader.read_line(&mut buf)?;
assert_eq!(buf, "##gff-version 3");
sourcepub fn lines(&mut self) -> Lines<'_, R> ⓘ
pub fn lines(&mut self) -> Lines<'_, R> ⓘ
Returns an iterator over lines starting from the current stream position.
When using this, the caller is responsible to stop reading at either EOF or when the
FASTA
directive is read, whichever comes first.
Unlike Self::read_line
, each line is parsed as a crate::Line
.
§Examples
use noodles_gff as gff;
let data = b"##gff-version 3
sq0\tNOODLES\tgene\t8\t13\t.\t+\t.\tgene_id=ndls0;gene_name=gene0
";
let mut reader = gff::Reader::new(&data[..]);
let mut lines = reader.lines();
let line = lines.next().transpose()?;
assert!(matches!(line, Some(gff::Line::Directive(_))));
let line = lines.next().transpose()?;
assert!(matches!(line, Some(gff::Line::Record(_))));
assert!(lines.next().is_none());
Examples found in repository?
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() -> io::Result<()> {
let src = env::args().nth(1).expect("missing src");
let mut reader = File::open(src).map(BufReader::new).map(gff::Reader::new)?;
let stdout = io::stdout().lock();
let mut writer = gff::Writer::new(stdout);
for result in reader.lines() {
let line = result?;
if line == Line::Directive(Directive::StartOfFasta) {
break;
}
writer.write_line(&line)?;
}
Ok(())
}
sourcepub fn read_lazy_line(&mut self, line: &mut Line) -> Result<usize>
pub fn read_lazy_line(&mut self, line: &mut Line) -> Result<usize>
Reads a single line without eagerly decoding it.
sourcepub fn records(&mut self) -> Records<'_, R> ⓘ
pub fn records(&mut self) -> Records<'_, R> ⓘ
Returns an iterator over records starting from the current stream position.
This filters lines for only records. It stops at either EOF or when the FASTA
directive
is read, whichever comes first.
§Examples
use noodles_gff as gff;
let data = b"##gff-version 3
sq0\tNOODLES\tgene\t8\t13\t.\t+\t.\tgene_id=ndls0;gene_name=gene0
";
let mut reader = gff::Reader::new(&data[..]);
let mut records = reader.records();
assert!(records.next().transpose()?.is_some());
assert!(records.next().is_none());
Examples found in repository?
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
fn main() -> io::Result<()> {
let src = env::args().nth(1).expect("missing src");
let mut reader = File::open(src).map(BufReader::new).map(gff::Reader::new)?;
let mut n = 0;
for result in reader.records() {
let _ = result?;
n += 1;
}
println!("{n}");
Ok(())
}