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