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 ProfilesI hSSconsensus;
94 private Hashtable<String, Object>[] complementConsensus;
96 private Hashtable<String, Object>[] hStrucConsensus;
98 private boolean av_ignoreGapsConsensus;
100 private boolean renderingVectors = false;
102 private boolean glyphLineDrawn = false;
105 * attributes set from AwtRenderPanelI
108 * old image used when data is currently being calculated and cannot be
111 private Image fadedImage;
114 * panel being rendered into
116 private ImageObserver annotationPanel;
119 * width of image to render in panel
121 private int imgWidth;
124 * offset to beginning of visible area
129 * offset to end of visible area
131 private int visHeight;
134 * indicate if the renderer should only render the visible portion of the
135 * annotation given the current view settings
137 private boolean useClip = true;
140 * master flag indicating if renderer should ever try to clip. not enabled for
143 private boolean canClip = false;
145 public AnnotationRenderer()
151 * Create a new annotation Renderer
154 * flag indicating if timing and redraw parameter info should be
157 public AnnotationRenderer(boolean debugRedraw)
159 this.debugRedraw = debugRedraw;
163 * Remove any references and resources when this object is no longer required
165 public void dispose()
167 hiddenColumns = null;
170 complementConsensus = null;
171 hStrucConsensus = null;
173 annotationPanel = null;
174 rendererFactoryI = null;
177 void drawStemAnnot(Graphics g, Annotation[] row_annotations, int lastSSX,
178 int x, int y, int iconOffset, int startRes, int column,
179 boolean validRes, boolean validEnd)
181 int sCol = (lastSSX / charWidth)
182 + hiddenColumns.visibleToAbsoluteColumn(startRes);
184 int x2 = (x * charWidth);
186 char dc = (column == 0 || row_annotations[column - 1] == null) ? ' '
187 : row_annotations[column - 1].secondaryStructure;
189 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
190 || dc != row_annotations[sCol - 1].secondaryStructure
192 boolean diffdownstream = !validRes || !validEnd
193 || row_annotations[column] == null
194 || dc != row_annotations[column].secondaryStructure;
196 if (diffupstream || diffdownstream)
198 // draw glyphline under arrow
199 drawGlyphLine(g, lastSSX, x, y, iconOffset);
201 g.setColor(STEM_COLOUR);
203 if (column > 0 && Rna.isClosingParenthesis(dc))
206 // if (validRes && column>1 && row_annotations[column-2]!=null &&
207 // dc.equals(row_annotations[column-2].displayCharacter))
210 * if new annotation with a closing base pair half of the stem,
211 * display a backward arrow
213 fillPolygon(g, new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
215 { y + iconOffset + 1, y + 13 + iconOffset,
216 y + 7 + iconOffset },
227 // display a forward arrow
231 * if annotation ending with an opeing base pair half of the stem,
232 * display a forward arrow
234 fillPolygon(g, new int[] { x2 - 6, x2 - 6, x2 - 1 },
236 { y + iconOffset + 1, y + 13 + iconOffset,
237 y + 7 + iconOffset },
248 fillRect(g, x1, y + 4 + iconOffset, x2 - x1, 6);
251 void drawNotCanonicalAnnot(Graphics g, Color nonCanColor,
252 Annotation[] row_annotations, int lastSSX, int x, int y,
253 int iconOffset, int startRes, int column, boolean validRes,
256 // Console.info(nonCanColor);
258 int sCol = (lastSSX / charWidth)
259 + hiddenColumns.visibleToAbsoluteColumn(startRes);
261 int x2 = (x * charWidth);
263 String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
264 : row_annotations[column - 1].displayCharacter;
266 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
267 || !dc.equals(row_annotations[sCol - 1].displayCharacter)
269 boolean diffdownstream = !validRes || !validEnd
270 || row_annotations[column] == null
271 || !dc.equals(row_annotations[column].displayCharacter);
272 // Console.info("Column "+column+" diff up:
274 // down:"+diffdownstream);
275 // If a closing base pair half of the stem, display a backward arrow
276 if (diffupstream || diffdownstream)
278 // draw glyphline under arrow
279 drawGlyphLine(g, lastSSX, x, y, iconOffset);
281 g.setColor(nonCanColor);
282 if (column > 0 && Rna.isClosingParenthesis(dc))
286 // if (validRes && column>1 && row_annotations[column-2]!=null &&
287 // dc.equals(row_annotations[column-2].displayCharacter))
289 fillPolygon(g, new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
291 { y + iconOffset + 1, y + 13 + iconOffset,
292 y + 7 + iconOffset },
304 // display a forward arrow
307 fillPolygon(g, new int[] { x2 - 6, x2 - 6, x2 - 1 },
309 { y + iconOffset + 1, y + 13 + iconOffset,
310 y + 7 + iconOffset },
321 fillRect(g, x1, y + 4 + iconOffset, x2 - x1, 6);
324 // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
326 public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
329 fm = annotPanel.getFontMetrics();
330 annotationPanel = annotPanel;
331 fadedImage = annotPanel.getFadedImage();
332 imgWidth = annotPanel.getFadedImageWidth();
333 // visible area for rendering
334 int[] bounds = annotPanel.getVisibleVRange();
338 visHeight = bounds[1];
353 rendererFactoryI = AnnotationRendererFactory.getRendererFactory();
354 updateFromAlignViewport(av);
357 public void updateFromAlignViewport(AlignViewportI av)
359 charWidth = av.getCharWidth();
360 endRes = av.getRanges().getEndRes();
361 charHeight = av.getCharHeight();
362 hasHiddenColumns = av.hasHiddenColumns();
363 validCharWidth = av.isValidCharWidth();
364 av_renderHistogram = av.isShowConsensusHistogram();
365 av_renderProfile = av.isShowSequenceLogo();
366 av_normaliseProfile = av.isNormaliseSequenceLogo();
367 profcolour = av.getResidueShading();
368 if (profcolour == null || profcolour.getColourScheme() == null)
371 * Use default colour for sequence logo if
372 * the alignment has no colourscheme set
373 * (would like to use user preference but n/a for applet)
375 ColourSchemeI col = av.getAlignment().isNucleotide()
376 ? new NucleotideColourScheme()
377 : new ZappoColourScheme();
378 profcolour = new ResidueShader(col);
380 columnSelection = av.getColumnSelection();
381 hiddenColumns = av.getAlignment().getHiddenColumns();
382 hconsensus = av.getSequenceConsensusHash();
383 hSSconsensus = av.getSequenceSSConsensusHash();
384 complementConsensus = av.getComplementConsensusHash();
385 hStrucConsensus = av.getRnaStructureConsensusHash();
386 av_ignoreGapsConsensus = av.isIgnoreGapsConsensus();
390 * Returns profile data; the first element is the profile type, the second is
391 * the number of distinct values, the third the total count, and the remainder
392 * depend on the profile type.
398 int[] getProfileFor(AlignmentAnnotation aa, int column)
400 // TODO : consider refactoring the global alignment calculation
401 // properties/rendering attributes as a global 'alignment group' which holds
402 // all vis settings for the alignment as a whole rather than a subset
404 if (aa.autoCalculated && (aa.label.startsWith("Consensus")
405 || aa.label.startsWith("cDNA Consensus")))
407 boolean forComplement = aa.label.startsWith("cDNA Consensus");
408 if (aa.groupRef != null && aa.groupRef.consensusData != null
409 && aa.groupRef.isShowSequenceLogo())
411 // TODO? group consensus for cDNA complement
412 return AAFrequency.extractProfile(
413 aa.groupRef.consensusData.get(column),
414 aa.groupRef.getIgnoreGapsConsensus());
416 // TODO extend annotation row to enable dynamic and static profile data to
418 if (aa.groupRef == null && aa.sequenceRef == null)
422 return AAFrequency.extractCdnaProfile(complementConsensus[column],
423 av_ignoreGapsConsensus);
427 return AAFrequency.extractProfile(hconsensus.get(column),
428 av_ignoreGapsConsensus);
433 else if(aa.autoCalculated && aa.label.startsWith("SecondaryStructureConsensus"))
435 return AAFrequency.extractProfile(
436 hSSconsensus.get(column),
437 av_ignoreGapsConsensus);
443 if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
445 // TODO implement group structure consensus
447 * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
448 * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
449 * group selections return StructureFrequency.extractProfile(
450 * aa.groupRef.consensusData[column], aa.groupRef
451 * .getIgnoreGapsConsensus()); }
453 // TODO extend annotation row to enable dynamic and static profile data
456 if (aa.groupRef == null && aa.sequenceRef == null
457 && hStrucConsensus != null
458 && hStrucConsensus.length > column)
460 return StructureFrequency.extractProfile(hStrucConsensus[column],
461 av_ignoreGapsConsensus);
470 private AnnotationRendererFactoryI rendererFactoryI;
473 * Render the annotation rows associated with an alignment.
478 * data and view settings to render
480 * destination for graphics
482 * row where a mouse event occured (or -1)
484 * first column that will be drawn
486 * last column that will be drawn
487 * @return true if the fadedImage was used for any alignment annotation rows
488 * currently being calculated
490 public boolean drawComponent(AwtRenderPanelI annotPanel,
491 AlignViewportI av, Graphics g, int activeRow, int startRes,
494 if (g instanceof EpsGraphics2D || g instanceof SVGGraphics2D)
496 this.setVectorRendering(true);
498 Graphics2D g2d = (Graphics2D) g;
500 long stime = System.currentTimeMillis();
501 boolean usedFaded = false;
503 // AnnotationPanel needs to implement: ImageObserver, access to
505 updateFromAwtRenderPanel(annotPanel, av);
506 fm = g.getFontMetrics();
507 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
518 boolean validRes = false;
519 boolean validEnd = false;
520 boolean labelAllCols = false;
521 // boolean centreColLabels;
522 // boolean centreColLabelsDef = av.isCentreColumnLabels();
523 boolean scaleColLabel = false;
524 final AlignmentAnnotation consensusAnnot = av
525 .getAlignmentConsensusAnnotation();
526 final AlignmentAnnotation structConsensusAnnot = av
527 .getAlignmentStrucConsensusAnnotation();
528 final AlignmentAnnotation complementConsensusAnnot = av
529 .getComplementConsensusAnnotation();
530 final AlignmentAnnotation ssConsensusAnnot = av
531 .getAlignmentSecondaryStructureConsensusAnnotation();
533 BitSet graphGroupDrawn = new BitSet();
534 int charOffset = 0; // offset for a label
537 int yfrom = 0, f_i = 0, yto = 0, f_to = 0;
538 boolean clipst = false, clipend = false;
539 for (int i = 0; i < aa.length; i++)
541 AlignmentAnnotation row = aa[i];
542 boolean renderHistogram = true;
543 boolean renderProfile = false;
544 boolean normaliseProfile = false;
545 boolean isRNA = row.isRNA();
547 // check if this is a consensus annotation row and set the display
548 // settings appropriately
549 // TODO: generalise this to have render styles for consensus/profile
551 if (row.groupRef != null && row == row.groupRef.getConsensus())
553 renderHistogram = row.groupRef.isShowConsensusHistogram();
554 renderProfile = row.groupRef.isShowSequenceLogo();
555 normaliseProfile = row.groupRef.isNormaliseSequenceLogo();
557 else if (row == consensusAnnot || row == structConsensusAnnot
558 || row == complementConsensusAnnot || row == ssConsensusAnnot)
560 renderHistogram = av_renderHistogram;
561 renderProfile = av_renderProfile;
562 normaliseProfile = av_normaliseProfile;
565 Annotation[] row_annotations = row.annotations;
570 // centreColLabels = row.centreColLabels || centreColLabelsDef;
571 labelAllCols = row.showAllColLabels;
572 scaleColLabel = row.scaleColLabel;
576 if (!useClip || ((y - charHeight) < visHeight
577 && (y + row.height + charHeight * 2) >= sOffset))
578 {// if_in_visible_region
589 if (row.graphGroup > -1 && graphGroupDrawn.get(row.graphGroup))
594 // this is so that we draw the characters below the graph
599 iconOffset = charHeight - fm.getDescent();
603 else if (row.hasText)
605 iconOffset = charHeight - fm.getDescent();
613 if (row.autoCalculated && av.isCalculationInProgress(row))
617 g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0,
618 y - row.height, imgWidth, y, annotationPanel);
619 g.setColor(Color.black);
620 // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
626 * else if (annotationPanel.av.updatingConservation &&
627 * aa[i].label.equals("Conservation")) {
629 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
630 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
631 * annotationPanel.imgWidth, y, annotationPanel);
633 * g.setColor(Color.black); //
634 * g.drawString("Calculating Conservation.....",20, y-row.height/2);
636 * continue; } else if (annotationPanel.av.updatingConservation &&
637 * aa[i].label.equals("Quality")) {
639 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
640 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
641 * annotationPanel.imgWidth, y, annotationPanel);
642 * g.setColor(Color.black); // /
643 * g.drawString("Calculating Quality....",20, y-row.height/2);
648 // first pass sets up state for drawing continuation from left-hand
652 // flag used for vector rendition
653 this.glyphLineDrawn = false;
654 x = (startRes == 0) ? 0 : -1;
655 while (x < endRes - startRes)
657 if (hasHiddenColumns)
659 column = hiddenColumns.visibleToAbsoluteColumn(startRes + x);
660 if (column > row_annotations.length - 1)
667 column = startRes + x;
670 if ((row_annotations == null)
671 || (row_annotations.length <= column)
672 || (row_annotations[column] == null))
680 final String displayChar = validRes
681 ? row_annotations[column].displayCharacter
688 g.setColor(Color.red);
690 if (columnSelection != null)
692 if (columnSelection.contains(column))
694 fillRect(g, x * charWidth, y, charWidth, charHeight);
698 if (row.getInvalidStrucPos() > x)
700 g.setColor(Color.orange);
701 fillRect(g, x * charWidth, y, charWidth, charHeight);
703 else if (row.getInvalidStrucPos() == x)
705 g.setColor(Color.orange.darker());
706 fillRect(g, x * charWidth, y, charWidth, charHeight);
708 if (validCharWidth && validRes && displayChar != null
709 && (displayChar.length() > 0))
711 // Graphics2D gg = (g);
712 float fmWidth = fm.charsWidth(displayChar.toCharArray(), 0,
713 displayChar.length());
716 * shrink label width to fit in column, if that is
717 * both configured and necessary
719 boolean scaledToFit = false;
720 float fmScaling = 1f;
721 if (scaleColLabel && fmWidth > charWidth)
724 fmScaling = charWidth;
725 fmScaling /= fmWidth;
726 // and update the label's width to reflect the scaling.
730 charOffset = (int) ((charWidth - fmWidth) / 2f);
732 if (row_annotations[column].colour == null)
734 g2d.setColor(Color.black);
738 g2d.setColor(row_annotations[column].colour);
742 * draw the label, unless it is the same secondary structure
743 * symbol (excluding RNA Helix) as the previous column
745 final int xPos = (x * charWidth) + charOffset;
746 final int yPos = y + iconOffset;
749 * translate to drawing position _before_ applying any scaling
751 g2d.translate(xPos, yPos);
755 * use a scaling transform to make the label narrower
756 * (JalviewJS doesn't have Font.deriveFont(AffineTransform))
759 AffineTransform.getScaleInstance(fmScaling, 1.0));
762 if (column == 0 || row.graph > 0)
764 g2d.drawString(displayChar, 0, 0);
766 else if (row_annotations[column - 1] == null || (labelAllCols
767 || !displayChar.equals(
768 row_annotations[column - 1].displayCharacter)
769 || (displayChar.length() < 2
770 && row_annotations[column].secondaryStructure == ' ')))
772 g2d.drawString(displayChar, 0, 0);
777 * undo scaling before translating back
778 * (restoring saved transform does NOT work in JS PDFGraphics!)
780 g2d.transform(AffineTransform
781 .getScaleInstance(1D / fmScaling, 1.0));
783 g2d.translate(-xPos, -yPos);
788 char ss = validRes ? row_annotations[column].secondaryStructure
793 // distinguish between forward/backward base-pairing
794 if (displayChar.indexOf(')') > -1)
803 if ((displayChar.indexOf(']') > -1))
811 // distinguish between forward/backward base-pairing
812 if (displayChar.indexOf('}') > -1)
820 // distinguish between forward/backward base-pairing
821 if (displayChar.indexOf('<') > -1)
827 if (isRNA && (ss >= CHAR_A) && (ss <= CHAR_Z))
829 // distinguish between forward/backward base-pairing
830 int ssLowerCase = ss + UPPER_TO_LOWER;
831 // TODO would .equals() be safer here? or charAt(0)?
832 if (displayChar.indexOf(ssLowerCase) > -1)
834 ss = (char) ssLowerCase;
838 if (!validRes || (ss != lastSS))
844 // int nb_annot = x - temp;
845 // Console.info("\t type :"+lastSS+"\t x
847 // annot :"+nb_annot);
850 case '(': // Stem case for RNA secondary structure
851 case ')': // and opposite direction
852 drawStemAnnot(g, row_annotations, lastSSX, x, y,
853 iconOffset, startRes, column, validRes, validEnd);
860 drawHelixAnnot(g, row_annotations, lastSSX, x, y,
861 iconOffset, startRes, column, validRes,
865 // no break if isRNA - falls through to drawNotCanonicalAnnot!
869 drawSheetAnnot(g, row_annotations, lastSSX, x, y,
870 iconOffset, startRes, column, validRes,
874 // no break if isRNA - fall through to drawNotCanonicalAnnot!
933 Color nonCanColor = getNotCanonicalColor(lastSS);
934 drawNotCanonicalAnnot(g, nonCanColor, row_annotations,
935 lastSSX, x, y, iconOffset, startRes, column,
940 if (isVectorRendering())
942 // draw single full width glyphline
943 drawGlyphLine(g, lastSSX, endRes - x, y, iconOffset);
944 // disable more glyph lines
945 this.glyphLineDrawn = true;
949 drawGlyphLine(g, lastSSX, x, y, iconOffset);
964 lastSSX = (x * charWidth);
971 if (column >= row_annotations.length)
973 column = row_annotations.length - 1;
980 if ((row_annotations == null) || (row_annotations.length <= column)
981 || (row_annotations[column] == null))
999 drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
1000 startRes, column, validRes, validEnd);
1003 // no break if isRNA - fall through to drawNotCanonicalAnnot!
1008 drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
1009 startRes, column, validRes, validEnd);
1012 // no break if isRNA - fall through to drawNotCanonicalAnnot!
1015 case ')': // Stem case for RNA secondary structure
1017 drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
1018 startRes, column, validRes, validEnd);
1075 // Console.info(lastSS);
1076 Color nonCanColor = getNotCanonicalColor(lastSS);
1077 drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX,
1078 x, y, iconOffset, startRes, column, validRes, validEnd);
1081 if (isVectorRendering())
1083 // draw single full width glyphline
1084 drawGlyphLine(g, lastSSX, endRes - x, y, iconOffset);
1085 // disable more glyph lines
1086 this.glyphLineDrawn = true;
1090 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1096 if (row.graph > 0 && row.graphHeight > 0)
1098 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1100 if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
1102 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1103 // computed efficiently for all visible labels
1104 float groupmax = -999999, groupmin = 9999999;
1105 for (int gg = 0; gg < aa.length; gg++)
1107 if (aa[gg].graphGroup != row.graphGroup)
1114 aa[gg].visible = false;
1116 if (aa[gg].graphMax > groupmax)
1118 groupmax = aa[gg].graphMax;
1120 if (aa[gg].graphMin < groupmin)
1122 groupmin = aa[gg].graphMin;
1126 for (int gg = 0; gg < aa.length; gg++)
1128 if (aa[gg].graphGroup == row.graphGroup)
1130 drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
1131 endRes, y, groupmin, groupmax, row.graphHeight);
1135 graphGroupDrawn.set(row.graphGroup);
1139 drawLineGraph(g, row, row_annotations, startRes, endRes, y,
1140 row.graphMin, row.graphMax, row.graphHeight);
1143 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1145 drawBarGraph(g, row, row_annotations, startRes, endRes,
1146 row.graphMin, row.graphMax, y, renderHistogram,
1147 renderProfile, normaliseProfile);
1151 AnnotationRowRendererI renderer = rendererFactoryI
1152 .getRendererFor(row);
1153 if (renderer != null)
1155 renderer.renderRow(g, charWidth, charHeight, hasHiddenColumns,
1156 av, hiddenColumns, columnSelection, row,
1157 row_annotations, startRes, endRes, row.graphMin,
1162 if (renderer == null)
1165 .println("No renderer found for " + row.toString());
1170 "rendered with " + renderer.getClass().toString());
1179 if (clipst && !clipend)
1183 } // end if_in_visible_region
1184 if (row.graph > 0 && row.hasText)
1200 Console.warn("Start clip at : " + yfrom + " (index " + f_i + ")");
1204 Console.warn("End clip at : " + yto + " (index " + f_to + ")");
1208 Console.warn("Annotation Rendering time:"
1209 + (System.currentTimeMillis() - stime));
1216 public static final Color GLYPHLINE_COLOR = Color.gray;
1218 public static final Color SHEET_COLOUR = Color.green;
1220 public static final Color HELIX_COLOUR = Color.red;
1222 public static final Color STEM_COLOUR = Color.blue;
1224 // private Color sdNOTCANONICAL_COLOUR;
1226 void drawGlyphLine(Graphics g, int lastSSX, int x, int y, int iconOffset)
1230 // if we've drawn a single long glyphline for an export, don't draw the
1235 g.setColor(GLYPHLINE_COLOR);
1236 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
1239 void drawSheetAnnot(Graphics g, Annotation[] row,
1241 int lastSSX, int x, int y, int iconOffset, int startRes,
1242 int column, boolean validRes, boolean validEnd)
1244 if (!validEnd || !validRes || row == null || row[column] == null
1245 || row[column].secondaryStructure != 'E')
1247 // draw the glyphline underneath
1248 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1250 g.setColor(SHEET_COLOUR);
1251 fillRect(g, lastSSX, y + 4 + iconOffset,
1252 (x * charWidth) - lastSSX - 4, 6);
1255 { (x * charWidth) - 6, (x * charWidth) - 6,
1256 (x * charWidth - 1) },
1258 { y + iconOffset + 1, y + 13 + iconOffset,
1259 y + 7 + iconOffset },
1264 g.setColor(SHEET_COLOUR);
1265 fillRect(g, lastSSX, y + 4 + iconOffset, (x * charWidth) - lastSSX,
1270 void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x,
1271 int y, int iconOffset, int startRes, int column, boolean validRes,
1274 int sCol = (lastSSX / charWidth)
1275 + hiddenColumns.visibleToAbsoluteColumn(startRes);
1277 int x2 = (x * charWidth);
1279 if (USE_FILL_ROUND_RECT || isVectorRendering())
1281 // draw glyph line behind helix (visible in EPS or SVG output)
1282 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1284 g.setColor(HELIX_COLOUR);
1286 int ofs = charWidth / 2;
1287 // Off by 1 offset when drawing rects and ovals
1288 // to offscreen image on the MAC
1289 fillRoundRect(g, lastSSX, y + 3 + iconOffset, x2 - x1 - 1, 8, 8, 8);
1290 if (sCol == 0 || row[sCol - 1] == null
1291 || row[sCol - 1].secondaryStructure != 'H')
1296 fillRoundRect(g, lastSSX, y + 3 + iconOffset, x2 - x1 - ofs, 8, 0,
1299 if (!validRes || row[column] == null
1300 || row[column].secondaryStructure != 'H')
1306 fillRoundRect(g, lastSSX + ofs, y + 3 + iconOffset, x2 - x1 - ofs,
1313 boolean leftEnd = sCol == 0 || row[sCol - 1] == null
1314 || row[sCol - 1].secondaryStructure != 'H';
1315 boolean rightEnd = !validRes || row[column] == null
1316 || row[column].secondaryStructure != 'H';
1318 if (leftEnd || rightEnd)
1320 drawGlyphLine(g, lastSSX, x, y, iconOffset);
1322 g.setColor(HELIX_COLOUR);
1326 fillArc(g, lastSSX, y + 3 + iconOffset, charWidth, 8, 90, 180);
1327 x1 += charWidth / 2;
1332 fillArc(g, ((x - 1) * charWidth), y + 3 + iconOffset, charWidth, 8,
1334 x2 -= charWidth / 2;
1337 fillRect(g, x1, y + 3 + iconOffset, x2 - x1, 8);
1340 void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1341 Annotation[] aa_annotations, int sRes, int eRes, int y, float min,
1342 float max, int graphHeight)
1344 if (sRes > aa_annotations.length)
1348 Stroke roundStroke = new BasicStroke(1, BasicStroke.CAP_ROUND,
1349 BasicStroke.JOIN_ROUND);
1350 Stroke squareStroke = new BasicStroke(1, BasicStroke.CAP_SQUARE,
1351 BasicStroke.JOIN_MITER);
1352 Graphics2D g2d = (Graphics2D) g;
1353 Stroke prevStroke = g2d.getStroke();
1354 g2d.setStroke(roundStroke);
1358 // Adjustment for fastpaint to left
1364 eRes = Math.min(eRes, aa_annotations.length);
1372 float range = max - min;
1377 y2 = y - (int) ((0 - min / range) * graphHeight);
1380 g.setColor(Color.gray);
1381 drawLine(g, squareStroke, x * charWidth - charWidth, y2,
1382 (eRes - sRes) * charWidth, y2);
1384 eRes = Math.min(eRes, aa_annotations.length);
1387 int aaMax = aa_annotations.length - 1;
1389 while (x < eRes - sRes)
1392 if (hasHiddenColumns)
1394 column = hiddenColumns.visibleToAbsoluteColumn(column);
1402 if (aa_annotations[column] == null)
1408 if (aa_annotations[column].colour == null)
1410 g.setColor(Color.black);
1414 g.setColor(aa_annotations[column].colour);
1417 if (aa_annotations[column - 1] == null
1418 && aa_annotations.length > column + 1
1419 && aa_annotations[column + 1] == null)
1422 y1 = y - (int) (((aa_annotations[column].value - min) / range)
1424 drawLine(g, x * charWidth + charWidth / 4, y1,
1425 x * charWidth + 3 * charWidth / 4, y1);
1430 if (aa_annotations[column - 1] == null)
1436 y1 = y - (int) (((aa_annotations[column - 1].value - min) / range)
1438 y2 = y - (int) (((aa_annotations[column].value - min) / range)
1441 drawLine(g, (x - 1) * charWidth + charWidth / 2, y1,
1442 x * charWidth + charWidth / 2, y2);
1446 if (_aa.threshold != null)
1448 g.setColor(_aa.threshold.colour);
1449 Graphics2D g2 = (Graphics2D) g;
1450 Stroke s = new BasicStroke(1, BasicStroke.CAP_SQUARE,
1451 BasicStroke.JOIN_ROUND, 3f, new float[]
1454 y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1455 drawLine(g, s, 0, y2, (eRes - sRes) * charWidth, y2);
1457 g2d.setStroke(prevStroke);
1460 @SuppressWarnings("unused")
1461 void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1462 Annotation[] aa_annotations, int sRes, int eRes, float min,
1463 float max, int y, boolean renderHistogram, boolean renderProfile,
1464 boolean normaliseProfile)
1466 if (sRes > aa_annotations.length)
1470 Font ofont = g.getFont();
1471 eRes = Math.min(eRes, aa_annotations.length);
1473 int x = 0, y1 = y, y2 = y;
1475 float range = max - min;
1479 y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1482 g.setColor(Color.gray);
1484 drawLine(g, x, y2, (eRes - sRes) * charWidth, y2);
1487 int aaMax = aa_annotations.length - 1;
1488 while (x < eRes - sRes)
1491 if (hasHiddenColumns)
1493 column = hiddenColumns.visibleToAbsoluteColumn(column);
1501 if (aa_annotations[column] == null)
1506 if (aa_annotations[column].colour == null)
1508 g.setColor(Color.black);
1512 g.setColor(aa_annotations[column].colour);
1515 y1 = y - (int) (((aa_annotations[column].value - min) / (range))
1518 if (renderHistogram)
1522 fillRect(g, x * charWidth, y2, charWidth, y1 - y2);
1526 fillRect(g, x * charWidth, y1, charWidth, y2 - y1);
1529 // draw profile if available
1534 * {profile type, #values, total count, char1, pct1, char2, pct2...}
1536 int profl[] = getProfileFor(_aa, column);
1540 // just try to draw the logo if profl is not null
1541 if (profl != null && profl[2] != 0)
1543 boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
1544 boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
1545 float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1546 final double normaliseFactor = normaliseProfile ? _aa.graphHeight
1550 * Render a single base for a sequence profile, a base pair for
1551 * structure profile, and a triplet for a cdna profile
1553 char[] dc = new char[isStructureProfile ? 2
1554 : (isCdnaProfile ? 3 : 1)];
1556 // lm is not necessary - we can just use fm - could be off by no more
1558 // LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1559 // Console.info(asc + " " + dec + " " + (asc -
1561 // + " " + (dec - lm.getDescent()));
1563 double asc = fm.getAscent();
1564 double dec = fm.getDescent();
1565 double fht = fm.getHeight();
1567 double scale = 1f / (normaliseProfile ? profl[2] : 100f);
1568 // float ofontHeight = 1f / fm.getAscent();// magnify to fill box
1571 * Traverse the character(s)/percentage data in the array
1576 // profl[1] is the number of values in the profile
1577 for (int i = 0, c = 3, last = profl[1]; i < last; i++)
1581 if (isStructureProfile)
1583 // todo can we encode a structure pair as an int, like codons?
1584 dc[0] = (char) profl[c++];
1585 dc[1] = (char) profl[c++];
1588 else if (isCdnaProfile)
1590 CodingUtils.decodeCodon2(profl[c++], dc);
1595 dc[0] = (char) profl[c++];
1598 // next profl[] position is profile % for the character(s)
1600 int percent = profl[c++];
1603 // failsafe in case a count rounds down to 0%
1606 double newHeight = normaliseFactor * scale * percent;
1609 * Set character colour as per alignment colour scheme; use the
1610 * codon translation if a cDNA profile
1612 Color colour = null;
1615 final String codonTranslation = ResidueProperties
1617 colour = profcolour.findColour(codonTranslation.charAt(0),
1623 colour = profcolour.findColour(dc[0], column, null);
1626 g.setColor(colour == Color.white ? Color.lightGray : colour);
1628 // Debug - render boxes around characters
1629 // g.setColor(Color.red);
1630 // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
1632 // g.setColor(profcolour.findColour(dc[0]).darker());
1634 double sx = 1f * charWidth / fm.charsWidth(dc, 0, dc.length);
1635 double sy = newHeight / asc;
1636 double newAsc = asc * sy;
1637 double newDec = dec * sy;
1638 // it is not necessary to recalculate lm for the new font.
1639 // note: lm.getBaselineOffsets()[lm.getBaselineIndex()]) must be 0
1640 // by definition. Was:
1641 // int hght = (int) (ht + (newAsc - newDec);
1642 // - lm.getBaselineOffsets()[lm.getBaselineIndex()]));
1644 if (Platform.isJS())
1647 * SwingJS does not implement font.deriveFont()
1648 * so use a scaling transform to draw instead,
1649 * this is off by a very small amount
1651 final int hght = (int) (ht2 + (newAsc - newDec));
1652 Graphics2D gg = (Graphics2D) g;
1653 int xShift = (int) Math.round(x * charWidth / sx);
1654 int yShift = (int) Math.round(hght / sy);
1655 gg.transform(AffineTransform.getScaleInstance(sx, sy));
1656 gg.drawString(s, xShift, yShift);
1658 AffineTransform.getScaleInstance(1D / sx, 1D / sy));
1668 // Java ('normal') method is to scale the font to fit
1670 final int hght = (int) (ht + (newAsc - newDec));
1672 .deriveFont(AffineTransform.getScaleInstance(sx, sy));
1674 g.drawChars(dc, 0, dc.length, x * charWidth, hght);
1684 if (_aa.threshold != null)
1686 g.setColor(_aa.threshold.colour);
1687 Stroke s = new BasicStroke(1, BasicStroke.CAP_SQUARE,
1688 BasicStroke.JOIN_ROUND, 3f, new float[]
1692 - ((_aa.threshold.value - min) / range) * _aa.graphHeight);
1693 drawLine(g, s, 0, y2, (eRes - sRes) * charWidth, y2);
1697 // used by overview window
1698 public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1699 Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1701 eRes = Math.min(eRes, aa_annotations.length);
1702 g.setColor(Color.white);
1703 fillRect(g, 0, 0, width, y);
1704 g.setColor(new Color(0, 0, 180));
1708 for (int j = sRes; j < eRes; j++)
1710 if (aa_annotations[j] != null)
1712 if (aa_annotations[j].colour == null)
1714 g.setColor(Color.black);
1718 g.setColor(aa_annotations[j].colour);
1721 height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1727 fillRect(g, x, y - height, charWidth, height);
1733 Color getNotCanonicalColor(char lastss)
1739 return new Color(255, 125, 5);
1743 return new Color(245, 115, 10);
1747 return new Color(235, 135, 15);
1751 return new Color(225, 105, 20);
1755 return new Color(215, 145, 30);
1759 return new Color(205, 95, 35);
1763 return new Color(195, 155, 45);
1767 return new Color(185, 85, 55);
1771 return new Color(175, 165, 65);
1775 return new Color(170, 75, 75);
1779 return new Color(160, 175, 85);
1783 return new Color(150, 65, 95);
1787 return new Color(140, 185, 105);
1791 return new Color(130, 55, 110);
1795 return new Color(120, 195, 120);
1799 return new Color(110, 45, 130);
1803 return new Color(100, 205, 140);
1807 return new Color(90, 35, 150);
1811 return new Color(85, 215, 160);
1815 return new Color(75, 25, 170);
1819 return new Color(65, 225, 180);
1823 return new Color(55, 15, 185);
1827 return new Color(45, 235, 195);
1831 return new Color(35, 5, 205);
1835 return new Color(25, 245, 215);
1839 return new Color(15, 0, 225);
1843 return new Color(10, 255, 235);
1847 return new Color(5, 150, 245);
1851 return new Color(0, 80, 255);
1854 Console.info("This is not a interaction : " + lastss);
1860 private void fillPolygon(Graphics g, int[] xpoints, int[] ypoints, int n)
1863 g.fillPolygon(xpoints, ypoints, n);
1867 private void fillRect(Graphics g, int a, int b, int c, int d)
1869 fillRect(g, false, a, b, c, d);
1872 private void fillRect(Graphics g, int a, int b, int c, int d)
1875 g.fillRect(a, b, c, d);
1878 private void fillRoundRect(Graphics g, int a, int b, int c, int d, int e,
1882 g.fillRoundRect(a, b, c, d, e, f);
1885 private void fillArc(Graphics g, int a, int b, int c, int d, int e, int f)
1888 g.fillArc(a, b, c, d, e, f);
1891 private void drawLine(Graphics g, Stroke s, int a, int b, int c, int d)
1893 Graphics2D g2d = (Graphics2D) g;
1894 Stroke p = g2d.getStroke();
1896 drawLine(g, a, b, c, d);
1900 private void drawLine(Graphics g, int a, int b, int c, int d)
1903 g.drawLine(a, b, c, d);
1906 private void setAntialias(Graphics g)
1908 if (isVectorRendering())
1910 // no need to antialias vector drawings
1913 if (Cache.getDefault("ANTI_ALIAS", true))
1915 Graphics2D g2d = (Graphics2D) g;
1916 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1917 RenderingHints.VALUE_ANTIALIAS_ON);
1921 private void unsetAntialias(Graphics g)
1923 if (isVectorRendering())
1925 // no need to antialias vector drawings
1928 Graphics2D g2d = (Graphics2D) g;
1929 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1930 RenderingHints.VALUE_ANTIALIAS_OFF);
1933 public void setVectorRendering(boolean b)
1935 renderingVectors = b;
1938 public boolean isVectorRendering()
1940 return renderingVectors;