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
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};
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)?;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<AnyPath0>(
&self,
path: AnyPath0,
skip_set: &HashSet<MetadataFields>
) -> Result<(Metadata, usize), Box<BedErrorPlus>>where
AnyPath0: AsRef<Path>,
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"]...)sourcepub fn read_bim<AnyPath0>(
&self,
path: AnyPath0,
skip_set: &HashSet<MetadataFields>
) -> Result<(Metadata, usize), Box<BedErrorPlus>>where
AnyPath0: AsRef<Path>,
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"]...)sourcepub fn write_fam<AnyPath0>(
&self,
path: AnyPath0
) -> Result<(), Box<BedErrorPlus>>where
AnyPath0: AsRef<Path>,
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)?;sourcepub fn write_bim<AnyPath0>(
&self,
path: AnyPath0
) -> Result<(), Box<BedErrorPlus>>where
AnyPath0: AsRef<Path>,
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)?;sourcepub fn fill(
&self,
iid_count: usize,
sid_count: usize
) -> Result<Metadata, Box<BedErrorPlus>>
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 PartialEq for Metadata
impl PartialEq for Metadata
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§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Pointable for T
impl<T> Pointable for T
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
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
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.