JAL-1620 version bump and release notes
[jalview.git] / src / jalview / renderer / AnnotationRenderer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
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.renderer;
22
23 import jalview.analysis.AAFrequency;
24 import jalview.analysis.StructureFrequency;
25 import jalview.api.AlignViewportI;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.Annotation;
28 import jalview.datamodel.ColumnSelection;
29 import jalview.schemes.ColourSchemeI;
30 import jalview.schemes.ResidueProperties;
31
32 import java.awt.BasicStroke;
33 import java.awt.Color;
34 import java.awt.Font;
35 import java.awt.FontMetrics;
36 import java.awt.Graphics;
37 import java.awt.Graphics2D;
38 import java.awt.Image;
39 import java.awt.font.LineMetrics;
40 import java.awt.geom.AffineTransform;
41 import java.awt.image.ImageObserver;
42 import java.util.BitSet;
43 import java.util.Hashtable;
44
45 import com.stevesoft.pat.Regex;
46
47 public class AnnotationRenderer
48 {
49   /**
50    * flag indicating if timing and redraw parameter info should be output
51    */
52   private final boolean debugRedraw;
53
54   public AnnotationRenderer()
55   {
56     this(false);
57   }
58
59   /**
60    * Create a new annotation Renderer
61    * 
62    * @param debugRedraw
63    *          flag indicating if timing and redraw parameter info should be
64    *          output
65    */
66   public AnnotationRenderer(boolean debugRedraw)
67   {
68     this.debugRedraw = debugRedraw;
69   }
70
71   public void drawStemAnnot(Graphics g, Annotation[] row_annotations,
72           int lastSSX, int x, int y, int iconOffset, int startRes,
73           int column, boolean validRes, boolean validEnd)
74   {
75     g.setColor(STEM_COLOUR);
76     int sCol = (lastSSX / charWidth) + startRes;
77     int x1 = lastSSX;
78     int x2 = (x * charWidth);
79     Regex closeparen = new Regex("(\\))");
80
81     char dc = (column == 0 || row_annotations[column - 1] == null) ? ' '
82             : row_annotations[column - 1].secondaryStructure;
83
84     boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
85             || dc != row_annotations[sCol - 1].secondaryStructure;
86     boolean diffdownstream = !validRes || !validEnd
87             || row_annotations[column] == null
88             || dc != row_annotations[column].secondaryStructure;
89     // System.out.println("Column "+column+" diff up: "+diffupstream+" down:"+diffdownstream);
90     // If a closing base pair half of the stem, display a backward arrow
91     if (column > 0 && ResidueProperties.isCloseParenRNA(dc))
92     {
93
94       if (diffupstream)
95       // if (validRes && column>1 && row_annotations[column-2]!=null &&
96       // dc.equals(row_annotations[column-2].displayCharacter))
97       {
98         g.fillPolygon(new int[]
99         { lastSSX + 5, lastSSX + 5, lastSSX }, new int[]
100         { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
101         x1 += 5;
102       }
103       if (diffdownstream)
104       {
105         x2 -= 1;
106       }
107     }
108     else
109     {
110
111       // display a forward arrow
112       if (diffdownstream)
113       {
114         g.fillPolygon(new int[]
115         { x2 - 5, x2 - 5, x2 }, new int[]
116         { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
117         x2 -= 5;
118       }
119       if (diffupstream)
120       {
121         x1 += 1;
122       }
123     }
124     // draw arrow body
125     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
126   }
127
128   private int charWidth, endRes, charHeight;
129
130   private boolean validCharWidth, hasHiddenColumns;
131
132   private FontMetrics fm;
133
134   private final boolean MAC = new jalview.util.Platform().isAMac();
135
136   boolean av_renderHistogram = true, av_renderProfile = true,
137           av_normaliseProfile = false;
138
139   ColourSchemeI profcolour = null;
140
141   private ColumnSelection columnSelection;
142
143   private Hashtable[] hconsensus;
144
145   private Hashtable[] hStrucConsensus;
146
147   private boolean av_ignoreGapsConsensus;
148
149   /**
150    * attributes set from AwtRenderPanelI
151    */
152   /**
153    * old image used when data is currently being calculated and cannot be
154    * rendered
155    */
156   private Image fadedImage;
157
158   /**
159    * panel being rendered into
160    */
161   private ImageObserver annotationPanel;
162
163   /**
164    * width of image to render in panel
165    */
166   private int imgWidth;
167
168   /**
169    * offset to beginning of visible area
170    */
171   private int sOffset;
172
173   /**
174    * offset to end of visible area
175    */
176   private int visHeight;
177
178   /**
179    * indicate if the renderer should only render the visible portion of the
180    * annotation given the current view settings
181    */
182   private boolean useClip = true;
183
184   /**
185    * master flag indicating if renderer should ever try to clip. not enabled for
186    * jalview 2.8.1
187    */
188   private boolean canClip = false;
189
190   public void drawNotCanonicalAnnot(Graphics g, Color nonCanColor,
191           Annotation[] row_annotations, int lastSSX, int x, int y,
192           int iconOffset, int startRes, int column, boolean validRes,
193           boolean validEnd)
194   {
195     // System.out.println(nonCanColor);
196
197     g.setColor(nonCanColor);
198     int sCol = (lastSSX / charWidth) + startRes;
199     int x1 = lastSSX;
200     int x2 = (x * charWidth);
201     Regex closeparen = new Regex("}|]|<|[a-z]");
202
203     String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
204             : row_annotations[column - 1].displayCharacter;
205
206     boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
207             || !dc.equals(row_annotations[sCol - 1].displayCharacter);
208     boolean diffdownstream = !validRes || !validEnd
209             || row_annotations[column] == null
210             || !dc.equals(row_annotations[column].displayCharacter);
211     // System.out.println("Column "+column+" diff up: "+diffupstream+" down:"+diffdownstream);
212     // If a closing base pair half of the stem, display a backward arrow
213     if (column > 0 && closeparen.search(dc))// closeletter_b.search(dc)||closeletter_c.search(dc)||closeletter_d.search(dc)||closecrochet.search(dc))
214                                             // )
215     {
216
217       if (diffupstream)
218       // if (validRes && column>1 && row_annotations[column-2]!=null &&
219       // dc.equals(row_annotations[column-2].displayCharacter))
220       {
221         g.fillPolygon(new int[]
222         { lastSSX + 5, lastSSX + 5, lastSSX }, new int[]
223         { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
224         x1 += 5;
225       }
226       if (diffdownstream)
227       {
228         x2 -= 1;
229       }
230     }
231     else
232     {
233
234       // display a forward arrow
235       if (diffdownstream)
236       {
237         g.fillPolygon(new int[]
238         { x2 - 5, x2 - 5, x2 }, new int[]
239         { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
240         x2 -= 5;
241       }
242       if (diffupstream)
243       {
244         x1 += 1;
245       }
246     }
247     // draw arrow body
248     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
249   }
250
251   // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
252   // av)
253   public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
254           AlignViewportI av)
255   {
256     fm = annotPanel.getFontMetrics();
257     annotationPanel = annotPanel;
258     fadedImage = annotPanel.getFadedImage();
259     imgWidth = annotPanel.getFadedImageWidth();
260     // visible area for rendering
261     int[] bounds = annotPanel.getVisibleVRange();
262     if (bounds != null)
263     {
264       sOffset = bounds[0];
265       visHeight = bounds[1];
266       if (visHeight == 0)
267       {
268         useClip = false;
269       }
270       else
271       {
272         useClip = canClip;
273       }
274     }
275     else
276     {
277       useClip = false;
278     }
279
280     updateFromAlignViewport(av);
281   }
282
283   public void updateFromAlignViewport(AlignViewportI av)
284   {
285     charWidth = av.getCharWidth();
286     endRes = av.getEndRes();
287     charHeight = av.getCharHeight();
288     hasHiddenColumns = av.hasHiddenColumns();
289     validCharWidth = av.isValidCharWidth();
290     av_renderHistogram = av.isShowConsensusHistogram();
291     av_renderProfile = av.isShowSequenceLogo();
292     av_normaliseProfile = av.isNormaliseSequenceLogo();
293     profcolour = av.getGlobalColourScheme();
294     if (profcolour == null)
295     {
296       // Set the default colour for sequence logo if the alignnent has no
297       // colourscheme set
298       profcolour = av.getAlignment().isNucleotide() ? new jalview.schemes.NucleotideColourScheme()
299               : new jalview.schemes.ZappoColourScheme();
300     }
301     boolean rna = av.getAlignment().isNucleotide();
302     columnSelection = av.getColumnSelection();
303     hconsensus = av.getSequenceConsensusHash();// hconsensus;
304     hStrucConsensus = av.getRnaStructureConsensusHash(); // hStrucConsensus;
305     av_ignoreGapsConsensus = av.getIgnoreGapsConsensus();
306   }
307
308   public int[] getProfileFor(AlignmentAnnotation aa, int column)
309   {
310     // TODO : consider refactoring the global alignment calculation
311     // properties/rendering attributes as a global 'alignment group' which holds
312     // all vis settings for the alignment as a whole rather than a subset
313     //
314     if (aa.autoCalculated && aa.label.startsWith("Consensus"))
315     {
316       if (aa.groupRef != null && aa.groupRef.consensusData != null
317               && aa.groupRef.isShowSequenceLogo())
318       {
319         return AAFrequency.extractProfile(
320                 aa.groupRef.consensusData[column],
321                 aa.groupRef.getIgnoreGapsConsensus());
322       }
323       // TODO extend annotation row to enable dynamic and static profile data to
324       // be stored
325       if (aa.groupRef == null && aa.sequenceRef == null)
326       {
327         return AAFrequency.extractProfile(hconsensus[column],
328                 av_ignoreGapsConsensus);
329       }
330     }
331     else
332     {
333       if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
334       {
335         // TODO implement group structure consensus
336         /*
337          * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
338          * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
339          * group selections return StructureFrequency.extractProfile(
340          * aa.groupRef.consensusData[column], aa.groupRef
341          * .getIgnoreGapsConsensus()); }
342          */
343         // TODO extend annotation row to enable dynamic and static profile data
344         // to
345         // be stored
346         if (aa.groupRef == null && aa.sequenceRef == null
347                 && hStrucConsensus != null
348                 && hStrucConsensus.length > column)
349         {
350           return StructureFrequency.extractProfile(hStrucConsensus[column],
351                   av_ignoreGapsConsensus);
352         }
353       }
354     }
355     return null;
356   }
357
358   boolean rna = false;
359
360   /**
361    * Render the annotation rows associated with an alignment.
362    * 
363    * @param annotPanel
364    *          container frame
365    * @param av
366    *          data and view settings to render
367    * @param g
368    *          destination for graphics
369    * @param activeRow
370    *          row where a mouse event occured (or -1)
371    * @param startRes
372    *          first column that will be drawn
373    * @param endRes
374    *          last column that will be drawn
375    * @return true if the fadedImage was used for any alignment annotation rows
376    *         currently being calculated
377    */
378   public boolean drawComponent(AwtRenderPanelI annotPanel,
379           AlignViewportI av, Graphics g, int activeRow, int startRes,
380           int endRes)
381   {
382     long stime = System.currentTimeMillis();
383     boolean usedFaded = false;
384     // NOTES:
385     // AnnotationPanel needs to implement: ImageObserver, access to
386     // AlignViewport
387     updateFromAwtRenderPanel(annotPanel, av);
388     fm = g.getFontMetrics();
389     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
390     int temp = 0;
391     if (aa == null)
392     {
393       return false;
394     }
395     int x = 0, y = 0;
396     int column = 0;
397     char lastSS;
398     int lastSSX;
399     int iconOffset = 0;
400     boolean validRes = false;
401     boolean validEnd = false;
402     boolean labelAllCols = false;
403     boolean centreColLabels, centreColLabelsDef = av
404             .getCentreColumnLabels();
405     boolean scaleColLabel = false;
406     AlignmentAnnotation consensusAnnot = av
407             .getAlignmentConsensusAnnotation(), structConsensusAnnot = av
408             .getAlignmentStrucConsensusAnnotation();
409     boolean renderHistogram = true, renderProfile = true, normaliseProfile = false, isRNA = rna;
410
411     BitSet graphGroupDrawn = new BitSet();
412     int charOffset = 0; // offset for a label
413     float fmWidth, fmScaling = 1f; // scaling for a label to fit it into a
414     // column.
415     Font ofont = g.getFont();
416     // \u03B2 \u03B1
417     // debug ints
418     int yfrom = 0, f_i = 0, yto = 0, f_to = 0;
419     boolean clipst = false, clipend = false;
420     for (int i = 0; i < aa.length; i++)
421     {
422       AlignmentAnnotation row = aa[i];
423       isRNA = row.isRNA();
424       {
425         // check if this is a consensus annotation row and set the display
426         // settings appropriately
427         // TODO: generalise this to have render styles for consensus/profile
428         // data
429         if (row.groupRef != null && row == row.groupRef.getConsensus())
430         {
431           renderHistogram = row.groupRef.isShowConsensusHistogram();
432           renderProfile = row.groupRef.isShowSequenceLogo();
433           normaliseProfile = row.groupRef.isNormaliseSequenceLogo();
434         }
435         else if (row == consensusAnnot || row == structConsensusAnnot)
436         {
437           renderHistogram = av_renderHistogram;
438           renderProfile = av_renderProfile;
439           normaliseProfile = av_normaliseProfile;
440         }
441         else
442         {
443           renderHistogram = true;
444           // don't need to set render/normaliseProfile since they are not
445           // currently used in any other annotation track renderer
446         }
447       }
448       Annotation[] row_annotations = row.annotations;
449       if (!row.visible)
450       {
451         continue;
452       }
453       centreColLabels = row.centreColLabels || centreColLabelsDef;
454       labelAllCols = row.showAllColLabels;
455       scaleColLabel = row.scaleColLabel;
456       lastSS = ' ';
457       lastSSX = 0;
458
459       if (!useClip
460               || ((y - charHeight) < visHeight && (y + row.height + charHeight * 2) >= sOffset))
461       {// if_in_visible_region
462         if (!clipst)
463         {
464           clipst = true;
465           yfrom = y;
466           f_i = i;
467         }
468         yto = y;
469         f_to = i;
470         if (row.graph > 0)
471         {
472           if (row.graphGroup > -1 && graphGroupDrawn.get(row.graphGroup))
473           {
474             continue;
475           }
476
477           // this is so that we draw the characters below the graph
478           y += row.height;
479
480           if (row.hasText)
481           {
482             iconOffset = charHeight - fm.getDescent();
483             y -= charHeight;
484           }
485         }
486         else if (row.hasText)
487         {
488           iconOffset = charHeight - fm.getDescent();
489
490         }
491         else
492         {
493           iconOffset = 0;
494         }
495
496         if (row.autoCalculated && av.isCalculationInProgress(row))
497         {
498           y += charHeight;
499           usedFaded = true;
500           g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0, y
501                   - row.height, imgWidth, y, annotationPanel);
502           g.setColor(Color.black);
503           // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
504
505           continue;
506         }
507
508         /*
509          * else if (annotationPanel.av.updatingConservation &&
510          * aa[i].label.equals("Conservation")) {
511          * 
512          * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
513          * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
514          * annotationPanel.imgWidth, y, annotationPanel);
515          * 
516          * g.setColor(Color.black); //
517          * g.drawString("Calculating Conservation.....",20, y-row.height/2);
518          * 
519          * continue; } else if (annotationPanel.av.updatingConservation &&
520          * aa[i].label.equals("Quality")) {
521          * 
522          * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
523          * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
524          * annotationPanel.imgWidth, y, annotationPanel);
525          * g.setColor(Color.black); // /
526          * g.drawString("Calculating Quality....",20, y-row.height/2);
527          * 
528          * continue; }
529          */
530         // first pass sets up state for drawing continuation from left-hand
531         // column
532         // of startRes
533         x = (startRes == 0) ? 0 : -1;
534         while (x < endRes - startRes)
535         {
536           if (hasHiddenColumns)
537           {
538             column = columnSelection.adjustForHiddenColumns(startRes + x);
539             if (column > row_annotations.length - 1)
540             {
541               break;
542             }
543           }
544           else
545           {
546             column = startRes + x;
547           }
548
549           if ((row_annotations == null)
550                   || (row_annotations.length <= column)
551                   || (row_annotations[column] == null))
552           {
553             validRes = false;
554           }
555           else
556           {
557             validRes = true;
558           }
559           if (x > -1)
560           {
561             if (activeRow == i)
562             {
563               g.setColor(Color.red);
564
565               if (columnSelection != null)
566               {
567                 for (int n = 0; n < columnSelection.size(); n++)
568                 {
569                   int v = columnSelection.columnAt(n);
570
571                   if (v == column)
572                   {
573                     g.fillRect(x * charWidth, y, charWidth, charHeight);
574                   }
575                 }
576               }
577             }
578             if (row.getInvalidStrucPos() > x)
579             {
580               g.setColor(Color.orange);
581               g.fillRect(x * charWidth, y, charWidth, charHeight);
582             }
583             else if (row.getInvalidStrucPos() == x)
584             {
585               g.setColor(Color.orange.darker());
586               g.fillRect(x * charWidth, y, charWidth, charHeight);
587             }
588             if (validCharWidth
589                     && validRes
590                     && row_annotations[column].displayCharacter != null
591                     && (row_annotations[column].displayCharacter.length() > 0))
592             {
593
594               if (centreColLabels || scaleColLabel)
595               {
596                 fmWidth = fm.charsWidth(
597                         row_annotations[column].displayCharacter
598                                 .toCharArray(), 0,
599                         row_annotations[column].displayCharacter.length());
600
601                 if (scaleColLabel)
602                 {
603                   // justify the label and scale to fit in column
604                   if (fmWidth > charWidth)
605                   {
606                     // scale only if the current font isn't already small enough
607                     fmScaling = charWidth;
608                     fmScaling /= fmWidth;
609                     g.setFont(ofont.deriveFont(AffineTransform
610                             .getScaleInstance(fmScaling, 1.0)));
611                     // and update the label's width to reflect the scaling.
612                     fmWidth = charWidth;
613                   }
614                 }
615               }
616               else
617               {
618                 fmWidth = fm
619                         .charWidth(row_annotations[column].displayCharacter
620                                 .charAt(0));
621               }
622               charOffset = (int) ((charWidth - fmWidth) / 2f);
623
624               if (row_annotations[column].colour == null)
625               {
626                 g.setColor(Color.black);
627               }
628               else
629               {
630                 g.setColor(row_annotations[column].colour);
631               }
632
633               if (column == 0 || row.graph > 0)
634               {
635                 g.drawString(row_annotations[column].displayCharacter,
636                         (x * charWidth) + charOffset, y + iconOffset);
637               }
638               else if (row_annotations[column - 1] == null
639                       || (labelAllCols
640                               || !row_annotations[column].displayCharacter
641                                       .equals(row_annotations[column - 1].displayCharacter) || (row_annotations[column].displayCharacter
642                               .length() < 2 && row_annotations[column].secondaryStructure == ' ')))
643               {
644                 g.drawString(row_annotations[column].displayCharacter, x
645                         * charWidth + charOffset, y + iconOffset);
646               }
647               g.setFont(ofont);
648             }
649           }
650           if (row.hasIcons)
651           {
652             char ss = validRes ? row_annotations[column].secondaryStructure
653                     : '-';
654
655             if (ss == '(')
656             {
657               // distinguish between forward/backward base-pairing
658               if (row_annotations[column].displayCharacter.indexOf(')') > -1)
659               {
660
661                 ss = ')';
662
663               }
664             }
665             if (ss == '[')
666             {
667               if ((row_annotations[column].displayCharacter.indexOf(']') > -1))
668               {
669                 ss = ']';
670
671               }
672             }
673             if (ss == '{')
674             {
675               // distinguish between forward/backward base-pairing
676               if (row_annotations[column].displayCharacter.indexOf('}') > -1)
677               {
678                 ss = '}';
679
680               }
681             }
682             if (ss == '<')
683             {
684               // distinguish between forward/backward base-pairing
685               if (row_annotations[column].displayCharacter.indexOf('<') > -1)
686               {
687                 ss = '>';
688
689               }
690             }
691             if (ss >= 65)
692             {
693               // distinguish between forward/backward base-pairing
694               if (row_annotations[column].displayCharacter.indexOf(ss + 32) > -1)
695               {
696
697                 ss = (char) (ss + 32);
698
699               }
700             }
701
702             if (!validRes || (ss != lastSS))
703             {
704
705               if (x > -1)
706               {
707
708                 int nb_annot = x - temp;
709                 // System.out.println("\t type :"+lastSS+"\t x :"+x+"\t nbre annot :"+nb_annot);
710                 switch (lastSS)
711                 {
712                 case '(': // Stem case for RNA secondary structure
713                 case ')': // and opposite direction
714                   drawStemAnnot(g, row_annotations, lastSSX, x, y,
715                           iconOffset, startRes, column, validRes, validEnd);
716                   temp = x;
717                   break;
718
719                 case 'H':
720                   if (!isRNA)
721                   {
722                     drawHelixAnnot(g, row_annotations, lastSSX, x, y,
723                             iconOffset, startRes, column, validRes,
724                             validEnd);
725                     break;
726                   }
727
728                 case 'E':
729                   if (!isRNA)
730                   {
731                     drawSheetAnnot(g, row_annotations, lastSSX, x, y,
732                             iconOffset, startRes, column, validRes,
733                             validEnd);
734                     break;
735                   }
736
737                 case '{':
738                 case '}':
739                 case '[':
740                 case ']':
741                 case '>':
742                 case '<':
743                 case 'A':
744                 case 'a':
745                 case 'B':
746                 case 'b':
747                 case 'C':
748                 case 'c':
749                 case 'D':
750                 case 'd':
751                 case 'e':
752                 case 'F':
753                 case 'f':
754                 case 'G':
755                 case 'g':
756                 case 'h':
757                 case 'I':
758                 case 'i':
759                 case 'J':
760                 case 'j':
761                 case 'K':
762                 case 'k':
763                 case 'L':
764                 case 'l':
765                 case 'M':
766                 case 'm':
767                 case 'N':
768                 case 'n':
769                 case 'O':
770                 case 'o':
771                 case 'P':
772                 case 'p':
773                 case 'Q':
774                 case 'q':
775                 case 'R':
776                 case 'r':
777                 case 'S':
778                 case 's':
779                 case 'T':
780                 case 't':
781                 case 'U':
782                 case 'u':
783                 case 'V':
784                 case 'v':
785                 case 'W':
786                 case 'w':
787                 case 'X':
788                 case 'x':
789                 case 'Y':
790                 case 'y':
791                 case 'Z':
792                 case 'z':
793
794                   Color nonCanColor = getNotCanonicalColor(lastSS);
795                   drawNotCanonicalAnnot(g, nonCanColor, row_annotations,
796                           lastSSX, x, y, iconOffset, startRes, column,
797                           validRes, validEnd);
798                   temp = x;
799                   break;
800                 default:
801                   g.setColor(Color.gray);
802                   g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth)
803                           - lastSSX, 2);
804                   temp = x;
805                   break;
806                 }
807               }
808               if (validRes)
809               {
810                 lastSS = ss;
811               }
812               else
813               {
814                 lastSS = ' ';
815               }
816               if (x > -1)
817               {
818                 lastSSX = (x * charWidth);
819               }
820             }
821           }
822           column++;
823           x++;
824         }
825         if (column >= row_annotations.length)
826         {
827           column = row_annotations.length - 1;
828           validEnd = false;
829         }
830         else
831         {
832           validEnd = true;
833         }
834         if ((row_annotations == null) || (row_annotations.length <= column)
835                 || (row_annotations[column] == null))
836         {
837           validRes = false;
838         }
839         else
840         {
841           validRes = true;
842         }
843
844         // x ++;
845
846         if (row.hasIcons)
847         {
848           switch (lastSS)
849           {
850
851           case 'H':
852             if (!isRNA)
853             {
854               drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
855                       startRes, column, validRes, validEnd);
856               break;
857             }
858
859           case 'E':
860             if (!isRNA)
861             {
862               drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
863                       startRes, column, validRes, validEnd);
864               break;
865             }
866
867           case '(':
868           case ')': // Stem case for RNA secondary structure
869
870             drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
871                     startRes, column, validRes, validEnd);
872
873             break;
874           case '{':
875           case '}':
876           case '[':
877           case ']':
878           case '>':
879           case '<':
880           case 'A':
881           case 'a':
882           case 'B':
883           case 'b':
884           case 'C':
885           case 'c':
886           case 'D':
887           case 'd':
888           case 'e':
889           case 'F':
890           case 'f':
891           case 'G':
892           case 'g':
893           case 'h':
894           case 'I':
895           case 'i':
896           case 'J':
897           case 'j':
898           case 'K':
899           case 'k':
900           case 'L':
901           case 'l':
902           case 'M':
903           case 'm':
904           case 'N':
905           case 'n':
906           case 'O':
907           case 'o':
908           case 'P':
909           case 'p':
910           case 'Q':
911           case 'q':
912           case 'R':
913           case 'r':
914           case 'T':
915           case 't':
916           case 'U':
917           case 'u':
918           case 'V':
919           case 'v':
920           case 'W':
921           case 'w':
922           case 'X':
923           case 'x':
924           case 'Y':
925           case 'y':
926           case 'Z':
927           case 'z':
928             // System.out.println(lastSS);
929             Color nonCanColor = getNotCanonicalColor(lastSS);
930             drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX,
931                     x, y, iconOffset, startRes, column, validRes, validEnd);
932             break;
933           default:
934             drawGlyphLine(g, row_annotations, lastSSX, x, y, iconOffset,
935                     startRes, column, validRes, validEnd);
936             break;
937           }
938         }
939
940         if (row.graph > 0 && row.graphHeight > 0)
941         {
942           if (row.graph == AlignmentAnnotation.LINE_GRAPH)
943           {
944             if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
945             {
946               // TODO: JAL-1291 revise rendering model so the graphGroup map is
947               // computed efficiently for all visible labels
948               float groupmax = -999999, groupmin = 9999999;
949               for (int gg = 0; gg < aa.length; gg++)
950               {
951                 if (aa[gg].graphGroup != row.graphGroup)
952                 {
953                   continue;
954                 }
955
956                 if (aa[gg] != row)
957                 {
958                   aa[gg].visible = false;
959                 }
960                 if (aa[gg].graphMax > groupmax)
961                 {
962                   groupmax = aa[gg].graphMax;
963                 }
964                 if (aa[gg].graphMin < groupmin)
965                 {
966                   groupmin = aa[gg].graphMin;
967                 }
968               }
969
970               for (int gg = 0; gg < aa.length; gg++)
971               {
972                 if (aa[gg].graphGroup == row.graphGroup)
973                 {
974                   drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
975                           endRes, y, groupmin, groupmax, row.graphHeight);
976                 }
977               }
978
979               graphGroupDrawn.set(row.graphGroup);
980             }
981             else
982             {
983               drawLineGraph(g, row, row_annotations, startRes, endRes, y,
984                       row.graphMin, row.graphMax, row.graphHeight);
985             }
986           }
987           else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
988           {
989             drawBarGraph(g, row, row_annotations, startRes, endRes,
990                     row.graphMin, row.graphMax, y, renderHistogram,
991                     renderProfile, normaliseProfile);
992           }
993         }
994       }
995       else
996       {
997         if (clipst && !clipend)
998         {
999           clipend = true;
1000         }
1001       }// end if_in_visible_region
1002       if (row.graph > 0 && row.hasText)
1003       {
1004         y += charHeight;
1005       }
1006
1007       if (row.graph == 0)
1008       {
1009         y += aa[i].height;
1010       }
1011     }
1012     if (debugRedraw)
1013     {
1014       if (canClip)
1015       {
1016         if (clipst)
1017         {
1018           System.err.println("Start clip at : " + yfrom + " (index " + f_i
1019                   + ")");
1020         }
1021         if (clipend)
1022         {
1023           System.err.println("End clip at : " + yto + " (index " + f_to
1024                   + ")");
1025         }
1026       }
1027       ;
1028       System.err.println("Annotation Rendering time:"
1029               + (System.currentTimeMillis() - stime));
1030     }
1031     ;
1032
1033     return !usedFaded;
1034   }
1035
1036   public static final Color GLYPHLINE_COLOR = Color.gray;
1037
1038   public static final Color SHEET_COLOUR = Color.green;
1039
1040   public static final Color HELIX_COLOUR = Color.red;
1041
1042   public static final Color STEM_COLOUR = Color.blue;
1043
1044   private Color sdNOTCANONICAL_COLOUR;
1045
1046   public void drawGlyphLine(Graphics g, Annotation[] row, int lastSSX,
1047           int x, int y, int iconOffset, int startRes, int column,
1048           boolean validRes, boolean validEnd)
1049   {
1050     g.setColor(GLYPHLINE_COLOR);
1051     g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
1052   }
1053
1054   public void drawSheetAnnot(Graphics g, Annotation[] row,
1055
1056   int lastSSX, int x, int y, int iconOffset, int startRes, int column,
1057           boolean validRes, boolean validEnd)
1058   {
1059     g.setColor(SHEET_COLOUR);
1060
1061     if (!validEnd || !validRes || row == null || row[column] == null
1062             || row[column].secondaryStructure != 'E')
1063     {
1064       g.fillRect(lastSSX, y + 4 + iconOffset,
1065               (x * charWidth) - lastSSX - 4, 7);
1066       g.fillPolygon(new int[]
1067       { (x * charWidth) - 4, (x * charWidth) - 4, (x * charWidth) },
1068               new int[]
1069               { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset },
1070               3);
1071     }
1072     else
1073     {
1074       g.fillRect(lastSSX, y + 4 + iconOffset,
1075               (x + 1) * charWidth - lastSSX, 7);
1076     }
1077
1078   }
1079
1080   public void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX,
1081           int x, int y, int iconOffset, int startRes, int column,
1082           boolean validRes, boolean validEnd)
1083   {
1084     g.setColor(HELIX_COLOUR);
1085
1086     int sCol = (lastSSX / charWidth) + startRes;
1087     int x1 = lastSSX;
1088     int x2 = (x * charWidth);
1089
1090     if (MAC)
1091     {
1092       int ofs = charWidth / 2;
1093       // Off by 1 offset when drawing rects and ovals
1094       // to offscreen image on the MAC
1095       g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1, 8, 8, 8);
1096       if (sCol == 0 || row[sCol - 1] == null
1097               || row[sCol - 1].secondaryStructure != 'H')
1098       {
1099       }
1100       else
1101       {
1102         // g.setColor(Color.orange);
1103         g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1 - ofs + 1, 8,
1104                 0, 0);
1105       }
1106       if (!validRes || row[column] == null
1107               || row[column].secondaryStructure != 'H')
1108       {
1109
1110       }
1111       else
1112       {
1113         // g.setColor(Color.magenta);
1114         g.fillRoundRect(lastSSX + ofs, y + 4 + iconOffset, x2 - x1 - ofs
1115                 + 1, 8, 0, 0);
1116
1117       }
1118
1119       return;
1120     }
1121
1122     if (sCol == 0 || row[sCol - 1] == null
1123             || row[sCol - 1].secondaryStructure != 'H')
1124     {
1125       g.fillArc(lastSSX, y + 4 + iconOffset, charWidth, 8, 90, 180);
1126       x1 += charWidth / 2;
1127     }
1128
1129     if (!validRes || row[column] == null
1130             || row[column].secondaryStructure != 'H')
1131     {
1132       g.fillArc((x * charWidth) - charWidth, y + 4 + iconOffset, charWidth,
1133               8, 270, 180);
1134       x2 -= charWidth / 2;
1135     }
1136
1137     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
1138   }
1139
1140   public void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1141           Annotation[] aa_annotations, int sRes, int eRes, int y,
1142           float min, float max, int graphHeight)
1143   {
1144     if (sRes > aa_annotations.length)
1145     {
1146       return;
1147     }
1148
1149     int x = 0;
1150
1151     // Adjustment for fastpaint to left
1152     if (eRes < endRes)
1153     {
1154       eRes++;
1155     }
1156
1157     eRes = Math.min(eRes, aa_annotations.length);
1158
1159     if (sRes == 0)
1160     {
1161       x++;
1162     }
1163
1164     int y1 = y, y2 = y;
1165     float range = max - min;
1166
1167     // //Draw origin
1168     if (min < 0)
1169     {
1170       y2 = y - (int) ((0 - min / range) * graphHeight);
1171     }
1172
1173     g.setColor(Color.gray);
1174     g.drawLine(x - charWidth, y2, (eRes - sRes + 1) * charWidth, y2);
1175
1176     eRes = Math.min(eRes, aa_annotations.length);
1177
1178     int column;
1179     int aaMax = aa_annotations.length - 1;
1180
1181     while (x < eRes - sRes)
1182     {
1183       column = sRes + x;
1184       if (hasHiddenColumns)
1185       {
1186         column = columnSelection.adjustForHiddenColumns(column);
1187       }
1188
1189       if (column > aaMax)
1190       {
1191         break;
1192       }
1193
1194       if (aa_annotations[column] == null
1195               || aa_annotations[column - 1] == null)
1196       {
1197         x++;
1198         continue;
1199       }
1200
1201       if (aa_annotations[column].colour == null)
1202       {
1203         g.setColor(Color.black);
1204       }
1205       else
1206       {
1207         g.setColor(aa_annotations[column].colour);
1208       }
1209
1210       y1 = y
1211               - (int) (((aa_annotations[column - 1].value - min) / range) * graphHeight);
1212       y2 = y
1213               - (int) (((aa_annotations[column].value - min) / range) * graphHeight);
1214
1215       g.drawLine(x * charWidth - charWidth / 2, y1, x * charWidth
1216               + charWidth / 2, y2);
1217       x++;
1218     }
1219
1220     if (_aa.threshold != null)
1221     {
1222       g.setColor(_aa.threshold.colour);
1223       Graphics2D g2 = (Graphics2D) g;
1224       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1225               BasicStroke.JOIN_ROUND, 3f, new float[]
1226               { 5f, 3f }, 0f));
1227
1228       y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1229       g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1230       g2.setStroke(new BasicStroke());
1231     }
1232   }
1233
1234   public void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1235           Annotation[] aa_annotations, int sRes, int eRes, float min,
1236           float max, int y, boolean renderHistogram, boolean renderProfile,
1237           boolean normaliseProfile)
1238   {
1239     if (sRes > aa_annotations.length)
1240     {
1241       return;
1242     }
1243     Font ofont = g.getFont();
1244     eRes = Math.min(eRes, aa_annotations.length);
1245
1246     int x = 0, y1 = y, y2 = y;
1247
1248     float range = max - min;
1249
1250     if (min < 0)
1251     {
1252       y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1253     }
1254
1255     g.setColor(Color.gray);
1256
1257     g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
1258
1259     int column;
1260     int aaMax = aa_annotations.length - 1;
1261     while (x < eRes - sRes)
1262     {
1263       column = sRes + x;
1264       if (hasHiddenColumns)
1265       {
1266         column = columnSelection.adjustForHiddenColumns(column);
1267       }
1268
1269       if (column > aaMax)
1270       {
1271         break;
1272       }
1273
1274       if (aa_annotations[column] == null)
1275       {
1276         x++;
1277         continue;
1278       }
1279       if (aa_annotations[column].colour == null)
1280       {
1281         g.setColor(Color.black);
1282       }
1283       else
1284       {
1285         g.setColor(aa_annotations[column].colour);
1286       }
1287
1288       y1 = y
1289               - (int) (((aa_annotations[column].value - min) / (range)) * _aa.graphHeight);
1290
1291       if (renderHistogram)
1292       {
1293         if (y1 - y2 > 0)
1294         {
1295           g.fillRect(x * charWidth, y2, charWidth, y1 - y2);
1296         }
1297         else
1298         {
1299           g.fillRect(x * charWidth, y1, charWidth, y2 - y1);
1300         }
1301       }
1302       // draw profile if available
1303       if (renderProfile)
1304       {
1305
1306         int profl[] = getProfileFor(_aa, column);
1307         // just try to draw the logo if profl is not null
1308         if (profl != null && profl[1] != 0)
1309         {
1310           float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1311           double htn = normaliseProfile ? _aa.graphHeight : (y2 - y1);// aa.graphHeight;
1312           double hght;
1313           float wdth;
1314           double ht2 = 0;
1315           char[] dc;
1316
1317           /**
1318            * profl.length == 74 indicates that the profile of a secondary
1319            * structure conservation row was accesed. Therefore dc gets length 2,
1320            * to have space for a basepair instead of just a single nucleotide
1321            */
1322           if (profl.length == 74)
1323           {
1324             dc = new char[2];
1325           }
1326           else
1327           {
1328             dc = new char[1];
1329           }
1330           LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1331           double scale = 1f / (normaliseProfile ? profl[1] : 100f);
1332           float ofontHeight = 1f / lm.getAscent();// magnify to fill box
1333           double scl = 0.0;
1334           for (int c = 2; c < profl[0];)
1335           {
1336             dc[0] = (char) profl[c++];
1337
1338             if (_aa.label.startsWith("StrucConsensus"))
1339             {
1340               dc[1] = (char) profl[c++];
1341             }
1342
1343             wdth = charWidth;
1344             wdth /= fm.charsWidth(dc, 0, dc.length);
1345
1346             ht += scl;
1347             {
1348               scl = htn * scale * profl[c++];
1349               lm = ofont.getLineMetrics(dc, 0, 1, g.getFontMetrics()
1350                       .getFontRenderContext());
1351               g.setFont(ofont.deriveFont(AffineTransform.getScaleInstance(
1352                       wdth, scl / lm.getAscent())));
1353               lm = g.getFontMetrics().getLineMetrics(dc, 0, 1, g);
1354
1355               // Debug - render boxes around characters
1356               // g.setColor(Color.red);
1357               // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
1358               // (int)(scl));
1359               // g.setColor(profcolour.findColour(dc[0]).darker());
1360               g.setColor(profcolour.findColour(dc[0], column, null));
1361
1362               hght = (ht + (scl - lm.getDescent() - lm.getBaselineOffsets()[lm
1363                       .getBaselineIndex()]));
1364
1365               g.drawChars(dc, 0, dc.length, x * charWidth, (int) hght);
1366             }
1367           }
1368           g.setFont(ofont);
1369         }
1370       }
1371       x++;
1372     }
1373     if (_aa.threshold != null)
1374     {
1375       g.setColor(_aa.threshold.colour);
1376       Graphics2D g2 = (Graphics2D) g;
1377       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1378               BasicStroke.JOIN_ROUND, 3f, new float[]
1379               { 5f, 3f }, 0f));
1380
1381       y2 = (int) (y - ((_aa.threshold.value - min) / range)
1382               * _aa.graphHeight);
1383       g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1384       g2.setStroke(new BasicStroke());
1385     }
1386   }
1387
1388   // used by overview window
1389   public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1390           Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1391   {
1392     eRes = Math.min(eRes, aa_annotations.length);
1393     g.setColor(Color.white);
1394     g.fillRect(0, 0, width, y);
1395     g.setColor(new Color(0, 0, 180));
1396
1397     int x = 0, height;
1398
1399     for (int j = sRes; j < eRes; j++)
1400     {
1401       if (aa_annotations[j] != null)
1402       {
1403         if (aa_annotations[j].colour == null)
1404         {
1405           g.setColor(Color.black);
1406         }
1407         else
1408         {
1409           g.setColor(aa_annotations[j].colour);
1410         }
1411
1412         height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1413         if (height > y)
1414         {
1415           height = y;
1416         }
1417
1418         g.fillRect(x, y - height, charWidth, height);
1419       }
1420       x += charWidth;
1421     }
1422   }
1423
1424   Color getNotCanonicalColor(char lastss)
1425   {
1426     switch (lastss)
1427     {
1428     case '{':
1429     case '}':
1430       return new Color(255, 125, 5);
1431
1432     case '[':
1433     case ']':
1434       return new Color(245, 115, 10);
1435
1436     case '>':
1437     case '<':
1438       return new Color(235, 135, 15);
1439
1440     case 'A':
1441     case 'a':
1442       return new Color(225, 105, 20);
1443
1444     case 'B':
1445     case 'b':
1446       return new Color(215, 145, 30);
1447
1448     case 'C':
1449     case 'c':
1450       return new Color(205, 95, 35);
1451
1452     case 'D':
1453     case 'd':
1454       return new Color(195, 155, 45);
1455
1456     case 'E':
1457     case 'e':
1458       return new Color(185, 85, 55);
1459
1460     case 'F':
1461     case 'f':
1462       return new Color(175, 165, 65);
1463
1464     case 'G':
1465     case 'g':
1466       return new Color(170, 75, 75);
1467
1468     case 'H':
1469     case 'h':
1470       return new Color(160, 175, 85);
1471
1472     case 'I':
1473     case 'i':
1474       return new Color(150, 65, 95);
1475
1476     case 'J':
1477     case 'j':
1478       return new Color(140, 185, 105);
1479
1480     case 'K':
1481     case 'k':
1482       return new Color(130, 55, 110);
1483
1484     case 'L':
1485     case 'l':
1486       return new Color(120, 195, 120);
1487
1488     case 'M':
1489     case 'm':
1490       return new Color(110, 45, 130);
1491
1492     case 'N':
1493     case 'n':
1494       return new Color(100, 205, 140);
1495
1496     case 'O':
1497     case 'o':
1498       return new Color(90, 35, 150);
1499
1500     case 'P':
1501     case 'p':
1502       return new Color(85, 215, 160);
1503
1504     case 'Q':
1505     case 'q':
1506       return new Color(75, 25, 170);
1507
1508     case 'R':
1509     case 'r':
1510       return new Color(65, 225, 180);
1511
1512     case 'S':
1513     case 's':
1514       return new Color(55, 15, 185);
1515
1516     case 'T':
1517     case 't':
1518       return new Color(45, 235, 195);
1519
1520     case 'U':
1521     case 'u':
1522       return new Color(35, 5, 205);
1523
1524     case 'V':
1525     case 'v':
1526       return new Color(25, 245, 215);
1527
1528     case 'W':
1529     case 'w':
1530       return new Color(15, 0, 225);
1531
1532     case 'X':
1533     case 'x':
1534       return new Color(10, 255, 235);
1535
1536     case 'Y':
1537     case 'y':
1538       return new Color(5, 150, 245);
1539
1540     case 'Z':
1541     case 'z':
1542       return new Color(0, 80, 255);
1543
1544     default:
1545       System.out.println("This is not a interaction : " + lastss);
1546       return null;
1547
1548     }
1549   }
1550 }