mev-4.0.01/source/org/tigr/microarray/mev/cluster/gui/impl/pca/PlotVectorViewer.java

Code
Comments
Other
Rev Date Author Line
2 26 Feb 07 jari 1 /*
2 26 Feb 07 jari 2 Copyright @ 1999-2004, The Institute for Genomic Research (TIGR).
2 26 Feb 07 jari 3 All rights reserved.
2 26 Feb 07 jari 4 */
2 26 Feb 07 jari 5 /*
2 26 Feb 07 jari 6  * $RCSfile: PlotVectorViewer.java,v $
2 26 Feb 07 jari 7  * $Revision: 1.7 $
2 26 Feb 07 jari 8  * $Date: 2006/03/24 15:51:05 $
2 26 Feb 07 jari 9  * $Author: eleanorahowe $
2 26 Feb 07 jari 10  * $State: Exp $
2 26 Feb 07 jari 11  */
2 26 Feb 07 jari 12 package org.tigr.microarray.mev.cluster.gui.impl.pca;
2 26 Feb 07 jari 13
2 26 Feb 07 jari 14 import java.awt.Color;
2 26 Feb 07 jari 15 import java.awt.Font;
2 26 Feb 07 jari 16 import java.awt.FontMetrics;
2 26 Feb 07 jari 17 import java.awt.Graphics;
2 26 Feb 07 jari 18 import java.awt.Graphics2D;
2 26 Feb 07 jari 19 import java.beans.Expression;
2 26 Feb 07 jari 20
2 26 Feb 07 jari 21 import javax.swing.JComponent;
2 26 Feb 07 jari 22 import javax.swing.JPanel;
2 26 Feb 07 jari 23 import javax.swing.JTextArea;
2 26 Feb 07 jari 24
2 26 Feb 07 jari 25 import org.tigr.microarray.mev.cluster.gui.IFramework;
2 26 Feb 07 jari 26 import org.tigr.microarray.mev.cluster.gui.impl.ViewerAdapter;
2 26 Feb 07 jari 27 import org.tigr.util.FloatMatrix;
2 26 Feb 07 jari 28
2 26 Feb 07 jari 29 public class PlotVectorViewer extends ViewerAdapter implements java.io.Serializable {
2 26 Feb 07 jari 30     
2 26 Feb 07 jari 31     private JComponent content;
2 26 Feb 07 jari 32     private int current = -1;
2 26 Feb 07 jari 33     FloatMatrix T;
2 26 Feb 07 jari 34     
2 26 Feb 07 jari 35     /**
2 26 Feb 07 jari 36      * Constructs a <code>PlotVectorViewer</code> for specified T-matrix.
2 26 Feb 07 jari 37      */
2 26 Feb 07 jari 38     public PlotVectorViewer(FloatMatrix T) {
2 26 Feb 07 jari 39   content = createContent(T);
2 26 Feb 07 jari 40       this.T = T;
2 26 Feb 07 jari 41     }
2 26 Feb 07 jari 42     /**
2 26 Feb 07 jari 43      * @inheritDoc
2 26 Feb 07 jari 44      */
2 26 Feb 07 jari 45     public Expression getExpression(){
2 26 Feb 07 jari 46       return new Expression(this, this.getClass(), "new", 
2 26 Feb 07 jari 47           new Object[]{T});
2 26 Feb 07 jari 48     }
2 26 Feb 07 jari 49     /**
2 26 Feb 07 jari 50      * Returns the viewer content.
2 26 Feb 07 jari 51      */
2 26 Feb 07 jari 52     public JComponent getContentComponent() {
2 26 Feb 07 jari 53   return content;
2 26 Feb 07 jari 54     }
2 26 Feb 07 jari 55     
2 26 Feb 07 jari 56     /**
2 26 Feb 07 jari 57      * Updates the viewer for a selected framework node.
2 26 Feb 07 jari 58      */
2 26 Feb 07 jari 59     public void onSelected(IFramework framework) {
2 26 Feb 07 jari 60   Object userObject = framework.getUserObject();
2 26 Feb 07 jari 61   if (userObject instanceof Integer) {
2 26 Feb 07 jari 62       current = ((Integer)userObject).intValue();
2 26 Feb 07 jari 63       return;
2 26 Feb 07 jari 64   }
2 26 Feb 07 jari 65   current = -1;
2 26 Feb 07 jari 66     }
2 26 Feb 07 jari 67     
2 26 Feb 07 jari 68     /**
2 26 Feb 07 jari 69      * Creates the viewer content.
2 26 Feb 07 jari 70      */
2 26 Feb 07 jari 71     private JComponent createContent(FloatMatrix T) {
2 26 Feb 07 jari 72   return new Plot(T);
2 26 Feb 07 jari 73     }
2 26 Feb 07 jari 74     
2 26 Feb 07 jari 75     /** Returns a component to be inserted into the scroll pane row header
2 26 Feb 07 jari 76      */
2 26 Feb 07 jari 77     public JComponent getRowHeaderComponent() {
2 26 Feb 07 jari 78         return null;
2 26 Feb 07 jari 79     }
2 26 Feb 07 jari 80     
2 26 Feb 07 jari 81     /**
2 26 Feb 07 jari 82      * The class to draw the viewer content.
2 26 Feb 07 jari 83      */
2 26 Feb 07 jari 84     private class Plot extends JPanel {
2 26 Feb 07 jari 85   
2 26 Feb 07 jari 86   private static final int left = 40;
2 26 Feb 07 jari 87   private static final int top  = 40;
2 26 Feb 07 jari 88   private FloatMatrix T;
2 26 Feb 07 jari 89   private Font font = new Font("monospaced", Font.BOLD, 10);
2 26 Feb 07 jari 90   
2 26 Feb 07 jari 91   /**
2 26 Feb 07 jari 92    * Constructs a <code>Plot</code> for specified T-matrix.
2 26 Feb 07 jari 93    */
2 26 Feb 07 jari 94   public Plot(FloatMatrix T) {
2 26 Feb 07 jari 95       setBackground(Color.white);
2 26 Feb 07 jari 96       setFont(font);
2 26 Feb 07 jari 97       this.T = T;
2 26 Feb 07 jari 98   }
2 26 Feb 07 jari 99   
2 26 Feb 07 jari 100   /**
2 26 Feb 07 jari 101    * Pains the content into specified graphics.
2 26 Feb 07 jari 102    */
2 26 Feb 07 jari 103   public void paint(Graphics g) {
2 26 Feb 07 jari 104       super.paint(g);
2 26 Feb 07 jari 105       final int rows = T.getRowDimension();
2 26 Feb 07 jari 106       if (rows < 1) {
2 26 Feb 07 jari 107     return;
2 26 Feb 07 jari 108       }
2 26 Feb 07 jari 109       int plotWidth  = getWidth()  - 80;
2 26 Feb 07 jari 110       int plotHeight = getHeight() - 80;
2 26 Feb 07 jari 111       if (current == -1 || plotWidth < 5 || plotHeight < 5) {
2 26 Feb 07 jari 112     return;
2 26 Feb 07 jari 113       }
2 26 Feb 07 jari 114       double stepY = 1;
2 26 Feb 07 jari 115       double factor = plotHeight;
2 26 Feb 07 jari 116       double stepX = plotWidth/(rows-1.0);
2 26 Feb 07 jari 117       double maxValue = T.get(0, current);
2 26 Feb 07 jari 118       if (Double.isNaN(maxValue)) {
2 26 Feb 07 jari 119     return;
2 26 Feb 07 jari 120       }
2 26 Feb 07 jari 121       for (int i=0; i<rows; i++) {
2 26 Feb 07 jari 122     if (Math.abs(T.get(i, current)) > maxValue) {
2 26 Feb 07 jari 123         maxValue = Math.abs(T.get(i, current));
2 26 Feb 07 jari 124     }
2 26 Feb 07 jari 125       }
2 26 Feb 07 jari 126       double scale = 1.0;
2 26 Feb 07 jari 127       int counter=1;
2 26 Feb 07 jari 128       int steps=1;
2 26 Feb 07 jari 129       if (maxValue>=1) {
2 26 Feb 07 jari 130     while (maxValue>=10) {
2 26 Feb 07 jari 131         maxValue/=10.0;
2 26 Feb 07 jari 132         counter*=10;
2 26 Feb 07 jari 133     }
2 26 Feb 07 jari 134     steps=((int)Math.round(maxValue+0.5));
2 26 Feb 07 jari 135     stepY = plotHeight/steps;
2 26 Feb 07 jari 136     factor /= (scale*2.0);
2 26 Feb 07 jari 137       } else {
2 26 Feb 07 jari 138     while (maxValue <= 1) {
2 26 Feb 07 jari 139         maxValue *= 10.0;
2 26 Feb 07 jari 140         counter *= 10.0;
2 26 Feb 07 jari 141     }
2 26 Feb 07 jari 142     scale = (int)(Math.round(maxValue+0.5));
2 26 Feb 07 jari 143     steps = ((int)Math.round(maxValue+0.5));
2 26 Feb 07 jari 144     stepY = plotHeight/(steps*2);
2 26 Feb 07 jari 145       }
2 26 Feb 07 jari 146       factor = factor/(scale*2.0)*counter;
2 26 Feb 07 jari 147       int curValue;
2 26 Feb 07 jari 148       int zeroValue = top + plotHeight/2;
2 26 Feb 07 jari 149       g.setColor(Color.black);
2 26 Feb 07 jari 150       g.drawRect(left, top, plotWidth, plotHeight);
2 26 Feb 07 jari 151       g.drawLine(left, top+plotHeight/2, left+plotWidth, top+plotHeight/2);
2 26 Feb 07 jari 152       int width;
2 26 Feb 07 jari 153       FontMetrics metrics = g.getFontMetrics();
2 26 Feb 07 jari 154       int height = metrics.getHeight();
2 26 Feb 07 jari 155       for (int i=1; i<steps; i++) {
2 26 Feb 07 jari 156     g.drawLine(left, top+(int)Math.round(i*stepY), left+5, top+(int)Math.round(i*stepY));
2 26 Feb 07 jari 157       }
2 26 Feb 07 jari 158       for (int i=steps+1; i<steps*2; i++) {
2 26 Feb 07 jari 159     g.drawLine(left, top+(int)Math.round(i*stepY), left+5, top+(int)Math.round(i*stepY));
2 26 Feb 07 jari 160       }
2 26 Feb 07 jari 161       for (int i=1; i<rows; i++) {
2 26 Feb 07 jari 162     g.drawLine(left+(int)Math.round(i*stepX), top+plotHeight-5, left+(int)Math.round(i*stepX), top+plotHeight);
2 26 Feb 07 jari 163       }
2 26 Feb 07 jari 164       g.setColor(Color.magenta);
2 26 Feb 07 jari 165       g.clipRect(left, top, plotWidth, plotHeight);
2 26 Feb 07 jari 166       int prevValue = -(int)Math.round(T.get(0, current)*factor);
2 26 Feb 07 jari 167       for (int i=1; i<rows; i++) {
2 26 Feb 07 jari 168     curValue = -(int)Math.round(T.get(i, current)*factor);
2 26 Feb 07 jari 169     g.drawLine(left+(int)Math.round((i-1)*stepX), zeroValue+prevValue, left+(int)Math.round(i*stepX), zeroValue+curValue);
2 26 Feb 07 jari 170     prevValue = curValue;
2 26 Feb 07 jari 171       }
2 26 Feb 07 jari 172       g.setColor(new Color(0,0,128));
2 26 Feb 07 jari 173       for (int i=0; i<rows; i++) {
2 26 Feb 07 jari 174     curValue = -(int)Math.round(T.get(i, current)*factor);
2 26 Feb 07 jari 175     g.fillOval(left+(int)Math.round(i*stepX)-3, zeroValue+curValue-3,6,6);
2 26 Feb 07 jari 176       }
2 26 Feb 07 jari 177       g.setClip(0, 0, getWidth(), getHeight());
2 26 Feb 07 jari 178       String str;
2 26 Feb 07 jari 179       for (int i=0; i<=steps*2; i++) {
2 26 Feb 07 jari 180     str = String.valueOf((scale-i)/counter);
2 26 Feb 07 jari 181     width = metrics.stringWidth(str);
2 26 Feb 07 jari 182     g.drawString(str, left-10-width, top+(int)Math.round(i*stepY)+5);
2 26 Feb 07 jari 183       }
2 26 Feb 07 jari 184       ((Graphics2D)g).rotate(-Math.PI/2.0);
2 26 Feb 07 jari 185       for (int i=0; i<rows; i++) {
2 26 Feb 07 jari 186     str = String.valueOf(i+1);
2 26 Feb 07 jari 187     width = metrics.stringWidth(str);
2 26 Feb 07 jari 188     g.drawString(str, -top-plotHeight-10-width, left+5+(int)Math.round(i*stepX));
2 26 Feb 07 jari 189       }
2 26 Feb 07 jari 190       ((Graphics2D)g).rotate(Math.PI/2.0);
2 26 Feb 07 jari 191   }
2 26 Feb 07 jari 192     }
2 26 Feb 07 jari 193 }