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

The following examples show how to use htsjdk.variant.variantcontext.VariantContext#isVariant() . 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: StrandBiasTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
//template method for calculating strand bias annotations using the three different methods
public Map<String, Object> annotate(final ReferenceContext ref,
                                    final VariantContext vc,
                                    final AlleleLikelihoods<GATKRead, Allele> likelihoods) {
    Utils.nonNull(vc);
    if ( !vc.isVariant() ) {
        return Collections.emptyMap();
    }

    // if the genotype and strand bias are provided, calculate the annotation from the Genotype (GT) field
    if ( vc.hasGenotypes() ) {
        for (final Genotype g : vc.getGenotypes()) {
            if (g.hasAnyAttribute(GATKVCFConstants.STRAND_BIAS_BY_SAMPLE_KEY)) {
                return calculateAnnotationFromGTfield(vc.getGenotypes());
            }
        }
    }

    if (likelihoods != null) {
        return calculateAnnotationFromLikelihoods(likelihoods, vc);
    }
    return Collections.emptyMap();
}
 
Example 2
Source File: ExcessHet.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Map<String, Object> annotate(final ReferenceContext ref,
                                    final VariantContext vc,
                                    final AlleleLikelihoods<GATKRead, Allele> likelihoods) {
    GenotypesContext genotypes = getFounderGenotypes(vc);
    if (genotypes == null || !vc.isVariant()) {
        return Collections.emptyMap();
    }
    final Pair<Integer, Double> sampleCountEH = calculateEH(vc, genotypes);
    final int sampleCount = sampleCountEH.getLeft();
    final double eh =  sampleCountEH.getRight();

    if (sampleCount < 1) {
        return Collections.emptyMap();
    }
    return Collections.singletonMap(getKeyNames().get(0), (Object) String.format("%.4f", eh));
}
 
Example 3
Source File: MappingQualityZero.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Map<String, Object> annotate(final ReferenceContext ref,
                                    final VariantContext vc,
                                    final AlleleLikelihoods<GATKRead, Allele> likelihoods) {
    Utils.nonNull(vc);
    if (!vc.isVariant() || likelihoods == null){
        return Collections.emptyMap();
    }
    //NOTE: unlike other annotations, this one returns 0 if likelihoods are empty
    final long mq0 = IntStream.range(0, likelihoods.numberOfSamples()).boxed()
            .flatMap(s -> likelihoods.sampleEvidence(s).stream())
            .filter(r -> r.getMappingQuality() == 0)
            .count();

    return Collections.singletonMap(getKeyNames().get(0), formattedValue(mq0));
}
 
Example 4
Source File: InbreedingCoeff.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Map<String, Object> annotate(final ReferenceContext ref,
                                    final VariantContext vc,
                                    final AlleleLikelihoods<GATKRead, Allele> likelihoods) {
    Utils.nonNull(vc);
    final GenotypesContext genotypes = getFounderGenotypes(vc);
    if (genotypes == null || genotypes.size() < MIN_SAMPLES || !vc.isVariant()) {
        logger.warn("InbreedingCoeff will not be calculated; at least " + MIN_SAMPLES + " samples must have called genotypes");
        return Collections.emptyMap();
    }
    final Pair<Integer, Double> sampleCountCoeff = calculateIC(vc, genotypes);
    final int sampleCount = sampleCountCoeff.getLeft();
    final double F = sampleCountCoeff.getRight();
    if (sampleCount < MIN_SAMPLES) {
        logger.warn("InbreedingCoeff will not be calculated for at least one position; at least " + MIN_SAMPLES + " samples must have called genotypes");
        return Collections.emptyMap();
    }
    return Collections.singletonMap(getKeyNames().get(0), String.format("%.4f", F));
}
 
Example 5
Source File: SnpEffPositionModifier.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public List<Object> getRelevantStates(
		final ReferenceContext referenceContext,
		final ReadsContext readsContext,
		final FeatureContext featureContext,
		final VariantContext comp,
		final String compName,
		final VariantContext eval,
		final String evalName,
		final String sampleName,
		final String FamilyName)
{
	final List<Object> relevantStates = new ArrayList<>();
	if (eval != null && eval.isVariant() && eval.hasAttribute(EFFECT_KEY)) {
		final SnpEffUtil.EffectType effectType = SnpEffUtil.EffectType.valueOf(
				eval.getAttribute(EFFECT_KEY).toString());

		if (SnpEffUtil.isSubTypeOf(effectType, SnpEffUtil.EffectType.EXON))        relevantStates.add(PositionModifier.GENE.name());
		if (SnpEffUtil.isSubTypeOf(effectType, SnpEffUtil.EffectType.CDS))         relevantStates.add(PositionModifier.CODING_REGION.name());
		if (SnpEffUtil.isSubTypeOf(effectType, SnpEffUtil.EffectType.STOP_GAINED)) relevantStates.add(PositionModifier.STOP_GAINED.name());
		if (SnpEffUtil.isSubTypeOf(effectType, SnpEffUtil.EffectType.STOP_LOST))   relevantStates.add(PositionModifier.STOP_LOST.name());

		if (SnpEffUtil.isSubTypeOf(effectType, SnpEffUtil.EffectType.SPLICE_SITE_ACCEPTOR) ||
			SnpEffUtil.isSubTypeOf(effectType, SnpEffUtil.EffectType.SPLICE_SITE_DONOR))
				relevantStates.add(PositionModifier.SPLICE_SITE.name());
	}

	return relevantStates;
}
 
Example 6
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 7
Source File: ReferenceConfidenceModelUnitTest.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void checkReferenceModelResult(final RefConfData data, final List<VariantContext> contexts, final List<Integer> expectedDPs, final List<VariantContext> calls) {
    Assert.assertNotNull(contexts);

    final SimpleInterval loc = data.getActiveRegion().getExtendedSpan();
    final List<Boolean> seenBP = new ArrayList<>(Collections.nCopies(data.getActiveRegion().getSpan().size(), false));

    for ( int i = 0; i < loc.size(); i++ ) {
        final GenomeLoc curPos = parser.createGenomeLoc(loc.getContig(), loc.getStart() + i);
        final VariantContext call = model.getOverlappingVariantContext(curPos, calls);
        final VariantContext refModel = model.getOverlappingVariantContext(curPos, contexts);

        if ( ! data.getActiveRegion().getSpan().contains(curPos) ) {
            // part of the extended interval, but not the full interval
            Assert.assertNull(refModel);
            continue;
        }

        if ( call != null ) {
            if (call.isVariant() && refModel.getType() ==  VariantContext.Type.SYMBOLIC ) {
                //Assert.assertEquals(refModel, call, "Should have found call " + call + " but found " + refModel + " instead");
                Assert.assertTrue(call.getReference().length() > 1); // must be a deletion.
                Assert.assertTrue(call.getStart() < refModel.getStart()); // the deletion must not start at the same position
                Assert.assertEquals(call.getReference().getBaseString().substring(refModel.getStart() - call.getStart(),
                            refModel.getStart() - call.getStart() + 1), refModel.getReference().getBaseString(), "" + data.getRefHap()); // the reference must be the same.
                Assert.assertTrue(refModel.getGenotype(0).getGQ() <= 0); // No confidence in the reference hom-ref call across the deletion
                Assert.assertEquals(refModel.getAlleles().size(),2); // the reference and the lonelly <NON_REF>
                Assert.assertEquals(refModel.getAlleles().get(1), GATKVCFConstants.NON_REF_SYMBOLIC_ALLELE);
            } else {
                Assert.assertEquals(refModel, call, "Should have found call " + call + " but found " + refModel + " instead");
            }

        } else {
            final int expectedDP = expectedDPs.get(curPos.getStart() - data.getActiveRegion().getSpan().getStart());
            Assert.assertEquals(refModel.getStart(), loc.getStart() + i);
            Assert.assertEquals(refModel.getEnd(), loc.getStart() + i);
            Assert.assertFalse(refModel.hasLog10PError());
            Assert.assertEquals(refModel.getAlternateAlleles().size(), 1);
            Assert.assertEquals(refModel.getAlternateAllele(0), GATKVCFConstants.NON_REF_SYMBOLIC_ALLELE);
            Assert.assertTrue(refModel.hasGenotype(sample));

            final Genotype g = refModel.getGenotype(sample);
            Assert.assertTrue(g.hasAD());
            Assert.assertTrue(g.hasDP());
            Assert.assertEquals(g.getDP(), expectedDP);
            Assert.assertTrue(g.hasGQ());
            Assert.assertTrue(g.hasPL());
        }

        final VariantContext vc = call == null ? refModel : call;
        if ( curPos.getStart() == vc.getStart() ) {
            for ( int pos = vc.getStart(); pos <= vc.getEnd(); pos++ ) {
                final int j = pos - data.getActiveRegion().getSpan().getStart();
                Assert.assertFalse(seenBP.get(j));
                seenBP.set(j, true);
            }
        }
    }

    for ( int i = 0; i < seenBP.size(); i++ ) {
        Assert.assertEquals((boolean)seenBP.get(i), true);
    }
}
 
Example 8
Source File: CallingMetricAccumulator.java    From picard with MIT License 4 votes vote down vote up
/** Returns true if the variant is --NOT-- interesting enough to be included in metrics calculations. */
static private boolean isVariantExcluded(final VariantContext vc) {

    // If the entire record is not a variant, exclude it
    return !vc.isVariant() || vc.getGenotypes().stream().allMatch(Genotype::isHomRef);
}
 
Example 9
Source File: VariantDataManager.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private boolean isValidVariant( final VariantContext evalVC, final VariantContext trainVC, final boolean TRUST_ALL_POLYMORPHIC) {
    return trainVC != null && trainVC.isNotFiltered() && trainVC.isVariant() && checkVariationClass( evalVC, trainVC ) &&
            (TRUST_ALL_POLYMORPHIC || !trainVC.hasGenotypes() || trainVC.isPolymorphicInSamples());
}
 
Example 10
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;
}