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;
145 public AnnotationRenderer()
151 * Create a new annotation Renderer
154 * flag indicating if timing and redraw parameter info should be
157 public AnnotationRenderer(boolean debugRedraw)
159 this.debugRedraw = debugRedraw;
163 * Remove any references and resources when this object is no longer required
165 public void dispose()
167 hiddenColumns = null;
169 complementConsensus = null;
170 hStrucConsensus = null;
172 annotationPanel = null;
173 rendererFactoryI = null;
176 void drawStemAnnot(Graphics g, Annotation[] row_annotations, int lastSSX,
177 int x, int y, int iconOffset, int startRes, int column,
178 boolean validRes, boolean validEnd)
180 int sCol = (lastSSX / charWidth)
181 + hiddenColumns.visibleToAbsoluteColumn(startRes);
183 int x2 = (x * charWidth);
185 char dc = (column == 0 || row_annotations[column - 1] == null) ? ' '
186 : row_annotations[column - 1].secondaryStructure;
188 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
189 || dc != row_annotations[sCol - 1].secondaryStructure
191 boolean diffdownstream = !validRes || !validEnd
192 || row_annotations[column] == null
193 || dc != row_annotations[column].secondaryStructure;
195 if (diffupstream || diffdownstream)
197 // draw glyphline under arrow
198 drawGlyphLine(g, lastSSX, x, y, iconOffset);
200 g.setColor(STEM_COLOUR);
202 if (column > 0 && Rna.isClosingParenthesis(dc))
205 // if (validRes && column>1 && row_annotations[column-2]!=null &&
206 // dc.equals(row_annotations[column-2].displayCharacter))
209 * if new annotation with a closing base pair half of the stem,
210 * display a backward arrow
212 fillPolygon(g, new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
214 { y + iconOffset + 1, y + 13 + iconOffset,
215 y + 7 + iconOffset },
226 // display a forward arrow
230 * if annotation ending with an opeing base pair half of the stem,
231 * display a forward arrow
233 fillPolygon(g, new int[] { x2 - 6, x2 - 6, x2 - 1 },
235 { y + iconOffset + 1, y + 13 + iconOffset,
236 y + 7 + iconOffset },
247 fillRect(g, x1, y + 4 + iconOffset, x2 - x1, 6);
250 void drawNotCanonicalAnnot(Graphics g, Color nonCanColor,
251 Annotation[] row_annotations, int lastSSX, int x, int y,
252 int iconOffset, int startRes, int column, boolean validRes,
255 // Console.info(nonCanColor);
257 int sCol = (lastSSX / charWidth)
258 + hiddenColumns.visibleToAbsoluteColumn(startRes);
260 int x2 = (x * charWidth);
262 String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
263 : row_annotations[column - 1].displayCharacter;
265 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
266 || !dc.equals(row_annotations[sCol - 1].displayCharacter)
268 boolean diffdownstream = !validRes || !validEnd
269 || row_annotations[column] == null
270 || !dc.equals(row_annotations[column].displayCharacter);
271 // Console.info("Column "+column+" diff up:
273 // down:"+diffdownstream);
274 // If a closing base pair half of the stem, display a backward arrow
275 if (diffupstream || diffdownstream)
277 // draw glyphline under arrow
278 drawGlyphLine(g, lastSSX, x, y, iconOffset);
280 g.setColor(nonCanColor);
281 if (column > 0 && Rna.isClosingParenthesis(dc))
285 // if (validRes && column>1 && row_annotations[column-2]!=null &&
286 // dc.equals(row_annotations[column-2].displayCharacter))
288 fillPolygon(g, new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
290 { y + iconOffset + 1, y + 13 + iconOffset,
291 y + 7 + iconOffset },
303 // display a forward arrow
306 fillPolygon(g, new int[] { x2 - 6, x2 - 6, x2 - 1 },
308 { y + iconOffset + 1, y + 13 + iconOffset,
309 y + 7 + iconOffset },
320 fillRect(g, x1, y + 4 + iconOffset, x2 - x1, 6);
323 // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
325 public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
328 fm = annotPanel.getFontMetrics();
329 annotationPanel = annotPanel;
330 fadedImage = annotPanel.getFadedImage();
331 imgWidth = annotPanel.getFadedImageWidth();
332 // visible area for rendering
333 int[] bounds = annotPanel.getVisibleVRange();
337 visHeight = bounds[1];
352 rendererFactoryI = AnnotationRendererFactory.getRendererFactory();
353 updateFromAlignViewport(av);
356 public void updateFromAlignViewport(AlignViewportI av)
358 charWidth = av.getCharWidth();
359 endRes = av.getRanges().getEndRes();
360 charHeight = av.getCharHeight();
361 hasHiddenColumns = av.hasHiddenColumns();
362 validCharWidth = av.isValidCharWidth();
363 av_renderHistogram = av.isShowConsensusHistogram();
364 av_renderProfile = av.isShowSequenceLogo();
365 av_normaliseProfile = av.isNormaliseSequenceLogo();
366 profcolour = av.getResidueShading();
367 if (profcolour == null || profcolour.getColourScheme() == null)
370 * Use default colour for sequence logo if
371 * the alignment has no colourscheme set
372 * (would like to use user preference but n/a for applet)
374 ColourSchemeI col = av.getAlignment().isNucleotide()
375 ? new NucleotideColourScheme()
376 : new ZappoColourScheme();
377 profcolour = new ResidueShader(col);
379 columnSelection = av.getColumnSelection();
380 hiddenColumns = av.getAlignment().getHiddenColumns();
381 hconsensus = av.getSequenceConsensusHash();
382 complementConsensus = av.getComplementConsensusHash();
383 hStrucConsensus = av.getRnaStructureConsensusHash();
384 av_ignoreGapsConsensus = av.isIgnoreGapsConsensus();
388 * Returns profile data; the first element is the profile type, the second is
389 * the number of distinct values, the third the total count, and the remainder
390 * depend on the profile type.
396 int[] getProfileFor(AlignmentAnnotation aa, int column)
398 // TODO : consider refactoring the global alignment calculation
399 // properties/rendering attributes as a global 'alignment group' which holds
400 // all vis settings for the alignment as a whole rather than a subset
402 if (aa.autoCalculated && (aa.label.startsWith("Consensus")
403 || aa.label.startsWith("cDNA Consensus")))
405 boolean forComplement = aa.label.startsWith("cDNA Consensus");
406 if (aa.groupRef != null && aa.groupRef.consensusData != null
407 && aa.groupRef.isShowSequenceLogo())
409 // TODO? group consensus for cDNA complement
410 return AAFrequency.extractProfile(
411 aa.groupRef.consensusData.get(column),
412 aa.groupRef.getIgnoreGapsConsensus());
414 // TODO extend annotation row to enable dynamic and static profile data to
416 if (aa.groupRef == null && aa.sequenceRef == null)
420 return AAFrequency.extractCdnaProfile(complementConsensus[column],
421 av_ignoreGapsConsensus);
425 return AAFrequency.extractProfile(hconsensus.get(column),
426 av_ignoreGapsConsensus);
432 if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
434 // TODO implement group structure consensus
436 * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
437 * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
438 * group selections return StructureFrequency.extractProfile(
439 * aa.groupRef.consensusData[column], aa.groupRef
440 * .getIgnoreGapsConsensus()); }
442 // TODO extend annotation row to enable dynamic and static profile data
445 if (aa.groupRef == null && aa.sequenceRef == null
446 && hStrucConsensus != null
447 && hStrucConsensus.length > column)
449 return StructureFrequency.extractProfile(hStrucConsensus[column],
450 av_ignoreGapsConsensus);
459 private AnnotationRendererFactoryI rendererFactoryI;
462 * Render the annotation rows associated with an alignment.
467 * data and view settings to render
469 * destination for graphics
471 * row where a mouse event occured (or -1)
473 * first column that will be drawn
475 * last column that will be drawn
476 * @return true if the fadedImage was used for any alignment annotation rows
477 * currently being calculated
479 public boolean drawComponent(AwtRenderPanelI annotPanel,
480 AlignViewportI av, Graphics g, int activeRow, int startRes,
483 if (g instanceof EpsGraphics2D || g instanceof SVGGraphics2D)
485 this.setVectorRendering(true);
487 Graphics2D g2d = (Graphics2D) g;
489 long stime = System.currentTimeMillis();
490 boolean usedFaded = false;
492 // AnnotationPanel needs to implement: ImageObserver, access to
494 updateFromAwtRenderPanel(annotPanel, av);
495 fm = g.getFontMetrics();
496 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
507 boolean validRes = false;
508 boolean validEnd = false;
509 boolean labelAllCols = false;
510 // boolean centreColLabels;
511 // boolean centreColLabelsDef = av.isCentreColumnLabels();
512 boolean scaleColLabel = false;
513 final AlignmentAnnotation consensusAnnot = av
514 .getAlignmentConsensusAnnotation();
515 final AlignmentAnnotation structConsensusAnnot = av
516 .getAlignmentStrucConsensusAnnotation();
517 final AlignmentAnnotation complementConsensusAnnot = av
518 .getComplementConsensusAnnotation();
520 BitSet graphGroupDrawn = new BitSet();
521 int charOffset = 0; // offset for a label
524 int yfrom = 0, f_i = 0, yto = 0, f_to = 0;
525 boolean clipst = false, clipend = false;
526 for (int i = 0; i < aa.length; i++)
528 AlignmentAnnotation row = aa[i];
529 boolean renderHistogram = true;
530 boolean renderProfile = false;
531 boolean normaliseProfile = false;
532 boolean isRNA = row.isRNA();
534 // check if this is a consensus annotation row and set the display
535 // settings appropriately
536 // TODO: generalise this to have render styles for consensus/profile
538 if (row.groupRef != null && row == row.groupRef.getConsensus())
540 renderHistogram = row.groupRef.isShowConsensusHistogram();
541 renderProfile = row.groupRef.isShowSequenceLogo();
542 normaliseProfile = row.groupRef.isNormaliseSequenceLogo();
544 else if (row == consensusAnnot || row == structConsensusAnnot
545 || row == complementConsensusAnnot)
547 renderHistogram = av_renderHistogram;
548 renderProfile = av_renderProfile;
549 normaliseProfile = av_normaliseProfile;
552 Annotation[] row_annotations = row.annotations;
557 // centreColLabels = row.centreColLabels || centreColLabelsDef;
558 labelAllCols = row.showAllColLabels;
559 scaleColLabel = row.scaleColLabel;
563 if (!useClip || ((y - charHeight) < visHeight
564 && (y + row.height + charHeight * 2) >= sOffset))
565 {// if_in_visible_region
576 if (row.graphGroup > -1 && graphGroupDrawn.get(row.graphGroup))
581 // this is so that we draw the characters below the graph
586 iconOffset = charHeight - fm.getDescent();
590 else if (row.hasText)
592 iconOffset = charHeight - fm.getDescent();
600 if (row.autoCalculated && av.isCalculationInProgress(row))
604 g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0,
605 y - row.height, imgWidth, y, annotationPanel);
606 g.setColor(Color.black);
607 // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
613 * else if (annotationPanel.av.updatingConservation &&
614 * aa[i].label.equals("Conservation")) {
616 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
617 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
618 * annotationPanel.imgWidth, y, annotationPanel);
620 * g.setColor(Color.black); //
621 * g.drawString("Calculating Conservation.....",20, y-row.height/2);
623 * continue; } else if (annotationPanel.av.updatingConservation &&
624 * aa[i].label.equals("Quality")) {
626 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
627 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
628 * annotationPanel.imgWidth, y, annotationPanel);
629 * g.setColor(Color.black); // /
630 * g.drawString("Calculating Quality....",20, y-row.height/2);
635 // first pass sets up state for drawing continuation from left-hand
639 // flag used for vector rendition
640 this.glyphLineDrawn = false;
641 x = (startRes == 0) ? 0 : -1;
642 while (x < endRes - startRes)
644 if (hasHiddenColumns)
646 column = hiddenColumns.visibleToAbsoluteColumn(startRes + x);
647 if (column > row_annotations.length - 1)
654 column = startRes + x;
657 if ((row_annotations == null)
658 || (row_annotations.length <= column)
659 || (row_annotations[column] == null))
667 final String displayChar = validRes
668 ? row_annotations[column].displayCharacter
675 g.setColor(Color.red);
677 if (columnSelection != null)
679 if (columnSelection.contains(column))
681 fillRect(g, x * charWidth, y, charWidth, charHeight);
685 if (row.getInvalidStrucPos() > x)
687 g.setColor(Color.orange);
688 fillRect(g, x * charWidth, y, charWidth, charHeight);
690 else if (row.getInvalidStrucPos() == x)
692 g.setColor(Color.orange.darker());
693 fillRect(g, x * charWidth, y, charWidth, charHeight);
695 if (validCharWidth && validRes && displayChar != null
696 && (displayChar.length() > 0))
698 // Graphics2D gg = (g);
699 float fmWidth = fm.charsWidth(displayChar.toCharArray(), 0,
700 displayChar.length());
703 * shrink label width to fit in column, if that is
704 * both configured and necessary
706 boolean scaledToFit = false;
707 float fmScaling = 1f;
708 if (scaleColLabel && fmWidth > charWidth)
711 fmScaling = charWidth;
712 fmScaling /= fmWidth;
713 // and update the label's width to reflect the scaling.
717 charOffset = (int) ((charWidth - fmWidth) / 2f);
719 if (row_annotations[column].colour == null)
721 g2d.setColor(Color.black);
725 g2d.setColor(row_annotations[column].colour);
729 * draw the label, unless it is the same secondary structure
730 * symbol (excluding RNA Helix) as the previous column
732 final int xPos = (x * charWidth) + charOffset;
733 final int yPos = y + iconOffset;
736 * translate to drawing position _before_ applying any scaling
738 g2d.translate(xPos, yPos);
742 * use a scaling transform to make the label narrower
743 * (JalviewJS doesn't have Font.deriveFont(AffineTransform))
746 AffineTransform.getScaleInstance(fmScaling, 1.0));
749 if (column == 0 || row.graph > 0)
751 g2d.drawString(displayChar, 0, 0);
753 else if (row_annotations[column - 1] == null || (labelAllCols
754 || !displayChar.equals(
755 row_annotations[column - 1].displayCharacter)
756 || (displayChar.length() < 2
757 && row_annotations[column].secondaryStructure == ' ')))
759 g2d.drawString(displayChar, 0, 0);
764 * undo scaling before translating back
765 * (restoring saved transform does NOT work in JS PDFGraphics!)
767 g2d.transform(AffineTransform
768 .getScaleInstance(1D / fmScaling, 1.0));
770 g2d.translate(-xPos, -yPos);
775 char ss = validRes ? row_annotations[column].secondaryStructure
780 // distinguish between forward/backward base-pairing
781 if (displayChar.indexOf(')') > -1)
790 if ((displayChar.indexOf(']') > -1))
798 // distinguish between forward/backward base-pairing
799 if (displayChar.indexOf('}') > -1)
807 // distinguish between forward/backward base-pairing
808 if (displayChar.indexOf('<') > -1)
814 if (isRNA && (ss >= CHAR_A) && (ss <= CHAR_Z))
816 // distinguish between forward/backward base-pairing
817 int ssLowerCase = ss + UPPER_TO_LOWER;
818 // TODO would .equals() be safer here? or charAt(0)?
819 if (displayChar.indexOf(ssLowerCase) > -1)
821 ss = (char) ssLowerCase;
825 if (!validRes || (ss != lastSS))
831 // int nb_annot = x - temp;
832 // Console.info("\t type :"+lastSS+"\t x
834 // annot :"+nb_annot);
837 case '(': // Stem case for RNA secondary structure
838 case ')': // and opposite direction
839 drawStemAnnot(g, row_annotations, lastSSX, x, y,
840 iconOffset, startRes, column, validRes, validEnd);
847 drawHelixAnnot(g, row_annotations, lastSSX, x, y,
848 iconOffset, startRes, column, validRes,
852 // no break if isRNA - falls through to drawNotCanonicalAnnot!
856 drawSheetAnnot(g, row_annotations, lastSSX, x, y,
857 iconOffset, startRes, column, validRes,
861 // no break if isRNA - fall through to drawNotCanonicalAnnot!
920 Color nonCanColor = getNotCanonicalColor(lastSS);
921 drawNotCanonicalAnnot(g, nonCanColor, row_annotations,
922 lastSSX, x, y, iconOffset, startRes, column,
927 if (isVectorRendering())
929 // draw single full width glyphline
930 drawGlyphLine(g, lastSSX, endRes - x, y, iconOffset);
931 // disable more glyph lines
932 this.glyphLineDrawn = true;
936 drawGlyphLine(g, lastSSX, x, y, iconOffset);
951 lastSSX = (x * charWidth);
958 if (column >= row_annotations.length)
960 column = row_annotations.length - 1;
967 if ((row_annotations == null) || (row_annotations.length <= column)
968 || (row_annotations[column] == null))
986 drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
987 startRes, column, validRes, validEnd);
990 // no break if isRNA - fall through to drawNotCanonicalAnnot!
995 drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
996 startRes, column, validRes, validEnd);
999 // no break if isRNA - fall through to drawNotCanonicalAnnot!
1002 case ')': // Stem case for RNA secondary structure
1004 drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
1005 startRes, column, validRes, validEnd);
1062 // Console.info(lastSS);
1063 Color nonCanColor = getNotCanonicalColor(lastSS);
1064 drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX,
1065 x, y, iconOffset, startRes, column, validRes, validEnd);
1068 if (isVectorRendering())
1070 // draw single full width glyphline
1071 drawGlyphLine(g, lastSSX, endRes - x, y, iconOffset);
1072 // disable more glyph lines
1073 this.glyphLineDrawn = true;
1077 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1083 if (row.graph > 0 && row.graphHeight > 0)
1085 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1087 if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
1089 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1090 // computed efficiently for all visible labels
1091 float groupmax = -999999, groupmin = 9999999;
1092 for (int gg = 0; gg < aa.length; gg++)
1094 if (aa[gg].graphGroup != row.graphGroup)
1101 aa[gg].visible = false;
1103 if (aa[gg].graphMax > groupmax)
1105 groupmax = aa[gg].graphMax;
1107 if (aa[gg].graphMin < groupmin)
1109 groupmin = aa[gg].graphMin;
1113 for (int gg = 0; gg < aa.length; gg++)
1115 if (aa[gg].graphGroup == row.graphGroup)
1117 drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
1118 endRes, y, groupmin, groupmax, row.graphHeight);
1122 graphGroupDrawn.set(row.graphGroup);
1126 drawLineGraph(g, row, row_annotations, startRes, endRes, y,
1127 row.graphMin, row.graphMax, row.graphHeight);
1130 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1132 drawBarGraph(g, row, row_annotations, startRes, endRes,
1133 row.graphMin, row.graphMax, y, renderHistogram,
1134 renderProfile, normaliseProfile);
1138 AnnotationRowRendererI renderer = rendererFactoryI
1139 .getRendererFor(row);
1140 if (renderer != null)
1142 renderer.renderRow(g, charWidth, charHeight, hasHiddenColumns,
1143 av, hiddenColumns, columnSelection, row,
1144 row_annotations, startRes, endRes, row.graphMin,
1149 if (renderer == null)
1152 .println("No renderer found for " + row.toString());
1157 "rendered with " + renderer.getClass().toString());
1166 if (clipst && !clipend)
1170 } // end if_in_visible_region
1171 if (row.graph > 0 && row.hasText)
1187 Console.warn("Start clip at : " + yfrom + " (index " + f_i + ")");
1191 Console.warn("End clip at : " + yto + " (index " + f_to + ")");
1195 Console.warn("Annotation Rendering time:"
1196 + (System.currentTimeMillis() - stime));
1203 public static final Color GLYPHLINE_COLOR = Color.gray;
1205 public static final Color SHEET_COLOUR = Color.green;
1207 public static final Color HELIX_COLOUR = Color.red;
1209 public static final Color STEM_COLOUR = Color.blue;
1211 // private Color sdNOTCANONICAL_COLOUR;
1213 void drawGlyphLine(Graphics g, int lastSSX, int x, int y, int iconOffset)
1217 // if we've drawn a single long glyphline for an export, don't draw the
1222 g.setColor(GLYPHLINE_COLOR);
1223 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
1226 void drawSheetAnnot(Graphics g, Annotation[] row,
1228 int lastSSX, int x, int y, int iconOffset, int startRes,
1229 int column, boolean validRes, boolean validEnd)
1231 if (!validEnd || !validRes || row == null || row[column] == null
1232 || row[column].secondaryStructure != 'E')
1234 // draw the glyphline underneath
1235 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1237 g.setColor(SHEET_COLOUR);
1238 fillRect(g, lastSSX, y + 4 + iconOffset,
1239 (x * charWidth) - lastSSX - 4, 6);
1242 { (x * charWidth) - 6, (x * charWidth) - 6,
1243 (x * charWidth - 1) },
1245 { y + iconOffset + 1, y + 13 + iconOffset,
1246 y + 7 + iconOffset },
1251 g.setColor(SHEET_COLOUR);
1252 fillRect(g, lastSSX, y + 4 + iconOffset, (x * charWidth) - lastSSX,
1257 void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x,
1258 int y, int iconOffset, int startRes, int column, boolean validRes,
1261 int sCol = (lastSSX / charWidth)
1262 + hiddenColumns.visibleToAbsoluteColumn(startRes);
1264 int x2 = (x * charWidth);
1266 if (USE_FILL_ROUND_RECT || isVectorRendering())
1268 // draw glyph line behind helix (visible in EPS or SVG output)
1269 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1271 g.setColor(HELIX_COLOUR);
1273 int ofs = charWidth / 2;
1274 // Off by 1 offset when drawing rects and ovals
1275 // to offscreen image on the MAC
1276 fillRoundRect(g, lastSSX, y + 3 + iconOffset, x2 - x1 - 1, 8, 8, 8);
1277 if (sCol == 0 || row[sCol - 1] == null
1278 || row[sCol - 1].secondaryStructure != 'H')
1283 fillRoundRect(g, lastSSX, y + 3 + iconOffset, x2 - x1 - ofs, 8, 0,
1286 if (!validRes || row[column] == null
1287 || row[column].secondaryStructure != 'H')
1293 fillRoundRect(g, lastSSX + ofs, y + 3 + iconOffset, x2 - x1 - ofs,
1300 boolean leftEnd = sCol == 0 || row[sCol - 1] == null
1301 || row[sCol - 1].secondaryStructure != 'H';
1302 boolean rightEnd = !validRes || row[column] == null
1303 || row[column].secondaryStructure != 'H';
1305 if (leftEnd || rightEnd)
1307 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1309 g.setColor(HELIX_COLOUR);
1313 fillArc(g, lastSSX, y + 3 + iconOffset, charWidth, 8, 90, 180);
1314 x1 += charWidth / 2;
1319 fillArc(g, ((x - 1) * charWidth), y + 3 + iconOffset, charWidth, 8,
1321 x2 -= charWidth / 2;
1324 fillRect(g, x1, y + 3 + iconOffset, x2 - x1, 8);
1327 void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1328 Annotation[] aa_annotations, int sRes, int eRes, int y, float min,
1329 float max, int graphHeight)
1331 if (sRes > aa_annotations.length)
1335 Stroke roundStroke = new BasicStroke(1, BasicStroke.CAP_ROUND,
1336 BasicStroke.JOIN_ROUND);
1337 Stroke squareStroke = new BasicStroke(1, BasicStroke.CAP_SQUARE,
1338 BasicStroke.JOIN_MITER);
1339 Graphics2D g2d = (Graphics2D) g;
1340 Stroke prevStroke = g2d.getStroke();
1341 g2d.setStroke(roundStroke);
1345 // Adjustment for fastpaint to left
1351 eRes = Math.min(eRes, aa_annotations.length);
1354 float range = max - min;
1359 y2 = y - (int) ((0 - min / range) * graphHeight);
1362 g.setColor(Color.gray);
1363 drawLine(g, squareStroke, x * charWidth, y2, (eRes - sRes) * charWidth,
1371 eRes = Math.min(eRes, aa_annotations.length);
1374 int aaMax = aa_annotations.length - 1;
1376 while (x <= eRes - sRes)
1379 if (hasHiddenColumns)
1381 column = hiddenColumns.visibleToAbsoluteColumn(column);
1389 if (aa_annotations[column] == null)
1395 if (aa_annotations[column].colour == null)
1397 g.setColor(Color.black);
1401 g.setColor(aa_annotations[column].colour);
1404 boolean previousValueExists = column > 0
1405 && aa_annotations[column - 1] != null;
1406 float previousValue = previousValueExists
1407 ? aa_annotations[column - 1].value
1409 float thisValue = aa_annotations[column].value;
1410 boolean nextValueExists = aa_annotations.length > column + 1
1411 && aa_annotations[column + 1] != null;
1412 float nextValue = nextValueExists ? aa_annotations[column + 1].value
1415 // check for standalone value
1416 if (!previousValueExists && !nextValueExists)
1418 y2 = y - yValueToPixelHeight(thisValue, min, range, graphHeight);
1419 drawLine(g, x * charWidth + charWidth / 4, y2,
1420 x * charWidth + 3 * charWidth / 4, y2);
1425 if (!previousValueExists)
1431 y1 = y - yValueToPixelHeight(previousValue, min, range, graphHeight);
1432 y2 = y - yValueToPixelHeight(thisValue, min, range, graphHeight);
1436 // only draw an initial half-line
1437 drawLine(g, x * charWidth, y1 + (y2 - y1) / 2,
1438 x * charWidth + charWidth / 2, y2);
1441 else if (x == eRes - sRes)
1443 // this is one past the end to draw -- only draw a half line
1444 drawLine(g, (x - 1) * charWidth + charWidth / 2, y1,
1445 x * charWidth - 1, y1 + (y2 - y1) / 2);
1450 drawLine(g, (x - 1) * charWidth + charWidth / 2, y1,
1451 x * charWidth + charWidth / 2, y2);
1456 if (_aa.threshold != null)
1458 g.setColor(_aa.threshold.colour);
1459 Graphics2D g2 = (Graphics2D) g;
1460 y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1461 drawLine(g, dashedLine(charWidth), 0, y2, (eRes - sRes) * charWidth,
1464 g2d.setStroke(prevStroke);
1467 private static double log2 = Math.log(2);
1469 // Cached dashed line Strokes
1470 private static Map<Integer, Stroke> dashedLineLookup = new HashMap<>();
1473 * Returns a dashed line stroke as close to 6-4 pixels as fits within the
1474 * charWidth. This allows translations of multiples of charWidth without
1475 * disrupting the dashed line. The exact values are 0.6-0.4 proportions of
1476 * charWidth for charWidth under 16. For charWidth 16 or over, the number of
1477 * dashes doubles as charWidth doubles.
1480 * @return Stroke with appropriate dashed line fitting exactly within the
1483 private static Stroke dashedLine(int charWidth)
1485 if (!dashedLineLookup.containsKey(charWidth))
1487 int power2 = charWidth >= 16 ? (int) (Math.log(charWidth) / log2) : 2;
1488 float width = ((float) charWidth) / ((float) Math.pow(2, power2 - 2));
1489 float segment1 = width * 0.6f;
1490 float segment2 = width - segment1;
1491 dashedLineLookup.put(charWidth, new BasicStroke(1,
1492 BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 3f, new float[]
1493 { segment1, segment2 }, 0f));
1495 return dashedLineLookup.get(charWidth);
1498 private static int yValueToPixelHeight(float value, float min,
1499 float range, int graphHeight)
1501 return (int) (((value - min) / range) * graphHeight);
1504 @SuppressWarnings("unused")
1505 void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1506 Annotation[] aa_annotations, int sRes, int eRes, float min,
1507 float max, int y, boolean renderHistogram, boolean renderProfile,
1508 boolean normaliseProfile)
1510 if (sRes > aa_annotations.length)
1514 Font ofont = g.getFont();
1515 eRes = Math.min(eRes, aa_annotations.length);
1517 int x = 0, y1 = y, y2 = y;
1519 float range = max - min;
1523 y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1526 g.setColor(Color.gray);
1528 drawLine(g, x, y2, (eRes - sRes) * charWidth, y2);
1531 int aaMax = aa_annotations.length - 1;
1532 while (x < eRes - sRes)
1535 if (hasHiddenColumns)
1537 column = hiddenColumns.visibleToAbsoluteColumn(column);
1545 if (aa_annotations[column] == null)
1550 if (aa_annotations[column].colour == null)
1552 g.setColor(Color.black);
1556 g.setColor(aa_annotations[column].colour);
1559 y1 = y - (int) (((aa_annotations[column].value - min) / (range))
1562 if (renderHistogram)
1566 fillRect(g, x * charWidth, y2, charWidth, y1 - y2);
1570 fillRect(g, x * charWidth, y1, charWidth, y2 - y1);
1573 // draw profile if available
1578 * {profile type, #values, total count, char1, pct1, char2, pct2...}
1580 int profl[] = getProfileFor(_aa, column);
1582 // just try to draw the logo if profl is not null
1583 if (profl != null && profl[2] != 0)
1585 boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
1586 boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
1587 float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1588 final double normaliseFactor = normaliseProfile ? _aa.graphHeight
1592 * Render a single base for a sequence profile, a base pair for
1593 * structure profile, and a triplet for a cdna profile
1595 char[] dc = new char[isStructureProfile ? 2
1596 : (isCdnaProfile ? 3 : 1)];
1598 // lm is not necessary - we can just use fm - could be off by no more
1600 // LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1601 // Console.info(asc + " " + dec + " " + (asc -
1603 // + " " + (dec - lm.getDescent()));
1605 double asc = fm.getAscent();
1606 double dec = fm.getDescent();
1607 double fht = fm.getHeight();
1609 double scale = 1f / (normaliseProfile ? profl[2] : 100f);
1610 // float ofontHeight = 1f / fm.getAscent();// magnify to fill box
1613 * Traverse the character(s)/percentage data in the array
1618 // profl[1] is the number of values in the profile
1619 for (int i = 0, c = 3, last = profl[1]; i < last; i++)
1623 if (isStructureProfile)
1625 // todo can we encode a structure pair as an int, like codons?
1626 dc[0] = (char) profl[c++];
1627 dc[1] = (char) profl[c++];
1630 else if (isCdnaProfile)
1632 CodingUtils.decodeCodon2(profl[c++], dc);
1637 dc[0] = (char) profl[c++];
1640 // next profl[] position is profile % for the character(s)
1642 int percent = profl[c++];
1645 // failsafe in case a count rounds down to 0%
1648 double newHeight = normaliseFactor * scale * percent;
1651 * Set character colour as per alignment colour scheme; use the
1652 * codon translation if a cDNA profile
1654 Color colour = null;
1657 final String codonTranslation = ResidueProperties
1659 colour = profcolour.findColour(codonTranslation.charAt(0),
1664 colour = profcolour.findColour(dc[0], column, null);
1666 g.setColor(colour == Color.white ? Color.lightGray : colour);
1668 double sx = 1f * charWidth / fm.charsWidth(dc, 0, dc.length);
1669 double sy = newHeight / asc;
1670 double newAsc = asc * sy;
1671 double newDec = dec * sy;
1672 // it is not necessary to recalculate lm for the new font.
1673 // note: lm.getBaselineOffsets()[lm.getBaselineIndex()]) must be 0
1674 // by definition. Was:
1675 // int hght = (int) (ht + (newAsc - newDec);
1676 // - lm.getBaselineOffsets()[lm.getBaselineIndex()]));
1678 if (Platform.isJS())
1681 * SwingJS does not implement font.deriveFont()
1682 * so use a scaling transform to draw instead,
1683 * this is off by a very small amount
1685 final int hght = (int) (ht2 + (newAsc - newDec));
1686 Graphics2D gg = (Graphics2D) g;
1687 int xShift = (int) Math.round(x * charWidth / sx);
1688 int yShift = (int) Math.round(hght / sy);
1689 gg.transform(AffineTransform.getScaleInstance(sx, sy));
1690 gg.drawString(s, xShift, yShift);
1692 AffineTransform.getScaleInstance(1D / sx, 1D / sy));
1702 // Java ('normal') method is to scale the font to fit
1704 final int hght = (int) (ht + (newAsc - newDec));
1706 .deriveFont(AffineTransform.getScaleInstance(sx, sy));
1708 g.drawChars(dc, 0, dc.length, x * charWidth, hght);
1718 if (_aa.threshold != null)
1720 g.setColor(_aa.threshold.colour);
1721 Stroke s = new BasicStroke(1, BasicStroke.CAP_SQUARE,
1722 BasicStroke.JOIN_ROUND, 3f, new float[]
1726 - ((_aa.threshold.value - min) / range) * _aa.graphHeight);
1727 drawLine(g, s, 0, y2, (eRes - sRes) * charWidth, y2);
1731 // used by overview window
1732 public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1733 Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1735 eRes = Math.min(eRes, aa_annotations.length);
1736 g.setColor(Color.white);
1737 fillRect(g, 0, 0, width, y);
1738 g.setColor(new Color(0, 0, 180));
1742 for (int j = sRes; j < eRes; j++)
1744 if (aa_annotations[j] != null)
1746 if (aa_annotations[j].colour == null)
1748 g.setColor(Color.black);
1752 g.setColor(aa_annotations[j].colour);
1755 height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1761 fillRect(g, x, y - height, charWidth, height);
1767 Color getNotCanonicalColor(char lastss)
1773 return new Color(255, 125, 5);
1777 return new Color(245, 115, 10);
1781 return new Color(235, 135, 15);
1785 return new Color(225, 105, 20);
1789 return new Color(215, 145, 30);
1793 return new Color(205, 95, 35);
1797 return new Color(195, 155, 45);
1801 return new Color(185, 85, 55);
1805 return new Color(175, 165, 65);
1809 return new Color(170, 75, 75);
1813 return new Color(160, 175, 85);
1817 return new Color(150, 65, 95);
1821 return new Color(140, 185, 105);
1825 return new Color(130, 55, 110);
1829 return new Color(120, 195, 120);
1833 return new Color(110, 45, 130);
1837 return new Color(100, 205, 140);
1841 return new Color(90, 35, 150);
1845 return new Color(85, 215, 160);
1849 return new Color(75, 25, 170);
1853 return new Color(65, 225, 180);
1857 return new Color(55, 15, 185);
1861 return new Color(45, 235, 195);
1865 return new Color(35, 5, 205);
1869 return new Color(25, 245, 215);
1873 return new Color(15, 0, 225);
1877 return new Color(10, 255, 235);
1881 return new Color(5, 150, 245);
1885 return new Color(0, 80, 255);
1888 Console.info("This is not a interaction : " + lastss);
1894 private void fillPolygon(Graphics g, int[] xpoints, int[] ypoints, int n)
1897 g.fillPolygon(xpoints, ypoints, n);
1901 private void fillRect(Graphics g, int a, int b, int c, int d)
1903 fillRect(g, false, a, b, c, d);
1906 private void fillRect(Graphics g, int a, int b, int c, int d)
1909 g.fillRect(a, b, c, d);
1912 private void fillRoundRect(Graphics g, int a, int b, int c, int d, int e,
1916 g.fillRoundRect(a, b, c, d, e, f);
1919 private void fillArc(Graphics g, int a, int b, int c, int d, int e, int f)
1922 g.fillArc(a, b, c, d, e, f);
1925 private void drawLine(Graphics g, Stroke s, int a, int b, int c, int d)
1927 Graphics2D g2d = (Graphics2D) g;
1928 Stroke p = g2d.getStroke();
1930 drawLine(g, a, b, c, d);
1934 private void drawLine(Graphics g, int a, int b, int c, int d)
1937 g.drawLine(a, b, c, d);
1940 private void setAntialias(Graphics g)
1942 if (isVectorRendering())
1944 // no need to antialias vector drawings
1947 if (Cache.getDefault("ANTI_ALIAS", true))
1949 Graphics2D g2d = (Graphics2D) g;
1950 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1951 RenderingHints.VALUE_ANTIALIAS_ON);
1955 private void unsetAntialias(Graphics g)
1957 if (isVectorRendering())
1959 // no need to antialias vector drawings
1962 Graphics2D g2d = (Graphics2D) g;
1963 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1964 RenderingHints.VALUE_ANTIALIAS_OFF);
1967 public void setVectorRendering(boolean b)
1969 renderingVectors = b;
1972 public boolean isVectorRendering()
1974 return renderingVectors;