JAL-967, JAL-1114, JAL-1115 - modified render and calculation api to work on bare...
[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 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    * DOCUMENT ME!
216    * 
217    * @param annotationPanel
218    *          TODO
219    * @param g
220    *          DOCUMENT ME!
221    * @param startRes
222    *          DOCUMENT ME!
223    * @param endRes
224    *          DOCUMENT ME!
225    */
226   public void drawComponent(AwtRenderPanelI annotPanel, AlignViewportI av,
227           Graphics g, int activeRow, int startRes, int endRes)
228   {
229     // NOTES:
230     // AnnotationPanel needs to implement: ImageObserver, access to
231     // AlignViewport
232     updateFromAwtRenderPanel(annotPanel, av);
233     fm = g.getFontMetrics();
234     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
235
236     int x = 0, y = 0;
237     int column = 0;
238     char lastSS;
239     int lastSSX;
240     int iconOffset = 0;
241     boolean validRes = false;
242     boolean validEnd = false;
243     boolean labelAllCols = false;
244     boolean centreColLabels, centreColLabelsDef = av
245             .getCentreColumnLabels();
246     boolean scaleColLabel = false;
247     boolean[] graphGroupDrawn = new boolean[aa.length];
248     int charOffset = 0; // offset for a label
249     float fmWidth, fmScaling = 1f; // scaling for a label to fit it into a
250     // column.
251     Font ofont = g.getFont();
252     // \u03B2 \u03B1
253     for (int i = 0; i < aa.length; i++)
254     {
255       AlignmentAnnotation row = aa[i];
256       Annotation[] row_annotations=row.annotations;
257       if (!row.visible)
258       {
259         continue;
260       }
261       centreColLabels = row.centreColLabels || centreColLabelsDef;
262       labelAllCols = row.showAllColLabels;
263       scaleColLabel = row.scaleColLabel;
264       lastSS = ' ';
265       lastSSX = 0;
266       if (row.graph > 0)
267       {
268         if (row.graphGroup > -1 && graphGroupDrawn[row.graphGroup])
269         {
270           continue;
271         }
272
273         // this is so that we draw the characters below the graph
274         y += row.height;
275
276         if (row.hasText)
277         {
278           iconOffset = charHeight - fm.getDescent();
279           y -= charHeight;
280         }
281       }
282       else if (row.hasText)
283       {
284         iconOffset = charHeight - fm.getDescent();
285
286       }
287       else
288       {
289         iconOffset = 0;
290       }
291
292       if (aa[i].autoCalculated && av.isCalculationInProgress(aa[i]))
293       {
294         y += charHeight;
295
296         g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0, y
297                 - row.height, imgWidth, y, annotationPanel);
298         g.setColor(Color.black);
299         // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
300
301         continue;
302       }
303
304       /*
305        * else if (annotationPanel.av.updatingConservation &&
306        * aa[i].label.equals("Conservation")) {
307        * 
308        * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
309        * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
310        * annotationPanel.imgWidth, y, annotationPanel);
311        * 
312        * g.setColor(Color.black); //
313        * g.drawString("Calculating Conservation.....",20, y-row.height/2);
314        * 
315        * continue; } else if (annotationPanel.av.updatingConservation &&
316        * aa[i].label.equals("Quality")) {
317        * 
318        * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
319        * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
320        * annotationPanel.imgWidth, y, annotationPanel); g.setColor(Color.black);
321        * // / g.drawString("Calculating Quality....",20, y-row.height/2);
322        * 
323        * continue; }
324        */
325       // first pass sets up state for drawing continuation from left-hand column
326       // of startRes
327       x = (startRes == 0) ? 0 : -1;
328       while (x < endRes - startRes)
329       {
330         if (hasHiddenColumns)
331         {
332           column = columnSelection.adjustForHiddenColumns(startRes + x);
333           if (column > row_annotations.length - 1)
334           {
335             break;
336           }
337         }
338         else
339         {
340           column = startRes + x;
341         }
342
343         if ((row_annotations == null) || (row_annotations.length <= column)
344                 || (row_annotations[column] == null))
345         {
346           validRes = false;
347         }
348         else
349         {
350           validRes = true;
351         }
352         if (x > -1)
353         {
354           if (activeRow == i)
355           {
356             g.setColor(Color.red);
357
358             if (columnSelection != null)
359             {
360               for (int n = 0; n < columnSelection.size(); n++)
361               {
362                 int v = columnSelection.columnAt(n);
363
364                 if (v == column)
365                 {
366                   g.fillRect(x * charWidth, y, charWidth, charHeight);
367                 }
368               }
369             }
370           }
371           if (!row.isValidStruc())
372           {
373             g.setColor(Color.orange);
374             g.fillRect((int) row.getInvalidStrucPos() * charWidth, y,
375                     charWidth, charHeight);
376           }
377           if (validCharWidth
378                   && validRes
379                   && row_annotations[column].displayCharacter != null
380                   && (row_annotations[column].displayCharacter.length() > 0))
381           {
382
383             if (centreColLabels || scaleColLabel)
384             {
385               fmWidth = fm.charsWidth(
386                       row_annotations[column].displayCharacter
387                               .toCharArray(), 0,
388                       row_annotations[column].displayCharacter.length());
389
390               if (scaleColLabel)
391               {
392                 // justify the label and scale to fit in column
393                 if (fmWidth > charWidth)
394                 {
395                   // scale only if the current font isn't already small enough
396                   fmScaling = charWidth;
397                   fmScaling /= fmWidth;
398                   g.setFont(ofont.deriveFont(AffineTransform
399                           .getScaleInstance(fmScaling, 1.0)));
400                   // and update the label's width to reflect the scaling.
401                   fmWidth = charWidth;
402                 }
403               }
404             }
405             else
406             {
407               fmWidth = fm
408                       .charWidth(row_annotations[column].displayCharacter
409                               .charAt(0));
410             }
411             charOffset = (int) ((charWidth - fmWidth) / 2f);
412
413             if (row_annotations[column].colour == null)
414               g.setColor(Color.black);
415             else
416               g.setColor(row_annotations[column].colour);
417
418             if (column == 0 || row.graph > 0)
419             {
420               g.drawString(row_annotations[column].displayCharacter,
421                       (x * charWidth) + charOffset, y + iconOffset);
422             }
423             else if (row_annotations[column - 1] == null
424                     || (labelAllCols
425                             || !row_annotations[column].displayCharacter
426                                     .equals(row_annotations[column - 1].displayCharacter) || (row_annotations[column].displayCharacter
427                             .length() < 2 && row_annotations[column].secondaryStructure == ' ')))
428             {
429               g.drawString(row_annotations[column].displayCharacter, x
430                       * charWidth + charOffset, y + iconOffset);
431             }
432             g.setFont(ofont);
433           }
434         }
435         if (row.hasIcons)
436         {
437           char ss = validRes ? row_annotations[column].secondaryStructure
438                   : ' ';
439           if (ss == 'S')
440           {
441             // distinguish between forward/backward base-pairing
442             if (row_annotations[column].displayCharacter.indexOf(')') > -1)
443             {
444               ss = 's';
445             }
446           }
447           if (!validRes || (ss != lastSS))
448           {
449             if (x > -1)
450             {
451               switch (lastSS)
452               {
453               case 'H':
454                 drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset, startRes,
455                         column, validRes, validEnd);
456                 break;
457
458               case 'E':
459                 drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset, startRes,
460                         column, validRes, validEnd);
461                 break;
462
463               case 'S': // Stem case for RNA secondary structure
464               case 's': // and opposite direction
465                 drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset, startRes,
466                         column, validRes, validEnd);
467                 break;
468
469               default:
470                 g.setColor(Color.gray);
471                 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth)
472                         - lastSSX, 2);
473
474                 break;
475               }
476             }
477             if (validRes)
478             {
479               lastSS = ss;
480             }
481             else
482             {
483               lastSS = ' ';
484             }
485             if (x > -1)
486             {
487               lastSSX = (x * charWidth);
488             }
489           }
490         }
491         column++;
492         x++;
493       }
494       if (column >= row_annotations.length)
495       {
496         column = row_annotations.length - 1;
497         validEnd = false;
498       }
499       else
500       {
501         validEnd = true;
502       }
503       if ((row_annotations == null) || (row_annotations.length <= column)
504               || (row_annotations[column] == null))
505       {
506         validRes = false;
507       }
508       else
509       {
510         validRes = true;
511       }
512
513       // x ++;
514
515       if (row.hasIcons)
516       {
517         switch (lastSS)
518         {
519         case 'H':
520           drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset, startRes,
521                   column, validRes, validEnd);
522           break;
523
524         case 'E':
525           drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset, startRes,
526                   column, validRes, validEnd);
527           break;
528         case 's':
529         case 'S': // Stem case for RNA secondary structure
530           drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset, startRes,
531                   column, validRes, validEnd);
532           break;
533         default:
534           drawGlyphLine(g, row_annotations, lastSSX, x, y, iconOffset, startRes,
535                   column, validRes, validEnd);
536           break;
537         }
538       }
539
540       if (row.graph > 0 && row.graphHeight > 0)
541       {
542         if (row.graph == AlignmentAnnotation.LINE_GRAPH)
543         {
544           if (row.graphGroup > -1 && !graphGroupDrawn[row.graphGroup])
545           {
546             float groupmax = -999999, groupmin = 9999999;
547             for (int gg = 0; gg < aa.length; gg++)
548             {
549               if (aa[gg].graphGroup != row.graphGroup)
550               {
551                 continue;
552               }
553
554               if (aa[gg] != row)
555               {
556                 aa[gg].visible = false;
557               }
558
559               if (aa[gg].graphMax > groupmax)
560               {
561                 groupmax = aa[gg].graphMax;
562               }
563               if (aa[gg].graphMin < groupmin)
564               {
565                 groupmin = aa[gg].graphMin;
566               }
567             }
568
569             for (int gg = 0; gg < aa.length; gg++)
570             {
571               if (aa[gg].graphGroup == row.graphGroup)
572               {
573                 drawLineGraph(g, aa[gg], aa[gg].annotations, startRes, endRes, y, groupmin,
574                         groupmax, row.graphHeight);
575               }
576             }
577
578             graphGroupDrawn[row.graphGroup] = true;
579           }
580           else
581           {
582             drawLineGraph(g, row, row_annotations, startRes, endRes, y, row.graphMin,
583                     row.graphMax, row.graphHeight);
584           }
585         }
586         else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
587         {
588           drawBarGraph(g, row, row_annotations, startRes, endRes, row.graphMin,
589                   row.graphMax, y);
590         }
591       }
592
593       if (row.graph > 0 && row.hasText)
594       {
595         y += charHeight;
596       }
597
598       if (row.graph == 0)
599       {
600         y += aa[i].height;
601       }
602     }
603   }
604
605   private Color GLYPHLINE_COLOR = Color.gray;
606
607   private Color SHEET_COLOUR = Color.green;
608
609   private Color HELIX_COLOUR = Color.red;
610
611   private Color STEM_COLOUR = Color.blue;
612
613   public void drawGlyphLine(Graphics g, Annotation[] row,
614           int lastSSX, int x, int y, int iconOffset, int startRes,
615           int column, boolean validRes, boolean validEnd)
616   {
617     g.setColor(GLYPHLINE_COLOR);
618     g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
619   }
620
621   public void drawSheetAnnot(Graphics g, Annotation[] row,
622           int lastSSX, int x, int y, int iconOffset, int startRes,
623           int column, boolean validRes, boolean validEnd)
624   {
625     g.setColor(SHEET_COLOUR);
626
627     if (!validEnd || !validRes || row==null || row[column] == null
628             || row[column].secondaryStructure != 'E')
629     {
630       g.fillRect(lastSSX, y + 4 + iconOffset,
631               (x * charWidth) - lastSSX - 4, 7);
632       g.fillPolygon(new int[]
633       { (x * charWidth) - 4, (x * charWidth) - 4, (x * charWidth) },
634               new int[]
635               { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset },
636               3);
637     }
638     else
639     {
640       g.fillRect(lastSSX, y + 4 + iconOffset,
641               (x + 1) * charWidth - lastSSX, 7);
642     }
643
644   }
645
646   public void drawHelixAnnot(Graphics g, Annotation[] row,
647           int lastSSX, int x, int y, int iconOffset, int startRes,
648           int column, boolean validRes, boolean validEnd)
649   {
650     g.setColor(HELIX_COLOUR);
651
652     int sCol = (lastSSX / charWidth) + startRes;
653     int x1 = lastSSX;
654     int x2 = (x * charWidth);
655
656     if (MAC)
657     {
658       int ofs = charWidth / 2;
659       // Off by 1 offset when drawing rects and ovals
660       // to offscreen image on the MAC
661       g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1, 8, 8, 8);
662       if (sCol == 0 || row[sCol - 1] == null
663               || row[sCol - 1].secondaryStructure != 'H')
664       {
665       }
666       else
667       {
668         // g.setColor(Color.orange);
669         g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1 - ofs + 1, 8,
670                 0, 0);
671       }
672       if (!validRes || row[column] == null
673               || row[column].secondaryStructure != 'H')
674       {
675
676       }
677       else
678       {
679         // g.setColor(Color.magenta);
680         g.fillRoundRect(lastSSX + ofs, y + 4 + iconOffset, x2 - x1 - ofs
681                 + 1, 8, 0, 0);
682
683       }
684
685       return;
686     }
687
688     if (sCol == 0 || row[sCol - 1] == null
689             || row[sCol - 1].secondaryStructure != 'H')
690     {
691       g.fillArc(lastSSX, y + 4 + iconOffset, charWidth, 8, 90, 180);
692       x1 += charWidth / 2;
693     }
694
695     if (!validRes || row[column] == null
696             || row[column].secondaryStructure != 'H')
697     {
698       g.fillArc((x * charWidth) - charWidth, y + 4 + iconOffset, charWidth,
699               8, 270, 180);
700       x2 -= charWidth / 2;
701     }
702
703     g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
704   }
705
706   public void drawLineGraph(Graphics g, AlignmentAnnotation _aa, Annotation[] aa_annotations, int sRes,
707           int eRes, int y, float min, float max, int graphHeight)
708   {
709     if (sRes > aa_annotations.length)
710     {
711       return;
712     }
713
714     int x = 0;
715
716     // Adjustment for fastpaint to left
717     if (eRes < endRes)
718     {
719       eRes++;
720     }
721
722     eRes = Math.min(eRes, aa_annotations.length);
723
724     if (sRes == 0)
725     {
726       x++;
727     }
728
729     int y1 = y, y2 = y;
730     float range = max - min;
731
732     // //Draw origin
733     if (min < 0)
734     {
735       y2 = y - (int) ((0 - min / range) * graphHeight);
736     }
737
738     g.setColor(Color.gray);
739     g.drawLine(x - charWidth, y2, (eRes - sRes + 1) * charWidth, y2);
740
741     eRes = Math.min(eRes, aa_annotations.length);
742
743     int column;
744     int aaMax = aa_annotations.length - 1;
745
746     while (x < eRes - sRes)
747     {
748       column = sRes + x;
749       if (hasHiddenColumns)
750       {
751         column = columnSelection.adjustForHiddenColumns(column);
752       }
753
754       if (column > aaMax)
755       {
756         break;
757       }
758
759       if (aa_annotations[column] == null
760               || aa_annotations[column - 1] == null)
761       {
762         x++;
763         continue;
764       }
765
766       if (aa_annotations[column].colour == null)
767         g.setColor(Color.black);
768       else
769         g.setColor(aa_annotations[column].colour);
770
771       y1 = y
772               - (int) (((aa_annotations[column - 1].value - min) / range) * graphHeight);
773       y2 = y
774               - (int) (((aa_annotations[column].value - min) / range) * graphHeight);
775
776       g.drawLine(x * charWidth - charWidth / 2, y1, x * charWidth
777               + charWidth / 2, y2);
778       x++;
779     }
780
781     if (_aa.threshold != null)
782     {
783       g.setColor(_aa.threshold.colour);
784       Graphics2D g2 = (Graphics2D) g;
785       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
786               BasicStroke.JOIN_ROUND, 3f, new float[]
787               { 5f, 3f }, 0f));
788
789       y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
790       g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
791       g2.setStroke(new BasicStroke());
792     }
793   }
794
795   public void drawBarGraph(Graphics g, AlignmentAnnotation _aa, Annotation[] aa_annotations, int sRes,
796           int eRes, float min, float max, int y)
797   {
798     if (sRes > aa_annotations.length)
799     {
800       return;
801     }
802     Font ofont = g.getFont();
803     eRes = Math.min(eRes, aa_annotations.length);
804
805     int x = 0, y1 = y, y2 = y;
806
807     float range = max - min;
808
809     if (min < 0)
810     {
811       y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
812     }
813
814     g.setColor(Color.gray);
815
816     g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
817
818     int column;
819     int aaMax = aa_annotations.length - 1;
820     boolean renderHistogram = true, renderProfile = true, normaliseProfile = false;
821     // if (aa.autoCalculated && aa.label.startsWith("Consensus"))
822     {
823       // TODO: generalise this to have render styles for consensus/profile data
824       if (_aa.groupRef != null)
825       {
826         renderHistogram = _aa.groupRef.isShowConsensusHistogram();
827         renderProfile = _aa.groupRef.isShowSequenceLogo();
828         normaliseProfile = _aa.groupRef.isNormaliseSequenceLogo();
829       }
830       else
831       {
832         renderHistogram = av_renderHistogram;
833         renderProfile = av_renderProfile;
834         normaliseProfile = av_normaliseProfile;
835       }
836     }
837     while (x < eRes - sRes)
838     {
839       column = sRes + x;
840       if (hasHiddenColumns)
841       {
842         column = columnSelection.adjustForHiddenColumns(column);
843       }
844
845       if (column > aaMax)
846       {
847         break;
848       }
849
850       if (aa_annotations[column] == null)
851       {
852         x++;
853         continue;
854       }
855       if (aa_annotations[column].colour == null)
856         g.setColor(Color.black);
857       else
858         g.setColor(aa_annotations[column].colour);
859
860       y1 = y
861               - (int) (((aa_annotations[column].value - min) / (range)) * _aa.graphHeight);
862
863       if (renderHistogram)
864       {
865         if (y1 - y2 > 0)
866         {
867           g.fillRect(x * charWidth, y2, charWidth, y1 - y2);
868         }
869         else
870         {
871           g.fillRect(x * charWidth, y1, charWidth, y2 - y1);
872         }
873       }
874       // draw profile if available
875       if (renderProfile)
876       {
877
878         int profl[] = getProfileFor(_aa, column);
879         // just try to draw the logo if profl is not null
880         if (profl != null && profl[1] != 0)
881         {
882           float ht = normaliseProfile ? y - _aa.graphHeight : y1;
883           double htn = normaliseProfile ? _aa.graphHeight : (y2 - y1);// aa.graphHeight;
884           double hght;
885           float wdth;
886           double ht2 = 0;
887           char[] dc;
888
889           /**
890            * profl.length == 74 indicates that the profile of a secondary
891            * structure conservation row was accesed. Therefore dc gets length 2,
892            * to have space for a basepair instead of just a single nucleotide
893            */
894           if (profl.length == 74)
895           {
896             dc = new char[2];
897           }
898           else
899           {
900             dc = new char[1];
901           }
902           LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
903           double scale = 1f / (normaliseProfile ? profl[1] : 100f);
904           float ofontHeight = 1f / lm.getAscent();// magnify to fill box
905           double scl = 0.0;
906           for (int c = 2; c < profl[0];)
907           {
908             dc[0] = (char) profl[c++];
909
910             if (_aa.label.startsWith("StrucConsensus"))
911             {
912               dc[1] = (char) profl[c++];
913             }
914
915             wdth = charWidth;
916             wdth /= fm.charsWidth(dc, 0, dc.length);
917
918             ht += scl;
919             {
920               scl = htn * scale * profl[c++];
921               lm = ofont.getLineMetrics(dc, 0, 1, g.getFontMetrics()
922                       .getFontRenderContext());
923               g.setFont(ofont.deriveFont(AffineTransform.getScaleInstance(
924                       wdth, scl / lm.getAscent())));
925               lm = g.getFontMetrics().getLineMetrics(dc, 0, 1, g);
926
927               // Debug - render boxes around characters
928               // g.setColor(Color.red);
929               // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
930               // (int)(scl));
931               // g.setColor(profcolour.findColour(dc[0]).darker());
932               g.setColor(profcolour.findColour(dc[0], column,null));
933
934               hght = (ht + (scl - lm.getDescent() - lm.getBaselineOffsets()[lm
935                       .getBaselineIndex()]));
936
937               g.drawChars(dc, 0, dc.length, x * charWidth, (int) hght);
938             }
939           }
940           g.setFont(ofont);
941         }
942       }
943       x++;
944     }
945     if (_aa.threshold != null)
946     {
947       g.setColor(_aa.threshold.colour);
948       Graphics2D g2 = (Graphics2D) g;
949       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
950               BasicStroke.JOIN_ROUND, 3f, new float[]
951               { 5f, 3f }, 0f));
952
953       y2 = (int) (y - ((_aa.threshold.value - min) / range) * _aa.graphHeight);
954       g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
955       g2.setStroke(new BasicStroke());
956     }
957   }
958
959   // used by overview window
960   public void drawGraph(Graphics g, AlignmentAnnotation _aa, Annotation[] aa_annotations, int width,
961           int y, int sRes, int eRes)
962   {
963     eRes = Math.min(eRes, aa_annotations.length);
964     g.setColor(Color.white);
965     g.fillRect(0, 0, width, y);
966     g.setColor(new Color(0, 0, 180));
967
968     int x = 0, height;
969
970     for (int j = sRes; j < eRes; j++)
971     {
972       if (aa_annotations[j] != null)
973       {
974         if (aa_annotations[j].colour == null)
975           g.setColor(Color.black);
976         else
977           g.setColor(aa_annotations[j].colour);
978
979         height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
980         if (height > y)
981         {
982           height = y;
983         }
984
985         g.fillRect(x, y - height, charWidth, height);
986       }
987       x += charWidth;
988     }
989   }
990 }