Search in sources :

Example 41 with Allele

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

the class VariantDataManager method writeOutRecalibrationTable.

public void writeOutRecalibrationTable(final VariantContextWriter recalWriter, final SAMSequenceDictionary seqDictionary) {
    // we need to sort in coordinate order in order to produce a valid VCF
    Collections.sort(data, VariantDatum.getComparator(seqDictionary));
    // create dummy alleles to be used
    List<Allele> alleles = Arrays.asList(Allele.create("N", true), Allele.create("<VQSR>", false));
    for (final VariantDatum datum : data) {
        if (VRAC.useASannotations)
            //use the alleles to distinguish between multiallelics in AS mode
            alleles = Arrays.asList(datum.referenceAllele, datum.alternateAllele);
        VariantContextBuilder builder = new VariantContextBuilder("VQSR", datum.loc.getContig(), datum.loc.getStart(), datum.loc.getEnd(), alleles);
        builder.attribute(VCFConstants.END_KEY, datum.loc.getEnd());
        builder.attribute(GATKVCFConstants.VQS_LOD_KEY, String.format("%.4f", datum.lod));
        builder.attribute(GATKVCFConstants.CULPRIT_KEY, (datum.worstAnnotation != -1 ? annotationKeys.get(datum.worstAnnotation) : "NULL"));
        if (datum.atTrainingSite)
            builder.attribute(GATKVCFConstants.POSITIVE_LABEL_KEY, true);
        if (datum.atAntiTrainingSite)
            builder.attribute(GATKVCFConstants.NEGATIVE_LABEL_KEY, true);
        recalWriter.add(builder.make());
    }
}
Also used : Allele(htsjdk.variant.variantcontext.Allele) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder)

Example 42 with Allele

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

the class GenotypeUtils method computeDiploidGenotypeCounts.

/**
     * Returns a triple of ref/het/hom genotype "counts".
     *
     * The exact meaning of the count is dependent on the rounding behavior.
     * if {@code roundContributionFromEachGenotype}: the counts are discrete integer counts of the most probable genotype for each {@link Genotype}
     * else: they are the sum of the normalized likelihoods of each genotype and will not be integers
     *
     * Skips non-diploid genotypes.
     *
     *
     * @param vc the VariantContext that the {@link Genotype}s originated from, non-null
     * @param genotypes a GenotypesContext containing genotypes to count, these must be a subset of {@code vc.getGenotypes()}, non-null
     * @param roundContributionFromEachGenotype if this is true, the normalized likelihood from each genotype will be rounded before
     *                                          adding to the total count
     */
public static GenotypeCounts computeDiploidGenotypeCounts(final VariantContext vc, final GenotypesContext genotypes, final boolean roundContributionFromEachGenotype) {
    Utils.nonNull(vc, "vc");
    Utils.nonNull(genotypes, "genotypes");
    final boolean doMultiallelicMapping = !vc.isBiallelic();
    int idxAA = 0, idxAB = 1, idxBB = 2;
    double refCount = 0;
    double hetCount = 0;
    double homCount = 0;
    for (final Genotype g : genotypes) {
        if (!isDiploidWithLikelihoods(g)) {
            continue;
        }
        // Genotype::getLikelihoods returns a new array, so modification in-place is safe
        final double[] normalizedLikelihoods = MathUtils.normalizeFromLog10ToLinearSpace(g.getLikelihoods().getAsVector());
        if (doMultiallelicMapping) {
            if (g.isHetNonRef()) {
                //all likelihoods go to homCount
                homCount++;
                continue;
            }
            //get alternate allele for each sample
            final Allele a1 = g.getAllele(0);
            final Allele a2 = g.getAllele(1);
            if (a2.isNonReference()) {
                final int[] idxVector = vc.getGLIndecesOfAlternateAllele(a2);
                idxAA = idxVector[0];
                idxAB = idxVector[1];
                idxBB = idxVector[2];
            } else //I expect hets to be reference first, but there are no guarantees (e.g. phasing)
            if (a1.isNonReference()) {
                final int[] idxVector = vc.getGLIndecesOfAlternateAllele(a1);
                idxAA = idxVector[0];
                idxAB = idxVector[1];
                idxBB = idxVector[2];
            }
        }
        if (roundContributionFromEachGenotype) {
            refCount += MathUtils.fastRound(normalizedLikelihoods[idxAA]);
            hetCount += MathUtils.fastRound(normalizedLikelihoods[idxAB]);
            homCount += MathUtils.fastRound(normalizedLikelihoods[idxBB]);
        } else {
            refCount += normalizedLikelihoods[idxAA];
            hetCount += normalizedLikelihoods[idxAB];
            homCount += normalizedLikelihoods[idxBB];
        }
    }
    return new GenotypeCounts(refCount, hetCount, homCount);
}
Also used : Allele(htsjdk.variant.variantcontext.Allele) Genotype(htsjdk.variant.variantcontext.Genotype)

Example 43 with Allele

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

the class CoverageUnitTest method testLikelihoods.

@Test
public void testLikelihoods() {
    final Allele REF = Allele.create("T", true);
    final Allele ALT = Allele.create("A");
    final int refDepth = 5;
    final int altDepth = 3;
    final List<GATKRead> refReads = IntStream.range(0, refDepth).mapToObj(n -> makeRead()).collect(Collectors.toList());
    final List<GATKRead> altReads = IntStream.range(0, altDepth).mapToObj(n -> makeRead()).collect(Collectors.toList());
    final ReadLikelihoods<Allele> likelihoods = AnnotationArtificialData.makeLikelihoods("sample1", refReads, altReads, -100.0, -100.0, REF, ALT);
    final VariantContext vc = makeVC();
    final ReferenceContext referenceContext = null;
    final Map<String, Object> annotate = new Coverage().annotate(referenceContext, vc, likelihoods);
    Assert.assertEquals(annotate.size(), 1, "size");
    Assert.assertEquals(annotate.keySet(), Collections.singleton(VCFConstants.DEPTH_KEY), "annots");
    final int m = altDepth + refDepth;
    Assert.assertEquals(annotate.get(VCFConstants.DEPTH_KEY), String.valueOf(m));
}
Also used : TextCigarCodec(htsjdk.samtools.TextCigarCodec) IntStream(java.util.stream.IntStream) Allele(htsjdk.variant.variantcontext.Allele) java.util(java.util) ImmutableMap(com.google.common.collect.ImmutableMap) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test) GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) Collectors(java.util.stream.Collectors) GenotypesContext(htsjdk.variant.variantcontext.GenotypesContext) ReadLikelihoods(org.broadinstitute.hellbender.utils.genotyper.ReadLikelihoods) ArtificialReadUtils(org.broadinstitute.hellbender.utils.read.ArtificialReadUtils) Assert(org.testng.Assert) AlleleList(org.broadinstitute.hellbender.utils.genotyper.AlleleList) IndexedAlleleList(org.broadinstitute.hellbender.utils.genotyper.IndexedAlleleList) VariantContext(htsjdk.variant.variantcontext.VariantContext) IndexedSampleList(org.broadinstitute.hellbender.utils.genotyper.IndexedSampleList) ReferenceContext(org.broadinstitute.hellbender.engine.ReferenceContext) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) VCFConstants(htsjdk.variant.vcf.VCFConstants) GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) Allele(htsjdk.variant.variantcontext.Allele) ReferenceContext(org.broadinstitute.hellbender.engine.ReferenceContext) VariantContext(htsjdk.variant.variantcontext.VariantContext) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 44 with Allele

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

the class CoverageUnitTest method makeVC.

private VariantContext makeVC() {
    final GenotypesContext testGC = GenotypesContext.create(2);
    final Allele refAllele = Allele.create("A", true);
    final Allele altAllele = Allele.create("T");
    return (new VariantContextBuilder()).alleles(Arrays.asList(refAllele, altAllele)).chr("1").start(15L).stop(15L).genotypes(testGC).make();
}
Also used : Allele(htsjdk.variant.variantcontext.Allele) GenotypesContext(htsjdk.variant.variantcontext.GenotypesContext) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder)

Example 45 with Allele

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

the class MappingQualityZeroUnitTest method testLikelihoods.

@Test
public void testLikelihoods() {
    final Allele REF = Allele.create("T", true);
    final Allele ALT = Allele.create("A");
    final int refDepth = 5;
    final int altDepth = 3;
    final int refMQ = 10;
    final int altMQ = 0;
    final List<GATKRead> refReads = IntStream.range(0, refDepth).mapToObj(i -> makeRead(refMQ)).collect(Collectors.toList());
    final List<GATKRead> altReads = IntStream.range(0, altDepth).mapToObj(i -> makeRead(altMQ)).collect(Collectors.toList());
    final ReadLikelihoods<Allele> likelihoods = AnnotationArtificialData.makeLikelihoods("sample1", refReads, altReads, -1.0, -1.0, REF, ALT);
    final VariantContext vc = makeVC();
    final ReferenceContext referenceContext = null;
    final Map<String, Object> annotate = new MappingQualityZero().annotate(referenceContext, vc, likelihoods);
    Assert.assertEquals(annotate.size(), 1, "size");
    Assert.assertEquals(annotate.keySet(), Collections.singleton(VCFConstants.MAPPING_QUALITY_ZERO_KEY), "annots");
    Assert.assertEquals(annotate.get(VCFConstants.MAPPING_QUALITY_ZERO_KEY), String.valueOf(altDepth));
}
Also used : IntStream(java.util.stream.IntStream) Allele(htsjdk.variant.variantcontext.Allele) Arrays(java.util.Arrays) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.testng.annotations.Test) GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) Collectors(java.util.stream.Collectors) GenotypesContext(htsjdk.variant.variantcontext.GenotypesContext) ReadLikelihoods(org.broadinstitute.hellbender.utils.genotyper.ReadLikelihoods) List(java.util.List) Assert(org.testng.Assert) Map(java.util.Map) AlleleList(org.broadinstitute.hellbender.utils.genotyper.AlleleList) IndexedAlleleList(org.broadinstitute.hellbender.utils.genotyper.IndexedAlleleList) VariantContext(htsjdk.variant.variantcontext.VariantContext) IndexedSampleList(org.broadinstitute.hellbender.utils.genotyper.IndexedSampleList) ReferenceContext(org.broadinstitute.hellbender.engine.ReferenceContext) Collections(java.util.Collections) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) VCFConstants(htsjdk.variant.vcf.VCFConstants) GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) Allele(htsjdk.variant.variantcontext.Allele) ReferenceContext(org.broadinstitute.hellbender.engine.ReferenceContext) VariantContext(htsjdk.variant.variantcontext.VariantContext) Test(org.testng.annotations.Test)

Aggregations

Allele (htsjdk.variant.variantcontext.Allele)91 Test (org.testng.annotations.Test)48 VariantContext (htsjdk.variant.variantcontext.VariantContext)44 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)27 Genotype (htsjdk.variant.variantcontext.Genotype)26 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)22 java.util (java.util)19 Collectors (java.util.stream.Collectors)16 IntStream (java.util.stream.IntStream)14 ReferenceContext (org.broadinstitute.hellbender.engine.ReferenceContext)13 CommandLineProgramTest (org.broadinstitute.hellbender.CommandLineProgramTest)12 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)12 File (java.io.File)11 StandardArgumentDefinitions (org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions)11 ReadLikelihoods (org.broadinstitute.hellbender.utils.genotyper.ReadLikelihoods)11 GATKRead (org.broadinstitute.hellbender.utils.read.GATKRead)11 GenotypeBuilder (htsjdk.variant.variantcontext.GenotypeBuilder)10 VCFConstants (htsjdk.variant.vcf.VCFConstants)10 IOException (java.io.IOException)10 Target (org.broadinstitute.hellbender.tools.exome.Target)10