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