Java Code Examples for htsjdk.variant.variantcontext.VariantContextBuilder#rmAttribute()
The following examples show how to use
htsjdk.variant.variantcontext.VariantContextBuilder#rmAttribute() .
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: SelectVariants.java From gatk with BSD 3-Clause "New" or "Revised" License | 6 votes |
private VariantContext buildVariantContextWithDroppedAnnotationsRemoved(final VariantContext vc) { if (infoAnnotationsToDrop.isEmpty() && genotypeAnnotationsToDrop.isEmpty()) { return vc; } final VariantContextBuilder rmAnnotationsBuilder = new VariantContextBuilder(vc); for (String infoField : infoAnnotationsToDrop) { rmAnnotationsBuilder.rmAttribute(infoField); } if (!genotypeAnnotationsToDrop.isEmpty()) { final ArrayList<Genotype> genotypesToWrite = new ArrayList<>(); for (Genotype genotype : vc.getGenotypes()) { final GenotypeBuilder genotypeBuilder = new GenotypeBuilder(genotype).noAttributes(); final Map<String, Object> attributes = new HashMap<>(genotype.getExtendedAttributes()); for (String genotypeAnnotation : genotypeAnnotationsToDrop) { attributes.remove(genotypeAnnotation); } genotypeBuilder.attributes(attributes); genotypesToWrite.add(genotypeBuilder.make()); } rmAnnotationsBuilder.genotypes(GenotypesContext.create(genotypesToWrite)); } return rmAnnotationsBuilder.make(); }
Example 2
Source File: LiftoverUtils.java From picard with MIT License | 4 votes |
/** * This will take an input VariantContext and lift to the provided interval. * If this interval is in the opposite orientation, all alleles and genotypes will be reverse complemented * and indels will be left-aligned. Currently this is only able to invert biallelic indels, and null will be * returned for any unsupported VC. * * @param source The VariantContext to lift * @param target The target interval * @param refSeq The reference sequence, which should match the target interval * @param writeOriginalPosition If true, INFO field annotations will be added to store the original position and contig * @param writeOriginalAlleles If true, an INFO field annotation will be added to store the original alleles in order. This can be useful in the case of more complex liftovers, like reverse complements, left aligned indels or swapped REF/ALT * @return The lifted VariantContext. This will be null if the input VariantContext could not be lifted. */ public static VariantContext liftVariant(final VariantContext source, final Interval target, final ReferenceSequence refSeq, final boolean writeOriginalPosition, final boolean writeOriginalAlleles) { if (target == null) { return null; } final VariantContextBuilder builder; if (target.isNegativeStrand()) { builder = reverseComplementVariantContext(source, target, refSeq); } else { builder = liftSimpleVariantContext(source, target); } if (builder == null) { return null; } builder.filters(source.getFilters()); builder.log10PError(source.getLog10PError()); // If any of the source alleles is symbolic, do not populate the END tag (protecting things like <NON_REF> from // getting screwed up in reverse-complemented variants if (source.hasAttribute(VCFConstants.END_KEY) && builder.getAlleles().stream().noneMatch(Allele::isSymbolic)) { builder.attribute(VCFConstants.END_KEY, (int) builder.getStop()); } else { builder.rmAttribute(VCFConstants.END_KEY); } // make sure that the variant isn't mistakenly set as "SwappedAlleles" builder.rmAttribute(SWAPPED_ALLELES); if (target.isNegativeStrand()) { builder.attribute(REV_COMPED_ALLELES, true); } else { // make sure that the variant isn't mistakenly set as "ReverseComplementedAlleles" (from a previous liftover, say) builder.rmAttribute(REV_COMPED_ALLELES); } builder.id(source.getID()); if (writeOriginalPosition) { builder.attribute(LiftoverVcf.ORIGINAL_CONTIG, source.getContig()); builder.attribute(LiftoverVcf.ORIGINAL_START, source.getStart()); } if (writeOriginalAlleles && !source.getAlleles().equals(builder.getAlleles())) { builder.attribute(LiftoverVcf.ORIGINAL_ALLELES, allelesToStringList(source.getAlleles())); } return builder.make(); }
Example 3
Source File: LiftoverUtils.java From picard with MIT License | 4 votes |
/** * method to swap the reference and alt alleles of a bi-allelic, SNP * * @param vc the {@link VariantContext} (bi-allelic SNP) that needs to have it's REF and ALT alleles swapped. * @param annotationsToReverse INFO field annotations (of double value) that will be reversed (x->1-x) * @param annotationsToDrop INFO field annotations that will be dropped from the result since they are invalid when REF and ALT are swapped * @return a new {@link VariantContext} with alleles swapped, INFO fields modified and in the genotypes, GT, AD and PL corrected appropriately */ public static VariantContext swapRefAlt(final VariantContext vc, final Collection<String> annotationsToReverse, final Collection<String> annotationsToDrop) { if (!vc.isBiallelic() || !vc.isSNP()) { throw new IllegalArgumentException("swapRefAlt can only process biallelic, SNPS, found " + vc.toString()); } final VariantContextBuilder swappedBuilder = new VariantContextBuilder(vc); swappedBuilder.attribute(SWAPPED_ALLELES, true); // Use getBaseString() (rather than the Allele itself) in order to create new Alleles with swapped // reference and non-variant attributes swappedBuilder.alleles(Arrays.asList(vc.getAlleles().get(1).getBaseString(), vc.getAlleles().get(0).getBaseString())); final Map<Allele, Allele> alleleMap = new HashMap<>(); // A mapping from the old allele to the new allele, to be used when fixing the genotypes alleleMap.put(vc.getAlleles().get(0), swappedBuilder.getAlleles().get(1)); alleleMap.put(vc.getAlleles().get(1), swappedBuilder.getAlleles().get(0)); final GenotypesContext swappedGenotypes = GenotypesContext.create(vc.getGenotypes().size()); for (final Genotype genotype : vc.getGenotypes()) { final List<Allele> swappedAlleles = new ArrayList<>(); for (final Allele allele : genotype.getAlleles()) { if (allele.isNoCall()) { swappedAlleles.add(allele); } else { swappedAlleles.add(alleleMap.get(allele)); } } // Flip AD final GenotypeBuilder builder = new GenotypeBuilder(genotype).alleles(swappedAlleles); if (genotype.hasAD() && genotype.getAD().length == 2) { final int[] ad = ArrayUtils.clone(genotype.getAD()); ArrayUtils.reverse(ad); builder.AD(ad); } else { builder.noAD(); } //Flip PL if (genotype.hasPL() && genotype.getPL().length == 3) { final int[] pl = ArrayUtils.clone(genotype.getPL()); ArrayUtils.reverse(pl); builder.PL(pl); } else { builder.noPL(); } swappedGenotypes.add(builder.make()); } swappedBuilder.genotypes(swappedGenotypes); for (final String key : vc.getAttributes().keySet()) { if (annotationsToDrop.contains(key)) { swappedBuilder.rmAttribute(key); } else if (annotationsToReverse.contains(key) && !vc.getAttributeAsString(key, "").equals(VCFConstants.MISSING_VALUE_v4)) { final double attributeToReverse = vc.getAttributeAsDouble(key, -1); if (attributeToReverse < 0 || attributeToReverse > 1) { log.warn("Trying to reverse attribute " + key + " but found value that isn't between 0 and 1: (" + attributeToReverse + ") in variant " + vc + ". Results might be wrong."); } swappedBuilder.attribute(key, 1 - attributeToReverse); } } return swappedBuilder.make(); }