This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to use .tbi index for looking through .vcf file?

I've got this code for creating my .tbi index file: (inputFile is my .vcf File)

VCFFileReader vcfReader = new VCFFileReader(inputFile, false);
TabixIndexCreator creator = new TabixIndexCreator(new TabixFormat().VCF);
int I = 0;
for (VariantContext context : vcfReader) {
    creator.addFeature(context, i);
    i++;
}
vcfReader.close();
creator.finalizeIndex(i).writeBasedOnFeatureFile(inputFile);

After that I try to iterate throw my my .vcf file using next construction:

VCFFileReader vcfReader2 = new VCFFileReader(inputFile, new File(inputFile.getAbsolutePath() + ".tbi"));
CloseableIterator iter = vcfReader2.query("gi|448814763|ref|NC_000962.3|", 1, 5);

and iter.hasNext() is false, though I've got this in my .vcf file.

Thanks

vcf index

I have a similar problem. I have a .tbi file and I want to use it to access my vcf file. I have this code:

VCFFileReader fileR= new VCFFileReader(new File("ex.vcf"), new File("ex.gz.tbi"), true);
VCFHeader fileHeader = fileR.getFileHeader();
VCFRecordCodec recCodec = new VCFRecordCodec(fileHeader);

TabixIndex tic = new TabixIndex("ex.gz.tbi"); 
BlockCompressedInputStream inputStream = new BlockCompressedInputStream("ex.gz"); 
long p = 0; 

String rrline = inputStream.readLine();

while(rrline!=null){
    System.out.println("LINE: " + rrline);
    p = inputStream.getFilePointer();
    rrline = inputStream.readLine();
}

I don't know how to use TabixIndex and BlockCompressedInputStream. Maybe the problem is that I don't understand what they are and what they do.

1 answer

The way you use TabixIndexCreator is not correct. You must read/write the vcf file using a BlockCompressedInputStream/BlockCompressedOutputStream

The second argument of creator.addFeature is not the nth index but the virtual pointer location.

See https://github.com/shenkers/ComparativeBrowser/blob/f75f6fcb23e2fa4d7b5ae42f9e33cd0b99b2046c/src/test/java/htsjdk/samtools/tabix/IndexingFeatureWriterNGTest.java for a example.

Log in to answer this question.