Search in sources :

Example 11 with GenotypeBuilder

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

the class EvaluateCopyNumberTriStateCalls method composeNonTruthOverlappingGenotype.

private Genotype composeNonTruthOverlappingGenotype(final VariantContext enclosingContext, final Genotype genotype) {
    final GenotypeBuilder builder = new GenotypeBuilder(genotype.getSampleName());
    if (genotype.isCalled()) {
        GATKProtectedVariantContextUtils.setGenotypeQualityFromPLs(builder, genotype);
        final int[] PL = genotype.getPL();
        final int callAlleleIndex = GATKProtectedMathUtils.minIndex(PL);
        final double quality = callQuality(genotype);
        builder.alleles(Collections.singletonList(enclosingContext.getAlleles().get(callAlleleIndex)));
        builder.attribute(VariantEvaluationContext.CALL_QUALITY_KEY, quality);
        final boolean discovered = XHMMSegmentGenotyper.DISCOVERY_TRUE.equals(GATKProtectedVariantContextUtils.getAttributeAsString(genotype, XHMMSegmentGenotyper.DISCOVERY_KEY, XHMMSegmentGenotyper.DISCOVERY_FALSE));
        if (callAlleleIndex != 0 && discovered) {
            builder.attribute(VariantEvaluationContext.EVALUATION_CLASS_KEY, EvaluationClass.UNKNOWN_POSITIVE.acronym);
        }
        if (quality < filterArguments.minimumCalledSegmentQuality) {
            builder.filter(EvaluationFilter.LowQuality.acronym);
        } else {
            builder.filter(EvaluationFilter.PASS);
        }
    } else {
        /* assume it is REF */
        /* TODO this is a hack to make Andrey's CODEX vcf work; and in general, VCFs that only include discovered
             * variants and NO_CALL (".") on other samples. The idea is to force the evaluation tool to take it call
             * as REF on all other samples. Otherwise, the effective allele frequency of the variant will be erroneously
             * high and will be filtered. */
        builder.alleles(Collections.singletonList(CopyNumberTriStateAllele.REF));
        builder.attribute(VariantEvaluationContext.CALL_QUALITY_KEY, 100000);
        builder.filter(EvaluationFilter.PASS);
    }
    return builder.make();
}
Also used : GenotypeBuilder(htsjdk.variant.variantcontext.GenotypeBuilder)

Example 12 with GenotypeBuilder

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

the class SelectVariants method subsetRecord.

/**
     * Helper method to subset a VC record, modifying some metadata stored in the INFO field (i.e. AN, AC, AF).
     *
     * @param vc       the VariantContext record to subset
     * @param preserveAlleles should we trim constant sequence from the beginning and/or end of all alleles, or preserve it?
     * @param removeUnusedAlternates removes alternate alleles with AC=0
     * @return the subsetted VariantContext
     */
private VariantContext subsetRecord(final VariantContext vc, final boolean preserveAlleles, final boolean removeUnusedAlternates) {
    //subContextFromSamples() always decodes the vc, which is a fairly expensive operation.  Avoid if possible
    if (noSamplesSpecified && !removeUnusedAlternates) {
        return vc;
    }
    // strip out the alternate alleles that aren't being used
    final VariantContext sub = vc.subContextFromSamples(samples, removeUnusedAlternates);
    // If no subsetting happened, exit now
    if (sub.getNSamples() == vc.getNSamples() && sub.getNAlleles() == vc.getNAlleles()) {
        return vc;
    }
    // fix the PL and AD values if sub has fewer alleles than original vc and remove a fraction of the genotypes if needed
    final GenotypesContext oldGs = sub.getGenotypes();
    GenotypesContext newGC = sub.getNAlleles() == vc.getNAlleles() ? oldGs : AlleleSubsettingUtils.subsetAlleles(oldGs, 0, vc.getAlleles(), sub.getAlleles(), GenotypeAssignmentMethod.DO_NOT_ASSIGN_GENOTYPES, vc.getAttributeAsInt(VCFConstants.DEPTH_KEY, 0));
    if (fractionGenotypes > 0) {
        final List<Genotype> genotypes = newGC.stream().map(genotype -> randomGenotypes.nextDouble() > fractionGenotypes ? genotype : new GenotypeBuilder(genotype).alleles(getNoCallAlleles(genotype.getPloidy())).noGQ().make()).collect(Collectors.toList());
        newGC = GenotypesContext.create(new ArrayList<>(genotypes));
    }
    // since the VC has been subset (either by sample or allele), we need to strip out the MLE tags
    final VariantContextBuilder builder = new VariantContextBuilder(sub);
    builder.rmAttributes(Arrays.asList(GATKVCFConstants.MLE_ALLELE_COUNT_KEY, GATKVCFConstants.MLE_ALLELE_FREQUENCY_KEY));
    builder.genotypes(newGC);
    addAnnotations(builder, vc, sub.getSampleNames());
    final VariantContext subset = builder.make();
    return preserveAlleles ? subset : GATKVariantContextUtils.trimAlleles(subset, true, true);
}
Also used : Genotype(htsjdk.variant.variantcontext.Genotype) VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) DocumentedFeature(org.broadinstitute.barclay.help.DocumentedFeature) Allele(htsjdk.variant.variantcontext.Allele) CommandLineProgramProperties(org.broadinstitute.barclay.argparser.CommandLineProgramProperties) java.util(java.util) GenotypeLikelihoods(htsjdk.variant.variantcontext.GenotypeLikelihoods) VCFStandardHeaderLines(htsjdk.variant.vcf.VCFStandardHeaderLines) VCFHeader(htsjdk.variant.vcf.VCFHeader) Argument(org.broadinstitute.barclay.argparser.Argument) GenotypeAssignmentMethod(org.broadinstitute.hellbender.tools.walkers.genotyper.GenotypeAssignmentMethod) StandardArgumentDefinitions(org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions) SampleDB(org.broadinstitute.hellbender.utils.samples.SampleDB) VariantProgramGroup(org.broadinstitute.hellbender.cmdline.programgroups.VariantProgramGroup) VariantIDsVariantFilter(org.broadinstitute.hellbender.engine.filters.VariantIDsVariantFilter) VariantContextUtils(htsjdk.variant.variantcontext.VariantContextUtils) SampleDBBuilder(org.broadinstitute.hellbender.utils.samples.SampleDBBuilder) ReferenceContext(org.broadinstitute.hellbender.engine.ReferenceContext) FeatureContext(org.broadinstitute.hellbender.engine.FeatureContext) VCFConstants(htsjdk.variant.vcf.VCFConstants) VariantWalker(org.broadinstitute.hellbender.engine.VariantWalker) GenotypeBuilder(htsjdk.variant.variantcontext.GenotypeBuilder) Predicate(java.util.function.Predicate) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) VariantFilterLibrary(org.broadinstitute.hellbender.engine.filters.VariantFilterLibrary) Collectors(java.util.stream.Collectors) VariantFilter(org.broadinstitute.hellbender.engine.filters.VariantFilter) File(java.io.File) GenotypesContext(htsjdk.variant.variantcontext.GenotypesContext) UserException(org.broadinstitute.hellbender.exceptions.UserException) AlleleSubsettingUtils(org.broadinstitute.hellbender.tools.walkers.genotyper.AlleleSubsettingUtils) org.broadinstitute.hellbender.utils.variant(org.broadinstitute.hellbender.utils.variant) MendelianViolation(org.broadinstitute.hellbender.utils.samples.MendelianViolation) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VariantTypesVariantFilter(org.broadinstitute.hellbender.engine.filters.VariantTypesVariantFilter) ChromosomeCounts(org.broadinstitute.hellbender.tools.walkers.annotator.ChromosomeCounts) PedigreeValidationType(org.broadinstitute.hellbender.utils.samples.PedigreeValidationType) VariantContext(htsjdk.variant.variantcontext.VariantContext) VCFUtils(htsjdk.variant.vcf.VCFUtils) Utils(org.broadinstitute.hellbender.utils.Utils) Hidden(org.broadinstitute.barclay.argparser.Hidden) FeatureInput(org.broadinstitute.hellbender.engine.FeatureInput) ReadsContext(org.broadinstitute.hellbender.engine.ReadsContext) Pattern(java.util.regex.Pattern) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) GenotypesContext(htsjdk.variant.variantcontext.GenotypesContext) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) VariantContext(htsjdk.variant.variantcontext.VariantContext) Genotype(htsjdk.variant.variantcontext.Genotype) GenotypeBuilder(htsjdk.variant.variantcontext.GenotypeBuilder)

Example 13 with GenotypeBuilder

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

the class GenotypingEngineUnitTest method testCalculateGenotypes.

// Want to run testCoveredByDeletion first to ensure clean list or recorded deletions
@Test(dependsOnMethods = { "testCoveredByDeletion" })
public void testCalculateGenotypes() {
    genotypingEngine.clearUpstreamDeletionsLoc();
    // Remove deletion
    final List<Genotype> genotypes = Arrays.asList(// first alt
    new GenotypeBuilder("sample1").alleles(gtAlleles).PL(new double[] { 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }).make(), // second alt
    new GenotypeBuilder("sample2").alleles(gtAlleles).PL(new double[] { 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0 }).make());
    final VariantContext vc = new VariantContextBuilder("test", "1", 1, refAllele.length(), allelesDel).genotypes(genotypes).make();
    final VariantContext vcOut = genotypingEngine.calculateGenotypes(vc, GenotypeLikelihoodsCalculationModel.INDEL, null);
    Assert.assertFalse(vcOut.getAlleles().contains(altT));
    // Make sure the spanning deletion is removed since the deletion was removed
    final Allele refAlleleSpanDel = Allele.create("C", true);
    final List<Allele> vcAllelesSpanDel = new ArrayList<>(Arrays.asList(refAlleleSpanDel, Allele.SPAN_DEL, GATKVCFConstants.NON_REF_SYMBOLIC_ALLELE));
    final List<Genotype> genotypesSpanDel = Arrays.asList(// first alt
    new GenotypeBuilder("sample1").alleles(gtAlleles).PL(new double[] { 0, 0, 100, 0, 0, 0 }).make(), new GenotypeBuilder("sample2").alleles(gtAlleles).PL(new double[] { 0, 0, 0, 0, 0, 0 }).make());
    final VariantContext vcSpanDel = new VariantContextBuilder("test1", "1", 2, 2 + refAlleleSpanDel.length() - 1, vcAllelesSpanDel).genotypes(genotypesSpanDel).make();
    final VariantContext vcOut1 = genotypingEngine.calculateGenotypes(vcSpanDel, GenotypeLikelihoodsCalculationModel.INDEL, null);
    Assert.assertFalse(vcOut1.getAlleles().contains(Allele.SPAN_DEL));
}
Also used : Allele(htsjdk.variant.variantcontext.Allele) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) ArrayList(java.util.ArrayList) Genotype(htsjdk.variant.variantcontext.Genotype) VariantContext(htsjdk.variant.variantcontext.VariantContext) GenotypeBuilder(htsjdk.variant.variantcontext.GenotypeBuilder) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 14 with GenotypeBuilder

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

the class HomRefBlockUnitTest method testAddGQGreaterThanMaxGenotypeQual.

@Test
public void testAddGQGreaterThanMaxGenotypeQual() {
    final VariantContext vc = getVariantContext();
    final HomRefBlock block90_100 = new HomRefBlock(vc, 90, 100, HomoSapiensConstants.DEFAULT_PLOIDY);
    // Test that adding a Genotype with GQ > 99 succeeds (ie., doesn't throw).
    // Internally, HomRefBlock should treat this GQ as 99.
    block90_100.add(vc.getStart(), new GenotypeBuilder(SAMPLE_NAME, vc.getAlleles()).GQ(150).DP(10).PL(new int[] { 0, 10, 100 }).make());
}
Also used : VariantContext(htsjdk.variant.variantcontext.VariantContext) GenotypeBuilder(htsjdk.variant.variantcontext.GenotypeBuilder) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 15 with GenotypeBuilder

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

the class GenotypeConcordanceTest method testGenotypeConcordanceDetermineStateDp.

@Test
public void testGenotypeConcordanceDetermineStateDp() throws Exception {
    final List<Allele> allelesNormal = makeUniqueListOfAlleles(Aref, C);
    final Genotype gtNormal = GenotypeBuilder.create(TRUTH_SAMPLE_NAME, Arrays.asList(Aref, C));
    final VariantContext vcNormal = new VariantContextBuilder("test", snpLoc, snpLocStart, snpLocStop, allelesNormal).genotypes(gtNormal).make();
    final List<Allele> allelesLowDp = makeUniqueListOfAlleles(Aref, C);
    final Genotype gtLowDp = new GenotypeBuilder(TRUTH_SAMPLE_NAME, Arrays.asList(Aref, C)).DP(4).make();
    final VariantContext vcLowDp = new VariantContextBuilder("test", snpLoc, snpLocStart, snpLocStop, allelesLowDp).genotypes(gtLowDp).make();
    testGenotypeConcordanceDetermineState(vcLowDp, TruthState.LOW_DP, vcNormal, CallState.HET_REF_VAR1, 0, 20);
    testGenotypeConcordanceDetermineState(vcLowDp, TruthState.HET_REF_VAR1, vcLowDp, CallState.HET_REF_VAR1, 0, 2);
    testGenotypeConcordanceDetermineState(vcNormal, TruthState.HET_REF_VAR1, vcLowDp, CallState.LOW_DP, 0, 20);
    testGenotypeConcordanceDetermineState(vcNormal, TruthState.HET_REF_VAR1, vcLowDp, CallState.HET_REF_VAR1, 0, 2);
    testGenotypeConcordanceDetermineState(vcLowDp, TruthState.LOW_DP, vcLowDp, CallState.LOW_DP, 0, 20);
    testGenotypeConcordanceDetermineState(vcLowDp, TruthState.HET_REF_VAR1, vcLowDp, CallState.HET_REF_VAR1, 0, 2);
}
Also used : Allele(htsjdk.variant.variantcontext.Allele) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) Genotype(htsjdk.variant.variantcontext.Genotype) VariantContext(htsjdk.variant.variantcontext.VariantContext) GenotypeBuilder(htsjdk.variant.variantcontext.GenotypeBuilder) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest)

Aggregations

GenotypeBuilder (htsjdk.variant.variantcontext.GenotypeBuilder)17 VariantContext (htsjdk.variant.variantcontext.VariantContext)14 Genotype (htsjdk.variant.variantcontext.Genotype)12 Allele (htsjdk.variant.variantcontext.Allele)11 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)7 Test (org.testng.annotations.Test)7 java.util (java.util)6 Collectors (java.util.stream.Collectors)4 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)4 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)3 File (java.io.File)3 Argument (org.broadinstitute.barclay.argparser.Argument)3 CommandLineProgramProperties (org.broadinstitute.barclay.argparser.CommandLineProgramProperties)3 DocumentedFeature (org.broadinstitute.barclay.help.DocumentedFeature)3 CommandLineProgramTest (org.broadinstitute.hellbender.CommandLineProgramTest)3 StandardArgumentDefinitions (org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions)3 ReferenceContext (org.broadinstitute.hellbender.engine.ReferenceContext)3 org.broadinstitute.hellbender.utils (org.broadinstitute.hellbender.utils)3 Locatable (htsjdk.samtools.util.Locatable)2 VariantContextWriterBuilder (htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder)2