use of org.broadinstitute.hellbender.engine.AlignmentContext in project gatk by broadinstitute.
the class LocusIteratorByState method lazyLoadNextAlignmentContext.
/**
* Creates the next alignment context from the given state. Note that this is implemented as a
* lazy load method. nextAlignmentContext MUST BE null in order for this method to advance to the
* next entry.
*/
private void lazyLoadNextAlignmentContext() {
while (nextAlignmentContext == null && readStates.hasNext()) {
readStates.collectPendingReads();
final Locatable location = getLocation();
final Map<String, ReadPileup> fullPileupPerSample = new LinkedHashMap<>();
for (final Map.Entry<String, PerSampleReadStateManager> sampleStatePair : readStates) {
final String sample = sampleStatePair.getKey();
final PerSampleReadStateManager readState = sampleStatePair.getValue();
final Iterator<AlignmentStateMachine> iterator = readState.iterator();
final List<PileupElement> pile = new ArrayList<>(readState.size());
while (iterator.hasNext()) {
// state object with the read/offset information
final AlignmentStateMachine state = iterator.next();
final GATKRead read = state.getRead();
final CigarOperator op = state.getCigarOperator();
if (!includeReadsWithNsAtLoci && op == CigarOperator.N) {
continue;
}
if (!dontIncludeReadInPileup(read, location.getStart())) {
if (!includeReadsWithDeletionAtLoci && op == CigarOperator.D) {
continue;
}
pile.add(state.makePileupElement());
}
}
if (!pile.isEmpty()) {
// if this pileup added at least one base, add it to the full pileup
fullPileupPerSample.put(sample, new ReadPileup(location, pile));
}
}
// critical - must be called after we get the current state offsets and location
readStates.updateReadStates();
if (!fullPileupPerSample.isEmpty()) {
// if we got reads with non-D/N over the current position, we are done
nextAlignmentContext = new AlignmentContext(location, new ReadPileup(location, fullPileupPerSample));
}
}
}
use of org.broadinstitute.hellbender.engine.AlignmentContext 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");
}
}
}
}
use of org.broadinstitute.hellbender.engine.AlignmentContext in project gatk by broadinstitute.
the class LocusIteratorByStateUnitTest method testUnmappedAndAllIReadsPassThrough.
@Test(enabled = false)
public void testUnmappedAndAllIReadsPassThrough() {
final int readLength = 10;
final GATKRead mapped1 = ArtificialReadUtils.createArtificialRead(header, "mapped1", 0, 1, readLength);
final GATKRead mapped2 = ArtificialReadUtils.createArtificialRead(header, "mapped2", 0, 1, readLength);
final GATKRead unmapped = ArtificialReadUtils.createArtificialRead(header, "unmapped", 0, 1, readLength);
final GATKRead allI = ArtificialReadUtils.createArtificialRead(header, "allI", 0, 1, readLength);
unmapped.setIsUnmapped();
unmapped.setCigar("*");
allI.setCigar(readLength + "I");
final List<GATKRead> reads = Arrays.asList(mapped1, unmapped, allI, mapped2);
// create the iterator by state with the fake reads and fake records
final LocusIteratorByState li;
li = makeLIBS(reads, DownsamplingMethod.NONE, true, header);
Assert.assertTrue(li.hasNext());
AlignmentContext context = li.next();
ReadPileup pileup = context.getBasePileup();
Assert.assertEquals(pileup.size(), 2, "Should see only 2 reads in pileup, even with unmapped and all I reads");
final List<GATKRead> rawReads = li.transferReadsFromAllPreviousPileups();
Assert.assertEquals(rawReads, reads, "Input and transferred read lists should be the same, and include the unmapped and all I reads");
}
use of org.broadinstitute.hellbender.engine.AlignmentContext in project gatk by broadinstitute.
the class LocusIteratorByStateUnitTest method testAllIReadsPassThrough.
@Test
public void testAllIReadsPassThrough() {
final int readLength = 10;
final GATKRead mapped1 = ArtificialReadUtils.createArtificialRead(header, "mapped1", 0, 1, readLength);
final GATKRead mapped2 = ArtificialReadUtils.createArtificialRead(header, "mapped2", 0, 1, readLength);
final GATKRead allI = ArtificialReadUtils.createArtificialRead(header, "allI", 0, 1, readLength);
allI.setCigar(readLength + "I");
final List<GATKRead> reads = Arrays.asList(mapped1, allI, mapped2);
// create the iterator by state with the fake reads and fake records
final LocusIteratorByState li;
li = makeLIBS(reads, DownsamplingMethod.NONE, true, header);
Assert.assertTrue(li.hasNext());
final AlignmentContext context = li.next();
final ReadPileup pileup = context.getBasePileup();
Assert.assertEquals(pileup.size(), 2, "Should see only 2 reads in pileup, even with all I reads");
final List<GATKRead> rawReads = li.transferReadsFromAllPreviousPileups();
Assert.assertEquals(rawReads, reads, "Input and transferred read lists should be the same, and include and all I reads");
}
use of org.broadinstitute.hellbender.engine.AlignmentContext in project gatk by broadinstitute.
the class LocusIteratorByStateUnitTest method testAdapterClipping.
@Test(enabled = true, dataProvider = "AdapterClippingTest")
public void testAdapterClipping(final int nClipsOnLeft, final int nReadContainingPileups, final int nClipsOnRight, final GATKRead read) {
final LocusIteratorByState li;
li = new LocusIteratorByState(new FakeCloseableIterator<>(Collections.singletonList(read).iterator()), DownsamplingMethod.NONE, false, sampleListForSAMWithoutReadGroups(), header, true);
int expectedPos = read.getStart() + nClipsOnLeft;
int nPileups = 0;
while (li.hasNext()) {
final AlignmentContext next = li.next();
Assert.assertEquals(next.getLocation().getStart(), expectedPos);
nPileups++;
expectedPos++;
}
final int nExpectedPileups = nReadContainingPileups;
Assert.assertEquals(nPileups, nExpectedPileups, "\"Wrong number of pileups seen for " + read + " with " + nClipsOnLeft + " clipped bases.");
}
Aggregations