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.geom.AffineTransform;
32 import java.awt.image.ImageObserver;
33 import java.util.BitSet;
34 import java.util.Hashtable;
36 import jalview.analysis.AAFrequency;
37 import jalview.analysis.CodingUtils;
38 import jalview.analysis.Rna;
39 import jalview.analysis.StructureFrequency;
40 import jalview.api.AlignViewportI;
41 import jalview.bin.Cache;
42 import jalview.bin.Console;
43 import jalview.datamodel.AlignmentAnnotation;
44 import jalview.datamodel.Annotation;
45 import jalview.datamodel.ColumnSelection;
46 import jalview.datamodel.HiddenColumns;
47 import jalview.datamodel.ProfilesI;
48 import jalview.renderer.api.AnnotationRendererFactoryI;
49 import jalview.renderer.api.AnnotationRowRendererI;
50 import jalview.schemes.ColourSchemeI;
51 import jalview.schemes.NucleotideColourScheme;
52 import jalview.schemes.ResidueProperties;
53 import jalview.schemes.ZappoColourScheme;
54 import jalview.util.Platform;
56 public class AnnotationRenderer
58 private static final int UPPER_TO_LOWER = 'a' - 'A'; // 32
60 private static final int CHAR_A = 'A'; // 65
62 private static final int CHAR_Z = 'Z'; // 90
65 * flag indicating if timing and redraw parameter info should be output
67 private final boolean debugRedraw;
69 private int charWidth, endRes, charHeight;
71 private boolean validCharWidth, hasHiddenColumns;
73 private FontMetrics fm;
75 private final boolean USE_FILL_ROUND_RECT = Platform.isAMacAndNotJS();
77 boolean av_renderHistogram = true, av_renderProfile = true,
78 av_normaliseProfile = false;
80 ResidueShaderI profcolour = null;
82 private ColumnSelection columnSelection;
84 private HiddenColumns hiddenColumns;
86 private ProfilesI hconsensus;
88 private Hashtable<String, Object>[] complementConsensus;
90 private Hashtable<String, Object>[] hStrucConsensus;
92 private boolean av_ignoreGapsConsensus;
95 * attributes set from AwtRenderPanelI
98 * old image used when data is currently being calculated and cannot be
101 private Image fadedImage;
104 * panel being rendered into
106 private ImageObserver annotationPanel;
109 * width of image to render in panel
111 private int imgWidth;
114 * offset to beginning of visible area
119 * offset to end of visible area
121 private int visHeight;
124 * indicate if the renderer should only render the visible portion of the
125 * annotation given the current view settings
127 private boolean useClip = true;
130 * master flag indicating if renderer should ever try to clip. not enabled for
133 private boolean canClip = false;
135 public AnnotationRenderer()
141 * Create a new annotation Renderer
144 * flag indicating if timing and redraw parameter info should be
147 public AnnotationRenderer(boolean debugRedraw)
149 this.debugRedraw = debugRedraw;
153 * Remove any references and resources when this object is no longer required
155 public void dispose()
157 hiddenColumns = null;
159 complementConsensus = null;
160 hStrucConsensus = null;
162 annotationPanel = null;
163 rendererFactoryI = null;
166 void drawStemAnnot(Graphics g, Annotation[] row_annotations, int lastSSX,
167 int x, int y, int iconOffset, int startRes, int column,
168 boolean validRes, boolean validEnd)
170 g.setColor(STEM_COLOUR);
171 int sCol = (lastSSX / charWidth)
172 + hiddenColumns.visibleToAbsoluteColumn(startRes);
174 int x2 = (x * charWidth);
176 char dc = (column == 0 || row_annotations[column - 1] == null) ? ' '
177 : row_annotations[column - 1].secondaryStructure;
179 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
180 || dc != row_annotations[sCol - 1].secondaryStructure;
181 boolean diffdownstream = !validRes || !validEnd
182 || row_annotations[column] == null
183 || dc != row_annotations[column].secondaryStructure;
185 if (column > 0 && Rna.isClosingParenthesis(dc))
188 // if (validRes && column>1 && row_annotations[column-2]!=null &&
189 // dc.equals(row_annotations[column-2].displayCharacter))
192 * if new annotation with a closing base pair half of the stem,
193 * display a backward arrow
195 fillPolygon(g, new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
197 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
208 // display a forward arrow
212 * if annotation ending with an opeing base pair half of the stem,
213 * display a forward arrow
215 fillPolygon(g, new int[] { x2 - 5, x2 - 5, x2 },
217 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
227 fillRect(g, x1, y + 4 + iconOffset, x2 - x1, 7);
230 void drawNotCanonicalAnnot(Graphics g, Color nonCanColor,
231 Annotation[] row_annotations, int lastSSX, int x, int y,
232 int iconOffset, int startRes, int column, boolean validRes,
235 // Console.info(nonCanColor);
237 int sCol = (lastSSX / charWidth)
238 + hiddenColumns.visibleToAbsoluteColumn(startRes);
240 int x2 = (x * charWidth);
242 String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
243 : row_annotations[column - 1].displayCharacter;
245 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
246 || !dc.equals(row_annotations[sCol - 1].displayCharacter);
247 boolean diffdownstream = !validRes || !validEnd
248 || row_annotations[column] == null
249 || !dc.equals(row_annotations[column].displayCharacter);
250 // Console.info("Column "+column+" diff up:
252 // down:"+diffdownstream);
253 // If a closing base pair half of the stem, display a backward arrow
254 if (diffupstream || diffdownstream)
256 // draw glyphline under arrow
257 this.drawGlyphLine(g, row_annotations, lastSSX, x, y, iconOffset,
258 startRes, column, validRes, validEnd);
260 g.setColor(nonCanColor);
261 if (column > 0 && Rna.isClosingParenthesis(dc))
265 // if (validRes && column>1 && row_annotations[column-2]!=null &&
266 // dc.equals(row_annotations[column-2].displayCharacter))
268 fillPolygon(g, new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
270 { y + iconOffset + 1, y + 13 + iconOffset,
271 y + 7 + iconOffset },
283 // display a forward arrow
286 fillPolygon(g, new int[] { x2 - 6, x2 - 6, x2 - 1 },
288 { y + iconOffset + 1, y + 13 + iconOffset,
289 y + 7 + iconOffset },
300 fillRect(g, x1 - 1, y + 4 + iconOffset, x2 - x1 + 1, 6);
303 // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
305 public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
308 fm = annotPanel.getFontMetrics();
309 annotationPanel = annotPanel;
310 fadedImage = annotPanel.getFadedImage();
311 imgWidth = annotPanel.getFadedImageWidth();
312 // visible area for rendering
313 int[] bounds = annotPanel.getVisibleVRange();
317 visHeight = bounds[1];
332 rendererFactoryI = AnnotationRendererFactory.getRendererFactory();
333 updateFromAlignViewport(av);
336 public void updateFromAlignViewport(AlignViewportI av)
338 charWidth = av.getCharWidth();
339 endRes = av.getRanges().getEndRes();
340 charHeight = av.getCharHeight();
341 hasHiddenColumns = av.hasHiddenColumns();
342 validCharWidth = av.isValidCharWidth();
343 av_renderHistogram = av.isShowConsensusHistogram();
344 av_renderProfile = av.isShowSequenceLogo();
345 av_normaliseProfile = av.isNormaliseSequenceLogo();
346 profcolour = av.getResidueShading();
347 if (profcolour == null || profcolour.getColourScheme() == null)
350 * Use default colour for sequence logo if
351 * the alignment has no colourscheme set
352 * (would like to use user preference but n/a for applet)
354 ColourSchemeI col = av.getAlignment().isNucleotide()
355 ? new NucleotideColourScheme()
356 : new ZappoColourScheme();
357 profcolour = new ResidueShader(col);
359 columnSelection = av.getColumnSelection();
360 hiddenColumns = av.getAlignment().getHiddenColumns();
361 hconsensus = av.getSequenceConsensusHash();
362 complementConsensus = av.getComplementConsensusHash();
363 hStrucConsensus = av.getRnaStructureConsensusHash();
364 av_ignoreGapsConsensus = av.isIgnoreGapsConsensus();
368 * Returns profile data; the first element is the profile type, the second is
369 * the number of distinct values, the third the total count, and the remainder
370 * depend on the profile type.
376 int[] getProfileFor(AlignmentAnnotation aa, int column)
378 // TODO : consider refactoring the global alignment calculation
379 // properties/rendering attributes as a global 'alignment group' which holds
380 // all vis settings for the alignment as a whole rather than a subset
382 if (aa.autoCalculated && (aa.label.startsWith("Consensus")
383 || aa.label.startsWith("cDNA Consensus")))
385 boolean forComplement = aa.label.startsWith("cDNA Consensus");
386 if (aa.groupRef != null && aa.groupRef.consensusData != null
387 && aa.groupRef.isShowSequenceLogo())
389 // TODO? group consensus for cDNA complement
390 return AAFrequency.extractProfile(
391 aa.groupRef.consensusData.get(column),
392 aa.groupRef.getIgnoreGapsConsensus());
394 // TODO extend annotation row to enable dynamic and static profile data to
396 if (aa.groupRef == null && aa.sequenceRef == null)
400 return AAFrequency.extractCdnaProfile(complementConsensus[column],
401 av_ignoreGapsConsensus);
405 return AAFrequency.extractProfile(hconsensus.get(column),
406 av_ignoreGapsConsensus);
412 if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
414 // TODO implement group structure consensus
416 * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
417 * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
418 * group selections return StructureFrequency.extractProfile(
419 * aa.groupRef.consensusData[column], aa.groupRef
420 * .getIgnoreGapsConsensus()); }
422 // TODO extend annotation row to enable dynamic and static profile data
425 if (aa.groupRef == null && aa.sequenceRef == null
426 && hStrucConsensus != null
427 && hStrucConsensus.length > column)
429 return StructureFrequency.extractProfile(hStrucConsensus[column],
430 av_ignoreGapsConsensus);
439 private AnnotationRendererFactoryI rendererFactoryI;
442 * Render the annotation rows associated with an alignment.
447 * data and view settings to render
449 * destination for graphics
451 * row where a mouse event occured (or -1)
453 * first column that will be drawn
455 * last column that will be drawn
456 * @return true if the fadedImage was used for any alignment annotation rows
457 * currently being calculated
459 public boolean drawComponent(AwtRenderPanelI annotPanel,
460 AlignViewportI av, Graphics g, int activeRow, int startRes,
463 Graphics2D g2d = (Graphics2D) g;
465 if (Cache.getDefault("ANTI_ALIAS", true))
467 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
468 RenderingHints.VALUE_ANTIALIAS_ON);
469 g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
470 RenderingHints.VALUE_STROKE_PURE);
474 long stime = System.currentTimeMillis();
475 boolean usedFaded = false;
477 // AnnotationPanel needs to implement: ImageObserver, access to
479 updateFromAwtRenderPanel(annotPanel, av);
480 fm = g.getFontMetrics();
481 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
492 boolean validRes = false;
493 boolean validEnd = false;
494 boolean labelAllCols = false;
495 // boolean centreColLabels;
496 // boolean centreColLabelsDef = av.isCentreColumnLabels();
497 boolean scaleColLabel = false;
498 final AlignmentAnnotation consensusAnnot = av
499 .getAlignmentConsensusAnnotation();
500 final AlignmentAnnotation structConsensusAnnot = av
501 .getAlignmentStrucConsensusAnnotation();
502 final AlignmentAnnotation complementConsensusAnnot = av
503 .getComplementConsensusAnnotation();
505 BitSet graphGroupDrawn = new BitSet();
506 int charOffset = 0; // offset for a label
509 int yfrom = 0, f_i = 0, yto = 0, f_to = 0;
510 boolean clipst = false, clipend = false;
511 for (int i = 0; i < aa.length; i++)
513 AlignmentAnnotation row = aa[i];
514 boolean renderHistogram = true;
515 boolean renderProfile = false;
516 boolean normaliseProfile = false;
517 boolean isRNA = row.isRNA();
519 // check if this is a consensus annotation row and set the display
520 // settings appropriately
521 // TODO: generalise this to have render styles for consensus/profile
523 if (row.groupRef != null && row == row.groupRef.getConsensus())
525 renderHistogram = row.groupRef.isShowConsensusHistogram();
526 renderProfile = row.groupRef.isShowSequenceLogo();
527 normaliseProfile = row.groupRef.isNormaliseSequenceLogo();
529 else if (row == consensusAnnot || row == structConsensusAnnot
530 || row == complementConsensusAnnot)
532 renderHistogram = av_renderHistogram;
533 renderProfile = av_renderProfile;
534 normaliseProfile = av_normaliseProfile;
537 Annotation[] row_annotations = row.annotations;
542 // centreColLabels = row.centreColLabels || centreColLabelsDef;
543 labelAllCols = row.showAllColLabels;
544 scaleColLabel = row.scaleColLabel;
548 if (!useClip || ((y - charHeight) < visHeight
549 && (y + row.height + charHeight * 2) >= sOffset))
550 {// if_in_visible_region
561 if (row.graphGroup > -1 && graphGroupDrawn.get(row.graphGroup))
566 // this is so that we draw the characters below the graph
571 iconOffset = charHeight - fm.getDescent();
575 else if (row.hasText)
577 iconOffset = charHeight - fm.getDescent();
585 if (row.autoCalculated && av.isCalculationInProgress(row))
589 g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0,
590 y - row.height, imgWidth, y, annotationPanel);
591 g.setColor(Color.black);
592 // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
598 * else if (annotationPanel.av.updatingConservation &&
599 * aa[i].label.equals("Conservation")) {
601 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
602 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
603 * annotationPanel.imgWidth, y, annotationPanel);
605 * g.setColor(Color.black); //
606 * g.drawString("Calculating Conservation.....",20, y-row.height/2);
608 * continue; } else if (annotationPanel.av.updatingConservation &&
609 * aa[i].label.equals("Quality")) {
611 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
612 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
613 * annotationPanel.imgWidth, y, annotationPanel);
614 * g.setColor(Color.black); // /
615 * g.drawString("Calculating Quality....",20, y-row.height/2);
619 // first pass sets up state for drawing continuation from left-hand
622 x = (startRes == 0) ? 0 : -1;
623 while (x < endRes - startRes)
625 if (hasHiddenColumns)
627 column = hiddenColumns.visibleToAbsoluteColumn(startRes + x);
628 if (column > row_annotations.length - 1)
635 column = startRes + x;
638 if ((row_annotations == null)
639 || (row_annotations.length <= column)
640 || (row_annotations[column] == null))
648 final String displayChar = validRes
649 ? row_annotations[column].displayCharacter
656 g.setColor(Color.red);
658 if (columnSelection != null)
660 if (columnSelection.contains(column))
662 fillRect(g, x * charWidth, y, charWidth, charHeight);
666 if (row.getInvalidStrucPos() > x)
668 g.setColor(Color.orange);
669 fillRect(g, x * charWidth, y, charWidth, charHeight);
671 else if (row.getInvalidStrucPos() == x)
673 g.setColor(Color.orange.darker());
674 fillRect(g, x * charWidth, y, charWidth, charHeight);
676 if (validCharWidth && validRes && displayChar != null
677 && (displayChar.length() > 0))
679 // Graphics2D gg = (g);
680 float fmWidth = fm.charsWidth(displayChar.toCharArray(), 0,
681 displayChar.length());
684 * shrink label width to fit in column, if that is
685 * both configured and necessary
687 boolean scaledToFit = false;
688 float fmScaling = 1f;
689 if (scaleColLabel && fmWidth > charWidth)
692 fmScaling = charWidth;
693 fmScaling /= fmWidth;
694 // and update the label's width to reflect the scaling.
698 charOffset = (int) ((charWidth - fmWidth) / 2f);
700 if (row_annotations[column].colour == null)
702 g2d.setColor(Color.black);
706 g2d.setColor(row_annotations[column].colour);
710 * draw the label, unless it is the same secondary structure
711 * symbol (excluding RNA Helix) as the previous column
713 final int xPos = (x * charWidth) + charOffset;
714 final int yPos = y + iconOffset;
717 * translate to drawing position _before_ applying any scaling
719 g2d.translate(xPos, yPos);
723 * use a scaling transform to make the label narrower
724 * (JalviewJS doesn't have Font.deriveFont(AffineTransform))
727 AffineTransform.getScaleInstance(fmScaling, 1.0));
730 if (column == 0 || row.graph > 0)
732 g2d.drawString(displayChar, 0, 0);
734 else if (row_annotations[column - 1] == null || (labelAllCols
735 || !displayChar.equals(
736 row_annotations[column - 1].displayCharacter)
737 || (displayChar.length() < 2
738 && row_annotations[column].secondaryStructure == ' ')))
740 g2d.drawString(displayChar, 0, 0);
745 * undo scaling before translating back
746 * (restoring saved transform does NOT work in JS PDFGraphics!)
748 g2d.transform(AffineTransform
749 .getScaleInstance(1D / fmScaling, 1.0));
751 g2d.translate(-xPos, -yPos);
756 char ss = validRes ? row_annotations[column].secondaryStructure
761 // distinguish between forward/backward base-pairing
762 if (displayChar.indexOf(')') > -1)
771 if ((displayChar.indexOf(']') > -1))
779 // distinguish between forward/backward base-pairing
780 if (displayChar.indexOf('}') > -1)
788 // distinguish between forward/backward base-pairing
789 if (displayChar.indexOf('<') > -1)
795 if (isRNA && (ss >= CHAR_A) && (ss <= CHAR_Z))
797 // distinguish between forward/backward base-pairing
798 int ssLowerCase = ss + UPPER_TO_LOWER;
799 // TODO would .equals() be safer here? or charAt(0)?
800 if (displayChar.indexOf(ssLowerCase) > -1)
802 ss = (char) ssLowerCase;
806 if (!validRes || (ss != lastSS))
812 // int nb_annot = x - temp;
813 // Console.info("\t type :"+lastSS+"\t x
815 // annot :"+nb_annot);
818 case '(': // Stem case for RNA secondary structure
819 case ')': // and opposite direction
820 drawStemAnnot(g, row_annotations, lastSSX, x, y,
821 iconOffset, startRes, column, validRes, validEnd);
828 drawHelixAnnot(g, row_annotations, lastSSX, x, y,
829 iconOffset, startRes, column, validRes,
833 // no break if isRNA - falls through to drawNotCanonicalAnnot!
837 drawSheetAnnot(g, row_annotations, lastSSX, x, y,
838 iconOffset, startRes, column, validRes,
842 // no break if isRNA - fall through to drawNotCanonicalAnnot!
901 Color nonCanColor = getNotCanonicalColor(lastSS);
902 drawNotCanonicalAnnot(g, nonCanColor, row_annotations,
903 lastSSX, x, y, iconOffset, startRes, column,
909 g.setColor(GLYPHLINE_COLOR);
910 g.fillRect(lastSSX, y + 6 + iconOffset,
911 (x * charWidth) - lastSSX, 2);
912 g.drawRect(lastSSX, y + 6 + iconOffset,
913 (x * charWidth) - lastSSX - 1, 2);
928 lastSSX = (x * charWidth);
935 if (column >= row_annotations.length)
937 column = row_annotations.length - 1;
944 if ((row_annotations == null) || (row_annotations.length <= column)
945 || (row_annotations[column] == null))
963 drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
964 startRes, column, validRes, validEnd);
967 // no break if isRNA - fall through to drawNotCanonicalAnnot!
972 drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
973 startRes, column, validRes, validEnd);
976 // no break if isRNA - fall through to drawNotCanonicalAnnot!
979 case ')': // Stem case for RNA secondary structure
981 drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
982 startRes, column, validRes, validEnd);
1039 // Console.info(lastSS);
1040 Color nonCanColor = getNotCanonicalColor(lastSS);
1041 drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX,
1042 x, y, iconOffset, startRes, column, validRes, validEnd);
1045 drawGlyphLine(g, row_annotations, lastSSX, x, y, iconOffset,
1046 startRes, column, validRes, validEnd);
1051 if (row.graph > 0 && row.graphHeight > 0)
1053 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1055 if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
1057 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1058 // computed efficiently for all visible labels
1059 float groupmax = -999999, groupmin = 9999999;
1060 for (int gg = 0; gg < aa.length; gg++)
1062 if (aa[gg].graphGroup != row.graphGroup)
1069 aa[gg].visible = false;
1071 if (aa[gg].graphMax > groupmax)
1073 groupmax = aa[gg].graphMax;
1075 if (aa[gg].graphMin < groupmin)
1077 groupmin = aa[gg].graphMin;
1081 for (int gg = 0; gg < aa.length; gg++)
1083 if (aa[gg].graphGroup == row.graphGroup)
1085 drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
1086 endRes, y, groupmin, groupmax, row.graphHeight);
1090 graphGroupDrawn.set(row.graphGroup);
1094 drawLineGraph(g, row, row_annotations, startRes, endRes, y,
1095 row.graphMin, row.graphMax, row.graphHeight);
1098 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1100 drawBarGraph(g, row, row_annotations, startRes, endRes,
1101 row.graphMin, row.graphMax, y, renderHistogram,
1102 renderProfile, normaliseProfile);
1106 AnnotationRowRendererI renderer = rendererFactoryI
1107 .getRendererFor(row);
1108 if (renderer != null)
1110 renderer.renderRow(g, charWidth, charHeight, hasHiddenColumns,
1111 av, hiddenColumns, columnSelection, row,
1112 row_annotations, startRes, endRes, row.graphMin,
1117 if (renderer == null)
1120 .println("No renderer found for " + row.toString());
1125 "rendered with " + renderer.getClass().toString());
1134 if (clipst && !clipend)
1138 } // end if_in_visible_region
1139 if (row.graph > 0 && row.hasText)
1155 Console.warn("Start clip at : " + yfrom + " (index " + f_i + ")");
1159 Console.warn("End clip at : " + yto + " (index " + f_to + ")");
1163 Console.warn("Annotation Rendering time:"
1164 + (System.currentTimeMillis() - stime));
1171 public static final Color GLYPHLINE_COLOR = Color.gray;
1173 public static final Color SHEET_COLOUR = Color.green;
1175 public static final Color HELIX_COLOUR = Color.red;
1177 public static final Color STEM_COLOUR = Color.blue;
1179 // private Color sdNOTCANONICAL_COLOUR;
1181 void drawGlyphLine(Graphics g, Annotation[] row, int lastSSX, int x,
1182 int y, int iconOffset, int startRes, int column, boolean validRes,
1186 g.setColor(GLYPHLINE_COLOR);
1187 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
1188 g.drawRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX - 1,
1192 void drawSheetAnnot(Graphics g, Annotation[] row,
1194 int lastSSX, int x, int y, int iconOffset, int startRes,
1195 int column, boolean validRes, boolean validEnd)
1197 if (!validEnd || !validRes || row == null || row[column] == null
1198 || row[column].secondaryStructure != 'E')
1200 // draw the glyphline underneath
1201 drawGlyphLine(g, row, lastSSX, x, y, iconOffset, startRes, column,
1202 validRes, validEnd);
1203 g.setColor(SHEET_COLOUR);
1204 fillRect(g, lastSSX, y + 4 + iconOffset,
1205 (x * charWidth) - lastSSX - 4, 6);
1208 { (x * charWidth) - 6, (x * charWidth) - 6,
1209 (x * charWidth - 1) },
1211 { y + iconOffset + 1, y + 13 + iconOffset,
1212 y + 7 + iconOffset },
1217 g.setColor(SHEET_COLOUR);
1218 fillRect(g, lastSSX, y + 4 + iconOffset, x * charWidth - lastSSX, 6);
1222 void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x,
1223 int y, int iconOffset, int startRes, int column, boolean validRes,
1226 g.setColor(HELIX_COLOUR);
1228 int sCol = (lastSSX / charWidth)
1229 + hiddenColumns.visibleToAbsoluteColumn(startRes);
1231 int x2 = (x * charWidth);
1235 if (USE_FILL_ROUND_RECT)
1237 int ofs = charWidth / 2;
1238 // Off by 1 offset when drawing rects and ovals
1239 // to offscreen image on the MAC
1240 fillRoundRect(g, lastSSX, y + 4 + iconOffset, x2 - x1 - 1, 8, 8, 8);
1241 if (sCol == 0 || row[sCol - 1] == null
1242 || row[sCol - 1].secondaryStructure != 'H')
1247 // g.setColor(Color.orange);
1248 fillRoundRect(g, lastSSX, y + 4 + iconOffset, x2 - x1 - ofs, 8, 0,
1251 if (!validRes || row[column] == null
1252 || row[column].secondaryStructure != 'H')
1258 // g.setColor(Color.magenta);
1259 fillRoundRect(g, lastSSX + ofs, y + 4 + iconOffset, x2 - x1 - ofs,
1267 if (sCol == 0 || row[sCol - 1] == null
1268 || row[sCol - 1].secondaryStructure != 'H')
1270 fillArc(g, lastSSX, y + 4 + iconOffset, charWidth, 8, 90, 180);
1271 x1 += charWidth / 2;
1274 if (!validRes || row[column] == null
1275 || row[column].secondaryStructure != 'H')
1277 fillArc(g, (x * charWidth) - charWidth - 1, y + 4 + iconOffset,
1278 charWidth, 8, 270, 180);
1279 x2 -= charWidth / 2;
1282 fillRect(g, x1, y + 4 + iconOffset, x2 - x1, 8);
1285 void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1286 Annotation[] aa_annotations, int sRes, int eRes, int y, float min,
1287 float max, int graphHeight)
1289 if (sRes > aa_annotations.length)
1296 // Adjustment for fastpaint to left
1302 eRes = Math.min(eRes, aa_annotations.length);
1310 float range = max - min;
1315 y2 = y - (int) ((0 - min / range) * graphHeight);
1319 g.setColor(Color.gray);
1320 g.drawLine(x - charWidth, y2, (eRes - sRes + 1) * charWidth, y2);
1322 eRes = Math.min(eRes, aa_annotations.length);
1325 int aaMax = aa_annotations.length - 1;
1327 while (x < eRes - sRes)
1330 if (hasHiddenColumns)
1332 column = hiddenColumns.visibleToAbsoluteColumn(column);
1340 if (aa_annotations[column] == null)
1346 if (aa_annotations[column].colour == null)
1348 g.setColor(Color.black);
1352 g.setColor(aa_annotations[column].colour);
1355 if (aa_annotations[column - 1] == null
1356 && aa_annotations.length > column + 1
1357 && aa_annotations[column + 1] == null)
1360 y1 = y - (int) (((aa_annotations[column].value - min) / range)
1362 g.drawLine(x * charWidth + charWidth / 4, y1,
1363 x * charWidth + 3 * charWidth / 4, y1);
1368 if (aa_annotations[column - 1] == null)
1374 y1 = y - (int) (((aa_annotations[column - 1].value - min) / range)
1376 y2 = y - (int) (((aa_annotations[column].value - min) / range)
1379 g.drawLine(x * charWidth - charWidth / 2, y1,
1380 x * charWidth + charWidth / 2, y2);
1384 if (_aa.threshold != null)
1386 g.setColor(_aa.threshold.colour);
1387 Graphics2D g2 = (Graphics2D) g;
1388 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1389 BasicStroke.JOIN_ROUND, 3f, new float[]
1392 y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1393 g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1394 g2.setStroke(new BasicStroke());
1398 @SuppressWarnings("unused")
1399 void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1400 Annotation[] aa_annotations, int sRes, int eRes, float min,
1401 float max, int y, boolean renderHistogram, boolean renderProfile,
1402 boolean normaliseProfile)
1404 if (sRes > aa_annotations.length)
1408 Font ofont = g.getFont();
1409 eRes = Math.min(eRes, aa_annotations.length);
1411 int x = 0, y1 = y, y2 = y;
1413 float range = max - min;
1417 y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1420 g.setColor(Color.gray);
1422 g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
1425 int aaMax = aa_annotations.length - 1;
1426 while (x < eRes - sRes)
1429 if (hasHiddenColumns)
1431 column = hiddenColumns.visibleToAbsoluteColumn(column);
1439 if (aa_annotations[column] == null)
1444 if (aa_annotations[column].colour == null)
1446 g.setColor(Color.black);
1450 g.setColor(aa_annotations[column].colour);
1453 y1 = y - (int) (((aa_annotations[column].value - min) / (range))
1456 if (renderHistogram)
1460 fillRect(g, x * charWidth, y2, charWidth, y1 - y2);
1464 fillRect(g, x * charWidth, y1, charWidth, y2 - y1);
1467 // draw profile if available
1472 * {profile type, #values, total count, char1, pct1, char2, pct2...}
1474 int profl[] = getProfileFor(_aa, column);
1476 // just try to draw the logo if profl is not null
1477 if (profl != null && profl[2] != 0)
1479 boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
1480 boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
1481 float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1482 final double normaliseFactor = normaliseProfile ? _aa.graphHeight
1486 * Render a single base for a sequence profile, a base pair for
1487 * structure profile, and a triplet for a cdna profile
1489 char[] dc = new char[isStructureProfile ? 2
1490 : (isCdnaProfile ? 3 : 1)];
1492 // lm is not necessary - we can just use fm - could be off by no more
1494 // LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1495 // Console.info(asc + " " + dec + " " + (asc -
1497 // + " " + (dec - lm.getDescent()));
1499 double asc = fm.getAscent();
1500 double dec = fm.getDescent();
1501 double fht = fm.getHeight();
1503 double scale = 1f / (normaliseProfile ? profl[2] : 100f);
1504 // float ofontHeight = 1f / fm.getAscent();// magnify to fill box
1507 * Traverse the character(s)/percentage data in the array
1512 // profl[1] is the number of values in the profile
1513 for (int i = 0, c = 3, last = profl[1]; i < last; i++)
1517 if (isStructureProfile)
1519 // todo can we encode a structure pair as an int, like codons?
1520 dc[0] = (char) profl[c++];
1521 dc[1] = (char) profl[c++];
1524 else if (isCdnaProfile)
1526 CodingUtils.decodeCodon2(profl[c++], dc);
1531 dc[0] = (char) profl[c++];
1534 // next profl[] position is profile % for the character(s)
1536 int percent = profl[c++];
1539 // failsafe in case a count rounds down to 0%
1542 double newHeight = normaliseFactor * scale * percent;
1545 * Set character colour as per alignment colour scheme; use the
1546 * codon translation if a cDNA profile
1548 Color colour = null;
1551 final String codonTranslation = ResidueProperties
1553 colour = profcolour.findColour(codonTranslation.charAt(0),
1558 colour = profcolour.findColour(dc[0], column, null);
1560 g.setColor(colour == Color.white ? Color.lightGray : colour);
1562 // Debug - render boxes around characters
1563 // g.setColor(Color.red);
1564 // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
1566 // g.setColor(profcolour.findColour(dc[0]).darker());
1568 double sx = 1f * charWidth / fm.charsWidth(dc, 0, dc.length);
1569 double sy = newHeight / asc;
1570 double newAsc = asc * sy;
1571 double newDec = dec * sy;
1572 // it is not necessary to recalculate lm for the new font.
1573 // note: lm.getBaselineOffsets()[lm.getBaselineIndex()]) must be 0
1574 // by definition. Was:
1575 // int hght = (int) (ht + (newAsc - newDec);
1576 // - lm.getBaselineOffsets()[lm.getBaselineIndex()]));
1578 if (Platform.isJS())
1581 * SwingJS does not implement font.deriveFont()
1582 * so use a scaling transform to draw instead,
1583 * this is off by a very small amount
1585 final int hght = (int) (ht2 + (newAsc - newDec));
1586 Graphics2D gg = (Graphics2D) g;
1587 int xShift = (int) Math.round(x * charWidth / sx);
1588 int yShift = (int) Math.round(hght / sy);
1589 gg.transform(AffineTransform.getScaleInstance(sx, sy));
1590 gg.drawString(s, xShift, yShift);
1592 AffineTransform.getScaleInstance(1D / sx, 1D / sy));
1602 // Java ('normal') method is to scale the font to fit
1604 final int hght = (int) (ht + (newAsc - newDec));
1606 .deriveFont(AffineTransform.getScaleInstance(sx, sy));
1608 g.drawChars(dc, 0, dc.length, x * charWidth, hght);
1618 if (_aa.threshold != null)
1620 g.setColor(_aa.threshold.colour);
1621 Graphics2D g2 = (Graphics2D) g;
1622 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1623 BasicStroke.JOIN_ROUND, 3f, new float[]
1627 - ((_aa.threshold.value - min) / range) * _aa.graphHeight);
1628 g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1629 g2.setStroke(new BasicStroke());
1633 // used by overview window
1634 public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1635 Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1637 eRes = Math.min(eRes, aa_annotations.length);
1638 g.setColor(Color.white);
1639 fillRect(g, 0, 0, width, y);
1640 g.setColor(new Color(0, 0, 180));
1644 for (int j = sRes; j < eRes; j++)
1646 if (aa_annotations[j] != null)
1648 if (aa_annotations[j].colour == null)
1650 g.setColor(Color.black);
1654 g.setColor(aa_annotations[j].colour);
1657 height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1663 fillRect(g, x, y - height, charWidth, height);
1669 Color getNotCanonicalColor(char lastss)
1675 return new Color(255, 125, 5);
1679 return new Color(245, 115, 10);
1683 return new Color(235, 135, 15);
1687 return new Color(225, 105, 20);
1691 return new Color(215, 145, 30);
1695 return new Color(205, 95, 35);
1699 return new Color(195, 155, 45);
1703 return new Color(185, 85, 55);
1707 return new Color(175, 165, 65);
1711 return new Color(170, 75, 75);
1715 return new Color(160, 175, 85);
1719 return new Color(150, 65, 95);
1723 return new Color(140, 185, 105);
1727 return new Color(130, 55, 110);
1731 return new Color(120, 195, 120);
1735 return new Color(110, 45, 130);
1739 return new Color(100, 205, 140);
1743 return new Color(90, 35, 150);
1747 return new Color(85, 215, 160);
1751 return new Color(75, 25, 170);
1755 return new Color(65, 225, 180);
1759 return new Color(55, 15, 185);
1763 return new Color(45, 235, 195);
1767 return new Color(35, 5, 205);
1771 return new Color(25, 245, 215);
1775 return new Color(15, 0, 225);
1779 return new Color(10, 255, 235);
1783 return new Color(5, 150, 245);
1787 return new Color(0, 80, 255);
1790 Console.info("This is not a interaction : " + lastss);
1796 private static void fillPolygon(Graphics g, int[] xpoints, int[] ypoints,
1800 g.fillPolygon(xpoints, ypoints, n);
1802 g.fillPolygon(xpoints, ypoints, n);
1803 g.drawPolygon(xpoints, ypoints, n);
1806 private static void fillRect(Graphics g, int a, int b, int c, int d)
1808 g.fillRect(a, b, c, d);
1809 g.drawRect(a, b, c, d);
1812 private static void fillRoundRect(Graphics g, int a, int b, int c, int d,
1816 g.fillRoundRect(a, b, c, d, e, f);
1817 g.drawRoundRect(a, b, c, d, e, f);
1820 private static void fillArc(Graphics g, int a, int b, int c, int d, int e,
1824 g.fillArc(a, b, c, d, e, f);
1825 g.drawArc(a, b, c, d, e, f);
1828 private static void setAntialias(Graphics g)
1830 if (Cache.getDefault("ANTI_ALIAS", true))
1832 Graphics2D g2d = (Graphics2D) g;
1833 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1834 RenderingHints.VALUE_ANTIALIAS_ON);
1838 private static void unsetAntialias(Graphics g)
1840 Graphics2D g2d = (Graphics2D) g;
1841 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1842 RenderingHints.VALUE_ANTIALIAS_OFF);