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 jalview.analysis.AAFrequency;
24 import jalview.analysis.CodingUtils;
25 import jalview.analysis.Rna;
26 import jalview.analysis.StructureFrequency;
27 import jalview.api.AlignViewportI;
28 import jalview.datamodel.AlignmentAnnotation;
29 import jalview.datamodel.Annotation;
30 import jalview.datamodel.ColumnSelection;
31 import jalview.datamodel.HiddenColumns;
32 import jalview.datamodel.ProfilesI;
33 import jalview.schemes.ColourSchemeI;
34 import jalview.schemes.NucleotideColourScheme;
35 import jalview.schemes.ResidueProperties;
36 import jalview.schemes.ZappoColourScheme;
37 import jalview.util.Platform;
39 import java.awt.BasicStroke;
40 import java.awt.Color;
42 import java.awt.FontMetrics;
43 import java.awt.Graphics;
44 import java.awt.Graphics2D;
45 import java.awt.Image;
46 import java.awt.geom.AffineTransform;
47 import java.awt.image.ImageObserver;
48 import java.util.BitSet;
49 import java.util.Hashtable;
51 public class AnnotationRenderer
53 private static final int UPPER_TO_LOWER = 'a' - 'A'; // 32
55 private static final int CHAR_A = 'A'; // 65
57 private static final int CHAR_Z = 'Z'; // 90
60 * flag indicating if timing and redraw parameter info should be output
62 private final boolean debugRedraw;
64 private int charWidth, endRes, charHeight;
66 private boolean validCharWidth, hasHiddenColumns;
68 private FontMetrics fm;
70 private final boolean USE_FILL_ROUND_RECT = Platform.isAMacAndNotJS();
72 boolean av_renderHistogram = true, av_renderProfile = true,
73 av_normaliseProfile = false;
75 ResidueShaderI profcolour = null;
77 private ColumnSelection columnSelection;
79 private HiddenColumns hiddenColumns;
81 private ProfilesI hconsensus;
83 private Hashtable<String, Object>[] complementConsensus;
85 private Hashtable<String, Object>[] hStrucConsensus;
87 private boolean av_ignoreGapsConsensus;
90 * attributes set from AwtRenderPanelI
93 * old image used when data is currently being calculated and cannot be
96 private Image fadedImage;
99 * panel being rendered into
101 private ImageObserver annotationPanel;
104 * width of image to render in panel
106 private int imgWidth;
109 * offset to beginning of visible area
114 * offset to end of visible area
116 private int visHeight;
119 * indicate if the renderer should only render the visible portion of the
120 * annotation given the current view settings
122 private boolean useClip = true;
125 * master flag indicating if renderer should ever try to clip. not enabled for
128 private boolean canClip = false;
130 public AnnotationRenderer()
136 * Create a new annotation Renderer
139 * flag indicating if timing and redraw parameter info should be
142 public AnnotationRenderer(boolean debugRedraw)
144 this.debugRedraw = debugRedraw;
148 * Remove any references and resources when this object is no longer required
150 public void dispose()
152 hiddenColumns = null;
154 complementConsensus = null;
155 hStrucConsensus = null;
157 annotationPanel = null;
160 void drawStemAnnot(Graphics g, Annotation[] row_annotations, int lastSSX,
161 int x, int y, int iconOffset, int startRes, int column,
162 boolean validRes, boolean validEnd)
164 g.setColor(STEM_COLOUR);
165 int sCol = (lastSSX / charWidth)
166 + hiddenColumns.visibleToAbsoluteColumn(startRes);
168 int x2 = (x * charWidth);
170 char dc = (column == 0 || row_annotations[column - 1] == null) ? ' '
171 : row_annotations[column - 1].secondaryStructure;
173 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
174 || dc != row_annotations[sCol - 1].secondaryStructure;
175 boolean diffdownstream = !validRes || !validEnd
176 || row_annotations[column] == null
177 || dc != row_annotations[column].secondaryStructure;
179 if (column > 0 && Rna.isClosingParenthesis(dc))
182 // if (validRes && column>1 && row_annotations[column-2]!=null &&
183 // dc.equals(row_annotations[column-2].displayCharacter))
186 * if new annotation with a closing base pair half of the stem,
187 * display a backward arrow
189 g.fillPolygon(new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
191 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
202 // display a forward arrow
206 * if annotation ending with an opeing base pair half of the stem,
207 * display a forward arrow
209 g.fillPolygon(new int[] { x2 - 5, x2 - 5, x2 },
211 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
221 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
224 void drawNotCanonicalAnnot(Graphics g, Color nonCanColor,
225 Annotation[] row_annotations, int lastSSX, int x, int y,
226 int iconOffset, int startRes, int column, boolean validRes,
229 // System.out.println(nonCanColor);
231 g.setColor(nonCanColor);
232 int sCol = (lastSSX / charWidth)
233 + hiddenColumns.visibleToAbsoluteColumn(startRes);
235 int x2 = (x * charWidth);
237 String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
238 : row_annotations[column - 1].displayCharacter;
240 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
241 || !dc.equals(row_annotations[sCol - 1].displayCharacter);
242 boolean diffdownstream = !validRes || !validEnd
243 || row_annotations[column] == null
244 || !dc.equals(row_annotations[column].displayCharacter);
245 // System.out.println("Column "+column+" diff up: "+diffupstream+"
246 // down:"+diffdownstream);
247 // If a closing base pair half of the stem, display a backward arrow
248 if (column > 0 && Rna.isClosingParenthesis(dc))
252 // if (validRes && column>1 && row_annotations[column-2]!=null &&
253 // dc.equals(row_annotations[column-2].displayCharacter))
255 g.fillPolygon(new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
257 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
269 // display a forward arrow
272 g.fillPolygon(new int[] { x2 - 5, x2 - 5, x2 },
274 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
284 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
287 // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
289 public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
292 fm = annotPanel.getFontMetrics();
293 annotationPanel = annotPanel;
294 fadedImage = annotPanel.getFadedImage();
295 imgWidth = annotPanel.getFadedImageWidth();
296 // visible area for rendering
297 int[] bounds = annotPanel.getVisibleVRange();
301 visHeight = bounds[1];
316 updateFromAlignViewport(av);
319 public void updateFromAlignViewport(AlignViewportI av)
321 charWidth = av.getCharWidth();
322 endRes = av.getRanges().getEndRes();
323 charHeight = av.getCharHeight();
324 hasHiddenColumns = av.hasHiddenColumns();
325 validCharWidth = av.isValidCharWidth();
326 av_renderHistogram = av.isShowConsensusHistogram();
327 av_renderProfile = av.isShowSequenceLogo();
328 av_normaliseProfile = av.isNormaliseSequenceLogo();
329 profcolour = av.getResidueShading();
330 if (profcolour == null || profcolour.getColourScheme() == null)
333 * Use default colour for sequence logo if
334 * the alignment has no colourscheme set
335 * (would like to use user preference but n/a for applet)
337 ColourSchemeI col = av.getAlignment().isNucleotide()
338 ? new NucleotideColourScheme()
339 : new ZappoColourScheme();
340 profcolour = new ResidueShader(col);
342 columnSelection = av.getColumnSelection();
343 hiddenColumns = av.getAlignment().getHiddenColumns();
344 hconsensus = av.getSequenceConsensusHash();
345 complementConsensus = av.getComplementConsensusHash();
346 hStrucConsensus = av.getRnaStructureConsensusHash();
347 av_ignoreGapsConsensus = av.isIgnoreGapsConsensus();
351 * Returns profile data; the first element is the profile type, the second is
352 * the number of distinct values, the third the total count, and the remainder
353 * depend on the profile type.
359 int[] getProfileFor(AlignmentAnnotation aa, int column)
361 // TODO : consider refactoring the global alignment calculation
362 // properties/rendering attributes as a global 'alignment group' which holds
363 // all vis settings for the alignment as a whole rather than a subset
365 if (aa.autoCalculated && (aa.label.startsWith("Consensus")
366 || aa.label.startsWith("cDNA Consensus")))
368 boolean forComplement = aa.label.startsWith("cDNA Consensus");
369 if (aa.groupRef != null && aa.groupRef.consensusData != null
370 && aa.groupRef.isShowSequenceLogo())
372 // TODO? group consensus for cDNA complement
373 return AAFrequency.extractProfile(
374 aa.groupRef.consensusData.get(column),
375 aa.groupRef.getIgnoreGapsConsensus());
377 // TODO extend annotation row to enable dynamic and static profile data to
379 if (aa.groupRef == null && aa.sequenceRef == null)
383 return AAFrequency.extractCdnaProfile(complementConsensus[column],
384 av_ignoreGapsConsensus);
388 return AAFrequency.extractProfile(hconsensus.get(column),
389 av_ignoreGapsConsensus);
395 if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
397 // TODO implement group structure consensus
399 * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
400 * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
401 * group selections return StructureFrequency.extractProfile(
402 * aa.groupRef.consensusData[column], aa.groupRef
403 * .getIgnoreGapsConsensus()); }
405 // TODO extend annotation row to enable dynamic and static profile data
408 if (aa.groupRef == null && aa.sequenceRef == null
409 && hStrucConsensus != null
410 && hStrucConsensus.length > column)
412 return StructureFrequency.extractProfile(hStrucConsensus[column],
413 av_ignoreGapsConsensus);
421 * Render the annotation rows associated with an alignment.
426 * data and view settings to render
428 * destination for graphics
430 * row where a mouse event occured (or -1)
432 * first column that will be drawn
434 * last column that will be drawn
435 * @return true if the fadedImage was used for any alignment annotation rows
436 * currently being calculated
438 public boolean drawComponent(AwtRenderPanelI annotPanel,
439 AlignViewportI av, Graphics g, int activeRow, int startRes,
442 long stime = System.currentTimeMillis();
443 boolean usedFaded = false;
445 // AnnotationPanel needs to implement: ImageObserver, access to
447 updateFromAwtRenderPanel(annotPanel, av);
448 fm = g.getFontMetrics();
449 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
460 boolean validRes = false;
461 boolean validEnd = false;
462 boolean labelAllCols = false;
463 // boolean centreColLabels;
464 // boolean centreColLabelsDef = av.isCentreColumnLabels();
465 boolean scaleColLabel = false;
466 final AlignmentAnnotation consensusAnnot = av
467 .getAlignmentConsensusAnnotation();
468 final AlignmentAnnotation structConsensusAnnot = av
469 .getAlignmentStrucConsensusAnnotation();
470 final AlignmentAnnotation complementConsensusAnnot = av
471 .getComplementConsensusAnnotation();
473 BitSet graphGroupDrawn = new BitSet();
474 int charOffset = 0; // offset for a label
477 int yfrom = 0, f_i = 0, yto = 0, f_to = 0;
478 boolean clipst = false, clipend = false;
479 for (int i = 0; i < aa.length; i++)
481 AlignmentAnnotation row = aa[i];
482 boolean renderHistogram = true;
483 boolean renderProfile = false;
484 boolean normaliseProfile = false;
485 boolean isRNA = row.isRNA();
487 // check if this is a consensus annotation row and set the display
488 // settings appropriately
489 // TODO: generalise this to have render styles for consensus/profile
491 if (row.groupRef != null && row == row.groupRef.getConsensus())
493 renderHistogram = row.groupRef.isShowConsensusHistogram();
494 renderProfile = row.groupRef.isShowSequenceLogo();
495 normaliseProfile = row.groupRef.isNormaliseSequenceLogo();
497 else if (row == consensusAnnot || row == structConsensusAnnot
498 || row == complementConsensusAnnot)
500 renderHistogram = av_renderHistogram;
501 renderProfile = av_renderProfile;
502 normaliseProfile = av_normaliseProfile;
505 Annotation[] row_annotations = row.annotations;
510 // centreColLabels = row.centreColLabels || centreColLabelsDef;
511 labelAllCols = row.showAllColLabels;
512 scaleColLabel = row.scaleColLabel;
516 if (!useClip || ((y - charHeight) < visHeight
517 && (y + row.height + charHeight * 2) >= sOffset))
518 {// if_in_visible_region
529 if (row.graphGroup > -1 && graphGroupDrawn.get(row.graphGroup))
534 // this is so that we draw the characters below the graph
539 iconOffset = charHeight - fm.getDescent();
543 else if (row.hasText)
545 iconOffset = charHeight - fm.getDescent();
553 if (row.autoCalculated && av.isCalculationInProgress(row))
557 g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0,
558 y - row.height, imgWidth, y, annotationPanel);
559 g.setColor(Color.black);
560 // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
566 * else if (annotationPanel.av.updatingConservation &&
567 * aa[i].label.equals("Conservation")) {
569 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
570 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
571 * annotationPanel.imgWidth, y, annotationPanel);
573 * g.setColor(Color.black); //
574 * g.drawString("Calculating Conservation.....",20, y-row.height/2);
576 * continue; } else if (annotationPanel.av.updatingConservation &&
577 * aa[i].label.equals("Quality")) {
579 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
580 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
581 * annotationPanel.imgWidth, y, annotationPanel);
582 * g.setColor(Color.black); // /
583 * g.drawString("Calculating Quality....",20, y-row.height/2);
587 // first pass sets up state for drawing continuation from left-hand
590 x = (startRes == 0) ? 0 : -1;
591 while (x < endRes - startRes)
593 if (hasHiddenColumns)
595 column = hiddenColumns.visibleToAbsoluteColumn(startRes + x);
596 if (column > row_annotations.length - 1)
603 column = startRes + x;
606 if ((row_annotations == null)
607 || (row_annotations.length <= column)
608 || (row_annotations[column] == null))
616 final String displayChar = validRes
617 ? row_annotations[column].displayCharacter
623 g.setColor(Color.red);
625 if (columnSelection != null)
627 if (columnSelection.contains(column))
629 g.fillRect(x * charWidth, y, charWidth, charHeight);
633 if (row.getInvalidStrucPos() > x)
635 g.setColor(Color.orange);
636 g.fillRect(x * charWidth, y, charWidth, charHeight);
638 else if (row.getInvalidStrucPos() == x)
640 g.setColor(Color.orange.darker());
641 g.fillRect(x * charWidth, y, charWidth, charHeight);
643 if (validCharWidth && validRes && displayChar != null
644 && (displayChar.length() > 0))
646 Graphics2D gg = ((Graphics2D) g);
647 float fmWidth = fm.charsWidth(displayChar.toCharArray(), 0,
648 displayChar.length());
651 * shrink label width to fit in column, if that is
652 * both configured and necessary
654 boolean scaledToFit = false;
655 float fmScaling = 1f;
656 if (scaleColLabel && fmWidth > charWidth)
659 fmScaling = charWidth;
660 fmScaling /= fmWidth;
661 // and update the label's width to reflect the scaling.
665 charOffset = (int) ((charWidth - fmWidth) / 2f);
667 if (row_annotations[column].colour == null)
669 gg.setColor(Color.black);
673 gg.setColor(row_annotations[column].colour);
677 * draw the label, unless it is the same secondary structure
678 * symbol (excluding RNA Helix) as the previous column
680 final int xPos = (x * charWidth) + charOffset;
681 final int yPos = y + iconOffset;
684 * translate to drawing position _before_ applying any scaling
686 gg.translate(xPos, yPos);
690 * use a scaling transform to make the label narrower
691 * (JalviewJS doesn't have Font.deriveFont(AffineTransform))
694 AffineTransform.getScaleInstance(fmScaling, 1.0));
696 if (column == 0 || row.graph > 0)
698 gg.drawString(displayChar, 0, 0);
700 else if (row_annotations[column - 1] == null || (labelAllCols
701 || !displayChar.equals(
702 row_annotations[column - 1].displayCharacter)
703 || (displayChar.length() < 2
704 && row_annotations[column].secondaryStructure == ' ')))
706 gg.drawString(displayChar, 0, 0);
711 * undo scaling before translating back
712 * (restoring saved transform does NOT work in JS PDFGraphics!)
714 gg.transform(AffineTransform
715 .getScaleInstance(1D / fmScaling, 1.0));
717 gg.translate(-xPos, -yPos);
722 char ss = validRes ? row_annotations[column].secondaryStructure
727 // distinguish between forward/backward base-pairing
728 if (displayChar.indexOf(')') > -1)
737 if ((displayChar.indexOf(']') > -1))
745 // distinguish between forward/backward base-pairing
746 if (displayChar.indexOf('}') > -1)
754 // distinguish between forward/backward base-pairing
755 if (displayChar.indexOf('<') > -1)
761 if (isRNA && (ss >= CHAR_A) && (ss <= CHAR_Z))
763 // distinguish between forward/backward base-pairing
764 int ssLowerCase = ss + UPPER_TO_LOWER;
765 // TODO would .equals() be safer here? or charAt(0)?
766 if (displayChar.indexOf(ssLowerCase) > -1)
768 ss = (char) ssLowerCase;
772 if (!validRes || (ss != lastSS))
778 // int nb_annot = x - temp;
779 // System.out.println("\t type :"+lastSS+"\t x :"+x+"\t nbre
780 // annot :"+nb_annot);
783 case '(': // Stem case for RNA secondary structure
784 case ')': // and opposite direction
785 drawStemAnnot(g, row_annotations, lastSSX, x, y,
786 iconOffset, startRes, column, validRes, validEnd);
793 drawHelixAnnot(g, row_annotations, lastSSX, x, y,
794 iconOffset, startRes, column, validRes,
798 // no break if isRNA - falls through to drawNotCanonicalAnnot!
802 drawSheetAnnot(g, row_annotations, lastSSX, x, y,
803 iconOffset, startRes, column, validRes,
807 // no break if isRNA - fall through to drawNotCanonicalAnnot!
866 Color nonCanColor = getNotCanonicalColor(lastSS);
867 drawNotCanonicalAnnot(g, nonCanColor, row_annotations,
868 lastSSX, x, y, iconOffset, startRes, column,
873 g.setColor(Color.gray);
874 g.fillRect(lastSSX, y + 6 + iconOffset,
875 (x * charWidth) - lastSSX, 2);
890 lastSSX = (x * charWidth);
897 if (column >= row_annotations.length)
899 column = row_annotations.length - 1;
906 if ((row_annotations == null) || (row_annotations.length <= column)
907 || (row_annotations[column] == null))
925 drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
926 startRes, column, validRes, validEnd);
929 // no break if isRNA - fall through to drawNotCanonicalAnnot!
934 drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
935 startRes, column, validRes, validEnd);
938 // no break if isRNA - fall through to drawNotCanonicalAnnot!
941 case ')': // Stem case for RNA secondary structure
943 drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
944 startRes, column, validRes, validEnd);
1001 // System.out.println(lastSS);
1002 Color nonCanColor = getNotCanonicalColor(lastSS);
1003 drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX,
1004 x, y, iconOffset, startRes, column, validRes, validEnd);
1007 drawGlyphLine(g, row_annotations, lastSSX, x, y, iconOffset,
1008 startRes, column, validRes, validEnd);
1013 if (row.graph > 0 && row.graphHeight > 0)
1015 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1017 if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
1019 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1020 // computed efficiently for all visible labels
1021 float groupmax = -999999, groupmin = 9999999;
1022 for (int gg = 0; gg < aa.length; gg++)
1024 if (aa[gg].graphGroup != row.graphGroup)
1031 aa[gg].visible = false;
1033 if (aa[gg].graphMax > groupmax)
1035 groupmax = aa[gg].graphMax;
1037 if (aa[gg].graphMin < groupmin)
1039 groupmin = aa[gg].graphMin;
1043 for (int gg = 0; gg < aa.length; gg++)
1045 if (aa[gg].graphGroup == row.graphGroup)
1047 drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
1048 endRes, y, groupmin, groupmax, row.graphHeight);
1052 graphGroupDrawn.set(row.graphGroup);
1056 drawLineGraph(g, row, row_annotations, startRes, endRes, y,
1057 row.graphMin, row.graphMax, row.graphHeight);
1060 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1062 drawBarGraph(g, row, row_annotations, startRes, endRes,
1063 row.graphMin, row.graphMax, y, renderHistogram,
1064 renderProfile, normaliseProfile);
1070 if (clipst && !clipend)
1074 } // end if_in_visible_region
1075 if (row.graph > 0 && row.hasText)
1092 "Start clip at : " + yfrom + " (index " + f_i + ")");
1097 "End clip at : " + yto + " (index " + f_to + ")");
1101 System.err.println("Annotation Rendering time:"
1102 + (System.currentTimeMillis() - stime));
1109 public static final Color GLYPHLINE_COLOR = Color.gray;
1111 public static final Color SHEET_COLOUR = Color.green;
1113 public static final Color HELIX_COLOUR = Color.red;
1115 public static final Color STEM_COLOUR = Color.blue;
1117 // private Color sdNOTCANONICAL_COLOUR;
1119 void drawGlyphLine(Graphics g, Annotation[] row, int lastSSX, int x,
1120 int y, int iconOffset, int startRes, int column, boolean validRes,
1123 g.setColor(GLYPHLINE_COLOR);
1124 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
1127 void drawSheetAnnot(Graphics g, Annotation[] row,
1129 int lastSSX, int x, int y, int iconOffset, int startRes,
1130 int column, boolean validRes, boolean validEnd)
1132 g.setColor(SHEET_COLOUR);
1134 if (!validEnd || !validRes || row == null || row[column] == null
1135 || row[column].secondaryStructure != 'E')
1137 g.fillRect(lastSSX, y + 4 + iconOffset, (x * charWidth) - lastSSX - 4,
1141 { (x * charWidth) - 4, (x * charWidth) - 4, (x * charWidth) },
1143 { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset },
1148 g.fillRect(lastSSX, y + 4 + iconOffset, (x + 1) * charWidth - lastSSX,
1154 void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x,
1155 int y, int iconOffset, int startRes, int column, boolean validRes,
1158 g.setColor(HELIX_COLOUR);
1160 int sCol = (lastSSX / charWidth)
1161 + hiddenColumns.visibleToAbsoluteColumn(startRes);
1163 int x2 = (x * charWidth);
1165 if (USE_FILL_ROUND_RECT)
1167 int ofs = charWidth / 2;
1168 // Off by 1 offset when drawing rects and ovals
1169 // to offscreen image on the MAC
1170 g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1, 8, 8, 8);
1171 if (sCol == 0 || row[sCol - 1] == null
1172 || row[sCol - 1].secondaryStructure != 'H')
1177 // g.setColor(Color.orange);
1178 g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1 - ofs + 1, 8,
1181 if (!validRes || row[column] == null
1182 || row[column].secondaryStructure != 'H')
1188 // g.setColor(Color.magenta);
1189 g.fillRoundRect(lastSSX + ofs, y + 4 + iconOffset,
1190 x2 - x1 - ofs + 1, 8, 0, 0);
1197 if (sCol == 0 || row[sCol - 1] == null
1198 || row[sCol - 1].secondaryStructure != 'H')
1200 g.fillArc(lastSSX, y + 4 + iconOffset, charWidth, 8, 90, 180);
1201 x1 += charWidth / 2;
1204 if (!validRes || row[column] == null
1205 || row[column].secondaryStructure != 'H')
1207 g.fillArc((x * charWidth) - charWidth, y + 4 + iconOffset, charWidth,
1209 x2 -= charWidth / 2;
1212 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
1215 void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1216 Annotation[] aa_annotations, int sRes, int eRes, int y, float min,
1217 float max, int graphHeight)
1219 if (sRes > aa_annotations.length)
1226 // Adjustment for fastpaint to left
1232 eRes = Math.min(eRes, aa_annotations.length);
1240 float range = max - min;
1245 y2 = y - (int) ((0 - min / range) * graphHeight);
1248 g.setColor(Color.gray);
1249 g.drawLine(x - charWidth, y2, (eRes - sRes + 1) * charWidth, y2);
1251 eRes = Math.min(eRes, aa_annotations.length);
1254 int aaMax = aa_annotations.length - 1;
1256 while (x < eRes - sRes)
1259 if (hasHiddenColumns)
1261 column = hiddenColumns.visibleToAbsoluteColumn(column);
1269 if (aa_annotations[column] == null
1270 || aa_annotations[column - 1] == null)
1276 if (aa_annotations[column].colour == null)
1278 g.setColor(Color.black);
1282 g.setColor(aa_annotations[column].colour);
1285 y1 = y - (int) (((aa_annotations[column - 1].value - min) / range)
1287 y2 = y - (int) (((aa_annotations[column].value - min) / range)
1290 g.drawLine(x * charWidth - charWidth / 2, y1,
1291 x * charWidth + charWidth / 2, y2);
1295 if (_aa.threshold != null)
1297 g.setColor(_aa.threshold.colour);
1298 Graphics2D g2 = (Graphics2D) g;
1299 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1300 BasicStroke.JOIN_ROUND, 3f, new float[]
1303 y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1304 g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1305 g2.setStroke(new BasicStroke());
1309 @SuppressWarnings("unused")
1310 void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1311 Annotation[] aa_annotations, int sRes, int eRes, float min,
1312 float max, int y, boolean renderHistogram, boolean renderProfile,
1313 boolean normaliseProfile)
1315 if (sRes > aa_annotations.length)
1319 Font ofont = g.getFont();
1320 eRes = Math.min(eRes, aa_annotations.length);
1322 int x = 0, y1 = y, y2 = y;
1324 float range = max - min;
1328 y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1331 g.setColor(Color.gray);
1333 g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
1336 int aaMax = aa_annotations.length - 1;
1337 while (x < eRes - sRes)
1340 if (hasHiddenColumns)
1342 column = hiddenColumns.visibleToAbsoluteColumn(column);
1350 if (aa_annotations[column] == null)
1355 if (aa_annotations[column].colour == null)
1357 g.setColor(Color.black);
1361 g.setColor(aa_annotations[column].colour);
1364 y1 = y - (int) (((aa_annotations[column].value - min) / (range))
1367 if (renderHistogram)
1371 g.fillRect(x * charWidth, y2, charWidth, y1 - y2);
1375 g.fillRect(x * charWidth, y1, charWidth, y2 - y1);
1378 // draw profile if available
1383 * {profile type, #values, total count, char1, pct1, char2, pct2...}
1385 int profl[] = getProfileFor(_aa, column);
1387 // just try to draw the logo if profl is not null
1388 if (profl != null && profl[2] != 0)
1390 boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
1391 boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
1392 float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1393 final double normaliseFactor = normaliseProfile ? _aa.graphHeight
1397 * Render a single base for a sequence profile, a base pair for
1398 * structure profile, and a triplet for a cdna profile
1400 char[] dc = new char[isStructureProfile ? 2
1401 : (isCdnaProfile ? 3 : 1)];
1403 // lm is not necessary - we can just use fm - could be off by no more
1405 // LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1406 // System.out.println(asc + " " + dec + " " + (asc - lm.getAscent())
1407 // + " " + (dec - lm.getDescent()));
1409 double asc = fm.getAscent();
1410 double dec = fm.getDescent();
1411 double fht = fm.getHeight();
1413 double scale = 1f / (normaliseProfile ? profl[2] : 100f);
1414 // float ofontHeight = 1f / fm.getAscent();// magnify to fill box
1417 * Traverse the character(s)/percentage data in the array
1422 // profl[1] is the number of values in the profile
1423 for (int i = 0, c = 3, last = profl[1]; i < last; i++)
1427 if (isStructureProfile)
1429 // todo can we encode a structure pair as an int, like codons?
1430 dc[0] = (char) profl[c++];
1431 dc[1] = (char) profl[c++];
1434 else if (isCdnaProfile)
1436 CodingUtils.decodeCodon2(profl[c++], dc);
1441 dc[0] = (char) profl[c++];
1444 // next profl[] position is profile % for the character(s)
1446 int percent = profl[c++];
1449 // failsafe in case a count rounds down to 0%
1452 double newHeight = normaliseFactor * scale * percent;
1455 * Set character colour as per alignment colour scheme; use the
1456 * codon translation if a cDNA profile
1458 Color colour = null;
1461 final String codonTranslation = ResidueProperties
1463 colour = profcolour.findColour(codonTranslation.charAt(0),
1468 colour = profcolour.findColour(dc[0], column, null);
1470 g.setColor(colour == Color.white ? Color.lightGray : colour);
1472 // Debug - render boxes around characters
1473 // g.setColor(Color.red);
1474 // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
1476 // g.setColor(profcolour.findColour(dc[0]).darker());
1478 double sx = 1f * charWidth / fm.charsWidth(dc, 0, dc.length);
1479 double sy = newHeight / asc;
1480 double newAsc = asc * sy;
1481 double newDec = dec * sy;
1482 // it is not necessary to recalculate lm for the new font.
1483 // note: lm.getBaselineOffsets()[lm.getBaselineIndex()]) must be 0
1484 // by definition. Was:
1485 // int hght = (int) (ht + (newAsc - newDec);
1486 // - lm.getBaselineOffsets()[lm.getBaselineIndex()]));
1488 if (Platform.isJS())
1491 * SwingJS does not implement font.deriveFont()
1492 * so use a scaling transform to draw instead,
1493 * this is off by a very small amount
1495 final int hght = (int) (ht2 + (newAsc - newDec));
1496 Graphics2D gg = (Graphics2D) g;
1497 int xShift = (int) Math.round(x * charWidth / sx);
1498 int yShift = (int) Math.round(hght / sy);
1499 gg.transform(AffineTransform.getScaleInstance(sx, sy));
1500 gg.drawString(s, xShift, yShift);
1502 AffineTransform.getScaleInstance(1D / sx, 1D / sy));
1512 // Java ('normal') method is to scale the font to fit
1514 final int hght = (int) (ht + (newAsc - newDec));
1516 .deriveFont(AffineTransform.getScaleInstance(sx, sy));
1518 g.drawChars(dc, 0, dc.length, x * charWidth, hght);
1528 if (_aa.threshold != null)
1530 g.setColor(_aa.threshold.colour);
1531 Graphics2D g2 = (Graphics2D) g;
1532 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1533 BasicStroke.JOIN_ROUND, 3f, new float[]
1537 - ((_aa.threshold.value - min) / range) * _aa.graphHeight);
1538 g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1539 g2.setStroke(new BasicStroke());
1543 // used by overview window
1544 public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1545 Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1547 eRes = Math.min(eRes, aa_annotations.length);
1548 g.setColor(Color.white);
1549 g.fillRect(0, 0, width, y);
1550 g.setColor(new Color(0, 0, 180));
1554 for (int j = sRes; j < eRes; j++)
1556 if (aa_annotations[j] != null)
1558 if (aa_annotations[j].colour == null)
1560 g.setColor(Color.black);
1564 g.setColor(aa_annotations[j].colour);
1567 height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1573 g.fillRect(x, y - height, charWidth, height);
1579 Color getNotCanonicalColor(char lastss)
1585 return new Color(255, 125, 5);
1589 return new Color(245, 115, 10);
1593 return new Color(235, 135, 15);
1597 return new Color(225, 105, 20);
1601 return new Color(215, 145, 30);
1605 return new Color(205, 95, 35);
1609 return new Color(195, 155, 45);
1613 return new Color(185, 85, 55);
1617 return new Color(175, 165, 65);
1621 return new Color(170, 75, 75);
1625 return new Color(160, 175, 85);
1629 return new Color(150, 65, 95);
1633 return new Color(140, 185, 105);
1637 return new Color(130, 55, 110);
1641 return new Color(120, 195, 120);
1645 return new Color(110, 45, 130);
1649 return new Color(100, 205, 140);
1653 return new Color(90, 35, 150);
1657 return new Color(85, 215, 160);
1661 return new Color(75, 25, 170);
1665 return new Color(65, 225, 180);
1669 return new Color(55, 15, 185);
1673 return new Color(45, 235, 195);
1677 return new Color(35, 5, 205);
1681 return new Color(25, 245, 215);
1685 return new Color(15, 0, 225);
1689 return new Color(10, 255, 235);
1693 return new Color(5, 150, 245);
1697 return new Color(0, 80, 255);
1700 System.out.println("This is not a interaction : " + lastss);