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