Skip to content

Commit d5c5f94

Browse files
committed
improve default assembler ergonomics
The zero-configuration assembler path is now explicit through Assembler::default(), which avoids sending callers through a no-op builder just to get the standard pipeline. Documentation and benchmark examples that only need defaults now use the Default implementation, leaving the builder reserved for real configuration. This also adds a correction tuning knob for the cases where callers want base correction to be more conservative. CorrectionParams::with_min_base_correction_delta_q requires a minimum Phred-quality gap before disagreeing overlap bases are rewritten, while keeping the default at zero so existing behavior is preserved. Both pairassembler crates move to 0.1.1 so this public API ergonomic improvement can be published and consumed together.
1 parent 9a645a8 commit d5c5f94

8 files changed

Lines changed: 105 additions & 15 deletions

File tree

‎Cargo.lock‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn main() -> libpairassembly::Result<()> {
3030
"IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII",
3131
)?;
3232

33-
let assembler = Assembler::builder().build()?;
33+
let assembler = Assembler::default();
3434
let pair = PairInput::new(forward, reverse);
3535

3636
let merged = assembler

‎libpairassembly/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "libpairassembly"
3-
version = "0.1.0"
3+
version = "0.1.2"
44
description.workspace = true
55
authors.workspace = true
66
edition.workspace = true

‎libpairassembly/src/assembler/config.rs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl Assembler {
9797
/// )?,
9898
/// );
9999
///
100-
/// let mut assembler = Assembler::builder().build()?;
100+
/// let mut assembler = Assembler::default();
101101
/// let merged = assembler
102102
/// .process_pair(&pair)?
103103
/// .expect("this fixture has an acceptable overlap");
@@ -183,7 +183,7 @@ impl Assembler {
183183
/// "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII",
184184
/// )?,
185185
/// );
186-
/// let mut assembler = Assembler::builder().build()?;
186+
/// let mut assembler = Assembler::default();
187187
///
188188
/// let merged = assembler
189189
/// .on_pair(&pair)?
@@ -219,6 +219,12 @@ impl Assembler {
219219
}
220220
}
221221

222+
impl Default for Assembler {
223+
fn default() -> Self {
224+
Self::from_config(AssemblerConfig::default())
225+
}
226+
}
227+
222228
/// Builder for [`Assembler`].
223229
#[derive(Debug, Clone, Default)]
224230
pub struct AssemblerBuilder {

‎libpairassembly/src/correct.rs‎

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ pub struct CorrectionParams {
3737

3838
/// If true, correction updates overlap qualities only and leaves called bases unchanged.
3939
pub quality_only: bool,
40+
41+
/// Minimum Phred-quality difference required before a disagreement corrects bases.
42+
pub min_base_correction_delta_q: u8,
4043
}
4144

4245
impl Default for CorrectionParams {
4346
fn default() -> Self {
4447
Self {
4548
max_output_qual: MAX_CORRECTED_PHRED_OUTPUT,
4649
quality_only: false,
50+
min_base_correction_delta_q: 0,
4751
}
4852
}
4953
}
@@ -83,6 +87,26 @@ impl CorrectionParams {
8387
..self
8488
}
8589
}
90+
91+
/// Require a minimum quality gap before correcting disagreeing overlap bases.
92+
///
93+
/// Matching bases still receive corrected quality scores. Disagreeing bases keep the merge
94+
/// result, or the original mate bases for pair correction, unless the higher-quality base is at
95+
/// least this many Phred points better supported.
96+
///
97+
/// ```rust
98+
/// use libpairassembly::CorrectionParams;
99+
///
100+
/// let params = CorrectionParams::default().with_min_base_correction_delta_q(10);
101+
/// assert_eq!(params.min_base_correction_delta_q, 10);
102+
/// ```
103+
#[must_use]
104+
pub fn with_min_base_correction_delta_q(self, min_base_correction_delta_q: u8) -> Self {
105+
Self {
106+
min_base_correction_delta_q,
107+
..self
108+
}
109+
}
86110
}
87111

88112
/// Aligned overlap-local input window shared by correction kernels.
@@ -273,7 +297,7 @@ impl OverlapCorrector {
273297
for idx in 0..window.len() {
274298
let (corrected_base, corrected_qual) = self.correct_column(window, idx);
275299

276-
if !self.params.quality_only {
300+
if !self.params.quality_only && self.should_correct_base(window, idx) {
277301
seq_overlap[idx] = corrected_base;
278302
}
279303
qual_overlap[idx] = corrected_qual;
@@ -296,7 +320,7 @@ impl OverlapCorrector {
296320
for idx in 0..window.len() {
297321
let (corrected_base, corrected_qual) = self.correct_column(window, idx);
298322

299-
if !self.params.quality_only {
323+
if !self.params.quality_only && self.should_correct_base(window, idx) {
300324
fwd_seq_overlap[idx] = corrected_base;
301325
rev_seq_overlap_rc[idx] = corrected_base;
302326
}
@@ -315,6 +339,16 @@ impl OverlapCorrector {
315339
self.params,
316340
)
317341
}
342+
343+
fn should_correct_base(&self, window: &CorrectionWindow<'_>, idx: usize) -> bool {
344+
if window.forward_sequence()[idx] == window.reverse_sequence()[idx] {
345+
return true;
346+
}
347+
348+
let fwd_qual = window.forward_qualities()[idx];
349+
let rev_qual = window.reverse_qualities()[idx];
350+
fwd_qual.abs_diff(rev_qual) >= self.params.min_base_correction_delta_q
351+
}
318352
}
319353

320354
impl CorrectedOrientedPair {
@@ -933,6 +967,58 @@ mod tests {
933967
assert_eq!(right.sequence_bytes(), b"C");
934968
}
935969

970+
#[test]
971+
fn test_min_base_correction_delta_preserves_ambiguous_mismatch_bases() {
972+
let overlap = PairOverlap::from_oriented_slices(
973+
OrientedPairSlices {
974+
id: "read1",
975+
fwd_seq: b"A",
976+
fwd_quality_score_bytes: &[35],
977+
rev_seq_rc: b"G",
978+
rev_quality_score_bytes_rc: &[30],
979+
},
980+
OverlapBounds::new(1, 0, 0),
981+
)
982+
.expect("single-base overlap fixture should be valid");
983+
984+
let corrected =
985+
OverlapCorrector::new(CorrectionParams::default().with_min_base_correction_delta_q(10))
986+
.correct_pair_overlap(&overlap)
987+
.expect("correcting from pair-overlap slices should succeed");
988+
let (left, right) = OwnedReadPair::try_from(corrected)
989+
.expect("corrected pair should convert to owned reads")
990+
.into_reads();
991+
992+
assert_eq!(left.sequence_bytes(), b"A");
993+
assert_eq!(right.sequence_bytes(), b"C");
994+
}
995+
996+
#[test]
997+
fn test_min_base_correction_delta_corrects_well_supported_mismatch_bases() {
998+
let overlap = PairOverlap::from_oriented_slices(
999+
OrientedPairSlices {
1000+
id: "read1",
1001+
fwd_seq: b"A",
1002+
fwd_quality_score_bytes: &[40],
1003+
rev_seq_rc: b"G",
1004+
rev_quality_score_bytes_rc: &[20],
1005+
},
1006+
OverlapBounds::new(1, 0, 0),
1007+
)
1008+
.expect("single-base overlap fixture should be valid");
1009+
1010+
let corrected =
1011+
OverlapCorrector::new(CorrectionParams::default().with_min_base_correction_delta_q(10))
1012+
.correct_pair_overlap(&overlap)
1013+
.expect("correcting from pair-overlap slices should succeed");
1014+
let (left, right) = OwnedReadPair::try_from(corrected)
1015+
.expect("corrected pair should convert to owned reads")
1016+
.into_reads();
1017+
1018+
assert_eq!(left.sequence_bytes(), b"A");
1019+
assert_eq!(right.sequence_bytes(), b"T");
1020+
}
1021+
9361022
proptest! {
9371023
#[test]
9381024
fn proptest_compute_corrected_score_respects_basic_kernel_invariants(

‎libpairassembly/src/lib.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
//! )?;
3535
//!
3636
//! let pair = PairInput::new(forward, reverse);
37-
//! let mut assembler = Assembler::builder().build()?;
37+
//! let mut assembler = Assembler::default();
3838
//!
3939
//! let merged = assembler
4040
//! .process_pair(&pair)?
@@ -65,7 +65,7 @@
6565
//! )?,
6666
//! );
6767
//!
68-
//! let mut assembler = Assembler::builder().build()?;
68+
//! let mut assembler = Assembler::default();
6969
//! assert!(assembler.process_pair(&pair)?.is_none());
7070
//! # Ok(())
7171
//! # }

‎pairassembler/Cargo.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pairassembler"
3-
version = "0.1.0"
3+
version = "0.1.2"
44
description.workspace = true
55
authors.workspace = true
66
edition.workspace = true
@@ -30,7 +30,7 @@ serde = { version = "1.0.203", features = ["serde_derive", "derive", "rc"] }
3030
serde_json = "1.0.117"
3131
tracing = "0.1.41"
3232
tracing-subscriber = { version = "0.3.19", features = ["env-filter", "fmt"] }
33-
libpairassembly = { path = "../libpairassembly", version = "0.1.0" }
33+
libpairassembly = { path = "../libpairassembly", version = "0.1.2" }
3434
noodles = { version = "0.93.0", features = ["fastq"] }
3535
flate2 = { version = "1.1.1", default-features = false, features = ["zlib-rs"] }
3636
rayon = "1.10.0"

‎pairassembler/benches/in_memory_merge.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ struct BenchPair {
1818
}
1919

2020
fn in_memory_mixed_pairs(c: &mut Criterion) {
21-
let mut assembler = Assembler::builder().build().unwrap_or_else(|error| {
22-
panic!("failed to build default assembler for benchmark: {error}");
23-
});
21+
let mut assembler = Assembler::default();
2422
let pairs = build_in_memory_pairs();
2523

2624
c.bench_function("in_memory_mixed_300_pairs", |b| {

0 commit comments

Comments
 (0)