All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eqtlmappingpipeline.graphics.map.Heatmap Maven / Gradle / Ivy

The newest version!
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eqtlmappingpipeline.graphics.map;

import eqtlmappingpipeline.graphics.Graphics;

/**
 *
 * @author harm-jan
 */
public class Heatmap extends Graphics {

    protected int r, g, b, a;
    protected boolean useAlphaGradient, colSorted, rowSorted;
    protected String[] rowNames, colNames;
    protected int[] rowOrder, colOrder;
    protected int desiredWidth, desiredHeight;
    protected boolean invertGradient;

    public Heatmap(int width, int height) {

        super();

        desiredWidth    = width;
        desiredHeight   = height;
        r = 0;
        g = 0;
        b = 255;
        a = 0;
        useAlphaGradient = true;
        invertGradient = false;
        colSorted = false;
        rowSorted = false;
        rowNames = null;
        colNames = null;
        rowOrder = null;
        colOrder = null;

        setMargins(50);
    }

     public Heatmap(int width, int height, boolean outputPDF, String outputLoc){
        super(width, height, outputPDF, outputLoc);
        desiredWidth    = width;
        desiredHeight   = height;
        r = 0;
        g = 0;
        b = 255;
        a = 0;
        useAlphaGradient = true;
        invertGradient = false;
        colSorted = false;
        rowSorted = false;
        rowNames = null;
        colNames = null;
        rowOrder = null;
        colOrder = null;

        setMargins(50);
        
    }

    public void plot(int[][] matrix) {}

    public void plot(double[][] matrix) {
        
        // we have set the desired width, and desired height, although this might not fit...
        graphWidth  = desiredWidth;
        graphHeight = desiredHeight;
        calcDrawArea();

        int numRows = matrix.length;
        int numCols = matrix[numRows - 1].length;

        int boxWidth  = (int) Math.floor((double) drawWidth  / numCols);
        int boxHeight = (int) Math.floor((double) drawHeight / numRows);

        double min  = Double.MAX_VALUE;
        double max  = Double.MIN_VALUE;
        for (int row = 0; row < numRows; row++) {
            for (int col = 0; col < numCols; col++) {
                double val = matrix[row][col];
                if (val > max) {
                    max = val;
                }
                if (val < min) {
                    min = val;
                }
            }
        }

        if(min < 0.0){
            min = Math.abs(min);
            for (int row = 0; row < numRows; row++) {
                for (int col = 0; col < numCols; col++) {
                    double val = matrix[row][col];
                    matrix[row][col] = val + min;
                }
            }
            max = max + min;
            min = 0.0;
        }

        double range = max;

        if (boxWidth == 0 || boxHeight == 0) {
            System.out.println("Cannot plot a heatmap on " + drawWidth + "x" + drawHeight + " for a matrix of " + numRows + "x" + numCols);
        } else {
            // i want symmetrical boxes, so take the lowest value...
            if (boxWidth > boxHeight) {
                boxWidth = boxHeight;
            } else {
                boxHeight = boxWidth;
            }

            setFont(boxWidth, "Monospaced");
            
            int maxRowNameLength = 0;
            int maxColNameLength = 0;
            
            if(rowNames != null){
                // find the maximal length of the string within the rownames
                maxRowNameLength = getMaxStringLength(rowNames);
            }

            if(colNames != null){
                // find the maximal length of the string within the rownames
                maxColNameLength = getMaxStringLength(colNames);
            }

            int calculatedWidth  = marginLeft + (boxWidth * numCols) + maxColNameLength + boxWidth + marginRight;
            int calculatedHeight = marginTop +  (boxWidth * numRows) + maxRowNameLength + boxWidth + marginBottom;

            // initialize the graphing objects
            init(calculatedWidth, calculatedHeight);
            setMargins(50);

            // plot the title
            setFont(15, "Georgia");

            // draw the grid
            drawGrid(numRows, numCols, boxWidth);

            // draw colored boxes in a certain order 
            if(colSorted && rowSorted){
                for(int row=0; row < numRows; row++){
                    int rowPosition = rowOrder[row];
                    for(int col=0; col < numCols; col++){
                        int colPosition = colOrder[col];
                        double val = matrix[row][col];
                        determineColor(val, min, range);
                        int y = (rowPosition * boxWidth) + marginTop;
                        int x = (colPosition * boxWidth) + marginLeft;
                        drawRect(x, y, boxWidth, boxHeight, true);
                    }
                }
            } else if (colSorted && !rowSorted){
                for(int row=0; row < numRows; row++){
                    int rowPosition = row;
                    for(int col=0; col < numCols; col++){
                        int colPosition = colOrder[col];
                        double val = matrix[row][col];
                        determineColor(val, min, range);
                        int y = (rowPosition * boxWidth) + marginTop;
                        int x = (colPosition * boxWidth) + marginLeft;
                        drawRect(x, y, boxWidth, boxHeight, true);
                    }
                }
            } else if (rowSorted && !colSorted){
                for(int row=0; row < numRows; row++){
                    int rowPosition = rowOrder[row];
                    for(int col=0; col < numCols; col++){
                        int colPosition = col;
                        double val = matrix[row][col];
                        determineColor(val, min, range);
                        int y = (rowPosition * boxWidth) + marginTop;
                        int x = (colPosition * boxWidth) + marginLeft;
                        drawRect(x, y, boxWidth, boxHeight, true);
                    }
                }
            } else {
                for (int row = 0; row < numRows; row++) {
                    for (int col = 0; col < numCols; col++) {
                        double val = matrix[row][col];
                        determineColor(val, min, range);
                        int y = (row * boxWidth) + marginTop;
                        int x = (col * boxWidth) + marginLeft;
                        drawRect(x, y, boxWidth, boxHeight, true);
                    }
                }
            }

            drawLabels(numRows, numCols, boxWidth, 0);
        }
    }

    protected void drawLabels(int numRows, int numCols, int boxWidth, int margin){
        setColor(128,128,128,255);
        setFont(boxWidth, "Verdana");
        // draw labels
        if(rowNames != null){
            int x = (numCols * boxWidth) + marginLeft + boxWidth + margin;
            if(rowSorted){
                for(int row=0; row 255){
                alpha = 255;
		System.out.println("Warming: value for alpha higher than max!: "+ value + "\t" + range);
            }
	    if(alpha < 0){
		alpha = 0;
		System.out.println("Warming: value for alpha lower than min!: "+ value + "\t" + range);
	    }
            setColor(r, g, b, alpha);
        } else {
            // determine color gradient from baseColor
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy