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