Merge branch 'features/JAL-1553_improvement-select-column-by-annotation-row-query...
[jalview.git] / src / jalview / appletgui / SeqCanvas.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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.appletgui;
22
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.SearchResults;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27
28 import java.awt.Color;
29 import java.awt.FontMetrics;
30 import java.awt.Graphics;
31 import java.awt.Image;
32 import java.awt.Panel;
33
34 public class SeqCanvas extends Panel
35 {
36   FeatureRenderer fr;
37
38   SequenceRenderer sr;
39
40   Image img;
41
42   Graphics gg;
43
44   int imgWidth;
45
46   int imgHeight;
47
48   AlignViewport av;
49
50   SearchResults searchResults = null;
51
52   boolean fastPaint = false;
53
54   int cursorX = 0;
55
56   int cursorY = 0;
57
58   public SeqCanvas(AlignViewport av)
59   {
60     this.av = av;
61     fr = new FeatureRenderer(av);
62     sr = new SequenceRenderer(av);
63     PaintRefresher.Register(this, av.getSequenceSetId());
64   }
65
66   public AlignViewport getViewport()
67   {
68     return av;
69   }
70
71   public FeatureRenderer getFeatureRenderer()
72   {
73     return fr;
74   }
75
76   public SequenceRenderer getSequenceRenderer()
77   {
78     return sr;
79   }
80
81   void drawNorthScale(Graphics g, int startx, int endx, int ypos)
82   {
83     int scalestartx = startx - startx % 10 + 10;
84
85     g.setColor(Color.black);
86
87     // NORTH SCALE
88     for (int i = scalestartx; i < endx; i += 10)
89     {
90       int value = i;
91       if (av.hasHiddenColumns())
92       {
93         value = av.getColumnSelection().adjustForHiddenColumns(value);
94       }
95
96       g.drawString(String.valueOf(value), (i - startx - 1) * av.charWidth,
97               ypos - (av.charHeight / 2));
98
99       g.drawLine(((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
100               (ypos + 2) - (av.charHeight / 2),
101               ((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
102               ypos - 2);
103     }
104   }
105
106   void drawWestScale(Graphics g, int startx, int endx, int ypos)
107   {
108     FontMetrics fm = getFontMetrics(av.getFont());
109     ypos += av.charHeight;
110     if (av.hasHiddenColumns())
111     {
112       startx = av.getColumnSelection().adjustForHiddenColumns(startx);
113       endx = av.getColumnSelection().adjustForHiddenColumns(endx);
114     }
115
116     int maxwidth = av.getAlignment().getWidth();
117     if (av.hasHiddenColumns())
118     {
119       maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
120     }
121
122     // WEST SCALE
123     for (int i = 0; i < av.getAlignment().getHeight(); i++)
124     {
125       SequenceI seq = av.getAlignment().getSequenceAt(i);
126       int index = startx;
127       int value = -1;
128
129       while (index < endx)
130       {
131         if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
132         {
133           index++;
134
135           continue;
136         }
137
138         value = av.getAlignment().getSequenceAt(i).findPosition(index);
139
140         break;
141       }
142
143       if (value != -1)
144       {
145         int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
146                 - av.charWidth / 2;
147         g.drawString(value + "", x, (ypos + (i * av.charHeight))
148                 - (av.charHeight / 5));
149       }
150     }
151   }
152
153   void drawEastScale(Graphics g, int startx, int endx, int ypos)
154   {
155     ypos += av.charHeight;
156
157     if (av.hasHiddenColumns())
158     {
159       endx = av.getColumnSelection().adjustForHiddenColumns(endx);
160     }
161
162     SequenceI seq;
163     // EAST SCALE
164     for (int i = 0; i < av.getAlignment().getHeight(); i++)
165     {
166       seq = av.getAlignment().getSequenceAt(i);
167       int index = endx;
168       int value = -1;
169
170       while (index > startx)
171       {
172         if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
173         {
174           index--;
175
176           continue;
177         }
178
179         value = seq.findPosition(index);
180
181         break;
182       }
183
184       if (value != -1)
185       {
186         g.drawString(String.valueOf(value), 0, (ypos + (i * av.charHeight))
187                 - (av.charHeight / 5));
188       }
189     }
190   }
191
192   int lastsr = 0;
193
194   void fastPaint(int horizontal, int vertical)
195   {
196     if (fastPaint || gg == null)
197     {
198       return;
199     }
200
201     // Its possible on certain browsers that the call to fastpaint
202     // is faster than it can paint, so this check here catches
203     // this possibility
204     if (lastsr + horizontal != av.startRes)
205     {
206       horizontal = av.startRes - lastsr;
207     }
208
209     lastsr = av.startRes;
210
211     fastPaint = true;
212     gg.copyArea(horizontal * av.charWidth, vertical * av.charHeight,
213             imgWidth - horizontal * av.charWidth, imgHeight - vertical
214                     * av.charHeight, -horizontal * av.charWidth, -vertical
215                     * av.charHeight);
216
217     int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq, transX = 0, transY = 0;
218
219     if (horizontal > 0) // scrollbar pulled right, image to the left
220     {
221       transX = (er - sr - horizontal) * av.charWidth;
222       sr = er - horizontal;
223     }
224     else if (horizontal < 0)
225     {
226       er = sr - horizontal;
227     }
228
229     else if (vertical > 0) // scroll down
230     {
231       ss = es - vertical;
232       if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
233       {
234         ss = av.startSeq;
235       }
236       else
237       {
238         transY = imgHeight - vertical * av.charHeight;
239       }
240     }
241     else if (vertical < 0)
242     {
243       es = ss - vertical;
244       if (es > av.endSeq)
245       {
246         es = av.endSeq;
247       }
248     }
249
250     gg.translate(transX, transY);
251
252     drawPanel(gg, sr, er, ss, es, 0);
253     gg.translate(-transX, -transY);
254
255     repaint();
256
257   }
258
259   /**
260    * Definitions of startx and endx (hopefully): SMJS This is what I'm working
261    * towards! startx is the first residue (starting at 0) to display. endx is
262    * the last residue to display (starting at 0). starty is the first sequence
263    * to display (starting at 0). endy is the last sequence to display (starting
264    * at 0). NOTE 1: The av limits are set in setFont in this class and in the
265    * adjustment listener in SeqPanel when the scrollbars move.
266    */
267   public void update(Graphics g)
268   {
269     paint(g);
270   }
271
272   public void paint(Graphics g)
273   {
274
275     if (img != null
276             && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
277                     .getClipBounds().height)))
278     {
279       g.drawImage(img, 0, 0, this);
280       fastPaint = false;
281       return;
282     }
283
284     if (fastPaint)
285     {
286       g.drawImage(img, 0, 0, this);
287       fastPaint = false;
288       return;
289     }
290
291     // this draws the whole of the alignment
292     imgWidth = this.getSize().width;
293     imgHeight = this.getSize().height;
294
295     imgWidth -= imgWidth % av.charWidth;
296     imgHeight -= imgHeight % av.charHeight;
297
298     if (imgWidth < 1 || imgHeight < 1)
299     {
300       return;
301     }
302
303     if (img == null || imgWidth != img.getWidth(this)
304             || imgHeight != img.getHeight(this))
305     {
306       img = createImage(imgWidth, imgHeight);
307       gg = img.getGraphics();
308       gg.setFont(av.getFont());
309     }
310
311     gg.setColor(Color.white);
312     gg.fillRect(0, 0, imgWidth, imgHeight);
313
314     if (av.getWrapAlignment())
315     {
316       drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
317     }
318     else
319     {
320       drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
321     }
322
323     g.drawImage(img, 0, 0, this);
324
325   }
326
327   int LABEL_WEST, LABEL_EAST;
328
329   public int getWrappedCanvasWidth(int cwidth)
330   {
331     cwidth -= cwidth % av.charWidth;
332
333     FontMetrics fm = getFontMetrics(av.getFont());
334
335     LABEL_EAST = 0;
336     LABEL_WEST = 0;
337
338     if (av.scaleRightWrapped)
339     {
340       LABEL_EAST = fm.stringWidth(getMask());
341     }
342
343     if (av.scaleLeftWrapped)
344     {
345       LABEL_WEST = fm.stringWidth(getMask());
346     }
347
348     return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
349   }
350
351   /**
352    * Generates a string of zeroes.
353    * 
354    * @return String
355    */
356   String getMask()
357   {
358     String mask = "0";
359     int maxWidth = 0;
360     int tmp;
361     AlignmentI alignment = av.getAlignment();
362     for (int i = 0; i < alignment.getHeight(); i++)
363     {
364       tmp = alignment.getSequenceAt(i).getEnd();
365       if (tmp > maxWidth)
366       {
367         maxWidth = tmp;
368       }
369     }
370
371     for (int i = maxWidth; i > 0; i /= 10)
372     {
373       mask += "0";
374     }
375     return mask;
376   }
377
378   public void drawWrappedPanel(Graphics g, int canvasWidth,
379           int canvasHeight, int startRes)
380   {
381     AlignmentI al = av.getAlignment();
382
383     FontMetrics fm = getFontMetrics(av.getFont());
384
385     if (av.scaleRightWrapped)
386     {
387       LABEL_EAST = fm.stringWidth(getMask());
388     }
389
390     if (av.scaleLeftWrapped)
391     {
392       LABEL_WEST = fm.stringWidth(getMask());
393     }
394
395     int hgap = av.charHeight;
396     if (av.scaleAboveWrapped)
397     {
398       hgap += av.charHeight;
399     }
400
401     int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
402     int cHeight = av.getAlignment().getHeight() * av.charHeight;
403
404     av.setWrappedWidth(cWidth);
405
406     av.endRes = av.startRes + cWidth;
407
408     int endx;
409     int ypos = hgap;
410
411     int maxwidth = av.getAlignment().getWidth() - 1;
412
413     if (av.hasHiddenColumns())
414     {
415       maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
416     }
417
418     while ((ypos <= canvasHeight) && (startRes < maxwidth))
419     {
420       endx = startRes + cWidth - 1;
421
422       if (endx > maxwidth)
423       {
424         endx = maxwidth;
425       }
426
427       g.setColor(Color.black);
428
429       if (av.scaleLeftWrapped)
430       {
431         drawWestScale(g, startRes, endx, ypos);
432       }
433
434       if (av.scaleRightWrapped)
435       {
436         g.translate(canvasWidth - LABEL_EAST, 0);
437         drawEastScale(g, startRes, endx, ypos);
438         g.translate(-(canvasWidth - LABEL_EAST), 0);
439       }
440
441       g.translate(LABEL_WEST, 0);
442
443       if (av.scaleAboveWrapped)
444       {
445         drawNorthScale(g, startRes, endx, ypos);
446       }
447       if (av.hasHiddenColumns() && av.showHiddenMarkers)
448       {
449         g.setColor(Color.blue);
450         int res;
451         for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
452                 .size(); i++)
453         {
454           res = av.getColumnSelection().findHiddenRegionPosition(i)
455                   - startRes;
456
457           if (res < 0 || res > endx - startRes)
458           {
459             continue;
460           }
461
462           gg.fillPolygon(new int[]
463           { res * av.charWidth - av.charHeight / 4,
464               res * av.charWidth + av.charHeight / 4, res * av.charWidth },
465                   new int[]
466                   { ypos - (av.charHeight / 2), ypos - (av.charHeight / 2),
467                       ypos - (av.charHeight / 2) + 8 }, 3);
468
469         }
470       }
471
472       if (g.getClip() == null)
473       {
474         g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
475       }
476
477       drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
478       g.setClip(null);
479
480       if (av.showAnnotation)
481       {
482         g.translate(0, cHeight + ypos + 4);
483         if (annotations == null)
484         {
485           annotations = new AnnotationPanel(av);
486         }
487
488         annotations.drawComponent(g, startRes, endx + 1);
489         g.translate(0, -cHeight - ypos - 4);
490       }
491       g.translate(-LABEL_WEST, 0);
492
493       ypos += cHeight + getAnnotationHeight() + hgap;
494
495       startRes += cWidth;
496     }
497
498   }
499
500   AnnotationPanel annotations;
501
502   int getAnnotationHeight()
503   {
504     if (!av.showAnnotation)
505     {
506       return 0;
507     }
508
509     if (annotations == null)
510     {
511       annotations = new AnnotationPanel(av);
512     }
513
514     return annotations.adjustPanelHeight();
515   }
516
517   void drawPanel(Graphics g1, int startRes, int endRes, int startSeq,
518           int endSeq, int offset)
519   {
520
521
522     if (!av.hasHiddenColumns())
523     {
524       draw(g1, startRes, endRes, startSeq, endSeq, offset);
525     }
526     else
527     {
528
529
530
531       int screenY = 0;
532       int blockStart = startRes;
533       int blockEnd = endRes;
534
535       if (av.getColumnSelection() != null
536               && av.getColumnSelection().getHiddenColumns() != null)
537       {
538         java.util.Vector regions = av.getColumnSelection()
539                 .getHiddenColumns();
540
541       for (int i = 0; i < regions.size(); i++)
542       {
543         int[] region = (int[]) regions.elementAt(i);
544         int hideStart = region[0];
545         int hideEnd = region[1];
546
547         if (hideStart <= blockStart)
548         {
549           blockStart += (hideEnd - hideStart) + 1;
550           continue;
551         }
552
553         blockEnd = hideStart - 1;
554
555         g1.translate(screenY * av.charWidth, 0);
556
557         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
558
559         if (av.getShowHiddenMarkers())
560         {
561           g1.setColor(Color.blue);
562           g1.drawLine((blockEnd - blockStart + 1) * av.charWidth - 1,
563                   0 + offset, (blockEnd - blockStart + 1) * av.charWidth
564                           - 1, (endSeq - startSeq) * av.charHeight + offset);
565         }
566
567         g1.translate(-screenY * av.charWidth, 0);
568         screenY += blockEnd - blockStart + 1;
569         blockStart = hideEnd + 1;
570       }
571       }
572       if (screenY <= (endRes - startRes))
573       {
574         blockEnd = blockStart + (endRes - startRes) - screenY;
575         g1.translate(screenY * av.charWidth, 0);
576         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
577
578         g1.translate(-screenY * av.charWidth, 0);
579       }
580     }
581
582   }
583
584   // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
585   // int x1, int x2, int y1, int y2, int startx, int starty,
586   void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
587           int offset)
588   {
589     g.setFont(av.getFont());
590     sr.prepare(g, av.renderGaps);
591
592     SequenceI nextSeq;
593
594     // / First draw the sequences
595     // ///////////////////////////
596     for (int i = startSeq; i < endSeq; i++)
597     {
598       nextSeq = av.getAlignment().getSequenceAt(i);
599
600       if (nextSeq == null)
601       {
602         continue;
603       }
604
605       sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
606               startRes, endRes, offset + ((i - startSeq) * av.charHeight));
607
608       if (av.isShowSequenceFeatures())
609       {
610         fr.drawSequence(g, nextSeq, startRes, endRes, offset
611                 + ((i - startSeq) * av.charHeight));
612       }
613
614       // / Highlight search Results once all sequences have been drawn
615       // ////////////////////////////////////////////////////////
616       if (searchResults != null)
617       {
618         int[] visibleResults = searchResults.getResults(nextSeq, startRes,
619                 endRes);
620         if (visibleResults != null)
621         {
622           for (int r = 0; r < visibleResults.length; r += 2)
623           {
624             sr.drawHighlightedText(nextSeq, visibleResults[r],
625                     visibleResults[r + 1], (visibleResults[r] - startRes)
626                             * av.charWidth, offset
627                             + ((i - startSeq) * av.charHeight));
628           }
629         }
630       }
631
632       if (av.cursorMode && cursorY == i && cursorX >= startRes
633               && cursorX <= endRes)
634       {
635         sr.drawCursor(nextSeq, cursorX,
636                 (cursorX - startRes) * av.charWidth, offset
637                         + ((i - startSeq) * av.charHeight));
638       }
639     }
640
641     if (av.getSelectionGroup() != null
642             || av.getAlignment().getGroups().size() > 0)
643     {
644       drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
645     }
646
647   }
648
649   void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
650           int startSeq, int endSeq, int offset)
651   {
652     //
653     // ///////////////////////////////////
654     // Now outline any areas if necessary
655     // ///////////////////////////////////
656     SequenceGroup group = av.getSelectionGroup();
657
658     int sx = -1;
659     int sy = -1;
660     int ex = -1;
661     int groupIndex = -1;
662
663     if ((group == null) && (av.getAlignment().getGroups().size() > 0))
664     {
665       group = av.getAlignment().getGroups().get(0);
666       groupIndex = 0;
667     }
668
669     if (group != null)
670     {
671       do
672       {
673         int oldY = -1;
674         int i = 0;
675         boolean inGroup = false;
676         int top = -1;
677         int bottom = -1;
678         int alHeight = av.getAlignment().getHeight() - 1;
679
680         for (i = startSeq; i < endSeq; i++)
681         {
682           sx = (group.getStartRes() - startRes) * av.charWidth;
683           sy = offset + ((i - startSeq) * av.charHeight);
684           ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
685
686           if (sx + ex < 0 || sx > imgWidth)
687           {
688             continue;
689           }
690
691           if ((sx <= (endRes - startRes) * av.charWidth)
692                   && group.getSequences(null).contains(
693                           av.getAlignment().getSequenceAt(i)))
694           {
695             if ((bottom == -1)
696                     && (i >= alHeight || !group.getSequences(null)
697                             .contains(
698                                     av.getAlignment().getSequenceAt(i + 1))))
699             {
700               bottom = sy + av.charHeight;
701             }
702
703             if (!inGroup)
704             {
705               if (((top == -1) && (i == 0))
706                       || !group.getSequences(null).contains(
707                               av.getAlignment().getSequenceAt(i - 1)))
708               {
709                 top = sy;
710               }
711
712               oldY = sy;
713               inGroup = true;
714
715               if (group == av.getSelectionGroup())
716               {
717                 g.setColor(Color.red);
718               }
719               else
720               {
721                 g.setColor(group.getOutlineColour());
722               }
723             }
724           }
725           else
726           {
727             if (inGroup)
728             {
729               if (sx >= 0 && sx < imgWidth)
730               {
731                 g.drawLine(sx, oldY, sx, sy);
732               }
733
734               if (sx + ex < imgWidth)
735               {
736                 g.drawLine(sx + ex, oldY, sx + ex, sy);
737               }
738
739               if (sx < 0)
740               {
741                 ex += sx;
742                 sx = 0;
743               }
744
745               if (sx + ex > imgWidth)
746               {
747                 ex = imgWidth;
748               }
749
750               else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
751               {
752                 ex = (endRes - startRes + 1) * av.charWidth;
753               }
754
755               if (top != -1)
756               {
757                 g.drawLine(sx, top, sx + ex, top);
758                 top = -1;
759               }
760
761               if (bottom != -1)
762               {
763                 g.drawLine(sx, bottom, sx + ex, bottom);
764                 bottom = -1;
765               }
766
767               inGroup = false;
768             }
769           }
770         }
771
772         if (inGroup)
773         {
774           sy = offset + ((i - startSeq) * av.charHeight);
775           if (sx >= 0 && sx < imgWidth)
776           {
777             g.drawLine(sx, oldY, sx, sy);
778           }
779
780           if (sx + ex < imgWidth)
781           {
782             g.drawLine(sx + ex, oldY, sx + ex, sy);
783           }
784
785           if (sx < 0)
786           {
787             ex += sx;
788             sx = 0;
789           }
790
791           if (sx + ex > imgWidth)
792           {
793             ex = imgWidth;
794           }
795           else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
796           {
797             ex = (endRes - startRes + 1) * av.charWidth;
798           }
799
800           if (top != -1)
801           {
802             g.drawLine(sx, top, sx + ex, top);
803             top = -1;
804           }
805
806           if (bottom != -1)
807           {
808             g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
809             bottom = -1;
810           }
811
812           inGroup = false;
813         }
814
815         groupIndex++;
816
817         if (groupIndex >= av.getAlignment().getGroups().size())
818         {
819           break;
820         }
821
822         group = av.getAlignment().getGroups()
823                 .get(groupIndex);
824       } while (groupIndex < av.getAlignment().getGroups().size());
825
826     }
827   }
828
829   public void highlightSearchResults(SearchResults results)
830   {
831     searchResults = results;
832
833     repaint();
834   }
835
836 }