Search in sources :

Example 71 with VariantContextWriter

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

the class GATKVariantContextUtilsUnitTest method testCreateVCFWriterLenientFalse.

@Test(dataProvider = "createVCFWriterLenientData", expectedExceptions = IllegalStateException.class)
public void testCreateVCFWriterLenientFalse(final String outputExtension, // unused
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.INDEX_ON_THE_FLY } : new Options[] {};
    try (final VariantContextWriter vcw = GATKVariantContextUtils.createVCFWriter(outputFile, makeSimpleSequenceDictionary(), createMD5, options)) {
        writeHeader(vcw);
        // write a bad attribute and throw...
        writeBadVariant(vcw);
    }
}
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 72 with VariantContextWriter

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

the class GATKVariantContextUtilsUnitTest method testCreateVCFWriterWithOptions.

@Test(dataProvider = "createVCFWriterData")
public void testCreateVCFWriterWithOptions(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.INDEX_ON_THE_FLY } : new Options[] {};
    try (final VariantContextWriter writer = GATKVariantContextUtils.createVCFWriter(outputFile, makeSimpleSequenceDictionary(), createMD5, options)) {
        writeHeader(writer);
    }
    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");
    verifyFileType(outputFile, outputExtension);
}
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 73 with VariantContextWriter

use of htsjdk.variant.variantcontext.writer.VariantContextWriter 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 74 with VariantContextWriter

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

the class GVCFWriterUnitTest method writeGVCFToDisk.

@Test(dataProvider = "toWriteToDisk")
public void writeGVCFToDisk(List<VariantContext> variants, List<MinimalData> expected) {
    final List<Integer> gqPartitions = Arrays.asList(1, 10, 30);
    final File outputFile = createTempFile("generated", ".g.vcf");
    try (VariantContextWriter writer = GATKVariantContextUtils.createVCFWriter(outputFile, null, false);
        GVCFWriter gvcfWriter = new GVCFWriter(writer, gqPartitions, HomoSapiensConstants.DEFAULT_PLOIDY)) {
        gvcfWriter.writeHeader(getMinimalVCFHeader());
        variants.forEach(gvcfWriter::add);
    }
    assertGVCFIsParseableAndVariantsMatch(outputFile, expected);
}
Also used : VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) File(java.io.File) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 75 with VariantContextWriter

use of htsjdk.variant.variantcontext.writer.VariantContextWriter 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)

Aggregations

VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)132 File (java.io.File)67 VCFHeader (htsjdk.variant.vcf.VCFHeader)65 VariantContext (htsjdk.variant.variantcontext.VariantContext)60 SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)53 ArrayList (java.util.ArrayList)41 IOException (java.io.IOException)38 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)35 VCFHeaderLine (htsjdk.variant.vcf.VCFHeaderLine)34 HashSet (java.util.HashSet)32 List (java.util.List)29 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)28 Allele (htsjdk.variant.variantcontext.Allele)28 Collectors (java.util.stream.Collectors)27 VcfIterator (com.github.lindenb.jvarkit.util.vcf.VcfIterator)26 VCFFileReader (htsjdk.variant.vcf.VCFFileReader)26 Parameter (com.beust.jcommander.Parameter)24 Logger (com.github.lindenb.jvarkit.util.log.Logger)24 Launcher (com.github.lindenb.jvarkit.util.jcommander.Launcher)23 Program (com.github.lindenb.jvarkit.util.jcommander.Program)23