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.CodingUtils;
42 import jalview.analysis.Rna;
43 import jalview.analysis.StructureFrequency;
44 import jalview.api.AlignViewportI;
45 import jalview.bin.Cache;
46 import jalview.bin.Console;
47 import jalview.datamodel.AlignmentAnnotation;
48 import jalview.datamodel.Annotation;
49 import jalview.datamodel.ColumnSelection;
50 import jalview.datamodel.HiddenColumns;
51 import jalview.datamodel.ProfilesI;
52 import jalview.renderer.api.AnnotationRendererFactoryI;
53 import jalview.renderer.api.AnnotationRowRendererI;
54 import jalview.schemes.ColourSchemeI;
55 import jalview.schemes.NucleotideColourScheme;
56 import jalview.schemes.ResidueProperties;
57 import jalview.schemes.ZappoColourScheme;
58 import jalview.util.Platform;
60 public class AnnotationRenderer
62 private static final int UPPER_TO_LOWER = 'a' - 'A'; // 32
64 private static final int CHAR_A = 'A'; // 65
66 private static final int CHAR_Z = 'Z'; // 90
69 * flag indicating if timing and redraw parameter info should be output
71 private final boolean debugRedraw;
73 private int charWidth, endRes, charHeight;
75 private boolean validCharWidth, hasHiddenColumns;
77 private FontMetrics fm;
79 private final boolean USE_FILL_ROUND_RECT = Platform.isAMacAndNotJS();
81 boolean av_renderHistogram = true, av_renderProfile = true,
82 av_normaliseProfile = false;
84 ResidueShaderI profcolour = null;
86 private ColumnSelection columnSelection;
88 private HiddenColumns hiddenColumns;
90 private ProfilesI hconsensus;
92 private Hashtable<String, Object>[] complementConsensus;
94 private Hashtable<String, Object>[] hStrucConsensus;
96 private boolean av_ignoreGapsConsensus;
98 private boolean vectorRendition = false;
100 private boolean glyphLineDrawn = false;
103 * attributes set from AwtRenderPanelI
106 * old image used when data is currently being calculated and cannot be
109 private Image fadedImage;
112 * panel being rendered into
114 private ImageObserver annotationPanel;
117 * width of image to render in panel
119 private int imgWidth;
122 * offset to beginning of visible area
127 * offset to end of visible area
129 private int visHeight;
132 * indicate if the renderer should only render the visible portion of the
133 * annotation given the current view settings
135 private boolean useClip = true;
138 * master flag indicating if renderer should ever try to clip. not enabled for
141 private boolean canClip = false;
143 public AnnotationRenderer()
149 * Create a new annotation Renderer
152 * flag indicating if timing and redraw parameter info should be
155 public AnnotationRenderer(boolean debugRedraw)
157 this.debugRedraw = debugRedraw;
161 * Remove any references and resources when this object is no longer required
163 public void dispose()
165 hiddenColumns = null;
167 complementConsensus = null;
168 hStrucConsensus = null;
170 annotationPanel = null;
171 rendererFactoryI = null;
174 void drawStemAnnot(Graphics g, Annotation[] row_annotations, int lastSSX,
175 int x, int y, int iconOffset, int startRes, int column,
176 boolean validRes, boolean validEnd)
178 g.setColor(STEM_COLOUR);
179 int sCol = (lastSSX / charWidth)
180 + hiddenColumns.visibleToAbsoluteColumn(startRes);
182 int x2 = (x * charWidth);
184 char dc = (column == 0 || row_annotations[column - 1] == null) ? ' '
185 : row_annotations[column - 1].secondaryStructure;
187 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
188 || dc != row_annotations[sCol - 1].secondaryStructure;
189 boolean diffdownstream = !validRes || !validEnd
190 || row_annotations[column] == null
191 || dc != row_annotations[column].secondaryStructure;
193 if (column > 0 && Rna.isClosingParenthesis(dc))
196 // if (validRes && column>1 && row_annotations[column-2]!=null &&
197 // dc.equals(row_annotations[column-2].displayCharacter))
200 * if new annotation with a closing base pair half of the stem,
201 * display a backward arrow
203 fillPolygon(g, new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
205 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
216 // display a forward arrow
220 * if annotation ending with an opeing base pair half of the stem,
221 * display a forward arrow
223 fillPolygon(g, new int[] { x2 - 5, x2 - 5, x2 },
225 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
235 fillRect(g, x1, y + 4 + iconOffset, x2 - x1, 7);
238 void drawNotCanonicalAnnot(Graphics g, Color nonCanColor,
239 Annotation[] row_annotations, int lastSSX, int x, int y,
240 int iconOffset, int startRes, int column, boolean validRes,
243 // Console.info(nonCanColor);
245 int sCol = (lastSSX / charWidth)
246 + hiddenColumns.visibleToAbsoluteColumn(startRes);
248 int x2 = (x * charWidth);
250 String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
251 : row_annotations[column - 1].displayCharacter;
253 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
254 || !dc.equals(row_annotations[sCol - 1].displayCharacter);
255 boolean diffdownstream = !validRes || !validEnd
256 || row_annotations[column] == null
257 || !dc.equals(row_annotations[column].displayCharacter);
258 // Console.info("Column "+column+" diff up:
260 // down:"+diffdownstream);
261 // If a closing base pair half of the stem, display a backward arrow
262 if (diffupstream || diffdownstream)
264 // draw glyphline under arrow
265 drawGlyphLine(g, lastSSX, x, y, iconOffset);
267 g.setColor(nonCanColor);
268 if (column > 0 && Rna.isClosingParenthesis(dc))
272 // if (validRes && column>1 && row_annotations[column-2]!=null &&
273 // dc.equals(row_annotations[column-2].displayCharacter))
275 fillPolygon(g, new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
277 { y + iconOffset + 1, y + 13 + iconOffset,
278 y + 7 + iconOffset },
290 // display a forward arrow
293 fillPolygon(g, new int[] { x2 - 6, x2 - 6, x2 - 1 },
295 { y + iconOffset + 1, y + 13 + iconOffset,
296 y + 7 + iconOffset },
307 fillRect(g, x1 - 1, y + 4 + iconOffset, x2 - x1 + 1, 6);
310 // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
312 public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
315 fm = annotPanel.getFontMetrics();
316 annotationPanel = annotPanel;
317 fadedImage = annotPanel.getFadedImage();
318 imgWidth = annotPanel.getFadedImageWidth();
319 // visible area for rendering
320 int[] bounds = annotPanel.getVisibleVRange();
324 visHeight = bounds[1];
339 rendererFactoryI = AnnotationRendererFactory.getRendererFactory();
340 updateFromAlignViewport(av);
343 public void updateFromAlignViewport(AlignViewportI av)
345 charWidth = av.getCharWidth();
346 endRes = av.getRanges().getEndRes();
347 charHeight = av.getCharHeight();
348 hasHiddenColumns = av.hasHiddenColumns();
349 validCharWidth = av.isValidCharWidth();
350 av_renderHistogram = av.isShowConsensusHistogram();
351 av_renderProfile = av.isShowSequenceLogo();
352 av_normaliseProfile = av.isNormaliseSequenceLogo();
353 profcolour = av.getResidueShading();
354 if (profcolour == null || profcolour.getColourScheme() == null)
357 * Use default colour for sequence logo if
358 * the alignment has no colourscheme set
359 * (would like to use user preference but n/a for applet)
361 ColourSchemeI col = av.getAlignment().isNucleotide()
362 ? new NucleotideColourScheme()
363 : new ZappoColourScheme();
364 profcolour = new ResidueShader(col);
366 columnSelection = av.getColumnSelection();
367 hiddenColumns = av.getAlignment().getHiddenColumns();
368 hconsensus = av.getSequenceConsensusHash();
369 complementConsensus = av.getComplementConsensusHash();
370 hStrucConsensus = av.getRnaStructureConsensusHash();
371 av_ignoreGapsConsensus = av.isIgnoreGapsConsensus();
375 * Returns profile data; the first element is the profile type, the second is
376 * the number of distinct values, the third the total count, and the remainder
377 * depend on the profile type.
383 int[] getProfileFor(AlignmentAnnotation aa, int column)
385 // TODO : consider refactoring the global alignment calculation
386 // properties/rendering attributes as a global 'alignment group' which holds
387 // all vis settings for the alignment as a whole rather than a subset
389 if (aa.autoCalculated && (aa.label.startsWith("Consensus")
390 || aa.label.startsWith("cDNA Consensus")))
392 boolean forComplement = aa.label.startsWith("cDNA Consensus");
393 if (aa.groupRef != null && aa.groupRef.consensusData != null
394 && aa.groupRef.isShowSequenceLogo())
396 // TODO? group consensus for cDNA complement
397 return AAFrequency.extractProfile(
398 aa.groupRef.consensusData.get(column),
399 aa.groupRef.getIgnoreGapsConsensus());
401 // TODO extend annotation row to enable dynamic and static profile data to
403 if (aa.groupRef == null && aa.sequenceRef == null)
407 return AAFrequency.extractCdnaProfile(complementConsensus[column],
408 av_ignoreGapsConsensus);
412 return AAFrequency.extractProfile(hconsensus.get(column),
413 av_ignoreGapsConsensus);
419 if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
421 // TODO implement group structure consensus
423 * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
424 * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
425 * group selections return StructureFrequency.extractProfile(
426 * aa.groupRef.consensusData[column], aa.groupRef
427 * .getIgnoreGapsConsensus()); }
429 // TODO extend annotation row to enable dynamic and static profile data
432 if (aa.groupRef == null && aa.sequenceRef == null
433 && hStrucConsensus != null
434 && hStrucConsensus.length > column)
436 return StructureFrequency.extractProfile(hStrucConsensus[column],
437 av_ignoreGapsConsensus);
446 private AnnotationRendererFactoryI rendererFactoryI;
449 * Render the annotation rows associated with an alignment.
454 * data and view settings to render
456 * destination for graphics
458 * row where a mouse event occured (or -1)
460 * first column that will be drawn
462 * last column that will be drawn
463 * @return true if the fadedImage was used for any alignment annotation rows
464 * currently being calculated
466 public boolean drawComponent(AwtRenderPanelI annotPanel,
467 AlignViewportI av, Graphics g, int activeRow, int startRes,
470 if (g instanceof EpsGraphics2D || g instanceof SVGGraphics2D)
472 this.setVectorRendition(true);
474 Graphics2D g2d = (Graphics2D) g;
475 // flag used for vector rendition
476 this.glyphLineDrawn = false;
478 long stime = System.currentTimeMillis();
479 boolean usedFaded = false;
481 // AnnotationPanel needs to implement: ImageObserver, access to
483 updateFromAwtRenderPanel(annotPanel, av);
484 fm = g.getFontMetrics();
485 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
496 boolean validRes = false;
497 boolean validEnd = false;
498 boolean labelAllCols = false;
499 // boolean centreColLabels;
500 // boolean centreColLabelsDef = av.isCentreColumnLabels();
501 boolean scaleColLabel = false;
502 final AlignmentAnnotation consensusAnnot = av
503 .getAlignmentConsensusAnnotation();
504 final AlignmentAnnotation structConsensusAnnot = av
505 .getAlignmentStrucConsensusAnnotation();
506 final AlignmentAnnotation complementConsensusAnnot = av
507 .getComplementConsensusAnnotation();
509 BitSet graphGroupDrawn = new BitSet();
510 int charOffset = 0; // offset for a label
513 int yfrom = 0, f_i = 0, yto = 0, f_to = 0;
514 boolean clipst = false, clipend = false;
515 for (int i = 0; i < aa.length; i++)
517 AlignmentAnnotation row = aa[i];
518 boolean renderHistogram = true;
519 boolean renderProfile = false;
520 boolean normaliseProfile = false;
521 boolean isRNA = row.isRNA();
523 // check if this is a consensus annotation row and set the display
524 // settings appropriately
525 // TODO: generalise this to have render styles for consensus/profile
527 if (row.groupRef != null && row == row.groupRef.getConsensus())
529 renderHistogram = row.groupRef.isShowConsensusHistogram();
530 renderProfile = row.groupRef.isShowSequenceLogo();
531 normaliseProfile = row.groupRef.isNormaliseSequenceLogo();
533 else if (row == consensusAnnot || row == structConsensusAnnot
534 || row == complementConsensusAnnot)
536 renderHistogram = av_renderHistogram;
537 renderProfile = av_renderProfile;
538 normaliseProfile = av_normaliseProfile;
541 Annotation[] row_annotations = row.annotations;
546 // centreColLabels = row.centreColLabels || centreColLabelsDef;
547 labelAllCols = row.showAllColLabels;
548 scaleColLabel = row.scaleColLabel;
552 if (!useClip || ((y - charHeight) < visHeight
553 && (y + row.height + charHeight * 2) >= sOffset))
554 {// if_in_visible_region
565 if (row.graphGroup > -1 && graphGroupDrawn.get(row.graphGroup))
570 // this is so that we draw the characters below the graph
575 iconOffset = charHeight - fm.getDescent();
579 else if (row.hasText)
581 iconOffset = charHeight - fm.getDescent();
589 if (row.autoCalculated && av.isCalculationInProgress(row))
593 g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0,
594 y - row.height, imgWidth, y, annotationPanel);
595 g.setColor(Color.black);
596 // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
602 * else if (annotationPanel.av.updatingConservation &&
603 * aa[i].label.equals("Conservation")) {
605 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
606 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
607 * annotationPanel.imgWidth, y, annotationPanel);
609 * g.setColor(Color.black); //
610 * g.drawString("Calculating Conservation.....",20, y-row.height/2);
612 * continue; } else if (annotationPanel.av.updatingConservation &&
613 * aa[i].label.equals("Quality")) {
615 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
616 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
617 * annotationPanel.imgWidth, y, annotationPanel);
618 * g.setColor(Color.black); // /
619 * g.drawString("Calculating Quality....",20, y-row.height/2);
624 // first pass sets up state for drawing continuation from left-hand
627 x = (startRes == 0) ? 0 : -1;
628 while (x < endRes - startRes)
630 if (hasHiddenColumns)
632 column = hiddenColumns.visibleToAbsoluteColumn(startRes + x);
633 if (column > row_annotations.length - 1)
640 column = startRes + x;
643 if ((row_annotations == null)
644 || (row_annotations.length <= column)
645 || (row_annotations[column] == null))
653 final String displayChar = validRes
654 ? row_annotations[column].displayCharacter
661 g.setColor(Color.red);
663 if (columnSelection != null)
665 if (columnSelection.contains(column))
667 fillRect(g, x * charWidth, y, charWidth, charHeight);
671 if (row.getInvalidStrucPos() > x)
673 g.setColor(Color.orange);
674 fillRect(g, x * charWidth, y, charWidth, charHeight);
676 else if (row.getInvalidStrucPos() == x)
678 g.setColor(Color.orange.darker());
679 fillRect(g, x * charWidth, y, charWidth, charHeight);
681 if (validCharWidth && validRes && displayChar != null
682 && (displayChar.length() > 0))
684 // Graphics2D gg = (g);
685 float fmWidth = fm.charsWidth(displayChar.toCharArray(), 0,
686 displayChar.length());
689 * shrink label width to fit in column, if that is
690 * both configured and necessary
692 boolean scaledToFit = false;
693 float fmScaling = 1f;
694 if (scaleColLabel && fmWidth > charWidth)
697 fmScaling = charWidth;
698 fmScaling /= fmWidth;
699 // and update the label's width to reflect the scaling.
703 charOffset = (int) ((charWidth - fmWidth) / 2f);
705 if (row_annotations[column].colour == null)
707 g2d.setColor(Color.black);
711 g2d.setColor(row_annotations[column].colour);
715 * draw the label, unless it is the same secondary structure
716 * symbol (excluding RNA Helix) as the previous column
718 final int xPos = (x * charWidth) + charOffset;
719 final int yPos = y + iconOffset;
722 * translate to drawing position _before_ applying any scaling
724 g2d.translate(xPos, yPos);
728 * use a scaling transform to make the label narrower
729 * (JalviewJS doesn't have Font.deriveFont(AffineTransform))
732 AffineTransform.getScaleInstance(fmScaling, 1.0));
735 if (column == 0 || row.graph > 0)
737 g2d.drawString(displayChar, 0, 0);
739 else if (row_annotations[column - 1] == null || (labelAllCols
740 || !displayChar.equals(
741 row_annotations[column - 1].displayCharacter)
742 || (displayChar.length() < 2
743 && row_annotations[column].secondaryStructure == ' ')))
745 g2d.drawString(displayChar, 0, 0);
750 * undo scaling before translating back
751 * (restoring saved transform does NOT work in JS PDFGraphics!)
753 g2d.transform(AffineTransform
754 .getScaleInstance(1D / fmScaling, 1.0));
756 g2d.translate(-xPos, -yPos);
761 char ss = validRes ? row_annotations[column].secondaryStructure
766 // distinguish between forward/backward base-pairing
767 if (displayChar.indexOf(')') > -1)
776 if ((displayChar.indexOf(']') > -1))
784 // distinguish between forward/backward base-pairing
785 if (displayChar.indexOf('}') > -1)
793 // distinguish between forward/backward base-pairing
794 if (displayChar.indexOf('<') > -1)
800 if (isRNA && (ss >= CHAR_A) && (ss <= CHAR_Z))
802 // distinguish between forward/backward base-pairing
803 int ssLowerCase = ss + UPPER_TO_LOWER;
804 // TODO would .equals() be safer here? or charAt(0)?
805 if (displayChar.indexOf(ssLowerCase) > -1)
807 ss = (char) ssLowerCase;
811 if (!validRes || (ss != lastSS))
817 // int nb_annot = x - temp;
818 // Console.info("\t type :"+lastSS+"\t x
820 // annot :"+nb_annot);
823 case '(': // Stem case for RNA secondary structure
824 case ')': // and opposite direction
825 drawStemAnnot(g, row_annotations, lastSSX, x, y,
826 iconOffset, startRes, column, validRes, validEnd);
833 drawHelixAnnot(g, row_annotations, lastSSX, x, y,
834 iconOffset, startRes, column, validRes,
838 // no break if isRNA - falls through to drawNotCanonicalAnnot!
842 drawSheetAnnot(g, row_annotations, lastSSX, x, y,
843 iconOffset, startRes, column, validRes,
847 // no break if isRNA - fall through to drawNotCanonicalAnnot!
906 Color nonCanColor = getNotCanonicalColor(lastSS);
907 drawNotCanonicalAnnot(g, nonCanColor, row_annotations,
908 lastSSX, x, y, iconOffset, startRes, column,
913 if (isVectorRendition())
915 // draw single full width glyphline
916 drawGlyphLine(g, lastSSX, endRes - x, y, iconOffset);
917 // disable more glyph lines
918 this.glyphLineDrawn = true;
922 drawGlyphLine(g, lastSSX, x, y, iconOffset);
937 lastSSX = (x * charWidth);
944 if (column >= row_annotations.length)
946 column = row_annotations.length - 1;
953 if ((row_annotations == null) || (row_annotations.length <= column)
954 || (row_annotations[column] == null))
972 drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
973 startRes, column, validRes, validEnd);
976 // no break if isRNA - fall through to drawNotCanonicalAnnot!
981 drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
982 startRes, column, validRes, validEnd);
985 // no break if isRNA - fall through to drawNotCanonicalAnnot!
988 case ')': // Stem case for RNA secondary structure
990 drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
991 startRes, column, validRes, validEnd);
1048 // Console.info(lastSS);
1049 Color nonCanColor = getNotCanonicalColor(lastSS);
1050 drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX,
1051 x, y, iconOffset, startRes, column, validRes, validEnd);
1054 if (isVectorRendition())
1056 // draw single full width glyphline
1057 drawGlyphLine(g, lastSSX, endRes - x, y, iconOffset);
1058 // disable more glyph lines
1059 this.glyphLineDrawn = true;
1063 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1069 if (row.graph > 0 && row.graphHeight > 0)
1071 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1073 if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
1075 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1076 // computed efficiently for all visible labels
1077 float groupmax = -999999, groupmin = 9999999;
1078 for (int gg = 0; gg < aa.length; gg++)
1080 if (aa[gg].graphGroup != row.graphGroup)
1087 aa[gg].visible = false;
1089 if (aa[gg].graphMax > groupmax)
1091 groupmax = aa[gg].graphMax;
1093 if (aa[gg].graphMin < groupmin)
1095 groupmin = aa[gg].graphMin;
1099 for (int gg = 0; gg < aa.length; gg++)
1101 if (aa[gg].graphGroup == row.graphGroup)
1103 drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
1104 endRes, y, groupmin, groupmax, row.graphHeight);
1108 graphGroupDrawn.set(row.graphGroup);
1112 drawLineGraph(g, row, row_annotations, startRes, endRes, y,
1113 row.graphMin, row.graphMax, row.graphHeight);
1116 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1118 drawBarGraph(g, row, row_annotations, startRes, endRes,
1119 row.graphMin, row.graphMax, y, renderHistogram,
1120 renderProfile, normaliseProfile);
1124 AnnotationRowRendererI renderer = rendererFactoryI
1125 .getRendererFor(row);
1126 if (renderer != null)
1128 renderer.renderRow(g, charWidth, charHeight, hasHiddenColumns,
1129 av, hiddenColumns, columnSelection, row,
1130 row_annotations, startRes, endRes, row.graphMin,
1135 if (renderer == null)
1138 .println("No renderer found for " + row.toString());
1143 "rendered with " + renderer.getClass().toString());
1152 if (clipst && !clipend)
1156 } // end if_in_visible_region
1157 if (row.graph > 0 && row.hasText)
1173 Console.warn("Start clip at : " + yfrom + " (index " + f_i + ")");
1177 Console.warn("End clip at : " + yto + " (index " + f_to + ")");
1181 Console.warn("Annotation Rendering time:"
1182 + (System.currentTimeMillis() - stime));
1189 public static final Color GLYPHLINE_COLOR = Color.gray;
1191 public static final Color SHEET_COLOUR = Color.green;
1193 public static final Color HELIX_COLOUR = Color.red;
1195 public static final Color STEM_COLOUR = Color.blue;
1197 // private Color sdNOTCANONICAL_COLOUR;
1199 void drawGlyphLine(Graphics g, int lastSSX, int x, int y, int iconOffset)
1203 // if we've drawn a single long glyphline for an export, don't draw the
1208 g.setColor(GLYPHLINE_COLOR);
1209 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX - 1,
1211 if (!isVectorRendition())
1213 g.drawRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX - 1,
1218 void drawSheetAnnot(Graphics g, Annotation[] row,
1220 int lastSSX, int x, int y, int iconOffset, int startRes,
1221 int column, boolean validRes, boolean validEnd)
1223 if (!validEnd || !validRes || row == null || row[column] == null
1224 || row[column].secondaryStructure != 'E')
1226 // draw the glyphline underneath
1227 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1229 g.setColor(SHEET_COLOUR);
1230 fillRect(g, lastSSX, y + 4 + iconOffset,
1231 (x * charWidth) - lastSSX - 4, isVectorRendition() ? 6 : 7);
1234 { (x * charWidth) - 6, (x * charWidth) - 6,
1235 (x * charWidth - 1) },
1237 { y + iconOffset + 1, y + 13 + iconOffset,
1238 y + 7 + iconOffset },
1243 g.setColor(SHEET_COLOUR);
1244 fillRect(g, lastSSX, y + 4 + iconOffset, x * charWidth - lastSSX,
1245 isVectorRendition() ? 6 : 7);
1249 void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x,
1250 int y, int iconOffset, int startRes, int column, boolean validRes,
1253 int sCol = (lastSSX / charWidth)
1254 + hiddenColumns.visibleToAbsoluteColumn(startRes);
1256 int x2 = (x * charWidth);
1258 if (USE_FILL_ROUND_RECT || isVectorRendition())
1260 // draw glyph line behind helix (visible in EPS or SVG output)
1261 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1263 g.setColor(HELIX_COLOUR);
1265 int ofs = charWidth / 2;
1266 // Off by 1 offset when drawing rects and ovals
1267 // to offscreen image on the MAC
1268 fillRoundRect(g, lastSSX, y + 3 + iconOffset, x2 - x1 - 1, 8, 8, 8);
1269 if (sCol == 0 || row[sCol - 1] == null
1270 || row[sCol - 1].secondaryStructure != 'H')
1275 // g.setColor(Color.orange);
1276 fillRoundRect(g, lastSSX, y + 3 + iconOffset, x2 - x1 - ofs, 8, 0,
1279 if (!validRes || row[column] == null
1280 || row[column].secondaryStructure != 'H')
1286 // g.setColor(Color.magenta);
1287 fillRoundRect(g, lastSSX + ofs, y + 3 + iconOffset, x2 - x1 - ofs,
1294 boolean leftEnd = sCol == 0 || row[sCol - 1] == null
1295 || row[sCol - 1].secondaryStructure != 'H';
1296 boolean rightEnd = !validRes || row[column] == null
1297 || row[column].secondaryStructure != 'H';
1299 if (leftEnd || rightEnd)
1301 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1303 g.setColor(HELIX_COLOUR);
1307 fillArc(g, lastSSX, y + 3 + iconOffset, charWidth, 8, 90, 180);
1308 x1 += charWidth / 2;
1313 fillArc(g, (x * charWidth) - charWidth - 1, y + 3 + iconOffset,
1314 charWidth, 8, 270, 180);
1315 x2 -= charWidth / 2;
1318 fillRect(g, x1, y + 3 + iconOffset, x2 - x1, 8);
1321 void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1322 Annotation[] aa_annotations, int sRes, int eRes, int y, float min,
1323 float max, int graphHeight)
1325 if (sRes > aa_annotations.length)
1329 Stroke roundStroke = new BasicStroke(1, BasicStroke.CAP_ROUND,
1330 BasicStroke.JOIN_ROUND);
1331 Stroke squareStroke = new BasicStroke(1, BasicStroke.CAP_SQUARE,
1332 BasicStroke.JOIN_MITER);
1333 Graphics2D g2d = (Graphics2D) g;
1334 Stroke prevStroke = g2d.getStroke();
1335 g2d.setStroke(roundStroke);
1339 // Adjustment for fastpaint to left
1345 eRes = Math.min(eRes, aa_annotations.length);
1353 float range = max - min;
1358 y2 = y - (int) ((0 - min / range) * graphHeight);
1361 g.setColor(Color.gray);
1362 drawLine(g, squareStroke, x * charWidth - charWidth, y2,
1363 (eRes - sRes) * charWidth, y2);
1365 eRes = Math.min(eRes, aa_annotations.length);
1368 int aaMax = aa_annotations.length - 1;
1370 while (x < eRes - sRes)
1373 if (hasHiddenColumns)
1375 column = hiddenColumns.visibleToAbsoluteColumn(column);
1383 if (aa_annotations[column] == null)
1389 if (aa_annotations[column].colour == null)
1391 g.setColor(Color.black);
1395 g.setColor(aa_annotations[column].colour);
1398 if (aa_annotations[column - 1] == null
1399 && aa_annotations.length > column + 1
1400 && aa_annotations[column + 1] == null)
1403 y1 = y - (int) (((aa_annotations[column].value - min) / range)
1405 drawLine(g, x * charWidth + charWidth / 4, y1,
1406 x * charWidth + 3 * charWidth / 4, y1);
1411 if (aa_annotations[column - 1] == null)
1417 y1 = y - (int) (((aa_annotations[column - 1].value - min) / range)
1419 y2 = y - (int) (((aa_annotations[column].value - min) / range)
1422 drawLine(g, (x - 1) * charWidth + charWidth / 2, y1,
1423 x * charWidth + charWidth / 2, y2);
1427 if (_aa.threshold != null)
1429 g.setColor(_aa.threshold.colour);
1430 Graphics2D g2 = (Graphics2D) g;
1431 Stroke s = new BasicStroke(1, BasicStroke.CAP_SQUARE,
1432 BasicStroke.JOIN_ROUND, 3f, new float[]
1435 y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1436 drawLine(g, s, 0, y2, (eRes - sRes) * charWidth, y2);
1438 g2d.setStroke(prevStroke);
1441 @SuppressWarnings("unused")
1442 void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1443 Annotation[] aa_annotations, int sRes, int eRes, float min,
1444 float max, int y, boolean renderHistogram, boolean renderProfile,
1445 boolean normaliseProfile)
1447 if (sRes > aa_annotations.length)
1451 Font ofont = g.getFont();
1452 eRes = Math.min(eRes, aa_annotations.length);
1454 int x = 0, y1 = y, y2 = y;
1456 float range = max - min;
1460 y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1463 g.setColor(Color.gray);
1465 drawLine(g, x, y2, (eRes - sRes) * charWidth, y2);
1468 int aaMax = aa_annotations.length - 1;
1469 while (x < eRes - sRes)
1472 if (hasHiddenColumns)
1474 column = hiddenColumns.visibleToAbsoluteColumn(column);
1482 if (aa_annotations[column] == null)
1487 if (aa_annotations[column].colour == null)
1489 g.setColor(Color.black);
1493 g.setColor(aa_annotations[column].colour);
1496 y1 = y - (int) (((aa_annotations[column].value - min) / (range))
1499 if (renderHistogram)
1503 fillRect(g, x * charWidth, y2, charWidth, y1 - y2);
1507 fillRect(g, x * charWidth, y1, charWidth, y2 - y1);
1510 // draw profile if available
1515 * {profile type, #values, total count, char1, pct1, char2, pct2...}
1517 int profl[] = getProfileFor(_aa, column);
1519 // just try to draw the logo if profl is not null
1520 if (profl != null && profl[2] != 0)
1522 boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
1523 boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
1524 float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1525 final double normaliseFactor = normaliseProfile ? _aa.graphHeight
1529 * Render a single base for a sequence profile, a base pair for
1530 * structure profile, and a triplet for a cdna profile
1532 char[] dc = new char[isStructureProfile ? 2
1533 : (isCdnaProfile ? 3 : 1)];
1535 // lm is not necessary - we can just use fm - could be off by no more
1537 // LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1538 // Console.info(asc + " " + dec + " " + (asc -
1540 // + " " + (dec - lm.getDescent()));
1542 double asc = fm.getAscent();
1543 double dec = fm.getDescent();
1544 double fht = fm.getHeight();
1546 double scale = 1f / (normaliseProfile ? profl[2] : 100f);
1547 // float ofontHeight = 1f / fm.getAscent();// magnify to fill box
1550 * Traverse the character(s)/percentage data in the array
1555 // profl[1] is the number of values in the profile
1556 for (int i = 0, c = 3, last = profl[1]; i < last; i++)
1560 if (isStructureProfile)
1562 // todo can we encode a structure pair as an int, like codons?
1563 dc[0] = (char) profl[c++];
1564 dc[1] = (char) profl[c++];
1567 else if (isCdnaProfile)
1569 CodingUtils.decodeCodon2(profl[c++], dc);
1574 dc[0] = (char) profl[c++];
1577 // next profl[] position is profile % for the character(s)
1579 int percent = profl[c++];
1582 // failsafe in case a count rounds down to 0%
1585 double newHeight = normaliseFactor * scale * percent;
1588 * Set character colour as per alignment colour scheme; use the
1589 * codon translation if a cDNA profile
1591 Color colour = null;
1594 final String codonTranslation = ResidueProperties
1596 colour = profcolour.findColour(codonTranslation.charAt(0),
1601 colour = profcolour.findColour(dc[0], column, null);
1603 g.setColor(colour == Color.white ? Color.lightGray : colour);
1605 // Debug - render boxes around characters
1606 // g.setColor(Color.red);
1607 // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
1609 // g.setColor(profcolour.findColour(dc[0]).darker());
1611 double sx = 1f * charWidth / fm.charsWidth(dc, 0, dc.length);
1612 double sy = newHeight / asc;
1613 double newAsc = asc * sy;
1614 double newDec = dec * sy;
1615 // it is not necessary to recalculate lm for the new font.
1616 // note: lm.getBaselineOffsets()[lm.getBaselineIndex()]) must be 0
1617 // by definition. Was:
1618 // int hght = (int) (ht + (newAsc - newDec);
1619 // - lm.getBaselineOffsets()[lm.getBaselineIndex()]));
1621 if (Platform.isJS())
1624 * SwingJS does not implement font.deriveFont()
1625 * so use a scaling transform to draw instead,
1626 * this is off by a very small amount
1628 final int hght = (int) (ht2 + (newAsc - newDec));
1629 Graphics2D gg = (Graphics2D) g;
1630 int xShift = (int) Math.round(x * charWidth / sx);
1631 int yShift = (int) Math.round(hght / sy);
1632 gg.transform(AffineTransform.getScaleInstance(sx, sy));
1633 gg.drawString(s, xShift, yShift);
1635 AffineTransform.getScaleInstance(1D / sx, 1D / sy));
1645 // Java ('normal') method is to scale the font to fit
1647 final int hght = (int) (ht + (newAsc - newDec));
1649 .deriveFont(AffineTransform.getScaleInstance(sx, sy));
1651 g.drawChars(dc, 0, dc.length, x * charWidth, hght);
1661 if (_aa.threshold != null)
1663 g.setColor(_aa.threshold.colour);
1664 Stroke s = new BasicStroke(1, BasicStroke.CAP_SQUARE,
1665 BasicStroke.JOIN_ROUND, 3f, new float[]
1669 - ((_aa.threshold.value - min) / range) * _aa.graphHeight);
1670 drawLine(g, s, 0, y2, (eRes - sRes) * charWidth, y2);
1674 // used by overview window
1675 public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1676 Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1678 eRes = Math.min(eRes, aa_annotations.length);
1679 g.setColor(Color.white);
1680 fillRect(g, 0, 0, width, y);
1681 g.setColor(new Color(0, 0, 180));
1685 for (int j = sRes; j < eRes; j++)
1687 if (aa_annotations[j] != null)
1689 if (aa_annotations[j].colour == null)
1691 g.setColor(Color.black);
1695 g.setColor(aa_annotations[j].colour);
1698 height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1704 fillRect(g, x, y - height, charWidth, height);
1710 Color getNotCanonicalColor(char lastss)
1716 return new Color(255, 125, 5);
1720 return new Color(245, 115, 10);
1724 return new Color(235, 135, 15);
1728 return new Color(225, 105, 20);
1732 return new Color(215, 145, 30);
1736 return new Color(205, 95, 35);
1740 return new Color(195, 155, 45);
1744 return new Color(185, 85, 55);
1748 return new Color(175, 165, 65);
1752 return new Color(170, 75, 75);
1756 return new Color(160, 175, 85);
1760 return new Color(150, 65, 95);
1764 return new Color(140, 185, 105);
1768 return new Color(130, 55, 110);
1772 return new Color(120, 195, 120);
1776 return new Color(110, 45, 130);
1780 return new Color(100, 205, 140);
1784 return new Color(90, 35, 150);
1788 return new Color(85, 215, 160);
1792 return new Color(75, 25, 170);
1796 return new Color(65, 225, 180);
1800 return new Color(55, 15, 185);
1804 return new Color(45, 235, 195);
1808 return new Color(35, 5, 205);
1812 return new Color(25, 245, 215);
1816 return new Color(15, 0, 225);
1820 return new Color(10, 255, 235);
1824 return new Color(5, 150, 245);
1828 return new Color(0, 80, 255);
1831 Console.info("This is not a interaction : " + lastss);
1837 private void fillPolygon(Graphics g, int[] xpoints, int[] ypoints, int n)
1840 g.fillPolygon(xpoints, ypoints, n);
1841 if (!isVectorRendition())
1844 g.fillPolygon(xpoints, ypoints, n);
1845 g.drawPolygon(xpoints, ypoints, n);
1850 private void fillRect(Graphics g, int a, int b, int c, int d)
1852 fillRect(g, false, a, b, c, d);
1855 private void fillRect(Graphics g, int a, int b, int c, int d)
1857 g.fillRect(a, b, c, d);
1859 if (false && !isVectorRendition() && drawRect)
1861 g.drawRect(a, b, c, d);
1866 private void fillRoundRect(Graphics g, int a, int b, int c, int d, int e,
1870 g.fillRoundRect(a, b, c, d, e, f);
1871 if (!isVectorRendition())
1873 g.drawRoundRect(a, b, c, d, e, f);
1877 private void fillArc(Graphics g, int a, int b, int c, int d, int e, int f)
1880 g.fillArc(a, b, c, d, e, f);
1881 if (!isVectorRendition())
1883 g.drawArc(a, b, c, d, e, f);
1887 private void drawLine(Graphics g, Stroke s, int a, int b, int c, int d)
1889 Graphics2D g2d = (Graphics2D) g;
1890 Stroke p = g2d.getStroke();
1892 drawLine(g, a, b, c, d);
1896 private void drawLine(Graphics g, int a, int b, int c, int d)
1899 g.drawLine(a, b, c, d);
1902 private void setAntialias(Graphics g)
1904 if (isVectorRendition())
1906 // no need to antialias vector drawings
1909 if (Cache.getDefault("ANTI_ALIAS", true))
1911 Graphics2D g2d = (Graphics2D) g;
1912 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1913 RenderingHints.VALUE_ANTIALIAS_ON);
1917 private void unsetAntialias(Graphics g)
1919 if (isVectorRendition())
1921 // no need to antialias vector drawings
1924 Graphics2D g2d = (Graphics2D) g;
1925 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1926 RenderingHints.VALUE_ANTIALIAS_OFF);
1929 public void setVectorRendition(boolean b)
1931 vectorRendition = b;
1934 public boolean isVectorRendition()
1936 return vectorRendition;