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 final Color HIDDEN_COLOUR = Color.DARK_GRAY.darker();
47 private FeatureColourFinder finder;
50 private BufferedImage miniMe;
52 // raw number of pixels to allocate to each column
53 private float pixelsPerCol;
55 // raw number of pixels to allocate to each row
56 private float pixelsPerSeq;
58 // flag to indicate whether to halt drawing
59 private volatile boolean redraw = false;
61 // reference to alignment, needed to get sequence groups
62 private AlignmentI al;
64 private ResidueShaderI shader;
66 private OverviewResColourFinder resColFinder;
68 public OverviewRenderer(FeatureRenderer fr, OverviewDimensions od,
70 ResidueShaderI resshader, OverviewResColourFinder colFinder)
72 finder = new FeatureColourFinder(fr);
73 resColFinder = colFinder;
78 pixelsPerCol = od.getPixelsPerCol();
79 pixelsPerSeq = od.getPixelsPerSeq();
80 miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
81 BufferedImage.TYPE_INT_RGB);
85 * Draw alignment rows and columns onto an image
88 * Iterator over rows to be drawn
90 * Iterator over columns to be drawn
91 * @return image containing the drawing
93 public BufferedImage draw(AlignmentRowsCollectionI rows,
94 AlignmentColsCollectionI cols)
96 int rgbcolor = Color.white.getRGB();
100 for (int alignmentRow : rows)
107 // get details of this alignment row
108 SequenceI seq = rows.getSequence(alignmentRow);
110 // rate limiting step when rendering overview for lots of groups
111 SequenceGroup[] allGroups = al.findAllGroups(seq);
113 // calculate where this row extends to in pixels
114 int endRow = Math.min(Math.round((seqIndex + 1) * pixelsPerSeq) - 1,
115 miniMe.getHeight() - 1);
119 for (int alignmentCol : cols)
126 // calculate where this column extends to in pixels
127 int endCol = Math.min(Math.round((colIndex + 1) * pixelsPerCol) - 1,
128 miniMe.getWidth() - 1);
130 // don't do expensive colour determination if we're not going to use it
131 // NB this is important to avoid performance issues in the overview
133 if (pixelCol <= endCol)
135 rgbcolor = getColumnColourFromSequence(allGroups, seq,
136 alignmentCol, finder);
138 // fill in the appropriate number of pixels
139 for (int row = pixelRow; row <= endRow; ++row)
141 for (int col = pixelCol; col <= endCol; ++col)
143 miniMe.setRGB(col, row, rgbcolor);
147 pixelCol = endCol + 1;
151 pixelRow = endRow + 1;
155 overlayHiddenRegions(rows, cols);
160 * Find the colour of a sequence at a specified column position
163 * sequence to get colour for
165 * column position to get colour for
167 * FeatureColourFinder to use
168 * @return colour of sequence at this position, as RGB
170 private int getColumnColourFromSequence(SequenceGroup[] allGroups,
171 jalview.datamodel.SequenceI seq,
172 int lastcol, FeatureColourFinder fcfinder)
174 Color color = Color.white;
176 if ((seq != null) && (seq.getLength() > lastcol))
178 color = resColFinder.getResidueColour(true, shader, allGroups, seq,
183 return color.getRGB();
187 * Overlay the hidden regions on the overview image
190 * collection of rows the overview is built over
192 * collection of columns the overview is built over
194 private void overlayHiddenRegions(AlignmentRowsCollectionI rows,
195 AlignmentColsCollectionI cols)
197 if (cols.hasHidden() || rows.hasHidden())
199 BufferedImage mask = buildHiddenImage(rows, cols, miniMe.getWidth(),
202 Graphics2D g = (Graphics2D) miniMe.getGraphics();
203 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
205 g.drawImage(mask, 0, 0, miniMe.getWidth(), miniMe.getHeight(), null);
210 * Build a masking image of hidden columns and rows to be applied on top of
211 * the main overview image.
214 * collection of rows the overview is built over
216 * collection of columns the overview is built over
218 * width of overview in pixels
220 * height of overview in pixels
221 * @return BufferedImage containing mask of hidden regions
223 private BufferedImage buildHiddenImage(AlignmentRowsCollectionI rows,
224 AlignmentColsCollectionI cols, int width, int height)
227 BufferedImage hiddenImage = new BufferedImage(width, height,
228 BufferedImage.TYPE_INT_ARGB);
233 Color hidden = resColFinder.getHiddenColour();
235 Graphics2D g2d = (Graphics2D) hiddenImage.getGraphics();
237 // set background to transparent
238 g2d.setComposite(AlphaComposite.Clear);
239 g2d.fillRect(0, 0, width, height);
241 // set next colour to opaque
242 g2d.setComposite(AlphaComposite.Src);
244 for (int alignmentCol : cols)
251 // calculate where this column extends to in pixels
252 int endCol = Math.min(Math.round((colIndex + 1) * pixelsPerCol) - 1,
253 hiddenImage.getWidth() - 1);
255 if (pixelCol <= endCol)
257 // determine the colour based on the sequence and column position
258 if (cols.isHidden(alignmentCol))
260 g2d.setColor(hidden);
261 g2d.fillRect(pixelCol, 0, endCol - pixelCol + 1, height);
264 pixelCol = endCol + 1;
272 for (int alignmentRow : rows)
279 // calculate where this row extends to in pixels
280 int endRow = Math.min(Math.round((seqIndex + 1) * pixelsPerSeq) - 1,
281 miniMe.getHeight() - 1);
283 // get details of this alignment row
284 if (rows.isHidden(alignmentRow))
286 g2d.setColor(hidden);
287 g2d.fillRect(0, pixelRow, width, endRow - pixelRow + 1);
289 pixelRow = endRow + 1;
297 * Draw the alignment annotation in the overview panel
300 * the graphics object to draw on
302 * alignment annotation information
304 * alignment character width value
306 * y-position for the annotation graph
308 * the collection of columns used in the overview panel
310 public void drawGraph(Graphics g, AlignmentAnnotation anno, int charWidth,
311 int y, AlignmentColsCollectionI cols)
313 Annotation[] annotations = anno.annotations;
314 g.setColor(Color.white);
315 g.fillRect(0, 0, miniMe.getWidth(), y);
320 for (int alignmentCol : cols)
326 if (alignmentCol >= annotations.length)
328 break; // no more annotations to draw here
332 int endCol = Math.min(Math.round((colIndex + 1) * pixelsPerCol) - 1,
333 miniMe.getWidth() - 1);
335 if (annotations[alignmentCol] != null)
337 if (annotations[alignmentCol].colour == null)
339 g.setColor(Color.black);
343 g.setColor(annotations[alignmentCol].colour);
346 height = (int) ((annotations[alignmentCol].value / anno.graphMax)
353 g.fillRect(pixelCol, y - height, endCol - pixelCol + 1, height);
355 pixelCol = endCol + 1;
362 * Allows redraw flag to be set
365 * value to set redraw to: true = redraw is occurring, false = no
368 public void setRedraw(boolean b)