1 package jalview.renderer;
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;
10 import java.awt.BasicStroke;
11 import java.awt.Color;
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;
22 import com.stevesoft.pat.Regex;
24 public class AnnotationRenderer
27 public AnnotationRenderer()
29 // TODO Auto-generated constructor stub
32 public void drawStemAnnot(Graphics g, AlignmentAnnotation row, int lastSSX, int x, int y, int iconOffset, int startRes, int column, boolean validRes, boolean validEnd)
34 g.setColor(STEM_COLOUR);
35 int sCol = (lastSSX / charWidth) + startRes;
37 int x2 = (x * charWidth);
38 Regex closeparen = new Regex("(\\))");
40 String dc = (column == 0 || row.annotations[column-1]==null) ? ""
41 : row.annotations[column - 1].displayCharacter;
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))
53 // if (validRes && column>1 && row.annotations[column-2]!=null &&
54 // dc.equals(row.annotations[column-2].displayCharacter))
56 g.fillPolygon(new int[]
57 { lastSSX + 5, lastSSX + 5, lastSSX }, new int[]
58 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
68 // display a forward arrow
71 g.fillPolygon(new int[]
72 { x2 - 5, x2 - 5, x2 }, new int[]
73 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
82 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
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;
96 * attributes set from AwtRenderPanelI
99 * old image used when data is currently being calculated and cannot be rendered
101 private Image fadedImage;
103 * panel being rendered into
105 private ImageObserver annotationPanel;
107 * width of image to render in panel
109 private int imgWidth;
111 // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI av)
112 public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel, AlignViewportI av)
114 fm = annotPanel.getFontMetrics();
115 annotationPanel = annotPanel;
116 fadedImage=annotPanel.getFadedImage();
117 imgWidth=annotPanel.getFadedImageWidth();
118 updateFromAlignViewport(av);
120 public void updateFromAlignViewport(AlignViewportI av)
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)
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();
136 columnSelection = av.getColumnSelection();
137 hconsensus = av.getSequenceConsensusHash();//hconsensus;
138 hStrucConsensus = av.getRnaStructureConsensusHash(); //hStrucConsensus;
139 av_ignoreGapsConsensus=av.getIgnoreGapsConsensus();
141 public int[] getProfileFor(AlignmentAnnotation aa, int column)
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
145 if (aa.autoCalculated && aa.label.startsWith("Consensus"))
147 if (aa.groupRef != null && aa.groupRef.consensusData != null
148 && aa.groupRef.isShowSequenceLogo())
150 return AAFrequency.extractProfile(
151 aa.groupRef.consensusData[column], aa.groupRef
152 .getIgnoreGapsConsensus());
154 // TODO extend annotation row to enable dynamic and static profile data to
156 if (aa.groupRef == null && aa.sequenceRef == null
159 return AAFrequency.extractProfile(hconsensus[column], av_ignoreGapsConsensus);
164 if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
166 // TODO implement group structure consensus
167 /* if (aa.groupRef != null && aa.groupRef.consensusData != null
168 && aa.groupRef.isShowSequenceLogo())
170 //TODO check what happens for group selections
171 return StructureFrequency.extractProfile(
172 aa.groupRef.consensusData[column], aa.groupRef
173 .getIgnoreGapsConsensus());
176 // TODO extend annotation row to enable dynamic and static profile data
179 if (aa.groupRef == null && aa.sequenceRef == null
180 && av_renderProfile && hStrucConsensus!=null && hStrucConsensus.length>column)
182 return StructureFrequency.extractProfile(hStrucConsensus[column],
183 av_ignoreGapsConsensus);
193 * @param annotationPanel TODO
201 public void drawComponent(AwtRenderPanelI annotPanel, AlignViewportI av, Graphics g, int activeRow, int startRes, int endRes)
204 // AnnotationPanel needs to implement: ImageObserver, access to AlignViewport
205 updateFromAwtRenderPanel(annotPanel, av);
206 fm = g.getFontMetrics();
207 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
214 boolean validRes = false;
215 boolean validEnd = false;
216 boolean labelAllCols = false;
217 boolean centreColLabels, centreColLabelsDef = av
218 .getCentreColumnLabels();
219 boolean scaleColLabel = false;
220 boolean[] graphGroupDrawn = new boolean[aa.length];
221 int charOffset = 0; // offset for a label
222 float fmWidth, fmScaling = 1f; // scaling for a label to fit it into a
224 Font ofont = g.getFont();
226 for (int i = 0; i < aa.length; i++)
228 AlignmentAnnotation row = aa[i];
234 centreColLabels = row.centreColLabels || centreColLabelsDef;
235 labelAllCols = row.showAllColLabels;
236 scaleColLabel = row.scaleColLabel;
241 if (row.graphGroup > -1 && graphGroupDrawn[row.graphGroup])
246 // this is so that we draw the characters below the graph
251 iconOffset = charHeight - fm.getDescent();
255 else if (row.hasText)
257 iconOffset = charHeight - fm.getDescent();
265 if (aa[i].autoCalculated && av.isCalculationInProgress(aa[i]))
269 g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0, y
270 - row.height, imgWidth, y, annotationPanel);
271 g.setColor(Color.black);
272 // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
277 /* else if (annotationPanel.av.updatingConservation
278 && aa[i].label.equals("Conservation"))
282 g.drawImage(annotationPanel.fadedImage, 0, y - row.height, annotationPanel.imgWidth, y, 0, y
283 - row.height, annotationPanel.imgWidth, y, annotationPanel);
285 g.setColor(Color.black);
286 // g.drawString("Calculating Conservation.....",20, y-row.height/2);
290 else if (annotationPanel.av.updatingConservation && aa[i].label.equals("Quality"))
294 g.drawImage(annotationPanel.fadedImage, 0, y - row.height, annotationPanel.imgWidth, y, 0, y
295 - row.height, annotationPanel.imgWidth, y, annotationPanel);
296 g.setColor(Color.black);
297 // / g.drawString("Calculating Quality....",20, y-row.height/2);
302 // first pass sets up state for drawing continuation from left-hand column
304 x = (startRes == 0) ? 0 : -1;
305 while (x < endRes - startRes)
307 if (hasHiddenColumns)
309 column = columnSelection.adjustForHiddenColumns(
311 if (column > row.annotations.length - 1)
318 column = startRes + x;
321 if ((row.annotations == null) || (row.annotations.length <= column)
322 || (row.annotations[column] == null))
334 g.setColor(Color.red);
336 if (columnSelection != null)
338 for (int n = 0; n < columnSelection.size(); n++)
340 int v = columnSelection.columnAt(n);
344 g.fillRect(x * charWidth, y, charWidth,
350 if (!row.isValidStruc())
352 g.setColor(Color.orange);
353 g.fillRect((int)row.getInvalidStrucPos()*charWidth, y, charWidth, charHeight);
357 && row.annotations[column].displayCharacter != null
358 && (row.annotations[column].displayCharacter.length() > 0))
361 if (centreColLabels || scaleColLabel)
363 fmWidth = (float) fm.charsWidth(
364 row.annotations[column].displayCharacter
366 row.annotations[column].displayCharacter.length());
370 // justify the label and scale to fit in column
371 if (fmWidth > charWidth)
373 // scale only if the current font isn't already small enough
374 fmScaling = charWidth;
375 fmScaling /= fmWidth;
376 g.setFont(ofont.deriveFont(AffineTransform
377 .getScaleInstance(fmScaling, 1.0)));
378 // and update the label's width to reflect the scaling.
386 .charWidth(row.annotations[column].displayCharacter
389 charOffset = (int) ((charWidth - fmWidth) / 2f);
391 if (row.annotations[column].colour == null)
392 g.setColor(Color.black);
394 g.setColor(row.annotations[column].colour);
396 if (column == 0 || row.graph > 0)
398 g.drawString(row.annotations[column].displayCharacter,
399 (x * charWidth) + charOffset, y + iconOffset);
401 else if (row.annotations[column - 1] == null
403 || !row.annotations[column].displayCharacter
404 .equals(row.annotations[column - 1].displayCharacter) || (row.annotations[column].displayCharacter
405 .length() < 2 && row.annotations[column].secondaryStructure == ' ')))
407 g.drawString(row.annotations[column].displayCharacter, x
408 * charWidth + charOffset, y + iconOffset);
415 char ss = validRes ? row.annotations[column].secondaryStructure
419 // distinguish between forward/backward base-pairing
420 if (row.annotations[column].displayCharacter.indexOf(')') > -1)
425 if (!validRes || (ss != lastSS))
432 drawHelixAnnot(g, row, lastSSX, x, y, iconOffset, startRes,
433 column, validRes, validEnd);
437 drawSheetAnnot(g, row, lastSSX, x, y, iconOffset, startRes,
438 column, validRes, validEnd);
441 case 'S': // Stem case for RNA secondary structure
442 case 's': // and opposite direction
443 drawStemAnnot(g, row, lastSSX, x, y, iconOffset, startRes,
444 column, validRes, validEnd);
448 g.setColor(Color.gray);
449 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth)
465 lastSSX = (x * charWidth);
472 if (column >= row.annotations.length)
474 column = row.annotations.length - 1;
481 if ((row.annotations == null) || (row.annotations.length <= column)
482 || (row.annotations[column] == null))
498 drawHelixAnnot(g, row, lastSSX, x, y, iconOffset, startRes,
499 column, validRes, validEnd);
503 drawSheetAnnot(g, row, lastSSX, x, y, iconOffset, startRes,
504 column, validRes, validEnd);
507 case 'S': // Stem case for RNA secondary structure
508 drawStemAnnot(g, row, lastSSX, x, y, iconOffset, startRes,
509 column, validRes, validEnd);
512 drawGlyphLine(g, row, lastSSX, x, y, iconOffset, startRes,
513 column, validRes, validEnd);
518 if (row.graph > 0 && row.graphHeight > 0)
520 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
522 if (row.graphGroup > -1 && !graphGroupDrawn[row.graphGroup])
524 float groupmax = -999999, groupmin = 9999999;
525 for (int gg = 0; gg < aa.length; gg++)
527 if (aa[gg].graphGroup != row.graphGroup)
534 aa[gg].visible = false;
537 if (aa[gg].graphMax > groupmax)
539 groupmax = aa[gg].graphMax;
541 if (aa[gg].graphMin < groupmin)
543 groupmin = aa[gg].graphMin;
547 for (int gg = 0; gg < aa.length; gg++)
549 if (aa[gg].graphGroup == row.graphGroup)
551 drawLineGraph(g, aa[gg], startRes, endRes, y, groupmin,
552 groupmax, row.graphHeight);
556 graphGroupDrawn[row.graphGroup] = true;
560 drawLineGraph( g, row, startRes, endRes, y, row.graphMin,
561 row.graphMax, row.graphHeight);
564 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
566 drawBarGraph(g, row, startRes, endRes, row.graphMin,
571 if (row.graph > 0 && row.hasText)
583 private Color GLYPHLINE_COLOR=Color.gray;
584 private Color SHEET_COLOUR=Color.green;
585 private Color HELIX_COLOUR=Color.red;
586 private Color STEM_COLOUR=Color.blue;
589 public void drawGlyphLine(Graphics g, AlignmentAnnotation row, int lastSSX, int x, int y, int iconOffset, int startRes, int column, boolean validRes, boolean validEnd)
591 g.setColor(GLYPHLINE_COLOR);
593 .fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth)
597 public void drawSheetAnnot(Graphics g, AlignmentAnnotation row, int lastSSX, int x, int y, int iconOffset, int startRes, int column, boolean validRes, boolean validEnd)
599 g.setColor(SHEET_COLOUR);
601 if (!validEnd || !validRes || row.annotations[column] == null
602 || row.annotations[column].secondaryStructure != 'E')
604 g.fillRect(lastSSX, y + 4 + iconOffset, (x * charWidth) - lastSSX
608 { (x * charWidth) - 4, (x * charWidth) - 4,
609 (x * charWidth) }, new int[]
610 { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset },
615 g.fillRect(lastSSX, y + 4 + iconOffset, (x + 1) * charWidth
621 public void drawHelixAnnot(Graphics g, AlignmentAnnotation row, int lastSSX, int x, int y, int iconOffset, int startRes, int column, boolean validRes, boolean validEnd)
623 g.setColor(HELIX_COLOUR);
625 int sCol = (lastSSX / charWidth) + startRes;
627 int x2 = (x * charWidth);
631 int ofs = charWidth / 2;
632 // Off by 1 offset when drawing rects and ovals
633 // to offscreen image on the MAC
634 g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1, 8, 8, 8);
635 if (sCol == 0 || row.annotations[sCol - 1] == null
636 || row.annotations[sCol - 1].secondaryStructure != 'H')
641 // g.setColor(Color.orange);
642 g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1 - ofs + 1, 8,
645 if (!validRes || row.annotations[column] == null
646 || row.annotations[column].secondaryStructure != 'H')
652 // g.setColor(Color.magenta);
653 g.fillRoundRect(lastSSX + ofs, y + 4 + iconOffset, x2 - x1 - ofs
661 if (sCol == 0 || row.annotations[sCol - 1] == null
662 || row.annotations[sCol - 1].secondaryStructure != 'H')
664 g.fillArc(lastSSX, y + 4 + iconOffset, charWidth, 8, 90, 180);
668 if (!validRes || row.annotations[column] == null
669 || row.annotations[column].secondaryStructure != 'H')
671 g.fillArc((x * charWidth) - charWidth, y + 4 + iconOffset,
672 charWidth, 8, 270, 180);
676 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
679 public void drawLineGraph(Graphics g, AlignmentAnnotation aa, int sRes, int eRes, int y, float min, float max, int graphHeight)
681 if (sRes > aa.annotations.length)
688 // Adjustment for fastpaint to left
694 eRes = Math.min(eRes, aa.annotations.length);
702 float range = max - min;
707 y2 = y - (int) ((0 - min / range) * graphHeight);
710 g.setColor(Color.gray);
711 g.drawLine(x - charWidth, y2, (eRes - sRes + 1) * charWidth, y2);
713 eRes = Math.min(eRes, aa.annotations.length);
716 int aaMax = aa.annotations.length - 1;
718 while (x < eRes - sRes)
721 if (hasHiddenColumns)
723 column = columnSelection.adjustForHiddenColumns(column);
731 if (aa.annotations[column] == null
732 || aa.annotations[column - 1] == null)
738 if (aa.annotations[column].colour == null)
739 g.setColor(Color.black);
741 g.setColor(aa.annotations[column].colour);
744 - (int) (((aa.annotations[column - 1].value - min) / range) * graphHeight);
746 - (int) (((aa.annotations[column].value - min) / range) * graphHeight);
748 g.drawLine(x * charWidth - charWidth / 2, y1, x * charWidth
749 + charWidth / 2, y2);
753 if (aa.threshold != null)
755 g.setColor(aa.threshold.colour);
756 Graphics2D g2 = (Graphics2D) g;
757 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
758 BasicStroke.JOIN_ROUND, 3f, new float[]
761 y2 = (int) (y - ((aa.threshold.value - min) / range) * graphHeight);
762 g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
763 g2.setStroke(new BasicStroke());
767 public void drawBarGraph(Graphics g, AlignmentAnnotation aa, int sRes, int eRes, float min, float max, int y)
769 if (sRes > aa.annotations.length)
773 Font ofont = g.getFont();
774 eRes = Math.min(eRes, aa.annotations.length);
776 int x = 0, y1 = y, y2 = y;
778 float range = max - min;
782 y2 = y - (int) ((0 - min / (range)) * aa.graphHeight);
785 g.setColor(Color.gray);
787 g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
790 int aaMax = aa.annotations.length - 1;
791 boolean renderHistogram = true, renderProfile = true, normaliseProfile=false;
792 // if (aa.autoCalculated && aa.label.startsWith("Consensus"))
794 // TODO: generalise this to have render styles for consensus/profile data
795 if (aa.groupRef != null)
797 renderHistogram = aa.groupRef.isShowConsensusHistogram();
798 renderProfile = aa.groupRef.isShowSequenceLogo();
799 normaliseProfile= aa.groupRef.isNormaliseSequenceLogo();
803 renderHistogram = av_renderHistogram;
804 renderProfile = av_renderProfile;
805 normaliseProfile= av_normaliseProfile;
808 while (x < eRes - sRes)
811 if (hasHiddenColumns)
813 column = columnSelection.adjustForHiddenColumns(column);
821 if (aa.annotations[column] == null)
826 if (aa.annotations[column].colour == null)
827 g.setColor(Color.black);
829 g.setColor(aa.annotations[column].colour);
832 - (int) (((aa.annotations[column].value - min) / (range)) * aa.graphHeight);
838 g.fillRect(x * charWidth, y2, charWidth, y1 - y2);
842 g.fillRect(x * charWidth, y1, charWidth, y2 - y1);
845 // draw profile if available
846 if (renderProfile && aa.annotations[column].value != 0)
849 int profl[] = getProfileFor(aa, column);
850 // just try to draw the logo if profl is not null
854 float ht = normaliseProfile ? y-aa.graphHeight : y1;
855 double htn = normaliseProfile ? aa.graphHeight : (y2 - y1);// aa.graphHeight;
861 * profl.length == 52 indicates that the profile of a secondary
862 * structure conservation row was accesed.
863 * Therefore dc gets length 2, to have space for a basepair instead of
864 * just a single nucleotide
866 if (profl.length == 74)
874 LineMetrics lm=g.getFontMetrics(ofont).getLineMetrics("Q", g);
875 double scale = 1f/(normaliseProfile ? profl[1] : 100f);
876 float ofontHeight = 1f/lm.getAscent();// magnify to fill box
878 for (int c = 2; profl != null && c < profl[0];)
880 dc[0] = (char) profl[c++];
882 if (aa.label.startsWith("StrucConsensus"))
884 dc[1] = (char) profl[c++];
888 wdth /= (float) fm.charsWidth(dc, 0, dc.length);
892 // if (aa.annotations[column].value==0) {
893 // g.setFont(ofont.deriveFont(AffineTransform.getScaleInstance(wdth,
894 // (ht2=(aa.graphHeight*0.1/av.charHeight)))));
897 scl=((double)htn)*scale* ((double) profl[c++]);
898 lm = ofont.getLineMetrics(dc, 0, 1, g.getFontMetrics().getFontRenderContext());
899 g.setFont(ofont.deriveFont(AffineTransform.getScaleInstance(
900 wdth, scl/lm.getAscent())));
901 lm = g.getFontMetrics().getLineMetrics(dc, 0, 1, g);
905 // ? );// try to get a
906 // colourscheme for the
907 // group(aa.groupRef.cs==null)
908 // ? av.textColour2 :
909 // cs.findColour(dc));
910 // System.out.println(dc[0]);
911 // Debug - render boxes around characters
912 // g.setColor(Color.red);
913 // g.drawRect(x*av.charWidth, (int)ht, av.charWidth, (int)(scl));
914 // g.setColor(profcolour.findColour(dc[0]).darker());
915 g.setColor(profcolour.findColour(dc[0]));
916 // (av.globalColourScheme!=null)
917 g.drawChars(dc, 0, dc.length, x * charWidth,
918 (int) (ht + (scl-lm.getDescent()-lm.getBaselineOffsets()[lm.getBaselineIndex()])));
919 // ht+=g.getFontMetrics().getAscent()-g.getFontMetrics().getDescent();
927 if (aa.threshold != null)
929 g.setColor(aa.threshold.colour);
930 Graphics2D g2 = (Graphics2D) g;
931 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
932 BasicStroke.JOIN_ROUND, 3f, new float[]
935 y2 = (int) (y - ((aa.threshold.value - min) / range) * aa.graphHeight);
936 g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
937 g2.setStroke(new BasicStroke());
940 // used by overview window
941 public void drawGraph(Graphics g, AlignmentAnnotation aa, int width,
942 int y, int sRes, int eRes)
944 eRes = Math.min(eRes, aa.annotations.length);
945 g.setColor(Color.white);
946 g.fillRect(0, 0, width, y);
947 g.setColor(new Color(0, 0, 180));
951 for (int j = sRes; j < eRes; j++)
953 if (aa.annotations[j] != null)
955 if (aa.annotations[j].colour == null)
956 g.setColor(Color.black);
958 g.setColor(aa.annotations[j].colour);
960 height = (int) ((aa.annotations[j].value / aa.graphMax) * y);
966 g.fillRect(x, y - height, charWidth, height);