public void run(final VariantContext vc) { vc.getAttribute("AN", null); } };
@Test(dataProvider = "noCallAndSymbolicData") public void testLiftOverNoCallAndSymbolic(final LiftOver liftOver, final VariantContext source, final VariantContext result, final boolean expectReversed) { final Interval target = liftOver.liftOver(new Interval(source.getContig(), source.getStart(), source.getEnd()), .95); Assert.assertEquals(target.isNegativeStrand(), expectReversed); VariantContext vc = LiftoverUtils.liftVariant(source, target, REFERENCE, true, true); VcfTestUtils.assertEquals(vc, result); Assert.assertEquals(vc.getAttribute(LiftoverVcf.ORIGINAL_CONTIG), source.getContig()); Assert.assertEquals(vc.getAttribute(LiftoverVcf.ORIGINAL_START), source.getStart()); Assert.assertTrue(source.getAlleles().equals(result.getAlleles()) != result.hasAttribute(LiftoverVcf.ORIGINAL_ALLELES)); if (!source.getAlleles().equals(result.getAlleles())) { List<String> resultAlleles = new ArrayList<>(); source.getAlleles().forEach(a -> resultAlleles.add(a.getDisplayString())); Assert.assertEquals(resultAlleles, result.getAttributeAsStringList(LiftoverVcf.ORIGINAL_ALLELES, null)); } }
/** * Add genotyping-based annotations to the new VC * * @param originalAttributes the non-null annotations from the original VC * @param newVC the new non-null VC * @return a non-null VC */ private VariantContext addGenotypingAnnotations(final Map<String, Object> originalAttributes, final VariantContext newVC) { // we want to carry forward the attributes from the original VC but make sure to add the MLE-based annotations final Map<String, Object> attrs = new HashMap<>(originalAttributes); attrs.put(GATKVCFConstants.MLE_ALLELE_COUNT_KEY, newVC.getAttribute(GATKVCFConstants.MLE_ALLELE_COUNT_KEY)); attrs.put(GATKVCFConstants.MLE_ALLELE_FREQUENCY_KEY, newVC.getAttribute(GATKVCFConstants.MLE_ALLELE_FREQUENCY_KEY)); if (newVC.hasAttribute(GATKVCFConstants.NUMBER_OF_DISCOVERED_ALLELES_KEY)) attrs.put(GATKVCFConstants.NUMBER_OF_DISCOVERED_ALLELES_KEY, newVC.getAttribute(GATKVCFConstants.NUMBER_OF_DISCOVERED_ALLELES_KEY)); if (newVC.hasAttribute(GATKVCFConstants.AS_QUAL_KEY)) attrs.put(GATKVCFConstants.AS_QUAL_KEY, newVC.getAttribute(GATKVCFConstants.AS_QUAL_KEY)); return new VariantContextBuilder(newVC).attributes(attrs).make(); }
@Override public Object get(String name) { Object result = null; if ( attributes.containsKey(name) ) { // dynamic resolution of name -> value via map result = attributes.get(name).get(vc); } else if ( vc.hasAttribute(name)) { result = vc.getAttribute(name); } else if ( vc.getFilters().contains(name) ) { result = true_string; } return result; }
@Override public Object get(String name) { Object result = null; if ( attributes.containsKey(name) ) { // dynamic resolution of name -> value via map result = attributes.get(name).get(vc); } else if ( vc.hasAttribute(name)) { result = vc.getAttribute(name); } else if ( vc.getFilters().contains(name) ) { result = true_string; } return result; }
public Object get(String name) { Object result = null; if ( x.containsKey(name) ) { // dynamic resolution of name -> value via map result = x.get(name).get(vc); } else if ( vc.hasAttribute(name)) { result = vc.getAttribute(name); } else if ( vc.getFilters().contains(name) ) { result = "1"; } //System.out.printf("dynamic lookup %s => %s%n", name, result); return result; }
/** * * @return true if the variant context is a reference block * */ public boolean isReferenceBlock() { return getAlternateAlleles().size() == 1 && getAlternateAllele(0).isNonRefAllele() && getAttribute(VCFConstants.END_KEY) != null; }
/** * * @return true if the variant context is a reference block * */ public boolean isReferenceBlock() { return getAlternateAlleles().size() == 1 && getAlternateAllele(0).isNonRefAllele() && getAttribute(VCFConstants.END_KEY) != null; }
/** * Get the alternate allele INFO field value * * @param infoFieldName VCF INFO field name * @param vc variant context * @param altAllele the alternate allele * @return alternate allele INFO field value * @throws ReviewedGATKException if the alternate allele is part of the variant context */ private static Object getAltAlleleInfoFieldValue(final String infoFieldName, final VariantContext vc, final Allele altAllele) { //final String[] splitOriginalField = vc.getAttribute(infoFieldName).toString().split(VCFConstants.INFO_FIELD_ARRAY_SEPARATOR); final Object[] splitOriginalField = getVAttributeValues(vc.getAttribute(infoFieldName)); // subset the field final BitSet alleleIndexesToUse = getAlleleIndexBitset(vc, Arrays.asList(altAllele)); // skip the first allele, which is the reference for (int i = 1; i < alleleIndexesToUse.size(); i++) { if (alleleIndexesToUse.get(i)) return splitOriginalField[i-1]; } throw new ReviewedGATKException("Alternate allele " + altAllele.toString() + " not in Variant Context " + vc.toString()); }
/** * Retrieve allele count data from VariantContext using VCFkey, checks for correct number of values in VCF * @param VCFkey VariantContext annotation tag of interest (should be AC or MLEAC) * @param context VariantContext from which to extract the data * @return int[] with allele count data */ private static int[] getAlleleCounts(final String VCFkey, final VariantContext context) { final Object alleleCountsFromVCF = context.getAttribute(VCFkey); if ( alleleCountsFromVCF instanceof List ) { if ( ((List) alleleCountsFromVCF).size() != context.getAlternateAlleles().size() ) throw new UserException(String.format("Variant does not contain the same number of MLE allele counts as alternate alleles for record at %s:%d", context.getChr(), context.getStart())); } else if ( alleleCountsFromVCF instanceof String || alleleCountsFromVCF instanceof Integer) {//here length is 1 if (context.getAlternateAlleles().size() != 1) throw new UserException(String.format("Variant does not contain the same number of MLE allele counts as alternate alleles for record at %s:%d", context.getChr(), context.getStart())); } return extractInts(alleleCountsFromVCF); }
@Test public void testUpdateChromosomeCountsInfo() { final Map<Allele, Integer> calledAltAlleles = new LinkedHashMap<>(); final Set<Double> alleleFrequency = new LinkedHashSet<Double>(calledAltAlleles.size()); final List<Allele> alleles = new ArrayList<>(Arrays.asList(Aref, C)); final int calledAlleles = alleles.size(); final List<Integer> numAltAlleles = new ArrayList<>(); final int alleleOccurence = 1; for ( final Allele allele : alleles ) { if ( allele.isNonReference() ) { calledAltAlleles.put(allele, alleleOccurence); alleleFrequency.add( ((double) alleleOccurence)/calledAlleles ); numAltAlleles.add(1); } } final VariantContextBuilder builder = new VariantContextBuilder("test", "chr1", 1, Aref.length(), alleles); GATKVariantContextUtils.updateChromosomeCountsInfo(calledAltAlleles, calledAlleles, builder); final VariantContext vc = builder.make(); Assert.assertEquals(vc.getAttribute(VCFConstants.ALLELE_COUNT_KEY), numAltAlleles.toArray()); Assert.assertEquals(vc.getAttribute(VCFConstants.ALLELE_NUMBER_KEY), alleles.size()); Assert.assertEquals(vc.getAttribute(VCFConstants.ALLELE_FREQUENCY_KEY), alleleFrequency.toArray()); }
@Test(dataProvider = "trueFalse") public void testAttributeResettingWorks(final boolean leaveModifyableAsIs) { final VariantContextBuilder root1 = new VariantContextBuilder(snpSource, snpLoc, snpLocStart, snpLocStop, Arrays.asList(Aref, C)); final VariantContextBuilder root2 = new VariantContextBuilder(snpSource, snpLoc, snpLocStart, snpLocStop, Arrays.asList(Aref, C)); final VariantContext result1 = root1.attribute("AC", 1).make(leaveModifyableAsIs); //this is a red-herring and should not change anything, however, if leaveModifyableAsIs is true, it does change result1. final VariantContext ignored = root1.attribute("AC", 2).make(leaveModifyableAsIs); final VariantContext result2 = root2.attribute("AC", 1).make(leaveModifyableAsIs); if (leaveModifyableAsIs) { Assert.assertNotSame(result1.getAttribute("AC"), result2.getAttribute("AC")); } else { Assert.assertEquals(result1.getAttribute("AC"), result2.getAttribute("AC")); } }
@Test(enabled = !DEBUG) public void testAnnotationSet() { for ( final boolean annotate : Arrays.asList(true, false)) { for ( final String set : Arrays.asList("set", "combine", "x")) { final List<String> priority = Arrays.asList("1", "2"); VariantContext vc1 = makeVC("1", Arrays.asList(Aref, T), VariantContext.PASSES_FILTERS); VariantContext vc2 = makeVC("2", Arrays.asList(Aref, T), VariantContext.PASSES_FILTERS); final VariantContext merged = GATKVariantContextUtils.simpleMerge( Arrays.asList(vc1, vc2), priority, GATKVariantContextUtils.FilteredRecordMergeType.KEEP_IF_ANY_UNFILTERED, GATKVariantContextUtils.GenotypeMergeType.PRIORITIZE, annotate, false, set, false, false); if ( annotate ) Assert.assertEquals(merged.getAttribute(set), GATKVariantContextUtils.MERGE_INTERSECTION); else Assert.assertFalse(merged.hasAttribute(set)); } } }
@Test(dataProvider = "AnnotateRsIDData") public void testAnnotateOverlaps(final VariantContext toAnnotate, final List<VariantContext> records, final String expectedID, final boolean expectOverlap) throws Exception { final String name = "binding"; final VariantOverlapAnnotator annotator = makeAnnotator(null, name); final VariantContext annotated = annotator.annotateOverlap(records, name, toAnnotate); Assert.assertNotNull(annotated); Assert.assertEquals(annotated.getID(), toAnnotate.getID(), "Shouldn't modify annotation"); Assert.assertEquals(annotated.hasAttribute(name), expectOverlap); if ( expectOverlap ) { Assert.assertEquals(annotated.getAttribute(name), true); } } }
@Test() public void testAttributeResettingWorks() { final VariantContextBuilder root1 = new VariantContextBuilder(snpSource, snpLoc, snpLocStart, snpLocStop, Arrays.asList(Aref, C)); final VariantContextBuilder root2 = new VariantContextBuilder(snpSource, snpLoc, snpLocStart, snpLocStop, Arrays.asList(Aref, C)); final VariantContext result1 = root1.attribute("AC", 1).make(); //this is a red-herring and should not change anything. final VariantContext ignored = root1.attribute("AC", 2).make(); final VariantContext result2 = root2.attribute("AC", 1).make(); Assert.assertEquals(result1.getAttribute("AC"), result2.getAttribute("AC")); } }
@Test(enabled = !DEBUG, dataProvider = "mergeFiltered") public void testMergeFiltered(MergeFilteredTest cfg) { final List<String> priority = vcs2priority(cfg.inputs); final VariantContext merged = GATKVariantContextUtils.simpleMerge( cfg.inputs, priority, cfg.type, GATKVariantContextUtils.GenotypeMergeType.PRIORITIZE, true, false, "set", false, false); // test alleles are equal Assert.assertEquals(merged.getAlleles(), cfg.expected.getAlleles()); // test set field Assert.assertEquals(merged.getAttribute("set"), cfg.setExpected); // test filter field Assert.assertEquals(merged.getFilters(), cfg.expected.getFilters()); }
@Override public GenericTSVRecord convert(VariantContext vc) { GenericTSVRecordBuilder builder = new GenericTSVRecordBuilder(); if (vc.getAlternateAlleles().size() > 1) { throw new RuntimeException( "Must have exactly zero or one ALT allele but this == " + this.toString()); } builder.setContig(vc.getContig()); builder.setPos(vc.getStart() - 1); if (vc.getAlternateAlleles().size() == 0) { builder.setRef("N"); builder.setAlt("N"); } else { builder.setRef(vc.getReference().toString()); builder.setAlt(vc.getAlternateAllele(0).toString()); } for (String colName : tsvOptions.getColumnNames()) { final GenericTSVValueColumnDescription desc = tsvOptions.getValueColumnDescriptions() .get(colName); builder.getValues().add(vc.getAttribute(desc.getFieldName())); } return builder.build(); }
@Override public void site(final BCF2Encoder encoder, final VariantContext vc) throws IOException { final Object rawValue = vc.getAttribute(getField(), null); final BCF2Type type = getFieldEncoder().getType(rawValue); if ( rawValue == null ) { // the value is missing, just write in null encoder.encodeType(0, type); } else { final int valueCount = getFieldEncoder().numElements(vc, rawValue); encoder.encodeType(valueCount, type); getFieldEncoder().encodeValue(encoder, rawValue, type, valueCount); } } }
@Override public void site(final BCF2Encoder encoder, final VariantContext vc) throws IOException { final Object rawValue = vc.getAttribute(getField(), null); final BCF2Type type = getFieldEncoder().getType(rawValue); if ( rawValue == null ) { // the value is missing, just write in null encoder.encodeType(0, type); } else { final int valueCount = getFieldEncoder().numElements(vc, rawValue); encoder.encodeType(valueCount, type); getFieldEncoder().encodeValue(encoder, rawValue, type, valueCount); } } }
@Override public void site(final BCF2Encoder encoder, final VariantContext vc) throws IOException { final Object rawValue = vc.getAttribute(getField(), null); final BCF2Type type = getFieldEncoder().getType(rawValue); if ( rawValue == null ) { // the value is missing, just write in null encoder.encodeType(0, type); } else { final int valueCount = getFieldEncoder().numElements(vc, rawValue); encoder.encodeType(valueCount, type); getFieldEncoder().encodeValue(encoder, rawValue, type, valueCount); } } }