| 1 |
|
| 2 |
//============================================================================ |
| 3 |
// |
| 4 |
// file: AlignmentPanel.java |
| 5 |
// |
| 6 |
// Copyright (c) 2007, Michael E. Smoot |
| 7 |
// |
| 8 |
// This program is free software; you can redistribute it and/or modify it |
| 9 |
// under the terms of the GNU General Public License as published by the |
| 10 |
// Free Software Foundation; either version 2 of the License, or (at your |
| 11 |
// option) any later version. |
| 12 |
// |
| 13 |
// This program is distributed in the hope that it will be useful, but |
| 14 |
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 |
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 16 |
// for more details. |
| 17 |
// |
| 18 |
// You should have received a copy of the GNU General Public License along |
| 19 |
// with this program; if not, write to the Free Software Foundation, Inc., |
| 20 |
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 |
// |
| 22 |
//============================================================================ |
| 23 |
|
| 24 |
package amap; |
| 25 |
|
| 26 |
import javax.swing.*; |
| 27 |
import java.util.*; |
| 28 |
import java.awt.event.*; |
| 29 |
import java.awt.Dimension; |
| 30 |
import java.awt.Color; |
| 31 |
import java.awt.Font; |
| 32 |
import java.awt.FontMetrics; |
| 33 |
import java.awt.Graphics; |
| 34 |
import java.awt.Graphics2D; |
| 35 |
import java.awt.RenderingHints; |
| 36 |
import java.awt.Shape; |
| 37 |
import java.awt.geom.Point2D; |
| 38 |
import java.awt.font.GlyphVector; |
| 39 |
import java.awt.font.FontRenderContext; |
| 40 |
|
| 41 |
public class AlignmentPanel extends JPanel implements ComponentListener { |
| 42 |
|
| 43 |
List<Alignment> aligns; |
| 44 |
List<String> orderedKeys; |
| 45 |
int index; |
| 46 |
int xOff; |
| 47 |
int yOff; |
| 48 |
int numCharsAcross; |
| 49 |
int nameWidth; |
| 50 |
int glyphWidth; |
| 51 |
int len; |
| 52 |
int groupOff; |
| 53 |
Font defaultFont; |
| 54 |
int height; |
| 55 |
int width; |
| 56 |
int os; |
| 57 |
Map<String,Color> backColMap; |
| 58 |
Map<String,Color> textColMap; |
| 59 |
public AlignmentPanel(List<Alignment> aligns, int initWidth) { |
| 60 |
super(); |
| 61 |
this.aligns = aligns; |
| 62 |
this.width = initWidth; |
| 63 |
Map<String,String> seqs = aligns.get(0).getSequences(); |
| 64 |
orderedKeys = aligns.get(0).getOrderedKeys(); |
| 65 |
defaultFont = new Font("Courier",Font.BOLD,12); |
| 66 |
index = 0; |
| 67 |
|
| 68 |
// Aaaaargh |
| 69 |
if ( System.getProperty("os.name").matches(".*[Mm][Aa][Cc].*") ) |
| 70 |
os = -1; |
| 71 |
else |
| 72 |
os = 1; |
| 73 |
|
| 74 |
backColMap = new HashMap<String,Color>(); |
| 75 |
backColMap.put("0",new Color(255,255,255)); |
| 76 |
backColMap.put("1",new Color(0,128,255)); // CMYK 1, 0.5, 0, 0 |
| 77 |
backColMap.put("2",new Color(64,25,255)); // CMYK 0.75, 0.9, 0, 0 |
| 78 |
backColMap.put("3",new Color(255,0,255)); // CMYK 0, 1, 0, 0 |
| 79 |
|
| 80 |
textColMap = new HashMap<String,Color>(); |
| 81 |
textColMap.put("0",Color.black); |
| 82 |
textColMap.put("1",Color.white); |
| 83 |
textColMap.put("2",new Color(255,230,41)); // CMYK 0, 0.1, 0.84, 0 |
| 84 |
textColMap.put("3",Color.black); |
| 85 |
|
| 86 |
sizeScreen( new Dimension(initWidth,0) ); |
| 87 |
} |
| 88 |
|
| 89 |
private void sizeScreen(Dimension d) { |
| 90 |
width = (int)(d.getWidth()); |
| 91 |
|
| 92 |
glyphWidth = 10; |
| 93 |
|
| 94 |
// find the width of the sequence name |
| 95 |
FontMetrics fm = getFontMetrics(defaultFont); |
| 96 |
nameWidth = 0; |
| 97 |
for ( String key : orderedKeys ) |
| 98 |
nameWidth = Math.max(nameWidth,fm.stringWidth(key)); |
| 99 |
|
| 100 |
// give the seq name some breathing room |
| 101 |
nameWidth += glyphWidth; |
| 102 |
|
| 103 |
// set margins |
| 104 |
xOff = 20; |
| 105 |
yOff = 20; |
| 106 |
|
| 107 |
// size of one multiple alignment row |
| 108 |
groupOff = yOff * (orderedKeys.size() + 1); |
| 109 |
|
| 110 |
// figure out how many multiple alignment rows there will be |
| 111 |
int seqAllowedWidth = width - xOff - nameWidth - xOff - xOff; |
| 112 |
numCharsAcross = seqAllowedWidth/glyphWidth; |
| 113 |
len = aligns.get(0).getSequences().get(orderedKeys.get(0)).length(); |
| 114 |
|
| 115 |
height = ((len/numCharsAcross)+1) * groupOff; |
| 116 |
height = Math.max(height, (int)d.getHeight()); |
| 117 |
|
| 118 |
setPreferredSize( new Dimension(width,height) ); |
| 119 |
} |
| 120 |
|
| 121 |
public void setIndex(int i) { |
| 122 |
if ( i >= 0 && i < aligns.size() ) |
| 123 |
index = i; |
| 124 |
else |
| 125 |
System.out.println("invalid index: " + i); |
| 126 |
} |
| 127 |
|
| 128 |
public void paint(Graphics g) { |
| 129 |
Graphics2D g2 = (Graphics2D)g; |
| 130 |
g2.setBackground(Color.white); |
| 131 |
g2.setPaint(Color.white); |
| 132 |
g2.fillRect(0,0,width,height); |
| 133 |
|
| 134 |
g2.setPaint(Color.black); |
| 135 |
g2.setFont( defaultFont ); |
| 136 |
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| 137 |
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
| 138 |
FontRenderContext frc = g2.getFontRenderContext(); |
| 139 |
Map<String,String> seqs = aligns.get(index).getSequences(); |
| 140 |
Map<String,String> cols = aligns.get(index).getColors(); |
| 141 |
int i = 0; |
| 142 |
for ( String name : orderedKeys ) { |
| 143 |
i++; |
| 144 |
|
| 145 |
String seq = seqs.get(name); |
| 146 |
String colString = cols.get(name); |
| 147 |
if ( seq == null ) |
| 148 |
continue; |
| 149 |
|
| 150 |
GlyphVector gv = defaultFont.createGlyphVector(frc,seq); |
| 151 |
|
| 152 |
for ( int j = 0; j < gv.getNumGlyphs(); j++ ) { |
| 153 |
|
| 154 |
int xLoc = xOff+nameWidth+((j%numCharsAcross)*glyphWidth); |
| 155 |
int yLoc = ((j/numCharsAcross)*groupOff)+(yOff*i); |
| 156 |
|
| 157 |
// set glyph position |
| 158 |
Point2D pos = gv.getGlyphPosition(j); |
| 159 |
pos.setLocation(xLoc, yLoc); |
| 160 |
gv.setGlyphPosition(j,pos); |
| 161 |
|
| 162 |
// draw the background rect |
| 163 |
String colCode = colString.substring(j,j+1); |
| 164 |
g2.setPaint(backColMap.get( colCode )); |
| 165 |
g2.fillRect(xLoc-1,yLoc-yOff+6,glyphWidth,yOff); |
| 166 |
|
| 167 |
// draw the sequence name |
| 168 |
if ( j%numCharsAcross == 0 ) { |
| 169 |
g2.setPaint(Color.black); |
| 170 |
g2.drawString(name,xOff,yLoc ); |
| 171 |
} |
| 172 |
|
| 173 |
// draw the glyph |
| 174 |
Shape glyph = gv.getGlyphOutline(j); |
| 175 |
g2.setPaint(textColMap.get( colCode )); |
| 176 |
g2.fill(glyph); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
public void componentResized(ComponentEvent e) { |
| 182 |
sizeScreen( e.getComponent().getSize() ); |
| 183 |
} |
| 184 |
|
| 185 |
public void componentShown(ComponentEvent e) {}; |
| 186 |
public void componentHidden(ComponentEvent e) {}; |
| 187 |
public void componentMoved(ComponentEvent e) {}; |
| 188 |
} |