mev-4.0.01/source/org/tigr/microarray/mev/cluster/gui/impl/ptm/PTMSubCentroidViewer.java

Code
Comments
Other
Rev Date Author Line
2 26 Feb 07 jari 1 /*
2 26 Feb 07 jari 2 Copyright @ 1999-2003, 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: PTMSubCentroidViewer.java,v $
2 26 Feb 07 jari 7  * $Revision: 1.5 $
2 26 Feb 07 jari 8  * $Date: 2006/05/02 16:56:57 $
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
2 26 Feb 07 jari 13 package org.tigr.microarray.mev.cluster.gui.impl.ptm;
2 26 Feb 07 jari 14
2 26 Feb 07 jari 15 import java.awt.Color;
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.awt.Rectangle;
2 26 Feb 07 jari 20 import java.awt.RenderingHints;
2 26 Feb 07 jari 21 import java.util.Vector;
2 26 Feb 07 jari 22
2 26 Feb 07 jari 23 import org.tigr.microarray.mev.cluster.gui.Experiment;
2 26 Feb 07 jari 24 import org.tigr.microarray.mev.cluster.gui.helpers.CentroidViewer;
2 26 Feb 07 jari 25
2 26 Feb 07 jari 26 /**
2 26 Feb 07 jari 27  *
2 26 Feb 07 jari 28  * @author  nbhagaba
2 26 Feb 07 jari 29  * @version
2 26 Feb 07 jari 30  */
2 26 Feb 07 jari 31 public class PTMSubCentroidViewer extends CentroidViewer {
2 26 Feb 07 jari 32     
2 26 Feb 07 jari 33     Vector templateVector;
2 26 Feb 07 jari 34     
2 26 Feb 07 jari 35     /** Creates new PTMSubCentroidViewer */
2 26 Feb 07 jari 36     public PTMSubCentroidViewer(Experiment experiment, int[][] clusters, Vector templateVector) {
2 26 Feb 07 jari 37     super(experiment, clusters);
2 26 Feb 07 jari 38     this.templateVector = templateVector;
2 26 Feb 07 jari 39     }
2 26 Feb 07 jari 40     /**
2 26 Feb 07 jari 41      * @inheritDoc
2 26 Feb 07 jari 42      */
2 26 Feb 07 jari 43     public PTMSubCentroidViewer(Experiment e, int[][] clusters, float[][] variances, float[][] means, float[][] codes) {
2 26 Feb 07 jari 44       super(e, clusters, variances, means, codes);
2 26 Feb 07 jari 45     }
2 26 Feb 07 jari 46     
2 26 Feb 07 jari 47     /**
2 26 Feb 07 jari 48      * Paints chart into specified graphics.
2 26 Feb 07 jari 49      */
2 26 Feb 07 jari 50     public void paint(Graphics g) {
2 26 Feb 07 jari 51     FontMetrics metrics = g.getFontMetrics();
2 26 Feb 07 jari 52     Rectangle rect = new Rectangle(40, 20, getWidth()-80, getHeight() - 40 - getNamesWidth(metrics));
2 26 Feb 07 jari 53     paint((Graphics2D)g, rect, true);
2 26 Feb 07 jari 54     }
2 26 Feb 07 jari 55     
2 26 Feb 07 jari 56     /**
2 26 Feb 07 jari 57      * Paints chart into specified graphics and with specified bounds.
2 26 Feb 07 jari 58      */
2 26 Feb 07 jari 59     public void paint(Graphics2D g, Rectangle rect, boolean drawMarks) {
2 26 Feb 07 jari 60       super.subPaint(g, rect, drawMarks);
2 26 Feb 07 jari 61   
2 26 Feb 07 jari 62   if (isAntiAliasing) {
2 26 Feb 07 jari 63       g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
2 26 Feb 07 jari 64       g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
2 26 Feb 07 jari 65   }
2 26 Feb 07 jari 66   
2 26 Feb 07 jari 67   final int left = rect.x;
2 26 Feb 07 jari 68   final int top = rect.y;
2 26 Feb 07 jari 69   final int width  = rect.width;
2 26 Feb 07 jari 70   final int height = rect.height;
2 26 Feb 07 jari 71   
2 26 Feb 07 jari 72   if (width < 5 || height < 5) {
2 26 Feb 07 jari 73       return;
2 26 Feb 07 jari 74   }
2 26 Feb 07 jari 75   
2 26 Feb 07 jari 76   final int zeroValue = top + (int)Math.round(height/2f);
2 26 Feb 07 jari 77   final int numberOfSamples  = experiment.getNumberOfSamples();
2 26 Feb 07 jari 78   
2 26 Feb 07 jari 79                 //do this outside paint once menu is set up
2 26 Feb 07 jari 80         if(yRangeOption == CentroidViewer.USE_EXPERIMENT_MAX)
2 26 Feb 07 jari 81             maxYValue = maxExperimentValue;
2 26 Feb 07 jari 82         else if(this.yRangeOption == CentroidViewer.USE_CLUSTER_MAX)
2 26 Feb 07 jari 83             maxYValue = maxClusterValue;
2 26 Feb 07 jari 84         
2 26 Feb 07 jari 85         if (maxYValue == 0.0f) {
2 26 Feb 07 jari 86             maxYValue = 1.0f;
2 26 Feb 07 jari 87         }
2 26 Feb 07 jari 88         
2 26 Feb 07 jari 89   if (maxYValue == 0) {
2 26 Feb 07 jari 90       maxYValue = 1;
2 26 Feb 07 jari 91   }
2 26 Feb 07 jari 92   
2 26 Feb 07 jari 93   final float factor = height/(2f*maxYValue);
2 26 Feb 07 jari 94   final float stepX  = width/(float)(numberOfSamples-1);
2 26 Feb 07 jari 95   final int   stepsY = (int)maxYValue+1;
2 26 Feb 07 jari 96   
2 26 Feb 07 jari 97   if (this.drawVariances /*&& clusters[clusterIndex].length > 0*/) {
2 26 Feb 07 jari 98       // draw variances
2 26 Feb 07 jari 99       g.setColor(bColor);
2 26 Feb 07 jari 100       for (int i=0; i<numberOfSamples; i++) {
2 26 Feb 07 jari 101     
2 26 Feb 07 jari 102     if(Float.isNaN(this.means[this.clusterIndex][i]) || Float.isNaN(this.variances[this.clusterIndex][i]) || (this.variances[this.clusterIndex][i] < 0.0f)) {
2 26 Feb 07 jari 103         continue;
2 26 Feb 07 jari 104     }
2 26 Feb 07 jari 105     
2 26 Feb 07 jari 106     g.drawLine(left+(int)Math.round(i*stepX)  , zeroValue - (int)Math.round((this.means[this.clusterIndex][i]-this.variances[this.clusterIndex][i])*factor),
2 26 Feb 07 jari 107     left+(int)Math.round(i*stepX)  , zeroValue - (int)Math.round((this.means[this.clusterIndex][i]+this.variances[this.clusterIndex][i])*factor));
2 26 Feb 07 jari 108     g.drawLine(left+(int)Math.round(i*stepX)-3, zeroValue - (int)Math.round((this.means[this.clusterIndex][i]-this.variances[this.clusterIndex][i])*factor),
2 26 Feb 07 jari 109     left+(int)Math.round(i*stepX)+3, zeroValue - (int)Math.round((this.means[this.clusterIndex][i]-this.variances[this.clusterIndex][i])*factor));
2 26 Feb 07 jari 110     g.drawLine(left+(int)Math.round(i*stepX)-3, zeroValue - (int)Math.round((this.means[this.clusterIndex][i]+this.variances[this.clusterIndex][i])*factor),
2 26 Feb 07 jari 111     left+(int)Math.round(i*stepX)+3, zeroValue - (int)Math.round((this.means[this.clusterIndex][i]+this.variances[this.clusterIndex][i])*factor));
2 26 Feb 07 jari 112     
2 26 Feb 07 jari 113       }
2 26 Feb 07 jari 114       
2 26 Feb 07 jari 115   }
2 26 Feb 07 jari 116   
2 26 Feb 07 jari 117   //System.out.println("PTMSubCentroidViewer: After if(drawVariances)");
2 26 Feb 07 jari 118   
2 26 Feb 07 jari 119   if (this.drawValues /*&& clusters[clusterIndex].length > 0*/) {
2 26 Feb 07 jari 120       // draw values
2 26 Feb 07 jari 121       float fValue, sValue;
2 26 Feb 07 jari 122       Color color;
2 26 Feb 07 jari 123       for (int sample=0; sample<numberOfSamples-1; sample++) {
2 26 Feb 07 jari 124     for (int probe=0; probe<getCluster().length; probe++) {
2 26 Feb 07 jari 125         fValue = this.experiment.get(getProbe(probe), sample);
2 26 Feb 07 jari 126         sValue = this.experiment.get(getProbe(probe), sample+1);
2 26 Feb 07 jari 127         if (Float.isNaN(fValue) || Float.isNaN(sValue)) {
2 26 Feb 07 jari 128       continue;
2 26 Feb 07 jari 129         }
2 26 Feb 07 jari 130         color = this.data.getProbeColor(this.experiment.getGeneIndexMappedToData(getProbe(probe)));
2 26 Feb 07 jari 131         color = color == null ? DEF_CLUSTER_COLOR : color;
2 26 Feb 07 jari 132         g.setColor(color);
2 26 Feb 07 jari 133         g.drawLine(left+(int)Math.round(sample*stepX)    , zeroValue - (int)Math.round(fValue*factor),
2 26 Feb 07 jari 134         left+(int)Math.round((sample+1)*stepX), zeroValue - (int)Math.round(sValue*factor));
2 26 Feb 07 jari 135     }
2 26 Feb 07 jari 136       }
2 26 Feb 07 jari 137   }
2 26 Feb 07 jari 138   
2 26 Feb 07 jari 139   //System.out.println("PTMSubCentroidViewer: After if(drawValues)");
2 26 Feb 07 jari 140   
2 26 Feb 07 jari 141   if (this.drawCodes && this.codes != null && this.clusters[clusterIndex].length > 0) {
2 26 Feb 07 jari 142       g.setColor(Color.gray);
2 26 Feb 07 jari 143       for (int i=0; i<numberOfSamples-1; i++) {
2 26 Feb 07 jari 144     g.drawLine(left+(int)Math.round(i*stepX), zeroValue-(int)Math.round(this.codes[this.clusterIndex][i]*factor), left+(int)Math.round((i+1)*stepX), zeroValue-(int)Math.round(this.codes[this.clusterIndex][i+1]*factor));
2 26 Feb 07 jari 145       }
2 26 Feb 07 jari 146   }
2 26 Feb 07 jari 147   
2 26 Feb 07 jari 148   //System.out.println("PTMSubCentroidViewer: After if(drawCodes)");
2 26 Feb 07 jari 149   
2 26 Feb 07 jari 150   
2 26 Feb 07 jari 151   // draw zero line
2 26 Feb 07 jari 152   g.setColor(Color.black);
2 26 Feb 07 jari 153   g.drawLine(left, zeroValue, left+width, zeroValue);
2 26 Feb 07 jari 154   
2 26 Feb 07 jari 155   //System.out.println("PTMSubCentroidViewer: After draw zero line");
2 26 Feb 07 jari 156   // draw magenta line
2 26 Feb 07 jari 157   if (getCluster() != null && getCluster().length > 0 /*&& clusters[clusterIndex].length > 0*/) {
2 26 Feb 07 jari 158       g.setColor(Color.magenta);
2 26 Feb 07 jari 159       for (int i=0; i<numberOfSamples-1; i++) {
2 26 Feb 07 jari 160     
2 26 Feb 07 jari 161     if(Float.isNaN(this.means[this.clusterIndex][i]) || Float.isNaN(this.means[this.clusterIndex][i+1])) {
2 26 Feb 07 jari 162         continue;
2 26 Feb 07 jari 163     }
2 26 Feb 07 jari 164     
2 26 Feb 07 jari 165     g.drawLine(left+(int)Math.round(i*stepX), zeroValue-(int)Math.round(this.means[this.clusterIndex][i]*factor), left+(int)Math.round((i+1)*stepX), zeroValue-(int)Math.round(this.means[this.clusterIndex][i+1]*factor));
2 26 Feb 07 jari 166       }
2 26 Feb 07 jari 167   }
2 26 Feb 07 jari 168   
2 26 Feb 07 jari 169   //System.out.println("PTMSubCentroidViewer: After draw magenta line");
2 26 Feb 07 jari 170   
2 26 Feb 07 jari 171   
2 26 Feb 07 jari 172   //draw template
2 26 Feb 07 jari 173   float[] templateArray = new float[templateVector.size()];
2 26 Feb 07 jari 174   
2 26 Feb 07 jari 175   for (int i = 0; i < templateArray.length; i++) {
2 26 Feb 07 jari 176       templateArray[i] = ((Float)(templateVector.get(i))).floatValue();
2 26 Feb 07 jari 177   }
2 26 Feb 07 jari 178   
2 26 Feb 07 jari 179   for (int i = 0; i < templateArray.length; i++) {
2 26 Feb 07 jari 180       templateArray[i] = templateArray[i] - 0.5f;
2 26 Feb 07 jari 181   }
2 26 Feb 07 jari 182   
2 26 Feb 07 jari 183   
2 26 Feb 07 jari 184   
2 26 Feb 07 jari 185   
2 26 Feb 07 jari 186   for (int i = 0; i < numberOfSamples - 1; i++) {
2 26 Feb 07 jari 187       g.setColor(Color.red);
2 26 Feb 07 jari 188       if (!Float.isNaN(templateArray[i])) {
2 26 Feb 07 jari 189     g.fillOval(left+(int)Math.round(i*stepX) - 2, zeroValue-(int)Math.round(templateArray[i]*factor) - 2, 5, 5);
2 26 Feb 07 jari 190       }
2 26 Feb 07 jari 191       if (!Float.isNaN(templateArray[i+1])) {
2 26 Feb 07 jari 192     g.fillOval(left+(int)Math.round((i+1)*stepX) - 2, zeroValue-(int)Math.round(templateArray[i+1]*factor) - 2, 5, 5);
2 26 Feb 07 jari 193       }
2 26 Feb 07 jari 194       if (Float.isNaN(templateArray[i]) || Float.isNaN(templateArray[i+1])) {
2 26 Feb 07 jari 195     continue;
2 26 Feb 07 jari 196       }
2 26 Feb 07 jari 197       g.setColor(Color.blue);
2 26 Feb 07 jari 198       g.drawLine(left+(int)Math.round(i*stepX), zeroValue-(int)Math.round(templateArray[i]*factor), left+(int)Math.round((i+1)*stepX), zeroValue-(int)Math.round(templateArray[i+1]*factor));
2 26 Feb 07 jari 199   }
2 26 Feb 07 jari 200   
2 26 Feb 07 jari 201   //System.out.println("PTMSubCentroidViewer: After draw template");
2 26 Feb 07 jari 202   
2 26 Feb 07 jari 203   // draw rectangle
2 26 Feb 07 jari 204   g.setColor(Color.black);
2 26 Feb 07 jari 205   g.drawRect(left, top, width, height);
2 26 Feb 07 jari 206   
2 26 Feb 07 jari 207   //System.out.println("PTMSubCentroidViewer: After draw rectangle");
2 26 Feb 07 jari 208   // draw X items
2 26 Feb 07 jari 209   for (int i=1; i<numberOfSamples-1; i++) {
2 26 Feb 07 jari 210       g.drawLine(left+(int)Math.round(i*stepX), top+height-5, left+(int)Math.round(i*stepX), top+height);
2 26 Feb 07 jari 211   }
2 26 Feb 07 jari 212   
2 26 Feb 07 jari 213   //System.out.println("PTMSubCentroidViewer: After draw X Items");
2 26 Feb 07 jari 214   
2 26 Feb 07 jari 215   //draw Y items
2 26 Feb 07 jari 216   for (int i=1; i<stepsY; i++) {
2 26 Feb 07 jari 217       g.drawLine(left, zeroValue-(int)Math.round(i*factor), left+5, zeroValue-(int)Math.round(i*factor));
2 26 Feb 07 jari 218       g.drawLine(left, zeroValue+(int)Math.round(i*factor), left+5, zeroValue+(int)Math.round(i*factor));
2 26 Feb 07 jari 219   }
2 26 Feb 07 jari 220   
2 26 Feb 07 jari 221   //System.out.println("PTMSubCentroidViewer: After draw Y Items");
2 26 Feb 07 jari 222   
2 26 Feb 07 jari 223   // draw genes info
2 26 Feb 07 jari 224   g.setColor(bColor);
2 26 Feb 07 jari 225   if (drawMarks) {
2 26 Feb 07 jari 226       FontMetrics metrics = g.getFontMetrics();
2 26 Feb 07 jari 227       String str;
2 26 Feb 07 jari 228       int strWidth;
2 26 Feb 07 jari 229       //draw Y digits
2 26 Feb 07 jari 230       for (int i=1; i<stepsY; i++) {
2 26 Feb 07 jari 231     str = String.valueOf(i);
2 26 Feb 07 jari 232     strWidth = metrics.stringWidth(str);
2 26 Feb 07 jari 233     g.drawString(str, left-10-strWidth, zeroValue+5-(int)Math.round(i*factor));
2 26 Feb 07 jari 234     str = String.valueOf(-i);
2 26 Feb 07 jari 235     strWidth = metrics.stringWidth(str);
2 26 Feb 07 jari 236     g.drawString(str, left-10-strWidth, zeroValue+5+(int)Math.round(i*factor));
2 26 Feb 07 jari 237       }
2 26 Feb 07 jari 238       
2 26 Feb 07 jari 239       //System.out.println("PTMSubCentroidViewer: After draw genes info");
2 26 Feb 07 jari 240       
2 26 Feb 07 jari 241       // draw X samples names
2 26 Feb 07 jari 242       g.rotate(-Math.PI/2.0);
2 26 Feb 07 jari 243       final int max_name_width = getNamesWidth(metrics);
2 26 Feb 07 jari 244       for (int i=0; i<numberOfSamples; i++) {
2 26 Feb 07 jari 245     g.drawString(data.getSampleName(experiment.getSampleIndex(i)), -height-top-10-max_name_width, left+(int)Math.round(i*stepX)+3);
2 26 Feb 07 jari 246       }
2 26 Feb 07 jari 247       g.rotate(Math.PI/2.0);
2 26 Feb 07 jari 248   }
2 26 Feb 07 jari 249   
2 26 Feb 07 jari 250   //System.out.println("PTMSubCentroidViewer: After if(drawMarks)");
2 26 Feb 07 jari 251   
2 26 Feb 07 jari 252   if (getCluster() != null && getCluster().length > 0 && this.drawVariances /*&& clusters[clusterIndex].length > 0*/) {
2 26 Feb 07 jari 253       // draw points
2 26 Feb 07 jari 254       g.setColor(bColor);
2 26 Feb 07 jari 255       for (int i=0; i<numberOfSamples; i++) {
2 26 Feb 07 jari 256     
2 26 Feb 07 jari 257     if (Float.isNaN(this.means[this.clusterIndex][i])) {
2 26 Feb 07 jari 258         continue;
2 26 Feb 07 jari 259     }
2 26 Feb 07 jari 260     
2 26 Feb 07 jari 261     g.fillOval(left+(int)Math.round(i*stepX)-3, zeroValue-(int)Math.round(this.means[this.clusterIndex][i]*factor)-3, 6, 6);
2 26 Feb 07 jari 262       }
2 26 Feb 07 jari 263   }
2 26 Feb 07 jari 264   
2 26 Feb 07 jari 265   //System.out.println("PTMSubCentroidViewer: After draw points");
2 26 Feb 07 jari 266   
2 26 Feb 07 jari 267   g.setColor(bColor);
2 26 Feb 07 jari 268   if (getCluster() == null || getCluster().length == 0) {
2 26 Feb 07 jari 269       g.drawString("No Genes", left+10, top+20);
2 26 Feb 07 jari 270   } else {
2 26 Feb 07 jari 271       g.drawString(getCluster().length+" Genes", left+10, top+20);
2 26 Feb 07 jari 272   }
2 26 Feb 07 jari 273     }
2 26 Feb 07 jari 274     
2 26 Feb 07 jari 275     
2 26 Feb 07 jari 276 }