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