Struct bam_builder::BamBuilder

source ·
pub struct BamBuilder {
    pub read_length: usize,
    pub base_quality: u8,
    pub sample_name: SampleName,
    pub read_group_id: ReadGroupId,
    pub sort_order: BamSortOrder,
    pub header: Header,
    pub rng: StdRng,
    pub records: Vec<Record>,
    pub default_quals: String,
    pub counter: usize,
}
Expand description

Builder for BAM records.

Sets up defaults for reads that are added as well as the BAM header information. Use BamBuilder::new to generate a BamBuilder with appropriately populated fields.

The pattern of interaction with BamBuilder is to use the BamBuilder::frag_builder and BamBuilder::pair_builder methods to create a respective ReadFragSpecBuilder and ReadPairSpecBuilder. The *SpecBuilder is creates a record with its .build() method, and the result can be added to the builder with BamBuilder::add_pair or BamBuilder::add_frag.

Fields§

§read_length: usize

Length of reads to generate

§base_quality: u8

Default base quality to use across all qualities

§sample_name: SampleName

Name of the sample

§read_group_id: ReadGroupId

The read group to include in tags

§sort_order: BamSortOrder

The sort order of the generated reads

§header: Header

The BAM header

§rng: StdRng

Random number generator

§records: Vec<Record>

The collection of records being accumulated

§default_quals: String

The default string o qualities to use

§counter: usize

Counter for helping to increment the name

Implementations§

source§

impl BamBuilder

source

pub fn new( read_length: usize, base_quality: u8, sample_name: String, read_group_id: Option<String>, sort: BamSortOrder, pseudo_sd: Option<SequenceDict>, seed: Option<usize> ) -> BamBuilder

Create a BamBuilder that fills in defaults based on the passed in options.

Example
use bam_builder::{bam_order::BamSortOrder, BamBuilder};

let mut builder = BamBuilder::new(
    100,                    // default read_length
    30,                     // default base_quality
    "Pair".to_owned(),      // name of samples
    None,                   // optional read group id
    BamSortOrder::Unsorted, // how to sort reads when `.sort` is called
    None,                   // optional sequence_dict
    None,                   // seed used for generating random bases
);
assert_eq!(builder.read_group_id.0, String::from("A"));
assert_eq!(builder.sample_name.0, String::from("Pair"));
assert_eq!(builder.records.len(), 0);
Examples found in repository?
examples/ex1.rs (lines 5-13)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
fn main() {
    // Create a builder with all defaults except the read_len is 100
    let mut builder = BamBuilder::new(
        100,
        30,
        "Pair".to_owned(),
        None,
        BamSortOrder::Unsorted,
        None,
        None,
    );

    // Create a builder for read pair spec
    let records = builder
        .pair_builder()
        .contig(0)
        .start1(0)
        .start2(200)
        .unmapped1(false)
        .unmapped2(false)
        .bases1("A".repeat(100))
        .bases2("C".repeat(100))
        .build()
        .unwrap();
    println!("{:?}", records);

    // Add the pair to bam builder
    builder.add_pair(records);
    println!("{:?}", builder);
    // Write records to a file
    builder
        .to_path(std::path::Path::new(&String::from("./test.bam")))
        .unwrap();
}
source

pub fn frag_builder(&mut self) -> ReadFragSpecBuilder

Create a ReadFragSpecBuilder with defaults filled in based on BamBuilder.

source

pub fn add_frag(&mut self, frag_spec: ReadFragSpec)

Add a single fragment to the builder.

source

pub fn pair_builder(&mut self) -> ReadPairSpecBuilder

Create a ReadPairSpecBuilder with defaults filled in based on the BamBuilder.

Examples found in repository?
examples/ex1.rs (line 17)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
fn main() {
    // Create a builder with all defaults except the read_len is 100
    let mut builder = BamBuilder::new(
        100,
        30,
        "Pair".to_owned(),
        None,
        BamSortOrder::Unsorted,
        None,
        None,
    );

    // Create a builder for read pair spec
    let records = builder
        .pair_builder()
        .contig(0)
        .start1(0)
        .start2(200)
        .unmapped1(false)
        .unmapped2(false)
        .bases1("A".repeat(100))
        .bases2("C".repeat(100))
        .build()
        .unwrap();
    println!("{:?}", records);

    // Add the pair to bam builder
    builder.add_pair(records);
    println!("{:?}", builder);
    // Write records to a file
    builder
        .to_path(std::path::Path::new(&String::from("./test.bam")))
        .unwrap();
}
source

pub fn add_pair(&mut self, pair_spec: ReadPairSpec)

Add a read pair to the builder.

Examples found in repository?
examples/ex1.rs (line 30)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
fn main() {
    // Create a builder with all defaults except the read_len is 100
    let mut builder = BamBuilder::new(
        100,
        30,
        "Pair".to_owned(),
        None,
        BamSortOrder::Unsorted,
        None,
        None,
    );

    // Create a builder for read pair spec
    let records = builder
        .pair_builder()
        .contig(0)
        .start1(0)
        .start2(200)
        .unmapped1(false)
        .unmapped2(false)
        .bases1("A".repeat(100))
        .bases2("C".repeat(100))
        .build()
        .unwrap();
    println!("{:?}", records);

    // Add the pair to bam builder
    builder.add_pair(records);
    println!("{:?}", builder);
    // Write records to a file
    builder
        .to_path(std::path::Path::new(&String::from("./test.bam")))
        .unwrap();
}
source

pub fn to_path(&self, path: &Path) -> Result<(), Error>

Writes BAM records to specified path.

Note that BamBuilder::sort should be called ahead of writing to ensure records are sorted.

Examples found in repository?
examples/ex1.rs (line 34)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
fn main() {
    // Create a builder with all defaults except the read_len is 100
    let mut builder = BamBuilder::new(
        100,
        30,
        "Pair".to_owned(),
        None,
        BamSortOrder::Unsorted,
        None,
        None,
    );

    // Create a builder for read pair spec
    let records = builder
        .pair_builder()
        .contig(0)
        .start1(0)
        .start2(200)
        .unmapped1(false)
        .unmapped2(false)
        .bases1("A".repeat(100))
        .bases2("C".repeat(100))
        .build()
        .unwrap();
    println!("{:?}", records);

    // Add the pair to bam builder
    builder.add_pair(records);
    println!("{:?}", builder);
    // Write records to a file
    builder
        .to_path(std::path::Path::new(&String::from("./test.bam")))
        .unwrap();
}
source

pub fn to_tmp(&self) -> Result<NamedTempFile, Error>

Write records to a tempfile. Tempfile will be deleted when NamedTempFile goes out of scope.

Note that BamBuilder::sort should be called ahead of writing to ensure records are sorted.

source

pub fn sort(&mut self)

Sort the records added thus far by whichever BamSortOrder was specified.

Trait Implementations§

source§

impl Debug for BamBuilder

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V