mev-4.0.01/source/org/tigr/microarray/mev/cluster/gui/impl/som/UMatrixDistanceViewer.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: UMatrixDistanceViewer.java,v $
2 26 Feb 07 jari 7  * $Revision: 1.8 $
2 26 Feb 07 jari 8  * $Date: 2006/03/24 15:51:36 $
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.som;
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.Dimension;
2 26 Feb 07 jari 16 import java.awt.Font;
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.awt.RenderingHints;
2 26 Feb 07 jari 20 import java.awt.event.ComponentAdapter;
2 26 Feb 07 jari 21 import java.awt.event.ComponentEvent;
2 26 Feb 07 jari 22 import java.awt.image.BufferedImage;
2 26 Feb 07 jari 23 import java.beans.Expression;
2 26 Feb 07 jari 24
2 26 Feb 07 jari 25 import javax.swing.JComponent;
2 26 Feb 07 jari 26 import javax.swing.JPanel;
2 26 Feb 07 jari 27
2 26 Feb 07 jari 28 import org.tigr.microarray.mev.cluster.gui.Experiment;
2 26 Feb 07 jari 29 import org.tigr.microarray.mev.cluster.gui.IData;
2 26 Feb 07 jari 30 import org.tigr.microarray.mev.cluster.gui.IDisplayMenu;
2 26 Feb 07 jari 31 import org.tigr.microarray.mev.cluster.gui.IFramework;
2 26 Feb 07 jari 32 import org.tigr.microarray.mev.cluster.gui.IViewer;
2 26 Feb 07 jari 33 import org.tigr.microarray.mev.cluster.gui.impl.util.Hexagon;
2 26 Feb 07 jari 34 import org.tigr.util.FloatMatrix;
2 26 Feb 07 jari 35
2 26 Feb 07 jari 36 public class UMatrixDistanceViewer extends JPanel implements IViewer {
2 26 Feb 07 jari 37 //    public static final long serialVersionUID = 202016040001L;
2 26 Feb 07 jari 38      
2 26 Feb 07 jari 39     private int[][] clusters;
2 26 Feb 07 jari 40     private FloatMatrix u_matrix;
2 26 Feb 07 jari 41     private int dim_x, dim_y;
2 26 Feb 07 jari 42     private String topology;
2 26 Feb 07 jari 43     
2 26 Feb 07 jari 44     private boolean isAntiAliasing = true;
2 26 Feb 07 jari 45     
2 26 Feb 07 jari 46     /**
2 26 Feb 07 jari 47      * Constructs a <code>UMatrixDistanceViewer</code> with specified parameters.
2 26 Feb 07 jari 48      */
2 26 Feb 07 jari 49     public UMatrixDistanceViewer(int[][] clusters, FloatMatrix u_matrix, int dim_x, int dim_y, String topology) {
2 26 Feb 07 jari 50   this.clusters = clusters;
2 26 Feb 07 jari 51   this.u_matrix = u_matrix;
2 26 Feb 07 jari 52   this.dim_x = dim_x;
2 26 Feb 07 jari 53   this.dim_y = dim_y;
2 26 Feb 07 jari 54   this.topology = topology;
2 26 Feb 07 jari 55   
2 26 Feb 07 jari 56   setBackground(Color.white);
2 26 Feb 07 jari 57   setFont(new Font("monospaced", Font.PLAIN, 10));
2 26 Feb 07 jari 58   addComponentListener(new Listener());
2 26 Feb 07 jari 59     }
2 26 Feb 07 jari 60     /**
2 26 Feb 07 jari 61      * An adapter constructor that simply calls UMatrixDistanceViewer(int[][] clusters, FloatMatrix u_matrix, int dim_x, int dim_y, String topology)
2 26 Feb 07 jari 62      */
2 26 Feb 07 jari 63     public UMatrixDistanceViewer(int[][] clusters, FloatMatrix u_matrix, Integer dim_x, Integer dim_y, String topology) {
2 26 Feb 07 jari 64       this(clusters, u_matrix, dim_x.intValue(), dim_y.intValue(), topology);
2 26 Feb 07 jari 65     }
2 26 Feb 07 jari 66     public Expression getExpression(){
2 26 Feb 07 jari 67       return new Expression(this, this.getClass(), "new", 
2 26 Feb 07 jari 68           new Object[]{this.clusters, this.u_matrix, new Integer(dim_x), new Integer(dim_y), this.topology});
2 26 Feb 07 jari 69     }
2 26 Feb 07 jari 70     
2 26 Feb 07 jari 71     /**
2 26 Feb 07 jari 72      * This method is only implemented to satisfy the IViewer interface - it returns 0 always.
2 26 Feb 07 jari 73      */
2 26 Feb 07 jari 74     public int getExperimentID(){return 0;}
2 26 Feb 07 jari 75     /**
2 26 Feb 07 jari 76      * This class is only implemented to satisfy the IViewer interface - does nothing.
2 26 Feb 07 jari 77      */
2 26 Feb 07 jari 78     public void setExperiment(Experiment e) {}
2 26 Feb 07 jari 79     /**
2 26 Feb 07 jari 80      * This class is only implemented to satisfy the IViewer interface - does nothing.
2 26 Feb 07 jari 81      */
2 26 Feb 07 jari 82     public void setExperimentID(int id){}
2 26 Feb 07 jari 83     
2 26 Feb 07 jari 84     /**
2 26 Feb 07 jari 85      * Sets the anti aliasing attribute.
2 26 Feb 07 jari 86      */
2 26 Feb 07 jari 87     public void setAntialiasing(boolean value) {
2 26 Feb 07 jari 88   this.isAntiAliasing = value;
2 26 Feb 07 jari 89     }
2 26 Feb 07 jari 90     
2 26 Feb 07 jari 91     /**
2 26 Feb 07 jari 92      * Paints the component into specified graphics.
2 26 Feb 07 jari 93      */
2 26 Feb 07 jari 94     public void paint(Graphics g1) {
2 26 Feb 07 jari 95   super.paint(g1);
2 26 Feb 07 jari 96   Graphics2D g = (Graphics2D)g1;
2 26 Feb 07 jari 97   if (isAntiAliasing) {
2 26 Feb 07 jari 98       g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
2 26 Feb 07 jari 99       g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
2 26 Feb 07 jari 100   }
2 26 Feb 07 jari 101   final int width = getWidth();
2 26 Feb 07 jari 102   final int height = getHeight();
2 26 Feb 07 jari 103   float stepX;
2 26 Feb 07 jari 104   float stepY;
2 26 Feb 07 jari 105   int xValue, yValue;
2 26 Feb 07 jari 106   Hexagon hexagon;
2 26 Feb 07 jari 107   g.setColor(Color.lightGray);
2 26 Feb 07 jari 108   if (this.topology.equals("hexagonal")) {
2 26 Feb 07 jari 109       stepX = width/(dim_x+0.5f);
2 26 Feb 07 jari 110       stepY = height/(dim_y+0.5f);
2 26 Feb 07 jari 111       hexagon = new Hexagon((int)Math.round(stepX), 270);
2 26 Feb 07 jari 112       hexagon.translate(0, 0);
2 26 Feb 07 jari 113       for (int y=0; y<dim_y; y++) {
2 26 Feb 07 jari 114     if (y%2 == 0) {
2 26 Feb 07 jari 115         hexagon.translate(-hexagon.getA(), 0);
2 26 Feb 07 jari 116     } else {
2 26 Feb 07 jari 117         hexagon.translate(0, 0);
2 26 Feb 07 jari 118     }
2 26 Feb 07 jari 119     for (int x=0; x<dim_x; x++) {
2 26 Feb 07 jari 120         hexagon.translate((int)Math.round(stepX), 0);
2 26 Feb 07 jari 121         if (this.clusters[x*dim_y+y].length == 0) {
2 26 Feb 07 jari 122       g.setColor(new Color(230,230,230));
2 26 Feb 07 jari 123       g.fillPolygon(hexagon);
2 26 Feb 07 jari 124       g.setColor(Color.lightGray);
2 26 Feb 07 jari 125         }
2 26 Feb 07 jari 126         g.drawPolygon(hexagon);
2 26 Feb 07 jari 127     }
2 26 Feb 07 jari 128     if (y%2 == 0) {
2 26 Feb 07 jari 129         hexagon.translate(-(int)Math.round(stepX)*dim_x+hexagon.getA(), hexagon.getB()+hexagon.getC());
2 26 Feb 07 jari 130     } else {
2 26 Feb 07 jari 131         hexagon.translate(-(int)Math.round(stepX)*dim_x, hexagon.getB()+hexagon.getC());
2 26 Feb 07 jari 132     }
2 26 Feb 07 jari 133       }
2 26 Feb 07 jari 134       Hexagon aHexagon;
2 26 Feb 07 jari 135       g.setColor(Color.black);
2 26 Feb 07 jari 136       for (int y=0; y<dim_y; y++) {
2 26 Feb 07 jari 137     for (int x=0; x<dim_x; x++) {
2 26 Feb 07 jari 138         xValue = (int)(stepX*this.u_matrix.get(x, y));
2 26 Feb 07 jari 139         aHexagon = new Hexagon(xValue, 270);
2 26 Feb 07 jari 140         aHexagon.translate(0, 0);
2 26 Feb 07 jari 141         aHexagon.translate((int)Math.round(stepX)*x, (int)Math.round((hexagon.getB()+hexagon.getC())*y+(hexagon.getHeight()-aHexagon.getHeight())/2));
2 26 Feb 07 jari 142         if ((y%2)==0) {
2 26 Feb 07 jari 143       aHexagon.translate(hexagon.getA(), 0);
2 26 Feb 07 jari 144         } else {
2 26 Feb 07 jari 145       aHexagon.translate((int)stepX, 0);
2 26 Feb 07 jari 146         }
2 26 Feb 07 jari 147         g.drawPolygon(aHexagon);
2 26 Feb 07 jari 148     }
2 26 Feb 07 jari 149       }
2 26 Feb 07 jari 150   } else {
2 26 Feb 07 jari 151       stepX = (float)width/(float)dim_x;
2 26 Feb 07 jari 152       stepY = (float)height/(float)dim_y;
2 26 Feb 07 jari 153       g.setColor(new Color(230,230,230));
2 26 Feb 07 jari 154       for (int y=0; y<dim_y; y++) {
2 26 Feb 07 jari 155     for (int x=0; x<dim_x; x++) {
2 26 Feb 07 jari 156         if (this.clusters[x*dim_y+y].length == 0) {
2 26 Feb 07 jari 157       g.fillRect((int)Math.round(x*stepX)+1, (int)Math.round(y*stepY)+1, (int)Math.round(stepX)-1, (int)Math.round(stepY)-1);
2 26 Feb 07 jari 158         }
2 26 Feb 07 jari 159     }
2 26 Feb 07 jari 160       }
2 26 Feb 07 jari 161       g.setColor(Color.lightGray);
2 26 Feb 07 jari 162       for (int i=0; i<dim_x; i++) {
2 26 Feb 07 jari 163     g.drawLine((int)Math.round(i*stepX), 0,(int)Math.round(i*stepX), height);
2 26 Feb 07 jari 164       }
2 26 Feb 07 jari 165       for (int i=0; i<dim_y; i++) {
2 26 Feb 07 jari 166     g.drawLine(0, (int)Math.round(i*stepY), width, (int)Math.round(i*stepY));
2 26 Feb 07 jari 167       }
2 26 Feb 07 jari 168       for (int y=0; y<dim_y; y++) {
2 26 Feb 07 jari 169     for (int x=0; x<dim_x; x++) {
2 26 Feb 07 jari 170         xValue=(int)Math.round(stepX*this.u_matrix.get(x, y));
2 26 Feb 07 jari 171         yValue=(int)Math.round(stepY*this.u_matrix.get(x, y));
2 26 Feb 07 jari 172         g.setColor(Color.black);
2 26 Feb 07 jari 173         g.drawRect((int)Math.round(x*stepX+(stepX-xValue)/2), (int)Math.round(y*stepY+((stepY-yValue)/2)), (int)xValue, (int)yValue);
2 26 Feb 07 jari 174     }
2 26 Feb 07 jari 175       }
2 26 Feb 07 jari 176   }
2 26 Feb 07 jari 177     }
2 26 Feb 07 jari 178     
2 26 Feb 07 jari 179     /**
2 26 Feb 07 jari 180      * @return null.
2 26 Feb 07 jari 181      */
2 26 Feb 07 jari 182     public JComponent getHeaderComponent() {
2 26 Feb 07 jari 183   return null;
2 26 Feb 07 jari 184     }
2 26 Feb 07 jari 185     
2 26 Feb 07 jari 186     /**
2 26 Feb 07 jari 187      * Returns this component.
2 26 Feb 07 jari 188      */
2 26 Feb 07 jari 189     public JComponent getContentComponent() {
2 26 Feb 07 jari 190   return this;
2 26 Feb 07 jari 191     }
2 26 Feb 07 jari 192     
2 26 Feb 07 jari 193     /**
2 26 Feb 07 jari 194      * @return null.
2 26 Feb 07 jari 195      */
2 26 Feb 07 jari 196     public BufferedImage getImage() {
2 26 Feb 07 jari 197   return null;
2 26 Feb 07 jari 198     }
2 26 Feb 07 jari 199     
2 26 Feb 07 jari 200     /**
2 26 Feb 07 jari 201      * Updates anti aliasing attributes when the viewer is selected.
2 26 Feb 07 jari 202      */
2 26 Feb 07 jari 203     public void onSelected(IFramework framework) {
2 26 Feb 07 jari 204   setAntialiasing(framework.getDisplayMenu().isAntiAliasing());
2 26 Feb 07 jari 205     }
2 26 Feb 07 jari 206     
2 26 Feb 07 jari 207     public void onDataChanged(IData data) {}
2 26 Feb 07 jari 208     
2 26 Feb 07 jari 209     /**
2 26 Feb 07 jari 210      * Updates anti aliasing attributes when the display menu is changed.
2 26 Feb 07 jari 211      */
2 26 Feb 07 jari 212     public void onMenuChanged(IDisplayMenu menu) {
2 26 Feb 07 jari 213   setAntialiasing(menu.isAntiAliasing());
2 26 Feb 07 jari 214     }
2 26 Feb 07 jari 215     
2 26 Feb 07 jari 216     public void onDeselected() {}
2 26 Feb 07 jari 217     public void onClosed() {}
2 26 Feb 07 jari 218     
2 26 Feb 07 jari 219     /**
2 26 Feb 07 jari 220      * Updates the viewer sizes.
2 26 Feb 07 jari 221      */
2 26 Feb 07 jari 222     private void updateSize() {
2 26 Feb 07 jari 223   if (this.topology.equals("hexagonal")) {
2 26 Feb 07 jari 224       float stepX = (float)getWidth()/((float)dim_x+0.5f);
2 26 Feb 07 jari 225       Hexagon hexagon = new Hexagon((int)Math.round(stepX), 270);
2 26 Feb 07 jari 226       int height = dim_y*(hexagon.getB()+hexagon.getC())+hexagon.getB();
2 26 Feb 07 jari 227       setPreferredSize(new Dimension(100, height));
2 26 Feb 07 jari 228   } else {
2 26 Feb 07 jari 229       setPreferredSize(new Dimension(100, 100));
2 26 Feb 07 jari 230   }
2 26 Feb 07 jari 231     }
2 26 Feb 07 jari 232     
2 26 Feb 07 jari 233     /** Returns a component to be inserted into the scroll pane row header
2 26 Feb 07 jari 234      */
2 26 Feb 07 jari 235     public JComponent getRowHeaderComponent() {
2 26 Feb 07 jari 236         return null;
2 26 Feb 07 jari 237     }
2 26 Feb 07 jari 238     
2 26 Feb 07 jari 239     /** Returns the corner component corresponding to the indicated corner,
2 26 Feb 07 jari 240      * posibly null
2 26 Feb 07 jari 241      */
2 26 Feb 07 jari 242     public JComponent getCornerComponent(int cornerIndex) {
2 26 Feb 07 jari 243         return null;
2 26 Feb 07 jari 244     }
2 26 Feb 07 jari 245
2 26 Feb 07 jari 246     
2 26 Feb 07 jari 247     public int[][] getClusters() {
2 26 Feb 07 jari 248         return null;
2 26 Feb 07 jari 249     }
2 26 Feb 07 jari 250     
2 26 Feb 07 jari 251     public Experiment getExperiment() {
2 26 Feb 07 jari 252         return null;
2 26 Feb 07 jari 253     }
2 26 Feb 07 jari 254     
2 26 Feb 07 jari 255     /** Returns int value indicating viewer type
2 26 Feb 07 jari 256      * Cluster.GENE_CLUSTER, Cluster.EXPERIMENT_CLUSTER, or -1 for both or unspecified
2 26 Feb 07 jari 257      */
2 26 Feb 07 jari 258     public int getViewerType() {
2 26 Feb 07 jari 259         return -1;
2 26 Feb 07 jari 260     }
2 26 Feb 07 jari 261     
2 26 Feb 07 jari 262     /**
2 26 Feb 07 jari 263      * The class to listen to the component events.
2 26 Feb 07 jari 264      */
2 26 Feb 07 jari 265     private class Listener extends ComponentAdapter {
2 26 Feb 07 jari 266   
2 26 Feb 07 jari 267   private boolean isRevalidate = true;
2 26 Feb 07 jari 268   
2 26 Feb 07 jari 269   public void componentResized(ComponentEvent e) {
2 26 Feb 07 jari 270       if (!this.isRevalidate) {
2 26 Feb 07 jari 271     this.isRevalidate = true;
2 26 Feb 07 jari 272     return;
2 26 Feb 07 jari 273       }
2 26 Feb 07 jari 274       // reset flag to ignore the next event
2 26 Feb 07 jari 275       this.isRevalidate = false;
2 26 Feb 07 jari 276       updateSize();
2 26 Feb 07 jari 277       revalidate();
2 26 Feb 07 jari 278   }
2 26 Feb 07 jari 279     }
2 26 Feb 07 jari 280 }