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.geom.AffineTransform;
31 import java.awt.image.ImageObserver;
32 import java.util.BitSet;
33 import java.util.Hashtable;
35 import jalview.analysis.AAFrequency;
36 import jalview.analysis.CodingUtils;
37 import jalview.analysis.Rna;
38 import jalview.analysis.StructureFrequency;
39 import jalview.api.AlignViewportI;
40 import jalview.datamodel.AlignmentAnnotation;
41 import jalview.datamodel.Annotation;
42 import jalview.datamodel.ColumnSelection;
43 import jalview.datamodel.HiddenColumns;
44 import jalview.datamodel.ProfilesI;
45 import jalview.renderer.api.AnnotationRendererFactoryI;
46 import jalview.renderer.api.AnnotationRowRendererI;
47 import jalview.schemes.ColourSchemeI;
48 import jalview.schemes.NucleotideColourScheme;
49 import jalview.schemes.ResidueProperties;
50 import jalview.schemes.ZappoColourScheme;
51 import jalview.util.Platform;
53 public class AnnotationRenderer
55 private static final int UPPER_TO_LOWER = 'a' - 'A'; // 32
57 private static final int CHAR_A = 'A'; // 65
59 private static final int CHAR_Z = 'Z'; // 90
62 * flag indicating if timing and redraw parameter info should be output
64 private final boolean debugRedraw;
66 private int charWidth, endRes, charHeight;
68 private boolean validCharWidth, hasHiddenColumns;
70 private FontMetrics fm;
72 private final boolean USE_FILL_ROUND_RECT = Platform.isAMacAndNotJS();
74 boolean av_renderHistogram = true, av_renderProfile = true,
75 av_normaliseProfile = false;
77 ResidueShaderI profcolour = null;
79 private ColumnSelection columnSelection;
81 private HiddenColumns hiddenColumns;
83 private ProfilesI hconsensus;
85 private Hashtable<String, Object>[] complementConsensus;
87 private Hashtable<String, Object>[] hStrucConsensus;
89 private boolean av_ignoreGapsConsensus;
92 * attributes set from AwtRenderPanelI
95 * old image used when data is currently being calculated and cannot be
98 private Image fadedImage;
101 * panel being rendered into
103 private ImageObserver annotationPanel;
106 * width of image to render in panel
108 private int imgWidth;
111 * offset to beginning of visible area
116 * offset to end of visible area
118 private int visHeight;
121 * indicate if the renderer should only render the visible portion of the
122 * annotation given the current view settings
124 private boolean useClip = true;
127 * master flag indicating if renderer should ever try to clip. not enabled for
130 private boolean canClip = false;
132 public AnnotationRenderer()
138 * Create a new annotation Renderer
141 * flag indicating if timing and redraw parameter info should be
144 public AnnotationRenderer(boolean debugRedraw)
146 this.debugRedraw = debugRedraw;
150 * Remove any references and resources when this object is no longer required
152 public void dispose()
154 hiddenColumns = null;
156 complementConsensus = null;
157 hStrucConsensus = null;
159 annotationPanel = null;
160 rendererFactoryI = null;
163 void drawStemAnnot(Graphics g, Annotation[] row_annotations, int lastSSX,
164 int x, int y, int iconOffset, int startRes, int column,
165 boolean validRes, boolean validEnd)
167 g.setColor(STEM_COLOUR);
168 int sCol = (lastSSX / charWidth)
169 + hiddenColumns.visibleToAbsoluteColumn(startRes);
171 int x2 = (x * charWidth);
173 char dc = (column == 0 || row_annotations[column - 1] == null) ? ' '
174 : row_annotations[column - 1].secondaryStructure;
176 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
177 || dc != row_annotations[sCol - 1].secondaryStructure;
178 boolean diffdownstream = !validRes || !validEnd
179 || row_annotations[column] == null
180 || dc != row_annotations[column].secondaryStructure;
182 if (column > 0 && Rna.isClosingParenthesis(dc))
185 // if (validRes && column>1 && row_annotations[column-2]!=null &&
186 // dc.equals(row_annotations[column-2].displayCharacter))
189 * if new annotation with a closing base pair half of the stem,
190 * display a backward arrow
192 g.fillPolygon(new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
194 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
205 // display a forward arrow
209 * if annotation ending with an opeing base pair half of the stem,
210 * display a forward arrow
212 g.fillPolygon(new int[] { x2 - 5, x2 - 5, x2 },
214 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
224 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
227 void drawNotCanonicalAnnot(Graphics g, Color nonCanColor,
228 Annotation[] row_annotations, int lastSSX, int x, int y,
229 int iconOffset, int startRes, int column, boolean validRes,
232 // System.out.println(nonCanColor);
234 g.setColor(nonCanColor);
235 int sCol = (lastSSX / charWidth)
236 + hiddenColumns.visibleToAbsoluteColumn(startRes);
238 int x2 = (x * charWidth);
240 String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
241 : row_annotations[column - 1].displayCharacter;
243 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
244 || !dc.equals(row_annotations[sCol - 1].displayCharacter);
245 boolean diffdownstream = !validRes || !validEnd
246 || row_annotations[column] == null
247 || !dc.equals(row_annotations[column].displayCharacter);
248 // System.out.println("Column "+column+" diff up: "+diffupstream+"
249 // down:"+diffdownstream);
250 // If a closing base pair half of the stem, display a backward arrow
251 if (column > 0 && Rna.isClosingParenthesis(dc))
255 // if (validRes && column>1 && row_annotations[column-2]!=null &&
256 // dc.equals(row_annotations[column-2].displayCharacter))
258 g.fillPolygon(new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
260 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
272 // display a forward arrow
275 g.fillPolygon(new int[] { x2 - 5, x2 - 5, x2 },
277 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
287 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
290 // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
292 public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
295 fm = annotPanel.getFontMetrics();
296 annotationPanel = annotPanel;
297 fadedImage = annotPanel.getFadedImage();
298 imgWidth = annotPanel.getFadedImageWidth();
299 // visible area for rendering
300 int[] bounds = annotPanel.getVisibleVRange();
304 visHeight = bounds[1];
319 rendererFactoryI = AnnotationRendererFactory.getRendererFactory();
320 updateFromAlignViewport(av);
323 public void updateFromAlignViewport(AlignViewportI av)
325 charWidth = av.getCharWidth();
326 endRes = av.getRanges().getEndRes();
327 charHeight = av.getCharHeight();
328 hasHiddenColumns = av.hasHiddenColumns();
329 validCharWidth = av.isValidCharWidth();
330 av_renderHistogram = av.isShowConsensusHistogram();
331 av_renderProfile = av.isShowSequenceLogo();
332 av_normaliseProfile = av.isNormaliseSequenceLogo();
333 profcolour = av.getResidueShading();
334 if (profcolour == null || profcolour.getColourScheme() == null)
337 * Use default colour for sequence logo if
338 * the alignment has no colourscheme set
339 * (would like to use user preference but n/a for applet)
341 ColourSchemeI col = av.getAlignment().isNucleotide()
342 ? new NucleotideColourScheme()
343 : new ZappoColourScheme();
344 profcolour = new ResidueShader(col);
346 columnSelection = av.getColumnSelection();
347 hiddenColumns = av.getAlignment().getHiddenColumns();
348 hconsensus = av.getSequenceConsensusHash();
349 complementConsensus = av.getComplementConsensusHash();
350 hStrucConsensus = av.getRnaStructureConsensusHash();
351 av_ignoreGapsConsensus = av.isIgnoreGapsConsensus();
355 * Returns profile data; the first element is the profile type, the second is
356 * the number of distinct values, the third the total count, and the remainder
357 * depend on the profile type.
363 int[] getProfileFor(AlignmentAnnotation aa, int column)
365 // TODO : consider refactoring the global alignment calculation
366 // properties/rendering attributes as a global 'alignment group' which holds
367 // all vis settings for the alignment as a whole rather than a subset
369 if (aa.autoCalculated && (aa.label.startsWith("Consensus")
370 || aa.label.startsWith("cDNA Consensus")))
372 boolean forComplement = aa.label.startsWith("cDNA Consensus");
373 if (aa.groupRef != null && aa.groupRef.consensusData != null
374 && aa.groupRef.isShowSequenceLogo())
376 // TODO? group consensus for cDNA complement
377 return AAFrequency.extractProfile(
378 aa.groupRef.consensusData.get(column),
379 aa.groupRef.getIgnoreGapsConsensus());
381 // TODO extend annotation row to enable dynamic and static profile data to
383 if (aa.groupRef == null && aa.sequenceRef == null)
387 return AAFrequency.extractCdnaProfile(complementConsensus[column],
388 av_ignoreGapsConsensus);
392 return AAFrequency.extractProfile(hconsensus.get(column),
393 av_ignoreGapsConsensus);
399 if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
401 // TODO implement group structure consensus
403 * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
404 * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
405 * group selections return StructureFrequency.extractProfile(
406 * aa.groupRef.consensusData[column], aa.groupRef
407 * .getIgnoreGapsConsensus()); }
409 // TODO extend annotation row to enable dynamic and static profile data
412 if (aa.groupRef == null && aa.sequenceRef == null
413 && hStrucConsensus != null
414 && hStrucConsensus.length > column)
416 return StructureFrequency.extractProfile(hStrucConsensus[column],
417 av_ignoreGapsConsensus);
426 private AnnotationRendererFactoryI rendererFactoryI;
429 * Render the annotation rows associated with an alignment.
434 * data and view settings to render
436 * destination for graphics
438 * row where a mouse event occured (or -1)
440 * first column that will be drawn
442 * last column that will be drawn
443 * @return true if the fadedImage was used for any alignment annotation rows
444 * currently being calculated
446 public boolean drawComponent(AwtRenderPanelI annotPanel,
447 AlignViewportI av, Graphics g, int activeRow, int startRes,
450 long stime = System.currentTimeMillis();
451 boolean usedFaded = false;
453 // AnnotationPanel needs to implement: ImageObserver, access to
455 updateFromAwtRenderPanel(annotPanel, av);
456 fm = g.getFontMetrics();
457 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
468 boolean validRes = false;
469 boolean validEnd = false;
470 boolean labelAllCols = false;
471 // boolean centreColLabels;
472 // boolean centreColLabelsDef = av.isCentreColumnLabels();
473 boolean scaleColLabel = false;
474 final AlignmentAnnotation consensusAnnot = av
475 .getAlignmentConsensusAnnotation();
476 final AlignmentAnnotation structConsensusAnnot = av
477 .getAlignmentStrucConsensusAnnotation();
478 final AlignmentAnnotation complementConsensusAnnot = av
479 .getComplementConsensusAnnotation();
481 BitSet graphGroupDrawn = new BitSet();
482 int charOffset = 0; // offset for a label
485 int yfrom = 0, f_i = 0, yto = 0, f_to = 0;
486 boolean clipst = false, clipend = false;
487 for (int i = 0; i < aa.length; i++)
489 AlignmentAnnotation row = aa[i];
490 boolean renderHistogram = true;
491 boolean renderProfile = false;
492 boolean normaliseProfile = false;
493 boolean isRNA = row.isRNA();
495 // check if this is a consensus annotation row and set the display
496 // settings appropriately
497 // TODO: generalise this to have render styles for consensus/profile
499 if (row.groupRef != null && row == row.groupRef.getConsensus())
501 renderHistogram = row.groupRef.isShowConsensusHistogram();
502 renderProfile = row.groupRef.isShowSequenceLogo();
503 normaliseProfile = row.groupRef.isNormaliseSequenceLogo();
505 else if (row == consensusAnnot || row == structConsensusAnnot
506 || row == complementConsensusAnnot)
508 renderHistogram = av_renderHistogram;
509 renderProfile = av_renderProfile;
510 normaliseProfile = av_normaliseProfile;
513 Annotation[] row_annotations = row.annotations;
518 // centreColLabels = row.centreColLabels || centreColLabelsDef;
519 labelAllCols = row.showAllColLabels;
520 scaleColLabel = row.scaleColLabel;
524 if (!useClip || ((y - charHeight) < visHeight
525 && (y + row.height + charHeight * 2) >= sOffset))
526 {// if_in_visible_region
537 if (row.graphGroup > -1 && graphGroupDrawn.get(row.graphGroup))
542 // this is so that we draw the characters below the graph
547 iconOffset = charHeight - fm.getDescent();
551 else if (row.hasText)
553 iconOffset = charHeight - fm.getDescent();
561 if (row.autoCalculated && av.isCalculationInProgress(row))
565 g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0,
566 y - row.height, imgWidth, y, annotationPanel);
567 g.setColor(Color.black);
568 // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
574 * else if (annotationPanel.av.updatingConservation &&
575 * aa[i].label.equals("Conservation")) {
577 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
578 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
579 * annotationPanel.imgWidth, y, annotationPanel);
581 * g.setColor(Color.black); //
582 * g.drawString("Calculating Conservation.....",20, y-row.height/2);
584 * continue; } else if (annotationPanel.av.updatingConservation &&
585 * aa[i].label.equals("Quality")) {
587 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
588 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
589 * annotationPanel.imgWidth, y, annotationPanel);
590 * g.setColor(Color.black); // /
591 * g.drawString("Calculating Quality....",20, y-row.height/2);
595 // first pass sets up state for drawing continuation from left-hand
598 x = (startRes == 0) ? 0 : -1;
599 while (x < endRes - startRes)
601 if (hasHiddenColumns)
603 column = hiddenColumns.visibleToAbsoluteColumn(startRes + x);
604 if (column > row_annotations.length - 1)
611 column = startRes + x;
614 if ((row_annotations == null)
615 || (row_annotations.length <= column)
616 || (row_annotations[column] == null))
624 final String displayChar = validRes
625 ? row_annotations[column].displayCharacter
631 g.setColor(Color.red);
633 if (columnSelection != null)
635 if (columnSelection.contains(column))
637 g.fillRect(x * charWidth, y, charWidth, charHeight);
641 if (row.getInvalidStrucPos() > x)
643 g.setColor(Color.orange);
644 g.fillRect(x * charWidth, y, charWidth, charHeight);
646 else if (row.getInvalidStrucPos() == x)
648 g.setColor(Color.orange.darker());
649 g.fillRect(x * charWidth, y, charWidth, charHeight);
651 if (validCharWidth && validRes && displayChar != null
652 && (displayChar.length() > 0))
654 Graphics2D gg = ((Graphics2D) g);
655 float fmWidth = fm.charsWidth(displayChar.toCharArray(), 0,
656 displayChar.length());
659 * shrink label width to fit in column, if that is
660 * both configured and necessary
662 boolean scaledToFit = false;
663 float fmScaling = 1f;
664 if (scaleColLabel && fmWidth > charWidth)
667 fmScaling = charWidth;
668 fmScaling /= fmWidth;
669 // and update the label's width to reflect the scaling.
673 charOffset = (int) ((charWidth - fmWidth) / 2f);
675 if (row_annotations[column].colour == null)
677 gg.setColor(Color.black);
681 gg.setColor(row_annotations[column].colour);
685 * draw the label, unless it is the same secondary structure
686 * symbol (excluding RNA Helix) as the previous column
688 final int xPos = (x * charWidth) + charOffset;
689 final int yPos = y + iconOffset;
692 * translate to drawing position _before_ applying any scaling
694 gg.translate(xPos, yPos);
698 * use a scaling transform to make the label narrower
699 * (JalviewJS doesn't have Font.deriveFont(AffineTransform))
702 AffineTransform.getScaleInstance(fmScaling, 1.0));
704 if (column == 0 || row.graph > 0)
706 gg.drawString(displayChar, 0, 0);
708 else if (row_annotations[column - 1] == null || (labelAllCols
709 || !displayChar.equals(
710 row_annotations[column - 1].displayCharacter)
711 || (displayChar.length() < 2
712 && row_annotations[column].secondaryStructure == ' ')))
714 gg.drawString(displayChar, 0, 0);
719 * undo scaling before translating back
720 * (restoring saved transform does NOT work in JS PDFGraphics!)
722 gg.transform(AffineTransform
723 .getScaleInstance(1D / fmScaling, 1.0));
725 gg.translate(-xPos, -yPos);
730 char ss = validRes ? row_annotations[column].secondaryStructure
735 // distinguish between forward/backward base-pairing
736 if (displayChar.indexOf(')') > -1)
745 if ((displayChar.indexOf(']') > -1))
753 // distinguish between forward/backward base-pairing
754 if (displayChar.indexOf('}') > -1)
762 // distinguish between forward/backward base-pairing
763 if (displayChar.indexOf('<') > -1)
769 if (isRNA && (ss >= CHAR_A) && (ss <= CHAR_Z))
771 // distinguish between forward/backward base-pairing
772 int ssLowerCase = ss + UPPER_TO_LOWER;
773 // TODO would .equals() be safer here? or charAt(0)?
774 if (displayChar.indexOf(ssLowerCase) > -1)
776 ss = (char) ssLowerCase;
780 if (!validRes || (ss != lastSS))
786 // int nb_annot = x - temp;
787 // System.out.println("\t type :"+lastSS+"\t x :"+x+"\t nbre
788 // annot :"+nb_annot);
791 case '(': // Stem case for RNA secondary structure
792 case ')': // and opposite direction
793 drawStemAnnot(g, row_annotations, lastSSX, x, y,
794 iconOffset, startRes, column, validRes, validEnd);
801 drawHelixAnnot(g, row_annotations, lastSSX, x, y,
802 iconOffset, startRes, column, validRes,
806 // no break if isRNA - falls through to drawNotCanonicalAnnot!
810 drawSheetAnnot(g, row_annotations, lastSSX, x, y,
811 iconOffset, startRes, column, validRes,
815 // no break if isRNA - fall through to drawNotCanonicalAnnot!
874 Color nonCanColor = getNotCanonicalColor(lastSS);
875 drawNotCanonicalAnnot(g, nonCanColor, row_annotations,
876 lastSSX, x, y, iconOffset, startRes, column,
881 g.setColor(Color.gray);
882 g.fillRect(lastSSX, y + 6 + iconOffset,
883 (x * charWidth) - lastSSX, 2);
898 lastSSX = (x * charWidth);
905 if (column >= row_annotations.length)
907 column = row_annotations.length - 1;
914 if ((row_annotations == null) || (row_annotations.length <= column)
915 || (row_annotations[column] == null))
933 drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
934 startRes, column, validRes, validEnd);
937 // no break if isRNA - fall through to drawNotCanonicalAnnot!
942 drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
943 startRes, column, validRes, validEnd);
946 // no break if isRNA - fall through to drawNotCanonicalAnnot!
949 case ')': // Stem case for RNA secondary structure
951 drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
952 startRes, column, validRes, validEnd);
1009 // System.out.println(lastSS);
1010 Color nonCanColor = getNotCanonicalColor(lastSS);
1011 drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX,
1012 x, y, iconOffset, startRes, column, validRes, validEnd);
1015 drawGlyphLine(g, row_annotations, lastSSX, x, y, iconOffset,
1016 startRes, column, validRes, validEnd);
1021 if (row.graph > 0 && row.graphHeight > 0)
1023 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1025 if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
1027 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1028 // computed efficiently for all visible labels
1029 float groupmax = -999999, groupmin = 9999999;
1030 for (int gg = 0; gg < aa.length; gg++)
1032 if (aa[gg].graphGroup != row.graphGroup)
1039 aa[gg].visible = false;
1041 if (aa[gg].graphMax > groupmax)
1043 groupmax = aa[gg].graphMax;
1045 if (aa[gg].graphMin < groupmin)
1047 groupmin = aa[gg].graphMin;
1051 for (int gg = 0; gg < aa.length; gg++)
1053 if (aa[gg].graphGroup == row.graphGroup)
1055 drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
1056 endRes, y, groupmin, groupmax, row.graphHeight);
1060 graphGroupDrawn.set(row.graphGroup);
1064 drawLineGraph(g, row, row_annotations, startRes, endRes, y,
1065 row.graphMin, row.graphMax, row.graphHeight);
1068 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1070 drawBarGraph(g, row, row_annotations, startRes, endRes,
1071 row.graphMin, row.graphMax, y, renderHistogram,
1072 renderProfile, normaliseProfile);
1076 AnnotationRowRendererI renderer = rendererFactoryI
1077 .getRendererFor(row);
1078 if (renderer != null)
1080 renderer.renderRow(g, charWidth, charHeight, hasHiddenColumns,
1081 av, hiddenColumns, columnSelection, row,
1082 row_annotations, startRes, endRes, row.graphMin,
1087 if (renderer == null)
1090 .println("No renderer found for " + row.toString());
1095 "rendered with " + renderer.getClass().toString());
1104 if (clipst && !clipend)
1108 } // end if_in_visible_region
1109 if (row.graph > 0 && row.hasText)
1126 "Start clip at : " + yfrom + " (index " + f_i + ")");
1131 "End clip at : " + yto + " (index " + f_to + ")");
1135 System.err.println("Annotation Rendering time:"
1136 + (System.currentTimeMillis() - stime));
1143 public static final Color GLYPHLINE_COLOR = Color.gray;
1145 public static final Color SHEET_COLOUR = Color.green;
1147 public static final Color HELIX_COLOUR = Color.red;
1149 public static final Color STEM_COLOUR = Color.blue;
1151 // private Color sdNOTCANONICAL_COLOUR;
1153 void drawGlyphLine(Graphics g, Annotation[] row, int lastSSX, int x,
1154 int y, int iconOffset, int startRes, int column, boolean validRes,
1157 g.setColor(GLYPHLINE_COLOR);
1158 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
1161 void drawSheetAnnot(Graphics g, Annotation[] row,
1163 int lastSSX, int x, int y, int iconOffset, int startRes,
1164 int column, boolean validRes, boolean validEnd)
1166 g.setColor(SHEET_COLOUR);
1168 if (!validEnd || !validRes || row == null || row[column] == null
1169 || row[column].secondaryStructure != 'E')
1171 g.fillRect(lastSSX, y + 4 + iconOffset, (x * charWidth) - lastSSX - 4,
1175 { (x * charWidth) - 4, (x * charWidth) - 4, (x * charWidth) },
1177 { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset },
1182 g.fillRect(lastSSX, y + 4 + iconOffset, (x + 1) * charWidth - lastSSX,
1188 void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x,
1189 int y, int iconOffset, int startRes, int column, boolean validRes,
1192 g.setColor(HELIX_COLOUR);
1194 int sCol = (lastSSX / charWidth)
1195 + hiddenColumns.visibleToAbsoluteColumn(startRes);
1197 int x2 = (x * charWidth);
1199 if (USE_FILL_ROUND_RECT)
1201 int ofs = charWidth / 2;
1202 // Off by 1 offset when drawing rects and ovals
1203 // to offscreen image on the MAC
1204 g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1, 8, 8, 8);
1205 if (sCol == 0 || row[sCol - 1] == null
1206 || row[sCol - 1].secondaryStructure != 'H')
1211 // g.setColor(Color.orange);
1212 g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1 - ofs + 1, 8,
1215 if (!validRes || row[column] == null
1216 || row[column].secondaryStructure != 'H')
1222 // g.setColor(Color.magenta);
1223 g.fillRoundRect(lastSSX + ofs, y + 4 + iconOffset,
1224 x2 - x1 - ofs + 1, 8, 0, 0);
1231 if (sCol == 0 || row[sCol - 1] == null
1232 || row[sCol - 1].secondaryStructure != 'H')
1234 g.fillArc(lastSSX, y + 4 + iconOffset, charWidth, 8, 90, 180);
1235 x1 += charWidth / 2;
1238 if (!validRes || row[column] == null
1239 || row[column].secondaryStructure != 'H')
1241 g.fillArc((x * charWidth) - charWidth, y + 4 + iconOffset, charWidth,
1243 x2 -= charWidth / 2;
1246 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
1249 void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1250 Annotation[] aa_annotations, int sRes, int eRes, int y, float min,
1251 float max, int graphHeight)
1253 if (sRes > aa_annotations.length)
1260 // Adjustment for fastpaint to left
1266 eRes = Math.min(eRes, aa_annotations.length);
1274 float range = max - min;
1279 y2 = y - (int) ((0 - min / range) * graphHeight);
1282 g.setColor(Color.gray);
1283 g.drawLine(x - charWidth, y2, (eRes - sRes + 1) * charWidth, y2);
1285 eRes = Math.min(eRes, aa_annotations.length);
1288 int aaMax = aa_annotations.length - 1;
1290 while (x < eRes - sRes)
1293 if (hasHiddenColumns)
1295 column = hiddenColumns.visibleToAbsoluteColumn(column);
1303 if (aa_annotations[column] == null)
1309 if (aa_annotations[column].colour == null)
1311 g.setColor(Color.black);
1315 g.setColor(aa_annotations[column].colour);
1318 if (aa_annotations[column - 1] == null
1319 && aa_annotations.length > column + 1
1320 && aa_annotations[column + 1] == null)
1323 y1 = y - (int) (((aa_annotations[column].value - min) / range)
1325 g.drawLine(x * charWidth + charWidth / 4, y1,
1326 x * charWidth + 3 * charWidth / 4, y1);
1331 if (aa_annotations[column - 1] == null)
1337 y1 = y - (int) (((aa_annotations[column - 1].value - min) / range)
1339 y2 = y - (int) (((aa_annotations[column].value - min) / range)
1342 g.drawLine(x * charWidth - charWidth / 2, y1,
1343 x * charWidth + charWidth / 2, y2);
1347 if (_aa.threshold != null)
1349 g.setColor(_aa.threshold.colour);
1350 Graphics2D g2 = (Graphics2D) g;
1351 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1352 BasicStroke.JOIN_ROUND, 3f, new float[]
1355 y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1356 g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1357 g2.setStroke(new BasicStroke());
1361 @SuppressWarnings("unused")
1362 void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1363 Annotation[] aa_annotations, int sRes, int eRes, float min,
1364 float max, int y, boolean renderHistogram, boolean renderProfile,
1365 boolean normaliseProfile)
1367 if (sRes > aa_annotations.length)
1371 Font ofont = g.getFont();
1372 eRes = Math.min(eRes, aa_annotations.length);
1374 int x = 0, y1 = y, y2 = y;
1376 float range = max - min;
1380 y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1383 g.setColor(Color.gray);
1385 g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
1388 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)
1407 if (aa_annotations[column].colour == null)
1409 g.setColor(Color.black);
1413 g.setColor(aa_annotations[column].colour);
1416 y1 = y - (int) (((aa_annotations[column].value - min) / (range))
1419 if (renderHistogram)
1423 g.fillRect(x * charWidth, y2, charWidth, y1 - y2);
1427 g.fillRect(x * charWidth, y1, charWidth, y2 - y1);
1430 // draw profile if available
1435 * {profile type, #values, total count, char1, pct1, char2, pct2...}
1437 int profl[] = getProfileFor(_aa, column);
1439 // just try to draw the logo if profl is not null
1440 if (profl != null && profl[2] != 0)
1442 boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
1443 boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
1444 float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1445 final double normaliseFactor = normaliseProfile ? _aa.graphHeight
1449 * Render a single base for a sequence profile, a base pair for
1450 * structure profile, and a triplet for a cdna profile
1452 char[] dc = new char[isStructureProfile ? 2
1453 : (isCdnaProfile ? 3 : 1)];
1455 // lm is not necessary - we can just use fm - could be off by no more
1457 // LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1458 // System.out.println(asc + " " + dec + " " + (asc - lm.getAscent())
1459 // + " " + (dec - lm.getDescent()));
1461 double asc = fm.getAscent();
1462 double dec = fm.getDescent();
1463 double fht = fm.getHeight();
1465 double scale = 1f / (normaliseProfile ? profl[2] : 100f);
1466 // float ofontHeight = 1f / fm.getAscent();// magnify to fill box
1469 * Traverse the character(s)/percentage data in the array
1474 // profl[1] is the number of values in the profile
1475 for (int i = 0, c = 3, last = profl[1]; i < last; i++)
1479 if (isStructureProfile)
1481 // todo can we encode a structure pair as an int, like codons?
1482 dc[0] = (char) profl[c++];
1483 dc[1] = (char) profl[c++];
1486 else if (isCdnaProfile)
1488 CodingUtils.decodeCodon2(profl[c++], dc);
1493 dc[0] = (char) profl[c++];
1496 // next profl[] position is profile % for the character(s)
1498 int percent = profl[c++];
1501 // failsafe in case a count rounds down to 0%
1504 double newHeight = normaliseFactor * scale * percent;
1507 * Set character colour as per alignment colour scheme; use the
1508 * codon translation if a cDNA profile
1510 Color colour = null;
1513 final String codonTranslation = ResidueProperties
1515 colour = profcolour.findColour(codonTranslation.charAt(0),
1520 colour = profcolour.findColour(dc[0], column, null);
1522 g.setColor(colour == Color.white ? Color.lightGray : colour);
1524 // Debug - render boxes around characters
1525 // g.setColor(Color.red);
1526 // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
1528 // g.setColor(profcolour.findColour(dc[0]).darker());
1530 double sx = 1f * charWidth / fm.charsWidth(dc, 0, dc.length);
1531 double sy = newHeight / asc;
1532 double newAsc = asc * sy;
1533 double newDec = dec * sy;
1534 // it is not necessary to recalculate lm for the new font.
1535 // note: lm.getBaselineOffsets()[lm.getBaselineIndex()]) must be 0
1536 // by definition. Was:
1537 // int hght = (int) (ht + (newAsc - newDec);
1538 // - lm.getBaselineOffsets()[lm.getBaselineIndex()]));
1540 if (Platform.isJS())
1543 * SwingJS does not implement font.deriveFont()
1544 * so use a scaling transform to draw instead,
1545 * this is off by a very small amount
1547 final int hght = (int) (ht2 + (newAsc - newDec));
1548 Graphics2D gg = (Graphics2D) g;
1549 int xShift = (int) Math.round(x * charWidth / sx);
1550 int yShift = (int) Math.round(hght / sy);
1551 gg.transform(AffineTransform.getScaleInstance(sx, sy));
1552 gg.drawString(s, xShift, yShift);
1554 AffineTransform.getScaleInstance(1D / sx, 1D / sy));
1564 // Java ('normal') method is to scale the font to fit
1566 final int hght = (int) (ht + (newAsc - newDec));
1568 .deriveFont(AffineTransform.getScaleInstance(sx, sy));
1570 g.drawChars(dc, 0, dc.length, x * charWidth, hght);
1580 if (_aa.threshold != null)
1582 g.setColor(_aa.threshold.colour);
1583 Graphics2D g2 = (Graphics2D) g;
1584 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1585 BasicStroke.JOIN_ROUND, 3f, new float[]
1589 - ((_aa.threshold.value - min) / range) * _aa.graphHeight);
1590 g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1591 g2.setStroke(new BasicStroke());
1595 // used by overview window
1596 public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1597 Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1599 eRes = Math.min(eRes, aa_annotations.length);
1600 g.setColor(Color.white);
1601 g.fillRect(0, 0, width, y);
1602 g.setColor(new Color(0, 0, 180));
1606 for (int j = sRes; j < eRes; j++)
1608 if (aa_annotations[j] != null)
1610 if (aa_annotations[j].colour == null)
1612 g.setColor(Color.black);
1616 g.setColor(aa_annotations[j].colour);
1619 height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1625 g.fillRect(x, y - height, charWidth, height);
1631 Color getNotCanonicalColor(char lastss)
1637 return new Color(255, 125, 5);
1641 return new Color(245, 115, 10);
1645 return new Color(235, 135, 15);
1649 return new Color(225, 105, 20);
1653 return new Color(215, 145, 30);
1657 return new Color(205, 95, 35);
1661 return new Color(195, 155, 45);
1665 return new Color(185, 85, 55);
1669 return new Color(175, 165, 65);
1673 return new Color(170, 75, 75);
1677 return new Color(160, 175, 85);
1681 return new Color(150, 65, 95);
1685 return new Color(140, 185, 105);
1689 return new Color(130, 55, 110);
1693 return new Color(120, 195, 120);
1697 return new Color(110, 45, 130);
1701 return new Color(100, 205, 140);
1705 return new Color(90, 35, 150);
1709 return new Color(85, 215, 160);
1713 return new Color(75, 25, 170);
1717 return new Color(65, 225, 180);
1721 return new Color(55, 15, 185);
1725 return new Color(45, 235, 195);
1729 return new Color(35, 5, 205);
1733 return new Color(25, 245, 215);
1737 return new Color(15, 0, 225);
1741 return new Color(10, 255, 235);
1745 return new Color(5, 150, 245);
1749 return new Color(0, 80, 255);
1752 System.out.println("This is not a interaction : " + lastss);