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