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, tmp_path, 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 = tmp_path()?;
let output_file = temp_out.join("random.bed");
WriteOptions::builder(output_file)
    .metadata(&metadata)
    .missing_value(-1)
    .write(&val)?;

Implementations

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, tmp_path};
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 = tmp_path()?;
let output_file = temp_out.join("random.bed");
WriteOptions::builder(output_file)
    .metadata(&metadata)
    .missing_value(-1)
    .write(&val)?;

Create an empty Metadata.

See Metadata::builder()

Optional family id of each of individual (sample)

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

Optional father id of each of individual (sample)

Optional mother id of each of individual (sample)

Optional sex each of individual (sample)

Optional phenotype for each individual (seldom used)

Optional chromosome of each SNP (variant)

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

Optional centimorgan position of each SNP (variant)

Optional base-pair position of each SNP (variant)

Optional first allele of each SNP (variant)

Optional second allele of each SNP (variant)

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"]...)

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"]...)

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, tmp_path};

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

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, tmp_path};

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

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

Should always be Self

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

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

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

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

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.