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