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.util.Comparison;
33 import jalview.viewmodel.OverviewDimensions;
35 import java.awt.Color;
36 import java.awt.Graphics;
37 import java.awt.image.BufferedImage;
39 public class OverviewRenderer
41 private FeatureColourFinder finder;
43 private jalview.api.SequenceRenderer sr;
46 private BufferedImage miniMe;
48 // raw number of pixels to allocate to each column
49 private float pixelsPerCol;
51 // raw number of pixels to allocate to each row
52 private float pixelsPerSeq;
54 // flag to indicate whether to halt drawing
55 private volatile boolean redraw = false;
57 // reference to alignment, needed to get sequence groups
58 private AlignmentI al;
60 private ResidueShaderI shader;
62 public OverviewRenderer(jalview.api.SequenceRenderer seqRenderer,
63 FeatureRenderer fr, OverviewDimensions od, AlignmentI alignment,
64 ResidueShaderI resshader)
67 finder = new FeatureColourFinder(fr);
72 pixelsPerCol = od.getPixelsPerCol();
73 pixelsPerSeq = od.getPixelsPerSeq();
74 miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
75 BufferedImage.TYPE_INT_RGB);
79 * Draw alignment rows and columns onto an image
82 * Iterator over rows to be drawn
84 * Iterator over columns to be drawn
85 * @return image containing the drawing
87 public BufferedImage draw(AlignmentRowsCollectionI rows,
88 AlignmentColsCollectionI cols)
90 int rgbcolor = Color.white.getRGB();
94 for (int alignmentRow : rows)
101 // get details of this alignment row
102 boolean hidden = rows.isHidden(alignmentRow);
103 SequenceI seq = rows.getSequence(alignmentRow);
104 // rate limiting step when rendering overview for lots of groups
105 SequenceGroup[] allGroups = al.findAllGroups(seq);
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)
120 // calculate where this column extends to in pixels
121 int endCol = Math.min(
122 Math.round((colIndex + 1) * pixelsPerCol) - 1,
123 miniMe.getWidth() - 1);
125 // don't do expensive colour determination if we're not going to use it
126 // NB this is important to avoid performance issues in the overview
128 if (pixelCol <= endCol)
130 // determine the colour based on the sequence and column position
131 rgbcolor = getColumnColourFromSequence(allGroups, seq,
132 hidden || cols.isHidden(alignmentCol), alignmentCol,
135 // fill in the appropriate number of pixels
136 for (int row = pixelRow; row <= endRow; ++row)
138 for (int col = pixelCol; col <= endCol; ++col)
140 miniMe.setRGB(col, row, rgbcolor);
144 pixelCol = endCol + 1;
148 pixelRow = endRow + 1;
155 * Find the colour of a sequence at a specified column position
157 private int getColumnColourFromSequence(SequenceGroup[] allGroups,
158 jalview.datamodel.SequenceI seq,
159 boolean isHidden, int lastcol, FeatureColourFinder fcfinder)
161 Color color = Color.white;
163 if ((seq != null) && (seq.getLength() > lastcol))
165 color = getResidueColour(allGroups, seq, lastcol, fcfinder);
170 color = color.darker().darker();
173 return color.getRGB();
176 private Color getResidueColour(SequenceGroup[] allGroups,
177 final SequenceI seq, int position,
178 FeatureColourFinder finder)
180 Color col = getResidueBoxColour(allGroups, seq, position);
184 col = finder.findFeatureColour(col, seq, position);
189 protected Color getResidueBoxColour(SequenceGroup[] allGroups,
190 SequenceI seq, int i)
193 ResidueShaderI currentShader;
195 SequenceGroup currentSequenceGroup = inCurrentSequenceGroup(allGroups,
197 if (currentSequenceGroup != null)
199 currentShader = currentSequenceGroup.getGroupColourScheme();
203 currentShader = shader;
206 return getBoxColour(currentShader, seq, i);
209 SequenceGroup inCurrentSequenceGroup(SequenceGroup[] allGroups, int res)
211 if (allGroups == null)
216 for (int i = 0; i < allGroups.length; i++)
218 if ((allGroups[i].getStartRes() <= res)
219 && (allGroups[i].getEndRes() >= res))
221 return (allGroups[i]);
228 Color getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
230 Color resBoxColour = Color.white;
231 char currentChar = seq.getCharAt(i);
233 if (shader.getColourScheme() != null)
235 if (Comparison.isGap(currentChar)
236 && !shader.getColourScheme().hasGapColour())
238 resBoxColour = Color.lightGray;
242 resBoxColour = shader.findColour(currentChar, i, seq);
245 else if (Comparison.isGap(currentChar))
247 resBoxColour = Color.lightGray;
254 * Draw the alignment annotation in the overview panel
257 * the graphics object to draw on
259 * alignment annotation information
261 * alignment character width value
263 * y-position for the annotation graph
265 * the collection of columns used in the overview panel
267 public void drawGraph(Graphics g, AlignmentAnnotation anno, int charWidth,
268 int y, AlignmentColsCollectionI cols)
270 Annotation[] annotations = anno.annotations;
271 g.setColor(Color.white);
272 g.fillRect(0, 0, miniMe.getWidth(), y);
277 for (int alignmentCol : cols)
283 if (alignmentCol >= annotations.length)
285 break; // no more annotations to draw here
289 int endCol = Math.min(
290 Math.round((colIndex + 1) * pixelsPerCol) - 1,
291 miniMe.getWidth() - 1);
293 if (annotations[alignmentCol] != null)
295 if (annotations[alignmentCol].colour == null)
297 g.setColor(Color.black);
301 g.setColor(annotations[alignmentCol].colour);
304 height = (int) ((annotations[alignmentCol].value / anno.graphMax) * y);
310 g.fillRect(pixelCol, y - height, endCol - pixelCol + 1, height);
312 pixelCol = endCol + 1;
318 public void setRedraw(boolean b)