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