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
impl BamBuilder
sourcepub 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
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?
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();
}
sourcepub fn frag_builder(&mut self) -> ReadFragSpecBuilder
pub fn frag_builder(&mut self) -> ReadFragSpecBuilder
Create a ReadFragSpecBuilder with defaults filled in based on BamBuilder.
sourcepub fn add_frag(&mut self, frag_spec: ReadFragSpec)
pub fn add_frag(&mut self, frag_spec: ReadFragSpec)
Add a single fragment to the builder.
sourcepub fn pair_builder(&mut self) -> ReadPairSpecBuilder
pub fn pair_builder(&mut self) -> ReadPairSpecBuilder
Create a ReadPairSpecBuilder with defaults filled in based on the BamBuilder.
Examples found in repository?
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();
}
sourcepub fn add_pair(&mut self, pair_spec: ReadPairSpec)
pub fn add_pair(&mut self, pair_spec: ReadPairSpec)
Add a read pair to the builder.
Examples found in repository?
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();
}
sourcepub fn to_path(&self, path: &Path) -> Result<(), Error>
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?
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();
}
sourcepub fn to_tmp(&self) -> Result<NamedTempFile, Error>
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.
sourcepub fn sort(&mut self)
pub fn sort(&mut self)
Sort the records added thus far by whichever BamSortOrder
was specified.