Search in sources :

Example 26 with VariantContextWriter

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

the class EvaluateCopyNumberTriStateCalls method doWork.

@Override
protected Object doWork() {
    final TargetCollection<Target> targets = targetArguments.readTargetCollection(false);
    final VCFFileReader truthReader = openVCFReader(truthFile);
    final VCFFileReader callsReader = openVCFReader(callsFile);
    final GenotypeEvaluationRecordWriter caseWriter = openGenotypeEvaluationOutputWriter(caseDetailOutputFile);
    if (samples.isEmpty()) {
        samples = composeSetOfSamplesToEvaluate(callsReader);
    }
    final VariantContextWriter outputWriter = openVCFWriter(outputFile, samples);
    final Map<String, EvaluationSampleSummaryRecord> sampleStats = samples.stream().collect(Collectors.toMap(s -> s, EvaluationSampleSummaryRecord::new));
    final List<SimpleInterval> intervals = composeListOfProcessingIntervalsFromInputs(truthReader, callsReader);
    for (final SimpleInterval interval : intervals) {
        for (final VariantEvaluationContext vc : processInterval(truthReader, callsReader, interval, targets)) {
            outputWriter.add(vc);
            outputCases(caseWriter, vc, targets);
            updateSampleStats(sampleStats, vc);
        }
    }
    truthReader.close();
    callsReader.close();
    outputWriter.close();
    closeCaseRecordWriter(caseWriter);
    writeSampleSummaryFile(sampleSummaryOutputFile, sampleStats);
    return "SUCCESS";
}
Also used : Genotype(htsjdk.variant.variantcontext.Genotype) DocumentedFeature(org.broadinstitute.barclay.help.DocumentedFeature) Allele(htsjdk.variant.variantcontext.Allele) htsjdk.variant.vcf(htsjdk.variant.vcf) CommandLineProgramProperties(org.broadinstitute.barclay.argparser.CommandLineProgramProperties) java.util(java.util) CopyNumberProgramGroup(org.broadinstitute.hellbender.cmdline.programgroups.CopyNumberProgramGroup) Argument(org.broadinstitute.barclay.argparser.Argument) VariantContextWriterBuilder(htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder) StandardArgumentDefinitions(org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions) ArgumentCollection(org.broadinstitute.barclay.argparser.ArgumentCollection) TargetArgumentCollection(org.broadinstitute.hellbender.tools.exome.TargetArgumentCollection) Function(java.util.function.Function) Pair(org.apache.commons.lang3.tuple.Pair) StreamSupport(java.util.stream.StreamSupport) TargetCollection(org.broadinstitute.hellbender.tools.exome.TargetCollection) org.broadinstitute.hellbender.utils(org.broadinstitute.hellbender.utils) Locatable(htsjdk.samtools.util.Locatable) CommandLineProgram(org.broadinstitute.hellbender.cmdline.CommandLineProgram) GenotypeBuilder(htsjdk.variant.variantcontext.GenotypeBuilder) IOException(java.io.IOException) CopyNumberTriStateAllele(org.broadinstitute.hellbender.tools.exome.germlinehmm.CopyNumberTriStateAllele) Collectors(java.util.stream.Collectors) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) File(java.io.File) HMMPostProcessor(org.broadinstitute.hellbender.utils.hmm.segmentation.HMMPostProcessor) XHMMSegmentGenotyper(org.broadinstitute.hellbender.tools.exome.germlinehmm.xhmm.XHMMSegmentGenotyper) Stream(java.util.stream.Stream) UserException(org.broadinstitute.hellbender.exceptions.UserException) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) Target(org.broadinstitute.hellbender.tools.exome.Target) XHMMSegmentCaller(org.broadinstitute.hellbender.tools.exome.germlinehmm.xhmm.XHMMSegmentCaller) VariantContext(htsjdk.variant.variantcontext.VariantContext) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Target(org.broadinstitute.hellbender.tools.exome.Target) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter)

Example 27 with VariantContextWriter

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

the class EvaluateCopyNumberTriStateCalls method openVCFWriter.

private VariantContextWriter openVCFWriter(final File outputFile, final Set<String> samples) {
    final VariantContextWriterBuilder builder = new VariantContextWriterBuilder();
    builder.setOutputFile(outputFile);
    builder.clearOptions();
    final VariantContextWriter result = builder.build();
    final VCFHeader header = new VCFHeader(Collections.emptySet(), samples);
    CopyNumberTriStateAllele.addHeaderLinesTo(header);
    EvaluationClass.addHeaderLinesTo(header);
    // Format annotations.
    header.addMetaDataLine(new VCFFormatHeaderLine(VCFConstants.GENOTYPE_KEY, 1, VCFHeaderLineType.Character, "Called genotype"));
    header.addMetaDataLine(new VCFFormatHeaderLine(VariantEvaluationContext.CALL_QUALITY_KEY, 1, VCFHeaderLineType.Float, "Quality of the call"));
    header.addMetaDataLine(new VCFFormatHeaderLine(VariantEvaluationContext.CALLED_SEGMENTS_COUNT_KEY, 1, VCFHeaderLineType.Integer, "Number of called segments that overlap with the truth"));
    header.addMetaDataLine(new VCFFormatHeaderLine(VariantEvaluationContext.CALLED_ALLELE_COUNTS_KEY, VCFHeaderLineCount.G, VCFHeaderLineType.Integer, "Called allele count for mixed calls"));
    header.addMetaDataLine(new VCFFormatHeaderLine(VariantEvaluationContext.TRUTH_COPY_FRACTION_KEY, 1, VCFHeaderLineType.Float, "Truth copy fraction estimated"));
    header.addMetaDataLine(new VCFFormatHeaderLine(VariantEvaluationContext.TRUTH_QUALITY_KEY, 1, VCFHeaderLineType.Float, "Truth call quality"));
    header.addMetaDataLine(new VCFFormatHeaderLine(VariantEvaluationContext.EVALUATION_CLASS_KEY, 1, VCFHeaderLineType.Character, "The evaluation class for the call or lack of call. It the values of the header key '" + EvaluationClass.VCF_HEADER_KEY + "'"));
    header.addMetaDataLine(new VCFFormatHeaderLine(VariantEvaluationContext.TRUTH_GENOTYPE_KEY, 1, VCFHeaderLineType.Character, "The truth genotype"));
    header.addMetaDataLine(new VCFFormatHeaderLine(VariantEvaluationContext.CALLED_TARGET_COUNT_KEY, 1, VCFHeaderLineType.Integer, "Number of targets covered by called segments"));
    header.addMetaDataLine(new VCFFormatHeaderLine(VariantEvaluationContext.CALL_QUALITY_KEY, 1, VCFHeaderLineType.Float, "1 - The probability of th event in Phred scale (the maximum if ther are more than one segment"));
    header.addMetaDataLine(new VCFFormatHeaderLine(VCFConstants.GENOTYPE_QUALITY_KEY, 1, VCFHeaderLineType.Integer, "The quality of the call (the maximum if there are more than one segment"));
    header.addMetaDataLine(new VCFFormatHeaderLine(VCFConstants.GENOTYPE_FILTER_KEY, VCFHeaderLineCount.UNBOUNDED, VCFHeaderLineType.Character, "Genotype filters"));
    // Info annotations.
    header.addMetaDataLine(new VCFInfoHeaderLine(VariantEvaluationContext.TRUTH_ALLELE_FREQUENCY_KEY, VCFHeaderLineCount.A, VCFHeaderLineType.Float, "The frequency of the alternative alleles in the truth callset"));
    header.addMetaDataLine(new VCFInfoHeaderLine(VariantEvaluationContext.TRUTH_ALLELE_NUMBER_KEY, 1, VCFHeaderLineType.Integer, "Total number of called alleles in the truth callset"));
    header.addMetaDataLine(new VCFInfoHeaderLine(VariantEvaluationContext.CALLS_ALLELE_FREQUENCY_KEY, VCFHeaderLineCount.A, VCFHeaderLineType.Float, "The frequency of the alternative alleles in the actual callset"));
    header.addMetaDataLine(new VCFInfoHeaderLine(VariantEvaluationContext.CALLS_ALLELE_NUMBER_KEY, 1, VCFHeaderLineType.Integer, "Total number of called alleles in the actual callset"));
    header.addMetaDataLine(new VCFInfoHeaderLine(VariantEvaluationContext.TRUTH_TARGET_COUNT_KEY, 1, VCFHeaderLineType.Integer, "Number of targets overlapped by this variant"));
    header.addMetaDataLine(new VCFInfoHeaderLine(VCFConstants.END_KEY, 1, VCFHeaderLineType.Integer, "Stop position for the variant"));
    // Filter annotations.
    for (final EvaluationFilter filter : EvaluationFilter.values()) {
        header.addMetaDataLine(new VCFFilterHeaderLine(filter.name(), filter.description));
        header.addMetaDataLine(new VCFFilterHeaderLine(filter.acronym, filter.description));
    }
    header.addMetaDataLine(new VCFFilterHeaderLine(EvaluationFilter.PASS, "Indicates that it passes all filters"));
    result.writeHeader(header);
    return result;
}
Also used : VariantContextWriterBuilder(htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter)

Example 28 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 29 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 30 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)

Aggregations

VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)31 File (java.io.File)19 VariantContext (htsjdk.variant.variantcontext.VariantContext)13 VariantContextWriterBuilder (htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder)12 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)12 Test (org.testng.annotations.Test)12 VCFHeader (htsjdk.variant.vcf.VCFHeader)9 VCFFileReader (htsjdk.variant.vcf.VCFFileReader)8 UserException (org.broadinstitute.hellbender.exceptions.UserException)8 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)7 Options (htsjdk.variant.variantcontext.writer.Options)6 ProgressLogger (org.broadinstitute.hellbender.utils.runtime.ProgressLogger)6 IOException (java.io.IOException)5 IndexedFastaSequenceFile (htsjdk.samtools.reference.IndexedFastaSequenceFile)4 ReferenceSequenceFile (htsjdk.samtools.reference.ReferenceSequenceFile)4 VariantContextComparator (htsjdk.variant.variantcontext.VariantContextComparator)4 CloseableIterator (htsjdk.samtools.util.CloseableIterator)3 MergingIterator (htsjdk.samtools.util.MergingIterator)3 Function (java.util.function.Function)3 Collectors (java.util.stream.Collectors)3