@Override public CloseableIterator<VariantContext> query(String contig, int beginPos, int endPos) { return vcfReader.query(contig, beginPos, endPos); }
/** If the current iterator is null or exhausted, move to the next interval. */ private void advance() { while ((currentIterator == null || !currentIterator.hasNext()) && this.intervals.hasNext()) { if (currentIterator != null) currentCloseableIterator.close(); final Interval interval = this.intervals.next(); final Interval previousInterval = this.lastInterval; this.currentCloseableIterator = this.reader.query(interval.getContig(), interval.getStart(), interval.getEnd()); this.currentIterator = this.currentCloseableIterator.stream().filter ( ctx -> null == previousInterval || !overlapsInterval(ctx, previousInterval) ).iterator(); this.lastInterval = interval; } }
/** If the current iterator is null or exhausted, move to the next interval. */ private void advance() { while ((currentIterator == null || !currentIterator.hasNext()) && this.intervals.hasNext()) { if (currentIterator != null) currentCloseableIterator.close(); final Interval interval = this.intervals.next(); final Interval previousInterval = this.lastInterval; this.currentCloseableIterator = this.reader.query(interval.getContig(), interval.getStart(), interval.getEnd()); this.currentIterator = this.currentCloseableIterator.stream().filter ( ctx -> null == previousInterval || !overlapsInterval(ctx, previousInterval) ).iterator(); this.lastInterval = interval; } }
/** This test doesn't even test the class, it just makes sure the cornercase test data is really a cornercase */ @Test public void ensureTestDataActuallyHasWideVariantAtTenMillion() { final Joiner joiner = Joiner.on(":"); // Cheat: do a string compare final VCFFileReader r = new VCFFileReader(VCF_WITH_MULTI_ALLELIC_VARIANT_AT_POSITION_10MILLION); Assert.assertEquals( joiner.join(r.query("1", TEN_MILLION, TEN_MILLION)), joiner.join(r.query("1", TEN_MILLION + 5, TEN_MILLION + 5)) ); r.close(); }
/** * Queries for records overlapping the {@link Locatable} specified. * Note that this method requires VCF files with an associated index. If no index exists a TribbleException will be thrown. * * @return non-null iterator over VariantContexts */ public CloseableIterator<VariantContext> query(final Locatable locatable) { return query(locatable.getContig(), locatable.getStart(), locatable.getEnd()); }
/** * Queries for records overlapping the {@link Locatable} specified. * Note that this method requires VCF files with an associated index. If no index exists a TribbleException will be thrown. * * @return non-null iterator over VariantContexts */ public CloseableIterator<VariantContext> query(final Locatable locatable) { return query(locatable.getContig(), locatable.getStart(), locatable.getEnd()); }
/** * Converts a {@link VcfFileSegment} into a {@link VariantContext} iterator. Applies filtering via {@link #intervalsOfInterestDetector} * if it is defined. */ private CloseableIterator<VariantContext> iteratorForSegment(final VcfFileSegment segment) { final CloseableIterator<VariantContext> query = localVcfFileReaders.get() // Get the collection of VCF file readers local to this thread .get(segment.vcf()) // Get or generate the reader for this segment's VCF file .query(segment.contig(), segment.start(), segment.stop()); // Query the segment // Then wrap the iterator in a on-the-fly interval-list based filter, if requested. final Collection<Predicate<VariantContext>> filters = new ArrayList<>(); if (intervalsOfInterestDetector != null) { filters.add(new OverlapsPredicate()); } filters.add(new NonUniqueVariantPredicate(segment)); return new PredicateFilterDecoratingClosableIterator<>(query, filters); }
/** * Converts a {@link VcfFileSegment} into a {@link VariantContext} iterator. Applies filtering via {@link #intervalsOfInterestDetector} * if it is defined. */ private CloseableIterator<VariantContext> iteratorForSegment(final VcfFileSegment segment) { final CloseableIterator<VariantContext> query = localVcfFileReaders.get() // Get the collection of VCF file readers local to this thread .get(segment.vcf()) // Get or generate the reader for this segment's VCF file .query(segment.contig(), segment.start(), segment.stop()); // Query the segment // Then wrap the iterator in a on-the-fly interval-list based filter, if requested. final Collection<Predicate<VariantContext>> filters = new ArrayList<>(); if (intervalsOfInterestDetector != null) { filters.add(new OverlapsPredicate()); } filters.add(new NonUniqueVariantPredicate(segment)); return new PredicateFilterDecoratingClosableIterator<>(query, filters); }
@Test public void handleSpaceInVCFInfoAndInvalidKey(){ // This reading would fail on the original htsjdk-1.141 VCFFileReader reader = new VCFFileReader(new File("test_data/malformed.vcf.gz")); reader.query("chr1", 1, 16000000); reader.close(); }
public <T> VcfIterator<T> iterator(Region region, VariantFilters<VariantContext> filters, Class<T> clazz) { open(); CloseableIterator<VariantContext> variantContextIterator = vcfReader.query(region.getChromosome(), region.getStart(), region.getEnd()); return getVariantIterator(filters, clazz, variantContextIterator); }
@Override public VariantContext annotateVariantContext(VariantContext obsVC) { try (CloseableIterator<VariantContext> iter = vcfReader.query(obsVC.getContig(), obsVC.getStart(), obsVC.getEnd())) { // Fetch all overlapping and matching genotypes from database and pair them with the correct allele from vc. List<GenotypeMatch> genotypeMatches = new ArrayList<>(); List<GenotypeMatch> positionOverlaps = new ArrayList<>(); while (iter.hasNext()) { final VariantContext dbVC = iter.next(); genotypeMatches.addAll(matcher.matchGenotypes(obsVC, dbVC)); // TODO: what to do about non-reference/non-alt ClinVar annotation "-1"? if (options.isReportOverlapping() || options.isReportOverlappingAsMatching()) positionOverlaps.addAll(matcher.positionOverlaps(obsVC, dbVC)); } List<GenotypeMatch> emptyList = new ArrayList<>(); // Use these records to annotate the variant call in obsVC (record-wise but also per alternative allele) if (options.isReportOverlappingAsMatching()) return annotateWithDBRecords(obsVC, positionOverlaps, emptyList); else if (options.isReportOverlapping()) return annotateWithDBRecords(obsVC, genotypeMatches, positionOverlaps); else return annotateWithDBRecords(obsVC, genotypeMatches, emptyList); } }
/** * Loads genotypes from the supplied reader into one or more Fingerprint objects and returns them in a * Map of Sample->Fingerprint. * * @param reader - VCF reader containing genotypes for one or more samples * @param specificSample - null to load genotypes for all samples contained in the file or the name * of an individual sample to load (and exclude all others). * @param source The path of the source file used. used to emit errors. * @return a Map of Sample name to Fingerprint */ public Map<String, Fingerprint> loadFingerprintsFromQueriableReader(final VCFFileReader reader, final String specificSample, final Path source) { SequenceUtil.assertSequenceDictionariesEqual(this.haplotypes.getHeader().getSequenceDictionary(), reader.getFileHeader().getSequenceDictionary()); final SortedSet<Snp> snps = new TreeSet<>(haplotypes.getAllSnps()); return loadFingerprintsFromVariantContexts(() -> snps.stream().map(snp -> { try { return reader.query(snp.getChrom(), snp.getPos(), snp.getPos()).next(); } catch (NoSuchElementException e) { return null; } }).iterator(), specificSample, source); }
/** * Loads genotypes from the supplied reader into one or more Fingerprint objects and returns them in a * Map of Sample->Fingerprint. * * @param reader - VCF reader containing genotypes for one or more samples * @param specificSample - null to load genotypes for all samples contained in the file or the name * of an individual sample to load (and exclude all others). * @param source The path of the source file used. used to emit errors. * @return a Map of Sample name to Fingerprint */ public Map<String, Fingerprint> loadFingerprintsFromQueriableReader(final VCFFileReader reader, final String specificSample, final Path source) { SequenceUtil.assertSequenceDictionariesEqual(this.haplotypes.getHeader().getSequenceDictionary(), reader.getFileHeader().getSequenceDictionary()); final SortedSet<Snp> snps = new TreeSet<>(haplotypes.getAllSnps()); return loadFingerprintsFromVariantContexts(() -> snps.stream().map(snp -> { try { return reader.query(snp.getChrom(), snp.getPos(), snp.getPos()).next(); } catch (NoSuchElementException e) { return null; } }).iterator(), specificSample, source); }
final Iterator<VariantContext> tmpIt = tmpReader.query(vcOrig); Assert.assertTrue(tmpIt.hasNext(), "Variant not returned from indexed file"); final VariantContext vcTmp = tmpIt.next();
Assert.assertEquals(42, countIteratedElements(compressedVcfReader.query("1", 868379 - 1, 1006891 + 1))); Assert.assertEquals(42, countIteratedElements(plainTextVcfReader.query("1", 868379 - 1, 1006891 + 1))); Assert.assertEquals(13, countIteratedElements(compressedVcfReader.query("1", 1, 836463 + 1))); Assert.assertEquals(13, countIteratedElements(plainTextVcfReader.query("1", 1, 836463 + 1))); Assert.assertEquals(36, countIteratedElements(compressedVcfReader.query("1", 76690833 - 1, 76837502 + 11111111))); Assert.assertEquals(36, countIteratedElements(plainTextVcfReader.query("1", 76690833 - 1, 76837502 + 11111111))); Assert.assertEquals(0, countIteratedElements(compressedVcfReader.query("1", 36606472 + 1, 36623523 - 1))); Assert.assertEquals(0, countIteratedElements(plainTextVcfReader.query("1", 36606472 + 1, 36623523 - 1))); Assert.assertEquals(0, countIteratedElements(compressedVcfReader.query("1", 1, 10))); Assert.assertEquals(0, countIteratedElements(plainTextVcfReader.query("1", 1, 10))); Assert.assertEquals(0, countIteratedElements(compressedVcfReader.query("1", 76837502 * 15, 76837502 * 16))); Assert.assertEquals(0, countIteratedElements(plainTextVcfReader.query("1", 76837502 * 15, 76837502 * 16))); } catch (NullPointerException e) { Assert.fail("Exception caught on querying: ", e);
@Test(dataProvider = "vcfDataProvider") public void testCreateTabixIndexFromVCF( final File inputVCF, final Interval queryInterval) throws IOException { // copy the original file and create the index for the copy final File tempDir = IOUtil.createTempDir("testCreateTabixIndexFromVCF", null); tempDir.deleteOnExit(); final File tmpVCF = new File(tempDir, inputVCF.getName()); Files.copy(inputVCF, tmpVCF); tmpVCF.deleteOnExit(); // this test creates a TABIX index (.tbi) final TabixIndex tabixIndexGz = IndexFactory.createTabixIndex(tmpVCF, new VCFCodec(), null); tabixIndexGz.writeBasedOnFeatureFile(tmpVCF); final File tmpIndex = Tribble.tabixIndexFile(tmpVCF); tmpIndex.deleteOnExit(); try (final VCFFileReader originalReader = new VCFFileReader(inputVCF,false); final VCFFileReader tmpReader = new VCFFileReader(tmpVCF, tmpIndex,true)) { Iterator<VariantContext> originalIt = originalReader.iterator(); Iterator<VariantContext> tmpIt = tmpReader.query(queryInterval.getContig(), queryInterval.getStart(), queryInterval.getEnd()); while (originalIt.hasNext()) { Assert.assertTrue(tmpIt.hasNext(), "variants missing from gzip query"); VariantContext vcTmp = tmpIt.next(); VariantContext vcOrig = originalIt.next(); Assert.assertEquals(vcOrig.getContig(), vcTmp.getContig()); Assert.assertEquals(vcOrig.getStart(), vcTmp.getStart()); Assert.assertEquals(vcOrig.getEnd(), vcTmp.getEnd()); } } }
iter = vcfReader.query(itv.getContig(), itv.getStart(), itv.getEnd()); System.err.println("Will read interval " + itv.toString()); } else {
@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); }