GeMSE.GS.Analysis.Stats.OneSamplePearsonsCorrelationPanel.java Source code

Java tutorial

Introduction

Here is the source code for GeMSE.GS.Analysis.Stats.OneSamplePearsonsCorrelationPanel.java

Source

/** GenoMetric Space Explorer (GeMSE) Copyright (C) 2017 Vahid Jalili
 *  This program 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 3 of the License, or
 *  (at your option) any later version.
 *  This program 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 this program; if not, write to the Free Software Foundation,
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */
package GeMSE.GS.Analysis.Stats;

import ExternalLibraries.HeatChart;
import GeMSE.GlobalVariables;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import org.apache.commons.math3.stat.correlation.PearsonsCorrelation;

/**
 *
 * @author Vahid Jalili
 */
public final class OneSamplePearsonsCorrelationPanel extends javax.swing.JPanel {
    public OneSamplePearsonsCorrelationPanel() {
        initComponents();

        Color.RGBtoHSB(214, 217, 223, bColor);
    }

    private double[][] _data;
    private PearsonsCorrelation _pearsonCorrelation;
    float[] bColor = new float[3];

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        HeatmapL = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        matrixDG = new javax.swing.JTable();

        HeatmapL.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        HeatmapL.setText("GeMSE");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout
                .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                                .addComponent(HeatmapL, javax.swing.GroupLayout.DEFAULT_SIZE, 607, Short.MAX_VALUE)
                                .addContainerGap()));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                        .addComponent(HeatmapL, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
                        .addContainerGap()));

        jTabbedPane1.addTab("   Heat Map  ", jPanel1);

        matrixDG.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { {}, {}, {}, {} }, new String[] {

        }));
        matrixDG.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        matrixDG.setCellSelectionEnabled(true);
        matrixDG.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jScrollPane1.setViewportView(matrixDG);

        jTabbedPane1.addTab("   Grid View   ", jScrollPane1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addContainerGap().addComponent(jTabbedPane1).addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap()
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 531, Short.MAX_VALUE)
                        .addContainerGap()));
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel HeatmapL;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable matrixDG;
    // End of variables declaration//GEN-END:variables

    public void RunAnalysis(double[][] data) {
        if (data == null)
            return;
        if (data.length < 3 || data[0].length < 2)
            return;
        _data = data;
        RunAnalysis();
    }

    private void RunAnalysis() {
        if (_data == null)
            return;
        _pearsonCorrelation = new PearsonsCorrelation(_data);
        double[][] matrix = (_pearsonCorrelation.getCorrelationMatrix()).getData();
        Plot(matrix);
        GridView(matrix);
    }

    private void Plot(double[][] matrix) {
        HeatChart heatChart = new HeatChart(matrix);
        heatChart.setBackgroundColour(Color.getHSBColor(bColor[0], bColor[1], bColor[2]));
        heatChart.setChartMargin(10);

        Dimension dimension = new Dimension(HeatmapL.getSize().width - 50, HeatmapL.getSize().height - 50);
        dimension.height = (int) Math
                .round((dimension.height - (heatChart.getChartMargin() * 5.0)) / matrix.length);
        dimension.width = (int) Math
                .round((dimension.width - (heatChart.getChartMargin() * 5.0)) / matrix[0].length);
        if (dimension.height < 1)
            dimension.height = 1;
        if (dimension.width < 1)
            dimension.width = 1;

        String[] xValues = new String[matrix.length];
        for (int i = 0; i < xValues.length; i++)
            xValues[i] = "";
        heatChart.setXValues(xValues);

        String[] yValues = new String[matrix[0].length];
        for (int i = 0; i < yValues.length; i++)
            yValues[i] = "";
        heatChart.setYValues(yValues);

        heatChart.setHighValueColour(GlobalVariables.hightValueColor);
        heatChart.setLowValueColour(GlobalVariables.lowValueColor);

        heatChart.setCellSize(dimension);

        HeatmapL.setText("");
        HeatmapL.setIcon(new javax.swing.ImageIcon(heatChart.getChartImage(false)));
    }

    private void GridView(double[][] matrix) {
        DefaultTableModel spaceTabMod = new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        String[] columnTitle = new String[matrix[0].length + 1];
        columnTitle[0] = "";
        for (int col = 1; col < columnTitle.length; col++)
            columnTitle[col] = "Column " + String.valueOf(col);
        spaceTabMod.setColumnIdentifiers(columnTitle);

        int col = 0;
        for (int r = 0; r < matrix.length; r++) {
            String[] row = new String[matrix[0].length + 1];
            row[0] = "Row " + Integer.toString(r);

            for (col = 0; col < matrix[0].length; col++)
                row[col + 1] = Double.toString(matrix[r][col]);

            spaceTabMod.addRow(row);
        }

        matrixDG.setModel(spaceTabMod);
        matrixDG.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        matrixDG.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        ResizeColumnWidth(matrixDG);
    }

    public void ResizeColumnWidth(JTable table) {
        final TableColumnModel columnModel = table.getColumnModel();
        for (int column = 0; column < table.getColumnCount(); column++) {
            int width = 50; // Min width

            TableColumn tableColumn = columnModel.getColumn(column);
            TableCellRenderer renderer = tableColumn.getHeaderRenderer();
            if (renderer == null)
                renderer = table.getTableHeader().getDefaultRenderer();

            Component component = renderer.getTableCellRendererComponent(table, tableColumn.getHeaderValue(), false,
                    false, -1, column);
            width = Math.max(component.getPreferredSize().width + 5, width);

            for (int row = 0; row < table.getRowCount(); row++) {
                renderer = table.getCellRenderer(row, column);
                Component comp = table.prepareRenderer(renderer, row, column);
                width = Math.max(comp.getPreferredSize().width + 5, width);
            }
            if (width > 400)
                width = 400;
            columnModel.getColumn(column).setPreferredWidth(width);
        }
    }
}