Search in sources :

Example 81 with VariantContextWriter

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

the class GATKToolUnitTest method testCreateVCFWriterLenientTrue.

@Test(dataProvider = "createVCFWriterLenientData")
public void testCreateVCFWriterLenientTrue(final File inputFile, final String outputExtension, final String indexExtension, final boolean createIndex, final boolean createMD5) throws IOException {
    final TestGATKToolWithVariants tool = new TestGATKToolWithVariants();
    // verify lenient==true is honored by writing a bad attribute
    final File outputFile = setupVCFWriter(inputFile, outputExtension, tool, createIndex, createMD5, true);
    try (VariantContextWriter writer = tool.createVCFWriter(outputFile)) {
        // write bad attribute succeed with lenient set
        writeHeaderAndBadVariant(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");
}
Also used : VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) File(java.io.File) IndexedFastaSequenceFile(htsjdk.samtools.reference.IndexedFastaSequenceFile) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 82 with VariantContextWriter

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

the class GATKToolUnitTest method testCreateVCFWriterWithNoSequenceDictionary.

@Test(dataProvider = "createVCFWriterData")
public void testCreateVCFWriterWithNoSequenceDictionary(final File inputFile, final String outputExtension, final String indexExtension, final boolean createIndex, final boolean createMD5) throws IOException {
    // verify that a null sequence dictionary still results in a file, but with no index
    final TestGATKVariantToolWithNoSequenceDictionary tool = new TestGATKVariantToolWithNoSequenceDictionary();
    final File outputFile = setupVCFWriter(inputFile, outputExtension, tool, createIndex, createMD5, false);
    final VariantContextWriter writer = tool.createVCFWriter(outputFile);
    writer.close();
    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");
    // always false with no seq dictionary
    Assert.assertEquals(outFileIndex.exists(), false, "An index file should not have been created");
    Assert.assertEquals(outFileMD5.exists(), createMD5, "The createMD5 argument was not honored");
}
Also used : VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) File(java.io.File) IndexedFastaSequenceFile(htsjdk.samtools.reference.IndexedFastaSequenceFile) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 83 with VariantContextWriter

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

the class GATKToolUnitTest method testCreateVCFWriterDefaults.

@Test(dataProvider = "createVCFWriterData")
public void testCreateVCFWriterDefaults(// unused
final File inputFile, final String outputExtension, final String indexExtension, // unused
final boolean createIndex, // unused
final boolean createMD5) throws IOException {
    // create a writer and make sure the default index/md5 params are honored
    final TestGATKToolWithVariants tool = createTestVariantTool(null);
    final File tmpDir = createTempDir("createVCFTest");
    final File outputFile = new File(tmpDir.getAbsolutePath(), "createVCFTest" + outputExtension);
    final VariantContextWriter writer = tool.createVCFWriter(outputFile);
    writer.close();
    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.assertTrue(outFileIndex.exists(), "The index file was not created");
    Assert.assertFalse(outFileMD5.exists(), "An md5 file was created and should not have been");
}
Also used : VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) File(java.io.File) IndexedFastaSequenceFile(htsjdk.samtools.reference.IndexedFastaSequenceFile) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 84 with VariantContextWriter

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

the class SVVCFWriter method writeVariants.

private static void writeVariants(final PipelineOptions pipelineOptions, final String fileName, final List<VariantContext> variantsArrayList, final SAMSequenceDictionary referenceSequenceDictionary) {
    try (final OutputStream outputStream = new BufferedOutputStream(BucketUtils.createFile(fileName))) {
        final VariantContextWriter vcfWriter = getVariantContextWriter(outputStream, referenceSequenceDictionary);
        vcfWriter.writeHeader(getVcfHeader(referenceSequenceDictionary));
        variantsArrayList.forEach(vcfWriter::add);
        vcfWriter.close();
    } catch (final IOException e) {
        throw new GATKException("Could not create output file", e);
    }
}
Also used : OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) IOException(java.io.IOException) GATKException(org.broadinstitute.hellbender.exceptions.GATKException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 85 with VariantContextWriter

use of htsjdk.variant.variantcontext.writer.VariantContextWriter in project hmftools by hartwigmedical.

the class MNVValidatorApplication method processVariants.

private static void processVariants(@NotNull final String filePath, @NotNull final String outputVcf, @NotNull final String tumorBam) {
    final VCFFileReader vcfReader = new VCFFileReader(new File(filePath), false);
    final VariantContextWriter vcfWriter = new VariantContextWriterBuilder().setOutputFile(outputVcf).setReferenceDictionary(vcfReader.getFileHeader().getSequenceDictionary()).build();
    vcfWriter.writeHeader(vcfReader.getFileHeader());
    final MNVValidator validator = ImmutableMNVValidator.of(tumorBam);
    Pair<PotentialMNVRegion, Optional<PotentialMNVRegion>> outputPair = ImmutablePair.of(PotentialMNVRegion.empty(), Optional.empty());
    for (final VariantContext variant : vcfReader) {
        final PotentialMNVRegion potentialMNV = outputPair.getLeft();
        outputPair = MNVDetector.fitsMNVRegion(potentialMNV, variant);
        outputPair.getRight().ifPresent(mnvRegion -> validator.mergeVariants(mnvRegion).forEach(vcfWriter::add));
    }
    validator.mergeVariants(outputPair.getLeft()).forEach(vcfWriter::add);
    vcfWriter.close();
    vcfReader.close();
    LOGGER.info("Written output variants to " + outputVcf);
}
Also used : PotentialMNVRegion(com.hartwig.hmftools.strelka.mnv.PotentialMNVRegion) VariantContextWriterBuilder(htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder) Optional(java.util.Optional) VCFFileReader(htsjdk.variant.vcf.VCFFileReader) ImmutableMNVValidator(com.hartwig.hmftools.strelka.mnv.ImmutableMNVValidator) MNVValidator(com.hartwig.hmftools.strelka.mnv.MNVValidator) VariantContext(htsjdk.variant.variantcontext.VariantContext) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) File(java.io.File)

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