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 g.setColor(nonCanColor);
238 int sCol = (lastSSX / charWidth)
239 + hiddenColumns.visibleToAbsoluteColumn(startRes);
241 int x2 = (x * charWidth);
243 String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
244 : row_annotations[column - 1].displayCharacter;
246 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
247 || !dc.equals(row_annotations[sCol - 1].displayCharacter);
248 boolean diffdownstream = !validRes || !validEnd
249 || row_annotations[column] == null
250 || !dc.equals(row_annotations[column].displayCharacter);
251 // Console.info("Column "+column+" diff up:
253 // down:"+diffdownstream);
254 // If a closing base pair half of the stem, display a backward arrow
255 if (column > 0 && Rna.isClosingParenthesis(dc))
259 // if (validRes && column>1 && row_annotations[column-2]!=null &&
260 // dc.equals(row_annotations[column-2].displayCharacter))
262 fillPolygon(g, new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
264 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
276 // display a forward arrow
279 fillPolygon(g, new int[] { x2 - 5, x2 - 5, x2 },
281 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
291 fillRect(g, x1, y + 4 + iconOffset, x2 - x1, 7);
294 // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
296 public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
299 fm = annotPanel.getFontMetrics();
300 annotationPanel = annotPanel;
301 fadedImage = annotPanel.getFadedImage();
302 imgWidth = annotPanel.getFadedImageWidth();
303 // visible area for rendering
304 int[] bounds = annotPanel.getVisibleVRange();
308 visHeight = bounds[1];
323 rendererFactoryI = AnnotationRendererFactory.getRendererFactory();
324 updateFromAlignViewport(av);
327 public void updateFromAlignViewport(AlignViewportI av)
329 charWidth = av.getCharWidth();
330 endRes = av.getRanges().getEndRes();
331 charHeight = av.getCharHeight();
332 hasHiddenColumns = av.hasHiddenColumns();
333 validCharWidth = av.isValidCharWidth();
334 av_renderHistogram = av.isShowConsensusHistogram();
335 av_renderProfile = av.isShowSequenceLogo();
336 av_normaliseProfile = av.isNormaliseSequenceLogo();
337 profcolour = av.getResidueShading();
338 if (profcolour == null || profcolour.getColourScheme() == null)
341 * Use default colour for sequence logo if
342 * the alignment has no colourscheme set
343 * (would like to use user preference but n/a for applet)
345 ColourSchemeI col = av.getAlignment().isNucleotide()
346 ? new NucleotideColourScheme()
347 : new ZappoColourScheme();
348 profcolour = new ResidueShader(col);
350 columnSelection = av.getColumnSelection();
351 hiddenColumns = av.getAlignment().getHiddenColumns();
352 hconsensus = av.getSequenceConsensusHash();
353 complementConsensus = av.getComplementConsensusHash();
354 hStrucConsensus = av.getRnaStructureConsensusHash();
355 av_ignoreGapsConsensus = av.isIgnoreGapsConsensus();
359 * Returns profile data; the first element is the profile type, the second is
360 * the number of distinct values, the third the total count, and the remainder
361 * depend on the profile type.
367 int[] getProfileFor(AlignmentAnnotation aa, int column)
369 // TODO : consider refactoring the global alignment calculation
370 // properties/rendering attributes as a global 'alignment group' which holds
371 // all vis settings for the alignment as a whole rather than a subset
373 if (aa.autoCalculated && (aa.label.startsWith("Consensus")
374 || aa.label.startsWith("cDNA Consensus")))
376 boolean forComplement = aa.label.startsWith("cDNA Consensus");
377 if (aa.groupRef != null && aa.groupRef.consensusData != null
378 && aa.groupRef.isShowSequenceLogo())
380 // TODO? group consensus for cDNA complement
381 return AAFrequency.extractProfile(
382 aa.groupRef.consensusData.get(column),
383 aa.groupRef.getIgnoreGapsConsensus());
385 // TODO extend annotation row to enable dynamic and static profile data to
387 if (aa.groupRef == null && aa.sequenceRef == null)
391 return AAFrequency.extractCdnaProfile(complementConsensus[column],
392 av_ignoreGapsConsensus);
396 return AAFrequency.extractProfile(hconsensus.get(column),
397 av_ignoreGapsConsensus);
403 if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
405 // TODO implement group structure consensus
407 * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
408 * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
409 * group selections return StructureFrequency.extractProfile(
410 * aa.groupRef.consensusData[column], aa.groupRef
411 * .getIgnoreGapsConsensus()); }
413 // TODO extend annotation row to enable dynamic and static profile data
416 if (aa.groupRef == null && aa.sequenceRef == null
417 && hStrucConsensus != null
418 && hStrucConsensus.length > column)
420 return StructureFrequency.extractProfile(hStrucConsensus[column],
421 av_ignoreGapsConsensus);
430 private AnnotationRendererFactoryI rendererFactoryI;
433 * Render the annotation rows associated with an alignment.
438 * data and view settings to render
440 * destination for graphics
442 * row where a mouse event occured (or -1)
444 * first column that will be drawn
446 * last column that will be drawn
447 * @return true if the fadedImage was used for any alignment annotation rows
448 * currently being calculated
450 public boolean drawComponent(AwtRenderPanelI annotPanel,
451 AlignViewportI av, Graphics g, int activeRow, int startRes,
454 Graphics2D g2d = (Graphics2D) g;
455 if (Cache.getDefault("ANTI_ALIAS", true))
457 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
458 RenderingHints.VALUE_ANTIALIAS_ON);
459 g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
460 RenderingHints.VALUE_STROKE_PURE);
463 long stime = System.currentTimeMillis();
464 boolean usedFaded = false;
466 // AnnotationPanel needs to implement: ImageObserver, access to
468 updateFromAwtRenderPanel(annotPanel, av);
469 fm = g.getFontMetrics();
470 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
481 boolean validRes = false;
482 boolean validEnd = false;
483 boolean labelAllCols = false;
484 // boolean centreColLabels;
485 // boolean centreColLabelsDef = av.isCentreColumnLabels();
486 boolean scaleColLabel = false;
487 final AlignmentAnnotation consensusAnnot = av
488 .getAlignmentConsensusAnnotation();
489 final AlignmentAnnotation structConsensusAnnot = av
490 .getAlignmentStrucConsensusAnnotation();
491 final AlignmentAnnotation complementConsensusAnnot = av
492 .getComplementConsensusAnnotation();
494 BitSet graphGroupDrawn = new BitSet();
495 int charOffset = 0; // offset for a label
498 int yfrom = 0, f_i = 0, yto = 0, f_to = 0;
499 boolean clipst = false, clipend = false;
500 for (int i = 0; i < aa.length; i++)
502 AlignmentAnnotation row = aa[i];
503 boolean renderHistogram = true;
504 boolean renderProfile = false;
505 boolean normaliseProfile = false;
506 boolean isRNA = row.isRNA();
508 // check if this is a consensus annotation row and set the display
509 // settings appropriately
510 // TODO: generalise this to have render styles for consensus/profile
512 if (row.groupRef != null && row == row.groupRef.getConsensus())
514 renderHistogram = row.groupRef.isShowConsensusHistogram();
515 renderProfile = row.groupRef.isShowSequenceLogo();
516 normaliseProfile = row.groupRef.isNormaliseSequenceLogo();
518 else if (row == consensusAnnot || row == structConsensusAnnot
519 || row == complementConsensusAnnot)
521 renderHistogram = av_renderHistogram;
522 renderProfile = av_renderProfile;
523 normaliseProfile = av_normaliseProfile;
526 Annotation[] row_annotations = row.annotations;
531 // centreColLabels = row.centreColLabels || centreColLabelsDef;
532 labelAllCols = row.showAllColLabels;
533 scaleColLabel = row.scaleColLabel;
537 if (!useClip || ((y - charHeight) < visHeight
538 && (y + row.height + charHeight * 2) >= sOffset))
539 {// if_in_visible_region
550 if (row.graphGroup > -1 && graphGroupDrawn.get(row.graphGroup))
555 // this is so that we draw the characters below the graph
560 iconOffset = charHeight - fm.getDescent();
564 else if (row.hasText)
566 iconOffset = charHeight - fm.getDescent();
574 if (row.autoCalculated && av.isCalculationInProgress(row))
578 g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0,
579 y - row.height, imgWidth, y, annotationPanel);
580 g.setColor(Color.black);
581 // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
587 * else if (annotationPanel.av.updatingConservation &&
588 * aa[i].label.equals("Conservation")) {
590 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
591 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
592 * annotationPanel.imgWidth, y, annotationPanel);
594 * g.setColor(Color.black); //
595 * g.drawString("Calculating Conservation.....",20, y-row.height/2);
597 * continue; } else if (annotationPanel.av.updatingConservation &&
598 * aa[i].label.equals("Quality")) {
600 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
601 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
602 * annotationPanel.imgWidth, y, annotationPanel);
603 * g.setColor(Color.black); // /
604 * g.drawString("Calculating Quality....",20, y-row.height/2);
608 // first pass sets up state for drawing continuation from left-hand
611 x = (startRes == 0) ? 0 : -1;
612 while (x < endRes - startRes)
614 if (hasHiddenColumns)
616 column = hiddenColumns.visibleToAbsoluteColumn(startRes + x);
617 if (column > row_annotations.length - 1)
624 column = startRes + x;
627 if ((row_annotations == null)
628 || (row_annotations.length <= column)
629 || (row_annotations[column] == null))
637 final String displayChar = validRes
638 ? row_annotations[column].displayCharacter
644 g.setColor(Color.red);
646 if (columnSelection != null)
648 if (columnSelection.contains(column))
650 fillRect(g, x * charWidth, y, charWidth, charHeight);
654 if (row.getInvalidStrucPos() > x)
656 g.setColor(Color.orange);
657 fillRect(g, x * charWidth, y, charWidth, charHeight);
659 else if (row.getInvalidStrucPos() == x)
661 g.setColor(Color.orange.darker());
662 fillRect(g, x * charWidth, y, charWidth, charHeight);
664 if (validCharWidth && validRes && displayChar != null
665 && (displayChar.length() > 0))
667 // Graphics2D gg = (g);
668 float fmWidth = fm.charsWidth(displayChar.toCharArray(), 0,
669 displayChar.length());
672 * shrink label width to fit in column, if that is
673 * both configured and necessary
675 boolean scaledToFit = false;
676 float fmScaling = 1f;
677 if (scaleColLabel && fmWidth > charWidth)
680 fmScaling = charWidth;
681 fmScaling /= fmWidth;
682 // and update the label's width to reflect the scaling.
686 charOffset = (int) ((charWidth - fmWidth) / 2f);
688 if (row_annotations[column].colour == null)
690 g2d.setColor(Color.black);
694 g2d.setColor(row_annotations[column].colour);
698 * draw the label, unless it is the same secondary structure
699 * symbol (excluding RNA Helix) as the previous column
701 final int xPos = (x * charWidth) + charOffset;
702 final int yPos = y + iconOffset;
705 * translate to drawing position _before_ applying any scaling
707 g2d.translate(xPos, yPos);
711 * use a scaling transform to make the label narrower
712 * (JalviewJS doesn't have Font.deriveFont(AffineTransform))
715 AffineTransform.getScaleInstance(fmScaling, 1.0));
717 if (column == 0 || row.graph > 0)
719 g2d.drawString(displayChar, 0, 0);
721 else if (row_annotations[column - 1] == null || (labelAllCols
722 || !displayChar.equals(
723 row_annotations[column - 1].displayCharacter)
724 || (displayChar.length() < 2
725 && row_annotations[column].secondaryStructure == ' ')))
727 g2d.drawString(displayChar, 0, 0);
732 * undo scaling before translating back
733 * (restoring saved transform does NOT work in JS PDFGraphics!)
735 g2d.transform(AffineTransform
736 .getScaleInstance(1D / fmScaling, 1.0));
738 g2d.translate(-xPos, -yPos);
743 char ss = validRes ? row_annotations[column].secondaryStructure
748 // distinguish between forward/backward base-pairing
749 if (displayChar.indexOf(')') > -1)
758 if ((displayChar.indexOf(']') > -1))
766 // distinguish between forward/backward base-pairing
767 if (displayChar.indexOf('}') > -1)
775 // distinguish between forward/backward base-pairing
776 if (displayChar.indexOf('<') > -1)
782 if (isRNA && (ss >= CHAR_A) && (ss <= CHAR_Z))
784 // distinguish between forward/backward base-pairing
785 int ssLowerCase = ss + UPPER_TO_LOWER;
786 // TODO would .equals() be safer here? or charAt(0)?
787 if (displayChar.indexOf(ssLowerCase) > -1)
789 ss = (char) ssLowerCase;
793 if (!validRes || (ss != lastSS))
799 // int nb_annot = x - temp;
800 // Console.info("\t type :"+lastSS+"\t x
802 // annot :"+nb_annot);
805 case '(': // Stem case for RNA secondary structure
806 case ')': // and opposite direction
807 drawStemAnnot(g, row_annotations, lastSSX, x, y,
808 iconOffset, startRes, column, validRes, validEnd);
815 drawHelixAnnot(g, row_annotations, lastSSX, x, y,
816 iconOffset, startRes, column, validRes,
820 // no break if isRNA - falls through to drawNotCanonicalAnnot!
824 drawSheetAnnot(g, row_annotations, lastSSX, x, y,
825 iconOffset, startRes, column, validRes,
829 // no break if isRNA - fall through to drawNotCanonicalAnnot!
888 Color nonCanColor = getNotCanonicalColor(lastSS);
889 drawNotCanonicalAnnot(g, nonCanColor, row_annotations,
890 lastSSX, x, y, iconOffset, startRes, column,
895 g.setColor(GLYPHLINE_COLOR);
896 g.fillRect(lastSSX, y + 6 + iconOffset,
897 (x * charWidth) - lastSSX, 2);
912 lastSSX = (x * charWidth);
919 if (column >= row_annotations.length)
921 column = row_annotations.length - 1;
928 if ((row_annotations == null) || (row_annotations.length <= column)
929 || (row_annotations[column] == null))
947 drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
948 startRes, column, validRes, validEnd);
951 // no break if isRNA - fall through to drawNotCanonicalAnnot!
956 drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
957 startRes, column, validRes, validEnd);
960 // no break if isRNA - fall through to drawNotCanonicalAnnot!
963 case ')': // Stem case for RNA secondary structure
965 drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
966 startRes, column, validRes, validEnd);
1023 // Console.info(lastSS);
1024 Color nonCanColor = getNotCanonicalColor(lastSS);
1025 drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX,
1026 x, y, iconOffset, startRes, column, validRes, validEnd);
1029 drawGlyphLine(g, row_annotations, lastSSX, x, y, iconOffset,
1030 startRes, column, validRes, validEnd);
1035 if (row.graph > 0 && row.graphHeight > 0)
1037 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1039 if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
1041 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1042 // computed efficiently for all visible labels
1043 float groupmax = -999999, groupmin = 9999999;
1044 for (int gg = 0; gg < aa.length; gg++)
1046 if (aa[gg].graphGroup != row.graphGroup)
1053 aa[gg].visible = false;
1055 if (aa[gg].graphMax > groupmax)
1057 groupmax = aa[gg].graphMax;
1059 if (aa[gg].graphMin < groupmin)
1061 groupmin = aa[gg].graphMin;
1065 for (int gg = 0; gg < aa.length; gg++)
1067 if (aa[gg].graphGroup == row.graphGroup)
1069 drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
1070 endRes, y, groupmin, groupmax, row.graphHeight);
1074 graphGroupDrawn.set(row.graphGroup);
1078 drawLineGraph(g, row, row_annotations, startRes, endRes, y,
1079 row.graphMin, row.graphMax, row.graphHeight);
1082 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1084 drawBarGraph(g, row, row_annotations, startRes, endRes,
1085 row.graphMin, row.graphMax, y, renderHistogram,
1086 renderProfile, normaliseProfile);
1090 AnnotationRowRendererI renderer = rendererFactoryI
1091 .getRendererFor(row);
1092 if (renderer != null)
1094 renderer.renderRow(g, charWidth, charHeight, hasHiddenColumns,
1095 av, hiddenColumns, columnSelection, row,
1096 row_annotations, startRes, endRes, row.graphMin,
1101 if (renderer == null)
1104 .println("No renderer found for " + row.toString());
1109 "rendered with " + renderer.getClass().toString());
1118 if (clipst && !clipend)
1122 } // end if_in_visible_region
1123 if (row.graph > 0 && row.hasText)
1139 Console.warn("Start clip at : " + yfrom + " (index " + f_i + ")");
1143 Console.warn("End clip at : " + yto + " (index " + f_to + ")");
1147 Console.warn("Annotation Rendering time:"
1148 + (System.currentTimeMillis() - stime));
1155 public static final Color GLYPHLINE_COLOR = Color.gray;
1157 public static final Color SHEET_COLOUR = Color.green.darker();
1159 public static final Color HELIX_COLOUR = Color.red;
1161 public static final Color STEM_COLOUR = Color.blue;
1163 // private Color sdNOTCANONICAL_COLOUR;
1165 void drawGlyphLine(Graphics g, Annotation[] row, int lastSSX, int x,
1166 int y, int iconOffset, int startRes, int column, boolean validRes,
1169 g.setColor(GLYPHLINE_COLOR);
1171 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
1174 void drawSheetAnnot(Graphics g, Annotation[] row,
1176 int lastSSX, int x, int y, int iconOffset, int startRes,
1177 int column, boolean validRes, boolean validEnd)
1179 g.setColor(SHEET_COLOUR);
1181 if (!validEnd || !validRes || row == null || row[column] == null
1182 || row[column].secondaryStructure != 'E')
1184 fillRect(g, lastSSX, y + 4 + iconOffset,
1185 (x * charWidth) - lastSSX - 4, 6);
1188 { (x * charWidth) - 4, (x * charWidth) - 4, (x * charWidth) },
1190 { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset },
1195 fillRect(g, lastSSX, y + 4 + iconOffset,
1196 (x + 1) * charWidth - lastSSX, 6);
1201 void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x,
1202 int y, int iconOffset, int startRes, int column, boolean validRes,
1205 g.setColor(HELIX_COLOUR);
1207 int sCol = (lastSSX / charWidth)
1208 + hiddenColumns.visibleToAbsoluteColumn(startRes);
1210 int x2 = (x * charWidth);
1214 if (USE_FILL_ROUND_RECT)
1216 int ofs = charWidth / 2;
1217 // Off by 1 offset when drawing rects and ovals
1218 // to offscreen image on the MAC
1219 fillRoundRect(g, lastSSX, y + 4 + iconOffset, x2 - x1, 8, 8, 8);
1220 if (sCol == 0 || row[sCol - 1] == null
1221 || row[sCol - 1].secondaryStructure != 'H')
1226 // g.setColor(Color.orange);
1227 fillRoundRect(g, lastSSX, y + 4 + iconOffset, x2 - x1 - ofs + 1, 8,
1230 if (!validRes || row[column] == null
1231 || row[column].secondaryStructure != 'H')
1237 // g.setColor(Color.magenta);
1238 fillRoundRect(g, lastSSX + ofs, y + 4 + iconOffset,
1239 x2 - x1 - ofs + 1, 8, 0, 0);
1246 if (sCol == 0 || row[sCol - 1] == null
1247 || row[sCol - 1].secondaryStructure != 'H')
1249 fillArc(g, lastSSX, y + 4 + iconOffset, charWidth, 8, 90, 180);
1250 x1 += charWidth / 2;
1253 if (!validRes || row[column] == null
1254 || row[column].secondaryStructure != 'H')
1256 fillArc(g, (x * charWidth) - charWidth, y + 4 + iconOffset, charWidth,
1258 x2 -= charWidth / 2;
1261 fillRect(g, x1, y + 4 + iconOffset, x2 - x1, 8);
1264 void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1265 Annotation[] aa_annotations, int sRes, int eRes, int y, float min,
1266 float max, int graphHeight)
1268 if (sRes > aa_annotations.length)
1275 // Adjustment for fastpaint to left
1281 eRes = Math.min(eRes, aa_annotations.length);
1289 float range = max - min;
1294 y2 = y - (int) ((0 - min / range) * graphHeight);
1297 g.setColor(Color.gray);
1298 g.drawLine(x - charWidth, y2, (eRes - sRes + 1) * charWidth, y2);
1300 eRes = Math.min(eRes, aa_annotations.length);
1303 int aaMax = aa_annotations.length - 1;
1305 while (x < eRes - sRes)
1308 if (hasHiddenColumns)
1310 column = hiddenColumns.visibleToAbsoluteColumn(column);
1318 if (aa_annotations[column] == null)
1324 if (aa_annotations[column].colour == null)
1326 g.setColor(Color.black);
1330 g.setColor(aa_annotations[column].colour);
1333 if (aa_annotations[column - 1] == null
1334 && aa_annotations.length > column + 1
1335 && aa_annotations[column + 1] == null)
1338 y1 = y - (int) (((aa_annotations[column].value - min) / range)
1340 g.drawLine(x * charWidth + charWidth / 4, y1,
1341 x * charWidth + 3 * charWidth / 4, y1);
1346 if (aa_annotations[column - 1] == null)
1352 y1 = y - (int) (((aa_annotations[column - 1].value - min) / range)
1354 y2 = y - (int) (((aa_annotations[column].value - min) / range)
1357 g.drawLine(x * charWidth - charWidth / 2, y1,
1358 x * charWidth + charWidth / 2, y2);
1362 if (_aa.threshold != null)
1364 g.setColor(_aa.threshold.colour);
1365 Graphics2D g2 = (Graphics2D) g;
1366 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1367 BasicStroke.JOIN_ROUND, 3f, new float[]
1370 y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1371 g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1372 g2.setStroke(new BasicStroke());
1376 @SuppressWarnings("unused")
1377 void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1378 Annotation[] aa_annotations, int sRes, int eRes, float min,
1379 float max, int y, boolean renderHistogram, boolean renderProfile,
1380 boolean normaliseProfile)
1382 if (sRes > aa_annotations.length)
1386 Font ofont = g.getFont();
1387 eRes = Math.min(eRes, aa_annotations.length);
1389 int x = 0, y1 = y, y2 = y;
1391 float range = max - min;
1395 y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1398 g.setColor(Color.gray);
1400 g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
1403 int aaMax = aa_annotations.length - 1;
1404 while (x < eRes - sRes)
1407 if (hasHiddenColumns)
1409 column = hiddenColumns.visibleToAbsoluteColumn(column);
1417 if (aa_annotations[column] == null)
1422 if (aa_annotations[column].colour == null)
1424 g.setColor(Color.black);
1428 g.setColor(aa_annotations[column].colour);
1431 y1 = y - (int) (((aa_annotations[column].value - min) / (range))
1434 if (renderHistogram)
1438 fillRect(g, x * charWidth, y2, charWidth, y1 - y2);
1442 fillRect(g, x * charWidth, y1, charWidth, y2 - y1);
1445 // draw profile if available
1450 * {profile type, #values, total count, char1, pct1, char2, pct2...}
1452 int profl[] = getProfileFor(_aa, column);
1454 // just try to draw the logo if profl is not null
1455 if (profl != null && profl[2] != 0)
1457 boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
1458 boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
1459 float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1460 final double normaliseFactor = normaliseProfile ? _aa.graphHeight
1464 * Render a single base for a sequence profile, a base pair for
1465 * structure profile, and a triplet for a cdna profile
1467 char[] dc = new char[isStructureProfile ? 2
1468 : (isCdnaProfile ? 3 : 1)];
1470 // lm is not necessary - we can just use fm - could be off by no more
1472 // LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1473 // Console.info(asc + " " + dec + " " + (asc -
1475 // + " " + (dec - lm.getDescent()));
1477 double asc = fm.getAscent();
1478 double dec = fm.getDescent();
1479 double fht = fm.getHeight();
1481 double scale = 1f / (normaliseProfile ? profl[2] : 100f);
1482 // float ofontHeight = 1f / fm.getAscent();// magnify to fill box
1485 * Traverse the character(s)/percentage data in the array
1490 // profl[1] is the number of values in the profile
1491 for (int i = 0, c = 3, last = profl[1]; i < last; i++)
1495 if (isStructureProfile)
1497 // todo can we encode a structure pair as an int, like codons?
1498 dc[0] = (char) profl[c++];
1499 dc[1] = (char) profl[c++];
1502 else if (isCdnaProfile)
1504 CodingUtils.decodeCodon2(profl[c++], dc);
1509 dc[0] = (char) profl[c++];
1512 // next profl[] position is profile % for the character(s)
1514 int percent = profl[c++];
1517 // failsafe in case a count rounds down to 0%
1520 double newHeight = normaliseFactor * scale * percent;
1523 * Set character colour as per alignment colour scheme; use the
1524 * codon translation if a cDNA profile
1526 Color colour = null;
1529 final String codonTranslation = ResidueProperties
1531 colour = profcolour.findColour(codonTranslation.charAt(0),
1536 colour = profcolour.findColour(dc[0], column, null);
1538 g.setColor(colour == Color.white ? Color.lightGray : colour);
1540 // Debug - render boxes around characters
1541 // g.setColor(Color.red);
1542 // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
1544 // g.setColor(profcolour.findColour(dc[0]).darker());
1546 double sx = 1f * charWidth / fm.charsWidth(dc, 0, dc.length);
1547 double sy = newHeight / asc;
1548 double newAsc = asc * sy;
1549 double newDec = dec * sy;
1550 // it is not necessary to recalculate lm for the new font.
1551 // note: lm.getBaselineOffsets()[lm.getBaselineIndex()]) must be 0
1552 // by definition. Was:
1553 // int hght = (int) (ht + (newAsc - newDec);
1554 // - lm.getBaselineOffsets()[lm.getBaselineIndex()]));
1556 if (Platform.isJS())
1559 * SwingJS does not implement font.deriveFont()
1560 * so use a scaling transform to draw instead,
1561 * this is off by a very small amount
1563 final int hght = (int) (ht2 + (newAsc - newDec));
1564 Graphics2D gg = (Graphics2D) g;
1565 int xShift = (int) Math.round(x * charWidth / sx);
1566 int yShift = (int) Math.round(hght / sy);
1567 gg.transform(AffineTransform.getScaleInstance(sx, sy));
1568 gg.drawString(s, xShift, yShift);
1570 AffineTransform.getScaleInstance(1D / sx, 1D / sy));
1580 // Java ('normal') method is to scale the font to fit
1582 final int hght = (int) (ht + (newAsc - newDec));
1584 .deriveFont(AffineTransform.getScaleInstance(sx, sy));
1586 g.drawChars(dc, 0, dc.length, x * charWidth, hght);
1596 if (_aa.threshold != null)
1598 g.setColor(_aa.threshold.colour);
1599 Graphics2D g2 = (Graphics2D) g;
1600 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1601 BasicStroke.JOIN_ROUND, 3f, new float[]
1605 - ((_aa.threshold.value - min) / range) * _aa.graphHeight);
1606 g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1607 g2.setStroke(new BasicStroke());
1611 // used by overview window
1612 public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1613 Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1615 eRes = Math.min(eRes, aa_annotations.length);
1616 g.setColor(Color.white);
1617 fillRect(g, 0, 0, width, y);
1618 g.setColor(new Color(0, 0, 180));
1622 for (int j = sRes; j < eRes; j++)
1624 if (aa_annotations[j] != null)
1626 if (aa_annotations[j].colour == null)
1628 g.setColor(Color.black);
1632 g.setColor(aa_annotations[j].colour);
1635 height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1641 fillRect(g, x, y - height, charWidth, height);
1647 Color getNotCanonicalColor(char lastss)
1653 return new Color(255, 125, 5);
1657 return new Color(245, 115, 10);
1661 return new Color(235, 135, 15);
1665 return new Color(225, 105, 20);
1669 return new Color(215, 145, 30);
1673 return new Color(205, 95, 35);
1677 return new Color(195, 155, 45);
1681 return new Color(185, 85, 55);
1685 return new Color(175, 165, 65);
1689 return new Color(170, 75, 75);
1693 return new Color(160, 175, 85);
1697 return new Color(150, 65, 95);
1701 return new Color(140, 185, 105);
1705 return new Color(130, 55, 110);
1709 return new Color(120, 195, 120);
1713 return new Color(110, 45, 130);
1717 return new Color(100, 205, 140);
1721 return new Color(90, 35, 150);
1725 return new Color(85, 215, 160);
1729 return new Color(75, 25, 170);
1733 return new Color(65, 225, 180);
1737 return new Color(55, 15, 185);
1741 return new Color(45, 235, 195);
1745 return new Color(35, 5, 205);
1749 return new Color(25, 245, 215);
1753 return new Color(15, 0, 225);
1757 return new Color(10, 255, 235);
1761 return new Color(5, 150, 245);
1765 return new Color(0, 80, 255);
1768 Console.info("This is not a interaction : " + lastss);
1774 private static void fillPolygon(Graphics g, int[] xpoints, int[] ypoints,
1778 g.fillPolygon(xpoints, ypoints, n);
1779 g.drawPolygon(xpoints, ypoints, n);
1782 private static void fillRect(Graphics g, int a, int b, int c, int d)
1785 g.fillRect(a, b, c, d);
1786 g.drawRect(a, b, c, d);
1789 private static void fillRoundRect(Graphics g, int a, int b, int c, int d,
1793 g.fillRoundRect(a, b, c, d, e, f);
1794 g.drawRoundRect(a, b, c, d, e, f);
1797 private static void fillArc(Graphics g, int a, int b, int c, int d, int e,
1801 g.fillArc(a, b, c, d, e, f);
1802 g.drawArc(a, b, c, d, e, f);
1805 private static void setAntiAlias(Graphics g)
1807 if (Cache.getDefault("ANTI_ALIAS", true))
1809 Graphics2D g2d = (Graphics2D) g;
1810 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1811 RenderingHints.VALUE_ANTIALIAS_ON);
1815 private static void unsetAntiAlias(Graphics g)
1817 Graphics2D g2d = (Graphics2D) g;
1818 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1819 RenderingHints.VALUE_ANTIALIAS_OFF);