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