JAL-3202 discard zero percentages in extracted profile
[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;
474     boolean renderProfile = false;
475     boolean normaliseProfile = false;
476     boolean isRNA = rna;
477
478     BitSet graphGroupDrawn = new BitSet();
479     int charOffset = 0; // offset for a label
480     // \u03B2 \u03B1
481     // debug ints
482     int yfrom = 0, f_i = 0, yto = 0, f_to = 0;
483     boolean clipst = false, clipend = false;
484     for (int i = 0; i < aa.length; i++)
485     {
486       AlignmentAnnotation row = aa[i];
487       isRNA = row.isRNA();
488       {
489         // check if this is a consensus annotation row and set the display
490         // settings appropriately
491         // TODO: generalise this to have render styles for consensus/profile
492         // data
493         if (row.groupRef != null && row == row.groupRef.getConsensus())
494         {
495           renderHistogram = row.groupRef.isShowConsensusHistogram();
496           renderProfile = row.groupRef.isShowSequenceLogo();
497           normaliseProfile = row.groupRef.isNormaliseSequenceLogo();
498         }
499         else if (row == consensusAnnot || row == structConsensusAnnot
500                 || row == complementConsensusAnnot)
501         {
502           renderHistogram = av_renderHistogram;
503           renderProfile = av_renderProfile;
504           normaliseProfile = av_normaliseProfile;
505         }
506         else
507         {
508           renderHistogram = true;
509           // don't need to set render/normaliseProfile since they are not
510           // currently used in any other annotation track renderer
511         }
512       }
513       Annotation[] row_annotations = row.annotations;
514       if (!row.visible)
515       {
516         continue;
517       }
518       centreColLabels = row.centreColLabels || centreColLabelsDef;
519       labelAllCols = row.showAllColLabels;
520       scaleColLabel = row.scaleColLabel;
521       lastSS = ' ';
522       lastSSX = 0;
523
524       if (!useClip || ((y - charHeight) < visHeight
525               && (y + row.height + charHeight * 2) >= sOffset))
526       {// if_in_visible_region
527         if (!clipst)
528         {
529           clipst = true;
530           yfrom = y;
531           f_i = i;
532         }
533         yto = y;
534         f_to = i;
535         if (row.graph > 0)
536         {
537           if (row.graphGroup > -1 && graphGroupDrawn.get(row.graphGroup))
538           {
539             continue;
540           }
541
542           // this is so that we draw the characters below the graph
543           y += row.height;
544
545           if (row.hasText)
546           {
547             iconOffset = charHeight - fm.getDescent();
548             y -= charHeight;
549           }
550         }
551         else if (row.hasText)
552         {
553           iconOffset = charHeight - fm.getDescent();
554
555         }
556         else
557         {
558           iconOffset = 0;
559         }
560
561         if (row.autoCalculated && av.isCalculationInProgress(row))
562         {
563           y += charHeight;
564           usedFaded = true;
565           g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0,
566                   y - row.height, imgWidth, y, annotationPanel);
567           g.setColor(Color.black);
568           // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
569
570           continue;
571         }
572
573         /*
574          * else if (annotationPanel.av.updatingConservation &&
575          * aa[i].label.equals("Conservation")) {
576          * 
577          * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
578          * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
579          * annotationPanel.imgWidth, y, annotationPanel);
580          * 
581          * g.setColor(Color.black); //
582          * g.drawString("Calculating Conservation.....",20, y-row.height/2);
583          * 
584          * continue; } else if (annotationPanel.av.updatingConservation &&
585          * aa[i].label.equals("Quality")) {
586          * 
587          * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
588          * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
589          * annotationPanel.imgWidth, y, annotationPanel);
590          * g.setColor(Color.black); // /
591          * g.drawString("Calculating Quality....",20, y-row.height/2);
592          * 
593          * continue; }
594          */
595         // first pass sets up state for drawing continuation from left-hand
596         // column
597         // of startRes
598         x = (startRes == 0) ? 0 : -1;
599         while (x < endRes - startRes)
600         {
601           if (hasHiddenColumns)
602           {
603             column = hiddenColumns.visibleToAbsoluteColumn(startRes + x);
604             if (column > row_annotations.length - 1)
605             {
606               break;
607             }
608           }
609           else
610           {
611             column = startRes + x;
612           }
613
614           if ((row_annotations == null)
615                   || (row_annotations.length <= column)
616                   || (row_annotations[column] == null))
617           {
618             validRes = false;
619           }
620           else
621           {
622             validRes = true;
623           }
624           final String displayChar = validRes
625                   ? row_annotations[column].displayCharacter
626                   : null;
627           if (x > -1)
628           {
629             if (activeRow == i)
630             {
631               g.setColor(Color.red);
632
633               if (columnSelection != null)
634               {
635                 if (columnSelection.contains(column))
636                 {
637                   g.fillRect(x * charWidth, y, charWidth, charHeight);
638                 }
639               }
640             }
641             if (row.getInvalidStrucPos() > x)
642             {
643               g.setColor(Color.orange);
644               g.fillRect(x * charWidth, y, charWidth, charHeight);
645             }
646             else if (row.getInvalidStrucPos() == x)
647             {
648               g.setColor(Color.orange.darker());
649               g.fillRect(x * charWidth, y, charWidth, charHeight);
650             }
651             if (validCharWidth && validRes && displayChar != null
652                     && (displayChar.length() > 0))
653             {
654               Graphics2D gg = ((Graphics2D) g);
655               float fmWidth = fm.charsWidth(displayChar.toCharArray(), 0,
656                       displayChar.length());
657
658               /*
659                * shrink label width to fit in column, if that is
660                * both configured and necessary
661                */
662               boolean scaledToFit = false;
663               float fmScaling = 1f;
664               if (scaleColLabel && fmWidth > charWidth)
665               {
666                 scaledToFit = true;
667                 fmScaling = charWidth;
668                 fmScaling /= fmWidth;
669                 // and update the label's width to reflect the scaling.
670                 fmWidth = charWidth;
671               }
672
673               charOffset = (int) ((charWidth - fmWidth) / 2f);
674
675               if (row_annotations[column].colour == null)
676               {
677                 gg.setColor(Color.black);
678               }
679               else
680               {
681                 gg.setColor(row_annotations[column].colour);
682               }
683
684               /*
685                * draw the label, unless it is the same secondary structure
686                * symbol (excluding RNA Helix) as the previous column
687                */
688               final int xPos = (x * charWidth) + charOffset;
689               final int yPos = y + iconOffset;
690
691               /*
692                * translate to drawing position _before_ applying any scaling
693                */
694               gg.translate(xPos, yPos);
695               if (scaledToFit)
696               {
697                 /*
698                  * use a scaling transform to make the label narrower
699                  * (JalviewJS doesn't have Font.deriveFont(AffineTransform))
700                  */
701                 gg.transform(
702                         AffineTransform.getScaleInstance(fmScaling, 1.0));
703               }
704               if (column == 0 || row.graph > 0)
705               {
706                 gg.drawString(displayChar, 0, 0);
707               }
708               else if (row_annotations[column - 1] == null || (labelAllCols
709                       || !displayChar.equals(
710                               row_annotations[column - 1].displayCharacter)
711                       || (displayChar.length() < 2
712                               && row_annotations[column].secondaryStructure == ' ')))
713               {
714                 gg.drawString(displayChar, 0, 0);
715               }
716               if (scaledToFit)
717               {
718                 /*
719                  * undo scaling before translating back 
720                  * (restoring saved transform does NOT work in JS PDFGraphics!)
721                  */
722                 gg.transform(AffineTransform
723                         .getScaleInstance(1D / fmScaling, 1.0));
724               }
725               gg.translate(-xPos, -yPos);
726             }
727           }
728           if (row.hasIcons)
729           {
730             char ss = validRes ? row_annotations[column].secondaryStructure
731                     : '-';
732
733             if (ss == '(')
734             {
735               // distinguish between forward/backward base-pairing
736               if (displayChar.indexOf(')') > -1)
737               {
738
739                 ss = ')';
740
741               }
742             }
743             if (ss == '[')
744             {
745               if ((displayChar.indexOf(']') > -1))
746               {
747                 ss = ']';
748
749               }
750             }
751             if (ss == '{')
752             {
753               // distinguish between forward/backward base-pairing
754               if (displayChar.indexOf('}') > -1)
755               {
756                 ss = '}';
757
758               }
759             }
760             if (ss == '<')
761             {
762               // distinguish between forward/backward base-pairing
763               if (displayChar.indexOf('<') > -1)
764               {
765                 ss = '>';
766
767               }
768             }
769             if (isRNA && (ss >= CHAR_A) && (ss <= CHAR_Z))
770             {
771               // distinguish between forward/backward base-pairing
772               int ssLowerCase = ss + UPPER_TO_LOWER;
773               // TODO would .equals() be safer here? or charAt(0)?
774               if (displayChar.indexOf(ssLowerCase) > -1)
775               {
776                 ss = (char) ssLowerCase;
777               }
778             }
779
780             if (!validRes || (ss != lastSS))
781             {
782
783               if (x > -1)
784               {
785
786                 int nb_annot = x - temp;
787                 // System.out.println("\t type :"+lastSS+"\t x :"+x+"\t nbre
788                 // annot :"+nb_annot);
789                 switch (lastSS)
790                 {
791                 case '(': // Stem case for RNA secondary structure
792                 case ')': // and opposite direction
793                   drawStemAnnot(g, row_annotations, lastSSX, x, y,
794                           iconOffset, startRes, column, validRes, validEnd);
795                   temp = x;
796                   break;
797
798                 case 'H':
799                   if (!isRNA)
800                   {
801                     drawHelixAnnot(g, row_annotations, lastSSX, x, y,
802                             iconOffset, startRes, column, validRes,
803                             validEnd);
804                     break;
805                   }
806                   // no break if isRNA - falls through to drawNotCanonicalAnnot!
807                 case 'E':
808                   if (!isRNA)
809                   {
810                     drawSheetAnnot(g, row_annotations, lastSSX, x, y,
811                             iconOffset, startRes, column, validRes,
812                             validEnd);
813                     break;
814                   }
815                   // no break if isRNA - fall through to drawNotCanonicalAnnot!
816
817                 case '{':
818                 case '}':
819                 case '[':
820                 case ']':
821                 case '>':
822                 case '<':
823                 case 'A':
824                 case 'a':
825                 case 'B':
826                 case 'b':
827                 case 'C':
828                 case 'c':
829                 case 'D':
830                 case 'd':
831                 case 'e':
832                 case 'F':
833                 case 'f':
834                 case 'G':
835                 case 'g':
836                 case 'h':
837                 case 'I':
838                 case 'i':
839                 case 'J':
840                 case 'j':
841                 case 'K':
842                 case 'k':
843                 case 'L':
844                 case 'l':
845                 case 'M':
846                 case 'm':
847                 case 'N':
848                 case 'n':
849                 case 'O':
850                 case 'o':
851                 case 'P':
852                 case 'p':
853                 case 'Q':
854                 case 'q':
855                 case 'R':
856                 case 'r':
857                 case 'S':
858                 case 's':
859                 case 'T':
860                 case 't':
861                 case 'U':
862                 case 'u':
863                 case 'V':
864                 case 'v':
865                 case 'W':
866                 case 'w':
867                 case 'X':
868                 case 'x':
869                 case 'Y':
870                 case 'y':
871                 case 'Z':
872                 case 'z':
873
874                   Color nonCanColor = getNotCanonicalColor(lastSS);
875                   drawNotCanonicalAnnot(g, nonCanColor, row_annotations,
876                           lastSSX, x, y, iconOffset, startRes, column,
877                           validRes, validEnd);
878                   temp = x;
879                   break;
880                 default:
881                   g.setColor(Color.gray);
882                   g.fillRect(lastSSX, y + 6 + iconOffset,
883                           (x * charWidth) - lastSSX, 2);
884                   temp = x;
885                   break;
886                 }
887               }
888               if (validRes)
889               {
890                 lastSS = ss;
891               }
892               else
893               {
894                 lastSS = ' ';
895               }
896               if (x > -1)
897               {
898                 lastSSX = (x * charWidth);
899               }
900             }
901           }
902           column++;
903           x++;
904         }
905         if (column >= row_annotations.length)
906         {
907           column = row_annotations.length - 1;
908           validEnd = false;
909         }
910         else
911         {
912           validEnd = true;
913         }
914         if ((row_annotations == null) || (row_annotations.length <= column)
915                 || (row_annotations[column] == null))
916         {
917           validRes = false;
918         }
919         else
920         {
921           validRes = true;
922         }
923         // x ++;
924
925         if (row.hasIcons)
926         {
927           switch (lastSS)
928           {
929
930           case 'H':
931             if (!isRNA)
932             {
933               drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
934                       startRes, column, validRes, validEnd);
935               break;
936             }
937             // no break if isRNA - fall through to drawNotCanonicalAnnot!
938
939           case 'E':
940             if (!isRNA)
941             {
942               drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
943                       startRes, column, validRes, validEnd);
944               break;
945             }
946             // no break if isRNA - fall through to drawNotCanonicalAnnot!
947
948           case '(':
949           case ')': // Stem case for RNA secondary structure
950
951             drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
952                     startRes, column, validRes, validEnd);
953
954             break;
955           case '{':
956           case '}':
957           case '[':
958           case ']':
959           case '>':
960           case '<':
961           case 'A':
962           case 'a':
963           case 'B':
964           case 'b':
965           case 'C':
966           case 'c':
967           case 'D':
968           case 'd':
969           case 'e':
970           case 'F':
971           case 'f':
972           case 'G':
973           case 'g':
974           case 'h':
975           case 'I':
976           case 'i':
977           case 'J':
978           case 'j':
979           case 'K':
980           case 'k':
981           case 'L':
982           case 'l':
983           case 'M':
984           case 'm':
985           case 'N':
986           case 'n':
987           case 'O':
988           case 'o':
989           case 'P':
990           case 'p':
991           case 'Q':
992           case 'q':
993           case 'R':
994           case 'r':
995           case 'T':
996           case 't':
997           case 'U':
998           case 'u':
999           case 'V':
1000           case 'v':
1001           case 'W':
1002           case 'w':
1003           case 'X':
1004           case 'x':
1005           case 'Y':
1006           case 'y':
1007           case 'Z':
1008           case 'z':
1009             // System.out.println(lastSS);
1010             Color nonCanColor = getNotCanonicalColor(lastSS);
1011             drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX,
1012                     x, y, iconOffset, startRes, column, validRes, validEnd);
1013             break;
1014           default:
1015             drawGlyphLine(g, row_annotations, lastSSX, x, y, iconOffset,
1016                     startRes, column, validRes, validEnd);
1017             break;
1018           }
1019         }
1020
1021         if (row.graph > 0 && row.graphHeight > 0)
1022         {
1023           if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1024           {
1025             if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
1026             {
1027               // TODO: JAL-1291 revise rendering model so the graphGroup map is
1028               // computed efficiently for all visible labels
1029               float groupmax = -999999, groupmin = 9999999;
1030               for (int gg = 0; gg < aa.length; gg++)
1031               {
1032                 if (aa[gg].graphGroup != row.graphGroup)
1033                 {
1034                   continue;
1035                 }
1036
1037                 if (aa[gg] != row)
1038                 {
1039                   aa[gg].visible = false;
1040                 }
1041                 if (aa[gg].graphMax > groupmax)
1042                 {
1043                   groupmax = aa[gg].graphMax;
1044                 }
1045                 if (aa[gg].graphMin < groupmin)
1046                 {
1047                   groupmin = aa[gg].graphMin;
1048                 }
1049               }
1050
1051               for (int gg = 0; gg < aa.length; gg++)
1052               {
1053                 if (aa[gg].graphGroup == row.graphGroup)
1054                 {
1055                   drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
1056                           endRes, y, groupmin, groupmax, row.graphHeight);
1057                 }
1058               }
1059
1060               graphGroupDrawn.set(row.graphGroup);
1061             }
1062             else
1063             {
1064               drawLineGraph(g, row, row_annotations, startRes, endRes, y,
1065                       row.graphMin, row.graphMax, row.graphHeight);
1066             }
1067           }
1068           else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1069           {
1070             drawBarGraph(g, row, row_annotations, startRes, endRes,
1071                     row.graphMin, row.graphMax, y, renderHistogram,
1072                     renderProfile, normaliseProfile);
1073           }
1074         }
1075       }
1076       else
1077       {
1078         if (clipst && !clipend)
1079         {
1080           clipend = true;
1081         }
1082       } // end if_in_visible_region
1083       if (row.graph > 0 && row.hasText)
1084       {
1085         y += charHeight;
1086       }
1087
1088       if (row.graph == 0)
1089       {
1090         y += aa[i].height;
1091       }
1092     }
1093     if (debugRedraw)
1094     {
1095       if (canClip)
1096       {
1097         if (clipst)
1098         {
1099           System.err.println(
1100                   "Start clip at : " + yfrom + " (index " + f_i + ")");
1101         }
1102         if (clipend)
1103         {
1104           System.err.println(
1105                   "End clip at : " + yto + " (index " + f_to + ")");
1106         }
1107       }
1108       ;
1109       System.err.println("Annotation Rendering time:"
1110               + (System.currentTimeMillis() - stime));
1111     }
1112     ;
1113
1114     return !usedFaded;
1115   }
1116
1117   public static final Color GLYPHLINE_COLOR = Color.gray;
1118
1119   public static final Color SHEET_COLOUR = Color.green;
1120
1121   public static final Color HELIX_COLOUR = Color.red;
1122
1123   public static final Color STEM_COLOUR = Color.blue;
1124
1125   private Color sdNOTCANONICAL_COLOUR;
1126
1127   void drawGlyphLine(Graphics g, Annotation[] row, int lastSSX, int x,
1128           int y, int iconOffset, int startRes, int column, boolean validRes,
1129           boolean validEnd)
1130   {
1131     g.setColor(GLYPHLINE_COLOR);
1132     g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
1133   }
1134
1135   void drawSheetAnnot(Graphics g, Annotation[] row,
1136
1137           int lastSSX, int x, int y, int iconOffset, int startRes,
1138           int column, boolean validRes, boolean validEnd)
1139   {
1140     g.setColor(SHEET_COLOUR);
1141
1142     if (!validEnd || !validRes || row == null || row[column] == null
1143             || row[column].secondaryStructure != 'E')
1144     {
1145       g.fillRect(lastSSX, y + 4 + iconOffset, (x * charWidth) - lastSSX - 4,
1146               7);
1147       g.fillPolygon(
1148               new int[]
1149               { (x * charWidth) - 4, (x * charWidth) - 4, (x * charWidth) },
1150               new int[]
1151               { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset },
1152               3);
1153     }
1154     else
1155     {
1156       g.fillRect(lastSSX, y + 4 + iconOffset, (x + 1) * charWidth - lastSSX,
1157               7);
1158     }
1159
1160   }
1161
1162   void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x,
1163           int y, int iconOffset, int startRes, int column, boolean validRes,
1164           boolean validEnd)
1165   {
1166     g.setColor(HELIX_COLOUR);
1167
1168     int sCol = (lastSSX / charWidth)
1169             + hiddenColumns.visibleToAbsoluteColumn(startRes);
1170     int x1 = lastSSX;
1171     int x2 = (x * charWidth);
1172
1173     if (USE_FILL_ROUND_RECT)
1174     {
1175       int ofs = charWidth / 2;
1176       // Off by 1 offset when drawing rects and ovals
1177       // to offscreen image on the MAC
1178       g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1, 8, 8, 8);
1179       if (sCol == 0 || row[sCol - 1] == null
1180               || row[sCol - 1].secondaryStructure != 'H')
1181       {
1182       }
1183       else
1184       {
1185         // g.setColor(Color.orange);
1186         g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1 - ofs + 1, 8,
1187                 0, 0);
1188       }
1189       if (!validRes || row[column] == null
1190               || row[column].secondaryStructure != 'H')
1191       {
1192
1193       }
1194       else
1195       {
1196         // g.setColor(Color.magenta);
1197         g.fillRoundRect(lastSSX + ofs, y + 4 + iconOffset,
1198                 x2 - x1 - ofs + 1, 8, 0, 0);
1199
1200       }
1201
1202       return;
1203     }
1204
1205     if (sCol == 0 || row[sCol - 1] == null
1206             || row[sCol - 1].secondaryStructure != 'H')
1207     {
1208       g.fillArc(lastSSX, y + 4 + iconOffset, charWidth, 8, 90, 180);
1209       x1 += charWidth / 2;
1210     }
1211
1212     if (!validRes || row[column] == null
1213             || row[column].secondaryStructure != 'H')
1214     {
1215       g.fillArc((x * charWidth) - charWidth, y + 4 + iconOffset, charWidth,
1216               8, 270, 180);
1217       x2 -= charWidth / 2;
1218     }
1219
1220     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
1221   }
1222
1223   void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1224           Annotation[] aa_annotations, int sRes, int eRes, int y, float min,
1225           float max, int graphHeight)
1226   {
1227     if (sRes > aa_annotations.length)
1228     {
1229       return;
1230     }
1231
1232     int x = 0;
1233
1234     // Adjustment for fastpaint to left
1235     if (eRes < endRes)
1236     {
1237       eRes++;
1238     }
1239
1240     eRes = Math.min(eRes, aa_annotations.length);
1241
1242     if (sRes == 0)
1243     {
1244       x++;
1245     }
1246
1247     int y1 = y, y2 = y;
1248     float range = max - min;
1249
1250     // //Draw origin
1251     if (min < 0)
1252     {
1253       y2 = y - (int) ((0 - min / range) * graphHeight);
1254     }
1255
1256     g.setColor(Color.gray);
1257     g.drawLine(x - charWidth, y2, (eRes - sRes + 1) * charWidth, y2);
1258
1259     eRes = Math.min(eRes, aa_annotations.length);
1260
1261     int column;
1262     int aaMax = aa_annotations.length - 1;
1263
1264     while (x < eRes - sRes)
1265     {
1266       column = sRes + x;
1267       if (hasHiddenColumns)
1268       {
1269         column = hiddenColumns.visibleToAbsoluteColumn(column);
1270       }
1271
1272       if (column > aaMax)
1273       {
1274         break;
1275       }
1276
1277       if (aa_annotations[column] == null
1278               || aa_annotations[column - 1] == null)
1279       {
1280         x++;
1281         continue;
1282       }
1283
1284       if (aa_annotations[column].colour == null)
1285       {
1286         g.setColor(Color.black);
1287       }
1288       else
1289       {
1290         g.setColor(aa_annotations[column].colour);
1291       }
1292
1293       y1 = y - (int) (((aa_annotations[column - 1].value - min) / range)
1294               * graphHeight);
1295       y2 = y - (int) (((aa_annotations[column].value - min) / range)
1296               * graphHeight);
1297
1298       g.drawLine(x * charWidth - charWidth / 2, y1,
1299               x * charWidth + charWidth / 2, y2);
1300       x++;
1301     }
1302
1303     if (_aa.threshold != null)
1304     {
1305       g.setColor(_aa.threshold.colour);
1306       Graphics2D g2 = (Graphics2D) g;
1307       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1308               BasicStroke.JOIN_ROUND, 3f, new float[]
1309               { 5f, 3f }, 0f));
1310
1311       y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1312       g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1313       g2.setStroke(new BasicStroke());
1314     }
1315   }
1316
1317   @SuppressWarnings("unused")
1318   void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1319           Annotation[] aa_annotations, int sRes, int eRes, float min,
1320           float max, int y, boolean renderHistogram, boolean renderProfile,
1321           boolean normaliseProfile)
1322   {
1323     if (sRes > aa_annotations.length)
1324     {
1325       return;
1326     }
1327     Font ofont = g.getFont();
1328     eRes = Math.min(eRes, aa_annotations.length);
1329
1330     int x = 0, y1 = y, y2 = y;
1331
1332     float range = max - min;
1333
1334     if (min < 0)
1335     {
1336       y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1337     }
1338
1339     g.setColor(Color.gray);
1340
1341     g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
1342
1343     int column;
1344     int aaMax = aa_annotations.length - 1;
1345     while (x < eRes - sRes)
1346     {
1347       column = sRes + x;
1348       if (hasHiddenColumns)
1349       {
1350         column = hiddenColumns.visibleToAbsoluteColumn(column);
1351       }
1352
1353       if (column > aaMax)
1354       {
1355         break;
1356       }
1357
1358       if (aa_annotations[column] == null)
1359       {
1360         x++;
1361         continue;
1362       }
1363       if (aa_annotations[column].colour == null)
1364       {
1365         g.setColor(Color.black);
1366       }
1367       else
1368       {
1369         g.setColor(aa_annotations[column].colour);
1370       }
1371
1372       y1 = y - (int) (((aa_annotations[column].value - min) / (range))
1373               * _aa.graphHeight);
1374
1375       if (renderHistogram)
1376       {
1377         if (y1 - y2 > 0)
1378         {
1379           g.fillRect(x * charWidth, y2, charWidth, y1 - y2);
1380         }
1381         else
1382         {
1383           g.fillRect(x * charWidth, y1, charWidth, y2 - y1);
1384         }
1385       }
1386       // draw profile if available
1387       if (renderProfile)
1388       {
1389
1390         /*
1391          * {profile type, #values, total count, char1, pct1, char2, pct2...}
1392          */
1393         int profl[] = getProfileFor(_aa, column);
1394
1395         // just try to draw the logo if profl is not null
1396         if (profl != null && profl[2] != 0)
1397         {
1398           boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
1399           boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
1400           float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1401           final double normaliseFactor = normaliseProfile ? _aa.graphHeight : (y2 - y1);
1402
1403           /**
1404            * Render a single base for a sequence profile, a base pair for
1405            * structure profile, and a triplet for a cdna profile
1406            */
1407           char[] dc = new char[isStructureProfile ? 2
1408                   : (isCdnaProfile ? 3 : 1)];
1409
1410           // lm is not necessary - we can just use fm - could be off by no more
1411           // than 0.5 px
1412           // LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1413           // System.out.println(asc + " " + dec + " " + (asc - lm.getAscent())
1414           // + " " + (dec - lm.getDescent()));
1415
1416           double asc = fm.getAscent();
1417           double dec = fm.getDescent();
1418           double fht = fm.getHeight();
1419
1420           double scale = 1f / (normaliseProfile ? profl[2] : 100f);
1421           // float ofontHeight = 1f / fm.getAscent();// magnify to fill box
1422
1423           /*
1424            * Traverse the character(s)/percentage data in the array
1425            */
1426
1427           float ht2 = ht;
1428
1429           // profl[1] is the number of values in the profile
1430           for (int i = 0, c = 3, last = profl[1]; i < last; i++)
1431           {
1432
1433             String s;
1434             if (isStructureProfile)
1435             {
1436               // todo can we encode a structure pair as an int, like codons?
1437               dc[0] = (char) profl[c++];
1438               dc[1] = (char) profl[c++];
1439               s = new String(dc);
1440             }
1441             else if (isCdnaProfile)
1442             {
1443               CodingUtils.decodeCodon2(profl[c++], dc);
1444               s = new String(dc);
1445             }
1446             else
1447             {
1448               dc[0] = (char) profl[c++];
1449               s = new String(dc);
1450             }
1451             // next profl[] position is profile % for the character(s)
1452             
1453             int percent = profl[c++];
1454             if (percent == 0)
1455             {
1456               // failsafe in case a count rounds down to 0%
1457               continue;
1458             }
1459             double newHeight = normaliseFactor * scale * percent;
1460
1461             /*
1462              * Set character colour as per alignment colour scheme; use the
1463              * codon translation if a cDNA profile
1464              */
1465             Color colour = null;
1466             if (isCdnaProfile)
1467             {
1468               final String codonTranslation = ResidueProperties
1469                       .codonTranslate(s);
1470               colour = profcolour.findColour(codonTranslation.charAt(0),
1471                       column, null);
1472             }
1473             else
1474             {
1475               colour = profcolour.findColour(dc[0], column, null);
1476             }
1477             g.setColor(colour == Color.white ? Color.lightGray : colour);
1478
1479             // Debug - render boxes around characters
1480             // g.setColor(Color.red);
1481             // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
1482             // (int)(scl));
1483             // g.setColor(profcolour.findColour(dc[0]).darker());
1484
1485             double sx = 1f * charWidth / fm.charsWidth(dc, 0, dc.length);            
1486             double sy = newHeight / asc;
1487             double newAsc = asc * sy; 
1488             double newDec = dec * sy;
1489             // it is not necessary to recalculate lm for the new font.
1490             // note: lm.getBaselineOffsets()[lm.getBaselineIndex()]) must be 0
1491             // by definition. Was:
1492             // int hght = (int) (ht + (newAsc - newDec);
1493             // - lm.getBaselineOffsets()[lm.getBaselineIndex()]));
1494
1495             if (Platform.isJS())
1496             {
1497               /*
1498                * SwingJS does not implement font.deriveFont()
1499                * so use a scaling transform to draw instead,
1500                * this is off by a very small amount
1501                */
1502               final int hght = (int) (ht2 + (newAsc - newDec));
1503               Graphics2D gg = (Graphics2D) g;
1504               int xShift = (int) Math.round(x * charWidth / sx);
1505               int yShift = (int) Math.round(hght / sy);
1506               gg.transform(AffineTransform.getScaleInstance(sx, sy));
1507               gg.drawString(s, xShift, yShift);
1508               gg.transform(
1509                       AffineTransform.getScaleInstance(1D / sx, 1D / sy));
1510               ht2 += newHeight;
1511             }
1512             else
1513             {
1514               /*
1515                * Java ('normal') method is to scale the font to fit
1516                */
1517               final int hght = (int) (ht + (newAsc - newDec));
1518               Font font = ofont
1519                       .deriveFont(AffineTransform.getScaleInstance(sx, sy));
1520               g.setFont(font);
1521               g.drawChars(dc, 0, dc.length, x * charWidth, hght);
1522               ht += newHeight;
1523             }
1524           }
1525         }
1526       }
1527       x++;
1528     }
1529     if (_aa.threshold != null)
1530     {
1531       g.setColor(_aa.threshold.colour);
1532       Graphics2D g2 = (Graphics2D) g;
1533       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1534               BasicStroke.JOIN_ROUND, 3f, new float[]
1535       { 5f, 3f }, 0f));
1536
1537       y2 = (int) (y
1538               - ((_aa.threshold.value - min) / range) * _aa.graphHeight);
1539       g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1540       g2.setStroke(new BasicStroke());
1541     }
1542   }
1543
1544   // used by overview window
1545   public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1546           Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1547   {
1548     eRes = Math.min(eRes, aa_annotations.length);
1549     g.setColor(Color.white);
1550     g.fillRect(0, 0, width, y);
1551     g.setColor(new Color(0, 0, 180));
1552
1553     int x = 0, height;
1554
1555     for (int j = sRes; j < eRes; j++)
1556     {
1557       if (aa_annotations[j] != null)
1558       {
1559         if (aa_annotations[j].colour == null)
1560         {
1561           g.setColor(Color.black);
1562         }
1563         else
1564         {
1565           g.setColor(aa_annotations[j].colour);
1566         }
1567
1568         height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1569         if (height > y)
1570         {
1571           height = y;
1572         }
1573
1574         g.fillRect(x, y - height, charWidth, height);
1575       }
1576       x += charWidth;
1577     }
1578   }
1579
1580   Color getNotCanonicalColor(char lastss)
1581   {
1582     switch (lastss)
1583     {
1584     case '{':
1585     case '}':
1586       return new Color(255, 125, 5);
1587
1588     case '[':
1589     case ']':
1590       return new Color(245, 115, 10);
1591
1592     case '>':
1593     case '<':
1594       return new Color(235, 135, 15);
1595
1596     case 'A':
1597     case 'a':
1598       return new Color(225, 105, 20);
1599
1600     case 'B':
1601     case 'b':
1602       return new Color(215, 145, 30);
1603
1604     case 'C':
1605     case 'c':
1606       return new Color(205, 95, 35);
1607
1608     case 'D':
1609     case 'd':
1610       return new Color(195, 155, 45);
1611
1612     case 'E':
1613     case 'e':
1614       return new Color(185, 85, 55);
1615
1616     case 'F':
1617     case 'f':
1618       return new Color(175, 165, 65);
1619
1620     case 'G':
1621     case 'g':
1622       return new Color(170, 75, 75);
1623
1624     case 'H':
1625     case 'h':
1626       return new Color(160, 175, 85);
1627
1628     case 'I':
1629     case 'i':
1630       return new Color(150, 65, 95);
1631
1632     case 'J':
1633     case 'j':
1634       return new Color(140, 185, 105);
1635
1636     case 'K':
1637     case 'k':
1638       return new Color(130, 55, 110);
1639
1640     case 'L':
1641     case 'l':
1642       return new Color(120, 195, 120);
1643
1644     case 'M':
1645     case 'm':
1646       return new Color(110, 45, 130);
1647
1648     case 'N':
1649     case 'n':
1650       return new Color(100, 205, 140);
1651
1652     case 'O':
1653     case 'o':
1654       return new Color(90, 35, 150);
1655
1656     case 'P':
1657     case 'p':
1658       return new Color(85, 215, 160);
1659
1660     case 'Q':
1661     case 'q':
1662       return new Color(75, 25, 170);
1663
1664     case 'R':
1665     case 'r':
1666       return new Color(65, 225, 180);
1667
1668     case 'S':
1669     case 's':
1670       return new Color(55, 15, 185);
1671
1672     case 'T':
1673     case 't':
1674       return new Color(45, 235, 195);
1675
1676     case 'U':
1677     case 'u':
1678       return new Color(35, 5, 205);
1679
1680     case 'V':
1681     case 'v':
1682       return new Color(25, 245, 215);
1683
1684     case 'W':
1685     case 'w':
1686       return new Color(15, 0, 225);
1687
1688     case 'X':
1689     case 'x':
1690       return new Color(10, 255, 235);
1691
1692     case 'Y':
1693     case 'y':
1694       return new Color(5, 150, 245);
1695
1696     case 'Z':
1697     case 'z':
1698       return new Color(0, 80, 255);
1699
1700     default:
1701       System.out.println("This is not a interaction : " + lastss);
1702       return null;
1703
1704     }
1705   }
1706 }