2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.renderer;
23 import java.awt.BasicStroke;
24 import java.awt.Color;
26 import java.awt.FontMetrics;
27 import java.awt.Graphics;
28 import java.awt.Graphics2D;
29 import java.awt.Image;
30 import java.awt.RenderingHints;
31 import java.awt.Stroke;
32 import java.awt.geom.AffineTransform;
33 import java.awt.image.ImageObserver;
34 import java.util.BitSet;
35 import java.util.HashMap;
36 import java.util.Hashtable;
39 import org.jfree.graphics2d.svg.SVGGraphics2D;
40 import org.jibble.epsgraphics.EpsGraphics2D;
42 import jalview.analysis.AAFrequency;
43 import jalview.analysis.CodingUtils;
44 import jalview.analysis.Rna;
45 import jalview.analysis.StructureFrequency;
46 import jalview.api.AlignViewportI;
47 import jalview.bin.Cache;
48 import jalview.bin.Console;
49 import jalview.datamodel.AlignmentAnnotation;
50 import jalview.datamodel.Annotation;
51 import jalview.datamodel.ColumnSelection;
52 import jalview.datamodel.HiddenColumns;
53 import jalview.datamodel.ProfilesI;
54 import jalview.renderer.api.AnnotationRendererFactoryI;
55 import jalview.renderer.api.AnnotationRowRendererI;
56 import jalview.schemes.ColourSchemeI;
57 import jalview.schemes.NucleotideColourScheme;
58 import jalview.schemes.ResidueProperties;
59 import jalview.schemes.ZappoColourScheme;
60 import jalview.util.Platform;
62 public class AnnotationRenderer
64 private static final int UPPER_TO_LOWER = 'a' - 'A'; // 32
66 private static final int CHAR_A = 'A'; // 65
68 private static final int CHAR_Z = 'Z'; // 90
71 * flag indicating if timing and redraw parameter info should be output
73 private final boolean debugRedraw;
75 private int charWidth, endRes, charHeight;
77 private boolean validCharWidth, hasHiddenColumns;
79 private FontMetrics fm;
81 private final boolean USE_FILL_ROUND_RECT = Platform.isAMacAndNotJS();
83 boolean av_renderHistogram = true, av_renderProfile = true,
84 av_normaliseProfile = false;
86 ResidueShaderI profcolour = null;
88 private ColumnSelection columnSelection;
90 private HiddenColumns hiddenColumns;
92 private ProfilesI hconsensus;
94 private Hashtable<String, Object>[] complementConsensus;
96 private Hashtable<String, Object>[] hStrucConsensus;
98 private boolean av_ignoreGapsConsensus;
100 private boolean renderingVectors = false;
102 private boolean glyphLineDrawn = false;
105 * attributes set from AwtRenderPanelI
108 * old image used when data is currently being calculated and cannot be
111 private Image fadedImage;
114 * panel being rendered into
116 private ImageObserver annotationPanel;
119 * width of image to render in panel
121 private int imgWidth;
124 * offset to beginning of visible area
129 * offset to end of visible area
131 private int visHeight;
134 * indicate if the renderer should only render the visible portion of the
135 * annotation given the current view settings
137 private boolean useClip = true;
140 * master flag indicating if renderer should ever try to clip. not enabled for
143 private boolean canClip = false;
146 * Property to set text antialiasing method
148 private static final String TEXT_ANTIALIAS_METHOD = "TEXT_ANTIALIAS_METHOD";
150 private static final Object textAntialiasMethod;
154 final String textAntialiasMethodPref = Cache
155 .getDefault(TEXT_ANTIALIAS_METHOD, "GASP");
156 switch (textAntialiasMethodPref)
159 textAntialiasMethod = RenderingHints.VALUE_TEXT_ANTIALIAS_ON;
162 textAntialiasMethod = RenderingHints.VALUE_TEXT_ANTIALIAS_GASP;
165 textAntialiasMethod = RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HBGR;
168 textAntialiasMethod = RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB;
171 textAntialiasMethod = RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VBGR;
174 textAntialiasMethod = RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VRGB;
177 textAntialiasMethod = RenderingHints.VALUE_TEXT_ANTIALIAS_OFF;
180 textAntialiasMethod = RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT;
183 jalview.bin.Console.warn(TEXT_ANTIALIAS_METHOD + " value '"
184 + textAntialiasMethodPref
185 + "' not recognised, defaulting to 'GASP'. See https://docs.oracle.com/javase/8/docs/api/java/awt/RenderingHints.html#KEY_TEXT_ANTIALIASING");
186 textAntialiasMethod = RenderingHints.VALUE_TEXT_ANTIALIAS_GASP;
190 public AnnotationRenderer()
196 * Create a new annotation Renderer
199 * flag indicating if timing and redraw parameter info should be
202 public AnnotationRenderer(boolean debugRedraw)
204 this.debugRedraw = debugRedraw;
208 * Remove any references and resources when this object is no longer required
210 public void dispose()
212 hiddenColumns = null;
214 complementConsensus = null;
215 hStrucConsensus = null;
217 annotationPanel = null;
218 rendererFactoryI = null;
221 void drawStemAnnot(Graphics g, Annotation[] row_annotations, int lastSSX,
222 int x, int y, int iconOffset, int startRes, int column,
223 boolean validRes, boolean validEnd)
225 int sCol = (lastSSX / charWidth)
226 + hiddenColumns.visibleToAbsoluteColumn(startRes);
228 int x2 = (x * charWidth);
230 char dc = (column == 0 || row_annotations[column - 1] == null) ? ' '
231 : row_annotations[column - 1].secondaryStructure;
233 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
234 || dc != row_annotations[sCol - 1].secondaryStructure
236 boolean diffdownstream = !validRes || !validEnd
237 || row_annotations[column] == null
238 || dc != row_annotations[column].secondaryStructure;
240 if (diffupstream || diffdownstream)
242 // draw glyphline under arrow
243 drawGlyphLine(g, lastSSX, x, y, iconOffset);
245 g.setColor(STEM_COLOUR);
247 if (column > 0 && Rna.isClosingParenthesis(dc))
250 // if (validRes && column>1 && row_annotations[column-2]!=null &&
251 // dc.equals(row_annotations[column-2].displayCharacter))
254 * if new annotation with a closing base pair half of the stem,
255 * display a backward arrow
257 fillPolygon(g, new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
259 { y + iconOffset + 1, y + 13 + iconOffset,
260 y + 7 + iconOffset },
271 // display a forward arrow
275 * if annotation ending with an opeing base pair half of the stem,
276 * display a forward arrow
278 fillPolygon(g, new int[] { x2 - 6, x2 - 6, x2 - 1 },
280 { y + iconOffset + 1, y + 13 + iconOffset,
281 y + 7 + iconOffset },
292 fillRect(g, x1, y + 4 + iconOffset, x2 - x1, 6);
295 void drawNotCanonicalAnnot(Graphics g, Color nonCanColor,
296 Annotation[] row_annotations, int lastSSX, int x, int y,
297 int iconOffset, int startRes, int column, boolean validRes,
300 // Console.info(nonCanColor);
302 int sCol = (lastSSX / charWidth)
303 + hiddenColumns.visibleToAbsoluteColumn(startRes);
305 int x2 = (x * charWidth);
307 String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
308 : row_annotations[column - 1].displayCharacter;
310 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
311 || !dc.equals(row_annotations[sCol - 1].displayCharacter)
313 boolean diffdownstream = !validRes || !validEnd
314 || row_annotations[column] == null
315 || !dc.equals(row_annotations[column].displayCharacter);
316 // Console.info("Column "+column+" diff up:
318 // down:"+diffdownstream);
319 // If a closing base pair half of the stem, display a backward arrow
320 if (diffupstream || diffdownstream)
322 // draw glyphline under arrow
323 drawGlyphLine(g, lastSSX, x, y, iconOffset);
325 g.setColor(nonCanColor);
326 if (column > 0 && Rna.isClosingParenthesis(dc))
330 // if (validRes && column>1 && row_annotations[column-2]!=null &&
331 // dc.equals(row_annotations[column-2].displayCharacter))
333 fillPolygon(g, new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
335 { y + iconOffset + 1, y + 13 + iconOffset,
336 y + 7 + iconOffset },
348 // display a forward arrow
351 fillPolygon(g, new int[] { x2 - 6, x2 - 6, x2 - 1 },
353 { y + iconOffset + 1, y + 13 + iconOffset,
354 y + 7 + iconOffset },
365 fillRect(g, x1, y + 4 + iconOffset, x2 - x1, 6);
368 // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
370 public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
373 fm = annotPanel.getFontMetrics();
374 annotationPanel = annotPanel;
375 fadedImage = annotPanel.getFadedImage();
376 imgWidth = annotPanel.getFadedImageWidth();
377 // visible area for rendering
378 int[] bounds = annotPanel.getVisibleVRange();
382 visHeight = bounds[1];
397 rendererFactoryI = AnnotationRendererFactory.getRendererFactory();
398 updateFromAlignViewport(av);
401 public void updateFromAlignViewport(AlignViewportI av)
403 charWidth = av.getCharWidth();
404 endRes = av.getRanges().getEndRes();
405 charHeight = av.getCharHeight();
406 hasHiddenColumns = av.hasHiddenColumns();
407 validCharWidth = av.isValidCharWidth();
408 av_renderHistogram = av.isShowConsensusHistogram();
409 av_renderProfile = av.isShowSequenceLogo();
410 av_normaliseProfile = av.isNormaliseSequenceLogo();
411 profcolour = av.getResidueShading();
412 if (profcolour == null || profcolour.getColourScheme() == null)
415 * Use default colour for sequence logo if
416 * the alignment has no colourscheme set
417 * (would like to use user preference but n/a for applet)
419 ColourSchemeI col = av.getAlignment().isNucleotide()
420 ? new NucleotideColourScheme()
421 : new ZappoColourScheme();
422 profcolour = new ResidueShader(col);
424 columnSelection = av.getColumnSelection();
425 hiddenColumns = av.getAlignment().getHiddenColumns();
426 hconsensus = av.getSequenceConsensusHash();
427 complementConsensus = av.getComplementConsensusHash();
428 hStrucConsensus = av.getRnaStructureConsensusHash();
429 av_ignoreGapsConsensus = av.isIgnoreGapsConsensus();
433 * Returns profile data; the first element is the profile type, the second is
434 * the number of distinct values, the third the total count, and the remainder
435 * depend on the profile type.
441 int[] getProfileFor(AlignmentAnnotation aa, int column)
443 // TODO : consider refactoring the global alignment calculation
444 // properties/rendering attributes as a global 'alignment group' which holds
445 // all vis settings for the alignment as a whole rather than a subset
447 if (aa.autoCalculated && (aa.label.startsWith("Consensus")
448 || aa.label.startsWith("cDNA Consensus")))
450 boolean forComplement = aa.label.startsWith("cDNA Consensus");
451 if (aa.groupRef != null && aa.groupRef.consensusData != null
452 && aa.groupRef.isShowSequenceLogo())
454 // TODO? group consensus for cDNA complement
455 return AAFrequency.extractProfile(
456 aa.groupRef.consensusData.get(column),
457 aa.groupRef.getIgnoreGapsConsensus());
459 // TODO extend annotation row to enable dynamic and static profile data to
461 if (aa.groupRef == null && aa.sequenceRef == null)
465 return AAFrequency.extractCdnaProfile(complementConsensus[column],
466 av_ignoreGapsConsensus);
470 return AAFrequency.extractProfile(hconsensus.get(column),
471 av_ignoreGapsConsensus);
477 if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
479 // TODO implement group structure consensus
481 * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
482 * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
483 * group selections return StructureFrequency.extractProfile(
484 * aa.groupRef.consensusData[column], aa.groupRef
485 * .getIgnoreGapsConsensus()); }
487 // TODO extend annotation row to enable dynamic and static profile data
490 if (aa.groupRef == null && aa.sequenceRef == null
491 && hStrucConsensus != null
492 && hStrucConsensus.length > column)
494 return StructureFrequency.extractProfile(hStrucConsensus[column],
495 av_ignoreGapsConsensus);
504 private AnnotationRendererFactoryI rendererFactoryI;
507 * Render the annotation rows associated with an alignment.
512 * data and view settings to render
514 * destination for graphics
516 * row where a mouse event occured (or -1)
518 * first column that will be drawn
520 * last column that will be drawn
521 * @return true if the fadedImage was used for any alignment annotation rows
522 * currently being calculated
524 public boolean drawComponent(AwtRenderPanelI annotPanel,
525 AlignViewportI av, Graphics g, int activeRow, int startRes,
528 if (g instanceof EpsGraphics2D || g instanceof SVGGraphics2D)
530 this.setVectorRendering(true);
532 Graphics2D g2d = (Graphics2D) g;
534 long stime = System.currentTimeMillis();
535 boolean usedFaded = false;
537 // AnnotationPanel needs to implement: ImageObserver, access to
539 updateFromAwtRenderPanel(annotPanel, av);
540 fm = g.getFontMetrics();
541 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
552 boolean validRes = false;
553 boolean validEnd = false;
554 boolean labelAllCols = false;
555 // boolean centreColLabels;
556 // boolean centreColLabelsDef = av.isCentreColumnLabels();
557 boolean scaleColLabel = false;
558 final AlignmentAnnotation consensusAnnot = av
559 .getAlignmentConsensusAnnotation();
560 final AlignmentAnnotation structConsensusAnnot = av
561 .getAlignmentStrucConsensusAnnotation();
562 final AlignmentAnnotation complementConsensusAnnot = av
563 .getComplementConsensusAnnotation();
565 BitSet graphGroupDrawn = new BitSet();
568 int yfrom = 0, f_i = 0, yto = 0, f_to = 0;
569 boolean clipst = false, clipend = false;
570 for (int i = 0; i < aa.length; i++)
572 AlignmentAnnotation row = aa[i];
573 boolean renderHistogram = true;
574 boolean renderProfile = false;
575 boolean normaliseProfile = false;
576 boolean isRNA = row.isRNA();
578 // check if this is a consensus annotation row and set the display
579 // settings appropriately
580 // TODO: generalise this to have render styles for consensus/profile
582 if (row.groupRef != null && row == row.groupRef.getConsensus())
584 renderHistogram = row.groupRef.isShowConsensusHistogram();
585 renderProfile = row.groupRef.isShowSequenceLogo();
586 normaliseProfile = row.groupRef.isNormaliseSequenceLogo();
588 else if (row == consensusAnnot || row == structConsensusAnnot
589 || row == complementConsensusAnnot)
591 renderHistogram = av_renderHistogram;
592 renderProfile = av_renderProfile;
593 normaliseProfile = av_normaliseProfile;
596 Annotation[] row_annotations = row.annotations;
601 // centreColLabels = row.centreColLabels || centreColLabelsDef;
602 labelAllCols = row.showAllColLabels;
603 scaleColLabel = row.scaleColLabel;
607 if (!useClip || ((y - charHeight) < visHeight
608 && (y + row.height + charHeight * 2) >= sOffset))
609 {// if_in_visible_region
620 if (row.graphGroup > -1 && graphGroupDrawn.get(row.graphGroup))
625 // this is so that we draw the characters below the graph
630 iconOffset = charHeight - fm.getDescent();
634 else if (row.hasText)
636 iconOffset = charHeight - fm.getDescent();
644 if (row.autoCalculated && av.isCalculationInProgress(row))
648 g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0,
649 y - row.height, imgWidth, y, annotationPanel);
650 g.setColor(Color.black);
651 // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
657 * else if (annotationPanel.av.updatingConservation &&
658 * aa[i].label.equals("Conservation")) {
660 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
661 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
662 * annotationPanel.imgWidth, y, annotationPanel);
664 * g.setColor(Color.black); //
665 * g.drawString("Calculating Conservation.....",20, y-row.height/2);
667 * continue; } else if (annotationPanel.av.updatingConservation &&
668 * aa[i].label.equals("Quality")) {
670 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
671 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
672 * annotationPanel.imgWidth, y, annotationPanel);
673 * g.setColor(Color.black); // /
674 * g.drawString("Calculating Quality....",20, y-row.height/2);
679 // first pass sets up state for drawing continuation from left-hand
683 // flag used for vector rendition
684 this.glyphLineDrawn = false;
685 x = (startRes == 0) ? 0 : -1;
687 while (x < endRes - startRes)
689 if (hasHiddenColumns)
691 column = hiddenColumns.visibleToAbsoluteColumn(startRes + x);
692 if (column > row_annotations.length - 1)
699 column = startRes + x;
702 if ((row_annotations == null)
703 || (row_annotations.length <= column)
704 || (row_annotations[column] == null))
712 final String displayChar = validRes
713 ? row_annotations[column].displayCharacter
720 g.setColor(Color.red);
722 if (columnSelection != null)
724 if (columnSelection.contains(column))
726 fillRect(g, x * charWidth, y, charWidth, charHeight);
730 if (row.getInvalidStrucPos() > x)
732 g.setColor(Color.orange);
733 fillRect(g, x * charWidth, y, charWidth, charHeight);
735 else if (row.getInvalidStrucPos() == x)
737 g.setColor(Color.orange.darker());
738 fillRect(g, x * charWidth, y, charWidth, charHeight);
740 if (validCharWidth && validRes && displayChar != null
741 && (displayChar.length() > 0))
743 float fmWidth = fm.stringWidth(displayChar);
746 * shrink label width to fit in column, if that is
747 * both configured and necessary
749 boolean scaledToFit = false;
750 float fmScaling = 1f;
751 if (scaleColLabel && fmWidth > charWidth)
754 fmScaling = (float) charWidth / fmWidth;
755 // and update the label's width to reflect the scaling.
759 float charOffset = (charWidth - fmWidth) / 2f;
761 if (row_annotations[column].colour == null)
763 g2d.setColor(Color.black);
767 g2d.setColor(row_annotations[column].colour);
771 * draw the label, unless it is the same secondary structure
772 * symbol (excluding RNA Helix) as the previous column
774 final float xPos = (x * charWidth) + charOffset;
775 final float yPos = y + iconOffset;
777 // Act on a copy of the Graphics2d object
778 Graphics2D g2dCopy = (Graphics2D) g2d.create();
779 // Clip to this annotation line (particularly width).
780 // This removes artifacts from text when side scrolling
781 // (particularly in wrap format), but can result in clipped
782 // characters until a full paint is drawn.
783 g2dCopy.setClip(0, (int) yPos - charHeight, imgWidth - 1,
786 * translate to drawing position _before_ applying any scaling
788 g2dCopy.translate(xPos, yPos);
792 * use a scaling transform to make the label narrower
793 * (JalviewJS doesn't have Font.deriveFont(AffineTransform))
796 AffineTransform.getScaleInstance(fmScaling, 1.0));
798 setAntialias(g2dCopy, true);
799 if (column == 0 || row.graph > 0)
801 g2dCopy.drawString(displayChar, 0, 0);
803 else if (row_annotations[column - 1] == null || (labelAllCols
804 || !displayChar.equals(
805 row_annotations[column - 1].displayCharacter)
806 || (displayChar.length() < 2
807 && row_annotations[column].secondaryStructure == ' ')))
809 g2dCopy.drawString(displayChar, 0, 0);
816 char ss = validRes ? row_annotations[column].secondaryStructure
821 // distinguish between forward/backward base-pairing
822 if (displayChar.indexOf(')') > -1)
831 if ((displayChar.indexOf(']') > -1))
839 // distinguish between forward/backward base-pairing
840 if (displayChar.indexOf('}') > -1)
848 // distinguish between forward/backward base-pairing
849 if (displayChar.indexOf('<') > -1)
855 if (isRNA && (ss >= CHAR_A) && (ss <= CHAR_Z))
857 // distinguish between forward/backward base-pairing
858 int ssLowerCase = ss + UPPER_TO_LOWER;
859 // TODO would .equals() be safer here? or charAt(0)?
860 if (displayChar.indexOf(ssLowerCase) > -1)
862 ss = (char) ssLowerCase;
866 if (!validRes || (ss != lastSS))
872 // int nb_annot = x - temp;
873 // Console.info("\t type :"+lastSS+"\t x
875 // annot :"+nb_annot);
878 case '(': // Stem case for RNA secondary structure
879 case ')': // and opposite direction
880 drawStemAnnot(g, row_annotations, lastSSX, x, y,
881 iconOffset, startRes, column, validRes, validEnd);
888 drawHelixAnnot(g, row_annotations, lastSSX, x, y,
889 iconOffset, startRes, column, validRes,
893 // no break if isRNA - falls through to drawNotCanonicalAnnot!
897 drawSheetAnnot(g, row_annotations, lastSSX, x, y,
898 iconOffset, startRes, column, validRes,
902 // no break if isRNA - fall through to drawNotCanonicalAnnot!
961 Color nonCanColor = getNotCanonicalColor(lastSS);
962 drawNotCanonicalAnnot(g, nonCanColor, row_annotations,
963 lastSSX, x, y, iconOffset, startRes, column,
968 if (isVectorRendering())
970 // draw single full width glyphline
971 drawGlyphLine(g, lastSSX, endRes - x, y, iconOffset);
972 // disable more glyph lines
973 this.glyphLineDrawn = true;
977 drawGlyphLine(g, lastSSX, x, y, iconOffset);
992 lastSSX = (x * charWidth);
999 if (column >= row_annotations.length)
1001 column = row_annotations.length - 1;
1008 if ((row_annotations == null) || (row_annotations.length <= column)
1009 || (row_annotations[column] == null))
1027 drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
1028 startRes, column, validRes, validEnd);
1031 // no break if isRNA - fall through to drawNotCanonicalAnnot!
1036 drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
1037 startRes, column, validRes, validEnd);
1040 // no break if isRNA - fall through to drawNotCanonicalAnnot!
1043 case ')': // Stem case for RNA secondary structure
1045 drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
1046 startRes, column, validRes, validEnd);
1103 // Console.info(lastSS);
1104 Color nonCanColor = getNotCanonicalColor(lastSS);
1105 drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX,
1106 x, y, iconOffset, startRes, column, validRes, validEnd);
1109 if (isVectorRendering())
1111 // draw single full width glyphline
1112 drawGlyphLine(g, lastSSX, endRes - x, y, iconOffset);
1113 // disable more glyph lines
1114 this.glyphLineDrawn = true;
1118 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1124 if (row.graph > 0 && row.graphHeight > 0)
1126 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1128 if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
1130 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1131 // computed efficiently for all visible labels
1132 float groupmax = -999999, groupmin = 9999999;
1133 for (int gg = 0; gg < aa.length; gg++)
1135 if (aa[gg].graphGroup != row.graphGroup)
1142 aa[gg].visible = false;
1144 if (aa[gg].graphMax > groupmax)
1146 groupmax = aa[gg].graphMax;
1148 if (aa[gg].graphMin < groupmin)
1150 groupmin = aa[gg].graphMin;
1154 for (int gg = 0; gg < aa.length; gg++)
1156 if (aa[gg].graphGroup == row.graphGroup)
1158 drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
1159 endRes, y, groupmin, groupmax, row.graphHeight);
1163 graphGroupDrawn.set(row.graphGroup);
1167 drawLineGraph(g, row, row_annotations, startRes, endRes, y,
1168 row.graphMin, row.graphMax, row.graphHeight);
1171 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1173 drawBarGraph(g, row, row_annotations, startRes, endRes,
1174 row.graphMin, row.graphMax, y, renderHistogram,
1175 renderProfile, normaliseProfile);
1179 AnnotationRowRendererI renderer = rendererFactoryI
1180 .getRendererFor(row);
1181 if (renderer != null)
1183 renderer.renderRow(g, charWidth, charHeight, hasHiddenColumns,
1184 av, hiddenColumns, columnSelection, row,
1185 row_annotations, startRes, endRes, row.graphMin,
1186 row.graphMax, y, isVectorRendering());
1190 if (renderer == null)
1193 .println("No renderer found for " + row.toString());
1198 "rendered with " + renderer.getClass().toString());
1207 if (clipst && !clipend)
1211 } // end if_in_visible_region
1212 if (row.graph > 0 && row.hasText)
1228 Console.warn("Start clip at : " + yfrom + " (index " + f_i + ")");
1232 Console.warn("End clip at : " + yto + " (index " + f_to + ")");
1236 Console.warn("Annotation Rendering time:"
1237 + (System.currentTimeMillis() - stime));
1244 public static final Color GLYPHLINE_COLOR = Color.gray;
1246 public static final Color SHEET_COLOUR = Color.green;
1248 public static final Color HELIX_COLOUR = Color.red;
1250 public static final Color STEM_COLOUR = Color.blue;
1252 // private Color sdNOTCANONICAL_COLOUR;
1254 void drawGlyphLine(Graphics g, int lastSSX, int x, int y, int iconOffset)
1258 // if we've drawn a single long glyphline for an export, don't draw the
1263 g.setColor(GLYPHLINE_COLOR);
1264 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
1267 void drawSheetAnnot(Graphics g, Annotation[] row,
1269 int lastSSX, int x, int y, int iconOffset, int startRes,
1270 int column, boolean validRes, boolean validEnd)
1272 if (!validEnd || !validRes || row == null || row[column] == null
1273 || row[column].secondaryStructure != 'E')
1275 // draw the glyphline underneath
1276 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1278 g.setColor(SHEET_COLOUR);
1279 fillRect(g, lastSSX, y + 4 + iconOffset,
1280 (x * charWidth) - lastSSX - 4, 6);
1283 { (x * charWidth) - 6, (x * charWidth) - 6,
1284 (x * charWidth - 1) },
1286 { y + iconOffset + 1, y + 13 + iconOffset,
1287 y + 7 + iconOffset },
1292 g.setColor(SHEET_COLOUR);
1293 fillRect(g, lastSSX, y + 4 + iconOffset, (x * charWidth) - lastSSX,
1298 void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x,
1299 int y, int iconOffset, int startRes, int column, boolean validRes,
1302 int sCol = (lastSSX / charWidth)
1303 + hiddenColumns.visibleToAbsoluteColumn(startRes);
1305 int x2 = (x * charWidth);
1307 if (USE_FILL_ROUND_RECT || isVectorRendering())
1309 // draw glyph line behind helix (visible in EPS or SVG output)
1310 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1312 g.setColor(HELIX_COLOUR);
1314 int ofs = charWidth / 2;
1315 // Off by 1 offset when drawing rects and ovals
1316 // to offscreen image on the MAC
1317 fillRoundRect(g, lastSSX, y + 3 + iconOffset, x2 - x1 - 1, 8, 8, 8);
1318 if (sCol == 0 || row[sCol - 1] == null
1319 || row[sCol - 1].secondaryStructure != 'H')
1324 fillRoundRect(g, lastSSX, y + 3 + iconOffset, x2 - x1 - ofs, 8, 0,
1327 if (!validRes || row[column] == null
1328 || row[column].secondaryStructure != 'H')
1334 fillRoundRect(g, lastSSX + ofs, y + 3 + iconOffset, x2 - x1 - ofs,
1341 boolean leftEnd = sCol == 0 || row[sCol - 1] == null
1342 || row[sCol - 1].secondaryStructure != 'H';
1343 boolean rightEnd = !validRes || row[column] == null
1344 || row[column].secondaryStructure != 'H';
1346 if (leftEnd || rightEnd)
1348 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1350 g.setColor(HELIX_COLOUR);
1354 fillArc(g, lastSSX, y + 3 + iconOffset, charWidth, 8, 90, 180);
1355 x1 += charWidth / 2;
1360 fillArc(g, ((x - 1) * charWidth), y + 3 + iconOffset, charWidth, 8,
1362 x2 -= charWidth / 2;
1365 fillRect(g, x1, y + 3 + iconOffset, x2 - x1, 8);
1368 void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1369 Annotation[] aa_annotations, int sRes, int eRes, int y, float min,
1370 float max, int graphHeight)
1372 if (sRes > aa_annotations.length)
1376 Stroke roundStroke = new BasicStroke(1, BasicStroke.CAP_ROUND,
1377 BasicStroke.JOIN_ROUND);
1378 Stroke squareStroke = new BasicStroke(1, BasicStroke.CAP_SQUARE,
1379 BasicStroke.JOIN_MITER);
1380 Graphics2D g2d = (Graphics2D) g;
1381 Stroke prevStroke = g2d.getStroke();
1382 g2d.setStroke(roundStroke);
1386 // Adjustment for fastpaint to left
1392 eRes = Math.min(eRes, aa_annotations.length);
1395 float range = max - min;
1400 y2 = y - (int) ((0 - min / range) * graphHeight);
1403 g.setColor(Color.gray);
1404 drawLine(g, squareStroke, x * charWidth, y2, (eRes - sRes) * charWidth,
1412 eRes = Math.min(eRes, aa_annotations.length);
1415 int aaMax = aa_annotations.length - 1;
1417 while (x <= eRes - sRes)
1420 if (hasHiddenColumns)
1422 column = hiddenColumns.visibleToAbsoluteColumn(column);
1430 if (aa_annotations[column] == null)
1436 if (aa_annotations[column].colour == null)
1438 g.setColor(Color.black);
1442 g.setColor(aa_annotations[column].colour);
1445 boolean previousValueExists = column > 0
1446 && aa_annotations[column - 1] != null;
1447 float previousValue = previousValueExists
1448 ? aa_annotations[column - 1].value
1450 float thisValue = aa_annotations[column].value;
1451 boolean nextValueExists = aa_annotations.length > column + 1
1452 && aa_annotations[column + 1] != null;
1453 float nextValue = nextValueExists ? aa_annotations[column + 1].value
1456 // check for standalone value
1457 if (!previousValueExists && !nextValueExists)
1459 y2 = y - yValueToPixelHeight(thisValue, min, range, graphHeight);
1460 drawLine(g, x * charWidth + charWidth / 4, y2,
1461 x * charWidth + 3 * charWidth / 4, y2);
1466 if (!previousValueExists)
1472 y1 = y - yValueToPixelHeight(previousValue, min, range, graphHeight);
1473 y2 = y - yValueToPixelHeight(thisValue, min, range, graphHeight);
1477 // only draw an initial half-line
1478 drawLine(g, x * charWidth, y1 + (y2 - y1) / 2,
1479 x * charWidth + charWidth / 2, y2);
1482 else if (x == eRes - sRes)
1484 // this is one past the end to draw -- only draw a half line
1485 drawLine(g, (x - 1) * charWidth + charWidth / 2, y1,
1486 x * charWidth - 1, y1 + (y2 - y1) / 2);
1491 drawLine(g, (x - 1) * charWidth + charWidth / 2, y1,
1492 x * charWidth + charWidth / 2, y2);
1497 if (_aa.threshold != null)
1499 g.setColor(_aa.threshold.colour);
1500 Graphics2D g2 = (Graphics2D) g;
1501 y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1502 drawLine(g, dashedLine(charWidth), 0, y2, (eRes - sRes) * charWidth,
1505 g2d.setStroke(prevStroke);
1508 private static double log2 = Math.log(2);
1510 // Cached dashed line Strokes
1511 private static Map<Integer, Stroke> dashedLineLookup = new HashMap<>();
1514 * Returns a dashed line stroke as close to 6-4 pixels as fits within the
1515 * charWidth. This allows translations of multiples of charWidth without
1516 * disrupting the dashed line. The exact values are 0.6-0.4 proportions of
1517 * charWidth for charWidth under 16. For charWidth 16 or over, the number of
1518 * dashes doubles as charWidth doubles.
1521 * @return Stroke with appropriate dashed line fitting exactly within the
1524 private static Stroke dashedLine(int charWidth)
1526 if (!dashedLineLookup.containsKey(charWidth))
1528 int power2 = charWidth >= 8 ? (int) (Math.log(charWidth) / log2) : 2;
1529 float width = ((float) charWidth) / ((float) Math.pow(2, power2 - 3));
1530 float segment1 = width * 0.6f;
1531 float segment2 = width - segment1;
1532 dashedLineLookup.put(charWidth, new BasicStroke(1,
1533 BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 3f, new float[]
1534 { segment1, segment2 }, 0f));
1536 return dashedLineLookup.get(charWidth);
1539 private static int yValueToPixelHeight(float value, float min,
1540 float range, int graphHeight)
1542 return (int) (((value - min) / range) * graphHeight);
1545 @SuppressWarnings("unused")
1546 void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1547 Annotation[] aa_annotations, int sRes, int eRes, float min,
1548 float max, int y, boolean renderHistogram, boolean renderProfile,
1549 boolean normaliseProfile)
1551 if (sRes > aa_annotations.length)
1555 Font ofont = g.getFont();
1556 eRes = Math.min(eRes, aa_annotations.length);
1558 int x = 0, y1 = y, y2 = y;
1560 float range = max - min;
1564 y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1567 g.setColor(Color.gray);
1569 drawLine(g, x, y2, (eRes - sRes) * charWidth, y2);
1572 int aaMax = aa_annotations.length - 1;
1573 while (x < eRes - sRes)
1576 if (hasHiddenColumns)
1578 column = hiddenColumns.visibleToAbsoluteColumn(column);
1586 if (aa_annotations[column] == null)
1591 if (aa_annotations[column].colour == null)
1593 g.setColor(Color.black);
1597 g.setColor(aa_annotations[column].colour);
1600 y1 = y - (int) (((aa_annotations[column].value - min) / (range))
1603 if (renderHistogram)
1607 fillRect(g, x * charWidth, y2, charWidth, y1 - y2);
1611 fillRect(g, x * charWidth, y1, charWidth, y2 - y1);
1614 // draw profile if available
1619 * {profile type, #values, total count, char1, pct1, char2, pct2...}
1621 int profl[] = getProfileFor(_aa, column);
1623 // just try to draw the logo if profl is not null
1624 if (profl != null && profl[2] != 0)
1626 boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
1627 boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
1628 float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1629 final double normaliseFactor = normaliseProfile ? _aa.graphHeight
1633 * Render a single base for a sequence profile, a base pair for
1634 * structure profile, and a triplet for a cdna profile
1636 char[] dc = new char[isStructureProfile ? 2
1637 : (isCdnaProfile ? 3 : 1)];
1639 // lm is not necessary - we can just use fm - could be off by no more
1641 // LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1642 // Console.info(asc + " " + dec + " " + (asc -
1644 // + " " + (dec - lm.getDescent()));
1646 double asc = fm.getAscent();
1647 double dec = fm.getDescent();
1648 double fht = fm.getHeight();
1650 double scale = 1f / (normaliseProfile ? profl[2] : 100f);
1651 // float ofontHeight = 1f / fm.getAscent();// magnify to fill box
1654 * Traverse the character(s)/percentage data in the array
1659 // profl[1] is the number of values in the profile
1660 for (int i = 0, c = 3, last = profl[1]; i < last; i++)
1664 if (isStructureProfile)
1666 // todo can we encode a structure pair as an int, like codons?
1667 dc[0] = (char) profl[c++];
1668 dc[1] = (char) profl[c++];
1671 else if (isCdnaProfile)
1673 CodingUtils.decodeCodon2(profl[c++], dc);
1678 dc[0] = (char) profl[c++];
1681 // next profl[] position is profile % for the character(s)
1683 int percent = profl[c++];
1686 // failsafe in case a count rounds down to 0%
1689 double newHeight = normaliseFactor * scale * percent;
1692 * Set character colour as per alignment colour scheme; use the
1693 * codon translation if a cDNA profile
1695 Color colour = null;
1698 final String codonTranslation = ResidueProperties
1700 colour = profcolour.findColour(codonTranslation.charAt(0),
1705 colour = profcolour.findColour(dc[0], column, null);
1707 g.setColor(colour == Color.white ? Color.lightGray : colour);
1709 double sx = 1f * charWidth / fm.charsWidth(dc, 0, dc.length);
1710 double sy = newHeight / asc;
1711 double newAsc = asc * sy;
1712 double newDec = dec * sy;
1713 // it is not necessary to recalculate lm for the new font.
1714 // note: lm.getBaselineOffsets()[lm.getBaselineIndex()]) must be 0
1715 // by definition. Was:
1716 // int hght = (int) (ht + (newAsc - newDec);
1717 // - lm.getBaselineOffsets()[lm.getBaselineIndex()]));
1719 if (Platform.isJS())
1722 * SwingJS does not implement font.deriveFont()
1723 * so use a scaling transform to draw instead,
1724 * this is off by a very small amount
1726 final int hght = (int) (ht2 + (newAsc - newDec));
1727 Graphics2D gg = (Graphics2D) g;
1728 int xShift = (int) Math.round(x * charWidth / sx);
1729 int yShift = (int) Math.round(hght / sy);
1730 gg.transform(AffineTransform.getScaleInstance(sx, sy));
1731 gg.drawString(s, xShift, yShift);
1733 AffineTransform.getScaleInstance(1D / sx, 1D / sy));
1743 // Java ('normal') method is to scale the font to fit
1745 final int hght = (int) (ht + (newAsc - newDec));
1747 .deriveFont(AffineTransform.getScaleInstance(sx, sy));
1749 g.drawChars(dc, 0, dc.length, x * charWidth, hght);
1759 if (_aa.threshold != null)
1761 g.setColor(_aa.threshold.colour);
1762 Stroke s = new BasicStroke(1, BasicStroke.CAP_SQUARE,
1763 BasicStroke.JOIN_ROUND, 3f, new float[]
1767 - ((_aa.threshold.value - min) / range) * _aa.graphHeight);
1768 drawLine(g, s, 0, y2, (eRes - sRes) * charWidth, y2);
1772 // used by overview window
1773 public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1774 Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1776 eRes = Math.min(eRes, aa_annotations.length);
1777 g.setColor(Color.white);
1778 fillRect(g, 0, 0, width, y);
1779 g.setColor(new Color(0, 0, 180));
1783 for (int j = sRes; j < eRes; j++)
1785 if (aa_annotations[j] != null)
1787 if (aa_annotations[j].colour == null)
1789 g.setColor(Color.black);
1793 g.setColor(aa_annotations[j].colour);
1796 height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1802 fillRect(g, x, y - height, charWidth, height);
1808 Color getNotCanonicalColor(char lastss)
1814 return new Color(255, 125, 5);
1818 return new Color(245, 115, 10);
1822 return new Color(235, 135, 15);
1826 return new Color(225, 105, 20);
1830 return new Color(215, 145, 30);
1834 return new Color(205, 95, 35);
1838 return new Color(195, 155, 45);
1842 return new Color(185, 85, 55);
1846 return new Color(175, 165, 65);
1850 return new Color(170, 75, 75);
1854 return new Color(160, 175, 85);
1858 return new Color(150, 65, 95);
1862 return new Color(140, 185, 105);
1866 return new Color(130, 55, 110);
1870 return new Color(120, 195, 120);
1874 return new Color(110, 45, 130);
1878 return new Color(100, 205, 140);
1882 return new Color(90, 35, 150);
1886 return new Color(85, 215, 160);
1890 return new Color(75, 25, 170);
1894 return new Color(65, 225, 180);
1898 return new Color(55, 15, 185);
1902 return new Color(45, 235, 195);
1906 return new Color(35, 5, 205);
1910 return new Color(25, 245, 215);
1914 return new Color(15, 0, 225);
1918 return new Color(10, 255, 235);
1922 return new Color(5, 150, 245);
1926 return new Color(0, 80, 255);
1929 Console.info("This is not a interaction : " + lastss);
1935 private void fillPolygon(Graphics g, int[] xpoints, int[] ypoints, int n)
1938 g.fillPolygon(xpoints, ypoints, n);
1942 private void fillRect(Graphics g, int a, int b, int c, int d)
1944 fillRect(g, false, a, b, c, d);
1947 private void fillRect(Graphics g, int a, int b, int c, int d)
1950 g.fillRect(a, b, c, d);
1953 private void fillRoundRect(Graphics g, int a, int b, int c, int d, int e,
1957 g.fillRoundRect(a, b, c, d, e, f);
1960 private void fillArc(Graphics g, int a, int b, int c, int d, int e, int f)
1963 g.fillArc(a, b, c, d, e, f);
1966 private void drawLine(Graphics g, Stroke s, int a, int b, int c, int d)
1968 Graphics2D g2d = (Graphics2D) g;
1969 Stroke p = g2d.getStroke();
1971 drawLine(g, a, b, c, d);
1975 private void drawLine(Graphics g, int a, int b, int c, int d)
1978 g.drawLine(a, b, c, d);
1981 private void setAntialias(Graphics g)
1983 setAntialias(g, false);
1986 private void setAntialias(Graphics g, boolean text)
1988 if (isVectorRendering())
1990 // no need to antialias vector drawings
1993 if (Cache.getDefault("ANTI_ALIAS", true))
1995 Graphics2D g2d = (Graphics2D) g;
1998 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
1999 this.textAntialiasMethod);
2003 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
2004 RenderingHints.VALUE_ANTIALIAS_ON);
2009 private void unsetAntialias(Graphics g)
2011 if (isVectorRendering())
2013 // no need to antialias vector drawings
2016 Graphics2D g2d = (Graphics2D) g;
2017 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
2018 RenderingHints.VALUE_ANTIALIAS_OFF);
2021 public void setVectorRendering(boolean b)
2023 renderingVectors = b;
2026 public boolean isVectorRendering()
2028 return renderingVectors;