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