Skip to content
Merged

Debug #149

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
41 changes: 32 additions & 9 deletions lib_ts_chainalign/src/alignment/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,47 @@ pub struct AlignmentSequences {
seq2: Vec<u8>,
seq1_name: String,
seq2_name: String,
start: AlignmentCoordinates,
end: AlignmentCoordinates,
}

impl AlignmentSequences {
pub fn new(seq1: Vec<u8>, seq2: Vec<u8>) -> Self {
Self {
pub fn new(
seq1: Vec<u8>,
seq2: Vec<u8>,
start: AlignmentCoordinates,
end: AlignmentCoordinates,
) -> Self {
Self::new_named(
seq1,
seq2,
seq1_name: "seq1".to_string(),
seq2_name: "seq2".to_string(),
}
"seq1".to_string(),
"seq2".to_string(),
start,
end,
)
}

pub fn new_complete(seq1: Vec<u8>, seq2: Vec<u8>) -> Self {
let end = AlignmentCoordinates::new_primary(seq1.len(), seq2.len());
Self::new(seq1, seq2, AlignmentCoordinates::new_primary(0, 0), end)
}

pub fn new_named(seq1: Vec<u8>, seq2: Vec<u8>, seq1_name: String, seq2_name: String) -> Self {
pub fn new_named(
seq1: Vec<u8>,
seq2: Vec<u8>,
seq1_name: String,
seq2_name: String,
start: AlignmentCoordinates,
end: AlignmentCoordinates,
) -> Self {
Self {
seq1,
seq2,
seq1_name,
seq2_name,
start,
end,
}
}

Expand Down Expand Up @@ -54,11 +77,11 @@ impl AlignmentSequences {
}

pub fn primary_start(&self) -> AlignmentCoordinates {
AlignmentCoordinates::new_primary(0, 0)
self.start
}

pub fn primary_end(&self) -> AlignmentCoordinates {
self.end(None)
self.end
}

pub fn secondary_end(&self, ts_kind: TsKind) -> AlignmentCoordinates {
Expand All @@ -67,7 +90,7 @@ impl AlignmentSequences {

pub fn end(&self, ts_kind: Option<TsKind>) -> AlignmentCoordinates {
match ts_kind {
None => AlignmentCoordinates::new_primary(self.seq1.len(), self.seq2.len()),
None => self.primary_end(),
Some(ts_kind @ (TsKind::TS11 | TsKind::TS21)) => {
AlignmentCoordinates::new_secondary(0, self.seq1.len(), ts_kind)
}
Expand Down
4 changes: 2 additions & 2 deletions lib_ts_chainalign/src/anchors/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn rc_fn(c: u8) -> u8 {

#[test]
fn test_coordinates() {
let sequences = AlignmentSequences::new(b"ACAC".to_vec(), b"ACGT".to_vec());
let sequences = AlignmentSequences::new_complete(b"ACAC".to_vec(), b"ACGT".to_vec());
let range = AlignmentRange::new_complete(sequences.seq1().len(), sequences.seq2().len());
let k = 2;

Expand All @@ -40,7 +40,7 @@ fn test_coordinates() {

#[test]
fn test_coordinates_rev() {
let sequences = AlignmentSequences::new(b"ACGT".to_vec(), b"ACAC".to_vec());
let sequences = AlignmentSequences::new_complete(b"ACGT".to_vec(), b"ACAC".to_vec());
let range = AlignmentRange::new_complete(sequences.seq1().len(), sequences.seq2().len());
let k = 2;

Expand Down
30 changes: 5 additions & 25 deletions lib_ts_chainalign/src/chain_align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use lib_tsalign::a_star_aligner::{
alignment_result::AlignmentResult,
template_switch_distance::{EqualCostRange, TemplateSwitchDirection},
};
use log::{debug, trace};
use log::{debug, info, trace};
use rustc_hash::FxHashMapSeed;
use std::{
fmt::Write,
Expand All @@ -28,7 +28,7 @@ use std::{
};

use crate::{
alignment::{AlignmentType, coordinates::AlignmentCoordinates, sequences::AlignmentSequences},
alignment::{AlignmentType, sequences::AlignmentSequences},
anchors::Anchors,
chain_align::{
chainer::{Context, Identifier, Node, closed_list::ChainerClosedList},
Expand All @@ -45,11 +45,8 @@ mod chainer;
mod evaluation;
pub mod performance_parameters;

#[expect(clippy::too_many_arguments)]
pub fn align<AlphabetType: Alphabet, Cost: AStarCost>(
sequences: &AlignmentSequences,
start: AlignmentCoordinates,
end: AlignmentCoordinates,
performance_parameters: &AlignmentPerformanceParameters<Cost>,
alignment_costs: &AlignmentCosts<Cost>,
rc_fn: &dyn Fn(u8) -> u8,
Expand All @@ -61,8 +58,6 @@ pub fn align<AlphabetType: Alphabet, Cost: AStarCost>(
ChainingOpenList::StdHeap => {
choose_closed_list::<AlphabetType, _, BinaryHeap<Node<Cost>, AStarNodeComparator>>(
sequences,
start,
end,
performance_parameters,
alignment_costs,
rc_fn,
Expand All @@ -73,8 +68,6 @@ pub fn align<AlphabetType: Alphabet, Cost: AStarCost>(
}
ChainingOpenList::LinearHeap => choose_closed_list::<AlphabetType, _, LinearHeap<_>>(
sequences,
start,
end,
performance_parameters,
alignment_costs,
rc_fn,
Expand All @@ -85,15 +78,12 @@ pub fn align<AlphabetType: Alphabet, Cost: AStarCost>(
}
}

#[expect(clippy::too_many_arguments)]
pub fn choose_closed_list<
AlphabetType: Alphabet,
Cost: AStarCost,
OpenList: AStarOpenList<Node<Cost>>,
>(
sequences: &AlignmentSequences,
start: AlignmentCoordinates,
end: AlignmentCoordinates,
performance_parameters: &AlignmentPerformanceParameters<Cost>,
alignment_costs: &AlignmentCosts<Cost>,
rc_fn: &dyn Fn(u8) -> u8,
Expand All @@ -105,8 +95,6 @@ pub fn choose_closed_list<
ChainingClosedList::FxHashMap => {
actually_align::<AlphabetType, _, FxHashMapSeed<_, _>, OpenList>(
sequences,
start,
end,
performance_parameters,
alignment_costs,
rc_fn,
Expand All @@ -118,8 +106,6 @@ pub fn choose_closed_list<
ChainingClosedList::Special => {
actually_align::<AlphabetType, _, ChainerClosedList<_>, OpenList>(
sequences,
start,
end,
performance_parameters,
alignment_costs,
rc_fn,
Expand All @@ -131,23 +117,21 @@ pub fn choose_closed_list<
}
}

#[expect(clippy::too_many_arguments)]
fn actually_align<
AlphabetType: Alphabet,
Cost: AStarCost,
ClosedList: AStarClosedList<Node<Cost>>,
OpenList: AStarOpenList<Node<Cost>>,
>(
sequences: &AlignmentSequences,
start: AlignmentCoordinates,
end: AlignmentCoordinates,
performance_parameters: &AlignmentPerformanceParameters<Cost>,
alignment_costs: &AlignmentCosts<Cost>,
rc_fn: &dyn Fn(u8) -> u8,
max_match_run: u32,
anchors: &Anchors,
chaining_cost_function: &mut ChainingCostFunction<Cost>,
) -> AlignmentResult<lib_tsalign::a_star_aligner::template_switch_distance::AlignmentType, Cost> {
info!("Aligning...");
let progress_bar = ProgressBar::new_spinner();
progress_bar.enable_steady_tick(Duration::from_millis(200));

Expand Down Expand Up @@ -280,8 +264,6 @@ fn actually_align<
let (evaluated_cost, _) = chain_evaluator.evaluate_chain(
anchors,
&chain,
start,
end,
max_match_run,
astar.context_mut().chaining_cost_function,
false,
Expand Down Expand Up @@ -370,8 +352,6 @@ fn actually_align<
let (evaluated_cost, alignments) = chain_evaluator.evaluate_chain(
anchors,
&chain,
start,
end,
max_match_run,
astar.context_mut().chaining_cost_function,
true,
Expand Down Expand Up @@ -464,8 +444,8 @@ fn actually_align<
.as_genome_subsequence(),
sequences.seq1_name(),
sequences.seq2_name(),
start.primary_ordinate_a().unwrap(),
start.primary_ordinate_b().unwrap(),
sequences.primary_start().primary_ordinate_a().unwrap(),
sequences.primary_start().primary_ordinate_b().unwrap(),
result.without_node_identifier(),
duration_seconds,
0,
Expand Down
21 changes: 9 additions & 12 deletions lib_ts_chainalign/src/chain_align/evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use log::trace;
use num_traits::Zero;

use crate::{
alignment::{
Alignment, AlignmentType, coordinates::AlignmentCoordinates, sequences::AlignmentSequences,
},
alignment::{Alignment, AlignmentType, sequences::AlignmentSequences},
anchors::{Anchors, primary::PrimaryAnchor, secondary::SecondaryAnchor},
chain_align::chainer::Identifier,
chaining_cost_function::ChainingCostFunction,
Expand All @@ -19,6 +17,7 @@ use crate::{
};

pub struct ChainEvaluator<'sequences, 'alignment_costs, 'rc_fn, Cost: AStarCost> {
sequences: &'sequences AlignmentSequences,
primary_aligner: GapAffineAligner<'sequences, 'alignment_costs, 'rc_fn, Cost>,
secondary_aligner: GapAffineAligner<'sequences, 'alignment_costs, 'rc_fn, Cost>,
ts_12_jump_aligner: Ts12JumpAligner<'sequences, 'alignment_costs, 'rc_fn, Cost>,
Expand All @@ -43,6 +42,7 @@ impl<'sequences, 'alignment_costs, 'rc_fn, Cost: AStarCost>
max_match_run: u32,
) -> Self {
Self {
sequences,
primary_aligner: GapAffineAligner::new(
sequences,
&alignment_costs.primary_costs,
Expand Down Expand Up @@ -78,13 +78,10 @@ impl<'sequences, 'alignment_costs, 'rc_fn, Cost: AStarCost>
}
}

#[expect(clippy::too_many_arguments)]
pub fn evaluate_chain(
&mut self,
anchors: &Anchors,
chain: &[Identifier],
start: AlignmentCoordinates,
end: AlignmentCoordinates,
max_match_run: u32,
chaining_cost_function: &mut ChainingCostFunction<Cost>,
final_evaluation: bool,
Expand Down Expand Up @@ -124,8 +121,8 @@ impl<'sequences, 'alignment_costs, 'rc_fn, Cost: AStarCost>
if final_evaluation || !chaining_cost_function.is_start_to_end_exact() {
self.additional_primary_targets_buffer.clear();
let (cost, alignment) = self.primary_aligner.align(
start,
end,
self.sequences.primary_start(),
self.sequences.primary_end(),
&mut self.additional_primary_targets_buffer,
&mut PanicOnExtend,
);
Expand Down Expand Up @@ -158,7 +155,7 @@ impl<'sequences, 'alignment_costs, 'rc_fn, Cost: AStarCost>
{
self.additional_primary_targets_buffer.clear();
let (cost, alignment) = self.primary_aligner.align(
start,
self.sequences.primary_start(),
end,
&mut self.additional_primary_targets_buffer,
&mut PanicOnExtend,
Expand Down Expand Up @@ -196,7 +193,7 @@ impl<'sequences, 'alignment_costs, 'rc_fn, Cost: AStarCost>
{
self.additional_secondary_targets_buffer.clear();
let (cost, alignment) = self.ts_12_jump_aligner.align(
start,
self.sequences.primary_start(),
end,
&mut self.additional_secondary_targets_buffer,
);
Expand Down Expand Up @@ -231,7 +228,7 @@ impl<'sequences, 'alignment_costs, 'rc_fn, Cost: AStarCost>
self.additional_primary_targets_buffer.clear();
let (cost, alignment) = self.primary_aligner.align(
start,
end,
self.sequences.primary_end(),
&mut self.additional_primary_targets_buffer,
&mut PanicOnExtend,
);
Expand Down Expand Up @@ -267,7 +264,7 @@ impl<'sequences, 'alignment_costs, 'rc_fn, Cost: AStarCost>
self.additional_primary_targets_buffer.clear();
let (cost, alignment) = self.ts_34_jump_aligner.align(
start,
end,
self.sequences.primary_end(),
&mut self.additional_primary_targets_buffer,
);
self.total_gap_fillings +=
Expand Down
18 changes: 12 additions & 6 deletions lib_ts_chainalign/src/chaining_cost_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ impl<Cost: AStarCost> ChainingCostFunction<Cost> {
chaining_lower_bounds: &ChainingLowerBounds<Cost>,
anchors: &Anchors,
sequences: &AlignmentSequences,
start: AlignmentCoordinates,
end: AlignmentCoordinates,
max_exact_cost_function_cost: Cost,
rc_fn: &dyn Fn(u8) -> u8,
) -> Self {
info!("Initialising chaining cost function...");
let start_time = Instant::now();

let start = sequences.primary_start();
let end = sequences.primary_end();
let k = usize::try_from(chaining_lower_bounds.max_match_run() + 1).unwrap();
let primary_anchor_amount = anchors.primary_len() + 2;
let primary_start_anchor_index = AnchorIndex::zero();
Expand Down Expand Up @@ -84,7 +84,7 @@ impl<Cost: AStarCost> ChainingCostFunction<Cost> {
trace!("Fill primary");
additional_primary_targets_output.clear();
primary_aligner.align_until_cost_limit(
sequences.primary_start(),
start,
max_exact_cost_function_cost,
&mut additional_primary_targets_output,
&mut PanicOnExtend,
Expand Down Expand Up @@ -663,7 +663,7 @@ impl<Cost: AStarCost> ChainingCostFunction<Cost> {
let target = &mut self.primary[[from_primary_index + 1, to_primary_index + 1]];
assert!(
*target <= cost,
"Target is larger than cost.\ntarget: {target}; cost: {cost}; from_primary_index: {from_primary_index}; to_primary_index: {to_primary_index}; is_exact: {is_exact}"
"Target is larger than cost.\ntarget: {target}; cost: {cost}; from_primary_index: {from_primary_index}; to_primary_index: {to_primary_index}; is_exact: {is_exact}",
);
let result = *target < cost;
*target = cost;
Expand Down Expand Up @@ -698,7 +698,10 @@ impl<Cost: AStarCost> ChainingCostFunction<Cost> {
self.primary.set_exact(primary_index + 1, end_index);
}
let target = &mut self.primary[[primary_index + 1, end_index]];
assert!(*target <= cost);
assert!(
*target <= cost,
"Target is larger than cost.\ntarget: {target}; cost: {cost}; from_primary_index: {primary_index}; is_exact: {is_exact}",
);
let result = *target < cost;
*target = cost;
result
Expand All @@ -715,7 +718,10 @@ impl<Cost: AStarCost> ChainingCostFunction<Cost> {
.set_exact(Self::primary_start_anchor_index(), end_index);
}
let target = &mut self.primary[[Self::primary_start_anchor_index(), end_index]];
assert!(*target <= cost);
assert!(
*target <= cost,
"Target is larger than cost.\ntarget: {target}; cost: {cost}; is_exact: {is_exact}",
);
let result = *target < cost;
*target = cost;
result
Expand Down
Loading