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.Hashtable;
37 import org.jfree.graphics2d.svg.SVGGraphics2D;
38 import org.jibble.epsgraphics.EpsGraphics2D;
40 import jalview.analysis.AAFrequency;
41 import jalview.analysis.AlignmentUtils;
42 import jalview.analysis.CodingUtils;
43 import jalview.analysis.Rna;
44 import jalview.analysis.StructureFrequency;
45 import jalview.api.AlignViewportI;
46 import jalview.bin.Cache;
47 import jalview.bin.Console;
48 import jalview.datamodel.AlignmentAnnotation;
49 import jalview.datamodel.Annotation;
50 import jalview.datamodel.ColumnSelection;
51 import jalview.datamodel.HiddenColumns;
52 import jalview.datamodel.ProfilesI;
53 import jalview.renderer.api.AnnotationRendererFactoryI;
54 import jalview.renderer.api.AnnotationRowRendererI;
55 import jalview.schemes.ColourSchemeI;
56 import jalview.schemes.NucleotideColourScheme;
57 import jalview.schemes.ResidueProperties;
58 import jalview.schemes.ZappoColourScheme;
59 import jalview.util.MessageManager;
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 ProfilesI hSSconsensus;
96 private Hashtable<String, Object>[] complementConsensus;
98 private Hashtable<String, Object>[] hStrucConsensus;
100 private boolean av_ignoreGapsConsensus;
102 private boolean renderingVectors = false;
104 private boolean glyphLineDrawn = false;
107 * attributes set from AwtRenderPanelI
110 * old image used when data is currently being calculated and cannot be
113 private Image fadedImage;
116 * panel being rendered into
118 private ImageObserver annotationPanel;
121 * width of image to render in panel
123 private int imgWidth;
126 * offset to beginning of visible area
131 * offset to end of visible area
133 private int visHeight;
136 * indicate if the renderer should only render the visible portion of the
137 * annotation given the current view settings
139 private boolean useClip = true;
142 * master flag indicating if renderer should ever try to clip. not enabled for
145 private boolean canClip = false;
147 public AnnotationRenderer()
153 * Create a new annotation Renderer
156 * flag indicating if timing and redraw parameter info should be
159 public AnnotationRenderer(boolean debugRedraw)
161 this.debugRedraw = debugRedraw;
165 * Remove any references and resources when this object is no longer required
167 public void dispose()
169 hiddenColumns = null;
172 complementConsensus = null;
173 hStrucConsensus = null;
175 annotationPanel = null;
176 rendererFactoryI = null;
179 void drawStemAnnot(Graphics g, Annotation[] row_annotations, int lastSSX,
180 int x, int y, int iconOffset, int startRes, int column,
181 boolean validRes, boolean validEnd)
183 int sCol = (lastSSX / charWidth)
184 + hiddenColumns.visibleToAbsoluteColumn(startRes);
186 int x2 = (x * charWidth);
188 char dc = (column == 0 || row_annotations[column - 1] == null) ? ' '
189 : row_annotations[column - 1].secondaryStructure;
191 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
192 || dc != row_annotations[sCol - 1].secondaryStructure
194 boolean diffdownstream = !validRes || !validEnd
195 || row_annotations[column] == null
196 || dc != row_annotations[column].secondaryStructure;
198 if (diffupstream || diffdownstream)
200 // draw glyphline under arrow
201 drawGlyphLine(g, lastSSX, x, y, iconOffset);
203 g.setColor(STEM_COLOUR);
205 if (column > 0 && Rna.isClosingParenthesis(dc))
208 // if (validRes && column>1 && row_annotations[column-2]!=null &&
209 // dc.equals(row_annotations[column-2].displayCharacter))
212 * if new annotation with a closing base pair half of the stem,
213 * display a backward arrow
215 fillPolygon(g, new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
217 { y + iconOffset + 1, y + 13 + iconOffset,
218 y + 7 + iconOffset },
229 // display a forward arrow
233 * if annotation ending with an opeing base pair half of the stem,
234 * display a forward arrow
236 fillPolygon(g, new int[] { x2 - 6, x2 - 6, x2 - 1 },
238 { y + iconOffset + 1, y + 13 + iconOffset,
239 y + 7 + iconOffset },
250 fillRect(g, x1, y + 4 + iconOffset, x2 - x1, 6);
253 void drawNotCanonicalAnnot(Graphics g, Color nonCanColor,
254 Annotation[] row_annotations, int lastSSX, int x, int y,
255 int iconOffset, int startRes, int column, boolean validRes,
258 // Console.info(nonCanColor);
260 int sCol = (lastSSX / charWidth)
261 + hiddenColumns.visibleToAbsoluteColumn(startRes);
263 int x2 = (x * charWidth);
265 String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
266 : row_annotations[column - 1].displayCharacter;
268 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
269 || !dc.equals(row_annotations[sCol - 1].displayCharacter)
271 boolean diffdownstream = !validRes || !validEnd
272 || row_annotations[column] == null
273 || !dc.equals(row_annotations[column].displayCharacter);
274 // Console.info("Column "+column+" diff up:
276 // down:"+diffdownstream);
277 // If a closing base pair half of the stem, display a backward arrow
278 if (diffupstream || diffdownstream)
280 // draw glyphline under arrow
281 drawGlyphLine(g, lastSSX, x, y, iconOffset);
283 g.setColor(nonCanColor);
284 if (column > 0 && Rna.isClosingParenthesis(dc))
288 // if (validRes && column>1 && row_annotations[column-2]!=null &&
289 // dc.equals(row_annotations[column-2].displayCharacter))
291 fillPolygon(g, new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
293 { y + iconOffset + 1, y + 13 + iconOffset,
294 y + 7 + iconOffset },
306 // display a forward arrow
309 fillPolygon(g, new int[] { x2 - 6, x2 - 6, x2 - 1 },
311 { y + iconOffset + 1, y + 13 + iconOffset,
312 y + 7 + iconOffset },
323 fillRect(g, x1, y + 4 + iconOffset, x2 - x1, 6);
326 // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
328 public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
331 fm = annotPanel.getFontMetrics();
332 annotationPanel = annotPanel;
333 fadedImage = annotPanel.getFadedImage();
334 imgWidth = annotPanel.getFadedImageWidth();
335 // visible area for rendering
336 int[] bounds = annotPanel.getVisibleVRange();
340 visHeight = bounds[1];
355 rendererFactoryI = AnnotationRendererFactory.getRendererFactory();
356 updateFromAlignViewport(av);
359 public void updateFromAlignViewport(AlignViewportI av)
361 charWidth = av.getCharWidth();
362 endRes = av.getRanges().getEndRes();
363 charHeight = av.getCharHeight();
364 hasHiddenColumns = av.hasHiddenColumns();
365 validCharWidth = av.isValidCharWidth();
366 av_renderHistogram = av.isShowConsensusHistogram();
367 av_renderProfile = av.isShowSequenceLogo();
368 av_normaliseProfile = av.isNormaliseSequenceLogo();
369 profcolour = av.getResidueShading();
370 if (profcolour == null || profcolour.getColourScheme() == null)
373 * Use default colour for sequence logo if
374 * the alignment has no colourscheme set
375 * (would like to use user preference but n/a for applet)
377 ColourSchemeI col = av.getAlignment().isNucleotide()
378 ? new NucleotideColourScheme()
379 : new ZappoColourScheme();
380 profcolour = new ResidueShader(col);
382 columnSelection = av.getColumnSelection();
383 hiddenColumns = av.getAlignment().getHiddenColumns();
384 hconsensus = av.getSequenceConsensusHash();
385 hSSconsensus = av.getSequenceSSConsensusHash();
386 complementConsensus = av.getComplementConsensusHash();
387 hStrucConsensus = av.getRnaStructureConsensusHash();
388 av_ignoreGapsConsensus = av.isIgnoreGapsConsensus();
392 * Returns profile data; the first element is the profile type, the second is
393 * the number of distinct values, the third the total count, and the remainder
394 * depend on the profile type.
400 int[] getProfileFor(AlignmentAnnotation aa, int column)
402 // TODO : consider refactoring the global alignment calculation
403 // properties/rendering attributes as a global 'alignment group' which holds
404 // all vis settings for the alignment as a whole rather than a subset
406 if (aa.autoCalculated && (aa.label.startsWith("Consensus")
407 || aa.label.startsWith("cDNA Consensus")))
409 boolean forComplement = aa.label.startsWith("cDNA Consensus");
410 if (aa.groupRef != null && aa.groupRef.consensusData != null
411 && aa.groupRef.isShowSequenceLogo())
413 // TODO? group consensus for cDNA complement
414 return AAFrequency.extractProfile(
415 aa.groupRef.consensusData.get(column),
416 aa.groupRef.getIgnoreGapsConsensus());
418 // TODO extend annotation row to enable dynamic and static profile data to
420 if (aa.groupRef == null && aa.sequenceRef == null)
424 return AAFrequency.extractCdnaProfile(complementConsensus[column],
425 av_ignoreGapsConsensus);
429 return AAFrequency.extractProfile(hconsensus.get(column),
430 av_ignoreGapsConsensus);
435 else if(aa.autoCalculated && aa.label.startsWith(MessageManager.getString("label.ssconsensus_label")))
437 return AAFrequency.extractProfile(
438 hSSconsensus.get(column),
439 av_ignoreGapsConsensus);
445 if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
447 // TODO implement group structure consensus
449 * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
450 * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
451 * group selections return StructureFrequency.extractProfile(
452 * aa.groupRef.consensusData[column], aa.groupRef
453 * .getIgnoreGapsConsensus()); }
455 // TODO extend annotation row to enable dynamic and static profile data
458 if (aa.groupRef == null && aa.sequenceRef == null
459 && hStrucConsensus != null
460 && hStrucConsensus.length > column)
462 return StructureFrequency.extractProfile(hStrucConsensus[column],
463 av_ignoreGapsConsensus);
472 private AnnotationRendererFactoryI rendererFactoryI;
475 * Render the annotation rows associated with an alignment.
480 * data and view settings to render
482 * destination for graphics
484 * row where a mouse event occured (or -1)
486 * first column that will be drawn
488 * last column that will be drawn
489 * @return true if the fadedImage was used for any alignment annotation rows
490 * currently being calculated
492 public boolean drawComponent(AwtRenderPanelI annotPanel,
493 AlignViewportI av, Graphics g, int activeRow, int startRes,
496 if (g instanceof EpsGraphics2D || g instanceof SVGGraphics2D)
498 this.setVectorRendering(true);
500 Graphics2D g2d = (Graphics2D) g;
502 long stime = System.currentTimeMillis();
503 boolean usedFaded = false;
505 // AnnotationPanel needs to implement: ImageObserver, access to
507 updateFromAwtRenderPanel(annotPanel, av);
508 fm = g.getFontMetrics();
509 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
520 boolean validRes = false;
521 boolean validEnd = false;
522 boolean labelAllCols = false;
523 // boolean centreColLabels;
524 // boolean centreColLabelsDef = av.isCentreColumnLabels();
525 boolean scaleColLabel = false;
526 final AlignmentAnnotation consensusAnnot = av
527 .getAlignmentConsensusAnnotation();
528 final AlignmentAnnotation structConsensusAnnot = av
529 .getAlignmentStrucConsensusAnnotation();
530 final AlignmentAnnotation complementConsensusAnnot = av
531 .getComplementConsensusAnnotation();
532 final AlignmentAnnotation ssConsensusAnnot = av
533 .getAlignmentSecondaryStructureConsensusAnnotation();
535 BitSet graphGroupDrawn = new BitSet();
536 int charOffset = 0; // offset for a label
539 int yfrom = 0, f_i = 0, yto = 0, f_to = 0;
540 boolean clipst = false, clipend = false;
541 for (int i = 0; i < aa.length; i++)
543 AlignmentAnnotation row = aa[i];
544 boolean renderHistogram = true;
545 boolean renderProfile = false;
546 boolean normaliseProfile = false;
547 boolean isRNA = row.isRNA();
549 // check if this is a consensus annotation row and set the display
550 // settings appropriately
551 // TODO: generalise this to have render styles for consensus/profile
553 if (row.groupRef != null && row == row.groupRef.getConsensus())
555 renderHistogram = row.groupRef.isShowConsensusHistogram();
556 renderProfile = row.groupRef.isShowSequenceLogo();
557 normaliseProfile = row.groupRef.isNormaliseSequenceLogo();
559 else if (row == consensusAnnot || row == structConsensusAnnot
560 || row == complementConsensusAnnot || row == ssConsensusAnnot)
562 renderHistogram = av_renderHistogram;
563 renderProfile = av_renderProfile;
564 normaliseProfile = av_normaliseProfile;
567 Annotation[] row_annotations = row.annotations;
572 // centreColLabels = row.centreColLabels || centreColLabelsDef;
573 labelAllCols = row.showAllColLabels;
574 scaleColLabel = row.scaleColLabel;
578 if (!useClip || ((y - charHeight) < visHeight
579 && (y + row.height + charHeight * 2) >= sOffset))
580 {// if_in_visible_region
591 if (row.graphGroup > -1 && graphGroupDrawn.get(row.graphGroup))
596 // this is so that we draw the characters below the graph
601 iconOffset = charHeight - fm.getDescent();
605 else if (row.hasText)
607 iconOffset = charHeight - fm.getDescent();
615 if (row.autoCalculated && av.isCalculationInProgress(row))
619 g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0,
620 y - row.height, imgWidth, y, annotationPanel);
621 g.setColor(Color.black);
622 // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
628 * else if (annotationPanel.av.updatingConservation &&
629 * aa[i].label.equals("Conservation")) {
631 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
632 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
633 * annotationPanel.imgWidth, y, annotationPanel);
635 * g.setColor(Color.black); //
636 * g.drawString("Calculating Conservation.....",20, y-row.height/2);
638 * continue; } else if (annotationPanel.av.updatingConservation &&
639 * aa[i].label.equals("Quality")) {
641 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
642 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
643 * annotationPanel.imgWidth, y, annotationPanel);
644 * g.setColor(Color.black); // /
645 * g.drawString("Calculating Quality....",20, y-row.height/2);
650 // first pass sets up state for drawing continuation from left-hand
654 // flag used for vector rendition
655 this.glyphLineDrawn = false;
656 x = (startRes == 0) ? 0 : -1;
657 while (x < endRes - startRes)
659 if (hasHiddenColumns)
661 column = hiddenColumns.visibleToAbsoluteColumn(startRes + x);
662 if (column > row_annotations.length - 1)
669 column = startRes + x;
672 if ((row_annotations == null)
673 || (row_annotations.length <= column)
674 || (row_annotations[column] == null))
682 final String displayChar = validRes
683 ? row_annotations[column].displayCharacter
690 g.setColor(Color.red);
692 if (columnSelection != null)
694 if (columnSelection.contains(column))
696 fillRect(g, x * charWidth, y, charWidth, charHeight);
700 if (row.getInvalidStrucPos() > x)
702 g.setColor(Color.orange);
703 fillRect(g, x * charWidth, y, charWidth, charHeight);
705 else if (row.getInvalidStrucPos() == x)
707 g.setColor(Color.orange.darker());
708 fillRect(g, x * charWidth, y, charWidth, charHeight);
710 if (validCharWidth && validRes && displayChar != null
711 && (displayChar.length() > 0))
713 // Graphics2D gg = (g);
714 float fmWidth = fm.charsWidth(displayChar.toCharArray(), 0,
715 displayChar.length());
718 * shrink label width to fit in column, if that is
719 * both configured and necessary
721 boolean scaledToFit = false;
722 float fmScaling = 1f;
723 if (scaleColLabel && fmWidth > charWidth)
726 fmScaling = charWidth;
727 fmScaling /= fmWidth;
728 // and update the label's width to reflect the scaling.
732 charOffset = (int) ((charWidth - fmWidth) / 2f);
734 if (row_annotations[column].colour == null)
736 g2d.setColor(Color.black);
740 g2d.setColor(row_annotations[column].colour);
744 * draw the label, unless it is the same secondary structure
745 * symbol (excluding RNA Helix) as the previous column
747 final int xPos = (x * charWidth) + charOffset;
748 final int yPos = y + iconOffset;
751 * translate to drawing position _before_ applying any scaling
753 g2d.translate(xPos, yPos);
757 * use a scaling transform to make the label narrower
758 * (JalviewJS doesn't have Font.deriveFont(AffineTransform))
761 AffineTransform.getScaleInstance(fmScaling, 1.0));
764 if (column == 0 || row.graph > 0)
766 g2d.drawString(displayChar, 0, 0);
768 else if (row_annotations[column - 1] == null || (labelAllCols
769 || !displayChar.equals(
770 row_annotations[column - 1].displayCharacter)
771 || (displayChar.length() < 2
772 && row_annotations[column].secondaryStructure == ' ')))
774 g2d.drawString(displayChar, 0, 0);
779 * undo scaling before translating back
780 * (restoring saved transform does NOT work in JS PDFGraphics!)
782 g2d.transform(AffineTransform
783 .getScaleInstance(1D / fmScaling, 1.0));
785 g2d.translate(-xPos, -yPos);
790 char ss = validRes ? row_annotations[column].secondaryStructure
795 // distinguish between forward/backward base-pairing
796 if (displayChar.indexOf(')') > -1)
805 if ((displayChar.indexOf(']') > -1))
813 // distinguish between forward/backward base-pairing
814 if (displayChar.indexOf('}') > -1)
822 // distinguish between forward/backward base-pairing
823 if (displayChar.indexOf('<') > -1)
829 if (isRNA && (ss >= CHAR_A) && (ss <= CHAR_Z))
831 // distinguish between forward/backward base-pairing
832 int ssLowerCase = ss + UPPER_TO_LOWER;
833 // TODO would .equals() be safer here? or charAt(0)?
834 if (displayChar.indexOf(ssLowerCase) > -1)
836 ss = (char) ssLowerCase;
840 if (!validRes || (ss != lastSS))
846 // int nb_annot = x - temp;
847 // Console.info("\t type :"+lastSS+"\t x
849 // annot :"+nb_annot);
852 case '(': // Stem case for RNA secondary structure
853 case ')': // and opposite direction
854 drawStemAnnot(g, row_annotations, lastSSX, x, y,
855 iconOffset, startRes, column, validRes, validEnd);
862 drawHelixAnnot(g, row_annotations, lastSSX, x, y,
863 iconOffset, startRes, column, validRes,
867 // no break if isRNA - falls through to drawNotCanonicalAnnot!
871 drawSheetAnnot(g, row_annotations, lastSSX, x, y,
872 iconOffset, startRes, column, validRes,
876 // no break if isRNA - fall through to drawNotCanonicalAnnot!
935 Color nonCanColor = getNotCanonicalColor(lastSS);
936 drawNotCanonicalAnnot(g, nonCanColor, row_annotations,
937 lastSSX, x, y, iconOffset, startRes, column,
942 if (isVectorRendering())
944 // draw single full width glyphline
945 drawGlyphLine(g, lastSSX, endRes - x, y, iconOffset);
946 // disable more glyph lines
947 this.glyphLineDrawn = true;
951 drawGlyphLine(g, lastSSX, x, y, iconOffset);
966 lastSSX = (x * charWidth);
973 if (column >= row_annotations.length)
975 column = row_annotations.length - 1;
982 if ((row_annotations == null) || (row_annotations.length <= column)
983 || (row_annotations[column] == null))
1001 drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
1002 startRes, column, validRes, validEnd);
1005 // no break if isRNA - fall through to drawNotCanonicalAnnot!
1010 drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
1011 startRes, column, validRes, validEnd);
1014 // no break if isRNA - fall through to drawNotCanonicalAnnot!
1017 case ')': // Stem case for RNA secondary structure
1019 drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
1020 startRes, column, validRes, validEnd);
1077 // Console.info(lastSS);
1078 Color nonCanColor = getNotCanonicalColor(lastSS);
1079 drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX,
1080 x, y, iconOffset, startRes, column, validRes, validEnd);
1083 if (isVectorRendering())
1085 // draw single full width glyphline
1086 drawGlyphLine(g, lastSSX, endRes - x, y, iconOffset);
1087 // disable more glyph lines
1088 this.glyphLineDrawn = true;
1092 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1098 if (row.graph > 0 && row.graphHeight > 0)
1100 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1102 if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
1104 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1105 // computed efficiently for all visible labels
1106 float groupmax = -999999, groupmin = 9999999;
1107 for (int gg = 0; gg < aa.length; gg++)
1109 if (aa[gg].graphGroup != row.graphGroup)
1116 aa[gg].visible = false;
1118 if (aa[gg].graphMax > groupmax)
1120 groupmax = aa[gg].graphMax;
1122 if (aa[gg].graphMin < groupmin)
1124 groupmin = aa[gg].graphMin;
1128 for (int gg = 0; gg < aa.length; gg++)
1130 if (aa[gg].graphGroup == row.graphGroup)
1132 drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
1133 endRes, y, groupmin, groupmax, row.graphHeight);
1137 graphGroupDrawn.set(row.graphGroup);
1141 drawLineGraph(g, row, row_annotations, startRes, endRes, y,
1142 row.graphMin, row.graphMax, row.graphHeight);
1145 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1147 drawBarGraph(g, row, row_annotations, startRes, endRes,
1148 row.graphMin, row.graphMax, y, renderHistogram,
1149 renderProfile, normaliseProfile);
1153 AnnotationRowRendererI renderer = rendererFactoryI
1154 .getRendererFor(row);
1155 if (renderer != null)
1157 renderer.renderRow(g, charWidth, charHeight, hasHiddenColumns,
1158 av, hiddenColumns, columnSelection, row,
1159 row_annotations, startRes, endRes, row.graphMin,
1164 if (renderer == null)
1167 .println("No renderer found for " + row.toString());
1172 "rendered with " + renderer.getClass().toString());
1181 if (clipst && !clipend)
1185 } // end if_in_visible_region
1186 if (row.graph > 0 && row.hasText)
1202 Console.warn("Start clip at : " + yfrom + " (index " + f_i + ")");
1206 Console.warn("End clip at : " + yto + " (index " + f_to + ")");
1210 Console.warn("Annotation Rendering time:"
1211 + (System.currentTimeMillis() - stime));
1218 public static final Color GLYPHLINE_COLOR = Color.gray;
1220 public static final Color SHEET_COLOUR = Color.green;
1222 public static final Color HELIX_COLOUR = Color.red;
1224 public static final Color STEM_COLOUR = Color.blue;
1226 // private Color sdNOTCANONICAL_COLOUR;
1228 void drawGlyphLine(Graphics g, int lastSSX, int x, int y, int iconOffset)
1232 // if we've drawn a single long glyphline for an export, don't draw the
1237 g.setColor(GLYPHLINE_COLOR);
1238 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
1241 void drawSheetAnnot(Graphics g, Annotation[] row,
1243 int lastSSX, int x, int y, int iconOffset, int startRes,
1244 int column, boolean validRes, boolean validEnd)
1246 if (!validEnd || !validRes || row == null || row[column] == null
1247 || row[column].secondaryStructure != 'E')
1249 // draw the glyphline underneath
1250 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1252 g.setColor(SHEET_COLOUR);
1253 fillRect(g, lastSSX, y + 4 + iconOffset,
1254 (x * charWidth) - lastSSX - 4, 6);
1257 { (x * charWidth) - 6, (x * charWidth) - 6,
1258 (x * charWidth - 1) },
1260 { y + iconOffset + 1, y + 13 + iconOffset,
1261 y + 7 + iconOffset },
1266 g.setColor(SHEET_COLOUR);
1267 fillRect(g, lastSSX, y + 4 + iconOffset, (x * charWidth) - lastSSX,
1272 void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x,
1273 int y, int iconOffset, int startRes, int column, boolean validRes,
1276 int sCol = (lastSSX / charWidth)
1277 + hiddenColumns.visibleToAbsoluteColumn(startRes);
1279 int x2 = (x * charWidth);
1281 if (USE_FILL_ROUND_RECT || isVectorRendering())
1283 // draw glyph line behind helix (visible in EPS or SVG output)
1284 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1286 g.setColor(HELIX_COLOUR);
1288 int ofs = charWidth / 2;
1289 // Off by 1 offset when drawing rects and ovals
1290 // to offscreen image on the MAC
1291 fillRoundRect(g, lastSSX, y + 3 + iconOffset, x2 - x1 - 1, 8, 8, 8);
1292 if (sCol == 0 || row[sCol - 1] == null
1293 || row[sCol - 1].secondaryStructure != 'H')
1298 fillRoundRect(g, lastSSX, y + 3 + iconOffset, x2 - x1 - ofs, 8, 0,
1301 if (!validRes || row[column] == null
1302 || row[column].secondaryStructure != 'H')
1308 fillRoundRect(g, lastSSX + ofs, y + 3 + iconOffset, x2 - x1 - ofs,
1315 boolean leftEnd = sCol == 0 || row[sCol - 1] == null
1316 || row[sCol - 1].secondaryStructure != 'H';
1317 boolean rightEnd = !validRes || row[column] == null
1318 || row[column].secondaryStructure != 'H';
1320 if (leftEnd || rightEnd)
1322 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1324 g.setColor(HELIX_COLOUR);
1328 fillArc(g, lastSSX, y + 3 + iconOffset, charWidth, 8, 90, 180);
1329 x1 += charWidth / 2;
1334 fillArc(g, ((x - 1) * charWidth), y + 3 + iconOffset, charWidth, 8,
1336 x2 -= charWidth / 2;
1339 fillRect(g, x1, y + 3 + iconOffset, x2 - x1, 8);
1342 void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1343 Annotation[] aa_annotations, int sRes, int eRes, int y, float min,
1344 float max, int graphHeight)
1346 if (sRes > aa_annotations.length)
1350 Stroke roundStroke = new BasicStroke(1, BasicStroke.CAP_ROUND,
1351 BasicStroke.JOIN_ROUND);
1352 Stroke squareStroke = new BasicStroke(1, BasicStroke.CAP_SQUARE,
1353 BasicStroke.JOIN_MITER);
1354 Graphics2D g2d = (Graphics2D) g;
1355 Stroke prevStroke = g2d.getStroke();
1356 g2d.setStroke(roundStroke);
1360 // Adjustment for fastpaint to left
1366 eRes = Math.min(eRes, aa_annotations.length);
1374 float range = max - min;
1379 y2 = y - (int) ((0 - min / range) * graphHeight);
1382 g.setColor(Color.gray);
1383 drawLine(g, squareStroke, x * charWidth - charWidth, y2,
1384 (eRes - sRes) * charWidth, y2);
1386 eRes = Math.min(eRes, aa_annotations.length);
1389 int aaMax = aa_annotations.length - 1;
1391 while (x < eRes - sRes)
1394 if (hasHiddenColumns)
1396 column = hiddenColumns.visibleToAbsoluteColumn(column);
1404 if (aa_annotations[column] == null)
1410 if (aa_annotations[column].colour == null)
1412 g.setColor(Color.black);
1416 g.setColor(aa_annotations[column].colour);
1419 if (aa_annotations[column - 1] == null
1420 && aa_annotations.length > column + 1
1421 && aa_annotations[column + 1] == null)
1424 y1 = y - (int) (((aa_annotations[column].value - min) / range)
1426 drawLine(g, x * charWidth + charWidth / 4, y1,
1427 x * charWidth + 3 * charWidth / 4, y1);
1432 if (aa_annotations[column - 1] == null)
1438 y1 = y - (int) (((aa_annotations[column - 1].value - min) / range)
1440 y2 = y - (int) (((aa_annotations[column].value - min) / range)
1443 drawLine(g, (x - 1) * charWidth + charWidth / 2, y1,
1444 x * charWidth + charWidth / 2, y2);
1448 if (_aa.threshold != null)
1450 g.setColor(_aa.threshold.colour);
1451 Graphics2D g2 = (Graphics2D) g;
1452 Stroke s = new BasicStroke(1, BasicStroke.CAP_SQUARE,
1453 BasicStroke.JOIN_ROUND, 3f, new float[]
1456 y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1457 drawLine(g, s, 0, y2, (eRes - sRes) * charWidth, y2);
1459 g2d.setStroke(prevStroke);
1462 @SuppressWarnings("unused")
1463 void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1464 Annotation[] aa_annotations, int sRes, int eRes, float min,
1465 float max, int y, boolean renderHistogram, boolean renderProfile,
1466 boolean normaliseProfile)
1468 if (sRes > aa_annotations.length)
1472 Font ofont = g.getFont();
1473 eRes = Math.min(eRes, aa_annotations.length);
1475 int x = 0, y1 = y, y2 = y;
1477 float range = max - min;
1481 y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1484 g.setColor(Color.gray);
1486 drawLine(g, x, y2, (eRes - sRes) * charWidth, y2);
1489 int aaMax = aa_annotations.length - 1;
1490 while (x < eRes - sRes)
1493 if (hasHiddenColumns)
1495 column = hiddenColumns.visibleToAbsoluteColumn(column);
1503 if (aa_annotations[column] == null)
1508 if (aa_annotations[column].colour == null)
1510 g.setColor(Color.black);
1514 g.setColor(aa_annotations[column].colour);
1517 y1 = y - (int) (((aa_annotations[column].value - min) / (range))
1520 if (renderHistogram)
1524 fillRect(g, x * charWidth, y2, charWidth, y1 - y2);
1528 fillRect(g, x * charWidth, y1, charWidth, y2 - y1);
1531 // draw profile if available
1536 * {profile type, #values, total count, char1, pct1, char2, pct2...}
1538 int profl[] = getProfileFor(_aa, column);
1542 // just try to draw the logo if profl is not null
1543 if (profl != null && profl[2] != 0)
1545 boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
1546 boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
1547 float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1548 final double normaliseFactor = normaliseProfile ? _aa.graphHeight
1552 * Render a single base for a sequence profile, a base pair for
1553 * structure profile, and a triplet for a cdna profile
1555 char[] dc = new char[isStructureProfile ? 2
1556 : (isCdnaProfile ? 3 : 1)];
1558 // lm is not necessary - we can just use fm - could be off by no more
1560 // LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1561 // Console.info(asc + " " + dec + " " + (asc -
1563 // + " " + (dec - lm.getDescent()));
1565 double asc = fm.getAscent();
1566 double dec = fm.getDescent();
1567 double fht = fm.getHeight();
1569 double scale = 1f / (normaliseProfile ? profl[2] : 100f);
1570 // float ofontHeight = 1f / fm.getAscent();// magnify to fill box
1573 * Traverse the character(s)/percentage data in the array
1578 // profl[1] is the number of values in the profile
1579 for (int i = 0, c = 3, last = profl[1]; i < last; i++)
1583 if (isStructureProfile)
1585 // todo can we encode a structure pair as an int, like codons?
1586 dc[0] = (char) profl[c++];
1587 dc[1] = (char) profl[c++];
1590 else if (isCdnaProfile)
1592 CodingUtils.decodeCodon2(profl[c++], dc);
1597 dc[0] = (char) profl[c++];
1600 // next profl[] position is profile % for the character(s)
1602 int percent = profl[c++];
1605 // failsafe in case a count rounds down to 0%
1608 double newHeight = normaliseFactor * scale * percent;
1611 * Set character colour as per alignment colour scheme; use the
1612 * codon translation if a cDNA profile
1614 Color colour = null;
1617 final String codonTranslation = ResidueProperties
1619 colour = profcolour.findColour(codonTranslation.charAt(0),
1622 if(_aa.label == MessageManager.getString("label.ssconsensus_label")) {
1623 colour = AlignmentUtils.getSecondaryStructureAnnotationColour(dc[0]);
1628 colour = profcolour.findColour(dc[0], column, null);
1631 g.setColor(colour == Color.white ? Color.lightGray : colour);
1633 // Debug - render boxes around characters
1634 // g.setColor(Color.red);
1635 // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
1637 // g.setColor(profcolour.findColour(dc[0]).darker());
1639 double sx = 1f * charWidth / fm.charsWidth(dc, 0, dc.length);
1640 double sy = newHeight / asc;
1641 double newAsc = asc * sy;
1642 double newDec = dec * sy;
1643 // it is not necessary to recalculate lm for the new font.
1644 // note: lm.getBaselineOffsets()[lm.getBaselineIndex()]) must be 0
1645 // by definition. Was:
1646 // int hght = (int) (ht + (newAsc - newDec);
1647 // - lm.getBaselineOffsets()[lm.getBaselineIndex()]));
1649 if (Platform.isJS())
1652 * SwingJS does not implement font.deriveFont()
1653 * so use a scaling transform to draw instead,
1654 * this is off by a very small amount
1656 final int hght = (int) (ht2 + (newAsc - newDec));
1657 Graphics2D gg = (Graphics2D) g;
1658 int xShift = (int) Math.round(x * charWidth / sx);
1659 int yShift = (int) Math.round(hght / sy);
1660 gg.transform(AffineTransform.getScaleInstance(sx, sy));
1661 gg.drawString(s, xShift, yShift);
1663 AffineTransform.getScaleInstance(1D / sx, 1D / sy));
1673 // Java ('normal') method is to scale the font to fit
1675 final int hght = (int) (ht + (newAsc - newDec));
1677 .deriveFont(AffineTransform.getScaleInstance(sx, sy));
1679 g.drawChars(dc, 0, dc.length, x * charWidth, hght);
1689 if (_aa.threshold != null)
1691 g.setColor(_aa.threshold.colour);
1692 Stroke s = new BasicStroke(1, BasicStroke.CAP_SQUARE,
1693 BasicStroke.JOIN_ROUND, 3f, new float[]
1697 - ((_aa.threshold.value - min) / range) * _aa.graphHeight);
1698 drawLine(g, s, 0, y2, (eRes - sRes) * charWidth, y2);
1702 // used by overview window
1703 public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1704 Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1706 eRes = Math.min(eRes, aa_annotations.length);
1707 g.setColor(Color.white);
1708 fillRect(g, 0, 0, width, y);
1709 g.setColor(new Color(0, 0, 180));
1713 for (int j = sRes; j < eRes; j++)
1715 if (aa_annotations[j] != null)
1717 if (aa_annotations[j].colour == null)
1719 g.setColor(Color.black);
1723 g.setColor(aa_annotations[j].colour);
1726 height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1732 fillRect(g, x, y - height, charWidth, height);
1738 Color getNotCanonicalColor(char lastss)
1744 return new Color(255, 125, 5);
1748 return new Color(245, 115, 10);
1752 return new Color(235, 135, 15);
1756 return new Color(225, 105, 20);
1760 return new Color(215, 145, 30);
1764 return new Color(205, 95, 35);
1768 return new Color(195, 155, 45);
1772 return new Color(185, 85, 55);
1776 return new Color(175, 165, 65);
1780 return new Color(170, 75, 75);
1784 return new Color(160, 175, 85);
1788 return new Color(150, 65, 95);
1792 return new Color(140, 185, 105);
1796 return new Color(130, 55, 110);
1800 return new Color(120, 195, 120);
1804 return new Color(110, 45, 130);
1808 return new Color(100, 205, 140);
1812 return new Color(90, 35, 150);
1816 return new Color(85, 215, 160);
1820 return new Color(75, 25, 170);
1824 return new Color(65, 225, 180);
1828 return new Color(55, 15, 185);
1832 return new Color(45, 235, 195);
1836 return new Color(35, 5, 205);
1840 return new Color(25, 245, 215);
1844 return new Color(15, 0, 225);
1848 return new Color(10, 255, 235);
1852 return new Color(5, 150, 245);
1856 return new Color(0, 80, 255);
1859 Console.info("This is not a interaction : " + lastss);
1865 private void fillPolygon(Graphics g, int[] xpoints, int[] ypoints, int n)
1868 g.fillPolygon(xpoints, ypoints, n);
1872 private void fillRect(Graphics g, int a, int b, int c, int d)
1874 fillRect(g, false, a, b, c, d);
1877 private void fillRect(Graphics g, int a, int b, int c, int d)
1880 g.fillRect(a, b, c, d);
1883 private void fillRoundRect(Graphics g, int a, int b, int c, int d, int e,
1887 g.fillRoundRect(a, b, c, d, e, f);
1890 private void fillArc(Graphics g, int a, int b, int c, int d, int e, int f)
1893 g.fillArc(a, b, c, d, e, f);
1896 private void drawLine(Graphics g, Stroke s, int a, int b, int c, int d)
1898 Graphics2D g2d = (Graphics2D) g;
1899 Stroke p = g2d.getStroke();
1901 drawLine(g, a, b, c, d);
1905 private void drawLine(Graphics g, int a, int b, int c, int d)
1908 g.drawLine(a, b, c, d);
1911 private void setAntialias(Graphics g)
1913 if (isVectorRendering())
1915 // no need to antialias vector drawings
1918 if (Cache.getDefault("ANTI_ALIAS", true))
1920 Graphics2D g2d = (Graphics2D) g;
1921 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1922 RenderingHints.VALUE_ANTIALIAS_ON);
1926 private void unsetAntialias(Graphics g)
1928 if (isVectorRendering())
1930 // no need to antialias vector drawings
1933 Graphics2D g2d = (Graphics2D) g;
1934 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1935 RenderingHints.VALUE_ANTIALIAS_OFF);
1938 public void setVectorRendering(boolean b)
1940 renderingVectors = b;
1943 public boolean isVectorRendering()
1945 return renderingVectors;