Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
little technicalities to make the right exceptions occur in unit tests
Browse files Browse the repository at this point in the history
davidbenjamin committed Jun 15, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 6568832 commit f215fa1
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
import java.nio.ByteBuffer;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* Class that manages the complicated steps involved in generating artificial haplotypes for the PDHMM:
@@ -501,15 +502,17 @@ public static Haplotype constructHaplotypeFromEvents(final Haplotype refHap, fin
final int resultHaplotypeLength = refHap.length() + events.stream().mapToInt(e -> e.altAllele().length() - e.refAllele().length()).sum();
final ByteBuffer newHapBases = ByteBuffer.allocate(resultHaplotypeLength);

Utils.validate(IntStream.range(0, events.size()-1).allMatch(n ->
events.get(n).getEnd() < actualStartExcludingInitialIndelBase(events.get(n+1))), () -> "PD event list: " + events + " is out of order.");

//ASSUME sorted for now
for (Event event : events) {
Allele refAllele = event.refAllele();
Allele altAllele = event.altAllele();

final int actualStart = event.getStart() + (event.isIndel() ? 1 : 0); // the +1 for indels accounts for the initial alt=ref dummy base
final int actualStart = actualStartExcludingInitialIndelBase(event); // the +1 for indels accounts for the initial alt=ref dummy base

int basesBeforeNextEvent = actualStart - lastPositionAdded;
Utils.validate(basesBeforeNextEvent >= 0, () -> "PD event list: " + events + " is out of order.");
runningCigar.add(new CigarElement(basesBeforeNextEvent, CigarOperator.M));

final int altRefLengthDiff = altAllele.length() - refAllele.length();
@@ -538,6 +541,10 @@ public static Haplotype constructHaplotypeFromEvents(final Haplotype refHap, fin
return result;
}

private static int actualStartExcludingInitialIndelBase(final Event event) {
return event.getStart() + (event.isIndel() ? 1 : 0);
}

// skip the dummy intial base of an indel
private static byte[] basesAfterFirst(final Allele altAllele) {
return Arrays.copyOfRange(altAllele.getBases(), 1, altAllele.length());
Original file line number Diff line number Diff line change
@@ -94,7 +94,7 @@ public void basicConstructHaplotypeFromVariants(List<Event> events, String expec
Assert.assertEquals(resultEMap.getNumberOfEvents(), events.size() - numberOfCompounds);
}

@Test(expectedExceptions = GATKException.class)
@Test(expectedExceptions = IllegalStateException.class)
public void TestOutOfOrderInputs() {
Haplotype ref = new Haplotype("AAAAAAAAAA".getBytes(), true, 500, TextCigarCodec.decode("10M"));
ref.setGenomeLocation(new SimpleInterval("20", 100, 110));
@@ -103,7 +103,7 @@ public void TestOutOfOrderInputs() {
Haplotype result = PartiallyDeterminedHaplotypeComputationEngine.constructHaplotypeFromEvents(ref, variants, true);
}

@Test(expectedExceptions = GATKException.class)
@Test(expectedExceptions = IllegalStateException.class)
public void TestSNPsOverlapping() {
Haplotype ref = new Haplotype("AAAAAAAAAA".getBytes(), true, 500, TextCigarCodec.decode("10M"));
ref.setGenomeLocation(new SimpleInterval("20", 100, 110));
@@ -112,7 +112,7 @@ public void TestSNPsOverlapping() {
Haplotype result = PartiallyDeterminedHaplotypeComputationEngine.constructHaplotypeFromEvents(ref, events, true);
}

@Test(expectedExceptions = GATKException.class)
@Test(expectedExceptions = IllegalStateException.class)
public void TestVariantNotOverlappingHap() {
Haplotype ref = new Haplotype("AAAAAAAAAA".getBytes(), true, 500, TextCigarCodec.decode("10M"));
ref.setGenomeLocation(new SimpleInterval("20", 100, 110));
@@ -121,7 +121,7 @@ public void TestVariantNotOverlappingHap() {
Haplotype result = PartiallyDeterminedHaplotypeComputationEngine.constructHaplotypeFromEvents(ref, events, true);
}

@Test(expectedExceptions = GATKException.class)
@Test(expectedExceptions = IllegalStateException.class)
public void TestVariantIndelPartiallyOverlapping() {
Haplotype ref = new Haplotype("AAAAAAAAAA".getBytes(), true, 500, TextCigarCodec.decode("10M"));
ref.setGenomeLocation(new SimpleInterval("20", 100, 110));

0 comments on commit f215fa1

Please sign in to comment.