JAL-1628 tweak signature of core annotation printer for outputting hidden columns...
[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     if (!av.hasHiddenColumns())
521     {
522       draw(g1, startRes, endRes, startSeq, endSeq, offset);
523     }
524     else
525     {
526       java.util.List<int[]> regions = av.getColumnSelection()
527               .getHiddenColumns();
528
529       int screenY = 0;
530       int blockStart = startRes;
531       int blockEnd = endRes;
532
533       for (int i = 0; i < regions.size(); i++)
534       {
535         int[] region = regions.get(i);
536         int hideStart = region[0];
537         int hideEnd = region[1];
538
539         if (hideStart <= blockStart)
540         {
541           blockStart += (hideEnd - hideStart) + 1;
542           continue;
543         }
544
545         blockEnd = hideStart - 1;
546
547         g1.translate(screenY * av.charWidth, 0);
548
549         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
550
551         if (av.getShowHiddenMarkers())
552         {
553           g1.setColor(Color.blue);
554           g1.drawLine((blockEnd - blockStart + 1) * av.charWidth - 1,
555                   0 + offset, (blockEnd - blockStart + 1) * av.charWidth
556                           - 1, (endSeq - startSeq) * av.charHeight + offset);
557         }
558
559         g1.translate(-screenY * av.charWidth, 0);
560         screenY += blockEnd - blockStart + 1;
561         blockStart = hideEnd + 1;
562       }
563
564       if (screenY <= (endRes - startRes))
565       {
566         blockEnd = blockStart + (endRes - startRes) - screenY;
567         g1.translate(screenY * av.charWidth, 0);
568         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
569
570         g1.translate(-screenY * av.charWidth, 0);
571       }
572     }
573
574   }
575
576   // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
577   // int x1, int x2, int y1, int y2, int startx, int starty,
578   void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
579           int offset)
580   {
581     g.setFont(av.getFont());
582     sr.prepare(g, av.renderGaps);
583
584     SequenceI nextSeq;
585
586     // / First draw the sequences
587     // ///////////////////////////
588     for (int i = startSeq; i < endSeq; i++)
589     {
590       nextSeq = av.getAlignment().getSequenceAt(i);
591
592       if (nextSeq == null)
593       {
594         continue;
595       }
596
597       sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
598               startRes, endRes, offset + ((i - startSeq) * av.charHeight));
599
600       if (av.isShowSequenceFeatures())
601       {
602         fr.drawSequence(g, nextSeq, startRes, endRes, offset
603                 + ((i - startSeq) * av.charHeight));
604       }
605
606       // / Highlight search Results once all sequences have been drawn
607       // ////////////////////////////////////////////////////////
608       if (searchResults != null)
609       {
610         int[] visibleResults = searchResults.getResults(nextSeq, startRes,
611                 endRes);
612         if (visibleResults != null)
613         {
614           for (int r = 0; r < visibleResults.length; r += 2)
615           {
616             sr.drawHighlightedText(nextSeq, visibleResults[r],
617                     visibleResults[r + 1], (visibleResults[r] - startRes)
618                             * av.charWidth, offset
619                             + ((i - startSeq) * av.charHeight));
620           }
621         }
622       }
623
624       if (av.cursorMode && cursorY == i && cursorX >= startRes
625               && cursorX <= endRes)
626       {
627         sr.drawCursor(nextSeq, cursorX,
628                 (cursorX - startRes) * av.charWidth, offset
629                         + ((i - startSeq) * av.charHeight));
630       }
631     }
632
633     if (av.getSelectionGroup() != null
634             || av.getAlignment().getGroups().size() > 0)
635     {
636       drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
637     }
638
639   }
640
641   void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
642           int startSeq, int endSeq, int offset)
643   {
644     //
645     // ///////////////////////////////////
646     // Now outline any areas if necessary
647     // ///////////////////////////////////
648     SequenceGroup group = av.getSelectionGroup();
649
650     int sx = -1;
651     int sy = -1;
652     int ex = -1;
653     int groupIndex = -1;
654
655     if ((group == null) && (av.getAlignment().getGroups().size() > 0))
656     {
657       group = av.getAlignment().getGroups().get(0);
658       groupIndex = 0;
659     }
660
661     if (group != null)
662     {
663       do
664       {
665         int oldY = -1;
666         int i = 0;
667         boolean inGroup = false;
668         int top = -1;
669         int bottom = -1;
670         int alHeight = av.getAlignment().getHeight() - 1;
671
672         for (i = startSeq; i < endSeq; i++)
673         {
674           sx = (group.getStartRes() - startRes) * av.charWidth;
675           sy = offset + ((i - startSeq) * av.charHeight);
676           ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
677
678           if (sx + ex < 0 || sx > imgWidth)
679           {
680             continue;
681           }
682
683           if ((sx <= (endRes - startRes) * av.charWidth)
684                   && group.getSequences(null).contains(
685                           av.getAlignment().getSequenceAt(i)))
686           {
687             if ((bottom == -1)
688                     && (i >= alHeight || !group.getSequences(null)
689                             .contains(
690                                     av.getAlignment().getSequenceAt(i + 1))))
691             {
692               bottom = sy + av.charHeight;
693             }
694
695             if (!inGroup)
696             {
697               if (((top == -1) && (i == 0))
698                       || !group.getSequences(null).contains(
699                               av.getAlignment().getSequenceAt(i - 1)))
700               {
701                 top = sy;
702               }
703
704               oldY = sy;
705               inGroup = true;
706
707               if (group == av.getSelectionGroup())
708               {
709                 g.setColor(Color.red);
710               }
711               else
712               {
713                 g.setColor(group.getOutlineColour());
714               }
715             }
716           }
717           else
718           {
719             if (inGroup)
720             {
721               if (sx >= 0 && sx < imgWidth)
722               {
723                 g.drawLine(sx, oldY, sx, sy);
724               }
725
726               if (sx + ex < imgWidth)
727               {
728                 g.drawLine(sx + ex, oldY, sx + ex, sy);
729               }
730
731               if (sx < 0)
732               {
733                 ex += sx;
734                 sx = 0;
735               }
736
737               if (sx + ex > imgWidth)
738               {
739                 ex = imgWidth;
740               }
741
742               else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
743               {
744                 ex = (endRes - startRes + 1) * av.charWidth;
745               }
746
747               if (top != -1)
748               {
749                 g.drawLine(sx, top, sx + ex, top);
750                 top = -1;
751               }
752
753               if (bottom != -1)
754               {
755                 g.drawLine(sx, bottom, sx + ex, bottom);
756                 bottom = -1;
757               }
758
759               inGroup = false;
760             }
761           }
762         }
763
764         if (inGroup)
765         {
766           sy = offset + ((i - startSeq) * av.charHeight);
767           if (sx >= 0 && sx < imgWidth)
768           {
769             g.drawLine(sx, oldY, sx, sy);
770           }
771
772           if (sx + ex < imgWidth)
773           {
774             g.drawLine(sx + ex, oldY, sx + ex, sy);
775           }
776
777           if (sx < 0)
778           {
779             ex += sx;
780             sx = 0;
781           }
782
783           if (sx + ex > imgWidth)
784           {
785             ex = imgWidth;
786           }
787           else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
788           {
789             ex = (endRes - startRes + 1) * av.charWidth;
790           }
791
792           if (top != -1)
793           {
794             g.drawLine(sx, top, sx + ex, top);
795             top = -1;
796           }
797
798           if (bottom != -1)
799           {
800             g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
801             bottom = -1;
802           }
803
804           inGroup = false;
805         }
806
807         groupIndex++;
808
809         if (groupIndex >= av.getAlignment().getGroups().size())
810         {
811           break;
812         }
813
814         group = av.getAlignment().getGroups()
815                 .get(groupIndex);
816       } while (groupIndex < av.getAlignment().getGroups().size());
817
818     }
819   }
820
821   public void highlightSearchResults(SearchResults results)
822   {
823     searchResults = results;
824
825     repaint();
826   }
827
828 }