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 boolean showAnnotation = true;
92 // TODO: in applet this was getSequenceConsensusHash()
93 // check if it makes any functional difference: hconsensus or conservation
94 if (av.getAlignmentConservationAnnotation() == null)
96 showAnnotation = false;
98 od = new OverviewDimensions(av.getPosProps(), showAnnotation);
100 addComponentListener(new ComponentAdapter()
103 public void componentResized(ComponentEvent evt)
105 if ((getWidth() != od.getWidth())
106 || (getHeight() != (od.getHeight())))
108 updateOverviewImage();
113 addMouseMotionListener(new MouseMotionAdapter()
116 public void mouseDragged(MouseEvent evt)
118 if (!av.getWrapAlignment())
120 // TODO: feature: jv2.5 detect shift drag and update selection from
122 od.updateViewportFromMouse(evt.getX(), evt.getY(), av
123 .getAlignment().getHiddenSequences(), av
124 .getColumnSelection(), av.getPosProps());
125 ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
130 addMouseListener(new MouseAdapter()
133 public void mousePressed(MouseEvent evt)
135 if (!av.getWrapAlignment())
137 od.updateViewportFromMouse(evt.getX(), evt.getY(), av
138 .getAlignment().getHiddenSequences(), av
139 .getColumnSelection(), av.getPosProps());
140 ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
145 updateOverviewImage();
149 * Updates the overview image when the related alignment panel is updated
151 public void updateOverviewImage()
161 if ((getWidth() > 0) && (getHeight() > 0))
163 od.setWidth(getWidth()); // width = getWidth();
164 od.setHeight(getHeight()); // sequencesHeight = getHeight() - graphHeight;
167 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
169 Thread thread = new Thread(this);
179 if (av.isShowSequenceFeatures())
181 fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer());
184 // why do we need to set preferred size again? was set in
185 // updateOverviewImage
186 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
188 miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
189 BufferedImage.TYPE_INT_RGB);
191 Graphics mg = miniMe.getGraphics();
192 mg.setColor(Color.orange);
193 mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight());
195 // calculate sampleCol and sampleRow
196 // alignment width is max number of residues/bases
197 // alignment height is number of sequences
198 int alwidth = av.getAlignment().getWidth();
199 int alheight = av.getAlignment().getAbsoluteHeight();
201 // sampleCol or sampleRow is the width/height allocated to each residue
202 // in particular, sometimes we may need more than one row/col of the
203 // BufferedImage allocated
204 // sampleCol is how much of a residue to assign to each pixel
205 // sampleRow is how many sequences to assign to each pixel
206 float sampleCol = alwidth / (float) od.getWidth();
207 float sampleRow = alheight / (float) od.getSequencesHeight();
209 buildImage(sampleRow, sampleCol);
211 if (av.getAlignmentConservationAnnotation() != null)
213 renderer.updateFromAlignViewport(av);
214 for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
216 mg.translate(col, od.getSequencesHeight());
217 renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(),
218 av.getAlignmentConservationAnnotation().annotations,
219 (int) (sampleCol) + 1, od.getGraphHeight(),
220 (int) (col * sampleCol), (int) (col * sampleCol) + 1);
221 mg.translate(-col, -od.getSequencesHeight());
232 updateOverviewImage();
242 private void buildImage(float sampleRow, float sampleCol)
246 int color = Color.white.getRGB();
248 jalview.datamodel.SequenceI seq = null;
250 final boolean hasHiddenCols = av.hasHiddenColumns();
251 boolean hiddenRow = false;
252 // get hidden row and hidden column map once at beginning.
253 // clone featureRenderer settings to avoid race conditions... if state is
254 // updated just need to refresh again
255 for (int row = 0; row < od.getSequencesHeight() && !resizeAgain; row++)
257 boolean doCopy = true;
258 int currentrow = (int) (row * sampleRow);
259 if (currentrow != lastrow)
263 lastrow = currentrow;
265 // get the sequence which would be at alignment index 'lastrow' if no
266 // columns were hidden, and determine whether it is hidden or not
267 hiddenRow = av.getAlignment().isHidden(lastrow);
268 seq = av.getAlignment().getSequenceAtAbsoluteIndex(lastrow);
273 System.out.println(lastrow + " null");
277 for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
281 color = miniMe.getRGB(col, row - 1);
283 else if ((int) (col * sampleCol) != lastcol
284 || (int) (row * sampleRow) != lastrow)
286 lastcol = (int) (col * sampleCol);
287 color = getColumnColourFromSequence(seq, hiddenRow, hasHiddenCols,
290 // else if ((int) (col * sampleCol) == lastcol && (int) (row *
291 // sampleRow) == lastrow))
292 // we just use the color we already have , so don't need to set it
294 miniMe.setRGB(col, row, color);
300 * Find the colour of a sequence at a specified column position
302 private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
303 boolean hiddenRow, boolean hasHiddenCols, int lastcol)
307 if (seq.getLength() > lastcol)
309 color = sr.getResidueBoxColour(seq, lastcol).getRGB();
311 if (av.isShowSequenceFeatures())
313 color = fr.findFeatureColour(color, seq, lastcol);
318 color = Color.white.getRGB(); // White
322 || (hasHiddenCols && !av.getColumnSelection()
323 .isVisible(lastcol)))
325 color = new Color(color).darker().darker().getRGB();
332 * Update the overview panel box when the associated alignment panel is
336 public void setBoxPosition()
338 od.setBoxPosition(av.getAlignment()
339 .getHiddenSequences(), av.getColumnSelection(), av.getPosProps());
345 public void paintComponent(Graphics g)
347 if (resizing || resizeAgain)
349 if (lastMiniMe == null)
351 g.setColor(Color.white);
352 g.fillRect(0, 0, getWidth(), getHeight());
356 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
358 g.setColor(TRANS_GREY);
359 g.fillRect(0, 0, getWidth(), getHeight());
361 else if (lastMiniMe != null)
363 g.drawImage(lastMiniMe, 0, 0, this);
364 if (lastMiniMe != miniMe)
366 g.setColor(TRANS_GREY);
367 g.fillRect(0, 0, getWidth(), getHeight());
370 // TODO: render selected regions
371 g.setColor(Color.red);