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.bin.Jalview;
29 import jalview.datamodel.AlignmentAnnotation;
30 import jalview.datamodel.Annotation;
31 import jalview.datamodel.ColumnSelection;
32 import jalview.datamodel.HiddenColumns;
33 import jalview.datamodel.ProfilesI;
34 import jalview.schemes.ColourSchemeI;
35 import jalview.schemes.NucleotideColourScheme;
36 import jalview.schemes.ResidueProperties;
37 import jalview.schemes.ZappoColourScheme;
38 import jalview.util.Platform;
40 import java.awt.BasicStroke;
41 import java.awt.Color;
43 import java.awt.FontMetrics;
44 import java.awt.Graphics;
45 import java.awt.Graphics2D;
46 import java.awt.Image;
47 import java.awt.geom.AffineTransform;
48 import java.awt.image.ImageObserver;
49 import java.util.BitSet;
50 import java.util.Hashtable;
52 public class AnnotationRenderer
54 private static final int UPPER_TO_LOWER = 'a' - 'A'; // 32
56 private static final int CHAR_A = 'A'; // 65
58 private static final int CHAR_Z = 'Z'; // 90
61 * flag indicating if timing and redraw parameter info should be output
63 private final boolean debugRedraw;
65 private int charWidth, endRes, charHeight;
67 private boolean validCharWidth, hasHiddenColumns;
69 private FontMetrics fm;
71 private final boolean MAC = Platform.isAMac();
73 boolean av_renderHistogram = true, av_renderProfile = true,
74 av_normaliseProfile = false;
76 ResidueShaderI profcolour = null;
78 private ColumnSelection columnSelection;
80 private HiddenColumns hiddenColumns;
82 private ProfilesI hconsensus;
84 private Hashtable[] complementConsensus;
86 private Hashtable[] hStrucConsensus;
88 private boolean av_ignoreGapsConsensus;
91 * attributes set from AwtRenderPanelI
94 * old image used when data is currently being calculated and cannot be
97 private Image fadedImage;
100 * panel being rendered into
102 private ImageObserver annotationPanel;
105 * width of image to render in panel
107 private int imgWidth;
110 * offset to beginning of visible area
115 * offset to end of visible area
117 private int visHeight;
120 * indicate if the renderer should only render the visible portion of the
121 * annotation given the current view settings
123 private boolean useClip = true;
126 * master flag indicating if renderer should ever try to clip. not enabled for
129 private boolean canClip = false;
131 public AnnotationRenderer()
137 * Create a new annotation Renderer
140 * flag indicating if timing and redraw parameter info should be
143 public AnnotationRenderer(boolean debugRedraw)
145 this.debugRedraw = debugRedraw;
149 * Remove any references and resources when this object is no longer required
151 public void dispose()
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);
423 * Render the annotation rows associated with an alignment.
428 * data and view settings to render
430 * destination for graphics
432 * row where a mouse event occured (or -1)
434 * first column that will be drawn
436 * last column that will be drawn
437 * @return true if the fadedImage was used for any alignment annotation rows
438 * currently being calculated
440 public boolean drawComponent(AwtRenderPanelI annotPanel,
441 AlignViewportI av, Graphics g, int activeRow, int startRes,
444 long stime = System.currentTimeMillis();
445 boolean usedFaded = false;
447 // AnnotationPanel needs to implement: ImageObserver, access to
449 updateFromAwtRenderPanel(annotPanel, av);
450 fm = g.getFontMetrics();
451 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
462 boolean validRes = false;
463 boolean validEnd = false;
464 boolean labelAllCols = false;
465 boolean centreColLabels;
466 boolean centreColLabelsDef = av.isCentreColumnLabels();
467 boolean scaleColLabel = false;
468 final AlignmentAnnotation consensusAnnot = av
469 .getAlignmentConsensusAnnotation();
470 final AlignmentAnnotation structConsensusAnnot = av
471 .getAlignmentStrucConsensusAnnotation();
472 final AlignmentAnnotation complementConsensusAnnot = av
473 .getComplementConsensusAnnotation();
474 boolean renderHistogram = true, renderProfile = true,
475 normaliseProfile = false, isRNA = rna;
477 BitSet graphGroupDrawn = new BitSet();
478 int charOffset = 0; // offset for a label
481 int yfrom = 0, f_i = 0, yto = 0, f_to = 0;
482 boolean clipst = false, clipend = false;
483 for (int i = 0; i < aa.length; i++)
485 AlignmentAnnotation row = aa[i];
488 // check if this is a consensus annotation row and set the display
489 // settings appropriately
490 // TODO: generalise this to have render styles for consensus/profile
492 if (row.groupRef != null && row == row.groupRef.getConsensus())
494 renderHistogram = row.groupRef.isShowConsensusHistogram();
495 renderProfile = row.groupRef.isShowSequenceLogo();
496 normaliseProfile = row.groupRef.isNormaliseSequenceLogo();
498 else if (row == consensusAnnot || row == structConsensusAnnot
499 || row == complementConsensusAnnot)
501 renderHistogram = av_renderHistogram;
502 renderProfile = av_renderProfile;
503 normaliseProfile = av_normaliseProfile;
507 renderHistogram = true;
508 // don't need to set render/normaliseProfile since they are not
509 // currently used in any other annotation track renderer
512 Annotation[] row_annotations = row.annotations;
517 centreColLabels = row.centreColLabels || centreColLabelsDef;
518 labelAllCols = row.showAllColLabels;
519 scaleColLabel = row.scaleColLabel;
523 if (!useClip || ((y - charHeight) < visHeight
524 && (y + row.height + charHeight * 2) >= sOffset))
525 {// if_in_visible_region
536 if (row.graphGroup > -1 && graphGroupDrawn.get(row.graphGroup))
541 // this is so that we draw the characters below the graph
546 iconOffset = charHeight - fm.getDescent();
550 else if (row.hasText)
552 iconOffset = charHeight - fm.getDescent();
560 if (row.autoCalculated && av.isCalculationInProgress(row))
564 g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0,
565 y - row.height, imgWidth, y, annotationPanel);
566 g.setColor(Color.black);
567 // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
573 * else if (annotationPanel.av.updatingConservation &&
574 * aa[i].label.equals("Conservation")) {
576 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
577 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
578 * annotationPanel.imgWidth, y, annotationPanel);
580 * g.setColor(Color.black); //
581 * g.drawString("Calculating Conservation.....",20, y-row.height/2);
583 * continue; } else if (annotationPanel.av.updatingConservation &&
584 * aa[i].label.equals("Quality")) {
586 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
587 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
588 * annotationPanel.imgWidth, y, annotationPanel);
589 * g.setColor(Color.black); // /
590 * g.drawString("Calculating Quality....",20, y-row.height/2);
594 // first pass sets up state for drawing continuation from left-hand
597 x = (startRes == 0) ? 0 : -1;
598 while (x < endRes - startRes)
600 if (hasHiddenColumns)
602 column = hiddenColumns.visibleToAbsoluteColumn(startRes + x);
603 if (column > row_annotations.length - 1)
610 column = startRes + x;
613 if ((row_annotations == null)
614 || (row_annotations.length <= column)
615 || (row_annotations[column] == null))
623 final String displayChar = validRes
624 ? row_annotations[column].displayCharacter
630 g.setColor(Color.red);
632 if (columnSelection != null)
634 if (columnSelection.contains(column))
636 g.fillRect(x * charWidth, y, charWidth, charHeight);
640 if (row.getInvalidStrucPos() > x)
642 g.setColor(Color.orange);
643 g.fillRect(x * charWidth, y, charWidth, charHeight);
645 else if (row.getInvalidStrucPos() == x)
647 g.setColor(Color.orange.darker());
648 g.fillRect(x * charWidth, y, charWidth, charHeight);
650 if (validCharWidth && validRes && displayChar != null
651 && (displayChar.length() > 0))
653 Graphics2D gg = ((Graphics2D) g);
654 AffineTransform oldTransform = gg.getTransform();
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;
690 gg.translate(xPos, yPos);
694 * use a scaling transform to make the label narrower
695 * (JalviewJS doesn't have Font.deriveFont(AffineTransform))
698 AffineTransform.getScaleInstance(fmScaling, 1.0));
700 if (column == 0 || row.graph > 0)
702 gg.drawString(displayChar, 0, 0);
704 else if (row_annotations[column - 1] == null || (labelAllCols
705 || !displayChar.equals(
706 row_annotations[column - 1].displayCharacter)
707 || (displayChar.length() < 2
708 && row_annotations[column].secondaryStructure == ' ')))
710 gg.drawString(displayChar, 0, 0);
712 gg.translate(-xPos, -yPos);
713 gg.setTransform(oldTransform);
718 char ss = validRes ? row_annotations[column].secondaryStructure
723 // distinguish between forward/backward base-pairing
724 if (displayChar.indexOf(')') > -1)
733 if ((displayChar.indexOf(']') > -1))
741 // distinguish between forward/backward base-pairing
742 if (displayChar.indexOf('}') > -1)
750 // distinguish between forward/backward base-pairing
751 if (displayChar.indexOf('<') > -1)
757 if (isRNA && (ss >= CHAR_A) && (ss <= CHAR_Z))
759 // distinguish between forward/backward base-pairing
760 int ssLowerCase = ss + UPPER_TO_LOWER;
761 // TODO would .equals() be safer here? or charAt(0)?
762 if (displayChar.indexOf(ssLowerCase) > -1)
764 ss = (char) ssLowerCase;
768 if (!validRes || (ss != lastSS))
774 int nb_annot = x - temp;
775 // System.out.println("\t type :"+lastSS+"\t x :"+x+"\t nbre
776 // annot :"+nb_annot);
779 case '(': // Stem case for RNA secondary structure
780 case ')': // and opposite direction
781 drawStemAnnot(g, row_annotations, lastSSX, x, y,
782 iconOffset, startRes, column, validRes, validEnd);
789 drawHelixAnnot(g, row_annotations, lastSSX, x, y,
790 iconOffset, startRes, column, validRes,
794 // no break if isRNA - falls through to drawNotCanonicalAnnot!
798 drawSheetAnnot(g, row_annotations, lastSSX, x, y,
799 iconOffset, startRes, column, validRes,
803 // no break if isRNA - fall through to drawNotCanonicalAnnot!
862 Color nonCanColor = getNotCanonicalColor(lastSS);
863 drawNotCanonicalAnnot(g, nonCanColor, row_annotations,
864 lastSSX, x, y, iconOffset, startRes, column,
869 g.setColor(Color.gray);
870 g.fillRect(lastSSX, y + 6 + iconOffset,
871 (x * charWidth) - lastSSX, 2);
886 lastSSX = (x * charWidth);
893 if (column >= row_annotations.length)
895 column = row_annotations.length - 1;
902 if ((row_annotations == null) || (row_annotations.length <= column)
903 || (row_annotations[column] == null))
921 drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
922 startRes, column, validRes, validEnd);
925 // no break if isRNA - fall through to drawNotCanonicalAnnot!
930 drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
931 startRes, column, validRes, validEnd);
934 // no break if isRNA - fall through to drawNotCanonicalAnnot!
937 case ')': // Stem case for RNA secondary structure
939 drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
940 startRes, column, validRes, validEnd);
997 // System.out.println(lastSS);
998 Color nonCanColor = getNotCanonicalColor(lastSS);
999 drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX,
1000 x, y, iconOffset, startRes, column, validRes, validEnd);
1003 drawGlyphLine(g, row_annotations, lastSSX, x, y, iconOffset,
1004 startRes, column, validRes, validEnd);
1009 if (row.graph > 0 && row.graphHeight > 0)
1011 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1013 if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
1015 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1016 // computed efficiently for all visible labels
1017 float groupmax = -999999, groupmin = 9999999;
1018 for (int gg = 0; gg < aa.length; gg++)
1020 if (aa[gg].graphGroup != row.graphGroup)
1027 aa[gg].visible = false;
1029 if (aa[gg].graphMax > groupmax)
1031 groupmax = aa[gg].graphMax;
1033 if (aa[gg].graphMin < groupmin)
1035 groupmin = aa[gg].graphMin;
1039 for (int gg = 0; gg < aa.length; gg++)
1041 if (aa[gg].graphGroup == row.graphGroup)
1043 drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
1044 endRes, y, groupmin, groupmax, row.graphHeight);
1048 graphGroupDrawn.set(row.graphGroup);
1052 drawLineGraph(g, row, row_annotations, startRes, endRes, y,
1053 row.graphMin, row.graphMax, row.graphHeight);
1056 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1058 drawBarGraph(g, row, row_annotations, startRes, endRes,
1059 row.graphMin, row.graphMax, y, renderHistogram,
1060 renderProfile, normaliseProfile);
1066 if (clipst && !clipend)
1070 } // end if_in_visible_region
1071 if (row.graph > 0 && row.hasText)
1088 "Start clip at : " + yfrom + " (index " + f_i + ")");
1093 "End clip at : " + yto + " (index " + f_to + ")");
1097 System.err.println("Annotation Rendering time:"
1098 + (System.currentTimeMillis() - stime));
1105 public static final Color GLYPHLINE_COLOR = Color.gray;
1107 public static final Color SHEET_COLOUR = Color.green;
1109 public static final Color HELIX_COLOUR = Color.red;
1111 public static final Color STEM_COLOUR = Color.blue;
1113 private Color sdNOTCANONICAL_COLOUR;
1115 void drawGlyphLine(Graphics g, Annotation[] row, int lastSSX, int x,
1116 int y, int iconOffset, int startRes, int column, boolean validRes,
1119 g.setColor(GLYPHLINE_COLOR);
1120 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
1123 void drawSheetAnnot(Graphics g, Annotation[] row,
1125 int lastSSX, int x, int y, int iconOffset, int startRes,
1126 int column, boolean validRes, boolean validEnd)
1128 g.setColor(SHEET_COLOUR);
1130 if (!validEnd || !validRes || row == null || row[column] == null
1131 || row[column].secondaryStructure != 'E')
1133 g.fillRect(lastSSX, y + 4 + iconOffset, (x * charWidth) - lastSSX - 4,
1137 { (x * charWidth) - 4, (x * charWidth) - 4, (x * charWidth) },
1139 { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset },
1144 g.fillRect(lastSSX, y + 4 + iconOffset, (x + 1) * charWidth - lastSSX,
1150 void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x,
1151 int y, int iconOffset, int startRes, int column, boolean validRes,
1154 g.setColor(HELIX_COLOUR);
1156 int sCol = (lastSSX / charWidth)
1157 + hiddenColumns.visibleToAbsoluteColumn(startRes);
1159 int x2 = (x * charWidth);
1163 int ofs = charWidth / 2;
1164 // Off by 1 offset when drawing rects and ovals
1165 // to offscreen image on the MAC
1166 g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1, 8, 8, 8);
1167 if (sCol == 0 || row[sCol - 1] == null
1168 || row[sCol - 1].secondaryStructure != 'H')
1173 // g.setColor(Color.orange);
1174 g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1 - ofs + 1, 8,
1177 if (!validRes || row[column] == null
1178 || row[column].secondaryStructure != 'H')
1184 // g.setColor(Color.magenta);
1185 g.fillRoundRect(lastSSX + ofs, y + 4 + iconOffset,
1186 x2 - x1 - ofs + 1, 8, 0, 0);
1193 if (sCol == 0 || row[sCol - 1] == null
1194 || row[sCol - 1].secondaryStructure != 'H')
1196 g.fillArc(lastSSX, y + 4 + iconOffset, charWidth, 8, 90, 180);
1197 x1 += charWidth / 2;
1200 if (!validRes || row[column] == null
1201 || row[column].secondaryStructure != 'H')
1203 g.fillArc((x * charWidth) - charWidth, y + 4 + iconOffset, charWidth,
1205 x2 -= charWidth / 2;
1208 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
1211 void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1212 Annotation[] aa_annotations, int sRes, int eRes, int y, float min,
1213 float max, int graphHeight)
1215 if (sRes > aa_annotations.length)
1222 // Adjustment for fastpaint to left
1228 eRes = Math.min(eRes, aa_annotations.length);
1236 float range = max - min;
1241 y2 = y - (int) ((0 - min / range) * graphHeight);
1244 g.setColor(Color.gray);
1245 g.drawLine(x - charWidth, y2, (eRes - sRes + 1) * charWidth, y2);
1247 eRes = Math.min(eRes, aa_annotations.length);
1250 int aaMax = aa_annotations.length - 1;
1252 while (x < eRes - sRes)
1255 if (hasHiddenColumns)
1257 column = hiddenColumns.visibleToAbsoluteColumn(column);
1265 if (aa_annotations[column] == null
1266 || aa_annotations[column - 1] == null)
1272 if (aa_annotations[column].colour == null)
1274 g.setColor(Color.black);
1278 g.setColor(aa_annotations[column].colour);
1281 y1 = y - (int) (((aa_annotations[column - 1].value - min) / range)
1283 y2 = y - (int) (((aa_annotations[column].value - min) / range)
1286 g.drawLine(x * charWidth - charWidth / 2, y1,
1287 x * charWidth + charWidth / 2, y2);
1291 if (_aa.threshold != null)
1293 g.setColor(_aa.threshold.colour);
1294 Graphics2D g2 = (Graphics2D) g;
1295 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1296 BasicStroke.JOIN_ROUND, 3f, new float[]
1299 y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1300 g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1301 g2.setStroke(new BasicStroke());
1305 @SuppressWarnings("unused")
1306 void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1307 Annotation[] aa_annotations, int sRes, int eRes, float min,
1308 float max, int y, boolean renderHistogram, boolean renderProfile,
1309 boolean normaliseProfile)
1311 if (sRes > aa_annotations.length)
1315 Font ofont = g.getFont();
1316 eRes = Math.min(eRes, aa_annotations.length);
1318 int x = 0, y1 = y, y2 = y;
1320 float range = max - min;
1324 y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1327 g.setColor(Color.gray);
1329 g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
1332 int aaMax = aa_annotations.length - 1;
1333 while (x < eRes - sRes)
1336 if (hasHiddenColumns)
1338 column = hiddenColumns.visibleToAbsoluteColumn(column);
1346 if (aa_annotations[column] == null)
1351 if (aa_annotations[column].colour == null)
1353 g.setColor(Color.black);
1357 g.setColor(aa_annotations[column].colour);
1360 y1 = y - (int) (((aa_annotations[column].value - min) / (range))
1363 if (renderHistogram)
1367 g.fillRect(x * charWidth, y2, charWidth, y1 - y2);
1371 g.fillRect(x * charWidth, y1, charWidth, y2 - y1);
1374 // draw profile if available
1379 * {profile type, #values, total count, char1, pct1, char2, pct2...}
1381 int profl[] = getProfileFor(_aa, column);
1383 // just try to draw the logo if profl is not null
1384 if (profl != null && profl[2] != 0)
1386 boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
1387 boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
1388 float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1389 double htn = normaliseProfile ? _aa.graphHeight : (y2 - y1);// aa.graphHeight;
1392 * Render a single base for a sequence profile, a base pair for
1393 * structure profile, and a triplet for a cdna profile
1395 char[] dc = new char[isStructureProfile ? 2
1396 : (isCdnaProfile ? 3 : 1)];
1398 // lm is not necessary - we can just use fm - could be off by no more
1400 // LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1401 // System.out.println(asc + " " + dec + " " + (asc - lm.getAscent())
1402 // + " " + (dec - lm.getDescent()));
1404 double asc = fm.getAscent();
1405 double dec = fm.getDescent();
1406 double fht = fm.getHeight();
1408 double scale = 1f / (normaliseProfile ? profl[2] : 100f);
1409 // float ofontHeight = 1f / fm.getAscent();// magnify to fill box
1412 * Traverse the character(s)/percentage data in the array
1417 // profl[1] is the number of values in the profile
1418 for (int i = 0, c = 3, last = profl[1]; i < last; i++)
1422 if (isStructureProfile)
1424 // todo can we encode a structure pair as an int, like codons?
1425 dc[0] = (char) profl[c++];
1426 dc[1] = (char) profl[c++];
1429 else if (isCdnaProfile)
1431 CodingUtils.decodeCodon2(profl[c++], dc);
1436 dc[0] = (char) profl[c++];
1439 // next profl[] position is profile % for the character(s)
1441 double newHeight = htn * scale * profl[c++];
1444 * Set character colour as per alignment colour scheme; use the
1445 * codon translation if a cDNA profile
1447 Color colour = null;
1450 final String codonTranslation = ResidueProperties
1452 colour = profcolour.findColour(codonTranslation.charAt(0),
1457 colour = profcolour.findColour(dc[0], column, null);
1459 g.setColor(colour == Color.white ? Color.lightGray : colour);
1461 // Debug - render boxes around characters
1462 // g.setColor(Color.red);
1463 // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
1465 // g.setColor(profcolour.findColour(dc[0]).darker());
1467 double sx = 1f * charWidth / fm.charsWidth(dc, 0, dc.length);
1468 double sy = newHeight / asc;
1469 double newAsc = asc * sy;
1470 double newDec = dec * sy;
1471 // it is not necessary to recalculate lm for the new font.
1472 // note: lm.getBaselineOffsets()[lm.getBaselineIndex()]) must be 0
1473 // by definition. Was:
1474 // int hght = (int) (ht + (newAsc - newDec);
1475 // - lm.getBaselineOffsets()[lm.getBaselineIndex()]));
1480 * SwingJS does not implement font.deriveFont()
1481 * so use a scaling transform to draw instead,
1482 * this is off by a very small amount
1484 final int hght = (int) (ht2 + (newAsc - newDec));
1485 Graphics2D gg = (Graphics2D) g.create();
1487 int xShift = (int) (x * charWidth / sx);
1488 int yShift = (int) (hght / sy);
1489 gg.transform(AffineTransform.getScaleInstance(sx, sy));
1490 gg.drawString(s, xShift, yShift);
1496 final int hght = (int) (ht + (newAsc - newDec));
1498 .deriveFont(AffineTransform.getScaleInstance(sx, sy));
1500 g.drawChars(dc, 0, dc.length, x * charWidth, hght);
1509 if (_aa.threshold != null)
1511 g.setColor(_aa.threshold.colour);
1512 Graphics2D g2 = (Graphics2D) g;
1513 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1514 BasicStroke.JOIN_ROUND, 3f, new float[]
1518 - ((_aa.threshold.value - min) / range) * _aa.graphHeight);
1519 g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1520 g2.setStroke(new BasicStroke());
1524 // used by overview window
1525 public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1526 Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1528 eRes = Math.min(eRes, aa_annotations.length);
1529 g.setColor(Color.white);
1530 g.fillRect(0, 0, width, y);
1531 g.setColor(new Color(0, 0, 180));
1535 for (int j = sRes; j < eRes; j++)
1537 if (aa_annotations[j] != null)
1539 if (aa_annotations[j].colour == null)
1541 g.setColor(Color.black);
1545 g.setColor(aa_annotations[j].colour);
1548 height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1554 g.fillRect(x, y - height, charWidth, height);
1560 Color getNotCanonicalColor(char lastss)
1566 return new Color(255, 125, 5);
1570 return new Color(245, 115, 10);
1574 return new Color(235, 135, 15);
1578 return new Color(225, 105, 20);
1582 return new Color(215, 145, 30);
1586 return new Color(205, 95, 35);
1590 return new Color(195, 155, 45);
1594 return new Color(185, 85, 55);
1598 return new Color(175, 165, 65);
1602 return new Color(170, 75, 75);
1606 return new Color(160, 175, 85);
1610 return new Color(150, 65, 95);
1614 return new Color(140, 185, 105);
1618 return new Color(130, 55, 110);
1622 return new Color(120, 195, 120);
1626 return new Color(110, 45, 130);
1630 return new Color(100, 205, 140);
1634 return new Color(90, 35, 150);
1638 return new Color(85, 215, 160);
1642 return new Color(75, 25, 170);
1646 return new Color(65, 225, 180);
1650 return new Color(55, 15, 185);
1654 return new Color(45, 235, 195);
1658 return new Color(35, 5, 205);
1662 return new Color(25, 245, 215);
1666 return new Color(15, 0, 225);
1670 return new Color(10, 255, 235);
1674 return new Color(5, 150, 245);
1678 return new Color(0, 80, 255);
1681 System.out.println("This is not a interaction : " + lastss);