From 8b1317a5f65a80b04415a11428b78978318df1b1 Mon Sep 17 00:00:00 2001 From: Jasper Krauter <12259417+jeeeesper@users.noreply.github.com> Date: Wed, 6 May 2026 11:15:43 +0300 Subject: [PATCH 1/2] feat: add flag to disable extending the alignment range --- lib_tsalign/src/a_star_aligner.rs | 23 ++++++++++--------- .../configurable_a_star_align.rs | 7 +++++- lib_tsalign/src/tests.rs | 4 ++++ python_bindings/src/lib.rs | 1 + tsalign/src/align.rs | 5 ++++ ...template_switch_distance_type_selectors.rs | 1 + 6 files changed, 29 insertions(+), 12 deletions(-) diff --git a/lib_tsalign/src/a_star_aligner.rs b/lib_tsalign/src/a_star_aligner.rs index 5c8c7cf..fc61f9a 100644 --- a/lib_tsalign/src/a_star_aligner.rs +++ b/lib_tsalign/src/a_star_aligner.rs @@ -181,7 +181,7 @@ pub fn template_switch_distance_a_star_align< query: &SubsequenceType, reference_name: &str, query_name: &str, - range: AlignmentRange, + mut range: AlignmentRange, config: &config::TemplateSwitchConfig< Strategies::Alphabet, ::Cost, @@ -189,6 +189,7 @@ pub fn template_switch_distance_a_star_align< cost_limit: Option, memory_limit: Option, force_label_correcting: bool, + extend_beyond_range: bool, template_switch_count_memory: ::Memory, ) -> AlignmentResult where @@ -227,16 +228,16 @@ where ); info!("Main alignment finished"); - info!("Extending template switches"); - debug!("CIGAR before extending: {}", result.cigar()); - let mut range = range; - let extension_steps = - result.extend_beyond_range_without_increasing_cost(reference, query, &mut range, config); - let range = range; - info!( - "Extended alignment {extension_steps} steps beyond the alignment range without increasing alignment costs" - ); - info!("Alignment ranges after extension {range}"); + if extend_beyond_range { + info!("Extending range"); + debug!("CIGAR before extending: {}", result.cigar()); + let extension_steps = result + .extend_beyond_range_without_increasing_cost(reference, query, &mut range, config); + info!( + "Extended alignment {extension_steps} steps beyond the alignment range without increasing alignment costs" + ); + info!("Alignment ranges after extension {range}"); + } info!("Extending template switches"); result.compute_ts_equal_cost_ranges(reference, query, &range, config); diff --git a/lib_tsalign/src/a_star_aligner/configurable_a_star_align.rs b/lib_tsalign/src/a_star_aligner/configurable_a_star_align.rs index 0c65830..4b6cf3a 100644 --- a/lib_tsalign/src/a_star_aligner/configurable_a_star_align.rs +++ b/lib_tsalign/src/a_star_aligner/configurable_a_star_align.rs @@ -95,6 +95,7 @@ struct QueryData<'a> { ranges: Option, cost_limit: Option, memory_limit: Option, + extend_beyond_range: bool, } #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] @@ -190,6 +191,7 @@ impl Aligner { ranges: Option, cost_limit: Option, memory_limit: Option, + extend_beyond_range: bool, ) -> AlignmentResult { let data = QueryData { reference_name, @@ -199,6 +201,7 @@ impl Aligner { ranges, cost_limit, memory_limit, + extend_beyond_range, }; self.align_select_min_length_strategy(data) } @@ -302,6 +305,7 @@ impl Aligner { cost_limit, data.memory_limit, false, + data.extend_beyond_range, count_strategy_memory, ) } @@ -330,7 +334,8 @@ mod tests { AlignmentCoordinates::new(33, 214) ).into(), None, - None); + None, + true); println!("{res:#?}"); assert!(res.statistics().cost.is_sign_positive()); } diff --git a/lib_tsalign/src/tests.rs b/lib_tsalign/src/tests.rs index 04699b1..adf3aec 100644 --- a/lib_tsalign/src/tests.rs +++ b/lib_tsalign/src/tests.rs @@ -71,6 +71,7 @@ fn test_tsnax_disc1_473() { None, None, false, + true, (), ); assert_eq!( @@ -111,6 +112,7 @@ fn test_tsnax_disc1_473() { None, None, false, + true, (), ); println!("{sample_alignment}"); @@ -141,6 +143,7 @@ fn test_tsnax_disc1_473() { None, None, false, + true, (), ); println!("{sample_alignment}"); @@ -171,6 +174,7 @@ fn test_tsnax_disc1_473() { None, None, false, + true, (), ); println!("{sample_alignment}"); diff --git a/python_bindings/src/lib.rs b/python_bindings/src/lib.rs index fbbd85f..d450f6a 100644 --- a/python_bindings/src/lib.rs +++ b/python_bindings/src/lib.rs @@ -129,6 +129,7 @@ impl TSAligner { Some(ranges), cost_limit, memory_limit, + true, ); match result { diff --git a/tsalign/src/align.rs b/tsalign/src/align.rs index 7ed18c8..cd294bd 100644 --- a/tsalign/src/align.rs +++ b/tsalign/src/align.rs @@ -210,6 +210,11 @@ pub struct Cli { /// Template switch inners can still align to the full sequence. #[clap(long)] use_embedded_rq_ranges: bool, + + /// If set, the aligner will not attempt to extend the alignment beyond the specified range + /// without increasing cost. + #[clap(long)] + dont_extend_beyond_range: bool, } #[derive(Args)] diff --git a/tsalign/src/align/template_switch_distance_type_selectors.rs b/tsalign/src/align/template_switch_distance_type_selectors.rs index d2cd440..57ffba6 100644 --- a/tsalign/src/align/template_switch_distance_type_selectors.rs +++ b/tsalign/src/align/template_switch_distance_type_selectors.rs @@ -356,6 +356,7 @@ fn align_a_star_template_switch_distance_call< cli.cost_limit, cli.memory_limit, cli.force_label_correcting, + !cli.dont_extend_beyond_range, template_switch_count_memory, ); info!("Finished aligning"); From 188d2c5f57fb8fed40b32287460069bbb8c9d897 Mon Sep 17 00:00:00 2001 From: Jasper Krauter <12259417+jeeeesper@users.noreply.github.com> Date: Wed, 6 May 2026 14:01:46 +0300 Subject: [PATCH 2/2] fix: clippy --- lib_tsalign/src/costs/cost_function.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib_tsalign/src/costs/cost_function.rs b/lib_tsalign/src/costs/cost_function.rs index 50cf2d1..b98d969 100644 --- a/lib_tsalign/src/costs/cost_function.rs +++ b/lib_tsalign/src/costs/cost_function.rs @@ -187,12 +187,9 @@ impl CostFunction