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