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