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 renderingVectors = 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 int sCol = (lastSSX / charWidth)
179 + hiddenColumns.visibleToAbsoluteColumn(startRes);
181 int x2 = (x * charWidth);
183 char dc = (column == 0 || row_annotations[column - 1] == null) ? ' '
184 : row_annotations[column - 1].secondaryStructure;
186 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
187 || dc != row_annotations[sCol - 1].secondaryStructure
189 boolean diffdownstream = !validRes || !validEnd
190 || row_annotations[column] == null
191 || dc != row_annotations[column].secondaryStructure;
193 if (diffupstream || diffdownstream)
195 // draw glyphline under arrow
196 drawGlyphLine(g, lastSSX, x, y, iconOffset);
198 g.setColor(STEM_COLOUR);
200 if (column > 0 && Rna.isClosingParenthesis(dc))
203 // if (validRes && column>1 && row_annotations[column-2]!=null &&
204 // dc.equals(row_annotations[column-2].displayCharacter))
207 * if new annotation with a closing base pair half of the stem,
208 * display a backward arrow
210 fillPolygon(g, new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
212 { y + iconOffset + 1, y + 13 + iconOffset,
213 y + 7 + iconOffset },
224 // display a forward arrow
228 * if annotation ending with an opeing base pair half of the stem,
229 * display a forward arrow
231 fillPolygon(g, new int[] { x2 - 6, x2 - 6, x2 - 1 },
233 { y + iconOffset + 1, y + 13 + iconOffset,
234 y + 7 + iconOffset },
245 fillRect(g, x1, y + 4 + iconOffset, x2 - x1, 6);
248 void drawNotCanonicalAnnot(Graphics g, Color nonCanColor,
249 Annotation[] row_annotations, int lastSSX, int x, int y,
250 int iconOffset, int startRes, int column, boolean validRes,
253 // Console.info(nonCanColor);
255 int sCol = (lastSSX / charWidth)
256 + hiddenColumns.visibleToAbsoluteColumn(startRes);
258 int x2 = (x * charWidth);
260 String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
261 : row_annotations[column - 1].displayCharacter;
263 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
264 || !dc.equals(row_annotations[sCol - 1].displayCharacter)
266 boolean diffdownstream = !validRes || !validEnd
267 || row_annotations[column] == null
268 || !dc.equals(row_annotations[column].displayCharacter);
269 // Console.info("Column "+column+" diff up:
271 // down:"+diffdownstream);
272 // If a closing base pair half of the stem, display a backward arrow
273 if (diffupstream || diffdownstream)
275 // draw glyphline under arrow
276 drawGlyphLine(g, lastSSX, x, y, iconOffset);
278 g.setColor(nonCanColor);
279 if (column > 0 && Rna.isClosingParenthesis(dc))
283 // if (validRes && column>1 && row_annotations[column-2]!=null &&
284 // dc.equals(row_annotations[column-2].displayCharacter))
286 fillPolygon(g, new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
288 { y + iconOffset + 1, y + 13 + iconOffset,
289 y + 7 + iconOffset },
301 // display a forward arrow
304 fillPolygon(g, new int[] { x2 - 6, x2 - 6, x2 - 1 },
306 { y + iconOffset + 1, y + 13 + iconOffset,
307 y + 7 + iconOffset },
318 fillRect(g, x1, y + 4 + iconOffset, x2 - x1, 6);
321 // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
323 public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
326 fm = annotPanel.getFontMetrics();
327 annotationPanel = annotPanel;
328 fadedImage = annotPanel.getFadedImage();
329 imgWidth = annotPanel.getFadedImageWidth();
330 // visible area for rendering
331 int[] bounds = annotPanel.getVisibleVRange();
335 visHeight = bounds[1];
350 rendererFactoryI = AnnotationRendererFactory.getRendererFactory();
351 updateFromAlignViewport(av);
354 public void updateFromAlignViewport(AlignViewportI av)
356 charWidth = av.getCharWidth();
357 endRes = av.getRanges().getEndRes();
358 charHeight = av.getCharHeight();
359 hasHiddenColumns = av.hasHiddenColumns();
360 validCharWidth = av.isValidCharWidth();
361 av_renderHistogram = av.isShowConsensusHistogram();
362 av_renderProfile = av.isShowSequenceLogo();
363 av_normaliseProfile = av.isNormaliseSequenceLogo();
364 profcolour = av.getResidueShading();
365 if (profcolour == null || profcolour.getColourScheme() == null)
368 * Use default colour for sequence logo if
369 * the alignment has no colourscheme set
370 * (would like to use user preference but n/a for applet)
372 ColourSchemeI col = av.getAlignment().isNucleotide()
373 ? new NucleotideColourScheme()
374 : new ZappoColourScheme();
375 profcolour = new ResidueShader(col);
377 columnSelection = av.getColumnSelection();
378 hiddenColumns = av.getAlignment().getHiddenColumns();
379 hconsensus = av.getSequenceConsensusHash();
380 complementConsensus = av.getComplementConsensusHash();
381 hStrucConsensus = av.getRnaStructureConsensusHash();
382 av_ignoreGapsConsensus = av.isIgnoreGapsConsensus();
386 * Returns profile data; the first element is the profile type, the second is
387 * the number of distinct values, the third the total count, and the remainder
388 * depend on the profile type.
394 int[] getProfileFor(AlignmentAnnotation aa, int column)
396 // TODO : consider refactoring the global alignment calculation
397 // properties/rendering attributes as a global 'alignment group' which holds
398 // all vis settings for the alignment as a whole rather than a subset
400 if (aa.autoCalculated && (aa.label.startsWith("Consensus")
401 || aa.label.startsWith("cDNA Consensus")))
403 boolean forComplement = aa.label.startsWith("cDNA Consensus");
404 if (aa.groupRef != null && aa.groupRef.consensusData != null
405 && aa.groupRef.isShowSequenceLogo())
407 // TODO? group consensus for cDNA complement
408 return AAFrequency.extractProfile(
409 aa.groupRef.consensusData.get(column),
410 aa.groupRef.getIgnoreGapsConsensus());
412 // TODO extend annotation row to enable dynamic and static profile data to
414 if (aa.groupRef == null && aa.sequenceRef == null)
418 return AAFrequency.extractCdnaProfile(complementConsensus[column],
419 av_ignoreGapsConsensus);
423 return AAFrequency.extractProfile(hconsensus.get(column),
424 av_ignoreGapsConsensus);
430 if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
432 // TODO implement group structure consensus
434 * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
435 * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
436 * group selections return StructureFrequency.extractProfile(
437 * aa.groupRef.consensusData[column], aa.groupRef
438 * .getIgnoreGapsConsensus()); }
440 // TODO extend annotation row to enable dynamic and static profile data
443 if (aa.groupRef == null && aa.sequenceRef == null
444 && hStrucConsensus != null
445 && hStrucConsensus.length > column)
447 return StructureFrequency.extractProfile(hStrucConsensus[column],
448 av_ignoreGapsConsensus);
457 private AnnotationRendererFactoryI rendererFactoryI;
460 * Render the annotation rows associated with an alignment.
465 * data and view settings to render
467 * destination for graphics
469 * row where a mouse event occured (or -1)
471 * first column that will be drawn
473 * last column that will be drawn
474 * @return true if the fadedImage was used for any alignment annotation rows
475 * currently being calculated
477 public boolean drawComponent(AwtRenderPanelI annotPanel,
478 AlignViewportI av, Graphics g, int activeRow, int startRes,
481 if (g instanceof EpsGraphics2D || g instanceof SVGGraphics2D)
483 this.setVectorRendering(true);
485 Graphics2D g2d = (Graphics2D) g;
487 long stime = System.currentTimeMillis();
488 boolean usedFaded = false;
490 // AnnotationPanel needs to implement: ImageObserver, access to
492 updateFromAwtRenderPanel(annotPanel, av);
493 fm = g.getFontMetrics();
494 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
505 boolean validRes = false;
506 boolean validEnd = false;
507 boolean labelAllCols = false;
508 // boolean centreColLabels;
509 // boolean centreColLabelsDef = av.isCentreColumnLabels();
510 boolean scaleColLabel = false;
511 final AlignmentAnnotation consensusAnnot = av
512 .getAlignmentConsensusAnnotation();
513 final AlignmentAnnotation structConsensusAnnot = av
514 .getAlignmentStrucConsensusAnnotation();
515 final AlignmentAnnotation complementConsensusAnnot = av
516 .getComplementConsensusAnnotation();
518 BitSet graphGroupDrawn = new BitSet();
519 int charOffset = 0; // offset for a label
522 int yfrom = 0, f_i = 0, yto = 0, f_to = 0;
523 boolean clipst = false, clipend = false;
524 for (int i = 0; i < aa.length; i++)
526 AlignmentAnnotation row = aa[i];
527 boolean renderHistogram = true;
528 boolean renderProfile = false;
529 boolean normaliseProfile = false;
530 boolean isRNA = row.isRNA();
532 // check if this is a consensus annotation row and set the display
533 // settings appropriately
534 // TODO: generalise this to have render styles for consensus/profile
536 if (row.groupRef != null && row == row.groupRef.getConsensus())
538 renderHistogram = row.groupRef.isShowConsensusHistogram();
539 renderProfile = row.groupRef.isShowSequenceLogo();
540 normaliseProfile = row.groupRef.isNormaliseSequenceLogo();
542 else if (row == consensusAnnot || row == structConsensusAnnot
543 || row == complementConsensusAnnot)
545 renderHistogram = av_renderHistogram;
546 renderProfile = av_renderProfile;
547 normaliseProfile = av_normaliseProfile;
550 Annotation[] row_annotations = row.annotations;
555 // centreColLabels = row.centreColLabels || centreColLabelsDef;
556 labelAllCols = row.showAllColLabels;
557 scaleColLabel = row.scaleColLabel;
561 if (!useClip || ((y - charHeight) < visHeight
562 && (y + row.height + charHeight * 2) >= sOffset))
563 {// if_in_visible_region
574 if (row.graphGroup > -1 && graphGroupDrawn.get(row.graphGroup))
579 // this is so that we draw the characters below the graph
584 iconOffset = charHeight - fm.getDescent();
588 else if (row.hasText)
590 iconOffset = charHeight - fm.getDescent();
598 if (row.autoCalculated && av.isCalculationInProgress(row))
602 g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0,
603 y - row.height, imgWidth, y, annotationPanel);
604 g.setColor(Color.black);
605 // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
611 * else if (annotationPanel.av.updatingConservation &&
612 * aa[i].label.equals("Conservation")) {
614 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
615 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
616 * annotationPanel.imgWidth, y, annotationPanel);
618 * g.setColor(Color.black); //
619 * g.drawString("Calculating Conservation.....",20, y-row.height/2);
621 * continue; } else if (annotationPanel.av.updatingConservation &&
622 * aa[i].label.equals("Quality")) {
624 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
625 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
626 * annotationPanel.imgWidth, y, annotationPanel);
627 * g.setColor(Color.black); // /
628 * g.drawString("Calculating Quality....",20, y-row.height/2);
633 // first pass sets up state for drawing continuation from left-hand
637 // flag used for vector rendition
638 this.glyphLineDrawn = false;
639 x = (startRes == 0) ? 0 : -1;
640 while (x < endRes - startRes)
642 if (hasHiddenColumns)
644 column = hiddenColumns.visibleToAbsoluteColumn(startRes + x);
645 if (column > row_annotations.length - 1)
652 column = startRes + x;
655 if ((row_annotations == null)
656 || (row_annotations.length <= column)
657 || (row_annotations[column] == null))
665 final String displayChar = validRes
666 ? row_annotations[column].displayCharacter
673 g.setColor(Color.red);
675 if (columnSelection != null)
677 if (columnSelection.contains(column))
679 fillRect(g, x * charWidth, y, charWidth, charHeight);
683 if (row.getInvalidStrucPos() > x)
685 g.setColor(Color.orange);
686 fillRect(g, x * charWidth, y, charWidth, charHeight);
688 else if (row.getInvalidStrucPos() == x)
690 g.setColor(Color.orange.darker());
691 fillRect(g, x * charWidth, y, charWidth, charHeight);
693 if (validCharWidth && validRes && displayChar != null
694 && (displayChar.length() > 0))
696 // Graphics2D gg = (g);
697 float fmWidth = fm.charsWidth(displayChar.toCharArray(), 0,
698 displayChar.length());
701 * shrink label width to fit in column, if that is
702 * both configured and necessary
704 boolean scaledToFit = false;
705 float fmScaling = 1f;
706 if (scaleColLabel && fmWidth > charWidth)
709 fmScaling = charWidth;
710 fmScaling /= fmWidth;
711 // and update the label's width to reflect the scaling.
715 charOffset = (int) ((charWidth - fmWidth) / 2f);
717 if (row_annotations[column].colour == null)
719 g2d.setColor(Color.black);
723 g2d.setColor(row_annotations[column].colour);
727 * draw the label, unless it is the same secondary structure
728 * symbol (excluding RNA Helix) as the previous column
730 final int xPos = (x * charWidth) + charOffset;
731 final int yPos = y + iconOffset;
734 * translate to drawing position _before_ applying any scaling
736 g2d.translate(xPos, yPos);
740 * use a scaling transform to make the label narrower
741 * (JalviewJS doesn't have Font.deriveFont(AffineTransform))
744 AffineTransform.getScaleInstance(fmScaling, 1.0));
747 if (column == 0 || row.graph > 0)
749 g2d.drawString(displayChar, 0, 0);
751 else if (row_annotations[column - 1] == null || (labelAllCols
752 || !displayChar.equals(
753 row_annotations[column - 1].displayCharacter)
754 || (displayChar.length() < 2
755 && row_annotations[column].secondaryStructure == ' ')))
757 g2d.drawString(displayChar, 0, 0);
762 * undo scaling before translating back
763 * (restoring saved transform does NOT work in JS PDFGraphics!)
765 g2d.transform(AffineTransform
766 .getScaleInstance(1D / fmScaling, 1.0));
768 g2d.translate(-xPos, -yPos);
773 char ss = validRes ? row_annotations[column].secondaryStructure
778 // distinguish between forward/backward base-pairing
779 if (displayChar.indexOf(')') > -1)
788 if ((displayChar.indexOf(']') > -1))
796 // distinguish between forward/backward base-pairing
797 if (displayChar.indexOf('}') > -1)
805 // distinguish between forward/backward base-pairing
806 if (displayChar.indexOf('<') > -1)
812 if (isRNA && (ss >= CHAR_A) && (ss <= CHAR_Z))
814 // distinguish between forward/backward base-pairing
815 int ssLowerCase = ss + UPPER_TO_LOWER;
816 // TODO would .equals() be safer here? or charAt(0)?
817 if (displayChar.indexOf(ssLowerCase) > -1)
819 ss = (char) ssLowerCase;
823 if (!validRes || (ss != lastSS))
829 // int nb_annot = x - temp;
830 // Console.info("\t type :"+lastSS+"\t x
832 // annot :"+nb_annot);
835 case '(': // Stem case for RNA secondary structure
836 case ')': // and opposite direction
837 drawStemAnnot(g, row_annotations, lastSSX, x, y,
838 iconOffset, startRes, column, validRes, validEnd);
845 drawHelixAnnot(g, row_annotations, lastSSX, x, y,
846 iconOffset, startRes, column, validRes,
850 // no break if isRNA - falls through to drawNotCanonicalAnnot!
854 drawSheetAnnot(g, row_annotations, lastSSX, x, y,
855 iconOffset, startRes, column, validRes,
859 // no break if isRNA - fall through to drawNotCanonicalAnnot!
918 Color nonCanColor = getNotCanonicalColor(lastSS);
919 drawNotCanonicalAnnot(g, nonCanColor, row_annotations,
920 lastSSX, x, y, iconOffset, startRes, column,
925 if (isVectorRendering())
927 // draw single full width glyphline
928 drawGlyphLine(g, lastSSX, endRes - x, y, iconOffset);
929 // disable more glyph lines
930 this.glyphLineDrawn = true;
934 drawGlyphLine(g, lastSSX, x, y, iconOffset);
949 lastSSX = (x * charWidth);
956 if (column >= row_annotations.length)
958 column = row_annotations.length - 1;
965 if ((row_annotations == null) || (row_annotations.length <= column)
966 || (row_annotations[column] == null))
984 drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
985 startRes, column, validRes, validEnd);
988 // no break if isRNA - fall through to drawNotCanonicalAnnot!
993 drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
994 startRes, column, validRes, validEnd);
997 // no break if isRNA - fall through to drawNotCanonicalAnnot!
1000 case ')': // Stem case for RNA secondary structure
1002 drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
1003 startRes, column, validRes, validEnd);
1060 // Console.info(lastSS);
1061 Color nonCanColor = getNotCanonicalColor(lastSS);
1062 drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX,
1063 x, y, iconOffset, startRes, column, validRes, validEnd);
1066 if (isVectorRendering())
1068 // draw single full width glyphline
1069 drawGlyphLine(g, lastSSX, endRes - x, y, iconOffset);
1070 // disable more glyph lines
1071 this.glyphLineDrawn = true;
1075 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1081 if (row.graph > 0 && row.graphHeight > 0)
1083 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1085 if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
1087 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1088 // computed efficiently for all visible labels
1089 float groupmax = -999999, groupmin = 9999999;
1090 for (int gg = 0; gg < aa.length; gg++)
1092 if (aa[gg].graphGroup != row.graphGroup)
1099 aa[gg].visible = false;
1101 if (aa[gg].graphMax > groupmax)
1103 groupmax = aa[gg].graphMax;
1105 if (aa[gg].graphMin < groupmin)
1107 groupmin = aa[gg].graphMin;
1111 for (int gg = 0; gg < aa.length; gg++)
1113 if (aa[gg].graphGroup == row.graphGroup)
1115 drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
1116 endRes, y, groupmin, groupmax, row.graphHeight);
1120 graphGroupDrawn.set(row.graphGroup);
1124 drawLineGraph(g, row, row_annotations, startRes, endRes, y,
1125 row.graphMin, row.graphMax, row.graphHeight);
1128 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1130 drawBarGraph(g, row, row_annotations, startRes, endRes,
1131 row.graphMin, row.graphMax, y, renderHistogram,
1132 renderProfile, normaliseProfile);
1136 AnnotationRowRendererI renderer = rendererFactoryI
1137 .getRendererFor(row);
1138 if (renderer != null)
1140 renderer.renderRow(g, charWidth, charHeight, hasHiddenColumns,
1141 av, hiddenColumns, columnSelection, row,
1142 row_annotations, startRes, endRes, row.graphMin,
1147 if (renderer == null)
1150 .println("No renderer found for " + row.toString());
1155 "rendered with " + renderer.getClass().toString());
1164 if (clipst && !clipend)
1168 } // end if_in_visible_region
1169 if (row.graph > 0 && row.hasText)
1185 Console.warn("Start clip at : " + yfrom + " (index " + f_i + ")");
1189 Console.warn("End clip at : " + yto + " (index " + f_to + ")");
1193 Console.warn("Annotation Rendering time:"
1194 + (System.currentTimeMillis() - stime));
1201 public static final Color GLYPHLINE_COLOR = Color.gray;
1203 public static final Color SHEET_COLOUR = Color.green;
1205 public static final Color HELIX_COLOUR = Color.red;
1207 public static final Color STEM_COLOUR = Color.blue;
1209 // private Color sdNOTCANONICAL_COLOUR;
1211 void drawGlyphLine(Graphics g, int lastSSX, int x, int y, int iconOffset)
1215 // if we've drawn a single long glyphline for an export, don't draw the
1220 g.setColor(GLYPHLINE_COLOR);
1221 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
1224 void drawSheetAnnot(Graphics g, Annotation[] row,
1226 int lastSSX, int x, int y, int iconOffset, int startRes,
1227 int column, boolean validRes, boolean validEnd)
1229 if (!validEnd || !validRes || row == null || row[column] == null
1230 || row[column].secondaryStructure != 'E')
1232 // draw the glyphline underneath
1233 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1235 g.setColor(SHEET_COLOUR);
1236 fillRect(g, lastSSX, y + 4 + iconOffset,
1237 (x * charWidth) - lastSSX - 4, 6);
1240 { (x * charWidth) - 6, (x * charWidth) - 6,
1241 (x * charWidth - 1) },
1243 { y + iconOffset + 1, y + 13 + iconOffset,
1244 y + 7 + iconOffset },
1249 g.setColor(SHEET_COLOUR);
1250 fillRect(g, lastSSX, y + 4 + iconOffset, (x * charWidth) - lastSSX,
1255 void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x,
1256 int y, int iconOffset, int startRes, int column, boolean validRes,
1259 int sCol = (lastSSX / charWidth)
1260 + hiddenColumns.visibleToAbsoluteColumn(startRes);
1262 int x2 = (x * charWidth);
1264 if (USE_FILL_ROUND_RECT || isVectorRendering())
1266 // draw glyph line behind helix (visible in EPS or SVG output)
1267 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1269 g.setColor(HELIX_COLOUR);
1271 int ofs = charWidth / 2;
1272 // Off by 1 offset when drawing rects and ovals
1273 // to offscreen image on the MAC
1274 fillRoundRect(g, lastSSX, y + 3 + iconOffset, x2 - x1 - 1, 8, 8, 8);
1275 if (sCol == 0 || row[sCol - 1] == null
1276 || row[sCol - 1].secondaryStructure != 'H')
1281 fillRoundRect(g, lastSSX, y + 3 + iconOffset, x2 - x1 - ofs, 8, 0,
1284 if (!validRes || row[column] == null
1285 || row[column].secondaryStructure != 'H')
1291 fillRoundRect(g, lastSSX + ofs, y + 3 + iconOffset, x2 - x1 - ofs,
1298 boolean leftEnd = sCol == 0 || row[sCol - 1] == null
1299 || row[sCol - 1].secondaryStructure != 'H';
1300 boolean rightEnd = !validRes || row[column] == null
1301 || row[column].secondaryStructure != 'H';
1303 if (leftEnd || rightEnd)
1305 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1307 g.setColor(HELIX_COLOUR);
1311 fillArc(g, lastSSX, y + 3 + iconOffset, charWidth, 8, 90, 180);
1312 x1 += charWidth / 2;
1317 fillArc(g, ((x - 1) * charWidth), y + 3 + iconOffset, charWidth, 8,
1319 x2 -= charWidth / 2;
1322 fillRect(g, x1, y + 3 + iconOffset, x2 - x1, 8);
1325 void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1326 Annotation[] aa_annotations, int sRes, int eRes, int y, float min,
1327 float max, int graphHeight)
1329 if (sRes > aa_annotations.length)
1333 Stroke roundStroke = new BasicStroke(1, BasicStroke.CAP_ROUND,
1334 BasicStroke.JOIN_ROUND);
1335 Stroke squareStroke = new BasicStroke(1, BasicStroke.CAP_SQUARE,
1336 BasicStroke.JOIN_MITER);
1337 Graphics2D g2d = (Graphics2D) g;
1338 Stroke prevStroke = g2d.getStroke();
1339 g2d.setStroke(roundStroke);
1343 // Adjustment for fastpaint to left
1349 eRes = Math.min(eRes, aa_annotations.length);
1357 float range = max - min;
1362 y2 = y - (int) ((0 - min / range) * graphHeight);
1365 g.setColor(Color.gray);
1366 drawLine(g, squareStroke, x * charWidth - charWidth, y2,
1367 (eRes - sRes) * charWidth, y2);
1369 eRes = Math.min(eRes, aa_annotations.length);
1372 int aaMax = aa_annotations.length - 1;
1374 while (x < eRes - sRes)
1377 if (hasHiddenColumns)
1379 column = hiddenColumns.visibleToAbsoluteColumn(column);
1387 if (aa_annotations[column] == null)
1393 if (aa_annotations[column].colour == null)
1395 g.setColor(Color.black);
1399 g.setColor(aa_annotations[column].colour);
1402 if (aa_annotations[column - 1] == null
1403 && aa_annotations.length > column + 1
1404 && aa_annotations[column + 1] == null)
1407 y1 = y - (int) (((aa_annotations[column].value - min) / range)
1409 drawLine(g, x * charWidth + charWidth / 4, y1,
1410 x * charWidth + 3 * charWidth / 4, y1);
1415 if (aa_annotations[column - 1] == null)
1421 y1 = y - (int) (((aa_annotations[column - 1].value - min) / range)
1423 y2 = y - (int) (((aa_annotations[column].value - min) / range)
1426 drawLine(g, (x - 1) * charWidth + charWidth / 2, y1,
1427 x * charWidth + charWidth / 2, y2);
1431 if (_aa.threshold != null)
1433 g.setColor(_aa.threshold.colour);
1434 Graphics2D g2 = (Graphics2D) g;
1435 Stroke s = new BasicStroke(1, BasicStroke.CAP_SQUARE,
1436 BasicStroke.JOIN_ROUND, 3f, new float[]
1439 y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1440 drawLine(g, s, 0, y2, (eRes - sRes) * charWidth, y2);
1442 g2d.setStroke(prevStroke);
1445 @SuppressWarnings("unused")
1446 void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1447 Annotation[] aa_annotations, int sRes, int eRes, float min,
1448 float max, int y, boolean renderHistogram, boolean renderProfile,
1449 boolean normaliseProfile)
1451 if (sRes > aa_annotations.length)
1455 Font ofont = g.getFont();
1456 eRes = Math.min(eRes, aa_annotations.length);
1458 int x = 0, y1 = y, y2 = y;
1460 float range = max - min;
1464 y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1467 g.setColor(Color.gray);
1469 drawLine(g, x, y2, (eRes - sRes) * charWidth, y2);
1472 int aaMax = aa_annotations.length - 1;
1473 while (x < eRes - sRes)
1476 if (hasHiddenColumns)
1478 column = hiddenColumns.visibleToAbsoluteColumn(column);
1486 if (aa_annotations[column] == null)
1491 if (aa_annotations[column].colour == null)
1493 g.setColor(Color.black);
1497 g.setColor(aa_annotations[column].colour);
1500 y1 = y - (int) (((aa_annotations[column].value - min) / (range))
1503 if (renderHistogram)
1507 fillRect(g, x * charWidth, y2, charWidth, y1 - y2);
1511 fillRect(g, x * charWidth, y1, charWidth, y2 - y1);
1514 // draw profile if available
1519 * {profile type, #values, total count, char1, pct1, char2, pct2...}
1521 int profl[] = getProfileFor(_aa, column);
1523 // just try to draw the logo if profl is not null
1524 if (profl != null && profl[2] != 0)
1526 boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
1527 boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
1528 float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1529 final double normaliseFactor = normaliseProfile ? _aa.graphHeight
1533 * Render a single base for a sequence profile, a base pair for
1534 * structure profile, and a triplet for a cdna profile
1536 char[] dc = new char[isStructureProfile ? 2
1537 : (isCdnaProfile ? 3 : 1)];
1539 // lm is not necessary - we can just use fm - could be off by no more
1541 // LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1542 // Console.info(asc + " " + dec + " " + (asc -
1544 // + " " + (dec - lm.getDescent()));
1546 double asc = fm.getAscent();
1547 double dec = fm.getDescent();
1548 double fht = fm.getHeight();
1550 double scale = 1f / (normaliseProfile ? profl[2] : 100f);
1551 // float ofontHeight = 1f / fm.getAscent();// magnify to fill box
1554 * Traverse the character(s)/percentage data in the array
1559 // profl[1] is the number of values in the profile
1560 for (int i = 0, c = 3, last = profl[1]; i < last; i++)
1564 if (isStructureProfile)
1566 // todo can we encode a structure pair as an int, like codons?
1567 dc[0] = (char) profl[c++];
1568 dc[1] = (char) profl[c++];
1571 else if (isCdnaProfile)
1573 CodingUtils.decodeCodon2(profl[c++], dc);
1578 dc[0] = (char) profl[c++];
1581 // next profl[] position is profile % for the character(s)
1583 int percent = profl[c++];
1586 // failsafe in case a count rounds down to 0%
1589 double newHeight = normaliseFactor * scale * percent;
1592 * Set character colour as per alignment colour scheme; use the
1593 * codon translation if a cDNA profile
1595 Color colour = null;
1598 final String codonTranslation = ResidueProperties
1600 colour = profcolour.findColour(codonTranslation.charAt(0),
1605 colour = profcolour.findColour(dc[0], column, null);
1607 g.setColor(colour == Color.white ? Color.lightGray : colour);
1609 // Debug - render boxes around characters
1610 // g.setColor(Color.red);
1611 // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
1613 // g.setColor(profcolour.findColour(dc[0]).darker());
1615 double sx = 1f * charWidth / fm.charsWidth(dc, 0, dc.length);
1616 double sy = newHeight / asc;
1617 double newAsc = asc * sy;
1618 double newDec = dec * sy;
1619 // it is not necessary to recalculate lm for the new font.
1620 // note: lm.getBaselineOffsets()[lm.getBaselineIndex()]) must be 0
1621 // by definition. Was:
1622 // int hght = (int) (ht + (newAsc - newDec);
1623 // - lm.getBaselineOffsets()[lm.getBaselineIndex()]));
1625 if (Platform.isJS())
1628 * SwingJS does not implement font.deriveFont()
1629 * so use a scaling transform to draw instead,
1630 * this is off by a very small amount
1632 final int hght = (int) (ht2 + (newAsc - newDec));
1633 Graphics2D gg = (Graphics2D) g;
1634 int xShift = (int) Math.round(x * charWidth / sx);
1635 int yShift = (int) Math.round(hght / sy);
1636 gg.transform(AffineTransform.getScaleInstance(sx, sy));
1637 gg.drawString(s, xShift, yShift);
1639 AffineTransform.getScaleInstance(1D / sx, 1D / sy));
1649 // Java ('normal') method is to scale the font to fit
1651 final int hght = (int) (ht + (newAsc - newDec));
1653 .deriveFont(AffineTransform.getScaleInstance(sx, sy));
1655 g.drawChars(dc, 0, dc.length, x * charWidth, hght);
1665 if (_aa.threshold != null)
1667 g.setColor(_aa.threshold.colour);
1668 Stroke s = new BasicStroke(1, BasicStroke.CAP_SQUARE,
1669 BasicStroke.JOIN_ROUND, 3f, new float[]
1673 - ((_aa.threshold.value - min) / range) * _aa.graphHeight);
1674 drawLine(g, s, 0, y2, (eRes - sRes) * charWidth, y2);
1678 // used by overview window
1679 public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1680 Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1682 eRes = Math.min(eRes, aa_annotations.length);
1683 g.setColor(Color.white);
1684 fillRect(g, 0, 0, width, y);
1685 g.setColor(new Color(0, 0, 180));
1689 for (int j = sRes; j < eRes; j++)
1691 if (aa_annotations[j] != null)
1693 if (aa_annotations[j].colour == null)
1695 g.setColor(Color.black);
1699 g.setColor(aa_annotations[j].colour);
1702 height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1708 fillRect(g, x, y - height, charWidth, height);
1714 Color getNotCanonicalColor(char lastss)
1720 return new Color(255, 125, 5);
1724 return new Color(245, 115, 10);
1728 return new Color(235, 135, 15);
1732 return new Color(225, 105, 20);
1736 return new Color(215, 145, 30);
1740 return new Color(205, 95, 35);
1744 return new Color(195, 155, 45);
1748 return new Color(185, 85, 55);
1752 return new Color(175, 165, 65);
1756 return new Color(170, 75, 75);
1760 return new Color(160, 175, 85);
1764 return new Color(150, 65, 95);
1768 return new Color(140, 185, 105);
1772 return new Color(130, 55, 110);
1776 return new Color(120, 195, 120);
1780 return new Color(110, 45, 130);
1784 return new Color(100, 205, 140);
1788 return new Color(90, 35, 150);
1792 return new Color(85, 215, 160);
1796 return new Color(75, 25, 170);
1800 return new Color(65, 225, 180);
1804 return new Color(55, 15, 185);
1808 return new Color(45, 235, 195);
1812 return new Color(35, 5, 205);
1816 return new Color(25, 245, 215);
1820 return new Color(15, 0, 225);
1824 return new Color(10, 255, 235);
1828 return new Color(5, 150, 245);
1832 return new Color(0, 80, 255);
1835 Console.info("This is not a interaction : " + lastss);
1841 private void fillPolygon(Graphics g, int[] xpoints, int[] ypoints, int n)
1844 g.fillPolygon(xpoints, ypoints, n);
1848 private void fillRect(Graphics g, int a, int b, int c, int d)
1850 fillRect(g, false, a, b, c, d);
1853 private void fillRect(Graphics g, int a, int b, int c, int d)
1856 g.fillRect(a, b, c, d);
1859 private void fillRoundRect(Graphics g, int a, int b, int c, int d, int e,
1863 g.fillRoundRect(a, b, c, d, e, f);
1866 private void fillArc(Graphics g, int a, int b, int c, int d, int e, int f)
1869 g.fillArc(a, b, c, d, e, f);
1872 private void drawLine(Graphics g, Stroke s, int a, int b, int c, int d)
1874 Graphics2D g2d = (Graphics2D) g;
1875 Stroke p = g2d.getStroke();
1877 drawLine(g, a, b, c, d);
1881 private void drawLine(Graphics g, int a, int b, int c, int d)
1884 g.drawLine(a, b, c, d);
1887 private void setAntialias(Graphics g)
1889 if (isVectorRendering())
1891 // no need to antialias vector drawings
1894 if (Cache.getDefault("ANTI_ALIAS", true))
1896 Graphics2D g2d = (Graphics2D) g;
1897 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1898 RenderingHints.VALUE_ANTIALIAS_ON);
1902 private void unsetAntialias(Graphics g)
1904 if (isVectorRendering())
1906 // no need to antialias vector drawings
1909 Graphics2D g2d = (Graphics2D) g;
1910 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1911 RenderingHints.VALUE_ANTIALIAS_OFF);
1914 public void setVectorRendering(boolean b)
1916 renderingVectors = b;
1919 public boolean isVectorRendering()
1921 return renderingVectors;