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