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

How can I create tabix .tbi index file for some .vcf file using java (htsjdk)?

tabix

2 answers

Use a TabixIndexCreator

Use createLinearIndex,

"a helper method for creating a linear binned index with default bin size"

http://samtools.github.io/htsjdk/javadoc/htsjdk/htsjdk/tribble/index/IndexFactory.html

static LinearIndex  createLinearIndex(java.io.File inputFile, FeatureCodec codec)

with a VCFCodec as FeatureCodec type parameter:

http://samtools.github.io/htsjdk/javadoc/htsjdk/htsjdk/variant/vcf/VCFCodec.html

and write it as in IGVtools.java writeTribbleIndex method:

public static void writeTribbleIndex(Index idx, String idxFile) throws IOException {
        LittleEndianOutputStream stream = null;
        try {
            stream = new LittleEndianOutputStream(new BufferedOutputStream(new FileOutputStream(idxFile)));
            idx.write(stream);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            // Delete output file as its probably corrupt
            File tmp = new File(idxFile);
            if (tmp.exists()) {
                tmp.delete();
            }
        } finally {
            if (stream != null) {
                stream.close();
            }
        }
    }

Log in to answer this question.