Search in sources :

Example 6 with Options

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

the class GATKVariantContextUtilsUnitTest method testCreateVCFWriterLenientTrue.

@Test(dataProvider = "createVCFWriterLenientData")
public void testCreateVCFWriterLenientTrue(final String outputExtension, final String indexExtension, final boolean createIndex, final boolean createMD5) throws IOException {
    final File tmpDir = createTempDir("createVCFTest");
    final File outputFile = new File(tmpDir.getAbsolutePath(), "createVCFTest" + outputExtension);
    Options[] options = createIndex ? new Options[] { Options.ALLOW_MISSING_FIELDS_IN_HEADER, Options.INDEX_ON_THE_FLY } : new Options[] { Options.ALLOW_MISSING_FIELDS_IN_HEADER };
    try (final VariantContextWriter vcw = GATKVariantContextUtils.createVCFWriter(outputFile, makeSimpleSequenceDictionary(), createMD5, options)) {
        writeHeader(vcw);
        // verify leniency by writing a bogus attribute
        writeBadVariant(vcw);
    }
    final File outFileIndex = new File(outputFile.getAbsolutePath() + indexExtension);
    final File outFileMD5 = new File(outputFile.getAbsolutePath() + ".md5");
    Assert.assertTrue(outputFile.exists(), "No output file was not created");
    Assert.assertEquals(outFileIndex.exists(), createIndex, "The createIndex argument was not honored");
    Assert.assertEquals(outFileMD5.exists(), createMD5, "The createMD5 argument was not honored");
}
Also used : Options(htsjdk.variant.variantcontext.writer.Options) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) File(java.io.File) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 7 with Options

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

the class GatherVcfs method gatherConventionally.

/** Code for gathering multiple VCFs that works regardless of input format and output format, but can be slow. */
private static void gatherConventionally(final SAMSequenceDictionary sequenceDictionary, final boolean createIndex, final List<Path> inputFiles, final File outputFile, final int cloudPrefetchBuffer) {
    final EnumSet<Options> options = EnumSet.copyOf(VariantContextWriterBuilder.DEFAULT_OPTIONS);
    if (createIndex)
        options.add(Options.INDEX_ON_THE_FLY);
    else
        options.remove(Options.INDEX_ON_THE_FLY);
    try (final VariantContextWriter out = new VariantContextWriterBuilder().setOutputFile(outputFile).setReferenceDictionary(sequenceDictionary).setOptions(options).build()) {
        final ProgressLogger progress = new ProgressLogger(log, 10000);
        VariantContext lastContext = null;
        Path lastFile = null;
        VCFHeader firstHeader = null;
        VariantContextComparator comparator = null;
        for (final Path f : inputFiles) {
            try {
                log.debug("Gathering from file: ", f.toUri().toString());
                final FeatureReader<VariantContext> variantReader = getReaderFromVCFUri(f, cloudPrefetchBuffer);
                final PeekableIterator<VariantContext> variantIterator;
                variantIterator = new PeekableIterator<>(variantReader.iterator());
                final VCFHeader header = (VCFHeader) variantReader.getHeader();
                if (firstHeader == null) {
                    firstHeader = header;
                    out.writeHeader(firstHeader);
                    comparator = new VariantContextComparator(firstHeader.getContigLines());
                }
                if (lastContext != null && variantIterator.hasNext()) {
                    final VariantContext vc = variantIterator.peek();
                    if (comparator.compare(vc, lastContext) <= 0) {
                        throw new IllegalStateException("First variant in file " + f.toUri().toString() + " is at " + vc.getSource() + " but last variant in earlier file " + lastFile.toUri().toString() + " is at " + lastContext.getSource());
                    }
                }
                while (variantIterator.hasNext()) {
                    lastContext = variantIterator.next();
                    out.add(lastContext);
                    progress.record(lastContext.getContig(), lastContext.getStart());
                }
                lastFile = f;
                CloserUtil.close(variantIterator);
                CloserUtil.close(variantReader);
            } catch (IOException e) {
                throw new UserException.CouldNotReadInputFile(f, e.getMessage(), e);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) Options(htsjdk.variant.variantcontext.writer.Options) VariantContext(htsjdk.variant.variantcontext.VariantContext) ProgressLogger(org.broadinstitute.hellbender.utils.runtime.ProgressLogger) VariantContextComparator(htsjdk.variant.variantcontext.VariantContextComparator) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) VariantContextWriterBuilder(htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) UserException(org.broadinstitute.hellbender.exceptions.UserException) VCFHeader(htsjdk.variant.vcf.VCFHeader)

Example 8 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 9 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