Search in sources :

Example 1 with Options

use of htsjdk.variant.variantcontext.writer.Options in project gatk by broadinstitute.

the class RenameSampleInVcf method doWork.

@Override
protected Object doWork() {
    IOUtil.assertFileIsReadable(INPUT);
    IOUtil.assertFileIsWritable(OUTPUT);
    try (final VCFFileReader in = new VCFFileReader(INPUT)) {
        final VCFHeader header = in.getFileHeader();
        Utils.validateArg(header.getGenotypeSamples().size() == 1, "Input VCF must be single-sample.");
        Utils.validateArg(OLD_SAMPLE_NAME == null || OLD_SAMPLE_NAME.equals(header.getGenotypeSamples().get(0)), () -> "Input VCF did not contain expected sample. Contained: " + header.getGenotypeSamples().get(0));
        final EnumSet<Options> options = EnumSet.copyOf(VariantContextWriterBuilder.DEFAULT_OPTIONS);
        if (CREATE_INDEX)
            options.add(Options.INDEX_ON_THE_FLY);
        else
            options.remove(Options.INDEX_ON_THE_FLY);
        final VCFHeader outHeader = new VCFHeader(header.getMetaDataInInputOrder(), CollectionUtil.makeList(NEW_SAMPLE_NAME));
        try (final VariantContextWriter out = new VariantContextWriterBuilder().setOutputFile(OUTPUT).setReferenceDictionary(outHeader.getSequenceDictionary()).setOptions(options).build()) {
            out.writeHeader(outHeader);
            for (final VariantContext ctx : in) {
                out.add(ctx);
            }
        }
    }
    return null;
}
Also used : Options(htsjdk.variant.variantcontext.writer.Options) VariantContextWriterBuilder(htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder) VCFFileReader(htsjdk.variant.vcf.VCFFileReader) VariantContext(htsjdk.variant.variantcontext.VariantContext) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VCFHeader(htsjdk.variant.vcf.VCFHeader)

Example 2 with Options

use of htsjdk.variant.variantcontext.writer.Options in project gatk by broadinstitute.

the class SortVcf method writeSortedOutput.

private void writeSortedOutput(final VCFHeader outputHeader, final SortingCollection<VariantContext> sortedOutput) {
    final ProgressLogger writeProgress = new ProgressLogger(logger, 25000, "wrote", "records");
    final EnumSet<Options> options = CREATE_INDEX ? EnumSet.of(Options.INDEX_ON_THE_FLY) : EnumSet.noneOf(Options.class);
    final VariantContextWriter out = new VariantContextWriterBuilder().setReferenceDictionary(outputHeader.getSequenceDictionary()).setOptions(options).setOutputFile(OUTPUT).build();
    out.writeHeader(outputHeader);
    for (final VariantContext variantContext : sortedOutput) {
        out.add(variantContext);
        writeProgress.record(variantContext.getContig(), variantContext.getStart());
    }
    out.close();
}
Also used : Options(htsjdk.variant.variantcontext.writer.Options) VariantContextWriterBuilder(htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder) VariantContext(htsjdk.variant.variantcontext.VariantContext) ProgressLogger(org.broadinstitute.hellbender.utils.runtime.ProgressLogger) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter)

Example 3 with Options

use of htsjdk.variant.variantcontext.writer.Options in project gatk by broadinstitute.

the class GATKVariantContextUtils method createVCFWriter.

/**
     * Creates a VariantContextWriter whose outputFile type is based on the extension of the output file name.
     * The default options set by VariantContextWriter are cleared before applying ALLOW_MISSING_FIELDS_IN_HEADER (if
     * <code>lenientProcessing</code> is set), followed by the set of options specified by any <code>options</code> args.
     *
     * @param outFile output File for this writer. May not be null.
     * @param referenceDictionary required if on the fly indexing is set, otherwise can be null
     * @param createMD5 true if an md5 file should be created
     * @param options variable length list of additional Options to be set for this writer
     * @returns VariantContextWriter must be closed by the caller
     */
public static VariantContextWriter createVCFWriter(final File outFile, final SAMSequenceDictionary referenceDictionary, final boolean createMD5, final Options... options) {
    Utils.nonNull(outFile);
    VariantContextWriterBuilder vcWriterBuilder = new VariantContextWriterBuilder().clearOptions().setOutputFile(outFile);
    if (VariantContextWriterBuilder.OutputType.UNSPECIFIED == getVariantFileTypeFromExtension(outFile)) {
        // the only way the user has to specify an output type is by file extension, and htsjdk
        // throws if it can't map the file extension to a known vcf type, so fallback to a default
        // of VCF
        logger.warn(String.format("Can't determine output variant file format from output file extension \"%s\". Defaulting to VCF.", FilenameUtils.getExtension(outFile.getPath())));
        vcWriterBuilder = vcWriterBuilder.setOutputFileType(VariantContextWriterBuilder.OutputType.VCF);
    }
    if (createMD5) {
        vcWriterBuilder.setCreateMD5();
    }
    if (null != referenceDictionary) {
        vcWriterBuilder = vcWriterBuilder.setReferenceDictionary(referenceDictionary);
    }
    for (Options opt : options) {
        vcWriterBuilder = vcWriterBuilder.setOption(opt);
    }
    return vcWriterBuilder.build();
}
Also used : Options(htsjdk.variant.variantcontext.writer.Options) VariantContextWriterBuilder(htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder)

Example 4 with Options

use of htsjdk.variant.variantcontext.writer.Options in project gatk by broadinstitute.

the class GATKTool method createVCFWriter.

/**
     * Creates a VariantContextWriter whose outputFile type is determined by
     * the outFile's extension, using the best available sequence dictionary for
     * this tool, and default index, leniency and md5 generation settings.
     *
     * @param outFile output File for this writer. May not be null.
     * @returns VariantContextWriter must be closed by the caller
     */
public VariantContextWriter createVCFWriter(final File outFile) {
    Utils.nonNull(outFile);
    final SAMSequenceDictionary sequenceDictionary = getBestAvailableSequenceDictionary();
    List<Options> options = new ArrayList<>();
    if (lenientVCFProcessing) {
        options.add(Options.ALLOW_MISSING_FIELDS_IN_HEADER);
    }
    if (createOutputVariantIndex) {
        if (null == sequenceDictionary) {
            logger.warn("An variant index will not be created - a sequence dictionary is required to create an output index");
        // fall through and create without an index
        } else {
            options.add(Options.INDEX_ON_THE_FLY);
        }
    }
    return GATKVariantContextUtils.createVCFWriter(outFile, sequenceDictionary, createOutputVariantMD5, options.toArray(new Options[options.size()]));
}
Also used : Options(htsjdk.variant.variantcontext.writer.Options) ArrayList(java.util.ArrayList) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary)

Example 5 with Options

use of htsjdk.variant.variantcontext.writer.Options in project gatk by broadinstitute.

the class SVVCFWriter method getVariantContextWriter.

private static VariantContextWriter getVariantContextWriter(final OutputStream outputStream, final SAMSequenceDictionary referenceSequenceDictionary) {
    VariantContextWriterBuilder vcWriterBuilder = new VariantContextWriterBuilder().clearOptions().setOutputStream(outputStream);
    if (null != referenceSequenceDictionary) {
        vcWriterBuilder = vcWriterBuilder.setReferenceDictionary(referenceSequenceDictionary);
    }
    // todo: remove this when things are solid?
    vcWriterBuilder = vcWriterBuilder.setOption(Options.ALLOW_MISSING_FIELDS_IN_HEADER);
    for (final Options opt : new Options[] {}) {
        vcWriterBuilder = vcWriterBuilder.setOption(opt);
    }
    return vcWriterBuilder.build();
}
Also used : Options(htsjdk.variant.variantcontext.writer.Options) PipelineOptions(com.google.cloud.dataflow.sdk.options.PipelineOptions) VariantContextWriterBuilder(htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder)

Aggregations

Options (htsjdk.variant.variantcontext.writer.Options)9 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)6 VariantContextWriterBuilder (htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder)5 VariantContext (htsjdk.variant.variantcontext.VariantContext)3 File (java.io.File)3 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)3 Test (org.testng.annotations.Test)3 VCFHeader (htsjdk.variant.vcf.VCFHeader)2 ProgressLogger (org.broadinstitute.hellbender.utils.runtime.ProgressLogger)2 PipelineOptions (com.google.cloud.dataflow.sdk.options.PipelineOptions)1 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)1 RuntimeIOException (htsjdk.samtools.util.RuntimeIOException)1 VariantContextComparator (htsjdk.variant.variantcontext.VariantContextComparator)1 VCFFileReader (htsjdk.variant.vcf.VCFFileReader)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 UserException (org.broadinstitute.hellbender.exceptions.UserException)1