Search in sources :

Example 1 with GammaDistribution

use of org.apache.commons.math3.distribution.GammaDistribution in project GDSC-SMLM by aherbert.

the class CreateData method createPhotonDistribution.

/**
	 * @return A photon distribution loaded from a file of floating-point values with the specified population mean.
	 */
private RealDistribution createPhotonDistribution() {
    if (PHOTON_DISTRIBUTION[PHOTON_CUSTOM].equals(settings.photonDistribution)) {
        // Get the distribution file
        String filename = Utils.getFilename("Photon_distribution", settings.photonDistributionFile);
        if (filename != null) {
            settings.photonDistributionFile = filename;
            try {
                InputStream is = new FileInputStream(new File(settings.photonDistributionFile));
                BufferedReader in = new BufferedReader(new UnicodeReader(is, null));
                StoredDataStatistics stats = new StoredDataStatistics();
                try {
                    String str = null;
                    double val = 0.0d;
                    while ((str = in.readLine()) != null) {
                        val = Double.parseDouble(str);
                        stats.add(val);
                    }
                } finally {
                    in.close();
                }
                if (stats.getSum() > 0) {
                    // Update the statistics to the desired mean.
                    double scale = (double) settings.photonsPerSecond / stats.getMean();
                    double[] values = stats.getValues();
                    for (int i = 0; i < values.length; i++) values[i] *= scale;
                    // TODO - Investigate the limits of this distribution. 
                    // How far above and below the input data will values be generated.
                    // Create the distribution using the recommended number of bins
                    final int binCount = stats.getN() / 10;
                    EmpiricalDistribution dist = new EmpiricalDistribution(binCount, createRandomGenerator());
                    dist.load(values);
                    return dist;
                }
            } catch (IOException e) {
            // Ignore
            } catch (NullArgumentException e) {
            // Ignore 
            } catch (NumberFormatException e) {
            // Ignore
            }
        }
        Utils.log("Failed to load custom photon distribution from file: %s. Default to fixed.", settings.photonDistributionFile);
    } else if (PHOTON_DISTRIBUTION[PHOTON_UNIFORM].equals(settings.photonDistribution)) {
        if (settings.photonsPerSecond < settings.photonsPerSecondMaximum) {
            UniformRealDistribution dist = new UniformRealDistribution(createRandomGenerator(), settings.photonsPerSecond, settings.photonsPerSecondMaximum);
            return dist;
        }
    } else if (PHOTON_DISTRIBUTION[PHOTON_GAMMA].equals(settings.photonDistribution)) {
        final double scaleParameter = settings.photonsPerSecond / settings.photonShape;
        GammaDistribution dist = new GammaDistribution(createRandomGenerator(), settings.photonShape, scaleParameter, ExponentialDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
        return dist;
    } else if (PHOTON_DISTRIBUTION[PHOTON_CORRELATED].equals(settings.photonDistribution)) {
        // No distribution required
        return null;
    }
    settings.photonDistribution = PHOTON_DISTRIBUTION[PHOTON_FIXED];
    return null;
}
Also used : EmpiricalDistribution(org.apache.commons.math3.random.EmpiricalDistribution) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StoredDataStatistics(gdsc.core.utils.StoredDataStatistics) UniformRealDistribution(org.apache.commons.math3.distribution.UniformRealDistribution) UnicodeReader(gdsc.core.utils.UnicodeReader) IOException(java.io.IOException) NullArgumentException(org.apache.commons.math3.exception.NullArgumentException) FileInputStream(java.io.FileInputStream) BufferedReader(java.io.BufferedReader) File(java.io.File) GammaDistribution(org.apache.commons.math3.distribution.GammaDistribution) CustomGammaDistribution(org.apache.commons.math3.distribution.CustomGammaDistribution)

Example 2 with GammaDistribution

use of org.apache.commons.math3.distribution.GammaDistribution in project gatk 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 3 with GammaDistribution

use of org.apache.commons.math3.distribution.GammaDistribution in project gatk by broadinstitute.

the class AlleleFractionSegmenterUnitTest method generateAllelicCount.

protected static AllelicCount generateAllelicCount(final double minorFraction, final SimpleInterval position, final RandomGenerator rng, final GammaDistribution biasGenerator, final double outlierProbability) {
    final int numReads = 100;
    final double bias = biasGenerator.sample();
    //flip a coin to decide alt minor (alt fraction = minor fraction) or ref minor (alt fraction = 1 - minor fraction)
    final double altFraction = rng.nextDouble() < 0.5 ? minorFraction : 1 - minorFraction;
    //the probability of an alt read is the alt fraction modified by the bias or, in the case of an outlier, random
    final double pAlt = rng.nextDouble() < outlierProbability ? rng.nextDouble() : altFraction / (altFraction + (1 - altFraction) * bias);
    final int numAltReads = new BinomialDistribution(rng, numReads, pAlt).sample();
    final int numRefReads = numReads - numAltReads;
    return new AllelicCount(position, numAltReads, numRefReads);
}
Also used : BinomialDistribution(org.apache.commons.math3.distribution.BinomialDistribution) AllelicCount(org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCount)

Example 4 with GammaDistribution

use of org.apache.commons.math3.distribution.GammaDistribution 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 5 with GammaDistribution

use of org.apache.commons.math3.distribution.GammaDistribution in project GDSC-SMLM by aherbert.

the class DiffusionRateTest method plotJumpDistances.

/**
	 * Plot a cumulative histogram and standard histogram of the jump distances.
	 *
	 * @param title
	 *            the title
	 * @param jumpDistances
	 *            the jump distances
	 * @param dimensions
	 *            the number of dimensions for the jumps
	 * @param steps
	 *            the steps
	 */
private void plotJumpDistances(String title, DoubleData jumpDistances, int dimensions) {
    // Cumulative histogram
    // --------------------
    double[] values = jumpDistances.values();
    String title2 = title + " Cumulative Jump Distance " + dimensions + "D";
    double[][] jdHistogram = JumpDistanceAnalysis.cumulativeHistogram(values);
    Plot2 jdPlot = new Plot2(title2, "Distance (um^2)", "Cumulative Probability", jdHistogram[0], jdHistogram[1]);
    PlotWindow pw2 = Utils.display(title2, jdPlot);
    if (Utils.isNewWindow())
        idList[idCount++] = pw2.getImagePlus().getID();
    // Plot the expected function
    // This is the Chi-squared distribution: The sum of the squares of k independent
    // standard normal random variables with k = dimensions. It is a special case of
    // the gamma distribution. If the normals have non-unit variance the distribution 
    // is scaled.
    // Chi       ~ Gamma(k/2, 2)      // using the scale parameterisation of the gamma
    // s^2 * Chi ~ Gamma(k/2, 2*s^2)
    // So if s^2 = 2D:
    // 2D * Chi  ~ Gamma(k/2, 4D)
    double estimatedD = simpleD * simpleSteps;
    double max = Maths.max(values);
    double[] x = Utils.newArray(1000, 0, max / 1000);
    double k = dimensions / 2.0;
    double mean = 4 * estimatedD;
    GammaDistribution dist = new GammaDistribution(k, mean);
    double[] y = new double[x.length];
    for (int i = 0; i < x.length; i++) y[i] = dist.cumulativeProbability(x[i]);
    jdPlot.setColor(Color.red);
    jdPlot.addPoints(x, y, Plot.LINE);
    Utils.display(title2, jdPlot);
    // Histogram
    // ---------
    title2 = title + " Jump " + dimensions + "D";
    int plotId = Utils.showHistogram(title2, jumpDistances, "Distance (um^2)", 0, 0, Math.max(20, values.length / 1000));
    if (Utils.isNewWindow())
        idList[idCount++] = plotId;
    // Recompute the expected function
    for (int i = 0; i < x.length; i++) y[i] = dist.density(x[i]);
    // Scale to have the same area
    if (Utils.xValues.length > 1) {
        final double area1 = jumpDistances.size() * (Utils.xValues[1] - Utils.xValues[0]);
        final double area2 = dist.cumulativeProbability(x[x.length - 1]);
        final double scaleFactor = area1 / area2;
        for (int i = 0; i < y.length; i++) y[i] *= scaleFactor;
    }
    jdPlot = Utils.plot;
    jdPlot.setColor(Color.red);
    jdPlot.addPoints(x, y, Plot.LINE);
    Utils.display(WindowManager.getImage(plotId).getTitle(), jdPlot);
}
Also used : PlotWindow(ij.gui.PlotWindow) Plot2(ij.gui.Plot2) GammaDistribution(org.apache.commons.math3.distribution.GammaDistribution)

Aggregations

GammaDistribution (org.apache.commons.math3.distribution.GammaDistribution)6 AllelicCountCollection (org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCountCollection)4 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2 Pair (org.apache.commons.lang3.tuple.Pair)2 BinomialDistribution (org.apache.commons.math3.distribution.BinomialDistribution)2 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)2 RandomGenerator (org.apache.commons.math3.random.RandomGenerator)2 RandomGeneratorFactory (org.apache.commons.math3.random.RandomGeneratorFactory)2 MathArrays (org.apache.commons.math3.util.MathArrays)2 org.broadinstitute.hellbender.tools.exome (org.broadinstitute.hellbender.tools.exome)2 AlleleFractionGlobalParameters (org.broadinstitute.hellbender.tools.exome.allelefraction.AlleleFractionGlobalParameters)2 AllelicCount (org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCount)2 AllelicPanelOfNormals (org.broadinstitute.hellbender.tools.pon.allelic.AllelicPanelOfNormals)2 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)2 PosteriorSummary (org.broadinstitute.hellbender.utils.mcmc.PosteriorSummary)2 Assert (org.testng.Assert)2 Test (org.testng.annotations.Test)2 StoredDataStatistics (gdsc.core.utils.StoredDataStatistics)1