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