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 BufferedImage miniMe;
47 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;
65 public OverviewCanvas(OverviewDimensions overviewDims,
66 AlignViewportI alignvp, AlignmentPanel alignp)
72 sr = new SequenceRenderer(av);
73 sr.renderGaps = false;
74 sr.forOverview = true;
75 fr = new FeatureRenderer(ap);
79 * Signals to drawing code that the associated alignment viewport
80 * has changed and a redraw will be required
82 public void restartDraw()
87 public void draw(boolean showSequenceFeatures, boolean showAnnotation)
91 if (showSequenceFeatures)
93 fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer());
96 // why do we need to set preferred size again? was set in
97 // updateOverviewImage
98 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
100 miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
101 BufferedImage.TYPE_INT_RGB);
103 Graphics mg = miniMe.getGraphics();
104 mg.setColor(Color.orange);
105 mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight());
107 // calculate sampleCol and sampleRow
108 // alignment width is max number of residues/bases
109 // alignment height is number of sequences
110 int alwidth = av.getAlignment().getWidth();
111 int alheight = av.getAlignment().getAbsoluteHeight();
113 // sampleCol or sampleRow is the width/height allocated to each residue
114 // in particular, sometimes we may need more than one row/col of the
115 // BufferedImage allocated
116 // sampleCol is how much of a residue to assign to each pixel
117 // sampleRow is how many sequences to assign to each pixel
118 float sampleCol = alwidth / (float) od.getWidth();
119 float sampleRow = alheight / (float) od.getSequencesHeight();
121 buildImage(sampleRow, sampleCol);
125 renderer.updateFromAlignViewport(av);
126 for (int col = 0; col < od.getWidth() && !restart; col++)
128 mg.translate(col, od.getSequencesHeight());
129 renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(),
130 av.getAlignmentConservationAnnotation().annotations,
131 (int) (sampleCol) + 1, od.getGraphHeight(),
132 (int) (col * sampleCol), (int) (col * sampleCol) + 1);
133 mg.translate(-col, -od.getSequencesHeight());
142 draw(showSequenceFeatures, showAnnotation);
151 public void paintComponent(Graphics g)
155 if (lastMiniMe == null)
157 g.setColor(Color.white);
158 g.fillRect(0, 0, getWidth(), getHeight());
162 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
164 g.setColor(TRANS_GREY);
165 g.fillRect(0, 0, getWidth(), getHeight());
167 else if (lastMiniMe != null)
169 g.drawImage(lastMiniMe, 0, 0, this);
170 if (lastMiniMe != miniMe)
172 g.setColor(TRANS_GREY);
173 g.fillRect(0, 0, getWidth(), getHeight());
177 g.setColor(Color.red);
182 * Build the overview panel image
184 private void buildImage(float sampleRow, float sampleCol)
188 int rgbcolor = Color.white.getRGB();
190 SequenceI seq = null;
191 FeatureColourFinder finder = new FeatureColourFinder(fr);
193 final boolean hasHiddenCols = av.hasHiddenColumns();
194 boolean hiddenRow = false;
195 // get hidden row and hidden column map once at beginning.
196 // clone featureRenderer settings to avoid race conditions... if state is
197 // updated just need to refresh again
198 for (int row = 0; row < od.getSequencesHeight() && !restart; row++)
200 boolean doCopy = true;
201 int currentrow = (int) (row * sampleRow);
202 if (currentrow != lastrow)
206 lastrow = currentrow;
208 // get the sequence which would be at alignment index 'lastrow' if no
209 // rows were hidden, and determine whether it is hidden or not
210 hiddenRow = av.getAlignment().isHidden(lastrow);
211 seq = av.getAlignment().getSequenceAtAbsoluteIndex(lastrow);
214 for (int col = 0; col < od.getWidth() && !restart; col++)
218 rgbcolor = miniMe.getRGB(col, row - 1);
220 else if ((int) (col * sampleCol) != lastcol
221 || (int) (row * sampleRow) != lastrow)
223 lastcol = (int) (col * sampleCol);
224 rgbcolor = getColumnColourFromSequence(seq, hiddenRow,
225 hasHiddenCols, lastcol, finder);
227 // else we just use the color we already have , so don't need to set it
229 miniMe.setRGB(col, row, rgbcolor);
235 * Find the colour of a sequence at a specified column position
237 private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
238 boolean hiddenRow, boolean hasHiddenCols, int lastcol,
239 FeatureColourFinder finder)
241 Color color = Color.white;
243 if ((seq != null) && (seq.getLength() > lastcol))
245 color = sr.getResidueColour(seq, lastcol, finder);
249 || (hasHiddenCols && !av.getColumnSelection()
250 .isVisible(lastcol)))
252 color = color.darker().darker();
255 return color.getRGB();