Search in sources :

Example 11 with VCFFileReader

use of htsjdk.variant.vcf.VCFFileReader in project gatk by broadinstitute.

the class FilterVcfTest method testNoFiltering.

/** Tests that all records get PASS set as their filter when extreme values are used for filtering. */
@Test
public void testNoFiltering() throws Exception {
    final File out = testFiltering(INPUT, 0, 0, 0, Double.MAX_VALUE);
    final VCFFileReader in = new VCFFileReader(out, false);
    for (final VariantContext ctx : in) {
        if (!ctx.filtersWereApplied() || ctx.isFiltered()) {
            Assert.fail("Context should not have been filtered: " + ctx.toString());
        }
    }
}
Also used : VCFFileReader(htsjdk.variant.vcf.VCFFileReader) VariantContext(htsjdk.variant.variantcontext.VariantContext) File(java.io.File) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest)

Example 12 with VCFFileReader

use of htsjdk.variant.vcf.VCFFileReader in project gatk-protected by broadinstitute.

the class CreateSomaticPanelOfNormals method doWork.

public Object doWork() {
    final List<File> inputVcfs = new ArrayList<>(vcfs);
    final Collection<CloseableIterator<VariantContext>> iterators = new ArrayList<>(inputVcfs.size());
    final Collection<VCFHeader> headers = new HashSet<>(inputVcfs.size());
    final VCFHeader headerOfFirstVcf = new VCFFileReader(inputVcfs.get(0), false).getFileHeader();
    final SAMSequenceDictionary sequenceDictionary = headerOfFirstVcf.getSequenceDictionary();
    final VariantContextComparator comparator = headerOfFirstVcf.getVCFRecordComparator();
    for (final File vcf : inputVcfs) {
        final VCFFileReader reader = new VCFFileReader(vcf, false);
        iterators.add(reader.iterator());
        final VCFHeader header = reader.getFileHeader();
        Utils.validateArg(comparator.isCompatible(header.getContigLines()), () -> vcf.getAbsolutePath() + " has incompatible contigs.");
        headers.add(header);
    }
    final VariantContextWriter writer = GATKVariantContextUtils.createVCFWriter(outputVcf, sequenceDictionary, false, Options.INDEX_ON_THE_FLY);
    writer.writeHeader(new VCFHeader(VCFUtils.smartMergeHeaders(headers, false)));
    final MergingIterator<VariantContext> mergingIterator = new MergingIterator<>(comparator, iterators);
    SimpleInterval currentPosition = new SimpleInterval("FAKE", 1, 1);
    final List<VariantContext> variantsAtThisPosition = new ArrayList<>(20);
    while (mergingIterator.hasNext()) {
        final VariantContext vc = mergingIterator.next();
        if (!currentPosition.overlaps(vc)) {
            processVariantsAtSamePosition(variantsAtThisPosition, writer);
            variantsAtThisPosition.clear();
            currentPosition = new SimpleInterval(vc.getContig(), vc.getStart(), vc.getStart());
        }
        variantsAtThisPosition.add(vc);
    }
    mergingIterator.close();
    writer.close();
    return "SUCCESS";
}
Also used : CloseableIterator(htsjdk.samtools.util.CloseableIterator) VCFFileReader(htsjdk.variant.vcf.VCFFileReader) VariantContext(htsjdk.variant.variantcontext.VariantContext) VariantContextComparator(htsjdk.variant.variantcontext.VariantContextComparator) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) MergingIterator(htsjdk.samtools.util.MergingIterator) SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VCFHeader(htsjdk.variant.vcf.VCFHeader) File(java.io.File)

Example 13 with VCFFileReader

use of htsjdk.variant.vcf.VCFFileReader in project gatk by broadinstitute.

the class GATKToolUnitTest method testFeaturesHeader.

@Test
public void testFeaturesHeader() throws Exception {
    final TestGATKToolWithFeatures tool = new TestGATKToolWithFeatures();
    final CommandLineParser clp = new CommandLineArgumentParser(tool);
    final File vcfFile = new File(publicTestDir + "org/broadinstitute/hellbender/engine/feature_data_source_test_with_bigHeader.vcf");
    final String[] args = { "--mask", vcfFile.getCanonicalPath() };
    clp.parseArguments(System.out, args);
    tool.onStartup();
    final Object headerForFeatures = tool.getHeaderForFeatures(tool.mask);
    Assert.assertTrue(headerForFeatures instanceof VCFHeader);
    final VCFHeader vcfheaderForFeatures = (VCFHeader) headerForFeatures;
    try (final VCFFileReader vcfReader = new VCFFileReader(vcfFile, false)) {
        //read the file directly and compare headers
        final VCFHeader vcfFileHeader = vcfReader.getFileHeader();
        Assert.assertEquals(vcfheaderForFeatures.getGenotypeSamples(), vcfFileHeader.getGenotypeSamples());
        Assert.assertEquals(vcfheaderForFeatures.getInfoHeaderLines(), vcfFileHeader.getInfoHeaderLines());
        Assert.assertEquals(vcfheaderForFeatures.getFormatHeaderLines(), vcfFileHeader.getFormatHeaderLines());
        Assert.assertEquals(vcfheaderForFeatures.getFilterLines(), vcfFileHeader.getFilterLines());
        Assert.assertEquals(vcfheaderForFeatures.getContigLines(), vcfFileHeader.getContigLines());
        Assert.assertEquals(vcfheaderForFeatures.getOtherHeaderLines(), vcfFileHeader.getOtherHeaderLines());
    }
    tool.doWork();
    tool.onShutdown();
}
Also used : CommandLineArgumentParser(org.broadinstitute.barclay.argparser.CommandLineArgumentParser) VCFFileReader(htsjdk.variant.vcf.VCFFileReader) CommandLineParser(org.broadinstitute.barclay.argparser.CommandLineParser) VCFHeader(htsjdk.variant.vcf.VCFHeader) File(java.io.File) IndexedFastaSequenceFile(htsjdk.samtools.reference.IndexedFastaSequenceFile) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 14 with VCFFileReader

use of htsjdk.variant.vcf.VCFFileReader in project gatk by broadinstitute.

the class GATKToolUnitTest method testBestSequenceDictionary_fromVariants.

@Test
public void testBestSequenceDictionary_fromVariants() throws Exception {
    final GATKTool tool = new TestGATKToolWithFeatures();
    final CommandLineParser clp = new CommandLineArgumentParser(tool);
    final File vcfFile = new File(publicTestDir + "org/broadinstitute/hellbender/engine/feature_data_source_test_withSequenceDict.vcf");
    final String[] args = { "--mask", vcfFile.getCanonicalPath() };
    clp.parseArguments(System.out, args);
    tool.onStartup();
    //read the dict back in and compare to vcf dict
    final SAMSequenceDictionary toolDict = tool.getBestAvailableSequenceDictionary();
    try (final VCFFileReader reader = new VCFFileReader(vcfFile)) {
        final SAMSequenceDictionary vcfDict = reader.getFileHeader().getSequenceDictionary();
        toolDict.assertSameDictionary(vcfDict);
        vcfDict.assertSameDictionary(toolDict);
        Assert.assertEquals(toolDict, vcfDict);
    }
}
Also used : CommandLineArgumentParser(org.broadinstitute.barclay.argparser.CommandLineArgumentParser) VCFFileReader(htsjdk.variant.vcf.VCFFileReader) CommandLineParser(org.broadinstitute.barclay.argparser.CommandLineParser) File(java.io.File) IndexedFastaSequenceFile(htsjdk.samtools.reference.IndexedFastaSequenceFile) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 15 with VCFFileReader

use of htsjdk.variant.vcf.VCFFileReader in project gatk by broadinstitute.

the class SortVcfTest method validateSortingResults.

/**
     * Checks the ordering and total number of variant context entries in the specified output VCF file.
     * Does NOT check explicitly that the VC genomic positions match exactly those from the inputs. We assume this behavior from other tests.
     *
     * @param output VCF file representing the output of SortVCF
     * @param expectedVariantContextCount the total number of variant context entries from all input files that were merged/sorted
     */
private void validateSortingResults(final File output, final int expectedVariantContextCount) {
    final VCFFileReader outputReader = new VCFFileReader(output, false);
    final VariantContextComparator outputComparator = outputReader.getFileHeader().getVCFRecordComparator();
    VariantContext last = null;
    int variantContextCount = 0;
    try (final CloseableIterator<VariantContext> iterator = outputReader.iterator()) {
        while (iterator.hasNext()) {
            final VariantContext outputContext = iterator.next();
            if (last != null)
                Assert.assertTrue(outputComparator.compare(last, outputContext) <= 0);
            last = outputContext;
            variantContextCount++;
        }
    }
    Assert.assertEquals(variantContextCount, expectedVariantContextCount);
}
Also used : VCFFileReader(htsjdk.variant.vcf.VCFFileReader) VariantContext(htsjdk.variant.variantcontext.VariantContext) VariantContextComparator(htsjdk.variant.variantcontext.VariantContextComparator)

Aggregations

VCFFileReader (htsjdk.variant.vcf.VCFFileReader)23 VariantContext (htsjdk.variant.variantcontext.VariantContext)16 File (java.io.File)11 VCFHeader (htsjdk.variant.vcf.VCFHeader)10 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)8 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)8 VariantContextComparator (htsjdk.variant.variantcontext.VariantContextComparator)6 VariantContextWriterBuilder (htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder)6 ProgressLogger (org.broadinstitute.hellbender.utils.runtime.ProgressLogger)5 ArrayList (java.util.ArrayList)4 UserException (org.broadinstitute.hellbender.exceptions.UserException)4 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)4 Test (org.testng.annotations.Test)4 CloseableIterator (htsjdk.samtools.util.CloseableIterator)3 MergingIterator (htsjdk.samtools.util.MergingIterator)3 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)3 IndexedFastaSequenceFile (htsjdk.samtools.reference.IndexedFastaSequenceFile)2 Genotype (htsjdk.variant.variantcontext.Genotype)2 VCFFilterHeaderLine (htsjdk.variant.vcf.VCFFilterHeaderLine)2 VCFRecordCodec (htsjdk.variant.vcf.VCFRecordCodec)2