From f5a301fb6354f0f3fad6cd01bd1c154ee15a1086 Mon Sep 17 00:00:00 2001 From: Mark DePristo Date: Tue, 23 Apr 2013 18:26:20 -0400 Subject: [PATCH] Bugfix for AlignmentUtils.trimCigarByBases -- Previous version would trim down 2M2D2M into 2M if you asked for the first 2 bases, but this can result in incorrect alignment of the bases to the reference as the bases no longer span the full reference interval expected. Fixed and added unit tests --- .../org/broadinstitute/sting/utils/sam/AlignmentUtils.java | 4 ++-- .../sting/utils/sam/AlignmentUtilsUnitTest.java | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/public/java/src/org/broadinstitute/sting/utils/sam/AlignmentUtils.java b/public/java/src/org/broadinstitute/sting/utils/sam/AlignmentUtils.java index 59a22c550..fa35e3f53 100644 --- a/public/java/src/org/broadinstitute/sting/utils/sam/AlignmentUtils.java +++ b/public/java/src/org/broadinstitute/sting/utils/sam/AlignmentUtils.java @@ -934,7 +934,7 @@ public final class AlignmentUtils { */ public static Cigar trimCigarByBases(final Cigar cigar, final int start, final int end) { if ( start < 0 ) throw new IllegalArgumentException("Start must be >= 0 but got " + start); - if ( end < start ) throw new IllegalArgumentException("End " + end + " is < start start " + start); + if ( end < start ) throw new IllegalArgumentException("End " + end + " is < start = " + start); if ( end > cigar.getReadLength() ) throw new IllegalArgumentException("End is beyond the cigar's read length " + end + " for cigar " + cigar ); final Cigar result = trimCigar(cigar, start, end, false); @@ -962,7 +962,7 @@ public final class AlignmentUtils { int pos = 0; for ( final CigarElement elt : cigar.getCigarElements() ) { - if ( pos > end ) break; + if ( pos > end && (byReference || elt.getOperator() != CigarOperator.D) ) break; switch ( elt.getOperator() ) { case D: diff --git a/public/java/test/org/broadinstitute/sting/utils/sam/AlignmentUtilsUnitTest.java b/public/java/test/org/broadinstitute/sting/utils/sam/AlignmentUtilsUnitTest.java index 2a2d80206..e7d54c460 100644 --- a/public/java/test/org/broadinstitute/sting/utils/sam/AlignmentUtilsUnitTest.java +++ b/public/java/test/org/broadinstitute/sting/utils/sam/AlignmentUtilsUnitTest.java @@ -792,7 +792,8 @@ public class AlignmentUtilsUnitTest { tests.add(new Object[]{"2M2D2I", 3, 3, "1I"}); tests.add(new Object[]{"2M2D2I", 2, 2, "2D1I"}); tests.add(new Object[]{"2M2D2I", 1, 2, "1M2D1I"}); - tests.add(new Object[]{"2M2D2I", 1, 1, "1M"}); + tests.add(new Object[]{"2M2D2I", 0, 1, "2M2D"}); + tests.add(new Object[]{"2M2D2I", 1, 1, "1M2D"}); return tests.toArray(new Object[][]{}); }