923616f86ff15dbe17e4a21c9d060313d7400736
[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.renderer.AnnotationRenderer;
24 import jalview.viewmodel.OverviewDimensions;
25
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;
35
36 import javax.swing.JPanel;
37
38 /**
39  * Panel displaying an overview of the full alignment, with an interactive box
40  * representing the viewport onto the alignment.
41  * 
42  * @author $author$
43  * @version $Revision$
44  */
45 public class OverviewPanel extends JPanel implements Runnable
46 {
47   private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
48
49   private final AnnotationRenderer renderer = new AnnotationRenderer();
50
51   private OverviewDimensions od;
52
53   private BufferedImage miniMe;
54
55   private BufferedImage lastMiniMe = null;
56
57   private AlignViewport av;
58
59   private AlignmentPanel ap;
60
61   //
62   private boolean resizing = false;
63
64   // This is set true if the user resizes whilst
65   // the overview is being calculated
66   private boolean resizeAgain = false;
67
68   // Can set different properties in this seqCanvas than
69   // main visible SeqCanvas
70   private SequenceRenderer sr;
71
72   private jalview.renderer.seqfeatures.FeatureRenderer fr;
73
74   /**
75    * Creates a new OverviewPanel object.
76    * 
77    * @param alPanel
78    *          The alignment panel which is shown in the overview panel
79    */
80   public OverviewPanel(AlignmentPanel alPanel)
81   {
82     this.av = alPanel.av;
83     this.ap = alPanel;
84     setLayout(null);
85
86     sr = new SequenceRenderer(av);
87     sr.renderGaps = false;
88     sr.forOverview = true;
89     fr = new FeatureRenderer(alPanel);
90
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)
95     {
96       showAnnotation = false;
97     }
98     od = new OverviewDimensions(av.getPosProps(), showAnnotation);
99
100     addComponentListener(new ComponentAdapter()
101     {
102       @Override
103       public void componentResized(ComponentEvent evt)
104       {
105         if ((getWidth() != od.getWidth())
106                 || (getHeight() != (od.getHeight())))
107         {
108           updateOverviewImage();
109         }
110       }
111     });
112
113     addMouseMotionListener(new MouseMotionAdapter()
114     {
115       @Override
116       public void mouseDragged(MouseEvent evt)
117       {
118         if (!av.getWrapAlignment())
119         {
120           // TODO: feature: jv2.5 detect shift drag and update selection from
121           // it.
122           od.updateViewportFromMouse(evt.getX(), evt.getY(), av
123                   .getAlignment().getHiddenSequences(), av
124                   .getColumnSelection(), av.getPosProps());
125           ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
126         }
127       }
128     });
129
130     addMouseListener(new MouseAdapter()
131     {
132       @Override
133       public void mousePressed(MouseEvent evt)
134       {
135         if (!av.getWrapAlignment())
136         {
137           od.updateViewportFromMouse(evt.getX(), evt.getY(), av
138                   .getAlignment().getHiddenSequences(), av
139                   .getColumnSelection(), av.getPosProps());
140           ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
141         }
142       }
143     });
144
145     updateOverviewImage();
146   }
147
148   /**
149    * Updates the overview image when the related alignment panel is updated
150    */
151   public void updateOverviewImage()
152   {
153     if (resizing)
154     {
155       resizeAgain = true;
156       return;
157     }
158
159     resizing = true;
160
161     if ((getWidth() > 0) && (getHeight() > 0))
162     {
163       od.setWidth(getWidth()); // width = getWidth();
164       od.setHeight(getHeight()); // sequencesHeight = getHeight() - graphHeight;
165     }
166
167     setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
168
169     Thread thread = new Thread(this);
170     thread.start();
171     repaint();
172   }
173
174   @Override
175   public void run()
176   {
177     miniMe = null;
178
179     if (av.isShowSequenceFeatures())
180     {
181       fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer());
182     }
183
184     // why do we need to set preferred size again? was set in
185     // updateOverviewImage
186     setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
187
188     miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
189             BufferedImage.TYPE_INT_RGB);
190
191     Graphics mg = miniMe.getGraphics();
192     mg.setColor(Color.orange);
193     mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight());
194
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();
200
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();
208
209     buildImage(sampleRow, sampleCol);
210
211     if (av.getAlignmentConservationAnnotation() != null)
212     {
213       renderer.updateFromAlignViewport(av);
214       for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
215       {
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());
222
223       }
224     }
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   private void buildImage(float sampleRow, float sampleCol)
243   {
244     int lastcol = -1;
245     int lastrow = -1;
246     int color = Color.white.getRGB();
247
248     jalview.datamodel.SequenceI seq = null;
249
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++)
256     {
257       boolean doCopy = true;
258       int currentrow = (int) (row * sampleRow);
259       if (currentrow != lastrow)
260       {
261         doCopy = false;
262
263         lastrow = currentrow;
264
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);
269       }
270
271       if (seq == null)
272       {
273         System.out.println(lastrow + " null");
274         continue;
275       }
276
277       for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
278       {
279         if (doCopy)
280         {
281           color = miniMe.getRGB(col, row - 1);
282         }
283         else if ((int) (col * sampleCol) != lastcol
284                 || (int) (row * sampleRow) != lastrow)
285         {
286           lastcol = (int) (col * sampleCol);
287           color = getColumnColourFromSequence(seq, hiddenRow, hasHiddenCols,
288                   lastcol);
289         }
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
293
294         miniMe.setRGB(col, row, color);
295       }
296     }
297   }
298
299   /*
300    * Find the colour of a sequence at a specified column position
301    */
302   private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
303           boolean hiddenRow, boolean hasHiddenCols, int lastcol)
304   {
305     int color;
306
307     if (seq.getLength() > lastcol)
308     {
309       color = sr.getResidueBoxColour(seq, lastcol).getRGB();
310
311       if (av.isShowSequenceFeatures())
312       {
313         color = fr.findFeatureColour(color, seq, lastcol);
314       }
315     }
316     else
317     {
318       color = Color.white.getRGB(); // White
319     }
320
321     if (hiddenRow
322             || (hasHiddenCols && !av.getColumnSelection()
323                     .isVisible(lastcol)))
324     {
325       color = new Color(color).darker().darker().getRGB();
326     }
327
328     return color;
329   }
330
331   /**
332    * Update the overview panel box when the associated alignment panel is
333    * changed
334    * 
335    */
336   public void setBoxPosition()
337   {
338     od.setBoxPosition(av.getAlignment()
339             .getHiddenSequences(), av.getColumnSelection(), av.getPosProps());
340     repaint();
341   }
342
343
344   @Override
345   public void paintComponent(Graphics g)
346   {
347     if (resizing || resizeAgain)
348     {
349       if (lastMiniMe == null)
350       {
351         g.setColor(Color.white);
352         g.fillRect(0, 0, getWidth(), getHeight());
353       }
354       else
355       {
356         g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
357       }
358       g.setColor(TRANS_GREY);
359       g.fillRect(0, 0, getWidth(), getHeight());
360     }
361     else if (lastMiniMe != null)
362     {
363       g.drawImage(lastMiniMe, 0, 0, this);
364       if (lastMiniMe != miniMe)
365       {
366         g.setColor(TRANS_GREY);
367         g.fillRect(0, 0, getWidth(), getHeight());
368       }
369     }
370     // TODO: render selected regions
371     g.setColor(Color.red);
372     od.drawBox(g);
373   }
374 }