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