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.api.RendererListenerI;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.Annotation;
29 import jalview.datamodel.SequenceGroup;
30 import jalview.datamodel.SequenceI;
31 import jalview.renderer.seqfeatures.FeatureColourFinder;
32 import jalview.renderer.seqfeatures.FeatureRenderer;
33 import jalview.viewmodel.OverviewDimensions;
35 import java.awt.AlphaComposite;
36 import java.awt.Color;
37 import java.awt.Graphics;
38 import java.awt.Graphics2D;
39 import java.awt.image.BufferedImage;
40 import java.beans.PropertyChangeSupport;
42 public class OverviewRenderer
44 // transparency of hidden cols/seqs overlay
45 private final float TRANSPARENCY = 0.5f;
47 public static final String UPDATE = "OverviewUpdate";
49 private static final int MAX_PROGRESS = 100;
51 private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
54 private FeatureColourFinder finder;
57 private BufferedImage miniMe;
59 // raw number of pixels to allocate to each column
60 private float pixelsPerCol;
62 // raw number of pixels to allocate to each row
63 private float pixelsPerSeq;
65 // height in pixels of graph
66 private int graphHeight;
68 // flag to indicate whether to halt drawing
69 private volatile boolean redraw = false;
71 // reference to alignment, needed to get sequence groups
72 private AlignmentI al;
74 private ResidueShaderI shader;
76 private OverviewResColourFinder resColFinder;
78 public OverviewRenderer(FeatureRenderer fr, OverviewDimensions od,
80 ResidueShaderI resshader, OverviewResColourFinder colFinder)
82 finder = new FeatureColourFinder(fr);
83 resColFinder = colFinder;
88 pixelsPerCol = od.getPixelsPerCol();
89 pixelsPerSeq = od.getPixelsPerSeq();
90 graphHeight = od.getGraphHeight();
91 miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
92 BufferedImage.TYPE_INT_RGB);
96 * Draw alignment rows and columns onto an image
99 * Iterator over rows to be drawn
101 * Iterator over columns to be drawn
102 * @return image containing the drawing
104 public BufferedImage draw(AlignmentRowsCollectionI rows,
105 AlignmentColsCollectionI cols)
107 int rgbcolor = Color.white.getRGB();
110 int alignmentHeight = miniMe.getHeight() - graphHeight;
111 int totalPixels = miniMe.getWidth() * alignmentHeight;
113 int lastRowUpdate = 0;
115 changeSupport.firePropertyChange(UPDATE, -1, 0);
117 for (int alignmentRow : rows)
124 // get details of this alignment row
125 SequenceI seq = rows.getSequence(alignmentRow);
127 // rate limiting step when rendering overview for lots of groups
128 SequenceGroup[] allGroups = al.findAllGroups(seq);
130 // calculate where this row extends to in pixels
131 int endRow = Math.min(Math.round((seqIndex + 1) * pixelsPerSeq) - 1,
132 miniMe.getHeight() - 1);
136 for (int alignmentCol : cols)
143 // calculate where this column extends to in pixels
144 int endCol = Math.min(Math.round((colIndex + 1) * pixelsPerCol) - 1,
145 miniMe.getWidth() - 1);
147 // don't do expensive colour determination if we're not going to use it
148 // NB this is important to avoid performance issues in the overview
150 if (pixelCol <= endCol)
152 rgbcolor = getColumnColourFromSequence(allGroups, seq,
155 // fill in the appropriate number of pixels
156 for (int row = pixelRow; row <= endRow; ++row)
158 for (int col = pixelCol; col <= endCol; ++col)
160 miniMe.setRGB(col, row, rgbcolor);
164 // store last update value
165 lastUpdate = sendProgressUpdate(
166 (pixelCol + 1) * (endRow - pixelRow), totalPixels,
167 lastRowUpdate, lastUpdate);
169 pixelCol = endCol + 1;
174 if (pixelRow != endRow + 1)
176 // store row offset and last update value
177 lastRowUpdate = sendProgressUpdate(endRow + 1, alignmentHeight, 0,
179 lastUpdate = lastRowUpdate;
180 pixelRow = endRow + 1;
185 overlayHiddenRegions(rows, cols);
186 // final update to progress bar if present
189 sendProgressUpdate(pixelRow - 1, alignmentHeight, 0, 0);
193 sendProgressUpdate(alignmentHeight, miniMe.getHeight(), 0, 0);
199 * Calculate progress update value and fire event
200 * @param rowOffset number of rows to offset calculation by
201 * @return new rowOffset - return value only to be used when at end of a row
203 private int sendProgressUpdate(int position, int maximum, int rowOffset,
206 int newUpdate = rowOffset
207 + Math.round(MAX_PROGRESS * ((float) position / maximum));
208 if (newUpdate > lastUpdate)
210 changeSupport.firePropertyChange(UPDATE, rowOffset, newUpdate);
217 * Find the RGB value of the colour of a sequence at a specified column position
220 * sequence to get colour for
222 * column position to get colour for
223 * @return colour of sequence at this position, as RGB
225 int getColumnColourFromSequence(SequenceGroup[] allGroups,
226 SequenceI seq, int lastcol)
228 Color color = resColFinder.GAP_COLOUR;
230 if ((seq != null) && (seq.getLength() > lastcol))
232 color = resColFinder.getResidueColour(true, shader, allGroups, seq,
236 return color.getRGB();
240 * Overlay the hidden regions on the overview image
243 * collection of rows the overview is built over
245 * collection of columns the overview is built over
247 private void overlayHiddenRegions(AlignmentRowsCollectionI rows,
248 AlignmentColsCollectionI cols)
250 if (cols.hasHidden() || rows.hasHidden())
252 BufferedImage mask = buildHiddenImage(rows, cols, miniMe.getWidth(),
255 Graphics2D g = (Graphics2D) miniMe.getGraphics();
256 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
258 g.drawImage(mask, 0, 0, miniMe.getWidth(), miniMe.getHeight(), null);
263 * Build a masking image of hidden columns and rows to be applied on top of
264 * the main overview image.
267 * collection of rows the overview is built over
269 * collection of columns the overview is built over
271 * width of overview in pixels
273 * height of overview in pixels
274 * @return BufferedImage containing mask of hidden regions
276 private BufferedImage buildHiddenImage(AlignmentRowsCollectionI rows,
277 AlignmentColsCollectionI cols, int width, int height)
280 BufferedImage hiddenImage = new BufferedImage(width, height,
281 BufferedImage.TYPE_INT_ARGB);
286 Color hidden = resColFinder.getHiddenColour();
288 Graphics2D g2d = (Graphics2D) hiddenImage.getGraphics();
290 // set background to transparent
291 g2d.setComposite(AlphaComposite.Clear);
292 g2d.fillRect(0, 0, width, height);
294 // set next colour to opaque
295 g2d.setComposite(AlphaComposite.Src);
297 for (int alignmentCol : cols)
304 // calculate where this column extends to in pixels
305 int endCol = Math.min(Math.round((colIndex + 1) * pixelsPerCol) - 1,
306 hiddenImage.getWidth() - 1);
308 if (pixelCol <= endCol)
310 // determine the colour based on the sequence and column position
311 if (cols.isHidden(alignmentCol))
313 g2d.setColor(hidden);
314 g2d.fillRect(pixelCol, 0, endCol - pixelCol + 1, height);
317 pixelCol = endCol + 1;
325 for (int alignmentRow : rows)
332 // calculate where this row extends to in pixels
333 int endRow = Math.min(Math.round((seqIndex + 1) * pixelsPerSeq) - 1,
334 miniMe.getHeight() - 1);
336 // get details of this alignment row
337 if (rows.isHidden(alignmentRow))
339 g2d.setColor(hidden);
340 g2d.fillRect(0, pixelRow, width, endRow - pixelRow + 1);
342 pixelRow = endRow + 1;
350 * Draw the alignment annotation in the overview panel
353 * the graphics object to draw on
355 * alignment annotation information
357 * y-position for the annotation graph
359 * the collection of columns used in the overview panel
361 public void drawGraph(Graphics g, AlignmentAnnotation anno, int y,
362 AlignmentColsCollectionI cols)
364 Annotation[] annotations = anno.annotations;
365 g.setColor(Color.white);
366 g.fillRect(0, 0, miniMe.getWidth(), y);
371 for (int alignmentCol : cols)
375 changeSupport.firePropertyChange(UPDATE, MAX_PROGRESS - 1, 0);
379 if (alignmentCol >= annotations.length)
381 break; // no more annotations to draw here
385 int endCol = Math.min(Math.round((colIndex + 1) * pixelsPerCol) - 1,
386 miniMe.getWidth() - 1);
388 if (annotations[alignmentCol] != null)
390 if (annotations[alignmentCol].colour == null)
392 g.setColor(Color.black);
396 g.setColor(annotations[alignmentCol].colour);
399 height = (int) ((annotations[alignmentCol].value / anno.graphMax)
406 g.fillRect(pixelCol, y - height, endCol - pixelCol + 1, height);
409 pixelCol = endCol + 1;
413 changeSupport.firePropertyChange(UPDATE, MAX_PROGRESS - 1,
418 * Allows redraw flag to be set
421 * value to set redraw to: true = redraw is occurring, false = no
424 public void setRedraw(boolean b)
432 public void addPropertyChangeListener(RendererListenerI listener)
434 changeSupport.addPropertyChangeListener(listener);
437 public void removePropertyChangeListener(RendererListenerI listener)
439 changeSupport.removePropertyChangeListener(listener);