Search in sources :

Example 31 with Genotype

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

the class EvaluateCopyNumberTriStateCalls method updateSampleStats.

private void updateSampleStats(final Map<String, EvaluationSampleSummaryRecord> sampleStats, final VariantEvaluationContext vc) {
    // ignore filtered out variants.
    if (vc.getFilters().stream().anyMatch(f -> !VCFConstants.PASSES_FILTERS_v4.equals(f))) {
        return;
    }
    for (final Genotype genotype : vc.getGenotypes()) {
        final Set<String> filters = genotype.getFilters() == null ? Collections.emptySet() : Stream.of(genotype.getFilters().split(VCFConstants.INFO_FIELD_ARRAY_SEPARATOR)).collect(Collectors.toSet());
        if (filters.stream().anyMatch(s -> !s.equals(VCFConstants.PASSES_FILTERS_v4))) {
            continue;
        }
        final String sample = genotype.getSampleName();
        final String evalClassString = GATKProtectedVariantContextUtils.getAttributeAsString(genotype, VariantEvaluationContext.EVALUATION_CLASS_KEY, null);
        if (evalClassString != null) {
            final EvaluationClass evalClass = EvaluationClass.parseString(evalClassString);
            sampleStats.get(sample).increase(evalClass);
        }
    }
}
Also used : Genotype(htsjdk.variant.variantcontext.Genotype)

Example 32 with Genotype

use of htsjdk.variant.variantcontext.Genotype 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 33 with Genotype

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

the class SelectVariants method addAnnotations.

/*
     * Add annotations to the new VC
     *
     * @param builder     the new VC to annotate
     * @param originalVC  the original VC
     * @param selectedSampleNames the post-selection list of sample names
     */
private void addAnnotations(final VariantContextBuilder builder, final VariantContext originalVC, final Set<String> selectedSampleNames) {
    if (fullyDecode) {
        // TODO -- annotations are broken with fully decoded data
        return;
    }
    if (keepOriginalChrCounts) {
        final int[] indexOfOriginalAlleleForNewAllele;
        final List<Allele> newAlleles = builder.getAlleles();
        final int numOriginalAlleles = originalVC.getNAlleles();
        // if the alleles already match up, we can just copy the previous list of counts
        if (numOriginalAlleles == newAlleles.size()) {
            indexOfOriginalAlleleForNewAllele = null;
        } else // otherwise we need to parse them and select out the correct ones
        {
            indexOfOriginalAlleleForNewAllele = new int[newAlleles.size() - 1];
            Arrays.fill(indexOfOriginalAlleleForNewAllele, -1);
            // note that we don't care about the reference allele at position 0
            for (int newI = 1; newI < newAlleles.size(); newI++) {
                final Allele newAlt = newAlleles.get(newI);
                for (int oldI = 0; oldI < numOriginalAlleles - 1; oldI++) {
                    if (newAlt.equals(originalVC.getAlternateAllele(oldI), false)) {
                        indexOfOriginalAlleleForNewAllele[newI - 1] = oldI;
                        break;
                    }
                }
            }
        }
        if (originalVC.hasAttribute(VCFConstants.ALLELE_COUNT_KEY)) {
            builder.attribute(GATKVCFConstants.ORIGINAL_AC_KEY, getReorderedAttributes(originalVC.getAttribute(VCFConstants.ALLELE_COUNT_KEY), indexOfOriginalAlleleForNewAllele));
        }
        if (originalVC.hasAttribute(VCFConstants.ALLELE_FREQUENCY_KEY)) {
            builder.attribute(GATKVCFConstants.ORIGINAL_AF_KEY, getReorderedAttributes(originalVC.getAttribute(VCFConstants.ALLELE_FREQUENCY_KEY), indexOfOriginalAlleleForNewAllele));
        }
        if (originalVC.hasAttribute(VCFConstants.ALLELE_NUMBER_KEY)) {
            builder.attribute(GATKVCFConstants.ORIGINAL_AN_KEY, originalVC.getAttribute(VCFConstants.ALLELE_NUMBER_KEY));
        }
    }
    VariantContextUtils.calculateChromosomeCounts(builder, false);
    if (keepOriginalDepth && originalVC.hasAttribute(VCFConstants.DEPTH_KEY)) {
        builder.attribute(GATKVCFConstants.ORIGINAL_DP_KEY, originalVC.getAttribute(VCFConstants.DEPTH_KEY));
    }
    boolean sawDP = false;
    int depth = 0;
    for (final String sample : selectedSampleNames) {
        final Genotype g = originalVC.getGenotype(sample);
        if (!g.isFiltered()) {
            if (g.hasDP()) {
                depth += g.getDP();
                sawDP = true;
            }
        }
    }
    if (sawDP) {
        builder.attribute(VCFConstants.DEPTH_KEY, depth);
    }
}
Also used : Allele(htsjdk.variant.variantcontext.Allele) Genotype(htsjdk.variant.variantcontext.Genotype)

Example 34 with Genotype

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

the class SelectVariants method initalizeAlleleAnyploidIndicesCache.

/**
     * Initialize cache of allele anyploid indices
     *
     * Initialize the cache of PL index to a list of alleles for each ploidy.
     *
     * @param vc    Variant Context
    */
private void initalizeAlleleAnyploidIndicesCache(final VariantContext vc) {
    if (vc.getType() != VariantContext.Type.NO_VARIATION) {
        // Bypass if not a variant
        for (final Genotype g : vc.getGenotypes()) {
            if (g.getPloidy() != 0) {
                // that the
                if (!ploidyToNumberOfAlleles.containsKey(g.getPloidy()) || ploidyToNumberOfAlleles.get(g.getPloidy()) < vc.getNAlleles()) {
                    GenotypeLikelihoods.initializeAnyploidPLIndexToAlleleIndices(vc.getNAlleles() - 1, g.getPloidy());
                    ploidyToNumberOfAlleles.put(g.getPloidy(), vc.getNAlleles());
                }
            }
        }
    }
}
Also used : Genotype(htsjdk.variant.variantcontext.Genotype)

Example 35 with Genotype

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

the class GVCFWriter method add.

/**
     * Add a VariantContext to this writer for emission
     *
     * Requires that the VC have exactly one genotype
     *
     * @param vc a non-null VariantContext
     */
@Override
public void add(VariantContext vc) {
    Utils.nonNull(vc);
    Utils.validateArg(vc.hasGenotypes(), "GVCF assumes that the VariantContext has genotypes");
    Utils.validateArg(vc.getGenotypes().size() == 1, () -> "GVCF assumes that the VariantContext has exactly one genotype but saw " + vc.getGenotypes().size());
    if (sampleName == null) {
        sampleName = vc.getGenotype(0).getSampleName();
    }
    if (currentBlock != null && !currentBlock.isContiguous(vc)) {
        // we've made a non-contiguous step (across interval, onto another chr), so finalize
        emitCurrentBlock();
    }
    final Genotype g = vc.getGenotype(0);
    if (g.isHomRef() && vc.hasAlternateAllele(GATKVCFConstants.NON_REF_SYMBOLIC_ALLELE) && vc.isBiallelic()) {
        // create bands
        final VariantContext maybeCompletedBand = addHomRefSite(vc, g);
        if (maybeCompletedBand != null) {
            underlyingWriter.add(maybeCompletedBand);
        }
    } else {
        // g is variant, so flush the bands and emit vc
        emitCurrentBlock();
        nextAvailableStart = vc.getEnd();
        contigOfNextAvailableStart = vc.getContig();
        underlyingWriter.add(vc);
    }
}
Also used : Genotype(htsjdk.variant.variantcontext.Genotype) VariantContext(htsjdk.variant.variantcontext.VariantContext)

Aggregations

Genotype (htsjdk.variant.variantcontext.Genotype)150 VariantContext (htsjdk.variant.variantcontext.VariantContext)97 Allele (htsjdk.variant.variantcontext.Allele)82 ArrayList (java.util.ArrayList)54 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)52 GenotypeBuilder (htsjdk.variant.variantcontext.GenotypeBuilder)51 File (java.io.File)48 VCFHeader (htsjdk.variant.vcf.VCFHeader)46 IOException (java.io.IOException)45 Collectors (java.util.stream.Collectors)42 Test (org.testng.annotations.Test)37 HashSet (java.util.HashSet)35 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)29 List (java.util.List)29 VCFHeaderLine (htsjdk.variant.vcf.VCFHeaderLine)27 VCFFormatHeaderLine (htsjdk.variant.vcf.VCFFormatHeaderLine)25 SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)23 HashMap (java.util.HashMap)23 java.util (java.util)22 Set (java.util.Set)22