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.datamodel.SequenceI;
24 import jalview.renderer.AnnotationRenderer;
25 import jalview.renderer.seqfeatures.FeatureColourFinder;
26 import jalview.viewmodel.OverviewDimensions;
28 import java.awt.Color;
29 import java.awt.Dimension;
30 import java.awt.Graphics;
31 import java.awt.event.ComponentAdapter;
32 import java.awt.event.ComponentEvent;
33 import java.awt.event.MouseAdapter;
34 import java.awt.event.MouseEvent;
35 import java.awt.event.MouseMotionAdapter;
36 import java.awt.image.BufferedImage;
38 import javax.swing.JPanel;
41 * Panel displaying an overview of the full alignment, with an interactive box
42 * representing the viewport onto the alignment.
47 public class OverviewPanel extends JPanel implements Runnable
49 private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
51 private final AnnotationRenderer renderer = new AnnotationRenderer();
53 private OverviewDimensions od;
55 private BufferedImage miniMe;
57 private BufferedImage lastMiniMe = null;
59 private AlignViewport av;
61 private AlignmentPanel ap;
64 private boolean resizing = false;
66 // This is set true if the user resizes whilst
67 // the overview is being calculated
68 private boolean resizeAgain = false;
70 // Can set different properties in this seqCanvas than
71 // main visible SeqCanvas
72 private SequenceRenderer sr;
74 jalview.renderer.seqfeatures.FeatureRenderer fr;
77 * Creates a new OverviewPanel object.
80 * The alignment panel which is shown in the overview panel
82 public OverviewPanel(AlignmentPanel alPanel)
88 sr = new SequenceRenderer(av);
89 sr.renderGaps = false;
90 sr.forOverview = true;
91 fr = new FeatureRenderer(ap);
93 od = new OverviewDimensions(av.getRanges(), av.isShowAnnotation());
95 addComponentListener(new ComponentAdapter()
98 public void componentResized(ComponentEvent evt)
100 if ((getWidth() != od.getWidth())
101 || (getHeight() != (od.getHeight())))
103 updateOverviewImage();
108 addMouseMotionListener(new MouseMotionAdapter()
111 public void mouseDragged(MouseEvent evt)
113 if (!av.getWrapAlignment())
115 od.updateViewportFromMouse(evt.getX(), evt.getY(), av
116 .getAlignment().getHiddenSequences(), av
117 .getColumnSelection(), av.getRanges());
118 ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
123 addMouseListener(new MouseAdapter()
126 public void mousePressed(MouseEvent evt)
128 if (!av.getWrapAlignment())
130 od.updateViewportFromMouse(evt.getX(), evt.getY(), av
131 .getAlignment().getHiddenSequences(), av
132 .getColumnSelection(), av.getRanges());
133 ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
138 updateOverviewImage();
142 * Updates the overview image when the related alignment panel is updated
144 public void updateOverviewImage()
154 if ((getWidth() > 0) && (getHeight() > 0))
156 od.setWidth(getWidth());
157 od.setHeight(getHeight());
160 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
162 Thread thread = new Thread(this);
172 if (av.isShowSequenceFeatures())
174 fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer());
177 // why do we need to set preferred size again? was set in
178 // updateOverviewImage
179 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
181 miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
182 BufferedImage.TYPE_INT_RGB);
184 Graphics mg = miniMe.getGraphics();
185 mg.setColor(Color.orange);
186 mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight());
188 // calculate sampleCol and sampleRow
189 // alignment width is max number of residues/bases
190 // alignment height is number of sequences
191 int alwidth = av.getAlignment().getWidth();
192 int alheight = av.getAlignment().getAbsoluteHeight();
194 // sampleCol or sampleRow is the width/height allocated to each residue
195 // in particular, sometimes we may need more than one row/col of the
196 // BufferedImage allocated
197 // sampleCol is how much of a residue to assign to each pixel
198 // sampleRow is how many sequences to assign to each pixel
199 float sampleCol = alwidth / (float) od.getWidth();
200 float sampleRow = alheight / (float) od.getSequencesHeight();
202 buildImage(sampleRow, sampleCol);
204 if (av.isShowAnnotation())
206 renderer.updateFromAlignViewport(av);
207 for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
209 mg.translate(col, od.getSequencesHeight());
210 renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(),
211 av.getAlignmentConservationAnnotation().annotations,
212 (int) (sampleCol) + 1, od.getGraphHeight(),
213 (int) (col * sampleCol), (int) (col * sampleCol) + 1);
214 mg.translate(-col, -od.getSequencesHeight());
225 updateOverviewImage();
236 * Build the overview panel image
238 private void buildImage(float sampleRow, float sampleCol)
242 int rgbColour = Color.white.getRGB();
244 SequenceI seq = null;
245 FeatureColourFinder finder = new FeatureColourFinder(fr);
247 final boolean hasHiddenCols = av.hasHiddenColumns();
248 boolean hiddenRow = false;
249 // get hidden row and hidden column map once at beginning.
250 // clone featureRenderer settings to avoid race conditions... if state is
251 // updated just need to refresh again
252 for (int row = 0; row < od.getSequencesHeight() && !resizeAgain; row++)
254 boolean doCopy = true;
255 int currentrow = (int) (row * sampleRow);
256 if (currentrow != lastrow)
260 lastrow = currentrow;
262 // get the sequence which would be at alignment index 'lastrow' if no
263 // rows were hidden, and determine whether it is hidden or not
264 hiddenRow = av.getAlignment().isHidden(lastrow);
265 seq = av.getAlignment().getSequenceAtAbsoluteIndex(lastrow);
268 for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
272 rgbColour = miniMe.getRGB(col, row - 1);
274 else if ((int) (col * sampleCol) != lastcol
275 || (int) (row * sampleRow) != lastrow)
277 lastcol = (int) (col * sampleCol);
278 rgbColour = getColumnColourFromSequence(seq, hiddenRow,
279 hasHiddenCols, lastcol, finder);
281 // else we just use the color we already have , so don't need to set it
283 miniMe.setRGB(col, row, rgbColour);
289 * Find the colour of a sequence at a specified column position
291 private int getColumnColourFromSequence(
292 jalview.datamodel.SequenceI seq,
293 boolean hiddenRow, boolean hasHiddenCols, int lastcol,
294 FeatureColourFinder finder)
296 Color color = Color.white;
298 if ((seq != null) && (seq.getLength() > lastcol))
300 color = sr.getResidueColour(seq, lastcol, finder);
304 || (hasHiddenCols && !av.getColumnSelection()
305 .isVisible(lastcol)))
307 color = color.darker().darker();
310 return color.getRGB();
314 * Update the overview panel box when the associated alignment panel is
318 public void setBoxPosition()
320 od.setBoxPosition(av.getAlignment()
321 .getHiddenSequences(), av.getColumnSelection(), av.getRanges());
327 public void paintComponent(Graphics g)
329 if (resizing || resizeAgain)
331 if (lastMiniMe == null)
333 g.setColor(Color.white);
334 g.fillRect(0, 0, getWidth(), getHeight());
338 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
340 g.setColor(TRANS_GREY);
341 g.fillRect(0, 0, getWidth(), getHeight());
343 else if (lastMiniMe != null)
345 g.drawImage(lastMiniMe, 0, 0, this);
346 if (lastMiniMe != miniMe)
348 g.setColor(TRANS_GREY);
349 g.fillRect(0, 0, getWidth(), getHeight());
353 g.setColor(Color.red);