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