Search in sources :

Example 1 with AlignmentStateMachine

use of org.broadinstitute.hellbender.utils.locusiterator.AlignmentStateMachine in project gatk by broadinstitute.

the class CalculateMixingFractions method apply.

@Override
public void apply(final VariantContext vc, final ReadsContext readsContext, final ReferenceContext refContext, final FeatureContext fc) {
    if (!isBiallelicSingletonHetSnp(vc)) {
        return;
    }
    final Optional<String> variantSample = StreamSupport.stream(vc.getGenotypes().spliterator(), false).filter(genotype -> genotype.isHet()).map(genotype -> genotype.getSampleName()).findFirst();
    if (!variantSample.isPresent()) {
        return;
    }
    final List<GATKRead> reads = new ArrayList<>();
    final List<Integer> offsets = new ArrayList<>();
    for (final GATKRead read : readsContext) {
        if (read.failsVendorQualityCheck()) {
            continue;
        }
        final AlignmentStateMachine asm = new AlignmentStateMachine(read);
        while (asm.stepForwardOnGenome() != null && asm.getGenomePosition() < vc.getStart()) {
        }
        if (asm.getGenomePosition() == vc.getStart()) {
            reads.add(read);
            offsets.add(asm.getReadOffset());
        }
    }
    final ReadPileup pileup = new ReadPileup(vc, reads, offsets);
    final byte altBase = vc.getAlternateAllele(0).getBases()[0];
    final long altCount = StreamSupport.stream(pileup.spliterator(), false).filter(pe -> pe.getBase() == altBase).count();
    final long totalCount = pileup.size();
    sampleCounts.get(variantSample.get()).addCounts(altCount, totalCount);
}
Also used : DocumentedFeature(org.broadinstitute.barclay.help.DocumentedFeature) htsjdk.variant.vcf(htsjdk.variant.vcf) CommandLineProgramProperties(org.broadinstitute.barclay.argparser.CommandLineProgramProperties) java.util(java.util) AlignmentStateMachine(org.broadinstitute.hellbender.utils.locusiterator.AlignmentStateMachine) ReadPileup(org.broadinstitute.hellbender.utils.pileup.ReadPileup) Argument(org.broadinstitute.barclay.argparser.Argument) StandardArgumentDefinitions(org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions) GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) Collectors(java.util.stream.Collectors) VariantProgramGroup(org.broadinstitute.hellbender.cmdline.programgroups.VariantProgramGroup) File(java.io.File) org.broadinstitute.hellbender.engine(org.broadinstitute.hellbender.engine) GATKProtectedVariantContextUtils(org.broadinstitute.hellbender.utils.GATKProtectedVariantContextUtils) VariantContext(htsjdk.variant.variantcontext.VariantContext) StreamSupport(java.util.stream.StreamSupport) MutableLong(org.apache.commons.lang.mutable.MutableLong) GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) ReadPileup(org.broadinstitute.hellbender.utils.pileup.ReadPileup) AlignmentStateMachine(org.broadinstitute.hellbender.utils.locusiterator.AlignmentStateMachine)

Example 2 with AlignmentStateMachine

use of org.broadinstitute.hellbender.utils.locusiterator.AlignmentStateMachine in project gatk-protected by broadinstitute.

the class CalculateMixingFractions method apply.

@Override
public void apply(final VariantContext vc, final ReadsContext readsContext, final ReferenceContext refContext, final FeatureContext fc) {
    if (!isBiallelicSingletonHetSnp(vc)) {
        return;
    }
    final Optional<String> variantSample = StreamSupport.stream(vc.getGenotypes().spliterator(), false).filter(genotype -> genotype.isHet()).map(genotype -> genotype.getSampleName()).findFirst();
    if (!variantSample.isPresent()) {
        return;
    }
    final List<GATKRead> reads = new ArrayList<>();
    final List<Integer> offsets = new ArrayList<>();
    for (final GATKRead read : readsContext) {
        if (read.failsVendorQualityCheck()) {
            continue;
        }
        final AlignmentStateMachine asm = new AlignmentStateMachine(read);
        while (asm.stepForwardOnGenome() != null && asm.getGenomePosition() < vc.getStart()) {
        }
        if (asm.getGenomePosition() == vc.getStart()) {
            reads.add(read);
            offsets.add(asm.getReadOffset());
        }
    }
    final ReadPileup pileup = new ReadPileup(vc, reads, offsets);
    final byte altBase = vc.getAlternateAllele(0).getBases()[0];
    final long altCount = StreamSupport.stream(pileup.spliterator(), false).filter(pe -> pe.getBase() == altBase).count();
    final long totalCount = pileup.size();
    sampleCounts.get(variantSample.get()).addCounts(altCount, totalCount);
}
Also used : DocumentedFeature(org.broadinstitute.barclay.help.DocumentedFeature) htsjdk.variant.vcf(htsjdk.variant.vcf) CommandLineProgramProperties(org.broadinstitute.barclay.argparser.CommandLineProgramProperties) java.util(java.util) AlignmentStateMachine(org.broadinstitute.hellbender.utils.locusiterator.AlignmentStateMachine) ReadPileup(org.broadinstitute.hellbender.utils.pileup.ReadPileup) Argument(org.broadinstitute.barclay.argparser.Argument) StandardArgumentDefinitions(org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions) GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) Collectors(java.util.stream.Collectors) VariantProgramGroup(org.broadinstitute.hellbender.cmdline.programgroups.VariantProgramGroup) File(java.io.File) org.broadinstitute.hellbender.engine(org.broadinstitute.hellbender.engine) GATKProtectedVariantContextUtils(org.broadinstitute.hellbender.utils.GATKProtectedVariantContextUtils) VariantContext(htsjdk.variant.variantcontext.VariantContext) StreamSupport(java.util.stream.StreamSupport) MutableLong(org.apache.commons.lang.mutable.MutableLong) GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) ReadPileup(org.broadinstitute.hellbender.utils.pileup.ReadPileup) AlignmentStateMachine(org.broadinstitute.hellbender.utils.locusiterator.AlignmentStateMachine)

Example 3 with AlignmentStateMachine

use of org.broadinstitute.hellbender.utils.locusiterator.AlignmentStateMachine in project gatk by broadinstitute.

the class PileupElement method createPileupForReadAndOffset.

/**
     * Create a pileup element for read at offset.
     *
     * offset must correspond to a valid read offset given the read's cigar, or an IllegalStateException will be throw
     *
     * @param read a read
     * @param offset the offset into the bases we'd like to use in the pileup
     * @return a valid PileupElement with read and at offset
     */
public static PileupElement createPileupForReadAndOffset(final GATKRead read, final int offset) {
    Utils.nonNull(read, "read is null");
    Utils.validIndex(offset, read.getLength());
    final AlignmentStateMachine stateMachine = new AlignmentStateMachine(read);
    while (stateMachine.stepForwardOnGenome() != null) {
        if (stateMachine.getReadOffset() == offset) {
            return stateMachine.makePileupElement();
        }
    }
    throw new IllegalStateException("Tried to create a pileup for read " + read + " with offset " + offset + " but we never saw such an offset in the alignment state machine");
}
Also used : AlignmentStateMachine(org.broadinstitute.hellbender.utils.locusiterator.AlignmentStateMachine)

Example 4 with AlignmentStateMachine

use of org.broadinstitute.hellbender.utils.locusiterator.AlignmentStateMachine in project gatk by broadinstitute.

the class PileupElementUnitTest method testPrevAndNextTest.

@Test(dataProvider = "PrevAndNextTest")
public void testPrevAndNextTest(final GATKRead read, final CigarOperator firstOp, final CigarOperator lastOp, final List<CigarOperator> ops) {
    final AlignmentStateMachine state = new AlignmentStateMachine(read);
    //before the first 'op' from the list
    state.stepForwardOnGenome();
    final PileupElement pe = state.makePileupElement();
    Assert.assertEquals(pe.getBetweenNextPosition().size(), ops.size());
    Assert.assertEquals(pe.getBetweenPrevPosition().size(), 0);
    assertEqualsOperators(pe.getBetweenNextPosition(), ops);
    Assert.assertEquals(pe.getPreviousOnGenomeCigarElement(), null);
    Assert.assertNotNull(pe.getNextOnGenomeCigarElement());
    Assert.assertEquals(pe.getNextOnGenomeCigarElement().getOperator(), lastOp);
    //after the first 'op' from the list
    state.stepForwardOnGenome();
    final PileupElement pe2 = state.makePileupElement();
    Assert.assertEquals(pe2.getBetweenPrevPosition().size(), ops.size());
    Assert.assertEquals(pe2.getBetweenNextPosition().size(), 0);
    assertEqualsOperators(pe2.getBetweenPrevPosition(), ops);
    Assert.assertNotNull(pe2.getPreviousOnGenomeCigarElement());
    Assert.assertEquals(pe2.getPreviousOnGenomeCigarElement().getOperator(), firstOp);
    Assert.assertEquals(pe2.getNextOnGenomeCigarElement(), null);
}
Also used : AlignmentStateMachine(org.broadinstitute.hellbender.utils.locusiterator.AlignmentStateMachine) LocusIteratorByStateBaseTest(org.broadinstitute.hellbender.utils.locusiterator.LocusIteratorByStateBaseTest) Test(org.testng.annotations.Test) LIBSTest(org.broadinstitute.hellbender.utils.locusiterator.LIBSTest)

Example 5 with AlignmentStateMachine

use of org.broadinstitute.hellbender.utils.locusiterator.AlignmentStateMachine in project gatk by broadinstitute.

the class PileupElementUnitTest method testImmediateBeforeAndAfterTest_simple.

@Test(dataProvider = "PrevAndNextTest_simple")
public void testImmediateBeforeAndAfterTest_simple(final GATKRead read, final CigarOperator middleOp) {
    final AlignmentStateMachine state = new AlignmentStateMachine(read);
    state.stepForwardOnGenome();
    //before the 'middleOp'
    final PileupElement pe1 = state.makePileupElement();
    Assert.assertEquals(pe1.getAdjacentOperator(PileupElement.Direction.PREV), null, "PREV");
    Assert.assertEquals(pe1.getAdjacentOperator(PileupElement.Direction.NEXT), middleOp, "NEXT");
    for (final CigarOperator op : CigarOperator.values()) {
        Assert.assertEquals(pe1.isImmediatelyBefore(op), middleOp == op, op.toString());
        Assert.assertFalse(pe1.isImmediatelyAfter(op), op.toString());
    }
    state.stepForwardOnGenome();
    //after the 'middleOp'
    final PileupElement pe2 = state.makePileupElement();
    Assert.assertEquals(pe2.getAdjacentOperator(PileupElement.Direction.PREV), middleOp, "PREV");
    Assert.assertEquals(pe2.getAdjacentOperator(PileupElement.Direction.NEXT), null, "NEXT");
    for (final CigarOperator op : CigarOperator.values()) {
        Assert.assertFalse(pe2.isImmediatelyBefore(op), op.toString());
        Assert.assertEquals(pe2.isImmediatelyAfter(op), op == middleOp, op.toString());
    }
}
Also used : CigarOperator(htsjdk.samtools.CigarOperator) AlignmentStateMachine(org.broadinstitute.hellbender.utils.locusiterator.AlignmentStateMachine) LocusIteratorByStateBaseTest(org.broadinstitute.hellbender.utils.locusiterator.LocusIteratorByStateBaseTest) Test(org.testng.annotations.Test) LIBSTest(org.broadinstitute.hellbender.utils.locusiterator.LIBSTest)

Aggregations

AlignmentStateMachine (org.broadinstitute.hellbender.utils.locusiterator.AlignmentStateMachine)7 LIBSTest (org.broadinstitute.hellbender.utils.locusiterator.LIBSTest)4 LocusIteratorByStateBaseTest (org.broadinstitute.hellbender.utils.locusiterator.LocusIteratorByStateBaseTest)4 Test (org.testng.annotations.Test)4 GATKRead (org.broadinstitute.hellbender.utils.read.GATKRead)3 CigarOperator (htsjdk.samtools.CigarOperator)2 VariantContext (htsjdk.variant.variantcontext.VariantContext)2 htsjdk.variant.vcf (htsjdk.variant.vcf)2 File (java.io.File)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 StreamSupport (java.util.stream.StreamSupport)2 MutableLong (org.apache.commons.lang.mutable.MutableLong)2 Argument (org.broadinstitute.barclay.argparser.Argument)2 CommandLineProgramProperties (org.broadinstitute.barclay.argparser.CommandLineProgramProperties)2 DocumentedFeature (org.broadinstitute.barclay.help.DocumentedFeature)2 StandardArgumentDefinitions (org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions)2 VariantProgramGroup (org.broadinstitute.hellbender.cmdline.programgroups.VariantProgramGroup)2 org.broadinstitute.hellbender.engine (org.broadinstitute.hellbender.engine)2 GATKProtectedVariantContextUtils (org.broadinstitute.hellbender.utils.GATKProtectedVariantContextUtils)2