@@ -2,7 +2,7 @@ use std::fmt::Display;
22
33use compact_genome:: interface:: sequence:: GenomeSequence ;
44use generic_a_star:: { AStarNode , cost:: AStarCost } ;
5- use num_traits:: { Bounded , Zero } ;
5+ use num_traits:: { Bounded , CheckedAdd , Zero } ;
66use strategies:: {
77 AlignmentStrategiesNodeMemory , AlignmentStrategySelector , node_ord:: NodeOrdStrategy ,
88 primary_match:: PrimaryMatchStrategy ,
@@ -25,7 +25,7 @@ pub use identifier::{
2525
2626use crate :: {
2727 a_star_aligner:: template_switch_distance:: strategies:: descendant:: TemplateSwitchDescendantStrategy ,
28- config:: BaseCost ,
28+ config:: BaseCost , costs :: cost_function :: CostFunction ,
2929} ;
3030
3131#[ derive( Debug , Clone , PartialEq , Eq ) ]
@@ -223,8 +223,8 @@ impl<Strategies: AlignmentStrategySelector> Node<Strategies> {
223223 SubsequenceType : GenomeSequence < Strategies :: Alphabet , SubsequenceType > + ?Sized ,
224224 > (
225225 & ' result self ,
226- rq_qr_cost_increment : Strategies :: Cost ,
227- rr_qq_cost_increment : Strategies :: Cost ,
226+ rq_qr_offset_costs : & ' result CostFunction < isize , Strategies :: Cost > ,
227+ rr_qq_offset_costs : & ' result CostFunction < isize , Strategies :: Cost > ,
228228 base_cost : & ' result BaseCost < Strategies :: Cost > ,
229229 context : & ' result Context < SubsequenceType , Strategies > ,
230230 ) -> impl ' result + Iterator < Item = Self > {
@@ -234,10 +234,6 @@ impl<Strategies: AlignmentStrategySelector> Node<Strategies> {
234234 ) {
235235 unreachable ! ( "This method is only called on primary nodes." )
236236 }
237- debug_assert ! (
238- rq_qr_cost_increment < Strategies :: Cost :: max_value( )
239- || rr_qq_cost_increment < Strategies :: Cost :: max_value( ) ,
240- ) ;
241237
242238 self . node_data
243239 . identifier
@@ -268,34 +264,42 @@ impl<Strategies: AlignmentStrategySelector> Node<Strategies> {
268264 * template_switch_ancestor,
269265 * template_switch_direction,
270266 ) ;
271- let cost_increment = match ( * template_switch_descendant, * template_switch_ancestor)
272- {
267+ let cost_function = match ( * template_switch_descendant, * template_switch_ancestor) {
273268 ( TemplateSwitchDescendant :: Reference , TemplateSwitchAncestor :: Query )
274269 | ( TemplateSwitchDescendant :: Query , TemplateSwitchAncestor :: Reference ) => {
275- rq_qr_cost_increment
270+ rq_qr_offset_costs
276271 }
277272 ( TemplateSwitchDescendant :: Reference , TemplateSwitchAncestor :: Reference )
278273 | ( TemplateSwitchDescendant :: Query , TemplateSwitchAncestor :: Query ) => {
279- rr_qq_cost_increment
274+ rr_qq_offset_costs
280275 }
281276 } ;
277+ let cost_increment = cost_function. evaluate ( template_switch_first_offset) ;
282278
283- ( base_cost != Strategies :: Cost :: max_value ( )
284- && cost_increment != Strategies :: Cost :: max_value ( ) )
285- . then ( || {
286- self . generate_successor (
287- identifier,
288- base_cost + cost_increment,
289- AlignmentType :: TemplateSwitchEntrance {
290- descendant : * template_switch_descendant,
291- ancestor : * template_switch_ancestor,
292- direction : * template_switch_direction,
293- equal_cost_range : EqualCostRange :: new_invalid ( ) ,
294- first_offset : * template_switch_first_offset,
295- } ,
296- context,
297- )
298- } )
279+ if base_cost != Strategies :: Cost :: max_value ( )
280+ && cost_increment != Strategies :: Cost :: max_value ( )
281+ {
282+ if let Some ( cost_increment) = base_cost. checked_add ( & cost_increment)
283+ && cost_increment != Strategies :: Cost :: max_value ( )
284+ {
285+ Some ( self . generate_successor (
286+ identifier,
287+ cost_increment,
288+ AlignmentType :: TemplateSwitchEntrance {
289+ descendant : * template_switch_descendant,
290+ ancestor : * template_switch_ancestor,
291+ direction : * template_switch_direction,
292+ equal_cost_range : EqualCostRange :: new_invalid ( ) ,
293+ first_offset : * template_switch_first_offset,
294+ } ,
295+ context,
296+ ) )
297+ } else {
298+ None
299+ }
300+ } else {
301+ None
302+ }
299303 } )
300304 }
301305
0 commit comments