From 298671382ffeab8f1f057cde83e8474963fbfb9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20K=C3=B6ster?= Date: Tue, 12 Nov 2024 15:22:36 +0100 Subject: [PATCH] fix: allow leading deletions in read_pos method of CigarStringView. (#447) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: allow leading deletions in read_pos method of CigarStringView. * fix testcase --------- Co-authored-by: Felix Mölder --- src/bam/mod.rs | 2 +- src/bam/record.rs | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/bam/mod.rs b/src/bam/mod.rs index ddf1d3221..b79bd89a2 100644 --- a/src/bam/mod.rs +++ b/src/bam/mod.rs @@ -2997,7 +2997,7 @@ CCCCCCCCCCCCCCCCCCC"[..], fn test_bam_header_sync() { let reader = Reader::from_path("test/test_issue_156_no_text.bam").unwrap(); let header_hashmap = Header::from_template(reader.header()).to_hashmap(); - let header_refseqs = header_hashmap.get("SQ".into()).unwrap(); + let header_refseqs = header_hashmap.get("SQ").unwrap(); assert_eq!(header_refseqs[0].get("SN").unwrap(), "ref_1",); assert_eq!(header_refseqs[0].get("LN").unwrap(), "10000000",); } diff --git a/src/bam/record.rs b/src/bam/record.rs index ced3cc8aa..bae3742b6 100644 --- a/src/bam/record.rs +++ b/src/bam/record.rs @@ -2097,10 +2097,15 @@ impl CigarStringView { } break; }, - Cigar::Del(_) => { - return Err(Error::BamUnexpectedCigarOperation { - msg: "'deletion' (D) found before any operation describing read sequence".to_owned() - }); + Cigar::Del(l) => { + // METHOD: leading deletions can happen in case of trimmed reads where + // a primer has been removed AFTER read mapping. + // Example: 24M8I8D18M9S before trimming, 32H8D18M9S after trimming + // with fgbio. While leading deletions should be impossible with + // normal read mapping, they make perfect sense with primer trimming + // because the mapper still had the evidence to decide in favor of + // the deletion via the primer sequence. + rpos += l; }, Cigar::RefSkip(_) => { return Err(Error::BamUnexpectedCigarOperation {