Reader

Struct Reader 

Source
pub struct Reader<R, V> { /* private fields */ }
Expand description

A SAF reader.

The reader is generic over the inner reader type and over the SAF Version being read. Version-specific aliases ReaderV3 and ReaderV4 are provided for convenience.

Implementations§

Source§

impl<R, V> Reader<R, V>
where R: BufRead, V: Version,

Source

pub fn create_record_buf(&self) -> Record<Id, V::Item>

Returns a new record suitable for use in reading.

Examples found in repository?
examples/saf_v3_read.rs (line 19)
10fn main() -> io::Result<()> {
11    let path = env::args().nth(1).expect("missing path to SAF member file");
12    let mut reader = saf::reader::Builder::v3().build_from_member_path(path)?;
13
14    let stdout = io::stdout();
15    let mut writer = stdout.lock();
16
17    write!(writer, "{}", reader.index())?;
18
19    let mut record = reader.create_record_buf();
20    while reader.read_record(&mut record)?.is_not_done() {
21        writeln!(writer, "{record:.2}")?;
22    }
23
24    Ok(())
25}
More examples
Hide additional examples
examples/saf_v4_read.rs (line 19)
10fn main() -> io::Result<()> {
11    let path = env::args().nth(1).expect("missing path to SAF member file");
12    let mut reader = saf::reader::Builder::v4().build_from_member_path(path)?;
13
14    let stdout = io::stdout();
15    let mut writer = stdout.lock();
16
17    write!(writer, "{}", reader.index())?;
18
19    let mut record = reader.create_record_buf();
20    while reader.read_record(&mut record)?.is_not_done() {
21        writeln!(writer, "{record:.2}")?;
22    }
23
24    Ok(())
25}
Source

pub fn from_bgzf( index: Index<V>, position_reader: Reader<R>, item_reader: Reader<R>, ) -> Option<Self>

Creates a new reader from its raw parts.

A Builder will typically be a more ergonimic way to create a reader.

Returns None if index contains no records.

Source

pub fn index(&self) -> &Index<V>

Returns the index.

Examples found in repository?
examples/saf_v3_read.rs (line 17)
10fn main() -> io::Result<()> {
11    let path = env::args().nth(1).expect("missing path to SAF member file");
12    let mut reader = saf::reader::Builder::v3().build_from_member_path(path)?;
13
14    let stdout = io::stdout();
15    let mut writer = stdout.lock();
16
17    write!(writer, "{}", reader.index())?;
18
19    let mut record = reader.create_record_buf();
20    while reader.read_record(&mut record)?.is_not_done() {
21        writeln!(writer, "{record:.2}")?;
22    }
23
24    Ok(())
25}
More examples
Hide additional examples
examples/saf_v4_read.rs (line 17)
10fn main() -> io::Result<()> {
11    let path = env::args().nth(1).expect("missing path to SAF member file");
12    let mut reader = saf::reader::Builder::v4().build_from_member_path(path)?;
13
14    let stdout = io::stdout();
15    let mut writer = stdout.lock();
16
17    write!(writer, "{}", reader.index())?;
18
19    let mut record = reader.create_record_buf();
20    while reader.read_record(&mut record)?.is_not_done() {
21        writeln!(writer, "{record:.2}")?;
22    }
23
24    Ok(())
25}
examples/saf_v3_intersect.rs (line 26)
10fn main() -> io::Result<()> {
11    let readers = env::args()
12        .skip(1)
13        .map(|p| saf::reader::Builder::v3().build_from_member_path(p))
14        .collect::<io::Result<Vec<_>>>()?;
15
16    // Note also the [`Reader::intersect`] and [`Intersect::intersect`] methods to construct
17    // intersecting reader when the number of readers are statically known.
18    let mut intersect = saf::Intersect::new(readers);
19
20    let stdout = io::stdout();
21    let mut writer = stdout.lock();
22
23    let mut bufs = intersect.create_record_bufs();
24    while intersect.read_records(&mut bufs)?.is_not_done() {
25        for (reader, buf) in intersect.get_readers().iter().zip(bufs.iter()) {
26            let contig = reader.index().records()[*buf.contig_id()].name();
27            let position = buf.position();
28            write!(writer, "{contig}\t{position}")?;
29
30            for v in buf.item().iter() {
31                write!(writer, "\t{v:.2}")?;
32            }
33
34            writeln!(writer)?;
35        }
36    }
37
38    Ok(())
39}
Source

pub fn index_mut(&mut self) -> &mut Index<V>

Returns a mutable reference to the index.

Source

pub fn into_parts(self) -> (Index<V>, Reader<R>, Reader<R>)

Returns the inner index, position reader, and item reader, consuming self.

Source

pub fn item_reader(&self) -> &Reader<R>

Returns the inner item reader.

Source

pub fn item_reader_mut(&mut self) -> &mut Reader<R>

Returns a mutable reference to the inner item reader.

Source

pub fn position_reader(&self) -> &Reader<R>

Returns the inner position reader.

Source

pub fn position_reader_mut(&mut self) -> &mut Reader<R>

Returns a mutable reference to the inner position reader.

Source

pub fn read_item(&mut self, buf: &mut V::Item) -> Result<ReadStatus>

Reads a single item from the item reader into the provided buffer.

Note that this will bring the item and position readers out of sync. Use Self::read_record instead unless you wish to manually re-sync the underlying readers.

Source

pub fn read_magic(&mut self) -> Result<()>

Reads and checks the magic numbers.

Assumes the streams are positioned at the beginning of the files.

Source

pub fn read_position(&mut self) -> Result<Option<u32>>

Reads a single position from the position reader.

Note that this will bring the item and position readers out of sync. Use Self::read_record instead unless you wish to manually re-sync the underlying readers.

Source

pub fn read_record( &mut self, record: &mut Record<Id, V::Item>, ) -> Result<ReadStatus>

Reads a single record.

Note that the record buffer needs to be correctly set up. Use Self::create_record_buf for a correctly initialised record buffer to use for reading.

Examples found in repository?
examples/saf_v3_read.rs (line 20)
10fn main() -> io::Result<()> {
11    let path = env::args().nth(1).expect("missing path to SAF member file");
12    let mut reader = saf::reader::Builder::v3().build_from_member_path(path)?;
13
14    let stdout = io::stdout();
15    let mut writer = stdout.lock();
16
17    write!(writer, "{}", reader.index())?;
18
19    let mut record = reader.create_record_buf();
20    while reader.read_record(&mut record)?.is_not_done() {
21        writeln!(writer, "{record:.2}")?;
22    }
23
24    Ok(())
25}
More examples
Hide additional examples
examples/saf_v4_read.rs (line 20)
10fn main() -> io::Result<()> {
11    let path = env::args().nth(1).expect("missing path to SAF member file");
12    let mut reader = saf::reader::Builder::v4().build_from_member_path(path)?;
13
14    let stdout = io::stdout();
15    let mut writer = stdout.lock();
16
17    write!(writer, "{}", reader.index())?;
18
19    let mut record = reader.create_record_buf();
20    while reader.read_record(&mut record)?.is_not_done() {
21        writeln!(writer, "{record:.2}")?;
22    }
23
24    Ok(())
25}
Source§

impl<R, V> Reader<R, V>
where R: BufRead + Seek, V: Version,

Source

pub fn intersect(self, other: Self) -> Intersect<R, V>

Creates an intersection of two readers.

The resulting intersecting readers will read only records that lie on the same contigs and the same positions. Further readers can be added to the resulting intersecting reader by chaining the Intersect::intersect method.

Source

pub fn seek(&mut self, contig_id: usize) -> Result<()>

Seeks to start of contig.

The contig_id refers to the position of records in the index.

§Panics

Panics if contig_id is larger than the number of records defined in the index.

Source

pub fn seek_by_name(&mut self, name: &str) -> Result<()>

Seeks to start of contig by name.

Note that this requires a linear search of names in the index with worst time complexity linear in the index size.. If the index is large, and the contig ID is known, prefer Self::seek is more efficient.

§Panics

Panics if sequence name is not defined in index.

Auto Trait Implementations§

§

impl<R, V> Freeze for Reader<R, V>
where R: Freeze,

§

impl<R, V> !RefUnwindSafe for Reader<R, V>

§

impl<R, V> Send for Reader<R, V>
where R: Send, V: Send,

§

impl<R, V> Sync for Reader<R, V>
where R: Sync, V: Sync,

§

impl<R, V> Unpin for Reader<R, V>
where R: Unpin, V: Unpin,

§

impl<R, V> !UnwindSafe for Reader<R, V>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.