2 |
26 Feb 07 |
jari |
1 |
/* |
2 |
26 Feb 07 |
jari |
Copyright @ 1999-2005, 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: SequenceLogo.java,v $ |
2 |
26 Feb 07 |
jari |
* $Revision: 1.4 $ |
2 |
26 Feb 07 |
jari |
* $Date: 2006/02/23 20:59:57 $ |
2 |
26 Feb 07 |
jari |
* $Author: caliente $ |
2 |
26 Feb 07 |
jari |
* $State: Exp $ |
2 |
26 Feb 07 |
jari |
11 |
*/ |
2 |
26 Feb 07 |
jari |
12 |
package org.tigr.microarray.mev.motif; |
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.Graphics2D; |
2 |
26 Feb 07 |
jari |
17 |
import java.awt.font.FontRenderContext; |
2 |
26 Feb 07 |
jari |
18 |
import java.awt.font.TextLayout; |
2 |
26 Feb 07 |
jari |
19 |
import java.awt.geom.AffineTransform; |
2 |
26 Feb 07 |
jari |
20 |
import java.awt.geom.Rectangle2D; |
2 |
26 Feb 07 |
jari |
21 |
|
2 |
26 Feb 07 |
jari |
22 |
public class SequenceLogo { |
2 |
26 Feb 07 |
jari |
23 |
int[][] Matrix; |
2 |
26 Feb 07 |
jari |
24 |
public int NumberOfBases; |
2 |
26 Feb 07 |
jari |
25 |
|
2 |
26 Feb 07 |
jari |
26 |
public SequenceLogo(int[][] matrix) { |
2 |
26 Feb 07 |
jari |
27 |
Matrix=matrix; |
2 |
26 Feb 07 |
jari |
28 |
NumberOfBases=Matrix[0].length; |
2 |
26 Feb 07 |
jari |
29 |
} |
2 |
26 Feb 07 |
jari |
30 |
|
2 |
26 Feb 07 |
jari |
31 |
public void Paint(Graphics2D g2, int x, int y, int Width, int Height, int Start, boolean DrawYAxis) { |
2 |
26 Feb 07 |
jari |
32 |
double ScaleX=1; |
2 |
26 Feb 07 |
jari |
33 |
double ScaleY=1; |
2 |
26 Feb 07 |
jari |
34 |
double BaseY; |
2 |
26 Feb 07 |
jari |
35 |
double H; |
2 |
26 Feb 07 |
jari |
36 |
double R; |
2 |
26 Feb 07 |
jari |
37 |
double Sum; |
2 |
26 Feb 07 |
jari |
38 |
double BaseHeightFactor=4; |
2 |
26 Feb 07 |
jari |
39 |
double BaseWidth=0; |
2 |
26 Feb 07 |
jari |
40 |
double BaseHeight=0; |
2 |
26 Feb 07 |
jari |
41 |
int Minimum; |
2 |
26 Feb 07 |
jari |
42 |
int MaxIndex=0; |
2 |
26 Feb 07 |
jari |
43 |
boolean ElementFound=false; |
2 |
26 Feb 07 |
jari |
44 |
float[] PreviousBase=new float[NumberOfBases]; |
2 |
26 Feb 07 |
jari |
45 |
String Base=""; |
2 |
26 Feb 07 |
jari |
46 |
String Bases="ACGT"; |
2 |
26 Feb 07 |
jari |
47 |
Rectangle2D Bounds; |
2 |
26 Feb 07 |
jari |
48 |
TextLayout Layout; |
2 |
26 Feb 07 |
jari |
49 |
Font LogoFont = new Font("serif",Font.BOLD, 400); |
2 |
26 Feb 07 |
jari |
50 |
g2.setFont(LogoFont); |
2 |
26 Feb 07 |
jari |
51 |
AffineTransform OldTransform=g2.getTransform(); |
2 |
26 Feb 07 |
jari |
52 |
FontRenderContext frc = g2.getFontRenderContext(); |
2 |
26 Feb 07 |
jari |
53 |
for (int k=0; k<4; k++) { |
2 |
26 Feb 07 |
jari |
54 |
for (int i=0; i<NumberOfBases; i++) { |
2 |
26 Feb 07 |
jari |
55 |
g2.setTransform(OldTransform); |
2 |
26 Feb 07 |
jari |
56 |
Minimum=Integer.MAX_VALUE; |
2 |
26 Feb 07 |
jari |
57 |
H=0; |
2 |
26 Feb 07 |
jari |
58 |
Sum=0; |
2 |
26 Feb 07 |
jari |
59 |
for (int j=0; j<4; j++) Sum+=Math.abs(Matrix[j][i]); |
2 |
26 Feb 07 |
jari |
60 |
ElementFound=false; |
2 |
26 Feb 07 |
jari |
61 |
for (int j=0; j<4; j++) { |
2 |
26 Feb 07 |
jari |
62 |
|
2 |
26 Feb 07 |
jari |
63 |
if ((Matrix[j][i]>0) && (Matrix[j][i]<=Minimum)) { |
2 |
26 Feb 07 |
jari |
64 |
Minimum=Matrix[j][i]; |
2 |
26 Feb 07 |
jari |
65 |
Base=Bases.substring(j,j+1); |
2 |
26 Feb 07 |
jari |
66 |
MaxIndex=j; |
2 |
26 Feb 07 |
jari |
67 |
BaseHeightFactor=Minimum*4; |
2 |
26 Feb 07 |
jari |
68 |
ElementFound=true; |
2 |
26 Feb 07 |
jari |
69 |
} |
2 |
26 Feb 07 |
jari |
70 |
if (Matrix[j][i]!=0) H+=((float)Math.abs(Matrix[j][i]/(double)Sum))*Math.log((float)Math.abs(Matrix[j][i]/(double)Sum))/Math.log(2); |
2 |
26 Feb 07 |
jari |
71 |
} |
2 |
26 Feb 07 |
jari |
72 |
H=-H; |
2 |
26 Feb 07 |
jari |
73 |
R=2-H; |
2 |
26 Feb 07 |
jari |
74 |
double Test=0.5; |
2 |
26 Feb 07 |
jari |
75 |
BaseHeightFactor=(float)Math.abs(Matrix[MaxIndex][i]/(double)Sum)*R; |
2 |
26 Feb 07 |
jari |
76 |
BaseHeightFactor*=0.5; // Maximum is 2 bit NOT 1 !! |
2 |
26 Feb 07 |
jari |
77 |
if (Matrix[MaxIndex][i]>0) Matrix[MaxIndex][i]=-Matrix[MaxIndex][i]; |
2 |
26 Feb 07 |
jari |
78 |
if (!ElementFound) continue; |
2 |
26 Feb 07 |
jari |
79 |
if (Base.compareTo("A")==0) g2.setColor(Color.green); |
2 |
26 Feb 07 |
jari |
80 |
if (Base.compareTo("C")==0) g2.setColor(Color.blue); |
2 |
26 Feb 07 |
jari |
81 |
if (Base.compareTo("G")==0) g2.setColor(Color.orange); |
2 |
26 Feb 07 |
jari |
82 |
if (Base.compareTo("T")==0) g2.setColor(Color.red); |
2 |
26 Feb 07 |
jari |
83 |
Layout = new TextLayout(Base,LogoFont, g2.getFontRenderContext()); |
2 |
26 Feb 07 |
jari |
84 |
Bounds = Layout.getBounds(); |
2 |
26 Feb 07 |
jari |
85 |
BaseY=y+Height-((int)Bounds.getHeight()+(int)Bounds.getY())*BaseHeightFactor; |
2 |
26 Feb 07 |
jari |
86 |
BaseHeight=(int)Bounds.getHeight(); |
2 |
26 Feb 07 |
jari |
87 |
BaseWidth=(int)Bounds.getWidth(); |
2 |
26 Feb 07 |
jari |
88 |
ScaleX=(Width)/((BaseWidth)*NumberOfBases); |
2 |
26 Feb 07 |
jari |
89 |
ScaleY=(Height*BaseHeightFactor)/BaseHeight; |
2 |
26 Feb 07 |
jari |
90 |
g2.scale(ScaleX,ScaleY); |
2 |
26 Feb 07 |
jari |
91 |
Layout.draw(g2,(float) (((x+1)/ScaleX-Bounds.getX()+i*BaseWidth)),(float) (BaseY/ScaleY-PreviousBase[i]/ScaleY)); |
2 |
26 Feb 07 |
jari |
92 |
PreviousBase[i]+=(float)((BaseHeight)*ScaleY); |
2 |
26 Feb 07 |
jari |
93 |
} |
2 |
26 Feb 07 |
jari |
94 |
} |
2 |
26 Feb 07 |
jari |
95 |
for (int i=0; i<NumberOfBases; i++) { |
2 |
26 Feb 07 |
jari |
96 |
for (int j=0; j<4; j++) { |
2 |
26 Feb 07 |
jari |
97 |
Matrix[j][i]=Math.abs(Matrix[j][i]); |
2 |
26 Feb 07 |
jari |
98 |
} |
2 |
26 Feb 07 |
jari |
99 |
} |
2 |
26 Feb 07 |
jari |
100 |
g2.setTransform(OldTransform); |
2 |
26 Feb 07 |
jari |
101 |
BaseHeightFactor=0.9; |
2 |
26 Feb 07 |
jari |
102 |
Layout = new TextLayout(String.valueOf(Start),LogoFont, g2.getFontRenderContext()); |
2 |
26 Feb 07 |
jari |
103 |
Bounds = Layout.getBounds(); |
2 |
26 Feb 07 |
jari |
104 |
BaseWidth=(int)Bounds.getHeight(); |
2 |
26 Feb 07 |
jari |
105 |
ScaleX=(Width)/((BaseWidth)*NumberOfBases); |
2 |
26 Feb 07 |
jari |
106 |
ScaleY=ScaleX; |
2 |
26 Feb 07 |
jari |
107 |
ScaleX*=BaseHeightFactor; |
2 |
26 Feb 07 |
jari |
108 |
g2.scale(ScaleX,ScaleY); |
2 |
26 Feb 07 |
jari |
109 |
g2.setColor(Color.black); |
2 |
26 Feb 07 |
jari |
110 |
g2.rotate(-Math.PI/2.0); |
2 |
26 Feb 07 |
jari |
111 |
for (int i=0; i<NumberOfBases; i++) { |
2 |
26 Feb 07 |
jari |
112 |
Layout = new TextLayout(String.valueOf(i+Start),LogoFont, g2.getFontRenderContext()); |
2 |
26 Feb 07 |
jari |
113 |
Bounds = Layout.getBounds(); |
2 |
26 Feb 07 |
jari |
114 |
BaseHeight=(int)Bounds.getWidth()+BaseWidth*0.5+Bounds.getX(); |
2 |
26 Feb 07 |
jari |
115 |
Layout.draw(g2,(float)((-y-Height)/ScaleY-BaseHeight),(float)((x/ScaleX+(i+1)/BaseHeightFactor*BaseWidth))); |
2 |
26 Feb 07 |
jari |
116 |
} |
2 |
26 Feb 07 |
jari |
117 |
if (DrawYAxis) { |
2 |
26 Feb 07 |
jari |
118 |
Layout = new TextLayout("bits",LogoFont, g2.getFontRenderContext()); |
2 |
26 Feb 07 |
jari |
119 |
Bounds = Layout.getBounds(); |
2 |
26 Feb 07 |
jari |
120 |
BaseHeight=(int)Bounds.getWidth()+BaseWidth*0.5+Bounds.getX(); |
2 |
26 Feb 07 |
jari |
121 |
Layout.draw(g2,(float)((-y-Height/2.0)/ScaleY-BaseHeight/2.0),(float)(x/ScaleX-BaseWidth*0.25)); |
2 |
26 Feb 07 |
jari |
122 |
g2.rotate(Math.PI/2.0); |
2 |
26 Feb 07 |
jari |
123 |
Layout = new TextLayout("0",LogoFont, g2.getFontRenderContext()); |
2 |
26 Feb 07 |
jari |
124 |
Bounds = Layout.getBounds(); |
2 |
26 Feb 07 |
jari |
125 |
BaseHeight=(int)Bounds.getHeight(); |
2 |
26 Feb 07 |
jari |
126 |
Layout.draw(g2,(float)((x/ScaleX)-BaseWidth),(float)((y+Height)/ScaleY)); |
2 |
26 Feb 07 |
jari |
127 |
Layout = new TextLayout("2",LogoFont, g2.getFontRenderContext()); |
2 |
26 Feb 07 |
jari |
128 |
Layout.draw(g2,(float)((x/ScaleX)-BaseWidth),(float)((y)/ScaleY+BaseHeight)); |
2 |
26 Feb 07 |
jari |
129 |
} |
2 |
26 Feb 07 |
jari |
130 |
g2.setTransform(OldTransform); |
2 |
26 Feb 07 |
jari |
131 |
g2.setColor(new Color(0,0,128)); |
2 |
26 Feb 07 |
jari |
132 |
g2.drawRect(x-1,y,Width+2,Height); |
2 |
26 Feb 07 |
jari |
133 |
} |
2 |
26 Feb 07 |
jari |
134 |
} |