Skip to main content

ReadBatch

Struct ReadBatch 

Source
pub struct ReadBatch { /* private fields */ }
Expand description

A batch of paired-end reads to map.

All slices must have the same length (count). Sequences are ASCII DNA strings (ACGTN) and qualities are phred+33 ASCII strings.

Implementations§

Source§

impl ReadBatch

Source

pub fn new() -> Self

Create a new empty batch.

Examples found in repository?
examples/align.rs (line 45)
12fn main() {
13    let args: Vec<String> = std::env::args().collect();
14    if args.len() < 2 {
15        eprintln!("Usage: {} <index_prefix>", args[0]);
16        std::process::exit(1);
17    }
18    let index_prefix = &args[1];
19
20    // Configure the mapper.
21    let options = MapperOptions {
22        sensitivity: Sensitivity::Full,
23        threads: 2,
24        verbose: 1,
25        ..Default::default()
26    };
27
28    // Open the index.
29    eprintln!("Opening index: {index_prefix}");
30    let mapper = YaraMapper::open(index_prefix, &options).unwrap_or_else(|e| {
31        eprintln!("Failed to open index: {e}");
32        std::process::exit(1);
33    });
34
35    eprintln!("Loaded {} contigs", mapper.contig_count());
36    for (i, (name, len)) in mapper.contig_names().iter().zip(mapper.contig_lengths()).enumerate() {
37        eprintln!("  contig {i}: {name} ({len} bp)");
38        if i >= 5 {
39            eprintln!("  ... ({} more)", mapper.contig_count() - i - 1);
40            break;
41        }
42    }
43
44    // Create a synthetic read pair (these won't align to anything real).
45    let mut batch = ReadBatch::new();
46    batch
47        .push(
48            "test_read_1",
49            ReadEnd {
50                seq: b"ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGT",
51                qual: b"IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII",
52            },
53            ReadEnd {
54                seq: b"TGCATGCATGCATGCATGCATGCATGCATGCATGCATGCATGCATGCATGCA",
55                qual: b"IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII",
56            },
57        )
58        .expect("failed to add read pair");
59
60    // Map the reads.
61    eprintln!("Mapping {} read pair(s)...", batch.len());
62    match mapper.map_paired(&batch) {
63        Ok(records) => {
64            eprintln!("Got {} alignment record(s)", records.len());
65            for rec in &records {
66                println!(
67                    "pair={} read1={} contig={} pos={} rev={} unmapped={} secondary={} mapq={} nm={} cigar={} flag={}",
68                    rec.read_pair_index,
69                    rec.is_read1,
70                    rec.contig_id,
71                    rec.pos,
72                    rec.is_reverse,
73                    rec.is_unmapped,
74                    rec.is_secondary,
75                    rec.mapq,
76                    rec.nm,
77                    rec.cigar_string(),
78                    rec.flag,
79                );
80            }
81        }
82        Err(e) => {
83            eprintln!("Mapping failed: {e}");
84            std::process::exit(1);
85        }
86    }
87}
Source

pub fn with_capacity(n: usize) -> Self

Create a batch with pre-allocated capacity for n pairs.

Source

pub fn push( &mut self, name: &str, r1: ReadEnd<'_>, r2: ReadEnd<'_>, ) -> Result<(), YaraError>

Add a read pair to the batch.

§Errors

Returns YaraError::InvalidInput if any input contains a null byte or if sequence and quality lengths do not match.

Examples found in repository?
examples/align.rs (lines 47-57)
12fn main() {
13    let args: Vec<String> = std::env::args().collect();
14    if args.len() < 2 {
15        eprintln!("Usage: {} <index_prefix>", args[0]);
16        std::process::exit(1);
17    }
18    let index_prefix = &args[1];
19
20    // Configure the mapper.
21    let options = MapperOptions {
22        sensitivity: Sensitivity::Full,
23        threads: 2,
24        verbose: 1,
25        ..Default::default()
26    };
27
28    // Open the index.
29    eprintln!("Opening index: {index_prefix}");
30    let mapper = YaraMapper::open(index_prefix, &options).unwrap_or_else(|e| {
31        eprintln!("Failed to open index: {e}");
32        std::process::exit(1);
33    });
34
35    eprintln!("Loaded {} contigs", mapper.contig_count());
36    for (i, (name, len)) in mapper.contig_names().iter().zip(mapper.contig_lengths()).enumerate() {
37        eprintln!("  contig {i}: {name} ({len} bp)");
38        if i >= 5 {
39            eprintln!("  ... ({} more)", mapper.contig_count() - i - 1);
40            break;
41        }
42    }
43
44    // Create a synthetic read pair (these won't align to anything real).
45    let mut batch = ReadBatch::new();
46    batch
47        .push(
48            "test_read_1",
49            ReadEnd {
50                seq: b"ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGT",
51                qual: b"IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII",
52            },
53            ReadEnd {
54                seq: b"TGCATGCATGCATGCATGCATGCATGCATGCATGCATGCATGCATGCATGCA",
55                qual: b"IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII",
56            },
57        )
58        .expect("failed to add read pair");
59
60    // Map the reads.
61    eprintln!("Mapping {} read pair(s)...", batch.len());
62    match mapper.map_paired(&batch) {
63        Ok(records) => {
64            eprintln!("Got {} alignment record(s)", records.len());
65            for rec in &records {
66                println!(
67                    "pair={} read1={} contig={} pos={} rev={} unmapped={} secondary={} mapq={} nm={} cigar={} flag={}",
68                    rec.read_pair_index,
69                    rec.is_read1,
70                    rec.contig_id,
71                    rec.pos,
72                    rec.is_reverse,
73                    rec.is_unmapped,
74                    rec.is_secondary,
75                    rec.mapq,
76                    rec.nm,
77                    rec.cigar_string(),
78                    rec.flag,
79                );
80            }
81        }
82        Err(e) => {
83            eprintln!("Mapping failed: {e}");
84            std::process::exit(1);
85        }
86    }
87}
Source

pub fn len(&self) -> usize

Number of read pairs in the batch.

Examples found in repository?
examples/align.rs (line 61)
12fn main() {
13    let args: Vec<String> = std::env::args().collect();
14    if args.len() < 2 {
15        eprintln!("Usage: {} <index_prefix>", args[0]);
16        std::process::exit(1);
17    }
18    let index_prefix = &args[1];
19
20    // Configure the mapper.
21    let options = MapperOptions {
22        sensitivity: Sensitivity::Full,
23        threads: 2,
24        verbose: 1,
25        ..Default::default()
26    };
27
28    // Open the index.
29    eprintln!("Opening index: {index_prefix}");
30    let mapper = YaraMapper::open(index_prefix, &options).unwrap_or_else(|e| {
31        eprintln!("Failed to open index: {e}");
32        std::process::exit(1);
33    });
34
35    eprintln!("Loaded {} contigs", mapper.contig_count());
36    for (i, (name, len)) in mapper.contig_names().iter().zip(mapper.contig_lengths()).enumerate() {
37        eprintln!("  contig {i}: {name} ({len} bp)");
38        if i >= 5 {
39            eprintln!("  ... ({} more)", mapper.contig_count() - i - 1);
40            break;
41        }
42    }
43
44    // Create a synthetic read pair (these won't align to anything real).
45    let mut batch = ReadBatch::new();
46    batch
47        .push(
48            "test_read_1",
49            ReadEnd {
50                seq: b"ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGT",
51                qual: b"IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII",
52            },
53            ReadEnd {
54                seq: b"TGCATGCATGCATGCATGCATGCATGCATGCATGCATGCATGCATGCATGCA",
55                qual: b"IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII",
56            },
57        )
58        .expect("failed to add read pair");
59
60    // Map the reads.
61    eprintln!("Mapping {} read pair(s)...", batch.len());
62    match mapper.map_paired(&batch) {
63        Ok(records) => {
64            eprintln!("Got {} alignment record(s)", records.len());
65            for rec in &records {
66                println!(
67                    "pair={} read1={} contig={} pos={} rev={} unmapped={} secondary={} mapq={} nm={} cigar={} flag={}",
68                    rec.read_pair_index,
69                    rec.is_read1,
70                    rec.contig_id,
71                    rec.pos,
72                    rec.is_reverse,
73                    rec.is_unmapped,
74                    rec.is_secondary,
75                    rec.mapq,
76                    rec.nm,
77                    rec.cigar_string(),
78                    rec.flag,
79                );
80            }
81        }
82        Err(e) => {
83            eprintln!("Mapping failed: {e}");
84            std::process::exit(1);
85        }
86    }
87}
Source

pub fn is_empty(&self) -> bool

Whether the batch is empty.

Trait Implementations§

Source§

impl Default for ReadBatch

Source§

fn default() -> ReadBatch

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