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