Search in sources :

Example 16 with VariantContextBuilder

use of htsjdk.variant.variantcontext.VariantContextBuilder in project gatk by broadinstitute.

the class GATKToolUnitTest method writeHeaderAndBadVariant.

private void writeHeaderAndBadVariant(final VariantContextWriter writer) {
    final VariantContextBuilder vcBuilder = new VariantContextBuilder("chr1", "1", 1, 1, Arrays.asList(Allele.create("A", true)));
    vcBuilder.attribute("fake", new Object());
    final VariantContext vc = vcBuilder.make();
    final VCFHeader vcfHeader = new VCFHeader();
    writer.writeHeader(vcfHeader);
    writer.add(vc);
}
Also used : VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) VariantContext(htsjdk.variant.variantcontext.VariantContext) VCFHeader(htsjdk.variant.vcf.VCFHeader)

Example 17 with VariantContextBuilder

use of htsjdk.variant.variantcontext.VariantContextBuilder in project gatk by broadinstitute.

the class VariantFilterUnitTest method createArtificialVC.

/**
     * Create an artificial VariantContext
     *
     */
private static VariantContext createArtificialVC(String id, SimpleInterval loc, List<Allele> alleles) {
    VariantContextBuilder vb = new VariantContextBuilder();
    vb.id(id);
    if (alleles != null)
        vb.alleles(alleles);
    if (loc != null)
        vb.loc(loc.getContig(), loc.getStart(), loc.getEnd());
    return vb.make();
}
Also used : VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder)

Example 18 with VariantContextBuilder

use of htsjdk.variant.variantcontext.VariantContextBuilder in project gatk-protected by broadinstitute.

the class GermlineProbabilityCalculatorUnitTest method testGetGermlineAltAlleleFrequencies.

@Test
public void testGetGermlineAltAlleleFrequencies() {
    final double defaultAF = 0.001;
    final double nonDefaultAF1 = 0.1;
    final double nonDefaultAF2 = 0.01;
    final Allele Aref = Allele.create("A", true);
    final Allele C = Allele.create("C");
    final Allele G = Allele.create("G");
    final Allele T = Allele.create("T");
    final String source = "SOURCE";
    final int start = 1;
    final int stop = 1;
    //biallelic, vc has the same alt allele
    final List<Allele> altAlleles1 = Arrays.asList(C);
    final VariantContext vc1 = new VariantContextBuilder(source, "1", start, stop, Arrays.asList(Aref, C)).attribute(VCFConstants.ALLELE_FREQUENCY_KEY, new double[] { nonDefaultAF1 }).make();
    final double[] af1 = GermlineProbabilityCalculator.getGermlineAltAlleleFrequencies(altAlleles1, Optional.of(vc1), defaultAF);
    Assert.assertEquals(af1.length, altAlleles1.size());
    Assert.assertEquals(af1[0], nonDefaultAF1, 0.00001);
    //biallelic, vc has different alt allele
    final List<Allele> altAlleles2 = Arrays.asList(C);
    final VariantContext vc2 = new VariantContextBuilder(source, "1", start, stop, Arrays.asList(Aref, G)).attribute(VCFConstants.ALLELE_FREQUENCY_KEY, new double[] { nonDefaultAF1 }).make();
    final double[] af2 = GermlineProbabilityCalculator.getGermlineAltAlleleFrequencies(altAlleles2, Optional.of(vc2), defaultAF);
    Assert.assertEquals(af2.length, altAlleles2.size());
    Assert.assertEquals(af2[0], defaultAF, 0.00001);
    //triallelic, same alt alleles
    final List<Allele> altAlleles3 = Arrays.asList(C, G);
    final VariantContext vc3 = new VariantContextBuilder(source, "1", start, stop, Arrays.asList(Aref, C, G)).attribute(VCFConstants.ALLELE_FREQUENCY_KEY, new double[] { nonDefaultAF1, nonDefaultAF2 }).make();
    final double[] af3 = GermlineProbabilityCalculator.getGermlineAltAlleleFrequencies(altAlleles3, Optional.of(vc3), defaultAF);
    Assert.assertEquals(af3.length, altAlleles3.size());
    Assert.assertEquals(af3[0], nonDefaultAF1, 0.00001);
    Assert.assertEquals(af3[1], nonDefaultAF2, 0.00001);
    //triallelic, same alt alleles in different order
    final List<Allele> altAlleles4 = Arrays.asList(C, G);
    final VariantContext vc4 = new VariantContextBuilder(source, "1", start, stop, Arrays.asList(Aref, G, C)).attribute(VCFConstants.ALLELE_FREQUENCY_KEY, new double[] { nonDefaultAF1, nonDefaultAF2 }).make();
    final double[] af4 = GermlineProbabilityCalculator.getGermlineAltAlleleFrequencies(altAlleles4, Optional.of(vc4), defaultAF);
    Assert.assertEquals(af4.length, altAlleles4.size());
    Assert.assertEquals(af4[0], nonDefaultAF2, 0.00001);
    Assert.assertEquals(af4[1], nonDefaultAF1, 0.00001);
    //triallelic, only one allele in common
    final List<Allele> altAlleles5 = Arrays.asList(C, G);
    final VariantContext vc5 = new VariantContextBuilder(source, "1", start, stop, Arrays.asList(Aref, C, T)).attribute(VCFConstants.ALLELE_FREQUENCY_KEY, new double[] { nonDefaultAF1, nonDefaultAF2 }).make();
    final double[] af5 = GermlineProbabilityCalculator.getGermlineAltAlleleFrequencies(altAlleles5, Optional.of(vc5), defaultAF);
    Assert.assertEquals(af5.length, altAlleles5.size());
    Assert.assertEquals(af5[0], nonDefaultAF1, 0.00001);
    Assert.assertEquals(af5[1], defaultAF, 0.00001);
}
Also used : Allele(htsjdk.variant.variantcontext.Allele) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) VariantContext(htsjdk.variant.variantcontext.VariantContext) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 19 with VariantContextBuilder

use of htsjdk.variant.variantcontext.VariantContextBuilder in project gatk by broadinstitute.

the class FilterApplyingVariantIterator method next.

/**
     * Provides the next record from the underlying iterator after applying filter strings generated
     * by the set of filters in use by the iterator.
     */
@Override
public VariantContext next() {
    final VariantContext ctx = this.iterator.next();
    final Set<String> filterStrings = new HashSet<>();
    // Collect variant level filters
    for (final VariantFilter filter : this.filters) {
        final String val = filter.filter(ctx);
        if (val != null)
            filterStrings.add(val);
    }
    // Collect genotype level filters in a Map of Sample -> List<filter string>
    final ListMap<String, String> gtFilterStrings = new ListMap<>();
    final Set<String> variantSamples = new HashSet<>();
    for (final Genotype gt : ctx.getGenotypes()) {
        if (gt.isCalled() && !gt.isHomRef())
            variantSamples.add(gt.getSampleName());
        for (final GenotypeFilter filter : gtFilters) {
            final String filterString = filter.filter(ctx, gt);
            if (filterString != null)
                gtFilterStrings.add(gt.getSampleName(), filterString);
        }
    }
    // If all genotypes are filtered apply a site level filter
    if (gtFilterStrings.keySet().containsAll(variantSamples)) {
        filterStrings.add(ALL_GTS_FILTERED);
    }
    // Make a builder and set the site level filter appropriately
    final VariantContextBuilder builder = new VariantContextBuilder(ctx);
    if (filterStrings.isEmpty()) {
        builder.passFilters();
    } else {
        builder.filters(filterStrings);
    }
    // Apply filters to the necessary genotypes
    builder.noGenotypes();
    final List<Genotype> newGenotypes = new ArrayList<>(ctx.getNSamples());
    for (final Genotype gt : ctx.getGenotypes()) {
        final GenotypeBuilder gtBuilder = new GenotypeBuilder(gt);
        final List<String> filters = gtFilterStrings.get(gt.getSampleName());
        if (filters == null || filters.isEmpty()) {
            gtBuilder.filter(PASS_FILTER);
        } else {
            gtBuilder.filters(filters);
        }
        newGenotypes.add(gtBuilder.make());
    }
    builder.genotypes(newGenotypes);
    return builder.make();
}
Also used : VariantContext(htsjdk.variant.variantcontext.VariantContext) Genotype(htsjdk.variant.variantcontext.Genotype) GenotypeBuilder(htsjdk.variant.variantcontext.GenotypeBuilder) ListMap(htsjdk.samtools.util.ListMap) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder)

Example 20 with VariantContextBuilder

use of htsjdk.variant.variantcontext.VariantContextBuilder in project gatk by broadinstitute.

the class CalculateGenotypePosteriors method apply.

@Override
public void apply(final VariantContext variant, final ReadsContext readsContext, final ReferenceContext referenceContext, final FeatureContext featureContext) {
    final Collection<VariantContext> vcs = featureContext.getValues(getDrivingVariantsFeatureInput());
    final Collection<VariantContext> otherVCs = featureContext.getValues(supportVariants);
    final int missing = supportVariants.size() - otherVCs.size();
    for (final VariantContext vc : vcs) {
        final VariantContext vc_familyPriors;
        final VariantContext vc_bothPriors;
        //do family priors first (if applicable)
        final VariantContextBuilder builder = new VariantContextBuilder(vc);
        //only compute family priors for biallelelic sites
        if (!skipFamilyPriors && vc.isBiallelic()) {
            final GenotypesContext gc = famUtils.calculatePosteriorGLs(vc);
            builder.genotypes(gc);
        }
        VariantContextUtils.calculateChromosomeCounts(builder, false);
        vc_familyPriors = builder.make();
        if (!skipPopulationPriors) {
            vc_bothPriors = PosteriorProbabilitiesUtils.calculatePosteriorProbs(vc_familyPriors, otherVCs, missing * numRefIfMissing, globalPrior, !ignoreInputSamples, defaultToAC, useACoff);
        } else {
            final VariantContextBuilder builder2 = new VariantContextBuilder(vc_familyPriors);
            VariantContextUtils.calculateChromosomeCounts(builder, false);
            vc_bothPriors = builder2.make();
        }
        vcfWriter.add(vc_bothPriors);
    }
}
Also used : GenotypesContext(htsjdk.variant.variantcontext.GenotypesContext) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) VariantContext(htsjdk.variant.variantcontext.VariantContext)

Aggregations

VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)57 VariantContext (htsjdk.variant.variantcontext.VariantContext)40 Test (org.testng.annotations.Test)26 Allele (htsjdk.variant.variantcontext.Allele)25 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)21 Genotype (htsjdk.variant.variantcontext.Genotype)12 ReferenceContext (org.broadinstitute.hellbender.engine.ReferenceContext)8 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)7 GenotypeBuilder (htsjdk.variant.variantcontext.GenotypeBuilder)6 GenotypesContext (htsjdk.variant.variantcontext.GenotypesContext)6 File (java.io.File)6 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)5 VCFHeader (htsjdk.variant.vcf.VCFHeader)5 CommandLineProgramTest (org.broadinstitute.hellbender.CommandLineProgramTest)5 BeforeTest (org.testng.annotations.BeforeTest)5 SAMSequenceRecord (htsjdk.samtools.SAMSequenceRecord)4 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)4 VCFHeaderLine (htsjdk.variant.vcf.VCFHeaderLine)3 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3