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