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, 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
sourceimpl Metadata
impl Metadata
sourcepub fn builder() -> MetadataBuilder
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, 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)?;sourcepub fn iid(&self) -> Option<&Array1<String>>
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 Nonesourcepub fn father(&self) -> Option<&Array1<String>>
pub fn father(&self) -> Option<&Array1<String>>
Optional father id of each of individual (sample)
sourcepub fn mother(&self) -> Option<&Array1<String>>
pub fn mother(&self) -> Option<&Array1<String>>
Optional mother id of each of individual (sample)
sourcepub fn pheno(&self) -> Option<&Array1<String>>
pub fn pheno(&self) -> Option<&Array1<String>>
Optional phenotype for each individual (seldom used)
sourcepub fn chromosome(&self) -> Option<&Array1<String>>
pub fn chromosome(&self) -> Option<&Array1<String>>
Optional chromosome of each SNP (variant)
sourcepub fn sid(&self) -> Option<&Array1<String>>
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 Nonesourcepub fn cm_position(&self) -> Option<&Array1<f32>>
pub fn cm_position(&self) -> Option<&Array1<f32>>
Optional centimorgan position of each SNP (variant)
sourcepub fn bp_position(&self) -> Option<&Array1<i32>>
pub fn bp_position(&self) -> Option<&Array1<i32>>
Optional base-pair position of each SNP (variant)
sourcepub fn read_fam<P: AsRef<Path>>(
&self,
path: P,
skip_set: &HashSet<MetadataFields>
) -> Result<(Metadata, usize), BedErrorPlus>
pub fn read_fam<P: AsRef<Path>>(
&self,
path: P,
skip_set: &HashSet<MetadataFields>
) -> Result<(Metadata, usize), BedErrorPlus>
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"]...)sourcepub fn read_bim<P: AsRef<Path>>(
&self,
path: P,
skip_set: &HashSet<MetadataFields>
) -> Result<(Metadata, usize), BedErrorPlus>
pub fn read_bim<P: AsRef<Path>>(
&self,
path: P,
skip_set: &HashSet<MetadataFields>
) -> Result<(Metadata, usize), BedErrorPlus>
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"]...)sourcepub fn write_fam<P: AsRef<Path>>(&self, path: P) -> Result<(), BedErrorPlus>
pub fn write_fam<P: AsRef<Path>>(&self, path: P) -> Result<(), BedErrorPlus>
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)?;sourcepub fn write_bim<P: AsRef<Path>>(&self, path: P) -> Result<(), BedErrorPlus>
pub fn write_bim<P: AsRef<Path>>(&self, path: P) -> Result<(), BedErrorPlus>
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)?;sourcepub fn fill(
&self,
iid_count: usize,
sid_count: usize
) -> Result<Metadata, BedErrorPlus>
pub fn fill(
&self,
iid_count: usize,
sid_count: usize
) -> Result<Metadata, 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
impl StructuralPartialEq for Metadata
Auto Trait Implementations
impl RefUnwindSafe for Metadata
impl !Send for Metadata
impl !Sync for Metadata
impl Unpin for Metadata
impl UnwindSafe for Metadata
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.