Search in sources :

Example 16 with FeatureDataSource

use of org.broadinstitute.hellbender.engine.FeatureDataSource in project gatk-protected by broadinstitute.

the class FilterByOrientationBiasIntegrationTest method testRun.

@Test
public void testRun() throws IOException {
    final File outputFile = File.createTempFile("ob_", ".vcf");
    final List<String> arguments = new ArrayList<>();
    arguments.add("-" + FilterByOrientationBias.PRE_ADAPTER_METRICS_DETAIL_FILE_SHORT_NAME);
    arguments.add(preAdapterQFile);
    arguments.add("-" + StandardArgumentDefinitions.VARIANT_SHORT_NAME);
    arguments.add(smallM2VcfMore);
    arguments.add("-" + StandardArgumentDefinitions.OUTPUT_SHORT_NAME);
    arguments.add(outputFile.getAbsolutePath());
    runCommandLine(arguments);
    Assert.assertTrue(outputFile.exists());
    final File summaryFile = new File(outputFile.getAbsolutePath() + FilterByOrientationBias.SUMMARY_FILE_SUFFIX);
    Assert.assertTrue(summaryFile.exists());
    final List<VariantContext> variantContexts = new ArrayList<>();
    final FeatureDataSource<VariantContext> featureDataSource = new FeatureDataSource<>(outputFile);
    for (final VariantContext vc : featureDataSource) {
        variantContexts.add(vc);
    }
    Assert.assertEquals(variantContexts.size(), 11);
    Assert.assertTrue(FileUtils.sizeOf(outputFile) > 0);
    Assert.assertTrue(FileUtils.sizeOf(summaryFile) > 0);
    boolean is_variant_context_tested = false;
    //  Also, make sure that the variant context has the filter as well.  Not just the genotypes.
    for (final VariantContext vc : variantContexts) {
        final Genotype tumorGenotype = vc.getGenotype("TUMOR");
        Assert.assertTrue((tumorGenotype.getFilters() == null) || (tumorGenotype.getFilters().contains(OrientationBiasFilterConstants.IS_ORIENTATION_BIAS_CUT)) || !OrientationBiasUtils.isGenotypeInTransitionWithComplement(tumorGenotype, Transition.transitionOf('G', 'T')));
        // If we see a filtered genotype, make sure the variant context was filtered as well.
        if ((tumorGenotype.getFilters() != null) && (tumorGenotype.getFilters().contains(OrientationBiasFilterConstants.IS_ORIENTATION_BIAS_CUT))) {
            Assert.assertTrue(vc.getFilters().contains(OrientationBiasFilterConstants.IS_ORIENTATION_BIAS_CUT));
            is_variant_context_tested = true;
        }
        final Genotype normalGenotype = vc.getGenotype("NORMAL");
        Assert.assertTrue((normalGenotype.getFilters() == null) || normalGenotype.getFilters().equals(VCFConstants.UNFILTERED) || normalGenotype.getFilters().equals(VCFConstants.PASSES_FILTERS_v4));
    }
    Assert.assertTrue(is_variant_context_tested, "Unit test may be broken.  Should have tested that variant context contained filter as well as genotype fields.");
    final List<OrientationSampleTransitionSummary> summaries = OrientationBiasUtils.readOrientationBiasSummaryTable(summaryFile);
    Assert.assertEquals(summaries.size(), 2);
    Assert.assertEquals(summaries.stream().filter(s -> s.getSample().equals("NORMAL")).count(), 1);
    Assert.assertEquals(summaries.stream().filter(s -> s.getSample().equals("TUMOR")).count(), 1);
    Assert.assertEquals(summaries.stream().filter(s -> s.getSample().equals("NORMAL")).map(s -> s.getArtifactMode()).filter(am -> am.equals(Transition.GtoT)).count(), 1);
    Assert.assertEquals(summaries.stream().filter(s -> s.getSample().equals("NORMAL")).map(s -> s.getArtifactModeComplement()).filter(am -> am.equals(Transition.CtoA)).count(), 1);
    Assert.assertEquals(summaries.stream().filter(s -> s.getSample().equals("TUMOR")).map(s -> s.getArtifactMode()).filter(am -> am.equals(Transition.GtoT)).count(), 1);
    Assert.assertEquals(summaries.stream().filter(s -> s.getSample().equals("TUMOR")).map(s -> s.getArtifactModeComplement()).filter(am -> am.equals(Transition.CtoA)).count(), 1);
    Assert.assertEquals(summaries.stream().filter(s -> s.getArtifactModeComplement().equals(s.getArtifactMode().complement())).count(), summaries.size());
    Assert.assertEquals(summaries.stream().filter(s -> s.getSample().equals("TUMOR")).map(s -> s.getArtifactModeComplement()).filter(am -> am.equals(Transition.CtoA)).count(), 1);
}
Also used : Genotype(htsjdk.variant.variantcontext.Genotype) FileUtils(org.apache.commons.io.FileUtils) StandardArgumentDefinitions(org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions) Test(org.testng.annotations.Test) IOException(java.io.IOException) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest) OrientationBiasFilterConstants(org.broadinstitute.hellbender.tools.exome.orientationbiasvariantfilter.OrientationBiasFilterConstants) File(java.io.File) OrientationSampleTransitionSummary(org.broadinstitute.hellbender.tools.exome.orientationbiasvariantfilter.OrientationSampleTransitionSummary) ArrayList(java.util.ArrayList) List(java.util.List) Assert(org.testng.Assert) FeatureDataSource(org.broadinstitute.hellbender.engine.FeatureDataSource) VariantContext(htsjdk.variant.variantcontext.VariantContext) Transition(org.broadinstitute.hellbender.tools.picard.analysis.artifacts.Transition) OrientationBiasUtils(org.broadinstitute.hellbender.tools.exome.orientationbiasvariantfilter.OrientationBiasUtils) VCFConstants(htsjdk.variant.vcf.VCFConstants) ArrayList(java.util.ArrayList) VariantContext(htsjdk.variant.variantcontext.VariantContext) Genotype(htsjdk.variant.variantcontext.Genotype) OrientationSampleTransitionSummary(org.broadinstitute.hellbender.tools.exome.orientationbiasvariantfilter.OrientationSampleTransitionSummary) File(java.io.File) FeatureDataSource(org.broadinstitute.hellbender.engine.FeatureDataSource) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest)

Example 17 with FeatureDataSource

use of org.broadinstitute.hellbender.engine.FeatureDataSource in project gatk-protected by broadinstitute.

the class FilterByOrientationBiasIntegrationTest method testNoVariantsRun.

@Test
public void testNoVariantsRun() throws IOException {
    final File outputFile = File.createTempFile("ob_no_variants", ".vcf");
    final List<String> arguments = new ArrayList<>();
    arguments.add("-" + FilterByOrientationBias.PRE_ADAPTER_METRICS_DETAIL_FILE_SHORT_NAME);
    arguments.add(preAdapterQFile);
    arguments.add("-" + StandardArgumentDefinitions.VARIANT_SHORT_NAME);
    arguments.add(emptyVcf);
    arguments.add("-" + StandardArgumentDefinitions.OUTPUT_SHORT_NAME);
    arguments.add(outputFile.getAbsolutePath());
    runCommandLine(arguments);
    Assert.assertTrue(outputFile.exists());
    final List<VariantContext> variantContexts = new ArrayList<>();
    final FeatureDataSource<VariantContext> featureDataSource = new FeatureDataSource<>(outputFile);
    for (final VariantContext vc : featureDataSource) {
        variantContexts.add(vc);
    }
    Assert.assertEquals(variantContexts.size(), 0);
    final File summaryFile = new File(outputFile.getAbsolutePath() + FilterByOrientationBias.SUMMARY_FILE_SUFFIX);
    Assert.assertTrue(summaryFile.exists());
    Assert.assertTrue(FileUtils.sizeOf(outputFile) > 0);
    Assert.assertTrue(FileUtils.sizeOf(summaryFile) > 0);
    final List<OrientationSampleTransitionSummary> summaries = OrientationBiasUtils.readOrientationBiasSummaryTable(summaryFile);
    Assert.assertEquals(summaries.size(), 0);
}
Also used : ArrayList(java.util.ArrayList) VariantContext(htsjdk.variant.variantcontext.VariantContext) File(java.io.File) OrientationSampleTransitionSummary(org.broadinstitute.hellbender.tools.exome.orientationbiasvariantfilter.OrientationSampleTransitionSummary) FeatureDataSource(org.broadinstitute.hellbender.engine.FeatureDataSource) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest)

Example 18 with FeatureDataSource

use of org.broadinstitute.hellbender.engine.FeatureDataSource in project gatk-protected by broadinstitute.

the class AnnotateVcfWithExpectedAlleleFractionIntegrationTest method test.

// run with made-up mixing fractions and the doctored 2-sample version of DREAM challenge sample 4
// described in {@link CalculateMixingFractionsIntegrationTest}
@Test
public void test() {
    final File table = createTempFile("mixing", ".table");
    final File outputVcf = createTempFile("output", ".vcf");
    //as in the input vcf
    final String sample1 = "SAMPLE1";
    //as in the input vcf
    final String sample2 = "SAMPLE2";
    final double fraction1 = 0.4;
    final double fraction2 = 0.6;
    MixingFraction.writeMixingFractions(Arrays.asList(new MixingFraction(sample1, fraction1), new MixingFraction(sample2, fraction2)), table);
    final String[] arguments = { "-" + StandardArgumentDefinitions.VARIANT_SHORT_NAME, INPUT_VCF.getAbsolutePath(), "-" + AnnotateVcfWithExpectedAlleleFraction.MIXING_FRACTIONS_TABLE_NAME, table.getAbsolutePath(), "-" + StandardArgumentDefinitions.OUTPUT_SHORT_NAME, outputVcf.getAbsolutePath() };
    runCommandLine(arguments);
    final List<VariantContext> input = StreamSupport.stream(new FeatureDataSource<VariantContext>(INPUT_VCF).spliterator(), false).collect(Collectors.toList());
    final List<VariantContext> output = StreamSupport.stream(new FeatureDataSource<VariantContext>(outputVcf).spliterator(), false).collect(Collectors.toList());
    Assert.assertEquals(input.size(), output.size());
    final List<String> inputKeys = input.stream().map(vc -> keyForVariant(vc)).collect(Collectors.toList());
    final List<String> outputKeys = output.stream().map(vc -> keyForVariant(vc)).collect(Collectors.toList());
    Assert.assertEquals(inputKeys, outputKeys);
    final List<Double> alleleFractions = output.stream().map(vc -> vc.getAttributeAsDouble(AnnotateVcfWithExpectedAlleleFraction.EXPECTED_ALLELE_FRACTION_NAME, -1)).collect(Collectors.toList());
    // the first few -- 0.2 is sample1 is het, 0.3 is sample 2 is het, 0.5 if both are het
    final List<Double> firstSeveralAlleleFractionsByHand = Arrays.asList(0.2, 0.2, 0.3, 0.2, 0.3, 0.5, 0.3, 0.2, 0.3, 0.3);
    Assert.assertEquals(alleleFractions.subList(0, firstSeveralAlleleFractionsByHand.size()), firstSeveralAlleleFractionsByHand);
    // hom var + het
    Assert.assertEquals(alleleFractions.get(16), 0.7);
    // het + hom var
    Assert.assertEquals(alleleFractions.get(18), 0.8);
    //both hom ref
    Assert.assertEquals(alleleFractions.get(26), 0.0);
}
Also used : Arrays(java.util.Arrays) StandardArgumentDefinitions(org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest) Collectors(java.util.stream.Collectors) File(java.io.File) AnnotateVcfWithExpectedAlleleFraction(org.broadinstitute.hellbender.tools.walkers.validation.AnnotateVcfWithExpectedAlleleFraction) List(java.util.List) MixingFraction(org.broadinstitute.hellbender.tools.walkers.validation.MixingFraction) Assert(org.testng.Assert) FeatureDataSource(org.broadinstitute.hellbender.engine.FeatureDataSource) VariantContext(htsjdk.variant.variantcontext.VariantContext) StreamSupport(java.util.stream.StreamSupport) VariantContext(htsjdk.variant.variantcontext.VariantContext) File(java.io.File) MixingFraction(org.broadinstitute.hellbender.tools.walkers.validation.MixingFraction) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest)

Example 19 with FeatureDataSource

use of org.broadinstitute.hellbender.engine.FeatureDataSource in project gatk-protected by broadinstitute.

the class OrientationBiasFiltererUnitTest method testCreateSampleToGenotypeVCMap.

@Test
public void testCreateSampleToGenotypeVCMap() {
    // Setup the test
    final FeatureDataSource<VariantContext> featureDataSource = new FeatureDataSource<>(new File(smallM2VcfMore));
    SortedSet<Transition> relevantTransitions = new TreeSet<>();
    relevantTransitions.add(Transition.transitionOf('G', 'T'));
    relevantTransitions.add(Transition.transitionOf('C', 'T'));
    final Map<Transition, Double> preAdapterQFakeScoreMap = new HashMap<>();
    final double amGTPreAdapterQ = 20.0;
    final double amCTPreAdapterQ = 25.0;
    // preAdapterQ suppression will do nothing.
    preAdapterQFakeScoreMap.put(relevantTransitions.first(), amGTPreAdapterQ);
    // preAdapterQ suppression will do nothing.
    preAdapterQFakeScoreMap.put(relevantTransitions.last(), amCTPreAdapterQ);
    final List<VariantContext> updatedVariants = new ArrayList<>();
    for (final VariantContext vc : featureDataSource) {
        final VariantContext updatedVariantContext = OrientationBiasFilterer.annotateVariantContextWithPreprocessingValues(vc, relevantTransitions, preAdapterQFakeScoreMap);
        updatedVariants.add(updatedVariantContext);
    }
    final List<String> sampleNames = updatedVariants.get(0).getSampleNamesOrderedByName();
    // Do the test
    // Create a mapping from sample name to a genotype->variant context map with the second map sorted by p_artifact (i.e. OrientationBiasFilterConstants.P_ARTIFACT_FIELD_NAME)
    final Map<String, SortedMap<Genotype, VariantContext>> sampleNameToVariants = OrientationBiasFilterer.createSampleToGenotypeVariantContextSortedMap(sampleNames, updatedVariants);
    Assert.assertEquals(sampleNameToVariants.keySet().size(), 2);
    Assert.assertTrue(sampleNameToVariants.keySet().contains("TUMOR"));
    Assert.assertTrue(sampleNameToVariants.keySet().contains("NORMAL"));
    Assert.assertEquals(sampleNameToVariants.get("TUMOR").keySet().size(), 8);
    // None of the normal genotypes should have a pvalue, so cannot/shouldn't be added to the sorted map
    Assert.assertEquals(sampleNameToVariants.get("NORMAL").keySet().size(), 0);
    // Check that the sorted map is getting smaller (or same) values of p_artifact and not staying put.
    double previousPArtifact = Double.POSITIVE_INFINITY;
    for (final Genotype genotypeTumor : sampleNameToVariants.get("TUMOR").keySet()) {
        final Double pArtifact = OrientationBiasUtils.getGenotypeDouble(genotypeTumor, OrientationBiasFilterConstants.P_ARTIFACT_FIELD_NAME, Double.POSITIVE_INFINITY);
        Assert.assertNotNull(pArtifact);
        Assert.assertTrue(pArtifact <= previousPArtifact);
        Assert.assertNotEquals(pArtifact, Double.POSITIVE_INFINITY);
    }
}
Also used : VariantContext(htsjdk.variant.variantcontext.VariantContext) Genotype(htsjdk.variant.variantcontext.Genotype) Transition(org.broadinstitute.hellbender.tools.picard.analysis.artifacts.Transition) File(java.io.File) FeatureDataSource(org.broadinstitute.hellbender.engine.FeatureDataSource) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 20 with FeatureDataSource

use of org.broadinstitute.hellbender.engine.FeatureDataSource in project gatk-protected by broadinstitute.

the class OrientationBiasFiltererUnitTest method testHighPloidy.

@Test(description = "This test just confirms that the OB filterer does not throw an exception if confronted with high ploidy call.  Also test that it is a superset of the original variant context.")
public void testHighPloidy() {
    final FeatureDataSource<VariantContext> featureDataSource = new FeatureDataSource<>(new File(smallM2HighPloidy));
    // Dummy values for relevant transitions and preAdapter Map
    SortedSet<Transition> relevantTransitions = new TreeSet<>();
    relevantTransitions.add(Transition.transitionOf('G', 'T'));
    relevantTransitions.add(Transition.transitionOf('C', 'T'));
    final Map<Transition, Double> preAdapterQFakeScoreMap = new HashMap<>();
    final double amGTPreAdapterQ = 20.0;
    final double amCTPreAdapterQ = 25.0;
    // preAdapterQ suppression will do nothing.
    preAdapterQFakeScoreMap.put(relevantTransitions.first(), amGTPreAdapterQ);
    // preAdapterQ suppression will do nothing.
    preAdapterQFakeScoreMap.put(relevantTransitions.last(), amCTPreAdapterQ);
    for (final VariantContext vc : featureDataSource) {
        final VariantContext updatedVariantContext = OrientationBiasFilterer.annotateVariantContextWithPreprocessingValues(vc, relevantTransitions, preAdapterQFakeScoreMap);
        final Set<String> originalGenotypeAttributes = vc.getGenotype("TUMOR1").getExtendedAttributes().keySet();
        final Set<String> newGenotypeAttributes = updatedVariantContext.getGenotype("TUMOR1").getExtendedAttributes().keySet();
        Assert.assertTrue(newGenotypeAttributes.containsAll(originalGenotypeAttributes));
        Assert.assertTrue(newGenotypeAttributes.size() == (originalGenotypeAttributes.size() + 2));
        for (final String ga : originalGenotypeAttributes) {
            Assert.assertEquals(updatedVariantContext.getGenotype("TUMOR1").getExtendedAttributes().get(ga), vc.getGenotype("TUMOR1").getExtendedAttributes().get(ga));
        }
    }
}
Also used : VariantContext(htsjdk.variant.variantcontext.VariantContext) Transition(org.broadinstitute.hellbender.tools.picard.analysis.artifacts.Transition) File(java.io.File) FeatureDataSource(org.broadinstitute.hellbender.engine.FeatureDataSource) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Aggregations

FeatureDataSource (org.broadinstitute.hellbender.engine.FeatureDataSource)39 Test (org.testng.annotations.Test)37 VariantContext (htsjdk.variant.variantcontext.VariantContext)36 File (java.io.File)34 CommandLineProgramTest (org.broadinstitute.hellbender.CommandLineProgramTest)19 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)18 Transition (org.broadinstitute.hellbender.tools.picard.analysis.artifacts.Transition)14 ArrayList (java.util.ArrayList)11 List (java.util.List)10 Genotype (htsjdk.variant.variantcontext.Genotype)8 Arrays (java.util.Arrays)8 Collectors (java.util.stream.Collectors)8 StreamSupport (java.util.stream.StreamSupport)8 Assert (org.testng.Assert)8 StandardArgumentDefinitions (org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions)6 OrientationSampleTransitionSummary (org.broadinstitute.hellbender.tools.exome.orientationbiasvariantfilter.OrientationSampleTransitionSummary)6 Set (java.util.Set)4 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)4 Pair (org.apache.commons.lang3.tuple.Pair)4 Main (org.broadinstitute.hellbender.Main)4