@@ -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
4245impl 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
320354impl 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(
0 commit comments