bed-reader

github crates.io docs.rs build status

Read and write the PLINK BED format, simply and efficiently.

Features

Examples

Read all genotype data from a .bed file.

```rust use ndarray as nd; use bedreader::{Bed, ReadOptions, asserteqnan, samplebed_file};

let filename = samplebedfile("small.bed")?; let mut bed = Bed::new(filename)?; let val = ReadOptions::builder().f64().read(&mut bed)?;

asserteqnan( &val, &nd::array![ [1.0, 0.0, f64::NAN, 0.0], [2.0, 0.0, f64::NAN, 2.0], [0.0, 1.0, 2.0, 0.0] ], );

use bed_reader::BedErrorPlus; // '#' needed for doctest

Ok::<(), BedErrorPlus>(())

```

Read every second individual (samples) and SNPs (variants) 20 to 30.

```rust

// '#' needed for doctest

use bedreader::{Bed, ReadOptions, asserteqnan, samplebed_file};

use ndarray::s;

let filename = samplebedfile("somemissing.bed")?; let mut bed = Bed::new(filename)?; let val = ReadOptions::builder() .iidindex(s![..;2]) .sid_index(20..30) .f64() .read(&mut bed)?;

assert!(val.dim() == (50, 10));

use bed_reader::BedErrorPlus; // '#' needed for doctest

Ok::<(), BedErrorPlus>(())

```

List the first 5 individual (sample) ids, the first 5 SNP (variant) ids, and every unique chromosome. Then, read every genomic value in chromosome 5.

```rust

use ndarray::s; // '#' needed for doctest

use bedreader::{Bed, ReadOptions, asserteqnan, samplebed_file};

let filename = samplebedfile("somemissing.bed")?;

use std::collections::HashSet;

let mut bed = Bed::new(filename)?; println!("{:?}", bed.iid()?.slice(s![..5])); // Outputs ndarray: ["iid0", "iid1", "iid2", "iid3", "iid4"] println!("{:?}", bed.sid()?.slice(s![..5])); // Outputs ndarray: ["sid0", "sid1", "sid2", "sid3", "sid4"] println!("{:?}", bed.chromosome()?.iter().collect::>()); // Outputs: {"12", "10", "4", "8", "19", "21", "9", "15", "6", "16", "13", "7", "17", "18", "1", "22", "11", "2", "20", "3", "5", "14"} let val = ReadOptions::builder() .sidindex(bed.chromosome()?.map(|elem| elem == "5")) .f64() .read(&mut bed)?;

assert!(val.dim() == (100, 6));

use bed_reader::BedErrorPlus; // '#' needed for doctest

Ok::<(), BedErrorPlus>(())

```

Project Links