Skip to main content

SamBuilder

Struct SamBuilder 

Source
pub struct SamBuilder {
    pub header: Header,
    /* private fields */
}
Expand description

Builder for creating test SAM/BAM records and files.

This builder simplifies test record creation by providing methods to add paired-end and single-end reads with sensible defaults. Records are accumulated internally and can be written to a file or iterated over.

§Examples

use fgumi_sam::builder::{SamBuilder, Strand};

// Create a builder with defaults
let mut builder = SamBuilder::new();

// Add a mapped pair
let pair = builder.add_pair()
    .contig(0)
    .start1(100)
    .start2(200)
    .build();

// Add a fragment
let frag = builder.add_frag()
    .name("frag1")
    .bases("ACGTACGT")
    .contig(0)
    .start(150)
    .build();

// Write to a temp file
let path = builder.to_temp_file().unwrap();

Fields§

§header: Header

SAM header

Implementations§

Source§

impl SamBuilder

Source

pub fn new() -> SamBuilder

Creates a new builder with default settings.

Default settings:

  • Read length: 100
  • Base quality: 30
  • Read group ID: “A”
  • Sample name: “Sample”
  • Reference sequences: chr1-chr22, chrX, chrY, chrM (200MB each)
Source

pub fn with_defaults(read_length: usize, base_quality: u8) -> SamBuilder

Creates a new builder with specified read length and base quality.

§Panics

Panics if DEFAULT_REFERENCE_LENGTH is zero (impossible in practice).

Source

pub fn new_unmapped() -> SamBuilder

Creates a builder for unmapped reads (no reference sequences in header).

Source

pub fn with_single_ref(ref_name: &str, ref_length: usize) -> SamBuilder

Creates a builder with a single reference sequence.

§Panics

Panics if ref_length is zero.

Source

pub fn records(&self) -> &[RecordBuf]

Returns a reference to the accumulated records.

Source

pub fn len(&self) -> usize

Returns the number of accumulated records.

Source

pub fn is_empty(&self) -> bool

Returns true if no records have been added.

Source

pub fn clear(&mut self)

Clears all accumulated records.

Source

pub fn iter(&self) -> impl Iterator<Item = &RecordBuf>

Returns an iterator over the accumulated records.

Source

pub fn push_record(&mut self, record: RecordBuf)

Pushes a raw record to the collection.

This is useful when you need to add pre-built records to the collection.

Source

pub fn add_pair(&mut self) -> PairBuilder<'_>

Starts building a paired-end read pair.

Returns a PairBuilder that can be configured and then built. The resulting records are added to this builder and also returned.

Source

pub fn add_frag(&mut self) -> FragBuilder<'_>

Starts building a single-end (fragment) read.

Returns a FragBuilder that can be configured and then built. The resulting record is added to this builder and also returned.

Source

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

Writes accumulated records to a BAM file.

§Errors

Returns an error if the file cannot be created or written.

Source

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

Writes accumulated records to a SAM file.

§Errors

Returns an error if the file cannot be created or written.

Source

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

Writes to a temporary BAM file and returns the path.

§Errors

Returns an error if the temporary file cannot be created or written.

Source§

impl SamBuilder

Source

pub fn new_mapped() -> SamBuilder

Creates a new builder for mapped BAM files with a single reference.

Creates a builder with “chr1” as the reference name and REFERENCE_LENGTH as length.

Source

pub fn add_pair_with_attrs( &mut self, name: &str, start1: Option<usize>, start2: Option<usize>, strand1: bool, strand2: bool, attrs: &HashMap<&str, Value>, )

Adds a pair using positional arguments.

A convenience method for adding pairs with common parameters.

§Arguments
  • name - Read name
  • start1 - Start position for R1 (None = unmapped)
  • start2 - Start position for R2 (None = unmapped)
  • strand1 - true = Plus, false = Minus for R1
  • strand2 - true = Plus, false = Minus for R2
  • attrs - Attributes to add to both reads
Source

pub fn add_frag_with_attrs( &mut self, name: &str, start: Option<usize>, strand: bool, attrs: &HashMap<&str, Value>, )

Adds a fragment using positional arguments.

A convenience method for adding fragments with common parameters.

§Arguments
  • name - Read name
  • start - Start position (None = unmapped)
  • strand - true = Plus, false = Minus
  • attrs - Attributes to add
Source

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

Writes accumulated records to a BAM file.

Alias for write_bam.

§Errors

Returns an error if the BAM file cannot be created or written to.

Trait Implementations§

Source§

impl Debug for SamBuilder

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for SamBuilder

Source§

fn default() -> SamBuilder

Returns the “default value” for a type. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

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>,

Source§

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.