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