diff --git a/src/common/wflign/src/wflign_patch.cpp b/src/common/wflign/src/wflign_patch.cpp index f0a78792..7a9e7d37 100644 --- a/src/common/wflign/src/wflign_patch.cpp +++ b/src/common/wflign/src/wflign_patch.cpp @@ -378,6 +378,38 @@ AlignmentBounds find_alignment_bounds(const alignment_t& aln) { return bounds; } +void trim_alignment(alignment_t& aln) { + // Trim head + int head_trim_q = 0, head_trim_t = 0; + while (aln.edit_cigar.begin_offset < aln.edit_cigar.end_offset) { + char op = aln.edit_cigar.cigar_ops[aln.edit_cigar.begin_offset]; + if (op != 'I' && op != 'D') break; + if (op == 'I') head_trim_q++; + if (op == 'D') head_trim_t++; + aln.edit_cigar.begin_offset++; + } + + // Trim tail + int tail_trim_q = 0, tail_trim_t = 0; + while (aln.edit_cigar.end_offset > aln.edit_cigar.begin_offset) { + char op = aln.edit_cigar.cigar_ops[aln.edit_cigar.end_offset - 1]; + if (op != 'I' && op != 'D') break; + if (op == 'I') tail_trim_q++; + if (op == 'D') tail_trim_t++; + aln.edit_cigar.end_offset--; + } + + // Adjust coordinates + if (aln.is_rev) { + aln.j += tail_trim_q; // For reverse alignments, tail trim affects the start + } else { + aln.j += head_trim_q; + } + aln.i += head_trim_t; + aln.query_length -= (head_trim_q + tail_trim_q); + aln.target_length -= (head_trim_t + tail_trim_t); +} + std::vector do_progressive_wfa_patch_alignment( const char* query, const uint64_t& query_start, @@ -1184,6 +1216,7 @@ void write_merged_alignment( } } else if (save_multi_patch_alns) { for (auto& aln : patch_alignments) { + trim_alignment(aln); multi_patch_alns.push_back(aln); } } diff --git a/src/common/wflign/src/wflign_patch.hpp b/src/common/wflign/src/wflign_patch.hpp index 26009a9d..f5dc4c5a 100644 --- a/src/common/wflign/src/wflign_patch.hpp +++ b/src/common/wflign/src/wflign_patch.hpp @@ -59,6 +59,7 @@ namespace wflign { const int64_t& chain_gap, const int& max_patching_score, const uint64_t& min_inversion_length); + void trim_alignment(alignment_t& aln); std::vector do_progressive_wfa_patch_alignment( const char* query, const uint64_t& query_start,