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.Annotation;
28 import jalview.datamodel.SequenceI;
29 import jalview.renderer.seqfeatures.FeatureColourFinder;
30 import jalview.renderer.seqfeatures.FeatureRenderer;
31 import jalview.viewmodel.OverviewDimensions;
33 import java.awt.Color;
34 import java.awt.Graphics;
35 import java.awt.image.BufferedImage;
36 import java.beans.PropertyChangeSupport;
38 public class OverviewRenderer
40 public static final String UPDATE = "OverviewUpdate";
42 protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
45 private FeatureColourFinder finder;
47 private jalview.api.SequenceRenderer sr;
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 // height in pixels of graph
59 private int graphHeight;
61 // flag to indicate whether to halt drawing
62 private volatile boolean redraw = false;
64 public OverviewRenderer(jalview.api.SequenceRenderer seqRenderer,
65 FeatureRenderer fr, OverviewDimensions od)
68 finder = new FeatureColourFinder(fr);
70 pixelsPerCol = od.getPixelsPerCol();
71 pixelsPerSeq = od.getPixelsPerSeq();
72 graphHeight = od.getGraphHeight();
73 miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
74 BufferedImage.TYPE_INT_RGB);
78 * Draw alignment rows and columns onto an image
81 * Iterator over rows to be drawn
83 * Iterator over columns to be drawn
84 * @return image containing the drawing
86 public BufferedImage draw(AlignmentRowsCollectionI rows,
87 AlignmentColsCollectionI cols)
89 int rgbcolor = Color.white.getRGB();
92 int alignmentHeight = miniMe.getHeight() - graphHeight;
94 changeSupport.firePropertyChange(UPDATE, -1, 0);
96 for (int alignmentRow : rows)
103 // get details of this alignment row
104 boolean hidden = rows.isHidden(alignmentRow);
105 SequenceI seq = rows.getSequence(alignmentRow);
107 // calculate where this row extends to in pixels
108 int endRow = Math.min(Math.round((seqIndex + 1) * pixelsPerSeq) - 1,
109 miniMe.getHeight() - 1);
113 for (int alignmentCol : cols)
121 // calculate where this column extends to in pixels
122 int endCol = Math.min(
123 Math.round((colIndex + 1) * pixelsPerCol) - 1,
124 miniMe.getWidth() - 1);
126 // don't do expensive colour determination if we're not going to use it
127 // NB this is important to avoid performance issues in the overview
129 if (pixelCol <= endCol)
131 // determine the colour based on the sequence and column position
132 rgbcolor = getColumnColourFromSequence(seq,
133 hidden || cols.isHidden(alignmentCol), alignmentCol,
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;
150 if (pixelRow != endRow + 1)
152 changeSupport.firePropertyChange(UPDATE,
153 Math.round(100 * (float) pixelRow / alignmentHeight),
155 100 * ((float) (endRow + 1) / alignmentHeight)));
156 pixelRow = endRow + 1;
161 // final update to progress bar if present
164 changeSupport.firePropertyChange(UPDATE,
165 Math.round(100 * (float) (pixelRow - 1) / alignmentHeight),
170 changeSupport.firePropertyChange(UPDATE,
171 Math.round(100 * alignmentHeight / miniMe.getHeight() - 1),
172 Math.round(100 * alignmentHeight / miniMe.getHeight()));
178 * Find the colour of a sequence at a specified column position
180 private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
181 boolean isHidden, int lastcol, FeatureColourFinder fcfinder)
183 Color color = Color.white;
185 if ((seq != null) && (seq.getLength() > lastcol))
187 color = sr.getResidueColour(seq, lastcol, fcfinder);
192 color = color.darker().darker();
195 return color.getRGB();
199 * Draw the alignment annotation in the overview panel
202 * the graphics object to draw on
204 * alignment annotation information
206 * alignment character width value
208 * y-position for the annotation graph
210 * the collection of columns used in the overview panel
212 public void drawGraph(Graphics g, AlignmentAnnotation anno, int charWidth,
213 int y, AlignmentColsCollectionI cols)
215 Annotation[] annotations = anno.annotations;
216 g.setColor(Color.white);
217 g.fillRect(0, 0, miniMe.getWidth(), y);
222 for (int alignmentCol : cols)
226 changeSupport.firePropertyChange(UPDATE, 99, 0);
230 if (alignmentCol >= annotations.length)
232 break; // no more annotations to draw here
236 int endCol = Math.min(
237 Math.round((colIndex + 1) * pixelsPerCol) - 1,
238 miniMe.getWidth() - 1);
240 if (annotations[alignmentCol] != null)
242 if (annotations[alignmentCol].colour == null)
244 g.setColor(Color.black);
248 g.setColor(annotations[alignmentCol].colour);
251 height = (int) ((annotations[alignmentCol].value / anno.graphMax) * y);
257 g.fillRect(pixelCol, y - height, endCol - pixelCol + 1, height);
259 pixelCol = endCol + 1;
263 changeSupport.firePropertyChange(UPDATE, 99, 100);
266 public void setRedraw(boolean b)
274 public void addPropertyChangeListener(RendererListenerI listener)
276 changeSupport.addPropertyChangeListener(listener);
279 public void removePropertyChangeListener(RendererListenerI listener)
281 changeSupport.removePropertyChangeListener(listener);