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[] complementConsensus;
85 private Hashtable[] 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()
153 complementConsensus = null;
154 hStrucConsensus = null;
156 annotationPanel = null;
159 void drawStemAnnot(Graphics g, Annotation[] row_annotations, int lastSSX,
160 int x, int y, int iconOffset, int startRes, int column,
161 boolean validRes, boolean validEnd)
163 g.setColor(STEM_COLOUR);
164 int sCol = (lastSSX / charWidth)
165 + hiddenColumns.visibleToAbsoluteColumn(startRes);
167 int x2 = (x * charWidth);
169 char dc = (column == 0 || row_annotations[column - 1] == null) ? ' '
170 : row_annotations[column - 1].secondaryStructure;
172 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
173 || dc != row_annotations[sCol - 1].secondaryStructure;
174 boolean diffdownstream = !validRes || !validEnd
175 || row_annotations[column] == null
176 || dc != row_annotations[column].secondaryStructure;
178 if (column > 0 && Rna.isClosingParenthesis(dc))
181 // if (validRes && column>1 && row_annotations[column-2]!=null &&
182 // dc.equals(row_annotations[column-2].displayCharacter))
185 * if new annotation with a closing base pair half of the stem,
186 * display a backward arrow
188 g.fillPolygon(new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
190 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
201 // display a forward arrow
205 * if annotation ending with an opeing base pair half of the stem,
206 * display a forward arrow
208 g.fillPolygon(new int[] { x2 - 5, x2 - 5, x2 },
210 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
220 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
223 void drawNotCanonicalAnnot(Graphics g, Color nonCanColor,
224 Annotation[] row_annotations, int lastSSX, int x, int y,
225 int iconOffset, int startRes, int column, boolean validRes,
228 // System.out.println(nonCanColor);
230 g.setColor(nonCanColor);
231 int sCol = (lastSSX / charWidth)
232 + hiddenColumns.visibleToAbsoluteColumn(startRes);
234 int x2 = (x * charWidth);
236 String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
237 : row_annotations[column - 1].displayCharacter;
239 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
240 || !dc.equals(row_annotations[sCol - 1].displayCharacter);
241 boolean diffdownstream = !validRes || !validEnd
242 || row_annotations[column] == null
243 || !dc.equals(row_annotations[column].displayCharacter);
244 // System.out.println("Column "+column+" diff up: "+diffupstream+"
245 // down:"+diffdownstream);
246 // If a closing base pair half of the stem, display a backward arrow
247 if (column > 0 && Rna.isClosingParenthesis(dc))
251 // if (validRes && column>1 && row_annotations[column-2]!=null &&
252 // dc.equals(row_annotations[column-2].displayCharacter))
254 g.fillPolygon(new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
256 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
268 // display a forward arrow
271 g.fillPolygon(new int[] { x2 - 5, x2 - 5, x2 },
273 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
283 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
286 // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
288 public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
291 fm = annotPanel.getFontMetrics();
292 annotationPanel = annotPanel;
293 fadedImage = annotPanel.getFadedImage();
294 imgWidth = annotPanel.getFadedImageWidth();
295 // visible area for rendering
296 int[] bounds = annotPanel.getVisibleVRange();
300 visHeight = bounds[1];
315 updateFromAlignViewport(av);
318 public void updateFromAlignViewport(AlignViewportI av)
320 charWidth = av.getCharWidth();
321 endRes = av.getRanges().getEndRes();
322 charHeight = av.getCharHeight();
323 hasHiddenColumns = av.hasHiddenColumns();
324 validCharWidth = av.isValidCharWidth();
325 av_renderHistogram = av.isShowConsensusHistogram();
326 av_renderProfile = av.isShowSequenceLogo();
327 av_normaliseProfile = av.isNormaliseSequenceLogo();
328 profcolour = av.getResidueShading();
329 if (profcolour == null || profcolour.getColourScheme() == null)
332 * Use default colour for sequence logo if
333 * the alignment has no colourscheme set
334 * (would like to use user preference but n/a for applet)
336 ColourSchemeI col = av.getAlignment().isNucleotide()
337 ? new NucleotideColourScheme()
338 : new ZappoColourScheme();
339 profcolour = new ResidueShader(col);
341 columnSelection = av.getColumnSelection();
342 hiddenColumns = av.getAlignment().getHiddenColumns();
343 hconsensus = av.getSequenceConsensusHash();
344 complementConsensus = av.getComplementConsensusHash();
345 hStrucConsensus = av.getRnaStructureConsensusHash();
346 av_ignoreGapsConsensus = av.isIgnoreGapsConsensus();
350 * Returns profile data; the first element is the profile type, the second is
351 * the number of distinct values, the third the total count, and the remainder
352 * depend on the profile type.
358 int[] getProfileFor(AlignmentAnnotation aa, int column)
360 // TODO : consider refactoring the global alignment calculation
361 // properties/rendering attributes as a global 'alignment group' which holds
362 // all vis settings for the alignment as a whole rather than a subset
364 if (aa.autoCalculated && (aa.label.startsWith("Consensus")
365 || aa.label.startsWith("cDNA Consensus")))
367 boolean forComplement = aa.label.startsWith("cDNA Consensus");
368 if (aa.groupRef != null && aa.groupRef.consensusData != null
369 && aa.groupRef.isShowSequenceLogo())
371 // TODO? group consensus for cDNA complement
372 return AAFrequency.extractProfile(
373 aa.groupRef.consensusData.get(column),
374 aa.groupRef.getIgnoreGapsConsensus());
376 // TODO extend annotation row to enable dynamic and static profile data to
378 if (aa.groupRef == null && aa.sequenceRef == null)
382 return AAFrequency.extractCdnaProfile(complementConsensus[column],
383 av_ignoreGapsConsensus);
387 return AAFrequency.extractProfile(hconsensus.get(column),
388 av_ignoreGapsConsensus);
394 if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
396 // TODO implement group structure consensus
398 * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
399 * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
400 * group selections return StructureFrequency.extractProfile(
401 * aa.groupRef.consensusData[column], aa.groupRef
402 * .getIgnoreGapsConsensus()); }
404 // TODO extend annotation row to enable dynamic and static profile data
407 if (aa.groupRef == null && aa.sequenceRef == null
408 && hStrucConsensus != null
409 && hStrucConsensus.length > column)
411 return StructureFrequency.extractProfile(hStrucConsensus[column],
412 av_ignoreGapsConsensus);
422 * Render the annotation rows associated with an alignment.
427 * data and view settings to render
429 * destination for graphics
431 * row where a mouse event occured (or -1)
433 * first column that will be drawn
435 * last column that will be drawn
436 * @return true if the fadedImage was used for any alignment annotation rows
437 * currently being calculated
439 public boolean drawComponent(AwtRenderPanelI annotPanel,
440 AlignViewportI av, Graphics g, int activeRow, int startRes,
443 long stime = System.currentTimeMillis();
444 boolean usedFaded = false;
446 // AnnotationPanel needs to implement: ImageObserver, access to
448 updateFromAwtRenderPanel(annotPanel, av);
449 fm = g.getFontMetrics();
450 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
461 boolean validRes = false;
462 boolean validEnd = false;
463 boolean labelAllCols = false;
464 boolean centreColLabels;
465 boolean centreColLabelsDef = av.isCentreColumnLabels();
466 boolean scaleColLabel = false;
467 final AlignmentAnnotation consensusAnnot = av
468 .getAlignmentConsensusAnnotation();
469 final AlignmentAnnotation structConsensusAnnot = av
470 .getAlignmentStrucConsensusAnnotation();
471 final AlignmentAnnotation complementConsensusAnnot = av
472 .getComplementConsensusAnnotation();
473 boolean renderHistogram = true, renderProfile = true,
474 normaliseProfile = false, isRNA = rna;
476 BitSet graphGroupDrawn = new BitSet();
477 int charOffset = 0; // offset for a label
480 int yfrom = 0, f_i = 0, yto = 0, f_to = 0;
481 boolean clipst = false, clipend = false;
482 for (int i = 0; i < aa.length; i++)
484 AlignmentAnnotation row = aa[i];
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;
506 renderHistogram = true;
507 // don't need to set render/normaliseProfile since they are not
508 // currently used in any other annotation track renderer
511 Annotation[] row_annotations = row.annotations;
516 centreColLabels = row.centreColLabels || centreColLabelsDef;
517 labelAllCols = row.showAllColLabels;
518 scaleColLabel = row.scaleColLabel;
522 if (!useClip || ((y - charHeight) < visHeight
523 && (y + row.height + charHeight * 2) >= sOffset))
524 {// if_in_visible_region
535 if (row.graphGroup > -1 && graphGroupDrawn.get(row.graphGroup))
540 // this is so that we draw the characters below the graph
545 iconOffset = charHeight - fm.getDescent();
549 else if (row.hasText)
551 iconOffset = charHeight - fm.getDescent();
559 if (row.autoCalculated && av.isCalculationInProgress(row))
563 g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0,
564 y - row.height, imgWidth, y, annotationPanel);
565 g.setColor(Color.black);
566 // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
572 * else if (annotationPanel.av.updatingConservation &&
573 * aa[i].label.equals("Conservation")) {
575 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
576 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
577 * annotationPanel.imgWidth, y, annotationPanel);
579 * g.setColor(Color.black); //
580 * g.drawString("Calculating Conservation.....",20, y-row.height/2);
582 * continue; } else if (annotationPanel.av.updatingConservation &&
583 * aa[i].label.equals("Quality")) {
585 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
586 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
587 * annotationPanel.imgWidth, y, annotationPanel);
588 * g.setColor(Color.black); // /
589 * g.drawString("Calculating Quality....",20, y-row.height/2);
593 // first pass sets up state for drawing continuation from left-hand
596 x = (startRes == 0) ? 0 : -1;
597 while (x < endRes - startRes)
599 if (hasHiddenColumns)
601 column = hiddenColumns.visibleToAbsoluteColumn(startRes + x);
602 if (column > row_annotations.length - 1)
609 column = startRes + x;
612 if ((row_annotations == null)
613 || (row_annotations.length <= column)
614 || (row_annotations[column] == null))
622 final String displayChar = validRes
623 ? row_annotations[column].displayCharacter
629 g.setColor(Color.red);
631 if (columnSelection != null)
633 if (columnSelection.contains(column))
635 g.fillRect(x * charWidth, y, charWidth, charHeight);
639 if (row.getInvalidStrucPos() > x)
641 g.setColor(Color.orange);
642 g.fillRect(x * charWidth, y, charWidth, charHeight);
644 else if (row.getInvalidStrucPos() == x)
646 g.setColor(Color.orange.darker());
647 g.fillRect(x * charWidth, y, charWidth, charHeight);
649 if (validCharWidth && validRes && displayChar != null
650 && (displayChar.length() > 0))
652 Graphics2D gg = ((Graphics2D) g);
653 float fmWidth = fm.charsWidth(displayChar.toCharArray(), 0,
654 displayChar.length());
657 * shrink label width to fit in column, if that is
658 * both configured and necessary
660 boolean scaledToFit = false;
661 float fmScaling = 1f;
662 if (scaleColLabel && fmWidth > charWidth)
665 fmScaling = charWidth;
666 fmScaling /= fmWidth;
667 // and update the label's width to reflect the scaling.
671 charOffset = (int) ((charWidth - fmWidth) / 2f);
673 if (row_annotations[column].colour == null)
675 gg.setColor(Color.black);
679 gg.setColor(row_annotations[column].colour);
683 * draw the label, unless it is the same secondary structure
684 * symbol (excluding RNA Helix) as the previous column
686 final int xPos = (x * charWidth) + charOffset;
687 final int yPos = y + iconOffset;
690 * translate to drawing position _before_ applying any scaling
692 gg.translate(xPos, yPos);
696 * use a scaling transform to make the label narrower
697 * (JalviewJS doesn't have Font.deriveFont(AffineTransform))
700 AffineTransform.getScaleInstance(fmScaling, 1.0));
702 if (column == 0 || row.graph > 0)
704 gg.drawString(displayChar, 0, 0);
706 else if (row_annotations[column - 1] == null || (labelAllCols
707 || !displayChar.equals(
708 row_annotations[column - 1].displayCharacter)
709 || (displayChar.length() < 2
710 && row_annotations[column].secondaryStructure == ' ')))
712 gg.drawString(displayChar, 0, 0);
717 * undo scaling before translating back
718 * (restoring saved transform does NOT work in JS PDFGraphics!)
720 gg.transform(AffineTransform
721 .getScaleInstance(1D / fmScaling, 1.0));
723 gg.translate(-xPos, -yPos);
728 char ss = validRes ? row_annotations[column].secondaryStructure
733 // distinguish between forward/backward base-pairing
734 if (displayChar.indexOf(')') > -1)
743 if ((displayChar.indexOf(']') > -1))
751 // distinguish between forward/backward base-pairing
752 if (displayChar.indexOf('}') > -1)
760 // distinguish between forward/backward base-pairing
761 if (displayChar.indexOf('<') > -1)
767 if (isRNA && (ss >= CHAR_A) && (ss <= CHAR_Z))
769 // distinguish between forward/backward base-pairing
770 int ssLowerCase = ss + UPPER_TO_LOWER;
771 // TODO would .equals() be safer here? or charAt(0)?
772 if (displayChar.indexOf(ssLowerCase) > -1)
774 ss = (char) ssLowerCase;
778 if (!validRes || (ss != lastSS))
784 int nb_annot = x - temp;
785 // System.out.println("\t type :"+lastSS+"\t x :"+x+"\t nbre
786 // annot :"+nb_annot);
789 case '(': // Stem case for RNA secondary structure
790 case ')': // and opposite direction
791 drawStemAnnot(g, row_annotations, lastSSX, x, y,
792 iconOffset, startRes, column, validRes, validEnd);
799 drawHelixAnnot(g, row_annotations, lastSSX, x, y,
800 iconOffset, startRes, column, validRes,
804 // no break if isRNA - falls through to drawNotCanonicalAnnot!
808 drawSheetAnnot(g, row_annotations, lastSSX, x, y,
809 iconOffset, startRes, column, validRes,
813 // no break if isRNA - fall through to drawNotCanonicalAnnot!
872 Color nonCanColor = getNotCanonicalColor(lastSS);
873 drawNotCanonicalAnnot(g, nonCanColor, row_annotations,
874 lastSSX, x, y, iconOffset, startRes, column,
879 g.setColor(Color.gray);
880 g.fillRect(lastSSX, y + 6 + iconOffset,
881 (x * charWidth) - lastSSX, 2);
896 lastSSX = (x * charWidth);
903 if (column >= row_annotations.length)
905 column = row_annotations.length - 1;
912 if ((row_annotations == null) || (row_annotations.length <= column)
913 || (row_annotations[column] == null))
931 drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
932 startRes, column, validRes, validEnd);
935 // no break if isRNA - fall through to drawNotCanonicalAnnot!
940 drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
941 startRes, column, validRes, validEnd);
944 // no break if isRNA - fall through to drawNotCanonicalAnnot!
947 case ')': // Stem case for RNA secondary structure
949 drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
950 startRes, column, validRes, validEnd);
1007 // System.out.println(lastSS);
1008 Color nonCanColor = getNotCanonicalColor(lastSS);
1009 drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX,
1010 x, y, iconOffset, startRes, column, validRes, validEnd);
1013 drawGlyphLine(g, row_annotations, lastSSX, x, y, iconOffset,
1014 startRes, column, validRes, validEnd);
1019 if (row.graph > 0 && row.graphHeight > 0)
1021 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1023 if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
1025 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1026 // computed efficiently for all visible labels
1027 float groupmax = -999999, groupmin = 9999999;
1028 for (int gg = 0; gg < aa.length; gg++)
1030 if (aa[gg].graphGroup != row.graphGroup)
1037 aa[gg].visible = false;
1039 if (aa[gg].graphMax > groupmax)
1041 groupmax = aa[gg].graphMax;
1043 if (aa[gg].graphMin < groupmin)
1045 groupmin = aa[gg].graphMin;
1049 for (int gg = 0; gg < aa.length; gg++)
1051 if (aa[gg].graphGroup == row.graphGroup)
1053 drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
1054 endRes, y, groupmin, groupmax, row.graphHeight);
1058 graphGroupDrawn.set(row.graphGroup);
1062 drawLineGraph(g, row, row_annotations, startRes, endRes, y,
1063 row.graphMin, row.graphMax, row.graphHeight);
1066 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1068 drawBarGraph(g, row, row_annotations, startRes, endRes,
1069 row.graphMin, row.graphMax, y, renderHistogram,
1070 renderProfile, normaliseProfile);
1076 if (clipst && !clipend)
1080 } // end if_in_visible_region
1081 if (row.graph > 0 && row.hasText)
1098 "Start clip at : " + yfrom + " (index " + f_i + ")");
1103 "End clip at : " + yto + " (index " + f_to + ")");
1107 System.err.println("Annotation Rendering time:"
1108 + (System.currentTimeMillis() - stime));
1115 public static final Color GLYPHLINE_COLOR = Color.gray;
1117 public static final Color SHEET_COLOUR = Color.green;
1119 public static final Color HELIX_COLOUR = Color.red;
1121 public static final Color STEM_COLOUR = Color.blue;
1123 private Color sdNOTCANONICAL_COLOUR;
1125 void drawGlyphLine(Graphics g, Annotation[] row, int lastSSX, int x,
1126 int y, int iconOffset, int startRes, int column, boolean validRes,
1129 g.setColor(GLYPHLINE_COLOR);
1130 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
1133 void drawSheetAnnot(Graphics g, Annotation[] row,
1135 int lastSSX, int x, int y, int iconOffset, int startRes,
1136 int column, boolean validRes, boolean validEnd)
1138 g.setColor(SHEET_COLOUR);
1140 if (!validEnd || !validRes || row == null || row[column] == null
1141 || row[column].secondaryStructure != 'E')
1143 g.fillRect(lastSSX, y + 4 + iconOffset, (x * charWidth) - lastSSX - 4,
1147 { (x * charWidth) - 4, (x * charWidth) - 4, (x * charWidth) },
1149 { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset },
1154 g.fillRect(lastSSX, y + 4 + iconOffset, (x + 1) * charWidth - lastSSX,
1160 void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x,
1161 int y, int iconOffset, int startRes, int column, boolean validRes,
1164 g.setColor(HELIX_COLOUR);
1166 int sCol = (lastSSX / charWidth)
1167 + hiddenColumns.visibleToAbsoluteColumn(startRes);
1169 int x2 = (x * charWidth);
1171 if (USE_FILL_ROUND_RECT)
1173 int ofs = charWidth / 2;
1174 // Off by 1 offset when drawing rects and ovals
1175 // to offscreen image on the MAC
1176 g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1, 8, 8, 8);
1177 if (sCol == 0 || row[sCol - 1] == null
1178 || row[sCol - 1].secondaryStructure != 'H')
1183 // g.setColor(Color.orange);
1184 g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1 - ofs + 1, 8,
1187 if (!validRes || row[column] == null
1188 || row[column].secondaryStructure != 'H')
1194 // g.setColor(Color.magenta);
1195 g.fillRoundRect(lastSSX + ofs, y + 4 + iconOffset,
1196 x2 - x1 - ofs + 1, 8, 0, 0);
1203 if (sCol == 0 || row[sCol - 1] == null
1204 || row[sCol - 1].secondaryStructure != 'H')
1206 g.fillArc(lastSSX, y + 4 + iconOffset, charWidth, 8, 90, 180);
1207 x1 += charWidth / 2;
1210 if (!validRes || row[column] == null
1211 || row[column].secondaryStructure != 'H')
1213 g.fillArc((x * charWidth) - charWidth, y + 4 + iconOffset, charWidth,
1215 x2 -= charWidth / 2;
1218 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
1221 void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1222 Annotation[] aa_annotations, int sRes, int eRes, int y, float min,
1223 float max, int graphHeight)
1225 if (sRes > aa_annotations.length)
1232 // Adjustment for fastpaint to left
1238 eRes = Math.min(eRes, aa_annotations.length);
1246 float range = max - min;
1251 y2 = y - (int) ((0 - min / range) * graphHeight);
1254 g.setColor(Color.gray);
1255 g.drawLine(x - charWidth, y2, (eRes - sRes + 1) * charWidth, y2);
1257 eRes = Math.min(eRes, aa_annotations.length);
1260 int aaMax = aa_annotations.length - 1;
1262 while (x < eRes - sRes)
1265 if (hasHiddenColumns)
1267 column = hiddenColumns.visibleToAbsoluteColumn(column);
1275 if (aa_annotations[column] == null
1276 || aa_annotations[column - 1] == null)
1282 if (aa_annotations[column].colour == null)
1284 g.setColor(Color.black);
1288 g.setColor(aa_annotations[column].colour);
1291 y1 = y - (int) (((aa_annotations[column - 1].value - min) / range)
1293 y2 = y - (int) (((aa_annotations[column].value - min) / range)
1296 g.drawLine(x * charWidth - charWidth / 2, y1,
1297 x * charWidth + charWidth / 2, y2);
1301 if (_aa.threshold != null)
1303 g.setColor(_aa.threshold.colour);
1304 Graphics2D g2 = (Graphics2D) g;
1305 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1306 BasicStroke.JOIN_ROUND, 3f, new float[]
1309 y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1310 g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1311 g2.setStroke(new BasicStroke());
1315 @SuppressWarnings("unused")
1316 void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1317 Annotation[] aa_annotations, int sRes, int eRes, float min,
1318 float max, int y, boolean renderHistogram, boolean renderProfile,
1319 boolean normaliseProfile)
1321 if (sRes > aa_annotations.length)
1325 Font ofont = g.getFont();
1326 eRes = Math.min(eRes, aa_annotations.length);
1328 int x = 0, y1 = y, y2 = y;
1330 float range = max - min;
1334 y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1337 g.setColor(Color.gray);
1339 g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
1342 int aaMax = aa_annotations.length - 1;
1343 while (x < eRes - sRes)
1346 if (hasHiddenColumns)
1348 column = hiddenColumns.visibleToAbsoluteColumn(column);
1356 if (aa_annotations[column] == null)
1361 if (aa_annotations[column].colour == null)
1363 g.setColor(Color.black);
1367 g.setColor(aa_annotations[column].colour);
1370 y1 = y - (int) (((aa_annotations[column].value - min) / (range))
1373 if (renderHistogram)
1377 g.fillRect(x * charWidth, y2, charWidth, y1 - y2);
1381 g.fillRect(x * charWidth, y1, charWidth, y2 - y1);
1384 // draw profile if available
1389 * {profile type, #values, total count, char1, pct1, char2, pct2...}
1391 int profl[] = getProfileFor(_aa, column);
1393 // just try to draw the logo if profl is not null
1394 if (profl != null && profl[2] != 0)
1396 boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
1397 boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
1398 float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1399 final double normaliseFactor = normaliseProfile ? _aa.graphHeight : (y2 - y1);
1402 * Render a single base for a sequence profile, a base pair for
1403 * structure profile, and a triplet for a cdna profile
1405 char[] dc = new char[isStructureProfile ? 2
1406 : (isCdnaProfile ? 3 : 1)];
1408 // lm is not necessary - we can just use fm - could be off by no more
1410 // LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1411 // System.out.println(asc + " " + dec + " " + (asc - lm.getAscent())
1412 // + " " + (dec - lm.getDescent()));
1414 double asc = fm.getAscent();
1415 double dec = fm.getDescent();
1416 double fht = fm.getHeight();
1418 double scale = 1f / (normaliseProfile ? profl[2] : 100f);
1419 // float ofontHeight = 1f / fm.getAscent();// magnify to fill box
1422 * Traverse the character(s)/percentage data in the array
1427 // profl[1] is the number of values in the profile
1428 for (int i = 0, c = 3, last = profl[1]; i < last; i++)
1432 if (isStructureProfile)
1434 // todo can we encode a structure pair as an int, like codons?
1435 dc[0] = (char) profl[c++];
1436 dc[1] = (char) profl[c++];
1439 else if (isCdnaProfile)
1441 CodingUtils.decodeCodon2(profl[c++], dc);
1446 dc[0] = (char) profl[c++];
1449 // next profl[] position is profile % for the character(s)
1451 double newHeight = normaliseFactor * scale * profl[c++];
1454 * Set character colour as per alignment colour scheme; use the
1455 * codon translation if a cDNA profile
1457 Color colour = null;
1460 final String codonTranslation = ResidueProperties
1462 colour = profcolour.findColour(codonTranslation.charAt(0),
1467 colour = profcolour.findColour(dc[0], column, null);
1469 g.setColor(colour == Color.white ? Color.lightGray : colour);
1471 // Debug - render boxes around characters
1472 // g.setColor(Color.red);
1473 // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
1475 // g.setColor(profcolour.findColour(dc[0]).darker());
1477 double sx = 1f * charWidth / fm.charsWidth(dc, 0, dc.length);
1478 double sy = newHeight / asc;
1479 double newAsc = asc * sy;
1480 double newDec = dec * sy;
1481 // it is not necessary to recalculate lm for the new font.
1482 // note: lm.getBaselineOffsets()[lm.getBaselineIndex()]) must be 0
1483 // by definition. Was:
1484 // int hght = (int) (ht + (newAsc - newDec);
1485 // - lm.getBaselineOffsets()[lm.getBaselineIndex()]));
1487 if (Platform.isJS())
1490 * SwingJS does not implement font.deriveFont()
1491 * so use a scaling transform to draw instead,
1492 * this is off by a very small amount
1494 final int hght = (int) (ht2 + (newAsc - newDec));
1495 Graphics2D gg = (Graphics2D) g;
1496 int xShift = (int) Math.round(x * charWidth / sx);
1497 int yShift = (int) Math.round(hght / sy);
1498 gg.transform(AffineTransform.getScaleInstance(sx, sy));
1499 gg.drawString(s, xShift, yShift);
1501 AffineTransform.getScaleInstance(1D / sx, 1D / sy));
1507 * Java ('normal') method is to scale the font to fit
1509 final int hght = (int) (ht + (newAsc - newDec));
1511 .deriveFont(AffineTransform.getScaleInstance(sx, sy));
1513 g.drawChars(dc, 0, dc.length, x * charWidth, hght);
1521 if (_aa.threshold != null)
1523 g.setColor(_aa.threshold.colour);
1524 Graphics2D g2 = (Graphics2D) g;
1525 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1526 BasicStroke.JOIN_ROUND, 3f, new float[]
1530 - ((_aa.threshold.value - min) / range) * _aa.graphHeight);
1531 g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1532 g2.setStroke(new BasicStroke());
1536 // used by overview window
1537 public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1538 Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1540 eRes = Math.min(eRes, aa_annotations.length);
1541 g.setColor(Color.white);
1542 g.fillRect(0, 0, width, y);
1543 g.setColor(new Color(0, 0, 180));
1547 for (int j = sRes; j < eRes; j++)
1549 if (aa_annotations[j] != null)
1551 if (aa_annotations[j].colour == null)
1553 g.setColor(Color.black);
1557 g.setColor(aa_annotations[j].colour);
1560 height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1566 g.fillRect(x, y - height, charWidth, height);
1572 Color getNotCanonicalColor(char lastss)
1578 return new Color(255, 125, 5);
1582 return new Color(245, 115, 10);
1586 return new Color(235, 135, 15);
1590 return new Color(225, 105, 20);
1594 return new Color(215, 145, 30);
1598 return new Color(205, 95, 35);
1602 return new Color(195, 155, 45);
1606 return new Color(185, 85, 55);
1610 return new Color(175, 165, 65);
1614 return new Color(170, 75, 75);
1618 return new Color(160, 175, 85);
1622 return new Color(150, 65, 95);
1626 return new Color(140, 185, 105);
1630 return new Color(130, 55, 110);
1634 return new Color(120, 195, 120);
1638 return new Color(110, 45, 130);
1642 return new Color(100, 205, 140);
1646 return new Color(90, 35, 150);
1650 return new Color(85, 215, 160);
1654 return new Color(75, 25, 170);
1658 return new Color(65, 225, 180);
1662 return new Color(55, 15, 185);
1666 return new Color(45, 235, 195);
1670 return new Color(35, 5, 205);
1674 return new Color(25, 245, 215);
1678 return new Color(15, 0, 225);
1682 return new Color(10, 255, 235);
1686 return new Color(5, 150, 245);
1690 return new Color(0, 80, 255);
1693 System.out.println("This is not a interaction : " + lastss);