1 package jalview.ext.htsjdk;
3 import htsjdk.samtools.util.CloseableIterator;
4 import htsjdk.variant.variantcontext.VariantContext;
5 import htsjdk.variant.vcf.VCFFileReader;
7 import java.io.Closeable;
9 import java.io.IOException;
12 * A thin wrapper for htsjdk classes to read either plain, or compressed, or
13 * compressed and indexed VCF files
15 public class VCFReader implements Closeable, Iterable<VariantContext>
17 private static final String GZ = "gz";
19 private static final String TBI_EXTENSION = ".tbi";
21 private boolean indexed;
23 private VCFFileReader reader;
26 * Constructor given a raw or compressed VCF file or a (tabix) index file
28 * For now, file type is inferred from its suffix: .gz or .bgz for compressed
29 * data, .tbi for an index file, anything else is assumed to be plain text
35 public VCFReader(String filePath) throws IOException
37 if (filePath.endsWith(GZ))
39 if (new File(filePath + TBI_EXTENSION).exists())
44 else if (filePath.endsWith(TBI_EXTENSION))
47 filePath = filePath.substring(0, filePath.length() - 4);
50 reader = new VCFFileReader(new File(filePath), indexed);
54 public void close() throws IOException
63 * Returns an iterator over VCF variants in the file. The client should call
64 * close() on the iterator when finished with it.
67 public CloseableIterator<VariantContext> iterator()
69 return reader == null ? null : reader.iterator();
73 * Queries for records overlapping the region specified. Note that this method
74 * requires a VCF file with an associated index. If no index exists a
75 * TribbleException will be thrown.
78 * the chromosome to query
80 * query interval start
85 public CloseableIterator<VariantContext> query(final String chrom,
86 final int start, final int end)
88 return reader == null ? null : reader.query(chrom, start, end);