Intersect

Struct Intersect 

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

An intersection of SAF file readers.

The intersection takes an arbitrary number of readers and returns data where all readers contain data for the same contig and position. It is assumed that contigs are sorted in the same order in each file, and that positions are sorted numerically within each contig.

Implementations§

Source§

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

Source

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

Returns a new collection of records suitable for use in reading.

Examples found in repository?
examples/saf_v3_intersect.rs (line 23)
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 intersect(self, reader: Reader<R, V>) -> Self

Creates a new intersecting reader with an additional reader, consuming self.

Since self is consumed, rather than mutated, this can be chained to build intersections of multiple readers. See also the Reader::intersect method for a way to start create the initial intersecting reader.

Source

pub fn get_readers(&self) -> &[Reader<R, V>]

Returns the inner readers.

Examples found in repository?
examples/saf_v3_intersect.rs (line 25)
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 get_readers_mut(&mut self) -> &mut [Reader<R, V>]

Returns a mutable reference to the inner readers.

Source

pub fn into_readers(self) -> Vec<Reader<R, V>>

Returns the inner readers, consuming self.

Source

pub fn new(readers: Vec<Reader<R, V>>) -> Self

Creates a new intersecting reader from a collection of readers.

§Panics

Panics if readers is empty.

Examples found in repository?
examples/saf_v3_intersect.rs (line 18)
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 read_records( &mut self, bufs: &mut [Record<Id, V::Item>], ) -> Result<ReadStatus>

Reads a set of intersecting records, one from each contained reader.

If successful, a record from each inner reader will be read into the corresponding buffer such that all resulting records will be on the same contig and the same position.

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

Examples found in repository?
examples/saf_v3_intersect.rs (line 24)
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}

Auto Trait Implementations§

§

impl<R, V> Freeze for Intersect<R, V>

§

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

§

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

§

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

§

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

§

impl<R, V> !UnwindSafe for Intersect<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.