1 package jalview.ext.htsjdk;
3 import htsjdk.samtools.util.CloseableIterator;
4 import htsjdk.variant.variantcontext.VariantContext;
5 import htsjdk.variant.vcf.VCFFileReader;
6 import htsjdk.variant.vcf.VCFHeader;
8 import java.io.Closeable;
10 import java.io.IOException;
13 * A thin wrapper for htsjdk classes to read either plain, or compressed, or
14 * compressed and indexed VCF files
16 public class VCFReader implements Closeable, Iterable<VariantContext>
18 private static final String GZ = "gz";
20 private static final String TBI_EXTENSION = ".tbi";
22 private boolean indexed;
24 private VCFFileReader reader;
27 * Constructor given a raw or compressed VCF file or a (tabix) index file
29 * For now, file type is inferred from its suffix: .gz or .bgz for compressed
30 * data, .tbi for an index file, anything else is assumed to be plain text
36 public VCFReader(String filePath) throws IOException
38 if (filePath.endsWith(GZ))
40 if (new File(filePath + TBI_EXTENSION).exists())
45 else if (filePath.endsWith(TBI_EXTENSION))
48 filePath = filePath.substring(0, filePath.length() - 4);
51 reader = new VCFFileReader(new File(filePath), indexed);
55 public void close() throws IOException
64 * Returns an iterator over VCF variants in the file. The client should call
65 * close() on the iterator when finished with it.
68 public CloseableIterator<VariantContext> iterator()
70 return reader == null ? null : reader.iterator();
74 * Queries for records overlapping the region specified. Note that this method
75 * requires a VCF file with an associated index. If no index exists a
76 * TribbleException will be thrown. Client code should call close() on the
77 * iterator when finished with it.
80 * the chromosome to query
82 * query interval start
87 public CloseableIterator<VariantContext> query(final String chrom,
88 final int start, final int end)
90 return reader == null ? null : reader.query(chrom, start, end);
94 * Returns an object that models the VCF file headers
98 public VCFHeader getFileHeader()
100 return reader == null ? null : reader.getFileHeader();