8588bf2294d1ba18a4e667f321f507f290814458
[jalview.git] / src / jalview / renderer / AnnotationRenderer.java
1 package jalview.renderer;
2
3 import jalview.analysis.AAFrequency;
4 import jalview.analysis.StructureFrequency;
5 import jalview.api.AlignViewportI;
6 import jalview.datamodel.AlignmentAnnotation;
7 import jalview.datamodel.Annotation;
8 import jalview.datamodel.ColumnSelection;
9 import jalview.schemes.ColourSchemeI;
10
11 import java.awt.BasicStroke;
12 import java.awt.Color;
13 import java.awt.Font;
14 import java.awt.FontMetrics;
15 import java.awt.Graphics;
16 import java.awt.Graphics2D;
17 import java.awt.Image;
18 import java.awt.font.LineMetrics;
19 import java.awt.geom.AffineTransform;
20 import java.awt.image.ImageObserver;
21 import java.util.Hashtable;
22
23 import com.stevesoft.pat.Regex;
24
25 public class AnnotationRenderer
26 {
27
28   public AnnotationRenderer()
29   {
30     // TODO Auto-generated constructor stub
31   }
32
33   public void drawStemAnnot(Graphics g, Annotation[] row_annotations,
34           int lastSSX, int x, int y, int iconOffset, int startRes,
35           int column, boolean validRes, boolean validEnd)
36   {
37     g.setColor(STEM_COLOUR);
38     int sCol = (lastSSX / charWidth) + startRes;
39     int x1 = lastSSX;
40     int x2 = (x * charWidth);
41     Regex closeparen = new Regex("(\\))");
42
43     String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
44             : row_annotations[column - 1].displayCharacter;
45
46     boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
47             || !dc.equals(row_annotations[sCol - 1].displayCharacter);
48     boolean diffdownstream = !validRes || !validEnd
49             || row_annotations[column] == null
50             || !dc.equals(row_annotations[column].displayCharacter);
51     // System.out.println("Column "+column+" diff up: "+diffupstream+" down:"+diffdownstream);
52     // If a closing base pair half of the stem, display a backward arrow
53     if (column > 0 && closeparen.search(dc))
54     {
55       if (diffupstream)
56       // if (validRes && column>1 && row_annotations[column-2]!=null &&
57       // dc.equals(row_annotations[column-2].displayCharacter))
58       {
59         g.fillPolygon(new int[]
60         { lastSSX + 5, lastSSX + 5, lastSSX }, new int[]
61         { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
62         x1 += 5;
63       }
64       if (diffdownstream)
65       {
66         x2 -= 1;
67       }
68     }
69     else
70     {
71       // display a forward arrow
72       if (diffdownstream)
73       {
74         g.fillPolygon(new int[]
75         { x2 - 5, x2 - 5, x2 }, new int[]
76         { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
77         x2 -= 5;
78       }
79       if (diffupstream)
80       {
81         x1 += 1;
82       }
83     }
84     // draw arrow body
85     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
86   }
87
88   private int charWidth, endRes, charHeight;
89
90   private boolean validCharWidth, hasHiddenColumns;
91
92   private FontMetrics fm;
93
94   private final boolean MAC = new jalview.util.Platform().isAMac();
95
96   boolean av_renderHistogram = true, av_renderProfile = true,
97           av_normaliseProfile = false;
98
99   ColourSchemeI profcolour = null;
100
101   private ColumnSelection columnSelection;
102
103   private Hashtable[] hconsensus;
104
105   private Hashtable[] hStrucConsensus;
106
107   private boolean av_ignoreGapsConsensus;
108
109   /**
110    * attributes set from AwtRenderPanelI
111    */
112   /**
113    * old image used when data is currently being calculated and cannot be
114    * rendered
115    */
116   private Image fadedImage;
117
118   /**
119    * panel being rendered into
120    */
121   private ImageObserver annotationPanel;
122
123   /**
124    * width of image to render in panel
125    */
126   private int imgWidth;
127
128   // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
129   // av)
130   public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
131           AlignViewportI av)
132   {
133     fm = annotPanel.getFontMetrics();
134     annotationPanel = annotPanel;
135     fadedImage = annotPanel.getFadedImage();
136     imgWidth = annotPanel.getFadedImageWidth();
137     updateFromAlignViewport(av);
138   }
139
140   public void updateFromAlignViewport(AlignViewportI av)
141   {
142     charWidth = av.getCharWidth();
143     endRes = av.getEndRes();
144     charHeight = av.getCharHeight();
145     hasHiddenColumns = av.hasHiddenColumns();
146     validCharWidth = av.isValidCharWidth();
147     av_renderHistogram = av.isShowConsensusHistogram();
148     av_renderProfile = av.isShowSequenceLogo();
149     av_normaliseProfile = av.isNormaliseSequenceLogo();
150     profcolour = av.getGlobalColourScheme();
151     if (profcolour == null)
152     {
153       // Set the default colour for sequence logo if the alignnent has no
154       // colourscheme set
155       profcolour = av.getAlignment().isNucleotide() ? new jalview.schemes.NucleotideColourScheme()
156               : new jalview.schemes.ZappoColourScheme();
157     }
158     columnSelection = av.getColumnSelection();
159     hconsensus = av.getSequenceConsensusHash();// hconsensus;
160     hStrucConsensus = av.getRnaStructureConsensusHash(); // hStrucConsensus;
161     av_ignoreGapsConsensus = av.getIgnoreGapsConsensus();
162   }
163
164   public int[] getProfileFor(AlignmentAnnotation aa, int column)
165   {
166     // TODO : consider refactoring the global alignment calculation
167     // properties/rendering attributes as a global 'alignment group' which holds
168     // all vis settings for the alignment as a whole rather than a subset
169     //
170     if (aa.autoCalculated && aa.label.startsWith("Consensus"))
171     {
172       if (aa.groupRef != null && aa.groupRef.consensusData != null
173               && aa.groupRef.isShowSequenceLogo())
174       {
175         return AAFrequency.extractProfile(
176                 aa.groupRef.consensusData[column],
177                 aa.groupRef.getIgnoreGapsConsensus());
178       }
179       // TODO extend annotation row to enable dynamic and static profile data to
180       // be stored
181       if (aa.groupRef == null && aa.sequenceRef == null && av_renderProfile)
182       {
183         return AAFrequency.extractProfile(hconsensus[column],
184                 av_ignoreGapsConsensus);
185       }
186     }
187     else
188     {
189       if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
190       {
191         // TODO implement group structure consensus
192         /*
193          * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
194          * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
195          * group selections return StructureFrequency.extractProfile(
196          * aa.groupRef.consensusData[column], aa.groupRef
197          * .getIgnoreGapsConsensus()); }
198          */
199         // TODO extend annotation row to enable dynamic and static profile data
200         // to
201         // be stored
202         if (aa.groupRef == null && aa.sequenceRef == null
203                 && av_renderProfile && hStrucConsensus != null
204                 && hStrucConsensus.length > column)
205         {
206           return StructureFrequency.extractProfile(hStrucConsensus[column],
207                   av_ignoreGapsConsensus);
208         }
209       }
210     }
211     return null;
212   }
213
214   /**
215    * Render the annotation rows associated with an alignment.
216    * @param annotPanel
217    *          container frame
218    * @param av
219    *          data and view settings to render
220    * @param g
221    *          destination for graphics
222    * @param activeRow
223    *          row where a mouse event occured (or -1)
224    * @param startRes
225    *          first column that will be drawn
226    * @param endRes
227    *          last column that will be drawn
228    * @return true if the fadedImage was used for any alignment annotation rows currently being calculated
229    */
230   public boolean drawComponent(AwtRenderPanelI annotPanel, AlignViewportI av,
231           Graphics g, int activeRow, int startRes, int endRes)
232   {
233     boolean usedFaded=false;
234     // NOTES:
235     // AnnotationPanel needs to implement: ImageObserver, access to
236     // AlignViewport
237     updateFromAwtRenderPanel(annotPanel, av);
238     fm = g.getFontMetrics();
239     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
240
241     int x = 0, y = 0;
242     int column = 0;
243     char lastSS;
244     int lastSSX;
245     int iconOffset = 0;
246     boolean validRes = false;
247     boolean validEnd = false;
248     boolean labelAllCols = false;
249     boolean centreColLabels, centreColLabelsDef = av
250             .getCentreColumnLabels();
251     boolean scaleColLabel = false;
252     boolean[] graphGroupDrawn = new boolean[aa.length];
253     int charOffset = 0; // offset for a label
254     float fmWidth, fmScaling = 1f; // scaling for a label to fit it into a
255     // column.
256     Font ofont = g.getFont();
257     // \u03B2 \u03B1
258     for (int i = 0; i < aa.length; i++)
259     {
260       AlignmentAnnotation row = aa[i];
261       Annotation[] row_annotations=row.annotations;
262       if (!row.visible)
263       {
264         continue;
265       }
266       centreColLabels = row.centreColLabels || centreColLabelsDef;
267       labelAllCols = row.showAllColLabels;
268       scaleColLabel = row.scaleColLabel;
269       lastSS = ' ';
270       lastSSX = 0;
271       if (row.graph > 0)
272       {
273         if (row.graphGroup > -1 && graphGroupDrawn[row.graphGroup])
274         {
275           continue;
276         }
277
278         // this is so that we draw the characters below the graph
279         y += row.height;
280
281         if (row.hasText)
282         {
283           iconOffset = charHeight - fm.getDescent();
284           y -= charHeight;
285         }
286       }
287       else if (row.hasText)
288       {
289         iconOffset = charHeight - fm.getDescent();
290
291       }
292       else
293       {
294         iconOffset = 0;
295       }
296
297       if (row.autoCalculated && av.isCalculationInProgress(row))
298       {
299         y += charHeight;
300         usedFaded=true;
301         g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0, y
302                 - row.height, imgWidth, y, annotationPanel);
303         g.setColor(Color.black);
304         // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
305
306         continue;
307       }
308
309       /*
310        * else if (annotationPanel.av.updatingConservation &&
311        * aa[i].label.equals("Conservation")) {
312        *
313        * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
314        * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
315        * annotationPanel.imgWidth, y, annotationPanel);
316        *
317        * g.setColor(Color.black); //
318        * g.drawString("Calculating Conservation.....",20, y-row.height/2);
319        *
320        * continue; } else if (annotationPanel.av.updatingConservation &&
321        * aa[i].label.equals("Quality")) {
322        *
323        * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
324        * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
325        * annotationPanel.imgWidth, y, annotationPanel); g.setColor(Color.black);
326        * // / g.drawString("Calculating Quality....",20, y-row.height/2);
327        *
328        * continue; }
329        */
330       // first pass sets up state for drawing continuation from left-hand column
331       // of startRes
332       x = (startRes == 0) ? 0 : -1;
333       while (x < endRes - startRes)
334       {
335         if (hasHiddenColumns)
336         {
337           column = columnSelection.adjustForHiddenColumns(startRes + x);
338           if (column > row_annotations.length - 1)
339           {
340             break;
341           }
342         }
343         else
344         {
345           column = startRes + x;
346         }
347
348         if ((row_annotations == null) || (row_annotations.length <= column)
349                 || (row_annotations[column] == null))
350         {
351           validRes = false;
352         }
353         else
354         {
355           validRes = true;
356         }
357         if (x > -1)
358         {
359           if (activeRow == i)
360           {
361             g.setColor(Color.red);
362
363             if (columnSelection != null)
364             {
365               for (int n = 0; n < columnSelection.size(); n++)
366               {
367                 int v = columnSelection.columnAt(n);
368
369                 if (v == column)
370                 {
371                   g.fillRect(x * charWidth, y, charWidth, charHeight);
372                 }
373               }
374             }
375           }
376           if (!row.isValidStruc())
377           {
378             g.setColor(Color.orange);
379             g.fillRect((int) row.getInvalidStrucPos() * charWidth, y,
380                     charWidth, charHeight);
381           }
382           if (validCharWidth
383                   && validRes
384                   && row_annotations[column].displayCharacter != null
385                   && (row_annotations[column].displayCharacter.length() > 0))
386           {
387
388             if (centreColLabels || scaleColLabel)
389             {
390               fmWidth = fm.charsWidth(
391                       row_annotations[column].displayCharacter
392                               .toCharArray(), 0,
393                       row_annotations[column].displayCharacter.length());
394
395               if (scaleColLabel)
396               {
397                 // justify the label and scale to fit in column
398                 if (fmWidth > charWidth)
399                 {
400                   // scale only if the current font isn't already small enough
401                   fmScaling = charWidth;
402                   fmScaling /= fmWidth;
403                   g.setFont(ofont.deriveFont(AffineTransform
404                           .getScaleInstance(fmScaling, 1.0)));
405                   // and update the label's width to reflect the scaling.
406                   fmWidth = charWidth;
407                 }
408               }
409             }
410             else
411             {
412               fmWidth = fm
413                       .charWidth(row_annotations[column].displayCharacter
414                               .charAt(0));
415             }
416             charOffset = (int) ((charWidth - fmWidth) / 2f);
417
418             if (row_annotations[column].colour == null)
419               g.setColor(Color.black);
420             else
421               g.setColor(row_annotations[column].colour);
422
423             if (column == 0 || row.graph > 0)
424             {
425               g.drawString(row_annotations[column].displayCharacter,
426                       (x * charWidth) + charOffset, y + iconOffset);
427             }
428             else if (row_annotations[column - 1] == null
429                     || (labelAllCols
430                             || !row_annotations[column].displayCharacter
431                                     .equals(row_annotations[column - 1].displayCharacter) || (row_annotations[column].displayCharacter
432                             .length() < 2 && row_annotations[column].secondaryStructure == ' ')))
433             {
434               g.drawString(row_annotations[column].displayCharacter, x
435                       * charWidth + charOffset, y + iconOffset);
436             }
437             g.setFont(ofont);
438           }
439         }
440         if (row.hasIcons)
441         {
442           char ss = validRes ? row_annotations[column].secondaryStructure
443                   : ' ';
444           if (ss == 'S')
445           {
446             // distinguish between forward/backward base-pairing
447             if (row_annotations[column].displayCharacter.indexOf(')') > -1)
448             {
449               ss = 's';
450             }
451           }
452           if (!validRes || (ss != lastSS))
453           {
454             if (x > -1)
455             {
456               switch (lastSS)
457               {
458               case 'H':
459                 drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset, startRes,
460                         column, validRes, validEnd);
461                 break;
462
463               case 'E':
464                 drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset, startRes,
465                         column, validRes, validEnd);
466                 break;
467
468               case 'S': // Stem case for RNA secondary structure
469               case 's': // and opposite direction
470                 drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset, startRes,
471                         column, validRes, validEnd);
472                 break;
473
474               default:
475                 g.setColor(Color.gray);
476                 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth)
477                         - lastSSX, 2);
478
479                 break;
480               }
481             }
482             if (validRes)
483             {
484               lastSS = ss;
485             }
486             else
487             {
488               lastSS = ' ';
489             }
490             if (x > -1)
491             {
492               lastSSX = (x * charWidth);
493             }
494           }
495         }
496         column++;
497         x++;
498       }
499       if (column >= row_annotations.length)
500       {
501         column = row_annotations.length - 1;
502         validEnd = false;
503       }
504       else
505       {
506         validEnd = true;
507       }
508       if ((row_annotations == null) || (row_annotations.length <= column)
509               || (row_annotations[column] == null))
510       {
511         validRes = false;
512       }
513       else
514       {
515         validRes = true;
516       }
517
518       // x ++;
519
520       if (row.hasIcons)
521       {
522         switch (lastSS)
523         {
524         case 'H':
525           drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset, startRes,
526                   column, validRes, validEnd);
527           break;
528
529         case 'E':
530           drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset, startRes,
531                   column, validRes, validEnd);
532           break;
533         case 's':
534         case 'S': // Stem case for RNA secondary structure
535           drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset, startRes,
536                   column, validRes, validEnd);
537           break;
538         default:
539           drawGlyphLine(g, row_annotations, lastSSX, x, y, iconOffset, startRes,
540                   column, validRes, validEnd);
541           break;
542         }
543       }
544
545       if (row.graph > 0 && row.graphHeight > 0)
546       {
547         if (row.graph == AlignmentAnnotation.LINE_GRAPH)
548         {
549           if (row.graphGroup > -1 && !graphGroupDrawn[row.graphGroup])
550           {
551             float groupmax = -999999, groupmin = 9999999;
552             for (int gg = 0; gg < aa.length; gg++)
553             {
554               if (aa[gg].graphGroup != row.graphGroup)
555               {
556                 continue;
557               }
558
559               if (aa[gg] != row)
560               {
561                 aa[gg].visible = false;
562               }
563
564               if (aa[gg].graphMax > groupmax)
565               {
566                 groupmax = aa[gg].graphMax;
567               }
568               if (aa[gg].graphMin < groupmin)
569               {
570                 groupmin = aa[gg].graphMin;
571               }
572             }
573
574             for (int gg = 0; gg < aa.length; gg++)
575             {
576               if (aa[gg].graphGroup == row.graphGroup)
577               {
578                 drawLineGraph(g, aa[gg], aa[gg].annotations, startRes, endRes, y, groupmin,
579                         groupmax, row.graphHeight);
580               }
581             }
582
583             graphGroupDrawn[row.graphGroup] = true;
584           }
585           else
586           {
587             drawLineGraph(g, row, row_annotations, startRes, endRes, y, row.graphMin,
588                     row.graphMax, row.graphHeight);
589           }
590         }
591         else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
592         {
593           drawBarGraph(g, row, row_annotations, startRes, endRes, row.graphMin,
594                   row.graphMax, y);
595         }
596       }
597
598       if (row.graph > 0 && row.hasText)
599       {
600         y += charHeight;
601       }
602
603       if (row.graph == 0)
604       {
605         y += aa[i].height;
606       }
607     }
608     return !usedFaded;
609   }
610
611   private final Color GLYPHLINE_COLOR = Color.gray;
612
613   private final Color SHEET_COLOUR = Color.green;
614
615   private final Color HELIX_COLOUR = Color.red;
616
617   private final Color STEM_COLOUR = Color.blue;
618
619   public void drawGlyphLine(Graphics g, Annotation[] row,
620           int lastSSX, int x, int y, int iconOffset, int startRes,
621           int column, boolean validRes, boolean validEnd)
622   {
623     g.setColor(GLYPHLINE_COLOR);
624     g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
625   }
626
627   public void drawSheetAnnot(Graphics g, Annotation[] row,
628           int lastSSX, int x, int y, int iconOffset, int startRes,
629           int column, boolean validRes, boolean validEnd)
630   {
631     g.setColor(SHEET_COLOUR);
632
633     if (!validEnd || !validRes || row==null || row[column] == null
634             || row[column].secondaryStructure != 'E')
635     {
636       g.fillRect(lastSSX, y + 4 + iconOffset,
637               (x * charWidth) - lastSSX - 4, 7);
638       g.fillPolygon(new int[]
639       { (x * charWidth) - 4, (x * charWidth) - 4, (x * charWidth) },
640               new int[]
641               { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset },
642               3);
643     }
644     else
645     {
646       g.fillRect(lastSSX, y + 4 + iconOffset,
647               (x + 1) * charWidth - lastSSX, 7);
648     }
649
650   }
651
652   public void drawHelixAnnot(Graphics g, Annotation[] row,
653           int lastSSX, int x, int y, int iconOffset, int startRes,
654           int column, boolean validRes, boolean validEnd)
655   {
656     g.setColor(HELIX_COLOUR);
657
658     int sCol = (lastSSX / charWidth) + startRes;
659     int x1 = lastSSX;
660     int x2 = (x * charWidth);
661
662     if (MAC)
663     {
664       int ofs = charWidth / 2;
665       // Off by 1 offset when drawing rects and ovals
666       // to offscreen image on the MAC
667       g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1, 8, 8, 8);
668       if (sCol == 0 || row[sCol - 1] == null
669               || row[sCol - 1].secondaryStructure != 'H')
670       {
671       }
672       else
673       {
674         // g.setColor(Color.orange);
675         g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1 - ofs + 1, 8,
676                 0, 0);
677       }
678       if (!validRes || row[column] == null
679               || row[column].secondaryStructure != 'H')
680       {
681
682       }
683       else
684       {
685         // g.setColor(Color.magenta);
686         g.fillRoundRect(lastSSX + ofs, y + 4 + iconOffset, x2 - x1 - ofs
687                 + 1, 8, 0, 0);
688
689       }
690
691       return;
692     }
693
694     if (sCol == 0 || row[sCol - 1] == null
695             || row[sCol - 1].secondaryStructure != 'H')
696     {
697       g.fillArc(lastSSX, y + 4 + iconOffset, charWidth, 8, 90, 180);
698       x1 += charWidth / 2;
699     }
700
701     if (!validRes || row[column] == null
702             || row[column].secondaryStructure != 'H')
703     {
704       g.fillArc((x * charWidth) - charWidth, y + 4 + iconOffset, charWidth,
705               8, 270, 180);
706       x2 -= charWidth / 2;
707     }
708
709     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
710   }
711
712   public void drawLineGraph(Graphics g, AlignmentAnnotation _aa, Annotation[] aa_annotations, int sRes,
713           int eRes, int y, float min, float max, int graphHeight)
714   {
715     if (sRes > aa_annotations.length)
716     {
717       return;
718     }
719
720     int x = 0;
721
722     // Adjustment for fastpaint to left
723     if (eRes < endRes)
724     {
725       eRes++;
726     }
727
728     eRes = Math.min(eRes, aa_annotations.length);
729
730     if (sRes == 0)
731     {
732       x++;
733     }
734
735     int y1 = y, y2 = y;
736     float range = max - min;
737
738     // //Draw origin
739     if (min < 0)
740     {
741       y2 = y - (int) ((0 - min / range) * graphHeight);
742     }
743
744     g.setColor(Color.gray);
745     g.drawLine(x - charWidth, y2, (eRes - sRes + 1) * charWidth, y2);
746
747     eRes = Math.min(eRes, aa_annotations.length);
748
749     int column;
750     int aaMax = aa_annotations.length - 1;
751
752     while (x < eRes - sRes)
753     {
754       column = sRes + x;
755       if (hasHiddenColumns)
756       {
757         column = columnSelection.adjustForHiddenColumns(column);
758       }
759
760       if (column > aaMax)
761       {
762         break;
763       }
764
765       if (aa_annotations[column] == null
766               || aa_annotations[column - 1] == null)
767       {
768         x++;
769         continue;
770       }
771
772       if (aa_annotations[column].colour == null)
773         g.setColor(Color.black);
774       else
775         g.setColor(aa_annotations[column].colour);
776
777       y1 = y
778               - (int) (((aa_annotations[column - 1].value - min) / range) * graphHeight);
779       y2 = y
780               - (int) (((aa_annotations[column].value - min) / range) * graphHeight);
781
782       g.drawLine(x * charWidth - charWidth / 2, y1, x * charWidth
783               + charWidth / 2, y2);
784       x++;
785     }
786
787     if (_aa.threshold != null)
788     {
789       g.setColor(_aa.threshold.colour);
790       Graphics2D g2 = (Graphics2D) g;
791       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
792               BasicStroke.JOIN_ROUND, 3f, new float[]
793               { 5f, 3f }, 0f));
794
795       y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
796       g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
797       g2.setStroke(new BasicStroke());
798     }
799   }
800
801   public void drawBarGraph(Graphics g, AlignmentAnnotation _aa, Annotation[] aa_annotations, int sRes,
802           int eRes, float min, float max, int y)
803   {
804     if (sRes > aa_annotations.length)
805     {
806       return;
807     }
808     Font ofont = g.getFont();
809     eRes = Math.min(eRes, aa_annotations.length);
810
811     int x = 0, y1 = y, y2 = y;
812
813     float range = max - min;
814
815     if (min < 0)
816     {
817       y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
818     }
819
820     g.setColor(Color.gray);
821
822     g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
823
824     int column;
825     int aaMax = aa_annotations.length - 1;
826     boolean renderHistogram = true, renderProfile = true, normaliseProfile = false;
827     // if (aa.autoCalculated && aa.label.startsWith("Consensus"))
828     {
829       // TODO: generalise this to have render styles for consensus/profile data
830       if (_aa.groupRef != null)
831       {
832         renderHistogram = _aa.groupRef.isShowConsensusHistogram();
833         renderProfile = _aa.groupRef.isShowSequenceLogo();
834         normaliseProfile = _aa.groupRef.isNormaliseSequenceLogo();
835       }
836       else
837       {
838         renderHistogram = av_renderHistogram;
839         renderProfile = av_renderProfile;
840         normaliseProfile = av_normaliseProfile;
841       }
842     }
843     while (x < eRes - sRes)
844     {
845       column = sRes + x;
846       if (hasHiddenColumns)
847       {
848         column = columnSelection.adjustForHiddenColumns(column);
849       }
850
851       if (column > aaMax)
852       {
853         break;
854       }
855
856       if (aa_annotations[column] == null)
857       {
858         x++;
859         continue;
860       }
861       if (aa_annotations[column].colour == null)
862         g.setColor(Color.black);
863       else
864         g.setColor(aa_annotations[column].colour);
865
866       y1 = y
867               - (int) (((aa_annotations[column].value - min) / (range)) * _aa.graphHeight);
868
869       if (renderHistogram)
870       {
871         if (y1 - y2 > 0)
872         {
873           g.fillRect(x * charWidth, y2, charWidth, y1 - y2);
874         }
875         else
876         {
877           g.fillRect(x * charWidth, y1, charWidth, y2 - y1);
878         }
879       }
880       // draw profile if available
881       if (renderProfile)
882       {
883
884         int profl[] = getProfileFor(_aa, column);
885         // just try to draw the logo if profl is not null
886         if (profl != null && profl[1] != 0)
887         {
888           float ht = normaliseProfile ? y - _aa.graphHeight : y1;
889           double htn = normaliseProfile ? _aa.graphHeight : (y2 - y1);// aa.graphHeight;
890           double hght;
891           float wdth;
892           double ht2 = 0;
893           char[] dc;
894
895           /**
896            * profl.length == 74 indicates that the profile of a secondary
897            * structure conservation row was accesed. Therefore dc gets length 2,
898            * to have space for a basepair instead of just a single nucleotide
899            */
900           if (profl.length == 74)
901           {
902             dc = new char[2];
903           }
904           else
905           {
906             dc = new char[1];
907           }
908           LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
909           double scale = 1f / (normaliseProfile ? profl[1] : 100f);
910           float ofontHeight = 1f / lm.getAscent();// magnify to fill box
911           double scl = 0.0;
912           for (int c = 2; c < profl[0];)
913           {
914             dc[0] = (char) profl[c++];
915
916             if (_aa.label.startsWith("StrucConsensus"))
917             {
918               dc[1] = (char) profl[c++];
919             }
920
921             wdth = charWidth;
922             wdth /= fm.charsWidth(dc, 0, dc.length);
923
924             ht += scl;
925             {
926               scl = htn * scale * profl[c++];
927               lm = ofont.getLineMetrics(dc, 0, 1, g.getFontMetrics()
928                       .getFontRenderContext());
929               g.setFont(ofont.deriveFont(AffineTransform.getScaleInstance(
930                       wdth, scl / lm.getAscent())));
931               lm = g.getFontMetrics().getLineMetrics(dc, 0, 1, g);
932
933               // Debug - render boxes around characters
934               // g.setColor(Color.red);
935               // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
936               // (int)(scl));
937               // g.setColor(profcolour.findColour(dc[0]).darker());
938               g.setColor(profcolour.findColour(dc[0], column,null));
939
940               hght = (ht + (scl - lm.getDescent() - lm.getBaselineOffsets()[lm
941                       .getBaselineIndex()]));
942
943               g.drawChars(dc, 0, dc.length, x * charWidth, (int) hght);
944             }
945           }
946           g.setFont(ofont);
947         }
948       }
949       x++;
950     }
951     if (_aa.threshold != null)
952     {
953       g.setColor(_aa.threshold.colour);
954       Graphics2D g2 = (Graphics2D) g;
955       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
956               BasicStroke.JOIN_ROUND, 3f, new float[]
957               { 5f, 3f }, 0f));
958
959       y2 = (int) (y - ((_aa.threshold.value - min) / range) * _aa.graphHeight);
960       g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
961       g2.setStroke(new BasicStroke());
962     }
963   }
964
965   // used by overview window
966   public void drawGraph(Graphics g, AlignmentAnnotation _aa, Annotation[] aa_annotations, int width,
967           int y, int sRes, int eRes)
968   {
969     eRes = Math.min(eRes, aa_annotations.length);
970     g.setColor(Color.white);
971     g.fillRect(0, 0, width, y);
972     g.setColor(new Color(0, 0, 180));
973
974     int x = 0, height;
975
976     for (int j = sRes; j < eRes; j++)
977     {
978       if (aa_annotations[j] != null)
979       {
980         if (aa_annotations[j].colour == null)
981           g.setColor(Color.black);
982         else
983           g.setColor(aa_annotations[j].colour);
984
985         height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
986         if (height > y)
987         {
988           height = y;
989         }
990
991         g.fillRect(x, y - height, charWidth, height);
992       }
993       x += charWidth;
994     }
995   }
996 }