Struct 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 async fn read_fam_cloud( &self, cloud_file: &CloudFile, skip_set: &HashSet<MetadataFields>, ) -> Result<(Metadata, usize), Box<BedErrorPlus>>

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

§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_url, CloudFile};

let skip_set = HashSet::<MetadataFields>::new();
let fam_cloud_file = CloudFile::new(sample_url("small.fam")?)?;
let bim_cloud_file = CloudFile::new(sample_url("small.bim")?)?;
let metadata_empty = Metadata::new();
let (metadata_fam, iid_count) =
    metadata_empty.read_fam_cloud(&fam_cloud_file, &skip_set).await?;
let (metadata_bim, sid_count) =
    metadata_fam.read_bim_cloud(&bim_cloud_file, &skip_set).await?;
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 async fn read_bim_cloud( &self, cloud_file: &CloudFile, skip_set: &HashSet<MetadataFields>, ) -> Result<(Metadata, usize), Box<BedErrorPlus>>

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

§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_url, CloudFile};

let skip_set = HashSet::<MetadataFields>::new();
let fam_cloud_file = CloudFile::new(sample_url("small.fam")?)?;
let bim_cloud_file = CloudFile::new(sample_url("small.bim")?)?;
let metadata_empty = Metadata::new();
let (metadata_fam, iid_count) =
    metadata_empty.read_fam_cloud(&fam_cloud_file, &skip_set).await?;
let (metadata_bim, sid_count) =
    metadata_fam.read_bim_cloud(&bim_cloud_file, &skip_set).await?;
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 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 duplicate 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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

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

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

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

impl<T> ToOwned for T
where T: Clone,

Source§

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

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

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