Search in sources :

Example 16 with VCFFileReader

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

the class ConvertGSVariantsToSegmentsIntegrationTest method composeExpectedSegments.

private List<HiddenStateSegmentRecord<CopyNumberTriState, Target>> composeExpectedSegments(final File vcf, final TargetCollection<Target> targets) throws IOException {
    final VCFFileReader reader = new VCFFileReader(vcf, false);
    final List<HiddenStateSegmentRecord<CopyNumberTriState, Target>> result = new ArrayList<>();
    reader.iterator().forEachRemaining(vc -> {
        final int targetCount = targets.indexRange(vc).size();
        for (final Genotype genotype : vc.getGenotypes()) {
            final int cn = Integer.parseInt(genotype.getExtendedAttribute("CN").toString());
            final double[] cnp = Stream.of(genotype.getExtendedAttribute("CNP").toString().replaceAll("\\[\\]", "").split(",")).mapToDouble(Double::parseDouble).toArray();
            final double cnpSum = MathUtils.approximateLog10SumLog10(cnp);
            final CopyNumberTriState call = expectedCall(cn);
            final double exactLog10Prob = expectedExactLog10(call, cnp);
            final HiddenStateSegment<CopyNumberTriState, Target> expectedSegment = new HiddenStateSegment<>(new SimpleInterval(vc), targetCount, Double.parseDouble(genotype.getExtendedAttribute("CNF").toString()), 0.000, call, -10.0 * exactLog10Prob, Double.NaN, Double.NaN, Double.NaN, -10.0 * (cnp[ConvertGSVariantsToSegments.NEUTRAL_COPY_NUMBER_DEFAULT] - cnpSum));
            result.add(new HiddenStateSegmentRecord<>(genotype.getSampleName(), expectedSegment));
        }
    });
    return result;
}
Also used : VCFFileReader(htsjdk.variant.vcf.VCFFileReader) ArrayList(java.util.ArrayList) Genotype(htsjdk.variant.variantcontext.Genotype) Target(org.broadinstitute.hellbender.tools.exome.Target) CopyNumberTriState(org.broadinstitute.hellbender.tools.exome.germlinehmm.CopyNumberTriState) HiddenStateSegment(org.broadinstitute.hellbender.utils.hmm.segmentation.HiddenStateSegment) HiddenStateSegmentRecord(org.broadinstitute.hellbender.utils.hmm.segmentation.HiddenStateSegmentRecord) SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval)

Example 17 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 18 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 19 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)

Example 20 with VCFFileReader

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

the class SplitVcfsTest method testSplit.

@Test
public void testSplit() {
    final File indelOutputFile = new File(OUTPUT_DATA_PATH, "split-vcfs-test-indels-delete-me.vcf");
    final File snpOutputFile = new File(OUTPUT_DATA_PATH, "split-vcfs-test-snps-delete-me.vcf");
    final File input = new File(TEST_DATA_PATH, "CEUTrio-merged-indels-snps.vcf");
    indelOutputFile.deleteOnExit();
    snpOutputFile.deleteOnExit();
    final String[] args = new String[] { "--input", input.getAbsolutePath(), "--SNP_OUTPUT", snpOutputFile.getAbsolutePath(), "--INDEL_OUTPUT", indelOutputFile.getAbsolutePath() };
    runCommandLine(args);
    final Queue<String> indelContigPositions = AbstractVcfMergingClpTester.loadContigPositions(indelOutputFile);
    final Queue<String> snpContigPositions = AbstractVcfMergingClpTester.loadContigPositions(snpOutputFile);
    final VCFFileReader reader = new VCFFileReader(input);
    for (final VariantContext inputContext : reader) {
        if (inputContext.isIndel())
            Assert.assertEquals(AbstractVcfMergingClpTester.getContigPosition(inputContext), indelContigPositions.poll());
        if (inputContext.isSNP())
            Assert.assertEquals(AbstractVcfMergingClpTester.getContigPosition(inputContext), snpContigPositions.poll());
    }
    // We should have polled everything off the indel (snp) queues
    Assert.assertEquals(indelContigPositions.size(), 0);
    Assert.assertEquals(snpContigPositions.size(), 0);
}
Also used : VCFFileReader(htsjdk.variant.vcf.VCFFileReader) VariantContext(htsjdk.variant.variantcontext.VariantContext) File(java.io.File) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest)

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