use of htsjdk.variant.variantcontext.VariantContextBuilder in project gatk by broadinstitute.
the class SelectVariants method apply.
@Override
public void apply(VariantContext vc, ReadsContext readsContext, ReferenceContext ref, FeatureContext featureContext) {
if (fullyDecode) {
vc = vc.fullyDecode(getHeaderForVariants(), lenientVCFProcessing);
}
if (mendelianViolations && invertLogic((mv.countFamilyViolations(sampleDB, samples, vc) == 0), invertMendelianViolations)) {
return;
}
if (discordanceOnly && !isDiscordant(vc, featureContext.getValues(discordanceTrack))) {
return;
}
if (concordanceOnly && !isConcordant(vc, featureContext.getValues(concordanceTrack))) {
return;
}
if (alleleRestriction.equals(NumberAlleleRestriction.BIALLELIC) && !vc.isBiallelic()) {
return;
}
if (alleleRestriction.equals(NumberAlleleRestriction.MULTIALLELIC) && vc.isBiallelic()) {
return;
}
if (containsIndelLargerOrSmallerThan(vc, maxIndelSize, minIndelSize)) {
return;
}
if (considerFilteredGenotypes()) {
final int numFilteredSamples = numFilteredGenotypes(vc);
final double fractionFilteredGenotypes = samples.isEmpty() ? 0.0 : numFilteredSamples / samples.size();
if (numFilteredSamples > maxFilteredGenotypes || numFilteredSamples < minFilteredGenotypes || fractionFilteredGenotypes > maxFractionFilteredGenotypes || fractionFilteredGenotypes < minFractionFilteredGenotypes)
return;
}
if (considerNoCallGenotypes()) {
final int numNoCallSamples = numNoCallGenotypes(vc);
final double fractionNoCallGenotypes = samples.isEmpty() ? 0.0 : ((double) numNoCallSamples) / samples.size();
if (numNoCallSamples > maxNOCALLnumber || fractionNoCallGenotypes > maxNOCALLfraction)
return;
}
// Initialize the cache of PL index to a list of alleles for each ploidy.
initalizeAlleleAnyploidIndicesCache(vc);
final VariantContext sub = subsetRecord(vc, preserveAlleles, removeUnusedAlternates);
final VariantContextBuilder builder = new VariantContextBuilder(vc);
if (setFilteredGenotypesToNocall) {
GATKVariantContextUtils.setFilteredGenotypeToNocall(builder, sub, setFilteredGenotypesToNocall, this::getGenotypeFilters);
}
final VariantContext filteredGenotypeToNocall = setFilteredGenotypesToNocall ? builder.make() : sub;
// Not excluding non-variants or subsetted polymorphic variants AND including filtered loci or subsetted variant is not filtered
if ((!XLnonVariants || filteredGenotypeToNocall.isPolymorphicInSamples()) && (!XLfiltered || !filteredGenotypeToNocall.isFiltered())) {
// Write the subsetted variant if it matches all of the expressions
boolean failedJexlMatch = false;
try {
for (VariantContextUtils.JexlVCMatchExp jexl : jexls) {
if (invertLogic(!VariantContextUtils.match(filteredGenotypeToNocall, jexl), invertSelect)) {
failedJexlMatch = true;
break;
}
}
} catch (IllegalArgumentException e) {
// expression detected...")
throw new UserException(e.getMessage() + "\nSee https://www.broadinstitute.org/gatk/guide/article?id=1255 for documentation on using JEXL in GATK", e);
}
if (!failedJexlMatch && (!selectRandomFraction || Utils.getRandomGenerator().nextDouble() < fractionRandom)) {
vcfWriter.add(filteredGenotypeToNocall);
}
}
}
use of htsjdk.variant.variantcontext.VariantContextBuilder 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);
}
use of htsjdk.variant.variantcontext.VariantContextBuilder in project gatk by broadinstitute.
the class AnnotateVcfWithBamDepth method apply.
@Override
public void apply(final VariantContext vc, final ReadsContext readsContext, final ReferenceContext refContext, final FeatureContext fc) {
final MutableInt depth = new MutableInt(0);
for (final GATKRead read : readsContext) {
if (!read.failsVendorQualityCheck() && !read.isDuplicate() && !read.isUnmapped() && read.getEnd() > read.getStart() && new SimpleInterval(read).contains(vc)) {
depth.increment();
}
}
vcfWriter.add(new VariantContextBuilder(vc).attribute(POOLED_BAM_DEPTH_ANNOTATION_NAME, depth.intValue()).make());
}
use of htsjdk.variant.variantcontext.VariantContextBuilder in project gatk by broadinstitute.
the class AnnotateVcfWithExpectedAlleleFraction method apply.
@Override
public void apply(final VariantContext vc, final ReadsContext readsContext, final ReferenceContext refContext, final FeatureContext fc) {
final double[] weights = vc.getGenotypes().stream().mapToDouble(g -> weight(g)).toArray();
final double expectedAlleleFraction = MathUtils.sum(MathArrays.ebeMultiply(weights, mixingFractionsInSampleOrder));
vcfWriter.add(new VariantContextBuilder(vc).attribute(EXPECTED_ALLELE_FRACTION_NAME, expectedAlleleFraction).make());
}
use of htsjdk.variant.variantcontext.VariantContextBuilder in project gatk by broadinstitute.
the class CreateSomaticPanelOfNormals method processVariantsAtSamePosition.
//TODO: this is the old Mutect behavior that just looks for multiple hits
//TODO: we should refine this
private static void processVariantsAtSamePosition(final List<VariantContext> variants, final VariantContextWriter writer) {
if (variants.size() > 1) {
final VariantContext mergedVc = AssemblyBasedCallerUtils.makeMergedVariantContext(variants);
final VariantContext outputVc = new VariantContextBuilder().source(mergedVc.getSource()).loc(mergedVc.getContig(), mergedVc.getStart(), mergedVc.getEnd()).alleles(mergedVc.getAlleles()).make();
writer.add(outputVc);
}
}
Aggregations