Eventually I got around this by adapting the code from https://github.com/lindenb/jvarkit/wiki/BedIndexTabix. Thanks a ton Pierre! Here's a method to block compress and index a bed file:
/**
* Block compress input bed file and create associated tabix index. Newly created file and index are
* deleted on exit if deleteOnExit true.
* @throws IOException
* */
private void blockCompressAndIndex(String in, String bgzfOut, boolean deleteOnExit) throws IOException {
System.err.print("Compressing: " + in + " to file: " + bgzfOut + "... ");
File inFile= new File(in);
File outFile= new File(bgzfOut);
LineIterator lin= IOUtils.openURIForLineIterator(inFile.getAbsolutePath());
BlockCompressedOutputStream writer = new BlockCompressedOutputStream(outFile);
long filePosition= writer.getFilePointer();
TabixIndexCreator indexCreator=new TabixIndexCreator(TabixFormat.BED);
BedLineCodec bedCodec= new BedLineCodec();
while(lin.hasNext()){
String line = lin.next();
BedLine bed = bedCodec.decode(line);
if(bed==null) continue;
writer.write(line.getBytes());
writer.write('\n');
indexCreator.addFeature(bed, filePosition);
filePosition = writer.getFilePointer();
}
writer.flush();
System.err.print("Indexing... ");
File tbi= new File(bgzfOut + TabixUtils.STANDARD_INDEX_EXTENSION);
if(tbi.exists() && tbi.isFile()){
System.err.println("Index file exists: " + tbi);
System.exit(1);
}
Index index = indexCreator.finalizeIndex(writer.getFilePointer());
index.writeBasedOnFeatureFile(outFile);
writer.close();
System.err.println("Done");
if(deleteOnExit){
outFile.deleteOnExit();
File idx= new File(outFile.getAbsolutePath() + TabixUtils.STANDARD_INDEX_EXTENSION);
idx.deleteOnExit();
}
}
are you sure your VCF was sorted ?
Hi Pierre, thanks for replying. Input file is bed not vcf, but anyway it looks ok, see edit. I'll have a look at your code, but still... Either there is a big bug in htsjdk.tribble createTabixIndex or I'm doing something silly...!
PS Not sure the link to the test file was working, before, I've updated it.