Skip to content

Commit

Permalink
new interface VCFReader (#1473)
Browse files Browse the repository at this point in the history
* Added a new VCFReader interface that VCFFileReader implements.
  • Loading branch information
lindenb authored Apr 17, 2020
1 parent 4ea6fa3 commit bd1b739
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/main/java/htsjdk/variant/vcf/VCFFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,18 @@

package htsjdk.variant.vcf;

import htsjdk.samtools.SAMFileHeader;
import htsjdk.samtools.SAMSequenceDictionary;
import htsjdk.samtools.util.CloseableIterator;
import htsjdk.samtools.util.FileExtensions;
import htsjdk.samtools.util.Interval;
import htsjdk.samtools.util.IntervalList;
import htsjdk.samtools.util.Locatable;
import htsjdk.tribble.AbstractFeatureReader;
import htsjdk.tribble.FeatureCodec;
import htsjdk.tribble.FeatureReader;
import htsjdk.tribble.TribbleException;
import htsjdk.variant.bcf2.BCF2Codec;
import htsjdk.variant.variantcontext.VariantContext;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
Expand All @@ -48,7 +45,7 @@
/**
* Simplified interface for reading from VCF/BCF files.
*/
public class VCFFileReader implements Closeable, Iterable<VariantContext> {
public class VCFFileReader implements VCFReader {

private final FeatureReader<VariantContext> reader;

Expand Down Expand Up @@ -311,9 +308,17 @@ public static Iterator<Interval> toIntervals(final VCFFileReader vcf, final bool
/**
* Returns the VCFHeader associated with this VCF/BCF file.
*/
public VCFHeader getFileHeader() {
@Override
public VCFHeader getHeader() {
return (VCFHeader) reader.getHeader();
}

/**
* Synonym of {@link #getHeader()}
*/
public final VCFHeader getFileHeader() {
return getHeader();
}

/**
* Returns an iterator over all records in this VCF/BCF file.
Expand Down Expand Up @@ -344,16 +349,6 @@ public CloseableIterator<VariantContext> query(final String chrom, final int sta
}
}

/**
* 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());
}

@Override
public void close() {
try {
Expand All @@ -369,6 +364,7 @@ public void close() {
*
* @return true if the reader can be queried, i.e. if the underlying Tribble reader is queryable.
*/
@Override
public boolean isQueryable() {
return reader.isQueryable();
}
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/htsjdk/variant/vcf/VCFReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* The MIT License
*
* Copyright (c) 2013 The Broad Institute
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package htsjdk.variant.vcf;

import java.io.Closeable;
import htsjdk.samtools.util.CloseableIterator;
import htsjdk.samtools.util.Locatable;
import htsjdk.variant.variantcontext.VariantContext;

/**
* Interface for reading VCF/BCF files.
*/
public interface VCFReader extends Closeable, Iterable<VariantContext> {

/**
* Returns the VCFHeader associated with this VCFReader.
*/
public VCFHeader getHeader();

/**
* Queries for records overlapping the region specified.
* Note that this method requires VCF files with an associated index. If no index exists a TribbleException will be thrown.
*
* @param chrom the chomosome to query
* @param start query interval start
* @param end query interval end
* @return non-null iterator over VariantContexts
*/
public CloseableIterator<VariantContext> query(final String chrom, final int start, final int end);

/**
* 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 default CloseableIterator<VariantContext> query(final Locatable locatable) {
return query(locatable.getContig(), locatable.getStart(), locatable.getEnd());
}

/**
* A method to check if the reader is query-able, i.e. if a call to {@link VCFFileReader#query(String, int, int)}
* can be successful
*
* @return true if the reader can be queried, i.e. if the underlying Tribble reader is queryable.
*/
public boolean isQueryable();

@Override
public CloseableIterator<VariantContext> iterator();

}

0 comments on commit bd1b739

Please sign in to comment.