Search in sources :

Example 1 with ACNVModeledSegment

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

the class CNLOHCaller method calcNewRhos.

private double[] calcNewRhos(final List<ACNVModeledSegment> segments, final List<double[][][]> responsibilitiesBySeg, final double lambda, final double[] rhos, final int[] mVals, final int[] nVals, final JavaSparkContext ctx) {
    // Since, we pass in the entire responsibilities matrix, we need the correct index for each rho.  That, and the
    //  fact that this is a univariate objective function, means we need to create an instance for each rho.  And
    //  then we blast across Spark.
    final List<Pair<? extends Function<Double, Double>, SearchInterval>> objectives = IntStream.range(0, rhos.length).mapToObj(i -> new Pair<>(new Function<Double, Double>() {

        @Override
        public Double apply(Double rho) {
            return calculateESmnObjective(rho, segments, responsibilitiesBySeg, mVals, nVals, lambda, i);
        }
    }, new SearchInterval(0.0, 1.0, rhos[i]))).collect(Collectors.toList());
    final JavaRDD<Pair<? extends Function<Double, Double>, SearchInterval>> objectivesRDD = ctx.parallelize(objectives);
    final List<Double> resultsAsDouble = objectivesRDD.map(objective -> optimizeIt(objective.getFirst(), objective.getSecond())).collect();
    return resultsAsDouble.stream().mapToDouble(Double::doubleValue).toArray();
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) JavaSparkContext(org.apache.spark.api.java.JavaSparkContext) SearchInterval(org.apache.commons.math3.optim.univariate.SearchInterval) RealVector(org.apache.commons.math3.linear.RealVector) Function(java.util.function.Function) ParamUtils(org.broadinstitute.hellbender.utils.param.ParamUtils) GammaDistribution(org.apache.commons.math3.distribution.GammaDistribution) Gamma(org.apache.commons.math3.special.Gamma) ACNVModeledSegment(org.broadinstitute.hellbender.tools.exome.ACNVModeledSegment) BaseAbstractUnivariateIntegrator(org.apache.commons.math3.analysis.integration.BaseAbstractUnivariateIntegrator) GoalType(org.apache.commons.math3.optim.nonlinear.scalar.GoalType) MatrixUtils(org.apache.commons.math3.linear.MatrixUtils) UnivariateObjectiveFunction(org.apache.commons.math3.optim.univariate.UnivariateObjectiveFunction) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) SimpsonIntegrator(org.apache.commons.math3.analysis.integration.SimpsonIntegrator) JavaRDD(org.apache.spark.api.java.JavaRDD) GATKProtectedMathUtils(org.broadinstitute.hellbender.utils.GATKProtectedMathUtils) Pair(org.apache.commons.math3.util.Pair) Collectors(java.util.stream.Collectors) BrentOptimizer(org.apache.commons.math3.optim.univariate.BrentOptimizer) Serializable(java.io.Serializable) List(java.util.List) Percentile(org.apache.commons.math3.stat.descriptive.rank.Percentile) Logger(org.apache.logging.log4j.Logger) MathUtils(org.broadinstitute.hellbender.utils.MathUtils) NormalDistribution(org.apache.commons.math3.distribution.NormalDistribution) UnivariateFunction(org.apache.commons.math3.analysis.UnivariateFunction) Variance(org.apache.commons.math3.stat.descriptive.moment.Variance) Utils(org.broadinstitute.hellbender.utils.Utils) RealMatrix(org.apache.commons.math3.linear.RealMatrix) VisibleForTesting(com.google.common.annotations.VisibleForTesting) HomoSapiensConstants(org.broadinstitute.hellbender.utils.variant.HomoSapiensConstants) MaxEval(org.apache.commons.math3.optim.MaxEval) LogManager(org.apache.logging.log4j.LogManager) Function(java.util.function.Function) UnivariateObjectiveFunction(org.apache.commons.math3.optim.univariate.UnivariateObjectiveFunction) UnivariateFunction(org.apache.commons.math3.analysis.UnivariateFunction) SearchInterval(org.apache.commons.math3.optim.univariate.SearchInterval) Pair(org.apache.commons.math3.util.Pair)

Example 2 with ACNVModeledSegment

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

the class CNLOHCaller method calculateESmnObjective.

// Returns a single number that is the total likelihood.  Note that it does this for the given rho, even though
//  the entire responsibility 4d array (list of 3d, where list is per segment, then each array is KxMxN)
//  is passed in.
//
//  HACK: All rhos have to be passed in with the index of interest.  Under the hood, all other values of rho are ignored.
private double calculateESmnObjective(final double rho, List<ACNVModeledSegment> segments, final List<double[][][]> responsibilitiesForSegsAsList, final int[] mVals, final int[] nVals, final double lambda, final int rhoIndex) {
    // We will want to sum an entire matrix that is S x M x N for the given rho.
    final double[][][] eSMN = new double[responsibilitiesForSegsAsList.size()][mVals.length][nVals.length];
    // Populate eSMN
    for (int s = 0; s < responsibilitiesForSegsAsList.size(); s++) {
        final ACNVModeledSegment seg = segments.get(s);
        final double mafMode = seg.getMinorAlleleFractionPosteriorSummary().getCenter();
        final double mafLow = seg.getMinorAlleleFractionPosteriorSummary().getLower();
        final double mafHigh = seg.getMinorAlleleFractionPosteriorSummary().getUpper();
        final double crMode = Math.pow(2, seg.getSegmentMeanPosteriorSummary().getCenter()) - segmentMeanBiasInCR;
        final double crLow = Math.pow(2, seg.getSegmentMeanPosteriorSummary().getLower()) - segmentMeanBiasInCR;
        final double crHigh = Math.pow(2, seg.getSegmentMeanPosteriorSummary().getUpper()) - segmentMeanBiasInCR;
        for (int m = 0; m < mVals.length; m++) {
            for (int n = 0; n < nVals.length; n++) {
                final double mafLikelihood = calculateFmaf(rho, mVals[m], nVals[n], mafMode, mafLow, mafHigh, normalNumCopies);
                final double crLikelihood = calculateFcr(rho, mVals[m], nVals[n], lambda, crMode, crLow, crHigh, segmentMeanVarianceInCR, normalNumCopies);
                if (((rho > 1) || (rho < 0)) || ((rho > 0) && (rho < rhoThreshold))) {
                    eSMN[s][m][n] = MIN_L;
                } else {
                    eSMN[s][m][n] = responsibilitiesForSegsAsList.get(s)[rhoIndex][m][n] * Math.log(mafLikelihood * crLikelihood);
                }
            }
        }
    }
    return GATKProtectedMathUtils.sum(eSMN);
}
Also used : ACNVModeledSegment(org.broadinstitute.hellbender.tools.exome.ACNVModeledSegment)

Example 3 with ACNVModeledSegment

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

the class CNLOHCaller method calculateVarianceOfCopyNeutralSegmentMeans.

/**
     * Attempt to get an idea of segment mean variance near copy neutral.
     *
     * @param segments Never {@code null}
     * @return variance of segment mean (in CR space) of segments that are "close enough" to copy neutral.
     *   Zero if no segments are "close enough"
     */
private double calculateVarianceOfCopyNeutralSegmentMeans(final List<ACNVModeledSegment> segments, final double meanBiasInCR) {
    Utils.nonNull(segments);
    // Only consider values "close enough" to copy neutral (CR == 1).
    final double neutralCR = 1 + meanBiasInCR;
    final double[] neutralSegmentMeans = segments.stream().mapToDouble(ACNVModeledSegment::getSegmentMeanInCRSpace).filter(m -> Math.abs(m - neutralCR) < CLOSE_ENOUGH_TO_COPY_NEUTRAL_IN_CR).toArray();
    return new Variance().evaluate(neutralSegmentMeans);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) JavaSparkContext(org.apache.spark.api.java.JavaSparkContext) SearchInterval(org.apache.commons.math3.optim.univariate.SearchInterval) RealVector(org.apache.commons.math3.linear.RealVector) Function(java.util.function.Function) ParamUtils(org.broadinstitute.hellbender.utils.param.ParamUtils) GammaDistribution(org.apache.commons.math3.distribution.GammaDistribution) Gamma(org.apache.commons.math3.special.Gamma) ACNVModeledSegment(org.broadinstitute.hellbender.tools.exome.ACNVModeledSegment) BaseAbstractUnivariateIntegrator(org.apache.commons.math3.analysis.integration.BaseAbstractUnivariateIntegrator) GoalType(org.apache.commons.math3.optim.nonlinear.scalar.GoalType) MatrixUtils(org.apache.commons.math3.linear.MatrixUtils) UnivariateObjectiveFunction(org.apache.commons.math3.optim.univariate.UnivariateObjectiveFunction) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) SimpsonIntegrator(org.apache.commons.math3.analysis.integration.SimpsonIntegrator) JavaRDD(org.apache.spark.api.java.JavaRDD) GATKProtectedMathUtils(org.broadinstitute.hellbender.utils.GATKProtectedMathUtils) Pair(org.apache.commons.math3.util.Pair) Collectors(java.util.stream.Collectors) BrentOptimizer(org.apache.commons.math3.optim.univariate.BrentOptimizer) Serializable(java.io.Serializable) List(java.util.List) Percentile(org.apache.commons.math3.stat.descriptive.rank.Percentile) Logger(org.apache.logging.log4j.Logger) MathUtils(org.broadinstitute.hellbender.utils.MathUtils) NormalDistribution(org.apache.commons.math3.distribution.NormalDistribution) UnivariateFunction(org.apache.commons.math3.analysis.UnivariateFunction) Variance(org.apache.commons.math3.stat.descriptive.moment.Variance) Utils(org.broadinstitute.hellbender.utils.Utils) RealMatrix(org.apache.commons.math3.linear.RealMatrix) VisibleForTesting(com.google.common.annotations.VisibleForTesting) HomoSapiensConstants(org.broadinstitute.hellbender.utils.variant.HomoSapiensConstants) MaxEval(org.apache.commons.math3.optim.MaxEval) LogManager(org.apache.logging.log4j.LogManager) ACNVModeledSegment(org.broadinstitute.hellbender.tools.exome.ACNVModeledSegment) Variance(org.apache.commons.math3.stat.descriptive.moment.Variance)

Example 4 with ACNVModeledSegment

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

the class PerformJointSegmentationIntegrationTest method testCommandLine.

// checks that segmentation output is created -- only the unit test checks correctness of results
@Test
public void testCommandLine() throws IOException {
    final File tnCoverageFile = LOG2_TN_COVERAGE_FILE;
    final File snpFile = ALLELIC_COUNTS_FILE;
    final File outputSegmentFile = createTempFile("segments", ".seg");
    final int initialNumCRStates = 3;
    final int initialNumAFStates = 3;
    final String[] arguments = { "-" + ExomeStandardArgumentDefinitions.TANGENT_NORMALIZED_COUNTS_FILE_SHORT_NAME, tnCoverageFile.getAbsolutePath(), "-" + ExomeStandardArgumentDefinitions.TUMOR_ALLELIC_COUNTS_FILE_SHORT_NAME, snpFile.getAbsolutePath(), "-" + PerformJointSegmentation.INITIAL_NUM_COPY_RATIO_STATES_SHORT_NAME, Integer.toString(initialNumCRStates), "-" + PerformJointSegmentation.INITIAL_NUM_ALLELE_FRACTION_STATES_SHORT_NAME, Integer.toString(initialNumAFStates), "-" + ExomeStandardArgumentDefinitions.SEGMENT_FILE_SHORT_NAME, outputSegmentFile.getAbsolutePath() };
    runCommandLine(arguments);
    final List<ACNVModeledSegment> segments = SegmentUtils.readACNVModeledSegmentFile(outputSegmentFile);
}
Also used : ACNVModeledSegment(org.broadinstitute.hellbender.tools.exome.ACNVModeledSegment) File(java.io.File) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest)

Example 5 with ACNVModeledSegment

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

the class ACSModeledSegmentUtilsUnitTest method testConversion.

@Test
public void testConversion() {
    final List<ACNVModeledSegment> segs = SegmentUtils.readACNVModeledSegmentFile(new File(TEST_FILE_PATH));
    final Genome genome = new Genome(AlleleFractionSimulatedData.TRIVIAL_TARGETS, Collections.emptyList());
    final List<ACSModeledSegment> acsSegs = segs.stream().map(seg -> ACSModeledSegmentUtils.convertACNVSegmentToACSSegment(seg, 2.0, genome, true)).collect(Collectors.toList());
    for (int i = 0; i < segs.size(); i++) {
        Assert.assertEquals(acsSegs.get(i).getTau() / 2.0, segs.get(i).getSegmentMeanInCRSpace(), 1e-10);
    }
}
Also used : List(java.util.List) ACNVModeledSegment(org.broadinstitute.hellbender.tools.exome.ACNVModeledSegment) Assert(org.testng.Assert) AlleleFractionSimulatedData(org.broadinstitute.hellbender.tools.exome.allelefraction.AlleleFractionSimulatedData) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Genome(org.broadinstitute.hellbender.tools.exome.Genome) Test(org.testng.annotations.Test) SegmentUtils(org.broadinstitute.hellbender.tools.exome.SegmentUtils) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) File(java.io.File) ACNVModeledSegment(org.broadinstitute.hellbender.tools.exome.ACNVModeledSegment) Genome(org.broadinstitute.hellbender.tools.exome.Genome) File(java.io.File) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Aggregations

ACNVModeledSegment (org.broadinstitute.hellbender.tools.exome.ACNVModeledSegment)16 Test (org.testng.annotations.Test)10 List (java.util.List)8 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)8 File (java.io.File)6 Arrays (java.util.Arrays)6 Collectors (java.util.stream.Collectors)6 Pair (org.apache.commons.math3.util.Pair)6 JavaSparkContext (org.apache.spark.api.java.JavaSparkContext)6 HomoSapiensConstants (org.broadinstitute.hellbender.utils.variant.HomoSapiensConstants)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 Serializable (java.io.Serializable)4 Function (java.util.function.Function)4 IntStream (java.util.stream.IntStream)4 UnivariateFunction (org.apache.commons.math3.analysis.UnivariateFunction)4 BaseAbstractUnivariateIntegrator (org.apache.commons.math3.analysis.integration.BaseAbstractUnivariateIntegrator)4 SimpsonIntegrator (org.apache.commons.math3.analysis.integration.SimpsonIntegrator)4 GammaDistribution (org.apache.commons.math3.distribution.GammaDistribution)4 NormalDistribution (org.apache.commons.math3.distribution.NormalDistribution)4 ArrayRealVector (org.apache.commons.math3.linear.ArrayRealVector)4