Struct bed_reader::Metadata

source ·
pub struct Metadata { /* private fields */ }
Expand description

Represents the metadata from PLINK .fam and .bim files.

Construct with Metadata::builder or Metadata::new.

Example

Extract metadata from a file. Create a random file with the same metadata.

use ndarray as nd;
use bed_reader::{Bed, WriteOptions, sample_bed_file};
use ndarray_rand::{rand::prelude::StdRng, rand::SeedableRng, rand_distr::Uniform, RandomExt};

let mut bed = Bed::new(sample_bed_file("small.bed")?)?;
let metadata = bed.metadata()?;
let shape = bed.dim()?;

let mut rng = StdRng::seed_from_u64(0);
let val = nd::Array::random_using(shape, Uniform::from(-1..3), &mut rng);

let temp_out = temp_testdir::TempDir::default();
let output_file = temp_out.join("random.bed");
WriteOptions::builder(output_file)
    .metadata(&metadata)
    .missing_value(-1)
    .write(&val)?;

Implementations§

source§

impl Metadata

source

pub fn builder() -> MetadataBuilder

Create a Metadata using a builder.

Example

Create metadata. Create a random file with the metadata.

use ndarray as nd;
use bed_reader::{Metadata, WriteOptions};
use ndarray_rand::{rand::prelude::StdRng, rand::SeedableRng, rand_distr::Uniform, RandomExt};

let metadata = Metadata::builder()
    .iid(["i1", "i2", "i3"])
    .sid(["s1", "s2", "s3", "s4"])
    .build()?;
let mut rng = StdRng::seed_from_u64(0);
let val = nd::Array::random_using((3, 4), Uniform::from(-1..3), &mut rng);
let temp_out = temp_testdir::TempDir::default();
let output_file = temp_out.join("random.bed");
WriteOptions::builder(output_file)
    .metadata(&metadata)
    .missing_value(-1)
    .write(&val)?;
source

pub fn new() -> Metadata

Create an empty Metadata.

See Metadata::builder()

source

pub fn fid(&self) -> Option<&Array1<String>>

Optional family id of each of individual (sample)

source

pub fn iid(&self) -> Option<&Array1<String>>

Optional individual id of each of individual (sample)

Example:
use ndarray as nd;
use bed_reader::Metadata;
let metadata = Metadata::builder().iid(["i1", "i2", "i3"]).build()?;
println!("{0:?}", metadata.iid()); // Outputs optional ndarray Some(["i1", "i2", "i3"]...)
println!("{0:?}", metadata.sid()); // Outputs None
source

pub fn father(&self) -> Option<&Array1<String>>

Optional father id of each of individual (sample)

source

pub fn mother(&self) -> Option<&Array1<String>>

Optional mother id of each of individual (sample)

source

pub fn sex(&self) -> Option<&Array1<i32>>

Optional sex each of individual (sample)

source

pub fn pheno(&self) -> Option<&Array1<String>>

Optional phenotype for each individual (seldom used)

source

pub fn chromosome(&self) -> Option<&Array1<String>>

Optional chromosome of each SNP (variant)

source

pub fn sid(&self) -> Option<&Array1<String>>

Optional SNP id of each SNP (variant)

Example:
use ndarray as nd;
use bed_reader::Metadata;
let metadata = Metadata::builder().iid(["i1", "i2", "i3"]).build()?;
println!("{0:?}", metadata.iid()); // Outputs optional ndarray Some(["i1", "i2", "i3"]...)
println!("{0:?}", metadata.sid()); // Outputs None
source

pub fn cm_position(&self) -> Option<&Array1<f32>>

Optional centimorgan position of each SNP (variant)

source

pub fn bp_position(&self) -> Option<&Array1<i32>>

Optional base-pair position of each SNP (variant)

source

pub fn allele_1(&self) -> Option<&Array1<String>>

Optional first allele of each SNP (variant)

source

pub fn allele_2(&self) -> Option<&Array1<String>>

Optional second allele of each SNP (variant)

source

pub fn read_fam<AnyPath0>( &self, path: AnyPath0, skip_set: &HashSet<MetadataFields> ) -> Result<(Metadata, usize), Box<BedErrorPlus>>where AnyPath0: AsRef<Path>,

Create a new Metadata by filling in empty fields with a .fam file.

Example

Read .fam and .bim information into a Metadata. Do not skip any fields.

use ndarray as nd;
use std::collections::HashSet;
use bed_reader::{Metadata, MetadataFields, sample_file};

let skip_set = HashSet::<MetadataFields>::new();
let metadata_empty = Metadata::new();
let (metadata_fam, iid_count) =
    metadata_empty.read_fam(sample_file("small.fam")?, &skip_set)?;
let (metadata_bim, sid_count) =
    metadata_fam.read_bim(sample_file("small.bim")?, &skip_set)?;
assert_eq!(iid_count, 3);
assert_eq!(sid_count, 4);
println!("{0:?}", metadata_fam.iid()); // Outputs optional ndarray Some(["iid1", "iid2", "iid3"]...)
println!("{0:?}", metadata_bim.sid()); // Outputs optional ndarray Some(["sid1", "sid2", "sid3", "sid4"]...)
println!("{0:?}", metadata_bim.chromosome()); // Outputs optional ndarray Some(["1", "1", "5", "Y"]...)
source

pub fn read_bim<AnyPath0>( &self, path: AnyPath0, skip_set: &HashSet<MetadataFields> ) -> Result<(Metadata, usize), Box<BedErrorPlus>>where AnyPath0: AsRef<Path>,

Create a new Metadata by filling in empty fields with a .bim file.

Example

Read .fam and .bim information into a Metadata. Do not skip any fields.

use ndarray as nd;
use std::collections::HashSet;
use bed_reader::{Metadata, MetadataFields, sample_file};

let skip_set = HashSet::<MetadataFields>::new();
let metadata_empty = Metadata::new();
let (metadata_fam, iid_count) =
    metadata_empty.read_fam(sample_file("small.fam")?, &skip_set)?;
let (metadata_bim, sid_count) =
    metadata_fam.read_bim(sample_file("small.bim")?, &skip_set)?;
assert_eq!(iid_count, 3);
assert_eq!(sid_count, 4);
println!("{0:?}", metadata_bim.iid()); // Outputs optional ndarray Some(["iid1", "iid2", "iid3"]...)
println!("{0:?}", metadata_bim.sid()); // Outputs optional ndarray Some(["sid1", "sid2", "sid3", "sid4"]...)
println!("{0:?}", metadata_bim.chromosome()); // Outputs optional ndarray Some(["1", "1", "5", "Y"]...)
source

pub fn write_fam<AnyPath0>( &self, path: AnyPath0 ) -> Result<(), Box<BedErrorPlus>>where AnyPath0: AsRef<Path>,

Write the metadata related to individuals/samples to a .fam file.

If any of the .fam metadata is not present, the function will return an error.

Example

Create metadata with iid and sid arrays, then fill in the other fields with default arrays, finally write the .fam information to a file.

 use ndarray as nd;
 use std::collections::HashSet;
 use bed_reader::Metadata;

 let metadata0 = Metadata::builder()
     .iid(["i1", "i2", "i3"])
     .sid(["s1", "s2", "s3", "s4"])
     .build()?;
 let metadata_filled = metadata0.fill(3, 4)?;
 let temp_out = temp_testdir::TempDir::default();
 let output_file = temp_out.join("no_bed.fam");
 metadata_filled.write_fam(output_file)?;
source

pub fn write_bim<AnyPath0>( &self, path: AnyPath0 ) -> Result<(), Box<BedErrorPlus>>where AnyPath0: AsRef<Path>,

Write the metadata related to SNPs/variants to a .bim file.

If any of the .bim metadata is not present, the function will return an error.

Example

Create metadata with iid and sid arrays, then fill in the other fields with default arrays, finally write the .bim information to a file.

 use ndarray as nd;
 use std::collections::HashSet;
 use bed_reader::Metadata;

 let metadata0 = Metadata::builder()
     .iid(["i1", "i2", "i3"])
     .sid(["s1", "s2", "s3", "s4"])
     .build()?;
 let metadata_filled = metadata0.fill(3, 4)?;
 let temp_out = temp_testdir::TempDir::default();
 let output_file = temp_out.join("no_bed.bim");
 metadata_filled.write_bim(output_file)?;
source

pub fn fill( &self, iid_count: usize, sid_count: usize ) -> Result<Metadata, Box<BedErrorPlus>>

Create a new Metadata by filling in empty fields with default values.

Example
use ndarray as nd;
use std::collections::HashSet;
use bed_reader::{Metadata, MetadataFields};

let metadata0 = Metadata::builder()
    .iid(["i1", "i2", "i3"])
    .sid(["s1", "s2", "s3", "s4"])
    .build()?;
let metadata_filled = metadata0.fill(3, 4)?;

println!("{0:?}", metadata_filled.iid()); // Outputs optional ndarray Some(["i1", "i2", "i3"]...)
println!("{0:?}", metadata_filled.sid()); // Outputs optional ndarray Some(["s1", "s2", "s3", "s4"]...)
println!("{0:?}", metadata_filled.chromosome()); // Outputs optional ndarray Some(["0", "0", "0", "0"]...)

Trait Implementations§

source§

impl Clone for Metadata

source§

fn clone(&self) -> Metadata

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Metadata

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Metadata

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl PartialEq for Metadata

source§

fn eq(&self, other: &Metadata) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl StructuralPartialEq for Metadata

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,

§

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

Checks if self is actually part of its subset T (and can be converted to it).
§

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

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> Scalar for Twhere T: 'static + Clone + PartialEq + Debug,