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?
3fn main() {
4 // Create a builder with all defaults except the read_len is 100
5 let mut builder = BamBuilder::new(
6 100,
7 30,
8 "Pair".to_owned(),
9 None,
10 BamSortOrder::Unsorted,
11 None,
12 None,
13 );
14
15 // Create a builder for read pair spec
16 let records = builder
17 .pair_builder()
18 .contig(0)
19 .start1(0)
20 .start2(200)
21 .unmapped1(false)
22 .unmapped2(false)
23 .bases1("A".repeat(100))
24 .bases2("C".repeat(100))
25 .build()
26 .unwrap();
27 println!("{:?}", records);
28
29 // Add the pair to bam builder
30 builder.add_pair(records);
31 println!("{:?}", builder);
32 // Write records to a file
33 builder
34 .to_path(std::path::Path::new(&String::from("./test.bam")))
35 .unwrap();
36}
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?
3fn main() {
4 // Create a builder with all defaults except the read_len is 100
5 let mut builder = BamBuilder::new(
6 100,
7 30,
8 "Pair".to_owned(),
9 None,
10 BamSortOrder::Unsorted,
11 None,
12 None,
13 );
14
15 // Create a builder for read pair spec
16 let records = builder
17 .pair_builder()
18 .contig(0)
19 .start1(0)
20 .start2(200)
21 .unmapped1(false)
22 .unmapped2(false)
23 .bases1("A".repeat(100))
24 .bases2("C".repeat(100))
25 .build()
26 .unwrap();
27 println!("{:?}", records);
28
29 // Add the pair to bam builder
30 builder.add_pair(records);
31 println!("{:?}", builder);
32 // Write records to a file
33 builder
34 .to_path(std::path::Path::new(&String::from("./test.bam")))
35 .unwrap();
36}
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?
3fn main() {
4 // Create a builder with all defaults except the read_len is 100
5 let mut builder = BamBuilder::new(
6 100,
7 30,
8 "Pair".to_owned(),
9 None,
10 BamSortOrder::Unsorted,
11 None,
12 None,
13 );
14
15 // Create a builder for read pair spec
16 let records = builder
17 .pair_builder()
18 .contig(0)
19 .start1(0)
20 .start2(200)
21 .unmapped1(false)
22 .unmapped2(false)
23 .bases1("A".repeat(100))
24 .bases2("C".repeat(100))
25 .build()
26 .unwrap();
27 println!("{:?}", records);
28
29 // Add the pair to bam builder
30 builder.add_pair(records);
31 println!("{:?}", builder);
32 // Write records to a file
33 builder
34 .to_path(std::path::Path::new(&String::from("./test.bam")))
35 .unwrap();
36}
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?
3fn main() {
4 // Create a builder with all defaults except the read_len is 100
5 let mut builder = BamBuilder::new(
6 100,
7 30,
8 "Pair".to_owned(),
9 None,
10 BamSortOrder::Unsorted,
11 None,
12 None,
13 );
14
15 // Create a builder for read pair spec
16 let records = builder
17 .pair_builder()
18 .contig(0)
19 .start1(0)
20 .start2(200)
21 .unmapped1(false)
22 .unmapped2(false)
23 .bases1("A".repeat(100))
24 .bases2("C".repeat(100))
25 .build()
26 .unwrap();
27 println!("{:?}", records);
28
29 // Add the pair to bam builder
30 builder.add_pair(records);
31 println!("{:?}", builder);
32 // Write records to a file
33 builder
34 .to_path(std::path::Path::new(&String::from("./test.bam")))
35 .unwrap();
36}
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.