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 None
Sourcepub 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 None
Sourcepub 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>>
pub fn read_fam<AnyPath0>( &self, path: AnyPath0, skip_set: &HashSet<MetadataFields>, ) -> Result<(Metadata, usize), Box<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 async fn read_fam_cloud(
&self,
cloud_file: &CloudFile,
skip_set: &HashSet<MetadataFields>,
) -> Result<(Metadata, usize), Box<BedErrorPlus>>
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"]...)
Sourcepub fn read_bim<AnyPath0>(
&self,
path: AnyPath0,
skip_set: &HashSet<MetadataFields>,
) -> Result<(Metadata, usize), Box<BedErrorPlus>>
pub fn read_bim<AnyPath0>( &self, path: AnyPath0, skip_set: &HashSet<MetadataFields>, ) -> Result<(Metadata, usize), Box<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 async fn read_bim_cloud(
&self,
cloud_file: &CloudFile,
skip_set: &HashSet<MetadataFields>,
) -> Result<(Metadata, usize), Box<BedErrorPlus>>
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"]...)
Sourcepub fn write_fam<AnyPath0>(
&self,
path: AnyPath0,
) -> Result<(), Box<BedErrorPlus>>
pub fn write_fam<AnyPath0>( &self, path: AnyPath0, ) -> Result<(), Box<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;
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>>
pub fn write_bim<AnyPath0>( &self, path: AnyPath0, ) -> Result<(), Box<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;
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§
impl StructuralPartialEq for Metadata
Auto Trait Implementations§
impl Freeze for Metadata
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moreSource§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).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.