Search in sources :

Example 1 with ScatterRenderer

use of org.jfree.chart.renderer.category.ScatterRenderer 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)

Aggregations

DoubleArrayList (cern.colt.list.DoubleArrayList)1 Ellipse2D (java.awt.geom.Ellipse2D)1 BufferedImage (java.awt.image.BufferedImage)1 List (java.util.List)1 JFreeChart (org.jfree.chart.JFreeChart)1 CategoryAxis (org.jfree.chart.axis.CategoryAxis)1 NumberAxis (org.jfree.chart.axis.NumberAxis)1 ValueAxis (org.jfree.chart.axis.ValueAxis)1 CategoryPlot (org.jfree.chart.plot.CategoryPlot)1 XYPlot (org.jfree.chart.plot.XYPlot)1 ScatterRenderer (org.jfree.chart.renderer.category.ScatterRenderer)1 XYDotRenderer (org.jfree.chart.renderer.xy.XYDotRenderer)1 XYItemRenderer (org.jfree.chart.renderer.xy.XYItemRenderer)1 DefaultMultiValueCategoryDataset (org.jfree.data.statistics.DefaultMultiValueCategoryDataset)1 Hour (org.jfree.data.time.Hour)1 Minute (org.jfree.data.time.Minute)1 TimeSeries (org.jfree.data.time.TimeSeries)1 TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)1 DefaultXYDataset (org.jfree.data.xy.DefaultXYDataset)1 SVDValueObject (ubic.gemma.core.analysis.preprocess.svd.SVDValueObject)1