Merge branch 'develop' into features/JAL-2446NCList
[jalview.git] / src / jalview / gui / OverviewPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.gui;
22
23 import jalview.datamodel.SequenceI;
24 import jalview.renderer.AnnotationRenderer;
25 import jalview.renderer.seqfeatures.FeatureColourFinder;
26 import jalview.viewmodel.OverviewDimensions;
27
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;
37
38 import javax.swing.JPanel;
39
40 /**
41  * Panel displaying an overview of the full alignment, with an interactive box
42  * representing the viewport onto the alignment.
43  * 
44  * @author $author$
45  * @version $Revision$
46  */
47 public class OverviewPanel extends JPanel implements Runnable
48 {
49   private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
50
51   private final AnnotationRenderer renderer = new AnnotationRenderer();
52
53   private OverviewDimensions od;
54
55   private BufferedImage miniMe;
56
57   private BufferedImage lastMiniMe = null;
58
59   private AlignViewport av;
60
61   private AlignmentPanel ap;
62
63   //
64   private boolean resizing = false;
65
66   // This is set true if the user resizes whilst
67   // the overview is being calculated
68   private boolean resizeAgain = false;
69
70   // Can set different properties in this seqCanvas than
71   // main visible SeqCanvas
72   private SequenceRenderer sr;
73
74   jalview.renderer.seqfeatures.FeatureRenderer fr;
75
76   /**
77    * Creates a new OverviewPanel object.
78    * 
79    * @param alPanel
80    *          The alignment panel which is shown in the overview panel
81    */
82   public OverviewPanel(AlignmentPanel alPanel)
83   {
84     this.av = alPanel.av;
85     this.ap = alPanel;
86     setLayout(null);
87
88     sr = new SequenceRenderer(av);
89     sr.renderGaps = false;
90     sr.forOverview = true;
91     fr = new FeatureRenderer(ap);
92
93     od = new OverviewDimensions(av.getRanges(),
94             (av.isShowAnnotation() && av
95                     .getAlignmentConservationAnnotation() != null));
96
97     addComponentListener(new ComponentAdapter()
98     {
99       @Override
100       public void componentResized(ComponentEvent evt)
101       {
102         if ((getWidth() != od.getWidth())
103                 || (getHeight() != (od.getHeight())))
104         {
105           updateOverviewImage();
106         }
107       }
108     });
109
110     addMouseMotionListener(new MouseMotionAdapter()
111     {
112       @Override
113       public void mouseDragged(MouseEvent evt)
114       {
115         if (!av.getWrapAlignment())
116         {
117           od.updateViewportFromMouse(evt.getX(), evt.getY(), av
118                   .getAlignment().getHiddenSequences(), av
119                   .getColumnSelection(), av.getRanges());
120           ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
121         }
122       }
123     });
124
125     addMouseListener(new MouseAdapter()
126     {
127       @Override
128       public void mousePressed(MouseEvent evt)
129       {
130         if (!av.getWrapAlignment())
131         {
132           od.updateViewportFromMouse(evt.getX(), evt.getY(), av
133                   .getAlignment().getHiddenSequences(), av
134                   .getColumnSelection(), av.getRanges());
135           ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
136         }
137       }
138     });
139
140     updateOverviewImage();
141   }
142
143   /**
144    * Updates the overview image when the related alignment panel is updated
145    */
146   public void updateOverviewImage()
147   {
148     if (resizing)
149     {
150       resizeAgain = true;
151       return;
152     }
153
154     resizing = true;
155
156     if ((getWidth() > 0) && (getHeight() > 0))
157     {
158       od.setWidth(getWidth());
159       od.setHeight(getHeight());
160     }
161
162     setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
163
164     Thread thread = new Thread(this);
165     thread.start();
166     repaint();
167   }
168
169   @Override
170   public void run()
171   {
172     miniMe = null;
173
174     if (av.isShowSequenceFeatures())
175     {
176       fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer());
177     }
178
179     // why do we need to set preferred size again? was set in
180     // updateOverviewImage
181     setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
182
183     miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
184             BufferedImage.TYPE_INT_RGB);
185
186     Graphics mg = miniMe.getGraphics();
187     mg.setColor(Color.orange);
188     mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight());
189
190     // calculate sampleCol and sampleRow
191     // alignment width is max number of residues/bases
192     // alignment height is number of sequences
193     int alwidth = av.getAlignment().getWidth();
194     int alheight = av.getAlignment().getAbsoluteHeight();
195
196     // sampleCol or sampleRow is the width/height allocated to each residue
197     // in particular, sometimes we may need more than one row/col of the
198     // BufferedImage allocated
199     // sampleCol is how much of a residue to assign to each pixel
200     // sampleRow is how many sequences to assign to each pixel
201     float sampleCol = alwidth / (float) od.getWidth();
202     float sampleRow = alheight / (float) od.getSequencesHeight();
203
204     long start = System.currentTimeMillis();
205     buildImage(sampleRow, sampleCol);
206
207     // check for conservation annotation to make sure overview works for DNA too
208     if (av.isShowAnnotation()
209             && (av.getAlignmentConservationAnnotation() != null))
210     {
211       renderer.updateFromAlignViewport(av);
212       for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
213       {
214         mg.translate(col, od.getSequencesHeight());
215         renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(),
216                 av.getAlignmentConservationAnnotation().annotations,
217                 (int) (sampleCol) + 1, od.getGraphHeight(),
218                 (int) (col * sampleCol), (int) (col * sampleCol) + 1);
219         mg.translate(-col, -od.getSequencesHeight());
220
221       }
222     }
223     System.out.println("Overview took "
224             + (System.currentTimeMillis() - start) + "ms");
225     System.gc();
226
227     resizing = false;
228
229     if (resizeAgain)
230     {
231       resizeAgain = false;
232       updateOverviewImage();
233     }
234     else
235     {
236       lastMiniMe = miniMe;
237     }
238
239     setBoxPosition();
240   }
241
242   /*
243    * Build the overview panel image
244    */
245   private void buildImage(float sampleRow, float sampleCol)
246   {
247     int lastcol = -1;
248     int lastrow = -1;
249     int rgbColour = Color.white.getRGB();
250
251     SequenceI seq = null;
252     FeatureColourFinder finder = new FeatureColourFinder(fr);
253
254     final boolean hasHiddenCols = av.hasHiddenColumns();
255     boolean hiddenRow = false;
256     // get hidden row and hidden column map once at beginning.
257     // clone featureRenderer settings to avoid race conditions... if state is
258     // updated just need to refresh again
259     for (int row = 0; row < od.getSequencesHeight() && !resizeAgain; row++)
260     {
261       boolean doCopy = true;
262       int currentrow = (int) (row * sampleRow);
263       if (currentrow != lastrow)
264       {
265         doCopy = false;
266
267         lastrow = currentrow;
268
269         // get the sequence which would be at alignment index 'lastrow' if no
270         // rows were hidden, and determine whether it is hidden or not
271         hiddenRow = av.getAlignment().isHidden(lastrow);
272         seq = av.getAlignment().getSequenceAtAbsoluteIndex(lastrow);
273       }
274
275       for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
276       {
277         if (doCopy)
278         {
279           rgbColour = miniMe.getRGB(col, row - 1);
280         }
281         else if ((int) (col * sampleCol) != lastcol
282                 || (int) (row * sampleRow) != lastrow)
283         {
284           lastcol = (int) (col * sampleCol);
285           rgbColour = getColumnColourFromSequence(seq, hiddenRow,
286                   hasHiddenCols, lastcol, finder);
287         }
288         // else we just use the color we already have , so don't need to set it
289
290         miniMe.setRGB(col, row, rgbColour);
291       }
292     }
293   }
294
295   /*
296    * Find the colour of a sequence at a specified column position
297    */
298   private int getColumnColourFromSequence(
299           jalview.datamodel.SequenceI seq,
300           boolean hiddenRow, boolean hasHiddenCols, int lastcol,
301           FeatureColourFinder finder)
302   {
303     Color color = Color.white;
304
305     if ((seq != null) && (seq.getLength() > lastcol))
306     {
307         color = sr.getResidueColour(seq, lastcol, finder);
308     }
309
310     if (hiddenRow
311             || (hasHiddenCols && !av.getColumnSelection()
312                     .isVisible(lastcol)))
313     {
314       color = color.darker().darker();
315     }
316
317     return color.getRGB();
318   }
319
320   /**
321    * Update the overview panel box when the associated alignment panel is
322    * changed
323    * 
324    */
325   public void setBoxPosition()
326   {
327     od.setBoxPosition(av.getAlignment()
328             .getHiddenSequences(), av.getColumnSelection(), av.getRanges());
329     repaint();
330   }
331
332
333   @Override
334   public void paintComponent(Graphics g)
335   {
336     if (resizing || resizeAgain)
337     {
338       if (lastMiniMe == null)
339       {
340         g.setColor(Color.white);
341         g.fillRect(0, 0, getWidth(), getHeight());
342       }
343       else
344       {
345         g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
346       }
347       g.setColor(TRANS_GREY);
348       g.fillRect(0, 0, getWidth(), getHeight());
349     }
350     else if (lastMiniMe != null)
351     {
352       g.drawImage(lastMiniMe, 0, 0, this);
353       if (lastMiniMe != miniMe)
354       {
355         g.setColor(TRANS_GREY);
356         g.fillRect(0, 0, getWidth(), getHeight());
357       }
358     }
359
360     g.setColor(Color.red);
361     od.drawBox(g);
362   }
363 }