@Override public VariantContextWriter makeWriter(final File file, final EnumSet<Options> baseOptions) { return new VariantContextWriterBuilder() .setOutputFile(file) .setReferenceDictionary(dictionary) .setOptions(baseOptions) .build(); }
/** * Create a VariantContextWriter. * * @param outputStream Output stream to write. * @param sequenceDictionary SAM sequence directory (it can be got from VCFHeader) * @param options Writer options * @return The variant context writer */ public static VariantContextWriter createVariantContextWriter(OutputStream outputStream, SAMSequenceDictionary sequenceDictionary, Options ...options) { // setup writer VariantContextWriterBuilder builder = new VariantContextWriterBuilder() .setOutputStream(outputStream) .setReferenceDictionary(sequenceDictionary) .setOption(Options.WRITE_FULL_FORMAT_FIELD) .unsetOption(Options.INDEX_ON_THE_FLY); // options if (options != null) { for (Options option : options) { builder.setOption(option); } } return builder.build(); }
/** * Converts a list of variants into a VCF file, given a {@link VCFHeader} * and an {@link OutputStream}. * <p> This function uses HTSJDK to create {@link VariantCall} objects then * writes them to the given output stream. Note that this implementation depends * heavily on HTSJDK and makes the same assumptions as HTSJDK (e.g. integer values GQ). * * @param header The header to use to generate the output VCF * @param variants A list of variants to encode in the output VCF * @param os The output stream to which to write the generated VCF */ public static void convertVariantToVcf(VCFHeader header, List<Variant> variants, OutputStream os, boolean writeHeader) { if (vcfWriter == null) { vcfWriter = new VariantContextWriterBuilder().clearOptions() .setOutputVCFStream(os).build(); } if (writeHeader) { vcfWriter.writeHeader(header); } for (Variant currVariant : variants) { vcfWriter.add(getContextFromVariant(header, currVariant)); } } }
protected VariantContextWriter createVariantContextWriter(Configuration conf, OutputStream out) { return new VariantContextWriterBuilder().clearOptions() .setOption(Options.FORCE_BCF) .setOutputBCFStream(out).build(); }
protected VariantContextWriter createVariantContextWriter(Configuration conf, OutputStream out) { return new VariantContextWriterBuilder().clearOptions() .setOutputStream(out).build(); }
/** * Set or unset option depending on the boolean given * @param option the option to modify * @param setIt true to set the option, false to unset it. * @return this <code>VariantContextWriterBuilder</code> */ public VariantContextWriterBuilder modifyOption(final Options option, final boolean setIt) { return (setIt) ? this.setOption(option) : this.unsetOption(option); }
@Test public void testSetOutputStream() { final OutputStream stream = new ByteArrayOutputStream(); final VariantContextWriterBuilder builder = new VariantContextWriterBuilder() .setReferenceDictionary(dictionary) .unsetOption(Options.INDEX_ON_THE_FLY) .setOutputStream(stream); VariantContextWriter writer = builder.build(); Assert.assertTrue(writer instanceof VCFWriter, "testSetOutputStream default"); writer = builder.setOption(Options.FORCE_BCF).build(); Assert.assertTrue(writer instanceof BCF2Writer, "testSetOutputStream FORCE_BCF set -> expected BCF stream, was VCF stream"); // test that FORCE_BCF remains in effect, overriding the explicit setting of VCF writer = builder.setOutputVCFStream(stream).build(); Assert.assertTrue(writer instanceof BCF2Writer, "testSetOutputStream FORCE_BCF set 2 -> expected BCF stream, was VCF stream"); writer = builder.unsetOption(Options.FORCE_BCF).build(); Assert.assertTrue(writer instanceof VCFWriter, "testSetOutputStream FORCE_BCF unset -> expected VCF stream, was BCF stream"); writer = builder.setOutputBCFStream(stream).build(); Assert.assertTrue(writer instanceof BCF2Writer, "testSetOutputStream BCF"); writer = builder.setOutputVCFStream(stream).build(); Assert.assertTrue(writer instanceof VCFWriter, "testSetOutputStream VCF"); }
@Test public void testBuffering() { final VariantContextWriterBuilder builder = new VariantContextWriterBuilder() .setReferenceDictionary(dictionary) .setOutputFile(vcf) .unsetOption(Options.INDEX_ON_THE_FLY); // so the potential BufferedOutputStream is not wrapped in a PositionalOutputStream VariantContextWriter writer = builder.build(); Assert.assertTrue(((VCFWriter) writer).getOutputStream() instanceof BufferedOutputStream, "testBuffering was not buffered by default"); writer = builder.unsetBuffering().build(); Assert.assertFalse(((VCFWriter) writer).getOutputStream() instanceof BufferedOutputStream, "testBuffering was buffered when unset"); writer = builder.setBuffer(8192).build(); Assert.assertTrue(((VCFWriter) writer).getOutputStream() instanceof BufferedOutputStream, "testBuffering was not buffered when set"); }
public VariantContextWriterBuilder getVariantContextWriterBuilder(File output, boolean createIndex) { VariantContextWriterBuilder builder = new VariantContextWriterBuilder() .setOutputFile(output) .setReferenceDictionary(getReference().getSequenceDictionary()); builder.clearOptions(); if (createIndex) { builder.setOption(Options.INDEX_ON_THE_FLY); } else { builder.clearIndexCreator(); } return builder; } /**
/** * Constructs an object which will write directly into the output file provided by the stub. * Intentionally delaying the writing of the header -- this should be filled in by the walker. * * Respecs the isCompressed() request in stub, so if isCompressed() is true then this * will create a storage output that dumps output to a BlockCompressedOutputStream. * * @param stub Stub to use when constructing the output file. */ public VariantContextWriterStorage(VariantContextWriterStub stub) { if ( stub.getOutputFile() != null ) { this.file = stub.getOutputFile(); writer = vcfWriterToFile(stub,stub.getOutputFile(),true,true); } else if ( stub.getOutputStream() != null ) { this.file = null; this.stream = stub.getOutputStream(); writer = new VariantContextWriterBuilder() .setOutputVCFStream(stream) .setReferenceDictionary(stub.getMasterSequenceDictionary()) .setOptions(stub.getWriterOptions(false)) .build(); } else throw new ReviewedGATKException("Unable to create target to which to write; storage was provided with neither a file nor a stream."); }
/** * Common parts of {@link VariantContextWriterBuilder} creation for the openVariantContextWriter functions. */ private static VariantContextWriterBuilder makeBuilder(VCFHeader header) { // construct factory object for VariantContextWriter VariantContextWriterBuilder builder = new VariantContextWriterBuilder(); builder.setReferenceDictionary(header.getSequenceDictionary()); // Be more lenient in missing header fields. builder.setOption(Options.ALLOW_MISSING_FIELDS_IN_HEADER); // Disable on-the-fly generation of Tribble index if the input file does not have a sequence dictionary. if (header.getSequenceDictionary() == null) builder.unsetOption(Options.INDEX_ON_THE_FLY); return builder; }
@Test public void testIndexingOnTheFlyForPath() throws IOException { final VariantContextWriterBuilder builder = new VariantContextWriterBuilder() .setReferenceDictionary(dictionary) .setOption(Options.INDEX_ON_THE_FLY); try(final VariantContextWriter writer = builder.setOutputPath(vcfPath).build()) try(final VariantContextWriter writer = builder.setOutputPath(bcfPath).build()){ try(VariantContextWriter writer = builder.setOutputPath(blockCompressedPath).setReferenceDictionary(dictionary).build()){ try (VariantContextWriter writer = builder.setReferenceDictionary(null).build()){
private void innerWriteToFifo(String pathToFifo) { // Do not enable INDEX_OF_THE_FLY because that is not compatible with writing to a pipe. final VariantContextWriterBuilder builder = new VariantContextWriterBuilder() .clearOptions() .setReferenceDictionary(dictionary); Path vcfPath = Paths.get(pathToFifo); VariantContextWriter writer = builder.setOutputPath(vcfPath).build(); writer.writeHeader(new VCFHeader()); writer.close(); }
mVCWriter = new VariantContextWriterBuilder() .setOutputBCFStream(mStream) .unsetOption(Options.INDEX_ON_THE_FLY).build(); break; case VCF_STREAM: mVCWriter = new VariantContextWriterBuilder() .setOutputVCFStream(mStream) .unsetOption(Options.INDEX_ON_THE_FLY).build(); break; default:
@Test public void testStdOut() { final VariantContextWriter writer = new VariantContextWriterBuilder().setOutputFile("/dev/stdout").clearOptions().build(); OutputStream s = ((VCFWriter) writer).getOutputStream(); Assert.assertNotNull(((VCFWriter) writer).getOutputStream()); Assert.assertNotEquals(((VCFWriter) writer).getStreamName(), IndexingVariantContextWriter.DEFAULT_READER_NAME); }
/** * Return a new {@link VariantContextWriter} that uses the header from <code>reader</code> but has the header * extended header through {@link #extendHeaderFields}. * * @param header * the VCF header to use for the construction * @param fileName * path to output file * @param additionalHeaderLines * additional {@link VCFHeaderLine}s to add * @param generateIndex * whether or not to generate an index * @return A correct writer for variantContexts */ public static VariantContextWriter openVariantContextWriter(VCFHeader header, String fileName, Collection<VCFHeaderLine> additionalHeaderLines, boolean generateIndex) { VariantContextWriterBuilder builder = makeBuilder(header); builder.setOutputFile(new File(fileName)); if (!generateIndex) builder.unsetOption(Options.INDEX_ON_THE_FLY); // construct VariantContextWriter and write out header VariantContextWriter out = builder.build(); final VCFHeader updatedHeader = extendHeaderFields(new VCFHeader(header)); for (VCFHeaderLine headerLine : additionalHeaderLines) updatedHeader.addMetaDataLine(headerLine); out.writeHeader(updatedHeader); return out; }
@Test public void testClearOptions() { // Verify that clearOptions doesn't have a side effect of carrying previously set options // forward to subsequent builders VariantContextWriterBuilder vcwb = new VariantContextWriterBuilder(); vcwb.clearOptions().setOption(Options.INDEX_ON_THE_FLY); final VariantContextWriterBuilder builder = new VariantContextWriterBuilder().clearOptions(); Assert.assertTrue(builder.options.isEmpty()); }
/** * Return a new {@link VariantContextWriter} that uses the header from <code>reader</code> but has the header * extended header through {@link #extendHeaderFields}. * * @param header * the VCF header to use for the construction * @param outStream * {@link OutputStream} to write to * @param additionalHeaderLines * additional {@link VCFHeaderLine}s to add * @return A correct writer for variantContexts */ public static VariantContextWriter openVariantContextWriter(VCFHeader header, OutputStream outStream, Collection<VCFHeaderLine> additionalHeaderLines) { VariantContextWriterBuilder builder = makeBuilder(header); builder.unsetOption(Options.INDEX_ON_THE_FLY); builder.setOutputStream(outStream); // construct VariantContextWriter and write out header VariantContextWriter out = builder.build(); final VCFHeader updatedHeader = extendHeaderFields(new VCFHeader(header)); for (VCFHeaderLine headerLine : additionalHeaderLines) updatedHeader.addMetaDataLine(headerLine); out.writeHeader(updatedHeader); return out; }
@Test public void testModifyOption() { final VariantContextWriterBuilder builder = new VariantContextWriterBuilder().clearOptions(); for (final Options option : Options.values()) { Assert.assertFalse(builder.isOptionSet(option)); // shouldn't be set builder.modifyOption(option, false); Assert.assertFalse(builder.isOptionSet(option)); // still shouldn't be set builder.modifyOption(option, true); Assert.assertTrue(builder.isOptionSet(option)); // now is set builder.modifyOption(option, false); Assert.assertFalse(builder.isOptionSet(option)); // has been unset } }
/** * Validate and build the <code>VariantContextWriter</code>. * * @return the <code>VariantContextWriter</code> as specified by previous method calls. * @throws RuntimeIOException if the writer is configured to write to a file, and the corresponding path does not exist. * @throws IllegalArgumentException if no output file or stream is specified. * @throws IllegalArgumentException if <code>Options.INDEX_ON_THE_FLY</code> is specified and no reference dictionary is provided. * @throws IllegalArgumentException if <code>Options.INDEX_ON_THE_FLY</code> is specified and a stream output is specified. */ public VariantContextWriter build() { return build(EMPTY_OPEN_OPTION_ARRAY); }