Struct bed_reader::ReadOptions
source · [−]pub struct ReadOptions<TVal: BedVal> { /* private fields */ }Expand description
Represents options for reading genotype data from a PLINK .bed file.
Construct with ReadOptions::builder.
See the Table of ReadOptions for a list of the supported options. See the Table of Index Expressions for a list of expressions for selecting individuals (sample) and SNPs (variants).
Implementations
sourceimpl<TVal: BedVal> ReadOptions<TVal>
impl<TVal: BedVal> ReadOptions<TVal>
sourcepub fn builder() -> ReadOptionsBuilder<TVal>
pub fn builder() -> ReadOptionsBuilder<TVal>
Read genotype data. Supports selection and options.
Also see
Bed::read(read without options). To fill a preallocated ndarray, seeReadOptionsBuilder::read_and_fill.
See the Table of ReadOptions for a list of the supported options. See the Table of Index Expressions for a list of expressions for selecting individuals (sample) and SNPs (variants).
Errors
See BedError and BedErrorPlus
for all possible errors.
Examples
use ndarray as nd;
use bed_reader::{Bed, ReadOptions, sample_bed_file};
use bed_reader::assert_eq_nan;
// Read all data from a .bed file into an ndarray of f64.
let file_name = sample_bed_file("small.bed")?;
let mut bed = Bed::new(file_name)?;
let val = ReadOptions::builder().f64().read(&mut bed)?;
assert_eq_nan(
&val,
&nd::array![
[1.0, 0.0, f64::NAN, 0.0],
[2.0, 0.0, f64::NAN, 2.0],
[0.0, 1.0, 2.0, 0.0]
],
);
// Read the SNPs indexed by 2.
let val = ReadOptions::builder().sid_index(2).f64().read(&mut bed)?;
assert_eq_nan(&val, &nd::array![[f64::NAN], [f64::NAN], [2.0]]);
// Read the SNPs indexed by 2, 3, and 4th from last.
let val = ReadOptions::builder()
.sid_index([2, 3, -4])
.f64()
.read(&mut bed)?;
assert_eq_nan(
&val,
&nd::array![[f64::NAN, 0.0, 1.0], [f64::NAN, 2.0, 2.0], [2.0, 0.0, 0.0]],
);
// Read SNPs from 1 (inclusive) to 4 (exclusive).
let val = ReadOptions::builder()
.sid_index(1..4)
.f64()
.read(&mut bed)?;
assert_eq_nan(
&val,
&nd::array![[0.0, f64::NAN, 0.0], [0.0, f64::NAN, 2.0], [1.0, 2.0, 0.0]],
);
// Print unique chrom values. Then, read all SNPs in chrom 5.
use std::collections::HashSet;
println!("{:?}", bed.chromosome()?.iter().collect::<HashSet<_>>());
// This outputs: {"1", "5", "Y"}.
let val = ReadOptions::builder()
.sid_index(bed.chromosome()?.map(|elem| elem == "5"))
.f64()
.read(&mut bed)?;
assert_eq_nan(&val, &nd::array![[f64::NAN], [f64::NAN], [2.0]]);
// Read 1st individual (across all SNPs).
let val = ReadOptions::builder().iid_index(0).f64().read(&mut bed)?;
assert_eq_nan(&val, &nd::array![[1.0, 0.0, f64::NAN, 0.0]]);
// Read every 2nd individual.
use ndarray::s;
let val = ReadOptions::builder()
.iid_index(s![..;2])
.f64()
.read(&mut bed)?;
assert_eq_nan(
&val,
&nd::array![[1.0, 0.0, f64::NAN, 0.0], [0.0, 1.0, 2.0, 0.0]],
);
// Read last and 2nd-to-last individuals and the last SNP
let val = ReadOptions::builder()
.iid_index([-1,-2])
.sid_index(-1)
.f64()
.read(&mut bed)?;
assert_eq_nan(&val, &nd::array![[0.0],[2.0]]);
// The output array can be f32, f64, or i8
let val = ReadOptions::builder().i8().read(&mut bed)?;
assert_eq_nan(
&val,
&nd::array![
[1, 0, -127, 0],
[2, 0, -127, 2],
[0, 1, 2, 0]
],
);sourcepub fn missing_value(&self) -> TVal
pub fn missing_value(&self) -> TVal
Value to be used for missing values (defaults to -127 or NaN).
Example
use ndarray as nd;
use bed_reader::{Bed, ReadOptions, sample_bed_file};
use bed_reader::assert_eq_nan;
let read_options = ReadOptions::builder().sid_index([2, 3, 0]).i8().build()?;
assert_eq!(read_options.missing_value(), -127);
let file_name = sample_bed_file("small.bed")?;
let mut bed = Bed::new(file_name)?;
let val = bed.read_with_options(&read_options)?;
assert_eq_nan(&val, &nd::array![[-127, 0, 1], [-127, 2, 2], [2, 0, 0]]);sourcepub fn iid_index(&self) -> &Index
pub fn iid_index(&self) -> &Index
Index of individuals (samples) to read (defaults to all).
Example
use ndarray as nd;
use bed_reader::{Bed, ReadOptions, sample_bed_file};
use bed_reader::assert_eq_nan;
let read_options = ReadOptions::builder().sid_index([2, 3, 0]).i8().build()?;
println!("{0:?}", read_options.iid_index()); // Outputs 'All'
println!("{0:?}", read_options.sid_index()); // Outputs 'Vec([2, 3, 0])'
let file_name = sample_bed_file("small.bed")?;
let mut bed = Bed::new(file_name)?;
let val = bed.read_with_options(&read_options)?;
assert_eq_nan(&val, &nd::array![[-127, 0, 1], [-127, 2, 2], [2, 0, 0]]);sourcepub fn sid_index(&self) -> &Index
pub fn sid_index(&self) -> &Index
Index of SNPs (variants) to read (defaults to all).
Example
use ndarray as nd;
use bed_reader::{Bed, ReadOptions, sample_bed_file};
use bed_reader::assert_eq_nan;
let read_options = ReadOptions::builder().sid_index([2, 3, 0]).i8().build()?;
println!("{0:?}", read_options.iid_index()); // Outputs 'All'
println!("{0:?}", read_options.sid_index()); // Outputs 'Vec([2, 3, 0])'
let file_name = sample_bed_file("small.bed")?;
let mut bed = Bed::new(file_name)?;
let val = bed.read_with_options(&read_options)?;
assert_eq_nan(&val, &nd::array![[-127, 0, 1], [-127, 2, 2], [2, 0, 0]]);sourcepub fn is_f(&self) -> bool
pub fn is_f(&self) -> bool
Is the order of the output array Fortran-style (defaults to true).
Example
use ndarray as nd;
use bed_reader::{Bed, ReadOptions, sample_bed_file};
use bed_reader::assert_eq_nan;
let read_options = ReadOptions::builder().sid_index([2, 3, 0]).i8().build()?;
assert_eq!(read_options.is_f(), true);
let file_name = sample_bed_file("small.bed")?;
let mut bed = Bed::new(file_name)?;
let val = bed.read_with_options(&read_options)?;
assert_eq_nan(&val, &nd::array![[-127, 0, 1], [-127, 2, 2], [2, 0, 0]]);sourcepub fn is_a1_counted(&self) -> bool
pub fn is_a1_counted(&self) -> bool
If allele 1 will be counted (defaults to true).
Example
use ndarray as nd;
use bed_reader::{Bed, ReadOptions, sample_bed_file};
use bed_reader::assert_eq_nan;
let read_options = ReadOptions::builder().sid_index([2, 3, 0]).i8().build()?;
assert_eq!(read_options.is_a1_counted(), true);
let file_name = sample_bed_file("small.bed")?;
let mut bed = Bed::new(file_name)?;
let val = bed.read_with_options(&read_options)?;
assert_eq_nan(&val, &nd::array![[-127, 0, 1], [-127, 2, 2], [2, 0, 0]]);sourcepub fn num_threads(&self) -> Option<usize>
pub fn num_threads(&self) -> Option<usize>
Number of threads to be used (None means set with
Environment Variables or use all processors).
Example
use ndarray as nd;
use bed_reader::{Bed, ReadOptions, sample_bed_file};
use bed_reader::assert_eq_nan;
let read_options = ReadOptions::builder().sid_index([2, 3, 0]).i8().build()?;
assert_eq!(read_options.num_threads(), None);
let file_name = sample_bed_file("small.bed")?;
let mut bed = Bed::new(file_name)?;
let val = bed.read_with_options(&read_options)?;
assert_eq_nan(&val, &nd::array![[-127, 0, 1], [-127, 2, 2], [2, 0, 0]]);Trait Implementations
sourceimpl<TVal: Clone + BedVal> Clone for ReadOptions<TVal>
impl<TVal: Clone + BedVal> Clone for ReadOptions<TVal>
sourcefn clone(&self) -> ReadOptions<TVal>
fn clone(&self) -> ReadOptions<TVal>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source. Read more
Auto Trait Implementations
impl<TVal> RefUnwindSafe for ReadOptions<TVal> where
TVal: RefUnwindSafe,
impl<TVal> Send for ReadOptions<TVal>
impl<TVal> Sync for ReadOptions<TVal>
impl<TVal> Unpin for ReadOptions<TVal> where
TVal: Unpin,
impl<TVal> UnwindSafe for ReadOptions<TVal> where
TVal: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> Pointable for T
impl<T> Pointable for T
impl<SS, SP> SupersetOf<SS> for SP where
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SP where
SS: SubsetOf<SP>,
fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct self from the equivalent element of its
superset. Read more
fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if self is actually part of its subset T (and can be converted to it).
fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as self.to_subset but without any property checks. Always succeeds.
fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts self to the equivalent element of its superset.