Java Code Examples for htsjdk.variant.variantcontext.VariantContext#getAttributeAsInt()

The following examples show how to use htsjdk.variant.variantcontext.VariantContext#getAttributeAsInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ValidationReport.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private SiteStatus calcSiteStatus(VariantContext vc) {
    if ( vc == null ) return SiteStatus.NO_CALL;
    if ( vc.isFiltered() ) return SiteStatus.FILTERED;
    if ( vc.isMonomorphicInSamples() ) return SiteStatus.MONO;
    if ( vc.hasGenotypes() ) return SiteStatus.POLY;  // must be polymorphic if isMonomorphicInSamples was false and there are genotypes

    if ( vc.hasAttribute(VCFConstants.ALLELE_COUNT_KEY) ) {
        int ac = 0;
        if ( vc.getNAlleles() > 2 ) {
            return SiteStatus.POLY;
        }
        else
            ac = vc.getAttributeAsInt(VCFConstants.ALLELE_COUNT_KEY, 0);
        return ac > 0 ? SiteStatus.POLY : SiteStatus.MONO;
    } else {
        return TREAT_ALL_SITES_IN_EVAL_VCF_AS_CALLED ? SiteStatus.POLY : SiteStatus.NO_CALL; // we can't figure out what to do
    }
}
 
Example 2
Source File: MicrosatelliteIndels.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void accept(@NotNull final VariantContext context) {
    if (isPassIndel(context)) {
        int repeatCount = context.getAttributeAsInt(REPEAT_COUNT_FLAG, 0);
        int repeatSequenceLength = context.getAttributeAsString(REPEAT_SEQUENCE_FLAG, Strings.EMPTY).length();

        if (repeatContextIsRelevant(repeatCount, repeatSequenceLength)) {
            indelCount++;
        }
    }
}
 
Example 3
Source File: StructuralVariantFactory.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
private static ImmutableStructuralVariantImpl.Builder setCommon(@NotNull ImmutableStructuralVariantImpl.Builder builder,
        @NotNull VariantContext context) {
    // backwards compatibility for Manta until fully over to GRIDSS
    int somaticScore = context.getAttributeAsInt(SOMATIC_SCORE, 0);
    double qualityScore = context.getPhredScaledQual();

    if (somaticScore > 0) {
        qualityScore = somaticScore;
    }

    builder.id(context.getID())
            .recovered(context.hasAttribute(RECOVERED))
            .recoveryMethod(context.getAttributeAsString(RECOVERY_METHOD, null))
            .recoveryFilter(context.getAttributeAsStringList(RECOVERY_FILTER, "").stream().collect(Collectors.joining(",")))
            .event(context.getAttributeAsString(EVENT, null))
            .startLinkedBy(context.getAttributeAsStringList(LOCAL_LINKED_BY, "")
                    .stream()
                    .filter(s -> !Strings.isNullOrEmpty(s))
                    .collect(Collectors.joining(",")))
            .endLinkedBy(context.getAttributeAsStringList(REMOTE_LINKED_BY, "")
                    .stream()
                    .filter(s -> !Strings.isNullOrEmpty(s))
                    .collect(Collectors.joining(",")))
            .imprecise(imprecise(context))
            .qualityScore(qualityScore)
            .insertSequenceAlignments(context.getAttributeAsStringList(UNTEMPLATED_SEQUENCE_ALIGNMENTS, "")
                    .stream()
                    .filter(s -> !Strings.isNullOrEmpty(s))
                    .collect(Collectors.joining(",")));
    if (context.hasAttribute(UNTEMPLATED_SEQUENCE_REPEAT_CLASS)) {
        builder.insertSequenceRepeatClass(context.getAttributeAsString(UNTEMPLATED_SEQUENCE_REPEAT_CLASS, ""))
                .insertSequenceRepeatType(context.getAttributeAsString(UNTEMPLATED_SEQUENCE_REPEAT_TYPE, ""))
                .insertSequenceRepeatOrientation(context.getAttributeAsString(UNTEMPLATED_SEQUENCE_REPEAT_ORIENTATION, "+").equals("+")
                        ? (byte) 1
                        : (byte) -1)
                .insertSequenceRepeatCoverage(context.getAttributeAsDouble(UNTEMPLATED_SEQUENCE_REPEAT_COVERAGE, 0));
    }
    return builder;
}
 
Example 4
Source File: SegmentedCpxVariantSimpleVariantExtractor.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private List<VariantContext> whenZeroSegments(final VariantContext complexVC, final ReferenceMultiSparkSource reference) {
    final Allele anchorBaseRefAllele = getAnchorBaseRefAllele(complexVC.getContig(), complexVC.getStart(), reference);
    final int altSeqLength = complexVC.getAttributeAsString(SEQ_ALT_HAPLOTYPE, "").length() - 2;
    final List<String> mappingQualities = SVUtils.getAttributeAsStringList(complexVC, MAPPING_QUALITIES);
    final int maxAlignLength = complexVC.getAttributeAsInt(MAX_ALIGN_LENGTH, 0);
    final VariantContext insertion = makeInsertion(complexVC.getContig(), complexVC.getStart(), complexVC.getStart(), altSeqLength, anchorBaseRefAllele)
            .attribute(CPX_EVENT_KEY, complexVC.getID())
            .attribute(CONTIG_NAMES, complexVC.getAttribute(CONTIG_NAMES))
            .attribute(MAPPING_QUALITIES, mappingQualities)
            .attribute(MAX_ALIGN_LENGTH, maxAlignLength)
            .make();
    return Collections.singletonList(insertion);
}
 
Example 5
Source File: SegmentedCpxVariantSimpleVariantExtractor.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Depending on how the ref segment is present in alt arrangement (if at all), logic as follows (order is important):
 * <ul>
 *     <li>
 *         if ref segment appear inverted and large enough
 *         <ul>
 *             <li> INV call is warranted </li>
 *             <li> INS call(s) before and after the INV, if inserted sequence long enough </li>
 *         </ul>
 *     </li>
 *
 *     <li>
 *         otherwise if ref segment is present as-is, i.e. no deletion call can be made,
 *         make insertion calls when possible
 *     </li>
 *     <li>
 *         otherwise
 *         <ul>
 *             <li> if the segment is large enough, make a DEL call, and insertion calls when possible </li>
 *             <li> otherwise a single fat INS call</li>
 *         </ul>
 *     </li>
 * </ul>
 *
 * <p>
 *     Note that the above logic has a bias towards getting INV calls, because
 *     when the (large enough) reference segment appears both as-is and inverted,
 *     the above logic will emit at least an INV call,
 *     whereas the (inverted) duplication(s) could also be reported as an DUP call as well, but...
 * </p>
 */
@Override
List<VariantContext> extract(final VariantContext complexVC, final ReferenceMultiSparkSource reference) {

    final List<String> segments = SVUtils.getAttributeAsStringList(complexVC, CPX_SV_REF_SEGMENTS);
    if (segments.isEmpty()) return whenZeroSegments(complexVC, reference);

    final SimpleInterval refSegment = new SimpleInterval(segments.get(0));
    final List<String> altArrangement = SVUtils.getAttributeAsStringList(complexVC, CPX_EVENT_ALT_ARRANGEMENTS);
    final int altSeqLength = complexVC.getAttributeAsString(SEQ_ALT_HAPLOTYPE, "").length();

    final List<VariantContextBuilder> result = new ArrayList<>();

    final int asIsAppearanceIdx = altArrangement.indexOf("1");
    final int invertedAppearanceIdx = altArrangement.indexOf("-1");
    if (invertedAppearanceIdx != -1 && refSegment.size() > EVENT_SIZE_THRESHOLD) { // inversion call
        whenInversionIsWarranted(refSegment, invertedAppearanceIdx, altArrangement, reference, result);
    } else if (asIsAppearanceIdx != -1) { // no inverted appearance or appear inverted but not large enough, and in the mean time appear as-is, so no deletion
        whenNoDeletionIsAllowed(refSegment, asIsAppearanceIdx, altArrangement, altSeqLength, reference, result);
    } else { // no as-is appearance && (inverted appearance might present not not large enough)
        whenNoInvAndNoAsIsAppearance(refSegment, altSeqLength, reference, result);
    }

    final String sourceID = complexVC.getID();
    final List<String> evidenceContigs = SVUtils.getAttributeAsStringList(complexVC, CONTIG_NAMES);
    final List<String> mappingQualities = SVUtils.getAttributeAsStringList(complexVC, MAPPING_QUALITIES);
    final int maxAlignLength = complexVC.getAttributeAsInt(MAX_ALIGN_LENGTH, 0);
    return result.stream()
            .map(vc -> vc.attribute(CPX_EVENT_KEY, sourceID).attribute(CONTIG_NAMES, evidenceContigs)
                         .attribute(MAPPING_QUALITIES, mappingQualities)
                         .attribute(MAX_ALIGN_LENGTH, maxAlignLength).make())
            .collect(Collectors.toList());
}
 
Example 6
Source File: SegmentedCpxVariantSimpleVariantExtractor.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private AnnotatedInterval(final VariantContext vc) {
    sourceVC = vc;
    interval = new SimpleInterval( vc.getContig(), vc.getStart(), vc.getEnd());
    id = vc.getID();
    type = vc.getAttributeAsString(SVTYPE, "");
    svlen = vc.getAttributeAsInt(SVLEN, 0);
    alleles = vc.getAlleles();
}
 
Example 7
Source File: RMSMappingQuality.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *
 * @return the number of reads at the given site, calculated as InfoField {@link VCFConstants#DEPTH_KEY} minus the
 * format field {@link GATKVCFConstants#MIN_DP_FORMAT_KEY} or DP of each of the HomRef genotypes at that site
 * @throws UserException.BadInput if the {@link VCFConstants#DEPTH_KEY} is missing or if the calculated depth is <= 0
 */
@VisibleForTesting
private static int getNumOfReads(final VariantContext vc) {
    if(vc.hasAttribute(GATKVCFConstants.MAPPING_QUALITY_DEPTH_DEPRECATED)) {
        int mqDP = vc.getAttributeAsInt(GATKVCFConstants.MAPPING_QUALITY_DEPTH_DEPRECATED, 0);
        if (mqDP > 0) {
            return mqDP;
        }
    }

    //don't use the full depth because we don't calculate MQ for reference blocks
    //don't count spanning deletion calls towards number of reads
    int numOfReads = vc.getAttributeAsInt(VCFConstants.DEPTH_KEY, -1);
    if(vc.hasGenotypes()) {
        for(final Genotype gt : vc.getGenotypes()) {
           if(hasReferenceDepth(gt)) {
                //site-level DP contribution will come from MIN_DP for gVCF-called reference variants or DP for BP resolution
                if (gt.hasExtendedAttribute(GATKVCFConstants.MIN_DP_FORMAT_KEY)) {
                    numOfReads -= Integer.parseInt(gt.getExtendedAttribute(GATKVCFConstants.MIN_DP_FORMAT_KEY).toString());
                } else if (gt.hasDP()) {
                    numOfReads -= gt.getDP();
                }
            }
            else if(hasSpanningDeletionAllele(gt)) {
                //site-level DP contribution will come from MIN_DP for gVCF-called reference variants or DP for BP resolution
                if (gt.hasExtendedAttribute(GATKVCFConstants.MIN_DP_FORMAT_KEY)) {
                    numOfReads -= Integer.parseInt(gt.getExtendedAttribute(GATKVCFConstants.MIN_DP_FORMAT_KEY).toString());
                } else if (gt.hasDP()) {
                    numOfReads -= gt.getDP();
                }
            }
        }
    }
    if (numOfReads <= 0){
        numOfReads = -1;  //return -1 to result in a NaN
    }
    return numOfReads;
}
 
Example 8
Source File: SvDiscoverFromLocalAssemblyContigAlignmentsSpark.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean test(final VariantContext variantContext) {
    if ( !variantContext.hasAttribute(GATKSVVCFConstants.CONTIG_NAMES) )
        return true;

    final int alnLength = variantContext.getAttributeAsInt(attributeKey, 0);
    return alnLength >= threshold;
}
 
Example 9
Source File: AlleleCount.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public List<Object> getRelevantStates(ReferenceContext referenceContext, ReadsContext readsContext, FeatureContext featureContext, VariantContext comp, String compName, VariantContext eval, String evalName, String sampleName, String familyName) {
    if (eval != null) {
        int AC = 0; // by default, the site is considered monomorphic

        try {
            if ( eval.isBiallelic() ) {
                if ( eval.hasAttribute(GATKVCFConstants.MLE_ALLELE_COUNT_KEY) ) {
                    // the MLEAC is allowed to be larger than the AN (e.g. in the case of all PLs being 0, the GT is ./. but the exact model may arbitrarily choose an AC>1)
                    AC = Math.min(eval.getAttributeAsInt(GATKVCFConstants.MLE_ALLELE_COUNT_KEY, 0), nchrom);
                } else if ( eval.hasAttribute(VCFConstants.ALLELE_COUNT_KEY) ) {
                    AC = eval.getAttributeAsInt(VCFConstants.ALLELE_COUNT_KEY, 0);
                }
            }
        } catch ( ClassCastException e ) {
            // protect ourselves from bad inputs
            // TODO -- fully decode VC
        }

        if ( AC == 0 && eval.isVariant() ) {
            // fall back to the direct calculation
            for (Allele allele : eval.getAlternateAlleles())
                AC = Math.max(AC, eval.getCalledChrCount(allele));
        }

        // make sure that the AC isn't invalid
        if ( AC > nchrom )
            throw new UserException(String.format("The AC value (%d) at position %s:%d " +
                    "is larger than the number of chromosomes over all samples (%d)", AC,
                    eval.getContig(), eval.getStart(), nchrom));

        return Collections.singletonList((Object) AC);
    } else {
        return Collections.emptyList();
    }
}
 
Example 10
Source File: NRatioFilter.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean isArtifact(final VariantContext vc, final Mutect2FilteringEngine filteringEngine) {
    final int[] ADs = filteringEngine.sumADsOverSamples(vc, true, true);
    final int altCount = (int) MathUtils.sum(ADs) - ADs[0];

    // if there is no NCount annotation or the altCount is 0, don't apply the filter
    if (altCount == 0 ) {
        return false;
    }

    final int NCount = vc.getAttributeAsInt(GATKVCFConstants.N_COUNT_KEY,0);

    return (double) NCount / altCount >= maxNRatio;
}
 
Example 11
Source File: SegmentedCpxVariantSimpleVariantExtractor.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
List<VariantContext> extract(final VariantContext complexVC, final ReferenceMultiSparkSource reference) {

    final List<SimpleInterval> refSegments =
            SVUtils.getAttributeAsStringList(complexVC, CPX_SV_REF_SEGMENTS).stream()
                    .map(SimpleInterval::new)
                    .collect(Collectors.toList());

    final List<String> altArrangement = SVUtils.getAttributeAsStringList(complexVC, CPX_EVENT_ALT_ARRANGEMENTS);

    final Tuple3<Set<SimpleInterval>, Set<Integer>, List<Integer>> missingAndPresentAndInvertedSegments = getMissingAndPresentAndInvertedSegments(refSegments, altArrangement);
    final Set<SimpleInterval> missingSegments = missingAndPresentAndInvertedSegments._1();
    final Set<Integer> presentSegments = missingAndPresentAndInvertedSegments._2();
    final List<Integer> invertedSegments = missingAndPresentAndInvertedSegments._3();

    final List<VariantContextBuilder> result = new ArrayList<>();

    // if affected ref sequence found as is (trusting the aligner), then only output front and/or rear insertions
    final int idx = findAllSegments(altArrangement, refSegments.size());
    if ( idx >= 0 ) {
        whenAllSegmentsAppearAsIs(complexVC, reference, refSegments, altArrangement, result, idx);
    } else {

        // inversions
        if (!invertedSegments.isEmpty()) {
            extractInversions(reference, refSegments, presentSegments, invertedSegments, result);
        }

        // deletions
        if (!missingSegments.isEmpty()) {
            extractDeletions(reference, missingSegments, result);
        }

        // head and tail insertions only
        extractFrontAndRearInsertions(complexVC, refSegments, altArrangement, reference, result);
    }

    final String sourceID = complexVC.getID();
    final List<String> evidenceContigs = SVUtils.getAttributeAsStringList(complexVC, CONTIG_NAMES);
    final List<String> mappingQualities = SVUtils.getAttributeAsStringList(complexVC, MAPPING_QUALITIES);
    final int maxAlignLength = complexVC.getAttributeAsInt(MAX_ALIGN_LENGTH, 0);

    return result.stream()
            .map(vc -> vc.attribute(CPX_EVENT_KEY, sourceID).attribute(CONTIG_NAMES, evidenceContigs)
                         .attribute(MAPPING_QUALITIES, mappingQualities)
                         .attribute(MAX_ALIGN_LENGTH, maxAlignLength).make())
            .collect(Collectors.toList());
}
 
Example 12
Source File: ClusteredEventsFilter.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public boolean isArtifact(final VariantContext vc, final Mutect2FilteringEngine filteringEngine) {
    final Integer eventCount = vc.getAttributeAsInt(GATKVCFConstants.EVENT_COUNT_IN_HAPLOTYPE_KEY, -1);
    return eventCount > maxEventsInRegion;
}
 
Example 13
Source File: Degeneracy.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public List<Object> getRelevantStates(ReferenceContext referenceContext, ReadsContext readsContext, FeatureContext featureContext, VariantContext comp, String compName, VariantContext eval, String evalName, String sampleName, String FamilyName) {
    ArrayList<Object> relevantStates = new ArrayList<Object>();

    relevantStates.add("all");

    if (eval != null && eval.isVariant()) {
        String type = null;
        String aa = null;
        Integer frame = null;

        if (eval.hasAttribute("refseq.functionalClass")) {
            aa = eval.getAttributeAsString("refseq.variantAA", null);
            frame = eval.getAttributeAsInt("refseq.frame", 0);
        } else if (eval.hasAttribute("refseq.functionalClass_1")) {
            int annotationId = 1;
            String key;

            do {
                key = String.format("refseq.functionalClass_%d", annotationId);

                String newtype = eval.getAttributeAsString(key, null);

                if ( newtype != null &&
                        ( type == null ||
                                ( type.equals("silent") && !newtype.equals("silent") ) ||
                                ( type.equals("missense") && newtype.equals("nonsense") ) )
                        ) {
                    type = newtype;

                    String aakey = String.format("refseq.variantAA_%d", annotationId);
                    aa = eval.getAttributeAsString(aakey, null);

                    if (aa != null) {
                        String framekey = String.format("refseq.frame_%d", annotationId);

                        if (eval.hasAttribute(framekey)) {
                            frame = eval.getAttributeAsInt(framekey, 0);
                        }
                    }
                }

                annotationId++;
            } while (eval.hasAttribute(key));
        }

        if (aa != null && degeneracies.containsKey(aa) && frame != null) {
            relevantStates.add(degeneracies.get(aa).get(frame));
        }
    }

    return relevantStates;
}
 
Example 14
Source File: CombineGenotypingArrayVcfs.java    From picard with MIT License 4 votes vote down vote up
/**
 * Merges multiple VariantContexts all for the same locus into a single hybrid.
 *
 * @param variantContexts           list of VCs
 * @return new VariantContext       representing the merge of variantContexts
 */
public static VariantContext merge(final List<VariantContext> variantContexts) {
    if ( variantContexts == null || variantContexts.isEmpty() )
        return null;

    // establish the baseline info from the first VC
    final VariantContext first = variantContexts.get(0);
    final String name = first.getSource();

    final Set<String> filters = new HashSet<>();

    int depth = 0;
    double log10PError = CommonInfo.NO_LOG10_PERROR;
    boolean anyVCHadFiltersApplied = false;
    GenotypesContext genotypes = GenotypesContext.create();

    // Go through all the VCs, verify that the loci and ID and other attributes agree.
    final Map<String, Object> firstAttributes = first.getAttributes();
    for (final VariantContext vc : variantContexts ) {
        if ((vc.getStart() != first.getStart()) || (!vc.getContig().equals(first.getContig()))) {
            throw new PicardException("Mismatch in loci among input VCFs");
        }
        if (!vc.getID().equals(first.getID())) {
            throw new PicardException("Mismatch in ID field among input VCFs");
        }
        if (!vc.getReference().equals(first.getReference())) {
            throw new PicardException("Mismatch in REF allele among input VCFs");
        }
        checkThatAllelesMatch(vc, first);

        genotypes.addAll(vc.getGenotypes());

        // We always take the QUAL of the first VC with a non-MISSING qual for the combined value
        if ( log10PError == CommonInfo.NO_LOG10_PERROR )
            log10PError =  vc.getLog10PError();

        filters.addAll(vc.getFilters());
        anyVCHadFiltersApplied |= vc.filtersWereApplied();

        // add attributes
        // special case DP (add it up)
        if (vc.hasAttribute(VCFConstants.DEPTH_KEY))
            depth += vc.getAttributeAsInt(VCFConstants.DEPTH_KEY, 0);

        // Go through all attributes - if any differ (other than AC, AF, or AN) that's an error.  (recalc AC, AF, and AN later)
        for (final Map.Entry<String, Object> p : vc.getAttributes().entrySet()) {
            final String key = p.getKey();
            if ((!key.equals("AC")) && (!key.equals("AF")) && (!key.equals("AN")) && (!key.equals("devX_AB")) && (!key.equals("devY_AB"))) {
                final Object value = p.getValue();
                final Object extantValue = firstAttributes.get(key);
                if (extantValue == null) {
                    // attribute in one VCF but not another.  Die!
                    throw new PicardException("Attribute '" + key + "' not found in all VCFs");
                }
                else if (!extantValue.equals(value)) {
                    // Attribute disagrees in value between one VCF Die! (if not AC, AF, nor AN, skipped above)
                    throw new PicardException("Values for attribute '" + key + "' disagrees among input VCFs");
                }
            }
        }
    }

    if (depth > 0)
        firstAttributes.put(VCFConstants.DEPTH_KEY, String.valueOf(depth));

    final VariantContextBuilder builder = new VariantContextBuilder().source(name).id(first.getID());
    builder.loc(first.getContig(), first.getStart(), first.getEnd());
    builder.alleles(first.getAlleles());
    builder.genotypes(genotypes);

    builder.attributes(new TreeMap<>(firstAttributes));
    // AC, AF, and AN are recalculated here
    VariantContextUtils.calculateChromosomeCounts(builder, false);

    builder.log10PError(log10PError);
    if ( anyVCHadFiltersApplied ) {
        builder.filters(filters.isEmpty() ? filters : new TreeSet<>(filters));
    }

    return builder.make();
}
 
Example 15
Source File: EvaluateCopyNumberTriStateCallsIntegrationTest.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void checkOutputCallsWithoutOverlappingTruthConcordance(final File truthFile, final File callsFile, final File targetsFile, final File vcfOutput, final EvaluationFiltersArgumentCollection filteringOptions) {
    final List<VariantContext> truthVariants = readVCFFile(truthFile);
    final List<VariantContext> outputVariants = readVCFFile(vcfOutput);
    final List<VariantContext> callsVariants = readVCFFile(callsFile);
    final Set<String> outputSamples = outputVariants.get(0).getSampleNames();
    final TargetCollection<Target> targets = TargetArgumentCollection.readTargetCollection(targetsFile);

    for (final VariantContext call : callsVariants) {
        final List<Target> overlappingTargets = targets.targets(call);
        final List<VariantContext> overlappingOutput = outputVariants.stream()
                .filter(vc -> new SimpleInterval(vc).overlaps(call))
                .collect(Collectors.toList());
        final List<VariantContext> overlappingTruth = truthVariants.stream()
                .filter(vc -> new SimpleInterval(vc).overlaps(call))
                .collect(Collectors.toList());

        if (!overlappingTruth.isEmpty()) {
            continue;
        }

        @SuppressWarnings("all")
        final Optional<VariantContext> matchingOutputOptional = overlappingOutput.stream()
                .filter(vc -> new SimpleInterval(call).equals(new SimpleInterval(vc)))
                .findAny();
        final VariantContext matchingOutput = matchingOutputOptional.get();
        final int sampleCallsCount[] = new int[CopyNumberTriStateAllele.ALL_ALLELES.size()];
        for (final String sample : outputSamples) {
            final Genotype outputGenotype = matchingOutput.getGenotype(sample);
            final Genotype callGenotype = call.getGenotype(sample);
            final Allele expectedCall = callGenotype.getAllele(0).isCalled() ? CopyNumberTriStateAllele.valueOf(callGenotype.getAllele(0)) : null;
            final Allele actualCall = outputGenotype.getAllele(0).isCalled() ? CopyNumberTriStateAllele.valueOf(outputGenotype.getAllele(0)) : null;
            Assert.assertEquals(expectedCall, actualCall);
            final boolean expectedDiscovered = XHMMSegmentGenotyper.DISCOVERY_TRUE.equals(GATKProtectedVariantContextUtils.getAttributeAsString(callGenotype, XHMMSegmentGenotyper.DISCOVERY_KEY, "N"));
            final boolean actualDiscovered = XHMMSegmentGenotyper.DISCOVERY_TRUE.equals(GATKProtectedVariantContextUtils.getAttributeAsString(callGenotype, XHMMSegmentGenotyper.DISCOVERY_KEY, "N"));
            Assert.assertEquals(actualDiscovered, expectedDiscovered);
            final int[] expectedCounts = new int[CopyNumberTriStateAllele.ALL_ALLELES.size()];
            if (expectedCall.isCalled() && actualDiscovered) {
                expectedCounts[CopyNumberTriStateAllele.valueOf(expectedCall).index()]++;
            }
            if (outputGenotype.hasExtendedAttribute(VariantEvaluationContext.CALLED_ALLELE_COUNTS_KEY)) {
                Assert.assertEquals(GATKProtectedVariantContextUtils.getAttributeAsIntArray(outputGenotype, VariantEvaluationContext.CALLED_ALLELE_COUNTS_KEY, () -> new int[CopyNumberTriStateAllele.ALL_ALLELES.size()], 0), expectedCounts);
            }
            if (outputGenotype.hasExtendedAttribute(VariantEvaluationContext.CALLED_SEGMENTS_COUNT_KEY)) {
                Assert.assertEquals(GATKProtectedVariantContextUtils.getAttributeAsInt(outputGenotype, VariantEvaluationContext.CALLED_SEGMENTS_COUNT_KEY, -1), expectedCall.isCalled() && actualDiscovered ? 1 : 0);
            }
            final String evalClass = GATKProtectedVariantContextUtils.getAttributeAsString(outputGenotype, VariantEvaluationContext.EVALUATION_CLASS_KEY, null);
            Assert.assertEquals(evalClass, expectedCall.isCalled() && actualDiscovered && expectedCall.isNonReference() ? EvaluationClass.UNKNOWN_POSITIVE.acronym : null);
            if (expectedCall.isCalled()) {
                sampleCallsCount[CopyNumberTriStateAllele.valueOf(expectedCall).index()]++;
            }
            Assert.assertEquals(GATKProtectedVariantContextUtils.getAttributeAsDouble(outputGenotype, VariantEvaluationContext.CALL_QUALITY_KEY, 0.0), callGQ(callGenotype), 0.01);
        }
        final int expectedAN = (int) MathUtils.sum(sampleCallsCount);
        final int observedAN = matchingOutput.getAttributeAsInt(VariantEvaluationContext.CALLS_ALLELE_NUMBER_KEY, -1);
        Assert.assertEquals(observedAN, expectedAN);
        final double[] expectedAF = Arrays.copyOfRange(IntStream.of(sampleCallsCount).mapToDouble(i -> expectedAN > 0 ? i / (double) expectedAN : 0.0)
                .toArray(), 1, sampleCallsCount.length);
        final double[] observedAF = GATKProtectedVariantContextUtils.getAttributeAsDoubleArray(matchingOutput, VariantEvaluationContext.CALLS_ALLELE_FREQUENCY_KEY, () -> new double[matchingOutput.getAlternateAlleles().size()], 0.0);
        Assert.assertNotNull(observedAF);
        assertEquals(observedAF, expectedAF, 0.01);
        Assert.assertEquals(matchingOutput.getAttributeAsInt(VariantEvaluationContext.TRUTH_ALLELE_NUMBER_KEY, -1), 0);
    }
}
 
Example 16
Source File: RecoveredVariantFactory.java    From hmftools with GNU General Public License v3.0 4 votes vote down vote up
private int uncertainty(@NotNull final VariantContext context) {
    final int homlen = 2 * context.getAttributeAsInt("HOMLEN", 0);
    final int cipos = cipos(context);
    return Math.max(homlen, cipos);
}
 
Example 17
Source File: SnpEffSummaryFactory.java    From hmftools with GNU General Public License v3.0 4 votes vote down vote up
@NotNull
public SnpEffSummary fromAnnotations(@NotNull final VariantContext context) {
    final boolean phasedInframeIndel = context.isIndel() && context.getAttributeAsInt(SageMetaData.PHASED_INFRAME_INDEL, 0) > 0;
    final List<SnpEffAnnotation> allAnnotations = SnpEffAnnotationFactory.fromContext(context);
    return fromAnnotations(phasedInframeIndel, allAnnotations);
}