Search in sources :

Example 1 with Histogram

use of ubic.basecode.math.distribution.Histogram in project Gemma by PavlidisLab.

the class DifferentialExpressionAnalyzerServiceImpl method addPvalueDistribution.

private void addPvalueDistribution(ExpressionAnalysisResultSet resultSet) {
    Histogram pvalHist = new Histogram("", 100, 0.0, 1.0);
    for (DifferentialExpressionAnalysisResult result : resultSet.getResults()) {
        Double pvalue = result.getPvalue();
        if (pvalue != null)
            pvalHist.fill(pvalue);
    }
    PvalueDistribution pvd = PvalueDistribution.Factory.newInstance();
    pvd.setNumBins(100);
    ByteArrayConverter bac = new ByteArrayConverter();
    pvd.setBinCounts(bac.doubleArrayToBytes(pvalHist.getArray()));
    // do not save yet.
    resultSet.setPvalueDistribution(pvd);
}
Also used : Histogram(ubic.basecode.math.distribution.Histogram) ByteArrayConverter(ubic.basecode.io.ByteArrayConverter) DifferentialExpressionAnalysisResult(ubic.gemma.model.analysis.expression.diff.DifferentialExpressionAnalysisResult) PvalueDistribution(ubic.gemma.model.analysis.expression.diff.PvalueDistribution)

Example 2 with Histogram

use of ubic.basecode.math.distribution.Histogram in project Gemma by PavlidisLab.

the class ExpressionExperimentQCController method getDiffExPvalueHistXYSeries.

/**
 * @return JFreeChart XYSeries representing the histogram for the requested result set
 */
private XYSeries getDiffExPvalueHistXYSeries(ExpressionExperiment ee, Long analysisId, Long rsId, String factorName) {
    if (ee == null || analysisId == null || rsId == null) {
        log.warn("Got invalid values: " + ee + " " + analysisId + " " + rsId + " " + factorName);
        return null;
    }
    Histogram hist = differentialExpressionResultService.loadPvalueDistribution(rsId);
    XYSeries xySeries;
    if (hist != null) {
        xySeries = new XYSeries(rsId, true, true);
        Double[] binEdges = hist.getBinEdges();
        double[] counts = hist.getArray();
        assert binEdges.length == counts.length;
        for (int i = 0; i < binEdges.length; i++) {
            xySeries.add(binEdges[i].doubleValue(), counts[i]);
        }
        return xySeries;
    }
    return null;
}
Also used : XYSeries(org.jfree.data.xy.XYSeries) Histogram(ubic.basecode.math.distribution.Histogram)

Example 3 with Histogram

use of ubic.basecode.math.distribution.Histogram in project Gemma by PavlidisLab.

the class TwoChannelMissingValuesImpl method computeSignalThreshold.

/**
 * Determine a threshold based on the data.
 */
private Double computeSignalThreshold(ExpressionDataDoubleMatrix preferred, ExpressionDataDoubleMatrix signalChannelA, ExpressionDataDoubleMatrix signalChannelB, ExpressionDataDoubleMatrix baseChannel) {
    Double min = Double.MAX_VALUE;
    Double max = Double.MIN_VALUE;
    for (ExpressionDataMatrixRowElement element : baseChannel.getRowElements()) {
        CompositeSequence designElement = element.getDesignElement();
        int numCols = preferred.columns(designElement);
        for (int col = 0; col < numCols; col++) {
            Double[] signalA = null;
            if (signalChannelA != null) {
                signalA = signalChannelA.getRow(designElement);
            }
            Double[] signalB = null;
            if (signalChannelB != null) {
                signalB = signalChannelB.getRow(designElement);
            }
            Double sigAV = (signalA == null || signalA[col] == null) ? Double.NaN : signalA[col];
            Double sigBV = (signalB == null || signalB[col] == null) ? Double.NaN : signalB[col];
            if (!sigAV.isNaN() && sigAV < min) {
                min = sigAV;
            } else if (!sigBV.isNaN() && sigBV < min) {
                min = sigBV;
            } else if (!sigAV.isNaN() && sigAV > max) {
                max = sigAV;
            } else if (!sigBV.isNaN() && sigBV > max) {
                max = sigBV;
            }
        }
    }
    Histogram h = new Histogram("range", 100, min, max);
    for (ExpressionDataMatrixRowElement element : baseChannel.getRowElements()) {
        CompositeSequence designElement = element.getDesignElement();
        int numCols = preferred.columns(designElement);
        for (int col = 0; col < numCols; col++) {
            Double[] signalA = null;
            if (signalChannelA != null) {
                signalA = signalChannelA.getRow(designElement);
            }
            Double[] signalB = null;
            if (signalChannelB != null) {
                signalB = signalChannelB.getRow(designElement);
            }
            Double sigAV = (signalA == null || signalA[col] == null) ? Double.NaN : signalA[col];
            Double sigBV = (signalB == null || signalB[col] == null) ? Double.NaN : signalB[col];
            if (!sigAV.isNaN())
                h.fill(sigAV);
            if (!sigBV.isNaN())
                h.fill(sigBV);
        }
    }
    Double thresh = h.getApproximateQuantile(TwoChannelMissingValuesImpl.QUANTILE_OF_SIGNAL_TO_USE_IF_NO_BKG_AVAILABLE);
    TwoChannelMissingValuesImpl.log.info("Threshold based on signal=" + thresh);
    return thresh;
}
Also used : ExpressionDataMatrixRowElement(ubic.gemma.core.datastructure.matrix.ExpressionDataMatrixRowElement) Histogram(ubic.basecode.math.distribution.Histogram) CompositeSequence(ubic.gemma.model.expression.designElement.CompositeSequence)

Example 4 with Histogram

use of ubic.basecode.math.distribution.Histogram in project Gemma by PavlidisLab.

the class ComBat method plot.

/**
 * Make diagnostic plots.
 * FIXME: As in the original ComBat, this only graphs the first batch's statistics. In principle we can (and perhaps
 * should) examine these plots for all the batches.
 *
 * @param filePrefix file prefix
 */
public void plot(String filePrefix) {
    if (this.gammaHat == null)
        throw new IllegalArgumentException("You must call 'run' first");
    /*
         * View the distribution of gammaHat, which we assume will have a normal distribution
         */
    DoubleMatrix1D ghr = gammaHat.viewRow(0);
    int NUM_HIST_BINS = 100;
    Histogram gammaHatHist = new Histogram("GammaHat", NUM_HIST_BINS, ghr);
    XYSeries ghplot = gammaHatHist.plot();
    Normal rn = new Normal(this.gammaBar.get(0), Math.sqrt(this.t2.get(0)), new MersenneTwister());
    Histogram ghtheoryT = new Histogram("Gamma", NUM_HIST_BINS, gammaHatHist.min(), gammaHatHist.max());
    for (int i = 0; i < 10000; i++) {
        double n = rn.nextDouble();
        ghtheoryT.fill(n);
    }
    XYSeries ghtheory = ghtheoryT.plot();
    File tmpfile;
    try {
        tmpfile = File.createTempFile(filePrefix + ".gammahat.histogram.", ".png");
        ComBat.log.info(tmpfile);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    try (OutputStream os = new FileOutputStream(tmpfile)) {
        this.writePlot(os, ghplot, ghtheory);
        /*
             * View the distribution of deltaHat, which we assume has an inverse gamma distribution
             */
        DoubleMatrix1D dhr = deltaHat.viewRow(0);
        Histogram deltaHatHist = new Histogram("DeltaHat", NUM_HIST_BINS, dhr);
        XYSeries dhplot = deltaHatHist.plot();
        Gamma g = new Gamma(aPrior.get(0), bPrior.get(0), new MersenneTwister());
        Histogram deltaHatT = new Histogram("Delta", NUM_HIST_BINS, deltaHatHist.min(), deltaHatHist.max());
        for (int i = 0; i < 10000; i++) {
            double invg = 1.0 / g.nextDouble();
            deltaHatT.fill(invg);
        }
        XYSeries dhtheory = deltaHatT.plot();
        tmpfile = File.createTempFile(filePrefix + ".deltahat.histogram.", ".png");
        ComBat.log.info(tmpfile);
        try (OutputStream os2 = new FileOutputStream(tmpfile)) {
            this.writePlot(os2, dhplot, dhtheory);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : XYSeries(org.jfree.data.xy.XYSeries) Histogram(ubic.basecode.math.distribution.Histogram) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Normal(cern.jet.random.Normal) Gamma(cern.jet.random.Gamma) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) DenseDoubleMatrix1D(cern.colt.matrix.impl.DenseDoubleMatrix1D) FileOutputStream(java.io.FileOutputStream) MersenneTwister(cern.jet.random.engine.MersenneTwister) File(java.io.File)

Example 5 with Histogram

use of ubic.basecode.math.distribution.Histogram in project Gemma by PavlidisLab.

the class DifferentialExpressionAnalyzerServiceTest method testWritePValuesHistogram.

@Test
public void testWritePValuesHistogram() {
    DifferentialExpressionAnalysisConfig config = new DifferentialExpressionAnalysisConfig();
    Collection<ExperimentalFactor> factors = ee.getExperimentalDesign().getExperimentalFactors();
    config.setFactorsToInclude(factors);
    Collection<DifferentialExpressionAnalysis> analyses = differentialExpressionAnalyzerService.runDifferentialExpressionAnalyses(ee, config);
    for (DifferentialExpressionAnalysis analysis : analyses) {
        differentialExpressionAnalysisService.thaw(analysis);
        for (ExpressionAnalysisResultSet resultSet : analysis.getResultSets()) {
            Histogram hist = resultService.loadPvalueDistribution(resultSet.getId());
            assertNotNull(hist);
        }
    }
}
Also used : Histogram(ubic.basecode.math.distribution.Histogram) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) DifferentialExpressionAnalysis(ubic.gemma.model.analysis.expression.diff.DifferentialExpressionAnalysis) ExpressionAnalysisResultSet(ubic.gemma.model.analysis.expression.diff.ExpressionAnalysisResultSet) AbstractGeoServiceTest(ubic.gemma.core.loader.expression.geo.AbstractGeoServiceTest) Test(org.junit.Test)

Aggregations

Histogram (ubic.basecode.math.distribution.Histogram)6 XYSeries (org.jfree.data.xy.XYSeries)2 ByteArrayConverter (ubic.basecode.io.ByteArrayConverter)2 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)1 DenseDoubleMatrix1D (cern.colt.matrix.impl.DenseDoubleMatrix1D)1 Gamma (cern.jet.random.Gamma)1 Normal (cern.jet.random.Normal)1 MersenneTwister (cern.jet.random.engine.MersenneTwister)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 BigInteger (java.math.BigInteger)1 Test (org.junit.Test)1 ExpressionDataMatrixRowElement (ubic.gemma.core.datastructure.matrix.ExpressionDataMatrixRowElement)1 AbstractGeoServiceTest (ubic.gemma.core.loader.expression.geo.AbstractGeoServiceTest)1 DifferentialExpressionAnalysis (ubic.gemma.model.analysis.expression.diff.DifferentialExpressionAnalysis)1 DifferentialExpressionAnalysisResult (ubic.gemma.model.analysis.expression.diff.DifferentialExpressionAnalysisResult)1 ExpressionAnalysisResultSet (ubic.gemma.model.analysis.expression.diff.ExpressionAnalysisResultSet)1 PvalueDistribution (ubic.gemma.model.analysis.expression.diff.PvalueDistribution)1