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