diff --git a/src/main/scala/com/fulcrumgenomics/alignment/Alignment.scala b/src/main/scala/com/fulcrumgenomics/alignment/Alignment.scala index cef44a6b9..75326bad1 100644 --- a/src/main/scala/com/fulcrumgenomics/alignment/Alignment.scala +++ b/src/main/scala/com/fulcrumgenomics/alignment/Alignment.scala @@ -333,7 +333,7 @@ case class Alignment(query: Array[Byte], // If the current element consumes the chosen sequence, calculate the end, else set to the same as start val elementConsumes = consumes(elem) - val currEnd = if (elementConsumes) currStart + elem.length - 1 else currStart + val currEnd = if (elementConsumes) currStart + elem.length - 1 else currStart-1 if (currEnd < start) { // Element before the desired window, need to bump start positions diff --git a/src/test/scala/com/fulcrumgenomics/alignment/AlignmentTest.scala b/src/test/scala/com/fulcrumgenomics/alignment/AlignmentTest.scala index 33ca8cda9..3268aa9b2 100644 --- a/src/test/scala/com/fulcrumgenomics/alignment/AlignmentTest.scala +++ b/src/test/scala/com/fulcrumgenomics/alignment/AlignmentTest.scala @@ -212,7 +212,7 @@ class AlignmentTest extends UnitSpec { it should "drop insertions and deletions that are adjacent to the desired region" in { val sub = Alignment("AACCAAAAAAAA", "AAAAAAAATTAA", 1, 1, Cigar("2=2I6=2D2="), 10).subByTarget(3, 8) sub.targetStart shouldBe 3 - sub.queryStart shouldBe 3 + sub.queryStart shouldBe 5 sub.cigar.toString() shouldBe "6=" } @@ -221,6 +221,16 @@ class AlignmentTest extends UnitSpec { an[Exception] shouldBe thrownBy { Alignment("AAAAA", "TAAAT", 2, 2, Cigar("3="), 0).subByTarget(2, 5) } } + it should "handle a real world test case" in { + val query = "GGCCAGAGTCCACAGATTAACCAGGGGATATGCTAGAAA" + val target = "CAGAGGCCACAGATTAACCAGGGGATATGCTAGAAA" + val ali = Alignment(query, target, 1, 1, Cigar("3I5=1X30="), 0) + val sub = ali.subByTarget(1, 20) + sub.queryStart shouldBe 4 + sub.targetStart shouldBe 1 + sub.cigar.toString shouldBe "5=1X14=" + } + "Alignment.subByQuery" should "yield appropriate sub-alignment when all bases are aligned" in { val sub = Alignment("AAAAAAAAAA", "AAAAAAAAAA", 1, 1, Cigar("10M"), 10).subByQuery(5, 6) sub.targetStart shouldBe 5