Search in sources :

Example 1 with AllelicCountCollection

use of org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountCollection in project gatk-protected by broadinstitute.

the class CalculatePulldownPhasePosteriors method doWork.

@Override
public Object doWork() {
    if (!new HDF5Library().load(null)) {
        //Note: passing null means using the default temp dir.
        throw new UserException.HardwareFeatureException("Cannot load the required HDF5 library. " + "HDF5 is currently supported on x86-64 architecture and Linux or OSX systems.");
    }
    //read counts, segments, and parameters from files
    final AllelicCountCollection counts = new AllelicCountCollection(snpCountsFile);
    final List<ACNVModeledSegment> segments = SegmentUtils.readACNVModeledSegmentFile(segmentsFile);
    final AlleleFractionState state = reconstructState(segments, parametersFile);
    //load allelic-bias panel of normals if provided
    final AllelicPanelOfNormals allelicPoN = allelicPoNFile != null ? AllelicPanelOfNormals.read(allelicPoNFile) : AllelicPanelOfNormals.EMPTY_PON;
    //calculate phase posteriors
    final List<SimpleInterval> unmodeledSegments = segments.stream().map(ACNVModeledSegment::getInterval).collect(Collectors.toList());
    final AllelicCountWithPhasePosteriorsCollection countsWithPhasePosteriors = calculatePhasePosteriors(counts, unmodeledSegments, state, allelicPoN);
    //write phase posteriors to file with same verbosity as input file
    countsWithPhasePosteriors.write(outputFile, counts.getVerbosity());
    return "SUCCESS";
}
Also used : AllelicPanelOfNormals(org.broadinstitute.hellbender.tools.pon.allelic.AllelicPanelOfNormals) AllelicCountWithPhasePosteriorsCollection(org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountWithPhasePosteriorsCollection) HDF5Library(org.broadinstitute.hdf5.HDF5Library) AllelicCountCollection(org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountCollection) SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval)

Example 2 with AllelicCountCollection

use of org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountCollection in project gatk-protected by broadinstitute.

the class CalculatePulldownPhasePosteriorsIntegrationTest method testCalculatePhasePosteriors.

/**
     * Uses {@link AlleleFractionSimulatedData} to test recovery of phase indicators.  Phase with highest posterior
     * probability is compared to the true phase; we require that
     * {@link CalculatePulldownPhasePosteriorsIntegrationTest#FRACTION_OF_INDICATORS_CORRECT_THRESHOLD} of the
     * indicators are recovered correctly.
     */
@Test
public void testCalculatePhasePosteriors() {
    final double averageHetsPerSegment = 100;
    final int numSegments = 100;
    final int averageDepth = 100;
    final double biasMean = 1.1;
    final double biasVariance = 0.01;
    final double outlierProbability = 0.02;
    final AlleleFractionSimulatedData simulatedData = new AlleleFractionSimulatedData(averageHetsPerSegment, numSegments, averageDepth, biasMean, biasVariance, outlierProbability);
    final SegmentedGenome segmentedGenome = simulatedData.getSegmentedGenome();
    final AlleleFractionState trueState = simulatedData.getTrueState();
    final AlleleFractionSimulatedData.PhaseIndicators truePhases = simulatedData.getTruePhases();
    final AllelicCountCollection counts = new AllelicCountCollection();
    //note that chromosomes are in lexicographical order
    segmentedGenome.getGenome().getSNPs().targets().stream().forEach(counts::add);
    final AllelicCountWithPhasePosteriorsCollection countsWithPhasePosteriors = CalculatePulldownPhasePosteriors.calculatePhasePosteriors(counts, segmentedGenome.getSegments(), trueState, AllelicPanelOfNormals.EMPTY_PON);
    int numIndicatorsCorrect = 0;
    //order is ALT_MINOR, REF_MINOR, OUTLIER
    final Iterator<AlleleFractionIndicator> phaseIterator = truePhases.iterator();
    final Iterator<AllelicCountWithPhasePosteriors> countWithPhasePosteriorsIterator = countsWithPhasePosteriors.getCounts().iterator();
    while (phaseIterator.hasNext() && countWithPhasePosteriorsIterator.hasNext()) {
        final AlleleFractionIndicator truePhase = phaseIterator.next();
        final AllelicCountWithPhasePosteriors countWithPhasePosteriors = countWithPhasePosteriorsIterator.next();
        final List<Double> phaseProbabilities = Arrays.asList(countWithPhasePosteriors.getAltMinorProb(), countWithPhasePosteriors.getRefMinorProb(), countWithPhasePosteriors.getOutlierProb());
        final int indexOfMaxProbPhase = phaseProbabilities.indexOf(Collections.max(phaseProbabilities));
        final AlleleFractionIndicator maxProbPhase = AlleleFractionIndicator.values()[indexOfMaxProbPhase];
        if (maxProbPhase.equals(truePhase)) {
            numIndicatorsCorrect++;
        }
    }
    final double fractionOfIndicatorsCorrect = (double) numIndicatorsCorrect / countsWithPhasePosteriors.getCounts().size();
    Assert.assertTrue(fractionOfIndicatorsCorrect >= FRACTION_OF_INDICATORS_CORRECT_THRESHOLD);
}
Also used : AllelicCountWithPhasePosteriorsCollection(org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountWithPhasePosteriorsCollection) AlleleFractionIndicator(org.broadinstitute.hellbender.tools.exome.allelefraction.AlleleFractionIndicator) AllelicCountWithPhasePosteriors(org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountWithPhasePosteriors) AlleleFractionState(org.broadinstitute.hellbender.tools.exome.allelefraction.AlleleFractionState) AlleleFractionSimulatedData(org.broadinstitute.hellbender.tools.exome.allelefraction.AlleleFractionSimulatedData) AllelicCountCollection(org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountCollection) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest)

Example 3 with AllelicCountCollection

use of org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountCollection in project gatk-protected by broadinstitute.

the class AlleleFractionSegmenterUnitTest method generateCounts.

//visible for testing joint segmentation
protected static AllelicCountCollection generateCounts(final List<Double> minorAlleleFractionSequence, final List<SimpleInterval> positions, final RandomGenerator rng, final AlleleFractionGlobalParameters trueParams) {
    //translate to ApacheCommons' parametrization of the gamma distribution
    final GammaDistribution biasGenerator = getGammaDistribution(trueParams, rng);
    final double outlierProbability = trueParams.getOutlierProbability();
    final AllelicCountCollection counts = new AllelicCountCollection();
    for (int n = 0; n < minorAlleleFractionSequence.size(); n++) {
        counts.add(generateAllelicCount(minorAlleleFractionSequence.get(n), positions.get(n), rng, biasGenerator, outlierProbability));
    }
    return counts;
}
Also used : AllelicCountCollection(org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountCollection) GammaDistribution(org.apache.commons.math3.distribution.GammaDistribution)

Example 4 with AllelicCountCollection

use of org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountCollection in project gatk-protected by broadinstitute.

the class AlleleFractionSegmenterUnitTest method testSegmentation.

@Test
public void testSegmentation() {
    final RandomGenerator rng = RandomGeneratorFactory.createRandomGenerator(new Random(563));
    final List<Double> trueWeights = Arrays.asList(0.2, 0.5, 0.3);
    final List<Double> trueMinorAlleleFractions = Arrays.asList(0.12, 0.32, 0.5);
    final double trueMemoryLength = 1e5;
    final AlleleFractionGlobalParameters trueParams = new AlleleFractionGlobalParameters(1.0, 0.01, 0.01);
    final AlleleFractionHMM trueModel = new AlleleFractionHMM(trueMinorAlleleFractions, trueWeights, trueMemoryLength, AllelicPanelOfNormals.EMPTY_PON, trueParams);
    // randomly set positions
    final int chainLength = 10000;
    final List<SimpleInterval> positions = CopyRatioSegmenterUnitTest.randomPositions("chr1", chainLength, rng, trueMemoryLength / 4);
    final List<Integer> trueStates = trueModel.generateHiddenStateChain(positions);
    final List<Double> truthMinorFractions = trueStates.stream().map(trueModel::getMinorAlleleFraction).collect(Collectors.toList());
    final AllelicCountCollection counts = generateCounts(truthMinorFractions, positions, rng, trueParams);
    final AlleleFractionSegmenter segmenter = new AlleleFractionSegmenter(10, counts, AllelicPanelOfNormals.EMPTY_PON);
    final List<ModeledSegment> segments = segmenter.getModeledSegments();
    final double[] segmentMinorFractions = segments.stream().flatMap(s -> Collections.nCopies((int) s.getTargetCount(), s.getSegmentMean()).stream()).mapToDouble(x -> x).toArray();
    final double averageMinorFractionError = IntStream.range(0, truthMinorFractions.size()).mapToDouble(n -> Math.abs(segmentMinorFractions[n] - truthMinorFractions.get(n))).average().getAsDouble();
    Assert.assertEquals(averageMinorFractionError, 0, 0.01);
}
Also used : AlleleFractionGlobalParameters(org.broadinstitute.hellbender.tools.exome.allelefraction.AlleleFractionGlobalParameters) IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) BinomialDistribution(org.apache.commons.math3.distribution.BinomialDistribution) ModeledSegment(org.broadinstitute.hellbender.tools.exome.ModeledSegment) AllelicCount(org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCount) Test(org.testng.annotations.Test) Random(java.util.Random) SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval) Collectors(java.util.stream.Collectors) GammaDistribution(org.apache.commons.math3.distribution.GammaDistribution) List(java.util.List) Assert(org.testng.Assert) RandomGenerator(org.apache.commons.math3.random.RandomGenerator) RandomGeneratorFactory(org.apache.commons.math3.random.RandomGeneratorFactory) AllelicPanelOfNormals(org.broadinstitute.hellbender.tools.pon.allelic.AllelicPanelOfNormals) AlleleFractionGlobalParameters(org.broadinstitute.hellbender.tools.exome.allelefraction.AlleleFractionGlobalParameters) AllelicCountCollection(org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountCollection) Collections(java.util.Collections) RandomGenerator(org.apache.commons.math3.random.RandomGenerator) Random(java.util.Random) AllelicCountCollection(org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountCollection) ModeledSegment(org.broadinstitute.hellbender.tools.exome.ModeledSegment) SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval) Test(org.testng.annotations.Test)

Example 5 with AllelicCountCollection

use of org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountCollection in project gatk-protected by broadinstitute.

the class AllelicPanelOfNormalsUnitTest method testPoNHyperparameterInitialization.

@Test
public void testPoNHyperparameterInitialization() {
    LoggingUtils.setLoggingLevel(Log.LogLevel.INFO);
    final AllelicPanelOfNormals allelicPoN = new AllelicPanelOfNormals(new AllelicCountCollection(ALLELIC_PON_NORMAL_COUNTS_FILE));
    final SimpleInterval firstSite = new SimpleInterval("1", 1, 1);
    //all sites in PoN are from chr1
    final SimpleInterval siteNotInPoN = new SimpleInterval("2", 1, 1);
    // test initialization of hyperparameters for first site in PoN (a = 1218, r = 1317)
    final double alphaAtFirstSite = allelicPoN.getAlpha(firstSite);
    final double betaAtFirstSite = allelicPoN.getBeta(firstSite);
    Assert.assertEquals(alphaAtFirstSite, ALPHA_EXPECTED_AT_FIRST_SITE, DELTA);
    Assert.assertEquals(betaAtFirstSite, BETA_EXPECTED_AT_FIRST_SITE, DELTA);
    // test initialization of MLE hyperparameters (which are default values for sites not in PoN)
    final double alphaNotInPoN = allelicPoN.getAlpha(siteNotInPoN);
    final double betaNotInPoN = allelicPoN.getBeta(siteNotInPoN);
    final double meanBias = allelicPoN.getGlobalMeanBias();
    final double biasVariance = allelicPoN.getGlobalBiasVariance();
    Assert.assertEquals(alphaNotInPoN, MLE_ALPHA_EXPECTED, DELTA);
    Assert.assertEquals(betaNotInPoN, MLE_BETA_EXPECTED, DELTA);
    Assert.assertEquals(meanBias, MLE_MEAN_BIAS_EXPECTED, DELTA);
    Assert.assertEquals(biasVariance, MLE_BIAS_VARIANCE_EXPECTED, DELTA);
}
Also used : AllelicCountCollection(org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountCollection) SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Aggregations

AllelicCountCollection (org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountCollection)30 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)16 AllelicPanelOfNormals (org.broadinstitute.hellbender.tools.pon.allelic.AllelicPanelOfNormals)14 Test (org.testng.annotations.Test)14 File (java.io.File)8 Collectors (java.util.stream.Collectors)8 AllelicCount (org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCount)8 IOException (java.io.IOException)6 List (java.util.List)6 GammaDistribution (org.apache.commons.math3.distribution.GammaDistribution)6 RandomGenerator (org.apache.commons.math3.random.RandomGenerator)6 UserException (org.broadinstitute.hellbender.exceptions.UserException)6 org.broadinstitute.hellbender.tools.exome (org.broadinstitute.hellbender.tools.exome)6 ModeledSegment (org.broadinstitute.hellbender.tools.exome.ModeledSegment)6 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)6 AlleleFractionGlobalParameters (org.broadinstitute.hellbender.tools.exome.allelefraction.AlleleFractionGlobalParameters)5 java.util (java.util)4 Arrays (java.util.Arrays)4 Random (java.util.Random)4 IntStream (java.util.stream.IntStream)4