guineu.modules.dataanalysis.PCA.ProjectionPlotPanel.java Source code

Java tutorial

Introduction

Here is the source code for guineu.modules.dataanalysis.PCA.ProjectionPlotPanel.java

Source

/*
 * Copyright 2007-2013 VTT Biotechnology
 * This file is part of Guineu.
 *
 * Guineu is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * Guineu is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * Guineu; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
 * Fifth Floor, Boston, MA 02110-1301 USA
 */
package guineu.modules.dataanalysis.PCA;

import guineu.data.Dataset;
import guineu.data.PeakListRow;
import guineu.data.impl.datasets.SimpleBasicDataset;
import guineu.data.impl.peaklists.SimplePeakListRowOther;
import guineu.main.GuineuCore;
import guineu.parameters.ParameterSet;
import java.awt.Color;
import java.awt.Font;
import java.text.NumberFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.ui.RectangleInsets;

/**
 * @author Taken from MZmine2 http://mzmine.sourceforge.net/
 *
 */
public class ProjectionPlotPanel extends ChartPanel {

    private static final Color gridColor = Color.lightGray;
    private static final Font titleFont = new Font("SansSerif", Font.PLAIN, 11);
    private static final float dataPointAlpha = 0.8f;
    private JFreeChart chart;
    private XYPlot plot;
    private ProjectionPlotItemLabelGenerator itemLabelGenerator;
    private ProjectionPlotRenderer spotRenderer;

    public ProjectionPlotPanel(ProjectionPlotWindow masterFrame, ProjectionPlotDataset dataset,
            ParameterSet parameters) {
        super(null);

        boolean createLegend = true;
        if (dataset.getNumberOfGroups() > 20) {
            createLegend = false;
        }

        chart = ChartFactory.createXYAreaChart("", dataset.getXLabel(), dataset.getYLabel(), dataset,
                PlotOrientation.VERTICAL, createLegend, false, false);
        chart.setBackgroundPaint(Color.white);

        setChart(chart);

        // title

        TextTitle chartTitle = chart.getTitle();
        chartTitle.setMargin(5, 0, 0, 0);
        chartTitle.setFont(titleFont);
        chart.removeSubtitle(chartTitle);

        // disable maximum size (we don't want scaling)
        setMaximumDrawWidth(Integer.MAX_VALUE);
        setMaximumDrawHeight(Integer.MAX_VALUE);

        // set the plot properties
        plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.white);
        plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));

        // set grid properties
        plot.setDomainGridlinePaint(gridColor);
        plot.setRangeGridlinePaint(gridColor);

        // set crosshair (selection) properties
        plot.setDomainCrosshairVisible(false);
        plot.setRangeCrosshairVisible(false);

        plot.setForegroundAlpha(dataPointAlpha);

        NumberFormat numberFormat = NumberFormat.getNumberInstance();

        // set the X axis (component 1) properties
        NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
        xAxis.setNumberFormatOverride(numberFormat);

        // set the Y axis (component 2) properties
        NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
        yAxis.setNumberFormatOverride(numberFormat);

        plot.setDataset(dataset);

        spotRenderer = new ProjectionPlotRenderer(plot, dataset);
        itemLabelGenerator = new ProjectionPlotItemLabelGenerator(parameters);
        spotRenderer.setBaseItemLabelGenerator(itemLabelGenerator);
        spotRenderer.setBaseItemLabelsVisible(true);
        spotRenderer.setBaseToolTipGenerator(new ProjectionPlotToolTipGenerator(parameters));
        plot.setRenderer(spotRenderer);

        // Setup legend
        if (createLegend) {
            LegendItemCollection legendItemsCollection = new LegendItemCollection();
            for (int groupNumber = 0; groupNumber < dataset.getNumberOfGroups(); groupNumber++) {
                Object paramValue = dataset.getGroupParameterValue(groupNumber);
                if (paramValue == null) {
                    // No parameter value available: search for raw data files
                    // within this group, and use their names as group's name
                    String fileNames = new String();
                    for (int itemNumber = 0; itemNumber < dataset.getItemCount(0); itemNumber++) {
                        String rawDataFile = dataset.getRawDataFile(itemNumber);
                        if (dataset.getGroupNumber(itemNumber) == groupNumber) {
                            fileNames = fileNames.concat(rawDataFile);
                        }
                    }
                    if (fileNames.length() == 0) {
                        fileNames = "Empty group";
                    }

                    paramValue = fileNames;
                }
                Color nextColor = (Color) spotRenderer.getGroupPaint(groupNumber);
                Color groupColor = new Color(nextColor.getRed(), nextColor.getGreen(), nextColor.getBlue(),
                        (int) Math.round(255 * dataPointAlpha));
                legendItemsCollection.add(new LegendItem(paramValue.toString(), "-", null, null,
                        spotRenderer.getDataPointsShape(), groupColor));
            }
            plot.setFixedLegendItems(legendItemsCollection);
        } else {

            Dataset legends = new SimpleBasicDataset("Legends");
            legends.addColumnName("Samples");

            for (int groupNumber = 0; groupNumber < dataset.getNumberOfGroups(); groupNumber++) {
                Object paramValue = dataset.getGroupParameterValue(groupNumber);
                if (paramValue == null) {
                    // No parameter value available: search for raw data files
                    // within this group, and use their names as group's name
                    String fileNames = new String();
                    for (int itemNumber = 0; itemNumber < dataset.getItemCount(0); itemNumber++) {
                        String rawDataFile = dataset.getRawDataFile(itemNumber);
                        if (dataset.getGroupNumber(itemNumber) == groupNumber) {
                            fileNames = fileNames.concat(rawDataFile.toString());
                        }
                    }
                    if (fileNames.length() == 0) {
                        fileNames = "Empty group";
                    }

                    paramValue = fileNames;
                }

                Color nextColor = (Color) spotRenderer.getGroupPaint(groupNumber);
                Color groupColor = new Color(nextColor.getRed(), nextColor.getGreen(), nextColor.getBlue(),
                        (int) Math.round(255 * dataPointAlpha));

                PeakListRow row = new SimplePeakListRowOther();
                row.setPeak("Samples", paramValue.toString());
                legends.addRow(row);
                legends.addRowColor(groupColor);
            }

            GuineuCore.getDesktop().AddNewFile(legends);
        }

    }

    protected void cycleItemLabelMode() {
        itemLabelGenerator.cycleLabelMode();
        spotRenderer.setBaseItemLabelsVisible(true);
    }
}