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.
23 import jalview.api.AlignViewportI;
24 import jalview.datamodel.SequenceI;
25 import jalview.renderer.AnnotationRenderer;
26 import jalview.renderer.seqfeatures.FeatureColourFinder;
27 import jalview.viewmodel.OverviewDimensions;
29 import java.awt.Color;
30 import java.awt.Dimension;
31 import java.awt.Graphics;
32 import java.awt.image.BufferedImage;
34 import javax.swing.JComponent;
37 public class OverviewCanvas extends JComponent
39 private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
41 // This is set true if the alignment view changes whilst
42 // the overview is being calculated
43 private volatile boolean restart = false;
45 private volatile boolean updaterunning = false;
47 private BufferedImage miniMe;
49 private BufferedImage lastMiniMe = null;
51 // Can set different properties in this seqCanvas than
52 // main visible SeqCanvas
53 private SequenceRenderer sr;
55 private jalview.renderer.seqfeatures.FeatureRenderer fr;
57 private final AnnotationRenderer renderer = new AnnotationRenderer();
59 OverviewDimensions od;
63 public OverviewCanvas(OverviewDimensions overviewDims,
64 AlignViewportI alignvp)
69 sr = new SequenceRenderer(av);
70 sr.renderGaps = false;
71 sr.forOverview = true;
72 fr = new jalview.renderer.seqfeatures.FeatureRenderer(av);
76 * Signals to drawing code that the associated alignment viewport
77 * has changed and a redraw will be required
79 public boolean restartDraw()
95 public void draw(boolean showSequenceFeatures, boolean showAnnotation,
96 FeatureRenderer transferRenderer)
100 if (showSequenceFeatures)
102 fr.transferSettings(transferRenderer);
105 // why do we need to set preferred size again? was set in
106 // updateOverviewImage
107 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
109 miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
110 BufferedImage.TYPE_INT_RGB);
112 Graphics mg = miniMe.getGraphics();
113 mg.setColor(Color.orange);
114 mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight());
116 // calculate sampleCol and sampleRow
117 // alignment width is max number of residues/bases
118 // alignment height is number of sequences
119 int alwidth = av.getAlignment().getWidth();
120 int alheight = av.getAlignment().getAbsoluteHeight();
122 // sampleCol or sampleRow is the width/height allocated to each residue
123 // in particular, sometimes we may need more than one row/col of the
124 // BufferedImage allocated
125 // sampleCol is how much of a residue to assign to each pixel
126 // sampleRow is how many sequences to assign to each pixel
127 float sampleCol = alwidth / (float) od.getWidth();
128 float sampleRow = alheight / (float) od.getSequencesHeight();
130 buildImage(sampleRow, sampleCol);
134 renderer.updateFromAlignViewport(av);
135 for (int col = 0; col < od.getWidth() && !restart; col++)
137 mg.translate(col, od.getSequencesHeight());
138 renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(),
139 av.getAlignmentConservationAnnotation().annotations,
140 (int) (sampleCol) + 1, od.getGraphHeight(),
141 (int) (col * sampleCol), (int) (col * sampleCol) + 1);
142 mg.translate(-col, -od.getSequencesHeight());
151 draw(showSequenceFeatures, showAnnotation, transferRenderer);
155 updaterunning = false;
161 public void paintComponent(Graphics g)
165 if (lastMiniMe == null)
167 g.setColor(Color.white);
168 g.fillRect(0, 0, getWidth(), getHeight());
172 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
174 g.setColor(TRANS_GREY);
175 g.fillRect(0, 0, getWidth(), getHeight());
177 else if (lastMiniMe != null)
179 g.drawImage(lastMiniMe, 0, 0, this);
180 if (lastMiniMe != miniMe)
182 g.setColor(TRANS_GREY);
183 g.fillRect(0, 0, getWidth(), getHeight());
187 g.setColor(Color.red);
192 * Build the overview panel image
194 private void buildImage(float sampleRow, float sampleCol)
198 int rgbcolor = Color.white.getRGB();
200 SequenceI seq = null;
201 FeatureColourFinder finder = new FeatureColourFinder(fr);
203 final boolean hasHiddenCols = av.hasHiddenColumns();
204 boolean hiddenRow = false;
205 // get hidden row and hidden column map once at beginning.
206 // clone featureRenderer settings to avoid race conditions... if state is
207 // updated just need to refresh again
208 for (int row = 0; row < od.getSequencesHeight() && !restart; row++)
210 boolean doCopy = true;
211 int currentrow = (int) (row * sampleRow);
212 if (currentrow != lastrow)
216 lastrow = currentrow;
218 // get the sequence which would be at alignment index 'lastrow' if no
219 // rows were hidden, and determine whether it is hidden or not
220 hiddenRow = av.getAlignment().isHidden(lastrow);
221 seq = av.getAlignment().getSequenceAtAbsoluteIndex(lastrow);
224 for (int col = 0; col < od.getWidth() && !restart; col++)
228 rgbcolor = miniMe.getRGB(col, row - 1);
230 else if ((int) (col * sampleCol) != lastcol
231 || (int) (row * sampleRow) != lastrow)
233 lastcol = (int) (col * sampleCol);
234 rgbcolor = getColumnColourFromSequence(seq, hiddenRow,
235 hasHiddenCols, lastcol, finder);
237 // else we just use the color we already have , so don't need to set it
239 miniMe.setRGB(col, row, rgbcolor);
245 * Find the colour of a sequence at a specified column position
247 private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
248 boolean hiddenRow, boolean hasHiddenCols, int lastcol,
249 FeatureColourFinder finder)
251 Color color = Color.white;
253 if ((seq != null) && (seq.getLength() > lastcol))
255 color = sr.getResidueColour(seq, lastcol, finder);
259 || (hasHiddenCols && !av.getColumnSelection()
260 .isVisible(lastcol)))
262 color = color.darker().darker();
265 return color.getRGB();