Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib_ts_chainalign/src/chaining_lower_bounds/ts_jump/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ fn test_max_match_run_0() {
},
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..200,
ancestor_gap: -100..100,
Expand Down Expand Up @@ -65,7 +66,8 @@ fn test_max_match_run_1() {
},
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..200,
ancestor_gap: -100..100,
Expand Down Expand Up @@ -110,7 +112,8 @@ fn test_max_match_run_2() {
},
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..200,
ancestor_gap: -100..100,
Expand Down
10 changes: 7 additions & 3 deletions lib_ts_chainalign/src/costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ pub struct GapAffineCosts<Cost> {

#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
pub struct TsLimits {
/// The maximum range of the 12-jump of a template switch.
/// The maximum range of the 12-jump of an inter (12 or 21) template switch.
/// This parameter is ignored for now.
pub jump_12: Range<isize>,
pub inter_jump_12: Range<isize>,
/// The maximum range of the 12-jump of an intra (11 or 22) template switch.
/// This parameter is ignored for now.
pub intra_jump_12: Range<isize>,
/// The maximum range of the 34-jump of a template switch.
Comment thread
sebschmi marked this conversation as resolved.
/// This parameter is ignored for now.
pub jump_34: Range<isize>,
Expand Down Expand Up @@ -63,7 +66,8 @@ impl<Cost: Zero> GapAffineCosts<Cost> {
impl TsLimits {
pub fn new_unlimited() -> Self {
Self {
jump_12: isize::MIN..isize::MAX,
inter_jump_12: isize::MIN..isize::MAX,
intra_jump_12: isize::MIN..isize::MAX,
jump_34: isize::MIN..isize::MAX,
length_23: usize::MIN..usize::MAX,
ancestor_gap: isize::MIN..isize::MAX,
Expand Down
3 changes: 2 additions & 1 deletion lib_ts_chainalign/src/costs/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ impl<AlphabetType: Alphabet, Cost: AStarCost> From<TemplateSwitchConfig<Alphabet
.into(),
ts_base_cost,
ts_limits: TsLimits {
jump_12: value.offset_costs.zero_range().unwrap(),
inter_jump_12: value.rq_qr_offset_costs.zero_range().unwrap(),
intra_jump_12: value.rr_qq_offset_costs.zero_range().unwrap(),
// tsalign costs do not support limiting this.
jump_34: isize::MIN..isize::MAX,
length_23: value.length_costs.zero_range().unwrap(),
Expand Down
27 changes: 18 additions & 9 deletions lib_ts_chainalign/src/exact_chaining/ts_12_jump/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ fn test_start_end() {
),
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..100,
ancestor_gap: -100..100,
Expand Down Expand Up @@ -86,7 +87,8 @@ fn test_partial_alignment() {
),
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..100,
ancestor_gap: -100..100,
Expand Down Expand Up @@ -134,7 +136,8 @@ fn test_gap_directions() {
),
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..100,
ancestor_gap: -100..100,
Expand Down Expand Up @@ -189,7 +192,8 @@ fn test_max_match_run_0() {
),
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..100,
ancestor_gap: -100..100,
Expand Down Expand Up @@ -235,7 +239,8 @@ fn test_max_match_run_1() {
),
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..100,
ancestor_gap: -100..100,
Expand Down Expand Up @@ -283,7 +288,8 @@ fn test_max_match_run_2() {
),
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..100,
ancestor_gap: -100..100,
Expand Down Expand Up @@ -338,7 +344,8 @@ fn test_only_jump() {
),
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..100,
ancestor_gap: -100..100,
Expand Down Expand Up @@ -381,7 +388,8 @@ fn test_only_jump_start() {
),
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..100,
ancestor_gap: -100..100,
Expand Down Expand Up @@ -424,7 +432,8 @@ fn test_only_jump_end() {
),
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..100,
ancestor_gap: -100..100,
Expand Down
27 changes: 18 additions & 9 deletions lib_ts_chainalign/src/exact_chaining/ts_34_jump/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ fn test_start_end() {
),
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..100,
ancestor_gap: -100..100,
Expand Down Expand Up @@ -80,7 +81,8 @@ fn test_partial_alignment() {
),
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..100,
ancestor_gap: -100..100,
Expand Down Expand Up @@ -122,7 +124,8 @@ fn test_gap_directions() {
),
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..100,
ancestor_gap: -100..100,
Expand Down Expand Up @@ -171,7 +174,8 @@ fn test_max_match_run_0() {
),
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..100,
ancestor_gap: -100..100,
Expand Down Expand Up @@ -211,7 +215,8 @@ fn test_max_match_run_1() {
),
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..100,
ancestor_gap: -100..100,
Expand Down Expand Up @@ -259,7 +264,8 @@ fn test_max_match_run_2() {
),
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..100,
ancestor_gap: -100..100,
Expand Down Expand Up @@ -308,7 +314,8 @@ fn test_only_jump() {
),
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..100,
ancestor_gap: -100..100,
Expand Down Expand Up @@ -345,7 +352,8 @@ fn test_only_jump_start() {
),
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..100,
ancestor_gap: -100..100,
Expand Down Expand Up @@ -382,7 +390,8 @@ fn test_only_jump_end() {
),
ts_base_cost: U32Cost::from(2u8),
ts_limits: TsLimits {
jump_12: -100..100,
inter_jump_12: -100..100,
intra_jump_12: -100..100,
jump_34: -100..100,
length_23: 0..100,
ancestor_gap: -100..100,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -744,9 +744,11 @@ impl Alignment<AlignmentType> {
secondary = ts_secondary;
direction = ts_direction;
let cost_increment = config.base_cost.get(primary, secondary, direction);
let Some(cost_increment) =
cost_increment.checked_add(&config.offset_costs.evaluate(&first_offset))
else {
let Some(cost_increment) = cost_increment.checked_add(
&config
.offset_costs(primary, secondary)
.evaluate(&first_offset),
) else {
return Cost::max_value();
};
primary_index = match primary {
Expand Down Expand Up @@ -863,22 +865,22 @@ mod tests {
static START_COSTS: LazyLock<Vec<U64Cost>> = LazyLock::new(|| {
[
CONFIG.base_cost.rqr
+ CONFIG.offset_costs.evaluate(&-6)
+ CONFIG.rq_qr_offset_costs.evaluate(&-6)
+ CONFIG.reverse_anti_primary_gap_costs.evaluate(&2)
+ CONFIG.length_costs.evaluate(&2)
+ CONFIG.length_difference_costs.evaluate(&0),
CONFIG.base_cost.rqr
+ CONFIG.offset_costs.evaluate(&-4)
+ CONFIG.rq_qr_offset_costs.evaluate(&-4)
+ CONFIG.reverse_anti_primary_gap_costs.evaluate(&3)
+ CONFIG.length_costs.evaluate(&3)
+ CONFIG.length_difference_costs.evaluate(&0),
CONFIG.base_cost.rqr
+ CONFIG.offset_costs.evaluate(&-2)
+ CONFIG.rq_qr_offset_costs.evaluate(&-2)
+ CONFIG.reverse_anti_primary_gap_costs.evaluate(&4)
+ CONFIG.length_costs.evaluate(&4)
+ CONFIG.length_difference_costs.evaluate(&0),
CONFIG.base_cost.rqr
+ CONFIG.offset_costs.evaluate(&0)
+ CONFIG.rq_qr_offset_costs.evaluate(&0)
+ CONFIG.secondary_reverse_edit_costs.substitution_cost(
DnaAlphabet::ascii_to_character(b'G').unwrap(),
DnaAlphabet::ascii_to_character(b'T').unwrap(),
Expand All @@ -887,7 +889,7 @@ mod tests {
+ CONFIG.length_costs.evaluate(&5)
+ CONFIG.length_difference_costs.evaluate(&0),
CONFIG.base_cost.rqr
+ CONFIG.offset_costs.evaluate(&2)
+ CONFIG.rq_qr_offset_costs.evaluate(&2)
+ CONFIG.secondary_reverse_edit_costs.substitution_cost(
DnaAlphabet::ascii_to_character(b'G').unwrap(),
DnaAlphabet::ascii_to_character(b'T').unwrap(),
Expand Down Expand Up @@ -1017,22 +1019,22 @@ mod tests {
static END_COSTS: LazyLock<Vec<U64Cost>> = LazyLock::new(|| {
[
CONFIG.base_cost.rqr
+ CONFIG.offset_costs.evaluate(&10)
+ CONFIG.rq_qr_offset_costs.evaluate(&10)
+ CONFIG.reverse_anti_primary_gap_costs.evaluate(&2)
+ CONFIG.length_costs.evaluate(&2)
+ CONFIG.length_difference_costs.evaluate(&0),
CONFIG.base_cost.rqr
+ CONFIG.offset_costs.evaluate(&10)
+ CONFIG.rq_qr_offset_costs.evaluate(&10)
+ CONFIG.reverse_anti_primary_gap_costs.evaluate(&3)
+ CONFIG.length_costs.evaluate(&3)
+ CONFIG.length_difference_costs.evaluate(&0),
CONFIG.base_cost.rqr
+ CONFIG.offset_costs.evaluate(&10)
+ CONFIG.rq_qr_offset_costs.evaluate(&10)
+ CONFIG.reverse_anti_primary_gap_costs.evaluate(&4)
+ CONFIG.length_costs.evaluate(&4)
+ CONFIG.length_difference_costs.evaluate(&0),
CONFIG.base_cost.rqr
+ CONFIG.offset_costs.evaluate(&10)
+ CONFIG.rq_qr_offset_costs.evaluate(&10)
+ CONFIG.secondary_reverse_edit_costs.substitution_cost(
DnaAlphabet::ascii_to_character(b'A').unwrap(),
DnaAlphabet::ascii_to_character(b'C').unwrap(),
Expand All @@ -1041,7 +1043,7 @@ mod tests {
+ CONFIG.length_costs.evaluate(&5)
+ CONFIG.length_difference_costs.evaluate(&0),
CONFIG.base_cost.rqr
+ CONFIG.offset_costs.evaluate(&10)
+ CONFIG.rq_qr_offset_costs.evaluate(&10)
+ CONFIG.secondary_reverse_edit_costs.substitution_cost(
DnaAlphabet::ascii_to_character(b'A').unwrap(),
DnaAlphabet::ascii_to_character(b'C').unwrap(),
Expand Down Expand Up @@ -1207,7 +1209,13 @@ mod tests {
),
left_flank_edit_costs: GapAffineAlignmentCostTable::new_zero(),
right_flank_edit_costs: GapAffineAlignmentCostTable::new_zero(),
offset_costs: CostFunction::try_from(
rq_qr_offset_costs: CostFunction::try_from(
(-20..=20)
.map(|i| (i, U64Cost::from(17 * u64::try_from(i + 21).unwrap())))
.collect::<Vec<_>>(),
)
.unwrap(),
rr_qq_offset_costs: CostFunction::try_from(
(-20..=20)
.map(|i| (i, U64Cost::from(17 * u64::try_from(i + 21).unwrap())))
.collect::<Vec<_>>(),
Expand Down
22 changes: 19 additions & 3 deletions lib_tsalign/src/a_star_aligner/template_switch_distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ impl<Strategies: AlignmentStrategySelector> Node<Strategies> {
SubsequenceType: GenomeSequence<Strategies::Alphabet, SubsequenceType> + ?Sized,
>(
&'result self,
cost_increment: Strategies::Cost,
rq_qr_cost_increment: Strategies::Cost,
rr_qq_cost_increment: Strategies::Cost,
base_cost: &'result BaseCost<Strategies::Cost>,
context: &'result Context<SubsequenceType, Strategies>,
) -> impl 'result + Iterator<Item = Self> {
Expand All @@ -230,7 +231,10 @@ impl<Strategies: AlignmentStrategySelector> Node<Strategies> {
) {
unreachable!("This method is only called on primary nodes.")
}
debug_assert!(cost_increment < Strategies::Cost::max_value());
debug_assert!(
rq_qr_cost_increment < Strategies::Cost::max_value()
|| rr_qq_cost_increment < Strategies::Cost::max_value(),
);

self.node_data
.identifier
Expand All @@ -252,8 +256,20 @@ impl<Strategies: AlignmentStrategySelector> Node<Strategies> {
*template_switch_secondary,
*template_switch_direction,
);
let cost_increment = match (*template_switch_primary, *template_switch_secondary) {
(TemplateSwitchPrimary::Reference, TemplateSwitchSecondary::Query)
| (TemplateSwitchPrimary::Query, TemplateSwitchSecondary::Reference) => {
rq_qr_cost_increment
}
(TemplateSwitchPrimary::Reference, TemplateSwitchSecondary::Reference)
| (TemplateSwitchPrimary::Query, TemplateSwitchSecondary::Query) => {
rr_qq_cost_increment
}
};

(base_cost != Strategies::Cost::max_value()).then(|| {
(base_cost != Strategies::Cost::max_value()
&& cost_increment != Strategies::Cost::max_value())
.then(|| {
self.generate_successor(
identifier,
cost_increment + base_cost,
Expand Down
Loading
Loading