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.renderer.AnnotationRenderer;
24 import jalview.viewmodel.OverviewDimensions;
26 import java.awt.Color;
27 import java.awt.Dimension;
28 import java.awt.Graphics;
29 import java.awt.event.ComponentAdapter;
30 import java.awt.event.ComponentEvent;
31 import java.awt.event.MouseAdapter;
32 import java.awt.event.MouseEvent;
33 import java.awt.event.MouseMotionAdapter;
34 import java.awt.image.BufferedImage;
36 import javax.swing.JPanel;
39 * Panel displaying an overview of the full alignment, with an interactive box
40 * representing the viewport onto the alignment.
45 public class OverviewPanel extends JPanel implements Runnable
47 private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
49 private final AnnotationRenderer renderer = new AnnotationRenderer();
51 private OverviewDimensions od;
53 private BufferedImage miniMe;
55 private BufferedImage lastMiniMe = null;
57 private AlignViewport av;
59 private AlignmentPanel ap;
62 private boolean resizing = false;
64 // This is set true if the user resizes whilst
65 // the overview is being calculated
66 private boolean resizeAgain = false;
68 // Can set different properties in this seqCanvas than
69 // main visible SeqCanvas
70 private SequenceRenderer sr;
72 private jalview.renderer.seqfeatures.FeatureRenderer fr;
75 * Creates a new OverviewPanel object.
78 * The alignment panel which is shown in the overview panel
80 public OverviewPanel(AlignmentPanel alPanel)
86 sr = new SequenceRenderer(av);
87 sr.renderGaps = false;
88 sr.forOverview = true;
89 fr = new FeatureRenderer(alPanel);
91 od = new OverviewDimensions(av);
93 addComponentListener(new ComponentAdapter()
96 public void componentResized(ComponentEvent evt)
98 if ((getWidth() != od.getWidth())
99 || (getHeight() != (od.getHeight())))
101 updateOverviewImage();
106 addMouseMotionListener(new MouseMotionAdapter()
109 public void mouseDragged(MouseEvent evt)
111 if (!av.getWrapAlignment())
113 // TODO: feature: jv2.5 detect shift drag and update selection from
115 od.setBoxPositionByMouse(evt.getX(), evt.getY());
116 ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
121 addMouseListener(new MouseAdapter()
124 public void mousePressed(MouseEvent evt)
126 if (!av.getWrapAlignment())
128 od.setBoxPositionByMouse(evt.getX(), evt.getY());
129 ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
134 updateOverviewImage();
138 * Updates the overview image when the related alignment panel is updated
140 public void updateOverviewImage()
150 if ((getWidth() > 0) && (getHeight() > 0))
152 od.setWidth(getWidth()); // width = getWidth();
153 od.setHeight(getHeight()); // sequencesHeight = getHeight() - graphHeight;
156 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
158 Thread thread = new Thread(this);
168 if (av.isShowSequenceFeatures())
170 fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer());
173 // why do we need to set preferred size again? was set in
174 // updateOverviewImage
175 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
177 miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
178 BufferedImage.TYPE_INT_RGB);
180 Graphics mg = miniMe.getGraphics();
181 mg.setColor(Color.orange);
182 mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight());
184 // calculate scale based on current alignment width and height
187 // calculate sampleCol and sampleRow
188 // alignment width is max number of residues/bases
189 // alignment height is number of sequences
190 int alwidth = av.getAlignment().getWidth();
191 int alheight = av.getAlignment().getHeight()
192 + av.getAlignment().getHiddenSequences().getSize();
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.getAlignmentConservationAnnotation() != null)
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();
235 private void buildImage(float sampleRow, float sampleCol)
239 int color = Color.white.getRGB();
241 jalview.datamodel.SequenceI seq = null;
242 final boolean hasHiddenRows = av.hasHiddenRows();
243 final boolean hasHiddenCols = av.hasHiddenColumns();
244 boolean hiddenRow = false;
245 // get hidden row and hidden column map once at beginning.
246 // clone featureRenderer settings to avoid race conditions... if state is
247 // updated just need to refresh again
248 for (int row = 0; row < od.getSequencesHeight() && !resizeAgain; row++)
250 boolean doCopy = true;
251 int currentrow = (int) (row * sampleRow);
252 if (currentrow != lastrow)
256 lastrow = currentrow;
259 // should be method(s) in Alignment
262 seq = av.getAlignment().getHiddenSequences()
263 .getHiddenSequence(lastrow);
266 int index = av.getAlignment().getHiddenSequences()
267 .findIndexWithoutHiddenSeqs(lastrow);
269 seq = av.getAlignment().getSequenceAt(index);
278 seq = av.getAlignment().getSequenceAt(lastrow);
280 // end of Alignment method(s)
285 System.out.println(lastrow + " null");
289 for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
293 color = miniMe.getRGB(col, row - 1);
295 else if ((int) (col * sampleCol) != lastcol
296 || (int) (row * sampleRow) != lastrow)
298 lastcol = (int) (col * sampleCol);
299 color = getColumnColourFromSequence(seq, hiddenRow, hasHiddenCols,
302 // else if ((int) (col * sampleCol) == lastcol && (int) (row *
303 // sampleRow) == lastrow))
304 // we just use the color we already have , so don't need to set it
306 miniMe.setRGB(col, row, color);
312 * Find the colour of a sequence at a specified column position
314 private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
315 boolean hiddenRow, boolean hasHiddenCols, int lastcol)
319 if (seq.getLength() > lastcol)
321 color = sr.getResidueBoxColour(seq, lastcol).getRGB();
323 if (av.isShowSequenceFeatures())
325 color = fr.findFeatureColour(color, seq, lastcol);
330 color = Color.white.getRGB(); // White
334 || (hasHiddenCols && !av.getColumnSelection()
335 .isVisible(lastcol)))
337 color = new Color(color).darker().darker().getRGB();
344 * Update the overview panel box when the associated alignment panel is
348 public void setBoxPosition()
356 public void paintComponent(Graphics g)
358 if (resizing || resizeAgain)
360 if (lastMiniMe == null)
362 g.setColor(Color.white);
363 g.fillRect(0, 0, getWidth(), getHeight());
367 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
369 g.setColor(TRANS_GREY);
370 g.fillRect(0, 0, getWidth(), getHeight());
372 else if (lastMiniMe != null)
374 g.drawImage(lastMiniMe, 0, 0, this);
375 if (lastMiniMe != miniMe)
377 g.setColor(TRANS_GREY);
378 g.fillRect(0, 0, getWidth(), getHeight());
381 // TODO: render selected regions
382 g.setColor(Color.red);