2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.renderer;
23 import jalview.api.AlignmentColsCollectionI;
24 import jalview.api.AlignmentRowsCollectionI;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.Annotation;
28 import jalview.datamodel.SequenceGroup;
29 import jalview.datamodel.SequenceI;
30 import jalview.renderer.seqfeatures.FeatureColourFinder;
31 import jalview.renderer.seqfeatures.FeatureRenderer;
32 import jalview.viewmodel.OverviewDimensions;
34 import java.awt.AlphaComposite;
35 import java.awt.Color;
36 import java.awt.Graphics;
37 import java.awt.Graphics2D;
38 import java.awt.image.BufferedImage;
40 public class OverviewRenderer
42 // transparency of hidden cols/seqs overlay
43 private final float TRANSPARENCY = 0.5f;
45 private FeatureColourFinder finder;
48 private BufferedImage miniMe;
50 // raw number of pixels to allocate to each column
51 private float pixelsPerCol;
53 // raw number of pixels to allocate to each row
54 private float pixelsPerSeq;
56 // flag to indicate whether to halt drawing
57 private volatile boolean redraw = false;
59 // reference to alignment, needed to get sequence groups
60 private AlignmentI al;
62 private ResidueShaderI shader;
64 private ResidueColourFinder resColFinder;
66 public OverviewRenderer(FeatureRenderer fr, OverviewDimensions od,
68 ResidueShaderI resshader)
70 finder = new FeatureColourFinder(fr);
71 resColFinder = new OverviewResColourFinder();
76 pixelsPerCol = od.getPixelsPerCol();
77 pixelsPerSeq = od.getPixelsPerSeq();
78 miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
79 BufferedImage.TYPE_INT_RGB);
83 * Draw alignment rows and columns onto an image
86 * Iterator over rows to be drawn
88 * Iterator over columns to be drawn
89 * @return image containing the drawing
91 public BufferedImage draw(AlignmentRowsCollectionI rows,
92 AlignmentColsCollectionI cols)
94 int rgbcolor = Color.white.getRGB();
98 for (int alignmentRow : rows)
105 // get details of this alignment row
106 SequenceI seq = rows.getSequence(alignmentRow);
108 // rate limiting step when rendering overview for lots of groups
109 SequenceGroup[] allGroups = al.findAllGroups(seq);
111 // calculate where this row extends to in pixels
112 int endRow = Math.min(Math.round((seqIndex + 1) * pixelsPerSeq) - 1,
113 miniMe.getHeight() - 1);
117 for (int alignmentCol : cols)
124 // calculate where this column extends to in pixels
125 int endCol = Math.min(Math.round((colIndex + 1) * pixelsPerCol) - 1,
126 miniMe.getWidth() - 1);
128 // don't do expensive colour determination if we're not going to use it
129 // NB this is important to avoid performance issues in the overview
131 if (pixelCol <= endCol)
133 rgbcolor = getColumnColourFromSequence(allGroups, seq,
134 alignmentCol, finder);
136 // fill in the appropriate number of pixels
137 for (int row = pixelRow; row <= endRow; ++row)
139 for (int col = pixelCol; col <= endCol; ++col)
141 miniMe.setRGB(col, row, rgbcolor);
145 pixelCol = endCol + 1;
149 pixelRow = endRow + 1;
153 overlayHiddenRegions(rows, cols);
158 * Find the colour of a sequence at a specified column position
161 * sequence to get colour for
163 * column position to get colour for
165 * FeatureColourFinder to use
166 * @return colour of sequence at this position, as RGB
168 private int getColumnColourFromSequence(SequenceGroup[] allGroups,
169 jalview.datamodel.SequenceI seq,
170 int lastcol, FeatureColourFinder fcfinder)
172 Color color = Color.white;
174 if ((seq != null) && (seq.getLength() > lastcol))
176 color = resColFinder.getResidueColour(true, shader, allGroups, seq,
181 return color.getRGB();
185 * Overlay the hidden regions on the overview image
188 * collection of rows the overview is built over
190 * collection of columns the overview is built over
192 private void overlayHiddenRegions(AlignmentRowsCollectionI rows,
193 AlignmentColsCollectionI cols)
195 if (cols.hasHidden() || rows.hasHidden())
197 BufferedImage mask = buildHiddenImage(rows, cols, miniMe.getWidth(),
200 Graphics2D g = (Graphics2D) miniMe.getGraphics();
201 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
203 g.drawImage(mask, 0, 0, miniMe.getWidth(), miniMe.getHeight(), null);
208 * Build a masking image of hidden columns and rows to be applied on top of
209 * the main overview image.
212 * collection of rows the overview is built over
214 * collection of columns the overview is built over
216 * width of overview in pixels
218 * height of overview in pixels
219 * @return BufferedImage containing mask of hidden regions
221 private BufferedImage buildHiddenImage(AlignmentRowsCollectionI rows,
222 AlignmentColsCollectionI cols, int width, int height)
225 BufferedImage hiddenImage = new BufferedImage(width, height,
226 BufferedImage.TYPE_INT_ARGB);
231 Color hidden = Color.DARK_GRAY.darker();
233 Graphics2D g2d = (Graphics2D) hiddenImage.getGraphics();
235 // set background to transparent
236 g2d.setComposite(AlphaComposite.Clear);
237 g2d.fillRect(0, 0, width, height);
239 // set next colour to opaque
240 g2d.setComposite(AlphaComposite.Src);
242 for (int alignmentCol : cols)
249 // calculate where this column extends to in pixels
250 int endCol = Math.min(Math.round((colIndex + 1) * pixelsPerCol) - 1,
251 hiddenImage.getWidth() - 1);
253 if (pixelCol <= endCol)
255 // determine the colour based on the sequence and column position
256 if (cols.isHidden(alignmentCol))
258 g2d.setColor(hidden);
259 g2d.fillRect(pixelCol, 0, endCol - pixelCol + 1, height);
262 pixelCol = endCol + 1;
270 for (int alignmentRow : rows)
277 // calculate where this row extends to in pixels
278 int endRow = Math.min(Math.round((seqIndex + 1) * pixelsPerSeq) - 1,
279 miniMe.getHeight() - 1);
281 // get details of this alignment row
282 if (rows.isHidden(alignmentRow))
284 g2d.setColor(hidden);
285 g2d.fillRect(0, pixelRow, width, endRow - pixelRow + 1);
287 pixelRow = endRow + 1;
295 * Draw the alignment annotation in the overview panel
298 * the graphics object to draw on
300 * alignment annotation information
302 * alignment character width value
304 * y-position for the annotation graph
306 * the collection of columns used in the overview panel
308 public void drawGraph(Graphics g, AlignmentAnnotation anno, int charWidth,
309 int y, AlignmentColsCollectionI cols)
311 Annotation[] annotations = anno.annotations;
312 g.setColor(Color.white);
313 g.fillRect(0, 0, miniMe.getWidth(), y);
318 for (int alignmentCol : cols)
324 if (alignmentCol >= annotations.length)
326 break; // no more annotations to draw here
330 int endCol = Math.min(Math.round((colIndex + 1) * pixelsPerCol) - 1,
331 miniMe.getWidth() - 1);
333 if (annotations[alignmentCol] != null)
335 if (annotations[alignmentCol].colour == null)
337 g.setColor(Color.black);
341 g.setColor(annotations[alignmentCol].colour);
344 height = (int) ((annotations[alignmentCol].value / anno.graphMax)
351 g.fillRect(pixelCol, y - height, endCol - pixelCol + 1, height);
353 pixelCol = endCol + 1;
360 * Allows redraw flag to be set
363 * value to set redraw to: true = redraw is occurring, false = no
366 public void setRedraw(boolean b)