Search in sources :

Example 1 with ArtificialBAMBuilder

use of org.broadinstitute.hellbender.utils.read.ArtificialBAMBuilder in project gatk by broadinstitute.

the class LocusIteratorByStateUnitTest method testLIBS_ComplexPileupTests.

@Test(enabled = true, dataProvider = "LIBS_ComplexPileupTests")
public void testLIBS_ComplexPileupTests(final int nReadsPerLocus, final int nLoci, final int nSamples, final boolean keepReads, final boolean grabReadsAfterEachCycle, final int downsampleTo) {
    final int readLength = 10;
    final boolean downsample = downsampleTo != -1;
    final DownsamplingMethod downsampler = downsample ? new DownsamplingMethod(DownsampleType.BY_SAMPLE, downsampleTo, null) : new DownsamplingMethod(DownsampleType.NONE, null, null);
    final ArtificialBAMBuilder bamBuilder = new ArtificialBAMBuilder(header.getSequenceDictionary(), nReadsPerLocus, nLoci);
    bamBuilder.createAndSetHeader(nSamples).setReadLength(readLength).setAlignmentStart(1);
    final List<GATKRead> reads = bamBuilder.makeReads();
    final LocusIteratorByState li;
    li = new LocusIteratorByState(new FakeCloseableIterator<>(reads.iterator()), downsampler, keepReads, bamBuilder.getSamples(), bamBuilder.getHeader(), true);
    final Set<GATKRead> seenSoFar = new LinkedHashSet<>();
    final Set<GATKRead> keptReads = new LinkedHashSet<>();
    int bpVisited = 0;
    while (li.hasNext()) {
        bpVisited++;
        final AlignmentContext alignmentContext = li.next();
        final ReadPileup p = alignmentContext.getBasePileup();
        AssertWellOrderedPileup(p);
        if (downsample) {
        // just not a safe test
        //Assert.assertTrue(p.getNumberOfElements() <= maxDownsampledCoverage * nSamples, "Too many reads at locus after downsampling");
        } else {
            final int minPileupSize = nReadsPerLocus * nSamples;
            Assert.assertTrue(p.size() >= minPileupSize);
        }
        // the number of reads starting here
        int nReadsStartingHere = 0;
        for (final GATKRead read : p.getReads()) if (read.getStart() == alignmentContext.getPosition())
            nReadsStartingHere++;
        // we can have no more than maxDownsampledCoverage per sample
        final int maxCoveragePerLocus = downsample ? downsampleTo : nReadsPerLocus;
        Assert.assertTrue(nReadsStartingHere <= maxCoveragePerLocus * nSamples);
        seenSoFar.addAll(p.getReads());
        if (keepReads && grabReadsAfterEachCycle) {
            final List<GATKRead> locusReads = li.transferReadsFromAllPreviousPileups();
            if (downsample) {
                // with downsampling we might have some reads here that were downsampled away
                // in the pileup.  We want to ensure that no more than the max coverage per sample is added
                Assert.assertTrue(locusReads.size() >= nReadsStartingHere);
                Assert.assertTrue(locusReads.size() <= maxCoveragePerLocus * nSamples);
            } else {
                Assert.assertEquals(locusReads.size(), nReadsStartingHere);
            }
            keptReads.addAll(locusReads);
            // check that all reads we've seen so far are in our keptReads
            for (final GATKRead read : seenSoFar) {
                Assert.assertTrue(keptReads.contains(read), "A read that appeared in a pileup wasn't found in the kept reads: " + read);
            }
        }
        if (!keepReads)
            Assert.assertTrue(li.getReadsFromAllPreviousPileups().isEmpty(), "Not keeping reads but the underlying list of reads isn't empty");
    }
    if (keepReads && !grabReadsAfterEachCycle)
        keptReads.addAll(li.transferReadsFromAllPreviousPileups());
    if (!downsample) {
        // downsampling may drop loci
        final int expectedBpToVisit = nLoci + readLength - 1;
        Assert.assertEquals(bpVisited, expectedBpToVisit, "Didn't visit the expected number of bp");
    }
    if (keepReads) {
        // check we have the right number of reads
        final int totalReads = nLoci * nReadsPerLocus * nSamples;
        if (!downsample) {
            // downsampling may drop reads
            Assert.assertEquals(keptReads.size(), totalReads, "LIBS didn't keep the right number of reads during the traversal");
            // check that the order of reads is the same as in our read list
            for (int i = 0; i < reads.size(); i++) {
                final GATKRead inputRead = reads.get(i);
                final GATKRead keptRead = reads.get(i);
                Assert.assertSame(keptRead, inputRead, "Input reads and kept reads differ at position " + i);
            }
        } else {
            Assert.assertTrue(keptReads.size() <= totalReads, "LIBS didn't keep the right number of reads during the traversal");
        }
        // check uniqueness
        final Set<String> readNames = new LinkedHashSet<>();
        for (final GATKRead read : keptReads) {
            Assert.assertFalse(readNames.contains(read.getName()), "Found duplicate reads in the kept reads");
            readNames.add(read.getName());
        }
        // check that all reads we've seen are in our keptReads
        for (final GATKRead read : seenSoFar) {
            Assert.assertTrue(keptReads.contains(read), "A read that appeared in a pileup wasn't found in the kept reads: " + read);
        }
        if (!downsample) {
            // check that every read in the list of keep reads occurred at least once in one of the pileups
            for (final GATKRead keptRead : keptReads) {
                Assert.assertTrue(seenSoFar.contains(keptRead), "There's a read " + keptRead + " in our keptReads list that never appeared in any pileup");
            }
        }
    }
}
Also used : GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) ReadPileup(org.broadinstitute.hellbender.utils.pileup.ReadPileup) ArtificialBAMBuilder(org.broadinstitute.hellbender.utils.read.ArtificialBAMBuilder) AlignmentContext(org.broadinstitute.hellbender.engine.AlignmentContext) DownsamplingMethod(org.broadinstitute.hellbender.utils.downsampling.DownsamplingMethod) Test(org.testng.annotations.Test)

Aggregations

AlignmentContext (org.broadinstitute.hellbender.engine.AlignmentContext)1 DownsamplingMethod (org.broadinstitute.hellbender.utils.downsampling.DownsamplingMethod)1 ReadPileup (org.broadinstitute.hellbender.utils.pileup.ReadPileup)1 ArtificialBAMBuilder (org.broadinstitute.hellbender.utils.read.ArtificialBAMBuilder)1 GATKRead (org.broadinstitute.hellbender.utils.read.GATKRead)1 Test (org.testng.annotations.Test)1