add partial button fix to annotation and statistics output
[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   private boolean av_ignoreBelowBackground;
91
92   /**
93    * attributes set from AwtRenderPanelI
94    */
95   /**
96    * old image used when data is currently being calculated and cannot be
97    * rendered
98    */
99   private Image fadedImage;
100
101   /**
102    * panel being rendered into
103    */
104   private ImageObserver annotationPanel;
105
106   /**
107    * width of image to render in panel
108    */
109   private int imgWidth;
110
111   /**
112    * offset to beginning of visible area
113    */
114   private int sOffset;
115
116   /**
117    * offset to end of visible area
118    */
119   private int visHeight;
120
121   /**
122    * indicate if the renderer should only render the visible portion of the
123    * annotation given the current view settings
124    */
125   private boolean useClip = true;
126
127   /**
128    * master flag indicating if renderer should ever try to clip. not enabled for
129    * jalview 2.8.1
130    */
131   private boolean canClip = false;
132
133   public AnnotationRenderer()
134   {
135     this(false);
136   }
137
138   /**
139    * Create a new annotation Renderer
140    * 
141    * @param debugRedraw
142    *          flag indicating if timing and redraw parameter info should be
143    *          output
144    */
145   public AnnotationRenderer(boolean debugRedraw)
146   {
147     this.debugRedraw = debugRedraw;
148   }
149
150   /**
151    * Remove any references and resources when this object is no longer required
152    */
153   public void dispose()
154   {
155     hconsensus = null;
156     complementConsensus = null;
157     hStrucConsensus = null;
158     fadedImage = null;
159     annotationPanel = null;
160   }
161
162   void drawStemAnnot(Graphics g, Annotation[] row_annotations, int lastSSX,
163           int x, int y, int iconOffset, int startRes, int column,
164           boolean validRes, boolean validEnd)
165   {
166     g.setColor(STEM_COLOUR);
167     int sCol = (lastSSX / charWidth) + 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[] { y + iconOffset, y + 14 + iconOffset,
192                     y + 8 + iconOffset }, 3);
193         x1 += 5;
194       }
195       if (diffdownstream)
196       {
197         x2 -= 1;
198       }
199     }
200     else
201     {
202       // display a forward arrow
203       if (diffdownstream)
204       {
205         /*
206          * if annotation ending with an opeing base pair half of the stem, 
207          * display a forward arrow
208          */
209         g.fillPolygon(new int[] { x2 - 5, x2 - 5, x2 }, new int[] {
210             y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
211         x2 -= 5;
212       }
213       if (diffupstream)
214       {
215         x1 += 1;
216       }
217     }
218     // draw arrow body
219     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
220   }
221
222   void drawNotCanonicalAnnot(Graphics g, Color nonCanColor,
223           Annotation[] row_annotations, int lastSSX, int x, int y,
224           int iconOffset, int startRes, int column, boolean validRes,
225           boolean validEnd)
226   {
227     // System.out.println(nonCanColor);
228
229     g.setColor(nonCanColor);
230     int sCol = (lastSSX / charWidth) + startRes;
231     int x1 = lastSSX;
232     int x2 = (x * charWidth);
233
234     String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
235             : row_annotations[column - 1].displayCharacter;
236
237     boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
238             || !dc.equals(row_annotations[sCol - 1].displayCharacter);
239     boolean diffdownstream = !validRes || !validEnd
240             || row_annotations[column] == null
241             || !dc.equals(row_annotations[column].displayCharacter);
242     // System.out.println("Column "+column+" diff up: "+diffupstream+" down:"+diffdownstream);
243     // If a closing base pair half of the stem, display a backward arrow
244     if (column > 0 && Rna.isClosingParenthesis(dc))
245     {
246
247       if (diffupstream)
248       // if (validRes && column>1 && row_annotations[column-2]!=null &&
249       // dc.equals(row_annotations[column-2].displayCharacter))
250       {
251         g.fillPolygon(new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
252                 new int[] { y + iconOffset, y + 14 + iconOffset,
253                     y + 8 + iconOffset }, 3);
254         x1 += 5;
255       }
256       if (diffdownstream)
257       {
258         x2 -= 1;
259       }
260     }
261     else
262     {
263
264       // display a forward arrow
265       if (diffdownstream)
266       {
267         g.fillPolygon(new int[] { x2 - 5, x2 - 5, x2 }, new int[] {
268             y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
269         x2 -= 5;
270       }
271       if (diffupstream)
272       {
273         x1 += 1;
274       }
275     }
276     // draw arrow body
277     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
278   }
279
280   // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
281   // av)
282   public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
283           AlignViewportI av)
284   {
285     fm = annotPanel.getFontMetrics();
286     annotationPanel = annotPanel;
287     fadedImage = annotPanel.getFadedImage();
288     imgWidth = annotPanel.getFadedImageWidth();
289     // visible area for rendering
290     int[] bounds = annotPanel.getVisibleVRange();
291     if (bounds != null)
292     {
293       sOffset = bounds[0];
294       visHeight = bounds[1];
295       if (visHeight == 0)
296       {
297         useClip = false;
298       }
299       else
300       {
301         useClip = canClip;
302       }
303     }
304     else
305     {
306       useClip = false;
307     }
308
309     updateFromAlignViewport(av);
310   }
311
312   public void updateFromAlignViewport(AlignViewportI av)
313   {
314     charWidth = av.getCharWidth();
315     endRes = av.getRanges().getEndRes();
316     charHeight = av.getCharHeight();
317     hasHiddenColumns = av.hasHiddenColumns();
318     validCharWidth = av.isValidCharWidth();
319     av_renderHistogram = av.isShowConsensusHistogram();
320     av_renderProfile = av.isShowSequenceLogo();
321     av_normaliseProfile = av.isNormaliseSequenceLogo();
322     profcolour = av.getResidueShading();
323     if (profcolour == null || profcolour.getColourScheme() == null)
324     {
325       /*
326        * Use default colour for sequence logo if 
327        * the alignment has no colourscheme set
328        * (would like to use user preference but n/a for applet)
329        */
330       ColourSchemeI col = av.getAlignment().isNucleotide() ? new NucleotideColourScheme()
331               : new ZappoColourScheme();
332       profcolour = new ResidueShader(col);
333     }
334     columnSelection = av.getColumnSelection();
335     hiddenColumns = av.getAlignment().getHiddenColumns();
336     hconsensus = av.getSequenceConsensusHash();
337     complementConsensus = av.getComplementConsensusHash();
338     hStrucConsensus = av.getRnaStructureConsensusHash();
339     av_ignoreGapsConsensus = av.isIgnoreGapsConsensus();
340     av_ignoreBelowBackground = av.isIgnoreBelowBackground();
341   }
342
343
344
345   /**
346    * Returns profile data; the first element is the profile type, the second is
347    * the number of distinct values, the third the total count, and the remainder
348    * depend on the profile type.
349    * 
350    * @param aa
351    * @param column
352    * @return
353    */
354   int[] getProfileFor(AlignmentAnnotation aa, int column)
355   {
356     // TODO : consider refactoring the global alignment calculation
357     // properties/rendering attributes as a global 'alignment group' which holds
358     // all vis settings for the alignment as a whole rather than a subset
359     //
360     if (aa.label.startsWith("Information"))
361     {
362       return AAFrequency.getHMMProfileFor(aa, column,
363               av_ignoreBelowBackground); // TODO check if this follows standard
364                                          // pipeline
365     }
366     if (aa.autoCalculated
367             && (aa.label.startsWith("Consensus") || aa.label
368                     .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(
386                   complementConsensus[column], av_ignoreGapsConsensus);
387         }
388         else
389         {
390           return AAFrequency.extractProfile(
391 hconsensus.get(column),
392                   av_ignoreGapsConsensus);
393         }
394       }
395     }
396     else
397     {
398       if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
399       {
400         // TODO implement group structure consensus
401         /*
402          * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
403          * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
404          * group selections return StructureFrequency.extractProfile(
405          * aa.groupRef.consensusData[column], aa.groupRef
406          * .getIgnoreGapsConsensus()); }
407          */
408         // TODO extend annotation row to enable dynamic and static profile data
409         // to
410         // be stored
411         if (aa.groupRef == null && aa.sequenceRef == null
412                 && hStrucConsensus != null
413                 && hStrucConsensus.length > column)
414         {
415           return StructureFrequency.extractProfile(hStrucConsensus[column],
416                   av_ignoreGapsConsensus);
417         }
418       }
419     }
420     return null;
421   }
422
423   boolean rna = false;
424
425   /**
426    * Render the annotation rows associated with an alignment.
427    * 
428    * @param annotPanel
429    *          container frame
430    * @param av
431    *          data and view settings to render
432    * @param g
433    *          destination for graphics
434    * @param activeRow
435    *          row where a mouse event occured (or -1)
436    * @param startRes
437    *          first column that will be drawn
438    * @param endRes
439    *          last column that will be drawn
440    * @return true if the fadedImage was used for any alignment annotation rows
441    *         currently being calculated
442    */
443   public boolean drawComponent(AwtRenderPanelI annotPanel,
444           AlignViewportI av, Graphics g, int activeRow, int startRes,
445           int endRes)
446   {
447     long stime = System.currentTimeMillis();
448     boolean usedFaded = false;
449     // NOTES:
450     // AnnotationPanel needs to implement: ImageObserver, access to
451     // AlignViewport
452     updateFromAwtRenderPanel(annotPanel, av);
453     fm = g.getFontMetrics();
454     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
455     int temp = 0;
456     if (aa == null)
457     {
458       return false;
459     }
460     int x = 0, y = 0;
461     int column = 0;
462     char lastSS;
463     int lastSSX;
464     int iconOffset = 0;
465     boolean validRes = false;
466     boolean validEnd = false;
467     boolean labelAllCols = false;
468     boolean centreColLabels;
469     boolean centreColLabelsDef = av.isCentreColumnLabels();
470     boolean scaleColLabel = false;
471     final AlignmentAnnotation consensusAnnot = av
472             .getAlignmentConsensusAnnotation();
473     final AlignmentAnnotation structConsensusAnnot = av
474             .getAlignmentStrucConsensusAnnotation();
475     final AlignmentAnnotation complementConsensusAnnot = av
476             .getComplementConsensusAnnotation();
477     boolean renderHistogram = true, renderProfile = true, 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
529               || ((y - charHeight) < visHeight && (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, y
570                   - 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.adjustForHiddenColumns(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 ? 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, y
699                         + iconOffset);
700               }
701               else if (row_annotations[column - 1] == null
702                       || (labelAllCols
703                               || !displayChar
704                                       .equals(row_annotations[column - 1].displayCharacter) || (displayChar
705                               .length() < 2 && row_annotations[column].secondaryStructure == ' ')))
706               {
707                 g.drawString(displayChar, x * charWidth + charOffset, y
708                         + 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 annot :"+nb_annot);
773                 switch (lastSS)
774                 {
775                 case '(': // Stem case for RNA secondary structure
776                 case ')': // and opposite direction
777                   drawStemAnnot(g, row_annotations, lastSSX, x, y,
778                           iconOffset, startRes, column, validRes, validEnd);
779                   temp = x;
780                   break;
781
782                 case 'H':
783                   if (!isRNA)
784                   {
785                     drawHelixAnnot(g, row_annotations, lastSSX, x, y,
786                             iconOffset, startRes, column, validRes,
787                             validEnd);
788                     break;
789                   }
790                   // no break if isRNA - falls through to drawNotCanonicalAnnot!
791                 case 'E':
792                   if (!isRNA)
793                   {
794                     drawSheetAnnot(g, row_annotations, lastSSX, x, y,
795                             iconOffset, startRes, column, validRes,
796                             validEnd);
797                     break;
798                   }
799                   // no break if isRNA - fall through to drawNotCanonicalAnnot!
800
801                 case '{':
802                 case '}':
803                 case '[':
804                 case ']':
805                 case '>':
806                 case '<':
807                 case 'A':
808                 case 'a':
809                 case 'B':
810                 case 'b':
811                 case 'C':
812                 case 'c':
813                 case 'D':
814                 case 'd':
815                 case 'e':
816                 case 'F':
817                 case 'f':
818                 case 'G':
819                 case 'g':
820                 case 'h':
821                 case 'I':
822                 case 'i':
823                 case 'J':
824                 case 'j':
825                 case 'K':
826                 case 'k':
827                 case 'L':
828                 case 'l':
829                 case 'M':
830                 case 'm':
831                 case 'N':
832                 case 'n':
833                 case 'O':
834                 case 'o':
835                 case 'P':
836                 case 'p':
837                 case 'Q':
838                 case 'q':
839                 case 'R':
840                 case 'r':
841                 case 'S':
842                 case 's':
843                 case 'T':
844                 case 't':
845                 case 'U':
846                 case 'u':
847                 case 'V':
848                 case 'v':
849                 case 'W':
850                 case 'w':
851                 case 'X':
852                 case 'x':
853                 case 'Y':
854                 case 'y':
855                 case 'Z':
856                 case 'z':
857
858                   Color nonCanColor = getNotCanonicalColor(lastSS);
859                   drawNotCanonicalAnnot(g, nonCanColor, row_annotations,
860                           lastSSX, x, y, iconOffset, startRes, column,
861                           validRes, validEnd);
862                   temp = x;
863                   break;
864                 default:
865                   g.setColor(Color.gray);
866                   g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth)
867                           - lastSSX, 2);
868                   temp = x;
869                   break;
870                 }
871               }
872               if (validRes)
873               {
874                 lastSS = ss;
875               }
876               else
877               {
878                 lastSS = ' ';
879               }
880               if (x > -1)
881               {
882                 lastSSX = (x * charWidth);
883               }
884             }
885           }
886           column++;
887           x++;
888         }
889         if (column >= row_annotations.length)
890         {
891           column = row_annotations.length - 1;
892           validEnd = false;
893         }
894         else
895         {
896           validEnd = true;
897         }
898         if ((row_annotations == null) || (row_annotations.length <= column)
899                 || (row_annotations[column] == null))
900         {
901           validRes = false;
902         }
903         else
904         {
905           validRes = true;
906         }
907         // x ++;
908
909         if (row.hasIcons)
910         {
911           switch (lastSS)
912           {
913
914           case 'H':
915             if (!isRNA)
916             {
917               drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
918                       startRes, column, validRes, validEnd);
919               break;
920             }
921             // no break if isRNA - fall through to drawNotCanonicalAnnot!
922
923           case 'E':
924             if (!isRNA)
925             {
926               drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
927                       startRes, column, validRes, validEnd);
928               break;
929             }
930             // no break if isRNA - fall through to drawNotCanonicalAnnot!
931
932           case '(':
933           case ')': // Stem case for RNA secondary structure
934
935             drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
936                     startRes, column, validRes, validEnd);
937
938             break;
939           case '{':
940           case '}':
941           case '[':
942           case ']':
943           case '>':
944           case '<':
945           case 'A':
946           case 'a':
947           case 'B':
948           case 'b':
949           case 'C':
950           case 'c':
951           case 'D':
952           case 'd':
953           case 'e':
954           case 'F':
955           case 'f':
956           case 'G':
957           case 'g':
958           case 'h':
959           case 'I':
960           case 'i':
961           case 'J':
962           case 'j':
963           case 'K':
964           case 'k':
965           case 'L':
966           case 'l':
967           case 'M':
968           case 'm':
969           case 'N':
970           case 'n':
971           case 'O':
972           case 'o':
973           case 'P':
974           case 'p':
975           case 'Q':
976           case 'q':
977           case 'R':
978           case 'r':
979           case 'T':
980           case 't':
981           case 'U':
982           case 'u':
983           case 'V':
984           case 'v':
985           case 'W':
986           case 'w':
987           case 'X':
988           case 'x':
989           case 'Y':
990           case 'y':
991           case 'Z':
992           case 'z':
993             // System.out.println(lastSS);
994             Color nonCanColor = getNotCanonicalColor(lastSS);
995             drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX,
996                     x, y, iconOffset, startRes, column, validRes, validEnd);
997             break;
998           default:
999             drawGlyphLine(g, row_annotations, lastSSX, x, y, iconOffset,
1000                     startRes, column, validRes, validEnd);
1001             break;
1002           }
1003         }
1004
1005         if (row.graph > 0 && row.graphHeight > 0)
1006         {
1007           if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1008           {
1009             if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
1010             {
1011               // TODO: JAL-1291 revise rendering model so the graphGroup map is
1012               // computed efficiently for all visible labels
1013               float groupmax = -999999, groupmin = 9999999;
1014               for (int gg = 0; gg < aa.length; gg++)
1015               {
1016                 if (aa[gg].graphGroup != row.graphGroup)
1017                 {
1018                   continue;
1019                 }
1020
1021                 if (aa[gg] != row)
1022                 {
1023                   aa[gg].visible = false;
1024                 }
1025                 if (aa[gg].graphMax > groupmax)
1026                 {
1027                   groupmax = aa[gg].graphMax;
1028                 }
1029                 if (aa[gg].graphMin < groupmin)
1030                 {
1031                   groupmin = aa[gg].graphMin;
1032                 }
1033               }
1034
1035               for (int gg = 0; gg < aa.length; gg++)
1036               {
1037                 if (aa[gg].graphGroup == row.graphGroup)
1038                 {
1039                   drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
1040                           endRes, y, groupmin, groupmax, row.graphHeight);
1041                 }
1042               }
1043
1044               graphGroupDrawn.set(row.graphGroup);
1045             }
1046             else
1047             {
1048               drawLineGraph(g, row, row_annotations, startRes, endRes, y,
1049                       row.graphMin, row.graphMax, row.graphHeight);
1050             }
1051           }
1052           else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1053           {
1054             drawBarGraph(g, row, row_annotations, startRes, endRes,
1055                     row.graphMin, row.graphMax, y, renderHistogram,
1056                     renderProfile, normaliseProfile);
1057           }
1058         }
1059       }
1060       else
1061       {
1062         if (clipst && !clipend)
1063         {
1064           clipend = true;
1065         }
1066       }// end if_in_visible_region
1067       if (row.graph > 0 && row.hasText)
1068       {
1069         y += charHeight;
1070       }
1071
1072       if (row.graph == 0)
1073       {
1074         y += aa[i].height;
1075       }
1076     }
1077     if (debugRedraw)
1078     {
1079       if (canClip)
1080       {
1081         if (clipst)
1082         {
1083           System.err.println("Start clip at : " + yfrom + " (index " + f_i
1084                   + ")");
1085         }
1086         if (clipend)
1087         {
1088           System.err.println("End clip at : " + yto + " (index " + f_to
1089                   + ")");
1090         }
1091       }
1092       ;
1093       System.err.println("Annotation Rendering time:"
1094               + (System.currentTimeMillis() - stime));
1095     }
1096     ;
1097
1098     return !usedFaded;
1099   }
1100
1101   public static final Color GLYPHLINE_COLOR = Color.gray;
1102
1103   public static final Color SHEET_COLOUR = Color.green;
1104
1105   public static final Color HELIX_COLOUR = Color.red;
1106
1107   public static final Color STEM_COLOUR = Color.blue;
1108
1109   private Color sdNOTCANONICAL_COLOUR;
1110
1111   void drawGlyphLine(Graphics g, Annotation[] row, int lastSSX, int x,
1112           int y, int iconOffset, int startRes, int column,
1113           boolean validRes, boolean validEnd)
1114   {
1115     g.setColor(GLYPHLINE_COLOR);
1116     g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
1117   }
1118
1119   void drawSheetAnnot(Graphics g, Annotation[] row,
1120
1121   int lastSSX, int x, int y, int iconOffset, int startRes, int column,
1122           boolean validRes, boolean validEnd)
1123   {
1124     g.setColor(SHEET_COLOUR);
1125
1126     if (!validEnd || !validRes || row == null || row[column] == null
1127             || row[column].secondaryStructure != 'E')
1128     {
1129       g.fillRect(lastSSX, y + 4 + iconOffset,
1130               (x * charWidth) - lastSSX - 4, 7);
1131       g.fillPolygon(new int[] { (x * charWidth) - 4, (x * charWidth) - 4,
1132           (x * charWidth) }, new int[] { y + iconOffset,
1133           y + 14 + iconOffset, y + 7 + iconOffset }, 3);
1134     }
1135     else
1136     {
1137       g.fillRect(lastSSX, y + 4 + iconOffset,
1138               (x + 1) * charWidth - lastSSX, 7);
1139     }
1140
1141   }
1142
1143   void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x,
1144           int y, int iconOffset, int startRes, int column,
1145           boolean validRes, boolean validEnd)
1146   {
1147     g.setColor(HELIX_COLOUR);
1148
1149     int sCol = (lastSSX / charWidth) + startRes;
1150     int x1 = lastSSX;
1151     int x2 = (x * charWidth);
1152
1153     if (MAC)
1154     {
1155       int ofs = charWidth / 2;
1156       // Off by 1 offset when drawing rects and ovals
1157       // to offscreen image on the MAC
1158       g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1, 8, 8, 8);
1159       if (sCol == 0 || row[sCol - 1] == null
1160               || row[sCol - 1].secondaryStructure != 'H')
1161       {
1162       }
1163       else
1164       {
1165         // g.setColor(Color.orange);
1166         g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1 - ofs + 1, 8,
1167                 0, 0);
1168       }
1169       if (!validRes || row[column] == null
1170               || row[column].secondaryStructure != 'H')
1171       {
1172
1173       }
1174       else
1175       {
1176         // g.setColor(Color.magenta);
1177         g.fillRoundRect(lastSSX + ofs, y + 4 + iconOffset, x2 - x1 - ofs
1178                 + 1, 8, 0, 0);
1179
1180       }
1181
1182       return;
1183     }
1184
1185     if (sCol == 0 || row[sCol - 1] == null
1186             || row[sCol - 1].secondaryStructure != 'H')
1187     {
1188       g.fillArc(lastSSX, y + 4 + iconOffset, charWidth, 8, 90, 180);
1189       x1 += charWidth / 2;
1190     }
1191
1192     if (!validRes || row[column] == null
1193             || row[column].secondaryStructure != 'H')
1194     {
1195       g.fillArc((x * charWidth) - charWidth, y + 4 + iconOffset, charWidth,
1196               8, 270, 180);
1197       x2 -= charWidth / 2;
1198     }
1199
1200     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
1201   }
1202
1203   void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1204           Annotation[] aa_annotations, int sRes, int eRes, int y,
1205           float min, float max, int graphHeight)
1206   {
1207     if (sRes > aa_annotations.length)
1208     {
1209       return;
1210     }
1211
1212     int x = 0;
1213
1214     // Adjustment for fastpaint to left
1215     if (eRes < endRes)
1216     {
1217       eRes++;
1218     }
1219
1220     eRes = Math.min(eRes, aa_annotations.length);
1221
1222     if (sRes == 0)
1223     {
1224       x++;
1225     }
1226
1227     int y1 = y, y2 = y;
1228     float range = max - min;
1229
1230     // //Draw origin
1231     if (min < 0)
1232     {
1233       y2 = y - (int) ((0 - min / range) * graphHeight);
1234     }
1235
1236     g.setColor(Color.gray);
1237     g.drawLine(x - charWidth, y2, (eRes - sRes + 1) * charWidth, y2);
1238
1239     eRes = Math.min(eRes, aa_annotations.length);
1240
1241     int column;
1242     int aaMax = aa_annotations.length - 1;
1243
1244     while (x < eRes - sRes)
1245     {
1246       column = sRes + x;
1247       if (hasHiddenColumns)
1248       {
1249         column = hiddenColumns.adjustForHiddenColumns(column);
1250       }
1251
1252       if (column > aaMax)
1253       {
1254         break;
1255       }
1256
1257       if (aa_annotations[column] == null
1258               || aa_annotations[column - 1] == null)
1259       {
1260         x++;
1261         continue;
1262       }
1263
1264       if (aa_annotations[column].colour == null)
1265       {
1266         g.setColor(Color.black);
1267       }
1268       else
1269       {
1270         g.setColor(aa_annotations[column].colour);
1271       }
1272
1273       y1 = y
1274               - (int) (((aa_annotations[column - 1].value - min) / range) * graphHeight);
1275       y2 = y
1276               - (int) (((aa_annotations[column].value - min) / range) * graphHeight);
1277
1278       g.drawLine(x * charWidth - charWidth / 2, y1, x * charWidth
1279               + charWidth / 2, y2);
1280       x++;
1281     }
1282
1283     if (_aa.threshold != null)
1284     {
1285       g.setColor(_aa.threshold.colour);
1286       Graphics2D g2 = (Graphics2D) g;
1287       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1288               BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f }, 0f));
1289
1290       y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1291       g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1292       g2.setStroke(new BasicStroke());
1293     }
1294   }
1295
1296   void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1297           Annotation[] aa_annotations, int sRes, int eRes, float min,
1298           float max, int y, boolean renderHistogram, boolean renderProfile,
1299           boolean normaliseProfile)
1300   {
1301     if (sRes > aa_annotations.length)
1302     {
1303       return;
1304     }
1305     Font ofont = g.getFont();
1306     eRes = Math.min(eRes, aa_annotations.length);
1307
1308     int x = 0, y1 = y, y2 = y;
1309
1310     float range = max - min;
1311
1312     if (min < 0)
1313     {
1314       y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1315     }
1316
1317     g.setColor(Color.gray);
1318
1319     g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
1320
1321     int column;
1322     int aaMax = aa_annotations.length - 1;
1323     while (x < eRes - sRes)
1324     {
1325       column = sRes + x;
1326       if (hasHiddenColumns)
1327       {
1328         column = hiddenColumns.adjustForHiddenColumns(column);
1329       }
1330
1331       if (column > aaMax)
1332       {
1333         break;
1334       }
1335
1336       if (aa_annotations[column] == null)
1337       {
1338         x++;
1339         continue;
1340       }
1341       if (aa_annotations[column].colour == null)
1342       {
1343         g.setColor(Color.black);
1344       }
1345       else
1346       {
1347         g.setColor(aa_annotations[column].colour);
1348       }
1349
1350       y1 = y
1351               - (int) (((aa_annotations[column].value - min) / (range)) * _aa.graphHeight);
1352
1353       if (renderHistogram)
1354       {
1355         if (y1 - y2 > 0)
1356         {
1357           g.fillRect(x * charWidth, y2, charWidth, y1 - y2);
1358         }
1359         else
1360         {
1361           g.fillRect(x * charWidth, y1, charWidth, y2 - y1);
1362         }
1363       }
1364       // draw profile if available
1365       if (renderProfile)
1366       {
1367
1368         /*
1369          * {profile type, #values, total count, char1, pct1, char2, pct2...}
1370          */
1371         int profl[] = getProfileFor(_aa, column);
1372
1373         // just try to draw the logo if profl is not null
1374         if (profl != null && profl[2] != 0)
1375         {
1376           boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
1377           boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
1378           float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1379           double htn = normaliseProfile ? _aa.graphHeight : (y2 - y1);// aa.graphHeight;
1380           double hght;
1381           float wdth;
1382           double ht2 = 0;
1383           char[] dc;
1384
1385           /**
1386            * Render a single base for a sequence profile, a base pair for
1387            * structure profile, and a triplet for a cdna profile
1388            */
1389           dc = new char[isStructureProfile ? 2 : (isCdnaProfile ? 3 : 1)];
1390
1391           LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1392           double scale = 1f / (normaliseProfile ? profl[2] : 100f);
1393           float ofontHeight = 1f / lm.getAscent();// magnify to fill box
1394           double scl = 0.0;
1395
1396           /*
1397            * Traverse the character(s)/percentage data in the array
1398            */
1399           int c = 3;
1400           int valuesProcessed = 0;
1401           // profl[1] is the number of values in the profile
1402           while (valuesProcessed < profl[1])
1403           {
1404             if (isStructureProfile)
1405             {
1406               // todo can we encode a structure pair as an int, like codons?
1407               dc[0] = (char) profl[c++];
1408               dc[1] = (char) profl[c++];
1409             }
1410             else if (isCdnaProfile)
1411             {
1412               dc = CodingUtils.decodeCodon(profl[c++]);
1413             }
1414             else
1415             {
1416               dc[0] = (char) profl[c++];
1417             }
1418
1419             wdth = charWidth;
1420             wdth /= fm.charsWidth(dc, 0, dc.length);
1421
1422             ht += scl;
1423             // next profl[] position is profile % for the character(s)
1424             scl = htn * scale * profl[c++];
1425             lm = ofont.getLineMetrics(dc, 0, 1, g.getFontMetrics()
1426                     .getFontRenderContext());
1427             Font font = ofont.deriveFont(AffineTransform.getScaleInstance(
1428                     wdth, scl / lm.getAscent()));
1429             g.setFont(font);
1430             lm = g.getFontMetrics().getLineMetrics(dc, 0, 1, g);
1431
1432             // Debug - render boxes around characters
1433             // g.setColor(Color.red);
1434             // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
1435             // (int)(scl));
1436             // g.setColor(profcolour.findColour(dc[0]).darker());
1437
1438             /*
1439              * Set character colour as per alignment colour scheme; use the
1440              * codon translation if a cDNA profile
1441              */
1442             Color colour = null;
1443             if (isCdnaProfile)
1444             {
1445               final String codonTranslation = ResidueProperties
1446                       .codonTranslate(new String(dc));
1447               colour = profcolour.findColour(codonTranslation.charAt(0),
1448                       column, null);
1449             }
1450             else
1451             {
1452               colour = profcolour.findColour(dc[0], column, null);
1453             }
1454             g.setColor(colour == Color.white ? Color.lightGray : colour);
1455
1456             hght = (ht + (scl - lm.getDescent() - lm.getBaselineOffsets()[lm
1457                     .getBaselineIndex()]));
1458
1459             g.drawChars(dc, 0, dc.length, x * charWidth, (int) hght);
1460             valuesProcessed++;
1461           }
1462           g.setFont(ofont);
1463         }
1464       }
1465       x++;
1466     }
1467     if (_aa.threshold != null)
1468     {
1469       g.setColor(_aa.threshold.colour);
1470       Graphics2D g2 = (Graphics2D) g;
1471       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1472               BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f }, 0f));
1473
1474       y2 = (int) (y - ((_aa.threshold.value - min) / range)
1475               * _aa.graphHeight);
1476       g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1477       g2.setStroke(new BasicStroke());
1478     }
1479   }
1480
1481   // used by overview window
1482   public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1483           Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1484   {
1485     eRes = Math.min(eRes, aa_annotations.length);
1486     g.setColor(Color.white);
1487     g.fillRect(0, 0, width, y);
1488     g.setColor(new Color(0, 0, 180));
1489
1490     int x = 0, height;
1491
1492     for (int j = sRes; j < eRes; j++)
1493     {
1494       if (aa_annotations[j] != null)
1495       {
1496         if (aa_annotations[j].colour == null)
1497         {
1498           g.setColor(Color.black);
1499         }
1500         else
1501         {
1502           g.setColor(aa_annotations[j].colour);
1503         }
1504
1505         height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1506         if (height > y)
1507         {
1508           height = y;
1509         }
1510
1511         g.fillRect(x, y - height, charWidth, height);
1512       }
1513       x += charWidth;
1514     }
1515   }
1516
1517   Color getNotCanonicalColor(char lastss)
1518   {
1519     switch (lastss)
1520     {
1521     case '{':
1522     case '}':
1523       return new Color(255, 125, 5);
1524
1525     case '[':
1526     case ']':
1527       return new Color(245, 115, 10);
1528
1529     case '>':
1530     case '<':
1531       return new Color(235, 135, 15);
1532
1533     case 'A':
1534     case 'a':
1535       return new Color(225, 105, 20);
1536
1537     case 'B':
1538     case 'b':
1539       return new Color(215, 145, 30);
1540
1541     case 'C':
1542     case 'c':
1543       return new Color(205, 95, 35);
1544
1545     case 'D':
1546     case 'd':
1547       return new Color(195, 155, 45);
1548
1549     case 'E':
1550     case 'e':
1551       return new Color(185, 85, 55);
1552
1553     case 'F':
1554     case 'f':
1555       return new Color(175, 165, 65);
1556
1557     case 'G':
1558     case 'g':
1559       return new Color(170, 75, 75);
1560
1561     case 'H':
1562     case 'h':
1563       return new Color(160, 175, 85);
1564
1565     case 'I':
1566     case 'i':
1567       return new Color(150, 65, 95);
1568
1569     case 'J':
1570     case 'j':
1571       return new Color(140, 185, 105);
1572
1573     case 'K':
1574     case 'k':
1575       return new Color(130, 55, 110);
1576
1577     case 'L':
1578     case 'l':
1579       return new Color(120, 195, 120);
1580
1581     case 'M':
1582     case 'm':
1583       return new Color(110, 45, 130);
1584
1585     case 'N':
1586     case 'n':
1587       return new Color(100, 205, 140);
1588
1589     case 'O':
1590     case 'o':
1591       return new Color(90, 35, 150);
1592
1593     case 'P':
1594     case 'p':
1595       return new Color(85, 215, 160);
1596
1597     case 'Q':
1598     case 'q':
1599       return new Color(75, 25, 170);
1600
1601     case 'R':
1602     case 'r':
1603       return new Color(65, 225, 180);
1604
1605     case 'S':
1606     case 's':
1607       return new Color(55, 15, 185);
1608
1609     case 'T':
1610     case 't':
1611       return new Color(45, 235, 195);
1612
1613     case 'U':
1614     case 'u':
1615       return new Color(35, 5, 205);
1616
1617     case 'V':
1618     case 'v':
1619       return new Color(25, 245, 215);
1620
1621     case 'W':
1622     case 'w':
1623       return new Color(15, 0, 225);
1624
1625     case 'X':
1626     case 'x':
1627       return new Color(10, 255, 235);
1628
1629     case 'Y':
1630     case 'y':
1631       return new Color(5, 150, 245);
1632
1633     case 'Z':
1634     case 'z':
1635       return new Color(0, 80, 255);
1636
1637     default:
1638       System.out.println("This is not a interaction : " + lastss);
1639       return null;
1640
1641     }
1642   }
1643 }