/** * This method makes a copy of the input VCF and creates an index file for it in the same location. * This is done so that we don't need to store the index file in the same repo * The copy of the input is done so that it and its index are in the same directory which is typically required. * * @param vcfFile the vcf file to index * @return File a vcf file (index file is created in same path). */ public static File createTemporaryIndexedVcfFromInput(final File vcfFile, final String tempFilePrefix) throws IOException { final String extension; if (vcfFile.getAbsolutePath().endsWith(IOUtil.VCF_FILE_EXTENSION)) extension = IOUtil.VCF_FILE_EXTENSION; else if (vcfFile.getAbsolutePath().endsWith(IOUtil.COMPRESSED_VCF_FILE_EXTENSION)) extension = IOUtil.COMPRESSED_VCF_FILE_EXTENSION; else throw new IllegalArgumentException("couldn't find a " + IOUtil.VCF_FILE_EXTENSION + " or " + IOUtil.COMPRESSED_VCF_FILE_EXTENSION + " ending for input file " + vcfFile.getAbsolutePath()); File output = createTemporaryIndexedVcfFile(tempFilePrefix, extension); try (final VCFFileReader in = new VCFFileReader(vcfFile, false); final VariantContextWriter out = new VariantContextWriterBuilder(). setReferenceDictionary(in.getFileHeader().getSequenceDictionary()). setOptions(EnumSet.of(Options.INDEX_ON_THE_FLY)). setOutputFile(output).build()) { out.writeHeader(in.getFileHeader()); for (final VariantContext ctx : in) { out.add(ctx); } } return output; }
/** * This method makes a copy of the input VCF and creates an index file for it in the same location. * This is done so that we don't need to store the index file in the same repo * The copy of the input is done so that it and its index are in the same directory which is typically required. * * @param vcfFile the vcf file to index * @return File a vcf file (index file is created in same path). */ public static File createTemporaryIndexedVcfFromInput(final File vcfFile, final String tempFilePrefix) throws IOException { final String extension; if (vcfFile.getAbsolutePath().endsWith(IOUtil.VCF_FILE_EXTENSION)) extension = IOUtil.VCF_FILE_EXTENSION; else if (vcfFile.getAbsolutePath().endsWith(IOUtil.COMPRESSED_VCF_FILE_EXTENSION)) extension = IOUtil.COMPRESSED_VCF_FILE_EXTENSION; else throw new IllegalArgumentException("couldn't find a " + IOUtil.VCF_FILE_EXTENSION + " or " + IOUtil.COMPRESSED_VCF_FILE_EXTENSION + " ending for input file " + vcfFile.getAbsolutePath()); File output = createTemporaryIndexedVcfFile(tempFilePrefix, extension); try (final VCFFileReader in = new VCFFileReader(vcfFile, false); final VariantContextWriter out = new VariantContextWriterBuilder(). setReferenceDictionary(in.getFileHeader().getSequenceDictionary()). setOptions(EnumSet.of(Options.INDEX_ON_THE_FLY)). setOutputFile(output).build()) { out.writeHeader(in.getFileHeader()); for (final VariantContext ctx : in) { out.add(ctx); } } return output; }