Source:
public void testReadAndWritePhasedBCF() throws IOException { final File vcfInputFile = new File("src/test/resources/htsjdk/variant/phased.vcf"); final File bcfOutputFile = File.createTempFile("testWriteAndReadBCFHeaderless.", ".bcf", tempDir); final File vcfOutputFile = File.createTempFile("testWriteAndReadBCFHeaderless.", ".vcf", tempDir); bcfOutputFile.deleteOnExit(); vcfOutputFile.deleteOnExit(); VCFFileReader vcfFile = new VCFFileReader(vcfInputFile); VariantContextWriter bcfWriter = new VariantContextWriterBuilder().setOutputFile(bcfOutputFile).setReferenceDictionary(vcfFile.getFileHeader().getSequenceDictionary()).build(); VariantContextWriter vcfWriter = new VariantContextWriterBuilder().setOutputFile(vcfOutputFile).setReferenceDictionary(vcfFile.getFileHeader().getSequenceDictionary()).build(); bcfWriter.writeHeader(vcfFile.getFileHeader()); vcfWriter.writeHeader(vcfFile.getFileHeader()); for (VariantContext vc : vcfFile.iterator().toList()) { Assert.assertEquals(vc.getGenotypes().stream().filter(Genotype::isPhased).count(),2); bcfWriter.add(vc); } bcfWriter.close(); try (final PositionalBufferedStream headerPbs = new PositionalBufferedStream(new FileInputStream(bcfOutputFile))) { BCF2Codec codec = new BCF2Codec(); codec.readHeader(headerPbs); while (!headerPbs.isDone()) { VariantContext vc = codec.decode(headerPbs); Assert.assertEquals(vc.getGenotypes().stream().filter(Genotype::isPhased).count(),2); vcfWriter.add(vc); } bcfWriter.close(); } VCFFileReader vcfOutput = new VCFFileReader(vcfInputFile); for (VariantContext vc : vcfOutput.iterator().toList()) { Assert.assertEquals(vc.getGenotypes().stream().filter(Genotype::isPhased).count(),2); } }

Target:
public void testReadAndWritePhasedBCF() throws IOException { final File vcfInputFile = new File("src/test/resources/htsjdk/variant/phased.vcf"); final File bcfOutputFile = File.createTempFile("testWriteAndReadBCFHeaderless.", ".bcf", tempDir); bcfOutputFile.deleteOnExit(); try ( VCFFileReader vcfFile = new VCFFileReader(vcfInputFile); VariantContextWriter bcfWriter = new VariantContextWriterBuilder().setOutputFile(bcfOutputFile).setReferenceDictionary(vcfFile.getFileHeader().getSequenceDictionary()).build(); ) { bcfWriter.writeHeader(vcfFile.getFileHeader()); for (VariantContext vc : vcfFile.iterator().toList()) { Assert.assertEquals(vc.getGenotypes().stream().filter(Genotype::isPhased).count(), 2); bcfWriter.add(vc); } bcfWriter.close(); final File vcfOutputFile = File.createTempFile("testWriteAndReadBCFHeaderless.", ".vcf", tempDir); vcfOutputFile.deleteOnExit(); try (final PositionalBufferedStream headerPbs = new PositionalBufferedStream(new FileInputStream(bcfOutputFile)); VariantContextWriter vcfWriter = new VariantContextWriterBuilder().setOutputFile(vcfOutputFile).setReferenceDictionary(vcfFile.getFileHeader().getSequenceDictionary()).build(); ) { vcfWriter.writeHeader(vcfFile.getFileHeader()); BCF2Codec codec = new BCF2Codec(); codec.readHeader(headerPbs); while (!headerPbs.isDone()) { VariantContext vc = codec.decode(headerPbs); Assert.assertEquals(vc.getGenotypes().stream().filter(Genotype::isPhased).count(), 2); vcfWriter.add(vc); } vcfWriter.close(); } try (VCFFileReader vcfOutput = new VCFFileReader(vcfInputFile);) { for (VariantContext vc : vcfOutput.iterator().toList()) { Assert.assertEquals(vc.getGenotypes().stream().filter(Genotype::isPhased).count(), 2); } } } }