Search in sources :

Example 6 with AlleleFractionGlobalParameters

use of org.broadinstitute.hellbender.tools.exome.allelefraction.AlleleFractionGlobalParameters in project gatk 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 7 with AlleleFractionGlobalParameters

use of org.broadinstitute.hellbender.tools.exome.allelefraction.AlleleFractionGlobalParameters in project gatk by broadinstitute.

the class JointAFCRSegmenterUnitTest method testSegmentation.

@Test
public void testSegmentation() {
    final RandomGenerator rng = RandomGeneratorFactory.createRandomGenerator(new Random(563));
    // probability that a datum is a het i.e. #hets / (#hets + #targets)
    final double hetProportion = 0.25;
    final List<Double> trueWeights = Arrays.asList(0.2, 0.5, 0.3);
    final double[] trueMinorAlleleFractions = new double[] { 0.12, 0.32, 0.5 };
    final double[] trueLog2CopyRatios = new double[] { -2.0, 0.0, 1.7 };
    final List<AFCRHiddenState> trueJointStates = IntStream.range(0, trueLog2CopyRatios.length).mapToObj(n -> new AFCRHiddenState(trueMinorAlleleFractions[n], trueLog2CopyRatios[n])).collect(Collectors.toList());
    final double trueMemoryLength = 1e5;
    final double trueCauchyWidth = 0.2;
    final int initialNumCRStates = 20;
    final int initialNumAFStates = 20;
    final AlleleFractionGlobalParameters trueAFParams = new AlleleFractionGlobalParameters(1.0, 0.01, 0.01);
    final JointAFCRHMM trueJointModel = new JointAFCRHMM(trueJointStates, trueWeights, trueMemoryLength, trueAFParams, AllelicPanelOfNormals.EMPTY_PON, trueCauchyWidth);
    // generate joint truth
    final int chainLength = 10000;
    final List<SimpleInterval> positions = CopyRatioSegmenterUnitTest.randomPositions("chr1", chainLength, rng, trueMemoryLength / 4);
    final List<Integer> trueHiddenStates = trueJointModel.generateHiddenStateChain(positions);
    final List<AFCRHiddenState> trueAFCRSequence = trueHiddenStates.stream().map(trueJointModel::getHiddenStateValue).collect(Collectors.toList());
    final double[] trueCopyRatioSequence = trueAFCRSequence.stream().mapToDouble(AFCRHiddenState::getLog2CopyRatio).toArray();
    final double[] trueAlleleFractionSequence = trueAFCRSequence.stream().mapToDouble(AFCRHiddenState::getMinorAlleleFraction).toArray();
    // generate separate af and cr data
    final GammaDistribution biasGenerator = AlleleFractionSegmenterUnitTest.getGammaDistribution(trueAFParams, rng);
    final double outlierProbability = trueAFParams.getOutlierProbability();
    final AllelicCountCollection afData = new AllelicCountCollection();
    final List<Double> crData = new ArrayList<>();
    final List<Target> crTargets = new ArrayList<>();
    for (int n = 0; n < positions.size(); n++) {
        final SimpleInterval position = positions.get(n);
        final AFCRHiddenState jointState = trueAFCRSequence.get(n);
        final double minorFraction = jointState.getMinorAlleleFraction();
        final double log2CopyRatio = jointState.getLog2CopyRatio();
        if (rng.nextDouble() < hetProportion) {
            // het datum
            afData.add(AlleleFractionSegmenterUnitTest.generateAllelicCount(minorFraction, position, rng, biasGenerator, outlierProbability));
        } else {
            //target datum
            crTargets.add(new Target(position));
            crData.add(CopyRatioSegmenterUnitTest.generateData(trueCauchyWidth, log2CopyRatio, rng));
        }
    }
    final ReadCountCollection rcc = new ReadCountCollection(crTargets, Arrays.asList("SAMPLE"), new Array2DRowRealMatrix(crData.stream().mapToDouble(x -> x).toArray()));
    final JointAFCRSegmenter segmenter = JointAFCRSegmenter.createJointSegmenter(initialNumCRStates, rcc, initialNumAFStates, afData, AllelicPanelOfNormals.EMPTY_PON);
    final TargetCollection<SimpleInterval> tc = new HashedListTargetCollection<>(positions);
    final List<Pair<SimpleInterval, AFCRHiddenState>> segmentation = segmenter.findSegments();
    final List<ACNVModeledSegment> jointSegments = segmentation.stream().map(pair -> {
        final SimpleInterval position = pair.getLeft();
        final AFCRHiddenState jointState = pair.getRight();
        final PosteriorSummary crSummary = PerformJointSegmentation.errorlessPosterior(jointState.getLog2CopyRatio());
        final PosteriorSummary afSummary = PerformJointSegmentation.errorlessPosterior(jointState.getMinorAlleleFraction());
        return new ACNVModeledSegment(position, crSummary, afSummary);
    }).collect(Collectors.toList());
    final double[] segmentCopyRatios = jointSegments.stream().flatMap(s -> Collections.nCopies(tc.targetCount(s.getInterval()), s.getSegmentMeanPosteriorSummary().getCenter()).stream()).mapToDouble(x -> x).toArray();
    final double[] segmentMinorFractions = jointSegments.stream().flatMap(s -> Collections.nCopies(tc.targetCount(s.getInterval()), s.getMinorAlleleFractionPosteriorSummary().getCenter()).stream()).mapToDouble(x -> x).toArray();
    final double averageMinorFractionError = Arrays.stream(MathArrays.ebeSubtract(trueAlleleFractionSequence, segmentMinorFractions)).map(Math::abs).average().getAsDouble();
    final double averageCopyRatioError = Arrays.stream(MathArrays.ebeSubtract(trueCopyRatioSequence, segmentCopyRatios)).map(Math::abs).average().getAsDouble();
    Assert.assertEquals(averageMinorFractionError, 0, 0.04);
    Assert.assertEquals(averageCopyRatioError, 0, 0.04);
}
Also used : IntStream(java.util.stream.IntStream) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) java.util(java.util) MathArrays(org.apache.commons.math3.util.MathArrays) org.broadinstitute.hellbender.tools.exome(org.broadinstitute.hellbender.tools.exome) Test(org.testng.annotations.Test) SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval) Collectors(java.util.stream.Collectors) GammaDistribution(org.apache.commons.math3.distribution.GammaDistribution) Pair(org.apache.commons.lang3.tuple.Pair) Assert(org.testng.Assert) RandomGenerator(org.apache.commons.math3.random.RandomGenerator) PosteriorSummary(org.broadinstitute.hellbender.utils.mcmc.PosteriorSummary) 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) RandomGenerator(org.apache.commons.math3.random.RandomGenerator) PosteriorSummary(org.broadinstitute.hellbender.utils.mcmc.PosteriorSummary) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval) GammaDistribution(org.apache.commons.math3.distribution.GammaDistribution) Pair(org.apache.commons.lang3.tuple.Pair) AlleleFractionGlobalParameters(org.broadinstitute.hellbender.tools.exome.allelefraction.AlleleFractionGlobalParameters) AllelicCountCollection(org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountCollection) Test(org.testng.annotations.Test)

Example 8 with AlleleFractionGlobalParameters

use of org.broadinstitute.hellbender.tools.exome.allelefraction.AlleleFractionGlobalParameters in project gatk by broadinstitute.

the class AlleleFractionHMMUnitTest method constructorTest.

@Test
public void constructorTest() {
    final double memoryLength = 1e6;
    final List<Double> minorAlleleFractions = Arrays.asList(0.1, 0.5, 0.23);
    final List<Double> weights = Arrays.asList(0.2, 0.2, 0.6);
    final AlleleFractionGlobalParameters params = new AlleleFractionGlobalParameters(0.1, 0.01, 0.03);
    final AlleleFractionHMM model = new AlleleFractionHMM(minorAlleleFractions, weights, memoryLength, AllelicPanelOfNormals.EMPTY_PON, params);
    Assert.assertEquals(memoryLength, model.getMemoryLength());
    for (int n = 0; n < weights.size(); n++) {
        Assert.assertEquals(weights.get(n), model.getWeight(n));
        Assert.assertEquals(minorAlleleFractions.get(n), model.getMinorAlleleFraction(n));
    }
    Assert.assertEquals(model.getParameters().getMeanBias(), params.getMeanBias());
    Assert.assertEquals(model.getParameters().getBiasVariance(), params.getBiasVariance());
    Assert.assertEquals(model.getParameters().getOutlierProbability(), params.getOutlierProbability());
}
Also used : AlleleFractionGlobalParameters(org.broadinstitute.hellbender.tools.exome.allelefraction.AlleleFractionGlobalParameters) Test(org.testng.annotations.Test)

Example 9 with AlleleFractionGlobalParameters

use of org.broadinstitute.hellbender.tools.exome.allelefraction.AlleleFractionGlobalParameters in project gatk by broadinstitute.

the class AlleleFractionHMMUnitTest method equalMinorFractionsTest.

// if all states have the same minor fraction, then regardless of data the hidden state probabilities are
// proportional to the weights
@Test
public void equalMinorFractionsTest() {
    // only the second state
    final List<Double> weights = Arrays.asList(0.2, 0.3, 0.5);
    final List<Double> minorAlleleFractions = Arrays.asList(0.3, 0.3, 0.3);
    final double memoryLength = 1e3;
    final AlleleFractionGlobalParameters params = new AlleleFractionGlobalParameters(0.1, 0.01, 0.03);
    final AlleleFractionHMM model = new AlleleFractionHMM(minorAlleleFractions, weights, memoryLength, AllelicPanelOfNormals.EMPTY_PON, params);
    final Random random = new Random(13);
    final int chainLength = 10000;
    final List<SimpleInterval> positions = new ArrayList<>();
    final List<AllelicCount> data = new ArrayList<>();
    int position = 1;
    for (int n = 0; n < chainLength; n++) {
        position += random.nextInt((int) (2 * memoryLength));
        final SimpleInterval interval = new SimpleInterval("chr1", position, position);
        positions.add(interval);
        data.add(new AllelicCount(interval, random.nextInt(30) + 1, random.nextInt(30) + 1));
    }
    final ForwardBackwardAlgorithm.Result<AllelicCount, SimpleInterval, Integer> fbResult = ForwardBackwardAlgorithm.apply(data, positions, model);
    for (int pos = 0; pos < chainLength; pos++) {
        for (int state = 0; state < weights.size(); state++) {
            Assert.assertEquals(fbResult.logProbability(pos, state), Math.log(weights.get(state)), 1e-5);
        }
    }
}
Also used : AlleleFractionGlobalParameters(org.broadinstitute.hellbender.tools.exome.allelefraction.AlleleFractionGlobalParameters) ForwardBackwardAlgorithm(org.broadinstitute.hellbender.utils.hmm.ForwardBackwardAlgorithm) ArrayList(java.util.ArrayList) Random(java.util.Random) SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval) AllelicCount(org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCount) Test(org.testng.annotations.Test)

Example 10 with AlleleFractionGlobalParameters

use of org.broadinstitute.hellbender.tools.exome.allelefraction.AlleleFractionGlobalParameters in project gatk-protected by broadinstitute.

the class AlleleFractionHMMUnitTest method equalMinorFractionsTest.

// if all states have the same minor fraction, then regardless of data the hidden state probabilities are
// proportional to the weights
@Test
public void equalMinorFractionsTest() {
    // only the second state
    final List<Double> weights = Arrays.asList(0.2, 0.3, 0.5);
    final List<Double> minorAlleleFractions = Arrays.asList(0.3, 0.3, 0.3);
    final double memoryLength = 1e3;
    final AlleleFractionGlobalParameters params = new AlleleFractionGlobalParameters(0.1, 0.01, 0.03);
    final AlleleFractionHMM model = new AlleleFractionHMM(minorAlleleFractions, weights, memoryLength, AllelicPanelOfNormals.EMPTY_PON, params);
    final Random random = new Random(13);
    final int chainLength = 10000;
    final List<SimpleInterval> positions = new ArrayList<>();
    final List<AllelicCount> data = new ArrayList<>();
    int position = 1;
    for (int n = 0; n < chainLength; n++) {
        position += random.nextInt((int) (2 * memoryLength));
        final SimpleInterval interval = new SimpleInterval("chr1", position, position);
        positions.add(interval);
        data.add(new AllelicCount(interval, random.nextInt(30) + 1, random.nextInt(30) + 1));
    }
    final ForwardBackwardAlgorithm.Result<AllelicCount, SimpleInterval, Integer> fbResult = ForwardBackwardAlgorithm.apply(data, positions, model);
    for (int pos = 0; pos < chainLength; pos++) {
        for (int state = 0; state < weights.size(); state++) {
            Assert.assertEquals(fbResult.logProbability(pos, state), Math.log(weights.get(state)), 1e-5);
        }
    }
}
Also used : AlleleFractionGlobalParameters(org.broadinstitute.hellbender.tools.exome.allelefraction.AlleleFractionGlobalParameters) ForwardBackwardAlgorithm(org.broadinstitute.hellbender.utils.hmm.ForwardBackwardAlgorithm) ArrayList(java.util.ArrayList) Random(java.util.Random) SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval) AllelicCount(org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCount) Test(org.testng.annotations.Test)

Aggregations

AlleleFractionGlobalParameters (org.broadinstitute.hellbender.tools.exome.allelefraction.AlleleFractionGlobalParameters)12 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)10 Test (org.testng.annotations.Test)10 AllelicCountCollection (org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountCollection)8 Random (java.util.Random)6 Collectors (java.util.stream.Collectors)6 RandomGenerator (org.apache.commons.math3.random.RandomGenerator)6 ModeledSegment (org.broadinstitute.hellbender.tools.exome.ModeledSegment)6 AllelicCount (org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCount)6 AllelicPanelOfNormals (org.broadinstitute.hellbender.tools.pon.allelic.AllelicPanelOfNormals)6 Arrays (java.util.Arrays)4 List (java.util.List)4 IntStream (java.util.stream.IntStream)4 Pair (org.apache.commons.lang3.tuple.Pair)4 GammaDistribution (org.apache.commons.math3.distribution.GammaDistribution)4 RandomGeneratorFactory (org.apache.commons.math3.random.RandomGeneratorFactory)4 Assert (org.testng.Assert)4 Doubles (com.google.cloud.dataflow.sdk.repackaged.com.google.common.primitives.Doubles)2 java.util (java.util)2 ArrayList (java.util.ArrayList)2