From d804de66130cda6d7010acbd1f1fe8495e27c3f4 Mon Sep 17 00:00:00 2001 From: "Dr. K. D. Murray" <1560490+kdm9@users.noreply.github.com> Date: Tue, 24 Sep 2024 11:16:37 +0200 Subject: [PATCH 1/2] fix: invalid paf produced for some patch alignments --- src/common/wflign/src/wflign_patch.cpp | 43 ++++++++++++++------------ 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/common/wflign/src/wflign_patch.cpp b/src/common/wflign/src/wflign_patch.cpp index 6eae79e6..9bfad249 100644 --- a/src/common/wflign/src/wflign_patch.cpp +++ b/src/common/wflign/src/wflign_patch.cpp @@ -2032,26 +2032,29 @@ query_start : query_end) // write how many reverse complement alignments were found //std::cerr << "got " << rev_patch_alns.size() << " rev patch alns" << std::endl; for (auto& patch_aln : multi_patch_alns) { - write_alignment_paf( - out, - patch_aln, - query_name, - query_total_length, - query_offset, - query_length, - query_is_rev, - target_name, - target_total_length, - target_offset, - target_length, - min_identity, - mashmap_estimated_identity, - false, // Don't add an endline after each alignment - true); // This is a reverse complement alignment - // write tag indicating that this is a multipatch alignment - out << "\t" << "pt:Z:true" << "\t" - // and if the patch is inverted as well - << "\t" << "iv:Z:" << (patch_aln.is_rev ? "true" : "false") << "\n"; + if (patch_aln.ok) { + // write_alignment_paf only writes anything if aln.ok. We need to guard the manual tag writing below with the same conditional to avoid writing an invalid PAF. + write_alignment_paf( + out, + patch_aln, + query_name, + query_total_length, + query_offset, + query_length, + query_is_rev, + target_name, + target_total_length, + target_offset, + target_length, + min_identity, + mashmap_estimated_identity, + false, // Don't add an endline after each alignment + true); // This is a reverse complement alignment + // write tag indicating that this is a multipatch alignment + out << "\t" << "pt:Z:true" << "\t" + // and if the patch is inverted as well + << "iv:Z:" << (patch_aln.is_rev ? "true" : "false") << "\n"; + } } } out << std::flush; From dd0bbc3b576c141e9021b9c64cfdc4dd75e51c28 Mon Sep 17 00:00:00 2001 From: "Dr. K.D. Murray" Date: Tue, 24 Sep 2024 16:07:13 +0200 Subject: [PATCH 2/2] properly detect if a paf line was written --- src/common/wflign/src/wflign_patch.cpp | 11 +++++++---- src/common/wflign/src/wflign_patch.hpp | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/common/wflign/src/wflign_patch.cpp b/src/common/wflign/src/wflign_patch.cpp index 9bfad249..817250a8 100644 --- a/src/common/wflign/src/wflign_patch.cpp +++ b/src/common/wflign/src/wflign_patch.cpp @@ -2032,9 +2032,8 @@ query_start : query_end) // write how many reverse complement alignments were found //std::cerr << "got " << rev_patch_alns.size() << " rev patch alns" << std::endl; for (auto& patch_aln : multi_patch_alns) { - if (patch_aln.ok) { - // write_alignment_paf only writes anything if aln.ok. We need to guard the manual tag writing below with the same conditional to avoid writing an invalid PAF. - write_alignment_paf( + // write_alignment_paf only writes anything if aln.ok. We need to guard the manual tag writing below with the same conditional to avoid writing an invalid PAF. + bool wrote = write_alignment_paf( out, patch_aln, query_name, @@ -2050,6 +2049,7 @@ query_start : query_end) mashmap_estimated_identity, false, // Don't add an endline after each alignment true); // This is a reverse complement alignment + if (wrote) { // write tag indicating that this is a multipatch alignment out << "\t" << "pt:Z:true" << "\t" // and if the patch is inverted as well @@ -2224,7 +2224,7 @@ void write_alignment_sam( free(patch_cigar); } -void write_alignment_paf( +bool write_alignment_paf( std::ostream& out, const alignment_t& aln, const std::string& query_name, @@ -2240,6 +2240,7 @@ void write_alignment_paf( const float& mashmap_estimated_identity, const bool& with_endline, const bool& is_rev_patch) { + bool ret = false; // return true if we wrote the alignment if (aln.ok) { uint64_t matches = 0; @@ -2294,9 +2295,11 @@ void write_alignment_paf( if (with_endline) { out << std::endl; } + ret = true; } free(cigar); } + return ret; } double float2phred(const double& prob) { diff --git a/src/common/wflign/src/wflign_patch.hpp b/src/common/wflign/src/wflign_patch.hpp index 80530a54..7e197abb 100644 --- a/src/common/wflign/src/wflign_patch.hpp +++ b/src/common/wflign/src/wflign_patch.hpp @@ -144,7 +144,7 @@ namespace wflign { const char* query, const char* target, const int64_t& target_pointer_shift); - void write_alignment_paf( + bool write_alignment_paf( std::ostream& out, const alignment_t& aln, const std::string& query_name,