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