@Override public void put(VariantContext vc) throws VariantContextFilterException { LOGGER.trace("Putting VariantContext into consumer " + vc.toString()); sink.accept(vc); }
@Test(dataProvider = "UsingAD") public void testUsingAD(final VariantContext vc, final double expectedQD) { final Map<String, Object> annotatedMap = new QualByDepth().annotate(null, null, null, null, vc, null); Assert.assertNotNull(annotatedMap, vc.toString()); final String QD = (String)annotatedMap.get("QD"); Assert.assertEquals(Double.valueOf(QD), expectedQD, 0.0001); }
/** * 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()); }
@Test(dataProvider = "variantProvider") public void testSnpFilter(final VariantContext vc, final boolean shouldPass) { final SnpFilter snpFilter = new SnpFilter(); Assert.assertEquals(snpFilter.test(vc), shouldPass, vc.toString()); } }
public enum GenotypeAssignmentMethod { /** * set all of the genotype GT values to NO_CALL */ SET_TO_NO_CALL, /** * set all of the genotype GT values to NO_CALL and remove annotations */ SET_TO_NO_CALL_NO_ANNOTATIONS, /** * Use the subsetted PLs to greedily assigned genotypes */ USE_PLS_TO_ASSIGN, /** * Try to match the original GT calls, if at all possible * * Suppose I have 3 alleles: A/B/C and the following samples: * * original_GT best_match to A/B best_match to A/C * S1 => A/A A/A A/A * S2 => A/B A/B A/A * S3 => B/B B/B A/A * S4 => B/C A/B A/C * S5 => C/C A/A C/C * * Basically, all alleles not in the subset map to ref. It means that het-alt genotypes
@Test(dataProvider = "variantProvider") public void testPassingVariantFilter(final VariantContext vc, final boolean shouldPass) { final PassingVariantFilter passingVariantFilter = new PassingVariantFilter(); Assert.assertEquals(passingVariantFilter.test(vc), shouldPass, vc.toString()); } }
@Test(dataProvider = "genotypeProvider") public void testHetFilter(final VariantContext vc, final String sample, final boolean shouldPass) { final GenotypeQualityFilter gqFilter = getFilter(sample); Assert.assertEquals(gqFilter.test(vc), shouldPass, vc.toString()); }
@Test public void ensureUniqueVariantObservationsEspeciallyMultiAllelicOnesThatAppearAtChunkingBoundaries() { final VariantIteratorProducer.Threadsafe iteratorFactory = new VariantIteratorProducer.Threadsafe( VcfFileSegmentGenerator.byWholeContigSubdividingWithWidth(TEN_MILLION), Arrays.asList(VCF_WITH_MULTI_ALLELIC_VARIANT_AT_POSITION_10MILLION) ); final Set<String> observed = new HashSet<String>(); for (final CloseableIterator<VariantContext> i : iteratorFactory.iterators()) { while (i.hasNext()) { final VariantContext next = i.next(); Assert.assertTrue(observed.add(next.toString()), "Second observation for " + next.toString()); } } }
@Test public void ensureSameVariantsReadAsSimpleVcfFileIterator() { final VariantIteratorProducer.Threadsafe iteratorFactory = new VariantIteratorProducer.Threadsafe( VcfFileSegmentGenerator.byWholeContigSubdividingWithWidth(TEN_MILLION), Arrays.asList(VCF_WITH_MULTI_ALLELIC_VARIANT_AT_POSITION_10MILLION) ); final Set<String> observedVcs = new HashSet<String>(); final Set<String> actual = new HashSet<String>(); final VCFFileReader actualVcs = new VCFFileReader(VCF_WITH_MULTI_ALLELIC_VARIANT_AT_POSITION_10MILLION); for (final VariantContext actualVc : actualVcs) { actual.add(actualVc.toString()); } for (final CloseableIterator<VariantContext> i : iteratorFactory.iterators()) { while (i.hasNext()) { observedVcs.add(i.next().toString()); } } Assert.assertEquals(actual, observedVcs); }
public String toString() { StringBuilder b = new StringBuilder(); b.append("VariantContextTestData: ["); final VariantContext vc = vcs.get(0); final VariantContextBuilder builder = new VariantContextBuilder(vc); builder.noGenotypes(); b.append(builder.make().toString()); if ( vc.getNSamples() < 5 ) { for ( final Genotype g : vc.getGenotypes() ) b.append(g.toString()); } else { b.append(" nGenotypes = ").append(vc.getNSamples()); } if ( vcs.size() > 1 ) b.append(" ----- with another ").append(vcs.size() - 1).append(" VariantContext records"); b.append("]"); return b.toString(); } }
@Override public void add( VariantContext vc ) { if ( doNotWriteGenotypes ) vc = new VariantContextBuilder(vc).noGenotypes().make(); vc = vc.fullyDecode(header, false); super.add(vc); // allow on the fly indexing try { final byte[] infoBlock = buildSitesData(vc); final byte[] genotypesBlock = buildSamplesData(vc); // write the two blocks to disk writeBlock(infoBlock, genotypesBlock); } catch ( IOException e ) { throw new RuntimeException("Error writing record to BCF2 file: " + vc.toString(), e); } }
/** * Tests that all records get PASS set as their filter when extreme values are used for filtering. */ @Test(dataProvider = "goodInputVcfs") public void testNoFiltering(final File input) throws Exception { final File out = testFiltering(input, ".vcf.gz", 0, 0, 0, Double.MAX_VALUE); final VCFFileReader in = new VCFFileReader(out, false); for (final VariantContext ctx : in) { if (!ctx.filtersWereApplied() || ctx.isFiltered()) { Assert.fail("Context should not have been filtered: " + ctx.toString()); } } in.close(); }
/** * Asserts readability and correctness of VCF over HTTP. The VCF is indexed and requires and index. */ @Test public void testVcfOverHTTP() throws IOException { final VCFCodec codec = new VCFCodec(); final AbstractFeatureReader<VariantContext, LineIterator> featureReaderHttp = AbstractFeatureReader.getFeatureReader(HTTP_INDEXED_VCF_PATH, codec, true); // Require an index to final AbstractFeatureReader<VariantContext, LineIterator> featureReaderLocal = AbstractFeatureReader.getFeatureReader(LOCAL_MIRROR_HTTP_INDEXED_VCF_PATH, codec, false); final CloseableTribbleIterator<VariantContext> localIterator = featureReaderLocal.iterator(); for (final Feature feat : featureReaderHttp.iterator()) { assertEquals(feat.toString(), localIterator.next().toString()); } assertFalse(localIterator.hasNext()); }
public static void assertEquals(final VariantContext actual, final VariantContext expected) { if (expected == null) { Assert.assertNull(actual); return; } final String expectedString = expected.toString(); Assert.assertNotNull(actual, "null status"); Assert.assertEquals(actual.getContig(), expected.getContig(), expectedString + " Different contigs: "); Assert.assertEquals(actual.getStart(), expected.getStart(), expectedString + " Different starts: "); Assert.assertEquals(actual.getEnd(), expected.getEnd(), expectedString + " Different ends: "); Assert.assertTrue(actual.hasSameAllelesAs(expected), "Alleles differ between " + actual + " and " + expected + ": "); assertEquals(actual.getGenotypes(), expected.getGenotypes()); Assert.assertEquals(actual.getID(), expected.getID(), "IDs differ for " + expectedString); Assert.assertEquals(actual.getFilters(), expected.getFilters(), "Filters differ for " + expectedString); Assert.assertEquals(actual.getAttributes(), expected.getAttributes(), "Attributes differ for " + expectedString); }
@Override public String map(final RefMetaDataTracker tracker, final ReferenceContext ref, final AlignmentContext context) { if ( tracker == null ) return null; final String contig = context.getLocation().getContig(); final long position = context.getPosition(); final char refAllele = (char)ref.getBase(); final List<VariantContext> VCs = tracker.getValues(sites, context.getLocation()); if(VCs != null && VCs.size() > 1) throw new UserException("More then one variant context at position: "+contig+":"+position); if(VCs == null || VCs.isEmpty()) return null; final VariantContext vc = VCs.get(0); if(!vc.isBiallelic()) { logger.warn("Ignoring site: cannot run ASE on non-biallelic sites: " + vc.toString()); return null; } if ( vc.getNAlleles() == 1 || vc.getAlternateAllele(0).getBases().length == 0 ) throw new UserException("The file of variant sites must contain heterozygous sites and cannot be a GVCF file containing <NON_REF> alleles."); final char altAllele = (char)vc.getAlternateAllele(0).getBases()[0]; final String siteID = vc.getID(); final ReadBackedPileup pileup = filterPileup(context.getBasePileup(), countType, includeReadsWithDeletionAtLoci()); // count up the depths of all and QC+ bases return calculateLineForSite(pileup, contig, position, siteID, refAllele, altAllele); }
@Override public void add( VariantContext vc ) { if ( doNotWriteGenotypes ) vc = new VariantContextBuilder(vc).noGenotypes().make(); vc = vc.fullyDecode(header, false); super.add(vc); // allow on the fly indexing try { final byte[] infoBlock = buildSitesData(vc); final byte[] genotypesBlock = buildSamplesData(vc); // write the two blocks to disk writeBlock(infoBlock, genotypesBlock); outputHasBeenWritten = true; } catch ( IOException e ) { throw new RuntimeIOException("Error writing record to BCF2 file: " + vc.toString(), e); } }
@Override public void add( VariantContext vc ) { if ( doNotWriteGenotypes ) vc = new VariantContextBuilder(vc).noGenotypes().make(); vc = vc.fullyDecode(header, false); super.add(vc); // allow on the fly indexing try { final byte[] infoBlock = buildSitesData(vc); final byte[] genotypesBlock = buildSamplesData(vc); // write the two blocks to disk writeBlock(infoBlock, genotypesBlock); outputHasBeenWritten = true; } catch ( IOException e ) { throw new RuntimeIOException("Error writing record to BCF2 file: " + vc.toString(), e); } }
public void checkReading(ValidationStringency validationStringency) throws Exception { String filename = "invalid_info_field.vcf"; Configuration conf = new Configuration(); String input_file = ClassLoader.getSystemClassLoader().getResource(filename).getFile(); conf.set("mapred.input.dir", "file://" + input_file); if (validationStringency != null) { VCFRecordReader.setValidationStringency(conf, validationStringency); } TaskAttemptContext taskAttemptContext = new TaskAttemptContextImpl(conf, mock(TaskAttemptID.class)); JobContext ctx = new JobContextImpl(conf, taskAttemptContext.getJobID()); VCFInputFormat inputFormat = new VCFInputFormat(conf); List<InputSplit> splits = inputFormat.getSplits(ctx); assertEquals(1, splits.size()); RecordReader<LongWritable, VariantContextWritable> reader = inputFormat.createRecordReader(splits.get(0), taskAttemptContext); int counter = 0; while (reader.nextKeyValue()) { VariantContextWritable writable = reader.getCurrentValue(); assertNotNull(writable); VariantContext vc = writable.get(); assertNotNull(vc); String value = vc.toString(); assertNotNull(value); counter++; } assertEquals(4, counter); }
@Test public void countEntries() throws Exception { VCFFileReader vcfFileReader = new VCFFileReader(new File("src/test/resources/" + filename), false); Iterator<VariantContext> variantIterator; if (interval == null) { variantIterator = vcfFileReader.iterator(); } else { variantIterator = vcfFileReader.query(interval.getContig(), interval.getStart(), interval.getEnd()); } int expectedCount = Iterators.size(variantIterator); int counter = 0; for (RecordReader<LongWritable, VariantContextWritable> reader : readers) { while (reader.nextKeyValue()) { writable = reader.getCurrentValue(); assertNotNull(writable); VariantContext vc = writable.get(); assertNotNull(vc); String value = vc.toString(); assertNotNull(value); counter++; } } assertEquals(expectedCount, counter); }