Search in sources :

Example 31 with ExperimentalFactor

use of ubic.gemma.model.expression.experiment.ExperimentalFactor in project Gemma by PavlidisLab.

the class ExperimentalDesignUtils method getBaselineConditions.

public static Map<ExperimentalFactor, FactorValue> getBaselineConditions(List<BioMaterial> samplesUsed, List<ExperimentalFactor> factors) {
    Map<ExperimentalFactor, FactorValue> baselineConditions = ExpressionDataMatrixColumnSort.getBaselineLevels(samplesUsed, factors);
    /*
         * For factors that don't have an obvious baseline, use the first factorvalue.
         */
    Collection<FactorValue> factorValuesOfFirstSample = samplesUsed.iterator().next().getFactorValues();
    for (ExperimentalFactor factor : factors) {
        if (!baselineConditions.containsKey(factor)) {
            for (FactorValue biomf : factorValuesOfFirstSample) {
                /*
                     * the first biomaterial has the values used as baseline
                     */
                if (biomf.getExperimentalFactor().equals(factor)) {
                    baselineConditions.put(factor, biomf);
                }
            }
        }
    }
    return baselineConditions;
}
Also used : FactorValue(ubic.gemma.model.expression.experiment.FactorValue) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor)

Example 32 with ExperimentalFactor

use of ubic.gemma.model.expression.experiment.ExperimentalFactor in project Gemma by PavlidisLab.

the class ExpressionAnalysisResultSetDaoImpl method thawLite.

@Override
public void thawLite(final ExpressionAnalysisResultSet resultSet) {
    Session session = this.getSessionFactory().getCurrentSession();
    session.buildLockRequest(LockOptions.NONE).lock(resultSet);
    for (ExperimentalFactor factor : resultSet.getExperimentalFactors()) {
        Hibernate.initialize(factor);
    }
    Hibernate.initialize(resultSet.getAnalysis());
    Hibernate.initialize(resultSet.getAnalysis().getExperimentAnalyzed());
}
Also used : ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) Session(org.hibernate.Session)

Example 33 with ExperimentalFactor

use of ubic.gemma.model.expression.experiment.ExperimentalFactor in project Gemma by PavlidisLab.

the class ExpressionExperimentQCController method getFactorNames.

private Map<Long, String> getFactorNames(ExpressionExperiment ee, int maxWidth) {
    Collection<ExperimentalFactor> factors = ee.getExperimentalDesign().getExperimentalFactors();
    Map<Long, String> efs = new HashMap<>();
    for (ExperimentalFactor ef : factors) {
        efs.put(ef.getId(), StringUtils.abbreviate(StringUtils.capitalize(ef.getName()), maxWidth));
    }
    return efs;
}
Also used : ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor)

Example 34 with ExperimentalFactor

use of ubic.gemma.model.expression.experiment.ExperimentalFactor in project Gemma by PavlidisLab.

the class ExpressionExperimentQCController method writeDetailedFactorAnalysis.

private boolean writeDetailedFactorAnalysis(ExpressionExperiment ee, OutputStream os) throws Exception {
    SVDValueObject svdo = svdService.getSvdFactorAnalysis(ee.getId());
    if (svdo == null)
        return false;
    if (svdo.getFactors().isEmpty() && svdo.getDates().isEmpty()) {
        return false;
    }
    Map<Integer, Map<Long, Double>> factorCorrelations = svdo.getFactorCorrelations();
    // Map<Integer, Map<Long, Double>> factorPvalues = svdo.getFactorPvalues();
    Map<Integer, Double> dateCorrelations = svdo.getDateCorrelations();
    assert ee.getId().equals(svdo.getId());
    // need the experimental design
    ee = expressionExperimentService.thawLite(ee);
    int maxWidth = 30;
    Map<Long, String> efs = this.getFactorNames(ee, maxWidth);
    Map<Long, ExperimentalFactor> efIdMap = EntityUtils.getIdMap(ee.getExperimentalDesign().getExperimentalFactors());
    Collection<Long> continuousFactors = new HashSet<>();
    for (ExperimentalFactor ef : ee.getExperimentalDesign().getExperimentalFactors()) {
        boolean isContinous = ExperimentalDesignUtils.isContinuous(ef);
        if (isContinous) {
            continuousFactors.add(ef.getId());
        }
    }
    /*
         * Make plots of the dates vs. PCs, factors vs. PCs.
         */
    int MAX_COMP = 3;
    Map<Long, List<JFreeChart>> charts = new LinkedHashMap<>();
    ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
    /*
         * FACTORS
         */
    String componentShorthand = "PC";
    for (Integer component : factorCorrelations.keySet()) {
        if (component >= MAX_COMP)
            break;
        String xaxisLabel = componentShorthand + (component + 1);
        for (Long efId : factorCorrelations.get(component).keySet()) {
            /*
                 * Should not happen.
                 */
            if (!efs.containsKey(efId)) {
                log.warn("No experimental factor with id " + efId);
                continue;
            }
            if (!svdo.getFactors().containsKey(efId)) {
                // this should not happen.
                continue;
            }
            boolean isCategorical = !continuousFactors.contains(efId);
            Map<Long, String> categories = new HashMap<>();
            if (isCategorical) {
                this.getCategories(efIdMap, efId, categories);
            }
            if (!charts.containsKey(efId)) {
                charts.put(efId, new ArrayList<JFreeChart>());
            }
            Double a = factorCorrelations.get(component).get(efId);
            // unique?
            String plotname = (efs.get(efId) == null ? "?" : efs.get(efId)) + " " + xaxisLabel;
            if (a != null && !Double.isNaN(a)) {
                String title = plotname + " " + String.format("%.2f", a);
                List<Double> values = svdo.getFactors().get(efId);
                Double[] eigenGene = this.getEigenGene(svdo, component);
                assert values.size() == eigenGene.length;
                /*
                     * Plot eigengene vs values, add correlation to the plot
                     */
                JFreeChart chart;
                if (isCategorical) {
                    /*
                         * Categorical factor
                         */
                    // use the absolute value of the correlation, since direction is arbitrary.
                    title = plotname + " " + String.format("r=%.2f", Math.abs(a));
                    DefaultMultiValueCategoryDataset dataset = new DefaultMultiValueCategoryDataset();
                    /*
                         * What this code does is organize the factor values by the groups.
                         */
                    Map<String, List<Double>> groupedValues = new TreeMap<>();
                    for (int i = 0; i < values.size(); i++) {
                        Long fvId = values.get(i).longValue();
                        String fvValue = categories.get(fvId);
                        if (fvValue == null) {
                            // is this all we need to do?
                            continue;
                        }
                        if (!groupedValues.containsKey(fvValue)) {
                            groupedValues.put(fvValue, new ArrayList<Double>());
                        }
                        groupedValues.get(fvValue).add(eigenGene[i]);
                        if (log.isDebugEnabled())
                            log.debug(fvValue + " " + values.get(i));
                    }
                    for (String key : groupedValues.keySet()) {
                        dataset.add(groupedValues.get(key), plotname, key);
                    }
                    // don't show the name of the X axis: it's redundant with the title.
                    NumberAxis rangeAxis = new NumberAxis(xaxisLabel);
                    rangeAxis.setAutoRangeIncludesZero(false);
                    // rangeAxis.setAutoRange( false );
                    rangeAxis.setAutoRangeMinimumSize(4.0);
                    // rangeAxis.setRange( new Range( -2, 2 ) );
                    CategoryPlot plot = new CategoryPlot(dataset, new CategoryAxis(null), rangeAxis, new ScatterRenderer());
                    plot.setRangeGridlinesVisible(false);
                    plot.setDomainGridlinesVisible(false);
                    chart = new JFreeChart(title, new Font("SansSerif", Font.BOLD, 12), plot, false);
                    ScatterRenderer renderer = (ScatterRenderer) plot.getRenderer();
                    float saturationDrop = (float) Math.min(1.0, component * 0.8f / MAX_COMP);
                    renderer.setSeriesFillPaint(0, Color.getHSBColor(0.0f, 1.0f - saturationDrop, 0.7f));
                    renderer.setSeriesShape(0, new Ellipse2D.Double(0, 0, 3, 3));
                    renderer.setUseOutlinePaint(false);
                    renderer.setUseFillPaint(true);
                    renderer.setBaseFillPaint(Color.white);
                    CategoryAxis domainAxis = plot.getDomainAxis();
                    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
                } else {
                    /*
                         * Continuous value factor
                         */
                    DefaultXYDataset series = new DefaultXYDataset();
                    series.addSeries(plotname, new double[][] { ArrayUtils.toPrimitive(values.toArray(new Double[] {})), ArrayUtils.toPrimitive(eigenGene) });
                    // don't show x-axis label, which would otherwise be efs.get( efId )
                    chart = ChartFactory.createScatterPlot(title, null, xaxisLabel, series, PlotOrientation.VERTICAL, false, false, false);
                    XYPlot plot = chart.getXYPlot();
                    plot.setRangeGridlinesVisible(false);
                    plot.setDomainGridlinesVisible(false);
                    XYItemRenderer renderer = plot.getRenderer();
                    renderer.setBasePaint(Color.white);
                    renderer.setSeriesShape(0, new Ellipse2D.Double(0, 0, 3, 3));
                    float saturationDrop = (float) Math.min(1.0, component * 0.8f / MAX_COMP);
                    renderer.setSeriesPaint(0, Color.getHSBColor(0.0f, 1.0f - saturationDrop, 0.7f));
                    plot.setRenderer(renderer);
                }
                chart.getTitle().setFont(new Font("SansSerif", Font.BOLD, 12));
                charts.get(efId).add(chart);
            }
        }
    }
    /*
         * DATES
         */
    charts.put(-1L, new ArrayList<JFreeChart>());
    for (Integer component : dateCorrelations.keySet()) {
        String xaxisLabel = componentShorthand + (component + 1);
        List<Date> dates = svdo.getDates();
        if (dates.isEmpty())
            break;
        long secspan = ubic.basecode.util.DateUtil.numberOfSecondsBetweenDates(dates);
        if (component >= MAX_COMP)
            break;
        Double a = dateCorrelations.get(component);
        if (a != null && !Double.isNaN(a)) {
            Double[] eigenGene = svdo.getvMatrix().getColObj(component);
            /*
                 * Plot eigengene vs values, add correlation to the plot
                 */
            TimeSeries series = new TimeSeries("Dates vs. eigen" + (component + 1));
            int i = 0;
            for (Date d : dates) {
                // if span is less than an hour, retain the minute.
                if (secspan < 60 * 60) {
                    series.addOrUpdate(new Minute(d), eigenGene[i++]);
                } else {
                    series.addOrUpdate(new Hour(d), eigenGene[i++]);
                }
            }
            TimeSeriesCollection dataset = new TimeSeriesCollection();
            dataset.addSeries(series);
            JFreeChart chart = ChartFactory.createTimeSeriesChart("Dates: " + xaxisLabel + " " + String.format("r=%.2f", a), null, xaxisLabel, dataset, false, false, false);
            XYPlot xyPlot = chart.getXYPlot();
            chart.getTitle().setFont(new Font("SansSerif", Font.BOLD, 12));
            // standard renderer makes lines.
            XYDotRenderer renderer = new XYDotRenderer();
            renderer.setBaseFillPaint(Color.white);
            renderer.setDotHeight(3);
            renderer.setDotWidth(3);
            // has no effect, need dotheight.
            renderer.setSeriesShape(0, new Ellipse2D.Double(0, 0, 3, 3));
            float saturationDrop = (float) Math.min(1.0, component * 0.8f / MAX_COMP);
            renderer.setSeriesPaint(0, Color.getHSBColor(0.0f, 1.0f - saturationDrop, 0.7f));
            ValueAxis domainAxis = xyPlot.getDomainAxis();
            domainAxis.setVerticalTickLabels(true);
            xyPlot.setRenderer(renderer);
            xyPlot.setRangeGridlinesVisible(false);
            xyPlot.setDomainGridlinesVisible(false);
            charts.get(-1L).add(chart);
        }
    }
    /*
         * Plot in a grid, with each factor as a column. FIXME What if we have too many factors to fit on the screen?
         */
    int columns = (int) Math.ceil(charts.size());
    int perChartSize = ExpressionExperimentQCController.DEFAULT_QC_IMAGE_SIZE_PX;
    BufferedImage image = new BufferedImage(columns * perChartSize, MAX_COMP * perChartSize, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    int currentX = 0;
    int currentY = 0;
    for (Long id : charts.keySet()) {
        for (JFreeChart chart : charts.get(id)) {
            this.addChartToGraphics(chart, g2, currentX, currentY, perChartSize, perChartSize);
            if (currentY + perChartSize < MAX_COMP * perChartSize) {
                currentY += perChartSize;
            } else {
                currentY = 0;
                currentX += perChartSize;
            }
        }
    }
    os.write(ChartUtilities.encodeAsPNG(image));
    return true;
}
Also used : TimeSeries(org.jfree.data.time.TimeSeries) Ellipse2D(java.awt.geom.Ellipse2D) DoubleArrayList(cern.colt.list.DoubleArrayList) List(java.util.List) XYItemRenderer(org.jfree.chart.renderer.xy.XYItemRenderer) DefaultMultiValueCategoryDataset(org.jfree.data.statistics.DefaultMultiValueCategoryDataset) Hour(org.jfree.data.time.Hour) XYDotRenderer(org.jfree.chart.renderer.xy.XYDotRenderer) CategoryPlot(org.jfree.chart.plot.CategoryPlot) XYPlot(org.jfree.chart.plot.XYPlot) NumberAxis(org.jfree.chart.axis.NumberAxis) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor) SVDValueObject(ubic.gemma.core.analysis.preprocess.svd.SVDValueObject) BufferedImage(java.awt.image.BufferedImage) Minute(org.jfree.data.time.Minute) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) ValueAxis(org.jfree.chart.axis.ValueAxis) DefaultXYDataset(org.jfree.data.xy.DefaultXYDataset) JFreeChart(org.jfree.chart.JFreeChart) CategoryAxis(org.jfree.chart.axis.CategoryAxis) ScatterRenderer(org.jfree.chart.renderer.category.ScatterRenderer)

Example 35 with ExperimentalFactor

use of ubic.gemma.model.expression.experiment.ExperimentalFactor in project Gemma by PavlidisLab.

the class SampleCoexpressionMatrixServiceImpl method regressMajorFactors.

/**
 * Regress out any 'major' factors, work with residuals only
 *
 * @param ee  the experiment to load the factors from
 * @param mat the double matrix of processed vectors to regress
 * @return regressed double matrix
 */
private ExpressionDataDoubleMatrix regressMajorFactors(ExpressionExperiment ee, ExpressionDataDoubleMatrix mat) {
    double importanceThreshold = 0.01;
    Set<ExperimentalFactor> importantFactors = svdService.getImportantFactors(ee, ee.getExperimentalDesign().getExperimentalFactors(), importanceThreshold);
    /* Remove 'batch' from important factors */
    ExperimentalFactor batch = null;
    for (ExperimentalFactor factor : importantFactors) {
        if (factor.getName().toLowerCase().equals("batch"))
            batch = factor;
    }
    if (batch != null) {
        importantFactors.remove(batch);
        SampleCoexpressionMatrixServiceImpl.log.info("Removed 'batch' from the list of significant factors.");
    }
    if (!importantFactors.isEmpty()) {
        SampleCoexpressionMatrixServiceImpl.log.info("Regressing out covariates");
        DifferentialExpressionAnalysisConfig config = new DifferentialExpressionAnalysisConfig();
        config.setFactorsToInclude(importantFactors);
        mat = lma.regressionResiduals(mat, config, true);
    }
    return mat;
}
Also used : DifferentialExpressionAnalysisConfig(ubic.gemma.core.analysis.expression.diff.DifferentialExpressionAnalysisConfig) ExperimentalFactor(ubic.gemma.model.expression.experiment.ExperimentalFactor)

Aggregations

ExperimentalFactor (ubic.gemma.model.expression.experiment.ExperimentalFactor)88 Test (org.junit.Test)31 FactorValue (ubic.gemma.model.expression.experiment.FactorValue)30 DifferentialExpressionAnalysis (ubic.gemma.model.analysis.expression.diff.DifferentialExpressionAnalysis)26 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)22 ExpressionExperiment (ubic.gemma.model.expression.experiment.ExpressionExperiment)18 HashSet (java.util.HashSet)17 AbstractGeoServiceTest (ubic.gemma.core.loader.expression.geo.AbstractGeoServiceTest)17 ExpressionAnalysisResultSet (ubic.gemma.model.analysis.expression.diff.ExpressionAnalysisResultSet)16 BioAssay (ubic.gemma.model.expression.bioAssay.BioAssay)14 DifferentialExpressionAnalysisResult (ubic.gemma.model.analysis.expression.diff.DifferentialExpressionAnalysisResult)12 CompositeSequence (ubic.gemma.model.expression.designElement.CompositeSequence)11 GeoDomainObjectGeneratorLocal (ubic.gemma.core.loader.expression.geo.GeoDomainObjectGeneratorLocal)10 AlreadyExistsInSystemException (ubic.gemma.core.loader.util.AlreadyExistsInSystemException)10 Before (org.junit.Before)8 Collection (java.util.Collection)7 ContrastResult (ubic.gemma.model.analysis.expression.diff.ContrastResult)6 AnalysisType (ubic.gemma.core.analysis.expression.diff.DifferentialExpressionAnalyzerServiceImpl.AnalysisType)5 ExperimentalFactorValueObject (ubic.gemma.model.expression.experiment.ExperimentalFactorValueObject)5 InputStream (java.io.InputStream)4