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